Skip to content

Commit

Permalink
removing the usage of a lockexception
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins authored and manusa committed Feb 28, 2023
1 parent f392a31 commit 0b1c842
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.extended.leaderelection.resourcelock.LeaderElectionRecord;
import io.fabric8.kubernetes.client.extended.leaderelection.resourcelock.Lock;
import io.fabric8.kubernetes.client.extended.leaderelection.resourcelock.LockException;
import io.fabric8.kubernetes.client.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -144,7 +143,7 @@ private void release(LeaderElectionRecord current) {
newLeaderElectionRecord.setVersion(current.getVersion());

leaderElectionConfig.getLock().update(kubernetesClient, newLeaderElectionRecord);
} catch (LockException | KubernetesClientException e) {
} catch (KubernetesClientException e) {
final String lockDescription = leaderElectionConfig.getLock().describe();
LOGGER.error("Exception occurred while releasing lock '{}'", lockDescription, e);
}
Expand All @@ -159,7 +158,7 @@ private CompletableFuture<Void> acquire() {
completion.complete(null);
}
LOGGER.debug("Failed to acquire lease '{}' retrying...", lockDescription);
} catch (LockException | KubernetesClientException exception) {
} catch (KubernetesClientException exception) {
LOGGER.error("Exception occurred while acquiring lock '{}'", lockDescription, exception);
}
}, () -> jitter(leaderElectionConfig.getRetryPeriod(), JITTER_FACTOR).toMillis(), executor);
Expand All @@ -183,13 +182,13 @@ private CompletableFuture<Void> renewWithTimeout() {
// renewal failed, exit
completion.complete(null);
}
} catch (LockException | KubernetesClientException exception) {
} catch (KubernetesClientException exception) {
LOGGER.debug("Exception occurred while renewing lock: {}", exception.getMessage(), exception);
}
}, () -> leaderElectionConfig.getRetryPeriod().toMillis(), executor);
}

synchronized boolean tryAcquireOrRenew() throws LockException {
synchronized boolean tryAcquireOrRenew() {
if (stopped) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
*/
package io.fabric8.kubernetes.client.extended.leaderelection.resourcelock;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.utils.Serialization;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -36,17 +34,17 @@ public class ConfigMapLock implements Lock {
private final String configMapNamespace;
private final String configMapName;
private final String identity;
private final ObjectMapper objectMapper;

public ConfigMapLock(String configMapNamespace, String configMapName, String identity) {
this.configMapNamespace = Objects.requireNonNull(configMapNamespace, "configMapNamespace is required");
this.configMapName = Objects.requireNonNull(configMapName, "configMapName is required");
this.identity = Objects.requireNonNull(identity, "identity is required");
objectMapper = Serialization.jsonMapper();
}

/**
* {@inheritDoc}
*
* @throws LockException
*/
@Override
public LeaderElectionRecord get(KubernetesClient client) {
Expand All @@ -58,9 +56,8 @@ public LeaderElectionRecord get(KubernetesClient client) {
.map(annotations -> annotations.get(LEADER_ELECTION_RECORD_ANNOTATION_KEY))
.map(annotation -> {
try {
return objectMapper.readValue(annotation, new TypeReference<LeaderElectionRecord>() {
});
} catch (JsonProcessingException ex) {
return Serialization.unmarshal(annotation, LeaderElectionRecord.class);
} catch (KubernetesClientException ex) {
LOGGER.error("Error deserializing LeaderElectionRecord from ConfigMap", ex);
return null;
}
Expand All @@ -77,37 +74,29 @@ public LeaderElectionRecord get(KubernetesClient client) {
*/
@Override
public void create(
KubernetesClient client, LeaderElectionRecord leaderElectionRecord) throws LockException {
KubernetesClient client, LeaderElectionRecord leaderElectionRecord) {

try {
client.configMaps().inNamespace(configMapNamespace).withName(configMapName).create(new ConfigMapBuilder()
.editOrNewMetadata().withNamespace(configMapNamespace).withName(configMapName)
.addToAnnotations(LEADER_ELECTION_RECORD_ANNOTATION_KEY, objectMapper.writeValueAsString(leaderElectionRecord))
.endMetadata()
.build());
} catch (Exception e) {
throw new LockException("Unable to create ConfigMapLock", e);
}
client.configMaps().inNamespace(configMapNamespace).withName(configMapName).create(new ConfigMapBuilder()
.editOrNewMetadata().withNamespace(configMapNamespace).withName(configMapName)
.addToAnnotations(LEADER_ELECTION_RECORD_ANNOTATION_KEY, Serialization.asJson(leaderElectionRecord))
.endMetadata()
.build());
}

/**
* {@inheritDoc}
*/
@Override
public void update(
KubernetesClient client, LeaderElectionRecord leaderElectionRecord) throws LockException {
KubernetesClient client, LeaderElectionRecord leaderElectionRecord) {

try {
final ConfigMap toReplace = client.configMaps().inNamespace(configMapNamespace).withName(configMapName).get();
toReplace.getMetadata().getAnnotations()
.put(LEADER_ELECTION_RECORD_ANNOTATION_KEY, objectMapper.writeValueAsString(leaderElectionRecord));
// Use replace instead of edit to avoid concurrent modifications, resourceVersion is locked to original record version
client.configMaps().inNamespace(configMapNamespace).withName(configMapName)
.lockResourceVersion((String) Objects.requireNonNull(leaderElectionRecord.getVersion()))
.replace(toReplace);
} catch (Exception e) {
throw new LockException("Unable to update ConfigMapLock", e);
}
final ConfigMap toReplace = client.configMaps().inNamespace(configMapNamespace).withName(configMapName).get();
toReplace.getMetadata().getAnnotations()
.put(LEADER_ELECTION_RECORD_ANNOTATION_KEY, Serialization.asJson(leaderElectionRecord));
// Use replace instead of edit to avoid concurrent modifications, resourceVersion is locked to original record version
client.configMaps().inNamespace(configMapNamespace).withName(configMapName)
.lockResourceVersion((String) Objects.requireNonNull(leaderElectionRecord.getVersion()))
.replace(toReplace);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,45 +62,37 @@ public LeaderElectionRecord get(KubernetesClient client) {
*/
@Override
public void create(
KubernetesClient client, LeaderElectionRecord leaderElectionRecord) throws LockException {
KubernetesClient client, LeaderElectionRecord leaderElectionRecord) {

try {
client.leases().inNamespace(leaseNamespace).withName(leaseName).create(new LeaseBuilder()
.withNewMetadata().withNamespace(leaseNamespace).withName(leaseName).endMetadata()
.withNewSpec()
.withHolderIdentity(leaderElectionRecord.getHolderIdentity())
.withLeaseDurationSeconds((int) leaderElectionRecord.getLeaseDuration().get(ChronoUnit.SECONDS))
.withAcquireTime(leaderElectionRecord.getAcquireTime())
.withRenewTime(leaderElectionRecord.getRenewTime())
.withLeaseTransitions(leaderElectionRecord.getLeaderTransitions())
.endSpec()
.build());
} catch (Exception e) {
throw new LockException("Unable to create LeaseLock", e);
}
client.leases().inNamespace(leaseNamespace).withName(leaseName).create(new LeaseBuilder()
.withNewMetadata().withNamespace(leaseNamespace).withName(leaseName).endMetadata()
.withNewSpec()
.withHolderIdentity(leaderElectionRecord.getHolderIdentity())
.withLeaseDurationSeconds((int) leaderElectionRecord.getLeaseDuration().get(ChronoUnit.SECONDS))
.withAcquireTime(leaderElectionRecord.getAcquireTime())
.withRenewTime(leaderElectionRecord.getRenewTime())
.withLeaseTransitions(leaderElectionRecord.getLeaderTransitions())
.endSpec()
.build());
}

/**
* {@inheritDoc}
*/
@Override
public void update(
KubernetesClient client, LeaderElectionRecord leaderElectionRecord) throws LockException {
KubernetesClient client, LeaderElectionRecord leaderElectionRecord) {

try {
final Lease toReplace = client.leases().inNamespace(leaseNamespace).withName(leaseName).get();
toReplace.getSpec().setHolderIdentity(leaderElectionRecord.getHolderIdentity());
toReplace.getSpec().setLeaseDurationSeconds((int) leaderElectionRecord.getLeaseDuration().get(ChronoUnit.SECONDS));
toReplace.getSpec().setAcquireTime(leaderElectionRecord.getAcquireTime());
toReplace.getSpec().setRenewTime(leaderElectionRecord.getRenewTime());
toReplace.getSpec().setLeaseTransitions(leaderElectionRecord.getLeaderTransitions());
// Use replace instead of edit to avoid concurrent modifications, resourceVersion is locked to original record version
client.leases().inNamespace(leaseNamespace).withName(leaseName)
.lockResourceVersion((String) Objects.requireNonNull(leaderElectionRecord.getVersion()))
.replace(toReplace);
} catch (Exception e) {
throw new LockException("Unable to update LeaseLock", e);
}
final Lease toReplace = client.leases().inNamespace(leaseNamespace).withName(leaseName).get();
toReplace.getSpec().setHolderIdentity(leaderElectionRecord.getHolderIdentity());
toReplace.getSpec().setLeaseDurationSeconds((int) leaderElectionRecord.getLeaseDuration().get(ChronoUnit.SECONDS));
toReplace.getSpec().setAcquireTime(leaderElectionRecord.getAcquireTime());
toReplace.getSpec().setRenewTime(leaderElectionRecord.getRenewTime());
toReplace.getSpec().setLeaseTransitions(leaderElectionRecord.getLeaderTransitions());
// Use replace instead of edit to avoid concurrent modifications, resourceVersion is locked to original record version
client.leases().inNamespace(leaseNamespace).withName(leaseName)
.lockResourceVersion((String) Objects.requireNonNull(leaderElectionRecord.getVersion()))
.replace(toReplace);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,18 @@ public interface Lock {
*
* @param client used to retrieve the LeaderElectionRecord
* @param leaderElectionRecord to update
* @throws LockException if update was not possible
*/
void create(
KubernetesClient client, LeaderElectionRecord leaderElectionRecord) throws LockException;
KubernetesClient client, LeaderElectionRecord leaderElectionRecord);

/**
* Attempts to update the current {@link LeaderElectionRecord}.
*
* @param client used to retrieve the LeaderElectionRecord
* @param leaderElectionRecord to update
* @throws LockException if update was not possible
*/
void update(
KubernetesClient client, LeaderElectionRecord leaderElectionRecord) throws LockException;
KubernetesClient client, LeaderElectionRecord leaderElectionRecord);

/**
* Returns the unique id of the lock holder.
Expand Down

This file was deleted.

0 comments on commit 0b1c842

Please sign in to comment.