[tarantool-patches] [PATCH 1/3] replication: use applier_state to check quorum
Konstantin Belyavskiy
k.belyavskiy at tarantool.org
Wed May 16 14:32:25 MSK 2018
Small refactoring: remove 'enum replica_state' since reuse a subset
from applier state machine 'enum replica_state' to check if we have
achieved replication quorum and hence can leave read-only mode.
---
src/box/replication.cc | 31 +++++++++++++++----------------
src/box/replication.h | 32 ++++++++++++--------------------
2 files changed, 27 insertions(+), 36 deletions(-)
diff --git a/src/box/replication.cc b/src/box/replication.cc
index 185c05305..9aac1f077 100644
--- a/src/box/replication.cc
+++ b/src/box/replication.cc
@@ -38,7 +38,6 @@
#include "box.h"
#include "gc.h"
-#include "applier.h"
#include "error.h"
#include "vclock.h" /* VCLOCK_MAX */
@@ -138,7 +137,7 @@ replica_new(void)
rlist_create(&replica->in_anon);
trigger_create(&replica->on_applier_state,
replica_on_applier_state_f, NULL, NULL);
- replica->state = REPLICA_DISCONNECTED;
+ replica->applier_sync_state = APPLIER_DISCONNECTED;
return replica;
}
@@ -219,9 +218,9 @@ replica_clear_applier(struct replica *replica)
static void
replica_on_applier_sync(struct replica *replica)
{
- assert(replica->state == REPLICA_CONNECTED);
+ assert(replica->applier_sync_state == APPLIER_CONNECTED);
- replica->state = REPLICA_SYNCED;
+ replica->applier_sync_state = APPLIER_SYNC;
replicaset.applier.synced++;
replicaset_check_quorum();
@@ -234,7 +233,7 @@ replica_on_applier_connect(struct replica *replica)
assert(tt_uuid_is_nil(&replica->uuid));
assert(!tt_uuid_is_nil(&applier->uuid));
- assert(replica->state == REPLICA_DISCONNECTED);
+ assert(replica->applier_sync_state == APPLIER_DISCONNECTED);
replica->uuid = applier->uuid;
@@ -265,7 +264,7 @@ replica_on_applier_connect(struct replica *replica)
replica_hash_insert(&replicaset.hash, replica);
}
- replica->state = REPLICA_CONNECTED;
+ replica->applier_sync_state = APPLIER_CONNECTED;
replicaset.applier.connected++;
}
@@ -276,7 +275,7 @@ replica_on_applier_reconnect(struct replica *replica)
assert(!tt_uuid_is_nil(&replica->uuid));
assert(!tt_uuid_is_nil(&applier->uuid));
- assert(replica->state == REPLICA_DISCONNECTED);
+ assert(replica->applier_sync_state == APPLIER_DISCONNECTED);
if (!tt_uuid_is_equal(&replica->uuid, &applier->uuid)) {
/*
@@ -298,32 +297,32 @@ replica_on_applier_reconnect(struct replica *replica)
replica_set_applier(orig, applier);
replica_clear_applier(replica);
- replica->state = REPLICA_DISCONNECTED;
+ replica->applier_sync_state = APPLIER_DISCONNECTED;
replica = orig;
}
- replica->state = REPLICA_CONNECTED;
+ replica->applier_sync_state = APPLIER_CONNECTED;
replicaset.applier.connected++;
}
static void
replica_on_applier_disconnect(struct replica *replica)
{
- switch (replica->state) {
- case REPLICA_SYNCED:
+ switch (replica->applier_sync_state) {
+ case APPLIER_SYNC:
assert(replicaset.applier.synced > 0);
replicaset.applier.synced--;
FALLTHROUGH;
- case REPLICA_CONNECTED:
+ case APPLIER_CONNECTED:
assert(replicaset.applier.connected > 0);
replicaset.applier.connected--;
break;
- case REPLICA_DISCONNECTED:
+ case APPLIER_DISCONNECTED:
break;
default:
unreachable();
}
- replica->state = REPLICA_DISCONNECTED;
+ replica->applier_sync_state = APPLIER_DISCONNECTED;
}
static void
@@ -424,7 +423,7 @@ replicaset_update(struct applier **appliers, int count)
continue;
applier = replica->applier;
replica_clear_applier(replica);
- replica->state = REPLICA_DISCONNECTED;
+ replica->applier_sync_state = APPLIER_DISCONNECTED;
applier_stop(applier);
applier_delete(applier);
}
@@ -458,7 +457,7 @@ replicaset_update(struct applier **appliers, int count)
replica_hash_insert(&replicaset.hash, replica);
}
- replica->state = REPLICA_CONNECTED;
+ replica->applier_sync_state = APPLIER_CONNECTED;
replicaset.applier.connected++;
}
rlist_swap(&replicaset.anon, &anon_replicas);
diff --git a/src/box/replication.h b/src/box/replication.h
index 7b85c2fc4..e2bdd814f 100644
--- a/src/box/replication.h
+++ b/src/box/replication.h
@@ -36,6 +36,7 @@
#define RB_COMPACT 1
#include <small/rb.h> /* replicaset_t */
#include <small/rlist.h>
+#include "applier.h"
#include <small/mempool.h>
#include "fiber_cond.h"
#include "vclock.h"
@@ -214,24 +215,6 @@ struct replicaset {
};
extern struct replicaset replicaset;
-enum replica_state {
- /**
- * Applier has not connected to the master yet
- * or has disconnected.
- */
- REPLICA_DISCONNECTED,
- /**
- * Applier has connected to the master and
- * received UUID.
- */
- REPLICA_CONNECTED,
- /**
- * Applier has synchronized with the master
- * (left "sync" and entered "follow" state).
- */
- REPLICA_SYNCED,
-};
-
/**
* Summary information about a replica in the replica set.
*/
@@ -260,8 +243,17 @@ struct replica {
* Trigger invoked when the applier changes its state.
*/
struct trigger on_applier_state;
- /** Replica sync state. */
- enum replica_state state;
+ /**
+ * During initial connect or reconnect we require applier
+ * to sync with the master before the replica can leave
+ * read-only mode. This enum reflects the state of the
+ * state machine for applier sync. Technically it is a
+ * subset of the applier state machine, but since it's
+ * much simpler and is used for a different purpose
+ * (achieving replication connect quorum), we keep it
+ * separate from applier.
+ */
+ enum applier_state applier_sync_state;
};
enum {
--
2.14.3 (Apple Git-98)
More information about the Tarantool-patches
mailing list