-
Notifications
You must be signed in to change notification settings - Fork 2
/
brick_ets.erl
4071 lines (3746 loc) · 174 KB
/
brick_ets.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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%%%-------------------------------------------------------------------
%%% Copyright (c) 2007-2013 Hibari developers. All rights reserved.
%%%
%%% Licensed 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.
%%%
%%% File : brick_ets.erl
%%% Purpose : ETS implementation of brick_server
%%%-------------------------------------------------------------------
%% @doc An ETS implementation of brick_server.
%%
%% This implementation does not do a very good separation of concerns
%% regarding truly-ETS-specific stuff and more general brick stuff.
%% So, this module may require quite a bit of refactoring.
%%
%% At the moment, this module takes care of the following:
%% <ul>
%% <li> ETS table management for the brick. </li>
%% <li> Enforcement of the Hibari-style, within-a-brick transaction logic. </li>
%% <li> Logging updates to disk for persistence purposes (including
%% sync updates similar to "group commit"). </li>
%% <li> Dirty key management: avoid accessing keys that are currently being
%% logged to disk. </li>
%% <li> Table checkpointing (though not fully implemented, e.g. automatic
%% checkpoints at prescribed times or when logs get too big or .... ) </li>
%% </ul>
%%
%% With review and inevitable dev growth, I expect several of these
%% tasks to move out of this module, probably back into
%% `brick_server.erl'. Also, a lot of this code is brute-force and
%% may need some beautification.
%%
-module(brick_ets).
-behaviour(gen_server).
-compile({inline_size,24}). % 24 is the default
-include("brick.hrl").
-include("brick_public.hrl").
-include("brick_hash.hrl").
-include("gmt_hlog.hrl").
-include_lib("kernel/include/file.hrl").
-define(CHECK_NAME, "CHECK").
-define(CHECK_FILE, ?CHECK_NAME ++ ".LOG").
-define(FOO_TIMEOUT, 10*000).
%% "production use"?
%% -define(BIGDIR_BITS1, 8).
%% -define(BIGDIR_BITS2, 4).
%% Debug use.
-define(BIGDIR_BITS1, 3).
-define(BIGDIR_BITS2, 3).
-define(BIGDIR_BITS1_MOD, (1 bsl ?BIGDIR_BITS1)).
-define(BIGDIR_BITS2_MOD, (1 bsl ?BIGDIR_BITS2)).
-export([debug_scan/1, debug_scan/2, debug_scan2/1, debug_scan2/2,
debug_scan3/2, debug_scan3/3, debug_scan4/2, debug_scan4/3]).
-export([verbose_expiration/2]).
%% External exports
-export([start_link/2, stop/1, dump_state/1, sync_stats/2, brick_name2data_dir/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
%% Brick callbacks
-export([bcb_force_flush/1, bcb_async_flush_log_serial/2,
bcb_log_mods/3, bcb_map_mods_to_storage/2,
bcb_get_many/3, bcb_get_list/2, bcb_repair_loop/3,
bcb_repair_diff_round1/3, bcb_repair_diff_round2/3,
bcb_delete_remaining_keys/2,
bcb_set_read_only_mode/2,
bcb_get_metadata/2, bcb_set_metadata/3, bcb_delete_metadata/2,
bcb_add_mods_to_dirty_tab/2, bcb_dirty_keys_in_range/3,
bcb_retry_dirty_key_ops/1,
bcb_lookup_key/3, bcb_lookup_key_storetuple/3,
bcb_start_time/1,
bcb_val_switcharoo/3, bcb_filter_mods_for_downstream/2,
bcb_make_log_replay_noop/1,
bcb_incr_logging_serial/1, bcb_peek_logging_serial/1,
bcb_peek_logging_op_q_len/1, bcb_get_logging_op_q/1,
bcb_status/1, bcb_common_log_sequence_file_is_bad/2]).
-export([disk_log_fold/3, disk_log_fold_bychunk/3]).
%% Internal exports
-export([sync_pid_loop/1, checkpoint_start/4]).
%% Debug/temp/benchmarking exports
-export([sync_pid_start/1]).
-export([file_input_fun/2, file_output_fun/1, sort_test0/0, slurp_log_chunks/1]).
%% For gmt_hlog_common's scavenger support.
-export([make_info_log_fun/1, really_cheap_exclusion/3, scavenger_get_keys/5,
count_live_bytes_in_log/1, temp_path_to_seqnum/1, which_path/3,
delete_seq/2, scavenge_one_seq_file_fun/4, copy_one_hunk/6]).
%% For brick_admin fast sync support
-export([storetuple_make/6]).
%% For gmt_hlog_common's use
-export([sequence_file_is_bad_common/6, append_external_bad_sequence_file/2,
delete_external_bad_sequence_file/1]).
%%%
%%% ETS Tables (Section 2.3.14.3 of Hibari Contributor's Guide)
%%%
%%% #state.ctab, the contents table:
%%% Except for changes made during a checkpoint, all data about a
%%% key lives in this table as a "store tuple".
%%%
%%% #state.dirty_tab, the dirty table:
%%% If a key has been updated but not yet flushed to disk, the key
%%% appears here. Necessary for any update, inside or outside of a
%%% micro-transaction, where race conditions are possible. See
%%% Section 2.3.14.6, "The dirty keys table".
%%%
%%% #state.etab, the expiry table:
%%% If a key has a non-zero expiry time associated with it (an
%%% integer in UNIX time_t form), then the expiry time appears in
%%% this table.
%%%
%%% #state.mdtab, the brick private metadata table:
%%% Used for private state management during data migration and
%%% other tasks.
%%%
%%% #state.shadowtab, the shadow table:
%%% During checkpoints, the contents table is frozen while the
%%% checkpoint process dumps its contents. All updates made while
%%% the checkpoint is running (insert or delete) are stored in this
%%% table. When the checkpoint is finished, the contents of this
%%% table are applied to the contents table, and then the shadow
%%% table is deleted.
%%%
%%% ctab format:
%%% {Key = binary()|term(), binary is preferred
%%% TStamp = bignum(), whatever brick_server:make_timestamp() makes
%%% Val = binary()|term(), binary is preferred
%%% }
%%% Flags and ExpTime are _not_ stored at this time.
%%%----------------------------------------------------------------------
%%% Types/Specs/Records
%%%----------------------------------------------------------------------
-export_type([extern_tuple/0, state_r/0]).
-type proplist() :: [ atom() | {any(),any()} ].
-record(state, {
name :: atom(), % My registered name
options :: proplist(),
start_time :: {non_neg_integer(),non_neg_integer(),non_neg_integer()},
do_logging = true :: boolean(),
do_sync = true :: boolean(),
log_dir :: string(),
bigdata_dir :: string(),
n_add = 0 :: non_neg_integer(),
n_replace = 0 :: non_neg_integer(),
n_set = 0 :: non_neg_integer(),
n_rename = 0 :: non_neg_integer(),
n_get = 0 :: non_neg_integer(),
n_get_many = 0 :: non_neg_integer(),
n_delete = 0 :: non_neg_integer(),
n_txn = 0 :: non_neg_integer(),
n_do = 0 :: non_neg_integer(),
n_expired = 0 :: non_neg_integer(),
%% CHAIN TODO: Do these 2 items really belong here??
logging_op_serial = 42 :: non_neg_integer(), % Serial # of logging op
logging_op_q = queue:new() :: queue(), % Queue of logged ops for
% replay when log sync is done.
%% n_log_replay = 0,
log :: pid(), % disk_log for data mod cmds
thisdo_mods :: [tuple()], % List of mods by this 'do'
check_pid :: pid(), % Pid of checkpoint helper
check_lastseqnum :: seqnum(), % integer() seq # of last checkpoint
ctab :: table_name(), % ETS table for cache
shadowtab :: table_name(), % Checkpoint shadow for ctab
shadowtab_name :: table_name(), % atom(), name shortcut only
bypass_shadowtab = false :: boolean(), % Flag for fold_shadow_into_ctab()
etab :: table_name(), % Expiry index table
mdtab :: table_name(), % Private metadata table
sync_pid :: pid(), % Pid of log sync process
checkpoint_timer :: reference(), % Timer for checkpoint
checkpoint_opts :: proplist(), % Proplist for checkpoint
dirty_tab :: table_name(), % ETS table: dirty key search
wait_on_dirty_q :: queue(), % Queue of ops waiting on
% dirty keys
read_only_p = false :: boolean(), % Mirror of upper-level's var.
expiry_tref :: reference(), % Timer ref for expiry events
scavenger_pid :: 'undefined' | pid(), % undefined | pid()
scavenger_tref :: reference(), % tref()
syncsum_count = 0 :: non_neg_integer(), % DEBUG?? syncsum stats
syncsum_msec = 0 :: non_neg_integer(),
syncsum_len = 0 :: integer(),
syncsum_tref :: reference(),
do_expiry_fun :: fun(), % fun
wal_mod :: atom(), % atom()
max_log_size = 0 :: non_neg_integer() % max log size
}).
-record(syncpid_arg, {
parent_pid :: pid(), % Pid of syncpid's parent brick
wal_pid :: pid(), % Pid of wal server.
wal_mod :: atom(),
name :: atom() % Name of brick
}).
-include("brick_specs.hrl").
-include("gmt_hlog.hrl").
-type log_fold_fun() :: fun((tuple(),tuple()) -> tuple()).
-type server_ref() :: atom() | {atom(), atom()} | {global, term()} | pid().
-type state_r() :: tuple().
-spec append_external_bad_sequence_file(atom(),integer()) -> ok.
-spec brick_name2data_dir(atom()) -> nonempty_string().
-spec copy_one_hunk(tuple(), term(), integer(), integer(), integer(), fun()) -> {{integer(), integer()}, integer()} | error.
-spec count_live_bytes_in_log(file:fd()) -> integer().
-spec delete_seq(#scav{},integer()) -> ok.
-spec disk_log_fold(log_fold_fun(),tuple()|integer(),term()) -> tuple()|integer().
-spec disk_log_fold_bychunk(fun(([tuple()],integer())->integer()),integer(),term()) -> integer().
-spec file_input_fun(term(),term()) -> fun((atom())->end_of_input|{error,term()}).
-spec file_output_fun(term()) -> fun((term()) -> ok | fun()).
-spec make_info_log_fun(list()) -> fun((_,_) -> 'ok').
-spec really_cheap_exclusion(atom(),fun(),fun()) -> fun().
-spec scavenger_get_keys(brick_server:brick_name(),brick_server:flags_list_many(),
key(),
function(), function()) -> ok.
-spec sequence_file_is_bad_common(nonempty_string(), atom(), pid(), atom(),integer(),integer()) -> ok.
-spec storetuple_make(key(), ts(), term(), integer(), exp_time(), flags_list()) -> store_tuple().
-spec temp_path_to_seqnum(nonempty_string()) -> integer().
-spec which_path(_,atom() | tuple(),_) -> {_,atom() | [any()]}.
-spec dump_state(server_ref() | atom()) -> {integer(), list(), state_r()}.
%% #scav moved to brick.hrl
%%%----------------------------------------------------------------------
%%% API
%%%----------------------------------------------------------------------
start_link(ServerName, Options) ->
gen_server:start_link(?MODULE, [ServerName, Options], []).
stop(Server) ->
gen_server:call(Server, {stop}).
dump_state(Server) ->
gen_server:call(Server, {dump_state}).
sync_stats(Server, Seconds) when seconds >= 0 ->
gen_server:call(Server, {sync_stats, Seconds}).
brick_name2data_dir(ServerName) ->
{ok, FileDir} = application:get_env(gdss_brick, brick_default_data_dir),
filename:join([FileDir, "hlog." ++ atom_to_list(ServerName)]).
%%%----------------------------------------------------------------------
%%% Callback functions from gen_server
%%%----------------------------------------------------------------------
%%----------------------------------------------------------------------
%% Func: init/1
%% Returns: {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%%----------------------------------------------------------------------
init([ServerName, Options]) ->
?DBG_GENx({qqq_starting, ServerName}),
process_flag(trap_exit, true),
?E_INFO("top of init: ~p, ~p", [ServerName, Options]),
%% Avoid race conditions where a quick restart of the brick
%% catches up with the not-yet-finished death of the previous log
%% proc.
%% io:format("USING gmt_hlog, old-school...."), timer:sleep(1000),
%% WalMod = proplists:get_value(wal_log_module, Options, gmt_hlog),
WalMod = proplists:get_value(wal_log_module, Options, gmt_hlog_local),
catch exit(whereis(WalMod:log_name2reg_name(ServerName)), kill),
DoLogging = proplists:get_value(do_logging, Options, true),
DoSync = proplists:get_value(do_sync, Options, true),
MaxLogSize =
case proplists:get_value(max_log_size, Options) of
undefined ->
{ok, Max} = application:get_env(gdss_brick, brick_max_log_size_mb),
Max * 1024 * 1024;
Max ->
Max * 1024 * 1024
end,
MinLogSize =
case proplists:get_value(min_log_size, Options) of
undefined ->
{ok, Min} = application:get_env(gdss_brick, brick_min_log_size_mb),
Min * 1024 * 1024;
Min ->
Min * 1024 * 1024
end,
BigDataDir = proplists:get_value(bigdata_dir, Options),
TabName = list_to_atom(atom_to_list(ServerName) ++ "_store"),
CTab = ets:new(TabName, [ordered_set, protected, named_table]),
ETabName = list_to_atom(atom_to_list(ServerName) ++ "_exp"),
ETab = ets:new(ETabName, [ordered_set, protected, named_table]),
MDTabName = list_to_atom(atom_to_list(ServerName) ++ "_md"),
MDTab = ets:new(MDTabName, [ordered_set, protected, named_table]),
ShadowTabName = list_to_atom(atom_to_list(ServerName) ++ "_shadow"),
{ok, LogPid} = WalMod:start_link([{name, ServerName},
{file_len_max, MaxLogSize},
{file_len_min, MinLogSize}]),
DirtyTabName = list_to_atom(atom_to_list(ServerName) ++ "_dirty"),
DirtyTab = ets:new(DirtyTabName, [ordered_set, protected, named_table]),
WaitOnDirty = queue:new(),
SyncPidArg = #syncpid_arg{parent_pid = self(), wal_pid = LogPid,
wal_mod = WalMod, name = ServerName},
SyncPid = spawn_link(fun() -> sync_pid_start(SyncPidArg) end),
LogOpQ = queue:new(),
DefaultExpFun = fun delete_keys_immediately/2,
DoExpFun =
case application:get_env(gdss_brick, brick_expiration_processor) of
{ok, "["++_ = ExpConfig} ->
case brick_server:get_tabprop_via_str(ServerName, ExpConfig) of
undefined -> DefaultExpFun;
XF -> XF
end;
_ ->
DefaultExpFun
end,
%% Brick's name vs. that brick's log's name:
%% brick name: X
%% log's directory name: hlog.X
%% log's registered name: X_hlog
LogDir = WalMod:log_name2data_dir(ServerName),
State0 = #state{name = ServerName, options = Options, start_time = now(),
do_logging = DoLogging, do_sync = DoSync,
log_dir = LogDir, bigdata_dir = BigDataDir,
log = LogPid, ctab = CTab, etab = ETab, mdtab = MDTab,
shadowtab = undefined, shadowtab_name = ShadowTabName,
sync_pid = SyncPid,
dirty_tab = DirtyTab, wait_on_dirty_q = WaitOnDirty,
logging_op_q = LogOpQ, do_expiry_fun = DoExpFun,
wal_mod = WalMod,
max_log_size = MaxLogSize - 32 % TODO: remove fudge factor!
},
self() ! do_init_second_half,
{ok, State0}.
%%----------------------------------------------------------------------
%% Func: handle_call/3
%% Returns: {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} | (terminate/2 is called)
%% {stop, Reason, State} (terminate/2 is called)
%%----------------------------------------------------------------------
handle_call({do, _SentAt, L, DoFlags} = DoOp, From, State) ->
%% This func is getting too-deeply indented ... but I really want all
%% this stuff easy-to-see in a single func body.
?DBG_OPx(DoOp),
case do_do(L, DoFlags, DoOp, From, State#state{thisdo_mods = []}, check_dirty) of
has_dirty_keys ->
?DBG_OPx({?MODULE, do, State#state.name, has_dirty_keys, DoOp}),
DQI = #dirty_q{from = From, do_op = DoOp},
DirtyQ = State#state.wait_on_dirty_q,
{noreply, State#state{wait_on_dirty_q = queue:in(DQI, DirtyQ)}};
{no_dirty_keys, {Reply, NewState}} ->
%% #state.thisdo_mods will be in reverse order of DoOpList!
Thisdo_Mods = lists:reverse(NewState#state.thisdo_mods),
if NewState#state.read_only_p, Thisdo_Mods /= [] ->
?DBG_OPx({?MODULE, do, State#state.name, read_only_mode, DoOp}),
{up1_read_only_mode, From, DoOp, NewState};
true ->
SyncOverride = proplists:get_value(sync_override, DoFlags),
case log_mods(NewState, SyncOverride) of
{goahead, NewState3} ->
?DBG_TLOGx({?MODULE, do, State#state.name, goahead, DoOp}),
ok = map_mods_into_ets(Thisdo_Mods,
NewState3),
?DBG_OPx({State#state.name, send_downstream}),
LoggingSerial =
NewState3#state.logging_op_serial,
ToDos = [{chain_send_downstream, LoggingSerial,
DoFlags, From, Reply, Thisdo_Mods}],
?DBG_TLOGx({?MODULE, do, State#state.name, todos_going_up, ToDos}),
N_do = NewState3#state.n_do,
%% !@#$!%! Grrr.....
%% If we're a head brick, we can increment
%% LoggingSerial all we want. However, we
%% don't have carte blanche if we're a
%% tail brick. If we're a tail,
%% incrementing this serial creates very
%% weird, bad situations with out-of-order
%% sync'ing. So, only increment the
%% serial if we've actually got mods.
NL = if Thisdo_Mods == [] -> LoggingSerial;
true -> LoggingSerial + 1
end,
case proplists:get_value(
local_op_only_do_not_forward, DoFlags) of
true ->
{reply, Reply, NewState3};
_ ->
{up1, ToDos,
{noreply, NewState3#state{
n_do = N_do + 1,
logging_op_serial = NL}}}
end;
{wait, NewState3} ->
?DBG_TLOGx({?MODULE, do, State#state.name, wait, DoOp}),
add_mods_to_dirty_tab(Thisdo_Mods,
NewState3),
LoggingSerial =
NewState3#state.logging_op_serial,
SyncMsg = {sync_msg, LoggingSerial},
NewState3#state.sync_pid ! SyncMsg,
LQI = #log_q{logging_serial = LoggingSerial,
thisdo_mods = Thisdo_Mods,
doflags = DoFlags,
from = From, reply = Reply},
NewQ = queue:in(LQI,
NewState3#state.logging_op_q),
N_do = NewState3#state.n_do,
%% If we had to wait for logging, then
%% we're waiting because there's something
%% we actually *are* logging. So
%% increasing the serial here is good.
NL = LoggingSerial + 1,
NewState4 =
NewState3#state{n_do = N_do + 1,
logging_op_serial = NL,
logging_op_q = NewQ},
{noreply, NewState4}
end
end;
{noreply_now, NewState} ->
{noreply, NewState};
silently_drop_reply ->
%% Somewhere deep in the bowels of the do list processing,
%% we hit some bad data. We can't form a good answer, so
%% don't reply at all.
{noreply, State}
end;
handle_call({status}, _From, State) ->
Reply = do_status(State),
{reply, Reply, State};
handle_call({state}, _From, State) ->
{reply, State, State};
handle_call({flush_all}, _From, State) ->
{Reply, NewState} = do_flush_all(State),
{reply, Reply, NewState};
handle_call({checkpoint, Options}, _From, State) ->
{Reply, NewState} = do_checkpoint(State, Options),
{reply, Reply, NewState};
handle_call({dump_state}, _From, State) ->
L = ets:tab2list(State#state.ctab),
{reply, {length(L), L, State}, State};
handle_call({sync_stats, Seconds}, _From, State) ->
catch brick_itimer:cancel(State#state.syncsum_tref),
{ok, TRef} = if Seconds > 0 -> brick_itimer:send_interval(Seconds*1000,
log_sync_stats);
true -> {ok, undefined}
end,
{reply, Seconds, State#state{syncsum_count = 0, syncsum_msec = 0,
syncsum_len = 0, syncsum_tref = TRef}};
handle_call({set_do_sync, NewValue}, _From, State) ->
{reply, State#state.do_sync, State#state{do_sync = NewValue}};
handle_call({set_do_logging, NewValue}, _From, State) ->
{reply, State#state.do_logging, State#state{do_logging = NewValue}};
handle_call(Request, _From, State) ->
?E_ERROR("~s handle_call: Request = ~P", [?MODULE, Request, 20]),
Reply = err,
{reply, Reply, State}.
%%----------------------------------------------------------------------
%% Func: handle_cast/2
%% Returns: {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State} (terminate/2 is called)
%%----------------------------------------------------------------------
handle_cast({checkpoint_last_seqnum, SeqNum}, State) ->
{noreply, State#state{check_lastseqnum = SeqNum}};
handle_cast({incr_expired, Amount}, #state{n_expired = N} = State) ->
{noreply, State#state{n_expired = N + Amount}};
handle_cast(Msg, State) ->
?E_ERROR("~s handle_cast: ~p: Msg = ~P",
[?MODULE, State#state.name, Msg, 20]),
exit({handle_cast, Msg}), % QQQ TODO debugging only
{noreply, State}.
%%----------------------------------------------------------------------
%% Func: handle_info/2
%% Returns: {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State} (terminate/2 is called)
%%----------------------------------------------------------------------
handle_info({sync_done, Pid, LastLogSerial}, State)
when Pid == State#state.sync_pid ->
?DBG_TLOGx({sync_done, State#state.name, lastlogserial, LastLogSerial}),
?DBG_TLOGx({sync_done, State#state.name, logging_op_serial, State#state.logging_op_serial}),
?DBG_TLOGx({sync_done, State#state.name, logging_op_q, State#state.logging_op_q}),
{LQI_List, State2} =
pull_items_out_of_logging_queue(LastLogSerial, State),
_ = [ok = map_mods_into_ets(DoOpList, State2) ||
#log_q{thisdo_mods = DoOpList} <- LQI_List],
_ = [ok = clear_dirty_tab(DoOpList, State2) ||
#log_q{thisdo_mods = DoOpList} <- LQI_List],
ToDos = [{chain_send_downstream, LQI#log_q.logging_serial,
LQI#log_q.doflags, LQI#log_q.from, LQI#log_q.reply,
LQI#log_q.thisdo_mods}
|| LQI <- LQI_List],
%% CHAIN TODO: ISSUE001: Does the ToDos list (above) contain mods
%% for the dirty keys? Or is the attempt below to
%% resubmit the dirty keys truly independent??
%% put(issue001, ToDos), ... looks like they're independent, whew!
{State3, ToDos2} = retry_dirty_key_ops(State2),
?DBG_TLOGx({sync_done_dirty_q, State#state.name, State#state.wait_on_dirty_q}),
{up1_sync_done, LastLogSerial, ToDos ++ ToDos2, State3};
handle_info(check_expiry, State) ->
case whereis(brick_server:make_expiry_regname(State#state.name)) of
undefined ->
{noreply, do_check_expiry(State)};
_ ->
{noreply, State}
end;
handle_info(check_checkpoint, State) ->
{noreply, do_check_checkpoint(State)};
handle_info({'EXIT', Pid, Reason}, State) when Pid == State#state.check_pid ->
case Reason of
done ->
ok;
{{?MODULE,_,_}, {line, _}, done} -> % smart exceptions too helpful!
ok;
_ ->
?E_WARNING("checkpoint: pid ~p died with ~p", [Pid, Reason])
end,
NewState = fold_shadow_into_ctab(State),
{noreply, NewState#state{check_pid = undefined}};
handle_info({'EXIT', Pid, Reason}, State) when Pid == State#state.sync_pid ->
?ELOG_ERROR("~s: sync pid ~p died ~p",
[State#state.name, Pid, Reason]),
{stop, normal, State};
handle_info({'EXIT', Pid, Done}, State)
when Pid == State#state.scavenger_pid andalso
(Done == done orelse element(3, Done) == done) -> % smart_exceptions
{noreply, State#state{scavenger_pid = undefined}};
handle_info({'EXIT', Pid, Reason}, State)
when Pid == State#state.scavenger_pid ->
?E_WARNING("QQQ: ~p: scavenger ~p exited with ~p",
[State#state.name, State#state.scavenger_pid, Reason]),
{noreply, State#state{scavenger_pid = undefined}};
handle_info({'EXIT', Pid, Reason}, State) when Pid == State#state.log ->
?E_WARNING("~p: log process ~p exited with ~p",
[State#state.name, State#state.log, Reason]),
{stop, Reason, State};
handle_info(qqq_debugging_only, S) ->
?DBG_GENx({debug, S#state.name, logging_op_serial, S#state.logging_op_serial}),
?DBG_GENx({debug, S#state.name, logging_op_q, S#state.logging_op_q}),
?DBG_GENx({debug, S#state.name, wait_on_dirty_q, S#state.wait_on_dirty_q}),
?DBG_GENx({debug, S#state.name, dirty_tab, ets:tab2list(S#state.dirty_tab)}),
{noreply, S};
handle_info({syncpid_stats, _Name, MSec, ms, Length}, State) ->
{noreply, State#state{syncsum_count = State#state.syncsum_count + 1,
syncsum_msec = State#state.syncsum_msec + MSec,
syncsum_len = State#state.syncsum_len + Length}};
handle_info(log_sync_stats, State) ->
AvgTime = if State#state.syncsum_count == 0 -> 0;
true -> State#state.syncsum_msec / State#state.syncsum_count
end,
AvgLen = if State#state.syncsum_count == 0 -> 0;
true -> State#state.syncsum_len / State#state.syncsum_count
end,
?E_INFO("sync summary: ~p: ~p syncs ~p msec avg ~p len avg",
[State#state.name, State#state.syncsum_count, AvgTime, AvgLen]),
{noreply, State#state{syncsum_count = 0, syncsum_msec = 0,
syncsum_len = 0}};
handle_info(do_init_second_half, State) ->
?E_INFO("do_init_second_half: ~p", [State#state.name]),
ok = gmt_hlog_common:full_writeback(),
MinimumSeqNum = read_checkpoint_num(State#state.log_dir),
{_LTODO_x, ErrList} = wal_scan_all(State, MinimumSeqNum),
?DBG_GEN("log ~p _LTODO_x = ~p ErrList = ~p",
[State#state.wal_mod, _LTODO_x, ErrList]),
if ErrList == [] ->
ok;
true ->
wal_scan_failed(ErrList, State)
end,
ZeroDiskErrorsP = (ErrList == []),
%% If the commonlog has told us that there are checksum errors,
%% purge all records used by those bad log sequence files.
%%
%% TODO: It's quite possible that most of the file is not corrupt.
%% We're throwing out some of the baby with the bath water when
%% we discard all keys that refer to the bad file(s). If we're
%% part of a chain with length > 1, things are still OK.
%% But if we're in standalone mode, then there's no one to repair
%% the missing keys, which could make us regret deleting keys
%% as many keys as we are doing here.
_ = [begin {Purged, _S} = purge_recs_by_seqnum(SeqNum, true, State),
?E_INFO("~s: purged ~p keys from sequence ~p",
[State#state.name, Purged, SeqNum]),
Purged
end || SeqNum <- read_external_bad_sequence_file(State#state.name)],
%% For recently bigdata_dir files, sync them all in a big safety blatt.
%% ... Except that it also seems to have the potential to slam the
%% ... disk system really, really hard, which can (in turn) create
%% ... extremely bad timeout conditions. {sigh} So, for now,
%% ... comment out the sync.
%% os:cmd("sync"),
%% Set these timers only after the WAL scan is finished.
{ok, CheckTimer} = brick_itimer:send_interval(30*1000, check_checkpoint),
brick_itimer:send_interval(1*1000, qqq_debugging_only),
{ok, ExpiryTRef} = brick_itimer:send_interval(1*1000, check_expiry),
?E_INFO("do_init_second_half: ~p finished", [State#state.name]),
self() ! {storage_layer_init_finished, State#state.name, ZeroDiskErrorsP},
{noreply, State#state{checkpoint_timer = CheckTimer,
expiry_tref = ExpiryTRef,
scavenger_tref = undefined,
check_lastseqnum = MinimumSeqNum}};
handle_info(_Info, State) ->
?E_ERROR("Hey: ~s handle_info: Info = ~P", [?MODULE, _Info, 20]),
{noreply, State}.
%%----------------------------------------------------------------------
%% Func: terminate/2
%% Purpose: Shutdown the server
%% Returns: any (ignored by gen_server)
%%----------------------------------------------------------------------
terminate(Reason, State) ->
?DBG_GENx({qqq_stopping, State#state.name, Reason}),
?DBG_GEN("Hey: terminate ~p: reason = ~p", [State#state.name, Reason]),
catch ets:delete(State#state.ctab),
catch ets:delete(State#state.etab),
catch ets:delete(State#state.shadowtab),
_X = (catch (State#state.wal_mod):stop(State#state.log)),
ok.
%%----------------------------------------------------------------------
%% Func: code_change/3
%% Purpose: Convert process state when code is changed
%% Returns: {ok, NewState}
%%----------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%%----------------------------------------------------------------------
%%% Internal functions
%%%----------------------------------------------------------------------
do_status(S) ->
{ok, [
{start_time, S#state.start_time},
{options, S#state.options},
{ets, [{A, ets:info(S#state.ctab, A)} || A <- [size, memory]]},
{ets_table_name, S#state.ctab},
{do_logging, S#state.do_logging},
{do_sync, S#state.do_sync},
{n_add, S#state.n_add},
{n_replace, S#state.n_replace},
{n_set, S#state.n_set},
{n_rename, S#state.n_rename},
{n_get, S#state.n_get},
{n_get_many, S#state.n_get_many},
{n_delete, S#state.n_delete},
{n_txn, S#state.n_txn},
{n_do, S#state.n_do},
{n_expired, S#state.n_expired},
{checkpoint, S#state.check_pid},
{wait_on_dirty_q, S#state.wait_on_dirty_q}
]}.
do_flush_all(State) ->
?DBG_GEN("Deleting all table data!", []),
ets:delete_all_objects(State#state.ctab),
ets:delete_all_objects(State#state.etab),
catch ets:delete_all_objects(State#state.shadowtab),
do_checkpoint(State, []).
%% @spec (list(), list(), do_op(), from_spec(), state_r(), check_dirty | term()) ->
%% has_dirty_keys | {no_dirty_keys, {reply_term(), new_state_r()}} |
%% {reply_now, term(), new_state_r()} | {noreply_now, new_state_r()}
%% @doc Do a 'do', phase 1: Check to see if all keys are local to this brick.
do_do(L, DoFlags, DoOp, From, State, CheckDirty) ->
%% CHAIN TODO: I think the not_calculated is probably not a good
%% idea, since chain processing has already harvested
%% the keys. However ... chain processing is *supposed*
%% to be orthogonal & independent. :-)
try
do_do1b(not_calculated, L, DoFlags, DoOp, From, State, CheckDirty)
catch
throw:silently_drop_reply -> % Instead, catch 1 caller higher? {shrug}
silently_drop_reply
end.
%% @doc Do a 'do', phase 1b: Check for any dirty keys.
do_do1b(DoKeys0, L, DoFlags, DoOp, From, State, check_dirty)
when State#state.do_logging == true ->
DoKeys = if DoKeys0 == not_calculated -> brick_server:harvest_do_keys(L);
true -> DoKeys0
end,
case any_dirty_keys_p(DoKeys, State) of
true ->
has_dirty_keys;
false ->
if State#state.bigdata_dir == undefined ->
%% QQQ TODO Is it a good idea to stash DoKeys
%% inside State at this point, or do it someplace
%% else? Earlier? Later? But later would mean
%% changing arity on do_do2() and more? Well, it
%% turns out that we don't need to keep DoKeys and
%% the read/write info it stores ... we can use
%% instead: if Thisdo_Mods == [], then all ops
%% were read-only.
{no_dirty_keys, do_do2(L, DoFlags, State)};
true ->
ReadP = any_readonly_keys_p(DoKeys),
Resub = proplists:get_value(squidflash_resubmit, DoFlags),
if ReadP == true andalso Resub == undefined ->
case squidflash_primer(DoOp, From, State) of
goahead ->
{no_dirty_keys, do_do2(L, DoFlags, State)};
_Else ->
{noreply_now, State}
end;
true ->
{no_dirty_keys, do_do2(L, DoFlags, State)}
end
end
end;
do_do1b(_DoKeys, L, DoFlags, _DoOp, _From, State, _) ->
%% No logging, so no worries about ops on dirty keys.
{no_dirty_keys, do_do2(L, DoFlags, State)}.
%% @doc Do a 'do', phase 2: If a 'txn' is present, check txn preconditions,
%% then (in all cases) perform the do's.
do_do2([txn|L], DoFlags, State) ->
%% do_txnlist checks to see if a transaction's conditions are
%% OK. If all are OK, then do_txnlist() will call do_dolist()
%% to apply the changes.
do_txnlist(L, DoFlags, State);
do_do2(L, DoFlags, State) ->
do_dolist(L, State, DoFlags, []).
%%% TODO Good candidate for replacement with lists:map() or fold*()?
do_dolist([], State, _DoFlags, Acc) ->
{lists:reverse(Acc), State};
do_dolist([{add, Key, TStamp, Value, ExpTime, Flags}|T], State, DoFlags, Acc) ->
if TStamp =:= 0 -> EffTStamp = brick_server:make_timestamp(); true -> EffTStamp = TStamp end,
{Res, NewState} = add_key(Key, EffTStamp, Value, ExpTime, Flags, State),
N_add = NewState#state.n_add,
do_dolist(T, NewState#state{n_add = N_add + 1}, DoFlags, [Res|Acc]);
do_dolist([{replace, Key, TStamp, Value, ExpTime, Flags}|T], State, DoFlags, Acc) ->
if TStamp =:= 0 -> EffTStamp = brick_server:make_timestamp(); true -> EffTStamp = TStamp end,
{Res, NewState} = replace_key(Key, EffTStamp, Value, ExpTime, Flags, State),
N_replace = NewState#state.n_replace,
do_dolist(T, NewState#state{n_replace = N_replace + 1}, DoFlags, [Res|Acc]);
do_dolist([{set, Key, TStamp, Value, ExpTime, Flags}|T], State, DoFlags, Acc) ->
if TStamp =:= 0 -> EffTStamp = brick_server:make_timestamp(); true -> EffTStamp = TStamp end,
{Res, NewState} = set_key(Key, EffTStamp, Value, ExpTime, Flags, State),
N_set = NewState#state.n_set,
do_dolist(T, NewState#state{n_set = N_set + 1}, DoFlags, [Res|Acc]);
do_dolist([{rename, Key, TStamp, NewKey, ExpTime, Flags}|T], State, DoFlags, Acc) ->
if TStamp =:= 0 -> EffTStamp = brick_server:make_timestamp(); true -> EffTStamp = TStamp end,
{Res, NewState} = rename_key(Key, EffTStamp, NewKey, ExpTime, Flags, State),
N_rename = NewState#state.n_rename,
do_dolist(T, NewState#state{n_rename = N_rename + 1}, DoFlags, [Res|Acc]);
do_dolist([{get, Key, Flags}|T], State, DoFlags, Acc) ->
Res = get_key(Key, Flags, State),
N_get = State#state.n_get,
do_dolist(T, State#state{n_get = N_get + 1}, DoFlags, [Res|Acc]);
do_dolist([{delete, Key, Flags}|T], State, DoFlags, Acc) ->
{Res, NewState} = delete_key(Key, Flags, State),
N_delete = NewState#state.n_delete,
do_dolist(T, NewState#state{n_delete = N_delete + 1}, DoFlags, [Res|Acc]);
do_dolist([{get_many, Key, Flags}|T], State, DoFlags, Acc) ->
{Res0, NewState} = get_many1(Key, Flags, true, DoFlags, State),
Res = {ok, Res0},
N_get_many = NewState#state.n_get_many,
do_dolist(T, NewState#state{n_get_many = N_get_many + 1}, DoFlags, [Res|Acc]);
do_dolist([next_op_is_silent, DoOp|T], State, DoFlags, Acc) ->
{_ResList, NewState} = do_dolist([DoOp], State, DoFlags, []),
do_dolist(T, NewState, DoFlags, Acc);
do_dolist([Unknown|T], State, DoFlags, Acc) ->
%% Unknown is an error code from a do_txnlist() step, or perhaps
%% some other return/status code ... pass it on.
do_dolist(T, State, DoFlags, [Unknown|Acc]).
do_txnlist(L, DoFlags, State) ->
%% It's a bit clumsy to have a separate accumulator for errors
%% (arg 6), but I guess we'll live with it.
do_txnlist(L, State, [], true, 1, DoFlags, []).
do_txnlist([], State, Acc, true, _N, DoFlags, []) ->
N_txn = State#state.n_txn,
{_, Reply} = do_do(lists:reverse(Acc), DoFlags,
doop_not_used, from_not_used,
State#state{n_txn = N_txn + 1}, false),
Reply;
do_txnlist([], State, _Acc, false, _, _DoFlags, ErrAcc) ->
{{txn_fail, lists:reverse(ErrAcc)}, State};
do_txnlist([{add, Key, TStamp, _Value, _ExpTime, Flags} = H|T], State,
Acc, Good, N, DoFlags, ErrAcc) ->
if TStamp =:= 0 ->
EffTStamp = brick_server:make_timestamp(),
EffH = setelement(3, H, EffTStamp);
true ->
EffH = H
end,
case key_exists_p(Key, Flags, State) of
{Key, TS, _, _, _, _} ->
Err = {key_exists, TS},
do_txnlist(T, State, [Err|Acc], false, N+1, DoFlags, [{N, Err}|ErrAcc]);
{ts_error, _} = Err ->
do_txnlist(T, State, [Err|Acc], false, N+1, DoFlags, [{N, Err}|ErrAcc]);
_ ->
do_txnlist(T, State, [EffH|Acc], Good, N+1, DoFlags, ErrAcc)
end;
do_txnlist([{replace, Key, TStamp, Value, _ExpTime, Flags} = H|T], State,
Acc, Good, N, DoFlags, ErrAcc) ->
if TStamp =:= 0 ->
EffTStamp = brick_server:make_timestamp(),
EffH = setelement(3, H, EffTStamp);
true ->
EffTStamp = TStamp,
EffH = H
end,
case key_exists_p(Key, Flags, State, true, EffTStamp, Value) of
{Key, _, _, _, _, _} ->
do_txnlist(T, State, [EffH|Acc], Good, N+1, DoFlags, ErrAcc);
{ts_error, _} = Err ->
do_txnlist(T, State, [Err|Acc], false, N+1, DoFlags, [{N, Err}|ErrAcc]);
_ ->
Err = key_not_exist,
do_txnlist(T, State, [Err|Acc], false, N+1, DoFlags, [{N, Err}|ErrAcc])
end;
do_txnlist([{set, Key, TStamp, Value, _ExpTime, Flags} = H|T], State,
Acc, Good, N, DoFlags, ErrAcc) ->
if TStamp =:= 0 ->
EffTStamp = brick_server:make_timestamp(),
EffH = setelement(3, H, EffTStamp);
true ->
EffTStamp = TStamp,
EffH = H
end,
Num = lists:foldl(fun(Flag, Sum) -> case check_flaglist(Flag, Flags) of
{true, _} -> Sum + 1;
_ -> Sum
end
end, 0, [must_exist, must_not_exist]),
if Num == 0 ->
case key_exists_p(Key, Flags, State, false, EffTStamp, Value) of
{ts_error, _} = Err ->
do_txnlist(T, State, [Err|Acc], false, N+1, DoFlags, [{N, Err}|ErrAcc]);
_ ->
do_txnlist(T, State, [EffH|Acc], Good, N+1, DoFlags, ErrAcc)
end;
true ->
Err = invalid_flag_present,
do_txnlist(T, State, [Err|Acc], false, N+1, DoFlags, [{N, Err}|ErrAcc])
end;
do_txnlist([{rename, Key, TStamp, NewKey, _ExpTime, Flags} = H|T], State,
Acc, Good, N, DoFlags, ErrAcc) ->
if TStamp =:= 0 ->
EffTStamp = brick_server:make_timestamp(),
EffH = setelement(3, H, EffTStamp);
true ->
EffTStamp = TStamp,
EffH = H
end,
case key_exists_p(Key, Flags, State, true, EffTStamp, NewKey) of
{NewKey, _, _, _, _, _} ->
%% special case when Key =:= NewKey
Err = key_not_exist,
do_txnlist(T, State, [Err|Acc], false, N+1, DoFlags, [{N, Err}|ErrAcc]);
{Key, _, _, _, _, _} ->
do_txnlist(T, State, [EffH|Acc], Good, N+1, DoFlags, ErrAcc);
{ts_error, _} = Err ->
do_txnlist(T, State, [Err|Acc], false, N+1, DoFlags, [{N, Err}|ErrAcc]);
_ ->
Err = key_not_exist,
do_txnlist(T, State, [Err|Acc], false, N+1, DoFlags, [{N, Err}|ErrAcc])
end;
do_txnlist([{Primitive, Key, Flags} = H|T], State,
Acc, Good, N, DoFlags, ErrAcc)
when Primitive == get ; Primitive == delete ; Primitive == get_many ->
case common_testset_must_mustnot(Key, Flags, State) of
ok ->
do_txnlist(T, State, [H|Acc], Good, N+1, DoFlags, ErrAcc);
{error, Err} ->
do_txnlist(T, State, [Err|Acc], false, N+1, DoFlags, [{N, Err}|ErrAcc])
end;
do_txnlist([next_op_is_silent = H|T], State, Acc, Good, N, DoFlags, ErrAcc) ->
do_txnlist(T, State, [H|Acc], Good, N+1, DoFlags, ErrAcc);
do_txnlist([Unknown|T], State, Acc, Good, N, DoFlags, ErrAcc) ->
%% Unknown is an error code from a do_txnlist() step, or perhaps
%% some other return/status code ... pass it on.
do_txnlist(T, State, [Unknown|Acc], Good, N+1, DoFlags, ErrAcc).
common_testset_must_mustnot(Key, Flags, S) ->
case check_flaglist(testset, Flags) of
{true, _} ->
%% If someone specified must_not_exist and {testset, TS},
%% then they're silly, because testset only makes sense if
%% the record *does* exist.
case key_exists_p(Key, Flags, S) of
{Key, _, _, _, _, _} ->
ok;
{ts_error, _} = Err ->
{error, Err};
_ ->
{error, key_not_exist}
end;
_ ->
WantedExist = case check_flaglist(must_exist, Flags) of
{true, _} ->
true;
_ ->
case check_flaglist(must_not_exist, Flags) of
{true, _} -> false;
_ -> dont_care
end
end,
{ActualExist, TS} = case key_exists_p(Key, Flags, S) of
{Key, TSx, _, _, _, _} -> {true, TSx};
_ -> {false, unused}
end,
if WantedExist == dont_care ; WantedExist == ActualExist ->
ok;
WantedExist == true, ActualExist == false ->
{error, key_not_exist};
WantedExist == false, ActualExist == true ->
{error, {key_exists, TS}};
true ->
{error, invalid_flag_present}
end
end.
-spec apply_exp_time_directive(flags_list(), exp_time(), exp_time()) ->
{ok, exp_time()} | {error, invalid_flag_present}.
apply_exp_time_directive(Flags, ExpTime, PreviousExpTime) ->
case check_flaglist(exp_time_directive, Flags) of
false ->
{ok, PreviousExpTime};
{true, keep} ->
{ok, PreviousExpTime};
{true, replace} ->
{ok, ExpTime};
{true, _} ->
{error, invalid_flag_present}
end.
-spec apply_attrib_directive(flags_list(), flags_list()) ->
{ok, flags_list()} | {error, invalid_flag_present}.
apply_attrib_directive(Flags, PreviousFlags) ->
case check_flaglist(attrib_directive, Flags) of
false ->
apply_attrib_directive1(Flags, PreviousFlags);
{true, keep} ->
apply_attrib_directive1(Flags, PreviousFlags);
{true, replace} ->
{ok, Flags};
{true, _} ->
{error, invalid_flag_present}
end.
-spec apply_attrib_directive1(flags_list(), flags_list()) -> {ok, flags_list()}.
apply_attrib_directive1([], PreviousFlags) ->
{ok, PreviousFlags};
apply_attrib_directive1(Flags, []) ->
{ok, Flags};
apply_attrib_directive1(Flags, PreviousFlags) ->
F = fun({Key, _}=Flag, Acc) ->