Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [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 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 18 | import java.time.Duration; |
Jordan Halterman | 046faeb | 2017-05-01 15:10:13 -0700 | [diff] [blame] | 19 | import java.util.Set; |
| 20 | import java.util.concurrent.CompletableFuture; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 21 | import java.util.stream.Collectors; |
Jordan Halterman | 046faeb | 2017-05-01 15:10:13 -0700 | [diff] [blame] | 22 | |
Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame] | 23 | import com.google.common.base.Suppliers; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 24 | import io.atomix.protocols.raft.RaftClient; |
| 25 | import io.atomix.protocols.raft.ReadConsistency; |
| 26 | import io.atomix.protocols.raft.cluster.MemberId; |
| 27 | import io.atomix.protocols.raft.protocol.RaftClientProtocol; |
| 28 | import io.atomix.protocols.raft.proxy.CommunicationStrategy; |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 29 | import io.atomix.protocols.raft.service.PropagationStrategy; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 30 | import io.atomix.protocols.raft.session.RaftSessionMetadata; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 31 | import org.onlab.util.HexString; |
| 32 | import org.onosproject.store.primitives.DistributedPrimitiveCreator; |
Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame] | 33 | import org.onosproject.store.primitives.resources.impl.AtomixAtomicCounterMap; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 34 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMap; |
Aaron Kruglikov | 61582a0 | 2016-09-06 13:18:58 -0700 | [diff] [blame] | 35 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimap; |
Aaron Kruglikov | ed88ff6 | 2016-08-01 16:02:09 -0700 | [diff] [blame] | 36 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMap; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 37 | import org.onosproject.store.primitives.resources.impl.AtomixCounter; |
Jordan Halterman | 4743258 | 2018-01-25 16:56:45 -0800 | [diff] [blame] | 38 | import org.onosproject.store.primitives.resources.impl.AtomixDistributedLock; |
Madan Jampani | 2914e4e | 2016-09-13 17:48:56 -0700 | [diff] [blame] | 39 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTree; |
Jordan Halterman | 5a1053e | 2017-05-19 18:03:47 -0700 | [diff] [blame] | 40 | import org.onosproject.store.primitives.resources.impl.AtomixIdGenerator; |
Madan Jampani | 39fff10 | 2016-02-14 13:17:28 -0800 | [diff] [blame] | 41 | import org.onosproject.store.primitives.resources.impl.AtomixLeaderElector; |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 42 | import org.onosproject.store.primitives.resources.impl.AtomixWorkQueue; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 43 | import org.onosproject.store.serializers.KryoNamespaces; |
| 44 | import org.onosproject.store.service.AsyncAtomicCounter; |
Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame] | 45 | import org.onosproject.store.service.AsyncAtomicCounterMap; |
Jordan Halterman | 5a1053e | 2017-05-19 18:03:47 -0700 | [diff] [blame] | 46 | import org.onosproject.store.service.AsyncAtomicIdGenerator; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 47 | import org.onosproject.store.service.AsyncAtomicValue; |
| 48 | import org.onosproject.store.service.AsyncConsistentMap; |
Aaron Kruglikov | 61582a0 | 2016-09-06 13:18:58 -0700 | [diff] [blame] | 49 | import org.onosproject.store.service.AsyncConsistentMultimap; |
Aaron Kruglikov | ed88ff6 | 2016-08-01 16:02:09 -0700 | [diff] [blame] | 50 | import org.onosproject.store.service.AsyncConsistentTreeMap; |
Jordan Halterman | 4743258 | 2018-01-25 16:56:45 -0800 | [diff] [blame] | 51 | import org.onosproject.store.service.AsyncDistributedLock; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 52 | import org.onosproject.store.service.AsyncDistributedSet; |
Madan Jampani | 2914e4e | 2016-09-13 17:48:56 -0700 | [diff] [blame] | 53 | import org.onosproject.store.service.AsyncDocumentTree; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 54 | import org.onosproject.store.service.AsyncLeaderElector; |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 55 | import org.onosproject.store.service.AtomicCounterMapOptions; |
| 56 | import org.onosproject.store.service.AtomicCounterOptions; |
| 57 | import org.onosproject.store.service.AtomicIdGeneratorOptions; |
| 58 | import org.onosproject.store.service.AtomicValueOptions; |
| 59 | import org.onosproject.store.service.ConsistentMapOptions; |
| 60 | import org.onosproject.store.service.ConsistentMultimapOptions; |
| 61 | import org.onosproject.store.service.ConsistentTreeMapOptions; |
| 62 | import org.onosproject.store.service.DistributedLockOptions; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 63 | import org.onosproject.store.service.DistributedPrimitive; |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 64 | import org.onosproject.store.service.DistributedSetOptions; |
| 65 | import org.onosproject.store.service.DocumentTreeOptions; |
| 66 | import org.onosproject.store.service.LeaderElectorOptions; |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 67 | import org.onosproject.store.service.PartitionClientInfo; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 68 | import org.onosproject.store.service.Serializer; |
Madan Jampani | 819d61d | 2016-07-25 20:29:43 -0700 | [diff] [blame] | 69 | import org.onosproject.store.service.WorkQueue; |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 70 | import org.onosproject.store.service.WorkQueueOptions; |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 71 | import org.slf4j.Logger; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 72 | |
Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame] | 73 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * StoragePartition client. |
| 77 | */ |
| 78 | public class StoragePartitionClient implements DistributedPrimitiveCreator, Managed<StoragePartitionClient> { |
| 79 | |
Jordan Halterman | 2124935 | 2018-01-23 12:35:09 -0800 | [diff] [blame] | 80 | private static final int MAX_RETRIES = 8; |
| 81 | private static final String ATOMIC_VALUES_CONSISTENT_MAP_NAME = "onos-atomic-values"; |
| 82 | |
Jordan Halterman | 153449c | 2018-04-05 18:31:26 -0700 | [diff] [blame] | 83 | private static final String MIN_TIMEOUT_PROPERTY = "onos.cluster.raft.client.minTimeoutMillis"; |
| 84 | private static final String MAX_TIMEOUT_PROPERTY = "onos.cluster.raft.client.maxTimeoutMillis"; |
| 85 | |
| 86 | private static final Duration MIN_TIMEOUT; |
| 87 | private static final Duration MAX_TIMEOUT; |
| 88 | |
| 89 | private static final long DEFAULT_MIN_TIMEOUT_MILLIS = 5000; |
| 90 | private static final long DEFAULT_MAX_TIMEOUT_MILLIS = 30000; |
| 91 | |
| 92 | static { |
| 93 | Duration minTimeout; |
| 94 | try { |
| 95 | minTimeout = Duration.ofMillis(Long.parseLong( |
| 96 | System.getProperty(MIN_TIMEOUT_PROPERTY, |
| 97 | String.valueOf(DEFAULT_MIN_TIMEOUT_MILLIS)))); |
| 98 | } catch (NumberFormatException e) { |
| 99 | minTimeout = Duration.ofMillis(DEFAULT_MIN_TIMEOUT_MILLIS); |
| 100 | } |
| 101 | MIN_TIMEOUT = minTimeout; |
| 102 | |
| 103 | Duration maxTimeout; |
| 104 | try { |
| 105 | maxTimeout = Duration.ofMillis(Long.parseLong( |
| 106 | System.getProperty(MAX_TIMEOUT_PROPERTY, |
| 107 | String.valueOf(DEFAULT_MAX_TIMEOUT_MILLIS)))); |
| 108 | } catch (NumberFormatException e) { |
| 109 | maxTimeout = Duration.ofMillis(DEFAULT_MAX_TIMEOUT_MILLIS); |
| 110 | } |
| 111 | MAX_TIMEOUT = maxTimeout; |
| 112 | } |
| 113 | |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 114 | private final Logger log = getLogger(getClass()); |
| 115 | |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 116 | private final StoragePartition partition; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 117 | private final MemberId localMemberId; |
| 118 | private final RaftClientProtocol protocol; |
| 119 | private RaftClient client; |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 120 | private final com.google.common.base.Supplier<AsyncConsistentMap<String, byte[]>> onosAtomicValuesMap = |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 121 | Suppliers.memoize(() -> newAsyncConsistentMap(ATOMIC_VALUES_CONSISTENT_MAP_NAME, |
| 122 | Serializer.using(KryoNamespaces.BASIC))); |
| 123 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 124 | public StoragePartitionClient(StoragePartition partition, MemberId localMemberId, RaftClientProtocol protocol) { |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 125 | this.partition = partition; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 126 | this.localMemberId = localMemberId; |
| 127 | this.protocol = protocol; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public CompletableFuture<Void> open() { |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 132 | synchronized (StoragePartitionClient.this) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 133 | client = newRaftClient(protocol); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 134 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 135 | return client.connect(partition.getMemberIds()).whenComplete((r, e) -> { |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 136 | if (e == null) { |
| 137 | log.info("Successfully started client for partition {}", partition.getId()); |
| 138 | } else { |
| 139 | log.info("Failed to start client for partition {}", partition.getId(), e); |
| 140 | } |
| 141 | }).thenApply(v -> null); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public CompletableFuture<Void> close() { |
| 146 | return client != null ? client.close() : CompletableFuture.completedFuture(null); |
| 147 | } |
| 148 | |
| 149 | @Override |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 150 | @SuppressWarnings("unchecked") |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 151 | public <K, V> AsyncConsistentMap<K, V> newAsyncConsistentMap(ConsistentMapOptions options) { |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 152 | AtomixConsistentMap rawMap = |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 153 | new AtomixConsistentMap(client.newProxyBuilder() |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 154 | .withName(options.name()) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 155 | .withServiceType(DistributedPrimitive.Type.CONSISTENT_MAP.name()) |
| 156 | .withReadConsistency(ReadConsistency.SEQUENTIAL) |
| 157 | .withCommunicationStrategy(CommunicationStrategy.ANY) |
Jordan Halterman | 153449c | 2018-04-05 18:31:26 -0700 | [diff] [blame] | 158 | .withMinTimeout(MIN_TIMEOUT) |
| 159 | .withMaxTimeout(MAX_TIMEOUT) |
Jordan Halterman | 2124935 | 2018-01-23 12:35:09 -0800 | [diff] [blame] | 160 | .withMaxRetries(MAX_RETRIES) |
Jordan Halterman | 4500817 | 2018-03-19 16:40:31 -0700 | [diff] [blame^] | 161 | .withRevision(options.version() != null && options.revisionType() != null |
| 162 | ? options.version().toInt() : 1) |
| 163 | .withPropagationStrategy(options.revisionType() != null |
| 164 | ? PropagationStrategy.valueOf(options.revisionType().name()) |
| 165 | : PropagationStrategy.NONE) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 166 | .build() |
| 167 | .open() |
| 168 | .join()); |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 169 | |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 170 | if (options.serializer() != null) { |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 171 | return DistributedPrimitives.newTranscodingMap(rawMap, |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 172 | key -> HexString.toHexString(options.serializer().encode(key)), |
| 173 | string -> options.serializer().decode(HexString.fromHexString(string)), |
| 174 | value -> value == null ? null : options.serializer().encode(value), |
| 175 | bytes -> options.serializer().decode(bytes)); |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 176 | } |
| 177 | return (AsyncConsistentMap<K, V>) rawMap; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 181 | @SuppressWarnings("unchecked") |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 182 | public <V> AsyncConsistentTreeMap<V> newAsyncConsistentTreeMap(ConsistentTreeMapOptions options) { |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 183 | AtomixConsistentTreeMap rawMap = |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 184 | new AtomixConsistentTreeMap(client.newProxyBuilder() |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 185 | .withName(options.name()) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 186 | .withServiceType(DistributedPrimitive.Type.CONSISTENT_TREEMAP.name()) |
| 187 | .withReadConsistency(ReadConsistency.SEQUENTIAL) |
| 188 | .withCommunicationStrategy(CommunicationStrategy.ANY) |
Jordan Halterman | 153449c | 2018-04-05 18:31:26 -0700 | [diff] [blame] | 189 | .withMinTimeout(MIN_TIMEOUT) |
| 190 | .withMaxTimeout(MAX_TIMEOUT) |
Jordan Halterman | 2124935 | 2018-01-23 12:35:09 -0800 | [diff] [blame] | 191 | .withMaxRetries(MAX_RETRIES) |
Jordan Halterman | 4500817 | 2018-03-19 16:40:31 -0700 | [diff] [blame^] | 192 | .withRevision(options.version() != null && options.revisionType() != null |
| 193 | ? options.version().toInt() : 1) |
| 194 | .withPropagationStrategy(options.revisionType() != null |
| 195 | ? PropagationStrategy.valueOf(options.revisionType().name()) |
| 196 | : PropagationStrategy.NONE) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 197 | .build() |
| 198 | .open() |
| 199 | .join()); |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 200 | |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 201 | if (options.serializer() != null) { |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 202 | return DistributedPrimitives.newTranscodingTreeMap( |
| 203 | rawMap, |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 204 | value -> value == null ? null : options.serializer().encode(value), |
| 205 | bytes -> options.serializer().decode(bytes)); |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 206 | } |
| 207 | return (AsyncConsistentTreeMap<V>) rawMap; |
Aaron Kruglikov | ed88ff6 | 2016-08-01 16:02:09 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | @Override |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 211 | @SuppressWarnings("unchecked") |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 212 | public <K, V> AsyncConsistentMultimap<K, V> newAsyncConsistentSetMultimap(ConsistentMultimapOptions options) { |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 213 | AtomixConsistentSetMultimap rawMap = |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 214 | new AtomixConsistentSetMultimap(client.newProxyBuilder() |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 215 | .withName(options.name()) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 216 | .withServiceType(DistributedPrimitive.Type.CONSISTENT_MULTIMAP.name()) |
| 217 | .withReadConsistency(ReadConsistency.SEQUENTIAL) |
| 218 | .withCommunicationStrategy(CommunicationStrategy.ANY) |
Jordan Halterman | 153449c | 2018-04-05 18:31:26 -0700 | [diff] [blame] | 219 | .withMinTimeout(MIN_TIMEOUT) |
| 220 | .withMaxTimeout(MAX_TIMEOUT) |
Jordan Halterman | 2124935 | 2018-01-23 12:35:09 -0800 | [diff] [blame] | 221 | .withMaxRetries(MAX_RETRIES) |
Jordan Halterman | 4500817 | 2018-03-19 16:40:31 -0700 | [diff] [blame^] | 222 | .withRevision(options.version() != null && options.revisionType() != null |
| 223 | ? options.version().toInt() : 1) |
| 224 | .withPropagationStrategy(options.revisionType() != null |
| 225 | ? PropagationStrategy.valueOf(options.revisionType().name()) |
| 226 | : PropagationStrategy.NONE) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 227 | .build() |
| 228 | .open() |
| 229 | .join()); |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 230 | |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 231 | if (options.serializer() != null) { |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 232 | return DistributedPrimitives.newTranscodingMultimap( |
| 233 | rawMap, |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 234 | key -> HexString.toHexString(options.serializer().encode(key)), |
| 235 | string -> options.serializer().decode(HexString.fromHexString(string)), |
| 236 | value -> options.serializer().encode(value), |
| 237 | bytes -> options.serializer().decode(bytes)); |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 238 | } |
| 239 | return (AsyncConsistentMultimap<K, V>) rawMap; |
Aaron Kruglikov | 61582a0 | 2016-09-06 13:18:58 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Override |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 243 | public <E> AsyncDistributedSet<E> newAsyncDistributedSet(DistributedSetOptions options) { |
| 244 | return DistributedPrimitives.newSetFromMap(newAsyncConsistentMap(options.name(), options.serializer())); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | @Override |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 248 | @SuppressWarnings("unchecked") |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 249 | public <K> AsyncAtomicCounterMap<K> newAsyncAtomicCounterMap(AtomicCounterMapOptions options) { |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 250 | AtomixAtomicCounterMap rawMap = new AtomixAtomicCounterMap(client.newProxyBuilder() |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 251 | .withName(options.name()) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 252 | .withServiceType(DistributedPrimitive.Type.COUNTER_MAP.name()) |
| 253 | .withReadConsistency(ReadConsistency.LINEARIZABLE_LEASE) |
| 254 | .withCommunicationStrategy(CommunicationStrategy.LEADER) |
Jordan Halterman | 153449c | 2018-04-05 18:31:26 -0700 | [diff] [blame] | 255 | .withMinTimeout(MIN_TIMEOUT) |
| 256 | .withMaxTimeout(MAX_TIMEOUT) |
Jordan Halterman | 2124935 | 2018-01-23 12:35:09 -0800 | [diff] [blame] | 257 | .withMaxRetries(MAX_RETRIES) |
Jordan Halterman | 4500817 | 2018-03-19 16:40:31 -0700 | [diff] [blame^] | 258 | .withRevision(options.version() != null && options.revisionType() != null |
| 259 | ? options.version().toInt() : 1) |
| 260 | .withPropagationStrategy(options.revisionType() != null |
| 261 | ? PropagationStrategy.valueOf(options.revisionType().name()) |
| 262 | : PropagationStrategy.NONE) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 263 | .build() |
| 264 | .open() |
| 265 | .join()); |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 266 | |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 267 | if (options.serializer() != null) { |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 268 | return DistributedPrimitives.newTranscodingAtomicCounterMap( |
| 269 | rawMap, |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 270 | key -> HexString.toHexString(options.serializer().encode(key)), |
| 271 | string -> options.serializer().decode(HexString.fromHexString(string))); |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 272 | } |
| 273 | return (AsyncAtomicCounterMap<K>) rawMap; |
Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | @Override |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 277 | public AsyncAtomicCounter newAsyncCounter(AtomicCounterOptions options) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 278 | return new AtomixCounter(client.newProxyBuilder() |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 279 | .withName(options.name()) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 280 | .withServiceType(DistributedPrimitive.Type.COUNTER.name()) |
| 281 | .withReadConsistency(ReadConsistency.LINEARIZABLE_LEASE) |
| 282 | .withCommunicationStrategy(CommunicationStrategy.LEADER) |
Jordan Halterman | 153449c | 2018-04-05 18:31:26 -0700 | [diff] [blame] | 283 | .withMinTimeout(MIN_TIMEOUT) |
| 284 | .withMaxTimeout(MAX_TIMEOUT) |
Jordan Halterman | 2124935 | 2018-01-23 12:35:09 -0800 | [diff] [blame] | 285 | .withMaxRetries(MAX_RETRIES) |
Jordan Halterman | 4500817 | 2018-03-19 16:40:31 -0700 | [diff] [blame^] | 286 | .withRevision(options.version() != null && options.revisionType() != null |
| 287 | ? options.version().toInt() : 1) |
| 288 | .withPropagationStrategy(options.revisionType() != null |
| 289 | ? PropagationStrategy.valueOf(options.revisionType().name()) |
| 290 | : PropagationStrategy.NONE) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 291 | .build() |
| 292 | .open() |
| 293 | .join()); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | @Override |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 297 | public AsyncAtomicIdGenerator newAsyncIdGenerator(AtomicIdGeneratorOptions options) { |
| 298 | return new AtomixIdGenerator(newAsyncCounter(options.name())); |
Jordan Halterman | 5a1053e | 2017-05-19 18:03:47 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | @Override |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 302 | public <V> AsyncAtomicValue<V> newAsyncAtomicValue(AtomicValueOptions options) { |
| 303 | return new DefaultAsyncAtomicValue<>(options.name(), options.serializer(), onosAtomicValuesMap.get()); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | @Override |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 307 | public <E> WorkQueue<E> newWorkQueue(WorkQueueOptions options) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 308 | AtomixWorkQueue atomixWorkQueue = new AtomixWorkQueue(client.newProxyBuilder() |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 309 | .withName(options.name()) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 310 | .withServiceType(DistributedPrimitive.Type.WORK_QUEUE.name()) |
| 311 | .withReadConsistency(ReadConsistency.LINEARIZABLE_LEASE) |
| 312 | .withCommunicationStrategy(CommunicationStrategy.LEADER) |
Jordan Halterman | 153449c | 2018-04-05 18:31:26 -0700 | [diff] [blame] | 313 | .withMinTimeout(MIN_TIMEOUT) |
| 314 | .withMaxTimeout(MAX_TIMEOUT) |
Jordan Halterman | 2124935 | 2018-01-23 12:35:09 -0800 | [diff] [blame] | 315 | .withMaxRetries(MAX_RETRIES) |
Jordan Halterman | 4500817 | 2018-03-19 16:40:31 -0700 | [diff] [blame^] | 316 | .withRevision(options.version() != null && options.revisionType() != null |
| 317 | ? options.version().toInt() : 1) |
| 318 | .withPropagationStrategy(options.revisionType() != null |
| 319 | ? PropagationStrategy.valueOf(options.revisionType().name()) |
| 320 | : PropagationStrategy.NONE) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 321 | .build() |
| 322 | .open() |
| 323 | .join()); |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 324 | return new DefaultDistributedWorkQueue<>(atomixWorkQueue, options.serializer()); |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | @Override |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 328 | public <V> AsyncDocumentTree<V> newAsyncDocumentTree(DocumentTreeOptions options) { |
| 329 | String serviceType = String.format("%s-%s", DistributedPrimitive.Type.DOCUMENT_TREE.name(), options.ordering()); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 330 | AtomixDocumentTree atomixDocumentTree = new AtomixDocumentTree(client.newProxyBuilder() |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 331 | .withName(options.name()) |
| 332 | .withServiceType(serviceType) |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 333 | .withReadConsistency(ReadConsistency.SEQUENTIAL) |
| 334 | .withCommunicationStrategy(CommunicationStrategy.ANY) |
Jordan Halterman | 153449c | 2018-04-05 18:31:26 -0700 | [diff] [blame] | 335 | .withMinTimeout(MIN_TIMEOUT) |
| 336 | .withMaxTimeout(MAX_TIMEOUT) |
Jordan Halterman | 2124935 | 2018-01-23 12:35:09 -0800 | [diff] [blame] | 337 | .withMaxRetries(MAX_RETRIES) |
Jordan Halterman | 4500817 | 2018-03-19 16:40:31 -0700 | [diff] [blame^] | 338 | .withRevision(options.version() != null && options.revisionType() != null |
| 339 | ? options.version().toInt() : 1) |
| 340 | .withPropagationStrategy(options.revisionType() != null |
| 341 | ? PropagationStrategy.valueOf(options.revisionType().name()) |
| 342 | : PropagationStrategy.NONE) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 343 | .build() |
| 344 | .open() |
| 345 | .join()); |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 346 | return new DefaultDistributedDocumentTree<>(options.name(), atomixDocumentTree, options.serializer()); |
Madan Jampani | 2914e4e | 2016-09-13 17:48:56 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | @Override |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 350 | public AsyncDistributedLock newAsyncDistributedLock(DistributedLockOptions options) { |
Jordan Halterman | 4743258 | 2018-01-25 16:56:45 -0800 | [diff] [blame] | 351 | return new AtomixDistributedLock(client.newProxyBuilder() |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 352 | .withName(options.name()) |
Jordan Halterman | 4743258 | 2018-01-25 16:56:45 -0800 | [diff] [blame] | 353 | .withServiceType(DistributedPrimitive.Type.LOCK.name()) |
| 354 | .withReadConsistency(ReadConsistency.LINEARIZABLE) |
| 355 | .withCommunicationStrategy(CommunicationStrategy.LEADER) |
Jordan Halterman | 153449c | 2018-04-05 18:31:26 -0700 | [diff] [blame] | 356 | .withMinTimeout(MIN_TIMEOUT) |
| 357 | .withMaxTimeout(MIN_TIMEOUT) |
Jordan Halterman | 4743258 | 2018-01-25 16:56:45 -0800 | [diff] [blame] | 358 | .withMaxRetries(MAX_RETRIES) |
Jordan Halterman | 4500817 | 2018-03-19 16:40:31 -0700 | [diff] [blame^] | 359 | .withRevision(options.version() != null && options.revisionType() != null |
| 360 | ? options.version().toInt() : 1) |
| 361 | .withPropagationStrategy(options.revisionType() != null |
| 362 | ? PropagationStrategy.valueOf(options.revisionType().name()) |
| 363 | : PropagationStrategy.NONE) |
Jordan Halterman | 4743258 | 2018-01-25 16:56:45 -0800 | [diff] [blame] | 364 | .build() |
| 365 | .open() |
| 366 | .join()); |
| 367 | } |
| 368 | |
| 369 | @Override |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 370 | public AsyncLeaderElector newAsyncLeaderElector(LeaderElectorOptions options) { |
Jordan Halterman | 46c5eaa | 2018-01-24 16:46:55 -0800 | [diff] [blame] | 371 | return new AtomixLeaderElector(client.newProxyBuilder() |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 372 | .withName(options.name()) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 373 | .withServiceType(DistributedPrimitive.Type.LEADER_ELECTOR.name()) |
| 374 | .withReadConsistency(ReadConsistency.LINEARIZABLE) |
| 375 | .withCommunicationStrategy(CommunicationStrategy.LEADER) |
Jordan Halterman | 2c04599 | 2018-03-20 21:33:00 -0700 | [diff] [blame] | 376 | .withMinTimeout(Duration.ofMillis(options.electionTimeoutMillis())) |
Jordan Halterman | 153449c | 2018-04-05 18:31:26 -0700 | [diff] [blame] | 377 | .withMaxTimeout(MIN_TIMEOUT) |
Jordan Halterman | 2124935 | 2018-01-23 12:35:09 -0800 | [diff] [blame] | 378 | .withMaxRetries(MAX_RETRIES) |
Jordan Halterman | 4500817 | 2018-03-19 16:40:31 -0700 | [diff] [blame^] | 379 | .withRevision(options.version() != null && options.revisionType() != null |
| 380 | ? options.version().toInt() : 1) |
| 381 | .withPropagationStrategy(options.revisionType() != null |
| 382 | ? PropagationStrategy.valueOf(options.revisionType().name()) |
| 383 | : PropagationStrategy.NONE) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 384 | .build() |
| 385 | .open() |
| 386 | .join()); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | @Override |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 390 | public Set<String> getAsyncConsistentMapNames() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 391 | return client.metadata().getSessions(DistributedPrimitive.Type.CONSISTENT_MAP.name()) |
| 392 | .join() |
| 393 | .stream() |
| 394 | .map(RaftSessionMetadata::serviceName) |
| 395 | .collect(Collectors.toSet()); |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | @Override |
| 399 | public Set<String> getAsyncAtomicCounterNames() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 400 | return client.metadata().getSessions(DistributedPrimitive.Type.COUNTER.name()) |
| 401 | .join() |
| 402 | .stream() |
| 403 | .map(RaftSessionMetadata::serviceName) |
| 404 | .collect(Collectors.toSet()); |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | @Override |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 408 | public Set<String> getWorkQueueNames() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 409 | return client.metadata().getSessions(DistributedPrimitive.Type.WORK_QUEUE.name()) |
| 410 | .join() |
| 411 | .stream() |
| 412 | .map(RaftSessionMetadata::serviceName) |
| 413 | .collect(Collectors.toSet()); |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | @Override |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 417 | public boolean isOpen() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 418 | return client != null; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 419 | } |
Madan Jampani | 471a7bc | 2016-04-27 15:51:38 -0700 | [diff] [blame] | 420 | |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 421 | /** |
| 422 | * Returns the {@link PartitionClientInfo information} for this client. |
| 423 | * @return partition client information |
| 424 | */ |
| 425 | public PartitionClientInfo clientInfo() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 426 | return new PartitionClientInfo(partition.getId(), partition.getMembers()); |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 429 | private RaftClient newRaftClient(RaftClientProtocol protocol) { |
| 430 | return RaftClient.newBuilder() |
| 431 | .withClientId("partition-" + partition.getId()) |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 432 | .withMemberId(localMemberId) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 433 | .withProtocol(protocol) |
Madan Jampani | 471a7bc | 2016-04-27 15:51:38 -0700 | [diff] [blame] | 434 | .build(); |
Madan Jampani | d5b200f | 2016-06-06 17:15:25 -0700 | [diff] [blame] | 435 | } |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 436 | } |