forked from discoproject/disco
/
ddfs_tag.erl
745 lines (652 loc) · 28.5 KB
/
ddfs_tag.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
-module(ddfs_tag).
-behaviour(gen_server).
-include("common_types.hrl").
-include("config.hrl").
-include("ddfs.hrl").
-include("ddfs_gc.hrl").
-include("ddfs_tag.hrl").
-include("gs_util.hrl").
-export([start/2, init/1, handle_call/3, handle_cast/2,
handle_info/2, terminate/2, code_change/3]).
-type replica() :: {erlang:timestamp(), nonempty_string()}.
-type replyto() :: {pid(), reference()}.
-record(state, {tag :: tagname(),
data :: none |
{missing, notfound} |
{missing, deleted} |
{error, _} |
{ok, #tagcontent{}},
delayed :: false | gb_tree(),
timeout :: non_neg_integer(),
replicas :: false | too_many_failed_nodes
| [{replica(), node()}],
url_cache :: false | gb_set()}).
-type state() :: #state{}.
% API messages.
-type get_msg() :: {get, attrib() | all, token()}.
-type put_msg() :: {put, attrib(), string(), token()}.
-type update_msg() :: {update, [url()], token(), proplists:proplist()}.
-type delayed_update_msg() :: {delayed_update, [url()], token(), proplists:proplist()}.
-type delete_attrib_msg() :: {delete_attrib, attrib(), token()}.
-type delete_msg() :: {delete, token()}.
% Special msg for GC.
-type gc_get_msg() :: gc_get.
% Special messages for the +deleted tag.
-type has_tagname_msg() :: {has_tagname, tagname()}.
-type get_tagnames_msg() :: get_tagnames.
-type delete_tagname_msg() :: {delete_tagname, tagname()}.
% Notifications.
-type notify_msg() :: {notify, term()}.
-type call_msg() :: get_msg() | put_msg() | update_msg() | delayed_update_msg()
| delete_attrib_msg() | delete_msg()
| has_tagname_msg() | get_tagnames_msg() | delete_tagname_msg()
| gc_get_msg().
-type cast_msg() :: notify_msg().
-export_type([call_msg/0, cast_msg/0]).
% THOUGHT: Eventually we want to partition tag keyspace instead
% of using a single global keyspace. This can be done relatively
% easily with consistent hashing: Each tag has a set of nodes
% (partition) which it operates on. If tag can't be found in its
% partition (due to addition of many new nodes, for instance),
% partition to be searched can be increased incrementally until
% the tag is found.
%
% Correspondingly GC'ing must ensure that K replicas for a tag
% are found within its partition rather than globally.
-spec start(tagname(), boolean()) -> ignore | {error,_} | {ok,pid()}.
start(TagName, NotFound) ->
application:start(crypto),
application:start(public_key),
application:start(ssl),
application:start(inets),
application:start(xmerl),
application:start(erlcloud),
gen_server:start(?MODULE, {TagName, NotFound}, []).
-spec init({tagname(), boolean()}) -> gs_init().
init({TagName, true}) ->
init(TagName, {missing, notfound}, ?TAG_EXPIRES_ONERROR);
init({TagName, false}) ->
init(TagName, none, ?TAG_EXPIRES).
-spec init(tagname(), none | {missing, notfound}, non_neg_integer()) -> gs_init().
init(TagName, Data, Timeout) ->
put(min_tagk, list_to_integer(disco:get_setting("DDFS_TAG_MIN_REPLICAS"))),
put(tagk, list_to_integer(disco:get_setting("DDFS_TAG_REPLICAS"))),
put(blobk, list_to_integer(disco:get_setting("DDFS_BLOB_REPLICAS"))),
put(use_s3, string:equal(disco:get_setting("DISCO_USE_S3"), "true")),
case get(use_s3) of
true ->
put(s3_bucket, disco:get_setting("DISCO_S3_BUCKET")),
disco_aws:set_aws_creds();
false ->
nothing
end,
{ok, #state{tag = TagName,
data = Data,
delayed = false,
replicas = false,
url_cache = false,
timeout = Timeout}}.
-type msg() :: call_msg()
%% internal messages:
| gc_get0.
-type notify() :: notify_msg()
%% internal messages:
| {notify0, term()}.
%%% Note to the reader!
%%%
%%% handle_casts below form a state machine. The order in which the functions
%%% are specified is very significant. The easiest way to understand the state
%%% machine is to follow the logic from the top to the bottom.
%%%
-spec handle_cast({msg(), from()} | {die, none} | notify(), state())
-> gs_noreply() | gs_stop(normal).
% We don't want to cache requests made by garbage collector
handle_cast({gc_get, ReplyTo}, #state{data = none} = S) ->
handle_cast({gc_get0, ReplyTo}, S#state{timeout = 100});
handle_cast({notify, M}, #state{data = none} = S) ->
handle_cast({notify0, M}, S#state{timeout = 100});
% On the other hand, if tag is already cached, GC'ing should
% not trash it
handle_cast({gc_get, ReplyTo}, S) ->
handle_cast({gc_get0, ReplyTo}, S);
handle_cast({notify, M}, S) ->
handle_cast({notify0, M}, S);
% First request for this tag: No tag data loaded - load it
handle_cast(M, #state{data = none, tag = Tag, timeout = TO} = S) ->
{Data, Replicas, Timeout} =
case is_tag_deleted(Tag) of
true ->
{{missing, deleted}, false, ?TAG_EXPIRES_ONERROR};
false ->
case get_tagdata(Tag) of
{ok, TagData, Repl} ->
case ddfs_tag_util:decode_tagcontent(TagData) of
{ok, Content} ->
{{ok, Content}, Repl, TO};
{error, _} = E ->
{E, false, ?TAG_EXPIRES_ONERROR}
end;
{missing, notfound} = E ->
{E, false, ?TAG_EXPIRES_ONERROR};
{error, _} = E ->
{E, false, ?TAG_EXPIRES_ONERROR}
end;
{error, _} = E ->
{E, false, ?TAG_EXPIRES_ONERROR}
end,
handle_cast(M, S#state{data = Data,
replicas = Replicas,
timeout = lists:min([Timeout, TO])});
% Delayed update with an empty buffer, initialize the buffer and a flush process
handle_cast({{delayed_update, _, _, _}, _} = M,
#state{data = {ok, _}, delayed = false, tag = Tag} = S) ->
spawn(fun() ->
timer:sleep(?DELAYED_FLUSH_INTERVAL),
ddfs:get_tag(ddfs_master, binary_to_list(Tag), all, internal)
end),
handle_cast(M, S#state{delayed = gb_trees:empty()});
% Normal delayed update, add request to the buffer, reply later
handle_cast({{delayed_update, Urls, Token, Opt}, ReplyTo},
#state{data = {ok, _}, delayed = Buffer} = S) ->
Do = fun(_TokenInfo) ->
do_delayed_update(Urls, Opt, ReplyTo, Buffer, S)
end,
S1 = authorize(write, Token, ReplyTo, S, Do),
{noreply, S1, S1#state.timeout};
% Delayed update but the tag doesn't exist yet, fall back to normal update
handle_cast({{delayed_update, Urls, Token, Opt}, ReplyTo}, S) ->
handle_cast({{update, Urls, Token, Opt}, ReplyTo}, S);
% Before handling any other requests, flush pending delayed updates
handle_cast(M, #state{delayed = Buffer} = S0) when Buffer =/= false ->
Fun =
fun({Opt, {Waiters, Urls}}, S) ->
% Note that we can supply bogus TokenInfo to do_update here,
% as it is only used when creating a new tag. We can
% guarantee that the tag exists at this point.
do_update({write, null}, Urls, Opt, Waiters, S)
end,
NewState =
lists:foldl(Fun, S0#state{delayed = false}, gb_trees:to_list(Buffer)),
handle_cast(M, NewState);
handle_cast({{delete, Token}, ReplyTo}, S) ->
Do = fun(_TokenInfo) -> do_delete(ReplyTo, S) end,
S1 = authorize(write, Token, ReplyTo, S, Do),
{noreply, S1, S1#state.timeout};
handle_cast({die, _}, S) ->
{stop, normal, S};
handle_cast({{get, Attrib, Token}, ReplyTo}, #state{data = {ok, D}} = S) ->
Do = fun(TokenInfo) ->
gen_server:reply(ReplyTo, do_get(TokenInfo, Attrib, D)),
S
end,
S1 = authorize(read, Token, ReplyTo, S, Do),
{noreply, S1, S1#state.timeout};
handle_cast({{get, _, _}, ReplyTo}, #state{data = Data, timeout = TO} = S) ->
gen_server:reply(ReplyTo, Data),
{noreply, S, TO};
handle_cast({_, ReplyTo}, #state{data = {error, _} = Data, timeout = TO} = S) ->
gen_server:reply(ReplyTo, Data),
{noreply, S, TO};
handle_cast({gc_get0, ReplyTo},
#state{data = {ok, D}, replicas = Replicas, timeout = TO} = S) ->
R = {D#tagcontent.id, D#tagcontent.urls, Replicas},
gen_server:reply(ReplyTo, R),
{noreply, S, TO};
handle_cast({gc_get0, ReplyTo}, #state{data = Data,
replicas = Replicas,
timeout = TO} = S) ->
gen_server:reply(ReplyTo, {Data, Replicas}),
{noreply, S, TO};
handle_cast({{update, Urls, Token, Opt}, ReplyTo}, S) ->
Do = fun(TokenInfo) -> do_update(TokenInfo, Urls, Opt, ReplyTo, S) end,
S1 = authorize(write, Token, ReplyTo, S, Do),
{noreply, S1, S1#state.timeout};
handle_cast({{put, Field, Value, Token}, ReplyTo}, #state{timeout = TO} = S) ->
case ddfs_tag_util:validate_value(Field, Value) of
true ->
Do =
fun (TokenInfo) ->
S1 = do_put(TokenInfo, Field, Value, ReplyTo, S),
S1#state{url_cache = false}
end,
S1 = authorize(write, Token, ReplyTo, S, Do),
{noreply, S1, S1#state.timeout};
false ->
_ = send_replies(ReplyTo, {error, invalid_attribute_value}),
{noreply, S, TO}
end;
handle_cast({{delete_attrib, Field, Token}, ReplyTo},
#state{data = {ok, _}} = S) ->
Do = fun(_TokenInfo) -> do_delete_attrib(Field, ReplyTo, S) end,
S1 = authorize(write, Token, ReplyTo, S, Do),
{noreply, S1, S1#state.timeout};
handle_cast({{delete_attrib, _Field, _Token}, ReplyTo},
#state{timeout = TO} = S) ->
_ = send_replies(ReplyTo, {error, unknown_attribute}),
{noreply, S, TO};
handle_cast({notify0, {gc_rr_update, Updates, Blacklist, UpdateId}}, S) ->
S1 = do_gc_rr_update(S, Updates, Blacklist, UpdateId),
{noreply, S1, S1#state.timeout};
% Special operations for the +deleted metatag
handle_cast(M, #state{url_cache = false, data = {missing, notfound}} = S) ->
handle_cast(M, S#state{url_cache = gb_sets:empty()});
handle_cast(M, #state{url_cache = false, data = {ok, Data}} = S) ->
Urls = Data#tagcontent.urls,
handle_cast(M, S#state{url_cache = init_url_cache(Urls)});
handle_cast({{has_tagname, Name}, ReplyTo},
#state{url_cache = Cache, timeout = TO} = S) ->
gen_server:reply(ReplyTo, gb_sets:is_member(Name, Cache)),
{noreply, S, TO};
handle_cast({get_tagnames, ReplyTo},
#state{url_cache = Cache, timeout = TO} = S) ->
gen_server:reply(ReplyTo, {ok, Cache}),
{noreply, S, TO};
handle_cast({{delete_tagname, Name}, ReplyTo}, #state{url_cache = Cache} = S) ->
NewDel = gb_sets:delete_any(Name, Cache),
NewUrls = [[<<"tag://", Tag/binary>>] || Tag <- gb_sets:to_list(NewDel)],
S1 = do_put({write, null},
urls,
NewUrls,
ReplyTo,
S#state{url_cache = NewDel}),
{noreply, S1, S1#state.timeout}.
-spec handle_call(term(), from(), state()) -> gs_reply(ok | state()).
handle_call(dbg_get_state, _, S) ->
{reply, S, S};
handle_call(_, _, S) -> {reply, ok, S}.
-spec handle_info(timeout, state()) -> gs_noreply_t() | gs_stop(normal);
({reference(), term()}, state()) -> gs_noreply().
handle_info(timeout, S) ->
handle_cast({die, none}, S);
% handle late replies to "catch gen_server:call"
handle_info({Ref, _Msg}, S) when is_reference(Ref) ->
{noreply, S}.
% callback stubs
-spec terminate(term(), state()) -> ok.
terminate(_Reason, _State) -> ok.
-spec code_change(term(), state(), term()) -> {ok, state()}.
code_change(_OldVsn, State, _Extra) -> {ok, State}.
-spec authorize(tokentype(), binary(), replyto(), state(),
fun((tokentype()) -> state())) -> state().
authorize(TokenType,
Token,
ReplyTo,
#state{data = {ok, #tagcontent{read_token = ReadToken,
write_token = WriteToken}}} = S,
Op) ->
authorize(TokenType, Token, ReplyTo, ReadToken, WriteToken, S, Op);
authorize(TokenType, Token, ReplyTo, State, Op) ->
authorize(TokenType, Token, ReplyTo, null, null, State, Op).
authorize(TokenType, Token, ReplyTo, ReadToken, WriteToken, S, Op) ->
case ddfs_tag_util:check_token(TokenType, Token, ReadToken, WriteToken) of
false ->
_ = send_replies(ReplyTo, {error, unauthorized}),
S;
TokenPrivilege ->
Op({TokenPrivilege, Token})
end.
do_update(TokenInfo, Urls, Opt, ReplyTo, #state{data = {missing, _}} = S) ->
do_update(TokenInfo, Urls, Opt, ReplyTo, [], S);
do_update(TokenInfo, Urls, Opt, ReplyTo, #state{data = {ok, D}} = S) ->
OldUrls = D#tagcontent.urls,
do_update(TokenInfo, Urls, Opt, ReplyTo, OldUrls, S).
do_update(TokenInfo, Urls, Opt, ReplyTo, OldUrls,
#state{url_cache = OldCache} = S) ->
case ddfs_tag_util:validate_urls(Urls) of
true ->
NoDup = proplists:is_defined(nodup, Opt),
{Cache, Merged} = merge_urls(Urls,
OldUrls,
NoDup,
OldCache),
do_put(TokenInfo,
urls,
Merged,
ReplyTo,
S#state{url_cache = Cache});
false ->
_ = send_replies(ReplyTo, {error, invalid_url_object}),
S
end.
% Normal update: Just add new urls to the list
merge_urls(NewUrls, OldUrls, false, _Cache) ->
{false, NewUrls ++ OldUrls};
% Set update (nodup): Remove duplicates.
%
% Note that merge_urls should keep the original order of
% both NewUrls and OldUrls list. It should drop entries from
% NewUrls that are duplicates or already exist in OldUrls.
merge_urls(NewUrls, OldUrls, true, false) ->
merge_urls(NewUrls, OldUrls, true, init_url_cache(OldUrls));
merge_urls(NewUrls, OldUrls, true, Cache) ->
find_unseen(NewUrls, Cache, OldUrls).
find_unseen([], Seen, Urls) ->
{Seen, Urls};
find_unseen([[Url|_] = Repl|Rest], Seen, Urls) ->
Name = ddfs_util:url_to_name(Url),
case {Name, gb_sets:is_member(Name, Seen)} of
{false, _} ->
find_unseen(Rest, Seen, [Repl|Urls]);
{_, false} ->
find_unseen(Rest, gb_sets:add(Name, Seen), [Repl|Urls]);
{_, true} ->
find_unseen(Rest, Seen, Urls)
end.
init_url_cache(Urls) ->
gb_sets:from_list([ddfs_util:url_to_name(Url) || [Url|_] <- Urls]).
-spec send_replies(replyto() | [replyto()],
{error, commit_failed | invalid_attribute_value |
invalid_url_object | unknown_attribute |
replication_failed | unauthorized} |
{ok, [binary(),...]} | ok) -> [any()].
send_replies(ReplyTo, Message) when is_tuple(ReplyTo) ->
send_replies([ReplyTo], Message);
send_replies(ReplyToList, Message) ->
[gen_server:reply(Re, Message) || Re <- ReplyToList].
-spec do_gc_rr_update(state(), [blob_update()], [node()], tagid()) -> state().
do_gc_rr_update(#state{data = {missing, _}} = S, _Updates, _Blacklist, _Id) ->
% The tag has been deleted, or cannot be found; ignore the update.
S;
do_gc_rr_update(#state{data = {error, _}} = S, _Updates, _Blacklist, _Id) ->
% Error retrieving tag data; ignore the update.
S;
do_gc_rr_update(#state{tag = TagName, data = {ok, D} = Tag} = S,
Updates, Blacklist, UpdateId) ->
OldUrls = D#tagcontent.urls,
Id = D#tagcontent.id,
Map = gb_trees:from_orddict(lists:sort(Updates)),
NewUrls = gc_update_urls(Id, OldUrls, Map, Blacklist, UpdateId),
{ok, NewTagContent} =
ddfs_tag_util:update_tagcontent(TagName, urls, NewUrls, Tag, null),
NewTagData = ddfs_tag_util:encode_tagcontent(NewTagContent),
TagId = NewTagContent#tagcontent.id,
case put_distribute({TagId, NewTagData}) of
{ok, DestNodes, _} ->
S#state{data = {ok, NewTagContent},
replicas = DestNodes,
url_cache = init_url_cache(NewUrls)};
{error, _} = E ->
lager:error("GC: unable to update tag ~p: ~p", [TagName, E]),
S
end.
-spec gc_update_urls(tagid(), [[url()]], gb_tree(), [node()], tagid())
-> [[url()]].
gc_update_urls(Id, OldUrls, Map, Blacklist, UpdateId) ->
gc_update_urls(Id, OldUrls, Map, Blacklist, UpdateId, []).
gc_update_urls(_Id, [], _Map, _Blacklist, _UpdateId, Acc) ->
lists:reverse(Acc);
gc_update_urls(Id, [[] | Rest], Map, Blacklist, UpdateId, Acc) ->
gc_update_urls(Id, Rest, Map, Blacklist, UpdateId, Acc);
gc_update_urls(Id, [[Url|_] = BlobSet | Rest], Map, Blacklist, UpdateId, Acc) ->
case ddfs_util:parse_url(Url) of
not_ddfs ->
gc_update_urls(Id, Rest, Map, Blacklist, UpdateId, [BlobSet|Acc]);
{_, _, _, _, BlobName} ->
case gb_trees:lookup(BlobName, Map) of
none ->
gc_update_urls(Id, Rest, Map, Blacklist, UpdateId, [BlobSet|Acc]);
{value, filter}
when Id =:= UpdateId ->
% Only apply the blacklist filter provided we
% still retain at least the minimum number of
% replicas. This is just a sanity check; the real
% safety check is done by GC/RR.
Filtered = filter_blacklist(BlobSet, Blacklist),
case length(Filtered) >= get(blobk) of
true ->
gc_update_urls(Id, Rest, Map, Blacklist, UpdateId, [Filtered|Acc]);
false ->
gc_update_urls(Id, Rest, Map, Blacklist, UpdateId, [BlobSet|Acc])
end;
{value, filter} ->
% The tag was modified after the filter safety
% check was done; skip the update.
gc_update_urls(Id, Rest, Map, Blacklist, UpdateId, [BlobSet|Acc]);
{value, NewUrls_} ->
% Ensure that new locations use the same scheme as
% the existing locations.
NewUrls = rewrite_scheme(Url, NewUrls_),
NewBlobSet = lists:usort(NewUrls ++ BlobSet),
gc_update_urls(Id, Rest, Map, Blacklist, UpdateId, [NewBlobSet|Acc])
end
end.
-spec rewrite_scheme(url(), [url()]) -> [url()].
rewrite_scheme(OrigUrl, Urls) ->
{S, _, _, _, _} = mochiweb_util:urlsplit(binary_to_list(OrigUrl)),
[rewrite_url(U, S) || U <- Urls].
rewrite_url(Url, S) ->
U1 = binary_to_list(Url),
{_, H, P, Q, F} = mochiweb_util:urlsplit(U1),
NewUrl = mochiweb_util:urlunsplit({S, H, P, Q, F}),
list_to_binary(NewUrl).
-spec filter_blacklist([url()], [node()]) -> [url()].
filter_blacklist(BlobSet, Blacklist) ->
lists:foldl(
fun(Url, Acc) ->
case ddfs_util:parse_url(Url) of
not_ddfs -> [Url | Acc];
{Host, _V, _T, _H, _B} ->
case disco:slave_safe(Host) of
false -> [Url | Acc];
Node -> case lists:member(Node, Blacklist) of
true -> Acc;
false -> [Url|Acc]
end
end
end
end, [], BlobSet).
-spec get_tagdata(tagname()) -> {missing, notfound} | {error, _}
| {ok, binary(), [{replica(), node()}]}.
get_tagdata(TagName) ->
{ok, ReadableNodes, RBSize} = ddfs_master:get_read_nodes(),
TagMinK = get(min_tagk),
case RBSize >= TagMinK of
true ->
{error, too_many_failed_nodes};
false ->
{Replies, Failed} =
gen_server:multi_call(ReadableNodes,
ddfs_node,
{get_tag_timestamp, TagName},
?NODE_TIMEOUT),
case [{TagNfo, Node} || {Node, {ok, TagNfo}} <- Replies] of
_ when length(Failed) + RBSize >= TagMinK ->
{error, too_many_failed_nodes};
[] ->
{missing, notfound};
L ->
{{Time, _Vol}, _Node} = lists:max(L),
Replicas = [X || {{T, _}, _} = X <- L, T == Time],
TagID = ddfs_util:pack_objname(TagName, Time),
read_tagdata(TagID, Replicas, [], tagdata_failed)
end
end.
read_tagdata(_TagID, Replicas, Failed, Error)
when length(Replicas) =:= length(Failed) ->
{error, Error};
read_tagdata(TagID, Replicas, Failed, _Error) ->
{TagNfo, SrcNode} = Chosen = disco_util:choose_random(Replicas -- Failed),
GetData = try ddfs_node:get_tag_data(SrcNode, TagID, TagNfo)
catch K:V -> {error, {K,V}}
end,
case GetData of
{ok, Data} ->
{_, DestNodes} = lists:unzip(Replicas),
{ok, Data, DestNodes};
E ->
read_tagdata(TagID, Replicas, [Chosen|Failed], E)
end.
-spec do_delayed_update([[binary()]], [term()], replyto(),
gb_tree(), state()) -> state().
do_delayed_update(Urls, Opt, ReplyTo, Buffer, S) ->
% We must handle updates with different set of options separately.
% Thus requests are indexed by the normalized set of options (OptKey)
OptKey = lists:sort(proplists:compact(Opt)),
case ddfs_tag_util:validate_urls(Urls) of
true ->
NewBuffer =
case gb_trees:lookup(OptKey, Buffer) of
none ->
gb_trees:insert(OptKey, {[ReplyTo], Urls}, Buffer);
{value, {OldWaiters, OldUrls}} ->
NWaiters = [ReplyTo|OldWaiters],
NUrls = Urls ++ OldUrls,
gb_trees:enter(OptKey, {NWaiters, NUrls}, Buffer)
end,
S#state{delayed = NewBuffer};
false ->
gen_server:reply(ReplyTo, {error, invalid_url_object}),
S
end.
-spec jsonbin(_) -> binary().
jsonbin(X) ->
iolist_to_binary(mochijson2:encode(X)).
-spec do_get({tokentype(), token()}, attrib() | all, tagcontent()) ->
{ok, binary()} | {error, unauthorized | unknown_attribute}.
do_get(_TokenInfo, all, D) ->
{ok, ddfs_tag_util:encode_tagcontent_secure(D)};
do_get(_TokenInfo, urls, D) ->
{ok, jsonbin(D#tagcontent.urls)};
do_get(_TokenInfo, read_token, D) ->
{ok, jsonbin(D#tagcontent.read_token)};
do_get({read, _}, write_token, _D) ->
{error, unauthorized};
do_get({write, _}, write_token, D) ->
{ok, jsonbin(D#tagcontent.write_token)};
do_get(_TokenInfo, {user, A}, D) ->
case proplists:lookup(A, D#tagcontent.user) of
{_, V} -> {ok, jsonbin(V)};
_ -> {error, unknown_attribute}
end.
-spec do_put({tokentype(), token()}, attrib(), string(), replyto(), state())
-> state().
do_put({_, Token},
Field,
Value,
ReplyTo,
#state{tag = TagName, data = TagData} = S) ->
case ddfs_tag_util:update_tagcontent(TagName, Field, Value, TagData, Token) of
{ok, TagContent} ->
NewTagData = ddfs_tag_util:encode_tagcontent(TagContent),
TagID = TagContent#tagcontent.id,
case put_distribute({TagID, NewTagData}) of
{ok, DestNodes, DestUrls} ->
if TagData =:= {missing, deleted} ->
{ok, _} = remove_from_deleted(TagName),
ok;
true -> ok
end,
_ = send_replies(ReplyTo, {ok, DestUrls}),
S#state{data = {ok, TagContent}, replicas = DestNodes};
{error, _} = E ->
_ = send_replies(ReplyTo, E),
S#state{url_cache = false}
end;
{error, _} = E ->
_ = send_replies(ReplyTo, E),
S#state{url_cache = false}
end.
do_delete_attrib(Field, ReplyTo, #state{tag = TagName, data = {ok, D}} = S) ->
TagContent = ddfs_tag_util:delete_tagattrib(TagName, Field, D),
NewTagData = ddfs_tag_util:encode_tagcontent(TagContent),
TagId = TagContent#tagcontent.id,
case put_distribute({TagId, NewTagData}) of
{ok, DestNodes, _DestUrls} ->
_ = send_replies(ReplyTo, ok),
S#state{data = {ok, TagContent},
replicas = DestNodes};
{error, _} = E ->
_ = send_replies(ReplyTo, E),
S
end.
% Put transaction:
% 1. choose nodes
% 2. multicall nodes, send TagData -> receive temporary file names
% 3. if failures -> retry with other nodes
% 4. if all nodes fail before tagk replicas are written, fail
% 5. multicall with temp file names, rename
% 6. if all fail, fail
% 7. if at least one multicall succeeds, return updated tagdata, desturls
-spec put_distribute({tagid(),binary()}) ->
{error, commit_failed | replication_failed} | {ok, [node()], [binary(),...]}.
put_distribute({TagID, _} = Msg) ->
case put_distribute(Msg, get(tagk), [], []) of
{ok, TagVol} ->
put_commit(TagID, TagVol);
{error, _} = E ->
E
end.
-spec put_distribute({tagid(), binary()}, non_neg_integer(),
[{node(), binary()}], [node()]) ->
{error, replication_failed} | {ok, [{node(), binary()}]}.
put_distribute({TagID, TagData}, K, OkNodes, _Exclude) when K == length(OkNodes) ->
disco_aws:put(get(s3_bucket), binary_to_list(TagID), TagData),
{ok, OkNodes};
put_distribute({TagID, TagData} = Msg, K, OkNodes, Exclude) ->
case get(use_s3) of
true ->
disco_aws:put(get(s3_bucket), binary_to_list(TagID), TagData),
put_distribute_(Msg, K, OkNodes, Exclude);
false ->
put_distribute_(Msg, K, OkNodes, Exclude)
end.
-spec put_distribute_({tagid(), binary()}, non_neg_integer(),
[{node(), binary()}], [node()]) ->
{error, replication_failed} | {ok, [{node(), binary()}]}.
put_distribute_({TagID, TagData} = Msg, K, OkNodes, Exclude) ->
TagMinK = get(min_tagk),
K0 = K - length(OkNodes),
{ok, Nodes} = ddfs_master:choose_write_nodes(K0, Exclude),
if
Nodes =:= [], length(OkNodes) < TagMinK ->
{error, replication_failed};
Nodes =:= [] ->
{ok, OkNodes};
true ->
PutMsg = {TagID, TagData},
{Replies, Failed} = gen_server:multi_call(Nodes,
ddfs_node,
{put_tag_data, PutMsg},
?NODE_TIMEOUT),
put_distribute(Msg, K,
OkNodes ++ [{Node, VolName}
|| {Node, {ok, VolName}} <- Replies],
Exclude ++ [Node || {Node, _} <- Replies] ++ Failed)
end.
-spec put_commit(tagid(), [{node(), binary()}]) ->
{error, commit_failed} | {ok, [node()], [binary(), ...]}.
put_commit(TagID, TagVol) ->
{Nodes, _} = lists:unzip(TagVol),
{NodeUrls, _} = gen_server:multi_call(Nodes,
ddfs_node,
{put_tag_commit, TagID, TagVol},
?NODE_TIMEOUT),
case [Url || {_Node, {ok, Url}} <- NodeUrls] of
[] ->
{error, commit_failed};
Urls ->
{ok, Nodes, Urls}
end.
-spec do_delete(replyto(), state()) -> state().
do_delete(ReplyTo, #state{tag = Tag} = S) ->
case add_to_deleted(Tag) of
{ok, _} ->
gen_server:reply(ReplyTo, ok),
gen_server:cast(self(), {die, none});
E ->
gen_server:reply(ReplyTo, E)
end,
S.
-spec is_tag_deleted(tagname()) -> _.
is_tag_deleted(<<"+deleted">>) -> false;
is_tag_deleted(Tag) ->
deleted_op({has_tagname, Tag}, ?NODEOP_TIMEOUT).
-spec add_to_deleted(tagname()) -> _.
add_to_deleted(Tag) ->
Urls = [[<<"tag://", Tag/binary>>]],
deleted_op({update, Urls, internal, [nodup]}, ?TAG_UPDATE_TIMEOUT).
-spec remove_from_deleted(tagname()) -> _.
remove_from_deleted(Tag) ->
deleted_op({delete_tagname, Tag}, ?NODEOP_TIMEOUT).
deleted_op(Op, Timeout) ->
ddfs_master:tag_operation(Op, <<"+deleted">>, Timeout).