Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Dont require knownleader #456

Merged
merged 2 commits into from
Dec 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 47 additions & 53 deletions src/main/java/com/orbitz/consul/cache/ConsulCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,68 +117,62 @@ protected ConsulCache(
this.responseCallback = new ConsulResponseCallback<List<V>>() {
@Override
public void onComplete(ConsulResponse<List<V>> consulResponse) {
if (!isRunning()) {
return;
}
long elapsedTime = stopWatch.elapsed(TimeUnit.MILLISECONDS);
updateIndex(consulResponse);
LOGGER.debug("Consul cache updated for {} (index={}), request duration: {} ms",
cacheDescriptor, latestIndex, elapsedTime);

ImmutableMap<K, V> full = convertToMap(consulResponse);

boolean changed = !full.equals(lastResponse.get());
eventHandler.cachePollingSuccess(cacheDescriptor, changed, elapsedTime);

if (changed) {
// changes
lastResponse.set(full);
// metadata changes
lastContact.set(consulResponse.getLastContact());
isKnownLeader.set(consulResponse.isKnownLeader());
}

if (consulResponse.isKnownLeader()) {
if (!isRunning()) {
return;
}
long elapsedTime = stopWatch.elapsed(TimeUnit.MILLISECONDS);
updateIndex(consulResponse);
LOGGER.debug("Consul cache updated for {} (index={}), request duration: {} ms",
cacheDescriptor, latestIndex, elapsedTime);

ImmutableMap<K, V> full = convertToMap(consulResponse);

boolean changed = !full.equals(lastResponse.get());
eventHandler.cachePollingSuccess(cacheDescriptor, changed, elapsedTime);

if (changed) {
// changes
lastResponse.set(full);
// metadata changes
lastContact.set(consulResponse.getLastContact());
isKnownLeader.set(consulResponse.isKnownLeader());
if (changed) {
Boolean locked = false;
if (state.get() == State.starting) {
listenersStartingLock.lock();
locked = true;
}

if (changed) {
Boolean locked = false;
if (state.get() == State.starting) {
listenersStartingLock.lock();
locked = true;
}
try {
for (Listener<K, V> l : listeners) {
try {
l.notify(full);
} catch (RuntimeException e) {
LOGGER.warn("ConsulCache Listener's notify method threw an exception.", e);
}
}
}
finally {
if (locked) {
listenersStartingLock.unlock();
try {
for (Listener<K, V> l : listeners) {
try {
l.notify(full);
} catch (RuntimeException e) {
LOGGER.warn("ConsulCache Listener's notify method threw an exception.", e);
}
}
}

if (state.compareAndSet(State.starting, State.started)) {
initLatch.countDown();
}

Duration timeToWait = cacheConfig.getMinimumDurationBetweenRequests();
if ((consulResponse.getResponse() == null || consulResponse.getResponse().isEmpty()) &&
cacheConfig.getMinimumDurationDelayOnEmptyResult().compareTo(timeToWait) > 0) {
timeToWait = cacheConfig.getMinimumDurationDelayOnEmptyResult();
finally {
if (locked) {
listenersStartingLock.unlock();
}
}
timeToWait = timeToWait.minusMillis(elapsedTime);
}

scheduler.schedule(ConsulCache.this::runCallback,
timeToWait.toMillis(), TimeUnit.MILLISECONDS);
if (state.compareAndSet(State.starting, State.started)) {
initLatch.countDown();
}

} else {
onFailure(new ConsulException("Consul cluster has no elected leader"));
Duration timeToWait = cacheConfig.getMinimumDurationBetweenRequests();
if ((consulResponse.getResponse() == null || consulResponse.getResponse().isEmpty()) &&
cacheConfig.getMinimumDurationDelayOnEmptyResult().compareTo(timeToWait) > 0) {
timeToWait = cacheConfig.getMinimumDurationDelayOnEmptyResult();
}
timeToWait = timeToWait.minusMillis(elapsedTime);

scheduler.schedule(ConsulCache.this::runCallback,
timeToWait.toMillis(), TimeUnit.MILLISECONDS);
}

@Override
Expand Down