ONOS-2429: Support for a relaxed read consistency mode in ConsistentMap
Change-Id: I7866d8d15de369fd23e482d66cc1c918c033f169
diff --git a/core/api/src/main/java/org/onosproject/store/service/ConsistentMapBuilder.java b/core/api/src/main/java/org/onosproject/store/service/ConsistentMapBuilder.java
index b4251c6..847adaf 100644
--- a/core/api/src/main/java/org/onosproject/store/service/ConsistentMapBuilder.java
+++ b/core/api/src/main/java/org/onosproject/store/service/ConsistentMapBuilder.java
@@ -113,6 +113,17 @@
ConsistentMapBuilder<K, V> withMeteringDisabled();
/**
+ * Provides weak consistency for map gets.
+ * <p>
+ * While this can lead to improved read performance, it can also make the behavior
+ * heard to reason. Only turn this on if you know what you are doing. By default
+ * reads are strongly consistent.
+ *
+ * @return this ConsistentMapBuilder
+ */
+ ConsistentMapBuilder<K, V> withRelaxedReadConsistency();
+
+ /**
* Builds an consistent map based on the configuration options
* supplied to this builder.
*
diff --git a/core/api/src/test/java/org/onosproject/store/service/TestConsistentMap.java b/core/api/src/test/java/org/onosproject/store/service/TestConsistentMap.java
index 8945137..3573230 100644
--- a/core/api/src/test/java/org/onosproject/store/service/TestConsistentMap.java
+++ b/core/api/src/test/java/org/onosproject/store/service/TestConsistentMap.java
@@ -247,6 +247,11 @@
}
@Override
+ public ConsistentMapBuilder<K, V> withRelaxedReadConsistency() {
+ return this;
+ }
+
+ @Override
public ConsistentMapBuilder<K, V> withMeteringDisabled() {
return this;
}
diff --git a/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/AsyncCachingConsistentMap.java b/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/AsyncCachingConsistentMap.java
new file mode 100644
index 0000000..bbc703d
--- /dev/null
+++ b/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/AsyncCachingConsistentMap.java
@@ -0,0 +1,56 @@
+package org.onosproject.store.consistent.impl;
+
+import java.util.concurrent.CompletableFuture;
+
+import org.onosproject.core.ApplicationId;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.Versioned;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+
+/**
+ * Extension of DefaultAsyncConsistentMap that provides a weaker read consistency
+ * guarantee in return for better read performance.
+ *
+ * @param <K> key type
+ * @param <V> value type
+ */
+public class AsyncCachingConsistentMap<K, V> extends DefaultAsyncConsistentMap<K, V> {
+
+ private final LoadingCache<K, CompletableFuture<Versioned<V>>> cache =
+ CacheBuilder.newBuilder()
+ .maximumSize(10000) // TODO: make configurable
+ .build(new CacheLoader<K, CompletableFuture<Versioned<V>>>() {
+ @Override
+ public CompletableFuture<Versioned<V>> load(K key)
+ throws Exception {
+ return AsyncCachingConsistentMap.super.get(key);
+ }
+ });
+
+ public AsyncCachingConsistentMap(String name,
+ ApplicationId applicationId,
+ Database database,
+ Serializer serializer,
+ boolean readOnly,
+ boolean purgeOnUninstall,
+ boolean meteringEnabled) {
+ super(name, applicationId, database, serializer, readOnly, purgeOnUninstall, meteringEnabled);
+ addListener(event -> cache.invalidate(event.key()));
+ }
+
+ @Override
+ public CompletableFuture<Versioned<V>> get(K key) {
+ CompletableFuture<Versioned<V>> cachedValue = cache.getIfPresent(key);
+ if (cachedValue != null) {
+ if (cachedValue.isCompletedExceptionally()) {
+ cache.invalidate(key);
+ } else {
+ return cachedValue;
+ }
+ }
+ return cache.getUnchecked(key);
+ }
+}
\ No newline at end of file
diff --git a/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DefaultConsistentMapBuilder.java b/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DefaultConsistentMapBuilder.java
index 965791d..0e11794 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DefaultConsistentMapBuilder.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DefaultConsistentMapBuilder.java
@@ -39,6 +39,7 @@
private boolean partitionsEnabled = true;
private boolean readOnly = false;
private boolean metering = true;
+ private boolean relaxedReadConsistency = false;
private final DatabaseManager manager;
public DefaultConsistentMapBuilder(DatabaseManager manager) {
@@ -90,6 +91,12 @@
return this;
}
+ @Override
+ public ConsistentMapBuilder<K, V> withRelaxedReadConsistency() {
+ relaxedReadConsistency = true;
+ return this;
+ }
+
private void validateInputs() {
checkState(name != null, "name must be specified");
checkState(serializer != null, "serializer must be specified");
@@ -110,14 +117,25 @@
private DefaultAsyncConsistentMap<K, V> buildAndRegisterMap() {
validateInputs();
- DefaultAsyncConsistentMap<K, V> asyncMap = new DefaultAsyncConsistentMap<>(
- name,
- applicationId,
- partitionsEnabled ? manager.partitionedDatabase : manager.inMemoryDatabase,
- serializer,
- readOnly,
- purgeOnUninstall,
- metering);
- return manager.registerMap(asyncMap);
+ Database database = partitionsEnabled ? manager.partitionedDatabase : manager.inMemoryDatabase;
+ if (relaxedReadConsistency) {
+ return manager.registerMap(
+ new AsyncCachingConsistentMap<>(name,
+ applicationId,
+ database,
+ serializer,
+ readOnly,
+ purgeOnUninstall,
+ metering));
+ } else {
+ return manager.registerMap(
+ new DefaultAsyncConsistentMap<>(name,
+ applicationId,
+ database,
+ serializer,
+ readOnly,
+ purgeOnUninstall,
+ metering));
+ }
}
}
\ No newline at end of file
diff --git a/incubator/store/src/main/java/org/onosproject/incubator/store/config/impl/DistributedNetworkConfigStore.java b/incubator/store/src/main/java/org/onosproject/incubator/store/config/impl/DistributedNetworkConfigStore.java
index 0b7c095..37d8a7f 100644
--- a/incubator/store/src/main/java/org/onosproject/incubator/store/config/impl/DistributedNetworkConfigStore.java
+++ b/incubator/store/src/main/java/org/onosproject/incubator/store/config/impl/DistributedNetworkConfigStore.java
@@ -94,6 +94,7 @@
configs = storageService.<ConfigKey, ObjectNode>consistentMapBuilder()
.withSerializer(Serializer.using(kryoBuilder.build()))
.withName("onos-network-configs")
+ .withRelaxedReadConsistency()
.build();
configs.addListener(listener);
log.info("Started");