sync_raft_progress_tracker.h 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (c) 2019 TAOS Data, Inc. <cli@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _TD_LIBS_SYNC_RAFT_PROGRESS_TRACKER_H
#define _TD_LIBS_SYNC_RAFT_PROGRESS_TRACKER_H

#include "sync_type.h"
#include "sync_raft_quorum_joint.h"
21
#include "sync_raft_progress.h"
22 23 24 25

struct SSyncRaftProgressTrackerConfig {
  SSyncRaftQuorumJointConfig voters;

26 27
	/** 
	 * autoLeave is true if the configuration is joint and a transition to the
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	 * incoming configuration should be carried out automatically by Raft when
	 * this is possible. If false, the configuration will be joint until the
	 * application initiates the transition manually.
   **/
  bool autoLeave;

	/** 
   * Learners is a set of IDs corresponding to the learners active in the
	 * current configuration.
   * 
	 * Invariant: Learners and Voters does not intersect, i.e. if a peer is in
	 * either half of the joint config, it can't be a learner; if it is a
	 * learner it can't be in either half of the joint config. This invariant
	 * simplifies the implementation since it allows peers to have clarity about
	 * its current role without taking into account joint consensus.
   **/
  SyncNodeId learners[TSDB_MAX_REPLICA];

  /**
   *  When we turn a voter into a learner during a joint consensus transition,
	 * we cannot add the learner directly when entering the joint state. This is
	 * because this would violate the invariant that the intersection of
	 * voters and learners is empty. For example, assume a Voter is removed and
	 * immediately re-added as a learner (or in other words, it is demoted):
   * 
	 * Initially, the configuration will be
   * 
	 *   voters:   {1 2 3}
	 *   learners: {}
   * 
	 * and we want to demote 3. Entering the joint configuration, we naively get
   * 
	 *   voters:   {1 2} & {1 2 3}
	 *   learners: {3}
   * 
	 * but this violates the invariant (3 is both voter and learner). Instead,
	 * we get
   * 
	 *   voters:   {1 2} & {1 2 3}
	 *   learners: {}
	 *   next_learners: {3}
   * 
	 * Where 3 is now still purely a voter, but we are remembering the intention
	 * to make it a learner upon transitioning into the final configuration:
   * 
	 *   voters:   {1 2}
	 *   learners: {3}
	 *   next_learners: {}
   * 
	 * Note that next_learners is not used while adding a learner that is not
	 * also a voter in the joint config. In this case, the learner is added
	 * right away when entering the joint configuration, so that it is caught up
	 * as soon as possible.
   **/
  SyncNodeId learnersNext[TSDB_MAX_REPLICA];
};

struct SSyncRaftProgressTracker {
  SSyncRaftProgressTrackerConfig config;

  SSyncRaftProgress progressMap[TSDB_MAX_REPLICA];

90
	SyncRaftVoteResult votes[TSDB_MAX_REPLICA];
91 92 93 94 95 96 97
  int maxInflight;
};

SSyncRaftProgressTracker* syncRaftOpenProgressTracker();

void syncRaftResetVotes(SSyncRaftProgressTracker*);

98
typedef void (*visitProgressFp)(int i, SSyncRaftProgress* progress, void* arg);
99 100
void syncRaftProgressVisit(SSyncRaftProgressTracker*, visitProgressFp visit, void* arg);

101 102 103 104 105 106 107 108 109 110 111 112
/**
 * syncRaftRecordVote records that the node with the given id voted for this Raft
 * instance if v == true (and declined it otherwise).
 **/
void syncRaftRecordVote(SSyncRaftProgressTracker* tracker, int i, bool grant);

/** 
 * syncRaftTallyVotes returns the number of granted and rejected Votes, and whether the
 * election outcome is known.
 **/
SyncRaftVoteResult syncRaftTallyVotes(SSyncRaftProgressTracker* tracker, int* rejected, int *granted);

113
#endif  /* _TD_LIBS_SYNC_RAFT_PROGRESS_TRACKER_H */