mirror of https://github.com/hashicorp/boundary
feat (clientcache): use a persistent cache (#5051)
By default, we will use a persistent cache for the
client cache. This will allow us to keep the cache
across restarts of the server and it will also
reduce the amount of time it takes to start the
server while reducing the amount of memory used.
This change also includes a validation of the cache
schema version. If the schema version is different
from the one expected, the cache will be
reset/recreated.
(cherry picked from commit cf8a51f383)
pull/5128/head
parent
384fc332be
commit
4d8a7dd39a
@ -0,0 +1,66 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestOpen(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
t.Run("success-file-url-with-reopening", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
db, err := Open(ctx, WithUrl(tmpDir+"/test.db"+fkPragma))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, db)
|
||||
assert.FileExists(t, tmpDir+"/test.db")
|
||||
|
||||
info, err := os.Stat(tmpDir + "/test.db")
|
||||
require.NoError(t, err)
|
||||
origCreatedAt := info.ModTime()
|
||||
|
||||
// Reopen the db and make sure the file is not recreated
|
||||
db, err = Open(ctx, WithUrl(tmpDir+"/test.db"+fkPragma))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, db)
|
||||
info, err = os.Stat(tmpDir + "/test.db")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, origCreatedAt, info.ModTime())
|
||||
})
|
||||
t.Run("success-mem-default-url", func(t *testing.T) {
|
||||
db, err := Open(ctx)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, db)
|
||||
})
|
||||
t.Run("recreate-on-version-mismatch", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
db, err := Open(ctx, WithUrl(tmpDir+"/test.db"+fkPragma))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, db)
|
||||
assert.FileExists(t, tmpDir+"/test.db")
|
||||
info, err := os.Stat(tmpDir + "/test.db")
|
||||
require.NoError(t, err)
|
||||
origCreatedAt := info.ModTime()
|
||||
|
||||
// Reopen the db with a different schema version: forcing the db to be recreated
|
||||
db, err = Open(ctx, WithUrl(tmpDir+"/test.db"+fkPragma), withTestValidSchemaVersion("2"))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, db)
|
||||
info, err = os.Stat(tmpDir + "/test.db")
|
||||
require.NoError(t, err)
|
||||
// The file should have been recreated with a new timestamp
|
||||
assert.NotEqual(t, origCreatedAt, info.ModTime())
|
||||
})
|
||||
}
|
||||
|
||||
const (
|
||||
dotDirname = ".boundary"
|
||||
dbFileName = "cache.db"
|
||||
fkPragma = "?_pragma=foreign_keys(1)"
|
||||
)
|
||||
@ -0,0 +1,11 @@
|
||||
-- Copyright (c) HashiCorp, Inc.
|
||||
-- SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
-- cannot vacuum from within a transaction, so we're not using a transaction
|
||||
-- when running these statements
|
||||
PRAGMA writable_schema = 1;
|
||||
DELETE FROM sqlite_master;
|
||||
PRAGMA writable_schema = 0;
|
||||
VACUUM;
|
||||
PRAGMA integrity_check;
|
||||
|
||||
Loading…
Reference in new issue