Skip to content

Commit

Permalink
Slight bug in robustness
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan-Helge Bergesen authored and Jan-Helge Bergesen committed Mar 18, 2016
1 parent eb8f414 commit 007b6c3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
15 changes: 8 additions & 7 deletions src/main/java/jhberges/camel/consul/leader/ConsulFacadeBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,17 @@ private static String leaderKeyInfo(final String baseUrl, final String serviceNa

private boolean renewSession(final Executor executor, final String url, final String serviceName) throws IOException {
assert sessionKey.isPresent();
String _sessionKey = sessionKey.get();
final String _sessionKey = sessionKey.get();
final String uri = String.format("%s/v1/session/renew/%s", url, _sessionKey);
logger.debug("PUT {}", uri);
final Response response = executor.execute(Request.Put(uri));
boolean renewedOk = response.returnResponse().getStatusLine().getStatusCode() == 200;

logger.debug("Session {} renewed={}", _sessionKey, renewedOk);
if (!renewedOk) {
logger.debug("Attempting to re-establish session for serviceName={}", serviceName);
destroySession(url, _sessionKey);
sessionKey = Optional.empty();
sessionKey = initSessionKey(serviceName);
renewedOk = sessionKey.isPresent();
}
Expand Down Expand Up @@ -95,8 +96,8 @@ private static Optional<String> unpackSessionKey(final HttpEntity entity) {
private int retryPeriod;
private double backOffMultiplier;

public ConsulFacadeBean(final String consulUrl, final Optional<String> username, final Optional<String> password,
int ttlInSeconds, int lockDelayInSeconds, boolean allowIslandMode, int createSessionTries, int retryPeriod, double backOffMultiplier)
public ConsulFacadeBean(final String consulUrl, final Optional<String> username, final Optional<String> password,
final int ttlInSeconds, final int lockDelayInSeconds, final boolean allowIslandMode, final int createSessionTries, final int retryPeriod, final double backOffMultiplier)
throws MalformedURLException {
this(consulUrl, username, password, Executor.newInstance());
this.ttlInSeconds = ttlInSeconds;
Expand Down Expand Up @@ -198,8 +199,8 @@ public void destroySession(final String consulUrl, final String sessionKey) {

}

public Optional<String> initSessionKey(String serviceName) {
public Optional<String> initSessionKey(final String serviceName) {

if (!sessionKey.isPresent()) {
sessionKey = createSession(
serviceName, ttlInSeconds, lockDelayInSeconds,
Expand Down Expand Up @@ -246,7 +247,7 @@ public Optional<Boolean> pollConsul(final String serviceName) {
logger.debug("PUT {}", uri);
final Response response = executor.execute(Request.Put(uri));
final Optional<Boolean> result = Optional.ofNullable(Boolean.valueOf(response.returnContent().asString()));
logger.debug("pollConsul - Result: {}", result);
logger.debug("pollConsul - session={} service={} result={}", sessionKey.get(), serviceName, result);
return result;
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected ConsulLeaderElector(
this.camelContext = camelContext;
this.producerTemplate = producerTemplate;
this.allowIslandMode = allowIslandMode;
Optional<String> sessionKey = consulFacade.initSessionKey(serviceName);
final Optional<String> sessionKey = consulFacade.initSessionKey(serviceName);
if (!sessionKey.isPresent() && !allowIslandMode) {
logger.error("Island mode disabled -- terminating abruptly!");
TERMINATION_CALLBACK.run();
Expand All @@ -53,15 +53,15 @@ public void onContextStop(final CamelContext context) {
super.onContextStop(context);
try {
consulFacade.close();
} catch (IOException e) {
} catch (final IOException e) {
logger.debug("Exception while closing facade: {}", e.getMessage());
}
}

@Override
public void run() {
final Optional<Boolean> isLeader = consulFacade.pollConsul(serviceName);
logger.debug("Poll result isLeader={} allowIslandMode={}", isLeader, allowIslandMode);
logger.debug("Poll result serviceName={} isLeader={} allowIslandMode={}", serviceName, isLeader, allowIslandMode);
try {
if (isLeader.orElse(allowIslandMode)) { // I.e if explicitly leader, or poll
// failed.
Expand Down

0 comments on commit 007b6c3

Please sign in to comment.