Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -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 | 5e5b3d6 | 2016-02-01 16:03:33 -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.resources.impl; |
| 17 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 18 | import java.io.File; |
| 19 | import java.io.IOException; |
| 20 | import java.nio.file.FileVisitResult; |
| 21 | import java.nio.file.Files; |
| 22 | import java.nio.file.Path; |
| 23 | import java.nio.file.Paths; |
| 24 | import java.nio.file.SimpleFileVisitor; |
| 25 | import java.nio.file.attribute.BasicFileAttributes; |
| 26 | import java.time.Instant; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 27 | import java.util.ArrayList; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 28 | import java.util.Collections; |
| 29 | import java.util.HashSet; |
| 30 | import java.util.LinkedList; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 31 | import java.util.List; |
| 32 | import java.util.concurrent.CompletableFuture; |
| 33 | import java.util.concurrent.CountDownLatch; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 34 | import java.util.concurrent.TimeUnit; |
| 35 | import java.util.function.Consumer; |
| 36 | import java.util.stream.Collectors; |
| 37 | |
| 38 | import com.google.common.collect.Lists; |
| 39 | import io.atomix.protocols.raft.RaftClient; |
| 40 | import io.atomix.protocols.raft.RaftError; |
| 41 | import io.atomix.protocols.raft.RaftServer; |
| 42 | import io.atomix.protocols.raft.ReadConsistency; |
| 43 | import io.atomix.protocols.raft.cluster.MemberId; |
| 44 | import io.atomix.protocols.raft.cluster.RaftMember; |
| 45 | import io.atomix.protocols.raft.cluster.impl.DefaultRaftMember; |
| 46 | import io.atomix.protocols.raft.event.RaftEvent; |
| 47 | import io.atomix.protocols.raft.event.impl.DefaultEventType; |
| 48 | import io.atomix.protocols.raft.operation.OperationType; |
| 49 | import io.atomix.protocols.raft.operation.RaftOperation; |
| 50 | import io.atomix.protocols.raft.operation.impl.DefaultOperationId; |
| 51 | import io.atomix.protocols.raft.protocol.AppendRequest; |
| 52 | import io.atomix.protocols.raft.protocol.AppendResponse; |
| 53 | import io.atomix.protocols.raft.protocol.CloseSessionRequest; |
| 54 | import io.atomix.protocols.raft.protocol.CloseSessionResponse; |
| 55 | import io.atomix.protocols.raft.protocol.CommandRequest; |
| 56 | import io.atomix.protocols.raft.protocol.CommandResponse; |
| 57 | import io.atomix.protocols.raft.protocol.ConfigureRequest; |
| 58 | import io.atomix.protocols.raft.protocol.ConfigureResponse; |
| 59 | import io.atomix.protocols.raft.protocol.InstallRequest; |
| 60 | import io.atomix.protocols.raft.protocol.InstallResponse; |
| 61 | import io.atomix.protocols.raft.protocol.JoinRequest; |
| 62 | import io.atomix.protocols.raft.protocol.JoinResponse; |
| 63 | import io.atomix.protocols.raft.protocol.KeepAliveRequest; |
| 64 | import io.atomix.protocols.raft.protocol.KeepAliveResponse; |
| 65 | import io.atomix.protocols.raft.protocol.LeaveRequest; |
| 66 | import io.atomix.protocols.raft.protocol.LeaveResponse; |
| 67 | import io.atomix.protocols.raft.protocol.MetadataRequest; |
| 68 | import io.atomix.protocols.raft.protocol.MetadataResponse; |
| 69 | import io.atomix.protocols.raft.protocol.OpenSessionRequest; |
| 70 | import io.atomix.protocols.raft.protocol.OpenSessionResponse; |
| 71 | import io.atomix.protocols.raft.protocol.PollRequest; |
| 72 | import io.atomix.protocols.raft.protocol.PollResponse; |
| 73 | import io.atomix.protocols.raft.protocol.PublishRequest; |
| 74 | import io.atomix.protocols.raft.protocol.QueryRequest; |
| 75 | import io.atomix.protocols.raft.protocol.QueryResponse; |
| 76 | import io.atomix.protocols.raft.protocol.RaftResponse; |
| 77 | import io.atomix.protocols.raft.protocol.ReconfigureRequest; |
| 78 | import io.atomix.protocols.raft.protocol.ReconfigureResponse; |
| 79 | import io.atomix.protocols.raft.protocol.ResetRequest; |
| 80 | import io.atomix.protocols.raft.protocol.VoteRequest; |
| 81 | import io.atomix.protocols.raft.protocol.VoteResponse; |
| 82 | import io.atomix.protocols.raft.proxy.CommunicationStrategy; |
| 83 | import io.atomix.protocols.raft.proxy.RaftProxy; |
| 84 | import io.atomix.protocols.raft.service.RaftService; |
| 85 | import io.atomix.protocols.raft.session.SessionId; |
| 86 | import io.atomix.protocols.raft.storage.RaftStorage; |
| 87 | import io.atomix.protocols.raft.storage.log.entry.CloseSessionEntry; |
| 88 | import io.atomix.protocols.raft.storage.log.entry.CommandEntry; |
| 89 | import io.atomix.protocols.raft.storage.log.entry.ConfigurationEntry; |
| 90 | import io.atomix.protocols.raft.storage.log.entry.InitializeEntry; |
| 91 | import io.atomix.protocols.raft.storage.log.entry.KeepAliveEntry; |
| 92 | import io.atomix.protocols.raft.storage.log.entry.MetadataEntry; |
| 93 | import io.atomix.protocols.raft.storage.log.entry.OpenSessionEntry; |
| 94 | import io.atomix.protocols.raft.storage.log.entry.QueryEntry; |
| 95 | import io.atomix.protocols.raft.storage.system.Configuration; |
| 96 | import io.atomix.storage.StorageLevel; |
| 97 | import org.junit.After; |
| 98 | import org.junit.Before; |
| 99 | import org.onlab.util.KryoNamespace; |
| 100 | import org.onosproject.cluster.NodeId; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 101 | import org.onosproject.store.primitives.impl.RaftClientCommunicator; |
| 102 | import org.onosproject.store.primitives.impl.RaftServerCommunicator; |
| 103 | import org.onosproject.store.service.Serializer; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 104 | |
| 105 | /** |
Aaron Kruglikov | b5a41e5 | 2016-06-23 15:37:41 -0700 | [diff] [blame] | 106 | * Base class for various Atomix tests. |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 107 | * |
| 108 | * @param <T> the Raft primitive type being tested |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 109 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 110 | public abstract class AtomixTestBase<T extends AbstractRaftPrimitive> { |
| 111 | |
| 112 | private static final Serializer PROTOCOL_SERIALIZER = Serializer.using(KryoNamespace.newBuilder() |
| 113 | .register(OpenSessionRequest.class) |
| 114 | .register(OpenSessionResponse.class) |
| 115 | .register(CloseSessionRequest.class) |
| 116 | .register(CloseSessionResponse.class) |
| 117 | .register(KeepAliveRequest.class) |
| 118 | .register(KeepAliveResponse.class) |
| 119 | .register(QueryRequest.class) |
| 120 | .register(QueryResponse.class) |
| 121 | .register(CommandRequest.class) |
| 122 | .register(CommandResponse.class) |
| 123 | .register(MetadataRequest.class) |
| 124 | .register(MetadataResponse.class) |
| 125 | .register(JoinRequest.class) |
| 126 | .register(JoinResponse.class) |
| 127 | .register(LeaveRequest.class) |
| 128 | .register(LeaveResponse.class) |
| 129 | .register(ConfigureRequest.class) |
| 130 | .register(ConfigureResponse.class) |
| 131 | .register(ReconfigureRequest.class) |
| 132 | .register(ReconfigureResponse.class) |
| 133 | .register(InstallRequest.class) |
| 134 | .register(InstallResponse.class) |
| 135 | .register(PollRequest.class) |
| 136 | .register(PollResponse.class) |
| 137 | .register(VoteRequest.class) |
| 138 | .register(VoteResponse.class) |
| 139 | .register(AppendRequest.class) |
| 140 | .register(AppendResponse.class) |
| 141 | .register(PublishRequest.class) |
| 142 | .register(ResetRequest.class) |
| 143 | .register(RaftResponse.Status.class) |
| 144 | .register(RaftError.class) |
| 145 | .register(RaftError.Type.class) |
| 146 | .register(ReadConsistency.class) |
| 147 | .register(byte[].class) |
| 148 | .register(long[].class) |
| 149 | .register(CloseSessionEntry.class) |
| 150 | .register(CommandEntry.class) |
| 151 | .register(ConfigurationEntry.class) |
| 152 | .register(InitializeEntry.class) |
| 153 | .register(KeepAliveEntry.class) |
| 154 | .register(MetadataEntry.class) |
| 155 | .register(OpenSessionEntry.class) |
| 156 | .register(QueryEntry.class) |
| 157 | .register(RaftOperation.class) |
| 158 | .register(RaftEvent.class) |
| 159 | .register(DefaultEventType.class) |
| 160 | .register(DefaultOperationId.class) |
| 161 | .register(OperationType.class) |
| 162 | .register(ReadConsistency.class) |
| 163 | .register(ArrayList.class) |
| 164 | .register(LinkedList.class) |
| 165 | .register(Collections.emptyList().getClass()) |
| 166 | .register(HashSet.class) |
| 167 | .register(DefaultRaftMember.class) |
| 168 | .register(MemberId.class) |
| 169 | .register(SessionId.class) |
| 170 | .register(RaftMember.Type.class) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 171 | .register(Instant.class) |
| 172 | .register(Configuration.class) |
| 173 | .register(AtomixAtomicCounterMapOperations.class) |
| 174 | .register(AtomixConsistentMapEvents.class) |
| 175 | .register(AtomixConsistentMapOperations.class) |
| 176 | .register(AtomixConsistentSetMultimapOperations.class) |
| 177 | .register(AtomixConsistentSetMultimapEvents.class) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 178 | .register(AtomixConsistentTreeMapOperations.class) |
| 179 | .register(AtomixCounterOperations.class) |
| 180 | .register(AtomixDocumentTreeEvents.class) |
| 181 | .register(AtomixDocumentTreeOperations.class) |
| 182 | .register(AtomixLeaderElectorEvents.class) |
| 183 | .register(AtomixLeaderElectorOperations.class) |
| 184 | .register(AtomixWorkQueueEvents.class) |
| 185 | .register(AtomixWorkQueueOperations.class) |
| 186 | .build()); |
| 187 | |
| 188 | private static final Serializer STORAGE_SERIALIZER = Serializer.using(KryoNamespace.newBuilder() |
| 189 | .register(CloseSessionEntry.class) |
| 190 | .register(CommandEntry.class) |
| 191 | .register(ConfigurationEntry.class) |
| 192 | .register(InitializeEntry.class) |
| 193 | .register(KeepAliveEntry.class) |
| 194 | .register(MetadataEntry.class) |
| 195 | .register(OpenSessionEntry.class) |
| 196 | .register(QueryEntry.class) |
| 197 | .register(RaftOperation.class) |
| 198 | .register(ReadConsistency.class) |
| 199 | .register(AtomixAtomicCounterMapOperations.class) |
| 200 | .register(AtomixConsistentMapOperations.class) |
| 201 | .register(AtomixConsistentSetMultimapOperations.class) |
| 202 | .register(AtomixConsistentTreeMapOperations.class) |
| 203 | .register(AtomixCounterOperations.class) |
| 204 | .register(AtomixDocumentTreeOperations.class) |
| 205 | .register(AtomixLeaderElectorOperations.class) |
| 206 | .register(AtomixWorkQueueOperations.class) |
| 207 | .register(ArrayList.class) |
| 208 | .register(HashSet.class) |
| 209 | .register(DefaultRaftMember.class) |
| 210 | .register(MemberId.class) |
| 211 | .register(RaftMember.Type.class) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 212 | .register(Instant.class) |
| 213 | .register(Configuration.class) |
| 214 | .register(byte[].class) |
| 215 | .register(long[].class) |
| 216 | .build()); |
| 217 | |
| 218 | protected TestClusterCommunicationServiceFactory communicationServiceFactory; |
| 219 | protected List<RaftMember> members = Lists.newCopyOnWriteArrayList(); |
| 220 | protected List<RaftClient> clients = Lists.newCopyOnWriteArrayList(); |
| 221 | protected List<RaftServer> servers = Lists.newCopyOnWriteArrayList(); |
| 222 | protected int nextId; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 223 | |
| 224 | /** |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 225 | * Creates the primitive service. |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 226 | * |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 227 | * @return the primitive service |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 228 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 229 | protected abstract RaftService createService(); |
| 230 | |
| 231 | /** |
| 232 | * Creates a new primitive. |
| 233 | * |
| 234 | * @param name the primitive name |
| 235 | * @return the primitive instance |
| 236 | */ |
| 237 | protected T newPrimitive(String name) { |
| 238 | RaftClient client = createClient(); |
| 239 | RaftProxy proxy = client.newProxyBuilder() |
| 240 | .withName(name) |
| 241 | .withServiceType("test") |
| 242 | .withReadConsistency(readConsistency()) |
| 243 | .withCommunicationStrategy(communicationStrategy()) |
| 244 | .build() |
| 245 | .open() |
| 246 | .join(); |
| 247 | return createPrimitive(proxy); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Creates a new primitive instance. |
| 252 | * |
| 253 | * @param proxy the primitive proxy |
| 254 | * @return the primitive instance |
| 255 | */ |
| 256 | protected abstract T createPrimitive(RaftProxy proxy); |
| 257 | |
| 258 | /** |
| 259 | * Returns the proxy read consistency. |
| 260 | * |
| 261 | * @return the primitive read consistency |
| 262 | */ |
| 263 | protected ReadConsistency readConsistency() { |
| 264 | return ReadConsistency.LINEARIZABLE; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Returns the proxy communication strategy. |
| 269 | * |
| 270 | * @return the primitive communication strategy |
| 271 | */ |
| 272 | protected CommunicationStrategy communicationStrategy() { |
| 273 | return CommunicationStrategy.LEADER; |
| 274 | } |
| 275 | |
| 276 | @Before |
| 277 | public void prepare() { |
| 278 | members.clear(); |
| 279 | clients.clear(); |
| 280 | servers.clear(); |
| 281 | communicationServiceFactory = new TestClusterCommunicationServiceFactory(); |
| 282 | createServers(3); |
| 283 | } |
| 284 | |
| 285 | @After |
| 286 | public void cleanup() { |
| 287 | shutdown(); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Shuts down clients and servers. |
| 292 | */ |
| 293 | private void shutdown() { |
| 294 | clients.forEach(c -> { |
| 295 | try { |
| 296 | c.close().get(10, TimeUnit.SECONDS); |
| 297 | } catch (Exception e) { |
| 298 | } |
| 299 | }); |
| 300 | |
| 301 | servers.forEach(s -> { |
| 302 | try { |
| 303 | if (s.isRunning()) { |
| 304 | s.shutdown().get(10, TimeUnit.SECONDS); |
| 305 | } |
| 306 | } catch (Exception e) { |
| 307 | } |
| 308 | }); |
| 309 | |
| 310 | Path directory = Paths.get("target/primitives/"); |
| 311 | if (Files.exists(directory)) { |
| 312 | try { |
| 313 | Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { |
| 314 | @Override |
| 315 | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 316 | Files.delete(file); |
| 317 | return FileVisitResult.CONTINUE; |
| 318 | } |
| 319 | |
| 320 | @Override |
| 321 | public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { |
| 322 | Files.delete(dir); |
| 323 | return FileVisitResult.CONTINUE; |
| 324 | } |
| 325 | }); |
| 326 | } catch (IOException e) { |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Returns the next unique member identifier. |
| 333 | * |
| 334 | * @return The next unique member identifier. |
| 335 | */ |
| 336 | private MemberId nextMemberId() { |
| 337 | return MemberId.from(String.valueOf(++nextId)); |
| 338 | } |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 339 | |
| 340 | /** |
| 341 | * Returns the next server address. |
| 342 | * |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 343 | * @param type The startup member type. |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 344 | * @return The next server address. |
| 345 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 346 | private RaftMember nextMember(RaftMember.Type type) { |
| 347 | return new TestMember(nextMemberId(), type); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /** |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 351 | * Creates a set of Raft servers. |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 352 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 353 | protected List<RaftServer> createServers(int nodes) { |
| 354 | List<RaftServer> servers = new ArrayList<>(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 355 | |
| 356 | for (int i = 0; i < nodes; i++) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 357 | members.add(nextMember(RaftMember.Type.ACTIVE)); |
| 358 | } |
| 359 | |
| 360 | CountDownLatch latch = new CountDownLatch(nodes); |
| 361 | for (int i = 0; i < nodes; i++) { |
| 362 | RaftServer server = createServer(members.get(i)); |
| 363 | server.bootstrap(members.stream().map(RaftMember::memberId).collect(Collectors.toList())) |
| 364 | .thenRun(latch::countDown); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 365 | servers.add(server); |
| 366 | } |
| 367 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 368 | try { |
| 369 | latch.await(30000, TimeUnit.MILLISECONDS); |
| 370 | } catch (InterruptedException e) { |
| 371 | throw new RuntimeException(e); |
| 372 | } |
| 373 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 374 | return servers; |
| 375 | } |
| 376 | |
| 377 | /** |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 378 | * Creates a Raft server. |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 379 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 380 | private RaftServer createServer(RaftMember member) { |
| 381 | RaftServer.Builder builder = RaftServer.newBuilder(member.memberId()) |
| 382 | .withType(member.getType()) |
| 383 | .withProtocol(new RaftServerCommunicator( |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame^] | 384 | "partition-1", |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 385 | PROTOCOL_SERIALIZER, |
| 386 | communicationServiceFactory.newCommunicationService(NodeId.nodeId(member.memberId().id())))) |
| 387 | .withStorage(RaftStorage.newBuilder() |
| 388 | .withStorageLevel(StorageLevel.MEMORY) |
| 389 | .withDirectory(new File(String.format("target/primitives/%s", member.memberId()))) |
| 390 | .withSerializer(new AtomixSerializerAdapter(STORAGE_SERIALIZER)) |
| 391 | .withMaxSegmentSize(1024 * 1024) |
| 392 | .build()) |
| 393 | .addService("test", this::createService); |
| 394 | |
| 395 | RaftServer server = builder.build(); |
| 396 | servers.add(server); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 397 | return server; |
| 398 | } |
| 399 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 400 | /** |
| 401 | * Creates a Raft client. |
| 402 | */ |
| 403 | private RaftClient createClient() { |
| 404 | MemberId memberId = nextMemberId(); |
| 405 | RaftClient client = RaftClient.newBuilder() |
| 406 | .withMemberId(memberId) |
| 407 | .withProtocol(new RaftClientCommunicator( |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame^] | 408 | "partition-1", |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 409 | PROTOCOL_SERIALIZER, |
| 410 | communicationServiceFactory.newCommunicationService(NodeId.nodeId(memberId.id())))) |
| 411 | .build(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 412 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 413 | client.connect(members.stream().map(RaftMember::memberId).collect(Collectors.toList())).join(); |
| 414 | clients.add(client); |
| 415 | return client; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 418 | /** |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 419 | * Test member. |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 420 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 421 | public static class TestMember implements RaftMember { |
| 422 | private final MemberId memberId; |
| 423 | private final Type type; |
| 424 | |
| 425 | public TestMember(MemberId memberId, Type type) { |
| 426 | this.memberId = memberId; |
| 427 | this.type = type; |
| 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public MemberId memberId() { |
| 432 | return memberId; |
| 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public int hash() { |
| 437 | return memberId.hashCode(); |
| 438 | } |
| 439 | |
| 440 | @Override |
| 441 | public Type getType() { |
| 442 | return type; |
| 443 | } |
| 444 | |
| 445 | @Override |
| 446 | public void addTypeChangeListener(Consumer<Type> listener) { |
| 447 | |
| 448 | } |
| 449 | |
| 450 | @Override |
| 451 | public void removeTypeChangeListener(Consumer<Type> listener) { |
| 452 | |
| 453 | } |
| 454 | |
| 455 | @Override |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 456 | public Instant getLastUpdated() { |
| 457 | return Instant.now(); |
| 458 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 459 | @Override |
| 460 | public CompletableFuture<Void> promote() { |
| 461 | return null; |
| 462 | } |
| 463 | |
| 464 | @Override |
| 465 | public CompletableFuture<Void> promote(Type type) { |
| 466 | return null; |
| 467 | } |
| 468 | |
| 469 | @Override |
| 470 | public CompletableFuture<Void> demote() { |
| 471 | return null; |
| 472 | } |
| 473 | |
| 474 | @Override |
| 475 | public CompletableFuture<Void> demote(Type type) { |
| 476 | return null; |
| 477 | } |
| 478 | |
| 479 | @Override |
| 480 | public CompletableFuture<Void> remove() { |
| 481 | return null; |
| 482 | } |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 483 | } |
| 484 | } |