Skip to content

Commit 54efec8

Browse files
committed
internal: 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 54efec8

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ 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 to the `utils.get_space`
13+
method (#95).
1114

1215
### Changed
1316

crud/common/const.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const.RELOAD_SCHEMA_TIMEOUT = 3 -- 3 seconds
55
const.FETCH_SHARDING_METADATA_TIMEOUT = 3 -- 3 seconds
66
const.SHARDING_RELOAD_RETRIES_NUM = 1
77

8+
const.GET_SPACE_TIMEOUT = 3 -- 3 seconds
9+
810
const.NEED_SCHEMA_RELOAD = 0x0001000
911
const.NEED_SHARDING_RELOAD = 0x0001001
1012

crud/common/utils.lua

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ local NotInitializedError = errors.new_class('NotInitialized')
2222
local StorageInfoError = errors.new_class('StorageInfoError')
2323
local VshardRouterError = errors.new_class('VshardRouterError', {capture_stack = false})
2424
local fiber_clock = require('fiber').clock
25+
local fiber = require('fiber')
2526

2627
local utils = {}
2728

@@ -105,19 +106,44 @@ function utils.get_space(space_name, replicasets)
105106
'perhaps other instances are unavailable or you have configured only the router')
106107
end
107108

108-
if replicaset.master == nil then
109-
local error_msg = string.format(
110-
'The master was not found in replicaset %s, ' ..
111-
'check status of the master and repeat the operation later',
112-
replicaset.uuid)
113-
return nil, GetSpaceError:new(error_msg)
114-
end
109+
if replicaset.master == nil or replicaset.master.conn.error ~= nil then
110+
-- Try to get master again with timeout.
111+
local deadline = fiber_clock() + const.GET_SPACE_TIMEOUT
112+
local vshard_router, err = utils.get_vshard_router_instance()
113+
if err ~= nil then
114+
return nil, GetSpaceError:new(err)
115+
end
116+
while true do
117+
::continue::
118+
fiber.yield()
119+
replicasets = vshard_router:routeall()
120+
replicaset = select(2, next(replicasets))
121+
122+
if replicaset.master ~= nil and replicaset.master.conn.error ~= nil then
123+
if fiber_clock() < deadline then
124+
-- Try to get the master again, the connection to it may be established.
125+
goto continue
126+
else
127+
local error_msg = string.format(
128+
'The connection to the master of replicaset %s is not valid: %s',
129+
replicaset.uuid, replicaset.master.conn.error)
130+
return nil, GetSpaceError:new(error_msg)
131+
end
132+
end
133+
134+
if replicaset.master == nil and fiber_clock() >= deadline then
135+
local error_msg = string.format(
136+
'The master was not found in replicaset %s, ' ..
137+
'check status of the master and repeat the operation later',
138+
replicaset.uuid)
139+
return nil, GetSpaceError:new(error_msg)
140+
end
115141

116-
if replicaset.master.conn.error ~= nil then
117-
local error_msg = string.format(
118-
'The connection to the master of replicaset %s is not valid: %s',
119-
replicaset.uuid, replicaset.master.conn.error)
120-
return nil, GetSpaceError:new(error_msg)
142+
if replicaset.master ~= nil and replicaset.master.conn.error == nil then
143+
-- Break if there is master and connection to it is established.
144+
break
145+
end
146+
end
121147
end
122148
local space = replicaset.master.conn.space[space_name]
123149

0 commit comments

Comments
 (0)