Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 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 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 18 | import io.atomix.AtomixClient; |
| 19 | import io.atomix.catalyst.serializer.Serializer; |
| 20 | import io.atomix.catalyst.transport.Address; |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame^] | 21 | import io.atomix.catalyst.transport.local.LocalServerRegistry; |
| 22 | import io.atomix.catalyst.transport.local.LocalTransport; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 23 | import io.atomix.copycat.client.CopycatClient; |
| 24 | import io.atomix.copycat.server.CopycatServer; |
| 25 | import io.atomix.copycat.server.storage.Storage; |
| 26 | import io.atomix.copycat.server.storage.StorageLevel; |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame^] | 27 | import io.atomix.manager.internal.ResourceManagerState; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 28 | import io.atomix.resource.ResourceType; |
| 29 | |
| 30 | import java.io.File; |
| 31 | import java.io.IOException; |
| 32 | import java.nio.file.Files; |
| 33 | import java.time.Duration; |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.List; |
| 36 | import java.util.concurrent.CompletableFuture; |
| 37 | import java.util.concurrent.CountDownLatch; |
| 38 | |
| 39 | import org.junit.After; |
| 40 | import org.junit.Before; |
| 41 | import org.onosproject.store.primitives.impl.CatalystSerializers; |
| 42 | |
| 43 | import com.google.common.util.concurrent.Uninterruptibles; |
| 44 | |
| 45 | /** |
| 46 | * Base class for various Atomix* tests. |
| 47 | */ |
| 48 | public 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 Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame^] | 55 | protected List<AtomixClient> atomixClients = new ArrayList<>(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 56 | 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 Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame^] | 91 | server.bootstrap(members).thenRun(latch::countDown); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 92 | 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 Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame^] | 104 | CopycatServer server = CopycatServer.builder(address) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 105 | .withTransport(new LocalTransport(registry)) |
| 106 | .withStorage(Storage.builder() |
| 107 | .withStorageLevel(StorageLevel.DISK) |
| 108 | .withDirectory(TEST_DIR + "/" + address.port()) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 109 | .build()) |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 110 | .withStateMachine(ResourceManagerState::new) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 111 | .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 Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame^] | 129 | .map(AtomixClient::close) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 130 | .toArray(CompletableFuture[]::new)); |
| 131 | |
| 132 | closeClients.thenCompose(v -> CompletableFuture.allOf(copycatServers.stream() |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame^] | 133 | .map(CopycatServer::shutdown) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 134 | .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 Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame^] | 165 | protected AtomixClient createAtomixClient() { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 166 | CountDownLatch latch = new CountDownLatch(1); |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame^] | 167 | AtomixClient client = AtomixClient.builder() |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 168 | .withTransport(new LocalTransport(registry)) |
| 169 | .withSerializer(serializer.clone()) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 170 | .build(); |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame^] | 171 | client.connect(members).thenRun(latch::countDown); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 172 | atomixClients.add(client); |
| 173 | Uninterruptibles.awaitUninterruptibly(latch); |
| 174 | return client; |
| 175 | } |
| 176 | } |