Skip to content

Commit 44e2efd

Browse files
authored
Streamline lock client usage (#2214)
* Ensure that the lock client is used in a similar way and not getting a lock is handled like an error * Return error when owner is denied
1 parent 71597f6 commit 44e2efd

12 files changed

+34
-57
lines changed

controllers/bounce_processes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (c bounceProcesses) reconcile(_ context.Context, r *FoundationDBClusterReco
135135
}
136136
}
137137

138-
_, err = r.takeLock(logger, cluster, fmt.Sprintf("bouncing processes: %v", addresses))
138+
err = r.takeLock(logger, cluster, fmt.Sprintf("bouncing processes: %v", addresses))
139139
if err != nil {
140140
return &requeue{curError: err}
141141
}

controllers/change_coordinators.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (c changeCoordinators) reconcile(ctx context.Context, r *FoundationDBCluste
7575
return nil
7676
}
7777

78-
_, err = r.takeLock(logger, cluster, "changing coordinators")
78+
err = r.takeLock(logger, cluster, "changing coordinators")
7979
if err != nil {
8080
return &requeue{curError: err, delayedRequeue: true}
8181
}

controllers/cluster_controller.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -467,22 +467,14 @@ func (r *FoundationDBClusterReconciler) getLockClient(logger logr.Logger, cluste
467467
}
468468

469469
// takeLock attempts to acquire a lock.
470-
func (r *FoundationDBClusterReconciler) takeLock(logger logr.Logger, cluster *fdbv1beta2.FoundationDBCluster, action string) (bool, error) {
470+
func (r *FoundationDBClusterReconciler) takeLock(logger logr.Logger, cluster *fdbv1beta2.FoundationDBCluster, action string) error {
471471
logger.Info("Taking lock on cluster", "action", action)
472472
lockClient, err := r.getLockClient(logger, cluster)
473473
if err != nil {
474-
return false, err
475-
}
476-
477-
hasLock, err := lockClient.TakeLock()
478-
if err != nil {
479-
return false, err
474+
return err
480475
}
481476

482-
if !hasLock {
483-
r.Recorder.Event(cluster, corev1.EventTypeNormal, "LockAcquisitionFailed", fmt.Sprintf("Lock required before %s", action))
484-
}
485-
return hasLock, nil
477+
return lockClient.TakeLock()
486478
}
487479

488480
// releaseLock attempts to release a lock.

controllers/exclude_processes.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,7 @@ func (e excludeProcesses) reconcile(ctx context.Context, r *FoundationDBClusterR
7373

7474
// Make sure the exclusions are coordinated across multiple operator instances.
7575
if cluster.ShouldUseLocks() {
76-
lockClient, err := r.getLockClient(logger, cluster)
77-
if err != nil {
78-
return &requeue{curError: err}
79-
}
80-
81-
_, err = lockClient.TakeLock()
76+
err = r.takeLock(logger, cluster, "exclude processes")
8277
if err != nil {
8378
return &requeue{curError: err, delayedRequeue: true}
8479
}

controllers/maintenance_mode_checker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ func (c maintenanceModeChecker) reconcile(_ context.Context, r *FoundationDBClus
9090
}
9191

9292
// Make sure we take a lock before we continue.
93-
hasLock, err := r.takeLock(logger, cluster, "maintenance mode check")
94-
if !hasLock {
93+
err = r.takeLock(logger, cluster, "maintenance mode check")
94+
if err != nil {
9595
return &requeue{curError: err}
9696
}
9797

controllers/remove_process_groups.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,7 @@ func includeProcessGroup(ctx context.Context, logger logr.Logger, r *FoundationD
278278

279279
// Make sure the inclusion are coordinated across multiple operator instances.
280280
if cluster.ShouldUseLocks() {
281-
lockClient, err := r.getLockClient(logger, cluster)
282-
if err != nil {
283-
return err
284-
}
285-
286-
_, err = lockClient.TakeLock()
281+
err = r.takeLock(logger, cluster, "remove process groups")
287282
if err != nil {
288283
return err
289284
}

controllers/update_database_configuration.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ func (u updateDatabaseConfiguration) reconcile(_ context.Context, r *FoundationD
9595
}
9696

9797
if !initialConfig {
98-
hasLock, err := r.takeLock(logger, cluster,
98+
err := r.takeLock(logger, cluster,
9999
fmt.Sprintf("reconfiguring the database to `%s`", configurationString))
100-
if !hasLock {
100+
if err != nil {
101101
return &requeue{curError: err, delayedRequeue: true}
102102
}
103103
}

controllers/update_pods.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ func deletePodsForUpdates(ctx context.Context, r *FoundationDBClusterReconciler,
370370
// Only lock the cluster if we are not running in the delete "All" mode.
371371
// Otherwise, we want to delete all Pods and don't require a lock to sync with other clusters.
372372
if deletionMode != fdbv1beta2.PodUpdateModeAll {
373-
_, err := r.takeLock(logger, cluster, "updating pods")
373+
err := r.takeLock(logger, cluster, "updating pods")
374374
if err != nil {
375375
return &requeue{curError: err}
376376
}

fdbclient/lock_client.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,24 @@ func (client *realLockClient) Disabled() bool {
5555
}
5656

5757
// TakeLock attempts to acquire a lock.
58-
func (client *realLockClient) TakeLock() (bool, error) {
58+
func (client *realLockClient) TakeLock() error {
5959
if client.disableLocks {
60-
return true, nil
60+
return nil
6161
}
6262

63-
hasLock, err := client.database.Transact(func(transaction fdb.Transaction) (interface{}, error) {
64-
return client.takeLockInTransaction(transaction)
63+
_, err := client.database.Transact(func(transaction fdb.Transaction) (interface{}, error) {
64+
lockErr := client.takeLockInTransaction(transaction)
65+
return nil, lockErr
6566
})
6667

67-
if hasLock == nil {
68-
return false, err
69-
}
70-
71-
return hasLock.(bool), err
68+
return err
7269
}
7370

7471
// takeLockInTransaction attempts to acquire a lock using an open transaction.
75-
func (client *realLockClient) takeLockInTransaction(transaction fdb.Transaction) (bool, error) {
72+
func (client *realLockClient) takeLockInTransaction(transaction fdb.Transaction) error {
7673
err := transaction.Options().SetAccessSystemKeys()
7774
if err != nil {
78-
return false, err
75+
return err
7976
}
8077

8178
lockKey := fdb.Key(fmt.Sprintf("%s/global", client.cluster.GetLockPrefix()))
@@ -84,31 +81,31 @@ func (client *realLockClient) takeLockInTransaction(transaction fdb.Transaction)
8481
if len(lockValue) == 0 {
8582
client.log.Info("Setting initial lock")
8683
client.updateLock(transaction, 0)
87-
return true, nil
84+
return nil
8885
}
8986

9087
lockTuple, err := tuple.Unpack(lockValue)
9188
if err != nil {
92-
return false, err
89+
return err
9390
}
9491

9592
if len(lockTuple) < 3 {
96-
return false, invalidLockValue{key: lockKey, value: lockValue}
93+
return invalidLockValue{key: lockKey, value: lockValue}
9794
}
9895

9996
currentLockOwnerID, valid := lockTuple[0].(string)
10097
if !valid {
101-
return false, invalidLockValue{key: lockKey, value: lockValue}
98+
return invalidLockValue{key: lockKey, value: lockValue}
10299
}
103100

104101
currentLockStartTime, valid := lockTuple[1].(int64)
105102
if !valid {
106-
return false, invalidLockValue{key: lockKey, value: lockValue}
103+
return invalidLockValue{key: lockKey, value: lockValue}
107104
}
108105

109106
currentLockEndTime, valid := lockTuple[2].(int64)
110107
if !valid {
111-
return false, invalidLockValue{key: lockKey, value: lockValue}
108+
return invalidLockValue{key: lockKey, value: lockValue}
112109
}
113110

114111
// ownerID represents the current cluster ID. If a lock is present the currentLockOwnerID represents the operator
@@ -123,7 +120,7 @@ func (client *realLockClient) takeLockInTransaction(transaction fdb.Transaction)
123120
newOwnerDenied := transaction.Get(client.getDenyListKey(ownerID)).MustGet() != nil
124121
if newOwnerDenied {
125122
logger.Info("Failed to get lock due to deny list")
126-
return false, nil
123+
return fmt.Errorf("failed to get lock due to deny list, owner ID: %s is on deny list", ownerID)
127124
}
128125

129126
oldOwnerDenied := transaction.Get(client.getDenyListKey(currentLockOwnerID)).MustGet() != nil
@@ -132,18 +129,18 @@ func (client *realLockClient) takeLockInTransaction(transaction fdb.Transaction)
132129
if shouldClear {
133130
logger.Info("Clearing expired lock")
134131
client.updateLock(transaction, currentLockStartTime)
135-
return true, nil
132+
return nil
136133
}
137134

138135
if currentLockOwnerID == ownerID {
139136
logger.Info("Extending previous lock")
140137
client.updateLock(transaction, currentLockStartTime)
141-
return true, nil
138+
return nil
142139
}
143140

144141
logger.Info("Failed to get lock")
145142

146-
return false, nil
143+
return fmt.Errorf("failed to get the lock")
147144
}
148145

149146
// updateLock sets the keys to acquire a lock.

pkg/fdbadminclient/lock_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type LockClient interface {
3030
Disabled() bool
3131

3232
// TakeLock attempts to acquire a lock.
33-
TakeLock() (bool, error)
33+
TakeLock() error
3434

3535
// ReleaseLock will release the current lock. The method will only release the lock if the current
3636
// operator is the lock holder.

0 commit comments

Comments
 (0)