From 4a0f73dcc656049fa7a3156be6cc01653fc42bd6 Mon Sep 17 00:00:00 2001 From: Hayden Blauzvern Date: Wed, 17 Jan 2024 16:40:58 +0000 Subject: [PATCH] Change Redis value for locking mechanism 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 --- pkg/witness/publish_checkpoint.go | 4 +++- pkg/witness/publish_checkpoint_test.go | 10 +++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/pkg/witness/publish_checkpoint.go b/pkg/witness/publish_checkpoint.go index b812e3687..aad8e14da 100644 --- a/pkg/witness/publish_checkpoint.go +++ b/pkg/witness/publish_checkpoint.go @@ -151,7 +151,9 @@ func (c *CheckpointPublisher) publish(tc *trillianclient.TrillianClient, sTreeID // return value ignored, which is whether or not the entry was set // no error is thrown if the key already exists - successNX, err := c.redisClient.SetNX(ctx, key, hexCP, 0).Result() + // use smallest value as it's unused + value := true + successNX, err := c.redisClient.SetNX(ctx, key, value, 0).Result() if err != nil { c.reqCounter.With( map[string]string{ diff --git a/pkg/witness/publish_checkpoint_test.go b/pkg/witness/publish_checkpoint_test.go index c6453f35e..94ed9dd48 100644 --- a/pkg/witness/publish_checkpoint_test.go +++ b/pkg/witness/publish_checkpoint_test.go @@ -64,7 +64,7 @@ func TestPublishCheckpoint(t *testing.T) { redisClient, mock := redismock.NewClientMock() ts := time.Now().Truncate(time.Duration(freq) * time.Minute).UnixNano() - mock.Regexp().ExpectSetNX(fmt.Sprintf("%d/%d", treeID, ts), "[0-9a-fA-F]+", 0).SetVal(true) + mock.Regexp().ExpectSetNX(fmt.Sprintf("%d/%d", treeID, ts), true, 0).SetVal(true) mock.Regexp().ExpectSet(fmt.Sprintf("%d/latest", treeID), "[0-9a-fA-F]+", 0).SetVal("OK") publisher := NewCheckpointPublisher(context.Background(), mockTrillianLogClient, int64(treeID), hostname, signer, redisClient, uint(freq), counter) @@ -119,7 +119,7 @@ func TestPublishCheckpointMultiple(t *testing.T) { redisClient, mock := redismock.NewClientMock() ts := time.Now().Truncate(time.Duration(freq) * time.Minute).UnixNano() - mock.Regexp().ExpectSetNX(fmt.Sprintf("%d/%d", treeID, ts), "[0-9a-fA-F]+", 0).SetVal(true) + mock.Regexp().ExpectSetNX(fmt.Sprintf("%d/%d", treeID, ts), true, 0).SetVal(true) mock.Regexp().ExpectSet(fmt.Sprintf("%d/latest", treeID), "[0-9a-fA-F]+", 0).SetVal("OK") publisher := NewCheckpointPublisher(context.Background(), mockTrillianLogClient, int64(treeID), hostname, signer, redisClient, uint(freq), counter) @@ -128,7 +128,7 @@ func TestPublishCheckpointMultiple(t *testing.T) { defer cancel() redisClientEx, mockEx := redismock.NewClientMock() - mockEx.Regexp().ExpectSetNX(fmt.Sprintf("%d/%d", treeID, ts), "[0-9a-fA-F]+", 0).SetVal(false) + mockEx.Regexp().ExpectSetNX(fmt.Sprintf("%d/%d", treeID, ts), true, 0).SetVal(false) publisherEx := NewCheckpointPublisher(context.Background(), mockTrillianLogClient, int64(treeID), hostname, signer, redisClientEx, uint(freq), counter) ctxEx, cancelEx := context.WithCancel(context.Background()) publisherEx.StartPublisher(ctxEx) @@ -255,7 +255,7 @@ func TestPublishCheckpointRedisFailure(t *testing.T) { redisClient, mock := redismock.NewClientMock() // error on first redis call - mock.Regexp().ExpectSetNX(".+", "[0-9a-fA-F]+", 0).SetErr(errors.New("redis error")) + mock.Regexp().ExpectSetNX(".+", true, 0).SetErr(errors.New("redis error")) publisher := NewCheckpointPublisher(context.Background(), mockTrillianLogClient, int64(treeID), hostname, signer, redisClient, uint(freq), counter) ctx, cancel := context.WithCancel(context.Background()) @@ -298,7 +298,7 @@ func TestPublishCheckpointRedisLatestFailure(t *testing.T) { Return(&trillian.GetLatestSignedLogRootResponse{SignedLogRoot: &trillian.SignedLogRoot{LogRoot: mRoot}}, nil) redisClient, mock := redismock.NewClientMock() - mock.Regexp().ExpectSetNX(".+", "[0-9a-fA-F]+", 0).SetVal(true) + mock.Regexp().ExpectSetNX(".+", true, 0).SetVal(true) // error on second redis call mock.Regexp().ExpectSet(".*", "[0-9a-fA-F]+", 0).SetErr(errors.New("error"))