1 package org.opendaylight.controller.cluster.example;
3 import akka.actor.ActorRef;
4 import akka.actor.ActorSystem;
5 import com.google.common.base.Optional;
6 import org.opendaylight.controller.cluster.example.messages.PrintRole;
7 import org.opendaylight.controller.cluster.example.messages.PrintState;
8 import org.opendaylight.controller.cluster.raft.ConfigParams;
9 import org.opendaylight.controller.cluster.raft.client.messages.AddRaftPeer;
10 import org.opendaylight.controller.cluster.raft.client.messages.RemoveRaftPeer;
12 import java.io.BufferedReader;
13 import java.io.InputStreamReader;
14 import java.util.HashMap;
16 import java.util.concurrent.ConcurrentHashMap;
19 * This is a test driver for testing akka-raft implementation
20 * Its uses ExampleActors and threads to push content(key-vals) to these actors
21 * Each ExampleActor can have one or more ClientActors. Each ClientActor spawns
22 * a thread and starts push logs to the actor its assigned to.
24 public class TestDriver {
26 private static final ActorSystem actorSystem = ActorSystem.create();
27 private static Map<String, String> allPeers = new HashMap<>();
28 private static Map<String, ActorRef> clientActorRefs = new HashMap<String, ActorRef>();
29 private static Map<String, ActorRef> actorRefs = new HashMap<String, ActorRef>();
30 private static LogGenerator logGenerator = new LogGenerator();
31 private int nameCounter = 0;
32 private static ConfigParams configParams = new ExampleConfigParamsImpl();
35 * Create nodes, add clients and start logging.
41 * reinstateNode:{nodeName}
43 * addClientsToNode:{nodeName, num}
46 * startLoggingForClient:{nodeName}
47 * stopLoggingForClient:{nodeName}
53 public static void main(String[] args) throws Exception {
54 TestDriver td = new TestDriver();
56 System.out.println("Enter command (type bye to exit):");
59 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
61 String command = br.readLine();
62 if (command.startsWith("bye")) {
65 } else if (command.startsWith("createNodes")) {
66 String[] arr = command.split(":");
67 int n = Integer.parseInt(arr[1]);
70 } else if (command.startsWith("addNodes")) {
71 String[] arr = command.split(":");
72 int n = Integer.parseInt(arr[1]);
75 } else if (command.startsWith("addClients")) {
76 String[] arr = command.split(":");
77 int n = Integer.parseInt(arr[1]);
80 } else if (command.startsWith("addClientsToNode")) {
81 String[] arr = command.split(":");
82 String nodeName = arr[1];
83 int n = Integer.parseInt(arr[1]);
84 td.addClientsToNode(nodeName, n);
86 } else if (command.startsWith("stopNode")) {
87 String[] arr = command.split(":");
90 } else if (command.startsWith("reinstateNode")) {
91 String[] arr = command.split(":");
92 td.reinstateNode(arr[1]);
94 } else if (command.startsWith("startLogging")) {
97 } else if (command.startsWith("startLoggingForClient")) {
98 String[] arr = command.split(":");
99 td.startLoggingForClient(clientActorRefs.get(arr[1]));
101 } else if (command.startsWith("stopLogging")) {
104 } else if (command.startsWith("stopLoggingForClient")) {
105 String[] arr = command.split(":");
106 td.stopLoggingForClient(clientActorRefs.get(arr[1]));
108 } else if (command.startsWith("printState")) {
110 } else if (command.startsWith("printNodes")) {
117 public static ActorRef createExampleActor(String name) {
118 return actorSystem.actorOf(ExampleActor.props(name, withoutPeer(name),
119 Optional.of(configParams)), name);
122 public void createNodes(int num) {
123 for (int i=0; i < num; i++) {
124 nameCounter = nameCounter + 1;
125 allPeers.put("example-"+nameCounter, "akka://default/user/example-"+nameCounter);
128 for (String s : allPeers.keySet()) {
129 ActorRef exampleActor = createExampleActor(s);
130 actorRefs.put(s, exampleActor);
131 System.out.println("Created node:"+s);
136 // add new nodes , pass in the count
137 public void addNodes(int num) {
138 Map<String, String> newPeers = new HashMap<>();
139 for (int i=0; i < num; i++) {
140 nameCounter = nameCounter + 1;
141 newPeers.put("example-"+nameCounter, "akka://default/user/example-"+nameCounter);
142 allPeers.put("example-"+nameCounter, "akka://default/user/example-"+nameCounter);
145 Map<String, ActorRef> newActorRefs = new HashMap<String, ActorRef>(num);
146 for (Map.Entry<String, String> entry : newPeers.entrySet()) {
147 ActorRef exampleActor = createExampleActor(entry.getKey());
148 newActorRefs.put(entry.getKey(), exampleActor);
150 //now also add these new nodes as peers from the previous nodes
151 for (ActorRef actor : actorRefs.values()) {
152 actor.tell(new AddRaftPeer(entry.getKey(), entry.getValue()), null);
155 System.out.println("Added node:" + entry);
158 actorRefs.putAll(newActorRefs);
162 // add num clients to all nodes in the system
163 public void addClients(int num) {
164 for(Map.Entry<String,ActorRef> actorRefEntry : actorRefs.entrySet()) {
165 for (int i=0; i < num; i++) {
166 String clientName = "client-" + i + "-" + actorRefEntry.getKey();
167 ActorRef clientActor = actorSystem.actorOf(
168 ClientActor.props(actorRefEntry.getValue()), clientName);
169 clientActorRefs.put(clientName, clientActor);
170 System.out.println("Created client-node:" + clientName);
175 // add num clients to a node
176 public void addClientsToNode(String actorName, int num) {
177 ActorRef actorRef = actorRefs.get(actorName);
178 for (int i=0; i < num; i++) {
179 String clientName = "client-" + i + "-" + actorName;
180 clientActorRefs.put(clientName,
181 actorSystem.actorOf(ClientActor.props(actorRef), clientName));
182 System.out.println("Added client-node:" + clientName);
186 public void stopNode(String actorName) {
187 ActorRef actorRef = actorRefs.get(actorName);
189 for (Map.Entry<String,ActorRef> entry : clientActorRefs.entrySet()) {
190 if (entry.getKey().endsWith(actorName)) {
191 actorSystem.stop(entry.getValue());
195 actorSystem.stop(actorRef);
196 actorRefs.remove(actorName);
198 for (ActorRef actor : actorRefs.values()) {
199 actor.tell(new RemoveRaftPeer(actorName), null);
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);
211 for (ActorRef actor : actorRefs.values()) {
212 actor.tell(new AddRaftPeer(actorName, address), null);
215 actorRefs.put(actorName, exampleActor);
217 addClientsToNode(actorName, 1);
220 public void startAllLogging() {
221 if(!clientActorRefs.isEmpty()) {
222 for(Map.Entry<String,ActorRef> client : clientActorRefs.entrySet()) {
223 logGenerator.startLoggingForClient(client.getValue());
224 System.out.println("Started logging for client:"+client.getKey());
227 System.out.println("There are no clients for any nodes. First create clients using commands- addClients:<num> or addClientsToNode:<nodename>:<num>");
232 public void startLoggingForClient(ActorRef client) {
233 logGenerator.startLoggingForClient(client);
236 public void stopAllLogging() {
237 for(Map.Entry<String,ActorRef> client : clientActorRefs.entrySet()) {
238 logGenerator.stopLoggingForClient(client.getValue());
242 public void stopLoggingForClient(ActorRef client) {
243 logGenerator.stopLoggingForClient(client);
246 public void printState() {
247 for (ActorRef ref : actorRefs.values()) {
248 ref.tell(new PrintState(), null);
252 public void printNodes() {
253 for (ActorRef ref : actorRefs.values()) {
254 ref.tell(new PrintRole(), null);
258 public ActorRef getLeader() {
263 private static Map<String, String> withoutPeer(String peerId) {
264 Map<String, String> without = new ConcurrentHashMap<>(allPeers);
265 without.remove(peerId);