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