riak / src / riak_claim.erl

%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License.  You may obtain
%% a copy of the License at

%%   http://www.apache.org/licenses/LICENSE-2.0

%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied.  See the License for the
%% specific language governing permissions and limitations
%% under the License.    

%% @doc The default functions used for claiming partition ownership.
%%      Generally, a wants_claim function should return either
%%      {yes, Integer} or 'no' where Integer is the number of
%%       additional partitions wanted by this node.
%%      A choose_claim function should return a riak_ring with
%%      one more partition claimed by this node than in the input ring.

-module(riak_claim).
-export([default_wants_claim/1, default_choose_claim/1,
         never_wants_claim/1]).

-include_lib("eunit/include/eunit.hrl").

%% @spec default_wants_claim(riak_ring()) -> {yes, integer()} | no
%% @doc Want a partition if we currently have less than floor(ringsize/nodes).
default_wants_claim(Ring) ->
    NumPart = riak_ring:num_partitions(Ring),
    NumOwners = length(lists:usort([node() | riak_ring:all_members(Ring)])),
    Mine = length(riak_ring:my_indices(Ring)),
    Want = trunc(NumPart / NumOwners) - Mine,
    case Want > 0 of 
        true -> {yes, Want};
        false -> no
    end.

%% @spec default_choose_claim(riak_ring()) -> riak_ring()
%% @doc Choose a partition at random.
default_choose_claim(Ring) ->
    riak_ring:transfer_node(riak_ring:random_other_index(Ring),
                              node(), Ring).

%% @spec never_wants_claim(riak_ring()) -> no
%% @doc For use by nodes that should not claim any partitions.
never_wants_claim(_) -> no.

wants_claim_test() ->
    riak_ring_manager:start_link(test),
    riak_eventer:start_link(test),
    riak_test_util:setup_mockring1(),
    {ok, Ring} = riak_ring_manager:get_my_ring(),
    ?assertEqual(yes, erlang:element(1,default_wants_claim(Ring))),
    riak_ring_manager:stop(),
    riak_eventer:stop().
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.