syncRaft.h 2.6 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@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_H
#define _TD_LIBS_SYNC_RAFT_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
M
Minghao Li 已提交
26
#include "sync.h"
M
Minghao Li 已提交
27 28 29 30
#include "syncMessage.h"
#include "taosdef.h"

typedef struct SRaftId {
M
Minghao Li 已提交
31
  SyncNodeId  addr;
M
Minghao Li 已提交
32 33 34 35
  SyncGroupId vgId;
} SRaftId;

typedef struct SRaft {
M
Minghao Li 已提交
36 37
  SRaftId   id;
  SSyncFSM* pFsm;
M
Minghao Li 已提交
38

M
Minghao Li 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  int32_t (*FpPing)(struct SRaft* ths, const RaftPing* pMsg);

  int32_t (*FpOnPing)(struct SRaft* ths, RaftPing* pMsg);

  int32_t (*FpOnPingReply)(struct SRaft* ths, RaftPingReply* pMsg);

  int32_t (*FpRequestVote)(struct SRaft* ths, const RaftRequestVote* pMsg);

  int32_t (*FpOnRequestVote)(struct SRaft* ths, RaftRequestVote* pMsg);

  int32_t (*FpOnRequestVoteReply)(struct SRaft* ths, RaftRequestVoteReply* pMsg);

  int32_t (*FpAppendEntries)(struct SRaft* ths, const RaftAppendEntries* pMsg);

  int32_t (*FpOnAppendEntries)(struct SRaft* ths, RaftAppendEntries* pMsg);

  int32_t (*FpOnAppendEntriesReply)(struct SRaft* ths, RaftAppendEntriesReply* pMsg);
M
Minghao Li 已提交
56

M
Minghao Li 已提交
57 58
} SRaft;

M
Minghao Li 已提交
59 60 61
SRaft* raftOpen(SRaftId raftId, SSyncFSM* pFsm);

void raftClose(SRaft* pRaft);
M
Minghao Li 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

static int32_t doRaftPing(struct SRaft* ths, const RaftPing* pMsg);

static int32_t onRaftPing(struct SRaft* ths, RaftPing* pMsg);

static int32_t onRaftPingReply(struct SRaft* ths, RaftPingReply* pMsg);

static int32_t doRaftRequestVote(struct SRaft* ths, const RaftRequestVote* pMsg);

static int32_t onRaftRequestVote(struct SRaft* ths, RaftRequestVote* pMsg);

static int32_t onRaftRequestVoteReply(struct SRaft* ths, RaftRequestVoteReply* pMsg);

static int32_t doRaftAppendEntries(struct SRaft* ths, const RaftAppendEntries* pMsg);

static int32_t onRaftAppendEntries(struct SRaft* ths, RaftAppendEntries* pMsg);

static int32_t onRaftAppendEntriesReply(struct SRaft* ths, RaftAppendEntriesReply* pMsg);

M
Minghao Li 已提交
81 82 83 84 85 86 87 88 89
int32_t raftPropose(SRaft* pRaft, const SSyncBuffer* pBuf, bool isWeak);

static int raftSendMsg(SRaftId destRaftId, const void* pMsg, const SRaft* pRaft);

#ifdef __cplusplus
}
#endif

#endif /*_TD_LIBS_SYNC_RAFT_H*/