30abf8c4e230699618771701e3a38ba34ad3512e
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / sharding / DistributedShardedDOMDataTreeRemotingTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.sharding;
10
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;
18
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;
29 import java.util.Set;
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;
58
59 public class DistributedShardedDOMDataTreeRemotingTest extends AbstractTest {
60
61     private static final Logger LOG = LoggerFactory.getLogger(DistributedShardedDOMDataTreeRemotingTest.class);
62
63     private static final Address MEMBER_1_ADDRESS =
64             AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558");
65
66     private static final DOMDataTreeIdentifier TEST_ID =
67             new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH);
68
69     private static final String MODULE_SHARDS_CONFIG = "module-shards-default.conf";
70
71     private ActorSystem leaderSystem;
72     private ActorSystem followerSystem;
73
74
75     private final Builder leaderDatastoreContextBuilder =
76             DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5);
77
78     private final DatastoreContext.Builder followerDatastoreContextBuilder =
79             DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5);
80
81     private DistributedDataStore leaderConfigDatastore;
82     private DistributedDataStore leaderOperDatastore;
83
84     private DistributedDataStore followerConfigDatastore;
85     private DistributedDataStore followerOperDatastore;
86
87
88     private IntegrationTestKit followerTestKit;
89     private IntegrationTestKit leaderTestKit;
90     private DistributedShardedDOMDataTree leaderShardFactory;
91
92     private DistributedShardedDOMDataTree followerShardFactory;
93     private ActorSystemProvider leaderSystemProvider;
94     private ActorSystemProvider followerSystemProvider;
95
96     @Before
97     public void setUp() {
98         InMemoryJournal.clear();
99         InMemorySnapshotStore.clear();
100
101         leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
102         Cluster.get(leaderSystem).join(MEMBER_1_ADDRESS);
103
104         followerSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member2"));
105         Cluster.get(followerSystem).join(MEMBER_1_ADDRESS);
106
107         leaderSystemProvider = Mockito.mock(ActorSystemProvider.class);
108         doReturn(leaderSystem).when(leaderSystemProvider).getActorSystem();
109
110         followerSystemProvider = Mockito.mock(ActorSystemProvider.class);
111         doReturn(followerSystem).when(followerSystemProvider).getActorSystem();
112
113     }
114
115     @After
116     public void tearDown() {
117         if (leaderConfigDatastore != null) {
118             leaderConfigDatastore.close();
119         }
120         if (leaderOperDatastore != null) {
121             leaderOperDatastore.close();
122         }
123
124         if (followerConfigDatastore != null) {
125             followerConfigDatastore.close();
126         }
127         if (followerOperDatastore != null) {
128             followerOperDatastore.close();
129         }
130
131         JavaTestKit.shutdownActorSystem(leaderSystem, null, Boolean.TRUE);
132         JavaTestKit.shutdownActorSystem(followerSystem, null, Boolean.TRUE);
133
134         InMemoryJournal.clear();
135         InMemorySnapshotStore.clear();
136     }
137
138     private void initEmptyDatastores() throws Exception {
139         leaderTestKit = new IntegrationTestKit(leaderSystem, leaderDatastoreContextBuilder);
140
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());
147
148         leaderShardFactory = new DistributedShardedDOMDataTree(leaderSystemProvider,
149                 leaderOperDatastore,
150                 leaderConfigDatastore);
151
152         followerTestKit = new IntegrationTestKit(followerSystem, followerDatastoreContextBuilder);
153
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());
159
160         followerShardFactory = new DistributedShardedDOMDataTree(followerSystemProvider,
161                 followerOperDatastore,
162                 followerConfigDatastore);
163
164         followerTestKit.waitForMembersUp("member-1");
165
166         leaderShardFactory.init();
167         followerShardFactory.init();
168
169         leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(),
170                 ClusterUtils.getCleanShardName(YangInstanceIdentifier.EMPTY));
171
172         leaderTestKit.waitUntilLeader(leaderOperDatastore.getActorContext(),
173                 ClusterUtils.getCleanShardName(YangInstanceIdentifier.EMPTY));
174     }
175
176     @Test
177     public void testProducerRegistrations() throws Exception {
178         initEmptyDatastores();
179
180         leaderTestKit.waitForMembersUp("member-2");
181
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);
187
188         leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(),
189                 ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()));
190
191         final ActorRef leaderShardManager = leaderConfigDatastore.getActorContext().getShardManager();
192
193         assertNotNull(findLocalShard(leaderConfigDatastore.getActorContext(),
194                 ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier())));
195
196         assertNotNull(findLocalShard(followerConfigDatastore.getActorContext(),
197                 ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier())));
198
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);
204
205         final DOMDataTreeProducer producer = leaderShardFactory.createProducer(Collections.singleton(TEST_ID));
206         try {
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"));
211         }
212
213         producer.close();
214
215         final DOMDataTreeProducer followerProducer =
216                 followerShardFactory.createProducer(Collections.singleton(TEST_ID));
217         try {
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"));
222         }
223
224         followerProducer.close();
225         // try to create a shard on an already registered prefix on follower
226         try {
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"));
233         }
234
235         shardRegistration.close().toCompletableFuture().get();
236     }
237
238     @Test
239     public void testWriteIntoMultipleShards() throws Exception {
240         initEmptyDatastores();
241
242         leaderTestKit.waitForMembersUp("member-2");
243
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);
249
250
251         leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(),
252                 ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()));
253         findLocalShard(followerConfigDatastore.getActorContext(),
254                 ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()));
255
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);
261
262         LOG.debug("Got after waiting for nonleader");
263         final DOMDataTreeProducer producer = leaderShardFactory.createProducer(Collections.singleton(TEST_ID));
264
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());
273
274         cursor.close();
275         LOG.warn("Got to pre submit");
276
277         tx.submit().checkedGet();
278
279         shardRegistration.close().toCompletableFuture().get();
280     }
281
282     @Test
283     public void testMultipleShardRegistrations() throws Exception {
284         initEmptyDatastores();
285
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);
289
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);
294
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);
299
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);
304
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));
313
314         // check leader has local shards
315         assertNotNull(findLocalShard(leaderConfigDatastore.getActorContext(),
316                 ClusterUtils.getCleanShardName(TestModel.TEST_PATH)));
317
318         assertNotNull(findLocalShard(leaderConfigDatastore.getActorContext(),
319                 ClusterUtils.getCleanShardName(TestModel.OUTER_CONTAINER_PATH)));
320
321         assertNotNull(findLocalShard(leaderConfigDatastore.getActorContext(),
322                 ClusterUtils.getCleanShardName(TestModel.INNER_LIST_PATH)));
323
324         assertNotNull(findLocalShard(leaderConfigDatastore.getActorContext(),
325                 ClusterUtils.getCleanShardName(TestModel.JUNK_PATH)));
326
327         // check follower has local shards
328         assertNotNull(findLocalShard(followerConfigDatastore.getActorContext(),
329                 ClusterUtils.getCleanShardName(TestModel.TEST_PATH)));
330
331         assertNotNull(findLocalShard(followerConfigDatastore.getActorContext(),
332                 ClusterUtils.getCleanShardName(TestModel.OUTER_CONTAINER_PATH)));
333
334         assertNotNull(findLocalShard(followerConfigDatastore.getActorContext(),
335                 ClusterUtils.getCleanShardName(TestModel.INNER_LIST_PATH)));
336
337         assertNotNull(findLocalShard(followerConfigDatastore.getActorContext(),
338                 ClusterUtils.getCleanShardName(TestModel.JUNK_PATH)));
339
340         LOG.debug("Closing registrations");
341
342         reg1.close().toCompletableFuture().get();
343         reg2.close().toCompletableFuture().get();
344         reg3.close().toCompletableFuture().get();
345         reg4.close().toCompletableFuture().get();
346
347         waitUntilShardIsDown(leaderConfigDatastore.getActorContext(),
348                 ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
349
350         waitUntilShardIsDown(leaderConfigDatastore.getActorContext(),
351                 ClusterUtils.getCleanShardName(TestModel.OUTER_CONTAINER_PATH));
352
353         waitUntilShardIsDown(leaderConfigDatastore.getActorContext(),
354                 ClusterUtils.getCleanShardName(TestModel.INNER_LIST_PATH));
355
356         waitUntilShardIsDown(leaderConfigDatastore.getActorContext(),
357                 ClusterUtils.getCleanShardName(TestModel.JUNK_PATH));
358
359         LOG.debug("All leader shards gone");
360
361         waitUntilShardIsDown(followerConfigDatastore.getActorContext(),
362                 ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
363
364         waitUntilShardIsDown(followerConfigDatastore.getActorContext(),
365                 ClusterUtils.getCleanShardName(TestModel.OUTER_CONTAINER_PATH));
366
367         waitUntilShardIsDown(followerConfigDatastore.getActorContext(),
368                 ClusterUtils.getCleanShardName(TestModel.INNER_LIST_PATH));
369
370         waitUntilShardIsDown(followerConfigDatastore.getActorContext(),
371                 ClusterUtils.getCleanShardName(TestModel.JUNK_PATH));
372
373         LOG.debug("All follower shards gone");
374     }
375
376     @Test
377     public void testMultipleRegistrationsAtOnePrefix() throws Exception {
378         initEmptyDatastores();
379
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);
385
386             leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(),
387                     ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
388
389             assertNotNull(findLocalShard(leaderConfigDatastore.getActorContext(),
390                     ClusterUtils.getCleanShardName(TestModel.TEST_PATH)));
391
392             assertNotNull(findLocalShard(followerConfigDatastore.getActorContext(),
393                     ClusterUtils.getCleanShardName(TestModel.TEST_PATH)));
394
395
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);
401
402             waitOnAsyncTask(reg1.close(), DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
403
404             waitUntilShardIsDown(leaderConfigDatastore.getActorContext(),
405                     ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
406
407             waitUntilShardIsDown(followerConfigDatastore.getActorContext(),
408                     ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
409         }
410
411     }
412 }