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

taosdata / TDengine / #4752

21 Sep 2025 11:53PM UTC coverage: 58.977% (+0.09%) from 58.889%
#4752

push

travis-ci

web-flow
fix: refine python taos error log matching in checkAsan.sh (#33029)

* fix: refine python taos error log matching in checkAsan.sh

* fix: improve python taos error log matching in checkAsan.sh

136080 of 293157 branches covered (46.42%)

Branch coverage included in aggregate %.

204728 of 284713 relevant lines covered (71.91%)

25614603.35 hits per line

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

51.57
/source/dnode/vnode/src/vnd/vnodeBufPool.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 "vnd.h"
17

18
/* ------------------------ STRUCTURES ------------------------ */
19
static int32_t vnodeBufPoolCreate(SVnode *pVnode, int32_t id, int64_t size, SVBufPool **ppPool) {
40,556✔
20
  SVBufPool *pPool;
21

22
  pPool = taosMemoryCalloc(1, sizeof(SVBufPool));
40,556!
23
  if (pPool == NULL) {
40,556!
24
    vError("vgId:%d, failed to allocate buffer pool", TD_VID(pVnode));
×
25
    return terrno;
×
26
  }
27
  pPool->node.data = taosMemoryMalloc(size);
40,556!
28
  if (NULL == pPool->node.data) {
40,561!
29
    vError("vgId:%d, failed to allocate buffer pool data, size:%" PRId64, TD_VID(pVnode), size);
×
30
    taosMemoryFree(pPool);
×
31
    return terrno;
×
32
  }
33

34
  // query handle list
35
  (void)taosThreadMutexInit(&pPool->mutex, NULL);
40,561✔
36
  pPool->nQuery = 0;
40,556✔
37
  pPool->qList.pNext = &pPool->qList;
40,556✔
38
  pPool->qList.ppNext = &pPool->qList.pNext;
40,556✔
39

40
  pPool->pVnode = pVnode;
40,556✔
41
  pPool->id = id;
40,556✔
42
  pPool->ptr = pPool->node.data;
40,556✔
43
  pPool->pTail = &pPool->node;
40,556✔
44
  pPool->node.prev = NULL;
40,556✔
45
  pPool->node.pnext = &pPool->pTail;
40,556✔
46
  pPool->node.size = size;
40,556✔
47

48
  if (VND_IS_RSMA(pVnode)) {
40,556!
49
    pPool->lock = taosMemoryMalloc(sizeof(TdThreadSpinlock));
×
50
    if (!pPool->lock) {
×
51
      taosMemoryFree(pPool->node.data);
×
52
      taosMemoryFree(pPool);
×
53
      return terrno;
×
54
    }
55
    if (taosThreadSpinInit(pPool->lock, 0) != 0) {
×
56
      taosMemoryFree((void *)pPool->lock);
×
57
      taosMemoryFree(pPool->node.data);
×
58
      taosMemoryFree(pPool);
×
59
      return terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
60
    }
61
  } else {
62
    pPool->lock = NULL;
40,556✔
63
  }
64

65
  *ppPool = pPool;
40,556✔
66
  return 0;
40,556✔
67
}
68

69
static void vnodeBufPoolDestroy(SVBufPool *pPool) {
40,566✔
70
  vnodeBufPoolReset(pPool);
40,566✔
71
  if (pPool->lock) {
40,566!
72
    (void)taosThreadSpinDestroy(pPool->lock);
×
73
    taosMemoryFree((void *)pPool->lock);
×
74
  }
75
  (void)taosThreadMutexDestroy(&pPool->mutex);
40,566✔
76
  taosMemoryFree(pPool->node.data);
40,566!
77
  taosMemoryFree(pPool);
40,566!
78
}
40,566✔
79

80
int vnodeOpenBufPool(SVnode *pVnode) {
13,522✔
81
  int64_t size = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
13,522✔
82

83
  for (int i = 0; i < VNODE_BUFPOOL_SEGMENTS; i++) {
54,076✔
84
    // create pool
85
    int32_t code;
86
    if ((code = vnodeBufPoolCreate(pVnode, i, size, &pVnode->aBufPool[i]))) {
40,555✔
87
      vError("vgId:%d, failed to open vnode buffer pool since %s", TD_VID(pVnode), tstrerror(terrno));
2!
88
      vnodeCloseBufPool(pVnode);
2✔
89
      return code;
×
90
    }
91

92
    // add to free list
93
    pVnode->aBufPool[i]->freeNext = pVnode->freeList;
40,554✔
94
    pVnode->freeList = pVnode->aBufPool[i];
40,554✔
95
  }
96

97
  vDebug("vgId:%d, vnode buffer pool is opened, size:%" PRId64, TD_VID(pVnode), size);
13,521✔
98
  return 0;
13,521✔
99
}
100

101
void vnodeCloseBufPool(SVnode *pVnode) {
13,521✔
102
  for (int32_t i = 0; i < VNODE_BUFPOOL_SEGMENTS; i++) {
54,087✔
103
    if (pVnode->aBufPool[i]) {
40,566!
104
      vnodeBufPoolDestroy(pVnode->aBufPool[i]);
40,566✔
105
      pVnode->aBufPool[i] = NULL;
40,566✔
106
    }
107
  }
108

109
  vDebug("vgId:%d, vnode buffer pool is closed", TD_VID(pVnode));
13,521✔
110
}
13,521✔
111

112
void vnodeBufPoolReset(SVBufPool *pPool) {
74,539✔
113
  if (pPool->nQuery != 0) {
74,539!
114
    vError("vgId:%d, buffer pool %p of id %d has %d queries, reset it may cause problem", TD_VID(pPool->pVnode), pPool,
×
115
           pPool->id, pPool->nQuery);
116
  }
117

118
  for (SVBufPoolNode *pNode = pPool->pTail; pNode->prev; pNode = pPool->pTail) {
3,021,290✔
119
    pNode->prev->pnext = &pPool->pTail;
2,946,751✔
120
    pPool->pTail = pNode->prev;
2,946,751✔
121
    pPool->size = pPool->size - sizeof(*pNode) - pNode->size;
2,946,751✔
122
    taosMemoryFree(pNode);
2,946,751!
123
  }
124

125
  pPool->size = 0;
74,539✔
126
  pPool->ptr = pPool->node.data;
74,539✔
127
}
74,539✔
128

129
void *vnodeBufPoolMallocAligned(SVBufPool *pPool, int size) {
1,368,087,162✔
130
  SVBufPoolNode *pNode;
131
  void          *p = NULL;
1,368,087,162✔
132
  uint8_t       *ptr = NULL;
1,368,087,162✔
133
  int            paddingLen = 0;
1,368,087,162✔
134

135
  if (pPool == NULL) {
1,368,087,162!
136
    terrno = TSDB_CODE_INVALID_PARA;
×
137
    return NULL;
×
138
  }
139

140
  if (pPool->lock) taosThreadSpinLock(pPool->lock);
1,368,087,162!
141

142
  ptr = pPool->ptr;
1,368,147,339✔
143
  paddingLen = (((long)ptr + 7) & ~7) - (long)ptr;
1,368,147,339✔
144

145
  if (pPool->node.size >= pPool->ptr - pPool->node.data + size + paddingLen) {
1,368,147,339✔
146
    // allocate from the anchor node
147
    p = pPool->ptr + paddingLen;
1,365,200,588✔
148
    size += paddingLen;
1,365,200,588✔
149
    pPool->ptr = pPool->ptr + size;
1,365,200,588✔
150
    pPool->size += size;
1,365,200,588✔
151
  } else {
152
    // allocate a new node
153
    pNode = taosMemoryMalloc(sizeof(*pNode) + size);
2,946,751!
154
    if (pNode == NULL) {
2,946,751!
155
      if (pPool->lock) {
×
156
        (void)taosThreadSpinUnlock(pPool->lock);
×
157
      }
158
      return NULL;
×
159
    }
160
    pNode->data = (uint8_t *)&pNode[1];
2,946,751✔
161

162
    p = pNode->data;
2,946,751✔
163
    pNode->size = size;
2,946,751✔
164
    pNode->prev = pPool->pTail;
2,946,751✔
165
    pNode->pnext = &pPool->pTail;
2,946,751✔
166
    pPool->pTail->pnext = &pNode->prev;
2,946,751✔
167
    pPool->pTail = pNode;
2,946,751✔
168

169
    pPool->size = pPool->size + sizeof(*pNode) + size;
2,946,751✔
170
  }
171
  if (pPool->lock) (void)taosThreadSpinUnlock(pPool->lock);
1,368,147,339!
172
  return p;
1,368,084,390✔
173
}
174

175
void *vnodeBufPoolMalloc(SVBufPool *pPool, int size) {
135,469✔
176
  SVBufPoolNode *pNode;
177
  void          *p = NULL;
135,469✔
178

179
  if (pPool == NULL) {
135,469!
180
    terrno = TSDB_CODE_INVALID_PARA;
×
181
    return NULL;
×
182
  }
183

184
  if (pPool->lock) taosThreadSpinLock(pPool->lock);
135,469!
185
  if (pPool->node.size >= pPool->ptr - pPool->node.data + size) {
135,470!
186
    // allocate from the anchor node
187
    p = pPool->ptr;
135,470✔
188
    pPool->ptr = pPool->ptr + size;
135,470✔
189
    pPool->size += size;
135,470✔
190
  } else {
191
    // allocate a new node
192
    pNode = taosMemoryMalloc(sizeof(*pNode) + size);
×
193
    if (pNode == NULL) {
×
194
      if (pPool->lock) (void)taosThreadSpinUnlock(pPool->lock);
×
195
      return NULL;
×
196
    }
197
    pNode->data = (uint8_t *)&pNode[1];
×
198

199
    p = pNode->data;
×
200
    pNode->size = size;
×
201
    pNode->prev = pPool->pTail;
×
202
    pNode->pnext = &pPool->pTail;
×
203
    pPool->pTail->pnext = &pNode->prev;
×
204
    pPool->pTail = pNode;
×
205

206
    pPool->size = pPool->size + sizeof(*pNode) + size;
×
207
  }
208
  if (pPool->lock) (void)taosThreadSpinUnlock(pPool->lock);
135,470!
209
  return p;
135,471✔
210
}
211

212
void vnodeBufPoolFree(SVBufPool *pPool, void *p) {
190,384✔
213
  // uint8_t       *ptr = (uint8_t *)p;
214
  // SVBufPoolNode *pNode;
215

216
  // if (ptr < pPool->node.data || ptr >= pPool->node.data + pPool->node.size) {
217
  //   pNode = &((SVBufPoolNode *)p)[-1];
218
  //   *pNode->pnext = pNode->prev;
219
  //   pNode->prev->pnext = pNode->pnext;
220

221
  //   pPool->size = pPool->size - sizeof(*pNode) - pNode->size;
222
  //   taosMemoryFree(pNode);
223
  // }
224
}
190,384✔
225

226
void vnodeBufPoolRef(SVBufPool *pPool) {
47,496✔
227
  int32_t nRef = atomic_fetch_add_32(&pPool->nRef, 1);
47,496✔
228
  if (nRef <= 0) {
47,494!
229
    vError("vgId:%d, buffer pool %p of id %d is referenced by %d", TD_VID(pPool->pVnode), pPool, pPool->id, nRef);
×
230
  }
231
}
47,494✔
232

233
static void vnodeBufPoolResize(SVBufPool *pPool, int64_t size) {
×
234
  if (pPool == NULL) return;
×
235

236
  uint8_t *pDataNew = taosMemoryMalloc(size);
×
237
  if (pDataNew == NULL) {
×
238
    vError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pPool->pVnode), __func__, __FILE__, __LINE__,
×
239
           tstrerror(terrno));
240
    return;
×
241
  }
242

243
  // Apply change
244
  int64_t oldSize = pPool->node.size;
×
245
  taosMemoryFree(pPool->node.data);
×
246
  pPool->node.data = pDataNew;
×
247
  pPool->node.size = size;
×
248

249
  vInfo("vgId:%d, buffer pool %d resized from %" PRId64 " to %" PRId64, TD_VID(pPool->pVnode), pPool->id, oldSize,
×
250
        size);
251
  return;
×
252
}
253

254
void vnodeBufPoolAddToFreeList(SVBufPool *pPool) {
33,974✔
255
  SVnode *pVnode = pPool->pVnode;
33,974✔
256

257
  int64_t size = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
33,974✔
258
  if (pPool->node.size != size) {
33,974!
259
    vnodeBufPoolResize(pPool, size);
×
260
  }
261

262
  // add to free list
263
  vDebug("vgId:%d, buffer pool %p of id %d is added to free list", TD_VID(pVnode), pPool, pPool->id);
33,974✔
264
  vnodeBufPoolReset(pPool);
33,974✔
265
  pPool->freeNext = pVnode->freeList;
33,971✔
266
  pVnode->freeList = pPool;
33,971✔
267
  (void)taosThreadCondSignal(&pVnode->poolNotEmpty);
33,971✔
268
}
33,972✔
269

270
void vnodeBufPoolUnRef(SVBufPool *pPool, bool proactive) {
47,496✔
271
  if (pPool == NULL) return;
47,496!
272

273
  SVnode *pVnode = pPool->pVnode;
47,496✔
274

275
  if (proactive) {
47,496!
276
    (void)taosThreadMutexLock(&pVnode->mutex);
47,496✔
277
  }
278

279
  if (atomic_sub_fetch_32(&pPool->nRef, 1) > 0) goto _exit;
47,496✔
280

281
  // remove from recycle queue or on-recycle position
282
  if (pVnode->onRecycle == pPool) {
360!
283
    pVnode->onRecycle = NULL;
×
284
  } else {
285
    if (pPool->recyclePrev) {
360!
286
      pPool->recyclePrev->recycleNext = pPool->recycleNext;
×
287
    } else {
288
      pVnode->recycleHead = pPool->recycleNext;
360✔
289
    }
290

291
    if (pPool->recycleNext) {
360!
292
      pPool->recycleNext->recyclePrev = pPool->recyclePrev;
×
293
    } else {
294
      pVnode->recycleTail = pPool->recyclePrev;
360✔
295
    }
296
    pPool->recyclePrev = pPool->recycleNext = NULL;
360✔
297
  }
298

299
  vnodeBufPoolAddToFreeList(pPool);
360✔
300

301
_exit:
47,496✔
302
  if (proactive) {
47,496!
303
    (void)taosThreadMutexUnlock(&pVnode->mutex);
47,496✔
304
  }
305
  return;
47,496✔
306
}
307

308
void vnodeBufPoolRegisterQuery(SVBufPool *pPool, SQueryNode *pQNode) {
7,569,687✔
309
  (void)taosThreadMutexLock(&pPool->mutex);
7,569,687✔
310

311
  pQNode->pNext = pPool->qList.pNext;
7,579,160✔
312
  pQNode->ppNext = &pPool->qList.pNext;
7,579,160✔
313
  pPool->qList.pNext->ppNext = &pQNode->pNext;
7,579,160✔
314
  pPool->qList.pNext = pQNode;
7,579,160✔
315
  pPool->nQuery++;
7,579,160✔
316

317
  (void)taosThreadMutexUnlock(&pPool->mutex);
7,579,160✔
318
}
7,578,102✔
319

320
void vnodeBufPoolDeregisterQuery(SVBufPool *pPool, SQueryNode *pQNode, bool proactive) {
7,578,576✔
321
  int32_t code = 0;
7,578,576✔
322

323
  if (proactive) {
7,578,576!
324
    (void)taosThreadMutexLock(&pPool->mutex);
7,579,029✔
325
  }
326

327
  pQNode->pNext->ppNext = pQNode->ppNext;
7,579,365✔
328
  *pQNode->ppNext = pQNode->pNext;
7,579,365✔
329
  pPool->nQuery--;
7,579,365✔
330

331
  if (proactive) {
7,579,365!
332
    (void)taosThreadMutexUnlock(&pPool->mutex);
7,579,826✔
333
  }
334
}
7,579,217✔
335

336
int32_t vnodeBufPoolRecycle(SVBufPool *pPool) {
×
337
  int32_t code = 0;
×
338

339
  SVnode *pVnode = pPool->pVnode;
×
340

341
  vDebug("vgId:%d, recycle buffer pool %p of id %d", TD_VID(pVnode), pPool, pPool->id);
×
342

343
  (void)taosThreadMutexLock(&pPool->mutex);
×
344

345
  SQueryNode *pNode = pPool->qList.pNext;
×
346
  while (pNode != &pPool->qList) {
×
347
    SQueryNode *pTNode = pNode->pNext;
×
348

349
    int32_t rc = pNode->reseek(pNode->pQHandle);
×
350
    if (rc == 0 || rc == TSDB_CODE_VND_QUERY_BUSY) {
×
351
      pNode = pTNode;
×
352
    } else {
353
      code = rc;
×
354
      goto _exit;
×
355
    }
356
  }
357

358
_exit:
×
359
  (void)taosThreadMutexUnlock(&pPool->mutex);
×
360
  return code;
×
361
}
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