• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

taosdata / TDengine / #3831

02 Apr 2025 01:14AM UTC coverage: 34.081% (-0.02%) from 34.097%
#3831

push

travis-ci

happyguoxy
test:alter gcda dir

148596 of 599532 branches covered (24.79%)

Branch coverage included in aggregate %.

222550 of 489473 relevant lines covered (45.47%)

1589752.67 hits per line

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

73.3
/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) {
67✔
22
  mInfo("start to init sdb in %s", pOption->path);
67!
23

24
  SSdb *pSdb = taosMemoryCalloc(1, sizeof(SSdb));
67!
25
  if (pSdb == NULL) {
67!
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};
67✔
32
  snprintf(path, sizeof(path), "%s%sdata", pOption->path, TD_DIRSEP);
67✔
33
  pSdb->currDir = taosStrdup(path);
67!
34
  snprintf(path, sizeof(path), "%s%stmp", pOption->path, TD_DIRSEP);
67✔
35
  pSdb->tmpDir = taosStrdup(path);
67!
36
  if (pSdb->currDir == NULL || pSdb->tmpDir == NULL) {
67!
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) {
67!
44
    sdbCleanup(pSdb);
×
45
    return NULL;
×
46
  }
47

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

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

68
void sdbCleanup(SSdb *pSdb) {
63✔
69
  mInfo("start to cleanup sdb");
63!
70

71
  int32_t code = 0;
63✔
72

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

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

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

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

90
    SSdbRow **ppRow = taosHashIterate(hash, NULL);
1,566✔
91
    while (ppRow != NULL) {
7,250✔
92
      SSdbRow *pRow = *ppRow;
5,684✔
93
      if (pRow == NULL) continue;
5,684!
94

95
      sdbFreeRow(pSdb, pRow, true);
5,684✔
96
      ppRow = taosHashIterate(hash, ppRow);
5,684✔
97
    }
98
  }
99

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

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

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

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

118
int32_t sdbSetTable(SSdb *pSdb, SSdbTable table) {
1,645✔
119
  int32_t code = 0;
1,645✔
120

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

133
  int32_t hashType = 0;
1,645✔
134
  if (keyType == SDB_KEY_INT32) {
1,645✔
135
    hashType = TSDB_DATA_TYPE_INT;
569✔
136
  } else if (keyType == SDB_KEY_INT64) {
1,076✔
137
    hashType = TSDB_DATA_TYPE_BIGINT;
191✔
138
  } else {
139
    hashType = TSDB_DATA_TYPE_BINARY;
885✔
140
  }
141

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

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

151
  TAOS_RETURN(0);
1,645✔
152
}
153

154
static int32_t sdbCreateDir(SSdb *pSdb) {
67✔
155
  int32_t code = 0;
67✔
156
  if (taosMulMkDir(pSdb->currDir) != 0) {
67!
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

162
  if (taosMkDir(pSdb->tmpDir) != 0) {
67!
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

168
  return 0;
67✔
169
}
170

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

176
  pSdb->applyIndex = index;
2,906✔
177
  pSdb->applyTerm = term;
2,906✔
178
  pSdb->applyConfig = config;
2,906✔
179
}
2,906✔
180

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

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

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

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