Skip to content

Commit

Permalink
Start key and iterator type
Browse files Browse the repository at this point in the history
Added the ability from where to start the iterator
and the type of the iterator itself. Start key can
be set as a function (dynamic parameter) or just a
static value. The type of the iterator can be specified
either with the box.index.* constant,
or with the name for example, 'EQ' or box.index.EQ

Needed for: #50
  • Loading branch information
ArtDu authored and ligurio committed Jul 6, 2021
1 parent d1706fa commit 7b4b1a2
Show file tree
Hide file tree
Showing 4 changed files with 724 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ Run a scheduled task to check and process (expire) tuples in a given space.
* `index` - Name or id of the index to iterate on. If omitted, will use the primary index.
If there's no index with this name, will throw an error.
Supported index types are TREE and HASH, using other types will result in an error.
* `iterator_type` - Type of the iterator to use, as string or box.index constant, for example, "EQ" or box.index.EQ.
Default is box.index.ALL.
See https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_index/pairs/.
* `start_key` - Start iterating from the tuple with this index value. If the iterator is "EQ", iterate over tuples with this index value.
The index value may be a single value, if the index consists of one field, a tuple with the index key parts, or a function which returns such value.
If omitted or nil, all tuples will be checked.
* `tuples_per_iteration` - Number of tuples to check in one batch (iteration). Default is 1024.
* `on_full_scan_start` - Function to call before starting a tuple scan.
* `on_full_scan_complete` - Function to call after completing a full scan.
Expand Down
35 changes: 33 additions & 2 deletions expirationd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ local constants = {
default_vinyl_assumed_space_len_factor = 2,
-- default function on full scan
default_on_full_scan = function() end,
-- default function for start_key
start_key = function() return nil end,
-- default iterating over the loop will go in ascending index
iterator_type = "ALL",
}

-- ========================================================================= --
Expand Down Expand Up @@ -93,7 +97,7 @@ local function default_do_worker_iteration(task)
local space_len = task.vinyl_assumed_space_len
local checked_tuples_count = 0
local vinyl_checked_tuples_count = 0
for _, tuple in task.index:pairs(nil, {iterator = box.index.ALL}) do
for _, tuple in task.index:pairs(task.start_key(), {iterator = task.iterator_type}) do
checked_tuples_count = checked_tuples_count + 1
vinyl_checked_tuples_count = vinyl_checked_tuples_count + 1
expiration_process(task, tuple)
Expand Down Expand Up @@ -232,6 +236,8 @@ local function create_task(name)
on_full_scan_success = constants.default_on_full_scan,
on_full_scan_start = constants.default_on_full_scan,
on_full_scan_complete = constants.default_on_full_scan,
start_key = constants.start_key,
iterator_type = constants.iterator_type,
}, { __index = Task_methods })
return task
end
Expand Down Expand Up @@ -274,13 +280,21 @@ end
-- * index -- Name or id of the index to iterate on; if omitted, will use the primary index;
-- if there's no index with this name, will throw an error.
-- supported index types are TREE and HASH, using other types will result in an error.
-- * iterator_type -- Type of the iterator to use, as string or box.index constant,
-- for example, 'EQ' or box.index.EQ; default is box.index.ALL;
-- see https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_index/pairs/.
-- * start_key -- Start iterating from the tuple with this index value;
-- or when iterator is 'EQ', iterate over tuples with this index value;
-- must be a value of the same data type as the index field or fields,
-- or a function which returns such value;
-- if omitted or nil, all tuples will be checked.
-- * tuples_per_iteration -- Number of tuples to check in one batch (iteration); default is 1024.
-- * on_full_scan_start -- Function to call before starting a full scan iteration.
-- * on_full_scan_complete -- Function to call after completing a full scan iteration.
-- * on_full_scan_success -- Function to call after successfully completing a full scan iteration.
-- * on_full_scan_error -- Function to call after terminating a full scan due to an error.
-- * args -- Passed to is_tuple_expired and
-- process_expired_tuple() as additional context.
-- * tuples_per_iteration -- Number of tuples to check in one batch (iteration); default is 1024.
-- * full_scan_time -- Time required for a full index scan (in seconds).
-- * iteration_delay -- Max sleep time between iterations (in seconds).
-- * full_scan_delay -- Sleep time between full scans (in seconds).
Expand Down Expand Up @@ -344,6 +358,23 @@ local function expirationd_run_task(name, space_id, is_tuple_expired, options)
end
task.index = expire_index

-- check iterator_type
if options.iterator_type ~= nil then
task.iterator_type = options.iterator_type
end

-- check start_key
if options.start_key ~= nil or options.start_key == box.NULL then
if type(options.start_key) == "function" then
task.start_key = function() return options.start_key() end
else
task.start_key = function() return options.start_key end
end
end

-- check valid of iterator_type and start key
task.index:pairs( task.start_key(), { iterator = task.iterator_type })

-- check expire and process after expiration handler's arguments
task.args = options.args

Expand Down
Loading

0 comments on commit 7b4b1a2

Please sign in to comment.