X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft-example%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fexample%2FTestDriver.java;h=b8be5d368012d53721c2e981ac5a5c4f6e2b0352;hp=cd2e4a506ce5365bc45cc89f160695516846b4f2;hb=refs%2Fchanges%2F09%2F83009%2F6;hpb=cd03422804f6371168be7dd014cc3fe215b35742 diff --git a/opendaylight/md-sal/sal-akka-raft-example/src/main/java/org/opendaylight/controller/cluster/example/TestDriver.java b/opendaylight/md-sal/sal-akka-raft-example/src/main/java/org/opendaylight/controller/cluster/example/TestDriver.java index cd2e4a506c..b8be5d3680 100644 --- a/opendaylight/md-sal/sal-akka-raft-example/src/main/java/org/opendaylight/controller/cluster/example/TestDriver.java +++ b/opendaylight/md-sal/sal-akka-raft-example/src/main/java/org/opendaylight/controller/cluster/example/TestDriver.java @@ -1,15 +1,23 @@ +/* + * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ package org.opendaylight.controller.cluster.example; import akka.actor.ActorRef; import akka.actor.ActorSystem; -import com.google.common.base.Optional; import com.google.common.collect.Lists; import com.typesafe.config.ConfigFactory; import java.io.BufferedReader; import java.io.InputStreamReader; +import java.nio.charset.Charset; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import org.opendaylight.controller.cluster.example.messages.PrintRole; import org.opendaylight.controller.cluster.example.messages.PrintState; @@ -21,12 +29,11 @@ import org.opendaylight.controller.cluster.raft.ConfigParams; * Each ExampleActor can have one or more ClientActors. Each ClientActor spawns * a thread and starts push logs to the actor its assigned to. */ +@SuppressWarnings("checkstyle:RegexpSingleLineJava") public class TestDriver { - - private static Map allPeers = new HashMap<>(); - private static Map clientActorRefs = new HashMap(); - private static Map actorRefs = new HashMap(); + private static Map clientActorRefs = new HashMap<>(); + private static Map actorRefs = new HashMap<>(); private static LogGenerator logGenerator = new LogGenerator(); private int nameCounter = 0; private static ConfigParams configParams = new ExampleConfigParamsImpl(); @@ -50,14 +57,12 @@ public class TestDriver { * 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 { + public static void main(final String[] args) throws Exception { actorSystem = ActorSystem.create("raft-test", ConfigFactory .load().getConfig("raft-test")); @@ -70,27 +75,30 @@ public class TestDriver { System.out.println("Enter command (type bye to exit):"); - BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - while(true) { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset())); + while (true) { String command = br.readLine(); + if (command == null) { + continue; + } if (command.startsWith("bye")) { System.exit(0); } else if (command.startsWith("createNodes")) { String[] arr = command.split(":"); - int n = Integer.parseInt(arr[1]); - td.createNodes(n); + int num = Integer.parseInt(arr[1]); + td.createNodes(num); } else if (command.startsWith("addClients")) { String[] arr = command.split(":"); - int n = Integer.parseInt(arr[1]); - td.addClients(n); + int num = Integer.parseInt(arr[1]); + td.addClients(num); } else if (command.startsWith("addClientsToNode")) { String[] arr = command.split(":"); String nodeName = arr[1]; - int n = Integer.parseInt(arr[1]); - td.addClientsToNode(nodeName, n); + int num = Integer.parseInt(arr[1]); + td.addClientsToNode(nodeName, num); } else if (command.startsWith("stopNode")) { String[] arr = command.split(":"); @@ -126,8 +134,8 @@ public class TestDriver { } // create the listener using a separate actor system for each example actor - private void createClusterRoleChangeListener(List memberIds) { - System.out.println("memberIds="+memberIds); + private static void createClusterRoleChangeListener(final List memberIds) { + System.out.println("memberIds=" + memberIds); for (String memberId : memberIds) { ActorRef listenerActor = listenerActorSystem.actorOf( ExampleRoleChangeListener.getProps(memberId), memberId + "-role-change-listener"); @@ -135,31 +143,30 @@ public class TestDriver { } } - public static ActorRef createExampleActor(String name) { + public static ActorRef createExampleActor(final String name) { return actorSystem.actorOf(ExampleActor.props(name, withoutPeer(name), Optional.of(configParams)), name); } - public void createNodes(int num) { - for (int i=0; i < num; i++) { + public void createNodes(final int num) { + for (int i = 0; i < num; i++) { nameCounter = nameCounter + 1; - allPeers.put("example-"+nameCounter, "akka://raft-test/user/example-"+nameCounter); + allPeers.put("example-" + nameCounter, "akka://raft-test/user/example-" + nameCounter); } for (String s : allPeers.keySet()) { ActorRef exampleActor = createExampleActor(s); actorRefs.put(s, exampleActor); - System.out.println("Created node:"+s); - + System.out.println("Created node:" + s); } 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()) { - for (int i=0; i < num; i++) { + public void addClients(final int num) { + for (Map.Entry actorRefEntry : actorRefs.entrySet()) { + for (int i = 0; i < num; i++) { String clientName = "client-" + i + "-" + actorRefEntry.getKey(); ActorRef clientActor = actorSystem.actorOf( ClientActor.props(actorRefEntry.getValue()), clientName); @@ -170,9 +177,9 @@ public class TestDriver { } // add num clients to a node - public void addClientsToNode(String actorName, int num) { + public void addClientsToNode(final String actorName, final int num) { ActorRef actorRef = actorRefs.get(actorName); - for (int i=0; i < num; i++) { + for (int i = 0; i < num; i++) { String clientName = "client-" + i + "-" + actorName; clientActorRefs.put(clientName, actorSystem.actorOf(ClientActor.props(actorRef), clientName)); @@ -180,7 +187,7 @@ public class TestDriver { } } - public void stopNode(String actorName) { + public void stopNode(final String actorName) { ActorRef actorRef = actorRefs.get(actorName); for (Map.Entry entry : clientActorRefs.entrySet()) { @@ -194,8 +201,8 @@ public class TestDriver { allPeers.remove(actorName); } - public void reinstateNode(String actorName) { - String address = "akka://default/user/"+actorName; + public void reinstateNode(final String actorName) { + String address = "akka://default/user/" + actorName; allPeers.put(actorName, address); ActorRef exampleActor = createExampleActor(actorName); @@ -205,28 +212,29 @@ public class TestDriver { } public void startAllLogging() { - if(!clientActorRefs.isEmpty()) { - for(Map.Entry client : clientActorRefs.entrySet()) { + if (!clientActorRefs.isEmpty()) { + for (Map.Entry client : clientActorRefs.entrySet()) { logGenerator.startLoggingForClient(client.getValue()); - System.out.println("Started logging for client:"+client.getKey()); + System.out.println("Started logging for client:" + client.getKey()); } } else { - System.out.println("There are no clients for any nodes. First create clients using commands- addClients: or addClientsToNode::"); + System.out.println( + "There are no clients for any nodes. First create clients using commands- addClients: or " + + "addClientsToNode::"); } - } - public void startLoggingForClient(ActorRef client) { + public void startLoggingForClient(final ActorRef client) { logGenerator.startLoggingForClient(client); } public void stopAllLogging() { - for(Map.Entry client : clientActorRefs.entrySet()) { + for (Map.Entry client : clientActorRefs.entrySet()) { logGenerator.stopLoggingForClient(client.getValue()); } } - public void stopLoggingForClient(ActorRef client) { + public void stopLoggingForClient(final ActorRef client) { logGenerator.stopLoggingForClient(client); } @@ -247,7 +255,7 @@ public class TestDriver { } - private static Map withoutPeer(String peerId) { + private static Map withoutPeer(final String peerId) { Map without = new ConcurrentHashMap<>(allPeers); without.remove(peerId);