2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.sharding;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.mockito.Mockito.doReturn;
16 import static org.opendaylight.controller.cluster.datastore.IntegrationTestKit.findLocalShard;
17 import static org.opendaylight.controller.cluster.datastore.IntegrationTestKit.waitUntilShardIsDown;
19 import akka.actor.ActorRef;
20 import akka.actor.ActorSystem;
21 import akka.actor.Address;
22 import akka.actor.AddressFromURIString;
23 import akka.cluster.Cluster;
24 import akka.testkit.JavaTestKit;
25 import com.google.common.collect.Lists;
26 import com.typesafe.config.ConfigFactory;
27 import java.util.Collections;
28 import java.util.HashSet;
30 import org.junit.After;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mockito;
35 import org.opendaylight.controller.cluster.ActorSystemProvider;
36 import org.opendaylight.controller.cluster.datastore.AbstractTest;
37 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
38 import org.opendaylight.controller.cluster.datastore.DatastoreContext.Builder;
39 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
40 import org.opendaylight.controller.cluster.datastore.IntegrationTestKit;
41 import org.opendaylight.controller.cluster.datastore.utils.ClusterUtils;
42 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
43 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
44 import org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration;
45 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
46 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
47 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
48 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
49 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
50 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
51 import org.opendaylight.mdsal.dom.api.DOMDataTreeShardingConflictException;
52 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
53 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
54 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
55 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
59 public class DistributedShardedDOMDataTreeRemotingTest extends AbstractTest {
61 private static final Logger LOG = LoggerFactory.getLogger(DistributedShardedDOMDataTreeRemotingTest.class);
63 private static final Address MEMBER_1_ADDRESS =
64 AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558");
66 private static final DOMDataTreeIdentifier TEST_ID =
67 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH);
69 private static final String MODULE_SHARDS_CONFIG = "module-shards-default.conf";
71 private ActorSystem leaderSystem;
72 private ActorSystem followerSystem;
75 private final Builder leaderDatastoreContextBuilder =
76 DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5);
78 private final DatastoreContext.Builder followerDatastoreContextBuilder =
79 DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5);
81 private DistributedDataStore leaderConfigDatastore;
82 private DistributedDataStore leaderOperDatastore;
84 private DistributedDataStore followerConfigDatastore;
85 private DistributedDataStore followerOperDatastore;
88 private IntegrationTestKit followerTestKit;
89 private IntegrationTestKit leaderTestKit;
90 private DistributedShardedDOMDataTree leaderShardFactory;
92 private DistributedShardedDOMDataTree followerShardFactory;
93 private ActorSystemProvider leaderSystemProvider;
94 private ActorSystemProvider followerSystemProvider;
98 InMemoryJournal.clear();
99 InMemorySnapshotStore.clear();
101 leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
102 Cluster.get(leaderSystem).join(MEMBER_1_ADDRESS);
104 followerSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member2"));
105 Cluster.get(followerSystem).join(MEMBER_1_ADDRESS);
107 leaderSystemProvider = Mockito.mock(ActorSystemProvider.class);
108 doReturn(leaderSystem).when(leaderSystemProvider).getActorSystem();
110 followerSystemProvider = Mockito.mock(ActorSystemProvider.class);
111 doReturn(followerSystem).when(followerSystemProvider).getActorSystem();
116 public void tearDown() {
117 if (leaderConfigDatastore != null) {
118 leaderConfigDatastore.close();
120 if (leaderOperDatastore != null) {
121 leaderOperDatastore.close();
124 if (followerConfigDatastore != null) {
125 followerConfigDatastore.close();
127 if (followerOperDatastore != null) {
128 followerOperDatastore.close();
131 JavaTestKit.shutdownActorSystem(leaderSystem, null, Boolean.TRUE);
132 JavaTestKit.shutdownActorSystem(followerSystem, null, Boolean.TRUE);
134 InMemoryJournal.clear();
135 InMemorySnapshotStore.clear();
138 private void initEmptyDatastores() throws Exception {
139 leaderTestKit = new IntegrationTestKit(leaderSystem, leaderDatastoreContextBuilder);
141 leaderConfigDatastore = leaderTestKit.setupDistributedDataStore(
142 "config", MODULE_SHARDS_CONFIG, true,
143 SchemaContextHelper.distributedShardedDOMDataTreeSchemaContext());
144 leaderOperDatastore = leaderTestKit.setupDistributedDataStore(
145 "operational", MODULE_SHARDS_CONFIG, true,
146 SchemaContextHelper.distributedShardedDOMDataTreeSchemaContext());
148 leaderShardFactory = new DistributedShardedDOMDataTree(leaderSystemProvider,
150 leaderConfigDatastore);
152 followerTestKit = new IntegrationTestKit(followerSystem, followerDatastoreContextBuilder);
154 followerConfigDatastore = followerTestKit.setupDistributedDataStore(
155 "config", MODULE_SHARDS_CONFIG, true, SchemaContextHelper.distributedShardedDOMDataTreeSchemaContext());
156 followerOperDatastore = followerTestKit.setupDistributedDataStore(
157 "operational", MODULE_SHARDS_CONFIG, true,
158 SchemaContextHelper.distributedShardedDOMDataTreeSchemaContext());
160 followerShardFactory = new DistributedShardedDOMDataTree(followerSystemProvider,
161 followerOperDatastore,
162 followerConfigDatastore);
164 followerTestKit.waitForMembersUp("member-1");
166 leaderShardFactory.init();
167 followerShardFactory.init();
169 leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(),
170 ClusterUtils.getCleanShardName(YangInstanceIdentifier.EMPTY));
172 leaderTestKit.waitUntilLeader(leaderOperDatastore.getActorContext(),
173 ClusterUtils.getCleanShardName(YangInstanceIdentifier.EMPTY));
177 public void testProducerRegistrations() throws Exception {
178 initEmptyDatastores();
180 leaderTestKit.waitForMembersUp("member-2");
182 // TODO refactor shard creation and verification to own method
183 final DistributedShardRegistration shardRegistration =
184 waitOnAsyncTask(leaderShardFactory.createDistributedShard(
185 TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME, AbstractTest.MEMBER_2_NAME)),
186 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
188 leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(),
189 ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()));
191 final ActorRef leaderShardManager = leaderConfigDatastore.getActorContext().getShardManager();
193 assertNotNull(findLocalShard(leaderConfigDatastore.getActorContext(),
194 ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier())));
196 assertNotNull(findLocalShard(followerConfigDatastore.getActorContext(),
197 ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier())));
199 final Set<String> peers = new HashSet<>();
200 IntegrationTestKit.verifyShardState(leaderConfigDatastore,
201 ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()), onDemandShardState ->
202 peers.addAll(onDemandShardState.getPeerAddresses().values()));
203 assertEquals(peers.size(), 1);
205 final DOMDataTreeProducer producer = leaderShardFactory.createProducer(Collections.singleton(TEST_ID));
207 followerShardFactory.createProducer(Collections.singleton(TEST_ID));
208 fail("Producer should be already registered on the other node");
209 } catch (final IllegalArgumentException e) {
210 assertTrue(e.getMessage().contains("is attached to producer"));
215 final DOMDataTreeProducer followerProducer =
216 followerShardFactory.createProducer(Collections.singleton(TEST_ID));
218 leaderShardFactory.createProducer(Collections.singleton(TEST_ID));
219 fail("Producer should be already registered on the other node");
220 } catch (final IllegalArgumentException e) {
221 assertTrue(e.getMessage().contains("is attached to producer"));
224 followerProducer.close();
225 // try to create a shard on an already registered prefix on follower
227 waitOnAsyncTask(followerShardFactory.createDistributedShard(
228 TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME, AbstractTest.MEMBER_2_NAME)),
229 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
230 fail("This prefix already should have a shard registration that was forwarded from the other node");
231 } catch (final DOMDataTreeShardingConflictException e) {
232 assertTrue(e.getMessage().contains("is already occupied by another shard"));
235 shardRegistration.close().toCompletableFuture().get();
239 public void testWriteIntoMultipleShards() throws Exception {
240 initEmptyDatastores();
242 leaderTestKit.waitForMembersUp("member-2");
244 LOG.debug("registering first shard");
245 final DistributedShardRegistration shardRegistration =
246 waitOnAsyncTask(leaderShardFactory.createDistributedShard(
247 TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME, AbstractTest.MEMBER_2_NAME)),
248 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
251 leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(),
252 ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()));
253 findLocalShard(followerConfigDatastore.getActorContext(),
254 ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()));
256 final Set<String> peers = new HashSet<>();
257 IntegrationTestKit.verifyShardState(leaderConfigDatastore,
258 ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()), onDemandShardState ->
259 peers.addAll(onDemandShardState.getPeerAddresses().values()));
260 assertEquals(peers.size(), 1);
262 LOG.debug("Got after waiting for nonleader");
263 final DOMDataTreeProducer producer = leaderShardFactory.createProducer(Collections.singleton(TEST_ID));
265 final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(true);
266 final DOMDataTreeWriteCursor cursor = tx.createCursor(TEST_ID);
267 Assert.assertNotNull(cursor);
268 final YangInstanceIdentifier nameId =
269 YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(TestModel.NAME_QNAME).build();
270 cursor.write(nameId.getLastPathArgument(),
271 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(
272 new NodeIdentifier(TestModel.NAME_QNAME)).withValue("Test Value").build());
275 LOG.warn("Got to pre submit");
277 tx.submit().checkedGet();
279 shardRegistration.close().toCompletableFuture().get();
283 public void testMultipleShardRegistrations() throws Exception {
284 initEmptyDatastores();
286 final DistributedShardRegistration reg1 = waitOnAsyncTask(leaderShardFactory.createDistributedShard(
287 TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME, AbstractTest.MEMBER_2_NAME)),
288 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
290 final DistributedShardRegistration reg2 = waitOnAsyncTask(leaderShardFactory.createDistributedShard(
291 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, TestModel.OUTER_CONTAINER_PATH),
292 Lists.newArrayList(AbstractTest.MEMBER_NAME, AbstractTest.MEMBER_2_NAME)),
293 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
295 final DistributedShardRegistration reg3 = waitOnAsyncTask(leaderShardFactory.createDistributedShard(
296 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, TestModel.INNER_LIST_PATH),
297 Lists.newArrayList(AbstractTest.MEMBER_NAME, AbstractTest.MEMBER_2_NAME)),
298 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
300 final DistributedShardRegistration reg4 = waitOnAsyncTask(leaderShardFactory.createDistributedShard(
301 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, TestModel.JUNK_PATH),
302 Lists.newArrayList(AbstractTest.MEMBER_NAME, AbstractTest.MEMBER_2_NAME)),
303 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
305 leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(),
306 ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
307 leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(),
308 ClusterUtils.getCleanShardName(TestModel.OUTER_CONTAINER_PATH));
309 leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(),
310 ClusterUtils.getCleanShardName(TestModel.INNER_LIST_PATH));
311 leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(),
312 ClusterUtils.getCleanShardName(TestModel.JUNK_PATH));
314 // check leader has local shards
315 assertNotNull(findLocalShard(leaderConfigDatastore.getActorContext(),
316 ClusterUtils.getCleanShardName(TestModel.TEST_PATH)));
318 assertNotNull(findLocalShard(leaderConfigDatastore.getActorContext(),
319 ClusterUtils.getCleanShardName(TestModel.OUTER_CONTAINER_PATH)));
321 assertNotNull(findLocalShard(leaderConfigDatastore.getActorContext(),
322 ClusterUtils.getCleanShardName(TestModel.INNER_LIST_PATH)));
324 assertNotNull(findLocalShard(leaderConfigDatastore.getActorContext(),
325 ClusterUtils.getCleanShardName(TestModel.JUNK_PATH)));
327 // check follower has local shards
328 assertNotNull(findLocalShard(followerConfigDatastore.getActorContext(),
329 ClusterUtils.getCleanShardName(TestModel.TEST_PATH)));
331 assertNotNull(findLocalShard(followerConfigDatastore.getActorContext(),
332 ClusterUtils.getCleanShardName(TestModel.OUTER_CONTAINER_PATH)));
334 assertNotNull(findLocalShard(followerConfigDatastore.getActorContext(),
335 ClusterUtils.getCleanShardName(TestModel.INNER_LIST_PATH)));
337 assertNotNull(findLocalShard(followerConfigDatastore.getActorContext(),
338 ClusterUtils.getCleanShardName(TestModel.JUNK_PATH)));
340 LOG.debug("Closing registrations");
342 reg1.close().toCompletableFuture().get();
343 reg2.close().toCompletableFuture().get();
344 reg3.close().toCompletableFuture().get();
345 reg4.close().toCompletableFuture().get();
347 waitUntilShardIsDown(leaderConfigDatastore.getActorContext(),
348 ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
350 waitUntilShardIsDown(leaderConfigDatastore.getActorContext(),
351 ClusterUtils.getCleanShardName(TestModel.OUTER_CONTAINER_PATH));
353 waitUntilShardIsDown(leaderConfigDatastore.getActorContext(),
354 ClusterUtils.getCleanShardName(TestModel.INNER_LIST_PATH));
356 waitUntilShardIsDown(leaderConfigDatastore.getActorContext(),
357 ClusterUtils.getCleanShardName(TestModel.JUNK_PATH));
359 LOG.debug("All leader shards gone");
361 waitUntilShardIsDown(followerConfigDatastore.getActorContext(),
362 ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
364 waitUntilShardIsDown(followerConfigDatastore.getActorContext(),
365 ClusterUtils.getCleanShardName(TestModel.OUTER_CONTAINER_PATH));
367 waitUntilShardIsDown(followerConfigDatastore.getActorContext(),
368 ClusterUtils.getCleanShardName(TestModel.INNER_LIST_PATH));
370 waitUntilShardIsDown(followerConfigDatastore.getActorContext(),
371 ClusterUtils.getCleanShardName(TestModel.JUNK_PATH));
373 LOG.debug("All follower shards gone");
377 public void testMultipleRegistrationsAtOnePrefix() throws Exception {
378 initEmptyDatastores();
380 for (int i = 0; i < 10; i++) {
381 LOG.debug("Round {}", i);
382 final DistributedShardRegistration reg1 = waitOnAsyncTask(leaderShardFactory.createDistributedShard(
383 TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME, AbstractTest.MEMBER_2_NAME)),
384 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
386 leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(),
387 ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
389 assertNotNull(findLocalShard(leaderConfigDatastore.getActorContext(),
390 ClusterUtils.getCleanShardName(TestModel.TEST_PATH)));
392 assertNotNull(findLocalShard(followerConfigDatastore.getActorContext(),
393 ClusterUtils.getCleanShardName(TestModel.TEST_PATH)));
396 final Set<String> peers = new HashSet<>();
397 IntegrationTestKit.verifyShardState(leaderConfigDatastore,
398 ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()), onDemandShardState ->
399 peers.addAll(onDemandShardState.getPeerAddresses().values()));
400 assertEquals(peers.size(), 1);
402 waitOnAsyncTask(reg1.close(), DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
404 waitUntilShardIsDown(leaderConfigDatastore.getActorContext(),
405 ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
407 waitUntilShardIsDown(followerConfigDatastore.getActorContext(),
408 ClusterUtils.getCleanShardName(TestModel.TEST_PATH));