Kamon icon indicating copy to clipboard operation
Kamon copied to clipboard

Invalid context propagated via Akka load-balancing router

Open pawelkaczor opened this issue 2 years ago • 0 comments

Below is the implementation of Akka load balancing router that I use for inter service communication in Akka cluster. According to my observations the router occasionally propagates invalid context (a context of a different message). I had to resolve the problem by manually passing the context information in an envelope.

import akka.actor.{ActorContext, ActorRef}
import akka.cluster.metrics.{AdaptiveLoadBalancingGroup, CpuMetricsSelector}
import akka.cluster.routing.{ClusterRouterGroup, ClusterRouterGroupSettings}

trait AdaptiveRouter {
  implicit val context: ActorContext

  def adaptiveLoadBalancingRouter(path: String, name: String, role: String): ActorRef = {
    val groupLogic = AdaptiveLoadBalancingGroup(metricsSelector = CpuMetricsSelector)
    val settings = ClusterRouterGroupSettings(
      totalInstances = 256,
      routeesPaths = List(path),
      allowLocalRoutees = true,
      useRoles = Set(role)
    )

    context.actorOf(ClusterRouterGroup(groupLogic, settings).props(), name)
  }
}


class Router extends Actor with AdaptiveRouter {

  private val router: ActorRef = adaptiveLoadBalancingRouter(...)

  def receive: Receive = {
    case msg =>
      (router ? other).pipeTo(sender())
  }
                                                
}

pawelkaczor avatar May 30 '22 07:05 pawelkaczor