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

taosdata / TDengine / #4672

14 Aug 2025 01:04PM UTC coverage: 59.715% (+2.2%) from 57.533%
#4672

push

travis-ci

web-flow
fix(query): fix order by column check of union operator (#32524)

137065 of 292055 branches covered (46.93%)

Branch coverage included in aggregate %.

1 of 13 new or added lines in 1 file covered. (7.69%)

20958 existing lines in 205 files now uncovered.

207155 of 284385 relevant lines covered (72.84%)

20501687.34 hits per line

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

66.24
/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) {
343✔
23
  if (x->fid < y->fid) return -1;
343✔
24
  if (x->fid > y->fid) return 1;
334!
25
  return 0;
×
26
}
27

28
static int32_t tVersionRangeCmprFn(SVersionRange* x, SVersionRange* y) {
75✔
29
  if (x->minVer < y->minVer) return -1;
75✔
30
  if (x->minVer > y->minVer) return 1;
36!
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() {
276✔
43
  STsdbFSetPartition* pSP = taosMemoryCalloc(1, sizeof(STsdbFSetPartition));
276!
44
  if (pSP == NULL) {
276!
45
    return NULL;
×
46
  }
47
  for (int32_t i = 0; i < TSDB_FSET_RANGE_TYP_MAX; i++) {
1,656✔
48
    TARRAY2_INIT(&pSP->verRanges[i]);
1,380✔
49
  }
50
  return pSP;
276✔
51
}
52

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

64
static int32_t tsdbFTypeToFRangeType(tsdb_ftype_t ftype) {
642✔
65
  switch (ftype) {
642!
66
    case TSDB_FTYPE_HEAD:
205✔
67
      return TSDB_FSET_RANGE_TYP_HEAD;
205✔
68
    case TSDB_FTYPE_DATA:
205✔
69
      return TSDB_FSET_RANGE_TYP_DATA;
205✔
70
    case TSDB_FTYPE_SMA:
205✔
71
      return TSDB_FSET_RANGE_TYP_SMA;
205✔
72
    case TSDB_FTYPE_TOMB:
27✔
73
      return TSDB_FSET_RANGE_TYP_TOMB;
27✔
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) {
268✔
81
  STsdbFSetPartition* p = tsdbFSetPartitionCreate();
268✔
82
  if (p == NULL) {
268!
83
    return terrno;
×
84
  }
85

86
  p->fid = fset->fid;
268✔
87

88
  int32_t code = 0;
268✔
89
  int32_t typ = 0;
268✔
90
  int32_t corrupt = false;
268✔
91
  int32_t count = 0;
268✔
92
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
1,340✔
93
    if (fset->farr[ftype] == NULL) continue;
1,072✔
94
    typ = tsdbFTypeToFRangeType(ftype);
642✔
95
    STFile* f = fset->farr[ftype]->f;
642✔
96
    if (f->maxVer > fset->maxVerValid) {
642!
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++;
642✔
104
    SVersionRange vr = {.minVer = f->minVer, .maxVer = f->maxVer};
642✔
105
    code = TARRAY2_SORT_INSERT(&p->verRanges[typ], vr, tVersionRangeCmprFn);
642✔
106
    if (code) {
642!
107
      tsdbFSetPartitionClear(&p);
×
108
      return code;
×
109
    }
110
  }
111

112
  typ = TSDB_FSET_RANGE_TYP_STT;
268✔
113
  const SSttLvl* lvl;
114
  TARRAY2_FOREACH(fset->lvlArr, lvl) {
442✔
115
    STFileObj* fobj;
116
    TARRAY2_FOREACH(lvl->fobjArr, fobj) {
384✔
117
      STFile* f = fobj->f;
210✔
118
      if (f->maxVer > fset->maxVerValid) {
210!
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++;
210✔
126
      SVersionRange vr = {.minVer = f->minVer, .maxVer = f->maxVer};
210✔
127
      code = TARRAY2_SORT_INSERT(&p->verRanges[typ], vr, tVersionRangeCmprFn);
210✔
128
      if (code) {
210!
129
        tsdbFSetPartitionClear(&p);
×
130
        return code;
×
131
      }
132
    }
133
  }
134
  if (corrupt && count == 0) {
268!
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;
268✔
143
  return 0;
268✔
144
}
145

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

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

159
  TARRAY2_DESTROY(ppList[0], tsdbFSetPartitionClear);
816!
160
  taosMemoryFree(ppList[0]);
540!
161
  ppList[0] = NULL;
540✔
162
}
163

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

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

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

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

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

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

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

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

233
    for (int32_t i = 0; i < typMax; i++) {
1,608✔
234
      int32_t iLen = TARRAY2_SIZE(&p->verRanges[i]);
1,340✔
235
      int32_t jItem = 0;
1,340✔
236
      jItem += sizeof(SVersionRange);
1,340✔
237
      jItem += sizeof(int64_t);
1,340✔
238
      uItem += sizeof(iLen) + jItem * iLen;
1,340✔
239
    }
240
    datLen += uItem;
268✔
241
  }
242
  return datLen;
270✔
243
}
244

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

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

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

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

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

273
      if ((code = tEncodeI32(&encoder, iLen))) goto _exit;
1,340!
274
      for (int32_t j = 0; j < iLen; j++) {
2,192✔
275
        SVersionRange r = TARRAY2_GET(iList, j);
852✔
276
        if ((code = tEncodeI64(&encoder, r.minVer))) goto _exit;
1,704!
277
        if ((code = tEncodeI64(&encoder, r.maxVer))) goto _exit;
1,704!
278
        if ((code = tEncodeI64(&encoder, reserved64))) goto _exit;
852!
279
      }
280
    }
281
  }
282

283
  tEndEncode(&encoder);
270✔
284

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

289
_exit:
×
290
  tEncoderClear(&encoder);
270✔
291
  return code;
270✔
292
}
293

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

301
  STsdbFSetPartition* p = NULL;
270✔
302

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

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

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

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

329
    for (int32_t i = 0; i < typMax; i++) {
48✔
330
      SVerRangeList* iList = &p->verRanges[i];
40✔
331
      int32_t        iLen = 0;
40✔
332
      if ((code = tDecodeI32(&decoder, &iLen))) goto _err;
40!
333
      for (int32_t j = 0; j < iLen; j++) {
58✔
334
        SVersionRange r = {0};
18✔
335
        if ((code = tDecodeI64(&decoder, &r.minVer))) goto _err;
18!
336
        if ((code = tDecodeI64(&decoder, &r.maxVer))) goto _err;
18!
337
        if ((code = tDecodeI64(&decoder, &reserved64))) goto _err;
18!
338
        if ((code = TARRAY2_APPEND(iList, r))) goto _err;
36!
339
      }
340
    }
341
    if ((code = TARRAY2_APPEND(pList, p))) goto _err;
16!
342
    p = NULL;
8✔
343
  }
344

345
  tEndDecode(&decoder);
270✔
346
  tDecoderClear(&decoder);
270✔
347
  return 0;
270✔
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) {
270✔
359
  STsdbFSetPartList* pList = tsdbFSetPartListCreate();
270✔
360
  if (pList == NULL) {
270!
361
    return NULL;
×
362
  }
363

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

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

390
ETsdbFsState tsdbSnapGetFsState(SVnode* pVnode) {
371,395✔
391
  if (!VND_IS_RSMA(pVnode)) {
371,395!
392
    return pVnode->pTsdb->pFS->fsstate;
371,411✔
393
  }
UNCOV
394
  for (int32_t lvl = 0; lvl < TSDB_RETENTION_MAX; ++lvl) {
×
UNCOV
395
    STsdb* pTsdb = SMA_RSMA_GET_TSDB(pVnode, lvl);
×
UNCOV
396
    if (pTsdb && pTsdb->pFS->fsstate != TSDB_FS_STATE_NORMAL) {
×
397
      return TSDB_FS_STATE_INCOMPLETE;
×
398
    }
399
  }
UNCOV
400
  return TSDB_FS_STATE_NORMAL;
×
401
}
402

403
// description
404
typedef struct STsdbPartitionInfo {
405
  int32_t            vgId;
406
  int32_t            tsdbMaxCnt;
407
  int32_t            subTyps[TSDB_RETENTION_MAX];
408
  STsdbFSetPartList* pLists[TSDB_RETENTION_MAX];
409
} STsdbPartitionInfo;
410

411
static int32_t tsdbPartitionInfoInit(SVnode* pVnode, STsdbPartitionInfo* pInfo) {
270✔
412
  int32_t subTyps[TSDB_RETENTION_MAX] = {SNAP_DATA_TSDB, SNAP_DATA_RSMA1, SNAP_DATA_RSMA2};
270✔
413
  pInfo->vgId = TD_VID(pVnode);
270✔
414
  pInfo->tsdbMaxCnt = (!VND_IS_RSMA(pVnode) ? 1 : TSDB_RETENTION_MAX);
270!
415

416
  if (!(sizeof(pInfo->subTyps) == sizeof(subTyps))) {
417
    return TSDB_CODE_INVALID_PARA;
418
  }
419
  memcpy(pInfo->subTyps, (char*)subTyps, sizeof(subTyps));
270✔
420

421
  // fset partition list
422
  memset(pInfo->pLists, 0, sizeof(pInfo->pLists[0]) * TSDB_RETENTION_MAX);
270✔
423
  for (int32_t j = 0; j < pInfo->tsdbMaxCnt; ++j) {
540✔
424
    STsdb* pTsdb = SMA_RSMA_GET_TSDB(pVnode, j);
270!
425
    pInfo->pLists[j] = tsdbSnapGetFSetPartList(pTsdb->pFS);
270✔
426
    if (pInfo->pLists[j] == NULL) {
270!
427
      return terrno;
×
428
    }
429
  }
430
  return 0;
270✔
431
}
432

433
static void tsdbPartitionInfoClear(STsdbPartitionInfo* pInfo) {
270✔
434
  for (int32_t j = 0; j < pInfo->tsdbMaxCnt; ++j) {
540✔
435
    if (pInfo->pLists[j] == NULL) continue;
270!
436
    tsdbFSetPartListDestroy(&pInfo->pLists[j]);
270✔
437
  }
438
}
270✔
439

440
static int32_t tsdbPartitionInfoEstSize(STsdbPartitionInfo* pInfo) {
270✔
441
  int32_t dataLen = 0;
270✔
442
  for (int32_t j = 0; j < pInfo->tsdbMaxCnt; ++j) {
540✔
443
    dataLen += sizeof(SSyncTLV);  // subTyps[j]
270✔
444
    dataLen += tTsdbFSetPartListDataLenCalc(pInfo->pLists[j]);
270✔
445
  }
446
  return dataLen;
270✔
447
}
448

449
static int32_t tsdbPartitionInfoSerialize(STsdbPartitionInfo* pInfo, uint8_t* buf, int32_t bufLen) {
270✔
450
  int32_t tlen = 0;
270✔
451
  int32_t offset = 0;
270✔
452
  for (int32_t j = 0; j < pInfo->tsdbMaxCnt; ++j) {
540✔
453
    SSyncTLV* pSubHead = (void*)((char*)buf + offset);
270✔
454
    int32_t   valOffset = offset + sizeof(*pSubHead);
270✔
455
    int32_t   code = tSerializeTsdbFSetPartList(pSubHead->val, bufLen - valOffset, pInfo->pLists[j], &tlen);
270✔
456
    if (code) {
270!
457
      tsdbError("vgId:%d, failed to serialize fset partition list of tsdb %d since %s", pInfo->vgId, j, terrstr());
×
458
      return code;
×
459
    }
460
    pSubHead->typ = pInfo->subTyps[j];
270✔
461
    pSubHead->len = tlen;
270✔
462
    offset += sizeof(*pSubHead) + tlen;
270✔
463
  }
464
  return offset;
270✔
465
}
466

467
// tsdb replication opts
468
static int32_t tTsdbRepOptsDataLenCalc(STsdbRepOpts* pInfo) {
270✔
469
  int32_t hdrLen = sizeof(int32_t);
270✔
470
  int32_t datLen = 0;
270✔
471

472
  int8_t  msgVer = 0;
270✔
473
  int64_t reserved64 = 0;
270✔
474
  int16_t format = 0;
270✔
475
  hdrLen += sizeof(msgVer);
270✔
476
  datLen += hdrLen;
270✔
477
  datLen += sizeof(format);
270✔
478
  datLen += sizeof(reserved64);
270✔
479
  datLen += sizeof(*pInfo);
270✔
480
  return datLen;
270✔
481
}
482

483
int32_t tSerializeTsdbRepOpts(void* buf, int32_t bufLen, STsdbRepOpts* pOpts) {
270✔
484
  int32_t  code = 0;
270✔
485
  SEncoder encoder = {0};
270✔
486
  int64_t  reserved64 = 0;
270✔
487
  int8_t   msgVer = TSDB_SNAP_MSG_VER;
270✔
488

489
  tEncoderInit(&encoder, buf, bufLen);
270✔
490

491
  if ((code = tStartEncode(&encoder))) goto _err;
270!
492
  if ((code = tEncodeI8(&encoder, msgVer))) goto _err;
540!
493
  int16_t format = pOpts->format;
270✔
494
  if ((code = tEncodeI16(&encoder, format))) goto _err;
540!
495
  if ((code = tEncodeI64(&encoder, reserved64))) goto _err;
270!
496

497
  tEndEncode(&encoder);
270✔
498
  int32_t tlen = encoder.pos;
270✔
499
  tEncoderClear(&encoder);
270✔
500
  return tlen;
270✔
501

502
_err:
×
503
  tEncoderClear(&encoder);
×
504
  return code;
×
505
}
506

507
int32_t tDeserializeTsdbRepOpts(void* buf, int32_t bufLen, STsdbRepOpts* pOpts) {
405✔
508
  int32_t  code;
509
  SDecoder decoder = {0};
405✔
510
  int64_t  reserved64 = 0;
405✔
511
  int8_t   msgVer = 0;
405✔
512

513
  tDecoderInit(&decoder, buf, bufLen);
405✔
514

515
  if ((code = tStartDecode(&decoder))) goto _err;
405!
516
  if ((code = tDecodeI8(&decoder, &msgVer))) goto _err;
405!
517
  if (msgVer != TSDB_SNAP_MSG_VER) goto _err;
405!
518
  int16_t format = 0;
405✔
519
  if ((code = tDecodeI16(&decoder, &format))) goto _err;
405!
520
  pOpts->format = format;
405✔
521
  if ((code = tDecodeI64(&decoder, &reserved64))) goto _err;
405!
522

523
  tEndDecode(&decoder);
405✔
524
  tDecoderClear(&decoder);
405✔
525
  return 0;
405✔
526

527
_err:
×
528
  tDecoderClear(&decoder);
×
529
  return code;
×
530
}
531

532
static int32_t tsdbRepOptsEstSize(STsdbRepOpts* pOpts) {
270✔
533
  int32_t dataLen = 0;
270✔
534
  dataLen += sizeof(SSyncTLV);
270✔
535
  dataLen += tTsdbRepOptsDataLenCalc(pOpts);
270✔
536
  return dataLen;
270✔
537
}
538

539
static int32_t tsdbRepOptsSerialize(STsdbRepOpts* pOpts, void* buf, int32_t bufLen) {
270✔
540
  SSyncTLV* pSubHead = buf;
270✔
541
  int32_t   offset = 0;
270✔
542
  int32_t   tlen = 0;
270✔
543
  if ((tlen = tSerializeTsdbRepOpts(pSubHead->val, bufLen, pOpts)) < 0) {
270!
544
    return tlen;
×
545
  }
546
  pSubHead->typ = SNAP_DATA_RAW;
270✔
547
  pSubHead->len = tlen;
270✔
548
  offset += sizeof(*pSubHead) + tlen;
270✔
549
  return offset;
270✔
550
}
551

552
// snap info
553
static int32_t tsdbSnapPrepDealWithSnapInfo(SVnode* pVnode, SSnapshot* pSnap, STsdbRepOpts* pInfo) {
135✔
554
  if (!pSnap->data) {
135!
555
    return 0;
×
556
  }
557
  int32_t code = 0;
135✔
558

559
  SSyncTLV* pHead = (void*)pSnap->data;
135✔
560
  int32_t   offset = 0;
135✔
561

562
  while (offset + sizeof(*pHead) < pHead->len) {
405✔
563
    SSyncTLV* pField = (void*)(pHead->val + offset);
270✔
564
    offset += sizeof(*pField) + pField->len;
270✔
565
    void*   buf = pField->val;
270✔
566
    int32_t bufLen = pField->len;
270✔
567

568
    switch (pField->typ) {
270!
569
      case SNAP_DATA_TSDB:
135✔
570
      case SNAP_DATA_RSMA1:
571
      case SNAP_DATA_RSMA2: {
572
      } break;
135✔
573
      case SNAP_DATA_RAW: {
135✔
574
        code = tDeserializeTsdbRepOpts(buf, bufLen, pInfo);
135✔
575
        if (code < 0) {
135!
576
          tsdbError("vgId:%d, failed to deserialize tsdb rep opts since %s", TD_VID(pVnode), terrstr());
×
577
          return code;
×
578
        }
579
      } break;
135✔
580
      default:
×
581
        code = TSDB_CODE_INVALID_MSG;
×
582
        tsdbError("vgId:%d, unexpected subfield type of snap info. typ:%d", TD_VID(pVnode), pField->typ);
×
583
        return code;
×
584
    }
585
  }
586

587
  return code;
135✔
588
}
589

590
int32_t tsdbSnapPrepDescription(SVnode* pVnode, SSnapshot* pSnap) {
270✔
591
  STsdbPartitionInfo  partitionInfo = {0};
270✔
592
  int                 code = 0;
270✔
593
  STsdbPartitionInfo* pInfo = &partitionInfo;
270✔
594

595
  code = tsdbPartitionInfoInit(pVnode, pInfo);
270✔
596
  if (code) {
270!
597
    goto _out;
×
598
  }
599

600
  // deal with snap info for reply
601
  STsdbRepOpts opts = {.format = TSDB_SNAP_REP_FMT_RAW};
270✔
602
  if (pSnap->type == TDMT_SYNC_PREP_SNAPSHOT_REPLY) {
270✔
603
    STsdbRepOpts leaderOpts = {0};
135✔
604
    if ((code = tsdbSnapPrepDealWithSnapInfo(pVnode, pSnap, &leaderOpts)) < 0) {
135!
605
      tsdbError("vgId:%d, failed to deal with snap info for reply since %s", TD_VID(pVnode), terrstr());
×
606
      goto _out;
×
607
    }
608
    opts.format = TMIN(opts.format, leaderOpts.format);
135✔
609
  }
610

611
  // info data realloc
612
  const int32_t headLen = sizeof(SSyncTLV);
270✔
613
  int32_t       bufLen = headLen;
270✔
614
  bufLen += tsdbPartitionInfoEstSize(pInfo);
270✔
615
  bufLen += tsdbRepOptsEstSize(&opts);
270✔
616
  if ((code = syncSnapInfoDataRealloc(pSnap, bufLen)) != 0) {
270!
617
    tsdbError("vgId:%d, failed to realloc memory for data of snap info. bytes:%d", TD_VID(pVnode), bufLen);
×
618
    goto _out;
×
619
  }
620

621
  // serialization
622
  char*   buf = (void*)pSnap->data;
270✔
623
  int32_t offset = headLen;
270✔
624
  int32_t tlen = 0;
270✔
625

626
  if ((tlen = tsdbPartitionInfoSerialize(pInfo, (uint8_t*)(buf + offset), bufLen - offset)) < 0) {
270!
627
    code = tlen;
×
628
    tsdbError("vgId:%d, failed to serialize tsdb partition info since %s", TD_VID(pVnode), terrstr());
×
629
    goto _out;
×
630
  }
631
  offset += tlen;
270✔
632

633
  if ((tlen = tsdbRepOptsSerialize(&opts, buf + offset, bufLen - offset)) < 0) {
270!
634
    code = tlen;
×
635
    tsdbError("vgId:%d, failed to serialize tsdb rep opts since %s", TD_VID(pVnode), terrstr());
×
636
    goto _out;
×
637
  }
638
  offset += tlen;
270✔
639

640
  // set header of info data
641
  SSyncTLV* pHead = pSnap->data;
270✔
642
  pHead->typ = pSnap->type;
270✔
643
  pHead->len = offset - headLen;
270✔
644

645
  tsdbInfo("vgId:%d, tsdb snap info prepared. type:%s, val length:%d", TD_VID(pVnode), TMSG_INFO(pHead->typ),
270!
646
           pHead->len);
647

648
_out:
×
649
  tsdbPartitionInfoClear(pInfo);
270✔
650
  return code;
270✔
651
}
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