Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

etcd integration #1

Merged
merged 1 commit into from
Mar 9, 2019
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
services:
- docker
before_cache:
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
- rm -fr $HOME/.gradle/caches/*/plugin-resolution/
Expand Down
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ Baigan configuration is an easy to use framework for Java applications allowing

Its simple but extensible interfaces allow for powerful integrations.

* fetch your configuration from arbitrary sources serving any format (e.g. local files, S3, ...)
* fetch your configuration from arbitrary sources serving any format (e.g. local files, S3, etcd ...)
* integrate with the [Spring Framework](https://spring.io/) enabling access to your configuration by simply annotating an interface.
* integrate with Baigan configuration in seconds, via the provided [Spring Boot](https://spring.io/projects/spring-boot) Auto-configuration library

## Usage

The following example makes use of the [File](file), [S3](s3) and [Spring Boot](spring-boot-autoconfigure) module.
The following example makes use of the [File](file), [S3](s3), [etcd](etcd) and [Spring Boot](spring-boot-autoconfigure) module.

Usage of Baigan configuration is as easy as:

Expand Down Expand Up @@ -56,12 +56,16 @@ class StoreConfiguration {
.cached(ofMinutes(2))
.onLocalFile(Path.of("example.yaml"))
.asYaml(),
"OtherConfiguration", chain(
"BackendConfiguration", chain(
FileStores.builder()
.cached(ofMinutes(3))
.on(s3("my-bucket", "config.json"))
.asJson(),
new CustomInMemoryStore())
new CustomInMemoryStore()),
"BusinessConfiguration", FileStores.builder()
.cached(ofMinutes(25))
.on(etcd(URI.create("http://etcd/v2/keys/configuration")))
.asYaml()
));
}
}
Expand All @@ -88,6 +92,7 @@ Baigan configuration comes with a set of powerful integrations.
* The [Spring Boot](spring-boot-autoconfigure) module simplifies configuration in Spring applications.
* The [File](file) module provides a `ConfigurationStore` implementation serving from JSON and YAML files.
* The [S3](s3) module allows for fetching configuration files from S3 buckets.
* The [etcd](etcd) module allows using an etcd cluster as the configuration backend.

## Development

Expand All @@ -96,3 +101,5 @@ To build the project run
```bash
$ ./gradlew clean check
```

Be aware that the build requires Docker.
132 changes: 132 additions & 0 deletions core/src/main/java/org/zalando/baigan/CachedConfigurationStore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package org.zalando.baigan;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.Objects;
import java.util.Optional;
import java.util.StringJoiner;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static java.time.Clock.systemUTC;
import static java.util.Objects.requireNonNull;

public final class CachedConfigurationStore implements ConfigurationStore {

private static final Logger LOG = LoggerFactory.getLogger(CachedConfigurationStore.class);

private final ConcurrentMap<NamespacedKey, CacheEntry> cache = new ConcurrentHashMap<>(20);
private final ConfigurationStore delegate;
private final Duration cacheEntryLifetime;
private final Clock clock;

public CachedConfigurationStore(final Duration cacheEntryLifetime, final ConfigurationStore delegate) {
this(systemUTC(), cacheEntryLifetime, delegate);
}

CachedConfigurationStore(final Clock clock, final Duration cacheEntryLifetime, final ConfigurationStore delegate) {
this.cacheEntryLifetime = cacheEntryLifetime;
this.clock = clock;
this.delegate = delegate;
}

@Override
public Optional<Configuration> getConfiguration(final String namespace, final String key) {
final CacheEntry entry = fetchFromCache(namespace, key);
return Optional.ofNullable(entry).map(CacheEntry::getConfiguration);
}

private CacheEntry fetchFromCache(final String namespace, final String key) {
final NamespacedKey cacheKey = new NamespacedKey(namespace, key);

final Instant now = Instant.now(clock);
final CacheEntry entry = computeIfAbsent(cacheKey, now);
if (entry != null && isExpired(entry, now)) {
LOG.debug("Cache entry [{}] is expired, re-computing.", entry);
cache.remove(cacheKey, entry);
return computeIfAbsent(cacheKey, now);
}
return entry;
}

private boolean isExpired(final CacheEntry entry, final Instant now) {
final Duration between = Duration.between(entry.getCachedAt(), now).abs();
return between.compareTo(cacheEntryLifetime) > 0;
}

private CacheEntry computeIfAbsent(final NamespacedKey key, final Instant now) {
return cache.computeIfAbsent(key, $ ->
delegate.getConfiguration(key.getNamespace(), key.getKey()).map(c ->
new CacheEntry(now, c)).orElse(null));
}

private static final class CacheEntry {
private final Instant cachedAt;
private final Configuration configuration;

private CacheEntry(final Instant cachedAt, final Configuration configuration) {
this.cachedAt = cachedAt;
this.configuration = configuration;
}

Instant getCachedAt() {
return cachedAt;
}

Configuration getConfiguration() {
return configuration;
}

@Override
public String toString() {
return new StringJoiner(", ", CacheEntry.class.getSimpleName() + "[", "]")
.add("cachedAt=" + cachedAt)
.add("configuration=" + configuration)
.toString();
}
}

private static final class NamespacedKey {
private final String namespace;
private final String key;

private NamespacedKey(final String namespace, final String key) {
this.namespace = requireNonNull(namespace);
this.key = requireNonNull(key);
}

String getNamespace() {
return namespace;
}

String getKey() {
return key;
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final NamespacedKey that = (NamespacedKey) o;
return namespace.equals(that.namespace) &&
key.equals(that.key);
}

@Override
public int hashCode() {
return Objects.hash(namespace, key);
}

@Override
public String toString() {
return new StringJoiner(", ", NamespacedKey.class.getSimpleName() + "[", "]")
.add("namespace='" + namespace + "'")
.add("key='" + key + "'")
.toString();
}
}

}
11 changes: 11 additions & 0 deletions core/src/main/java/org/zalando/baigan/Configuration.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.zalando.baigan;

import java.util.StringJoiner;

import static java.util.Objects.requireNonNull;

public final class Configuration<T> {
Expand All @@ -25,4 +27,13 @@ public String getDescription() {
public T getValue() {
return value;
}

@Override
public String toString() {
return new StringJoiner(", ", Configuration.class.getSimpleName() + "[", "]")
.add("key='" + key + "'")
.add("description='" + description + "'")
.add("value=" + value)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package org.zalando.baigan;

import java.util.Optional;
import java.util.function.BiFunction;

public interface ConfigurationStore {
@FunctionalInterface
public interface ConfigurationStore extends BiFunction<String, String, Optional<Configuration>> {

@Override
default Optional<Configuration> apply(String namespace, String key) {
return getConfiguration(namespace, key);
}

@SuppressWarnings("unchecked")
default <T> Optional<Configuration<T>> getTypedConfiguration(final String namespace, final String key) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package org.zalando.baigan;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static java.time.Duration.ofMinutes;

class CachedConfigurationStoreTest {

private final MockClock clock = new MockClock(Instant.now());
private final ConfigurationStore delegate = mock(ConfigurationStore.class);
private final CachedConfigurationStore store = new CachedConfigurationStore(clock, ofMinutes(1), delegate);

@BeforeEach
void setUp() {
when(store.getConfiguration("ns", "key"))
.thenReturn(Optional.of(new Configuration<>("ns.key", "description", "value")));
}

@Test
void cachesConfiguration() {
store.getConfiguration("ns", "key");
store.getConfiguration("ns", "key");

verify(delegate).getConfiguration("ns", "key");
verifyNoMoreInteractions(delegate);
}

@Test
void expiresCacheEntry() {
store.getConfiguration("ns", "key");
clock.forward(ofMinutes(2));
store.getConfiguration("ns", "key");

verify(delegate, times(2)).getConfiguration("ns", "key");
verifyNoMoreInteractions(delegate);
}

@Test
void cachesAfterExpiringCacheEntry() {
store.getConfiguration("ns", "key");
clock.forward(ofMinutes(2));
store.getConfiguration("ns", "key");
store.getConfiguration("ns", "key");

verify(delegate, times(2)).getConfiguration("ns", "key");
verifyNoMoreInteractions(delegate);
}

@Test
void doesNotCacheMisses() {
assertEquals(Optional.empty(), store.getConfiguration("ns", "missing"));
assertEquals(Optional.empty(), store.getConfiguration("ns", "missing"));

verify(delegate, times(2)).getConfiguration("ns", "missing");
verifyNoMoreInteractions(delegate);
}

private static class MockClock extends Clock {
private Instant instant;

MockClock(final Instant instant) {
this.instant = instant;
}

void forward(final Duration duration) {
this.instant = instant.plus(duration);
}

@Override
public Instant instant() {
return instant;
}

@Override
public ZoneId getZone() {
throw new UnsupportedOperationException();
}

@Override
public Clock withZone(final ZoneId zone) {
throw new UnsupportedOperationException();
}
}
}
Loading