2 * Copyright (c) 2016 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.assertNotNull;
12 import static org.junit.Assert.assertSame;
13 import static org.junit.Assert.fail;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.ArgumentMatchers.anyCollection;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.reset;
19 import static org.mockito.Mockito.verify;
20 import static org.mockito.Mockito.verifyNoMoreInteractions;
22 import akka.actor.ActorSystem;
23 import akka.actor.Address;
24 import akka.actor.AddressFromURIString;
25 import akka.cluster.Cluster;
26 import akka.testkit.javadsl.TestKit;
27 import com.google.common.base.Throwables;
28 import com.google.common.util.concurrent.FluentFuture;
29 import com.typesafe.config.ConfigFactory;
30 import java.util.Collection;
31 import java.util.concurrent.ExecutionException;
32 import java.util.concurrent.TimeUnit;
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Ignore;
36 import org.junit.Test;
37 import org.mockito.ArgumentCaptor;
38 import org.opendaylight.controller.cluster.databroker.ClientBackedDataStore;
39 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
40 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
41 import org.opendaylight.mdsal.common.api.DataValidationFailedException;
42 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
43 import org.opendaylight.mdsal.common.api.PostCanCommitStep;
44 import org.opendaylight.mdsal.common.api.PostPreCommitStep;
45 import org.opendaylight.mdsal.common.api.ThreePhaseCommitStep;
46 import org.opendaylight.mdsal.dom.api.DOMDataTreeCandidate;
47 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort;
48 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
49 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
50 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
51 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
52 import org.opendaylight.yangtools.yang.common.Uint64;
53 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
54 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
56 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
57 import org.opendaylight.yangtools.yang.data.tree.api.ModificationType;
58 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
60 public class DataTreeCohortIntegrationTest {
62 private static final DataValidationFailedException FAILED_CAN_COMMIT =
63 new DataValidationFailedException(YangInstanceIdentifier.class, TestModel.TEST_PATH, "Test failure.");
64 private static final FluentFuture<PostCanCommitStep> FAILED_CAN_COMMIT_FUTURE =
65 FluentFutures.immediateFailedFluentFuture(FAILED_CAN_COMMIT);
67 private static final DOMDataTreeIdentifier TEST_ID =
68 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH);
70 private static ActorSystem system;
72 private final DatastoreContext.Builder datastoreContextBuilder =
73 DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100);
76 public static void setUpClass() {
77 system = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
78 final Address member1Address = AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558");
79 Cluster.get(system).join(member1Address);
83 public static void tearDownClass() {
84 TestKit.shutdownActorSystem(system);
88 protected ActorSystem getSystem() {
92 @SuppressWarnings({ "unchecked", "rawtypes" })
94 public void testSuccessfulCanCommitWithNoopPostStep() throws Exception {
95 final var cohort = mock(DOMDataTreeCommitCohort.class);
96 doReturn(PostCanCommitStep.NOOP_SUCCESSFUL_FUTURE).when(cohort).canCommit(any(Object.class),
97 any(EffectiveModelContext.class), anyCollection());
98 ArgumentCaptor<Collection> candidateCapt = ArgumentCaptor.forClass(Collection.class);
99 IntegrationTestKit kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
101 try (var dataStore = kit.setupAbstractDataStore(ClientBackedDataStore.class,
102 "testSuccessfulCanCommitWithNoopPostStep", "test-1")) {
103 final var cohortReg = dataStore.registerCommitCohort(TEST_ID, cohort);
104 assertNotNull(cohortReg);
106 IntegrationTestKit.verifyShardState(dataStore, "test-1",
107 state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
109 final var node = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
110 kit.testWriteTransaction(dataStore, TestModel.TEST_PATH, node);
111 verify(cohort).canCommit(any(Object.class), any(EffectiveModelContext.class), candidateCapt.capture());
112 assertDataTreeCandidate((DOMDataTreeCandidate) candidateCapt.getValue().iterator().next(), TEST_ID,
113 ModificationType.WRITE, node, null);
116 doReturn(PostCanCommitStep.NOOP_SUCCESSFUL_FUTURE).when(cohort).canCommit(any(Object.class),
117 any(EffectiveModelContext.class), anyCollection());
119 kit.testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
120 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
121 .withChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 42))
123 verify(cohort).canCommit(any(Object.class), any(EffectiveModelContext.class), anyCollection());
127 IntegrationTestKit.verifyShardState(dataStore, "test-1",
128 state -> assertEquals("Cohort registrations", 0, state.getCommitCohortActors().size()));
130 kit.testWriteTransaction(dataStore, TestModel.TEST_PATH, node);
131 verifyNoMoreInteractions(cohort);
136 public void testFailedCanCommit() throws Exception {
137 final var failedCohort = mock(DOMDataTreeCommitCohort.class);
139 doReturn(FAILED_CAN_COMMIT_FUTURE).when(failedCohort).canCommit(any(Object.class),
140 any(EffectiveModelContext.class), anyCollection());
142 final var kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
143 try (var dataStore = kit.setupAbstractDataStore(ClientBackedDataStore.class, "testFailedCanCommit", "test-1")) {
144 dataStore.registerCommitCohort(TEST_ID, failedCohort);
146 IntegrationTestKit.verifyShardState(dataStore, "test-1",
147 state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
149 DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
150 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
151 DOMStoreThreePhaseCommitCohort dsCohort = writeTx.ready();
153 dsCohort.canCommit().get(5, TimeUnit.SECONDS);
154 fail("Exception should be raised.");
155 } catch (ExecutionException e) {
156 assertSame(FAILED_CAN_COMMIT, Throwables.getRootCause(e));
161 @SuppressWarnings({ "unchecked", "rawtypes" })
163 public void testCanCommitWithListEntries() throws Exception {
164 final var cohort = mock(DOMDataTreeCommitCohort.class);
165 doReturn(PostCanCommitStep.NOOP_SUCCESSFUL_FUTURE).when(cohort).canCommit(any(Object.class),
166 any(EffectiveModelContext.class), anyCollection());
167 final var kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
169 try (var dataStore = kit.setupAbstractDataStore(ClientBackedDataStore.class,
170 "testCanCommitWithMultipleListEntries", "cars-1")) {
171 final var cohortReg = dataStore.registerCommitCohort(
172 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH
173 .node(CarsModel.CAR_QNAME)), cohort);
174 assertNotNull(cohortReg);
176 IntegrationTestKit.verifyShardState(dataStore, "cars-1",
177 state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
179 // First write an empty base container and verify the cohort isn't invoked.
181 DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
182 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
183 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
184 kit.doCommit(writeTx.ready());
185 verifyNoMoreInteractions(cohort);
187 // Write a single car entry and verify the cohort is invoked.
189 writeTx = dataStore.newWriteOnlyTransaction();
190 final YangInstanceIdentifier optimaPath = CarsModel.newCarPath("optima");
191 final MapEntryNode optimaNode = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
192 writeTx.write(optimaPath, optimaNode);
193 kit.doCommit(writeTx.ready());
195 ArgumentCaptor<Collection> candidateCapture = ArgumentCaptor.forClass(Collection.class);
196 verify(cohort).canCommit(any(Object.class), any(EffectiveModelContext.class), candidateCapture.capture());
197 assertDataTreeCandidate((DOMDataTreeCandidate) candidateCapture.getValue().iterator().next(),
198 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, optimaPath), ModificationType.WRITE,
201 // Write replace the cars container with 2 new car entries. The cohort should get invoked with 3
202 // DOMDataTreeCandidates: once for each of the 2 new car entries (WRITE mod) and once for the deleted prior
203 // car entry (DELETE mod).
206 doReturn(PostCanCommitStep.NOOP_SUCCESSFUL_FUTURE).when(cohort).canCommit(any(Object.class),
207 any(EffectiveModelContext.class), anyCollection());
209 writeTx = dataStore.newWriteOnlyTransaction();
210 final YangInstanceIdentifier sportagePath = CarsModel.newCarPath("sportage");
211 final MapEntryNode sportageNode = CarsModel.newCarEntry("sportage", Uint64.valueOf(20000));
212 final YangInstanceIdentifier soulPath = CarsModel.newCarPath("soul");
213 final MapEntryNode soulNode = CarsModel.newCarEntry("soul", Uint64.valueOf(20000));
214 writeTx.write(CarsModel.BASE_PATH, CarsModel.newCarsNode(CarsModel.newCarsMapNode(sportageNode,soulNode)));
215 kit.doCommit(writeTx.ready());
217 candidateCapture = ArgumentCaptor.forClass(Collection.class);
218 verify(cohort).canCommit(any(Object.class), any(EffectiveModelContext.class), candidateCapture.capture());
220 assertDataTreeCandidate(findCandidate(candidateCapture, sportagePath), new DOMDataTreeIdentifier(
221 LogicalDatastoreType.CONFIGURATION, sportagePath), ModificationType.WRITE,
224 assertDataTreeCandidate(findCandidate(candidateCapture, soulPath), new DOMDataTreeIdentifier(
225 LogicalDatastoreType.CONFIGURATION, soulPath), ModificationType.WRITE,
228 assertDataTreeCandidate(findCandidate(candidateCapture, optimaPath), new DOMDataTreeIdentifier(
229 LogicalDatastoreType.CONFIGURATION, optimaPath), ModificationType.DELETE,
232 // Delete the cars container - cohort should be invoked for the 2 deleted car entries.
235 doReturn(PostCanCommitStep.NOOP_SUCCESSFUL_FUTURE).when(cohort).canCommit(any(Object.class),
236 any(EffectiveModelContext.class), anyCollection());
238 writeTx = dataStore.newWriteOnlyTransaction();
239 writeTx.delete(CarsModel.BASE_PATH);
240 kit.doCommit(writeTx.ready());
242 candidateCapture = ArgumentCaptor.forClass(Collection.class);
243 verify(cohort).canCommit(any(Object.class), any(EffectiveModelContext.class), candidateCapture.capture());
245 assertDataTreeCandidate(findCandidate(candidateCapture, sportagePath), new DOMDataTreeIdentifier(
246 LogicalDatastoreType.CONFIGURATION, sportagePath), ModificationType.DELETE,
249 assertDataTreeCandidate(findCandidate(candidateCapture, soulPath), new DOMDataTreeIdentifier(
250 LogicalDatastoreType.CONFIGURATION, soulPath), ModificationType.DELETE,
256 @SuppressWarnings("rawtypes")
257 private static DOMDataTreeCandidate findCandidate(final ArgumentCaptor<Collection> candidateCapture,
258 final YangInstanceIdentifier rootPath) {
259 for (Object obj: candidateCapture.getValue()) {
260 DOMDataTreeCandidate candidate = (DOMDataTreeCandidate)obj;
261 if (rootPath.equals(candidate.getRootPath().getRootIdentifier())) {
270 * FIXME: Since we invoke DOMDataTreeCommitCohort#canCommit on preCommit (as that's when we generate a
271 * DataTreeCandidate) and since currently preCommit is a noop in the Shard backend (it is combined with commit),
272 * we can't actually test abort after canCommit.
276 public void testAbortAfterCanCommit() throws Exception {
277 final var cohortToAbort = mock(DOMDataTreeCommitCohort.class);
278 final var stepToAbort = mock(PostCanCommitStep.class);
279 doReturn(ThreePhaseCommitStep.NOOP_ABORT_FUTURE).when(stepToAbort).abort();
280 doReturn(PostPreCommitStep.NOOP_FUTURE).when(stepToAbort).preCommit();
281 doReturn(FluentFutures.immediateFluentFuture(stepToAbort)).when(cohortToAbort).canCommit(any(Object.class),
282 any(EffectiveModelContext.class), anyCollection());
284 var kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
285 try (var dataStore = kit.setupAbstractDataStore(ClientBackedDataStore.class, "testAbortAfterCanCommit",
286 "test-1", "cars-1")) {
287 dataStore.registerCommitCohort(TEST_ID, cohortToAbort);
289 IntegrationTestKit.verifyShardState(dataStore, "test-1",
290 state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
292 var writeTx = dataStore.newWriteOnlyTransaction();
293 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
294 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
295 var dsCohort = writeTx.ready();
297 dsCohort.canCommit().get(5, TimeUnit.SECONDS);
298 dsCohort.preCommit().get(5, TimeUnit.SECONDS);
299 dsCohort.abort().get(5, TimeUnit.SECONDS);
300 verify(stepToAbort).abort();
304 private static void assertDataTreeCandidate(final DOMDataTreeCandidate candidate,
305 final DOMDataTreeIdentifier expTreeId, final ModificationType expType,
306 final NormalizedNode expDataAfter, final NormalizedNode expDataBefore) {
307 assertNotNull("Expected candidate for path " + expTreeId.getRootIdentifier(), candidate);
308 assertEquals("rootPath", expTreeId, candidate.getRootPath());
309 assertEquals("modificationType", expType, candidate.getRootNode().modificationType());
310 assertEquals("dataAfter", expDataAfter, candidate.getRootNode().dataAfter());
311 assertEquals("dataBefore", expDataBefore, candidate.getRootNode().dataBefore());