portunus_affinity behaviour (portunus v0.8.0)

View Source

An affinity strategy gives each portunus node (cluster member) a score for a key, and portunus_machine succession promotes the highest-scoring contender. Ties are broken in favor of the node that joined the succession queue first.

Two strategies are supported:

  • deterministic: a pure function of the key and the cluster members, so every node computes the same ranking. Built-in variants: fifo, pinned, preferred, hash
  • dynamic: a node scores itself from local dynamically calculated state such as metrics (load, resource consumption, etc). Built-in variants: metric, random

Affinity strategies are passed around as a spec() that carries a short name (fifo, pinned, random, and so on). To use a custom module, pass a {Module, Args} tuple where Module implements the kind/0 and score/3 callbacks.

Summary

Functions

This node's score for Key, given the current cluster Members.

Types

spec()

-type spec() :: default | {strategy() | module(), term()} | fun((term(), [node()]) -> integer()).

strategy()

-type strategy() :: fifo | pinned | preferred | hash | metric | random.

Callbacks

kind()

-callback kind() -> deterministic | dynamic.

score(Key, Members, Args)

-callback score(Key :: term(), Members :: [node()], Args :: term()) -> integer().

Functions

kind/1

-spec kind(spec()) -> deterministic | dynamic.

score(Key, Members, Args)

-spec score(spec(), term(), [node()]) -> integer().

This node's score for Key, given the current cluster Members.