blob: da00555ddf58251fc2606c6f32b6f87e650c936e [file] [log] [blame]
Jonathan Hart6ec029a2015-03-24 17:12:35 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jonathan Hart6ec029a2015-03-24 17:12:35 -07003 *
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 */
Madan Jampanif4c88502016-01-21 12:35:36 -080016package org.onosproject.store.primitives.impl;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070017
slowr878625f2017-10-24 14:53:49 -070018import java.util.Collection;
19import java.util.List;
20import java.util.concurrent.ExecutorService;
21import java.util.concurrent.ScheduledExecutorService;
22import java.util.concurrent.TimeUnit;
23import java.util.function.BiFunction;
24import java.util.function.Supplier;
25
Jordan Halterman00e92da2018-05-22 23:05:52 -070026import org.onlab.util.KryoNamespace;
27import org.onosproject.cluster.NodeId;
28import org.onosproject.persistence.PersistenceService;
29import org.onosproject.store.Timestamp;
30import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
31import org.onosproject.store.service.EventuallyConsistentMap;
32import org.onosproject.store.service.EventuallyConsistentMapBuilder;
33
Jonathan Hart6ec029a2015-03-24 17:12:35 -070034import static com.google.common.base.Preconditions.checkArgument;
35import static com.google.common.base.Preconditions.checkNotNull;
36
37/**
38 * Eventually consistent map builder.
39 */
40public class EventuallyConsistentMapBuilderImpl<K, V>
41 implements EventuallyConsistentMapBuilder<K, V> {
Jordan Halterman28183ee2017-10-17 17:29:10 -070042 private final ClusterCommunicationService clusterCommunicator;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070043
slowr878625f2017-10-24 14:53:49 -070044 private NodeId localNodeId;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070045 private String name;
HIGUCHI Yuta163efb52016-05-18 19:24:19 -070046 private KryoNamespace serializer;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070047 private KryoNamespace.Builder serializerBuilder;
48 private ExecutorService eventExecutor;
49 private ExecutorService communicationExecutor;
50 private ScheduledExecutorService backgroundExecutor;
Madan Jampanibcf1a482015-06-24 19:05:56 -070051 private BiFunction<K, V, Timestamp> timestampProvider;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070052 private BiFunction<K, V, Collection<NodeId>> peerUpdateFunction;
53 private boolean tombstonesDisabled = false;
54 private long antiEntropyPeriod = 5;
55 private TimeUnit antiEntropyTimeUnit = TimeUnit.SECONDS;
56 private boolean convergeFaster = false;
Jonathan Hartca335e92015-03-05 10:34:32 -080057 private boolean persistent = false;
Aaron Kruglikov66cf0b92015-10-26 15:46:54 -070058 private boolean persistentMap = false;
59 private final PersistenceService persistenceService;
slowr878625f2017-10-24 14:53:49 -070060 private Supplier<List<NodeId>> peersSupplier;
61 private Supplier<List<NodeId>> bootstrapPeersSupplier;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070062
63 /**
64 * Creates a new eventually consistent map builder.
slowr878625f2017-10-24 14:53:49 -070065 * @param localNodeId local node id
66 * @param clusterCommunicator cluster communication service
67 * @param persistenceService persistence service
68 * @param peersSupplier supplier for peers
69 * @param bootstrapPeersSupplier supplier for peers for bootstrap
Jonathan Hart6ec029a2015-03-24 17:12:35 -070070 */
Jordan Halterman28183ee2017-10-17 17:29:10 -070071 public EventuallyConsistentMapBuilderImpl(
slowr878625f2017-10-24 14:53:49 -070072 NodeId localNodeId,
Jordan Halterman28183ee2017-10-17 17:29:10 -070073 ClusterCommunicationService clusterCommunicator,
slowr878625f2017-10-24 14:53:49 -070074 PersistenceService persistenceService,
75 Supplier<List<NodeId>> peersSupplier,
76 Supplier<List<NodeId>> bootstrapPeersSupplier
77 ) {
78 this.localNodeId = localNodeId;
Aaron Kruglikov66cf0b92015-10-26 15:46:54 -070079 this.persistenceService = persistenceService;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070080 this.clusterCommunicator = checkNotNull(clusterCommunicator);
slowr878625f2017-10-24 14:53:49 -070081 this.peersSupplier = peersSupplier;
82 this.bootstrapPeersSupplier = bootstrapPeersSupplier;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070083 }
84
85 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -070086 public EventuallyConsistentMapBuilder<K, V> withName(String name) {
Jonathan Hart6ec029a2015-03-24 17:12:35 -070087 this.name = checkNotNull(name);
88 return this;
89 }
90
91 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -070092 public EventuallyConsistentMapBuilder<K, V> withSerializer(
Jonathan Hart6ec029a2015-03-24 17:12:35 -070093 KryoNamespace.Builder serializerBuilder) {
94 this.serializerBuilder = checkNotNull(serializerBuilder);
95 return this;
96 }
97
98 @Override
HIGUCHI Yuta163efb52016-05-18 19:24:19 -070099 public EventuallyConsistentMapBuilder<K, V> withSerializer(KryoNamespace serializer) {
100 this.serializer = checkNotNull(serializer);
101 return this;
102 }
103
104 @Override
Madan Jampanibcf1a482015-06-24 19:05:56 -0700105 public EventuallyConsistentMapBuilder<K, V> withTimestampProvider(
106 BiFunction<K, V, Timestamp> timestampProvider) {
107 this.timestampProvider = checkNotNull(timestampProvider);
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700108 return this;
109 }
110
111 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -0700112 public EventuallyConsistentMapBuilder<K, V> withEventExecutor(ExecutorService executor) {
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700113 this.eventExecutor = checkNotNull(executor);
114 return this;
115 }
116
117 @Override
118 public EventuallyConsistentMapBuilder<K, V> withCommunicationExecutor(
119 ExecutorService executor) {
120 communicationExecutor = checkNotNull(executor);
121 return this;
122 }
123
124 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -0700125 public EventuallyConsistentMapBuilder<K, V> withBackgroundExecutor(ScheduledExecutorService executor) {
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700126 this.backgroundExecutor = checkNotNull(executor);
127 return this;
128 }
129
130 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -0700131 public EventuallyConsistentMapBuilder<K, V> withPeerUpdateFunction(
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700132 BiFunction<K, V, Collection<NodeId>> peerUpdateFunction) {
133 this.peerUpdateFunction = checkNotNull(peerUpdateFunction);
134 return this;
135 }
136
137 @Override
138 public EventuallyConsistentMapBuilder<K, V> withTombstonesDisabled() {
139 tombstonesDisabled = true;
140 return this;
141 }
142
143 @Override
144 public EventuallyConsistentMapBuilder<K, V> withAntiEntropyPeriod(long period, TimeUnit unit) {
145 checkArgument(period > 0, "anti-entropy period must be greater than 0");
146 antiEntropyPeriod = period;
147 antiEntropyTimeUnit = checkNotNull(unit);
148 return this;
149 }
150
151 @Override
152 public EventuallyConsistentMapBuilder<K, V> withFasterConvergence() {
153 convergeFaster = true;
154 return this;
155 }
156
157 @Override
Jonathan Hartca335e92015-03-05 10:34:32 -0800158 public EventuallyConsistentMapBuilder<K, V> withPersistence() {
Aaron Kruglikov66cf0b92015-10-26 15:46:54 -0700159 checkNotNull(this.persistenceService);
Jonathan Hartca335e92015-03-05 10:34:32 -0800160 persistent = true;
161 return this;
162 }
163
164 @Override
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700165 public EventuallyConsistentMap<K, V> build() {
166 checkNotNull(name, "name is a mandatory parameter");
Madan Jampanibcf1a482015-06-24 19:05:56 -0700167 checkNotNull(timestampProvider, "timestampProvider is a mandatory parameter");
HIGUCHI Yuta163efb52016-05-18 19:24:19 -0700168 if (serializer == null && serializerBuilder != null) {
169 serializer = serializerBuilder.build(name);
170 }
171 checkNotNull(serializer, "serializer is a mandatory parameter");
slowr878625f2017-10-24 14:53:49 -0700172 checkNotNull(localNodeId, "local node id cannot be null");
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700173
slowr878625f2017-10-24 14:53:49 -0700174 return new EventuallyConsistentMapImpl<>(
175 localNodeId,
176 name,
177 clusterCommunicator,
178 serializer,
179 timestampProvider,
180 peerUpdateFunction,
181 eventExecutor,
182 communicationExecutor,
183 backgroundExecutor,
184 tombstonesDisabled,
185 antiEntropyPeriod,
186 antiEntropyTimeUnit,
187 convergeFaster,
188 persistent,
189 persistenceService,
190 peersSupplier,
191 bootstrapPeersSupplier
192 );
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700193 }
194}