When configuring the Cluster Routing in the config file using:
akka.actor.deployment {
/jobDispatcher/singleton/workerRouter {
router = round-robin-pool
nr-of-instances = 5
cluster {
enabled = on
max-number-of-instances-per-node = 1
allow-local-routees = on
}
}
}
I can lookup a routed worker using:
ActorRef actor = context().actorOf( //
FromConfig.getInstance().props( //
Props.create(MyRoutedActor.class)), //
"workerRouter");
I would prefer configuring the Pool programmatically since I want to hide the details from my user.
However using:
ActorRef actor = context().actorOf(new ClusterRouterPool(new RoundRobinPool(5), //
new ClusterRouterPoolSettings(100, 1, true, "")) //
.props(Props.create(MyRoutedActor.class)),
"workerRouter");
does not route the calls to Routees in the cluster (only the local .
How do I configure the routing correctly?
Try to use ClusterRouterPool
Akka doc says [http://doc.akka.io/docs/akka/2.4/scala/cluster-usage.html]:
Pool - router that creates routees as child actors and deploys them on remote nodes. Each router will have its own routee instances. For example, if you start a router on 3 nodes in a 10-node cluster, you will have 30 routees in total if the router is configured to use one instance per node. The routees created by the different routers will not be shared among the routers. One example of a use case for this type of router is a single master that coordinates jobs and delegates the actual work to routees running on other nodes in the cluster.
The example http://doc.akka.io/docs/akka/2.4/java/cluster-usage.html#Router_with_Pool_of_Remote_Deployed_Routees
akka.actor.deployment {
/statsService/singleton/workerRouter {
router = consistent-hashing-pool
cluster {
enabled = on
max-nr-of-instances-per-node = 3
allow-local-routees = on
use-role = compute
}
}
}
The code to do programmatically (also from the akka docs):
int totalInstances = 100;
int maxInstancesPerNode = 3;
boolean allowLocalRoutees = false;
String useRole = "compute";
ActorRef workerRouter = getContext().actorOf(
new ClusterRouterPool(new ConsistentHashingPool(0),
new ClusterRouterPoolSettings(totalInstances, maxInstancesPerNode,
allowLocalRoutees, useRole)).props(Props
.create(StatsWorker.class)), "workerRouter3");
I have an akka cluster in scala, and this is my code:
val workerRouter = context.actorOf(
ClusterRouterGroup(AdaptiveLoadBalancingGroup(MixMetricsSelector), ClusterRouterGroupSettings( //RoundRobinGroup(Nil)
totalInstances = 1000, routeesPaths = List("/user/worker"),
allowLocalRoutees = true, useRole = Some("workerRole"))).props(),
name = "pool")
Related
Given the following appliction.conf :
akka {
loglevel = debug
actor {
provider = cluster
serialization-bindings {
"sample.cluster.CborSerializable" = jackson-cbor
}
}
remote {
artery {
canonical.hostname = "127.0.0.1"
canonical.port = 0
}
}
cluster {
roles= ["testrole1" , "testrole2"]
seed-nodes = [
"akka://ClusterSystem#127.0.0.1:25251",
"akka://ClusterSystem#127.0.0.1:25252"]
downing-provider-class = "akka.cluster.sbr.SplitBrainResolverProvider"
}
}
To discern between the roles within an Actor I use :
void register(Member member) {
if (member.hasRole("testrole1")) {
//start actor a1
}
else if (member.hasRole("testrole2")) {
//start actor a2
}
}
edited from src (https://doc.akka.io/docs/akka/current/cluster-usage.html)
To enable role for a node I use the following config :
Within application.conf I configure the array for the roles but this appears to be at the cluster level rather than node level. In other words it does not seem possible to configure application.conf such that the Akka cluster is instructed to start actor a1 on node n1 and actor a2 on node n2? Should note details be specified at the level of akka.cluster in application.conf ?
For each node, is it required to specify multiple application.conf configuration files?
For example, application.conf for testrole1
akka {
loglevel = debug
actor {
provider = cluster
serialization-bindings {
"sample.cluster.CborSerializable" = jackson-cbor
}
}
remote {
artery {
canonical.hostname = "127.0.0.1"
canonical.port = 0
}
}
cluster {
roles= ["testrole1"]
seed-nodes = [
"akka://ClusterSystem#127.0.0.1:25251",
"akka://ClusterSystem#127.0.0.1:25252"]
downing-provider-class = "akka.cluster.sbr.SplitBrainResolverProvider"
}
}
application.conf for testrole2 :
akka {
loglevel = debug
actor {
provider = cluster
serialization-bindings {
"sample.cluster.CborSerializable" = jackson-cbor
}
}
remote {
artery {
canonical.hostname = "127.0.0.1"
canonical.port = 0
}
}
cluster {
roles= ["testrole2"]
seed-nodes = [
"akka://ClusterSystem#127.0.0.1:25251",
"akka://ClusterSystem#127.0.0.1:25252"]
downing-provider-class = "akka.cluster.sbr.SplitBrainResolverProvider"
}
}
The difference between each application.conf defined above is the value of akka.cluster.roles is either "testrole1" or "testrole2".
How should application.conf be configured such that the Akka cluster is instructed to start actor a1 on node n1 and actor a2 on node n2? Should node details be specified at the level of akka.cluster in application.conf ?
Update:
Another option is to pass the rolename via an environment variable? I've just noticed this is explicitly stated here: https://doc.akka.io/docs/akka/current/typed/cluster.html "The node roles are defined in the configuration property named akka.cluster.roles and typically defined in the start script as a system property or environment variable." In this scenario, use the same application.conf file for all nodes but each node uses an environment variable. For example, an updated appliction.conf (note addition of "ENV_VARIABLE")
akka {
loglevel = debug
actor {
provider = cluster
serialization-bindings {
"sample.cluster.CborSerializable" = jackson-cbor
}
}
remote {
artery {
canonical.hostname = "127.0.0.1"
canonical.port = 0
}
}
cluster {
roles= ["ENV_VARIABLE"]
seed-nodes = [
"akka://ClusterSystem#127.0.0.1:25251",
"akka://ClusterSystem#127.0.0.1:25252"]
downing-provider-class = "akka.cluster.sbr.SplitBrainResolverProvider"
}
}
Cluster startup scripts determine the role for each node via the ENV_VARIABLE parameter, is this a viable solution?
If you're going to assign different roles to different nodes, those nodes cannot use the same configuration. The easiest way to accomplish this is through n1 having "testRole1" in its akka.cluster.roles list and n2 having "testRole2" in its akka.cluster.roles list.
Everything in akka.cluster config is only configuring that node for participation in the cluster (it's configuring the cluster component on that node). A few of the settings have to be the same across the nodes of a cluster (e.g. the SBR settings), but a setting on n1 doesn't affect a setting on n2.
I am creating a kubernetes cluster using kubeadm. How can I deploy the pod on a specific node. below the code that've used to deploy the pod in a simple minikube cluster. Thanks
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
CoreV1Api api = new CoreV1Api();
V1ObjectMeta meta = new V1ObjectMeta();
meta.name("ms2-pod");
Map<String, String> labels = new HashMap<>();
labels.put("app", "ms2-pod");
meta.labels(labels);
V1ContainerPort port = new V1ContainerPort();
port.containerPort(9090);
V1Container container = new V1Container();
container.name("ms2-container");
container.image("ms2");
container.imagePullPolicy("IfNotPresent");
container.ports(Arrays.asList(port));
V1PodSpec spec = new V1PodSpec();
spec.containers(Arrays.asList(container));
V1Pod podBody = new V1Pod();
podBody.apiVersion("v1");
podBody.kind("Pod");
podBody.metadata(meta);
podBody.spec(spec);
V1Pod pod = api.createNamespacedPod("default", podBody, null, null, null);
How can we use fully the kubectl functionalities inside a kubeadm cluster using the K8S Client Api in Java?
You can use node selectors or node affinity or anti-affinity depending on what you are trying to do.
Node Name, this is the simplest way of scheduling a pod to a specific node, its usage is not recommended due to its limitations.
Node selectors will allow you to choose a node that has specific labels using label selectors
Node affinity works almost the same as node selectors but allows you to specify if the rule defined in the selector is required or preferred. Allowing the pod to be scheduled in any other node even if the constraint is not met.
An example for using a node selector is below (assuming that you added the label named "nodeLabelKey" with value "nodeLabelValue" to your node):
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
CoreV1Api api = new CoreV1Api();
V1ObjectMeta meta = new V1ObjectMeta();
meta.name("ms2-pod");
Map<String, String> labels = new HashMap<>();
labels.put("app", "ms2-pod");
meta.labels(labels);
V1ContainerPort port = new V1ContainerPort();
port.containerPort(9090);
V1Container container = new V1Container();
container.name("ms2-container");
container.image("ms2");
container.imagePullPolicy("IfNotPresent");
container.ports(Arrays.asList(port));
V1PodSpec spec = new V1PodSpec();
spec.containers(Arrays.asList(container));
V1Pod podBody = new V1Pod();
podBody.apiVersion("v1");
podBody.kind("Pod");
podBody.metadata(meta);
podBody.spec(spec);
Map<String, String> nodeSelectorMap = new HashMap<>();
nodeSelectorMap.put("nodeLabelKey", "nodeLabelValue");
spec.nodeSelector(nodeSelectorMap);
V1Pod pod = api.createNamespacedPod("default", podBody, null, null, null);
How should we retrieve the full details of all brokers(connected/disconnected) from kafka cluster/zookeeper ?
I found following way to fetch only active brokers, but I want the the IP address of broker which is serving previously in cluster but it is disconnected now
Following code snippet gives list of active brokers:
ZooKeeper zkInstance = new ZooKeeper("mymachine:port", 10000, null);
brokerIDs = zkInstance.getChildren("/brokers/ids", false);
for (String brokerID : brokerIDs) {
brokerInfo = new String(zkInstance.getData("/brokers/ids/" + brokerID, false, null));
String host=brokerInfo.substring(brokerInfo.indexOf("\"host\"")).split(",") [0].replaceAll("\"","").split(":")[1];
String port=brokerInfo.substring(brokerInfo.indexOf("\"jmx_port\"")).split(",") [0].replaceAll("\"","").split(":")[1];
System.out.println(host+":"+port);
}
Output:
my-machine-1:port
my-machine-2:port
my-machine-3:port
my-machine-4:port
I need information of all connected/disconnected brokers in multi node kafka cluster
Use describeCluster() of the AdminClient class to get the broker details such as host,port,id and rock.
Please refer the following code:
Properties kafkaProperties = new Properties();
kafkaProperties.put("bootstrap.servers", "localhost:9092,localhost:9093,localhost:9094");
AdminClient adminClient = AdminClient.create(kafkaProperties);
DescribeClusterResult describeClusterResult = adminClient.describeCluster();
Collection<Node> brokerDetails = describeClusterResult.nodes().get();
System.out.println("host and port details");
for(Node broker:brokerDetails) {
System.out.println(broker.host()+":"+broker.port());
}
I am new to Kafka. Tried to implement consumer and producer classes to send and receive messages. Need to configure bootstrap.servers for both classes which is a list of broker's ip and port separated by ,. For example,
producerConfig.put("bootstrap.servers",
"172.16.20.207:9946,172.16.20.234:9125,172.16.20.36:9636");
Since the application will be running on the master node of a cluster, it should be able to retrieve the broker information from ZooKeeper just like the answer to Kafka: Get broker host from ZooKeeper.
public static void main(String[] args) throws Exception {
ZooKeeper zk = new ZooKeeper("localhost:2181", 10000, null);
List<String> ids = zk.getChildren("/brokers/ids", false);
for (String id : ids) {
String brokerInfo = new String(zk.getData("/brokers/ids/" + id, false, null));
System.out.println(id + ": " + brokerInfo);
}
}
However this brokerInfo is in Json format which looks like this:
{"jmx_port":-1,"timestamp":"1428512949385","host":"192.168.0.11","version":1,"port":9093}
In this same post, another one suggested the following way of getting connection string for each broker and join them together with comma.
for (String id : ids) {
String brokerInfoString = new String(zk.getData("/brokers/ids/" + id, false, null));
Broker broker = Broker.createBroker(Integer.valueOf(id), brokerInfoString);
if (broker != null) {
brokerList.add(broker.connectionString());
}
}
If this Broker class is from org.apache.kafka.common.requests.UpdateMetadataRequest.Broker, it does not have methods createBroker and connectionString.
Found another similar post Getting the list of Brokers Dynamically. But it did not say how to get the attribute from broker info such as host and port. I can probably write a parser for the json like string to extract them, but I suspect there is more Kafka native way to do that. Any suggestions?
EDIT: I realized the Broker class is from kafka.cluster.Broker. Still it does not have method connectionstring().
You could use ZkUtils to retrieve all the broker information in the cluster, as show below:
ZkUtils zk = ZkUtils.apply("zkHost:2181", 6000, 6000, true);
List<Broker> brokers = JavaConversions.seqAsJavaList(zk.getAllBrokersInCluster());
for (Broker broker : brokers) {
//assuming you do not enable security
System.out.println(broker.getBrokerEndPoint(SecurityProtocol.PLAINTEXT).host());
}
zk.close();
I have an actor who is routing messages to a group of other actors who act as wrappers on top of a volatile service. So far everythign is great, but I'd like to be able to control how many actors exist acting on this service (since they may represent socket connections or other physical properties) so beign able to manage scaling them would be nice.
I see that there is a remove routee method on the router and it does remove the routes, but is there a way to send a poison pill to my child actors first before they are removed? The docs say that a poison pill message should come through when removing the routee this way but I'm not seeing that happen.
I have code like this
final Collection<Routee> routees = JavaConversions.asJavaCollection(router.routees());
for (final Routee routee : routees.stream()
.limit(numberToRemove)
.collect(toList())) {
router = router.removeRoutee(routee);
}
So looks like I was missing the fact that I had to send a poison pill manually in order to stop my router. Here is a full scala demo app
import akka.actor._
import akka.routing._
case class Add()
case class Remove()
class Worker(id: Integer) extends UntypedActor {
println(s"Made worker $id")
#throws[Exception](classOf[Exception]) override
def preStart(): Unit = {
println(s"Starting $id")
}
#throws[Exception](classOf[Exception]) override
def postStop(): Unit = {
println(s"Stopping $id")
}
#throws[Exception](classOf[Exception])
override def onReceive(message: Any): Unit = message match {
case _ => println(s"Message received on actor $id")
}
}
class Master extends Actor {
var count = 0
def makeWorker() = {
val id = count
count = count + 1
context.actorOf(Props(new Worker(id)))
}
var activeWorkers = Seq.fill(2) {
makeWorker()
}
var router = Router(RoundRobinRoutingLogic(), activeWorkers.map(r => {
context watch r
ActorRefRoutee(r)
}).toIndexedSeq)
def receive = {
case Remove =>
println("Removing route")
val head = router.routees.head.asInstanceOf[ActorRefRoutee].ref
head ! PoisonPill
context unwatch head
router = router.removeRoutee(head)
printRoutes()
case Add =>
println("Adding route")
val worker = makeWorker()
context watch worker
router = router.addRoutee(worker)
printRoutes()
case w: AnyRef =>
printRoutes()
router.route(w, sender())
}
def printRoutes(): Unit ={
val size = router.routees.size
println(s"Total routes $size")
}
}
object Main extends App {
var system = ActorSystem.create("foo")
var master = system.actorOf(Props[Master])
master ! "foo"
master ! Remove
master ! "foo"
master ! "bar"
master ! Add
master ! "biz"
}