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 | 7b7644f | 2017-10-16 12:57:55 -0700 | [diff] [blame] | 21 | import java.util.concurrent.TimeUnit; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 22 | import java.util.stream.Collectors; |
Jordan Halterman | 046faeb | 2017-05-01 15:10:13 -0700 | [diff] [blame] | 23 | |
Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame] | 24 | import com.google.common.base.Suppliers; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 25 | import io.atomix.protocols.raft.RaftClient; |
| 26 | import io.atomix.protocols.raft.ReadConsistency; |
| 27 | import io.atomix.protocols.raft.cluster.MemberId; |
| 28 | import io.atomix.protocols.raft.protocol.RaftClientProtocol; |
| 29 | import io.atomix.protocols.raft.proxy.CommunicationStrategy; |
| 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 | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 55 | import org.onosproject.store.service.DistributedPrimitive; |
Jordan Halterman | d0d8035 | 2017-08-10 15:08:27 -0700 | [diff] [blame] | 56 | import org.onosproject.store.service.Ordering; |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 57 | import org.onosproject.store.service.PartitionClientInfo; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 58 | import org.onosproject.store.service.Serializer; |
Madan Jampani | 819d61d | 2016-07-25 20:29:43 -0700 | [diff] [blame] | 59 | import org.onosproject.store.service.WorkQueue; |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 60 | import org.slf4j.Logger; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 61 | |
Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame] | 62 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * StoragePartition client. |
| 66 | */ |
| 67 | public class StoragePartitionClient implements DistributedPrimitiveCreator, Managed<StoragePartitionClient> { |
| 68 | |
Jordan Halterman | 2124935 | 2018-01-23 12:35:09 -0800 | [diff] [blame] | 69 | private static final int MAX_RETRIES = 8; |
| 70 | private static final String ATOMIC_VALUES_CONSISTENT_MAP_NAME = "onos-atomic-values"; |
| 71 | |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 72 | private final Logger log = getLogger(getClass()); |
| 73 | |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 74 | private final StoragePartition partition; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 75 | private final MemberId localMemberId; |
| 76 | private final RaftClientProtocol protocol; |
| 77 | private RaftClient client; |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 78 | private final com.google.common.base.Supplier<AsyncConsistentMap<String, byte[]>> onosAtomicValuesMap = |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 79 | Suppliers.memoize(() -> newAsyncConsistentMap(ATOMIC_VALUES_CONSISTENT_MAP_NAME, |
| 80 | Serializer.using(KryoNamespaces.BASIC))); |
| 81 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 82 | public StoragePartitionClient(StoragePartition partition, MemberId localMemberId, RaftClientProtocol protocol) { |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 83 | this.partition = partition; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 84 | this.localMemberId = localMemberId; |
| 85 | this.protocol = protocol; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public CompletableFuture<Void> open() { |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 90 | synchronized (StoragePartitionClient.this) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 91 | client = newRaftClient(protocol); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 92 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 93 | return client.connect(partition.getMemberIds()).whenComplete((r, e) -> { |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 94 | if (e == null) { |
| 95 | log.info("Successfully started client for partition {}", partition.getId()); |
| 96 | } else { |
| 97 | log.info("Failed to start client for partition {}", partition.getId(), e); |
| 98 | } |
| 99 | }).thenApply(v -> null); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public CompletableFuture<Void> close() { |
| 104 | return client != null ? client.close() : CompletableFuture.completedFuture(null); |
| 105 | } |
| 106 | |
| 107 | @Override |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 108 | @SuppressWarnings("unchecked") |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 109 | public <K, V> AsyncConsistentMap<K, V> newAsyncConsistentMap(String name, Serializer serializer) { |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 110 | AtomixConsistentMap rawMap = |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 111 | new AtomixConsistentMap(client.newProxyBuilder() |
| 112 | .withName(name) |
| 113 | .withServiceType(DistributedPrimitive.Type.CONSISTENT_MAP.name()) |
| 114 | .withReadConsistency(ReadConsistency.SEQUENTIAL) |
| 115 | .withCommunicationStrategy(CommunicationStrategy.ANY) |
| 116 | .withTimeout(Duration.ofSeconds(30)) |
Jordan Halterman | 2124935 | 2018-01-23 12:35:09 -0800 | [diff] [blame] | 117 | .withMaxRetries(MAX_RETRIES) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 118 | .build() |
| 119 | .open() |
| 120 | .join()); |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 121 | |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 122 | if (serializer != null) { |
| 123 | return DistributedPrimitives.newTranscodingMap(rawMap, |
| 124 | key -> HexString.toHexString(serializer.encode(key)), |
| 125 | string -> serializer.decode(HexString.fromHexString(string)), |
| 126 | value -> value == null ? null : serializer.encode(value), |
| 127 | bytes -> serializer.decode(bytes)); |
| 128 | } |
| 129 | return (AsyncConsistentMap<K, V>) rawMap; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 133 | @SuppressWarnings("unchecked") |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 134 | public <V> AsyncConsistentTreeMap<V> newAsyncConsistentTreeMap(String name, Serializer serializer) { |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 135 | AtomixConsistentTreeMap rawMap = |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 136 | new AtomixConsistentTreeMap(client.newProxyBuilder() |
| 137 | .withName(name) |
| 138 | .withServiceType(DistributedPrimitive.Type.CONSISTENT_TREEMAP.name()) |
| 139 | .withReadConsistency(ReadConsistency.SEQUENTIAL) |
| 140 | .withCommunicationStrategy(CommunicationStrategy.ANY) |
| 141 | .withTimeout(Duration.ofSeconds(30)) |
Jordan Halterman | 2124935 | 2018-01-23 12:35:09 -0800 | [diff] [blame] | 142 | .withMaxRetries(MAX_RETRIES) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 143 | .build() |
| 144 | .open() |
| 145 | .join()); |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 146 | |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 147 | if (serializer != null) { |
| 148 | return DistributedPrimitives.newTranscodingTreeMap( |
| 149 | rawMap, |
| 150 | value -> value == null ? null : serializer.encode(value), |
| 151 | bytes -> serializer.decode(bytes)); |
| 152 | } |
| 153 | return (AsyncConsistentTreeMap<V>) rawMap; |
Aaron Kruglikov | ed88ff6 | 2016-08-01 16:02:09 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 157 | @SuppressWarnings("unchecked") |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 158 | public <K, V> AsyncConsistentMultimap<K, V> newAsyncConsistentSetMultimap(String name, Serializer serializer) { |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 159 | AtomixConsistentSetMultimap rawMap = |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 160 | new AtomixConsistentSetMultimap(client.newProxyBuilder() |
| 161 | .withName(name) |
| 162 | .withServiceType(DistributedPrimitive.Type.CONSISTENT_MULTIMAP.name()) |
| 163 | .withReadConsistency(ReadConsistency.SEQUENTIAL) |
| 164 | .withCommunicationStrategy(CommunicationStrategy.ANY) |
| 165 | .withTimeout(Duration.ofSeconds(30)) |
Jordan Halterman | 2124935 | 2018-01-23 12:35:09 -0800 | [diff] [blame] | 166 | .withMaxRetries(MAX_RETRIES) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 167 | .build() |
| 168 | .open() |
| 169 | .join()); |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 170 | |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 171 | if (serializer != null) { |
| 172 | return DistributedPrimitives.newTranscodingMultimap( |
| 173 | rawMap, |
| 174 | key -> HexString.toHexString(serializer.encode(key)), |
| 175 | string -> serializer.decode(HexString.fromHexString(string)), |
| 176 | value -> serializer.encode(value), |
| 177 | bytes -> serializer.decode(bytes)); |
| 178 | } |
| 179 | return (AsyncConsistentMultimap<K, V>) rawMap; |
Aaron Kruglikov | 61582a0 | 2016-09-06 13:18:58 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 183 | public <E> AsyncDistributedSet<E> newAsyncDistributedSet(String name, Serializer serializer) { |
| 184 | return DistributedPrimitives.newSetFromMap(newAsyncConsistentMap(name, serializer)); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | @Override |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 188 | @SuppressWarnings("unchecked") |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 189 | public <K> AsyncAtomicCounterMap<K> newAsyncAtomicCounterMap(String name, Serializer serializer) { |
Jordan Halterman | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 190 | AtomixAtomicCounterMap rawMap = new AtomixAtomicCounterMap(client.newProxyBuilder() |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 191 | .withName(name) |
| 192 | .withServiceType(DistributedPrimitive.Type.COUNTER_MAP.name()) |
| 193 | .withReadConsistency(ReadConsistency.LINEARIZABLE_LEASE) |
| 194 | .withCommunicationStrategy(CommunicationStrategy.LEADER) |
| 195 | .withTimeout(Duration.ofSeconds(30)) |
Jordan Halterman | 2124935 | 2018-01-23 12:35:09 -0800 | [diff] [blame] | 196 | .withMaxRetries(MAX_RETRIES) |
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 | e7f363e | 2017-07-28 16:52:20 -0700 | [diff] [blame] | 201 | if (serializer != null) { |
| 202 | return DistributedPrimitives.newTranscodingAtomicCounterMap( |
| 203 | rawMap, |
| 204 | key -> HexString.toHexString(serializer.encode(key)), |
| 205 | string -> serializer.decode(HexString.fromHexString(string))); |
| 206 | } |
| 207 | return (AsyncAtomicCounterMap<K>) rawMap; |
Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | @Override |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 211 | public AsyncAtomicCounter newAsyncCounter(String name) { |
| 212 | return new AtomixCounter(client.newProxyBuilder() |
| 213 | .withName(name) |
| 214 | .withServiceType(DistributedPrimitive.Type.COUNTER.name()) |
| 215 | .withReadConsistency(ReadConsistency.LINEARIZABLE_LEASE) |
| 216 | .withCommunicationStrategy(CommunicationStrategy.LEADER) |
| 217 | .withTimeout(Duration.ofSeconds(30)) |
Jordan Halterman | 2124935 | 2018-01-23 12:35:09 -0800 | [diff] [blame] | 218 | .withMaxRetries(MAX_RETRIES) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 219 | .build() |
| 220 | .open() |
| 221 | .join()); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @Override |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 225 | public AsyncAtomicIdGenerator newAsyncIdGenerator(String name) { |
| 226 | return new AtomixIdGenerator(newAsyncCounter(name)); |
Jordan Halterman | 5a1053e | 2017-05-19 18:03:47 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | @Override |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 230 | public <V> AsyncAtomicValue<V> newAsyncAtomicValue(String name, Serializer serializer) { |
| 231 | return new DefaultAsyncAtomicValue<>(name, serializer, onosAtomicValuesMap.get()); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | @Override |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 235 | public <E> WorkQueue<E> newWorkQueue(String name, Serializer serializer) { |
| 236 | AtomixWorkQueue atomixWorkQueue = new AtomixWorkQueue(client.newProxyBuilder() |
| 237 | .withName(name) |
| 238 | .withServiceType(DistributedPrimitive.Type.WORK_QUEUE.name()) |
| 239 | .withReadConsistency(ReadConsistency.LINEARIZABLE_LEASE) |
| 240 | .withCommunicationStrategy(CommunicationStrategy.LEADER) |
| 241 | .withTimeout(Duration.ofSeconds(5)) |
Jordan Halterman | 2124935 | 2018-01-23 12:35:09 -0800 | [diff] [blame] | 242 | .withMaxRetries(MAX_RETRIES) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 243 | .build() |
| 244 | .open() |
| 245 | .join()); |
| 246 | return new DefaultDistributedWorkQueue<>(atomixWorkQueue, serializer); |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | @Override |
Jordan Halterman | d0d8035 | 2017-08-10 15:08:27 -0700 | [diff] [blame] | 250 | public <V> AsyncDocumentTree<V> newAsyncDocumentTree(String name, Serializer serializer, Ordering ordering) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 251 | AtomixDocumentTree atomixDocumentTree = new AtomixDocumentTree(client.newProxyBuilder() |
| 252 | .withName(name) |
Jordan Halterman | d0d8035 | 2017-08-10 15:08:27 -0700 | [diff] [blame] | 253 | .withServiceType(String.format("%s-%s", DistributedPrimitive.Type.DOCUMENT_TREE.name(), ordering)) |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 254 | .withReadConsistency(ReadConsistency.SEQUENTIAL) |
| 255 | .withCommunicationStrategy(CommunicationStrategy.ANY) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 256 | .withTimeout(Duration.ofSeconds(30)) |
Jordan Halterman | 2124935 | 2018-01-23 12:35:09 -0800 | [diff] [blame] | 257 | .withMaxRetries(MAX_RETRIES) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 258 | .build() |
| 259 | .open() |
| 260 | .join()); |
| 261 | return new DefaultDistributedDocumentTree<>(name, atomixDocumentTree, serializer); |
Madan Jampani | 2914e4e | 2016-09-13 17:48:56 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | @Override |
Jordan Halterman | 4743258 | 2018-01-25 16:56:45 -0800 | [diff] [blame] | 265 | public AsyncDistributedLock newAsyncDistributedLock(String name) { |
| 266 | return new AtomixDistributedLock(client.newProxyBuilder() |
| 267 | .withName(name) |
| 268 | .withServiceType(DistributedPrimitive.Type.LOCK.name()) |
| 269 | .withReadConsistency(ReadConsistency.LINEARIZABLE) |
| 270 | .withCommunicationStrategy(CommunicationStrategy.LEADER) |
| 271 | .withMinTimeout(Duration.ofSeconds(1)) |
| 272 | .withMaxTimeout(Duration.ofSeconds(5)) |
| 273 | .withMaxRetries(MAX_RETRIES) |
| 274 | .build() |
| 275 | .open() |
| 276 | .join()); |
| 277 | } |
| 278 | |
| 279 | @Override |
Jordan Halterman | 7b7644f | 2017-10-16 12:57:55 -0700 | [diff] [blame] | 280 | public AsyncLeaderElector newAsyncLeaderElector(String name, long leaderTimeout, TimeUnit timeUnit) { |
Jordan Halterman | 46c5eaa | 2018-01-24 16:46:55 -0800 | [diff] [blame] | 281 | return new AtomixLeaderElector(client.newProxyBuilder() |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 282 | .withName(name) |
| 283 | .withServiceType(DistributedPrimitive.Type.LEADER_ELECTOR.name()) |
| 284 | .withReadConsistency(ReadConsistency.LINEARIZABLE) |
| 285 | .withCommunicationStrategy(CommunicationStrategy.LEADER) |
Jordan Halterman | 19486e3 | 2017-11-02 15:00:06 -0700 | [diff] [blame] | 286 | .withMinTimeout(Duration.ofMillis(timeUnit.toMillis(leaderTimeout))) |
| 287 | .withMaxTimeout(Duration.ofSeconds(5)) |
Jordan Halterman | 2124935 | 2018-01-23 12:35:09 -0800 | [diff] [blame] | 288 | .withMaxRetries(MAX_RETRIES) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 289 | .build() |
| 290 | .open() |
| 291 | .join()); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | @Override |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 295 | public Set<String> getAsyncConsistentMapNames() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 296 | return client.metadata().getSessions(DistributedPrimitive.Type.CONSISTENT_MAP.name()) |
| 297 | .join() |
| 298 | .stream() |
| 299 | .map(RaftSessionMetadata::serviceName) |
| 300 | .collect(Collectors.toSet()); |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public Set<String> getAsyncAtomicCounterNames() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 305 | return client.metadata().getSessions(DistributedPrimitive.Type.COUNTER.name()) |
| 306 | .join() |
| 307 | .stream() |
| 308 | .map(RaftSessionMetadata::serviceName) |
| 309 | .collect(Collectors.toSet()); |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | @Override |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 313 | public Set<String> getWorkQueueNames() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 314 | return client.metadata().getSessions(DistributedPrimitive.Type.WORK_QUEUE.name()) |
| 315 | .join() |
| 316 | .stream() |
| 317 | .map(RaftSessionMetadata::serviceName) |
| 318 | .collect(Collectors.toSet()); |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | @Override |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 322 | public boolean isOpen() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 323 | return client != null; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 324 | } |
Madan Jampani | 471a7bc | 2016-04-27 15:51:38 -0700 | [diff] [blame] | 325 | |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 326 | /** |
| 327 | * Returns the {@link PartitionClientInfo information} for this client. |
| 328 | * @return partition client information |
| 329 | */ |
| 330 | public PartitionClientInfo clientInfo() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 331 | return new PartitionClientInfo(partition.getId(), partition.getMembers()); |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 334 | private RaftClient newRaftClient(RaftClientProtocol protocol) { |
| 335 | return RaftClient.newBuilder() |
| 336 | .withClientId("partition-" + partition.getId()) |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 337 | .withMemberId(localMemberId) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 338 | .withProtocol(protocol) |
Madan Jampani | 471a7bc | 2016-04-27 15:51:38 -0700 | [diff] [blame] | 339 | .build(); |
Madan Jampani | d5b200f | 2016-06-06 17:15:25 -0700 | [diff] [blame] | 340 | } |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 341 | } |