2016-10-25 01:57:12 +01:00
|
|
|
%% -------- Inker's Clerk ---------
|
|
|
|
%%
|
|
|
|
%% The Inker's clerk runs compaction jobs on behalf of the Inker, informing the
|
|
|
|
%% Inker of any manifest changes when complete.
|
|
|
|
%%
|
|
|
|
%% -------- Value Compaction ---------
|
|
|
|
%%
|
|
|
|
%% Compaction requires the Inker to have four different types of keys
|
|
|
|
%% * stnd - A standard key of the form {SQN, stnd, LedgerKey} which maps to a
|
|
|
|
%% value of {Object, KeyDeltas}
|
|
|
|
%% * tomb - A tombstone for a LedgerKey {SQN, tomb, LedgerKey}
|
|
|
|
%% * keyd - An object containing key deltas only of the form
|
|
|
|
%% {SQN, keyd, LedgerKey} which maps to a value of {KeyDeltas}
|
|
|
|
%%
|
|
|
|
%% Each LedgerKey has a Tag, and for each Tag there should be a compaction
|
|
|
|
%% strategy, which will be set to one of the following:
|
|
|
|
%% * retain - KeyDeltas must be retained permanently, only values can be
|
|
|
|
%% compacted (if replaced or not_present in the ledger)
|
|
|
|
%% * recalc - The full object can be removed through comapction (if replaced or
|
|
|
|
%% not_present in the ledger), as each object with that tag can have the Key
|
|
|
|
%% Deltas recreated by passing into an assigned recalc function {LedgerKey,
|
|
|
|
%% SQN, Object, KeyDeltas, PencillerSnapshot}
|
|
|
|
%% * recovr - At compaction time this is equivalent to recalc, only KeyDeltas
|
|
|
|
%% are lost when reloading the Ledger from the Journal, and it is assumed that
|
|
|
|
%% those deltas will be resolved through external anti-entropy (e.g. read
|
|
|
|
%% repair or AAE) - or alternatively the risk of loss of persisted data from
|
|
|
|
%% the ledger is accepted for this data type
|
|
|
|
%%
|
|
|
|
%% During the compaction process for the Journal, the file chosen for
|
|
|
|
%% compaction is scanned in SQN order, and a FilterFun is passed (which will
|
|
|
|
%% normally perform a check against a snapshot of the persisted part of the
|
|
|
|
%% Ledger). If the given key is of type stnd, and this object is no longer the
|
|
|
|
%% active object under the LedgerKey, then the object can be compacted out of
|
|
|
|
%% the journal. This will lead to either its removal (if the strategy for the
|
|
|
|
%% Tag is recovr or recalc), or its replacement with a KeyDelta object.
|
|
|
|
%%
|
|
|
|
%% Tombstones cannot be reaped through this compaction process.
|
|
|
|
%%
|
|
|
|
%% Currently, KeyDeltas are also reaped if the LedgerKey has been updated and
|
|
|
|
%% the Tag has a recovr strategy. This may be the case when KeyDeltas are used
|
|
|
|
%% as a way of directly representing a change, and where anti-entropy can
|
|
|
|
%% recover from a loss.
|
|
|
|
%%
|
2016-11-14 11:17:14 +00:00
|
|
|
%% -------- Removing Compacted Files ---------
|
|
|
|
%%
|
|
|
|
%% Once a compaction job is complete, and the manifest change has been
|
|
|
|
%% committed, the individual journal files will get a deletion prompt. The
|
|
|
|
%% Journal processes should copy the file to the waste folder, before erasing
|
|
|
|
%% themselves.
|
|
|
|
%%
|
|
|
|
%% The Inker will have a waste duration setting, and before running compaction
|
|
|
|
%% should delete all over-age items (using the file modified date) from the
|
|
|
|
%% waste.
|
|
|
|
%%
|
2016-10-25 01:57:12 +01:00
|
|
|
%% -------- Tombstone Reaping ---------
|
|
|
|
%%
|
|
|
|
%% Value compaction does not remove tombstones from the database, and so a
|
|
|
|
%% separate compaction job is required for this.
|
|
|
|
%%
|
|
|
|
%% Tombstones can only be reaped for Tags set to recovr or recalc.
|
|
|
|
%%
|
|
|
|
%% The tombstone reaping process should select a file to compact, and then
|
|
|
|
%% take that file and discover the LedgerKeys of all reapable tombstones.
|
|
|
|
%% The lesger should then be scanned from SQN 0 looking for unreaped objects
|
|
|
|
%% before the tombstone. If no ushc objects exist for that tombstone, it can
|
|
|
|
%% now be reaped as part of the compaction job.
|
|
|
|
%%
|
2016-11-14 11:17:14 +00:00
|
|
|
%% Other tombstones cannot be reaped, as otherwise on laoding a ledger an old
|
2016-10-25 01:57:12 +01:00
|
|
|
%% version of the object may re-emerge.
|
2016-09-20 16:13:36 +01:00
|
|
|
|
|
|
|
-module(leveled_iclerk).
|
|
|
|
|
|
|
|
-behaviour(gen_server).
|
|
|
|
|
2016-10-18 01:59:03 +01:00
|
|
|
-include("include/leveled.hrl").
|
2016-09-20 16:13:36 +01:00
|
|
|
|
|
|
|
-export([init/1,
|
|
|
|
handle_call/3,
|
|
|
|
handle_cast/2,
|
|
|
|
handle_info/2,
|
|
|
|
terminate/2,
|
|
|
|
clerk_new/1,
|
2016-09-28 18:26:52 +01:00
|
|
|
clerk_compact/6,
|
2016-10-14 13:36:12 +01:00
|
|
|
clerk_hashtablecalc/3,
|
2016-09-20 16:13:36 +01:00
|
|
|
clerk_stop/1,
|
|
|
|
code_change/3]).
|
|
|
|
|
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
|
2016-09-27 14:58:26 +01:00
|
|
|
-define(JOURNAL_FILEX, "cdb").
|
|
|
|
-define(PENDING_FILEX, "pnd").
|
2016-09-26 10:55:08 +01:00
|
|
|
-define(SAMPLE_SIZE, 200).
|
2016-10-26 11:50:59 +01:00
|
|
|
-define(BATCH_SIZE, 32).
|
2016-09-21 18:31:42 +01:00
|
|
|
-define(BATCHES_TO_CHECK, 8).
|
2016-09-27 14:58:26 +01:00
|
|
|
%% How many consecutive files to compact in one run
|
|
|
|
-define(MAX_COMPACTION_RUN, 4).
|
|
|
|
%% Sliding scale to allow preference of longer runs up to maximum
|
|
|
|
-define(SINGLEFILE_COMPACTION_TARGET, 60.0).
|
|
|
|
-define(MAXRUN_COMPACTION_TARGET, 80.0).
|
2016-10-08 22:15:48 +01:00
|
|
|
-define(CRC_SIZE, 4).
|
2016-10-25 23:13:14 +01:00
|
|
|
-define(DEFAULT_RELOAD_STRATEGY, leveled_codec:inker_reload_strategy([])).
|
2016-11-14 11:17:14 +00:00
|
|
|
-define(DEFAULT_WASTE_RETENTION_PERIOD, 86400).
|
2016-09-27 14:58:26 +01:00
|
|
|
|
|
|
|
-record(state, {inker :: pid(),
|
|
|
|
max_run_length :: integer(),
|
2016-10-25 23:13:14 +01:00
|
|
|
cdb_options,
|
2016-11-14 11:17:14 +00:00
|
|
|
waste_retention_period :: integer(),
|
|
|
|
waste_path :: string(),
|
2016-10-25 23:13:14 +01:00
|
|
|
reload_strategy = ?DEFAULT_RELOAD_STRATEGY :: list()}).
|
2016-09-27 14:58:26 +01:00
|
|
|
|
|
|
|
-record(candidate, {low_sqn :: integer(),
|
|
|
|
filename :: string(),
|
|
|
|
journal :: pid(),
|
|
|
|
compaction_perc :: float()}).
|
2016-09-20 16:13:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
%%%============================================================================
|
|
|
|
%%% API
|
|
|
|
%%%============================================================================
|
|
|
|
|
2016-09-27 14:58:26 +01:00
|
|
|
clerk_new(InkerClerkOpts) ->
|
|
|
|
gen_server:start(?MODULE, [InkerClerkOpts], []).
|
2016-09-20 16:13:36 +01:00
|
|
|
|
2016-09-28 18:26:52 +01:00
|
|
|
clerk_compact(Pid, Checker, InitiateFun, FilterFun, Inker, Timeout) ->
|
|
|
|
gen_server:cast(Pid,
|
|
|
|
{compact,
|
|
|
|
Checker,
|
|
|
|
InitiateFun,
|
|
|
|
FilterFun,
|
|
|
|
Inker,
|
|
|
|
Timeout}).
|
2016-09-26 10:55:08 +01:00
|
|
|
|
2016-10-14 13:36:12 +01:00
|
|
|
clerk_hashtablecalc(HashTree, StartPos, CDBpid) ->
|
|
|
|
{ok, Clerk} = gen_server:start(?MODULE, [#iclerk_options{}], []),
|
|
|
|
gen_server:cast(Clerk, {hashtable_calc, HashTree, StartPos, CDBpid}).
|
|
|
|
|
2016-09-20 16:13:36 +01:00
|
|
|
clerk_stop(Pid) ->
|
|
|
|
gen_server:cast(Pid, stop).
|
|
|
|
|
|
|
|
%%%============================================================================
|
|
|
|
%%% gen_server callbacks
|
|
|
|
%%%============================================================================
|
|
|
|
|
2016-09-27 14:58:26 +01:00
|
|
|
init([IClerkOpts]) ->
|
2016-10-25 23:13:14 +01:00
|
|
|
ReloadStrategy = IClerkOpts#iclerk_options.reload_strategy,
|
2016-11-14 11:17:14 +00:00
|
|
|
CDBopts = IClerkOpts#iclerk_options.cdb_options,
|
|
|
|
WP = CDBopts#cdb_options.waste_path,
|
|
|
|
WRP = case IClerkOpts#iclerk_options.waste_retention_period of
|
|
|
|
undefined ->
|
|
|
|
?DEFAULT_WASTE_RETENTION_PERIOD;
|
|
|
|
WRP0 ->
|
|
|
|
WRP0
|
|
|
|
end,
|
|
|
|
MRL = case IClerkOpts#iclerk_options.max_run_length of
|
|
|
|
undefined ->
|
|
|
|
?MAX_COMPACTION_RUN;
|
|
|
|
MRL0 ->
|
|
|
|
MRL0
|
|
|
|
end,
|
|
|
|
|
|
|
|
{ok, #state{max_run_length = MRL,
|
2016-09-27 14:58:26 +01:00
|
|
|
inker = IClerkOpts#iclerk_options.inker,
|
2016-11-14 11:17:14 +00:00
|
|
|
cdb_options = CDBopts,
|
|
|
|
reload_strategy = ReloadStrategy,
|
|
|
|
waste_path = WP,
|
|
|
|
waste_retention_period = WRP}}.
|
2016-09-20 16:13:36 +01:00
|
|
|
|
2016-09-27 14:58:26 +01:00
|
|
|
handle_call(_Msg, _From, State) ->
|
2016-10-03 23:34:28 +01:00
|
|
|
{reply, not_supported, State}.
|
2016-09-20 16:13:36 +01:00
|
|
|
|
2016-09-28 18:26:52 +01:00
|
|
|
handle_cast({compact, Checker, InitiateFun, FilterFun, Inker, _Timeout},
|
|
|
|
State) ->
|
2016-11-14 11:17:14 +00:00
|
|
|
% Empty the waste folder
|
|
|
|
clear_waste(State),
|
2016-09-27 14:58:26 +01:00
|
|
|
% Need to fetch manifest at start rather than have it be passed in
|
|
|
|
% Don't want to process a queued call waiting on an old manifest
|
2016-11-01 00:46:14 +00:00
|
|
|
[_Active|Manifest] = leveled_inker:ink_getmanifest(Inker),
|
2016-09-27 14:58:26 +01:00
|
|
|
MaxRunLength = State#state.max_run_length,
|
2016-10-05 18:28:31 +01:00
|
|
|
{FilterServer, MaxSQN} = InitiateFun(Checker),
|
2016-09-28 11:41:56 +01:00
|
|
|
CDBopts = State#state.cdb_options,
|
|
|
|
|
2016-10-05 18:28:31 +01:00
|
|
|
Candidates = scan_all_files(Manifest, FilterFun, FilterServer, MaxSQN),
|
2016-10-27 00:57:19 +01:00
|
|
|
BestRun0 = assess_candidates(Candidates, MaxRunLength),
|
|
|
|
case score_run(BestRun0, MaxRunLength) of
|
2016-10-30 22:06:44 +00:00
|
|
|
Score when Score > 0.0 ->
|
2016-10-27 00:57:19 +01:00
|
|
|
BestRun1 = sort_run(BestRun0),
|
|
|
|
print_compaction_run(BestRun1, MaxRunLength),
|
2016-11-14 11:40:02 +00:00
|
|
|
ManifestSlice = compact_files(BestRun1,
|
|
|
|
CDBopts,
|
|
|
|
FilterFun,
|
|
|
|
FilterServer,
|
|
|
|
MaxSQN,
|
|
|
|
State#state.reload_strategy),
|
2016-09-27 14:58:26 +01:00
|
|
|
FilesToDelete = lists:map(fun(C) ->
|
|
|
|
{C#candidate.low_sqn,
|
|
|
|
C#candidate.filename,
|
|
|
|
C#candidate.journal}
|
|
|
|
end,
|
2016-10-27 00:57:19 +01:00
|
|
|
BestRun1),
|
2016-11-03 16:05:43 +00:00
|
|
|
leveled_log:log("IC002", [length(FilesToDelete)]),
|
|
|
|
case is_process_alive(Inker) of
|
2016-10-03 23:34:28 +01:00
|
|
|
true ->
|
2016-11-03 16:05:43 +00:00
|
|
|
update_inker(Inker,
|
|
|
|
ManifestSlice,
|
2016-11-14 11:40:02 +00:00
|
|
|
FilesToDelete),
|
2016-10-03 23:34:28 +01:00
|
|
|
{noreply, State};
|
|
|
|
false ->
|
2016-11-03 16:05:43 +00:00
|
|
|
leveled_log:log("IC001", []),
|
|
|
|
{stop, normal, State}
|
2016-10-03 23:34:28 +01:00
|
|
|
end;
|
2016-09-27 14:58:26 +01:00
|
|
|
Score ->
|
2016-11-03 16:05:43 +00:00
|
|
|
leveled_log:log("IC003", [Score]),
|
2016-10-03 23:34:28 +01:00
|
|
|
ok = leveled_inker:ink_compactioncomplete(Inker),
|
2016-09-27 14:58:26 +01:00
|
|
|
{noreply, State}
|
|
|
|
end;
|
2016-10-14 13:36:12 +01:00
|
|
|
handle_cast({hashtable_calc, HashTree, StartPos, CDBpid}, State) ->
|
|
|
|
{IndexList, HashTreeBin} = leveled_cdb:hashtable_calc(HashTree, StartPos),
|
|
|
|
ok = leveled_cdb:cdb_returnhashtable(CDBpid, IndexList, HashTreeBin),
|
|
|
|
{stop, normal, State};
|
2016-09-20 16:13:36 +01:00
|
|
|
handle_cast(stop, State) ->
|
|
|
|
{stop, normal, State}.
|
|
|
|
|
|
|
|
handle_info(_Info, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
terminate(_Reason, _State) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
|
|
|
|
%%%============================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%============================================================================
|
|
|
|
|
|
|
|
|
2016-10-05 18:28:31 +01:00
|
|
|
check_single_file(CDB, FilterFun, FilterServer, MaxSQN, SampleSize, BatchSize) ->
|
|
|
|
FN = leveled_cdb:cdb_filename(CDB),
|
2016-09-21 18:31:42 +01:00
|
|
|
PositionList = leveled_cdb:cdb_getpositions(CDB, SampleSize),
|
|
|
|
KeySizeList = fetch_inbatches(PositionList, BatchSize, CDB, []),
|
2016-09-26 10:55:08 +01:00
|
|
|
R0 = lists:foldl(fun(KS, {ActSize, RplSize}) ->
|
2016-10-25 01:57:12 +01:00
|
|
|
{{SQN, _Type, PK}, Size} = KS,
|
2016-09-27 14:58:26 +01:00
|
|
|
Check = FilterFun(FilterServer, PK, SQN),
|
2016-10-05 18:28:31 +01:00
|
|
|
case {Check, SQN > MaxSQN} of
|
|
|
|
{true, _} ->
|
2016-10-08 22:15:48 +01:00
|
|
|
{ActSize + Size - ?CRC_SIZE, RplSize};
|
2016-10-05 18:28:31 +01:00
|
|
|
{false, true} ->
|
2016-10-08 22:15:48 +01:00
|
|
|
{ActSize + Size - ?CRC_SIZE, RplSize};
|
2016-10-05 18:28:31 +01:00
|
|
|
_ ->
|
2016-10-08 22:15:48 +01:00
|
|
|
{ActSize, RplSize + Size - ?CRC_SIZE}
|
2016-09-26 10:55:08 +01:00
|
|
|
end end,
|
|
|
|
{0, 0},
|
|
|
|
KeySizeList),
|
|
|
|
{ActiveSize, ReplacedSize} = R0,
|
2016-10-07 18:07:03 +01:00
|
|
|
Score = case ActiveSize + ReplacedSize of
|
|
|
|
0 ->
|
|
|
|
100.0;
|
|
|
|
_ ->
|
|
|
|
100 * ActiveSize / (ActiveSize + ReplacedSize)
|
|
|
|
end,
|
2016-11-03 16:05:43 +00:00
|
|
|
leveled_log:log("IC004", [FN, Score]),
|
2016-10-05 18:28:31 +01:00
|
|
|
Score.
|
2016-09-26 10:55:08 +01:00
|
|
|
|
2016-10-05 18:28:31 +01:00
|
|
|
scan_all_files(Manifest, FilterFun, FilterServer, MaxSQN) ->
|
|
|
|
scan_all_files(Manifest, FilterFun, FilterServer, MaxSQN, []).
|
2016-09-26 10:55:08 +01:00
|
|
|
|
2016-10-05 18:28:31 +01:00
|
|
|
scan_all_files([], _FilterFun, _FilterServer, _MaxSQN, CandidateList) ->
|
2016-09-26 10:55:08 +01:00
|
|
|
CandidateList;
|
2016-10-05 18:28:31 +01:00
|
|
|
scan_all_files([Entry|Tail], FilterFun, FilterServer, MaxSQN, CandidateList) ->
|
2016-09-27 14:58:26 +01:00
|
|
|
{LowSQN, FN, JournalP} = Entry,
|
|
|
|
CpctPerc = check_single_file(JournalP,
|
|
|
|
FilterFun,
|
|
|
|
FilterServer,
|
2016-10-05 18:28:31 +01:00
|
|
|
MaxSQN,
|
2016-09-27 14:58:26 +01:00
|
|
|
?SAMPLE_SIZE,
|
|
|
|
?BATCH_SIZE),
|
|
|
|
scan_all_files(Tail,
|
|
|
|
FilterFun,
|
|
|
|
FilterServer,
|
2016-10-05 18:28:31 +01:00
|
|
|
MaxSQN,
|
2016-09-27 14:58:26 +01:00
|
|
|
CandidateList ++
|
|
|
|
[#candidate{low_sqn = LowSQN,
|
|
|
|
filename = FN,
|
|
|
|
journal = JournalP,
|
|
|
|
compaction_perc = CpctPerc}]).
|
2016-09-26 10:55:08 +01:00
|
|
|
|
2016-09-21 18:31:42 +01:00
|
|
|
fetch_inbatches([], _BatchSize, _CDB, CheckedList) ->
|
|
|
|
CheckedList;
|
|
|
|
fetch_inbatches(PositionList, BatchSize, CDB, CheckedList) ->
|
2016-09-27 14:58:26 +01:00
|
|
|
{Batch, Tail} = if
|
|
|
|
length(PositionList) >= BatchSize ->
|
|
|
|
lists:split(BatchSize, PositionList);
|
|
|
|
true ->
|
|
|
|
{PositionList, []}
|
|
|
|
end,
|
|
|
|
KL_List = leveled_cdb:cdb_directfetch(CDB, Batch, key_size),
|
2016-09-21 18:31:42 +01:00
|
|
|
fetch_inbatches(Tail, BatchSize, CDB, CheckedList ++ KL_List).
|
|
|
|
|
2016-09-27 14:58:26 +01:00
|
|
|
assess_candidates(AllCandidates, MaxRunLength) ->
|
|
|
|
NaiveBestRun = assess_candidates(AllCandidates, MaxRunLength, [], []),
|
|
|
|
case length(AllCandidates) of
|
|
|
|
L when L > MaxRunLength, MaxRunLength > 1 ->
|
|
|
|
%% Assess with different offsets from the start
|
|
|
|
SqL = lists:seq(1, MaxRunLength - 1),
|
|
|
|
lists:foldl(fun(Counter, BestRun) ->
|
|
|
|
SubList = lists:nthtail(Counter,
|
|
|
|
AllCandidates),
|
|
|
|
assess_candidates(SubList,
|
|
|
|
MaxRunLength,
|
|
|
|
[],
|
|
|
|
BestRun)
|
|
|
|
end,
|
|
|
|
NaiveBestRun,
|
|
|
|
SqL);
|
|
|
|
_ ->
|
|
|
|
NaiveBestRun
|
|
|
|
end.
|
|
|
|
|
|
|
|
assess_candidates([], _MaxRunLength, _CurrentRun0, BestAssessment) ->
|
|
|
|
BestAssessment;
|
|
|
|
assess_candidates([HeadC|Tail], MaxRunLength, CurrentRun0, BestAssessment) ->
|
|
|
|
CurrentRun1 = choose_best_assessment(CurrentRun0 ++ [HeadC],
|
|
|
|
[HeadC],
|
|
|
|
MaxRunLength),
|
|
|
|
assess_candidates(Tail,
|
|
|
|
MaxRunLength,
|
|
|
|
CurrentRun1,
|
|
|
|
choose_best_assessment(CurrentRun1,
|
|
|
|
BestAssessment,
|
|
|
|
MaxRunLength)).
|
|
|
|
|
|
|
|
|
|
|
|
choose_best_assessment(RunToAssess, BestRun, MaxRunLength) ->
|
|
|
|
case length(RunToAssess) of
|
|
|
|
LR1 when LR1 > MaxRunLength ->
|
|
|
|
BestRun;
|
|
|
|
_ ->
|
|
|
|
AssessScore = score_run(RunToAssess, MaxRunLength),
|
|
|
|
BestScore = score_run(BestRun, MaxRunLength),
|
|
|
|
if
|
|
|
|
AssessScore > BestScore ->
|
|
|
|
RunToAssess;
|
|
|
|
true ->
|
|
|
|
BestRun
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
score_run([], _MaxRunLength) ->
|
|
|
|
0.0;
|
|
|
|
score_run(Run, MaxRunLength) ->
|
|
|
|
TargetIncr = case MaxRunLength of
|
|
|
|
1 ->
|
|
|
|
0.0;
|
|
|
|
MaxRunSize ->
|
|
|
|
(?MAXRUN_COMPACTION_TARGET
|
|
|
|
- ?SINGLEFILE_COMPACTION_TARGET)
|
|
|
|
/ (MaxRunSize - 1)
|
|
|
|
end,
|
|
|
|
Target = ?SINGLEFILE_COMPACTION_TARGET + TargetIncr * (length(Run) - 1),
|
|
|
|
RunTotal = lists:foldl(fun(Cand, Acc) ->
|
|
|
|
Acc + Cand#candidate.compaction_perc end,
|
|
|
|
0.0,
|
|
|
|
Run),
|
|
|
|
Target - RunTotal / length(Run).
|
|
|
|
|
2016-09-26 10:55:08 +01:00
|
|
|
|
2016-09-27 14:58:26 +01:00
|
|
|
print_compaction_run(BestRun, MaxRunLength) ->
|
2016-11-03 16:05:43 +00:00
|
|
|
leveled_log:log("IC005", [length(BestRun),
|
|
|
|
score_run(BestRun, MaxRunLength)]),
|
2016-09-27 14:58:26 +01:00
|
|
|
lists:foreach(fun(File) ->
|
2016-11-03 16:05:43 +00:00
|
|
|
leveled_log:log("IC006", [File#candidate.filename])
|
2016-09-27 14:58:26 +01:00
|
|
|
end,
|
|
|
|
BestRun).
|
2016-09-26 10:55:08 +01:00
|
|
|
|
2016-10-27 00:57:19 +01:00
|
|
|
sort_run(RunOfFiles) ->
|
|
|
|
CompareFun = fun(Cand1, Cand2) ->
|
|
|
|
Cand1#candidate.low_sqn =< Cand2#candidate.low_sqn end,
|
|
|
|
lists:sort(CompareFun, RunOfFiles).
|
|
|
|
|
2016-11-14 11:40:02 +00:00
|
|
|
update_inker(Inker, ManifestSlice, FilesToDelete) ->
|
2016-11-03 16:05:43 +00:00
|
|
|
{ok, ManSQN} = leveled_inker:ink_updatemanifest(Inker,
|
|
|
|
ManifestSlice,
|
|
|
|
FilesToDelete),
|
|
|
|
ok = leveled_inker:ink_compactioncomplete(Inker),
|
|
|
|
leveled_log:log("IC007", []),
|
2016-11-14 11:40:02 +00:00
|
|
|
lists:foreach(fun({_SQN, _FN, J2D}) ->
|
|
|
|
leveled_cdb:cdb_deletepending(J2D,
|
|
|
|
ManSQN,
|
|
|
|
Inker)
|
|
|
|
end,
|
|
|
|
FilesToDelete),
|
|
|
|
ok.
|
2016-10-27 00:57:19 +01:00
|
|
|
|
2016-10-25 23:13:14 +01:00
|
|
|
compact_files(BestRun, CDBopts, FilterFun, FilterServer, MaxSQN, RStrategy) ->
|
2016-09-27 14:58:26 +01:00
|
|
|
BatchesOfPositions = get_all_positions(BestRun, []),
|
|
|
|
compact_files(BatchesOfPositions,
|
|
|
|
CDBopts,
|
|
|
|
null,
|
|
|
|
FilterFun,
|
|
|
|
FilterServer,
|
2016-10-05 18:28:31 +01:00
|
|
|
MaxSQN,
|
2016-10-25 23:13:14 +01:00
|
|
|
RStrategy,
|
2016-11-14 11:40:02 +00:00
|
|
|
[]).
|
2016-09-20 16:13:36 +01:00
|
|
|
|
2016-09-28 18:26:52 +01:00
|
|
|
|
2016-10-05 18:28:31 +01:00
|
|
|
compact_files([], _CDBopts, null, _FilterFun, _FilterServer, _MaxSQN,
|
2016-11-14 11:40:02 +00:00
|
|
|
_RStrategy, ManSlice0) ->
|
|
|
|
ManSlice0;
|
2016-10-05 18:28:31 +01:00
|
|
|
compact_files([], _CDBopts, ActiveJournal0, _FilterFun, _FilterServer, _MaxSQN,
|
2016-11-14 11:40:02 +00:00
|
|
|
_RStrategy, ManSlice0) ->
|
2016-09-28 11:41:56 +01:00
|
|
|
ManSlice1 = ManSlice0 ++ generate_manifest_entry(ActiveJournal0),
|
2016-11-14 11:40:02 +00:00
|
|
|
ManSlice1;
|
2016-10-05 18:28:31 +01:00
|
|
|
compact_files([Batch|T], CDBopts, ActiveJournal0,
|
|
|
|
FilterFun, FilterServer, MaxSQN,
|
2016-11-14 11:40:02 +00:00
|
|
|
RStrategy, ManSlice0) ->
|
2016-09-27 14:58:26 +01:00
|
|
|
{SrcJournal, PositionList} = Batch,
|
|
|
|
KVCs0 = leveled_cdb:cdb_directfetch(SrcJournal,
|
|
|
|
PositionList,
|
|
|
|
key_value_check),
|
2016-11-14 11:40:02 +00:00
|
|
|
KVCs1 = filter_output(KVCs0,
|
|
|
|
FilterFun,
|
|
|
|
FilterServer,
|
|
|
|
MaxSQN,
|
|
|
|
RStrategy),
|
2016-09-27 14:58:26 +01:00
|
|
|
{ActiveJournal1, ManSlice1} = write_values(KVCs1,
|
|
|
|
CDBopts,
|
|
|
|
ActiveJournal0,
|
|
|
|
ManSlice0),
|
2016-10-05 18:28:31 +01:00
|
|
|
compact_files(T, CDBopts, ActiveJournal1, FilterFun, FilterServer, MaxSQN,
|
2016-11-14 11:40:02 +00:00
|
|
|
RStrategy, ManSlice1).
|
2016-09-27 14:58:26 +01:00
|
|
|
|
|
|
|
get_all_positions([], PositionBatches) ->
|
|
|
|
PositionBatches;
|
|
|
|
get_all_positions([HeadRef|RestOfBest], PositionBatches) ->
|
|
|
|
SrcJournal = HeadRef#candidate.journal,
|
|
|
|
Positions = leveled_cdb:cdb_getpositions(SrcJournal, all),
|
2016-11-03 16:05:43 +00:00
|
|
|
leveled_log:log("IC008", [HeadRef#candidate.filename, length(Positions)]),
|
2016-09-28 11:41:56 +01:00
|
|
|
Batches = split_positions_into_batches(lists:sort(Positions),
|
|
|
|
SrcJournal,
|
|
|
|
[]),
|
2016-09-27 14:58:26 +01:00
|
|
|
get_all_positions(RestOfBest, PositionBatches ++ Batches).
|
|
|
|
|
|
|
|
split_positions_into_batches([], _Journal, Batches) ->
|
|
|
|
Batches;
|
|
|
|
split_positions_into_batches(Positions, Journal, Batches) ->
|
2016-09-28 11:41:56 +01:00
|
|
|
{ThisBatch, Tail} = if
|
|
|
|
length(Positions) > ?BATCH_SIZE ->
|
|
|
|
lists:split(?BATCH_SIZE, Positions);
|
|
|
|
true ->
|
|
|
|
{Positions, []}
|
|
|
|
end,
|
2016-09-27 14:58:26 +01:00
|
|
|
split_positions_into_batches(Tail,
|
|
|
|
Journal,
|
|
|
|
Batches ++ [{Journal, ThisBatch}]).
|
|
|
|
|
|
|
|
|
2016-10-25 23:13:14 +01:00
|
|
|
filter_output(KVCs, FilterFun, FilterServer, MaxSQN, ReloadStrategy) ->
|
2016-11-14 11:40:02 +00:00
|
|
|
lists:foldl(fun(KVC0, Acc) ->
|
2016-10-25 23:13:14 +01:00
|
|
|
R = leveled_codec:compact_inkerkvc(KVC0, ReloadStrategy),
|
|
|
|
case R of
|
|
|
|
skip ->
|
2016-11-14 11:40:02 +00:00
|
|
|
Acc;
|
2016-10-25 23:13:14 +01:00
|
|
|
{TStrat, KVC1} ->
|
|
|
|
{K, _V, CrcCheck} = KVC0,
|
|
|
|
{SQN, LedgerKey} = leveled_codec:from_journalkey(K),
|
|
|
|
KeyValid = FilterFun(FilterServer, LedgerKey, SQN),
|
|
|
|
case {KeyValid, CrcCheck, SQN > MaxSQN, TStrat} of
|
|
|
|
{false, true, false, retain} ->
|
2016-11-14 11:40:02 +00:00
|
|
|
Acc ++ [KVC1];
|
2016-10-25 23:13:14 +01:00
|
|
|
{false, true, false, _} ->
|
2016-11-14 11:40:02 +00:00
|
|
|
Acc;
|
|
|
|
_ ->
|
|
|
|
Acc ++ [KVC0]
|
2016-10-25 23:13:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
2016-11-14 11:40:02 +00:00
|
|
|
[],
|
2016-10-25 23:13:14 +01:00
|
|
|
KVCs).
|
2016-09-27 14:58:26 +01:00
|
|
|
|
|
|
|
|
2016-09-28 11:41:56 +01:00
|
|
|
write_values([], _CDBopts, Journal0, ManSlice0) ->
|
|
|
|
{Journal0, ManSlice0};
|
2016-10-26 11:39:27 +01:00
|
|
|
write_values(KVCList, CDBopts, Journal0, ManSlice0) ->
|
|
|
|
KVList = lists:map(fun({K, V, _C}) ->
|
|
|
|
{K, leveled_codec:create_value_for_journal(V)}
|
|
|
|
end,
|
|
|
|
KVCList),
|
2016-09-28 11:41:56 +01:00
|
|
|
{ok, Journal1} = case Journal0 of
|
|
|
|
null ->
|
2016-10-26 11:39:27 +01:00
|
|
|
{TK, _TV} = lists:nth(1, KVList),
|
|
|
|
{SQN, _LK} = leveled_codec:from_journalkey(TK),
|
2016-09-28 11:41:56 +01:00
|
|
|
FP = CDBopts#cdb_options.file_path,
|
|
|
|
FN = leveled_inker:filepath(FP,
|
|
|
|
SQN,
|
|
|
|
compact_journal),
|
2016-11-03 16:05:43 +00:00
|
|
|
leveled_log:log("IC009", [FN]),
|
2016-09-28 11:41:56 +01:00
|
|
|
leveled_cdb:cdb_open_writer(FN,
|
|
|
|
CDBopts);
|
|
|
|
_ ->
|
|
|
|
{ok, Journal0}
|
|
|
|
end,
|
2016-10-26 11:39:27 +01:00
|
|
|
R = leveled_cdb:cdb_mput(Journal1, KVList),
|
2016-09-27 14:58:26 +01:00
|
|
|
case R of
|
|
|
|
ok ->
|
2016-10-26 11:39:27 +01:00
|
|
|
{Journal1, ManSlice0};
|
2016-09-27 14:58:26 +01:00
|
|
|
roll ->
|
2016-09-28 11:41:56 +01:00
|
|
|
ManSlice1 = ManSlice0 ++ generate_manifest_entry(Journal1),
|
2016-10-26 11:39:27 +01:00
|
|
|
write_values(KVCList, CDBopts, null, ManSlice1)
|
2016-09-27 14:58:26 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2016-09-28 11:41:56 +01:00
|
|
|
generate_manifest_entry(ActiveJournal) ->
|
|
|
|
{ok, NewFN} = leveled_cdb:cdb_complete(ActiveJournal),
|
|
|
|
{ok, PidR} = leveled_cdb:cdb_open_reader(NewFN),
|
2016-10-25 01:57:12 +01:00
|
|
|
{StartSQN, _Type, _PK} = leveled_cdb:cdb_firstkey(PidR),
|
2016-09-28 11:41:56 +01:00
|
|
|
[{StartSQN, NewFN, PidR}].
|
2016-09-27 14:58:26 +01:00
|
|
|
|
|
|
|
|
2016-11-14 11:17:14 +00:00
|
|
|
clear_waste(State) ->
|
|
|
|
WP = State#state.waste_path,
|
|
|
|
WRP = State#state.waste_retention_period,
|
|
|
|
{ok, ClearedJournals} = file:list_dir(WP),
|
|
|
|
N = calendar:datetime_to_gregorian_seconds(calendar:local_time()),
|
|
|
|
lists:foreach(fun(DelJ) ->
|
|
|
|
LMD = filelib:last_modified(WP ++ DelJ),
|
|
|
|
case N - calendar:datetime_to_gregorian_seconds(LMD) of
|
|
|
|
LMD_Delta when LMD_Delta >= WRP ->
|
|
|
|
ok = file:delete(WP ++ DelJ),
|
|
|
|
leveled_log:log("IC010", [WP ++ DelJ]);
|
|
|
|
LMD_Delta ->
|
|
|
|
leveled_log:log("IC011", [WP ++ DelJ,
|
|
|
|
LMD_Delta]),
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
ClearedJournals).
|
2016-09-27 14:58:26 +01:00
|
|
|
|
|
|
|
|
2016-09-20 16:13:36 +01:00
|
|
|
|
|
|
|
%%%============================================================================
|
|
|
|
%%% Test
|
|
|
|
%%%============================================================================
|
2016-09-27 14:58:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
-ifdef(TEST).
|
|
|
|
|
|
|
|
simple_score_test() ->
|
|
|
|
Run1 = [#candidate{compaction_perc = 75.0},
|
|
|
|
#candidate{compaction_perc = 75.0},
|
|
|
|
#candidate{compaction_perc = 76.0},
|
|
|
|
#candidate{compaction_perc = 70.0}],
|
|
|
|
?assertMatch(6.0, score_run(Run1, 4)),
|
|
|
|
Run2 = [#candidate{compaction_perc = 75.0}],
|
|
|
|
?assertMatch(-15.0, score_run(Run2, 4)),
|
2016-10-05 18:28:31 +01:00
|
|
|
?assertMatch(0.0, score_run([], 4)),
|
|
|
|
Run3 = [#candidate{compaction_perc = 100.0}],
|
|
|
|
?assertMatch(-40.0, score_run(Run3, 4)).
|
2016-09-27 14:58:26 +01:00
|
|
|
|
|
|
|
score_compare_test() ->
|
|
|
|
Run1 = [#candidate{compaction_perc = 75.0},
|
|
|
|
#candidate{compaction_perc = 75.0},
|
|
|
|
#candidate{compaction_perc = 76.0},
|
|
|
|
#candidate{compaction_perc = 70.0}],
|
|
|
|
?assertMatch(6.0, score_run(Run1, 4)),
|
|
|
|
Run2 = [#candidate{compaction_perc = 75.0}],
|
|
|
|
?assertMatch(Run1, choose_best_assessment(Run1, Run2, 4)),
|
|
|
|
?assertMatch(Run2, choose_best_assessment(Run1 ++ Run2, Run2, 4)).
|
|
|
|
|
2016-11-14 11:17:14 +00:00
|
|
|
file_gc_test() ->
|
|
|
|
State = #state{waste_path="test/waste/",
|
|
|
|
waste_retention_period=1},
|
|
|
|
ok = filelib:ensure_dir(State#state.waste_path),
|
|
|
|
file:write_file(State#state.waste_path ++ "1.cdb", term_to_binary("Hello")),
|
|
|
|
timer:sleep(1100),
|
|
|
|
file:write_file(State#state.waste_path ++ "2.cdb", term_to_binary("Hello")),
|
|
|
|
clear_waste(State),
|
|
|
|
{ok, ClearedJournals} = file:list_dir(State#state.waste_path),
|
|
|
|
?assertMatch(["2.cdb"], ClearedJournals),
|
|
|
|
timer:sleep(1100),
|
|
|
|
clear_waste(State),
|
|
|
|
{ok, ClearedJournals2} = file:list_dir(State#state.waste_path),
|
|
|
|
?assertMatch([], ClearedJournals2).
|
|
|
|
|
2016-09-27 14:58:26 +01:00
|
|
|
find_bestrun_test() ->
|
|
|
|
%% Tests dependent on these defaults
|
|
|
|
%% -define(MAX_COMPACTION_RUN, 4).
|
|
|
|
%% -define(SINGLEFILE_COMPACTION_TARGET, 60.0).
|
|
|
|
%% -define(MAXRUN_COMPACTION_TARGET, 80.0).
|
|
|
|
%% Tested first with blocks significant as no back-tracking
|
|
|
|
Block1 = [#candidate{compaction_perc = 75.0},
|
|
|
|
#candidate{compaction_perc = 85.0},
|
|
|
|
#candidate{compaction_perc = 62.0},
|
|
|
|
#candidate{compaction_perc = 70.0}],
|
|
|
|
Block2 = [#candidate{compaction_perc = 58.0},
|
|
|
|
#candidate{compaction_perc = 95.0},
|
|
|
|
#candidate{compaction_perc = 95.0},
|
|
|
|
#candidate{compaction_perc = 65.0}],
|
|
|
|
Block3 = [#candidate{compaction_perc = 90.0},
|
|
|
|
#candidate{compaction_perc = 100.0},
|
|
|
|
#candidate{compaction_perc = 100.0},
|
|
|
|
#candidate{compaction_perc = 100.0}],
|
|
|
|
Block4 = [#candidate{compaction_perc = 75.0},
|
|
|
|
#candidate{compaction_perc = 76.0},
|
|
|
|
#candidate{compaction_perc = 76.0},
|
|
|
|
#candidate{compaction_perc = 60.0}],
|
|
|
|
Block5 = [#candidate{compaction_perc = 80.0},
|
|
|
|
#candidate{compaction_perc = 80.0}],
|
|
|
|
CList0 = Block1 ++ Block2 ++ Block3 ++ Block4 ++ Block5,
|
|
|
|
?assertMatch(Block4, assess_candidates(CList0, 4, [], [])),
|
|
|
|
CList1 = CList0 ++ [#candidate{compaction_perc = 20.0}],
|
|
|
|
?assertMatch([#candidate{compaction_perc = 20.0}],
|
|
|
|
assess_candidates(CList1, 4, [], [])),
|
|
|
|
CList2 = Block4 ++ Block3 ++ Block2 ++ Block1 ++ Block5,
|
|
|
|
?assertMatch(Block4, assess_candidates(CList2, 4, [], [])),
|
|
|
|
CList3 = Block5 ++ Block1 ++ Block2 ++ Block3 ++ Block4,
|
|
|
|
?assertMatch([#candidate{compaction_perc = 62.0},
|
|
|
|
#candidate{compaction_perc = 70.0},
|
|
|
|
#candidate{compaction_perc = 58.0}],
|
|
|
|
assess_candidates(CList3, 4, [], [])),
|
|
|
|
%% Now do some back-tracking to get a genuinely optimal solution without
|
|
|
|
%% needing to re-order
|
|
|
|
?assertMatch([#candidate{compaction_perc = 62.0},
|
|
|
|
#candidate{compaction_perc = 70.0},
|
|
|
|
#candidate{compaction_perc = 58.0}],
|
|
|
|
assess_candidates(CList0, 4)),
|
|
|
|
?assertMatch([#candidate{compaction_perc = 62.0},
|
|
|
|
#candidate{compaction_perc = 70.0},
|
|
|
|
#candidate{compaction_perc = 58.0}],
|
|
|
|
assess_candidates(CList0, 5)),
|
|
|
|
?assertMatch([#candidate{compaction_perc = 62.0},
|
|
|
|
#candidate{compaction_perc = 70.0},
|
|
|
|
#candidate{compaction_perc = 58.0},
|
|
|
|
#candidate{compaction_perc = 95.0},
|
|
|
|
#candidate{compaction_perc = 95.0},
|
|
|
|
#candidate{compaction_perc = 65.0}],
|
|
|
|
assess_candidates(CList0, 6)).
|
|
|
|
|
2016-10-25 23:13:14 +01:00
|
|
|
test_ledgerkey(Key) ->
|
|
|
|
{o, "Bucket", Key, null}.
|
|
|
|
|
|
|
|
test_inkerkv(SQN, Key, V, IdxSpecs) ->
|
|
|
|
{{SQN, ?INKT_STND, test_ledgerkey(Key)}, term_to_binary({V, IdxSpecs})}.
|
|
|
|
|
2016-09-28 11:41:56 +01:00
|
|
|
fetch_testcdb(RP) ->
|
2016-09-27 14:58:26 +01:00
|
|
|
FN1 = leveled_inker:filepath(RP, 1, new_journal),
|
|
|
|
{ok, CDB1} = leveled_cdb:cdb_open_writer(FN1, #cdb_options{}),
|
2016-10-25 23:13:14 +01:00
|
|
|
{K1, V1} = test_inkerkv(1, "Key1", "Value1", []),
|
|
|
|
{K2, V2} = test_inkerkv(2, "Key2", "Value2", []),
|
|
|
|
{K3, V3} = test_inkerkv(3, "Key3", "Value3", []),
|
|
|
|
{K4, V4} = test_inkerkv(4, "Key1", "Value4", []),
|
|
|
|
{K5, V5} = test_inkerkv(5, "Key1", "Value5", []),
|
|
|
|
{K6, V6} = test_inkerkv(6, "Key1", "Value6", []),
|
|
|
|
{K7, V7} = test_inkerkv(7, "Key1", "Value7", []),
|
|
|
|
{K8, V8} = test_inkerkv(8, "Key1", "Value8", []),
|
2016-09-27 14:58:26 +01:00
|
|
|
ok = leveled_cdb:cdb_put(CDB1, K1, V1),
|
|
|
|
ok = leveled_cdb:cdb_put(CDB1, K2, V2),
|
|
|
|
ok = leveled_cdb:cdb_put(CDB1, K3, V3),
|
|
|
|
ok = leveled_cdb:cdb_put(CDB1, K4, V4),
|
|
|
|
ok = leveled_cdb:cdb_put(CDB1, K5, V5),
|
|
|
|
ok = leveled_cdb:cdb_put(CDB1, K6, V6),
|
|
|
|
ok = leveled_cdb:cdb_put(CDB1, K7, V7),
|
|
|
|
ok = leveled_cdb:cdb_put(CDB1, K8, V8),
|
|
|
|
{ok, FN2} = leveled_cdb:cdb_complete(CDB1),
|
2016-09-28 11:41:56 +01:00
|
|
|
leveled_cdb:cdb_open_reader(FN2).
|
|
|
|
|
|
|
|
check_single_file_test() ->
|
|
|
|
RP = "../test/journal",
|
|
|
|
{ok, CDB} = fetch_testcdb(RP),
|
2016-10-25 23:13:14 +01:00
|
|
|
LedgerSrv1 = [{8, {o, "Bucket", "Key1", null}},
|
|
|
|
{2, {o, "Bucket", "Key2", null}},
|
|
|
|
{3, {o, "Bucket", "Key3", null}}],
|
2016-09-27 14:58:26 +01:00
|
|
|
LedgerFun1 = fun(Srv, Key, ObjSQN) ->
|
|
|
|
case lists:keyfind(ObjSQN, 1, Srv) of
|
|
|
|
{ObjSQN, Key} ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end end,
|
2016-10-05 18:28:31 +01:00
|
|
|
Score1 = check_single_file(CDB, LedgerFun1, LedgerSrv1, 9, 8, 4),
|
2016-09-27 14:58:26 +01:00
|
|
|
?assertMatch(37.5, Score1),
|
|
|
|
LedgerFun2 = fun(_Srv, _Key, _ObjSQN) -> true end,
|
2016-10-05 18:28:31 +01:00
|
|
|
Score2 = check_single_file(CDB, LedgerFun2, LedgerSrv1, 9, 8, 4),
|
2016-09-27 14:58:26 +01:00
|
|
|
?assertMatch(100.0, Score2),
|
2016-10-05 18:28:31 +01:00
|
|
|
Score3 = check_single_file(CDB, LedgerFun1, LedgerSrv1, 9, 8, 3),
|
2016-09-27 14:58:26 +01:00
|
|
|
?assertMatch(37.5, Score3),
|
2016-10-05 18:28:31 +01:00
|
|
|
Score4 = check_single_file(CDB, LedgerFun1, LedgerSrv1, 4, 8, 4),
|
|
|
|
?assertMatch(75.0, Score4),
|
2016-10-26 20:39:16 +01:00
|
|
|
ok = leveled_cdb:cdb_deletepending(CDB),
|
2016-09-28 11:41:56 +01:00
|
|
|
ok = leveled_cdb:cdb_destroy(CDB).
|
2016-10-05 18:28:31 +01:00
|
|
|
|
|
|
|
|
2016-10-25 23:13:14 +01:00
|
|
|
compact_single_file_setup() ->
|
2016-09-28 11:41:56 +01:00
|
|
|
RP = "../test/journal",
|
|
|
|
{ok, CDB} = fetch_testcdb(RP),
|
|
|
|
Candidate = #candidate{journal = CDB,
|
|
|
|
low_sqn = 1,
|
|
|
|
filename = "test",
|
|
|
|
compaction_perc = 37.5},
|
2016-10-25 23:13:14 +01:00
|
|
|
LedgerSrv1 = [{8, {o, "Bucket", "Key1", null}},
|
|
|
|
{2, {o, "Bucket", "Key2", null}},
|
|
|
|
{3, {o, "Bucket", "Key3", null}}],
|
2016-09-28 11:41:56 +01:00
|
|
|
LedgerFun1 = fun(Srv, Key, ObjSQN) ->
|
|
|
|
case lists:keyfind(ObjSQN, 1, Srv) of
|
|
|
|
{ObjSQN, Key} ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end end,
|
|
|
|
CompactFP = leveled_inker:filepath(RP, journal_compact_dir),
|
|
|
|
ok = filelib:ensure_dir(CompactFP),
|
2016-10-25 23:13:14 +01:00
|
|
|
{Candidate, LedgerSrv1, LedgerFun1, CompactFP, CDB}.
|
|
|
|
|
|
|
|
compact_single_file_recovr_test() ->
|
|
|
|
{Candidate,
|
|
|
|
LedgerSrv1,
|
|
|
|
LedgerFun1,
|
|
|
|
CompactFP,
|
|
|
|
CDB} = compact_single_file_setup(),
|
2016-11-14 11:40:02 +00:00
|
|
|
[{LowSQN, FN, PidR}] = compact_files([Candidate],
|
|
|
|
#cdb_options{file_path=CompactFP},
|
|
|
|
LedgerFun1,
|
|
|
|
LedgerSrv1,
|
|
|
|
9,
|
|
|
|
[{?STD_TAG, recovr}]),
|
2016-09-28 11:41:56 +01:00
|
|
|
io:format("FN of ~s~n", [FN]),
|
|
|
|
?assertMatch(2, LowSQN),
|
2016-10-25 23:13:14 +01:00
|
|
|
?assertMatch(probably,
|
|
|
|
leveled_cdb:cdb_keycheck(PidR,
|
|
|
|
{8,
|
|
|
|
stnd,
|
|
|
|
test_ledgerkey("Key1")})),
|
|
|
|
?assertMatch(missing, leveled_cdb:cdb_get(PidR,
|
|
|
|
{7,
|
|
|
|
stnd,
|
|
|
|
test_ledgerkey("Key1")})),
|
|
|
|
?assertMatch(missing, leveled_cdb:cdb_get(PidR,
|
|
|
|
{1,
|
|
|
|
stnd,
|
|
|
|
test_ledgerkey("Key1")})),
|
|
|
|
{_RK1, RV1} = leveled_cdb:cdb_get(PidR,
|
|
|
|
{2,
|
|
|
|
stnd,
|
|
|
|
test_ledgerkey("Key2")}),
|
|
|
|
?assertMatch({"Value2", []}, binary_to_term(RV1)),
|
2016-10-26 20:39:16 +01:00
|
|
|
ok = leveled_cdb:cdb_deletepending(CDB),
|
2016-09-28 18:26:52 +01:00
|
|
|
ok = leveled_cdb:cdb_destroy(CDB).
|
2016-09-28 11:41:56 +01:00
|
|
|
|
2016-09-27 14:58:26 +01:00
|
|
|
|
2016-10-25 23:13:14 +01:00
|
|
|
compact_single_file_retain_test() ->
|
|
|
|
{Candidate,
|
|
|
|
LedgerSrv1,
|
|
|
|
LedgerFun1,
|
|
|
|
CompactFP,
|
|
|
|
CDB} = compact_single_file_setup(),
|
2016-11-14 11:40:02 +00:00
|
|
|
[{LowSQN, FN, PidR}] = compact_files([Candidate],
|
|
|
|
#cdb_options{file_path=CompactFP},
|
|
|
|
LedgerFun1,
|
|
|
|
LedgerSrv1,
|
|
|
|
9,
|
|
|
|
[{?STD_TAG, retain}]),
|
2016-10-25 23:13:14 +01:00
|
|
|
io:format("FN of ~s~n", [FN]),
|
|
|
|
?assertMatch(1, LowSQN),
|
|
|
|
?assertMatch(probably,
|
|
|
|
leveled_cdb:cdb_keycheck(PidR,
|
|
|
|
{8,
|
|
|
|
stnd,
|
|
|
|
test_ledgerkey("Key1")})),
|
|
|
|
?assertMatch(missing, leveled_cdb:cdb_get(PidR,
|
|
|
|
{7,
|
|
|
|
stnd,
|
|
|
|
test_ledgerkey("Key1")})),
|
|
|
|
?assertMatch(missing, leveled_cdb:cdb_get(PidR,
|
|
|
|
{1,
|
|
|
|
stnd,
|
|
|
|
test_ledgerkey("Key1")})),
|
|
|
|
{_RK1, RV1} = leveled_cdb:cdb_get(PidR,
|
|
|
|
{2,
|
|
|
|
stnd,
|
|
|
|
test_ledgerkey("Key2")}),
|
|
|
|
?assertMatch({"Value2", []}, binary_to_term(RV1)),
|
2016-10-26 20:39:16 +01:00
|
|
|
ok = leveled_cdb:cdb_deletepending(CDB),
|
2016-10-25 23:13:14 +01:00
|
|
|
ok = leveled_cdb:cdb_destroy(CDB).
|
|
|
|
|
2016-10-08 22:15:48 +01:00
|
|
|
compact_empty_file_test() ->
|
|
|
|
RP = "../test/journal",
|
|
|
|
FN1 = leveled_inker:filepath(RP, 1, new_journal),
|
|
|
|
CDBopts = #cdb_options{binary_mode=true},
|
|
|
|
{ok, CDB1} = leveled_cdb:cdb_open_writer(FN1, CDBopts),
|
2016-10-25 23:13:14 +01:00
|
|
|
ok = leveled_cdb:cdb_put(CDB1, {1, stnd, test_ledgerkey("Key1")}, <<>>),
|
2016-10-08 22:15:48 +01:00
|
|
|
{ok, FN2} = leveled_cdb:cdb_complete(CDB1),
|
|
|
|
{ok, CDB2} = leveled_cdb:cdb_open_reader(FN2),
|
2016-10-25 23:13:14 +01:00
|
|
|
LedgerSrv1 = [{8, {o, "Bucket", "Key1", null}},
|
|
|
|
{2, {o, "Bucket", "Key2", null}},
|
|
|
|
{3, {o, "Bucket", "Key3", null}}],
|
2016-11-04 19:33:11 +00:00
|
|
|
LedgerFun1 = fun(_Srv, _Key, _ObjSQN) -> false end,
|
2016-10-08 22:15:48 +01:00
|
|
|
Score1 = check_single_file(CDB2, LedgerFun1, LedgerSrv1, 9, 8, 4),
|
|
|
|
?assertMatch(100.0, Score1).
|
|
|
|
|
2016-10-27 00:57:19 +01:00
|
|
|
compare_candidate_test() ->
|
|
|
|
Candidate1 = #candidate{low_sqn=1},
|
|
|
|
Candidate2 = #candidate{low_sqn=2},
|
|
|
|
Candidate3 = #candidate{low_sqn=3},
|
|
|
|
Candidate4 = #candidate{low_sqn=4},
|
|
|
|
?assertMatch([Candidate1, Candidate2, Candidate3, Candidate4],
|
|
|
|
sort_run([Candidate3, Candidate2, Candidate4, Candidate1])).
|
|
|
|
|
2016-11-04 12:22:15 +00:00
|
|
|
compact_singlefile_totwosmallfiles_test() ->
|
|
|
|
RP = "../test/journal",
|
|
|
|
CP = "../test/journal/journal_file/post_compact/",
|
|
|
|
ok = filelib:ensure_dir(CP),
|
|
|
|
FN1 = leveled_inker:filepath(RP, 1, new_journal),
|
|
|
|
CDBoptsLarge = #cdb_options{binary_mode=true, max_size=30000000},
|
|
|
|
{ok, CDB1} = leveled_cdb:cdb_open_writer(FN1, CDBoptsLarge),
|
|
|
|
lists:foreach(fun(X) ->
|
|
|
|
LK = test_ledgerkey("Key" ++ integer_to_list(X)),
|
|
|
|
Value = term_to_binary({crypto:rand_bytes(1024), []}),
|
|
|
|
ok = leveled_cdb:cdb_put(CDB1,
|
|
|
|
{X, ?INKT_STND, LK},
|
|
|
|
Value)
|
|
|
|
end,
|
|
|
|
lists:seq(1, 1000)),
|
|
|
|
{ok, NewName} = leveled_cdb:cdb_complete(CDB1),
|
|
|
|
{ok, CDBr} = leveled_cdb:cdb_open_reader(NewName),
|
|
|
|
CDBoptsSmall = #cdb_options{binary_mode=true, max_size=400000, file_path=CP},
|
|
|
|
BestRun1 = [#candidate{low_sqn=1,
|
|
|
|
filename=leveled_cdb:cdb_filename(CDBr),
|
|
|
|
journal=CDBr,
|
|
|
|
compaction_perc=50.0}],
|
|
|
|
FakeFilterFun = fun(_FS, _LK, SQN) -> SQN rem 2 == 0 end,
|
|
|
|
|
2016-11-14 11:40:02 +00:00
|
|
|
ManifestSlice = compact_files(BestRun1,
|
|
|
|
CDBoptsSmall,
|
|
|
|
FakeFilterFun,
|
|
|
|
null,
|
|
|
|
900,
|
|
|
|
[{?STD_TAG, recovr}]),
|
2016-11-04 12:22:15 +00:00
|
|
|
?assertMatch(2, length(ManifestSlice)),
|
|
|
|
lists:foreach(fun({_SQN, _FN, CDB}) ->
|
|
|
|
ok = leveled_cdb:cdb_deletepending(CDB),
|
|
|
|
ok = leveled_cdb:cdb_destroy(CDB)
|
|
|
|
end,
|
|
|
|
ManifestSlice),
|
|
|
|
ok = leveled_cdb:cdb_deletepending(CDBr),
|
|
|
|
ok = leveled_cdb:cdb_destroy(CDBr).
|
|
|
|
|
|
|
|
|
2016-09-27 14:58:26 +01:00
|
|
|
-endif.
|