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 59A6E6E1C9; Sat, 6 Nov 2021 03:01:23 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 59A6E6E1C9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1636156883; bh=ptGLzBrhXwbA01QbFK6nCImtz6JkrEHDW+aqlFWwDRQ=; 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=kiQvg9Q/qy9UxA4NPryoILGwZSZyU5fhNMarmGw6yixHLlLEGyw1b4GR+OhBWLqiZ hCmX9NJqDYYF7hi0ZhTiwDFwBU7+G3ezXL2gk3ae9AwvxYIdnAYdkz6IUfVY4oHesX eGJz2cc9jrAVT5Z7kNe+ZYcZlyN8QJJQGzUNyCt0= Received: from smtpng3.i.mail.ru (smtpng3.i.mail.ru [94.100.177.149]) (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 868696F852 for ; Sat, 6 Nov 2021 02:56:54 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 868696F852 Received: by smtpng3.m.smailru.net with esmtpa (envelope-from ) id 1mj94n-0006s6-BZ; Sat, 06 Nov 2021 02:56:53 +0300 To: tarantool-patches@dev.tarantool.org, sergepetrenko@tarantool.org, vdavydov@tarantool.org Date: Sat, 6 Nov 2021 00:56:40 +0100 Message-Id: <99799340f06652929897e363e42a5227d12277a1.1636156453.git.v.shpilevoy@tarantool.org> 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: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9F9D976862A30D4FE71E4F4AD1F019DBD166E326202659E54182A05F53808504022F2005FDCAF1E3ED23A116EAA461C8499655A02222ED5C14841937C798229E2 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE78044725E6B9C7F2CEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006376EC5B14D896A2D978638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8948091C805BB4B522F16B1BD1625D042117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8BAA867293B0326636D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B62CFFCC7B69C47339089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458834459D11680B505F04FA6A337665710073A35CBA27B0265 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8183A4AFAF3EA6BDC44C234C8B12C006B7A47BC8DA519486CCCE420D02A10CA6E128AB1989007AAB65FB1881A6453793CE9C32612AADDFBE061C801D989C91DAA47C32612AADDFBE0610F49EF363AAD6E829510FB958DCE06DB6ED91DBE5ABE359A7835AC3E74CD595B23D4379F09C64C7393EDB24507CE13387DFF0A840B692CF8 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34ADE558D2B396DA7CA2B0A8C14E5B8A98C131BDF02E67306B44FF987C5D2488BEDF75E3AB884F07E61D7E09C32AA3244C79606B26471ABF043FC5B3803DBFFD8163871F383B54D9B3FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj75A/c671lqWEXzbY8E15zQ== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5DC9C4CD4CF15DB0DD74E2B4CD7D1305223841015FED1DE5223CC9A89AB576DD93FB559BB5D741EB963CF37A108A312F5C27E8A8C3839CE0E267EA787935ED9F1B X-Mras: Ok Subject: [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: 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. --- 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: + } + ``` \ 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') + 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. +-- +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)