2 * Copyright (c) 2015 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 static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.fail;
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSystem;
14 import akka.actor.Address;
15 import akka.actor.AddressFromURIString;
16 import akka.cluster.Cluster;
17 import akka.cluster.ClusterEvent.CurrentClusterState;
18 import akka.cluster.Member;
19 import akka.cluster.MemberStatus;
20 import com.google.common.base.Optional;
21 import com.google.common.base.Preconditions;
22 import com.google.common.base.Stopwatch;
23 import com.google.common.collect.Sets;
24 import com.google.common.util.concurrent.Uninterruptibles;
25 import com.typesafe.config.ConfigFactory;
26 import java.util.List;
28 import java.util.concurrent.TimeUnit;
29 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
30 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
31 import org.opendaylight.controller.cluster.raft.client.messages.GetOnDemandRaftState;
32 import org.opendaylight.controller.cluster.raft.client.messages.OnDemandRaftState;
33 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import scala.concurrent.Await;
36 import scala.concurrent.Future;
37 import scala.concurrent.duration.Duration;
40 * Class that represents a cluster member node for unit tests. It encapsulates an actor system with
41 * config and (optional) operational data store instances. The Builder is used to specify the setup
42 * parameters and create the data store instances. The actor system is automatically joined to address
43 * 127.0.0.1:2558 so one member must specify an akka cluster configuration with that address.
45 * @author Thomas Pantelis
47 public class MemberNode {
48 static final Address MEMBER_1_ADDRESS = AddressFromURIString.parse("akka.tcp://cluster-test@127.0.0.1:2558");
50 private IntegrationTestKit kit;
51 private DistributedDataStore configDataStore;
52 private DistributedDataStore operDataStore;
53 private DatastoreContext.Builder datastoreContextBuilder;
54 private boolean cleanedUp;
57 * Constructs a Builder.
59 * @param members the list to which the resulting MemberNode will be added. This makes it easier for
60 * callers to cleanup instances on test completion.
61 * @return a Builder instance
63 public static Builder builder(List<MemberNode> members) {
64 return new Builder(members);
67 public IntegrationTestKit kit() {
72 public DistributedDataStore configDataStore() {
73 return configDataStore;
77 public DistributedDataStore operDataStore() {
81 public DatastoreContext.Builder datastoreContextBuilder() {
82 return datastoreContextBuilder;
85 public void waitForMembersUp(String... otherMembers) {
86 Set<String> otherMembersSet = Sets.newHashSet(otherMembers);
87 Stopwatch sw = Stopwatch.createStarted();
88 while(sw.elapsed(TimeUnit.SECONDS) <= 10) {
89 CurrentClusterState state = Cluster.get(kit.getSystem()).state();
90 for(Member m: state.getMembers()) {
91 if(m.status() == MemberStatus.up() && otherMembersSet.remove(m.getRoles().iterator().next()) &&
92 otherMembersSet.isEmpty()) {
97 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
100 fail("Member(s) " + otherMembersSet + " are not Up");
103 public void cleanup() {
106 kit.cleanup(configDataStore);
107 kit.cleanup(operDataStore);
108 kit.shutdownActorSystem(kit.getSystem(), Boolean.TRUE);
112 public static void verifyRaftState(DistributedDataStore datastore, String shardName, RaftStateVerifier verifier)
114 ActorContext actorContext = datastore.getActorContext();
116 Future<ActorRef> future = actorContext.findLocalShardAsync(shardName);
117 ActorRef shardActor = Await.result(future, Duration.create(10, TimeUnit.SECONDS));
119 AssertionError lastError = null;
120 Stopwatch sw = Stopwatch.createStarted();
121 while(sw.elapsed(TimeUnit.SECONDS) <= 5) {
122 OnDemandRaftState raftState = (OnDemandRaftState)actorContext.
123 executeOperation(shardActor, GetOnDemandRaftState.INSTANCE);
126 verifier.verify(raftState);
128 } catch (AssertionError e) {
130 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
137 public static void verifyRaftPeersPresent(DistributedDataStore datastore, final String shardName,
138 String... peerMemberNames) throws Exception {
139 final Set<String> peerIds = Sets.newHashSet();
140 for(String p: peerMemberNames) {
141 peerIds.add(ShardIdentifier.builder().memberName(p).shardName(shardName).
142 type(datastore.getActorContext().getDataStoreName()).build().toString());
145 verifyRaftState(datastore, shardName, new RaftStateVerifier() {
147 public void verify(OnDemandRaftState raftState) {
148 assertEquals("Peers for shard " + shardName, peerIds, raftState.getPeerAddresses().keySet());
153 public static void verifyNoShardPresent(DistributedDataStore datastore, String shardName) {
154 Stopwatch sw = Stopwatch.createStarted();
155 while(sw.elapsed(TimeUnit.SECONDS) <= 5) {
156 Optional<ActorRef> shardReply = datastore.getActorContext().findLocalShard(shardName);
157 if(!shardReply.isPresent()) {
161 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
164 fail("Shard " + shardName + " is present");
167 public static class Builder {
168 private final List<MemberNode> members;
169 private String moduleShardsConfig;
170 private String akkaConfig;
171 private String[] waitForshardLeader = new String[0];
172 private String testName;
173 private SchemaContext schemaContext;
174 private boolean createOperDatastore = true;
175 private DatastoreContext.Builder datastoreContextBuilder = DatastoreContext.newBuilder().
176 shardHeartbeatIntervalInMillis(300).shardElectionTimeoutFactor(30);
178 Builder(List<MemberNode> members) {
179 this.members = members;
183 * Specifies the name of the module shards config file. This is required.
185 * @return this Builder
187 public Builder moduleShardsConfig(String moduleShardsConfig) {
188 this.moduleShardsConfig = moduleShardsConfig;
193 * Specifies the name of the akka configuration. This is required.
195 * @return this Builder
197 public Builder akkaConfig(String akkaConfig) {
198 this.akkaConfig = akkaConfig;
203 * Specifies the name of the test that is appended to the data store names. This is required.
205 * @return this Builder
207 public Builder testName(String testName) {
208 this.testName = testName;
213 * Specifies the optional names of the shards to initially wait for a leader to be elected.
215 * @return this Builder
217 public Builder waitForShardLeader(String... shardNames) {
218 this.waitForshardLeader = shardNames;
223 * Specifies whether or not to create an operational data store. Defaults to true.
225 * @return this Builder
227 public Builder createOperDatastore(boolean value) {
228 this.createOperDatastore = value;
233 * Specifies the SchemaContext for the data stores. Defaults to SchemaContextHelper.full().
235 * @return this Builder
237 public Builder schemaContext(SchemaContext schemaContext) {
238 this.schemaContext = schemaContext;
243 * Specifies the DatastoreContext Builder. If not specified, a default instance is used.
245 * @return this Builder
247 public Builder datastoreContextBuilder(DatastoreContext.Builder builder) {
248 datastoreContextBuilder = builder;
252 public MemberNode build() {
253 Preconditions.checkNotNull(moduleShardsConfig, "moduleShardsConfig must be specified");
254 Preconditions.checkNotNull(akkaConfig, "akkaConfig must be specified");
255 Preconditions.checkNotNull(testName, "testName must be specified");
257 if(schemaContext == null) {
258 schemaContext = SchemaContextHelper.full();
261 MemberNode node = new MemberNode();
262 node.datastoreContextBuilder = datastoreContextBuilder;
264 ActorSystem system = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig(akkaConfig));
265 Cluster.get(system).join(MEMBER_1_ADDRESS);
267 node.kit = new IntegrationTestKit(system, datastoreContextBuilder);
269 String memberName = new ClusterWrapperImpl(system).getCurrentMemberName();
270 node.kit.getDatastoreContextBuilder().shardManagerPersistenceId("shard-manager-config-" + memberName);
271 node.configDataStore = node.kit.setupDistributedDataStore("config_" + testName, moduleShardsConfig,
272 true, schemaContext, waitForshardLeader);
274 if(createOperDatastore) {
275 node.kit.getDatastoreContextBuilder().shardManagerPersistenceId("shard-manager-oper-" + memberName);
276 node.operDataStore = node.kit.setupDistributedDataStore("oper_" + testName, moduleShardsConfig,
277 true, schemaContext, waitForshardLeader);
285 public static interface RaftStateVerifier {
286 void verify(OnDemandRaftState raftState);