From 8e5d9f2a9bcdee64d524181950f4ba2790e78aa3 Mon Sep 17 00:00:00 2001
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
Date: Wed, 8 May 2024 19:53:35 +0200
Subject: [PATCH] applier: move applier_txn_last_tm into applier

It was stored in struct replica, now is in struct applier. The
motivation is that applier-specific data must be inside the
applier.

Also it makes the next commits look more logical. They are going
to change this timestamp when applier progresses through its state
machine. It looks strange when the applier is changing the replica
object. Replica is on an upper level in the hierarchy. It owns the
applier and the applier ideally mustn't know about struct replica
(hardly possible to achieve), or at least not change it (this is
feasible).

In scope of #9748

NO_DOC=internal
NO_TEST=refactoring
NO_CHANGELOG=refactoring
---
 src/box/applier.cc     |  8 +++-----
 src/box/applier.h      | 11 +++++++----
 src/box/replication.cc |  1 -
 src/box/replication.h  |  5 -----
 4 files changed, 10 insertions(+), 15 deletions(-)

diff --git a/src/box/applier.cc b/src/box/applier.cc
index 009985e162..58838bfb06 100644
--- a/src/box/applier.cc
+++ b/src/box/applier.cc
@@ -1254,8 +1254,8 @@ static void
 replica_txn_wal_write_cb(struct replica_cb_data *rcb)
 {
 	struct replica *r = replica_by_id(rcb->replica_id);
-	if (likely(r != NULL))
-		r->applier_txn_last_tm = rcb->txn_last_tm;
+	if (likely(r != NULL && r->applier != NULL))
+		r->applier->txn_last_tm = rcb->txn_last_tm;
 }
 
 static int
@@ -1735,9 +1735,7 @@ applier_signal_ack(struct applier *applier)
 		 * timestamp in tm field. If user delete the node from _cluster
 		 * space, we obtain a nil pointer here.
 		 */
-		struct replica *r = replica_by_id(applier->instance_id);
-		applier->ack_msg.txn_last_tm = (r == NULL ? 0 :
-						r->applier_txn_last_tm);
+		applier->ack_msg.txn_last_tm = applier->txn_last_tm;
 		applier->ack_msg.vclock_sync = applier->last_vclock_sync;
 		applier->ack_msg.term = box_raft()->term;
 		vclock_copy(&applier->ack_msg.vclock, &replicaset.vclock);
diff --git a/src/box/applier.h b/src/box/applier.h
index 7451a302b2..5f97fb3d96 100644
--- a/src/box/applier.h
+++ b/src/box/applier.h
@@ -115,10 +115,7 @@ struct applier_data_msg {
 /** A message sent from tx to applier thread to trigger ACK. */
 struct applier_ack_msg {
 	struct cmsg base;
-	/**
-	 * Last written transaction timestamp.
-	 * Set to replica::applier_txn_last_tm.
-	 */
+	/** Last written transaction timestamp. */
 	double txn_last_tm;
 	/** Last known raft term. */
 	uint64_t term;
@@ -213,6 +210,12 @@ struct applier {
 	struct applier_msg *pending_msgs[3];
 	/** Pending message count. */
 	int pending_msg_cnt;
+	/**
+	 * Last written transaction timestamp. It can be updated by TX thread
+	 * while the ACK msg with its own copy of this timestamp is being used
+	 * by a worker thread.
+	 */
+	double txn_last_tm;
 	/** Message sent to applier thread to trigger ACK. */
 	struct applier_ack_msg ack_msg;
 	struct cmsg_hop ack_route[2];
diff --git a/src/box/replication.cc b/src/box/replication.cc
index 83bc4b6cd4..50cbe77ef0 100644
--- a/src/box/replication.cc
+++ b/src/box/replication.cc
@@ -304,7 +304,6 @@ replica_new(void)
 	trigger_create(&replica->on_applier_state,
 		       replica_on_applier_state_f, NULL, NULL);
 	replica->applier_sync_state = APPLIER_DISCONNECTED;
-	replica->applier_txn_last_tm = 0;
 	latch_create(&replica->order_latch);
 	return replica;
 }
diff --git a/src/box/replication.h b/src/box/replication.h
index b58d54419a..82a54976e4 100644
--- a/src/box/replication.h
+++ b/src/box/replication.h
@@ -427,11 +427,6 @@ struct replica {
 	 * separate from applier.
 	 */
 	enum applier_state applier_sync_state;
-	/**
-	 * Applier's last written to WAL transaction timestamp.
-	 * Needed for relay lagging statistics.
-	 */
-	double applier_txn_last_tm;
 	/* The latch is used to order replication requests. */
 	struct latch order_latch;
 };
-- 
GitLab