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

taosdata / TDengine / #3798

31 Mar 2025 10:39AM UTC coverage: 9.424% (-20.9%) from 30.372%
#3798

push

travis-ci

happyguoxy
test:add test cases

21549 of 307601 branches covered (7.01%)

Branch coverage included in aggregate %.

36084 of 303967 relevant lines covered (11.87%)

58620.7 hits per line

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

0.0
/source/dnode/vnode/src/tsdb/tsdbOpen.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 "meta.h"
17
#include "tsdb.h"
18
#include "tsdbFS2.h"
19

20
extern int32_t tsdbOpenCompMonitor(STsdb *tsdb);
21
extern void    tsdbCloseCompMonitor(STsdb *tsdb);
22

23
void tsdbSetKeepCfg(STsdb *pTsdb, STsdbCfg *pCfg) {
×
24
  STsdbKeepCfg *pKeepCfg = &pTsdb->keepCfg;
×
25
  pKeepCfg->precision = pCfg->precision;
×
26
  pKeepCfg->days = pCfg->days;
×
27
  pKeepCfg->keep0 = pCfg->keep0;
×
28
  pKeepCfg->keep1 = pCfg->keep1;
×
29
  pKeepCfg->keep2 = pCfg->keep2;
×
30
  pKeepCfg->keepTimeOffset = pCfg->keepTimeOffset;
×
31
}
×
32

33
int64_t tsdbGetEarliestTs(STsdb *pTsdb) {
×
34
  STsdbKeepCfg *pCfg = &pTsdb->keepCfg;
×
35

36
  int64_t now = taosGetTimestamp(pCfg->precision);
×
37
  int64_t ts = now - (tsTickPerMin[pCfg->precision] * pCfg->keep2) + 1;  // needs to add one tick
×
38
  return ts;
×
39
}
40

41
int32_t tsdbOpen(SVnode *pVnode, STsdb **ppTsdb, const char *dir, STsdbKeepCfg *pKeepCfg, int8_t rollback, bool force) {
×
42
  STsdb  *pTsdb = NULL;
×
43
  int     slen = 0;
×
44
  int32_t code;
45
  int32_t lino;
46

47
  *ppTsdb = NULL;
×
48
  slen = strlen(pVnode->path) + strlen(dir) + 2;
×
49

50
  // create handle
51
  pTsdb = (STsdb *)taosMemoryCalloc(1, sizeof(*pTsdb) + slen);
×
52
  if (pTsdb == NULL) {
×
53
    TAOS_CHECK_RETURN(terrno);
×
54
  }
55

56
  pTsdb->path = (char *)&pTsdb[1];
×
57
  snprintf(pTsdb->path, TD_PATH_MAX, "%s%s%s", pVnode->path, TD_DIRSEP, dir);
×
58
  // taosRealPath(pTsdb->path, NULL, slen);
59
  pTsdb->pVnode = pVnode;
×
60
  (void)taosThreadMutexInit(&pTsdb->mutex, NULL);
×
61
  if (!pKeepCfg) {
×
62
    tsdbSetKeepCfg(pTsdb, &pVnode->config.tsdbCfg);
×
63
  } else {
64
    memcpy(&pTsdb->keepCfg, pKeepCfg, sizeof(STsdbKeepCfg));
×
65
  }
66

67
  // create dir
68
  if (pVnode->pTfs) {
×
69
    code = tfsMkdir(pVnode->pTfs, pTsdb->path);
×
70
    TSDB_CHECK_CODE(code, lino, _exit);
×
71
  } else {
72
    code = taosMkDir(pTsdb->path);
×
73
    TSDB_CHECK_CODE(code, lino, _exit);
×
74
  }
75

76
  // open tsdb
77
  code = tsdbOpenFS(pTsdb, &pTsdb->pFS, rollback);
×
78
  TSDB_CHECK_CODE(code, lino, _exit);
×
79

80
  if (pTsdb->pFS->fsstate == TSDB_FS_STATE_INCOMPLETE && force == false) {
×
81
    TAOS_CHECK_GOTO(TSDB_CODE_NEED_RETRY, &lino, _exit);
×
82
  }
83

84
  code = tsdbOpenCache(pTsdb);
×
85
  TSDB_CHECK_CODE(code, lino, _exit);
×
86

87
#ifdef TD_ENTERPRISE
88
  TAOS_CHECK_GOTO(tsdbOpenCompMonitor(pTsdb), &lino, _exit);
×
89
#endif
90

91
_exit:
×
92
  if (code) {
×
93
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, lino, tstrerror(code));
×
94
    tsdbCloseFS(&pTsdb->pFS);
×
95
    (void)taosThreadMutexDestroy(&pTsdb->mutex);
×
96
    taosMemoryFree(pTsdb);
×
97
  } else {
98
    tsdbDebug("vgId:%d, tsdb is opened at %s, days:%d, keep:%d,%d,%d, keepTimeoffset:%d", TD_VID(pVnode), pTsdb->path,
×
99
              pTsdb->keepCfg.days, pTsdb->keepCfg.keep0, pTsdb->keepCfg.keep1, pTsdb->keepCfg.keep2,
100
              pTsdb->keepCfg.keepTimeOffset);
101
    *ppTsdb = pTsdb;
×
102
  }
103
  return code;
×
104
}
105

106
void tsdbClose(STsdb **pTsdb) {
×
107
  if (*pTsdb) {
×
108
    STsdb *pdb = *pTsdb;
×
109
    tsdbDebug("vgId:%d, tsdb is close at %s, days:%d, keep:%d,%d,%d, keepTimeOffset:%d", TD_VID(pdb->pVnode), pdb->path,
×
110
              pdb->keepCfg.days, pdb->keepCfg.keep0, pdb->keepCfg.keep1, pdb->keepCfg.keep2,
111
              pdb->keepCfg.keepTimeOffset);
112
    (void)taosThreadMutexLock(&(*pTsdb)->mutex);
×
113
    tsdbMemTableDestroy((*pTsdb)->mem, true);
×
114
    (*pTsdb)->mem = NULL;
×
115
    (void)taosThreadMutexUnlock(&(*pTsdb)->mutex);
×
116

117
    tsdbCloseFS(&(*pTsdb)->pFS);
×
118
    tsdbCloseCache(*pTsdb);
×
119
#ifdef TD_ENTERPRISE
120
    tsdbCloseCompMonitor(*pTsdb);
×
121
#endif
122
    (void)taosThreadMutexDestroy(&(*pTsdb)->mutex);
×
123
    taosMemoryFreeClear(*pTsdb);
×
124
  }
125
  return;
×
126
}
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