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

taosdata / TDengine / #4473

08 Jul 2025 09:38AM UTC coverage: 62.922% (+0.7%) from 62.22%
#4473

push

travis-ci

web-flow
Merge pull request #31712 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

158525 of 321496 branches covered (49.31%)

Branch coverage included in aggregate %.

56 of 60 new or added lines in 13 files covered. (93.33%)

1333 existing lines in 67 files now uncovered.

245526 of 320647 relevant lines covered (76.57%)

17689640.25 hits per line

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

58.82
/source/util/src/tpagedbuf.c
1
#define _DEFAULT_SOURCE
2
#include "tpagedbuf.h"
3
#include "taoserror.h"
4
#include "tcompression.h"
5
#include "tlog.h"
6
#include "tsimplehash.h"
7

8
#define GET_PAYLOAD_DATA(_p)           ((char*)(_p)->pData + POINTER_BYTES)
9
#define BUF_PAGE_IN_MEM(_p)            ((_p)->pData != NULL)
10
#define CLEAR_BUF_PAGE_IN_MEM_FLAG(_p) ((_p)->pData = NULL)
11
#define HAS_DATA_IN_DISK(_p)           ((_p)->offset >= 0)
12
#define NO_IN_MEM_AVAILABLE_PAGES(_b)  (listNEles((_b)->lruList) >= (_b)->inMemPages)
13

14
typedef struct SPageDiskInfo {
15
  int64_t offset;
16
  int32_t length;
17
} SPageDiskInfo, SFreeListItem;
18

19
struct SPageInfo {
20
  SListNode* pn;  // point to list node struct. it is NULL when the page is evicted from the in-memory buffer
21
  void*      pData;
22
  int64_t    offset;
23
  int32_t    pageId;
24
  int32_t    length : 29;
25
  bool       used : 1;   // set current page is in used
26
  bool       dirty : 1;  // set current buffer page is dirty or not
27
};
28

29
struct SDiskbasedBuf {
30
  int32_t    numOfPages;
31
  int64_t    totalBufSize;
32
  uint64_t   fileSize;  // disk file size
33
  TdFilePtr  pFile;
34
  int32_t    allocateId;  // allocated page id
35
  char*      path;        // file path
36
  char*      prefix;      // file name prefix
37
  int32_t    pageSize;    // current used page size
38
  int32_t    inMemPages;  // numOfPages that are allocated in memory
39
  SList*     freePgList;  // free page list
40
  SArray*    pIdList;     // page id list
41
  SSHashObj* all;
42
  SList*     lruList;
43
  void*      emptyDummyIdList;  // dummy id list
44
  void*      assistBuf;         // assistant buffer for compress/decompress data
45
  SArray*    pFree;             // free area in file
46
  bool       comp;              // compressed before flushed to disk
47
  uint64_t   nextPos;           // next page flush position
48

49
  char*               id;           // for debug purpose
50
  bool                printStatis;  // Print statistics info when closing this buffer.
51
  SDiskbasedBufStatis statis;
52
};
53

54
static int32_t createDiskFile(SDiskbasedBuf* pBuf) {
9,343✔
55
  if (pBuf->path == NULL) {  // prepare the file name when needed it
9,343!
56
    char path[PATH_MAX] = {0};
9,343✔
57
    taosGetTmpfilePath(pBuf->prefix, "paged-buf", path);
9,343✔
58
    pBuf->path = taosStrdup(path);
9,343!
59
    if (pBuf->path == NULL) {
9,343!
60
      return terrno;
×
61
    }
62
  }
63

64
  pBuf->pFile =
9,343✔
65
      taosOpenFile(pBuf->path, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_READ | TD_FILE_TRUNC | TD_FILE_AUTO_DEL);
9,343✔
66
  if (pBuf->pFile == NULL) {
9,343!
67
    return terrno;
×
68
  }
69

70
  return TSDB_CODE_SUCCESS;
9,343✔
71
}
72

73
static char* doCompressData(void* data, int32_t srcSize, int32_t* dst, SDiskbasedBuf* pBuf) {  // do nothing
8,955,598✔
74
  if (!pBuf->comp) {
8,955,598!
75
    *dst = srcSize;
8,955,621✔
76
    return data;
8,955,621✔
77
  }
78

79
  *dst = tsCompressString(data, srcSize, 1, pBuf->assistBuf, srcSize, ONE_STAGE_COMP, NULL, 0);
×
80

81
  memcpy(data, pBuf->assistBuf, *dst);
×
82
  return data;
×
83
}
84

85
static int32_t doDecompressData(void* data, int32_t srcSize, int32_t* dst, SDiskbasedBuf* pBuf) {  // do nothing
8,005,346✔
86
  int32_t code = 0;
8,005,346✔
87
  if (!pBuf->comp) {
8,005,346!
88
    *dst = srcSize;
8,005,350✔
89
    return code;
8,005,350✔
90
  }
91

92
  *dst = tsDecompressString(data, srcSize, 1, pBuf->assistBuf, pBuf->pageSize, ONE_STAGE_COMP, NULL, 0);
×
93
  if (*dst > 0) {
×
94
    memcpy(data, pBuf->assistBuf, *dst);
×
95
  } else if (*dst < 0) {
×
96
    return terrno;
×
97
  }
98
  return code;
×
99
  ;
100
}
101

102
static uint64_t allocateNewPositionInFile(SDiskbasedBuf* pBuf, size_t size) {
8,642,323✔
103
  if (pBuf->pFree == NULL) {
8,642,323!
104
    return pBuf->nextPos;
×
105
  } else {
106
    int32_t offset = -1;
8,642,323✔
107

108
    size_t num = taosArrayGetSize(pBuf->pFree);
8,642,323✔
109
    for (int32_t i = 0; i < num; ++i) {
8,642,048!
110
      SFreeListItem* pi = taosArrayGet(pBuf->pFree, i);
×
111
      if (pi->length >= size) {
×
112
        offset = pi->offset;
×
113
        pi->offset += (int32_t)size;
×
114
        pi->length -= (int32_t)size;
×
115

116
        return offset;
×
117
      }
118
    }
119

120
    // no available recycle space, allocate new area in file
121
    return pBuf->nextPos;
8,642,048✔
122
  }
123
}
124

125
/**
126
 *   +--------------------------+-------------------+--------------+
127
 *   | PTR to SPageInfo (8bytes)| Payload (PageSize)| 2 Extra Bytes|
128
 *   +--------------------------+-------------------+--------------+
129
 * @param pBuf
130
 * @param pg
131
 * @return
132
 */
133

134
static FORCE_INLINE size_t getAllocPageSize(int32_t pageSize) { return pageSize + POINTER_BYTES + sizeof(SFilePage); }
39,746,493✔
135

136
static int32_t doFlushBufPageImpl(SDiskbasedBuf* pBuf, int64_t offset, const char* pData, int32_t size) {
8,955,212✔
137
  int64_t ret = taosLSeekFile(pBuf->pFile, offset, SEEK_SET);
8,955,212✔
138
  if (ret < 0) {
8,956,807!
139
    return terrno;
×
140
  }
141

142
  ret = (int32_t)taosWriteFile(pBuf->pFile, pData, size);
8,956,807✔
143
  if (ret != size) {
8,956,842!
144
    return terrno;
×
145
  }
146

147
  // extend the file
148
  if (pBuf->fileSize < offset + size) {
8,956,888✔
149
    pBuf->fileSize = offset + size;
8,643,627✔
150
  }
151

152
  pBuf->statis.flushBytes += size;
8,956,888✔
153
  pBuf->statis.flushPages += 1;
8,956,888✔
154

155
  return TSDB_CODE_SUCCESS;
8,956,888✔
156
}
157

158
static char* doFlushBufPage(SDiskbasedBuf* pBuf, SPageInfo* pg) {
14,424,411✔
159
  if (pg->pData == NULL || pg->used) {
14,424,411!
160
    uError("invalid params in paged buffer process when flushing buf to disk, %s", pBuf->id);
×
161
    terrno = TSDB_CODE_INVALID_PARA;
×
162
    return NULL;
×
163
  }
164

165
  int32_t size = pBuf->pageSize;
14,424,436✔
166
  int64_t offset = pg->offset;
14,424,436✔
167

168
  char* t = NULL;
14,424,436✔
169
  if ((!HAS_DATA_IN_DISK(pg)) || pg->dirty) {
14,424,436✔
170
    void* payload = GET_PAYLOAD_DATA(pg);
8,955,082✔
171
    t = doCompressData(payload, pBuf->pageSize + sizeof(SFilePage), &size, pBuf);
8,955,082✔
172
    if (size < 0) {
8,955,633!
173
      uError("failed to compress data when flushing data to disk, %s", pBuf->id);
×
174
      terrno = TSDB_CODE_INVALID_PARA;
×
175
      return NULL;
×
176
    }
177
  }
178

179
  // this page is flushed to disk for the first time
180
  if (pg->dirty) {
14,424,987✔
181
    if (!HAS_DATA_IN_DISK(pg)) {
8,955,596✔
182
      offset = allocateNewPositionInFile(pBuf, size);
8,642,379✔
183
      pBuf->nextPos += size;
8,642,022✔
184

185
      int32_t code = doFlushBufPageImpl(pBuf, offset, t, size);
8,642,022✔
186
      if (code != TSDB_CODE_SUCCESS) {
8,643,477!
187
        return NULL;
×
188
      }
189
    } else {
190
      // length becomes greater, current space is not enough, allocate new place, otherwise, do nothing
191
      if (pg->length < size) {
313,217!
192
        // 1. add current space to free list
193
        SPageDiskInfo dinfo = {.length = pg->length, .offset = offset};
×
194
        if (NULL == taosArrayPush(pBuf->pFree, &dinfo)) {
×
195
          return NULL;
×
196
        }
197

198
        // 2. allocate new position, and update the info
199
        offset = allocateNewPositionInFile(pBuf, size);
×
200
        pBuf->nextPos += size;
×
201
      }
202

203
      int32_t code = doFlushBufPageImpl(pBuf, offset, t, size);
313,217✔
204
      if (code != TSDB_CODE_SUCCESS) {
313,251!
205
        return NULL;
×
206
      }
207
    }
208
  } else {  // NOTE: the size may be -1, the this recycle page has not been flushed to disk yet.
209
    size = pg->length;
5,469,391✔
210
  }
211

212
  char* pDataBuf = pg->pData;
14,426,119✔
213
  memset(pDataBuf, 0, getAllocPageSize(pBuf->pageSize));
14,426,119✔
214

215
#ifdef BUF_PAGE_DEBUG
216
  uDebug("page_flush %p, pageId:%d, offset:%d", pDataBuf, pg->pageId, offset);
217
#endif
218

219
  pg->offset = offset;
14,426,119✔
220
  pg->length = size;  // on disk size
14,426,119✔
221
  return pDataBuf;
14,426,119✔
222
}
223

224
static char* flushBufPage(SDiskbasedBuf* pBuf, SPageInfo* pg) {
14,424,403✔
225
  int32_t ret = TSDB_CODE_SUCCESS;
14,424,403✔
226

227
  if (pBuf->pFile == NULL) {
14,424,403✔
228
    if ((ret = createDiskFile(pBuf)) != TSDB_CODE_SUCCESS) {
9,343!
229
      terrno = ret;
×
230
      return NULL;
×
231
    }
232
  }
233

234
  char* p = doFlushBufPage(pBuf, pg);
14,424,403✔
235
  CLEAR_BUF_PAGE_IN_MEM_FLAG(pg);
14,425,771✔
236

237
  pg->dirty = false;
14,425,771✔
238
  return p;
14,425,771✔
239
}
240

241
// load file block data in disk
242
static int32_t loadPageFromDisk(SDiskbasedBuf* pBuf, SPageInfo* pg) {
8,004,704✔
243
  if (pg->offset < 0 || pg->length <= 0) {
8,004,704!
244
    uError("failed to load buf page from disk, offset:%" PRId64 ", length:%d, %s", pg->offset, pg->length, pBuf->id);
×
245
    return TSDB_CODE_INVALID_PARA;
×
246
  }
247

248
  int64_t ret = taosLSeekFile(pBuf->pFile, pg->offset, SEEK_SET);
8,005,210✔
249
  if (ret < 0) {
8,005,331!
250
    ret = terrno;
×
251
    return ret;
×
252
  }
253

254
  void* pPage = (void*)GET_PAYLOAD_DATA(pg);
8,005,331✔
255
  ret = taosReadFile(pBuf->pFile, pPage, pg->length);
8,005,331✔
256
  if (ret != pg->length) {
8,005,374!
257
    ret = terrno;
×
258
    return ret;
×
259
  }
260

261
  pBuf->statis.loadBytes += pg->length;
8,005,374✔
262
  pBuf->statis.loadPages += 1;
8,005,374✔
263

264
  int32_t fullSize = 0;
8,005,374✔
265
  return doDecompressData(pPage, pg->length, &fullSize, pBuf);
8,005,374✔
266
}
267

268
static SPageInfo* registerNewPageInfo(SDiskbasedBuf* pBuf, int32_t pageId) {
31,761,192✔
269
  pBuf->numOfPages += 1;
31,761,192✔
270

271
  SPageInfo* ppi = taosMemoryMalloc(sizeof(SPageInfo));
31,761,192!
272
  if (ppi == NULL) {
31,766,580!
273
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
274
    return NULL;
×
275
  }
276

277
  ppi->pageId = pageId;
31,766,580✔
278
  ppi->pData = NULL;
31,766,580✔
279
  ppi->offset = -1;
31,766,580✔
280
  ppi->length = -1;
31,766,580✔
281
  ppi->used = true;
31,766,580✔
282
  ppi->pn = NULL;
31,766,580✔
283
  ppi->dirty = false;
31,766,580✔
284

285
  SPageInfo** pRet = taosArrayPush(pBuf->pIdList, &ppi);
31,766,580✔
286
  if (NULL == pRet) {
31,761,117!
287
    taosMemoryFree(ppi);
×
288
    return NULL;
×
289
  }
290
  return *pRet;
31,761,117✔
291
}
292

293
static SListNode* getEldestUnrefedPage(SDiskbasedBuf* pBuf) {
14,425,257✔
294
  SListIter iter = {0};
14,425,257✔
295
  tdListInitIter(pBuf->lruList, &iter, TD_LIST_BACKWARD);
14,425,257✔
296

297
  SListNode* pn = NULL;
14,425,294✔
298
  while ((pn = tdListNext(&iter)) != NULL) {
16,875,476!
299
    SPageInfo* pageInfo = *(SPageInfo**)pn->data;
16,875,811✔
300

301
    SPageInfo* p = *(SPageInfo**)(pageInfo->pData);
16,875,811✔
302

303
    if (!pageInfo->used) {
16,875,811✔
304
      break;
14,425,629✔
305
    }
306
  }
307

308
  return pn;
14,425,606✔
309
}
310

311
static char* evictBufPage(SDiskbasedBuf* pBuf) {
14,425,304✔
312
  SListNode* pn = getEldestUnrefedPage(pBuf);
14,425,304✔
313
  if (pn == NULL) {  // no available buffer pages now, return.
14,425,476!
314
    return NULL;
×
315
  }
316

317
  terrno = 0;
14,425,476✔
318
  pn = tdListPopNode(pBuf->lruList, pn);
14,425,410✔
319

320
  SPageInfo* d = *(SPageInfo**)pn->data;
14,423,791✔
321

322
  d->pn = NULL;
14,423,791✔
323
  taosMemoryFreeClear(pn);
14,423,791!
324

325
  return flushBufPage(pBuf, d);
14,424,361✔
326
}
327

328
static int32_t lruListPushFront(SList* pList, SPageInfo* pi) {
39,764,444✔
329
  int32_t code = tdListPrepend(pList, &pi);
39,764,444✔
330
  if (TSDB_CODE_SUCCESS != code) {
39,766,107!
331
    return code;
×
332
  }
333
  SListNode* front = tdListGetHead(pList);
39,766,107✔
334
  pi->pn = front;
39,761,722✔
335
  return TSDB_CODE_SUCCESS;
39,761,722✔
336
}
337

338
static void lruListMoveToFront(SList* pList, SPageInfo* pi) {
1,118,889,548✔
339
  pi->pn = tdListPopNode(pList, pi->pn);
1,118,889,548✔
340
  tdListPrependNode(pList, pi->pn);
1,118,290,401✔
341
}
1,117,635,067✔
342

343
static SPageInfo* getPageInfoFromPayload(void* page) {
1,625,699,967✔
344
  char* p = (char*)page - POINTER_BYTES;
1,625,699,967✔
345

346
  SPageInfo* ppi = ((SPageInfo**)p)[0];
1,625,699,967✔
347
  return ppi;
1,625,699,967✔
348
}
349

350
int32_t createDiskbasedBuf(SDiskbasedBuf** pBuf, int32_t pagesize, int64_t inMemBufSize, const char* id,
10,487,796✔
351
                           const char* dir) {
352
  int32_t code = 0;
10,487,796✔
353
  *pBuf = NULL;
10,487,796✔
354
  SDiskbasedBuf* pPBuf = taosMemoryCalloc(1, sizeof(SDiskbasedBuf));
10,487,796!
355
  if (pPBuf == NULL) {
10,492,234!
356
    code = terrno;
×
357
    goto _error;
×
358
  }
359

360
  pPBuf->pageSize = pagesize;
10,492,234✔
361
  pPBuf->numOfPages = 0;  // all pages are in buffer in the first place
10,492,234✔
362
  pPBuf->totalBufSize = 0;
10,492,234✔
363
  pPBuf->allocateId = -1;
10,492,234✔
364
  pPBuf->pFile = NULL;
10,492,234✔
365
  pPBuf->id = taosStrdup(id);
10,492,234!
366
  if (id != NULL && pPBuf->id == NULL) {
10,492,955!
367
    code = terrno;
×
368
    goto _error;
×
369
  }
370
  pPBuf->fileSize = 0;
10,492,955✔
371
  pPBuf->pFree = taosArrayInit(4, sizeof(SFreeListItem));
10,492,955✔
372
  pPBuf->freePgList = tdListNew(POINTER_BYTES);
10,495,244✔
373
  if (pPBuf->pFree == NULL || pPBuf->freePgList == NULL) {
10,492,761!
374
    code = terrno;
×
375
    goto _error;
×
376
  }
377

378
  // at least more than 2 pages must be in memory
379
  if (inMemBufSize < pagesize * 2) {
10,494,026✔
380
    inMemBufSize = pagesize * 2;
4,332✔
381
  }
382

383
  pPBuf->inMemPages = inMemBufSize / pagesize;  // maximum allowed pages, it is a soft limit.
10,494,026✔
384
  pPBuf->lruList = tdListNew(POINTER_BYTES);
10,494,026✔
385
  if (pPBuf->lruList == NULL) {
10,491,752!
386
    code = terrno;
×
387
    goto _error;
×
388
  }
389

390
  // init id hash table
391
  _hash_fn_t fn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT);
10,491,752✔
392
  pPBuf->pIdList = taosArrayInit(4, POINTER_BYTES);
10,492,402✔
393
  if (pPBuf->pIdList == NULL) {
10,496,229!
394
    code = terrno;
×
395
    goto _error;
×
396
  }
397

398
  pPBuf->all = tSimpleHashInit(64, fn);
10,496,229✔
399
  if (pPBuf->all == NULL) {
10,490,272!
400
    code = terrno;
×
401
    goto _error;
×
402
  }
403

404
  pPBuf->prefix = (char*)dir;
10,490,272✔
405
  pPBuf->emptyDummyIdList = taosArrayInit(1, sizeof(int32_t));
10,490,272✔
406
  if (pPBuf->emptyDummyIdList == NULL) {
10,496,701!
UNCOV
407
    code = terrno;
×
408
    goto _error;
×
409
  }
410

411
  //  qDebug("QInfo:0x%"PRIx64 ", create resBuf for output, page size:%d, inmem buf pages:%d, file:%s", qId,
412
  //  pPBuf->pageSize, pPBuf->inMemPages, pPBuf->path);
413

414
  *pBuf = pPBuf;
10,496,838✔
415
  return TSDB_CODE_SUCCESS;
10,496,838✔
416

417
_error:
×
418
  destroyDiskbasedBuf(pPBuf);
×
419
  *pBuf = NULL;
×
420
  return code;
×
421
}
422

423
static char* doExtractPage(SDiskbasedBuf* pBuf) {
39,745,735✔
424
  char* availablePage = NULL;
39,745,735✔
425
  if (NO_IN_MEM_AVAILABLE_PAGES(pBuf)) {
39,745,735✔
426
    availablePage = evictBufPage(pBuf);
14,425,361✔
427
    if (availablePage == NULL) {
14,425,777!
428
      uWarn("no available buf pages, current:%d, max:%d, reason: %s, %s", listNEles(pBuf->lruList), pBuf->inMemPages,
×
429
            terrstr(), pBuf->id)
430
    }
431
  } else {
432
    availablePage =
50,658,200✔
433
        taosMemoryCalloc(1, getAllocPageSize(pBuf->pageSize));  // add extract bytes in case of zipped buffer increased.
50,640,748!
434
    if (availablePage == NULL) {
25,337,826!
435
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
436
    }
437
  }
438

439
  return availablePage;
39,763,603✔
440
}
441

442
void* getNewBufPage(SDiskbasedBuf* pBuf, int32_t* pageId) {
31,740,955✔
443
  pBuf->statis.getPages += 1;
31,740,955✔
444

445
  char* availablePage = doExtractPage(pBuf);
31,740,955✔
446
  if (availablePage == NULL) {
31,761,352!
447
    return NULL;
×
448
  }
449

450
  SPageInfo* pi = NULL;
31,761,352✔
451
  int32_t    code = 0;
31,761,352✔
452
  if (listNEles(pBuf->freePgList) != 0) {
31,761,352!
453
    SListNode* pItem = tdListPopHead(pBuf->freePgList);
×
454
    pi = *(SPageInfo**)pItem->data;
×
455
    pi->used = true;
×
456
    *pageId = pi->pageId;
×
457
    taosMemoryFreeClear(pItem);
×
458
    code = lruListPushFront(pBuf->lruList, pi);
×
459
    if (TSDB_CODE_SUCCESS != code) {
×
460
      taosMemoryFree(pi);
889!
461
      taosMemoryFree(availablePage);
×
462
      terrno = code;
×
463
      return NULL;
×
464
    }
465
  } else {  // create a new pageinfo
466
    // register new id in this group
467
    *pageId = (++pBuf->allocateId);
31,761,352✔
468

469
    // register page id info
470
    pi = registerNewPageInfo(pBuf, *pageId);
31,761,352✔
471
    if (pi == NULL) {
31,759,120!
472
      taosMemoryFree(availablePage);
×
473
      return NULL;
×
474
    }
475

476
    // add to hash map
477
    int32_t code = tSimpleHashPut(pBuf->all, pageId, sizeof(int32_t), &pi, POINTER_BYTES);
31,759,120✔
478

479
    if (TSDB_CODE_SUCCESS == code) {
31,763,893!
480
      // add to LRU list
481
      code = lruListPushFront(pBuf->lruList, pi);
31,764,067✔
482
    }
483
    if (TSDB_CODE_SUCCESS == code) {
31,758,720!
484
      pBuf->totalBufSize += pBuf->pageSize;
31,758,720✔
485
    } else {
486
      taosMemoryFree(availablePage);
×
487
      SPageInfo **pLast = taosArrayPop(pBuf->pIdList);
×
488
      int32_t ret = tSimpleHashRemove(pBuf->all, pageId, sizeof(int32_t));
×
489
      if (ret != TSDB_CODE_SUCCESS) {
×
490
        uError("%s failed to clear pageId %d from buf hash-set since %s", __func__, *pageId, tstrerror(ret));
×
491
      }
492
      taosMemoryFree(pi);
×
493
      terrno = code;
×
494
      return NULL;
×
495
    }
496
  }
497

498
  pi->pData = availablePage;
31,757,831✔
499

500
  ((void**)pi->pData)[0] = pi;
31,757,831✔
501
#ifdef BUF_PAGE_DEBUG
502
  uDebug("page_getNewBufPage , pi->pData:%p, pageId:%d, offset:%" PRId64, pi->pData, pi->pageId, pi->offset);
503
#endif
504

505
  return (void*)(GET_PAYLOAD_DATA(pi));
31,757,831✔
506
}
507

508
void* getBufPage(SDiskbasedBuf* pBuf, int32_t id) {
1,243,076,984✔
509
  if (id < 0) {
1,243,076,984!
510
    terrno = TSDB_CODE_INVALID_PARA;
×
511
    uError("invalid page id:%d, %s", id, pBuf->id);
×
512
    return NULL;
×
513
  }
514

515
  pBuf->statis.getPages += 1;
1,243,076,984✔
516

517
  SPageInfo** pi = tSimpleHashGet(pBuf->all, &id, sizeof(int32_t));
1,243,076,984✔
518
  if (pi == NULL || *pi == NULL) {
1,248,795,969!
519
    uError("failed to locate the buffer page:%d, %s", id, pBuf->id);
×
520
    terrno = TSDB_CODE_INVALID_PARA;
×
521
    return NULL;
×
522
  }
523

524
  if (BUF_PAGE_IN_MEM(*pi)) {  // it is in memory
1,249,552,421✔
525
    // no need to update the LRU list if only one page exists
526
    if (pBuf->numOfPages == 1) {
1,241,176,231✔
527
      (*pi)->used = true;
124,593,262✔
528
      return (void*)(GET_PAYLOAD_DATA(*pi));
124,593,262✔
529
    }
530

531
    SPageInfo** pInfo = (SPageInfo**)((*pi)->pn->data);
1,116,582,969✔
532
    if (*pInfo != *pi) {
1,116,582,969!
533
      terrno = TSDB_CODE_APP_ERROR;
×
534
      uError("inconsistently data in paged buffer, pInfo:%p, pi:%p, %s", *pInfo, *pi, pBuf->id);
×
535
      return NULL;
×
536
    }
537

538
    lruListMoveToFront(pBuf->lruList, (*pi));
1,116,582,969✔
539
    (*pi)->used = true;
1,117,629,217✔
540

541
#ifdef BUF_PAGE_DEBUG
542
    uDebug("page_getBufPage1 pageId:%d, offset:%" PRId64, (*pi)->pageId, (*pi)->offset);
543
#endif
544
    return (void*)(GET_PAYLOAD_DATA(*pi));
1,117,629,217✔
545
  } else {  // not in memory
546

547
    (*pi)->pData = doExtractPage(pBuf);
8,376,190✔
548

549
    // failed to evict buffer page, return with error code.
550
    if ((*pi)->pData == NULL) {
8,004,693!
551
      return NULL;
×
552
    }
553

554
    // set the ptr to the new SPageInfo
555
    ((void**)((*pi)->pData))[0] = (*pi);
8,004,693✔
556

557
    int32_t code = lruListPushFront(pBuf->lruList, *pi);
8,004,693✔
558
    if (TSDB_CODE_SUCCESS != code) {
8,004,738!
559
      taosMemoryFree((*pi)->pData);
×
560
      (*pi)->pData = NULL;
×
561
      terrno = code;
×
562
      return NULL;
×
563
    }
564
    (*pi)->used = true;
8,004,738✔
565

566
    // some data has been flushed to disk, and needs to be loaded into buffer again.
567
    if (HAS_DATA_IN_DISK(*pi)) {
8,004,738✔
568
      int32_t code = loadPageFromDisk(pBuf, *pi);
8,004,713✔
569
      if (code != 0) {
8,005,293✔
570
        taosMemoryFree((*pi)->pData);
27!
571
        (*pi)->pData = NULL;
×
572
        terrno = code;
×
573
        return NULL;
×
574
      }
575
    }
576
#ifdef BUF_PAGE_DEBUG
577
    uDebug("page_getBufPage2 pageId:%d, offset:%" PRId64, (*pi)->pageId, (*pi)->offset);
578
#endif
579
    return (void*)(GET_PAYLOAD_DATA(*pi));
8,005,291✔
580
  }
581
}
582

583
void releaseBufPage(SDiskbasedBuf* pBuf, void* page) {
1,001,867,524✔
584
  if (page == NULL) {
1,001,867,524!
585
    return;
×
586
  }
587

588
  SPageInfo* ppi = getPageInfoFromPayload(page);
1,001,867,524✔
589
  releaseBufPageInfo(pBuf, ppi);
1,001,912,148✔
590
}
591

592
void releaseBufPageInfo(SDiskbasedBuf* pBuf, SPageInfo* pi) {
1,002,548,672✔
593
#ifdef BUF_PAGE_DEBUG
594
  uDebug("page_releaseBufPageInfo pageId:%d, used:%d, offset:%" PRId64, pi->pageId, pi->used, pi->offset);
595
#endif
596

597
  if (pi == NULL) {
1,002,548,672!
598
    return;
×
599
  }
600

601
  if (pi->pData == NULL) {
1,002,548,672!
602
    uError("pi->pData (page data) is null");
×
603
    return;
×
604
  }
605

606
  pi->used = false;
1,002,548,672✔
607
  pBuf->statis.releasePages += 1;
1,002,548,672✔
608
}
609

610
size_t getTotalBufSize(const SDiskbasedBuf* pBuf) { return (size_t)pBuf->totalBufSize; }
1✔
611

612
SArray* getDataBufPagesIdList(SDiskbasedBuf* pBuf) { return pBuf->pIdList; }
3✔
613

614
void destroyDiskbasedBuf(SDiskbasedBuf* pBuf) {
12,838,938✔
615
  if (pBuf == NULL) {
12,838,938✔
616
    return;
2,340,529✔
617
  }
618

619
  dBufPrintStatis(pBuf);
10,498,409✔
620

621
  bool needRemoveFile = false;
10,499,080✔
622
  if (pBuf->pFile != NULL) {
10,499,080✔
623
    needRemoveFile = true;
9,343✔
624
    uDebug(
9,343✔
625
        "Paged buffer closed, total:%.2f Kb (%d Pages), inmem size:%.2f Kb (%d Pages), file size:%.2f Kb, page "
626
        "size:%.2f Kb, %s",
627
        pBuf->totalBufSize / 1024.0, pBuf->numOfPages, listNEles(pBuf->lruList) * pBuf->pageSize / 1024.0,
628
        listNEles(pBuf->lruList), pBuf->fileSize / 1024.0, pBuf->pageSize / 1024.0f, pBuf->id);
629

630
    int32_t code = taosCloseFile(&pBuf->pFile);
9,343✔
631
    if (TSDB_CODE_SUCCESS != code) {
9,343!
632
      uDebug("WARNING tPage failed to close file when destroy disk basebuf: %s", pBuf->path);
×
633
    }
634
  } else {
635
    uDebug("Paged buffer closed, total:%.2f Kb, no file created, %s", pBuf->totalBufSize / 1024.0, pBuf->id);
10,489,737✔
636
  }
637

638
  // print the statistics information
639
  {
640
    SDiskbasedBufStatis* ps = &pBuf->statis;
10,498,721✔
641
    if (ps->loadPages == 0) {
10,498,721✔
642
      uDebug("Get/Release pages:%d/%d, flushToDisk:%.2f Kb (%d Pages), loadFromDisk:%.2f Kb (%d Pages)", ps->getPages,
10,489,390✔
643
             ps->releasePages, ps->flushBytes / 1024.0f, ps->flushPages, ps->loadBytes / 1024.0f, ps->loadPages);
644
    } else {
645
      uDebug(
9,331✔
646
          "Get/Release pages:%d/%d, flushToDisk:%.2f Kb (%d Pages), loadFromDisk:%.2f Kb (%d Pages), avgPgSize:%.2f Kb",
647
          ps->getPages, ps->releasePages, ps->flushBytes / 1024.0f, ps->flushPages, ps->loadBytes / 1024.0f,
648
          ps->loadPages, ps->loadBytes / (1024.0 * ps->loadPages));
649
    }
650
  }
651

652
  if (needRemoveFile) {
10,498,721✔
653
    int32_t ret = taosRemoveFile(pBuf->path);
9,343✔
654
    if (ret != 0) {  // print the error and discard this error info
9,343!
655
      uDebug("WARNING tPage remove file failed. path=%s, code:%s", pBuf->path, strerror(ERRNO));
9,343✔
656
    }
657
  }
658

659
  taosMemoryFreeClear(pBuf->path);
10,497,846!
660

661
  size_t n = taosArrayGetSize(pBuf->pIdList);
10,497,846✔
662
  for (int32_t i = 0; i < n; ++i) {
41,062,898✔
663
    SPageInfo* pi = taosArrayGetP(pBuf->pIdList, i);
30,562,235✔
664
    taosMemoryFreeClear(pi->pData);
30,547,592!
665
    taosMemoryFreeClear(pi);
30,565,741!
666
  }
667

668
  taosArrayDestroy(pBuf->pIdList);
10,500,663✔
669

670
  pBuf->lruList = tdListFree(pBuf->lruList);
10,499,383✔
671
  pBuf->freePgList = tdListFree(pBuf->freePgList);
10,500,175✔
672

673
  taosArrayDestroy(pBuf->emptyDummyIdList);
10,500,199✔
674
  taosArrayDestroy(pBuf->pFree);
10,500,135✔
675

676
  tSimpleHashCleanup(pBuf->all);
10,500,250✔
677

678
  taosMemoryFreeClear(pBuf->id);
10,500,372!
679
  taosMemoryFreeClear(pBuf->assistBuf);
10,500,287!
680
  taosMemoryFreeClear(pBuf);
10,500,287!
681
}
682

683
SPageInfo* getLastPageInfo(SArray* pList) {
×
684
  size_t     size = taosArrayGetSize(pList);
×
685
  SPageInfo* pPgInfo = taosArrayGetP(pList, size - 1);
×
686
  return pPgInfo;
×
687
}
688

689
int32_t getPageId(const SPageInfo* pPgInfo) { return pPgInfo->pageId; }
×
690

691
int32_t getBufPageSize(const SDiskbasedBuf* pBuf) { return pBuf->pageSize; }
611,758,129✔
692

693
int32_t getNumOfInMemBufPages(const SDiskbasedBuf* pBuf) { return pBuf->inMemPages; }
2,052,201✔
694

695
bool isAllDataInMemBuf(const SDiskbasedBuf* pBuf) { return pBuf->fileSize == 0; }
×
696

697
void setBufPageDirty(void* pPage, bool dirty) {
639,568,798✔
698
  SPageInfo* ppi = getPageInfoFromPayload(pPage);
639,568,798✔
699
  ppi->dirty = dirty;
639,474,004✔
700
}
639,474,004✔
701

702
int32_t setBufPageCompressOnDisk(SDiskbasedBuf* pBuf, bool comp) {
×
703
  pBuf->comp = comp;
×
704
  if (comp && (pBuf->assistBuf == NULL)) {
×
705
    pBuf->assistBuf = taosMemoryMalloc(pBuf->pageSize + 2);  // EXTRA BYTES
×
706
    if (pBuf->assistBuf) {
×
707
      return terrno;
×
708
    }
709
  }
710
  return TSDB_CODE_SUCCESS;
×
711
}
712

713
int32_t dBufSetBufPageRecycled(SDiskbasedBuf* pBuf, void* pPage) {
×
714
  SPageInfo* ppi = getPageInfoFromPayload(pPage);
×
715

716
  int32_t code = tdListAppend(pBuf->freePgList, &ppi);
×
717
  if (TSDB_CODE_SUCCESS != code) {
×
718
    return code;
×
719
  }
720

721
  ppi->used = false;
×
722
  ppi->dirty = false;
×
723

724
  // add this pageinfo into the free page info list
725
  SListNode* pNode = tdListPopNode(pBuf->lruList, ppi->pn);
×
726
  taosMemoryFreeClear(ppi->pData);
×
727
  taosMemoryFreeClear(pNode);
×
728
  ppi->pn = NULL;
×
729
  return TSDB_CODE_SUCCESS;
×
730
}
731

732
void dBufSetPrintInfo(SDiskbasedBuf* pBuf) { pBuf->printStatis = true; }
3,404,567✔
733

734
SDiskbasedBufStatis getDBufStatis(const SDiskbasedBuf* pBuf) { return pBuf->statis; }
2,181,184✔
735

736
void dBufPrintStatis(const SDiskbasedBuf* pBuf) {
10,498,255✔
737
  if (!pBuf->printStatis) {
10,498,255✔
738
    return;
7,094,025✔
739
  }
740

741
  const SDiskbasedBufStatis* ps = &pBuf->statis;
3,404,230✔
742

743
#if 0
744
  printf(
745
      "Paged buffer closed, total:%.2f Kb (%d Pages), inmem size:%.2f Kb (%d Pages), file size:%.2f Kb, page size:%.2f "
746
      "Kb, %s\n",
747
      pBuf->totalBufSize / 1024.0, pBuf->numOfPages, listNEles(pBuf->lruList) * pBuf->pageSize / 1024.0,
748
      listNEles(pBuf->lruList), pBuf->fileSize / 1024.0, pBuf->pageSize / 1024.0f, pBuf->id);
749
#endif
750

751
  if (ps->loadPages > 0) {
3,404,230✔
752
    (void)printf(
468✔
753
        "Get/Release pages:%d/%d, flushToDisk:%.2f Kb (%d Pages), loadFromDisk:%.2f Kb (%d Pages), avgPageSize:%.2f "
754
        "Kb\n",
755
        ps->getPages, ps->releasePages, ps->flushBytes / 1024.0f, ps->flushPages, ps->loadBytes / 1024.0f,
468✔
756
        ps->loadPages, ps->loadBytes / (1024.0 * ps->loadPages));
468✔
757
  } else {
758
    // printf("no page loaded\n");
759
  }
760
}
761

762
void clearDiskbasedBuf(SDiskbasedBuf* pBuf) {
26,189✔
763
  size_t n = taosArrayGetSize(pBuf->pIdList);
26,189✔
764
  for (int32_t i = 0; i < n; ++i) {
1,230,604✔
765
    SPageInfo* pi = taosArrayGetP(pBuf->pIdList, i);
1,204,416✔
766
    taosMemoryFreeClear(pi->pData);
1,204,327!
767
    taosMemoryFreeClear(pi);
1,204,456✔
768
  }
769

770
  taosArrayClear(pBuf->pIdList);
26,188✔
771

772
  tdListEmpty(pBuf->lruList);
26,189✔
773
  tdListEmpty(pBuf->freePgList);
26,190✔
774

775
  taosArrayClear(pBuf->emptyDummyIdList);
26,188✔
776
  taosArrayClear(pBuf->pFree);
26,188✔
777

778
  tSimpleHashClear(pBuf->all);
26,187✔
779

780
  pBuf->numOfPages = 0;  // all pages are in buffer in the first place
26,187✔
781
  pBuf->totalBufSize = 0;
26,187✔
782
  pBuf->allocateId = -1;
26,187✔
783
  pBuf->fileSize = 0;
26,187✔
784
}
26,187✔
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