Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.store.primitives.resources.impl; |
| 17 | |
| 18 | import io.atomix.Atomix; |
| 19 | import io.atomix.AtomixClient; |
| 20 | import io.atomix.catalyst.serializer.Serializer; |
| 21 | import io.atomix.catalyst.transport.Address; |
| 22 | import io.atomix.catalyst.transport.LocalServerRegistry; |
| 23 | import io.atomix.catalyst.transport.LocalTransport; |
| 24 | import io.atomix.copycat.client.CopycatClient; |
| 25 | import io.atomix.copycat.server.CopycatServer; |
| 26 | import io.atomix.copycat.server.storage.Storage; |
| 27 | import io.atomix.copycat.server.storage.StorageLevel; |
| 28 | import io.atomix.manager.state.ResourceManagerState; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 29 | import io.atomix.resource.ResourceType; |
| 30 | |
| 31 | import java.io.File; |
| 32 | import java.io.IOException; |
| 33 | import java.nio.file.Files; |
| 34 | import java.time.Duration; |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.List; |
| 37 | import java.util.concurrent.CompletableFuture; |
| 38 | import java.util.concurrent.CountDownLatch; |
| 39 | |
| 40 | import org.junit.After; |
| 41 | import org.junit.Before; |
| 42 | import org.onosproject.store.primitives.impl.CatalystSerializers; |
| 43 | |
| 44 | import com.google.common.util.concurrent.Uninterruptibles; |
| 45 | |
| 46 | /** |
| 47 | * Base class for various Atomix* tests. |
| 48 | */ |
| 49 | public 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 Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame^] | 92 | server.start().thenRun(latch::countDown); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 93 | 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 Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 105 | 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 Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 110 | .build()) |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame^] | 111 | .withStateMachine(ResourceManagerState::new) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 112 | .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 Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame^] | 134 | .map(CopycatServer::stop) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 135 | .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 Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 171 | .build(); |
| 172 | client.open().thenRun(latch::countDown); |
| 173 | atomixClients.add(client); |
| 174 | Uninterruptibles.awaitUninterruptibly(latch); |
| 175 | return client; |
| 176 | } |
| 177 | } |