syncMessage.c 102.6 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 88 89
    pRoot = cJSON_CreateObject();
    char* s;
    s = syncUtilprintBin((char*)(pRpcMsg->pCont), pRpcMsg->contLen);
    cJSON_AddStringToObject(pRoot, "pCont", s);
wafwerar's avatar
wafwerar 已提交
90
    taosMemoryFree(s);
M
Minghao Li 已提交
91 92
    s = syncUtilprintBin2((char*)(pRpcMsg->pCont), pRpcMsg->contLen);
    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 98 99
    char* s;
    s = syncUtilprintBin((char*)(pRpcMsg->pCont), pRpcMsg->contLen);
    cJSON_AddStringToObject(pRoot, "pCont", s);
wafwerar's avatar
wafwerar 已提交
100
    taosMemoryFree(s);
M
Minghao Li 已提交
101 102
    s = syncUtilprintBin2((char*)(pRpcMsg->pCont), pRpcMsg->contLen);
    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 514 515
      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 = syncUtilprintBin((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data", s);
wafwerar's avatar
wafwerar 已提交
516
    taosMemoryFree(s);
M
Minghao Li 已提交
517 518
    s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen);
    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 784 785
      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 = syncUtilprintBin((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data", s);
wafwerar's avatar
wafwerar 已提交
786
    taosMemoryFree(s);
M
Minghao Li 已提交
787 788
    s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen);
    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 834
// ---- message process SyncClientRequest----
SyncClientRequest* syncClientRequestBuild(uint32_t dataLen) {
M
Minghao Li 已提交
835
  uint32_t           bytes = sizeof(SyncClientRequest) + dataLen;
wafwerar's avatar
wafwerar 已提交
836
  SyncClientRequest* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
837 838
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
839
  pMsg->msgType = TDMT_SYNC_CLIENT_REQUEST;
M
Minghao Li 已提交
840 841 842 843 844 845
  pMsg->seqNum = 0;
  pMsg->isWeak = false;
  pMsg->dataLen = dataLen;
  return pMsg;
}

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

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

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

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

M
Minghao Li 已提交
873
char* syncClientRequestSerialize2(const SyncClientRequest* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
874
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
875
  ASSERT(buf != NULL);
M
Minghao Li 已提交
876 877 878 879 880 881 882 883 884
  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 已提交
885
  SyncClientRequest* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
886
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
887
  syncClientRequestDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
888
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
889 890 891
  return pMsg;
}

M
Minghao Li 已提交
892
// step 2. SyncClientRequest => RpcMsg, to queue
M
Minghao Li 已提交
893 894 895 896 897 898 899 900 901 902 903 904
void syncClientRequest2RpcMsg(const SyncClientRequest* pMsg, SRpcMsg* pRpcMsg) {
  memset(pRpcMsg, 0, sizeof(*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 已提交
905
// step 3. RpcMsg => SyncClientRequest, from queue
M
Minghao Li 已提交
906 907
SyncClientRequest* syncClientRequestFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncClientRequest* pMsg = syncClientRequestDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
908
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
909 910 911
  return pMsg;
}

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

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

    char* s;
    s = syncUtilprintBin((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data", s);
wafwerar's avatar
wafwerar 已提交
929
    taosMemoryFree(s);
M
Minghao Li 已提交
930 931
    s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data2", s);
wafwerar's avatar
wafwerar 已提交
932
    taosMemoryFree(s);
M
Minghao Li 已提交
933
  }
M
Minghao Li 已提交
934

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

M
Minghao Li 已提交
940 941 942 943 944 945 946 947 948 949
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 已提交
950
  printf("syncClientRequestPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
951
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
952
  taosMemoryFree(serialized);
M
Minghao Li 已提交
953 954 955 956
}

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

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

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

976 977 978 979 980 981
// ---- message process SyncClientRequestBatch----

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

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

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

  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
1010
    msgArr[i] = *(rpcMsgPArr[i]);
1011 1012 1013 1014 1015
  }

  return pMsg;
}

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 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
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 = syncUtilprintBin((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data", s);
    taosMemoryFree(s);
    s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen);
    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 已提交
1120
  printf("syncClientRequestBatchPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
1121 1122 1123 1124 1125 1126
  fflush(NULL);
  taosMemoryFree(serialized);
}

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

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

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

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

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

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

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

M
Minghao Li 已提交
1173
char* syncRequestVoteSerialize2(const SyncRequestVote* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
1174
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
1175
  ASSERT(buf != NULL);
M
Minghao Li 已提交
1176 1177 1178 1179 1180 1181 1182 1183 1184
  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 已提交
1185
  SyncRequestVote* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1186
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1187
  syncRequestVoteDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
1188
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1189 1190 1191
  return pMsg;
}

M
Minghao Li 已提交
1192 1193 1194 1195 1196 1197 1198
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 已提交
1199

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

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

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

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

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
1220
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
M
Minghao Li 已提交
1221 1222 1223 1224
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
1225
      char     host[128] = {0};
M
Minghao Li 已提交
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
      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;
1239
      char     host[128] = {0};
M
Minghao Li 已提交
1240 1241 1242 1243 1244 1245 1246 1247
      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 已提交
1248
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
M
Minghao Li 已提交
1249
    cJSON_AddStringToObject(pRoot, "term", u64buf);
S
Shengliang Guan 已提交
1250
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->lastLogIndex);
M
Minghao Li 已提交
1251
    cJSON_AddStringToObject(pRoot, "lastLogIndex", u64buf);
S
Shengliang Guan 已提交
1252
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->lastLogTerm);
M
Minghao Li 已提交
1253 1254
    cJSON_AddStringToObject(pRoot, "lastLogTerm", u64buf);
  }
M
Minghao Li 已提交
1255 1256 1257 1258

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

M
Minghao Li 已提交
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
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 已提交
1271
  printf("syncRequestVotePrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
1272
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1273
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1274 1275 1276 1277
}

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

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

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

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

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

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

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

M
Minghao Li 已提交
1324
char* syncRequestVoteReplySerialize2(const SyncRequestVoteReply* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
1325
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
1326
  ASSERT(buf != NULL);
M
Minghao Li 已提交
1327 1328 1329 1330 1331 1332 1333 1334 1335
  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 已提交
1336
  SyncRequestVoteReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1337
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1338
  syncRequestVoteReplyDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
1339
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1340 1341 1342
  return pMsg;
}

M
Minghao Li 已提交
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
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 已提交
1355 1356
SyncRequestVoteReply* syncRequestVoteReplyFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncRequestVoteReply* pMsg = syncRequestVoteReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
1357
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1358 1359 1360
  return pMsg;
}

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

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

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
1371
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
M
Minghao Li 已提交
1372 1373 1374 1375
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
1376
      char     host[128] = {0};
M
Minghao Li 已提交
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
      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;
1390
      char     host[128] = {0};
M
Minghao Li 已提交
1391 1392 1393 1394 1395 1396 1397 1398
      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 已提交
1399
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
M
Minghao Li 已提交
1400 1401 1402
    cJSON_AddStringToObject(pRoot, "term", u64buf);
    cJSON_AddNumberToObject(pRoot, "vote_granted", pMsg->voteGranted);
  }
M
Minghao Li 已提交
1403 1404 1405 1406

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

M
Minghao Li 已提交
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
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 已提交
1419
  printf("syncRequestVoteReplyPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
1420
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1421
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1422 1423 1424 1425
}

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

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

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

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

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

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

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

char* syncAppendEntriesSerialize2(const SyncAppendEntries* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
1475
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
1476
  ASSERT(buf != NULL);
M
Minghao Li 已提交
1477 1478 1479 1480 1481 1482 1483 1484 1485
  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 已提交
1486
  SyncAppendEntries* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1487
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1488
  syncAppendEntriesDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
1489
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1490
  return pMsg;
M
Minghao Li 已提交
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
}

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 已提交
1505 1506
SyncAppendEntries* syncAppendEntriesFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncAppendEntries* pMsg = syncAppendEntriesDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
1507
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1508 1509 1510
  return pMsg;
}

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

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

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
1521
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
M
Minghao Li 已提交
1522 1523 1524 1525
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
1526
      char     host[128] = {0};
M
Minghao Li 已提交
1527 1528 1529 1530 1531 1532 1533 1534 1535
      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 已提交
1536
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
M
Minghao Li 已提交
1537 1538 1539 1540
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
1541
      char     host[128] = {0};
M
Minghao Li 已提交
1542 1543 1544 1545 1546 1547 1548 1549
      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 已提交
1550
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
M
Minghao Li 已提交
1551 1552
    cJSON_AddStringToObject(pRoot, "term", u64buf);

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

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

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

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

    cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen);
    char* s;
    s = syncUtilprintBin((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data", s);
wafwerar's avatar
wafwerar 已提交
1569
    taosMemoryFree(s);
M
Minghao Li 已提交
1570 1571
    s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data2", s);
wafwerar's avatar
wafwerar 已提交
1572
    taosMemoryFree(s);
M
Minghao Li 已提交
1573
  }
M
Minghao Li 已提交
1574 1575 1576 1577 1578 1579

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

M
Minghao Li 已提交
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
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 已提交
1590
  printf("syncAppendEntriesPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
1591
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1592
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1593 1594 1595 1596
}

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

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

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

1616 1617 1618 1619
// ---- message process SyncAppendEntriesBatch----

// block1: SOffsetAndContLen
// block2: SOffsetAndContLen Array
1620
// block3: entry Array
1621

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

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

  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) {
1648
    // init meta <offset, contLen>
1649
    if (i == 0) {
1650 1651
      metaArr[i].offset = metaArrayLen;
      metaArr[i].contLen = entryPArr[i]->bytes;
1652 1653
    } else {
      metaArr[i].offset = metaArr[i - 1].offset + metaArr[i - 1].contLen;
1654
      metaArr[i].contLen = entryPArr[i]->bytes;
1655 1656
    }

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

  return pMsg;
}
1664

1665 1666
SOffsetAndContLen* syncAppendEntriesBatchMetaTableArray(SyncAppendEntriesBatch* pMsg) {
  return (SOffsetAndContLen*)(pMsg->data);
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 1731 1732

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 已提交
1733
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747
    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 已提交
1748
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
    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 已提交
1762
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
1763 1764
    cJSON_AddStringToObject(pRoot, "term", u64buf);

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

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

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

S
Shengliang Guan 已提交
1774
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->privateTerm);
1775 1776 1777 1778 1779 1780
    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>
1781
    int32_t entryArrayLen = pMsg->dataLen - metaArrayLen;
1782 1783

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

    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);
    }

1797 1798
    cJSON* pEntryArr = cJSON_CreateArray();
    cJSON_AddItemToObject(pRoot, "entryArr", pEntryArr);
1799
    for (int i = 0; i < pMsg->dataCount; ++i) {
1800 1801 1802
      SSyncRaftEntry* pEntry = (SSyncRaftEntry*)(pMsg->data + metaArr[i].offset);
      cJSON*          pEntryJson = syncEntry2Json(pEntry);
      cJSON_AddItemToArray(pEntryArr, pEntryJson);
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828
    }

    char* s;
    s = syncUtilprintBin((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data", s);
    taosMemoryFree(s);
    s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen);
    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 已提交
1829
  printf("syncAppendEntriesBatchPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
1830 1831 1832 1833 1834 1835
  fflush(NULL);
  taosMemoryFree(serialized);
}

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

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

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

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

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

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

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

M
Minghao Li 已提交
1882
char* syncAppendEntriesReplySerialize2(const SyncAppendEntriesReply* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
1883
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
1884
  ASSERT(buf != NULL);
M
Minghao Li 已提交
1885 1886 1887 1888 1889 1890 1891 1892 1893
  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 已提交
1894
  SyncAppendEntriesReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1895
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1896
  syncAppendEntriesReplyDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
1897
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1898 1899 1900
  return pMsg;
}

M
Minghao Li 已提交
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
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 已提交
1913 1914
SyncAppendEntriesReply* syncAppendEntriesReplyFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncAppendEntriesReply* pMsg = syncAppendEntriesReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
1915
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1916 1917 1918
  return pMsg;
}

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

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

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
1929
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
M
Minghao Li 已提交
1930 1931 1932 1933
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
1934
      char     host[128] = {0};
M
Minghao Li 已提交
1935 1936 1937 1938 1939 1940 1941 1942 1943
      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 已提交
1944
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
M
Minghao Li 已提交
1945 1946 1947 1948
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
1949
      char     host[128] = {0};
M
Minghao Li 已提交
1950 1951 1952 1953 1954 1955 1956 1957
      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 已提交
1958
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->privateTerm);
1959 1960
    cJSON_AddStringToObject(pRoot, "privateTerm", u64buf);

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

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

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 已提交
1985
  printf("syncAppendEntriesReplyPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
1986
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1987
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1988 1989 1990 1991
}

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

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

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

M
Minghao Li 已提交
2011 2012 2013 2014 2015 2016 2017
// ---- 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;
2018
  pMsg->msgType = TDMT_SYNC_HEARTBEAT;
M
Minghao Li 已提交
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 2136 2137
  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 已提交
2138
  printf("syncHeartbeatPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
2139 2140 2141 2142 2143 2144
  fflush(NULL);
  taosMemoryFree(serialized);
}

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

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

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

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 2290 2291
// ---- 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 已提交
2292
  printf("syncHeartbeatReplyPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
2293 2294 2295 2296 2297 2298
  fflush(NULL);
  taosMemoryFree(serialized);
}

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

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

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

M
Minghao Li 已提交
2318
// ---- message process SyncApplyMsg----
M
Minghao Li 已提交
2319 2320 2321 2322 2323
SyncApplyMsg* syncApplyMsgBuild(uint32_t dataLen) {
  uint32_t      bytes = sizeof(SyncApplyMsg) + dataLen;
  SyncApplyMsg* pMsg = taosMemoryMalloc(bytes);
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
2324
  pMsg->msgType = TDMT_SYNC_APPLY_MSG;
M
Minghao Li 已提交
2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344
  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 已提交
2345
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
2346 2347 2348 2349 2350
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncApplyMsgDeserialize(const char* buf, uint32_t len, SyncApplyMsg* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
2351
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
2352 2353 2354 2355
}

char* syncApplyMsgSerialize2(const SyncApplyMsg* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
2356
  ASSERT(buf != NULL);
M
Minghao Li 已提交
2357 2358 2359 2360 2361 2362 2363 2364 2365 2366
  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 已提交
2367
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
2368
  syncApplyMsgDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
2369
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
2370 2371
  return pMsg;
}
M
Minghao Li 已提交
2372 2373

// SyncApplyMsg to SRpcMsg, put it into ApplyQ
M
Minghao Li 已提交
2374 2375 2376 2377 2378 2379 2380
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 已提交
2381 2382

// get SRpcMsg from ApplyQ, to SyncApplyMsg
M
Minghao Li 已提交
2383 2384 2385 2386 2387 2388 2389 2390
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 已提交
2391 2392

// SyncApplyMsg to OriginalRpcMsg
M
Minghao Li 已提交
2393 2394 2395 2396 2397 2398 2399 2400 2401
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) {
2402
  char   u64buf[128] = {0};
M
Minghao Li 已提交
2403 2404 2405 2406 2407 2408 2409 2410
  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 已提交
2411
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->fsmMeta.index);
M
Minghao Li 已提交
2412 2413 2414 2415 2416
    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);
    cJSON_AddStringToObject(pRoot, "fsmMeta.state.str", syncUtilState2String(pMsg->fsmMeta.state));
S
Shengliang Guan 已提交
2417
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->fsmMeta.seqNum);
M
Minghao Li 已提交
2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428
    cJSON_AddStringToObject(pRoot, "fsmMeta.seqNum", u64buf);

    cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen);
    char* s;
    s = syncUtilprintBin((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data", s);
    taosMemoryFree(s);
    s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data2", s);
    taosMemoryFree(s);
  }
M
Minghao Li 已提交
2429

M
Minghao Li 已提交
2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
  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 已提交
2441 2442

// for debug ----------------------
M
Minghao Li 已提交
2443 2444
void syncApplyMsgPrint(const SyncApplyMsg* pMsg) {
  char* serialized = syncApplyMsg2Str(pMsg);
S
Shengliang Guan 已提交
2445
  printf("syncApplyMsgPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
2446 2447 2448 2449 2450 2451
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncApplyMsgPrint2(char* s, const SyncApplyMsg* pMsg) {
  char* serialized = syncApplyMsg2Str(pMsg);
S
Shengliang Guan 已提交
2452
  printf("syncApplyMsgPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
2453 2454 2455 2456 2457 2458
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncApplyMsgLog(const SyncApplyMsg* pMsg) {
  char* serialized = syncApplyMsg2Str(pMsg);
S
Shengliang Guan 已提交
2459
  sTrace("ssyncApplyMsgLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
2460 2461 2462 2463
  taosMemoryFree(serialized);
}

void syncApplyMsgLog2(char* s, const SyncApplyMsg* pMsg) {
M
Minghao Li 已提交
2464 2465
  if (gRaftDetailLog) {
    char* serialized = syncApplyMsg2Str(pMsg);
S
Shengliang Guan 已提交
2466
    sTrace("syncApplyMsgLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
2467 2468
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
2469
}
2470 2471 2472 2473 2474 2475 2476 2477

// ---------------------------------------------
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;
2478
  pMsg->msgType = TDMT_SYNC_SNAPSHOT_SEND;
2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489
  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 已提交
2490
  ASSERT(pMsg->bytes <= bufLen);
2491 2492 2493 2494 2495
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncSnapshotSendDeserialize(const char* buf, uint32_t len, SyncSnapshotSend* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
2496 2497
  ASSERT(len == pMsg->bytes);
  ASSERT(pMsg->bytes == sizeof(SyncSnapshotSend) + pMsg->dataLen);
2498 2499 2500 2501
}

char* syncSnapshotSendSerialize2(const SyncSnapshotSend* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
2502
  ASSERT(buf != NULL);
2503 2504 2505 2506 2507 2508 2509 2510 2511 2512
  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 已提交
2513
  ASSERT(pMsg != NULL);
2514
  syncSnapshotSendDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
2515
  ASSERT(len == pMsg->bytes);
2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532
  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 已提交
2533
  ASSERT(pMsg != NULL);
2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546
  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 已提交
2547
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561
    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 已提交
2562
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575
    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 已提交
2576
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
2577 2578
    cJSON_AddStringToObject(pRoot, "term", u64buf);

S
Shengliang Guan 已提交
2579
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->privateTerm);
2580 2581
    cJSON_AddStringToObject(pRoot, "privateTerm", u64buf);

S
Shengliang Guan 已提交
2582
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->beginIndex);
2583 2584
    cJSON_AddStringToObject(pRoot, "beginIndex", u64buf);

S
Shengliang Guan 已提交
2585
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->lastIndex);
2586 2587
    cJSON_AddStringToObject(pRoot, "lastIndex", u64buf);

S
Shengliang Guan 已提交
2588
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->lastConfigIndex);
2589 2590 2591
    cJSON_AddStringToObject(pRoot, "lastConfigIndex", u64buf);
    cJSON_AddItemToObject(pRoot, "lastConfig", syncCfg2Json((SSyncCfg*)&(pMsg->lastConfig)));

S
Shengliang Guan 已提交
2592
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->lastTerm);
2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621
    cJSON_AddStringToObject(pRoot, "lastTerm", u64buf);

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

    cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen);
    char* s;
    s = syncUtilprintBin((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data", s);
    taosMemoryFree(s);
    s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen);
    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 已提交
2622
  printf("syncSnapshotSendPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
2623 2624 2625 2626 2627 2628
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncSnapshotSendPrint2(char* s, const SyncSnapshotSend* pMsg) {
  char* serialized = syncSnapshotSend2Str(pMsg);
S
Shengliang Guan 已提交
2629
  printf("syncSnapshotSendPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
2630 2631 2632 2633 2634 2635
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncSnapshotSendLog(const SyncSnapshotSend* pMsg) {
  char* serialized = syncSnapshotSend2Str(pMsg);
S
Shengliang Guan 已提交
2636
  sTrace("syncSnapshotSendLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
2637 2638 2639 2640
  taosMemoryFree(serialized);
}

void syncSnapshotSendLog2(char* s, const SyncSnapshotSend* pMsg) {
M
Minghao Li 已提交
2641 2642
  if (gRaftDetailLog) {
    char* serialized = syncSnapshotSend2Str(pMsg);
S
Shengliang Guan 已提交
2643
    sTrace("syncSnapshotSendLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
2644 2645
    taosMemoryFree(serialized);
  }
2646 2647 2648
}

// ---------------------------------------------
M
Minghao Li 已提交
2649
SyncSnapshotRsp* syncSnapshotRspBuild(int32_t vgId) {
2650 2651 2652 2653 2654
  uint32_t         bytes = sizeof(SyncSnapshotRsp);
  SyncSnapshotRsp* pMsg = taosMemoryMalloc(bytes);
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
  pMsg->vgId = vgId;
2655
  pMsg->msgType = TDMT_SYNC_SNAPSHOT_RSP;
2656 2657 2658 2659 2660 2661 2662 2663 2664 2665
  return pMsg;
}

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

void syncSnapshotRspSerialize(const SyncSnapshotRsp* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
2666
  ASSERT(pMsg->bytes <= bufLen);
2667 2668 2669 2670 2671
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncSnapshotRspDeserialize(const char* buf, uint32_t len, SyncSnapshotRsp* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
2672
  ASSERT(len == pMsg->bytes);
2673 2674 2675 2676
}

char* syncSnapshotRspSerialize2(const SyncSnapshotRsp* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
2677
  ASSERT(buf != NULL);
2678 2679 2680 2681 2682 2683 2684 2685 2686 2687
  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 已提交
2688
  ASSERT(pMsg != NULL);
2689
  syncSnapshotRspDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
2690
  ASSERT(len == pMsg->bytes);
2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707
  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 已提交
2708
  ASSERT(pMsg != NULL);
2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721
  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 已提交
2722
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736
    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 已提交
2737
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750
    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 已提交
2751
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
2752 2753
    cJSON_AddStringToObject(pRoot, "term", u64buf);

S
Shengliang Guan 已提交
2754
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->privateTerm);
2755 2756
    cJSON_AddStringToObject(pRoot, "privateTerm", u64buf);

S
Shengliang Guan 已提交
2757
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->lastIndex);
2758 2759
    cJSON_AddStringToObject(pRoot, "lastIndex", u64buf);

S
Shengliang Guan 已提交
2760
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->lastTerm);
2761 2762 2763
    cJSON_AddStringToObject(pRoot, "lastTerm", u64buf);

    cJSON_AddNumberToObject(pRoot, "ack", pMsg->ack);
2764
    cJSON_AddNumberToObject(pRoot, "code", pMsg->code);
2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781
  }

  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 已提交
2782
  printf("syncSnapshotRspPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
2783 2784 2785 2786 2787 2788
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncSnapshotRspPrint2(char* s, const SyncSnapshotRsp* pMsg) {
  char* serialized = syncSnapshotRsp2Str(pMsg);
S
Shengliang Guan 已提交
2789
  printf("syncSnapshotRspPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
2790 2791 2792 2793 2794 2795
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncSnapshotRspLog(const SyncSnapshotRsp* pMsg) {
  char* serialized = syncSnapshotRsp2Str(pMsg);
S
Shengliang Guan 已提交
2796
  sTrace("syncSnapshotRspLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
2797 2798 2799 2800
  taosMemoryFree(serialized);
}

void syncSnapshotRspLog2(char* s, const SyncSnapshotRsp* pMsg) {
M
Minghao Li 已提交
2801 2802
  if (gRaftDetailLog) {
    char* serialized = syncSnapshotRsp2Str(pMsg);
S
Shengliang Guan 已提交
2803
    sTrace("syncSnapshotRspLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
2804 2805
    taosMemoryFree(serialized);
  }
2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825
}

// ---------------------------------------------
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 已提交
2826
  ASSERT(pMsg->bytes <= bufLen);
2827 2828 2829 2830 2831
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncLeaderTransferDeserialize(const char* buf, uint32_t len, SyncLeaderTransfer* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
2832
  ASSERT(len == pMsg->bytes);
2833 2834 2835 2836
}

char* syncLeaderTransferSerialize2(const SyncLeaderTransfer* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
2837
  ASSERT(buf != NULL);
2838 2839 2840 2841 2842 2843 2844 2845 2846 2847
  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 已提交
2848
  ASSERT(pMsg != NULL);
2849
  syncLeaderTransferDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
2850
  ASSERT(len == pMsg->bytes);
2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867
  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 已提交
2868
  ASSERT(pMsg != NULL);
2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882
  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 已提交
2883
        snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897
        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 已提交
2898
        snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913
        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 已提交
2914
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->newLeaderId.addr);
2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943
    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;
}

// for debug ----------------------
void syncLeaderTransferPrint(const SyncLeaderTransfer* pMsg) {
  char* serialized = syncLeaderTransfer2Str(pMsg);
S
Shengliang Guan 已提交
2944
  printf("syncLeaderTransferPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
2945 2946 2947 2948 2949 2950
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncLeaderTransferPrint2(char* s, const SyncLeaderTransfer* pMsg) {
  char* serialized = syncLeaderTransfer2Str(pMsg);
S
Shengliang Guan 已提交
2951
  printf("syncLeaderTransferPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
2952 2953 2954 2955 2956 2957
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncLeaderTransferLog(const SyncLeaderTransfer* pMsg) {
  char* serialized = syncLeaderTransfer2Str(pMsg);
S
Shengliang Guan 已提交
2958
  sTrace("syncLeaderTransferLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
2959 2960 2961 2962 2963 2964
  taosMemoryFree(serialized);
}

void syncLeaderTransferLog2(char* s, const SyncLeaderTransfer* pMsg) {
  if (gRaftDetailLog) {
    char* serialized = syncLeaderTransfer2Str(pMsg);
S
Shengliang Guan 已提交
2965
    sTrace("syncLeaderTransferLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
2966 2967
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987
}

// ---------------------------------------------
SyncReconfigFinish* syncReconfigFinishBuild(int32_t vgId) {
  uint32_t            bytes = sizeof(SyncReconfigFinish);
  SyncReconfigFinish* pMsg = taosMemoryMalloc(bytes);
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
  pMsg->vgId = vgId;
  pMsg->msgType = TDMT_SYNC_CONFIG_CHANGE_FINISH;
  return pMsg;
}

void syncReconfigFinishDestroy(SyncReconfigFinish* pMsg) {
  if (pMsg != NULL) {
    taosMemoryFree(pMsg);
  }
}

void syncReconfigFinishSerialize(const SyncReconfigFinish* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
2988
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
2989 2990 2991 2992 2993
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncReconfigFinishDeserialize(const char* buf, uint32_t len, SyncReconfigFinish* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
2994
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
2995 2996 2997 2998
}

char* syncReconfigFinishSerialize2(const SyncReconfigFinish* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
2999
  ASSERT(buf != NULL);
M
Minghao Li 已提交
3000 3001 3002 3003 3004 3005 3006 3007 3008 3009
  syncReconfigFinishSerialize(pMsg, buf, pMsg->bytes);
  if (len != NULL) {
    *len = pMsg->bytes;
  }
  return buf;
}

SyncReconfigFinish* syncReconfigFinishDeserialize2(const char* buf, uint32_t len) {
  uint32_t            bytes = *((uint32_t*)buf);
  SyncReconfigFinish* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
3010
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
3011
  syncReconfigFinishDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
3012
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029
  return pMsg;
}

void syncReconfigFinish2RpcMsg(const SyncReconfigFinish* pMsg, SRpcMsg* pRpcMsg) {
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
  pRpcMsg->msgType = pMsg->msgType;
  pRpcMsg->contLen = pMsg->bytes;
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
  syncReconfigFinishSerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen);
}

void syncReconfigFinishFromRpcMsg(const SRpcMsg* pRpcMsg, SyncReconfigFinish* pMsg) {
  syncReconfigFinishDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
}

SyncReconfigFinish* syncReconfigFinishFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncReconfigFinish* pMsg = syncReconfigFinishDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
3030
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047
  return pMsg;
}

cJSON* syncReconfigFinish2Json(const SyncReconfigFinish* 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* pOldCfg = syncCfg2Json((SSyncCfg*)(&(pMsg->oldCfg)));
    cJSON* pNewCfg = syncCfg2Json((SSyncCfg*)(&(pMsg->newCfg)));
    cJSON_AddItemToObject(pRoot, "oldCfg", pOldCfg);
    cJSON_AddItemToObject(pRoot, "newCfg", pNewCfg);

S
Shengliang Guan 已提交
3048
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->newCfgIndex);
M
Minghao Li 已提交
3049 3050
    cJSON_AddStringToObject(pRoot, "newCfgIndex", u64buf);

S
Shengliang Guan 已提交
3051
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->newCfgTerm);
M
Minghao Li 已提交
3052 3053
    cJSON_AddStringToObject(pRoot, "newCfgTerm", u64buf);

S
Shengliang Guan 已提交
3054
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->newCfgSeqNum);
M
Minghao Li 已提交
3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072
    cJSON_AddStringToObject(pRoot, "newCfgSeqNum", u64buf);
  }

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

char* syncReconfigFinish2Str(const SyncReconfigFinish* pMsg) {
  cJSON* pJson = syncReconfigFinish2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

// for debug ----------------------
void syncReconfigFinishPrint(const SyncReconfigFinish* pMsg) {
  char* serialized = syncReconfigFinish2Str(pMsg);
S
Shengliang Guan 已提交
3073
  printf("syncReconfigFinishPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
3074 3075 3076 3077 3078 3079
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncReconfigFinishPrint2(char* s, const SyncReconfigFinish* pMsg) {
  char* serialized = syncReconfigFinish2Str(pMsg);
S
Shengliang Guan 已提交
3080
  printf("syncReconfigFinishPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
3081 3082 3083 3084 3085 3086
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncReconfigFinishLog(const SyncReconfigFinish* pMsg) {
  char* serialized = syncReconfigFinish2Str(pMsg);
S
Shengliang Guan 已提交
3087
  sTrace("syncReconfigFinishLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
3088 3089 3090 3091 3092 3093
  taosMemoryFree(serialized);
}

void syncReconfigFinishLog2(char* s, const SyncReconfigFinish* pMsg) {
  if (gRaftDetailLog) {
    char* serialized = syncReconfigFinish2Str(pMsg);
S
Shengliang Guan 已提交
3094
    sTrace("syncReconfigFinishLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
3095 3096
    taosMemoryFree(serialized);
  }
3097
}