X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft-example%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fexample%2FTestDriver.java;h=8f414c7a6b2dd636818463fb29d8280b858835df;hb=0f02b7edeb1454c1a568f0f1b050757e7503ddf7;hp=cd2e4a506ce5365bc45cc89f160695516846b4f2;hpb=a54ec60368110d22794602343c934902f6833c65;p=controller.git 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..8f414c7a6b 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,3 +1,11 @@ +/* + * 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; @@ -7,6 +15,7 @@ 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; @@ -21,12 +30,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,12 +58,10 @@ 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 { @@ -70,27 +76,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 +135,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(List memberIds) { + System.out.println("memberIds=" + memberIds); for (String memberId : memberIds) { ActorRef listenerActor = listenerActorSystem.actorOf( ExampleRoleChangeListener.getProps(memberId), memberId + "-role-change-listener"); @@ -141,16 +150,15 @@ public class TestDriver { } public void createNodes(int num) { - for (int i=0; i < num; i++) { + 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())); @@ -158,8 +166,8 @@ public class TestDriver { // 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++) { + 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); @@ -172,7 +180,7 @@ public class TestDriver { // add num clients to a node public void addClientsToNode(String actorName, 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)); @@ -195,7 +203,7 @@ public class TestDriver { } public void reinstateNode(String actorName) { - String address = "akka://default/user/"+actorName; + String address = "akka://default/user/" + actorName; allPeers.put(actorName, address); ActorRef exampleActor = createExampleActor(actorName); @@ -205,15 +213,16 @@ 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) { @@ -221,7 +230,7 @@ public class TestDriver { } public void stopAllLogging() { - for(Map.Entry client : clientActorRefs.entrySet()) { + for (Map.Entry client : clientActorRefs.entrySet()) { logGenerator.stopLoggingForClient(client.getValue()); } }