Skip to content

Commit 1204196

Browse files
committed
fix some errors
1 parent 9667431 commit 1204196

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

redis/redis-checkpoint.go

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ import (
1616
par "github.com/vmware/vmware-go-kcl/clientlibrary/partition"
1717
)
1818

19-
type ErrLeaseNotAcquired struct {
20-
cause string
21-
}
22-
2319
// RedisCheckpoint implements the Checkpoint interface using Redis as a backend
2420
type RedisCheckpoint struct {
2521
prefix string
@@ -30,7 +26,8 @@ type RedisCheckpoint struct {
3026
kclConfig *cfg.KinesisClientLibConfiguration
3127
lastLeaseSync time.Time
3228
rs *redsync.Redsync
33-
mutex *redsync.Mutex
29+
shardPrefix string
30+
mutexPrefix string
3431
}
3532

3633
type RedisCheckpointOptions struct {
@@ -51,10 +48,6 @@ var (
5148
ctx = context.Background()
5249
)
5350

54-
func (e ErrLeaseNotAcquired) Error() string {
55-
return fmt.Sprintf("lease not acquired: %s", e.cause)
56-
}
57-
5851
func NewRedisCheckpoint(kclConfig *cfg.KinesisClientLibConfiguration, options *RedisCheckpointOptions) *RedisCheckpoint {
5952

6053
prefix := options.Prefix
@@ -70,6 +63,8 @@ func NewRedisCheckpoint(kclConfig *cfg.KinesisClientLibConfiguration, options *R
7063

7164
checkpointer := &RedisCheckpoint{
7265
prefix: prefix,
66+
shardPrefix: prefix + ":shard:",
67+
mutexPrefix: prefix + ":mutex:",
7368
redisEndpoint: redisEndpoint,
7469
LeaseDuration: kclConfig.FailoverTimeMillis,
7570
kclConfig: kclConfig,
@@ -132,7 +127,7 @@ func (checkpointer *RedisCheckpoint) GetLease(shard *par.ShardStatus, newAssignT
132127
claimRequest = currentCheckpointClaimRequest
133128
if newAssignTo != claimRequest && !isClaimRequestExpired {
134129
checkpointer.kclConfig.Logger.Debugf("another worker: %s has a claim on this shard. Not going to renew the lease", claimRequest)
135-
return errors.New(chk.ErrShardClaimed)
130+
return errors.New(chk.ErrShardClaimed + " " + claimRequest)
136131
}
137132
}
138133
}
@@ -145,11 +140,11 @@ func (checkpointer *RedisCheckpoint) GetLease(shard *par.ShardStatus, newAssignT
145140
return err
146141
} else if checkpointer.kclConfig.EnableLeaseStealing {
147142
if time.Now().UTC().Before(currentLeaseTimeout) && assignedTo != newAssignTo && !isClaimRequestExpired {
148-
return ErrLeaseNotAcquired{fmt.Sprintf("current lease timeout not yet expired shard: %s, leaseTimeout: %s, assignedTo: %s, newAssignedTo: %s", shard.ID, currentLeaseTimeout, assignedTo, newAssignTo)}
143+
return chk.ErrLeaseNotAcquired{}
149144
}
150145
} else {
151146
if time.Now().UTC().Before(currentLeaseTimeout) && assignedTo != newAssignTo {
152-
return ErrLeaseNotAcquired{fmt.Sprintf("current lease timeout not yet expired shard: %s, leaseTimeout: %s, assignedTo: %s, newAssignedTo: %s", shard.ID, currentLeaseTimeout, assignedTo, newAssignTo)}
147+
return chk.ErrLeaseNotAcquired{}
153148
}
154149
checkpointer.kclConfig.Logger.Debugf("Attempting to get a lock for shard: %s, leaseTimeout: %s, assignedTo: %s, newAssignedTo: %s", shard.ID, currentLeaseTimeout, assignedTo, newAssignTo)
155150
}
@@ -166,7 +161,8 @@ func (checkpointer *RedisCheckpoint) GetLease(shard *par.ShardStatus, newAssignT
166161
err = checkpointer.putItem(newCheckpoint)
167162

168163
if err != nil {
169-
return ErrLeaseNotAcquired{err.Error()}
164+
checkpointer.kclConfig.Logger.Errorf("putItem err : %s", err)
165+
return chk.ErrLeaseNotAcquired{}
170166
}
171167

172168
shard.Mux.Lock()
@@ -192,6 +188,7 @@ func (checkpointer *RedisCheckpoint) CheckpointSequence(shard *par.ShardStatus)
192188
Checkpoint: shard.GetCheckpoint(),
193189
AssignedTo: shard.GetLeaseOwner(),
194190
LeaseTimeout: leaseTimeout,
191+
ClaimRequest: shard.ClaimRequest,
195192
}
196193

197194
if len(shard.ParentShardId) > 0 {
@@ -219,10 +216,8 @@ func (checkpointer *RedisCheckpoint) FetchCheckpoint(shard *par.ShardStatus) err
219216
return chk.ErrSequenceIDNotFound
220217
}
221218

222-
sequenceID := checkpoint.Checkpoint
223-
224-
checkpointer.kclConfig.Logger.Debugf("Retrieved Shard Iterator %s", sequenceID)
225-
shard.SetCheckpoint(sequenceID)
219+
checkpointer.kclConfig.Logger.Debugf("Retrieved Shard Iterator %s", checkpoint.Checkpoint)
220+
shard.SetCheckpoint(checkpoint.Checkpoint)
226221
shard.SetLeaseOwner(checkpoint.AssignedTo)
227222

228223
// Use up-to-date leaseTimeout to avoid ConditionalCheckFailedException when claiming
@@ -360,21 +355,21 @@ func (checkpointer *RedisCheckpoint) syncLeases(shardStatus map[string]*par.Shar
360355
checkpointer.lastLeaseSync = time.Now()
361356
var cursor uint64
362357

363-
iter := checkpointer.svc.Scan(ctx, cursor, checkpointer.prefix+"*", 1).Iterator()
358+
iter := checkpointer.svc.Scan(ctx, cursor, checkpointer.shardPrefix+"*", 1).Iterator()
364359

365360
for iter.Next(ctx) {
366361
key := iter.Val()
367362
j, err := checkpointer.svc.Get(ctx, key).Result()
368363

369364
if err != nil { // just logging
370-
log.Errorf("syncLeases Get Error: %s, %s", err.Error(), key)
365+
log.Errorf("syncLeases Get Error: %s, %s", err, key)
371366
continue
372367
}
373368

374369
cp, err := jsonToCheckpoint(j)
375370

376371
if err != nil { // just logging
377-
log.Errorf("syncLeases jsonToCheckpoint Error: %s, %s", err.Error(), j)
372+
log.Errorf("syncLeases jsonToCheckpoint Error: %s, %s", err, j)
378373
continue
379374
}
380375

@@ -401,7 +396,7 @@ func (checkpointer *RedisCheckpoint) putItem(newCheckpoint *ShardCheckpoint) err
401396
var _ string
402397

403398
if j, err = json.Marshal(newCheckpoint); err == nil {
404-
if err = checkpointer.svc.Set(ctx, checkpointer.prefix+newCheckpoint.ShardID, string(j), 0).Err(); err == nil {
399+
if err = checkpointer.svc.Set(ctx, checkpointer.shardPrefix+newCheckpoint.ShardID, string(j), 0).Err(); err == nil {
405400
checkpointer.kclConfig.Logger.Infof("putItem : %s", j)
406401
return nil
407402
}
@@ -419,7 +414,7 @@ func jsonToCheckpoint(j string) (*ShardCheckpoint, error) {
419414
}
420415

421416
func (checkpointer *RedisCheckpoint) getItem(shardID string) (*ShardCheckpoint, error) {
422-
if r, err := checkpointer.svc.Get(ctx, checkpointer.prefix+shardID).Result(); err != nil {
417+
if r, err := checkpointer.svc.Get(ctx, checkpointer.shardPrefix+shardID).Result(); err != nil {
423418
if err == Nil {
424419
return &ShardCheckpoint{ShardID: shardID}, nil
425420
} else {
@@ -432,7 +427,7 @@ func (checkpointer *RedisCheckpoint) getItem(shardID string) (*ShardCheckpoint,
432427
}
433428

434429
func (checkpointer *RedisCheckpoint) removeItem(shardID string) error {
435-
err := checkpointer.svc.Del(ctx, checkpointer.prefix+shardID).Err()
430+
err := checkpointer.svc.Del(ctx, checkpointer.shardPrefix+shardID).Err()
436431

437432
if err != nil {
438433
checkpointer.kclConfig.Logger.Infof("removeItem : %s", shardID)
@@ -442,7 +437,7 @@ func (checkpointer *RedisCheckpoint) removeItem(shardID string) error {
442437
}
443438

444439
func (checkpointer *RedisCheckpoint) lock(shardID string) (*redsync.Mutex, error) {
445-
mutexname := checkpointer.prefix + ":locks:" + shardID
440+
mutexname := checkpointer.mutexPrefix + shardID
446441
mutex := checkpointer.rs.NewMutex(mutexname)
447442

448443
if err := mutex.Lock(); err != nil {

0 commit comments

Comments
 (0)