syncMessage.c 102.2 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
410
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
  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 已提交
434
  uint32_t len;
M
Minghao Li 已提交
435
  char*    data = NULL;
H
Hongze Cheng 已提交
436
  if (tDecodeBinary(&decoder, (uint8_t**)(&data), &len) < 0) {
M
Minghao Li 已提交
437 438
    return NULL;
  }
M
Minghao Li 已提交
439
  ASSERT(len = pMsg->dataLen);
M
Minghao Li 已提交
440 441 442
  memcpy(pMsg->data, data, len);

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

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

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

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

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

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

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
475
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
M
Minghao Li 已提交
476 477 478 479
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
480
      char     host[128] = {0};
M
Minghao Li 已提交
481 482 483 484 485 486 487 488 489
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

SyncPingReply* syncPingReplyDeserialize3(void* buf, int32_t bufLen) {
H
Hongze Cheng 已提交
659 660
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
M
Minghao Li 已提交
661 662 663 664 665 666 667 668 669 670 671
  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 已提交
672
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
  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 已提交
696
  uint32_t len;
M
Minghao Li 已提交
697
  char*    data = NULL;
H
Hongze Cheng 已提交
698
  if (tDecodeBinary(&decoder, (uint8_t**)(&data), &len) < 0) {
M
Minghao Li 已提交
699 700
    return NULL;
  }
M
Minghao Li 已提交
701
  ASSERT(len = pMsg->dataLen);
M
Minghao Li 已提交
702 703 704
  memcpy(pMsg->data, data, len);

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

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

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

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

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
737
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
M
Minghao Li 已提交
738 739 740 741
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
742
      char     host[128] = {0};
M
Minghao Li 已提交
743 744 745 746 747 748 749 750 751
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

M
Minghao Li 已提交
924 925 926 927 928 929 930 931 932 933
char* syncClientRequest2Str(const SyncClientRequest* pMsg) {
  cJSON* pJson = syncClientRequest2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

// for debug ----------------------
void syncClientRequestPrint(const SyncClientRequest* pMsg) {
  char* serialized = syncClientRequest2Str(pMsg);
S
Shengliang Guan 已提交
934
  printf("syncClientRequestPrint | len:%" PRIu64 " | %s \n", strlen(serialized), serialized);
M
Minghao Li 已提交
935
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
936
  taosMemoryFree(serialized);
M
Minghao Li 已提交
937 938 939 940
}

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

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

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

960 961 962 963 964 965
// ---- message process SyncClientRequestBatch----

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

966
SyncClientRequestBatch* syncClientRequestBatchBuild(SRpcMsg** rpcMsgPArr, SRaftMeta* raftArr, int32_t arrSize,
967
                                                    int32_t vgId) {
968
  ASSERT(rpcMsgPArr != NULL);
969 970 971
  ASSERT(arrSize > 0);

  int32_t dataLen = 0;
M
Minghao Li 已提交
972 973 974
  int32_t raftMetaArrayLen = sizeof(SRaftMeta) * arrSize;
  int32_t rpcArrayLen = sizeof(SRpcMsg) * arrSize;
  dataLen += (raftMetaArrayLen + rpcArrayLen);
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993

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

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

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

    // init msgArr
994
    msgArr[i] = *(rpcMsgPArr[i]);
995 996 997 998 999
  }

  return pMsg;
}

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

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

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

    taosMemoryFree(pMsg);
  }
}

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

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

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

  return pSyncMsg;
}

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

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

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

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

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

    char* s;
    s = syncUtilprintBin((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data", s);
    taosMemoryFree(s);
    s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data2", s);
    taosMemoryFree(s);
  }

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

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

// for debug ----------------------
void syncClientRequestBatchPrint(const SyncClientRequestBatch* pMsg) {
  char* serialized = syncClientRequestBatch2Str(pMsg);
S
Shengliang Guan 已提交
1104
  printf("syncClientRequestBatchPrint | len:%" PRIu64 " | %s \n", strlen(serialized), serialized);
1105 1106 1107 1108 1109 1110
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncClientRequestBatchPrint2(char* s, const SyncClientRequestBatch* pMsg) {
  char* serialized = syncClientRequestBatch2Str(pMsg);
S
Shengliang Guan 已提交
1111
  printf("syncClientRequestBatchPrint2 | len:%" PRIu64 " | %s | %s \n", strlen(serialized), s, serialized);
1112 1113 1114 1115 1116 1117
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncClientRequestBatchLog(const SyncClientRequestBatch* pMsg) {
  char* serialized = syncClientRequestBatch2Str(pMsg);
S
Shengliang Guan 已提交
1118
  sTrace("syncClientRequestBatchLog | len:%" PRIu64 " | %s", strlen(serialized), serialized);
1119 1120 1121 1122 1123 1124
  taosMemoryFree(serialized);
}

void syncClientRequestBatchLog2(char* s, const SyncClientRequestBatch* pMsg) {
  if (gRaftDetailLog) {
    char* serialized = syncClientRequestBatch2Str(pMsg);
S
Shengliang Guan 已提交
1125
    sTraceLong("syncClientRequestBatchLog2 | len:%" PRIu64 " | %s | %s", strlen(serialized), s, serialized);
1126 1127 1128
    taosMemoryFree(serialized);
  }
}
M
Minghao Li 已提交
1129

M
Minghao Li 已提交
1130
// ---- message process SyncRequestVote----
M
Minghao Li 已提交
1131
SyncRequestVote* syncRequestVoteBuild(int32_t vgId) {
M
Minghao Li 已提交
1132
  uint32_t         bytes = sizeof(SyncRequestVote);
wafwerar's avatar
wafwerar 已提交
1133
  SyncRequestVote* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1134 1135
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
M
Minghao Li 已提交
1136
  pMsg->vgId = vgId;
1137
  pMsg->msgType = TDMT_SYNC_REQUEST_VOTE;
M
Minghao Li 已提交
1138
  return pMsg;
M
Minghao Li 已提交
1139 1140
}

M
Minghao Li 已提交
1141 1142
void syncRequestVoteDestroy(SyncRequestVote* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
1143
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
1144 1145
  }
}
M
Minghao Li 已提交
1146

M
Minghao Li 已提交
1147
void syncRequestVoteSerialize(const SyncRequestVote* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
1148
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
1149 1150
  memcpy(buf, pMsg, pMsg->bytes);
}
M
Minghao Li 已提交
1151

M
Minghao Li 已提交
1152 1153
void syncRequestVoteDeserialize(const char* buf, uint32_t len, SyncRequestVote* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
1154
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1155
}
M
Minghao Li 已提交
1156

M
Minghao Li 已提交
1157
char* syncRequestVoteSerialize2(const SyncRequestVote* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
1158
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
1159
  ASSERT(buf != NULL);
M
Minghao Li 已提交
1160 1161 1162 1163 1164 1165 1166 1167 1168
  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 已提交
1169
  SyncRequestVote* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1170
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1171
  syncRequestVoteDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
1172
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1173 1174 1175
  return pMsg;
}

M
Minghao Li 已提交
1176 1177 1178 1179 1180 1181 1182
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 已提交
1183

M
Minghao Li 已提交
1184 1185 1186
void syncRequestVoteFromRpcMsg(const SRpcMsg* pRpcMsg, SyncRequestVote* pMsg) {
  syncRequestVoteDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
}
M
Minghao Li 已提交
1187

M
Minghao Li 已提交
1188 1189
SyncRequestVote* syncRequestVoteFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncRequestVote* pMsg = syncRequestVoteDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
1190
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1191
  return pMsg;
M
Minghao Li 已提交
1192 1193
}

M
Minghao Li 已提交
1194
cJSON* syncRequestVote2Json(const SyncRequestVote* pMsg) {
1195
  char   u64buf[128] = {0};
M
Minghao Li 已提交
1196 1197
  cJSON* pRoot = cJSON_CreateObject();

M
Minghao Li 已提交
1198 1199
  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
1200
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
1201 1202 1203
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
1204
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
M
Minghao Li 已提交
1205 1206 1207 1208
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
1209
      char     host[128] = {0};
M
Minghao Li 已提交
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
      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;
1223
      char     host[128] = {0};
M
Minghao Li 已提交
1224 1225 1226 1227 1228 1229 1230 1231
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
    cJSON_AddItemToObject(pRoot, "destId", pDestId);

S
Shengliang Guan 已提交
1232
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
M
Minghao Li 已提交
1233
    cJSON_AddStringToObject(pRoot, "term", u64buf);
S
Shengliang Guan 已提交
1234
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->lastLogIndex);
M
Minghao Li 已提交
1235
    cJSON_AddStringToObject(pRoot, "lastLogIndex", u64buf);
S
Shengliang Guan 已提交
1236
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->lastLogTerm);
M
Minghao Li 已提交
1237 1238
    cJSON_AddStringToObject(pRoot, "lastLogTerm", u64buf);
  }
M
Minghao Li 已提交
1239 1240 1241 1242

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

M
Minghao Li 已提交
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
char* syncRequestVote2Str(const SyncRequestVote* pMsg) {
  cJSON* pJson = syncRequestVote2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

// for debug ----------------------
void syncRequestVotePrint(const SyncRequestVote* pMsg) {
  char* serialized = syncRequestVote2Str(pMsg);
S
Shengliang Guan 已提交
1255
  printf("syncRequestVotePrint | len:%" PRIu64 " | %s \n", strlen(serialized), serialized);
M
Minghao Li 已提交
1256
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1257
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1258 1259 1260 1261
}

void syncRequestVotePrint2(char* s, const SyncRequestVote* pMsg) {
  char* serialized = syncRequestVote2Str(pMsg);
S
Shengliang Guan 已提交
1262
  printf("syncRequestVotePrint2 | len:%" PRIu64 " | %s | %s \n", strlen(serialized), s, serialized);
M
Minghao Li 已提交
1263
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1264
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1265 1266 1267 1268
}

void syncRequestVoteLog(const SyncRequestVote* pMsg) {
  char* serialized = syncRequestVote2Str(pMsg);
S
Shengliang Guan 已提交
1269
  sTrace("syncRequestVoteLog | len:%" PRIu64 " | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
1270
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1271 1272 1273
}

void syncRequestVoteLog2(char* s, const SyncRequestVote* pMsg) {
M
Minghao Li 已提交
1274 1275
  if (gRaftDetailLog) {
    char* serialized = syncRequestVote2Str(pMsg);
S
Shengliang Guan 已提交
1276
    sTrace("syncRequestVoteLog2 | len:%" PRIu64 " | %s | %s", strlen(serialized), s, serialized);
M
Minghao Li 已提交
1277 1278
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
1279 1280
}

M
Minghao Li 已提交
1281
// ---- message process SyncRequestVoteReply----
M
Minghao Li 已提交
1282
SyncRequestVoteReply* syncRequestVoteReplyBuild(int32_t vgId) {
M
Minghao Li 已提交
1283
  uint32_t              bytes = sizeof(SyncRequestVoteReply);
wafwerar's avatar
wafwerar 已提交
1284
  SyncRequestVoteReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1285 1286
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
M
Minghao Li 已提交
1287
  pMsg->vgId = vgId;
1288
  pMsg->msgType = TDMT_SYNC_REQUEST_VOTE_REPLY;
M
Minghao Li 已提交
1289
  return pMsg;
M
Minghao Li 已提交
1290 1291 1292 1293
}

void syncRequestVoteReplyDestroy(SyncRequestVoteReply* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
1294
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
1295 1296 1297 1298
  }
}

void syncRequestVoteReplySerialize(const SyncRequestVoteReply* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
1299
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
1300 1301 1302 1303 1304
  memcpy(buf, pMsg, pMsg->bytes);
}

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

M
Minghao Li 已提交
1308
char* syncRequestVoteReplySerialize2(const SyncRequestVoteReply* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
1309
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
1310
  ASSERT(buf != NULL);
M
Minghao Li 已提交
1311 1312 1313 1314 1315 1316 1317 1318 1319
  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 已提交
1320
  SyncRequestVoteReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1321
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1322
  syncRequestVoteReplyDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
1323
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1324 1325 1326
  return pMsg;
}

M
Minghao Li 已提交
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
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 已提交
1339 1340
SyncRequestVoteReply* syncRequestVoteReplyFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncRequestVoteReply* pMsg = syncRequestVoteReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
1341
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1342 1343 1344
  return pMsg;
}

M
Minghao Li 已提交
1345
cJSON* syncRequestVoteReply2Json(const SyncRequestVoteReply* pMsg) {
1346
  char   u64buf[128] = {0};
M
Minghao Li 已提交
1347 1348
  cJSON* pRoot = cJSON_CreateObject();

M
Minghao Li 已提交
1349 1350
  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
1351
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
1352 1353 1354
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
1355
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
M
Minghao Li 已提交
1356 1357 1358 1359
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
1360
      char     host[128] = {0};
M
Minghao Li 已提交
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
      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;
1374
      char     host[128] = {0};
M
Minghao Li 已提交
1375 1376 1377 1378 1379 1380 1381 1382
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
    cJSON_AddItemToObject(pRoot, "destId", pDestId);

S
Shengliang Guan 已提交
1383
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
M
Minghao Li 已提交
1384 1385 1386
    cJSON_AddStringToObject(pRoot, "term", u64buf);
    cJSON_AddNumberToObject(pRoot, "vote_granted", pMsg->voteGranted);
  }
M
Minghao Li 已提交
1387 1388 1389 1390

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

M
Minghao Li 已提交
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
char* syncRequestVoteReply2Str(const SyncRequestVoteReply* pMsg) {
  cJSON* pJson = syncRequestVoteReply2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

// for debug ----------------------
void syncRequestVoteReplyPrint(const SyncRequestVoteReply* pMsg) {
  char* serialized = syncRequestVoteReply2Str(pMsg);
S
Shengliang Guan 已提交
1403
  printf("syncRequestVoteReplyPrint | len:%" PRIu64 " | %s \n", strlen(serialized), serialized);
M
Minghao Li 已提交
1404
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1405
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1406 1407 1408 1409
}

void syncRequestVoteReplyPrint2(char* s, const SyncRequestVoteReply* pMsg) {
  char* serialized = syncRequestVoteReply2Str(pMsg);
S
Shengliang Guan 已提交
1410
  printf("syncRequestVoteReplyPrint2 | len:%" PRIu64 " | %s | %s \n", strlen(serialized), s, serialized);
M
Minghao Li 已提交
1411
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1412
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1413 1414 1415 1416
}

void syncRequestVoteReplyLog(const SyncRequestVoteReply* pMsg) {
  char* serialized = syncRequestVoteReply2Str(pMsg);
S
Shengliang Guan 已提交
1417
  sTrace("syncRequestVoteReplyLog | len:%" PRIu64 " | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
1418
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1419 1420 1421
}

void syncRequestVoteReplyLog2(char* s, const SyncRequestVoteReply* pMsg) {
M
Minghao Li 已提交
1422 1423
  if (gRaftDetailLog) {
    char* serialized = syncRequestVoteReply2Str(pMsg);
S
Shengliang Guan 已提交
1424
    sTrace("syncRequestVoteReplyLog2 | len:%" PRIu64 " | %s | %s", strlen(serialized), s, serialized);
M
Minghao Li 已提交
1425 1426
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
1427 1428
}

M
Minghao Li 已提交
1429
// ---- message process SyncAppendEntries----
M
Minghao Li 已提交
1430
SyncAppendEntries* syncAppendEntriesBuild(uint32_t dataLen, int32_t vgId) {
M
Minghao Li 已提交
1431
  uint32_t           bytes = sizeof(SyncAppendEntries) + dataLen;
wafwerar's avatar
wafwerar 已提交
1432
  SyncAppendEntries* 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;
M
Minghao Li 已提交
1437
  pMsg->dataLen = dataLen;
M
Minghao Li 已提交
1438
  return pMsg;
M
Minghao Li 已提交
1439 1440 1441 1442
}

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

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

void syncAppendEntriesDeserialize(const char* buf, uint32_t len, SyncAppendEntries* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
1454 1455
  ASSERT(len == pMsg->bytes);
  ASSERT(pMsg->bytes == sizeof(SyncAppendEntries) + pMsg->dataLen);
M
Minghao Li 已提交
1456 1457 1458
}

char* syncAppendEntriesSerialize2(const SyncAppendEntries* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
1459
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
1460
  ASSERT(buf != NULL);
M
Minghao Li 已提交
1461 1462 1463 1464 1465 1466 1467 1468 1469
  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 已提交
1470
  SyncAppendEntries* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1471
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1472
  syncAppendEntriesDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
1473
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1474
  return pMsg;
M
Minghao Li 已提交
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
}

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 已提交
1489 1490
SyncAppendEntries* syncAppendEntriesFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncAppendEntries* pMsg = syncAppendEntriesDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
1491
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1492 1493 1494
  return pMsg;
}

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

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

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
1505
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
M
Minghao Li 已提交
1506 1507 1508 1509
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
1510
      char     host[128] = {0};
M
Minghao Li 已提交
1511 1512 1513 1514 1515 1516 1517 1518 1519
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

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

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

S
Shengliang Guan 已提交
1537
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->privateTerm);
1538 1539
    cJSON_AddStringToObject(pRoot, "privateTerm", u64buf);

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

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

S
Shengliang Guan 已提交
1546
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->commitIndex);
M
Minghao Li 已提交
1547
    cJSON_AddStringToObject(pRoot, "commitIndex", u64buf);
M
Minghao Li 已提交
1548 1549 1550 1551 1552

    cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen);
    char* s;
    s = syncUtilprintBin((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data", s);
wafwerar's avatar
wafwerar 已提交
1553
    taosMemoryFree(s);
M
Minghao Li 已提交
1554 1555
    s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data2", s);
wafwerar's avatar
wafwerar 已提交
1556
    taosMemoryFree(s);
M
Minghao Li 已提交
1557
  }
M
Minghao Li 已提交
1558 1559 1560 1561 1562 1563

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

M
Minghao Li 已提交
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
char* syncAppendEntries2Str(const SyncAppendEntries* pMsg) {
  cJSON* pJson = syncAppendEntries2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

// for debug ----------------------
void syncAppendEntriesPrint(const SyncAppendEntries* pMsg) {
  char* serialized = syncAppendEntries2Str(pMsg);
S
Shengliang Guan 已提交
1574
  printf("syncAppendEntriesPrint | len:%" PRIu64 " | %s \n", strlen(serialized), serialized);
M
Minghao Li 已提交
1575
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1576
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1577 1578 1579 1580
}

void syncAppendEntriesPrint2(char* s, const SyncAppendEntries* pMsg) {
  char* serialized = syncAppendEntries2Str(pMsg);
S
Shengliang Guan 已提交
1581
  printf("syncAppendEntriesPrint2 | len:%" PRIu64 " | %s | %s \n", strlen(serialized), s, serialized);
M
Minghao Li 已提交
1582
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1583
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1584 1585 1586 1587
}

void syncAppendEntriesLog(const SyncAppendEntries* pMsg) {
  char* serialized = syncAppendEntries2Str(pMsg);
S
Shengliang Guan 已提交
1588
  sTrace("syncAppendEntriesLog | len:%" PRIu64 " | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
1589
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1590 1591 1592
}

void syncAppendEntriesLog2(char* s, const SyncAppendEntries* pMsg) {
M
Minghao Li 已提交
1593 1594
  if (gRaftDetailLog) {
    char* serialized = syncAppendEntries2Str(pMsg);
S
Shengliang Guan 已提交
1595
    sTrace("syncAppendEntriesLog2 | len:%" PRIu64 " | %s | %s", strlen(serialized), s, serialized);
M
Minghao Li 已提交
1596 1597
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
1598 1599
}

1600 1601 1602 1603
// ---- message process SyncAppendEntriesBatch----

// block1: SOffsetAndContLen
// block2: SOffsetAndContLen Array
1604
// block3: entry Array
1605

1606 1607
SyncAppendEntriesBatch* syncAppendEntriesBatchBuild(SSyncRaftEntry** entryPArr, int32_t arrSize, int32_t vgId) {
  ASSERT(entryPArr != NULL);
1608
  ASSERT(arrSize >= 0);
1609 1610 1611

  int32_t dataLen = 0;
  int32_t metaArrayLen = sizeof(SOffsetAndContLen) * arrSize;  // <offset, contLen>
1612
  int32_t entryArrayLen = 0;
1613
  for (int i = 0; i < arrSize; ++i) {  // SRpcMsg pCont
1614 1615
    SSyncRaftEntry* pEntry = entryPArr[i];
    entryArrayLen += pEntry->bytes;
1616
  }
1617
  dataLen += (metaArrayLen + entryArrayLen);
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631

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

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

  for (int i = 0; i < arrSize; ++i) {
1632
    // init meta <offset, contLen>
1633
    if (i == 0) {
1634 1635
      metaArr[i].offset = metaArrayLen;
      metaArr[i].contLen = entryPArr[i]->bytes;
1636 1637
    } else {
      metaArr[i].offset = metaArr[i - 1].offset + metaArr[i - 1].contLen;
1638
      metaArr[i].contLen = entryPArr[i]->bytes;
1639 1640
    }

1641 1642 1643
    // init entry array
    ASSERT(metaArr[i].contLen == entryPArr[i]->bytes);
    memcpy(pData + metaArr[i].offset, entryPArr[i], metaArr[i].contLen);
1644 1645 1646 1647
  }

  return pMsg;
}
1648

1649 1650
SOffsetAndContLen* syncAppendEntriesBatchMetaTableArray(SyncAppendEntriesBatch* pMsg) {
  return (SOffsetAndContLen*)(pMsg->data);
1651
}
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716

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

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

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

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

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

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

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

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

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

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

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
1717
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
      char     host[128] = {0};
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

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

S
Shengliang Guan 已提交
1746
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
1747 1748
    cJSON_AddStringToObject(pRoot, "term", u64buf);

S
Shengliang Guan 已提交
1749
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->prevLogIndex);
1750 1751
    cJSON_AddStringToObject(pRoot, "prevLogIndex", u64buf);

S
Shengliang Guan 已提交
1752
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->prevLogTerm);
1753 1754
    cJSON_AddStringToObject(pRoot, "prevLogTerm", u64buf);

S
Shengliang Guan 已提交
1755
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->commitIndex);
1756 1757
    cJSON_AddStringToObject(pRoot, "commitIndex", u64buf);

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

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

    int32_t metaArrayLen = sizeof(SOffsetAndContLen) * pMsg->dataCount;  // <offset, contLen>
1765
    int32_t entryArrayLen = pMsg->dataLen - metaArrayLen;
1766 1767

    cJSON_AddNumberToObject(pRoot, "metaArrayLen", metaArrayLen);
1768
    cJSON_AddNumberToObject(pRoot, "entryArrayLen", entryArrayLen);
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780

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

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

1781 1782
    cJSON* pEntryArr = cJSON_CreateArray();
    cJSON_AddItemToObject(pRoot, "entryArr", pEntryArr);
1783
    for (int i = 0; i < pMsg->dataCount; ++i) {
1784 1785 1786
      SSyncRaftEntry* pEntry = (SSyncRaftEntry*)(pMsg->data + metaArr[i].offset);
      cJSON*          pEntryJson = syncEntry2Json(pEntry);
      cJSON_AddItemToArray(pEntryArr, pEntryJson);
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
    }

    char* s;
    s = syncUtilprintBin((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data", s);
    taosMemoryFree(s);
    s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data2", s);
    taosMemoryFree(s);
  }

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

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

// for debug ----------------------
void syncAppendEntriesBatchPrint(const SyncAppendEntriesBatch* pMsg) {
  char* serialized = syncAppendEntriesBatch2Str(pMsg);
S
Shengliang Guan 已提交
1813
  printf("syncAppendEntriesBatchPrint | len:%" PRIu64 " | %s \n", strlen(serialized), serialized);
1814 1815 1816 1817 1818 1819
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncAppendEntriesBatchPrint2(char* s, const SyncAppendEntriesBatch* pMsg) {
  char* serialized = syncAppendEntriesBatch2Str(pMsg);
S
Shengliang Guan 已提交
1820
  printf("syncAppendEntriesBatchPrint2 | len:%" PRIu64 " | %s | %s \n", strlen(serialized), s, serialized);
1821 1822 1823 1824 1825 1826
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncAppendEntriesBatchLog(const SyncAppendEntriesBatch* pMsg) {
  char* serialized = syncAppendEntriesBatch2Str(pMsg);
S
Shengliang Guan 已提交
1827
  sTrace("syncAppendEntriesBatchLog | len:%" PRIu64 " | %s", strlen(serialized), serialized);
1828 1829 1830 1831 1832 1833
  taosMemoryFree(serialized);
}

void syncAppendEntriesBatchLog2(char* s, const SyncAppendEntriesBatch* pMsg) {
  if (gRaftDetailLog) {
    char* serialized = syncAppendEntriesBatch2Str(pMsg);
S
Shengliang Guan 已提交
1834
    sTraceLong("syncAppendEntriesBatchLog2 | len:%" PRIu64 " | %s | %s", strlen(serialized), s, serialized);
1835 1836 1837 1838
    taosMemoryFree(serialized);
  }
}

M
Minghao Li 已提交
1839
// ---- message process SyncAppendEntriesReply----
M
Minghao Li 已提交
1840
SyncAppendEntriesReply* syncAppendEntriesReplyBuild(int32_t vgId) {
M
Minghao Li 已提交
1841
  uint32_t                bytes = sizeof(SyncAppendEntriesReply);
wafwerar's avatar
wafwerar 已提交
1842
  SyncAppendEntriesReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1843 1844
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
M
Minghao Li 已提交
1845
  pMsg->vgId = vgId;
1846
  pMsg->msgType = TDMT_SYNC_APPEND_ENTRIES_REPLY;
M
Minghao Li 已提交
1847
  return pMsg;
M
Minghao Li 已提交
1848 1849 1850 1851
}

void syncAppendEntriesReplyDestroy(SyncAppendEntriesReply* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
1852
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
1853 1854 1855 1856
  }
}

void syncAppendEntriesReplySerialize(const SyncAppendEntriesReply* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
1857
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
1858 1859 1860 1861 1862
  memcpy(buf, pMsg, pMsg->bytes);
}

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

M
Minghao Li 已提交
1866
char* syncAppendEntriesReplySerialize2(const SyncAppendEntriesReply* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
1867
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
1868
  ASSERT(buf != NULL);
M
Minghao Li 已提交
1869 1870 1871 1872 1873 1874 1875 1876 1877
  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 已提交
1878
  SyncAppendEntriesReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1879
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1880
  syncAppendEntriesReplyDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
1881
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
1882 1883 1884
  return pMsg;
}

M
Minghao Li 已提交
1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
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 已提交
1897 1898
SyncAppendEntriesReply* syncAppendEntriesReplyFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncAppendEntriesReply* pMsg = syncAppendEntriesReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
1899
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
1900 1901 1902
  return pMsg;
}

M
Minghao Li 已提交
1903
cJSON* syncAppendEntriesReply2Json(const SyncAppendEntriesReply* pMsg) {
1904
  char   u64buf[128] = {0};
M
Minghao Li 已提交
1905
  cJSON* pRoot = cJSON_CreateObject();
M
Minghao Li 已提交
1906 1907 1908

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
1909
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
1910 1911 1912
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
1913
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
M
Minghao Li 已提交
1914 1915 1916 1917
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
1918
      char     host[128] = {0};
M
Minghao Li 已提交
1919 1920 1921 1922 1923 1924 1925 1926 1927
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

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

S
Shengliang Guan 已提交
1942
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->privateTerm);
1943 1944
    cJSON_AddStringToObject(pRoot, "privateTerm", u64buf);

S
Shengliang Guan 已提交
1945
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
M
Minghao Li 已提交
1946 1947
    cJSON_AddStringToObject(pRoot, "term", u64buf);
    cJSON_AddNumberToObject(pRoot, "success", pMsg->success);
S
Shengliang Guan 已提交
1948
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->matchIndex);
M
Minghao Li 已提交
1949
    cJSON_AddStringToObject(pRoot, "matchIndex", u64buf);
1950 1951
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->startTime);
    cJSON_AddStringToObject(pRoot, "startTime", u64buf);
M
Minghao Li 已提交
1952 1953 1954 1955 1956
  }

  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncAppendEntriesReply", pRoot);
  return pJson;
M
Minghao Li 已提交
1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968
}

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

// for debug ----------------------
void syncAppendEntriesReplyPrint(const SyncAppendEntriesReply* pMsg) {
  char* serialized = syncAppendEntriesReply2Str(pMsg);
S
Shengliang Guan 已提交
1969
  printf("syncAppendEntriesReplyPrint | len:%" PRIu64 " | %s \n", strlen(serialized), serialized);
M
Minghao Li 已提交
1970
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1971
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1972 1973 1974 1975
}

void syncAppendEntriesReplyPrint2(char* s, const SyncAppendEntriesReply* pMsg) {
  char* serialized = syncAppendEntriesReply2Str(pMsg);
S
Shengliang Guan 已提交
1976
  printf("syncAppendEntriesReplyPrint2 | len:%" PRIu64 " | %s | %s \n", strlen(serialized), s, serialized);
M
Minghao Li 已提交
1977
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1978
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1979 1980 1981 1982
}

void syncAppendEntriesReplyLog(const SyncAppendEntriesReply* pMsg) {
  char* serialized = syncAppendEntriesReply2Str(pMsg);
S
Shengliang Guan 已提交
1983
  sTrace("syncAppendEntriesReplyLog | len:%" PRIu64 " | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
1984
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1985 1986 1987
}

void syncAppendEntriesReplyLog2(char* s, const SyncAppendEntriesReply* pMsg) {
M
Minghao Li 已提交
1988 1989
  if (gRaftDetailLog) {
    char* serialized = syncAppendEntriesReply2Str(pMsg);
S
Shengliang Guan 已提交
1990
    sTrace("syncAppendEntriesReplyLog2 | len:%" PRIu64 " | %s | %s", strlen(serialized), s, serialized);
M
Minghao Li 已提交
1991 1992
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
1993
}
M
Minghao Li 已提交
1994

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

void syncHeartbeatPrint(const SyncHeartbeat* pMsg) {
  char* serialized = syncHeartbeat2Str(pMsg);
  printf("syncHeartbeatPrint | len:%" PRIu64 " | %s \n", strlen(serialized), serialized);
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncHeartbeatPrint2(char* s, const SyncHeartbeat* pMsg) {
  char* serialized = syncHeartbeat2Str(pMsg);
  printf("syncHeartbeatPrint2 | len:%" PRIu64 " | %s | %s \n", strlen(serialized), s, serialized);
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncHeartbeatLog(const SyncHeartbeat* pMsg) {
  char* serialized = syncHeartbeat2Str(pMsg);
  sTrace("syncHeartbeatLog | len:%" PRIu64 " | %s", strlen(serialized), serialized);
  taosMemoryFree(serialized);
}

void syncHeartbeatLog2(char* s, const SyncHeartbeat* pMsg) {
  if (gRaftDetailLog) {
    char* serialized = syncHeartbeat2Str(pMsg);
    sTrace("syncHeartbeatLog2 | len:%" PRIu64 " | %s | %s", strlen(serialized), s, serialized);
    taosMemoryFree(serialized);
  }
}

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 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301
// ---- message process SyncHeartbeatReply----
SyncHeartbeatReply* syncHeartbeatReplyBuild(int32_t vgId) {
  uint32_t            bytes = sizeof(SyncHeartbeatReply);
  SyncHeartbeatReply* pMsg = taosMemoryMalloc(bytes);
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
  pMsg->vgId = vgId;
  pMsg->msgType = TDMT_SYNC_HEARTBEAT_REPLY;
  return pMsg;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

void syncHeartbeatReplyPrint(const SyncHeartbeatReply* pMsg) {
  char* serialized = syncHeartbeatReply2Str(pMsg);
  printf("syncHeartbeatReplyPrint | len:%" PRIu64 " | %s \n", strlen(serialized), serialized);
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncHeartbeatReplyPrint2(char* s, const SyncHeartbeatReply* pMsg) {
  char* serialized = syncHeartbeatReply2Str(pMsg);
  printf("syncHeartbeatReplyPrint2 | len:%" PRIu64 " | %s | %s \n", strlen(serialized), s, serialized);
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncHeartbeatReplyLog(const SyncHeartbeatReply* pMsg) {
  char* serialized = syncHeartbeatReply2Str(pMsg);
  sTrace("syncHeartbeatReplyLog | len:%" PRIu64 " | %s", strlen(serialized), serialized);
  taosMemoryFree(serialized);
}

void syncHeartbeatReplyLog2(char* s, const SyncHeartbeatReply* pMsg) {
  if (gRaftDetailLog) {
    char* serialized = syncHeartbeatReply2Str(pMsg);
    sTrace("syncHeartbeatReplyLog2 | len:%" PRIu64 " | %s | %s", strlen(serialized), s, serialized);
    taosMemoryFree(serialized);
  }
}

M
Minghao Li 已提交
2302
// ---- message process SyncApplyMsg----
M
Minghao Li 已提交
2303 2304 2305 2306 2307
SyncApplyMsg* syncApplyMsgBuild(uint32_t dataLen) {
  uint32_t      bytes = sizeof(SyncApplyMsg) + dataLen;
  SyncApplyMsg* pMsg = taosMemoryMalloc(bytes);
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
2308
  pMsg->msgType = TDMT_SYNC_APPLY_MSG;
M
Minghao Li 已提交
2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328
  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 已提交
2329
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
2330 2331 2332 2333 2334
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncApplyMsgDeserialize(const char* buf, uint32_t len, SyncApplyMsg* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
2335
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
2336 2337 2338 2339
}

char* syncApplyMsgSerialize2(const SyncApplyMsg* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
2340
  ASSERT(buf != NULL);
M
Minghao Li 已提交
2341 2342 2343 2344 2345 2346 2347 2348 2349 2350
  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 已提交
2351
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
2352
  syncApplyMsgDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
2353
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
2354 2355
  return pMsg;
}
M
Minghao Li 已提交
2356 2357

// SyncApplyMsg to SRpcMsg, put it into ApplyQ
M
Minghao Li 已提交
2358 2359 2360 2361 2362 2363 2364
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 已提交
2365 2366

// get SRpcMsg from ApplyQ, to SyncApplyMsg
M
Minghao Li 已提交
2367 2368 2369 2370 2371 2372 2373 2374
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 已提交
2375 2376

// SyncApplyMsg to OriginalRpcMsg
M
Minghao Li 已提交
2377 2378 2379 2380 2381 2382 2383 2384 2385
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) {
2386
  char   u64buf[128] = {0};
M
Minghao Li 已提交
2387 2388 2389 2390 2391 2392 2393 2394
  cJSON* pRoot = cJSON_CreateObject();

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

S
Shengliang Guan 已提交
2395
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->fsmMeta.index);
M
Minghao Li 已提交
2396 2397 2398 2399 2400
    cJSON_AddStringToObject(pRoot, "fsmMeta.index", u64buf);
    cJSON_AddNumberToObject(pRoot, "fsmMeta.isWeak", pMsg->fsmMeta.isWeak);
    cJSON_AddNumberToObject(pRoot, "fsmMeta.code", pMsg->fsmMeta.code);
    cJSON_AddNumberToObject(pRoot, "fsmMeta.state", pMsg->fsmMeta.state);
    cJSON_AddStringToObject(pRoot, "fsmMeta.state.str", syncUtilState2String(pMsg->fsmMeta.state));
S
Shengliang Guan 已提交
2401
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->fsmMeta.seqNum);
M
Minghao Li 已提交
2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412
    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 已提交
2413

M
Minghao Li 已提交
2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424
  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 已提交
2425 2426

// for debug ----------------------
M
Minghao Li 已提交
2427 2428
void syncApplyMsgPrint(const SyncApplyMsg* pMsg) {
  char* serialized = syncApplyMsg2Str(pMsg);
S
Shengliang Guan 已提交
2429
  printf("syncApplyMsgPrint | len:%" PRIu64 " | %s \n", strlen(serialized), serialized);
M
Minghao Li 已提交
2430 2431 2432 2433 2434 2435
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncApplyMsgPrint2(char* s, const SyncApplyMsg* pMsg) {
  char* serialized = syncApplyMsg2Str(pMsg);
S
Shengliang Guan 已提交
2436
  printf("syncApplyMsgPrint2 | len:%" PRIu64 " | %s | %s \n", strlen(serialized), s, serialized);
M
Minghao Li 已提交
2437 2438 2439 2440 2441 2442
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncApplyMsgLog(const SyncApplyMsg* pMsg) {
  char* serialized = syncApplyMsg2Str(pMsg);
S
Shengliang Guan 已提交
2443
  sTrace("ssyncApplyMsgLog | len:%" PRIu64 " | %s", strlen(serialized), serialized);
M
Minghao Li 已提交
2444 2445 2446 2447
  taosMemoryFree(serialized);
}

void syncApplyMsgLog2(char* s, const SyncApplyMsg* pMsg) {
M
Minghao Li 已提交
2448 2449
  if (gRaftDetailLog) {
    char* serialized = syncApplyMsg2Str(pMsg);
S
Shengliang Guan 已提交
2450
    sTrace("syncApplyMsgLog2 | len:%" PRIu64 " | %s | %s", strlen(serialized), s, serialized);
M
Minghao Li 已提交
2451 2452
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
2453
}
2454 2455 2456 2457 2458 2459 2460 2461

// ---------------------------------------------
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;
2462
  pMsg->msgType = TDMT_SYNC_SNAPSHOT_SEND;
2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473
  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 已提交
2474
  ASSERT(pMsg->bytes <= bufLen);
2475 2476 2477 2478 2479
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncSnapshotSendDeserialize(const char* buf, uint32_t len, SyncSnapshotSend* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
2480 2481
  ASSERT(len == pMsg->bytes);
  ASSERT(pMsg->bytes == sizeof(SyncSnapshotSend) + pMsg->dataLen);
2482 2483 2484 2485
}

char* syncSnapshotSendSerialize2(const SyncSnapshotSend* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
2486
  ASSERT(buf != NULL);
2487 2488 2489 2490 2491 2492 2493 2494 2495 2496
  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 已提交
2497
  ASSERT(pMsg != NULL);
2498
  syncSnapshotSendDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
2499
  ASSERT(len == pMsg->bytes);
2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516
  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 已提交
2517
  ASSERT(pMsg != NULL);
2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530
  return pMsg;
}

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

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

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
2531
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
      char     host[128];
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

    cJSON* pDestId = cJSON_CreateObject();
S
Shengliang Guan 已提交
2546
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
      char     host[128];
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
    cJSON_AddItemToObject(pRoot, "destId", pDestId);

S
Shengliang Guan 已提交
2560
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
2561 2562
    cJSON_AddStringToObject(pRoot, "term", u64buf);

S
Shengliang Guan 已提交
2563
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->privateTerm);
2564 2565
    cJSON_AddStringToObject(pRoot, "privateTerm", u64buf);

S
Shengliang Guan 已提交
2566
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->beginIndex);
2567 2568
    cJSON_AddStringToObject(pRoot, "beginIndex", u64buf);

S
Shengliang Guan 已提交
2569
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->lastIndex);
2570 2571
    cJSON_AddStringToObject(pRoot, "lastIndex", u64buf);

S
Shengliang Guan 已提交
2572
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->lastConfigIndex);
2573 2574 2575
    cJSON_AddStringToObject(pRoot, "lastConfigIndex", u64buf);
    cJSON_AddItemToObject(pRoot, "lastConfig", syncCfg2Json((SSyncCfg*)&(pMsg->lastConfig)));

S
Shengliang Guan 已提交
2576
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->lastTerm);
2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605
    cJSON_AddStringToObject(pRoot, "lastTerm", u64buf);

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

    cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen);
    char* s;
    s = syncUtilprintBin((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data", s);
    taosMemoryFree(s);
    s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data2", s);
    taosMemoryFree(s);
  }

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

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

// for debug ----------------------
void syncSnapshotSendPrint(const SyncSnapshotSend* pMsg) {
  char* serialized = syncSnapshotSend2Str(pMsg);
S
Shengliang Guan 已提交
2606
  printf("syncSnapshotSendPrint | len:%" PRIu64 " | %s \n", strlen(serialized), serialized);
2607 2608 2609 2610 2611 2612
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncSnapshotSendPrint2(char* s, const SyncSnapshotSend* pMsg) {
  char* serialized = syncSnapshotSend2Str(pMsg);
S
Shengliang Guan 已提交
2613
  printf("syncSnapshotSendPrint2 | len:%" PRIu64 " | %s | %s \n", strlen(serialized), s, serialized);
2614 2615 2616 2617 2618 2619
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncSnapshotSendLog(const SyncSnapshotSend* pMsg) {
  char* serialized = syncSnapshotSend2Str(pMsg);
S
Shengliang Guan 已提交
2620
  sTrace("syncSnapshotSendLog | len:%" PRIu64 " | %s", strlen(serialized), serialized);
2621 2622 2623 2624
  taosMemoryFree(serialized);
}

void syncSnapshotSendLog2(char* s, const SyncSnapshotSend* pMsg) {
M
Minghao Li 已提交
2625 2626
  if (gRaftDetailLog) {
    char* serialized = syncSnapshotSend2Str(pMsg);
S
Shengliang Guan 已提交
2627
    sTrace("syncSnapshotSendLog2 | len:%" PRIu64 " | %s | %s", strlen(serialized), s, serialized);
M
Minghao Li 已提交
2628 2629
    taosMemoryFree(serialized);
  }
2630 2631 2632
}

// ---------------------------------------------
M
Minghao Li 已提交
2633
SyncSnapshotRsp* syncSnapshotRspBuild(int32_t vgId) {
2634 2635 2636 2637 2638
  uint32_t         bytes = sizeof(SyncSnapshotRsp);
  SyncSnapshotRsp* pMsg = taosMemoryMalloc(bytes);
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
  pMsg->vgId = vgId;
2639
  pMsg->msgType = TDMT_SYNC_SNAPSHOT_RSP;
2640 2641 2642 2643 2644 2645 2646 2647 2648 2649
  return pMsg;
}

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

void syncSnapshotRspSerialize(const SyncSnapshotRsp* pMsg, char* buf, uint32_t bufLen) {
M
Minghao Li 已提交
2650
  ASSERT(pMsg->bytes <= bufLen);
2651 2652 2653 2654 2655
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncSnapshotRspDeserialize(const char* buf, uint32_t len, SyncSnapshotRsp* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
2656
  ASSERT(len == pMsg->bytes);
2657 2658 2659 2660
}

char* syncSnapshotRspSerialize2(const SyncSnapshotRsp* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
2661
  ASSERT(buf != NULL);
2662 2663 2664 2665 2666 2667 2668 2669 2670 2671
  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 已提交
2672
  ASSERT(pMsg != NULL);
2673
  syncSnapshotRspDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
2674
  ASSERT(len == pMsg->bytes);
2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691
  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 已提交
2692
  ASSERT(pMsg != NULL);
2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705
  return pMsg;
}

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

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

    cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
2706
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
      char     host[128];
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

    cJSON* pDestId = cJSON_CreateObject();
S
Shengliang Guan 已提交
2721
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
      char     host[128];
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
    cJSON_AddItemToObject(pRoot, "destId", pDestId);

S
Shengliang Guan 已提交
2735
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->term);
2736 2737
    cJSON_AddStringToObject(pRoot, "term", u64buf);

S
Shengliang Guan 已提交
2738
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->privateTerm);
2739 2740
    cJSON_AddStringToObject(pRoot, "privateTerm", u64buf);

S
Shengliang Guan 已提交
2741
    snprintf(u64buf, sizeof(u64buf), "%" PRId64, pMsg->lastIndex);
2742 2743
    cJSON_AddStringToObject(pRoot, "lastIndex", u64buf);

S
Shengliang Guan 已提交
2744
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->lastTerm);
2745 2746 2747
    cJSON_AddStringToObject(pRoot, "lastTerm", u64buf);

    cJSON_AddNumberToObject(pRoot, "ack", pMsg->ack);
2748
    cJSON_AddNumberToObject(pRoot, "code", pMsg->code);
2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765
  }

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

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

// for debug ----------------------
void syncSnapshotRspPrint(const SyncSnapshotRsp* pMsg) {
  char* serialized = syncSnapshotRsp2Str(pMsg);
S
Shengliang Guan 已提交
2766
  printf("syncSnapshotRspPrint | len:%" PRIu64 " | %s \n", strlen(serialized), serialized);
2767 2768 2769 2770 2771 2772
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncSnapshotRspPrint2(char* s, const SyncSnapshotRsp* pMsg) {
  char* serialized = syncSnapshotRsp2Str(pMsg);
S
Shengliang Guan 已提交
2773
  printf("syncSnapshotRspPrint2 | len:%" PRIu64 " | %s | %s \n", strlen(serialized), s, serialized);
2774 2775 2776 2777 2778 2779
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncSnapshotRspLog(const SyncSnapshotRsp* pMsg) {
  char* serialized = syncSnapshotRsp2Str(pMsg);
S
Shengliang Guan 已提交
2780
  sTrace("syncSnapshotRspLog | len:%" PRIu64 " | %s", strlen(serialized), serialized);
2781 2782 2783 2784
  taosMemoryFree(serialized);
}

void syncSnapshotRspLog2(char* s, const SyncSnapshotRsp* pMsg) {
M
Minghao Li 已提交
2785 2786
  if (gRaftDetailLog) {
    char* serialized = syncSnapshotRsp2Str(pMsg);
S
Shengliang Guan 已提交
2787
    sTrace("syncSnapshotRspLog2 | len:%" PRIu64 " | %s | %s", strlen(serialized), s, serialized);
M
Minghao Li 已提交
2788 2789
    taosMemoryFree(serialized);
  }
2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809
}

// ---------------------------------------------
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 已提交
2810
  ASSERT(pMsg->bytes <= bufLen);
2811 2812 2813 2814 2815
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncLeaderTransferDeserialize(const char* buf, uint32_t len, SyncLeaderTransfer* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
2816
  ASSERT(len == pMsg->bytes);
2817 2818 2819 2820
}

char* syncLeaderTransferSerialize2(const SyncLeaderTransfer* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
2821
  ASSERT(buf != NULL);
2822 2823 2824 2825 2826 2827 2828 2829 2830 2831
  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 已提交
2832
  ASSERT(pMsg != NULL);
2833
  syncLeaderTransferDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
2834
  ASSERT(len == pMsg->bytes);
2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851
  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 已提交
2852
  ASSERT(pMsg != NULL);
2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866
  return pMsg;
}

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

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

    /*
        cJSON* pSrcId = cJSON_CreateObject();
S
Shengliang Guan 已提交
2867
        snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->srcId.addr);
2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881
        cJSON_AddStringToObject(pSrcId, "addr", u64buf);
        {
          uint64_t u64 = pMsg->srcId.addr;
          cJSON*   pTmp = pSrcId;
          char     host[128];
          uint16_t port;
          syncUtilU642Addr(u64, host, sizeof(host), &port);
          cJSON_AddStringToObject(pTmp, "addr_host", host);
          cJSON_AddNumberToObject(pTmp, "addr_port", port);
        }
        cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
        cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

        cJSON* pDestId = cJSON_CreateObject();
S
Shengliang Guan 已提交
2882
        snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->destId.addr);
2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897
        cJSON_AddStringToObject(pDestId, "addr", u64buf);
        {
          uint64_t u64 = pMsg->destId.addr;
          cJSON*   pTmp = pDestId;
          char     host[128];
          uint16_t port;
          syncUtilU642Addr(u64, host, sizeof(host), &port);
          cJSON_AddStringToObject(pTmp, "addr_host", host);
          cJSON_AddNumberToObject(pTmp, "addr_port", port);
        }
        cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
        cJSON_AddItemToObject(pRoot, "destId", pDestId);
    */

    cJSON* pNewerId = cJSON_CreateObject();
S
Shengliang Guan 已提交
2898
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->newLeaderId.addr);
2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927
    cJSON_AddStringToObject(pNewerId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->newLeaderId.addr;
      cJSON*   pTmp = pNewerId;
      char     host[128];
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pNewerId, "vgId", pMsg->newLeaderId.vgId);
    cJSON_AddItemToObject(pRoot, "newLeaderId", pNewerId);
  }

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

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

// for debug ----------------------
void syncLeaderTransferPrint(const SyncLeaderTransfer* pMsg) {
  char* serialized = syncLeaderTransfer2Str(pMsg);
S
Shengliang Guan 已提交
2928
  printf("syncLeaderTransferPrint | len:%" PRIu64 " | %s \n", strlen(serialized), serialized);
2929 2930 2931 2932 2933 2934
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncLeaderTransferPrint2(char* s, const SyncLeaderTransfer* pMsg) {
  char* serialized = syncLeaderTransfer2Str(pMsg);
S
Shengliang Guan 已提交
2935
  printf("syncLeaderTransferPrint2 | len:%" PRIu64 " | %s | %s \n", strlen(serialized), s, serialized);
2936 2937 2938 2939 2940 2941
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncLeaderTransferLog(const SyncLeaderTransfer* pMsg) {
  char* serialized = syncLeaderTransfer2Str(pMsg);
S
Shengliang Guan 已提交
2942
  sTrace("syncLeaderTransferLog | len:%" PRIu64 " | %s", strlen(serialized), serialized);
2943 2944 2945 2946 2947 2948
  taosMemoryFree(serialized);
}

void syncLeaderTransferLog2(char* s, const SyncLeaderTransfer* pMsg) {
  if (gRaftDetailLog) {
    char* serialized = syncLeaderTransfer2Str(pMsg);
S
Shengliang Guan 已提交
2949
    sTrace("syncLeaderTransferLog2 | len:%" PRIu64 " | %s | %s", strlen(serialized), s, serialized);
2950 2951
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971
}

// ---------------------------------------------
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 已提交
2972
  ASSERT(pMsg->bytes <= bufLen);
M
Minghao Li 已提交
2973 2974 2975 2976 2977
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncReconfigFinishDeserialize(const char* buf, uint32_t len, SyncReconfigFinish* pMsg) {
  memcpy(pMsg, buf, len);
M
Minghao Li 已提交
2978
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
2979 2980 2981 2982
}

char* syncReconfigFinishSerialize2(const SyncReconfigFinish* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
2983
  ASSERT(buf != NULL);
M
Minghao Li 已提交
2984 2985 2986 2987 2988 2989 2990 2991 2992 2993
  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 已提交
2994
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
2995
  syncReconfigFinishDeserialize(buf, len, pMsg);
M
Minghao Li 已提交
2996
  ASSERT(len == pMsg->bytes);
M
Minghao Li 已提交
2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013
  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 已提交
3014
  ASSERT(pMsg != NULL);
M
Minghao Li 已提交
3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031
  return pMsg;
}

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

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

    cJSON* pOldCfg = syncCfg2Json((SSyncCfg*)(&(pMsg->oldCfg)));
    cJSON* pNewCfg = syncCfg2Json((SSyncCfg*)(&(pMsg->newCfg)));
    cJSON_AddItemToObject(pRoot, "oldCfg", pOldCfg);
    cJSON_AddItemToObject(pRoot, "newCfg", pNewCfg);

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

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

S
Shengliang Guan 已提交
3038
    snprintf(u64buf, sizeof(u64buf), "%" PRIu64, pMsg->newCfgSeqNum);
M
Minghao Li 已提交
3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056
    cJSON_AddStringToObject(pRoot, "newCfgSeqNum", u64buf);
  }

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

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

// for debug ----------------------
void syncReconfigFinishPrint(const SyncReconfigFinish* pMsg) {
  char* serialized = syncReconfigFinish2Str(pMsg);
S
Shengliang Guan 已提交
3057
  printf("syncReconfigFinishPrint | len:%" PRIu64 " | %s \n", strlen(serialized), serialized);
M
Minghao Li 已提交
3058 3059 3060 3061 3062 3063
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncReconfigFinishPrint2(char* s, const SyncReconfigFinish* pMsg) {
  char* serialized = syncReconfigFinish2Str(pMsg);
S
Shengliang Guan 已提交
3064
  printf("syncReconfigFinishPrint2 | len:%" PRIu64 " | %s | %s \n", strlen(serialized), s, serialized);
M
Minghao Li 已提交
3065 3066 3067 3068 3069 3070
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncReconfigFinishLog(const SyncReconfigFinish* pMsg) {
  char* serialized = syncReconfigFinish2Str(pMsg);
S
Shengliang Guan 已提交
3071
  sTrace("syncReconfigFinishLog | len:%" PRIu64 " | %s", strlen(serialized), serialized);
M
Minghao Li 已提交
3072 3073 3074 3075 3076 3077
  taosMemoryFree(serialized);
}

void syncReconfigFinishLog2(char* s, const SyncReconfigFinish* pMsg) {
  if (gRaftDetailLog) {
    char* serialized = syncReconfigFinish2Str(pMsg);
S
Shengliang Guan 已提交
3078
    sTrace("syncReconfigFinishLog2 | len:%" PRIu64 " | %s | %s", strlen(serialized), s, serialized);
M
Minghao Li 已提交
3079 3080
    taosMemoryFree(serialized);
  }
3081
}