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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,10 @@ vshard.router.info()
* `mode` - `read` or `write`
* `func` - function name
* `args` - array of arguments to func
* `opts` - call options. Can contain only one parameter - `timeout` in seconds.
* `opts` - call options:
* `timeout` - timeout in seconds;
* `return_raw` - when true, values stay packed in a msgpack object;
an empty result is an empty object, not `nil`; an error is decoded.

**Returns:** original return value from `func` or nil and error object.
Error object has type attribute equal to 'ShardingError' or one of error types from tarantool ('ClientError', 'OutOfMemory', 'SocketError' ...).
Expand Down
239 changes: 237 additions & 2 deletions test/router-luatest/router_2_2_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,14 @@ local function test_return_raw_template(g, mode)
t.assert(not res.err, 'no error')

--
-- Empty result set.
-- Empty result set. An empty object is returned, like netbox does.
--
res = router:exec(function(mode)
return add_details(ivshard.router[mode](1, 'echo', {},
{timeout = iwait_timeout, return_raw = true}))
end, {mode})
t.assert(not res.val, 'no value')
t.assert_equals(res.val_type, 'userdata', 'empty result is raw')
t.assert_equals(res.val[1], nil, 'no value')
t.assert(not res.err, 'no error')

--
Expand All @@ -170,6 +171,18 @@ local function test_return_raw_template(g, mode)
t.assert_covers(res.err, {type = 'ClientError', code = box.error.PROC_LUA},
'error value')

--
-- Raw values stay packed, but the error is decoded for lerror.make().
--
res = router:exec(function(mode)
return add_details(ivshard.router[mode](1, 'box_error', {1, 2, 3},
{timeout = iwait_timeout, return_raw = true}))
end, {mode})
t.assert(not res.val, 'no value')
t.assert_equals(res.err_type, 'table', 'raw error type')
t.assert_covers(res.err, {type = 'ClientError', code = box.error.PROC_LUA},
'raw error value')

--
-- Route error.
--
Expand Down Expand Up @@ -207,6 +220,167 @@ g.test_return_raw = function(g)
end)
end

--
-- gh-592: the synchronous call cut the storage.call() status flag out of the
-- raw response, but the asynchronous one did not, and returned decoded values.
--
g.test_return_raw_async = function(g)
t.run_only_if(vutil.feature.netbox_return_raw)

for _, name in ipairs({'callrw', 'callro'}) do
g.router:exec(function(call_name)
local msgpack = require('msgpack')
local call = ivshard.router[call_name]

local values = call(1, 'echo', {1, 2, 3},
{timeout = iwait_timeout, return_raw = true, is_async = true}):
wait_result()
ilt.assert(msgpack.is_object(values), 'values are raw')
ilt.assert_equals({values[1], values[2], values[3]}, {1, 2, 3},
'raw values are intact')

local empty = call(1, 'echo', {},
{timeout = iwait_timeout, return_raw = true, is_async = true}):
wait_result()
ilt.assert(msgpack.is_object(empty), 'empty result is raw')
ilt.assert_equals(empty[1], nil, 'no values')

local _, err = call(1, 'box_error', {},
{timeout = iwait_timeout, return_raw = true, is_async = true}):
wait_result()
ilt.assert_equals(type(err), 'table', 'error is decoded')
ilt.assert_equals(err.code, box.error.PROC_LUA, 'error is intact')
end, {name})
end

--
-- The result stays packed however it is obtained, also on repeat.
--
g.router:exec(function()
local msgpack = require('msgpack')
local opts = {timeout = iwait_timeout, return_raw = true,
is_async = true}
local future = ivshard.router.callro(1, 'echo', {1, 2, 3}, opts)
ilt.assert(msgpack.is_object(future:wait_result()), 'wait_result')
ilt.assert(msgpack.is_object(future:result()), 'result after wait')
ilt.assert(msgpack.is_object(future:result()), 'result again')

local through_pairs = ivshard.router.callro(1, 'echo', {1, 2, 3},
opts)
local found = false
for _, value in through_pairs:pairs(iwait_timeout) do
ilt.assert(msgpack.is_object(value), 'pairs')
-- The status flag must be cut out: values start at [1].
ilt.assert_equals({value[1], value[2], value[3]}, {1, 2, 3},
'pairs raw values')
ilt.assert_equals(value[4], nil, 'pairs has no status')
found = true
end
ilt.assert(found, 'pairs returned the result')
end)

--
-- An error through pairs() in the raw mode.
--
g.router:exec(function()
local f = ivshard.router.callro(1, 'box_error', {},
{timeout = iwait_timeout, return_raw = true, is_async = true})
local found = false
for _, value in f:pairs(iwait_timeout) do
ilt.assert_equals(value[1], nil, 'no status')
ilt.assert_equals(type(value[2]), 'table', 'error is decoded')
ilt.assert_equals(value[2].code, box.error.PROC_LUA,
'error code')
found = true
end
ilt.assert(found, 'pairs returned the error')
end)

--
-- Without 'return_raw' the result is a plain table.
--
for _, name in ipairs({'callrw', 'callro'}) do
g.router:exec(function(call_name)
local msgpack = require('msgpack')
local res = ivshard.router[call_name](1, 'echo', {1, 2, 3},
{timeout = iwait_timeout, is_async = true}):wait_result()
ilt.assert(not msgpack.is_object(res), 'result is decoded')
ilt.assert_equals(res[1], 1, 'decoded value')
end, {name})
end
end

--
-- A raw response can have a nil status: vshard.storage.call() returns one when
-- bucket_ref() fails. In msgpack a nil in an array is packed as null and comes
-- back as box.NULL, which is truthy. If it wasn't turned back into nil, the
-- error would be taken for a success and returned as a value.
--
g.test_return_raw_nil_status = function(g)
t.run_only_if(vutil.feature.netbox_return_raw)

g.router:exec(function()
local msgpack = require('msgpack')
local replicaset = ivshard.router.internal.static_router.replicasets[
ivshard.router.route(1).id]
ilt.assert_not_equals(replicaset, nil, 'replicaset is found')
-- 'callro' comes from the metatable, so the stub is removed via rawset.
local had_own = rawget(replicaset, 'callro') ~= nil
local old_callro = replicaset.callro
replicaset.callro = function()
return msgpack.object({box.NULL, box.error.new(
box.error.PROC_LUA, 'raw nil status')})
end

local ok, val, err = pcall(function()
return ivshard.router.callro(1, 'echo', {1},
{timeout = iwait_timeout,
return_raw = true})
end)

if had_own then
replicaset.callro = old_callro
else
rawset(replicaset, 'callro', nil)
end

ilt.assert(ok, 'call did not throw')
ilt.assert_equals(val, nil, 'no value')
ilt.assert_not_equals(err, nil, 'error is not nil')
ilt.assert_equals(type(err), 'table', 'error is decoded')
ilt.assert_equals(err.code, box.error.PROC_LUA, 'error code')

-- The async path defers the extraction to the future wrapper. Stub the
-- replicaset to return a future resolving to the same packed response,
-- so that the wrapper, not the send path, handles the nil status.
local base = {
wait_result = function() return msgpack.object({box.NULL,
box.error.new(box.error.PROC_LUA, 'raw nil status')}) end,
result = function() return nil end,
is_ready = function() return true end,
discard = function() end,
}
replicaset.callro = function()
return base
end
local future = ivshard.router.callro(1, 'echo', {1},
{timeout = iwait_timeout,
return_raw = true,
is_async = true})
if had_own then
replicaset.callro = old_callro
else
rawset(replicaset, 'callro', nil)
end

local val2, err2 = future:wait_result()
ilt.assert_equals(val2, nil, 'async no value')
ilt.assert_not_equals(err2, nil, 'async error is not nil')
ilt.assert_equals(type(err2), 'table', 'async error is decoded')
ilt.assert_equals(err2.code, box.error.PROC_LUA, 'async error code')
end)
end

g.test_group_buckets = function(g)
local bids = vtest.storage_get_n_buckets(g.replica_1_a, 2)
local val, err = g.router:exec(function(bid1, bid2)
Expand Down Expand Up @@ -1090,6 +1264,67 @@ g.test_retryable_transfer_in_progress = function(g)
end, {bid, g.replica_2_a:replicaset_uuid()})
end

--
-- gh-682: the retry must work with 'return_raw' too. The error needs to
-- be decoded, otherwise the router can't see its code to reroute.
--
g.test_retryable_transfer_in_progress_raw = function(g)
t.run_only_if(vutil.feature.netbox_return_raw)

local bid = g.replica_2_a:exec(function()
return _G.get_first_bucket()
end)

g.replica_1_a:exec(function()
ivshard.storage.internal.errinj.ERRINJ_LAST_RECEIVE_DELAY = true
end)

g.replica_2_a:exec(function(bid, uuid)
rawset(_G, 'bucket_send_fiber', ifiber.create(function()
return ivshard.storage.bucket_send(
bid, uuid, {timeout = iwait_timeout})
end))
_G.bucket_send_fiber:set_joinable(true)
ivshard.storage.internal.bucket_refs[bid] = nil
end, {bid, g.replica_1_a:replicaset_uuid()})

g.router:exec(function(bid)
rawset(_G, 'callrw_fiber', ifiber.create(function()
return ivshard.router.callrw(
bid, 'echo', {123}, {timeout = iwait_timeout,
return_raw = true})
end))
_G.callrw_fiber:set_joinable(true)
end, {bid})

g.replica_1_a:exec(function()
ivshard.storage.internal.errinj.ERRINJ_LAST_RECEIVE_DELAY = false
end)
g.replica_2_a:exec(function()
local ok, res = _G.bucket_send_fiber:join()
ilt.assert(ok and res)
_G.bucket_send_fiber = nil
_G.bucket_gc_wait()
end)

g.router:exec(function()
local msgpack = require('msgpack')
local ok, res, err = _G.callrw_fiber:join()
ilt.assert(ok)
ilt.assert_equals(err, nil, 'no error')
ilt.assert(msgpack.is_object(res), 'result is raw')
ilt.assert_equals(res[1], 123, 'retried and got the value')
_G.callrw_fiber = nil
end)

g.replica_1_a:exec(function(bid, uuid)
local _, err = ivshard.storage.bucket_send(
bid, uuid, {timeout = iwait_timeout})
ilt.assert_equals(err, nil)
_G.bucket_gc_wait()
end, {bid, g.replica_2_a:replicaset_uuid()})
end

--
-- gh-ee-9: service objects are nullified too late.
--
Expand Down
58 changes: 39 additions & 19 deletions vshard/router/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -472,15 +472,43 @@ local function vshard_future_serialize(self)
-- recursive serialization.
local s = setmetatable(table.deepcopy(self), {})
s._base = nil
s._do_return_raw = nil
return s
end

local function vshard_future_is_ready(self)
return self._base:is_ready()
end

local function vshard_future_wrap_result(res)
--
-- Cut the storage.call() status flag out of a raw response. The rest is
-- repacked into a new array and stays packed, except the error: lerror.make()
-- does not understand msgpack, and the router retry needs err.code.
--
local function storage_call_raw_extract(res)
local it = res:iterator()
local count = it:decode_array_header()
-- A nil status is packed as null and decoded as box.NULL, which is truthy.
-- So only true means success.
local status = it:decode()
-- An empty result stays an empty object, same as in raw netbox.
res = it:take_array(count - 1)
if status ~= true then
-- The error is the only value in a failed response.
return false, res[1]
end
return true, res
end

local function vshard_future_wrap_result(res, do_return_raw)
local storage_ok, err
if do_return_raw and msgpack_is_object(res) then
storage_ok, res = storage_call_raw_extract(res)
if not storage_ok then
return nil, lerror.make(res)
end
return res
end
storage_ok, res, err = res[1], res[2], res[3]
if storage_ok then
if res == nil and err ~= nil then
Expand All @@ -496,15 +524,15 @@ local function vshard_future_result(self)
if res == nil then
return nil, lerror.make(err)
end
return vshard_future_wrap_result(res)
return vshard_future_wrap_result(res, self._do_return_raw)
end

local function vshard_future_wait_result(self, timeout)
local res, err = future_wait(self._base, timeout)
if res == nil then
return nil, lerror.make(err)
end
return vshard_future_wrap_result(res)
return vshard_future_wrap_result(res, self._do_return_raw)
end

local function vshard_future_discard(self)
Expand Down Expand Up @@ -545,7 +573,7 @@ local function vshard_future_iter_next(iter, i)
end
-- Will not block because the request is already finished.
if base_next(base, i) == nil then
res, err = vshard_future_wrap_result(res)
res, err = vshard_future_wrap_result(res, base_req._do_return_raw)
if res ~= nil then
return i, res
end
Expand Down Expand Up @@ -584,9 +612,10 @@ local vshard_future_mt = {
-- So vshard.router.call should wrap a future object with its own
-- unpacker of result.
--
local function vshard_future_new(future)
local function vshard_future_new(future, do_return_raw)
-- Use '_' as a prefix so as users could use all normal names.
return setmetatable({_base = future}, vshard_future_mt)
return setmetatable({_base = future, _do_return_raw = do_return_raw},
vshard_future_mt)
end

-- Perform shard operation
Expand Down Expand Up @@ -645,18 +674,8 @@ local function router_call_impl(router, bucket_id, mode, prefer_replica,
replicaset[call](replicaset, 'vshard.storage.call',
{bucket_id, mode, func, args}, opts)
if do_return_raw and msgpack_is_object(storage_call_status) then
-- Storage.call returns in the first value a flag whether user's
-- function threw an exception or not. Need to extract it.
-- Unfortunately, it forces to repack the rest of values into a
-- new array. But the values themselves are not decoded.
local it = storage_call_status:iterator()
local count = it:decode_array_header()
storage_call_status = it:decode()
-- When no values, nil is not packed into msgpack object. Same
-- as in raw netbox.
if count > 1 then
call_status = it:take_array(count - 1)
end
storage_call_status, call_status =
storage_call_raw_extract(storage_call_status)
call_error = nil
end
if storage_call_status then
Expand All @@ -669,7 +688,8 @@ local function router_call_impl(router, bucket_id, mode, prefer_replica,
-- values: true/false and func result. But
-- async returns future object. No true/false
-- nor func result. So return the first value.
return vshard_future_new(storage_call_status)
return vshard_future_new(storage_call_status,
do_return_raw)
end
end
err = lerror.make(call_status)
Expand Down