Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate FileStore engine support #2119

Merged
merged 8 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/golang-test-darwin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
test:
strategy:
matrix:
store: ['jsonfile', 'sqlite']
store: ['sqlite']
runs-on: macos-latest
steps:
- name: Install Go
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/golang-test-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
matrix:
arch: [ '386','amd64' ]
store: [ 'jsonfile', 'sqlite', 'postgres']
store: [ 'sqlite', 'postgres']
runs-on: ubuntu-latest
steps:
- name: Install Go
Expand Down
10 changes: 7 additions & 3 deletions client/internal/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ func TestEngine_MultiplePeers(t *testing.T) {
return
}
defer sigServer.Stop()
mgmtServer, mgmtAddr, err := startManagement(dir)
mgmtServer, mgmtAddr, err := startManagement(t, dir)
if err != nil {
t.Fatal(err)
return
Expand Down Expand Up @@ -1037,7 +1037,9 @@ func startSignal(t *testing.T) (*grpc.Server, string, error) {
return s, lis.Addr().String(), nil
}

func startManagement(dataDir string) (*grpc.Server, string, error) {
func startManagement(t *testing.T, dataDir string) (*grpc.Server, string, error) {
t.Helper()

config := &server.Config{
Stuns: []*server.Host{},
TURNConfig: &server.TURNConfig{},
Expand All @@ -1054,10 +1056,12 @@ func startManagement(dataDir string) (*grpc.Server, string, error) {
return nil, "", err
}
s := grpc.NewServer(grpc.KeepaliveEnforcementPolicy(kaep), grpc.KeepaliveParams(kasp))
store, _, err := server.NewTestStoreFromJson(config.Datadir)

store, cleanUp, err := server.NewTestStoreFromJson(config.Datadir)
if err != nil {
return nil, "", err
}
t.Cleanup(cleanUp)

peersUpdateManager := server.NewPeersUpdateManager(nil)
eventStore := &activity.InMemoryEventStore{}
Expand Down
67 changes: 0 additions & 67 deletions management/cmd/migration_down.go

This file was deleted.

34 changes: 2 additions & 32 deletions management/cmd/migration_up.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package cmd

import (
"errors"
"flag"
"fmt"
"os"
"path"

"github.com/netbirdio/netbird/management/server"
"github.com/netbirdio/netbird/util"
Expand All @@ -29,36 +26,9 @@ var upCmd = &cobra.Command{
return fmt.Errorf("failed initializing log %v", err)
}

fileStorePath := path.Join(mgmtDataDir, "store.json")
if _, err := os.Stat(fileStorePath); errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("%s doesn't exist, couldn't continue the operation", fileStorePath)
if err := server.MigrateFileStoreToSqlite(mgmtDataDir); err != nil {
return err
}

sqlStorePath := path.Join(mgmtDataDir, "store.db")
if _, err := os.Stat(sqlStorePath); err == nil {
return fmt.Errorf("%s already exists, couldn't continue the operation", sqlStorePath)
}

fstore, err := server.NewFileStore(mgmtDataDir, nil)
if err != nil {
return fmt.Errorf("failed creating file store: %s: %v", mgmtDataDir, err)
}

fsStoreAccounts := len(fstore.GetAllAccounts())
log.Infof("%d account will be migrated from file store %s to sqlite store %s",
fsStoreAccounts, fileStorePath, sqlStorePath)

store, err := server.NewSqliteStoreFromFileStore(fstore, mgmtDataDir, nil)
if err != nil {
return fmt.Errorf("failed creating file store: %s: %v", mgmtDataDir, err)
}

sqliteStoreAccounts := len(store.GetAllAccounts())
if fsStoreAccounts != sqliteStoreAccounts {
return fmt.Errorf("failed to migrate accounts from file to sqlite. Expected accounts: %d, got: %d",
fsStoreAccounts, sqliteStoreAccounts)
}

log.Info("Migration finished successfully")

return nil
Expand Down
1 change: 0 additions & 1 deletion management/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func init() {
migrationCmd.MarkFlagRequired("datadir") //nolint

migrationCmd.AddCommand(upCmd)
migrationCmd.AddCommand(downCmd)

rootCmd.AddCommand(migrationCmd)
}
Expand Down
18 changes: 16 additions & 2 deletions management/server/sql_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
Expand All @@ -27,6 +28,10 @@ import (
"github.com/netbirdio/netbird/route"
)

const (
storeSqliteFileName = "store.db"
)

// SqlStore represents an account storage backed by a Sql DB persisted to disk
type SqlStore struct {
db *gorm.DB
Expand Down Expand Up @@ -623,10 +628,10 @@ func (s *SqlStore) GetStoreEngine() StoreEngine {

// NewSqliteStore creates a new SQLite store.
func NewSqliteStore(dataDir string, metrics telemetry.AppMetrics) (*SqlStore, error) {
storeStr := "store.db?cache=shared"
storeStr := fmt.Sprintf("%s?cache=shared", storeSqliteFileName)
if runtime.GOOS == "windows" {
// Vo avoid `The process cannot access the file because it is being used by another process` on Windows
storeStr = "store.db"
storeStr = storeSqliteFileName
}

file := filepath.Join(dataDir, storeStr)
Expand Down Expand Up @@ -655,6 +660,15 @@ func NewPostgresqlStore(dsn string, metrics telemetry.AppMetrics) (*SqlStore, er
return NewSqlStore(db, PostgresStoreEngine, metrics)
}

// newPostgresStore initializes a new Postgres store.
func newPostgresStore(metrics telemetry.AppMetrics) (Store, error) {
dsn, ok := os.LookupEnv(postgresDsnEnv)
if !ok {
return nil, fmt.Errorf("%s is not set", postgresDsnEnv)
}
return NewPostgresqlStore(dsn, metrics)
}

// NewSqliteStoreFromFileStore restores a store from FileStore and stores SQLite DB in the file located in datadir.
func NewSqliteStoreFromFileStore(fileStore *FileStore, dataDir string, metrics telemetry.AppMetrics) (*SqlStore, error) {
store, err := NewSqliteStore(dataDir, metrics)
Expand Down
Loading
Loading