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

bug in block cache open call #1580

Open
wants to merge 7 commits into
base: blobfuse/2.4.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 2.4.1 (Unreleased)
**Bug Fixes**
- Create block pool only in the child process.
- Prevent the block cache to truncate the file size to zero when the file is opened in O_WRONLY mode when writebackcache is disabled.

## 2.4.0 (Unreleased)
**Features**
Expand Down
4 changes: 2 additions & 2 deletions component/block_cache/block_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func (bc *BlockCache) CreateFile(options internal.CreateFileOptions) (*handlemap

// OpenFile: Create a handle for the file user has requested to open
func (bc *BlockCache) OpenFile(options internal.OpenFileOptions) (*handlemap.Handle, error) {
log.Trace("BlockCache::OpenFile : name=%s, flags=%d, mode=%s", options.Name, options.Flags, options.Mode)
log.Trace("BlockCache::OpenFile : name=%s, flags=%X, mode=%s", options.Name, options.Flags, options.Mode)

attr, err := bc.NextComponent().GetAttr(internal.GetAttrOptions{Name: options.Name})
if err != nil {
Expand All @@ -398,7 +398,7 @@ func (bc *BlockCache) OpenFile(options internal.OpenFileOptions) (*handlemap.Han
log.Debug("BlockCache::OpenFile : Size of file handle.Size %v", handle.Size)
bc.prepareHandleForBlockCache(handle)

if options.Flags&os.O_TRUNC != 0 || (options.Flags&os.O_WRONLY != 0 && options.Flags&os.O_APPEND == 0) {
if options.Flags&os.O_TRUNC != 0 {
vibhansa-msft marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shall add a UT case around this validating that truncate clears the file contents while wronly does not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added, Please review it

// If file is opened in truncate or wronly mode then we need to wipe out the data consider current file size as 0
log.Debug("BlockCache::OpenFile : Truncate %v to 0", options.Name)
handle.Size = 0
Expand Down
75 changes: 75 additions & 0 deletions component/block_cache/block_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2625,6 +2625,81 @@ func (suite *blockCacheTestSuite) TestZZZZZStreamToBlockCacheConfig() {
}
}

// This test checks the size of the file when the file is opened in
// O_TRUNC mode and O_WRONLY mode.
func (suite *blockCacheTestSuite) TestSizeOfFileInOpen() {
// Write-back cache is turned on by default while mounting.
config := "block_cache:\n block-size-mb: 1\n mem-size-mb: 20\n prefetch: 12\n parallelism: 1"
tobj, err := setupPipeline(config)
suite.assert.Nil(err)
defer tobj.cleanupPipeline()

path := getTestFileName(suite.T().Name())
storagePath := filepath.Join(tobj.fake_storage_path, path)
localPath := filepath.Join(tobj.disk_cache_path, path)

// ------------------------------------------------------------------
// Create a local file
fh, err := os.Create(localPath)
suite.assert.Nil(err)

// write 1MB data at offset 0
n, err := fh.WriteAt(dataBuff[:_1MB], 0)
suite.assert.Nil(err)
suite.assert.Equal(n, int(_1MB))

err = fh.Close()
suite.assert.Nil(err)
// ------------------------------------------------------------------
// Create a file using Mountpoint
options := internal.CreateFileOptions{Name: path, Mode: 0777}
h, err := tobj.blockCache.CreateFile(options)
suite.assert.Nil(err)
suite.assert.NotNil(h)
suite.assert.Equal(h.Size, int64(0))
suite.assert.False(h.Dirty())

// write 1MB data at offset 0
n, err = tobj.blockCache.WriteFile(internal.WriteFileOptions{Handle: h, Offset: 0, Data: dataBuff[:_1MB]})
suite.assert.Nil(err)
suite.assert.Equal(n, int(_1MB))
suite.assert.True(h.Dirty())

err = tobj.blockCache.CloseFile(internal.CloseFileOptions{Handle: h})
suite.assert.Nil(err)
//---------------------------------------------------------------------

//Open and close the file using the given flag in local and mountpoint and
// check the size is same or not.
check := func(flag int) int {
lfh, err := os.OpenFile(localPath, flag, 0666)
suite.assert.Nil(err)
suite.assert.NotNil(lfh)
err = lfh.Close()
suite.assert.Nil(err)

openFileOptions := internal.OpenFileOptions{Name: path, Flags: flag, Mode: 0777}
rfh, err := tobj.blockCache.OpenFile(openFileOptions)
suite.assert.Nil(err)
err = tobj.blockCache.CloseFile(internal.CloseFileOptions{Handle: rfh})
suite.assert.Nil(err)

statInfoLocal, err := os.Stat(localPath)
suite.assert.Nil(err)
sizeInLocal := statInfoLocal.Size()

statInfoMount, err := os.Stat(storagePath)
suite.assert.Nil(err)
sizeInMount := statInfoMount.Size()
suite.assert.Equal(sizeInLocal, sizeInMount)
return int(sizeInLocal)
}
size := check(os.O_WRONLY) // size of the file would be 1MB
suite.assert.Equal(size, int(_1MB))
size = check(os.O_TRUNC) // size of the file would be zero here.
suite.assert.Equal(size, int(0))
}

// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestBlockCacheTestSuite(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions component/loopback/loopback_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,13 @@ func (lfs *LoopbackFS) CommitData(options internal.CommitDataOptions) error {
return err
}

if len(options.List) == 0 {
err = blob.Truncate(0)
if err != nil {
return err
}
}

for idx, id := range options.List {
path := fmt.Sprintf("%s_%s", filepath.Join(lfs.path, options.Name), strings.ReplaceAll(id, "/", "_"))
info, err := os.Lstat(path)
Expand Down