blob: 75ac27d05b38bde148e031f2cbf58e58de75a6b8 [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 */
16
17package org.onosproject.store.service;
18
19import org.onlab.util.KryoNamespace;
20import org.onosproject.cluster.NodeId;
21
22import java.util.Collection;
23import java.util.concurrent.ExecutorService;
24import java.util.concurrent.ScheduledExecutorService;
25import java.util.concurrent.TimeUnit;
26import java.util.function.BiFunction;
27
28/**
29 * Builder for eventually consistent maps.
30 *
31 * @param <K> type for map keys
32 * @param <V> type for map values
33 */
34public interface EventuallyConsistentMapBuilder<K, V> {
35
36 /**
37 * Sets the name of the map.
38 * <p>
39 * Each map is identified by a string map name. EventuallyConsistentMapImpl
40 * objects in different JVMs that use the same map name will form a
41 * distributed map across JVMs (provided the cluster service is aware of
42 * both nodes).
43 * </p>
44 * <p>
45 * Note: This is a mandatory parameter.
46 * </p>
47 *
48 * @param name name of the map
49 * @return this EventuallyConsistentMapBuilder
50 */
51 public EventuallyConsistentMapBuilder<K, V> withName(String name);
52
53 /**
54 * Sets a serializer builder that can be used to create a serializer that
55 * can serialize both the keys and values put into the map. The serializer
56 * builder should be pre-populated with any classes that will be put into
57 * the map.
58 * <p>
59 * Note: This is a mandatory parameter.
60 * </p>
61 *
62 * @param serializerBuilder serializer builder
63 * @return this EventuallyConsistentMapBuilder
64 */
65 public EventuallyConsistentMapBuilder<K, V> withSerializer(
66 KryoNamespace.Builder serializerBuilder);
67
68 /**
69 * Sets the clock service to use for generating timestamps for map updates.
70 * <p>
71 * The client must provide an {@link org.onosproject.store.service.ClockService}
72 * which can generate timestamps for a given key. The clock service is free
73 * to generate timestamps however it wishes, however these timestamps will
74 * be used to serialize updates to the map so they must be strict enough
75 * to ensure updates are properly ordered for the use case (i.e. in some
76 * cases wallclock time will suffice, whereas in other cases logical time
77 * will be necessary).
78 * </p>
79 * <p>
80 * Note: This is a mandatory parameter.
81 * </p>
82 *
83 * @param clockService clock service
84 * @return this EventuallyConsistentMapBuilder
85 */
86 public EventuallyConsistentMapBuilder<K, V> withClockService(
87 ClockService<K, V> clockService);
88
89 /**
90 * Sets the executor to use for processing events coming in from peers.
91 *
92 * @param executor event executor
93 * @return this EventuallyConsistentMapBuilder
94 */
95 public EventuallyConsistentMapBuilder<K, V> withEventExecutor(
96 ExecutorService executor);
97
98 /**
99 * Sets the executor to use for sending events to peers.
100 *
101 * @param executor event executor
102 * @return this EventuallyConsistentMapBuilder
103 */
104 public EventuallyConsistentMapBuilder<K, V> withCommunicationExecutor(
105 ExecutorService executor);
106
107 /**
108 * Sets the executor to use for background anti-entropy tasks.
109 *
110 * @param executor event executor
111 * @return this EventuallyConsistentMapBuilder
112 */
113 public EventuallyConsistentMapBuilder<K, V> withBackgroundExecutor(
114 ScheduledExecutorService executor);
115
116 /**
117 * Sets a function that can determine which peers to replicate updates to.
118 * <p>
119 * The default function replicates to all nodes.
120 * </p>
121 *
122 * @param peerUpdateFunction function that takes a K, V input and returns
123 * a collection of NodeIds to replicate the event
124 * to
125 * @return this EventuallyConsistentMapBuilder
126 */
127 public EventuallyConsistentMapBuilder<K, V> withPeerUpdateFunction(
128 BiFunction<K, V, Collection<NodeId>> peerUpdateFunction);
129
130 /**
131 * Prevents this map from writing tombstones of items that have been
132 * removed. This may result in zombie items reappearing after they have
133 * been removed.
134 * <p>
135 * The default behavior is tombstones are enabled.
136 * </p>
137 *
138 * @return this EventuallyConsistentMapBuilder
139 */
140 public EventuallyConsistentMapBuilder<K, V> withTombstonesDisabled();
141
142 /**
143 * Configures how often to run the anti-entropy background task.
144 * <p>
145 * The default anti-entropy period is 5 seconds.
146 * </p>
147 *
148 * @param period anti-entropy period
149 * @param unit time unit for the period
150 * @return this EventuallyConsistentMapBuilder
151 */
152 public EventuallyConsistentMapBuilder<K, V> withAntiEntropyPeriod(
153 long period, TimeUnit unit);
154
155 /**
156 * Configure anti-entropy to converge faster at the cost of doing more work
157 * for each anti-entropy cycle. Suited to maps with low update rate where
158 * convergence time is more important than throughput.
159 * <p>
160 * The default behavior is to do less anti-entropy work at the cost of
161 * slower convergence.
162 * </p>
163 *
164 * @return this EventuallyConsistentMapBuilder
165 */
166 public EventuallyConsistentMapBuilder<K, V> withFasterConvergence();
167
168 /**
Jonathan Hartca335e92015-03-05 10:34:32 -0800169 * Configure the map to persist data to disk.
170 * <p>
171 * The default behavior is no persistence
172 * </p>
173 *
174 * @return this EventuallyConsistentMapBuilder
175 */
176 public EventuallyConsistentMapBuilder<K, V> withPersistence();
177
178 /**
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700179 * Builds an eventually consistent map based on the configuration options
180 * supplied to this builder.
181 *
182 * @return new eventually consistent map
183 * @throws java.lang.RuntimeException if a mandatory parameter is missing
184 */
185 public EventuallyConsistentMap<K, V> build();
186}