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