Skip to content

Commit 9b86add

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 9b86add

19 files changed

+63
-43
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

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)
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/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: 27 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,26 @@ 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)
100+
local replicasets, replicaset
101+
local deadline = fiber.clock() + const.GET_SPACE_TIMEOUT
102+
while (
103+
-- Break if the deadline condition is exceeded.
104+
-- Handling for deadline errors are below in the code.
105+
fiber.clock() < deadline
106+
) do
107+
-- Try to get master with timeout.
108+
fiber.yield()
109+
replicasets = vshard_router:routeall()
110+
replicaset = select(2, next(replicasets))
111+
if replicaset ~= nil and
112+
replicaset.master ~= nil and
113+
replicaset.master.conn.error == nil then
114+
-- Break if there is master in replicaset and
115+
-- connection to master is established.
116+
break
117+
end
118+
end
101119

102120
if replicaset == nil then
103121
return nil, GetSpaceError:new(
@@ -119,13 +137,14 @@ function utils.get_space(space_name, replicasets)
119137
replicaset.uuid, replicaset.master.conn.error)
120138
return nil, GetSpaceError:new(error_msg)
121139
end
140+
122141
local space = replicaset.master.conn.space[space_name]
123142

124143
return space
125144
end
126145

127-
function utils.get_space_format(space_name, replicasets)
128-
local space, err = utils.get_space(space_name, replicasets)
146+
function utils.get_space_format(space_name, vshard_router)
147+
local space, err = utils.get_space(space_name, vshard_router)
129148
if err ~= nil then
130149
return nil, GetSpaceFormatError:new("An error occurred during the operation: %s", err)
131150
end
@@ -664,7 +683,7 @@ function utils.cut_rows(rows, metadata, field_names)
664683
end
665684

666685
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())
686+
local space_format, err = utils.get_space_format(space_name, vshard_router)
668687
if err ~= nil then
669688
return nil, FlattenError:new("Failed to get space format: %s", err), const.NEED_SCHEMA_RELOAD
670689
end
@@ -835,9 +854,9 @@ function utils.storage_info(opts)
835854
end
836855
end
837856

838-
local deadline = fiber_clock() + timeout
857+
local deadline = fiber.clock() + timeout
839858
for replica_uuid, future in pairs(futures_by_replicas) do
840-
local wait_timeout = deadline - fiber_clock()
859+
local wait_timeout = deadline - fiber.clock()
841860
if wait_timeout < 0 then
842861
wait_timeout = 0
843862
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
133133
if err ~= nil then
134134
return nil, {
135135
UpsertManyError:new("An error occurred during the operation: %s", err)

test/helper.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ function helpers.is_space_exist(router, space_name)
452452
local vshard = require('vshard')
453453
local utils = require('crud.common.utils')
454454
455-
local space, err = utils.get_space(..., vshard.router.routeall())
455+
local space, err = utils.get_space(..., vshard.router)
456456
if err ~= nil then
457457
return nil, err
458458
end

0 commit comments

Comments
 (0)