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

taosdata / TDengine / #4875

09 Dec 2025 01:22AM UTC coverage: 64.472% (-0.2%) from 64.623%
#4875

push

travis-ci

guanshengliang
fix: temporarily disable memory leak detection for UDF tests (#33856)

162014 of 251293 relevant lines covered (64.47%)

104318075.66 hits per line

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

87.69
/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

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

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

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

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

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

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

68
void sdbCleanup(SSdb *pSdb) {
501,712✔
69
  mInfo("start to cleanup sdb");
501,712✔
70

71
  int32_t code = 0;
501,712✔
72

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

77
  if (pSdb->currDir != NULL) {
501,712✔
78
    taosMemoryFreeClear(pSdb->currDir);
501,712✔
79
  }
80

81
  if (pSdb->tmpDir != NULL) {
501,712✔
82
    taosRemoveDir(pSdb->tmpDir);
501,712✔
83
    taosMemoryFreeClear(pSdb->tmpDir);
501,712✔
84
  }
85

86
  for (ESdbType i = 0; i < SDB_MAX; ++i) {
20,570,192✔
87
    SHashObj *hash = pSdb->hashObjs[i];
20,068,480✔
88
    if (hash == NULL) continue;
20,068,480✔
89

90
    SSdbRow **ppRow = taosHashIterate(hash, NULL);
18,061,632✔
91
    while (ppRow != NULL) {
86,006,702✔
92
      SSdbRow *pRow = *ppRow;
67,945,070✔
93
      if (pRow == NULL) continue;
67,945,070✔
94

95
      sdbFreeRow(pSdb, pRow, true);
67,945,070✔
96
      ppRow = taosHashIterate(hash, ppRow);
67,945,070✔
97
    }
98
  }
99

100
  for (ESdbType i = 0; i < SDB_MAX; ++i) {
20,570,192✔
101
    SHashObj *hash = pSdb->hashObjs[i];
20,068,480✔
102
    if (hash == NULL) continue;
20,068,480✔
103

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

110
    mInfo("sdb table:%s is cleaned up", sdbTableName(i));
18,061,632✔
111
  }
112

113
  (void)taosThreadMutexDestroy(&pSdb->filelock);
501,712✔
114
  taosMemoryFree(pSdb);
501,712✔
115
  mInfo("sdb is cleaned up");
501,712✔
116
}
501,712✔
117

118
int32_t sdbSetTable(SSdb *pSdb, SSdbTable table) {
18,090,756✔
119
  int32_t code = 0;
18,090,756✔
120

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

134
  int32_t hashType = 0;
18,090,756✔
135
  if (keyType == SDB_KEY_INT32) {
18,090,756✔
136
    hashType = TSDB_DATA_TYPE_INT;
7,537,815✔
137
  } else if (keyType == SDB_KEY_INT64) {
10,552,941✔
138
    hashType = TSDB_DATA_TYPE_BIGINT;
2,512,605✔
139
  } else {
140
    hashType = TSDB_DATA_TYPE_BINARY;
8,040,336✔
141
  }
142

143
  SHashObj *hash = taosHashInit(64, taosGetDefaultHashFunction(hashType), true, HASH_ENTRY_LOCK);
18,090,756✔
144
  if (hash == NULL) {
18,090,756✔
145
    TAOS_RETURN(terrno);
×
146
  }
147

148
  pSdb->maxId[sdbType] = 0;
18,090,756✔
149
  pSdb->hashObjs[sdbType] = hash;
18,090,756✔
150
  mInfo("sdb table:%s is initialized", sdbTableName(sdbType));
18,090,756✔
151

152
  TAOS_RETURN(0);
18,090,756✔
153
}
154

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

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

169
  return 0;
502,521✔
170
}
171

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

177
  pSdb->applyIndex = index;
41,227,672✔
178
  pSdb->applyTerm = term;
41,227,672✔
179
  pSdb->applyConfig = config;
41,227,672✔
180
}
41,227,672✔
181

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

193
void sdbWriteLock(SSdb *pSdb, int32_t type) {
1,679,028,305✔
194
  TdThreadRwlock *pLock = &pSdb->locks[type];
1,679,028,305✔
195
  // mTrace("sdb table:%d start write lock:%p", type, pLock);
196
  (void)taosThreadRwlockWrlock(pLock);
1,679,031,176✔
197
  // mTrace("sdb table:%d stop write lock:%p", type, pLock);
198
}
1,679,049,819✔
199

200
void sdbReadLock(SSdb *pSdb, int32_t type) {
2,137,234,214✔
201
  TdThreadRwlock *pLock = &pSdb->locks[type];
2,137,234,214✔
202
  // mTrace("sdb table:%d start read lock:%p", type, pLock);
203
  (void)taosThreadRwlockRdlock(pLock);
2,137,250,148✔
204
  // mTrace("sdb table:%d stop read lock:%p", type, pLock);
205
}
2,137,272,454✔
206

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