Skip to content
This repository
Browse code

Change ring_manager to keep ring in state, not mochiglobal

  • Loading branch information...
commit bee1d973a28767964a099390d0dfe1051837f325 1 parent 1f42b0e
Joseph Blomstedt jtuple authored

Showing 1 changed file with 29 additions and 24 deletions. Show diff stats Hide diff stats

  1. +29 24 src/riak_core_ring_manager.erl
53 src/riak_core_ring_manager.erl
@@ -50,6 +50,7 @@
50 50
51 51 -record(state, {
52 52 mode,
  53 + ring,
53 54 raw_ring
54 55 }).
55 56
@@ -73,10 +74,7 @@ start_link(test) ->
73 74
74 75 %% @spec get_my_ring() -> {ok, riak_core_ring:riak_core_ring()} | {error, Reason}
75 76 get_my_ring() ->
76   - case mochiglobal:get(?RING_KEY) of
77   - Ring when is_tuple(Ring) -> {ok, Ring};
78   - undefined -> {error, no_ring}
79   - end.
  77 + gen_server2:call(?MODULE, get_my_ring, infinity).
80 78
81 79 get_raw_ring() ->
82 80 gen_server2:call(?MODULE, get_raw_ring, infinity).
@@ -218,21 +216,23 @@ init([Mode]) ->
218 216 %% Do *not* save the ring to disk here. On startup we deliberately come
219 217 %% up with a ring where the local node owns all partitions so that any
220 218 %% fallback vnodes will be started so they can hand off.
221   - set_ring_global(Ring),
  219 + State = set_ring_global(Ring, #state{mode = Mode, raw_ring=Ring}),
222 220 riak_core_ring_events:ring_update(Ring),
223   - {ok, #state{mode = Mode, raw_ring=Ring}}.
  221 + {ok, State}.
224 222
225 223
  224 +handle_call(get_my_ring, _From, #state{ring=Ring} = State) ->
  225 + {reply, {ok, Ring}, State};
226 226 handle_call(get_raw_ring, _From, #state{raw_ring=Ring} = State) ->
227 227 {reply, {ok, Ring}, State};
228 228 handle_call({set_my_ring, RingIn}, _From, State) ->
229 229 Ring = riak_core_ring:upgrade(RingIn),
230   - prune_write_notify_ring(Ring),
231   - {reply,ok,State#state{raw_ring=Ring}};
  230 + State2 = prune_write_notify_ring(Ring, State),
  231 + {reply,ok,State2#state{raw_ring=Ring}};
232 232 handle_call(refresh_my_ring, _From, State) ->
233 233 %% This node is leaving the cluster so create a fresh ring file
234 234 FreshRing = riak_core_ring:fresh(),
235   - set_ring_global(FreshRing),
  235 + State2 = set_ring_global(FreshRing, State),
236 236 %% Make sure the fresh ring gets written before stopping
237 237 do_write_ringfile(FreshRing),
238 238
@@ -240,17 +240,17 @@ handle_call(refresh_my_ring, _From, State) ->
240 240 %% so we can safely stop now.
241 241 riak_core:stop("node removal completed, exiting."),
242 242
243   - {reply,ok,State#state{raw_ring=FreshRing}};
  243 + {reply,ok,State2#state{raw_ring=FreshRing}};
244 244 handle_call({ring_trans, Fun, Args}, _From, State=#state{raw_ring=Ring}) ->
245 245 case catch Fun(Ring, Args) of
246 246 {new_ring, NewRing} ->
247   - prune_write_notify_ring(NewRing),
  247 + State2 = prune_write_notify_ring(NewRing, State),
248 248 riak_core_gossip:random_recursive_gossip(NewRing),
249   - {reply, {ok, NewRing}, State#state{raw_ring=NewRing}};
  249 + {reply, {ok, NewRing}, State2#state{raw_ring=NewRing}};
250 250 {reconciled_ring, NewRing} ->
251   - prune_write_notify_ring(NewRing),
  251 + State2 = prune_write_notify_ring(NewRing, State),
252 252 riak_core_gossip:recursive_gossip(NewRing),
253   - {reply, {ok, NewRing}, State#state{raw_ring=NewRing}};
  253 + {reply, {ok, NewRing}, State2#state{raw_ring=NewRing}};
254 254 ignore ->
255 255 {reply, not_changed, State};
256 256 Other ->
@@ -260,14 +260,13 @@ handle_call({ring_trans, Fun, Args}, _From, State=#state{raw_ring=Ring}) ->
260 260 end;
261 261 handle_call({set_cluster_name, Name}, _From, State=#state{raw_ring=Ring}) ->
262 262 NewRing = riak_core_ring:set_cluster_name(Ring, Name),
263   - prune_write_notify_ring(NewRing),
264   - {reply, ok, State#state{raw_ring=NewRing}}.
  263 + State2 = prune_write_notify_ring(NewRing, State),
  264 + {reply, ok, State2#state{raw_ring=NewRing}}.
265 265
266 266 handle_cast(stop, State) ->
267 267 {stop,normal,State};
268 268
269   -handle_cast({refresh_my_ring, ClusterName}, State) ->
270   - {ok, Ring} = get_my_ring(),
  269 +handle_cast({refresh_my_ring, ClusterName}, State=#state{ring=Ring}) ->
271 270 case riak_core_ring:cluster_name(Ring) of
272 271 ClusterName ->
273 272 handle_cast(refresh_my_ring, State);
@@ -339,7 +338,11 @@ run_fixups([{App, Fixup}|T], BucketName, BucketProps) ->
339 338 %% Set the ring in mochiglobal. Exported during unit testing
340 339 %% to make test setup simpler - no need to spin up a riak_core_ring_manager
341 340 %% process.
342   -set_ring_global(Ring) ->
  341 +%% TODO: No longer using mochiglobal, fix unit tests to start manager.
  342 +set_ring_global(_Ring) ->
  343 + throw(todo_fix_test).
  344 +
  345 +set_ring_global(Ring, State) ->
343 346 DefaultProps = case application:get_env(riak_core, default_bucket_props) of
344 347 {ok, Val} ->
345 348 Val;
@@ -372,16 +375,16 @@ set_ring_global(Ring) ->
372 375 %% Mark ring as tainted to check if it is ever leaked over gossip or
373 376 %% relied upon for any non-local ring operations.
374 377 TaintedRing = riak_core_ring:set_tainted(FixedRing),
375   - %% store the modified ring in mochiglobal
376   - mochiglobal:put(?RING_KEY, TaintedRing).
  378 + State#state{ring=TaintedRing}.
377 379
378 380 %% Persist a new ring file, set the global value and notify any listeners
379   -prune_write_notify_ring(Ring) ->
  381 +prune_write_notify_ring(Ring, State) ->
380 382 riak_core_ring:check_tainted(Ring, "Error: Persisting tainted ring"),
381 383 riak_core_ring_manager:prune_ringfiles(),
382 384 do_write_ringfile(Ring),
383   - set_ring_global(Ring),
384   - riak_core_ring_events:ring_update(Ring).
  385 + NewState = set_ring_global(Ring, State),
  386 + riak_core_ring_events:ring_update(Ring),
  387 + NewState.
385 388
386 389 %% ===================================================================
387 390 %% Unit tests
@@ -407,12 +410,14 @@ prune_list_test() ->
407 410 ?assertEqual(PrunedList1, prune_list(TSList1)),
408 411 ?assertEqual(PrunedList2, prune_list(TSList2)).
409 412
  413 +%% TODO: Fix/remove test.
410 414 set_ring_global_test() ->
411 415 application:set_env(riak_core,ring_creation_size, 4),
412 416 Ring = riak_core_ring:fresh(),
413 417 set_ring_global(Ring),
414 418 ?assert(riak_core_ring:nearly_equal(Ring, mochiglobal:get(?RING_KEY))).
415 419
  420 +%% TODO: Fix test to start riak_core_ring_manager to use new get_my_ring
416 421 set_my_ring_test() ->
417 422 application:set_env(riak_core,ring_creation_size, 4),
418 423 Ring = riak_core_ring:fresh(),

0 comments on commit bee1d97

Please sign in to comment.
Something went wrong with that request. Please try again.