From 989bb8f0df2b155be1699ad316f4491bbbd5c2c3 Mon Sep 17 00:00:00 2001
From: Vladimir Davydov <vdavydov.dev@gmail.com>
Date: Sat, 14 Jul 2018 20:59:57 +0300
Subject: [PATCH] Include oldest vclock available on the instance in
 IPROTO_BALLOT

It will be used to check if a replica fell too much behind its peers and
so needs to be rebootstrapped.

Needed for #461
---
 src/box/box.cc             |  1 +
 src/box/gc.c               |  6 ++++++
 src/box/gc.h               |  6 ++++++
 src/box/iproto_constants.h |  1 +
 src/box/xrow.c             | 13 ++++++++++---
 src/box/xrow.h             |  2 ++
 6 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/src/box/box.cc b/src/box/box.cc
index a3348fef52..dbcb7e8dac 100644
--- a/src/box/box.cc
+++ b/src/box/box.cc
@@ -1569,6 +1569,7 @@ box_process_vote(struct ballot *ballot)
 {
 	ballot->is_ro = cfg_geti("read_only") != 0;
 	vclock_copy(&ballot->vclock, &replicaset.vclock);
+	gc_vclock(&ballot->gc_vclock);
 }
 
 /** Insert a new cluster into _schema */
diff --git a/src/box/gc.c b/src/box/gc.c
index 9eccf5d301..84897dabb6 100644
--- a/src/box/gc.c
+++ b/src/box/gc.c
@@ -164,6 +164,12 @@ gc_free(void)
 	latch_destroy(&gc.latch);
 }
 
+void
+gc_vclock(struct vclock *vclock)
+{
+	vclock_copy(vclock, &gc.wal_vclock);
+}
+
 /** Find the consumer that uses the oldest checkpoint. */
 struct gc_consumer *
 gc_tree_first_checkpoint(gc_tree_t *consumers)
diff --git a/src/box/gc.h b/src/box/gc.h
index 1bc8aedf2b..36a951bf1e 100644
--- a/src/box/gc.h
+++ b/src/box/gc.h
@@ -60,6 +60,12 @@ gc_init(void);
 void
 gc_free(void);
 
+/**
+ * Get the oldest available vclock.
+ */
+void
+gc_vclock(struct vclock *vclock);
+
 /**
  * Invoke garbage collection in order to remove files left
  * from old checkpoints. The number of checkpoints saved by
diff --git a/src/box/iproto_constants.h b/src/box/iproto_constants.h
index d3d20f02b6..f282a0b2b1 100644
--- a/src/box/iproto_constants.h
+++ b/src/box/iproto_constants.h
@@ -87,6 +87,7 @@ enum iproto_key {
 enum iproto_ballot_key {
 	IPROTO_BALLOT_IS_RO = 0x01,
 	IPROTO_BALLOT_VCLOCK = 0x02,
+	IPROTO_BALLOT_GC_VCLOCK = 0x03,
 };
 
 #define bit(c) (1ULL<<IPROTO_##c)
diff --git a/src/box/xrow.c b/src/box/xrow.c
index 2a73372d1b..269a6e6802 100644
--- a/src/box/xrow.c
+++ b/src/box/xrow.c
@@ -344,9 +344,10 @@ iproto_reply_vote(struct obuf *out, const struct ballot *ballot,
 		  uint64_t sync, uint32_t schema_version)
 {
 	size_t max_size = IPROTO_HEADER_LEN + mp_sizeof_map(1) +
-		mp_sizeof_uint(UINT32_MAX) + mp_sizeof_map(2) +
+		mp_sizeof_uint(UINT32_MAX) + mp_sizeof_map(3) +
 		mp_sizeof_uint(UINT32_MAX) + mp_sizeof_bool(ballot->is_ro) +
-		mp_sizeof_uint(UINT32_MAX) + mp_sizeof_vclock(&ballot->vclock);
+		mp_sizeof_uint(UINT32_MAX) + mp_sizeof_vclock(&ballot->vclock) +
+		mp_sizeof_uint(UINT32_MAX) + mp_sizeof_vclock(&ballot->gc_vclock);
 
 	char *buf = obuf_reserve(out, max_size);
 	if (buf == NULL) {
@@ -358,11 +359,13 @@ iproto_reply_vote(struct obuf *out, const struct ballot *ballot,
 	char *data = buf + IPROTO_HEADER_LEN;
 	data = mp_encode_map(data, 1);
 	data = mp_encode_uint(data, IPROTO_BALLOT);
-	data = mp_encode_map(data, 2);
+	data = mp_encode_map(data, 3);
 	data = mp_encode_uint(data, IPROTO_BALLOT_IS_RO);
 	data = mp_encode_bool(data, ballot->is_ro);
 	data = mp_encode_uint(data, IPROTO_BALLOT_VCLOCK);
 	data = mp_encode_vclock(data, &ballot->vclock);
+	data = mp_encode_uint(data, IPROTO_BALLOT_GC_VCLOCK);
+	data = mp_encode_vclock(data, &ballot->gc_vclock);
 	size_t size = data - buf;
 	assert(size <= max_size);
 
@@ -933,6 +936,10 @@ xrow_decode_ballot(struct xrow_header *row, struct ballot *ballot)
 			if (mp_decode_vclock(&data, &ballot->vclock) != 0)
 				goto err;
 			break;
+		case IPROTO_BALLOT_GC_VCLOCK:
+			if (mp_decode_vclock(&data, &ballot->gc_vclock) != 0)
+				goto err;
+			break;
 		default:
 			mp_next(&data);
 		}
diff --git a/src/box/xrow.h b/src/box/xrow.h
index be2774bb19..9887382c7b 100644
--- a/src/box/xrow.h
+++ b/src/box/xrow.h
@@ -229,6 +229,8 @@ struct ballot {
 	bool is_ro;
 	/** Current instance vclock. */
 	struct vclock vclock;
+	/** Oldest vclock available on the instance. */
+	struct vclock gc_vclock;
 };
 
 /**
-- 
GitLab