blob: cf2c029044129d2331968c6a1969646d71d0a194 [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 /**
Madan Jampanibcf1a482015-06-24 19:05:56 -070070 * Sets the function to use for generating timestamps for map updates.
Jonathan Hart6ec029a2015-03-24 17:12:35 -070071 * <p>
Madan Jampanibcf1a482015-06-24 19:05:56 -070072 * The client must provide an {@code BiFunction<K, V, Timestamp>}
73 * which can generate timestamps for a given key. The function is free
Jonathan Hart6ec029a2015-03-24 17:12:35 -070074 * to generate timestamps however it wishes, however these timestamps will
75 * be used to serialize updates to the map so they must be strict enough
76 * to ensure updates are properly ordered for the use case (i.e. in some
77 * cases wallclock time will suffice, whereas in other cases logical time
78 * will be necessary).
79 * </p>
80 * <p>
81 * Note: This is a mandatory parameter.
82 * </p>
83 *
Madan Jampanibcf1a482015-06-24 19:05:56 -070084 * @param timestampProvider provides a new timestamp
Jonathan Hart6ec029a2015-03-24 17:12:35 -070085 * @return this EventuallyConsistentMapBuilder
86 */
Madan Jampanibcf1a482015-06-24 19:05:56 -070087 EventuallyConsistentMapBuilder<K, V> withTimestampProvider(
88 BiFunction<K, V, Timestamp> timestampProvider);
Jonathan Hart6ec029a2015-03-24 17:12:35 -070089
90 /**
91 * Sets the executor to use for processing events coming in from peers.
92 *
93 * @param executor event executor
94 * @return this EventuallyConsistentMapBuilder
95 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070096 EventuallyConsistentMapBuilder<K, V> withEventExecutor(
Jonathan Hart6ec029a2015-03-24 17:12:35 -070097 ExecutorService executor);
98
99 /**
100 * Sets the executor to use for sending events to peers.
101 *
102 * @param executor event executor
103 * @return this EventuallyConsistentMapBuilder
104 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700105 EventuallyConsistentMapBuilder<K, V> withCommunicationExecutor(
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700106 ExecutorService executor);
107
108 /**
109 * Sets the executor to use for background anti-entropy tasks.
110 *
111 * @param executor event executor
112 * @return this EventuallyConsistentMapBuilder
113 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700114 EventuallyConsistentMapBuilder<K, V> withBackgroundExecutor(
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700115 ScheduledExecutorService executor);
116
117 /**
118 * Sets a function that can determine which peers to replicate updates to.
119 * <p>
120 * The default function replicates to all nodes.
121 * </p>
122 *
123 * @param peerUpdateFunction function that takes a K, V input and returns
124 * a collection of NodeIds to replicate the event
125 * to
126 * @return this EventuallyConsistentMapBuilder
127 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700128 EventuallyConsistentMapBuilder<K, V> withPeerUpdateFunction(
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700129 BiFunction<K, V, Collection<NodeId>> peerUpdateFunction);
130
131 /**
132 * Prevents this map from writing tombstones of items that have been
133 * removed. This may result in zombie items reappearing after they have
134 * been removed.
135 * <p>
136 * The default behavior is tombstones are enabled.
137 * </p>
138 *
139 * @return this EventuallyConsistentMapBuilder
140 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700141 EventuallyConsistentMapBuilder<K, V> withTombstonesDisabled();
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700142
143 /**
144 * Configures how often to run the anti-entropy background task.
145 * <p>
146 * The default anti-entropy period is 5 seconds.
147 * </p>
148 *
149 * @param period anti-entropy period
150 * @param unit time unit for the period
151 * @return this EventuallyConsistentMapBuilder
152 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700153 EventuallyConsistentMapBuilder<K, V> withAntiEntropyPeriod(
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700154 long period, TimeUnit unit);
155
156 /**
157 * Configure anti-entropy to converge faster at the cost of doing more work
158 * for each anti-entropy cycle. Suited to maps with low update rate where
159 * convergence time is more important than throughput.
160 * <p>
161 * The default behavior is to do less anti-entropy work at the cost of
162 * slower convergence.
163 * </p>
164 *
165 * @return this EventuallyConsistentMapBuilder
166 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700167 EventuallyConsistentMapBuilder<K, V> withFasterConvergence();
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700168
169 /**
Jonathan Hartca335e92015-03-05 10:34:32 -0800170 * Configure the map to persist data to disk.
171 * <p>
172 * The default behavior is no persistence
173 * </p>
174 *
175 * @return this EventuallyConsistentMapBuilder
176 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700177 EventuallyConsistentMapBuilder<K, V> withPersistence();
Jonathan Hartca335e92015-03-05 10:34:32 -0800178
179 /**
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700180 * Builds an eventually consistent map based on the configuration options
181 * supplied to this builder.
182 *
183 * @return new eventually consistent map
184 * @throws java.lang.RuntimeException if a mandatory parameter is missing
185 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700186 EventuallyConsistentMap<K, V> build();
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700187}