1
1
local fiber = require (' fiber' )
2
2
local errors = require (' errors' )
3
3
local log = require (' log' )
4
+ local vshard = require (' vshard' )
4
5
5
6
local call = require (' crud.common.call' )
6
7
local const = require (' crud.common.const' )
7
8
local 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' )
9
10
local storage_cache = require (' crud.common.sharding.storage_metadata_cache' )
10
11
local sharding_func = require (' crud.common.sharding.sharding_func' )
11
12
local sharding_key = require (' crud.common.sharding.sharding_key' )
@@ -22,8 +23,11 @@ local sharding_metadata_module = {}
22
23
local function locked (f )
23
24
dev_checks (' function' )
24
25
25
- return function (timeout , ...)
26
+ return function (timeout , vshard_router , ...)
26
27
local timeout_deadline = fiber .clock () + timeout
28
+
29
+ local cache = router_cache .get_instance (vshard_router )
30
+
27
31
local ok = cache .fetch_lock :put (true , timeout )
28
32
-- channel:put() returns false in two cases: when timeout is exceeded
29
33
-- or channel has been closed. However error message describes only
@@ -34,7 +38,7 @@ local function locked(f)
34
38
" Timeout for fetching sharding metadata is exceeded" )
35
39
end
36
40
local timeout = timeout_deadline - fiber .clock ()
37
- local status , err = pcall (f , timeout , ... )
41
+ local status , err = pcall (f , timeout , vshard_router , ... )
38
42
cache .fetch_lock :get ()
39
43
if not status or err ~= nil then
40
44
return err
95
99
-- cache.fetch_lock become unlocked during timeout passed to
96
100
-- _fetch_on_router().
97
101
-- 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 )
100
106
101
107
if (metadata_map_name ~= nil ) and (cache [metadata_map_name ]) ~= nil then
102
108
return
@@ -109,11 +115,11 @@ local _fetch_on_router = locked(function(timeout, space_name, metadata_map_name)
109
115
return err
110
116
end
111
117
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 ] = {},
117
123
}
118
124
return
119
125
end
@@ -129,24 +135,26 @@ local _fetch_on_router = locked(function(timeout, space_name, metadata_map_name)
129
135
end
130
136
end )
131
137
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
+
133
141
if cache [metadata_map_name ] ~= nil then
134
142
return {
135
143
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 ]
137
145
}
138
146
end
139
147
140
148
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 )
142
150
if err ~= nil then
143
151
return nil , err
144
152
end
145
153
146
154
if cache [metadata_map_name ] ~= nil then
147
155
return {
148
156
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 ],
150
158
}
151
159
end
152
160
@@ -163,10 +171,10 @@ end
163
171
-- that nil without error is a successfull return value.
164
172
-- - nil and error, when something goes wrong on fetching attempt.
165
173
--
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' )
168
176
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 )
170
178
end
171
179
172
180
-- Get sharding func for a certain space.
@@ -178,28 +186,31 @@ end
178
186
-- that nil without error is a successfull return value.
179
187
-- - nil and error, when something goes wrong on fetching attempt.
180
188
--
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' )
183
191
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 )
185
193
end
186
194
187
195
function 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 .name )
189
198
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 )
191
200
end
192
201
193
202
function 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 .name )
195
205
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 )
197
207
end
198
208
199
209
function 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 .name )
201
212
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 )
203
214
if err ~= nil then
204
215
log .warn (' Failed to reload sharding cache: %s' , err )
205
216
end
0 commit comments