sync_raft_progress_tracker.h 5.0 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
// Config reflects the configuration tracked in a ProgressTracker.
27 28 29
struct SSyncRaftProgressTrackerConfig {
  SSyncRaftQuorumJointConfig voters;

30 31 32 33
	// 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.
34 35
  bool autoLeave;

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
	// 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;
81 82 83 84 85
};

struct SSyncRaftProgressTracker {
  SSyncRaftProgressTrackerConfig config;

86
  SSyncRaftProgressMap progressMap;
87

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

91
  int maxInflightMsgs;
92 93

	SSyncRaft* pRaft;
94 95
};

96
SSyncRaftProgressTracker* syncRaftOpenProgressTracker(SSyncRaft* pRaft);
97

98 99 100 101 102
void syncRaftInitTrackConfig(SSyncRaftProgressTrackerConfig* config);
void syncRaftFreeTrackConfig(SSyncRaftProgressTrackerConfig* config);

void syncRaftFreeTrackConfig(SSyncRaftProgressTrackerConfig* config);

103
// ResetVotes prepares for a new round of vote counting via recordVote.
104 105 106 107
void syncRaftResetVotes(SSyncRaftProgressTracker*);

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

108 109
// RecordVote records that the node with the given id voted for this Raft
// instance if v == true (and declined it otherwise).
L
lichuang 已提交
110
void syncRaftRecordVote(SSyncRaftProgressTracker* tracker, SyncNodeId id, bool grant);
111

112
void syncRaftCopyTrackerConfig(const SSyncRaftProgressTrackerConfig* from, SSyncRaftProgressTrackerConfig* to);
113

114
int syncRaftCheckTrackerConfigInProgress(SSyncRaftProgressTrackerConfig* config, SSyncRaftProgressMap* progressMap);
115

116 117
// TallyVotes returns the number of granted and rejected Votes, and whether the
// election outcome is known.
118
ESyncRaftVoteResult syncRaftTallyVotes(SSyncRaftProgressTracker* tracker, int* rejected, int *granted);
119

120 121 122 123 124 125 126 127 128
void syncRaftConfigState(SSyncRaftProgressTracker* tracker, SSyncConfigState* cs);

// Committed returns the largest log index known to be committed based on what
// the voting members of the group have acknowledged.
SyncIndex syncRaftCommittedIndex(SSyncRaftProgressTracker* tracker);

// QuorumActive returns true if the quorum is active from the view of the local
// raft state machine. Otherwise, it returns false.
bool syncRaftQuorumActive(SSyncRaftProgressTracker* tracker);
129

130 131
bool syncRaftIsInNodeMap(const SSyncRaftNodeMap* nodeMap, SyncNodeId nodeId);

132
#endif  /* _TD_LIBS_SYNC_RAFT_PROGRESS_TRACKER_H */