Skip to content

Commit fc28ac1

Browse files
Change Redis value for locking mechanism (#1957)
The lock value is never read. Only the key is used to determine if an instance has already created a checkpoint for a given period. Changing this to the smallest primitive value to reduce storage capacity needs. Signed-off-by: Hayden Blauzvern <[email protected]>
1 parent ca115c9 commit fc28ac1

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

pkg/witness/publish_checkpoint.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ func (c *CheckpointPublisher) publish(tc *trillianclient.TrillianClient, sTreeID
151151

152152
// return value ignored, which is whether or not the entry was set
153153
// no error is thrown if the key already exists
154-
successNX, err := c.redisClient.SetNX(ctx, key, hexCP, 0).Result()
154+
// use smallest value as it's unused
155+
value := true
156+
successNX, err := c.redisClient.SetNX(ctx, key, value, 0).Result()
155157
if err != nil {
156158
c.reqCounter.With(
157159
map[string]string{

pkg/witness/publish_checkpoint_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func TestPublishCheckpoint(t *testing.T) {
6464

6565
redisClient, mock := redismock.NewClientMock()
6666
ts := time.Now().Truncate(time.Duration(freq) * time.Minute).UnixNano()
67-
mock.Regexp().ExpectSetNX(fmt.Sprintf("%d/%d", treeID, ts), "[0-9a-fA-F]+", 0).SetVal(true)
67+
mock.Regexp().ExpectSetNX(fmt.Sprintf("%d/%d", treeID, ts), true, 0).SetVal(true)
6868
mock.Regexp().ExpectSet(fmt.Sprintf("%d/latest", treeID), "[0-9a-fA-F]+", 0).SetVal("OK")
6969

7070
publisher := NewCheckpointPublisher(context.Background(), mockTrillianLogClient, int64(treeID), hostname, signer, redisClient, uint(freq), counter)
@@ -119,7 +119,7 @@ func TestPublishCheckpointMultiple(t *testing.T) {
119119

120120
redisClient, mock := redismock.NewClientMock()
121121
ts := time.Now().Truncate(time.Duration(freq) * time.Minute).UnixNano()
122-
mock.Regexp().ExpectSetNX(fmt.Sprintf("%d/%d", treeID, ts), "[0-9a-fA-F]+", 0).SetVal(true)
122+
mock.Regexp().ExpectSetNX(fmt.Sprintf("%d/%d", treeID, ts), true, 0).SetVal(true)
123123
mock.Regexp().ExpectSet(fmt.Sprintf("%d/latest", treeID), "[0-9a-fA-F]+", 0).SetVal("OK")
124124

125125
publisher := NewCheckpointPublisher(context.Background(), mockTrillianLogClient, int64(treeID), hostname, signer, redisClient, uint(freq), counter)
@@ -128,7 +128,7 @@ func TestPublishCheckpointMultiple(t *testing.T) {
128128
defer cancel()
129129

130130
redisClientEx, mockEx := redismock.NewClientMock()
131-
mockEx.Regexp().ExpectSetNX(fmt.Sprintf("%d/%d", treeID, ts), "[0-9a-fA-F]+", 0).SetVal(false)
131+
mockEx.Regexp().ExpectSetNX(fmt.Sprintf("%d/%d", treeID, ts), true, 0).SetVal(false)
132132
publisherEx := NewCheckpointPublisher(context.Background(), mockTrillianLogClient, int64(treeID), hostname, signer, redisClientEx, uint(freq), counter)
133133
ctxEx, cancelEx := context.WithCancel(context.Background())
134134
publisherEx.StartPublisher(ctxEx)
@@ -255,7 +255,7 @@ func TestPublishCheckpointRedisFailure(t *testing.T) {
255255

256256
redisClient, mock := redismock.NewClientMock()
257257
// error on first redis call
258-
mock.Regexp().ExpectSetNX(".+", "[0-9a-fA-F]+", 0).SetErr(errors.New("redis error"))
258+
mock.Regexp().ExpectSetNX(".+", true, 0).SetErr(errors.New("redis error"))
259259

260260
publisher := NewCheckpointPublisher(context.Background(), mockTrillianLogClient, int64(treeID), hostname, signer, redisClient, uint(freq), counter)
261261
ctx, cancel := context.WithCancel(context.Background())
@@ -298,7 +298,7 @@ func TestPublishCheckpointRedisLatestFailure(t *testing.T) {
298298
Return(&trillian.GetLatestSignedLogRootResponse{SignedLogRoot: &trillian.SignedLogRoot{LogRoot: mRoot}}, nil)
299299

300300
redisClient, mock := redismock.NewClientMock()
301-
mock.Regexp().ExpectSetNX(".+", "[0-9a-fA-F]+", 0).SetVal(true)
301+
mock.Regexp().ExpectSetNX(".+", true, 0).SetVal(true)
302302
// error on second redis call
303303
mock.Regexp().ExpectSet(".*", "[0-9a-fA-F]+", 0).SetErr(errors.New("error"))
304304

0 commit comments

Comments
 (0)