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

taosdata / TDengine / #4720

08 Sep 2025 08:43AM UTC coverage: 58.139% (-0.6%) from 58.762%
#4720

push

travis-ci

web-flow
Merge pull request #32881 from taosdata/enh/add-new-windows-ci

fix(ci): update workflow reference to use new Windows CI YAML

133181 of 292179 branches covered (45.58%)

Branch coverage included in aggregate %.

201691 of 283811 relevant lines covered (71.07%)

5442780.71 hits per line

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

57.07
/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) {
37,217✔
20
  SVBufPool *pPool;
21

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

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

48
  if (VND_IS_RSMA(pVnode)) {
37,219!
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);
2!
57
      taosMemoryFree(pPool->node.data);
×
58
      taosMemoryFree(pPool);
×
59
      return terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
60
    }
61
  } else {
62
    pPool->lock = NULL;
37,220✔
63
  }
64

65
  *ppPool = pPool;
37,218✔
66
  return 0;
37,218✔
67
}
68

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

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

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

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

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

101
void vnodeCloseBufPool(SVnode *pVnode) {
12,409✔
102
  for (int32_t i = 0; i < VNODE_BUFPOOL_SEGMENTS; i++) {
49,634✔
103
    if (pVnode->aBufPool[i]) {
37,224!
104
      vnodeBufPoolDestroy(pVnode->aBufPool[i]);
37,226✔
105
      pVnode->aBufPool[i] = NULL;
37,227✔
106
    }
107
  }
108

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

112
void vnodeBufPoolReset(SVBufPool *pPool) {
59,662✔
113
  if (pPool->nQuery != 0) {
59,662!
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) {
277,124✔
119
    pNode->prev->pnext = &pPool->pTail;
217,460✔
120
    pPool->pTail = pNode->prev;
217,460✔
121
    pPool->size = pPool->size - sizeof(*pNode) - pNode->size;
217,460✔
122
    taosMemoryFree(pNode);
217,460!
123
  }
124

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

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

135
  if (pPool == NULL) {
123,892,303!
136
    terrno = TSDB_CODE_INVALID_PARA;
×
137
    return NULL;
×
138
  }
139

140
  if (pPool->lock) taosThreadSpinLock(pPool->lock);
123,892,303!
141

142
  ptr = pPool->ptr;
123,905,020✔
143
  paddingLen = (((long)ptr + 7) & ~7) - (long)ptr;
123,905,020✔
144

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

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

169
    pPool->size = pPool->size + sizeof(*pNode) + size;
217,460✔
170
  }
171
  if (pPool->lock) (void)taosThreadSpinUnlock(pPool->lock);
123,905,020!
172
  return p;
123,900,905✔
173
}
174

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

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

184
  if (pPool->lock) taosThreadSpinLock(pPool->lock);
19,496!
185
  if (pPool->node.size >= pPool->ptr - pPool->node.data + size) {
19,496!
186
    // allocate from the anchor node
187
    p = pPool->ptr;
19,496✔
188
    pPool->ptr = pPool->ptr + size;
19,496✔
189
    pPool->size += size;
19,496✔
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);
19,496!
209
  return p;
19,496✔
210
}
211

212
void vnodeBufPoolFree(SVBufPool *pPool, void *p) {
180,726✔
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
}
180,726✔
225

226
void vnodeBufPoolRef(SVBufPool *pPool) {
34,839✔
227
  int32_t nRef = atomic_fetch_add_32(&pPool->nRef, 1);
34,839✔
228
  if (nRef <= 0) {
34,841!
229
    vError("vgId:%d, buffer pool %p of id %d is referenced by %d", TD_VID(pPool->pVnode), pPool, pPool->id, nRef);
×
230
  }
231
}
34,841✔
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) {
22,437✔
255
  SVnode *pVnode = pPool->pVnode;
22,437✔
256

257
  int64_t size = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
22,437✔
258
  if (pPool->node.size != size) {
22,437!
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);
22,437✔
264
  vnodeBufPoolReset(pPool);
22,437✔
265
  pPool->freeNext = pVnode->freeList;
22,437✔
266
  pVnode->freeList = pPool;
22,437✔
267
  (void)taosThreadCondSignal(&pVnode->poolNotEmpty);
22,437✔
268
}
22,437✔
269

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

273
  SVnode *pVnode = pPool->pVnode;
34,846✔
274

275
  if (proactive) {
34,846✔
276
    (void)taosThreadMutexLock(&pVnode->mutex);
34,842✔
277
  }
278

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

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

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

299
  vnodeBufPoolAddToFreeList(pPool);
125✔
300

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

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

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

317
  (void)taosThreadMutexUnlock(&pPool->mutex);
843,084✔
318
}
843,072✔
319

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

323
  if (proactive) {
843,098!
324
    (void)taosThreadMutexLock(&pPool->mutex);
843,098✔
325
  }
326

327
  pQNode->pNext->ppNext = pQNode->ppNext;
843,105✔
328
  *pQNode->ppNext = pQNode->pNext;
843,105✔
329
  pPool->nQuery--;
843,105✔
330

331
  if (proactive) {
843,105✔
332
    (void)taosThreadMutexUnlock(&pPool->mutex);
843,104✔
333
  }
334
}
843,104✔
335

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

339
  SVnode *pVnode = pPool->pVnode;
2✔
340

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

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

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

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

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

© 2025 Coveralls, Inc