Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Move Object] Fs level integration move object #2843

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions internal/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1994,13 +1994,44 @@ func (fs *fileSystem) Rename(
}
return fs.renameNonHierarchicalDir(ctx, oldParent, op.OldName, newParent, op.NewName)
}
return fs.renameFile(ctx, oldParent, op.OldName, child.MinObject, newParent, op.NewName)
if child.Bucket.BucketType() == gcs.Hierarchical {
return fs.renameHierarchicalFile(ctx, oldParent, op.OldName, child.MinObject, newParent, op.NewName)
}
return fs.renameNonHierarchicalFile(ctx, oldParent, op.OldName, child.MinObject, newParent, op.NewName)
}

// LOCKS_EXCLUDED(fs.mu)
// LOCKS_EXCLUDED(oldParent)
// LOCKS_EXCLUDED(newParent)
func (fs *fileSystem) renameHierarchicalFile(ctx context.Context, oldParent inode.DirInode, oldName string, oldObject *gcs.MinObject, newParent inode.DirInode, newName string) error {
oldParent.Lock()
defer oldParent.Unlock()

if newParent != oldParent {
newParent.Lock()
defer newParent.Unlock()
}

newFileName := inode.NewFileName(newParent.Name(), newName)

if _, err := oldParent.RenameFile(ctx, oldObject, newFileName.GcsObjectName()); err != nil {
return fmt.Errorf("RenameFile: while renaming file: %w", err)
}

if err := fs.invalidateChildFileCacheIfExist(oldParent, oldName); err != nil {
return fmt.Errorf("renameHierarchicalFile: while invalidating cache for delete file: %w", err)
}

// Insert new file in type cache.
newParent.InsertFileIntoTypeCache(newName)

return nil
}

// LOCKS_EXCLUDED(fs.mu)
// LOCKS_EXCLUDED(oldParent)
// LOCKS_EXCLUDED(newParent)
func (fs *fileSystem) renameFile(
func (fs *fileSystem) renameNonHierarchicalFile(
ctx context.Context,
oldParent inode.DirInode,
oldName string,
Expand All @@ -2027,7 +2058,7 @@ func (fs *fileSystem) renameFile(
&oldObject.MetaGeneration)

if err := fs.invalidateChildFileCacheIfExist(oldParent, oldObject.Name); err != nil {
return fmt.Errorf("renameFile: while invalidating cache for delete file: %w", err)
return fmt.Errorf("renameNonHierarchicalFile: while invalidating cache for delete file: %w", err)
}

oldParent.Unlock()
Expand Down
68 changes: 68 additions & 0 deletions internal/fs/hns_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,71 @@ func (t *HNSBucketTests) TestCreateLocalFileInSamePathAfterDeletingParentDirecto
_, err = os.Stat(filePath)
assert.NoError(t.T(), err)
}

func (t *HNSBucketTests) TestRenameFileWithSrcFileDoesNotExist() {
oldDirPath := path.Join(mntDir, "file")
newDirPath := path.Join(mntDir, "file_rename")

err := os.Rename(oldDirPath, newDirPath)

assert.Error(t.T(), err)
assert.True(t.T(), strings.Contains(err.Error(), "no such file or directory"))
_, err = os.Stat(newDirPath)
assert.Error(t.T(), err)
assert.True(t.T(), strings.Contains(err.Error(), "no such file or directory"))
}

func (t *HNSBucketTests) TestRenameFileWithDstDestFileExist() {
oldFilePath := path.Join(mntDir, "foo", "file1.txt")
_, err := os.Stat(oldFilePath)
assert.NoError(t.T(), err)
// In the setup phase, we created file1.txt within the bar directory.
newFilePath := path.Join(mntDir, "foo", "file2.txt")
_, err = os.Stat(newFilePath)
assert.NoError(t.T(), err)

err = os.Rename(oldFilePath, newFilePath)

assert.NoError(t.T(), err)
_, err = os.Stat(oldFilePath)
assert.Error(t.T(), err)
assert.True(t.T(), strings.Contains(err.Error(), "no such file or directory"))
}

func (t *HNSBucketTests) TestRenameFileWithDifferentParent() {
oldFilePath := path.Join(mntDir, "foo", "file1.txt")
_, err := os.Stat(oldFilePath)
assert.NoError(t.T(), err)
// In the setup phase, we created file1.txt within the bar directory.
newFilePath := path.Join(mntDir, "bar", "file3.txt")
_, err = os.Stat(newFilePath)
assert.True(t.T(), strings.Contains(err.Error(), "no such file or directory"))

err = os.Rename(oldFilePath, newFilePath)

assert.NoError(t.T(), err)
_, err = os.Stat(oldFilePath)
assert.Error(t.T(), err)
assert.True(t.T(), strings.Contains(err.Error(), "no such file or directory"))
_, err = os.Stat(newFilePath)
assert.NoError(t.T(), err)
}

func (t *HNSBucketTests) TestRenameFileWithSameParent() {
oldFilePath := path.Join(mntDir, "foo", "file1.txt")
_, err := os.Stat(oldFilePath)
assert.NoError(t.T(), err)
// In the setup phase, we created file1.txt within the bar directory.
newFilePath := path.Join(mntDir, "foo", "file3.txt")
_, err = os.Stat(newFilePath)
assert.True(t.T(), strings.Contains(err.Error(), "no such file or directory"))

err = os.Rename(oldFilePath, newFilePath)

assert.NoError(t.T(), err)
_, err = os.Stat(oldFilePath)
assert.Error(t.T(), err)
assert.True(t.T(), strings.Contains(err.Error(), "no such file or directory"))
_, err = os.Stat(newFilePath)
assert.NoError(t.T(), err)
}
Loading