blob: db52ce9bfe930ad1035a9e59e2e4c7a9b90c1ec6 [file] [log] [blame]
Madan Jampani5e5b3d62016-02-01 16:03:33 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampani5e5b3d62016-02-01 16:03:33 -08003 *
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 */
16package org.onosproject.store.primitives.resources.impl;
17
Aaron Kruglikovc0c27c02016-06-07 16:05:00 -070018import com.google.common.util.concurrent.Uninterruptibles;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080019import io.atomix.AtomixClient;
20import io.atomix.catalyst.serializer.Serializer;
21import io.atomix.catalyst.transport.Address;
Madan Jampani630e7ac2016-05-31 11:34:05 -070022import io.atomix.catalyst.transport.local.LocalServerRegistry;
Aaron Kruglikovc0c27c02016-06-07 16:05:00 -070023import io.atomix.catalyst.transport.netty.NettyTransport;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080024import io.atomix.copycat.client.CopycatClient;
25import io.atomix.copycat.server.CopycatServer;
26import io.atomix.copycat.server.storage.Storage;
27import io.atomix.copycat.server.storage.StorageLevel;
Madan Jampani630e7ac2016-05-31 11:34:05 -070028import io.atomix.manager.internal.ResourceManagerState;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080029import io.atomix.resource.ResourceType;
Aaron Kruglikovc0c27c02016-06-07 16:05:00 -070030import org.onlab.junit.TestTools;
31import org.onosproject.store.primitives.impl.CatalystSerializers;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080032
33import java.io.File;
34import java.io.IOException;
35import java.nio.file.Files;
36import java.time.Duration;
37import java.util.ArrayList;
38import java.util.List;
39import java.util.concurrent.CompletableFuture;
40import java.util.concurrent.CountDownLatch;
Aaron Kruglikovc0c27c02016-06-07 16:05:00 -070041import java.util.concurrent.atomic.AtomicInteger;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080042
43/**
Aaron Kruglikovb5a41e52016-06-23 15:37:41 -070044 * Base class for various Atomix tests.
Madan Jampani5e5b3d62016-02-01 16:03:33 -080045 */
46public abstract class AtomixTestBase {
Aaron Kruglikovb5a41e52016-06-23 15:37:41 -070047 protected static File testDir;
48 protected static LocalServerRegistry registry = new LocalServerRegistry();
49 protected static List<Address> members = new ArrayList<>();
50 protected static List<CopycatClient> copycatClients = new ArrayList<>();
51 protected static List<CopycatServer> copycatServers = new ArrayList<>();
52 protected static List<AtomixClient> atomixClients = new ArrayList<>();
53 protected static List<CopycatServer> atomixServers = new ArrayList<>();
54 protected static Serializer serializer = CatalystSerializers.getSerializer();
55 protected static AtomicInteger port = new AtomicInteger(49200);
Madan Jampani5e5b3d62016-02-01 16:03:33 -080056
57 /**
58 * Creates a new resource state machine.
59 *
60 * @return A new resource state machine.
61 */
62 protected abstract ResourceType resourceType();
63
64 /**
65 * Returns the next server address.
66 *
67 * @return The next server address.
68 */
Aaron Kruglikovb5a41e52016-06-23 15:37:41 -070069 private static Address nextAddress() {
Aaron Kruglikovc0c27c02016-06-07 16:05:00 -070070 Address address = new Address("127.0.0.1",
71 TestTools.findAvailablePort(port.getAndIncrement()));
Madan Jampani5e5b3d62016-02-01 16:03:33 -080072 members.add(address);
73 return address;
74 }
75
76 /**
77 * Creates a set of Copycat servers.
78 */
Aaron Kruglikovb5a41e52016-06-23 15:37:41 -070079 protected static List<CopycatServer> createCopycatServers(int nodes)
80 throws Throwable {
Madan Jampani5e5b3d62016-02-01 16:03:33 -080081 CountDownLatch latch = new CountDownLatch(nodes);
82 List<CopycatServer> servers = new ArrayList<>();
83
84 List<Address> members = new ArrayList<>();
Madan Jampani5e5b3d62016-02-01 16:03:33 -080085
86 for (int i = 0; i < nodes; i++) {
Aaron Kruglikovc0c27c02016-06-07 16:05:00 -070087 Address address = nextAddress();
88 members.add(address);
89 CopycatServer server = createCopycatServer(address);
90 if (members.size() <= 1) {
91 server.bootstrap().thenRun(latch::countDown).join();
92 } else {
Aaron Kruglikovb5a41e52016-06-23 15:37:41 -070093 server.join(members).join();
Aaron Kruglikovc0c27c02016-06-07 16:05:00 -070094 }
Madan Jampani5e5b3d62016-02-01 16:03:33 -080095 servers.add(server);
96 }
97
Madan Jampani5e5b3d62016-02-01 16:03:33 -080098 return servers;
99 }
100
101 /**
102 * Creates a Copycat server.
103 */
Aaron Kruglikovb5a41e52016-06-23 15:37:41 -0700104 protected static CopycatServer createCopycatServer(Address address) {
Madan Jampani630e7ac2016-05-31 11:34:05 -0700105 CopycatServer server = CopycatServer.builder(address)
Aaron Kruglikovc0c27c02016-06-07 16:05:00 -0700106 .withTransport(NettyTransport.builder().withThreads(1).build())
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800107 .withStorage(Storage.builder()
Aaron Kruglikovc0c27c02016-06-07 16:05:00 -0700108 .withStorageLevel(StorageLevel.MEMORY)
109 .build())
Madan Jampani65f24bb2016-03-15 15:16:18 -0700110 .withStateMachine(ResourceManagerState::new)
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800111 .withSerializer(serializer.clone())
112 .withHeartbeatInterval(Duration.ofMillis(25))
113 .withElectionTimeout(Duration.ofMillis(50))
114 .withSessionTimeout(Duration.ofMillis(100))
115 .build();
116 copycatServers.add(server);
117 return server;
118 }
119
Aaron Kruglikovb5a41e52016-06-23 15:37:41 -0700120 public static void clearTests() throws Exception {
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800121 registry = new LocalServerRegistry();
122 members = new ArrayList<>();
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800123
124 CompletableFuture<Void> closeClients =
125 CompletableFuture.allOf(atomixClients.stream()
Aaron Kruglikovb5a41e52016-06-23 15:37:41 -0700126 .map(AtomixClient::close)
127 .toArray(CompletableFuture[]::new));
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800128
Aaron Kruglikovb5a41e52016-06-23 15:37:41 -0700129 closeClients
130 .thenCompose(v -> CompletableFuture
131 .allOf(copycatServers.stream()
Madan Jampani630e7ac2016-05-31 11:34:05 -0700132 .map(CopycatServer::shutdown)
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800133 .toArray(CompletableFuture[]::new))).join();
134
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800135 atomixClients = new ArrayList<>();
136
137 copycatServers = new ArrayList<>();
138 }
139
140 /**
141 * Deletes a directory recursively.
142 */
143 private void deleteDirectory(File directory) throws IOException {
144 if (directory.exists()) {
145 File[] files = directory.listFiles();
146 if (files != null) {
147 for (File file : files) {
148 if (file.isDirectory()) {
149 deleteDirectory(file);
150 } else {
151 Files.delete(file.toPath());
152 }
153 }
154 }
155 Files.delete(directory.toPath());
156 }
157 }
158
159 /**
160 * Creates a Atomix client.
161 */
Madan Jampani630e7ac2016-05-31 11:34:05 -0700162 protected AtomixClient createAtomixClient() {
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800163 CountDownLatch latch = new CountDownLatch(1);
Madan Jampani630e7ac2016-05-31 11:34:05 -0700164 AtomixClient client = AtomixClient.builder()
Aaron Kruglikovc0c27c02016-06-07 16:05:00 -0700165 .withTransport(NettyTransport.builder().withThreads(1).build())
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800166 .withSerializer(serializer.clone())
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800167 .build();
Madan Jampani630e7ac2016-05-31 11:34:05 -0700168 client.connect(members).thenRun(latch::countDown);
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800169 atomixClients.add(client);
170 Uninterruptibles.awaitUninterruptibly(latch);
171 return client;
172 }
173}