Skip to content

Commit 093e848

Browse files
GRISHNOVDifferentialOrange
authored andcommitted
internal: master presence check for get space
Added validation of the master presence in replicaset and the master connection to the `utils.get_space` method before receiving the space from the connection. Closes #331
1 parent 0026e51 commit 093e848

File tree

17 files changed

+87
-19
lines changed

17 files changed

+87
-19
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1212
### Changed
1313

1414
### Fixed
15+
* Added validation of the master presence in replicaset and the
16+
master connection to the `utils.get_space` method before
17+
receiving the space from the connection (#331).
1518

1619
## [0.14.1] - 10-11-22
1720

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, BorderError:new("An error occurred during the operation: %s", 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: 26 additions & 1 deletion
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,37 @@ end
9798

9899
function utils.get_space(space_name, replicasets)
99100
local replicaset = select(2, next(replicasets))
101+
102+
if replicaset == nil then
103+
return nil, GetSpaceError:new(
104+
'The router returned empty replicasets: ' ..
105+
'perhaps other instances are unavailable or you have configured only the router')
106+
end
107+
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
115+
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)
121+
end
100122
local space = replicaset.master.conn.space[space_name]
101123

102124
return space
103125
end
104126

105127
function utils.get_space_format(space_name, replicasets)
106-
local space = utils.get_space(space_name, replicasets)
128+
local space, err = utils.get_space(space_name, replicasets)
129+
if err ~= nil then
130+
return nil, GetSpaceFormatError:new("An error occurred during the operation: %s", err)
131+
end
107132
if space == nil then
108133
return nil, GetSpaceFormatError:new("Space %q doesn't exist", space_name)
109134
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, CountError:new("An error occurred during the operation: %s", 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, DeleteError:new("An error occurred during the operation: %s", 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, GetError:new("An error occurred during the operation: %s", 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, InsertError:new("An error occurred during the operation: %s", 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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,12 @@ 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, {
137+
InsertManyError:new("An error occurred during the operation: %s", err)
138+
}, const.NEED_SCHEMA_RELOAD
139+
end
135140
if space == nil then
136141
return nil, {InsertManyError:new("Space %q doesn't exist", space_name)}, const.NEED_SCHEMA_RELOAD
137142
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, LenError:new("An error occurred during the operation: %s", 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, ReplaceError:new("An error occurred during the operation: %s", 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

0 commit comments

Comments
 (0)