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

taosdata / TDengine / #4896

24 Dec 2025 07:36AM UTC coverage: 65.929% (+0.4%) from 65.513%
#4896

push

travis-ci

web-flow
enh: [TS-7591] Some code refactor and add more log. (#34022)

326 of 537 new or added lines in 4 files covered. (60.71%)

370 existing lines in 111 files now uncovered.

185828 of 281861 relevant lines covered (65.93%)

116309824.55 hits per line

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

70.51
/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) {
157,793,252✔
30
  for (int64_t i = pBuf->start; i < pBuf->end; ++i) {
157,793,252✔
UNCOV
31
    if (pBuf->entryDeleteCb) {
×
UNCOV
32
      pBuf->entryDeleteCb(pBuf->entries[i % pBuf->size]);
×
33
    }
UNCOV
34
    pBuf->entries[i % pBuf->size] = NULL;
×
35
  }
36
  pBuf->start = SYNC_SNAPSHOT_SEQ_BEGIN + 1;
157,792,671✔
37
  pBuf->end = pBuf->start;
157,792,671✔
38
  pBuf->cursor = pBuf->start - 1;
157,792,498✔
39
}
157,793,303✔
40

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

45
  syncSnapBufferReset(pBuf);
78,719,170✔
46

47
  (void)taosThreadMutexDestroy(&pBuf->mutex);
78,718,387✔
48
  taosMemoryFree(ppBuf[0]);
78,719,082✔
49
  ppBuf[0] = NULL;
78,717,868✔
50
  return;
78,717,868✔
51
}
52

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

66
int32_t snapshotSenderCreate(SSyncNode *pSyncNode, int32_t replicaIndex, SSyncSnapshotSender **ppSender) {
74,169,807✔
67
  int32_t code = 0;
74,169,807✔
68
  *ppSender = NULL;
74,169,807✔
69
  bool condition = (pSyncNode->pFsm->FpSnapshotStartRead != NULL) && (pSyncNode->pFsm->FpSnapshotStopRead != NULL) &&
148,343,477✔
70
                   (pSyncNode->pFsm->FpSnapshotDoRead != NULL);
74,171,361✔
71
  if (!condition) {
74,171,361✔
72
    TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
×
73
  }
74

75
  SSyncSnapshotSender *pSender = taosMemoryCalloc(1, sizeof(SSyncSnapshotSender));
74,171,361✔
76
  if (pSender == NULL) {
74,168,902✔
77
    TAOS_RETURN(terrno);
×
78
  }
79

80
  pSender->start = false;
74,168,902✔
81
  pSender->seq = SYNC_SNAPSHOT_SEQ_INVALID;
74,168,148✔
82
  pSender->ack = SYNC_SNAPSHOT_SEQ_INVALID;
74,168,148✔
83
  pSender->pReader = NULL;
74,168,148✔
84
  pSender->sendingMS = SYNC_SNAPSHOT_RETRY_MS;
74,168,148✔
85
  pSender->pSyncNode = pSyncNode;
74,168,148✔
86
  pSender->replicaIndex = replicaIndex;
74,168,222✔
87
  pSender->term = raftStoreGetTerm(pSyncNode);
74,168,723✔
88
  pSender->senderStartTime = -1;
74,172,145✔
89
  pSender->finish = false;
74,172,072✔
90

91
  code = pSender->pSyncNode->pFsm->FpGetSnapshotInfo(pSender->pSyncNode->pFsm, &pSender->snapshot);
74,172,145✔
92
  if (code != 0) {
74,171,340✔
93
    taosMemoryFreeClear(pSender);
×
94
    TAOS_RETURN(code);
×
95
  }
96
  SSyncSnapBuffer *pSndBuf = NULL;
74,171,340✔
97
  code = syncSnapBufferCreate(&pSndBuf);
74,171,267✔
98
  if (pSndBuf == NULL) {
74,170,506✔
99
    taosMemoryFreeClear(pSender);
×
100
    TAOS_RETURN(code);
×
101
  }
102
  pSndBuf->entryDeleteCb = syncSnapBlockDestroy;
74,170,506✔
103
  pSender->pSndBuf = pSndBuf;
74,170,506✔
104

105
  syncSnapBufferReset(pSender->pSndBuf);
74,170,506✔
106
  *ppSender = pSender;
74,170,443✔
107
  TAOS_RETURN(code);
74,170,535✔
108
}
109

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

120
static int32_t snapshotSenderClearInfoData(SSyncSnapshotSender *pSender) {
73,881,028✔
121
  if (pSender->snapshotParam.data) {
73,881,028✔
122
    taosMemoryFree(pSender->snapshotParam.data);
21,969✔
123
    pSender->snapshotParam.data = NULL;
21,969✔
124
  }
125

126
  if (pSender->snapshot.data) {
73,881,028✔
127
    taosMemoryFree(pSender->snapshot.data);
×
128
    pSender->snapshot.data = NULL;
×
129
  }
130
  return 0;
73,881,028✔
131
}
132

133
void snapshotSenderDestroy(SSyncSnapshotSender *pSender) {
73,860,475✔
134
  if (pSender == NULL) return;
73,860,475✔
135

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

142
  // free snap buffer
143
  if (pSender->pSndBuf) {
73,860,475✔
144
    syncSnapBufferDestroy(&pSender->pSndBuf);
73,860,475✔
145
  }
146

147
  (void)snapshotSenderClearInfoData(pSender);
73,858,351✔
148

149
  // free sender
150
  taosMemoryFree(pSender);
73,859,059✔
151
}
152

153
bool snapshotSenderIsStart(SSyncSnapshotSender *pSender) { return atomic_load_8(&pSender->start); }
73,266,736✔
154

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

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

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

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

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

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

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

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

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

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

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

231
    syncSnapBufferReset(pSender->pSndBuf);
21,969✔
232

233
    (void)snapshotSenderClearInfoData(pSender);
21,969✔
234

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

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

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

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

262
  if (pBlock != NULL && blockLen > 0) {
352,756✔
263
    (void)memcpy(pMsg->data, pBlock, blockLen);
308,818✔
264
  }
265
  pMsg->payloadType = typ;
352,756✔
266

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

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

283
  if (pSender->seq < SYNC_SNAPSHOT_SEQ_END) {
352,756✔
284
    pSender->seq++;
330,787✔
285

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

293
      pBlk->seq = pSender->seq;
308,818✔
294

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

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

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

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

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

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

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

403
  taosMsleep(1);
21,969✔
404

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

411
  return 0;
21,969✔
412
}
413

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

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

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

447
  SSyncSnapBuffer *pRcvBuf = NULL;
4,859,637✔
448
  code = syncSnapBufferCreate(&pRcvBuf);
4,859,608✔
449
  if (pRcvBuf == NULL) {
4,859,637✔
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,859,637✔
459
  pReceiver->pRcvBuf = pRcvBuf;
4,859,637✔
460

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

466
static int32_t snapshotReceiverClearInfoData(SSyncSnapshotReceiver *pReceiver) {
4,881,487✔
467
  if (pReceiver->snapshotParam.data) {
4,881,487✔
468
    taosMemoryFree(pReceiver->snapshotParam.data);
21,970✔
469
    pReceiver->snapshotParam.data = NULL;
21,970✔
470
  }
471

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

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

482
  (void)taosThreadMutexLock(&pReceiver->writerMutex);
4,859,517✔
483
  // close writer
484
  if (pReceiver->pWriter != NULL) {
4,859,517✔
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,859,517✔
494

495
  (void)taosThreadMutexDestroy(&pReceiver->writerMutex);
4,859,517✔
496

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

502
  (void)snapshotReceiverClearInfoData(pReceiver);
4,859,517✔
503

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

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

512
static int32_t snapshotReceiverSignatureCmp(SSyncSnapshotReceiver *pReceiver, SyncSnapshotSend *pMsg) {
330,847✔
513
  int32_t code = 0;
330,847✔
514
  if (pReceiver->term < pMsg->term) {
330,847✔
515
    code = -1;
×
516
    goto _OVER;
×
517
  }
518
  if (pReceiver->term > pMsg->term) {
330,847✔
519
    code = 1;
×
520
    goto _OVER;
×
521
  }
522
  if (pReceiver->receiverStartTime < pMsg->snapStartTime) {
330,847✔
523
    code = -2;
×
524
    goto _OVER;
×
525
  }
526
  if (pReceiver->receiverStartTime > pMsg->snapStartTime) {
330,847✔
527
    code = 2;
×
528
    goto _OVER;
×
529
  }
530
_OVER:
330,847✔
531
  if (code > 0) {
330,847✔
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) {
330,847✔
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;
330,847✔
540
}
541

542
static int32_t snapshotReceiverStartWriter(SSyncSnapshotReceiver *pReceiver, SyncSnapshotSend *pBeginMsg) {
21,970✔
543
  if (pReceiver->pWriter != NULL) {
21,970✔
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;
21,970✔
550

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

558
  // start writer
559
  int32_t code = pReceiver->pSyncNode->pFsm->FpSnapshotStartWrite(pReceiver->pSyncNode->pFsm, &pReceiver->snapshotParam,
21,970✔
560
                                                                  &pReceiver->pWriter);
561
  if (code != 0) {
21,970✔
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");
21,970✔
568
  return 0;
21,970✔
569
}
570

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

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

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

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

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

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

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

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

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

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

621
static int32_t snapshotReceiverFinish(SSyncSnapshotReceiver *pReceiver, SyncSnapshotSend *pMsg) {
21,970✔
622
  int32_t code = 0;
21,970✔
623
  if (pReceiver->pWriter != NULL) {
21,970✔
624
    // write data
625
    sRInfo(pReceiver, "snapshot receiver write about to finish, blockLen:%d seq:%d", pMsg->dataLen, pMsg->seq);
21,970✔
626
    if (pMsg->dataLen > 0) {
21,970✔
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) {
21,970✔
639
      pReceiver->pSyncNode->commitIndex = pReceiver->snapshot.lastApplyIndex;
21,970✔
640
    }
641

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

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

661
    // update progress
662
    pReceiver->ack = SYNC_SNAPSHOT_SEQ_END;
21,970✔
663

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

673
    // reset wal
674
    code =
675
        pReceiver->pSyncNode->pLogStore->syncLogRestoreFromSnapshot(pReceiver->pSyncNode->pLogStore, pMsg->lastIndex);
21,970✔
676
    if (code != 0) {
21,970✔
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");
21,970✔
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;
21,970✔
688
}
689

690
static int32_t snapshotReceiverGotData(SSyncSnapshotReceiver *pReceiver, SyncSnapshotSend *pMsg) {
286,907✔
691
  if (pMsg->seq != pReceiver->ack + 1) {
286,907✔
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);
286,907✔
697

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

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

706
  if (pMsg->dataLen > 0) {
286,907✔
707
    // apply data block
708
    int32_t code = pReceiver->pSyncNode->pFsm->FpSnapshotDoWrite(pReceiver->pSyncNode->pFsm, pReceiver->pWriter,
573,814✔
709
                                                                 pMsg->data, pMsg->dataLen);
286,907✔
710
    if (code != 0) {
286,907✔
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);
286,907✔
718

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

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

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

730
  if (syncNodeIsMnode(ths)) {
43,940✔
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;
43,940✔
735
    SWal              *pWal = pData->pWal;
43,940✔
736

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

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

743
  return snapStart;
43,940✔
744
}
745

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

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

760
  // exchange snap info
761
  if ((code = pSyncNode->pFsm->FpGetSnapshotInfo(pSyncNode->pFsm, pInfo)) != 0) {
21,970✔
762
    sRError(pReceiver, "failed to get snapshot info. type: %d", pMsg->payloadType);
×
763
    goto _exit;
×
764
  }
765
  SSyncTLV *datHead = pInfo->data;
21,970✔
766
  if (datHead->typ != TDMT_SYNC_PREP_SNAPSHOT_REPLY) {
21,970✔
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;
21,970✔
772

773
  // save exchanged snap info
774
  SSnapshotParam *pParam = &pReceiver->snapshotParam;
21,970✔
775
  data = taosMemoryRealloc(pParam->data, dataLen);
21,970✔
776
  if (data == NULL) {
21,970✔
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;
21,970✔
783
  data = NULL;
21,970✔
784
  (void)memcpy(pParam->data, pInfo->data, dataLen);
21,970✔
785

786
_exit:
21,970✔
787
  TAOS_RETURN(code);
21,970✔
788
}
789

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

795
  if (snapshotReceiverIsStart(pReceiver)) {
21,970✔
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");
21,970✔
813
    goto _START_RECEIVER;
21,970✔
814
  }
815

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

822
  snapshotReceiverStart(pReceiver, pMsg);
21,970✔
823

824
_SEND_REPLY:;
21,970✔
825

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

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

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

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

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

860
  if (snapshotReceiverSignatureCmp(pReceiver, pMsg) != 0) {
21,970✔
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) {
21,970✔
868
    sRError(pReceiver, "failed to start snapshot writer since %s", tstrerror(code));
×
869
    goto _SEND_REPLY;
×
870
  }
871

872
  SyncIndex beginIndex = syncNodeGetSnapBeginIndex(pSyncNode);
21,970✔
873
  if (pReceiver->snapshotParam.start != beginIndex) {
21,970✔
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;
21,970✔
880
_SEND_REPLY:
21,970✔
881

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

885
  TAOS_RETURN(code);
21,970✔
886
}
887

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

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

911
  if (pBlock != NULL && blockLen > 0) {
330,847✔
912
    (void)memcpy(pRspMsg->data, pBlock, blockLen);
21,970✔
913
  }
914

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

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

928
  (void)taosThreadMutexLock(&pRcvBuf->mutex);
286,907✔
929

930
  if (pMsg->seq - pRcvBuf->start >= pRcvBuf->size) {
286,907✔
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;
286,907✔
936

937
  if (pMsg->seq > pRcvBuf->cursor) {
286,907✔
938
    if (pRcvBuf->entries[pMsg->seq % pRcvBuf->size]) {
286,907✔
939
      pRcvBuf->entryDeleteCb(pRcvBuf->entries[pMsg->seq % pRcvBuf->size]);
×
940
    }
941
    pRcvBuf->entries[pMsg->seq % pRcvBuf->size] = pMsg;
286,907✔
942
    ppMsg[0] = NULL;
286,907✔
943
    pRcvBuf->end = TMAX(pMsg->seq + 1, pRcvBuf->end);
286,907✔
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) {
573,814✔
950
    if (pRcvBuf->entries[seq % pRcvBuf->size]) {
409,011✔
951
      pRcvBuf->cursor = seq;
286,907✔
952
    } else {
953
      break;
122,104✔
954
    }
955
  }
956

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

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

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

986
  if (snapshotReceiverSignatureCmp(pReceiver, pMsg) != 0) {
286,907✔
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);
286,907✔
993
}
994

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

1002
  if (snapshotReceiverSignatureCmp(pReceiver, pMsg) != 0) {
21,970✔
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);
21,970✔
1009
  if (code == 0) {
21,970✔
1010
    snapshotReceiverStop(pReceiver);
21,970✔
1011
  }
1012

1013
_SEND_REPLY:;
×
1014

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

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

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

1040
  TAOS_RETURN(code);
21,970✔
1041
}
1042

1043
int64_t lastRecvPrintLog = 0;
1044

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

1051
  // if already drop replica, do not process
1052
  if (!syncNodeInRaftGroup(pSyncNode, &pMsg->srcId)) {
352,817✔
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)) {
352,817✔
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) {
352,817✔
1067
    if (pMsg->term > raftStoreGetTerm(pSyncNode)) {
163,041✔
1068
      syncNodeStepDown(pSyncNode, pMsg->term, pMsg->srcId, "snapshot");
×
1069
    }
1070
  } else {
1071
    syncNodeUpdateTermWithoutStepDown(pSyncNode, pMsg->term);
189,776✔
1072
  }
1073

1074
  if (pSyncNode->state != TAOS_SYNC_STATE_FOLLOWER && pSyncNode->state != TAOS_SYNC_STATE_LEARNER) {
352,817✔
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) {
352,817✔
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) {
352,817✔
1088
    sInfo(
21,970✔
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);
21,970✔
1093
    sDebug(
21,970✔
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;
21,970✔
1098
  }
1099

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

1112
  // data
1113
  if (pMsg->seq > SYNC_SNAPSHOT_SEQ_BEGIN && pMsg->seq < SYNC_SNAPSHOT_SEQ_END) {
308,877✔
1114
    int64_t currentTimestamp = taosGetTimestampMs()/1000;
286,907✔
1115
    if (currentTimestamp > lastRecvPrintLog) {
286,907✔
1116
      sInfo("vgId:%d, snapshot replication progress:6/8:follower:3/4, start to receive. msg signature:(%" PRId64
23,210✔
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
263,697✔
1122
             ", %" PRId64 "), snapshot msg seq:%d",
1123
             pSyncNode->vgId, pMsg->term, pMsg->snapStartTime, pMsg->seq);
1124
    }
1125
    lastRecvPrintLog = currentTimestamp;
286,907✔
1126
    code = syncNodeOnSnapshotReceive(pSyncNode, ppMsg);
286,907✔
1127
    sDebug("vgId:%d, snapshot replication progress:6/8:follower:3/4, finish to receive.", pSyncNode->vgId);
286,907✔
1128
    goto _out;
286,907✔
1129
  }
1130

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

1142
    code = syncLogBufferReInit(pSyncNode->pLogBuf, pSyncNode);
21,970✔
1143
    if (code != 0) {
21,970✔
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
21,970✔
1147
           ", %" PRId64 ")",
1148
           pSyncNode->vgId, pMsg->term, pMsg->snapStartTime);
1149
    goto _out;
21,970✔
1150
  }
1151

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

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

1160
  SSyncTLV *datHead = (void *)pMsg->data;
21,969✔
1161
  if (datHead->typ != pMsg->payloadType) {
21,969✔
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;
21,969✔
1166

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

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

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

1185
  if (pMsg->snapBeginIndex > pSyncNode->commitIndex + 1) {
21,969✔
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);
21,969✔
1194
  TAOS_CHECK_GOTO(pSyncNode->pFsm->FpGetSnapshotInfo(pSyncNode->pFsm, &snapshot), NULL, _out);
21,969✔
1195

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

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

1203
  // update sender
1204
  pSender->snapshot = snapshot;
21,969✔
1205

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

1211
  code = pSyncNode->pFsm->FpSnapshotStartRead(pSyncNode->pFsm, &pSender->snapshotParam, &pSender->pReader);
21,969✔
1212
  if (code != 0) {
21,969✔
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);
21,969✔
1219

1220
  code = snapshotSend(pSender);
21,969✔
1221

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

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

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

1244
  (void)taosThreadMutexLock(&pSndBuf->mutex);
308,818✔
1245
  if (snapshotSenderSignatureCmp(pSender, pMsg) != 0) {
308,818✔
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)) {
308,818✔
1252
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
1253
    goto _out;
×
1254
  }
1255

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

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

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

1275
  for (int64_t ack = pSndBuf->cursor + 1; ack < pSndBuf->end; ++ack) {
595,667✔
1276
    SyncSnapBlock *pBlk = pSndBuf->entries[ack % pSndBuf->size];
551,729✔
1277
    if (pBlk->acked) {
551,729✔
1278
      pSndBuf->cursor = ack;
286,849✔
1279
    } else {
1280
      break;
264,880✔
1281
    }
1282
  }
1283

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

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

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

1306
int64_t lastSendPrintLog = 0;
1307

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

1313
  // if already drop replica, do not process
1314
  if (!syncNodeInRaftGroup(pSyncNode, &pMsg->srcId)) {
352,756✔
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);
352,756✔
1321
  if (pSender == NULL) {
352,756✔
1322
    syncLogRecvSyncSnapshotRsp(pSyncNode, pMsg, "sender is null", &pRpcMsg->info.traceId);
×
1323
    TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
×
1324
  }
1325

1326
  if (!snapshotSenderIsStart(pSender)) {
352,756✔
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;
352,756✔
1334
  if ((order = snapshotSenderSignatureCmp(pSender, pMsg)) > 0) {
352,756✔
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) {
352,756✔
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) {
352,756✔
1344
    sSError(pSender, "snapshot sender not leader");
×
1345
    code = TSDB_CODE_SYN_NOT_LEADER;
×
1346
    goto _ERROR;
×
1347
  }
1348

1349
  SyncTerm currentTerm = raftStoreGetTerm(pSyncNode);
352,756✔
1350
  if (pMsg->term != currentTerm) {
352,756✔
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) {
352,756✔
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) {
352,756✔
1365
    sSInfo(pSender, "snapshot replication progress:3/8:leader:2/4, process prepare rsp, msg:%s, snap ack:%d, ",
21,969✔
1366
           TMSG_INFO(pRpcMsg->msgType), pMsg->ack);
1367
    if ((code = syncNodeOnSnapshotPrepRsp(pSyncNode, pSender, pMsg)) != 0) {
21,969✔
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) {
352,756✔
1374
    int64_t currentTimestamp = taosGetTimestampMs()/1000;
308,818✔
1375
    if (currentTimestamp > lastSendPrintLog) {
308,818✔
1376
      sSInfo(pSender, "snapshot replication progress:5/8:leader:3/4, send buffer, msg:%s, snap ack:%d",
23,849✔
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",
284,969✔
1380
              TMSG_INFO(pRpcMsg->msgType), pMsg->ack);
1381
    }
1382
    lastSendPrintLog = currentTimestamp;
308,818✔
1383
    if ((code = syncSnapBufferSend(pSender, ppMsg)) != 0) {
308,818✔
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) {
352,756✔
1392
    sSInfo(pSender, "snapshot replication progress:8/8:leader:4/4, process end rsp");
21,969✔
1393
    snapshotSenderStop(pSender, true);
21,969✔
1394
    TAOS_CHECK_GOTO(syncNodeReplicateReset(pSyncNode, &pMsg->srcId), NULL, _ERROR);
21,969✔
1395
  }
1396

1397
  return 0;
352,756✔
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