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

taosdata / TDengine / #4828

29 Oct 2025 11:10AM UTC coverage: 61.071% (-0.3%) from 61.383%
#4828

push

travis-ci

web-flow
Merge pull request #33419 from taosdata/3.0

3.0

155924 of 324872 branches covered (48.0%)

Branch coverage included in aggregate %.

207404 of 270051 relevant lines covered (76.8%)

243478715.98 hits per line

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

64.46
/source/dnode/vnode/src/tsdb/tsdbSnapInfo.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
#include "tsdb.h"
17
#include "tsdbFS2.h"
18

19
#define TSDB_SNAP_MSG_VER 1
20

21
// fset partition
22
static int32_t tsdbFSetPartCmprFn(STsdbFSetPartition* x, STsdbFSetPartition* y) {
298,705✔
23
  if (x->fid < y->fid) return -1;
298,705✔
24
  if (x->fid > y->fid) return 1;
151,929!
25
  return 0;
×
26
}
27

28
static int32_t tVersionRangeCmprFn(SVersionRange* x, SVersionRange* y) {
29,729✔
29
  if (x->minVer < y->minVer) return -1;
29,729!
30
  if (x->minVer > y->minVer) return 1;
×
31
  if (x->maxVer < y->maxVer) return -1;
×
32
  if (x->maxVer > y->maxVer) return 1;
×
33
  return 0;
×
34
}
35

36
static int32_t tsdbTFileSetRangeCmprFn(STFileSetRange* x, STFileSetRange* y) {
×
37
  if (x->fid < y->fid) return -1;
×
38
  if (x->fid > y->fid) return 1;
×
39
  return 0;
×
40
}
41

42
STsdbFSetPartition* tsdbFSetPartitionCreate() {
210,394✔
43
  STsdbFSetPartition* pSP = taosMemoryCalloc(1, sizeof(STsdbFSetPartition));
210,394!
44
  if (pSP == NULL) {
210,394!
45
    return NULL;
×
46
  }
47
  for (int32_t i = 0; i < TSDB_FSET_RANGE_TYP_MAX; i++) {
1,262,364✔
48
    TARRAY2_INIT(&pSP->verRanges[i]);
1,051,970✔
49
  }
50
  return pSP;
210,394✔
51
}
52

53
void tsdbFSetPartitionClear(STsdbFSetPartition** ppSP) {
210,394✔
54
  if (ppSP == NULL || ppSP[0] == NULL) {
210,394!
55
    return;
×
56
  }
57
  for (int32_t i = 0; i < TSDB_FSET_RANGE_TYP_MAX; i++) {
1,262,364✔
58
    TARRAY2_DESTROY(&ppSP[0]->verRanges[i], NULL);
1,051,970!
59
  }
60
  taosMemoryFree(ppSP[0]);
210,394!
61
  ppSP[0] = NULL;
210,394✔
62
}
63

64
static int32_t tsdbFTypeToFRangeType(tsdb_ftype_t ftype) {
354,564✔
65
  switch (ftype) {
354,564!
66
    case TSDB_FTYPE_HEAD:
113,960✔
67
      return TSDB_FSET_RANGE_TYP_HEAD;
113,960✔
68
    case TSDB_FTYPE_DATA:
113,960✔
69
      return TSDB_FSET_RANGE_TYP_DATA;
113,960✔
70
    case TSDB_FTYPE_SMA:
113,960✔
71
      return TSDB_FSET_RANGE_TYP_SMA;
113,960✔
72
    case TSDB_FTYPE_TOMB:
12,684✔
73
      return TSDB_FSET_RANGE_TYP_TOMB;
12,684✔
74
    case TSDB_FTYPE_STT:
×
75
      return TSDB_FSET_RANGE_TYP_STT;
×
76
  }
77
  return TSDB_FSET_RANGE_TYP_MAX;
×
78
}
79

80
static int32_t tsdbTFileSetToFSetPartition(STFileSet* fset, STsdbFSetPartition** ppSP) {
199,576✔
81
  STsdbFSetPartition* p = tsdbFSetPartitionCreate();
199,576✔
82
  if (p == NULL) {
199,576!
83
    return terrno;
×
84
  }
85

86
  p->fid = fset->fid;
199,576✔
87

88
  int32_t code = 0;
199,576✔
89
  int32_t typ = 0;
199,576✔
90
  int32_t corrupt = false;
199,576✔
91
  int32_t count = 0;
199,576✔
92
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
997,880✔
93
    if (fset->farr[ftype] == NULL) continue;
798,304✔
94
    typ = tsdbFTypeToFRangeType(ftype);
354,564✔
95
    STFile* f = fset->farr[ftype]->f;
354,564✔
96
    if (f->maxVer > fset->maxVerValid) {
354,564!
97
      corrupt = true;
×
98
      tsdbError("skip incomplete data file: fid:%d, maxVerValid:%" PRId64 ", minVer:%" PRId64 ", maxVer:%" PRId64
×
99
                ", ftype: %d",
100
                fset->fid, fset->maxVerValid, f->minVer, f->maxVer, ftype);
101
      continue;
×
102
    }
103
    count++;
354,564✔
104
    SVersionRange vr = {.minVer = f->minVer, .maxVer = f->maxVer};
354,564✔
105
    code = TARRAY2_SORT_INSERT(&p->verRanges[typ], vr, tVersionRangeCmprFn);
354,564✔
106
    if (code) {
354,564!
107
      tsdbFSetPartitionClear(&p);
×
108
      return code;
×
109
    }
110
  }
111

112
  typ = TSDB_FSET_RANGE_TYP_STT;
199,576✔
113
  const SSttLvl* lvl;
114
  TARRAY2_FOREACH(fset->lvlArr, lvl) {
353,739✔
115
    STFileObj* fobj;
116
    TARRAY2_FOREACH(lvl->fobjArr, fobj) {
308,326✔
117
      STFile* f = fobj->f;
154,163✔
118
      if (f->maxVer > fset->maxVerValid) {
154,163!
119
        corrupt = true;
×
120
        tsdbError("skip incomplete stt file.fid:%d, maxVerValid:%" PRId64 ", minVer:%" PRId64 ", maxVer:%" PRId64
×
121
                  ", ftype: %d",
122
                  fset->fid, fset->maxVerValid, f->minVer, f->maxVer, typ);
123
        continue;
×
124
      }
125
      count++;
154,163✔
126
      SVersionRange vr = {.minVer = f->minVer, .maxVer = f->maxVer};
154,163✔
127
      code = TARRAY2_SORT_INSERT(&p->verRanges[typ], vr, tVersionRangeCmprFn);
154,163✔
128
      if (code) {
154,163!
129
        tsdbFSetPartitionClear(&p);
×
130
        return code;
×
131
      }
132
    }
133
  }
134
  if (corrupt && count == 0) {
199,576!
135
    SVersionRange vr = {.minVer = VERSION_MIN, .maxVer = fset->maxVerValid};
×
136
    code = TARRAY2_SORT_INSERT(&p->verRanges[typ], vr, tVersionRangeCmprFn);
×
137
    if (code) {
×
138
      tsdbFSetPartitionClear(&p);
×
139
      return code;
×
140
    }
141
  }
142
  ppSP[0] = p;
199,576✔
143
  return 0;
199,576✔
144
}
145

146
// fset partition list
147
STsdbFSetPartList* tsdbFSetPartListCreate() {
354,560✔
148
  STsdbFSetPartList* pList = taosMemoryCalloc(1, sizeof(STsdbFSetPartList));
354,560!
149
  if (pList == NULL) {
354,560!
150
    return NULL;
×
151
  }
152
  TARRAY2_INIT(pList);
354,560✔
153
  return pList;
354,560✔
154
}
155

156
void tsdbFSetPartListDestroy(STsdbFSetPartList** ppList) {
354,560✔
157
  if (ppList == NULL || ppList[0] == NULL) return;
354,560!
158

159
  TARRAY2_DESTROY(ppList[0], tsdbFSetPartitionClear);
564,954!
160
  taosMemoryFree(ppList[0]);
354,560!
161
  ppList[0] = NULL;
354,560✔
162
}
163

164
int32_t tsdbFSetPartListToRangeDiff(STsdbFSetPartList* pList, TFileSetRangeArray** ppRanges) {
177,280✔
165
  int32_t code = 0;
177,280✔
166

167
  TFileSetRangeArray* pDiff = taosMemoryCalloc(1, sizeof(TFileSetRangeArray));
177,280!
168
  if (pDiff == NULL) {
177,280!
169
    code = terrno;
×
170
    goto _err;
×
171
  }
172
  TARRAY2_INIT(pDiff);
177,280✔
173

174
  STsdbFSetPartition* part;
175
  TARRAY2_FOREACH(pList, part) {
188,098✔
176
    STFileSetRange* r = taosMemoryCalloc(1, sizeof(STFileSetRange));
10,818!
177
    if (r == NULL) {
10,818!
178
      code = terrno;
×
179
      goto _err;
×
180
    }
181
    int64_t maxVerValid = -1;
10,818✔
182
    int32_t typMax = TSDB_FSET_RANGE_TYP_MAX;
10,818✔
183
    for (int32_t i = 0; i < typMax; i++) {
64,908✔
184
      SVerRangeList* iList = &part->verRanges[i];
54,090✔
185
      SVersionRange  vr = {0};
54,090✔
186
      TARRAY2_FOREACH(iList, vr) {
81,216✔
187
        if (vr.maxVer < vr.minVer) {
27,126!
188
          continue;
×
189
        }
190
        maxVerValid = TMAX(maxVerValid, vr.maxVer);
27,126✔
191
      }
192
    }
193
    r->fid = part->fid;
10,818✔
194
    r->sver = maxVerValid + 1;
10,818✔
195
    r->ever = VERSION_MAX;
10,818✔
196
    tsdbDebug("range diff fid:%" PRId64 ", sver:%" PRId64 ", ever:%" PRId64, part->fid, r->sver, r->ever);
10,818!
197
    code = TARRAY2_SORT_INSERT(pDiff, r, tsdbTFileSetRangeCmprFn);
10,818✔
198
    if (code) {
10,818!
199
      taosMemoryFree(r);
×
200
      goto _err;
×
201
    }
202
  }
203
  ppRanges[0] = pDiff;
177,280✔
204

205
  tsdbInfo("pDiff size:%d", TARRAY2_SIZE(pDiff));
177,280!
206
  return 0;
177,280✔
207

208
_err:
×
209
  if (pDiff) {
×
210
    tsdbTFileSetRangeArrayDestroy(&pDiff);
×
211
  }
212
  return code;
×
213
}
214

215
// serialization
216
int32_t tTsdbFSetPartListDataLenCalc(STsdbFSetPartList* pList) {
177,280✔
217
  int32_t hdrLen = sizeof(int32_t);
177,280✔
218
  int32_t datLen = 0;
177,280✔
219

220
  int8_t  msgVer = 1;
177,280✔
221
  int32_t len = TARRAY2_SIZE(pList);
177,280✔
222
  hdrLen += sizeof(msgVer);
177,280✔
223
  hdrLen += sizeof(len);
177,280✔
224
  datLen += hdrLen;
177,280✔
225

226
  for (int32_t u = 0; u < len; u++) {
376,856✔
227
    STsdbFSetPartition* p = TARRAY2_GET(pList, u);
199,576✔
228
    int32_t             typMax = TSDB_FSET_RANGE_TYP_MAX;
199,576✔
229
    int32_t             uItem = 0;
199,576✔
230
    uItem += sizeof(STsdbFSetPartition);
199,576✔
231
    uItem += sizeof(typMax);
199,576✔
232

233
    for (int32_t i = 0; i < typMax; i++) {
1,197,456✔
234
      int32_t iLen = TARRAY2_SIZE(&p->verRanges[i]);
997,880✔
235
      int32_t jItem = 0;
997,880✔
236
      jItem += sizeof(SVersionRange);
997,880✔
237
      jItem += sizeof(int64_t);
997,880✔
238
      uItem += sizeof(iLen) + jItem * iLen;
997,880✔
239
    }
240
    datLen += uItem;
199,576✔
241
  }
242
  return datLen;
177,280✔
243
}
244

245
static int32_t tSerializeTsdbFSetPartList(void* buf, int32_t bufLen, STsdbFSetPartList* pList, int32_t* encodeSize) {
177,280✔
246
  SEncoder encoder = {0};
177,280✔
247
  int8_t   reserved8 = 0;
177,280✔
248
  int16_t  reserved16 = 0;
177,280✔
249
  int64_t  reserved64 = 0;
177,280✔
250
  int8_t   msgVer = TSDB_SNAP_MSG_VER;
177,280✔
251
  int32_t  len = TARRAY2_SIZE(pList);
177,280✔
252
  int32_t  code = 0;
177,280✔
253

254
  tEncoderInit(&encoder, buf, bufLen);
177,280✔
255
  if ((code = tStartEncode(&encoder))) goto _exit;
177,280!
256
  if ((code = tEncodeI8(&encoder, msgVer))) goto _exit;
354,560!
257
  if ((code = tEncodeI32(&encoder, len))) goto _exit;
177,280!
258

259
  for (int32_t u = 0; u < len; u++) {
376,856✔
260
    STsdbFSetPartition* p = TARRAY2_GET(pList, u);
199,576✔
261
    if ((code = tEncodeI64(&encoder, p->fid))) goto _exit;
399,152!
262
    if ((code = tEncodeI8(&encoder, p->stat))) goto _exit;
399,152!
263
    if ((code = tEncodeI8(&encoder, reserved8))) goto _exit;
399,152!
264
    if ((code = tEncodeI16(&encoder, reserved16))) goto _exit;
399,152!
265

266
    int32_t typMax = TSDB_FSET_RANGE_TYP_MAX;
199,576!
267
    if ((code = tEncodeI32(&encoder, typMax))) goto _exit;
199,576!
268

269
    for (int32_t i = 0; i < typMax; i++) {
1,197,456✔
270
      SVerRangeList* iList = &p->verRanges[i];
997,880✔
271
      int32_t        iLen = TARRAY2_SIZE(iList);
997,880!
272

273
      if ((code = tEncodeI32(&encoder, iLen))) goto _exit;
997,880!
274
      for (int32_t j = 0; j < iLen; j++) {
1,506,607✔
275
        SVersionRange r = TARRAY2_GET(iList, j);
508,727✔
276
        if ((code = tEncodeI64(&encoder, r.minVer))) goto _exit;
1,017,454!
277
        if ((code = tEncodeI64(&encoder, r.maxVer))) goto _exit;
1,017,454!
278
        if ((code = tEncodeI64(&encoder, reserved64))) goto _exit;
508,727!
279
      }
280
    }
281
  }
282

283
  tEndEncode(&encoder);
177,280✔
284

285
  if (encodeSize) {
177,280!
286
    encodeSize[0] = encoder.pos;
177,280✔
287
  }
288

289
_exit:
177,280✔
290
  tEncoderClear(&encoder);
177,280✔
291
  return code;
177,280✔
292
}
293

294
int32_t tDeserializeTsdbFSetPartList(void* buf, int32_t bufLen, STsdbFSetPartList* pList) {
177,280✔
295
  SDecoder decoder = {0};
177,280✔
296
  int8_t   reserved8 = 0;
177,280✔
297
  int16_t  reserved16 = 0;
177,280✔
298
  int64_t  reserved64 = 0;
177,280✔
299
  int32_t  code = 0;
177,280✔
300

301
  STsdbFSetPartition* p = NULL;
177,280✔
302

303
  tDecoderInit(&decoder, buf, bufLen);
177,280✔
304
  int8_t  msgVer = 0;
177,280✔
305
  int32_t len = 0;
177,280✔
306
  if ((code = tStartDecode(&decoder))) goto _err;
177,280!
307
  if ((code = tDecodeI8(&decoder, &msgVer))) goto _err;
177,280!
308
  if (msgVer != TSDB_SNAP_MSG_VER) {
177,280!
309
    code = TSDB_CODE_INVALID_MSG;
×
310
    goto _err;
×
311
  }
312
  if ((code = tDecodeI32(&decoder, &len))) goto _err;
177,280!
313

314
  for (int32_t u = 0; u < len; u++) {
188,098✔
315
    p = tsdbFSetPartitionCreate();
10,818✔
316
    if (p == NULL) {
10,818!
317
      code = terrno;
×
318
      goto _err;
×
319
    }
320

321
    if ((code = tDecodeI64(&decoder, &p->fid))) goto _err;
21,636!
322
    if ((code = tDecodeI8(&decoder, &p->stat))) goto _err;
21,636!
323
    if ((code = tDecodeI8(&decoder, &reserved8))) goto _err;
10,818!
324
    if ((code = tDecodeI16(&decoder, &reserved16))) goto _err;
10,818!
325

326
    int32_t typMax = 0;
10,818✔
327
    if ((code = tDecodeI32(&decoder, &typMax))) goto _err;
10,818!
328

329
    for (int32_t i = 0; i < typMax; i++) {
64,908✔
330
      SVerRangeList* iList = &p->verRanges[i];
54,090✔
331
      int32_t        iLen = 0;
54,090✔
332
      if ((code = tDecodeI32(&decoder, &iLen))) goto _err;
54,090!
333
      for (int32_t j = 0; j < iLen; j++) {
81,216✔
334
        SVersionRange r = {0};
27,126✔
335
        if ((code = tDecodeI64(&decoder, &r.minVer))) goto _err;
27,126!
336
        if ((code = tDecodeI64(&decoder, &r.maxVer))) goto _err;
27,126!
337
        if ((code = tDecodeI64(&decoder, &reserved64))) goto _err;
27,126!
338
        if ((code = TARRAY2_APPEND(iList, r))) goto _err;
54,252!
339
      }
340
    }
341
    if ((code = TARRAY2_APPEND(pList, p))) goto _err;
21,636!
342
    p = NULL;
10,818✔
343
  }
344

345
  tEndDecode(&decoder);
177,280✔
346
  tDecoderClear(&decoder);
177,280✔
347
  return 0;
177,280✔
348

349
_err:
×
350
  if (p) {
×
351
    tsdbFSetPartitionClear(&p);
×
352
  }
353
  tDecoderClear(&decoder);
×
354
  return code;
×
355
}
356

357
// fs state
358
static STsdbFSetPartList* tsdbSnapGetFSetPartList(STFileSystem* fs) {
177,280✔
359
  STsdbFSetPartList* pList = tsdbFSetPartListCreate();
177,280✔
360
  if (pList == NULL) {
177,280!
361
    return NULL;
×
362
  }
363

364
  int32_t code = 0;
177,280✔
365
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
177,280✔
366
  STFileSet* fset;
367
  TARRAY2_FOREACH(fs->fSetArr, fset) {
376,856✔
368
    STsdbFSetPartition* pItem = NULL;
199,576✔
369
    code = tsdbTFileSetToFSetPartition(fset, &pItem);
199,576✔
370
    if (code) {
199,576!
371
      terrno = code;
×
372
      break;
×
373
    }
374
    code = TARRAY2_SORT_INSERT(pList, pItem, tsdbFSetPartCmprFn);
199,576✔
375
    if (code) {
199,576!
376
      terrno = code;
×
377
      break;
×
378
    }
379
  }
380
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
177,280✔
381

382
  if (code) {
177,280!
383
    TARRAY2_DESTROY(pList, tsdbFSetPartitionClear);
×
384
    taosMemoryFree(pList);
×
385
    pList = NULL;
×
386
  }
387
  return pList;
177,280✔
388
}
389

390
ETsdbFsState tsdbSnapGetFsState(SVnode* pVnode) { return pVnode->pTsdb->pFS->fsstate; }
319,057,596✔
391

392
// description
393
typedef struct STsdbPartitionInfo {
394
  int32_t            vgId;
395
  int32_t            tsdbMaxCnt;
396
  int32_t            subTyps[TSDB_RETENTION_MAX];
397
  STsdbFSetPartList* pLists[TSDB_RETENTION_MAX];
398
} STsdbPartitionInfo;
399

400
static int32_t tsdbPartitionInfoInit(SVnode* pVnode, STsdbPartitionInfo* pInfo) {
177,280✔
401
  int32_t subTyps[TSDB_RETENTION_MAX] = {SNAP_DATA_TSDB, SNAP_DATA_RSMA1, SNAP_DATA_RSMA2};
177,280✔
402
  pInfo->vgId = TD_VID(pVnode);
177,280✔
403
  pInfo->tsdbMaxCnt = 1;
177,280✔
404

405
  if (!(sizeof(pInfo->subTyps) == sizeof(subTyps))) {
406
    return TSDB_CODE_INVALID_PARA;
407
  }
408
  memcpy(pInfo->subTyps, (char*)subTyps, sizeof(subTyps));
177,280!
409

410
  // fset partition list
411
  memset(pInfo->pLists, 0, sizeof(pInfo->pLists[0]) * TSDB_RETENTION_MAX);
177,280!
412
  for (int32_t j = 0; j < pInfo->tsdbMaxCnt; ++j) {
354,560✔
413
    STsdb* pTsdb = SMA_RSMA_GET_TSDB(pVnode, j);
177,280!
414
    pInfo->pLists[j] = tsdbSnapGetFSetPartList(pTsdb->pFS);
177,280✔
415
    if (pInfo->pLists[j] == NULL) {
177,280!
416
      return terrno;
×
417
    }
418
  }
419
  return 0;
177,280✔
420
}
421

422
static void tsdbPartitionInfoClear(STsdbPartitionInfo* pInfo) {
177,280✔
423
  for (int32_t j = 0; j < pInfo->tsdbMaxCnt; ++j) {
354,560✔
424
    if (pInfo->pLists[j] == NULL) continue;
177,280!
425
    tsdbFSetPartListDestroy(&pInfo->pLists[j]);
177,280✔
426
  }
427
}
177,280✔
428

429
static int32_t tsdbPartitionInfoEstSize(STsdbPartitionInfo* pInfo) {
177,280✔
430
  int32_t dataLen = 0;
177,280✔
431
  for (int32_t j = 0; j < pInfo->tsdbMaxCnt; ++j) {
354,560✔
432
    dataLen += sizeof(SSyncTLV);  // subTyps[j]
177,280✔
433
    dataLen += tTsdbFSetPartListDataLenCalc(pInfo->pLists[j]);
177,280✔
434
  }
435
  return dataLen;
177,280✔
436
}
437

438
static int32_t tsdbPartitionInfoSerialize(STsdbPartitionInfo* pInfo, uint8_t* buf, int32_t bufLen) {
177,280✔
439
  int32_t tlen = 0;
177,280✔
440
  int32_t offset = 0;
177,280✔
441
  for (int32_t j = 0; j < pInfo->tsdbMaxCnt; ++j) {
354,560✔
442
    SSyncTLV* pSubHead = (void*)((char*)buf + offset);
177,280✔
443
    int32_t   valOffset = offset + sizeof(*pSubHead);
177,280✔
444
    int32_t   code = tSerializeTsdbFSetPartList(pSubHead->val, bufLen - valOffset, pInfo->pLists[j], &tlen);
177,280✔
445
    if (code) {
177,280!
446
      tsdbError("vgId:%d, failed to serialize fset partition list of tsdb %d since %s", pInfo->vgId, j, terrstr());
×
447
      return code;
×
448
    }
449
    pSubHead->typ = pInfo->subTyps[j];
177,280✔
450
    pSubHead->len = tlen;
177,280✔
451
    offset += sizeof(*pSubHead) + tlen;
177,280✔
452
  }
453
  return offset;
177,280✔
454
}
455

456
// tsdb replication opts
457
static int32_t tTsdbRepOptsDataLenCalc(STsdbRepOpts* pInfo) {
177,280✔
458
  int32_t hdrLen = sizeof(int32_t);
177,280✔
459
  int32_t datLen = 0;
177,280✔
460

461
  int8_t  msgVer = 0;
177,280✔
462
  int64_t reserved64 = 0;
177,280✔
463
  int16_t format = 0;
177,280✔
464
  hdrLen += sizeof(msgVer);
177,280✔
465
  datLen += hdrLen;
177,280✔
466
  datLen += sizeof(format);
177,280✔
467
  datLen += sizeof(reserved64);
177,280✔
468
  datLen += sizeof(*pInfo);
177,280✔
469
  return datLen;
177,280✔
470
}
471

472
int32_t tSerializeTsdbRepOpts(void* buf, int32_t bufLen, STsdbRepOpts* pOpts) {
177,280✔
473
  int32_t  code = 0;
177,280✔
474
  SEncoder encoder = {0};
177,280✔
475
  int64_t  reserved64 = 0;
177,280✔
476
  int8_t   msgVer = TSDB_SNAP_MSG_VER;
177,280✔
477

478
  tEncoderInit(&encoder, buf, bufLen);
177,280✔
479

480
  if ((code = tStartEncode(&encoder))) goto _err;
177,280!
481
  if ((code = tEncodeI8(&encoder, msgVer))) goto _err;
354,560!
482
  int16_t format = pOpts->format;
177,280✔
483
  if ((code = tEncodeI16(&encoder, format))) goto _err;
354,560!
484
  if ((code = tEncodeI64(&encoder, reserved64))) goto _err;
177,280!
485

486
  tEndEncode(&encoder);
177,280✔
487
  int32_t tlen = encoder.pos;
177,280✔
488
  tEncoderClear(&encoder);
177,280✔
489
  return tlen;
177,280✔
490

491
_err:
×
492
  tEncoderClear(&encoder);
×
493
  return code;
×
494
}
495

496
int32_t tDeserializeTsdbRepOpts(void* buf, int32_t bufLen, STsdbRepOpts* pOpts) {
265,915✔
497
  int32_t  code;
498
  SDecoder decoder = {0};
265,915✔
499
  int64_t  reserved64 = 0;
265,915✔
500
  int8_t   msgVer = 0;
265,915✔
501

502
  tDecoderInit(&decoder, buf, bufLen);
265,915✔
503

504
  if ((code = tStartDecode(&decoder))) goto _err;
265,915!
505
  if ((code = tDecodeI8(&decoder, &msgVer))) goto _err;
265,915!
506
  if (msgVer != TSDB_SNAP_MSG_VER) goto _err;
265,915!
507
  int16_t format = 0;
265,915✔
508
  if ((code = tDecodeI16(&decoder, &format))) goto _err;
265,915!
509
  pOpts->format = format;
265,915✔
510
  if ((code = tDecodeI64(&decoder, &reserved64))) goto _err;
265,915!
511

512
  tEndDecode(&decoder);
265,915✔
513
  tDecoderClear(&decoder);
265,915✔
514
  return 0;
265,915✔
515

516
_err:
×
517
  tDecoderClear(&decoder);
×
518
  return code;
×
519
}
520

521
static int32_t tsdbRepOptsEstSize(STsdbRepOpts* pOpts) {
177,280✔
522
  int32_t dataLen = 0;
177,280✔
523
  dataLen += sizeof(SSyncTLV);
177,280✔
524
  dataLen += tTsdbRepOptsDataLenCalc(pOpts);
177,280✔
525
  return dataLen;
177,280✔
526
}
527

528
static int32_t tsdbRepOptsSerialize(STsdbRepOpts* pOpts, void* buf, int32_t bufLen) {
177,280✔
529
  SSyncTLV* pSubHead = buf;
177,280✔
530
  int32_t   offset = 0;
177,280✔
531
  int32_t   tlen = 0;
177,280✔
532
  if ((tlen = tSerializeTsdbRepOpts(pSubHead->val, bufLen, pOpts)) < 0) {
177,280!
533
    return tlen;
×
534
  }
535
  pSubHead->typ = SNAP_DATA_RAW;
177,280✔
536
  pSubHead->len = tlen;
177,280✔
537
  offset += sizeof(*pSubHead) + tlen;
177,280✔
538
  return offset;
177,280✔
539
}
540

541
// snap info
542
static int32_t tsdbSnapPrepDealWithSnapInfo(SVnode* pVnode, SSnapshot* pSnap, STsdbRepOpts* pInfo) {
88,635✔
543
  if (!pSnap->data) {
88,635!
544
    return 0;
×
545
  }
546
  int32_t code = 0;
88,635✔
547

548
  SSyncTLV* pHead = (void*)pSnap->data;
88,635✔
549
  int32_t   offset = 0;
88,635✔
550

551
  while (offset + sizeof(*pHead) < pHead->len) {
265,905✔
552
    SSyncTLV* pField = (void*)(pHead->val + offset);
177,270✔
553
    offset += sizeof(*pField) + pField->len;
177,270✔
554
    void*   buf = pField->val;
177,270✔
555
    int32_t bufLen = pField->len;
177,270✔
556

557
    switch (pField->typ) {
177,270!
558
      case SNAP_DATA_TSDB:
88,635✔
559
      case SNAP_DATA_RSMA1:
560
      case SNAP_DATA_RSMA2: {
561
      } break;
88,635✔
562
      case SNAP_DATA_RAW: {
88,635✔
563
        code = tDeserializeTsdbRepOpts(buf, bufLen, pInfo);
88,635✔
564
        if (code < 0) {
88,635!
565
          tsdbError("vgId:%d, failed to deserialize tsdb rep opts since %s", TD_VID(pVnode), terrstr());
×
566
          return code;
×
567
        }
568
      } break;
88,635✔
569
      default:
×
570
        code = TSDB_CODE_INVALID_MSG;
×
571
        tsdbError("vgId:%d, unexpected subfield type of snap info. typ:%d", TD_VID(pVnode), pField->typ);
×
572
        return code;
×
573
    }
574
  }
575

576
  return code;
88,635✔
577
}
578

579
int32_t tsdbSnapPrepDescription(SVnode* pVnode, SSnapshot* pSnap) {
177,280✔
580
  STsdbPartitionInfo  partitionInfo = {0};
177,280✔
581
  int                 code = 0;
177,280✔
582
  STsdbPartitionInfo* pInfo = &partitionInfo;
177,280✔
583

584
  code = tsdbPartitionInfoInit(pVnode, pInfo);
177,280✔
585
  if (code) {
177,280!
586
    goto _out;
×
587
  }
588

589
  // deal with snap info for reply
590
  STsdbRepOpts opts = {.format = TSDB_SNAP_REP_FMT_RAW};
177,280✔
591
  if (pSnap->type == TDMT_SYNC_PREP_SNAPSHOT_REPLY) {
177,280✔
592
    STsdbRepOpts leaderOpts = {0};
88,635✔
593
    if ((code = tsdbSnapPrepDealWithSnapInfo(pVnode, pSnap, &leaderOpts)) < 0) {
88,635!
594
      tsdbError("vgId:%d, failed to deal with snap info for reply since %s", TD_VID(pVnode), terrstr());
×
595
      goto _out;
×
596
    }
597
    opts.format = TMIN(opts.format, leaderOpts.format);
88,635✔
598
  }
599

600
  // info data realloc
601
  const int32_t headLen = sizeof(SSyncTLV);
177,280✔
602
  int32_t       bufLen = headLen;
177,280✔
603
  bufLen += tsdbPartitionInfoEstSize(pInfo);
177,280✔
604
  bufLen += tsdbRepOptsEstSize(&opts);
177,280✔
605
  if ((code = syncSnapInfoDataRealloc(pSnap, bufLen)) != 0) {
177,280!
606
    tsdbError("vgId:%d, failed to realloc memory for data of snap info. bytes:%d", TD_VID(pVnode), bufLen);
×
607
    goto _out;
×
608
  }
609

610
  // serialization
611
  char*   buf = (void*)pSnap->data;
177,280✔
612
  int32_t offset = headLen;
177,280✔
613
  int32_t tlen = 0;
177,280✔
614

615
  if ((tlen = tsdbPartitionInfoSerialize(pInfo, (uint8_t*)(buf + offset), bufLen - offset)) < 0) {
177,280!
616
    code = tlen;
×
617
    tsdbError("vgId:%d, failed to serialize tsdb partition info since %s", TD_VID(pVnode), terrstr());
×
618
    goto _out;
×
619
  }
620
  offset += tlen;
177,280✔
621

622
  if ((tlen = tsdbRepOptsSerialize(&opts, buf + offset, bufLen - offset)) < 0) {
177,280!
623
    code = tlen;
×
624
    tsdbError("vgId:%d, failed to serialize tsdb rep opts since %s", TD_VID(pVnode), terrstr());
×
625
    goto _out;
×
626
  }
627
  offset += tlen;
177,280✔
628

629
  // set header of info data
630
  SSyncTLV* pHead = pSnap->data;
177,280✔
631
  pHead->typ = pSnap->type;
177,280✔
632
  pHead->len = offset - headLen;
177,280✔
633

634
  tsdbInfo("vgId:%d, tsdb snap info prepared. type:%s, val length:%d", TD_VID(pVnode), TMSG_INFO(pHead->typ),
177,280!
635
           pHead->len);
636

637
_out:
177,280✔
638
  tsdbPartitionInfoClear(pInfo);
177,280✔
639
  return code;
177,280✔
640
}
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