Repository navigation
fix(posix): move the tree size along with items moved on disk - #853
NickWalters wants to merge 5 commits into
Conversation
|
@NickWalters I see that you created a lot of PRs related to POSIX metadata inconsistencies. Thx for the time to look into this. I'll set the labels. Can I get your opinion on #860 which tries to address aflakyness we see in CI runs. It conflicts with this PR and you might already have a stronger opinion which way to move forward. Personally, I think the propagation inconsistencies at least are better addressed by the async propagation implementation but @aduffeck ran into issues with it as well. In any case we should try to review these as they improve and fix bugs in our core usecase:
|
|
Thanks for the labels and the summary. Short version: #860 can still count a file's size twice. I'd recalculate the folder from disk instead. The problem with #860Only the watcher updates
A rescan from disk ends the same way. Both specs below fail on #860 in every run, and spec 1 passes on main. Revision restore has the same gap, and What I'd do insteadWhen a size change can't be propagated, mark the parent dirty and let the existing dirty-folder rescan recalculate it from disk. That needs no new attribute, and That is commit e2cc6b9 in this PR, with three specs that break the propagation: an edited file, a move into a broken folder and a move out of one. If you prefer #860, I'll drop it. Neither fix recovers a failure above the direct parent: that waits for a full rescan. If you'd rather keep the checkpoint, the upload, restore and rescan paths have to update it too, and Spec 1: API upload (tree_test.go, "Watching tree" / "of files")It("keeps the tree size right after an API upload of a file assimilated from disk", func() {
fileRef := &provider.Reference{ResourceId: env.SpaceRootRes, Path: subtree + "/api.txt"}
dirRef := &provider.Reference{ResourceId: env.SpaceRootRes, Path: subtree}
treeSize := func(g Gomega) uint64 {
dir, err := env.Lookup.NodeFromResource(env.Ctx, dirRef)
g.Expect(err).ToNot(HaveOccurred())
size, err := dir.GetTreeSize(env.Ctx)
g.Expect(err).ToNot(HaveOccurred())
return size
}
realBS, err := posixbs.New(env.Root)
Expect(err).ToNot(HaveOccurred())
env.Blobstore.On("Upload", mock.Anything, mock.Anything, mock.Anything).Return(func(n *node.Node, src, ct string) error { return realBS.Upload(n, src, ct) })
env.Permissions.On("AssemblePermissions", mock.Anything, mock.Anything, mock.Anything).Return(node.OwnerPermissions(), nil)
Expect(os.WriteFile(root+"/api.txt", []byte("hello"), 0600)).To(Succeed())
Eventually(func(g Gomega) { g.Expect(treeSize(g)).To(Equal(uint64(5))) }).Should(Succeed())
content := []byte("hello world!")
ids, err := env.Fs.InitiateUpload(env.Ctx, fileRef, int64(len(content)), map[string]string{})
Expect(err).ToNot(HaveOccurred())
_, err = env.Fs.Upload(env.Ctx, storage.UploadRequest{
Ref: &provider.Reference{Path: "/" + ids["simple"]},
Body: io.NopCloser(bytes.NewReader(content)),
Length: int64(len(content)),
}, nil)
Expect(err).ToNot(HaveOccurred())
_, err = env.Lookup.NodeFromResource(env.Ctx, fileRef)
Expect(err).ToNot(HaveOccurred())
Eventually(func(g Gomega) { g.Expect(treeSize(g)).To(Equal(uint64(12))) }).Should(Succeed())
Consistently(func(g Gomega) { g.Expect(treeSize(g)).To(Equal(uint64(12))) }, 3*time.Second).Should(Succeed())
Expect(os.WriteFile(root+"/api.txt", []byte("hello world!!"), 0600)).To(Succeed())
Eventually(func(g Gomega) { g.Expect(treeSize(g)).To(Equal(uint64(13))) }).Should(Succeed())
Consistently(func(g Gomega) { g.Expect(treeSize(g)).To(Equal(uint64(13))) }, 2*time.Second).Should(Succeed())
})Needs Spec 2: heal from disk (same place)Updated: it now waits for treesize 13 before It("keeps the tree size right after a heal from disk", func() {
fileRef := &provider.Reference{ResourceId: env.SpaceRootRes, Path: subtree + "/grow.txt"}
dirRef := &provider.Reference{ResourceId: env.SpaceRootRes, Path: subtree}
treeSize := func(g Gomega) uint64 {
dir, err := env.Lookup.NodeFromResource(env.Ctx, dirRef)
g.Expect(err).ToNot(HaveOccurred())
size, err := dir.GetTreeSize(env.Ctx)
g.Expect(err).ToNot(HaveOccurred())
return size
}
Expect(os.WriteFile(root+"/grow.txt", []byte("hello"), 0600)).To(Succeed())
Eventually(func(g Gomega) { g.Expect(treeSize(g)).To(Equal(uint64(5))) }).Should(Succeed())
dir, err := env.Lookup.NodeFromResource(env.Ctx, dirRef)
Expect(err).ToNot(HaveOccurred())
Expect(env.Lookup.MetadataBackend().Set(env.Ctx, dir, prefixes.TreesizeAttr, []byte("not-a-number"))).To(Succeed())
Expect(os.WriteFile(root+"/grow.txt", []byte("hello world!"), 0600)).To(Succeed())
Eventually(func(g Gomega) {
n, err := env.Lookup.NodeFromResource(env.Ctx, fileRef)
g.Expect(err).ToNot(HaveOccurred())
p, err := n.Xattr(env.Ctx, prefixes.PropagatedSizeAttr)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(n.Blobsize).To(Equal(int64(12)))
g.Expect(string(p)).To(Equal("5"))
}).Should(Succeed())
// heal from disk the way startup does
Expect(env.Tree.WarmupIDCache(root, true, false)).To(Succeed())
Eventually(func(g Gomega) { g.Expect(treeSize(g)).To(Equal(uint64(12))) }).Should(Succeed())
Expect(os.WriteFile(root+"/grow.txt", []byte("hello world!!"), 0600)).To(Succeed())
Eventually(func(g Gomega) {
n, err := env.Lookup.NodeFromResource(env.Ctx, fileRef)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(n.Blobsize).To(Equal(int64(13)))
}).Should(Succeed())
Eventually(func(g Gomega) { g.Expect(treeSize(g)).To(Equal(uint64(13))) }).Should(Succeed())
Consistently(func(g Gomega) { g.Expect(treeSize(g)).To(Equal(uint64(13))) }, 3*time.Second).Should(Succeed())
}) |
|
I pushed 8c9cb1e with fixes from another review:
|
When the watcher assimilated an item that was moved to another directory on disk, updateFile only propagated the change of the item's own size. The old parents kept the item's size and the new parents never got it, so folder sizes and quota drifted apart while the space total stayed right. updateFile now treats a stored parent id that differs from the parent on disk as a move. Like Tree.Move, it moves the item's size from the old parents to the new ones. It adds the current size to the new parents first, so a file that changed during the move is counted with its new size. A full scan, like the one at startup, calculates the tree sizes from disk but kept the stale parent id of an item moved while nothing watched. The next assimilation of that item would then move its size a second time, so the scan now stores the new parent id when it also covers the old parent. Tree.Move renamed first and set the parent id last. A watcher assimilation in between would now also move the size, and a cancelled request left the move half done. So Tree.Move sets the parent id and name right after the rename and finishes its bookkeeping without the request's cancellation.
When the watcher could not propagate the size diff of an item, the diff was lost. blobsize and the parent id already had their new values, so a later assimilation of the item saw no diff and no move, and the parents stayed wrong until a full scan. The same happened to the old parents of a moved item when taking its size off them failed. Such a failure now marks the affected parents dirty and recalculates their tree sizes from disk with the dirty-only warmup. For a moved item these are the new parent and, if it still holds the size, the old parent. blobsize stays the only baseline, so the API paths need no change.
Tree.Move renamed an item and only then wrote its new parent id, without holding the item's lock. A watcher that assimilated the item in between took it for a move on disk and moved its size a second time. Move now holds the item's lock from the rename until the parent id is written. A full walk only updated the parent id of an item moved on disk when the id cache already knew the old parent. With a cold cache the walk can visit the new parent first, so the item kept its old parent id and its next assimilation moved the size again. The walk now tries those items again once it is done. Two recalculations after failed propagations could walk the same folder at the same time, both read the old tree size and propagate the same diff. They now run one at a time. Two specs checked a common ancestor with Consistently before they waited for its size to settle. They wait for it first now.
…e deleted Move writes the new parent id right after the rename. If deleting the old id cache entry then failed, Move returned before it moved the tree sizes, and the watcher didn't move them either, because the item already had its new parent id. Log the error and go on, like a failed CacheID right after it.
523ec16 to
c73c996
Compare
|
Rebased onto main now that #849, #808 and #809 are in. The only changes are the conflict fixes. From my side this is ready to merge. If you go with #860 instead, tell me and I'll drop the recalculation commit (now 7fbd8a8). |
Tree.Move read the tree size of a moved folder after it released the folder's lock, and through the node the request loaded before the move. A size change that propagated through the folder after the unlock went to the new parent and was also part of the size that Move carried, so the new parent counted it twice. A size read through an older node missed changes since the request loaded it. Move now reads the tree size, or the blobsize of a file, from the metadata backend while it still holds the lock. Fixes opencloud-eu#886
Fixes opencloud-eu/opencloud#3109, together with #849. Fixes #886.
Problem
On a posixfs storage with the watcher on, a file or folder moved to another folder can leave its size in the old folder. The new folder never gets it. The space total stays right, but folder sizes drift apart. In cs3org#3109 one folder shows 51 GB for 49 GB on disk, and another shows much less than it holds.
There are three ways to get there:
mv a/x b/. The watcher assimilates the moved item, butupdateFileonly propagates a change of the item's own size. For a move that is 0.Tree.Moverenames first. With a cancelled context,IDCache.DeleteByPathfails andMovereturns before it sets the parent id or moves the size. The watcher then handles it like case 1. The log in Add missing condition for permission checks on spaces cs3org/reva#3109 showscontext cancelederrors during large moves in the web UI.A restart fixes the sizes on 7.2.1 and later. On 7.2.0 (reva v2.46.6) the startup scan skipped the whole storage root, which is probably why a restart didn't help the reporter.
How to reproduce
aandb, and put a file ina.mv a/file b/on disk.akeeps the size of the file, andbstays at 0.Fix
updateFiletreats a stored parent id that differs from the parent on disk as a move. LikeTree.Move, it moves the size of the item from the old parents to the new ones. It adds the current size to the new parents first, so a file that changed during the move is counted with its new size. Then it takes the stored size off the old parents.Tree.Movewrites the parent id and name right after the rename. Until the id cache is updated,newNoderesolves to the old path, so the write addresses the item by path. A watcher assimilation that runs during the id cache update (slow for large folders) now sees a known item and doesn't move the size a second time. After the rename,Moveusescontext.WithoutCancel, likeupload.godoes, so a cancelled request can't leave the move half done.WarmupIDCachewalk stores the new parent id of an item that was moved while nothing watched, if the walk also covers the old parent. Otherwise the next assimilation of that item would move its size again.blobsizeand the parent id already had their new values, so the change or the move was lost until a full scan. This is the alternative to fix(posix): retry propagating a file's size when propagation fails #860 discussed below. If you prefer fix(posix): retry propagating a file's size when propagation fails #860, I'll drop this commit.Tree.Movereads the size it moves while it still holds the item's lock, and from the metadata backend instead of the node the request loaded. Before, a size change that propagated through a moved folder right after the unlock was counted twice in the new parent (posix: Tree.Move reads the moved folder's tree size outside its lock #886).Tests
Eight new specs. All of them fail without this change and pass with it:
tree_test.go, through the watcher: a folder and a file moved on disk to another folder, and a file that changes size right after its move.tree_non_watching_test.go:Tree.Movewith a cancelled context, and a move on disk that a full walk picks up before the file is edited. The second one callsassimilatethrough a newexport_test.go.tree_test.go, for 7fbd8a8: a propagation that fails for an edited file, for a move into a folder, and for a move out of a folder. Each breaks a folder's tree size so that propagating to it fails, and checks that the folders get the right size without the file being touched again. They pass 30 of 30 runs.tree_non_watching_test.go, for 38a42e6: a folder whose size changes after the request loaded it is moved with its current size.I also ran API moves of a folder with 1500 files while the watcher was on. Before the
Tree.Movechange, the size moved twice in about one run of five. After it, 7 of 7 runs were correct. That check depends on timing, so it is not part of the PR.go test -race ./pkg/storage/fs/posix/...passes locally, and golangci-lint is clean on the package.Notes
inotifywaitwithKillOthers, so two test runs on one machine stop each other's watchers. That is posix watcher kills every inotifywait process of its user at startup #852.