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 0BDBD6EC40; Tue, 29 Jun 2021 01:15:16 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 0BDBD6EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1624918516; bh=i9vvqwY6AaubP3kptxMtmRvlbRq8Fz4hDrocRiyc1vM=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=VuQV8SFBdob1co3oZEwcVUisx5LLxwb6WRnoTAmDwL+vqZujgeAOb6kAu+Nembdg8 xURz1Nn+CJF1qO7CIVDk65UZKiTruyEGl5EZgSuOal1PMLxklKw7cowFzrs762J8wJ vfaJNWxbN1wJm/mnH156eaJrL21O5cj0hBqUqwd8= Received: from smtp61.i.mail.ru (smtp61.i.mail.ru [217.69.128.41]) (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 B1B3C6EC45 for ; Tue, 29 Jun 2021 01:13:12 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org B1B3C6EC45 Received: by smtp61.i.mail.ru with esmtpa (envelope-from ) id 1lxzV9-0007oC-RH; Tue, 29 Jun 2021 01:13:12 +0300 To: v.shpilevoy@tarantool.org, gorcunov@gmail.com Date: Tue, 29 Jun 2021 01:12:50 +0300 Message-Id: <46c167f8c9bfe0e35370b0cbf10505d9d4a888ba.1624918077.git.sergepetrenko@tarantool.org> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-4EC0790: 10 X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD954DFF1DC42D673FB8EEAA58EF109ED2DCA3B880632F394B8182A05F53808504058CE5926ED1B63398C548A69DDB6D4D1D7687A452332555E17B1BAFB7BCF0940 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE76256B078082242EEEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006378B49D47CE295E66E8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8FFF6FC4BE78416632E1F6970128641C3117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC2EE5AD8F952D28FBA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD186FD1C55BDD38FC3FD2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B60A62CEF541B197C8089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CD87436A60A035F40C410FF2B0FBD3B8A74D9035282B8B9859C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF92B0BE0DA6BB795D699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D340CB2836B823694493552FB188AD1D90232ADF2D9CA9DDCAE7333CE8054BFBDACDBB6F56E7B528C841D7E09C32AA3244C81F41DF848BA6A1277186750B69294843A92A9747B6CC886927AC6DF5659F194 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojbL9S8ysBdXjjQb0ILB/OApPwEuO8bhx9 X-Mailru-Sender: 3B9A0136629DC9125D61937A2360A446C3FEE19542AA4C0E502030DF52F61EF5638C30DFD0F9E248424AE0EB1F3D1D21E2978F233C3FAE6EE63DB1732555E4A8EE80603BA4A5B0BC112434F685709FCF0DA7A0AF5A3A8387 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v3 04/12] box: make promote always bump the term 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 Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" When called without elections, promote resulted in multiple PROMOTE entries for the same term. This is not right, because all the promotions for the same term except the first one would be ignored as already seen. Part-of #6034 --- src/box/box.cc | 13 ++++--- src/box/raft.c | 36 ++++++++++++++++++ src/box/raft.h | 4 ++ .../gh-4114-local-space-replication.result | 7 ++-- .../gh-4114-local-space-replication.test.lua | 4 +- .../gh-6034-promote-bump-term.result | 37 +++++++++++++++++++ .../gh-6034-promote-bump-term.test.lua | 16 ++++++++ test/replication/suite.cfg | 1 + 8 files changed, 107 insertions(+), 11 deletions(-) create mode 100644 test/replication/gh-6034-promote-bump-term.result create mode 100644 test/replication/gh-6034-promote-bump-term.test.lua diff --git a/src/box/box.cc b/src/box/box.cc index 6a0950f44..ce37b307d 100644 --- a/src/box/box.cc +++ b/src/box/box.cc @@ -1687,16 +1687,19 @@ box_promote(void) rc = -1; } else { promote: - /* We cannot possibly get here in a volatile state. */ - assert(box_raft()->volatile_term == box_raft()->term); - txn_limbo_write_promote(&txn_limbo, wait_lsn, - box_raft()->term); + if (try_wait) { + raft_new_term(box_raft()); + if (box_raft_wait_persisted() < 0) + return -1; + } + uint64_t term = box_raft()->term; + txn_limbo_write_promote(&txn_limbo, wait_lsn, term); struct synchro_request req = { .type = IPROTO_PROMOTE, .replica_id = former_leader_id, .origin_id = instance_id, .lsn = wait_lsn, - .term = box_raft()->term, + .term = term, }; txn_limbo_process(&txn_limbo, &req); assert(txn_limbo_is_empty(&txn_limbo)); diff --git a/src/box/raft.c b/src/box/raft.c index 7f787c0c5..17caf6f54 100644 --- a/src/box/raft.c +++ b/src/box/raft.c @@ -354,6 +354,42 @@ box_raft_wait_leader_found(void) return 0; } +struct raft_wait_persisted_data { + struct fiber *waiter; + uint64_t term; +}; + +static int +box_raft_wait_persisted_f(struct trigger *trig, void *event) +{ + struct raft *raft = event; + struct raft_wait_persisted_data *data = trig->data; + if (raft->term >= data->term) + fiber_wakeup(data->waiter); + return 0; +} + +int +box_raft_wait_persisted(void) +{ + if (box_raft()->term == box_raft()->volatile_term) + return 0; + struct raft_wait_persisted_data data = { + .waiter = fiber(), + .term = box_raft()->volatile_term, + }; + struct trigger trig; + trigger_create(&trig, box_raft_wait_persisted_f, &data, NULL); + raft_on_update(box_raft(), &trig); + fiber_yield(); + trigger_clear(&trig); + if (fiber_is_cancelled()) { + diag_set(FiberIsCancelled); + return -1; + } + return 0; +} + void box_raft_init(void) { diff --git a/src/box/raft.h b/src/box/raft.h index 6b6136510..6e27b098f 100644 --- a/src/box/raft.h +++ b/src/box/raft.h @@ -101,6 +101,10 @@ box_raft_process(struct raft_request *req, uint32_t source); int box_raft_wait_leader_found(); +/** Block this fiber until the current volatile term is persisted. */ +int +box_raft_wait_persisted(void); + void box_raft_init(void); diff --git a/test/replication/gh-4114-local-space-replication.result b/test/replication/gh-4114-local-space-replication.result index 9b63a4b99..e71eb60a8 100644 --- a/test/replication/gh-4114-local-space-replication.result +++ b/test/replication/gh-4114-local-space-replication.result @@ -45,9 +45,8 @@ test_run:cmd('switch replica') | --- | - true | ... -box.info.vclock[0] +a = box.info.vclock[0] or 0 | --- - | - null | ... box.cfg{checkpoint_count=1} | --- @@ -77,9 +76,9 @@ box.space.test:insert{3} | - [3] | ... -box.info.vclock[0] +assert(box.info.vclock[0] == a + 3) | --- - | - 3 + | - true | ... test_run:cmd('switch default') diff --git a/test/replication/gh-4114-local-space-replication.test.lua b/test/replication/gh-4114-local-space-replication.test.lua index c18fb3b10..65fef3bf6 100644 --- a/test/replication/gh-4114-local-space-replication.test.lua +++ b/test/replication/gh-4114-local-space-replication.test.lua @@ -18,7 +18,7 @@ for i = 1,10 do box.space.test:insert{i} end box.info.vclock[0] == a + 10 or box.info.vclock[0] - a test_run:cmd('switch replica') -box.info.vclock[0] +a = box.info.vclock[0] or 0 box.cfg{checkpoint_count=1} box.space.test:select{} box.space.test:insert{1} @@ -27,7 +27,7 @@ box.space.test:insert{2} box.snapshot() box.space.test:insert{3} -box.info.vclock[0] +assert(box.info.vclock[0] == a + 3) test_run:cmd('switch default') diff --git a/test/replication/gh-6034-promote-bump-term.result b/test/replication/gh-6034-promote-bump-term.result new file mode 100644 index 000000000..20e352922 --- /dev/null +++ b/test/replication/gh-6034-promote-bump-term.result @@ -0,0 +1,37 @@ +-- test-run result file version 2 +test_run = require('test_run').new() + | --- + | ... + +-- gh-6034: test that every box.ctl.promote() bumps +-- the instance's term. Even when elections are disabled. Even for consequent +-- promotes on the same instance. +election_mode = box.cfg.election_mode + | --- + | ... +box.cfg{election_mode='off'} + | --- + | ... + +term = box.info.election.term + | --- + | ... +box.ctl.promote() + | --- + | ... +assert(box.info.election.term == term + 1) + | --- + | - true + | ... +box.ctl.promote() + | --- + | ... +assert(box.info.election.term == term + 2) + | --- + | - true + | ... + +-- Cleanup. +box.cfg{election_mode=election_mode} + | --- + | ... diff --git a/test/replication/gh-6034-promote-bump-term.test.lua b/test/replication/gh-6034-promote-bump-term.test.lua new file mode 100644 index 000000000..5847dbb8f --- /dev/null +++ b/test/replication/gh-6034-promote-bump-term.test.lua @@ -0,0 +1,16 @@ +test_run = require('test_run').new() + +-- gh-6034: test that every box.ctl.promote() bumps +-- the instance's term. Even when elections are disabled. Even for consequent +-- promotes on the same instance. +election_mode = box.cfg.election_mode +box.cfg{election_mode='off'} + +term = box.info.election.term +box.ctl.promote() +assert(box.info.election.term == term + 1) +box.ctl.promote() +assert(box.info.election.term == term + 2) + +-- Cleanup. +box.cfg{election_mode=election_mode} diff --git a/test/replication/suite.cfg b/test/replication/suite.cfg index c4b3fbd9c..496b2e104 100644 --- a/test/replication/suite.cfg +++ b/test/replication/suite.cfg @@ -48,6 +48,7 @@ "gh-5613-bootstrap-prefer-booted.test.lua": {}, "gh-6027-applier-error-show.test.lua": {}, "gh-6032-promote-wal-write.test.lua": {}, + "gh-6034-promote-bump-term.test.lua": {}, "gh-6057-qsync-confirm-async-no-wal.test.lua": {}, "gh-6094-rs-uuid-mismatch.test.lua": {}, "gh-6127-election-join-new.test.lua": {}, -- 2.30.1 (Apple Git-130)