mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-03 12:09:09 +02:00
[management] disable prepareStmt for sqlite (#3228)
This commit is contained in:
@@ -956,7 +956,7 @@ func NewSqliteStore(ctx context.Context, dataDir string, metrics telemetry.AppMe
|
||||
}
|
||||
|
||||
file := filepath.Join(dataDir, storeStr)
|
||||
db, err := gorm.Open(sqlite.Open(file), getGormConfig())
|
||||
db, err := gorm.Open(sqlite.Open(file), getGormConfig(SqliteStoreEngine))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -966,7 +966,7 @@ func NewSqliteStore(ctx context.Context, dataDir string, metrics telemetry.AppMe
|
||||
|
||||
// NewPostgresqlStore creates a new Postgres store.
|
||||
func NewPostgresqlStore(ctx context.Context, dsn string, metrics telemetry.AppMetrics) (*SqlStore, error) {
|
||||
db, err := gorm.Open(postgres.Open(dsn), getGormConfig())
|
||||
db, err := gorm.Open(postgres.Open(dsn), getGormConfig(PostgresStoreEngine))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -976,7 +976,7 @@ func NewPostgresqlStore(ctx context.Context, dsn string, metrics telemetry.AppMe
|
||||
|
||||
// NewMysqlStore creates a new MySQL store.
|
||||
func NewMysqlStore(ctx context.Context, dsn string, metrics telemetry.AppMetrics) (*SqlStore, error) {
|
||||
db, err := gorm.Open(mysql.Open(dsn+"?charset=utf8&parseTime=True&loc=Local"), getGormConfig())
|
||||
db, err := gorm.Open(mysql.Open(dsn+"?charset=utf8&parseTime=True&loc=Local"), getGormConfig(MysqlStoreEngine))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -984,11 +984,15 @@ func NewMysqlStore(ctx context.Context, dsn string, metrics telemetry.AppMetrics
|
||||
return NewSqlStore(ctx, db, MysqlStoreEngine, metrics)
|
||||
}
|
||||
|
||||
func getGormConfig() *gorm.Config {
|
||||
func getGormConfig(engine Engine) *gorm.Config {
|
||||
prepStmt := true
|
||||
if engine == SqliteStoreEngine {
|
||||
prepStmt = false
|
||||
}
|
||||
return &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
CreateBatchSize: 400,
|
||||
PrepareStmt: true,
|
||||
PrepareStmt: prepStmt,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,16 +10,18 @@ import (
|
||||
"net/netip"
|
||||
"os"
|
||||
"runtime"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/netbirdio/netbird/management/server/util"
|
||||
"github.com/rs/xid"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/netbirdio/netbird/management/server/util"
|
||||
|
||||
nbdns "github.com/netbirdio/netbird/dns"
|
||||
resourceTypes "github.com/netbirdio/netbird/management/server/networks/resources/types"
|
||||
routerTypes "github.com/netbirdio/netbird/management/server/networks/routers/types"
|
||||
@@ -2843,3 +2845,73 @@ func TestSqlStore_DeletePeer(t *testing.T) {
|
||||
require.Error(t, err)
|
||||
require.Nil(t, peer)
|
||||
}
|
||||
|
||||
func TestSqlStore_DatabaseBlocking(t *testing.T) {
|
||||
store, cleanup, err := NewTestStoreFromSQL(context.Background(), "../testdata/store_with_expired_peers.sql", t.TempDir())
|
||||
t.Cleanup(cleanup)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
concurrentReads := 40
|
||||
|
||||
testRunSuccessful := false
|
||||
wgSuccess := sync.WaitGroup{}
|
||||
wgSuccess.Add(concurrentReads)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
start := make(chan struct{})
|
||||
|
||||
for i := 0; i < concurrentReads/2; i++ {
|
||||
go func() {
|
||||
t.Logf("Entered routine 1-%d", i)
|
||||
|
||||
<-start
|
||||
err := store.ExecuteInTransaction(context.Background(), func(tx Store) error {
|
||||
_, err := tx.GetAccountIDByPeerID(context.Background(), LockingStrengthShare, "cfvprsrlo1hqoo49ohog")
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("Failed, got error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
t.Log("Got User from routine 1")
|
||||
wgSuccess.Done()
|
||||
}()
|
||||
}
|
||||
|
||||
for i := 0; i < concurrentReads/2; i++ {
|
||||
go func() {
|
||||
t.Logf("Entered routine 2-%d", i)
|
||||
|
||||
<-start
|
||||
_, err := store.GetAccountIDByPeerID(context.Background(), LockingStrengthShare, "cfvprsrlo1hqoo49ohog")
|
||||
if err != nil {
|
||||
t.Errorf("Failed, got error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
t.Log("Got User from routine 2")
|
||||
wgSuccess.Done()
|
||||
}()
|
||||
}
|
||||
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
close(start)
|
||||
t.Log("Started routines")
|
||||
|
||||
go func() {
|
||||
wgSuccess.Wait()
|
||||
testRunSuccessful = true
|
||||
}()
|
||||
|
||||
<-ctx.Done()
|
||||
if !testRunSuccessful {
|
||||
t.Fatalf("Test failed")
|
||||
}
|
||||
|
||||
t.Logf("Test completed")
|
||||
}
|
||||
|
||||
@@ -319,7 +319,7 @@ func NewTestStoreFromSQL(ctx context.Context, filename string, dataDir string) (
|
||||
}
|
||||
|
||||
file := filepath.Join(dataDir, storeStr)
|
||||
db, err := gorm.Open(sqlite.Open(file), getGormConfig())
|
||||
db, err := gorm.Open(sqlite.Open(file), getGormConfig(kind))
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user