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 | |
Aaron Kruglikov | c0c27c0 | 2016-06-07 16:05:00 -0700 | [diff] [blame] | 18 | import com.google.common.util.concurrent.Uninterruptibles; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 19 | import io.atomix.AtomixClient; |
| 20 | import io.atomix.catalyst.serializer.Serializer; |
| 21 | import io.atomix.catalyst.transport.Address; |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 22 | import io.atomix.catalyst.transport.local.LocalServerRegistry; |
Aaron Kruglikov | c0c27c0 | 2016-06-07 16:05:00 -0700 | [diff] [blame] | 23 | import io.atomix.catalyst.transport.netty.NettyTransport; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 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; |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 28 | import io.atomix.manager.internal.ResourceManagerState; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 29 | import io.atomix.resource.ResourceType; |
Aaron Kruglikov | c0c27c0 | 2016-06-07 16:05:00 -0700 | [diff] [blame] | 30 | import org.junit.After; |
| 31 | import org.junit.Before; |
| 32 | import org.onlab.junit.TestTools; |
| 33 | import org.onosproject.store.primitives.impl.CatalystSerializers; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 34 | |
| 35 | import java.io.File; |
| 36 | import java.io.IOException; |
| 37 | import java.nio.file.Files; |
| 38 | import java.time.Duration; |
| 39 | import java.util.ArrayList; |
| 40 | import java.util.List; |
| 41 | import java.util.concurrent.CompletableFuture; |
| 42 | import java.util.concurrent.CountDownLatch; |
Aaron Kruglikov | c0c27c0 | 2016-06-07 16:05:00 -0700 | [diff] [blame] | 43 | import java.util.concurrent.atomic.AtomicInteger; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 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; |
Aaron Kruglikov | c0c27c0 | 2016-06-07 16:05:00 -0700 | [diff] [blame] | 51 | protected final AtomicInteger port = new AtomicInteger(49200); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 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() { |
Aaron Kruglikov | c0c27c0 | 2016-06-07 16:05:00 -0700 | [diff] [blame] | 72 | Address address = new Address("127.0.0.1", |
| 73 | TestTools.findAvailablePort(port.getAndIncrement())); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 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<>(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 86 | |
| 87 | for (int i = 0; i < nodes; i++) { |
Aaron Kruglikov | c0c27c0 | 2016-06-07 16:05:00 -0700 | [diff] [blame] | 88 | Address address = nextAddress(); |
| 89 | members.add(address); |
| 90 | CopycatServer server = createCopycatServer(address); |
| 91 | if (members.size() <= 1) { |
| 92 | server.bootstrap().thenRun(latch::countDown).join(); |
| 93 | } else { |
| 94 | server.join(members).thenRun(latch::countDown); |
| 95 | } |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 96 | servers.add(server); |
| 97 | } |
| 98 | |
| 99 | Uninterruptibles.awaitUninterruptibly(latch); |
| 100 | |
| 101 | return servers; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Creates a Copycat server. |
| 106 | */ |
| 107 | protected CopycatServer createCopycatServer(Address address) { |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 108 | CopycatServer server = CopycatServer.builder(address) |
Aaron Kruglikov | c0c27c0 | 2016-06-07 16:05:00 -0700 | [diff] [blame] | 109 | .withTransport(NettyTransport.builder().withThreads(1).build()) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 110 | .withStorage(Storage.builder() |
Aaron Kruglikov | c0c27c0 | 2016-06-07 16:05:00 -0700 | [diff] [blame] | 111 | .withStorageLevel(StorageLevel.MEMORY) |
| 112 | .build()) |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 113 | .withStateMachine(ResourceManagerState::new) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 114 | .withSerializer(serializer.clone()) |
| 115 | .withHeartbeatInterval(Duration.ofMillis(25)) |
| 116 | .withElectionTimeout(Duration.ofMillis(50)) |
| 117 | .withSessionTimeout(Duration.ofMillis(100)) |
| 118 | .build(); |
| 119 | copycatServers.add(server); |
| 120 | return server; |
| 121 | } |
| 122 | |
| 123 | @Before |
| 124 | @After |
| 125 | public void clearTests() throws Exception { |
| 126 | registry = new LocalServerRegistry(); |
| 127 | members = new ArrayList<>(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 128 | |
| 129 | CompletableFuture<Void> closeClients = |
| 130 | CompletableFuture.allOf(atomixClients.stream() |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 131 | .map(AtomixClient::close) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 132 | .toArray(CompletableFuture[]::new)); |
| 133 | |
| 134 | closeClients.thenCompose(v -> CompletableFuture.allOf(copycatServers.stream() |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 135 | .map(CopycatServer::shutdown) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 136 | .toArray(CompletableFuture[]::new))).join(); |
| 137 | |
| 138 | deleteDirectory(TEST_DIR); |
| 139 | |
| 140 | atomixClients = new ArrayList<>(); |
| 141 | |
| 142 | copycatServers = new ArrayList<>(); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Deletes a directory recursively. |
| 147 | */ |
| 148 | private void deleteDirectory(File directory) throws IOException { |
| 149 | if (directory.exists()) { |
| 150 | File[] files = directory.listFiles(); |
| 151 | if (files != null) { |
| 152 | for (File file : files) { |
| 153 | if (file.isDirectory()) { |
| 154 | deleteDirectory(file); |
| 155 | } else { |
| 156 | Files.delete(file.toPath()); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | Files.delete(directory.toPath()); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Creates a Atomix client. |
| 166 | */ |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 167 | protected AtomixClient createAtomixClient() { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 168 | CountDownLatch latch = new CountDownLatch(1); |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 169 | AtomixClient client = AtomixClient.builder() |
Aaron Kruglikov | c0c27c0 | 2016-06-07 16:05:00 -0700 | [diff] [blame] | 170 | .withTransport(NettyTransport.builder().withThreads(1).build()) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 171 | .withSerializer(serializer.clone()) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 172 | .build(); |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 173 | client.connect(members).thenRun(latch::countDown); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 174 | atomixClients.add(client); |
| 175 | Uninterruptibles.awaitUninterruptibly(latch); |
| 176 | return client; |
| 177 | } |
| 178 | } |