2016-08-02 17:51:43 +01:00
|
|
|
%% -------- PENCILLER ---------
|
2016-07-29 17:19:30 +01:00
|
|
|
%%
|
2016-08-16 12:45:48 +01:00
|
|
|
%% The penciller is responsible for writing and re-writing the ledger - a
|
2016-08-02 13:44:48 +01:00
|
|
|
%% persisted, ordered view of non-recent Keys and Metadata which have been
|
|
|
|
%% added to the store.
|
|
|
|
%% - The penciller maintains a manifest of all the files within the current
|
2017-01-06 10:09:15 +00:00
|
|
|
%% Ledger.
|
2016-08-16 12:45:48 +01:00
|
|
|
%% - The Penciller provides re-write (compaction) work up to be managed by
|
|
|
|
%% the Penciller's Clerk
|
2016-10-09 22:33:45 +01:00
|
|
|
%% - The Penciller can be cloned and maintains a register of clones who have
|
|
|
|
%% requested snapshots of the Ledger
|
2017-01-20 16:36:20 +00:00
|
|
|
%% - The accepts new dumps (in the form of a leveled_tree accomponied by
|
2017-01-06 10:09:15 +00:00
|
|
|
%% an array of hash-listing binaries) from the Bookie, and responds either 'ok'
|
|
|
|
%% to the bookie if the information is accepted nad the Bookie can refresh its
|
|
|
|
%% memory, or 'returned' if the bookie must continue without refreshing as the
|
|
|
|
%% Penciller is not currently able to accept the update (potentially due to a
|
|
|
|
%% backlog of compaction work)
|
2016-10-03 23:34:28 +01:00
|
|
|
%% - The Penciller's persistence of the ledger may not be reliable, in that it
|
|
|
|
%% may lose data but only in sequence from a particular sequence number. On
|
|
|
|
%% startup the Penciller will inform the Bookie of the highest sequence number
|
|
|
|
%% it has, and the Bookie should load any missing data from that point out of
|
2016-11-05 15:59:31 +00:00
|
|
|
%% the journal.
|
2016-07-29 17:19:30 +01:00
|
|
|
%%
|
2016-08-02 17:51:43 +01:00
|
|
|
%% -------- LEDGER ---------
|
2016-07-29 17:19:30 +01:00
|
|
|
%%
|
2016-08-02 13:44:48 +01:00
|
|
|
%% The Ledger is divided into many levels
|
2017-01-06 10:09:15 +00:00
|
|
|
%% - L0: New keys are received from the Bookie and and kept in the levelzero
|
|
|
|
%% cache, until that cache is the size of a SST file, and it is then persisted
|
2016-12-29 02:07:14 +00:00
|
|
|
%% as a SST file at this level. L0 SST files can be larger than the normal
|
2016-08-16 12:45:48 +01:00
|
|
|
%% maximum size - so we don't have to consider problems of either having more
|
|
|
|
%% than one L0 file (and handling what happens on a crash between writing the
|
|
|
|
%% files when the second may have overlapping sequence numbers), or having a
|
|
|
|
%% remainder with overlapping in sequence numbers in memory after the file is
|
2017-01-06 10:09:15 +00:00
|
|
|
%% written. Once the persistence is completed, the L0 cache can be erased.
|
2016-12-29 02:07:14 +00:00
|
|
|
%% There can be only one SST file at Level 0, so the work to merge that file
|
2016-08-16 12:45:48 +01:00
|
|
|
%% to the lower level must be the highest priority, as otherwise writes to the
|
|
|
|
%% ledger will stall, when there is next a need to persist.
|
2016-12-29 02:07:14 +00:00
|
|
|
%% - L1 TO L7: May contain multiple processes managing non-overlapping SST
|
2016-08-16 12:45:48 +01:00
|
|
|
%% files. Compaction work should be sheduled if the number of files exceeds
|
|
|
|
%% the target size of the level, where the target size is 8 ^ n.
|
2016-07-27 18:03:44 +01:00
|
|
|
%%
|
|
|
|
%% The most recent revision of a Key can be found by checking each level until
|
2016-08-02 13:44:48 +01:00
|
|
|
%% the key is found. To check a level the correct file must be sought from the
|
|
|
|
%% manifest for that level, and then a call is made to that file. If the Key
|
|
|
|
%% is not present then every level should be checked.
|
2016-07-27 18:03:44 +01:00
|
|
|
%%
|
|
|
|
%% If a compaction change takes the size of a level beyond the target size,
|
|
|
|
%% then compaction work for that level + 1 should be added to the compaction
|
|
|
|
%% work queue.
|
2016-08-16 12:45:48 +01:00
|
|
|
%% Compaction work is fetched by the Penciller's Clerk because:
|
2016-07-27 18:03:44 +01:00
|
|
|
%% - it has timed out due to a period of inactivity
|
|
|
|
%% - it has been triggered by the a cast to indicate the arrival of high
|
|
|
|
%% priority compaction work
|
2016-08-02 17:51:43 +01:00
|
|
|
%% The Penciller's Clerk (which performs compaction worker) will always call
|
2016-08-16 12:45:48 +01:00
|
|
|
%% the Penciller to find out the highest priority work currently required
|
2016-08-02 17:51:43 +01:00
|
|
|
%% whenever it has either completed work, or a timeout has occurred since it
|
|
|
|
%% was informed there was no work to do.
|
2016-07-27 18:03:44 +01:00
|
|
|
%%
|
2016-08-16 12:45:48 +01:00
|
|
|
%% When the clerk picks work it will take the current manifest, and the
|
|
|
|
%% Penciller assumes the manifest sequence number is to be incremented.
|
2016-11-07 11:17:13 +00:00
|
|
|
%% When the clerk has completed the work it can request that the manifest
|
2016-08-16 12:45:48 +01:00
|
|
|
%% change be committed by the Penciller. The commit is made through changing
|
|
|
|
%% the filename of the new manifest - so the Penciller is not held up by the
|
|
|
|
%% process of wiritng a file, just altering file system metadata.
|
2016-08-02 13:44:48 +01:00
|
|
|
%%
|
2016-08-02 17:51:43 +01:00
|
|
|
%% ---------- PUSH ----------
|
|
|
|
%%
|
2016-08-16 12:45:48 +01:00
|
|
|
%% The Penciller must support the PUSH of a dump of keys from the Bookie. The
|
2016-08-02 17:51:43 +01:00
|
|
|
%% call to PUSH should be immediately acknowledged, and then work should be
|
2017-01-06 10:09:15 +00:00
|
|
|
%% completed to merge the cache update into the L0 cache.
|
2016-08-02 17:51:43 +01:00
|
|
|
%%
|
|
|
|
%% The Penciller MUST NOT accept a new PUSH if the Clerk has commenced the
|
2017-01-06 10:09:15 +00:00
|
|
|
%% conversion of the current L0 cache into a SST file, but not completed this
|
2016-10-27 09:45:05 +01:00
|
|
|
%% change. The Penciller in this case returns the push, and the Bookie should
|
2016-11-07 11:17:13 +00:00
|
|
|
%% continue to grow the cache before trying again.
|
2016-08-02 17:51:43 +01:00
|
|
|
%%
|
|
|
|
%% ---------- FETCH ----------
|
|
|
|
%%
|
2016-11-07 11:17:13 +00:00
|
|
|
%% On request to fetch a key the Penciller should look first in the in-memory
|
2016-12-29 02:07:14 +00:00
|
|
|
%% L0 tree, then look in the SST files Level by Level (including level 0),
|
2016-11-07 11:17:13 +00:00
|
|
|
%% consulting the Manifest to determine which file should be checked at each
|
|
|
|
%% level.
|
2016-08-02 17:51:43 +01:00
|
|
|
%%
|
|
|
|
%% ---------- SNAPSHOT ----------
|
|
|
|
%%
|
2016-10-03 23:34:28 +01:00
|
|
|
%% Iterators may request a snapshot of the database. A snapshot is a cloned
|
2016-10-27 20:56:18 +01:00
|
|
|
%% Penciller seeded not from disk, but by the in-memory L0 gb_tree and the
|
2016-12-29 02:07:14 +00:00
|
|
|
%% in-memory manifest, allowing for direct reference for the SST file processes.
|
2016-10-03 23:34:28 +01:00
|
|
|
%%
|
|
|
|
%% Clones formed to support snapshots are registered by the Penciller, so that
|
2016-12-29 02:07:14 +00:00
|
|
|
%% SST files valid at the point of the snapshot until either the iterator is
|
2016-08-02 17:51:43 +01:00
|
|
|
%% completed or has timed out.
|
|
|
|
%%
|
|
|
|
%% ---------- ON STARTUP ----------
|
|
|
|
%%
|
|
|
|
%% On Startup the Bookie with ask the Penciller to initiate the Ledger first.
|
2016-12-29 02:07:14 +00:00
|
|
|
%% To initiate the Ledger the must consult the manifest, and then start a SST
|
2016-08-02 17:51:43 +01:00
|
|
|
%% management process for each file in the manifest.
|
|
|
|
%%
|
2016-08-15 16:43:39 +01:00
|
|
|
%% The penciller should then try and read any Level 0 file which has the
|
|
|
|
%% manifest sequence number one higher than the last store in the manifest.
|
2016-08-02 17:51:43 +01:00
|
|
|
%%
|
|
|
|
%% The Bookie will ask the Inker for any Keys seen beyond that sequence number
|
|
|
|
%% before the startup of the overall store can be completed.
|
|
|
|
%%
|
|
|
|
%% ---------- ON SHUTDOWN ----------
|
|
|
|
%%
|
|
|
|
%% On a controlled shutdown the Penciller should attempt to write any in-memory
|
2016-12-29 02:07:14 +00:00
|
|
|
%% ETS table to a L0 SST file, assuming one is nto already pending. If one is
|
2016-10-03 23:34:28 +01:00
|
|
|
%% already pending then the Penciller will not persist this part of the Ledger.
|
2016-08-02 17:51:43 +01:00
|
|
|
%%
|
|
|
|
%% ---------- FOLDER STRUCTURE ----------
|
|
|
|
%%
|
|
|
|
%% The following folders are used by the Penciller
|
2016-10-03 23:34:28 +01:00
|
|
|
%% $ROOT/ledger/ledger_manifest/ - used for keeping manifest files
|
2016-12-29 02:07:14 +00:00
|
|
|
%% $ROOT/ledger/ledger_files/ - containing individual SST files
|
2016-08-02 17:51:43 +01:00
|
|
|
%%
|
|
|
|
%% In larger stores there could be a large number of files in the ledger_file
|
|
|
|
%% folder - perhaps o(1000). It is assumed that modern file systems should
|
|
|
|
%% handle this efficiently.
|
|
|
|
%%
|
|
|
|
%% ---------- COMPACTION & MANIFEST UPDATES ----------
|
|
|
|
%%
|
|
|
|
%% The Penciller can have one and only one Clerk for performing compaction
|
|
|
|
%% work. When the Clerk has requested and taken work, it should perform the
|
2016-12-29 02:07:14 +00:00
|
|
|
%5 compaction work starting the new SST process to manage the new Ledger state
|
2016-08-02 17:51:43 +01:00
|
|
|
%% and then write a new manifest file that represents that state with using
|
2016-08-09 16:09:29 +01:00
|
|
|
%% the next Manifest sequence number as the filename:
|
|
|
|
%% - nonzero_<ManifestSQN#>.pnd
|
2016-08-02 17:51:43 +01:00
|
|
|
%%
|
2016-08-09 16:09:29 +01:00
|
|
|
%% The Penciller on accepting the change should rename the manifest file to -
|
|
|
|
%% - nonzero_<ManifestSQN#>.crr
|
2016-08-02 17:51:43 +01:00
|
|
|
%%
|
2016-08-09 16:09:29 +01:00
|
|
|
%% On startup, the Penciller should look for the nonzero_*.crr file with the
|
2016-10-19 17:34:58 +01:00
|
|
|
%% highest such manifest sequence number. This will be started as the
|
2016-12-29 02:07:14 +00:00
|
|
|
%% manifest, together with any _0_0.sst file found at that Manifest SQN.
|
2016-10-19 17:34:58 +01:00
|
|
|
%% Level zero files are not kept in the persisted manifest, and adding a L0
|
|
|
|
%% file does not advanced the Manifest SQN.
|
2016-08-02 17:51:43 +01:00
|
|
|
%%
|
|
|
|
%% The pace at which the store can accept updates will be dependent on the
|
|
|
|
%% speed at which the Penciller's Clerk can merge files at lower levels plus
|
|
|
|
%% the time it takes to merge from Level 0. As if a clerk has commenced
|
2016-12-29 02:07:14 +00:00
|
|
|
%% compaction work at a lower level and then immediately a L0 SST file is
|
2016-08-02 17:51:43 +01:00
|
|
|
%% written the Penciller will need to wait for this compaction work to
|
|
|
|
%% complete and the L0 file to be compacted before the ETS table can be
|
|
|
|
%% allowed to again reach capacity
|
2016-08-09 16:09:29 +01:00
|
|
|
%%
|
|
|
|
%% The writing of L0 files do not require the involvement of the clerk.
|
2016-10-27 20:56:18 +01:00
|
|
|
%% The L0 files are prompted directly by the penciller when the in-memory tree
|
2016-11-07 11:17:13 +00:00
|
|
|
%% has reached capacity. This places the penciller in a levelzero_pending
|
2016-12-29 02:07:14 +00:00
|
|
|
%% state, and in this state it must return new pushes. Once the SST file has
|
2016-11-07 11:17:13 +00:00
|
|
|
%% been completed it will confirm completion to the penciller which can then
|
|
|
|
%% revert the levelzero_pending state, add the file to the manifest and clear
|
|
|
|
%% the current level zero in-memory view.
|
|
|
|
%%
|
|
|
|
|
2016-07-28 17:22:50 +01:00
|
|
|
|
2016-08-02 13:44:48 +01:00
|
|
|
-module(leveled_penciller).
|
2016-07-27 18:03:44 +01:00
|
|
|
|
2016-08-09 16:09:29 +01:00
|
|
|
-behaviour(gen_server).
|
|
|
|
|
2016-10-18 01:59:03 +01:00
|
|
|
-include("include/leveled.hrl").
|
2016-07-27 18:03:44 +01:00
|
|
|
|
2017-01-13 18:23:57 +00:00
|
|
|
-export([
|
|
|
|
init/1,
|
2016-08-09 16:09:29 +01:00
|
|
|
handle_call/3,
|
|
|
|
handle_cast/2,
|
|
|
|
handle_info/2,
|
|
|
|
terminate/2,
|
2017-01-13 18:23:57 +00:00
|
|
|
code_change/3]).
|
|
|
|
|
|
|
|
-export([
|
2016-08-09 16:09:29 +01:00
|
|
|
pcl_start/1,
|
|
|
|
pcl_pushmem/2,
|
2016-10-31 01:33:33 +00:00
|
|
|
pcl_fetchlevelzero/2,
|
2016-08-09 16:09:29 +01:00
|
|
|
pcl_fetch/2,
|
2016-12-11 01:02:56 +00:00
|
|
|
pcl_fetch/3,
|
2016-10-12 17:12:49 +01:00
|
|
|
pcl_fetchkeys/5,
|
2016-11-20 21:21:31 +00:00
|
|
|
pcl_fetchnextkey/5,
|
2016-09-26 10:55:08 +01:00
|
|
|
pcl_checksequencenumber/3,
|
2016-08-09 16:09:29 +01:00
|
|
|
pcl_workforclerk/1,
|
2017-01-14 16:36:05 +00:00
|
|
|
pcl_manifestchange/2,
|
2016-12-11 21:01:10 +00:00
|
|
|
pcl_confirml0complete/4,
|
2017-01-17 11:18:58 +00:00
|
|
|
pcl_confirmdelete/3,
|
2016-08-15 16:43:39 +01:00
|
|
|
pcl_close/1,
|
2016-11-21 12:34:40 +00:00
|
|
|
pcl_doom/1,
|
2016-09-21 18:31:42 +01:00
|
|
|
pcl_registersnapshot/2,
|
2016-10-12 17:12:49 +01:00
|
|
|
pcl_releasesnapshot/2,
|
2016-09-23 18:50:29 +01:00
|
|
|
pcl_loadsnapshot/2,
|
2017-01-13 18:23:57 +00:00
|
|
|
pcl_getstartupsequencenumber/1]).
|
|
|
|
|
|
|
|
-export([
|
|
|
|
filepath/3,
|
2016-09-15 10:53:24 +01:00
|
|
|
clean_testdir/1]).
|
2016-07-27 18:03:44 +01:00
|
|
|
|
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
|
2016-07-28 17:22:50 +01:00
|
|
|
-define(LEVEL_SCALEFACTOR, [{0, 0}, {1, 8}, {2, 64}, {3, 512},
|
2016-10-19 17:34:58 +01:00
|
|
|
{4, 4096}, {5, 32768}, {6, 262144},
|
|
|
|
{7, infinity}]).
|
2016-07-27 18:03:44 +01:00
|
|
|
-define(MAX_LEVELS, 8).
|
|
|
|
-define(MAX_WORK_WAIT, 300).
|
2016-08-02 17:51:43 +01:00
|
|
|
-define(MANIFEST_FP, "ledger_manifest").
|
|
|
|
-define(FILES_FP, "ledger_files").
|
|
|
|
-define(CURRENT_FILEX, "crr").
|
|
|
|
-define(PENDING_FILEX, "pnd").
|
2016-08-09 16:09:29 +01:00
|
|
|
-define(MEMTABLE, mem).
|
2016-12-11 04:53:36 +00:00
|
|
|
-define(MAX_TABLESIZE, 28000). % This is less than max - but COIN_SIDECOUNT
|
|
|
|
-define(SUPER_MAX_TABLE_SIZE, 40000).
|
2016-08-16 12:45:48 +01:00
|
|
|
-define(PROMPT_WAIT_ONL0, 5).
|
2016-11-05 14:04:45 +00:00
|
|
|
-define(WORKQUEUE_BACKLOG_TOLERANCE, 4).
|
2016-12-11 06:54:41 +00:00
|
|
|
-define(COIN_SIDECOUNT, 5).
|
2016-12-21 18:28:14 +00:00
|
|
|
-define(SLOW_FETCH, 20000).
|
2016-12-29 10:21:57 +00:00
|
|
|
-define(ITERATOR_SCANWIDTH, 4).
|
2017-01-14 20:57:43 +00:00
|
|
|
-define(SNAPSHOT_TIMEOUT, 3600).
|
2016-09-21 18:31:42 +01:00
|
|
|
|
2017-01-13 18:23:57 +00:00
|
|
|
-record(state, {manifest, % a manifest record from the leveled_manifest module
|
2016-10-30 18:25:30 +00:00
|
|
|
persisted_sqn = 0 :: integer(), % The highest SQN persisted
|
2017-01-12 13:48:43 +00:00
|
|
|
|
|
|
|
ledger_sqn = 0 :: integer(), % The highest SQN added to L0
|
2016-08-12 01:05:59 +01:00
|
|
|
root_path = "../test" :: string(),
|
2016-10-27 20:56:18 +01:00
|
|
|
|
2016-10-30 18:25:30 +00:00
|
|
|
clerk :: pid(),
|
2016-10-27 20:56:18 +01:00
|
|
|
|
|
|
|
levelzero_pending = false :: boolean(),
|
|
|
|
levelzero_constructor :: pid(),
|
2017-01-20 16:36:20 +00:00
|
|
|
levelzero_cache = [] :: list(), % a list of trees
|
2016-10-30 18:25:30 +00:00
|
|
|
levelzero_size = 0 :: integer(),
|
2016-10-27 20:56:18 +01:00
|
|
|
levelzero_maxcachesize :: integer(),
|
2016-12-09 14:36:03 +00:00
|
|
|
levelzero_cointoss = false :: boolean(),
|
2017-01-05 21:58:33 +00:00
|
|
|
levelzero_index, % An array
|
2016-10-27 20:56:18 +01:00
|
|
|
|
2016-09-23 18:50:29 +01:00
|
|
|
is_snapshot = false :: boolean(),
|
|
|
|
snapshot_fully_loaded = false :: boolean(),
|
2016-10-27 20:56:18 +01:00
|
|
|
source_penciller :: pid(),
|
2016-12-09 18:30:40 +00:00
|
|
|
levelzero_astree :: list(),
|
2016-10-27 20:56:18 +01:00
|
|
|
|
2017-01-14 22:03:57 +00:00
|
|
|
work_ongoing = false :: boolean(), % i.e. compaction work
|
|
|
|
work_backlog = false :: boolean(), % i.e. compaction work
|
2016-12-22 14:03:31 +00:00
|
|
|
|
|
|
|
head_timing :: tuple()}).
|
2016-08-09 16:09:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
%%%============================================================================
|
|
|
|
%%% API
|
|
|
|
%%%============================================================================
|
2016-09-15 18:38:23 +01:00
|
|
|
|
2016-08-12 01:05:59 +01:00
|
|
|
|
2016-09-08 14:21:30 +01:00
|
|
|
pcl_start(PCLopts) ->
|
|
|
|
gen_server:start(?MODULE, [PCLopts], []).
|
2016-08-09 16:09:29 +01:00
|
|
|
|
2016-12-11 01:02:56 +00:00
|
|
|
pcl_pushmem(Pid, LedgerCache) ->
|
2016-08-09 16:09:29 +01:00
|
|
|
%% Bookie to dump memory onto penciller
|
2016-12-11 01:02:56 +00:00
|
|
|
gen_server:call(Pid, {push_mem, LedgerCache}, infinity).
|
2016-10-31 01:33:33 +00:00
|
|
|
|
|
|
|
pcl_fetchlevelzero(Pid, Slot) ->
|
2016-11-03 19:02:50 +00:00
|
|
|
%% Timeout to cause crash of L0 file when it can't get the close signal
|
|
|
|
%% as it is deadlocked making this call.
|
|
|
|
%%
|
|
|
|
%% If the timeout gets hit outside of close scenario the Penciller will
|
|
|
|
%% be stuck in L0 pending
|
2016-11-14 17:18:28 +00:00
|
|
|
gen_server:call(Pid, {fetch_levelzero, Slot}, 60000).
|
2016-08-09 16:09:29 +01:00
|
|
|
|
|
|
|
pcl_fetch(Pid, Key) ->
|
2016-12-11 01:02:56 +00:00
|
|
|
Hash = leveled_codec:magic_hash(Key),
|
|
|
|
if
|
|
|
|
Hash /= no_lookup ->
|
|
|
|
gen_server:call(Pid, {fetch, Key, Hash}, infinity)
|
|
|
|
end.
|
|
|
|
|
|
|
|
pcl_fetch(Pid, Key, Hash) ->
|
|
|
|
gen_server:call(Pid, {fetch, Key, Hash}, infinity).
|
2016-08-09 16:09:29 +01:00
|
|
|
|
2016-10-12 17:12:49 +01:00
|
|
|
pcl_fetchkeys(Pid, StartKey, EndKey, AccFun, InitAcc) ->
|
|
|
|
gen_server:call(Pid,
|
2016-11-20 21:21:31 +00:00
|
|
|
{fetch_keys, StartKey, EndKey, AccFun, InitAcc, -1},
|
|
|
|
infinity).
|
|
|
|
|
|
|
|
pcl_fetchnextkey(Pid, StartKey, EndKey, AccFun, InitAcc) ->
|
|
|
|
gen_server:call(Pid,
|
|
|
|
{fetch_keys, StartKey, EndKey, AccFun, InitAcc, 1},
|
2016-10-12 17:12:49 +01:00
|
|
|
infinity).
|
|
|
|
|
2016-09-26 10:55:08 +01:00
|
|
|
pcl_checksequencenumber(Pid, Key, SQN) ->
|
2016-12-11 01:02:56 +00:00
|
|
|
Hash = leveled_codec:magic_hash(Key),
|
|
|
|
if
|
|
|
|
Hash /= no_lookup ->
|
|
|
|
gen_server:call(Pid, {check_sqn, Key, Hash, SQN}, infinity)
|
|
|
|
end.
|
|
|
|
|
2016-08-09 16:09:29 +01:00
|
|
|
pcl_workforclerk(Pid) ->
|
2017-01-15 00:52:43 +00:00
|
|
|
gen_server:cast(Pid, work_for_clerk).
|
2016-08-09 16:09:29 +01:00
|
|
|
|
2017-01-14 16:36:05 +00:00
|
|
|
pcl_manifestchange(Pid, Manifest) ->
|
2017-01-13 18:23:57 +00:00
|
|
|
gen_server:cast(Pid, {manifest_change, Manifest}).
|
2016-08-10 13:02:08 +01:00
|
|
|
|
2016-12-11 21:01:10 +00:00
|
|
|
pcl_confirml0complete(Pid, FN, StartKey, EndKey) ->
|
|
|
|
gen_server:cast(Pid, {levelzero_complete, FN, StartKey, EndKey}).
|
2016-11-05 11:22:27 +00:00
|
|
|
|
2017-01-17 11:18:58 +00:00
|
|
|
pcl_confirmdelete(Pid, FileName, FilePid) ->
|
|
|
|
gen_server:cast(Pid, {confirm_delete, FileName, FilePid}).
|
2016-08-09 16:09:29 +01:00
|
|
|
|
2016-08-15 16:43:39 +01:00
|
|
|
pcl_getstartupsequencenumber(Pid) ->
|
2016-09-21 18:31:42 +01:00
|
|
|
gen_server:call(Pid, get_startup_sqn, infinity).
|
|
|
|
|
|
|
|
pcl_registersnapshot(Pid, Snapshot) ->
|
|
|
|
gen_server:call(Pid, {register_snapshot, Snapshot}, infinity).
|
|
|
|
|
2016-10-12 17:12:49 +01:00
|
|
|
pcl_releasesnapshot(Pid, Snapshot) ->
|
|
|
|
gen_server:cast(Pid, {release_snapshot, Snapshot}).
|
|
|
|
|
2016-09-23 18:50:29 +01:00
|
|
|
pcl_loadsnapshot(Pid, Increment) ->
|
|
|
|
gen_server:call(Pid, {load_snapshot, Increment}, infinity).
|
|
|
|
|
2016-08-15 16:43:39 +01:00
|
|
|
pcl_close(Pid) ->
|
2016-10-13 17:51:47 +01:00
|
|
|
gen_server:call(Pid, close, 60000).
|
2016-08-15 16:43:39 +01:00
|
|
|
|
2016-11-21 12:34:40 +00:00
|
|
|
pcl_doom(Pid) ->
|
|
|
|
gen_server:call(Pid, doom, 60000).
|
2016-09-21 18:31:42 +01:00
|
|
|
|
2016-08-09 16:09:29 +01:00
|
|
|
%%%============================================================================
|
|
|
|
%%% gen_server callbacks
|
|
|
|
%%%============================================================================
|
|
|
|
|
2016-09-08 14:21:30 +01:00
|
|
|
init([PCLopts]) ->
|
2016-09-23 18:50:29 +01:00
|
|
|
case {PCLopts#penciller_options.root_path,
|
|
|
|
PCLopts#penciller_options.start_snapshot} of
|
|
|
|
{undefined, true} ->
|
|
|
|
SrcPenciller = PCLopts#penciller_options.source_penciller,
|
2016-10-27 20:56:18 +01:00
|
|
|
{ok, State} = pcl_registersnapshot(SrcPenciller, self()),
|
2017-01-17 14:11:50 +00:00
|
|
|
ManifestClone = leveled_pmanifest:copy_manifest(State#state.manifest),
|
2016-11-02 18:14:46 +00:00
|
|
|
leveled_log:log("P0001", [self()]),
|
2017-01-13 18:23:57 +00:00
|
|
|
{ok, State#state{is_snapshot=true,
|
|
|
|
source_penciller=SrcPenciller,
|
|
|
|
manifest=ManifestClone}};
|
2016-09-23 18:50:29 +01:00
|
|
|
%% Need to do something about timeout
|
|
|
|
{_RootPath, false} ->
|
2016-09-21 18:31:42 +01:00
|
|
|
start_from_file(PCLopts)
|
|
|
|
end.
|
2016-08-15 16:43:39 +01:00
|
|
|
|
2016-08-09 16:09:29 +01:00
|
|
|
|
2017-01-05 21:58:33 +00:00
|
|
|
handle_call({push_mem, {PushedTree, PushedIdx, MinSQN, MaxSQN}},
|
2016-12-11 01:02:56 +00:00
|
|
|
From,
|
|
|
|
State=#state{is_snapshot=Snap}) when Snap == false ->
|
2016-10-27 20:56:18 +01:00
|
|
|
% The push_mem process is as follows:
|
|
|
|
%
|
2017-01-20 16:36:20 +00:00
|
|
|
% 1 - Receive a cache. The cache has four parts: a tree of keys and
|
2017-01-06 10:09:15 +00:00
|
|
|
% values, an array of 256 binaries listing the hashes present in the
|
2017-01-20 16:36:20 +00:00
|
|
|
% tree, a min SQN and a max SQN
|
2016-10-27 20:56:18 +01:00
|
|
|
%
|
2016-11-05 11:22:27 +00:00
|
|
|
% 2 - Check to see if there is a levelzero file pending. If so, the
|
|
|
|
% update must be returned. If not the update can be accepted
|
2016-10-27 20:56:18 +01:00
|
|
|
%
|
2016-11-05 11:22:27 +00:00
|
|
|
% 3 - The Penciller can now reply to the Bookie to show if the push has
|
2016-10-27 20:56:18 +01:00
|
|
|
% been accepted
|
|
|
|
%
|
2016-10-30 18:25:30 +00:00
|
|
|
% 4 - Update the cache:
|
|
|
|
% a) Append the cache to the list
|
2017-01-06 10:09:15 +00:00
|
|
|
% b) Add each of the 256 hash-listing binaries to the master L0 index array
|
2016-10-30 18:25:30 +00:00
|
|
|
%
|
|
|
|
% Check the approximate size of the cache. If it is over the maximum size,
|
2017-01-06 10:09:15 +00:00
|
|
|
% trigger a background L0 file write and update state of levelzero_pending.
|
2016-11-18 21:35:45 +00:00
|
|
|
case State#state.levelzero_pending or State#state.work_backlog of
|
|
|
|
true ->
|
|
|
|
leveled_log:log("P0018", [returned,
|
|
|
|
State#state.levelzero_pending,
|
|
|
|
State#state.work_backlog]),
|
2016-11-05 13:42:44 +00:00
|
|
|
{reply, returned, State};
|
2016-11-18 21:35:45 +00:00
|
|
|
false ->
|
|
|
|
leveled_log:log("P0018", [ok, false, false]),
|
2016-11-05 13:42:44 +00:00
|
|
|
gen_server:reply(From, ok),
|
2017-01-05 21:58:33 +00:00
|
|
|
{noreply,
|
|
|
|
update_levelzero(State#state.levelzero_size,
|
|
|
|
{PushedTree, PushedIdx, MinSQN, MaxSQN},
|
|
|
|
State#state.ledger_sqn,
|
|
|
|
State#state.levelzero_cache,
|
|
|
|
State)}
|
2016-11-05 13:42:44 +00:00
|
|
|
end;
|
2016-12-11 01:02:56 +00:00
|
|
|
handle_call({fetch, Key, Hash}, _From, State) ->
|
2016-12-22 14:03:31 +00:00
|
|
|
{R, HeadTimer} = timed_fetch_mem(Key,
|
|
|
|
Hash,
|
2017-01-13 18:23:57 +00:00
|
|
|
State#state.manifest,
|
2016-12-22 14:03:31 +00:00
|
|
|
State#state.levelzero_cache,
|
|
|
|
State#state.levelzero_index,
|
|
|
|
State#state.head_timing),
|
|
|
|
{reply, R, State#state{head_timing=HeadTimer}};
|
2016-12-11 01:02:56 +00:00
|
|
|
handle_call({check_sqn, Key, Hash, SQN}, _From, State) ->
|
2016-10-07 10:04:48 +01:00
|
|
|
{reply,
|
2016-12-22 14:03:31 +00:00
|
|
|
compare_to_sqn(plain_fetch_mem(Key,
|
|
|
|
Hash,
|
2017-01-13 18:23:57 +00:00
|
|
|
State#state.manifest,
|
2016-12-22 14:03:31 +00:00
|
|
|
State#state.levelzero_cache,
|
|
|
|
State#state.levelzero_index),
|
2016-10-07 10:04:48 +01:00
|
|
|
SQN),
|
|
|
|
State};
|
2016-11-20 21:21:31 +00:00
|
|
|
handle_call({fetch_keys, StartKey, EndKey, AccFun, InitAcc, MaxKeys},
|
2016-10-12 17:12:49 +01:00
|
|
|
_From,
|
|
|
|
State=#state{snapshot_fully_loaded=Ready})
|
|
|
|
when Ready == true ->
|
2016-11-25 14:50:13 +00:00
|
|
|
L0AsList =
|
2016-11-20 21:21:31 +00:00
|
|
|
case State#state.levelzero_astree of
|
|
|
|
undefined ->
|
|
|
|
leveled_pmem:merge_trees(StartKey,
|
|
|
|
EndKey,
|
|
|
|
State#state.levelzero_cache,
|
2017-01-21 11:38:26 +00:00
|
|
|
leveled_tree:empty(?CACHE_TYPE));
|
2016-11-25 14:50:13 +00:00
|
|
|
List ->
|
|
|
|
List
|
2016-11-20 21:21:31 +00:00
|
|
|
end,
|
2017-01-12 13:48:43 +00:00
|
|
|
|
|
|
|
SetupFoldFun =
|
|
|
|
fun(Level, Acc) ->
|
2017-01-17 14:11:50 +00:00
|
|
|
Pointers = leveled_pmanifest:range_lookup(State#state.manifest,
|
2017-01-13 18:23:57 +00:00
|
|
|
Level,
|
|
|
|
StartKey,
|
|
|
|
EndKey),
|
2017-01-12 13:48:43 +00:00
|
|
|
case Pointers of
|
|
|
|
[] -> Acc;
|
2017-01-13 18:23:57 +00:00
|
|
|
PL -> Acc ++ [{Level, PL}]
|
2017-01-12 13:48:43 +00:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
SSTiter = lists:foldl(SetupFoldFun, [], lists:seq(0, ?MAX_LEVELS - 1)),
|
|
|
|
|
2016-12-29 02:07:14 +00:00
|
|
|
Acc = keyfolder({L0AsList, SSTiter},
|
2016-11-20 21:21:31 +00:00
|
|
|
{StartKey, EndKey},
|
|
|
|
{AccFun, InitAcc},
|
|
|
|
MaxKeys),
|
2017-01-12 13:48:43 +00:00
|
|
|
|
2016-11-25 14:50:13 +00:00
|
|
|
{reply, Acc, State#state{levelzero_astree = L0AsList}};
|
2016-09-26 10:55:08 +01:00
|
|
|
handle_call(get_startup_sqn, _From, State) ->
|
2016-10-30 18:25:30 +00:00
|
|
|
{reply, State#state.persisted_sqn, State};
|
2016-09-26 10:55:08 +01:00
|
|
|
handle_call({register_snapshot, Snapshot}, _From, State) ->
|
2017-01-17 14:11:50 +00:00
|
|
|
Manifest0 = leveled_pmanifest:add_snapshot(State#state.manifest,
|
2017-01-13 18:23:57 +00:00
|
|
|
Snapshot,
|
|
|
|
?SNAPSHOT_TIMEOUT),
|
|
|
|
{reply, {ok, State}, State#state{manifest = Manifest0}};
|
2017-01-05 21:58:33 +00:00
|
|
|
handle_call({load_snapshot, {BookieIncrTree, BookieIdx, MinSQN, MaxSQN}},
|
|
|
|
_From, State) ->
|
2017-03-02 17:49:43 +00:00
|
|
|
{LedgerSQN, L0Size, L0Cache} =
|
|
|
|
case BookieIncrTree of
|
|
|
|
empty_cache ->
|
|
|
|
{State#state.ledger_sqn,
|
|
|
|
State#state.levelzero_size,
|
|
|
|
State#state.levelzero_cache};
|
|
|
|
_ ->
|
|
|
|
leveled_pmem:add_to_cache(State#state.levelzero_size,
|
|
|
|
{BookieIncrTree, MinSQN, MaxSQN},
|
|
|
|
State#state.ledger_sqn,
|
|
|
|
State#state.levelzero_cache)
|
|
|
|
end,
|
|
|
|
L0Index =
|
|
|
|
case BookieIdx of
|
|
|
|
empty_index ->
|
|
|
|
State#state.levelzero_index;
|
|
|
|
_ ->
|
|
|
|
leveled_pmem:add_to_index(BookieIdx,
|
|
|
|
State#state.levelzero_index,
|
|
|
|
length(L0Cache))
|
|
|
|
end,
|
2016-10-30 18:25:30 +00:00
|
|
|
{reply, ok, State#state{levelzero_cache=L0Cache,
|
|
|
|
levelzero_size=L0Size,
|
2017-01-05 21:58:33 +00:00
|
|
|
levelzero_index=L0Index,
|
2016-10-30 18:25:30 +00:00
|
|
|
ledger_sqn=LedgerSQN,
|
|
|
|
snapshot_fully_loaded=true}};
|
2016-10-31 01:33:33 +00:00
|
|
|
handle_call({fetch_levelzero, Slot}, _From, State) ->
|
|
|
|
{reply, lists:nth(Slot, State#state.levelzero_cache), State};
|
2016-09-26 10:55:08 +01:00
|
|
|
handle_call(close, _From, State) ->
|
2016-11-21 12:34:40 +00:00
|
|
|
{stop, normal, ok, State};
|
|
|
|
handle_call(doom, _From, State) ->
|
|
|
|
leveled_log:log("P0030", []),
|
|
|
|
ManifestFP = State#state.root_path ++ "/" ++ ?MANIFEST_FP ++ "/",
|
|
|
|
FilesFP = State#state.root_path ++ "/" ++ ?FILES_FP ++ "/",
|
|
|
|
{stop, normal, {ok, [ManifestFP, FilesFP]}, State}.
|
2016-10-27 20:56:18 +01:00
|
|
|
|
2017-01-13 18:23:57 +00:00
|
|
|
handle_cast({manifest_change, NewManifest}, State) ->
|
2017-01-17 14:11:50 +00:00
|
|
|
NewManSQN = leveled_pmanifest:get_manifest_sqn(NewManifest),
|
2017-01-17 10:12:15 +00:00
|
|
|
ok = leveled_pclerk:clerk_promptdeletions(State#state.clerk, NewManSQN),
|
2017-01-14 22:03:57 +00:00
|
|
|
{noreply, State#state{manifest = NewManifest, work_ongoing=false}};
|
2016-10-12 17:12:49 +01:00
|
|
|
handle_cast({release_snapshot, Snapshot}, State) ->
|
2017-01-17 14:11:50 +00:00
|
|
|
Manifest0 = leveled_pmanifest:release_snapshot(State#state.manifest,
|
2017-01-13 18:23:57 +00:00
|
|
|
Snapshot),
|
2016-11-02 18:14:46 +00:00
|
|
|
leveled_log:log("P0003", [Snapshot]),
|
2017-01-13 18:23:57 +00:00
|
|
|
{noreply, State#state{manifest=Manifest0}};
|
2017-01-17 11:18:58 +00:00
|
|
|
handle_cast({confirm_delete, Filename, FilePid}, State=#state{is_snapshot=Snap})
|
2016-10-21 12:18:06 +01:00
|
|
|
when Snap == false ->
|
2017-01-15 00:52:43 +00:00
|
|
|
case State#state.work_ongoing of
|
|
|
|
false ->
|
2017-01-17 14:11:50 +00:00
|
|
|
R2D = leveled_pmanifest:ready_to_delete(State#state.manifest,
|
2017-01-15 00:52:43 +00:00
|
|
|
Filename),
|
|
|
|
case R2D of
|
2017-01-17 11:18:58 +00:00
|
|
|
{true, M0} ->
|
2017-01-15 00:52:43 +00:00
|
|
|
leveled_log:log("P0005", [Filename]),
|
2017-01-17 11:18:58 +00:00
|
|
|
ok = leveled_sst:sst_deleteconfirmed(FilePid),
|
2017-01-15 00:52:43 +00:00
|
|
|
{noreply, State#state{manifest=M0}};
|
2017-01-17 11:18:58 +00:00
|
|
|
{false, _M0} ->
|
2017-01-15 00:52:43 +00:00
|
|
|
{noreply, State}
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
% If there is ongoing work, then we can't safely update the pidmap
|
|
|
|
% as any change will be reverted when the manifest is passed back
|
|
|
|
% from the Clerk
|
2016-10-21 12:18:06 +01:00
|
|
|
{noreply, State}
|
2016-11-05 11:22:27 +00:00
|
|
|
end;
|
2016-12-11 21:01:10 +00:00
|
|
|
handle_cast({levelzero_complete, FN, StartKey, EndKey}, State) ->
|
2016-11-05 11:22:27 +00:00
|
|
|
leveled_log:log("P0029", []),
|
|
|
|
ManEntry = #manifest_entry{start_key=StartKey,
|
|
|
|
end_key=EndKey,
|
|
|
|
owner=State#state.levelzero_constructor,
|
|
|
|
filename=FN},
|
2017-01-17 14:11:50 +00:00
|
|
|
ManifestSQN = leveled_pmanifest:get_manifest_sqn(State#state.manifest) + 1,
|
|
|
|
UpdMan = leveled_pmanifest:insert_manifest_entry(State#state.manifest,
|
2017-01-13 18:23:57 +00:00
|
|
|
ManifestSQN,
|
|
|
|
0,
|
|
|
|
ManEntry),
|
2016-11-05 11:22:27 +00:00
|
|
|
% Prompt clerk to ask about work - do this for every L0 roll
|
2017-01-05 21:58:33 +00:00
|
|
|
UpdIndex = leveled_pmem:clear_index(State#state.levelzero_index),
|
2016-11-05 11:22:27 +00:00
|
|
|
ok = leveled_pclerk:clerk_prompt(State#state.clerk),
|
|
|
|
{noreply, State#state{levelzero_cache=[],
|
2017-01-05 21:58:33 +00:00
|
|
|
levelzero_index=UpdIndex,
|
2016-11-05 11:22:27 +00:00
|
|
|
levelzero_pending=false,
|
|
|
|
levelzero_constructor=undefined,
|
|
|
|
levelzero_size=0,
|
2016-11-05 12:03:21 +00:00
|
|
|
manifest=UpdMan,
|
2017-01-15 00:52:43 +00:00
|
|
|
persisted_sqn=State#state.ledger_sqn}};
|
|
|
|
handle_cast(work_for_clerk, State) ->
|
2017-02-09 23:41:28 +00:00
|
|
|
case {State#state.levelzero_pending, State#state.work_ongoing} of
|
|
|
|
{false, false} ->
|
|
|
|
% TODO - as part of supervision tree and retry work:
|
|
|
|
% Need to check for work_ongoing as well as levelzero_pending as
|
|
|
|
% there may be a race that could lead to the clerk doing the same
|
|
|
|
% thing twice.
|
|
|
|
%
|
|
|
|
% This has implications though if we auto-restart the pclerk in the
|
|
|
|
% future, without altering this state - it may never be able to
|
|
|
|
% request work due to ongoing work that crashed the previous clerk
|
|
|
|
%
|
|
|
|
% Perhaps the pclerk should not be restarted because of this, and
|
|
|
|
% the failure should ripple up
|
2017-01-17 14:11:50 +00:00
|
|
|
{WL, WC} = leveled_pmanifest:check_for_work(State#state.manifest,
|
2017-01-15 00:52:43 +00:00
|
|
|
?LEVEL_SCALEFACTOR),
|
|
|
|
case WC of
|
|
|
|
0 ->
|
|
|
|
{noreply, State#state{work_backlog=false}};
|
|
|
|
N when N > ?WORKQUEUE_BACKLOG_TOLERANCE ->
|
|
|
|
leveled_log:log("P0024", [N, true]),
|
|
|
|
[TL|_Tail] = WL,
|
|
|
|
ok = leveled_pclerk:clerk_push(State#state.clerk,
|
|
|
|
{TL, State#state.manifest}),
|
|
|
|
{noreply,
|
|
|
|
State#state{work_backlog=true, work_ongoing=true}};
|
|
|
|
N ->
|
|
|
|
leveled_log:log("P0024", [N, false]),
|
|
|
|
[TL|_Tail] = WL,
|
|
|
|
ok = leveled_pclerk:clerk_push(State#state.clerk,
|
|
|
|
{TL, State#state.manifest}),
|
|
|
|
{noreply,
|
|
|
|
State#state{work_backlog=false, work_ongoing=true}}
|
2017-02-09 23:41:28 +00:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{noreply, State}
|
2017-01-15 00:52:43 +00:00
|
|
|
end.
|
2016-09-26 10:55:08 +01:00
|
|
|
|
2016-10-30 18:25:30 +00:00
|
|
|
|
2016-11-05 15:59:31 +00:00
|
|
|
handle_info(_Info, State) ->
|
2016-09-26 10:55:08 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
2016-10-12 17:12:49 +01:00
|
|
|
terminate(Reason, State=#state{is_snapshot=Snap}) when Snap == true ->
|
|
|
|
ok = pcl_releasesnapshot(State#state.source_penciller, self()),
|
2016-11-02 18:14:46 +00:00
|
|
|
leveled_log:log("P0007", [Reason]),
|
2016-10-07 10:04:48 +01:00
|
|
|
ok;
|
2016-10-12 17:12:49 +01:00
|
|
|
terminate(Reason, State) ->
|
2016-09-26 10:55:08 +01:00
|
|
|
%% Level 0 files lie outside of the manifest, and so if there is no L0
|
|
|
|
%% file present it is safe to write the current contents of memory. If
|
|
|
|
%% there is a L0 file present - then the memory can be dropped (it is
|
|
|
|
%% recoverable from the ledger, and there should not be a lot to recover
|
|
|
|
%% as presumably the ETS file has been recently flushed, hence the presence
|
|
|
|
%% of a L0 file).
|
|
|
|
%%
|
2017-01-12 13:48:43 +00:00
|
|
|
%% The penciller should close each file in the manifest, and cast a close
|
|
|
|
%% on the clerk.
|
|
|
|
ok = leveled_pclerk:clerk_close(State#state.clerk),
|
|
|
|
|
2016-11-02 18:14:46 +00:00
|
|
|
leveled_log:log("P0008", [Reason]),
|
2017-01-17 14:11:50 +00:00
|
|
|
L0_Present = leveled_pmanifest:key_lookup(State#state.manifest, 0, all),
|
2017-01-14 19:41:09 +00:00
|
|
|
L0_Left = State#state.levelzero_size > 0,
|
|
|
|
case {State#state.levelzero_pending, L0_Present, L0_Left} of
|
|
|
|
{false, false, true} ->
|
2017-01-14 16:36:05 +00:00
|
|
|
L0Pid = roll_memory(State, true),
|
2016-12-29 02:07:14 +00:00
|
|
|
ok = leveled_sst:sst_close(L0Pid);
|
2016-11-14 17:18:28 +00:00
|
|
|
StatusTuple ->
|
|
|
|
leveled_log:log("P0010", [StatusTuple])
|
2016-10-07 10:04:48 +01:00
|
|
|
end,
|
2016-10-19 00:10:48 +01:00
|
|
|
|
|
|
|
% Tidy shutdown of individual files
|
2017-01-17 10:12:15 +00:00
|
|
|
EntryCloseFun =
|
|
|
|
fun(ME) ->
|
|
|
|
ok = leveled_sst:sst_close(ME#manifest_entry.owner)
|
|
|
|
end,
|
2017-01-17 14:11:50 +00:00
|
|
|
leveled_pmanifest:close_manifest(State#state.manifest, EntryCloseFun),
|
2016-11-02 18:14:46 +00:00
|
|
|
leveled_log:log("P0011", []),
|
2016-10-07 10:04:48 +01:00
|
|
|
ok.
|
2016-09-26 10:55:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
|
2016-08-09 16:09:29 +01:00
|
|
|
%%%============================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%============================================================================
|
|
|
|
|
2016-11-21 12:34:40 +00:00
|
|
|
|
2016-09-21 18:31:42 +01:00
|
|
|
start_from_file(PCLopts) ->
|
|
|
|
RootPath = PCLopts#penciller_options.root_path,
|
|
|
|
MaxTableSize = case PCLopts#penciller_options.max_inmemory_tablesize of
|
|
|
|
undefined ->
|
|
|
|
?MAX_TABLESIZE;
|
|
|
|
M ->
|
|
|
|
M
|
|
|
|
end,
|
2016-10-27 20:56:18 +01:00
|
|
|
|
2017-01-13 18:23:57 +00:00
|
|
|
{ok, MergeClerk} = leveled_pclerk:clerk_new(self(), RootPath),
|
2016-12-09 14:36:03 +00:00
|
|
|
|
|
|
|
CoinToss = PCLopts#penciller_options.levelzero_cointoss,
|
|
|
|
% Used to randomly defer the writing of L0 file. Intended to help with
|
|
|
|
% vnode syncronisation issues (e.g. stop them all by default merging to
|
|
|
|
% level zero concurrently)
|
|
|
|
|
2016-10-30 18:25:30 +00:00
|
|
|
InitState = #state{clerk=MergeClerk,
|
2016-09-21 18:31:42 +01:00
|
|
|
root_path=RootPath,
|
2016-12-09 14:36:03 +00:00
|
|
|
levelzero_maxcachesize=MaxTableSize,
|
2016-12-11 05:23:24 +00:00
|
|
|
levelzero_cointoss=CoinToss,
|
|
|
|
levelzero_index=leveled_pmem:new_index()},
|
2016-09-21 18:31:42 +01:00
|
|
|
|
|
|
|
%% Open manifest
|
2017-01-17 14:11:50 +00:00
|
|
|
Manifest0 = leveled_pmanifest:open_manifest(RootPath),
|
2017-01-13 18:23:57 +00:00
|
|
|
OpenFun =
|
|
|
|
fun(FN) ->
|
|
|
|
{ok, Pid, {_FK, _LK}} = leveled_sst:sst_open(FN),
|
|
|
|
Pid
|
2017-01-12 13:48:43 +00:00
|
|
|
end,
|
2017-01-13 18:23:57 +00:00
|
|
|
SQNFun = fun leveled_sst:sst_getmaxsequencenumber/1,
|
2017-01-17 14:11:50 +00:00
|
|
|
{MaxSQN, Manifest1} = leveled_pmanifest:load_manifest(Manifest0,
|
2017-01-13 18:23:57 +00:00
|
|
|
OpenFun,
|
|
|
|
SQNFun),
|
2016-11-02 18:14:46 +00:00
|
|
|
leveled_log:log("P0014", [MaxSQN]),
|
2017-01-17 14:11:50 +00:00
|
|
|
ManSQN = leveled_pmanifest:get_manifest_sqn(Manifest1),
|
2017-01-14 22:26:26 +00:00
|
|
|
leveled_log:log("P0035", [ManSQN]),
|
2016-10-27 20:56:18 +01:00
|
|
|
%% Find any L0 files
|
2017-01-14 22:31:45 +00:00
|
|
|
L0FN = filepath(RootPath, ManSQN + 1, new_merge_files) ++ "_0_0.sst",
|
2016-10-27 20:56:18 +01:00
|
|
|
case filelib:is_file(L0FN) of
|
|
|
|
true ->
|
2016-11-02 18:14:46 +00:00
|
|
|
leveled_log:log("P0015", [L0FN]),
|
2016-10-27 20:56:18 +01:00
|
|
|
{ok,
|
|
|
|
L0Pid,
|
2016-12-29 02:07:14 +00:00
|
|
|
{L0StartKey, L0EndKey}} = leveled_sst:sst_open(L0FN),
|
|
|
|
L0SQN = leveled_sst:sst_getmaxsequencenumber(L0Pid),
|
2017-01-12 13:48:43 +00:00
|
|
|
L0Entry = #manifest_entry{start_key = L0StartKey,
|
|
|
|
end_key = L0EndKey,
|
2017-01-13 18:23:57 +00:00
|
|
|
filename = L0FN,
|
|
|
|
owner = L0Pid},
|
2017-01-17 14:11:50 +00:00
|
|
|
Manifest2 = leveled_pmanifest:insert_manifest_entry(Manifest1,
|
2017-01-13 18:23:57 +00:00
|
|
|
ManSQN + 1,
|
|
|
|
0,
|
|
|
|
L0Entry),
|
2016-11-02 18:14:46 +00:00
|
|
|
leveled_log:log("P0016", [L0SQN]),
|
2016-10-30 18:25:30 +00:00
|
|
|
LedgerSQN = max(MaxSQN, L0SQN),
|
2016-10-27 20:56:18 +01:00
|
|
|
{ok,
|
2017-01-13 18:23:57 +00:00
|
|
|
InitState#state{manifest = Manifest2,
|
2017-01-12 13:48:43 +00:00
|
|
|
ledger_sqn = LedgerSQN,
|
2017-01-13 18:23:57 +00:00
|
|
|
persisted_sqn = LedgerSQN}};
|
2016-10-27 20:56:18 +01:00
|
|
|
false ->
|
2016-11-02 18:14:46 +00:00
|
|
|
leveled_log:log("P0017", []),
|
2016-10-27 20:56:18 +01:00
|
|
|
{ok,
|
2017-01-13 18:23:57 +00:00
|
|
|
InitState#state{manifest = Manifest1,
|
2017-01-12 13:48:43 +00:00
|
|
|
ledger_sqn = MaxSQN,
|
2017-01-13 18:23:57 +00:00
|
|
|
persisted_sqn = MaxSQN}}
|
2016-09-21 18:31:42 +01:00
|
|
|
end.
|
|
|
|
|
2016-10-30 18:25:30 +00:00
|
|
|
|
2017-01-05 21:58:33 +00:00
|
|
|
update_levelzero(L0Size, {PushedTree, PushedIdx, MinSQN, MaxSQN},
|
2016-12-11 01:02:56 +00:00
|
|
|
LedgerSQN, L0Cache, State) ->
|
2016-12-11 05:23:24 +00:00
|
|
|
SW = os:timestamp(),
|
2016-12-11 01:02:56 +00:00
|
|
|
Update = leveled_pmem:add_to_cache(L0Size,
|
|
|
|
{PushedTree, MinSQN, MaxSQN},
|
2016-10-30 18:25:30 +00:00
|
|
|
LedgerSQN,
|
|
|
|
L0Cache),
|
2017-01-05 21:58:33 +00:00
|
|
|
UpdL0Index = leveled_pmem:add_to_index(PushedIdx,
|
|
|
|
State#state.levelzero_index,
|
|
|
|
length(L0Cache) + 1),
|
2016-12-11 05:23:24 +00:00
|
|
|
|
2016-12-11 01:02:56 +00:00
|
|
|
{UpdMaxSQN, NewL0Size, UpdL0Cache} = Update,
|
2016-10-30 18:25:30 +00:00
|
|
|
if
|
2016-12-11 01:02:56 +00:00
|
|
|
UpdMaxSQN >= LedgerSQN ->
|
2016-10-30 18:25:30 +00:00
|
|
|
UpdState = State#state{levelzero_cache=UpdL0Cache,
|
|
|
|
levelzero_size=NewL0Size,
|
2017-01-05 21:58:33 +00:00
|
|
|
levelzero_index=UpdL0Index,
|
2016-12-11 01:02:56 +00:00
|
|
|
ledger_sqn=UpdMaxSQN},
|
2016-10-30 18:25:30 +00:00
|
|
|
CacheTooBig = NewL0Size > State#state.levelzero_maxcachesize,
|
2016-12-11 01:58:25 +00:00
|
|
|
CacheMuchTooBig = NewL0Size > ?SUPER_MAX_TABLE_SIZE,
|
2017-01-17 14:11:50 +00:00
|
|
|
L0Free = not leveled_pmanifest:levelzero_present(State#state.manifest),
|
2016-12-09 14:36:03 +00:00
|
|
|
RandomFactor =
|
|
|
|
case State#state.levelzero_cointoss of
|
|
|
|
true ->
|
|
|
|
case random:uniform(?COIN_SIDECOUNT) of
|
|
|
|
1 ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
true
|
|
|
|
end,
|
2017-01-14 22:03:57 +00:00
|
|
|
NoPendingManifestChange = not State#state.work_ongoing,
|
2016-12-11 05:23:24 +00:00
|
|
|
JitterCheck = RandomFactor or CacheMuchTooBig,
|
2017-01-14 22:03:57 +00:00
|
|
|
case {CacheTooBig, L0Free, JitterCheck, NoPendingManifestChange} of
|
|
|
|
{true, true, true, true} ->
|
2016-12-11 05:23:24 +00:00
|
|
|
L0Constructor = roll_memory(UpdState, false),
|
|
|
|
leveled_log:log_timer("P0031", [], SW),
|
2016-10-30 18:25:30 +00:00
|
|
|
UpdState#state{levelzero_pending=true,
|
|
|
|
levelzero_constructor=L0Constructor};
|
|
|
|
_ ->
|
2016-12-11 05:23:24 +00:00
|
|
|
leveled_log:log_timer("P0031", [], SW),
|
2016-10-30 18:25:30 +00:00
|
|
|
UpdState
|
2017-01-04 21:36:59 +00:00
|
|
|
end
|
2016-10-30 18:25:30 +00:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2016-10-31 01:33:33 +00:00
|
|
|
%% Casting a large object (the levelzero cache) to the gen_server did not lead
|
|
|
|
%% to an immediate return as expected. With 32K keys in the TreeList it could
|
|
|
|
%% take around 35-40ms.
|
|
|
|
%%
|
2016-12-29 02:07:14 +00:00
|
|
|
%% To avoid blocking this gen_server, the SST file can request each item of the
|
2016-10-31 01:33:33 +00:00
|
|
|
%% cache one at a time.
|
|
|
|
%%
|
|
|
|
%% The Wait is set to false to use a cast when calling this in normal operation
|
|
|
|
%% where as the Wait of true is used at shutdown
|
|
|
|
|
|
|
|
roll_memory(State, false) ->
|
|
|
|
FileName = levelzero_filename(State),
|
2016-12-29 02:07:14 +00:00
|
|
|
leveled_log:log("P0019", [FileName, State#state.ledger_sqn]),
|
2016-10-31 01:33:33 +00:00
|
|
|
PCL = self(),
|
|
|
|
FetchFun = fun(Slot) -> pcl_fetchlevelzero(PCL, Slot) end,
|
2016-12-29 02:07:14 +00:00
|
|
|
R = leveled_sst:sst_newlevelzero(FileName,
|
2016-10-31 01:33:33 +00:00
|
|
|
length(State#state.levelzero_cache),
|
|
|
|
FetchFun,
|
2016-12-29 02:07:14 +00:00
|
|
|
PCL,
|
|
|
|
State#state.ledger_sqn),
|
2016-10-31 01:33:33 +00:00
|
|
|
{ok, Constructor, _} = R,
|
|
|
|
Constructor;
|
|
|
|
roll_memory(State, true) ->
|
|
|
|
FileName = levelzero_filename(State),
|
|
|
|
FetchFun = fun(Slot) -> lists:nth(Slot, State#state.levelzero_cache) end,
|
2016-12-29 02:07:14 +00:00
|
|
|
KVList = leveled_pmem:to_list(length(State#state.levelzero_cache),
|
|
|
|
FetchFun),
|
|
|
|
R = leveled_sst:sst_new(FileName, 0, KVList, State#state.ledger_sqn),
|
2016-10-31 01:33:33 +00:00
|
|
|
{ok, Constructor, _} = R,
|
|
|
|
Constructor.
|
2016-10-20 02:23:45 +01:00
|
|
|
|
2016-10-31 01:33:33 +00:00
|
|
|
levelzero_filename(State) ->
|
2017-01-17 14:11:50 +00:00
|
|
|
ManSQN = leveled_pmanifest:get_manifest_sqn(State#state.manifest) + 1,
|
2016-10-27 20:56:18 +01:00
|
|
|
FileName = State#state.root_path
|
|
|
|
++ "/" ++ ?FILES_FP ++ "/"
|
2017-01-13 18:23:57 +00:00
|
|
|
++ integer_to_list(ManSQN) ++ "_0_0",
|
2016-10-31 01:33:33 +00:00
|
|
|
FileName.
|
2016-09-21 18:31:42 +01:00
|
|
|
|
2017-01-13 18:23:57 +00:00
|
|
|
timed_fetch_mem(Key, Hash, Manifest, L0Cache, L0Index, HeadTimer) ->
|
2016-12-22 14:03:31 +00:00
|
|
|
SW = os:timestamp(),
|
2017-01-13 18:23:57 +00:00
|
|
|
{R, Level} = fetch_mem(Key, Hash, Manifest, L0Cache, L0Index),
|
2016-12-22 14:03:31 +00:00
|
|
|
UpdHeadTimer =
|
|
|
|
case R of
|
|
|
|
not_present ->
|
2016-12-22 15:45:38 +00:00
|
|
|
leveled_log:head_timing(HeadTimer, SW, Level, not_present);
|
2016-12-22 14:03:31 +00:00
|
|
|
_ ->
|
2016-12-22 15:45:38 +00:00
|
|
|
leveled_log:head_timing(HeadTimer, SW, Level, found)
|
2016-12-22 14:03:31 +00:00
|
|
|
end,
|
|
|
|
{R, UpdHeadTimer}.
|
2016-09-26 10:55:08 +01:00
|
|
|
|
2017-01-13 18:23:57 +00:00
|
|
|
plain_fetch_mem(Key, Hash, Manifest, L0Cache, L0Index) ->
|
|
|
|
R = fetch_mem(Key, Hash, Manifest, L0Cache, L0Index),
|
2016-12-22 14:03:31 +00:00
|
|
|
element(1, R).
|
2016-12-11 05:23:24 +00:00
|
|
|
|
2017-01-13 18:23:57 +00:00
|
|
|
fetch_mem(Key, Hash, Manifest, L0Cache, L0Index) ->
|
2017-01-05 21:58:33 +00:00
|
|
|
PosList = leveled_pmem:check_index(Hash, L0Index),
|
|
|
|
L0Check = leveled_pmem:check_levelzero(Key, Hash, PosList, L0Cache),
|
2016-10-30 18:25:30 +00:00
|
|
|
case L0Check of
|
|
|
|
{false, not_found} ->
|
2017-01-13 18:23:57 +00:00
|
|
|
fetch(Key, Hash, Manifest, 0, fun timed_sst_get/3);
|
2016-10-30 18:25:30 +00:00
|
|
|
{true, KV} ->
|
2016-12-22 14:03:31 +00:00
|
|
|
{KV, 0}
|
2016-08-09 16:09:29 +01:00
|
|
|
end.
|
|
|
|
|
2017-01-13 18:23:57 +00:00
|
|
|
fetch(_Key, _Hash, _Manifest, ?MAX_LEVELS + 1, _FetchFun) ->
|
2016-12-22 14:03:31 +00:00
|
|
|
{not_present, basement};
|
2017-01-13 18:23:57 +00:00
|
|
|
fetch(Key, Hash, Manifest, Level, FetchFun) ->
|
2017-01-17 14:11:50 +00:00
|
|
|
case leveled_pmanifest:key_lookup(Manifest, Level, Key) of
|
2017-01-12 13:48:43 +00:00
|
|
|
false ->
|
2017-01-13 18:23:57 +00:00
|
|
|
fetch(Key, Hash, Manifest, Level + 1, FetchFun);
|
|
|
|
FP ->
|
2017-01-12 13:48:43 +00:00
|
|
|
case FetchFun(FP, Key, Hash) of
|
2016-12-11 21:01:10 +00:00
|
|
|
not_present ->
|
2017-01-13 18:23:57 +00:00
|
|
|
fetch(Key, Hash, Manifest, Level + 1, FetchFun);
|
2016-12-11 21:01:10 +00:00
|
|
|
ObjectFound ->
|
2016-12-22 14:03:31 +00:00
|
|
|
{ObjectFound, Level}
|
2016-08-09 16:09:29 +01:00
|
|
|
end
|
|
|
|
end.
|
2016-09-26 10:55:08 +01:00
|
|
|
|
2016-12-29 02:07:14 +00:00
|
|
|
timed_sst_get(PID, Key, Hash) ->
|
2016-12-21 12:45:27 +00:00
|
|
|
SW = os:timestamp(),
|
2016-12-29 02:07:14 +00:00
|
|
|
R = leveled_sst:sst_get(PID, Key, Hash),
|
2016-12-21 12:45:27 +00:00
|
|
|
T0 = timer:now_diff(os:timestamp(), SW),
|
2017-02-26 21:48:04 +00:00
|
|
|
log_slowfetch(T0, R, PID, ?SLOW_FETCH).
|
|
|
|
|
|
|
|
log_slowfetch(T0, R, PID, FetchTolerance) ->
|
2016-12-21 12:45:27 +00:00
|
|
|
case {T0, R} of
|
2017-02-26 21:48:04 +00:00
|
|
|
{T, R} when T < FetchTolerance ->
|
2016-12-21 12:45:27 +00:00
|
|
|
R;
|
|
|
|
{T, not_present} ->
|
|
|
|
leveled_log:log("PC016", [PID, T, not_present]),
|
|
|
|
not_present;
|
|
|
|
{T, R} ->
|
|
|
|
leveled_log:log("PC016", [PID, T, found]),
|
|
|
|
R
|
|
|
|
end.
|
2016-08-09 16:09:29 +01:00
|
|
|
|
2016-10-07 10:04:48 +01:00
|
|
|
compare_to_sqn(Obj, SQN) ->
|
|
|
|
case Obj of
|
|
|
|
not_present ->
|
|
|
|
false;
|
|
|
|
Obj ->
|
2016-10-13 21:02:15 +01:00
|
|
|
SQNToCompare = leveled_codec:strip_to_seqonly(Obj),
|
2016-10-07 10:04:48 +01:00
|
|
|
if
|
|
|
|
SQNToCompare > SQN ->
|
|
|
|
false;
|
|
|
|
true ->
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2016-10-12 17:12:49 +01:00
|
|
|
%% Looks to find the best choice for the next key across the levels (other
|
|
|
|
%% than in-memory table)
|
|
|
|
%% In finding the best choice, the next key in a given level may be a next
|
|
|
|
%% block or next file pointer which will need to be expanded
|
|
|
|
|
|
|
|
find_nextkey(QueryArray, StartKey, EndKey) ->
|
|
|
|
find_nextkey(QueryArray,
|
2016-10-13 17:51:47 +01:00
|
|
|
0,
|
2016-10-12 17:12:49 +01:00
|
|
|
{null, null},
|
2016-12-29 02:07:14 +00:00
|
|
|
StartKey,
|
|
|
|
EndKey,
|
2016-12-29 10:21:57 +00:00
|
|
|
?ITERATOR_SCANWIDTH).
|
2016-10-12 17:12:49 +01:00
|
|
|
|
2016-12-29 02:07:14 +00:00
|
|
|
find_nextkey(_QueryArray, LCnt, {null, null}, _StartKey, _EndKey, _Width)
|
2016-10-12 17:12:49 +01:00
|
|
|
when LCnt > ?MAX_LEVELS ->
|
|
|
|
% The array has been scanned wihtout finding a best key - must be
|
|
|
|
% exhausted - respond to indicate no more keys to be found by the
|
|
|
|
% iterator
|
|
|
|
no_more_keys;
|
2016-12-29 02:07:14 +00:00
|
|
|
find_nextkey(QueryArray, LCnt, {BKL, BestKV}, _StartKey, _EndKey, _Width)
|
2016-10-12 17:12:49 +01:00
|
|
|
when LCnt > ?MAX_LEVELS ->
|
|
|
|
% All levels have been scanned, so need to remove the best result from
|
|
|
|
% the array, and return that array along with the best key/sqn/status
|
|
|
|
% combination
|
|
|
|
{BKL, [BestKV|Tail]} = lists:keyfind(BKL, 1, QueryArray),
|
|
|
|
{lists:keyreplace(BKL, 1, QueryArray, {BKL, Tail}), BestKV};
|
2016-12-29 02:07:14 +00:00
|
|
|
find_nextkey(QueryArray, LCnt, {BestKeyLevel, BestKV},
|
|
|
|
StartKey, EndKey, Width) ->
|
2016-10-12 17:12:49 +01:00
|
|
|
% Get the next key at this level
|
|
|
|
{NextKey, RestOfKeys} = case lists:keyfind(LCnt, 1, QueryArray) of
|
|
|
|
false ->
|
|
|
|
{null, null};
|
|
|
|
{LCnt, []} ->
|
|
|
|
{null, null};
|
|
|
|
{LCnt, [NK|ROfKs]} ->
|
|
|
|
{NK, ROfKs}
|
|
|
|
end,
|
|
|
|
% Compare the next key at this level with the best key
|
|
|
|
case {NextKey, BestKeyLevel, BestKV} of
|
|
|
|
{null, BKL, BKV} ->
|
|
|
|
% There is no key at this level - go to the next level
|
2016-12-29 02:07:14 +00:00
|
|
|
find_nextkey(QueryArray,
|
|
|
|
LCnt + 1,
|
|
|
|
{BKL, BKV},
|
|
|
|
StartKey, EndKey, Width);
|
2017-01-14 16:36:05 +00:00
|
|
|
{{next, Owner, _SK}, BKL, BKV} ->
|
2016-10-12 17:12:49 +01:00
|
|
|
% The first key at this level is pointer to a file - need to query
|
|
|
|
% the file to expand this level out before proceeding
|
2016-12-29 02:07:14 +00:00
|
|
|
Pointer = {next, Owner, StartKey, EndKey},
|
|
|
|
UpdList = leveled_sst:expand_list_by_pointer(Pointer,
|
|
|
|
RestOfKeys,
|
|
|
|
Width),
|
|
|
|
NewEntry = {LCnt, UpdList},
|
2016-10-12 17:12:49 +01:00
|
|
|
% Need to loop around at this level (LCnt) as we have not yet
|
|
|
|
% examined a real key at this level
|
|
|
|
find_nextkey(lists:keyreplace(LCnt, 1, QueryArray, NewEntry),
|
|
|
|
LCnt,
|
|
|
|
{BKL, BKV},
|
2016-12-29 02:07:14 +00:00
|
|
|
StartKey, EndKey, Width);
|
|
|
|
{{pointer, SSTPid, Slot, PSK, PEK}, BKL, BKV} ->
|
2016-10-12 17:12:49 +01:00
|
|
|
% The first key at this level is pointer within a file - need to
|
|
|
|
% query the file to expand this level out before proceeding
|
2016-12-29 02:07:14 +00:00
|
|
|
Pointer = {pointer, SSTPid, Slot, PSK, PEK},
|
|
|
|
UpdList = leveled_sst:expand_list_by_pointer(Pointer,
|
|
|
|
RestOfKeys,
|
|
|
|
Width),
|
|
|
|
NewEntry = {LCnt, UpdList},
|
2016-10-12 17:12:49 +01:00
|
|
|
% Need to loop around at this level (LCnt) as we have not yet
|
|
|
|
% examined a real key at this level
|
|
|
|
find_nextkey(lists:keyreplace(LCnt, 1, QueryArray, NewEntry),
|
|
|
|
LCnt,
|
|
|
|
{BKL, BKV},
|
2016-12-29 02:07:14 +00:00
|
|
|
StartKey, EndKey, Width);
|
2016-10-12 17:12:49 +01:00
|
|
|
{{Key, Val}, null, null} ->
|
|
|
|
% No best key set - so can assume that this key is the best key,
|
2016-10-20 16:00:08 +01:00
|
|
|
% and check the lower levels
|
2016-10-12 17:12:49 +01:00
|
|
|
find_nextkey(QueryArray,
|
|
|
|
LCnt + 1,
|
|
|
|
{LCnt, {Key, Val}},
|
2016-12-29 02:07:14 +00:00
|
|
|
StartKey, EndKey, Width);
|
2016-10-12 17:12:49 +01:00
|
|
|
{{Key, Val}, _BKL, {BestKey, _BestVal}} when Key < BestKey ->
|
|
|
|
% There is a real key and a best key to compare, and the real key
|
|
|
|
% at this level is before the best key, and so is now the new best
|
|
|
|
% key
|
|
|
|
% The QueryArray is not modified until we have checked all levels
|
|
|
|
find_nextkey(QueryArray,
|
|
|
|
LCnt + 1,
|
|
|
|
{LCnt, {Key, Val}},
|
2016-12-29 02:07:14 +00:00
|
|
|
StartKey, EndKey, Width);
|
2016-10-12 17:12:49 +01:00
|
|
|
{{Key, Val}, BKL, {BestKey, BestVal}} when Key == BestKey ->
|
2016-10-13 21:02:15 +01:00
|
|
|
SQN = leveled_codec:strip_to_seqonly({Key, Val}),
|
|
|
|
BestSQN = leveled_codec:strip_to_seqonly({BestKey, BestVal}),
|
2016-10-12 17:12:49 +01:00
|
|
|
if
|
|
|
|
SQN =< BestSQN ->
|
|
|
|
% This is a dominated key, so we need to skip over it
|
|
|
|
NewEntry = {LCnt, RestOfKeys},
|
|
|
|
find_nextkey(lists:keyreplace(LCnt, 1, QueryArray, NewEntry),
|
|
|
|
LCnt + 1,
|
|
|
|
{BKL, {BestKey, BestVal}},
|
2016-12-29 02:07:14 +00:00
|
|
|
StartKey, EndKey, Width);
|
2016-10-12 17:12:49 +01:00
|
|
|
SQN > BestSQN ->
|
|
|
|
% There is a real key at the front of this level and it has
|
|
|
|
% a higher SQN than the best key, so we should use this as
|
|
|
|
% the best key
|
|
|
|
% But we also need to remove the dominated key from the
|
|
|
|
% lower level in the query array
|
|
|
|
OldBestEntry = lists:keyfind(BKL, 1, QueryArray),
|
|
|
|
{BKL, [{BestKey, BestVal}|BestTail]} = OldBestEntry,
|
|
|
|
find_nextkey(lists:keyreplace(BKL,
|
|
|
|
1,
|
|
|
|
QueryArray,
|
|
|
|
{BKL, BestTail}),
|
|
|
|
LCnt + 1,
|
|
|
|
{LCnt, {Key, Val}},
|
2016-12-29 02:07:14 +00:00
|
|
|
StartKey, EndKey, Width)
|
2016-10-12 17:12:49 +01:00
|
|
|
end;
|
|
|
|
{_, BKL, BKV} ->
|
|
|
|
% This is not the best key
|
2016-12-29 02:07:14 +00:00
|
|
|
find_nextkey(QueryArray,
|
|
|
|
LCnt + 1,
|
|
|
|
{BKL, BKV},
|
|
|
|
StartKey, EndKey, Width)
|
2016-10-12 17:12:49 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2016-12-29 02:07:14 +00:00
|
|
|
keyfolder(IMMiter, SSTiter, StartKey, EndKey, {AccFun, Acc}) ->
|
|
|
|
keyfolder({IMMiter, SSTiter}, {StartKey, EndKey}, {AccFun, Acc}, -1).
|
2016-11-20 21:21:31 +00:00
|
|
|
|
|
|
|
keyfolder(_Iterators, _KeyRange, {_AccFun, Acc}, MaxKeys) when MaxKeys == 0 ->
|
|
|
|
Acc;
|
2016-12-29 02:07:14 +00:00
|
|
|
keyfolder({[], SSTiter}, KeyRange, {AccFun, Acc}, MaxKeys) ->
|
2016-11-20 21:21:31 +00:00
|
|
|
{StartKey, EndKey} = KeyRange,
|
2016-12-29 02:07:14 +00:00
|
|
|
case find_nextkey(SSTiter, StartKey, EndKey) of
|
2016-10-12 17:12:49 +01:00
|
|
|
no_more_keys ->
|
|
|
|
Acc;
|
2016-12-29 02:07:14 +00:00
|
|
|
{NxSSTiter, {SSTKey, SSTVal}} ->
|
|
|
|
Acc1 = AccFun(SSTKey, SSTVal, Acc),
|
|
|
|
keyfolder({[], NxSSTiter}, KeyRange, {AccFun, Acc1}, MaxKeys - 1)
|
2016-10-12 17:12:49 +01:00
|
|
|
end;
|
2016-12-29 02:07:14 +00:00
|
|
|
keyfolder({[{IMMKey, IMMVal}|NxIMMiterator], SSTiterator}, KeyRange,
|
2016-11-25 14:50:13 +00:00
|
|
|
{AccFun, Acc}, MaxKeys) ->
|
2016-11-20 21:21:31 +00:00
|
|
|
{StartKey, EndKey} = KeyRange,
|
2016-11-25 14:50:13 +00:00
|
|
|
case {IMMKey < StartKey, leveled_codec:endkey_passed(EndKey, IMMKey)} of
|
|
|
|
{true, _} ->
|
|
|
|
|
2016-11-20 21:21:31 +00:00
|
|
|
% Normally everything is pre-filterd, but the IMM iterator can
|
2016-11-25 14:50:13 +00:00
|
|
|
% be re-used and so may be behind the StartKey if the StartKey has
|
2016-11-20 21:21:31 +00:00
|
|
|
% advanced from the previous use
|
2016-12-29 02:07:14 +00:00
|
|
|
keyfolder({NxIMMiterator, SSTiterator},
|
2016-11-20 21:21:31 +00:00
|
|
|
KeyRange,
|
|
|
|
{AccFun, Acc},
|
|
|
|
MaxKeys);
|
2016-11-25 14:50:13 +00:00
|
|
|
{false, true} ->
|
|
|
|
% There are no more keys in-range in the in-memory
|
|
|
|
% iterator, so take action as if this iterator is empty
|
|
|
|
% (see above)
|
2016-12-29 02:07:14 +00:00
|
|
|
keyfolder({[], SSTiterator},
|
2016-11-25 14:50:13 +00:00
|
|
|
KeyRange,
|
|
|
|
{AccFun, Acc},
|
|
|
|
MaxKeys);
|
|
|
|
{false, false} ->
|
2016-12-29 02:07:14 +00:00
|
|
|
case find_nextkey(SSTiterator, StartKey, EndKey) of
|
2016-11-25 14:50:13 +00:00
|
|
|
no_more_keys ->
|
|
|
|
% No more keys in range in the persisted store, so use the
|
|
|
|
% in-memory KV as the next
|
|
|
|
Acc1 = AccFun(IMMKey, IMMVal, Acc),
|
2016-12-29 02:07:14 +00:00
|
|
|
keyfolder({NxIMMiterator, SSTiterator},
|
2016-11-20 21:21:31 +00:00
|
|
|
KeyRange,
|
2016-11-25 14:50:13 +00:00
|
|
|
{AccFun, Acc1},
|
|
|
|
MaxKeys - 1);
|
2016-12-29 02:07:14 +00:00
|
|
|
{NxSSTiterator, {SSTKey, SSTVal}} ->
|
2016-11-25 14:50:13 +00:00
|
|
|
% There is a next key, so need to know which is the
|
|
|
|
% next key between the two (and handle two keys
|
|
|
|
% with different sequence numbers).
|
|
|
|
case leveled_codec:key_dominates({IMMKey,
|
|
|
|
IMMVal},
|
2016-12-29 02:07:14 +00:00
|
|
|
{SSTKey,
|
|
|
|
SSTVal}) of
|
2016-11-25 14:50:13 +00:00
|
|
|
left_hand_first ->
|
2016-10-12 17:12:49 +01:00
|
|
|
Acc1 = AccFun(IMMKey, IMMVal, Acc),
|
2016-12-29 02:07:14 +00:00
|
|
|
keyfolder({NxIMMiterator, SSTiterator},
|
2016-11-20 21:21:31 +00:00
|
|
|
KeyRange,
|
|
|
|
{AccFun, Acc1},
|
|
|
|
MaxKeys - 1);
|
2016-11-25 14:50:13 +00:00
|
|
|
right_hand_first ->
|
2016-12-29 02:07:14 +00:00
|
|
|
Acc1 = AccFun(SSTKey, SSTVal, Acc),
|
2016-11-25 14:50:13 +00:00
|
|
|
keyfolder({[{IMMKey, IMMVal}|NxIMMiterator],
|
2016-12-29 02:07:14 +00:00
|
|
|
NxSSTiterator},
|
2016-11-25 14:50:13 +00:00
|
|
|
KeyRange,
|
|
|
|
{AccFun, Acc1},
|
|
|
|
MaxKeys - 1);
|
|
|
|
left_hand_dominant ->
|
|
|
|
Acc1 = AccFun(IMMKey, IMMVal, Acc),
|
2016-12-29 02:07:14 +00:00
|
|
|
keyfolder({NxIMMiterator, NxSSTiterator},
|
2016-11-25 14:50:13 +00:00
|
|
|
KeyRange,
|
|
|
|
{AccFun, Acc1},
|
|
|
|
MaxKeys - 1)
|
2016-10-12 17:12:49 +01:00
|
|
|
end
|
2016-10-13 17:51:47 +01:00
|
|
|
end
|
2016-10-12 17:12:49 +01:00
|
|
|
end.
|
|
|
|
|
2016-10-05 09:54:53 +01:00
|
|
|
|
2016-09-08 14:21:30 +01:00
|
|
|
filepath(RootPath, files) ->
|
2017-01-14 19:41:09 +00:00
|
|
|
FP = RootPath ++ "/" ++ ?FILES_FP,
|
|
|
|
filelib:ensure_dir(FP ++ "/"),
|
|
|
|
FP.
|
2016-09-08 14:21:30 +01:00
|
|
|
|
2016-08-09 16:09:29 +01:00
|
|
|
filepath(RootPath, NewMSN, new_merge_files) ->
|
2016-09-08 14:21:30 +01:00
|
|
|
filepath(RootPath, files) ++ "/" ++ integer_to_list(NewMSN).
|
2016-08-09 16:09:29 +01:00
|
|
|
|
2016-08-12 01:05:59 +01:00
|
|
|
|
|
|
|
|
2016-07-27 18:03:44 +01:00
|
|
|
%%%============================================================================
|
|
|
|
%%% Test
|
|
|
|
%%%============================================================================
|
|
|
|
|
2016-09-08 14:21:30 +01:00
|
|
|
-ifdef(TEST).
|
|
|
|
|
2016-12-29 02:07:14 +00:00
|
|
|
|
|
|
|
generate_randomkeys({Count, StartSQN}) ->
|
|
|
|
generate_randomkeys(Count, StartSQN, []);
|
|
|
|
generate_randomkeys(Count) ->
|
|
|
|
generate_randomkeys(Count, 0, []).
|
|
|
|
|
|
|
|
generate_randomkeys(0, _SQN, Acc) ->
|
|
|
|
lists:reverse(Acc);
|
|
|
|
generate_randomkeys(Count, SQN, Acc) ->
|
|
|
|
K = {o,
|
|
|
|
lists:concat(["Bucket", random:uniform(1024)]),
|
|
|
|
lists:concat(["Key", random:uniform(1024)]),
|
|
|
|
null},
|
|
|
|
RandKey = {K,
|
|
|
|
{SQN,
|
|
|
|
{active, infinity},
|
|
|
|
leveled_codec:magic_hash(K),
|
|
|
|
null}},
|
|
|
|
generate_randomkeys(Count - 1, SQN + 1, [RandKey|Acc]).
|
|
|
|
|
|
|
|
|
2016-09-08 14:21:30 +01:00
|
|
|
clean_testdir(RootPath) ->
|
2017-01-17 14:11:50 +00:00
|
|
|
clean_subdir(leveled_pmanifest:filepath(RootPath, manifest)),
|
2016-09-08 14:21:30 +01:00
|
|
|
clean_subdir(filepath(RootPath, files)).
|
|
|
|
|
|
|
|
clean_subdir(DirPath) ->
|
|
|
|
case filelib:is_dir(DirPath) of
|
|
|
|
true ->
|
|
|
|
{ok, Files} = file:list_dir(DirPath),
|
2016-10-13 17:51:47 +01:00
|
|
|
lists:foreach(fun(FN) ->
|
|
|
|
File = filename:join(DirPath, FN),
|
2016-11-03 16:46:25 +00:00
|
|
|
ok = file:delete(File),
|
|
|
|
io:format("Success deleting ~s~n", [File])
|
|
|
|
end,
|
2016-09-08 14:21:30 +01:00
|
|
|
Files);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end.
|
2016-07-27 18:03:44 +01:00
|
|
|
|
2016-10-18 19:41:33 +01:00
|
|
|
|
2016-10-26 21:03:50 +01:00
|
|
|
maybe_pause_push(PCL, KL) ->
|
2017-01-20 16:36:20 +00:00
|
|
|
T0 = [],
|
2017-01-05 21:58:33 +00:00
|
|
|
I0 = leveled_pmem:new_index(),
|
|
|
|
T1 = lists:foldl(fun({K, V}, {AccSL, AccIdx, MinSQN, MaxSQN}) ->
|
2017-01-20 16:36:20 +00:00
|
|
|
UpdSL = [{K, V}|AccSL],
|
2016-12-11 01:02:56 +00:00
|
|
|
SQN = leveled_codec:strip_to_seqonly({K, V}),
|
2017-01-05 21:58:33 +00:00
|
|
|
H = leveled_codec:magic_hash(K),
|
|
|
|
UpdIdx = leveled_pmem:prepare_for_index(AccIdx, H),
|
|
|
|
{UpdSL, UpdIdx, min(SQN, MinSQN), max(SQN, MaxSQN)}
|
2016-12-11 01:02:56 +00:00
|
|
|
end,
|
2017-01-05 21:58:33 +00:00
|
|
|
{T0, I0, infinity, 0},
|
2016-10-27 20:56:18 +01:00
|
|
|
KL),
|
2017-01-20 16:36:20 +00:00
|
|
|
SL = element(1, T1),
|
2017-01-21 11:38:26 +00:00
|
|
|
Tree = leveled_tree:from_orderedlist(lists:ukeysort(1, SL), ?CACHE_TYPE),
|
2017-01-20 16:36:20 +00:00
|
|
|
T2 = setelement(1, T1, Tree),
|
|
|
|
case pcl_pushmem(PCL, T2) of
|
2016-10-30 18:25:30 +00:00
|
|
|
returned ->
|
2016-10-27 20:56:18 +01:00
|
|
|
timer:sleep(50),
|
2016-10-26 21:03:50 +01:00
|
|
|
maybe_pause_push(PCL, KL);
|
2016-10-27 20:56:18 +01:00
|
|
|
ok ->
|
2016-10-08 22:15:48 +01:00
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2016-12-11 01:02:56 +00:00
|
|
|
%% old test data doesn't have the magic hash
|
|
|
|
add_missing_hash({K, {SQN, ST, MD}}) ->
|
|
|
|
{K, {SQN, ST, leveled_codec:magic_hash(K), MD}}.
|
|
|
|
|
|
|
|
|
2017-02-26 20:52:40 +00:00
|
|
|
clean_dir_test() ->
|
|
|
|
% Pointless gesture to test coverage
|
|
|
|
RootPath = "../test/ledger",
|
|
|
|
?assertMatch(ok, file:write_file(RootPath ++ "/test.bob", "hello")),
|
2017-02-27 20:23:36 +00:00
|
|
|
ok = clean_subdir(RootPath ++ "/test.bob"),
|
|
|
|
ok = file:delete(RootPath ++ "/test.bob").
|
2017-02-26 20:52:40 +00:00
|
|
|
|
2016-09-08 14:21:30 +01:00
|
|
|
simple_server_test() ->
|
|
|
|
RootPath = "../test/ledger",
|
|
|
|
clean_testdir(RootPath),
|
|
|
|
{ok, PCL} = pcl_start(#penciller_options{root_path=RootPath,
|
|
|
|
max_inmemory_tablesize=1000}),
|
2016-12-11 01:02:56 +00:00
|
|
|
Key1_Pre = {{o,"Bucket0001", "Key0001", null},
|
|
|
|
{1, {active, infinity}, null}},
|
|
|
|
Key1 = add_missing_hash(Key1_Pre),
|
2016-12-29 02:07:14 +00:00
|
|
|
KL1 = generate_randomkeys({1000, 2}),
|
2016-12-11 01:02:56 +00:00
|
|
|
Key2_Pre = {{o,"Bucket0002", "Key0002", null},
|
2016-11-07 10:11:57 +00:00
|
|
|
{1002, {active, infinity}, null}},
|
2016-12-11 01:02:56 +00:00
|
|
|
Key2 = add_missing_hash(Key2_Pre),
|
2016-12-29 02:07:14 +00:00
|
|
|
KL2 = generate_randomkeys({900, 1003}),
|
2016-11-07 10:11:57 +00:00
|
|
|
% Keep below the max table size by having 900 not 1000
|
2016-12-11 01:02:56 +00:00
|
|
|
Key3_Pre = {{o,"Bucket0003", "Key0003", null},
|
2016-11-07 10:11:57 +00:00
|
|
|
{2003, {active, infinity}, null}},
|
2016-12-11 01:02:56 +00:00
|
|
|
Key3 = add_missing_hash(Key3_Pre),
|
2016-12-29 02:07:14 +00:00
|
|
|
KL3 = generate_randomkeys({1000, 2004}),
|
2016-12-11 01:02:56 +00:00
|
|
|
Key4_Pre = {{o,"Bucket0004", "Key0004", null},
|
2016-11-07 10:11:57 +00:00
|
|
|
{3004, {active, infinity}, null}},
|
2016-12-11 01:02:56 +00:00
|
|
|
Key4 = add_missing_hash(Key4_Pre),
|
2016-12-29 02:07:14 +00:00
|
|
|
KL4 = generate_randomkeys({1000, 3005}),
|
2016-10-27 20:56:18 +01:00
|
|
|
ok = maybe_pause_push(PCL, [Key1]),
|
2016-10-12 17:12:49 +01:00
|
|
|
?assertMatch(Key1, pcl_fetch(PCL, {o,"Bucket0001", "Key0001", null})),
|
2016-10-27 20:56:18 +01:00
|
|
|
ok = maybe_pause_push(PCL, KL1),
|
2016-10-12 17:12:49 +01:00
|
|
|
?assertMatch(Key1, pcl_fetch(PCL, {o,"Bucket0001", "Key0001", null})),
|
2016-10-26 21:03:50 +01:00
|
|
|
ok = maybe_pause_push(PCL, [Key2]),
|
2016-10-12 17:12:49 +01:00
|
|
|
?assertMatch(Key1, pcl_fetch(PCL, {o,"Bucket0001", "Key0001", null})),
|
|
|
|
?assertMatch(Key2, pcl_fetch(PCL, {o,"Bucket0002", "Key0002", null})),
|
2016-10-27 20:56:18 +01:00
|
|
|
|
2016-10-26 21:03:50 +01:00
|
|
|
ok = maybe_pause_push(PCL, KL2),
|
2016-10-27 20:56:18 +01:00
|
|
|
?assertMatch(Key2, pcl_fetch(PCL, {o,"Bucket0002", "Key0002", null})),
|
2016-10-26 21:03:50 +01:00
|
|
|
ok = maybe_pause_push(PCL, [Key3]),
|
2016-10-19 17:34:58 +01:00
|
|
|
|
2016-10-12 17:12:49 +01:00
|
|
|
?assertMatch(Key1, pcl_fetch(PCL, {o,"Bucket0001", "Key0001", null})),
|
|
|
|
?assertMatch(Key2, pcl_fetch(PCL, {o,"Bucket0002", "Key0002", null})),
|
|
|
|
?assertMatch(Key3, pcl_fetch(PCL, {o,"Bucket0003", "Key0003", null})),
|
2016-11-07 10:11:57 +00:00
|
|
|
timer:sleep(200),
|
|
|
|
% This sleep should make sure that the merge to L1 has occurred
|
|
|
|
% This will free up the L0 slot for the remainder to be written in shutdown
|
2016-09-08 14:21:30 +01:00
|
|
|
ok = pcl_close(PCL),
|
2016-10-27 20:56:18 +01:00
|
|
|
|
2016-09-08 14:21:30 +01:00
|
|
|
{ok, PCLr} = pcl_start(#penciller_options{root_path=RootPath,
|
|
|
|
max_inmemory_tablesize=1000}),
|
2016-11-07 10:11:57 +00:00
|
|
|
?assertMatch(2003, pcl_getstartupsequencenumber(PCLr)),
|
|
|
|
% ok = maybe_pause_push(PCLr, [Key2] ++ KL2 ++ [Key3]),
|
2016-10-27 20:56:18 +01:00
|
|
|
|
2016-10-12 17:12:49 +01:00
|
|
|
?assertMatch(Key1, pcl_fetch(PCLr, {o,"Bucket0001", "Key0001", null})),
|
|
|
|
?assertMatch(Key2, pcl_fetch(PCLr, {o,"Bucket0002", "Key0002", null})),
|
|
|
|
?assertMatch(Key3, pcl_fetch(PCLr, {o,"Bucket0003", "Key0003", null})),
|
2016-10-26 21:03:50 +01:00
|
|
|
ok = maybe_pause_push(PCLr, KL3),
|
|
|
|
ok = maybe_pause_push(PCLr, [Key4]),
|
|
|
|
ok = maybe_pause_push(PCLr, KL4),
|
2016-10-12 17:12:49 +01:00
|
|
|
?assertMatch(Key1, pcl_fetch(PCLr, {o,"Bucket0001", "Key0001", null})),
|
|
|
|
?assertMatch(Key2, pcl_fetch(PCLr, {o,"Bucket0002", "Key0002", null})),
|
|
|
|
?assertMatch(Key3, pcl_fetch(PCLr, {o,"Bucket0003", "Key0003", null})),
|
|
|
|
?assertMatch(Key4, pcl_fetch(PCLr, {o,"Bucket0004", "Key0004", null})),
|
2017-01-14 19:41:09 +00:00
|
|
|
|
2016-09-26 10:55:08 +01:00
|
|
|
SnapOpts = #penciller_options{start_snapshot = true,
|
2016-10-07 10:04:48 +01:00
|
|
|
source_penciller = PCLr},
|
2016-09-26 10:55:08 +01:00
|
|
|
{ok, PclSnap} = pcl_start(SnapOpts),
|
2016-12-11 01:02:56 +00:00
|
|
|
leveled_bookie:load_snapshot(PclSnap,
|
|
|
|
leveled_bookie:empty_ledgercache()),
|
2017-01-14 19:41:09 +00:00
|
|
|
|
2016-10-12 17:12:49 +01:00
|
|
|
?assertMatch(Key1, pcl_fetch(PclSnap, {o,"Bucket0001", "Key0001", null})),
|
|
|
|
?assertMatch(Key2, pcl_fetch(PclSnap, {o,"Bucket0002", "Key0002", null})),
|
|
|
|
?assertMatch(Key3, pcl_fetch(PclSnap, {o,"Bucket0003", "Key0003", null})),
|
|
|
|
?assertMatch(Key4, pcl_fetch(PclSnap, {o,"Bucket0004", "Key0004", null})),
|
2016-09-26 10:55:08 +01:00
|
|
|
?assertMatch(true, pcl_checksequencenumber(PclSnap,
|
2016-10-12 17:12:49 +01:00
|
|
|
{o,
|
|
|
|
"Bucket0001",
|
|
|
|
"Key0001",
|
|
|
|
null},
|
2016-09-26 10:55:08 +01:00
|
|
|
1)),
|
|
|
|
?assertMatch(true, pcl_checksequencenumber(PclSnap,
|
2016-10-12 17:12:49 +01:00
|
|
|
{o,
|
|
|
|
"Bucket0002",
|
|
|
|
"Key0002",
|
|
|
|
null},
|
2016-09-26 10:55:08 +01:00
|
|
|
1002)),
|
|
|
|
?assertMatch(true, pcl_checksequencenumber(PclSnap,
|
2016-10-12 17:12:49 +01:00
|
|
|
{o,
|
|
|
|
"Bucket0003",
|
|
|
|
"Key0003",
|
|
|
|
null},
|
2016-10-27 20:56:18 +01:00
|
|
|
2003)),
|
2016-09-26 10:55:08 +01:00
|
|
|
?assertMatch(true, pcl_checksequencenumber(PclSnap,
|
2016-10-12 17:12:49 +01:00
|
|
|
{o,
|
|
|
|
"Bucket0004",
|
|
|
|
"Key0004",
|
|
|
|
null},
|
2016-10-27 20:56:18 +01:00
|
|
|
3004)),
|
2016-10-20 02:23:45 +01:00
|
|
|
% Add some more keys and confirm that check sequence number still
|
2016-09-26 10:55:08 +01:00
|
|
|
% sees the old version in the previous snapshot, but will see the new version
|
|
|
|
% in a new snapshot
|
2017-01-14 19:41:09 +00:00
|
|
|
|
2016-12-11 01:02:56 +00:00
|
|
|
Key1A_Pre = {{o,"Bucket0001", "Key0001", null},
|
|
|
|
{4005, {active, infinity}, null}},
|
|
|
|
Key1A = add_missing_hash(Key1A_Pre),
|
2016-12-29 02:07:14 +00:00
|
|
|
KL1A = generate_randomkeys({2000, 4006}),
|
2016-10-26 21:03:50 +01:00
|
|
|
ok = maybe_pause_push(PCLr, [Key1A]),
|
|
|
|
ok = maybe_pause_push(PCLr, KL1A),
|
2016-09-26 10:55:08 +01:00
|
|
|
?assertMatch(true, pcl_checksequencenumber(PclSnap,
|
2016-10-12 17:12:49 +01:00
|
|
|
{o,
|
|
|
|
"Bucket0001",
|
|
|
|
"Key0001",
|
|
|
|
null},
|
2016-09-26 10:55:08 +01:00
|
|
|
1)),
|
|
|
|
ok = pcl_close(PclSnap),
|
2017-01-14 19:41:09 +00:00
|
|
|
|
2016-09-26 10:55:08 +01:00
|
|
|
{ok, PclSnap2} = pcl_start(SnapOpts),
|
2016-12-11 01:02:56 +00:00
|
|
|
leveled_bookie:load_snapshot(PclSnap2, leveled_bookie:empty_ledgercache()),
|
2016-09-26 10:55:08 +01:00
|
|
|
?assertMatch(false, pcl_checksequencenumber(PclSnap2,
|
2016-10-12 17:12:49 +01:00
|
|
|
{o,
|
|
|
|
"Bucket0001",
|
|
|
|
"Key0001",
|
|
|
|
null},
|
2016-09-26 10:55:08 +01:00
|
|
|
1)),
|
|
|
|
?assertMatch(true, pcl_checksequencenumber(PclSnap2,
|
2016-10-12 17:12:49 +01:00
|
|
|
{o,
|
|
|
|
"Bucket0001",
|
|
|
|
"Key0001",
|
|
|
|
null},
|
2016-10-27 20:56:18 +01:00
|
|
|
4005)),
|
2016-09-26 10:55:08 +01:00
|
|
|
?assertMatch(true, pcl_checksequencenumber(PclSnap2,
|
2016-10-12 17:12:49 +01:00
|
|
|
{o,
|
|
|
|
"Bucket0002",
|
|
|
|
"Key0002",
|
|
|
|
null},
|
2016-09-26 10:55:08 +01:00
|
|
|
1002)),
|
|
|
|
ok = pcl_close(PclSnap2),
|
2016-09-08 14:21:30 +01:00
|
|
|
ok = pcl_close(PCLr),
|
|
|
|
clean_testdir(RootPath).
|
|
|
|
|
2016-09-21 18:31:42 +01:00
|
|
|
|
2016-10-12 17:12:49 +01:00
|
|
|
simple_findnextkey_test() ->
|
|
|
|
QueryArray = [
|
|
|
|
{2, [{{o, "Bucket1", "Key1"}, {5, {active, infinity}, null}},
|
|
|
|
{{o, "Bucket1", "Key5"}, {4, {active, infinity}, null}}]},
|
|
|
|
{3, [{{o, "Bucket1", "Key3"}, {3, {active, infinity}, null}}]},
|
|
|
|
{5, [{{o, "Bucket1", "Key2"}, {2, {active, infinity}, null}}]}
|
|
|
|
],
|
|
|
|
{Array2, KV1} = find_nextkey(QueryArray,
|
|
|
|
{o, "Bucket1", "Key0"},
|
|
|
|
{o, "Bucket1", "Key5"}),
|
|
|
|
?assertMatch({{o, "Bucket1", "Key1"}, {5, {active, infinity}, null}}, KV1),
|
|
|
|
{Array3, KV2} = find_nextkey(Array2,
|
|
|
|
{o, "Bucket1", "Key0"},
|
|
|
|
{o, "Bucket1", "Key5"}),
|
|
|
|
?assertMatch({{o, "Bucket1", "Key2"}, {2, {active, infinity}, null}}, KV2),
|
|
|
|
{Array4, KV3} = find_nextkey(Array3,
|
|
|
|
{o, "Bucket1", "Key0"},
|
|
|
|
{o, "Bucket1", "Key5"}),
|
|
|
|
?assertMatch({{o, "Bucket1", "Key3"}, {3, {active, infinity}, null}}, KV3),
|
|
|
|
{Array5, KV4} = find_nextkey(Array4,
|
|
|
|
{o, "Bucket1", "Key0"},
|
|
|
|
{o, "Bucket1", "Key5"}),
|
|
|
|
?assertMatch({{o, "Bucket1", "Key5"}, {4, {active, infinity}, null}}, KV4),
|
|
|
|
ER = find_nextkey(Array5,
|
|
|
|
{o, "Bucket1", "Key0"},
|
|
|
|
{o, "Bucket1", "Key5"}),
|
|
|
|
?assertMatch(no_more_keys, ER).
|
|
|
|
|
|
|
|
sqnoverlap_findnextkey_test() ->
|
|
|
|
QueryArray = [
|
2016-12-11 01:02:56 +00:00
|
|
|
{2, [{{o, "Bucket1", "Key1"}, {5, {active, infinity}, 0, null}},
|
|
|
|
{{o, "Bucket1", "Key5"}, {4, {active, infinity}, 0, null}}]},
|
|
|
|
{3, [{{o, "Bucket1", "Key3"}, {3, {active, infinity}, 0, null}}]},
|
|
|
|
{5, [{{o, "Bucket1", "Key5"}, {2, {active, infinity}, 0, null}}]}
|
2016-10-12 17:12:49 +01:00
|
|
|
],
|
|
|
|
{Array2, KV1} = find_nextkey(QueryArray,
|
|
|
|
{o, "Bucket1", "Key0"},
|
|
|
|
{o, "Bucket1", "Key5"}),
|
2016-12-11 01:02:56 +00:00
|
|
|
?assertMatch({{o, "Bucket1", "Key1"}, {5, {active, infinity}, 0, null}},
|
|
|
|
KV1),
|
2016-10-12 17:12:49 +01:00
|
|
|
{Array3, KV2} = find_nextkey(Array2,
|
|
|
|
{o, "Bucket1", "Key0"},
|
|
|
|
{o, "Bucket1", "Key5"}),
|
2016-12-11 01:02:56 +00:00
|
|
|
?assertMatch({{o, "Bucket1", "Key3"}, {3, {active, infinity}, 0, null}},
|
|
|
|
KV2),
|
2016-10-12 17:12:49 +01:00
|
|
|
{Array4, KV3} = find_nextkey(Array3,
|
|
|
|
{o, "Bucket1", "Key0"},
|
|
|
|
{o, "Bucket1", "Key5"}),
|
2016-12-11 01:02:56 +00:00
|
|
|
?assertMatch({{o, "Bucket1", "Key5"}, {4, {active, infinity}, 0, null}},
|
|
|
|
KV3),
|
2016-10-12 17:12:49 +01:00
|
|
|
ER = find_nextkey(Array4,
|
|
|
|
{o, "Bucket1", "Key0"},
|
|
|
|
{o, "Bucket1", "Key5"}),
|
|
|
|
?assertMatch(no_more_keys, ER).
|
|
|
|
|
|
|
|
sqnoverlap_otherway_findnextkey_test() ->
|
|
|
|
QueryArray = [
|
2016-12-11 01:02:56 +00:00
|
|
|
{2, [{{o, "Bucket1", "Key1"}, {5, {active, infinity}, 0, null}},
|
|
|
|
{{o, "Bucket1", "Key5"}, {1, {active, infinity}, 0, null}}]},
|
|
|
|
{3, [{{o, "Bucket1", "Key3"}, {3, {active, infinity}, 0, null}}]},
|
|
|
|
{5, [{{o, "Bucket1", "Key5"}, {2, {active, infinity}, 0, null}}]}
|
2016-10-12 17:12:49 +01:00
|
|
|
],
|
|
|
|
{Array2, KV1} = find_nextkey(QueryArray,
|
|
|
|
{o, "Bucket1", "Key0"},
|
|
|
|
{o, "Bucket1", "Key5"}),
|
2016-12-11 01:02:56 +00:00
|
|
|
?assertMatch({{o, "Bucket1", "Key1"}, {5, {active, infinity}, 0, null}},
|
|
|
|
KV1),
|
2016-10-12 17:12:49 +01:00
|
|
|
{Array3, KV2} = find_nextkey(Array2,
|
|
|
|
{o, "Bucket1", "Key0"},
|
|
|
|
{o, "Bucket1", "Key5"}),
|
2016-12-11 01:02:56 +00:00
|
|
|
?assertMatch({{o, "Bucket1", "Key3"}, {3, {active, infinity}, 0, null}},
|
|
|
|
KV2),
|
2016-10-12 17:12:49 +01:00
|
|
|
{Array4, KV3} = find_nextkey(Array3,
|
|
|
|
{o, "Bucket1", "Key0"},
|
|
|
|
{o, "Bucket1", "Key5"}),
|
2016-12-11 01:02:56 +00:00
|
|
|
?assertMatch({{o, "Bucket1", "Key5"}, {2, {active, infinity}, 0, null}},
|
|
|
|
KV3),
|
2016-10-12 17:12:49 +01:00
|
|
|
ER = find_nextkey(Array4,
|
|
|
|
{o, "Bucket1", "Key0"},
|
|
|
|
{o, "Bucket1", "Key5"}),
|
|
|
|
?assertMatch(no_more_keys, ER).
|
|
|
|
|
|
|
|
foldwithimm_simple_test() ->
|
|
|
|
QueryArray = [
|
2017-01-20 16:36:20 +00:00
|
|
|
{2, [{{o, "Bucket1", "Key1", null},
|
|
|
|
{5, {active, infinity}, 0, null}},
|
|
|
|
{{o, "Bucket1", "Key5", null},
|
|
|
|
{1, {active, infinity}, 0, null}}]},
|
|
|
|
{3, [{{o, "Bucket1", "Key3", null},
|
|
|
|
{3, {active, infinity}, 0, null}}]},
|
|
|
|
{5, [{{o, "Bucket1", "Key5", null},
|
|
|
|
{2, {active, infinity}, 0, null}}]}
|
2016-10-12 17:12:49 +01:00
|
|
|
],
|
2017-01-20 16:36:20 +00:00
|
|
|
KL1A = [{{o, "Bucket1", "Key6", null}, {7, {active, infinity}, 0, null}},
|
|
|
|
{{o, "Bucket1", "Key1", null}, {8, {active, infinity}, 0, null}},
|
|
|
|
{{o, "Bucket1", "Key8", null}, {9, {active, infinity}, 0, null}}],
|
2017-01-21 11:38:26 +00:00
|
|
|
IMM2 = leveled_tree:from_orderedlist(lists:ukeysort(1, KL1A), ?CACHE_TYPE),
|
2017-01-20 16:36:20 +00:00
|
|
|
IMMiter = leveled_tree:match_range({o, "Bucket1", "Key1", null},
|
|
|
|
{o, null, null, null},
|
|
|
|
IMM2),
|
2016-10-13 21:02:15 +01:00
|
|
|
AccFun = fun(K, V, Acc) -> SQN = leveled_codec:strip_to_seqonly({K, V}),
|
2016-10-12 17:12:49 +01:00
|
|
|
Acc ++ [{K, SQN}] end,
|
|
|
|
Acc = keyfolder(IMMiter,
|
|
|
|
QueryArray,
|
2017-01-20 16:36:20 +00:00
|
|
|
{o, "Bucket1", "Key1", null}, {o, "Bucket1", "Key6", null},
|
2016-10-12 17:12:49 +01:00
|
|
|
{AccFun, []}),
|
2017-01-20 16:36:20 +00:00
|
|
|
?assertMatch([{{o, "Bucket1", "Key1", null}, 8},
|
|
|
|
{{o, "Bucket1", "Key3", null}, 3},
|
|
|
|
{{o, "Bucket1", "Key5", null}, 2},
|
|
|
|
{{o, "Bucket1", "Key6", null}, 7}], Acc),
|
2016-10-12 17:12:49 +01:00
|
|
|
|
2017-01-20 16:36:20 +00:00
|
|
|
IMMiterA = [{{o, "Bucket1", "Key1", null},
|
|
|
|
{8, {active, infinity}, 0, null}}],
|
2016-10-12 17:12:49 +01:00
|
|
|
AccA = keyfolder(IMMiterA,
|
2017-01-20 16:36:20 +00:00
|
|
|
QueryArray,
|
|
|
|
{o, "Bucket1", "Key1", null}, {o, "Bucket1", "Key6", null},
|
|
|
|
{AccFun, []}),
|
|
|
|
?assertMatch([{{o, "Bucket1", "Key1", null}, 8},
|
|
|
|
{{o, "Bucket1", "Key3", null}, 3},
|
|
|
|
{{o, "Bucket1", "Key5", null}, 2}], AccA),
|
2016-10-12 17:12:49 +01:00
|
|
|
|
2017-01-20 16:36:20 +00:00
|
|
|
KL1B = [{{o, "Bucket1", "Key4", null}, {10, {active, infinity}, 0, null}}|KL1A],
|
2017-01-21 11:38:26 +00:00
|
|
|
IMM3 = leveled_tree:from_orderedlist(lists:ukeysort(1, KL1B), ?CACHE_TYPE),
|
2017-01-20 16:36:20 +00:00
|
|
|
IMMiterB = leveled_tree:match_range({o, "Bucket1", "Key1", null},
|
|
|
|
{o, null, null, null},
|
|
|
|
IMM3),
|
2016-10-12 17:12:49 +01:00
|
|
|
AccB = keyfolder(IMMiterB,
|
|
|
|
QueryArray,
|
2017-01-20 16:36:20 +00:00
|
|
|
{o, "Bucket1", "Key1", null}, {o, "Bucket1", "Key6", null},
|
2016-10-12 17:12:49 +01:00
|
|
|
{AccFun, []}),
|
2017-01-20 16:36:20 +00:00
|
|
|
?assertMatch([{{o, "Bucket1", "Key1", null}, 8},
|
|
|
|
{{o, "Bucket1", "Key3", null}, 3},
|
|
|
|
{{o, "Bucket1", "Key4", null}, 10},
|
|
|
|
{{o, "Bucket1", "Key5", null}, 2},
|
|
|
|
{{o, "Bucket1", "Key6", null}, 7}], AccB).
|
2016-10-12 17:12:49 +01:00
|
|
|
|
2016-10-29 00:52:49 +01:00
|
|
|
create_file_test() ->
|
2016-12-29 02:07:14 +00:00
|
|
|
Filename = "../test/new_file.sst",
|
2016-10-29 00:52:49 +01:00
|
|
|
ok = file:write_file(Filename, term_to_binary("hello")),
|
2016-12-29 02:07:14 +00:00
|
|
|
KVL = lists:usort(generate_randomkeys(10000)),
|
2017-01-21 11:38:26 +00:00
|
|
|
Tree = leveled_tree:from_orderedlist(KVL, ?CACHE_TYPE),
|
2016-10-31 01:33:33 +00:00
|
|
|
FetchFun = fun(Slot) -> lists:nth(Slot, [Tree]) end,
|
2016-10-30 18:25:30 +00:00
|
|
|
{ok,
|
|
|
|
SP,
|
2016-12-29 02:07:14 +00:00
|
|
|
noreply} = leveled_sst:sst_newlevelzero(Filename,
|
2016-10-31 01:33:33 +00:00
|
|
|
1,
|
|
|
|
FetchFun,
|
2016-12-29 02:07:14 +00:00
|
|
|
undefined,
|
|
|
|
10000),
|
2016-10-29 00:52:49 +01:00
|
|
|
lists:foreach(fun(X) ->
|
|
|
|
case checkready(SP) of
|
|
|
|
timeout ->
|
|
|
|
timer:sleep(X);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end end,
|
|
|
|
[50, 50, 50, 50, 50]),
|
|
|
|
{ok, SrcFN, StartKey, EndKey} = checkready(SP),
|
|
|
|
io:format("StartKey ~w EndKey ~w~n", [StartKey, EndKey]),
|
|
|
|
?assertMatch({o, _, _, _}, StartKey),
|
|
|
|
?assertMatch({o, _, _, _}, EndKey),
|
2016-12-29 02:07:14 +00:00
|
|
|
?assertMatch("../test/new_file.sst", SrcFN),
|
|
|
|
ok = leveled_sst:sst_clear(SP),
|
|
|
|
{ok, Bin} = file:read_file("../test/new_file.sst.discarded"),
|
2016-10-29 00:52:49 +01:00
|
|
|
?assertMatch("hello", binary_to_term(Bin)).
|
|
|
|
|
2017-02-26 21:48:04 +00:00
|
|
|
slow_fetch_test() ->
|
|
|
|
?assertMatch(not_present, log_slowfetch(2, not_present, "fake", 1)).
|
|
|
|
|
2016-11-05 13:42:44 +00:00
|
|
|
checkready(Pid) ->
|
|
|
|
try
|
2016-12-29 02:07:14 +00:00
|
|
|
leveled_sst:sst_checkready(Pid)
|
2016-11-05 13:42:44 +00:00
|
|
|
catch
|
|
|
|
exit:{timeout, _} ->
|
|
|
|
timeout
|
|
|
|
end.
|
|
|
|
|
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-11-05 13:42:44 +00:00
|
|
|
|
2016-11-05 15:59:31 +00:00
|
|
|
-endif.
|