-
Notifications
You must be signed in to change notification settings - Fork 96
/
riak_cs_riak_client.erl
383 lines (336 loc) · 13.4 KB
/
riak_cs_riak_client.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
%% ---------------------------------------------------------------------
%%
%% Copyright (c) 2007-2014 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_cs_riak_client).
-behaviour(gen_server).
%% API
-export([checkout/0, checkout/1,
checkin/1, checkin/2]).
-export([pbc_pools/0,
pbc_pool_name/1,
rts_puller/4]).
-export([
stop/1,
get_bucket/2,
set_bucket_name/2,
get_user/2,
save_user/3,
set_manifest_bag/2,
get_manifest_bag/1,
set_manifest/2,
master_pbc/1,
manifest_pbc/1,
block_pbc/1
]).
-export([start_link/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
%% exported for other `riak_client' implementations
-export([get_bucket_with_pbc/2,
get_user_with_pbc/2,
save_user_with_pbc/3]).
-include("riak_cs.hrl").
-include_lib("riak_pb/include/riak_pb_kv_codec.hrl").
-include_lib("riakc/include/riakc.hrl").
-define(SERVER, ?MODULE).
-record(state, {
master_pbc :: undefined | pid(),
bucket_name,
bucket_obj
}).
start_link(_Args) ->
case application:get_env(riak_cs, riak_client) of
{ok, Mod} ->
gen_server:start_link(Mod, [], []);
undefined ->
Mod = case riak_cs_config:is_multibag_enabled() of
true -> riak_cs_multibag_riak_client;
false -> ?MODULE
end,
application:set_env(riak_cs, riak_client, Mod),
gen_server:start_link(Mod, [], [])
end.
stop(Pid) ->
gen_server:call(Pid, stop).
-spec checkout() -> {ok, riak_client()} | {error, term()}.
checkout() ->
checkout(request_pool).
-spec checkout(atom()) -> {ok, riak_client()} | {error, term()}.
checkout(Pool) ->
try
case poolboy:checkout(Pool, false) of
full ->
{error, all_workers_busy};
RcPid ->
ok = gen_server:call(RcPid, cleanup),
{ok, RcPid}
end
catch
_:Error ->
{error, {poolboy_error, Error}}
end.
-spec checkin(riak_client()) -> ok.
checkin(RcPid) ->
checkin(request_pool, RcPid).
-spec checkin(atom(), riak_client()) -> ok.
checkin(Pool, RcPid) ->
ok = gen_server:call(RcPid, cleanup),
poolboy:checkin(Pool, RcPid).
-spec pbc_pools() -> [atom()].
pbc_pools() ->
[pbc_pool_name(B) || {B, _, _} <- riak_cs_mb_helper:bags()].
-spec pbc_pool_name(master | bag_id()) -> atom().
pbc_pool_name(master) ->
pbc_pool_master;
pbc_pool_name(undefined) ->
pbc_pool_master;
pbc_pool_name(BagId) when is_binary(BagId) ->
list_to_atom(lists:flatten(io_lib:format("pbc_pool_~s", [BagId]))).
%% @doc Make a thunk that looks up samples for a given bucket and suffix.
-spec rts_puller(riak_client(), binary(), iolist(), riak_cs_stats:key()) -> fun().
rts_puller(RcPid, Bucket, Suffix, StatsKey) ->
fun(Slice, {Samples, Errors}) ->
{ok, MasterPbc} = riak_cs_riak_client:master_pbc(RcPid),
Timeout = riak_cs_config:get_access_timeout(),
case riak_cs_pbc:get(MasterPbc, Bucket, rts:slice_key(Slice, Suffix), [],
Timeout, StatsKey) of
{ok, Object} ->
RawSamples =
[ catch element(2, {struct,_}=mochijson2:decode(V))
|| V <- riakc_obj:get_values(Object) ],
{NewSamples, EncodingErrors} =
lists:partition(fun({'EXIT',_}) -> false;
(_) -> true
end,
RawSamples),
{NewSamples++Samples,
[{Slice, {encoding, length(EncodingErrors)}}
|| EncodingErrors /= []]
++Errors};
{error, notfound} ->
%% this is normal - we ask for all possible
%% archives, and just deal with the ones that exist
{Samples, Errors};
{error, Error} ->
{Samples, [{Slice, Error}|Errors]}
end
end.
-spec get_bucket(riak_client(), binary()) -> {ok, riakc_obj:riakc_obj()} | {error, term()}.
get_bucket(RcPid, BucketName) when is_binary(BucketName) ->
gen_server:call(RcPid, {get_bucket, BucketName}, infinity).
-spec set_bucket_name(riak_client(), binary()) -> ok | {error, term()}.
set_bucket_name(RcPid, BucketName) when is_binary(BucketName) ->
gen_server:call(RcPid, {set_bucket_name, BucketName}, infinity).
%% @doc Perform an initial read attempt with R=PR=N.
%% If the initial read fails retry using
%% R=quorum and PR=1, but indicate that bucket deletion
%% indicators should not be cleaned up.
-spec get_user(riak_client(),
UserKey :: binary()) ->
{ok, {riakc_obj:riakc_obj(), KeepDeletedBuckets :: boolean()}} |
{error, term()}.
get_user(RcPid, UserKey) when is_binary(UserKey) ->
gen_server:call(RcPid, {get_user, UserKey}, infinity).
-spec save_user(riak_client(), rcs_user(), riakc_obj:riakc_obj()) -> ok | {error, term()}.
save_user(RcPid, User, OldUserObj) ->
gen_server:call(RcPid, {save_user, User, OldUserObj}, infinity).
-spec set_manifest(riak_client(), lfs_manifest()) -> ok | {error, term()}.
set_manifest(RcPid, Manifest) ->
gen_server:call(RcPid, {set_manifest, {Manifest?MANIFEST.uuid, Manifest}}).
-spec set_manifest_bag(riak_client(), binary()) -> ok | {error, term()}.
set_manifest_bag(RcPid, ManifestBagId) ->
gen_server:call(RcPid, {set_manifest_bag, ManifestBagId}).
-spec get_manifest_bag(riak_client()) -> {ok, binary()} | {error, term()}.
get_manifest_bag(RcPid) ->
gen_server:call(RcPid, get_manifest_bag).
%% TODO: Using this function is more or less a cheat.
%% It's better to export new function to manipulate manifests
%% from this module.
-spec master_pbc(riak_client()) -> {ok, MasterPbc::pid()} | {error, term()}.
master_pbc(RcPid) ->
gen_server:call(RcPid, master_pbc).
%% TODO: Also this is cheat
-spec manifest_pbc(riak_client()) -> {ok, ManifetPbc::pid()} | {error, term()}.
manifest_pbc(RcPid) ->
gen_server:call(RcPid, manifest_pbc).
%% TODO: Also this is cheat
-spec block_pbc(riak_client()) -> {ok, BlockPbc::pid()} | {error, term()}.
block_pbc(RcPid) ->
gen_server:call(RcPid, block_pbc).
%%% Internal functions
init([]) ->
{ok, fresh_state()}.
handle_call(stop, _From, State) ->
_ = do_cleanup(State),
{stop, normal, ok, State};
handle_call(cleanup, _From, State) ->
{reply, ok, do_cleanup(State)};
handle_call({get_bucket, BucketName}, _From, State) ->
case do_get_bucket(State#state{bucket_name=BucketName}) of
{ok, #state{bucket_obj=BucketObj} = NewState} ->
{reply, {ok, BucketObj}, NewState};
{error, Reason, NewState} ->
{reply, {error, Reason}, NewState}
end;
handle_call({set_bucket_name, _BucketName}, _From, State) ->
{reply, ok, State};
handle_call({get_user, UserKey}, _From, State) ->
case ensure_master_pbc(State) of
{ok, #state{master_pbc=MasterPbc} = NewState} ->
Res = get_user_with_pbc(MasterPbc, UserKey),
{reply, Res, NewState};
{error, Reason} ->
{reply, {error, Reason}, State}
end;
handle_call({save_user, User, OldUserObj}, _From, State) ->
case ensure_master_pbc(State) of
{ok, #state{master_pbc=MasterPbc} = NewState} ->
Res = save_user_with_pbc(MasterPbc, User, OldUserObj),
{reply, Res, NewState};
{error, Reason} ->
{reply, {error, Reason}, State}
end;
handle_call(master_pbc, _From, State) ->
case ensure_master_pbc(State) of
{ok, #state{master_pbc=MasterPbc} = NewState} ->
{reply, {ok, MasterPbc}, NewState};
{error, Reason} ->
{reply, {error, Reason}, State}
end;
handle_call(manifest_pbc, _From, State) ->
case ensure_master_pbc(State) of
{ok, #state{master_pbc=MasterPbc} = NewState} ->
{reply, {ok, MasterPbc}, NewState};
{error, Reason} ->
{reply, {error, Reason}, State}
end;
handle_call({set_manifest, {_UUID, _Manifest}}, _From, State) ->
{reply, ok, State};
handle_call({set_manifest_bag, _ManifestBagId}, _From, State) ->
{reply, ok, State};
handle_call(get_manifest_bag, _From, State) ->
{reply, {ok, master}, State};
handle_call(block_pbc, _From, State) ->
case ensure_master_pbc(State) of
{ok, #state{master_pbc=MasterPbc} = NewState} ->
{reply, {ok, MasterPbc}, NewState};
{error, Reason} ->
{reply, {error, Reason}, State}
end;
handle_call(Request, _From, State) ->
Reply = {error, {invalid_request, Request}},
{reply, Reply, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%% Internal functions
fresh_state() ->
#state{}.
do_cleanup(State) ->
stop_pbc(State#state.master_pbc),
fresh_state().
stop_pbc(undefined) ->
ok;
stop_pbc(Pbc) when is_pid(Pbc) ->
riak_cs_utils:close_riak_connection(pbc_pool_name(master), Pbc),
ok.
do_get_bucket(State) ->
case ensure_master_pbc(State) of
{ok, #state{master_pbc=MasterPbc, bucket_name=BucketName} = NewState} ->
case get_bucket_with_pbc(MasterPbc, BucketName) of
{ok, Obj} ->
{ok, NewState#state{bucket_obj=Obj}};
{error, Reason} ->
{error, Reason, NewState}
end;
{error, Reason} ->
{error, Reason, State}
end.
ensure_master_pbc(#state{master_pbc = MasterPbc} = State)
when is_pid(MasterPbc) ->
{ok, State};
ensure_master_pbc(#state{} = State) ->
case riak_cs_utils:riak_connection(pbc_pool_name(master)) of
{ok, MasterPbc} -> {ok, State#state{master_pbc=MasterPbc}};
{error, Reason} -> {error, Reason}
end.
get_bucket_with_pbc(MasterPbc, BucketName) ->
Timeout = riak_cs_config:get_bucket_timeout(),
riak_cs_pbc:get(MasterPbc, ?BUCKETS_BUCKET, BucketName, [], Timeout,
[riakc, get_cs_bucket]).
get_user_with_pbc(MasterPbc, Key) ->
get_user_with_pbc(MasterPbc, Key, riak_cs_config:fast_user_get()).
get_user_with_pbc(MasterPbc, Key, true) ->
weak_get_user_with_pbc(MasterPbc, Key);
get_user_with_pbc(MasterPbc, Key, false) ->
case strong_get_user_with_pbc(MasterPbc, Key) of
{ok, _} = OK -> OK;
{error, <<"{pr_val_unsatisfied,", _/binary>>} ->
weak_get_user_with_pbc(MasterPbc, Key);
{error, Reason} ->
_ = lager:warning("Fetching user record with strong option failed: ~p", [Reason]),
Timeout = riak_cs_config:get_user_timeout(),
_ = riak_cs_pbc:pause_to_reconnect(MasterPbc, Reason, Timeout),
weak_get_user_with_pbc(MasterPbc, Key)
end.
strong_get_user_with_pbc(MasterPbc, Key) ->
StrongOptions = [{r, all}, {pr, all}, {notfound_ok, false}],
Timeout = riak_cs_config:get_user_timeout(),
case riak_cs_pbc:get(MasterPbc, ?USER_BUCKET, Key, StrongOptions,
Timeout, [riakc, get_cs_user_strong]) of
{ok, Obj} ->
%% since we read from all primaries, we're less concerned
%% with there being an 'out-of-date' replica that we might
%% conflict with (and not be able to properly resolve
%% conflicts).
KeepDeletedBuckets = false,
{ok, {Obj, KeepDeletedBuckets}};
{error, _} = Error ->
Error
end.
weak_get_user_with_pbc(MasterPbc, Key) ->
Timeout = riak_cs_config:get_user_timeout(),
WeakOptions = [{r, quorum}, {pr, one}, {notfound_ok, false}],
case riak_cs_pbc:get(MasterPbc, ?USER_BUCKET, Key, WeakOptions,
Timeout, [riakc, get_cs_user]) of
{ok, Obj} ->
%% We weren't able to read from all primary vnodes, so
%% don't risk losing information by pruning the bucket
%% list.
KeepDeletedBuckets = true,
{ok, {Obj, KeepDeletedBuckets}};
{error, Reason} ->
{error, Reason}
end.
save_user_with_pbc(MasterPbc, User, OldUserObj) ->
Indexes = [{?EMAIL_INDEX, User?RCS_USER.email},
{?ID_INDEX, User?RCS_USER.canonical_id}],
MD = dict:store(?MD_INDEX, Indexes, dict:new()),
UpdUserObj = riakc_obj:update_metadata(
riakc_obj:update_value(OldUserObj,
riak_cs_utils:encode_term(User)),
MD),
Timeout = riak_cs_config:put_user_timeout(),
riak_cs_pbc:put(MasterPbc, UpdUserObj, Timeout, [riakc, put_cs_user]).