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

Commit

Permalink
Override default backoff delay for Cache (#184)
Browse files Browse the repository at this point in the history
* [Fix] KVCache does not work for exact key search

Commit 3714976 changes the way the prefix are removed from the key.
It works fine for key/subkey pattern but fails when we are looking for the
exact key (for intance, "a/b").

* Allows to override key extractor (#172)

* Add single line of documentation to example 4 (#176)

* Fix indentation and renumber examples

In example 4 there was an extra level of indentation, and there was no example 5, but examples 6, and 7, 8. I fixed the indentation and the numbering gap.

* add slight documentation to example 4

I added a single line of documentation to example 4 as per #174.

* Revert "Fix indentation and renumber examples"

* Fix indentation and renumber examples (#175)

In example 4 there was an extra level of indentation, and there was no example 5, but examples 6, and 7, 8. I fixed the indentation and the numbering gap.

* allow 404s on kv grabs

* fixing KV tests for 404

* 0.13.2

* Add support for Check-And-Set option to DELETE operation (#178)

* Add eclipse project files to .gitignore

* Add support for Check-And-Set option to DELETE operation.

* transaction support (#181)

* operator api support (#182)

* Override default backoff delay for Cache

Default backoff delay for caches is equal to 10s.
Enable this value to be overriden from properties if such a property exists.
For instance, "-Dcom.orbitz.consul.cache.backOffDelay=500".

I chose to use properties as the code of caches starts to grow fast, some
caches already have 4 constructors and i think it could be better to avoid
the creation of another one.
  • Loading branch information
yfouquet authored and rickfast committed Nov 4, 2016
1 parent 01290b9 commit 1b4e3a2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
47 changes: 35 additions & 12 deletions src/main/java/com/orbitz/consul/cache/ConsulCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.orbitz.consul.ConsulException;
import com.orbitz.consul.async.ConsulResponseCallback;
Expand All @@ -12,8 +13,17 @@
import org.slf4j.LoggerFactory;

import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.*;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import static com.google.common.base.Preconditions.checkArgument;
Expand All @@ -31,6 +41,10 @@ enum State {latent, starting, started, stopped }

private final static Logger LOGGER = LoggerFactory.getLogger(ConsulCache.class);

@VisibleForTesting
static final String BACKOFF_DELAY_PROPERTY = "com.orbitz.consul.cache.backOffDelay";
private static final long BACKOFF_DELAY_QTY_IN_MS = getBackOffDelayInMs(System.getProperties());

private final AtomicReference<BigInteger> latestIndex = new AtomicReference<BigInteger>(null);
private final AtomicReference<ImmutableMap<K, V>> lastResponse = new AtomicReference<ImmutableMap<K, V>>(ImmutableMap.<K, V>of());
private final AtomicReference<State> state = new AtomicReference<State>(State.latent);
Expand All @@ -45,14 +59,6 @@ enum State {latent, starting, started, stopped }
ConsulCache(
Function<V, K> keyConversion,
CallbackConsumer<V> callbackConsumer) {
this(keyConversion, callbackConsumer, 10, TimeUnit.SECONDS);
}

ConsulCache(
Function<V, K> keyConversion,
CallbackConsumer<V> callbackConsumer,
final long backoffDelayQty,
final TimeUnit backoffDelayUnit) {

this.keyConversion = keyConversion;
this.callBackConsumer = callbackConsumer;
Expand Down Expand Up @@ -95,18 +101,35 @@ public void onFailure(Throwable throwable) {
if (!isRunning()) {
return;
}
LOGGER.error(String.format("Error getting response from consul. will retry in %d %s", backoffDelayQty, backoffDelayUnit), throwable);
LOGGER.error(String.format("Error getting response from consul. will retry in %d %s", BACKOFF_DELAY_QTY_IN_MS, TimeUnit.MILLISECONDS), throwable);

executorService.schedule(new Runnable() {
@Override
public void run() {
runCallback();
}
}, backoffDelayQty, backoffDelayUnit);
}, BACKOFF_DELAY_QTY_IN_MS, TimeUnit.MILLISECONDS);
}
};
}

@VisibleForTesting
static long getBackOffDelayInMs(Properties properties) {
String backOffDelay = null;
try {
backOffDelay = properties.getProperty(BACKOFF_DELAY_PROPERTY);
if (!Strings.isNullOrEmpty(backOffDelay)) {
return Long.parseLong(backOffDelay);
}
} catch (Exception ex) {
LOGGER.warn(backOffDelay != null ?
String.format("Error parsing property variable %s: %s", BACKOFF_DELAY_PROPERTY, backOffDelay) :
String.format("Error extracting property variable %s", BACKOFF_DELAY_PROPERTY),
ex);
}
return TimeUnit.SECONDS.toMillis(10);
}

public void start() throws Exception {
checkState(state.compareAndSet(State.latent, State.starting),"Cannot transition from state %s to %s", state.get(), State.starting);
runCallback();
Expand Down
29 changes: 23 additions & 6 deletions src/test/java/com/orbitz/consul/cache/ConsulCacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@
import com.orbitz.consul.option.ConsistencyMode;
import com.orbitz.consul.option.ImmutableQueryOptions;
import com.orbitz.consul.option.QueryOptions;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -260,7 +257,7 @@ public void testLifeCycleDoubleStart() throws Exception {
nc.start();
assertEquals(ConsulCache.State.starting, nc.getState());

if (!nc.awaitInitialized(1, TimeUnit.SECONDS)) {
if (!nc.awaitInitialized(10, TimeUnit.SECONDS)) {
fail("cache initialization failed");
}
assertEquals(ConsulCache.State.started, nc.getState());
Expand Down Expand Up @@ -378,4 +375,24 @@ public void testWatchParamsWithAdditionalIndexAndWaitingThrows() {
.build();
ConsulCache.watchParams(index, 10, additionalOptions);
}

@Test
public void testDefaultBackOffDelay() {
Properties properties = new Properties();
Assert.assertEquals(10000L, ConsulCache.getBackOffDelayInMs(properties));
}

@Test
public void testBackOffDelayFromProperties() {
Properties properties = new Properties();
properties.setProperty(ConsulCache.BACKOFF_DELAY_PROPERTY, "500");
Assert.assertEquals(500L, ConsulCache.getBackOffDelayInMs(properties));
}

@Test
public void testBackOffDelayDoesNotThrow() {
Properties properties = new Properties();
properties.setProperty(ConsulCache.BACKOFF_DELAY_PROPERTY, "unparseableLong");
Assert.assertEquals(10000L, ConsulCache.getBackOffDelayInMs(properties));
}
}

0 comments on commit 1b4e3a2

Please sign in to comment.