Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.store.service; |
| 18 | |
| 19 | import org.onlab.util.KryoNamespace; |
| 20 | import org.onosproject.cluster.NodeId; |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 21 | import org.onosproject.store.Timestamp; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 22 | |
| 23 | import java.util.Collection; |
| 24 | import java.util.concurrent.ExecutorService; |
| 25 | import java.util.concurrent.ScheduledExecutorService; |
| 26 | import java.util.concurrent.TimeUnit; |
| 27 | import 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 | */ |
| 35 | public 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 SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 52 | EventuallyConsistentMapBuilder<K, V> withName(String name); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 53 | |
| 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 SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 66 | EventuallyConsistentMapBuilder<K, V> withSerializer( |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 67 | KryoNamespace.Builder serializerBuilder); |
| 68 | |
| 69 | /** |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 70 | * Sets the function to use for generating timestamps for map updates. |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 71 | * <p> |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 72 | * The client must provide an {@code BiFunction<K, V, Timestamp>} |
| 73 | * which can generate timestamps for a given key. The function is free |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 74 | * 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 Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 84 | * @param timestampProvider provides a new timestamp |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 85 | * @return this EventuallyConsistentMapBuilder |
| 86 | */ |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 87 | EventuallyConsistentMapBuilder<K, V> withTimestampProvider( |
| 88 | BiFunction<K, V, Timestamp> timestampProvider); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 89 | |
| 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 SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 96 | EventuallyConsistentMapBuilder<K, V> withEventExecutor( |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 97 | 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 SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 105 | EventuallyConsistentMapBuilder<K, V> withCommunicationExecutor( |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 106 | 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 SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 114 | EventuallyConsistentMapBuilder<K, V> withBackgroundExecutor( |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 115 | 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 SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 128 | EventuallyConsistentMapBuilder<K, V> withPeerUpdateFunction( |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 129 | 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 SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 141 | EventuallyConsistentMapBuilder<K, V> withTombstonesDisabled(); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 142 | |
| 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 SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 153 | EventuallyConsistentMapBuilder<K, V> withAntiEntropyPeriod( |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 154 | 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 SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 167 | EventuallyConsistentMapBuilder<K, V> withFasterConvergence(); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 168 | |
| 169 | /** |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 170 | * 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 SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 177 | EventuallyConsistentMapBuilder<K, V> withPersistence(); |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 178 | |
| 179 | /** |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 180 | * 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 SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 186 | EventuallyConsistentMap<K, V> build(); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 187 | } |