Merge "Ignore generated journal directory"
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / example / TestDriver.java
1 package org.opendaylight.controller.cluster.example;
2
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;
11
12 import java.io.BufferedReader;
13 import java.io.InputStreamReader;
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.concurrent.ConcurrentHashMap;
17
18 /**
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.
23  */
24 public class TestDriver {
25
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();
33
34     /**
35      * Create nodes, add clients and start logging.
36      * Commands
37      *  bye
38      *  createNodes:{num}
39      *  addNodes:{num}
40      *  stopNode:{nodeName}
41      *  reinstateNode:{nodeName}
42      *  addClients:{num}
43      *  addClientsToNode:{nodeName, num}
44      *  startLogging
45      *  stopLogging
46      *  startLoggingForClient:{nodeName}
47      *  stopLoggingForClient:{nodeName}
48      *  printNodes
49      *  printState
50      * @param args
51      * @throws Exception
52      */
53     public static void main(String[] args) throws Exception {
54         TestDriver td = new TestDriver();
55
56         System.out.println("Enter command (type bye to exit):");
57
58
59         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
60         while(true) {
61             String command = br.readLine();
62             if (command.startsWith("bye")) {
63                 System.exit(0);
64
65             } else if (command.startsWith("createNodes")) {
66                 String[] arr = command.split(":");
67                 int n = Integer.parseInt(arr[1]);
68                 td.createNodes(n);
69
70             } else if (command.startsWith("addNodes")) {
71                 String[] arr = command.split(":");
72                 int n = Integer.parseInt(arr[1]);
73                 td.addNodes(n);
74
75             } else if (command.startsWith("addClients")) {
76                 String[] arr = command.split(":");
77                 int n = Integer.parseInt(arr[1]);
78                 td.addClients(n);
79
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);
85
86             } else if (command.startsWith("stopNode")) {
87                 String[] arr = command.split(":");
88                 td.stopNode(arr[1]);
89
90             } else if (command.startsWith("reinstateNode")) {
91                 String[] arr = command.split(":");
92                 td.reinstateNode(arr[1]);
93
94             } else if (command.startsWith("startLogging")) {
95                 td.startAllLogging();
96
97             } else if (command.startsWith("startLoggingForClient")) {
98                 String[] arr = command.split(":");
99                 td.startLoggingForClient(clientActorRefs.get(arr[1]));
100
101             } else if (command.startsWith("stopLogging")) {
102                 td.stopAllLogging();
103
104             } else if (command.startsWith("stopLoggingForClient")) {
105                 String[] arr = command.split(":");
106                 td.stopLoggingForClient(clientActorRefs.get(arr[1]));
107
108             } else if (command.startsWith("printState")) {
109                 td.printState();
110             } else if (command.startsWith("printNodes")) {
111                 td.printNodes();
112             }
113
114         }
115     }
116
117     public static ActorRef createExampleActor(String name) {
118         return actorSystem.actorOf(ExampleActor.props(name, withoutPeer(name),
119             Optional.of(configParams)), name);
120     }
121
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);
126         }
127
128         for (String s : allPeers.keySet())  {
129             ActorRef exampleActor = createExampleActor(s);
130             actorRefs.put(s, exampleActor);
131             System.out.println("Created node:"+s);
132
133         }
134     }
135
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);
143
144         }
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);
149
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);
153             }
154
155             System.out.println("Added node:" + entry);
156         }
157
158         actorRefs.putAll(newActorRefs);
159     }
160
161
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);
171             }
172         }
173     }
174
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);
183         }
184     }
185
186     public void stopNode(String actorName) {
187         ActorRef actorRef = actorRefs.get(actorName);
188
189         for (Map.Entry<String,ActorRef> entry : clientActorRefs.entrySet()) {
190             if (entry.getKey().endsWith(actorName)) {
191                 actorSystem.stop(entry.getValue());
192             }
193         }
194
195         actorSystem.stop(actorRef);
196         actorRefs.remove(actorName);
197
198         for (ActorRef actor : actorRefs.values()) {
199             actor.tell(new RemoveRaftPeer(actorName), null);
200         }
201
202         allPeers.remove(actorName);
203     }
204
205     public void reinstateNode(String actorName) {
206         String address = "akka://default/user/"+actorName;
207         allPeers.put(actorName, address);
208
209         ActorRef exampleActor = createExampleActor(actorName);
210
211         for (ActorRef actor : actorRefs.values()) {
212             actor.tell(new AddRaftPeer(actorName, address), null);
213         }
214
215         actorRefs.put(actorName, exampleActor);
216
217         addClientsToNode(actorName, 1);
218     }
219
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());
225             }
226         } else {
227             System.out.println("There are no clients for any nodes. First create clients using commands- addClients:<num> or addClientsToNode:<nodename>:<num>");
228         }
229
230     }
231
232     public void startLoggingForClient(ActorRef client) {
233         logGenerator.startLoggingForClient(client);
234     }
235
236     public void stopAllLogging() {
237         for(Map.Entry<String,ActorRef> client : clientActorRefs.entrySet()) {
238             logGenerator.stopLoggingForClient(client.getValue());
239         }
240     }
241
242     public void stopLoggingForClient(ActorRef client) {
243         logGenerator.stopLoggingForClient(client);
244     }
245
246     public void printState() {
247         for (ActorRef ref : actorRefs.values()) {
248             ref.tell(new PrintState(), null);
249         }
250     }
251
252     public void printNodes() {
253         for (ActorRef ref : actorRefs.values()) {
254             ref.tell(new PrintRole(), null);
255         }
256     }
257
258     public ActorRef getLeader() {
259         return null;
260     }
261
262
263     private static Map<String, String> withoutPeer(String peerId) {
264         Map<String, String> without = new ConcurrentHashMap<>(allPeers);
265         without.remove(peerId);
266
267         return without;
268     }
269 }
270