1
0
Fork 0

Merge branch 'master' into auto-vac-ops-improv

master
Philip O'Toole 8 months ago
commit 4d0311814a

@ -2,6 +2,9 @@
### New features
- [PR #1619](https://github.com/rqlite/rqlite/pull/1619), [PR #1622](https://github.com/rqlite/rqlite/pull/1622): Support automatic `VACUUM` of the SQLite database. Fixes [#1609](https://github.com/rqlite/rqlite/issues/1609).
### Implementation changes and bug fixes
- [PR #1623](https://github.com/rqlite/rqlite/pull/1623): Unit tests for Swappable DB.
## 8.16.8 (January 20th 2024)
### Implementation changes and bug fixes
- [PR #1615](https://github.com/rqlite/rqlite/pull/1615): Add extensive WAL checkpoint test at the database level.

@ -87,6 +87,13 @@ func (s *SwappableDB) Query(q *command.Request, xTime bool) ([]*command.QueryRow
return s.db.Query(q, xTime)
}
// QueryStringStmt calls QueryStringStmt on the underlying database.
func (s *SwappableDB) QueryStringStmt(query string) ([]*command.QueryRows, error) {
s.dbMu.RLock()
defer s.dbMu.RUnlock()
return s.db.QueryStringStmt(query)
}
// VacuumInto calls VacuumInto on the underlying database.
func (s *SwappableDB) VacuumInto(path string) error {
s.dbMu.RLock()

@ -0,0 +1,121 @@
package db
import (
"os"
"testing"
)
// Test_OpenSwappable_Success tests that OpenSwappable correctly opens a database and returns
// a valid SwappableDB instance.
func Test_OpenSwappable_Success(t *testing.T) {
path := mustTempPath()
defer os.Remove(path)
// Attempt to open a swappable database
swappableDB, err := OpenSwappable(path, false, false)
if err != nil {
t.Fatalf("failed to open swappable database: %s", err)
}
defer swappableDB.Close()
// Verify that the returned SwappableDB is not nil
if swappableDB == nil {
t.Fatalf("expected non-nil SwappableDB")
}
// Confirm a file was created at the specified path
if !fileExists(path) {
t.Fatalf("database file not created at %s", path)
}
// Check the paths of the underlying database
if swappableDB.Path() != path {
t.Fatalf("expected swappable database path to be %s, got %s", path, swappableDB.Path())
}
}
// Test_OpenSwappable_InvalidPath tests that OpenSwappable returns an error when provided
// with an invalid file path.
func Test_OpenSwappable_InvalidPath(t *testing.T) {
invalidPath := "/invalid/path/to/database"
// Attempt to open a swappable database with an invalid path
swappableDB, err := OpenSwappable(invalidPath, false, false)
if err == nil {
swappableDB.Close()
t.Fatalf("expected an error when opening swappable database with invalid path, got nil")
}
// Check that no SwappableDB instance is returned
if swappableDB != nil {
t.Fatalf("expected nil SwappableDB instance, got non-nil")
}
}
// Test_SwapSuccess tests that the Swap function successfully swaps the underlying database.
func Test_SwapSuccess(t *testing.T) {
// Create a new database with content
srcPath := mustTempPath()
defer os.Remove(srcPath)
srcDB, err := Open(srcPath, false, false)
if err != nil {
t.Fatalf("failed to open source database: %s", err)
}
defer srcDB.Close()
mustExecute(srcDB, "CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name TEXT)")
mustExecute(srcDB, `INSERT INTO foo(name) VALUES("test")`)
// Create a SwappableDB with an empty database
swappablePath := mustTempPath()
defer os.Remove(swappablePath)
swappableDB, err := OpenSwappable(swappablePath, false, false)
if err != nil {
t.Fatalf("failed to open swappable database: %s", err)
}
defer swappableDB.Close()
// Perform the swap
if err := swappableDB.Swap(srcPath, false, false); err != nil {
t.Fatalf("failed to swap database: %s", err)
}
// Confirm the SwappableDB contains the data from the source database
rows, err := swappableDB.QueryStringStmt("SELECT * FROM foo")
if err != nil {
t.Fatalf("failed to query swapped database: %s", err)
}
if exp, got := `[{"columns":["id","name"],"types":["integer","text"],"values":[[1,"test"]]}]`, asJSON(rows); exp != got {
t.Fatalf("unexpected results after swap, expected %s, got %s", exp, got)
}
}
// Test_SwapInvalidSQLiteFile tests that the Swap function returns an error when provided
// with an invalid SQLite file.
func Test_SwapInvalidSQLiteFile(t *testing.T) {
// Create a SwappableDB with an empty database
swappablePath := mustTempPath()
defer os.Remove(swappablePath)
swappableDB, err := OpenSwappable(swappablePath, false, false)
if err != nil {
t.Fatalf("failed to open swappable database: %s", err)
}
defer swappableDB.Close()
// Create an invalid SQLite file
invalidSQLiteFilePath := mustTempPath()
defer os.Remove(invalidSQLiteFilePath)
file, err := os.Create(invalidSQLiteFilePath)
if err != nil {
t.Fatalf("failed to create invalid SQLite file: %s", err)
}
if _, err := file.WriteString("not a valid SQLite file"); err != nil {
t.Fatalf("failed to write to invalid SQLite file: %s", err)
}
file.Close()
// Attempt to swap with the invalid SQLite file
err = swappableDB.Swap(invalidSQLiteFilePath, false, false)
if err == nil {
t.Fatalf("expected an error when swapping with an invalid SQLite file, got nil")
}
}

@ -9,7 +9,7 @@
import os
import unittest
from helpers import Node, Cluster, d_, write_random_file, deprovision_node, random_string, TIMEOUT
from helpers import Node, Cluster, d_, write_random_file, deprovision_node, random_string, poll_query, TIMEOUT
RQLITED_PATH = os.environ['RQLITED_PATH']
@ -253,8 +253,7 @@ class TestAutoClusteringKVStores(unittest.TestCase):
n3.wait_for_leader()
n3.wait_for_all_applied()
self.assertEqual(n3.disco_mode(), mode)
j = n3.query('SELECT * FROM foo', level='none')
self.assertEqual(j, d_("{'results': [{'values': [[1, 'fiona']], 'types': ['integer', 'text'], 'columns': ['id', 'name']}]}"))
poll_query(n3, 'SELECT * FROM foo', d_("{'results': [{'values': [[1, 'fiona']], 'types': ['integer', 'text'], 'columns': ['id', 'name']}]}"))
# Add a fifth node, this time running in non-voter mode. Should join fine.
n4 = Node(RQLITED_PATH, '4', raft_voter=False)
@ -262,8 +261,7 @@ class TestAutoClusteringKVStores(unittest.TestCase):
n4.wait_for_leader()
n4.wait_for_all_applied()
self.assertEqual(n4.disco_mode(), mode)
j = n4.query('SELECT * FROM foo', level='none')
self.assertEqual(j, d_("{'results': [{'values': [[1, 'fiona']], 'types': ['integer', 'text'], 'columns': ['id', 'name']}]}"))
poll_query(n4, 'SELECT * FROM foo', d_("{'results': [{'values': [[1, 'fiona']], 'types': ['integer', 'text'], 'columns': ['id', 'name']}]}"))
deprovision_node(n1)
deprovision_node(n2)

Loading…
Cancel
Save