2 * Copyright 2015 Open Networking Laboratory
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onosproject.store.ecmap;
18 import org.onlab.util.KryoNamespace;
19 import org.onosproject.cluster.ClusterService;
20 import org.onosproject.cluster.NodeId;
21 import org.onosproject.store.Timestamp;
22 import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
23 import org.onosproject.store.service.EventuallyConsistentMap;
24 import org.onosproject.store.service.EventuallyConsistentMapBuilder;
26 import java.util.Collection;
27 import java.util.concurrent.ExecutorService;
28 import java.util.concurrent.ScheduledExecutorService;
29 import java.util.concurrent.TimeUnit;
30 import java.util.function.BiFunction;
32 import static com.google.common.base.Preconditions.checkArgument;
33 import static com.google.common.base.Preconditions.checkNotNull;
36 * Eventually consistent map builder.
38 public class EventuallyConsistentMapBuilderImpl<K, V>
39 implements EventuallyConsistentMapBuilder<K, V> {
40 private final ClusterService clusterService;
41 private final ClusterCommunicationService clusterCommunicator;
44 private KryoNamespace.Builder serializerBuilder;
45 private ExecutorService eventExecutor;
46 private ExecutorService communicationExecutor;
47 private ScheduledExecutorService backgroundExecutor;
48 private BiFunction<K, V, Timestamp> timestampProvider;
49 private BiFunction<K, V, Collection<NodeId>> peerUpdateFunction;
50 private boolean tombstonesDisabled = false;
51 private long antiEntropyPeriod = 5;
52 private TimeUnit antiEntropyTimeUnit = TimeUnit.SECONDS;
53 private boolean convergeFaster = false;
54 private boolean persistent = false;
57 * Creates a new eventually consistent map builder.
59 * @param clusterService cluster service
60 * @param clusterCommunicator cluster communication service
62 public EventuallyConsistentMapBuilderImpl(ClusterService clusterService,
63 ClusterCommunicationService clusterCommunicator) {
64 this.clusterService = checkNotNull(clusterService);
65 this.clusterCommunicator = checkNotNull(clusterCommunicator);
69 public EventuallyConsistentMapBuilder<K, V> withName(String name) {
70 this.name = checkNotNull(name);
75 public EventuallyConsistentMapBuilder<K, V> withSerializer(
76 KryoNamespace.Builder serializerBuilder) {
77 this.serializerBuilder = checkNotNull(serializerBuilder);
82 public EventuallyConsistentMapBuilder<K, V> withTimestampProvider(
83 BiFunction<K, V, Timestamp> timestampProvider) {
84 this.timestampProvider = checkNotNull(timestampProvider);
89 public EventuallyConsistentMapBuilder<K, V> withEventExecutor(ExecutorService executor) {
90 this.eventExecutor = checkNotNull(executor);
95 public EventuallyConsistentMapBuilder<K, V> withCommunicationExecutor(
96 ExecutorService executor) {
97 communicationExecutor = checkNotNull(executor);
102 public EventuallyConsistentMapBuilder<K, V> withBackgroundExecutor(ScheduledExecutorService executor) {
103 this.backgroundExecutor = checkNotNull(executor);
108 public EventuallyConsistentMapBuilder<K, V> withPeerUpdateFunction(
109 BiFunction<K, V, Collection<NodeId>> peerUpdateFunction) {
110 this.peerUpdateFunction = checkNotNull(peerUpdateFunction);
115 public EventuallyConsistentMapBuilder<K, V> withTombstonesDisabled() {
116 tombstonesDisabled = true;
121 public EventuallyConsistentMapBuilder<K, V> withAntiEntropyPeriod(long period, TimeUnit unit) {
122 checkArgument(period > 0, "anti-entropy period must be greater than 0");
123 antiEntropyPeriod = period;
124 antiEntropyTimeUnit = checkNotNull(unit);
129 public EventuallyConsistentMapBuilder<K, V> withFasterConvergence() {
130 convergeFaster = true;
135 public EventuallyConsistentMapBuilder<K, V> withPersistence() {
141 public EventuallyConsistentMap<K, V> build() {
142 checkNotNull(name, "name is a mandatory parameter");
143 checkNotNull(serializerBuilder, "serializerBuilder is a mandatory parameter");
144 checkNotNull(timestampProvider, "timestampProvider is a mandatory parameter");
146 return new EventuallyConsistentMapImpl<>(name,
153 communicationExecutor,