blob: a553ffff387e0eb112b1fc23113d2159483ce39e [file] [log] [blame]
Jonathan Hart6ec029a2015-03-24 17:12:35 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16package org.onosproject.store.ecmap;
17
18import org.onlab.util.KryoNamespace;
19import org.onosproject.cluster.ClusterService;
20import org.onosproject.cluster.NodeId;
Madan Jampanibcf1a482015-06-24 19:05:56 -070021import org.onosproject.store.Timestamp;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070022import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070023import org.onosproject.store.service.EventuallyConsistentMap;
24import org.onosproject.store.service.EventuallyConsistentMapBuilder;
25
26import java.util.Collection;
27import java.util.concurrent.ExecutorService;
28import java.util.concurrent.ScheduledExecutorService;
29import java.util.concurrent.TimeUnit;
30import java.util.function.BiFunction;
31
32import static com.google.common.base.Preconditions.checkArgument;
33import static com.google.common.base.Preconditions.checkNotNull;
34
35/**
36 * Eventually consistent map builder.
37 */
38public class EventuallyConsistentMapBuilderImpl<K, V>
39 implements EventuallyConsistentMapBuilder<K, V> {
40 private final ClusterService clusterService;
41 private final ClusterCommunicationService clusterCommunicator;
42
43 private String name;
44 private KryoNamespace.Builder serializerBuilder;
45 private ExecutorService eventExecutor;
46 private ExecutorService communicationExecutor;
47 private ScheduledExecutorService backgroundExecutor;
Madan Jampanibcf1a482015-06-24 19:05:56 -070048 private BiFunction<K, V, Timestamp> timestampProvider;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070049 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;
Jonathan Hartca335e92015-03-05 10:34:32 -080054 private boolean persistent = false;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070055
56 /**
57 * Creates a new eventually consistent map builder.
58 *
59 * @param clusterService cluster service
60 * @param clusterCommunicator cluster communication service
61 */
62 public EventuallyConsistentMapBuilderImpl(ClusterService clusterService,
63 ClusterCommunicationService clusterCommunicator) {
64 this.clusterService = checkNotNull(clusterService);
65 this.clusterCommunicator = checkNotNull(clusterCommunicator);
66 }
67
68 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -070069 public EventuallyConsistentMapBuilder<K, V> withName(String name) {
Jonathan Hart6ec029a2015-03-24 17:12:35 -070070 this.name = checkNotNull(name);
71 return this;
72 }
73
74 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -070075 public EventuallyConsistentMapBuilder<K, V> withSerializer(
Jonathan Hart6ec029a2015-03-24 17:12:35 -070076 KryoNamespace.Builder serializerBuilder) {
77 this.serializerBuilder = checkNotNull(serializerBuilder);
78 return this;
79 }
80
81 @Override
Madan Jampanibcf1a482015-06-24 19:05:56 -070082 public EventuallyConsistentMapBuilder<K, V> withTimestampProvider(
83 BiFunction<K, V, Timestamp> timestampProvider) {
84 this.timestampProvider = checkNotNull(timestampProvider);
Jonathan Hart6ec029a2015-03-24 17:12:35 -070085 return this;
86 }
87
88 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -070089 public EventuallyConsistentMapBuilder<K, V> withEventExecutor(ExecutorService executor) {
Jonathan Hart6ec029a2015-03-24 17:12:35 -070090 this.eventExecutor = checkNotNull(executor);
91 return this;
92 }
93
94 @Override
95 public EventuallyConsistentMapBuilder<K, V> withCommunicationExecutor(
96 ExecutorService executor) {
97 communicationExecutor = checkNotNull(executor);
98 return this;
99 }
100
101 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -0700102 public EventuallyConsistentMapBuilder<K, V> withBackgroundExecutor(ScheduledExecutorService executor) {
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700103 this.backgroundExecutor = checkNotNull(executor);
104 return this;
105 }
106
107 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -0700108 public EventuallyConsistentMapBuilder<K, V> withPeerUpdateFunction(
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700109 BiFunction<K, V, Collection<NodeId>> peerUpdateFunction) {
110 this.peerUpdateFunction = checkNotNull(peerUpdateFunction);
111 return this;
112 }
113
114 @Override
115 public EventuallyConsistentMapBuilder<K, V> withTombstonesDisabled() {
116 tombstonesDisabled = true;
117 return this;
118 }
119
120 @Override
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);
125 return this;
126 }
127
128 @Override
129 public EventuallyConsistentMapBuilder<K, V> withFasterConvergence() {
130 convergeFaster = true;
131 return this;
132 }
133
134 @Override
Jonathan Hartca335e92015-03-05 10:34:32 -0800135 public EventuallyConsistentMapBuilder<K, V> withPersistence() {
136 persistent = true;
137 return this;
138 }
139
140 @Override
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700141 public EventuallyConsistentMap<K, V> build() {
142 checkNotNull(name, "name is a mandatory parameter");
143 checkNotNull(serializerBuilder, "serializerBuilder is a mandatory parameter");
Madan Jampanibcf1a482015-06-24 19:05:56 -0700144 checkNotNull(timestampProvider, "timestampProvider is a mandatory parameter");
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700145
146 return new EventuallyConsistentMapImpl<>(name,
147 clusterService,
148 clusterCommunicator,
149 serializerBuilder,
Madan Jampanibcf1a482015-06-24 19:05:56 -0700150 timestampProvider,
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700151 peerUpdateFunction,
152 eventExecutor,
153 communicationExecutor,
154 backgroundExecutor,
155 tombstonesDisabled,
156 antiEntropyPeriod,
157 antiEntropyTimeUnit,
Jonathan Hartca335e92015-03-05 10:34:32 -0800158 convergeFaster,
159 persistent);
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700160 }
161}