Skip to content

Commit

Permalink
internal: use vshard router object
Browse files Browse the repository at this point in the history
This patch is the groundwork for vshard groups and custom routers
support.

Test runs have shown that this patch do not affects the performance of
crud requests.

Part of #44
  • Loading branch information
DifferentialOrange committed Aug 31, 2022
1 parent 1920cca commit 2519df9
Show file tree
Hide file tree
Showing 21 changed files with 54 additions and 29 deletions.
5 changes: 3 additions & 2 deletions crud/borders.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ local function call_get_border_on_router(border_name, space_name, index_name, op

opts = opts or {}

local space = utils.get_space(space_name, vshard.router.routeall())
local vshard_router = vshard.router.static
local replicasets = vshard_router:routeall()
local space = utils.get_space(space_name, replicasets)
if space == nil then
return nil, BorderError:new("Space %q doesn't exist", space_name), const.NEED_SCHEMA_RELOAD
end
Expand All @@ -98,7 +100,6 @@ local function call_get_border_on_router(border_name, space_name, index_name, op
local cmp_key_parts = utils.merge_primary_key_parts(index.parts, primary_index.parts)
local field_names = utils.enrich_field_names_with_cmp_key(opts.fields, cmp_key_parts, space:format())

local replicasets = vshard.router.routeall()
local call_opts = {
mode = 'read',
replicasets = replicasets,
Expand Down
9 changes: 6 additions & 3 deletions crud/common/call.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ local function wrap_vshard_err(err, func_name, replicaset_uuid, bucket_id)
end

if replicaset_uuid == nil then
local replicaset, _ = vshard.router.route(bucket_id)
local vshard_router = vshard.router.static
local replicaset, _ = vshard_router:route(bucket_id)
if replicaset == nil then
return CallError:new(
"Function returned an error, but we couldn't figure out the replicaset: %s", err
Expand Down Expand Up @@ -150,7 +151,8 @@ function call.single(bucket_id, func_name, func_args, opts)

local timeout = opts.timeout or const.DEFAULT_VSHARD_CALL_TIMEOUT

local res, err = vshard.router[vshard_call_name](bucket_id, func_name, func_args, {
local vshard_router = vshard.router.static
local res, err = vshard_router[vshard_call_name](vshard_router, bucket_id, func_name, func_args, {
timeout = timeout,
})

Expand All @@ -172,7 +174,8 @@ function call.any(func_name, func_args, opts)

local timeout = opts.timeout or const.DEFAULT_VSHARD_CALL_TIMEOUT

local replicasets, err = vshard.router.routeall()
local vshard_router = vshard.router.static
local replicasets, err = vshard_router:routeall()
if replicasets == nil then
return nil, CallError:new("Failed to get all replicasets: %s", err.err)
end
Expand Down
5 changes: 3 additions & 2 deletions crud/common/map_call_cases/base_iter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ function BaseIterator:new(opts)
if opts.replicasets ~= nil then
replicasets = opts.replicasets
else
replicasets, err = vshard.router.routeall()
if replicasets == nil then
local vshard_router = vshard.router.static
replicasets, err = vshard_router:routeall()
if err ~= nil then
return nil, GetReplicasetsError:new("Failed to get all replicasets: %s", err.err)
end
end
Expand Down
3 changes: 2 additions & 1 deletion crud/common/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ function schema.wrap_func_reload(func, ...)
break
end

local ok, reload_schema_err = reload_schema(vshard.router.routeall())
local vshard_router = vshard.router.static
local ok, reload_schema_err = reload_schema(vshard_router:routeall())
if not ok then
log.warn("Failed to reload schema: %s", reload_schema_err)
break
Expand Down
8 changes: 5 additions & 3 deletions crud/common/sharding/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ local sharding_utils = require('crud.common.sharding.utils')
local sharding = {}

function sharding.get_replicasets_by_bucket_id(bucket_id)
local replicaset, err = vshard.router.route(bucket_id)
local vshard_router = vshard.router.static
local replicaset, err = vshard_router:route(bucket_id)
if replicaset == nil then
return nil, GetReplicasetsError:new("Failed to get replicaset for bucket_id %s: %s", bucket_id, err.err)
end
Expand Down Expand Up @@ -43,7 +44,7 @@ function sharding.key_get_bucket_id(space_name, key, specified_bucket_id)
}
end

return { bucket_id = vshard.router.bucket_id_strcrc32(key) }
return { bucket_id = vshard_router:bucket_id_strcrc32(key) }
end

function sharding.tuple_get_bucket_id(tuple, space, specified_bucket_id)
Expand Down Expand Up @@ -241,7 +242,8 @@ function sharding.split_tuples_by_replicaset(tuples, space, opts)
skip_sharding_hash_check = false
end

local replicaset, err = vshard.router.route(sharding_data.bucket_id)
local vshard_router = vshard.router.static
local replicaset, err = vshard_router:route(sharding_data.bucket_id)
if replicaset == nil then
return nil, GetReplicasetsError:new(
"Failed to get replicaset for bucket_id %s: %s",
Expand Down
6 changes: 4 additions & 2 deletions crud/common/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,8 @@ function utils.cut_rows(rows, metadata, field_names)
end

local function flatten_obj(space_name, obj)
local space_format, err = utils.get_space_format(space_name, vshard.router.routeall())
local vshard_router = vshard.router.static
local space_format, err = utils.get_space_format(space_name, vshard_router:routeall())
if err ~= nil then
return nil, FlattenError:new("Failed to get space format: %s", err), const.NEED_SCHEMA_RELOAD
end
Expand Down Expand Up @@ -760,7 +761,8 @@ end
--
-- @return a table of storage states by replica uuid.
function utils.storage_info(opts)
local replicasets, err = vshard.router.routeall()
local vshard_router = vshard.router.static
local replicasets, err = vshard_router:routeall()
if replicasets == nil then
return nil, StorageInfoError:new("Failed to get all replicasets: %s", err.err)
end
Expand Down
3 changes: 2 additions & 1 deletion crud/count.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ local function call_count_on_router(space_name, user_conditions, opts)
return nil, CountError:new("Failed to parse conditions: %s", err)
end

local replicasets, err = vshard.router.routeall()
local vshard_router = vshard.router.static
local replicasets, err = vshard_router:routeall()
if err ~= nil then
return nil, CountError:new("Failed to get all replicasets: %s", err)
end
Expand Down
3 changes: 2 additions & 1 deletion crud/delete.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ local function call_delete_on_router(space_name, key, opts)

opts = opts or {}

local space = utils.get_space(space_name, vshard.router.routeall())
local vshard_router = vshard.router.static
local space = utils.get_space(space_name, vshard_router:routeall())
if space == nil then
return nil, DeleteError:new("Space %q doesn't exist", space_name), const.NEED_SCHEMA_RELOAD
end
Expand Down
3 changes: 2 additions & 1 deletion crud/get.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ local function call_get_on_router(space_name, key, opts)

opts = opts or {}

local space = utils.get_space(space_name, vshard.router.routeall())
local vshard_router = vshard.router.static
local space = utils.get_space(space_name, vshard_router:routeall())
if space == nil then
return nil, GetError:new("Space %q doesn't exist", space_name), const.NEED_SCHEMA_RELOAD
end
Expand Down
3 changes: 2 additions & 1 deletion crud/insert.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ local function call_insert_on_router(space_name, original_tuple, opts)

opts = opts or {}

local space = utils.get_space(space_name, vshard.router.routeall())
local vshard_router = vshard.router.static
local space = utils.get_space(space_name, vshard_router:routeall())
if space == nil then
return nil, InsertError:new("Space %q doesn't exist", space_name), const.NEED_SCHEMA_RELOAD
end
Expand Down
3 changes: 2 additions & 1 deletion crud/insert_many.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ local function call_insert_many_on_router(space_name, original_tuples, opts)

opts = opts or {}

local space = utils.get_space(space_name, vshard.router.routeall())
local vshard_router = vshard.router.static
local space = utils.get_space(space_name, vshard_router:routeall())
if space == nil then
return nil, {InsertManyError:new("Space %q doesn't exist", space_name)}, const.NEED_SCHEMA_RELOAD
end
Expand Down
5 changes: 3 additions & 2 deletions crud/len.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ function len.call(space_name, opts)
'Please, use space name instead.')
end

local space = utils.get_space(space_name, vshard.router.routeall())
local vshard_router = vshard.router.static
local space = utils.get_space(space_name, vshard_router:routeall())
if space == nil then
return nil, LenError:new("Space %q doesn't exist", space_name)
end

local results, err = vshard.router.map_callrw(LEN_FUNC_NAME, {space_name}, opts)
local results, err = vshard_router:map_callrw(LEN_FUNC_NAME, {space_name}, opts)

if err ~= nil then
return nil, LenError:new("Failed to call len on storage-side: %s", err)
Expand Down
3 changes: 2 additions & 1 deletion crud/replace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ local function call_replace_on_router(space_name, original_tuple, opts)

opts = opts or {}

local space, err = utils.get_space(space_name, vshard.router.routeall())
local vshard_router = vshard.router.static
local space, err = utils.get_space(space_name, vshard_router:routeall())
if err ~= nil then
return nil, ReplaceError:new("Failed to get space %q: %s", space_name, err), const.NEED_SCHEMA_RELOAD
end
Expand Down
3 changes: 2 additions & 1 deletion crud/replace_many.lua
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ local function call_replace_many_on_router(space_name, original_tuples, opts)

opts = opts or {}

local space = utils.get_space(space_name, vshard.router.routeall())
local vshard_router = vshard.router.static
local space = utils.get_space(space_name, vshard_router:routeall())
if space == nil then
return nil, {ReplaceManyError:new("Space %q doesn't exist", space_name)}, const.NEED_SCHEMA_RELOAD
end
Expand Down
3 changes: 2 additions & 1 deletion crud/select/compat/select.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ local function build_select_iterator(space_name, user_conditions, opts)
return nil, SelectError:new("Failed to parse conditions: %s", err)
end

local replicasets, err = vshard.router.routeall()
local vshard_router = vshard.router.static
local replicasets, err = vshard_router:routeall()
if err ~= nil then
return nil, SelectError:new("Failed to get all replicasets: %s", err)
end
Expand Down
3 changes: 2 additions & 1 deletion crud/select/compat/select_old.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ local function build_select_iterator(space_name, user_conditions, opts)
return nil, SelectError:new("Failed to parse conditions: %s", err)
end

local replicasets, err = vshard.router.routeall()
local vshard_router = vshard.router.static
local replicasets, err = vshard_router:routeall()
if err ~= nil then
return nil, SelectError:new("Failed to get all replicasets: %s", err)
end
Expand Down
3 changes: 2 additions & 1 deletion crud/stats/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ function stats.get(space_name)
end

local function resolve_space_name(space_id)
local replicasets = vshard.router.routeall()
local vshard_router = vshard.router.static
local replicasets = vshard_router:routeall()
if next(replicasets) == nil then
log.warn('Failed to resolve space name for stats: no replicasets found')
return nil
Expand Down
3 changes: 2 additions & 1 deletion crud/truncate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ function truncate.call(space_name, opts)

opts = opts or {}

local replicasets = vshard.router.routeall()
local vshard_router = vshard.router.static
local replicasets = vshard_router:routeall()
local _, err = call.map(TRUNCATE_FUNC_NAME, {space_name}, {
mode = 'write',
replicasets = replicasets,
Expand Down
3 changes: 2 additions & 1 deletion crud/update.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ local function call_update_on_router(space_name, key, user_operations, opts)

opts = opts or {}

local space, err = utils.get_space(space_name, vshard.router.routeall())
local vshard_router = vshard.router.static
local space, err = utils.get_space(space_name, vshard_router:routeall())
if err ~= nil then
return nil, UpdateError:new("Failed to get space %q: %s", space_name, err), const.NEED_SCHEMA_RELOAD
end
Expand Down
3 changes: 2 additions & 1 deletion crud/upsert.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ local function call_upsert_on_router(space_name, original_tuple, user_operations

opts = opts or {}

local space, err = utils.get_space(space_name, vshard.router.routeall())
local vshard_router = vshard.router.static
local space, err = utils.get_space(space_name, vshard_router:routeall())
if err ~= nil then
return nil, UpsertError:new("Failed to get space %q: %s", space_name, err), const.NEED_SCHEMA_RELOAD
end
Expand Down
3 changes: 2 additions & 1 deletion crud/upsert_many.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ local function call_upsert_many_on_router(space_name, original_tuples_operation_

opts = opts or {}

local space = utils.get_space(space_name, vshard.router.routeall())
local vshard_router = vshard.router.static
local space = utils.get_space(space_name, vshard_router:routeall())
if space == nil then
return nil, {UpsertManyError:new("Space %q doesn't exist", space_name)}, const.NEED_SCHEMA_RELOAD
end
Expand Down

0 comments on commit 2519df9

Please sign in to comment.