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

taosdata / TDengine / #3562

20 Dec 2024 09:57AM UTC coverage: 26.655% (-32.2%) from 58.812%
#3562

push

travis-ci

web-flow
Merge pull request #29229 from taosdata/enh/TS-5749-3.0

enh: seperate tsdb async tasks to different thread pools

21498 of 109421 branches covered (19.65%)

Branch coverage included in aggregate %.

66 of 96 new or added lines in 7 files covered. (68.75%)

39441 existing lines in 157 files now uncovered.

35007 of 102566 relevant lines covered (34.13%)

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

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

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

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

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

71
  int32_t code = 0;
13✔
72

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

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

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

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

90
    SSdbRow **ppRow = taosHashIterate(hash, NULL);
338✔
91
    while (ppRow != NULL) {
1,647✔
92
      SSdbRow *pRow = *ppRow;
1,309✔
93
      if (pRow == NULL) continue;
1,309!
94

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

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

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

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

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

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

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

133
  int32_t hashType = 0;
338✔
134
  if (keyType == SDB_KEY_INT32) {
338✔
135
    hashType = TSDB_DATA_TYPE_INT;
117✔
136
  } else if (keyType == SDB_KEY_INT64) {
221✔
137
    hashType = TSDB_DATA_TYPE_BIGINT;
39✔
138
  } else {
139
    hashType = TSDB_DATA_TYPE_BINARY;
182✔
140
  }
141

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

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

151
  TAOS_RETURN(0);
338✔
152
}
153

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

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

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

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

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

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

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