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

taosdata / TDengine / #3817

01 Apr 2025 07:46AM UTC coverage: 34.08% (+0.04%) from 34.045%
#3817

push

travis-ci

happyguoxy
test:alter gcda dir

148556 of 599532 branches covered (24.78%)

Branch coverage included in aggregate %.

222572 of 489464 relevant lines covered (45.47%)

759283.34 hits per line

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

58.02
/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) {
920✔
47
  int32_t code = taosRemoveFile(fname);
920✔
48
  if (code) {
920!
49
    tsdbError("failed to remove file:%s, code:%d, error:%s", fname, code, tstrerror(code));
×
50
  } else {
51
    tsdbInfo("file:%s is removed", fname);
920!
52
  }
53
}
920✔
54

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

167
static int32_t head_to_json(const STFile *file, cJSON *json) { return tfile_to_json(file, json); }
890✔
168
static int32_t data_to_json(const STFile *file, cJSON *json) { return tfile_to_json(file, json); }
891✔
169
static int32_t sma_to_json(const STFile *file, cJSON *json) { return tfile_to_json(file, json); }
891✔
170
static int32_t tomb_to_json(const STFile *file, cJSON *json) { return tfile_to_json(file, json); }
1✔
171
static int32_t stt_to_json(const STFile *file, cJSON *json) {
1,086✔
172
  TAOS_CHECK_RETURN(tfile_to_json(file, json));
1,086!
173

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

179
  return 0;
1,084✔
180
}
181

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

189
  const cJSON *item;
190

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

199
  return 0;
20✔
200
}
201

202
int32_t tsdbTFileToJson(const STFile *file, cJSON *json) {
3,757✔
203
  if (file->type == TSDB_FTYPE_STT) {
3,757✔
204
    return g_tfile_info[file->type].to_json(file, json);
1,086✔
205
  } else {
206
    cJSON *item = cJSON_AddObjectToObject(json, g_tfile_info[file->type].suffix);
2,671✔
207
    if (item == NULL) {
2,674!
208
      return TSDB_CODE_OUT_OF_MEMORY;
×
209
    }
210
    return g_tfile_info[file->type].to_json(file, item);
2,674✔
211
  }
212
}
213

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

217
  if (ftype == TSDB_FTYPE_STT) {
112✔
218
    TAOS_CHECK_RETURN(g_tfile_info[ftype].from_json(json, f));
20!
219
  } else {
220
    const cJSON *item = cJSON_GetObjectItem(json, g_tfile_info[ftype].suffix);
92✔
221
    if (cJSON_IsObject(item)) {
92✔
222
      TAOS_CHECK_RETURN(g_tfile_info[ftype].from_json(item, f));
12!
223
    } else {
224
      return TSDB_CODE_NOT_FOUND;
80✔
225
    }
226
  }
227

228
  return 0;
32✔
229
}
230

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

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

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

251
  if (fobj->ref <= 0 || fobj->state != TSDB_FSTATE_LIVE) {
265!
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;
265✔
258
  (void)taosThreadMutexUnlock(&fobj->mutex);
265✔
259
  tsdbTrace("ref file %s, fobj:%p ref:%d", fobj->fname, fobj, nRef);
265✔
260
  return 0;
265✔
261
}
262

263
int32_t tsdbTFileObjUnref(STFileObj *fobj) {
8,762✔
264
  (void)taosThreadMutexLock(&fobj->mutex);
8,762✔
265
  int32_t nRef = --fobj->ref;
8,764✔
266
  (void)taosThreadMutexUnlock(&fobj->mutex);
8,764✔
267

268
  if (nRef < 0) {
8,762!
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);
8,762✔
274
  if (nRef == 0) {
8,762✔
275
    if (fobj->state == TSDB_FSTATE_DEAD) {
8,497!
276
      tsdbRemoveFile(fobj->fname);
×
277
    }
278
    taosMemoryFree(fobj);
8,497!
279
  }
280

281
  return 0;
8,763✔
282
}
283

284
static void tsdbTFileObjRemoveLC(STFileObj *fobj, bool remove_all) {
917✔
285
  if (fobj->f->type != TSDB_FTYPE_DATA || fobj->f->lcn < 1) {
917!
286
    tsdbRemoveFile(fobj->fname);
917✔
287
    return;
917✔
288
  }
289
#ifdef USE_S3
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
#endif
334
}
335

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

354
int32_t tsdbTFileObjRemoveUpdateLC(STFileObj *fobj) {
×
355
  (void)taosThreadMutexLock(&fobj->mutex);
×
356

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

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

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

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

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

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

434
bool tsdbIsSameTFile(const STFile *f1, const STFile *f2) {
2,924✔
435
  if (f1->type != f2->type) return false;
2,924!
436
  if (f1->did.level != f2->did.level) return false;
2,924!
437
  if (f1->did.id != f2->did.id) return false;
2,924!
438
  if (f1->fid != f2->fid) return false;
2,924!
439
  if (f1->cid != f2->cid) return false;
2,924✔
440
  if (f1->lcn != f2->lcn) return false;
2,634!
441
  return true;
2,634✔
442
}
443

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

450
int32_t tsdbTFileObjCmpr(const STFileObj **fobj1, const STFileObj **fobj2) {
947✔
451
  if (fobj1[0]->f->cid < fobj2[0]->f->cid) {
947✔
452
    return -1;
3✔
453
  } else if (fobj1[0]->f->cid > fobj2[0]->f->cid) {
944✔
454
    return 1;
317✔
455
  } else {
456
    return 0;
627✔
457
  }
458
}
459

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