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

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

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

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

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

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

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

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

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

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

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

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

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

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

78
  } else if (pRpcMsg->msgType == TDMT_SYNC_COMMON_RESPONSE) {
M
Minghao Li 已提交
79 80 81 82
    pRoot = cJSON_CreateObject();
    char* s;
    s = syncUtilprintBin((char*)(pRpcMsg->pCont), pRpcMsg->contLen);
    cJSON_AddStringToObject(pRoot, "pCont", s);
wafwerar's avatar
wafwerar 已提交
83
    taosMemoryFree(s);
M
Minghao Li 已提交
84 85
    s = syncUtilprintBin2((char*)(pRpcMsg->pCont), pRpcMsg->contLen);
    cJSON_AddStringToObject(pRoot, "pCont2", s);
wafwerar's avatar
wafwerar 已提交
86
    taosMemoryFree(s);
M
Minghao Li 已提交
87

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

M
Minghao Li 已提交
99 100 101
  cJSON_AddNumberToObject(pRoot, "msgType", pRpcMsg->msgType);
  cJSON_AddNumberToObject(pRoot, "contLen", pRpcMsg->contLen);
  cJSON_AddNumberToObject(pRoot, "code", pRpcMsg->code);
M
Minghao Li 已提交
102
  // cJSON_AddNumberToObject(pRoot, "persist", pRpcMsg->persist);
M
Minghao Li 已提交
103

M
Minghao Li 已提交
104 105 106 107 108 109 110
  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "RpcMsg", pRoot);
  return pJson;
}

cJSON* syncRpcUnknownMsg2Json() {
  cJSON* pRoot = cJSON_CreateObject();
111
  cJSON_AddNumberToObject(pRoot, "msgType", TDMT_SYNC_UNKNOWN);
M
Minghao Li 已提交
112
  cJSON_AddStringToObject(pRoot, "data", "unknown message");
M
Minghao Li 已提交
113 114

  cJSON* pJson = cJSON_CreateObject();
M
Minghao Li 已提交
115
  cJSON_AddItemToObject(pJson, "SyncUnknown", pRoot);
M
Minghao Li 已提交
116 117 118
  return pJson;
}

M
Minghao Li 已提交
119 120 121 122 123 124 125
char* syncRpcMsg2Str(SRpcMsg* pRpcMsg) {
  cJSON* pJson = syncRpcMsg2Json(pRpcMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

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

void syncRpcMsgPrint2(char* s, SRpcMsg* pMsg) {
  char* serialized = syncRpcMsg2Str(pMsg);
  printf("syncRpcMsgPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
138
  taosMemoryFree(serialized);
M
Minghao Li 已提交
139 140 141 142 143
}

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

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

M
Minghao Li 已提交
153 154 155
// ---- message process SyncTimeout----
SyncTimeout* syncTimeoutBuild() {
  uint32_t     bytes = sizeof(SyncTimeout);
wafwerar's avatar
wafwerar 已提交
156
  SyncTimeout* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
157 158
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
159
  pMsg->msgType = TDMT_SYNC_TIMEOUT;
M
Minghao Li 已提交
160 161 162
  return pMsg;
}

M
Minghao Li 已提交
163 164
SyncTimeout* syncTimeoutBuild2(ESyncTimeoutType timeoutType, uint64_t logicClock, int32_t timerMS, int32_t vgId,
                               void* data) {
M
Minghao Li 已提交
165
  SyncTimeout* pMsg = syncTimeoutBuild();
M
Minghao Li 已提交
166
  pMsg->vgId = vgId;
M
Minghao Li 已提交
167 168 169 170 171 172 173
  pMsg->timeoutType = timeoutType;
  pMsg->logicClock = logicClock;
  pMsg->timerMS = timerMS;
  pMsg->data = data;
  return pMsg;
}

M
Minghao Li 已提交
174 175
void syncTimeoutDestroy(SyncTimeout* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
176
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189
  }
}

void syncTimeoutSerialize(const SyncTimeout* pMsg, char* buf, uint32_t bufLen) {
  assert(pMsg->bytes <= bufLen);
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncTimeoutDeserialize(const char* buf, uint32_t len, SyncTimeout* pMsg) {
  memcpy(pMsg, buf, len);
  assert(len == pMsg->bytes);
}

M
Minghao Li 已提交
190
char* syncTimeoutSerialize2(const SyncTimeout* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
191
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
192 193 194 195 196 197 198 199 200 201
  assert(buf != NULL);
  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 已提交
202
  SyncTimeout* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
203 204 205 206 207 208
  assert(pMsg != NULL);
  syncTimeoutDeserialize(buf, len, pMsg);
  assert(len == pMsg->bytes);
  return pMsg;
}

M
Minghao Li 已提交
209 210 211 212 213 214 215 216 217 218 219 220
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 已提交
221 222
SyncTimeout* syncTimeoutFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncTimeout* pMsg = syncTimeoutDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
223
  assert(pMsg != NULL);
M
Minghao Li 已提交
224 225 226
  return pMsg;
}

M
Minghao Li 已提交
227
cJSON* syncTimeout2Json(const SyncTimeout* pMsg) {
C
Cary Xu 已提交
228
  char   u64buf[128] = {0};
M
Minghao Li 已提交
229
  cJSON* pRoot = cJSON_CreateObject();
M
Minghao Li 已提交
230 231 232

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
233
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
234 235 236 237 238 239 240 241
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);
    cJSON_AddNumberToObject(pRoot, "timeoutType", pMsg->timeoutType);
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->logicClock);
    cJSON_AddStringToObject(pRoot, "logicClock", u64buf);
    cJSON_AddNumberToObject(pRoot, "timerMS", pMsg->timerMS);
    snprintf(u64buf, sizeof(u64buf), "%p", pMsg->data);
    cJSON_AddStringToObject(pRoot, "data", u64buf);
  }
M
Minghao Li 已提交
242 243 244 245 246 247

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

M
Minghao Li 已提交
248 249 250 251 252 253 254 255 256 257
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);
258
  printf("syncTimeoutPrint | len:%zu | %s \n", strlen(serialized), serialized);
M
Minghao Li 已提交
259
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
260
  taosMemoryFree(serialized);
M
Minghao Li 已提交
261 262 263 264 265 266
}

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

void syncTimeoutLog(const SyncTimeout* pMsg) {
  char* serialized = syncTimeout2Str(pMsg);
  sTrace("syncTimeoutLog | len:%lu | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
273
  taosMemoryFree(serialized);
M
Minghao Li 已提交
274 275 276 277 278
}

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

M
Minghao Li 已提交
282
// ---- message process SyncPing----
M
Minghao Li 已提交
283
SyncPing* syncPingBuild(uint32_t dataLen) {
M
Minghao Li 已提交
284
  uint32_t  bytes = sizeof(SyncPing) + dataLen;
wafwerar's avatar
wafwerar 已提交
285
  SyncPing* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
286 287
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
288
  pMsg->msgType = TDMT_SYNC_PING;
M
Minghao Li 已提交
289
  pMsg->dataLen = dataLen;
M
Minghao Li 已提交
290
  return pMsg;
M
Minghao Li 已提交
291 292
}

M
Minghao Li 已提交
293
SyncPing* syncPingBuild2(const SRaftId* srcId, const SRaftId* destId, int32_t vgId, const char* str) {
M
Minghao Li 已提交
294 295
  uint32_t  dataLen = strlen(str) + 1;
  SyncPing* pMsg = syncPingBuild(dataLen);
M
Minghao Li 已提交
296
  pMsg->vgId = vgId;
M
Minghao Li 已提交
297 298 299 300 301 302
  pMsg->srcId = *srcId;
  pMsg->destId = *destId;
  snprintf(pMsg->data, pMsg->dataLen, "%s", str);
  return pMsg;
}

M
Minghao Li 已提交
303 304
SyncPing* syncPingBuild3(const SRaftId* srcId, const SRaftId* destId, int32_t vgId) {
  SyncPing* pMsg = syncPingBuild2(srcId, destId, vgId, "ping");
M
Minghao Li 已提交
305 306 307
  return pMsg;
}

M
Minghao Li 已提交
308 309
void syncPingDestroy(SyncPing* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
310
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
311 312 313
  }
}

M
Minghao Li 已提交
314 315 316
void syncPingSerialize(const SyncPing* pMsg, char* buf, uint32_t bufLen) {
  assert(pMsg->bytes <= bufLen);
  memcpy(buf, pMsg, pMsg->bytes);
M
Minghao Li 已提交
317 318
}

M
Minghao Li 已提交
319 320 321
void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pMsg) {
  memcpy(pMsg, buf, len);
  assert(len == pMsg->bytes);
M
Minghao Li 已提交
322
  assert(pMsg->bytes == sizeof(SyncPing) + pMsg->dataLen);
M
Minghao Li 已提交
323 324
}

M
Minghao Li 已提交
325
char* syncPingSerialize2(const SyncPing* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
326
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
327 328 329 330 331 332 333 334 335 336
  assert(buf != NULL);
  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 已提交
337
  SyncPing* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
338 339 340 341 342 343
  assert(pMsg != NULL);
  syncPingDeserialize(buf, len, pMsg);
  assert(len == pMsg->bytes);
  return pMsg;
}

M
Minghao Li 已提交
344
int32_t syncPingSerialize3(const SyncPing* pMsg, char* buf, int32_t bufLen) {
H
Hongze Cheng 已提交
345 346
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
M
Minghao Li 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
  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 已提交
381
  tEncoderClear(&encoder);
M
Minghao Li 已提交
382 383 384 385
  return tlen;
}

SyncPing* syncPingDeserialize3(void* buf, int32_t bufLen) {
H
Hongze Cheng 已提交
386 387
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
M
Minghao Li 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
  if (tStartDecode(&decoder) < 0) {
    return NULL;
  }

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

  pMsg = taosMemoryMalloc(bytes);
  assert(pMsg != NULL);
  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 已提交
423
  uint32_t len;
M
Minghao Li 已提交
424
  char*    data = NULL;
H
Hongze Cheng 已提交
425
  if (tDecodeBinary(&decoder, (uint8_t**)(&data), &len) < 0) {
M
Minghao Li 已提交
426 427 428 429 430 431
    return NULL;
  }
  assert(len = pMsg->dataLen);
  memcpy(pMsg->data, data, len);

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
432
  tDecoderClear(&decoder);
M
Minghao Li 已提交
433 434 435
  return pMsg;
}

M
Minghao Li 已提交
436
void syncPing2RpcMsg(const SyncPing* pMsg, SRpcMsg* pRpcMsg) {
M
Minghao Li 已提交
437
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
M
Minghao Li 已提交
438 439
  pRpcMsg->msgType = pMsg->msgType;
  pRpcMsg->contLen = pMsg->bytes;
M
Minghao Li 已提交
440
  pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
M
Minghao Li 已提交
441
  syncPingSerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen);
M
Minghao Li 已提交
442 443
}

M
Minghao Li 已提交
444 445
void syncPingFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPing* pMsg) {
  syncPingDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
M
Minghao Li 已提交
446 447
}

M
Minghao Li 已提交
448 449
SyncPing* syncPingFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncPing* pMsg = syncPingDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
450
  assert(pMsg != NULL);
M
Minghao Li 已提交
451 452 453
  return pMsg;
}

M
Minghao Li 已提交
454
cJSON* syncPing2Json(const SyncPing* pMsg) {
455
  char   u64buf[128] = {0};
M
Minghao Li 已提交
456
  cJSON* pRoot = cJSON_CreateObject();
M
Minghao Li 已提交
457 458 459

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
460
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
461 462 463 464 465 466 467 468
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->srcId.addr);
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
469
      char     host[128] = {0};
M
Minghao Li 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482 483
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

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

M
Minghao Li 已提交
503 504 505 506 507
  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncPing", pRoot);
  return pJson;
}

M
Minghao Li 已提交
508 509 510 511 512
char* syncPing2Str(const SyncPing* pMsg) {
  cJSON* pJson = syncPing2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
M
Minghao Li 已提交
513 514
}

M
Minghao Li 已提交
515 516 517 518 519
// for debug ----------------------
void syncPingPrint(const SyncPing* pMsg) {
  char* serialized = syncPing2Str(pMsg);
  printf("syncPingPrint | len:%lu | %s \n", strlen(serialized), serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
520
  taosMemoryFree(serialized);
M
Minghao Li 已提交
521 522 523 524 525 526
}

void syncPingPrint2(char* s, const SyncPing* pMsg) {
  char* serialized = syncPing2Str(pMsg);
  printf("syncPingPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
527
  taosMemoryFree(serialized);
M
Minghao Li 已提交
528 529 530 531 532
}

void syncPingLog(const SyncPing* pMsg) {
  char* serialized = syncPing2Str(pMsg);
  sTrace("syncPingLog | len:%lu | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
533
  taosMemoryFree(serialized);
M
Minghao Li 已提交
534 535 536 537 538
}

void syncPingLog2(char* s, const SyncPing* pMsg) {
  char* serialized = syncPing2Str(pMsg);
  sTrace("syncPingLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
wafwerar's avatar
wafwerar 已提交
539
  taosMemoryFree(serialized);
M
Minghao Li 已提交
540 541
}

M
Minghao Li 已提交
542 543
// ---- message process SyncPingReply----
SyncPingReply* syncPingReplyBuild(uint32_t dataLen) {
M
Minghao Li 已提交
544
  uint32_t       bytes = sizeof(SyncPingReply) + dataLen;
wafwerar's avatar
wafwerar 已提交
545
  SyncPingReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
546 547
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
548
  pMsg->msgType = TDMT_SYNC_PING_REPLY;
M
Minghao Li 已提交
549
  pMsg->dataLen = dataLen;
M
Minghao Li 已提交
550
  return pMsg;
M
Minghao Li 已提交
551 552
}

M
Minghao Li 已提交
553
SyncPingReply* syncPingReplyBuild2(const SRaftId* srcId, const SRaftId* destId, int32_t vgId, const char* str) {
M
Minghao Li 已提交
554 555
  uint32_t       dataLen = strlen(str) + 1;
  SyncPingReply* pMsg = syncPingReplyBuild(dataLen);
M
Minghao Li 已提交
556
  pMsg->vgId = vgId;
M
Minghao Li 已提交
557 558 559 560 561 562
  pMsg->srcId = *srcId;
  pMsg->destId = *destId;
  snprintf(pMsg->data, pMsg->dataLen, "%s", str);
  return pMsg;
}

M
Minghao Li 已提交
563 564
SyncPingReply* syncPingReplyBuild3(const SRaftId* srcId, const SRaftId* destId, int32_t vgId) {
  SyncPingReply* pMsg = syncPingReplyBuild2(srcId, destId, vgId, "pang");
M
Minghao Li 已提交
565 566 567
  return pMsg;
}

M
Minghao Li 已提交
568 569
void syncPingReplyDestroy(SyncPingReply* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
570
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
571 572 573 574 575 576 577 578 579 580 581
  }
}

void syncPingReplySerialize(const SyncPingReply* pMsg, char* buf, uint32_t bufLen) {
  assert(pMsg->bytes <= bufLen);
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncPingReplyDeserialize(const char* buf, uint32_t len, SyncPingReply* pMsg) {
  memcpy(pMsg, buf, len);
  assert(len == pMsg->bytes);
M
Minghao Li 已提交
582
  assert(pMsg->bytes == sizeof(SyncPing) + pMsg->dataLen);
M
Minghao Li 已提交
583 584
}

M
Minghao Li 已提交
585
char* syncPingReplySerialize2(const SyncPingReply* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
586
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
587 588 589 590 591 592 593 594 595 596
  assert(buf != NULL);
  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 已提交
597
  SyncPingReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
598 599 600 601 602 603
  assert(pMsg != NULL);
  syncPingReplyDeserialize(buf, len, pMsg);
  assert(len == pMsg->bytes);
  return pMsg;
}

M
Minghao Li 已提交
604
int32_t syncPingReplySerialize3(const SyncPingReply* pMsg, char* buf, int32_t bufLen) {
H
Hongze Cheng 已提交
605 606
  SEncoder encoder = {0};
  tEncoderInit(&encoder, buf, bufLen);
M
Minghao Li 已提交
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
  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 已提交
641
  tEncoderClear(&encoder);
M
Minghao Li 已提交
642 643 644 645
  return tlen;
}

SyncPingReply* syncPingReplyDeserialize3(void* buf, int32_t bufLen) {
H
Hongze Cheng 已提交
646 647
  SDecoder decoder = {0};
  tDecoderInit(&decoder, buf, bufLen);
M
Minghao Li 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
  if (tStartDecode(&decoder) < 0) {
    return NULL;
  }

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

  pMsg = taosMemoryMalloc(bytes);
  assert(pMsg != NULL);
  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 已提交
683
  uint32_t len;
M
Minghao Li 已提交
684
  char*    data = NULL;
H
Hongze Cheng 已提交
685
  if (tDecodeBinary(&decoder, (uint8_t**)(&data), &len) < 0) {
M
Minghao Li 已提交
686 687 688 689 690 691
    return NULL;
  }
  assert(len = pMsg->dataLen);
  memcpy(pMsg->data, data, len);

  tEndDecode(&decoder);
H
Hongze Cheng 已提交
692
  tDecoderClear(&decoder);
M
Minghao Li 已提交
693 694 695
  return pMsg;
}

M
Minghao Li 已提交
696
void syncPingReply2RpcMsg(const SyncPingReply* pMsg, SRpcMsg* pRpcMsg) {
M
Minghao Li 已提交
697
  memset(pRpcMsg, 0, sizeof(*pRpcMsg));
M
Minghao Li 已提交
698 699 700 701 702 703 704 705 706 707
  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 已提交
708 709
SyncPingReply* syncPingReplyFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncPingReply* pMsg = syncPingReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
710
  assert(pMsg != NULL);
M
Minghao Li 已提交
711 712 713
  return pMsg;
}

M
Minghao Li 已提交
714
cJSON* syncPingReply2Json(const SyncPingReply* pMsg) {
715
  char   u64buf[128] = {0};
M
Minghao Li 已提交
716
  cJSON* pRoot = cJSON_CreateObject();
M
Minghao Li 已提交
717 718 719

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
720
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
721 722 723 724 725 726 727 728
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->srcId.addr);
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
729
      char     host[128] = {0};
M
Minghao Li 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742 743
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

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

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

M
Minghao Li 已提交
768 769 770 771 772
char* syncPingReply2Str(const SyncPingReply* pMsg) {
  cJSON* pJson = syncPingReply2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
M
Minghao Li 已提交
773 774
}

M
Minghao Li 已提交
775 776 777
// for debug ----------------------
void syncPingReplyPrint(const SyncPingReply* pMsg) {
  char* serialized = syncPingReply2Str(pMsg);
778
  printf("syncPingReplyPrint | len:%zu | %s \n", strlen(serialized), serialized);
M
Minghao Li 已提交
779
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
780
  taosMemoryFree(serialized);
M
Minghao Li 已提交
781 782 783 784
}

void syncPingReplyPrint2(char* s, const SyncPingReply* pMsg) {
  char* serialized = syncPingReply2Str(pMsg);
785
  printf("syncPingReplyPrint2 | len:%zu | %s | %s \n", strlen(serialized), s, serialized);
M
Minghao Li 已提交
786
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
787
  taosMemoryFree(serialized);
M
Minghao Li 已提交
788 789 790 791
}

void syncPingReplyLog(const SyncPingReply* pMsg) {
  char* serialized = syncPingReply2Str(pMsg);
792
  sTrace("syncPingReplyLog | len:%zu | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
793
  taosMemoryFree(serialized);
M
Minghao Li 已提交
794 795 796 797
}

void syncPingReplyLog2(char* s, const SyncPingReply* pMsg) {
  char* serialized = syncPingReply2Str(pMsg);
798
  sTrace("syncPingReplyLog2 | len:%zu | %s | %s", strlen(serialized), s, serialized);
wafwerar's avatar
wafwerar 已提交
799
  taosMemoryFree(serialized);
M
Minghao Li 已提交
800 801
}

M
Minghao Li 已提交
802 803
// ---- message process SyncClientRequest----
SyncClientRequest* syncClientRequestBuild(uint32_t dataLen) {
M
Minghao Li 已提交
804
  uint32_t           bytes = sizeof(SyncClientRequest) + dataLen;
wafwerar's avatar
wafwerar 已提交
805
  SyncClientRequest* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
806 807
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
808
  pMsg->msgType = TDMT_SYNC_CLIENT_REQUEST;
M
Minghao Li 已提交
809 810 811 812 813 814
  pMsg->seqNum = 0;
  pMsg->isWeak = false;
  pMsg->dataLen = dataLen;
  return pMsg;
}

M
Minghao Li 已提交
815
// step 1. original SRpcMsg => SyncClientRequest, add seqNum, isWeak
M
Minghao Li 已提交
816
SyncClientRequest* syncClientRequestBuild2(const SRpcMsg* pOriginalRpcMsg, uint64_t seqNum, bool isWeak, int32_t vgId) {
M
Minghao Li 已提交
817
  SyncClientRequest* pMsg = syncClientRequestBuild(pOriginalRpcMsg->contLen);
M
Minghao Li 已提交
818
  pMsg->vgId = vgId;
M
Minghao Li 已提交
819 820 821 822 823 824 825
  pMsg->originalRpcType = pOriginalRpcMsg->msgType;
  pMsg->seqNum = seqNum;
  pMsg->isWeak = isWeak;
  memcpy(pMsg->data, pOriginalRpcMsg->pCont, pOriginalRpcMsg->contLen);
  return pMsg;
}

M
Minghao Li 已提交
826 827
void syncClientRequestDestroy(SyncClientRequest* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
828
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
829 830 831 832 833 834 835 836 837 838 839 840 841
  }
}

void syncClientRequestSerialize(const SyncClientRequest* pMsg, char* buf, uint32_t bufLen) {
  assert(pMsg->bytes <= bufLen);
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncClientRequestDeserialize(const char* buf, uint32_t len, SyncClientRequest* pMsg) {
  memcpy(pMsg, buf, len);
  assert(len == pMsg->bytes);
}

M
Minghao Li 已提交
842
char* syncClientRequestSerialize2(const SyncClientRequest* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
843
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
844 845 846 847 848 849 850 851 852 853
  assert(buf != NULL);
  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 已提交
854
  SyncClientRequest* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
855 856 857 858 859 860
  assert(pMsg != NULL);
  syncClientRequestDeserialize(buf, len, pMsg);
  assert(len == pMsg->bytes);
  return pMsg;
}

M
Minghao Li 已提交
861
// step 2. SyncClientRequest => RpcMsg, to queue
M
Minghao Li 已提交
862 863 864 865 866 867 868 869 870 871 872 873
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 已提交
874
// step 3. RpcMsg => SyncClientRequest, from queue
M
Minghao Li 已提交
875 876
SyncClientRequest* syncClientRequestFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncClientRequest* pMsg = syncClientRequestDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
877
  assert(pMsg != NULL);
M
Minghao Li 已提交
878 879 880
  return pMsg;
}

M
Minghao Li 已提交
881
cJSON* syncClientRequest2Json(const SyncClientRequest* pMsg) {
882
  char   u64buf[128] = {0};
M
Minghao Li 已提交
883
  cJSON* pRoot = cJSON_CreateObject();
M
Minghao Li 已提交
884 885 886

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
887
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
888 889 890 891 892 893 894 895 896 897
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);
    cJSON_AddNumberToObject(pRoot, "originalRpcType", pMsg->originalRpcType);
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->seqNum);
    cJSON_AddStringToObject(pRoot, "seqNum", u64buf);
    cJSON_AddNumberToObject(pRoot, "isWeak", pMsg->isWeak);
    cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen);

    char* s;
    s = syncUtilprintBin((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data", s);
wafwerar's avatar
wafwerar 已提交
898
    taosMemoryFree(s);
M
Minghao Li 已提交
899 900
    s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data2", s);
wafwerar's avatar
wafwerar 已提交
901
    taosMemoryFree(s);
M
Minghao Li 已提交
902
  }
M
Minghao Li 已提交
903

M
Minghao Li 已提交
904 905 906 907 908
  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncClientRequest", pRoot);
  return pJson;
}

M
Minghao Li 已提交
909 910 911 912 913 914 915 916 917 918 919 920
char* syncClientRequest2Str(const SyncClientRequest* pMsg) {
  cJSON* pJson = syncClientRequest2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

// for debug ----------------------
void syncClientRequestPrint(const SyncClientRequest* pMsg) {
  char* serialized = syncClientRequest2Str(pMsg);
  printf("syncClientRequestPrint | len:%lu | %s \n", strlen(serialized), serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
921
  taosMemoryFree(serialized);
M
Minghao Li 已提交
922 923 924 925 926 927
}

void syncClientRequestPrint2(char* s, const SyncClientRequest* pMsg) {
  char* serialized = syncClientRequest2Str(pMsg);
  printf("syncClientRequestPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
928
  taosMemoryFree(serialized);
M
Minghao Li 已提交
929 930 931 932 933
}

void syncClientRequestLog(const SyncClientRequest* pMsg) {
  char* serialized = syncClientRequest2Str(pMsg);
  sTrace("syncClientRequestLog | len:%lu | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
934
  taosMemoryFree(serialized);
M
Minghao Li 已提交
935 936 937 938 939
}

void syncClientRequestLog2(char* s, const SyncClientRequest* pMsg) {
  char* serialized = syncClientRequest2Str(pMsg);
  sTrace("syncClientRequestLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
wafwerar's avatar
wafwerar 已提交
940
  taosMemoryFree(serialized);
M
Minghao Li 已提交
941 942
}

M
Minghao Li 已提交
943
// ---- message process SyncRequestVote----
M
Minghao Li 已提交
944
SyncRequestVote* syncRequestVoteBuild(int32_t vgId) {
M
Minghao Li 已提交
945
  uint32_t         bytes = sizeof(SyncRequestVote);
wafwerar's avatar
wafwerar 已提交
946
  SyncRequestVote* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
947 948
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
M
Minghao Li 已提交
949
  pMsg->vgId = vgId;
950
  pMsg->msgType = TDMT_SYNC_REQUEST_VOTE;
M
Minghao Li 已提交
951
  return pMsg;
M
Minghao Li 已提交
952 953
}

M
Minghao Li 已提交
954 955
void syncRequestVoteDestroy(SyncRequestVote* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
956
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
957 958
  }
}
M
Minghao Li 已提交
959

M
Minghao Li 已提交
960 961 962 963
void syncRequestVoteSerialize(const SyncRequestVote* pMsg, char* buf, uint32_t bufLen) {
  assert(pMsg->bytes <= bufLen);
  memcpy(buf, pMsg, pMsg->bytes);
}
M
Minghao Li 已提交
964

M
Minghao Li 已提交
965 966 967 968
void syncRequestVoteDeserialize(const char* buf, uint32_t len, SyncRequestVote* pMsg) {
  memcpy(pMsg, buf, len);
  assert(len == pMsg->bytes);
}
M
Minghao Li 已提交
969

M
Minghao Li 已提交
970
char* syncRequestVoteSerialize2(const SyncRequestVote* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
971
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
972 973 974 975 976 977 978 979 980 981
  assert(buf != NULL);
  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 已提交
982
  SyncRequestVote* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
983 984 985 986 987 988
  assert(pMsg != NULL);
  syncRequestVoteDeserialize(buf, len, pMsg);
  assert(len == pMsg->bytes);
  return pMsg;
}

M
Minghao Li 已提交
989 990 991 992 993 994 995
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 已提交
996

M
Minghao Li 已提交
997 998 999
void syncRequestVoteFromRpcMsg(const SRpcMsg* pRpcMsg, SyncRequestVote* pMsg) {
  syncRequestVoteDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
}
M
Minghao Li 已提交
1000

M
Minghao Li 已提交
1001 1002
SyncRequestVote* syncRequestVoteFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncRequestVote* pMsg = syncRequestVoteDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
1003
  assert(pMsg != NULL);
M
Minghao Li 已提交
1004
  return pMsg;
M
Minghao Li 已提交
1005 1006
}

M
Minghao Li 已提交
1007
cJSON* syncRequestVote2Json(const SyncRequestVote* pMsg) {
1008
  char   u64buf[128] = {0};
M
Minghao Li 已提交
1009 1010
  cJSON* pRoot = cJSON_CreateObject();

M
Minghao Li 已提交
1011 1012
  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
1013
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
1014 1015 1016 1017 1018 1019 1020 1021
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->srcId.addr);
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
1022
      char     host[128] = {0};
M
Minghao Li 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
      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;
1036
      char     host[128] = {0};
M
Minghao Li 已提交
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
    cJSON_AddItemToObject(pRoot, "destId", pDestId);

    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->term);
    cJSON_AddStringToObject(pRoot, "term", u64buf);
M
Minghao Li 已提交
1047
    snprintf(u64buf, sizeof(u64buf), "%ld", pMsg->lastLogIndex);
M
Minghao Li 已提交
1048 1049 1050 1051
    cJSON_AddStringToObject(pRoot, "lastLogIndex", u64buf);
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->lastLogTerm);
    cJSON_AddStringToObject(pRoot, "lastLogTerm", u64buf);
  }
M
Minghao Li 已提交
1052 1053 1054 1055

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

M
Minghao Li 已提交
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
char* syncRequestVote2Str(const SyncRequestVote* pMsg) {
  cJSON* pJson = syncRequestVote2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

// for debug ----------------------
void syncRequestVotePrint(const SyncRequestVote* pMsg) {
  char* serialized = syncRequestVote2Str(pMsg);
  printf("syncRequestVotePrint | len:%lu | %s \n", strlen(serialized), serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1070
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1071 1072 1073 1074 1075 1076
}

void syncRequestVotePrint2(char* s, const SyncRequestVote* pMsg) {
  char* serialized = syncRequestVote2Str(pMsg);
  printf("syncRequestVotePrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1077
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1078 1079 1080 1081 1082
}

void syncRequestVoteLog(const SyncRequestVote* pMsg) {
  char* serialized = syncRequestVote2Str(pMsg);
  sTrace("syncRequestVoteLog | len:%lu | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
1083
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1084 1085 1086 1087 1088
}

void syncRequestVoteLog2(char* s, const SyncRequestVote* pMsg) {
  char* serialized = syncRequestVote2Str(pMsg);
  sTrace("syncRequestVoteLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
wafwerar's avatar
wafwerar 已提交
1089
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1090 1091
}

M
Minghao Li 已提交
1092
// ---- message process SyncRequestVoteReply----
M
Minghao Li 已提交
1093
SyncRequestVoteReply* syncRequestVoteReplyBuild(int32_t vgId) {
M
Minghao Li 已提交
1094
  uint32_t              bytes = sizeof(SyncRequestVoteReply);
wafwerar's avatar
wafwerar 已提交
1095
  SyncRequestVoteReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1096 1097
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
M
Minghao Li 已提交
1098
  pMsg->vgId = vgId;
1099
  pMsg->msgType = TDMT_SYNC_REQUEST_VOTE_REPLY;
M
Minghao Li 已提交
1100
  return pMsg;
M
Minghao Li 已提交
1101 1102 1103 1104
}

void syncRequestVoteReplyDestroy(SyncRequestVoteReply* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
1105
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
  }
}

void syncRequestVoteReplySerialize(const SyncRequestVoteReply* pMsg, char* buf, uint32_t bufLen) {
  assert(pMsg->bytes <= bufLen);
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncRequestVoteReplyDeserialize(const char* buf, uint32_t len, SyncRequestVoteReply* pMsg) {
  memcpy(pMsg, buf, len);
  assert(len == pMsg->bytes);
}

M
Minghao Li 已提交
1119
char* syncRequestVoteReplySerialize2(const SyncRequestVoteReply* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
1120
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
  assert(buf != NULL);
  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 已提交
1131
  SyncRequestVoteReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1132 1133 1134 1135 1136 1137
  assert(pMsg != NULL);
  syncRequestVoteReplyDeserialize(buf, len, pMsg);
  assert(len == pMsg->bytes);
  return pMsg;
}

M
Minghao Li 已提交
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
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 已提交
1150 1151
SyncRequestVoteReply* syncRequestVoteReplyFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncRequestVoteReply* pMsg = syncRequestVoteReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
1152
  assert(pMsg != NULL);
M
Minghao Li 已提交
1153 1154 1155
  return pMsg;
}

M
Minghao Li 已提交
1156
cJSON* syncRequestVoteReply2Json(const SyncRequestVoteReply* pMsg) {
1157
  char   u64buf[128] = {0};
M
Minghao Li 已提交
1158 1159
  cJSON* pRoot = cJSON_CreateObject();

M
Minghao Li 已提交
1160 1161
  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
1162
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
1163 1164 1165 1166 1167 1168 1169 1170
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->srcId.addr);
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
1171
      char     host[128] = {0};
M
Minghao Li 已提交
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
      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;
1185
      char     host[128] = {0};
M
Minghao Li 已提交
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
    cJSON_AddItemToObject(pRoot, "destId", pDestId);

    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->term);
    cJSON_AddStringToObject(pRoot, "term", u64buf);
    cJSON_AddNumberToObject(pRoot, "vote_granted", pMsg->voteGranted);
  }
M
Minghao Li 已提交
1198 1199 1200 1201

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

M
Minghao Li 已提交
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
char* syncRequestVoteReply2Str(const SyncRequestVoteReply* pMsg) {
  cJSON* pJson = syncRequestVoteReply2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

// for debug ----------------------
void syncRequestVoteReplyPrint(const SyncRequestVoteReply* pMsg) {
  char* serialized = syncRequestVoteReply2Str(pMsg);
  printf("syncRequestVoteReplyPrint | len:%lu | %s \n", strlen(serialized), serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1216
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1217 1218 1219 1220 1221 1222
}

void syncRequestVoteReplyPrint2(char* s, const SyncRequestVoteReply* pMsg) {
  char* serialized = syncRequestVoteReply2Str(pMsg);
  printf("syncRequestVoteReplyPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1223
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1224 1225 1226 1227 1228
}

void syncRequestVoteReplyLog(const SyncRequestVoteReply* pMsg) {
  char* serialized = syncRequestVoteReply2Str(pMsg);
  sTrace("syncRequestVoteReplyLog | len:%lu | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
1229
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1230 1231 1232 1233 1234
}

void syncRequestVoteReplyLog2(char* s, const SyncRequestVoteReply* pMsg) {
  char* serialized = syncRequestVoteReply2Str(pMsg);
  sTrace("syncRequestVoteReplyLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
wafwerar's avatar
wafwerar 已提交
1235
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1236 1237
}

M
Minghao Li 已提交
1238
// ---- message process SyncAppendEntries----
M
Minghao Li 已提交
1239
SyncAppendEntries* syncAppendEntriesBuild(uint32_t dataLen, int32_t vgId) {
M
Minghao Li 已提交
1240
  uint32_t           bytes = sizeof(SyncAppendEntries) + dataLen;
wafwerar's avatar
wafwerar 已提交
1241
  SyncAppendEntries* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1242 1243
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
M
Minghao Li 已提交
1244
  pMsg->vgId = vgId;
1245
  pMsg->msgType = TDMT_SYNC_APPEND_ENTRIES;
M
Minghao Li 已提交
1246
  pMsg->dataLen = dataLen;
M
Minghao Li 已提交
1247
  return pMsg;
M
Minghao Li 已提交
1248 1249 1250 1251
}

void syncAppendEntriesDestroy(SyncAppendEntries* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
1252
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
  }
}

void syncAppendEntriesSerialize(const SyncAppendEntries* pMsg, char* buf, uint32_t bufLen) {
  assert(pMsg->bytes <= bufLen);
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncAppendEntriesDeserialize(const char* buf, uint32_t len, SyncAppendEntries* pMsg) {
  memcpy(pMsg, buf, len);
  assert(len == pMsg->bytes);
M
Minghao Li 已提交
1264 1265 1266 1267
  assert(pMsg->bytes == sizeof(SyncAppendEntries) + pMsg->dataLen);
}

char* syncAppendEntriesSerialize2(const SyncAppendEntries* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
1268
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
  assert(buf != NULL);
  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 已提交
1279
  SyncAppendEntries* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1280 1281 1282 1283
  assert(pMsg != NULL);
  syncAppendEntriesDeserialize(buf, len, pMsg);
  assert(len == pMsg->bytes);
  return pMsg;
M
Minghao Li 已提交
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
}

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 已提交
1298 1299
SyncAppendEntries* syncAppendEntriesFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncAppendEntries* pMsg = syncAppendEntriesDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
1300
  assert(pMsg != NULL);
M
Minghao Li 已提交
1301 1302 1303
  return pMsg;
}

M
Minghao Li 已提交
1304
cJSON* syncAppendEntries2Json(const SyncAppendEntries* pMsg) {
1305
  char   u64buf[128] = {0};
M
Minghao Li 已提交
1306 1307
  cJSON* pRoot = cJSON_CreateObject();

M
Minghao Li 已提交
1308 1309
  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
1310
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
1311 1312 1313 1314 1315 1316 1317 1318
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->srcId.addr);
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
1319
      char     host[128] = {0};
M
Minghao Li 已提交
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

    cJSON* pDestId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->destId.addr);
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
1334
      char     host[128] = {0};
M
Minghao Li 已提交
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId);
    cJSON_AddItemToObject(pRoot, "destId", pDestId);

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

1346 1347 1348
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->privateTerm);
    cJSON_AddStringToObject(pRoot, "privateTerm", u64buf);

M
Minghao Li 已提交
1349 1350
    snprintf(u64buf, sizeof(u64buf), "%ld", pMsg->prevLogIndex);
    cJSON_AddStringToObject(pRoot, "prevLogIndex", u64buf);
M
Minghao Li 已提交
1351 1352 1353 1354

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

M
Minghao Li 已提交
1355 1356
    snprintf(u64buf, sizeof(u64buf), "%ld", pMsg->commitIndex);
    cJSON_AddStringToObject(pRoot, "commitIndex", u64buf);
M
Minghao Li 已提交
1357 1358 1359 1360 1361

    cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen);
    char* s;
    s = syncUtilprintBin((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data", s);
wafwerar's avatar
wafwerar 已提交
1362
    taosMemoryFree(s);
M
Minghao Li 已提交
1363 1364
    s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen);
    cJSON_AddStringToObject(pRoot, "data2", s);
wafwerar's avatar
wafwerar 已提交
1365
    taosMemoryFree(s);
M
Minghao Li 已提交
1366
  }
M
Minghao Li 已提交
1367 1368 1369 1370 1371 1372

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

M
Minghao Li 已提交
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
char* syncAppendEntries2Str(const SyncAppendEntries* pMsg) {
  cJSON* pJson = syncAppendEntries2Json(pMsg);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

// for debug ----------------------
void syncAppendEntriesPrint(const SyncAppendEntries* pMsg) {
  char* serialized = syncAppendEntries2Str(pMsg);
  printf("syncAppendEntriesPrint | len:%lu | %s \n", strlen(serialized), serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1385
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1386 1387 1388 1389 1390 1391
}

void syncAppendEntriesPrint2(char* s, const SyncAppendEntries* pMsg) {
  char* serialized = syncAppendEntries2Str(pMsg);
  printf("syncAppendEntriesPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1392
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1393 1394 1395 1396 1397
}

void syncAppendEntriesLog(const SyncAppendEntries* pMsg) {
  char* serialized = syncAppendEntries2Str(pMsg);
  sTrace("syncAppendEntriesLog | len:%lu | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
1398
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1399 1400 1401 1402 1403
}

void syncAppendEntriesLog2(char* s, const SyncAppendEntries* pMsg) {
  char* serialized = syncAppendEntries2Str(pMsg);
  sTrace("syncAppendEntriesLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
wafwerar's avatar
wafwerar 已提交
1404
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1405 1406
}

M
Minghao Li 已提交
1407
// ---- message process SyncAppendEntriesReply----
M
Minghao Li 已提交
1408
SyncAppendEntriesReply* syncAppendEntriesReplyBuild(int32_t vgId) {
M
Minghao Li 已提交
1409
  uint32_t                bytes = sizeof(SyncAppendEntriesReply);
wafwerar's avatar
wafwerar 已提交
1410
  SyncAppendEntriesReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1411 1412
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
M
Minghao Li 已提交
1413
  pMsg->vgId = vgId;
1414
  pMsg->msgType = TDMT_SYNC_APPEND_ENTRIES_REPLY;
M
Minghao Li 已提交
1415
  return pMsg;
M
Minghao Li 已提交
1416 1417 1418 1419
}

void syncAppendEntriesReplyDestroy(SyncAppendEntriesReply* pMsg) {
  if (pMsg != NULL) {
wafwerar's avatar
wafwerar 已提交
1420
    taosMemoryFree(pMsg);
M
Minghao Li 已提交
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
  }
}

void syncAppendEntriesReplySerialize(const SyncAppendEntriesReply* pMsg, char* buf, uint32_t bufLen) {
  assert(pMsg->bytes <= bufLen);
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncAppendEntriesReplyDeserialize(const char* buf, uint32_t len, SyncAppendEntriesReply* pMsg) {
  memcpy(pMsg, buf, len);
  assert(len == pMsg->bytes);
}

M
Minghao Li 已提交
1434
char* syncAppendEntriesReplySerialize2(const SyncAppendEntriesReply* pMsg, uint32_t* len) {
wafwerar's avatar
wafwerar 已提交
1435
  char* buf = taosMemoryMalloc(pMsg->bytes);
M
Minghao Li 已提交
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
  assert(buf != NULL);
  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 已提交
1446
  SyncAppendEntriesReply* pMsg = taosMemoryMalloc(bytes);
M
Minghao Li 已提交
1447 1448 1449 1450 1451 1452
  assert(pMsg != NULL);
  syncAppendEntriesReplyDeserialize(buf, len, pMsg);
  assert(len == pMsg->bytes);
  return pMsg;
}

M
Minghao Li 已提交
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
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 已提交
1465 1466
SyncAppendEntriesReply* syncAppendEntriesReplyFromRpcMsg2(const SRpcMsg* pRpcMsg) {
  SyncAppendEntriesReply* pMsg = syncAppendEntriesReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
1467
  assert(pMsg != NULL);
M
Minghao Li 已提交
1468 1469 1470
  return pMsg;
}

M
Minghao Li 已提交
1471
cJSON* syncAppendEntriesReply2Json(const SyncAppendEntriesReply* pMsg) {
1472
  char   u64buf[128] = {0};
M
Minghao Li 已提交
1473
  cJSON* pRoot = cJSON_CreateObject();
M
Minghao Li 已提交
1474 1475 1476

  if (pMsg != NULL) {
    cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes);
M
Minghao Li 已提交
1477
    cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId);
M
Minghao Li 已提交
1478 1479 1480 1481 1482 1483 1484 1485
    cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType);

    cJSON* pSrcId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->srcId.addr);
    cJSON_AddStringToObject(pSrcId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->srcId.addr;
      cJSON*   pTmp = pSrcId;
1486
      char     host[128] = {0};
M
Minghao Li 已提交
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
      uint16_t port;
      syncUtilU642Addr(u64, host, sizeof(host), &port);
      cJSON_AddStringToObject(pTmp, "addr_host", host);
      cJSON_AddNumberToObject(pTmp, "addr_port", port);
    }
    cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId);
    cJSON_AddItemToObject(pRoot, "srcId", pSrcId);

    cJSON* pDestId = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->destId.addr);
    cJSON_AddStringToObject(pDestId, "addr", u64buf);
    {
      uint64_t u64 = pMsg->destId.addr;
      cJSON*   pTmp = pDestId;
1501
      char     host[128] = {0};
M
Minghao Li 已提交
1502 1503 1504 1505 1506 1507 1508 1509
      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);

1510 1511 1512
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->privateTerm);
    cJSON_AddStringToObject(pRoot, "privateTerm", u64buf);

M
Minghao Li 已提交
1513 1514 1515
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->term);
    cJSON_AddStringToObject(pRoot, "term", u64buf);
    cJSON_AddNumberToObject(pRoot, "success", pMsg->success);
M
Minghao Li 已提交
1516
    snprintf(u64buf, sizeof(u64buf), "%ld", pMsg->matchIndex);
M
Minghao Li 已提交
1517
    cJSON_AddStringToObject(pRoot, "matchIndex", u64buf);
M
Minghao Li 已提交
1518 1519 1520 1521 1522
  }

  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SyncAppendEntriesReply", pRoot);
  return pJson;
M
Minghao Li 已提交
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
}

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

// for debug ----------------------
void syncAppendEntriesReplyPrint(const SyncAppendEntriesReply* pMsg) {
  char* serialized = syncAppendEntriesReply2Str(pMsg);
  printf("syncAppendEntriesReplyPrint | len:%lu | %s \n", strlen(serialized), serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1537
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1538 1539 1540 1541 1542 1543
}

void syncAppendEntriesReplyPrint2(char* s, const SyncAppendEntriesReply* pMsg) {
  char* serialized = syncAppendEntriesReply2Str(pMsg);
  printf("syncAppendEntriesReplyPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
1544
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1545 1546 1547 1548 1549
}

void syncAppendEntriesReplyLog(const SyncAppendEntriesReply* pMsg) {
  char* serialized = syncAppendEntriesReply2Str(pMsg);
  sTrace("syncAppendEntriesReplyLog | len:%lu | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
1550
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1551 1552 1553 1554 1555
}

void syncAppendEntriesReplyLog2(char* s, const SyncAppendEntriesReply* pMsg) {
  char* serialized = syncAppendEntriesReply2Str(pMsg);
  sTrace("syncAppendEntriesReplyLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
wafwerar's avatar
wafwerar 已提交
1556
  taosMemoryFree(serialized);
M
Minghao Li 已提交
1557
}
M
Minghao Li 已提交
1558 1559

// ---- message process SyncApplyMsg----
M
Minghao Li 已提交
1560 1561 1562 1563 1564
SyncApplyMsg* syncApplyMsgBuild(uint32_t dataLen) {
  uint32_t      bytes = sizeof(SyncApplyMsg) + dataLen;
  SyncApplyMsg* pMsg = taosMemoryMalloc(bytes);
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
1565
  pMsg->msgType = TDMT_SYNC_APPLY_MSG;
M
Minghao Li 已提交
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
  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) {
  assert(pMsg->bytes <= bufLen);
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncApplyMsgDeserialize(const char* buf, uint32_t len, SyncApplyMsg* pMsg) {
  memcpy(pMsg, buf, len);
  assert(len == pMsg->bytes);
}

char* syncApplyMsgSerialize2(const SyncApplyMsg* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
  assert(buf != NULL);
  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);
  assert(pMsg != NULL);
  syncApplyMsgDeserialize(buf, len, pMsg);
  assert(len == pMsg->bytes);
  return pMsg;
}
M
Minghao Li 已提交
1613 1614

// SyncApplyMsg to SRpcMsg, put it into ApplyQ
M
Minghao Li 已提交
1615 1616 1617 1618 1619 1620 1621
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 已提交
1622 1623

// get SRpcMsg from ApplyQ, to SyncApplyMsg
M
Minghao Li 已提交
1624 1625 1626 1627 1628 1629 1630 1631
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 已提交
1632 1633

// SyncApplyMsg to OriginalRpcMsg
M
Minghao Li 已提交
1634 1635 1636 1637 1638 1639 1640 1641 1642
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) {
1643
  char   u64buf[128] = {0};
M
Minghao Li 已提交
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
  cJSON* pRoot = cJSON_CreateObject();

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

    snprintf(u64buf, sizeof(u64buf), "%ld", pMsg->fsmMeta.index);
    cJSON_AddStringToObject(pRoot, "fsmMeta.index", u64buf);
    cJSON_AddNumberToObject(pRoot, "fsmMeta.isWeak", pMsg->fsmMeta.isWeak);
    cJSON_AddNumberToObject(pRoot, "fsmMeta.code", pMsg->fsmMeta.code);
    cJSON_AddNumberToObject(pRoot, "fsmMeta.state", pMsg->fsmMeta.state);
    cJSON_AddStringToObject(pRoot, "fsmMeta.state.str", syncUtilState2String(pMsg->fsmMeta.state));
    snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->fsmMeta.seqNum);
    cJSON_AddStringToObject(pRoot, "fsmMeta.seqNum", u64buf);

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

M
Minghao Li 已提交
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
  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 已提交
1682 1683

// for debug ----------------------
M
Minghao Li 已提交
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
void syncApplyMsgPrint(const SyncApplyMsg* pMsg) {
  char* serialized = syncApplyMsg2Str(pMsg);
  printf("syncApplyMsgPrint | len:%lu | %s \n", strlen(serialized), serialized);
  fflush(NULL);
  taosMemoryFree(serialized);
}

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

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

void syncApplyMsgLog2(char* s, const SyncApplyMsg* pMsg) {
  char* serialized = syncApplyMsg2Str(pMsg);
  sTrace("syncApplyMsgLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
  taosMemoryFree(serialized);
}
1709 1710 1711 1712 1713 1714 1715 1716

// ---------------------------------------------
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;
1717
  pMsg->msgType = TDMT_SYNC_SNAPSHOT_SEND;
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 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 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
  pMsg->dataLen = dataLen;
  return pMsg;
}

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

void syncSnapshotSendSerialize(const SyncSnapshotSend* pMsg, char* buf, uint32_t bufLen) {
  assert(pMsg->bytes <= bufLen);
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncSnapshotSendDeserialize(const char* buf, uint32_t len, SyncSnapshotSend* pMsg) {
  memcpy(pMsg, buf, len);
  assert(len == pMsg->bytes);
  assert(pMsg->bytes == sizeof(SyncSnapshotSend) + pMsg->dataLen);
}

char* syncSnapshotSendSerialize2(const SyncSnapshotSend* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
  assert(buf != NULL);
  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);
  assert(pMsg != NULL);
  syncSnapshotSendDeserialize(buf, len, pMsg);
  assert(len == pMsg->bytes);
  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);
  assert(pMsg != NULL);
  return pMsg;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ---------------------------------------------
M
Minghao Li 已提交
1876
SyncSnapshotRsp* syncSnapshotRspBuild(int32_t vgId) {
1877 1878 1879 1880 1881
  uint32_t         bytes = sizeof(SyncSnapshotRsp);
  SyncSnapshotRsp* pMsg = taosMemoryMalloc(bytes);
  memset(pMsg, 0, bytes);
  pMsg->bytes = bytes;
  pMsg->vgId = vgId;
1882
  pMsg->msgType = TDMT_SYNC_SNAPSHOT_RSP;
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027
  return pMsg;
}

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

void syncSnapshotRspSerialize(const SyncSnapshotRsp* pMsg, char* buf, uint32_t bufLen) {
  assert(pMsg->bytes <= bufLen);
  memcpy(buf, pMsg, pMsg->bytes);
}

void syncSnapshotRspDeserialize(const char* buf, uint32_t len, SyncSnapshotRsp* pMsg) {
  memcpy(pMsg, buf, len);
  assert(len == pMsg->bytes);
}

char* syncSnapshotRspSerialize2(const SyncSnapshotRsp* pMsg, uint32_t* len) {
  char* buf = taosMemoryMalloc(pMsg->bytes);
  assert(buf != NULL);
  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);
  assert(pMsg != NULL);
  syncSnapshotRspDeserialize(buf, len, pMsg);
  assert(len == pMsg->bytes);
  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);
  assert(pMsg != NULL);
  return pMsg;
}

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

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

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

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

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

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

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

    cJSON_AddNumberToObject(pRoot, "ack", pMsg->ack);
  }

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

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

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

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

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

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