Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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 | */ |
Madan Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 16 | package org.onosproject.store.primitives.impl; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 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; |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 45 | private KryoNamespace serializer; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 46 | private KryoNamespace.Builder serializerBuilder; |
| 47 | private ExecutorService eventExecutor; |
| 48 | private ExecutorService communicationExecutor; |
| 49 | private ScheduledExecutorService backgroundExecutor; |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 50 | private BiFunction<K, V, Timestamp> timestampProvider; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 51 | private BiFunction<K, V, Collection<NodeId>> peerUpdateFunction; |
| 52 | private boolean tombstonesDisabled = false; |
| 53 | private long antiEntropyPeriod = 5; |
| 54 | private TimeUnit antiEntropyTimeUnit = TimeUnit.SECONDS; |
| 55 | private boolean convergeFaster = false; |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 56 | private boolean persistent = false; |
Aaron Kruglikov | 66cf0b9 | 2015-10-26 15:46:54 -0700 | [diff] [blame] | 57 | private boolean persistentMap = false; |
| 58 | private final PersistenceService persistenceService; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Creates a new eventually consistent map builder. |
| 62 | * |
| 63 | * @param clusterService cluster service |
| 64 | * @param clusterCommunicator cluster communication service |
Jian Li | dfba739 | 2016-01-22 16:46:58 -0800 | [diff] [blame] | 65 | * @param persistenceService persistence service |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 66 | */ |
| 67 | public EventuallyConsistentMapBuilderImpl(ClusterService clusterService, |
Aaron Kruglikov | 66cf0b9 | 2015-10-26 15:46:54 -0700 | [diff] [blame] | 68 | ClusterCommunicationService clusterCommunicator, |
| 69 | PersistenceService persistenceService) { |
| 70 | this.persistenceService = persistenceService; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 71 | this.clusterService = checkNotNull(clusterService); |
| 72 | this.clusterCommunicator = checkNotNull(clusterCommunicator); |
| 73 | } |
| 74 | |
| 75 | @Override |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 76 | public EventuallyConsistentMapBuilder<K, V> withName(String name) { |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 77 | this.name = checkNotNull(name); |
| 78 | return this; |
| 79 | } |
| 80 | |
| 81 | @Override |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 82 | public EventuallyConsistentMapBuilder<K, V> withSerializer( |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 83 | KryoNamespace.Builder serializerBuilder) { |
| 84 | this.serializerBuilder = checkNotNull(serializerBuilder); |
| 85 | return this; |
| 86 | } |
| 87 | |
| 88 | @Override |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 89 | public EventuallyConsistentMapBuilder<K, V> withSerializer(KryoNamespace serializer) { |
| 90 | this.serializer = checkNotNull(serializer); |
| 91 | return this; |
| 92 | } |
| 93 | |
| 94 | @Override |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 95 | public EventuallyConsistentMapBuilder<K, V> withTimestampProvider( |
| 96 | BiFunction<K, V, Timestamp> timestampProvider) { |
| 97 | this.timestampProvider = checkNotNull(timestampProvider); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 98 | return this; |
| 99 | } |
| 100 | |
| 101 | @Override |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 102 | public EventuallyConsistentMapBuilder<K, V> withEventExecutor(ExecutorService executor) { |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 103 | this.eventExecutor = checkNotNull(executor); |
| 104 | return this; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public EventuallyConsistentMapBuilder<K, V> withCommunicationExecutor( |
| 109 | ExecutorService executor) { |
| 110 | communicationExecutor = checkNotNull(executor); |
| 111 | return this; |
| 112 | } |
| 113 | |
| 114 | @Override |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 115 | public EventuallyConsistentMapBuilder<K, V> withBackgroundExecutor(ScheduledExecutorService executor) { |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 116 | this.backgroundExecutor = checkNotNull(executor); |
| 117 | return this; |
| 118 | } |
| 119 | |
| 120 | @Override |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 121 | public EventuallyConsistentMapBuilder<K, V> withPeerUpdateFunction( |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 122 | BiFunction<K, V, Collection<NodeId>> peerUpdateFunction) { |
| 123 | this.peerUpdateFunction = checkNotNull(peerUpdateFunction); |
| 124 | return this; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public EventuallyConsistentMapBuilder<K, V> withTombstonesDisabled() { |
| 129 | tombstonesDisabled = true; |
| 130 | return this; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public EventuallyConsistentMapBuilder<K, V> withAntiEntropyPeriod(long period, TimeUnit unit) { |
| 135 | checkArgument(period > 0, "anti-entropy period must be greater than 0"); |
| 136 | antiEntropyPeriod = period; |
| 137 | antiEntropyTimeUnit = checkNotNull(unit); |
| 138 | return this; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public EventuallyConsistentMapBuilder<K, V> withFasterConvergence() { |
| 143 | convergeFaster = true; |
| 144 | return this; |
| 145 | } |
| 146 | |
| 147 | @Override |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 148 | public EventuallyConsistentMapBuilder<K, V> withPersistence() { |
Aaron Kruglikov | 66cf0b9 | 2015-10-26 15:46:54 -0700 | [diff] [blame] | 149 | checkNotNull(this.persistenceService); |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 150 | persistent = true; |
| 151 | return this; |
| 152 | } |
| 153 | |
| 154 | @Override |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 155 | public EventuallyConsistentMap<K, V> build() { |
| 156 | checkNotNull(name, "name is a mandatory parameter"); |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 157 | checkNotNull(timestampProvider, "timestampProvider is a mandatory parameter"); |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 158 | if (serializer == null && serializerBuilder != null) { |
| 159 | serializer = serializerBuilder.build(name); |
| 160 | } |
| 161 | checkNotNull(serializer, "serializer is a mandatory parameter"); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 162 | |
| 163 | return new EventuallyConsistentMapImpl<>(name, |
| 164 | clusterService, |
| 165 | clusterCommunicator, |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 166 | serializer, |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 167 | timestampProvider, |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 168 | peerUpdateFunction, |
| 169 | eventExecutor, |
| 170 | communicationExecutor, |
| 171 | backgroundExecutor, |
| 172 | tombstonesDisabled, |
| 173 | antiEntropyPeriod, |
| 174 | antiEntropyTimeUnit, |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 175 | convergeFaster, |
Aaron Kruglikov | 66cf0b9 | 2015-10-26 15:46:54 -0700 | [diff] [blame] | 176 | persistent, |
| 177 | persistenceService); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 178 | } |
| 179 | } |