Skip to content
Snippets Groups Projects
Commit ca53ab91 authored by Konstantin Belyavskiy's avatar Konstantin Belyavskiy Committed by Konstantin Osipov
Browse files

replication: use applier_state to check quorum

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.
parent 06a63686
No related branches found
No related tags found
No related merge requests found
......@@ -38,7 +38,6 @@
#include "box.h"
#include "gc.h"
#include "applier.h"
#include "error.h"
#include "vclock.h" /* VCLOCK_MAX */
......@@ -140,7 +139,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;
latch_create(&replica->order_latch);
return replica;
}
......@@ -224,9 +223,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();
......@@ -239,7 +238,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;
......@@ -270,7 +269,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++;
}
......@@ -281,7 +280,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)) {
/*
......@@ -303,32 +302,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
......@@ -429,7 +428,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);
}
......@@ -463,7 +462,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);
......
......@@ -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"
......@@ -218,24 +219,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.
*/
......@@ -264,8 +247,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;
/* The latch is used to order replication requests. */
struct latch order_latch;
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment