forked from basho/riak_kv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathriak_kv_ensemble_backend.erl
243 lines (207 loc) · 8.37 KB
/
riak_kv_ensemble_backend.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
%% -------------------------------------------------------------------
%%
%% Copyright (c) 2013 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
%% Implementation of {@link //riak_ensemble/riak_ensemble_backend} behavior that
%% connects riak_ensemble to riak_kv vnodes.
%%
-module(riak_kv_ensemble_backend).
-behaviour(riak_ensemble_backend).
-export([init/3, new_obj/4]).
-export([obj_epoch/1, obj_seq/1, obj_key/1, obj_value/1]).
-export([set_obj_epoch/2, set_obj_seq/2, set_obj_value/2]).
-export([get/3, put/4, tick/5, ping/2, ready_to_start/0]).
-export([synctree_path/2]).
-export([reply/2]).
-export([obj_newer/2]).
-export([handle_down/4]).
-include_lib("riak_ensemble/include/riak_ensemble_types.hrl").
-record(state, {ensemble :: ensemble_id(),
id :: peer_id(),
proxy :: atom(),
proxy_ref :: reference(),
vnode_ref :: reference(),
async :: pid()}).
-type obj() :: riak_object:riak_object().
-type state() :: #state{}.
-type key() :: {_,_}.
-type value() :: any().
%%===================================================================
-spec init(ensemble_id(), peer_id(), [any()]) -> state().
init(Ensemble, Id, []) ->
{{kv, _PL, _N, Idx}, _} = Id,
Proxy = riak_core_vnode_proxy:reg_name(riak_kv_vnode, Idx),
ProxyRef = erlang:monitor(process, Proxy),
{ok, Vnode} = riak_core_vnode_manager:get_vnode_pid(Idx, riak_kv_vnode),
VnodeRef = erlang:monitor(process, Vnode),
#state{ensemble=Ensemble,
id=Id,
proxy=Proxy,
proxy_ref=ProxyRef,
vnode_ref=VnodeRef}.
%%===================================================================
-spec new_obj(epoch(), seq(), key(), value()) -> obj().
new_obj(Epoch, Seq, {B,K}, Value) ->
case riak_object:is_robject(Value) of
true ->
set_epoch_seq(Epoch, Seq, Value);
false ->
RObj = riak_object:new(B, K, Value),
set_epoch_seq(Epoch, Seq, RObj)
end.
set_epoch_seq(Epoch, Seq, RObj) ->
EpochSeq = (Epoch bsl 32) + Seq,
%% riak_object:set_vclock(RObj, vclock:fresh(Epoch, Seq)),
RObj2 = riak_object:set_vclock(RObj, vclock:fresh(eseq, EpochSeq)),
RObj3 = riak_object:update_last_modified(RObj2),
riak_object:apply_updates(RObj3).
get_epoch_seq(RObj) ->
EpochSeq = vclock:get_counter(eseq, riak_object:vclock(RObj)),
<<Epoch:32/integer, Seq:32/integer>> = <<EpochSeq:64/integer>>,
{Epoch, Seq}.
%%===================================================================
-spec obj_epoch(obj()) -> epoch().
obj_epoch(RObj) ->
{Epoch, _} = get_epoch_seq(RObj),
Epoch.
-spec obj_seq(obj()) -> seq().
obj_seq(RObj) ->
{_, Seq} = get_epoch_seq(RObj),
Seq.
-spec obj_key(obj()) -> key().
obj_key(RObj) ->
{riak_object:bucket(RObj), riak_object:key(RObj)}.
-spec obj_value(obj()) -> value().
obj_value(RObj) ->
riak_object:get_value(RObj).
%% TODO: Move to riak_ensemble_backend or something
obj_newer(TestObj, BaseObj) ->
A = {obj_epoch(TestObj), obj_seq(TestObj)},
B = {obj_epoch(BaseObj), obj_seq(BaseObj)},
A > B.
%%===================================================================
-spec set_obj_epoch(epoch(), obj()) -> obj().
set_obj_epoch(Epoch, RObj) ->
{_, Seq} = get_epoch_seq(RObj),
set_epoch_seq(Epoch, Seq, RObj).
-spec set_obj_seq(seq(), obj()) -> obj().
set_obj_seq(Seq, RObj) ->
{Epoch, _} = get_epoch_seq(RObj),
set_epoch_seq(Epoch, Seq, RObj).
-spec set_obj_value(value(), obj()) -> obj().
set_obj_value(Value, RObj) ->
case riak_object:is_robject(Value) of
true ->
Contents = riak_object:get_contents(Value),
riak_object:set_contents(RObj, Contents);
false ->
riak_object:apply_updates(riak_object:update_value(RObj, Value))
end.
%%===================================================================
-spec get(key(), riak_ensemble_backend:from(), state()) -> state().
get(Key, From, State=#state{proxy=Proxy}) ->
ok = send_msg(Proxy, From, {ensemble_get, Key}),
State.
-spec put(key(), obj(), riak_ensemble_backend:from(), state()) -> state().
put(Key, Obj, From, State=#state{proxy=Proxy}) ->
ok = send_msg(Proxy, From, {ensemble_put, Key, Obj}),
State.
-spec send_msg(atom(), riak_ensemble_backend:from(), any()) -> ok.
send_msg(Proxy, From, Msg) ->
Msg2 = erlang:append_element(Msg, From),
catch Proxy ! Msg2,
ok.
-spec reply(riak_ensemble_backend:from(), any()) -> ok.
reply(From, Reply) ->
riak_ensemble_backend:reply(From, Reply),
ok.
%%===================================================================
-spec handle_down(reference(), pid(), term(), state()) -> false |
{reset, state()}.
handle_down(Ref, _Pid, Reason, #state{id=Id,
proxy=Proxy,
vnode_ref=VnodeRef,
proxy_ref=ProxyRef}=State) ->
{{kv, _PL, _N, Idx}, _} = Id,
case Ref of
VnodeRef ->
lager:warning("Vnode for Idx: ~p crashed with reason: ~p.",
[Idx, Reason]),
%% There are some races here. The vnode may not have been restarted yet
%% or the manager itself could be down.
%% TODO: Add a timer:sleep()? Something else?
{ok, Vnode} =
riak_core_vnode_manager:get_vnode_pid(Idx, riak_kv_vnode),
VnodeRef2 = erlang:monitor(process, Vnode),
{reset, State#state{vnode_ref=VnodeRef2}};
ProxyRef ->
lager:warning("Vnode Proxy for Idx: ~p crashed with reason: ~p.",
[Idx, Reason]),
ProxyRef2 = erlang:monitor(process, Proxy),
{reset, State#state{proxy_ref=ProxyRef2}};
_ ->
false
end.
%%===================================================================
-spec tick(epoch(), seq(), peer_id(), views(), state()) -> state().
tick(_Epoch, _Seq, _Leader, Views, State=#state{id=Id}) ->
%% TODO: Should this entire function be async?
{{kv, Idx, N, _}, _} = Id,
Latest = hd(Views),
{ok, CHBin} = riak_core_ring_manager:get_chash_bin(),
{PL, _} = chashbin:itr_pop(N, chashbin:exact_iterator(Idx, CHBin)),
%% TODO: Make ensembles/peers use ensemble/peer as actual peer name so this is unneeded
Peers = [{{kv, Idx, N, Idx2}, Node} || {Idx2, Node} <- PL],
Add = Peers -- Latest,
Del = Latest -- Peers,
Changes = [{add, Peer} || Peer <- Add] ++ [{del, Peer} || Peer <- Del],
case Changes of
[] ->
State;
_ ->
%% io:format("## ~p~n~p~n~p~n", [Peers, Latest, Changes]),
State2 = maybe_async_update(Changes, State),
State2
end.
maybe_async_update(Changes, State=#state{async=Async}) ->
CurrentAsync = is_pid(Async) andalso is_process_alive(Async),
case CurrentAsync of
true ->
State;
false ->
Self = self(),
Async2 = spawn(fun() ->
riak_ensemble_peer:update_members(Self, Changes, 5000)
end),
State#state{async=Async2}
end.
%%===================================================================
ping(From, State=#state{proxy=Proxy}) ->
catch Proxy ! {ensemble_ping, From},
{async, State}.
-spec ready_to_start() -> boolean().
ready_to_start() ->
lists:member(riak_kv, riak_core_node_watcher:services(node())).
synctree_path(_Ensemble, Id) ->
{{kv, PL, N, Idx}, _} = Id,
Bin = term_to_binary({PL, N}),
%% Use a prefix byte to leave open the possibility of different
%% tree id encodings (eg. not term_to_binary) in the future.
TreeId = <<0, Bin/binary>>,
{TreeId, "kv_" ++ integer_to_list(Idx)}.