unified-storage: sqlkv skeleton (#115176)

* implement sqlkv skeleton and include sqlkv in badgerkv tests
This commit is contained in:
Will Assis
2025-12-15 08:56:15 -05:00
committed by GitHub
parent 7c6475262d
commit 12dd3dffe0
11 changed files with 511 additions and 108 deletions
+87 -25
View File
@@ -7,6 +7,10 @@ import (
"time"
"github.com/bwmarrin/snowflake"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/storage/unified/sql/db/dbimpl"
"github.com/grafana/grafana/pkg/tests/testsuite"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -21,6 +25,20 @@ func setupTestEventStore(t *testing.T) *eventStore {
return newEventStore(kv)
}
func TestMain(m *testing.M) {
testsuite.Run(m)
}
// nolint:unused
func setupTestEventStoreSqlKv(t *testing.T) *eventStore {
dbstore := db.InitTestDB(t)
eDB, err := dbimpl.ProvideResourceDB(dbstore, setting.NewCfg(), nil)
require.NoError(t, err)
kv, err := NewSQLKV(eDB)
require.NoError(t, err)
return newEventStore(kv)
}
func TestNewEventStore(t *testing.T) {
store := setupTestEventStore(t)
assert.NotNil(t, store.kv)
@@ -180,10 +198,21 @@ func TestEventStore_ParseEventKey(t *testing.T) {
assert.Equal(t, originalKey, parsedKey)
}
func TestEventStore_Save_Get(t *testing.T) {
ctx := context.Background()
store := setupTestEventStore(t)
func runEventStoreTestWith(t *testing.T, storeName string, newStoreFn func(*testing.T) *eventStore, testFn func(*testing.T, context.Context, *eventStore)) {
t.Run(storeName, func(t *testing.T) {
ctx := context.Background()
store := newStoreFn(t)
testFn(t, ctx, store)
})
}
func TestEventStore_Save_Get(t *testing.T) {
runEventStoreTestWith(t, "badger", setupTestEventStore, testEventStoreSaveGet)
// enable this when sqlkv is ready
// runEventStoreTestWith(t, "sqlkv", setupTestEventStoreSqlKv, testEventStoreSaveGet)
}
func testEventStoreSaveGet(t *testing.T, ctx context.Context, store *eventStore) {
event := Event{
Namespace: "default",
Group: "apps",
@@ -216,9 +245,12 @@ func TestEventStore_Save_Get(t *testing.T) {
}
func TestEventStore_Get_NotFound(t *testing.T) {
ctx := context.Background()
store := setupTestEventStore(t)
runEventStoreTestWith(t, "badger", setupTestEventStore, testEventStoreGetNotFound)
// enable this when sqlkv is ready
// runEventStoreTestWith(t, "sqlkv", setupTestEventStoreSqlKv, testEventStoreGetNotFound)
}
func testEventStoreGetNotFound(t *testing.T, ctx context.Context, store *eventStore) {
nonExistentKey := EventKey{
Namespace: "default",
Group: "apps",
@@ -233,9 +265,12 @@ func TestEventStore_Get_NotFound(t *testing.T) {
}
func TestEventStore_LastEventKey(t *testing.T) {
ctx := context.Background()
store := setupTestEventStore(t)
runEventStoreTestWith(t, "badger", setupTestEventStore, testEventStoreLastEventKey)
// enable this when sqlkv is ready
// runEventStoreTestWith(t, "sqlkv", setupTestEventStoreSqlKv, testEventStoreLastEventKey)
}
func testEventStoreLastEventKey(t *testing.T, ctx context.Context, store *eventStore) {
// Test when no events exist
_, err := store.LastEventKey(ctx)
assert.Error(t, err)
@@ -292,9 +327,12 @@ func TestEventStore_LastEventKey(t *testing.T) {
}
func TestEventStore_ListKeysSince(t *testing.T) {
ctx := context.Background()
store := setupTestEventStore(t)
runEventStoreTestWith(t, "badger", setupTestEventStore, testEventStoreListKeysSince)
// enable this when sqlkv is ready
// runEventStoreTestWith(t, "sqlkv", setupTestEventStoreSqlKv, testEventStoreListKeysSince)
}
func testEventStoreListKeysSince(t *testing.T, ctx context.Context, store *eventStore) {
// Add events with different resource versions
events := []Event{
{
@@ -349,9 +387,12 @@ func TestEventStore_ListKeysSince(t *testing.T) {
}
func TestEventStore_ListSince(t *testing.T) {
ctx := context.Background()
store := setupTestEventStore(t)
runEventStoreTestWith(t, "badger", setupTestEventStore, testEventStoreListSince)
// enable this when sqlkv is ready
// runEventStoreTestWith(t, "sqlkv", setupTestEventStoreSqlKv, testEventStoreListSince)
}
func testEventStoreListSince(t *testing.T, ctx context.Context, store *eventStore) {
// Add events with different resource versions
events := []Event{
{
@@ -404,9 +445,12 @@ func TestEventStore_ListSince(t *testing.T) {
}
func TestEventStore_ListSince_Empty(t *testing.T) {
ctx := context.Background()
store := setupTestEventStore(t)
runEventStoreTestWith(t, "badger", setupTestEventStore, testEventStoreListSinceEmpty)
// enable this when sqlkv is ready
// runEventStoreTestWith(t, "sqlkv", setupTestEventStoreSqlKv, testEventStoreListSinceEmpty)
}
func testEventStoreListSinceEmpty(t *testing.T, ctx context.Context, store *eventStore) {
// List events when store is empty
retrievedEvents := make([]Event, 0)
for event, err := range store.ListSince(ctx, 0) {
@@ -459,9 +503,12 @@ func TestEventKey_Struct(t *testing.T) {
}
func TestEventStore_Save_InvalidJSON(t *testing.T) {
ctx := context.Background()
store := setupTestEventStore(t)
runEventStoreTestWith(t, "badger", setupTestEventStore, testEventStoreSaveInvalidJSON)
// enable this when sqlkv is ready
// runEventStoreTestWith(t, "sqlkv", setupTestEventStoreSqlKv, testEventStoreSaveInvalidJSON)
}
func testEventStoreSaveInvalidJSON(t *testing.T, ctx context.Context, store *eventStore) {
// This should work fine as the Event struct should be serializable
event := Event{
Namespace: "default",
@@ -477,9 +524,12 @@ func TestEventStore_Save_InvalidJSON(t *testing.T) {
}
func TestEventStore_CleanupOldEvents(t *testing.T) {
ctx := context.Background()
store := setupTestEventStore(t)
runEventStoreTestWith(t, "badger", setupTestEventStore, testEventStoreCleanupOldEvents)
// enable this when sqlkv is ready
// runEventStoreTestWith(t, "sqlkv", setupTestEventStoreSqlKv, testEventStoreCleanupOldEvents)
}
func testEventStoreCleanupOldEvents(t *testing.T, ctx context.Context, store *eventStore) {
now := time.Now()
oldRV := snowflakeFromTime(now.Add(-48 * time.Hour)) // 48 hours ago
recentRV := snowflakeFromTime(now.Add(-1 * time.Hour)) // 1 hour ago
@@ -565,9 +615,12 @@ func TestEventStore_CleanupOldEvents(t *testing.T) {
}
func TestEventStore_CleanupOldEvents_NoOldEvents(t *testing.T) {
ctx := context.Background()
store := setupTestEventStore(t)
runEventStoreTestWith(t, "badger", setupTestEventStore, testEventStoreCleanupOldEventsNoOldEvents)
// enable this when sqlkv is ready
// runEventStoreTestWith(t, "sqlkv", setupTestEventStoreSqlKv, testEventStoreCleanupOldEventsNoOldEvents)
}
func testEventStoreCleanupOldEventsNoOldEvents(t *testing.T, ctx context.Context, store *eventStore) {
// Create an event 1 hour old
rv := snowflakeFromTime(time.Now().Add(-1 * time.Hour))
event := Event{
@@ -603,9 +656,12 @@ func TestEventStore_CleanupOldEvents_NoOldEvents(t *testing.T) {
}
func TestEventStore_CleanupOldEvents_EmptyStore(t *testing.T) {
ctx := context.Background()
store := setupTestEventStore(t)
runEventStoreTestWith(t, "badger", setupTestEventStore, testEventStoreCleanupOldEventsEmptyStore)
// enable this when sqlkv is ready
// runEventStoreTestWith(t, "sqlkv", setupTestEventStoreSqlKv, testEventStoreCleanupOldEventsEmptyStore)
}
func testEventStoreCleanupOldEventsEmptyStore(t *testing.T, ctx context.Context, store *eventStore) {
// Clean up events from empty store
deletedCount, err := store.CleanupOldEvents(ctx, time.Now().Add(-24*time.Hour))
require.NoError(t, err)
@@ -613,9 +669,12 @@ func TestEventStore_CleanupOldEvents_EmptyStore(t *testing.T) {
}
func TestEventStore_BatchDelete(t *testing.T) {
ctx := context.Background()
store := setupTestEventStore(t)
runEventStoreTestWith(t, "badger", setupTestEventStore, testEventStoreBatchDelete)
// enable this when sqlkv is ready
// runEventStoreTestWith(t, "sqlkv", setupTestEventStoreSqlKv, testEventStoreBatchDelete)
}
func testEventStoreBatchDelete(t *testing.T, ctx context.Context, store *eventStore) {
// Create multiple events (more than batch size to test batching)
eventKeys := make([]string, 75)
for i := 0; i < 75; i++ {
@@ -722,9 +781,12 @@ func TestSnowflakeFromTime(t *testing.T) {
}
func TestListKeysSince_WithSnowflakeTime(t *testing.T) {
ctx := context.Background()
store := setupTestEventStore(t)
runEventStoreTestWith(t, "badger", setupTestEventStore, testListKeysSinceWithSnowflakeTime)
// enable this when sqlkv is ready
// runEventStoreTestWith(t, "sqlkv", setupTestEventStoreSqlKv, testListKeysSinceWithSnowflakeTime)
}
func testListKeysSinceWithSnowflakeTime(t *testing.T, ctx context.Context, store *eventStore) {
// Create events with snowflake-based resource versions at different times
now := time.Now()
events := []Event{