Skip to content

Commit

Permalink
move external redislock library to a pkg package
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan.rieckhof committed Jan 13, 2025
1 parent 338783c commit cce2020
Show file tree
Hide file tree
Showing 5 changed files with 344 additions and 5 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/PuerkitoBio/rehttp v1.4.0
github.com/adjust/rmq/v5 v5.2.0
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
github.com/bsm/redislock v0.9.3
github.com/caarlos0/env/v10 v10.0.0
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d
github.com/dave/jennifer v1.4.1
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/bsm/redislock v0.9.3 h1:osmvugkXGiLDEhzUPdM0EUtKpTEgLLuli4Ky2Z4vx38=
github.com/bsm/redislock v0.9.3/go.mod h1:Epf7AJLiSFwLCiZcfi6pWFO/8eAYrYpQXFxEDPoDeAk=
github.com/caarlos0/env/v10 v10.0.0 h1:yIHUBZGsyqCnpTkbjk8asUlx6RFhhEs+h7TOBdgdzXA=
github.com/caarlos0/env/v10 v10.0.0/go.mod h1:ZfulV76NvVPw3tm591U4SwL3Xx9ldzBP9aGxzeN7G18=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down
33 changes: 32 additions & 1 deletion maintenance/failover/failover.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ package failover
import (
"context"
"fmt"
"github.com/pace/bricks/pkg/lock/redislock"

Check failure on line 8 in maintenance/failover/failover.go

View workflow job for this annotation

GitHub Actions / test (1.23)

File is not properly formatted (gofumpt)

Check failure on line 8 in maintenance/failover/failover.go

View workflow job for this annotation

GitHub Actions / test (1.23)

File is not properly formatted (gofumpt)
"net/http"
"strings"
"sync"
"time"

"github.com/rs/zerolog"

"github.com/bsm/redislock"
"github.com/pace/bricks/backend/k8sapi"
"github.com/pace/bricks/maintenance/errors"
"github.com/pace/bricks/maintenance/health"
Expand Down Expand Up @@ -52,6 +52,7 @@ type ActivePassive struct {
clusterName string
timeToFailover time.Duration
locker *redislock.Client
redisClient *redis.Client

// access to the kubernetes api
k8sClient *k8sapi.Client
Expand All @@ -77,6 +78,7 @@ func NewActivePassive(clusterName string, timeToFailover time.Duration, redisCli
clusterName: clusterName,
timeToFailover: timeToFailover,
locker: redislock.New(redisClient),
redisClient: redisClient,
k8sClient: k8sClient,
}
health.SetCustomReadinessCheck(activePassive.Handler)
Expand All @@ -92,6 +94,8 @@ func (a *ActivePassive) Run(ctx context.Context) error {
defer errors.HandleWithCtx(ctx, "activepassive failover handler")

lockName := "activepassive:lock:" + a.clusterName
token := "activepassive:token:" + a.clusterName

logger := log.Ctx(ctx).With().Str("failover", lockName).Logger()
ctx = logger.WithContext(ctx)

Expand Down Expand Up @@ -124,6 +128,7 @@ func (a *ActivePassive) Run(ctx context.Context) error {
if a.getState() == ACTIVE {
err := lock.Refresh(ctx, a.timeToFailover, &redislock.Options{
RetryStrategy: redislock.LimitRetry(redislock.LinearBackoff(a.timeToFailover/3), 3),
Token: token,
})
if err != nil {
logger.Info().Err(err).Msg("failed to refresh the lock; becoming undefined...")
Expand All @@ -136,6 +141,7 @@ func (a *ActivePassive) Run(ctx context.Context) error {

lock, err = a.locker.Obtain(ctx, lockName, a.timeToFailover, &redislock.Options{
RetryStrategy: redislock.LimitRetry(redislock.LinearBackoff(a.timeToFailover/3), 3),
Token: token,
})
if err != nil {
if a.getState() != PASSIVE {
Expand All @@ -149,6 +155,31 @@ func (a *ActivePassive) Run(ctx context.Context) error {
logger.Debug().Msg("lock acquired; becoming active...")
a.becomeActive(ctx)

logger.Debug().Msg("check if lock exists")

// Verify that key exists, then, retrieve the value
keyExists, err := a.redisClient.Exists(ctx, lockName).Result()
if err != nil {
logger.Error().Err(err).Msgf("Stefan: Failed to check that lock/key '%v' exists", lockName)

continue
}

if keyExists == 0 {
logger.Info().Msgf("Stefan: Lock/Key '%s' does not exist", lockName)

continue
}

lockValue, err := a.redisClient.Get(ctx, lockName).Result()
if err != nil {
logger.Error().Err(err).Msg("Error getting key value")

continue
}

logger.Info().Msgf("Stefan: Key value is: %v", lockValue)

// Check TTL of the newly acquired lock
ttl, err := safeGetTTL(ctx, lock, logger)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/lock/redis/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"context"
"errors"
"fmt"
"github.com/pace/bricks/pkg/lock/redislock"
"sync"
"time"

redisbackend "github.com/pace/bricks/backend/redis"
pberrors "github.com/pace/bricks/maintenance/errors"

"github.com/bsm/redislock"
"github.com/redis/go-redis/v9"
"github.com/rs/zerolog/log"
)
Expand Down
Loading

0 comments on commit cce2020

Please sign in to comment.