Cleanup and document FindLeaderReply 05/34605/4
authorRobert Varga <rovarga@cisco.com>
Sat, 13 Feb 2016 20:58:40 +0000 (21:58 +0100)
committerRobert Varga <rovarga@cisco.com>
Sat, 13 Feb 2016 21:38:01 +0000 (22:38 +0100)
Adds javadoc describing FindLeaderReply and makes getLeaderActor()
return an Optional<String> instead of null. Also eliminate switch log
errors via Logger, not println();

Change-Id: I7efc98b6e652d7e01192b9bb6e1bd4ad1c9d8b76
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/client/messages/FindLeaderReply.java
opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorTestKit.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardTestKit.java

index 8b2e45aa0601b3d62624f4bd3831042af36e6246..549fe038ed5467ca77d963816d4325bbcee0ad3d 100644 (file)
@@ -8,17 +8,28 @@
 
 package org.opendaylight.controller.cluster.raft.client.messages;
 
+import com.google.common.annotations.VisibleForTesting;
 import java.io.Serializable;
+import java.util.Optional;
+import javax.annotation.Nullable;
 
-public class FindLeaderReply implements Serializable {
+/**
+ * Reply to {@link FindLeader} message, containing the address of the leader actor, as known to the raft actor which
+ * sent the message. If the responding actor does not have knowledge of the leader, {@link #getLeaderActor()} will
+ * return {@link Optional#empty()}.
+ *
+ * This message is intended for testing purposes only.
+ */
+@VisibleForTesting
+public final class FindLeaderReply implements Serializable {
     private static final long serialVersionUID = 1L;
     private final String leaderActor;
 
-    public FindLeaderReply(String leaderActor) {
+    public FindLeaderReply(@Nullable final String leaderActor) {
         this.leaderActor = leaderActor;
     }
 
-    public String getLeaderActor() {
-        return leaderActor;
+    public Optional<String> getLeaderActor() {
+        return Optional.ofNullable(leaderActor);
     }
 }
index 3d9214151f6302d26631d35d06f0f4d9266c77ef..ac9f8da34ee6365d5bdc66dcb67a512cf449dfae 100644 (file)
@@ -13,17 +13,21 @@ import akka.pattern.Patterns;
 import akka.testkit.JavaTestKit;
 import akka.util.Timeout;
 import com.google.common.util.concurrent.Uninterruptibles;
+import java.util.Optional;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import org.junit.Assert;
 import org.opendaylight.controller.cluster.raft.client.messages.FindLeader;
 import org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import scala.concurrent.Await;
 import scala.concurrent.Future;
 import scala.concurrent.duration.Duration;
 import scala.concurrent.duration.FiniteDuration;
 
 public class RaftActorTestKit extends JavaTestKit {
+    private static final Logger LOG = LoggerFactory.getLogger(RaftActorTestKit.class);
     private final ActorRef raftActor;
 
     public RaftActorTestKit(ActorSystem actorSystem, String actorName) {
@@ -63,17 +67,15 @@ public class RaftActorTestKit extends JavaTestKit {
         for(int i = 0; i < 20 * 5; i++) {
             Future<Object> future = Patterns.ask(actorRef, FindLeader.INSTANCE, new Timeout(duration));
             try {
-                FindLeaderReply resp = (FindLeaderReply) Await.result(future, duration);
-                if(resp.getLeaderActor() != null) {
+                final Optional<String> maybeLeader = ((FindLeaderReply)Await.result(future, duration)).getLeaderActor();
+                if (maybeLeader.isPresent()) {
                     return;
                 }
             } catch(TimeoutException e) {
             } catch(Exception e) {
-                System.err.println("FindLeader threw ex");
-                e.printStackTrace();
+                LOG.error("FindLeader failed", e);
             }
 
-
             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
         }
 
index d9d0b9bc8c7796b9a2c7c90ebced1e4136a15244..6ea6dae2231451fc7d549c4d9880aa8d4ef85bdc 100644 (file)
@@ -11,7 +11,6 @@ package org.opendaylight.controller.cluster.datastore;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.doReturn;
@@ -233,7 +232,7 @@ public class ShardTest extends AbstractShardTest {
             shard.tell(FindLeader.INSTANCE, getRef());
             final FindLeaderReply findLeadeReply =
                     expectMsgClass(duration("5 seconds"), FindLeaderReply.class);
-            assertNull("Expected the shard not to be the leader", findLeadeReply.getLeaderActor());
+            assertFalse("Expected the shard not to be the leader", findLeadeReply.getLeaderActor().isPresent());
 
             // Signal the onChangeListenerRegistered latch to tell the thread above to proceed
             // with the election process.
@@ -332,7 +331,7 @@ public class ShardTest extends AbstractShardTest {
             shard.tell(FindLeader.INSTANCE, getRef());
             final FindLeaderReply findLeadeReply =
                     expectMsgClass(duration("5 seconds"), FindLeaderReply.class);
-            assertNull("Expected the shard not to be the leader", findLeadeReply.getLeaderActor());
+            assertFalse("Expected the shard not to be the leader", findLeadeReply.getLeaderActor().isPresent());
 
             writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
 
index 9df933b5bac75e84f13cbfbe3ec46e9bd9b0b3b8..0ac21d8cd2f9ac88c924db31580ad6c306d7950a 100644 (file)
@@ -13,17 +13,21 @@ import akka.pattern.Patterns;
 import akka.testkit.JavaTestKit;
 import akka.util.Timeout;
 import com.google.common.util.concurrent.Uninterruptibles;
+import java.util.Optional;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import org.junit.Assert;
 import org.opendaylight.controller.cluster.raft.client.messages.FindLeader;
 import org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import scala.concurrent.Await;
 import scala.concurrent.Future;
 import scala.concurrent.duration.Duration;
 import scala.concurrent.duration.FiniteDuration;
 
 public class ShardTestKit extends JavaTestKit {
+    private static final Logger LOG = LoggerFactory.getLogger(ShardTestKit.class);
 
     public ShardTestKit(ActorSystem actorSystem) {
         super(actorSystem);
@@ -51,17 +55,16 @@ public class ShardTestKit extends JavaTestKit {
         for(int i = 0; i < 20 * 5; i++) {
             Future<Object> future = Patterns.ask(shard, FindLeader.INSTANCE, new Timeout(duration));
             try {
-                FindLeaderReply resp = (FindLeaderReply)Await.result(future, duration);
-                if(resp.getLeaderActor() != null) {
-                    return resp.getLeaderActor();
+                final Optional<String> maybeLeader = ((FindLeaderReply)Await.result(future, duration)).getLeaderActor();
+                if (maybeLeader.isPresent()) {
+                    return maybeLeader.get();
                 }
             } catch(TimeoutException e) {
+                LOG.trace("FindLeader timed out", e);
             } catch(Exception e) {
-                System.err.println("FindLeader threw ex");
-                e.printStackTrace();
+                LOG.error("FindLeader failed", e);
             }
 
-
             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
         }
 
@@ -75,17 +78,16 @@ public class ShardTestKit extends JavaTestKit {
         for(int i = 0; i < 20 * 5; i++) {
             Future<Object> future = Patterns.ask(shard, FindLeader.INSTANCE, new Timeout(duration));
             try {
-                FindLeaderReply resp = (FindLeaderReply)Await.result(future, duration);
-                if(resp.getLeaderActor() == null) {
+                final Optional<String> maybeLeader = ((FindLeaderReply)Await.result(future, duration)).getLeaderActor();
+                if (!maybeLeader.isPresent()) {
                     return;
                 }
 
-                lastResponse = resp.getLeaderActor();
+                lastResponse = maybeLeader.get();
             } catch(TimeoutException e) {
                 lastResponse = e;
             } catch(Exception e) {
-                System.err.println("FindLeader threw ex");
-                e.printStackTrace();
+                LOG.error("FindLeader failed", e);
                 lastResponse = e;
             }