Giter Site home page Giter Site logo

akka-locality's Introduction

akka-locality

This module provides constructs that help to make better use of the locality of actors within a clustered Akka system. For a full explanation of the problem it addresses, check out this article

SBT

libraryDependencies += "io.bernhardt" %% "akka-locality" % "1.1.0"

Maven

<dependency>
  <groupId>io.bernhardt</groupId>
  <artifactId>akka-locality_2.12</artifactId>
  <version>1.1.0</version>
</dependency>

Shard location aware routers

This type of router is useful for systems in which the routees of cluster-aware routers need to communicate with sharded entities.

With a common routing logic (random, round-robin) there may be an extra network hop (or two when considering replies) between a routee and the sharded entities it needs to talk to. Shard location aware routers optimize this by routing to the routee closest to the sharded entity. It does so by using the same rules for extracting the shardId from a message as used by the shard regions themselves.

When the router has not yet retrieved sharding state, it falls back to random routing. When there are more than one candidate routee close to a sharded entity, one of them is picked at random.

In order to use these routers, the Locality extension must be started:

Scala

import io.bernhardt.akka.locality._
import akka.actor.ActorSystem

val system: ActorSystem = ActorSystem("system")
val locality = Locality(system)

Java

import io.bernhardt.akka.locality;
import akka.actor.ActorSystem;

ActorSystem system = ActorSystem.create("system");
Locality locality = Locality.get(system);

You can then use the group or pool routers as a cluster-aware router. These routers must be declared in code, as they require to be passed elements from the sharding setup:

Scala

import akka.actor.{ActorSystem, ActorRef}
import akka.cluster.sharding.ShardRegion
import akka.cluster.routing._

import io.bernhardt.akka.locality.Locality

val system: ActorSystem = ActorSystem("system")
val locality: Locality = Locality(system)
val extractEntityId: ShardRegion.ExtractEntityId = ???
val extractShardId: ShardRegion.ExtractShardId = ???
val region: ActorRef = ???

val router = system.actorOf(ClusterRouterGroup(locality.shardLocationAwareGroup(
  routeePaths = Nil,
  shardRegion = region,
  extractEntityId = extractEntityId,
  extractShardId = extractShardId
), ClusterRouterGroupSettings(
  totalInstances = 5,
  routeesPaths = List("/user/routee"),
  allowLocalRoutees = true
)).props(), "shard-location-aware-router")

Java

import akka.actor.ActorSystem;
import akka.actor.ActorRef;
import akka.cluster.sharding.ShardRegion;
import akka.cluster.routing.ClusterRouterGroup;
import akka.cluster.routing.ClusterRouterGroupSettings;

ActorRef region = ...;
ShardRegion.MessageExtractor messageExtractor = ...;
int totalInstances = 5;
Iterable<String> routeesPaths = Collections.singletonList("/user/routee");
boolean allowLocalRoutees = true;
Set<String> useRoles = new HashSet<>(Arrays.asList("role"));

ActorRef router = system.actorOf(
    new ClusterRouterGroup(
        locality.shardLocationAwareGroup(
            routeesPaths,
            region,
            messageExtractor
        ),
        new ClusterRouterGroupSettings(
            totalInstances,
            routeesPaths,
            allowLocalRoutees,
            useRoles
        )
    ).props(), "shard-location-aware-router");

Always make sure that:

  • you use exactly the same logic for the routers as you use for sharding
  • you deploy the routers on all the nodes on which sharding is enabled

Configuration

See reference.conf for more information about the configuration of the routing mechanism.

akka-locality's People

Contributors

manuelbernhardt avatar ollyw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

ollyw goseign

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.