blob: 82265addd643551a02d9b761cfd5a87bd4339399 [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
Madan Jampani5e5b3d62016-02-01 16:03:33 -080018import io.atomix.AtomixClient;
19import io.atomix.catalyst.serializer.Serializer;
20import io.atomix.catalyst.transport.Address;
Madan Jampani630e7ac2016-05-31 11:34:05 -070021import io.atomix.catalyst.transport.local.LocalServerRegistry;
22import io.atomix.catalyst.transport.local.LocalTransport;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080023import io.atomix.copycat.client.CopycatClient;
24import io.atomix.copycat.server.CopycatServer;
25import io.atomix.copycat.server.storage.Storage;
26import io.atomix.copycat.server.storage.StorageLevel;
Madan Jampani630e7ac2016-05-31 11:34:05 -070027import io.atomix.manager.internal.ResourceManagerState;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080028import io.atomix.resource.ResourceType;
29
30import java.io.File;
31import java.io.IOException;
32import java.nio.file.Files;
33import java.time.Duration;
34import java.util.ArrayList;
35import java.util.List;
36import java.util.concurrent.CompletableFuture;
37import java.util.concurrent.CountDownLatch;
38
39import org.junit.After;
40import org.junit.Before;
41import org.onosproject.store.primitives.impl.CatalystSerializers;
42
43import com.google.common.util.concurrent.Uninterruptibles;
44
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;
51 protected int port;
52 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() {
72 Address address = new Address("localhost", port++);
73 members.add(address);
74 return address;
75 }
76
77 /**
78 * Creates a set of Copycat servers.
79 */
80 protected List<CopycatServer> createCopycatServers(int nodes) throws Throwable {
81 CountDownLatch latch = new CountDownLatch(nodes);
82 List<CopycatServer> servers = new ArrayList<>();
83
84 List<Address> members = new ArrayList<>();
85 for (int i = 0; i < nodes; i++) {
86 members.add(nextAddress());
87 }
88
89 for (int i = 0; i < nodes; i++) {
90 CopycatServer server = createCopycatServer(members.get(i));
Madan Jampani630e7ac2016-05-31 11:34:05 -070091 server.bootstrap(members).thenRun(latch::countDown);
Madan Jampani5e5b3d62016-02-01 16:03:33 -080092 servers.add(server);
93 }
94
95 Uninterruptibles.awaitUninterruptibly(latch);
96
97 return servers;
98 }
99
100 /**
101 * Creates a Copycat server.
102 */
103 protected CopycatServer createCopycatServer(Address address) {
Madan Jampani630e7ac2016-05-31 11:34:05 -0700104 CopycatServer server = CopycatServer.builder(address)
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800105 .withTransport(new LocalTransport(registry))
106 .withStorage(Storage.builder()
107 .withStorageLevel(StorageLevel.DISK)
108 .withDirectory(TEST_DIR + "/" + address.port())
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800109 .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
120 @Before
121 @After
122 public void clearTests() throws Exception {
123 registry = new LocalServerRegistry();
124 members = new ArrayList<>();
125 port = 5000;
126
127 CompletableFuture<Void> closeClients =
128 CompletableFuture.allOf(atomixClients.stream()
Madan Jampani630e7ac2016-05-31 11:34:05 -0700129 .map(AtomixClient::close)
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800130 .toArray(CompletableFuture[]::new));
131
132 closeClients.thenCompose(v -> CompletableFuture.allOf(copycatServers.stream()
Madan Jampani630e7ac2016-05-31 11:34:05 -0700133 .map(CopycatServer::shutdown)
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800134 .toArray(CompletableFuture[]::new))).join();
135
136 deleteDirectory(TEST_DIR);
137
138 atomixClients = new ArrayList<>();
139
140 copycatServers = new ArrayList<>();
141 }
142
143 /**
144 * Deletes a directory recursively.
145 */
146 private void deleteDirectory(File directory) throws IOException {
147 if (directory.exists()) {
148 File[] files = directory.listFiles();
149 if (files != null) {
150 for (File file : files) {
151 if (file.isDirectory()) {
152 deleteDirectory(file);
153 } else {
154 Files.delete(file.toPath());
155 }
156 }
157 }
158 Files.delete(directory.toPath());
159 }
160 }
161
162 /**
163 * Creates a Atomix client.
164 */
Madan Jampani630e7ac2016-05-31 11:34:05 -0700165 protected AtomixClient createAtomixClient() {
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800166 CountDownLatch latch = new CountDownLatch(1);
Madan Jampani630e7ac2016-05-31 11:34:05 -0700167 AtomixClient client = AtomixClient.builder()
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800168 .withTransport(new LocalTransport(registry))
169 .withSerializer(serializer.clone())
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800170 .build();
Madan Jampani630e7ac2016-05-31 11:34:05 -0700171 client.connect(members).thenRun(latch::countDown);
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800172 atomixClients.add(client);
173 Uninterruptibles.awaitUninterruptibly(latch);
174 return client;
175 }
176}