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 9E0CB6FFBB; Fri, 12 Nov 2021 02:55:37 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 9E0CB6FFBB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1636674937; bh=52++qq0e0+pZ5puYxDY9RjorzOsHMITknWqWOkEZX8A=; 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=oT/4xlBMMF0Zj0Yzx3fyAaT/Xw/yFALOpRYZnNFhFvHlYQOnm/g/dCyxOSjLhfLCR 69IeXcNYkWjKxO8CXmCQ+ODAxsVRqimfljHjwAesZ3EtCiPBPS2h0CE318dHyViSGi CpWuy31020ujRZvzyqqt28N5yQCMvtSwp0o5J0N4= 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 0A8A870356 for ; Fri, 12 Nov 2021 02:54:34 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 0A8A870356 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mlJtp-0003tY-BX; Fri, 12 Nov 2021 02:54:33 +0300 To: tarantool-patches@dev.tarantool.org, sergepetrenko@tarantool.org Date: Fri, 12 Nov 2021 00:54:21 +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: 4F1203BC0FB41BD927BBD05C2915ABC3EA6F6B4670DDA5A1324597B560173557182A05F5380850404C228DA9ACA6FE27D3D0326860FAAE26E1ECA49F7C300E1EA005F777109EFBA754A90CAE50B2239C X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7850A3F981F25E362EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F790063770398A047C76876C8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D894BE2F8B1FB8F7736EEAB9E0E02CB0A2117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8B11A27A05CE189FECA471835C12D1D977C4224003CC8364762BB6847A3DEAEFB0F43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B68655334FD4449CB9ECD01F8117BC8BEAAAE862A0553A39223F8577A6DFFEA7CE31A2885C41F97C443847C11F186F3C59DAA53EE0834AAEE X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458834459D11680B50537BF44C8E1EC058A3101C983B23679DA X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8183A4AFAF3EA6BDC44C234C8B12C006B7A3C0B852DA0DA35D186290086C269A998854DC430D5F2C86AB1881A6453793CE9C32612AADDFBE061C801D989C91DAA47C32612AADDFBE0610F49EF363AAD6E829510FB958DCE06DB6ED91DBE5ABE359AC8952F428387DEC01F9270390973918F93EDB24507CE13387DFF0A840B692CF8 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34CA9B3B5DE5D383E6D8462C4FC27B25B193F5E2E7D322EE0E43F389E19DE4A000C50F3C8F976270671D7E09C32AA3244CB1321472D0A89E3F0C137A13F706D94B30452B15D76AEC14729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj4t8MBgWr8bK4Uolfo2h24w== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5DBF1FC16C89A39BDA6E391163005E35C53841015FED1DE5223CC9A89AB576DD93FB559BB5D741EB963CF37A108A312F5C27E8A8C3839CE0E267EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v2 10/11] 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: Vladislav Shpilevoy via Tarantool-patches Reply-To: Vladislav Shpilevoy Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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 @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. --- .../unreleased/gh-5568-readonly-reason.md | 4 + src/box/box.cc | 54 +++- .../gh_5568_read_only_reason_test.lua | 285 ++++++++++++++++++ 3 files changed, 335 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..f3a2db986 --- /dev/null +++ b/changelogs/unreleased/gh-5568-readonly-reason.md @@ -0,0 +1,4 @@ +## feature/core + +* Error objects with the code `box.error.READONLY` now have additional fields + explaining why the error happened (gh-5568). diff --git a/src/box/box.cc b/src/box/box.cc index 1ed1ce3f8..323982969 100644 --- a/src/box/box.cc +++ b/src/box/box.cc @@ -177,16 +177,54 @@ 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_set_str(e, "reason", "election"); + error_set_str(e, "state", raft_state_str(raft->state)); + error_set_uint(e, "term", raft->volatile_term); + uint32_t id = raft->leader; + if (id != REPLICA_ID_NIL) { + error_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_set_uuid(e, "leader_uuid", &r->uuid); + } + } else if (txn_limbo_is_ro(&txn_limbo)) { + error_set_str(e, "reason", "synchro"); + uint32_t id = txn_limbo.owner_id; + error_set_uint(e, "queue_owner_id", id); + error_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_set_uuid(e, "queue_owner_uuid", &r->uuid); + } else { + error_set_str(e, "reason", "state"); + if (is_ro) + error_set_str(e, "state", "read_only"); + else if (is_orphan) + error_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..8effa69e8 --- /dev/null +++ b/test/replication-luatest/gh_5568_read_only_reason_test.lua @@ -0,0 +1,285 @@ +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() + 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.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.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 not its UUID. +-- +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 -- 2.24.3 (Apple Git-128)