Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Redis value for locking mechanism #1957

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
haydentherapper marked this conversation as resolved.
Show resolved Hide resolved
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
Loading