unified-storage: add ListSinceModified to kv store (#110250)

* implement ListKeysSince in event store

 implement data store version

---------

Co-authored-by: Georges Chaudy <chaudyg@gmail.com>
This commit is contained in:
Will Assis
2025-09-04 12:26:40 -04:00
committed by GitHub
co-authored by Georges Chaudy
parent eed8d189ac
commit ea7c370edd
4 changed files with 566 additions and 31 deletions
@@ -38,8 +38,9 @@ func TestEventKey_String(t *testing.T) {
Resource: "resource",
Name: "test-resource",
ResourceVersion: 1000,
Action: "created",
},
expected: "1000~default~apps~resource~test-resource",
expected: "1000~default~apps~resource~test-resource~created",
},
{
name: "empty namespace",
@@ -49,8 +50,9 @@ func TestEventKey_String(t *testing.T) {
Resource: "resource",
Name: "test-resource",
ResourceVersion: 2000,
Action: "updated",
},
expected: "2000~~apps~resource~test-resource",
expected: "2000~~apps~resource~test-resource~updated",
},
{
name: "special characters in name",
@@ -60,8 +62,9 @@ func TestEventKey_String(t *testing.T) {
Resource: "resource",
Name: "test-resource-with-dashes",
ResourceVersion: 3000,
Action: "deleted",
},
expected: "3000~test-ns~apps~resource~test-resource-with-dashes",
expected: "3000~test-ns~apps~resource~test-resource-with-dashes~deleted",
},
}
@@ -82,35 +85,38 @@ func TestEventKey_Validate(t *testing.T) {
}{
{
name: "valid key",
key: "1000~default~apps~resource~test-resource",
key: "1000~default~apps~resource~test-resource~created",
expected: EventKey{
ResourceVersion: 1000,
Namespace: "default",
Group: "apps",
Resource: "resource",
Name: "test-resource",
Action: "created",
},
},
{
name: "empty namespace",
key: "2000~~apps~resource~test-resource",
key: "2000~~apps~resource~test-resource~updated",
expected: EventKey{
ResourceVersion: 2000,
Namespace: "",
Group: "apps",
Resource: "resource",
Name: "test-resource",
Action: "updated",
},
},
{
name: "special characters in name",
key: "3000~test-ns~apps~resource~test-resource-with-dashes",
key: "3000~test-ns~apps~resource~test-resource-with-dashes~updated",
expected: EventKey{
ResourceVersion: 3000,
Namespace: "test-ns",
Group: "apps",
Resource: "resource",
Name: "test-resource-with-dashes",
Action: "updated",
},
},
{
@@ -120,12 +126,12 @@ func TestEventKey_Validate(t *testing.T) {
},
{
name: "invalid key - too many parts",
key: "1000~default~apps~resource~test~extra",
key: "1000~default~apps~resource~test~extra~parts",
expectError: true,
},
{
name: "invalid resource version",
key: "invalid~default~apps~resource~test",
key: "invalid~default~apps~resource~test~cerated",
expectError: true,
},
{
@@ -157,6 +163,7 @@ func TestEventStore_ParseEventKey(t *testing.T) {
Group: "apps",
Resource: "resource",
Name: "test-resource",
Action: "created",
}
// Convert to string and back
@@ -193,6 +200,7 @@ func TestEventStore_Save_Get(t *testing.T) {
Resource: event.Resource,
Name: event.Name,
ResourceVersion: event.ResourceVersion,
Action: event.Action,
}
retrievedEvent, err := store.Get(ctx, eventKey)
@@ -210,6 +218,7 @@ func TestEventStore_Get_NotFound(t *testing.T) {
Resource: "resource",
Name: "non-existent",
ResourceVersion: 9999,
Action: "created",
}
_, err := store.Get(ctx, nonExistentKey)
@@ -269,11 +278,69 @@ func TestEventStore_LastEventKey(t *testing.T) {
Resource: "resource",
Name: "test-2",
ResourceVersion: 3000,
Action: "created",
}
assert.Equal(t, expectedKey, lastKey)
}
func TestEventStore_ListKeysSince(t *testing.T) {
ctx := context.Background()
store := setupTestEventStore(t)
// Add events with different resource versions
events := []Event{
{
Namespace: "default",
Group: "apps",
Resource: "resource",
Name: "test-1",
ResourceVersion: 1000,
Action: DataActionCreated,
},
{
Namespace: "default",
Group: "apps",
Resource: "resource",
Name: "test-2",
ResourceVersion: 2000,
Action: DataActionUpdated,
},
{
Namespace: "default",
Group: "apps",
Resource: "resource",
Name: "test-3",
ResourceVersion: 3000,
Action: DataActionDeleted,
},
}
// Save all events
for _, event := range events {
err := store.Save(ctx, event)
require.NoError(t, err)
}
// List events since RV 1500 (should get events with RV 2000 and 3000)
retrievedEvents := make([]string, 0, 2)
for eventKey, err := range store.ListKeysSince(ctx, 1500) {
require.NoError(t, err)
retrievedEvents = append(retrievedEvents, eventKey)
}
// Should return events in ascending order of resource version
require.Len(t, retrievedEvents, 2)
evt1, err := ParseEventKey(retrievedEvents[0])
require.NoError(t, err)
assert.Equal(t, int64(2000), evt1.ResourceVersion)
assert.Equal(t, "test-2", evt1.Name)
evt2, err := ParseEventKey(retrievedEvents[1])
require.NoError(t, err)
assert.Equal(t, int64(3000), evt2.ResourceVersion)
assert.Equal(t, "test-3", evt2.Name)
}
func TestEventStore_ListSince(t *testing.T) {
ctx := context.Background()
store := setupTestEventStore(t)
@@ -322,7 +389,11 @@ func TestEventStore_ListSince(t *testing.T) {
// Should return events in descending order of resource version
require.Len(t, retrievedEvents, 2)
assert.Equal(t, int64(2000), retrievedEvents[0].ResourceVersion)
assert.Equal(t, "test-2", retrievedEvents[0].Name)
assert.Equal(t, DataActionUpdated, retrievedEvents[0].Action)
assert.Equal(t, int64(3000), retrievedEvents[1].ResourceVersion)
assert.Equal(t, "test-3", retrievedEvents[1].Name)
assert.Equal(t, DataActionDeleted, retrievedEvents[1].Action)
}
func TestEventStore_ListSince_Empty(t *testing.T) {
@@ -370,6 +441,7 @@ func TestEventKey_Struct(t *testing.T) {
Resource: "resource",
Name: "test-resource",
ResourceVersion: 1234567890,
Action: "created",
}
assert.Equal(t, "test-namespace", key.Namespace)