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

taosdata / TDengine / #4971

28 Feb 2026 08:05AM UTC coverage: 67.671% (-0.04%) from 67.707%
#4971

push

travis-ci

web-flow
fix(planner): disable project block merge in non-top-level subplans (#34617)

208281 of 307783 relevant lines covered (67.67%)

130135765.28 hits per line

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

71.94
/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) {
138,985,674✔
30
  for (int64_t i = pBuf->start; i < pBuf->end; ++i) {
138,992,263✔
31
    if (pBuf->entryDeleteCb) {
6,589✔
32
      pBuf->entryDeleteCb(pBuf->entries[i % pBuf->size]);
6,589✔
33
    }
34
    pBuf->entries[i % pBuf->size] = NULL;
6,589✔
35
  }
36
  pBuf->start = SYNC_SNAPSHOT_SEQ_BEGIN + 1;
138,984,940✔
37
  pBuf->end = pBuf->start;
138,984,661✔
38
  pBuf->cursor = pBuf->start - 1;
138,984,661✔
39
}
138,983,985✔
40

41
static void syncSnapBufferDestroy(SSyncSnapBuffer **ppBuf) {
69,373,690✔
42
  if (ppBuf == NULL || ppBuf[0] == NULL) return;
69,373,690✔
43
  SSyncSnapBuffer *pBuf = ppBuf[0];
69,373,782✔
44

45
  syncSnapBufferReset(pBuf);
69,373,782✔
46

47
  (void)taosThreadMutexDestroy(&pBuf->mutex);
69,373,084✔
48
  taosMemoryFree(ppBuf[0]);
69,373,084✔
49
  ppBuf[0] = NULL;
69,370,908✔
50
  return;
69,370,908✔
51
}
52

53
static int32_t syncSnapBufferCreate(SSyncSnapBuffer **ppBuf) {
69,555,324✔
54
  SSyncSnapBuffer *pBuf = taosMemoryCalloc(1, sizeof(SSyncSnapBuffer));
69,555,324✔
55
  if (pBuf == NULL) {
69,553,860✔
56
    *ppBuf = NULL;
×
57
    TAOS_RETURN(terrno);
×
58
  }
59
  pBuf->size = sizeof(pBuf->entries) / sizeof(void *);
69,553,860✔
60
  if (pBuf->size != TSDB_SYNC_SNAP_BUFFER_SIZE) return TSDB_CODE_SYN_INTERNAL_ERROR;
69,553,441✔
61
  (void)taosThreadMutexInit(&pBuf->mutex, NULL);
69,554,160✔
62
  *ppBuf = pBuf;
69,554,919✔
63
  TAOS_RETURN(0);
69,554,919✔
64
}
65

66
int32_t snapshotSenderCreate(SSyncNode *pSyncNode, int32_t replicaIndex, SSyncSnapshotSender **ppSender) {
65,243,087✔
67
  int32_t code = 0;
65,243,087✔
68
  *ppSender = NULL;
65,243,087✔
69
  bool condition = (pSyncNode->pFsm->FpSnapshotStartRead != NULL) && (pSyncNode->pFsm->FpSnapshotStopRead != NULL) &&
130,487,488✔
70
                   (pSyncNode->pFsm->FpSnapshotDoRead != NULL);
65,243,726✔
71
  if (!condition) {
65,243,726✔
72
    TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
×
73
  }
74

75
  SSyncSnapshotSender *pSender = taosMemoryCalloc(1, sizeof(SSyncSnapshotSender));
65,243,726✔
76
  if (pSender == NULL) {
65,240,486✔
77
    TAOS_RETURN(terrno);
×
78
  }
79

80
  pSender->start = false;
65,240,486✔
81
  pSender->seq = SYNC_SNAPSHOT_SEQ_INVALID;
65,240,486✔
82
  pSender->ack = SYNC_SNAPSHOT_SEQ_INVALID;
65,240,486✔
83
  pSender->pReader = NULL;
65,239,893✔
84
  pSender->sendingMS = SYNC_SNAPSHOT_RETRY_MS;
65,239,893✔
85
  pSender->pSyncNode = pSyncNode;
65,240,620✔
86
  pSender->replicaIndex = replicaIndex;
65,240,582✔
87
  pSender->term = raftStoreGetTerm(pSyncNode);
65,240,748✔
88
  pSender->senderStartTime = -1;
65,245,764✔
89
  pSender->finish = false;
65,245,508✔
90

91
  code = pSender->pSyncNode->pFsm->FpGetSnapshotInfo(pSender->pSyncNode->pFsm, &pSender->snapshot);
65,245,223✔
92
  if (code != 0) {
65,244,173✔
93
    taosMemoryFreeClear(pSender);
×
94
    TAOS_RETURN(code);
×
95
  }
96
  SSyncSnapBuffer *pSndBuf = NULL;
65,244,173✔
97
  code = syncSnapBufferCreate(&pSndBuf);
65,244,173✔
98
  if (pSndBuf == NULL) {
65,243,856✔
99
    taosMemoryFreeClear(pSender);
×
100
    TAOS_RETURN(code);
×
101
  }
102
  pSndBuf->entryDeleteCb = syncSnapBlockDestroy;
65,243,856✔
103
  pSender->pSndBuf = pSndBuf;
65,243,856✔
104

105
  syncSnapBufferReset(pSender->pSndBuf);
65,243,856✔
106
  *ppSender = pSender;
65,241,616✔
107
  TAOS_RETURN(code);
65,242,280✔
108
}
109

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

120
static int32_t snapshotSenderClearInfoData(SSyncSnapshotSender *pSender) {
65,089,255✔
121
  if (pSender->snapshotParam.data) {
65,089,255✔
122
    taosMemoryFree(pSender->snapshotParam.data);
28,708✔
123
    pSender->snapshotParam.data = NULL;
28,708✔
124
  }
125

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

133
void snapshotSenderDestroy(SSyncSnapshotSender *pSender) {
65,061,429✔
134
  if (pSender == NULL) return;
65,061,429✔
135

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

142
  // free snap buffer
143
  if (pSender->pSndBuf) {
65,061,043✔
144
    syncSnapBufferDestroy(&pSender->pSndBuf);
65,062,035✔
145
  }
146

147
  (void)snapshotSenderClearInfoData(pSender);
65,058,368✔
148

149
  // free sender
150
  taosMemoryFree(pSender);
65,060,547✔
151
}
152

153
bool snapshotSenderIsStart(SSyncSnapshotSender *pSender) { return atomic_load_8(&pSender->start); }
65,344,454✔
154

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

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

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

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

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

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

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

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

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

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

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

231
    syncSnapBufferReset(pSender->pSndBuf);
28,708✔
232

233
    (void)snapshotSenderClearInfoData(pSender);
28,708✔
234

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

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

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

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

262
  if (pBlock != NULL && blockLen > 0) {
444,719✔
263
    (void)memcpy(pMsg->data, pBlock, blockLen);
387,478✔
264
  }
265
  pMsg->payloadType = typ;
444,719✔
266

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

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

283
  if (pSender->seq < SYNC_SNAPSHOT_SEQ_END) {
444,719✔
284
    pSender->seq++;
416,186✔
285

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

293
      pBlk->seq = pSender->seq;
387,478✔
294

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

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

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

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

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

341
_OUT:;
444,719✔
342
  if (pBlk != NULL) {
444,719✔
343
    syncSnapBlockDestroy(pBlk);
28,708✔
344
    pBlk = NULL;
28,708✔
345
  }
346
  TAOS_RETURN(code);
444,719✔
347
}
348

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

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

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

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

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

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

403
  taosMsleep(1);
28,708✔
404

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

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

414
  return 0;
28,708✔
415
}
416

417
// receiver
418
int32_t snapshotReceiverCreate(SSyncNode *pSyncNode, SRaftId fromId, SSyncSnapshotReceiver **ppReceiver) {
4,311,544✔
419
  int32_t code = 0;
4,311,544✔
420
  *ppReceiver = NULL;
4,311,544✔
421
  bool condition = (pSyncNode->pFsm->FpSnapshotStartWrite != NULL) && (pSyncNode->pFsm->FpSnapshotStopWrite != NULL) &&
8,623,126✔
422
                   (pSyncNode->pFsm->FpSnapshotDoWrite != NULL);
4,311,225✔
423
  if (!condition) {
4,311,901✔
424
    TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
×
425
  }
426

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

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

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

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

469
static int32_t snapshotReceiverClearInfoData(SSyncSnapshotReceiver *pReceiver) {
4,340,538✔
470
  if (pReceiver->snapshotParam.data) {
4,340,538✔
471
    taosMemoryFree(pReceiver->snapshotParam.data);
28,696✔
472
    pReceiver->snapshotParam.data = NULL;
28,696✔
473
  }
474

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

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

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

498
  (void)taosThreadMutexDestroy(&pReceiver->writerMutex);
4,311,842✔
499

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

505
  (void)snapshotReceiverClearInfoData(pReceiver);
4,311,842✔
506

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

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

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

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

551
  // update ack
552
  pReceiver->ack = SYNC_SNAPSHOT_SEQ_BEGIN;
28,696✔
553

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

664
    // update progress
665
    pReceiver->ack = SYNC_SNAPSHOT_SEQ_END;
28,524✔
666

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

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

690
  return 0;
28,524✔
691
}
692

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

699
  (void)taosThreadMutexLock(&pReceiver->writerMutex);
353,116✔
700

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

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

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

720
  (void)taosThreadMutexUnlock(&pReceiver->writerMutex);
353,022✔
721

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

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

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

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

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

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

746
  return snapStart;
57,392✔
747
}
748

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

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

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

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

789
_exit:
28,696✔
790
  TAOS_RETURN(code);
28,696✔
791
}
792

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

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

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

825
  snapshotReceiverStart(pReceiver, pMsg);
28,696✔
826

827
_SEND_REPLY:;
28,696✔
828

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

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

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

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

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

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

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

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

882
  code = 0;
28,696✔
883
_SEND_REPLY:
28,696✔
884

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

888
  TAOS_RETURN(code);
28,696✔
889
}
890

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

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

914
  if (pBlock != NULL && blockLen > 0) {
410,508✔
915
    (void)memcpy(pRspMsg->data, pBlock, blockLen);
28,696✔
916
  }
917

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

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

931
  (void)taosThreadMutexLock(&pRcvBuf->mutex);
353,538✔
932

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

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

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

952
  for (int64_t seq = pRcvBuf->cursor + 1; seq < pRcvBuf->end; ++seq) {
706,654✔
953
    if (pRcvBuf->entries[seq % pRcvBuf->size]) {
491,365✔
954
      pRcvBuf->cursor = seq;
353,116✔
955
    } else {
956
      break;
138,249✔
957
    }
958
  }
959

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

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

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

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

995
  return syncSnapBufferRecv(pReceiver, ppMsg);
353,538✔
996
}
997

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

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

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

1016
_SEND_REPLY:;
×
1017

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

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

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

1043
  TAOS_RETURN(code);
28,524✔
1044
}
1045

1046
int64_t lastRecvPrintLog = 0;
1047

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

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

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

1069
  if (pSyncNode->raftCfg.cfg.nodeInfo[pSyncNode->raftCfg.cfg.myIndex].nodeRole != TAOS_SYNC_ROLE_LEARNER) {
439,454✔
1070
    if (pMsg->term > raftStoreGetTerm(pSyncNode)) {
203,865✔
1071
      syncNodeStepDown(pSyncNode, pMsg->term, pMsg->srcId, "snapshot");
×
1072
    }
1073
  } else {
1074
    syncNodeUpdateTermWithoutStepDown(pSyncNode, pMsg->term);
235,589✔
1075
  }
1076

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1210
  // update sender
1211
  pSender->snapshot = snapshot;
28,708✔
1212

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

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

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

1227
  code = snapshotSend(pSender);
28,708✔
1228

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

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

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

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

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

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

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

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

1282
  for (int64_t ack = pSndBuf->cursor + 1; ack < pSndBuf->end; ++ack) {
735,386✔
1283
    SyncSnapBlock *pBlk = pSndBuf->entries[ack % pSndBuf->size];
678,145✔
1284
    if (pBlk->acked) {
678,145✔
1285
      pSndBuf->cursor = ack;
353,339✔
1286
    } else {
1287
      break;
324,806✔
1288
    }
1289
  }
1290

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

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

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

1313
int64_t lastSendPrintLog = 0;
1314

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

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

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

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

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

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

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

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

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

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

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

1407
  return 0;
439,288✔
1408

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

© 2026 Coveralls, Inc