Skip to content

Commit

Permalink
Change Redis value for locking mechanism (#1957)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
haydentherapper authored Jan 17, 2024
1 parent ca115c9 commit fc28ac1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 3 additions & 1 deletion pkg/witness/publish_checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
10 changes: 5 additions & 5 deletions pkg/witness/publish_checkpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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"))

Expand Down

0 comments on commit fc28ac1

Please sign in to comment.