blob: 01c33ec2f1d29af437cac9a88d7748f2d0ac065f [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.junit.After;
31import org.junit.Before;
32import org.onlab.junit.TestTools;
33import org.onosproject.store.primitives.impl.CatalystSerializers;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080034
35import java.io.File;
36import java.io.IOException;
37import java.nio.file.Files;
38import java.time.Duration;
39import java.util.ArrayList;
40import java.util.List;
41import java.util.concurrent.CompletableFuture;
42import java.util.concurrent.CountDownLatch;
Aaron Kruglikovc0c27c02016-06-07 16:05:00 -070043import java.util.concurrent.atomic.AtomicInteger;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080044
45/**
46 * Base class for various Atomix* tests.
47 */
48public abstract class AtomixTestBase {
49 private static final File TEST_DIR = new File("target/test-logs");
50 protected LocalServerRegistry registry;
Aaron Kruglikovc0c27c02016-06-07 16:05:00 -070051 protected final AtomicInteger port = new AtomicInteger(49200);
Madan Jampani5e5b3d62016-02-01 16:03:33 -080052 protected List<Address> members;
53 protected List<CopycatClient> copycatClients = new ArrayList<>();
54 protected List<CopycatServer> copycatServers = new ArrayList<>();
Madan Jampani630e7ac2016-05-31 11:34:05 -070055 protected List<AtomixClient> atomixClients = new ArrayList<>();
Madan Jampani5e5b3d62016-02-01 16:03:33 -080056 protected List<CopycatServer> atomixServers = new ArrayList<>();
57 protected Serializer serializer = CatalystSerializers.getSerializer();
58
59 /**
60 * Creates a new resource state machine.
61 *
62 * @return A new resource state machine.
63 */
64 protected abstract ResourceType resourceType();
65
66 /**
67 * Returns the next server address.
68 *
69 * @return The next server address.
70 */
71 private Address nextAddress() {
Aaron Kruglikovc0c27c02016-06-07 16:05:00 -070072 Address address = new Address("127.0.0.1",
73 TestTools.findAvailablePort(port.getAndIncrement()));
Madan Jampani5e5b3d62016-02-01 16:03:33 -080074 members.add(address);
75 return address;
76 }
77
78 /**
79 * Creates a set of Copycat servers.
80 */
81 protected List<CopycatServer> createCopycatServers(int nodes) throws Throwable {
82 CountDownLatch latch = new CountDownLatch(nodes);
83 List<CopycatServer> servers = new ArrayList<>();
84
85 List<Address> members = new ArrayList<>();
Madan Jampani5e5b3d62016-02-01 16:03:33 -080086
87 for (int i = 0; i < nodes; i++) {
Aaron Kruglikovc0c27c02016-06-07 16:05:00 -070088 Address address = nextAddress();
89 members.add(address);
90 CopycatServer server = createCopycatServer(address);
91 if (members.size() <= 1) {
92 server.bootstrap().thenRun(latch::countDown).join();
93 } else {
94 server.join(members).thenRun(latch::countDown);
95 }
Madan Jampani5e5b3d62016-02-01 16:03:33 -080096 servers.add(server);
97 }
98
99 Uninterruptibles.awaitUninterruptibly(latch);
100
101 return servers;
102 }
103
104 /**
105 * Creates a Copycat server.
106 */
107 protected CopycatServer createCopycatServer(Address address) {
Madan Jampani630e7ac2016-05-31 11:34:05 -0700108 CopycatServer server = CopycatServer.builder(address)
Aaron Kruglikovc0c27c02016-06-07 16:05:00 -0700109 .withTransport(NettyTransport.builder().withThreads(1).build())
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800110 .withStorage(Storage.builder()
Aaron Kruglikovc0c27c02016-06-07 16:05:00 -0700111 .withStorageLevel(StorageLevel.MEMORY)
112 .build())
Madan Jampani65f24bb2016-03-15 15:16:18 -0700113 .withStateMachine(ResourceManagerState::new)
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800114 .withSerializer(serializer.clone())
115 .withHeartbeatInterval(Duration.ofMillis(25))
116 .withElectionTimeout(Duration.ofMillis(50))
117 .withSessionTimeout(Duration.ofMillis(100))
118 .build();
119 copycatServers.add(server);
120 return server;
121 }
122
123 @Before
124 @After
125 public void clearTests() throws Exception {
126 registry = new LocalServerRegistry();
127 members = new ArrayList<>();
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800128
129 CompletableFuture<Void> closeClients =
130 CompletableFuture.allOf(atomixClients.stream()
Madan Jampani630e7ac2016-05-31 11:34:05 -0700131 .map(AtomixClient::close)
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800132 .toArray(CompletableFuture[]::new));
133
134 closeClients.thenCompose(v -> CompletableFuture.allOf(copycatServers.stream()
Madan Jampani630e7ac2016-05-31 11:34:05 -0700135 .map(CopycatServer::shutdown)
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800136 .toArray(CompletableFuture[]::new))).join();
137
138 deleteDirectory(TEST_DIR);
139
140 atomixClients = new ArrayList<>();
141
142 copycatServers = new ArrayList<>();
143 }
144
145 /**
146 * Deletes a directory recursively.
147 */
148 private void deleteDirectory(File directory) throws IOException {
149 if (directory.exists()) {
150 File[] files = directory.listFiles();
151 if (files != null) {
152 for (File file : files) {
153 if (file.isDirectory()) {
154 deleteDirectory(file);
155 } else {
156 Files.delete(file.toPath());
157 }
158 }
159 }
160 Files.delete(directory.toPath());
161 }
162 }
163
164 /**
165 * Creates a Atomix client.
166 */
Madan Jampani630e7ac2016-05-31 11:34:05 -0700167 protected AtomixClient createAtomixClient() {
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800168 CountDownLatch latch = new CountDownLatch(1);
Madan Jampani630e7ac2016-05-31 11:34:05 -0700169 AtomixClient client = AtomixClient.builder()
Aaron Kruglikovc0c27c02016-06-07 16:05:00 -0700170 .withTransport(NettyTransport.builder().withThreads(1).build())
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800171 .withSerializer(serializer.clone())
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800172 .build();
Madan Jampani630e7ac2016-05-31 11:34:05 -0700173 client.connect(members).thenRun(latch::countDown);
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800174 atomixClients.add(client);
175 Uninterruptibles.awaitUninterruptibly(latch);
176 return client;
177 }
178}