mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
* filer, mount: keep a hard-linked file to one link id and count its names on the filer A hard link's bookkeeping was done by the client. To link a file a mount reads its entry and sends two writes, an update of the source and a create of the new name, both with a link id and a count of the file's names. Two clients that link one file at the same moment each worked from the entry as it was before the other's writes: - With no link id on the entry yet, each made one up. The second update moved the source to its id, the store took the name off the first, and the first client's new name was left alone on it: one file's chunks under two link ids. - With a link id there, both read the same count and sent it plus one. The record counted one name fewer than the file had. Any write through a name carried the client's copy of the count and replaced the record the same way. Either way the delete of the last counted name deleted the record and the chunks while another name still read them. The filer's own store now counts the names of a hard-linked file itself (countLink): one more when the name written did not hold the link before, the same when it did or when a rename moves it there (WithMovedLink), one for a record that starts. The count sent is not used, and the entry gets the count stored, so the response and the event carry it. The lookup of the name, the count, the record and the name are stored as one change of the record (changeLinkRecord), which DeleteHardLink goes through as well: under a lock per link id, and in a transaction where the store refuses a commit over a change made since it began, run again when it does. A wrapper around a copy of some entries, such as a mount's metadata cache, sees only some of the names and stores the count it is given. UpdateEntry refuses to give a name that holds a hard link another link id that no file has yet (refuseSecondHardLink). The mount sends a file's first link's update as a unary RPC, which keeps that refusal apart from an I/O failure, reads the entry again and links under the id the file has by now. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * filer: take a replicated update's link id without refusing it Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: look a path-specific name up outside the record's transaction Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: check the leveldb open and close it in the hard link test Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: send a refused first link again from the entry as stored Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: keep a first link's refusal over the next filer's error Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: count a hard link's names next to the clients' count instead of in its place The filer's count replaced the one clients send, which lost two things that count did by being worked out ahead: - A link's first write, the update of the source, carries the count with the new name in it already. While the second write is on its way that kept a delete of the source from being taken for the delete of the file's last name (lastLinkDropped goes by the count it read), and the chunks from being deleted under the name about to come. Counted only when it arrived, the new name was stored with chunks that were gone: 104 of 300 rounds of a mount linking a file while another deletes it. - A write of a name the store refused had counted the name already, and sent again counted it once more. The filer now keeps its count of the names in a key of its own next to the record (linkNames), written once the name is stored, and the record gets whichever is higher: the count the client sent or the names counted. It is never lower than what the client alone would have left, and never lower than the names the file has. A record from before has its count recorded as the names when it is first written again. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * mount: send a refused first link again with the file's inline content The second attempt builds the new name's create from the entry as the filer has it by now: attributes, chunks and extended attributes. A file the filer keeps inside its entry has its bytes in Content, and the create is what the filer writes the link's shared record from, so without them the record lost the file's content. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * filer: store a path-specific name before the record's lock is let go A second create of the same new name on this filer took the lock between the first one's count and its name write, missed the name, and counted it again: the file's record and chunks then outlived its last name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: keep a link's names key out of the keys of a directory's entries The key began with the link's random id. One that began as the root's entry keys do in a store keyed by path put the names' 4 bytes in the root's listing, and it failed to decode: listing / and the etcd scan under fsck failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: record a hard link's name before it is stored, and take it off if it is not The names counted for a hard link were recorded once the name was stored. On a store without transactions a failure of that write left the name stored and not counted, and the write sent again found the name there and did not count it either: the names recorded stayed one below the names stored, the side a file is deleted under a name from. They are recorded first now, inside the change of the record that counted them, and a name the store then does not take is counted off again (storeCountedName). A failure halfway leaves them one above the names stored, and the file leaked. A name in a path-specific store is recorded with the record as well, and counted off in a change of its own. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * filer: keep a hard link's name counted when a failed write stored it A write whose deadline passes after the store took the name returns an error, and counting the name off then left the names one below the names stored. A later write with the count from before the link lowered the record to them, and the file was deleted under a name it still had. Read the name back first, and keep it counted where that cannot be told. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: carry the stored RemoteEntry into the name a retried first link adds The retry rebuilds the new name from the entry the file has once another client linked it, but kept the RemoteEntry of the entry first read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: MapleWhiteZZZ <18653045+MapleWhiteZZZ@users.noreply.github.com> Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com> Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
550 lines
19 KiB
Go
550 lines
19 KiB
Go
package filer
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"math"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/viant/ptrie"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/stats"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
)
|
|
|
|
var (
|
|
_ = VirtualFilerStore(&FilerStoreWrapper{})
|
|
_ = Debuggable(&FilerStoreWrapper{})
|
|
_ = EntryScanner(&FilerStoreWrapper{})
|
|
)
|
|
|
|
type VirtualFilerStore interface {
|
|
FilerStore
|
|
DeleteHardLink(ctx context.Context, hardLinkId HardLinkId) error
|
|
DeleteOneEntry(ctx context.Context, entry *Entry) error
|
|
InsertEntryKnownAbsent(ctx context.Context, entry *Entry) error
|
|
AddPathSpecificStore(path string, storeId string, store FilerStore)
|
|
OnBucketCreation(bucket string)
|
|
OnBucketDeletion(bucket string)
|
|
CanDropWholeBucket() bool
|
|
SameActualStore(a, b util.FullPath) bool
|
|
}
|
|
|
|
type FilerStoreWrapper struct {
|
|
defaultStore FilerStore
|
|
pathToStore ptrie.Trie[string]
|
|
storeIdToStore map[string]FilerStore
|
|
hasPathSpecificStore bool // fast check to skip MatchPrefix when no path-specific stores
|
|
|
|
// countsLinks is set on a filer's own store (Filer.SetStore): it holds
|
|
// every name of a hard-linked file, so it counts them itself and lets
|
|
// no write leave the file's link count below that (linkNames). A wrapper
|
|
// around a copy of some entries, such as a mount's metadata cache, stores
|
|
// the count it is given.
|
|
countsLinks bool
|
|
linkLocks [64]sync.Mutex
|
|
}
|
|
|
|
func NewFilerStoreWrapper(store FilerStore) *FilerStoreWrapper {
|
|
if innerStore, ok := store.(*FilerStoreWrapper); ok {
|
|
return innerStore
|
|
}
|
|
return &FilerStoreWrapper{
|
|
defaultStore: store,
|
|
pathToStore: ptrie.New[string](),
|
|
storeIdToStore: make(map[string]FilerStore),
|
|
}
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) CanDropWholeBucket() bool {
|
|
if ba, ok := fsw.defaultStore.(BucketAware); ok {
|
|
return ba.CanDropWholeBucket()
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) OnBucketCreation(bucket string) {
|
|
for _, store := range fsw.storeIdToStore {
|
|
if ba, ok := store.(BucketAware); ok {
|
|
ba.OnBucketCreation(bucket)
|
|
}
|
|
}
|
|
if ba, ok := fsw.defaultStore.(BucketAware); ok {
|
|
ba.OnBucketCreation(bucket)
|
|
}
|
|
}
|
|
func (fsw *FilerStoreWrapper) OnBucketDeletion(bucket string) {
|
|
for _, store := range fsw.storeIdToStore {
|
|
if ba, ok := store.(BucketAware); ok {
|
|
ba.OnBucketDeletion(bucket)
|
|
}
|
|
}
|
|
if ba, ok := fsw.defaultStore.(BucketAware); ok {
|
|
ba.OnBucketDeletion(bucket)
|
|
}
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) AddPathSpecificStore(path string, storeId string, store FilerStore) {
|
|
fsw.storeIdToStore[storeId] = NewFilerStorePathTranslator(path, store)
|
|
err := fsw.pathToStore.Put([]byte(path), storeId)
|
|
if err != nil {
|
|
glog.Fatalf("put path specific store: %v", err)
|
|
}
|
|
fsw.hasPathSpecificStore = true
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) getActualStore(path util.FullPath) (store FilerStore) {
|
|
store = fsw.defaultStore
|
|
// Fast path: skip MatchPrefix if no path-specific stores are configured (common case)
|
|
if !fsw.hasPathSpecificStore {
|
|
return
|
|
}
|
|
if path == "/" || path == "//" {
|
|
return
|
|
}
|
|
var storeId string
|
|
fsw.pathToStore.MatchPrefix([]byte(path), func(key []byte, value string) bool {
|
|
storeId = value
|
|
return false
|
|
})
|
|
if storeId != "" {
|
|
store = fsw.storeIdToStore[storeId]
|
|
}
|
|
return
|
|
}
|
|
|
|
// listsInNameOrder reports whether a listing of dirPath can start at a file
|
|
// name the store did not hand out itself.
|
|
func (fsw *FilerStoreWrapper) listsInNameOrder(dirPath util.FullPath) bool {
|
|
store := fsw.getActualStore(dirPath + "/")
|
|
if t, ok := store.(*FilerStorePathTranslator); ok {
|
|
store = t.actualStore
|
|
}
|
|
u, ok := store.(UnorderedListingStore)
|
|
return !ok || !u.ListsUnordered()
|
|
}
|
|
|
|
// SameActualStore reports whether two paths resolve to the same underlying
|
|
// store. When path-specific stores are configured, different subtrees may
|
|
// be served by different backends.
|
|
func (fsw *FilerStoreWrapper) SameActualStore(a, b util.FullPath) bool {
|
|
return fsw.getActualStore(a) == fsw.getActualStore(b)
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) getDefaultStore() (store FilerStore) {
|
|
return fsw.defaultStore
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) GetName() string {
|
|
return fsw.getDefaultStore().GetName()
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) Initialize(configuration util.Configuration, prefix string) error {
|
|
return fsw.getDefaultStore().Initialize(configuration, prefix)
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) InsertEntry(ctx context.Context, entry *Entry) error {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
ctx = context.WithoutCancel(ctx)
|
|
actualStore := fsw.getActualStore(entry.FullPath)
|
|
stats.FilerStoreCounter.WithLabelValues(actualStore.GetName(), "insert").Inc()
|
|
start := time.Now()
|
|
defer func() {
|
|
stats.FilerStoreHistogram.WithLabelValues(actualStore.GetName(), "insert").Observe(time.Since(start).Seconds())
|
|
}()
|
|
|
|
filer_pb.BeforeEntrySerialization(entry.GetChunks())
|
|
normalizeEntryMimeForStore(entry)
|
|
delete(entry.Extended, ExtKeepRemoteObjectKey)
|
|
|
|
if len(entry.HardLinkId) > 0 {
|
|
glog.V(4).InfofCtx(ctx, "InsertEntry %s has HardLinkId %x counter=%d",
|
|
entry.FullPath, entry.HardLinkId, entry.HardLinkCounter)
|
|
}
|
|
|
|
replacedLink, err := fsw.writeWithHardLink(ctx, entry, actualStore.InsertEntry)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fsw.dropLink(ctx, entry.FullPath, replacedLink)
|
|
return nil
|
|
}
|
|
|
|
// InsertEntryKnownAbsent skips the pre-insert FindEntry path when the caller has
|
|
// already established that the target path does not exist.
|
|
func (fsw *FilerStoreWrapper) InsertEntryKnownAbsent(ctx context.Context, entry *Entry) error {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
ctx = context.WithoutCancel(ctx)
|
|
actualStore := fsw.getActualStore(entry.FullPath)
|
|
stats.FilerStoreCounter.WithLabelValues(actualStore.GetName(), "insert").Inc()
|
|
start := time.Now()
|
|
defer func() {
|
|
stats.FilerStoreHistogram.WithLabelValues(actualStore.GetName(), "insert").Observe(time.Since(start).Seconds())
|
|
}()
|
|
|
|
filer_pb.BeforeEntrySerialization(entry.GetChunks())
|
|
normalizeEntryMimeForStore(entry)
|
|
delete(entry.Extended, ExtKeepRemoteObjectKey)
|
|
|
|
if len(entry.HardLinkId) > 0 {
|
|
glog.V(4).InfofCtx(ctx, "InsertEntryKnownAbsent %s has HardLinkId %x counter=%d",
|
|
entry.FullPath, entry.HardLinkId, entry.HardLinkCounter)
|
|
var names *linkNames
|
|
storeName := func(ctx context.Context) error { return actualStore.InsertEntry(ctx, entry) }
|
|
var store func(context.Context) error
|
|
if actualStore != fsw.defaultStore {
|
|
store = func(ctx context.Context) error {
|
|
return fsw.storeCountedName(ctx, entry, names, false, storeName)
|
|
}
|
|
}
|
|
return fsw.changeLinkRecord(ctx, entry, func(ctx context.Context) error {
|
|
var err error
|
|
if names, err = fsw.saveHardLink(ctx, entry, true); err != nil {
|
|
return fmt.Errorf("setHardLink %x: %v", entry.HardLinkId, err)
|
|
}
|
|
if err = fsw.recordLinkNames(ctx, names); err != nil || store != nil {
|
|
return err // outside the record's transaction: stored after it
|
|
}
|
|
return fsw.storeCountedName(ctx, entry, names, true, storeName)
|
|
}, store)
|
|
}
|
|
|
|
return actualStore.InsertEntry(ctx, entry)
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) UpdateEntry(ctx context.Context, entry *Entry) error {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
ctx = context.WithoutCancel(ctx)
|
|
actualStore := fsw.getActualStore(entry.FullPath)
|
|
stats.FilerStoreCounter.WithLabelValues(actualStore.GetName(), "update").Inc()
|
|
start := time.Now()
|
|
defer func() {
|
|
stats.FilerStoreHistogram.WithLabelValues(actualStore.GetName(), "update").Observe(time.Since(start).Seconds())
|
|
}()
|
|
|
|
filer_pb.BeforeEntrySerialization(entry.GetChunks())
|
|
normalizeEntryMimeForStore(entry)
|
|
delete(entry.Extended, ExtKeepRemoteObjectKey)
|
|
|
|
if len(entry.HardLinkId) > 0 {
|
|
glog.V(4).InfofCtx(ctx, "UpdateEntry %s has HardLinkId %x counter=%d",
|
|
entry.FullPath, entry.HardLinkId, entry.HardLinkCounter)
|
|
}
|
|
|
|
replacedLink, err := fsw.writeWithHardLink(ctx, entry, actualStore.UpdateEntry)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fsw.dropLink(ctx, entry.FullPath, replacedLink)
|
|
return nil
|
|
}
|
|
|
|
func normalizeEntryMimeForStore(entry *Entry) {
|
|
if entry == nil {
|
|
return
|
|
}
|
|
if !entry.IsDirectory() && entry.Mime == "application/octet-stream" {
|
|
entry.Mime = ""
|
|
}
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) FindEntry(ctx context.Context, fp util.FullPath) (entry *Entry, err error) {
|
|
ctx = context.WithoutCancel(ctx)
|
|
actualStore := fsw.getActualStore(fp)
|
|
stats.FilerStoreCounter.WithLabelValues(actualStore.GetName(), "find").Inc()
|
|
start := time.Now()
|
|
defer func() {
|
|
stats.FilerStoreHistogram.WithLabelValues(actualStore.GetName(), "find").Observe(time.Since(start).Seconds())
|
|
}()
|
|
|
|
entry, err = actualStore.FindEntry(ctx, fp)
|
|
// glog.V(4).Infof("FindEntry %s: %v", fp, err)
|
|
if err != nil {
|
|
if fsw.CanDropWholeBucket() && strings.Contains(err.Error(), "Table") && strings.Contains(err.Error(), "doesn't exist") {
|
|
err = filer_pb.ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
fsw.maybeReadHardLink(ctx, entry)
|
|
|
|
filer_pb.AfterEntryDeserialization(entry.GetChunks())
|
|
return
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) DeleteEntry(ctx context.Context, fp util.FullPath) (err error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
ctx = context.WithoutCancel(ctx)
|
|
actualStore := fsw.getActualStore(fp)
|
|
stats.FilerStoreCounter.WithLabelValues(actualStore.GetName(), "delete").Inc()
|
|
start := time.Now()
|
|
defer func() {
|
|
stats.FilerStoreHistogram.WithLabelValues(actualStore.GetName(), "delete").Observe(time.Since(start).Seconds())
|
|
}()
|
|
|
|
existingEntry, findErr := fsw.FindEntry(ctx, fp)
|
|
if findErr != nil && !errors.Is(findErr, filer_pb.ErrNotFound) {
|
|
return findErr
|
|
}
|
|
if existingEntry == nil {
|
|
return nil
|
|
}
|
|
if err := actualStore.DeleteEntry(ctx, fp); err != nil {
|
|
return err
|
|
}
|
|
if ctx.Value("OP") != "MV" {
|
|
// a rename takes the name, link and all, elsewhere
|
|
fsw.dropLink(ctx, fp, existingEntry.HardLinkId)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) DeleteOneEntry(ctx context.Context, existingEntry *Entry) (err error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
ctx = context.WithoutCancel(ctx)
|
|
actualStore := fsw.getActualStore(existingEntry.FullPath)
|
|
stats.FilerStoreCounter.WithLabelValues(actualStore.GetName(), "delete").Inc()
|
|
start := time.Now()
|
|
defer func() {
|
|
stats.FilerStoreHistogram.WithLabelValues(actualStore.GetName(), "delete").Observe(time.Since(start).Seconds())
|
|
}()
|
|
|
|
if err := actualStore.DeleteEntry(ctx, existingEntry.FullPath); err != nil {
|
|
return err
|
|
}
|
|
if ctx.Value("OP") != "MV" {
|
|
// a rename takes the name, link and all, elsewhere
|
|
fsw.dropLink(ctx, existingEntry.FullPath, existingEntry.HardLinkId)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) DeleteFolderChildren(ctx context.Context, fp util.FullPath) (err error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
ctx = context.WithoutCancel(ctx)
|
|
actualStore := fsw.getActualStore(fp + "/")
|
|
stats.FilerStoreCounter.WithLabelValues(actualStore.GetName(), "deleteFolderChildren").Inc()
|
|
start := time.Now()
|
|
defer func() {
|
|
stats.FilerStoreHistogram.WithLabelValues(actualStore.GetName(), "deleteFolderChildren").Observe(time.Since(start).Seconds())
|
|
}()
|
|
|
|
return actualStore.DeleteFolderChildren(ctx, fp)
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc ListEachEntryFunc) (string, error) {
|
|
ctx = context.WithoutCancel(ctx)
|
|
actualStore := fsw.getActualStore(dirPath + "/")
|
|
stats.FilerStoreCounter.WithLabelValues(actualStore.GetName(), "list").Inc()
|
|
start := time.Now()
|
|
defer func() {
|
|
stats.FilerStoreHistogram.WithLabelValues(actualStore.GetName(), "list").Observe(time.Since(start).Seconds())
|
|
}()
|
|
|
|
// glog.V(4).Infof("ListDirectoryEntries %s from %s limit %d", dirPath, startFileName, limit)
|
|
return actualStore.ListDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit, func(entry *Entry) (bool, error) {
|
|
fsw.maybeReadHardLink(ctx, entry)
|
|
filer_pb.AfterEntryDeserialization(entry.GetChunks())
|
|
return eachEntryFunc(entry)
|
|
})
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) ListDirectoryPrefixedEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc ListEachEntryFunc) (lastFileName string, err error) {
|
|
ctx = context.WithoutCancel(ctx)
|
|
actualStore := fsw.getActualStore(dirPath + "/")
|
|
stats.FilerStoreCounter.WithLabelValues(actualStore.GetName(), "prefixList").Inc()
|
|
start := time.Now()
|
|
defer func() {
|
|
stats.FilerStoreHistogram.WithLabelValues(actualStore.GetName(), "prefixList").Observe(time.Since(start).Seconds())
|
|
}()
|
|
if limit > math.MaxInt32-1 {
|
|
limit = math.MaxInt32 - 1
|
|
}
|
|
// glog.V(4).Infof("ListDirectoryPrefixedEntries %s from %s prefix %s limit %d", dirPath, startFileName, prefix, limit)
|
|
adjustedEntryFunc := func(entry *Entry) (bool, error) {
|
|
fsw.maybeReadHardLink(ctx, entry)
|
|
filer_pb.AfterEntryDeserialization(entry.GetChunks())
|
|
return eachEntryFunc(entry)
|
|
}
|
|
lastFileName, err = actualStore.ListDirectoryPrefixedEntries(ctx, dirPath, startFileName, includeStartFile, limit, prefix, adjustedEntryFunc)
|
|
if err == ErrUnsupportedListDirectoryPrefixed {
|
|
lastFileName, err = fsw.prefixFilterEntries(ctx, dirPath, startFileName, includeStartFile, limit, prefix, adjustedEntryFunc)
|
|
}
|
|
return lastFileName, err
|
|
}
|
|
|
|
// ScanEntries delegates to the default store. With path-specific stores
|
|
// configured, entries below dirPath could live in several stores and one
|
|
// snapshot per store is not one snapshot, so the scan is declined. Hard links
|
|
// are resolved inside the snapshot; a store error fails the scan (the per-path
|
|
// copy can be stale), a missing record falls back to it as listings do.
|
|
func (fsw *FilerStoreWrapper) ScanEntries(ctx context.Context, dirPath util.FullPath, fn func(ctx context.Context, entry *Entry) error) error {
|
|
if fsw.hasPathSpecificStore {
|
|
return ErrUnsupportedEntryScan
|
|
}
|
|
scanner, ok := fsw.defaultStore.(EntryScanner)
|
|
if !ok {
|
|
return ErrUnsupportedEntryScan
|
|
}
|
|
stats.FilerStoreCounter.WithLabelValues(fsw.defaultStore.GetName(), "scan").Inc()
|
|
start := time.Now()
|
|
defer func() {
|
|
stats.FilerStoreHistogram.WithLabelValues(fsw.defaultStore.GetName(), "scan").Observe(time.Since(start).Seconds())
|
|
}()
|
|
return scanner.ScanEntries(ctx, dirPath, func(scanCtx context.Context, entry *Entry) error {
|
|
if err := fsw.maybeReadHardLink(scanCtx, entry); err != nil && !errors.Is(err, ErrKvNotFound) {
|
|
return fmt.Errorf("resolve hard link of %s: %w", entry.FullPath, err)
|
|
}
|
|
filer_pb.AfterEntryDeserialization(entry.GetChunks())
|
|
return fn(scanCtx, entry)
|
|
})
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) prefixFilterEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc ListEachEntryFunc) (lastFileName string, err error) {
|
|
actualStore := fsw.getActualStore(dirPath + "/")
|
|
|
|
if prefix == "" {
|
|
return actualStore.ListDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit, eachEntryFunc)
|
|
}
|
|
|
|
// lastFileName must stay at the last name delivered to eachEntryFunc; the
|
|
// raw scan cursor runs ahead over non-matching names and cannot be returned.
|
|
count := int64(0)
|
|
scanName, scanInclusive := startFileName, includeStartFile
|
|
for count < limit {
|
|
var page []*Entry
|
|
if scanName, err = actualStore.ListDirectoryEntries(ctx, dirPath, scanName, scanInclusive, limit, func(entry *Entry) (bool, error) {
|
|
page = append(page, entry)
|
|
return true, nil
|
|
}); err != nil {
|
|
return
|
|
}
|
|
scanInclusive = false
|
|
if len(page) == 0 {
|
|
return
|
|
}
|
|
for _, entry := range page {
|
|
if !strings.HasPrefix(entry.Name(), prefix) {
|
|
continue
|
|
}
|
|
count++
|
|
res, resErr := eachEntryFunc(entry)
|
|
if resErr != nil {
|
|
err = fmt.Errorf("failed to process eachEntryFunc for entry %q: %w", entry.Name(), resErr)
|
|
return
|
|
}
|
|
lastFileName = entry.Name()
|
|
if !res || count >= limit {
|
|
return
|
|
}
|
|
}
|
|
if scanName >= prefix && !strings.HasPrefix(scanName, prefix) {
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// transactionCovers reports whether a transaction of the default store covers
|
|
// the writes to the entries at paths: the store's transactions are real
|
|
// (IsolatingTransactionStore) and it holds those entries. The writes of a
|
|
// path-specific store bypass the default store's transaction. An entry in the
|
|
// default store has its parents there too.
|
|
func (fsw *FilerStoreWrapper) transactionCovers(paths ...util.FullPath) bool {
|
|
isolating, ok := fsw.defaultStore.(IsolatingTransactionStore)
|
|
if !ok || !isolating.IsolatesTransactions() {
|
|
return false
|
|
}
|
|
for _, p := range paths {
|
|
if fsw.getActualStore(p) != fsw.defaultStore {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// refusesConflictingCommits reports whether a transaction of the default store
|
|
// that read and wrote the entry at path is refused when another writer
|
|
// committed that entry meanwhile: the store's commits are (ConflictRefusingStore)
|
|
// and it holds the entry.
|
|
func (fsw *FilerStoreWrapper) refusesConflictingCommits(path util.FullPath) bool {
|
|
refusing, ok := fsw.defaultStore.(ConflictRefusingStore)
|
|
return ok && refusing.RefusesConflictingCommits() && fsw.getActualStore(path) == fsw.defaultStore
|
|
}
|
|
|
|
// overwritesAtOnce reports whether an entry inserted at path replaces what is
|
|
// stored there at once and whole: the default store says that its inserts do
|
|
// (OverwritingStore), and it holds the entry.
|
|
func (fsw *FilerStoreWrapper) overwritesAtOnce(path util.FullPath) bool {
|
|
overwriting, ok := fsw.defaultStore.(OverwritingStore)
|
|
return ok && overwriting.OverwritesAtOnce() && fsw.getActualStore(path) == fsw.defaultStore
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) BeginTransaction(ctx context.Context) (context.Context, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
ctx = context.WithoutCancel(ctx)
|
|
return fsw.getDefaultStore().BeginTransaction(ctx)
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) CommitTransaction(ctx context.Context) error {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
ctx = context.WithoutCancel(ctx)
|
|
return fsw.getDefaultStore().CommitTransaction(ctx)
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) RollbackTransaction(ctx context.Context) error {
|
|
ctx = context.WithoutCancel(ctx)
|
|
return fsw.getDefaultStore().RollbackTransaction(ctx)
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) Shutdown() {
|
|
fsw.getDefaultStore().Shutdown()
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) KvPut(ctx context.Context, key []byte, value []byte) (err error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
ctx = context.WithoutCancel(ctx)
|
|
return fsw.getDefaultStore().KvPut(ctx, key, value)
|
|
}
|
|
func (fsw *FilerStoreWrapper) KvGet(ctx context.Context, key []byte) (value []byte, err error) {
|
|
ctx = context.WithoutCancel(ctx)
|
|
return fsw.getDefaultStore().KvGet(ctx, key)
|
|
}
|
|
func (fsw *FilerStoreWrapper) KvDelete(ctx context.Context, key []byte) (err error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
ctx = context.WithoutCancel(ctx)
|
|
return fsw.getDefaultStore().KvDelete(ctx, key)
|
|
}
|
|
|
|
func (fsw *FilerStoreWrapper) Debug(writer io.Writer) {
|
|
if debuggable, ok := fsw.getDefaultStore().(Debuggable); ok {
|
|
debuggable.Debug(writer)
|
|
}
|
|
}
|