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

View File

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

View File

@@ -72,6 +72,30 @@ func TestStore_Open(t *testing.T) {
_ = store _ = 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) { func TestStore_RawDB(t *testing.T) {
store, err := kvstore.OpenStore("test") store, err := kvstore.OpenStore("test")
if err != nil { if err != nil {