syncMessage.c 76.9 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"
M
Minghao Li 已提交
18
#include "syncUtil.h"
M
Minghao Li 已提交
19
#include "tcoding.h"
M
Minghao Li 已提交
20

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

M
Minghao Li 已提交
198
char* syncTimeoutSerialize2(const SyncTimeout* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
199
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
200
  ASSERT(buf != NULL);
M
Minghao Li 已提交
201 202 203 204 205 206 207 208 209
  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 已提交
210
  SyncTimeout* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
211
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
212
  syncTimeoutDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
213
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
214 215 216
  return pMsg;
}

M
Minghao Li 已提交
217 218 219 220 221 222 223 224 225 226 227 228
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 已提交
229 230
SyncTimeout* syncTimeoutFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncTimeout* pMsg = syncTimeoutDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
231
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
232 233 234
  return pMsg;
}

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

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
241
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
242 243 244 245 246 247 248 249
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);
    cJSON_AddNumberToObject(pRoot, "timeoutType", pMsg->timeoutType);
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->logicClock);
    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 已提交
250 251 252 253 254 255

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

M
Minghao Li 已提交
256 257 258 259 260 261 262 263 264 265
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);
266
  printf("syncTimeoutPrint | len:%zu | %s \n", strlen(serialized), serialized);
M
Minghao Li 已提交
267
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
268
  taosMemoryFree(serialized);
M
Minghao Li 已提交
269 270 271 272 273 274
}

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

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

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

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

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

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

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

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

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

M
Minghao Li 已提交
335
char* syncPingSerialize2(const SyncPing* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
336
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
337
  ASSERT(buf != NULL);
M
Minghao Li 已提交
338 339 340 341 342 343 344 345 346
  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 已提交
347
  SyncPing* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
348
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
349
  syncPingDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
350
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
351 352 353
  return pMsg;
}

M
Minghao Li 已提交
354
int32_t syncPingSerialize3(const SyncPing* pMsg, char* buf, int32_t bufLen) {
H
Hongze Cheng 已提交
355 356
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
M
Minghao Li 已提交
357 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
  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 已提交
391
  tEncoderClear(&encoder);
M
Minghao Li 已提交
392 393 394 395
  return tlen;
}

SyncPing* syncPingDeserialize3(void* buf, int32_t bufLen) {
H
Hongze Cheng 已提交
396 397
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
M
Minghao Li 已提交
398 399 400 401 402 403 404 405 406 407 408
  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 已提交
409
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
  pMsg->bytes = bytes;

  if (tDecodeI32(&decoder, &pMsg->vgId) < 0) {
    return NULL;
  }
  if (tDecodeU32(&decoder, &pMsg->msgType) < 0) {
    return NULL;
  }
  if (tDecodeU64(&decoder, &pMsg->srcId.addr) < 0) {
    return NULL;
  }
  if (tDecodeI32(&decoder, &pMsg->srcId.vgId) < 0) {
    return NULL;
  }
  if (tDecodeU64(&decoder, &pMsg->destId.addr) < 0) {
    return NULL;
  }
  if (tDecodeI32(&decoder, &pMsg->destId.vgId) < 0) {
    return NULL;
  }
  if (tDecodeU32(&decoder, &pMsg->dataLen) < 0) {
    return NULL;
  }
H
Hongze Cheng 已提交
433
  uint32_t len;
M
Minghao Li 已提交
434
  char*    data = NULL;
H
Hongze Cheng 已提交
435
  if (tDecodeBinary(&decoder, (uint8_t**)(&data), &len) < 0) {
M
Minghao Li 已提交
436 437
    return NULL;
  }
M
Minghao Li 已提交
438
  ASSERT(len = pMsg->dataLen);
M
Minghao Li 已提交
439 440 441
  memcpy(pMsg->data, data, len);

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
442
  tDecoderClear(&decoder);
M
Minghao Li 已提交
443 444 445
  return pMsg;
}

M
Minghao Li 已提交
446
void syncPing2RpcMsg(const SyncPing* pMsg, SRpcMsg* pRpcMsg) {
M
Minghao Li 已提交
447
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
M
Minghao Li 已提交
448 449
  pRpcMsg->msgType = pMsg->msgType;
  pRpcMsg->contLen = pMsg->bytes;
M
Minghao Li 已提交
450
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
M
Minghao Li 已提交
451
  syncPingSerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
452 453
}

M
Minghao Li 已提交
454 455
void syncPingFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPing* pMsg) {
  syncPingDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
M
Minghao Li 已提交
456 457
}

M
Minghao Li 已提交
458 459
SyncPing* syncPingFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncPing* pMsg = syncPingDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
460
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
461 462 463
  return pMsg;
}

M
Minghao Li 已提交
464
cJSON* syncPing2Json(const SyncPing* pMsg) {
465
  char   u64buf[128] = {0};
M
Minghao Li 已提交
466
  cJSON* pRoot = cJSON_CreateObject();
M
Minghao Li 已提交
467 468 469

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
470
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
471 472 473 474 475 476 477 478
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->srcId.addr);
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
479
      char     host[128] = {0};
M
Minghao Li 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493
      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), "%lu", pMsg->destId.addr);
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
494
      char     host[128] = {0};
M
Minghao Li 已提交
495 496 497 498 499 500 501 502 503 504 505 506
      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 已提交
507
    taosMemoryFree(s);
M
Minghao Li 已提交
508 509
    s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data2", s);
wafwerar's avatar
wafwerar 已提交
510
    taosMemoryFree(s);
M
Minghao Li 已提交
511
  }
M
Minghao Li 已提交
512

M
Minghao Li 已提交
513 514 515 516 517
  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncPing", pRoot);
  return pJson;
}

M
Minghao Li 已提交
518 519 520 521 522
char* syncPing2Str(const SyncPing* pMsg) {
  cJSON* pJson = syncPing2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
M
Minghao Li 已提交
523 524
}

M
Minghao Li 已提交
525 526 527 528 529
// for debug ----------------------
void syncPingPrint(const SyncPing* pMsg) {
  char* serialized = syncPing2Str(pMsg);
  printf("syncPingPrint | len:%lu | %s \n", strlen(serialized), serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
530
  taosMemoryFree(serialized);
M
Minghao Li 已提交
531 532 533 534 535 536
}

void syncPingPrint2(char* s, const SyncPing* pMsg) {
  char* serialized = syncPing2Str(pMsg);
  printf("syncPingPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
537
  taosMemoryFree(serialized);
M
Minghao Li 已提交
538 539 540 541 542
}

void syncPingLog(const SyncPing* pMsg) {
  char* serialized = syncPing2Str(pMsg);
  sTrace("syncPingLog | len:%lu | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
543
  taosMemoryFree(serialized);
M
Minghao Li 已提交
544 545 546
}

void syncPingLog2(char* s, const SyncPing* pMsg) {
M
Minghao Li 已提交
547 548 549 550 551
  if (gRaftDetailLog) {
    char* serialized = syncPing2Str(pMsg);
    sTrace("syncPingLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
552 553
}

M
Minghao Li 已提交
554 555
// ---- message process SyncPingReply----
SyncPingReply* syncPingReplyBuild(uint32_t dataLen) {
M
Minghao Li 已提交
556
  uint32_t       bytes = sizeof(SyncPingReply) + dataLen;
wafwerar's avatar
wafwerar 已提交
557
  SyncPingReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
558 559
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
560
  pMsg->msgType = TDMT_SYNC_PING_REPLY;
M
Minghao Li 已提交
561
  pMsg->dataLen = dataLen;
M
Minghao Li 已提交
562
  return pMsg;
M
Minghao Li 已提交
563 564
}

M
Minghao Li 已提交
565
SyncPingReply* syncPingReplyBuild2(const SRaftId* srcId, const SRaftId* destId, int32_t vgId, const char* str) {
M
Minghao Li 已提交
566 567
  uint32_t       dataLen = strlen(str) + 1;
  SyncPingReply* pMsg = syncPingReplyBuild(dataLen);
M
Minghao Li 已提交
568
  pMsg->vgId = vgId;
M
Minghao Li 已提交
569 570 571 572 573 574
  pMsg->srcId = *srcId;
  pMsg->destId = *destId;
  snprintf(pMsg->data, pMsg->dataLen, "%s", str);
  return pMsg;
}

M
Minghao Li 已提交
575 576
SyncPingReply* syncPingReplyBuild3(const SRaftId* srcId, const SRaftId* destId, int32_t vgId) {
  SyncPingReply* pMsg = syncPingReplyBuild2(srcId, destId, vgId, "pang");
M
Minghao Li 已提交
577 578 579
  return pMsg;
}

M
Minghao Li 已提交
580 581
void syncPingReplyDestroy(SyncPingReply* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
582
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
583 584 585 586
  }
}

void syncPingReplySerialize(const SyncPingReply* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
587
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
588 589 590 591 592
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncPingReplyDeserialize(const char* buf, uint32_t len, SyncPingReply* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
593 594
  ASSERT(len == pMsg->bytes);
  ASSERT(pMsg->bytes == sizeof(SyncPing) + pMsg->dataLen);
M
Minghao Li 已提交
595 596
}

M
Minghao Li 已提交
597
char* syncPingReplySerialize2(const SyncPingReply* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
598
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
599
  ASSERT(buf != NULL);
M
Minghao Li 已提交
600 601 602 603 604 605 606 607 608
  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 已提交
609
  SyncPingReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
610
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
611
  syncPingReplyDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
612
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
613 614 615
  return pMsg;
}

M
Minghao Li 已提交
616
int32_t syncPingReplySerialize3(const SyncPingReply* pMsg, char* buf, int32_t bufLen) {
H
Hongze Cheng 已提交
617 618
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
M
Minghao Li 已提交
619 620 621 622 623 624 625 626 627 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
  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 已提交
653
  tEncoderClear(&encoder);
M
Minghao Li 已提交
654 655 656 657
  return tlen;
}

SyncPingReply* syncPingReplyDeserialize3(void* buf, int32_t bufLen) {
H
Hongze Cheng 已提交
658 659
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
M
Minghao Li 已提交
660 661 662 663 664 665 666 667 668 669 670
  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 已提交
671
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
  pMsg->bytes = bytes;

  if (tDecodeI32(&decoder, &pMsg->vgId) < 0) {
    return NULL;
  }
  if (tDecodeU32(&decoder, &pMsg->msgType) < 0) {
    return NULL;
  }
  if (tDecodeU64(&decoder, &pMsg->srcId.addr) < 0) {
    return NULL;
  }
  if (tDecodeI32(&decoder, &pMsg->srcId.vgId) < 0) {
    return NULL;
  }
  if (tDecodeU64(&decoder, &pMsg->destId.addr) < 0) {
    return NULL;
  }
  if (tDecodeI32(&decoder, &pMsg->destId.vgId) < 0) {
    return NULL;
  }
  if (tDecodeU32(&decoder, &pMsg->dataLen) < 0) {
    return NULL;
  }
H
Hongze Cheng 已提交
695
  uint32_t len;
M
Minghao Li 已提交
696
  char*    data = NULL;
H
Hongze Cheng 已提交
697
  if (tDecodeBinary(&decoder, (uint8_t**)(&data), &len) < 0) {
M
Minghao Li 已提交
698 699
    return NULL;
  }
M
Minghao Li 已提交
700
  ASSERT(len = pMsg->dataLen);
M
Minghao Li 已提交
701 702 703
  memcpy(pMsg->data, data, len);

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
704
  tDecoderClear(&decoder);
M
Minghao Li 已提交
705 706 707
  return pMsg;
}

M
Minghao Li 已提交
708
void syncPingReply2RpcMsg(const SyncPingReply* pMsg, SRpcMsg* pRpcMsg) {
M
Minghao Li 已提交
709
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
M
Minghao Li 已提交
710 711 712 713 714 715 716 717 718 719
  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 已提交
720 721
SyncPingReply* syncPingReplyFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncPingReply* pMsg = syncPingReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
722
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
723 724 725
  return pMsg;
}

M
Minghao Li 已提交
726
cJSON* syncPingReply2Json(const SyncPingReply* pMsg) {
727
  char   u64buf[128] = {0};
M
Minghao Li 已提交
728
  cJSON* pRoot = cJSON_CreateObject();
M
Minghao Li 已提交
729 730 731

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
732
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
733 734 735 736 737 738 739 740
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->srcId.addr);
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
741
      char     host[128] = {0};
M
Minghao Li 已提交
742 743 744 745 746 747 748 749 750 751 752 753 754 755
      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), "%lu", pMsg->destId.addr);
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
756
      char     host[128] = {0};
M
Minghao Li 已提交
757 758 759 760 761 762 763 764 765 766 767 768
      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 已提交
769
    taosMemoryFree(s);
M
Minghao Li 已提交
770 771
    s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data2", s);
wafwerar's avatar
wafwerar 已提交
772
    taosMemoryFree(s);
M
Minghao Li 已提交
773
  }
M
Minghao Li 已提交
774 775 776 777

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

M
Minghao Li 已提交
780 781 782 783 784
char* syncPingReply2Str(const SyncPingReply* pMsg) {
  cJSON* pJson = syncPingReply2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
M
Minghao Li 已提交
785 786
}

M
Minghao Li 已提交
787 788 789
// for debug ----------------------
void syncPingReplyPrint(const SyncPingReply* pMsg) {
  char* serialized = syncPingReply2Str(pMsg);
790
  printf("syncPingReplyPrint | len:%zu | %s \n", strlen(serialized), serialized);
M
Minghao Li 已提交
791
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
792
  taosMemoryFree(serialized);
M
Minghao Li 已提交
793 794 795 796
}

void syncPingReplyPrint2(char* s, const SyncPingReply* pMsg) {
  char* serialized = syncPingReply2Str(pMsg);
797
  printf("syncPingReplyPrint2 | len:%zu | %s | %s \n", strlen(serialized), s, serialized);
M
Minghao Li 已提交
798
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
799
  taosMemoryFree(serialized);
M
Minghao Li 已提交
800 801 802 803
}

void syncPingReplyLog(const SyncPingReply* pMsg) {
  char* serialized = syncPingReply2Str(pMsg);
804
  sTrace("syncPingReplyLog | len:%zu | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
805
  taosMemoryFree(serialized);
M
Minghao Li 已提交
806 807 808
}

void syncPingReplyLog2(char* s, const SyncPingReply* pMsg) {
M
Minghao Li 已提交
809 810 811 812 813
  if (gRaftDetailLog) {
    char* serialized = syncPingReply2Str(pMsg);
    sTrace("syncPingReplyLog2 | len:%zu | %s | %s", strlen(serialized), s, serialized);
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
814 815
}

M
Minghao Li 已提交
816 817
// ---- message process SyncClientRequest----
SyncClientRequest* syncClientRequestBuild(uint32_t dataLen) {
M
Minghao Li 已提交
818
  uint32_t           bytes = sizeof(SyncClientRequest) + dataLen;
wafwerar's avatar
wafwerar 已提交
819
  SyncClientRequest* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
820 821
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
822
  pMsg->msgType = TDMT_SYNC_CLIENT_REQUEST;
M
Minghao Li 已提交
823 824 825 826 827 828
  pMsg->seqNum = 0;
  pMsg->isWeak = false;
  pMsg->dataLen = dataLen;
  return pMsg;
}

M
Minghao Li 已提交
829
// step 1. original SRpcMsg => SyncClientRequest, add seqNum, isWeak
M
Minghao Li 已提交
830
SyncClientRequest* syncClientRequestBuild2(const SRpcMsg* pOriginalRpcMsg, uint64_t seqNum, bool isWeak, int32_t vgId) {
M
Minghao Li 已提交
831
  SyncClientRequest* pMsg = syncClientRequestBuild(pOriginalRpcMsg->contLen);
M
Minghao Li 已提交
832
  pMsg->vgId = vgId;
M
Minghao Li 已提交
833 834 835 836 837 838 839
  pMsg->originalRpcType = pOriginalRpcMsg->msgType;
  pMsg->seqNum = seqNum;
  pMsg->isWeak = isWeak;
  memcpy(pMsg->data, pOriginalRpcMsg->pCont, pOriginalRpcMsg->contLen);
  return pMsg;
}

M
Minghao Li 已提交
840 841
void syncClientRequestDestroy(SyncClientRequest* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
842
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
843 844 845 846
  }
}

void syncClientRequestSerialize(const SyncClientRequest* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
847
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
848 849 850 851 852
  memcpy(buf, pMsg, pMsg->bytes);
}

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

M
Minghao Li 已提交
856
char* syncClientRequestSerialize2(const SyncClientRequest* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
857
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
858
  ASSERT(buf != NULL);
M
Minghao Li 已提交
859 860 861 862 863 864 865 866 867
  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 已提交
868
  SyncClientRequest* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
869
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
870
  syncClientRequestDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
871
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
872 873 874
  return pMsg;
}

M
Minghao Li 已提交
875
// step 2. SyncClientRequest => RpcMsg, to queue
M
Minghao Li 已提交
876 877 878 879 880 881 882 883 884 885 886 887
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 已提交
888
// step 3. RpcMsg => SyncClientRequest, from queue
M
Minghao Li 已提交
889 890
SyncClientRequest* syncClientRequestFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncClientRequest* pMsg = syncClientRequestDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
891
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
892 893 894
  return pMsg;
}

M
Minghao Li 已提交
895
cJSON* syncClientRequest2Json(const SyncClientRequest* pMsg) {
896
  char   u64buf[128] = {0};
M
Minghao Li 已提交
897
  cJSON* pRoot = cJSON_CreateObject();
M
Minghao Li 已提交
898 899 900

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
901
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
902 903 904 905 906 907 908 909 910 911
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);
    cJSON_AddNumberToObject(pRoot, "originalRpcType", pMsg->originalRpcType);
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->seqNum);
    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 已提交
912
    taosMemoryFree(s);
M
Minghao Li 已提交
913 914
    s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data2", s);
wafwerar's avatar
wafwerar 已提交
915
    taosMemoryFree(s);
M
Minghao Li 已提交
916
  }
M
Minghao Li 已提交
917

M
Minghao Li 已提交
918 919 920 921 922
  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncClientRequest", pRoot);
  return pJson;
}

M
Minghao Li 已提交
923 924 925 926 927 928 929 930 931 932 933 934
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);
  printf("syncClientRequestPrint | len:%lu | %s \n", strlen(serialized), serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
935
  taosMemoryFree(serialized);
M
Minghao Li 已提交
936 937 938 939 940 941
}

void syncClientRequestPrint2(char* s, const SyncClientRequest* pMsg) {
  char* serialized = syncClientRequest2Str(pMsg);
  printf("syncClientRequestPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
942
  taosMemoryFree(serialized);
M
Minghao Li 已提交
943 944 945 946 947
}

void syncClientRequestLog(const SyncClientRequest* pMsg) {
  char* serialized = syncClientRequest2Str(pMsg);
  sTrace("syncClientRequestLog | len:%lu | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
948
  taosMemoryFree(serialized);
M
Minghao Li 已提交
949 950 951
}

void syncClientRequestLog2(char* s, const SyncClientRequest* pMsg) {
M
Minghao Li 已提交
952 953 954 955 956
  if (gRaftDetailLog) {
    char* serialized = syncClientRequest2Str(pMsg);
    sTrace("syncClientRequestLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
957 958
}

M
Minghao Li 已提交
959
// ---- message process SyncRequestVote----
M
Minghao Li 已提交
960
SyncRequestVote* syncRequestVoteBuild(int32_t vgId) {
M
Minghao Li 已提交
961
  uint32_t         bytes = sizeof(SyncRequestVote);
wafwerar's avatar
wafwerar 已提交
962
  SyncRequestVote* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
963 964
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
M
Minghao Li 已提交
965
  pMsg->vgId = vgId;
966
  pMsg->msgType = TDMT_SYNC_REQUEST_VOTE;
M
Minghao Li 已提交
967
  return pMsg;
M
Minghao Li 已提交
968 969
}

M
Minghao Li 已提交
970 971
void syncRequestVoteDestroy(SyncRequestVote* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
972
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
973 974
  }
}
M
Minghao Li 已提交
975

M
Minghao Li 已提交
976
void syncRequestVoteSerialize(const SyncRequestVote* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
977
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
978 979
  memcpy(buf, pMsg, pMsg->bytes);
}
M
Minghao Li 已提交
980

M
Minghao Li 已提交
981 982
void syncRequestVoteDeserialize(const char* buf, uint32_t len, SyncRequestVote* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
983
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
984
}
M
Minghao Li 已提交
985

M
Minghao Li 已提交
986
char* syncRequestVoteSerialize2(const SyncRequestVote* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
987
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
988
  ASSERT(buf != NULL);
M
Minghao Li 已提交
989 990 991 992 993 994 995 996 997
  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 已提交
998
  SyncRequestVote* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
999
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1000
  syncRequestVoteDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
1001
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1002 1003 1004
  return pMsg;
}

M
Minghao Li 已提交
1005 1006 1007 1008 1009 1010 1011
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 已提交
1012

M
Minghao Li 已提交
1013 1014 1015
void syncRequestVoteFromRpcMsg(const SRpcMsg* pRpcMsg, SyncRequestVote* pMsg) {
  syncRequestVoteDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
}
M
Minghao Li 已提交
1016

M
Minghao Li 已提交
1017 1018
SyncRequestVote* syncRequestVoteFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncRequestVote* pMsg = syncRequestVoteDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
1019
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1020
  return pMsg;
M
Minghao Li 已提交
1021 1022
}

M
Minghao Li 已提交
1023
cJSON* syncRequestVote2Json(const SyncRequestVote* pMsg) {
1024
  char   u64buf[128] = {0};
M
Minghao Li 已提交
1025 1026
  cJSON* pRoot = cJSON_CreateObject();

M
Minghao Li 已提交
1027 1028
  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
1029
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
1030 1031 1032 1033 1034 1035 1036 1037
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->srcId.addr);
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
1038
      char     host[128] = {0};
M
Minghao Li 已提交
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
      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;
1052
      char     host[128] = {0};
M
Minghao Li 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
      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), "%lu", pMsg->term);
    cJSON_AddStringToObject(pRoot, "term", u64buf);
M
Minghao Li 已提交
1063
    snprintf(u64buf, sizeof(u64buf), "%ld", pMsg->lastLogIndex);
M
Minghao Li 已提交
1064 1065 1066 1067
    cJSON_AddStringToObject(pRoot, "lastLogIndex", u64buf);
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->lastLogTerm);
    cJSON_AddStringToObject(pRoot, "lastLogTerm", u64buf);
  }
M
Minghao Li 已提交
1068 1069 1070 1071

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

M
Minghao Li 已提交
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
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);
  printf("syncRequestVotePrint | len:%lu | %s \n", strlen(serialized), serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1086
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1087 1088 1089 1090 1091 1092
}

void syncRequestVotePrint2(char* s, const SyncRequestVote* pMsg) {
  char* serialized = syncRequestVote2Str(pMsg);
  printf("syncRequestVotePrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1093
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1094 1095 1096 1097 1098
}

void syncRequestVoteLog(const SyncRequestVote* pMsg) {
  char* serialized = syncRequestVote2Str(pMsg);
  sTrace("syncRequestVoteLog | len:%lu | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
1099
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1100 1101 1102
}

void syncRequestVoteLog2(char* s, const SyncRequestVote* pMsg) {
M
Minghao Li 已提交
1103 1104 1105 1106 1107
  if (gRaftDetailLog) {
    char* serialized = syncRequestVote2Str(pMsg);
    sTrace("syncRequestVoteLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
1108 1109
}

M
Minghao Li 已提交
1110
// ---- message process SyncRequestVoteReply----
M
Minghao Li 已提交
1111
SyncRequestVoteReply* syncRequestVoteReplyBuild(int32_t vgId) {
M
Minghao Li 已提交
1112
  uint32_t              bytes = sizeof(SyncRequestVoteReply);
wafwerar's avatar
wafwerar 已提交
1113
  SyncRequestVoteReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1114 1115
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
M
Minghao Li 已提交
1116
  pMsg->vgId = vgId;
1117
  pMsg->msgType = TDMT_SYNC_REQUEST_VOTE_REPLY;
M
Minghao Li 已提交
1118
  return pMsg;
M
Minghao Li 已提交
1119 1120 1121 1122
}

void syncRequestVoteReplyDestroy(SyncRequestVoteReply* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
1123
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
1124 1125 1126 1127
  }
}

void syncRequestVoteReplySerialize(const SyncRequestVoteReply* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
1128
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
1129 1130 1131 1132 1133
  memcpy(buf, pMsg, pMsg->bytes);
}

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

M
Minghao Li 已提交
1137
char* syncRequestVoteReplySerialize2(const SyncRequestVoteReply* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
1138
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
1139
  ASSERT(buf != NULL);
M
Minghao Li 已提交
1140 1141 1142 1143 1144 1145 1146 1147 1148
  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 已提交
1149
  SyncRequestVoteReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1150
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1151
  syncRequestVoteReplyDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
1152
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1153 1154 1155
  return pMsg;
}

M
Minghao Li 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
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 已提交
1168 1169
SyncRequestVoteReply* syncRequestVoteReplyFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncRequestVoteReply* pMsg = syncRequestVoteReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
1170
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1171 1172 1173
  return pMsg;
}

M
Minghao Li 已提交
1174
cJSON* syncRequestVoteReply2Json(const SyncRequestVoteReply* pMsg) {
1175
  char   u64buf[128] = {0};
M
Minghao Li 已提交
1176 1177
  cJSON* pRoot = cJSON_CreateObject();

M
Minghao Li 已提交
1178 1179
  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
1180
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
1181 1182 1183 1184 1185 1186 1187 1188
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->srcId.addr);
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
1189
      char     host[128] = {0};
M
Minghao Li 已提交
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
      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;
1203
      char     host[128] = {0};
M
Minghao Li 已提交
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
      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), "%lu", pMsg->term);
    cJSON_AddStringToObject(pRoot, "term", u64buf);
    cJSON_AddNumberToObject(pRoot, "vote_granted", pMsg->voteGranted);
  }
M
Minghao Li 已提交
1216 1217 1218 1219

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

M
Minghao Li 已提交
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
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);
  printf("syncRequestVoteReplyPrint | len:%lu | %s \n", strlen(serialized), serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1234
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1235 1236 1237 1238 1239 1240
}

void syncRequestVoteReplyPrint2(char* s, const SyncRequestVoteReply* pMsg) {
  char* serialized = syncRequestVoteReply2Str(pMsg);
  printf("syncRequestVoteReplyPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1241
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1242 1243 1244 1245 1246
}

void syncRequestVoteReplyLog(const SyncRequestVoteReply* pMsg) {
  char* serialized = syncRequestVoteReply2Str(pMsg);
  sTrace("syncRequestVoteReplyLog | len:%lu | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
1247
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1248 1249 1250
}

void syncRequestVoteReplyLog2(char* s, const SyncRequestVoteReply* pMsg) {
M
Minghao Li 已提交
1251 1252 1253 1254 1255
  if (gRaftDetailLog) {
    char* serialized = syncRequestVoteReply2Str(pMsg);
    sTrace("syncRequestVoteReplyLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
1256 1257
}

M
Minghao Li 已提交
1258
// ---- message process SyncAppendEntries----
M
Minghao Li 已提交
1259
SyncAppendEntries* syncAppendEntriesBuild(uint32_t dataLen, int32_t vgId) {
M
Minghao Li 已提交
1260
  uint32_t           bytes = sizeof(SyncAppendEntries) + dataLen;
wafwerar's avatar
wafwerar 已提交
1261
  SyncAppendEntries* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1262 1263
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
M
Minghao Li 已提交
1264
  pMsg->vgId = vgId;
1265
  pMsg->msgType = TDMT_SYNC_APPEND_ENTRIES;
M
Minghao Li 已提交
1266
  pMsg->dataLen = dataLen;
M
Minghao Li 已提交
1267
  return pMsg;
M
Minghao Li 已提交
1268 1269 1270 1271
}

void syncAppendEntriesDestroy(SyncAppendEntries* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
1272
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
1273 1274 1275 1276
  }
}

void syncAppendEntriesSerialize(const SyncAppendEntries* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
1277
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
1278 1279 1280 1281 1282
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncAppendEntriesDeserialize(const char* buf, uint32_t len, SyncAppendEntries* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
1283 1284
  ASSERT(len == pMsg->bytes);
  ASSERT(pMsg->bytes == sizeof(SyncAppendEntries) + pMsg->dataLen);
M
Minghao Li 已提交
1285 1286 1287
}

char* syncAppendEntriesSerialize2(const SyncAppendEntries* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
1288
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
1289
  ASSERT(buf != NULL);
M
Minghao Li 已提交
1290 1291 1292 1293 1294 1295 1296 1297 1298
  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 已提交
1299
  SyncAppendEntries* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1300
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1301
  syncAppendEntriesDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
1302
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1303
  return pMsg;
M
Minghao Li 已提交
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
}

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 已提交
1318 1319
SyncAppendEntries* syncAppendEntriesFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncAppendEntries* pMsg = syncAppendEntriesDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
1320
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1321 1322 1323
  return pMsg;
}

M
Minghao Li 已提交
1324
cJSON* syncAppendEntries2Json(const SyncAppendEntries* pMsg) {
1325
  char   u64buf[128] = {0};
M
Minghao Li 已提交
1326 1327
  cJSON* pRoot = cJSON_CreateObject();

M
Minghao Li 已提交
1328 1329
  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
1330
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
1331 1332 1333 1334 1335 1336 1337 1338
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->srcId.addr);
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
1339
      char     host[128] = {0};
M
Minghao Li 已提交
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
      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), "%lu", pMsg->destId.addr);
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
1354
      char     host[128] = {0};
M
Minghao Li 已提交
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
      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), "%lu", pMsg->term);
    cJSON_AddStringToObject(pRoot, "term", u64buf);

1366 1367 1368
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->privateTerm);
    cJSON_AddStringToObject(pRoot, "privateTerm", u64buf);

M
Minghao Li 已提交
1369 1370
    snprintf(u64buf, sizeof(u64buf), "%ld", pMsg->prevLogIndex);
    cJSON_AddStringToObject(pRoot, "prevLogIndex", u64buf);
M
Minghao Li 已提交
1371 1372 1373 1374

    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->prevLogTerm);
    cJSON_AddStringToObject(pRoot, "pre_log_term", u64buf);

M
Minghao Li 已提交
1375 1376
    snprintf(u64buf, sizeof(u64buf), "%ld", pMsg->commitIndex);
    cJSON_AddStringToObject(pRoot, "commitIndex", u64buf);
M
Minghao Li 已提交
1377 1378 1379 1380 1381

    cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen);
    char* s;
    s = syncUtilprintBin((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data", s);
wafwerar's avatar
wafwerar 已提交
1382
    taosMemoryFree(s);
M
Minghao Li 已提交
1383 1384
    s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data2", s);
wafwerar's avatar
wafwerar 已提交
1385
    taosMemoryFree(s);
M
Minghao Li 已提交
1386
  }
M
Minghao Li 已提交
1387 1388 1389 1390 1391 1392

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

M
Minghao Li 已提交
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
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);
  printf("syncAppendEntriesPrint | len:%lu | %s \n", strlen(serialized), serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1405
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1406 1407 1408 1409 1410 1411
}

void syncAppendEntriesPrint2(char* s, const SyncAppendEntries* pMsg) {
  char* serialized = syncAppendEntries2Str(pMsg);
  printf("syncAppendEntriesPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1412
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1413 1414 1415 1416 1417
}

void syncAppendEntriesLog(const SyncAppendEntries* pMsg) {
  char* serialized = syncAppendEntries2Str(pMsg);
  sTrace("syncAppendEntriesLog | len:%lu | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
1418
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1419 1420 1421
}

void syncAppendEntriesLog2(char* s, const SyncAppendEntries* pMsg) {
M
Minghao Li 已提交
1422 1423 1424 1425 1426
  if (gRaftDetailLog) {
    char* serialized = syncAppendEntries2Str(pMsg);
    sTrace("syncAppendEntriesLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
1427 1428
}

M
Minghao Li 已提交
1429
// ---- message process SyncAppendEntriesReply----
M
Minghao Li 已提交
1430
SyncAppendEntriesReply* syncAppendEntriesReplyBuild(int32_t vgId) {
M
Minghao Li 已提交
1431
  uint32_t                bytes = sizeof(SyncAppendEntriesReply);
wafwerar's avatar
wafwerar 已提交
1432
  SyncAppendEntriesReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1433 1434
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
M
Minghao Li 已提交
1435
  pMsg->vgId = vgId;
1436
  pMsg->msgType = TDMT_SYNC_APPEND_ENTRIES_REPLY;
M
Minghao Li 已提交
1437
  return pMsg;
M
Minghao Li 已提交
1438 1439 1440 1441
}

void syncAppendEntriesReplyDestroy(SyncAppendEntriesReply* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
1442
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
1443 1444 1445 1446
  }
}

void syncAppendEntriesReplySerialize(const SyncAppendEntriesReply* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
1447
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
1448 1449 1450 1451 1452
  memcpy(buf, pMsg, pMsg->bytes);
}

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

M
Minghao Li 已提交
1456
char* syncAppendEntriesReplySerialize2(const SyncAppendEntriesReply* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
1457
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
1458
  ASSERT(buf != NULL);
M
Minghao Li 已提交
1459 1460 1461 1462 1463 1464 1465 1466 1467
  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 已提交
1468
  SyncAppendEntriesReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1469
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1470
  syncAppendEntriesReplyDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
1471
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1472 1473 1474
  return pMsg;
}

M
Minghao Li 已提交
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
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 已提交
1487 1488
SyncAppendEntriesReply* syncAppendEntriesReplyFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncAppendEntriesReply* pMsg = syncAppendEntriesReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
1489
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1490 1491 1492
  return pMsg;
}

M
Minghao Li 已提交
1493
cJSON* syncAppendEntriesReply2Json(const SyncAppendEntriesReply* pMsg) {
1494
  char   u64buf[128] = {0};
M
Minghao Li 已提交
1495
  cJSON* pRoot = cJSON_CreateObject();
M
Minghao Li 已提交
1496 1497 1498

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
1499
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
1500 1501 1502 1503 1504 1505 1506 1507
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->srcId.addr);
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
1508
      char     host[128] = {0};
M
Minghao Li 已提交
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
      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), "%lu", pMsg->destId.addr);
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
1523
      char     host[128] = {0};
M
Minghao Li 已提交
1524 1525 1526 1527 1528 1529 1530 1531
      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);

1532 1533 1534
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->privateTerm);
    cJSON_AddStringToObject(pRoot, "privateTerm", u64buf);

M
Minghao Li 已提交
1535 1536 1537
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->term);
    cJSON_AddStringToObject(pRoot, "term", u64buf);
    cJSON_AddNumberToObject(pRoot, "success", pMsg->success);
M
Minghao Li 已提交
1538
    snprintf(u64buf, sizeof(u64buf), "%ld", pMsg->matchIndex);
M
Minghao Li 已提交
1539
    cJSON_AddStringToObject(pRoot, "matchIndex", u64buf);
M
Minghao Li 已提交
1540 1541 1542 1543 1544
  }

  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncAppendEntriesReply", pRoot);
  return pJson;
M
Minghao Li 已提交
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
}

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);
  printf("syncAppendEntriesReplyPrint | len:%lu | %s \n", strlen(serialized), serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1559
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1560 1561 1562 1563 1564 1565
}

void syncAppendEntriesReplyPrint2(char* s, const SyncAppendEntriesReply* pMsg) {
  char* serialized = syncAppendEntriesReply2Str(pMsg);
  printf("syncAppendEntriesReplyPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1566
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1567 1568 1569 1570 1571
}

void syncAppendEntriesReplyLog(const SyncAppendEntriesReply* pMsg) {
  char* serialized = syncAppendEntriesReply2Str(pMsg);
  sTrace("syncAppendEntriesReplyLog | len:%lu | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
1572
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1573 1574 1575
}

void syncAppendEntriesReplyLog2(char* s, const SyncAppendEntriesReply* pMsg) {
M
Minghao Li 已提交
1576 1577 1578 1579 1580
  if (gRaftDetailLog) {
    char* serialized = syncAppendEntriesReply2Str(pMsg);
    sTrace("syncAppendEntriesReplyLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
1581
}
M
Minghao Li 已提交
1582 1583

// ---- message process SyncApplyMsg----
M
Minghao Li 已提交
1584 1585 1586 1587 1588
SyncApplyMsg* syncApplyMsgBuild(uint32_t dataLen) {
  uint32_t      bytes = sizeof(SyncApplyMsg) + dataLen;
  SyncApplyMsg* pMsg = taosMemoryMalloc(bytes);
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
1589
  pMsg->msgType = TDMT_SYNC_APPLY_MSG;
M
Minghao Li 已提交
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
  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 已提交
1610
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
1611 1612 1613 1614 1615
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncApplyMsgDeserialize(const char* buf, uint32_t len, SyncApplyMsg* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
1616
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1617 1618 1619 1620
}

char* syncApplyMsgSerialize2(const SyncApplyMsg* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
1621
  ASSERT(buf != NULL);
M
Minghao Li 已提交
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
  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 已提交
1632
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1633
  syncApplyMsgDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
1634
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1635 1636
  return pMsg;
}
M
Minghao Li 已提交
1637 1638

// SyncApplyMsg to SRpcMsg, put it into ApplyQ
M
Minghao Li 已提交
1639 1640 1641 1642 1643 1644 1645
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 已提交
1646 1647

// get SRpcMsg from ApplyQ, to SyncApplyMsg
M
Minghao Li 已提交
1648 1649 1650 1651 1652 1653 1654 1655
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 已提交
1656 1657

// SyncApplyMsg to OriginalRpcMsg
M
Minghao Li 已提交
1658 1659 1660 1661 1662 1663 1664 1665 1666
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) {
1667
  char   u64buf[128] = {0};
M
Minghao Li 已提交
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
  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);

    snprintf(u64buf, sizeof(u64buf), "%ld", pMsg->fsmMeta.index);
    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));
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->fsmMeta.seqNum);
    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 已提交
1694

M
Minghao Li 已提交
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705
  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 已提交
1706 1707

// for debug ----------------------
M
Minghao Li 已提交
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
void syncApplyMsgPrint(const SyncApplyMsg* pMsg) {
  char* serialized = syncApplyMsg2Str(pMsg);
  printf("syncApplyMsgPrint | len:%lu | %s \n", strlen(serialized), serialized);
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncApplyMsgPrint2(char* s, const SyncApplyMsg* pMsg) {
  char* serialized = syncApplyMsg2Str(pMsg);
  printf("syncApplyMsgPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncApplyMsgLog(const SyncApplyMsg* pMsg) {
  char* serialized = syncApplyMsg2Str(pMsg);
  sTrace("ssyncApplyMsgLog | len:%lu | %s", strlen(serialized), serialized);
  taosMemoryFree(serialized);
}

void syncApplyMsgLog2(char* s, const SyncApplyMsg* pMsg) {
M
Minghao Li 已提交
1729 1730 1731 1732 1733
  if (gRaftDetailLog) {
    char* serialized = syncApplyMsg2Str(pMsg);
    sTrace("syncApplyMsgLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
1734
}
1735 1736 1737 1738 1739 1740 1741 1742

// ---------------------------------------------
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;
1743
  pMsg->msgType = TDMT_SYNC_SNAPSHOT_SEND;
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
  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 已提交
1755
  ASSERT(pMsg->bytes <= bufLen);
1756 1757 1758 1759 1760
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncSnapshotSendDeserialize(const char* buf, uint32_t len, SyncSnapshotSend* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
1761 1762
  ASSERT(len == pMsg->bytes);
  ASSERT(pMsg->bytes == sizeof(SyncSnapshotSend) + pMsg->dataLen);
1763 1764 1765 1766
}

char* syncSnapshotSendSerialize2(const SyncSnapshotSend* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
1767
  ASSERT(buf != NULL);
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
  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 已提交
1778
  ASSERT(pMsg != NULL);
1779
  syncSnapshotSendDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
1780
  ASSERT(len == pMsg->bytes);
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797
  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 已提交
1798
  ASSERT(pMsg != NULL);
1799 1800 1801 1802 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 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
  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();
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->srcId.addr);
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
      char     host[128];
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

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

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

1844 1845 1846
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->privateTerm);
    cJSON_AddStringToObject(pRoot, "privateTerm", u64buf);

1847 1848 1849
    snprintf(u64buf, sizeof(u64buf), "%ld", pMsg->lastIndex);
    cJSON_AddStringToObject(pRoot, "lastIndex", u64buf);

1850 1851 1852 1853
    snprintf(u64buf, sizeof(u64buf), "%ld", pMsg->lastConfigIndex);
    cJSON_AddStringToObject(pRoot, "lastConfigIndex", u64buf);
    cJSON_AddItemToObject(pRoot, "lastConfig", syncCfg2Json((SSyncCfg*)&(pMsg->lastConfig)));

1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->lastTerm);
    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);
  printf("syncSnapshotSendPrint | len:%lu | %s \n", strlen(serialized), serialized);
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncSnapshotSendPrint2(char* s, const SyncSnapshotSend* pMsg) {
  char* serialized = syncSnapshotSend2Str(pMsg);
  printf("syncSnapshotSendPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncSnapshotSendLog(const SyncSnapshotSend* pMsg) {
  char* serialized = syncSnapshotSend2Str(pMsg);
  sTrace("syncSnapshotSendLog | len:%lu | %s", strlen(serialized), serialized);
  taosMemoryFree(serialized);
}

void syncSnapshotSendLog2(char* s, const SyncSnapshotSend* pMsg) {
M
Minghao Li 已提交
1903 1904 1905 1906 1907
  if (gRaftDetailLog) {
    char* serialized = syncSnapshotSend2Str(pMsg);
    sTrace("syncSnapshotSendLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
    taosMemoryFree(serialized);
  }
1908 1909 1910
}

// ---------------------------------------------
M
Minghao Li 已提交
1911
SyncSnapshotRsp* syncSnapshotRspBuild(int32_t vgId) {
1912 1913 1914 1915 1916
  uint32_t         bytes = sizeof(SyncSnapshotRsp);
  SyncSnapshotRsp* pMsg = taosMemoryMalloc(bytes);
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
  pMsg->vgId = vgId;
1917
  pMsg->msgType = TDMT_SYNC_SNAPSHOT_RSP;
1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
  return pMsg;
}

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

void syncSnapshotRspSerialize(const SyncSnapshotRsp* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
1928
  ASSERT(pMsg->bytes <= bufLen);
1929 1930 1931 1932 1933
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncSnapshotRspDeserialize(const char* buf, uint32_t len, SyncSnapshotRsp* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
1934
  ASSERT(len == pMsg->bytes);
1935 1936 1937 1938
}

char* syncSnapshotRspSerialize2(const SyncSnapshotRsp* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
1939
  ASSERT(buf != NULL);
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949
  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 已提交
1950
  ASSERT(pMsg != NULL);
1951
  syncSnapshotRspDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
1952
  ASSERT(len == pMsg->bytes);
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
  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 已提交
1970
  ASSERT(pMsg != NULL);
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
  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();
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->srcId.addr);
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
      char     host[128];
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

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

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

2016 2017 2018
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->privateTerm);
    cJSON_AddStringToObject(pRoot, "privateTerm", u64buf);

2019 2020 2021 2022 2023 2024 2025
    snprintf(u64buf, sizeof(u64buf), "%ld", pMsg->lastIndex);
    cJSON_AddStringToObject(pRoot, "lastIndex", u64buf);

    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->lastTerm);
    cJSON_AddStringToObject(pRoot, "lastTerm", u64buf);

    cJSON_AddNumberToObject(pRoot, "ack", pMsg->ack);
2026
    cJSON_AddNumberToObject(pRoot, "code", pMsg->code);
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
  }

  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);
  printf("syncSnapshotRspPrint | len:%lu | %s \n", strlen(serialized), serialized);
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncSnapshotRspPrint2(char* s, const SyncSnapshotRsp* pMsg) {
  char* serialized = syncSnapshotRsp2Str(pMsg);
  printf("syncSnapshotRspPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncSnapshotRspLog(const SyncSnapshotRsp* pMsg) {
  char* serialized = syncSnapshotRsp2Str(pMsg);
  sTrace("syncSnapshotRspLog | len:%lu | %s", strlen(serialized), serialized);
  taosMemoryFree(serialized);
}

void syncSnapshotRspLog2(char* s, const SyncSnapshotRsp* pMsg) {
M
Minghao Li 已提交
2063 2064 2065 2066 2067
  if (gRaftDetailLog) {
    char* serialized = syncSnapshotRsp2Str(pMsg);
    sTrace("syncSnapshotRspLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
    taosMemoryFree(serialized);
  }
2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087
}

// ---------------------------------------------
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 已提交
2088
  ASSERT(pMsg->bytes <= bufLen);
2089 2090 2091 2092 2093
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncLeaderTransferDeserialize(const char* buf, uint32_t len, SyncLeaderTransfer* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
2094
  ASSERT(len == pMsg->bytes);
2095 2096 2097 2098
}

char* syncLeaderTransferSerialize2(const SyncLeaderTransfer* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
2099
  ASSERT(buf != NULL);
2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
  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 已提交
2110
  ASSERT(pMsg != NULL);
2111
  syncLeaderTransferDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
2112
  ASSERT(len == pMsg->bytes);
2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129
  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 已提交
2130
  ASSERT(pMsg != NULL);
2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229
  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();
        snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->srcId.addr);
        cJSON_AddStringToObject(pSrcId, "addr", u64buf);
        {
          uint64_t u64 = pMsg->srcId.addr;
          cJSON*   pTmp = pSrcId;
          char     host[128];
          uint16_t port;
          syncUtilU642Addr(u64, host, sizeof(host), &port);
          cJSON_AddStringToObject(pTmp, "addr_host", host);
          cJSON_AddNumberToObject(pTmp, "addr_port", port);
        }
        cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
        cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

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

    cJSON* pNewerId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->newLeaderId.addr);
    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);
  printf("syncLeaderTransferPrint | len:%lu | %s \n", strlen(serialized), serialized);
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncLeaderTransferPrint2(char* s, const SyncLeaderTransfer* pMsg) {
  char* serialized = syncLeaderTransfer2Str(pMsg);
  printf("syncLeaderTransferPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncLeaderTransferLog(const SyncLeaderTransfer* pMsg) {
  char* serialized = syncLeaderTransfer2Str(pMsg);
  sTrace("syncLeaderTransferLog | len:%lu | %s", strlen(serialized), serialized);
  taosMemoryFree(serialized);
}

void syncLeaderTransferLog2(char* s, const SyncLeaderTransfer* pMsg) {
  if (gRaftDetailLog) {
    char* serialized = syncLeaderTransfer2Str(pMsg);
    sTrace("syncLeaderTransferLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249
}

// ---------------------------------------------
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 已提交
2250
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
2251 2252 2253 2254 2255
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncReconfigFinishDeserialize(const char* buf, uint32_t len, SyncReconfigFinish* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
2256
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
2257 2258 2259 2260
}

char* syncReconfigFinishSerialize2(const SyncReconfigFinish* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
2261
  ASSERT(buf != NULL);
M
Minghao Li 已提交
2262 2263 2264 2265 2266 2267 2268 2269 2270 2271
  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 已提交
2272
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
2273
  syncReconfigFinishDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
2274
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291
  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 已提交
2292
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358
  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);

    snprintf(u64buf, sizeof(u64buf), "%ld", pMsg->newCfgIndex);
    cJSON_AddStringToObject(pRoot, "newCfgIndex", u64buf);

    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->newCfgTerm);
    cJSON_AddStringToObject(pRoot, "newCfgTerm", u64buf);

    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->newCfgSeqNum);
    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);
  printf("syncReconfigFinishPrint | len:%lu | %s \n", strlen(serialized), serialized);
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncReconfigFinishPrint2(char* s, const SyncReconfigFinish* pMsg) {
  char* serialized = syncReconfigFinish2Str(pMsg);
  printf("syncReconfigFinishPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncReconfigFinishLog(const SyncReconfigFinish* pMsg) {
  char* serialized = syncReconfigFinish2Str(pMsg);
  sTrace("syncReconfigFinishLog | len:%lu | %s", strlen(serialized), serialized);
  taosMemoryFree(serialized);
}

void syncReconfigFinishLog2(char* s, const SyncReconfigFinish* pMsg) {
  if (gRaftDetailLog) {
    char* serialized = syncReconfigFinish2Str(pMsg);
    sTrace("syncReconfigFinishLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
    taosMemoryFree(serialized);
  }
2359
}