diff --git a/weed/s3api/s3api_object_versioning.go b/weed/s3api/s3api_object_versioning.go index bb75cd5fa1..a0ad83e484 100644 --- a/weed/s3api/s3api_object_versioning.go +++ b/weed/s3api/s3api_object_versioning.go @@ -2430,20 +2430,28 @@ func (s3a *S3ApiServer) healStaleLatestVersionPointer(bucket, normalizedObject s return latestEntry, nil } -// staleVersionPointerGrace leaves a pointer to a recently minted version as it -// is: its file can still be on its way, e.g. when filer.sync applies the +// staleVersionPointerGrace leaves a recently minted or recently stored pointer +// as it is: its file can still be on its way, e.g. when filer.sync applies the // .versions directory before the version file, and a local removal or repair // then would replicate back and delete or roll back the version on the peer. const staleVersionPointerGrace = 10 * time.Minute // versionPointerGraceLeft returns how much of the grace the pointer has left; -// zero means its version file is overdue. +// zero means its version file is overdue. The grace runs from the later of the +// version's mint, stamped by the minting cluster, and the local commit of the +// .versions entry, so neither a clock ahead of the minting cluster nor a sync +// backlog ends it before the file could arrive. func versionPointerGraceLeft(versionsEntry *filer_pb.Entry) time.Duration { - ts := getVersionTimestamp(string(versionsEntry.GetExtended()[s3_constants.ExtLatestVersionIdKey])) - if ts <= 0 { - return 0 + var left time.Duration + for _, ts := range []int64{ + getVersionTimestamp(string(versionsEntry.GetExtended()[s3_constants.ExtLatestVersionIdKey])), + versionsEntry.GetCommitTsNs(), + } { + if ts > 0 { + left = max(left, staleVersionPointerGrace-time.Since(time.Unix(0, ts))) + } } - return min(max(staleVersionPointerGrace-time.Since(time.Unix(0, ts)), 0), staleVersionPointerGrace) + return min(left, staleVersionPointerGrace) } // conditionalUpdateEntryRouted persists the repair on the object's write diff --git a/weed/s3api/s3api_object_versioning_inflight_test.go b/weed/s3api/s3api_object_versioning_inflight_test.go index 4a0b1bcf42..e84d5c0ca9 100644 --- a/weed/s3api/s3api_object_versioning_inflight_test.go +++ b/weed/s3api/s3api_object_versioning_inflight_test.go @@ -19,6 +19,11 @@ func versionPointerEntry(versionId string) *filer_pb.Entry { return &filer_pb.Entry{Extended: map[string][]byte{s3_constants.ExtLatestVersionIdKey: []byte(versionId)}} } +func committedAt(entry *filer_pb.Entry, at time.Time) *filer_pb.Entry { + entry.CommitTsNs = at.UnixNano() + return entry +} + func TestVersionPointerGraceLeft(t *testing.T) { now := time.Now() for _, tc := range []struct { @@ -31,6 +36,9 @@ func TestVersionPointerGraceLeft(t *testing.T) { {"minted within the grace", versionPointerEntry(invertedVersionId(now.Add(-staleVersionPointerGrace + 2*time.Minute))), time.Minute, 2 * time.Minute}, {"minted in the future", versionPointerEntry(invertedVersionId(now.Add(time.Hour))), staleVersionPointerGrace, staleVersionPointerGrace}, {"minted before the grace", versionPointerEntry(invertedVersionId(now.Add(-2 * staleVersionPointerGrace))), 0, 0}, + {"minted before the grace, stored just now", committedAt(versionPointerEntry(invertedVersionId(now.Add(-2*staleVersionPointerGrace))), now), staleVersionPointerGrace - time.Minute, staleVersionPointerGrace}, + {"minted and stored before the grace", committedAt(versionPointerEntry(invertedVersionId(now.Add(-2*staleVersionPointerGrace))), now.Add(-staleVersionPointerGrace)), 0, 0}, + {"null version stored just now", committedAt(versionPointerEntry("null"), now), staleVersionPointerGrace - time.Minute, staleVersionPointerGrace}, {"null version", versionPointerEntry("null"), 0, 0}, {"no pointer", &filer_pb.Entry{}, 0, 0}, } {