• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #5011

03 Apr 2026 03:59PM UTC coverage: 72.3% (+0.008%) from 72.292%
#5011

push

travis-ci

web-flow
merge: from main to 3.0 branch #35067

4053 of 5985 new or added lines in 68 files covered. (67.72%)

732 existing lines in 143 files now uncovered.

257430 of 356056 relevant lines covered (72.3%)

131834103.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

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

16
#define _DEFAULT_SOURCE
17
#include "syncSnapshot.h"
18
#include "syncIndexMgr.h"
19
#include "syncPipeline.h"
20
#include "syncRaftCfg.h"
21
#include "syncRaftLog.h"
22
#include "syncRaftStore.h"
23
#include "syncReplication.h"
24
#include "syncUtil.h"
25
#include "tglobal.h"
26

27
static SyncIndex syncNodeGetSnapBeginIndex(SSyncNode *ths);
28

29
static void syncSnapBufferReset(SSyncSnapBuffer *pBuf) {
160,344,755✔
30
  for (int64_t i = pBuf->start; i < pBuf->end; ++i) {
160,346,845✔
31
    if (pBuf->entryDeleteCb) {
2,090✔
32
      pBuf->entryDeleteCb(pBuf->entries[i % pBuf->size]);
2,090✔
33
    }
34
    pBuf->entries[i % pBuf->size] = NULL;
2,090✔
35
  }
36
  pBuf->start = SYNC_SNAPSHOT_SEQ_BEGIN + 1;
160,345,120✔
37
  pBuf->end = pBuf->start;
160,345,120✔
38
  pBuf->cursor = pBuf->start - 1;
160,347,021✔
39
}
160,346,861✔
40

41
static void syncSnapBufferDestroy(SSyncSnapBuffer **ppBuf) {
80,045,971✔
42
  if (ppBuf == NULL || ppBuf[0] == NULL) return;
80,045,971✔
43
  SSyncSnapBuffer *pBuf = ppBuf[0];
80,045,971✔
44

45
  syncSnapBufferReset(pBuf);
80,046,183✔
46

47
  (void)taosThreadMutexDestroy(&pBuf->mutex);
80,046,269✔
48
  taosMemoryFree(ppBuf[0]);
80,046,455✔
49
  ppBuf[0] = NULL;
80,040,805✔
50
  return;
80,041,710✔
51
}
52

53
static int32_t syncSnapBufferCreate(SSyncSnapBuffer **ppBuf) {
80,215,574✔
54
  SSyncSnapBuffer *pBuf = taosMemoryCalloc(1, sizeof(SSyncSnapBuffer));
80,215,574✔
55
  if (pBuf == NULL) {
80,212,381✔
56
    *ppBuf = NULL;
×
57
    TAOS_RETURN(terrno);
×
58
  }
59
  pBuf->size = sizeof(pBuf->entries) / sizeof(void *);
80,212,381✔
60
  if (pBuf->size != TSDB_SYNC_SNAP_BUFFER_SIZE) return TSDB_CODE_SYN_INTERNAL_ERROR;
80,212,381✔
61
  (void)taosThreadMutexInit(&pBuf->mutex, NULL);
80,211,354✔
62
  *ppBuf = pBuf;
80,213,271✔
63
  TAOS_RETURN(0);
80,213,271✔
64
}
65

66
int32_t snapshotSenderCreate(SSyncNode *pSyncNode, int32_t replicaIndex, SSyncSnapshotSender **ppSender) {
75,240,415✔
67
  int32_t code = 0;
75,240,415✔
68
  *ppSender = NULL;
75,240,415✔
69
  bool condition = (pSyncNode->pFsm->FpSnapshotStartRead != NULL) && (pSyncNode->pFsm->FpSnapshotStopRead != NULL) &&
150,483,240✔
70
                   (pSyncNode->pFsm->FpSnapshotDoRead != NULL);
75,242,270✔
71
  if (!condition) {
75,242,176✔
72
    TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
×
73
  }
74

75
  SSyncSnapshotSender *pSender = taosMemoryCalloc(1, sizeof(SSyncSnapshotSender));
75,242,176✔
76
  if (pSender == NULL) {
75,238,655✔
77
    TAOS_RETURN(terrno);
×
78
  }
79

80
  pSender->start = false;
75,238,655✔
81
  pSender->seq = SYNC_SNAPSHOT_SEQ_INVALID;
75,238,309✔
82
  pSender->ack = SYNC_SNAPSHOT_SEQ_INVALID;
75,238,629✔
83
  pSender->pReader = NULL;
75,239,349✔
84
  pSender->sendingMS = SYNC_SNAPSHOT_RETRY_MS;
75,239,679✔
85
  pSender->pSyncNode = pSyncNode;
75,238,736✔
86
  pSender->replicaIndex = replicaIndex;
75,239,429✔
87
  pSender->term = raftStoreGetTerm(pSyncNode);
75,239,429✔
88
  pSender->senderStartTime = -1;
75,242,809✔
89
  pSender->finish = false;
75,242,809✔
90

91
  code = pSender->pSyncNode->pFsm->FpGetSnapshotInfo(pSender->pSyncNode->pFsm, &pSender->snapshot);
75,243,024✔
92
  if (code != 0) {
75,242,090✔
93
    taosMemoryFreeClear(pSender);
×
94
    TAOS_RETURN(code);
×
95
  }
96
  SSyncSnapBuffer *pSndBuf = NULL;
75,242,090✔
97
  code = syncSnapBufferCreate(&pSndBuf);
75,242,090✔
98
  if (pSndBuf == NULL) {
75,240,768✔
99
    taosMemoryFreeClear(pSender);
×
100
    TAOS_RETURN(code);
×
101
  }
102
  pSndBuf->entryDeleteCb = syncSnapBlockDestroy;
75,240,768✔
103
  pSender->pSndBuf = pSndBuf;
75,240,768✔
104

105
  syncSnapBufferReset(pSender->pSndBuf);
75,240,928✔
106
  *ppSender = pSender;
75,240,664✔
107
  TAOS_RETURN(code);
75,240,664✔
108
}
109

110
void syncSnapBlockDestroy(void *ptr) {
483,759✔
111
  SyncSnapBlock *pBlk = ptr;
483,759✔
112
  if (pBlk->pBlock != NULL) {
483,759✔
113
    taosMemoryFree(pBlk->pBlock);
441,122✔
114
    pBlk->pBlock = NULL;
441,122✔
115
    pBlk->blockLen = 0;
441,122✔
116
  }
117
  taosMemoryFree(pBlk);
483,759✔
118
}
483,759✔
119

120
static int32_t snapshotSenderClearInfoData(SSyncSnapshotSender *pSender) {
75,112,899✔
121
  if (pSender->snapshotParam.data) {
75,112,899✔
122
    taosMemoryFree(pSender->snapshotParam.data);
43,335✔
123
    pSender->snapshotParam.data = NULL;
43,335✔
124
  }
125

126
  if (pSender->snapshot.data) {
75,113,115✔
127
    taosMemoryFree(pSender->snapshot.data);
×
128
    pSender->snapshot.data = NULL;
×
129
  }
130
  return 0;
75,113,624✔
131
}
132

133
void snapshotSenderDestroy(SSyncSnapshotSender *pSender) {
75,072,284✔
134
  if (pSender == NULL) return;
75,072,284✔
135

136
  // close reader
137
  if (pSender->pReader != NULL) {
75,072,284✔
138
    pSender->pSyncNode->pFsm->FpSnapshotStopRead(pSender->pSyncNode->pFsm, pSender->pReader);
×
139
    pSender->pReader = NULL;
×
140
  }
141

142
  // free snap buffer
143
  if (pSender->pSndBuf) {
75,072,284✔
144
    syncSnapBufferDestroy(&pSender->pSndBuf);
75,072,077✔
145
  }
146

147
  (void)snapshotSenderClearInfoData(pSender);
75,068,341✔
148

149
  // free sender
150
  taosMemoryFree(pSender);
75,071,036✔
151
}
152

153
bool snapshotSenderIsStart(SSyncSnapshotSender *pSender) { return atomic_load_8(&pSender->start); }
75,526,084✔
154

155
int32_t snapshotSenderStart(SSyncSnapshotSender *pSender) {
43,335✔
156
  int32_t code = 0;
43,335✔
157

158
  int8_t started = atomic_val_compare_exchange_8(&pSender->start, false, true);
43,335✔
159
  if (started) return 0;
43,335✔
160

161
  pSender->seq = SYNC_SNAPSHOT_SEQ_PREP;
43,335✔
162
  pSender->ack = SYNC_SNAPSHOT_SEQ_INVALID;
43,335✔
163
  pSender->pReader = NULL;
43,335✔
164
  pSender->snapshotParam.start = SYNC_INDEX_INVALID;
43,335✔
165
  pSender->snapshotParam.end = SYNC_INDEX_INVALID;
43,335✔
166
  pSender->snapshot.data = NULL;
43,335✔
167
  pSender->snapshotParam.end = SYNC_INDEX_INVALID;
43,335✔
168
  pSender->snapshot.lastApplyIndex = SYNC_INDEX_INVALID;
43,335✔
169
  pSender->snapshot.lastApplyTerm = SYNC_TERM_INVALID;
43,335✔
170
  pSender->snapshot.lastConfigIndex = SYNC_INDEX_INVALID;
43,335✔
171

172
  (void)memset(&pSender->lastConfig, 0, sizeof(pSender->lastConfig));
43,335✔
173
  pSender->sendingMS = 0;
43,335✔
174
  pSender->term = raftStoreGetTerm(pSender->pSyncNode);
43,335✔
175
  pSender->senderStartTime = taosGetMonoTimestampMs();
43,335✔
176
  pSender->lastSendTime = taosGetTimestampMs();
43,335✔
177
  pSender->finish = false;
43,335✔
178

179
  // Get snapshot info
180
  SSyncNode *pSyncNode = pSender->pSyncNode;
43,335✔
181
  SSnapshot  snapInfo = {.type = TDMT_SYNC_PREP_SNAPSHOT};
43,335✔
182
  if ((code = pSyncNode->pFsm->FpGetSnapshotInfo(pSyncNode->pFsm, &snapInfo)) != 0) {
43,335✔
183
    sSError(pSender, "snapshot get info failure since %s", tstrerror(code));
×
184
    goto _out;
×
185
  }
186

187
  void   *pData = snapInfo.data;
43,335✔
188
  int32_t type = (pData) ? snapInfo.type : 0;
43,335✔
189
  int32_t dataLen = 0;
43,335✔
190
  if (pData) {
43,335✔
191
    SSyncTLV *datHead = pData;
43,335✔
192
    if (datHead->typ != TDMT_SYNC_PREP_SNAPSHOT) {
43,335✔
193
      sSError(pSender, "unexpected data typ in data of snapshot info. typ: %d", datHead->typ);
×
194
      code = TSDB_CODE_INVALID_DATA_FMT;
×
195
      goto _out;
×
196
    }
197
    dataLen = sizeof(SSyncTLV) + datHead->len;
43,335✔
198
  }
199

200
  sInfo("vgId:%d, send msg:%s", pSyncNode->vgId, TMSG_INFO(type));
43,335✔
201
  if ((code = syncSnapSendMsg(pSender, pSender->seq, pData, dataLen, type)) != 0) {
43,335✔
202
    goto _out;
×
203
  }
204

205
  SRaftId destId = pSender->pSyncNode->replicasId[pSender->replicaIndex];
43,335✔
206
  sSInfo(pSender, "snapshot sender start, to dnode:%d.", DID(&destId));
43,335✔
207
_out:
43,335✔
208
  if (snapInfo.data) {
43,335✔
209
    taosMemoryFree(snapInfo.data);
43,335✔
210
    snapInfo.data = NULL;
43,335✔
211
  }
212
  TAOS_RETURN(code);
43,335✔
213
}
214

215
void snapshotSenderStop(SSyncSnapshotSender *pSender, bool finish) {
43,335✔
216
  sSDebug(pSender, "snapshot sender stop, finish:%d reader:%p", finish, pSender->pReader);
43,335✔
217

218
  // update flag
219
  int8_t stopped = !atomic_val_compare_exchange_8(&pSender->start, true, false);
43,335✔
220
  if (stopped) return;
43,335✔
221
  (void)taosThreadMutexLock(&pSender->pSndBuf->mutex);
43,335✔
222
  {
223
    pSender->finish = finish;
43,335✔
224

225
    // close reader
226
    if (pSender->pReader != NULL) {
43,335✔
227
      pSender->pSyncNode->pFsm->FpSnapshotStopRead(pSender->pSyncNode->pFsm, pSender->pReader);
42,637✔
228
      pSender->pReader = NULL;
42,637✔
229
    }
230

231
    syncSnapBufferReset(pSender->pSndBuf);
43,335✔
232

233
    (void)snapshotSenderClearInfoData(pSender);
43,335✔
234

235
    SRaftId destId = pSender->pSyncNode->replicasId[pSender->replicaIndex];
43,335✔
236
    sSInfo(pSender, "snapshot sender stop, to dnode:%d, finish:%d", DID(&destId), finish);
43,335✔
237
  }
238
  (void)taosThreadMutexUnlock(&pSender->pSndBuf->mutex);
43,335✔
239
}
240

241
int32_t syncSnapSendMsg(SSyncSnapshotSender *pSender, int32_t seq, void *pBlock, int32_t blockLen, int32_t typ) {
569,621✔
242
  int32_t code = 0;
569,621✔
243
  SRpcMsg rpcMsg = {0};
569,621✔
244

245
  if ((code = syncBuildSnapshotSend(&rpcMsg, blockLen, pSender->pSyncNode->vgId)) != 0) {
569,621✔
246
    sSError(pSender, "failed to build snap replication msg since %s", tstrerror(code));
×
247
    goto _OUT;
×
248
  }
249

250
  SyncSnapshotSend *pMsg = rpcMsg.pCont;
569,621✔
251
  pMsg->srcId = pSender->pSyncNode->myRaftId;
569,621✔
252
  pMsg->destId = pSender->pSyncNode->replicasId[pSender->replicaIndex];
569,621✔
253
  pMsg->term = pSender->term;
569,621✔
254
  pMsg->beginIndex = pSender->snapshotParam.start;
569,621✔
255
  pMsg->lastIndex = pSender->snapshot.lastApplyIndex;
569,621✔
256
  pMsg->lastTerm = pSender->snapshot.lastApplyTerm;
569,621✔
257
  pMsg->lastConfigIndex = pSender->snapshot.lastConfigIndex;
569,621✔
258
  pMsg->lastConfig = pSender->lastConfig;
569,621✔
259
  pMsg->snapStartTime = pSender->senderStartTime;
569,621✔
260
  pMsg->seq = seq;
569,621✔
261

262
  if (pBlock != NULL && blockLen > 0) {
569,621✔
263
    (void)memcpy(pMsg->data, pBlock, blockLen);
484,457✔
264
  }
265
  pMsg->payloadType = typ;
569,621✔
266

267
  // send msg
268
  if ((code = syncNodeSendMsgById(&pMsg->destId, pSender->pSyncNode, &rpcMsg)) != 0) {
569,621✔
269
    sSError(pSender, "failed to send snap replication msg since %s. seq:%d", tstrerror(code), seq);
×
270
    goto _OUT;
×
271
  }
272

273
_OUT:
569,621✔
274
  TAOS_RETURN(code);
569,621✔
275
}
276

277
// when sender receive ack, call this function to send msg from seq
278
// seq = ack + 1, already updated
279
static int32_t snapshotSend(SSyncSnapshotSender *pSender) {
568,923✔
280
  int32_t        code = 0;
568,923✔
281
  SyncSnapBlock *pBlk = NULL;
568,923✔
282

283
  if (pSender->seq < SYNC_SNAPSHOT_SEQ_END) {
568,923✔
284
    pSender->seq++;
526,396✔
285

286
    if (pSender->seq > SYNC_SNAPSHOT_SEQ_BEGIN) {
526,396✔
287
      pBlk = taosMemoryCalloc(1, sizeof(SyncSnapBlock));
483,759✔
288
      if (pBlk == NULL) {
483,759✔
289
        code = terrno;
×
290
        goto _OUT;
×
291
      }
292

293
      pBlk->seq = pSender->seq;
483,759✔
294

295
      // read data
296
      code = pSender->pSyncNode->pFsm->FpSnapshotDoRead(pSender->pSyncNode->pFsm, pSender->pReader, &pBlk->pBlock,
483,759✔
297
                                                        &pBlk->blockLen);
298
      if (code != 0) {
483,759✔
299
        sSError(pSender, "snapshot sender read failed since %s", tstrerror(code));
×
300
        goto _OUT;
×
301
      }
302

303
      if (pBlk->blockLen > 0) {
483,759✔
304
        // has read data
305
        sSDebug(pSender, "snapshot sender continue to read, blockLen:%d seq:%d", pBlk->blockLen, pBlk->seq);
441,122✔
306
      } else {
307
        // read finish, update seq to end
308
        pSender->seq = SYNC_SNAPSHOT_SEQ_END;
42,637✔
309
        sSInfo(pSender, "snapshot sender read to the end");
42,637✔
310
        goto _OUT;
42,637✔
311
      }
312
    }
313
  }
314

315
  if (!(pSender->seq >= SYNC_SNAPSHOT_SEQ_BEGIN && pSender->seq <= SYNC_SNAPSHOT_SEQ_END)) {
526,286✔
316
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
317
    goto _OUT;
×
318
  }
319

320
  // send msg
321
  int32_t blockLen = (pBlk) ? pBlk->blockLen : 0;
526,286✔
322
  void   *pBlock = (pBlk) ? pBlk->pBlock : NULL;
526,286✔
323
  if ((code = syncSnapSendMsg(pSender, pSender->seq, pBlock, blockLen, 0)) != 0) {
526,286✔
324
    goto _OUT;
×
325
  }
326

327
  // put in buffer
328
  int64_t nowMs = taosGetTimestampMs();
526,286✔
329
  if (pBlk) {
526,286✔
330
    if (!(pBlk->seq > SYNC_SNAPSHOT_SEQ_BEGIN && pBlk->seq < SYNC_SNAPSHOT_SEQ_END)) {
441,122✔
331
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
332
      goto _OUT;
×
333
    }
334
    pBlk->sendTimeMs = nowMs;
441,122✔
335
    pSender->pSndBuf->entries[pSender->seq % pSender->pSndBuf->size] = pBlk;
441,122✔
336
    pBlk = NULL;
441,122✔
337
    pSender->pSndBuf->end = TMAX(pSender->seq + 1, pSender->pSndBuf->end);
441,122✔
338
  }
339
  pSender->lastSendTime = nowMs;
526,286✔
340

341
_OUT:;
568,923✔
342
  if (pBlk != NULL) {
568,923✔
343
    syncSnapBlockDestroy(pBlk);
42,637✔
344
    pBlk = NULL;
42,637✔
345
  }
346
  TAOS_RETURN(code);
568,923✔
347
}
348

349
// send snapshot data from cache
350
int32_t snapshotReSend(SSyncSnapshotSender *pSender) {
×
351
  SSyncSnapBuffer *pSndBuf = pSender->pSndBuf;
×
352
  int32_t          code = 0;
×
353
  (void)taosThreadMutexLock(&pSndBuf->mutex);
×
354
  if (pSender->pReader == NULL || pSender->finish || !snapshotSenderIsStart(pSender)) {
×
355
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
356
    goto _out;
×
357
  }
358

359
  for (int32_t seq = pSndBuf->cursor + 1; seq < pSndBuf->end; ++seq) {
×
360
    SyncSnapBlock *pBlk = pSndBuf->entries[seq % pSndBuf->size];
×
361
    if (!pBlk) {
×
362
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
363
      goto _out;
×
364
    }
365
    int64_t nowMs = taosGetTimestampMs();
×
366
    if (pBlk->acked || nowMs < pBlk->sendTimeMs + SYNC_SNAP_RESEND_MS) {
×
367
      continue;
×
368
    }
369
    if ((code = syncSnapSendMsg(pSender, pBlk->seq, pBlk->pBlock, pBlk->blockLen, 0)) != 0) {
×
370
      goto _out;
×
371
    }
372
    pBlk->sendTimeMs = nowMs;
×
373
  }
374

375
  if (pSender->seq != SYNC_SNAPSHOT_SEQ_END && pSndBuf->end <= pSndBuf->start) {
×
376
    if ((code = snapshotSend(pSender)) != 0) {
×
377
      goto _out;
×
378
    }
379
  }
380

381
  if (pSender->seq == SYNC_SNAPSHOT_SEQ_END && pSndBuf->end <= pSndBuf->start) {
×
382
    if ((code = syncSnapSendMsg(pSender, pSender->seq, NULL, 0, 0)) != 0) {
×
383
      goto _out;
×
384
    }
385
  }
386
_out:;
×
387
  (void)taosThreadMutexUnlock(&pSndBuf->mutex);
×
388
  TAOS_RETURN(code);
×
389
}
390

391
int32_t syncNodeStartSnapshot(SSyncNode *pSyncNode, SRaftId *pDestId, char *reason) {
43,566✔
392
  SSyncSnapshotSender *pSender = syncNodeGetSnapshotSender(pSyncNode, pDestId);
43,566✔
393
  if (pSender == NULL) {
43,566✔
394
    sNError(pSyncNode, "snapshot sender start error since get failed");
×
395
    TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
×
396
  }
397

398
  if (snapshotSenderIsStart(pSender)) {
43,566✔
399
    sSDebug(pSender, "snapshot sender already start, ignore");
231✔
400
    return 0;
231✔
401
  }
402

403
  taosMsleep(1);
43,335✔
404

405
  sInfo("vgId:%d, snapshot replication progress:1/8:leader:1/4 to dnode:%d, reason:%s", pSyncNode->vgId, DID(pDestId),
43,335✔
406
        reason);
407

408
  int32_t code = snapshotSenderStart(pSender);
43,335✔
409
  if (code != 0) {
43,335✔
410
    sSError(pSender, "snapshot sender start error since %s", tstrerror(code));
×
411
    TAOS_RETURN(code);
×
412
  }
413

414
  return 0;
43,335✔
415
}
416

417
// receiver
418
int32_t snapshotReceiverCreate(SSyncNode *pSyncNode, SRaftId fromId, SSyncSnapshotReceiver **ppReceiver) {
4,973,266✔
419
  int32_t code = 0;
4,973,266✔
420
  *ppReceiver = NULL;
4,973,266✔
421
  bool condition = (pSyncNode->pFsm->FpSnapshotStartWrite != NULL) && (pSyncNode->pFsm->FpSnapshotStopWrite != NULL) &&
9,947,286✔
422
                   (pSyncNode->pFsm->FpSnapshotDoWrite != NULL);
4,973,643✔
423
  if (!condition) {
4,973,643✔
424
    TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
×
425
  }
426

427
  SSyncSnapshotReceiver *pReceiver = taosMemoryCalloc(1, sizeof(SSyncSnapshotReceiver));
4,973,643✔
428
  if (pReceiver == NULL) {
4,973,020✔
429
    TAOS_RETURN(terrno);
×
430
  }
431

432
  pReceiver->start = false;
4,973,020✔
433
  pReceiver->receiverStartTime = 0;
4,973,020✔
434
  pReceiver->ack = SYNC_SNAPSHOT_SEQ_BEGIN;
4,973,020✔
435
  pReceiver->pWriter = NULL;
4,973,020✔
436
  code = taosThreadMutexInit(&pReceiver->writerMutex, NULL);
4,973,020✔
437
  if (code != 0) {
4,973,643✔
438
    taosMemoryFree(pReceiver);
×
439
    pReceiver = NULL;
×
440
    TAOS_RETURN(code);
×
441
  }
442
  pReceiver->pSyncNode = pSyncNode;
4,973,643✔
443
  pReceiver->fromId = fromId;
4,973,020✔
444
  pReceiver->term = raftStoreGetTerm(pSyncNode);
4,973,020✔
445
  pReceiver->snapshot.data = NULL;
4,973,020✔
446
  pReceiver->snapshot.lastApplyIndex = SYNC_INDEX_INVALID;
4,973,643✔
447
  pReceiver->snapshot.lastApplyTerm = 0;
4,973,643✔
448
  pReceiver->snapshot.lastConfigIndex = SYNC_INDEX_INVALID;
4,973,020✔
449

450
  SSyncSnapBuffer *pRcvBuf = NULL;
4,973,020✔
451
  code = syncSnapBufferCreate(&pRcvBuf);
4,973,643✔
452
  if (pRcvBuf == NULL) {
4,973,643✔
453
    int32_t ret = taosThreadMutexDestroy(&pReceiver->writerMutex);
×
454
    if (ret != 0) {
×
455
      sError("failed to destroy mutex since %s", tstrerror(ret));
×
456
    }
457
    taosMemoryFree(pReceiver);
×
458
    pReceiver = NULL;
×
459
    TAOS_RETURN(code);
×
460
  }
461
  pRcvBuf->entryDeleteCb = rpcFreeCont;
4,973,643✔
462
  pReceiver->pRcvBuf = pRcvBuf;
4,973,643✔
463

464
  syncSnapBufferReset(pReceiver->pRcvBuf);
4,973,643✔
465
  *ppReceiver = pReceiver;
4,973,643✔
466
  TAOS_RETURN(code);
4,973,643✔
467
}
468

469
static int32_t snapshotReceiverClearInfoData(SSyncSnapshotReceiver *pReceiver) {
5,016,890✔
470
  if (pReceiver->snapshotParam.data) {
5,016,890✔
471
    taosMemoryFree(pReceiver->snapshotParam.data);
43,314✔
472
    pReceiver->snapshotParam.data = NULL;
43,314✔
473
  }
474

475
  if (pReceiver->snapshot.data) {
5,016,890✔
476
    taosMemoryFree(pReceiver->snapshot.data);
×
477
    pReceiver->snapshot.data = NULL;
×
478
  }
479
  return 0;
5,016,890✔
480
}
481

482
void snapshotReceiverDestroy(SSyncSnapshotReceiver *pReceiver) {
4,973,576✔
483
  if (pReceiver == NULL) return;
4,973,576✔
484

485
  (void)taosThreadMutexLock(&pReceiver->writerMutex);
4,973,576✔
486
  // close writer
487
  if (pReceiver->pWriter != NULL) {
4,973,576✔
488
    int32_t code = pReceiver->pSyncNode->pFsm->FpSnapshotStopWrite(pReceiver->pSyncNode->pFsm, pReceiver->pWriter,
×
489
                                                                   false, &pReceiver->snapshot);
490
    if (code != 0) {
×
491
      sError("vgId:%d, snapshot receiver stop failed while destroy since %s", pReceiver->pSyncNode->vgId,
×
492
             tstrerror(code));
493
    }
494
    pReceiver->pWriter = NULL;
×
495
  }
496
  (void)taosThreadMutexUnlock(&pReceiver->writerMutex);
4,973,576✔
497

498
  (void)taosThreadMutexDestroy(&pReceiver->writerMutex);
4,973,576✔
499

500
  // free snap buf
501
  if (pReceiver->pRcvBuf) {
4,973,576✔
502
    syncSnapBufferDestroy(&pReceiver->pRcvBuf);
4,973,576✔
503
  }
504

505
  (void)snapshotReceiverClearInfoData(pReceiver);
4,973,576✔
506

507
  // free receiver
508
  taosMemoryFree(pReceiver);
4,973,576✔
509
}
510

511
bool snapshotReceiverIsStart(SSyncSnapshotReceiver *pReceiver) {
14,086,465✔
512
  return (pReceiver != NULL ? atomic_load_8(&pReceiver->start) : false);
14,086,465✔
513
}
514

515
static int32_t snapshotReceiverSignatureCmp(SSyncSnapshotReceiver *pReceiver, SyncSnapshotSend *pMsg) {
524,777✔
516
  int32_t code = 0;
524,777✔
517
  if (pReceiver->term < pMsg->term) {
524,777✔
518
    code = -1;
×
519
    goto _OVER;
×
520
  }
521
  if (pReceiver->term > pMsg->term) {
524,777✔
522
    code = 1;
×
523
    goto _OVER;
×
524
  }
525
  if (pReceiver->receiverStartTime < pMsg->snapStartTime) {
524,777✔
526
    code = -2;
697✔
527
    goto _OVER;
697✔
528
  }
529
  if (pReceiver->receiverStartTime > pMsg->snapStartTime) {
524,080✔
530
    code = 2;
×
531
    goto _OVER;
×
532
  }
533
_OVER:
524,080✔
534
  if (code > 0) {
524,777✔
535
    sRError(pReceiver, "receiver signature failed, stale snapshot, result:%d, msg signature:(%" PRId64 ", %" PRId64 ")",
×
536
            code, pMsg->term, pMsg->snapStartTime);
537
  } else if (code < 0) {
524,777✔
538
    sRWarn(pReceiver,
697✔
539
           "receiver signature failed, result:%d, a newer snapshot, msg signature:(%" PRId64 ", %" PRId64 ")", code,
540
           pMsg->term, pMsg->snapStartTime);
541
  }
542
  return code;
524,777✔
543
}
544

545
static int32_t snapshotReceiverStartWriter(SSyncSnapshotReceiver *pReceiver, SyncSnapshotSend *pBeginMsg) {
42,617✔
546
  if (pReceiver->pWriter != NULL) {
42,617✔
547
    sRError(pReceiver, "snapshot receiver writer already started before");
×
548
    TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
×
549
  }
550

551
  // update ack
552
  pReceiver->ack = SYNC_SNAPSHOT_SEQ_BEGIN;
42,617✔
553

554
  // update snapshot
555
  pReceiver->snapshot.lastApplyIndex = pBeginMsg->lastIndex;
42,617✔
556
  pReceiver->snapshot.lastApplyTerm = pBeginMsg->lastTerm;
42,617✔
557
  pReceiver->snapshot.lastConfigIndex = pBeginMsg->lastConfigIndex;
42,617✔
558
  pReceiver->snapshotParam.start = pBeginMsg->beginIndex;
42,617✔
559
  pReceiver->snapshotParam.end = pBeginMsg->lastIndex;
42,617✔
560

561
  // start writer
562
  int32_t code = pReceiver->pSyncNode->pFsm->FpSnapshotStartWrite(pReceiver->pSyncNode->pFsm, &pReceiver->snapshotParam,
42,617✔
563
                                                                  &pReceiver->pWriter);
564
  if (code != 0) {
42,617✔
565
    sRError(pReceiver, "snapshot receiver start writer failed since %s", tstrerror(code));
×
566
    TAOS_RETURN(code);
×
567
  }
568

569
  // event log
570
  sRInfo(pReceiver, "snapshot receiver writer started");
42,617✔
571
  return 0;
42,617✔
572
}
573

574
void snapshotReceiverStart(SSyncSnapshotReceiver *pReceiver, SyncSnapshotSend *pPreMsg) {
43,314✔
575
  if (snapshotReceiverIsStart(pReceiver)) {
43,314✔
576
    sRInfo(pReceiver, "snapshot receiver has started");
×
577
    return;
×
578
  }
579

580
  int8_t started = atomic_val_compare_exchange_8(&pReceiver->start, false, true);
43,314✔
581
  if (started) return;
43,314✔
582

583
  pReceiver->ack = SYNC_SNAPSHOT_SEQ_PREP;
43,314✔
584
  pReceiver->term = pPreMsg->term;
43,314✔
585
  pReceiver->fromId = pPreMsg->srcId;
43,314✔
586
  pReceiver->receiverStartTime = pPreMsg->snapStartTime;
43,314✔
587

588
  pReceiver->snapshotParam.start = syncNodeGetSnapBeginIndex(pReceiver->pSyncNode);
43,314✔
589
  pReceiver->snapshotParam.end = -1;
43,314✔
590

591
  sRInfo(pReceiver, "snapshot receiver start, from dnode:%d.", DID(&pReceiver->fromId));
43,314✔
592
}
593

594
void snapshotReceiverStop(SSyncSnapshotReceiver *pReceiver) {
43,314✔
595
  sRDebug(pReceiver, "snapshot receiver stop, not apply, writer:%p", pReceiver->pWriter);
43,314✔
596

597
  int8_t stopped = !atomic_val_compare_exchange_8(&pReceiver->start, true, false);
43,314✔
598
  if (stopped) return;
43,314✔
599

600
  (void)taosThreadMutexLock(&pReceiver->writerMutex);
43,314✔
601
  {
602
    if (pReceiver->pWriter != NULL) {
43,314✔
603
      int32_t code = pReceiver->pSyncNode->pFsm->FpSnapshotStopWrite(pReceiver->pSyncNode->pFsm, pReceiver->pWriter,
110✔
604
                                                                     false, &pReceiver->snapshot);
605
      if (code != 0) {
110✔
606
        sRError(pReceiver, "snapshot receiver stop write failed since %s", tstrerror(code));
×
607
      }
608
      pReceiver->pWriter = NULL;
110✔
609
    } else {
610
      sRInfo(pReceiver, "snapshot receiver stop, writer is null");
43,204✔
611
    }
612
  }
613
  (void)taosThreadMutexUnlock(&pReceiver->writerMutex);
43,314✔
614

615
  (void)taosThreadMutexLock(&pReceiver->pRcvBuf->mutex);
43,314✔
616
  {
617
    syncSnapBufferReset(pReceiver->pRcvBuf);
43,314✔
618

619
    (void)snapshotReceiverClearInfoData(pReceiver);
43,314✔
620
  }
621
  (void)taosThreadMutexUnlock(&pReceiver->pRcvBuf->mutex);
43,314✔
622
}
623

624
static int32_t snapshotReceiverFinish(SSyncSnapshotReceiver *pReceiver, SyncSnapshotSend *pMsg) {
42,507✔
625
  int32_t code = 0;
42,507✔
626
  if (pReceiver->pWriter != NULL) {
42,507✔
627
    // write data
628
    sRInfo(pReceiver, "snapshot receiver write about to finish, blockLen:%d seq:%d", pMsg->dataLen, pMsg->seq);
42,507✔
629
    if (pMsg->dataLen > 0) {
42,507✔
630
      (void)taosThreadMutexLock(&pReceiver->writerMutex);
×
631
      code = pReceiver->pSyncNode->pFsm->FpSnapshotDoWrite(pReceiver->pSyncNode->pFsm, pReceiver->pWriter, pMsg->data,
×
632
                                                           pMsg->dataLen);
×
633
      (void)taosThreadMutexUnlock(&pReceiver->writerMutex);
×
634
      if (code != 0) {
×
635
        sRError(pReceiver, "failed to finish snapshot receiver write since %s", tstrerror(code));
×
636
        TAOS_RETURN(code);
×
637
      }
638
    }
639

640
    // update commit index
641
    if (pReceiver->snapshot.lastApplyIndex > pReceiver->pSyncNode->commitIndex) {
42,507✔
642
      pReceiver->pSyncNode->commitIndex = pReceiver->snapshot.lastApplyIndex;
42,507✔
643
    }
644

645
    // maybe update term
646
    if (pReceiver->snapshot.lastApplyTerm > raftStoreGetTerm(pReceiver->pSyncNode)) {
42,507✔
647
      raftStoreSetTerm(pReceiver->pSyncNode, pReceiver->snapshot.lastApplyTerm);
×
648
    }
649

650
    (void)taosThreadMutexLock(&pReceiver->writerMutex);
42,507✔
651
    if (pReceiver->pWriter != NULL) {
42,507✔
652
      // stop writer, apply data
653
      code = pReceiver->pSyncNode->pFsm->FpSnapshotStopWrite(pReceiver->pSyncNode->pFsm, pReceiver->pWriter, true,
42,507✔
654
                                                             &pReceiver->snapshot);
655
      if (code != 0) {
42,507✔
656
        sRError(pReceiver, "snapshot receiver apply failed since %s", tstrerror(code));
×
657
        TAOS_RETURN(code);
×
658
      }
659
      pReceiver->pWriter = NULL;
42,507✔
660
      sRInfo(pReceiver, "snapshot receiver write stopped");
42,507✔
661
    }
662
    (void)taosThreadMutexUnlock(&pReceiver->writerMutex);
42,507✔
663

664
    // update progress
665
    pReceiver->ack = SYNC_SNAPSHOT_SEQ_END;
42,507✔
666

667
    // get fsmState
668
    SSnapshot snapshot = {0};
42,507✔
669
    code = pReceiver->pSyncNode->pFsm->FpGetSnapshotInfo(pReceiver->pSyncNode->pFsm, &snapshot);
42,507✔
670
    if (code != 0) {
42,507✔
671
      sRError(pReceiver, "snapshot receiver get snapshot info failed since %s", tstrerror(code));
×
672
      TAOS_RETURN(code);
×
673
    }
674
    pReceiver->pSyncNode->fsmState = snapshot.state;
42,507✔
675

676
    // reset wal
677
    code =
678
        pReceiver->pSyncNode->pLogStore->syncLogRestoreFromSnapshot(pReceiver->pSyncNode->pLogStore, pMsg->lastIndex);
42,507✔
679
    if (code != 0) {
42,507✔
680
      sRError(pReceiver, "failed to snapshot receiver log restore since %s", tstrerror(code));
×
681
      TAOS_RETURN(code);
×
682
    }
683
    sRInfo(pReceiver, "wal log restored from snapshot");
42,507✔
684
  } else {
685
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
686
    sRError(pReceiver, "snapshot receiver finish error since writer is null");
×
687
    TAOS_RETURN(code);
×
688
  }
689

690
  return 0;
42,507✔
691
}
692

693
static int32_t snapshotReceiverGotData(SSyncSnapshotReceiver *pReceiver, SyncSnapshotSend *pMsg) {
438,956✔
694
  if (pMsg->seq != pReceiver->ack + 1) {
438,956✔
695
    sRError(pReceiver, "snapshot receiver invalid seq, ack:%d seq:%d", pReceiver->ack, pMsg->seq);
×
696
    TAOS_RETURN(TSDB_CODE_SYN_INVALID_SNAPSHOT_MSG);
×
697
  }
698

699
  (void)taosThreadMutexLock(&pReceiver->writerMutex);
438,956✔
700

701
  if (pReceiver->pWriter == NULL) {
438,956✔
UNCOV
702
    (void)taosThreadMutexUnlock(&pReceiver->writerMutex);
×
UNCOV
703
    sRError(pReceiver, "snapshot receiver failed to write data since writer is null");
×
UNCOV
704
    TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
×
705
  }
706

707
  sRDebug(pReceiver, "snapshot receiver continue to write, blockLen:%d seq:%d", pMsg->dataLen, pMsg->seq);
438,956✔
708

709
  if (pMsg->dataLen > 0) {
438,956✔
710
    // apply data block
711
    int32_t code = pReceiver->pSyncNode->pFsm->FpSnapshotDoWrite(pReceiver->pSyncNode->pFsm, pReceiver->pWriter,
877,912✔
712
                                                                 pMsg->data, pMsg->dataLen);
438,956✔
713
    if (code != 0) {
438,956✔
714
      (void)taosThreadMutexUnlock(&pReceiver->writerMutex);
×
715
      sRError(pReceiver, "snapshot receiver continue write failed since %s", tstrerror(code));
×
716
      TAOS_RETURN(code);
×
717
    }
718
  }
719

720
  (void)taosThreadMutexUnlock(&pReceiver->writerMutex);
438,956✔
721

722
  // update progress
723
  pReceiver->ack = pMsg->seq;
438,956✔
724

725
  // event log
726
  sRDebug(pReceiver, "snapshot receiver continue to write finish");
438,956✔
727
  return 0;
438,956✔
728
}
729

730
SyncIndex syncNodeGetSnapBeginIndex(SSyncNode *ths) {
85,931✔
731
  SyncIndex snapStart = SYNC_INDEX_INVALID;
85,931✔
732

733
  if (syncNodeIsMnode(ths)) {
85,931✔
734
    snapStart = SYNC_INDEX_BEGIN;
×
735
    sNInfo(ths, "snapshot begin index is %" PRId64 " since its mnode", snapStart);
×
736
  } else {
737
    SSyncLogStoreData *pData = ths->pLogStore->data;
85,931✔
738
    SWal              *pWal = pData->pWal;
85,931✔
739

740
    int64_t walCommitVer = walGetCommittedVer(pWal);
85,931✔
741
    snapStart = TMAX(ths->commitIndex, walCommitVer) + 1;
85,931✔
742

743
    sNInfo(ths, "snapshot begin index is %" PRId64, snapStart);
85,931✔
744
  }
745

746
  return snapStart;
85,931✔
747
}
748

749
static int32_t syncSnapReceiverExchgSnapInfo(SSyncNode *pSyncNode, SSyncSnapshotReceiver *pReceiver,
43,314✔
750
                                             SyncSnapshotSend *pMsg, SSnapshot *pInfo) {
751
  if (pMsg->payloadType != TDMT_SYNC_PREP_SNAPSHOT) return TSDB_CODE_SYN_INTERNAL_ERROR;
43,314✔
752
  int32_t code = 0, lino = 0;
43,314✔
753

754
  // copy snap info from leader
755
  void *data = taosMemoryCalloc(1, pMsg->dataLen);
43,314✔
756
  if (data == NULL) {
43,314✔
757
    TAOS_CHECK_EXIT(terrno);
×
758
  }
759
  pInfo->data = data;
43,314✔
760
  data = NULL;
43,314✔
761
  (void)memcpy(pInfo->data, pMsg->data, pMsg->dataLen);
43,314✔
762

763
  // exchange snap info
764
  if ((code = pSyncNode->pFsm->FpGetSnapshotInfo(pSyncNode->pFsm, pInfo)) != 0) {
43,314✔
765
    sRError(pReceiver, "failed to get snapshot info. type: %d", pMsg->payloadType);
×
766
    goto _exit;
×
767
  }
768
  SSyncTLV *datHead = pInfo->data;
43,314✔
769
  if (datHead->typ != TDMT_SYNC_PREP_SNAPSHOT_REPLY) {
43,314✔
770
    sRError(pReceiver, "unexpected data typ in data of snapshot info. typ: %d", datHead->typ);
×
771
    code = TSDB_CODE_INVALID_DATA_FMT;
×
772
    goto _exit;
×
773
  }
774
  int32_t dataLen = sizeof(SSyncTLV) + datHead->len;
43,314✔
775

776
  // save exchanged snap info
777
  SSnapshotParam *pParam = &pReceiver->snapshotParam;
43,314✔
778
  data = taosMemoryRealloc(pParam->data, dataLen);
43,314✔
779
  if (data == NULL) {
43,314✔
780
    code = terrno;
×
781
    sError("vgId:%d, failed to realloc memory for snapshot prep due to %s. dataLen:%d", pSyncNode->vgId,
×
782
           tstrerror(code), dataLen);
783
    goto _exit;
×
784
  }
785
  pParam->data = data;
43,314✔
786
  data = NULL;
43,314✔
787
  (void)memcpy(pParam->data, pInfo->data, dataLen);
43,314✔
788

789
_exit:
43,314✔
790
  TAOS_RETURN(code);
43,314✔
791
}
792

793
static int32_t syncNodeOnSnapshotPrep(SSyncNode *pSyncNode, SyncSnapshotSend *pMsg) {
43,314✔
794
  SSyncSnapshotReceiver *pReceiver = pSyncNode->pNewNodeReceiver;
43,314✔
795
  int64_t                timeNow = taosGetTimestampMs();
43,314✔
796
  int32_t                code = 0;
43,314✔
797

798
  if (snapshotReceiverIsStart(pReceiver)) {
43,314✔
799
    // already start
800
    int32_t order = 0;
697✔
801
    if ((order = snapshotReceiverSignatureCmp(pReceiver, pMsg)) < 0) {  // order < 0
697✔
802
      sWarn("failed to prepare snapshot, received a new snapshot preparation. restart receiver.");
697✔
803
      goto _START_RECEIVER;
697✔
804
    } else if (order == 0) {  // order == 0
×
805
      sInfo("prepare snapshot, received a duplicate snapshot preparation. send reply.");
×
806
      goto _SEND_REPLY;
×
807
    } else {  // order > 0
808
      // ignore
809
      sError("failed to prepare snapshot, received a stale snapshot preparation. ignore.");
×
810
      code = TSDB_CODE_SYN_MISMATCHED_SIGNATURE;
×
811
      goto _SEND_REPLY;
×
812
    }
813
  } else {
814
    // start new
815
    sRInfo(pReceiver, "snapshot receiver not start yet so start new one");
42,617✔
816
    goto _START_RECEIVER;
42,617✔
817
  }
818

819
_START_RECEIVER:
43,314✔
820
  if (snapshotReceiverIsStart(pReceiver)) {
43,314✔
821
    sRInfo(pReceiver, "snapshot receiver already start and force stop pre one");
697✔
822
    snapshotReceiverStop(pReceiver);
697✔
823
  }
824

825
  snapshotReceiverStart(pReceiver, pMsg);
43,314✔
826

827
_SEND_REPLY:;
43,314✔
828

829
  SSnapshot snapInfo = {.type = TDMT_SYNC_PREP_SNAPSHOT_REPLY};
43,314✔
830
  int32_t   dataLen = 0;
43,314✔
831
  if (pMsg->payloadType == TDMT_SYNC_PREP_SNAPSHOT) {
43,314✔
832
    if ((code = syncSnapReceiverExchgSnapInfo(pSyncNode, pReceiver, pMsg, &snapInfo)) != 0) {
43,314✔
833
      goto _out;
×
834
    }
835
    SSyncTLV *datHead = snapInfo.data;
43,314✔
836
    dataLen = sizeof(SSyncTLV) + datHead->len;
43,314✔
837
  }
838

839
  // send response
840
  int32_t type = (snapInfo.data) ? snapInfo.type : 0;
43,314✔
841
  if ((code = syncSnapSendRsp(pReceiver, pMsg, snapInfo.data, dataLen, type, code)) != 0) {
43,314✔
842
    goto _out;
×
843
  }
844

845
_out:
43,314✔
846
  if (snapInfo.data) {
43,314✔
847
    taosMemoryFree(snapInfo.data);
43,314✔
848
    snapInfo.data = NULL;
43,314✔
849
  }
850
  TAOS_RETURN(code);
43,314✔
851
}
852

853
static int32_t syncNodeOnSnapshotBegin(SSyncNode *pSyncNode, SyncSnapshotSend *pMsg) {
42,617✔
854
  // condition 1
855
  SSyncSnapshotReceiver *pReceiver = pSyncNode->pNewNodeReceiver;
42,617✔
856
  int32_t                code = TSDB_CODE_SYN_INTERNAL_ERROR;
42,617✔
857

858
  if (!snapshotReceiverIsStart(pReceiver)) {
42,617✔
859
    sRError(pReceiver, "failed to begin snapshot receiver since not started");
×
860
    goto _SEND_REPLY;
×
861
  }
862

863
  if (snapshotReceiverSignatureCmp(pReceiver, pMsg) != 0) {
42,617✔
864
    code = TSDB_CODE_SYN_MISMATCHED_SIGNATURE;
×
865
    sError("failed to begin snapshot, since %s", tstrerror(code));
×
866
    goto _SEND_REPLY;
×
867
  }
868

869
  // start writer
870
  if ((code = snapshotReceiverStartWriter(pReceiver, pMsg)) != 0) {
42,617✔
871
    sRError(pReceiver, "failed to start snapshot writer since %s", tstrerror(code));
×
872
    goto _SEND_REPLY;
×
873
  }
874

875
  SyncIndex beginIndex = syncNodeGetSnapBeginIndex(pSyncNode);
42,617✔
876
  if (pReceiver->snapshotParam.start != beginIndex) {
42,617✔
877
    sRError(pReceiver, "snapshot begin index is changed unexpectedly. sver:%" PRId64 ", beginIndex:%" PRId64,
×
878
            pReceiver->snapshotParam.start, beginIndex);
879
    goto _SEND_REPLY;
×
880
  }
881

882
  code = 0;
42,617✔
883
_SEND_REPLY:
42,617✔
884

885
  // send response
886
  TAOS_CHECK_RETURN(syncSnapSendRsp(pReceiver, pMsg, NULL, 0, 0, code));
42,617✔
887

888
  TAOS_RETURN(code);
42,617✔
889
}
890

891
int32_t syncSnapSendRsp(SSyncSnapshotReceiver *pReceiver, SyncSnapshotSend *pMsg, void *pBlock, int32_t blockLen,
524,887✔
892
                        int32_t type, int32_t rspCode) {
893
  int32_t    code = 0;
524,887✔
894
  SSyncNode *pSyncNode = pReceiver->pSyncNode;
524,887✔
895
  // build msg
896
  SRpcMsg rpcMsg = {0};
524,887✔
897
  if ((code = syncBuildSnapshotSendRsp(&rpcMsg, blockLen, pSyncNode->vgId)) != 0) {
524,887✔
898
    sRError(pReceiver, "failed to build snapshot receiver resp since %s", tstrerror(code));
×
899
    TAOS_RETURN(code);
×
900
  }
901

902
  SyncSnapshotRsp *pRspMsg = rpcMsg.pCont;
524,887✔
903
  pRspMsg->srcId = pSyncNode->myRaftId;
524,887✔
904
  pRspMsg->destId = pMsg->srcId;
524,887✔
905
  pRspMsg->term = pMsg->term;
524,887✔
906
  pRspMsg->lastIndex = pMsg->lastIndex;
524,887✔
907
  pRspMsg->lastTerm = pMsg->lastTerm;
524,887✔
908
  pRspMsg->startTime = pMsg->snapStartTime;
524,887✔
909
  pRspMsg->ack = pMsg->seq;
524,887✔
910
  pRspMsg->code = rspCode;
524,887✔
911
  pRspMsg->snapBeginIndex = pReceiver->snapshotParam.start;
524,887✔
912
  pRspMsg->payloadType = type;
524,887✔
913

914
  if (pBlock != NULL && blockLen > 0) {
524,887✔
915
    (void)memcpy(pRspMsg->data, pBlock, blockLen);
43,314✔
916
  }
917

918
  // send msg
919
  if ((code = syncNodeSendMsgById(&pRspMsg->destId, pSyncNode, &rpcMsg)) != 0) {
524,887✔
920
    sRError(pReceiver, "failed to send snapshot receiver resp since %s", tstrerror(code));
×
921
    TAOS_RETURN(code);
×
922
  }
923
  return 0;
524,887✔
924
}
925

926
static int32_t syncSnapBufferRecv(SSyncSnapshotReceiver *pReceiver, SyncSnapshotSend **ppMsg) {
438,956✔
927
  int32_t           code = 0;
438,956✔
928
  SSyncSnapBuffer  *pRcvBuf = pReceiver->pRcvBuf;
438,956✔
929
  SyncSnapshotSend *pMsg = ppMsg[0];
438,956✔
930

931
  (void)taosThreadMutexLock(&pRcvBuf->mutex);
438,956✔
932

933
  if (pMsg->seq - pRcvBuf->start >= pRcvBuf->size) {
438,956✔
934
    code = TSDB_CODE_SYN_BUFFER_FULL;
×
935
    goto _out;
×
936
  }
937

938
  if (!(pRcvBuf->start <= pRcvBuf->cursor + 1 && pRcvBuf->cursor < pRcvBuf->end)) return TSDB_CODE_SYN_INTERNAL_ERROR;
438,956✔
939

940
  if (pMsg->seq > pRcvBuf->cursor) {
438,956✔
941
    if (pRcvBuf->entries[pMsg->seq % pRcvBuf->size]) {
438,956✔
942
      pRcvBuf->entryDeleteCb(pRcvBuf->entries[pMsg->seq % pRcvBuf->size]);
×
943
    }
944
    pRcvBuf->entries[pMsg->seq % pRcvBuf->size] = pMsg;
438,956✔
945
    ppMsg[0] = NULL;
438,956✔
946
    pRcvBuf->end = TMAX(pMsg->seq + 1, pRcvBuf->end);
438,956✔
947
  } else if (pMsg->seq < pRcvBuf->start) {
×
948
    code = syncSnapSendRsp(pReceiver, pMsg, NULL, 0, 0, code);
×
949
    goto _out;
×
950
  }
951

952
  for (int64_t seq = pRcvBuf->cursor + 1; seq < pRcvBuf->end; ++seq) {
877,912✔
953
    if (pRcvBuf->entries[seq % pRcvBuf->size]) {
603,380✔
954
      pRcvBuf->cursor = seq;
438,956✔
955
    } else {
956
      break;
164,424✔
957
    }
958
  }
959

960
  for (int64_t seq = pRcvBuf->start; seq <= pRcvBuf->cursor; ++seq) {
877,912✔
961
    if ((code = snapshotReceiverGotData(pReceiver, pRcvBuf->entries[seq % pRcvBuf->size])) != 0) {
438,956✔
UNCOV
962
      if (code >= SYNC_SNAPSHOT_SEQ_INVALID) {
×
963
        code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
964
      }
965
    }
966
    pRcvBuf->start = seq + 1;
438,956✔
967
    if (syncSnapSendRsp(pReceiver, pRcvBuf->entries[seq % pRcvBuf->size], NULL, 0, 0, code) != 0) {
438,956✔
968
      sError("failed to send snap rsp");
×
969
    }
970
    pRcvBuf->entryDeleteCb(pRcvBuf->entries[seq % pRcvBuf->size]);
438,956✔
971
    pRcvBuf->entries[seq % pRcvBuf->size] = NULL;
438,956✔
972
    if (code) goto _out;
438,956✔
973
  }
974

975
_out:
438,956✔
976
  (void)taosThreadMutexUnlock(&pRcvBuf->mutex);
438,956✔
977
  TAOS_RETURN(code);
438,956✔
978
}
979

980
static int32_t syncNodeOnSnapshotReceive(SSyncNode *pSyncNode, SyncSnapshotSend **ppMsg) {
438,956✔
981
  // condition 4
982
  // transfering
983
  SyncSnapshotSend *pMsg = ppMsg[0];
438,956✔
984
  if (!pMsg) return TSDB_CODE_SYN_INTERNAL_ERROR;
438,956✔
985
  SSyncSnapshotReceiver *pReceiver = pSyncNode->pNewNodeReceiver;
438,956✔
986
  int64_t                timeNow = taosGetTimestampMs();
438,956✔
987
  int32_t                code = 0;
438,956✔
988

989
  if (snapshotReceiverSignatureCmp(pReceiver, pMsg) != 0) {
438,956✔
990
    code = TSDB_CODE_SYN_MISMATCHED_SIGNATURE;
×
991
    sError("failed to receive snapshot data, since %s", tstrerror(code));
×
992
    return syncSnapSendRsp(pReceiver, pMsg, NULL, 0, 0, code);
×
993
  }
994

995
  return syncSnapBufferRecv(pReceiver, ppMsg);
438,956✔
996
}
997

998
static int32_t syncNodeOnSnapshotEnd(SSyncNode *pSyncNode, SyncSnapshotSend *pMsg) {
42,507✔
999
  // condition 2
1000
  // end, finish FSM
1001
  SSyncSnapshotReceiver *pReceiver = pSyncNode->pNewNodeReceiver;
42,507✔
1002
  int64_t                timeNow = taosGetTimestampMs();
42,507✔
1003
  int32_t                code = 0;
42,507✔
1004

1005
  if (snapshotReceiverSignatureCmp(pReceiver, pMsg) != 0) {
42,507✔
1006
    code = TSDB_CODE_SYN_MISMATCHED_SIGNATURE;
×
1007
    sError("failed to end snapshot, since %s", tstrerror(code));
×
1008
    goto _SEND_REPLY;
×
1009
  }
1010

1011
  code = snapshotReceiverFinish(pReceiver, pMsg);
42,507✔
1012
  if (code == 0) {
42,507✔
1013
    snapshotReceiverStop(pReceiver);
42,507✔
1014
  }
1015

1016
_SEND_REPLY:;
×
1017

1018
  // build msg
1019
  SRpcMsg rpcMsg = {0};
42,507✔
1020
  if ((code = syncBuildSnapshotSendRsp(&rpcMsg, 0, pSyncNode->vgId)) != 0) {
42,507✔
1021
    sRError(pReceiver, "snapshot receiver build rsp failed since %s", tstrerror(code));
×
1022
    TAOS_RETURN(code);
×
1023
  }
1024

1025
  SyncSnapshotRsp *pRspMsg = rpcMsg.pCont;
42,507✔
1026
  pRspMsg->srcId = pSyncNode->myRaftId;
42,507✔
1027
  pRspMsg->destId = pMsg->srcId;
42,507✔
1028
  pRspMsg->term = raftStoreGetTerm(pSyncNode);
42,507✔
1029
  pRspMsg->lastIndex = pMsg->lastIndex;
42,507✔
1030
  pRspMsg->lastTerm = pMsg->lastTerm;
42,507✔
1031
  pRspMsg->startTime = pMsg->snapStartTime;
42,507✔
1032
  pRspMsg->ack = pReceiver->ack;  // receiver maybe already closed
42,507✔
1033
  pRspMsg->code = code;
42,507✔
1034
  pRspMsg->snapBeginIndex = pReceiver->snapshotParam.start;
42,507✔
1035

1036
  // send msg
1037
  syncLogSendSyncSnapshotRsp(pSyncNode, pRspMsg, "snapshot receiver end", &rpcMsg.info.traceId);
42,507✔
1038
  if ((code = syncNodeSendMsgById(&pRspMsg->destId, pSyncNode, &rpcMsg)) != 0) {
42,507✔
1039
    sRError(pReceiver, "snapshot receiver send rsp failed since %s", tstrerror(code));
×
1040
    TAOS_RETURN(code);
×
1041
  }
1042

1043
  TAOS_RETURN(code);
42,507✔
1044
}
1045

1046
int64_t lastRecvPrintLog = 0;
1047

1048
int32_t syncNodeOnSnapshot(SSyncNode *pSyncNode, SRpcMsg *pRpcMsg) {
567,394✔
1049
  SyncSnapshotSend     **ppMsg = (SyncSnapshotSend **)&pRpcMsg->pCont;
567,394✔
1050
  SyncSnapshotSend      *pMsg = ppMsg[0];
567,394✔
1051
  SSyncSnapshotReceiver *pReceiver = pSyncNode->pNewNodeReceiver;
567,394✔
1052
  int32_t                code = 0;
567,394✔
1053

1054
  // if already drop replica, do not process
1055
  if (!syncNodeInRaftGroup(pSyncNode, &pMsg->srcId)) {
567,394✔
1056
    syncLogRecvSyncSnapshotSend(pSyncNode, pMsg, "not in my config", &pRpcMsg->info.traceId);
×
1057
    code = TSDB_CODE_SYN_NOT_IN_RAFT_GROUP;
×
1058
    TAOS_RETURN(code);
×
1059
  }
1060

1061
  if (pMsg->term < raftStoreGetTerm(pSyncNode)) {
567,394✔
1062
    sRError(pReceiver, "reject snap replication with smaller term. msg term:%" PRId64 ", seq:%d", pMsg->term,
×
1063
            pMsg->seq);
1064
    code = TSDB_CODE_SYN_TERM_NOT_MATCH;
×
1065
    if (syncSnapSendRsp(pReceiver, pMsg, NULL, 0, 0, code) != 0) sError("failed to send snap rsp");
×
1066
    TAOS_RETURN(code);
×
1067
  }
1068

1069
  if (pSyncNode->raftCfg.cfg.nodeInfo[pSyncNode->raftCfg.cfg.myIndex].nodeRole != TAOS_SYNC_ROLE_LEARNER) {
567,394✔
1070
    if (pMsg->term > raftStoreGetTerm(pSyncNode)) {
265,073✔
1071
      syncNodeStepDown(pSyncNode, pMsg->term, pMsg->srcId, "snapshot");
×
1072
    }
1073
  } else {
1074
    syncNodeUpdateTermWithoutStepDown(pSyncNode, pMsg->term);
302,321✔
1075
  }
1076

1077
  if (pSyncNode->state != TAOS_SYNC_STATE_FOLLOWER && pSyncNode->state != TAOS_SYNC_STATE_LEARNER) {
567,394✔
1078
    sRError(pReceiver, "snapshot receiver not a follower or learner");
×
1079
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
1080
    TAOS_RETURN(code);
×
1081
  }
1082

1083
  if (pMsg->seq < SYNC_SNAPSHOT_SEQ_PREP || pMsg->seq > SYNC_SNAPSHOT_SEQ_END) {
567,394✔
1084
    sRError(pReceiver, "snap replication msg with invalid seq:%d", pMsg->seq);
×
1085
    code = TSDB_CODE_SYN_INVALID_SNAPSHOT_MSG;
×
1086
    TAOS_RETURN(code);
×
1087
  }
1088

1089
  // prepare
1090
  if (pMsg->seq == SYNC_SNAPSHOT_SEQ_PREP) {
567,394✔
1091
    sInfo(
43,314✔
1092
        "vgId:%d, snapshot replication progress:2/8:follower:1/4, start to prepare, recv msg:%s, snap seq:%d, msg "
1093
        "signature:(%" PRId64 ", %" PRId64 ")",
1094
        pSyncNode->vgId, TMSG_INFO(pRpcMsg->msgType), pMsg->seq, pMsg->term, pMsg->snapStartTime);
1095
    pSyncNode->snapSeq = pMsg->seq;
43,314✔
1096
    code = syncNodeOnSnapshotPrep(pSyncNode, pMsg);
43,314✔
1097
    sDebug(
43,314✔
1098
        "vgId:%d, snapshot replication progress:2/8:follower:1/4, finish to prepare, recv msg:%s, snap seq:%d, msg "
1099
        "signature:(%" PRId64 ", %" PRId64 ")",
1100
        pSyncNode->vgId, TMSG_INFO(pRpcMsg->msgType), pMsg->seq, pMsg->term, pMsg->snapStartTime);
1101
    goto _out;
43,314✔
1102
  }
1103

1104
  // begin
1105
  if (pMsg->seq == SYNC_SNAPSHOT_SEQ_BEGIN) {
524,080✔
1106
    sInfo("vgId:%d, snapshot replication progress:4/8:follower:2/4, start to begin,replication. msg signature:(%" PRId64
42,617✔
1107
          ", %" PRId64 "), snapshot msg seq:%d",
1108
          pSyncNode->vgId, pMsg->term, pMsg->snapStartTime, pMsg->seq);
1109
    pSyncNode->snapSeq = pMsg->seq;
42,617✔
1110
    code = syncNodeOnSnapshotBegin(pSyncNode, pMsg);
42,617✔
1111
    sDebug("vgId:%d, snapshot replication progress:4/8:follower:2/4, finish to begin. msg signature:(%" PRId64
42,617✔
1112
           ", %" PRId64 ")",
1113
           pSyncNode->vgId, pMsg->term, pMsg->snapStartTime);
1114
    goto _out;
42,617✔
1115
  }
1116

1117
  // data
1118
  if (pMsg->seq > SYNC_SNAPSHOT_SEQ_BEGIN && pMsg->seq < SYNC_SNAPSHOT_SEQ_END) {
481,463✔
1119
    int64_t currentTimestamp = taosGetTimestampMs()/1000;
438,956✔
1120
    if (currentTimestamp > lastRecvPrintLog) {
438,956✔
1121
      sInfo("vgId:%d, snapshot replication progress:6/8:follower:3/4, start to receive. msg signature:(%" PRId64
44,193✔
1122
            ", %" PRId64 "), snapshot msg seq:%d",
1123
            pSyncNode->vgId, pMsg->term, pMsg->snapStartTime, pMsg->seq);
1124

1125
    } else {
1126
      sDebug("vgId:%d, snapshot replication progress:6/8:follower:3/4, start to receive. msg signature:(%" PRId64
394,763✔
1127
             ", %" PRId64 "), snapshot msg seq:%d",
1128
             pSyncNode->vgId, pMsg->term, pMsg->snapStartTime, pMsg->seq);
1129
    }
1130
    pSyncNode->snapSeq = pMsg->seq;
438,956✔
1131
    lastRecvPrintLog = currentTimestamp;
438,956✔
1132
    code = syncNodeOnSnapshotReceive(pSyncNode, ppMsg);
438,956✔
1133
    sDebug("vgId:%d, snapshot replication progress:6/8:follower:3/4, finish to receive.", pSyncNode->vgId);
438,956✔
1134
    goto _out;
438,956✔
1135
  }
1136

1137
  // end
1138
  if (pMsg->seq == SYNC_SNAPSHOT_SEQ_END) {
42,507✔
1139
    sInfo("vgId:%d, snapshot replication progress:7/8:follower:4/4, start to end. msg signature:(%" PRId64 ", %" PRId64
42,507✔
1140
          "), snapshot msg seq:%d",
1141
          pSyncNode->vgId, pMsg->term, pMsg->snapStartTime, pMsg->seq);
1142
    pSyncNode->snapSeq = pMsg->seq;
42,507✔
1143
    code = syncNodeOnSnapshotEnd(pSyncNode, pMsg);
42,507✔
1144
    if (code != 0) {
42,507✔
1145
      sRError(pReceiver, "failed to end snapshot.");
×
1146
      goto _out;
×
1147
    }
1148

1149
    code = syncLogBufferReInit(pSyncNode->pLogBuf, pSyncNode);
42,507✔
1150
    if (code != 0) {
42,507✔
1151
      sRError(pReceiver, "failed to reinit log buffer since %s", tstrerror(code));
×
1152
    }
1153
    sDebug("vgId:%d, snapshot replication progress:7/7:follower:4/4, finish to end. msg signature:(%" PRId64
42,507✔
1154
           ", %" PRId64 ")",
1155
           pSyncNode->vgId, pMsg->term, pMsg->snapStartTime);
1156
    goto _out;
42,507✔
1157
  }
1158

1159
_out:;
×
1160
  syncNodeResetElectTimer(pSyncNode);
567,394✔
1161
  TAOS_RETURN(code);
567,394✔
1162
}
1163

1164
static int32_t syncSnapSenderExchgSnapInfo(SSyncNode *pSyncNode, SSyncSnapshotSender *pSender, SyncSnapshotRsp *pMsg) {
43,335✔
1165
  if (pMsg->payloadType != TDMT_SYNC_PREP_SNAPSHOT_REPLY) return TSDB_CODE_SYN_INTERNAL_ERROR;
43,335✔
1166

1167
  SSyncTLV *datHead = (void *)pMsg->data;
43,335✔
1168
  if (datHead->typ != pMsg->payloadType) {
43,335✔
1169
    sSError(pSender, "unexpected data type in data of SyncSnapshotRsp. typ: %d", datHead->typ);
×
1170
    TAOS_RETURN(TSDB_CODE_INVALID_DATA_FMT);
×
1171
  }
1172
  int32_t dataLen = sizeof(SSyncTLV) + datHead->len;
43,335✔
1173

1174
  SSnapshotParam *pParam = &pSender->snapshotParam;
43,335✔
1175
  void           *data = taosMemoryRealloc(pParam->data, dataLen);
43,335✔
1176
  if (data == NULL) {
43,335✔
1177
    TAOS_RETURN(terrno);
×
1178
  }
1179
  (void)memcpy(data, pMsg->data, dataLen);
43,335✔
1180

1181
  pParam->data = data;
43,335✔
1182
  data = NULL;
43,335✔
1183
  sSInfo(pSender, "data of snapshot param. len: %d", datHead->len);
43,335✔
1184
  return 0;
43,335✔
1185
}
1186

1187
// sender
1188
static int32_t syncNodeOnSnapshotPrepRsp(SSyncNode *pSyncNode, SSyncSnapshotSender *pSender, SyncSnapshotRsp *pMsg) {
43,335✔
1189
  int32_t   code = 0;
43,335✔
1190
  SSnapshot snapshot = {0};
43,335✔
1191

1192
  if (pMsg->snapBeginIndex > pSyncNode->commitIndex + 1) {
43,335✔
1193
    sSError(pSender,
×
1194
            "snapshot begin index is greater than commit index. msg snapBeginIndex:%" PRId64
1195
            ", node commitIndex:%" PRId64,
1196
            pMsg->snapBeginIndex, pSyncNode->commitIndex);
1197
    TAOS_RETURN(TSDB_CODE_SYN_INVALID_SNAPSHOT_MSG);
×
1198
  }
1199

1200
  (void)taosThreadMutexLock(&pSender->pSndBuf->mutex);
43,335✔
1201
  TAOS_CHECK_GOTO(pSyncNode->pFsm->FpGetSnapshotInfo(pSyncNode->pFsm, &snapshot), NULL, _out);
43,335✔
1202

1203
  // prepare <begin, end>
1204
  pSender->snapshotParam.start = pMsg->snapBeginIndex;
43,335✔
1205
  pSender->snapshotParam.end = snapshot.lastApplyIndex;
43,335✔
1206

1207
  sSInfo(pSender, "prepare snapshot, recv-begin:%" PRId64 ", snapshot.last:%" PRId64 ", snapshot.term:%" PRId64,
43,335✔
1208
         pMsg->snapBeginIndex, snapshot.lastApplyIndex, snapshot.lastApplyTerm);
1209

1210
  // update sender
1211
  pSender->snapshot = snapshot;
43,335✔
1212

1213
  // start reader
1214
  if (pMsg->payloadType == TDMT_SYNC_PREP_SNAPSHOT_REPLY) {
43,335✔
1215
    TAOS_CHECK_GOTO(syncSnapSenderExchgSnapInfo(pSyncNode, pSender, pMsg), NULL, _out);
43,335✔
1216
  }
1217

1218
  code = pSyncNode->pFsm->FpSnapshotStartRead(pSyncNode->pFsm, &pSender->snapshotParam, &pSender->pReader);
43,335✔
1219
  if (code != 0) {
43,335✔
1220
    sSError(pSender, "prepare snapshot failed since %s", tstrerror(code));
698✔
1221
    goto _out;
698✔
1222
  }
1223

1224
  // update next index
1225
  syncIndexMgrSetIndex(pSyncNode->pNextIndex, &pMsg->srcId, snapshot.lastApplyIndex + 1);
42,637✔
1226

1227
  code = snapshotSend(pSender);
42,637✔
1228

1229
_out:
43,335✔
1230
  (void)taosThreadMutexUnlock(&pSender->pSndBuf->mutex);
43,335✔
1231
  TAOS_RETURN(code);
43,335✔
1232
}
1233

1234
static int32_t snapshotSenderSignatureCmp(SSyncSnapshotSender *pSender, SyncSnapshotRsp *pMsg) {
1,049,200✔
1235
  int32_t code = 0;
1,049,200✔
1236
  if (pSender->term < pMsg->term) return -1;
1,049,200✔
1237
  if (pSender->term > pMsg->term) return 1;
1,049,200✔
1238
  if (pSender->senderStartTime < pMsg->startTime) return -2;
1,049,200✔
1239
  if (pSender->senderStartTime > pMsg->startTime) return 2;
1,049,200✔
1240
  if (code != 0)
1,049,200✔
1241
    sSError(pSender, "sender signature failed, result:%d, msg signature:(%" PRId64 ", %" PRId64 ")", code, pMsg->term,
×
1242
            pMsg->startTime);
1243
  return 0;
1,049,200✔
1244
}
1245

1246
static int32_t syncSnapBufferSend(SSyncSnapshotSender *pSender, SyncSnapshotRsp **ppMsg) {
481,669✔
1247
  int32_t          code = 0;
481,669✔
1248
  SSyncSnapBuffer *pSndBuf = pSender->pSndBuf;
481,669✔
1249
  SyncSnapshotRsp *pMsg = ppMsg[0];
481,669✔
1250

1251
  (void)taosThreadMutexLock(&pSndBuf->mutex);
481,669✔
1252
  if (snapshotSenderSignatureCmp(pSender, pMsg) != 0) {
481,669✔
1253
    code = TSDB_CODE_SYN_MISMATCHED_SIGNATURE;
×
1254
    sError("failed to send snapshot data, since %s", tstrerror(code));
×
1255
    goto _out;
×
1256
  }
1257

1258
  if (pSender->pReader == NULL || pSender->finish || !snapshotSenderIsStart(pSender)) {
481,669✔
1259
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
1260
    goto _out;
×
1261
  }
1262

1263
  if (pMsg->ack - pSndBuf->start >= pSndBuf->size) {
481,669✔
1264
    code = TSDB_CODE_SYN_BUFFER_FULL;
×
1265
    goto _out;
×
1266
  }
1267

1268
  if (!(pSndBuf->start <= pSndBuf->cursor + 1 && pSndBuf->cursor < pSndBuf->end)) {
481,669✔
1269
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
1270
    goto _out;
×
1271
  }
1272

1273
  if (pMsg->ack > pSndBuf->cursor && pMsg->ack < pSndBuf->end) {
481,669✔
1274
    SyncSnapBlock *pBlk = pSndBuf->entries[pMsg->ack % pSndBuf->size];
439,032✔
1275
    if (!pBlk) {
439,032✔
1276
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
1277
      goto _out;
×
1278
    }
1279
    pBlk->acked = 1;
439,032✔
1280
  }
1281

1282
  for (int64_t ack = pSndBuf->cursor + 1; ack < pSndBuf->end; ++ack) {
920,701✔
1283
    SyncSnapBlock *pBlk = pSndBuf->entries[ack % pSndBuf->size];
835,537✔
1284
    if (pBlk->acked) {
835,537✔
1285
      pSndBuf->cursor = ack;
439,032✔
1286
    } else {
1287
      break;
396,505✔
1288
    }
1289
  }
1290

1291
  for (int64_t ack = pSndBuf->start; ack <= pSndBuf->cursor; ++ack) {
920,701✔
1292
    pSndBuf->entryDeleteCb(pSndBuf->entries[ack % pSndBuf->size]);
439,032✔
1293
    pSndBuf->entries[ack % pSndBuf->size] = NULL;
439,032✔
1294
    pSndBuf->start = ack + 1;
439,032✔
1295
  }
1296

1297
  while (pSender->seq != SYNC_SNAPSHOT_SEQ_END && pSender->seq - pSndBuf->start < tsSnapReplMaxWaitN) {
965,428✔
1298
    if ((code = snapshotSend(pSender)) != 0) {
483,759✔
1299
      goto _out;
×
1300
    }
1301
  }
1302

1303
  if (pSender->seq == SYNC_SNAPSHOT_SEQ_END && pSndBuf->end <= pSndBuf->start) {
481,669✔
1304
    if ((code = snapshotSend(pSender)) != 0) {
42,527✔
1305
      goto _out;
×
1306
    }
1307
  }
1308
_out:
481,669✔
1309
  (void)taosThreadMutexUnlock(&pSndBuf->mutex);
481,669✔
1310
  TAOS_RETURN(code);
481,669✔
1311
}
1312

1313
int64_t lastSendPrintLog = 0;
1314

1315
int32_t syncNodeOnSnapshotRsp(SSyncNode *pSyncNode, SRpcMsg *pRpcMsg) {
567,531✔
1316
  SyncSnapshotRsp **ppMsg = (SyncSnapshotRsp **)&pRpcMsg->pCont;
567,531✔
1317
  SyncSnapshotRsp  *pMsg = ppMsg[0];
567,531✔
1318
  int32_t           code = 0;
567,531✔
1319

1320
  // if already drop replica, do not process
1321
  if (!syncNodeInRaftGroup(pSyncNode, &pMsg->srcId)) {
567,531✔
1322
    syncLogRecvSyncSnapshotRsp(pSyncNode, pMsg, "maybe replica already dropped", &pRpcMsg->info.traceId);
×
1323
    TAOS_RETURN(TSDB_CODE_SYN_NOT_IN_RAFT_GROUP);
×
1324
  }
1325

1326
  // get sender
1327
  SSyncSnapshotSender *pSender = syncNodeGetSnapshotSender(pSyncNode, &pMsg->srcId);
567,531✔
1328
  if (pSender == NULL) {
567,531✔
1329
    syncLogRecvSyncSnapshotRsp(pSyncNode, pMsg, "sender is null", &pRpcMsg->info.traceId);
×
1330
    TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
×
1331
  }
1332

1333
  if (!snapshotSenderIsStart(pSender)) {
567,531✔
1334
    sSError(pSender, "snapshot sender stopped. sender startTime:%" PRId64 ", msg startTime:%" PRId64,
×
1335
            pSender->senderStartTime, pMsg->startTime);
1336
    TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
×
1337
  }
1338

1339
  // check signature
1340
  int32_t order = 0;
567,531✔
1341
  if ((order = snapshotSenderSignatureCmp(pSender, pMsg)) > 0) {
567,531✔
1342
    sError("failed to check snapshot rsp signature, ignore a stale snap rsp.");
×
1343
    TAOS_RETURN(TSDB_CODE_SYN_MISMATCHED_SIGNATURE);
×
1344
  } else if (order < 0) {
567,531✔
1345
    sError("failed to check snapshot rsp signature, snapshot sender is stale. stop");
×
1346
    code = TSDB_CODE_SYN_MISMATCHED_SIGNATURE;
×
1347
    goto _ERROR;
×
1348
  }
1349

1350
  if (pSyncNode->state != TAOS_SYNC_STATE_LEADER && pSyncNode->state != TAOS_SYNC_STATE_ASSIGNED_LEADER) {
567,531✔
1351
    sSError(pSender, "snapshot sender not leader");
×
1352
    code = TSDB_CODE_SYN_NOT_LEADER;
×
1353
    goto _ERROR;
×
1354
  }
1355

1356
  SyncTerm currentTerm = raftStoreGetTerm(pSyncNode);
567,531✔
1357
  if (pMsg->term != currentTerm) {
567,531✔
1358
    sSError(pSender, "snapshot sender term mismatch, msg term:%" PRId64 " currentTerm:%" PRId64, pMsg->term,
×
1359
            currentTerm);
1360
    code = TSDB_CODE_SYN_TERM_NOT_MATCH;
×
1361
    goto _ERROR;
×
1362
  }
1363

1364
  if (pMsg->code != 0) {
567,531✔
1365
    sSError(pSender, "snapshot sender receive error:%s 0x%x and stop sender", tstrerror(pMsg->code), pMsg->code);
×
1366
    code = pMsg->code;
×
1367
    goto _ERROR;
×
1368
  }
1369

1370
  // send begin
1371
  if (pMsg->ack == SYNC_SNAPSHOT_SEQ_PREP) {
567,531✔
1372
    sSInfo(pSender, "snapshot replication progress:3/8:leader:2/4, process prepare rsp, msg:%s, snap ack:%d, ",
43,335✔
1373
           TMSG_INFO(pRpcMsg->msgType), pMsg->ack);
1374
    pSyncNode->snapSeq = pMsg->ack;
43,335✔
1375
    if ((code = syncNodeOnSnapshotPrepRsp(pSyncNode, pSender, pMsg)) != 0) {
43,335✔
1376
      goto _ERROR;
698✔
1377
    }
1378
  }
1379

1380
  // send msg of data or end
1381
  if (pMsg->ack >= SYNC_SNAPSHOT_SEQ_BEGIN && pMsg->ack < SYNC_SNAPSHOT_SEQ_END) {
566,833✔
1382
    int64_t currentTimestamp = taosGetTimestampMs()/1000;
481,669✔
1383
    if (currentTimestamp > lastSendPrintLog) {
481,669✔
1384
      sSInfo(pSender, "snapshot replication progress:5/8:leader:3/4, send buffer, msg:%s, snap ack:%d",
44,691✔
1385
             TMSG_INFO(pRpcMsg->msgType), pMsg->ack);
1386
    } else {
1387
      sSDebug(pSender, "snapshot replication progress:5/8:leader:3/4, send buffer, msg:%s, snap ack:%d",
436,978✔
1388
              TMSG_INFO(pRpcMsg->msgType), pMsg->ack);
1389
    }
1390
    lastSendPrintLog = currentTimestamp;
481,669✔
1391
    pSyncNode->snapSeq = pMsg->ack;
481,669✔
1392
    if ((code = syncSnapBufferSend(pSender, ppMsg)) != 0) {
481,669✔
1393
      sSError(pSender, "failed to replicate snap since %s. seq:%d, pReader:%p, finish:%d", tstrerror(code),
×
1394
              pSender->seq, pSender->pReader, pSender->finish);
1395
      goto _ERROR;
×
1396
    }
1397
  }
1398

1399
  // end
1400
  if (pMsg->ack == SYNC_SNAPSHOT_SEQ_END) {
566,833✔
1401
    sSInfo(pSender, "snapshot replication progress:8/8:leader:4/4, process end rsp");
42,527✔
1402
    pSyncNode->snapSeq = pMsg->ack;
42,527✔
1403
    snapshotSenderStop(pSender, true);
42,527✔
1404
    TAOS_CHECK_GOTO(syncNodeReplicateReset(pSyncNode, &pMsg->srcId), NULL, _ERROR);
42,527✔
1405
  }
1406

1407
  return 0;
566,833✔
1408

1409
_ERROR:
698✔
1410
  snapshotSenderStop(pSender, false);
698✔
1411
  if (syncNodeReplicateReset(pSyncNode, &pMsg->srcId) != 0) sError("failed to reset replicate");
698✔
1412
  TAOS_RETURN(code);
698✔
1413
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc