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

taosdata / TDengine / #4814

16 Oct 2025 11:40AM UTC coverage: 58.728% (+0.03%) from 58.702%
#4814

push

travis-ci

web-flow
fix(tref): increase TSDB_REF_OBJECTS from 100 to 2000 for improved reference handling (#33281)

139974 of 303532 branches covered (46.12%)

Branch coverage included in aggregate %.

211648 of 295200 relevant lines covered (71.7%)

23886139.71 hits per line

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

68.59
/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) {
32,299✔
20
  SVBufPool *pPool;
21

22
  pPool = taosMemoryCalloc(1, sizeof(SVBufPool));
32,299!
23
  if (pPool == NULL) {
32,299!
24
    vError("vgId:%d, failed to allocate buffer pool", TD_VID(pVnode));
×
25
    return terrno;
×
26
  }
27
  pPool->node.data = taosMemoryMalloc(size);
32,299!
28
  if (NULL == pPool->node.data) {
32,294!
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);
32,294✔
36
  pPool->nQuery = 0;
32,292✔
37
  pPool->qList.pNext = &pPool->qList;
32,292✔
38
  pPool->qList.ppNext = &pPool->qList.pNext;
32,292✔
39

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

48
  *ppPool = pPool;
32,292✔
49
  return 0;
32,292✔
50
}
51

52
static void vnodeBufPoolDestroy(SVBufPool *pPool) {
32,310✔
53
  vnodeBufPoolReset(pPool);
32,310✔
54
  (void)taosThreadMutexDestroy(&pPool->mutex);
32,309✔
55
  taosMemoryFree(pPool->node.data);
32,309!
56
  taosMemoryFree(pPool);
32,310!
57
}
32,310✔
58

59
int vnodeOpenBufPool(SVnode *pVnode) {
10,770✔
60
  int64_t size = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
10,770✔
61

62
  for (int i = 0; i < VNODE_BUFPOOL_SEGMENTS; i++) {
43,070✔
63
    // create pool
64
    int32_t code;
65
    if ((code = vnodeBufPoolCreate(pVnode, i, size, &pVnode->aBufPool[i]))) {
32,301!
66
      vError("vgId:%d, failed to open vnode buffer pool since %s", TD_VID(pVnode), tstrerror(terrno));
×
67
      vnodeCloseBufPool(pVnode);
×
68
      return code;
×
69
    }
70

71
    // add to free list
72
    pVnode->aBufPool[i]->freeNext = pVnode->freeList;
32,300✔
73
    pVnode->freeList = pVnode->aBufPool[i];
32,300✔
74
  }
75

76
  vDebug("vgId:%d, vnode buffer pool is opened, size:%" PRId64, TD_VID(pVnode), size);
10,769✔
77
  return 0;
10,770✔
78
}
79

80
void vnodeCloseBufPool(SVnode *pVnode) {
10,770✔
81
  for (int32_t i = 0; i < VNODE_BUFPOOL_SEGMENTS; i++) {
43,080✔
82
    if (pVnode->aBufPool[i]) {
32,310!
83
      vnodeBufPoolDestroy(pVnode->aBufPool[i]);
32,310✔
84
      pVnode->aBufPool[i] = NULL;
32,310✔
85
    }
86
  }
87

88
  vDebug("vgId:%d, vnode buffer pool is closed", TD_VID(pVnode));
10,770✔
89
}
10,770✔
90

91
void vnodeBufPoolReset(SVBufPool *pPool) {
57,311✔
92
  if (pPool->nQuery != 0) {
57,311!
93
    vError("vgId:%d, buffer pool %p of id %d has %d queries, reset it may cause problem", TD_VID(pPool->pVnode), pPool,
×
94
           pPool->id, pPool->nQuery);
95
  }
96

97
  for (SVBufPoolNode *pNode = pPool->pTail; pNode->prev; pNode = pPool->pTail) {
2,993,842✔
98
    pNode->prev->pnext = &pPool->pTail;
2,936,532✔
99
    pPool->pTail = pNode->prev;
2,936,532✔
100
    pPool->size = pPool->size - sizeof(*pNode) - pNode->size;
2,936,532✔
101
    taosMemoryFree(pNode);
2,936,532!
102
  }
103

104
  pPool->size = 0;
57,310✔
105
  pPool->ptr = pPool->node.data;
57,310✔
106
}
57,310✔
107

108
void *vnodeBufPoolMallocAligned(SVBufPool *pPool, int size) {
1,450,282,323✔
109
  SVBufPoolNode *pNode;
110
  void          *p = NULL;
1,450,282,323✔
111
  uint8_t       *ptr = NULL;
1,450,282,323✔
112
  int            paddingLen = 0;
1,450,282,323✔
113

114
  if (pPool == NULL) {
1,450,282,323!
115
    terrno = TSDB_CODE_INVALID_PARA;
×
116
    return NULL;
×
117
  }
118

119
  ptr = pPool->ptr;
1,450,282,323✔
120
  paddingLen = (((long)ptr + 7) & ~7) - (long)ptr;
1,450,282,323✔
121

122
  if (pPool->node.size >= pPool->ptr - pPool->node.data + size + paddingLen) {
1,450,282,323✔
123
    // allocate from the anchor node
124
    p = pPool->ptr + paddingLen;
1,447,407,018✔
125
    size += paddingLen;
1,447,407,018✔
126
    pPool->ptr = pPool->ptr + size;
1,447,407,018✔
127
    pPool->size += size;
1,447,407,018✔
128
  } else {
129
    // allocate a new node
130
    pNode = taosMemoryMalloc(sizeof(*pNode) + size);
2,875,305!
131
    if (pNode == NULL) {
2,936,531!
132
      return NULL;
×
133
    }
134
    pNode->data = (uint8_t *)&pNode[1];
2,936,531✔
135

136
    p = pNode->data;
2,936,531✔
137
    pNode->size = size;
2,936,531✔
138
    pNode->prev = pPool->pTail;
2,936,531✔
139
    pNode->pnext = &pPool->pTail;
2,936,531✔
140
    pPool->pTail->pnext = &pNode->prev;
2,936,531✔
141
    pPool->pTail = pNode;
2,936,531✔
142

143
    pPool->size = pPool->size + sizeof(*pNode) + size;
2,936,531✔
144
  }
145
  return p;
1,450,343,549✔
146
}
147

148
void *vnodeBufPoolMalloc(SVBufPool *pPool, int size) {
131,880✔
149
  SVBufPoolNode *pNode;
150
  void          *p = NULL;
131,880✔
151

152
  if (pPool == NULL) {
131,880!
153
    terrno = TSDB_CODE_INVALID_PARA;
×
154
    return NULL;
×
155
  }
156

157
  if (pPool->node.size >= pPool->ptr - pPool->node.data + size) {
131,880!
158
    // allocate from the anchor node
159
    p = pPool->ptr;
131,881✔
160
    pPool->ptr = pPool->ptr + size;
131,881✔
161
    pPool->size += size;
131,881✔
162
  } else {
163
    // allocate a new node
164
    pNode = taosMemoryMalloc(sizeof(*pNode) + size);
×
165
    if (pNode == NULL) {
1!
166
      return NULL;
×
167
    }
168
    pNode->data = (uint8_t *)&pNode[1];
1✔
169

170
    p = pNode->data;
1✔
171
    pNode->size = size;
1✔
172
    pNode->prev = pPool->pTail;
1✔
173
    pNode->pnext = &pPool->pTail;
1✔
174
    pPool->pTail->pnext = &pNode->prev;
1✔
175
    pPool->pTail = pNode;
1✔
176

177
    pPool->size = pPool->size + sizeof(*pNode) + size;
1✔
178
  }
179
  return p;
131,882✔
180
}
181

182
void vnodeBufPoolFree(SVBufPool *pPool, void *p) {
181,653✔
183
  // uint8_t       *ptr = (uint8_t *)p;
184
  // SVBufPoolNode *pNode;
185

186
  // if (ptr < pPool->node.data || ptr >= pPool->node.data + pPool->node.size) {
187
  //   pNode = &((SVBufPoolNode *)p)[-1];
188
  //   *pNode->pnext = pNode->prev;
189
  //   pNode->prev->pnext = pNode->pnext;
190

191
  //   pPool->size = pPool->size - sizeof(*pNode) - pNode->size;
192
  //   taosMemoryFree(pNode);
193
  // }
194
}
181,653✔
195

196
void vnodeBufPoolRef(SVBufPool *pPool) {
35,770✔
197
  int32_t nRef = atomic_fetch_add_32(&pPool->nRef, 1);
35,770✔
198
  if (nRef <= 0) {
35,768!
199
    vError("vgId:%d, buffer pool %p of id %d is referenced by %d", TD_VID(pPool->pVnode), pPool, pPool->id, nRef);
×
200
  }
201
}
35,768✔
202

203
static void vnodeBufPoolResize(SVBufPool *pPool, int64_t size) {
×
204
  if (pPool == NULL) return;
×
205

206
  uint8_t *pDataNew = taosMemoryMalloc(size);
×
207
  if (pDataNew == NULL) {
×
208
    vError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pPool->pVnode), __func__, __FILE__, __LINE__,
×
209
           tstrerror(terrno));
210
    return;
×
211
  }
212

213
  // Apply change
214
  int64_t oldSize = pPool->node.size;
×
215
  taosMemoryFree(pPool->node.data);
×
216
  pPool->node.data = pDataNew;
×
217
  pPool->node.size = size;
×
218

219
  vInfo("vgId:%d, buffer pool %d resized from %" PRId64 " to %" PRId64, TD_VID(pPool->pVnode), pPool->id, oldSize,
×
220
        size);
221
  return;
×
222
}
223

224
void vnodeBufPoolAddToFreeList(SVBufPool *pPool) {
25,001✔
225
  SVnode *pVnode = pPool->pVnode;
25,001✔
226

227
  int64_t size = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
25,001✔
228
  if (pPool->node.size != size) {
25,001!
229
    vnodeBufPoolResize(pPool, size);
×
230
  }
231

232
  // add to free list
233
  vDebug("vgId:%d, buffer pool %p of id %d is added to free list", TD_VID(pVnode), pPool, pPool->id);
25,001✔
234
  vnodeBufPoolReset(pPool);
25,001✔
235
  pPool->freeNext = pVnode->freeList;
25,000✔
236
  pVnode->freeList = pPool;
25,000✔
237
  (void)taosThreadCondSignal(&pVnode->poolNotEmpty);
25,000✔
238
}
25,001✔
239

240
void vnodeBufPoolUnRef(SVBufPool *pPool, bool proactive) {
35,770✔
241
  if (pPool == NULL) return;
35,770!
242

243
  SVnode *pVnode = pPool->pVnode;
35,770✔
244

245
  if (proactive) {
35,770✔
246
    (void)taosThreadMutexLock(&pVnode->mutex);
35,768✔
247
  }
248

249
  if (atomic_sub_fetch_32(&pPool->nRef, 1) > 0) goto _exit;
35,771✔
250

251
  // remove from recycle queue or on-recycle position
252
  if (pVnode->onRecycle == pPool) {
428✔
253
    pVnode->onRecycle = NULL;
1✔
254
  } else {
255
    if (pPool->recyclePrev) {
427!
256
      pPool->recyclePrev->recycleNext = pPool->recycleNext;
×
257
    } else {
258
      pVnode->recycleHead = pPool->recycleNext;
427✔
259
    }
260

261
    if (pPool->recycleNext) {
427!
262
      pPool->recycleNext->recyclePrev = pPool->recyclePrev;
×
263
    } else {
264
      pVnode->recycleTail = pPool->recyclePrev;
427✔
265
    }
266
    pPool->recyclePrev = pPool->recycleNext = NULL;
427✔
267
  }
268

269
  vnodeBufPoolAddToFreeList(pPool);
428✔
270

271
_exit:
35,771✔
272
  if (proactive) {
35,771✔
273
    (void)taosThreadMutexUnlock(&pVnode->mutex);
35,769✔
274
  }
275
  return;
35,771✔
276
}
277

278
void vnodeBufPoolRegisterQuery(SVBufPool *pPool, SQueryNode *pQNode) {
6,865,037✔
279
  (void)taosThreadMutexLock(&pPool->mutex);
6,865,037✔
280

281
  pQNode->pNext = pPool->qList.pNext;
6,874,643✔
282
  pQNode->ppNext = &pPool->qList.pNext;
6,874,643✔
283
  pPool->qList.pNext->ppNext = &pQNode->pNext;
6,874,643✔
284
  pPool->qList.pNext = pQNode;
6,874,643✔
285
  pPool->nQuery++;
6,874,643✔
286

287
  (void)taosThreadMutexUnlock(&pPool->mutex);
6,874,643✔
288
}
6,873,154✔
289

290
void vnodeBufPoolDeregisterQuery(SVBufPool *pPool, SQueryNode *pQNode, bool proactive) {
6,874,143✔
291
  int32_t code = 0;
6,874,143✔
292

293
  if (proactive) {
6,874,143!
294
    (void)taosThreadMutexLock(&pPool->mutex);
6,874,399✔
295
  }
296

297
  pQNode->pNext->ppNext = pQNode->ppNext;
6,874,744✔
298
  *pQNode->ppNext = pQNode->pNext;
6,874,744✔
299
  pPool->nQuery--;
6,874,744✔
300

301
  if (proactive) {
6,874,744!
302
    (void)taosThreadMutexUnlock(&pPool->mutex);
6,875,024✔
303
  }
304
}
6,874,667✔
305

306
int32_t vnodeBufPoolRecycle(SVBufPool *pPool) {
1✔
307
  int32_t code = 0;
1✔
308

309
  SVnode *pVnode = pPool->pVnode;
1✔
310

311
  vDebug("vgId:%d, recycle buffer pool %p of id %d", TD_VID(pVnode), pPool, pPool->id);
1!
312

313
  (void)taosThreadMutexLock(&pPool->mutex);
1✔
314

315
  SQueryNode *pNode = pPool->qList.pNext;
1✔
316
  while (pNode != &pPool->qList) {
2✔
317
    SQueryNode *pTNode = pNode->pNext;
1✔
318

319
    int32_t rc = pNode->reseek(pNode->pQHandle);
1✔
320
    if (rc == 0 || rc == TSDB_CODE_VND_QUERY_BUSY) {
1!
321
      pNode = pTNode;
1✔
322
    } else {
323
      code = rc;
×
324
      goto _exit;
×
325
    }
326
  }
327

328
_exit:
1✔
329
  (void)taosThreadMutexUnlock(&pPool->mutex);
1✔
330
  return code;
1✔
331
}
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