2 * Copyright (c) 2014, 2017 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
8 package org.opendaylight.controller.cluster.datastore;
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;
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;
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;
50 @RunWith(Parameterized.class)
51 public class DistributedDataStoreWithSegmentedJournalIntegrationTest
52 extends AbstractDistributedDataStoreIntegrationTest {
54 @Parameters(name = "{0}")
55 public static Collection<Object[]> data() {
56 return Arrays.asList(new Object[][] {
57 { TestDistributedDataStore.class }, { TestClientBackedDataStore.class }
63 InMemorySnapshotStore.clear();
64 system = ActorSystem.create("cluster-test",
65 ConfigFactory.load("segmented.conf").getConfig("Member1"));
66 cleanSnapshotDir(system);
68 Address member1Address = AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558");
69 Cluster.get(system).join(member1Address);
73 public void tearDown() {
74 TestKit.shutdownActorSystem(system, true);
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"));
82 if (!journalDir.exists()) {
87 FileUtils.cleanDirectory(journalDir);
88 } catch (IOException e) {
94 public void testManyWritesDeletes() throws Exception {
95 final IntegrationTestKit testKit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
96 CollectionNodeBuilder<MapEntryNode, MapNode> carMapBuilder = ImmutableNodes.mapNodeBuilder(CAR_QNAME);
98 try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(
99 testParameter, "testManyWritesDeletes", "module-shards-cars-member-1.conf", true, "cars")) {
101 DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
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());
109 for (int i = 0; i < numCars; ++i) {
110 DOMStoreReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
112 YangInstanceIdentifier path = CarsModel.newCarPath("car" + i);
113 MapEntryNode data = CarsModel.newCarEntry("car" + i, Uint64.valueOf(20000));
115 rwTx.merge(path, data);
116 carMapBuilder.withChild(data);
118 testKit.doCommit(rwTx.ready());
121 rwTx = txChain.newReadWriteTransaction();
124 carMapBuilder.withoutChild(path.getLastPathArgument());
125 testKit.doCommit(rwTx.ready());
129 final Optional<NormalizedNode<?, ?>> optional = txChain.newReadOnlyTransaction()
130 .read(CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
131 assertTrue("isPresent", optional.isPresent());
133 MapNode cars = carMapBuilder.build();
135 assertEquals("cars not matching result", cars, optional.get());
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()) {
150 assertTrue("Shard did not persist all journal entries in time.", sw.elapsed(TimeUnit.SECONDS) <= 5);
152 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
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")) {
160 DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
161 MapNode cars = carMapBuilder.build();
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());