Checkpoint without waiting on readers and log a busy checkpoint (closes #102)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
The 5-second checkpoint ran in TRUNCATE mode, which waits up to the 5-second busy timeout for every open read to finish, and it did so holding the database lock every batch write needs. It now runs in PASSIVE mode, which never waits; the writing connection's journal_size_limit (64 MiB) shrinks the WAL file when SQLite starts it again. Checkpoint reads SQLite's busy flag and returns ErrCheckpointBusy, which the maintainer logs as busy, at warning level, instead of as a completed checkpoint. Model: opus-5-5
This commit is contained in:
@@ -67,6 +67,10 @@ const (
|
||||
lockWaitTimeout = time.Millisecond
|
||||
// lockTestASN is the AS the batch write in the lock tests stores.
|
||||
lockTestASN = 64500
|
||||
// readerCheckpointLimit is how long a checkpoint, and a batch write
|
||||
// waiting for it, may take while a reader is open: well under the busy
|
||||
// timeout, which a checkpoint that waits for the reader takes.
|
||||
readerCheckpointLimit = time.Second
|
||||
)
|
||||
|
||||
// Parameters for the prefix-length sample tests.
|
||||
@@ -457,6 +461,33 @@ func TestWritingConnectionCache(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestWritingConnectionWALSizeLimit checks that the connection kept for
|
||||
// writing, the one that writes the WAL, has the WAL size limit: Checkpoint does
|
||||
// not truncate the WAL file, so the limit is what shrinks it.
|
||||
func TestWritingConnectionWALSizeLimit(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
db, err := New(&config.Config{StateDir: t.TempDir()}, logger.New())
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create database: %v", err)
|
||||
}
|
||||
defer func() { _ = db.Close() }()
|
||||
|
||||
var limit int64
|
||||
|
||||
err = db.writer.QueryRowContext(t.Context(), "PRAGMA journal_size_limit").
|
||||
Scan(&limit)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read the writing connection's journal_size_limit: %v",
|
||||
err)
|
||||
}
|
||||
|
||||
if limit != sqliteWALSizeLimitBytes {
|
||||
t.Errorf("writing connection: journal_size_limit = %d, want %d",
|
||||
limit, sqliteWALSizeLimitBytes)
|
||||
}
|
||||
}
|
||||
|
||||
// readPragma reads the integer value of PRAGMA name on connection i, c.
|
||||
func readPragma(
|
||||
ctx context.Context, t *testing.T, c *sql.Conn, i int, name string,
|
||||
@@ -799,6 +830,91 @@ func TestCheckpointLockWaitEndsWithContext(t *testing.T) {
|
||||
db.mu.Unlock()
|
||||
}
|
||||
|
||||
// TestCheckpointDoesNotWaitForReader keeps a read transaction on
|
||||
// live_routes_v4 open while a route is committed, and checks that Checkpoint,
|
||||
// and a batch write started while Checkpoint holds the database lock, both end
|
||||
// within readerCheckpointLimit. A TRUNCATE checkpoint waited the busy timeout
|
||||
// for the reader while holding the lock
|
||||
// (https://git.eeqj.de/sneak/routewatch/issues/102).
|
||||
func TestCheckpointDoesNotWaitForReader(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
db, messages := newDatabaseWithLockMessages(t)
|
||||
ctx := context.Background()
|
||||
ts := time.Now().UTC()
|
||||
|
||||
err := db.UpsertLiveRouteBatch(
|
||||
[]*LiveRoute{mkV4Route(t, "198.51.100.0/24", lockTestASN, ts)})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to store the route the reader reads: %v", err)
|
||||
}
|
||||
|
||||
reader, err := db.db.Conn(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open the reading connection: %v", err)
|
||||
}
|
||||
defer func() { _ = reader.Close() }()
|
||||
|
||||
// A plain BEGIN starts a transaction that only reads, unlike the BEGIN
|
||||
// IMMEDIATE of _txlock. It keeps the snapshot its first read takes until
|
||||
// it ends.
|
||||
_, err = reader.ExecContext(ctx, "BEGIN")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to begin the read transaction: %v", err)
|
||||
}
|
||||
defer func() { _, _ = reader.ExecContext(ctx, "ROLLBACK") }()
|
||||
|
||||
var routes int
|
||||
|
||||
err = reader.QueryRowContext(ctx,
|
||||
"SELECT count(*) FROM live_routes_v4").Scan(&routes)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read the live routes: %v", err)
|
||||
}
|
||||
|
||||
err = db.UpsertLiveRouteBatch(
|
||||
[]*LiveRoute{mkV4Route(t, "203.0.113.0/24", lockTestASN, ts)})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to store a route after the read: %v", err)
|
||||
}
|
||||
|
||||
started := time.Now()
|
||||
checkpointed := make(chan error, 1)
|
||||
|
||||
go func() { checkpointed <- db.Checkpoint(ctx) }()
|
||||
|
||||
waitForLockMessage(t, messages, lockHeldMessage, "Checkpoint", checkpointed)
|
||||
|
||||
// A route write, not an AS write: the first new AS loads the embedded AS
|
||||
// names, which takes seconds under -race.
|
||||
route := mkV4Route(t, "192.0.2.0/24", lockTestASN, ts)
|
||||
written := make(chan error, 1)
|
||||
|
||||
go func() { written <- db.UpsertLiveRouteBatch([]*LiveRoute{route}) }()
|
||||
|
||||
err = <-checkpointed
|
||||
if err != nil {
|
||||
t.Errorf("Checkpoint: %v", err)
|
||||
}
|
||||
|
||||
took := time.Since(started)
|
||||
if took >= readerCheckpointLimit {
|
||||
t.Errorf("Checkpoint took %v with a reader open, want under %v",
|
||||
took, readerCheckpointLimit)
|
||||
}
|
||||
|
||||
err = <-written
|
||||
if err != nil {
|
||||
t.Errorf("batch write during Checkpoint: %v", err)
|
||||
}
|
||||
|
||||
took = time.Since(started)
|
||||
if took >= readerCheckpointLimit {
|
||||
t.Errorf("batch write during Checkpoint ended after %v, want under %v",
|
||||
took, readerCheckpointLimit)
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetRandomPrefixesByLengthListsPrefixOnce stores one prefix of each
|
||||
// family learned from two peers in the same second and checks that the
|
||||
// prefix-length sample lists each of them once.
|
||||
|
||||
Reference in New Issue
Block a user