From: Robert Varga Date: Tue, 17 May 2016 20:36:55 +0000 (+0200) Subject: BUG-5626: use lambdas, Runnable and Consumer instead of Procedure X-Git-Tag: release/boron~182 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=dc538304e4d5f91185c140cc227575f4305344df BUG-5626: use lambdas, Runnable and Consumer instead of Procedure This eliminates the use of Procedure in SnapshotManager, converting - Procedure to java.lang.Runnable - Procedure --- diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorSnapshotMessageSupport.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorSnapshotMessageSupport.java index d33f780a2d..328de80a83 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorSnapshotMessageSupport.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorSnapshotMessageSupport.java @@ -8,7 +8,6 @@ package org.opendaylight.controller.cluster.raft; import akka.actor.ActorRef; -import akka.japi.Procedure; import akka.persistence.SaveSnapshotFailure; import akka.persistence.SaveSnapshotSuccess; import com.google.common.annotations.VisibleForTesting; @@ -40,20 +39,6 @@ class RaftActorSnapshotMessageSupport { private final RaftActorSnapshotCohort cohort; private final Logger log; - private final Procedure createSnapshotProcedure = new Procedure() { - @Override - public void apply(Void notUsed) { - cohort.createSnapshot(context.getActor()); - } - }; - - private final Procedure applySnapshotProcedure = new Procedure() { - @Override - public void apply(byte[] state) { - cohort.applySnapshot(state); - } - }; - private Duration snapshotReplyActorTimeout = Duration.create(30, TimeUnit.SECONDS); RaftActorSnapshotMessageSupport(final RaftActorContext context, final RaftActorSnapshotCohort cohort) { @@ -61,8 +46,8 @@ class RaftActorSnapshotMessageSupport { this.cohort = cohort; this.log = context.getLogger(); - context.getSnapshotManager().setCreateSnapshotCallable(createSnapshotProcedure); - context.getSnapshotManager().setApplySnapshotProcedure(applySnapshotProcedure); + context.getSnapshotManager().setCreateSnapshotRunnable(() -> cohort.createSnapshot(context.getActor())); + context.getSnapshotManager().setApplySnapshotConsumer(cohort::applySnapshot); } boolean handleSnapshotMessage(Object message, ActorRef sender) { diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/SnapshotManager.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/SnapshotManager.java index 7109980f3d..b195686d20 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/SnapshotManager.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/SnapshotManager.java @@ -8,10 +8,10 @@ package org.opendaylight.controller.cluster.raft; -import akka.japi.Procedure; import akka.persistence.SnapshotSelectionCriteria; import com.google.common.annotations.VisibleForTesting; import java.util.List; +import java.util.function.Consumer; import org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot; import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot; import org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot; @@ -37,10 +37,10 @@ public class SnapshotManager implements SnapshotState { private CaptureSnapshot captureSnapshot; private long lastSequenceNumber = -1; - private Procedure createSnapshotProcedure; + private Runnable createSnapshotProcedure; private ApplySnapshot applySnapshot; - private Procedure applySnapshotProcedure; + private Consumer applySnapshotProcedure; public SnapshotManager(RaftActorContext context, Logger logger) { this.context = context; @@ -91,11 +91,11 @@ public class SnapshotManager implements SnapshotState { return currentState.trimLog(desiredTrimIndex); } - public void setCreateSnapshotCallable(Procedure createSnapshotProcedure) { + public void setCreateSnapshotRunnable(Runnable createSnapshotProcedure) { this.createSnapshotProcedure = createSnapshotProcedure; } - public void setApplySnapshotProcedure(Procedure applySnapshotProcedure) { + public void setApplySnapshotConsumer(Consumer applySnapshotProcedure) { this.applySnapshotProcedure = applySnapshotProcedure; } @@ -250,7 +250,7 @@ public class SnapshotManager implements SnapshotState { SnapshotManager.this.currentState = CREATING; try { - createSnapshotProcedure.apply(null); + createSnapshotProcedure.run(); } catch (Exception e) { SnapshotManager.this.currentState = IDLE; LOG.error("Error creating snapshot", e); @@ -399,7 +399,7 @@ public class SnapshotManager implements SnapshotState { context.getTermInformation().update(snapshot.getElectionTerm(), snapshot.getElectionVotedFor()); if(snapshot.getState().length > 0 ) { - applySnapshotProcedure.apply(snapshot.getState()); + applySnapshotProcedure.accept(snapshot.getState()); } applySnapshot.getCallback().onSuccess(); diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/MockRaftActorContext.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/MockRaftActorContext.java index 34572653c3..723de650c2 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/MockRaftActorContext.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/MockRaftActorContext.java @@ -116,7 +116,7 @@ public class MockRaftActorContext extends RaftActorContextImpl { @Override public SnapshotManager getSnapshotManager() { SnapshotManager snapshotManager = super.getSnapshotManager(); - snapshotManager.setCreateSnapshotCallable(NoopProcedure.instance()); + snapshotManager.setCreateSnapshotRunnable(() -> { }); return snapshotManager; } diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/SnapshotManagerTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/SnapshotManagerTest.java index e6f7bd7b9e..e35c9bd44a 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/SnapshotManagerTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/SnapshotManagerTest.java @@ -22,7 +22,6 @@ import static org.mockito.Mockito.reset; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import akka.actor.ActorRef; -import akka.japi.Procedure; import akka.persistence.SnapshotSelectionCriteria; import akka.testkit.TestActorRef; import java.util.Arrays; @@ -59,7 +58,7 @@ public class SnapshotManagerTest extends AbstractActorTest { private RaftActorBehavior mockRaftActorBehavior; @Mock - private Procedure mockProcedure; + private Runnable mockProcedure; @Mock private ElectionTerm mockElectionTerm; @@ -94,7 +93,7 @@ public class SnapshotManagerTest extends AbstractActorTest { actorRef = factory.createTestActor(MessageCollectorActor.props(), factory.generateActorId("test-")); doReturn(actorRef).when(mockRaftActorContext).getActor(); - snapshotManager.setCreateSnapshotCallable(mockProcedure); + snapshotManager.setCreateSnapshotRunnable(mockProcedure); } @After @@ -116,7 +115,7 @@ public class SnapshotManagerTest extends AbstractActorTest { assertEquals(true, snapshotManager.isCapturing()); - verify(mockProcedure).apply(null); + verify(mockProcedure).run(); CaptureSnapshot captureSnapshot = snapshotManager.getCaptureSnapshot(); @@ -143,7 +142,7 @@ public class SnapshotManagerTest extends AbstractActorTest { assertEquals(true, snapshotManager.isCapturing()); - verify(mockProcedure).apply(null); + verify(mockProcedure).run(); CaptureSnapshot captureSnapshot = snapshotManager.getCaptureSnapshot(); @@ -171,7 +170,7 @@ public class SnapshotManagerTest extends AbstractActorTest { assertEquals(true, snapshotManager.isCapturing()); - verify(mockProcedure).apply(null); + verify(mockProcedure).run(); CaptureSnapshot captureSnapshot = snapshotManager.getCaptureSnapshot(); @@ -194,7 +193,7 @@ public class SnapshotManagerTest extends AbstractActorTest { @Test public void testCaptureWithCreateProcedureError () throws Exception { - doThrow(new Exception("mock")).when(mockProcedure).apply(null); + doThrow(new RuntimeException("mock")).when(mockProcedure).run(); boolean capture = snapshotManager.capture(new MockRaftActorContext.MockReplicatedLogEntry(1,9, new MockRaftActorContext.MockPayload()), 9); @@ -203,7 +202,7 @@ public class SnapshotManagerTest extends AbstractActorTest { assertEquals(false, snapshotManager.isCapturing()); - verify(mockProcedure).apply(null); + verify(mockProcedure).run(); } @Test @@ -213,7 +212,7 @@ public class SnapshotManagerTest extends AbstractActorTest { assertTrue(capture); - verify(mockProcedure).apply(null); + verify(mockProcedure).run(); reset(mockProcedure); @@ -223,7 +222,7 @@ public class SnapshotManagerTest extends AbstractActorTest { assertFalse(capture); - verify(mockProcedure, never()).apply(null); + verify(mockProcedure, never()).run(); } @Test