From 2519df992aba1eac5afede998299e12ede9a5b6b Mon Sep 17 00:00:00 2001 From: Georgy Moiseev Date: Thu, 18 Aug 2022 14:37:58 +0300 Subject: [PATCH] internal: use vshard router object 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 --- crud/borders.lua | 5 +++-- crud/common/call.lua | 9 ++++++--- crud/common/map_call_cases/base_iter.lua | 5 +++-- crud/common/schema.lua | 3 ++- crud/common/sharding/init.lua | 8 +++++--- crud/common/utils.lua | 6 ++++-- crud/count.lua | 3 ++- crud/delete.lua | 3 ++- crud/get.lua | 3 ++- crud/insert.lua | 3 ++- crud/insert_many.lua | 3 ++- crud/len.lua | 5 +++-- crud/replace.lua | 3 ++- crud/replace_many.lua | 3 ++- crud/select/compat/select.lua | 3 ++- crud/select/compat/select_old.lua | 3 ++- crud/stats/init.lua | 3 ++- crud/truncate.lua | 3 ++- crud/update.lua | 3 ++- crud/upsert.lua | 3 ++- crud/upsert_many.lua | 3 ++- 21 files changed, 54 insertions(+), 29 deletions(-) diff --git a/crud/borders.lua b/crud/borders.lua index 1c4a81c2..cc8fed1d 100644 --- a/crud/borders.lua +++ b/crud/borders.lua @@ -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 @@ -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, diff --git a/crud/common/call.lua b/crud/common/call.lua index a89c414b..bffb4391 100644 --- a/crud/common/call.lua +++ b/crud/common/call.lua @@ -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 @@ -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, }) @@ -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 diff --git a/crud/common/map_call_cases/base_iter.lua b/crud/common/map_call_cases/base_iter.lua index bb732a75..ad61c7f7 100644 --- a/crud/common/map_call_cases/base_iter.lua +++ b/crud/common/map_call_cases/base_iter.lua @@ -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 diff --git a/crud/common/schema.lua b/crud/common/schema.lua index 0979a544..94ba2ec3 100644 --- a/crud/common/schema.lua +++ b/crud/common/schema.lua @@ -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 diff --git a/crud/common/sharding/init.lua b/crud/common/sharding/init.lua index 95bfecab..aaf79015 100644 --- a/crud/common/sharding/init.lua +++ b/crud/common/sharding/init.lua @@ -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 @@ -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) @@ -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", diff --git a/crud/common/utils.lua b/crud/common/utils.lua index 9153c3df..ff07608b 100644 --- a/crud/common/utils.lua +++ b/crud/common/utils.lua @@ -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 @@ -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 diff --git a/crud/count.lua b/crud/count.lua index 6e35ac6e..332fcbaa 100644 --- a/crud/count.lua +++ b/crud/count.lua @@ -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 diff --git a/crud/delete.lua b/crud/delete.lua index 14b79c99..7cee6b09 100644 --- a/crud/delete.lua +++ b/crud/delete.lua @@ -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 diff --git a/crud/get.lua b/crud/get.lua index dbf1304b..0d84e134 100644 --- a/crud/get.lua +++ b/crud/get.lua @@ -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 diff --git a/crud/insert.lua b/crud/insert.lua index bb09f735..a996a7c7 100644 --- a/crud/insert.lua +++ b/crud/insert.lua @@ -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 diff --git a/crud/insert_many.lua b/crud/insert_many.lua index 6d076a6a..cbb4e189 100644 --- a/crud/insert_many.lua +++ b/crud/insert_many.lua @@ -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 diff --git a/crud/len.lua b/crud/len.lua index 3f6f7aaa..bb3ef956 100644 --- a/crud/len.lua +++ b/crud/len.lua @@ -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) diff --git a/crud/replace.lua b/crud/replace.lua index 3e993ded..85ac90fb 100644 --- a/crud/replace.lua +++ b/crud/replace.lua @@ -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 diff --git a/crud/replace_many.lua b/crud/replace_many.lua index 75829a3c..e318559f 100644 --- a/crud/replace_many.lua +++ b/crud/replace_many.lua @@ -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 diff --git a/crud/select/compat/select.lua b/crud/select/compat/select.lua index 4f9964ed..e904793b 100644 --- a/crud/select/compat/select.lua +++ b/crud/select/compat/select.lua @@ -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 diff --git a/crud/select/compat/select_old.lua b/crud/select/compat/select_old.lua index 4d054b3e..928cac7e 100644 --- a/crud/select/compat/select_old.lua +++ b/crud/select/compat/select_old.lua @@ -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 diff --git a/crud/stats/init.lua b/crud/stats/init.lua index fefb32be..49152939 100644 --- a/crud/stats/init.lua +++ b/crud/stats/init.lua @@ -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 diff --git a/crud/truncate.lua b/crud/truncate.lua index 778b2ab5..58fb3417 100644 --- a/crud/truncate.lua +++ b/crud/truncate.lua @@ -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, diff --git a/crud/update.lua b/crud/update.lua index cea9fec9..4b1a18be 100644 --- a/crud/update.lua +++ b/crud/update.lua @@ -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 diff --git a/crud/upsert.lua b/crud/upsert.lua index a5c92eab..e602c84e 100644 --- a/crud/upsert.lua +++ b/crud/upsert.lua @@ -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 diff --git a/crud/upsert_many.lua b/crud/upsert_many.lua index 96e38cd9..47aa7a11 100644 --- a/crud/upsert_many.lua +++ b/crud/upsert_many.lua @@ -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