From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id C57D66ECC0; Fri, 17 Dec 2021 03:27:41 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C57D66ECC0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1639700861; bh=lbGy2S1vwvZgc/4LDeg7dgrqxhBFJt3/YTg9fOChLsc=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=hwy9cit6Oj97C5entufvZWmu4/45DUI+pvjUjfpPZrTwdzPEDWpFG2H30rn4Tz24Q 2QojFEcUS6NsnzacLBRr3T9AmjpiHYFYssiCwhmmbOcoL1CUpmW9fyVFXlKtisb9// CKyT6BOWm5K07nlNxHVpecX++48MFP57l1UUzZCk= Received: from smtpng1.i.mail.ru (smtpng1.i.mail.ru [94.100.181.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 9874A6ECDE for ; Fri, 17 Dec 2021 03:25:36 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 9874A6ECDE Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1my143-0007At-UD; Fri, 17 Dec 2021 03:25:36 +0300 To: tarantool-patches@dev.tarantool.org, olegrok@tarantool.org Date: Fri, 17 Dec 2021 01:25:30 +0100 Message-Id: X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: EEAE043A70213CC8 X-77F55803: 4F1203BC0FB41BD9B5397E24C93BDA67728EE92B76C34EA93F43F472DF252270182A05F538085040835A1A98766971EAD65A2FC7FBBAE08FB5DDF23A5150CE63603F745031572C0A X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7CA8E915ACC910FBDEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F790063737452AF4BFD067BF8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D859390F37B68873353EDA1BB3F0EE1162117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC3A703B70628EAD7BA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F44604297287769387670735201E561CDFBCA1751F6FD1C55BDD38FC3FD2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B62CFFCC7B69C47339089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C4C7A0BC55FA0FE5FC3163393FEE54070F9D80E07F76ECF39A2384B59795FECCA3B1881A6453793CE9C32612AADDFBE061C61BE10805914D3804EBA3D8E7E5B87ABF8C51168CD8EBDBD215BE4436AF2686DC48ACC2A39D04F89CDFB48F4795C241BDAD6C7F3747799A X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34F38194B2C99DC1284674CC087C1753FC3A8CC929E909AB3928E41300C8A63FA3E106C882D02E08831D7E09C32AA3244C6BCE94442F26A6F1F18B344260F67950725D5B54B2FE4575729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojieEIankJUzoDWnpVwRHrYQ== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D7DB0AA48D3F871F3EC9A6286D2FBA3113841015FED1DE5223CC9A89AB576DD93FB559BB5D741EB963CF37A108A312F5C27E8A8C3839CE0E25FEEDEB644C299C0ED14614B50AE0675 X-Mras: Ok Subject: [Tarantool-patches] [PATCH vshard 4/5] error: introduce from_string X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Vladislav Shpilevoy via Tarantool-patches Reply-To: Vladislav Shpilevoy Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" vshard.storage.call() and most of the other vshard.storage.* functions now raise an exception STORAGE_IS_DISABLED when the storage is disabled. The router wants to catch it to handle in a special way. But unfortunately, - error(obj) in a Lua function is wrapped into LuajitError. 'obj' is saved into 'message' using its __tostring meta-method. - It is not possible to create your own error type in a sane way. These 2 facts mean that the router needs to be able to extract the original error from LuajitError's message. In vshard errors are serialized into json, so a valid vshard error, such as STORAGE_IS_DISABLED, can be extracted from LuajitError's message if it wasn't truncated due to being too long. For this particular error it won't happen. The patch introduces new method vshard.error.from_string() to perform this extraction for its further usage in router. Part of #298 --- test/unit/error.result | 18 ++++++++++++++++++ test/unit/error.test.lua | 6 ++++++ vshard/error.lua | 19 +++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/test/unit/error.result b/test/unit/error.result index bb4e0cc..258b14f 100644 --- a/test/unit/error.result +++ b/test/unit/error.result @@ -48,6 +48,24 @@ test_run:grep_log('default', '"reason":"reason","code":11,"type":"ShardingError" --- - '"reason":"reason","code":11,"type":"ShardingError"' ... +e = lerror.vshard(lerror.code.STORAGE_IS_DISABLED, 'any reason') +--- +... +e = lerror.from_string(tostring(e)) +--- +... +assert(e.code == lerror.code.STORAGE_IS_DISABLED) +--- +- true +... +assert(e.type == 'ShardingError') +--- +- true +... +assert(e.message == 'Storage is disabled: any reason') +--- +- true +... -- -- Part of gh-100: check `error.vshard`. -- diff --git a/test/unit/error.test.lua b/test/unit/error.test.lua index 0a51d33..5e669b6 100644 --- a/test/unit/error.test.lua +++ b/test/unit/error.test.lua @@ -19,6 +19,12 @@ log = require('log') log.info('Log error: %s', vshard_error) test_run:grep_log('default', '"reason":"reason","code":11,"type":"ShardingError"') +e = lerror.vshard(lerror.code.STORAGE_IS_DISABLED, 'any reason') +e = lerror.from_string(tostring(e)) +assert(e.code == lerror.code.STORAGE_IS_DISABLED) +assert(e.type == 'ShardingError') +assert(e.message == 'Storage is disabled: any reason') + -- -- Part of gh-100: check `error.vshard`. -- diff --git a/vshard/error.lua b/vshard/error.lua index 2b97eae..96c4bdd 100644 --- a/vshard/error.lua +++ b/vshard/error.lua @@ -244,6 +244,24 @@ local function make_error(e) end end +-- +-- Restore an error object from its string serialization. +-- +local function from_string(str) + -- Error objects in VShard are stringified into json. Hence can restore also + -- as json. The only problem is that the json might be truncated if it was + -- stored in an error message of a real error object. It is not very + -- reliable. + local ok, res = pcall(json.decode, str) + if not ok then + return nil + end + if type(res) ~= 'table' or type(res.type) ~= 'string' or + type(res.code) ~= 'number' or type(res.message) ~= 'string' then + return nil + end + return make_error(res) +end local function make_alert(code, ...) local format = error_message_template[code] @@ -266,6 +284,7 @@ return { box = box_error, vshard = vshard_error, make = make_error, + from_string = from_string, alert = make_alert, timeout = make_timeout, } -- 2.24.3 (Apple Git-128)