[Tarantool-patches] [PATCH vshard 01/11] error: introduce vshard.error.timeout()

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Tue Feb 23 03:15:37 MSK 2021


The function returns a box.error.TIMEOUT error converted to the
format used by vshard.

Probably it wouldn't be needed if only Tarantool >= 1.10 was
supported - then error.make(box.error.new(box.error.TIMEOUT))
wouldn't be so bad. But 1.9 is supposed to work as well, and to
create a timeout error on <= 1.9 it is necessary to make a pcall()
which is long and ugly.

vshard.error.timeout() provides a version-agnostic way of
returning timeout errors.

The patch is motivated by timeout error being actively used in the
future patches about map-reduce.

Needed for #147
---
 test/router/sync.result   | 10 +++++++---
 test/router/sync.test.lua |  3 ++-
 test/unit/error.result    | 22 ++++++++++++++++++++++
 test/unit/error.test.lua  |  9 +++++++++
 vshard/error.lua          | 10 ++++++++++
 vshard/replicaset.lua     |  3 +--
 vshard/router/init.lua    |  6 ++----
 vshard/storage/init.lua   |  6 ++----
 8 files changed, 55 insertions(+), 14 deletions(-)

diff --git a/test/router/sync.result b/test/router/sync.result
index 6f0821d..040d611 100644
--- a/test/router/sync.result
+++ b/test/router/sync.result
@@ -45,10 +45,14 @@ vshard.router.bootstrap()
 ---
 - true
 ...
-vshard.router.sync(-1)
+res, err = vshard.router.sync(-1)
 ---
-- null
-- Timeout exceeded
+...
+util.portable_error(err)
+---
+- type: ClientError
+  code: 78
+  message: Timeout exceeded
 ...
 res, err = vshard.router.sync(0)
 ---
diff --git a/test/router/sync.test.lua b/test/router/sync.test.lua
index 3150343..cb36b0e 100644
--- a/test/router/sync.test.lua
+++ b/test/router/sync.test.lua
@@ -15,7 +15,8 @@ util = require('util')
 
 vshard.router.bootstrap()
 
-vshard.router.sync(-1)
+res, err = vshard.router.sync(-1)
+util.portable_error(err)
 res, err = vshard.router.sync(0)
 util.portable_error(err)
 
diff --git a/test/unit/error.result b/test/unit/error.result
index 8552d91..738cfeb 100644
--- a/test/unit/error.result
+++ b/test/unit/error.result
@@ -97,3 +97,25 @@ util.portable_error(err)
   code: 32
   message: '[string "function raise_lua_err() assert(false) end "]:1: assertion failed!'
 ...
+--
+-- lerror.timeout() - portable alternative to box.error.new(box.error.TIMEOUT).
+--
+err = lerror.timeout()
+---
+...
+type(err)
+---
+- table
+...
+assert(err.code == box.error.TIMEOUT)
+---
+- true
+...
+err.type
+---
+- ClientError
+...
+err.message
+---
+- Timeout exceeded
+...
diff --git a/test/unit/error.test.lua b/test/unit/error.test.lua
index 859414e..0a51d33 100644
--- a/test/unit/error.test.lua
+++ b/test/unit/error.test.lua
@@ -36,3 +36,12 @@ function raise_lua_err() assert(false) end
 ok, err = pcall(raise_lua_err)
 err = lerror.make(err)
 util.portable_error(err)
+
+--
+-- lerror.timeout() - portable alternative to box.error.new(box.error.TIMEOUT).
+--
+err = lerror.timeout()
+type(err)
+assert(err.code == box.error.TIMEOUT)
+err.type
+err.message
diff --git a/vshard/error.lua b/vshard/error.lua
index 65da763..a6f46a9 100644
--- a/vshard/error.lua
+++ b/vshard/error.lua
@@ -212,10 +212,20 @@ local function make_alert(code, ...)
     return setmetatable(r, { __serialize = 'seq' })
 end
 
+--
+-- Create a timeout error object. Box.error.new() can't be used because is
+-- present only since 1.10.
+--
+local function make_timeout()
+    local _, err = pcall(box.error, box.error.TIMEOUT)
+    return make_error(err)
+end
+
 return {
     code = error_code,
     box = box_error,
     vshard = vshard_error,
     make = make_error,
     alert = make_alert,
+    timeout = make_timeout,
 }
diff --git a/vshard/replicaset.lua b/vshard/replicaset.lua
index 9c792b3..7437e3b 100644
--- a/vshard/replicaset.lua
+++ b/vshard/replicaset.lua
@@ -401,8 +401,7 @@ local function replicaset_template_multicallro(prefer_replica, balance)
         local timeout = opts.timeout or consts.CALL_TIMEOUT_MAX
         local net_status, storage_status, retval, err, replica
         if timeout <= 0 then
-            net_status, err = pcall(box.error, box.error.TIMEOUT)
-            return nil, lerror.make(err)
+            return nil, lerror.timeout()
         end
         local end_time = fiber_clock() + timeout
         while not net_status and timeout > 0 do
diff --git a/vshard/router/init.lua b/vshard/router/init.lua
index eeb7515..97bcb0a 100644
--- a/vshard/router/init.lua
+++ b/vshard/router/init.lua
@@ -628,8 +628,7 @@ local function router_call_impl(router, bucket_id, mode, prefer_replica,
     if err then
         return nil, err
     else
-        local _, boxerror = pcall(box.error, box.error.TIMEOUT)
-        return nil, lerror.box(boxerror)
+        return nil, lerror.timeout()
     end
 end
 
@@ -1235,8 +1234,7 @@ local function router_sync(router, timeout)
     local opts = {timeout = timeout}
     for rs_uuid, replicaset in pairs(router.replicasets) do
         if timeout < 0 then
-            local ok, err = pcall(box.error, box.error.TIMEOUT)
-            return nil, err
+            return nil, lerror.timeout()
         end
         local status, err = replicaset:callrw('vshard.storage.sync', arg, opts)
         if not status then
diff --git a/vshard/storage/init.lua b/vshard/storage/init.lua
index a3e7008..e0ce31d 100644
--- a/vshard/storage/init.lua
+++ b/vshard/storage/init.lua
@@ -756,8 +756,7 @@ local function sync(timeout)
         lfiber.sleep(0.001)
     until fiber_clock() > tstart + timeout
     log.warn("Timed out during synchronizing replicaset")
-    local ok, err = pcall(box.error, box.error.TIMEOUT)
-    return nil, lerror.make(err)
+    return nil, lerror.timeout()
 end
 
 --------------------------------------------------------------------------------
@@ -1344,8 +1343,7 @@ local function bucket_send_xc(bucket_id, destination, opts, exception_guard)
     while ref.rw ~= 0 do
         timeout = deadline - fiber_clock()
         if not M.bucket_rw_lock_is_ready_cond:wait(timeout) then
-            status, err = pcall(box.error, box.error.TIMEOUT)
-            return nil, lerror.make(err)
+            return nil, lerror.timeout()
         end
         lfiber.testcancel()
     end
-- 
2.24.3 (Apple Git-128)



More information about the Tarantool-patches mailing list