Skip to content

Commit f117224

Browse files
committed
initial commit
0 parents  commit f117224

25 files changed

+1075
-0
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.beam
2+
.eunit
3+
deps/*
4+
apps/mfmn/ebin
5+
*~
6+
dev/*
7+
doc/*
8+
rel/mfmn

Makefile

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
REBAR = $(shell pwd)/rebar
2+
3+
.PHONY: deps rel stagedevrel
4+
5+
all: deps compile
6+
7+
compile:
8+
$(REBAR) compile
9+
10+
deps:
11+
$(REBAR) get-deps
12+
13+
clean:
14+
$(REBAR) clean
15+
16+
distclean: clean devclean relclean
17+
$(REBAR) delete-deps
18+
19+
test:
20+
$(REBAR) skip_deps=true eunit
21+
22+
rel: all
23+
$(REBAR) generate
24+
25+
relclean:
26+
rm -rf rel/mfmn
27+
28+
devrel: dev1 dev2 dev3
29+
30+
###
31+
### Docs
32+
###
33+
docs:
34+
$(REBAR) skip_deps=true doc
35+
36+
##
37+
## Developer targets
38+
##
39+
40+
stage : rel
41+
$(foreach dep,$(wildcard deps/* wildcard apps/*), rm -rf rel/mfmn/lib/$(shell basename $(dep))-* && ln -sf $(abspath $(dep)) rel/mfmn/lib;)
42+
43+
44+
stagedevrel: dev1 dev2 dev3
45+
$(foreach dev,$^,\
46+
$(foreach dep,$(wildcard deps/* wildcard apps/*), rm -rf dev/$(dev)/lib/$(shell basename $(dep))-* && ln -sf $(abspath $(dep)) dev/$(dev)/lib;))
47+
48+
devrel: dev1 dev2 dev3
49+
50+
51+
devclean:
52+
rm -rf dev
53+
54+
dev1 dev2 dev3: all
55+
mkdir -p dev
56+
(cd rel && $(REBAR) generate target_dir=../dev/$@ overlay_vars=vars/$@.config)
57+
58+
59+
##
60+
## Dialyzer
61+
##
62+
APPS = kernel stdlib sasl erts ssl tools os_mon runtime_tools crypto inets \
63+
xmerl webtool snmp public_key mnesia eunit syntax_tools compiler
64+
COMBO_PLT = $(HOME)/.mfmn_combo_dialyzer_plt
65+
66+
check_plt: deps compile
67+
dialyzer --check_plt --plt $(COMBO_PLT) --apps $(APPS) \
68+
deps/*/ebin apps/*/ebin
69+
70+
build_plt: deps compile
71+
dialyzer --build_plt --output_plt $(COMBO_PLT) --apps $(APPS) \
72+
deps/*/ebin apps/*/ebin
73+
74+
dialyzer: deps compile
75+
@echo
76+
@echo Use "'make check_plt'" to check PLT prior to using this target.
77+
@echo Use "'make build_plt'" to build PLT prior to using this target.
78+
@echo
79+
@sleep 1
80+
dialyzer -Wno_return --plt $(COMBO_PLT) deps/*/ebin apps/*/ebin
81+
82+
83+
cleanplt:
84+
@echo
85+
@echo "Are you sure? It takes about 1/2 hour to re-build."
86+
@echo Deleting $(COMBO_PLT) in 5 seconds.
87+
@echo
88+
sleep 5
89+
rm $(COMBO_PLT)

apps/mfmn/src/.mfmn_vnode.erl.swp

12 KB
Binary file not shown.

apps/mfmn/src/mfmn.app.src

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
%% -*- erlang -*-
2+
{application, mfmn,
3+
[
4+
{description, ""},
5+
{vsn, "1"},
6+
{registered, []},
7+
{applications, [
8+
kernel,
9+
stdlib,
10+
riak_core
11+
]},
12+
{mod, { mfmn_app, []}},
13+
{env, []}
14+
]}.

apps/mfmn/src/mfmn.erl

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
-module(mfmn).
2+
-include("mfmn.hrl").
3+
-include_lib("riak_core/include/riak_core_vnode.hrl").
4+
5+
-export([
6+
ping/0,
7+
put/2,
8+
get/1
9+
]).
10+
11+
%% Public API
12+
13+
%% @doc Pings a random vnode to make sure communication is functional
14+
ping() ->
15+
DocIdx = riak_core_util:chash_key({<<"ping">>, term_to_binary(now())}),
16+
PrefList = riak_core_apl:get_primary_apl(DocIdx, 1, mfmn),
17+
[{IndexNode, _Type}] = PrefList,
18+
riak_core_vnode_master:sync_spawn_command(IndexNode, ping, mfmn_vnode_master).
19+
20+
put(Key, Value)->
21+
DocIdx = riak_core_util:chash_key({<<"key">>, term_to_binary(Key)}),
22+
PrefList = riak_core_apl:get_primary_apl(DocIdx, 1, mfmn),
23+
[{IndexNode, _Type}] = PrefList,
24+
riak_core_vnode_master:sync_spawn_command(IndexNode, {put, Key, Value}, mfmn_vnode_master).
25+
26+
get(Key)->
27+
DocIdx = riak_core_util:chash_key({<<"key">>, term_to_binary(Key)}),
28+
PrefList = riak_core_apl:get_primary_apl(DocIdx, 1, mfmn),
29+
[{IndexNode, _Type}] = PrefList,
30+
riak_core_vnode_master:sync_spawn_command(IndexNode, {get, Key}, mfmn_vnode_master).

apps/mfmn/src/mfmn.hrl

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-define(PRINT(Var), io:format("DEBUG: ~p:~p - ~p~n~n ~p~n~n", [?MODULE, ?LINE, ??Var, Var])).

apps/mfmn/src/mfmn_app.erl

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
-module(mfmn_app).
2+
3+
-behaviour(application).
4+
5+
%% Application callbacks
6+
-export([start/2, stop/1]).
7+
8+
%% ===================================================================
9+
%% Application callbacks
10+
%% ===================================================================
11+
12+
start(_StartType, _StartArgs) ->
13+
case mfmn_sup:start_link() of
14+
{ok, Pid} ->
15+
ok = riak_core:register([{vnode_module, mfmn_vnode}]),
16+
ok = riak_core_ring_events:add_guarded_handler(mfmn_ring_event_handler, []),
17+
ok = riak_core_node_watcher_events:add_guarded_handler(mfmn_node_event_handler, []),
18+
ok = riak_core_node_watcher:service_up(mfmn, self()),
19+
{ok, Pid};
20+
{error, Reason} ->
21+
{error, Reason}
22+
end.
23+
24+
stop(_State) ->
25+
ok.

apps/mfmn/src/mfmn_console.erl

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
%% @doc Interface for mfmn-admin commands.
2+
-module(mfmn_console).
3+
-export([join/1,
4+
leave/1,
5+
remove/1,
6+
ringready/1]).
7+
8+
join([NodeStr]) ->
9+
try riak_core:join(NodeStr) of
10+
ok ->
11+
io:format("Sent join request to ~s\n", [NodeStr]),
12+
ok;
13+
{error, not_reachable} ->
14+
io:format("Node ~s is not reachable!\n", [NodeStr]),
15+
error;
16+
{error, different_ring_sizes} ->
17+
io:format("Failed: ~s has a different ring_creation_size~n",
18+
[NodeStr]),
19+
error
20+
catch
21+
Exception:Reason ->
22+
lager:error("Join failed ~p:~p", [Exception, Reason]),
23+
io:format("Join failed, see log for details~n"),
24+
error
25+
end.
26+
27+
leave([]) ->
28+
remove_node(node()).
29+
30+
remove([Node]) ->
31+
remove_node(list_to_atom(Node)).
32+
33+
remove_node(Node) when is_atom(Node) ->
34+
try catch(riak_core:remove_from_cluster(Node)) of
35+
{'EXIT', {badarg, [{erlang, hd, [[]]}|_]}} ->
36+
%% This is a workaround because
37+
%% riak_core_gossip:remove_from_cluster doesn't check if
38+
%% the result of subtracting the current node from the
39+
%% cluster member list results in the empty list. When
40+
%% that code gets refactored this can probably go away.
41+
io:format("Leave failed, this node is the only member.~n"),
42+
error;
43+
Res ->
44+
io:format(" ~p\n", [Res])
45+
catch
46+
Exception:Reason ->
47+
lager:error("Leave failed ~p:~p", [Exception, Reason]),
48+
io:format("Leave failed, see log for details~n"),
49+
error
50+
end.
51+
52+
-spec(ringready([]) -> ok | error).
53+
ringready([]) ->
54+
try riak_core_status:ringready() of
55+
{ok, Nodes} ->
56+
io:format("TRUE All nodes agree on the ring ~p\n", [Nodes]);
57+
{error, {different_owners, N1, N2}} ->
58+
io:format("FALSE Node ~p and ~p list different partition owners\n",
59+
[N1, N2]),
60+
error;
61+
{error, {nodes_down, Down}} ->
62+
io:format("FALSE ~p down. All nodes need to be up to check.\n",
63+
[Down]),
64+
error
65+
catch
66+
Exception:Reason ->
67+
lager:error("Ringready failed ~p:~p", [Exception, Reason]),
68+
io:format("Ringready failed, see log for details~n"),
69+
error
70+
end.
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
%% This file is provided to you under the Apache License,
2+
%% Version 2.0 (the "License"); you may not use this file
3+
%% except in compliance with the License. You may obtain
4+
%% a copy of the License at
5+
%%
6+
%% http://www.apache.org/licenses/LICENSE-2.0
7+
%%
8+
%% Unless required by applicable law or agreed to in writing,
9+
%% software distributed under the License is distributed on an
10+
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11+
%% KIND, either express or implied. See the License for the
12+
%% specific language governing permissions and limitations
13+
%% under the License.
14+
15+
%% Copyright (c) 2007-2011 Basho Technologies, Inc. All Rights Reserved.
16+
17+
-module(mfmn_node_event_handler).
18+
-behaviour(gen_event).
19+
20+
%% gen_event callbacks
21+
-export([init/1, handle_event/2, handle_call/2,
22+
handle_info/2, terminate/2, code_change/3]).
23+
-record(state, {}).
24+
25+
init([]) ->
26+
{ok, #state{}}.
27+
28+
handle_event({service_update, _Services}, State) ->
29+
{ok, State}.
30+
31+
handle_call(_Event, State) ->
32+
{ok, ok, State}.
33+
34+
handle_info(_Info, State) ->
35+
{ok, State}.
36+
37+
terminate(_Reason, _State) ->
38+
ok.
39+
40+
code_change(_OldVsn, State, _Extra) ->
41+
{ok, State}.
42+
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
%% This file is provided to you under the Apache License,
2+
%% Version 2.0 (the "License"); you may not use this file
3+
%% except in compliance with the License. You may obtain
4+
%% a copy of the License at
5+
%%
6+
%% http://www.apache.org/licenses/LICENSE-2.0
7+
%%
8+
%% Unless required by applicable law or agreed to in writing,
9+
%% software distributed under the License is distributed on an
10+
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11+
%% KIND, either express or implied. See the License for the
12+
%% specific language governing permissions and limitations
13+
%% under the License.
14+
15+
%% Copyright (c) 2007-2011 Basho Technologies, Inc. All Rights Reserved.
16+
17+
-module(mfmn_ring_event_handler).
18+
-behaviour(gen_event).
19+
20+
%% gen_event callbacks
21+
-export([init/1, handle_event/2, handle_call/2,
22+
handle_info/2, terminate/2, code_change/3]).
23+
-record(state, {}).
24+
25+
init([]) ->
26+
{ok, #state{}}.
27+
28+
handle_event({ring_update, _Ring}, State) ->
29+
{ok, State}.
30+
31+
handle_call(_Event, State) ->
32+
{ok, ok, State}.
33+
34+
handle_info(_Info, State) ->
35+
{ok, State}.
36+
37+
terminate(_Reason, _State) ->
38+
ok.
39+
40+
code_change(_OldVsn, State, _Extra) ->
41+
{ok, State}.
42+

apps/mfmn/src/mfmn_sup.erl

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
-module(mfmn_sup).
2+
3+
-behaviour(supervisor).
4+
5+
%% API
6+
-export([start_link/0]).
7+
8+
%% Supervisor callbacks
9+
-export([init/1]).
10+
11+
%% ===================================================================
12+
%% API functions
13+
%% ===================================================================
14+
15+
start_link() ->
16+
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
17+
18+
%% ===================================================================
19+
%% Supervisor callbacks
20+
%% ===================================================================
21+
22+
init(_Args) ->
23+
VMaster = { mfmn_vnode_master,
24+
{riak_core_vnode_master, start_link, [mfmn_vnode]},
25+
permanent, 5000, worker, [riak_core_vnode_master]},
26+
27+
{ ok,
28+
{ {one_for_one, 5, 10},
29+
[VMaster]}}.

0 commit comments

Comments
 (0)