Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-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 | */ |
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 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 18 | import java.util.Collection; |
| 19 | import java.util.concurrent.ExecutorService; |
| 20 | import java.util.concurrent.ScheduledExecutorService; |
| 21 | import java.util.concurrent.TimeUnit; |
| 22 | import java.util.function.BiFunction; |
| 23 | |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 24 | import org.onlab.util.KryoNamespace; |
| 25 | import org.onosproject.cluster.ClusterService; |
| 26 | import org.onosproject.cluster.NodeId; |
| 27 | import org.onosproject.persistence.PersistenceService; |
| 28 | import org.onosproject.store.Timestamp; |
| 29 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 30 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 31 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; |
| 32 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 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> { |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 41 | private final ClusterService clusterService; |
| 42 | private final ClusterCommunicationService clusterCommunicator; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 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 | */ |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 67 | public EventuallyConsistentMapBuilderImpl( |
| 68 | ClusterService clusterService, |
| 69 | ClusterCommunicationService clusterCommunicator, |
| 70 | PersistenceService persistenceService) { |
Aaron Kruglikov | 66cf0b9 | 2015-10-26 15:46:54 -0700 | [diff] [blame] | 71 | this.persistenceService = persistenceService; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 72 | this.clusterService = checkNotNull(clusterService); |
| 73 | this.clusterCommunicator = checkNotNull(clusterCommunicator); |
| 74 | } |
| 75 | |
| 76 | @Override |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 77 | public EventuallyConsistentMapBuilder<K, V> withName(String name) { |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 78 | this.name = checkNotNull(name); |
| 79 | return this; |
| 80 | } |
| 81 | |
| 82 | @Override |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 83 | public EventuallyConsistentMapBuilder<K, V> withSerializer( |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 84 | KryoNamespace.Builder serializerBuilder) { |
| 85 | this.serializerBuilder = checkNotNull(serializerBuilder); |
| 86 | return this; |
| 87 | } |
| 88 | |
| 89 | @Override |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 90 | public EventuallyConsistentMapBuilder<K, V> withSerializer(KryoNamespace serializer) { |
| 91 | this.serializer = checkNotNull(serializer); |
| 92 | return this; |
| 93 | } |
| 94 | |
| 95 | @Override |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 96 | public EventuallyConsistentMapBuilder<K, V> withTimestampProvider( |
| 97 | BiFunction<K, V, Timestamp> timestampProvider) { |
| 98 | this.timestampProvider = checkNotNull(timestampProvider); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 99 | return this; |
| 100 | } |
| 101 | |
| 102 | @Override |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 103 | public EventuallyConsistentMapBuilder<K, V> withEventExecutor(ExecutorService executor) { |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 104 | this.eventExecutor = checkNotNull(executor); |
| 105 | return this; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public EventuallyConsistentMapBuilder<K, V> withCommunicationExecutor( |
| 110 | ExecutorService executor) { |
| 111 | communicationExecutor = checkNotNull(executor); |
| 112 | return this; |
| 113 | } |
| 114 | |
| 115 | @Override |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 116 | public EventuallyConsistentMapBuilder<K, V> withBackgroundExecutor(ScheduledExecutorService executor) { |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 117 | this.backgroundExecutor = checkNotNull(executor); |
| 118 | return this; |
| 119 | } |
| 120 | |
| 121 | @Override |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 122 | public EventuallyConsistentMapBuilder<K, V> withPeerUpdateFunction( |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 123 | BiFunction<K, V, Collection<NodeId>> peerUpdateFunction) { |
| 124 | this.peerUpdateFunction = checkNotNull(peerUpdateFunction); |
| 125 | return this; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public EventuallyConsistentMapBuilder<K, V> withTombstonesDisabled() { |
| 130 | tombstonesDisabled = true; |
| 131 | return this; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public EventuallyConsistentMapBuilder<K, V> withAntiEntropyPeriod(long period, TimeUnit unit) { |
| 136 | checkArgument(period > 0, "anti-entropy period must be greater than 0"); |
| 137 | antiEntropyPeriod = period; |
| 138 | antiEntropyTimeUnit = checkNotNull(unit); |
| 139 | return this; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public EventuallyConsistentMapBuilder<K, V> withFasterConvergence() { |
| 144 | convergeFaster = true; |
| 145 | return this; |
| 146 | } |
| 147 | |
| 148 | @Override |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 149 | public EventuallyConsistentMapBuilder<K, V> withPersistence() { |
Aaron Kruglikov | 66cf0b9 | 2015-10-26 15:46:54 -0700 | [diff] [blame] | 150 | checkNotNull(this.persistenceService); |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 151 | persistent = true; |
| 152 | return this; |
| 153 | } |
| 154 | |
| 155 | @Override |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 156 | public EventuallyConsistentMap<K, V> build() { |
| 157 | checkNotNull(name, "name is a mandatory parameter"); |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 158 | checkNotNull(timestampProvider, "timestampProvider is a mandatory parameter"); |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 159 | if (serializer == null && serializerBuilder != null) { |
| 160 | serializer = serializerBuilder.build(name); |
| 161 | } |
| 162 | checkNotNull(serializer, "serializer is a mandatory parameter"); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 163 | |
| 164 | return new EventuallyConsistentMapImpl<>(name, |
| 165 | clusterService, |
| 166 | clusterCommunicator, |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 167 | serializer, |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 168 | timestampProvider, |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 169 | peerUpdateFunction, |
| 170 | eventExecutor, |
| 171 | communicationExecutor, |
| 172 | backgroundExecutor, |
| 173 | tombstonesDisabled, |
| 174 | antiEntropyPeriod, |
| 175 | antiEntropyTimeUnit, |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 176 | convergeFaster, |
Aaron Kruglikov | 66cf0b9 | 2015-10-26 15:46:54 -0700 | [diff] [blame] | 177 | persistent, |
| 178 | persistenceService); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 179 | } |
| 180 | } |