/
riak_core_ring_manager.erl
332 lines (279 loc) · 11.4 KB
/
riak_core_ring_manager.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
330
331
%% -------------------------------------------------------------------
%%
%% riak_core: Core Riak Application
%%
%% Copyright (c) 2007-2010 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.
%%
%% -------------------------------------------------------------------
%% @doc the local view of the cluster's ring configuration
-module(riak_core_ring_manager).
-include_lib("eunit/include/eunit.hrl").
-define(RING_KEY, riak_ring).
-behaviour(gen_server2).
-export([start_link/0,
start_link/1,
get_my_ring/0,
refresh_my_ring/0,
set_my_ring/1,
write_ringfile/0,
prune_ringfiles/0,
read_ringfile/1,
find_latest_ringfile/0,
do_write_ringfile/1,
ring_trans/2,
stop/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-ifdef(TEST).
-export([set_ring_global/1]).
-include_lib("eunit/include/eunit.hrl").
-endif.
%% ===================================================================
%% Public API
%% ===================================================================
start_link() ->
gen_server2:start_link({local, ?MODULE}, ?MODULE, [live], []).
%% Testing entry point
start_link(test) ->
gen_server2:start_link({local, ?MODULE}, ?MODULE, [test], []).
%% @spec get_my_ring() -> {ok, riak_core_ring:riak_core_ring()} | {error, Reason}
get_my_ring() ->
case mochiglobal:get(?RING_KEY) of
Ring when is_tuple(Ring) -> {ok, Ring};
undefined -> {error, no_ring}
end.
%% @spec refresh_my_ring() -> ok
refresh_my_ring() ->
gen_server2:call(?MODULE, refresh_my_ring, infinity).
%% @spec set_my_ring(riak_core_ring:riak_core_ring()) -> ok
set_my_ring(Ring) ->
gen_server2:call(?MODULE, {set_my_ring, Ring}, infinity).
%% @spec write_ringfile() -> ok
write_ringfile() ->
gen_server2:cast(?MODULE, write_ringfile).
ring_trans(Fun, Args) ->
gen_server2:call(?MODULE, {ring_trans, Fun, Args}, infinity).
do_write_ringfile(Ring) ->
{{Year, Month, Day},{Hour, Minute, Second}} = calendar:universal_time(),
TS = io_lib:format(".~B~2.10.0B~2.10.0B~2.10.0B~2.10.0B~2.10.0B",
[Year, Month, Day, Hour, Minute, Second]),
case app_helper:get_env(riak_core, ring_state_dir) of
"<nostore>" -> nop;
Dir ->
Cluster = app_helper:get_env(riak_core, cluster_name),
FN = Dir ++ "/riak_core_ring." ++ Cluster ++ TS,
ok = filelib:ensure_dir(FN),
ok = file:write_file(FN, term_to_binary(Ring))
end.
%% @spec find_latest_ringfile() -> string()
find_latest_ringfile() ->
Dir = app_helper:get_env(riak_core, ring_state_dir),
case file:list_dir(Dir) of
{ok, Filenames} ->
Cluster = app_helper:get_env(riak_core, cluster_name),
Timestamps = [list_to_integer(TS) || {"riak_core_ring", C1, TS} <-
[list_to_tuple(string:tokens(FN, ".")) || FN <- Filenames],
C1 =:= Cluster],
SortedTimestamps = lists:reverse(lists:sort(Timestamps)),
case SortedTimestamps of
[Latest | _] ->
{ok, Dir ++ "/riak_core_ring." ++ Cluster ++ "." ++ integer_to_list(Latest)};
_ ->
{error, not_found}
end;
{error, Reason} ->
{error, Reason}
end.
%% @spec read_ringfile(string()) -> riak_core_ring:riak_core_ring()
read_ringfile(RingFile) ->
{ok, Binary} = file:read_file(RingFile),
binary_to_term(Binary).
%% @spec prune_ringfiles() -> ok
prune_ringfiles() ->
case app_helper:get_env(riak_core, ring_state_dir) of
"<nostore>" -> ok;
Dir ->
Cluster = app_helper:get_env(riak_core, cluster_name),
case file:list_dir(Dir) of
{error,enoent} -> ok;
{ok, []} -> ok;
{ok, Filenames} ->
Timestamps = [TS || {"riak_core_ring", C1, TS} <-
[list_to_tuple(string:tokens(FN, ".")) || FN <- Filenames],
C1 =:= Cluster],
if Timestamps /= [] ->
%% there are existing ring files
TSPat = [io_lib:fread("~4d~2d~2d~2d~2d~2d",TS) ||
TS <- Timestamps],
TSL = lists:reverse(lists:sort([TS ||
{ok,TS,[]} <- TSPat])),
Keep = prune_list(TSL),
KeepTSs = [lists:flatten(
io_lib:format(
"~B~2.10.0B~2.10.0B~2.10.0B~2.10.0B~2.10.0B",K))
|| K <- Keep],
DelFNs = [Dir ++ "/" ++ FN || FN <- Filenames,
lists:all(fun(TS) ->
string:str(FN,TS)=:=0
end, KeepTSs)],
[file:delete(DelFN) || DelFN <- DelFNs],
ok;
true ->
%% directory wasn't empty, but there are no ring
%% files in it
ok
end
end
end.
%% @private (only used for test instances)
stop() ->
gen_server2:cast(?MODULE, stop).
%% ===================================================================
%% gen_server callbacks
%% ===================================================================
init([Mode]) ->
case Mode of
live ->
Ring = riak_core_ring:fresh();
test ->
Ring = riak_core_ring:fresh(16,node())
end,
%% Set the ring and send initial notification to local observers that
%% ring has changed.
%% Do *not* save the ring to disk here. On startup we deliberately come
%% up with a ring where the local node owns all partitions so that any
%% fallback vnodes will be started so they can hand off.
set_ring_global(Ring),
riak_core_ring_events:ring_update(Ring),
{ok, Mode}.
handle_call({set_my_ring, Ring}, _From, State) ->
prune_write_notify_ring(Ring),
{reply,ok,State};
handle_call(refresh_my_ring, _From, State) ->
%% This node is leaving the cluster so create a fresh ring file
FreshRing = riak_core_ring:fresh(),
set_ring_global(FreshRing),
%% Make sure the fresh ring gets written before stopping
do_write_ringfile(FreshRing),
%% Handoff is complete and fresh ring is written
%% so we can safely stop now.
riak_core:stop("node removal completed, exiting."),
{reply,ok,State};
handle_call({ring_trans, Fun, Args}, _From, State) ->
{ok, Ring} = get_my_ring(),
case catch Fun(Ring, Args) of
{new_ring, NewRing} ->
prune_write_notify_ring(NewRing),
case riak_core_ring:random_other_node(NewRing) of
no_node ->
ignore;
Node ->
riak_core_gossip:send_ring(Node)
end,
{reply, {ok, NewRing}, State};
ignore ->
{reply, not_changed, State};
Other ->
error_logger:error_msg("ring_trans: invalid return value: ~p~n",
[Other]),
{reply, not_changed, State}
end.
handle_cast(stop, State) ->
{stop,normal,State};
handle_cast(write_ringfile, test) ->
{noreply,test};
handle_cast(write_ringfile, State) ->
{ok, Ring} = get_my_ring(),
do_write_ringfile(Ring),
{noreply,State}.
handle_info(_Info, State) ->
{noreply, State}.
%% @private
terminate(_Reason, _State) ->
ok.
%% @private
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%% ===================================================================
%% Internal functions
%% ===================================================================
prune_list([X|Rest]) ->
lists:usort(lists:append([[X],back(1,X,Rest),back(2,X,Rest),
back(3,X,Rest),back(4,X,Rest),back(5,X,Rest)])).
back(_N,_X,[]) -> [];
back(N,X,[H|T]) ->
case lists:nth(N,X) =:= lists:nth(N,H) of
true -> back(N,X,T);
false -> [H]
end.
%% Set the ring in mochiglobal. Exported during unit testing
%% to make test setup simpler - no need to spin up a riak_core_ring_manager
%% process.
set_ring_global(Ring) ->
mochiglobal:put(?RING_KEY, Ring).
%% Persist a new ring file, set the global value and notify any listeners
prune_write_notify_ring(Ring) ->
riak_core_ring_manager:prune_ringfiles(),
do_write_ringfile(Ring),
set_ring_global(Ring),
riak_core_ring_events:ring_update(Ring).
%% ===================================================================
%% Unit tests
%% ===================================================================
-ifdef(TEST).
back_test() ->
X = [1,2,3],
List1 = [[1,2,3],[4,2,3], [7,8,3], [11,12,13], [1,2,3]],
List2 = [[7,8,9], [1,2,3]],
List3 = [[1,2,3]],
?assertEqual([[4,2,3]], back(1, X, List1)),
?assertEqual([[7,8,9]], back(1, X, List2)),
?assertEqual([], back(1, X, List3)),
?assertEqual([[7,8,3]], back(2, X, List1)),
?assertEqual([[11,12,13]], back(3, X, List1)).
prune_list_test() ->
TSList1 = [[2011,2,28,16,32,16],[2011,2,28,16,32,36],[2011,2,28,16,30,27],[2011,2,28,16,32,16],[2011,2,28,16,32,36]],
TSList2 = [[2011,2,28,16,32,36],[2011,2,28,16,31,16],[2011,2,28,16,30,27],[2011,2,28,16,32,16],[2011,2,28,16,32,36]],
PrunedList1 = [[2011,2,28,16,30,27],[2011,2,28,16,32,16]],
PrunedList2 = [[2011,2,28,16,31,16],[2011,2,28,16,32,36]],
?assertEqual(PrunedList1, prune_list(TSList1)),
?assertEqual(PrunedList2, prune_list(TSList2)).
set_ring_global_test() ->
application:set_env(riak_core,ring_creation_size, 4),
Ring = riak_core_ring:fresh(),
set_ring_global(Ring),
?assertEqual(Ring, mochiglobal:get(?RING_KEY)).
set_my_ring_test() ->
application:set_env(riak_core,ring_creation_size, 4),
Ring = riak_core_ring:fresh(),
set_ring_global(Ring),
?assertEqual({ok, Ring}, get_my_ring()).
refresh_my_ring_test() ->
application:set_env(riak_core, ring_creation_size, 4),
application:set_env(riak_core, ring_state_dir, "/tmp"),
application:set_env(riak_core, cluster_name, "test"),
riak_core_ring_events:start_link(),
riak_core_ring_manager:start_link(test),
riak_core_vnode_sup:start_link(),
riak_core_vnode_master:start_link(riak_core_vnode),
riak_core_test_util:setup_mockring1(),
?assertEqual(ok, riak_core_ring_manager:refresh_my_ring()),
riak_core_ring_manager:stop(),
%% Cleanup the ring file created for this test
{ok, RingFile} = find_latest_ringfile(),
file:delete(RingFile).
-endif.