Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions test/replicaset-luatest/replicaset_3_test.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local fiber = require('fiber')
local json = require('json')
local t = require('luatest')
Comment thread
Serpentian marked this conversation as resolved.
Resolved
local vreplicaset = require('vshard.replicaset')
local vtest = require('test.luatest_helpers.vtest')
Expand Down Expand Up @@ -1186,3 +1187,77 @@ test_group.test_conn_recovery_service_during_replica_backoff = function(g)
g.replica_1_b:exec(function() ivshard.storage.enable() end)
wait_conn_recovery_service_activity(rs, uuid_1_b, 'idling')
end

--
-- gh-651: errors are logged as json no matter their kind.
--
test_group.test_replica_call_logs_errors_as_json = function(g)
g.replica_1_a:exec(function(cfg)
-- Disable the ratelimiter, so that every error is logged.
local vconsts = require('vshard.consts')
rawset(_G, 'test_log_ratelimit_interval',
vconsts.LOG_RATELIMIT_INTERVAL)
vconsts.LOG_RATELIMIT_INTERVAL = 0
local _, rs = next(require('vshard.replicaset').buildall(cfg))
rs:wait_connected_all({timeout = 10})
rawset(_G, 'test_replica', rs.master)
-- Make the call fail with the given error as if it was thrown by
-- the net.box.
rawset(_G, 'test_call_failing_with', function(func, err)
_G.test_replica.conn.call = function() error(err, 0) end
local ok, _, res = _G.test_replica:call(func, {}, {timeout = 10})
ilt.assert_not(ok)
return res
end)
end, {global_cfg})

-- Returns the error object printed into the log by the call of the func.
local function get_logged_error(func)
local line = g.replica_1_a:grep_log(
string.format("Exception during calling '%s' on .*", func))
t.assert(line, string.format("Failed to find '%s' in logs", func))
local encoded = line:match('(%b{})%s*$')
t.assert(encoded, string.format("Error is not json: %s", line))
return json.decode(encoded)
end

-- String.
local res = g.replica_1_a:exec(function()
return _G.test_call_failing_with('echo_string', 'string error')
end)
t.assert_equals(res.message, 'string error')
t.assert_covers(get_logged_error('echo_string'), {
message = 'string error',
code = box.error.PROC_LUA,
})

-- Box error.
g.replica_1_a:exec(function()
local err = box.error.new(box.error.NO_CONNECTION)
_G.test_call_failing_with('echo_box', err)
end)
t.assert_covers(get_logged_error('echo_box'), {
code = box.error.NO_CONNECTION,
})

-- VShard error.
g.replica_1_a:exec(function()
local err = require('vshard.error').vshard(
require('vshard.error').code.NO_SUCH_REPLICASET, 'rs1')
_G.test_call_failing_with('echo_vshard', err)
end)
t.assert_covers(get_logged_error('echo_vshard'), {
type = 'ShardingError',
code = verror.code.NO_SUCH_REPLICASET,
name = 'NO_SUCH_REPLICASET',
})

g.replica_1_a:exec(function()
_G.test_replica.conn:close()
require('vshard.consts').LOG_RATELIMIT_INTERVAL =
_G.test_log_ratelimit_interval
_G.test_replica = nil
_G.test_call_failing_with = nil
_G.test_log_ratelimit_interval = nil
end)
end
35 changes: 35 additions & 0 deletions test/unit-luatest/error_test.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local t = require('luatest')
local json = require('json')
local vutil = require('vshard.util')
local verror = require('vshard.error')

Expand Down Expand Up @@ -33,3 +34,37 @@ g.test_box_error_prev = function()
t.assert_equals(e2, ve2)
t.assert_equals(e3, ve3)
end

--
-- gh-651: __tostring of an error is json, and it does not throw when the error
-- can't be encoded.
--
g.test_tostring = function()
local errs = {
verror.make('string error'),
verror.make(box.error.new(box.error.PROC_LUA, 'box error')),
verror.vshard(verror.code.NO_SUCH_REPLICASET, 'rs1'),
verror.make({message = 'table error', type = 'CustomError'}),
}
for _, err in pairs(errs) do
t.assert_equals(tostring(err), json.encode(err))
end
end

g.test_tostring_unencodable = function()
-- Function can't be encoded.
local err = verror.make({message = 'msg', context = function() end})
t.assert_equals(tostring(err), 'msg')
-- Table key must be a number or a string.
err = verror.make({message = 'msg', payload = {[false] = 'value'}})
t.assert_equals(tostring(err), 'msg')
-- NaN can be encoded only when the json is configured to do so.
local old_cfg = json.cfg.encode_invalid_numbers
json.cfg{encode_invalid_numbers = false}
local ok, res = pcall(function()
return tostring(verror.make({message = 'msg', invalid = 0 / 0}))
end)
json.cfg{encode_invalid_numbers = old_cfg}
t.assert(ok, res)
t.assert_equals(res, 'msg')
end
24 changes: 19 additions & 5 deletions vshard/error.lua
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,21 @@ for code, err in pairs(error_message_template) do
error_code[err.name] = code
end

--
-- The error is stringified while it is logged, so this must not throw.
-- json.encode() does on the values it can't encode, e.g. on a function or on a
-- table key, which is not a number or a string.
--
local function error_tostring(err)
local ok, res = pcall(json.encode, err)
Comment thread
Serpentian marked this conversation as resolved.
Resolved
if ok then
return res
end
return tostring(err.message)
end

local error_mt = {__tostring = error_tostring}

--
-- There are 2 error types:
-- * box_error - it is created on tarantool errors: client error,
Expand All @@ -235,13 +250,12 @@ end
-- 'ShardingError', one of codes below and optional
-- message.
--
local box_error_mt = {__tostring = json.encode}
local function box_error(original_error)
local res = setmetatable(original_error:unpack(), box_error_mt)
local res = setmetatable(original_error:unpack(), error_mt)
local pos = res
local prev = pos.prev
while prev ~= nil do
prev = setmetatable(prev:unpack(), box_error_mt)
prev = setmetatable(prev:unpack(), error_mt)
pos.prev = prev
pos = prev
prev = pos.prev
Expand All @@ -265,7 +279,7 @@ local function vshard_error(code, ...)
assert(#args == args_passed_cnt,
string.format('Wrong number of arguments are passed to %s error',
format.name))
local ret = setmetatable({}, {__tostring = json.encode})
local ret = setmetatable({}, error_mt)
-- Save error arguments.
for i = 1, #args do
ret[args[i]] = select(i, ...)
Expand All @@ -288,7 +302,7 @@ local function make_error(e)
elseif type(e) == 'string' then
return box_error(box.error.new(box.error.PROC_LUA, e))
elseif type(e) == 'table' then
return setmetatable(e, {__tostring = json.encode})
return setmetatable(e, error_mt)
else
return e
end
Expand Down
29 changes: 18 additions & 11 deletions vshard/replicaset.lua
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ local function conn_vconnect_check_or_close(conn)
if not ok and err and not (err.type == 'ShardingError' and
err.code == lerror.code.VHANDSHAKE_NOT_COMPLETE) then
log.warn('Closing the connection to %s due to failed initial ' ..
'handshake: %s', conn.replica, err)
'handshake: %s', conn.replica, tostring(lerror.make(err)))
conn:close()
end
return ok, err
Expand Down Expand Up @@ -265,7 +265,8 @@ local function conn_vconnect_wait_or_close(conn, timeout)
local ok, err = conn_vconnect_wait(conn, timeout)
if not ok and not lerror.is_timeout(err) then
log.warn('Closing the connection to %s after waiting due to failed ' ..
'initial handshake: %s', conn.replica, err)
'initial handshake: %s', conn.replica,
tostring(lerror.make(err)))
conn:close()
end
return ok, err
Expand Down Expand Up @@ -708,9 +709,11 @@ local function replica_call(replica, func, args, opts)
err.type == 'ClientError') then
err = lerror.from_string(err.message) or err
end
err = lerror.make(err)
replica.limiter:log_error(err,
"Exception during calling '%s' on '%s': %s", func, replica, err)
return false, nil, lerror.make(err)
"Exception during calling '%s' on '%s': %s", func, replica,
tostring(err))
return false, nil, err
else
replica_on_success_request(replica)
end
Expand Down Expand Up @@ -981,7 +984,8 @@ local function replicaset_template_multicallro(prefer_replica, balance)
if not replica.backoff_ts then
log.warn('Replica %s goes into backoff for %s sec '..
'after error %s', replica.id,
consts.REPLICA_BACKOFF_INTERVAL, retval)
consts.REPLICA_BACKOFF_INTERVAL,
tostring(lerror.make(retval)))
replica.backoff_ts = now
replica.backoff_err = retval
local msg = string.format(health_status_msgs.BACKOFF,
Expand Down Expand Up @@ -1253,7 +1257,7 @@ local function replicaset_locate_master(replicaset)
elseif not ok and cur_master == master then
log.info('Master of replicaset %s, node %s, does not respond: ' ..
'%s. Trying to find a new one',
replicaset.id, master.id, err)
replicaset.id, master.id, tostring(lerror.make(err)))
-- Try to search for a new master. Master is not nullified, since
-- we are unsure that there's a new one in the replicaset, but we
-- must anyway check, because the connection may be shown as
Expand Down Expand Up @@ -1810,7 +1814,8 @@ local function replica_failover_ping(replica, opts)
local net_status, info, err =
replica:call('vshard.storage._call', {'info', info_opts}, opts)
if not info then
replica:update_status(status_name, SYELLOW, err)
replica:update_status(status_name, SYELLOW,
tostring(lerror.make(err)))
return net_status, info, err
elseif not info.health then
local msg = 'Health state in ping is missing - please upgrade storage'
Expand Down Expand Up @@ -1852,7 +1857,7 @@ local function replica_failover_service_step(replica, data)
if not net_status then
data.limiter:log_error(err, data.info:set_status_error(
'Ping error from %s: perhaps a connection is down: %s',
replica, err))
replica, tostring(lerror.make(err))))
-- Connection hangs. Recreate it to be able to
-- fail over to a replica next by priority. The
-- old connection is not closed in case if it just
Expand Down Expand Up @@ -1986,7 +1991,7 @@ local function replicaset_failover_service_step(replicaset, data)
if not ok then
data.limiter:log_error(replica_is_changed, data.info:set_status_error(
'Error during failovering: %s',
lerror.make(replica_is_changed)))
tostring(lerror.make(replica_is_changed))))
replica_is_changed = true
else
data.info:set_status_ok()
Expand Down Expand Up @@ -2113,7 +2118,8 @@ local function replicaset_master_search_service_step(replicaset, data)
replicaset_master_search_step(replicaset, {mode = mode})
if err then
data.limiter:log_error(err, data.info:set_status_error(
'Error during master search: %s', lerror.make(err)))
'Error during master search: %s',
tostring(lerror.make(err))))
end
if is_done then
timeout = consts.MASTER_SEARCH_IDLE_INTERVAL
Expand Down Expand Up @@ -2171,7 +2177,8 @@ local function worker_f(owner)
local ok, ret = pcall(service.func, owner, service.data)
fiber.testcancel()
if not ok then
log.error('%s failed: %s', service.func_name, ret)
log.error('%s failed: %s', service.func_name,
tostring(lerror.make(ret)))
-- Immediately retry after yield. Works as reloadable fiber.
service.deadline = 0
goto next_service
Expand Down
4 changes: 2 additions & 2 deletions vshard/router/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ local function discovery_service_f(router, service, limiter)
if not future then
limiter:log_warn(err, service:set_status_error(
'Error during discovery %s, retry will be done later: %s',
rs_id, err))
rs_id, tostring(lerror.make(err))))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tarantool 1.10 is not supported by VShard anymore, and the minimum supported version (2.11) already has no problems with calling the tostring by itself. So this new tostring is excessive. Let's drop it from the patch.

The lerror.make can remain. The motivation is consistent logging of errors: otherwise Tarantool's error's are logged as message only, but vshard ones: with code, fields, message and so on. The commit msg should be updated.

goto continue
end
iter.future = future
Expand Down Expand Up @@ -339,7 +339,7 @@ local function discovery_service_f(router, service, limiter)
future:discard()
limiter:log_warn(err, service:set_status_error(
'Error during discovery %s, retry will be done '..
'later: %s', rs_id, err))
'later: %s', rs_id, tostring(lerror.make(err))))
goto continue
end
local replicaset = router.replicasets[rs_id]
Expand Down
Loading