|
| 1 | +local fiber = require('fiber') |
| 2 | +local errors = require('errors') |
| 3 | + |
| 4 | +local call = require('crud.common.call') |
| 5 | +local const = require('crud.common.const') |
| 6 | +local dev_checks = require('crud.common.dev_checks') |
| 7 | +local cache = require('crud.common.sharding_key_cache') |
| 8 | +local utils = require('crud.common.utils') |
| 9 | + |
| 10 | +local FetchShardingKeyError = errors.new_class('FetchShardingKeyError', {capture_stack = false}) |
| 11 | +local WrongShardingConfigurationError = errors.new_class('WrongShardingConfigurationError', {capture_stack = false}) |
| 12 | + |
| 13 | +local FETCH_FUNC_NAME = '_crud.fetch_on_storage' |
| 14 | + |
| 15 | +local sharding_key_module = {} |
| 16 | + |
| 17 | +-- Function decorator that is used to prevent _fetch_on_router() from being |
| 18 | +-- called concurrently by different fibers. |
| 19 | +local function locked(f) |
| 20 | + dev_checks('function') |
| 21 | + |
| 22 | + return function(timeout, ...) |
| 23 | + local timeout_deadline = fiber.clock() + timeout |
| 24 | + local ok = cache.fetch_lock:put(true, timeout) |
| 25 | + -- channel:put() returns false in two cases: when timeout is exceeded |
| 26 | + -- or channel has been closed. However error message describes only |
| 27 | + -- first reason, I'm not sure we need to disclose to users such details |
| 28 | + -- like problems with synchronization objects. |
| 29 | + if not ok then |
| 30 | + return FetchShardingKeyError:new( |
| 31 | + "Timeout for fetching sharding key is exceeded") |
| 32 | + end |
| 33 | + local timeout = timeout_deadline - fiber.clock() |
| 34 | + local status, err = pcall(f, timeout, ...) |
| 35 | + cache.fetch_lock:get() |
| 36 | + if not status or err ~= nil then |
| 37 | + return err |
| 38 | + end |
| 39 | + end |
| 40 | +end |
| 41 | + |
| 42 | +-- Build a structure similar to index, but it is not a real index object, |
| 43 | +-- it contains only parts key with fieldno's. |
| 44 | +local function as_index_object(space_name, space_format, sharding_key_def) |
| 45 | + dev_checks('string', 'table', 'table') |
| 46 | + |
| 47 | + local fieldnos = {} |
| 48 | + local fieldno_map = utils.get_format_fieldno_map(space_format) |
| 49 | + for _, field_name in ipairs(sharding_key_def) do |
| 50 | + local fieldno = fieldno_map[field_name] |
| 51 | + if fieldno == nil then |
| 52 | + return nil, WrongShardingConfigurationError:new( |
| 53 | + "No such field (%s) in a space format (%s)", field_name, space_name) |
| 54 | + end |
| 55 | + table.insert(fieldnos, {fieldno = fieldno}) |
| 56 | + end |
| 57 | + |
| 58 | + return {parts = fieldnos} |
| 59 | +end |
| 60 | + |
| 61 | +-- Return a map with metadata or nil when space box.space._ddl_sharding_key is |
| 62 | +-- not available on storage. |
| 63 | +function sharding_key_module.fetch_on_storage() |
| 64 | + local sharding_key_space = box.space._ddl_sharding_key |
| 65 | + if sharding_key_space == nil then |
| 66 | + return nil |
| 67 | + end |
| 68 | + |
| 69 | + local SPACE_NAME_FIELDNO = 1 |
| 70 | + local SPACE_SHARDING_KEY_FIELDNO = 2 |
| 71 | + local metadata_map = {} |
| 72 | + for _, tuple in sharding_key_space:pairs() do |
| 73 | + local space_name = tuple[SPACE_NAME_FIELDNO] |
| 74 | + local sharding_key_def = tuple[SPACE_SHARDING_KEY_FIELDNO] |
| 75 | + local space_format = box.space[space_name]:format() |
| 76 | + metadata_map[space_name] = { |
| 77 | + sharding_key_def = sharding_key_def, |
| 78 | + space_format = space_format, |
| 79 | + } |
| 80 | + end |
| 81 | + |
| 82 | + return metadata_map |
| 83 | +end |
| 84 | + |
| 85 | +-- Under high load we may get a case when more than one fiber will fetch |
| 86 | +-- metadata from storages. It is not good from performance point of view. |
| 87 | +-- locked() wraps a _fetch_on_router() to limit a number of fibers that fetches |
| 88 | +-- a sharding metadata by a single one, other fibers will wait while |
| 89 | +-- cache.fetch_lock become unlocked during timeout passed to |
| 90 | +-- _fetch_on_router(). |
| 91 | +local _fetch_on_router = locked(function(timeout) |
| 92 | + dev_checks('number') |
| 93 | + |
| 94 | + if cache.sharding_key_as_index_obj_map ~= nil then |
| 95 | + return |
| 96 | + end |
| 97 | + |
| 98 | + local metadata_map, err = call.any(FETCH_FUNC_NAME, {}, { |
| 99 | + timeout = timeout |
| 100 | + }) |
| 101 | + if err ~= nil then |
| 102 | + return err |
| 103 | + end |
| 104 | + if metadata_map == nil then |
| 105 | + cache.sharding_key_as_index_obj_map = {} |
| 106 | + return |
| 107 | + end |
| 108 | + |
| 109 | + cache.sharding_key_as_index_obj_map = {} |
| 110 | + for space_name, metadata in pairs(metadata_map) do |
| 111 | + local sharding_key_as_index_obj, err = as_index_object(space_name, |
| 112 | + metadata.space_format, |
| 113 | + metadata.sharding_key_def) |
| 114 | + if err ~= nil then |
| 115 | + return err |
| 116 | + end |
| 117 | + cache.sharding_key_as_index_obj_map[space_name] = sharding_key_as_index_obj |
| 118 | + end |
| 119 | +end) |
| 120 | + |
| 121 | +-- Get sharding index for a certain space. |
| 122 | +-- |
| 123 | +-- Return: |
| 124 | +-- - sharding key as index object, when sharding key definition found on |
| 125 | +-- storage. |
| 126 | +-- - nil, when sharding key definition was not found on storage. Pay attention |
| 127 | +-- that nil without error is a successfull return value. |
| 128 | +-- - nil and error, when something goes wrong on fetching attempt. |
| 129 | +-- |
| 130 | +function sharding_key_module.fetch_on_router(space_name, timeout) |
| 131 | + dev_checks('string', '?number') |
| 132 | + |
| 133 | + if cache.sharding_key_as_index_obj_map ~= nil then |
| 134 | + return cache.sharding_key_as_index_obj_map[space_name] |
| 135 | + end |
| 136 | + |
| 137 | + local timeout = timeout or const.FETCH_SHARDING_KEY_TIMEOUT |
| 138 | + local err = _fetch_on_router(timeout) |
| 139 | + if err ~= nil then |
| 140 | + if cache.sharding_key_as_index_obj_map ~= nil then |
| 141 | + return cache.sharding_key_as_index_obj_map[space_name] |
| 142 | + end |
| 143 | + return nil, err |
| 144 | + end |
| 145 | + |
| 146 | + if cache.sharding_key_as_index_obj_map ~= nil then |
| 147 | + return cache.sharding_key_as_index_obj_map[space_name] |
| 148 | + end |
| 149 | + |
| 150 | + return nil, FetchShardingKeyError:new( |
| 151 | + "Fetching sharding key for space '%s' is failed", space_name) |
| 152 | +end |
| 153 | + |
| 154 | +function sharding_key_module.init() |
| 155 | + _G._crud.fetch_on_storage = sharding_key_module.fetch_on_storage |
| 156 | +end |
| 157 | + |
| 158 | +sharding_key_module.internal = { |
| 159 | + as_index_object = as_index_object, |
| 160 | +} |
| 161 | + |
| 162 | +return sharding_key_module |
0 commit comments