Skip to content

Commit

Permalink
Use slog in sqlite backend (#4467)
Browse files Browse the repository at this point in the history
Closes #4013.
  • Loading branch information
chilagrow authored Jul 18, 2024
1 parent 2e60284 commit 8fdcfd9
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 35 deletions.
2 changes: 1 addition & 1 deletion internal/backends/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func testBackends(t *testing.T) map[string]*testBackend {

b, err := sqlite.NewBackend(&sqlite.NewBackendParams{
URI: testutil.TestSQLiteURI(t, ""),
L: l.Named("sqlite"),
L: logging.WithName(testutil.SLogger(t), "sqlite"),
P: sp,
BatchSize: 100,
})
Expand Down
4 changes: 2 additions & 2 deletions internal/backends/sqlite/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ package sqlite
import (
"cmp"
"context"
"log/slog"
"slices"

"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"

"github.com/FerretDB/FerretDB/internal/backends"
"github.com/FerretDB/FerretDB/internal/backends/sqlite/metadata"
Expand All @@ -38,7 +38,7 @@ type backend struct {
//nolint:vet // for readability
type NewBackendParams struct {
URI string
L *zap.Logger
L *slog.Logger
P *state.Provider
BatchSize int
_ struct{} // prevent unkeyed literals
Expand Down
2 changes: 1 addition & 1 deletion internal/backends/sqlite/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestCappedCollectionInsertAllQueryExplain(t *testing.T) {
sp, err := state.NewProvider("")
require.NoError(t, err)

b, err := NewBackend(&NewBackendParams{URI: testutil.TestSQLiteURI(t, ""), L: testutil.Logger(t), P: sp, BatchSize: 100})
b, err := NewBackend(&NewBackendParams{URI: testutil.TestSQLiteURI(t, ""), L: testutil.SLogger(t), P: sp, BatchSize: 100})
require.NoError(t, err)
t.Cleanup(b.Close)

Expand Down
6 changes: 4 additions & 2 deletions internal/backends/sqlite/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestDatabaseStats(t *testing.T) {
sp, err := state.NewProvider("")
require.NoError(t, err)

b, err := NewBackend(&NewBackendParams{URI: testutil.TestSQLiteURI(t, ""), L: testutil.Logger(t), P: sp})
b, err := NewBackend(&NewBackendParams{URI: testutil.TestSQLiteURI(t, ""), L: testutil.SLogger(t), P: sp})
require.NoError(t, err)
t.Cleanup(b.Close)

Expand Down Expand Up @@ -72,7 +72,9 @@ func TestDatabaseStatsFreeStorage(t *testing.T) {
name, params := name, params
t.Run(name, func(t *testing.T) {
uri := testutil.TestSQLiteURI(t, "") + params
b, err := NewBackend(&NewBackendParams{URI: uri, L: testutil.Logger(t), P: sp, BatchSize: 100})

var b backends.Backend
b, err = NewBackend(&NewBackendParams{URI: uri, L: testutil.SLogger(t), P: sp, BatchSize: 100})
require.NoError(t, err)

t.Cleanup(b.Close)
Expand Down
10 changes: 6 additions & 4 deletions internal/backends/sqlite/metadata/pool/opendb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ package pool
import (
"context"
"database/sql"
"log/slog"

"go.uber.org/zap"
_ "modernc.org/sqlite" // register database/sql driver

"github.com/FerretDB/FerretDB/internal/util/fsql"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/logging"
"github.com/FerretDB/FerretDB/internal/util/state"
)

Expand All @@ -32,7 +34,7 @@ import (
// so no validation is needed.
// One exception is very long full path names for the filesystem,
// but we don't check it.
func openDB(name, uri string, memory bool, l *zap.Logger, sp *state.Provider) (*fsql.DB, error) {
func openDB(name, uri string, memory bool, l *slog.Logger, sp *state.Provider) (*fsql.DB, error) {
db, err := sql.Open("sqlite", uri)
if err != nil {
return nil, lazyerrors.Error(err)
Expand Down Expand Up @@ -63,13 +65,13 @@ func openDB(name, uri string, memory bool, l *zap.Logger, sp *state.Provider) (*

row := db.QueryRowContext(context.Background(), "SELECT sqlite_version()")
if err := row.Scan(&s.BackendVersion); err != nil {
l.Error("sqlite.metadata.pool.openDB: failed to query SQLite version", zap.Error(err))
l.Error("sqlite.metadata.pool.openDB: failed to query SQLite version", logging.Error(err))
}
})
if err != nil {
l.Error("sqlite.metadata.pool.openDB: failed to update state", zap.Error(err))
l.Error("sqlite.metadata.pool.openDB: failed to update state", logging.Error(err))
}
}

return fsql.WrapDB(db, name, l), nil
return fsql.WrapDB(db, name, zap.L()), nil
}
23 changes: 15 additions & 8 deletions internal/backends/sqlite/metadata/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package pool
import (
"context"
"fmt"
"log/slog"
"net/url"
"os"
"path"
Expand All @@ -29,11 +30,11 @@ import (
"sync"

"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
"golang.org/x/exp/maps"

"github.com/FerretDB/FerretDB/internal/util/fsql"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/logging"
"github.com/FerretDB/FerretDB/internal/util/observability"
"github.com/FerretDB/FerretDB/internal/util/resource"
"github.com/FerretDB/FerretDB/internal/util/state"
Expand All @@ -53,7 +54,7 @@ const (
//nolint:vet // for readability
type Pool struct {
uri url.URL
l *zap.Logger
l *slog.Logger
sp *state.Provider

rw sync.RWMutex
Expand All @@ -68,7 +69,7 @@ type Pool struct {
//
// The returned map is the initial set of existing databases.
// It should not be modified.
func New(u string, l *zap.Logger, sp *state.Provider) (*Pool, map[string]*fsql.DB, error) {
func New(u string, l *slog.Logger, sp *state.Provider) (*Pool, map[string]*fsql.DB, error) {
uri, err := parseURI(u)
if err != nil {
return nil, nil, fmt.Errorf("failed to parse SQLite URI %q: %s", u, err)
Expand Down Expand Up @@ -98,7 +99,7 @@ func New(u string, l *zap.Logger, sp *state.Provider) (*Pool, map[string]*fsql.D
name := p.databaseName(f)
uri := p.databaseURI(name)

p.l.Debug("Opening existing database.", zap.String("name", name), zap.String("uri", uri))
p.l.Debug("Opening existing database", slog.String("name", name), slog.String("uri", uri))

db, err := openDB(name, uri, p.memory(), l, p.sp)
if err != nil {
Expand Down Expand Up @@ -208,7 +209,7 @@ func (p *Pool) GetOrCreate(ctx context.Context, name string) (*fsql.DB, bool, er
return nil, false, lazyerrors.Errorf("%s: %w", uri, err)
}

p.l.Debug("Database created.", zap.String("name", name), zap.String("uri", uri))
p.l.DebugContext(ctx, "Database created", slog.String("name", name), slog.String("uri", uri))

p.dbs[name] = db

Expand All @@ -232,18 +233,24 @@ func (p *Pool) Drop(ctx context.Context, name string) bool {
}

if err := db.Close(); err != nil {
p.l.Warn("Failed to close database connection.", zap.String("name", name), zap.Error(err))
p.l.WarnContext(ctx, "Failed to close database connection", slog.String("name", name), logging.Error(err))
}

delete(p.dbs, name)

if f := p.databaseFile(name); f != "" {
if err := os.Remove(f); err != nil {
p.l.Warn("Failed to remove database file.", zap.String("file", f), zap.String("name", name), zap.Error(err))
p.l.WarnContext(
ctx,
"Failed to remove database file",
slog.String("file", f),
slog.String("name", name),
logging.Error(err),
)
}
}

p.l.Debug("Database dropped.", zap.String("name", name))
p.l.DebugContext(ctx, "Database dropped", slog.String("name", name))

return true
}
Expand Down
13 changes: 7 additions & 6 deletions internal/backends/sqlite/metadata/pool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestCreateDrop(t *testing.T) {
require.NoError(t, err)

// that also tests that query parameters are preserved by using non-writable directory
p, _, err := New("file:/?mode=memory&_pragma=journal_mode(wal)", testutil.Logger(t), sp)
p, _, err := New("file:/?mode=memory&_pragma=journal_mode(wal)", testutil.SLogger(t), sp)
require.NoError(t, err)
t.Cleanup(p.Close)

Expand Down Expand Up @@ -102,7 +102,8 @@ func TestCreateDropStress(t *testing.T) {
"memory-immediate": "file:./" + dir + "/?mode=memory&_txlock=immediate",
} {
t.Run(testName, func(t *testing.T) {
p, _, err := New(uri, testutil.Logger(t), sp)
var p *Pool
p, _, err = New(uri, testutil.SLogger(t), sp)
require.NoError(t, err)
t.Cleanup(p.Close)

Expand Down Expand Up @@ -165,7 +166,7 @@ func TestMemory(t *testing.T) {

dbName := testutil.DatabaseName(t) + "1"

p0, dbs, err := New(uri, testutil.Logger(t), sp)
p0, dbs, err := New(uri, testutil.SLogger(t), sp)
require.NoError(t, err)
assert.Empty(t, dbs)
t.Cleanup(p0.Close)
Expand All @@ -174,7 +175,7 @@ func TestMemory(t *testing.T) {
require.NoError(t, err)
assert.True(t, created)

p1, dbs, err := New(uri+"?mode=memory", testutil.Logger(t), sp)
p1, dbs, err := New(uri+"?mode=memory", testutil.SLogger(t), sp)
require.NoError(t, err)
assert.Empty(t, dbs, "dir content should be ignored for mode=memory")
t.Cleanup(p1.Close)
Expand Down Expand Up @@ -204,7 +205,7 @@ func TestMemory(t *testing.T) {
_, err = db2.ExecContext(ctx, "CREATE TABLE test (id INT) STRICT")
require.NoError(t, err)

p2, dbs, err := New(uri+"?mode=memory", testutil.Logger(t), sp)
p2, dbs, err := New(uri+"?mode=memory", testutil.SLogger(t), sp)
require.NoError(t, err)
assert.Empty(t, dbs)
t.Cleanup(p2.Close)
Expand All @@ -225,7 +226,7 @@ func TestDefaults(t *testing.T) {
sp, err := state.NewProvider("")
require.NoError(t, err)

p, _, err := New(testutil.TestSQLiteURI(t, ""), testutil.Logger(t), sp)
p, _, err := New(testutil.TestSQLiteURI(t, ""), testutil.SLogger(t), sp)
require.NoError(t, err)
t.Cleanup(p.Close)

Expand Down
6 changes: 3 additions & 3 deletions internal/backends/sqlite/metadata/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import (
"context"
"fmt"
"hash/fnv"
"log/slog"
"slices"
"sort"
"strings"
"sync"

"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
"golang.org/x/exp/maps"

"github.com/FerretDB/FerretDB/internal/backends"
Expand Down Expand Up @@ -57,7 +57,7 @@ const (
// Exported methods are safe for concurrent use. Unexported methods are not.
type Registry struct {
p *pool.Pool
l *zap.Logger
l *slog.Logger
BatchSize int

// rw protects colls but also acts like a global lock for the whole registry.
Expand All @@ -70,7 +70,7 @@ type Registry struct {
}

// NewRegistry creates a registry for SQLite databases in the directory specified by SQLite URI.
func NewRegistry(u string, batchSize int, l *zap.Logger, sp *state.Provider) (*Registry, error) {
func NewRegistry(u string, batchSize int, l *slog.Logger, sp *state.Provider) (*Registry, error) {
p, initDBs, err := pool.New(u, l, sp)
if err != nil {
return nil, err
Expand Down
20 changes: 14 additions & 6 deletions internal/backends/sqlite/metadata/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestCreateDrop(t *testing.T) {
sp, err := state.NewProvider("")
require.NoError(t, err)

r, err := NewRegistry(testutil.TestSQLiteURI(t, ""), 100, testutil.Logger(t), sp)
r, err := NewRegistry(testutil.TestSQLiteURI(t, ""), 100, testutil.SLogger(t), sp)
require.NoError(t, err)
t.Cleanup(r.Close)

Expand Down Expand Up @@ -113,7 +113,9 @@ func TestCreateDropStress(t *testing.T) {
} {
t.Run(testName, func(t *testing.T) {
uri := testutil.TestSQLiteURI(t, "") + params
r, err := NewRegistry(uri, 100, testutil.Logger(t), sp)

var r *Registry
r, err = NewRegistry(uri, 100, testutil.SLogger(t), sp)
require.NoError(t, err)
t.Cleanup(r.Close)

Expand Down Expand Up @@ -158,7 +160,9 @@ func TestCreateSameStress(t *testing.T) {
} {
t.Run(testName, func(t *testing.T) {
uri := testutil.TestSQLiteURI(t, "") + params
r, err := NewRegistry(uri, 100, testutil.Logger(t), sp)

var r *Registry
r, err = NewRegistry(uri, 100, testutil.SLogger(t), sp)
require.NoError(t, err)
t.Cleanup(r.Close)

Expand Down Expand Up @@ -227,7 +231,9 @@ func TestDropSameStress(t *testing.T) {
} {
t.Run(testName, func(t *testing.T) {
uri := testutil.TestSQLiteURI(t, "") + params
r, err := NewRegistry(uri, 100, testutil.Logger(t), sp)

var r *Registry
r, err = NewRegistry(uri, 100, testutil.SLogger(t), sp)
require.NoError(t, err)
t.Cleanup(r.Close)

Expand Down Expand Up @@ -280,7 +286,9 @@ func TestCreateDropSameStress(t *testing.T) {
} {
t.Run(testName, func(t *testing.T) {
uri := testutil.TestSQLiteURI(t, "") + params
r, err := NewRegistry(uri, 100, testutil.Logger(t), sp)

var r *Registry
r, err = NewRegistry(uri, 100, testutil.SLogger(t), sp)
require.NoError(t, err)
t.Cleanup(r.Close)

Expand Down Expand Up @@ -329,7 +337,7 @@ func TestIndexesCreateDrop(t *testing.T) {
sp, err := state.NewProvider("")
require.NoError(t, err)

r, err := NewRegistry(testutil.TestSQLiteURI(t, ""), 100, testutil.Logger(t), sp)
r, err := NewRegistry(testutil.TestSQLiteURI(t, ""), 100, testutil.SLogger(t), sp)
require.NoError(t, err)
t.Cleanup(r.Close)

Expand Down
2 changes: 1 addition & 1 deletion internal/backends/sqlite/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestCollectionsStats(t *testing.T) {
sp, err := state.NewProvider("")
require.NoError(t, err)

r, err := metadata.NewRegistry(testutil.TestSQLiteURI(t, ""), 100, testutil.Logger(t), sp)
r, err := metadata.NewRegistry(testutil.TestSQLiteURI(t, ""), 100, testutil.SLogger(t), sp)
require.NoError(t, err)
t.Cleanup(r.Close)

Expand Down
3 changes: 2 additions & 1 deletion internal/handler/registry/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import (
"github.com/FerretDB/FerretDB/internal/backends/sqlite"
"github.com/FerretDB/FerretDB/internal/handler"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/logging"
)

// init registers "sqlite" handler.
func init() {
registry["sqlite"] = func(opts *NewHandlerOpts) (*handler.Handler, CloseBackendFunc, error) {
b, err := sqlite.NewBackend(&sqlite.NewBackendParams{
URI: opts.SQLiteURL,
L: opts.Logger.Named("sqlite"),
L: logging.WithName(opts.SLogger, "sqlite"),
P: opts.StateProvider,
BatchSize: opts.BatchSize,
})
Expand Down

0 comments on commit 8fdcfd9

Please sign in to comment.