Skip to content

Commit 8bfa3b5

Browse files
committed
internal: master availability check for get space
Added validation of the master availability to the `utils.get_space` method before receiving the space through the connection. Closes #95 Closes #331
1 parent 0026e51 commit 8bfa3b5

17 files changed

+74
-20
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 validation of the master availability to the `utils.get_space`
12+
method before receiving the space through the connection (#95 #331).
1113

1214
### Changed
1315

crud/borders.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ local function call_get_border_on_router(vshard_router, border_name, space_name,
7474
})
7575

7676
local replicasets = vshard_router:routeall()
77-
local space = utils.get_space(space_name, replicasets)
77+
local space, err = utils.get_space(space_name, replicasets)
78+
if err ~= nil then
79+
return nil, err, const.NEED_SCHEMA_RELOAD
80+
end
7881
if space == nil then
7982
return nil, BorderError:new("Space %q doesn't exist", space_name), const.NEED_SCHEMA_RELOAD
8083
end

crud/common/utils.lua

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ local FlattenError = errors.new_class("FlattenError", {capture_stack = false})
1515
local UnflattenError = errors.new_class("UnflattenError", {capture_stack = false})
1616
local ParseOperationsError = errors.new_class('ParseOperationsError', {capture_stack = false})
1717
local ShardingError = errors.new_class('ShardingError', {capture_stack = false})
18+
local GetSpaceError = errors.new_class('GetSpaceError')
1819
local GetSpaceFormatError = errors.new_class('GetSpaceFormatError', {capture_stack = false})
1920
local FilterFieldsError = errors.new_class('FilterFieldsError', {capture_stack = false})
2021
local NotInitializedError = errors.new_class('NotInitialized')
@@ -97,13 +98,30 @@ end
9798

9899
function utils.get_space(space_name, replicasets)
99100
local replicaset = select(2, next(replicasets))
101+
102+
if replicaset.master == nil then
103+
local error_msg = string.format(
104+
'The master is not configured for replicaset %s, ' ..
105+
'check status of the master and repeat the operation later',
106+
replicaset.uuid)
107+
return nil, GetSpaceError:new(error_msg)
108+
end
109+
if replicaset.master.conn.error ~= nil then
110+
local error_msg = string.format(
111+
'The connection to the master of replicaset %s is not valid: %s',
112+
replicaset.uuid, replicaset.master.conn.error)
113+
return nil, GetSpaceError:new(error_msg)
114+
end
100115
local space = replicaset.master.conn.space[space_name]
101116

102-
return space
117+
return space, nil
103118
end
104119

105120
function utils.get_space_format(space_name, replicasets)
106-
local space = utils.get_space(space_name, replicasets)
121+
local space, err = utils.get_space(space_name, replicasets)
122+
if err ~= nil then
123+
return nil, err
124+
end
107125
if space == nil then
108126
return nil, GetSpaceFormatError:new("Space %q doesn't exist", space_name)
109127
end

crud/count.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,10 @@ local function call_count_on_router(vshard_router, space_name, user_conditions,
137137
return nil, CountError:new("Failed to get router replicasets: %s", err)
138138
end
139139

140-
local space = utils.get_space(space_name, replicasets)
140+
local space, err = utils.get_space(space_name, replicasets)
141+
if err ~= nil then
142+
return nil, err, const.NEED_SCHEMA_RELOAD
143+
end
141144
if space == nil then
142145
return nil, CountError:new("Space %q doesn't exist", space_name), const.NEED_SCHEMA_RELOAD
143146
end

crud/delete.lua

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

65-
local space = utils.get_space(space_name, vshard_router:routeall())
65+
local space, err = utils.get_space(space_name, vshard_router:routeall())
66+
if err ~= nil then
67+
return nil, err, const.NEED_SCHEMA_RELOAD
68+
end
6669
if space == nil then
6770
return nil, DeleteError:new("Space %q doesn't exist", space_name), const.NEED_SCHEMA_RELOAD
6871
end

crud/get.lua

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

68-
local space = utils.get_space(space_name, vshard_router:routeall())
68+
local space, err = utils.get_space(space_name, vshard_router:routeall())
69+
if err ~= nil then
70+
return nil, err, const.NEED_SCHEMA_RELOAD
71+
end
6972
if space == nil then
7073
return nil, GetError:new("Space %q doesn't exist", space_name), const.NEED_SCHEMA_RELOAD
7174
end

crud/insert.lua

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

67-
local space = utils.get_space(space_name, vshard_router:routeall())
67+
local space, err = utils.get_space(space_name, vshard_router:routeall())
68+
if err ~= nil then
69+
return nil, err, const.NEED_SCHEMA_RELOAD
70+
end
6871
if space == nil then
6972
return nil, InsertError:new("Space %q doesn't exist", space_name), const.NEED_SCHEMA_RELOAD
7073
end

crud/insert_many.lua

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

134-
local space = utils.get_space(space_name, vshard_router:routeall())
134+
local space, err = utils.get_space(space_name, vshard_router:routeall())
135+
if err ~= nil then
136+
return nil, err, const.NEED_SCHEMA_RELOAD
137+
end
135138
if space == nil then
136139
return nil, {InsertManyError:new("Space %q doesn't exist", space_name)}, const.NEED_SCHEMA_RELOAD
137140
end

crud/len.lua

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

67-
local space = utils.get_space(space_name, vshard_router:routeall())
67+
local space, err = utils.get_space(space_name, vshard_router:routeall())
68+
if err ~= nil then
69+
return nil, err
70+
end
6871
if space == nil then
6972
return nil, LenError:new("Space %q doesn't exist", space_name)
7073
end

crud/replace.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,8 @@ local function call_replace_on_router(vshard_router, space_name, original_tuple,
6666

6767
local space, err = utils.get_space(space_name, vshard_router:routeall())
6868
if err ~= nil then
69-
return nil, ReplaceError:new("Failed to get space %q: %s", space_name, err), const.NEED_SCHEMA_RELOAD
69+
return nil, err, const.NEED_SCHEMA_RELOAD
7070
end
71-
7271
if space == nil then
7372
return nil, ReplaceError:new("Space %q doesn't exist", space_name), const.NEED_SCHEMA_RELOAD
7473
end

crud/replace_many.lua

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

136-
local space = utils.get_space(space_name, vshard_router:routeall())
136+
local space, err = utils.get_space(space_name, vshard_router:routeall())
137+
if err ~= nil then
138+
return nil, err, const.NEED_SCHEMA_RELOAD
139+
end
137140
if space == nil then
138141
return nil, {ReplaceManyError:new("Space %q doesn't exist", space_name)}, const.NEED_SCHEMA_RELOAD
139142
end

crud/select/compat/select.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ local function build_select_iterator(vshard_router, space_name, user_conditions,
4747
return nil, SelectError:new("Failed to get router replicasets: %s", err)
4848
end
4949

50-
local space = utils.get_space(space_name, replicasets)
50+
local space, err = utils.get_space(space_name, replicasets)
51+
if err ~= nil then
52+
return nil, err, const.NEED_SCHEMA_RELOAD
53+
end
5154
if space == nil then
5255
return nil, SelectError:new("Space %q doesn't exist", space_name), const.NEED_SCHEMA_RELOAD
5356
end

crud/select/compat/select_old.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ local function build_select_iterator(vshard_router, space_name, user_conditions,
112112
return nil, SelectError:new("Failed to get router replicasets: %s", err)
113113
end
114114

115-
local space = utils.get_space(space_name, replicasets)
115+
local space, err = utils.get_space(space_name, replicasets)
116+
if err ~= nil then
117+
return nil, err, const.NEED_SCHEMA_RELOAD
118+
end
116119
if space == nil then
117120
return nil, SelectError:new("Space %q doesn't exist", space_name), const.NEED_SCHEMA_RELOAD
118121
end

crud/stats/init.lua

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

267-
local space = utils.get_space(space_id, replicasets)
267+
local space, err = utils.get_space(space_id, replicasets)
268+
if err ~= nil then
269+
log.warn(err)
270+
return nil
271+
end
268272
if space == nil then
269273
log.warn('Failed to resolve space name for stats: no space found for id %d with default router', space_id)
270274
return nil

crud/update.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,8 @@ local function call_update_on_router(vshard_router, space_name, key, user_operat
8686

8787
local space, err = utils.get_space(space_name, vshard_router:routeall())
8888
if err ~= nil then
89-
return nil, UpdateError:new("Failed to get space %q: %s", space_name, err), const.NEED_SCHEMA_RELOAD
89+
return nil, err, const.NEED_SCHEMA_RELOAD
9090
end
91-
9291
if space == nil then
9392
return nil, UpdateError:new("Space %q doesn't exist", space_name), const.NEED_SCHEMA_RELOAD
9493
end

crud/upsert.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@ local function call_upsert_on_router(vshard_router, space_name, original_tuple,
6464

6565
local space, err = utils.get_space(space_name, vshard_router:routeall())
6666
if err ~= nil then
67-
return nil, UpsertError:new("Failed to get space %q: %s", space_name, err), const.NEED_SCHEMA_RELOAD
67+
return nil, err, const.NEED_SCHEMA_RELOAD
6868
end
69-
7069
if space == nil then
7170
return nil, UpsertError:new("Space %q doesn't exist", space_name), const.NEED_SCHEMA_RELOAD
7271
end

crud/upsert_many.lua

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

132-
local space = utils.get_space(space_name, vshard_router:routeall())
132+
local space, err = utils.get_space(space_name, vshard_router:routeall())
133+
if err ~= nil then
134+
return nil, err, const.NEED_SCHEMA_RELOAD
135+
end
133136
if space == nil then
134137
return nil, {UpsertManyError:new("Space %q doesn't exist", space_name)}, const.NEED_SCHEMA_RELOAD
135138
end

0 commit comments

Comments
 (0)