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