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