<commit_before>%%%-------------------------------------------------------------------
%%% File:      untitled.erl
%%% @author    <NAME> <> []
%%% @copyright 2008 <NAME>
%%% @doc  Membership process keeps track of dynomite node membership.  Maintains a version history.
%%%
%%% @end  
%%%
%%% @since 2008-03-30 by <NAME>
%%%-------------------------------------------------------------------
-module(membership).
-author('<NAME>').

-behaviour(gen_server).
-define(VIRTUALNODES, 100).

%% API
-export([start_link/0, join_ring/1, hash_ring/0, server_for_key/1, server_for_key/2]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).

-record(membership, {hash_ring, member_table}).

-ifdef(TEST).
-include("etest/membership_test.erl").
-endif.

%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% @spec start_link() -> {ok,Pid} | ignore | {error,Error}
%% @doc Starts the server
%% @end 
%%--------------------------------------------------------------------
start_link() ->
  gen_server:start({local, membership}, ?MODULE, [], []).

join_ring(Server) ->
	gen_server:multi_call(membership, {join_ring, Server}).
	
hash_ring() ->
	gen_server:call(membership, hash_ring).
	
server_for_key(Key) ->
  gen_server:call(membership, {server_for_key, Key, 1}).
	
server_for_key(Key, N) ->
	gen_server:call(membership, {server_for_key, Key, N}).

%%====================================================================
%% gen_server callbacks
%%====================================================================

%%--------------------------------------------------------------------
%% @spec init(Args) -> {ok, State} |
%%                         {ok, State, Timeout} |
%%                         ignore               |
%%                         {stop, Reason}
%% @doc Initiates the server
%% @end 
%%--------------------------------------------------------------------
init([]) ->
    State = create_membership_state([]),
    
		{ok, create_membership_state([])}.

%%--------------------------------------------------------------------
%% @spec 
%% handle_call(Request, From, State) -> {reply, Reply, State} |
%%                                      {reply, Reply, State, Timeout} |
%%                                      {noreply, State} |
%%                                      {noreply, State, Timeout} |
%%                                      {stop, Reason, Reply, State} |
%%                                      {stop, Reason, State}
%% @doc Handling call messages
%% @end 
%%--------------------------------------------------------------------

handle_call({join_ring, {Name, Node}}, _From, State) ->
	{Added, NewState} = int_join_ring({Name, Node}, State),
	{reply, Added, NewState};
<commit_msg>membership servers should get the latest state when they come up<commit_after>%%%-------------------------------------------------------------------
%%% File:      untitled.erl
%%% @author    <NAME> <> []
%%% @copyright 2008 <NAME>
%%% @doc  Membership process keeps track of dynomite node membership.  Maintains a version history.
%%%
%%% @end  
%%%
%%% @since 2008-03-30 by <NAME>
%%%-------------------------------------------------------------------
-module(membership).
-author('<NAME>').

-behaviour(gen_server).
-define(VIRTUALNODES, 100).

%% API
-export([start_link/0, join_ring/1, hash_ring/0, server_for_key/1, server_for_key/2]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).

-record(membership, {hash_ring, member_table}).

-ifdef(TEST).
-include("etest/membership_test.erl").
-endif.

%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% @spec start_link() -> {ok,Pid} | ignore | {error,Error}
%% @doc Starts the server
%% @end 
%%--------------------------------------------------------------------
start_link() ->
  gen_server:start({local, membership}, ?MODULE, [], []).

join_ring(Server) ->
	gen_server:multi_call(membership, {join_ring, Server}).
	
hash_ring() ->
	gen_server:call(membership, hash_ring).
	
server_for_key(Key) ->
  gen_server:call(membership, {server_for_key, Key, 1}).
	
server_for_key(Key, N) ->
	gen_server:call(membership, {server_for_key, Key, N}).

%%====================================================================
%% gen_server callbacks
%%====================================================================

%%--------------------------------------------------------------------
%% @spec init(Args) -> {ok, State} |
%%                         {ok, State, Timeout} |
%%                         ignore               |
%%                         {stop, Reason}
%% @doc Initiates the server
%% @end 
%%--------------------------------------------------------------------
init([]) ->
    State = create_membership_state([]),
    {Replies,_BadNodes} = gen_server:multi_call(membership, {merge_rings, State}),
    [{_Node, MergedState}|_] = Replies,
		{ok, MergedState}.

%%--------------------------------------------------------------------
%% @spec 
%% handle_call(Request, From, State) -> {reply, Reply, State} |
%%                                      {reply, Reply, State, Timeout} |
%%                                      {noreply, State} |
%%                                      {noreply, State, Timeout} |
%%                                      {stop, Reason, Reply, State} |
%%                                      {stop, Reason, State}
%% @doc Handling call messages
%% @end 
%%--------------------------------------------------------------------

handle_call({join_ring, {Name, Node}}, _From, State) ->
	{Added, NewState} = int_join_ring({Name, Node}, State),
	{reply, Added, NewState};

{'fraction_non_alphanumeric': 0.3986095017381228, 'fraction_numerical': 0.008938917397781824, 'mean_word_length': 1.6167171935902989, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4859197', 'n_tokens_mistral': 1625, 'n_tokens_neox': 1516, 'n_words': 311}