From 82123744b705e210d352f71480fcce4598990841 Mon Sep 17 00:00:00 2001 From: ztkmk Date: Wed, 16 Sep 2020 03:35:19 +0900 Subject: [PATCH] [#130] implement new master actor - modify spawnable interface - fix master actor test failure --- .../dss/core/actor/DssExceptionSpawnable.java | 2 +- .../dss/core/actor/DssMasterActor.java | 2 +- .../dss/core/actor/DssNetworkSpawnable.java | 2 +- .../dss/core/actor/DssResolverSpawnable.java | 2 +- .../dss/core/actor/DssServiceSpawnable.java | 2 +- .../core/actor/util/DssCommonActorUtils.java | 17 +- .../dss/core/actor/DssMasterActorTest.java | 193 +++++++++++------- 7 files changed, 142 insertions(+), 78 deletions(-) diff --git a/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/DssExceptionSpawnable.java b/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/DssExceptionSpawnable.java index fd49c88f..2ddc738b 100644 --- a/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/DssExceptionSpawnable.java +++ b/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/DssExceptionSpawnable.java @@ -23,7 +23,7 @@ public interface DssExceptionSpawnable extends DssExceptionAcceptable { /** * Create DssExceptionActorProperty from DssMasterActorProperty */ - E createDssExceptionActorProperty(M masterProperty); + DssExceptionActorProperty createDssExceptionActorProperty(M masterProperty); /** * spawn exception actor and set it diff --git a/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/DssMasterActor.java b/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/DssMasterActor.java index ead48de6..a4c99b56 100644 --- a/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/DssMasterActor.java +++ b/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/DssMasterActor.java @@ -20,7 +20,7 @@ public interface DssMasterActor extends DssActor, DssException /** * Get master property */ - M getDssMasterActorProperty(); + DssMasterActorProperty getDssMasterActorProperty(); /** * Get current master actor status diff --git a/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/DssNetworkSpawnable.java b/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/DssNetworkSpawnable.java index 15e4ffe9..0b517f42 100644 --- a/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/DssNetworkSpawnable.java +++ b/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/DssNetworkSpawnable.java @@ -23,7 +23,7 @@ public interface DssNetworkSpawnable extends DssNetworkAcceptable { /** * Create DssNetworkActorProperty from DssMasterActorProperty */ -

P createDssNetworkActorProperty(M masterProperty); + DssNetworkActorProperty createDssNetworkActorProperty(M masterProperty); /** * spawn network actor and set it diff --git a/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/DssResolverSpawnable.java b/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/DssResolverSpawnable.java index 6b44d034..7a0c95bb 100644 --- a/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/DssResolverSpawnable.java +++ b/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/DssResolverSpawnable.java @@ -23,7 +23,7 @@ public interface DssResolverSpawnable extends DssResolverAcceptable { /** * Create DssResolverActorProperty from DssMasterActorProperty */ -

P createDssResolverActorProperty(M masterProperty); + DssResolverActorProperty createDssResolverActorProperty(M masterProperty); /** * spawn resolver actor and set it diff --git a/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/DssServiceSpawnable.java b/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/DssServiceSpawnable.java index ff109a27..f9036c11 100644 --- a/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/DssServiceSpawnable.java +++ b/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/DssServiceSpawnable.java @@ -26,7 +26,7 @@ public interface DssServiceSpawnable extends DssServiceAcceptable { /** * Create DssServiceActorProperty list from DssMasterActorProperty */ -

List

createDssServiceActorPropertyList(M masterProperty); + List createDssServiceActorPropertyList(M masterProperty); /** * Create DssServiceActorResolvable from DssServiceActorProperty and service actor diff --git a/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/util/DssCommonActorUtils.java b/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/util/DssCommonActorUtils.java index ce68f409..dfa976a5 100644 --- a/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/util/DssCommonActorUtils.java +++ b/dss-core/src/main/java/io/github/ztkmkoo/dss/core/actor/util/DssCommonActorUtils.java @@ -15,6 +15,12 @@ */ public class DssCommonActorUtils { + private static final String MSG_BEHAVIOR_CREATOR_NULL = "DssBehaviorCreator is null"; + private static final String MSG_PROPERTY_NULL = "DssActorProperty is null"; + private static final String MSG_ACTOR_NULL = "AbstractDssActor is null"; + private static final String MSG_ACTOR_CONTEXT_NULL = "ActorContext is null"; + private static final String MSG_BEHAVIOR_NULL = "Behavior is null"; + private DssCommonActorUtils() {} public static ActorRef spawn( @@ -22,7 +28,12 @@ public static creator, T property, String name) { - return spawn(parent, Objects.requireNonNull(creator).create(Objects.requireNonNull(property)), name); + return spawn( + parent, + Objects + .requireNonNull(creator, MSG_BEHAVIOR_CREATOR_NULL) + .create(Objects.requireNonNull(property, MSG_PROPERTY_NULL)), + name); } public static ActorRef spawn( @@ -30,7 +41,7 @@ public static ActorRef spawn( Behavior behavior, String name) { return Objects - .requireNonNull(Objects.requireNonNull(parent).getContext()) - .spawn(Objects.requireNonNull(behavior), name); + .requireNonNull(Objects.requireNonNull(parent, MSG_ACTOR_NULL).getContext(), MSG_ACTOR_CONTEXT_NULL) + .spawn(Objects.requireNonNull(behavior, MSG_BEHAVIOR_NULL), name); } } diff --git a/dss-core/src/test/java/io/github/ztkmkoo/dss/core/actor/DssMasterActorTest.java b/dss-core/src/test/java/io/github/ztkmkoo/dss/core/actor/DssMasterActorTest.java index bbf8cd5d..da40c962 100644 --- a/dss-core/src/test/java/io/github/ztkmkoo/dss/core/actor/DssMasterActorTest.java +++ b/dss-core/src/test/java/io/github/ztkmkoo/dss/core/actor/DssMasterActorTest.java @@ -3,21 +3,23 @@ import akka.actor.testkit.typed.javadsl.TestProbe; import akka.actor.typed.ActorRef; import akka.actor.typed.Behavior; +import akka.actor.typed.javadsl.AbstractBehavior; import akka.actor.typed.javadsl.ActorContext; import akka.actor.typed.javadsl.Behaviors; import akka.actor.typed.javadsl.Receive; import io.github.ztkmkoo.dss.core.actor.enumeration.DssMasterActorStatus; -import io.github.ztkmkoo.dss.core.actor.property.DssMasterActorProperty; -import io.github.ztkmkoo.dss.core.message.DssCommand; -import io.github.ztkmkoo.dss.core.message.DssMasterCommand; -import io.github.ztkmkoo.dss.core.message.DssNetworkCommand; -import io.github.ztkmkoo.dss.core.message.DssResolverCommand; +import io.github.ztkmkoo.dss.core.actor.property.*; +import io.github.ztkmkoo.dss.core.message.*; +import io.github.ztkmkoo.dss.core.service.DssServiceGenerator; import lombok.Builder; import lombok.Getter; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -53,23 +55,13 @@ private static R getProbeResponse(TestProbe } @Test - void spawnResolverActor() { + void initializeMasterActor() { final ActorRef masterActor = newMasterActorRef(); final TestProbe probe = testKit.createTestProbe(); - masterActor.tell(ResolverActorRefRequest.builder().sender(probe.getRef()).build()); + masterActor.tell(new InitializeRequest(probe.getRef())); - final ResolverActorRefResponse response = getProbeResponse(probe, ResolverActorRefResponse.class); - assertNotNull(response.getActorRef()); - } - - @Test - void spawnNetworkActor() { - final ActorRef masterActor = newMasterActorRef(); - final TestProbe probe = testKit.createTestProbe(); - masterActor.tell(NetworkActorRefRequest.builder().sender(probe.getRef()).build()); - - final NetworkActorRefResponse response = getProbeResponse(probe, NetworkActorRefResponse.class); - assertNotNull(response.getActorRef()); + final InitializeResponse response = getProbeResponse(probe, InitializeResponse.class); + assertNotNull(response); } @Test @@ -107,21 +99,13 @@ private TestMasterActor(ActorContext context) { @Override public Receive createReceive() { - return newReceiveBuilder() - .onMessage(ResolverActorRefRequest.class, msg -> { - getLog().info("Receive: {}", msg); - final ActorRef resolver = spawnResolverActor(this); - msg.getSender().tell(ResolverActorRefResponse.builder().actorRef(resolver).build()); - return Behaviors.same(); - }) - .onMessage(NetworkActorRefRequest.class, msg -> { - getLog().info("Receive: {}", msg); - final ActorRef network = spawnNetworkActor(this); - msg.getSender().tell(NetworkActorRefResponse.builder().actorRef(network).build()); - return Behaviors.same(); + return masterReceiveBuilder(this) + .onMessage(InitializeRequest.class, msg -> { + initializeMasterActor(this); + Objects.requireNonNull(msg.getSender()); + msg.getSender().tell(new InitializeResponse()); + return this; }) - .onMessage(DssMasterCommand.StatusRequest.class, this::handlingStatusRequest) - .onMessage(DssMasterCommand.StatusUpdate.class, this::handlingStatusUpdate) .build(); } @@ -136,27 +120,38 @@ public ActorRef getResolverActor() { } @Override - public ActorRef getNetworkActor() { - return null; + public void setResolverActor(ActorRef actorRef) { + } @Override - public DssMasterActorProperty getProperty() { + public ActorRef getNetworkActor() { return null; } @Override - public Behavior createResolverActorBehavior() { - return Behaviors - .receive(DssResolverCommand.class) - .build(); + public void setNetworkActor(ActorRef actorRef) { + } @Override - public Behavior createNetworkActorBehavior() { - return Behaviors - .receive(DssNetworkCommand.class) - .build(); + public DssMasterActorProperty getDssMasterActorProperty() { + return new DssMasterActorProperty() { + @Override + public int getBossThreadCount() { + return 0; + } + + @Override + public int getWorkerThreadCount() { + return 0; + } + + @Override + public List getServiceGeneratorList() { + return null; + } + }; } @Override @@ -168,53 +163,111 @@ public DssMasterActorStatus getMasterActorStatus() { public void setMasterActorStatus(DssMasterActorStatus status) { this.status = status; } - } - private static class ResolverActorRefRequest implements DssMasterCommand{ - private static final long serialVersionUID = -6715113886545519228L; + @Override + public DssBehaviorCreator getExceptionBehaviorCreator() { + return property -> Behaviors.setup(TestActor::new); + } - @Getter - private final ActorRef sender; + @Override + public DssExceptionActorProperty createDssExceptionActorProperty(M masterProperty) { + return new DssExceptionActorProperty() {}; + } + + @Override + public ActorRef getExceptionActor() { + return null; + } + + @Override + public void setExceptionActor(ActorRef actorRef) { + + } + + @Override + public DssBehaviorCreator getNetworkBehaviorCreator() { + return property -> Behaviors.setup(TestActor::new); + } + + @Override + public DssNetworkActorProperty createDssNetworkActorProperty(M masterProperty) { + return new DssNetworkActorProperty() { + @Override + public int getBossThreadCount() { + return 1; + } + + @Override + public int getWorkerThreadCount() { + return 1; + } + }; + } + + @Override + public DssBehaviorCreator getResolverBehaviorCreator() { + return property -> Behaviors.setup(TestActor::new); + } + + @Override + public DssResolverActorProperty createDssResolverActorProperty(M masterProperty) { + return new DssResolverActorProperty() {}; + } + + @Override + public DssBehaviorCreator getServiceBehaviorCreator() { + return property -> Behaviors.setup(TestActor::new); + } + + @Override + public List createDssServiceActorPropertyList(M masterProperty) { + return new ArrayList<>(); + } + + @Override + public

DssServiceActorResolvable createDssServiceActorResolvable(P property, ActorRef actor) { + return null; + } + + @Override + public Map> getServiceActorMap() { + return null; + } + + @Override + public void putServiceActorResolvable(String key, DssServiceActorResolvable value) { - @Builder - public ResolverActorRefRequest(ActorRef sender) { - this.sender = sender; } } - private static class ResolverActorRefResponse implements DssMasterCommand { - private static final long serialVersionUID = 6219916186960923214L; + private static class TestActor extends AbstractBehavior { - @Getter - private final ActorRef actorRef; + public TestActor(ActorContext context) { + super(context); + } - @Builder - public ResolverActorRefResponse(ActorRef actorRef) { - this.actorRef = actorRef; + @Override + public Receive createReceive() { + return null; } } - private static class NetworkActorRefRequest implements DssMasterCommand { - private static final long serialVersionUID = 249969752402472135L; + private static class InitializeRequest implements DssMasterCommand { + private static final long serialVersionUID = -6715113886545519228L; @Getter private final ActorRef sender; @Builder - public NetworkActorRefRequest(ActorRef sender) { + public InitializeRequest(ActorRef sender) { this.sender = sender; } } - private static class NetworkActorRefResponse implements DssMasterCommand { - private static final long serialVersionUID = -7299143779560419564L; - - @Getter - private final ActorRef actorRef; + private static class InitializeResponse implements DssMasterCommand { + private static final long serialVersionUID = -4705847872255308681L; @Builder - public NetworkActorRefResponse(ActorRef actorRef) { - this.actorRef = actorRef; - } + public InitializeResponse() { } } } \ No newline at end of file