From 450b978df2fc1f530cbb978a97295b174f965329 Mon Sep 17 00:00:00 2001
From: Serge Petrenko <sergepetrenko@tarantool.org>
Date: Wed, 3 May 2023 11:35:58 +0300
Subject: [PATCH] replication: move connect_state calculations out from trigger

The idea is to simplify applier_on_connect_f as much as possible, so
that it only wakes the waiter up, but doesn't participate in any
decisions regarding the replicaset connection state.

In-scope-of #8509

NO_DOC=refactoring
NO_TEST=refactoring
NO_CHANGELOG=refactoring
---
 src/box/replication.cc | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/src/box/replication.cc b/src/box/replication.cc
index c99151c7e9..0005eb661a 100644
--- a/src/box/replication.cc
+++ b/src/box/replication.cc
@@ -898,15 +898,9 @@ applier_on_connect_f(struct trigger *trigger, void *event)
 	struct replicaset_connect_state *state = on_connect->state;
 	struct applier *applier = (struct applier *)event;
 
-	switch (applier->state) {
-	case APPLIER_OFF:
-	case APPLIER_STOPPED:
-		state->failed++;
-		break;
-	case APPLIER_CONNECTED:
-		state->connected++;
-		break;
-	default:
+	if (applier->state != APPLIER_OFF &&
+	    applier->state != APPLIER_STOPPED &&
+	    applier->state != APPLIER_CONNECTED) {
 		return 0;
 	}
 	fiber_cond_signal(&state->wakeup);
@@ -961,6 +955,18 @@ replicaset_is_connected(struct replicaset_connect_state *state,
 			return true;
 		}
 	}
+	/* Update connected and failed counters. */
+	state->connected = 0;
+	state->failed = 0;
+	for (int i = 0; i < count; i++) {
+		struct applier *applier = appliers[i];
+		if (applier->state == APPLIER_CONNECTED) {
+			state->connected++;
+		} else if (applier->state == APPLIER_STOPPED ||
+			   applier->state == APPLIER_OFF) {
+			state->failed++;
+		}
+	}
 	if (state->connected == count)
 		return true;
 	/*
-- 
GitLab