Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 18 | import java.io.File; |
| 19 | import java.io.IOException; |
| 20 | import java.nio.file.FileVisitResult; |
| 21 | import java.nio.file.Files; |
| 22 | import java.nio.file.Path; |
| 23 | import java.nio.file.Paths; |
| 24 | import java.nio.file.SimpleFileVisitor; |
| 25 | import java.nio.file.attribute.BasicFileAttributes; |
| 26 | import java.time.Instant; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 27 | import java.util.ArrayList; |
| 28 | import java.util.List; |
| 29 | import java.util.concurrent.CompletableFuture; |
| 30 | import java.util.concurrent.CountDownLatch; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 31 | import java.util.concurrent.TimeUnit; |
| 32 | import java.util.function.Consumer; |
| 33 | import java.util.stream.Collectors; |
| 34 | |
| 35 | import com.google.common.collect.Lists; |
| 36 | import io.atomix.protocols.raft.RaftClient; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 37 | import io.atomix.protocols.raft.RaftServer; |
| 38 | import io.atomix.protocols.raft.ReadConsistency; |
| 39 | import io.atomix.protocols.raft.cluster.MemberId; |
| 40 | import io.atomix.protocols.raft.cluster.RaftMember; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 41 | import io.atomix.protocols.raft.proxy.CommunicationStrategy; |
| 42 | import io.atomix.protocols.raft.proxy.RaftProxy; |
| 43 | import io.atomix.protocols.raft.service.RaftService; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 44 | import io.atomix.protocols.raft.storage.RaftStorage; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 45 | import io.atomix.storage.StorageLevel; |
| 46 | import org.junit.After; |
| 47 | import org.junit.Before; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 48 | import org.onosproject.cluster.NodeId; |
| 49 | import org.onosproject.cluster.PartitionId; |
| 50 | import org.onosproject.store.primitives.impl.RaftClientCommunicator; |
| 51 | import org.onosproject.store.primitives.impl.RaftServerCommunicator; |
Jordan Halterman | c957199 | 2017-11-03 13:43:19 -0700 | [diff] [blame] | 52 | import org.onosproject.store.primitives.impl.StorageNamespaces; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 53 | import org.onosproject.store.service.Serializer; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 54 | |
| 55 | /** |
Aaron Kruglikov | b5a41e5 | 2016-06-23 15:37:41 -0700 | [diff] [blame] | 56 | * Base class for various Atomix tests. |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 57 | * |
| 58 | * @param <T> the Raft primitive type being tested |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 59 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 60 | public abstract class AtomixTestBase<T extends AbstractRaftPrimitive> { |
| 61 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 62 | protected TestClusterCommunicationServiceFactory communicationServiceFactory; |
| 63 | protected List<RaftMember> members = Lists.newCopyOnWriteArrayList(); |
| 64 | protected List<RaftClient> clients = Lists.newCopyOnWriteArrayList(); |
| 65 | protected List<RaftServer> servers = Lists.newCopyOnWriteArrayList(); |
| 66 | protected int nextId; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 67 | |
| 68 | /** |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 69 | * Creates the primitive service. |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 70 | * |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 71 | * @return the primitive service |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 72 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 73 | protected abstract RaftService createService(); |
| 74 | |
| 75 | /** |
| 76 | * Creates a new primitive. |
| 77 | * |
| 78 | * @param name the primitive name |
| 79 | * @return the primitive instance |
| 80 | */ |
| 81 | protected T newPrimitive(String name) { |
| 82 | RaftClient client = createClient(); |
| 83 | RaftProxy proxy = client.newProxyBuilder() |
| 84 | .withName(name) |
| 85 | .withServiceType("test") |
| 86 | .withReadConsistency(readConsistency()) |
| 87 | .withCommunicationStrategy(communicationStrategy()) |
| 88 | .build() |
| 89 | .open() |
| 90 | .join(); |
| 91 | return createPrimitive(proxy); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Creates a new primitive instance. |
| 96 | * |
| 97 | * @param proxy the primitive proxy |
| 98 | * @return the primitive instance |
| 99 | */ |
| 100 | protected abstract T createPrimitive(RaftProxy proxy); |
| 101 | |
| 102 | /** |
| 103 | * Returns the proxy read consistency. |
| 104 | * |
| 105 | * @return the primitive read consistency |
| 106 | */ |
| 107 | protected ReadConsistency readConsistency() { |
| 108 | return ReadConsistency.LINEARIZABLE; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Returns the proxy communication strategy. |
| 113 | * |
| 114 | * @return the primitive communication strategy |
| 115 | */ |
| 116 | protected CommunicationStrategy communicationStrategy() { |
| 117 | return CommunicationStrategy.LEADER; |
| 118 | } |
| 119 | |
| 120 | @Before |
| 121 | public void prepare() { |
| 122 | members.clear(); |
| 123 | clients.clear(); |
| 124 | servers.clear(); |
| 125 | communicationServiceFactory = new TestClusterCommunicationServiceFactory(); |
| 126 | createServers(3); |
| 127 | } |
| 128 | |
| 129 | @After |
| 130 | public void cleanup() { |
| 131 | shutdown(); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Shuts down clients and servers. |
| 136 | */ |
| 137 | private void shutdown() { |
| 138 | clients.forEach(c -> { |
| 139 | try { |
| 140 | c.close().get(10, TimeUnit.SECONDS); |
| 141 | } catch (Exception e) { |
| 142 | } |
| 143 | }); |
| 144 | |
| 145 | servers.forEach(s -> { |
| 146 | try { |
| 147 | if (s.isRunning()) { |
| 148 | s.shutdown().get(10, TimeUnit.SECONDS); |
| 149 | } |
| 150 | } catch (Exception e) { |
| 151 | } |
| 152 | }); |
| 153 | |
| 154 | Path directory = Paths.get("target/primitives/"); |
| 155 | if (Files.exists(directory)) { |
| 156 | try { |
| 157 | Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { |
| 158 | @Override |
| 159 | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 160 | Files.delete(file); |
| 161 | return FileVisitResult.CONTINUE; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { |
| 166 | Files.delete(dir); |
| 167 | return FileVisitResult.CONTINUE; |
| 168 | } |
| 169 | }); |
| 170 | } catch (IOException e) { |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Returns the next unique member identifier. |
| 177 | * |
| 178 | * @return The next unique member identifier. |
| 179 | */ |
| 180 | private MemberId nextMemberId() { |
| 181 | return MemberId.from(String.valueOf(++nextId)); |
| 182 | } |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 183 | |
| 184 | /** |
| 185 | * Returns the next server address. |
| 186 | * |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 187 | * @param type The startup member type. |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 188 | * @return The next server address. |
| 189 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 190 | private RaftMember nextMember(RaftMember.Type type) { |
| 191 | return new TestMember(nextMemberId(), type); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /** |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 195 | * Creates a set of Raft servers. |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 196 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 197 | protected List<RaftServer> createServers(int nodes) { |
| 198 | List<RaftServer> servers = new ArrayList<>(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 199 | |
| 200 | for (int i = 0; i < nodes; i++) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 201 | members.add(nextMember(RaftMember.Type.ACTIVE)); |
| 202 | } |
| 203 | |
| 204 | CountDownLatch latch = new CountDownLatch(nodes); |
| 205 | for (int i = 0; i < nodes; i++) { |
| 206 | RaftServer server = createServer(members.get(i)); |
| 207 | server.bootstrap(members.stream().map(RaftMember::memberId).collect(Collectors.toList())) |
| 208 | .thenRun(latch::countDown); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 209 | servers.add(server); |
| 210 | } |
| 211 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 212 | try { |
| 213 | latch.await(30000, TimeUnit.MILLISECONDS); |
| 214 | } catch (InterruptedException e) { |
| 215 | throw new RuntimeException(e); |
| 216 | } |
| 217 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 218 | return servers; |
| 219 | } |
| 220 | |
| 221 | /** |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 222 | * Creates a Raft server. |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 223 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 224 | private RaftServer createServer(RaftMember member) { |
| 225 | RaftServer.Builder builder = RaftServer.newBuilder(member.memberId()) |
| 226 | .withType(member.getType()) |
| 227 | .withProtocol(new RaftServerCommunicator( |
| 228 | PartitionId.from(1), |
Jordan Halterman | c957199 | 2017-11-03 13:43:19 -0700 | [diff] [blame] | 229 | Serializer.using(StorageNamespaces.RAFT_PROTOCOL), |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 230 | communicationServiceFactory.newCommunicationService(NodeId.nodeId(member.memberId().id())))) |
| 231 | .withStorage(RaftStorage.newBuilder() |
| 232 | .withStorageLevel(StorageLevel.MEMORY) |
| 233 | .withDirectory(new File(String.format("target/primitives/%s", member.memberId()))) |
Jordan Halterman | c957199 | 2017-11-03 13:43:19 -0700 | [diff] [blame] | 234 | .withSerializer(new AtomixSerializerAdapter(Serializer.using(StorageNamespaces.RAFT_STORAGE))) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 235 | .withMaxSegmentSize(1024 * 1024) |
| 236 | .build()) |
| 237 | .addService("test", this::createService); |
| 238 | |
| 239 | RaftServer server = builder.build(); |
| 240 | servers.add(server); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 241 | return server; |
| 242 | } |
| 243 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 244 | /** |
| 245 | * Creates a Raft client. |
| 246 | */ |
| 247 | private RaftClient createClient() { |
| 248 | MemberId memberId = nextMemberId(); |
| 249 | RaftClient client = RaftClient.newBuilder() |
| 250 | .withMemberId(memberId) |
| 251 | .withProtocol(new RaftClientCommunicator( |
| 252 | PartitionId.from(1), |
Jordan Halterman | c957199 | 2017-11-03 13:43:19 -0700 | [diff] [blame] | 253 | Serializer.using(StorageNamespaces.RAFT_PROTOCOL), |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 254 | communicationServiceFactory.newCommunicationService(NodeId.nodeId(memberId.id())))) |
| 255 | .build(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 256 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 257 | client.connect(members.stream().map(RaftMember::memberId).collect(Collectors.toList())).join(); |
| 258 | clients.add(client); |
| 259 | return client; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 262 | /** |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 263 | * Test member. |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 264 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 265 | public static class TestMember implements RaftMember { |
| 266 | private final MemberId memberId; |
| 267 | private final Type type; |
| 268 | |
| 269 | public TestMember(MemberId memberId, Type type) { |
| 270 | this.memberId = memberId; |
| 271 | this.type = type; |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public MemberId memberId() { |
| 276 | return memberId; |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public int hash() { |
| 281 | return memberId.hashCode(); |
| 282 | } |
| 283 | |
| 284 | @Override |
| 285 | public Type getType() { |
| 286 | return type; |
| 287 | } |
| 288 | |
| 289 | @Override |
| 290 | public void addTypeChangeListener(Consumer<Type> listener) { |
| 291 | |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public void removeTypeChangeListener(Consumer<Type> listener) { |
| 296 | |
| 297 | } |
| 298 | |
| 299 | @Override |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 300 | public Instant getLastUpdated() { |
| 301 | return Instant.now(); |
| 302 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 303 | @Override |
| 304 | public CompletableFuture<Void> promote() { |
| 305 | return null; |
| 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public CompletableFuture<Void> promote(Type type) { |
| 310 | return null; |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public CompletableFuture<Void> demote() { |
| 315 | return null; |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public CompletableFuture<Void> demote(Type type) { |
| 320 | return null; |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public CompletableFuture<Void> remove() { |
| 325 | return null; |
| 326 | } |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 327 | } |
| 328 | } |