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