项目作者: erleans

项目描述 :
Erlang Orleans
高级语言: Erlang
项目地址: git://github.com/erleans/erleans.git
创建时间: 2017-03-04T18:24:41Z
项目社区:https://github.com/erleans/erleans

开源协议:Apache License 2.0

下载


Erleans

Common Testcodecov

Erleans is a framework for building distributed applications in Erlang and Elixir based on Microsoft Orleans.

Requirements

Rebar3 3.24.0 or above or Elixir 1.18+.

Components

Grains

Stateful grains are backed by persistent storage and referenced by a primary key set by the grain. An activation of a grain is a single Erlang process in on an Erlang node (silo) in an Erlang cluster. Activation placement is handled by Erleans and communication is over standard Erlang distribution. If a grain is sent a message and does not have a current activation one is spawned.

Grain state is persisted through a database provider with an always increasing change id or etag. If the change id or etag has been by another activation the activation attempting to save state will stop.

Activations are registered through
global by default.

Stateless Grains

Stateless grains have no restriction on the number of activations and do not persist state to a database.

Stateless grain activations are pooled through gproc.

Reminders (TODO)

Timers that are associated with a grain, meaning if a grain is not active but a reminder for that grain ticks the grain is activated at that time and the reminder is delivered.

Observers (TODO)

Processes can subscribe to grains to receive notifications for grain specific
events. If a grain supports observers a group is created through
pg.

Providers

Interface that must be implemented for any persistent store to be used for grains.

Streams have a provider type as well for providing a pluggable stream layer.

Differences from gen_server

No starting or linking, a grain is activated when it is sent a request if an activation is not currently running.

Grain Placement

  • prefer_local: If an activation does not exist this causes the new activation to be on the same node making the request.
  • random: Picks a random node to create any new activation of a grain.
  • stateless: Stateless grains are always local. If no local activation to the request exists one is created up to a default maximum value.
  • {stateless, Max :: integer()}: Allows for up to Max number of activations for a grain to exist per node. A new activation, up until Max exist on the node, will be created for a request if an existing activation is not currently busy.

Erlang Example

The grain implementation test_grain is found in test/:

  1. -module(test_grain).
  2. -behaviour(erleans_grain).
  3. ...
  4. placement() ->
  5. prefer_local.
  6. provider() ->
  7. in_memory.
  8. deactivated_counter(Ref) ->
  9. erleans_grain:call(Ref, deactivated_counter).
  10. activated_counter(Ref) ->
  11. erleans_grain:call(Ref, activated_counter).
  12. node(Ref) ->
  13. erleans_grain:call(Ref, node).
  14. state(_) ->
  15. #{activated_counter => 0,
  16. deactivated_counter => 0}.
  17. activate(_, State=#{activated_counter := Counter}) ->
  18. {ok, State#{activated_counter => Counter+1}, #{}}.
  1. $ rebar3 as test shell
  2. ...
  3. > Grain1 = erleans:get_grain(test_grain, <<"grain1">>).
  4. > test_grain:activated_counter(Grain1).
  5. {ok, 1}

Elixir Example

  1. defmodule ErleansElixirExample do
  2. use Erleans.Grain,
  3. placement: :prefer_local,
  4. provider: :postgres,
  5. state: %{:counter => 0}
  6. def get(ref) do
  7. :erleans_grain.call(ref, :get)
  8. end
  9. def increment(ref) do
  10. :erleans_grain.cast(ref, :increment)
  11. end
  12. def handle_call(:get, from, state = %{:counter => counter}) do
  13. {:ok, state, [{:reply, from, counter}]}
  14. end
  15. def handle_cast(:increment, state = %{:counter => counter}) do
  16. new_state = %{state | :counter => counter + 1}
  17. {:ok, new_state, [:save_state]}
  18. end
  19. end
  1. $ mix deps.get
  2. $ mix compile
  3. $ iex --sname a@localhost -S mix
  4. iex(a@localhost)1> ref = Erleans.get_grain(ErleansElixirExample, "somename")
  5. ...
  6. iex(a@localhost)2> ErleansElixirExample.get(ref)
  7. 0
  8. iex(a@localhost)3> ErleansElixirExample.increment(ref)
  9. :ok
  10. iex(a@localhost)4> ErleansElixirExample.get(ref)
  11. 1

Contributing

Running Tests

  1. $ epmd -daemon
  2. $ rebar3 ct