sync_raft_quorum_joint.c 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

L
lichuang 已提交
16
#include "sync_raft_node_map.h"
17 18 19 20 21 22 23 24 25
#include "sync_raft_quorum_majority.h"
#include "sync_raft_quorum_joint.h"
#include "sync_raft_quorum.h"

/**
 * syncRaftVoteResult takes a mapping of voters to yes/no (true/false) votes and returns
 * a result indicating whether the vote is pending, lost, or won. A joint quorum
 * requires both majority quorums to vote in favor.
 **/
L
lichuang 已提交
26 27 28
ESyncRaftVoteType syncRaftVoteResult(SSyncRaftQuorumJointConfig* config, SHashObj* votesMap) {
  ESyncRaftVoteResult r1 = syncRaftMajorityVoteResult(&(config->incoming), votesMap);
  ESyncRaftVoteResult r2 = syncRaftMajorityVoteResult(&(config->outgoing), votesMap);
29 30 31 32 33 34 35 36 37 38 39 40 41 42

  if (r1 == r2) {
    // If they agree, return the agreed state.
    return r1;
  }

  if (r1 == SYNC_RAFT_VOTE_LOST || r2 == SYNC_RAFT_VOTE_LOST) {
    // If either config has lost, loss is the only possible outcome.
    return SYNC_RAFT_VOTE_LOST;
  }

  // One side won, the other one is pending, so the whole outcome is.
  return SYNC_RAFT_VOTE_PENDING;
}
43

44 45 46 47 48 49 50 51 52 53
void syncRaftInitQuorumJointConfig(SSyncRaftQuorumJointConfig* config) {
  syncRaftInitNodeMap(&config->incoming);
  syncRaftInitNodeMap(&config->outgoing);
}

void syncRaftFreeQuorumJointConfig(SSyncRaftQuorumJointConfig* config) {
  syncRaftFreeNodeMap(&config->incoming);
  syncRaftFreeNodeMap(&config->outgoing);
}

54
void syncRaftJointConfigAddToIncoming(SSyncRaftQuorumJointConfig* config, SyncNodeId id) {
55
  syncRaftAddToNodeMap(&config->incoming, id);
56 57 58
}

void syncRaftJointConfigRemoveFromIncoming(SSyncRaftQuorumJointConfig* config, SyncNodeId id) {
59
  syncRaftRemoveFromNodeMap(&config->incoming, id);
60 61
}

62
void syncRaftJointConfigIDs(SSyncRaftQuorumJointConfig* config, SSyncRaftNodeMap* nodeMap) {
L
lichuang 已提交
63
  syncRaftCopyNodeMap(&config->incoming, nodeMap);
64

L
lichuang 已提交
65
  syncRaftUnionNodeMap(&config->outgoing, nodeMap);
66 67 68 69 70 71 72 73 74
}

SyncIndex syncRaftJointConfigCommittedIndex(const SSyncRaftQuorumJointConfig* config, matchAckIndexerFp indexer, void* arg) {
  SyncIndex index0, index1;

  index0 = syncRaftMajorityConfigCommittedIndex(&config->incoming, indexer, arg);
  index1 = syncRaftMajorityConfigCommittedIndex(&config->outgoing, indexer, arg);

  return index0 < index1 ? index0 : index1;
75
}