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.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.mockito.Mockito.doReturn;
15 import static org.opendaylight.controller.cluster.datastore.IntegrationTestKit.findLocalShard;
16 import static org.opendaylight.controller.cluster.datastore.IntegrationTestKit.waitUntilShardIsDown;
18 import akka.actor.ActorRef;
19 import akka.actor.ActorSystem;
20 import akka.actor.Address;
21 import akka.actor.AddressFromURIString;
22 import akka.cluster.Cluster;
23 import akka.testkit.JavaTestKit;
24 import com.google.common.collect.Lists;
25 import com.typesafe.config.ConfigFactory;
26 import java.util.Collections;
27 import org.junit.After;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mockito;
32 import org.opendaylight.controller.cluster.ActorSystemProvider;
33 import org.opendaylight.controller.cluster.datastore.AbstractTest;
34 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
35 import org.opendaylight.controller.cluster.datastore.DatastoreContext.Builder;
36 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
37 import org.opendaylight.controller.cluster.datastore.IntegrationTestKit;
38 import org.opendaylight.controller.cluster.datastore.utils.ClusterUtils;
39 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
40 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
41 import org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration;
42 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
43 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
44 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
45 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
46 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
47 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
48 import org.opendaylight.mdsal.dom.api.DOMDataTreeShardingConflictException;
49 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
52 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 public class DistributedShardedDOMDataTreeRemotingTest extends AbstractTest {
58 private static final Logger LOG = LoggerFactory.getLogger(DistributedShardedDOMDataTreeRemotingTest.class);
60 private static final Address MEMBER_1_ADDRESS =
61 AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558");
63 private static final DOMDataTreeIdentifier TEST_ID =
64 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH);
66 private static final String MODULE_SHARDS_CONFIG = "module-shards-cars-member-1-and-2.conf";
68 private ActorSystem leaderSystem;
69 private ActorSystem followerSystem;
72 private final Builder leaderDatastoreContextBuilder =
73 DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5)
75 org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION);
77 private final DatastoreContext.Builder followerDatastoreContextBuilder =
78 DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5)
80 org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION);
82 private DistributedDataStore leaderConfigDatastore;
83 private DistributedDataStore leaderOperDatastore;
85 private DistributedDataStore followerConfigDatastore;
86 private DistributedDataStore followerOperDatastore;
89 private IntegrationTestKit followerTestKit;
90 private IntegrationTestKit leaderTestKit;
91 private DistributedShardedDOMDataTree leaderShardFactory;
93 private DistributedShardedDOMDataTree followerShardFactory;
94 private ActorSystemProvider leaderSystemProvider;
95 private ActorSystemProvider followerSystemProvider;
99 InMemoryJournal.clear();
100 InMemorySnapshotStore.clear();
102 leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
103 Cluster.get(leaderSystem).join(MEMBER_1_ADDRESS);
105 followerSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member2"));
106 Cluster.get(followerSystem).join(MEMBER_1_ADDRESS);
108 leaderSystemProvider = Mockito.mock(ActorSystemProvider.class);
109 doReturn(leaderSystem).when(leaderSystemProvider).getActorSystem();
111 followerSystemProvider = Mockito.mock(ActorSystemProvider.class);
112 doReturn(followerSystem).when(followerSystemProvider).getActorSystem();
117 public void tearDown() {
118 if (leaderConfigDatastore != null) {
119 leaderConfigDatastore.close();
121 if (leaderOperDatastore != null) {
122 leaderOperDatastore.close();
125 if (followerConfigDatastore != null) {
126 followerConfigDatastore.close();
128 if (followerOperDatastore != null) {
129 followerOperDatastore.close();
132 JavaTestKit.shutdownActorSystem(leaderSystem);
133 JavaTestKit.shutdownActorSystem(followerSystem);
135 InMemoryJournal.clear();
136 InMemorySnapshotStore.clear();
139 private void initEmptyDatastores() throws Exception {
140 leaderTestKit = new IntegrationTestKit(leaderSystem, leaderDatastoreContextBuilder);
142 leaderConfigDatastore = leaderTestKit.setupDistributedDataStore(
143 "config", MODULE_SHARDS_CONFIG, true,
144 SchemaContextHelper.distributedShardedDOMDataTreeSchemaContext());
145 leaderOperDatastore = leaderTestKit.setupDistributedDataStore(
146 "operational", MODULE_SHARDS_CONFIG, true,
147 SchemaContextHelper.distributedShardedDOMDataTreeSchemaContext());
149 leaderShardFactory = new DistributedShardedDOMDataTree(leaderSystemProvider,
151 leaderConfigDatastore);
153 followerTestKit = new IntegrationTestKit(followerSystem, followerDatastoreContextBuilder);
155 followerConfigDatastore = followerTestKit.setupDistributedDataStore(
156 "config", MODULE_SHARDS_CONFIG, true, SchemaContextHelper.distributedShardedDOMDataTreeSchemaContext());
157 followerOperDatastore = followerTestKit.setupDistributedDataStore(
158 "operational", MODULE_SHARDS_CONFIG, true,
159 SchemaContextHelper.distributedShardedDOMDataTreeSchemaContext());
161 followerShardFactory = new DistributedShardedDOMDataTree(followerSystemProvider,
162 followerOperDatastore,
163 followerConfigDatastore);
165 leaderShardFactory.init();
166 followerShardFactory.init();
168 leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(),
169 ClusterUtils.getCleanShardName(YangInstanceIdentifier.EMPTY));
171 leaderTestKit.waitUntilLeader(leaderOperDatastore.getActorContext(),
172 ClusterUtils.getCleanShardName(YangInstanceIdentifier.EMPTY));
177 public void testProducerRegistrations() throws Exception {
178 initEmptyDatastores();
180 leaderTestKit.waitForMembersUp("member-2");
182 final DistributedShardRegistration shardRegistration =
183 waitOnAsyncTask(leaderShardFactory.createDistributedShard(
184 TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME, AbstractTest.MEMBER_2_NAME)),
185 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
187 leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(),
188 ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()));
190 final ActorRef leaderShardManager = leaderConfigDatastore.getActorContext().getShardManager();
192 assertNotNull(findLocalShard(leaderConfigDatastore.getActorContext(),
193 ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier())));
195 assertNotNull(findLocalShard(followerConfigDatastore.getActorContext(),
196 ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier())));
198 final DOMDataTreeProducer producer = leaderShardFactory.createProducer(Collections.singleton(TEST_ID));
200 followerShardFactory.createProducer(Collections.singleton(TEST_ID));
201 fail("Producer should be already registered on the other node");
202 } catch (final IllegalArgumentException e) {
203 assertTrue(e.getMessage().contains("is attached to producer"));
208 final DOMDataTreeProducer followerProducer =
209 followerShardFactory.createProducer(Collections.singleton(TEST_ID));
211 leaderShardFactory.createProducer(Collections.singleton(TEST_ID));
212 fail("Producer should be already registered on the other node");
213 } catch (final IllegalArgumentException e) {
214 assertTrue(e.getMessage().contains("is attached to producer"));
217 followerProducer.close();
218 // try to create a shard on an already registered prefix on follower
220 waitOnAsyncTask(followerShardFactory.createDistributedShard(
221 TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME, AbstractTest.MEMBER_2_NAME)),
222 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
223 fail("This prefix already should have a shard registration that was forwarded from the other node");
224 } catch (final DOMDataTreeShardingConflictException e) {
225 assertTrue(e.getMessage().contains("is already occupied by another shard"));
228 shardRegistration.close().toCompletableFuture().get();
232 public void testWriteIntoMultipleShards() throws Exception {
233 initEmptyDatastores();
235 leaderTestKit.waitForMembersUp("member-2");
237 LOG.debug("registering first shard");
238 final DistributedShardRegistration shardRegistration =
239 waitOnAsyncTask(leaderShardFactory.createDistributedShard(
240 TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME, AbstractTest.MEMBER_2_NAME)),
241 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
244 leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(),
245 ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()));
246 findLocalShard(followerConfigDatastore.getActorContext(),
247 ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()));
249 LOG.debug("Got after waiting for nonleader");
250 final DOMDataTreeProducer producer = leaderShardFactory.createProducer(Collections.singleton(TEST_ID));
252 final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(true);
253 final DOMDataTreeWriteCursor cursor = tx.createCursor(TEST_ID);
254 Assert.assertNotNull(cursor);
255 final YangInstanceIdentifier nameId =
256 YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(TestModel.NAME_QNAME).build();
257 cursor.write(nameId.getLastPathArgument(),
258 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(
259 new NodeIdentifier(TestModel.NAME_QNAME)).withValue("Test Value").build());
262 LOG.warn("Got to pre submit");
264 tx.submit().checkedGet();
266 shardRegistration.close().toCompletableFuture().get();
270 public void testMultipleShardRegistrations() throws Exception {
271 initEmptyDatastores();
273 final DistributedShardRegistration reg1 = waitOnAsyncTask(leaderShardFactory.createDistributedShard(
274 TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME, AbstractTest.MEMBER_2_NAME)),
275 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
277 final DistributedShardRegistration reg2 = waitOnAsyncTask(leaderShardFactory.createDistributedShard(
278 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, TestModel.OUTER_CONTAINER_PATH),
279 Lists.newArrayList(AbstractTest.MEMBER_NAME, AbstractTest.MEMBER_2_NAME)),
280 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
282 final DistributedShardRegistration reg3 = waitOnAsyncTask(leaderShardFactory.createDistributedShard(
283 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, TestModel.INNER_LIST_PATH),
284 Lists.newArrayList(AbstractTest.MEMBER_NAME, AbstractTest.MEMBER_2_NAME)),
285 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
287 final DistributedShardRegistration reg4 = waitOnAsyncTask(leaderShardFactory.createDistributedShard(
288 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, TestModel.JUNK_PATH),
289 Lists.newArrayList(AbstractTest.MEMBER_NAME, AbstractTest.MEMBER_2_NAME)),
290 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
292 leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(),
293 ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
294 leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(),
295 ClusterUtils.getCleanShardName(TestModel.OUTER_CONTAINER_PATH));
296 leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(),
297 ClusterUtils.getCleanShardName(TestModel.INNER_LIST_PATH));
298 leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(),
299 ClusterUtils.getCleanShardName(TestModel.JUNK_PATH));
301 // check leader has local shards
302 assertNotNull(findLocalShard(leaderConfigDatastore.getActorContext(),
303 ClusterUtils.getCleanShardName(TestModel.TEST_PATH)));
305 assertNotNull(findLocalShard(leaderConfigDatastore.getActorContext(),
306 ClusterUtils.getCleanShardName(TestModel.OUTER_CONTAINER_PATH)));
308 assertNotNull(findLocalShard(leaderConfigDatastore.getActorContext(),
309 ClusterUtils.getCleanShardName(TestModel.INNER_LIST_PATH)));
311 assertNotNull(findLocalShard(leaderConfigDatastore.getActorContext(),
312 ClusterUtils.getCleanShardName(TestModel.JUNK_PATH)));
314 // check follower has local shards
315 assertNotNull(findLocalShard(followerConfigDatastore.getActorContext(),
316 ClusterUtils.getCleanShardName(TestModel.TEST_PATH)));
318 assertNotNull(findLocalShard(followerConfigDatastore.getActorContext(),
319 ClusterUtils.getCleanShardName(TestModel.OUTER_CONTAINER_PATH)));
321 assertNotNull(findLocalShard(followerConfigDatastore.getActorContext(),
322 ClusterUtils.getCleanShardName(TestModel.INNER_LIST_PATH)));
324 assertNotNull(findLocalShard(followerConfigDatastore.getActorContext(),
325 ClusterUtils.getCleanShardName(TestModel.JUNK_PATH)));
328 LOG.debug("Closing registrations");
330 reg1.close().toCompletableFuture().get();
331 reg2.close().toCompletableFuture().get();
332 reg3.close().toCompletableFuture().get();
333 reg4.close().toCompletableFuture().get();
335 waitUntilShardIsDown(leaderConfigDatastore.getActorContext(),
336 ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
338 waitUntilShardIsDown(leaderConfigDatastore.getActorContext(),
339 ClusterUtils.getCleanShardName(TestModel.OUTER_CONTAINER_PATH));
341 waitUntilShardIsDown(leaderConfigDatastore.getActorContext(),
342 ClusterUtils.getCleanShardName(TestModel.INNER_LIST_PATH));
344 waitUntilShardIsDown(leaderConfigDatastore.getActorContext(),
345 ClusterUtils.getCleanShardName(TestModel.JUNK_PATH));
347 LOG.debug("All leader shards gone");
349 waitUntilShardIsDown(followerConfigDatastore.getActorContext(),
350 ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
352 waitUntilShardIsDown(followerConfigDatastore.getActorContext(),
353 ClusterUtils.getCleanShardName(TestModel.OUTER_CONTAINER_PATH));
355 waitUntilShardIsDown(followerConfigDatastore.getActorContext(),
356 ClusterUtils.getCleanShardName(TestModel.INNER_LIST_PATH));
358 waitUntilShardIsDown(followerConfigDatastore.getActorContext(),
359 ClusterUtils.getCleanShardName(TestModel.JUNK_PATH));
361 LOG.debug("All follower shards gone");
365 public void testMultipleRegistrationsAtOnePrefix() throws Exception {
366 initEmptyDatastores();
368 for (int i = 0; i < 10; i++) {
369 LOG.debug("Round {}", i);
370 final DistributedShardRegistration reg1 = waitOnAsyncTask(leaderShardFactory.createDistributedShard(
371 TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME, AbstractTest.MEMBER_2_NAME)),
372 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
374 leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(),
375 ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
377 assertNotNull(findLocalShard(leaderConfigDatastore.getActorContext(),
378 ClusterUtils.getCleanShardName(TestModel.TEST_PATH)));
380 assertNotNull(findLocalShard(followerConfigDatastore.getActorContext(),
381 ClusterUtils.getCleanShardName(TestModel.TEST_PATH)));
383 waitOnAsyncTask(reg1.close(), DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
385 waitUntilShardIsDown(leaderConfigDatastore.getActorContext(),
386 ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
388 waitUntilShardIsDown(followerConfigDatastore.getActorContext(),
389 ClusterUtils.getCleanShardName(TestModel.TEST_PATH));