Bump odlparent/yangtools/mdsal
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DistributedDataStoreWithSegmentedJournalIntegrationTest.java
1 /*
2  * Copyright (c) 2014, 2017 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 package org.opendaylight.controller.cluster.datastore;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.opendaylight.controller.md.cluster.datastore.model.CarsModel.CAR_QNAME;
13
14 import akka.actor.ActorSystem;
15 import akka.actor.Address;
16 import akka.actor.AddressFromURIString;
17 import akka.cluster.Cluster;
18 import akka.testkit.javadsl.TestKit;
19 import com.google.common.base.Stopwatch;
20 import com.google.common.util.concurrent.Uninterruptibles;
21 import com.typesafe.config.ConfigFactory;
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.Optional;
27 import java.util.concurrent.TimeUnit;
28 import java.util.concurrent.atomic.AtomicBoolean;
29 import org.apache.commons.io.FileUtils;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.junit.runners.Parameterized;
35 import org.junit.runners.Parameterized.Parameters;
36 import org.opendaylight.controller.cluster.databroker.TestClientBackedDataStore;
37 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
38 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
39 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
40 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
41 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
42 import org.opendaylight.yangtools.yang.common.Uint64;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.builder.CollectionNodeBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
50
51 @RunWith(Parameterized.class)
52 public class DistributedDataStoreWithSegmentedJournalIntegrationTest
53         extends AbstractDistributedDataStoreIntegrationTest {
54
55     @Parameters(name = "{0}")
56     public static Collection<Object[]> data() {
57         return Arrays.asList(new Object[][] {
58                 { TestDistributedDataStore.class }, { TestClientBackedDataStore.class }
59         });
60     }
61
62     @Before
63     public void setUp() {
64         InMemorySnapshotStore.clear();
65         system = ActorSystem.create("cluster-test",
66                 ConfigFactory.load("segmented.conf").getConfig("Member1"));
67         cleanSnapshotDir(system);
68
69         Address member1Address = AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558");
70         Cluster.get(system).join(member1Address);
71     }
72
73     @After
74     public void tearDown() {
75         TestKit.shutdownActorSystem(system, true);
76         system = null;
77     }
78
79     private static void cleanSnapshotDir(final ActorSystem system) {
80         File journalDir = new File(system.settings().config()
81                 .getString("akka.persistence.journal.segmented-file.root-directory"));
82
83         if (!journalDir.exists()) {
84             return;
85         }
86
87         try {
88             FileUtils.cleanDirectory(journalDir);
89         } catch (IOException e) {
90             // Ignore
91         }
92     }
93
94     @Test
95     public void testManyWritesDeletes() throws Exception {
96         final IntegrationTestKit testKit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
97         CollectionNodeBuilder<MapEntryNode, SystemMapNode> carMapBuilder = ImmutableNodes.mapNodeBuilder(CAR_QNAME);
98
99         try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(
100                 testParameter, "testManyWritesDeletes", "module-shards-cars-member-1.conf", true, "cars")) {
101
102             DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
103
104             DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
105             writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
106             writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
107             testKit.doCommit(writeTx.ready());
108
109             int numCars = 20;
110             for (int i = 0; i < numCars; ++i) {
111                 DOMStoreReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
112
113                 YangInstanceIdentifier path = CarsModel.newCarPath("car" + i);
114                 MapEntryNode data = CarsModel.newCarEntry("car" + i, Uint64.valueOf(20000));
115
116                 rwTx.merge(path, data);
117                 carMapBuilder.withChild(data);
118
119                 testKit.doCommit(rwTx.ready());
120
121                 if (i % 5 == 0) {
122                     rwTx = txChain.newReadWriteTransaction();
123
124                     rwTx.delete(path);
125                     carMapBuilder.withoutChild(path.getLastPathArgument());
126                     testKit.doCommit(rwTx.ready());
127                 }
128             }
129
130             final Optional<NormalizedNode> optional = txChain.newReadOnlyTransaction()
131                     .read(CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
132             assertTrue("isPresent", optional.isPresent());
133
134             MapNode cars = carMapBuilder.build();
135
136             assertEquals("cars not matching result", cars, optional.get());
137
138             txChain.close();
139
140
141             // wait until the journal is actually persisted, killing the datastore early results in missing entries
142             Stopwatch sw = Stopwatch.createStarted();
143             AtomicBoolean done = new AtomicBoolean(false);
144             while (!done.get()) {
145                 MemberNode.verifyRaftState(dataStore, "cars", raftState -> {
146                     if (raftState.getLastApplied() == raftState.getLastLogIndex()) {
147                         done.set(true);
148                     }
149                 });
150
151                 assertTrue("Shard did not persist all journal entries in time.", sw.elapsed(TimeUnit.SECONDS) <= 5);
152
153                 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
154             }
155         }
156
157         // test restoration from journal and verify data matches
158         try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(
159                 testParameter, "testManyWritesDeletes", "module-shards-cars-member-1.conf", true, "cars")) {
160
161             DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
162             MapNode cars = carMapBuilder.build();
163
164             final Optional<NormalizedNode> optional = txChain.newReadOnlyTransaction()
165                     .read(CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
166             assertTrue("isPresent", optional.isPresent());
167             assertEquals("restored cars do not match snapshot", cars, optional.get());
168
169             txChain.close();
170         }
171     }
172 }