Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | b6d3167 | 2018-07-27 17:03:46 -0700 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 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 | */ |
Thomas Vachuska | b6d3167 | 2018-07-27 17:03:46 -0700 | [diff] [blame] | 16 | package org.onosproject.store.atomix.primitives.impl; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 17 | |
slowr | 878625f | 2017-10-24 14:53:49 -0700 | [diff] [blame] | 18 | import java.util.Collection; |
| 19 | import java.util.List; |
| 20 | import java.util.concurrent.ExecutorService; |
| 21 | import java.util.concurrent.ScheduledExecutorService; |
| 22 | import java.util.concurrent.TimeUnit; |
| 23 | import java.util.function.BiFunction; |
| 24 | import java.util.function.Supplier; |
| 25 | |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 26 | import org.onlab.util.KryoNamespace; |
| 27 | import org.onosproject.cluster.NodeId; |
| 28 | import org.onosproject.persistence.PersistenceService; |
| 29 | import org.onosproject.store.Timestamp; |
| 30 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 31 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 32 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; |
| 33 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 34 | import static com.google.common.base.Preconditions.checkArgument; |
| 35 | import static com.google.common.base.Preconditions.checkNotNull; |
| 36 | |
| 37 | /** |
| 38 | * Eventually consistent map builder. |
| 39 | */ |
| 40 | public class EventuallyConsistentMapBuilderImpl<K, V> |
| 41 | implements EventuallyConsistentMapBuilder<K, V> { |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 42 | private final ClusterCommunicationService clusterCommunicator; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 43 | |
slowr | 878625f | 2017-10-24 14:53:49 -0700 | [diff] [blame] | 44 | private NodeId localNodeId; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 45 | private String name; |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 46 | private KryoNamespace serializer; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 47 | private KryoNamespace.Builder serializerBuilder; |
| 48 | private ExecutorService eventExecutor; |
| 49 | private ExecutorService communicationExecutor; |
| 50 | private ScheduledExecutorService backgroundExecutor; |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 51 | private BiFunction<K, V, Timestamp> timestampProvider; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 52 | private BiFunction<K, V, Collection<NodeId>> peerUpdateFunction; |
| 53 | private boolean tombstonesDisabled = false; |
| 54 | private long antiEntropyPeriod = 5; |
| 55 | private TimeUnit antiEntropyTimeUnit = TimeUnit.SECONDS; |
| 56 | private boolean convergeFaster = false; |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 57 | private boolean persistent = false; |
Aaron Kruglikov | 66cf0b9 | 2015-10-26 15:46:54 -0700 | [diff] [blame] | 58 | private boolean persistentMap = false; |
| 59 | private final PersistenceService persistenceService; |
slowr | 878625f | 2017-10-24 14:53:49 -0700 | [diff] [blame] | 60 | private Supplier<List<NodeId>> peersSupplier; |
| 61 | private Supplier<List<NodeId>> bootstrapPeersSupplier; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Creates a new eventually consistent map builder. |
slowr | 878625f | 2017-10-24 14:53:49 -0700 | [diff] [blame] | 65 | * @param localNodeId local node id |
| 66 | * @param clusterCommunicator cluster communication service |
| 67 | * @param persistenceService persistence service |
| 68 | * @param peersSupplier supplier for peers |
| 69 | * @param bootstrapPeersSupplier supplier for peers for bootstrap |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 70 | */ |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 71 | public EventuallyConsistentMapBuilderImpl( |
slowr | 878625f | 2017-10-24 14:53:49 -0700 | [diff] [blame] | 72 | NodeId localNodeId, |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 73 | ClusterCommunicationService clusterCommunicator, |
slowr | 878625f | 2017-10-24 14:53:49 -0700 | [diff] [blame] | 74 | PersistenceService persistenceService, |
| 75 | Supplier<List<NodeId>> peersSupplier, |
| 76 | Supplier<List<NodeId>> bootstrapPeersSupplier |
| 77 | ) { |
| 78 | this.localNodeId = localNodeId; |
Aaron Kruglikov | 66cf0b9 | 2015-10-26 15:46:54 -0700 | [diff] [blame] | 79 | this.persistenceService = persistenceService; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 80 | this.clusterCommunicator = checkNotNull(clusterCommunicator); |
slowr | 878625f | 2017-10-24 14:53:49 -0700 | [diff] [blame] | 81 | this.peersSupplier = peersSupplier; |
| 82 | this.bootstrapPeersSupplier = bootstrapPeersSupplier; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 86 | public EventuallyConsistentMapBuilder<K, V> withName(String name) { |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 87 | this.name = checkNotNull(name); |
| 88 | return this; |
| 89 | } |
| 90 | |
| 91 | @Override |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 92 | public EventuallyConsistentMapBuilder<K, V> withSerializer( |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 93 | KryoNamespace.Builder serializerBuilder) { |
| 94 | this.serializerBuilder = checkNotNull(serializerBuilder); |
| 95 | return this; |
| 96 | } |
| 97 | |
| 98 | @Override |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 99 | public EventuallyConsistentMapBuilder<K, V> withSerializer(KryoNamespace serializer) { |
| 100 | this.serializer = checkNotNull(serializer); |
| 101 | return this; |
| 102 | } |
| 103 | |
| 104 | @Override |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 105 | public EventuallyConsistentMapBuilder<K, V> withTimestampProvider( |
| 106 | BiFunction<K, V, Timestamp> timestampProvider) { |
| 107 | this.timestampProvider = checkNotNull(timestampProvider); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 108 | return this; |
| 109 | } |
| 110 | |
| 111 | @Override |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 112 | public EventuallyConsistentMapBuilder<K, V> withEventExecutor(ExecutorService executor) { |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 113 | this.eventExecutor = checkNotNull(executor); |
| 114 | return this; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public EventuallyConsistentMapBuilder<K, V> withCommunicationExecutor( |
| 119 | ExecutorService executor) { |
| 120 | communicationExecutor = checkNotNull(executor); |
| 121 | return this; |
| 122 | } |
| 123 | |
| 124 | @Override |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 125 | public EventuallyConsistentMapBuilder<K, V> withBackgroundExecutor(ScheduledExecutorService executor) { |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 126 | this.backgroundExecutor = checkNotNull(executor); |
| 127 | return this; |
| 128 | } |
| 129 | |
| 130 | @Override |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 131 | public EventuallyConsistentMapBuilder<K, V> withPeerUpdateFunction( |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 132 | BiFunction<K, V, Collection<NodeId>> peerUpdateFunction) { |
| 133 | this.peerUpdateFunction = checkNotNull(peerUpdateFunction); |
| 134 | return this; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public EventuallyConsistentMapBuilder<K, V> withTombstonesDisabled() { |
| 139 | tombstonesDisabled = true; |
| 140 | return this; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public EventuallyConsistentMapBuilder<K, V> withAntiEntropyPeriod(long period, TimeUnit unit) { |
| 145 | checkArgument(period > 0, "anti-entropy period must be greater than 0"); |
| 146 | antiEntropyPeriod = period; |
| 147 | antiEntropyTimeUnit = checkNotNull(unit); |
| 148 | return this; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public EventuallyConsistentMapBuilder<K, V> withFasterConvergence() { |
| 153 | convergeFaster = true; |
| 154 | return this; |
| 155 | } |
| 156 | |
| 157 | @Override |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 158 | public EventuallyConsistentMapBuilder<K, V> withPersistence() { |
Aaron Kruglikov | 66cf0b9 | 2015-10-26 15:46:54 -0700 | [diff] [blame] | 159 | checkNotNull(this.persistenceService); |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 160 | persistent = true; |
| 161 | return this; |
| 162 | } |
| 163 | |
| 164 | @Override |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 165 | public EventuallyConsistentMap<K, V> build() { |
| 166 | checkNotNull(name, "name is a mandatory parameter"); |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 167 | checkNotNull(timestampProvider, "timestampProvider is a mandatory parameter"); |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 168 | if (serializer == null && serializerBuilder != null) { |
| 169 | serializer = serializerBuilder.build(name); |
| 170 | } |
| 171 | checkNotNull(serializer, "serializer is a mandatory parameter"); |
slowr | 878625f | 2017-10-24 14:53:49 -0700 | [diff] [blame] | 172 | checkNotNull(localNodeId, "local node id cannot be null"); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 173 | |
slowr | 878625f | 2017-10-24 14:53:49 -0700 | [diff] [blame] | 174 | return new EventuallyConsistentMapImpl<>( |
| 175 | localNodeId, |
| 176 | name, |
| 177 | clusterCommunicator, |
| 178 | serializer, |
| 179 | timestampProvider, |
| 180 | peerUpdateFunction, |
| 181 | eventExecutor, |
| 182 | communicationExecutor, |
| 183 | backgroundExecutor, |
| 184 | tombstonesDisabled, |
| 185 | antiEntropyPeriod, |
| 186 | antiEntropyTimeUnit, |
| 187 | convergeFaster, |
| 188 | persistent, |
| 189 | persistenceService, |
| 190 | peersSupplier, |
| 191 | bootstrapPeersSupplier |
| 192 | ); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 193 | } |
| 194 | } |