2 * Copyright (c) 2014, 2015 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
9 package org.opendaylight.controller.cluster.example;
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSystem;
13 import com.google.common.base.Optional;
14 import com.google.common.collect.Lists;
15 import com.typesafe.config.ConfigFactory;
16 import java.io.BufferedReader;
17 import java.io.InputStreamReader;
18 import java.util.HashMap;
19 import java.util.List;
21 import java.util.concurrent.ConcurrentHashMap;
22 import org.opendaylight.controller.cluster.example.messages.PrintRole;
23 import org.opendaylight.controller.cluster.example.messages.PrintState;
24 import org.opendaylight.controller.cluster.raft.ConfigParams;
27 * This is a test driver for testing akka-raft implementation
28 * Its uses ExampleActors and threads to push content(key-vals) to these actors
29 * Each ExampleActor can have one or more ClientActors. Each ClientActor spawns
30 * a thread and starts push logs to the actor its assigned to.
32 public class TestDriver {
35 private static Map<String, String> allPeers = new HashMap<>();
36 private static Map<String, ActorRef> clientActorRefs = new HashMap<>();
37 private static Map<String, ActorRef> actorRefs = new HashMap<>();
38 private static LogGenerator logGenerator = new LogGenerator();
39 private int nameCounter = 0;
40 private static ConfigParams configParams = new ExampleConfigParamsImpl();
42 private static ActorSystem actorSystem;
43 private static ActorSystem listenerActorSystem;
46 * Create nodes, add clients and start logging.
51 * reinstateNode:{nodeName}
53 * addClientsToNode:{nodeName, num}
56 * startLoggingForClient:{nodeName}
57 * stopLoggingForClient:{nodeName}
61 * Note: when run on IDE and on debug log level, the debug logs in
62 * AbstractUptypedActor and AbstractUptypedPersistentActor would need to be commented out.
63 * Also RaftActor handleCommand(), debug log which prints for every command other than AE/AER
68 public static void main(String[] args) throws Exception {
70 actorSystem = ActorSystem.create("raft-test", ConfigFactory
71 .load().getConfig("raft-test"));
73 listenerActorSystem = ActorSystem.create("raft-test-listener", ConfigFactory
74 .load().getConfig("raft-test-listener"));
76 TestDriver td = new TestDriver();
78 System.out.println("Enter command (type bye to exit):");
81 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
83 String command = br.readLine();
84 if (command.startsWith("bye")) {
87 } else if (command.startsWith("createNodes")) {
88 String[] arr = command.split(":");
89 int n = Integer.parseInt(arr[1]);
92 } else if (command.startsWith("addClients")) {
93 String[] arr = command.split(":");
94 int n = Integer.parseInt(arr[1]);
97 } else if (command.startsWith("addClientsToNode")) {
98 String[] arr = command.split(":");
99 String nodeName = arr[1];
100 int n = Integer.parseInt(arr[1]);
101 td.addClientsToNode(nodeName, n);
103 } else if (command.startsWith("stopNode")) {
104 String[] arr = command.split(":");
107 } else if (command.startsWith("reinstateNode")) {
108 String[] arr = command.split(":");
109 td.reinstateNode(arr[1]);
111 } else if (command.startsWith("startLogging")) {
112 td.startAllLogging();
114 } else if (command.startsWith("startLoggingForClient")) {
115 String[] arr = command.split(":");
116 td.startLoggingForClient(clientActorRefs.get(arr[1]));
118 } else if (command.startsWith("stopLogging")) {
121 } else if (command.startsWith("stopLoggingForClient")) {
122 String[] arr = command.split(":");
123 td.stopLoggingForClient(clientActorRefs.get(arr[1]));
125 } else if (command.startsWith("printState")) {
127 } else if (command.startsWith("printNodes")) {
130 System.out.println("Invalid command:" + command);
136 // create the listener using a separate actor system for each example actor
137 private static void createClusterRoleChangeListener(List<String> memberIds) {
138 System.out.println("memberIds="+memberIds);
139 for (String memberId : memberIds) {
140 ActorRef listenerActor = listenerActorSystem.actorOf(
141 ExampleRoleChangeListener.getProps(memberId), memberId + "-role-change-listener");
142 System.out.println("Role Change Listener created:" + listenerActor.path().toString());
146 public static ActorRef createExampleActor(String name) {
147 return actorSystem.actorOf(ExampleActor.props(name, withoutPeer(name),
148 Optional.of(configParams)), name);
151 public void createNodes(int num) {
152 for (int i=0; i < num; i++) {
153 nameCounter = nameCounter + 1;
154 allPeers.put("example-"+nameCounter, "akka://raft-test/user/example-"+nameCounter);
157 for (String s : allPeers.keySet()) {
158 ActorRef exampleActor = createExampleActor(s);
159 actorRefs.put(s, exampleActor);
160 System.out.println("Created node:"+s);
164 createClusterRoleChangeListener(Lists.newArrayList(allPeers.keySet()));
167 // add num clients to all nodes in the system
168 public void addClients(int num) {
169 for(Map.Entry<String,ActorRef> actorRefEntry : actorRefs.entrySet()) {
170 for (int i=0; i < num; i++) {
171 String clientName = "client-" + i + "-" + actorRefEntry.getKey();
172 ActorRef clientActor = actorSystem.actorOf(
173 ClientActor.props(actorRefEntry.getValue()), clientName);
174 clientActorRefs.put(clientName, clientActor);
175 System.out.println("Created client-node:" + clientName);
180 // add num clients to a node
181 public void addClientsToNode(String actorName, int num) {
182 ActorRef actorRef = actorRefs.get(actorName);
183 for (int i=0; i < num; i++) {
184 String clientName = "client-" + i + "-" + actorName;
185 clientActorRefs.put(clientName,
186 actorSystem.actorOf(ClientActor.props(actorRef), clientName));
187 System.out.println("Added client-node:" + clientName);
191 public void stopNode(String actorName) {
192 ActorRef actorRef = actorRefs.get(actorName);
194 for (Map.Entry<String,ActorRef> entry : clientActorRefs.entrySet()) {
195 if (entry.getKey().endsWith(actorName)) {
196 actorSystem.stop(entry.getValue());
200 actorSystem.stop(actorRef);
201 actorRefs.remove(actorName);
202 allPeers.remove(actorName);
205 public void reinstateNode(String actorName) {
206 String address = "akka://default/user/"+actorName;
207 allPeers.put(actorName, address);
209 ActorRef exampleActor = createExampleActor(actorName);
210 actorRefs.put(actorName, exampleActor);
212 addClientsToNode(actorName, 1);
215 public void startAllLogging() {
216 if(!clientActorRefs.isEmpty()) {
217 for(Map.Entry<String,ActorRef> client : clientActorRefs.entrySet()) {
218 logGenerator.startLoggingForClient(client.getValue());
219 System.out.println("Started logging for client:"+client.getKey());
222 System.out.println("There are no clients for any nodes. First create clients using commands- addClients:<num> or addClientsToNode:<nodename>:<num>");
227 public void startLoggingForClient(ActorRef client) {
228 logGenerator.startLoggingForClient(client);
231 public void stopAllLogging() {
232 for(Map.Entry<String,ActorRef> client : clientActorRefs.entrySet()) {
233 logGenerator.stopLoggingForClient(client.getValue());
237 public void stopLoggingForClient(ActorRef client) {
238 logGenerator.stopLoggingForClient(client);
241 public void printState() {
242 for (ActorRef ref : actorRefs.values()) {
243 ref.tell(new PrintState(), null);
247 public void printNodes() {
248 for (ActorRef ref : actorRefs.values()) {
249 ref.tell(new PrintRole(), null);
253 public ActorRef getLeader() {
258 private static Map<String, String> withoutPeer(String peerId) {
259 Map<String, String> without = new ConcurrentHashMap<>(allPeers);
260 without.remove(peerId);