11local fiber = require (' fiber' )
22local errors = require (' errors' )
33local log = require (' log' )
4+ local vshard = require (' vshard' )
45
56local call = require (' crud.common.call' )
67local const = require (' crud.common.const' )
78local dev_checks = require (' crud.common.dev_checks' )
8- local cache = require (' crud.common.sharding.router_metadata_cache' )
9+ local router_cache = require (' crud.common.sharding.router_metadata_cache' )
910local storage_cache = require (' crud.common.sharding.storage_metadata_cache' )
1011local sharding_func = require (' crud.common.sharding.sharding_func' )
1112local sharding_key = require (' crud.common.sharding.sharding_key' )
@@ -22,8 +23,11 @@ local sharding_metadata_module = {}
2223local function locked (f )
2324 dev_checks (' function' )
2425
25- return function (timeout , ...)
26+ return function (timeout , vshard_router , ...)
2627 local timeout_deadline = fiber .clock () + timeout
28+
29+ local cache = router_cache .get_instance (vshard_router )
30+
2731 local ok = cache .fetch_lock :put (true , timeout )
2832 -- channel:put() returns false in two cases: when timeout is exceeded
2933 -- or channel has been closed. However error message describes only
@@ -34,7 +38,7 @@ local function locked(f)
3438 " Timeout for fetching sharding metadata is exceeded" )
3539 end
3640 local timeout = timeout_deadline - fiber .clock ()
37- local status , err = pcall (f , timeout , ... )
41+ local status , err = pcall (f , timeout , vshard_router , ... )
3842 cache .fetch_lock :get ()
3943 if not status or err ~= nil then
4044 return err
9599-- cache.fetch_lock become unlocked during timeout passed to
96100-- _fetch_on_router().
97101-- metadata_map_name == nil means forced reload.
98- local _fetch_on_router = locked (function (timeout , space_name , metadata_map_name )
99- dev_checks (' number' , ' string' , ' ?string' )
102+ local _fetch_on_router = locked (function (timeout , vshard_router , space_name , metadata_map_name )
103+ dev_checks (' number' , ' table' , ' string' , ' ?string' )
104+
105+ local cache = router_cache .get_instance (vshard_router )
100106
101107 if (metadata_map_name ~= nil ) and (cache [metadata_map_name ]) ~= nil then
102108 return
@@ -109,11 +115,11 @@ local _fetch_on_router = locked(function(timeout, space_name, metadata_map_name)
109115 return err
110116 end
111117 if metadata_map == nil then
112- cache [cache .SHARDING_KEY_MAP_NAME ] = {}
113- cache [cache .SHARDING_FUNC_MAP_NAME ] = {}
114- cache [cache .META_HASH_MAP_NAME ] = {
115- [cache .SHARDING_KEY_MAP_NAME ] = {},
116- [cache .SHARDING_FUNC_MAP_NAME ] = {},
118+ cache [router_cache .SHARDING_KEY_MAP_NAME ] = {}
119+ cache [router_cache .SHARDING_FUNC_MAP_NAME ] = {}
120+ cache [router_cache .META_HASH_MAP_NAME ] = {
121+ [router_cache .SHARDING_KEY_MAP_NAME ] = {},
122+ [router_cache .SHARDING_FUNC_MAP_NAME ] = {},
117123 }
118124 return
119125 end
@@ -129,24 +135,26 @@ local _fetch_on_router = locked(function(timeout, space_name, metadata_map_name)
129135 end
130136end )
131137
132- local function fetch_on_router (space_name , metadata_map_name , timeout )
138+ local function fetch_on_router (vshard_router , space_name , metadata_map_name , timeout )
139+ local cache = router_cache .get_instance (vshard_router )
140+
133141 if cache [metadata_map_name ] ~= nil then
134142 return {
135143 value = cache [metadata_map_name ][space_name ],
136- hash = cache [cache .META_HASH_MAP_NAME ][metadata_map_name ][space_name ]
144+ hash = cache [router_cache .META_HASH_MAP_NAME ][metadata_map_name ][space_name ]
137145 }
138146 end
139147
140148 local timeout = timeout or const .FETCH_SHARDING_METADATA_TIMEOUT
141- local err = _fetch_on_router (timeout , space_name , metadata_map_name )
149+ local err = _fetch_on_router (timeout , vshard_router , space_name , metadata_map_name )
142150 if err ~= nil then
143151 return nil , err
144152 end
145153
146154 if cache [metadata_map_name ] ~= nil then
147155 return {
148156 value = cache [metadata_map_name ][space_name ],
149- hash = cache [cache .META_HASH_MAP_NAME ][metadata_map_name ][space_name ],
157+ hash = cache [router_cache .META_HASH_MAP_NAME ][metadata_map_name ][space_name ],
150158 }
151159 end
152160
@@ -163,10 +171,10 @@ end
163171-- that nil without error is a successfull return value.
164172-- - nil and error, when something goes wrong on fetching attempt.
165173--
166- function sharding_metadata_module .fetch_sharding_key_on_router (space_name , timeout )
167- dev_checks (' string' , ' ?number' )
174+ function sharding_metadata_module .fetch_sharding_key_on_router (vshard_router , space_name , timeout )
175+ dev_checks (' table ' , ' string' , ' ?number' )
168176
169- return fetch_on_router (space_name , cache .SHARDING_KEY_MAP_NAME , timeout )
177+ return fetch_on_router (vshard_router , space_name , router_cache .SHARDING_KEY_MAP_NAME , timeout )
170178end
171179
172180-- Get sharding func for a certain space.
@@ -178,28 +186,31 @@ end
178186-- that nil without error is a successfull return value.
179187-- - nil and error, when something goes wrong on fetching attempt.
180188--
181- function sharding_metadata_module .fetch_sharding_func_on_router (space_name , timeout )
182- dev_checks (' string' , ' ?number' )
189+ function sharding_metadata_module .fetch_sharding_func_on_router (vshard_router , space_name , timeout )
190+ dev_checks (' table ' , ' string' , ' ?number' )
183191
184- return fetch_on_router (space_name , cache .SHARDING_FUNC_MAP_NAME , timeout )
192+ return fetch_on_router (vshard_router , space_name , router_cache .SHARDING_FUNC_MAP_NAME , timeout )
185193end
186194
187195function sharding_metadata_module .update_sharding_key_cache (space_name )
188- cache .drop_caches ()
196+ local vshard_router = vshard .router .static
197+ router_cache .drop_instance (vshard_router )
189198
190- return sharding_metadata_module .fetch_sharding_key_on_router (space_name )
199+ return sharding_metadata_module .fetch_sharding_key_on_router (vshard_router , space_name )
191200end
192201
193202function sharding_metadata_module .update_sharding_func_cache (space_name )
194- cache .drop_caches ()
203+ local vshard_router = vshard .router .static
204+ router_cache .drop_instance (vshard_router )
195205
196- return sharding_metadata_module .fetch_sharding_func_on_router (space_name )
206+ return sharding_metadata_module .fetch_sharding_func_on_router (vshard_router , space_name )
197207end
198208
199209function sharding_metadata_module .reload_sharding_cache (space_name )
200- cache .drop_caches ()
210+ local vshard_router = vshard .router .static
211+ router_cache .drop_instance (vshard_router )
201212
202- local err = _fetch_on_router (const .FETCH_SHARDING_METADATA_TIMEOUT , space_name , nil )
213+ local err = _fetch_on_router (const .FETCH_SHARDING_METADATA_TIMEOUT , vshard_router , space_name , nil )
203214 if err ~= nil then
204215 log .warn (' Failed to reload sharding cache: %s' , err )
205216 end
0 commit comments