Skip to content

Commit 00fcbfb

Browse files
committed
feature: master presence timout for get space
Added timeout condition for the validation of master presence in replicaset and for the master connection to the `utils.get_space` method. Closes #95
1 parent 73bf5bf commit 00fcbfb

19 files changed

+82
-54
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
88
## [Unreleased]
99

1010
### Added
11+
* Added timeout condition for the validation of master presence in
12+
replicaset and for the master connection (#95).
1113

1214
### Changed
1315

README.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ where:
204204
* `space_name` (`string`) - name of the space to insert an object
205205
* `tuple` / `object` (`table`) - tuple/object to insert
206206
* `opts`:
207-
* `timeout` (`?number`) - `vshard.call` timeout (in seconds)
207+
* `timeout` (`?number`) - `vshard.call` timeout and vshard master
208+
discovery timeout (in seconds)
208209
* `bucket_id` (`?number|cdata`) - bucket ID
209210
* `fields` (`?table`) - field names for getting only a subset of fields
210211
* `vshard_router` (`?string|table`) - Cartridge vshard group name or
@@ -263,7 +264,8 @@ where:
263264
* `space_name` (`string`) - name of the space to insert an object
264265
* `tuples` / `objects` (`table`) - array of tuples/objects to insert
265266
* `opts`:
266-
* `timeout` (`?number`) - `vshard.call` timeout (in seconds)
267+
* `timeout` (`?number`) - `vshard.call` timeout and vshard master
268+
discovery timeout (in seconds)
267269
* `fields` (`?table`) - field names for getting only a subset of fields
268270
* `stop_on_error` (`?boolean`) - stop on a first error and report error
269271
regarding the failed operation and error about what tuples were not
@@ -411,7 +413,8 @@ where:
411413
* `opts`:
412414
* `fields` (`?table`) - field names for getting only a subset of fields
413415
* `bucket_id` (`?number|cdata`) - bucket ID
414-
* `timeout` (`?number`) - `vshard.call` timeout (in seconds)
416+
* `timeout` (`?number`) - `vshard.call` timeout and vshard master
417+
discovery timeout (in seconds)
415418
* `mode` (`?string`, `read` or `write`) - if `write` is specified then `get` is
416419
performed on master, default value is `read`
417420
* `prefer_replica` (`?boolean`) - if `true` then the preferred target is one of
@@ -450,7 +453,8 @@ where:
450453
* `key` (`any`) - primary key value
451454
* `operations` (`table`) - update [operations](https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_space/update/)
452455
* `opts`:
453-
* `timeout` (`?number`) - `vshard.call` timeout (in seconds)
456+
* `timeout` (`?number`) - `vshard.call` timeout and vshard master
457+
discovery timeout (in seconds)
454458
* `bucket_id` (`?number|cdata`) - bucket ID
455459
* `fields` (`?table`) - field names for getting only a subset of fields
456460
* `vshard_router` (`?string|table`) - Cartridge vshard group name or
@@ -485,7 +489,8 @@ where:
485489
* `space_name` (`string`) - name of the space
486490
* `key` (`any`) - primary key value
487491
* `opts`:
488-
* `timeout` (`?number`) - `vshard.call` timeout (in seconds)
492+
* `timeout` (`?number`) - `vshard.call` timeout and vshard master
493+
discovery timeout (in seconds)
489494
* `bucket_id` (`?number|cdata`) - bucket ID
490495
* `fields` (`?table`) - field names for getting only a subset of fields
491496
* `vshard_router` (`?string|table`) - Cartridge vshard group name or
@@ -522,7 +527,8 @@ where:
522527
* `space_name` (`string`) - name of the space
523528
* `tuple` / `object` (`table`) - tuple/object to insert or replace exist one
524529
* `opts`:
525-
* `timeout` (`?number`) - `vshard.call` timeout (in seconds)
530+
* `timeout` (`?number`) - `vshard.call` timeout and vshard master
531+
discovery timeout (in seconds)
526532
* `bucket_id` (`?number|cdata`) - bucket ID
527533
* `fields` (`?table`) - field names for getting only a subset of fields
528534
* `vshard_router` (`?string|table`) - Cartridge vshard group name or
@@ -581,7 +587,8 @@ where:
581587
* `space_name` (`string`) - name of the space to insert/replace an object
582588
* `tuples` / `objects` (`table`) - array of tuples/objects to insert
583589
* `opts`:
584-
* `timeout` (`?number`) - `vshard.call` timeout (in seconds)
590+
* `timeout` (`?number`) - `vshard.call` timeout and vshard master
591+
discovery timeout (in seconds)
585592
* `fields` (`?table`) - field names for getting only a subset of fields
586593
* `stop_on_error` (`?boolean`) - stop on a first error and report error
587594
regarding the failed operation and error about what tuples were not
@@ -730,7 +737,8 @@ where:
730737
* `tuple` / `object` (`table`) - tuple/object to insert if there is no existing tuple which matches the key fields
731738
* `operations` (`table`) - update [operations](https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_space/update/) if there is an existing tuple which matches the key fields of tuple
732739
* `opts`:
733-
* `timeout` (`?number`) - `vshard.call` timeout (in seconds)
740+
* `timeout` (`?number`) - `vshard.call` timeout and vshard master
741+
discovery timeout (in seconds)
734742
* `bucket_id` (`?number|cdata`) - bucket ID
735743
* `fields` (`?table`) - field names for getting only a subset of fields
736744
* `vshard_router` (`?string|table`) - Cartridge vshard group name or
@@ -785,7 +793,8 @@ where:
785793
if there is tuple with duplicate key then existing tuple will
786794
be updated with update operations
787795
* `opts`:
788-
* `timeout` (`?number`) - `vshard.call` timeout (in seconds)
796+
* `timeout` (`?number`) - `vshard.call` timeout and vshard master
797+
discovery timeout (in seconds)
789798
* `fields` (`?table`) - field names for getting only a subset of fields
790799
* `stop_on_error` (`?boolean`) - stop on a first error and report error
791800
regarding the failed operation and error about what tuples were not
@@ -1108,7 +1117,8 @@ where:
11081117

11091118
* `space_name` (`string`) - name of the space
11101119
* `opts`:
1111-
* `timeout` (`?number`) - `vshard.call` timeout (in seconds)
1120+
* `timeout` (`?number`) - `vshard.call` timeout and vshard master
1121+
discovery timeout (in seconds)
11121122
* `vshard_router` (`?string|table`) - Cartridge vshard group name or
11131123
vshard router instance. Set this parameter if your space is not
11141124
a part of the default vshard cluster
@@ -1220,7 +1230,8 @@ where:
12201230
* `opts`:
12211231
* `yield_every` (`?number`) - number of tuples processed to yield after,
12221232
`yield_every` should be > 0, default value is 100
1223-
* `timeout` (`?number`) - `vshard.call` timeout (in seconds), default value is 2
1233+
* `timeout` (`?number`) - `vshard.call` timeout and vshard master
1234+
discovery timeout (in seconds), default value is 2
12241235
* `bucket_id` (`?number|cdata`) - bucket ID
12251236
* `force_map_call` (`?boolean`) - if `true`
12261237
then the map call is performed without any optimizations even,

crud/borders.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ local function call_get_border_on_router(vshard_router, border_name, space_name,
7373
vshard_router = '?string|table',
7474
})
7575

76-
local replicasets = vshard_router:routeall()
77-
local space, err = utils.get_space(space_name, replicasets)
76+
local space, err = utils.get_space(space_name, vshard_router, opts.timeout)
7877
if err ~= nil then
7978
return nil, BorderError:new("An error occurred during the operation: %s", err), const.NEED_SCHEMA_RELOAD
8079
end
@@ -100,6 +99,10 @@ local function call_get_border_on_router(vshard_router, border_name, space_name,
10099
local cmp_key_parts = utils.merge_primary_key_parts(index.parts, primary_index.parts)
101100
local field_names = utils.enrich_field_names_with_cmp_key(opts.fields, cmp_key_parts, space:format())
102101

102+
local replicasets, err = vshard_router:routeall()
103+
if err ~= nil then
104+
return nil, BorderError:new("Failed to get router replicasets: %s", err)
105+
end
103106
local call_opts = {
104107
mode = 'read',
105108
replicasets = replicasets,

crud/common/utils.lua

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ local FilterFieldsError = errors.new_class('FilterFieldsError', {capture_stack =
2121
local NotInitializedError = errors.new_class('NotInitialized')
2222
local StorageInfoError = errors.new_class('StorageInfoError')
2323
local VshardRouterError = errors.new_class('VshardRouterError', {capture_stack = false})
24-
local fiber_clock = require('fiber').clock
24+
local fiber = require('fiber')
2525

2626
local utils = {}
2727

@@ -96,8 +96,25 @@ function utils.format_replicaset_error(replicaset_uuid, msg, ...)
9696
)
9797
end
9898

99-
function utils.get_space(space_name, replicasets)
100-
local replicaset = select(2, next(replicasets))
99+
function utils.get_space(space_name, vshard_router, timeout)
100+
local replicasets, replicaset
101+
timeout = timeout or const.DEFAULT_VSHARD_CALL_TIMEOUT
102+
local deadline = fiber.clock() + timeout
103+
while (
104+
-- Break if the deadline condition is exceeded.
105+
-- Handling for deadline errors are below in the code.
106+
fiber.clock() < deadline
107+
) do
108+
-- Try to get master with timeout.
109+
fiber.yield()
110+
replicasets = vshard_router:routeall()
111+
replicaset = select(2, next(replicasets))
112+
if replicaset ~= nil and
113+
replicaset.master ~= nil and
114+
replicaset.master.conn.error == nil then
115+
break
116+
end
117+
end
101118

102119
if replicaset == nil then
103120
return nil, GetSpaceError:new(
@@ -119,13 +136,14 @@ function utils.get_space(space_name, replicasets)
119136
replicaset.uuid, replicaset.master.conn.error)
120137
return nil, GetSpaceError:new(error_msg)
121138
end
139+
122140
local space = replicaset.master.conn.space[space_name]
123141

124142
return space
125143
end
126144

127-
function utils.get_space_format(space_name, replicasets)
128-
local space, err = utils.get_space(space_name, replicasets)
145+
function utils.get_space_format(space_name, vshard_router)
146+
local space, err = utils.get_space(space_name, vshard_router)
129147
if err ~= nil then
130148
return nil, GetSpaceFormatError:new("An error occurred during the operation: %s", err)
131149
end
@@ -664,7 +682,7 @@ function utils.cut_rows(rows, metadata, field_names)
664682
end
665683

666684
local function flatten_obj(vshard_router, space_name, obj, skip_nullability_check)
667-
local space_format, err = utils.get_space_format(space_name, vshard_router:routeall())
685+
local space_format, err = utils.get_space_format(space_name, vshard_router)
668686
if err ~= nil then
669687
return nil, FlattenError:new("Failed to get space format: %s", err), const.NEED_SCHEMA_RELOAD
670688
end
@@ -835,9 +853,9 @@ function utils.storage_info(opts)
835853
end
836854
end
837855

838-
local deadline = fiber_clock() + timeout
856+
local deadline = fiber.clock() + timeout
839857
for replica_uuid, future in pairs(futures_by_replicas) do
840-
local wait_timeout = deadline - fiber_clock()
858+
local wait_timeout = deadline - fiber.clock()
841859
if wait_timeout < 0 then
842860
wait_timeout = 0
843861
end

crud/count.lua

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,7 @@ local function call_count_on_router(vshard_router, space_name, user_conditions,
132132
return nil, CountError:new("Failed to parse conditions: %s", err)
133133
end
134134

135-
local replicasets, err = vshard_router:routeall()
136-
if err ~= nil then
137-
return nil, CountError:new("Failed to get router replicasets: %s", err)
138-
end
139-
140-
local space, err = utils.get_space(space_name, replicasets)
135+
local space, err = utils.get_space(space_name, vshard_router, opts.timeout)
141136
if err ~= nil then
142137
return nil, CountError:new("An error occurred during the operation: %s", err), const.NEED_SCHEMA_RELOAD
143138
end
@@ -169,7 +164,10 @@ local function call_count_on_router(vshard_router, space_name, user_conditions,
169164
check_count_safety(space_name, plan, opts)
170165

171166
-- set replicasets to count from
172-
local replicasets_to_count = replicasets
167+
local replicasets_to_count, err = vshard_router:routeall()
168+
if err ~= nil then
169+
return nil, CountError:new("Failed to get router replicasets: %s", err)
170+
end
173171

174172
-- Whether to call one storage replicaset or perform
175173
-- map-reduce?

crud/delete.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ local function call_delete_on_router(vshard_router, space_name, key, opts)
6262
vshard_router = '?string|table',
6363
})
6464

65-
local space, err = utils.get_space(space_name, vshard_router:routeall())
65+
local space, err = utils.get_space(space_name, vshard_router, opts.timeout)
6666
if err ~= nil then
6767
return nil, DeleteError:new("An error occurred during the operation: %s", err), const.NEED_SCHEMA_RELOAD
6868
end

crud/get.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ local function call_get_on_router(vshard_router, space_name, key, opts)
6565
vshard_router = '?string|table',
6666
})
6767

68-
local space, err = utils.get_space(space_name, vshard_router:routeall())
68+
local space, err = utils.get_space(space_name, vshard_router, opts.timeout)
6969
if err ~= nil then
7070
return nil, GetError:new("An error occurred during the operation: %s", err), const.NEED_SCHEMA_RELOAD
7171
end

crud/insert.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ local function call_insert_on_router(vshard_router, space_name, original_tuple,
6464
vshard_router = '?string|table',
6565
})
6666

67-
local space, err = utils.get_space(space_name, vshard_router:routeall())
67+
local space, err = utils.get_space(space_name, vshard_router, opts.timeout)
6868
if err ~= nil then
6969
return nil, InsertError:new("An error occurred during the operation: %s", err), const.NEED_SCHEMA_RELOAD
7070
end

crud/insert_many.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ local function call_insert_many_on_router(vshard_router, space_name, original_tu
131131
vshard_router = '?string|table',
132132
})
133133

134-
local space, err = utils.get_space(space_name, vshard_router:routeall())
134+
local space, err = utils.get_space(space_name, vshard_router, opts.timeout)
135135
if err ~= nil then
136136
return nil, {
137137
InsertManyError:new("An error occurred during the operation: %s", err)

crud/len.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function len.call(space_name, opts)
6464
return nil, LenError:new(err)
6565
end
6666

67-
local space, err = utils.get_space(space_name, vshard_router:routeall())
67+
local space, err = utils.get_space(space_name, vshard_router, opts.timeout)
6868
if err ~= nil then
6969
return nil, LenError:new("An error occurred during the operation: %s", err)
7070
end

crud/replace.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ local function call_replace_on_router(vshard_router, space_name, original_tuple,
6464
vshard_router = '?string|table',
6565
})
6666

67-
local space, err = utils.get_space(space_name, vshard_router:routeall())
67+
local space, err = utils.get_space(space_name, vshard_router, opts.timeout)
6868
if err ~= nil then
6969
return nil, ReplaceError:new("An error occurred during the operation: %s", err), const.NEED_SCHEMA_RELOAD
7070
end

crud/replace_many.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ local function call_replace_many_on_router(vshard_router, space_name, original_t
133133
vshard_router = '?string|table',
134134
})
135135

136-
local space, err = utils.get_space(space_name, vshard_router:routeall())
136+
local space, err = utils.get_space(space_name, vshard_router, opts.timeout)
137137
if err ~= nil then
138138
return nil, {
139139
ReplaceManyError:new("An error occurred during the operation: %s", err)

crud/select/compat/select.lua

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,7 @@ local function build_select_iterator(vshard_router, space_name, user_conditions,
4242
return nil, SelectError:new("Failed to parse conditions: %s", err)
4343
end
4444

45-
local replicasets, err = vshard_router:routeall()
46-
if err ~= nil then
47-
return nil, SelectError:new("Failed to get router replicasets: %s", err)
48-
end
49-
50-
local space, err = utils.get_space(space_name, replicasets)
45+
local space, err = utils.get_space(space_name, vshard_router)
5146
if err ~= nil then
5247
return nil, SelectError:new("An error occurred during the operation: %s", err), const.NEED_SCHEMA_RELOAD
5348
end
@@ -84,7 +79,10 @@ local function build_select_iterator(vshard_router, space_name, user_conditions,
8479
end
8580

8681
-- set replicasets to select from
87-
local replicasets_to_select = replicasets
82+
local replicasets_to_select, err = vshard_router:routeall()
83+
if err ~= nil then
84+
return nil, SelectError:new("Failed to get router replicasets: %s", err)
85+
end
8886

8987
-- Whether to call one storage replicaset or perform
9088
-- map-reduce?

crud/select/compat/select_old.lua

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,7 @@ local function build_select_iterator(vshard_router, space_name, user_conditions,
107107
return nil, SelectError:new("Failed to parse conditions: %s", err)
108108
end
109109

110-
local replicasets, err = vshard_router:routeall()
111-
if err ~= nil then
112-
return nil, SelectError:new("Failed to get router replicasets: %s", err)
113-
end
114-
115-
local space, err = utils.get_space(space_name, replicasets)
110+
local space, err = utils.get_space(space_name, vshard_router)
116111
if err ~= nil then
117112
return nil, SelectError:new("An error occurred during the operation: %s", err), const.NEED_SCHEMA_RELOAD
118113
end
@@ -151,7 +146,10 @@ local function build_select_iterator(vshard_router, space_name, user_conditions,
151146
end
152147

153148
-- set replicasets to select from
154-
local replicasets_to_select = replicasets
149+
local replicasets_to_select, err = vshard_router:routeall()
150+
if err ~= nil then
151+
return nil, SelectError:new("Failed to get router replicasets: %s", err)
152+
end
155153

156154
-- See explanation of this logic in
157155
-- crud/select/compat/select.lua.

crud/stats/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ local function resolve_space_name(space_id)
264264
return nil
265265
end
266266

267-
local space, err = utils.get_space(space_id, replicasets)
267+
local space, err = utils.get_space(space_id, vshard_router)
268268
if err ~= nil then
269269
log.warn("An error occurred during getting space: %s", err)
270270
return nil

crud/update.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ local function call_update_on_router(vshard_router, space_name, key, user_operat
8484
vshard_router = '?string|table',
8585
})
8686

87-
local space, err = utils.get_space(space_name, vshard_router:routeall())
87+
local space, err = utils.get_space(space_name, vshard_router, opts.timeout)
8888
if err ~= nil then
8989
return nil, UpdateError:new("An error occurred during the operation: %s", err), const.NEED_SCHEMA_RELOAD
9090
end

crud/upsert.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ local function call_upsert_on_router(vshard_router, space_name, original_tuple,
6262
vshard_router = '?string|table',
6363
})
6464

65-
local space, err = utils.get_space(space_name, vshard_router:routeall())
65+
local space, err = utils.get_space(space_name, vshard_router, opts.timeout)
6666
if err ~= nil then
6767
return nil, UpsertError:new("An error occurred during the operation: %s", err), const.NEED_SCHEMA_RELOAD
6868
end

crud/upsert_many.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ local function call_upsert_many_on_router(vshard_router, space_name, original_tu
129129
vshard_router = '?string|table',
130130
})
131131

132-
local space, err = utils.get_space(space_name, vshard_router:routeall())
132+
local space, err = utils.get_space(space_name, vshard_router, opts.timeout)
133133
if err ~= nil then
134134
return nil, {
135135
UpsertManyError:new("An error occurred during the operation: %s", err)

0 commit comments

Comments
 (0)