syncMessage.c 112.2 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

#include "syncMessage.h"
17
#include "syncRaftCfg.h"
18
#include "syncRaftEntry.h"
M
Minghao Li 已提交
19
#include "syncUtil.h"
M
Minghao Li 已提交
20
#include "tcoding.h"
M
Minghao Li 已提交
21

M
Minghao Li 已提交
22 23 24 25
// ---------------------------------------------
cJSON* syncRpcMsg2Json(SRpcMsg* pRpcMsg) {
  cJSON* pRoot;

M
Minghao Li 已提交
26
  // in compiler optimization, switch case = if else constants
27
  if (pRpcMsg->msgType == TDMT_SYNC_TIMEOUT) {
M
Minghao Li 已提交
28
    SyncTimeout* pSyncMsg = syncTimeoutDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
29
    pRoot = syncTimeout2Json(pSyncMsg);
M
Minghao Li 已提交
30
    syncTimeoutDestroy(pSyncMsg);
M
Minghao Li 已提交
31

32
  } else if (pRpcMsg->msgType == TDMT_SYNC_PING) {
M
Minghao Li 已提交
33
    SyncPing* pSyncMsg = syncPingDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
34
    pRoot = syncPing2Json(pSyncMsg);
M
Minghao Li 已提交
35
    syncPingDestroy(pSyncMsg);
M
Minghao Li 已提交
36

37
  } else if (pRpcMsg->msgType == TDMT_SYNC_PING_REPLY) {
M
Minghao Li 已提交
38
    SyncPingReply* pSyncMsg = syncPingReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
39
    pRoot = syncPingReply2Json(pSyncMsg);
M
Minghao Li 已提交
40
    syncPingReplyDestroy(pSyncMsg);
M
Minghao Li 已提交
41

42
  } else if (pRpcMsg->msgType == TDMT_SYNC_CLIENT_REQUEST) {
M
Minghao Li 已提交
43
    SyncClientRequest* pSyncMsg = syncClientRequestDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
44
    pRoot = syncClientRequest2Json(pSyncMsg);
M
Minghao Li 已提交
45
    syncClientRequestDestroy(pSyncMsg);
M
Minghao Li 已提交
46

47
  } else if (pRpcMsg->msgType == TDMT_SYNC_CLIENT_REQUEST_REPLY) {
M
Minghao Li 已提交
48 49
    pRoot = syncRpcUnknownMsg2Json();

50
  } else if (pRpcMsg->msgType == TDMT_SYNC_REQUEST_VOTE) {
M
Minghao Li 已提交
51
    SyncRequestVote* pSyncMsg = syncRequestVoteDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
52
    pRoot = syncRequestVote2Json(pSyncMsg);
M
Minghao Li 已提交
53
    syncRequestVoteDestroy(pSyncMsg);
M
Minghao Li 已提交
54

55
  } else if (pRpcMsg->msgType == TDMT_SYNC_REQUEST_VOTE_REPLY) {
M
Minghao Li 已提交
56
    SyncRequestVoteReply* pSyncMsg = syncRequestVoteReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
57
    pRoot = syncRequestVoteReply2Json(pSyncMsg);
M
Minghao Li 已提交
58
    syncRequestVoteReplyDestroy(pSyncMsg);
M
Minghao Li 已提交
59

60
  } else if (pRpcMsg->msgType == TDMT_SYNC_APPEND_ENTRIES) {
M
Minghao Li 已提交
61
    SyncAppendEntries* pSyncMsg = syncAppendEntriesDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
62
    pRoot = syncAppendEntries2Json(pSyncMsg);
M
Minghao Li 已提交
63
    syncAppendEntriesDestroy(pSyncMsg);
M
Minghao Li 已提交
64

65
  } else if (pRpcMsg->msgType == TDMT_SYNC_APPEND_ENTRIES_REPLY) {
M
Minghao Li 已提交
66
    SyncAppendEntriesReply* pSyncMsg = syncAppendEntriesReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
67
    pRoot = syncAppendEntriesReply2Json(pSyncMsg);
M
Minghao Li 已提交
68
    syncAppendEntriesReplyDestroy(pSyncMsg);
M
Minghao Li 已提交
69

70
  } else if (pRpcMsg->msgType == TDMT_SYNC_SNAPSHOT_SEND) {
M
Minghao Li 已提交
71 72 73 74
    SyncSnapshotSend* pSyncMsg = syncSnapshotSendDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
    pRoot = syncSnapshotSend2Json(pSyncMsg);
    syncSnapshotSendDestroy(pSyncMsg);

75
  } else if (pRpcMsg->msgType == TDMT_SYNC_SNAPSHOT_RSP) {
M
Minghao Li 已提交
76 77 78 79
    SyncSnapshotRsp* pSyncMsg = syncSnapshotRspDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
    pRoot = syncSnapshotRsp2Json(pSyncMsg);
    syncSnapshotRspDestroy(pSyncMsg);

80 81 82 83 84
  } else if (pRpcMsg->msgType == TDMT_SYNC_LEADER_TRANSFER) {
    SyncLeaderTransfer* pSyncMsg = syncLeaderTransferDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
    pRoot = syncLeaderTransfer2Json(pSyncMsg);
    syncLeaderTransferDestroy(pSyncMsg);

85
  } else if (pRpcMsg->msgType == TDMT_SYNC_COMMON_RESPONSE) {
M
Minghao Li 已提交
86 87
    pRoot = cJSON_CreateObject();
    char* s;
S
Shengliang Guan 已提交
88
    s = syncUtilPrintBin((char*)(pRpcMsg->pCont), pRpcMsg->contLen);
M
Minghao Li 已提交
89
    cJSON_AddStringToObject(pRoot, "pCont", s);
wafwerar's avatar
wafwerar 已提交
90
    taosMemoryFree(s);
S
Shengliang Guan 已提交
91
    s = syncUtilPrintBin2((char*)(pRpcMsg->pCont), pRpcMsg->contLen);
M
Minghao Li 已提交
92
    cJSON_AddStringToObject(pRoot, "pCont2", s);
wafwerar's avatar
wafwerar 已提交
93
    taosMemoryFree(s);
M
Minghao Li 已提交
94

M
Minghao Li 已提交
95
  } else {
M
Minghao Li 已提交
96
    pRoot = cJSON_CreateObject();
M
Minghao Li 已提交
97
    char* s;
S
Shengliang Guan 已提交
98
    s = syncUtilPrintBin((char*)(pRpcMsg->pCont), pRpcMsg->contLen);
M
Minghao Li 已提交
99
    cJSON_AddStringToObject(pRoot, "pCont", s);
wafwerar's avatar
wafwerar 已提交
100
    taosMemoryFree(s);
S
Shengliang Guan 已提交
101
    s = syncUtilPrintBin2((char*)(pRpcMsg->pCont), pRpcMsg->contLen);
M
Minghao Li 已提交
102
    cJSON_AddStringToObject(pRoot, "pCont2", s);
wafwerar's avatar
wafwerar 已提交
103
    taosMemoryFree(s);
M
Minghao Li 已提交
104 105
  }

M
Minghao Li 已提交
106 107 108
  cJSON_AddNumberToObject(pRoot, "msgType", pRpcMsg->msgType);
  cJSON_AddNumberToObject(pRoot, "contLen", pRpcMsg->contLen);
  cJSON_AddNumberToObject(pRoot, "code", pRpcMsg->code);
M
Minghao Li 已提交
109
  // cJSON_AddNumberToObject(pRoot, "persist", pRpcMsg->persist);
M
Minghao Li 已提交
110

M
Minghao Li 已提交
111 112 113 114 115 116 117
  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "RpcMsg", pRoot);
  return pJson;
}

cJSON* syncRpcUnknownMsg2Json() {
  cJSON* pRoot = cJSON_CreateObject();
118
  cJSON_AddNumberToObject(pRoot, "msgType", TDMT_SYNC_UNKNOWN);
M
Minghao Li 已提交
119
  cJSON_AddStringToObject(pRoot, "data", "unknown message");
M
Minghao Li 已提交
120 121

  cJSON* pJson = cJSON_CreateObject();
M
Minghao Li 已提交
122
  cJSON_AddItemToObject(pJson, "SyncUnknown", pRoot);
M
Minghao Li 已提交
123 124 125
  return pJson;
}

M
Minghao Li 已提交
126 127 128 129 130 131 132
char* syncRpcMsg2Str(SRpcMsg* pRpcMsg) {
  cJSON* pJson = syncRpcMsg2Json(pRpcMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

M
Minghao Li 已提交
133 134 135
// for debug ----------------------
void syncRpcMsgPrint(SRpcMsg* pMsg) {
  char* serialized = syncRpcMsg2Str(pMsg);
S
Shengliang Guan 已提交
136
  printf("syncRpcMsgPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
137
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
138
  taosMemoryFree(serialized);
M
Minghao Li 已提交
139 140 141 142
}

void syncRpcMsgPrint2(char* s, SRpcMsg* pMsg) {
  char* serialized = syncRpcMsg2Str(pMsg);
S
Shengliang Guan 已提交
143
  printf("syncRpcMsgPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
144
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
145
  taosMemoryFree(serialized);
M
Minghao Li 已提交
146 147 148 149
}

void syncRpcMsgLog(SRpcMsg* pMsg) {
  char* serialized = syncRpcMsg2Str(pMsg);
S
Shengliang Guan 已提交
150
  sTrace("syncRpcMsgLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
151
  taosMemoryFree(serialized);
M
Minghao Li 已提交
152 153 154
}

void syncRpcMsgLog2(char* s, SRpcMsg* pMsg) {
M
Minghao Li 已提交
155 156
  if (gRaftDetailLog) {
    char* serialized = syncRpcMsg2Str(pMsg);
S
Shengliang Guan 已提交
157
    sTrace("syncRpcMsgLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
158 159
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
160 161
}

M
Minghao Li 已提交
162 163 164
// ---- message process SyncTimeout----
SyncTimeout* syncTimeoutBuild() {
  uint32_t     bytes = sizeof(SyncTimeout);
wafwerar's avatar
wafwerar 已提交
165
  SyncTimeout* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
166 167
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
168
  pMsg->msgType = TDMT_SYNC_TIMEOUT;
M
Minghao Li 已提交
169 170 171
  return pMsg;
}

M
Minghao Li 已提交
172 173
SyncTimeout* syncTimeoutBuild2(ESyncTimeoutType timeoutType, uint64_t logicClock, int32_t timerMS, int32_t vgId,
                               void* data) {
M
Minghao Li 已提交
174
  SyncTimeout* pMsg = syncTimeoutBuild();
M
Minghao Li 已提交
175
  pMsg->vgId = vgId;
M
Minghao Li 已提交
176 177 178 179 180 181 182
  pMsg->timeoutType = timeoutType;
  pMsg->logicClock = logicClock;
  pMsg->timerMS = timerMS;
  pMsg->data = data;
  return pMsg;
}

M
Minghao Li 已提交
183 184
void syncTimeoutDestroy(SyncTimeout* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
185
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
186 187 188 189
  }
}

void syncTimeoutSerialize(const SyncTimeout* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
190
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
191 192 193 194 195
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncTimeoutDeserialize(const char* buf, uint32_t len, SyncTimeout* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
196
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
197 198
}

M
Minghao Li 已提交
199
char* syncTimeoutSerialize2(const SyncTimeout* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
200
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
201
  ASSERT(buf != NULL);
M
Minghao Li 已提交
202 203 204 205 206 207 208 209 210
  syncTimeoutSerialize(pMsg, buf, pMsg->bytes);
  if (len != NULL) {
    *len = pMsg->bytes;
  }
  return buf;
}

SyncTimeout* syncTimeoutDeserialize2(const char* buf, uint32_t len) {
  uint32_t     bytes = *((uint32_t*)buf);
wafwerar's avatar
wafwerar 已提交
211
  SyncTimeout* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
212
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
213
  syncTimeoutDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
214
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
215 216 217
  return pMsg;
}

M
Minghao Li 已提交
218 219 220 221 222 223 224 225 226 227 228 229
void syncTimeout2RpcMsg(const SyncTimeout* pMsg, SRpcMsg* pRpcMsg) {
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
  pRpcMsg->msgType = pMsg->msgType;
  pRpcMsg->contLen = pMsg->bytes;
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
  syncTimeoutSerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen);
}

void syncTimeoutFromRpcMsg(const SRpcMsg* pRpcMsg, SyncTimeout* pMsg) {
  syncTimeoutDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
}

M
Minghao Li 已提交
230 231
SyncTimeout* syncTimeoutFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncTimeout* pMsg = syncTimeoutDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
232
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
233 234 235
  return pMsg;
}

M
Minghao Li 已提交
236
cJSON* syncTimeout2Json(const SyncTimeout* pMsg) {
C
Cary Xu 已提交
237
  char   u64buf[128] = {0};
M
Minghao Li 已提交
238
  cJSON* pRoot = cJSON_CreateObject();
M
Minghao Li 已提交
239 240 241

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
242
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
243 244
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);
    cJSON_AddNumberToObject(pRoot, "timeoutType", pMsg->timeoutType);
S
Shengliang Guan 已提交
245
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->logicClock);
M
Minghao Li 已提交
246 247 248 249 250
    cJSON_AddStringToObject(pRoot, "logicClock", u64buf);
    cJSON_AddNumberToObject(pRoot, "timerMS", pMsg->timerMS);
    snprintf(u64buf, sizeof(u64buf), "%p", pMsg->data);
    cJSON_AddStringToObject(pRoot, "data", u64buf);
  }
M
Minghao Li 已提交
251 252 253 254 255 256

  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncTimeout", pRoot);
  return pJson;
}

M
Minghao Li 已提交
257 258 259 260 261 262 263 264 265 266
char* syncTimeout2Str(const SyncTimeout* pMsg) {
  cJSON* pJson = syncTimeout2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

// for debug ----------------------
void syncTimeoutPrint(const SyncTimeout* pMsg) {
  char* serialized = syncTimeout2Str(pMsg);
267
  printf("syncTimeoutPrint | len:%zu | %s \n", strlen(serialized), serialized);
M
Minghao Li 已提交
268
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
269
  taosMemoryFree(serialized);
M
Minghao Li 已提交
270 271 272 273
}

void syncTimeoutPrint2(char* s, const SyncTimeout* pMsg) {
  char* serialized = syncTimeout2Str(pMsg);
S
Shengliang Guan 已提交
274
  printf("syncTimeoutPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
275
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
276
  taosMemoryFree(serialized);
M
Minghao Li 已提交
277 278 279 280
}

void syncTimeoutLog(const SyncTimeout* pMsg) {
  char* serialized = syncTimeout2Str(pMsg);
S
Shengliang Guan 已提交
281
  sTrace("syncTimeoutLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
282
  taosMemoryFree(serialized);
M
Minghao Li 已提交
283 284 285
}

void syncTimeoutLog2(char* s, const SyncTimeout* pMsg) {
M
Minghao Li 已提交
286 287
  if (gRaftDetailLog) {
    char* serialized = syncTimeout2Str(pMsg);
S
Shengliang Guan 已提交
288
    sTrace("syncTimeoutLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
289 290
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
291 292
}

M
Minghao Li 已提交
293
// ---- message process SyncPing----
M
Minghao Li 已提交
294
SyncPing* syncPingBuild(uint32_t dataLen) {
M
Minghao Li 已提交
295
  uint32_t  bytes = sizeof(SyncPing) + dataLen;
wafwerar's avatar
wafwerar 已提交
296
  SyncPing* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
297 298
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
299
  pMsg->msgType = TDMT_SYNC_PING;
M
Minghao Li 已提交
300
  pMsg->dataLen = dataLen;
M
Minghao Li 已提交
301
  return pMsg;
M
Minghao Li 已提交
302 303
}

M
Minghao Li 已提交
304
SyncPing* syncPingBuild2(const SRaftId* srcId, const SRaftId* destId, int32_t vgId, const char* str) {
M
Minghao Li 已提交
305 306
  uint32_t  dataLen = strlen(str) + 1;
  SyncPing* pMsg = syncPingBuild(dataLen);
M
Minghao Li 已提交
307
  pMsg->vgId = vgId;
M
Minghao Li 已提交
308 309 310 311 312 313
  pMsg->srcId = *srcId;
  pMsg->destId = *destId;
  snprintf(pMsg->data, pMsg->dataLen, "%s", str);
  return pMsg;
}

M
Minghao Li 已提交
314 315
SyncPing* syncPingBuild3(const SRaftId* srcId, const SRaftId* destId, int32_t vgId) {
  SyncPing* pMsg = syncPingBuild2(srcId, destId, vgId, "ping");
M
Minghao Li 已提交
316 317 318
  return pMsg;
}

M
Minghao Li 已提交
319 320
void syncPingDestroy(SyncPing* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
321
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
322 323 324
  }
}

M
Minghao Li 已提交
325
void syncPingSerialize(const SyncPing* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
326
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
327
  memcpy(buf, pMsg, pMsg->bytes);
M
Minghao Li 已提交
328 329
}

M
Minghao Li 已提交
330 331
void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
332 333
  ASSERT(len == pMsg->bytes);
  ASSERT(pMsg->bytes == sizeof(SyncPing) + pMsg->dataLen);
M
Minghao Li 已提交
334 335
}

M
Minghao Li 已提交
336
char* syncPingSerialize2(const SyncPing* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
337
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
338
  ASSERT(buf != NULL);
M
Minghao Li 已提交
339 340 341 342 343 344 345 346 347
  syncPingSerialize(pMsg, buf, pMsg->bytes);
  if (len != NULL) {
    *len = pMsg->bytes;
  }
  return buf;
}

SyncPing* syncPingDeserialize2(const char* buf, uint32_t len) {
  uint32_t  bytes = *((uint32_t*)buf);
wafwerar's avatar
wafwerar 已提交
348
  SyncPing* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
349
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
350
  syncPingDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
351
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
352 353 354
  return pMsg;
}

M
Minghao Li 已提交
355
int32_t syncPingSerialize3(const SyncPing* pMsg, char* buf, int32_t bufLen) {
H
Hongze Cheng 已提交
356 357
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
M
Minghao Li 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
  if (tStartEncode(&encoder) < 0) {
    return -1;
  }

  if (tEncodeU32(&encoder, pMsg->bytes) < 0) {
    return -1;
  }
  if (tEncodeI32(&encoder, pMsg->vgId) < 0) {
    return -1;
  }
  if (tEncodeU32(&encoder, pMsg->msgType) < 0) {
    return -1;
  }
  if (tEncodeU64(&encoder, pMsg->srcId.addr) < 0) {
    return -1;
  }
  if (tEncodeI32(&encoder, pMsg->srcId.vgId) < 0) {
    return -1;
  }
  if (tEncodeU64(&encoder, pMsg->destId.addr) < 0) {
    return -1;
  }
  if (tEncodeI32(&encoder, pMsg->destId.vgId) < 0) {
    return -1;
  }
  if (tEncodeU32(&encoder, pMsg->dataLen) < 0) {
    return -1;
  }
  if (tEncodeBinary(&encoder, pMsg->data, pMsg->dataLen)) {
    return -1;
  }

  tEndEncode(&encoder);
  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
392
  tEncoderClear(&encoder);
M
Minghao Li 已提交
393 394 395 396
  return tlen;
}

SyncPing* syncPingDeserialize3(void* buf, int32_t bufLen) {
H
Hongze Cheng 已提交
397 398
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
M
Minghao Li 已提交
399 400 401 402 403 404 405 406 407 408 409
  if (tStartDecode(&decoder) < 0) {
    return NULL;
  }

  SyncPing* pMsg = NULL;
  uint32_t  bytes;
  if (tDecodeU32(&decoder, &bytes) < 0) {
    return NULL;
  }

  pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
410
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
411 412 413
  pMsg->bytes = bytes;

  if (tDecodeI32(&decoder, &pMsg->vgId) < 0) {
M
Minghao Li 已提交
414
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
415 416 417
    return NULL;
  }
  if (tDecodeU32(&decoder, &pMsg->msgType) < 0) {
M
Minghao Li 已提交
418
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
419 420 421
    return NULL;
  }
  if (tDecodeU64(&decoder, &pMsg->srcId.addr) < 0) {
M
Minghao Li 已提交
422
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
423 424 425
    return NULL;
  }
  if (tDecodeI32(&decoder, &pMsg->srcId.vgId) < 0) {
M
Minghao Li 已提交
426
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
427 428 429
    return NULL;
  }
  if (tDecodeU64(&decoder, &pMsg->destId.addr) < 0) {
M
Minghao Li 已提交
430
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
431 432 433
    return NULL;
  }
  if (tDecodeI32(&decoder, &pMsg->destId.vgId) < 0) {
M
Minghao Li 已提交
434
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
435 436 437
    return NULL;
  }
  if (tDecodeU32(&decoder, &pMsg->dataLen) < 0) {
M
Minghao Li 已提交
438
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
439 440
    return NULL;
  }
H
Hongze Cheng 已提交
441
  uint32_t len;
M
Minghao Li 已提交
442
  char*    data = NULL;
H
Hongze Cheng 已提交
443
  if (tDecodeBinary(&decoder, (uint8_t**)(&data), &len) < 0) {
M
Minghao Li 已提交
444
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
445 446
    return NULL;
  }
M
Minghao Li 已提交
447
  ASSERT(len == pMsg->dataLen);
M
Minghao Li 已提交
448 449 450
  memcpy(pMsg->data, data, len);

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
451
  tDecoderClear(&decoder);
M
Minghao Li 已提交
452 453 454
  return pMsg;
}

M
Minghao Li 已提交
455
void syncPing2RpcMsg(const SyncPing* pMsg, SRpcMsg* pRpcMsg) {
M
Minghao Li 已提交
456
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
M
Minghao Li 已提交
457 458
  pRpcMsg->msgType = pMsg->msgType;
  pRpcMsg->contLen = pMsg->bytes;
M
Minghao Li 已提交
459
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
M
Minghao Li 已提交
460
  syncPingSerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
461 462
}

M
Minghao Li 已提交
463 464
void syncPingFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPing* pMsg) {
  syncPingDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
M
Minghao Li 已提交
465 466
}

M
Minghao Li 已提交
467 468
SyncPing* syncPingFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncPing* pMsg = syncPingDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
469
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
470 471 472
  return pMsg;
}

M
Minghao Li 已提交
473
cJSON* syncPing2Json(const SyncPing* pMsg) {
474
  char   u64buf[128] = {0};
M
Minghao Li 已提交
475
  cJSON* pRoot = cJSON_CreateObject();
M
Minghao Li 已提交
476 477 478

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
479
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
480 481 482
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
483
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
M
Minghao Li 已提交
484 485 486 487
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
488
      char     host[128] = {0};
M
Minghao Li 已提交
489 490 491 492 493 494 495 496 497
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

    cJSON* pDestId = cJSON_CreateObject();
S
Shengliang Guan 已提交
498
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
M
Minghao Li 已提交
499 500 501 502
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
503
      char     host[128] = {0};
M
Minghao Li 已提交
504 505 506 507 508 509 510 511 512 513
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
    cJSON_AddItemToObject(pRoot, "destId", pDestId);

    cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen);
    char* s;
S
Shengliang Guan 已提交
514
    s = syncUtilPrintBin((char*)(pMsg->data), pMsg->dataLen);
M
Minghao Li 已提交
515
    cJSON_AddStringToObject(pRoot, "data", s);
wafwerar's avatar
wafwerar 已提交
516
    taosMemoryFree(s);
S
Shengliang Guan 已提交
517
    s = syncUtilPrintBin2((char*)(pMsg->data), pMsg->dataLen);
M
Minghao Li 已提交
518
    cJSON_AddStringToObject(pRoot, "data2", s);
wafwerar's avatar
wafwerar 已提交
519
    taosMemoryFree(s);
M
Minghao Li 已提交
520
  }
M
Minghao Li 已提交
521

M
Minghao Li 已提交
522 523 524 525 526
  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncPing", pRoot);
  return pJson;
}

M
Minghao Li 已提交
527 528 529 530 531
char* syncPing2Str(const SyncPing* pMsg) {
  cJSON* pJson = syncPing2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
M
Minghao Li 已提交
532 533
}

M
Minghao Li 已提交
534 535 536
// for debug ----------------------
void syncPingPrint(const SyncPing* pMsg) {
  char* serialized = syncPing2Str(pMsg);
S
Shengliang Guan 已提交
537
  printf("syncPingPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
538
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
539
  taosMemoryFree(serialized);
M
Minghao Li 已提交
540 541 542 543
}

void syncPingPrint2(char* s, const SyncPing* pMsg) {
  char* serialized = syncPing2Str(pMsg);
S
Shengliang Guan 已提交
544
  printf("syncPingPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
545
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
546
  taosMemoryFree(serialized);
M
Minghao Li 已提交
547 548 549 550
}

void syncPingLog(const SyncPing* pMsg) {
  char* serialized = syncPing2Str(pMsg);
S
Shengliang Guan 已提交
551
  sTrace("syncPingLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
552
  taosMemoryFree(serialized);
M
Minghao Li 已提交
553 554 555
}

void syncPingLog2(char* s, const SyncPing* pMsg) {
M
Minghao Li 已提交
556 557
  if (gRaftDetailLog) {
    char* serialized = syncPing2Str(pMsg);
S
Shengliang Guan 已提交
558
    sTrace("syncPingLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
559 560
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
561 562
}

M
Minghao Li 已提交
563 564
// ---- message process SyncPingReply----
SyncPingReply* syncPingReplyBuild(uint32_t dataLen) {
M
Minghao Li 已提交
565
  uint32_t       bytes = sizeof(SyncPingReply) + dataLen;
wafwerar's avatar
wafwerar 已提交
566
  SyncPingReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
567 568
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
569
  pMsg->msgType = TDMT_SYNC_PING_REPLY;
M
Minghao Li 已提交
570
  pMsg->dataLen = dataLen;
M
Minghao Li 已提交
571
  return pMsg;
M
Minghao Li 已提交
572 573
}

M
Minghao Li 已提交
574
SyncPingReply* syncPingReplyBuild2(const SRaftId* srcId, const SRaftId* destId, int32_t vgId, const char* str) {
M
Minghao Li 已提交
575 576
  uint32_t       dataLen = strlen(str) + 1;
  SyncPingReply* pMsg = syncPingReplyBuild(dataLen);
M
Minghao Li 已提交
577
  pMsg->vgId = vgId;
M
Minghao Li 已提交
578 579 580 581 582 583
  pMsg->srcId = *srcId;
  pMsg->destId = *destId;
  snprintf(pMsg->data, pMsg->dataLen, "%s", str);
  return pMsg;
}

M
Minghao Li 已提交
584 585
SyncPingReply* syncPingReplyBuild3(const SRaftId* srcId, const SRaftId* destId, int32_t vgId) {
  SyncPingReply* pMsg = syncPingReplyBuild2(srcId, destId, vgId, "pang");
M
Minghao Li 已提交
586 587 588
  return pMsg;
}

M
Minghao Li 已提交
589 590
void syncPingReplyDestroy(SyncPingReply* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
591
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
592 593 594 595
  }
}

void syncPingReplySerialize(const SyncPingReply* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
596
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
597 598 599 600 601
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncPingReplyDeserialize(const char* buf, uint32_t len, SyncPingReply* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
602
  ASSERT(len == pMsg->bytes);
603
  ASSERT(pMsg->bytes == sizeof(SyncPingReply) + pMsg->dataLen);
M
Minghao Li 已提交
604 605
}

M
Minghao Li 已提交
606
char* syncPingReplySerialize2(const SyncPingReply* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
607
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
608
  ASSERT(buf != NULL);
M
Minghao Li 已提交
609 610 611 612 613 614 615 616 617
  syncPingReplySerialize(pMsg, buf, pMsg->bytes);
  if (len != NULL) {
    *len = pMsg->bytes;
  }
  return buf;
}

SyncPingReply* syncPingReplyDeserialize2(const char* buf, uint32_t len) {
  uint32_t       bytes = *((uint32_t*)buf);
wafwerar's avatar
wafwerar 已提交
618
  SyncPingReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
619
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
620
  syncPingReplyDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
621
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
622 623 624
  return pMsg;
}

M
Minghao Li 已提交
625
int32_t syncPingReplySerialize3(const SyncPingReply* pMsg, char* buf, int32_t bufLen) {
H
Hongze Cheng 已提交
626 627
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
M
Minghao Li 已提交
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
  if (tStartEncode(&encoder) < 0) {
    return -1;
  }

  if (tEncodeU32(&encoder, pMsg->bytes) < 0) {
    return -1;
  }
  if (tEncodeI32(&encoder, pMsg->vgId) < 0) {
    return -1;
  }
  if (tEncodeU32(&encoder, pMsg->msgType) < 0) {
    return -1;
  }
  if (tEncodeU64(&encoder, pMsg->srcId.addr) < 0) {
    return -1;
  }
  if (tEncodeI32(&encoder, pMsg->srcId.vgId) < 0) {
    return -1;
  }
  if (tEncodeU64(&encoder, pMsg->destId.addr) < 0) {
    return -1;
  }
  if (tEncodeI32(&encoder, pMsg->destId.vgId) < 0) {
    return -1;
  }
  if (tEncodeU32(&encoder, pMsg->dataLen) < 0) {
    return -1;
  }
  if (tEncodeBinary(&encoder, pMsg->data, pMsg->dataLen)) {
    return -1;
  }

  tEndEncode(&encoder);
  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
662
  tEncoderClear(&encoder);
M
Minghao Li 已提交
663 664 665 666
  return tlen;
}

SyncPingReply* syncPingReplyDeserialize3(void* buf, int32_t bufLen) {
H
Hongze Cheng 已提交
667 668
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
M
Minghao Li 已提交
669 670 671 672 673 674 675 676 677 678 679
  if (tStartDecode(&decoder) < 0) {
    return NULL;
  }

  SyncPingReply* pMsg = NULL;
  uint32_t       bytes;
  if (tDecodeU32(&decoder, &bytes) < 0) {
    return NULL;
  }

  pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
680
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
681 682 683
  pMsg->bytes = bytes;

  if (tDecodeI32(&decoder, &pMsg->vgId) < 0) {
M
Minghao Li 已提交
684
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
685 686 687
    return NULL;
  }
  if (tDecodeU32(&decoder, &pMsg->msgType) < 0) {
M
Minghao Li 已提交
688
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
689 690 691
    return NULL;
  }
  if (tDecodeU64(&decoder, &pMsg->srcId.addr) < 0) {
M
Minghao Li 已提交
692
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
693 694 695
    return NULL;
  }
  if (tDecodeI32(&decoder, &pMsg->srcId.vgId) < 0) {
M
Minghao Li 已提交
696
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
697 698 699
    return NULL;
  }
  if (tDecodeU64(&decoder, &pMsg->destId.addr) < 0) {
M
Minghao Li 已提交
700
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
701 702 703
    return NULL;
  }
  if (tDecodeI32(&decoder, &pMsg->destId.vgId) < 0) {
M
Minghao Li 已提交
704
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
705 706 707
    return NULL;
  }
  if (tDecodeU32(&decoder, &pMsg->dataLen) < 0) {
M
Minghao Li 已提交
708
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
709 710
    return NULL;
  }
H
Hongze Cheng 已提交
711
  uint32_t len;
M
Minghao Li 已提交
712
  char*    data = NULL;
H
Hongze Cheng 已提交
713
  if (tDecodeBinary(&decoder, (uint8_t**)(&data), &len) < 0) {
M
Minghao Li 已提交
714
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
715 716
    return NULL;
  }
M
Minghao Li 已提交
717
  ASSERT(len == pMsg->dataLen);
M
Minghao Li 已提交
718 719 720
  memcpy(pMsg->data, data, len);

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
721
  tDecoderClear(&decoder);
M
Minghao Li 已提交
722 723 724
  return pMsg;
}

M
Minghao Li 已提交
725
void syncPingReply2RpcMsg(const SyncPingReply* pMsg, SRpcMsg* pRpcMsg) {
M
Minghao Li 已提交
726
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
M
Minghao Li 已提交
727 728 729 730 731 732 733 734 735 736
  pRpcMsg->msgType = pMsg->msgType;
  pRpcMsg->contLen = pMsg->bytes;
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
  syncPingReplySerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen);
}

void syncPingReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPingReply* pMsg) {
  syncPingReplyDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
}

M
Minghao Li 已提交
737 738
SyncPingReply* syncPingReplyFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncPingReply* pMsg = syncPingReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
739
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
740 741 742
  return pMsg;
}

M
Minghao Li 已提交
743
cJSON* syncPingReply2Json(const SyncPingReply* pMsg) {
744
  char   u64buf[128] = {0};
M
Minghao Li 已提交
745
  cJSON* pRoot = cJSON_CreateObject();
M
Minghao Li 已提交
746 747 748

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
749
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
750 751 752
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
753
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
M
Minghao Li 已提交
754 755 756 757
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
758
      char     host[128] = {0};
M
Minghao Li 已提交
759 760 761 762 763 764 765 766 767
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

    cJSON* pDestId = cJSON_CreateObject();
S
Shengliang Guan 已提交
768
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
M
Minghao Li 已提交
769 770 771 772
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
773
      char     host[128] = {0};
M
Minghao Li 已提交
774 775 776 777 778 779 780 781 782 783
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
    cJSON_AddItemToObject(pRoot, "destId", pDestId);

    cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen);
    char* s;
S
Shengliang Guan 已提交
784
    s = syncUtilPrintBin((char*)(pMsg->data), pMsg->dataLen);
M
Minghao Li 已提交
785
    cJSON_AddStringToObject(pRoot, "data", s);
wafwerar's avatar
wafwerar 已提交
786
    taosMemoryFree(s);
S
Shengliang Guan 已提交
787
    s = syncUtilPrintBin2((char*)(pMsg->data), pMsg->dataLen);
M
Minghao Li 已提交
788
    cJSON_AddStringToObject(pRoot, "data2", s);
wafwerar's avatar
wafwerar 已提交
789
    taosMemoryFree(s);
M
Minghao Li 已提交
790
  }
M
Minghao Li 已提交
791 792 793 794

  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncPingReply", pRoot);
  return pJson;
M
Minghao Li 已提交
795 796
}

M
Minghao Li 已提交
797 798 799 800 801
char* syncPingReply2Str(const SyncPingReply* pMsg) {
  cJSON* pJson = syncPingReply2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
M
Minghao Li 已提交
802 803
}

M
Minghao Li 已提交
804 805 806
// for debug ----------------------
void syncPingReplyPrint(const SyncPingReply* pMsg) {
  char* serialized = syncPingReply2Str(pMsg);
807
  printf("syncPingReplyPrint | len:%zu | %s \n", strlen(serialized), serialized);
M
Minghao Li 已提交
808
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
809
  taosMemoryFree(serialized);
M
Minghao Li 已提交
810 811 812 813
}

void syncPingReplyPrint2(char* s, const SyncPingReply* pMsg) {
  char* serialized = syncPingReply2Str(pMsg);
814
  printf("syncPingReplyPrint2 | len:%zu | %s | %s \n", strlen(serialized), s, serialized);
M
Minghao Li 已提交
815
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
816
  taosMemoryFree(serialized);
M
Minghao Li 已提交
817 818 819 820
}

void syncPingReplyLog(const SyncPingReply* pMsg) {
  char* serialized = syncPingReply2Str(pMsg);
821
  sTrace("syncPingReplyLog | len:%zu | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
822
  taosMemoryFree(serialized);
M
Minghao Li 已提交
823 824 825
}

void syncPingReplyLog2(char* s, const SyncPingReply* pMsg) {
M
Minghao Li 已提交
826 827 828 829 830
  if (gRaftDetailLog) {
    char* serialized = syncPingReply2Str(pMsg);
    sTrace("syncPingReplyLog2 | len:%zu | %s | %s", strlen(serialized), s, serialized);
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
831 832
}

M
Minghao Li 已提交
833
// ---- message process SyncClientRequest----
S
Shengliang Guan 已提交
834
SyncClientRequest* syncClientRequestAlloc(uint32_t dataLen) {
M
Minghao Li 已提交
835
  uint32_t           bytes = sizeof(SyncClientRequest) + dataLen;
S
Shengliang Guan 已提交
836
  SyncClientRequest* pMsg = taosMemoryCalloc(1, bytes);
M
Minghao Li 已提交
837
  pMsg->bytes = bytes;
838
  pMsg->msgType = TDMT_SYNC_CLIENT_REQUEST;
M
Minghao Li 已提交
839 840 841 842 843 844
  pMsg->seqNum = 0;
  pMsg->isWeak = false;
  pMsg->dataLen = dataLen;
  return pMsg;
}

M
Minghao Li 已提交
845
// step 1. original SRpcMsg => SyncClientRequest, add seqNum, isWeak
S
Shengliang Guan 已提交
846 847
SyncClientRequest* syncClientRequestBuild(const SRpcMsg* pOriginalRpcMsg, uint64_t seqNum, bool isWeak, int32_t vgId) {
  SyncClientRequest* pMsg = syncClientRequestAlloc(pOriginalRpcMsg->contLen);
M
Minghao Li 已提交
848
  pMsg->vgId = vgId;
M
Minghao Li 已提交
849 850 851 852 853 854 855
  pMsg->originalRpcType = pOriginalRpcMsg->msgType;
  pMsg->seqNum = seqNum;
  pMsg->isWeak = isWeak;
  memcpy(pMsg->data, pOriginalRpcMsg->pCont, pOriginalRpcMsg->contLen);
  return pMsg;
}

M
Minghao Li 已提交
856 857
void syncClientRequestDestroy(SyncClientRequest* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
858
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
859 860 861 862
  }
}

void syncClientRequestSerialize(const SyncClientRequest* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
863
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
864 865 866 867 868
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncClientRequestDeserialize(const char* buf, uint32_t len, SyncClientRequest* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
869
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
870 871
}

M
Minghao Li 已提交
872
char* syncClientRequestSerialize2(const SyncClientRequest* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
873
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
874
  ASSERT(buf != NULL);
M
Minghao Li 已提交
875 876 877 878 879 880 881 882 883
  syncClientRequestSerialize(pMsg, buf, pMsg->bytes);
  if (len != NULL) {
    *len = pMsg->bytes;
  }
  return buf;
}

SyncClientRequest* syncClientRequestDeserialize2(const char* buf, uint32_t len) {
  uint32_t           bytes = *((uint32_t*)buf);
wafwerar's avatar
wafwerar 已提交
884
  SyncClientRequest* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
885
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
886
  syncClientRequestDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
887
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
888 889 890
  return pMsg;
}

M
Minghao Li 已提交
891
// step 2. SyncClientRequest => RpcMsg, to queue
M
Minghao Li 已提交
892 893 894 895 896 897 898 899 900 901 902
void syncClientRequest2RpcMsg(const SyncClientRequest* pMsg, SRpcMsg* pRpcMsg) {
  pRpcMsg->msgType = pMsg->msgType;
  pRpcMsg->contLen = pMsg->bytes;
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
  syncClientRequestSerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen);
}

void syncClientRequestFromRpcMsg(const SRpcMsg* pRpcMsg, SyncClientRequest* pMsg) {
  syncClientRequestDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
}

M
Minghao Li 已提交
903
// step 3. RpcMsg => SyncClientRequest, from queue
M
Minghao Li 已提交
904 905
SyncClientRequest* syncClientRequestFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncClientRequest* pMsg = syncClientRequestDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
906
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
907 908 909
  return pMsg;
}

M
Minghao Li 已提交
910
cJSON* syncClientRequest2Json(const SyncClientRequest* pMsg) {
911
  char   u64buf[128] = {0};
M
Minghao Li 已提交
912
  cJSON* pRoot = cJSON_CreateObject();
M
Minghao Li 已提交
913 914 915

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
916
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
917 918
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);
    cJSON_AddNumberToObject(pRoot, "originalRpcType", pMsg->originalRpcType);
S
Shengliang Guan 已提交
919
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->seqNum);
M
Minghao Li 已提交
920 921 922 923 924
    cJSON_AddStringToObject(pRoot, "seqNum", u64buf);
    cJSON_AddNumberToObject(pRoot, "isWeak", pMsg->isWeak);
    cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen);

    char* s;
S
Shengliang Guan 已提交
925
    s = syncUtilPrintBin((char*)(pMsg->data), pMsg->dataLen);
M
Minghao Li 已提交
926
    cJSON_AddStringToObject(pRoot, "data", s);
wafwerar's avatar
wafwerar 已提交
927
    taosMemoryFree(s);
S
Shengliang Guan 已提交
928
    s = syncUtilPrintBin2((char*)(pMsg->data), pMsg->dataLen);
M
Minghao Li 已提交
929
    cJSON_AddStringToObject(pRoot, "data2", s);
wafwerar's avatar
wafwerar 已提交
930
    taosMemoryFree(s);
M
Minghao Li 已提交
931
  }
M
Minghao Li 已提交
932

M
Minghao Li 已提交
933 934 935 936 937
  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncClientRequest", pRoot);
  return pJson;
}

M
Minghao Li 已提交
938 939 940 941 942 943 944 945 946 947
char* syncClientRequest2Str(const SyncClientRequest* pMsg) {
  cJSON* pJson = syncClientRequest2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

// for debug ----------------------
void syncClientRequestPrint(const SyncClientRequest* pMsg) {
  char* serialized = syncClientRequest2Str(pMsg);
S
Shengliang Guan 已提交
948
  printf("syncClientRequestPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
949
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
950
  taosMemoryFree(serialized);
M
Minghao Li 已提交
951 952 953 954
}

void syncClientRequestPrint2(char* s, const SyncClientRequest* pMsg) {
  char* serialized = syncClientRequest2Str(pMsg);
S
Shengliang Guan 已提交
955
  printf("syncClientRequestPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
956
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
957
  taosMemoryFree(serialized);
M
Minghao Li 已提交
958 959 960 961
}

void syncClientRequestLog(const SyncClientRequest* pMsg) {
  char* serialized = syncClientRequest2Str(pMsg);
S
Shengliang Guan 已提交
962
  sTrace("syncClientRequestLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
963
  taosMemoryFree(serialized);
M
Minghao Li 已提交
964 965 966
}

void syncClientRequestLog2(char* s, const SyncClientRequest* pMsg) {
M
Minghao Li 已提交
967 968
  if (gRaftDetailLog) {
    char* serialized = syncClientRequest2Str(pMsg);
S
Shengliang Guan 已提交
969
    sTrace("syncClientRequestLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
970 971
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
972 973
}

974 975 976 977 978 979
// ---- message process SyncClientRequestBatch----

// block1:
// block2: SRaftMeta array
// block3: rpc msg array (with pCont)

980
SyncClientRequestBatch* syncClientRequestBatchBuild(SRpcMsg** rpcMsgPArr, SRaftMeta* raftArr, int32_t arrSize,
981
                                                    int32_t vgId) {
982
  ASSERT(rpcMsgPArr != NULL);
983 984 985
  ASSERT(arrSize > 0);

  int32_t dataLen = 0;
M
Minghao Li 已提交
986 987 988
  int32_t raftMetaArrayLen = sizeof(SRaftMeta) * arrSize;
  int32_t rpcArrayLen = sizeof(SRpcMsg) * arrSize;
  dataLen += (raftMetaArrayLen + rpcArrayLen);
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007

  uint32_t                bytes = sizeof(SyncClientRequestBatch) + dataLen;
  SyncClientRequestBatch* pMsg = taosMemoryMalloc(bytes);
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
  pMsg->vgId = vgId;
  pMsg->msgType = TDMT_SYNC_CLIENT_REQUEST_BATCH;
  pMsg->dataCount = arrSize;
  pMsg->dataLen = dataLen;

  SRaftMeta* raftMetaArr = (SRaftMeta*)(pMsg->data);
  SRpcMsg*   msgArr = (SRpcMsg*)((char*)(pMsg->data) + raftMetaArrayLen);

  for (int i = 0; i < arrSize; ++i) {
    // init raftMetaArr
    raftMetaArr[i].isWeak = raftArr[i].isWeak;
    raftMetaArr[i].seqNum = raftArr[i].seqNum;

    // init msgArr
1008
    msgArr[i] = *(rpcMsgPArr[i]);
1009 1010 1011 1012 1013
  }

  return pMsg;
}

1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
void syncClientRequestBatch2RpcMsg(const SyncClientRequestBatch* pSyncMsg, SRpcMsg* pRpcMsg) {
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
  pRpcMsg->msgType = pSyncMsg->msgType;
  pRpcMsg->contLen = pSyncMsg->bytes;
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
  memcpy(pRpcMsg->pCont, pSyncMsg, pRpcMsg->contLen);
}

void syncClientRequestBatchDestroy(SyncClientRequestBatch* pMsg) {
  if (pMsg != NULL) {
    taosMemoryFree(pMsg);
  }
}

void syncClientRequestBatchDestroyDeep(SyncClientRequestBatch* pMsg) {
  if (pMsg != NULL) {
    int32_t  arrSize = pMsg->dataCount;
    int32_t  raftMetaArrayLen = sizeof(SRaftMeta) * arrSize;
    SRpcMsg* msgArr = (SRpcMsg*)((char*)(pMsg->data) + raftMetaArrayLen);
    for (int i = 0; i < arrSize; ++i) {
      if (msgArr[i].pCont != NULL) {
        rpcFreeCont(msgArr[i].pCont);
      }
    }

    taosMemoryFree(pMsg);
  }
}

SRaftMeta* syncClientRequestBatchMetaArr(const SyncClientRequestBatch* pSyncMsg) {
  SRaftMeta* raftMetaArr = (SRaftMeta*)(pSyncMsg->data);
  return raftMetaArr;
}

SRpcMsg* syncClientRequestBatchRpcMsgArr(const SyncClientRequestBatch* pSyncMsg) {
  int32_t  arrSize = pSyncMsg->dataCount;
  int32_t  raftMetaArrayLen = sizeof(SRaftMeta) * arrSize;
  SRpcMsg* msgArr = (SRpcMsg*)((char*)(pSyncMsg->data) + raftMetaArrayLen);
  return msgArr;
}

SyncClientRequestBatch* syncClientRequestBatchFromRpcMsg(const SRpcMsg* pRpcMsg) {
  SyncClientRequestBatch* pSyncMsg = taosMemoryMalloc(pRpcMsg->contLen);
  ASSERT(pSyncMsg != NULL);
  memcpy(pSyncMsg, pRpcMsg->pCont, pRpcMsg->contLen);
  ASSERT(pRpcMsg->contLen == pSyncMsg->bytes);

  return pSyncMsg;
}

cJSON* syncClientRequestBatch2Json(const SyncClientRequestBatch* pMsg) {
  char   u64buf[128] = {0};
  cJSON* pRoot = cJSON_CreateObject();

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);
    cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen);
    cJSON_AddNumberToObject(pRoot, "dataCount", pMsg->dataCount);

    SRaftMeta* metaArr = syncClientRequestBatchMetaArr(pMsg);
    SRpcMsg*   msgArr = syncClientRequestBatchRpcMsgArr(pMsg);

    cJSON* pMetaArr = cJSON_CreateArray();
    cJSON_AddItemToObject(pRoot, "metaArr", pMetaArr);
    for (int i = 0; i < pMsg->dataCount; ++i) {
      cJSON* pMeta = cJSON_CreateObject();
      cJSON_AddNumberToObject(pMeta, "seqNum", metaArr[i].seqNum);
      cJSON_AddNumberToObject(pMeta, "isWeak", metaArr[i].isWeak);
      cJSON_AddItemToArray(pMetaArr, pMeta);
    }

    cJSON* pMsgArr = cJSON_CreateArray();
    cJSON_AddItemToObject(pRoot, "msgArr", pMsgArr);
    for (int i = 0; i < pMsg->dataCount; ++i) {
      cJSON* pRpcMsgJson = syncRpcMsg2Json(&msgArr[i]);
      cJSON_AddItemToArray(pMsgArr, pRpcMsgJson);
    }

    char* s;
S
Shengliang Guan 已提交
1095
    s = syncUtilPrintBin((char*)(pMsg->data), pMsg->dataLen);
1096 1097
    cJSON_AddStringToObject(pRoot, "data", s);
    taosMemoryFree(s);
S
Shengliang Guan 已提交
1098
    s = syncUtilPrintBin2((char*)(pMsg->data), pMsg->dataLen);
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
    cJSON_AddStringToObject(pRoot, "data2", s);
    taosMemoryFree(s);
  }

  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncClientRequestBatch", pRoot);
  return pJson;
}

char* syncClientRequestBatch2Str(const SyncClientRequestBatch* pMsg) {
  cJSON* pJson = syncClientRequestBatch2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

// for debug ----------------------
void syncClientRequestBatchPrint(const SyncClientRequestBatch* pMsg) {
  char* serialized = syncClientRequestBatch2Str(pMsg);
S
Shengliang Guan 已提交
1118
  printf("syncClientRequestBatchPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
1119 1120 1121 1122 1123 1124
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncClientRequestBatchPrint2(char* s, const SyncClientRequestBatch* pMsg) {
  char* serialized = syncClientRequestBatch2Str(pMsg);
S
Shengliang Guan 已提交
1125
  printf("syncClientRequestBatchPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
1126 1127 1128 1129 1130 1131
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncClientRequestBatchLog(const SyncClientRequestBatch* pMsg) {
  char* serialized = syncClientRequestBatch2Str(pMsg);
S
Shengliang Guan 已提交
1132
  sTrace("syncClientRequestBatchLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
1133 1134 1135 1136 1137 1138
  taosMemoryFree(serialized);
}

void syncClientRequestBatchLog2(char* s, const SyncClientRequestBatch* pMsg) {
  if (gRaftDetailLog) {
    char* serialized = syncClientRequestBatch2Str(pMsg);
S
Shengliang Guan 已提交
1139
    sLTrace("syncClientRequestBatchLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
1140 1141 1142
    taosMemoryFree(serialized);
  }
}
M
Minghao Li 已提交
1143

M
Minghao Li 已提交
1144
// ---- message process SyncRequestVote----
M
Minghao Li 已提交
1145
SyncRequestVote* syncRequestVoteBuild(int32_t vgId) {
M
Minghao Li 已提交
1146
  uint32_t         bytes = sizeof(SyncRequestVote);
wafwerar's avatar
wafwerar 已提交
1147
  SyncRequestVote* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1148 1149
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
M
Minghao Li 已提交
1150
  pMsg->vgId = vgId;
1151
  pMsg->msgType = TDMT_SYNC_REQUEST_VOTE;
M
Minghao Li 已提交
1152
  return pMsg;
M
Minghao Li 已提交
1153 1154
}

M
Minghao Li 已提交
1155 1156
void syncRequestVoteDestroy(SyncRequestVote* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
1157
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
1158 1159
  }
}
M
Minghao Li 已提交
1160

M
Minghao Li 已提交
1161
void syncRequestVoteSerialize(const SyncRequestVote* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
1162
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
1163 1164
  memcpy(buf, pMsg, pMsg->bytes);
}
M
Minghao Li 已提交
1165

M
Minghao Li 已提交
1166 1167
void syncRequestVoteDeserialize(const char* buf, uint32_t len, SyncRequestVote* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
1168
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1169
}
M
Minghao Li 已提交
1170

M
Minghao Li 已提交
1171
char* syncRequestVoteSerialize2(const SyncRequestVote* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
1172
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
1173
  ASSERT(buf != NULL);
M
Minghao Li 已提交
1174 1175 1176 1177 1178 1179 1180 1181 1182
  syncRequestVoteSerialize(pMsg, buf, pMsg->bytes);
  if (len != NULL) {
    *len = pMsg->bytes;
  }
  return buf;
}

SyncRequestVote* syncRequestVoteDeserialize2(const char* buf, uint32_t len) {
  uint32_t         bytes = *((uint32_t*)buf);
wafwerar's avatar
wafwerar 已提交
1183
  SyncRequestVote* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1184
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1185
  syncRequestVoteDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
1186
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1187 1188 1189
  return pMsg;
}

M
Minghao Li 已提交
1190 1191 1192 1193 1194 1195 1196
void syncRequestVote2RpcMsg(const SyncRequestVote* pMsg, SRpcMsg* pRpcMsg) {
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
  pRpcMsg->msgType = pMsg->msgType;
  pRpcMsg->contLen = pMsg->bytes;
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
  syncRequestVoteSerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen);
}
M
Minghao Li 已提交
1197

M
Minghao Li 已提交
1198 1199 1200
void syncRequestVoteFromRpcMsg(const SRpcMsg* pRpcMsg, SyncRequestVote* pMsg) {
  syncRequestVoteDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
}
M
Minghao Li 已提交
1201

M
Minghao Li 已提交
1202 1203
SyncRequestVote* syncRequestVoteFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncRequestVote* pMsg = syncRequestVoteDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
1204
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1205
  return pMsg;
M
Minghao Li 已提交
1206 1207
}

M
Minghao Li 已提交
1208
cJSON* syncRequestVote2Json(const SyncRequestVote* pMsg) {
1209
  char   u64buf[128] = {0};
M
Minghao Li 已提交
1210 1211
  cJSON* pRoot = cJSON_CreateObject();

M
Minghao Li 已提交
1212 1213
  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
1214
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
1215 1216 1217
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
1218
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
M
Minghao Li 已提交
1219 1220 1221 1222
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
1223
      char     host[128] = {0};
M
Minghao Li 已提交
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

    cJSON* pDestId = cJSON_CreateObject();
    cJSON_AddNumberToObject(pDestId, "addr", pMsg->destId.addr);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
1237
      char     host[128] = {0};
M
Minghao Li 已提交
1238 1239 1240 1241 1242 1243 1244 1245
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
    cJSON_AddItemToObject(pRoot, "destId", pDestId);

S
Shengliang Guan 已提交
1246
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
M
Minghao Li 已提交
1247
    cJSON_AddStringToObject(pRoot, "term", u64buf);
S
Shengliang Guan 已提交
1248
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->lastLogIndex);
M
Minghao Li 已提交
1249
    cJSON_AddStringToObject(pRoot, "lastLogIndex", u64buf);
S
Shengliang Guan 已提交
1250
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->lastLogTerm);
M
Minghao Li 已提交
1251 1252
    cJSON_AddStringToObject(pRoot, "lastLogTerm", u64buf);
  }
M
Minghao Li 已提交
1253 1254 1255 1256

  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncRequestVote", pRoot);
  return pJson;
M
Minghao Li 已提交
1257
}
M
Minghao Li 已提交
1258

M
Minghao Li 已提交
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
char* syncRequestVote2Str(const SyncRequestVote* pMsg) {
  cJSON* pJson = syncRequestVote2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

// for debug ----------------------
void syncRequestVotePrint(const SyncRequestVote* pMsg) {
  char* serialized = syncRequestVote2Str(pMsg);
S
Shengliang Guan 已提交
1269
  printf("syncRequestVotePrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
1270
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1271
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1272 1273 1274 1275
}

void syncRequestVotePrint2(char* s, const SyncRequestVote* pMsg) {
  char* serialized = syncRequestVote2Str(pMsg);
S
Shengliang Guan 已提交
1276
  printf("syncRequestVotePrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
1277
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1278
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1279 1280 1281 1282
}

void syncRequestVoteLog(const SyncRequestVote* pMsg) {
  char* serialized = syncRequestVote2Str(pMsg);
S
Shengliang Guan 已提交
1283
  sTrace("syncRequestVoteLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
1284
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1285 1286 1287
}

void syncRequestVoteLog2(char* s, const SyncRequestVote* pMsg) {
M
Minghao Li 已提交
1288 1289
  if (gRaftDetailLog) {
    char* serialized = syncRequestVote2Str(pMsg);
S
Shengliang Guan 已提交
1290
    sTrace("syncRequestVoteLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
1291 1292
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
1293 1294
}

M
Minghao Li 已提交
1295
// ---- message process SyncRequestVoteReply----
M
Minghao Li 已提交
1296
SyncRequestVoteReply* syncRequestVoteReplyBuild(int32_t vgId) {
M
Minghao Li 已提交
1297
  uint32_t              bytes = sizeof(SyncRequestVoteReply);
wafwerar's avatar
wafwerar 已提交
1298
  SyncRequestVoteReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1299 1300
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
M
Minghao Li 已提交
1301
  pMsg->vgId = vgId;
1302
  pMsg->msgType = TDMT_SYNC_REQUEST_VOTE_REPLY;
M
Minghao Li 已提交
1303
  return pMsg;
M
Minghao Li 已提交
1304 1305 1306 1307
}

void syncRequestVoteReplyDestroy(SyncRequestVoteReply* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
1308
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
1309 1310 1311 1312
  }
}

void syncRequestVoteReplySerialize(const SyncRequestVoteReply* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
1313
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
1314 1315 1316 1317 1318
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncRequestVoteReplyDeserialize(const char* buf, uint32_t len, SyncRequestVoteReply* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
1319
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1320 1321
}

M
Minghao Li 已提交
1322
char* syncRequestVoteReplySerialize2(const SyncRequestVoteReply* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
1323
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
1324
  ASSERT(buf != NULL);
M
Minghao Li 已提交
1325 1326 1327 1328 1329 1330 1331 1332 1333
  syncRequestVoteReplySerialize(pMsg, buf, pMsg->bytes);
  if (len != NULL) {
    *len = pMsg->bytes;
  }
  return buf;
}

SyncRequestVoteReply* syncRequestVoteReplyDeserialize2(const char* buf, uint32_t len) {
  uint32_t              bytes = *((uint32_t*)buf);
wafwerar's avatar
wafwerar 已提交
1334
  SyncRequestVoteReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1335
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1336
  syncRequestVoteReplyDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
1337
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1338 1339 1340
  return pMsg;
}

M
Minghao Li 已提交
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
void syncRequestVoteReply2RpcMsg(const SyncRequestVoteReply* pMsg, SRpcMsg* pRpcMsg) {
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
  pRpcMsg->msgType = pMsg->msgType;
  pRpcMsg->contLen = pMsg->bytes;
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
  syncRequestVoteReplySerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen);
}

void syncRequestVoteReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncRequestVoteReply* pMsg) {
  syncRequestVoteReplyDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
}

M
Minghao Li 已提交
1353 1354
SyncRequestVoteReply* syncRequestVoteReplyFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncRequestVoteReply* pMsg = syncRequestVoteReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
1355
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1356 1357 1358
  return pMsg;
}

M
Minghao Li 已提交
1359
cJSON* syncRequestVoteReply2Json(const SyncRequestVoteReply* pMsg) {
1360
  char   u64buf[128] = {0};
M
Minghao Li 已提交
1361 1362
  cJSON* pRoot = cJSON_CreateObject();

M
Minghao Li 已提交
1363 1364
  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
1365
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
1366 1367 1368
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
1369
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
M
Minghao Li 已提交
1370 1371 1372 1373
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
1374
      char     host[128] = {0};
M
Minghao Li 已提交
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

    cJSON* pDestId = cJSON_CreateObject();
    cJSON_AddNumberToObject(pDestId, "addr", pMsg->destId.addr);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
1388
      char     host[128] = {0};
M
Minghao Li 已提交
1389 1390 1391 1392 1393 1394 1395 1396
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
    cJSON_AddItemToObject(pRoot, "destId", pDestId);

S
Shengliang Guan 已提交
1397
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
M
Minghao Li 已提交
1398 1399 1400
    cJSON_AddStringToObject(pRoot, "term", u64buf);
    cJSON_AddNumberToObject(pRoot, "vote_granted", pMsg->voteGranted);
  }
M
Minghao Li 已提交
1401 1402 1403 1404

  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncRequestVoteReply", pRoot);
  return pJson;
M
Minghao Li 已提交
1405 1406
}

M
Minghao Li 已提交
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
char* syncRequestVoteReply2Str(const SyncRequestVoteReply* pMsg) {
  cJSON* pJson = syncRequestVoteReply2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

// for debug ----------------------
void syncRequestVoteReplyPrint(const SyncRequestVoteReply* pMsg) {
  char* serialized = syncRequestVoteReply2Str(pMsg);
S
Shengliang Guan 已提交
1417
  printf("syncRequestVoteReplyPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
1418
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1419
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1420 1421 1422 1423
}

void syncRequestVoteReplyPrint2(char* s, const SyncRequestVoteReply* pMsg) {
  char* serialized = syncRequestVoteReply2Str(pMsg);
S
Shengliang Guan 已提交
1424
  printf("syncRequestVoteReplyPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
1425
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1426
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1427 1428 1429 1430
}

void syncRequestVoteReplyLog(const SyncRequestVoteReply* pMsg) {
  char* serialized = syncRequestVoteReply2Str(pMsg);
S
Shengliang Guan 已提交
1431
  sTrace("syncRequestVoteReplyLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
1432
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1433 1434 1435
}

void syncRequestVoteReplyLog2(char* s, const SyncRequestVoteReply* pMsg) {
M
Minghao Li 已提交
1436 1437
  if (gRaftDetailLog) {
    char* serialized = syncRequestVoteReply2Str(pMsg);
S
Shengliang Guan 已提交
1438
    sTrace("syncRequestVoteReplyLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
1439 1440
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
1441 1442
}

M
Minghao Li 已提交
1443
// ---- message process SyncAppendEntries----
M
Minghao Li 已提交
1444
SyncAppendEntries* syncAppendEntriesBuild(uint32_t dataLen, int32_t vgId) {
M
Minghao Li 已提交
1445
  uint32_t           bytes = sizeof(SyncAppendEntries) + dataLen;
wafwerar's avatar
wafwerar 已提交
1446
  SyncAppendEntries* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1447 1448
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
M
Minghao Li 已提交
1449
  pMsg->vgId = vgId;
1450
  pMsg->msgType = TDMT_SYNC_APPEND_ENTRIES;
M
Minghao Li 已提交
1451
  pMsg->dataLen = dataLen;
M
Minghao Li 已提交
1452
  return pMsg;
M
Minghao Li 已提交
1453 1454 1455 1456
}

void syncAppendEntriesDestroy(SyncAppendEntries* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
1457
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
1458 1459 1460 1461
  }
}

void syncAppendEntriesSerialize(const SyncAppendEntries* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
1462
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
1463 1464 1465 1466 1467
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncAppendEntriesDeserialize(const char* buf, uint32_t len, SyncAppendEntries* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
1468 1469
  ASSERT(len == pMsg->bytes);
  ASSERT(pMsg->bytes == sizeof(SyncAppendEntries) + pMsg->dataLen);
M
Minghao Li 已提交
1470 1471 1472
}

char* syncAppendEntriesSerialize2(const SyncAppendEntries* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
1473
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
1474
  ASSERT(buf != NULL);
M
Minghao Li 已提交
1475 1476 1477 1478 1479 1480 1481 1482 1483
  syncAppendEntriesSerialize(pMsg, buf, pMsg->bytes);
  if (len != NULL) {
    *len = pMsg->bytes;
  }
  return buf;
}

SyncAppendEntries* syncAppendEntriesDeserialize2(const char* buf, uint32_t len) {
  uint32_t           bytes = *((uint32_t*)buf);
wafwerar's avatar
wafwerar 已提交
1484
  SyncAppendEntries* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1485
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1486
  syncAppendEntriesDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
1487
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1488
  return pMsg;
M
Minghao Li 已提交
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
}

void syncAppendEntries2RpcMsg(const SyncAppendEntries* pMsg, SRpcMsg* pRpcMsg) {
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
  pRpcMsg->msgType = pMsg->msgType;
  pRpcMsg->contLen = pMsg->bytes;
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
  syncAppendEntriesSerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen);
}

void syncAppendEntriesFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntries* pMsg) {
  syncAppendEntriesDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
}

M
Minghao Li 已提交
1503 1504
SyncAppendEntries* syncAppendEntriesFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncAppendEntries* pMsg = syncAppendEntriesDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
1505
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1506 1507 1508
  return pMsg;
}

M
Minghao Li 已提交
1509
cJSON* syncAppendEntries2Json(const SyncAppendEntries* pMsg) {
1510
  char   u64buf[128] = {0};
M
Minghao Li 已提交
1511 1512
  cJSON* pRoot = cJSON_CreateObject();

M
Minghao Li 已提交
1513 1514
  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
1515
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
1516 1517 1518
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
1519
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
M
Minghao Li 已提交
1520 1521 1522 1523
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
1524
      char     host[128] = {0};
M
Minghao Li 已提交
1525 1526 1527 1528 1529 1530 1531 1532 1533
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

    cJSON* pDestId = cJSON_CreateObject();
S
Shengliang Guan 已提交
1534
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
M
Minghao Li 已提交
1535 1536 1537 1538
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
1539
      char     host[128] = {0};
M
Minghao Li 已提交
1540 1541 1542 1543 1544 1545 1546 1547
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
    cJSON_AddItemToObject(pRoot, "destId", pDestId);

S
Shengliang Guan 已提交
1548
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
M
Minghao Li 已提交
1549 1550
    cJSON_AddStringToObject(pRoot, "term", u64buf);

S
Shengliang Guan 已提交
1551
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->privateTerm);
1552 1553
    cJSON_AddStringToObject(pRoot, "privateTerm", u64buf);

S
Shengliang Guan 已提交
1554
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->prevLogIndex);
M
Minghao Li 已提交
1555
    cJSON_AddStringToObject(pRoot, "prevLogIndex", u64buf);
M
Minghao Li 已提交
1556

S
Shengliang Guan 已提交
1557
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->prevLogTerm);
M
Minghao Li 已提交
1558 1559
    cJSON_AddStringToObject(pRoot, "pre_log_term", u64buf);

S
Shengliang Guan 已提交
1560
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->commitIndex);
M
Minghao Li 已提交
1561
    cJSON_AddStringToObject(pRoot, "commitIndex", u64buf);
M
Minghao Li 已提交
1562 1563 1564

    cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen);
    char* s;
S
Shengliang Guan 已提交
1565
    s = syncUtilPrintBin((char*)(pMsg->data), pMsg->dataLen);
M
Minghao Li 已提交
1566
    cJSON_AddStringToObject(pRoot, "data", s);
wafwerar's avatar
wafwerar 已提交
1567
    taosMemoryFree(s);
S
Shengliang Guan 已提交
1568
    s = syncUtilPrintBin2((char*)(pMsg->data), pMsg->dataLen);
M
Minghao Li 已提交
1569
    cJSON_AddStringToObject(pRoot, "data2", s);
wafwerar's avatar
wafwerar 已提交
1570
    taosMemoryFree(s);
M
Minghao Li 已提交
1571
  }
M
Minghao Li 已提交
1572 1573 1574 1575 1576 1577

  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncAppendEntries", pRoot);
  return pJson;
}

M
Minghao Li 已提交
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
char* syncAppendEntries2Str(const SyncAppendEntries* pMsg) {
  cJSON* pJson = syncAppendEntries2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

// for debug ----------------------
void syncAppendEntriesPrint(const SyncAppendEntries* pMsg) {
  char* serialized = syncAppendEntries2Str(pMsg);
S
Shengliang Guan 已提交
1588
  printf("syncAppendEntriesPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
1589
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1590
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1591 1592 1593 1594
}

void syncAppendEntriesPrint2(char* s, const SyncAppendEntries* pMsg) {
  char* serialized = syncAppendEntries2Str(pMsg);
S
Shengliang Guan 已提交
1595
  printf("syncAppendEntriesPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
1596
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1597
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1598 1599 1600 1601
}

void syncAppendEntriesLog(const SyncAppendEntries* pMsg) {
  char* serialized = syncAppendEntries2Str(pMsg);
S
Shengliang Guan 已提交
1602
  sTrace("syncAppendEntriesLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
1603
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1604 1605 1606
}

void syncAppendEntriesLog2(char* s, const SyncAppendEntries* pMsg) {
M
Minghao Li 已提交
1607 1608
  if (gRaftDetailLog) {
    char* serialized = syncAppendEntries2Str(pMsg);
S
Shengliang Guan 已提交
1609
    sTrace("syncAppendEntriesLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
1610 1611
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
1612 1613
}

1614 1615 1616 1617
// ---- message process SyncAppendEntriesBatch----

// block1: SOffsetAndContLen
// block2: SOffsetAndContLen Array
1618
// block3: entry Array
1619

1620 1621
SyncAppendEntriesBatch* syncAppendEntriesBatchBuild(SSyncRaftEntry** entryPArr, int32_t arrSize, int32_t vgId) {
  ASSERT(entryPArr != NULL);
1622
  ASSERT(arrSize >= 0);
1623 1624 1625

  int32_t dataLen = 0;
  int32_t metaArrayLen = sizeof(SOffsetAndContLen) * arrSize;  // <offset, contLen>
1626
  int32_t entryArrayLen = 0;
1627
  for (int i = 0; i < arrSize; ++i) {  // SRpcMsg pCont
1628 1629
    SSyncRaftEntry* pEntry = entryPArr[i];
    entryArrayLen += pEntry->bytes;
1630
  }
1631
  dataLen += (metaArrayLen + entryArrayLen);
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645

  uint32_t                bytes = sizeof(SyncAppendEntriesBatch) + dataLen;
  SyncAppendEntriesBatch* pMsg = taosMemoryMalloc(bytes);
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
  pMsg->vgId = vgId;
  pMsg->msgType = TDMT_SYNC_APPEND_ENTRIES_BATCH;
  pMsg->dataCount = arrSize;
  pMsg->dataLen = dataLen;

  SOffsetAndContLen* metaArr = (SOffsetAndContLen*)(pMsg->data);
  char*              pData = pMsg->data;

  for (int i = 0; i < arrSize; ++i) {
1646
    // init meta <offset, contLen>
1647
    if (i == 0) {
1648 1649
      metaArr[i].offset = metaArrayLen;
      metaArr[i].contLen = entryPArr[i]->bytes;
1650 1651
    } else {
      metaArr[i].offset = metaArr[i - 1].offset + metaArr[i - 1].contLen;
1652
      metaArr[i].contLen = entryPArr[i]->bytes;
1653 1654
    }

1655 1656 1657
    // init entry array
    ASSERT(metaArr[i].contLen == entryPArr[i]->bytes);
    memcpy(pData + metaArr[i].offset, entryPArr[i], metaArr[i].contLen);
1658 1659 1660 1661
  }

  return pMsg;
}
1662

1663 1664
SOffsetAndContLen* syncAppendEntriesBatchMetaTableArray(SyncAppendEntriesBatch* pMsg) {
  return (SOffsetAndContLen*)(pMsg->data);
1665
}
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730

void syncAppendEntriesBatchDestroy(SyncAppendEntriesBatch* pMsg) {
  if (pMsg != NULL) {
    taosMemoryFree(pMsg);
  }
}

void syncAppendEntriesBatchSerialize(const SyncAppendEntriesBatch* pMsg, char* buf, uint32_t bufLen) {
  ASSERT(pMsg->bytes <= bufLen);
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncAppendEntriesBatchDeserialize(const char* buf, uint32_t len, SyncAppendEntriesBatch* pMsg) {
  memcpy(pMsg, buf, len);
  ASSERT(len == pMsg->bytes);
  ASSERT(pMsg->bytes == sizeof(SyncAppendEntriesBatch) + pMsg->dataLen);
}

char* syncAppendEntriesBatchSerialize2(const SyncAppendEntriesBatch* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
  ASSERT(buf != NULL);
  syncAppendEntriesBatchSerialize(pMsg, buf, pMsg->bytes);
  if (len != NULL) {
    *len = pMsg->bytes;
  }
  return buf;
}

SyncAppendEntriesBatch* syncAppendEntriesBatchDeserialize2(const char* buf, uint32_t len) {
  uint32_t                bytes = *((uint32_t*)buf);
  SyncAppendEntriesBatch* pMsg = taosMemoryMalloc(bytes);
  ASSERT(pMsg != NULL);
  syncAppendEntriesBatchDeserialize(buf, len, pMsg);
  ASSERT(len == pMsg->bytes);
  return pMsg;
}

void syncAppendEntriesBatch2RpcMsg(const SyncAppendEntriesBatch* pMsg, SRpcMsg* pRpcMsg) {
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
  pRpcMsg->msgType = pMsg->msgType;
  pRpcMsg->contLen = pMsg->bytes;
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
  syncAppendEntriesBatchSerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen);
}

void syncAppendEntriesBatchFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntriesBatch* pMsg) {
  syncAppendEntriesBatchDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
}

SyncAppendEntriesBatch* syncAppendEntriesBatchFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncAppendEntriesBatch* pMsg = syncAppendEntriesBatchDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
  ASSERT(pMsg != NULL);
  return pMsg;
}

cJSON* syncAppendEntriesBatch2Json(const SyncAppendEntriesBatch* pMsg) {
  char   u64buf[128] = {0};
  cJSON* pRoot = cJSON_CreateObject();

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
1731
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
      char     host[128] = {0};
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

    cJSON* pDestId = cJSON_CreateObject();
S
Shengliang Guan 已提交
1746
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
      char     host[128] = {0};
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
    cJSON_AddItemToObject(pRoot, "destId", pDestId);

S
Shengliang Guan 已提交
1760
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
1761 1762
    cJSON_AddStringToObject(pRoot, "term", u64buf);

S
Shengliang Guan 已提交
1763
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->prevLogIndex);
1764 1765
    cJSON_AddStringToObject(pRoot, "prevLogIndex", u64buf);

S
Shengliang Guan 已提交
1766
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->prevLogTerm);
1767 1768
    cJSON_AddStringToObject(pRoot, "prevLogTerm", u64buf);

S
Shengliang Guan 已提交
1769
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->commitIndex);
1770 1771
    cJSON_AddStringToObject(pRoot, "commitIndex", u64buf);

S
Shengliang Guan 已提交
1772
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->privateTerm);
1773 1774 1775 1776 1777 1778
    cJSON_AddStringToObject(pRoot, "privateTerm", u64buf);

    cJSON_AddNumberToObject(pRoot, "dataCount", pMsg->dataCount);
    cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen);

    int32_t metaArrayLen = sizeof(SOffsetAndContLen) * pMsg->dataCount;  // <offset, contLen>
1779
    int32_t entryArrayLen = pMsg->dataLen - metaArrayLen;
1780 1781

    cJSON_AddNumberToObject(pRoot, "metaArrayLen", metaArrayLen);
1782
    cJSON_AddNumberToObject(pRoot, "entryArrayLen", entryArrayLen);
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794

    SOffsetAndContLen* metaArr = (SOffsetAndContLen*)(pMsg->data);

    cJSON* pMetaArr = cJSON_CreateArray();
    cJSON_AddItemToObject(pRoot, "metaArr", pMetaArr);
    for (int i = 0; i < pMsg->dataCount; ++i) {
      cJSON* pMeta = cJSON_CreateObject();
      cJSON_AddNumberToObject(pMeta, "offset", metaArr[i].offset);
      cJSON_AddNumberToObject(pMeta, "contLen", metaArr[i].contLen);
      cJSON_AddItemToArray(pMetaArr, pMeta);
    }

1795 1796
    cJSON* pEntryArr = cJSON_CreateArray();
    cJSON_AddItemToObject(pRoot, "entryArr", pEntryArr);
1797
    for (int i = 0; i < pMsg->dataCount; ++i) {
1798 1799 1800
      SSyncRaftEntry* pEntry = (SSyncRaftEntry*)(pMsg->data + metaArr[i].offset);
      cJSON*          pEntryJson = syncEntry2Json(pEntry);
      cJSON_AddItemToArray(pEntryArr, pEntryJson);
1801 1802 1803
    }

    char* s;
S
Shengliang Guan 已提交
1804
    s = syncUtilPrintBin((char*)(pMsg->data), pMsg->dataLen);
1805 1806
    cJSON_AddStringToObject(pRoot, "data", s);
    taosMemoryFree(s);
S
Shengliang Guan 已提交
1807
    s = syncUtilPrintBin2((char*)(pMsg->data), pMsg->dataLen);
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
    cJSON_AddStringToObject(pRoot, "data2", s);
    taosMemoryFree(s);
  }

  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncAppendEntriesBatch", pRoot);
  return pJson;
}

char* syncAppendEntriesBatch2Str(const SyncAppendEntriesBatch* pMsg) {
  cJSON* pJson = syncAppendEntriesBatch2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

// for debug ----------------------
void syncAppendEntriesBatchPrint(const SyncAppendEntriesBatch* pMsg) {
  char* serialized = syncAppendEntriesBatch2Str(pMsg);
S
Shengliang Guan 已提交
1827
  printf("syncAppendEntriesBatchPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
1828 1829 1830 1831 1832 1833
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncAppendEntriesBatchPrint2(char* s, const SyncAppendEntriesBatch* pMsg) {
  char* serialized = syncAppendEntriesBatch2Str(pMsg);
S
Shengliang Guan 已提交
1834
  printf("syncAppendEntriesBatchPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
1835 1836 1837 1838 1839 1840
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncAppendEntriesBatchLog(const SyncAppendEntriesBatch* pMsg) {
  char* serialized = syncAppendEntriesBatch2Str(pMsg);
S
Shengliang Guan 已提交
1841
  sTrace("syncAppendEntriesBatchLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
1842 1843 1844 1845 1846 1847
  taosMemoryFree(serialized);
}

void syncAppendEntriesBatchLog2(char* s, const SyncAppendEntriesBatch* pMsg) {
  if (gRaftDetailLog) {
    char* serialized = syncAppendEntriesBatch2Str(pMsg);
S
Shengliang Guan 已提交
1848
    sLTrace("syncAppendEntriesBatchLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
1849 1850 1851 1852
    taosMemoryFree(serialized);
  }
}

M
Minghao Li 已提交
1853
// ---- message process SyncAppendEntriesReply----
M
Minghao Li 已提交
1854
SyncAppendEntriesReply* syncAppendEntriesReplyBuild(int32_t vgId) {
M
Minghao Li 已提交
1855
  uint32_t                bytes = sizeof(SyncAppendEntriesReply);
wafwerar's avatar
wafwerar 已提交
1856
  SyncAppendEntriesReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1857 1858
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
M
Minghao Li 已提交
1859
  pMsg->vgId = vgId;
1860
  pMsg->msgType = TDMT_SYNC_APPEND_ENTRIES_REPLY;
M
Minghao Li 已提交
1861
  return pMsg;
M
Minghao Li 已提交
1862 1863 1864 1865
}

void syncAppendEntriesReplyDestroy(SyncAppendEntriesReply* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
1866
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
1867 1868 1869 1870
  }
}

void syncAppendEntriesReplySerialize(const SyncAppendEntriesReply* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
1871
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
1872 1873 1874 1875 1876
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncAppendEntriesReplyDeserialize(const char* buf, uint32_t len, SyncAppendEntriesReply* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
1877
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1878 1879
}

M
Minghao Li 已提交
1880
char* syncAppendEntriesReplySerialize2(const SyncAppendEntriesReply* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
1881
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
1882
  ASSERT(buf != NULL);
M
Minghao Li 已提交
1883 1884 1885 1886 1887 1888 1889 1890 1891
  syncAppendEntriesReplySerialize(pMsg, buf, pMsg->bytes);
  if (len != NULL) {
    *len = pMsg->bytes;
  }
  return buf;
}

SyncAppendEntriesReply* syncAppendEntriesReplyDeserialize2(const char* buf, uint32_t len) {
  uint32_t                bytes = *((uint32_t*)buf);
wafwerar's avatar
wafwerar 已提交
1892
  SyncAppendEntriesReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1893
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1894
  syncAppendEntriesReplyDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
1895
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1896 1897 1898
  return pMsg;
}

M
Minghao Li 已提交
1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
void syncAppendEntriesReply2RpcMsg(const SyncAppendEntriesReply* pMsg, SRpcMsg* pRpcMsg) {
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
  pRpcMsg->msgType = pMsg->msgType;
  pRpcMsg->contLen = pMsg->bytes;
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
  syncAppendEntriesReplySerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen);
}

void syncAppendEntriesReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntriesReply* pMsg) {
  syncAppendEntriesReplyDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
}

M
Minghao Li 已提交
1911 1912
SyncAppendEntriesReply* syncAppendEntriesReplyFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncAppendEntriesReply* pMsg = syncAppendEntriesReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
1913
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1914 1915 1916
  return pMsg;
}

M
Minghao Li 已提交
1917
cJSON* syncAppendEntriesReply2Json(const SyncAppendEntriesReply* pMsg) {
1918
  char   u64buf[128] = {0};
M
Minghao Li 已提交
1919
  cJSON* pRoot = cJSON_CreateObject();
M
Minghao Li 已提交
1920 1921 1922

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
1923
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
1924 1925 1926
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
1927
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
M
Minghao Li 已提交
1928 1929 1930 1931
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
1932
      char     host[128] = {0};
M
Minghao Li 已提交
1933 1934 1935 1936 1937 1938 1939 1940 1941
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

    cJSON* pDestId = cJSON_CreateObject();
S
Shengliang Guan 已提交
1942
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
M
Minghao Li 已提交
1943 1944 1945 1946
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
1947
      char     host[128] = {0};
M
Minghao Li 已提交
1948 1949 1950 1951 1952 1953 1954 1955
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
    cJSON_AddItemToObject(pRoot, "destId", pDestId);

S
Shengliang Guan 已提交
1956
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->privateTerm);
1957 1958
    cJSON_AddStringToObject(pRoot, "privateTerm", u64buf);

S
Shengliang Guan 已提交
1959
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
M
Minghao Li 已提交
1960 1961
    cJSON_AddStringToObject(pRoot, "term", u64buf);
    cJSON_AddNumberToObject(pRoot, "success", pMsg->success);
S
Shengliang Guan 已提交
1962
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->matchIndex);
M
Minghao Li 已提交
1963
    cJSON_AddStringToObject(pRoot, "matchIndex", u64buf);
1964 1965
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->startTime);
    cJSON_AddStringToObject(pRoot, "startTime", u64buf);
M
Minghao Li 已提交
1966 1967 1968 1969 1970
  }

  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncAppendEntriesReply", pRoot);
  return pJson;
M
Minghao Li 已提交
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982
}

char* syncAppendEntriesReply2Str(const SyncAppendEntriesReply* pMsg) {
  cJSON* pJson = syncAppendEntriesReply2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

// for debug ----------------------
void syncAppendEntriesReplyPrint(const SyncAppendEntriesReply* pMsg) {
  char* serialized = syncAppendEntriesReply2Str(pMsg);
S
Shengliang Guan 已提交
1983
  printf("syncAppendEntriesReplyPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
1984
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1985
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1986 1987 1988 1989
}

void syncAppendEntriesReplyPrint2(char* s, const SyncAppendEntriesReply* pMsg) {
  char* serialized = syncAppendEntriesReply2Str(pMsg);
S
Shengliang Guan 已提交
1990
  printf("syncAppendEntriesReplyPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
1991
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1992
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1993 1994 1995 1996
}

void syncAppendEntriesReplyLog(const SyncAppendEntriesReply* pMsg) {
  char* serialized = syncAppendEntriesReply2Str(pMsg);
S
Shengliang Guan 已提交
1997
  sTrace("syncAppendEntriesReplyLog | len:%d| %s", (int32_t)strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
1998
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1999 2000 2001
}

void syncAppendEntriesReplyLog2(char* s, const SyncAppendEntriesReply* pMsg) {
M
Minghao Li 已提交
2002 2003
  if (gRaftDetailLog) {
    char* serialized = syncAppendEntriesReply2Str(pMsg);
S
Shengliang Guan 已提交
2004
    sTrace("syncAppendEntriesReplyLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
2005 2006
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
2007
}
M
Minghao Li 已提交
2008

M
Minghao Li 已提交
2009 2010 2011 2012 2013 2014 2015
// ---- message process SyncHeartbeat----
SyncHeartbeat* syncHeartbeatBuild(int32_t vgId) {
  uint32_t       bytes = sizeof(SyncHeartbeat);
  SyncHeartbeat* pMsg = taosMemoryMalloc(bytes);
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
  pMsg->vgId = vgId;
2016
  pMsg->msgType = TDMT_SYNC_HEARTBEAT;
M
Minghao Li 已提交
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
  return pMsg;
}

void syncHeartbeatDestroy(SyncHeartbeat* pMsg) {
  if (pMsg != NULL) {
    taosMemoryFree(pMsg);
  }
}

void syncHeartbeatSerialize(const SyncHeartbeat* pMsg, char* buf, uint32_t bufLen) {
  ASSERT(pMsg->bytes <= bufLen);
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncHeartbeatDeserialize(const char* buf, uint32_t len, SyncHeartbeat* pMsg) {
  memcpy(pMsg, buf, len);
  ASSERT(len == pMsg->bytes);
}

char* syncHeartbeatSerialize2(const SyncHeartbeat* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
  ASSERT(buf != NULL);
  syncHeartbeatSerialize(pMsg, buf, pMsg->bytes);
  if (len != NULL) {
    *len = pMsg->bytes;
  }
  return buf;
}

SyncHeartbeat* syncHeartbeatDeserialize2(const char* buf, uint32_t len) {
  uint32_t       bytes = *((uint32_t*)buf);
  SyncHeartbeat* pMsg = taosMemoryMalloc(bytes);
  ASSERT(pMsg != NULL);
  syncHeartbeatDeserialize(buf, len, pMsg);
  ASSERT(len == pMsg->bytes);
  return pMsg;
}

void syncHeartbeat2RpcMsg(const SyncHeartbeat* pMsg, SRpcMsg* pRpcMsg) {
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
  pRpcMsg->msgType = pMsg->msgType;
  pRpcMsg->contLen = pMsg->bytes;
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
  syncHeartbeatSerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen);
}

void syncHeartbeatFromRpcMsg(const SRpcMsg* pRpcMsg, SyncHeartbeat* pMsg) {
  syncHeartbeatDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
}

SyncHeartbeat* syncHeartbeatFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncHeartbeat* pMsg = syncHeartbeatDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
  ASSERT(pMsg != NULL);
  return pMsg;
}

cJSON* syncHeartbeat2Json(const SyncHeartbeat* pMsg) {
  char   u64buf[128] = {0};
  cJSON* pRoot = cJSON_CreateObject();

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
      char     host[128] = {0};
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

    cJSON* pDestId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
      char     host[128] = {0};
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
    cJSON_AddItemToObject(pRoot, "destId", pDestId);

    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
    cJSON_AddStringToObject(pRoot, "term", u64buf);

    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->privateTerm);
    cJSON_AddStringToObject(pRoot, "privateTerm", u64buf);

    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->commitIndex);
    cJSON_AddStringToObject(pRoot, "commitIndex", u64buf);
  }

  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncHeartbeat", pRoot);
  return pJson;
}

char* syncHeartbeat2Str(const SyncHeartbeat* pMsg) {
  cJSON* pJson = syncHeartbeat2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

void syncHeartbeatPrint(const SyncHeartbeat* pMsg) {
  char* serialized = syncHeartbeat2Str(pMsg);
S
Shengliang Guan 已提交
2136
  printf("syncHeartbeatPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
2137 2138 2139 2140 2141 2142
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncHeartbeatPrint2(char* s, const SyncHeartbeat* pMsg) {
  char* serialized = syncHeartbeat2Str(pMsg);
S
Shengliang Guan 已提交
2143
  printf("syncHeartbeatPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
2144 2145 2146 2147 2148 2149
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncHeartbeatLog(const SyncHeartbeat* pMsg) {
  char* serialized = syncHeartbeat2Str(pMsg);
S
Shengliang Guan 已提交
2150
  sTrace("syncHeartbeatLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
2151 2152 2153 2154 2155 2156
  taosMemoryFree(serialized);
}

void syncHeartbeatLog2(char* s, const SyncHeartbeat* pMsg) {
  if (gRaftDetailLog) {
    char* serialized = syncHeartbeat2Str(pMsg);
S
Shengliang Guan 已提交
2157
    sTrace("syncHeartbeatLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
2158 2159 2160 2161
    taosMemoryFree(serialized);
  }
}

2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289
// ---- message process SyncHeartbeatReply----
SyncHeartbeatReply* syncHeartbeatReplyBuild(int32_t vgId) {
  uint32_t            bytes = sizeof(SyncHeartbeatReply);
  SyncHeartbeatReply* pMsg = taosMemoryMalloc(bytes);
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
  pMsg->vgId = vgId;
  pMsg->msgType = TDMT_SYNC_HEARTBEAT_REPLY;
  return pMsg;
}

void syncHeartbeatReplyDestroy(SyncHeartbeatReply* pMsg) {
  if (pMsg != NULL) {
    taosMemoryFree(pMsg);
  }
}

void syncHeartbeatReplySerialize(const SyncHeartbeatReply* pMsg, char* buf, uint32_t bufLen) {
  ASSERT(pMsg->bytes <= bufLen);
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncHeartbeatReplyDeserialize(const char* buf, uint32_t len, SyncHeartbeatReply* pMsg) {
  memcpy(pMsg, buf, len);
  ASSERT(len == pMsg->bytes);
}

char* syncHeartbeatReplySerialize2(const SyncHeartbeatReply* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
  ASSERT(buf != NULL);
  syncHeartbeatReplySerialize(pMsg, buf, pMsg->bytes);
  if (len != NULL) {
    *len = pMsg->bytes;
  }
  return buf;
}

SyncHeartbeatReply* syncHeartbeatReplyDeserialize2(const char* buf, uint32_t len) {
  uint32_t            bytes = *((uint32_t*)buf);
  SyncHeartbeatReply* pMsg = taosMemoryMalloc(bytes);
  ASSERT(pMsg != NULL);
  syncHeartbeatReplyDeserialize(buf, len, pMsg);
  ASSERT(len == pMsg->bytes);
  return pMsg;
}

void syncHeartbeatReply2RpcMsg(const SyncHeartbeatReply* pMsg, SRpcMsg* pRpcMsg) {
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
  pRpcMsg->msgType = pMsg->msgType;
  pRpcMsg->contLen = pMsg->bytes;
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
  syncHeartbeatReplySerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen);
}

void syncHeartbeatReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncHeartbeatReply* pMsg) {
  syncHeartbeatReplyDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
}

SyncHeartbeatReply* syncHeartbeatReplyFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncHeartbeatReply* pMsg = syncHeartbeatReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
  ASSERT(pMsg != NULL);
  return pMsg;
}

cJSON* syncHeartbeatReply2Json(const SyncHeartbeatReply* pMsg) {
  char   u64buf[128] = {0};
  cJSON* pRoot = cJSON_CreateObject();

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
      char     host[128] = {0};
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

    cJSON* pDestId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
      char     host[128] = {0};
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
    cJSON_AddItemToObject(pRoot, "destId", pDestId);

    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->privateTerm);
    cJSON_AddStringToObject(pRoot, "privateTerm", u64buf);

    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
    cJSON_AddStringToObject(pRoot, "term", u64buf);

    cJSON_AddStringToObject(pRoot, "matchIndex", u64buf);
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->startTime);
    cJSON_AddStringToObject(pRoot, "startTime", u64buf);
  }

  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncHeartbeatReply", pRoot);
  return pJson;
}

char* syncHeartbeatReply2Str(const SyncHeartbeatReply* pMsg) {
  cJSON* pJson = syncHeartbeatReply2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

void syncHeartbeatReplyPrint(const SyncHeartbeatReply* pMsg) {
  char* serialized = syncHeartbeatReply2Str(pMsg);
S
Shengliang Guan 已提交
2290
  printf("syncHeartbeatReplyPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
2291 2292 2293 2294 2295 2296
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncHeartbeatReplyPrint2(char* s, const SyncHeartbeatReply* pMsg) {
  char* serialized = syncHeartbeatReply2Str(pMsg);
S
Shengliang Guan 已提交
2297
  printf("syncHeartbeatReplyPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
2298 2299 2300 2301 2302 2303
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncHeartbeatReplyLog(const SyncHeartbeatReply* pMsg) {
  char* serialized = syncHeartbeatReply2Str(pMsg);
S
Shengliang Guan 已提交
2304
  sTrace("syncHeartbeatReplyLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
2305 2306 2307 2308 2309 2310
  taosMemoryFree(serialized);
}

void syncHeartbeatReplyLog2(char* s, const SyncHeartbeatReply* pMsg) {
  if (gRaftDetailLog) {
    char* serialized = syncHeartbeatReply2Str(pMsg);
S
Shengliang Guan 已提交
2311
    sTrace("syncHeartbeatReplyLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
2312 2313 2314 2315
    taosMemoryFree(serialized);
  }
}

M
Minghao Li 已提交
2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462
// ---- message process SyncPreSnapshot----
SyncPreSnapshot* syncPreSnapshotBuild(int32_t vgId) {
  uint32_t         bytes = sizeof(SyncPreSnapshot);
  SyncPreSnapshot* pMsg = taosMemoryMalloc(bytes);
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
  pMsg->vgId = vgId;
  pMsg->msgType = TDMT_SYNC_PRE_SNAPSHOT;
  return pMsg;
}

void syncPreSnapshotDestroy(SyncPreSnapshot* pMsg) {
  if (pMsg != NULL) {
    taosMemoryFree(pMsg);
  }
}

void syncPreSnapshotSerialize(const SyncPreSnapshot* pMsg, char* buf, uint32_t bufLen) {
  ASSERT(pMsg->bytes <= bufLen);
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncPreSnapshotDeserialize(const char* buf, uint32_t len, SyncPreSnapshot* pMsg) {
  memcpy(pMsg, buf, len);
  ASSERT(len == pMsg->bytes);
}

char* syncPreSnapshotSerialize2(const SyncPreSnapshot* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
  ASSERT(buf != NULL);
  syncPreSnapshotSerialize(pMsg, buf, pMsg->bytes);
  if (len != NULL) {
    *len = pMsg->bytes;
  }
  return buf;
}

SyncPreSnapshot* syncPreSnapshotDeserialize2(const char* buf, uint32_t len) {
  uint32_t         bytes = *((uint32_t*)buf);
  SyncPreSnapshot* pMsg = taosMemoryMalloc(bytes);
  ASSERT(pMsg != NULL);
  syncPreSnapshotDeserialize(buf, len, pMsg);
  ASSERT(len == pMsg->bytes);
  return pMsg;
}

void syncPreSnapshot2RpcMsg(const SyncPreSnapshot* pMsg, SRpcMsg* pRpcMsg) {
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
  pRpcMsg->msgType = pMsg->msgType;
  pRpcMsg->contLen = pMsg->bytes;
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
  syncPreSnapshotSerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen);
}

void syncPreSnapshotFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPreSnapshot* pMsg) {
  syncPreSnapshotDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
}

SyncPreSnapshot* syncPreSnapshotFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncPreSnapshot* pMsg = syncPreSnapshotDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
  ASSERT(pMsg != NULL);
  return pMsg;
}

cJSON* syncPreSnapshot2Json(const SyncPreSnapshot* pMsg) {
  char   u64buf[128] = {0};
  cJSON* pRoot = cJSON_CreateObject();

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
      char     host[128] = {0};
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

    cJSON* pDestId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
      char     host[128] = {0};
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
    cJSON_AddItemToObject(pRoot, "destId", pDestId);

    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
    cJSON_AddStringToObject(pRoot, "term", u64buf);
  }

  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncPreSnapshot", pRoot);
  return pJson;
}

char* syncPreSnapshot2Str(const SyncPreSnapshot* pMsg) {
  cJSON* pJson = syncPreSnapshot2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

void syncPreSnapshotPrint(const SyncPreSnapshot* pMsg) {
  char* serialized = syncPreSnapshot2Str(pMsg);
  printf("syncPreSnapshotPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncPreSnapshotPrint2(char* s, const SyncPreSnapshot* pMsg) {
  char* serialized = syncPreSnapshot2Str(pMsg);
  printf("syncPreSnapshotPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncPreSnapshotLog(const SyncPreSnapshot* pMsg) {
  char* serialized = syncPreSnapshot2Str(pMsg);
  sTrace("syncPreSnapshotLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
  taosMemoryFree(serialized);
}

void syncPreSnapshotLog2(char* s, const SyncPreSnapshot* pMsg) {
  if (gRaftDetailLog) {
    char* serialized = syncPreSnapshot2Str(pMsg);
    sTrace("syncPreSnapshotLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
    taosMemoryFree(serialized);
  }
}

M
Minghao Li 已提交
2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568
// ---- message process SyncPreSnapshotReply----
SyncPreSnapshotReply* syncPreSnapshotReplyBuild(int32_t vgId) {
  uint32_t              bytes = sizeof(SyncPreSnapshotReply);
  SyncPreSnapshotReply* pMsg = taosMemoryMalloc(bytes);
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
  pMsg->vgId = vgId;
  pMsg->msgType = TDMT_SYNC_PRE_SNAPSHOT_REPLY;
  return pMsg;
}

void syncPreSnapshotReplyDestroy(SyncPreSnapshotReply* pMsg) {
  if (pMsg != NULL) {
    taosMemoryFree(pMsg);
  }
}

void syncPreSnapshotReplySerialize(const SyncPreSnapshotReply* pMsg, char* buf, uint32_t bufLen) {
  ASSERT(pMsg->bytes <= bufLen);
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncPreSnapshotReplyDeserialize(const char* buf, uint32_t len, SyncPreSnapshotReply* pMsg) {
  memcpy(pMsg, buf, len);
  ASSERT(len == pMsg->bytes);
}

char* syncPreSnapshotReplySerialize2(const SyncPreSnapshotReply* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
  ASSERT(buf != NULL);
  syncPreSnapshotReplySerialize(pMsg, buf, pMsg->bytes);
  if (len != NULL) {
    *len = pMsg->bytes;
  }
  return buf;
}

SyncPreSnapshotReply* syncPreSnapshotReplyDeserialize2(const char* buf, uint32_t len) {
  uint32_t              bytes = *((uint32_t*)buf);
  SyncPreSnapshotReply* pMsg = taosMemoryMalloc(bytes);
  ASSERT(pMsg != NULL);
  syncPreSnapshotReplyDeserialize(buf, len, pMsg);
  ASSERT(len == pMsg->bytes);
  return pMsg;
}

void syncPreSnapshotReply2RpcMsg(const SyncPreSnapshotReply* pMsg, SRpcMsg* pRpcMsg) {
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
  pRpcMsg->msgType = pMsg->msgType;
  pRpcMsg->contLen = pMsg->bytes;
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
  syncPreSnapshotReplySerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen);
}

void syncPreSnapshotReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPreSnapshotReply* pMsg) {
  syncPreSnapshotReplyDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
}

SyncPreSnapshotReply* syncPreSnapshotReplyFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncPreSnapshotReply* pMsg = syncPreSnapshotReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
  ASSERT(pMsg != NULL);
  return pMsg;
}

cJSON* syncPreSnapshotReply2Json(const SyncPreSnapshotReply* pMsg) {
  char   u64buf[128] = {0};
  cJSON* pRoot = cJSON_CreateObject();

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
      char     host[128] = {0};
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

    cJSON* pDestId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
      char     host[128] = {0};
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
    cJSON_AddItemToObject(pRoot, "destId", pDestId);

    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
    cJSON_AddStringToObject(pRoot, "term", u64buf);

2569 2570
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->snapStart);
    cJSON_AddStringToObject(pRoot, "snap-start", u64buf);
M
Minghao Li 已提交
2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612
  }

  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncPreSnapshotReply", pRoot);
  return pJson;
}

char* syncPreSnapshotReply2Str(const SyncPreSnapshotReply* pMsg) {
  cJSON* pJson = syncPreSnapshotReply2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

void syncPreSnapshotReplyPrint(const SyncPreSnapshotReply* pMsg) {
  char* serialized = syncPreSnapshotReply2Str(pMsg);
  printf("syncPreSnapshotReplyPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncPreSnapshotReplyPrint2(char* s, const SyncPreSnapshotReply* pMsg) {
  char* serialized = syncPreSnapshotReply2Str(pMsg);
  printf("syncPreSnapshotReplyPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncPreSnapshotReplyLog(const SyncPreSnapshotReply* pMsg) {
  char* serialized = syncPreSnapshotReply2Str(pMsg);
  sTrace("syncPreSnapshotReplyLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
  taosMemoryFree(serialized);
}

void syncPreSnapshotReplyLog2(char* s, const SyncPreSnapshotReply* pMsg) {
  if (gRaftDetailLog) {
    char* serialized = syncPreSnapshotReply2Str(pMsg);
    sTrace("syncPreSnapshotReplyLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
    taosMemoryFree(serialized);
  }
}

M
Minghao Li 已提交
2613
// ---- message process SyncApplyMsg----
M
Minghao Li 已提交
2614 2615 2616 2617 2618
SyncApplyMsg* syncApplyMsgBuild(uint32_t dataLen) {
  uint32_t      bytes = sizeof(SyncApplyMsg) + dataLen;
  SyncApplyMsg* pMsg = taosMemoryMalloc(bytes);
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
2619
  pMsg->msgType = TDMT_SYNC_APPLY_MSG;
M
Minghao Li 已提交
2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639
  pMsg->dataLen = dataLen;
  return pMsg;
}

SyncApplyMsg* syncApplyMsgBuild2(const SRpcMsg* pOriginalRpcMsg, int32_t vgId, SFsmCbMeta* pMeta) {
  SyncApplyMsg* pMsg = syncApplyMsgBuild(pOriginalRpcMsg->contLen);
  pMsg->vgId = vgId;
  pMsg->originalRpcType = pOriginalRpcMsg->msgType;
  pMsg->fsmMeta = *pMeta;
  memcpy(pMsg->data, pOriginalRpcMsg->pCont, pOriginalRpcMsg->contLen);
  return pMsg;
}

void syncApplyMsgDestroy(SyncApplyMsg* pMsg) {
  if (pMsg != NULL) {
    taosMemoryFree(pMsg);
  }
}

void syncApplyMsgSerialize(const SyncApplyMsg* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
2640
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
2641 2642 2643 2644 2645
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncApplyMsgDeserialize(const char* buf, uint32_t len, SyncApplyMsg* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
2646
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
2647 2648 2649 2650
}

char* syncApplyMsgSerialize2(const SyncApplyMsg* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
2651
  ASSERT(buf != NULL);
M
Minghao Li 已提交
2652 2653 2654 2655 2656 2657 2658 2659 2660 2661
  syncApplyMsgSerialize(pMsg, buf, pMsg->bytes);
  if (len != NULL) {
    *len = pMsg->bytes;
  }
  return buf;
}

SyncApplyMsg* syncApplyMsgDeserialize2(const char* buf, uint32_t len) {
  uint32_t      bytes = *((uint32_t*)buf);
  SyncApplyMsg* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
2662
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
2663
  syncApplyMsgDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
2664
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
2665 2666
  return pMsg;
}
M
Minghao Li 已提交
2667 2668

// SyncApplyMsg to SRpcMsg, put it into ApplyQ
M
Minghao Li 已提交
2669 2670 2671 2672 2673 2674 2675
void syncApplyMsg2RpcMsg(const SyncApplyMsg* pMsg, SRpcMsg* pRpcMsg) {
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
  pRpcMsg->msgType = pMsg->msgType;
  pRpcMsg->contLen = pMsg->bytes;
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
  syncApplyMsgSerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen);
}
M
Minghao Li 已提交
2676 2677

// get SRpcMsg from ApplyQ, to SyncApplyMsg
M
Minghao Li 已提交
2678 2679 2680 2681 2682 2683 2684 2685
void syncApplyMsgFromRpcMsg(const SRpcMsg* pRpcMsg, SyncApplyMsg* pMsg) {
  syncApplyMsgDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
}

SyncApplyMsg* syncApplyMsgFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncApplyMsg* pMsg = syncApplyMsgDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
  return pMsg;
}
M
Minghao Li 已提交
2686 2687

// SyncApplyMsg to OriginalRpcMsg
M
Minghao Li 已提交
2688 2689 2690 2691 2692 2693 2694 2695 2696
void syncApplyMsg2OriginalRpcMsg(const SyncApplyMsg* pMsg, SRpcMsg* pOriginalRpcMsg) {
  memset(pOriginalRpcMsg, 0, sizeof(*pOriginalRpcMsg));
  pOriginalRpcMsg->msgType = pMsg->originalRpcType;
  pOriginalRpcMsg->contLen = pMsg->dataLen;
  pOriginalRpcMsg->pCont = rpcMallocCont(pOriginalRpcMsg->contLen);
  memcpy(pOriginalRpcMsg->pCont, pMsg->data, pOriginalRpcMsg->contLen);
}

cJSON* syncApplyMsg2Json(const SyncApplyMsg* pMsg) {
2697
  char   u64buf[128] = {0};
M
Minghao Li 已提交
2698 2699 2700 2701 2702 2703 2704 2705
  cJSON* pRoot = cJSON_CreateObject();

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);
    cJSON_AddNumberToObject(pRoot, "originalRpcType", pMsg->originalRpcType);

S
Shengliang Guan 已提交
2706
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->fsmMeta.index);
M
Minghao Li 已提交
2707 2708 2709 2710
    cJSON_AddStringToObject(pRoot, "fsmMeta.index", u64buf);
    cJSON_AddNumberToObject(pRoot, "fsmMeta.isWeak", pMsg->fsmMeta.isWeak);
    cJSON_AddNumberToObject(pRoot, "fsmMeta.code", pMsg->fsmMeta.code);
    cJSON_AddNumberToObject(pRoot, "fsmMeta.state", pMsg->fsmMeta.state);
2711
    cJSON_AddStringToObject(pRoot, "fsmMeta.state.str", syncStr(pMsg->fsmMeta.state));
S
Shengliang Guan 已提交
2712
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->fsmMeta.seqNum);
M
Minghao Li 已提交
2713 2714 2715 2716
    cJSON_AddStringToObject(pRoot, "fsmMeta.seqNum", u64buf);

    cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen);
    char* s;
S
Shengliang Guan 已提交
2717
    s = syncUtilPrintBin((char*)(pMsg->data), pMsg->dataLen);
M
Minghao Li 已提交
2718 2719
    cJSON_AddStringToObject(pRoot, "data", s);
    taosMemoryFree(s);
S
Shengliang Guan 已提交
2720
    s = syncUtilPrintBin2((char*)(pMsg->data), pMsg->dataLen);
M
Minghao Li 已提交
2721 2722 2723
    cJSON_AddStringToObject(pRoot, "data2", s);
    taosMemoryFree(s);
  }
M
Minghao Li 已提交
2724

M
Minghao Li 已提交
2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735
  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncApplyMsg", pRoot);
  return pJson;
}

char* syncApplyMsg2Str(const SyncApplyMsg* pMsg) {
  cJSON* pJson = syncApplyMsg2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}
M
Minghao Li 已提交
2736 2737

// for debug ----------------------
M
Minghao Li 已提交
2738 2739
void syncApplyMsgPrint(const SyncApplyMsg* pMsg) {
  char* serialized = syncApplyMsg2Str(pMsg);
S
Shengliang Guan 已提交
2740
  printf("syncApplyMsgPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
2741 2742 2743 2744 2745 2746
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncApplyMsgPrint2(char* s, const SyncApplyMsg* pMsg) {
  char* serialized = syncApplyMsg2Str(pMsg);
S
Shengliang Guan 已提交
2747
  printf("syncApplyMsgPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
2748 2749 2750 2751 2752 2753
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncApplyMsgLog(const SyncApplyMsg* pMsg) {
  char* serialized = syncApplyMsg2Str(pMsg);
S
Shengliang Guan 已提交
2754
  sTrace("ssyncApplyMsgLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
2755 2756 2757 2758
  taosMemoryFree(serialized);
}

void syncApplyMsgLog2(char* s, const SyncApplyMsg* pMsg) {
M
Minghao Li 已提交
2759 2760
  if (gRaftDetailLog) {
    char* serialized = syncApplyMsg2Str(pMsg);
S
Shengliang Guan 已提交
2761
    sTrace("syncApplyMsgLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
2762 2763
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
2764
}
2765 2766 2767 2768 2769 2770 2771 2772

// ---------------------------------------------
SyncSnapshotSend* syncSnapshotSendBuild(uint32_t dataLen, int32_t vgId) {
  uint32_t          bytes = sizeof(SyncSnapshotSend) + dataLen;
  SyncSnapshotSend* pMsg = taosMemoryMalloc(bytes);
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
  pMsg->vgId = vgId;
2773
  pMsg->msgType = TDMT_SYNC_SNAPSHOT_SEND;
2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784
  pMsg->dataLen = dataLen;
  return pMsg;
}

void syncSnapshotSendDestroy(SyncSnapshotSend* pMsg) {
  if (pMsg != NULL) {
    taosMemoryFree(pMsg);
  }
}

void syncSnapshotSendSerialize(const SyncSnapshotSend* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
2785
  ASSERT(pMsg->bytes <= bufLen);
2786 2787 2788 2789 2790
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncSnapshotSendDeserialize(const char* buf, uint32_t len, SyncSnapshotSend* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
2791 2792
  ASSERT(len == pMsg->bytes);
  ASSERT(pMsg->bytes == sizeof(SyncSnapshotSend) + pMsg->dataLen);
2793 2794 2795 2796
}

char* syncSnapshotSendSerialize2(const SyncSnapshotSend* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
2797
  ASSERT(buf != NULL);
2798 2799 2800 2801 2802 2803 2804 2805 2806 2807
  syncSnapshotSendSerialize(pMsg, buf, pMsg->bytes);
  if (len != NULL) {
    *len = pMsg->bytes;
  }
  return buf;
}

SyncSnapshotSend* syncSnapshotSendDeserialize2(const char* buf, uint32_t len) {
  uint32_t          bytes = *((uint32_t*)buf);
  SyncSnapshotSend* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
2808
  ASSERT(pMsg != NULL);
2809
  syncSnapshotSendDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
2810
  ASSERT(len == pMsg->bytes);
2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827
  return pMsg;
}

void syncSnapshotSend2RpcMsg(const SyncSnapshotSend* pMsg, SRpcMsg* pRpcMsg) {
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
  pRpcMsg->msgType = pMsg->msgType;
  pRpcMsg->contLen = pMsg->bytes;
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
  syncSnapshotSendSerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen);
}

void syncSnapshotSendFromRpcMsg(const SRpcMsg* pRpcMsg, SyncSnapshotSend* pMsg) {
  syncSnapshotSendDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
}

SyncSnapshotSend* syncSnapshotSendFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncSnapshotSend* pMsg = syncSnapshotSendDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
2828
  ASSERT(pMsg != NULL);
2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841
  return pMsg;
}

cJSON* syncSnapshotSend2Json(const SyncSnapshotSend* pMsg) {
  char   u64buf[128];
  cJSON* pRoot = cJSON_CreateObject();

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
2842
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
      char     host[128];
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

    cJSON* pDestId = cJSON_CreateObject();
S
Shengliang Guan 已提交
2857
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
      char     host[128];
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
    cJSON_AddItemToObject(pRoot, "destId", pDestId);

S
Shengliang Guan 已提交
2871
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
2872 2873
    cJSON_AddStringToObject(pRoot, "term", u64buf);

M
Minghao Li 已提交
2874 2875
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->startTime);
    cJSON_AddStringToObject(pRoot, "startTime", u64buf);
2876

S
Shengliang Guan 已提交
2877
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->beginIndex);
2878 2879
    cJSON_AddStringToObject(pRoot, "beginIndex", u64buf);

S
Shengliang Guan 已提交
2880
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->lastIndex);
2881 2882
    cJSON_AddStringToObject(pRoot, "lastIndex", u64buf);

S
Shengliang Guan 已提交
2883
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->lastConfigIndex);
2884 2885 2886
    cJSON_AddStringToObject(pRoot, "lastConfigIndex", u64buf);
    cJSON_AddItemToObject(pRoot, "lastConfig", syncCfg2Json((SSyncCfg*)&(pMsg->lastConfig)));

S
Shengliang Guan 已提交
2887
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->lastTerm);
2888 2889 2890 2891 2892 2893
    cJSON_AddStringToObject(pRoot, "lastTerm", u64buf);

    cJSON_AddNumberToObject(pRoot, "seq", pMsg->seq);

    cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen);
    char* s;
S
Shengliang Guan 已提交
2894
    s = syncUtilPrintBin((char*)(pMsg->data), pMsg->dataLen);
2895 2896
    cJSON_AddStringToObject(pRoot, "data", s);
    taosMemoryFree(s);
S
Shengliang Guan 已提交
2897
    s = syncUtilPrintBin2((char*)(pMsg->data), pMsg->dataLen);
2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916
    cJSON_AddStringToObject(pRoot, "data2", s);
    taosMemoryFree(s);
  }

  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncSnapshotSend", pRoot);
  return pJson;
}

char* syncSnapshotSend2Str(const SyncSnapshotSend* pMsg) {
  cJSON* pJson = syncSnapshotSend2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

// for debug ----------------------
void syncSnapshotSendPrint(const SyncSnapshotSend* pMsg) {
  char* serialized = syncSnapshotSend2Str(pMsg);
S
Shengliang Guan 已提交
2917
  printf("syncSnapshotSendPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
2918 2919 2920 2921 2922 2923
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncSnapshotSendPrint2(char* s, const SyncSnapshotSend* pMsg) {
  char* serialized = syncSnapshotSend2Str(pMsg);
S
Shengliang Guan 已提交
2924
  printf("syncSnapshotSendPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
2925 2926 2927 2928 2929 2930
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncSnapshotSendLog(const SyncSnapshotSend* pMsg) {
  char* serialized = syncSnapshotSend2Str(pMsg);
S
Shengliang Guan 已提交
2931
  sTrace("syncSnapshotSendLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
2932 2933 2934 2935
  taosMemoryFree(serialized);
}

void syncSnapshotSendLog2(char* s, const SyncSnapshotSend* pMsg) {
M
Minghao Li 已提交
2936 2937
  if (gRaftDetailLog) {
    char* serialized = syncSnapshotSend2Str(pMsg);
S
Shengliang Guan 已提交
2938
    sTrace("syncSnapshotSendLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
2939 2940
    taosMemoryFree(serialized);
  }
2941 2942 2943
}

// ---------------------------------------------
M
Minghao Li 已提交
2944
SyncSnapshotRsp* syncSnapshotRspBuild(int32_t vgId) {
2945 2946 2947 2948 2949
  uint32_t         bytes = sizeof(SyncSnapshotRsp);
  SyncSnapshotRsp* pMsg = taosMemoryMalloc(bytes);
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
  pMsg->vgId = vgId;
2950
  pMsg->msgType = TDMT_SYNC_SNAPSHOT_RSP;
2951 2952 2953 2954 2955 2956 2957 2958 2959 2960
  return pMsg;
}

void syncSnapshotRspDestroy(SyncSnapshotRsp* pMsg) {
  if (pMsg != NULL) {
    taosMemoryFree(pMsg);
  }
}

void syncSnapshotRspSerialize(const SyncSnapshotRsp* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
2961
  ASSERT(pMsg->bytes <= bufLen);
2962 2963 2964 2965 2966
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncSnapshotRspDeserialize(const char* buf, uint32_t len, SyncSnapshotRsp* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
2967
  ASSERT(len == pMsg->bytes);
2968 2969 2970 2971
}

char* syncSnapshotRspSerialize2(const SyncSnapshotRsp* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
2972
  ASSERT(buf != NULL);
2973 2974 2975 2976 2977 2978 2979 2980 2981 2982
  syncSnapshotRspSerialize(pMsg, buf, pMsg->bytes);
  if (len != NULL) {
    *len = pMsg->bytes;
  }
  return buf;
}

SyncSnapshotRsp* syncSnapshotRspDeserialize2(const char* buf, uint32_t len) {
  uint32_t         bytes = *((uint32_t*)buf);
  SyncSnapshotRsp* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
2983
  ASSERT(pMsg != NULL);
2984
  syncSnapshotRspDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
2985
  ASSERT(len == pMsg->bytes);
2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002
  return pMsg;
}

void syncSnapshotRsp2RpcMsg(const SyncSnapshotRsp* pMsg, SRpcMsg* pRpcMsg) {
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
  pRpcMsg->msgType = pMsg->msgType;
  pRpcMsg->contLen = pMsg->bytes;
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
  syncSnapshotRspSerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen);
}

void syncSnapshotRspFromRpcMsg(const SRpcMsg* pRpcMsg, SyncSnapshotRsp* pMsg) {
  syncSnapshotRspDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
}

SyncSnapshotRsp* syncSnapshotRspFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncSnapshotRsp* pMsg = syncSnapshotRspDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
3003
  ASSERT(pMsg != NULL);
3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016
  return pMsg;
}

cJSON* syncSnapshotRsp2Json(const SyncSnapshotRsp* pMsg) {
  char   u64buf[128];
  cJSON* pRoot = cJSON_CreateObject();

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
3017
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
      char     host[128];
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

    cJSON* pDestId = cJSON_CreateObject();
S
Shengliang Guan 已提交
3032
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
      char     host[128];
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
    cJSON_AddItemToObject(pRoot, "destId", pDestId);

S
Shengliang Guan 已提交
3046
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
3047 3048
    cJSON_AddStringToObject(pRoot, "term", u64buf);

M
Minghao Li 已提交
3049 3050
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->startTime);
    cJSON_AddStringToObject(pRoot, "startTime", u64buf);
3051

S
Shengliang Guan 已提交
3052
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->lastIndex);
3053 3054
    cJSON_AddStringToObject(pRoot, "lastIndex", u64buf);

S
Shengliang Guan 已提交
3055
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->lastTerm);
3056 3057 3058
    cJSON_AddStringToObject(pRoot, "lastTerm", u64buf);

    cJSON_AddNumberToObject(pRoot, "ack", pMsg->ack);
3059
    cJSON_AddNumberToObject(pRoot, "code", pMsg->code);
M
Minghao Li 已提交
3060 3061 3062

    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->snapBeginIndex);
    cJSON_AddStringToObject(pRoot, "snap-begin", u64buf);
3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079
  }

  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncSnapshotRsp", pRoot);
  return pJson;
}

char* syncSnapshotRsp2Str(const SyncSnapshotRsp* pMsg) {
  cJSON* pJson = syncSnapshotRsp2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

// for debug ----------------------
void syncSnapshotRspPrint(const SyncSnapshotRsp* pMsg) {
  char* serialized = syncSnapshotRsp2Str(pMsg);
S
Shengliang Guan 已提交
3080
  printf("syncSnapshotRspPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
3081 3082 3083 3084 3085 3086
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncSnapshotRspPrint2(char* s, const SyncSnapshotRsp* pMsg) {
  char* serialized = syncSnapshotRsp2Str(pMsg);
S
Shengliang Guan 已提交
3087
  printf("syncSnapshotRspPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
3088 3089 3090 3091 3092 3093
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncSnapshotRspLog(const SyncSnapshotRsp* pMsg) {
  char* serialized = syncSnapshotRsp2Str(pMsg);
S
Shengliang Guan 已提交
3094
  sTrace("syncSnapshotRspLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
3095 3096 3097 3098
  taosMemoryFree(serialized);
}

void syncSnapshotRspLog2(char* s, const SyncSnapshotRsp* pMsg) {
M
Minghao Li 已提交
3099 3100
  if (gRaftDetailLog) {
    char* serialized = syncSnapshotRsp2Str(pMsg);
S
Shengliang Guan 已提交
3101
    sTrace("syncSnapshotRspLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
3102 3103
    taosMemoryFree(serialized);
  }
3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123
}

// ---------------------------------------------
SyncLeaderTransfer* syncLeaderTransferBuild(int32_t vgId) {
  uint32_t            bytes = sizeof(SyncLeaderTransfer);
  SyncLeaderTransfer* pMsg = taosMemoryMalloc(bytes);
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
  pMsg->vgId = vgId;
  pMsg->msgType = TDMT_SYNC_LEADER_TRANSFER;
  return pMsg;
}

void syncLeaderTransferDestroy(SyncLeaderTransfer* pMsg) {
  if (pMsg != NULL) {
    taosMemoryFree(pMsg);
  }
}

void syncLeaderTransferSerialize(const SyncLeaderTransfer* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
3124
  ASSERT(pMsg->bytes <= bufLen);
3125 3126 3127 3128 3129
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncLeaderTransferDeserialize(const char* buf, uint32_t len, SyncLeaderTransfer* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
3130
  ASSERT(len == pMsg->bytes);
3131 3132 3133 3134
}

char* syncLeaderTransferSerialize2(const SyncLeaderTransfer* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
3135
  ASSERT(buf != NULL);
3136 3137 3138 3139 3140 3141 3142 3143 3144 3145
  syncLeaderTransferSerialize(pMsg, buf, pMsg->bytes);
  if (len != NULL) {
    *len = pMsg->bytes;
  }
  return buf;
}

SyncLeaderTransfer* syncLeaderTransferDeserialize2(const char* buf, uint32_t len) {
  uint32_t            bytes = *((uint32_t*)buf);
  SyncLeaderTransfer* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
3146
  ASSERT(pMsg != NULL);
3147
  syncLeaderTransferDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
3148
  ASSERT(len == pMsg->bytes);
3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165
  return pMsg;
}

void syncLeaderTransfer2RpcMsg(const SyncLeaderTransfer* pMsg, SRpcMsg* pRpcMsg) {
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
  pRpcMsg->msgType = pMsg->msgType;
  pRpcMsg->contLen = pMsg->bytes;
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
  syncLeaderTransferSerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen);
}

void syncLeaderTransferFromRpcMsg(const SRpcMsg* pRpcMsg, SyncLeaderTransfer* pMsg) {
  syncLeaderTransferDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
}

SyncLeaderTransfer* syncLeaderTransferFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncLeaderTransfer* pMsg = syncLeaderTransferDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
3166
  ASSERT(pMsg != NULL);
3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180
  return pMsg;
}

cJSON* syncLeaderTransfer2Json(const SyncLeaderTransfer* pMsg) {
  char   u64buf[128];
  cJSON* pRoot = cJSON_CreateObject();

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    /*
        cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
3181
        snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195
        cJSON_AddStringToObject(pSrcId, "addr", u64buf);
        {
          uint64_t u64 = pMsg->srcId.addr;
          cJSON*   pTmp = pSrcId;
          char     host[128];
          uint16_t port;
          syncUtilU642Addr(u64, host, sizeof(host), &port);
          cJSON_AddStringToObject(pTmp, "addr_host", host);
          cJSON_AddNumberToObject(pTmp, "addr_port", port);
        }
        cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
        cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

        cJSON* pDestId = cJSON_CreateObject();
S
Shengliang Guan 已提交
3196
        snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211
        cJSON_AddStringToObject(pDestId, "addr", u64buf);
        {
          uint64_t u64 = pMsg->destId.addr;
          cJSON*   pTmp = pDestId;
          char     host[128];
          uint16_t port;
          syncUtilU642Addr(u64, host, sizeof(host), &port);
          cJSON_AddStringToObject(pTmp, "addr_host", host);
          cJSON_AddNumberToObject(pTmp, "addr_port", port);
        }
        cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
        cJSON_AddItemToObject(pRoot, "destId", pDestId);
    */

    cJSON* pNewerId = cJSON_CreateObject();
S
Shengliang Guan 已提交
3212
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->newLeaderId.addr);
3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238
    cJSON_AddStringToObject(pNewerId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->newLeaderId.addr;
      cJSON*   pTmp = pNewerId;
      char     host[128];
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pNewerId, "vgId", pMsg->newLeaderId.vgId);
    cJSON_AddItemToObject(pRoot, "newLeaderId", pNewerId);
  }

  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncLeaderTransfer", pRoot);
  return pJson;
}

char* syncLeaderTransfer2Str(const SyncLeaderTransfer* pMsg) {
  cJSON* pJson = syncLeaderTransfer2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

3239 3240 3241
const char* syncLocalCmdGetStr(int32_t cmd) {
  if (cmd == SYNC_LOCAL_CMD_STEP_DOWN) {
    return "step-down";
3242 3243
  } else if (cmd == SYNC_LOCAL_CMD_FOLLOWER_CMT) {
    return "follower-commit";
3244 3245 3246 3247 3248
  }

  return "unknown-local-cmd";
}

M
Minghao Li 已提交
3249
SyncLocalCmd* syncLocalCmdBuild(int32_t vgId) {
M
Minghao Li 已提交
3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354
  uint32_t      bytes = sizeof(SyncLocalCmd);
  SyncLocalCmd* pMsg = taosMemoryMalloc(bytes);
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
  pMsg->vgId = vgId;
  pMsg->msgType = TDMT_SYNC_LOCAL_CMD;
  return pMsg;
}

void syncLocalCmdDestroy(SyncLocalCmd* pMsg) {
  if (pMsg != NULL) {
    taosMemoryFree(pMsg);
  }
}

void syncLocalCmdSerialize(const SyncLocalCmd* pMsg, char* buf, uint32_t bufLen) {
  ASSERT(pMsg->bytes <= bufLen);
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncLocalCmdDeserialize(const char* buf, uint32_t len, SyncLocalCmd* pMsg) {
  memcpy(pMsg, buf, len);
  ASSERT(len == pMsg->bytes);
}

char* syncLocalCmdSerialize2(const SyncLocalCmd* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
  ASSERT(buf != NULL);
  syncLocalCmdSerialize(pMsg, buf, pMsg->bytes);
  if (len != NULL) {
    *len = pMsg->bytes;
  }
  return buf;
}

SyncLocalCmd* syncLocalCmdDeserialize2(const char* buf, uint32_t len) {
  uint32_t      bytes = *((uint32_t*)buf);
  SyncLocalCmd* pMsg = taosMemoryMalloc(bytes);
  ASSERT(pMsg != NULL);
  syncLocalCmdDeserialize(buf, len, pMsg);
  ASSERT(len == pMsg->bytes);
  return pMsg;
}

void syncLocalCmd2RpcMsg(const SyncLocalCmd* pMsg, SRpcMsg* pRpcMsg) {
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
  pRpcMsg->msgType = pMsg->msgType;
  pRpcMsg->contLen = pMsg->bytes;
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
  syncLocalCmdSerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen);
}

void syncLocalCmdFromRpcMsg(const SRpcMsg* pRpcMsg, SyncLocalCmd* pMsg) {
  syncLocalCmdDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
}

SyncLocalCmd* syncLocalCmdFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncLocalCmd* pMsg = syncLocalCmdDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
  ASSERT(pMsg != NULL);
  return pMsg;
}

cJSON* syncLocalCmd2Json(const SyncLocalCmd* pMsg) {
  char   u64buf[128];
  cJSON* pRoot = cJSON_CreateObject();

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
      char     host[128];
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

    cJSON* pDestId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
      char     host[128];
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
    cJSON_AddItemToObject(pRoot, "destId", pDestId);

    cJSON_AddNumberToObject(pRoot, "cmd", pMsg->cmd);

    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->sdNewTerm);
    cJSON_AddStringToObject(pRoot, "sd-new-term", u64buf);
3355 3356 3357

    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->fcIndex);
    cJSON_AddStringToObject(pRoot, "fc-index", u64buf);
M
Minghao Li 已提交
3358 3359 3360
  }

  cJSON* pJson = cJSON_CreateObject();
M
Minghao Li 已提交
3361
  cJSON_AddItemToObject(pJson, "SyncLocalCmd2Json", pRoot);
M
Minghao Li 已提交
3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399
  return pJson;
}

char* syncLocalCmd2Str(const SyncLocalCmd* pMsg) {
  cJSON* pJson = syncLocalCmd2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

// for debug ----------------------
void syncLocalCmdPrint(const SyncLocalCmd* pMsg) {
  char* serialized = syncLocalCmd2Str(pMsg);
  printf("syncLocalCmdPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncLocalCmdPrint2(char* s, const SyncLocalCmd* pMsg) {
  char* serialized = syncLocalCmd2Str(pMsg);
  printf("syncLocalCmdPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncLocalCmdLog(const SyncLocalCmd* pMsg) {
  char* serialized = syncLocalCmd2Str(pMsg);
  sTrace("syncLocalCmdLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
  taosMemoryFree(serialized);
}

void syncLocalCmdLog2(char* s, const SyncLocalCmd* pMsg) {
  if (gRaftDetailLog) {
    char* serialized = syncLocalCmd2Str(pMsg);
    sTrace("syncLocalCmdLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
    taosMemoryFree(serialized);
  }
}