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

taosdata / TDengine / #3545

02 Dec 2024 06:22AM UTC coverage: 60.839% (-0.04%) from 60.88%
#3545

push

travis-ci

web-flow
Merge pull request #28961 from taosdata/fix/refactor-vnode-management-open-vnode

fix/refactor-vnode-management-open-vnode

120592 of 253473 branches covered (47.58%)

Branch coverage included in aggregate %.

102 of 145 new or added lines in 3 files covered. (70.34%)

477 existing lines in 108 files now uncovered.

201840 of 276506 relevant lines covered (73.0%)

19392204.25 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

167
static int32_t head_to_json(const STFile *file, cJSON *json) { return tfile_to_json(file, json); }
14,950✔
168
static int32_t data_to_json(const STFile *file, cJSON *json) { return tfile_to_json(file, json); }
14,950✔
169
static int32_t sma_to_json(const STFile *file, cJSON *json) { return tfile_to_json(file, json); }
14,950✔
170
static int32_t tomb_to_json(const STFile *file, cJSON *json) { return tfile_to_json(file, json); }
10,146✔
171
static int32_t stt_to_json(const STFile *file, cJSON *json) {
193,399✔
172
  TAOS_CHECK_RETURN(tfile_to_json(file, json));
193,399!
173

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

179
  return 0;
193,430✔
180
}
181

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

189
  const cJSON *item;
190

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

199
  return 0;
2,132✔
200
}
201

202
int32_t tsdbTFileToJson(const STFile *file, cJSON *json) {
248,395✔
203
  if (file->type == TSDB_FTYPE_STT) {
248,395✔
204
    return g_tfile_info[file->type].to_json(file, json);
193,399✔
205
  } else {
206
    cJSON *item = cJSON_AddObjectToObject(json, g_tfile_info[file->type].suffix);
54,996✔
207
    if (item == NULL) {
54,996!
208
      return TSDB_CODE_OUT_OF_MEMORY;
×
209
    }
210
    return g_tfile_info[file->type].to_json(file, item);
54,996✔
211
  }
212
}
213

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

217
  if (ftype == TSDB_FTYPE_STT) {
10,320✔
218
    TAOS_CHECK_RETURN(g_tfile_info[ftype].from_json(json, f));
2,134!
219
  } else {
220
    const cJSON *item = cJSON_GetObjectItem(json, g_tfile_info[ftype].suffix);
8,186✔
221
    if (cJSON_IsObject(item)) {
8,232✔
222
      TAOS_CHECK_RETURN(g_tfile_info[ftype].from_json(item, f));
1,263!
223
    } else {
224
      return TSDB_CODE_NOT_FOUND;
6,971✔
225
    }
226
  }
227

228
  return 0;
3,395✔
229
}
230

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

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

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

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

257
  nRef = ++fobj->ref;
10,182,269✔
258
  (void)taosThreadMutexUnlock(&fobj->mutex);
10,182,269✔
259
  tsdbTrace("ref file %s, fobj:%p ref %d", fobj->fname, fobj, nRef);
10,182,886✔
260
  return 0;
10,182,706✔
261
}
262

263
int32_t tsdbTFileObjUnref(STFileObj *fobj) {
10,651,500✔
264
  (void)taosThreadMutexLock(&fobj->mutex);
10,651,500✔
265
  int32_t nRef = --fobj->ref;
10,658,033✔
266
  (void)taosThreadMutexUnlock(&fobj->mutex);
10,658,033✔
267

268
  if (nRef < 0) {
10,659,332!
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);
10,659,332✔
274
  if (nRef == 0) {
10,659,332✔
275
    if (fobj->state == TSDB_FSTATE_DEAD) {
480,537✔
276
      tsdbRemoveFile(fobj->fname);
3,982✔
277
    }
278
    taosMemoryFree(fobj);
480,537✔
279
  }
280

281
  return 0;
10,658,945✔
282
}
283

284
static void tsdbTFileObjRemoveLC(STFileObj *fobj, bool remove_all) {
11,915✔
285
  if (fobj->f->type != TSDB_FTYPE_DATA || fobj->f->lcn < 1) {
11,915!
286
    tsdbRemoveFile(fobj->fname);
11,915✔
287
    return;
11,915✔
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) {
15,897✔
336
  (void)taosThreadMutexLock(&fobj->mutex);
15,897✔
337
  if (fobj->state != TSDB_FSTATE_LIVE || fobj->ref <= 0) {
15,897!
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;
15,897✔
343
  int32_t nRef = --fobj->ref;
15,897✔
344
  (void)taosThreadMutexUnlock(&fobj->mutex);
15,897✔
345
  tsdbTrace("remove unref file %s, fobj:%p ref %d", fobj->fname, fobj, nRef);
15,897✔
346
  if (nRef == 0) {
15,897✔
347
    tsdbTFileObjRemoveLC(fobj, true);
11,915✔
348
    taosMemoryFree(fobj);
11,915✔
349
  }
350
  return 0;
15,897✔
351
}
352

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

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

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

377
  if (pTfs) {
664,673!
378
    snprintf(fname,                              //
664,749✔
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,                             //
664,753✔
387
             f->cid,                             //
664,753✔
388
             g_tfile_info[f->type].suffix);
664,753✔
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
}
664,669✔
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) {
86,881✔
434
  if (f1->type != f2->type) return false;
86,881!
435
  if (f1->did.level != f2->did.level) return false;
86,881✔
436
  if (f1->did.id != f2->did.id) return false;
86,862✔
437
  if (f1->fid != f2->fid) return false;
86,838!
438
  if (f1->cid != f2->cid) return false;
86,838✔
439
  if (f1->lcn != f2->lcn) return false;
84,566!
440
  return true;
84,566✔
441
}
442

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

449
int32_t tsdbTFileObjCmpr(const STFileObj **fobj1, const STFileObj **fobj2) {
20,585✔
450
  if (fobj1[0]->f->cid < fobj2[0]->f->cid) {
20,585✔
451
    return -1;
568✔
452
  } else if (fobj1[0]->f->cid > fobj2[0]->f->cid) {
20,017✔
453
    return 1;
6,480✔
454
  } else {
455
    return 0;
13,537✔
456
  }
457
}
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

© 2025 Coveralls, Inc