syncSnapshot.c 19.8 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 "syncSnapshot.h"
17
#include "syncIndexMgr.h"
M
Minghao Li 已提交
18
#include "syncRaftLog.h"
M
Minghao Li 已提交
19 20
#include "syncRaftStore.h"
#include "syncUtil.h"
M
Minghao Li 已提交
21
#include "wal.h"
M
Minghao Li 已提交
22

23
// static void snapshotSenderDoStart(SSyncSnapshotSender *pSender);
M
Minghao Li 已提交
24
static void snapshotReceiverDoStart(SSyncSnapshotReceiver *pReceiver);
M
Minghao Li 已提交
25

M
Minghao Li 已提交
26
SSyncSnapshotSender *snapshotSenderCreate(SSyncNode *pSyncNode, int32_t replicaIndex) {
M
Minghao Li 已提交
27 28
  bool condition = (pSyncNode->pFsm->FpSnapshotStartRead != NULL) && (pSyncNode->pFsm->FpSnapshotStopRead != NULL) &&
                   (pSyncNode->pFsm->FpSnapshotDoRead != NULL);
M
Minghao Li 已提交
29

M
Minghao Li 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  SSyncSnapshotSender *pSender = NULL;
  if (condition) {
    pSender = taosMemoryMalloc(sizeof(SSyncSnapshotSender));
    ASSERT(pSender != NULL);
    memset(pSender, 0, sizeof(*pSender));

    pSender->start = false;
    pSender->seq = SYNC_SNAPSHOT_SEQ_INVALID;
    pSender->ack = SYNC_SNAPSHOT_SEQ_INVALID;
    pSender->pReader = NULL;
    pSender->pCurrentBlock = NULL;
    pSender->blockLen = 0;
    pSender->sendingMS = SYNC_SNAPSHOT_RETRY_MS;
    pSender->pSyncNode = pSyncNode;
    pSender->replicaIndex = replicaIndex;
    pSender->term = pSyncNode->pRaftStore->currentTerm;
M
Minghao Li 已提交
46
    pSender->privateTerm = taosGetTimestampMs() + 100;
M
Minghao Li 已提交
47 48
    pSender->pSyncNode->pFsm->FpGetSnapshot(pSender->pSyncNode->pFsm, &(pSender->snapshot));

M
Minghao Li 已提交
49
    pSender->finish = false;
M
Minghao Li 已提交
50 51 52
  } else {
    sInfo("snapshotSenderCreate cannot create sender");
  }
M
Minghao Li 已提交
53

M
Minghao Li 已提交
54 55
  return pSender;
}
M
Minghao Li 已提交
56

M
Minghao Li 已提交
57 58 59 60 61
void snapshotSenderDestroy(SSyncSnapshotSender *pSender) {
  if (pSender != NULL) {
    taosMemoryFree(pSender);
  }
}
M
Minghao Li 已提交
62

M
Minghao Li 已提交
63
// begin send snapshot (current term, seq begin)
64
void snapshotSenderDoStart(SSyncSnapshotSender *pSender) {
M
Minghao Li 已提交
65 66 67
  // when start, increase term
  ++(pSender->privateTerm);

M
Minghao Li 已提交
68 69 70
  pSender->term = pSender->pSyncNode->pRaftStore->currentTerm;
  pSender->seq = SYNC_SNAPSHOT_SEQ_BEGIN;
  pSender->ack = SYNC_SNAPSHOT_SEQ_INVALID;
M
Minghao Li 已提交
71

M
Minghao Li 已提交
72 73
  // open snapshot reader
  ASSERT(pSender->pReader == NULL);
M
Minghao Li 已提交
74 75 76
  int32_t ret = pSender->pSyncNode->pFsm->FpSnapshotStartRead(pSender->pSyncNode->pFsm, &(pSender->pReader));
  ASSERT(ret == 0);

M
Minghao Li 已提交
77
  // get current snapshot info
M
Minghao Li 已提交
78 79
  pSender->pSyncNode->pFsm->FpGetSnapshot(pSender->pSyncNode->pFsm, &(pSender->snapshot));

M
Minghao Li 已提交
80
  // build begin msg
M
Minghao Li 已提交
81 82 83 84 85 86
  SyncSnapshotSend *pMsg = syncSnapshotSendBuild(0, pSender->pSyncNode->vgId);
  pMsg->srcId = pSender->pSyncNode->myRaftId;
  pMsg->destId = (pSender->pSyncNode->replicasId)[pSender->replicaIndex];
  pMsg->term = pSender->pSyncNode->pRaftStore->currentTerm;
  pMsg->lastIndex = pSender->snapshot.lastApplyIndex;
  pMsg->lastTerm = pSender->snapshot.lastApplyTerm;
M
Minghao Li 已提交
87
  pMsg->seq = pSender->seq;  // SYNC_SNAPSHOT_SEQ_BEGIN
M
Minghao Li 已提交
88
  pMsg->privateTerm = pSender->privateTerm;
M
Minghao Li 已提交
89

M
Minghao Li 已提交
90
  // send
M
Minghao Li 已提交
91 92 93
  SRpcMsg rpcMsg;
  syncSnapshotSend2RpcMsg(pMsg, &rpcMsg);
  syncNodeSendMsgById(&(pMsg->destId), pSender->pSyncNode, &rpcMsg);
M
Minghao Li 已提交
94 95

  char *msgStr = syncSnapshotSend2Str(pMsg);
M
Minghao Li 已提交
96
  sTrace("snapshot send begin seq:%d ack:%d send msg:%s", pSender->seq, pSender->ack, msgStr);
M
Minghao Li 已提交
97 98
  taosMemoryFree(msgStr);

M
Minghao Li 已提交
99 100 101
  syncSnapshotSendDestroy(pMsg);
}

102
#if 0
M
Minghao Li 已提交
103
// when entry in snapshot, start sender
M
Minghao Li 已提交
104 105
void snapshotSenderStart(SSyncSnapshotSender *pSender) {
  if (!(pSender->start)) {
M
Minghao Li 已提交
106
    // start
M
Minghao Li 已提交
107 108 109
    snapshotSenderDoStart(pSender);
    pSender->start = true;
  } else {
M
Minghao Li 已提交
110
    // already start
M
Minghao Li 已提交
111 112
    ASSERT(pSender->pSyncNode->pRaftStore->currentTerm >= pSender->term);

M
Minghao Li 已提交
113
    // if current term is higher, need start again
M
Minghao Li 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126
    if (pSender->pSyncNode->pRaftStore->currentTerm > pSender->term) {
      // force peer rollback
      SyncSnapshotSend *pMsg = syncSnapshotSendBuild(0, pSender->pSyncNode->vgId);
      pMsg->srcId = pSender->pSyncNode->myRaftId;
      pMsg->destId = (pSender->pSyncNode->replicasId)[pSender->replicaIndex];
      pMsg->term = pSender->pSyncNode->pRaftStore->currentTerm;
      pMsg->lastIndex = pSender->snapshot.lastApplyIndex;
      pMsg->lastTerm = pSender->snapshot.lastApplyTerm;
      pMsg->seq = SYNC_SNAPSHOT_SEQ_FORCE_CLOSE;

      SRpcMsg rpcMsg;
      syncSnapshotSend2RpcMsg(pMsg, &rpcMsg);
      syncNodeSendMsgById(&(pMsg->destId), pSender->pSyncNode, &rpcMsg);
M
Minghao Li 已提交
127 128

      char *msgStr = syncSnapshotSend2Str(pMsg);
M
Minghao Li 已提交
129
      sTrace("snapshot send force close seq:%d ack:%d send msg:%s", pSender->seq, pSender->ack, msgStr);
M
Minghao Li 已提交
130 131
      taosMemoryFree(msgStr);

M
Minghao Li 已提交
132 133 134 135 136
      syncSnapshotSendDestroy(pMsg);

      // close reader
      int32_t ret = pSender->pSyncNode->pFsm->FpSnapshotStopRead(pSender->pSyncNode->pFsm, pSender->pReader);
      ASSERT(ret == 0);
M
Minghao Li 已提交
137
      pSender->pReader = NULL;
M
Minghao Li 已提交
138 139 140

      // start again
      snapshotSenderDoStart(pSender);
M
Minghao Li 已提交
141
      pSender->start = true;
M
Minghao Li 已提交
142
    } else {
M
Minghao Li 已提交
143 144
      // current term, do nothing
      ASSERT(pSender->pSyncNode->pRaftStore->currentTerm == pSender->term);
M
Minghao Li 已提交
145 146
    }
  }
M
Minghao Li 已提交
147 148 149 150

  char *s = snapshotSender2Str(pSender);
  sInfo("snapshotSenderStart %s", s);
  taosMemoryFree(s);
M
Minghao Li 已提交
151
}
152
#endif
M
Minghao Li 已提交
153 154

void snapshotSenderStop(SSyncSnapshotSender *pSender) {
M
Minghao Li 已提交
155 156 157 158 159
  if (pSender->pReader != NULL) {
    int32_t ret = pSender->pSyncNode->pFsm->FpSnapshotStopRead(pSender->pSyncNode->pFsm, pSender->pReader);
    ASSERT(ret == 0);
    pSender->pReader = NULL;
  }
M
Minghao Li 已提交
160 161 162

  if (pSender->pCurrentBlock != NULL) {
    taosMemoryFree(pSender->pCurrentBlock);
M
Minghao Li 已提交
163
    pSender->pCurrentBlock = NULL;
M
Minghao Li 已提交
164 165
    pSender->blockLen = 0;
  }
M
Minghao Li 已提交
166 167

  pSender->start = false;
M
Minghao Li 已提交
168 169 170 171

  char *s = snapshotSender2Str(pSender);
  sInfo("snapshotSenderStop %s", s);
  taosMemoryFree(s);
M
Minghao Li 已提交
172 173
}

M
Minghao Li 已提交
174 175
// when sender receiver ack, call this function to send msg from seq
// seq = ack + 1, already updated
M
Minghao Li 已提交
176 177 178 179
int32_t snapshotSend(SSyncSnapshotSender *pSender) {
  // free memory last time (seq - 1)
  if (pSender->pCurrentBlock != NULL) {
    taosMemoryFree(pSender->pCurrentBlock);
M
Minghao Li 已提交
180
    pSender->pCurrentBlock = NULL;
M
Minghao Li 已提交
181 182 183 184 185 186 187
    pSender->blockLen = 0;
  }

  // read data
  int32_t ret = pSender->pSyncNode->pFsm->FpSnapshotDoRead(pSender->pSyncNode->pFsm, pSender->pReader,
                                                           &(pSender->pCurrentBlock), &(pSender->blockLen));
  ASSERT(ret == 0);
M
Minghao Li 已提交
188 189 190 191 192 193
  if (pSender->blockLen > 0) {
    // has read data
  } else {
    // read finish
    pSender->seq = SYNC_SNAPSHOT_SEQ_END;
  }
M
Minghao Li 已提交
194 195 196 197 198 199 200 201

  SyncSnapshotSend *pMsg = syncSnapshotSendBuild(pSender->blockLen, pSender->pSyncNode->vgId);
  pMsg->srcId = pSender->pSyncNode->myRaftId;
  pMsg->destId = (pSender->pSyncNode->replicasId)[pSender->replicaIndex];
  pMsg->term = pSender->pSyncNode->pRaftStore->currentTerm;
  pMsg->lastIndex = pSender->snapshot.lastApplyIndex;
  pMsg->lastTerm = pSender->snapshot.lastApplyTerm;
  pMsg->seq = pSender->seq;
M
Minghao Li 已提交
202
  pMsg->privateTerm = pSender->privateTerm;
M
Minghao Li 已提交
203 204 205 206 207
  memcpy(pMsg->data, pSender->pCurrentBlock, pSender->blockLen);

  SRpcMsg rpcMsg;
  syncSnapshotSend2RpcMsg(pMsg, &rpcMsg);
  syncNodeSendMsgById(&(pMsg->destId), pSender->pSyncNode, &rpcMsg);
M
Minghao Li 已提交
208 209 210

  char *msgStr = syncSnapshotSend2Str(pMsg);
  if (pSender->seq == SYNC_SNAPSHOT_SEQ_END) {
M
Minghao Li 已提交
211
    sTrace("snapshot send finish seq:%d ack:%d send msg:%s", pSender->seq, pSender->ack, msgStr);
M
Minghao Li 已提交
212
  } else {
M
Minghao Li 已提交
213
    sTrace("snapshot send sending seq:%d ack:%d send msg:%s", pSender->seq, pSender->ack, msgStr);
M
Minghao Li 已提交
214 215 216
  }
  taosMemoryFree(msgStr);

M
Minghao Li 已提交
217 218 219 220 221
  syncSnapshotSendDestroy(pMsg);

  return 0;
}

M
Minghao Li 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
// send snapshot data from cache
int32_t snapshotReSend(SSyncSnapshotSender *pSender) {
  if (pSender->pCurrentBlock != NULL) {
    SyncSnapshotSend *pMsg = syncSnapshotSendBuild(pSender->blockLen, pSender->pSyncNode->vgId);
    pMsg->srcId = pSender->pSyncNode->myRaftId;
    pMsg->destId = (pSender->pSyncNode->replicasId)[pSender->replicaIndex];
    pMsg->term = pSender->pSyncNode->pRaftStore->currentTerm;
    pMsg->lastIndex = pSender->snapshot.lastApplyIndex;
    pMsg->lastTerm = pSender->snapshot.lastApplyTerm;
    pMsg->seq = pSender->seq;
    memcpy(pMsg->data, pSender->pCurrentBlock, pSender->blockLen);

    SRpcMsg rpcMsg;
    syncSnapshotSend2RpcMsg(pMsg, &rpcMsg);
    syncNodeSendMsgById(&(pMsg->destId), pSender->pSyncNode, &rpcMsg);
M
Minghao Li 已提交
237 238

    char *msgStr = syncSnapshotSend2Str(pMsg);
M
Minghao Li 已提交
239
    sTrace("snapshot send resend seq:%d ack:%d send msg:%s", pSender->seq, pSender->ack, msgStr);
M
Minghao Li 已提交
240 241
    taosMemoryFree(msgStr);

M
Minghao Li 已提交
242 243 244 245 246
    syncSnapshotSendDestroy(pMsg);
  }
  return 0;
}

M
Minghao Li 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
cJSON *snapshotSender2Json(SSyncSnapshotSender *pSender) {
  char   u64buf[128];
  cJSON *pRoot = cJSON_CreateObject();

  if (pSender != NULL) {
    cJSON_AddNumberToObject(pRoot, "start", pSender->start);
    cJSON_AddNumberToObject(pRoot, "seq", pSender->seq);
    cJSON_AddNumberToObject(pRoot, "ack", pSender->ack);

    snprintf(u64buf, sizeof(u64buf), "%p", pSender->pReader);
    cJSON_AddStringToObject(pRoot, "pReader", u64buf);

    snprintf(u64buf, sizeof(u64buf), "%p", pSender->pCurrentBlock);
    cJSON_AddStringToObject(pRoot, "pCurrentBlock", u64buf);
    cJSON_AddNumberToObject(pRoot, "blockLen", pSender->blockLen);

    if (pSender->pCurrentBlock != NULL) {
      char *s;
      s = syncUtilprintBin((char *)(pSender->pCurrentBlock), pSender->blockLen);
      cJSON_AddStringToObject(pRoot, "pCurrentBlock", s);
      taosMemoryFree(s);
      s = syncUtilprintBin2((char *)(pSender->pCurrentBlock), pSender->blockLen);
      cJSON_AddStringToObject(pRoot, "pCurrentBlock2", s);
      taosMemoryFree(s);
    }

    cJSON *pSnapshot = cJSON_CreateObject();
    snprintf(u64buf, sizeof(u64buf), "%lu", pSender->snapshot.lastApplyIndex);
M
Minghao Li 已提交
275
    cJSON_AddStringToObject(pSnapshot, "lastApplyIndex", u64buf);
M
Minghao Li 已提交
276
    snprintf(u64buf, sizeof(u64buf), "%lu", pSender->snapshot.lastApplyTerm);
M
Minghao Li 已提交
277
    cJSON_AddStringToObject(pSnapshot, "lastApplyTerm", u64buf);
M
Minghao Li 已提交
278 279 280 281 282 283 284 285 286
    cJSON_AddItemToObject(pRoot, "snapshot", pSnapshot);

    snprintf(u64buf, sizeof(u64buf), "%lu", pSender->sendingMS);
    cJSON_AddStringToObject(pRoot, "sendingMS", u64buf);
    snprintf(u64buf, sizeof(u64buf), "%p", pSender->pSyncNode);
    cJSON_AddStringToObject(pRoot, "pSyncNode", u64buf);
    cJSON_AddNumberToObject(pRoot, "replicaIndex", pSender->replicaIndex);
    snprintf(u64buf, sizeof(u64buf), "%lu", pSender->term);
    cJSON_AddStringToObject(pRoot, "term", u64buf);
287 288
    snprintf(u64buf, sizeof(u64buf), "%lu", pSender->privateTerm);
    cJSON_AddStringToObject(pRoot, "privateTerm", u64buf);
M
Minghao Li 已提交
289
    cJSON_AddNumberToObject(pRoot, "finish", pSender->finish);
M
Minghao Li 已提交
290 291 292 293 294 295 296 297 298
  }

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

char *snapshotSender2Str(SSyncSnapshotSender *pSender) {
  cJSON *pJson = snapshotSender2Json(pSender);
M
Minghao Li 已提交
299
  char * serialized = cJSON_Print(pJson);
M
Minghao Li 已提交
300 301 302 303 304
  cJSON_Delete(pJson);
  return serialized;
}

// -------------------------------------
305
SSyncSnapshotReceiver *snapshotReceiverCreate(SSyncNode *pSyncNode, int32_t replicaIndex) {
M
Minghao Li 已提交
306 307
  bool condition = (pSyncNode->pFsm->FpSnapshotStartWrite != NULL) && (pSyncNode->pFsm->FpSnapshotStopWrite != NULL) &&
                   (pSyncNode->pFsm->FpSnapshotDoWrite != NULL);
308

M
Minghao Li 已提交
309 310 311 312 313
  SSyncSnapshotReceiver *pReceiver;
  if (condition) {
    pReceiver = taosMemoryMalloc(sizeof(SSyncSnapshotReceiver));
    ASSERT(pReceiver != NULL);
    memset(pReceiver, 0, sizeof(*pReceiver));
314

M
Minghao Li 已提交
315 316 317 318 319 320
    pReceiver->start = false;
    pReceiver->ack = SYNC_SNAPSHOT_SEQ_BEGIN;
    pReceiver->pWriter = NULL;
    pReceiver->pSyncNode = pSyncNode;
    pReceiver->replicaIndex = replicaIndex;
    pReceiver->term = pSyncNode->pRaftStore->currentTerm;
M
Minghao Li 已提交
321 322
    pReceiver->privateTerm = 0;

M
Minghao Li 已提交
323 324 325
  } else {
    sInfo("snapshotReceiverCreate cannot create receiver");
  }
326 327 328

  return pReceiver;
}
M
Minghao Li 已提交
329

330 331 332 333 334
void snapshotReceiverDestroy(SSyncSnapshotReceiver *pReceiver) {
  if (pReceiver != NULL) {
    taosMemoryFree(pReceiver);
  }
}
M
Minghao Li 已提交
335

M
Minghao Li 已提交
336 337 338 339 340 341 342 343 344 345 346 347
// begin receive snapshot msg (current term, seq begin)
static void snapshotReceiverDoStart(SSyncSnapshotReceiver *pReceiver) {
  pReceiver->term = pReceiver->pSyncNode->pRaftStore->currentTerm;
  pReceiver->ack = SYNC_SNAPSHOT_SEQ_BEGIN;

  ASSERT(pReceiver->pWriter == NULL);
  int32_t ret = pReceiver->pSyncNode->pFsm->FpSnapshotStartWrite(pReceiver->pSyncNode->pFsm, &(pReceiver->pWriter));
  ASSERT(ret == 0);
}

// if receiver receive msg from seq = SYNC_SNAPSHOT_SEQ_BEGIN, start receiver
// if already start, force close, start again
348 349
void snapshotReceiverStart(SSyncSnapshotReceiver *pReceiver) {
  if (!(pReceiver->start)) {
M
Minghao Li 已提交
350 351
    // start
    snapshotReceiverDoStart(pReceiver);
M
Minghao Li 已提交
352
    pReceiver->start = true;
M
Minghao Li 已提交
353

354
  } else {
M
Minghao Li 已提交
355 356 357 358 359 360 361 362 363 364
    // already start

    // force close, abandon incomplete data
    int32_t ret =
        pReceiver->pSyncNode->pFsm->FpSnapshotStopWrite(pReceiver->pSyncNode->pFsm, pReceiver->pWriter, false);
    ASSERT(ret == 0);
    pReceiver->pWriter = NULL;

    // start again
    snapshotReceiverDoStart(pReceiver);
M
Minghao Li 已提交
365
    pReceiver->start = true;
M
Minghao Li 已提交
366

367 368
    ASSERT(0);
  }
M
Minghao Li 已提交
369 370 371 372

  char *s = snapshotReceiver2Str(pReceiver);
  sInfo("snapshotReceiverStart %s", s);
  taosMemoryFree(s);
373
}
M
Minghao Li 已提交
374

375
void snapshotReceiverStop(SSyncSnapshotReceiver *pReceiver) {
M
Minghao Li 已提交
376 377 378 379 380
  if (pReceiver->pWriter != NULL) {
    int32_t ret =
        pReceiver->pSyncNode->pFsm->FpSnapshotStopWrite(pReceiver->pSyncNode->pFsm, pReceiver->pWriter, false);
    ASSERT(ret == 0);
    pReceiver->pWriter = NULL;
381
  }
M
Minghao Li 已提交
382 383

  pReceiver->start = false;
M
Minghao Li 已提交
384
  ++(pReceiver->privateTerm);
M
Minghao Li 已提交
385 386 387 388

  char *s = snapshotReceiver2Str(pReceiver);
  sInfo("snapshotReceiverStop %s", s);
  taosMemoryFree(s);
389 390 391 392 393
}

cJSON *snapshotReceiver2Json(SSyncSnapshotReceiver *pReceiver) {
  char   u64buf[128];
  cJSON *pRoot = cJSON_CreateObject();
M
Minghao Li 已提交
394

395 396 397
  if (pReceiver != NULL) {
    cJSON_AddNumberToObject(pRoot, "start", pReceiver->start);
    cJSON_AddNumberToObject(pRoot, "ack", pReceiver->ack);
M
Minghao Li 已提交
398

399 400 401 402 403 404 405 406
    snprintf(u64buf, sizeof(u64buf), "%p", pReceiver->pWriter);
    cJSON_AddStringToObject(pRoot, "pWriter", u64buf);

    snprintf(u64buf, sizeof(u64buf), "%p", pReceiver->pSyncNode);
    cJSON_AddStringToObject(pRoot, "pSyncNode", u64buf);
    cJSON_AddNumberToObject(pRoot, "replicaIndex", pReceiver->replicaIndex);
    snprintf(u64buf, sizeof(u64buf), "%lu", pReceiver->term);
    cJSON_AddStringToObject(pRoot, "term", u64buf);
407 408 409

    snprintf(u64buf, sizeof(u64buf), "%lu", pReceiver->privateTerm);
    cJSON_AddStringToObject(pRoot, "privateTerm", u64buf);
410 411 412 413 414 415 416 417 418
  }

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

char *snapshotReceiver2Str(SSyncSnapshotReceiver *pReceiver) {
  cJSON *pJson = snapshotReceiver2Json(pReceiver);
M
Minghao Li 已提交
419
  char * serialized = cJSON_Print(pJson);
420 421 422
  cJSON_Delete(pJson);
  return serialized;
}
M
Minghao Li 已提交
423

424
// receiver do something
M
Minghao Li 已提交
425
int32_t syncNodeOnSnapshotSendCb(SSyncNode *pSyncNode, SyncSnapshotSend *pMsg) {
M
Minghao Li 已提交
426
  // get receiver
427 428
  SSyncSnapshotReceiver *pReceiver = pSyncNode->pNewNodeReceiver;
  bool                   needRsp = false;
M
Minghao Li 已提交
429

430 431
  // state, term, seq/ack
  if (pSyncNode->state == TAOS_SYNC_STATE_FOLLOWER) {
M
Minghao Li 已提交
432 433 434 435 436
    if (pMsg->term == pSyncNode->pRaftStore->currentTerm) {
      if (pMsg->seq == SYNC_SNAPSHOT_SEQ_BEGIN) {
        // begin
        snapshotReceiverStart(pReceiver);
        pReceiver->ack = pMsg->seq;
M
Minghao Li 已提交
437
        pReceiver->privateTerm = pMsg->privateTerm;
M
Minghao Li 已提交
438 439
        needRsp = true;

M
Minghao Li 已提交
440
        char *msgStr = syncSnapshotSend2Str(pMsg);
M
Minghao Li 已提交
441 442
        sTrace("snapshot recv begin ack:%d, lastIndex:%ld, lastTerm:%lu, recv msg:%s", pReceiver->ack, pMsg->lastIndex,
               pMsg->lastTerm, msgStr);
M
Minghao Li 已提交
443 444
        taosMemoryFree(msgStr);

M
Minghao Li 已提交
445 446
      } else if (pMsg->seq == SYNC_SNAPSHOT_SEQ_END) {
        // end, finish FSM
447
        pSyncNode->pFsm->FpSnapshotDoWrite(pSyncNode->pFsm, pReceiver->pWriter, pMsg->data, pMsg->dataLen);
M
Minghao Li 已提交
448
        pSyncNode->pFsm->FpSnapshotStopWrite(pSyncNode->pFsm, pReceiver->pWriter, true);
M
Minghao Li 已提交
449

M
Minghao Li 已提交
450
        pSyncNode->pLogStore->syncLogSetBeginIndex(pSyncNode->pLogStore, pMsg->lastIndex + 1);
M
Minghao Li 已提交
451
        char *    logSimpleStr = logStoreSimple2Str(pSyncNode->pLogStore);
M
Minghao Li 已提交
452 453
        SSnapshot snapshot;
        pSyncNode->pFsm->FpGetSnapshot(pSyncNode->pFsm, &snapshot);
M
Minghao Li 已提交
454 455 456 457
        sInfo(
            "snapshot recv finish, update log begin index:%ld, snapshot.lastApplyIndex:%ld, "
            "snapshot.lastApplyTerm:%lu, raft log:%s",
            pMsg->lastIndex + 1, snapshot.lastApplyIndex, snapshot.lastApplyTerm, logSimpleStr);
M
Minghao Li 已提交
458 459 460 461
        taosMemoryFree(logSimpleStr);

        // walRestoreFromSnapshot(pSyncNode->pWal, pMsg->lastIndex);
        // sInfo("walRestoreFromSnapshot lastIndex:%ld", pMsg->lastIndex);
M
Minghao Li 已提交
462

M
Minghao Li 已提交
463
        pReceiver->pWriter = NULL;
M
Minghao Li 已提交
464
        snapshotReceiverStop(pReceiver);
M
Minghao Li 已提交
465
        pReceiver->ack = pMsg->seq;
M
Minghao Li 已提交
466
        needRsp = true;
M
Minghao Li 已提交
467

M
Minghao Li 已提交
468
        char *msgStr = syncSnapshotSend2Str(pMsg);
M
Minghao Li 已提交
469 470
        sTrace("snapshot recv end ack:%d, lastIndex:%ld, lastTerm:%lu, recv msg:%s", pReceiver->ack, pMsg->lastIndex,
               pMsg->lastTerm, msgStr);
M
Minghao Li 已提交
471 472
        taosMemoryFree(msgStr);

M
Minghao Li 已提交
473 474 475 476 477
      } else if (pMsg->seq == SYNC_SNAPSHOT_SEQ_FORCE_CLOSE) {
        pSyncNode->pFsm->FpSnapshotStopWrite(pSyncNode->pFsm, pReceiver->pWriter, false);
        snapshotReceiverStop(pReceiver);
        needRsp = false;

M
Minghao Li 已提交
478
        char *msgStr = syncSnapshotSend2Str(pMsg);
M
Minghao Li 已提交
479 480
        sTrace("snapshot recv force close ack:%d, lastIndex:%ld, lastTerm:%lu, recv msg:%s", pReceiver->ack,
               pMsg->lastIndex, pMsg->lastTerm, msgStr);
M
Minghao Li 已提交
481

M
Minghao Li 已提交
482 483
        taosMemoryFree(msgStr);

M
Minghao Li 已提交
484 485 486 487 488 489 490 491
      } else if (pMsg->seq > SYNC_SNAPSHOT_SEQ_BEGIN && pMsg->seq < SYNC_SNAPSHOT_SEQ_END) {
        // transfering
        if (pMsg->seq == pReceiver->ack + 1) {
          pSyncNode->pFsm->FpSnapshotDoWrite(pSyncNode->pFsm, pReceiver->pWriter, pMsg->data, pMsg->dataLen);
          pReceiver->ack = pMsg->seq;
        }
        needRsp = true;

M
Minghao Li 已提交
492
        char *msgStr = syncSnapshotSend2Str(pMsg);
M
Minghao Li 已提交
493 494
        sTrace("snapshot recv receiving ack:%d, lastIndex:%ld, lastTerm:%lu, recv msg:%s", pReceiver->ack,
               pMsg->lastIndex, pMsg->lastTerm, msgStr);
M
Minghao Li 已提交
495 496
        taosMemoryFree(msgStr);

M
Minghao Li 已提交
497 498
      } else {
        ASSERT(0);
499
      }
M
Minghao Li 已提交
500

M
Minghao Li 已提交
501 502 503 504 505 506 507 508
      if (needRsp) {
        SyncSnapshotRsp *pRspMsg = syncSnapshotRspBuild(pSyncNode->vgId);
        pRspMsg->srcId = pSyncNode->myRaftId;
        pRspMsg->destId = pMsg->srcId;
        pRspMsg->term = pSyncNode->pRaftStore->currentTerm;
        pRspMsg->lastIndex = pMsg->lastIndex;
        pRspMsg->lastTerm = pMsg->lastTerm;
        pRspMsg->ack = pReceiver->ack;
M
Minghao Li 已提交
509
        pRspMsg->privateTerm = pReceiver->privateTerm;
M
Minghao Li 已提交
510 511 512 513

        SRpcMsg rpcMsg;
        syncSnapshotRsp2RpcMsg(pRspMsg, &rpcMsg);
        syncNodeSendMsgById(&(pRspMsg->destId), pSyncNode, &rpcMsg);
M
Minghao Li 已提交
514

M
Minghao Li 已提交
515 516
        syncSnapshotRspDestroy(pRspMsg);
      }
M
Minghao Li 已提交
517
    }
M
Minghao Li 已提交
518 519
  } else {
    syncNodeLog2("syncNodeOnSnapshotSendCb not follower", pSyncNode);
M
Minghao Li 已提交
520
  }
M
Minghao Li 已提交
521

M
Minghao Li 已提交
522 523 524
  return 0;
}

M
Minghao Li 已提交
525 526
// sender receives ack, set seq = ack + 1, send msg from seq
// if ack == SYNC_SNAPSHOT_SEQ_END, stop sender
527
int32_t syncNodeOnSnapshotRspCb(SSyncNode *pSyncNode, SyncSnapshotRsp *pMsg) {
M
Minghao Li 已提交
528
  // get sender
529 530 531 532 533 534 535 536 537 538 539
  SSyncSnapshotSender *pSender = NULL;
  for (int i = 0; i < pSyncNode->replicaNum; ++i) {
    if (syncUtilSameId(&(pMsg->srcId), &((pSyncNode->replicasId)[i]))) {
      pSender = (pSyncNode->senders)[i];
    }
  }
  ASSERT(pSender != NULL);

  // state, term, seq/ack
  if (pSyncNode->state == TAOS_SYNC_STATE_LEADER) {
    if (pMsg->term == pSyncNode->pRaftStore->currentTerm) {
M
Minghao Li 已提交
540 541
      // receiver ack is finish, close sender
      if (pMsg->ack == SYNC_SNAPSHOT_SEQ_END) {
M
Minghao Li 已提交
542
        pSender->finish = true;
M
Minghao Li 已提交
543
        snapshotSenderStop(pSender);
544 545

        // update nextIndex private term
M
Minghao Li 已提交
546
        // syncIndexMgrSetTerm(pSyncNode->pNextIndex, &(pMsg->srcId), pSender->privateTerm);
547

M
Minghao Li 已提交
548 549 550 551
        return 0;
      }

      // send next msg
552
      if (pMsg->ack == pSender->seq) {
M
Minghao Li 已提交
553
        // update sender ack
554 555
        pSender->ack = pMsg->ack;
        (pSender->seq)++;
M
Minghao Li 已提交
556
        snapshotSend(pSender);
M
Minghao Li 已提交
557 558 559 560
      } else if (pMsg->ack == pSender->seq - 1) {
        snapshotReSend(pSender);
      } else {
        ASSERT(0);
561 562
      }
    }
M
Minghao Li 已提交
563 564
  } else {
    syncNodeLog2("syncNodeOnSnapshotRspCb not leader", pSyncNode);
565 566 567 568
  }

  return 0;
}