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

taosdata / TDengine / #4887

16 Dec 2025 08:27AM UTC coverage: 65.289% (-0.003%) from 65.292%
#4887

push

travis-ci

web-flow
feat[TS-7233]: audit (#33850)

377 of 536 new or added lines in 28 files covered. (70.34%)

1025 existing lines in 111 files now uncovered.

178977 of 274129 relevant lines covered (65.29%)

102580217.43 hits per line

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

71.45
/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) {
151,330,299✔
30
  for (int64_t i = pBuf->start; i < pBuf->end; ++i) {
151,341,114✔
31
    if (pBuf->entryDeleteCb) {
10,815✔
32
      pBuf->entryDeleteCb(pBuf->entries[i % pBuf->size]);
10,815✔
33
    }
34
    pBuf->entries[i % pBuf->size] = NULL;
10,815✔
35
  }
36
  pBuf->start = SYNC_SNAPSHOT_SEQ_BEGIN + 1;
151,331,769✔
37
  pBuf->end = pBuf->start;
151,330,703✔
38
  pBuf->cursor = pBuf->start - 1;
151,331,181✔
39
}
151,330,081✔
40

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

45
  syncSnapBufferReset(pBuf);
75,536,785✔
46

47
  (void)taosThreadMutexDestroy(&pBuf->mutex);
75,535,592✔
48
  taosMemoryFree(ppBuf[0]);
75,535,301✔
49
  ppBuf[0] = NULL;
75,533,351✔
50
  return;
75,533,642✔
51
}
52

53
static int32_t syncSnapBufferCreate(SSyncSnapBuffer **ppBuf) {
75,754,553✔
54
  SSyncSnapBuffer *pBuf = taosMemoryCalloc(1, sizeof(SSyncSnapBuffer));
75,754,553✔
55
  if (pBuf == NULL) {
75,751,092✔
56
    *ppBuf = NULL;
×
57
    TAOS_RETURN(terrno);
×
58
  }
59
  pBuf->size = sizeof(pBuf->entries) / sizeof(void *);
75,751,092✔
60
  if (pBuf->size != TSDB_SYNC_SNAP_BUFFER_SIZE) return TSDB_CODE_SYN_INTERNAL_ERROR;
75,751,092✔
61
  (void)taosThreadMutexInit(&pBuf->mutex, NULL);
75,751,740✔
62
  *ppBuf = pBuf;
75,754,333✔
63
  TAOS_RETURN(0);
75,754,333✔
64
}
65

66
int32_t snapshotSenderCreate(SSyncNode *pSyncNode, int32_t replicaIndex, SSyncSnapshotSender **ppSender) {
71,096,012✔
67
  int32_t code = 0;
71,096,012✔
68
  *ppSender = NULL;
71,096,012✔
69
  bool condition = (pSyncNode->pFsm->FpSnapshotStartRead != NULL) && (pSyncNode->pFsm->FpSnapshotStopRead != NULL) &&
142,192,336✔
70
                   (pSyncNode->pFsm->FpSnapshotDoRead != NULL);
71,098,150✔
71
  if (!condition) {
71,096,753✔
72
    TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
×
73
  }
74

75
  SSyncSnapshotSender *pSender = taosMemoryCalloc(1, sizeof(SSyncSnapshotSender));
71,096,753✔
76
  if (pSender == NULL) {
71,092,442✔
77
    TAOS_RETURN(terrno);
×
78
  }
79

80
  pSender->start = false;
71,092,442✔
81
  pSender->seq = SYNC_SNAPSHOT_SEQ_INVALID;
71,092,711✔
82
  pSender->ack = SYNC_SNAPSHOT_SEQ_INVALID;
71,092,711✔
83
  pSender->pReader = NULL;
71,092,711✔
84
  pSender->sendingMS = SYNC_SNAPSHOT_RETRY_MS;
71,092,711✔
85
  pSender->pSyncNode = pSyncNode;
71,093,484✔
86
  pSender->replicaIndex = replicaIndex;
71,093,484✔
87
  pSender->term = raftStoreGetTerm(pSyncNode);
71,095,336✔
88
  pSender->senderStartTime = -1;
71,098,430✔
89
  pSender->finish = false;
71,098,430✔
90

91
  code = pSender->pSyncNode->pFsm->FpGetSnapshotInfo(pSender->pSyncNode->pFsm, &pSender->snapshot);
71,098,430✔
92
  if (code != 0) {
71,098,512✔
93
    taosMemoryFreeClear(pSender);
×
94
    TAOS_RETURN(code);
×
95
  }
96
  SSyncSnapBuffer *pSndBuf = NULL;
71,098,512✔
97
  code = syncSnapBufferCreate(&pSndBuf);
71,098,512✔
98
  if (pSndBuf == NULL) {
71,097,040✔
99
    taosMemoryFreeClear(pSender);
×
100
    TAOS_RETURN(code);
×
101
  }
102
  pSndBuf->entryDeleteCb = syncSnapBlockDestroy;
71,097,040✔
103
  pSender->pSndBuf = pSndBuf;
71,097,040✔
104

105
  syncSnapBufferReset(pSender->pSndBuf);
71,097,813✔
106
  *ppSender = pSender;
71,094,371✔
107
  TAOS_RETURN(code);
71,094,371✔
108
}
109

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

120
static int32_t snapshotSenderClearInfoData(SSyncSnapshotSender *pSender) {
70,899,316✔
121
  if (pSender->snapshotParam.data) {
70,899,316✔
122
    taosMemoryFree(pSender->snapshotParam.data);
20,700✔
123
    pSender->snapshotParam.data = NULL;
20,700✔
124
  }
125

126
  if (pSender->snapshot.data) {
70,899,316✔
127
    taosMemoryFree(pSender->snapshot.data);
×
128
    pSender->snapshot.data = NULL;
×
129
  }
130
  return 0;
70,898,988✔
131
}
132

133
void snapshotSenderDestroy(SSyncSnapshotSender *pSender) {
70,878,983✔
134
  if (pSender == NULL) return;
70,878,983✔
135

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

142
  // free snap buffer
143
  if (pSender->pSndBuf) {
70,878,983✔
144
    syncSnapBufferDestroy(&pSender->pSndBuf);
70,879,207✔
145
  }
146

147
  (void)snapshotSenderClearInfoData(pSender);
70,877,020✔
148

149
  // free sender
150
  taosMemoryFree(pSender);
70,878,288✔
151
}
152

153
bool snapshotSenderIsStart(SSyncSnapshotSender *pSender) { return atomic_load_8(&pSender->start); }
70,274,046✔
154

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

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

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

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

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

187
  void   *pData = snapInfo.data;
20,700✔
188
  int32_t type = (pData) ? snapInfo.type : 0;
20,700✔
189
  int32_t dataLen = 0;
20,700✔
190
  if (pData) {
20,700✔
191
    SSyncTLV *datHead = pData;
20,700✔
192
    if (datHead->typ != TDMT_SYNC_PREP_SNAPSHOT) {
20,700✔
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;
20,700✔
198
  }
199

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

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

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

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

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

231
    syncSnapBufferReset(pSender->pSndBuf);
20,700✔
232

233
    (void)snapshotSenderClearInfoData(pSender);
20,700✔
234

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

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

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

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

262
  if (pBlock != NULL && blockLen > 0) {
336,745✔
263
    (void)memcpy(pMsg->data, pBlock, blockLen);
295,654✔
264
  }
265
  pMsg->payloadType = typ;
336,745✔
266

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

273
_OUT:
336,745✔
274
  TAOS_RETURN(code);
336,745✔
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) {
336,745✔
280
  int32_t        code = 0;
336,745✔
281
  SyncSnapBlock *pBlk = NULL;
336,745✔
282

283
  if (pSender->seq < SYNC_SNAPSHOT_SEQ_END) {
336,745✔
284
    pSender->seq++;
316,354✔
285

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

293
      pBlk->seq = pSender->seq;
295,654✔
294

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

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

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

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

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

341
_OUT:;
336,745✔
342
  if (pBlk != NULL) {
336,745✔
343
    syncSnapBlockDestroy(pBlk);
20,700✔
344
    pBlk = NULL;
20,700✔
345
  }
346
  TAOS_RETURN(code);
336,745✔
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) {
20,700✔
392
  SSyncSnapshotSender *pSender = syncNodeGetSnapshotSender(pSyncNode, pDestId);
20,700✔
393
  if (pSender == NULL) {
20,700✔
394
    sNError(pSyncNode, "snapshot sender start error since get failed");
×
395
    TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
×
396
  }
397

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

403
  taosMsleep(1);
20,700✔
404

405
  int32_t code = snapshotSenderStart(pSender);
20,700✔
406
  if (code != 0) {
20,700✔
407
    sSError(pSender, "snapshot sender start error since %s", tstrerror(code));
×
408
    TAOS_RETURN(code);
×
409
  }
410

411
  return 0;
20,700✔
412
}
413

414
// receiver
415
int32_t snapshotReceiverCreate(SSyncNode *pSyncNode, SRaftId fromId, SSyncSnapshotReceiver **ppReceiver) {
4,656,531✔
416
  int32_t code = 0;
4,656,531✔
417
  *ppReceiver = NULL;
4,656,531✔
418
  bool condition = (pSyncNode->pFsm->FpSnapshotStartWrite != NULL) && (pSyncNode->pFsm->FpSnapshotStopWrite != NULL) &&
9,313,648✔
419
                   (pSyncNode->pFsm->FpSnapshotDoWrite != NULL);
4,656,824✔
420
  if (!condition) {
4,656,824✔
421
    TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
×
422
  }
423

424
  SSyncSnapshotReceiver *pReceiver = taosMemoryCalloc(1, sizeof(SSyncSnapshotReceiver));
4,656,824✔
425
  if (pReceiver == NULL) {
4,656,824✔
426
    TAOS_RETURN(terrno);
×
427
  }
428

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

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

461
  syncSnapBufferReset(pReceiver->pRcvBuf);
4,656,463✔
462
  *ppReceiver = pReceiver;
4,656,824✔
463
  TAOS_RETURN(code);
4,656,824✔
464
}
465

466
static int32_t snapshotReceiverClearInfoData(SSyncSnapshotReceiver *pReceiver) {
4,677,386✔
467
  if (pReceiver->snapshotParam.data) {
4,677,386✔
468
    taosMemoryFree(pReceiver->snapshotParam.data);
20,679✔
469
    pReceiver->snapshotParam.data = NULL;
20,679✔
470
  }
471

472
  if (pReceiver->snapshot.data) {
4,677,386✔
473
    taosMemoryFree(pReceiver->snapshot.data);
×
474
    pReceiver->snapshot.data = NULL;
×
475
  }
476
  return 0;
4,676,822✔
477
}
478

479
void snapshotReceiverDestroy(SSyncSnapshotReceiver *pReceiver) {
4,656,707✔
480
  if (pReceiver == NULL) return;
4,656,707✔
481

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

495
  (void)taosThreadMutexDestroy(&pReceiver->writerMutex);
4,656,707✔
496

497
  // free snap buf
498
  if (pReceiver->pRcvBuf) {
4,656,707✔
499
    syncSnapBufferDestroy(&pReceiver->pRcvBuf);
4,656,707✔
500
  }
501

502
  (void)snapshotReceiverClearInfoData(pReceiver);
4,656,707✔
503

504
  // free receiver
505
  taosMemoryFree(pReceiver);
4,656,707✔
506
}
507

508
bool snapshotReceiverIsStart(SSyncSnapshotReceiver *pReceiver) {
13,017,069✔
509
  return (pReceiver != NULL ? atomic_load_8(&pReceiver->start) : false);
13,017,069✔
510
}
511

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

542
static int32_t snapshotReceiverStartWriter(SSyncSnapshotReceiver *pReceiver, SyncSnapshotSend *pBeginMsg) {
20,679✔
543
  if (pReceiver->pWriter != NULL) {
20,679✔
544
    sRError(pReceiver, "snapshot receiver writer already started before");
×
545
    TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
×
546
  }
547

548
  // update ack
549
  pReceiver->ack = SYNC_SNAPSHOT_SEQ_BEGIN;
20,679✔
550

551
  // update snapshot
552
  pReceiver->snapshot.lastApplyIndex = pBeginMsg->lastIndex;
20,679✔
553
  pReceiver->snapshot.lastApplyTerm = pBeginMsg->lastTerm;
20,679✔
554
  pReceiver->snapshot.lastConfigIndex = pBeginMsg->lastConfigIndex;
20,679✔
555
  pReceiver->snapshotParam.start = pBeginMsg->beginIndex;
20,679✔
556
  pReceiver->snapshotParam.end = pBeginMsg->lastIndex;
20,679✔
557

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

566
  // event log
567
  sRInfo(pReceiver, "snapshot receiver writer started");
20,679✔
568
  return 0;
20,679✔
569
}
570

571
void snapshotReceiverStart(SSyncSnapshotReceiver *pReceiver, SyncSnapshotSend *pPreMsg) {
20,679✔
572
  if (snapshotReceiverIsStart(pReceiver)) {
20,679✔
573
    sRInfo(pReceiver, "snapshot receiver has started");
×
574
    return;
×
575
  }
576

577
  int8_t started = atomic_val_compare_exchange_8(&pReceiver->start, false, true);
20,679✔
578
  if (started) return;
20,679✔
579

580
  pReceiver->ack = SYNC_SNAPSHOT_SEQ_PREP;
20,679✔
581
  pReceiver->term = pPreMsg->term;
20,679✔
582
  pReceiver->fromId = pPreMsg->srcId;
20,679✔
583
  pReceiver->receiverStartTime = pPreMsg->snapStartTime;
20,679✔
584

585
  pReceiver->snapshotParam.start = syncNodeGetSnapBeginIndex(pReceiver->pSyncNode);
20,679✔
586
  pReceiver->snapshotParam.end = -1;
20,679✔
587

588
  sRInfo(pReceiver, "snapshot receiver start, from dnode:%d.", DID(&pReceiver->fromId));
20,679✔
589
}
590

591
void snapshotReceiverStop(SSyncSnapshotReceiver *pReceiver) {
20,679✔
592
  sRDebug(pReceiver, "snapshot receiver stop, not apply, writer:%p", pReceiver->pWriter);
20,679✔
593

594
  int8_t stopped = !atomic_val_compare_exchange_8(&pReceiver->start, true, false);
20,679✔
595
  if (stopped) return;
20,679✔
596

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

612
  (void)taosThreadMutexLock(&pReceiver->pRcvBuf->mutex);
20,679✔
613
  {
614
    syncSnapBufferReset(pReceiver->pRcvBuf);
20,679✔
615

616
    (void)snapshotReceiverClearInfoData(pReceiver);
20,679✔
617
  }
618
  (void)taosThreadMutexUnlock(&pReceiver->pRcvBuf->mutex);
20,679✔
619
}
620

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

637
    // update commit index
638
    if (pReceiver->snapshot.lastApplyIndex > pReceiver->pSyncNode->commitIndex) {
20,370✔
639
      pReceiver->pSyncNode->commitIndex = pReceiver->snapshot.lastApplyIndex;
20,370✔
640
    }
641

642
    // maybe update term
643
    if (pReceiver->snapshot.lastApplyTerm > raftStoreGetTerm(pReceiver->pSyncNode)) {
20,370✔
644
      raftStoreSetTerm(pReceiver->pSyncNode, pReceiver->snapshot.lastApplyTerm);
×
645
    }
646

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

661
    // update progress
662
    pReceiver->ack = SYNC_SNAPSHOT_SEQ_END;
20,370✔
663

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

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

687
  return 0;
20,370✔
688
}
689

690
static int32_t snapshotReceiverGotData(SSyncSnapshotReceiver *pReceiver, SyncSnapshotSend *pMsg) {
270,701✔
691
  if (pMsg->seq != pReceiver->ack + 1) {
270,701✔
UNCOV
692
    sRError(pReceiver, "snapshot receiver invalid seq, ack:%d seq:%d", pReceiver->ack, pMsg->seq);
×
UNCOV
693
    TAOS_RETURN(TSDB_CODE_SYN_INVALID_SNAPSHOT_MSG);
×
694
  }
695

696
  (void)taosThreadMutexLock(&pReceiver->writerMutex);
270,701✔
697

698
  if (pReceiver->pWriter == NULL) {
270,701✔
699
    (void)taosThreadMutexUnlock(&pReceiver->writerMutex);
309✔
700
    sRError(pReceiver, "snapshot receiver failed to write data since writer is null");
309✔
701
    TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
309✔
702
  }
703

704
  sRDebug(pReceiver, "snapshot receiver continue to write, blockLen:%d seq:%d", pMsg->dataLen, pMsg->seq);
270,392✔
705

706
  if (pMsg->dataLen > 0) {
270,392✔
707
    // apply data block
708
    int32_t code = pReceiver->pSyncNode->pFsm->FpSnapshotDoWrite(pReceiver->pSyncNode->pFsm, pReceiver->pWriter,
540,784✔
709
                                                                 pMsg->data, pMsg->dataLen);
270,392✔
710
    if (code != 0) {
270,392✔
711
      (void)taosThreadMutexUnlock(&pReceiver->writerMutex);
×
712
      sRError(pReceiver, "snapshot receiver continue write failed since %s", tstrerror(code));
×
713
      TAOS_RETURN(code);
×
714
    }
715
  }
716

717
  (void)taosThreadMutexUnlock(&pReceiver->writerMutex);
270,392✔
718

719
  // update progress
720
  pReceiver->ack = pMsg->seq;
270,392✔
721

722
  // event log
723
  sRDebug(pReceiver, "snapshot receiver continue to write finish");
270,392✔
724
  return 0;
270,392✔
725
}
726

727
SyncIndex syncNodeGetSnapBeginIndex(SSyncNode *ths) {
41,358✔
728
  SyncIndex snapStart = SYNC_INDEX_INVALID;
41,358✔
729

730
  if (syncNodeIsMnode(ths)) {
41,358✔
731
    snapStart = SYNC_INDEX_BEGIN;
×
732
    sNInfo(ths, "snapshot begin index is %" PRId64 " since its mnode", snapStart);
×
733
  } else {
734
    SSyncLogStoreData *pData = ths->pLogStore->data;
41,358✔
735
    SWal              *pWal = pData->pWal;
41,358✔
736

737
    int64_t walCommitVer = walGetCommittedVer(pWal);
41,358✔
738
    snapStart = TMAX(ths->commitIndex, walCommitVer) + 1;
41,358✔
739

740
    sNInfo(ths, "snapshot begin index is %" PRId64, snapStart);
41,358✔
741
  }
742

743
  return snapStart;
41,358✔
744
}
745

746
static int32_t syncSnapReceiverExchgSnapInfo(SSyncNode *pSyncNode, SSyncSnapshotReceiver *pReceiver,
20,679✔
747
                                             SyncSnapshotSend *pMsg, SSnapshot *pInfo) {
748
  if (pMsg->payloadType != TDMT_SYNC_PREP_SNAPSHOT) return TSDB_CODE_SYN_INTERNAL_ERROR;
20,679✔
749
  int32_t code = 0, lino = 0;
20,679✔
750

751
  // copy snap info from leader
752
  void *data = taosMemoryCalloc(1, pMsg->dataLen);
20,679✔
753
  if (data == NULL) {
20,679✔
754
    TAOS_CHECK_EXIT(terrno);
×
755
  }
756
  pInfo->data = data;
20,679✔
757
  data = NULL;
20,679✔
758
  (void)memcpy(pInfo->data, pMsg->data, pMsg->dataLen);
20,679✔
759

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

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

786
_exit:
20,679✔
787
  TAOS_RETURN(code);
20,679✔
788
}
789

790
static int32_t syncNodeOnSnapshotPrep(SSyncNode *pSyncNode, SyncSnapshotSend *pMsg) {
20,679✔
791
  SSyncSnapshotReceiver *pReceiver = pSyncNode->pNewNodeReceiver;
20,679✔
792
  int64_t                timeNow = taosGetTimestampMs();
20,679✔
793
  int32_t                code = 0;
20,679✔
794

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

816
_START_RECEIVER:
20,679✔
817
  if (snapshotReceiverIsStart(pReceiver)) {
20,679✔
818
    sRInfo(pReceiver, "snapshot receiver already start and force stop pre one");
×
819
    snapshotReceiverStop(pReceiver);
×
820
  }
821

822
  snapshotReceiverStart(pReceiver, pMsg);
20,679✔
823

824
_SEND_REPLY:;
20,679✔
825

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

836
  // send response
837
  int32_t type = (snapInfo.data) ? snapInfo.type : 0;
20,679✔
838
  if ((code = syncSnapSendRsp(pReceiver, pMsg, snapInfo.data, dataLen, type, code)) != 0) {
20,679✔
839
    goto _out;
×
840
  }
841

842
_out:
20,679✔
843
  if (snapInfo.data) {
20,679✔
844
    taosMemoryFree(snapInfo.data);
20,679✔
845
    snapInfo.data = NULL;
20,679✔
846
  }
847
  TAOS_RETURN(code);
20,679✔
848
}
849

850
static int32_t syncNodeOnSnapshotBegin(SSyncNode *pSyncNode, SyncSnapshotSend *pMsg) {
20,679✔
851
  // condition 1
852
  SSyncSnapshotReceiver *pReceiver = pSyncNode->pNewNodeReceiver;
20,679✔
853
  int32_t                code = TSDB_CODE_SYN_INTERNAL_ERROR;
20,679✔
854

855
  if (!snapshotReceiverIsStart(pReceiver)) {
20,679✔
856
    sRError(pReceiver, "failed to begin snapshot receiver since not started");
×
857
    goto _SEND_REPLY;
×
858
  }
859

860
  if (snapshotReceiverSignatureCmp(pReceiver, pMsg) != 0) {
20,679✔
861
    code = TSDB_CODE_SYN_MISMATCHED_SIGNATURE;
×
862
    sError("failed to begin snapshot, since %s", tstrerror(code));
×
863
    goto _SEND_REPLY;
×
864
  }
865

866
  // start writer
867
  if ((code = snapshotReceiverStartWriter(pReceiver, pMsg)) != 0) {
20,679✔
868
    sRError(pReceiver, "failed to start snapshot writer since %s", tstrerror(code));
×
869
    goto _SEND_REPLY;
×
870
  }
871

872
  SyncIndex beginIndex = syncNodeGetSnapBeginIndex(pSyncNode);
20,679✔
873
  if (pReceiver->snapshotParam.start != beginIndex) {
20,679✔
874
    sRError(pReceiver, "snapshot begin index is changed unexpectedly. sver:%" PRId64 ", beginIndex:%" PRId64,
×
875
            pReceiver->snapshotParam.start, beginIndex);
876
    goto _SEND_REPLY;
×
877
  }
878

879
  code = 0;
20,679✔
880
_SEND_REPLY:
20,679✔
881

882
  // send response
883
  TAOS_CHECK_RETURN(syncSnapSendRsp(pReceiver, pMsg, NULL, 0, 0, code));
20,679✔
884

885
  TAOS_RETURN(code);
20,679✔
886
}
887

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

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

911
  if (pBlock != NULL && blockLen > 0) {
312,059✔
912
    (void)memcpy(pRspMsg->data, pBlock, blockLen);
20,679✔
913
  }
914

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

923
static int32_t syncSnapBufferRecv(SSyncSnapshotReceiver *pReceiver, SyncSnapshotSend **ppMsg) {
273,482✔
924
  int32_t           code = 0;
273,482✔
925
  SSyncSnapBuffer  *pRcvBuf = pReceiver->pRcvBuf;
273,482✔
926
  SyncSnapshotSend *pMsg = ppMsg[0];
273,482✔
927

928
  (void)taosThreadMutexLock(&pRcvBuf->mutex);
273,482✔
929

930
  if (pMsg->seq - pRcvBuf->start >= pRcvBuf->size) {
273,482✔
931
    code = TSDB_CODE_SYN_BUFFER_FULL;
×
932
    goto _out;
×
933
  }
934

935
  if (!(pRcvBuf->start <= pRcvBuf->cursor + 1 && pRcvBuf->cursor < pRcvBuf->end)) return TSDB_CODE_SYN_INTERNAL_ERROR;
273,482✔
936

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

949
  for (int64_t seq = pRcvBuf->cursor + 1; seq < pRcvBuf->end; ++seq) {
544,183✔
950
    if (pRcvBuf->entries[seq % pRcvBuf->size]) {
374,092✔
951
      pRcvBuf->cursor = seq;
270,701✔
952
    } else {
953
      break;
103,391✔
954
    }
955
  }
956

957
  for (int64_t seq = pRcvBuf->start; seq <= pRcvBuf->cursor; ++seq) {
543,874✔
958
    if ((code = snapshotReceiverGotData(pReceiver, pRcvBuf->entries[seq % pRcvBuf->size])) != 0) {
270,701✔
959
      if (code >= SYNC_SNAPSHOT_SEQ_INVALID) {
309✔
960
        code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
961
      }
962
    }
963
    pRcvBuf->start = seq + 1;
270,701✔
964
    if (syncSnapSendRsp(pReceiver, pRcvBuf->entries[seq % pRcvBuf->size], NULL, 0, 0, code) != 0) {
270,701✔
965
      sError("failed to send snap rsp");
×
966
    }
967
    pRcvBuf->entryDeleteCb(pRcvBuf->entries[seq % pRcvBuf->size]);
270,701✔
968
    pRcvBuf->entries[seq % pRcvBuf->size] = NULL;
270,701✔
969
    if (code) goto _out;
270,701✔
970
  }
971

972
_out:
273,173✔
973
  (void)taosThreadMutexUnlock(&pRcvBuf->mutex);
273,482✔
974
  TAOS_RETURN(code);
273,482✔
975
}
976

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

986
  if (snapshotReceiverSignatureCmp(pReceiver, pMsg) != 0) {
273,482✔
987
    code = TSDB_CODE_SYN_MISMATCHED_SIGNATURE;
×
988
    sError("failed to receive snapshot data, since %s", tstrerror(code));
×
989
    return syncSnapSendRsp(pReceiver, pMsg, NULL, 0, 0, code);
×
990
  }
991

992
  return syncSnapBufferRecv(pReceiver, ppMsg);
273,482✔
993
}
994

995
static int32_t syncNodeOnSnapshotEnd(SSyncNode *pSyncNode, SyncSnapshotSend *pMsg) {
20,370✔
996
  // condition 2
997
  // end, finish FSM
998
  SSyncSnapshotReceiver *pReceiver = pSyncNode->pNewNodeReceiver;
20,370✔
999
  int64_t                timeNow = taosGetTimestampMs();
20,370✔
1000
  int32_t                code = 0;
20,370✔
1001

1002
  if (snapshotReceiverSignatureCmp(pReceiver, pMsg) != 0) {
20,370✔
1003
    code = TSDB_CODE_SYN_MISMATCHED_SIGNATURE;
×
1004
    sError("failed to end snapshot, since %s", tstrerror(code));
×
1005
    goto _SEND_REPLY;
×
1006
  }
1007

1008
  code = snapshotReceiverFinish(pReceiver, pMsg);
20,370✔
1009
  if (code == 0) {
20,370✔
1010
    snapshotReceiverStop(pReceiver);
20,370✔
1011
  }
1012

1013
_SEND_REPLY:;
×
1014

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

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

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

1040
  TAOS_RETURN(code);
20,370✔
1041
}
1042

1043
int64_t lastRecvPrintLog = 0;
1044

1045
int32_t syncNodeOnSnapshot(SSyncNode *pSyncNode, SRpcMsg *pRpcMsg) {
335,210✔
1046
  SyncSnapshotSend     **ppMsg = (SyncSnapshotSend **)&pRpcMsg->pCont;
335,210✔
1047
  SyncSnapshotSend      *pMsg = ppMsg[0];
335,210✔
1048
  SSyncSnapshotReceiver *pReceiver = pSyncNode->pNewNodeReceiver;
335,210✔
1049
  int32_t                code = 0;
335,210✔
1050

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

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

1066
  if (pSyncNode->raftCfg.cfg.nodeInfo[pSyncNode->raftCfg.cfg.myIndex].nodeRole != TAOS_SYNC_ROLE_LEARNER) {
335,210✔
1067
    if (pMsg->term > raftStoreGetTerm(pSyncNode)) {
154,508✔
1068
      syncNodeStepDown(pSyncNode, pMsg->term, pMsg->srcId, "snapshot");
×
1069
    }
1070
  } else {
1071
    syncNodeUpdateTermWithoutStepDown(pSyncNode, pMsg->term);
180,702✔
1072
  }
1073

1074
  if (pSyncNode->state != TAOS_SYNC_STATE_FOLLOWER && pSyncNode->state != TAOS_SYNC_STATE_LEARNER) {
335,210✔
1075
    sRError(pReceiver, "snapshot receiver not a follower or learner");
×
1076
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
1077
    TAOS_RETURN(code);
×
1078
  }
1079

1080
  if (pMsg->seq < SYNC_SNAPSHOT_SEQ_PREP || pMsg->seq > SYNC_SNAPSHOT_SEQ_END) {
335,210✔
1081
    sRError(pReceiver, "snap replication msg with invalid seq:%d", pMsg->seq);
×
1082
    code = TSDB_CODE_SYN_INVALID_SNAPSHOT_MSG;
×
1083
    TAOS_RETURN(code);
×
1084
  }
1085

1086
  // prepare
1087
  if (pMsg->seq == SYNC_SNAPSHOT_SEQ_PREP) {
335,210✔
1088
    sInfo(
20,679✔
1089
        "vgId:%d, snapshot replication progress:2/8:follower:1/4, start to prepare, recv msg:%s, snap seq:%d, msg "
1090
        "signature:(%" PRId64 ", %" PRId64 ")",
1091
        pSyncNode->vgId, TMSG_INFO(pRpcMsg->msgType), pMsg->seq, pMsg->term, pMsg->snapStartTime);
1092
    code = syncNodeOnSnapshotPrep(pSyncNode, pMsg);
20,679✔
1093
    sDebug(
20,679✔
1094
        "vgId:%d, snapshot replication progress:2/8:follower:1/4, finish to prepare, recv msg:%s, snap seq:%d, msg "
1095
        "signature:(%" PRId64 ", %" PRId64 ")",
1096
        pSyncNode->vgId, TMSG_INFO(pRpcMsg->msgType), pMsg->seq, pMsg->term, pMsg->snapStartTime);
1097
    goto _out;
20,679✔
1098
  }
1099

1100
  // begin
1101
  if (pMsg->seq == SYNC_SNAPSHOT_SEQ_BEGIN) {
314,531✔
1102
    sInfo("vgId:%d, snapshot replication progress:4/8:follower:2/4, start to begin,replication. msg signature:(%" PRId64
20,679✔
1103
          ", %" PRId64 "), snapshot msg seq:%d",
1104
          pSyncNode->vgId, pMsg->term, pMsg->snapStartTime, pMsg->seq);
1105
    code = syncNodeOnSnapshotBegin(pSyncNode, pMsg);
20,679✔
1106
    sDebug("vgId:%d, snapshot replication progress:4/8:follower:2/4, finish to begin. msg signature:(%" PRId64
20,679✔
1107
           ", %" PRId64 ")",
1108
           pSyncNode->vgId, pMsg->term, pMsg->snapStartTime);
1109
    goto _out;
20,679✔
1110
  }
1111

1112
  // data
1113
  if (pMsg->seq > SYNC_SNAPSHOT_SEQ_BEGIN && pMsg->seq < SYNC_SNAPSHOT_SEQ_END) {
293,852✔
1114
    int64_t currentTimestamp = taosGetTimestampMs()/1000;
273,482✔
1115
    if (currentTimestamp > lastRecvPrintLog) {
273,482✔
1116
      sInfo("vgId:%d, snapshot replication progress:6/8:follower:3/4, start to receive. msg signature:(%" PRId64
22,859✔
1117
            ", %" PRId64 "), snapshot msg seq:%d",
1118
            pSyncNode->vgId, pMsg->term, pMsg->snapStartTime, pMsg->seq);
1119

1120
    } else {
1121
      sDebug("vgId:%d, snapshot replication progress:6/8:follower:3/4, start to receive. msg signature:(%" PRId64
250,623✔
1122
             ", %" PRId64 "), snapshot msg seq:%d",
1123
             pSyncNode->vgId, pMsg->term, pMsg->snapStartTime, pMsg->seq);
1124
    }
1125
    lastRecvPrintLog = currentTimestamp;
273,482✔
1126
    code = syncNodeOnSnapshotReceive(pSyncNode, ppMsg);
273,482✔
1127
    sDebug("vgId:%d, snapshot replication progress:6/8:follower:3/4, finish to receive.", pSyncNode->vgId);
273,482✔
1128
    goto _out;
273,482✔
1129
  }
1130

1131
  // end
1132
  if (pMsg->seq == SYNC_SNAPSHOT_SEQ_END) {
20,370✔
1133
    sInfo("vgId:%d, snapshot replication progress:7/8:follower:4/4, start to end. msg signature:(%" PRId64 ", %" PRId64
20,370✔
1134
          "), snapshot msg seq:%d",
1135
          pSyncNode->vgId, pMsg->term, pMsg->snapStartTime, pMsg->seq);
1136
    code = syncNodeOnSnapshotEnd(pSyncNode, pMsg);
20,370✔
1137
    if (code != 0) {
20,370✔
1138
      sRError(pReceiver, "failed to end snapshot.");
×
1139
      goto _out;
×
1140
    }
1141

1142
    code = syncLogBufferReInit(pSyncNode->pLogBuf, pSyncNode);
20,370✔
1143
    if (code != 0) {
20,370✔
1144
      sRError(pReceiver, "failed to reinit log buffer since %s", tstrerror(code));
×
1145
    }
1146
    sDebug("vgId:%d, snapshot replication progress:7/7:follower:4/4, finish to end. msg signature:(%" PRId64
20,370✔
1147
           ", %" PRId64 ")",
1148
           pSyncNode->vgId, pMsg->term, pMsg->snapStartTime);
1149
    goto _out;
20,370✔
1150
  }
1151

1152
_out:;
×
1153
  syncNodeResetElectTimer(pSyncNode);
335,210✔
1154
  TAOS_RETURN(code);
335,210✔
1155
}
1156

1157
static int32_t syncSnapSenderExchgSnapInfo(SSyncNode *pSyncNode, SSyncSnapshotSender *pSender, SyncSnapshotRsp *pMsg) {
20,700✔
1158
  if (pMsg->payloadType != TDMT_SYNC_PREP_SNAPSHOT_REPLY) return TSDB_CODE_SYN_INTERNAL_ERROR;
20,700✔
1159

1160
  SSyncTLV *datHead = (void *)pMsg->data;
20,700✔
1161
  if (datHead->typ != pMsg->payloadType) {
20,700✔
1162
    sSError(pSender, "unexpected data type in data of SyncSnapshotRsp. typ: %d", datHead->typ);
×
1163
    TAOS_RETURN(TSDB_CODE_INVALID_DATA_FMT);
×
1164
  }
1165
  int32_t dataLen = sizeof(SSyncTLV) + datHead->len;
20,700✔
1166

1167
  SSnapshotParam *pParam = &pSender->snapshotParam;
20,700✔
1168
  void           *data = taosMemoryRealloc(pParam->data, dataLen);
20,700✔
1169
  if (data == NULL) {
20,700✔
1170
    TAOS_RETURN(terrno);
×
1171
  }
1172
  (void)memcpy(data, pMsg->data, dataLen);
20,700✔
1173

1174
  pParam->data = data;
20,700✔
1175
  data = NULL;
20,700✔
1176
  sSInfo(pSender, "data of snapshot param. len: %d", datHead->len);
20,700✔
1177
  return 0;
20,700✔
1178
}
1179

1180
// sender
1181
static int32_t syncNodeOnSnapshotPrepRsp(SSyncNode *pSyncNode, SSyncSnapshotSender *pSender, SyncSnapshotRsp *pMsg) {
20,700✔
1182
  int32_t   code = 0;
20,700✔
1183
  SSnapshot snapshot = {0};
20,700✔
1184

1185
  if (pMsg->snapBeginIndex > pSyncNode->commitIndex + 1) {
20,700✔
1186
    sSError(pSender,
×
1187
            "snapshot begin index is greater than commit index. msg snapBeginIndex:%" PRId64
1188
            ", node commitIndex:%" PRId64,
1189
            pMsg->snapBeginIndex, pSyncNode->commitIndex);
1190
    TAOS_RETURN(TSDB_CODE_SYN_INVALID_SNAPSHOT_MSG);
×
1191
  }
1192

1193
  (void)taosThreadMutexLock(&pSender->pSndBuf->mutex);
20,700✔
1194
  TAOS_CHECK_GOTO(pSyncNode->pFsm->FpGetSnapshotInfo(pSyncNode->pFsm, &snapshot), NULL, _out);
20,700✔
1195

1196
  // prepare <begin, end>
1197
  pSender->snapshotParam.start = pMsg->snapBeginIndex;
20,700✔
1198
  pSender->snapshotParam.end = snapshot.lastApplyIndex;
20,700✔
1199

1200
  sSInfo(pSender, "prepare snapshot, recv-begin:%" PRId64 ", snapshot.last:%" PRId64 ", snapshot.term:%" PRId64,
20,700✔
1201
         pMsg->snapBeginIndex, snapshot.lastApplyIndex, snapshot.lastApplyTerm);
1202

1203
  // update sender
1204
  pSender->snapshot = snapshot;
20,700✔
1205

1206
  // start reader
1207
  if (pMsg->payloadType == TDMT_SYNC_PREP_SNAPSHOT_REPLY) {
20,700✔
1208
    TAOS_CHECK_GOTO(syncSnapSenderExchgSnapInfo(pSyncNode, pSender, pMsg), NULL, _out);
20,700✔
1209
  }
1210

1211
  code = pSyncNode->pFsm->FpSnapshotStartRead(pSyncNode->pFsm, &pSender->snapshotParam, &pSender->pReader);
20,700✔
1212
  if (code != 0) {
20,700✔
1213
    sSError(pSender, "prepare snapshot failed since %s", tstrerror(code));
×
1214
    goto _out;
×
1215
  }
1216

1217
  // update next index
1218
  syncIndexMgrSetIndex(pSyncNode->pNextIndex, &pMsg->srcId, snapshot.lastApplyIndex + 1);
20,700✔
1219

1220
  code = snapshotSend(pSender);
20,700✔
1221

1222
_out:
20,700✔
1223
  (void)taosThreadMutexUnlock(&pSender->pSndBuf->mutex);
20,700✔
1224
  TAOS_RETURN(code);
20,700✔
1225
}
1226

1227
static int32_t snapshotSenderSignatureCmp(SSyncSnapshotSender *pSender, SyncSnapshotRsp *pMsg) {
621,893✔
1228
  int32_t code = 0;
621,893✔
1229
  if (pSender->term < pMsg->term) return -1;
621,893✔
1230
  if (pSender->term > pMsg->term) return 1;
621,893✔
1231
  if (pSender->senderStartTime < pMsg->startTime) return -2;
621,893✔
1232
  if (pSender->senderStartTime > pMsg->startTime) return 2;
621,893✔
1233
  if (code != 0)
621,893✔
1234
    sSError(pSender, "sender signature failed, result:%d, msg signature:(%" PRId64 ", %" PRId64 ")", code, pMsg->term,
×
1235
            pMsg->startTime);
1236
  return 0;
621,893✔
1237
}
1238

1239
static int32_t syncSnapBufferSend(SSyncSnapshotSender *pSender, SyncSnapshotRsp **ppMsg) {
290,401✔
1240
  int32_t          code = 0;
290,401✔
1241
  SSyncSnapBuffer *pSndBuf = pSender->pSndBuf;
290,401✔
1242
  SyncSnapshotRsp *pMsg = ppMsg[0];
290,401✔
1243

1244
  (void)taosThreadMutexLock(&pSndBuf->mutex);
290,401✔
1245
  if (snapshotSenderSignatureCmp(pSender, pMsg) != 0) {
290,401✔
1246
    code = TSDB_CODE_SYN_MISMATCHED_SIGNATURE;
×
1247
    sError("failed to send snapshot data, since %s", tstrerror(code));
×
1248
    goto _out;
×
1249
  }
1250

1251
  if (pSender->pReader == NULL || pSender->finish || !snapshotSenderIsStart(pSender)) {
290,401✔
1252
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
1253
    goto _out;
×
1254
  }
1255

1256
  if (pMsg->ack - pSndBuf->start >= pSndBuf->size) {
290,401✔
1257
    code = TSDB_CODE_SYN_BUFFER_FULL;
×
1258
    goto _out;
×
1259
  }
1260

1261
  if (!(pSndBuf->start <= pSndBuf->cursor + 1 && pSndBuf->cursor < pSndBuf->end)) {
290,401✔
1262
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
1263
    goto _out;
×
1264
  }
1265

1266
  if (pMsg->ack > pSndBuf->cursor && pMsg->ack < pSndBuf->end) {
290,401✔
1267
    SyncSnapBlock *pBlk = pSndBuf->entries[pMsg->ack % pSndBuf->size];
269,701✔
1268
    if (!pBlk) {
269,701✔
1269
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
1270
      goto _out;
×
1271
    }
1272
    pBlk->acked = 1;
269,701✔
1273
  }
1274

1275
  for (int64_t ack = pSndBuf->cursor + 1; ack < pSndBuf->end; ++ack) {
560,102✔
1276
    SyncSnapBlock *pBlk = pSndBuf->entries[ack % pSndBuf->size];
519,011✔
1277
    if (pBlk->acked) {
519,011✔
1278
      pSndBuf->cursor = ack;
269,701✔
1279
    } else {
1280
      break;
249,310✔
1281
    }
1282
  }
1283

1284
  for (int64_t ack = pSndBuf->start; ack <= pSndBuf->cursor; ++ack) {
560,102✔
1285
    pSndBuf->entryDeleteCb(pSndBuf->entries[ack % pSndBuf->size]);
269,701✔
1286
    pSndBuf->entries[ack % pSndBuf->size] = NULL;
269,701✔
1287
    pSndBuf->start = ack + 1;
269,701✔
1288
  }
1289

1290
  while (pSender->seq != SYNC_SNAPSHOT_SEQ_END && pSender->seq - pSndBuf->start < tsSnapReplMaxWaitN) {
586,055✔
1291
    if ((code = snapshotSend(pSender)) != 0) {
295,654✔
1292
      goto _out;
×
1293
    }
1294
  }
1295

1296
  if (pSender->seq == SYNC_SNAPSHOT_SEQ_END && pSndBuf->end <= pSndBuf->start) {
290,401✔
1297
    if ((code = snapshotSend(pSender)) != 0) {
20,391✔
1298
      goto _out;
×
1299
    }
1300
  }
1301
_out:
290,401✔
1302
  (void)taosThreadMutexUnlock(&pSndBuf->mutex);
290,401✔
1303
  TAOS_RETURN(code);
290,401✔
1304
}
1305

1306
int64_t lastSendPrintLog = 0;
1307

1308
int32_t syncNodeOnSnapshotRsp(SSyncNode *pSyncNode, SRpcMsg *pRpcMsg) {
331,492✔
1309
  SyncSnapshotRsp **ppMsg = (SyncSnapshotRsp **)&pRpcMsg->pCont;
331,492✔
1310
  SyncSnapshotRsp  *pMsg = ppMsg[0];
331,492✔
1311
  int32_t           code = 0;
331,492✔
1312

1313
  // if already drop replica, do not process
1314
  if (!syncNodeInRaftGroup(pSyncNode, &pMsg->srcId)) {
331,492✔
1315
    syncLogRecvSyncSnapshotRsp(pSyncNode, pMsg, "maybe replica already dropped", &pRpcMsg->info.traceId);
×
1316
    TAOS_RETURN(TSDB_CODE_SYN_NOT_IN_RAFT_GROUP);
×
1317
  }
1318

1319
  // get sender
1320
  SSyncSnapshotSender *pSender = syncNodeGetSnapshotSender(pSyncNode, &pMsg->srcId);
331,492✔
1321
  if (pSender == NULL) {
331,492✔
1322
    syncLogRecvSyncSnapshotRsp(pSyncNode, pMsg, "sender is null", &pRpcMsg->info.traceId);
×
1323
    TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
×
1324
  }
1325

1326
  if (!snapshotSenderIsStart(pSender)) {
331,492✔
1327
    sSError(pSender, "snapshot sender stopped. sender startTime:%" PRId64 ", msg startTime:%" PRId64,
×
1328
            pSender->senderStartTime, pMsg->startTime);
1329
    TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
×
1330
  }
1331

1332
  // check signature
1333
  int32_t order = 0;
331,492✔
1334
  if ((order = snapshotSenderSignatureCmp(pSender, pMsg)) > 0) {
331,492✔
1335
    sError("failed to check snapshot rsp signature, ignore a stale snap rsp.");
×
1336
    TAOS_RETURN(TSDB_CODE_SYN_MISMATCHED_SIGNATURE);
×
1337
  } else if (order < 0) {
331,492✔
1338
    sError("failed to check snapshot rsp signature, snapshot sender is stale. stop");
×
1339
    code = TSDB_CODE_SYN_MISMATCHED_SIGNATURE;
×
1340
    goto _ERROR;
×
1341
  }
1342

1343
  if (pSyncNode->state != TAOS_SYNC_STATE_LEADER && pSyncNode->state != TAOS_SYNC_STATE_ASSIGNED_LEADER) {
331,492✔
1344
    sSError(pSender, "snapshot sender not leader");
×
1345
    code = TSDB_CODE_SYN_NOT_LEADER;
×
1346
    goto _ERROR;
×
1347
  }
1348

1349
  SyncTerm currentTerm = raftStoreGetTerm(pSyncNode);
331,492✔
1350
  if (pMsg->term != currentTerm) {
331,492✔
1351
    sSError(pSender, "snapshot sender term mismatch, msg term:%" PRId64 " currentTerm:%" PRId64, pMsg->term,
×
1352
            currentTerm);
1353
    code = TSDB_CODE_SYN_TERM_NOT_MATCH;
×
1354
    goto _ERROR;
×
1355
  }
1356

1357
  if (pMsg->code != 0) {
331,492✔
1358
    sSError(pSender, "snapshot sender receive error:%s 0x%x and stop sender", tstrerror(pMsg->code), pMsg->code);
×
1359
    code = pMsg->code;
×
1360
    goto _ERROR;
×
1361
  }
1362

1363
  // send begin
1364
  if (pMsg->ack == SYNC_SNAPSHOT_SEQ_PREP) {
331,492✔
1365
    sSInfo(pSender, "snapshot replication progress:3/8:leader:2/4, process prepare rsp, msg:%s, snap ack:%d, ",
20,700✔
1366
           TMSG_INFO(pRpcMsg->msgType), pMsg->ack);
1367
    if ((code = syncNodeOnSnapshotPrepRsp(pSyncNode, pSender, pMsg)) != 0) {
20,700✔
1368
      goto _ERROR;
×
1369
    }
1370
  }
1371

1372
  // send msg of data or end
1373
  if (pMsg->ack >= SYNC_SNAPSHOT_SEQ_BEGIN && pMsg->ack < SYNC_SNAPSHOT_SEQ_END) {
331,492✔
1374
    int64_t currentTimestamp = taosGetTimestampMs()/1000;
290,401✔
1375
    if (currentTimestamp > lastSendPrintLog) {
290,401✔
1376
      sSInfo(pSender, "snapshot replication progress:5/8:leader:3/4, send buffer, msg:%s, snap ack:%d",
21,935✔
1377
             TMSG_INFO(pRpcMsg->msgType), pMsg->ack);
1378
    } else {
1379
      sSDebug(pSender, "snapshot replication progress:5/8:leader:3/4, send buffer, msg:%s, snap ack:%d",
268,466✔
1380
              TMSG_INFO(pRpcMsg->msgType), pMsg->ack);
1381
    }
1382
    lastSendPrintLog = currentTimestamp;
290,401✔
1383
    if ((code = syncSnapBufferSend(pSender, ppMsg)) != 0) {
290,401✔
1384
      sSError(pSender, "failed to replicate snap since %s. seq:%d, pReader:%p, finish:%d", tstrerror(code),
×
1385
              pSender->seq, pSender->pReader, pSender->finish);
1386
      goto _ERROR;
×
1387
    }
1388
  }
1389

1390
  // end
1391
  if (pMsg->ack == SYNC_SNAPSHOT_SEQ_END) {
331,492✔
1392
    sSInfo(pSender, "snapshot replication progress:8/8:leader:4/4, process end rsp");
20,391✔
1393
    snapshotSenderStop(pSender, true);
20,391✔
1394
    TAOS_CHECK_GOTO(syncNodeReplicateReset(pSyncNode, &pMsg->srcId), NULL, _ERROR);
20,391✔
1395
  }
1396

1397
  return 0;
331,492✔
1398

1399
_ERROR:
×
1400
  snapshotSenderStop(pSender, false);
×
1401
  if (syncNodeReplicateReset(pSyncNode, &pMsg->srcId) != 0) sError("failed to reset replicate");
×
1402
  TAOS_RETURN(code);
×
1403
}
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