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

taosdata / TDengine / #4143

24 May 2025 03:30AM UTC coverage: 32.868% (-29.4%) from 62.238%
#4143

push

travis-ci

web-flow
test: migrate stream cases (#31164)

76401 of 312956 branches covered (24.41%)

Branch coverage included in aggregate %.

128686 of 311012 relevant lines covered (41.38%)

579734.08 hits per line

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

66.67
/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) {
53✔
24
  STsdbKeepCfg *pKeepCfg = &pTsdb->keepCfg;
53✔
25
  pKeepCfg->precision = pCfg->precision;
53✔
26
  pKeepCfg->days = pCfg->days;
53✔
27
  pKeepCfg->keep0 = pCfg->keep0;
53✔
28
  pKeepCfg->keep1 = pCfg->keep1;
53✔
29
  pKeepCfg->keep2 = pCfg->keep2;
53✔
30
  pKeepCfg->keepTimeOffset = pCfg->keepTimeOffset;
53✔
31
}
53✔
32

33
int64_t tsdbGetEarliestTs(STsdb *pTsdb) {
9,940✔
34
  STsdbKeepCfg *pCfg = &pTsdb->keepCfg;
9,940✔
35

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

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

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

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

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

67
  // create dir
68
  if (pVnode->pTfs) {
53!
69
    code = tfsMkdir(pVnode->pTfs, pTsdb->path);
53✔
70
    TSDB_CHECK_CODE(code, lino, _exit);
53!
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);
53✔
78
  TSDB_CHECK_CODE(code, lino, _exit);
53!
79

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

84
  code = tsdbOpenCache(pTsdb);
53✔
85
  TSDB_CHECK_CODE(code, lino, _exit);
53!
86

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

91
_exit:
53✔
92
  if (code) {
53!
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,
53✔
99
              pTsdb->keepCfg.days, pTsdb->keepCfg.keep0, pTsdb->keepCfg.keep1, pTsdb->keepCfg.keep2,
100
              pTsdb->keepCfg.keepTimeOffset);
101
    *ppTsdb = pTsdb;
53✔
102
  }
103
  return code;
53✔
104
}
105

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

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