forked from mtusi2026/mtusi2026lab4
feat: two-store reconciler with safe repair and cost measurement (grade 5, track 2)
This commit is contained in:
+47
-1
@@ -6,6 +6,8 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"messenger/internal/config"
|
||||
@@ -79,6 +81,22 @@ func run(command string, args []string) error {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("removed %s\n", args[0])
|
||||
case "seed":
|
||||
if len(args) != 3 {
|
||||
usage()
|
||||
}
|
||||
count, err := strconv.Atoi(args[1])
|
||||
if err != nil || count <= 0 {
|
||||
return fmt.Errorf("count must be a positive integer")
|
||||
}
|
||||
size, err := strconv.Atoi(args[2])
|
||||
if err != nil || size <= 0 {
|
||||
return fmt.Errorf("size must be a positive integer")
|
||||
}
|
||||
if err := seed(ctx, store, args[0], count, size); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("seeded %d objects under %s\n", count, args[0])
|
||||
default:
|
||||
usage()
|
||||
}
|
||||
@@ -105,8 +123,36 @@ func newStore() (*s3.Store, error) {
|
||||
accessKeyID, secretAccessKey, getenv("S3_BUCKET", "messenger-attachments"))
|
||||
}
|
||||
|
||||
func seed(ctx context.Context, store *s3.Store, prefix string, count, size int) error {
|
||||
content := make([]byte, size)
|
||||
workers := 8
|
||||
work := make(chan int)
|
||||
errors := make(chan error, workers)
|
||||
var group sync.WaitGroup
|
||||
for i := 0; i < workers; i++ {
|
||||
group.Add(1)
|
||||
go func() {
|
||||
defer group.Done()
|
||||
for index := range work {
|
||||
key := fmt.Sprintf("%s/%06d", prefix, index)
|
||||
if err := store.Put(ctx, key, bytes.NewReader(content), int64(size), "application/octet-stream"); err != nil {
|
||||
errors <- err
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
for index := 0; index < count; index++ {
|
||||
work <- index
|
||||
}
|
||||
close(work)
|
||||
group.Wait()
|
||||
close(errors)
|
||||
return <-errors
|
||||
}
|
||||
|
||||
func usage() {
|
||||
fmt.Fprintln(os.Stderr, "usage: s3tool put <key> <file> | delete <key> | list [prefix] | buckets | remove-bucket <name>")
|
||||
fmt.Fprintln(os.Stderr, "usage: s3tool put <key> <file> | delete <key> | list [prefix] | buckets | remove-bucket <name> | seed <prefix> <count> <bytes>")
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user