Skip to content

Commit 0d11d02

Browse files
shawkinsmanusa
authored andcommitted
removing the usage of a lockexception
1 parent f392a31 commit 0d11d02

File tree

5 files changed

+47
-95
lines changed

5 files changed

+47
-95
lines changed

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extended/leaderelection/LeaderElector.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import io.fabric8.kubernetes.client.KubernetesClientException;
2020
import io.fabric8.kubernetes.client.extended.leaderelection.resourcelock.LeaderElectionRecord;
2121
import io.fabric8.kubernetes.client.extended.leaderelection.resourcelock.Lock;
22-
import io.fabric8.kubernetes.client.extended.leaderelection.resourcelock.LockException;
2322
import io.fabric8.kubernetes.client.utils.Utils;
2423
import org.slf4j.Logger;
2524
import org.slf4j.LoggerFactory;
@@ -144,7 +143,7 @@ private void release(LeaderElectionRecord current) {
144143
newLeaderElectionRecord.setVersion(current.getVersion());
145144

146145
leaderElectionConfig.getLock().update(kubernetesClient, newLeaderElectionRecord);
147-
} catch (LockException | KubernetesClientException e) {
146+
} catch (KubernetesClientException e) {
148147
final String lockDescription = leaderElectionConfig.getLock().describe();
149148
LOGGER.error("Exception occurred while releasing lock '{}'", lockDescription, e);
150149
}
@@ -159,7 +158,7 @@ private CompletableFuture<Void> acquire() {
159158
completion.complete(null);
160159
}
161160
LOGGER.debug("Failed to acquire lease '{}' retrying...", lockDescription);
162-
} catch (LockException | KubernetesClientException exception) {
161+
} catch (KubernetesClientException exception) {
163162
LOGGER.error("Exception occurred while acquiring lock '{}'", lockDescription, exception);
164163
}
165164
}, () -> jitter(leaderElectionConfig.getRetryPeriod(), JITTER_FACTOR).toMillis(), executor);
@@ -183,13 +182,13 @@ private CompletableFuture<Void> renewWithTimeout() {
183182
// renewal failed, exit
184183
completion.complete(null);
185184
}
186-
} catch (LockException | KubernetesClientException exception) {
185+
} catch (KubernetesClientException exception) {
187186
LOGGER.debug("Exception occurred while renewing lock: {}", exception.getMessage(), exception);
188187
}
189188
}, () -> leaderElectionConfig.getRetryPeriod().toMillis(), executor);
190189
}
191190

192-
synchronized boolean tryAcquireOrRenew() throws LockException {
191+
synchronized boolean tryAcquireOrRenew() {
193192
if (stopped) {
194193
return false;
195194
}

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extended/leaderelection/resourcelock/ConfigMapLock.java

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
*/
1616
package io.fabric8.kubernetes.client.extended.leaderelection.resourcelock;
1717

18-
import com.fasterxml.jackson.core.JsonProcessingException;
19-
import com.fasterxml.jackson.core.type.TypeReference;
20-
import com.fasterxml.jackson.databind.ObjectMapper;
2118
import io.fabric8.kubernetes.api.model.ConfigMap;
2219
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
2320
import io.fabric8.kubernetes.api.model.ObjectMeta;
2421
import io.fabric8.kubernetes.client.KubernetesClient;
22+
import io.fabric8.kubernetes.client.KubernetesClientException;
2523
import io.fabric8.kubernetes.client.utils.Serialization;
2624
import org.slf4j.Logger;
2725
import org.slf4j.LoggerFactory;
@@ -36,17 +34,17 @@ public class ConfigMapLock implements Lock {
3634
private final String configMapNamespace;
3735
private final String configMapName;
3836
private final String identity;
39-
private final ObjectMapper objectMapper;
4037

4138
public ConfigMapLock(String configMapNamespace, String configMapName, String identity) {
4239
this.configMapNamespace = Objects.requireNonNull(configMapNamespace, "configMapNamespace is required");
4340
this.configMapName = Objects.requireNonNull(configMapName, "configMapName is required");
4441
this.identity = Objects.requireNonNull(identity, "identity is required");
45-
objectMapper = Serialization.jsonMapper();
4642
}
4743

4844
/**
4945
* {@inheritDoc}
46+
*
47+
* @throws LockException
5048
*/
5149
@Override
5250
public LeaderElectionRecord get(KubernetesClient client) {
@@ -58,9 +56,8 @@ public LeaderElectionRecord get(KubernetesClient client) {
5856
.map(annotations -> annotations.get(LEADER_ELECTION_RECORD_ANNOTATION_KEY))
5957
.map(annotation -> {
6058
try {
61-
return objectMapper.readValue(annotation, new TypeReference<LeaderElectionRecord>() {
62-
});
63-
} catch (JsonProcessingException ex) {
59+
return Serialization.unmarshal(annotation, LeaderElectionRecord.class);
60+
} catch (KubernetesClientException ex) {
6461
LOGGER.error("Error deserializing LeaderElectionRecord from ConfigMap", ex);
6562
return null;
6663
}
@@ -77,37 +74,29 @@ public LeaderElectionRecord get(KubernetesClient client) {
7774
*/
7875
@Override
7976
public void create(
80-
KubernetesClient client, LeaderElectionRecord leaderElectionRecord) throws LockException {
77+
KubernetesClient client, LeaderElectionRecord leaderElectionRecord) {
8178

82-
try {
83-
client.configMaps().inNamespace(configMapNamespace).withName(configMapName).create(new ConfigMapBuilder()
84-
.editOrNewMetadata().withNamespace(configMapNamespace).withName(configMapName)
85-
.addToAnnotations(LEADER_ELECTION_RECORD_ANNOTATION_KEY, objectMapper.writeValueAsString(leaderElectionRecord))
86-
.endMetadata()
87-
.build());
88-
} catch (Exception e) {
89-
throw new LockException("Unable to create ConfigMapLock", e);
90-
}
79+
client.configMaps().inNamespace(configMapNamespace).withName(configMapName).create(new ConfigMapBuilder()
80+
.editOrNewMetadata().withNamespace(configMapNamespace).withName(configMapName)
81+
.addToAnnotations(LEADER_ELECTION_RECORD_ANNOTATION_KEY, Serialization.asJson(leaderElectionRecord))
82+
.endMetadata()
83+
.build());
9184
}
9285

9386
/**
9487
* {@inheritDoc}
9588
*/
9689
@Override
9790
public void update(
98-
KubernetesClient client, LeaderElectionRecord leaderElectionRecord) throws LockException {
91+
KubernetesClient client, LeaderElectionRecord leaderElectionRecord) {
9992

100-
try {
101-
final ConfigMap toReplace = client.configMaps().inNamespace(configMapNamespace).withName(configMapName).get();
102-
toReplace.getMetadata().getAnnotations()
103-
.put(LEADER_ELECTION_RECORD_ANNOTATION_KEY, objectMapper.writeValueAsString(leaderElectionRecord));
104-
// Use replace instead of edit to avoid concurrent modifications, resourceVersion is locked to original record version
105-
client.configMaps().inNamespace(configMapNamespace).withName(configMapName)
106-
.lockResourceVersion((String) Objects.requireNonNull(leaderElectionRecord.getVersion()))
107-
.replace(toReplace);
108-
} catch (Exception e) {
109-
throw new LockException("Unable to update ConfigMapLock", e);
110-
}
93+
final ConfigMap toReplace = client.configMaps().inNamespace(configMapNamespace).withName(configMapName).get();
94+
toReplace.getMetadata().getAnnotations()
95+
.put(LEADER_ELECTION_RECORD_ANNOTATION_KEY, Serialization.asJson(leaderElectionRecord));
96+
// Use replace instead of edit to avoid concurrent modifications, resourceVersion is locked to original record version
97+
client.configMaps().inNamespace(configMapNamespace).withName(configMapName)
98+
.lockResourceVersion((String) Objects.requireNonNull(leaderElectionRecord.getVersion()))
99+
.replace(toReplace);
111100
}
112101

113102
/**

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extended/leaderelection/resourcelock/LeaseLock.java

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -62,45 +62,37 @@ public LeaderElectionRecord get(KubernetesClient client) {
6262
*/
6363
@Override
6464
public void create(
65-
KubernetesClient client, LeaderElectionRecord leaderElectionRecord) throws LockException {
65+
KubernetesClient client, LeaderElectionRecord leaderElectionRecord) {
6666

67-
try {
68-
client.leases().inNamespace(leaseNamespace).withName(leaseName).create(new LeaseBuilder()
69-
.withNewMetadata().withNamespace(leaseNamespace).withName(leaseName).endMetadata()
70-
.withNewSpec()
71-
.withHolderIdentity(leaderElectionRecord.getHolderIdentity())
72-
.withLeaseDurationSeconds((int) leaderElectionRecord.getLeaseDuration().get(ChronoUnit.SECONDS))
73-
.withAcquireTime(leaderElectionRecord.getAcquireTime())
74-
.withRenewTime(leaderElectionRecord.getRenewTime())
75-
.withLeaseTransitions(leaderElectionRecord.getLeaderTransitions())
76-
.endSpec()
77-
.build());
78-
} catch (Exception e) {
79-
throw new LockException("Unable to create LeaseLock", e);
80-
}
67+
client.leases().inNamespace(leaseNamespace).withName(leaseName).create(new LeaseBuilder()
68+
.withNewMetadata().withNamespace(leaseNamespace).withName(leaseName).endMetadata()
69+
.withNewSpec()
70+
.withHolderIdentity(leaderElectionRecord.getHolderIdentity())
71+
.withLeaseDurationSeconds((int) leaderElectionRecord.getLeaseDuration().get(ChronoUnit.SECONDS))
72+
.withAcquireTime(leaderElectionRecord.getAcquireTime())
73+
.withRenewTime(leaderElectionRecord.getRenewTime())
74+
.withLeaseTransitions(leaderElectionRecord.getLeaderTransitions())
75+
.endSpec()
76+
.build());
8177
}
8278

8379
/**
8480
* {@inheritDoc}
8581
*/
8682
@Override
8783
public void update(
88-
KubernetesClient client, LeaderElectionRecord leaderElectionRecord) throws LockException {
84+
KubernetesClient client, LeaderElectionRecord leaderElectionRecord) {
8985

90-
try {
91-
final Lease toReplace = client.leases().inNamespace(leaseNamespace).withName(leaseName).get();
92-
toReplace.getSpec().setHolderIdentity(leaderElectionRecord.getHolderIdentity());
93-
toReplace.getSpec().setLeaseDurationSeconds((int) leaderElectionRecord.getLeaseDuration().get(ChronoUnit.SECONDS));
94-
toReplace.getSpec().setAcquireTime(leaderElectionRecord.getAcquireTime());
95-
toReplace.getSpec().setRenewTime(leaderElectionRecord.getRenewTime());
96-
toReplace.getSpec().setLeaseTransitions(leaderElectionRecord.getLeaderTransitions());
97-
// Use replace instead of edit to avoid concurrent modifications, resourceVersion is locked to original record version
98-
client.leases().inNamespace(leaseNamespace).withName(leaseName)
99-
.lockResourceVersion((String) Objects.requireNonNull(leaderElectionRecord.getVersion()))
100-
.replace(toReplace);
101-
} catch (Exception e) {
102-
throw new LockException("Unable to update LeaseLock", e);
103-
}
86+
final Lease toReplace = client.leases().inNamespace(leaseNamespace).withName(leaseName).get();
87+
toReplace.getSpec().setHolderIdentity(leaderElectionRecord.getHolderIdentity());
88+
toReplace.getSpec().setLeaseDurationSeconds((int) leaderElectionRecord.getLeaseDuration().get(ChronoUnit.SECONDS));
89+
toReplace.getSpec().setAcquireTime(leaderElectionRecord.getAcquireTime());
90+
toReplace.getSpec().setRenewTime(leaderElectionRecord.getRenewTime());
91+
toReplace.getSpec().setLeaseTransitions(leaderElectionRecord.getLeaderTransitions());
92+
// Use replace instead of edit to avoid concurrent modifications, resourceVersion is locked to original record version
93+
client.leases().inNamespace(leaseNamespace).withName(leaseName)
94+
.lockResourceVersion((String) Objects.requireNonNull(leaderElectionRecord.getVersion()))
95+
.replace(toReplace);
10496
}
10597

10698
/**

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extended/leaderelection/resourcelock/Lock.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,18 @@ public interface Lock {
3434
*
3535
* @param client used to retrieve the LeaderElectionRecord
3636
* @param leaderElectionRecord to update
37-
* @throws LockException if update was not possible
3837
*/
3938
void create(
40-
KubernetesClient client, LeaderElectionRecord leaderElectionRecord) throws LockException;
39+
KubernetesClient client, LeaderElectionRecord leaderElectionRecord);
4140

4241
/**
4342
* Attempts to update the current {@link LeaderElectionRecord}.
4443
*
4544
* @param client used to retrieve the LeaderElectionRecord
4645
* @param leaderElectionRecord to update
47-
* @throws LockException if update was not possible
4846
*/
4947
void update(
50-
KubernetesClient client, LeaderElectionRecord leaderElectionRecord) throws LockException;
48+
KubernetesClient client, LeaderElectionRecord leaderElectionRecord);
5149

5250
/**
5351
* Returns the unique id of the lock holder.

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extended/leaderelection/resourcelock/LockException.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)