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