sync_raft_progress_tracker.h 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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"
20
#include "sync_raft_quorum.h"
21
#include "sync_raft_quorum_joint.h"
22
#include "sync_raft_progress.h"
23
#include "sync_raft_proto.h"
L
lichuang 已提交
24
#include "thash.h"
25 26 27 28

struct SSyncRaftProgressTrackerConfig {
  SSyncRaftQuorumJointConfig voters;

29 30 31 32
	// autoLeave is true if the configuration is joint and a transition to the
	// 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.
33 34
  bool autoLeave;

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
	// 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.
  SSyncRaftNodeMap learners;

	// 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.
  SSyncRaftNodeMap learnersNext;
80 81 82 83 84
};

struct SSyncRaftProgressTracker {
  SSyncRaftProgressTrackerConfig config;

85
  SSyncRaftProgressMap progressMap;
86

L
lichuang 已提交
87 88 89
	// nodeid -> ESyncRaftVoteType map
	SHashObj* votesMap;

90
  int maxInflightMsgs;
91 92

	SSyncRaft* pRaft;
93 94
};

95
SSyncRaftProgressTracker* syncRaftOpenProgressTracker(SSyncRaft* pRaft);
96 97 98 99 100

void syncRaftResetVotes(SSyncRaftProgressTracker*);

void syncRaftProgressVisit(SSyncRaftProgressTracker*, visitProgressFp visit, void* arg);

101 102 103 104
/**
 * syncRaftRecordVote records that the node with the given id voted for this Raft
 * instance if v == true (and declined it otherwise).
 **/
L
lichuang 已提交
105
void syncRaftRecordVote(SSyncRaftProgressTracker* tracker, SyncNodeId id, bool grant);
106

107 108 109 110
void syncRaftCloneTrackerConfig(const SSyncRaftProgressTrackerConfig* config, SSyncRaftProgressTrackerConfig* result);

int syncRaftCheckProgress(const SSyncRaftProgressTrackerConfig* config, SSyncRaftProgressMap* progressMap);

111 112 113 114
/** 
 * syncRaftTallyVotes returns the number of granted and rejected Votes, and whether the
 * election outcome is known.
 **/
115
ESyncRaftVoteResult syncRaftTallyVotes(SSyncRaftProgressTracker* tracker, int* rejected, int *granted);
116

117 118
void syncRaftConfigState(const SSyncRaftProgressTracker* tracker, SSyncConfigState* cs);

119 120
bool syncRaftIsInNodeMap(const SSyncRaftNodeMap* nodeMap, SyncNodeId nodeId);

121
#endif  /* _TD_LIBS_SYNC_RAFT_PROGRESS_TRACKER_H */