Tarantool development patches archive
 help / color / mirror / Atom feed
From: Sergey Petrenko via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>, gorcunov@gmail.com
Cc: tarantool-patches@dev.tarantool.org
Subject: Re: [Tarantool-patches] [PATCH v4 09/16] box: split promote() into reasonable parts
Date: Fri, 23 Jul 2021 09:45:03 +0200	[thread overview]
Message-ID: <d0e5c04d-479a-b473-fabf-68e5ae49cc95@tarantool.org> (raw)
In-Reply-To: <c73cebab-dcf5-4a43-2937-92cf2c324f47@tarantool.org>


22.07.2021 01:26, Vladislav Shpilevoy пишет:
> Thanks for working on this!
>
> See 3 comments below.

Thanks for the review!


>> diff --git a/src/box/box.cc b/src/box/box.cc
>> index 86370514a..445875f8f 100644
>> --- a/src/box/box.cc
>> +++ b/src/box/box.cc
>> @@ -1527,6 +1527,147 @@ box_wait_quorum(uint32_t lead_id, int64_t target_lsn, int quorum,
> <...>
>
>> +
>> +/**
>> + * Check whether the greatest promote term has changed since it was last read.
>> + * IOW check that a foreign PROMOTE arrived while we were sleeping.
>> + */
>> +static int
>> +box_check_promote_term_changed(uint64_t promote_term)
> 1. Normally you call check functions using the pattern
> "check_something_correct". Here the correct behaviour is the term
> being intact. So I propose to rename it to box_check_promote_term_intact.


Ok, sure.


>> +{
>> +	if (txn_limbo.promote_greatest_term != promote_term) {
>> +		diag_set(ClientError, ER_INTERFERING_PROMOTE,
>> +			 txn_limbo.owner_id);
>> +		return -1;
>> +	}
>> +	return 0;
>> +}
> <...>
>
>> +
>> +/**
>> + * A helper to wait until all limbo entries are ready to be confirmed, i.e.
>> + * written to WAL and have gathered a quorum of ACKs from replicas.
>> + * Return lsn of the last limbo entry on success, -1 on error.
>> + */
>> +static int64_t
>> +box_wait_limbo_acked(void)
>> +{
>> +	if (txn_limbo_is_empty(&txn_limbo))
>> +		return txn_limbo.confirmed_lsn;
>> +
>> +	uint64_t promote_term = txn_limbo.promote_greatest_term;
>> +	int quorum = replication_synchro_quorum;
>> +	struct txn_limbo_entry *last_entry;
>> +	last_entry = txn_limbo_last_synchro_entry(&txn_limbo);
>> +	/* Wait for the last entries WAL write. */
>> +	if (last_entry->lsn < 0) {
>> +		int64_t tid = last_entry->txn->id;
>> +
>> +		if (wal_sync(NULL) < 0)
>> +			return -1;
>> +
>> +		if (box_check_promote_term_changed(promote_term) < 0)
> 2. Why < 0? It is not a in the code guidelines, but don't we usually
> use '!= 0'? '< 0' normally assumes you can get > 0, 0, and < 0 meaning
> different things, like it is done in iproto occassionally.


I've put '< 0' here without a second thought.

I'm just used to if (smth() < 0) { err; }, I guess.

AFAICS there are more places where we use if (rc != 0) { err;} more,

so I'll change my code accordingly.


>> +			return -1;
>> +		if (txn_limbo_is_empty(&txn_limbo))
>> +			return txn_limbo.confirmed_lsn;
>> +		if (tid != txn_limbo_last_synchro_entry(&txn_limbo)->txn->id) {
>> +			diag_set(ClientError, ER_QUORUM_WAIT, quorum,
>> +				 "new synchronous transactions appeared");
>> +			return -1;
>> +		}
>> +	}
> <...>
>
>> +
>> +/** Write and process a PROMOTE request. */
>> +static void
>> +box_issue_promote(uint32_t prev_leader_id, int64_t promote_lsn)
>> +{
>> +	assert(box_raft()->volatile_term == box_raft()->term);
>> +	assert(promote_lsn >= 0);
>> +	txn_limbo_write_promote(&txn_limbo, promote_lsn,
>> +				box_raft()->term);
> 3. Maybe cache box_raft() value in a variable? Its usage would look shorter
> then. The same in other places where it is used more than once. Up to
> you.

Done.

Incremental diff:

=========================

diff --git a/src/box/box.cc b/src/box/box.cc
index 341857267..d83c30918 100644
--- a/src/box/box.cc
+++ b/src/box/box.cc
@@ -1563,7 +1563,7 @@ box_run_elections(void)
   * IOW check that a foreign PROMOTE arrived while we were sleeping.
   */
  static int
-box_check_promote_term_changed(uint64_t promote_term)
+box_check_promote_term_intact(uint64_t promote_term)
  {
      if (txn_limbo.promote_greatest_term != promote_term) {
          diag_set(ClientError, ER_INTERFERING_PROMOTE,
@@ -1579,7 +1579,7 @@ box_try_wait_confirm(double timeout)
  {
      uint64_t promote_term = txn_limbo.promote_greatest_term;
      txn_limbo_wait_empty(&txn_limbo, timeout);
-    return box_check_promote_term_changed(promote_term);
+    return box_check_promote_term_intact(promote_term);
  }

  /**
@@ -1604,7 +1604,7 @@ box_wait_limbo_acked(void)
          if (wal_sync(NULL) < 0)
              return -1;

-        if (box_check_promote_term_changed(promote_term) < 0)
+        if (box_check_promote_term_intact(promote_term) != 0)
              return -1;
          if (txn_limbo_is_empty(&txn_limbo))
              return txn_limbo.confirmed_lsn;
@@ -1618,10 +1618,10 @@ box_wait_limbo_acked(void)
      int64_t wait_lsn = last_entry->lsn;

      if (box_wait_quorum(txn_limbo.owner_id, wait_lsn, quorum,
-                replication_synchro_timeout) < 0)
+                replication_synchro_timeout) != 0)
          return -1;

-    if (box_check_promote_term_changed(promote_term) < 0)
+    if (box_check_promote_term_intact(promote_term) != 0)
          return -1;

      if (txn_limbo_is_empty(&txn_limbo))
@@ -1722,10 +1722,10 @@ box_promote(void)

      int64_t wait_lsn = -1;

-    if (run_elections && box_run_elections() < 0)
+    if (run_elections && box_run_elections() != 0)
          return -1;
      if (try_wait &&
-        box_try_wait_confirm(2 * replication_synchro_timeout) < 0)
+        box_try_wait_confirm(2 * replication_synchro_timeout) != 0)
          return -1;
      if ((wait_lsn = box_wait_limbo_acked()) < 0)
          return -1;

=========================

>> +	struct synchro_request req = {
>> +		.type = IPROTO_PROMOTE,
>> +		.replica_id = prev_leader_id,
>> +		.origin_id = instance_id,
>> +		.lsn = promote_lsn,
>> +		.term = box_raft()->term,
>> +	};
>> +	txn_limbo_process(&txn_limbo, &req);
>> +	assert(txn_limbo_is_empty(&txn_limbo));
>> +}

  reply	other threads:[~2021-07-23  7:46 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-14 18:25 [Tarantool-patches] [PATCH v4 00/16] forbid implicit limbo ownership transition Serge Petrenko via Tarantool-patches
2021-07-14 18:25 ` [Tarantool-patches] [PATCH v4 01/16] replication: always send raft state to subscribers Serge Petrenko via Tarantool-patches
2021-07-14 18:25 ` [Tarantool-patches] [PATCH v4 02/16] txn_limbo: fix promote term filtering Serge Petrenko via Tarantool-patches
2021-07-14 18:25 ` [Tarantool-patches] [PATCH v4 03/16] txn_limbo: persist the latest effective promote in snapshot Serge Petrenko via Tarantool-patches
2021-07-14 18:25 ` [Tarantool-patches] [PATCH v4 04/16] replication: encode version in JOIN request Serge Petrenko via Tarantool-patches
2021-07-14 18:25 ` [Tarantool-patches] [PATCH v4 05/16] replication: add META stage to JOIN Serge Petrenko via Tarantool-patches
2021-07-14 18:25 ` [Tarantool-patches] [PATCH v4 06/16] replication: send latest effective promote in initial join Serge Petrenko via Tarantool-patches
2021-07-21 23:24   ` Vladislav Shpilevoy via Tarantool-patches
2021-07-23  7:44     ` Sergey Petrenko via Tarantool-patches
2021-07-26 23:43       ` Vladislav Shpilevoy via Tarantool-patches
2021-07-14 18:25 ` [Tarantool-patches] [PATCH v4 07/16] replication: send current Raft term in join response Serge Petrenko via Tarantool-patches
2021-07-21 23:24   ` Vladislav Shpilevoy via Tarantool-patches
2021-07-23  7:44     ` Sergey Petrenko via Tarantool-patches
2021-07-26 23:43       ` Vladislav Shpilevoy via Tarantool-patches
2021-07-29 20:46         ` Sergey Petrenko via Tarantool-patches
2021-07-14 18:25 ` [Tarantool-patches] [PATCH v4 08/16] raft: refactor raft_new_term() Serge Petrenko via Tarantool-patches
2021-07-14 18:25 ` [Tarantool-patches] [PATCH v4 09/16] box: split promote() into reasonable parts Serge Petrenko via Tarantool-patches
2021-07-21 23:26   ` Vladislav Shpilevoy via Tarantool-patches
2021-07-23  7:45     ` Sergey Petrenko via Tarantool-patches [this message]
2021-07-26 23:44       ` Vladislav Shpilevoy via Tarantool-patches
2021-07-29 20:46         ` Sergey Petrenko via Tarantool-patches
2021-07-14 18:25 ` [Tarantool-patches] [PATCH v4 10/16] box: make promote always bump the term Serge Petrenko via Tarantool-patches
2021-07-26 23:45   ` Vladislav Shpilevoy via Tarantool-patches
2021-07-29 20:46     ` Sergey Petrenko via Tarantool-patches
2021-07-14 18:25 ` [Tarantool-patches] [PATCH v4 11/16] box: make promote on the current leader a no-op Serge Petrenko via Tarantool-patches
2021-07-21 23:26   ` Vladislav Shpilevoy via Tarantool-patches
2021-07-23  7:45     ` Sergey Petrenko via Tarantool-patches
2021-07-14 18:25 ` [Tarantool-patches] [PATCH v4 12/16] box: fix an assertion failure after a spurious wakeup in promote Serge Petrenko via Tarantool-patches
2021-07-21 23:29   ` Vladislav Shpilevoy via Tarantool-patches
2021-07-23  7:45     ` Sergey Petrenko via Tarantool-patches
2021-07-14 18:25 ` [Tarantool-patches] [PATCH v4 13/16] box: allow calling promote on a candidate Serge Petrenko via Tarantool-patches
2021-07-15 14:06   ` Serge Petrenko via Tarantool-patches
2021-07-14 18:25 ` [Tarantool-patches] [PATCH v4 14/16] box: extract promote() settings to a separate method Serge Petrenko via Tarantool-patches
2021-07-14 18:25 ` [Tarantool-patches] [PATCH v4 15/16] replication: forbid implicit limbo owner transition Serge Petrenko via Tarantool-patches
2021-07-14 18:25 ` [Tarantool-patches] [PATCH v4 16/16] box: introduce `box.ctl.demote` Serge Petrenko via Tarantool-patches
2021-07-15 17:13   ` Serge Petrenko via Tarantool-patches
2021-07-15 20:11   ` [Tarantool-patches] [PATCH v4 17/16] replication: fix flaky election_qsync.test Serge Petrenko via Tarantool-patches
2021-07-26 23:43 ` [Tarantool-patches] [PATCH v4 00/16] forbid implicit limbo ownership transition Vladislav Shpilevoy via Tarantool-patches
2021-07-29 20:47   ` Sergey Petrenko via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d0e5c04d-479a-b473-fabf-68e5ae49cc95@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=gorcunov@gmail.com \
    --cc=sergepetrenko@tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v4 09/16] box: split promote() into reasonable parts' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox