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.access.concepts.MemberName;
30 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
31 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
32 import org.opendaylight.controller.cluster.raft.client.messages.GetOnDemandRaftState;
33 import org.opendaylight.controller.cluster.raft.client.messages.OnDemandRaftState;
34 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import scala.concurrent.Await;
37 import scala.concurrent.Future;
38 import scala.concurrent.duration.Duration;
41 * Class that represents a cluster member node for unit tests. It encapsulates an actor system with
42 * config and (optional) operational data store instances. The Builder is used to specify the setup
43 * parameters and create the data store instances. The actor system is automatically joined to address
44 * 127.0.0.1:2558 so one member must specify an akka cluster configuration with that address.
46 * @author Thomas Pantelis
48 public class MemberNode {
49 static final Address MEMBER_1_ADDRESS = AddressFromURIString.parse("akka.tcp://cluster-test@127.0.0.1:2558");
51 private IntegrationTestKit kit;
52 private DistributedDataStore configDataStore;
53 private DistributedDataStore operDataStore;
54 private DatastoreContext.Builder datastoreContextBuilder;
55 private boolean cleanedUp;
58 * Constructs a Builder.
60 * @param members the list to which the resulting MemberNode will be added. This makes it easier for
61 * callers to cleanup instances on test completion.
62 * @return a Builder instance
64 public static Builder builder(List<MemberNode> members) {
65 return new Builder(members);
68 public IntegrationTestKit kit() {
73 public DistributedDataStore configDataStore() {
74 return configDataStore;
78 public DistributedDataStore operDataStore() {
82 public DatastoreContext.Builder datastoreContextBuilder() {
83 return datastoreContextBuilder;
86 public void waitForMembersUp(String... otherMembers) {
87 kit.waitForMembersUp(otherMembers);
90 public void waitForMemberDown(String member) {
91 Stopwatch sw = Stopwatch.createStarted();
92 while(sw.elapsed(TimeUnit.SECONDS) <= 10) {
93 CurrentClusterState state = Cluster.get(kit.getSystem()).state();
94 for(Member m: state.getUnreachable()) {
95 if(member.equals(m.getRoles().iterator().next())) {
100 for(Member m: state.getMembers()) {
101 if(m.status() != MemberStatus.up() && member.equals(m.getRoles().iterator().next())) {
106 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
109 fail("Member " + member + " is now down");
112 public void cleanup() {
115 if (configDataStore != null) {
116 configDataStore.close();
118 if (operDataStore != null) {
119 operDataStore.close();
122 IntegrationTestKit.shutdownActorSystem(kit.getSystem(), Boolean.TRUE);
126 public static void verifyRaftState(DistributedDataStore datastore, String shardName, RaftStateVerifier verifier)
128 ActorContext actorContext = datastore.getActorContext();
130 Future<ActorRef> future = actorContext.findLocalShardAsync(shardName);
131 ActorRef shardActor = Await.result(future, Duration.create(10, TimeUnit.SECONDS));
133 AssertionError lastError = null;
134 Stopwatch sw = Stopwatch.createStarted();
135 while(sw.elapsed(TimeUnit.SECONDS) <= 5) {
136 OnDemandRaftState raftState = (OnDemandRaftState)actorContext.
137 executeOperation(shardActor, GetOnDemandRaftState.INSTANCE);
140 verifier.verify(raftState);
142 } catch (AssertionError e) {
144 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
151 public static void verifyRaftPeersPresent(DistributedDataStore datastore, final String shardName,
152 String... peerMemberNames) throws Exception {
153 final Set<String> peerIds = Sets.newHashSet();
154 for(String p: peerMemberNames) {
155 peerIds.add(ShardIdentifier.create(shardName, MemberName.forName(p),
156 datastore.getActorContext().getDataStoreName()).toString());
159 verifyRaftState(datastore, shardName, raftState -> assertEquals("Peers for shard " + shardName, peerIds,
160 raftState.getPeerAddresses().keySet()));
163 public static void verifyNoShardPresent(DistributedDataStore datastore, String shardName) {
164 Stopwatch sw = Stopwatch.createStarted();
165 while(sw.elapsed(TimeUnit.SECONDS) <= 5) {
166 Optional<ActorRef> shardReply = datastore.getActorContext().findLocalShard(shardName);
167 if(!shardReply.isPresent()) {
171 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
174 fail("Shard " + shardName + " is present");
177 public static class Builder {
178 private final List<MemberNode> members;
179 private String moduleShardsConfig;
180 private String akkaConfig;
181 private String[] waitForshardLeader = new String[0];
182 private String testName;
183 private SchemaContext schemaContext;
184 private boolean createOperDatastore = true;
185 private DatastoreContext.Builder datastoreContextBuilder = DatastoreContext.newBuilder().
186 shardHeartbeatIntervalInMillis(300).shardElectionTimeoutFactor(30);
188 Builder(List<MemberNode> members) {
189 this.members = members;
193 * Specifies the name of the module shards config file. This is required.
195 * @return this Builder
197 public Builder moduleShardsConfig(String moduleShardsConfig) {
198 this.moduleShardsConfig = moduleShardsConfig;
203 * Specifies the name of the akka configuration. This is required.
205 * @return this Builder
207 public Builder akkaConfig(String akkaConfig) {
208 this.akkaConfig = akkaConfig;
213 * Specifies the name of the test that is appended to the data store names. This is required.
215 * @return this Builder
217 public Builder testName(String testName) {
218 this.testName = testName;
223 * Specifies the optional names of the shards to initially wait for a leader to be elected.
225 * @return this Builder
227 public Builder waitForShardLeader(String... shardNames) {
228 this.waitForshardLeader = shardNames;
233 * Specifies whether or not to create an operational data store. Defaults to true.
235 * @return this Builder
237 public Builder createOperDatastore(boolean value) {
238 this.createOperDatastore = value;
243 * Specifies the SchemaContext for the data stores. Defaults to SchemaContextHelper.full().
245 * @return this Builder
247 public Builder schemaContext(SchemaContext schemaContext) {
248 this.schemaContext = schemaContext;
253 * Specifies the DatastoreContext Builder. If not specified, a default instance is used.
255 * @return this Builder
257 public Builder datastoreContextBuilder(DatastoreContext.Builder builder) {
258 datastoreContextBuilder = builder;
262 public MemberNode build() {
263 Preconditions.checkNotNull(moduleShardsConfig, "moduleShardsConfig must be specified");
264 Preconditions.checkNotNull(akkaConfig, "akkaConfig must be specified");
265 Preconditions.checkNotNull(testName, "testName must be specified");
267 if(schemaContext == null) {
268 schemaContext = SchemaContextHelper.full();
271 MemberNode node = new MemberNode();
272 node.datastoreContextBuilder = datastoreContextBuilder;
274 ActorSystem system = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig(akkaConfig));
275 Cluster.get(system).join(MEMBER_1_ADDRESS);
277 node.kit = new IntegrationTestKit(system, datastoreContextBuilder);
279 String memberName = new ClusterWrapperImpl(system).getCurrentMemberName().getName();
280 node.kit.getDatastoreContextBuilder().shardManagerPersistenceId("shard-manager-config-" + memberName);
281 node.configDataStore = node.kit.setupDistributedDataStore("config_" + testName, moduleShardsConfig,
282 true, schemaContext, waitForshardLeader);
284 if(createOperDatastore) {
285 node.kit.getDatastoreContextBuilder().shardManagerPersistenceId("shard-manager-oper-" + memberName);
286 node.operDataStore = node.kit.setupDistributedDataStore("oper_" + testName, moduleShardsConfig,
287 true, schemaContext, waitForshardLeader);
295 public static interface RaftStateVerifier {
296 void verify(OnDemandRaftState raftState);