1- local vshard = require (' vshard' )
21local errors = require (' errors' )
32
43local BucketIDError = errors .new_class (" BucketIDError" , {capture_stack = false })
@@ -13,8 +12,7 @@ local sharding_utils = require('crud.common.sharding.utils')
1312
1413local sharding = {}
1514
16- function sharding .get_replicasets_by_bucket_id (bucket_id )
17- local vshard_router = vshard .router .static
15+ function sharding .get_replicasets_by_bucket_id (vshard_router , bucket_id )
1816 local replicaset , err = vshard_router :route (bucket_id )
1917 if replicaset == nil then
2018 return nil , GetReplicasetsError :new (" Failed to get replicaset for bucket_id %s: %s" , bucket_id , err .err )
@@ -25,14 +23,13 @@ function sharding.get_replicasets_by_bucket_id(bucket_id)
2523 }
2624end
2725
28- function sharding .key_get_bucket_id (space_name , key , specified_bucket_id )
29- dev_checks (' string' , ' ?' , ' ?number|cdata' )
26+ function sharding .key_get_bucket_id (vshard_router , space_name , key , specified_bucket_id )
27+ dev_checks (' table ' , ' string' , ' ?' , ' ?number|cdata' )
3028
3129 if specified_bucket_id ~= nil then
3230 return { bucket_id = specified_bucket_id }
3331 end
3432
35- local vshard_router = vshard .router .static
3633 local sharding_func_data , err = sharding_metadata_module .fetch_sharding_func_on_router (vshard_router , space_name )
3734 if err ~= nil then
3835 return nil , err
@@ -48,13 +45,12 @@ function sharding.key_get_bucket_id(space_name, key, specified_bucket_id)
4845 return { bucket_id = vshard_router :bucket_id_strcrc32 (key ) }
4946end
5047
51- function sharding .tuple_get_bucket_id (tuple , space , specified_bucket_id )
48+ function sharding .tuple_get_bucket_id (vshard_router , tuple , space , specified_bucket_id )
5249 if specified_bucket_id ~= nil then
5350 return { bucket_id = specified_bucket_id }
5451 end
5552
5653 local sharding_index_parts = space .index [0 ].parts
57- local vshard_router = vshard .router .static
5854 local sharding_key_data , err = sharding_metadata_module .fetch_sharding_key_on_router (vshard_router , space .name )
5955 if err ~= nil then
6056 return nil , err
@@ -64,7 +60,7 @@ function sharding.tuple_get_bucket_id(tuple, space, specified_bucket_id)
6460 end
6561 local key = utils .extract_key (tuple , sharding_index_parts )
6662
67- local bucket_id_data , err = sharding .key_get_bucket_id (space .name , key , nil )
63+ local bucket_id_data , err = sharding .key_get_bucket_id (vshard_router , space .name , key , nil )
6864 if err ~= nil then
6965 return nil , err
7066 end
@@ -76,7 +72,7 @@ function sharding.tuple_get_bucket_id(tuple, space, specified_bucket_id)
7672 }
7773end
7874
79- function sharding .tuple_set_and_return_bucket_id (tuple , space , specified_bucket_id )
75+ function sharding .tuple_set_and_return_bucket_id (vshard_router , tuple , space , specified_bucket_id )
8076 local bucket_id_fieldno , err = utils .get_bucket_id_fieldno (space )
8177 if err ~= nil then
8278 return nil , BucketIDError :new (" Failed to get bucket ID fieldno: %s" , err )
@@ -98,7 +94,7 @@ function sharding.tuple_set_and_return_bucket_id(tuple, space, specified_bucket_
9894 local sharding_data = { bucket_id = tuple [bucket_id_fieldno ] }
9995
10096 if sharding_data .bucket_id == nil then
101- sharding_data , err = sharding .tuple_get_bucket_id (tuple , space )
97+ sharding_data , err = sharding .tuple_get_bucket_id (vshard_router , tuple , space )
10298 if err ~= nil then
10399 return nil , err
104100 end
@@ -144,18 +140,18 @@ function sharding.batching_result_needs_sharding_reload(errs, tuples_count)
144140 return sharding_errs_count == tuples_count
145141end
146142
147- function sharding .wrap_method (method , space_name , ...)
143+ function sharding .wrap_method (vshard_router , method , space_name , ...)
148144 local i = 0
149145
150146 local res , err , need_reload
151147 while true do
152- res , err , need_reload = method (space_name , ... )
148+ res , err , need_reload = method (vshard_router , space_name , ... )
153149
154150 if err == nil or need_reload ~= const .NEED_SHARDING_RELOAD then
155151 break
156152 end
157153
158- sharding_metadata_module .reload_sharding_cache (space_name )
154+ sharding_metadata_module .reload_sharding_cache (vshard_router , space_name )
159155
160156 i = i + 1
161157
@@ -169,12 +165,12 @@ end
169165
170166-- This wrapper assumes reload is performed inside the method and
171167-- expect ShardingHashMismatchError error to be thrown.
172- function sharding .wrap_select_method (method , space_name , ...)
168+ function sharding .wrap_select_method (vshard_router , method , space_name , ...)
173169 local i = 0
174170
175171 local ok , res , err
176172 while true do
177- ok , res , err = pcall (method , space_name , ... )
173+ ok , res , err = pcall (method , vshard_router , space_name , ... )
178174
179175 if ok == true then
180176 break
212208-- @return [1] batches
213209-- Map where key is a replicaset and value
214210-- is table of tuples related to this replicaset
215- function sharding .split_tuples_by_replicaset (tuples , space , opts )
216- dev_checks (' table' , ' table' , {
211+ function sharding .split_tuples_by_replicaset (vshard_router , tuples , space , opts )
212+ dev_checks (' table' , ' table' , ' table ' , {
217213 operations = ' ?table' ,
218214 })
219215
@@ -227,7 +223,7 @@ function sharding.split_tuples_by_replicaset(tuples, space, opts)
227223 local sharding_data
228224 local err
229225 for i , tuple in ipairs (tuples ) do
230- sharding_data , err = sharding .tuple_set_and_return_bucket_id (tuple , space )
226+ sharding_data , err = sharding .tuple_set_and_return_bucket_id (vshard_router , tuple , space )
231227 if err ~= nil then
232228 return nil , BucketIDError :new (" Failed to get bucket ID: %s" , err )
233229 end
@@ -244,7 +240,6 @@ function sharding.split_tuples_by_replicaset(tuples, space, opts)
244240 skip_sharding_hash_check = false
245241 end
246242
247- local vshard_router = vshard .router .static
248243 local replicaset , err = vshard_router :route (sharding_data .bucket_id )
249244 if replicaset == nil then
250245 return nil , GetReplicasetsError :new (
0 commit comments