Skip to content

Commit

Permalink
Add back TestCompact
Browse files Browse the repository at this point in the history
  • Loading branch information
marco6 committed Jun 18, 2024
1 parent d7cc713 commit 3726d7f
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ go.vet:
$(DQLITE_BUILD_SCRIPTS_DIR)/static-go-vet.sh ./...

go.test:
go test -tags=libsqlite3 -v ./test
go test -tags=libsqlite3 -v ./...

go.test.dqlite:
$(DQLITE_BUILD_SCRIPTS_DIR)/static-go-test.sh -v ./test
Expand Down
126 changes: 92 additions & 34 deletions pkg/kine/drivers/sqlite/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"context"
"database/sql"
"fmt"
"os"
"path"
"testing"

"github.com/canonical/k8s-dqlite/pkg/kine/drivers/generic"
"github.com/canonical/k8s-dqlite/pkg/kine/drivers/sqlite"
"github.com/canonical/k8s-dqlite/pkg/kine/logstructured/sqllog"
"github.com/canonical/k8s-dqlite/pkg/kine/server"
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus"
)

Expand All @@ -18,17 +20,17 @@ func init() {
}

func setupV0(db *sql.DB) error {

// Create the very old key_value table
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS kine
(
id INTEGER primary key autoincrement,
name INTEGER,
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
created INTEGER,
deleted INTEGER,
create_revision INTEGER NOT NULL,
prev_revision INTEGER,
ttl INTEGER,
lease INTEGER,
value BLOB,
old_value BLOB
)`); err != nil {
Expand All @@ -41,18 +43,13 @@ CREATE TABLE IF NOT EXISTS kine
func TestMigration(t *testing.T) {
const driver = "sqlite3"

folder, err := os.MkdirTemp("", "kine_test-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(folder)

folder := t.TempDir()
dbPath := path.Join(folder, "db.sqlite")
db, err := sql.Open(driver, dbPath)
defer db.Close()
if err != nil {
t.Fatal(err)
}
defer db.Close()

setupV0(db)

Expand All @@ -78,13 +75,63 @@ WHERE type = 'index'
}
}

func TestCompaction(t *testing.T) {
ctx := context.Background()

t.Run("SmallDatabaseDeleteEntry", func(t *testing.T) {
g := NewWithT(t)
backend, dialect, err := newBackend(ctx, t)
if err != nil {
t.Fatal(err)
}
defer dialect.DB.Close()

addEntries(ctx, dialect, 2)
deleteEntries(ctx, dialect, 1)

initialSize, err := backend.DbSize(ctx)
g.Expect(err).To(BeNil())

err = backend.DoCompact(ctx)
g.Expect(err).To(BeNil())

finalSize, err := backend.DbSize(ctx)
g.Expect(err).To(BeNil())

// Expecting no compaction
g.Expect(finalSize).To(BeNumerically("==", initialSize))
})

t.Run("LargeDatabaseDeleteFivePercent", func(t *testing.T) {
g := NewWithT(t)
backend, dialect, err := newBackend(ctx, t)
if err != nil {
t.Fatal(err)
}
defer dialect.DB.Close()

addEntries(ctx, dialect, 10_000)
deleteEntries(ctx, dialect, 500)

initialSize, err := backend.DbSize(ctx)
g.Expect(err).To(BeNil())

err = backend.DoCompact(ctx)
g.Expect(err).To(BeNil())

finalSize, err := backend.DbSize(ctx)
g.Expect(err).To(BeNil())

// Expecting compaction
g.Expect(finalSize).To(BeNumerically("<", initialSize))
})
}

func BenchmarkCompaction(b *testing.B) {
b.StopTimer()
ctx := context.Background()

dir := b.TempDir()
dataSource := path.Join(dir, "k8s.sqlite")
backend, dialect, err := sqlite.NewVariant(ctx, "sqlite3", dataSource)
backend, dialect, err := newBackend(ctx, b)
if err != nil {
b.Fatal(err)
}
Expand All @@ -98,8 +145,29 @@ func BenchmarkCompaction(b *testing.B) {
// that the deleted rows are about 5% of the total.
addCount := delCount * 20

// Insert addCount entries
_, err = dialect.DB.ExecContext(ctx, `
if err := addEntries(ctx, dialect, addCount); err != nil {
b.Fatal(err)
}
if err := deleteEntries(ctx, dialect, delCount); err != nil {
b.Fatal(err)
}

b.StartTimer()
err = backend.DoCompact(ctx)
if err != nil {
b.Fatal(err)
}
b.StopTimer()
}

func newBackend(ctx context.Context, tb testing.TB) (server.Backend, *generic.Generic, error) {
dir := tb.TempDir()
dataSource := path.Join(dir, "k8s.sqlite")
return sqlite.NewVariant(ctx, "sqlite3", dataSource)
}

func addEntries(ctx context.Context, dialect *generic.Generic, count int) error {
_, err := dialect.DB.ExecContext(ctx, `
WITH RECURSIVE gen_id AS(
SELECT COALESCE(MAX(id), 0)+1 AS id FROM kine
Expand All @@ -111,13 +179,12 @@ WITH RECURSIVE gen_id AS(
)
INSERT INTO kine
SELECT id, 'testkey-'||id, 1, 0, id, 0, 0, 'value-'||id, NULL FROM gen_id;
`, addCount)
if err != nil {
b.Fatal(err)
}
`, count)
return err
}

// Delete 5% of the entries
_, err = dialect.DB.ExecContext(ctx, fmt.Sprintf(`
func deleteEntries(ctx context.Context, dialect *generic.Generic, count int) error {
_, err := dialect.DB.ExecContext(ctx, fmt.Sprintf(`
INSERT INTO kine(
name, created, deleted, create_revision, prev_revision, lease, value, old_value
)
Expand All @@ -131,15 +198,6 @@ JOIN (
) maxkv ON maxkv.id = kv.id
WHERE kv.deleted = 0
ORDER BY kv.name
LIMIT %d`, delCount))
if err != nil {
b.Fatal(err)
}

b.StartTimer()
err = backend.DoCompact(ctx)
if err != nil {
b.Fatal(err)
}
b.StopTimer()
LIMIT %d`, count))
return err
}

0 comments on commit 3726d7f

Please sign in to comment.