blob: bca67a02289e5152994eacc19d76ea64e70095d5 [file] [log] [blame]
Madan Jampani5e5b3d62016-02-01 16:03:33 -08001/*
2 * Copyright 2016 Open Networking Laboratory
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 */
16package org.onosproject.store.primitives.resources.impl;
17
18import io.atomix.Atomix;
19import io.atomix.AtomixClient;
20import io.atomix.catalyst.serializer.Serializer;
21import io.atomix.catalyst.transport.Address;
22import io.atomix.catalyst.transport.LocalServerRegistry;
23import io.atomix.catalyst.transport.LocalTransport;
24import 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;
28import io.atomix.manager.state.ResourceManagerState;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080029import io.atomix.resource.ResourceType;
30
31import java.io.File;
32import java.io.IOException;
33import java.nio.file.Files;
34import java.time.Duration;
35import java.util.ArrayList;
36import java.util.List;
37import java.util.concurrent.CompletableFuture;
38import java.util.concurrent.CountDownLatch;
39
40import org.junit.After;
41import org.junit.Before;
42import org.onosproject.store.primitives.impl.CatalystSerializers;
43
44import com.google.common.util.concurrent.Uninterruptibles;
45
46/**
47 * Base class for various Atomix* tests.
48 */
49public abstract class AtomixTestBase {
50 private static final File TEST_DIR = new File("target/test-logs");
51 protected LocalServerRegistry registry;
52 protected int port;
53 protected List<Address> members;
54 protected List<CopycatClient> copycatClients = new ArrayList<>();
55 protected List<CopycatServer> copycatServers = new ArrayList<>();
56 protected List<Atomix> atomixClients = new ArrayList<>();
57 protected List<CopycatServer> atomixServers = new ArrayList<>();
58 protected Serializer serializer = CatalystSerializers.getSerializer();
59
60 /**
61 * Creates a new resource state machine.
62 *
63 * @return A new resource state machine.
64 */
65 protected abstract ResourceType resourceType();
66
67 /**
68 * Returns the next server address.
69 *
70 * @return The next server address.
71 */
72 private Address nextAddress() {
73 Address address = new Address("localhost", port++);
74 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<>();
86 for (int i = 0; i < nodes; i++) {
87 members.add(nextAddress());
88 }
89
90 for (int i = 0; i < nodes; i++) {
91 CopycatServer server = createCopycatServer(members.get(i));
Madan Jampani65f24bb2016-03-15 15:16:18 -070092 server.start().thenRun(latch::countDown);
Madan Jampani5e5b3d62016-02-01 16:03:33 -080093 servers.add(server);
94 }
95
96 Uninterruptibles.awaitUninterruptibly(latch);
97
98 return servers;
99 }
100
101 /**
102 * Creates a Copycat server.
103 */
104 protected CopycatServer createCopycatServer(Address address) {
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800105 CopycatServer server = CopycatServer.builder(address, members)
106 .withTransport(new LocalTransport(registry))
107 .withStorage(Storage.builder()
108 .withStorageLevel(StorageLevel.DISK)
109 .withDirectory(TEST_DIR + "/" + address.port())
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800110 .build())
Madan Jampani65f24bb2016-03-15 15:16:18 -0700111 .withStateMachine(ResourceManagerState::new)
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800112 .withSerializer(serializer.clone())
113 .withHeartbeatInterval(Duration.ofMillis(25))
114 .withElectionTimeout(Duration.ofMillis(50))
115 .withSessionTimeout(Duration.ofMillis(100))
116 .build();
117 copycatServers.add(server);
118 return server;
119 }
120
121 @Before
122 @After
123 public void clearTests() throws Exception {
124 registry = new LocalServerRegistry();
125 members = new ArrayList<>();
126 port = 5000;
127
128 CompletableFuture<Void> closeClients =
129 CompletableFuture.allOf(atomixClients.stream()
130 .map(Atomix::close)
131 .toArray(CompletableFuture[]::new));
132
133 closeClients.thenCompose(v -> CompletableFuture.allOf(copycatServers.stream()
Madan Jampani65f24bb2016-03-15 15:16:18 -0700134 .map(CopycatServer::stop)
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800135 .toArray(CompletableFuture[]::new))).join();
136
137 deleteDirectory(TEST_DIR);
138
139 atomixClients = new ArrayList<>();
140
141 copycatServers = new ArrayList<>();
142 }
143
144 /**
145 * Deletes a directory recursively.
146 */
147 private void deleteDirectory(File directory) throws IOException {
148 if (directory.exists()) {
149 File[] files = directory.listFiles();
150 if (files != null) {
151 for (File file : files) {
152 if (file.isDirectory()) {
153 deleteDirectory(file);
154 } else {
155 Files.delete(file.toPath());
156 }
157 }
158 }
159 Files.delete(directory.toPath());
160 }
161 }
162
163 /**
164 * Creates a Atomix client.
165 */
166 protected Atomix createAtomixClient() {
167 CountDownLatch latch = new CountDownLatch(1);
168 Atomix client = AtomixClient.builder(members)
169 .withTransport(new LocalTransport(registry))
170 .withSerializer(serializer.clone())
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800171 .build();
172 client.open().thenRun(latch::countDown);
173 atomixClients.add(client);
174 Uninterruptibles.awaitUninterruptibly(latch);
175 return client;
176 }
177}