Default to using SQLite as the garage metadata db engine

This commit is contained in:
Brian Picciano 2024-12-26 20:37:00 +01:00
parent d5323964c6
commit 9407ec23c2
5 changed files with 91 additions and 1 deletions

View File

@ -86,6 +86,8 @@ var subCmdDaemon = subCmd{
}()
}
ctx.logger.Info(ctx, "Daemon successfully started up")
<-ctx.Done()
return nil
},

View File

@ -91,6 +91,11 @@ func garageWriteChildConfig(
)
)
dbEngine, err := garagesrv.GetDBEngine(alloc.MetaPath)
if err != nil {
return "", false, fmt.Errorf("getting alloc db engine: %w", err)
}
changed, err := garagesrv.WriteGarageTomlFile(
ctx,
logger,
@ -101,6 +106,7 @@ func garageWriteChildConfig(
RPCSecret: rpcSecret,
AdminToken: adminToken,
DBEngine: dbEngine,
LocalNode: node,
BootstrapPeers: hostBootstrap.GarageNodes(),

View File

@ -4,11 +4,15 @@ import (
"isle/bootstrap"
"isle/daemon/daecommon"
"isle/garage"
"isle/garage/garagesrv"
"isle/jsonutil"
"isle/nebula"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCreate(t *testing.T) {
@ -136,7 +140,7 @@ func TestNetwork_SetConfig(t *testing.T) {
return roles
}
t.Run("add storage alloc", func(t *testing.T) {
t.Run("add storage alloc/simple", func(t *testing.T) {
var (
h = newIntegrationHarness(t)
network = h.createNetwork(t, "primus", nil)
@ -185,6 +189,55 @@ func TestNetwork_SetConfig(t *testing.T) {
layout, err := network.garageAdminClient(t).GetLayout(h.ctx)
assert.NoError(t, err)
assert.ElementsMatch(t, expRoles, layout.Roles)
t.Log("Checking that garage is using the expected db engine")
garageConfig, err := os.ReadFile(
filepath.Join(network.runtimeDir.Path, "garage-4901.toml"),
)
assert.NoError(t, err)
assert.Contains(t,
string(garageConfig),
`db_engine = "`+garagesrv.DBEngineSqlite+`"`,
)
})
t.Run("add storage alloc/lmdb", func(t *testing.T) {
var (
h = newIntegrationHarness(t)
network = h.createNetwork(t, "primus", nil)
networkConfig = network.getConfig(t)
dataPath = h.mkDir(t, "data").Path
metaPath = h.mkDir(t, "meta").Path
)
networkConfig.Storage.Allocations = append(
networkConfig.Storage.Allocations,
daecommon.ConfigStorageAllocation{
DataPath: dataPath,
MetaPath: metaPath,
Capacity: 1,
S3APIPort: 4900,
RPCPort: 4901,
AdminPort: 4902,
},
)
// Creating the directory is enough to ensure that Isle chooses LMDB as
// the db engine.
lmdbPath := filepath.Join(metaPath, "db.lmdb")
require.NoError(t, os.Mkdir(lmdbPath, 0755))
assert.NoError(t, network.SetConfig(h.ctx, networkConfig))
t.Log("Checking that garage is using the expected db engine")
garageConfig, err := os.ReadFile(
filepath.Join(network.runtimeDir.Path, "garage-4901.toml"),
)
assert.NoError(t, err)
assert.Contains(t,
string(garageConfig),
`db_engine = "`+garagesrv.DBEngineLMDB+`"`,
)
})
t.Run("remove storage alloc", func(t *testing.T) {

View File

@ -14,6 +14,15 @@ import (
"strconv"
)
// DBEngine enumerates the garage db engines which are supported by Isle.
type DBEngine string
// Enumeration of DBEngine values.
const (
DBEngineLMDB DBEngine = "lmdb"
DBEngineSqlite DBEngine = "sqlite"
)
func nodeKeyPath(metaDirPath string) string {
return filepath.Join(metaDirPath, "node_key")
}
@ -150,3 +159,21 @@ func InitAlloc(metaDirPath string, initRPCPort int) (string, int, error) {
return pubKeyStr, rpcPort, nil
}
// GetDBEngine returns the DBEngine currently being used at a particular meta
// data directory. Defaults to DBEngineSqlite if the directory doesn't exist or
// hasn't been fully initialized yet.
func GetDBEngine(metaDirPath string) (DBEngine, error) {
dbLMDBPath := filepath.Join(metaDirPath, "db.lmdb")
stat, err := os.Stat(dbLMDBPath)
if errors.Is(err, fs.ErrNotExist) {
return DBEngineSqlite, nil
} else if err != nil {
return "", fmt.Errorf("checking if %q exists: %w", dbLMDBPath, err)
} else if stat.IsDir() {
return DBEngineLMDB, nil
}
return DBEngineSqlite, nil
}

View File

@ -22,6 +22,7 @@ type GarageTomlData struct {
RPCSecret string
AdminToken string
DBEngine DBEngine
garage.LocalNode
BootstrapPeers []garage.RemoteNode
@ -30,6 +31,7 @@ type GarageTomlData struct {
var garageTomlTpl = template.Must(template.New("").Parse(`
metadata_dir = "{{ .MetaPath }}"
data_dir = "{{ .DataPath }}"
db_engine = "{{ .DBEngine }}"
replication_mode = "` + strconv.Itoa(garage.ReplicationFactor) + `"