syncSnapshot.c 17.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 "syncSnapshot.h"
M
Minghao Li 已提交
17 18
#include "syncRaftStore.h"
#include "syncUtil.h"
M
Minghao Li 已提交
19

M
Minghao Li 已提交
20
static void snapshotSenderDoStart(SSyncSnapshotSender *pSender);
M
Minghao Li 已提交
21
static void snapshotReceiverDoStart(SSyncSnapshotReceiver *pReceiver);
M
Minghao Li 已提交
22

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

M
Minghao Li 已提交
27 28 29 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;
  } else {
    sInfo("snapshotSenderCreate cannot create sender");
  }
M
Minghao Li 已提交
46

M
Minghao Li 已提交
47 48
  return pSender;
}
M
Minghao Li 已提交
49

M
Minghao Li 已提交
50 51 52 53 54
void snapshotSenderDestroy(SSyncSnapshotSender *pSender) {
  if (pSender != NULL) {
    taosMemoryFree(pSender);
  }
}
M
Minghao Li 已提交
55

M
Minghao Li 已提交
56
// begin send snapshot (current term, seq begin)
M
Minghao Li 已提交
57 58 59 60
static void snapshotSenderDoStart(SSyncSnapshotSender *pSender) {
  pSender->term = pSender->pSyncNode->pRaftStore->currentTerm;
  pSender->seq = SYNC_SNAPSHOT_SEQ_BEGIN;
  pSender->ack = SYNC_SNAPSHOT_SEQ_INVALID;
M
Minghao Li 已提交
61

M
Minghao Li 已提交
62 63
  // open snapshot reader
  ASSERT(pSender->pReader == NULL);
M
Minghao Li 已提交
64 65 66
  int32_t ret = pSender->pSyncNode->pFsm->FpSnapshotStartRead(pSender->pSyncNode->pFsm, &(pSender->pReader));
  ASSERT(ret == 0);

M
Minghao Li 已提交
67
  // get current snapshot info
M
Minghao Li 已提交
68 69
  pSender->pSyncNode->pFsm->FpGetSnapshot(pSender->pSyncNode->pFsm, &(pSender->snapshot));

M
Minghao Li 已提交
70
  // build begin msg
M
Minghao Li 已提交
71 72 73 74 75 76
  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 已提交
77
  pMsg->seq = pSender->seq;  // SYNC_SNAPSHOT_SEQ_BEGIN
M
Minghao Li 已提交
78

M
Minghao Li 已提交
79
  // send
M
Minghao Li 已提交
80 81 82
  SRpcMsg rpcMsg;
  syncSnapshotSend2RpcMsg(pMsg, &rpcMsg);
  syncNodeSendMsgById(&(pMsg->destId), pSender->pSyncNode, &rpcMsg);
M
Minghao Li 已提交
83 84

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

M
Minghao Li 已提交
88 89 90
  syncSnapshotSendDestroy(pMsg);
}

M
Minghao Li 已提交
91
// when entry in snapshot, start sender
M
Minghao Li 已提交
92 93
void snapshotSenderStart(SSyncSnapshotSender *pSender) {
  if (!(pSender->start)) {
M
Minghao Li 已提交
94
    // start
M
Minghao Li 已提交
95 96 97
    snapshotSenderDoStart(pSender);
    pSender->start = true;
  } else {
M
Minghao Li 已提交
98
    // already start
M
Minghao Li 已提交
99 100
    ASSERT(pSender->pSyncNode->pRaftStore->currentTerm >= pSender->term);

M
Minghao Li 已提交
101
    // if current term is higher, need start again
M
Minghao Li 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114
    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 已提交
115 116

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

M
Minghao Li 已提交
120 121 122 123 124
      syncSnapshotSendDestroy(pMsg);

      // close reader
      int32_t ret = pSender->pSyncNode->pFsm->FpSnapshotStopRead(pSender->pSyncNode->pFsm, pSender->pReader);
      ASSERT(ret == 0);
M
Minghao Li 已提交
125
      pSender->pReader = NULL;
M
Minghao Li 已提交
126 127 128

      // start again
      snapshotSenderDoStart(pSender);
M
Minghao Li 已提交
129
      pSender->start = true;
M
Minghao Li 已提交
130
    } else {
M
Minghao Li 已提交
131 132
      // current term, do nothing
      ASSERT(pSender->pSyncNode->pRaftStore->currentTerm == pSender->term);
M
Minghao Li 已提交
133 134
    }
  }
M
Minghao Li 已提交
135 136 137 138

  char *s = snapshotSender2Str(pSender);
  sInfo("snapshotSenderStart %s", s);
  taosMemoryFree(s);
M
Minghao Li 已提交
139 140 141
}

void snapshotSenderStop(SSyncSnapshotSender *pSender) {
M
Minghao Li 已提交
142 143 144 145 146
  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 已提交
147 148 149

  if (pSender->pCurrentBlock != NULL) {
    taosMemoryFree(pSender->pCurrentBlock);
M
Minghao Li 已提交
150
    pSender->pCurrentBlock = NULL;
M
Minghao Li 已提交
151 152
    pSender->blockLen = 0;
  }
M
Minghao Li 已提交
153 154

  pSender->start = false;
M
Minghao Li 已提交
155 156 157 158

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

M
Minghao Li 已提交
161 162
// when sender receiver ack, call this function to send msg from seq
// seq = ack + 1, already updated
M
Minghao Li 已提交
163 164 165 166
int32_t snapshotSend(SSyncSnapshotSender *pSender) {
  // free memory last time (seq - 1)
  if (pSender->pCurrentBlock != NULL) {
    taosMemoryFree(pSender->pCurrentBlock);
M
Minghao Li 已提交
167
    pSender->pCurrentBlock = NULL;
M
Minghao Li 已提交
168 169 170 171 172 173 174
    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 已提交
175 176 177 178 179 180
  if (pSender->blockLen > 0) {
    // has read data
  } else {
    // read finish
    pSender->seq = SYNC_SNAPSHOT_SEQ_END;
  }
M
Minghao Li 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193

  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 已提交
194 195 196

  char *msgStr = syncSnapshotSend2Str(pMsg);
  if (pSender->seq == SYNC_SNAPSHOT_SEQ_END) {
M
Minghao Li 已提交
197
    sTrace("snapshot send finish seq:%d ack:%d send msg:%s", pSender->seq, pSender->ack, msgStr);
M
Minghao Li 已提交
198
  } else {
M
Minghao Li 已提交
199
    sTrace("snapshot send sending seq:%d ack:%d send msg:%s", pSender->seq, pSender->ack, msgStr);
M
Minghao Li 已提交
200 201 202
  }
  taosMemoryFree(msgStr);

M
Minghao Li 已提交
203 204 205 206 207
  syncSnapshotSendDestroy(pMsg);

  return 0;
}

M
Minghao Li 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
// 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 已提交
223 224

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

M
Minghao Li 已提交
228 229 230 231 232
    syncSnapshotSendDestroy(pMsg);
  }
  return 0;
}

M
Minghao Li 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
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 已提交
261
    cJSON_AddStringToObject(pSnapshot, "lastApplyIndex", u64buf);
M
Minghao Li 已提交
262
    snprintf(u64buf, sizeof(u64buf), "%lu", pSender->snapshot.lastApplyTerm);
M
Minghao Li 已提交
263
    cJSON_AddStringToObject(pSnapshot, "lastApplyTerm", u64buf);
M
Minghao Li 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    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);
  }

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

char *snapshotSender2Str(SSyncSnapshotSender *pSender) {
  cJSON *pJson = snapshotSender2Json(pSender);
M
Minghao Li 已提交
282
  char * serialized = cJSON_Print(pJson);
M
Minghao Li 已提交
283 284 285 286 287
  cJSON_Delete(pJson);
  return serialized;
}

// -------------------------------------
288
SSyncSnapshotReceiver *snapshotReceiverCreate(SSyncNode *pSyncNode, int32_t replicaIndex) {
M
Minghao Li 已提交
289 290
  bool condition = (pSyncNode->pFsm->FpSnapshotStartWrite != NULL) && (pSyncNode->pFsm->FpSnapshotStopWrite != NULL) &&
                   (pSyncNode->pFsm->FpSnapshotDoWrite != NULL);
291

M
Minghao Li 已提交
292 293 294 295 296
  SSyncSnapshotReceiver *pReceiver;
  if (condition) {
    pReceiver = taosMemoryMalloc(sizeof(SSyncSnapshotReceiver));
    ASSERT(pReceiver != NULL);
    memset(pReceiver, 0, sizeof(*pReceiver));
297

M
Minghao Li 已提交
298 299 300 301 302 303 304 305 306
    pReceiver->start = false;
    pReceiver->ack = SYNC_SNAPSHOT_SEQ_BEGIN;
    pReceiver->pWriter = NULL;
    pReceiver->pSyncNode = pSyncNode;
    pReceiver->replicaIndex = replicaIndex;
    pReceiver->term = pSyncNode->pRaftStore->currentTerm;
  } else {
    sInfo("snapshotReceiverCreate cannot create receiver");
  }
307 308 309

  return pReceiver;
}
M
Minghao Li 已提交
310

311 312 313 314 315
void snapshotReceiverDestroy(SSyncSnapshotReceiver *pReceiver) {
  if (pReceiver != NULL) {
    taosMemoryFree(pReceiver);
  }
}
M
Minghao Li 已提交
316

M
Minghao Li 已提交
317 318 319 320 321 322 323 324 325 326 327 328
// 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
329 330
void snapshotReceiverStart(SSyncSnapshotReceiver *pReceiver) {
  if (!(pReceiver->start)) {
M
Minghao Li 已提交
331 332
    // start
    snapshotReceiverDoStart(pReceiver);
M
Minghao Li 已提交
333
    pReceiver->start = true;
M
Minghao Li 已提交
334

335
  } else {
M
Minghao Li 已提交
336 337 338 339 340 341 342 343 344 345
    // 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 已提交
346
    pReceiver->start = true;
M
Minghao Li 已提交
347

348 349
    ASSERT(0);
  }
M
Minghao Li 已提交
350 351 352 353

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

356
void snapshotReceiverStop(SSyncSnapshotReceiver *pReceiver) {
M
Minghao Li 已提交
357 358 359 360 361
  if (pReceiver->pWriter != NULL) {
    int32_t ret =
        pReceiver->pSyncNode->pFsm->FpSnapshotStopWrite(pReceiver->pSyncNode->pFsm, pReceiver->pWriter, false);
    ASSERT(ret == 0);
    pReceiver->pWriter = NULL;
362
  }
M
Minghao Li 已提交
363 364

  pReceiver->start = false;
M
Minghao Li 已提交
365 366 367 368

  char *s = snapshotReceiver2Str(pReceiver);
  sInfo("snapshotReceiverStop %s", s);
  taosMemoryFree(s);
369 370 371 372 373
}

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

375 376 377
  if (pReceiver != NULL) {
    cJSON_AddNumberToObject(pRoot, "start", pReceiver->start);
    cJSON_AddNumberToObject(pRoot, "ack", pReceiver->ack);
M
Minghao Li 已提交
378

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
    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);
  }

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

char *snapshotReceiver2Str(SSyncSnapshotReceiver *pReceiver) {
  cJSON *pJson = snapshotReceiver2Json(pReceiver);
M
Minghao Li 已提交
396
  char * serialized = cJSON_Print(pJson);
397 398 399
  cJSON_Delete(pJson);
  return serialized;
}
M
Minghao Li 已提交
400

401
// receiver do something
M
Minghao Li 已提交
402
int32_t syncNodeOnSnapshotSendCb(SSyncNode *pSyncNode, SyncSnapshotSend *pMsg) {
M
Minghao Li 已提交
403
  // get receiver
M
Minghao Li 已提交
404 405 406 407 408 409
  SSyncSnapshotReceiver *pReceiver = NULL;
  for (int i = 0; i < pSyncNode->replicaNum; ++i) {
    if (syncUtilSameId(&(pMsg->srcId), &((pSyncNode->replicasId)[i]))) {
      pReceiver = (pSyncNode->receivers)[i];
    }
  }
M
Minghao Li 已提交
410 411 412 413 414

  // add new replica
  if (pReceiver == NULL) {
    pReceiver = pSyncNode->pNewNodeReceiver;
  }
M
Minghao Li 已提交
415

M
Minghao Li 已提交
416 417
  bool needRsp = false;

418 419
  // state, term, seq/ack
  if (pSyncNode->state == TAOS_SYNC_STATE_FOLLOWER) {
M
Minghao Li 已提交
420 421 422 423 424 425 426
    if (pMsg->term == pSyncNode->pRaftStore->currentTerm) {
      if (pMsg->seq == SYNC_SNAPSHOT_SEQ_BEGIN) {
        // begin
        snapshotReceiverStart(pReceiver);
        pReceiver->ack = pMsg->seq;
        needRsp = true;

M
Minghao Li 已提交
427 428 429 430
        char *msgStr = syncSnapshotSend2Str(pMsg);
        sTrace("snapshot recv begin ack:%d recv msg:%s", pReceiver->ack, msgStr);
        taosMemoryFree(msgStr);

M
Minghao Li 已提交
431 432
      } else if (pMsg->seq == SYNC_SNAPSHOT_SEQ_END) {
        // end, finish FSM
433
        pSyncNode->pFsm->FpSnapshotDoWrite(pSyncNode->pFsm, pReceiver->pWriter, pMsg->data, pMsg->dataLen);
M
Minghao Li 已提交
434 435
        pSyncNode->pFsm->FpSnapshotStopWrite(pSyncNode->pFsm, pReceiver->pWriter, true);
        snapshotReceiverStop(pReceiver);
M
Minghao Li 已提交
436
        pReceiver->ack = pMsg->seq;
M
Minghao Li 已提交
437
        needRsp = false;
M
Minghao Li 已提交
438

M
Minghao Li 已提交
439 440 441 442
        char *msgStr = syncSnapshotSend2Str(pMsg);
        sTrace("snapshot recv end ack:%d recv msg:%s", pReceiver->ack, msgStr);
        taosMemoryFree(msgStr);

M
Minghao Li 已提交
443 444 445 446 447
      } else if (pMsg->seq == SYNC_SNAPSHOT_SEQ_FORCE_CLOSE) {
        pSyncNode->pFsm->FpSnapshotStopWrite(pSyncNode->pFsm, pReceiver->pWriter, false);
        snapshotReceiverStop(pReceiver);
        needRsp = false;

M
Minghao Li 已提交
448 449 450 451
        char *msgStr = syncSnapshotSend2Str(pMsg);
        sTrace("snapshot recv force close ack:%d recv msg:%s", pReceiver->ack, msgStr);
        taosMemoryFree(msgStr);

M
Minghao Li 已提交
452 453 454 455 456 457 458 459
      } 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 已提交
460 461 462 463
        char *msgStr = syncSnapshotSend2Str(pMsg);
        sTrace("snapshot recv receiving ack:%d recv msg:%s", pReceiver->ack, msgStr);
        taosMemoryFree(msgStr);

M
Minghao Li 已提交
464 465
      } else {
        ASSERT(0);
466
      }
M
Minghao Li 已提交
467

M
Minghao Li 已提交
468 469 470 471 472 473 474 475 476 477 478 479
      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;

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

M
Minghao Li 已提交
481 482
        syncSnapshotRspDestroy(pRspMsg);
      }
M
Minghao Li 已提交
483
    }
M
Minghao Li 已提交
484 485
  } else {
    syncNodeLog2("syncNodeOnSnapshotSendCb not follower", pSyncNode);
M
Minghao Li 已提交
486
  }
M
Minghao Li 已提交
487

M
Minghao Li 已提交
488 489 490
  return 0;
}

M
Minghao Li 已提交
491 492
// sender receives ack, set seq = ack + 1, send msg from seq
// if ack == SYNC_SNAPSHOT_SEQ_END, stop sender
493
int32_t syncNodeOnSnapshotRspCb(SSyncNode *pSyncNode, SyncSnapshotRsp *pMsg) {
M
Minghao Li 已提交
494
  // get sender
495 496 497 498 499 500 501 502 503 504 505
  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 已提交
506 507 508 509 510 511 512
      // receiver ack is finish, close sender
      if (pMsg->ack == SYNC_SNAPSHOT_SEQ_END) {
        snapshotSenderStop(pSender);
        return 0;
      }

      // send next msg
513
      if (pMsg->ack == pSender->seq) {
M
Minghao Li 已提交
514
        // update sender ack
515 516
        pSender->ack = pMsg->ack;
        (pSender->seq)++;
M
Minghao Li 已提交
517
        snapshotSend(pSender);
M
Minghao Li 已提交
518 519 520 521
      } else if (pMsg->ack == pSender->seq - 1) {
        snapshotReSend(pSender);
      } else {
        ASSERT(0);
522 523
      }
    }
M
Minghao Li 已提交
524 525
  } else {
    syncNodeLog2("syncNodeOnSnapshotRspCb not leader", pSyncNode);
526 527 528 529
  }

  return 0;
}