X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fexample%2FTestDriver.java;h=cd2e4a506ce5365bc45cc89f160695516846b4f2;hp=fd6e192bf0497777de2643a1b3f28a2a76b72e42;hb=a469dbcec569cc972df0cd57cf725a2173d2604a;hpb=7aed170ee84b4b70bc12163b07773a5944da040b diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/example/TestDriver.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/example/TestDriver.java index fd6e192bf0..cd2e4a506c 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/example/TestDriver.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/example/TestDriver.java @@ -3,17 +3,17 @@ package org.opendaylight.controller.cluster.example; import akka.actor.ActorRef; import akka.actor.ActorSystem; import com.google.common.base.Optional; -import org.opendaylight.controller.cluster.example.messages.PrintRole; -import org.opendaylight.controller.cluster.example.messages.PrintState; -import org.opendaylight.controller.cluster.raft.ConfigParams; -import org.opendaylight.controller.cluster.raft.client.messages.AddRaftPeer; -import org.opendaylight.controller.cluster.raft.client.messages.RemoveRaftPeer; - +import com.google.common.collect.Lists; +import com.typesafe.config.ConfigFactory; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import org.opendaylight.controller.cluster.example.messages.PrintRole; +import org.opendaylight.controller.cluster.example.messages.PrintState; +import org.opendaylight.controller.cluster.raft.ConfigParams; /** * This is a test driver for testing akka-raft implementation @@ -23,7 +23,7 @@ import java.util.concurrent.ConcurrentHashMap; */ public class TestDriver { - private static final ActorSystem actorSystem = ActorSystem.create(); + private static Map allPeers = new HashMap<>(); private static Map clientActorRefs = new HashMap(); private static Map actorRefs = new HashMap(); @@ -31,12 +31,14 @@ public class TestDriver { private int nameCounter = 0; private static ConfigParams configParams = new ExampleConfigParamsImpl(); + private static ActorSystem actorSystem; + private static ActorSystem listenerActorSystem; + /** * Create nodes, add clients and start logging. * Commands * bye * createNodes:{num} - * addNodes:{num} * stopNode:{nodeName} * reinstateNode:{nodeName} * addClients:{num} @@ -47,10 +49,22 @@ public class TestDriver { * stopLoggingForClient:{nodeName} * printNodes * printState + * + * Note: when run on IDE and on debug log level, the debug logs in + * AbstractUptypedActor and AbstractUptypedPersistentActor would need to be commented out. + * Also RaftActor handleCommand(), debug log which prints for every command other than AE/AER + * * @param args * @throws Exception */ public static void main(String[] args) throws Exception { + + actorSystem = ActorSystem.create("raft-test", ConfigFactory + .load().getConfig("raft-test")); + + listenerActorSystem = ActorSystem.create("raft-test-listener", ConfigFactory + .load().getConfig("raft-test-listener")); + TestDriver td = new TestDriver(); System.out.println("Enter command (type bye to exit):"); @@ -67,11 +81,6 @@ public class TestDriver { int n = Integer.parseInt(arr[1]); td.createNodes(n); - } else if (command.startsWith("addNodes")) { - String[] arr = command.split(":"); - int n = Integer.parseInt(arr[1]); - td.addNodes(n); - } else if (command.startsWith("addClients")) { String[] arr = command.split(":"); int n = Integer.parseInt(arr[1]); @@ -109,11 +118,23 @@ public class TestDriver { td.printState(); } else if (command.startsWith("printNodes")) { td.printNodes(); + } else { + System.out.println("Invalid command:" + command); } } } + // create the listener using a separate actor system for each example actor + private void createClusterRoleChangeListener(List memberIds) { + System.out.println("memberIds="+memberIds); + for (String memberId : memberIds) { + ActorRef listenerActor = listenerActorSystem.actorOf( + ExampleRoleChangeListener.getProps(memberId), memberId + "-role-change-listener"); + System.out.println("Role Change Listener created:" + listenerActor.path().toString()); + } + } + public static ActorRef createExampleActor(String name) { return actorSystem.actorOf(ExampleActor.props(name, withoutPeer(name), Optional.of(configParams)), name); @@ -122,7 +143,7 @@ public class TestDriver { public void createNodes(int num) { for (int i=0; i < num; i++) { nameCounter = nameCounter + 1; - allPeers.put("example-"+nameCounter, "akka://default/user/example-"+nameCounter); + allPeers.put("example-"+nameCounter, "akka://raft-test/user/example-"+nameCounter); } for (String s : allPeers.keySet()) { @@ -131,34 +152,10 @@ public class TestDriver { System.out.println("Created node:"+s); } - } - - // add new nodes , pass in the count - public void addNodes(int num) { - Map newPeers = new HashMap<>(); - for (int i=0; i < num; i++) { - nameCounter = nameCounter + 1; - newPeers.put("example-"+nameCounter, "akka://default/user/example-"+nameCounter); - allPeers.put("example-"+nameCounter, "akka://default/user/example-"+nameCounter); - } - Map newActorRefs = new HashMap(num); - for (Map.Entry entry : newPeers.entrySet()) { - ActorRef exampleActor = createExampleActor(entry.getKey()); - newActorRefs.put(entry.getKey(), exampleActor); - - //now also add these new nodes as peers from the previous nodes - for (ActorRef actor : actorRefs.values()) { - actor.tell(new AddRaftPeer(entry.getKey(), entry.getValue()), null); - } - - System.out.println("Added node:" + entry); - } - - actorRefs.putAll(newActorRefs); + createClusterRoleChangeListener(Lists.newArrayList(allPeers.keySet())); } - // add num clients to all nodes in the system public void addClients(int num) { for(Map.Entry actorRefEntry : actorRefs.entrySet()) { @@ -194,11 +191,6 @@ public class TestDriver { actorSystem.stop(actorRef); actorRefs.remove(actorName); - - for (ActorRef actor : actorRefs.values()) { - actor.tell(new RemoveRaftPeer(actorName), null); - } - allPeers.remove(actorName); } @@ -207,11 +199,6 @@ public class TestDriver { allPeers.put(actorName, address); ActorRef exampleActor = createExampleActor(actorName); - - for (ActorRef actor : actorRefs.values()) { - actor.tell(new AddRaftPeer(actorName, address), null); - } - actorRefs.put(actorName, exampleActor); addClientsToNode(actorName, 1);