-
Notifications
You must be signed in to change notification settings - Fork 392
/
riak_core.erl
329 lines (304 loc) · 11.5 KB
/
riak_core.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
%% -------------------------------------------------------------------
%%
%% Riak: A lightweight, decentralized key-value store.
%%
%% Copyright (c) 2007-2011 Basho Technologies, Inc. All Rights Reserved.
%%
%% 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.
%%
%% -------------------------------------------------------------------
-module(riak_core).
-export([stop/0, stop/1, join/1, join/3, remove/1, down/1, leave/0,
remove_from_cluster/1]).
-export([register_vnode_module/1, vnode_modules/0]).
-export([register/1, bucket_fixups/0]).
-export([add_guarded_event_handler/3, add_guarded_event_handler/4]).
-export([delete_guarded_event_handler/3]).
%% @spec stop() -> ok
%% @doc Stop the riak application and the calling process.
stop() -> stop("riak stop requested").
-ifdef(TEST).
stop(Reason) ->
lager:notice("~p", [Reason]),
% if we're in test mode, we don't want to halt the node, so instead
% we just stop the application.
application:stop(riak_core).
-else.
stop(Reason) ->
% we never do an application:stop because that makes it very hard
% to really halt the runtime, which is what we need here.
lager:notice("~p", [Reason]),
init:stop().
-endif.
%%
%% @doc Join the ring found on the specified remote node
%%
join(NodeStr) when is_list(NodeStr) ->
join(riak_core_util:str_to_node(NodeStr));
join(Node) when is_atom(Node) ->
join(node(), Node).
join(Node, Node) ->
{error, self_join};
join(_, Node) ->
join(riak_core_gossip:legacy_gossip(), node(), Node).
join(true, _, Node) ->
legacy_join(Node);
join(false, _, Node) ->
case net_adm:ping(Node) of
pang ->
{error, not_reachable};
pong ->
case rpc:call(Node, riak_core_gossip, legacy_gossip, []) of
true ->
legacy_join(Node);
_ ->
%% Failure due to trying to join older node that
%% doesn't define legacy_gossip will be handled
%% in standard_join based on seeing a legacy ring.
standard_join(Node)
end
end.
standard_join(Node) when is_atom(Node) ->
case net_adm:ping(Node) of
pong ->
case rpc:call(Node, riak_core_ring_manager, get_my_ring, []) of
{ok, Ring} ->
case riak_core_ring:legacy_ring(Ring) of
true ->
legacy_join(Node);
false ->
standard_join(Node, Ring)
end;
_ ->
{error, unable_to_get_join_ring}
end;
pang ->
{error, not_reachable}
end.
standard_join(Node, Ring) ->
{ok, MyRing} = riak_core_ring_manager:get_my_ring(),
SameSize = (riak_core_ring:num_partitions(MyRing) =:=
riak_core_ring:num_partitions(Ring)),
Singleton = ([node()] =:= riak_core_ring:all_members(MyRing)),
case {Singleton, SameSize} of
{false, _} ->
{error, not_single_node};
{_, false} ->
{error, different_ring_sizes};
_ ->
GossipVsn = riak_core_gossip:gossip_version(),
Ring2 = riak_core_ring:add_member(node(), Ring,
node()),
Ring3 = riak_core_ring:set_owner(Ring2, node()),
Ring4 =
riak_core_ring:update_member_meta(node(),
Ring3,
node(),
gossip_vsn,
GossipVsn),
riak_core_ring_manager:set_my_ring(Ring4),
riak_core_gossip:send_ring(Node, node())
end.
legacy_join(Node) when is_atom(Node) ->
{ok, OurRingSize} = application:get_env(riak_core, ring_creation_size),
case net_adm:ping(Node) of
pong ->
case rpc:call(Node,
application,
get_env,
[riak_core, ring_creation_size]) of
{ok, OurRingSize} ->
riak_core_gossip:send_ring(Node, node());
_ ->
{error, different_ring_sizes}
end;
pang ->
{error, not_reachable}
end.
remove(Node) ->
{ok, Ring} = riak_core_ring_manager:get_my_ring(),
case {riak_core_ring:all_members(Ring),
riak_core_ring:member_status(Ring, Node)} of
{_, invalid} ->
{error, not_member};
{[Node], _} ->
{error, only_member};
_ ->
case riak_core_gossip:legacy_gossip() of
true ->
legacy_remove(Node);
false ->
standard_remove(Node)
end
end.
standard_remove(Node) ->
riak_core_ring_manager:ring_trans(
fun(Ring2, _) ->
Ring3 = riak_core_ring:remove_member(node(), Ring2, Node),
Ring4 = riak_core_ring:ring_changed(node(), Ring3),
{new_ring, Ring4}
end, []),
ok.
down(Node) ->
down(riak_core_gossip:legacy_gossip(), Node).
down(true, _) ->
{error, legacy_mode};
down(false, Node) ->
{ok, Ring} = riak_core_ring_manager:get_my_ring(),
case net_adm:ping(Node) of
pong ->
{error, is_up};
pang ->
case {riak_core_ring:all_members(Ring),
riak_core_ring:member_status(Ring, Node)} of
{_, invalid} ->
{error, not_member};
{[Node], _} ->
{error, only_member};
_ ->
riak_core_ring_manager:ring_trans(
fun(Ring2, _) ->
Ring3 = riak_core_ring:down_member(node(), Ring2, Node),
Ring4 = riak_core_ring:ring_changed(node(), Ring3),
{new_ring, Ring4}
end, []),
ok
end
end.
leave() ->
Node = node(),
{ok, Ring} = riak_core_ring_manager:get_my_ring(),
case {riak_core_ring:all_members(Ring),
riak_core_ring:member_status(Ring, Node)} of
{_, invalid} ->
{error, not_member};
{[Node], _} ->
{error, only_member};
{_, valid} ->
case riak_core_gossip:legacy_gossip() of
true ->
legacy_remove(Node);
false ->
standard_leave(Node)
end;
{_, _} ->
{error, already_leaving}
end.
standard_leave(Node) ->
riak_core_ring_manager:ring_trans(
fun(Ring2, _) ->
Ring3 = riak_core_ring:leave_member(Node, Ring2, Node),
{new_ring, Ring3}
end, []),
ok.
%% @spec remove_from_cluster(ExitingNode :: atom()) -> term()
%% @doc Cause all partitions owned by ExitingNode to be taken over
%% by other nodes.
remove_from_cluster(ExitingNode) when is_atom(ExitingNode) ->
remove(ExitingNode).
legacy_remove(Node) when is_atom(Node) ->
case catch(riak_core_gossip_legacy: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.
{error, only_member};
ok ->
ok
end.
vnode_modules() ->
case application:get_env(riak_core, vnode_modules) of
undefined -> [];
{ok, Mods} -> Mods
end.
bucket_fixups() ->
case application:get_env(riak_core, bucket_fixups) of
undefined -> [];
{ok, Mods} -> Mods
end.
%% @doc Register a riak_core application.
register([]) ->
ok;
register([{bucket_fixup, FixupMod}|T]) ->
register_mod(FixupMod, bucket_fixups),
register(T);
register([{vnode_module, VNodeMod}|T]) ->
register_mod(VNodeMod, vnode_modules),
register(T).
register_vnode_module(VNodeMod) when is_atom(VNodeMod) ->
register_mod(VNodeMod, vnode_modules).
register_mod(Module, Type) when is_atom(Module), is_atom(Type) ->
{ok, App} = case application:get_application(self()) of
{ok, AppName} -> {ok, AppName};
undefined -> app_for_module(Module)
end,
case application:get_env(riak_core, Type) of
undefined ->
application:set_env(riak_core, Type, [{App,Module}]);
{ok, Mods} ->
application:set_env(riak_core, Type, [{App,Module}|Mods])
end,
riak_core_ring_events:force_sync_update().
%% @spec add_guarded_event_handler(HandlerMod, Handler, Args) -> AddResult
%% HandlerMod = module()
%% Handler = module() | {module(), term()}
%% Args = list()
%% AddResult = ok | {error, Reason::term()}
add_guarded_event_handler(HandlerMod, Handler, Args) ->
add_guarded_event_handler(HandlerMod, Handler, Args, undefined).
%% @spec add_guarded_event_handler(HandlerMod, Handler, Args, ExitFun) -> AddResult
%% HandlerMod = module()
%% Handler = module() | {module(), term()}
%% Args = list()
%% ExitFun = fun(Handler, Reason::term())
%% AddResult = ok | {error, Reason::term()}
%%
%% @doc Add a "guarded" event handler to a gen_event instance.
%% A guarded handler is implemented as a supervised gen_server
%% (riak_core_eventhandler_guard) that adds a supervised handler in its
%% init() callback and exits when the handler crashes so it can be
%% restarted by the supervisor.
add_guarded_event_handler(HandlerMod, Handler, Args, ExitFun) ->
riak_core_eventhandler_sup:start_guarded_handler(HandlerMod, Handler, Args, ExitFun).
%% @spec delete_guarded_event_handler(HandlerMod, Handler, Args) -> Result
%% HandlerMod = module()
%% Handler = module() | {module(), term()}
%% Args = term()
%% Result = term() | {error, module_not_found} | {'EXIT', Reason}
%% Reason = term()
%%
%% @doc Delete a guarded event handler from a gen_event instance.
%%
%% Args is an arbitrary term which is passed as one of the arguments to
%% Module:terminate/2.
%%
%% The return value is the return value of Module:terminate/2. If the
%% specified event handler is not installed, the function returns
%% {error,module_not_found}. If the callback function fails with Reason,
%% the function returns {'EXIT',Reason}.
delete_guarded_event_handler(HandlerMod, Handler, Args) ->
riak_core_eventhandler_sup:stop_guarded_handler(HandlerMod, Handler, Args).
app_for_module(Mod) ->
app_for_module(application:which_applications(), Mod).
app_for_module([], _Mod) ->
undefined;
app_for_module([{App,_,_}|T], Mod) ->
{ok, Mods} = application:get_key(App, modules),
case lists:member(Mod, Mods) of
true -> {ok, App};
false -> app_for_module(T, Mod)
end.