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

taosdata / TDengine / #4065

13 May 2025 05:36AM UTC coverage: 59.223% (+4.8%) from 54.389%
#4065

push

travis-ci

happyguoxy
Sync branches at 2025-05-13 13:35

144240 of 312886 branches covered (46.1%)

Branch coverage included in aggregate %.

225142 of 310823 relevant lines covered (72.43%)

5484141.37 hits per line

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

61.52
/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) {
35,716✔
20
  SVBufPool *pPool;
21

22
  pPool = taosMemoryMalloc(sizeof(SVBufPool) + size);
35,716!
23
  if (pPool == NULL) {
35,717!
24
    return terrno;
×
25
  }
26
  memset(pPool, 0, sizeof(SVBufPool));
35,717✔
27

28
  // query handle list
29
  (void)taosThreadMutexInit(&pPool->mutex, NULL);
35,717✔
30
  pPool->nQuery = 0;
35,714✔
31
  pPool->qList.pNext = &pPool->qList;
35,714✔
32
  pPool->qList.ppNext = &pPool->qList.pNext;
35,714✔
33

34
  pPool->pVnode = pVnode;
35,714✔
35
  pPool->id = id;
35,714✔
36
  pPool->ptr = pPool->node.data;
35,714✔
37
  pPool->pTail = &pPool->node;
35,714✔
38
  pPool->node.prev = NULL;
35,714✔
39
  pPool->node.pnext = &pPool->pTail;
35,714✔
40
  pPool->node.size = size;
35,714✔
41

42
  if (VND_IS_RSMA(pVnode)) {
35,714✔
43
    pPool->lock = taosMemoryMalloc(sizeof(TdThreadSpinlock));
63!
44
    if (!pPool->lock) {
63!
45
      taosMemoryFree(pPool);
×
46
      return terrno;
×
47
    }
48
    if (taosThreadSpinInit(pPool->lock, 0) != 0) {
63!
49
      taosMemoryFree((void *)pPool->lock);
×
50
      taosMemoryFree(pPool);
×
51
      return terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
52
    }
53
  } else {
54
    pPool->lock = NULL;
35,651✔
55
  }
56

57
  *ppPool = pPool;
35,714✔
58
  return 0;
35,714✔
59
}
60

61
static void vnodeBufPoolDestroy(SVBufPool *pPool) {
35,727✔
62
  vnodeBufPoolReset(pPool);
35,727✔
63
  if (pPool->lock) {
35,727✔
64
    (void)taosThreadSpinDestroy(pPool->lock);
63✔
65
    taosMemoryFree((void *)pPool->lock);
63!
66
  }
67
  (void)taosThreadMutexDestroy(&pPool->mutex);
35,727✔
68
  taosMemoryFree(pPool);
35,727!
69
}
35,727✔
70

71
int vnodeOpenBufPool(SVnode *pVnode) {
11,909✔
72
  int64_t size = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
11,909✔
73

74
  for (int i = 0; i < VNODE_BUFPOOL_SEGMENTS; i++) {
47,626✔
75
    // create pool
76
    int32_t code;
77
    if ((code = vnodeBufPoolCreate(pVnode, i, size, &pVnode->aBufPool[i]))) {
35,713!
78
      vError("vgId:%d, failed to open vnode buffer pool since %s", TD_VID(pVnode), tstrerror(terrno));
×
79
      vnodeCloseBufPool(pVnode);
×
80
      return code;
×
81
    }
82

83
    // add to free list
84
    pVnode->aBufPool[i]->freeNext = pVnode->freeList;
35,717✔
85
    pVnode->freeList = pVnode->aBufPool[i];
35,717✔
86
  }
87

88
  vDebug("vgId:%d, vnode buffer pool is opened, size:%" PRId64, TD_VID(pVnode), size);
11,913✔
89
  return 0;
11,907✔
90
}
91

92
void vnodeCloseBufPool(SVnode *pVnode) {
11,909✔
93
  for (int32_t i = 0; i < VNODE_BUFPOOL_SEGMENTS; i++) {
47,636✔
94
    if (pVnode->aBufPool[i]) {
35,727!
95
      vnodeBufPoolDestroy(pVnode->aBufPool[i]);
35,727✔
96
      pVnode->aBufPool[i] = NULL;
35,727✔
97
    }
98
  }
99

100
  vDebug("vgId:%d, vnode buffer pool is closed", TD_VID(pVnode));
11,909✔
101
}
11,909✔
102

103
void vnodeBufPoolReset(SVBufPool *pPool) {
56,647✔
104
  if (pPool->nQuery != 0) {
56,647!
105
    vError("vgId:%d, buffer pool %p of id %d has %d queries, reset it may cause problem", TD_VID(pPool->pVnode), pPool,
×
106
           pPool->id, pPool->nQuery);
107
  }
108

109
  for (SVBufPoolNode *pNode = pPool->pTail; pNode->prev; pNode = pPool->pTail) {
9,789,886✔
110
    pNode->prev->pnext = &pPool->pTail;
9,733,239✔
111
    pPool->pTail = pNode->prev;
9,733,239✔
112
    pPool->size = pPool->size - sizeof(*pNode) - pNode->size;
9,733,239✔
113
    taosMemoryFree(pNode);
9,733,239!
114
  }
115

116
  pPool->size = 0;
56,647✔
117
  pPool->ptr = pPool->node.data;
56,647✔
118
}
56,647✔
119

120
void *vnodeBufPoolMallocAligned(SVBufPool *pPool, int size) {
138,402,033✔
121
  SVBufPoolNode *pNode;
122
  void          *p = NULL;
138,402,033✔
123
  uint8_t       *ptr = NULL;
138,402,033✔
124
  int            paddingLen = 0;
138,402,033✔
125

126
  if (pPool == NULL) {
138,402,033!
127
    terrno = TSDB_CODE_INVALID_PARA;
×
128
    return NULL;
×
129
  }
130

131
  if (pPool->lock) taosThreadSpinLock(pPool->lock);
138,402,033✔
132

133
  ptr = pPool->ptr;
138,571,562✔
134
  paddingLen = (((long)ptr + 7) & ~7) - (long)ptr;
138,571,562✔
135

136
  if (pPool->node.size >= pPool->ptr - pPool->node.data + size + paddingLen) {
138,571,562✔
137
    // allocate from the anchor node
138
    p = pPool->ptr + paddingLen;
128,838,323✔
139
    size += paddingLen;
128,838,323✔
140
    pPool->ptr = pPool->ptr + size;
128,838,323✔
141
    pPool->size += size;
128,838,323✔
142
  } else {
143
    // allocate a new node
144
    pNode = taosMemoryMalloc(sizeof(*pNode) + size);
9,733,239!
145
    if (pNode == NULL) {
9,733,239!
146
      if (pPool->lock) {
×
147
        (void)taosThreadSpinUnlock(pPool->lock);
×
148
      }
149
      return NULL;
×
150
    }
151

152
    p = pNode->data;
9,733,239✔
153
    pNode->size = size;
9,733,239✔
154
    pNode->prev = pPool->pTail;
9,733,239✔
155
    pNode->pnext = &pPool->pTail;
9,733,239✔
156
    pPool->pTail->pnext = &pNode->prev;
9,733,239✔
157
    pPool->pTail = pNode;
9,733,239✔
158

159
    pPool->size = pPool->size + sizeof(*pNode) + size;
9,733,239✔
160
  }
161
  if (pPool->lock) (void)taosThreadSpinUnlock(pPool->lock);
138,571,562✔
162
  return p;
139,396,126✔
163
}
164

165
void *vnodeBufPoolMalloc(SVBufPool *pPool, int size) {
90,129✔
166
  SVBufPoolNode *pNode;
167
  void          *p = NULL;
90,129✔
168

169
  if (pPool == NULL) {
90,129!
170
    terrno = TSDB_CODE_INVALID_PARA;
×
171
    return NULL;
×
172
  }
173

174
  if (pPool->lock) taosThreadSpinLock(pPool->lock);
90,129✔
175
  if (pPool->node.size >= pPool->ptr - pPool->node.data + size) {
90,130!
176
    // allocate from the anchor node
177
    p = pPool->ptr;
90,130✔
178
    pPool->ptr = pPool->ptr + size;
90,130✔
179
    pPool->size += size;
90,130✔
180
  } else {
181
    // allocate a new node
182
    pNode = taosMemoryMalloc(sizeof(*pNode) + size);
×
183
    if (pNode == NULL) {
×
184
      if (pPool->lock) (void)taosThreadSpinUnlock(pPool->lock);
×
185
      return NULL;
×
186
    }
187

188
    p = pNode->data;
×
189
    pNode->size = size;
×
190
    pNode->prev = pPool->pTail;
×
191
    pNode->pnext = &pPool->pTail;
×
192
    pPool->pTail->pnext = &pNode->prev;
×
193
    pPool->pTail = pNode;
×
194

195
    pPool->size = pPool->size + sizeof(*pNode) + size;
×
196
  }
197
  if (pPool->lock) (void)taosThreadSpinUnlock(pPool->lock);
90,130✔
198
  return p;
90,132✔
199
}
200

201
void vnodeBufPoolFree(SVBufPool *pPool, void *p) {
12,778✔
202
  // uint8_t       *ptr = (uint8_t *)p;
203
  // SVBufPoolNode *pNode;
204

205
  // if (ptr < pPool->node.data || ptr >= pPool->node.data + pPool->node.size) {
206
  //   pNode = &((SVBufPoolNode *)p)[-1];
207
  //   *pNode->pnext = pNode->prev;
208
  //   pNode->prev->pnext = pNode->pnext;
209

210
  //   pPool->size = pPool->size - sizeof(*pNode) - pNode->size;
211
  //   taosMemoryFree(pNode);
212
  // }
213
}
12,778✔
214

215
void vnodeBufPoolRef(SVBufPool *pPool) {
32,909✔
216
  int32_t nRef = atomic_fetch_add_32(&pPool->nRef, 1);
32,909✔
217
  if (nRef <= 0) {
32,906!
218
    vError("vgId:%d, buffer pool %p of id %d is referenced by %d", TD_VID(pPool->pVnode), pPool, pPool->id, nRef);
×
219
  }
220
}
32,906✔
221

222
void vnodeBufPoolAddToFreeList(SVBufPool *pPool) {
20,922✔
223
  SVnode *pVnode = pPool->pVnode;
20,922✔
224

225
  int64_t size = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
20,922✔
226
  if (pPool->node.size != size) {
20,922!
227
    SVBufPool *pNewPool = NULL;
×
228
    if (vnodeBufPoolCreate(pVnode, pPool->id, size, &pNewPool) < 0) {
×
229
      vWarn("vgId:%d, failed to change buffer pool of id %d size from %" PRId64 " to %" PRId64 " since %s",
×
230
            TD_VID(pVnode), pPool->id, pPool->node.size, size, tstrerror(ERRNO));
231
    } else {
232
      vInfo("vgId:%d, buffer pool of id %d size changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pPool->id,
×
233
            pPool->node.size, size);
234

235
      vnodeBufPoolDestroy(pPool);
×
236
      pPool = pNewPool;
×
237
      pVnode->aBufPool[pPool->id] = pPool;
×
238
    }
239
  }
240

241
  // add to free list
242
  vDebug("vgId:%d, buffer pool %p of id %d is added to free list", TD_VID(pVnode), pPool, pPool->id);
20,922✔
243
  vnodeBufPoolReset(pPool);
20,922✔
244
  pPool->freeNext = pVnode->freeList;
20,920✔
245
  pVnode->freeList = pPool;
20,920✔
246
  (void)taosThreadCondSignal(&pVnode->poolNotEmpty);
20,920✔
247
}
20,921✔
248

249
void vnodeBufPoolUnRef(SVBufPool *pPool, bool proactive) {
32,909✔
250
  if (pPool == NULL) return;
32,909!
251

252
  SVnode *pVnode = pPool->pVnode;
32,909✔
253

254
  if (proactive) {
32,909!
255
    (void)taosThreadMutexLock(&pVnode->mutex);
32,909✔
256
  }
257

258
  if (atomic_sub_fetch_32(&pPool->nRef, 1) > 0) goto _exit;
32,909✔
259

260
  // remove from recycle queue or on-recycle position
261
  if (pVnode->onRecycle == pPool) {
509!
262
    pVnode->onRecycle = NULL;
×
263
  } else {
264
    if (pPool->recyclePrev) {
509!
265
      pPool->recyclePrev->recycleNext = pPool->recycleNext;
×
266
    } else {
267
      pVnode->recycleHead = pPool->recycleNext;
509✔
268
    }
269

270
    if (pPool->recycleNext) {
509!
271
      pPool->recycleNext->recyclePrev = pPool->recyclePrev;
×
272
    } else {
273
      pVnode->recycleTail = pPool->recyclePrev;
509✔
274
    }
275
    pPool->recyclePrev = pPool->recycleNext = NULL;
509✔
276
  }
277

278
  vnodeBufPoolAddToFreeList(pPool);
509✔
279

280
_exit:
32,908✔
281
  if (proactive) {
32,908!
282
    (void)taosThreadMutexUnlock(&pVnode->mutex);
32,908✔
283
  }
284
  return;
32,909✔
285
}
286

287
void vnodeBufPoolRegisterQuery(SVBufPool *pPool, SQueryNode *pQNode) {
860,595✔
288
  (void)taosThreadMutexLock(&pPool->mutex);
860,595✔
289

290
  pQNode->pNext = pPool->qList.pNext;
861,097✔
291
  pQNode->ppNext = &pPool->qList.pNext;
861,097✔
292
  pPool->qList.pNext->ppNext = &pQNode->pNext;
861,097✔
293
  pPool->qList.pNext = pQNode;
861,097✔
294
  pPool->nQuery++;
861,097✔
295

296
  (void)taosThreadMutexUnlock(&pPool->mutex);
861,097✔
297
}
861,084✔
298

299
void vnodeBufPoolDeregisterQuery(SVBufPool *pPool, SQueryNode *pQNode, bool proactive) {
860,857✔
300
  int32_t code = 0;
860,857✔
301

302
  if (proactive) {
860,857!
303
    (void)taosThreadMutexLock(&pPool->mutex);
860,943✔
304
  }
305

306
  pQNode->pNext->ppNext = pQNode->ppNext;
861,041✔
307
  *pQNode->ppNext = pQNode->pNext;
861,041✔
308
  pPool->nQuery--;
861,041✔
309

310
  if (proactive) {
861,041!
311
    (void)taosThreadMutexUnlock(&pPool->mutex);
861,119✔
312
  }
313
}
861,052✔
314

315
int32_t vnodeBufPoolRecycle(SVBufPool *pPool) {
×
316
  int32_t code = 0;
×
317

318
  SVnode *pVnode = pPool->pVnode;
×
319

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

322
  (void)taosThreadMutexLock(&pPool->mutex);
×
323

324
  SQueryNode *pNode = pPool->qList.pNext;
×
325
  while (pNode != &pPool->qList) {
×
326
    SQueryNode *pTNode = pNode->pNext;
×
327

328
    int32_t rc = pNode->reseek(pNode->pQHandle);
×
329
    if (rc == 0 || rc == TSDB_CODE_VND_QUERY_BUSY) {
×
330
      pNode = pTNode;
×
331
    } else {
332
      code = rc;
×
333
      goto _exit;
×
334
    }
335
  }
336

337
_exit:
×
338
  (void)taosThreadMutexUnlock(&pPool->mutex);
×
339
  return code;
×
340
}
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