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 B73386F3E5; Mon, 8 Nov 2021 18:18:36 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org B73386F3E5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1636384716; bh=oxSacauMkEl/J7wE9KtrBi2Bp/8UtWAZ30NRAoJbGQM=; h=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=OJ2goh7/JrP7R8gc8XL7bxDLyHGXHsg+f4ABg4FNjzzkodOYawLZ+bUT1bf02nvsx GWf2QN6KctdcW4j1k1yuTGxy1p6neiA3RHM21mjbgVJXcGINS/TQG+r5rElqcIeE7/ CQ/Y9Rp5wJfm9GSXbJL+bCurZgGTV18h2D1Xft80= Received: from smtp37.i.mail.ru (smtp37.i.mail.ru [94.100.177.97]) (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 3D8426F3E5 for ; Mon, 8 Nov 2021 18:18:35 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 3D8426F3E5 Received: by smtp37.i.mail.ru with esmtpa (envelope-from ) id 1mk6Pq-0001pY-5Q; Mon, 08 Nov 2021 18:18:34 +0300 Message-ID: <2af7c607-01a1-7060-4ecd-fe9f526c8c07@tarantool.org> Date: Mon, 8 Nov 2021 18:18:33 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.2.1 To: Vladislav Shpilevoy , tarantool-patches@dev.tarantool.org, vdavydov@tarantool.org References: <99799340f06652929897e363e42a5227d12277a1.1636156453.git.v.shpilevoy@tarantool.org> Content-Language: en-GB In-Reply-To: <99799340f06652929897e363e42a5227d12277a1.1636156453.git.v.shpilevoy@tarantool.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9731B3922EC063979318FB517B78237B61CEAD5F2B9D8678E00894C459B0CD1B9AD1B637983158D7FE7107D62BD54CA57AA1D1AB37C96470EACCB5262BB541CA2 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE76574C3D62D66A535EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F790063707C4856229E8E7E48638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D850E1A41282198876E391075C0FE01141117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8BAA867293B0326636D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EE7B96B19DC40933214AAC223A686B1DECD8FC6C240DEA7642DBF02ECDB25306B2B78CF848AE20165D0A6AB1C7CE11FEE362B3BD3CC35DA588C0837EA9F3D19764C4224003CC836476EA7A3FFF5B025636E2021AF6380DFAD1A18204E546F3947CB11811A4A51E3B096D1867E19FE1407959CC434672EE6371089D37D7C0E48F6C8AA50765F79006373568875668442E59EFF80C71ABB335746BA297DBC24807EABDAD6C7F3747799A X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458834459D11680B5050C315A302238E54BFF7AE58A04E470D6 X-C1DE0DAB: 0D63561A33F958A5FCAC99286925900CD072160668AB340BC505230F3AF8A4A6D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA753753CEE10E4ED4A7410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D348F9E8EBB46231487EF0FF183002DF65A0A79C76F0140F51EB095761298DA9E28FE48314B91BC84E01D7E09C32AA3244C76E74FA2BD07700BDE7CAE2AEBBCD3893FD9C8CA1B0515E0FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj9/1IdNVT2RZeJFprs9xg1Q== X-Mailru-Sender: 11C2EC085EDE56FA38FD4C59F7EFE40728AE62873301A4B4DCEB7C79B2CD08971166E8976F3FAEB26BB2E709EA627F343C7DDD459B58856F0E45BC603594F5A135B915D4279FF0579437F6177E88F7363CDA0F3B3F5B9367 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH 9/9] box: enrich ER_READONLY with new details 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: Serge Petrenko via Tarantool-patches Reply-To: Serge Petrenko Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 06.11.2021 02:56, Vladislav Shpilevoy пишет: > ER_READONLY used not to have any details about the exact reason > why the instance is read-only. The patch changes that by adding > new fields into the error which explain why the error happened and > even help to avoid it for next requests. > > Now from the error object can tell whether it was raised because > of box.cfg.read_only = true, or the instance is an orphan, or it > has election enabled but is not a leader, or the transaction limbo > belongs to another instance. > > The alternative to ClientError alteration via error_payload was > not to touch struct error and introduce a new error type > specifically for ER_READONLY via a new C++ class like > ReadOnlyError. But it had drawbacks: > > - There may be clients who expect ER_READONLY to have ClientError > type. For instance, they check err.code == ER_READONLY only for > err.type == 'ClientError'; > > - Having to introduce a new C++ class each time when want to add a > new field into an error has to end. Rather sooner than later. > > Closes #5568 Thanks for the patch! Looks nice, generally, with 3 minor comments. > > @TarantoolBot document > Title: box.error.READONLY new attributes > > Users could see the error code as `box.error.READONLY` in `.code` > field of an error object. The error didn't have any other > attributes except common ones like 'type'. > > Now from the `box.error.READONLY` error users can see why it > happened. The reasons can be the following: > > * The instance has `box.cfg.read_only = true`. Then the `READONLY` > error has at least these fields: > ``` > tarantool> err:unpack() > --- > - state: read_only > reason: state > code: 7 > type: ClientError > ... > ``` > > * The instance is an orphan. It enters that state if number of > connected replicas is < `box.cfg.replication_connect_quorum`. Then > `READONLY` error has at least these fields: > ``` > tarantool> err:unpack() > --- > - state: orphan > reason: state > code: 7 > type: ClientError > ... > ``` > > * The synchro queue has an owner which is not the given instance. > It usually happens if synchronous replication is used and there is > another instance who called `box.ctl.promote()`. Then `READONLY` > error has at least these fields: > ``` > tarantool> err:unpack() > --- > - queue_owner_id: > queue_owner_uuid: > reason: synchro > term: > code: 7 > type: ClientError > ... > ``` > Note than `queue_owner_uuid` sometimes might be not present. > > * The instance has `box.cfg.election_mode` not `off` and it is not > a leader. Then `READONLY` error has at least these fields: > ``` > tarantool> err:unpack() > --- > - state: > leader_id: > leader_uuid: > reason: election > term: > code: 7 > type: ClientError > ... > ``` > `leader_id` and `leader_uuid` might be absent if the leader is not > known. For example, an election is still in progress. Note, than > `leader_uuid` sometimes might be not present even if `leader_id` > is. > > If multiple reasons are true at the same time, then only one is > returned in order reversed from how they are listed above. IOW, > election, synchro, box.cfg.read_only, orphan. > --- > There are 2 test groups because the 'unsafe' one sometimes crashes if try to > make a cleanup. Instances of the second test group can only be recreated. > > .../unreleased/gh-5568-readonly-reason.md | 24 ++ > src/box/box.cc | 56 +++- > .../gh_5568_read_only_reason_test.lua | 287 ++++++++++++++++++ > 3 files changed, 359 insertions(+), 8 deletions(-) > create mode 100644 changelogs/unreleased/gh-5568-readonly-reason.md > create mode 100644 test/replication-luatest/gh_5568_read_only_reason_test.lua > > diff --git a/changelogs/unreleased/gh-5568-readonly-reason.md b/changelogs/unreleased/gh-5568-readonly-reason.md > new file mode 100644 > index 000000000..f90b53bdf > --- /dev/null > +++ b/changelogs/unreleased/gh-5568-readonly-reason.md > @@ -0,0 +1,24 @@ > +## feature/core > + > +* Error objects with the code `box.error.READONLY` now have additional fields > + explaining why the error happened (gh-5568). > + > + For instance, if the error was due to `box.cfg{read_only = true}`, then the > + error will have fields: > + ``` > + { > + reason = 'state', > + state = 'read_only' > + } > + ``` > + Or if it was due to not being a leader with `box.cfg{election_mode = ...}` set > + not to `off`, then: > + ``` > + { > + state: , > + leader_id: , > + leader_uuid: , > + reason: election, > + term: > + } > + ``` 1. Let's duplicate the additional error information in the error message.    If it's hard to log every field, let's at least encode reason and state,    so that the user knows what's wrong by simply looking at the error message. > \ No newline at end of file > diff --git a/src/box/box.cc b/src/box/box.cc > index 1ed1ce3f8..236c02533 100644 > --- a/src/box/box.cc > +++ b/src/box/box.cc > @@ -177,16 +177,56 @@ box_update_ro_summary(void) > static int > box_check_writable(void) > { > - if (is_ro_summary) { > + if (!is_ro_summary) > + return 0; > + struct error *e = diag_set(ClientError, ER_READONLY); > + struct raft *raft = box_raft(); > + /* > + * In case of multiple reasons at the same time only one is reported. > + * But the order is important. For example, if the instance has election > + * enabled, for the client it is better to see that it is a 'follower' > + * and who is the leader than just see cfg 'read_only' is true. > + */ > + if (raft_is_ro(raft)) { > + error_field_set_str(e, "reason", "election"); > + error_field_set_str(e, "state", raft_state_str(raft->state)); > + error_field_set_uint(e, "term", raft->volatile_term); > + uint32_t id = raft->leader; > + if (id != REPLICA_ID_NIL) { > + error_field_set_uint(e, "leader_id", id); > + struct replica *r = replica_by_id(id); > + /* > + * XXX: when the leader is dropped from _cluster, it > + * is not reported to Raft. > + */ > + if (r != NULL) { > + error_field_set_uuid(e, "leader_uuid", > + &r->uuid); > + } > + } > + } else if (txn_limbo_is_ro(&txn_limbo)) { > + error_field_set_str(e, "reason", "synchro"); > + uint32_t id = txn_limbo.owner_id; > + error_field_set_uint(e, "queue_owner_id", id); > + error_field_set_uint(e, "term", raft->volatile_term); > + struct replica *r = replica_by_id(id); > /* > - * XXX: return a special error when the node is not a leader to > - * reroute to the leader node. > + * XXX: when an instance is deleted from _cluster, its limbo's > + * ownership is not cleared. > */ > - diag_set(ClientError, ER_READONLY); > - diag_log(); > - return -1; > - } > - return 0; > + if (r != NULL) > + error_field_set_uuid(e, "queue_owner_uuid", &r->uuid); > + } else { > + error_field_set_str(e, "reason", "state"); > + if (is_ro) > + error_field_set_str(e, "state", "read_only"); > + else if (is_orphan) > + error_field_set_str(e, "state", "orphan"); > + else > + assert(false); > + } > + diag_log(); > + return -1; > } > > static void > diff --git a/test/replication-luatest/gh_5568_read_only_reason_test.lua b/test/replication-luatest/gh_5568_read_only_reason_test.lua > new file mode 100644 > index 000000000..d4f7ff957 > --- /dev/null > +++ b/test/replication-luatest/gh_5568_read_only_reason_test.lua > @@ -0,0 +1,287 @@ > +local t = require('luatest') > +local cluster = require('test.luatest_helpers.cluster') > +local helpers = require('test.luatest_helpers') > + > +-- > +-- gh-5568: ER_READONLY needs to carry additional info explaining the reason > +-- why the error happened. It should help the clients to do better error logging > +-- and to use the provided info to find the actual leader if the reason is in > +-- being not a leader. > +-- > + > +-- > +-- Make functions which will create and destroy a cluster. Usage of the closures > +-- is a workaround for luatest hooks not accepting their group as a parameter. > +-- > +local function make_create_cluster(g) return function(param) > + g.cluster = cluster:new({}) > + local master_uri = helpers.instance_uri('master') > + local replica_uri = helpers.instance_uri('replica') > + local replication = {master_uri, replica_uri} > + local box_cfg = { > + listen = master_uri, > + replication = replication, > + -- To speed up new term when try to elect a new leader. > + replication_timeout = 0.1, > + } > + g.master = g.cluster:build_server({alias = 'master', box_cfg = box_cfg}) > + > + box_cfg.listen = replica_uri > + g.replica = g.cluster:build_server({alias = 'replica', box_cfg = box_cfg}) > + > + g.cluster:add_server(g.master) > + g.cluster:add_server(g.replica) > + g.cluster:start() > +end end > + > +local function make_destroy_cluster(g) return function() > + g.cluster:drop() > +end end > + > +-- > +-- This group's test cases leave instances in a valid state after which they can > +-- be reused. > +-- > +local g = t.group('gh-5568-read-only-reason1') > + > +g.before_all(make_create_cluster(g)) > +g.after_all(make_destroy_cluster(g)) > + > +-- > +-- Read-only because of box.cfg{read_only = true}. > +-- > +g.test_read_only_reason_cfg = function(g) > + local ok, err = g.master:exec(function() > + box.cfg{read_only = true} > + local ok, err = pcall(box.schema.create_space, 'test') > + return ok, err:unpack() > + end) > + t.assert(not ok, 'fail ddl') > + t.assert_covers(err, { > + reason = 'state', > + state = 'read_only', > + code = box.error.READONLY, > + type = 'ClientError' > + }, 'reason is read_only') > + > + -- Cleanup. > + g.master:exec(function() > + box.cfg{read_only = false} > + end) > +end > + > +-- > +-- Read-only because is an orphan. > +-- > +g.test_read_only_reason_orphan = function(g) > + local fake_uri = helpers.instance_uri('fake') > + local old_timeout, ok, err = g.master:exec(function(fake_uri) > + -- Make connect-quorum impossible to satisfy using a fake instance. > + local old_timeout = box.cfg.replication_connect_timeout > + local repl = table.copy(box.cfg.replication) > + table.insert(repl, fake_uri) > + box.cfg{ > + replication = repl, > + replication_connect_timeout = 0.001, > + } > + local ok, err = pcall(box.schema.create_space, 'test') > + return old_timeout, ok, err:unpack() > + end, {fake_uri}) > + t.assert(not ok, 'fail ddl') > + t.assert_covers(err, { > + reason = 'state', > + state = 'orphan', > + code = box.error.READONLY, > + type = 'ClientError' > + }, 'reason is orphan') > + > + -- Cleanup. > + g.master:exec(function(old_timeout) > + local repl = table.copy(box.cfg.replication) > + table.remove(repl) > + box.cfg{ > + replication = repl, > + replication_connect_timeout = old_timeout, > + } > + end, {old_timeout}) > +end > + > +-- > +-- Read-only because is not a leader. Does not know the leader. > +-- > +g.test_read_only_reason_election_no_leader = function(g) > + local ok, err = g.master:exec(function() > + box.cfg{election_mode = 'voter'} > + local ok, err = pcall(box.schema.create_space, 'test') > + return ok, err:unpack() > + end) > + t.assert(not ok, 'fail ddl') > + t.assert(err.term, 'has term') > + t.assert_covers(err, { > + reason = 'election', > + state = 'follower', > + code = box.error.READONLY, > + type = 'ClientError' > + }, 'reason is election, no leader') > + t.assert(not err.leader_id, 'no leader id') > + t.assert(not err.leader_uuid, 'no leader uuid') > + > + -- Cleanup. > + g.master:exec(function() > + box.cfg{election_mode = 'off'} > + end) > +end > + > +-- > +-- Read-only because is not a leader. Knows the leader. > +-- > +g.test_read_only_reason_election_has_leader = function(g) > + g.master:exec(function() > + box.cfg{election_mode = 'candidate'} > + end) > + g.master:wait_election_leader() > + g.replica:wait_election_leader_found() > + local ok, err = g.replica:exec(function() > + box.cfg{election_mode = 'voter'} > + local ok, err = pcall(box.schema.create_space, 'test') > + return ok, err:unpack() > + end) > + t.assert(not ok, 'fail ddl') > + t.assert(err.term, 'has term') > + t.assert_covers(err, { > + reason = 'election', > + state = 'follower', > + leader_id = g.master:instance_id(), > + leader_uuid = g.master:instance_uuid(), > + code = box.error.READONLY, > + type = 'ClientError' > + }, 'reason is election, has leader') > + > + -- Cleanup. > + g.master:exec(function() > + box.cfg{election_mode = 'off'} > + box.ctl.demote() > + end) > + g.replica:exec(function() > + box.cfg{election_mode = 'off'} > + end) > +end > + > +-- > +-- Read-only because does not own the limbo. Knows the owner. > +-- > +g.test_read_only_reason_synchro = function(g) > + g.master:exec(function() > + box.schema.create_space('test', {is_sync = true}):create_index('pk') 2. The space creation isn't necessary for this test. > + box.ctl.promote() > + end) > + > + t.helpers.retrying({}, function() > + assert(g.replica:exec(function() > + return box.info.synchro.queue.owner ~= 0 > + end)) > + end) > + > + local ok, err = g.replica:exec(function() > + local ok, err = pcall(box.schema.create_space, 'test2') > + return ok, err:unpack() > + end) > + t.assert(not ok, 'fail ddl') > + t.assert(err.term, 'has term') > + t.assert_covers(err, { > + reason = 'synchro', > + queue_owner_id = g.master:instance_id(), > + queue_owner_uuid = g.master:instance_uuid(), > + code = box.error.READONLY, > + type = 'ClientError' > + }, 'reason is synchro, has owner') > + > + -- Cleanup. > + g.master:exec(function() > + box.space.test:drop() > + box.ctl.demote() > + end) > +end > + > +-- > +-- This group's test cases leave instances in an invalid state after which they > +-- should be re-created. > +-- > +g = t.group('gh-5568-read-only-reason2') > + > +g.before_each(make_create_cluster(g)) > +g.after_each(make_destroy_cluster(g)) > + > +-- > +-- Read-only because is not a leader. Knows the leader, but not its UUID. > +-- > +g.test_read_only_reason_election_has_leader_no_uuid = function(g) > + g.replica:exec(function() > + box.cfg{election_mode = 'voter'} > + end) > + g.master:exec(function() > + box.cfg{election_mode = 'candidate'} > + end) > + g.master:wait_election_leader() > + g.replica:wait_election_leader_found() > + local leader_id = g.master:instance_id() > + > + g.master:exec(function() > + box.space._cluster:run_triggers(false) > + box.space._cluster:delete{box.info.id} > + end) > + > + t.helpers.retrying({}, function() > + assert(g.replica:exec(function(leader_id) > + return box.space._cluster:get{leader_id} == nil > + end, {leader_id})) > + end) > + > + local ok, err = g.replica:exec(function() > + local ok, err = pcall(box.schema.create_space, 'test') > + return ok, err:unpack() > + end) > + t.assert(not ok, 'fail ddl') > + t.assert(err.term, 'has term') > + t.assert(not err.leader_uuid, 'has no leader uuid') > + t.assert_covers(err, { > + reason = 'election', > + state = 'follower', > + leader_id = leader_id, > + code = box.error.READONLY, > + type = 'ClientError' > + }, 'reason is election, has leader but no uuid') > +end > + > +-- > +-- Read-only because does not own the limbo. Knows the owner, but now its UUID. > +-- 3. Typo: now -> not. > +g.test_read_only_reason_synchro_no_uuid = function(g) > + g.master:exec(function() > + box.ctl.promote() > + box.space._cluster:run_triggers(false) > + box.space._cluster:delete{box.info.id} > + end) > + > + local leader_id = g.master:instance_id() > + t.helpers.retrying({}, function() > + assert(g.replica:exec(function(leader_id) > + return box.info.synchro.queue.owner ~= 0 and > + box.space._cluster:get{leader_id} == nil > + end, {leader_id})) > + end) > + > + local ok, err = g.replica:exec(function() > + local ok, err = pcall(box.schema.create_space, 'test') > + return ok, err:unpack() > + end) > + t.assert(not ok, 'fail ddl') > + t.assert(err.term, 'has term') > + t.assert(not err.queue_owner_uuid) > + t.assert_covers(err, { > + reason = 'synchro', > + queue_owner_id = leader_id, > + code = box.error.READONLY, > + type = 'ClientError' > + }, 'reason is synchro, has owner but no uuid') > +end -- Serge Petrenko