为KV存储增加文件锁

This commit is contained in:
GoEdgeLab
2024-03-31 10:37:06 +08:00
parent 0f22e87711
commit 2a26570c90
3 changed files with 43 additions and 9 deletions

View File

@@ -14,7 +14,7 @@ type DB struct {
namespace string
tableMap map[string]TableInterface
locker sync.RWMutex
mu sync.RWMutex
}
func NewDB(store *Store, dbName string) (*DB, error) {
@@ -34,8 +34,8 @@ func (this *DB) AddTable(table TableInterface) {
table.SetNamespace([]byte(this.Namespace() + table.Name() + "$"))
table.SetDB(this)
this.locker.Lock()
defer this.locker.Unlock()
this.mu.Lock()
defer this.mu.Unlock()
this.tableMap[table.Name()] = table
}
@@ -53,8 +53,8 @@ func (this *DB) Store() *Store {
}
func (this *DB) Close() error {
this.locker.Lock()
defer this.locker.Unlock()
this.mu.Lock()
defer this.mu.Unlock()
var lastErr error
for _, table := range this.tableMap {

View File

@@ -7,6 +7,7 @@ import (
"fmt"
"github.com/TeaOSLab/EdgeNode/internal/events"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
"github.com/cockroachdb/pebble"
"github.com/iwind/TeaGo/Tea"
@@ -23,6 +24,7 @@ type Store struct {
path string
rawDB *pebble.DB
locker *fsutils.Locker
isClosed bool
@@ -46,6 +48,7 @@ func NewStore(storeName string) (*Store, error) {
return &Store{
name: storeName,
path: Tea.Root + "/data/stores/" + storeName + StoreSuffix,
locker: fsutils.NewLocker(Tea.Root + "/data/stores/" + storeName + StoreSuffix + "/.fs"),
}, nil
}
@@ -112,6 +115,11 @@ func DefaultStore() (*Store, error) {
}
func (this *Store) Open() error {
err := this.locker.Lock()
if err != nil {
return err
}
var opt = &pebble.Options{
Logger: NewLogger(),
}
@@ -179,6 +187,8 @@ func (this *Store) Close() error {
return nil
}
_ = this.locker.Release()
this.mu.Lock()
var lastErr error
for _, db := range this.dbs {

View File

@@ -72,6 +72,30 @@ func TestStore_Open(t *testing.T) {
_ = store
}
func TestStore_Twice(t *testing.T) {
{
t.Log(1)
store, err := kvstore.OpenStore("test")
if err != nil {
t.Fatal(err)
}
_ = store.Close()
}
{
t.Log("2")
store, err := kvstore.OpenStore("test")
if err != nil {
t.Fatal(err)
}
defer func() {
_ = store.Close()
}()
}
t.Log("opened")
}
func TestStore_RawDB(t *testing.T) {
store, err := kvstore.OpenStore("test")
if err != nil {