Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit e65e736

Browse files
authored
Redis: simplify DeleteAllKeysWithPrefix (#64407)
The `DeleteAllKeysWithPrefix` method is now only used in tests to ensure the test keyspace is clear. This PR makes it clear this method is only used in tests, and simplifies the implementation so it no longer needs a script + direct Redis connection. Another tiny Redis refactor to prepare for multi-tenancy work.
1 parent fae95fd commit e65e736

File tree

12 files changed

+108
-114
lines changed

12 files changed

+108
-114
lines changed

cmd/worker/internal/ratelimit/handler_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,11 @@ func TestHandler_Handle(t *testing.T) {
4141
return err
4242
},
4343
}
44+
4445
t.Cleanup(func() {
45-
c := pool.Get()
46-
err := redispool.DeleteAllKeysWithPrefix(c, prefix)
47-
if err != nil {
46+
if err := redispool.DeleteAllKeysWithPrefix(redispool.RedisKeyValue(pool), prefix); err != nil {
4847
t.Logf("Failed to clear redis: %+v\n", err)
4948
}
50-
c.Close()
5149
})
5250

5351
conf.Mock(&conf.Unified{

internal/ratelimit/globallimiter.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,9 +540,8 @@ func SetupForTest(t TB) {
540540
}
541541
}
542542

543-
err := redispool.DeleteAllKeysWithPrefix(c, tokenBucketGlobalPrefix)
544-
if err != nil {
545-
t.Fatalf("cold not clear test prefix: &v", err)
543+
if err := redispool.DeleteAllKeysWithPrefix(redispool.RedisKeyValue(pool), tokenBucketGlobalPrefix); err != nil {
544+
t.Fatalf("could not clear test prefix: &v", err)
546545
}
547546
}
548547

internal/ratelimit/globallimiter_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,7 @@ func redisPoolForTest(t *testing.T, prefix string) *redis.Pool {
318318
},
319319
}
320320

321-
c := pool.Get()
322-
t.Cleanup(func() {
323-
_ = c.Close()
324-
})
325-
326-
if err := redispool.DeleteAllKeysWithPrefix(c, prefix); err != nil {
321+
if err := redispool.DeleteAllKeysWithPrefix(redispool.RedisKeyValue(pool), prefix); err != nil {
327322
t.Logf("Could not clear test prefix name=%q prefix=%q error=%v", t.Name(), prefix, err)
328323
}
329324

internal/rcache/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ go_test(
3030
"requires-network",
3131
],
3232
deps = [
33-
"//internal/redispool",
3433
"@com_github_stretchr_testify//assert",
3534
"@com_github_stretchr_testify//require",
3635
],

internal/rcache/fifo_list.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,11 @@ func (l *FIFOList) kv() redispool.KeyValue {
134134
}
135135
return l._kv
136136
}
137+
138+
func bytes(s ...string) [][]byte {
139+
t := make([][]byte, len(s))
140+
for i, v := range s {
141+
t[i] = []byte(v)
142+
}
143+
return t
144+
}

internal/rcache/rcache.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,7 @@ func SetupForTest(t testing.TB) redispool.KeyValue {
272272
}
273273
}
274274

275-
err := redispool.DeleteAllKeysWithPrefix(c, globalPrefix)
276-
if err != nil {
275+
if err := redispool.DeleteAllKeysWithPrefix(kvMock, globalPrefix); err != nil {
277276
log15.Error("Could not clear test prefix", "name", t.Name(), "globalPrefix", globalPrefix, "error", err)
278277
}
279278

internal/rcache/rcache_test.go

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
package rcache
22

33
import (
4-
"reflect"
5-
"strconv"
64
"testing"
75
"time"
86

97
"github.com/stretchr/testify/assert"
10-
11-
"github.com/sourcegraph/sourcegraph/internal/redispool"
128
)
139

1410
func TestCache_namespace(t *testing.T) {
@@ -95,55 +91,6 @@ func TestCache_simple(t *testing.T) {
9591
}
9692
}
9793

98-
func TestCache_deleteAllKeysWithPrefix(t *testing.T) {
99-
kv := SetupForTest(t)
100-
101-
c := New(kv, "some_prefix")
102-
var aKeys, bKeys []string
103-
var key string
104-
for i := range 10 {
105-
if i%2 == 0 {
106-
key = "a:" + strconv.Itoa(i)
107-
aKeys = append(aKeys, key)
108-
} else {
109-
key = "b:" + strconv.Itoa(i)
110-
bKeys = append(bKeys, key)
111-
}
112-
113-
c.Set(key, []byte(strconv.Itoa(i)))
114-
}
115-
116-
pool := kv.Pool()
117-
118-
conn := pool.Get()
119-
defer conn.Close()
120-
121-
err := redispool.DeleteAllKeysWithPrefix(conn, c.rkeyPrefix()+"a")
122-
if err != nil {
123-
t.Error(err)
124-
}
125-
126-
getMulti := func(keys ...string) [][]byte {
127-
t.Helper()
128-
var vals [][]byte
129-
for _, k := range keys {
130-
v, _ := c.Get(k)
131-
vals = append(vals, v)
132-
}
133-
return vals
134-
}
135-
136-
vals := getMulti(aKeys...)
137-
if got, exp := vals, [][]byte{nil, nil, nil, nil, nil}; !reflect.DeepEqual(exp, got) {
138-
t.Errorf("Expected %v, but got %v", exp, got)
139-
}
140-
141-
vals = getMulti(bKeys...)
142-
if got, exp := vals, bytes("1", "3", "5", "7", "9"); !reflect.DeepEqual(exp, got) {
143-
t.Errorf("Expected %v, but got %v", exp, got)
144-
}
145-
}
146-
14794
func TestCache_Increase(t *testing.T) {
14895
kv := SetupForTest(t)
14996

@@ -274,11 +221,3 @@ func TestCache_Hashes(t *testing.T) {
274221
assert.NoError(t, err)
275222
assert.Equal(t, 0, del4)
276223
}
277-
278-
func bytes(s ...string) [][]byte {
279-
t := make([][]byte, len(s))
280-
for i, v := range s {
281-
t[i] = []byte(v)
282-
}
283-
return t
284-
}

internal/redispool/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ go_library(
99
"mocks.go",
1010
"redispool.go",
1111
"sysreq.go",
12-
"utils.go",
12+
"test_utils.go",
1313
],
1414
importpath = "github.com/sourcegraph/sourcegraph/internal/redispool",
1515
visibility = ["//:__subpackages__"],

internal/redispool/keyvalue_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,14 +389,15 @@ func redisKeyValueForTest(t *testing.T) redispool.KeyValue {
389389
}
390390
}
391391

392-
if err := redispool.DeleteAllKeysWithPrefix(c, prefix); err != nil {
392+
kv := redispool.RedisKeyValue(pool)
393+
if err := redispool.DeleteAllKeysWithPrefix(kv, prefix); err != nil {
393394
t.Logf("Could not clear test prefix name=%q prefix=%q error=%v", t.Name(), prefix, err)
394395
}
395396

396-
kv := redispool.RedisKeyValue(pool).(interface {
397+
redisKv := kv.(interface {
397398
WithPrefix(string) redispool.KeyValue
398399
})
399-
return kv.WithPrefix(prefix)
400+
return redisKv.WithPrefix(prefix)
400401
}
401402

402403
func bytes(ss ...string) [][]byte {

internal/redispool/redispool_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ package redispool
33
import (
44
"flag"
55
"os"
6+
"reflect"
7+
"strconv"
68
"testing"
9+
"time"
710

11+
"github.com/gomodule/redigo/redis"
812
"github.com/sourcegraph/log/logtest"
913
)
1014

@@ -32,3 +36,71 @@ func TestMain(m *testing.M) {
3236
logtest.Init(m)
3337
os.Exit(m.Run())
3438
}
39+
40+
func TestDeleteAllKeysWithPrefix(t *testing.T) {
41+
t.Helper()
42+
43+
pool := &redis.Pool{
44+
MaxIdle: 3,
45+
IdleTimeout: 240 * time.Second,
46+
Dial: func() (redis.Conn, error) {
47+
return redis.Dial("tcp", "127.0.0.1:6379")
48+
},
49+
TestOnBorrow: func(c redis.Conn, t time.Time) error {
50+
_, err := c.Do("PING")
51+
return err
52+
},
53+
}
54+
55+
c := pool.Get()
56+
defer c.Close()
57+
58+
// If we are not on CI, skip the test if our redis connection fails.
59+
if os.Getenv("CI") == "" {
60+
_, err := c.Do("PING")
61+
if err != nil {
62+
t.Skip("could not connect to redis", err)
63+
}
64+
}
65+
66+
kv := RedisKeyValue(pool)
67+
var aKeys, bKeys []string
68+
var key string
69+
for i := range 10 {
70+
if i%2 == 0 {
71+
key = "a:" + strconv.Itoa(i)
72+
aKeys = append(aKeys, key)
73+
} else {
74+
key = "b:" + strconv.Itoa(i)
75+
bKeys = append(bKeys, key)
76+
}
77+
78+
if err := kv.Set(key, []byte(strconv.Itoa(i))); err != nil {
79+
t.Fatalf("could not set key %s: %v", key, err)
80+
}
81+
}
82+
83+
if err := DeleteAllKeysWithPrefix(kv, "a"); err != nil {
84+
t.Fatal(err)
85+
}
86+
87+
getMulti := func(keys ...string) []string {
88+
t.Helper()
89+
var vals []string
90+
for _, k := range keys {
91+
v, _ := kv.Get(k).String()
92+
vals = append(vals, v)
93+
}
94+
return vals
95+
}
96+
97+
vals := getMulti(aKeys...)
98+
if got, exp := vals, []string{"", "", "", "", ""}; !reflect.DeepEqual(exp, got) {
99+
t.Errorf("Expected %v, but got %v", exp, got)
100+
}
101+
102+
vals = getMulti(bKeys...)
103+
if got, exp := vals, []string{"1", "3", "5", "7", "9"}; !reflect.DeepEqual(exp, got) {
104+
t.Errorf("Expected %v, but got %v", exp, got)
105+
}
106+
}

0 commit comments

Comments
 (0)