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

taosdata / TDengine / #3621

22 Feb 2025 11:44AM UTC coverage: 2.037% (-61.5%) from 63.573%
#3621

push

travis-ci

web-flow
Merge pull request #29874 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

4357 of 287032 branches covered (1.52%)

Branch coverage included in aggregate %.

0 of 174 new or added lines in 18 files covered. (0.0%)

213359 existing lines in 469 files now uncovered.

7260 of 283369 relevant lines covered (2.56%)

23737.72 hits per line

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

0.0
/source/dnode/mnode/sdb/src/sdb.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
#define _DEFAULT_SOURCE
17
#include "sdb.h"
18

19
static int32_t sdbCreateDir(SSdb *pSdb);
20

UNCOV
21
SSdb *sdbInit(SSdbOpt *pOption) {
×
UNCOV
22
  mInfo("start to init sdb in %s", pOption->path);
×
23

UNCOV
24
  SSdb *pSdb = taosMemoryCalloc(1, sizeof(SSdb));
×
UNCOV
25
  if (pSdb == NULL) {
×
26
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
27
    mError("failed to init sdb since %s", terrstr());
×
28
    return NULL;
×
29
  }
30

UNCOV
31
  char path[PATH_MAX + 100] = {0};
×
UNCOV
32
  snprintf(path, sizeof(path), "%s%sdata", pOption->path, TD_DIRSEP);
×
UNCOV
33
  pSdb->currDir = taosStrdup(path);
×
UNCOV
34
  snprintf(path, sizeof(path), "%s%stmp", pOption->path, TD_DIRSEP);
×
UNCOV
35
  pSdb->tmpDir = taosStrdup(path);
×
UNCOV
36
  if (pSdb->currDir == NULL || pSdb->tmpDir == NULL) {
×
37
    sdbCleanup(pSdb);
×
38
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
39
    mError("failed to init sdb since %s", terrstr());
×
40
    return NULL;
×
41
  }
42

UNCOV
43
  if (sdbCreateDir(pSdb) != 0) {
×
44
    sdbCleanup(pSdb);
×
45
    return NULL;
×
46
  }
47

UNCOV
48
  for (ESdbType i = 0; i < SDB_MAX; ++i) {
×
UNCOV
49
    (void)taosThreadRwlockInit(&pSdb->locks[i], NULL);
×
UNCOV
50
    pSdb->maxId[i] = 0;
×
UNCOV
51
    pSdb->tableVer[i] = 0;
×
UNCOV
52
    pSdb->keyTypes[i] = SDB_KEY_INT32;
×
53
  }
54

UNCOV
55
  pSdb->pWal = pOption->pWal;
×
UNCOV
56
  pSdb->applyIndex = -1;
×
UNCOV
57
  pSdb->applyTerm = -1;
×
UNCOV
58
  pSdb->applyConfig = -1;
×
UNCOV
59
  pSdb->commitIndex = -1;
×
UNCOV
60
  pSdb->commitTerm = -1;
×
UNCOV
61
  pSdb->commitConfig = -1;
×
UNCOV
62
  pSdb->pMnode = pOption->pMnode;
×
UNCOV
63
  (void)taosThreadMutexInit(&pSdb->filelock, NULL);
×
UNCOV
64
  mInfo("sdb init success");
×
UNCOV
65
  return pSdb;
×
66
}
67

UNCOV
68
void sdbCleanup(SSdb *pSdb) {
×
UNCOV
69
  mInfo("start to cleanup sdb");
×
70

UNCOV
71
  int32_t code = 0;
×
72

UNCOV
73
  if ((code = sdbWriteFile(pSdb, 0)) != 0) {
×
UNCOV
74
    mError("failed to write sdb file since %s", tstrerror(code));
×
75
  }
76

UNCOV
77
  if (pSdb->currDir != NULL) {
×
UNCOV
78
    taosMemoryFreeClear(pSdb->currDir);
×
79
  }
80

UNCOV
81
  if (pSdb->tmpDir != NULL) {
×
UNCOV
82
    taosRemoveDir(pSdb->tmpDir);
×
UNCOV
83
    taosMemoryFreeClear(pSdb->tmpDir);
×
84
  }
85

UNCOV
86
  for (ESdbType i = 0; i < SDB_MAX; ++i) {
×
UNCOV
87
    SHashObj *hash = pSdb->hashObjs[i];
×
UNCOV
88
    if (hash == NULL) continue;
×
89

UNCOV
90
    SSdbRow **ppRow = taosHashIterate(hash, NULL);
×
UNCOV
91
    while (ppRow != NULL) {
×
UNCOV
92
      SSdbRow *pRow = *ppRow;
×
UNCOV
93
      if (pRow == NULL) continue;
×
94

UNCOV
95
      sdbFreeRow(pSdb, pRow, true);
×
UNCOV
96
      ppRow = taosHashIterate(hash, ppRow);
×
97
    }
98
  }
99

UNCOV
100
  for (ESdbType i = 0; i < SDB_MAX; ++i) {
×
UNCOV
101
    SHashObj *hash = pSdb->hashObjs[i];
×
UNCOV
102
    if (hash == NULL) continue;
×
103

UNCOV
104
    taosHashClear(hash);
×
UNCOV
105
    taosHashCleanup(hash);
×
UNCOV
106
    (void)taosThreadRwlockDestroy(&pSdb->locks[i]);
×
UNCOV
107
    pSdb->hashObjs[i] = NULL;
×
UNCOV
108
    memset(&pSdb->locks[i], 0, sizeof(pSdb->locks[i]));
×
109

UNCOV
110
    mInfo("sdb table:%s is cleaned up", sdbTableName(i));
×
111
  }
112

UNCOV
113
  (void)taosThreadMutexDestroy(&pSdb->filelock);
×
UNCOV
114
  taosMemoryFree(pSdb);
×
UNCOV
115
  mInfo("sdb is cleaned up");
×
UNCOV
116
}
×
117

UNCOV
118
int32_t sdbSetTable(SSdb *pSdb, SSdbTable table) {
×
UNCOV
119
  int32_t code = 0;
×
120

UNCOV
121
  ESdbType sdbType = table.sdbType;
×
UNCOV
122
  EKeyType keyType = table.keyType;
×
UNCOV
123
  pSdb->keyTypes[sdbType] = table.keyType;
×
UNCOV
124
  pSdb->insertFps[sdbType] = table.insertFp;
×
UNCOV
125
  pSdb->updateFps[sdbType] = table.updateFp;
×
UNCOV
126
  pSdb->deleteFps[sdbType] = table.deleteFp;
×
UNCOV
127
  pSdb->deployFps[sdbType] = table.deployFp;
×
UNCOV
128
  pSdb->encodeFps[sdbType] = table.encodeFp;
×
UNCOV
129
  pSdb->decodeFps[sdbType] = table.decodeFp;
×
UNCOV
130
  pSdb->afterRestoredFps[sdbType] = table.afterRestoredFp;
×
UNCOV
131
  pSdb->validateFps[sdbType] = table.validateFp;
×
132

UNCOV
133
  int32_t hashType = 0;
×
UNCOV
134
  if (keyType == SDB_KEY_INT32) {
×
UNCOV
135
    hashType = TSDB_DATA_TYPE_INT;
×
UNCOV
136
  } else if (keyType == SDB_KEY_INT64) {
×
UNCOV
137
    hashType = TSDB_DATA_TYPE_BIGINT;
×
138
  } else {
UNCOV
139
    hashType = TSDB_DATA_TYPE_BINARY;
×
140
  }
141

UNCOV
142
  SHashObj *hash = taosHashInit(64, taosGetDefaultHashFunction(hashType), true, HASH_ENTRY_LOCK);
×
UNCOV
143
  if (hash == NULL) {
×
144
    TAOS_RETURN(terrno);
×
145
  }
146

UNCOV
147
  pSdb->maxId[sdbType] = 0;
×
UNCOV
148
  pSdb->hashObjs[sdbType] = hash;
×
UNCOV
149
  mInfo("sdb table:%s is initialized", sdbTableName(sdbType));
×
150

UNCOV
151
  TAOS_RETURN(0);
×
152
}
153

UNCOV
154
static int32_t sdbCreateDir(SSdb *pSdb) {
×
UNCOV
155
  int32_t code = 0;
×
UNCOV
156
  if (taosMulMkDir(pSdb->currDir) != 0) {
×
157
    code = TAOS_SYSTEM_ERROR(errno);
×
158
    mError("failed to create dir:%s since %s", pSdb->currDir, tstrerror(code));
×
159
    TAOS_RETURN(code);
×
160
  }
161

UNCOV
162
  if (taosMkDir(pSdb->tmpDir) != 0) {
×
163
    code = TAOS_SYSTEM_ERROR(errno);
×
164
    mError("failed to create dir:%s since %s", pSdb->tmpDir, tstrerror(code));
×
165
    TAOS_RETURN(code);
×
166
  }
167

UNCOV
168
  return 0;
×
169
}
170

UNCOV
171
void sdbSetApplyInfo(SSdb *pSdb, int64_t index, int64_t term, int64_t config) {
×
UNCOV
172
  mInfo("vgId:1, mnode apply info changed from index:%" PRId64 " term:%" PRId64 " config:%" PRId64 " to index:%" PRId64
×
173
        " term:%" PRId64 " config:%" PRId64,
174
        pSdb->applyIndex, pSdb->applyTerm, pSdb->applyConfig, index, term, config);
175

UNCOV
176
  pSdb->applyIndex = index;
×
UNCOV
177
  pSdb->applyTerm = term;
×
UNCOV
178
  pSdb->applyConfig = config;
×
UNCOV
179
}
×
180

UNCOV
181
void sdbGetCommitInfo(SSdb *pSdb, int64_t *index, int64_t *term, int64_t *config) {
×
UNCOV
182
  *index = pSdb->commitIndex;
×
UNCOV
183
  *term = pSdb->commitTerm;
×
UNCOV
184
  *config = pSdb->commitConfig;
×
185
#if 1
UNCOV
186
  mTrace("mnode current info, apply index:%" PRId64 " term:%" PRId64 " config:%" PRId64 ", commit index:%" PRId64
×
187
         " term:%" PRId64 " config:%" PRId64,
188
         pSdb->applyIndex, pSdb->applyTerm, pSdb->applyConfig, *index, *term, *config);
189
#endif
UNCOV
190
}
×
191

UNCOV
192
void sdbWriteLock(SSdb *pSdb, int32_t type) {
×
UNCOV
193
  TdThreadRwlock *pLock = &pSdb->locks[type];
×
194
  // mTrace("sdb table:%d start write lock:%p", type, pLock);
UNCOV
195
  (void)taosThreadRwlockWrlock(pLock);
×
196
  // mTrace("sdb table:%d stop write lock:%p", type, pLock);
UNCOV
197
}
×
198

UNCOV
199
void sdbReadLock(SSdb *pSdb, int32_t type) {
×
UNCOV
200
  TdThreadRwlock *pLock = &pSdb->locks[type];
×
201
  // mTrace("sdb table:%d start read lock:%p", type, pLock);
UNCOV
202
  (void)taosThreadRwlockRdlock(pLock);
×
203
  // mTrace("sdb table:%d stop read lock:%p", type, pLock);
UNCOV
204
}
×
205

UNCOV
206
void sdbUnLock(SSdb *pSdb, int32_t type) {
×
UNCOV
207
  TdThreadRwlock *pLock = &pSdb->locks[type];
×
208
  // mTrace("sdb table:%d unlock:%p", type, pLock);
UNCOV
209
  (void)taosThreadRwlockUnlock(pLock);
×
UNCOV
210
}
×
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