From f879daabe6523ab64e5100d54c8ce5930cbccbd3 Mon Sep 17 00:00:00 2001 From: denk Date: Tue, 24 Nov 2026 19:31:00 +0300 Subject: [PATCH 1/2] router: fix call retry with return_raw 'return_raw' gives the storage's response to the router packed. Its first element is a flag whether the call succeeded. The error comes packed too. lerror.make() can't decode msgpack, so the error lost its code, and the router could not see that it was retriable. The failed request was returned to the user instead of being rerouted to the new bucket location. Unpack the error, but keep the rest of the response packed. A nil flag is packed as null and comes back as box.NULL, which is truthy. Treat it as a failure too, otherwise the error was returned as a regular value. Closes #682 NO_DOC=bugfix --- test/router-luatest/router_2_2_test.lua | 115 ++++++++++++++++++++++++ vshard/router/init.lua | 9 +- 2 files changed, 123 insertions(+), 1 deletion(-) diff --git a/test/router-luatest/router_2_2_test.lua b/test/router-luatest/router_2_2_test.lua index 218accb7..203e41af 100644 --- a/test/router-luatest/router_2_2_test.lua +++ b/test/router-luatest/router_2_2_test.lua @@ -170,6 +170,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. -- @@ -207,6 +219,48 @@ g.test_return_raw = function(g) 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') + 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) @@ -1090,6 +1144,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. -- diff --git a/vshard/router/init.lua b/vshard/router/init.lua index 0848b449..8794749d 100644 --- a/vshard/router/init.lua +++ b/vshard/router/init.lua @@ -651,12 +651,19 @@ local function router_call_impl(router, bucket_id, mode, prefer_replica, -- 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() + -- A nil status is packed as null. + -- It decodes as a truthy box.NULL, so only true means success. + storage_call_status = it:decode() == true -- 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 + if not storage_call_status and call_status ~= nil then + -- The error is packed as well. Unpack it for + -- lerror.make(): the retry below needs err.code. + call_status = call_status[1] + end call_error = nil end if storage_call_status then From d5666a039c3d23502096db503be47dd9d2f43c27 Mon Sep 17 00:00:00 2001 From: denk Date: Tue, 24 Nov 2026 19:31:00 +0300 Subject: [PATCH 2/2] router: support return_raw in async calls The synchronous call cut the storage's status flag out of the raw response, but the asynchronous one did not. It went through the usual branch, which decodes the values, so 'return_raw' had no effect with 'is_async'. Cut the flag in the future wrapper as well, and return the rest as is, the same way as the synchronous call and netbox do. Now the synchronous call also returns an empty object for an empty response instead of nil, to keep both paths identical and to match netbox. Closes #592 NO_DOC=bugfix --- README.md | 5 +- test/router-luatest/router_2_2_test.lua | 124 +++++++++++++++++++++++- vshard/router/init.lua | 65 ++++++++----- 3 files changed, 165 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 38183607..faf1cc39 100644 --- a/README.md +++ b/README.md @@ -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' ...). diff --git a/test/router-luatest/router_2_2_test.lua b/test/router-luatest/router_2_2_test.lua index 203e41af..dd30364a 100644 --- a/test/router-luatest/router_2_2_test.lua +++ b/test/router-luatest/router_2_2_test.lua @@ -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') -- @@ -219,6 +220,96 @@ 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 @@ -258,6 +349,35 @@ g.test_return_raw_nil_status = function(g) 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 diff --git a/vshard/router/init.lua b/vshard/router/init.lua index 8794749d..44f10c52 100644 --- a/vshard/router/init.lua +++ b/vshard/router/init.lua @@ -472,6 +472,7 @@ 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 @@ -479,8 +480,35 @@ 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 @@ -496,7 +524,7 @@ 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) @@ -504,7 +532,7 @@ local function vshard_future_wait_result(self, 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) @@ -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 @@ -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 @@ -645,25 +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() - -- A nil status is packed as null. - -- It decodes as a truthy box.NULL, so only true means success. - storage_call_status = it:decode() == true - -- 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 - if not storage_call_status and call_status ~= nil then - -- The error is packed as well. Unpack it for - -- lerror.make(): the retry below needs err.code. - call_status = call_status[1] - end + storage_call_status, call_status = + storage_call_raw_extract(storage_call_status) call_error = nil end if storage_call_status then @@ -676,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)