2016-08-02 13:44:48 +01:00
|
|
|
%% -------- Inker ---------
|
2016-09-05 15:01:23 +01:00
|
|
|
%%
|
|
|
|
%% The Inker is responsible for managing access and updates to the Journal.
|
|
|
|
%%
|
|
|
|
%% The Inker maintains a manifest of what files make up the Journal, and which
|
|
|
|
%% file is the current append-only nursery log to accept new PUTs into the
|
|
|
|
%% Journal. The Inker also marshals GET requests to the appropriate database
|
|
|
|
%% file within the Journal (routed by sequence number). The Inker is also
|
|
|
|
%% responsible for scheduling compaction work to be carried out by the Inker's
|
|
|
|
%% clerk.
|
|
|
|
%%
|
2016-10-07 18:07:03 +01:00
|
|
|
%% -------- Journal Files ---------
|
2016-09-05 15:01:23 +01:00
|
|
|
%%
|
2016-10-07 18:07:03 +01:00
|
|
|
%% The Journal is a series of files originally named as <SQN>_<GUID>
|
2016-09-05 15:01:23 +01:00
|
|
|
%% where the sequence number is the first object sequence number (key) within
|
|
|
|
%% the given database file. The files will be named *.cdb at the point they
|
|
|
|
%% have been made immutable (through a rename operation). Prior to this, they
|
|
|
|
%% will originally start out as a *.pnd file.
|
|
|
|
%%
|
|
|
|
%% At some stage in the future compacted versions of old journal cdb files may
|
2016-10-07 18:07:03 +01:00
|
|
|
%% be produced. These files will be named <SQN>-<NewGUID>.cdb, and once
|
|
|
|
%% the manifest is updated the original <SQN>_<GUID>.cdb (or
|
|
|
|
%% <SQN>_<previousGUID>.cdb) files they replace will be erased.
|
2016-08-02 13:44:48 +01:00
|
|
|
%%
|
2016-10-07 18:07:03 +01:00
|
|
|
%% The current Journal is made up of a set of files referenced in the manifest.
|
|
|
|
%% No PUTs are made to files which are not in the manifest.
|
2016-08-02 13:44:48 +01:00
|
|
|
%%
|
2016-09-05 15:01:23 +01:00
|
|
|
%% The Journal is ordered by sequence number from front to back both within
|
|
|
|
%% and across files.
|
|
|
|
%%
|
|
|
|
%% On startup the Inker should open the manifest with the highest sequence
|
|
|
|
%% number, and this will contain the list of filenames that make up the
|
2016-10-07 18:07:03 +01:00
|
|
|
%% non-recent part of the Journal. All the filenames should then be opened.
|
|
|
|
%% How they are opened depends on the file extension:
|
|
|
|
%%
|
|
|
|
%% - If the file extension is *.cdb the file is opened read only
|
|
|
|
%% - If the file extension is *.pnd and the file is not the most recent in the
|
|
|
|
%% manifest, then the file should be completed bfore being opened read-only
|
|
|
|
%% - If the file extension is *.pnd the file is opened for writing
|
|
|
|
%%
|
|
|
|
%% -------- Manifest Files ---------
|
|
|
|
%%
|
|
|
|
%% The manifest is just saved as a straight term_to_binary blob, with a
|
|
|
|
%% filename ordered by the Manifest SQN. The Manifest is first saved with a
|
|
|
|
%% *.pnd extension, and then renamed to one with a *.man extension.
|
|
|
|
%%
|
|
|
|
%% On startup the *.man manifest file with the highest manifest sequence
|
|
|
|
%% number should be used.
|
2016-09-05 15:01:23 +01:00
|
|
|
%%
|
|
|
|
%% -------- Objects ---------
|
|
|
|
%%
|
|
|
|
%% From the perspective of the Inker, objects to store are made up of:
|
2016-10-25 01:57:12 +01:00
|
|
|
%% - An Inker Key formed from
|
|
|
|
%% - A sequence number (assigned by the Inker)
|
|
|
|
%% - An Inker key type (stnd, tomb or keyd)
|
|
|
|
%% - A Ledger Key (as an Erlang term)
|
|
|
|
%% - A value formed from
|
|
|
|
%% - An object (an Erlang term) which should be null for tomb types, and
|
|
|
|
%% maybe null for keyd types
|
|
|
|
%% - A set of Key Deltas associated with the change (which may be an
|
|
|
|
%% empty list )
|
|
|
|
%%
|
|
|
|
%% Note that only the Inker key type of stnd is directly fetchable, other
|
|
|
|
%% key types are to be found only in scans and so can be added without being
|
|
|
|
%% entered into the hashtree
|
2016-09-05 15:01:23 +01:00
|
|
|
%%
|
|
|
|
%% -------- Compaction ---------
|
|
|
|
%%
|
|
|
|
%% Compaction is a process whereby an Inker's clerk will:
|
2016-10-07 18:07:03 +01:00
|
|
|
%% - Request a view of the current Inker manifest and a snaphot of the Ledger
|
2016-10-09 22:33:45 +01:00
|
|
|
%% - Test all files within the Journal to find the approximate comapction
|
2016-10-07 18:07:03 +01:00
|
|
|
%% potential percentage (the volume of the Journal that has been replaced)
|
|
|
|
%% - Attempts to find the optimal "run" of files to compact
|
|
|
|
%% - Compacts those files in the run, by rolling over the files re-writing
|
|
|
|
%% to a new Journal if and only if the Key is still present in the Ledger (or
|
|
|
|
%% the sequence number of the Key is higher than the SQN of the snapshot)
|
|
|
|
%% - Requests the Inker update the manifest with the new changes
|
|
|
|
%% - Instructs the files to destroy themselves when they are next closed
|
2016-08-02 13:44:48 +01:00
|
|
|
%%
|
2016-10-07 18:07:03 +01:00
|
|
|
%% TODO: how to instruct the files to close is tbd
|
2016-08-02 13:44:48 +01:00
|
|
|
%%
|
2016-08-02 17:51:43 +01:00
|
|
|
|
2016-10-07 18:07:03 +01:00
|
|
|
|
2016-08-02 17:51:43 +01:00
|
|
|
-module(leveled_inker).
|
|
|
|
|
2016-09-05 15:01:23 +01:00
|
|
|
-behaviour(gen_server).
|
|
|
|
|
2016-10-18 01:59:03 +01:00
|
|
|
-include("include/leveled.hrl").
|
2016-09-05 15:01:23 +01:00
|
|
|
|
|
|
|
-export([init/1,
|
|
|
|
handle_call/3,
|
|
|
|
handle_cast/2,
|
|
|
|
handle_info/2,
|
|
|
|
terminate/2,
|
|
|
|
code_change/3,
|
|
|
|
ink_start/1,
|
|
|
|
ink_put/4,
|
|
|
|
ink_get/3,
|
2016-09-15 10:53:24 +01:00
|
|
|
ink_fetch/3,
|
2016-10-31 17:26:28 +00:00
|
|
|
ink_keycheck/3,
|
2016-09-15 10:53:24 +01:00
|
|
|
ink_loadpcl/4,
|
2016-09-23 18:50:29 +01:00
|
|
|
ink_registersnapshot/2,
|
2016-10-26 20:39:16 +01:00
|
|
|
ink_confirmdelete/2,
|
2016-09-26 10:55:08 +01:00
|
|
|
ink_compactjournal/3,
|
2016-10-03 23:34:28 +01:00
|
|
|
ink_compactioncomplete/1,
|
2016-10-26 20:39:16 +01:00
|
|
|
ink_compactionpending/1,
|
2016-09-27 14:58:26 +01:00
|
|
|
ink_getmanifest/1,
|
2016-10-03 23:34:28 +01:00
|
|
|
ink_updatemanifest/3,
|
2017-05-22 15:31:42 +01:00
|
|
|
ink_printmanifest/1,
|
2016-09-27 14:58:26 +01:00
|
|
|
ink_close/1,
|
2016-11-21 12:34:40 +00:00
|
|
|
ink_doom/1,
|
2016-09-05 15:01:23 +01:00
|
|
|
build_dummy_journal/0,
|
2016-09-27 14:58:26 +01:00
|
|
|
clean_testdir/1,
|
2016-09-28 11:41:56 +01:00
|
|
|
filepath/2,
|
2016-09-27 14:58:26 +01:00
|
|
|
filepath/3]).
|
2016-09-05 15:01:23 +01:00
|
|
|
|
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
|
|
|
|
-define(MANIFEST_FP, "journal_manifest").
|
|
|
|
-define(FILES_FP, "journal_files").
|
2016-09-27 14:58:26 +01:00
|
|
|
-define(COMPACT_FP, "post_compact").
|
2016-11-14 11:17:14 +00:00
|
|
|
-define(WASTE_FP, "waste").
|
2016-09-05 15:01:23 +01:00
|
|
|
-define(JOURNAL_FILEX, "cdb").
|
|
|
|
-define(PENDING_FILEX, "pnd").
|
2016-11-05 14:31:10 +00:00
|
|
|
-define(LOADING_PAUSE, 1000).
|
2016-09-15 10:53:24 +01:00
|
|
|
-define(LOADING_BATCH, 1000).
|
2016-09-05 15:01:23 +01:00
|
|
|
|
|
|
|
-record(state, {manifest = [] :: list(),
|
|
|
|
manifest_sqn = 0 :: integer(),
|
|
|
|
journal_sqn = 0 :: integer(),
|
2017-07-31 19:47:58 +02:00
|
|
|
active_journaldb :: pid() | undefined,
|
2016-09-27 14:58:26 +01:00
|
|
|
pending_removals = [] :: list(),
|
2016-09-26 10:55:08 +01:00
|
|
|
registered_snapshots = [] :: list(),
|
2017-07-31 19:47:58 +02:00
|
|
|
root_path :: string() | undefined,
|
|
|
|
cdb_options :: #cdb_options{} | undefined,
|
|
|
|
clerk :: pid() | undefined,
|
2016-10-03 23:34:28 +01:00
|
|
|
compaction_pending = false :: boolean(),
|
2016-10-07 10:04:48 +01:00
|
|
|
is_snapshot = false :: boolean(),
|
2017-11-06 15:54:58 +00:00
|
|
|
compression_method :: lz4|native,
|
2017-11-06 18:44:08 +00:00
|
|
|
compress_on_receipt :: boolean(),
|
2017-07-31 19:47:58 +02:00
|
|
|
source_inker :: pid() | undefined}).
|
2016-09-05 15:01:23 +01:00
|
|
|
|
|
|
|
|
2017-05-22 15:31:42 +01:00
|
|
|
-type inker_options() :: #inker_options{}.
|
|
|
|
|
|
|
|
|
2016-09-05 15:01:23 +01:00
|
|
|
%%%============================================================================
|
|
|
|
%%% API
|
|
|
|
%%%============================================================================
|
2017-05-22 15:31:42 +01:00
|
|
|
|
|
|
|
-spec ink_start(inker_options()) -> {ok, pid()}.
|
|
|
|
%% @doc
|
|
|
|
%% Startup an inker process - passing in options.
|
|
|
|
%%
|
|
|
|
%% The first options are root_path and start_snapshot - if the inker is to be a
|
|
|
|
%% snapshot clone of another inker then start_snapshot should be true,
|
|
|
|
%% otherwise the root_path sill be used to find a file structure to provide a
|
|
|
|
%% starting point of state for the inker.
|
|
|
|
%%
|
|
|
|
%% The inker will need ot read and write CDB files (if it is not a snapshot),
|
|
|
|
%% and so a cdb_options record should be passed in as an inker_option to be
|
|
|
|
%% used when opening such files.
|
|
|
|
%%
|
|
|
|
%% The inker will need to know what the reload strategy is, to inform the
|
|
|
|
%% clerk about the rules to enforce during compaction.
|
2016-09-07 17:58:12 +01:00
|
|
|
ink_start(InkerOpts) ->
|
|
|
|
gen_server:start(?MODULE, [InkerOpts], []).
|
2016-09-05 15:01:23 +01:00
|
|
|
|
2017-05-22 15:31:42 +01:00
|
|
|
-spec ink_put(pid(),
|
|
|
|
{atom(), any(), any(), any()}|string(),
|
|
|
|
any(),
|
|
|
|
{list(), integer()|infinity}) ->
|
|
|
|
{ok, integer(), integer()}.
|
|
|
|
%% @doc
|
|
|
|
%% PUT an object into the journal, returning the sequence number for the PUT
|
|
|
|
%% as well as the size of the object (information required by the ledger).
|
|
|
|
%%
|
|
|
|
%% The primary key is expected to be a tuple of the form
|
|
|
|
%% {Tag, Bucket, Key, null}, but unit tests support pure string Keys and so
|
|
|
|
%% these types are also supported.
|
|
|
|
%%
|
|
|
|
%% KeyChanges is a tuple of {KeyChanges, TTL} where the TTL is an
|
|
|
|
%% expiry time (or infinity).
|
2016-09-05 15:01:23 +01:00
|
|
|
ink_put(Pid, PrimaryKey, Object, KeyChanges) ->
|
|
|
|
gen_server:call(Pid, {put, PrimaryKey, Object, KeyChanges}, infinity).
|
|
|
|
|
2017-05-22 15:31:42 +01:00
|
|
|
-spec ink_get(pid(),
|
|
|
|
{atom(), any(), any(), any()}|string(),
|
|
|
|
integer()) ->
|
|
|
|
{{integer(), any()}, {any(), any()}}.
|
|
|
|
%% @doc
|
|
|
|
%% Fetch the object as stored in the Journal. Will not mask errors, should be
|
|
|
|
%% used only in tests
|
2016-09-05 15:01:23 +01:00
|
|
|
ink_get(Pid, PrimaryKey, SQN) ->
|
|
|
|
gen_server:call(Pid, {get, PrimaryKey, SQN}, infinity).
|
|
|
|
|
2017-05-22 15:31:42 +01:00
|
|
|
-spec ink_fetch(pid(),
|
|
|
|
{atom(), any(), any(), any()}|string(),
|
|
|
|
integer()) ->
|
|
|
|
any().
|
|
|
|
%% @doc
|
|
|
|
%% Fetch the value that was stored for a given Key at a particular SQN (i.e.
|
|
|
|
%% this must be a SQN of the write for this key). the full object is returned
|
|
|
|
%% or the atome not_present if there is no such Key stored at that SQN, or if
|
|
|
|
%% fetching the Key prompted some anticipated error (e.g. CRC check failed)
|
2016-09-15 10:53:24 +01:00
|
|
|
ink_fetch(Pid, PrimaryKey, SQN) ->
|
|
|
|
gen_server:call(Pid, {fetch, PrimaryKey, SQN}, infinity).
|
2016-09-09 15:58:19 +01:00
|
|
|
|
2017-05-22 15:31:42 +01:00
|
|
|
-spec ink_keycheck(pid(),
|
|
|
|
{atom(), any(), any(), any()}|string(),
|
|
|
|
integer()) ->
|
|
|
|
probably|missing.
|
|
|
|
%% @doc
|
|
|
|
%% Quick check to determine if key is probably present. Positive results have
|
|
|
|
%% a very small false positive rate, as can be triggered through a hash
|
|
|
|
%% collision.
|
2016-10-31 17:26:28 +00:00
|
|
|
ink_keycheck(Pid, PrimaryKey, SQN) ->
|
|
|
|
gen_server:call(Pid, {key_check, PrimaryKey, SQN}, infinity).
|
|
|
|
|
2017-05-22 15:31:42 +01:00
|
|
|
-spec ink_registersnapshot(pid(), pid()) -> {list(), pid()}.
|
|
|
|
%% @doc
|
|
|
|
%% Register a snapshot clone for the process, returning the Manifest and the
|
|
|
|
%% pid of the active journal.
|
2016-09-23 18:50:29 +01:00
|
|
|
ink_registersnapshot(Pid, Requestor) ->
|
2016-10-05 09:54:53 +01:00
|
|
|
gen_server:call(Pid, {register_snapshot, Requestor}, infinity).
|
2016-09-05 15:01:23 +01:00
|
|
|
|
2017-05-22 15:31:42 +01:00
|
|
|
-spec ink_releasesnapshot(pid(), pid()) -> ok.
|
|
|
|
%% @doc
|
|
|
|
%% Release a registered snapshot as it is no longer in use. This should be
|
|
|
|
%% called by all terminating snapshots - otherwise space may not be cleared
|
|
|
|
%% following compaction.
|
2016-10-07 10:04:48 +01:00
|
|
|
ink_releasesnapshot(Pid, Snapshot) ->
|
2016-11-14 19:34:11 +00:00
|
|
|
gen_server:cast(Pid, {release_snapshot, Snapshot}).
|
2016-10-07 10:04:48 +01:00
|
|
|
|
2017-05-22 15:31:42 +01:00
|
|
|
-spec ink_confirmdelete(pid(), integer()) -> boolean().
|
|
|
|
%% @doc
|
|
|
|
%% Confirm if a Journal CDB file can be deleted, as it has been set to delete
|
|
|
|
%% and is no longer in use by any snapshots
|
2016-10-26 20:39:16 +01:00
|
|
|
ink_confirmdelete(Pid, ManSQN) ->
|
2016-11-14 19:34:11 +00:00
|
|
|
gen_server:call(Pid, {confirm_delete, ManSQN}).
|
2016-10-26 20:39:16 +01:00
|
|
|
|
2017-05-22 15:31:42 +01:00
|
|
|
-spec ink_close(pid()) -> ok.
|
|
|
|
%% @doc
|
|
|
|
%% Close the inker, prompting all the Journal file processes to be called.
|
2016-09-06 17:17:31 +01:00
|
|
|
ink_close(Pid) ->
|
2016-10-30 22:06:44 +00:00
|
|
|
gen_server:call(Pid, close, infinity).
|
2016-09-06 17:17:31 +01:00
|
|
|
|
2017-05-22 15:31:42 +01:00
|
|
|
-spec ink_doom(pid()) -> {ok, [{string(), string(), string(), string()}]}.
|
|
|
|
%% @doc
|
|
|
|
%% Test function used to close a file, and return all file paths (potentially
|
|
|
|
%% to erase all persisted existence)
|
2016-11-21 12:34:40 +00:00
|
|
|
ink_doom(Pid) ->
|
|
|
|
gen_server:call(Pid, doom, 60000).
|
|
|
|
|
2017-05-22 15:31:42 +01:00
|
|
|
-spec ink_loadpcl(pid(), integer(), fun(), pid()) -> ok.
|
|
|
|
%% @doc
|
|
|
|
%% Function to prompt load of the Ledger at startup. the Penciller should
|
|
|
|
%% have determined the lowest SQN not present in the Ledger, and the inker
|
|
|
|
%% should fold over the Journal from that point, using the function to load
|
|
|
|
%% penciller with the results.
|
|
|
|
%%
|
|
|
|
%% The load fun should be a five arity function like:
|
|
|
|
%% load_fun(KeyInJournal, ValueInJournal, _Position, Acc0, ExtractFun)
|
2016-09-15 10:53:24 +01:00
|
|
|
ink_loadpcl(Pid, MinSQN, FilterFun, Penciller) ->
|
|
|
|
gen_server:call(Pid, {load_pcl, MinSQN, FilterFun, Penciller}, infinity).
|
|
|
|
|
2017-05-22 15:31:42 +01:00
|
|
|
-spec ink_compactjournal(pid(), pid(), integer()) -> ok.
|
|
|
|
%% @doc
|
|
|
|
%% Trigger a compaction event. the compaction event will use a sqn check
|
|
|
|
%% against the Ledger to see if a value can be compacted - if the penciller
|
|
|
|
%% believes it to be superseded that it can be compacted.
|
|
|
|
%%
|
|
|
|
%% The inker will get the maximum persisted sequence number from the
|
|
|
|
%% initiate_penciller_snapshot/1 function - and use that as a pre-filter so
|
|
|
|
%% that any value that was written more recently than the last flush to disk
|
|
|
|
%% of the Ledger will not be considered for compaction (as this may be
|
|
|
|
%% required to reload the Ledger on startup).
|
2016-10-05 18:28:31 +01:00
|
|
|
ink_compactjournal(Pid, Bookie, Timeout) ->
|
2016-09-28 18:26:52 +01:00
|
|
|
CheckerInitiateFun = fun initiate_penciller_snapshot/1,
|
2017-05-26 10:51:30 +01:00
|
|
|
CheckerCloseFun = fun leveled_penciller:pcl_close/1,
|
2016-09-28 18:26:52 +01:00
|
|
|
CheckerFilterFun = fun leveled_penciller:pcl_checksequencenumber/3,
|
|
|
|
gen_server:call(Pid,
|
|
|
|
{compact,
|
2016-10-05 18:28:31 +01:00
|
|
|
Bookie,
|
2016-09-28 18:26:52 +01:00
|
|
|
CheckerInitiateFun,
|
2017-05-26 10:51:30 +01:00
|
|
|
CheckerCloseFun,
|
2016-09-28 18:26:52 +01:00
|
|
|
CheckerFilterFun,
|
|
|
|
Timeout},
|
2016-10-03 23:34:28 +01:00
|
|
|
infinity).
|
2016-09-28 18:26:52 +01:00
|
|
|
|
|
|
|
%% Allows the Checker to be overriden in test, use something other than a
|
|
|
|
%% penciller
|
2017-05-26 10:51:30 +01:00
|
|
|
ink_compactjournal(Pid, Checker, InitiateFun, CloseFun, FilterFun, Timeout) ->
|
2016-09-28 18:26:52 +01:00
|
|
|
gen_server:call(Pid,
|
|
|
|
{compact,
|
|
|
|
Checker,
|
|
|
|
InitiateFun,
|
2017-05-26 10:51:30 +01:00
|
|
|
CloseFun,
|
2016-09-28 18:26:52 +01:00
|
|
|
FilterFun,
|
|
|
|
Timeout},
|
|
|
|
infinity).
|
2017-11-08 11:20:22 +00:00
|
|
|
|
2017-05-22 15:31:42 +01:00
|
|
|
-spec ink_compactioncomplete(pid()) -> ok.
|
|
|
|
%% @doc
|
|
|
|
%% Used by a clerk to state that a compaction process is over, only change
|
|
|
|
%% is to unlock the Inker for further compactions.
|
2016-10-03 23:34:28 +01:00
|
|
|
ink_compactioncomplete(Pid) ->
|
|
|
|
gen_server:call(Pid, compaction_complete, infinity).
|
|
|
|
|
2017-05-22 15:31:42 +01:00
|
|
|
-spec ink_compactionpending(pid()) -> boolean().
|
|
|
|
%% @doc
|
|
|
|
%% Is there ongoing compaction work? No compaction work should be initiated
|
|
|
|
%5 if there is already some compaction work ongoing.
|
2016-10-26 20:39:16 +01:00
|
|
|
ink_compactionpending(Pid) ->
|
|
|
|
gen_server:call(Pid, compaction_pending, infinity).
|
2017-05-22 15:31:42 +01:00
|
|
|
|
|
|
|
-spec ink_getmanifest(pid()) -> list().
|
|
|
|
%% @doc
|
|
|
|
%% Allows the clerk to fetch the manifest at the point it starts a compaction
|
|
|
|
%% job
|
2016-09-27 14:58:26 +01:00
|
|
|
ink_getmanifest(Pid) ->
|
|
|
|
gen_server:call(Pid, get_manifest, infinity).
|
|
|
|
|
2017-05-22 15:31:42 +01:00
|
|
|
-spec ink_updatemanifest(pid(), list(), list()) -> {ok, integer()}.
|
|
|
|
%% @doc
|
|
|
|
%% Add a section of new entries into the manifest, and drop a bunch of deleted
|
|
|
|
%% files out of the manifest. Used to update the manifest after a compaction
|
|
|
|
%% job.
|
|
|
|
%%
|
|
|
|
%% Returns {ok, ManSQN} with the ManSQN being the sequence number of the
|
|
|
|
%% updated manifest
|
2016-10-03 23:34:28 +01:00
|
|
|
ink_updatemanifest(Pid, ManifestSnippet, DeletedFiles) ->
|
2016-09-27 14:58:26 +01:00
|
|
|
gen_server:call(Pid,
|
|
|
|
{update_manifest,
|
|
|
|
ManifestSnippet,
|
|
|
|
DeletedFiles},
|
|
|
|
infinity).
|
|
|
|
|
2017-05-22 15:31:42 +01:00
|
|
|
-spec ink_printmanifest(pid()) -> ok.
|
|
|
|
%% @doc
|
|
|
|
%% Used in tests to print out the manifest
|
|
|
|
ink_printmanifest(Pid) ->
|
2016-09-07 17:58:12 +01:00
|
|
|
gen_server:call(Pid, print_manifest, infinity).
|
|
|
|
|
2016-09-05 15:01:23 +01:00
|
|
|
%%%============================================================================
|
|
|
|
%%% gen_server callbacks
|
|
|
|
%%%============================================================================
|
|
|
|
|
2016-09-07 17:58:12 +01:00
|
|
|
init([InkerOpts]) ->
|
2017-07-31 20:20:39 +02:00
|
|
|
leveled_rand:seed(),
|
2016-09-23 18:50:29 +01:00
|
|
|
case {InkerOpts#inker_options.root_path,
|
|
|
|
InkerOpts#inker_options.start_snapshot} of
|
|
|
|
{undefined, true} ->
|
|
|
|
SrcInker = InkerOpts#inker_options.source_inker,
|
2016-10-05 09:54:53 +01:00
|
|
|
{Manifest,
|
2016-10-07 18:07:03 +01:00
|
|
|
ActiveJournalDB} = ink_registersnapshot(SrcInker, self()),
|
2016-09-23 18:50:29 +01:00
|
|
|
{ok, #state{manifest=Manifest,
|
2016-10-03 23:34:28 +01:00
|
|
|
active_journaldb=ActiveJournalDB,
|
2016-10-07 10:04:48 +01:00
|
|
|
source_inker=SrcInker,
|
2016-10-03 23:34:28 +01:00
|
|
|
is_snapshot=true}};
|
2016-09-23 18:50:29 +01:00
|
|
|
%% Need to do something about timeout
|
|
|
|
{_RootPath, false} ->
|
|
|
|
start_from_file(InkerOpts)
|
|
|
|
end.
|
2016-09-05 15:01:23 +01:00
|
|
|
|
|
|
|
|
2016-10-07 18:07:03 +01:00
|
|
|
handle_call({put, Key, Object, KeyChanges}, _From, State) ->
|
2016-09-05 15:01:23 +01:00
|
|
|
case put_object(Key, Object, KeyChanges, State) of
|
2017-01-19 09:47:56 +00:00
|
|
|
{_, UpdState, ObjSize} ->
|
2016-10-07 18:07:03 +01:00
|
|
|
{reply, {ok, UpdState#state.journal_sqn, ObjSize}, UpdState}
|
2016-09-05 15:01:23 +01:00
|
|
|
end;
|
2016-09-15 10:53:24 +01:00
|
|
|
handle_call({fetch, Key, SQN}, _From, State) ->
|
2017-03-29 15:37:04 +01:00
|
|
|
case get_object(Key, SQN, State#state.manifest, true) of
|
2016-09-15 10:53:24 +01:00
|
|
|
{{SQN, Key}, {Value, _IndexSpecs}} ->
|
|
|
|
{reply, {ok, Value}, State};
|
|
|
|
Other ->
|
2016-11-03 16:05:43 +00:00
|
|
|
leveled_log:log("I0001", [Key, SQN, Other]),
|
2016-09-15 10:53:24 +01:00
|
|
|
{reply, not_present, State}
|
|
|
|
end;
|
2016-09-05 15:01:23 +01:00
|
|
|
handle_call({get, Key, SQN}, _From, State) ->
|
2016-10-07 18:07:03 +01:00
|
|
|
{reply, get_object(Key, SQN, State#state.manifest), State};
|
2016-10-31 17:26:28 +00:00
|
|
|
handle_call({key_check, Key, SQN}, _From, State) ->
|
|
|
|
{reply, key_check(Key, SQN, State#state.manifest), State};
|
2016-09-15 10:53:24 +01:00
|
|
|
handle_call({load_pcl, StartSQN, FilterFun, Penciller}, _From, State) ->
|
2017-01-19 12:23:28 +00:00
|
|
|
Manifest = lists:reverse(leveled_imanifest:to_list(State#state.manifest)),
|
2016-09-15 10:53:24 +01:00
|
|
|
Reply = load_from_sequence(StartSQN, FilterFun, Penciller, Manifest),
|
|
|
|
{reply, Reply, State};
|
2016-09-26 10:55:08 +01:00
|
|
|
handle_call({register_snapshot, Requestor}, _From , State) ->
|
|
|
|
Rs = [{Requestor,
|
|
|
|
State#state.manifest_sqn}|State#state.registered_snapshots],
|
2016-11-03 16:05:43 +00:00
|
|
|
leveled_log:log("I0002", [Requestor, State#state.manifest_sqn]),
|
2016-09-06 17:17:31 +01:00
|
|
|
{reply, {State#state.manifest,
|
2016-10-07 18:07:03 +01:00
|
|
|
State#state.active_journaldb},
|
2016-09-26 10:55:08 +01:00
|
|
|
State#state{registered_snapshots=Rs}};
|
2016-10-26 20:39:16 +01:00
|
|
|
handle_call({confirm_delete, ManSQN}, _From, State) ->
|
|
|
|
Reply = lists:foldl(fun({_R, SnapSQN}, Bool) ->
|
2016-11-04 15:56:57 +00:00
|
|
|
case SnapSQN >= ManSQN of
|
2016-10-26 20:39:16 +01:00
|
|
|
true ->
|
|
|
|
Bool;
|
|
|
|
false ->
|
|
|
|
false
|
|
|
|
end end,
|
|
|
|
true,
|
|
|
|
State#state.registered_snapshots),
|
|
|
|
{reply, Reply, State};
|
2016-09-27 14:58:26 +01:00
|
|
|
handle_call(get_manifest, _From, State) ->
|
2017-01-19 12:23:28 +00:00
|
|
|
{reply, leveled_imanifest:to_list(State#state.manifest), State};
|
2016-09-27 14:58:26 +01:00
|
|
|
handle_call({update_manifest,
|
|
|
|
ManifestSnippet,
|
|
|
|
DeletedFiles}, _From, State) ->
|
2017-01-19 12:23:28 +00:00
|
|
|
DropFun =
|
|
|
|
fun(E, Acc) ->
|
|
|
|
leveled_imanifest:remove_entry(Acc, E)
|
|
|
|
end,
|
|
|
|
Man0 = lists:foldl(DropFun, State#state.manifest, DeletedFiles),
|
|
|
|
AddFun =
|
|
|
|
fun(E, Acc) ->
|
|
|
|
leveled_imanifest:add_entry(Acc, E, false)
|
|
|
|
end,
|
|
|
|
Man1 = lists:foldl(AddFun, Man0, ManifestSnippet),
|
2016-09-27 14:58:26 +01:00
|
|
|
NewManifestSQN = State#state.manifest_sqn + 1,
|
2017-01-19 12:23:28 +00:00
|
|
|
leveled_imanifest:printer(Man1),
|
|
|
|
leveled_imanifest:writer(Man1, NewManifestSQN, State#state.root_path),
|
2016-10-26 20:39:16 +01:00
|
|
|
{reply,
|
|
|
|
{ok, NewManifestSQN},
|
|
|
|
State#state{manifest=Man1,
|
|
|
|
manifest_sqn=NewManifestSQN,
|
|
|
|
pending_removals=DeletedFiles}};
|
2016-09-07 17:58:12 +01:00
|
|
|
handle_call(print_manifest, _From, State) ->
|
2017-01-19 12:23:28 +00:00
|
|
|
leveled_imanifest:printer(State#state.manifest),
|
2016-09-07 17:58:12 +01:00
|
|
|
{reply, ok, State};
|
2016-09-28 18:26:52 +01:00
|
|
|
handle_call({compact,
|
|
|
|
Checker,
|
|
|
|
InitiateFun,
|
2017-05-26 10:51:30 +01:00
|
|
|
CloseFun,
|
2016-09-28 18:26:52 +01:00
|
|
|
FilterFun,
|
|
|
|
Timeout},
|
|
|
|
_From, State) ->
|
2016-09-27 14:58:26 +01:00
|
|
|
leveled_iclerk:clerk_compact(State#state.clerk,
|
2016-09-28 18:26:52 +01:00
|
|
|
Checker,
|
|
|
|
InitiateFun,
|
2017-05-26 10:51:30 +01:00
|
|
|
CloseFun,
|
2016-09-28 18:26:52 +01:00
|
|
|
FilterFun,
|
2016-09-27 14:58:26 +01:00
|
|
|
self(),
|
|
|
|
Timeout),
|
2016-10-03 23:34:28 +01:00
|
|
|
{reply, ok, State#state{compaction_pending=true}};
|
|
|
|
handle_call(compaction_complete, _From, State) ->
|
|
|
|
{reply, ok, State#state{compaction_pending=false}};
|
2016-10-26 20:39:16 +01:00
|
|
|
handle_call(compaction_pending, _From, State) ->
|
|
|
|
{reply, State#state.compaction_pending, State};
|
2016-10-30 22:06:44 +00:00
|
|
|
handle_call(close, _From, State) ->
|
2016-11-21 12:34:40 +00:00
|
|
|
{stop, normal, ok, State};
|
|
|
|
handle_call(doom, _From, State) ->
|
|
|
|
FPs = [filepath(State#state.root_path, journal_dir),
|
|
|
|
filepath(State#state.root_path, manifest_dir),
|
|
|
|
filepath(State#state.root_path, journal_compact_dir),
|
|
|
|
filepath(State#state.root_path, journal_waste_dir)],
|
|
|
|
leveled_log:log("I0018", []),
|
|
|
|
{stop, normal, {ok, FPs}, State}.
|
2016-09-05 15:01:23 +01:00
|
|
|
|
2016-11-14 19:34:11 +00:00
|
|
|
handle_cast({release_snapshot, Snapshot}, State) ->
|
|
|
|
Rs = lists:keydelete(Snapshot, 1, State#state.registered_snapshots),
|
|
|
|
leveled_log:log("I0003", [Snapshot]),
|
|
|
|
leveled_log:log("I0004", [length(Rs)]),
|
|
|
|
{noreply, State#state{registered_snapshots=Rs}}.
|
2016-09-05 15:01:23 +01:00
|
|
|
|
|
|
|
handle_info(_Info, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
2016-09-06 17:17:31 +01:00
|
|
|
terminate(Reason, State) ->
|
2016-10-03 23:34:28 +01:00
|
|
|
case State#state.is_snapshot of
|
|
|
|
true ->
|
2016-10-07 10:04:48 +01:00
|
|
|
ok = ink_releasesnapshot(State#state.source_inker, self());
|
2016-10-03 23:34:28 +01:00
|
|
|
false ->
|
2016-11-03 16:05:43 +00:00
|
|
|
leveled_log:log("I0005", [Reason]),
|
|
|
|
leveled_log:log("I0006", [State#state.journal_sqn,
|
|
|
|
State#state.manifest_sqn]),
|
2016-10-03 23:34:28 +01:00
|
|
|
leveled_iclerk:clerk_stop(State#state.clerk),
|
|
|
|
lists:foreach(fun({Snap, _SQN}) -> ok = ink_close(Snap) end,
|
|
|
|
State#state.registered_snapshots),
|
2016-11-03 16:05:43 +00:00
|
|
|
leveled_log:log("I0007", []),
|
2017-01-19 12:23:28 +00:00
|
|
|
leveled_imanifest:printer(State#state.manifest),
|
|
|
|
ManAsList = leveled_imanifest:to_list(State#state.manifest),
|
|
|
|
ok = close_allmanifest(ManAsList)
|
2016-10-03 23:34:28 +01:00
|
|
|
end.
|
2016-09-05 15:01:23 +01:00
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
|
|
|
|
%%%============================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%============================================================================
|
|
|
|
|
2016-10-25 23:13:14 +01:00
|
|
|
start_from_file(InkOpts) ->
|
|
|
|
RootPath = InkOpts#inker_options.root_path,
|
|
|
|
CDBopts = InkOpts#inker_options.cdb_options,
|
2016-11-14 11:17:14 +00:00
|
|
|
|
2016-09-23 18:50:29 +01:00
|
|
|
JournalFP = filepath(RootPath, journal_dir),
|
2016-10-07 18:07:03 +01:00
|
|
|
filelib:ensure_dir(JournalFP),
|
|
|
|
CompactFP = filepath(RootPath, journal_compact_dir),
|
|
|
|
filelib:ensure_dir(CompactFP),
|
2016-11-14 11:17:14 +00:00
|
|
|
WasteFP = filepath(RootPath, journal_waste_dir),
|
|
|
|
filelib:ensure_dir(WasteFP),
|
2016-09-23 18:50:29 +01:00
|
|
|
ManifestFP = filepath(RootPath, manifest_dir),
|
2016-10-29 00:52:49 +01:00
|
|
|
ok = filelib:ensure_dir(ManifestFP),
|
2016-11-14 11:17:14 +00:00
|
|
|
|
2016-10-29 00:52:49 +01:00
|
|
|
{ok, ManifestFilenames} = file:list_dir(ManifestFP),
|
2016-09-27 14:58:26 +01:00
|
|
|
|
2016-11-14 11:17:14 +00:00
|
|
|
IClerkCDBOpts = CDBopts#cdb_options{file_path = CompactFP,
|
|
|
|
waste_path = WasteFP},
|
2016-10-25 23:13:14 +01:00
|
|
|
ReloadStrategy = InkOpts#inker_options.reload_strategy,
|
2016-10-29 00:52:49 +01:00
|
|
|
MRL = InkOpts#inker_options.max_run_length,
|
2016-11-14 11:17:14 +00:00
|
|
|
WRP = InkOpts#inker_options.waste_retention_period,
|
2017-11-06 18:44:08 +00:00
|
|
|
PressMethod = InkOpts#inker_options.compression_method,
|
|
|
|
PressOnReceipt = InkOpts#inker_options.compress_on_receipt,
|
2016-09-27 14:58:26 +01:00
|
|
|
IClerkOpts = #iclerk_options{inker = self(),
|
2016-10-25 23:13:14 +01:00
|
|
|
cdb_options=IClerkCDBOpts,
|
2016-11-14 11:17:14 +00:00
|
|
|
waste_retention_period = WRP,
|
2016-10-29 00:52:49 +01:00
|
|
|
reload_strategy = ReloadStrategy,
|
2017-11-06 18:44:08 +00:00
|
|
|
compression_method = PressMethod,
|
2016-10-29 00:52:49 +01:00
|
|
|
max_run_length = MRL},
|
2016-09-27 14:58:26 +01:00
|
|
|
{ok, Clerk} = leveled_iclerk:clerk_new(IClerkOpts),
|
|
|
|
|
2016-09-23 18:50:29 +01:00
|
|
|
{Manifest,
|
2016-10-07 18:07:03 +01:00
|
|
|
ManifestSQN,
|
2016-09-23 18:50:29 +01:00
|
|
|
JournalSQN,
|
2016-10-07 18:07:03 +01:00
|
|
|
ActiveJournal} = build_manifest(ManifestFilenames,
|
2016-09-23 18:50:29 +01:00
|
|
|
RootPath,
|
|
|
|
CDBopts),
|
2016-10-07 18:07:03 +01:00
|
|
|
{ok, #state{manifest = Manifest,
|
2016-09-23 18:50:29 +01:00
|
|
|
manifest_sqn = ManifestSQN,
|
|
|
|
journal_sqn = JournalSQN,
|
|
|
|
active_journaldb = ActiveJournal,
|
|
|
|
root_path = RootPath,
|
2016-11-14 11:17:14 +00:00
|
|
|
cdb_options = CDBopts#cdb_options{waste_path=WasteFP},
|
2017-11-06 18:44:08 +00:00
|
|
|
compression_method = PressMethod,
|
|
|
|
compress_on_receipt = PressOnReceipt,
|
2016-09-26 10:55:08 +01:00
|
|
|
clerk = Clerk}}.
|
2016-09-23 18:50:29 +01:00
|
|
|
|
|
|
|
|
2016-10-25 23:13:14 +01:00
|
|
|
put_object(LedgerKey, Object, KeyChanges, State) ->
|
2016-09-05 15:01:23 +01:00
|
|
|
NewSQN = State#state.journal_sqn + 1,
|
2017-01-19 09:47:56 +00:00
|
|
|
ActiveJournal = State#state.active_journaldb,
|
2017-11-06 15:54:58 +00:00
|
|
|
{JournalKey, JournalBin} =
|
|
|
|
leveled_codec:to_inkerkv(LedgerKey,
|
|
|
|
NewSQN,
|
|
|
|
Object,
|
|
|
|
KeyChanges,
|
2017-11-06 18:44:08 +00:00
|
|
|
State#state.compression_method,
|
|
|
|
State#state.compress_on_receipt),
|
2017-01-19 09:47:56 +00:00
|
|
|
case leveled_cdb:cdb_put(ActiveJournal,
|
2016-10-25 23:13:14 +01:00
|
|
|
JournalKey,
|
2016-10-26 11:39:27 +01:00
|
|
|
JournalBin) of
|
2016-09-05 15:01:23 +01:00
|
|
|
ok ->
|
2016-12-20 23:11:50 +00:00
|
|
|
{ok,
|
2017-02-26 19:07:55 +00:00
|
|
|
State#state{journal_sqn=NewSQN},
|
2016-12-20 23:11:50 +00:00
|
|
|
byte_size(JournalBin)};
|
2016-09-05 15:01:23 +01:00
|
|
|
roll ->
|
2016-12-20 23:16:52 +00:00
|
|
|
SWroll = os:timestamp(),
|
2017-01-19 09:47:56 +00:00
|
|
|
LastKey = leveled_cdb:cdb_lastkey(ActiveJournal),
|
|
|
|
ok = leveled_cdb:cdb_roll(ActiveJournal),
|
|
|
|
Manifest0 = leveled_imanifest:append_lastkey(State#state.manifest,
|
|
|
|
ActiveJournal,
|
|
|
|
LastKey),
|
2016-09-07 17:58:12 +01:00
|
|
|
CDBopts = State#state.cdb_options,
|
2016-10-07 18:07:03 +01:00
|
|
|
ManEntry = start_new_activejournal(NewSQN,
|
|
|
|
State#state.root_path,
|
|
|
|
CDBopts),
|
2017-01-18 15:23:06 +00:00
|
|
|
{_, _, NewJournalP, _} = ManEntry,
|
2017-01-19 12:23:28 +00:00
|
|
|
Manifest1 = leveled_imanifest:add_entry(Manifest0, ManEntry, true),
|
|
|
|
ok = leveled_imanifest:writer(Manifest1,
|
|
|
|
State#state.manifest_sqn + 1,
|
|
|
|
State#state.root_path),
|
2016-10-25 01:57:12 +01:00
|
|
|
ok = leveled_cdb:cdb_put(NewJournalP,
|
2016-10-25 23:13:14 +01:00
|
|
|
JournalKey,
|
2016-10-26 11:39:27 +01:00
|
|
|
JournalBin),
|
2016-12-20 23:16:52 +00:00
|
|
|
leveled_log:log_timer("I0008", [], SWroll),
|
2016-10-07 18:07:03 +01:00
|
|
|
{rolling,
|
|
|
|
State#state{journal_sqn=NewSQN,
|
2017-01-19 09:47:56 +00:00
|
|
|
manifest=Manifest1,
|
2016-10-07 18:07:03 +01:00
|
|
|
manifest_sqn = State#state.manifest_sqn + 1,
|
|
|
|
active_journaldb=NewJournalP},
|
2016-10-25 23:13:14 +01:00
|
|
|
byte_size(JournalBin)}
|
2016-10-23 22:45:43 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2016-10-25 23:13:14 +01:00
|
|
|
get_object(LedgerKey, SQN, Manifest) ->
|
2017-03-29 15:37:04 +01:00
|
|
|
get_object(LedgerKey, SQN, Manifest, false).
|
|
|
|
|
|
|
|
get_object(LedgerKey, SQN, Manifest, ToIgnoreKeyChanges) ->
|
2017-01-17 16:30:04 +00:00
|
|
|
JournalP = leveled_imanifest:find_entry(SQN, Manifest),
|
2017-11-06 15:54:58 +00:00
|
|
|
{InkerKey, _V, true} =
|
2017-11-06 18:44:08 +00:00
|
|
|
leveled_codec:to_inkerkv(LedgerKey, SQN, to_fetch),
|
2016-10-25 23:13:14 +01:00
|
|
|
Obj = leveled_cdb:cdb_get(JournalP, InkerKey),
|
2017-03-29 15:37:04 +01:00
|
|
|
leveled_codec:from_inkerkv(Obj, ToIgnoreKeyChanges).
|
2016-09-07 17:58:12 +01:00
|
|
|
|
2016-10-31 17:26:28 +00:00
|
|
|
key_check(LedgerKey, SQN, Manifest) ->
|
2017-01-17 16:30:04 +00:00
|
|
|
JournalP = leveled_imanifest:find_entry(SQN, Manifest),
|
2017-11-06 15:54:58 +00:00
|
|
|
{InkerKey, _V, true} =
|
2017-11-06 18:44:08 +00:00
|
|
|
leveled_codec:to_inkerkv(LedgerKey, SQN, to_fetch),
|
2016-10-31 17:26:28 +00:00
|
|
|
leveled_cdb:cdb_keycheck(JournalP, InkerKey).
|
2016-09-05 15:01:23 +01:00
|
|
|
|
|
|
|
build_manifest(ManifestFilenames,
|
2016-09-07 17:58:12 +01:00
|
|
|
RootPath,
|
|
|
|
CDBopts) ->
|
2016-10-07 18:07:03 +01:00
|
|
|
% Find the manifest with a highest Manifest sequence number
|
|
|
|
% Open it and read it to get the current Confirmed Manifest
|
2017-01-19 12:23:28 +00:00
|
|
|
ManifestRegex = "(?<MSQN>[0-9]+)\\." ++ leveled_imanifest:complete_filex(),
|
2016-09-05 15:01:23 +01:00
|
|
|
ValidManSQNs = sequencenumbers_fromfilenames(ManifestFilenames,
|
|
|
|
ManifestRegex,
|
|
|
|
'MSQN'),
|
2016-10-07 18:07:03 +01:00
|
|
|
{Manifest,
|
2016-09-05 15:01:23 +01:00
|
|
|
ManifestSQN} = case length(ValidManSQNs) of
|
|
|
|
0 ->
|
2016-10-07 18:07:03 +01:00
|
|
|
{[], 1};
|
2016-09-05 15:01:23 +01:00
|
|
|
_ ->
|
|
|
|
PersistedManSQN = lists:max(ValidManSQNs),
|
2017-01-19 12:23:28 +00:00
|
|
|
M1 = leveled_imanifest:reader(PersistedManSQN,
|
2016-10-07 18:07:03 +01:00
|
|
|
RootPath),
|
|
|
|
{M1, PersistedManSQN}
|
2016-09-05 15:01:23 +01:00
|
|
|
end,
|
|
|
|
|
2016-10-07 18:07:03 +01:00
|
|
|
% Open the manifest files, completing if necessary and ensure there is
|
|
|
|
% a valid active journal at the head of the manifest
|
|
|
|
OpenManifest = open_all_manifest(Manifest, RootPath, CDBopts),
|
2017-01-17 16:30:04 +00:00
|
|
|
|
2017-01-19 12:23:28 +00:00
|
|
|
{ActiveLowSQN,
|
|
|
|
_FN,
|
|
|
|
ActiveJournal,
|
|
|
|
_LK} = leveled_imanifest:head_entry(OpenManifest),
|
2016-10-07 18:07:03 +01:00
|
|
|
JournalSQN = case leveled_cdb:cdb_lastkey(ActiveJournal) of
|
|
|
|
empty ->
|
|
|
|
ActiveLowSQN;
|
2016-10-25 01:57:12 +01:00
|
|
|
{JSQN, _Type, _LastKey} ->
|
2016-10-07 18:07:03 +01:00
|
|
|
JSQN
|
|
|
|
end,
|
2016-09-05 15:01:23 +01:00
|
|
|
|
2016-10-07 18:07:03 +01:00
|
|
|
% Update the manifest if it has been changed by the process of laoding
|
|
|
|
% the manifest (must also increment the manifest SQN).
|
2017-01-17 16:30:04 +00:00
|
|
|
UpdManifestSQN =
|
|
|
|
if
|
|
|
|
length(OpenManifest) > length(Manifest) ->
|
|
|
|
leveled_log:log("I0009", []),
|
2017-01-19 12:23:28 +00:00
|
|
|
leveled_imanifest:printer(OpenManifest),
|
2017-01-17 16:30:04 +00:00
|
|
|
NextSQN = ManifestSQN + 1,
|
2017-01-19 12:23:28 +00:00
|
|
|
leveled_imanifest:writer(OpenManifest, NextSQN, RootPath),
|
2017-01-17 16:30:04 +00:00
|
|
|
NextSQN;
|
|
|
|
true ->
|
|
|
|
leveled_log:log("I0010", []),
|
2017-01-19 12:23:28 +00:00
|
|
|
leveled_imanifest:printer(OpenManifest),
|
2017-01-17 16:30:04 +00:00
|
|
|
ManifestSQN
|
|
|
|
end,
|
2016-10-07 18:07:03 +01:00
|
|
|
{OpenManifest, UpdManifestSQN, JournalSQN, ActiveJournal}.
|
|
|
|
|
2016-09-05 15:01:23 +01:00
|
|
|
|
2016-10-07 18:07:03 +01:00
|
|
|
close_allmanifest([]) ->
|
|
|
|
ok;
|
|
|
|
close_allmanifest([H|ManifestT]) ->
|
2017-01-17 16:30:04 +00:00
|
|
|
{_, _, Pid, _} = H,
|
2016-09-06 17:17:31 +01:00
|
|
|
ok = leveled_cdb:cdb_close(Pid),
|
2016-10-07 18:07:03 +01:00
|
|
|
close_allmanifest(ManifestT).
|
|
|
|
|
|
|
|
|
|
|
|
open_all_manifest([], RootPath, CDBOpts) ->
|
2016-11-03 16:05:43 +00:00
|
|
|
leveled_log:log("I0011", []),
|
2017-01-19 12:23:28 +00:00
|
|
|
leveled_imanifest:add_entry([],
|
|
|
|
start_new_activejournal(1, RootPath, CDBOpts),
|
|
|
|
true);
|
2016-10-07 18:07:03 +01:00
|
|
|
open_all_manifest(Man0, RootPath, CDBOpts) ->
|
2017-01-19 12:23:28 +00:00
|
|
|
Man1 = leveled_imanifest:to_list(Man0),
|
2017-01-17 16:30:04 +00:00
|
|
|
[{HeadSQN, HeadFN, _IgnorePid, HeadLK}|ManifestTail] = Man1,
|
|
|
|
OpenJournalFun =
|
|
|
|
fun(ManEntry) ->
|
2017-02-26 22:46:57 +00:00
|
|
|
{LowSQN, FN, _, LK_RO} = ManEntry,
|
|
|
|
CFN = FN ++ "." ++ ?JOURNAL_FILEX,
|
|
|
|
PFN = FN ++ "." ++ ?PENDING_FILEX,
|
|
|
|
case filelib:is_file(CFN) of
|
|
|
|
true ->
|
|
|
|
{ok, Pid} = leveled_cdb:cdb_reopen_reader(CFN,
|
|
|
|
LK_RO),
|
|
|
|
{LowSQN, FN, Pid, LK_RO};
|
|
|
|
false ->
|
|
|
|
W = leveled_cdb:cdb_open_writer(PFN, CDBOpts),
|
|
|
|
{ok, Pid} = W,
|
|
|
|
ok = leveled_cdb:cdb_roll(Pid),
|
|
|
|
LK_WR = leveled_cdb:cdb_lastkey(Pid),
|
|
|
|
{LowSQN, FN, Pid, LK_WR}
|
|
|
|
end
|
2017-01-17 16:30:04 +00:00
|
|
|
end,
|
2017-01-19 12:23:28 +00:00
|
|
|
OpenedTailAsList = lists:map(OpenJournalFun, ManifestTail),
|
|
|
|
OpenedTail = leveled_imanifest:from_list(OpenedTailAsList),
|
2017-01-18 15:23:06 +00:00
|
|
|
CompleteHeadFN = HeadFN ++ "." ++ ?JOURNAL_FILEX,
|
|
|
|
PendingHeadFN = HeadFN ++ "." ++ ?PENDING_FILEX,
|
|
|
|
case filelib:is_file(CompleteHeadFN) of
|
|
|
|
true ->
|
|
|
|
leveled_log:log("I0012", [HeadFN]),
|
|
|
|
{ok, HeadR} = leveled_cdb:cdb_open_reader(CompleteHeadFN),
|
|
|
|
LastKey = leveled_cdb:cdb_lastkey(HeadR),
|
|
|
|
LastSQN = element(1, LastKey),
|
|
|
|
ManToHead = leveled_imanifest:add_entry(OpenedTail,
|
|
|
|
{HeadSQN,
|
|
|
|
HeadFN,
|
|
|
|
HeadR,
|
2017-01-19 12:23:28 +00:00
|
|
|
LastKey},
|
|
|
|
true),
|
2017-01-18 15:23:06 +00:00
|
|
|
NewManEntry = start_new_activejournal(LastSQN + 1,
|
|
|
|
RootPath,
|
|
|
|
CDBOpts),
|
2017-01-19 12:23:28 +00:00
|
|
|
leveled_imanifest:add_entry(ManToHead,
|
|
|
|
NewManEntry,
|
|
|
|
true);
|
2017-01-18 15:23:06 +00:00
|
|
|
false ->
|
|
|
|
{ok, HeadW} = leveled_cdb:cdb_open_writer(PendingHeadFN,
|
|
|
|
CDBOpts),
|
|
|
|
leveled_imanifest:add_entry(OpenedTail,
|
2017-01-19 12:23:28 +00:00
|
|
|
{HeadSQN, HeadFN, HeadW, HeadLK},
|
|
|
|
true)
|
2017-01-18 15:23:06 +00:00
|
|
|
end.
|
2016-10-07 18:07:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
start_new_activejournal(SQN, RootPath, CDBOpts) ->
|
|
|
|
Filename = filepath(RootPath, SQN, new_journal),
|
|
|
|
{ok, PidW} = leveled_cdb:cdb_open_writer(Filename, CDBOpts),
|
2017-01-17 16:30:04 +00:00
|
|
|
{SQN, Filename, PidW, empty}.
|
2016-09-05 15:01:23 +01:00
|
|
|
|
|
|
|
|
2016-09-15 10:53:24 +01:00
|
|
|
%% Scan between sequence numbers applying FilterFun to each entry where
|
|
|
|
%% FilterFun{K, V, Acc} -> Penciller Key List
|
|
|
|
%% Load the output for the CDB file into the Penciller.
|
|
|
|
|
2017-01-18 15:23:06 +00:00
|
|
|
load_from_sequence(_MinSQN, _FilterFun, _PCL, []) ->
|
2016-09-15 10:53:24 +01:00
|
|
|
ok;
|
2017-01-18 15:23:06 +00:00
|
|
|
load_from_sequence(MinSQN, FilterFun, PCL, [{LowSQN, FN, Pid, _LK}|Rest])
|
2016-09-19 15:31:26 +01:00
|
|
|
when LowSQN >= MinSQN ->
|
2016-10-05 09:54:53 +01:00
|
|
|
load_between_sequence(MinSQN,
|
|
|
|
MinSQN + ?LOADING_BATCH,
|
|
|
|
FilterFun,
|
2017-01-18 15:23:06 +00:00
|
|
|
PCL,
|
2016-10-05 09:54:53 +01:00
|
|
|
Pid,
|
|
|
|
undefined,
|
|
|
|
FN,
|
|
|
|
Rest);
|
2017-01-18 15:23:06 +00:00
|
|
|
load_from_sequence(MinSQN, FilterFun, PCL, [{_LowSQN, FN, Pid, _LK}|Rest]) ->
|
2016-10-05 09:54:53 +01:00
|
|
|
case Rest of
|
|
|
|
[] ->
|
|
|
|
load_between_sequence(MinSQN,
|
|
|
|
MinSQN + ?LOADING_BATCH,
|
2016-09-15 10:53:24 +01:00
|
|
|
FilterFun,
|
2017-01-18 15:23:06 +00:00
|
|
|
PCL,
|
2016-10-05 09:54:53 +01:00
|
|
|
Pid,
|
|
|
|
undefined,
|
|
|
|
FN,
|
|
|
|
Rest);
|
2017-01-19 09:47:56 +00:00
|
|
|
[{NextSQN, _NxtFN, _NxtPid, _NxtLK}|_Rest] when NextSQN > MinSQN ->
|
2016-10-05 09:54:53 +01:00
|
|
|
load_between_sequence(MinSQN,
|
|
|
|
MinSQN + ?LOADING_BATCH,
|
|
|
|
FilterFun,
|
2017-01-18 15:23:06 +00:00
|
|
|
PCL,
|
2016-10-05 09:54:53 +01:00
|
|
|
Pid,
|
|
|
|
undefined,
|
|
|
|
FN,
|
|
|
|
Rest);
|
|
|
|
_ ->
|
2017-01-18 15:23:06 +00:00
|
|
|
load_from_sequence(MinSQN, FilterFun, PCL, Rest)
|
2016-10-05 09:54:53 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
load_between_sequence(MinSQN, MaxSQN, FilterFun, Penciller,
|
|
|
|
CDBpid, StartPos, FN, Rest) ->
|
2016-11-03 16:05:43 +00:00
|
|
|
leveled_log:log("I0014", [FN, MinSQN]),
|
2016-12-11 01:02:56 +00:00
|
|
|
InitAcc = {MinSQN, MaxSQN, leveled_bookie:empty_ledgercache()},
|
2016-10-05 09:54:53 +01:00
|
|
|
Res = case leveled_cdb:cdb_scan(CDBpid, FilterFun, InitAcc, StartPos) of
|
2016-12-11 01:02:56 +00:00
|
|
|
{eof, {AccMinSQN, _AccMaxSQN, AccLC}} ->
|
|
|
|
ok = push_to_penciller(Penciller, AccLC),
|
2016-10-05 09:54:53 +01:00
|
|
|
{ok, AccMinSQN};
|
2016-12-11 01:02:56 +00:00
|
|
|
{LastPosition, {_AccMinSQN, _AccMaxSQN, AccLC}} ->
|
|
|
|
ok = push_to_penciller(Penciller, AccLC),
|
2016-10-05 09:54:53 +01:00
|
|
|
NextSQN = MaxSQN + 1,
|
|
|
|
load_between_sequence(NextSQN,
|
|
|
|
NextSQN + ?LOADING_BATCH,
|
|
|
|
FilterFun,
|
|
|
|
Penciller,
|
|
|
|
CDBpid,
|
|
|
|
LastPosition,
|
|
|
|
FN,
|
|
|
|
Rest)
|
|
|
|
end,
|
|
|
|
case Res of
|
|
|
|
{ok, LMSQN} ->
|
|
|
|
load_from_sequence(LMSQN, FilterFun, Penciller, Rest);
|
|
|
|
ok ->
|
|
|
|
ok
|
2016-09-15 10:53:24 +01:00
|
|
|
end.
|
2016-09-09 15:58:19 +01:00
|
|
|
|
2016-12-11 01:02:56 +00:00
|
|
|
push_to_penciller(Penciller, LedgerCache) ->
|
2016-10-20 02:23:45 +01:00
|
|
|
% The push to penciller must start as a tree to correctly de-duplicate
|
|
|
|
% the list by order before becoming a de-duplicated list for loading
|
2017-01-20 16:36:20 +00:00
|
|
|
LC0 = leveled_bookie:loadqueue_ledgercache(LedgerCache),
|
|
|
|
push_to_penciller_loop(Penciller, LC0).
|
|
|
|
|
|
|
|
push_to_penciller_loop(Penciller, LedgerCache) ->
|
2016-12-11 01:02:56 +00:00
|
|
|
case leveled_bookie:push_ledgercache(Penciller, LedgerCache) of
|
2016-10-30 18:25:30 +00:00
|
|
|
returned ->
|
2016-10-20 02:23:45 +01:00
|
|
|
timer:sleep(?LOADING_PAUSE),
|
2017-01-20 16:36:20 +00:00
|
|
|
push_to_penciller_loop(Penciller, LedgerCache);
|
2016-10-27 20:56:18 +01:00
|
|
|
ok ->
|
2016-09-15 10:53:24 +01:00
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2016-09-05 15:01:23 +01:00
|
|
|
|
|
|
|
sequencenumbers_fromfilenames(Filenames, Regex, IntName) ->
|
|
|
|
lists:foldl(fun(FN, Acc) ->
|
|
|
|
case re:run(FN,
|
|
|
|
Regex,
|
|
|
|
[{capture, [IntName], list}]) of
|
|
|
|
nomatch ->
|
|
|
|
Acc;
|
|
|
|
{match, [Int]} when is_list(Int) ->
|
2016-11-03 20:00:55 +00:00
|
|
|
Acc ++ [list_to_integer(Int)]
|
2016-09-05 15:01:23 +01:00
|
|
|
end end,
|
|
|
|
[],
|
|
|
|
Filenames).
|
|
|
|
|
|
|
|
|
|
|
|
filepath(RootPath, journal_dir) ->
|
|
|
|
RootPath ++ "/" ++ ?FILES_FP ++ "/";
|
|
|
|
filepath(RootPath, manifest_dir) ->
|
2016-09-27 14:58:26 +01:00
|
|
|
RootPath ++ "/" ++ ?MANIFEST_FP ++ "/";
|
|
|
|
filepath(RootPath, journal_compact_dir) ->
|
2016-11-14 11:17:14 +00:00
|
|
|
filepath(RootPath, journal_dir) ++ "/" ++ ?COMPACT_FP ++ "/";
|
|
|
|
filepath(RootPath, journal_waste_dir) ->
|
|
|
|
filepath(RootPath, journal_dir) ++ "/" ++ ?WASTE_FP ++ "/".
|
2016-09-05 15:01:23 +01:00
|
|
|
|
|
|
|
filepath(RootPath, NewSQN, new_journal) ->
|
|
|
|
filename:join(filepath(RootPath, journal_dir),
|
2016-10-07 18:07:03 +01:00
|
|
|
integer_to_list(NewSQN) ++ "_"
|
2016-10-18 01:59:03 +01:00
|
|
|
++ leveled_codec:generate_uuid()
|
2016-09-28 11:41:56 +01:00
|
|
|
++ "." ++ ?PENDING_FILEX);
|
|
|
|
filepath(CompactFilePath, NewSQN, compact_journal) ->
|
|
|
|
filename:join(CompactFilePath,
|
2016-10-07 18:07:03 +01:00
|
|
|
integer_to_list(NewSQN) ++ "_"
|
2016-10-18 01:59:03 +01:00
|
|
|
++ leveled_codec:generate_uuid()
|
2016-09-05 15:01:23 +01:00
|
|
|
++ "." ++ ?PENDING_FILEX).
|
|
|
|
|
2016-10-07 18:07:03 +01:00
|
|
|
|
2016-10-05 18:28:31 +01:00
|
|
|
initiate_penciller_snapshot(Bookie) ->
|
2017-10-17 20:39:11 +01:00
|
|
|
{ok, LedgerSnap, _} =
|
|
|
|
leveled_bookie:book_snapshot(Bookie, ledger, undefined, true),
|
2016-10-05 18:28:31 +01:00
|
|
|
MaxSQN = leveled_penciller:pcl_getstartupsequencenumber(LedgerSnap),
|
|
|
|
{LedgerSnap, MaxSQN}.
|
2016-09-28 18:26:52 +01:00
|
|
|
|
2016-09-05 15:01:23 +01:00
|
|
|
%%%============================================================================
|
|
|
|
%%% Test
|
|
|
|
%%%============================================================================
|
|
|
|
|
|
|
|
-ifdef(TEST).
|
|
|
|
|
2017-03-20 15:43:54 +00:00
|
|
|
create_value_for_journal(Obj, Comp) ->
|
2017-11-06 15:54:58 +00:00
|
|
|
leveled_codec:create_value_for_journal(Obj, Comp, native).
|
2017-03-20 15:43:54 +00:00
|
|
|
|
2016-09-05 15:01:23 +01:00
|
|
|
build_dummy_journal() ->
|
2016-10-25 23:13:14 +01:00
|
|
|
F = fun(X) -> X end,
|
|
|
|
build_dummy_journal(F).
|
|
|
|
|
|
|
|
build_dummy_journal(KeyConvertF) ->
|
2016-09-08 14:21:30 +01:00
|
|
|
RootPath = "../test/journal",
|
2016-09-28 18:26:52 +01:00
|
|
|
clean_testdir(RootPath),
|
2016-09-05 15:01:23 +01:00
|
|
|
JournalFP = filepath(RootPath, journal_dir),
|
|
|
|
ManifestFP = filepath(RootPath, manifest_dir),
|
|
|
|
ok = filelib:ensure_dir(RootPath),
|
|
|
|
ok = filelib:ensure_dir(JournalFP),
|
|
|
|
ok = filelib:ensure_dir(ManifestFP),
|
|
|
|
F1 = filename:join(JournalFP, "nursery_1.pnd"),
|
|
|
|
{ok, J1} = leveled_cdb:cdb_open_writer(F1),
|
2016-10-25 23:13:14 +01:00
|
|
|
{K1, V1} = {KeyConvertF("Key1"), "TestValue1"},
|
|
|
|
{K2, V2} = {KeyConvertF("Key2"), "TestValue2"},
|
2017-03-20 15:43:54 +00:00
|
|
|
ok = leveled_cdb:cdb_put(J1,
|
|
|
|
{1, stnd, K1},
|
|
|
|
create_value_for_journal({V1, []}, false)),
|
|
|
|
ok = leveled_cdb:cdb_put(J1,
|
|
|
|
{2, stnd, K2},
|
|
|
|
create_value_for_journal({V2, []}, false)),
|
2016-10-07 18:07:03 +01:00
|
|
|
ok = leveled_cdb:cdb_roll(J1),
|
2017-01-17 16:30:04 +00:00
|
|
|
LK1 = leveled_cdb:cdb_lastkey(J1),
|
2016-11-08 22:43:22 +00:00
|
|
|
lists:foldl(fun(X, Closed) ->
|
|
|
|
case Closed of
|
|
|
|
true -> true;
|
|
|
|
false ->
|
|
|
|
case leveled_cdb:cdb_checkhashtable(J1) of
|
|
|
|
true -> leveled_cdb:cdb_close(J1), true;
|
|
|
|
false -> timer:sleep(X), false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
false,
|
|
|
|
lists:seq(1, 5)),
|
2016-09-05 15:01:23 +01:00
|
|
|
F2 = filename:join(JournalFP, "nursery_3.pnd"),
|
|
|
|
{ok, J2} = leveled_cdb:cdb_open_writer(F2),
|
2016-10-25 23:13:14 +01:00
|
|
|
{K1, V3} = {KeyConvertF("Key1"), "TestValue3"},
|
|
|
|
{K4, V4} = {KeyConvertF("Key4"), "TestValue4"},
|
2017-03-20 15:43:54 +00:00
|
|
|
ok = leveled_cdb:cdb_put(J2,
|
|
|
|
{3, stnd, K1},
|
|
|
|
create_value_for_journal({V3, []}, false)),
|
|
|
|
ok = leveled_cdb:cdb_put(J2,
|
|
|
|
{4, stnd, K4},
|
|
|
|
create_value_for_journal({V4, []}, false)),
|
2017-01-17 16:30:04 +00:00
|
|
|
LK2 = leveled_cdb:cdb_lastkey(J2),
|
2016-09-05 15:01:23 +01:00
|
|
|
ok = leveled_cdb:cdb_close(J2),
|
2017-01-17 16:30:04 +00:00
|
|
|
Manifest = [{1, "../test/journal/journal_files/nursery_1", "pid1", LK1},
|
|
|
|
{3, "../test/journal/journal_files/nursery_3", "pid2", LK2}],
|
2016-09-05 15:01:23 +01:00
|
|
|
ManifestBin = term_to_binary(Manifest),
|
|
|
|
{ok, MF1} = file:open(filename:join(ManifestFP, "1.man"),
|
|
|
|
[binary, raw, read, write]),
|
|
|
|
ok = file:write(MF1, ManifestBin),
|
|
|
|
ok = file:close(MF1).
|
|
|
|
|
|
|
|
|
|
|
|
clean_testdir(RootPath) ->
|
|
|
|
clean_subdir(filepath(RootPath, journal_dir)),
|
2016-10-05 18:28:31 +01:00
|
|
|
clean_subdir(filepath(RootPath, journal_compact_dir)),
|
2016-09-05 15:01:23 +01:00
|
|
|
clean_subdir(filepath(RootPath, manifest_dir)).
|
|
|
|
|
|
|
|
clean_subdir(DirPath) ->
|
2016-10-03 23:34:28 +01:00
|
|
|
ok = filelib:ensure_dir(DirPath),
|
2016-09-05 15:01:23 +01:00
|
|
|
{ok, Files} = file:list_dir(DirPath),
|
2016-09-28 18:26:52 +01:00
|
|
|
lists:foreach(fun(FN) ->
|
|
|
|
File = filename:join(DirPath, FN),
|
|
|
|
case file:delete(File) of
|
|
|
|
ok -> io:format("Success deleting ~s~n", [File]);
|
|
|
|
_ -> io:format("Error deleting ~s~n", [File])
|
|
|
|
end
|
|
|
|
end,
|
2016-09-05 15:01:23 +01:00
|
|
|
Files).
|
|
|
|
|
2016-10-07 18:07:03 +01:00
|
|
|
simple_inker_test() ->
|
2016-09-08 14:21:30 +01:00
|
|
|
RootPath = "../test/journal",
|
2016-09-07 17:58:12 +01:00
|
|
|
build_dummy_journal(),
|
2017-03-20 15:43:54 +00:00
|
|
|
CDBopts = #cdb_options{max_size=300000, binary_mode=true},
|
2016-09-07 17:58:12 +01:00
|
|
|
{ok, Ink1} = ink_start(#inker_options{root_path=RootPath,
|
2017-11-06 15:54:58 +00:00
|
|
|
cdb_options=CDBopts,
|
2017-11-06 18:44:08 +00:00
|
|
|
compression_method=native,
|
|
|
|
compress_on_receipt=true}),
|
2016-10-07 18:07:03 +01:00
|
|
|
Obj1 = ink_get(Ink1, "Key1", 1),
|
|
|
|
?assertMatch({{1, "Key1"}, {"TestValue1", []}}, Obj1),
|
2017-03-20 15:43:54 +00:00
|
|
|
Obj3 = ink_get(Ink1, "Key1", 3),
|
|
|
|
?assertMatch({{3, "Key1"}, {"TestValue3", []}}, Obj3),
|
|
|
|
Obj4 = ink_get(Ink1, "Key4", 4),
|
|
|
|
?assertMatch({{4, "Key4"}, {"TestValue4", []}}, Obj4),
|
2016-09-07 17:58:12 +01:00
|
|
|
ink_close(Ink1),
|
|
|
|
clean_testdir(RootPath).
|
2016-10-08 22:15:48 +01:00
|
|
|
|
|
|
|
simple_inker_completeactivejournal_test() ->
|
|
|
|
RootPath = "../test/journal",
|
|
|
|
build_dummy_journal(),
|
2017-03-20 15:43:54 +00:00
|
|
|
CDBopts = #cdb_options{max_size=300000, binary_mode=true},
|
2016-10-16 16:58:55 +01:00
|
|
|
JournalFP = filepath(RootPath, journal_dir),
|
|
|
|
F2 = filename:join(JournalFP, "nursery_3.pnd"),
|
|
|
|
{ok, PidW} = leveled_cdb:cdb_open_writer(F2),
|
|
|
|
{ok, _F2} = leveled_cdb:cdb_complete(PidW),
|
2016-10-18 01:59:03 +01:00
|
|
|
F1 = filename:join(JournalFP, "nursery_1.cdb"),
|
|
|
|
F1r = filename:join(JournalFP, "nursery_1.pnd"),
|
|
|
|
ok = file:rename(F1, F1r),
|
2016-10-08 22:15:48 +01:00
|
|
|
{ok, Ink1} = ink_start(#inker_options{root_path=RootPath,
|
2017-11-06 15:54:58 +00:00
|
|
|
cdb_options=CDBopts,
|
2017-11-06 18:44:08 +00:00
|
|
|
compression_method=native,
|
|
|
|
compress_on_receipt=true}),
|
2016-10-08 22:15:48 +01:00
|
|
|
Obj1 = ink_get(Ink1, "Key1", 1),
|
|
|
|
?assertMatch({{1, "Key1"}, {"TestValue1", []}}, Obj1),
|
|
|
|
Obj2 = ink_get(Ink1, "Key4", 4),
|
|
|
|
?assertMatch({{4, "Key4"}, {"TestValue4", []}}, Obj2),
|
|
|
|
ink_close(Ink1),
|
|
|
|
clean_testdir(RootPath).
|
2016-09-07 17:58:12 +01:00
|
|
|
|
2016-10-25 23:13:14 +01:00
|
|
|
test_ledgerkey(Key) ->
|
|
|
|
{o, "Bucket", Key, null}.
|
|
|
|
|
2016-09-28 18:26:52 +01:00
|
|
|
compact_journal_test() ->
|
2017-11-08 11:20:22 +00:00
|
|
|
{timeout, 60, fun compact_journal_testto/0}.
|
|
|
|
|
|
|
|
compact_journal_testto() ->
|
2016-09-28 18:26:52 +01:00
|
|
|
RootPath = "../test/journal",
|
2016-10-25 23:13:14 +01:00
|
|
|
build_dummy_journal(fun test_ledgerkey/1),
|
2016-09-28 18:26:52 +01:00
|
|
|
CDBopts = #cdb_options{max_size=300000},
|
2016-10-25 23:13:14 +01:00
|
|
|
RStrategy = [{?STD_TAG, recovr}],
|
2016-09-28 18:26:52 +01:00
|
|
|
{ok, Ink1} = ink_start(#inker_options{root_path=RootPath,
|
2016-10-25 23:13:14 +01:00
|
|
|
cdb_options=CDBopts,
|
2017-11-06 15:54:58 +00:00
|
|
|
reload_strategy=RStrategy,
|
2017-11-06 18:44:08 +00:00
|
|
|
compression_method=native,
|
|
|
|
compress_on_receipt=false}),
|
2016-10-25 23:13:14 +01:00
|
|
|
{ok, NewSQN1, _ObjSize} = ink_put(Ink1,
|
|
|
|
test_ledgerkey("KeyAA"),
|
2017-05-22 15:31:42 +01:00
|
|
|
"TestValueAA",
|
|
|
|
{[], infinity}),
|
2016-09-28 18:26:52 +01:00
|
|
|
?assertMatch(NewSQN1, 5),
|
2017-05-22 15:31:42 +01:00
|
|
|
ok = ink_printmanifest(Ink1),
|
2016-10-25 23:13:14 +01:00
|
|
|
R0 = ink_get(Ink1, test_ledgerkey("KeyAA"), 5),
|
2017-05-22 15:31:42 +01:00
|
|
|
?assertMatch(R0,
|
|
|
|
{{5, test_ledgerkey("KeyAA")},
|
|
|
|
{"TestValueAA", {[], infinity}}}),
|
2016-10-07 18:07:03 +01:00
|
|
|
FunnyLoop = lists:seq(1, 48),
|
2016-09-28 18:26:52 +01:00
|
|
|
Checker = lists:map(fun(X) ->
|
|
|
|
PK = "KeyZ" ++ integer_to_list(X),
|
|
|
|
{ok, SQN, _} = ink_put(Ink1,
|
2016-10-25 23:13:14 +01:00
|
|
|
test_ledgerkey(PK),
|
2017-07-31 20:20:39 +02:00
|
|
|
leveled_rand:rand_bytes(10000),
|
2017-05-22 15:31:42 +01:00
|
|
|
{[], infinity}),
|
2016-10-25 23:13:14 +01:00
|
|
|
{SQN, test_ledgerkey(PK)}
|
2016-09-28 18:26:52 +01:00
|
|
|
end,
|
|
|
|
FunnyLoop),
|
2016-10-25 23:13:14 +01:00
|
|
|
{ok, NewSQN2, _ObjSize} = ink_put(Ink1,
|
|
|
|
test_ledgerkey("KeyBB"),
|
2017-05-22 15:31:42 +01:00
|
|
|
"TestValueBB",
|
|
|
|
{[], infinity}),
|
2016-09-28 18:26:52 +01:00
|
|
|
?assertMatch(NewSQN2, 54),
|
|
|
|
ActualManifest = ink_getmanifest(Ink1),
|
2017-05-22 15:31:42 +01:00
|
|
|
ok = ink_printmanifest(Ink1),
|
2016-10-07 18:07:03 +01:00
|
|
|
?assertMatch(3, length(ActualManifest)),
|
2016-09-28 18:26:52 +01:00
|
|
|
ok = ink_compactjournal(Ink1,
|
|
|
|
Checker,
|
2016-10-05 18:28:31 +01:00
|
|
|
fun(X) -> {X, 55} end,
|
2017-05-26 10:51:30 +01:00
|
|
|
fun(_F) -> ok end,
|
2016-09-28 18:26:52 +01:00
|
|
|
fun(L, K, SQN) -> lists:member({SQN, K}, L) end,
|
|
|
|
5000),
|
|
|
|
timer:sleep(1000),
|
2016-10-07 18:07:03 +01:00
|
|
|
CompactedManifest1 = ink_getmanifest(Ink1),
|
|
|
|
?assertMatch(2, length(CompactedManifest1)),
|
|
|
|
Checker2 = lists:sublist(Checker, 16),
|
|
|
|
ok = ink_compactjournal(Ink1,
|
|
|
|
Checker2,
|
|
|
|
fun(X) -> {X, 55} end,
|
2017-05-26 10:51:30 +01:00
|
|
|
fun(_F) -> ok end,
|
2016-10-07 18:07:03 +01:00
|
|
|
fun(L, K, SQN) -> lists:member({SQN, K}, L) end,
|
|
|
|
5000),
|
|
|
|
timer:sleep(1000),
|
|
|
|
CompactedManifest2 = ink_getmanifest(Ink1),
|
2017-01-18 15:23:06 +00:00
|
|
|
R = lists:foldl(fun({_SQN, FN, _P, _LK}, Acc) ->
|
2016-10-07 18:07:03 +01:00
|
|
|
case string:str(FN, "post_compact") of
|
|
|
|
N when N > 0 ->
|
|
|
|
true;
|
|
|
|
0 ->
|
|
|
|
Acc
|
|
|
|
end end,
|
|
|
|
false,
|
|
|
|
CompactedManifest2),
|
2017-03-30 12:15:36 +01:00
|
|
|
?assertMatch(false, R),
|
2016-10-07 18:07:03 +01:00
|
|
|
?assertMatch(2, length(CompactedManifest2)),
|
2016-09-28 18:26:52 +01:00
|
|
|
ink_close(Ink1),
|
2017-11-08 11:20:22 +00:00
|
|
|
% Need to wait for delete_pending files to timeout
|
|
|
|
timer:sleep(10000),
|
2016-09-28 18:26:52 +01:00
|
|
|
clean_testdir(RootPath).
|
2016-09-05 15:01:23 +01:00
|
|
|
|
2016-10-07 18:07:03 +01:00
|
|
|
empty_manifest_test() ->
|
|
|
|
RootPath = "../test/journal",
|
|
|
|
clean_testdir(RootPath),
|
|
|
|
CDBopts = #cdb_options{max_size=300000},
|
|
|
|
{ok, Ink1} = ink_start(#inker_options{root_path=RootPath,
|
2017-11-06 15:54:58 +00:00
|
|
|
cdb_options=CDBopts,
|
2017-11-06 18:44:08 +00:00
|
|
|
compression_method=native,
|
|
|
|
compress_on_receipt=true}),
|
2016-10-07 18:07:03 +01:00
|
|
|
?assertMatch(not_present, ink_fetch(Ink1, "Key1", 1)),
|
2016-11-18 21:35:45 +00:00
|
|
|
|
|
|
|
CheckFun = fun(L, K, SQN) -> lists:member({SQN, K}, L) end,
|
|
|
|
?assertMatch(false, CheckFun([], "key", 1)),
|
2016-10-07 18:07:03 +01:00
|
|
|
ok = ink_compactjournal(Ink1,
|
|
|
|
[],
|
|
|
|
fun(X) -> {X, 55} end,
|
2017-05-26 10:51:30 +01:00
|
|
|
fun(_F) -> ok end,
|
2016-11-18 21:35:45 +00:00
|
|
|
CheckFun,
|
2016-10-07 18:07:03 +01:00
|
|
|
5000),
|
|
|
|
timer:sleep(1000),
|
|
|
|
?assertMatch(1, length(ink_getmanifest(Ink1))),
|
|
|
|
ok = ink_close(Ink1),
|
2016-11-03 20:00:55 +00:00
|
|
|
|
|
|
|
% Add pending manifest to be ignored
|
|
|
|
FN = filepath(RootPath, manifest_dir) ++ "999.pnd",
|
|
|
|
ok = file:write_file(FN, term_to_binary("Hello")),
|
|
|
|
|
2016-10-07 18:07:03 +01:00
|
|
|
{ok, Ink2} = ink_start(#inker_options{root_path=RootPath,
|
2017-11-06 15:54:58 +00:00
|
|
|
cdb_options=CDBopts,
|
2017-11-06 18:44:08 +00:00
|
|
|
compression_method=native,
|
|
|
|
compress_on_receipt=false}),
|
2016-10-07 18:07:03 +01:00
|
|
|
?assertMatch(not_present, ink_fetch(Ink2, "Key1", 1)),
|
2017-05-22 15:31:42 +01:00
|
|
|
{ok, SQN, Size} = ink_put(Ink2, "Key1", "Value1", {[], infinity}),
|
2016-10-07 18:07:03 +01:00
|
|
|
?assertMatch(2, SQN),
|
|
|
|
?assertMatch(true, Size > 0),
|
|
|
|
{ok, V} = ink_fetch(Ink2, "Key1", 2),
|
|
|
|
?assertMatch("Value1", V),
|
|
|
|
ink_close(Ink2),
|
|
|
|
clean_testdir(RootPath).
|
2016-11-14 20:43:38 +00:00
|
|
|
|
|
|
|
coverage_cheat_test() ->
|
|
|
|
{noreply, _State0} = handle_info(timeout, #state{}),
|
|
|
|
{ok, _State1} = code_change(null, #state{}, null).
|
2016-10-07 18:07:03 +01:00
|
|
|
|
2017-07-31 19:47:58 +02:00
|
|
|
-endif.
|