2 * Copyright (c) 2014 Brocade Communications 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 java.util.Collections;
11 import org.junit.Assert;
12 import org.junit.Test;
13 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
14 import org.opendaylight.controller.cluster.datastore.messages.AbortTransactionReply;
15 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply;
16 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
17 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransactionReply;
18 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
19 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
20 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
21 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
22 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
23 import org.opendaylight.controller.protobuff.messages.cohort3pc.ThreePhaseCommitCohortMessages;
24 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
25 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import scala.concurrent.duration.FiniteDuration;
30 import akka.actor.ActorRef;
31 import akka.actor.ActorSelection;
32 import akka.actor.PoisonPill;
33 import akka.actor.Props;
34 import akka.dispatch.Dispatchers;
35 import akka.testkit.TestActorRef;
38 * Tests backwards compatibility support from Helium-1 to Helium.
40 * In Helium-1, the 3-phase commit support was moved from the ThreePhaseCommitCohort actor to the
41 * Shard. As a consequence, a new transactionId field was added to the CanCommitTransaction,
42 * CommitTransaction and AbortTransaction messages. With a base Helium version node, these messages
43 * would be sans transactionId so this test verifies the Shard handles that properly.
45 * @author Thomas Pantelis
47 public class ShardTransactionHeliumBackwardsCompatibilityTest extends AbstractActorTest {
50 public void testTransactionCommit() throws Exception {
51 new ShardTestKit(getSystem()) {{
52 SchemaContext schemaContext = TestModel.createTestContext();
53 Props shardProps = Shard.props(ShardIdentifier.builder().memberName("member-1").
54 shardName("inventory").type("config").build(),
55 Collections.<ShardIdentifier,String>emptyMap(),
56 DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).build(),
57 schemaContext).withDispatcher(Dispatchers.DefaultDispatcherId());
59 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(), shardProps,
60 "testTransactionCommit");
62 waitUntilLeader(shard);
64 // Send CreateTransaction message with no messages version
66 String transactionID = "txn-1";
67 shard.tell(ShardTransactionMessages.CreateTransaction.newBuilder()
68 .setTransactionId(transactionID)
69 .setTransactionType(TransactionProxy.TransactionType.WRITE_ONLY.ordinal())
70 .setTransactionChainId("").build(), getRef());
72 final FiniteDuration duration = duration("5 seconds");
74 CreateTransactionReply reply = expectMsgClass(duration, CreateTransactionReply.class);
76 ActorSelection txActor = getSystem().actorSelection(reply.getTransactionActorPath());
78 // Write data to the Tx
80 txActor.tell(new WriteData(TestModel.TEST_PATH,
81 ImmutableNodes.containerNode(TestModel.TEST_QNAME), schemaContext), getRef());
83 expectMsgClass(duration, WriteDataReply.class);
87 txActor.tell(new ReadyTransaction().toSerializable(), getRef());
89 ReadyTransactionReply readyReply = ReadyTransactionReply.fromSerializable(expectMsgClass(
90 duration, ReadyTransactionReply.SERIALIZABLE_CLASS));
92 ActorSelection cohortActor = getSystem().actorSelection(readyReply.getCohortPath());
94 // Send the CanCommitTransaction message with no transactionId.
96 cohortActor.tell(ThreePhaseCommitCohortMessages.CanCommitTransaction.newBuilder().build(),
99 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS);
101 // Send the PreCommitTransaction message with no transactionId.
103 cohortActor.tell(ThreePhaseCommitCohortMessages.PreCommitTransaction.newBuilder().build(),
106 expectMsgClass(duration, PreCommitTransactionReply.SERIALIZABLE_CLASS);
108 // Send the CommitTransaction message with no transactionId.
110 cohortActor.tell(ThreePhaseCommitCohortMessages.CommitTransaction.newBuilder().build(),
113 expectMsgClass(duration, CommitTransactionReply.SERIALIZABLE_CLASS);
115 NormalizedNode<?, ?> node = ShardTest.readStore(shard, TestModel.TEST_PATH);
116 Assert.assertNotNull("Data not found in store", node);
118 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
123 public void testTransactionAbort() throws Exception {
124 new ShardTestKit(getSystem()) {{
125 SchemaContext schemaContext = TestModel.createTestContext();
126 Props shardProps = Shard.props(ShardIdentifier.builder().memberName("member-1").
127 shardName("inventory").type("config").build(),
128 Collections.<ShardIdentifier,String>emptyMap(),
129 DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).build(),
130 schemaContext).withDispatcher(Dispatchers.DefaultDispatcherId());
132 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(), shardProps,
133 "testTransactionAbort");
135 waitUntilLeader(shard);
137 // Send CreateTransaction message with no messages version
139 String transactionID = "txn-1";
140 shard.tell(ShardTransactionMessages.CreateTransaction.newBuilder()
141 .setTransactionId(transactionID)
142 .setTransactionType(TransactionProxy.TransactionType.WRITE_ONLY.ordinal())
143 .setTransactionChainId("").build(), getRef());
145 final FiniteDuration duration = duration("5 seconds");
147 CreateTransactionReply reply = expectMsgClass(duration, CreateTransactionReply.class);
149 ActorSelection txActor = getSystem().actorSelection(reply.getTransactionActorPath());
151 // Write data to the Tx
153 txActor.tell(new WriteData(TestModel.TEST_PATH,
154 ImmutableNodes.containerNode(TestModel.TEST_QNAME), schemaContext), getRef());
156 expectMsgClass(duration, WriteDataReply.class);
160 txActor.tell(new ReadyTransaction().toSerializable(), getRef());
162 ReadyTransactionReply readyReply = ReadyTransactionReply.fromSerializable(expectMsgClass(
163 duration, ReadyTransactionReply.SERIALIZABLE_CLASS));
165 ActorSelection cohortActor = getSystem().actorSelection(readyReply.getCohortPath());
167 // Send the CanCommitTransaction message with no transactionId.
169 cohortActor.tell(ThreePhaseCommitCohortMessages.CanCommitTransaction.newBuilder().build(),
172 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS);
174 // Send the AbortTransaction message with no transactionId.
176 cohortActor.tell(ThreePhaseCommitCohortMessages.AbortTransaction.newBuilder().build(),
179 expectMsgClass(duration, AbortTransactionReply.SERIALIZABLE_CLASS);
181 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());