Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gen_fsm replaced by gen_statem in vnode #80

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge riak_core_lite master into local branch
  • Loading branch information
woelki committed Oct 12, 2020
commit 020090debe79db481c6b9f6d9e2ede74ed85e939
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ dialyzer:
${REBAR} dialyzer

lint:
${REBAR} as lint lint
${REBAR} lint
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,11 @@ something Core related
* If you've found a bug in riak_core_lite,
[file](https://github.com/riak-core-lite/riak_core_lite/issues) a clear, concise,
explanatory issue against this repo.

## Reference Implementation

For some reference on how `riak_core_lite` can be used, you can read about projects which are using `riak_core_lite` as a library:

- [rcl_memkv](https://github.com/albsch/rcl_memkv): A minimalistic in-memory key-value store to understand how to implement the handoff behavior properly
- [rclref](https://github.com/wattlebirdaz/rclref): A reference implementation of a distributed key-value store using riak_core_lite featuring quorum reads and writes.
- [AntidoteDB](https://github.com/AntidoteDB/antidote): A a highly available geo-replicated key-value database which uses riak_core_lite for sharding of data centers.
11 changes: 3 additions & 8 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{xref_checks, [ undefined_function_calls, locals_not_used, deprecated_function_calls ]}.

%% Code formatter
{plugins, [rebar3_format, rebar3_proper]}.
{project_plugins, [rebar3_format, rebar3_lint, rebar3_proper]}.
{format, [ {formatter, otp_formatter} ]}.

%%-------------------------------------------------------------------
Expand All @@ -21,17 +21,12 @@
{plugins, [{coveralls, {git, "https://github.com/markusn/coveralls-erl", {branch, "master"}}}]},
{deps, [meck]}
]},
{docs, [{deps, [{edown, "0.7.0"}]}]},
{proper, [
{erl_opts, [nowarn_export_all,{d, 'PROPER'}, {d, 'TEST'}]},
{erl_opts, [nowarn_export_all, {d, 'PROPER'}, {d, 'TEST'}]},
{plugins, [{coveralls, {git, "https://github.com/markusn/coveralls-erl", {branch, "master"}}}]},
{deps, [meck, {proper, "1.3.0"}, recon]}
]},
{lint, [
{plugins, [
{rebar3_lint, {git, "https://github.com/project-fifo/rebar3_lint.git", {tag, "0.1.11"}}}
]}
]}
{docs, [{deps, [{edown, "0.7.0"}]}]}
]}.

{cover_enabled, true}.
Expand Down
112 changes: 42 additions & 70 deletions src/chash.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,15 @@

-module(chash).

-export([contains_name/2,
fresh/2,
lookup/2,
key_of/1,
members/1,
merge_rings/2,
next_index/2,
nodes/1,
predecessors/2,
predecessors/3,
ring_increment/1,
size/1,
successors/2,
successors/3,
update/3]).
-export([contains_name/2, fresh/2, lookup/2, key_of/1,
members/1, merge_rings/2, next_index/2, nodes/1,
predecessors/2, predecessors/3, ring_increment/1,
size/1, successors/2, successors/3, update/3]).

-export_type([chash/0, index/0, index_as_int/0]).

-define(RINGTOP,
trunc(math:pow(2, 160) - 1)). % SHA-1 space
trunc(math:pow(2, 160) - 1)). % SHA-1 space

-ifdef(TEST).

Expand Down Expand Up @@ -86,7 +75,7 @@

%% @doc Return true if named Node owns any partitions in the ring, else false.
-spec contains_name(Name :: chash_node(),
CHash :: chash()) -> boolean().
CHash :: chash()) -> boolean().

contains_name(Name, CHash) ->
{_NumPartitions, Nodes} = CHash,
Expand All @@ -97,7 +86,7 @@ contains_name(Name, CHash) ->
%% is not much larger than the intended eventual number of
%% participating nodes, then performance will suffer.
-spec fresh(NumPartitions :: num_partitions(),
SeedNode :: chash_node()) -> chash().
SeedNode :: chash_node()) -> chash().

fresh(NumPartitions, SeedNode) ->
Inc = ring_increment(NumPartitions),
Expand All @@ -107,7 +96,7 @@ fresh(NumPartitions, SeedNode) ->

%% @doc Find the Node that owns the partition identified by IndexAsInt.
-spec lookup(IndexAsInt :: index_as_int(),
CHash :: chash()) -> chash_node().
CHash :: chash()) -> chash_node().

lookup(IndexAsInt, CHash) ->
{_NumPartitions, Nodes} = CHash,
Expand All @@ -134,7 +123,7 @@ members(CHash) ->
%% If multiple nodes are actively claiming nodes in the same
%% time period, churn will occur. Be prepared to live with it.
-spec merge_rings(CHashA :: chash(),
CHashB :: chash()) -> chash().
CHashB :: chash()) -> chash().

merge_rings(CHashA, CHashB) ->
{NumPartitions, NodesA} = CHashA,
Expand All @@ -146,7 +135,7 @@ merge_rings(CHashA, CHashB) ->
%% @doc Given the integer representation of a chash key,
%% return the next ring index integer value.
-spec next_index(IntegerKey :: integer(),
CHash :: chash()) -> index_as_int().
CHash :: chash()) -> index_as_int().

next_index(IntegerKey, {NumPartitions, _}) ->
Inc = ring_increment(NumPartitions),
Expand All @@ -155,13 +144,11 @@ next_index(IntegerKey, {NumPartitions, _}) ->
%% @doc Return the entire set of NodeEntries in the ring.
-spec nodes(CHash :: chash()) -> [node_entry()].

nodes(CHash) ->
{_NumPartitions, Nodes} = CHash,
Nodes.
nodes(CHash) -> {_NumPartitions, Nodes} = CHash, Nodes.

%% @doc Given an object key, return all NodeEntries in order starting at Index.
-spec ordered_from(Index :: index(),
CHash :: chash()) -> [node_entry()].
CHash :: chash()) -> [node_entry()].

ordered_from(Index, {NumPartitions, Nodes}) ->
<<IndexAsInt:160/integer>> = Index,
Expand All @@ -172,7 +159,7 @@ ordered_from(Index, {NumPartitions, Nodes}) ->
%% @doc Given an object key, return all NodeEntries in reverse order
%% starting at Index.
-spec predecessors(Index :: index() | index_as_int(),
CHash :: chash()) -> [node_entry()].
CHash :: chash()) -> [node_entry()].

predecessors(Index, CHash) ->
{NumPartitions, _Nodes} = CHash,
Expand All @@ -181,20 +168,20 @@ predecessors(Index, CHash) ->
%% @doc Given an object key, return the next N NodeEntries in reverse order
%% starting at Index.
-spec predecessors(Index :: index() | index_as_int(),
CHash :: chash(), N :: integer()) -> [node_entry()].
CHash :: chash(), N :: integer()) -> [node_entry()].

predecessors(Index, CHash, N) when is_integer(Index) ->
predecessors(<<Index:160/integer>>, CHash, N);
predecessors(Index, CHash, N) ->
Num = max_n(N, CHash),
{Res, _} = lists:split(Num,
lists:reverse(ordered_from(Index, CHash))),
lists:reverse(ordered_from(Index, CHash))),
Res.

%% @doc Return increment between ring indexes given
%% the number of ring partitions.
-spec ring_increment(NumPartitions ::
pos_integer()) -> pos_integer().
pos_integer()) -> pos_integer().

ring_increment(NumPartitions) ->
(?RINGTOP) div NumPartitions.
Expand All @@ -203,12 +190,11 @@ ring_increment(NumPartitions) ->
-spec size(CHash :: chash()) -> integer().

size(CHash) ->
{_NumPartitions, Nodes} = CHash,
length(Nodes).
{_NumPartitions, Nodes} = CHash, length(Nodes).

%% @doc Given an object key, return all NodeEntries in order starting at Index.
-spec successors(Index :: index(),
CHash :: chash()) -> [node_entry()].
CHash :: chash()) -> [node_entry()].

successors(Index, CHash) ->
{NumPartitions, _Nodes} = CHash,
Expand All @@ -217,28 +203,24 @@ successors(Index, CHash) ->
%% @doc Given an object key, return the next N NodeEntries in order
%% starting at Index.
-spec successors(Index :: index(), CHash :: chash(),
N :: integer()) -> [node_entry()].
N :: integer()) -> [node_entry()].

successors(Index, CHash, N) ->
Num = max_n(N, CHash),
Ordered = ordered_from(Index, CHash),
{NumPartitions, _Nodes} = CHash,
if Num =:= NumPartitions -> Ordered;
true ->
{Res, _} = lists:split(Num, Ordered),
Res
true -> {Res, _} = lists:split(Num, Ordered), Res
end.

%% @doc Make the partition beginning at IndexAsInt owned by Name'd node.
-spec update(IndexAsInt :: index_as_int(),
Name :: chash_node(), CHash :: chash()) -> chash().
Name :: chash_node(), CHash :: chash()) -> chash().

update(IndexAsInt, Name, CHash) ->
{NumPartitions, Nodes} = CHash,
NewNodes = lists:keyreplace(IndexAsInt,
1,
Nodes,
{IndexAsInt, Name}),
NewNodes = lists:keyreplace(IndexAsInt, 1, Nodes,
{IndexAsInt, Name}),
{NumPartitions, NewNodes}.

%% ====================================================================
Expand All @@ -249,14 +231,14 @@ update(IndexAsInt, Name, CHash) ->
%% @doc Return either N or the number of partitions in the ring, whichever
%% is lesser.
-spec max_n(N :: integer(),
CHash :: chash()) -> integer().
CHash :: chash()) -> integer().

max_n(N, {NumPartitions, _Nodes}) ->
erlang:min(N, NumPartitions).

%% @private
-spec random_node(NodeA :: chash_node(),
NodeB :: chash_node()) -> chash_node().
NodeB :: chash_node()) -> chash_node().

random_node(NodeA, NodeA) -> NodeA;
random_node(NodeA, NodeB) ->
Expand All @@ -273,34 +255,25 @@ update_test() ->
% Create a fresh ring...
CHash = chash:fresh(5, Node),
GetNthIndex = fun (N, {_, Nodes}) ->
{Index, _} = lists:nth(N, Nodes),
Index
end,
{Index, _} = lists:nth(N, Nodes), Index
end,
% Test update...
FirstIndex = GetNthIndex(1, CHash),
ThirdIndex = GetNthIndex(3, CHash),
{5,
[{_, NewNode},
{_, Node},
{_, Node},
{_, Node},
{_, Node},
{_, Node}]} =
update(FirstIndex, NewNode, CHash),
[{_, NewNode}, {_, Node}, {_, Node}, {_, Node},
{_, Node}, {_, Node}]} =
update(FirstIndex, NewNode, CHash),
{5,
[{_, Node},
{_, Node},
{_, NewNode},
{_, Node},
{_, Node},
{_, Node}]} =
update(ThirdIndex, NewNode, CHash).
[{_, Node}, {_, Node}, {_, NewNode}, {_, Node},
{_, Node}, {_, Node}]} =
update(ThirdIndex, NewNode, CHash).

contains_test() ->
CHash = chash:fresh(8, the_node),
?assertEqual(true, (contains_name(the_node, CHash))),
?assertEqual(false,
(contains_name(some_other_node, CHash))).
(contains_name(some_other_node, CHash))).

max_n_test() ->
CHash = chash:fresh(8, the_node),
Expand All @@ -309,27 +282,26 @@ max_n_test() ->

simple_size_test() ->
?assertEqual(8,
(length(chash:nodes(chash:fresh(8, the_node))))).
(length(chash:nodes(chash:fresh(8, the_node))))).

successors_length_test() ->
?assertEqual(8,
(length(chash:successors(chash:key_of(0),
chash:fresh(8, the_node))))).
(length(chash:successors(chash:key_of(0),
chash:fresh(8, the_node))))).

inverse_pred_test() ->
CHash = chash:fresh(8, the_node),
S = [I
|| {I, _} <- chash:successors(chash:key_of(4), CHash)],
|| {I, _} <- chash:successors(chash:key_of(4), CHash)],
P = [I
|| {I, _}
<- chash:predecessors(chash:key_of(4), CHash)],
|| {I, _}
<- chash:predecessors(chash:key_of(4), CHash)],
?assertEqual(S, (lists:reverse(P))).

merge_test() ->
CHashA = chash:fresh(8, node_one),
CHashB = chash:update(0,
node_one,
chash:fresh(8, node_two)),
CHashB = chash:update(0, node_one,
chash:fresh(8, node_two)),
CHash = chash:merge_rings(CHashA, CHashB),
?assertEqual(node_one, (chash:lookup(0, CHash))).

Expand Down
Loading