blob: 9d6a016f7451e3a5138e902a6a1a9cbe23647fe0 [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
Jonathan Hart6ec029a2015-03-24 17:12:35 -070018import java.util.Collection;
19import java.util.concurrent.ExecutorService;
20import java.util.concurrent.ScheduledExecutorService;
21import java.util.concurrent.TimeUnit;
22import java.util.function.BiFunction;
23
Jordan Halterman28183ee2017-10-17 17:29:10 -070024import org.onlab.util.KryoNamespace;
25import org.onosproject.cluster.ClusterService;
26import org.onosproject.cluster.NodeId;
27import org.onosproject.persistence.PersistenceService;
28import org.onosproject.store.Timestamp;
29import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
30import org.onosproject.store.service.EventuallyConsistentMap;
31import org.onosproject.store.service.EventuallyConsistentMapBuilder;
32
Jonathan Hart6ec029a2015-03-24 17:12:35 -070033import 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> {
Jordan Halterman28183ee2017-10-17 17:29:10 -070041 private final ClusterService clusterService;
42 private final ClusterCommunicationService clusterCommunicator;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070043
44 private String name;
HIGUCHI Yuta163efb52016-05-18 19:24:19 -070045 private KryoNamespace serializer;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070046 private KryoNamespace.Builder serializerBuilder;
47 private ExecutorService eventExecutor;
48 private ExecutorService communicationExecutor;
49 private ScheduledExecutorService backgroundExecutor;
Madan Jampanibcf1a482015-06-24 19:05:56 -070050 private BiFunction<K, V, Timestamp> timestampProvider;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070051 private BiFunction<K, V, Collection<NodeId>> peerUpdateFunction;
52 private boolean tombstonesDisabled = false;
53 private long antiEntropyPeriod = 5;
54 private TimeUnit antiEntropyTimeUnit = TimeUnit.SECONDS;
55 private boolean convergeFaster = false;
Jonathan Hartca335e92015-03-05 10:34:32 -080056 private boolean persistent = false;
Aaron Kruglikov66cf0b92015-10-26 15:46:54 -070057 private boolean persistentMap = false;
58 private final PersistenceService persistenceService;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070059
60 /**
61 * Creates a new eventually consistent map builder.
62 *
63 * @param clusterService cluster service
64 * @param clusterCommunicator cluster communication service
Jian Lidfba7392016-01-22 16:46:58 -080065 * @param persistenceService persistence service
Jonathan Hart6ec029a2015-03-24 17:12:35 -070066 */
Jordan Halterman28183ee2017-10-17 17:29:10 -070067 public EventuallyConsistentMapBuilderImpl(
68 ClusterService clusterService,
69 ClusterCommunicationService clusterCommunicator,
70 PersistenceService persistenceService) {
Aaron Kruglikov66cf0b92015-10-26 15:46:54 -070071 this.persistenceService = persistenceService;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070072 this.clusterService = checkNotNull(clusterService);
73 this.clusterCommunicator = checkNotNull(clusterCommunicator);
74 }
75
76 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -070077 public EventuallyConsistentMapBuilder<K, V> withName(String name) {
Jonathan Hart6ec029a2015-03-24 17:12:35 -070078 this.name = checkNotNull(name);
79 return this;
80 }
81
82 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -070083 public EventuallyConsistentMapBuilder<K, V> withSerializer(
Jonathan Hart6ec029a2015-03-24 17:12:35 -070084 KryoNamespace.Builder serializerBuilder) {
85 this.serializerBuilder = checkNotNull(serializerBuilder);
86 return this;
87 }
88
89 @Override
HIGUCHI Yuta163efb52016-05-18 19:24:19 -070090 public EventuallyConsistentMapBuilder<K, V> withSerializer(KryoNamespace serializer) {
91 this.serializer = checkNotNull(serializer);
92 return this;
93 }
94
95 @Override
Madan Jampanibcf1a482015-06-24 19:05:56 -070096 public EventuallyConsistentMapBuilder<K, V> withTimestampProvider(
97 BiFunction<K, V, Timestamp> timestampProvider) {
98 this.timestampProvider = checkNotNull(timestampProvider);
Jonathan Hart6ec029a2015-03-24 17:12:35 -070099 return this;
100 }
101
102 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -0700103 public EventuallyConsistentMapBuilder<K, V> withEventExecutor(ExecutorService executor) {
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700104 this.eventExecutor = checkNotNull(executor);
105 return this;
106 }
107
108 @Override
109 public EventuallyConsistentMapBuilder<K, V> withCommunicationExecutor(
110 ExecutorService executor) {
111 communicationExecutor = checkNotNull(executor);
112 return this;
113 }
114
115 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -0700116 public EventuallyConsistentMapBuilder<K, V> withBackgroundExecutor(ScheduledExecutorService executor) {
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700117 this.backgroundExecutor = checkNotNull(executor);
118 return this;
119 }
120
121 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -0700122 public EventuallyConsistentMapBuilder<K, V> withPeerUpdateFunction(
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700123 BiFunction<K, V, Collection<NodeId>> peerUpdateFunction) {
124 this.peerUpdateFunction = checkNotNull(peerUpdateFunction);
125 return this;
126 }
127
128 @Override
129 public EventuallyConsistentMapBuilder<K, V> withTombstonesDisabled() {
130 tombstonesDisabled = true;
131 return this;
132 }
133
134 @Override
135 public EventuallyConsistentMapBuilder<K, V> withAntiEntropyPeriod(long period, TimeUnit unit) {
136 checkArgument(period > 0, "anti-entropy period must be greater than 0");
137 antiEntropyPeriod = period;
138 antiEntropyTimeUnit = checkNotNull(unit);
139 return this;
140 }
141
142 @Override
143 public EventuallyConsistentMapBuilder<K, V> withFasterConvergence() {
144 convergeFaster = true;
145 return this;
146 }
147
148 @Override
Jonathan Hartca335e92015-03-05 10:34:32 -0800149 public EventuallyConsistentMapBuilder<K, V> withPersistence() {
Aaron Kruglikov66cf0b92015-10-26 15:46:54 -0700150 checkNotNull(this.persistenceService);
Jonathan Hartca335e92015-03-05 10:34:32 -0800151 persistent = true;
152 return this;
153 }
154
155 @Override
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700156 public EventuallyConsistentMap<K, V> build() {
157 checkNotNull(name, "name is a mandatory parameter");
Madan Jampanibcf1a482015-06-24 19:05:56 -0700158 checkNotNull(timestampProvider, "timestampProvider is a mandatory parameter");
HIGUCHI Yuta163efb52016-05-18 19:24:19 -0700159 if (serializer == null && serializerBuilder != null) {
160 serializer = serializerBuilder.build(name);
161 }
162 checkNotNull(serializer, "serializer is a mandatory parameter");
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700163
164 return new EventuallyConsistentMapImpl<>(name,
165 clusterService,
166 clusterCommunicator,
HIGUCHI Yuta163efb52016-05-18 19:24:19 -0700167 serializer,
Madan Jampanibcf1a482015-06-24 19:05:56 -0700168 timestampProvider,
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700169 peerUpdateFunction,
170 eventExecutor,
171 communicationExecutor,
172 backgroundExecutor,
173 tombstonesDisabled,
174 antiEntropyPeriod,
175 antiEntropyTimeUnit,
Jonathan Hartca335e92015-03-05 10:34:32 -0800176 convergeFaster,
Aaron Kruglikov66cf0b92015-10-26 15:46:54 -0700177 persistent,
178 persistenceService);
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700179 }
180}