syncMessage.h 3.4 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_MESSAGE_H
#define _TD_LIBS_SYNC_MESSAGE_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
#include "syncRaftEntry.h"
#include "taosdef.h"

M
Minghao Li 已提交
30
// encode as uint64
M
Minghao Li 已提交
31 32 33 34 35 36 37 38 39 40 41
typedef enum ESyncMessageType {
  SYNC_PING = 0,
  SYNC_PING_REPLY,
  SYNC_CLIENT_REQUEST,
  SYNC_CLIENT_REQUEST_REPLY,
  SYNC_REQUEST_VOTE,
  SYNC_REQUEST_VOTE_REPLY,
  SYNC_APPEND_ENTRIES,
  SYNC_APPEND_ENTRIES_REPLY,
} ESyncMessageType;

M
Minghao Li 已提交
42 43 44 45 46 47 48
/*
typedef struct SRaftId {
  SyncNodeId  addr;  // typedef uint64_t SyncNodeId;
  SyncGroupId vgId;  // typedef int32_t  SyncGroupId;
} SRaftId;
*/

M
Minghao Li 已提交
49
typedef struct SyncPing {
M
Minghao Li 已提交
50 51 52 53 54 55 56 57 58
  uint32_t bytes;
  uint32_t msgType;
  SRaftId  srcId;
  SRaftId  destId;
  uint32_t dataLen;
  char*    data;
} SyncPing;

#define SYNC_PING_FIX_LEN (sizeof(uint32_t) + sizeof(uint32_t) + sizeof(SRaftId) + sizeof(SRaftId) + sizeof(uint32_t))
M
Minghao Li 已提交
59

M
Minghao Li 已提交
60
typedef struct SyncPingReply {
M
Minghao Li 已提交
61 62 63 64 65 66 67
  uint32_t bytes;
  uint32_t msgType;
  SRaftId  srcId;
  SRaftId  destId;
  uint32_t dataLen;
  char*    data;
} SyncPingReply;
M
Minghao Li 已提交
68 69

typedef struct SyncClientRequest {
M
Minghao Li 已提交
70 71 72 73 74 75
  ESyncMessageType msgType;
  char*            data;
  uint32_t         dataLen;
  int64_t          seqNum;
  bool             isWeak;
} SyncClientRequest;
M
Minghao Li 已提交
76 77

typedef struct SyncClientRequestReply {
M
Minghao Li 已提交
78 79 80 81 82
  ESyncMessageType msgType;
  int32_t          errCode;
  SSyncBuffer*     pErrMsg;
  SSyncBuffer*     pLeaderHint;
} SyncClientRequestReply;
M
Minghao Li 已提交
83

M
Minghao Li 已提交
84 85 86 87 88 89 90
typedef struct SyncRequestVote {
  ESyncMessageType msgType;
  SyncTerm         currentTerm;
  SyncNodeId       nodeId;
  SyncGroupId      vgId;
  SyncIndex        lastLogIndex;
  SyncTerm         lastLogTerm;
M
Minghao Li 已提交
91
} SyncRequestVote;
M
Minghao Li 已提交
92 93 94 95 96 97 98

typedef struct SyncRequestVoteReply {
  ESyncMessageType msgType;
  SyncTerm         currentTerm;
  SyncNodeId       nodeId;
  SyncGroupId      vgId;
  bool             voteGranted;
M
Minghao Li 已提交
99
} SyncRequestVoteReply;
M
Minghao Li 已提交
100 101 102 103 104 105 106 107

typedef struct SyncAppendEntries {
  ESyncMessageType msgType;
  SyncTerm         currentTerm;
  SyncNodeId       nodeId;
  SyncIndex        prevLogIndex;
  SyncTerm         prevLogTerm;
  int32_t          entryCount;
M
Minghao Li 已提交
108
  SSyncRaftEntry*  logEntries;
M
Minghao Li 已提交
109
  SyncIndex        commitIndex;
M
Minghao Li 已提交
110
} SyncAppendEntries;
M
Minghao Li 已提交
111 112 113 114 115 116 117

typedef struct SyncAppendEntriesReply {
  ESyncMessageType msgType;
  SyncTerm         currentTerm;
  SyncNodeId       nodeId;
  bool             success;
  SyncIndex        matchIndex;
M
Minghao Li 已提交
118 119 120 121 122 123 124 125 126 127 128 129
} SyncAppendEntriesReply;

// ---- message build ----
SyncPing* syncPingBuild(uint32_t dataLen);

void syncPingDestroy(SyncPing* pSyncPing);

void syncPingSerialize(const SyncPing* pSyncPing, char* buf, uint32_t bufLen);

void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing);

void syncPing2RpcMsg(const SyncPing* pSyncPing, SRpcMsg* pRpcMsg);
M
Minghao Li 已提交
130 131 132 133 134 135

#ifdef __cplusplus
}
#endif

#endif /*_TD_LIBS_SYNC_MESSAGE_H*/