blob: 51b8f6df85be0758f8e7f8205be0985267e88980 [file] [log] [blame]
Jonathan Hart6ec029a2015-03-24 17:12:35 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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
18import org.onlab.util.KryoNamespace;
19import org.onosproject.cluster.ClusterService;
20import org.onosproject.cluster.NodeId;
Aaron Kruglikov66cf0b92015-10-26 15:46:54 -070021import org.onosproject.persistence.PersistenceService;
Madan Jampanibcf1a482015-06-24 19:05:56 -070022import org.onosproject.store.Timestamp;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070023import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070024import org.onosproject.store.service.EventuallyConsistentMap;
25import org.onosproject.store.service.EventuallyConsistentMapBuilder;
26
27import java.util.Collection;
28import java.util.concurrent.ExecutorService;
29import java.util.concurrent.ScheduledExecutorService;
30import java.util.concurrent.TimeUnit;
31import java.util.function.BiFunction;
32
33import static com.google.common.base.Preconditions.checkArgument;
34import static com.google.common.base.Preconditions.checkNotNull;
35
36/**
37 * Eventually consistent map builder.
38 */
39public class EventuallyConsistentMapBuilderImpl<K, V>
40 implements EventuallyConsistentMapBuilder<K, V> {
41 private final ClusterService clusterService;
42 private final ClusterCommunicationService clusterCommunicator;
43
44 private String name;
45 private KryoNamespace.Builder serializerBuilder;
46 private ExecutorService eventExecutor;
47 private ExecutorService communicationExecutor;
48 private ScheduledExecutorService backgroundExecutor;
Madan Jampanibcf1a482015-06-24 19:05:56 -070049 private BiFunction<K, V, Timestamp> timestampProvider;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070050 private BiFunction<K, V, Collection<NodeId>> peerUpdateFunction;
51 private boolean tombstonesDisabled = false;
52 private long antiEntropyPeriod = 5;
53 private TimeUnit antiEntropyTimeUnit = TimeUnit.SECONDS;
54 private boolean convergeFaster = false;
Jonathan Hartca335e92015-03-05 10:34:32 -080055 private boolean persistent = false;
Aaron Kruglikov66cf0b92015-10-26 15:46:54 -070056 private boolean persistentMap = false;
57 private final PersistenceService persistenceService;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070058
59 /**
60 * Creates a new eventually consistent map builder.
61 *
62 * @param clusterService cluster service
63 * @param clusterCommunicator cluster communication service
Jian Lidfba7392016-01-22 16:46:58 -080064 * @param persistenceService persistence service
Jonathan Hart6ec029a2015-03-24 17:12:35 -070065 */
66 public EventuallyConsistentMapBuilderImpl(ClusterService clusterService,
Aaron Kruglikov66cf0b92015-10-26 15:46:54 -070067 ClusterCommunicationService clusterCommunicator,
68 PersistenceService persistenceService) {
69 this.persistenceService = persistenceService;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070070 this.clusterService = checkNotNull(clusterService);
71 this.clusterCommunicator = checkNotNull(clusterCommunicator);
72 }
73
74 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -070075 public EventuallyConsistentMapBuilder<K, V> withName(String name) {
Jonathan Hart6ec029a2015-03-24 17:12:35 -070076 this.name = checkNotNull(name);
77 return this;
78 }
79
80 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -070081 public EventuallyConsistentMapBuilder<K, V> withSerializer(
Jonathan Hart6ec029a2015-03-24 17:12:35 -070082 KryoNamespace.Builder serializerBuilder) {
83 this.serializerBuilder = checkNotNull(serializerBuilder);
84 return this;
85 }
86
87 @Override
Madan Jampanibcf1a482015-06-24 19:05:56 -070088 public EventuallyConsistentMapBuilder<K, V> withTimestampProvider(
89 BiFunction<K, V, Timestamp> timestampProvider) {
90 this.timestampProvider = checkNotNull(timestampProvider);
Jonathan Hart6ec029a2015-03-24 17:12:35 -070091 return this;
92 }
93
94 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -070095 public EventuallyConsistentMapBuilder<K, V> withEventExecutor(ExecutorService executor) {
Jonathan Hart6ec029a2015-03-24 17:12:35 -070096 this.eventExecutor = checkNotNull(executor);
97 return this;
98 }
99
100 @Override
101 public EventuallyConsistentMapBuilder<K, V> withCommunicationExecutor(
102 ExecutorService executor) {
103 communicationExecutor = checkNotNull(executor);
104 return this;
105 }
106
107 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -0700108 public EventuallyConsistentMapBuilder<K, V> withBackgroundExecutor(ScheduledExecutorService executor) {
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700109 this.backgroundExecutor = checkNotNull(executor);
110 return this;
111 }
112
113 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -0700114 public EventuallyConsistentMapBuilder<K, V> withPeerUpdateFunction(
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700115 BiFunction<K, V, Collection<NodeId>> peerUpdateFunction) {
116 this.peerUpdateFunction = checkNotNull(peerUpdateFunction);
117 return this;
118 }
119
120 @Override
121 public EventuallyConsistentMapBuilder<K, V> withTombstonesDisabled() {
122 tombstonesDisabled = true;
123 return this;
124 }
125
126 @Override
127 public EventuallyConsistentMapBuilder<K, V> withAntiEntropyPeriod(long period, TimeUnit unit) {
128 checkArgument(period > 0, "anti-entropy period must be greater than 0");
129 antiEntropyPeriod = period;
130 antiEntropyTimeUnit = checkNotNull(unit);
131 return this;
132 }
133
134 @Override
135 public EventuallyConsistentMapBuilder<K, V> withFasterConvergence() {
136 convergeFaster = true;
137 return this;
138 }
139
140 @Override
Jonathan Hartca335e92015-03-05 10:34:32 -0800141 public EventuallyConsistentMapBuilder<K, V> withPersistence() {
Aaron Kruglikov66cf0b92015-10-26 15:46:54 -0700142 checkNotNull(this.persistenceService);
Jonathan Hartca335e92015-03-05 10:34:32 -0800143 persistent = true;
144 return this;
145 }
146
147 @Override
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700148 public EventuallyConsistentMap<K, V> build() {
149 checkNotNull(name, "name is a mandatory parameter");
150 checkNotNull(serializerBuilder, "serializerBuilder is a mandatory parameter");
Madan Jampanibcf1a482015-06-24 19:05:56 -0700151 checkNotNull(timestampProvider, "timestampProvider is a mandatory parameter");
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700152
153 return new EventuallyConsistentMapImpl<>(name,
154 clusterService,
155 clusterCommunicator,
156 serializerBuilder,
Madan Jampanibcf1a482015-06-24 19:05:56 -0700157 timestampProvider,
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700158 peerUpdateFunction,
159 eventExecutor,
160 communicationExecutor,
161 backgroundExecutor,
162 tombstonesDisabled,
163 antiEntropyPeriod,
164 antiEntropyTimeUnit,
Jonathan Hartca335e92015-03-05 10:34:32 -0800165 convergeFaster,
Aaron Kruglikov66cf0b92015-10-26 15:46:54 -0700166 persistent,
167 persistenceService);
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700168 }
169}