Skip to content

Commit 04ef762

Browse files
authored
Merge pull request #763 from ellemouton/fixLocalTempStoreBucket
firewalldb: fetch correct bucket for session level temp KV store
2 parents ba81aaf + 0259fc8 commit 04ef762

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

firewalldb/kvstores.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ import (
99

1010
/*
1111
The KVStores are stored in the following structure in the KV db. Note that
12-
the `perm` and `temp` buckets are identical in structure. The only difference is
13-
that the `temp` bucket is cleared on restart of the db.
12+
the `perm` and `temp` buckets are identical in structure. The only difference
13+
is that the `temp` bucket is cleared on restart of the db. The reason persisting
14+
the temporary store changes instead of just keeping an in-memory store is that
15+
we can then guarantee idempotency if changes are made to both the permanent and
16+
temporary stores.
1417
1518
rules -> perm -> rule-name -> global -> {k:v}
1619
-> sessions -> group ID -> session-kv-store -> {k:v}
@@ -81,11 +84,17 @@ type KVStoreTx interface {
8184
Local() KVStore
8285

8386
// GlobalTemp is similar to the Global store except that its contents
84-
// is cleared upon restart of the database.
87+
// is cleared upon restart of the database. The reason persisting the
88+
// temporary store changes instead of just keeping an in-memory store is
89+
// that we can then guarantee idempotency if changes are made to both
90+
// the permanent and temporary stores.
8591
GlobalTemp() KVStore
8692

8793
// LocalTemp is similar to the Local store except that its contents is
88-
// cleared upon restart of the database.
94+
// cleared upon restart of the database. The reason persisting the
95+
// temporary store changes instead of just keeping an in-memory store is
96+
// that we can then guarantee idempotency if changes are made to both
97+
// the permanent and temporary stores.
8998
LocalTemp() KVStore
9099
}
91100

@@ -268,7 +277,7 @@ func (tx *kvStoreTx) GlobalTemp() KVStore {
268277
//
269278
// NOTE: this is part of the KVStoreTx interface.
270279
func (tx *kvStoreTx) LocalTemp() KVStore {
271-
fn := getSessionRuleBucket(true, tx.ruleName, tx.groupID)
280+
fn := getSessionRuleBucket(false, tx.ruleName, tx.groupID)
272281
if tx.featureName != "" {
273282
fn = getSessionFeatureRuleBucket(
274283
false, tx.ruleName, tx.groupID, tx.featureName,

firewalldb/kvstores_test.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,39 @@ func TestKVStoreTxs(t *testing.T) {
6060

6161
// TestTempAndPermStores tests that the kv stores stored under the `temp` bucket
6262
// are properly deleted and re-initialised upon restart but that anything under
63-
// the `perm` bucket is retained.
63+
// the `perm` bucket is retained. We repeat the test for both the session level
64+
// KV stores and the session feature level stores.
6465
func TestTempAndPermStores(t *testing.T) {
66+
t.Run("session level kv store", func(t *testing.T) {
67+
testTempAndPermStores(t, false)
68+
})
69+
70+
t.Run("session feature level kv store", func(t *testing.T) {
71+
testTempAndPermStores(t, true)
72+
})
73+
}
74+
75+
// testTempAndPermStores tests that the kv stores stored under the `temp` bucket
76+
// are properly deleted and re-initialised upon restart but that anything under
77+
// the `perm` bucket is retained. If featureSpecificStore is true, then this
78+
// will test the session feature level KV stores. Otherwise, it will test the
79+
// session level KV stores.
80+
func testTempAndPermStores(t *testing.T, featureSpecificStore bool) {
6581
ctx := context.Background()
6682
tmpDir := t.TempDir()
6783

84+
var featureName string
85+
if featureSpecificStore {
86+
featureName = "auto-fees"
87+
}
88+
6889
db, err := NewDB(tmpDir, "test.db", nil)
6990
require.NoError(t, err)
7091
t.Cleanup(func() {
7192
_ = db.Close()
7293
})
7394

74-
store := db.GetKVStores("test-rule", [4]byte{1, 1, 1, 1}, "auto-fees")
95+
store := db.GetKVStores("test-rule", [4]byte{1, 1, 1, 1}, featureName)
7596

7697
err = store.Update(func(tx KVStoreTx) error {
7798
// Set an item in the temp store.
@@ -119,7 +140,7 @@ func TestTempAndPermStores(t *testing.T) {
119140
_ = db.Close()
120141
_ = os.RemoveAll(tmpDir)
121142
})
122-
store = db.GetKVStores("test-rule", [4]byte{1, 1, 1, 1}, "auto-fees")
143+
store = db.GetKVStores("test-rule", [4]byte{1, 1, 1, 1}, featureName)
123144

124145
// The temp store should no longer have the stored value but the perm
125146
// store should .

0 commit comments

Comments
 (0)