Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
angbrav committed Nov 29, 2013
0 parents commit f117224
Show file tree
Hide file tree
Showing 25 changed files with 1,075 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.beam
.eunit
deps/*
apps/mfmn/ebin
*~
dev/*
doc/*
rel/mfmn
89 changes: 89 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
REBAR = $(shell pwd)/rebar

.PHONY: deps rel stagedevrel

all: deps compile

compile:
$(REBAR) compile

deps:
$(REBAR) get-deps

clean:
$(REBAR) clean

distclean: clean devclean relclean
$(REBAR) delete-deps

test:
$(REBAR) skip_deps=true eunit

rel: all
$(REBAR) generate

relclean:
rm -rf rel/mfmn

devrel: dev1 dev2 dev3

###
### Docs
###
docs:
$(REBAR) skip_deps=true doc

##
## Developer targets
##

stage : rel
$(foreach dep,$(wildcard deps/* wildcard apps/*), rm -rf rel/mfmn/lib/$(shell basename $(dep))-* && ln -sf $(abspath $(dep)) rel/mfmn/lib;)


stagedevrel: dev1 dev2 dev3
$(foreach dev,$^,\
$(foreach dep,$(wildcard deps/* wildcard apps/*), rm -rf dev/$(dev)/lib/$(shell basename $(dep))-* && ln -sf $(abspath $(dep)) dev/$(dev)/lib;))

devrel: dev1 dev2 dev3


devclean:
rm -rf dev

dev1 dev2 dev3: all
mkdir -p dev
(cd rel && $(REBAR) generate target_dir=../dev/$@ overlay_vars=vars/$@.config)


##
## Dialyzer
##
APPS = kernel stdlib sasl erts ssl tools os_mon runtime_tools crypto inets \
xmerl webtool snmp public_key mnesia eunit syntax_tools compiler
COMBO_PLT = $(HOME)/.mfmn_combo_dialyzer_plt

check_plt: deps compile
dialyzer --check_plt --plt $(COMBO_PLT) --apps $(APPS) \
deps/*/ebin apps/*/ebin

build_plt: deps compile
dialyzer --build_plt --output_plt $(COMBO_PLT) --apps $(APPS) \
deps/*/ebin apps/*/ebin

dialyzer: deps compile
@echo
@echo Use "'make check_plt'" to check PLT prior to using this target.
@echo Use "'make build_plt'" to build PLT prior to using this target.
@echo
@sleep 1
dialyzer -Wno_return --plt $(COMBO_PLT) deps/*/ebin apps/*/ebin


cleanplt:
@echo
@echo "Are you sure? It takes about 1/2 hour to re-build."
@echo Deleting $(COMBO_PLT) in 5 seconds.
@echo
sleep 5
rm $(COMBO_PLT)
Binary file added apps/mfmn/src/.mfmn_vnode.erl.swp
Binary file not shown.
14 changes: 14 additions & 0 deletions apps/mfmn/src/mfmn.app.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
%% -*- erlang -*-
{application, mfmn,
[
{description, ""},
{vsn, "1"},
{registered, []},
{applications, [
kernel,
stdlib,
riak_core
]},
{mod, { mfmn_app, []}},
{env, []}
]}.
30 changes: 30 additions & 0 deletions apps/mfmn/src/mfmn.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-module(mfmn).
-include("mfmn.hrl").
-include_lib("riak_core/include/riak_core_vnode.hrl").

-export([
ping/0,
put/2,
get/1
]).

%% Public API

%% @doc Pings a random vnode to make sure communication is functional
ping() ->
DocIdx = riak_core_util:chash_key({<<"ping">>, term_to_binary(now())}),
PrefList = riak_core_apl:get_primary_apl(DocIdx, 1, mfmn),
[{IndexNode, _Type}] = PrefList,
riak_core_vnode_master:sync_spawn_command(IndexNode, ping, mfmn_vnode_master).

put(Key, Value)->
DocIdx = riak_core_util:chash_key({<<"key">>, term_to_binary(Key)}),
PrefList = riak_core_apl:get_primary_apl(DocIdx, 1, mfmn),
[{IndexNode, _Type}] = PrefList,
riak_core_vnode_master:sync_spawn_command(IndexNode, {put, Key, Value}, mfmn_vnode_master).

get(Key)->
DocIdx = riak_core_util:chash_key({<<"key">>, term_to_binary(Key)}),
PrefList = riak_core_apl:get_primary_apl(DocIdx, 1, mfmn),
[{IndexNode, _Type}] = PrefList,
riak_core_vnode_master:sync_spawn_command(IndexNode, {get, Key}, mfmn_vnode_master).
1 change: 1 addition & 0 deletions apps/mfmn/src/mfmn.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-define(PRINT(Var), io:format("DEBUG: ~p:~p - ~p~n~n ~p~n~n", [?MODULE, ?LINE, ??Var, Var])).
25 changes: 25 additions & 0 deletions apps/mfmn/src/mfmn_app.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-module(mfmn_app).

-behaviour(application).

%% Application callbacks
-export([start/2, stop/1]).

%% ===================================================================
%% Application callbacks
%% ===================================================================

start(_StartType, _StartArgs) ->
case mfmn_sup:start_link() of
{ok, Pid} ->
ok = riak_core:register([{vnode_module, mfmn_vnode}]),
ok = riak_core_ring_events:add_guarded_handler(mfmn_ring_event_handler, []),
ok = riak_core_node_watcher_events:add_guarded_handler(mfmn_node_event_handler, []),
ok = riak_core_node_watcher:service_up(mfmn, self()),
{ok, Pid};
{error, Reason} ->
{error, Reason}
end.

stop(_State) ->
ok.
70 changes: 70 additions & 0 deletions apps/mfmn/src/mfmn_console.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
%% @doc Interface for mfmn-admin commands.
-module(mfmn_console).
-export([join/1,
leave/1,
remove/1,
ringready/1]).

join([NodeStr]) ->
try riak_core:join(NodeStr) of
ok ->
io:format("Sent join request to ~s\n", [NodeStr]),
ok;
{error, not_reachable} ->
io:format("Node ~s is not reachable!\n", [NodeStr]),
error;
{error, different_ring_sizes} ->
io:format("Failed: ~s has a different ring_creation_size~n",
[NodeStr]),
error
catch
Exception:Reason ->
lager:error("Join failed ~p:~p", [Exception, Reason]),
io:format("Join failed, see log for details~n"),
error
end.

leave([]) ->
remove_node(node()).

remove([Node]) ->
remove_node(list_to_atom(Node)).

remove_node(Node) when is_atom(Node) ->
try catch(riak_core:remove_from_cluster(Node)) of
{'EXIT', {badarg, [{erlang, hd, [[]]}|_]}} ->
%% This is a workaround because
%% riak_core_gossip:remove_from_cluster doesn't check if
%% the result of subtracting the current node from the
%% cluster member list results in the empty list. When
%% that code gets refactored this can probably go away.
io:format("Leave failed, this node is the only member.~n"),
error;
Res ->
io:format(" ~p\n", [Res])
catch
Exception:Reason ->
lager:error("Leave failed ~p:~p", [Exception, Reason]),
io:format("Leave failed, see log for details~n"),
error
end.

-spec(ringready([]) -> ok | error).
ringready([]) ->
try riak_core_status:ringready() of
{ok, Nodes} ->
io:format("TRUE All nodes agree on the ring ~p\n", [Nodes]);
{error, {different_owners, N1, N2}} ->
io:format("FALSE Node ~p and ~p list different partition owners\n",
[N1, N2]),
error;
{error, {nodes_down, Down}} ->
io:format("FALSE ~p down. All nodes need to be up to check.\n",
[Down]),
error
catch
Exception:Reason ->
lager:error("Ringready failed ~p:~p", [Exception, Reason]),
io:format("Ringready failed, see log for details~n"),
error
end.
42 changes: 42 additions & 0 deletions apps/mfmn/src/mfmn_node_event_handler.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
%% 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.

%% Copyright (c) 2007-2011 Basho Technologies, Inc. All Rights Reserved.

-module(mfmn_node_event_handler).
-behaviour(gen_event).

%% gen_event callbacks
-export([init/1, handle_event/2, handle_call/2,
handle_info/2, terminate/2, code_change/3]).
-record(state, {}).

init([]) ->
{ok, #state{}}.

handle_event({service_update, _Services}, State) ->
{ok, State}.

handle_call(_Event, State) ->
{ok, ok, State}.

handle_info(_Info, State) ->
{ok, State}.

terminate(_Reason, _State) ->
ok.

code_change(_OldVsn, State, _Extra) ->
{ok, State}.

42 changes: 42 additions & 0 deletions apps/mfmn/src/mfmn_ring_event_handler.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
%% 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.

%% Copyright (c) 2007-2011 Basho Technologies, Inc. All Rights Reserved.

-module(mfmn_ring_event_handler).
-behaviour(gen_event).

%% gen_event callbacks
-export([init/1, handle_event/2, handle_call/2,
handle_info/2, terminate/2, code_change/3]).
-record(state, {}).

init([]) ->
{ok, #state{}}.

handle_event({ring_update, _Ring}, State) ->
{ok, State}.

handle_call(_Event, State) ->
{ok, ok, State}.

handle_info(_Info, State) ->
{ok, State}.

terminate(_Reason, _State) ->
ok.

code_change(_OldVsn, State, _Extra) ->
{ok, State}.

29 changes: 29 additions & 0 deletions apps/mfmn/src/mfmn_sup.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-module(mfmn_sup).

-behaviour(supervisor).

%% API
-export([start_link/0]).

%% Supervisor callbacks
-export([init/1]).

%% ===================================================================
%% API functions
%% ===================================================================

start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).

%% ===================================================================
%% Supervisor callbacks
%% ===================================================================

init(_Args) ->
VMaster = { mfmn_vnode_master,
{riak_core_vnode_master, start_link, [mfmn_vnode]},
permanent, 5000, worker, [riak_core_vnode_master]},

{ ok,
{ {one_for_one, 5, 10},
[VMaster]}}.
Loading

0 comments on commit f117224

Please sign in to comment.