syncMessage.h 7.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 "cJSON.h"
M
Minghao Li 已提交
27
#include "sync.h"
M
Minghao Li 已提交
28 29 30
#include "syncRaftEntry.h"
#include "taosdef.h"

M
Minghao Li 已提交
31
// encode as uint32
M
Minghao Li 已提交
32
typedef enum ESyncMessageType {
M
Minghao Li 已提交
33 34
  SYNC_PING = 101,
  SYNC_PING_REPLY = 103,
M
Minghao Li 已提交
35 36 37 38 39 40
  SYNC_CLIENT_REQUEST = 105,
  SYNC_CLIENT_REQUEST_REPLY = 107,
  SYNC_REQUEST_VOTE = 109,
  SYNC_REQUEST_VOTE_REPLY = 111,
  SYNC_APPEND_ENTRIES = 113,
  SYNC_APPEND_ENTRIES_REPLY = 115,
M
Minghao Li 已提交
41 42
} ESyncMessageType;

M
Minghao Li 已提交
43
// ---------------------------------------------
M
Minghao Li 已提交
44
typedef struct SyncPing {
M
Minghao Li 已提交
45 46 47 48
  uint32_t bytes;
  uint32_t msgType;
  SRaftId  srcId;
  SRaftId  destId;
M
Minghao Li 已提交
49
  // private data
M
Minghao Li 已提交
50
  uint32_t dataLen;
M
Minghao Li 已提交
51
  char     data[];
M
Minghao Li 已提交
52 53 54
} SyncPing;

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

M
Minghao Li 已提交
56
SyncPing* syncPingBuild(uint32_t dataLen);
M
Minghao Li 已提交
57 58 59 60 61 62
void      syncPingDestroy(SyncPing* pMsg);
void      syncPingSerialize(const SyncPing* pMsg, char* buf, uint32_t bufLen);
void      syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pMsg);
void      syncPing2RpcMsg(const SyncPing* pMsg, SRpcMsg* pRpcMsg);
void      syncPingFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPing* pMsg);
cJSON*    syncPing2Json(const SyncPing* pMsg);
M
Minghao Li 已提交
63 64 65
SyncPing* syncPingBuild2(const SRaftId* srcId, const SRaftId* destId, const char* str);
SyncPing* syncPingBuild3(const SRaftId* srcId, const SRaftId* destId);

M
Minghao Li 已提交
66
// ---------------------------------------------
M
Minghao Li 已提交
67
typedef struct SyncPingReply {
M
Minghao Li 已提交
68 69 70 71
  uint32_t bytes;
  uint32_t msgType;
  SRaftId  srcId;
  SRaftId  destId;
M
Minghao Li 已提交
72
  // private data
M
Minghao Li 已提交
73
  uint32_t dataLen;
M
Minghao Li 已提交
74
  char     data[];
M
Minghao Li 已提交
75
} SyncPingReply;
M
Minghao Li 已提交
76

M
Minghao Li 已提交
77 78 79 80
#define SYNC_PING_REPLY_FIX_LEN \
  (sizeof(uint32_t) + sizeof(uint32_t) + sizeof(SRaftId) + sizeof(SRaftId) + sizeof(uint32_t))

SyncPingReply* syncPingReplyBuild(uint32_t dataLen);
M
Minghao Li 已提交
81 82 83 84 85 86
void           syncPingReplyDestroy(SyncPingReply* pMsg);
void           syncPingReplySerialize(const SyncPingReply* pMsg, char* buf, uint32_t bufLen);
void           syncPingReplyDeserialize(const char* buf, uint32_t len, SyncPingReply* pMsg);
void           syncPingReply2RpcMsg(const SyncPingReply* pMsg, SRpcMsg* pRpcMsg);
void           syncPingReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPingReply* pMsg);
cJSON*         syncPingReply2Json(const SyncPingReply* pMsg);
M
Minghao Li 已提交
87 88 89
SyncPingReply* syncPingReplyBuild2(const SRaftId* srcId, const SRaftId* destId, const char* str);
SyncPingReply* syncPingReplyBuild3(const SRaftId* srcId, const SRaftId* destId);

M
Minghao Li 已提交
90
// ---------------------------------------------
M
Minghao Li 已提交
91
typedef struct SyncClientRequest {
M
Minghao Li 已提交
92 93 94 95 96 97
  uint32_t bytes;
  uint32_t msgType;
  int64_t  seqNum;
  bool     isWeak;
  uint32_t dataLen;
  char     data[];
M
Minghao Li 已提交
98
} SyncClientRequest;
M
Minghao Li 已提交
99

M
Minghao Li 已提交
100
// ---------------------------------------------
M
Minghao Li 已提交
101
typedef struct SyncClientRequestReply {
M
Minghao Li 已提交
102 103 104 105
  uint32_t bytes;
  uint32_t msgType;
  int32_t  errCode;
  SRaftId  leaderHint;
M
Minghao Li 已提交
106
} SyncClientRequestReply;
M
Minghao Li 已提交
107

M
Minghao Li 已提交
108
// ---------------------------------------------
M
Minghao Li 已提交
109
typedef struct SyncRequestVote {
M
Minghao Li 已提交
110 111 112 113 114 115 116 117
  uint32_t bytes;
  uint32_t msgType;
  SRaftId  srcId;
  SRaftId  destId;
  // private data
  SyncTerm  currentTerm;
  SyncIndex lastLogIndex;
  SyncTerm  lastLogTerm;
M
Minghao Li 已提交
118
} SyncRequestVote;
M
Minghao Li 已提交
119

M
Minghao Li 已提交
120 121 122 123 124 125 126 127 128
SyncRequestVote* syncRequestVoteBuild();
void             syncRequestVoteDestroy(SyncRequestVote* pMsg);
void             syncRequestVoteSerialize(const SyncRequestVote* pMsg, char* buf, uint32_t bufLen);
void             syncRequestVoteDeserialize(const char* buf, uint32_t len, SyncRequestVote* pMsg);
void             syncRequestVote2RpcMsg(const SyncRequestVote* pMsg, SRpcMsg* pRpcMsg);
void             syncRequestVoteFromRpcMsg(const SRpcMsg* pRpcMsg, SyncRequestVote* pMsg);
cJSON*           syncRequestVote2Json(const SyncRequestVote* pMsg);

// ---------------------------------------------
M
Minghao Li 已提交
129
typedef struct SyncRequestVoteReply {
M
Minghao Li 已提交
130 131 132 133 134 135 136
  uint32_t bytes;
  uint32_t msgType;
  SRaftId  srcId;
  SRaftId  destId;
  // private data
  SyncTerm term;
  bool     voteGranted;
M
Minghao Li 已提交
137
} SyncRequestVoteReply;
M
Minghao Li 已提交
138

M
Minghao Li 已提交
139 140 141 142 143 144 145 146 147
SyncRequestVoteReply* SyncRequestVoteReplyBuild();
void                  syncRequestVoteReplyDestroy(SyncRequestVoteReply* pMsg);
void                  syncRequestVoteReplySerialize(const SyncRequestVoteReply* pMsg, char* buf, uint32_t bufLen);
void                  syncRequestVoteReplyDeserialize(const char* buf, uint32_t len, SyncRequestVoteReply* pMsg);
void                  syncRequestVoteReply2RpcMsg(const SyncRequestVoteReply* pMsg, SRpcMsg* pRpcMsg);
void                  syncRequestVoteReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncRequestVoteReply* pMsg);
cJSON*                syncRequestVoteReply2Json(const SyncRequestVoteReply* pMsg);

// ---------------------------------------------
M
Minghao Li 已提交
148
typedef struct SyncAppendEntries {
M
Minghao Li 已提交
149 150 151 152 153 154 155 156 157 158
  uint32_t bytes;
  uint32_t msgType;
  SRaftId  srcId;
  SRaftId  destId;
  // private data
  SyncIndex prevLogIndex;
  SyncTerm  prevLogTerm;
  SyncIndex commitIndex;
  uint32_t  dataLen;
  char      data[];
M
Minghao Li 已提交
159
} SyncAppendEntries;
M
Minghao Li 已提交
160

M
Minghao Li 已提交
161 162 163 164 165 166 167 168 169 170 171 172
#define SYNC_APPEND_ENTRIES_FIX_LEN                                                                                 \
  (sizeof(uint32_t) + sizeof(uint32_t) + sizeof(SRaftId) + sizeof(SRaftId) + sizeof(SyncIndex) + sizeof(SyncTerm) + \
   sizeof(SyncIndex) + sizeof(uint32_t))

SyncAppendEntries* syncAppendEntriesBuild(uint32_t dataLen);
void               syncAppendEntriesDestroy(SyncAppendEntries* pMsg);
void               syncAppendEntriesSerialize(const SyncAppendEntries* pMsg, char* buf, uint32_t bufLen);
void               syncAppendEntriesDeserialize(const char* buf, uint32_t len, SyncAppendEntries* pMsg);
void               syncAppendEntries2RpcMsg(const SyncAppendEntries* pMsg, SRpcMsg* pRpcMsg);
void               syncAppendEntriesFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntries* pMsg);
cJSON*             syncAppendEntries2Json(const SyncAppendEntries* pMsg);

M
Minghao Li 已提交
173
// ---------------------------------------------
M
Minghao Li 已提交
174
typedef struct SyncAppendEntriesReply {
M
Minghao Li 已提交
175 176 177 178 179 180 181
  uint32_t bytes;
  uint32_t msgType;
  SRaftId  srcId;
  SRaftId  destId;
  // private data
  bool      success;
  SyncIndex matchIndex;
M
Minghao Li 已提交
182 183
} SyncAppendEntriesReply;

M
Minghao Li 已提交
184 185 186 187 188 189 190 191
SyncAppendEntriesReply* syncAppendEntriesReplyBuild();
void                    syncAppendEntriesReplyDestroy(SyncAppendEntriesReply* pMsg);
void                    syncAppendEntriesReplySerialize(const SyncAppendEntriesReply* pMsg, char* buf, uint32_t bufLen);
void                    syncAppendEntriesReplyDeserialize(const char* buf, uint32_t len, SyncAppendEntriesReply* pMsg);
void                    syncAppendEntriesReply2RpcMsg(const SyncAppendEntriesReply* pMsg, SRpcMsg* pRpcMsg);
void                    syncAppendEntriesReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntriesReply* pMsg);
cJSON*                  syncAppendEntriesReply2Json(const SyncAppendEntriesReply* pMsg);

M
Minghao Li 已提交
192 193 194 195 196
#ifdef __cplusplus
}
#endif

#endif /*_TD_LIBS_SYNC_MESSAGE_H*/