blob: 7ab30ea9de9583fcefd53d8ea869792e57dde53b [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 */
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
64 */
65 public EventuallyConsistentMapBuilderImpl(ClusterService clusterService,
Aaron Kruglikov66cf0b92015-10-26 15:46:54 -070066 ClusterCommunicationService clusterCommunicator,
67 PersistenceService persistenceService) {
68 this.persistenceService = persistenceService;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070069 this.clusterService = checkNotNull(clusterService);
70 this.clusterCommunicator = checkNotNull(clusterCommunicator);
71 }
72
73 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -070074 public EventuallyConsistentMapBuilder<K, V> withName(String name) {
Jonathan Hart6ec029a2015-03-24 17:12:35 -070075 this.name = checkNotNull(name);
76 return this;
77 }
78
79 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -070080 public EventuallyConsistentMapBuilder<K, V> withSerializer(
Jonathan Hart6ec029a2015-03-24 17:12:35 -070081 KryoNamespace.Builder serializerBuilder) {
82 this.serializerBuilder = checkNotNull(serializerBuilder);
83 return this;
84 }
85
86 @Override
Madan Jampanibcf1a482015-06-24 19:05:56 -070087 public EventuallyConsistentMapBuilder<K, V> withTimestampProvider(
88 BiFunction<K, V, Timestamp> timestampProvider) {
89 this.timestampProvider = checkNotNull(timestampProvider);
Jonathan Hart6ec029a2015-03-24 17:12:35 -070090 return this;
91 }
92
93 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -070094 public EventuallyConsistentMapBuilder<K, V> withEventExecutor(ExecutorService executor) {
Jonathan Hart6ec029a2015-03-24 17:12:35 -070095 this.eventExecutor = checkNotNull(executor);
96 return this;
97 }
98
99 @Override
100 public EventuallyConsistentMapBuilder<K, V> withCommunicationExecutor(
101 ExecutorService executor) {
102 communicationExecutor = checkNotNull(executor);
103 return this;
104 }
105
106 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -0700107 public EventuallyConsistentMapBuilder<K, V> withBackgroundExecutor(ScheduledExecutorService executor) {
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700108 this.backgroundExecutor = checkNotNull(executor);
109 return this;
110 }
111
112 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -0700113 public EventuallyConsistentMapBuilder<K, V> withPeerUpdateFunction(
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700114 BiFunction<K, V, Collection<NodeId>> peerUpdateFunction) {
115 this.peerUpdateFunction = checkNotNull(peerUpdateFunction);
116 return this;
117 }
118
119 @Override
120 public EventuallyConsistentMapBuilder<K, V> withTombstonesDisabled() {
121 tombstonesDisabled = true;
122 return this;
123 }
124
125 @Override
126 public EventuallyConsistentMapBuilder<K, V> withAntiEntropyPeriod(long period, TimeUnit unit) {
127 checkArgument(period > 0, "anti-entropy period must be greater than 0");
128 antiEntropyPeriod = period;
129 antiEntropyTimeUnit = checkNotNull(unit);
130 return this;
131 }
132
133 @Override
134 public EventuallyConsistentMapBuilder<K, V> withFasterConvergence() {
135 convergeFaster = true;
136 return this;
137 }
138
139 @Override
Jonathan Hartca335e92015-03-05 10:34:32 -0800140 public EventuallyConsistentMapBuilder<K, V> withPersistence() {
Aaron Kruglikov66cf0b92015-10-26 15:46:54 -0700141 checkNotNull(this.persistenceService);
Jonathan Hartca335e92015-03-05 10:34:32 -0800142 persistent = true;
143 return this;
144 }
145
146 @Override
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700147 public EventuallyConsistentMap<K, V> build() {
148 checkNotNull(name, "name is a mandatory parameter");
149 checkNotNull(serializerBuilder, "serializerBuilder is a mandatory parameter");
Madan Jampanibcf1a482015-06-24 19:05:56 -0700150 checkNotNull(timestampProvider, "timestampProvider is a mandatory parameter");
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700151
152 return new EventuallyConsistentMapImpl<>(name,
153 clusterService,
154 clusterCommunicator,
155 serializerBuilder,
Madan Jampanibcf1a482015-06-24 19:05:56 -0700156 timestampProvider,
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700157 peerUpdateFunction,
158 eventExecutor,
159 communicationExecutor,
160 backgroundExecutor,
161 tombstonesDisabled,
162 antiEntropyPeriod,
163 antiEntropyTimeUnit,
Jonathan Hartca335e92015-03-05 10:34:32 -0800164 convergeFaster,
Aaron Kruglikov66cf0b92015-10-26 15:46:54 -0700165 persistent,
166 persistenceService);
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700167 }
168}