blob: ff9aff55224a135227f69fe24861d2a7f5cb1a74 [file] [log] [blame]
Jonathan Hart6ec029a2015-03-24 17:12:35 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-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 */
16
17package org.onosproject.store.service;
18
19import org.onlab.util.KryoNamespace;
20import org.onosproject.cluster.NodeId;
Madan Jampanibcf1a482015-06-24 19:05:56 -070021import org.onosproject.store.Timestamp;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070022
23import java.util.Collection;
24import java.util.concurrent.ExecutorService;
25import java.util.concurrent.ScheduledExecutorService;
26import java.util.concurrent.TimeUnit;
27import java.util.function.BiFunction;
28
29/**
30 * Builder for eventually consistent maps.
31 *
32 * @param <K> type for map keys
33 * @param <V> type for map values
34 */
35public interface EventuallyConsistentMapBuilder<K, V> {
36
37 /**
38 * Sets the name of the map.
39 * <p>
40 * Each map is identified by a string map name. EventuallyConsistentMapImpl
41 * objects in different JVMs that use the same map name will form a
42 * distributed map across JVMs (provided the cluster service is aware of
43 * both nodes).
44 * </p>
45 * <p>
46 * Note: This is a mandatory parameter.
47 * </p>
48 *
49 * @param name name of the map
50 * @return this EventuallyConsistentMapBuilder
51 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070052 EventuallyConsistentMapBuilder<K, V> withName(String name);
Jonathan Hart6ec029a2015-03-24 17:12:35 -070053
54 /**
55 * Sets a serializer builder that can be used to create a serializer that
56 * can serialize both the keys and values put into the map. The serializer
57 * builder should be pre-populated with any classes that will be put into
58 * the map.
59 * <p>
60 * Note: This is a mandatory parameter.
61 * </p>
62 *
63 * @param serializerBuilder serializer builder
64 * @return this EventuallyConsistentMapBuilder
65 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070066 EventuallyConsistentMapBuilder<K, V> withSerializer(
Jonathan Hart6ec029a2015-03-24 17:12:35 -070067 KryoNamespace.Builder serializerBuilder);
68
69 /**
HIGUCHI Yuta163efb52016-05-18 19:24:19 -070070 * Sets a serializer that can be used to create a serializer that
71 * can serialize both the keys and values put into the map. The serializer
72 * builder should be pre-populated with any classes that will be put into
73 * the map.
74 * <p>
75 * Note: This is a mandatory parameter.
76 * </p>
77 *
78 * @param serializer serializer
79 * @return this EventuallyConsistentMapBuilder
80 */
81 EventuallyConsistentMapBuilder<K, V> withSerializer(KryoNamespace serializer);
82
83 /**
Madan Jampanibcf1a482015-06-24 19:05:56 -070084 * Sets the function to use for generating timestamps for map updates.
Jonathan Hart6ec029a2015-03-24 17:12:35 -070085 * <p>
Madan Jampanibcf1a482015-06-24 19:05:56 -070086 * The client must provide an {@code BiFunction<K, V, Timestamp>}
87 * which can generate timestamps for a given key. The function is free
Jonathan Hart6ec029a2015-03-24 17:12:35 -070088 * to generate timestamps however it wishes, however these timestamps will
89 * be used to serialize updates to the map so they must be strict enough
90 * to ensure updates are properly ordered for the use case (i.e. in some
91 * cases wallclock time will suffice, whereas in other cases logical time
92 * will be necessary).
93 * </p>
94 * <p>
95 * Note: This is a mandatory parameter.
96 * </p>
97 *
Madan Jampanibcf1a482015-06-24 19:05:56 -070098 * @param timestampProvider provides a new timestamp
Jonathan Hart6ec029a2015-03-24 17:12:35 -070099 * @return this EventuallyConsistentMapBuilder
100 */
Madan Jampanibcf1a482015-06-24 19:05:56 -0700101 EventuallyConsistentMapBuilder<K, V> withTimestampProvider(
102 BiFunction<K, V, Timestamp> timestampProvider);
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700103
104 /**
105 * Sets the executor to use for processing events coming in from peers.
106 *
107 * @param executor event executor
108 * @return this EventuallyConsistentMapBuilder
109 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700110 EventuallyConsistentMapBuilder<K, V> withEventExecutor(
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700111 ExecutorService executor);
112
113 /**
114 * Sets the executor to use for sending events to peers.
115 *
116 * @param executor event executor
117 * @return this EventuallyConsistentMapBuilder
118 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700119 EventuallyConsistentMapBuilder<K, V> withCommunicationExecutor(
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700120 ExecutorService executor);
121
122 /**
123 * Sets the executor to use for background anti-entropy tasks.
124 *
125 * @param executor event executor
126 * @return this EventuallyConsistentMapBuilder
127 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700128 EventuallyConsistentMapBuilder<K, V> withBackgroundExecutor(
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700129 ScheduledExecutorService executor);
130
131 /**
132 * Sets a function that can determine which peers to replicate updates to.
133 * <p>
134 * The default function replicates to all nodes.
135 * </p>
136 *
137 * @param peerUpdateFunction function that takes a K, V input and returns
138 * a collection of NodeIds to replicate the event
139 * to
140 * @return this EventuallyConsistentMapBuilder
141 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700142 EventuallyConsistentMapBuilder<K, V> withPeerUpdateFunction(
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700143 BiFunction<K, V, Collection<NodeId>> peerUpdateFunction);
144
145 /**
146 * Prevents this map from writing tombstones of items that have been
147 * removed. This may result in zombie items reappearing after they have
148 * been removed.
149 * <p>
150 * The default behavior is tombstones are enabled.
151 * </p>
152 *
153 * @return this EventuallyConsistentMapBuilder
154 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700155 EventuallyConsistentMapBuilder<K, V> withTombstonesDisabled();
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700156
157 /**
158 * Configures how often to run the anti-entropy background task.
159 * <p>
160 * The default anti-entropy period is 5 seconds.
161 * </p>
162 *
163 * @param period anti-entropy period
164 * @param unit time unit for the period
165 * @return this EventuallyConsistentMapBuilder
166 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700167 EventuallyConsistentMapBuilder<K, V> withAntiEntropyPeriod(
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700168 long period, TimeUnit unit);
169
170 /**
171 * Configure anti-entropy to converge faster at the cost of doing more work
172 * for each anti-entropy cycle. Suited to maps with low update rate where
173 * convergence time is more important than throughput.
174 * <p>
175 * The default behavior is to do less anti-entropy work at the cost of
176 * slower convergence.
177 * </p>
178 *
179 * @return this EventuallyConsistentMapBuilder
180 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700181 EventuallyConsistentMapBuilder<K, V> withFasterConvergence();
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700182
183 /**
Jonathan Hartca335e92015-03-05 10:34:32 -0800184 * Configure the map to persist data to disk.
185 * <p>
186 * The default behavior is no persistence
187 * </p>
188 *
189 * @return this EventuallyConsistentMapBuilder
190 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700191 EventuallyConsistentMapBuilder<K, V> withPersistence();
Jonathan Hartca335e92015-03-05 10:34:32 -0800192
193 /**
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700194 * Builds an eventually consistent map based on the configuration options
195 * supplied to this builder.
196 *
197 * @return new eventually consistent map
198 * @throws java.lang.RuntimeException if a mandatory parameter is missing
199 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700200 EventuallyConsistentMap<K, V> build();
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700201}