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