blob: d655d52670151716e3f2a95eda2a260d2e9812de [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;
29import io.atomix.resource.ResourceRegistry;
30import io.atomix.resource.ResourceType;
31
32import java.io.File;
33import java.io.IOException;
34import java.nio.file.Files;
35import java.time.Duration;
36import java.util.ArrayList;
37import java.util.List;
38import java.util.concurrent.CompletableFuture;
39import java.util.concurrent.CountDownLatch;
40
41import org.junit.After;
42import org.junit.Before;
43import org.onosproject.store.primitives.impl.CatalystSerializers;
44
45import com.google.common.util.concurrent.Uninterruptibles;
46
47/**
48 * Base class for various Atomix* tests.
49 */
50public abstract class AtomixTestBase {
51 private static final File TEST_DIR = new File("target/test-logs");
52 protected LocalServerRegistry registry;
53 protected int port;
54 protected List<Address> members;
55 protected List<CopycatClient> copycatClients = new ArrayList<>();
56 protected List<CopycatServer> copycatServers = new ArrayList<>();
57 protected List<Atomix> atomixClients = new ArrayList<>();
58 protected List<CopycatServer> atomixServers = new ArrayList<>();
59 protected Serializer serializer = CatalystSerializers.getSerializer();
60
61 /**
62 * Creates a new resource state machine.
63 *
64 * @return A new resource state machine.
65 */
66 protected abstract ResourceType resourceType();
67
68 /**
69 * Returns the next server address.
70 *
71 * @return The next server address.
72 */
73 private Address nextAddress() {
74 Address address = new Address("localhost", port++);
75 members.add(address);
76 return address;
77 }
78
79 /**
80 * Creates a set of Copycat servers.
81 */
82 protected List<CopycatServer> createCopycatServers(int nodes) throws Throwable {
83 CountDownLatch latch = new CountDownLatch(nodes);
84 List<CopycatServer> servers = new ArrayList<>();
85
86 List<Address> members = new ArrayList<>();
87 for (int i = 0; i < nodes; i++) {
88 members.add(nextAddress());
89 }
90
91 for (int i = 0; i < nodes; i++) {
92 CopycatServer server = createCopycatServer(members.get(i));
93 server.open().thenRun(latch::countDown);
94 servers.add(server);
95 }
96
97 Uninterruptibles.awaitUninterruptibly(latch);
98
99 return servers;
100 }
101
102 /**
103 * Creates a Copycat server.
104 */
105 protected CopycatServer createCopycatServer(Address address) {
106 ResourceRegistry resourceRegistry = new ResourceRegistry();
107 resourceRegistry.register(resourceType());
108 CopycatServer server = CopycatServer.builder(address, members)
109 .withTransport(new LocalTransport(registry))
110 .withStorage(Storage.builder()
111 .withStorageLevel(StorageLevel.DISK)
112 .withDirectory(TEST_DIR + "/" + address.port())
113 .withSerializer(serializer.clone())
114 .build())
115 .withStateMachine(() -> new ResourceManagerState(resourceRegistry))
116 .withSerializer(serializer.clone())
117 .withHeartbeatInterval(Duration.ofMillis(25))
118 .withElectionTimeout(Duration.ofMillis(50))
119 .withSessionTimeout(Duration.ofMillis(100))
120 .build();
121 copycatServers.add(server);
122 return server;
123 }
124
125 @Before
126 @After
127 public void clearTests() throws Exception {
128 registry = new LocalServerRegistry();
129 members = new ArrayList<>();
130 port = 5000;
131
132 CompletableFuture<Void> closeClients =
133 CompletableFuture.allOf(atomixClients.stream()
134 .map(Atomix::close)
135 .toArray(CompletableFuture[]::new));
136
137 closeClients.thenCompose(v -> CompletableFuture.allOf(copycatServers.stream()
138 .map(CopycatServer::close)
139 .toArray(CompletableFuture[]::new))).join();
140
141 deleteDirectory(TEST_DIR);
142
143 atomixClients = new ArrayList<>();
144
145 copycatServers = new ArrayList<>();
146 }
147
148 /**
149 * Deletes a directory recursively.
150 */
151 private void deleteDirectory(File directory) throws IOException {
152 if (directory.exists()) {
153 File[] files = directory.listFiles();
154 if (files != null) {
155 for (File file : files) {
156 if (file.isDirectory()) {
157 deleteDirectory(file);
158 } else {
159 Files.delete(file.toPath());
160 }
161 }
162 }
163 Files.delete(directory.toPath());
164 }
165 }
166
167 /**
168 * Creates a Atomix client.
169 */
170 protected Atomix createAtomixClient() {
171 CountDownLatch latch = new CountDownLatch(1);
172 Atomix client = AtomixClient.builder(members)
173 .withTransport(new LocalTransport(registry))
174 .withSerializer(serializer.clone())
175 .withResourceResolver(r -> r.register(resourceType()))
176 .build();
177 client.open().thenRun(latch::countDown);
178 atomixClients.add(client);
179 Uninterruptibles.awaitUninterruptibly(latch);
180 return client;
181 }
182}