2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.cluster;
|
2017-02-14 13:10:23 +02:00
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
import akka.actor.Props;
|
|
|
|
|
import akka.cluster.metrics.AdaptiveLoadBalancingGroup;
|
|
|
|
|
import akka.cluster.metrics.AdaptiveLoadBalancingPool;
|
|
|
|
|
import akka.cluster.metrics.HeapMetricsSelector;
|
|
|
|
|
import akka.cluster.metrics.SystemLoadAverageMetricsSelector;
|
|
|
|
|
import akka.cluster.routing.ClusterRouterGroup;
|
|
|
|
|
import akka.cluster.routing.ClusterRouterGroupSettings;
|
|
|
|
|
import akka.cluster.routing.ClusterRouterPool;
|
|
|
|
|
import akka.cluster.routing.ClusterRouterPoolSettings;
|
|
|
|
|
import scala.concurrent.duration.Duration;
|
|
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.ReceiveTimeout;
|
2017-02-23 00:50:07 +05:00
|
|
|
import akka.actor.AbstractActor;
|
2017-02-14 13:10:23 +02:00
|
|
|
import akka.event.Logging;
|
|
|
|
|
import akka.event.LoggingAdapter;
|
|
|
|
|
import akka.routing.FromConfig;
|
|
|
|
|
|
|
|
|
|
//#frontend
|
2017-02-23 00:50:07 +05:00
|
|
|
public class FactorialFrontend extends AbstractActor {
|
2017-02-14 13:10:23 +02:00
|
|
|
final int upToN;
|
|
|
|
|
final boolean repeat;
|
|
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);
|
2017-02-14 13:10:23 +02:00
|
|
|
|
|
|
|
|
ActorRef backend = getContext().actorOf(FromConfig.getInstance().props(),
|
|
|
|
|
"factorialBackendRouter");
|
|
|
|
|
|
|
|
|
|
public FactorialFrontend(int upToN, boolean repeat) {
|
|
|
|
|
this.upToN = upToN;
|
|
|
|
|
this.repeat = repeat;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void preStart() {
|
|
|
|
|
sendJobs();
|
|
|
|
|
getContext().setReceiveTimeout(Duration.create(10, TimeUnit.SECONDS));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2017-02-23 00:50:07 +05:00
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.match(FactorialResult.class, result -> {
|
|
|
|
|
if (result.n == upToN) {
|
|
|
|
|
log.debug("{}! = {}", result.n, result.factorial);
|
|
|
|
|
if (repeat)
|
|
|
|
|
sendJobs();
|
|
|
|
|
else
|
2017-03-17 10:18:15 +01:00
|
|
|
getContext().stop(getSelf());
|
2017-02-23 00:50:07 +05:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.match(ReceiveTimeout.class, x -> {
|
|
|
|
|
log.info("Timeout");
|
|
|
|
|
sendJobs();
|
|
|
|
|
})
|
|
|
|
|
.build();
|
2017-02-14 13:10:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sendJobs() {
|
|
|
|
|
log.info("Starting batch of factorials up to [{}]", upToN);
|
|
|
|
|
for (int n = 1; n <= upToN; n++) {
|
2017-03-16 09:30:00 +01:00
|
|
|
backend.tell(n, getSelf());
|
2017-02-14 13:10:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//#frontend
|
|
|
|
|
|
|
|
|
|
//not used, only for documentation
|
2017-02-23 00:50:07 +05:00
|
|
|
abstract class FactorialFrontend2 extends AbstractActor {
|
2017-02-14 13:10:23 +02:00
|
|
|
//#router-lookup-in-code
|
|
|
|
|
int totalInstances = 100;
|
|
|
|
|
Iterable<String> routeesPaths = Arrays.asList("/user/factorialBackend", "");
|
|
|
|
|
boolean allowLocalRoutees = true;
|
|
|
|
|
String useRole = "backend";
|
|
|
|
|
ActorRef backend = getContext().actorOf(
|
|
|
|
|
new ClusterRouterGroup(new AdaptiveLoadBalancingGroup(
|
|
|
|
|
HeapMetricsSelector.getInstance(), Collections.<String> emptyList()),
|
|
|
|
|
new ClusterRouterGroupSettings(totalInstances, routeesPaths,
|
|
|
|
|
allowLocalRoutees, useRole)).props(), "factorialBackendRouter2");
|
|
|
|
|
//#router-lookup-in-code
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//not used, only for documentation
|
2017-02-23 00:50:07 +05:00
|
|
|
abstract class FactorialFrontend3 extends AbstractActor {
|
2017-02-14 13:10:23 +02:00
|
|
|
//#router-deploy-in-code
|
|
|
|
|
int totalInstances = 100;
|
|
|
|
|
int maxInstancesPerNode = 3;
|
|
|
|
|
boolean allowLocalRoutees = false;
|
|
|
|
|
String useRole = "backend";
|
|
|
|
|
ActorRef backend = getContext().actorOf(
|
|
|
|
|
new ClusterRouterPool(new AdaptiveLoadBalancingPool(
|
|
|
|
|
SystemLoadAverageMetricsSelector.getInstance(), 0),
|
|
|
|
|
new ClusterRouterPoolSettings(totalInstances, maxInstancesPerNode,
|
|
|
|
|
allowLocalRoutees, useRole)).props(Props
|
|
|
|
|
.create(FactorialBackend.class)), "factorialBackendRouter3");
|
|
|
|
|
//#router-deploy-in-code
|
|
|
|
|
}
|