diff --git a/weed/shell/command_fs_verify_restore.go b/weed/shell/command_fs_verify_restore.go index 86f82054be..b453482e42 100644 --- a/weed/shell/command_fs_verify_restore.go +++ b/weed/shell/command_fs_verify_restore.go @@ -24,7 +24,15 @@ import ( // restoreUploadFn stores data under a new file id; gzipped says data is // already gzip-compressed and must be stored that way. -type restoreUploadFn func(path util.FullPath, collection string, data []byte, gzipped bool, mimeType string) (fileId string, err error) +type restoreUploadFn func(target restoreTarget, data []byte, gzipped bool, mimeType string) (fileId string, err error) + +// restoreTarget is where a restored needle is assigned: the entry's path and +// lifetime, so the new needle expires with the entry, and the collection. +type restoreTarget struct { + path util.FullPath + collection string + ttlSec int32 +} type restorableChunk struct { fileId string @@ -79,10 +87,11 @@ func (c *commandFsVerify) restoreEntry(path util.FullPath, attr *filer_pb.FuseAt return false, true } + ttlSec := current.GetAttributes().GetTtlSec() newFileIds := make(map[string]string) var uploaded []orphanedNeedle for _, r := range ready { - fileId, err := c.uploadRestored(path, r.collection, r.needle.Data, r.needle.IsCompressed(), restoreMimeType(r.needle)) + fileId, err := c.uploadRestored(restoreTarget{path, r.collection, ttlSec}, r.needle.Data, r.needle.IsCompressed(), restoreMimeType(r.needle)) if err != nil { fmt.Fprintf(c.writer, "not restored %s chunk %s: %v\n", path, r.fileId, err) continue @@ -113,7 +122,7 @@ func (c *commandFsVerify) restoreEntry(path util.FullPath, attr *filer_pb.FuseAt if !chunk.IsChunkManifest || applied[chunk.GetFileIdString()] != "" { continue } - rewritten, manifestUploads, err := c.rewriteRestoredManifest(path, ready[0].collection, chunk, newFileIds, applied) + rewritten, manifestUploads, err := c.rewriteRestoredManifest(restoreTarget{path, ready[0].collection, ttlSec}, chunk, newFileIds, applied) if err != nil { fmt.Fprintf(c.writer, "restore %s: rewrite manifest %s: %v\n", path, chunk.GetFileIdString(), err) continue @@ -259,7 +268,7 @@ func newOrphanedNeedle(fileId string) orphanedNeedle { // manifests included, and stores the result as a new manifest needle. It // returns nil when the manifest holds none of newFileIds, and the manifest // needles it uploaded otherwise. -func (c *commandFsVerify) rewriteRestoredManifest(path util.FullPath, collection string, manifest *filer_pb.FileChunk, newFileIds, applied map[string]string) (*filer_pb.FileChunk, []orphanedNeedle, error) { +func (c *commandFsVerify) rewriteRestoredManifest(target restoreTarget, manifest *filer_pb.FileChunk, newFileIds, applied map[string]string) (*filer_pb.FileChunk, []orphanedNeedle, error) { if len(manifest.CipherKey) > 0 { return nil, nil, errors.New("encrypted manifest") } @@ -270,7 +279,7 @@ func (c *commandFsVerify) rewriteRestoredManifest(path util.FullPath, collection repointed := make(map[string]string) var uploaded []orphanedNeedle fail := func(err error) (*filer_pb.FileChunk, []orphanedNeedle, error) { - deleteOrphanedNeedles(c.env, path, uploaded, true) + deleteOrphanedNeedles(c.env, target.path, uploaded, true) return nil, nil, err } for i, chunk := range held { @@ -279,7 +288,7 @@ func (c *commandFsVerify) rewriteRestoredManifest(path util.FullPath, collection held[i] = withFileId(chunk, fileId) repointed[oldFileId] = fileId } else if chunk.IsChunkManifest { - nested, nestedUploads, err := c.rewriteRestoredManifest(path, collection, chunk, newFileIds, repointed) + nested, nestedUploads, err := c.rewriteRestoredManifest(target, chunk, newFileIds, repointed) if err != nil { return fail(err) } @@ -297,7 +306,7 @@ func (c *commandFsVerify) rewriteRestoredManifest(path util.FullPath, collection if err != nil { return fail(err) } - fileId, err := c.uploadRestored(path, collection, blob, false, "") + fileId, err := c.uploadRestored(target, blob, false, "") if err != nil { return fail(err) } @@ -311,13 +320,14 @@ func (c *commandFsVerify) rewriteRestoredManifest(path util.FullPath, collection // uploadRestoredBytes assigns a file id through the filer, uploads data in // the given stored form, and reads it back: the restore only counts when the // volume server holds exactly these bytes, gzipped exactly when asked. -func (c *commandFsVerify) uploadRestoredBytes(path util.FullPath, collection string, data []byte, gzipped bool, mimeType string) (string, error) { +func (c *commandFsVerify) uploadRestoredBytes(target restoreTarget, data []byte, gzipped bool, mimeType string) (string, error) { var assign *filer_pb.AssignVolumeResponse err := c.env.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error { resp, err := client.AssignVolume(context.Background(), &filer_pb.AssignVolumeRequest{ Count: 1, - Collection: collection, - Path: path.Sanitized(), + Collection: target.collection, + TtlSec: target.ttlSec, + Path: target.path.Sanitized(), ExpectedDataSize: uint64(len(data)), }) if err != nil { @@ -360,7 +370,7 @@ func (c *commandFsVerify) uploadRestoredBytes(path util.FullPath, collection str err = readBackRestored(url, assign.FileId, data, gzipped) } if err != nil { - deleteOrphanedNeedles(c.env, path, []orphanedNeedle{newOrphanedNeedle(assign.FileId)}, true) + deleteOrphanedNeedles(c.env, target.path, []orphanedNeedle{newOrphanedNeedle(assign.FileId)}, true) return "", fmt.Errorf("upload %s: %w", assign.FileId, err) } return assign.FileId, nil diff --git a/weed/shell/command_fs_verify_restore_test.go b/weed/shell/command_fs_verify_restore_test.go index a2786ee11e..7fac77a39b 100644 --- a/weed/shell/command_fs_verify_restore_test.go +++ b/weed/shell/command_fs_verify_restore_test.go @@ -47,6 +47,7 @@ func (s *restoreTestFiler) CreateEntry(_ context.Context, req *filer_pb.CreateEn type restoreTestUpload struct { collection string + ttlSec int32 data []byte gzipped bool } @@ -73,8 +74,8 @@ func newRestoreTestCommand(t *testing.T, filer *restoreTestFiler, restoreDir str t.Cleanup(source.Close) c.restore = source uploads := &[]restoreTestUpload{} - c.uploadRestored = func(_ util.FullPath, collection string, data []byte, gzipped bool, _ string) (string, error) { - *uploads = append(*uploads, restoreTestUpload{collection: collection, data: append([]byte(nil), data...), gzipped: gzipped}) + c.uploadRestored = func(target restoreTarget, data []byte, gzipped bool, _ string) (string, error) { + *uploads = append(*uploads, restoreTestUpload{collection: target.collection, ttlSec: target.ttlSec, data: append([]byte(nil), data...), gzipped: gzipped}) return needle.NewFileId(9, uint64(100+len(*uploads)), 0xabcd).String(), nil } return c, output, uploads @@ -95,7 +96,7 @@ func TestFsVerifyRestoreEntry(t *testing.T) { v.close() mtime := time.Now().Add(-time.Hour).Unix() - attr := &filer_pb.FuseAttributes{Mtime: mtime, Md5: []byte("md5")} + attr := &filer_pb.FuseAttributes{Mtime: mtime, Md5: []byte("md5"), TtlSec: 86400} restorable := testChunk(7, 1, 0x11, 0, len(plain), etagOf(plain)) healthy := testChunk(7, 3, 0x33, int64(len(plain)), 10, "") compressed := testChunk(7, 2, 0x22, int64(len(plain))+10, len(text), etagOf(text)) @@ -123,7 +124,8 @@ func TestFsVerifyRestoreEntry(t *testing.T) { if len(*uploads) != 2 { t.Fatalf("%d uploads, want 2", len(*uploads)) } - if u := (*uploads)[0]; !bytes.Equal(u.data, plain) || u.gzipped || u.collection != "pics" { + // the new needle is assigned with the entry's lifetime + if u := (*uploads)[0]; !bytes.Equal(u.data, plain) || u.gzipped || u.collection != "pics" || u.ttlSec != 86400 { t.Errorf("plain upload = %+v", u) } // the stored gzip bytes go up as they are, marked gzipped @@ -226,7 +228,7 @@ func TestFsVerifyRestoreChunkInsideManifest(t *testing.T) { manifest := testChunk(8, 1, 0x88, 0, len(data)+100, "manifest-etag") manifest.IsChunkManifest = true path := util.NewFullPath("/dir", "file") - attr := &filer_pb.FuseAttributes{Mtime: time.Now().Add(-time.Hour).Unix()} + attr := &filer_pb.FuseAttributes{Mtime: time.Now().Add(-time.Hour).Unix(), TtlSec: 3600} filer := &restoreTestFiler{stubFilerServer: stubFilerServer{ entries: map[string]*filer_pb.Entry{string(path): {Name: "file", Attributes: attr, Chunks: []*filer_pb.FileChunk{manifest}}}, volumeUrls: map[string]string{"8": startStubManifestVolume(t, map[*filer_pb.FileChunk][]*filer_pb.FileChunk{manifest: held})}, @@ -237,6 +239,9 @@ func TestFsVerifyRestoreChunkInsideManifest(t *testing.T) { if !restored || len(filer.updates) != 1 || len(*uploads) != 2 { t.Fatalf("restored=%v updates=%d uploads=%d\n%s", restored, len(filer.updates), len(*uploads), output) } + if (*uploads)[1].ttlSec != 3600 { + t.Errorf("manifest assigned with ttl %d, want the entry's 3600", (*uploads)[1].ttlSec) + } m := &filer_pb.FileChunkManifest{} if err := proto.Unmarshal((*uploads)[1].data, m); err != nil { t.Fatal(err)