forked from cerc-io/plugeth
core/rawdb: use readonly file lock in readonly mode (#28180)
This allows using the freezer from multiple processes at once in read-only mode. Co-authored-by: Martin Holst Swende <martin@swende.se>
This commit is contained in:
parent
d135bafdcb
commit
f1b2ec0833
@ -108,7 +108,11 @@ func NewFreezer(datadir string, namespace string, readonly bool, maxTableSize ui
|
||||
// Leveldb uses LOCK as the filelock filename. To prevent the
|
||||
// name collision, we use FLOCK as the lock name.
|
||||
lock := flock.New(flockFile)
|
||||
if locked, err := lock.TryLock(); err != nil {
|
||||
tryLock := lock.TryLock
|
||||
if readonly {
|
||||
tryLock = lock.TryRLock
|
||||
}
|
||||
if locked, err := tryLock(); err != nil {
|
||||
return nil, err
|
||||
} else if !locked {
|
||||
return nil, errors.New("locking failed")
|
||||
|
@ -283,6 +283,57 @@ func TestFreezerReadonlyValidate(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFreezerConcurrentReadonly(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tables := map[string]bool{"a": true}
|
||||
dir := t.TempDir()
|
||||
|
||||
f, err := NewFreezer(dir, "", false, 2049, tables)
|
||||
if err != nil {
|
||||
t.Fatal("can't open freezer", err)
|
||||
}
|
||||
var item = make([]byte, 1024)
|
||||
batch := f.tables["a"].newBatch()
|
||||
items := uint64(10)
|
||||
for i := uint64(0); i < items; i++ {
|
||||
require.NoError(t, batch.AppendRaw(i, item))
|
||||
}
|
||||
require.NoError(t, batch.commit())
|
||||
if loaded := f.tables["a"].items.Load(); loaded != items {
|
||||
t.Fatalf("unexpected number of items in table, want: %d, have: %d", items, loaded)
|
||||
}
|
||||
require.NoError(t, f.Close())
|
||||
|
||||
var (
|
||||
wg sync.WaitGroup
|
||||
fs = make([]*Freezer, 5)
|
||||
errs = make([]error, 5)
|
||||
)
|
||||
for i := 0; i < 5; i++ {
|
||||
wg.Add(1)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
|
||||
f, err := NewFreezer(dir, "", true, 2049, tables)
|
||||
if err == nil {
|
||||
fs[i] = f
|
||||
} else {
|
||||
errs[i] = err
|
||||
}
|
||||
}(i)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
for i := range fs {
|
||||
if err := errs[i]; err != nil {
|
||||
t.Fatal("failed to open freezer", err)
|
||||
}
|
||||
require.NoError(t, fs[i].Close())
|
||||
}
|
||||
}
|
||||
|
||||
func newFreezerForTesting(t *testing.T, tables map[string]bool) (*Freezer, string) {
|
||||
t.Helper()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user