blob: 1bfaaed9558b219260f4cc3cc9b93409d394c59a [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
18import org.onlab.util.KryoNamespace;
Jordan Halterman980a8c12017-09-22 18:01:19 -070019import org.onosproject.cluster.MembershipService;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070020import 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;
Jordan Halterman980a8c12017-09-22 18:01:19 -070023import org.onosproject.store.cluster.messaging.ClusterCommunicator;
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> {
Jordan Halterman980a8c12017-09-22 18:01:19 -070041 private final MembershipService clusterService;
42 private final ClusterCommunicator 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 Halterman980a8c12017-09-22 18:01:19 -070067 public EventuallyConsistentMapBuilderImpl(MembershipService clusterService,
68 ClusterCommunicator clusterCommunicator,
Aaron Kruglikov66cf0b92015-10-26 15:46:54 -070069 PersistenceService persistenceService) {
70 this.persistenceService = persistenceService;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070071 this.clusterService = checkNotNull(clusterService);
72 this.clusterCommunicator = checkNotNull(clusterCommunicator);
73 }
74
75 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -070076 public EventuallyConsistentMapBuilder<K, V> withName(String name) {
Jonathan Hart6ec029a2015-03-24 17:12:35 -070077 this.name = checkNotNull(name);
78 return this;
79 }
80
81 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -070082 public EventuallyConsistentMapBuilder<K, V> withSerializer(
Jonathan Hart6ec029a2015-03-24 17:12:35 -070083 KryoNamespace.Builder serializerBuilder) {
84 this.serializerBuilder = checkNotNull(serializerBuilder);
85 return this;
86 }
87
88 @Override
HIGUCHI Yuta163efb52016-05-18 19:24:19 -070089 public EventuallyConsistentMapBuilder<K, V> withSerializer(KryoNamespace serializer) {
90 this.serializer = checkNotNull(serializer);
91 return this;
92 }
93
94 @Override
Madan Jampanibcf1a482015-06-24 19:05:56 -070095 public EventuallyConsistentMapBuilder<K, V> withTimestampProvider(
96 BiFunction<K, V, Timestamp> timestampProvider) {
97 this.timestampProvider = checkNotNull(timestampProvider);
Jonathan Hart6ec029a2015-03-24 17:12:35 -070098 return this;
99 }
100
101 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -0700102 public EventuallyConsistentMapBuilder<K, V> withEventExecutor(ExecutorService executor) {
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700103 this.eventExecutor = checkNotNull(executor);
104 return this;
105 }
106
107 @Override
108 public EventuallyConsistentMapBuilder<K, V> withCommunicationExecutor(
109 ExecutorService executor) {
110 communicationExecutor = checkNotNull(executor);
111 return this;
112 }
113
114 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -0700115 public EventuallyConsistentMapBuilder<K, V> withBackgroundExecutor(ScheduledExecutorService executor) {
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700116 this.backgroundExecutor = checkNotNull(executor);
117 return this;
118 }
119
120 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -0700121 public EventuallyConsistentMapBuilder<K, V> withPeerUpdateFunction(
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700122 BiFunction<K, V, Collection<NodeId>> peerUpdateFunction) {
123 this.peerUpdateFunction = checkNotNull(peerUpdateFunction);
124 return this;
125 }
126
127 @Override
128 public EventuallyConsistentMapBuilder<K, V> withTombstonesDisabled() {
129 tombstonesDisabled = true;
130 return this;
131 }
132
133 @Override
134 public EventuallyConsistentMapBuilder<K, V> withAntiEntropyPeriod(long period, TimeUnit unit) {
135 checkArgument(period > 0, "anti-entropy period must be greater than 0");
136 antiEntropyPeriod = period;
137 antiEntropyTimeUnit = checkNotNull(unit);
138 return this;
139 }
140
141 @Override
142 public EventuallyConsistentMapBuilder<K, V> withFasterConvergence() {
143 convergeFaster = true;
144 return this;
145 }
146
147 @Override
Jonathan Hartca335e92015-03-05 10:34:32 -0800148 public EventuallyConsistentMapBuilder<K, V> withPersistence() {
Aaron Kruglikov66cf0b92015-10-26 15:46:54 -0700149 checkNotNull(this.persistenceService);
Jonathan Hartca335e92015-03-05 10:34:32 -0800150 persistent = true;
151 return this;
152 }
153
154 @Override
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700155 public EventuallyConsistentMap<K, V> build() {
156 checkNotNull(name, "name is a mandatory parameter");
Madan Jampanibcf1a482015-06-24 19:05:56 -0700157 checkNotNull(timestampProvider, "timestampProvider is a mandatory parameter");
HIGUCHI Yuta163efb52016-05-18 19:24:19 -0700158 if (serializer == null && serializerBuilder != null) {
159 serializer = serializerBuilder.build(name);
160 }
161 checkNotNull(serializer, "serializer is a mandatory parameter");
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700162
163 return new EventuallyConsistentMapImpl<>(name,
164 clusterService,
165 clusterCommunicator,
HIGUCHI Yuta163efb52016-05-18 19:24:19 -0700166 serializer,
Madan Jampanibcf1a482015-06-24 19:05:56 -0700167 timestampProvider,
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700168 peerUpdateFunction,
169 eventExecutor,
170 communicationExecutor,
171 backgroundExecutor,
172 tombstonesDisabled,
173 antiEntropyPeriod,
174 antiEntropyTimeUnit,
Jonathan Hartca335e92015-03-05 10:34:32 -0800175 convergeFaster,
Aaron Kruglikov66cf0b92015-10-26 15:46:54 -0700176 persistent,
177 persistenceService);
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700178 }
179}