-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Pyramid delete before open (#4062)
* Fix: Pyramid delete before open * Second attempt * Fixes 1 * Fixes 2 * Fixes 3 * Fixes 4 * Update pkg/pyramid/file_tracker_test.go Co-authored-by: itai-david <90712874+itai-david@users.noreply.github.com> Co-authored-by: itai-david <90712874+itai-david@users.noreply.github.com>
- Loading branch information
Showing
3 changed files
with
147 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package pyramid | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/treeverse/lakefs/pkg/pyramid/params" | ||
) | ||
|
||
// fileTracker tracks file open requests in TierFS to avoid race conditions with cache rejection/eviction | ||
type fileTracker struct { | ||
refMap map[string]*tracked | ||
mu sync.Mutex | ||
delete deleteCallback | ||
} | ||
|
||
type deleteCallback func(path params.RelativePath) | ||
|
||
type tracked struct { | ||
ref int | ||
deleted bool | ||
} | ||
|
||
func NewFileTracker(delete deleteCallback) *fileTracker { | ||
return &fileTracker{ | ||
refMap: map[string]*tracked{}, | ||
delete: delete, | ||
} | ||
} | ||
|
||
func (t *fileTracker) Open(path params.RelativePath) func() { | ||
t.mu.Lock() | ||
defer t.mu.Unlock() | ||
if val, ok := t.refMap[string(path)]; ok { | ||
val.ref++ | ||
} else { | ||
t.refMap[string(path)] = &tracked{ | ||
ref: 1, | ||
} | ||
} | ||
return func() { | ||
t.close(path) | ||
} | ||
} | ||
|
||
func (t *fileTracker) close(path params.RelativePath) { | ||
t.mu.Lock() | ||
defer t.mu.Unlock() | ||
if val, ok := t.refMap[string(path)]; ok { | ||
val.ref-- | ||
if val.ref == 0 { | ||
delete(t.refMap, string(path)) | ||
if val.deleted { | ||
t.delete(path) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (t *fileTracker) Delete(path params.RelativePath) { | ||
t.mu.Lock() | ||
defer t.mu.Unlock() | ||
if val, ok := t.refMap[string(path)]; ok { | ||
val.deleted = true | ||
} else { | ||
t.delete(path) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package pyramid_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/treeverse/lakefs/pkg/pyramid" | ||
"github.com/treeverse/lakefs/pkg/pyramid/params" | ||
) | ||
|
||
var testFileMap map[string]bool | ||
|
||
func TestTracker(t *testing.T) { | ||
const filename = "test_filename" | ||
file1 := params.RelativePath(filename) | ||
file2 := params.RelativePath("dummy_file") | ||
testFileMap = map[string]bool{} | ||
tracker := pyramid.NewFileTracker(testTrackerDeleteCB) | ||
|
||
// Delete non existent | ||
tracker.Delete(file2) | ||
|
||
// Delete file before close | ||
testFileMap[string(file1)] = true | ||
testFileMap[string(file2)] = true | ||
closer := tracker.Open(file1) | ||
_ = tracker.Open(file1) | ||
_ = tracker.Open(file2) | ||
tracker.Delete(file1) | ||
require.True(t, testFileMap[string(file1)]) | ||
|
||
// Close one reference and ensure file still not deleted | ||
closer() | ||
require.True(t, testFileMap[string(file1)]) | ||
|
||
// Close last reference and ensure file was deleted | ||
closer() | ||
require.False(t, testFileMap[string(file1)]) | ||
require.True(t, testFileMap[string(file2)]) | ||
|
||
// Close deleted file - sanity | ||
closer() | ||
|
||
// Delete after close | ||
testFileMap[string(file1)] = true | ||
_ = tracker.Open(file1) | ||
closer() | ||
tracker.Delete(file1) | ||
require.False(t, testFileMap[string(file1)]) | ||
require.True(t, testFileMap[string(file2)]) | ||
} | ||
|
||
func testTrackerDeleteCB(p params.RelativePath) { | ||
testFileMap[string(p)] = false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters