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

taosdata / TDengine / #3610

12 Feb 2025 09:54AM UTC coverage: 54.713% (-8.4%) from 63.066%
#3610

push

travis-ci

web-flow
Merge pull request #29745 from taosdata/fix/TD33664-3.0

fix: --version show information check for 3.0

120957 of 286549 branches covered (42.21%)

Branch coverage included in aggregate %.

190849 of 283342 relevant lines covered (67.36%)

4969786.97 hits per line

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

62.44
/source/dnode/vnode/src/tsdb/tsdbFile2.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 "tsdbFile2.h"
17
#include "tcs.h"
18
#include "vnd.h"
19

20
// to_json
21
static int32_t head_to_json(const STFile *file, cJSON *json);
22
static int32_t data_to_json(const STFile *file, cJSON *json);
23
static int32_t sma_to_json(const STFile *file, cJSON *json);
24
static int32_t tomb_to_json(const STFile *file, cJSON *json);
25
static int32_t stt_to_json(const STFile *file, cJSON *json);
26

27
// from_json
28
static int32_t head_from_json(const cJSON *json, STFile *file);
29
static int32_t data_from_json(const cJSON *json, STFile *file);
30
static int32_t sma_from_json(const cJSON *json, STFile *file);
31
static int32_t tomb_from_json(const cJSON *json, STFile *file);
32
static int32_t stt_from_json(const cJSON *json, STFile *file);
33

34
static const struct {
35
  const char *suffix;
36
  int32_t (*to_json)(const STFile *file, cJSON *json);
37
  int32_t (*from_json)(const cJSON *json, STFile *file);
38
} g_tfile_info[] = {
39
    [TSDB_FTYPE_HEAD] = {"head", head_to_json, head_from_json},
40
    [TSDB_FTYPE_DATA] = {"data", data_to_json, data_from_json},
41
    [TSDB_FTYPE_SMA] = {"sma", sma_to_json, sma_from_json},
42
    [TSDB_FTYPE_TOMB] = {"tomb", tomb_to_json, tomb_from_json},
43
    [TSDB_FTYPE_STT] = {"stt", stt_to_json, stt_from_json},
44
};
45

46
void tsdbRemoveFile(const char *fname) {
5,065✔
47
  int32_t code = taosRemoveFile(fname);
5,065✔
48
  if (code) {
5,066!
49
    tsdbError("failed to remove file:%s, code:%d, error:%s", fname, code, tstrerror(code));
×
50
  } else {
51
    tsdbInfo("file:%s is removed", fname);
5,066!
52
  }
53
}
5,066✔
54

55
static int32_t tfile_to_json(const STFile *file, cJSON *json) {
150,222✔
56
  /* did.level */
57
  if (cJSON_AddNumberToObject(json, "did.level", file->did.level) == NULL) {
150,222!
58
    return TSDB_CODE_OUT_OF_MEMORY;
×
59
  }
60

61
  /* did.id */
62
  if (cJSON_AddNumberToObject(json, "did.id", file->did.id) == NULL) {
150,234!
63
    return TSDB_CODE_OUT_OF_MEMORY;
×
64
  }
65

66
  /* lcn - last chunk number */
67
  if (cJSON_AddNumberToObject(json, "lcn", file->lcn) == NULL) {
150,240!
68
    return TSDB_CODE_OUT_OF_MEMORY;
×
69
  }
70

71
  /* fid */
72
  if (cJSON_AddNumberToObject(json, "fid", file->fid) == NULL) {
150,226!
73
    return TSDB_CODE_OUT_OF_MEMORY;
×
74
  }
75

76
  /* cid */
77
  if (cJSON_AddNumberToObject(json, "cid", file->cid) == NULL) {
150,231!
78
    return TSDB_CODE_OUT_OF_MEMORY;
×
79
  }
80

81
  /* size */
82
  if (cJSON_AddNumberToObject(json, "size", file->size) == NULL) {
150,234!
83
    return TSDB_CODE_OUT_OF_MEMORY;
×
84
  }
85

86
  if (file->minVer <= file->maxVer) {
150,229!
87
    /* minVer */
88
    if (cJSON_AddNumberToObject(json, "minVer", file->minVer) == NULL) {
150,229!
89
      return TSDB_CODE_OUT_OF_MEMORY;
×
90
    }
91

92
    /* maxVer */
93
    if (cJSON_AddNumberToObject(json, "maxVer", file->maxVer) == NULL) {
150,238!
94
      return TSDB_CODE_OUT_OF_MEMORY;
×
95
    }
96
  }
97
  return 0;
150,235✔
98
}
99

100
static int32_t tfile_from_json(const cJSON *json, STFile *file) {
1,532✔
101
  const cJSON *item;
102

103
  /* did.level */
104
  item = cJSON_GetObjectItem(json, "did.level");
1,532✔
105
  if (cJSON_IsNumber(item)) {
1,534!
106
    file->did.level = item->valuedouble;
1,534✔
107
  } else {
108
    return TSDB_CODE_FILE_CORRUPTED;
×
109
  }
110

111
  /* did.id */
112
  item = cJSON_GetObjectItem(json, "did.id");
1,534✔
113
  if (cJSON_IsNumber(item)) {
1,531!
114
    file->did.id = item->valuedouble;
1,531✔
115
  } else {
116
    return TSDB_CODE_FILE_CORRUPTED;
×
117
  }
118

119
  /* lcn */
120
  item = cJSON_GetObjectItem(json, "lcn");
1,531✔
121
  if (cJSON_IsNumber(item)) {
1,531!
122
    file->lcn = item->valuedouble;
1,531✔
123
  } else {
124
    // return TSDB_CODE_FILE_CORRUPTED;
125
  }
126

127
  /* fid */
128
  item = cJSON_GetObjectItem(json, "fid");
1,531✔
129
  if (cJSON_IsNumber(item)) {
1,531!
130
    file->fid = item->valuedouble;
1,531✔
131
  } else {
132
    return TSDB_CODE_FILE_CORRUPTED;
×
133
  }
134

135
  /* cid */
136
  item = cJSON_GetObjectItem(json, "cid");
1,531✔
137
  if (cJSON_IsNumber(item)) {
1,533!
138
    file->cid = item->valuedouble;
1,534✔
139
  } else {
140
    return TSDB_CODE_FILE_CORRUPTED;
×
141
  }
142

143
  /* size */
144
  item = cJSON_GetObjectItem(json, "size");
1,534✔
145
  if (cJSON_IsNumber(item)) {
1,532!
146
    file->size = item->valuedouble;
1,532✔
147
  } else {
148
    return TSDB_CODE_FILE_CORRUPTED;
×
149
  }
150

151
  /* minVer */
152
  file->minVer = VERSION_MAX;
1,532✔
153
  item = cJSON_GetObjectItem(json, "minVer");
1,532✔
154
  if (cJSON_IsNumber(item)) {
1,532!
155
    file->minVer = item->valuedouble;
1,532✔
156
  }
157

158
  /* maxVer */
159
  file->maxVer = VERSION_MIN;
1,532✔
160
  item = cJSON_GetObjectItem(json, "maxVer");
1,532✔
161
  if (cJSON_IsNumber(item)) {
1,533!
162
    file->maxVer = item->valuedouble;
1,533✔
163
  }
164
  return 0;
1,533✔
165
}
166

167
static int32_t head_to_json(const STFile *file, cJSON *json) { return tfile_to_json(file, json); }
3,049✔
168
static int32_t data_to_json(const STFile *file, cJSON *json) { return tfile_to_json(file, json); }
3,050✔
169
static int32_t sma_to_json(const STFile *file, cJSON *json) { return tfile_to_json(file, json); }
3,050✔
170
static int32_t tomb_to_json(const STFile *file, cJSON *json) { return tfile_to_json(file, json); }
2,952✔
171
static int32_t stt_to_json(const STFile *file, cJSON *json) {
138,126✔
172
  TAOS_CHECK_RETURN(tfile_to_json(file, json));
138,126!
173

174
  /* lvl */
175
  if (cJSON_AddNumberToObject(json, "level", file->stt->level) == NULL) {
138,136!
176
    return TSDB_CODE_OUT_OF_MEMORY;
×
177
  }
178

179
  return 0;
138,138✔
180
}
181

182
static int32_t head_from_json(const cJSON *json, STFile *file) { return tfile_from_json(json, file); }
247✔
183
static int32_t data_from_json(const cJSON *json, STFile *file) { return tfile_from_json(json, file); }
247✔
184
static int32_t sma_from_json(const cJSON *json, STFile *file) { return tfile_from_json(json, file); }
247✔
185
static int32_t tomb_from_json(const cJSON *json, STFile *file) { return tfile_from_json(json, file); }
×
186
static int32_t stt_from_json(const cJSON *json, STFile *file) {
791✔
187
  TAOS_CHECK_RETURN(tfile_from_json(json, file));
791!
188

189
  const cJSON *item;
190

191
  /* lvl */
192
  item = cJSON_GetObjectItem(json, "level");
792✔
193
  if (cJSON_IsNumber(item)) {
792!
194
    file->stt->level = item->valuedouble;
792✔
195
  } else {
196
    return TSDB_CODE_FILE_CORRUPTED;
×
197
  }
198

199
  return 0;
792✔
200
}
201

202
int32_t tsdbTFileToJson(const STFile *file, cJSON *json) {
150,226✔
203
  if (file->type == TSDB_FTYPE_STT) {
150,226✔
204
    return g_tfile_info[file->type].to_json(file, json);
138,128✔
205
  } else {
206
    cJSON *item = cJSON_AddObjectToObject(json, g_tfile_info[file->type].suffix);
12,098✔
207
    if (item == NULL) {
12,100!
208
      return TSDB_CODE_OUT_OF_MEMORY;
×
209
    }
210
    return g_tfile_info[file->type].to_json(file, item);
12,100✔
211
  }
212
}
213

214
int32_t tsdbJsonToTFile(const cJSON *json, tsdb_ftype_t ftype, STFile *f) {
4,770✔
215
  f[0] = (STFile){.type = ftype};
4,770✔
216

217
  if (ftype == TSDB_FTYPE_STT) {
4,770✔
218
    TAOS_CHECK_RETURN(g_tfile_info[ftype].from_json(json, f));
791!
219
  } else {
220
    const cJSON *item = cJSON_GetObjectItem(json, g_tfile_info[ftype].suffix);
3,979✔
221
    if (cJSON_IsObject(item)) {
3,993✔
222
      TAOS_CHECK_RETURN(g_tfile_info[ftype].from_json(item, f));
741✔
223
    } else {
224
      return TSDB_CODE_NOT_FOUND;
3,238✔
225
    }
226
  }
227

228
  return 0;
1,532✔
229
}
230

231
int32_t tsdbTFileObjInit(STsdb *pTsdb, const STFile *f, STFileObj **fobj) {
308,603✔
232
  fobj[0] = taosMemoryMalloc(sizeof(*fobj[0]));
308,603!
233
  if (!fobj[0]) {
308,740!
234
    return terrno;
×
235
  }
236

237
  (void)taosThreadMutexInit(&fobj[0]->mutex, NULL);
308,740✔
238
  fobj[0]->f[0] = f[0];
308,684✔
239
  fobj[0]->state = TSDB_FSTATE_LIVE;
308,684✔
240
  fobj[0]->ref = 1;
308,684✔
241
  tsdbTFileName(pTsdb, f, fobj[0]->fname);
308,684✔
242
  // fobj[0]->nlevel = tfsGetLevel(pTsdb->pVnode->pTfs);
243
  fobj[0]->nlevel = vnodeNodeId(pTsdb->pVnode);
308,585✔
244
  return 0;
308,556✔
245
}
246

247
int32_t tsdbTFileObjRef(STFileObj *fobj) {
1,487,516✔
248
  int32_t nRef;
249
  (void)taosThreadMutexLock(&fobj->mutex);
1,487,516✔
250

251
  if (fobj->ref <= 0 || fobj->state != TSDB_FSTATE_LIVE) {
1,490,558!
252
    tsdbError("file %s, fobj:%p ref %d", fobj->fname, fobj, fobj->ref);
×
253
    (void)taosThreadMutexUnlock(&fobj->mutex);
×
254
    return TSDB_CODE_FAILED;
×
255
  }
256

257
  nRef = ++fobj->ref;
1,490,829✔
258
  (void)taosThreadMutexUnlock(&fobj->mutex);
1,490,829✔
259
  tsdbTrace("ref file %s, fobj:%p ref %d", fobj->fname, fobj, nRef);
1,490,760✔
260
  return 0;
1,490,782✔
261
}
262

263
int32_t tsdbTFileObjUnref(STFileObj *fobj) {
1,794,907✔
264
  (void)taosThreadMutexLock(&fobj->mutex);
1,794,907✔
265
  int32_t nRef = --fobj->ref;
1,795,998✔
266
  (void)taosThreadMutexUnlock(&fobj->mutex);
1,795,998✔
267

268
  if (nRef < 0) {
1,795,982!
269
    tsdbError("file %s, fobj:%p ref %d", fobj->fname, fobj, nRef);
×
270
    return TSDB_CODE_FAILED;
×
271
  }
272

273
  tsdbTrace("unref file %s, fobj:%p ref %d", fobj->fname, fobj, nRef);
1,795,982✔
274
  if (nRef == 0) {
1,795,981✔
275
    if (fobj->state == TSDB_FSTATE_DEAD) {
303,693✔
276
      tsdbRemoveFile(fobj->fname);
50✔
277
    }
278
    taosMemoryFree(fobj);
303,693!
279
  }
280

281
  return 0;
1,795,856✔
282
}
283

284
static void tsdbTFileObjRemoveLC(STFileObj *fobj, bool remove_all) {
5,015✔
285
  if (fobj->f->type != TSDB_FTYPE_DATA || fobj->f->lcn < 1) {
5,015!
286
    tsdbRemoveFile(fobj->fname);
5,015✔
287
    return;
5,016✔
288
  }
289

290
  if (!remove_all) {
×
291
    // remove local last chunk file
292
    char lc_path[TSDB_FILENAME_LEN];
293
    tstrncpy(lc_path, fobj->fname, TSDB_FQDN_LEN);
×
294

295
    char *dot = strrchr(lc_path, '.');
×
296
    if (!dot) {
×
297
      tsdbError("unexpected path: %s", lc_path);
×
298
      return;
×
299
    }
300
    snprintf(dot + 1, TSDB_FQDN_LEN - (dot + 1 - lc_path), "%d.data", fobj->f->lcn);
×
301

302
    tsdbRemoveFile(lc_path);
×
303

304
  } else {
305
    // delete by data file prefix
306
    char lc_path[TSDB_FILENAME_LEN];
307
    tstrncpy(lc_path, fobj->fname, TSDB_FQDN_LEN);
×
308

309
    char   *object_name = taosDirEntryBaseName(lc_path);
×
310
    int32_t node_id = fobj->nlevel;
×
311
    char    object_name_prefix[TSDB_FILENAME_LEN];
312
    snprintf(object_name_prefix, TSDB_FQDN_LEN, "%d/%s", node_id, object_name);
×
313

314
    char *dot = strrchr(object_name_prefix, '.');
×
315
    if (!dot) {
×
316
      tsdbError("unexpected path: %s", object_name_prefix);
×
317
      return;
×
318
    }
319
    *(dot + 1) = 0;
×
320

321
    tcsDeleteObjectsByPrefix(object_name_prefix);
×
322

323
    // remove local last chunk file
324
    dot = strrchr(lc_path, '.');
×
325
    if (!dot) {
×
326
      tsdbError("unexpected path: %s", lc_path);
×
327
      return;
×
328
    }
329
    snprintf(dot + 1, TSDB_FQDN_LEN - (dot + 1 - lc_path), "%d.data", fobj->f->lcn);
×
330

331
    tsdbRemoveFile(lc_path);
×
332
  }
333
}
334

335
int32_t tsdbTFileObjRemove(STFileObj *fobj) {
5,020✔
336
  (void)taosThreadMutexLock(&fobj->mutex);
5,020✔
337
  if (fobj->state != TSDB_FSTATE_LIVE || fobj->ref <= 0) {
5,021!
338
    tsdbError("file %s, fobj:%p ref %d", fobj->fname, fobj, fobj->ref);
×
339
    (void)taosThreadMutexUnlock(&fobj->mutex);
×
340
    return TSDB_CODE_FAILED;
×
341
  }
342
  fobj->state = TSDB_FSTATE_DEAD;
5,021✔
343
  int32_t nRef = --fobj->ref;
5,021✔
344
  (void)taosThreadMutexUnlock(&fobj->mutex);
5,021✔
345
  tsdbTrace("remove unref file %s, fobj:%p ref %d", fobj->fname, fobj, nRef);
5,021✔
346
  if (nRef == 0) {
5,021✔
347
    tsdbTFileObjRemoveLC(fobj, true);
4,974✔
348
    taosMemoryFree(fobj);
4,974!
349
  }
350
  return 0;
5,021✔
351
}
352

353
int32_t tsdbTFileObjRemoveUpdateLC(STFileObj *fobj) {
45✔
354
  (void)taosThreadMutexLock(&fobj->mutex);
45✔
355

356
  if (fobj->state != TSDB_FSTATE_LIVE || fobj->ref <= 0) {
45!
357
    (void)taosThreadMutexUnlock(&fobj->mutex);
×
358
    tsdbError("file %s, fobj:%p ref %d", fobj->fname, fobj, fobj->ref);
×
359
    return TSDB_CODE_FAILED;
×
360
  }
361

362
  fobj->state = TSDB_FSTATE_DEAD;
45✔
363
  int32_t nRef = --fobj->ref;
45✔
364
  (void)taosThreadMutexUnlock(&fobj->mutex);
45✔
365
  tsdbTrace("remove unref file %s, fobj:%p ref %d", fobj->fname, fobj, nRef);
45!
366
  if (nRef == 0) {
45✔
367
    tsdbTFileObjRemoveLC(fobj, false);
42✔
368
    taosMemoryFree(fobj);
42!
369
  }
370
  return 0;
45✔
371
}
372

373
void tsdbTFileName(STsdb *pTsdb, const STFile *f, char fname[]) {
442,010✔
374
  SVnode *pVnode = pTsdb->pVnode;
442,010✔
375
  STfs   *pTfs = pVnode->pTfs;
442,010✔
376

377
  if (pTfs) {
442,010!
378
    snprintf(fname,                              //
443,511✔
379
             TSDB_FILENAME_LEN,                  //
380
             "%s%s%s%sv%df%dver%" PRId64 ".%s",  //
381
             tfsGetDiskPath(pTfs, f->did),       //
382
             TD_DIRSEP,                          //
383
             pTsdb->path,                        //
384
             TD_DIRSEP,                          //
385
             TD_VID(pVnode),                     //
386
             f->fid,                             //
443,157✔
387
             f->cid,                             //
443,157✔
388
             g_tfile_info[f->type].suffix);
443,157✔
389
  } else {
390
    snprintf(fname,                          //
×
391
             TSDB_FILENAME_LEN,              //
392
             "%s%sv%df%dver%" PRId64 ".%s",  //
393
             pTsdb->path,                    //
394
             TD_DIRSEP,                      //
395
             TD_VID(pVnode),                 //
396
             f->fid,                         //
×
397
             f->cid,                         //
×
398
             g_tfile_info[f->type].suffix);
×
399
  }
400
}
442,364✔
401

402
void tsdbTFileLastChunkName(STsdb *pTsdb, const STFile *f, char fname[]) {
×
403
  SVnode *pVnode = pTsdb->pVnode;
×
404
  STfs   *pTfs = pVnode->pTfs;
×
405

406
  if (pTfs) {
×
407
    snprintf(fname,                                 //
×
408
             TSDB_FILENAME_LEN,                     //
409
             "%s%s%s%sv%df%dver%" PRId64 ".%d.%s",  //
410
             tfsGetDiskPath(pTfs, f->did),          //
411
             TD_DIRSEP,                             //
412
             pTsdb->path,                           //
413
             TD_DIRSEP,                             //
414
             TD_VID(pVnode),                        //
415
             f->fid,                                //
×
416
             f->cid,                                //
×
417
             f->lcn,                                //
×
418
             g_tfile_info[f->type].suffix);
×
419
  } else {
420
    snprintf(fname,                             //
×
421
             TSDB_FILENAME_LEN,                 //
422
             "%s%sv%df%dver%" PRId64 ".%d.%s",  //
423
             pTsdb->path,                       //
424
             TD_DIRSEP,                         //
425
             TD_VID(pVnode),                    //
426
             f->fid,                            //
×
427
             f->cid,                            //
×
428
             f->lcn,                            //
×
429
             g_tfile_info[f->type].suffix);
×
430
  }
431
}
×
432

433
bool tsdbIsSameTFile(const STFile *f1, const STFile *f2) {
15,217✔
434
  if (f1->type != f2->type) return false;
15,217!
435
  if (f1->did.level != f2->did.level) return false;
15,217✔
436
  if (f1->did.id != f2->did.id) return false;
15,170✔
437
  if (f1->fid != f2->fid) return false;
15,155!
438
  if (f1->cid != f2->cid) return false;
15,155✔
439
  if (f1->lcn != f2->lcn) return false;
14,846!
440
  return true;
14,846✔
441
}
442

443
bool tsdbIsTFileChanged(const STFile *f1, const STFile *f2) {
14,846✔
444
  if (f1->size != f2->size) return true;
14,846✔
445
  // if (f1->type == TSDB_FTYPE_STT && f1->stt->nseg != f2->stt->nseg) return true;
446
  return false;
14,790✔
447
}
448

449
int32_t tsdbTFileObjCmpr(const STFileObj **fobj1, const STFileObj **fobj2) {
6,404✔
450
  if (fobj1[0]->f->cid < fobj2[0]->f->cid) {
6,404✔
451
    return -1;
34✔
452
  } else if (fobj1[0]->f->cid > fobj2[0]->f->cid) {
6,370✔
453
    return 1;
1,678✔
454
  } else {
455
    return 0;
4,692✔
456
  }
457
}
458

459
const char *tsdbFTypeLabel(tsdb_ftype_t ftype) { return g_tfile_info[ftype].suffix; }
135,287✔
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