mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-10-31 01:10:41 +01:00
* filer.backup: add -s3FlattenVersions to mirror the current S3 view Versioned buckets are stored as <key>.versions/v_<versionId> files plus a latest-version pointer on the .versions directory, so a backup mirror held internal version files and could never be restored through the S3 API. With -s3FlattenVersions the backup maps events on .versions directories onto the flattened object key: a new latest version uploads to <key>, a latest delete marker or a cleared pointer deletes <key> when -doDeleteFiles is set, and the suspended-versioning null object resolves to the regular path. Version files themselves are never mirrored. The initial snapshot seeds only the version the pointer names. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * filer.backup: serialize flattened writes per object key Same-path non-barrier dir updates run concurrently in the metadata processor, so two pointer updates for one object -- or a pointer update and a regular-path write to it -- could overlap and let the older upload finish last. Lock on the flattened key: versions events lock the object key, every other event its own path, via a striped mutex. * filer.backup: apply exclusions to the flattened object key File-name and path excludes ran against the .versions entry, so an excluded object was still copied flat and its deletion still removed the destination copy. Match the rules against the flattened key on both the apply and the versions-dir-removal paths. * filer.backup: re-resolve the pointer on version-file events A tag or attribute update on the current version touches the v_* file, not the .versions pointer, so dropping every version-file event left the mirror with stale tags until the next pointer flip. Re-resolve the pointer on each version-file event; noncurrent versions still stay out. * filer.backup: tolerate vanished versions dirs in flattened snapshot The flattened seed path returned source errors before the existing missing-source policy ran, so one version deleted mid-walk could force a full re-walk. Apply the same 404/superseded skip as the generic seed branch, and cover the review fixes with tests. * filer.backup: lock both flattened keys an event touches A rename or delete can retire one flattened key while writing another, so serialize on the old- and new-side keys together. Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * filer.backup: refresh the flattened key only for the still-current child A v_* event on a non-pointed version cannot move the flattened key, so skip the re-upload; tag and attribute updates on the current version still flow through. Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * replication: let resolved-latest writes bypass the source staleness check A filer sink skips a CreateEntry whose source path no longer holds the entry, but a flattened write lands on a key whose source path is not the one it was read from, so the check dropped every flattened upload. Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * filer.backup: reclaim destination chunks on flattened deletes Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * filer.backup: route regular-path events for versioned keys through the pointer A null-object write or delete at the regular path can coexist with a .versions pointer that names a live version; the pointer decides the flattened key, so deleting the null object must not remove it. Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * filer.backup: make the checkpoint identity cover the flattening mode Resuming a checkpoint taken in the other mode would skip the flattened re-seed and leave mirrored version files behind; a mode change now starts a fresh snapshot. Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test: cover flattened regular-path routing and per-mode checkpoints Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * filer.backup: resolve both sides of a flattened rename * test: cover flattened renames with one versioned side * replication: let resolved-latest writes overwrite a newer destination The stale-destination skip trusted a newer destination mtime, but a resolved-latest write can legitimately install an older version over the copy of a since-deleted latest. Skip only on provably identical content (matching chunk provenance, or equal ETag with full byte coverage). * filer.backup: rescan .versions when the latest null object is gone Deleting ?versionId=null removes the regular-path object without touching the null-is-latest pointer, so the flattened key kept its stale copy. Fall back to the newest remaining version (or a flat delete), the same rescan the S3 read path applies. * replication: compare inline and remote content in the destination-holds checks A chunk-less entry keeps its bytes in Content and a remote-only entry in the object RemoteEntry points at, but the identical-content shortcuts only compared chunk lists and chunk-derived ETags. Two different inline files looked identical, so a flattened re-copy or a same-mtime rewrite kept the previous version's bytes. Compare Content bytes and remote object identity wherever the checks claim the destination already holds the reference's content. * filer.backup: paginate the null-latest .versions rescan A limit=0 listing is capped at DirListingLimit server-side, and old-format version names sort oldest-first, so a history longer than one page left the flattened copy on an older version after the null object was removed. Page through the directory like the S3 read path does. * replication: require matching writable metadata for resolved-latest skips A skip keeps the destination's stored metadata, so identical bytes were not enough: stale attributes or extended entries (e.g. a superseded version id) survived under equal content. Compare the fields a rewrite controls — attributes minus filer-assigned inode/crtime, with the store's atime/ctime zero-defaults applied — plus the extended map, in both the provenance and ETag fast paths. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * replication: require the extended key to exist when comparing metadata * replication: ignore a stored rule TTL when the resolved write carries none * filer.backup: rename -s3FlattenVersions to -s3.flattenVersions Match the -s3.* flag naming convention used by the other s3 options. --------- Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
356 lines
14 KiB
Go
356 lines
14 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"path"
|
|
"regexp"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/replication/sink"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"github.com/seaweedfs/seaweedfs/weed/util/wildcard"
|
|
)
|
|
|
|
// s3VersionFlattener mirrors the current S3 view of versioned buckets into the
|
|
// sink: under each "<key>.versions" directory only the latest version is
|
|
// written to "<key>", so the destination always holds a plain restorable copy.
|
|
// Version files never reach the sink themselves — the latest-version pointer
|
|
// on the .versions directory decides what the flattened key holds.
|
|
type s3VersionFlattener struct {
|
|
filerSource filer_pb.FilerClient
|
|
dataSink sink.ReplicationSink
|
|
sourcePath string
|
|
targetPath string
|
|
doDeleteFiles bool
|
|
excludePaths []string
|
|
reExcludeFileName *regexp.Regexp
|
|
excludeFileNames []*wildcard.WildcardMatcher
|
|
excludePathPatterns []*wildcard.WildcardMatcher
|
|
flatLocks [256]sync.Mutex
|
|
}
|
|
|
|
func isVersionsDirName(name string) bool {
|
|
return strings.HasSuffix(name, s3_constants.VersionsFolder)
|
|
}
|
|
|
|
func insideVersionsDir(dir string) bool {
|
|
return isVersionsDirName(path.Base(dir))
|
|
}
|
|
|
|
// lockFor serializes the handling of one event by the key the flattened mirror
|
|
// writes to: versions-related events lock their object key, everything else its
|
|
// own path, so a pointer update and a regular-path write to the same object
|
|
// cannot overlap. Both sides of the event are locked, since a rename or delete
|
|
// can retire one flattened key while writing another. Non-barrier dir updates
|
|
// run concurrently in the metadata processor, which would otherwise let an
|
|
// older flattened upload finish last.
|
|
func (f *s3VersionFlattener) lockFor(resp *filer_pb.SubscribeMetadataResponse) func() {
|
|
oldKey, newKey := eventFlatKeys(resp)
|
|
if oldKey == "" {
|
|
oldKey = newKey
|
|
}
|
|
if newKey == "" {
|
|
newKey = oldKey
|
|
}
|
|
if newKey == "" {
|
|
oldKey, newKey = resp.Directory, resp.Directory
|
|
}
|
|
a := util.HashStringToLong(oldKey) & int64(len(f.flatLocks)-1)
|
|
b := util.HashStringToLong(newKey) & int64(len(f.flatLocks)-1)
|
|
if a == b {
|
|
f.flatLocks[a].Lock()
|
|
return f.flatLocks[a].Unlock
|
|
}
|
|
if b < a {
|
|
a, b = b, a
|
|
}
|
|
f.flatLocks[a].Lock()
|
|
f.flatLocks[b].Lock()
|
|
return func() {
|
|
f.flatLocks[b].Unlock()
|
|
f.flatLocks[a].Unlock()
|
|
}
|
|
}
|
|
|
|
func eventFlatKeys(resp *filer_pb.SubscribeMetadataResponse) (string, string) {
|
|
message := resp.EventNotification
|
|
return flatKeyOf(resp.Directory, message.OldEntry),
|
|
flatKeyOf(filer_pb.MetadataEventTargetDirectory(resp), message.NewEntry)
|
|
}
|
|
|
|
func flatKeyOf(dir string, entry *filer_pb.Entry) string {
|
|
if entry == nil {
|
|
return ""
|
|
}
|
|
if insideVersionsDir(dir) && strings.HasPrefix(entry.Name, "v_") {
|
|
return string(objectKeyOf(util.FullPath(dir)))
|
|
}
|
|
if entry.IsDirectory && isVersionsDirName(entry.Name) {
|
|
return string(objectKeyOf(util.FullPath(dir).Child(entry.Name)))
|
|
}
|
|
return string(util.FullPath(dir).Child(entry.Name))
|
|
}
|
|
|
|
// process handles the versions-related part of one metadata event and reports
|
|
// whether the event was consumed. Version files under a .versions directory
|
|
// are dropped outright; create/update/delete of the .versions directory
|
|
// itself is mapped onto the flattened object key through its latest-version
|
|
// pointer. Anything else is left for the generic event path.
|
|
func (f *s3VersionFlattener) process(resp *filer_pb.SubscribeMetadataResponse) (bool, error) {
|
|
message := resp.EventNotification
|
|
if message == nil {
|
|
return false, nil
|
|
}
|
|
targetDir := filer_pb.MetadataEventTargetDirectory(resp)
|
|
|
|
name := ""
|
|
if message.NewEntry != nil {
|
|
name = message.NewEntry.Name
|
|
} else if message.OldEntry != nil {
|
|
name = message.OldEntry.Name
|
|
}
|
|
if strings.HasPrefix(name, "v_") && (insideVersionsDir(resp.Directory) || insideVersionsDir(targetDir)) {
|
|
// A version file changed; only a change to the version the pointer
|
|
// still names can move the flattened key (e.g. a tag update), so
|
|
// re-resolve the pointer and refresh just for that child.
|
|
versionsDir := resp.Directory
|
|
if !insideVersionsDir(versionsDir) {
|
|
versionsDir = targetDir
|
|
}
|
|
dirEntry, _, _, err := filer_pb.GetEntry(context.Background(), f.filerSource, util.FullPath(versionsDir))
|
|
if err != nil {
|
|
if errors.Is(err, filer_pb.ErrNotFound) {
|
|
return true, nil
|
|
}
|
|
return true, err
|
|
}
|
|
if name != string(dirEntry.Extended[s3_constants.ExtLatestVersionFileNameKey]) {
|
|
return true, nil
|
|
}
|
|
return true, f.applyLatest(util.FullPath(versionsDir), dirEntry, false, message.Signatures)
|
|
}
|
|
|
|
oldVersionsDir := ""
|
|
newVersionsDir := ""
|
|
if message.OldEntry != nil && message.OldEntry.IsDirectory && isVersionsDirName(message.OldEntry.Name) {
|
|
oldVersionsDir = string(util.FullPath(resp.Directory).Child(message.OldEntry.Name))
|
|
}
|
|
if message.NewEntry != nil && message.NewEntry.IsDirectory && isVersionsDirName(message.NewEntry.Name) {
|
|
newVersionsDir = string(util.FullPath(targetDir).Child(message.NewEntry.Name))
|
|
}
|
|
if oldVersionsDir == "" && newVersionsDir == "" {
|
|
return f.processRegularPath(resp, targetDir, message)
|
|
}
|
|
|
|
if newVersionsDir != "" {
|
|
if err := f.applyLatest(util.FullPath(newVersionsDir), message.NewEntry, message.OldEntry == nil, message.Signatures); err != nil {
|
|
return true, err
|
|
}
|
|
}
|
|
// The versions directory is gone (deleted outright or renamed away):
|
|
// whatever the flattened key held is no longer current.
|
|
if oldVersionsDir != "" && oldVersionsDir != newVersionsDir {
|
|
objectKey := objectKeyOf(util.FullPath(oldVersionsDir))
|
|
if f.isExcludedObject(objectKey, util.FullPath(oldVersionsDir), message.OldEntry) {
|
|
return true, nil
|
|
}
|
|
if err := f.deleteObject(objectKey, message.Signatures); err != nil {
|
|
return true, err
|
|
}
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func objectKeyOf(versionsDir util.FullPath) util.FullPath {
|
|
dir, name := versionsDir.DirAndName()
|
|
return util.FullPath(dir).Child(strings.TrimSuffix(name, s3_constants.VersionsFolder))
|
|
}
|
|
|
|
// processRegularPath guards the generic event path for keys that carry a
|
|
// .versions directory: a regular-path entry (the null object, or its delete)
|
|
// can coexist with a pointer that names a live version, and only the pointer
|
|
// decides the flattened key — e.g. deleting the null object must not remove a
|
|
// flattened key whose current version is live. A rename touches two keys, so
|
|
// each side is resolved on its own; when exactly one side is versioned the
|
|
// other is handed to the generic path as a lone delete or create. Keys with
|
|
// no .versions sibling are left to the generic path entirely.
|
|
func (f *s3VersionFlattener) processRegularPath(resp *filer_pb.SubscribeMetadataResponse, targetDir string, message *filer_pb.EventNotification) (bool, error) {
|
|
oldHandled, err := f.refreshIfVersioned(util.FullPath(resp.Directory), message.OldEntry, message.Signatures)
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
newHandled := oldHandled
|
|
if !filer_pb.IsUpdate(resp) {
|
|
newHandled, err = f.refreshIfVersioned(util.FullPath(targetDir), message.NewEntry, message.Signatures)
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
}
|
|
if !filer_pb.IsRename(resp) || oldHandled == newHandled {
|
|
return oldHandled || newHandled, nil
|
|
}
|
|
if oldHandled {
|
|
// The flattened old key keeps whatever its pointer still names; the
|
|
// generic path only needs to create the moved entry.
|
|
message.OldEntry = nil
|
|
} else {
|
|
// The flattened old key held the moved file; the generic path only
|
|
// needs to delete it.
|
|
message.NewEntry = nil
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
// refreshIfVersioned re-resolves the flattened key of dir/entry.Name when a
|
|
// .versions directory sits next to it, reporting whether that sibling exists.
|
|
// The pointer decides what the flattened key holds, so a regular-path event
|
|
// only refreshes through it and never writes the flattened key directly.
|
|
func (f *s3VersionFlattener) refreshIfVersioned(dir util.FullPath, entry *filer_pb.Entry, signatures []int32) (bool, error) {
|
|
if entry == nil || entry.IsDirectory {
|
|
return false, nil
|
|
}
|
|
versionsDir := dir.Child(entry.Name + s3_constants.VersionsFolder)
|
|
dirEntry, _, _, err := filer_pb.GetEntry(context.Background(), f.filerSource, versionsDir)
|
|
if err != nil {
|
|
if errors.Is(err, filer_pb.ErrNotFound) {
|
|
return false, nil
|
|
}
|
|
return true, err
|
|
}
|
|
return true, f.applyLatest(versionsDir, dirEntry, false, signatures)
|
|
}
|
|
|
|
// applyLatest writes the object a .versions pointer currently names to the
|
|
// flattened key: the latest version file, a delete marker (flat delete), the
|
|
// null object at the regular path, or — once every version is gone and the
|
|
// pointer is cleared — nothing.
|
|
func (f *s3VersionFlattener) applyLatest(versionsDir util.FullPath, dirEntry *filer_pb.Entry, isCreate bool, signatures []int32) error {
|
|
objectKey := objectKeyOf(versionsDir)
|
|
if f.isExcludedObject(objectKey, versionsDir, dirEntry) {
|
|
return nil
|
|
}
|
|
|
|
extended := dirEntry.Extended
|
|
switch {
|
|
case string(extended[s3_constants.ExtLatestVersionIsDeleteMarker]) == "true":
|
|
return f.deleteObject(objectKey, signatures)
|
|
case string(extended[s3_constants.ExtNullVersionIsLatestKey]) == "true":
|
|
return f.uploadNullLatest(objectKey, versionsDir, signatures)
|
|
default:
|
|
if latestName := string(extended[s3_constants.ExtLatestVersionFileNameKey]); latestName != "" {
|
|
return f.uploadLatest(objectKey, versionsDir.Child(latestName), signatures)
|
|
}
|
|
// A create event lands before the pointer is written; an update with
|
|
// no pointer means the last version was removed.
|
|
if isCreate {
|
|
return nil
|
|
}
|
|
return f.deleteObject(objectKey, signatures)
|
|
}
|
|
}
|
|
|
|
// uploadNullLatest flattens the regular-path null object while the pointer
|
|
// names it latest. A ?versionId=null delete removes that object without
|
|
// touching the pointer, so on a miss the newest remaining version wins (or a
|
|
// flat delete when none survives) — the same rescan the S3 read path falls
|
|
// back to.
|
|
func (f *s3VersionFlattener) uploadNullLatest(objectKey, versionsDir util.FullPath, signatures []int32) error {
|
|
if _, _, _, err := filer_pb.GetEntry(context.Background(), f.filerSource, objectKey); err == nil {
|
|
return f.uploadLatest(objectKey, objectKey, signatures)
|
|
} else if !errors.Is(err, filer_pb.ErrNotFound) {
|
|
return err
|
|
}
|
|
latestName, err := f.latestVersionFileName(versionsDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if latestName == "" {
|
|
return f.deleteObject(objectKey, signatures)
|
|
}
|
|
return f.uploadLatest(objectKey, versionsDir.Child(latestName), signatures)
|
|
}
|
|
|
|
// latestVersionFileName returns the name of the newest version file under
|
|
// versionsDir by version-id order — delete markers included, the upload
|
|
// resolves them to a flat delete — or "" when no version entry remains. It
|
|
// paginates: the filer caps one request at DirListingLimit, and old-format
|
|
// version names sort oldest-first, so a single page can miss the true latest.
|
|
func (f *s3VersionFlattener) latestVersionFileName(versionsDir util.FullPath) (string, error) {
|
|
var latestName, latestId string
|
|
err := f.filerSource.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
|
startFrom := ""
|
|
for {
|
|
var lastName string
|
|
var pageCount uint32
|
|
if err := filer_pb.SeaweedList(context.Background(), client, string(versionsDir), "", func(entry *filer_pb.Entry, _ bool) error {
|
|
pageCount++
|
|
if entry == nil {
|
|
return nil
|
|
}
|
|
lastName = entry.Name
|
|
versionId := s3api.VersionIdFromEntry(entry)
|
|
if versionId != "" && (latestId == "" || s3api.CompareVersionIds(versionId, latestId) < 0) {
|
|
latestId, latestName = versionId, entry.Name
|
|
}
|
|
return nil
|
|
}, startFrom, false, filer.PaginationSize); err != nil {
|
|
return err
|
|
}
|
|
if pageCount < filer.PaginationSize || lastName == "" {
|
|
return nil
|
|
}
|
|
startFrom = lastName
|
|
}
|
|
})
|
|
return latestName, err
|
|
}
|
|
|
|
func (f *s3VersionFlattener) uploadLatest(objectKey, sourceKey util.FullPath, signatures []int32) error {
|
|
entry, _, _, err := filer_pb.GetEntry(context.Background(), f.filerSource, sourceKey)
|
|
if err != nil {
|
|
if errors.Is(err, filer_pb.ErrNotFound) {
|
|
// The pointed-at version (or null object) is not readable yet or is
|
|
// already gone again; its own event or a later pointer update
|
|
// converges the flattened key.
|
|
glog.V(1).Infof("flatten %s: source %s not found: %v", objectKey, sourceKey, err)
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
if string(entry.Extended[s3_constants.ExtDeleteMarkerKey]) == "true" {
|
|
return f.deleteObject(objectKey, signatures)
|
|
}
|
|
var mTime int64
|
|
if entry.Attributes != nil {
|
|
mTime = entry.Attributes.Mtime
|
|
}
|
|
return sink.CreateEntryLatest(f.dataSink, destKey(f.dataSink, f.targetPath, f.sourcePath, objectKey, mTime), entry, signatures)
|
|
}
|
|
|
|
// isExcludedObject applies the source scope and exclusion rules to the
|
|
// flattened object key — including its own name, so a file-name pattern that
|
|
// excludes "k" also excludes the flattened "k" even though the event names
|
|
// "k.versions".
|
|
func (f *s3VersionFlattener) isExcludedObject(objectKey, versionsDir util.FullPath, dirEntry *filer_pb.Entry) bool {
|
|
dir, name := objectKey.DirAndName()
|
|
return !util.IsEqualOrUnder(string(objectKey), f.sourcePath) ||
|
|
matchesExcludePath(string(objectKey), f.excludePaths) ||
|
|
matchesExcludePath(string(versionsDir), f.excludePaths) ||
|
|
isEntryExcluded(dir, dirEntry, f.reExcludeFileName, f.excludeFileNames, f.excludePathPatterns) ||
|
|
isEntryExcluded(dir, &filer_pb.Entry{Name: name}, f.reExcludeFileName, f.excludeFileNames, f.excludePathPatterns)
|
|
}
|
|
|
|
func (f *s3VersionFlattener) deleteObject(objectKey util.FullPath, signatures []int32) error {
|
|
if !f.doDeleteFiles || f.dataSink.IsIncremental() {
|
|
return nil
|
|
}
|
|
return sink.DeleteEntry(f.dataSink, destKey(f.dataSink, f.targetPath, f.sourcePath, objectKey, 0), false, true, false, signatures)
|
|
}
|