A behavior for building eventually consistent, replicated data stores across distributed nodes.
This module provides the infrastructure for:
- Replicating data across all nodes running the same scope
- Automatic peer discovery
- Subscription-based notifications for data changes
- Version translation for rolling upgrades
Implementing modules define how data is stored, updated, and how changes
are calculated and propagated. This is a generalization of the pg module.
Summary
Types
-type global_view() :: dynamic().
-type local_data() :: dynamic().
-type scope() :: atom().
-type state() :: #state{scope :: atom(), module :: module(), options :: options(), version :: version(), global_view :: global_view(), local_data :: local_data(), peers :: #{pid() => {reference(), version(), local_data()}}, subscriptions :: subscriptions(), subscribe_refs :: #{reference() => subscription()}}.
-type subscribe_result() :: dynamic().
-type subscription() :: dynamic().
-type subscriptions() :: #{subscription() => #{reference() => pid()}}.
-type update() :: dynamic().
-type version() :: dynamic().
Callbacks
-callback data_diff(Old :: local_data(), New :: local_data()) -> update().
-callback init_global_view(scope(), options()) -> global_view().
-callback init_local_data(options()) -> local_data().
-callback new_subscription(subscription(), global_view()) -> subscribe_result().
-callback stop_global_view(global_view()) -> term().
-callback translate_local_data(MyVersion :: version(), PeerVersion :: version(), local_data()) -> local_data() | {'$plain_message', dynamic()}.
-callback translate_message(dynamic(), global_view()) -> {update, update(), global_view()} | {drop, global_view()} | {update, pid(), update(), global_view()} | {local_data, pid(), version(), local_data(), global_view()} | {discover, pid(), version(), global_view()}.
-callback update_global_view_and_notify(node(), update(), subscriptions(), global_view()) -> global_view().
-callback update_local_data(update(), local_data()) -> local_data().
-callback version() -> version().
Functions
-spec handle_call({update, update()}, gen_server:from(), state()) -> {reply, ok, state()}; ({subscribe, subscription()}, gen_server:from(), state()) -> {reply, {reference(), subscribe_result()}, state()}; ({unsubscribe, reference()}, gen_server:from(), state()) -> {reply, ok, state()}.
-spec handle_info(Message, state()) -> {noreply, state()} when Message :: {discover, pid(), version()} | {local_data, pid(), version(), local_data()} | {update, pid(), update()} | {nodeup, node()} | {nodedown, node()} | {{'DOWN', subscribe}, reference(), process, pid(), dynamic()} | {{'DOWN', peer}, reference(), process, pid(), dynamic()} | dynamic().
-spec subscribe(scope(), subscription()) -> {reference(), subscribe_result()}.