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

taosdata / TDengine / #3524

08 Nov 2024 04:27AM UTC coverage: 60.898% (+5.0%) from 55.861%
#3524

push

travis-ci

web-flow
Merge pull request #28647 from taosdata/fix/3.0/TD-32519_drop_ctb

fix TD-32519 drop child table with tsma caused crash

118687 of 248552 branches covered (47.75%)

Branch coverage included in aggregate %.

286 of 337 new or added lines in 18 files covered. (84.87%)

9647 existing lines in 190 files now uncovered.

199106 of 273291 relevant lines covered (72.85%)

15236719.35 hits per line

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

62.42
/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) {
10,881✔
55
  if (pBuf->path == NULL) {  // prepare the file name when needed it
10,881!
56
    char path[PATH_MAX] = {0};
10,881✔
57
    taosGetTmpfilePath(pBuf->prefix, "paged-buf", path);
10,881✔
58
    pBuf->path = taosStrdup(path);
10,881✔
59
    if (pBuf->path == NULL) {
10,881!
60
      return terrno;
×
61
    }
62
  }
63

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

70
  return TSDB_CODE_SUCCESS;
10,881✔
71
}
72

73
static char* doCompressData(void* data, int32_t srcSize, int32_t* dst, SDiskbasedBuf* pBuf) {  // do nothing
10,013,399✔
74
  if (!pBuf->comp) {
10,013,399!
75
    *dst = srcSize;
10,013,599✔
76
    return data;
10,013,599✔
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
9,573,591✔
86
  int32_t code = 0;
9,573,591✔
87
  if (!pBuf->comp) {
9,573,591!
88
    *dst = srcSize;
9,573,656✔
89
    return code;
9,573,656✔
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) {
9,579,206✔
103
  if (pBuf->pFree == NULL) {
9,579,206!
104
    return pBuf->nextPos;
×
105
  } else {
106
    int32_t offset = -1;
9,579,206✔
107

108
    size_t num = taosArrayGetSize(pBuf->pFree);
9,579,206✔
109
    for (int32_t i = 0; i < num; ++i) {
9,578,977!
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;
9,578,977✔
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); }
41,517,984✔
135

136
static int32_t doFlushBufPageImpl(SDiskbasedBuf* pBuf, int64_t offset, const char* pData, int32_t size) {
10,013,227✔
137
  int64_t ret = taosLSeekFile(pBuf->pFile, offset, SEEK_SET);
10,013,227✔
138
  if (ret < 0) {
10,015,255!
139
    return terrno;
×
140
  }
141

142
  ret = (int32_t)taosWriteFile(pBuf->pFile, pData, size);
10,015,255✔
143
  if (ret != size) {
10,015,183!
144
    return terrno;
×
145
  }
146

147
  // extend the file
148
  if (pBuf->fileSize < offset + size) {
10,015,269✔
149
    pBuf->fileSize = offset + size;
9,580,901✔
150
  }
151

152
  pBuf->statis.flushBytes += size;
10,015,269✔
153
  pBuf->statis.flushPages += 1;
10,015,269✔
154

155
  return TSDB_CODE_SUCCESS;
10,015,269✔
156
}
157

158
static char* doFlushBufPage(SDiskbasedBuf* pBuf, SPageInfo* pg) {
15,355,264✔
159
  if (pg->pData == NULL || pg->used) {
15,355,264!
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;
15,355,811✔
166
  int64_t offset = pg->offset;
15,355,811✔
167

168
  char* t = NULL;
15,355,811✔
169
  if ((!HAS_DATA_IN_DISK(pg)) || pg->dirty) {
15,355,811✔
170
    void* payload = GET_PAYLOAD_DATA(pg);
10,012,110✔
171
    t = doCompressData(payload, pBuf->pageSize + sizeof(SFilePage), &size, pBuf);
10,012,110✔
172
    if (size < 0) {
10,013,577!
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) {
15,357,278✔
181
    if (!HAS_DATA_IN_DISK(pg)) {
10,013,546✔
182
      offset = allocateNewPositionInFile(pBuf, size);
9,579,238✔
183
      pBuf->nextPos += size;
9,578,950✔
184

185
      int32_t code = doFlushBufPageImpl(pBuf, offset, t, size);
9,578,950✔
186
      if (code != TSDB_CODE_SUCCESS) {
9,580,345!
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) {
434,308!
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);
434,308✔
204
      if (code != TSDB_CODE_SUCCESS) {
434,359!
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,343,732✔
210
  }
211

212
  char* pDataBuf = pg->pData;
15,358,436✔
213
  memset(pDataBuf, 0, getAllocPageSize(pBuf->pageSize));
15,358,436✔
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;
15,358,436✔
220
  pg->length = size;  // on disk size
15,358,436✔
221
  return pDataBuf;
15,358,436✔
222
}
223

224
static char* flushBufPage(SDiskbasedBuf* pBuf, SPageInfo* pg) {
15,355,176✔
225
  int32_t ret = TSDB_CODE_SUCCESS;
15,355,176✔
226

227
  if (pBuf->pFile == NULL) {
15,355,176✔
228
    if ((ret = createDiskFile(pBuf)) != TSDB_CODE_SUCCESS) {
10,881!
229
      terrno = ret;
×
230
      return NULL;
×
231
    }
232
  }
233

234
  char* p = doFlushBufPage(pBuf, pg);
15,355,176✔
235
  CLEAR_BUF_PAGE_IN_MEM_FLAG(pg);
15,356,777✔
236

237
  pg->dirty = false;
15,356,777✔
238
  return p;
15,356,777✔
239
}
240

241
// load file block data in disk
242
static int32_t loadPageFromDisk(SDiskbasedBuf* pBuf, SPageInfo* pg) {
9,571,618✔
243
  if (pg->offset < 0 || pg->length <= 0) {
9,571,618!
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);
9,573,275✔
249
  if (ret < 0) {
9,573,674!
250
    ret = terrno;
×
251
    return ret;
×
252
  }
253

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

261
  pBuf->statis.loadBytes += pg->length;
9,573,709✔
262
  pBuf->statis.loadPages += 1;
9,573,709✔
263

264
  int32_t fullSize = 0;
9,573,709✔
265
  return doDecompressData(pPage, pg->length, &fullSize, pBuf);
9,573,709✔
266
}
267

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

271
  SPageInfo* ppi = taosMemoryMalloc(sizeof(SPageInfo));
31,963,956✔
272
  if (ppi == NULL) {
31,985,795!
UNCOV
273
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
274
    return NULL;
×
275
  }
276

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

285
  SPageInfo** pRet = taosArrayPush(pBuf->pIdList, &ppi);
31,985,795✔
286
  if (NULL == pRet) {
31,982,215!
UNCOV
287
    taosMemoryFree(ppi);
×
UNCOV
288
    return NULL;
×
289
  }
290
  return *pRet;
31,982,215✔
291
}
292

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

297
  SListNode* pn = NULL;
15,356,805✔
298
  while ((pn = tdListNext(&iter)) != NULL) {
18,335,764✔
299
    SPageInfo* pageInfo = *(SPageInfo**)pn->data;
18,336,084✔
300

301
    SPageInfo* p = *(SPageInfo**)(pageInfo->pData);
18,336,084✔
302

303
    if (!pageInfo->used) {
18,336,084✔
304
      break;
15,357,125✔
305
    }
306
  }
307

308
  return pn;
15,357,138✔
309
}
310

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

317
  terrno = 0;
15,356,991✔
318
  pn = tdListPopNode(pBuf->lruList, pn);
15,356,852✔
319

320
  SPageInfo* d = *(SPageInfo**)pn->data;
15,353,639✔
321

322
  d->pn = NULL;
15,353,639✔
323
  taosMemoryFreeClear(pn);
15,353,639!
324

325
  return flushBufPage(pBuf, d);
15,354,431✔
326
}
327

328
static int32_t lruListPushFront(SList* pList, SPageInfo* pi) {
41,551,073✔
329
  int32_t code = tdListPrepend(pList, &pi);
41,551,073✔
330
  if (TSDB_CODE_SUCCESS != code) {
41,553,471!
UNCOV
331
    return code;
×
332
  }
333
  SListNode* front = tdListGetHead(pList);
41,553,471✔
334
  pi->pn = front;
41,549,041✔
335
  return TSDB_CODE_SUCCESS;
41,549,041✔
336
}
337

338
static void lruListMoveToFront(SList* pList, SPageInfo* pi) {
1,264,153,915✔
339
  pi->pn = tdListPopNode(pList, pi->pn);
1,264,153,915✔
340
  tdListPrependNode(pList, pi->pn);
1,263,388,858✔
341
}
1,262,743,250✔
342

343
static SPageInfo* getPageInfoFromPayload(void* page) {
1,840,923,864✔
344
  char* p = (char*)page - POINTER_BYTES;
1,840,923,864✔
345

346
  SPageInfo* ppi = ((SPageInfo**)p)[0];
1,840,923,864✔
347
  return ppi;
1,840,923,864✔
348
}
349

350
int32_t createDiskbasedBuf(SDiskbasedBuf** pBuf, int32_t pagesize, int32_t inMemBufSize, const char* id,
9,111,064✔
351
                           const char* dir) {
352
  *pBuf = NULL;
9,111,064✔
353
  SDiskbasedBuf* pPBuf = taosMemoryCalloc(1, sizeof(SDiskbasedBuf));
9,111,064✔
354
  if (pPBuf == NULL) {
9,114,737!
UNCOV
355
    goto _error;
×
356
  }
357

358
  pPBuf->pageSize = pagesize;
9,114,737✔
359
  pPBuf->numOfPages = 0;  // all pages are in buffer in the first place
9,114,737✔
360
  pPBuf->totalBufSize = 0;
9,114,737✔
361
  pPBuf->allocateId = -1;
9,114,737✔
362
  pPBuf->pFile = NULL;
9,114,737✔
363
  pPBuf->id = taosStrdup(id);
9,114,737✔
364
  if (id != NULL && pPBuf->id == NULL) {
9,116,949!
UNCOV
365
    goto _error;
×
366
  }
367
  pPBuf->fileSize = 0;
9,116,949✔
368
  pPBuf->pFree = taosArrayInit(4, sizeof(SFreeListItem));
9,116,949✔
369
  pPBuf->freePgList = tdListNew(POINTER_BYTES);
9,119,617✔
370
  if (pPBuf->pFree == NULL || pPBuf->freePgList == NULL) {
9,116,808!
UNCOV
371
    goto _error;
×
372
  }
373

374
  // at least more than 2 pages must be in memory
375
  if (inMemBufSize < pagesize * 2) {
9,116,808✔
376
    inMemBufSize = pagesize * 2;
4,324✔
377
  }
378

379
  pPBuf->inMemPages = inMemBufSize / pagesize;  // maximum allowed pages, it is a soft limit.
9,116,808✔
380
  pPBuf->lruList = tdListNew(POINTER_BYTES);
9,116,808✔
381
  if (pPBuf->lruList == NULL) {
9,117,779!
UNCOV
382
    goto _error;
×
383
  }
384

385
  // init id hash table
386
  _hash_fn_t fn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT);
9,117,779✔
387
  pPBuf->pIdList = taosArrayInit(4, POINTER_BYTES);
9,116,808✔
388
  if (pPBuf->pIdList == NULL) {
9,119,882!
UNCOV
389
    goto _error;
×
390
  }
391

392
  pPBuf->all = tSimpleHashInit(64, fn);
9,119,882✔
393
  if (pPBuf->all == NULL) {
9,113,533!
UNCOV
394
    goto _error;
×
395
  }
396

397
  pPBuf->prefix = (char*)dir;
9,113,533✔
398
  pPBuf->emptyDummyIdList = taosArrayInit(1, sizeof(int32_t));
9,113,533✔
399
  if (pPBuf->emptyDummyIdList == NULL) {
9,120,155!
UNCOV
400
    goto _error;
×
401
  }
402

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

406
  *pBuf = pPBuf;
9,120,155✔
407
  return TSDB_CODE_SUCCESS;
9,120,155✔
408

UNCOV
409
_error:
×
UNCOV
410
  destroyDiskbasedBuf(pPBuf);
×
UNCOV
411
  *pBuf = NULL;
×
UNCOV
412
  return TSDB_CODE_OUT_OF_MEMORY;
×
413
}
414

415
static char* doExtractPage(SDiskbasedBuf* pBuf) {
41,516,360✔
416
  char* availablePage = NULL;
41,516,360✔
417
  if (NO_IN_MEM_AVAILABLE_PAGES(pBuf)) {
41,516,360✔
418
    availablePage = evictBufPage(pBuf);
15,356,812✔
419
    if (availablePage == NULL) {
15,356,804!
420
      uWarn("no available buf pages, current:%d, max:%d, reason: %s, %s", listNEles(pBuf->lruList), pBuf->inMemPages,
×
421
            terrstr(), pBuf->id)
422
    }
423
  } else {
424
    availablePage =
425
        taosMemoryCalloc(1, getAllocPageSize(pBuf->pageSize));  // add extract bytes in case of zipped buffer increased.
52,319,096✔
426
    if (availablePage == NULL) {
26,175,215!
UNCOV
427
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
428
    }
429
  }
430

431
  return availablePage;
41,529,563✔
432
}
433

434
void* getNewBufPage(SDiskbasedBuf* pBuf, int32_t* pageId) {
31,945,480✔
435
  pBuf->statis.getPages += 1;
31,945,480✔
436

437
  char* availablePage = doExtractPage(pBuf);
31,945,480✔
438
  if (availablePage == NULL) {
31,961,392!
UNCOV
439
    return NULL;
×
440
  }
441

442
  SPageInfo* pi = NULL;
31,961,392✔
443
  int32_t    code = 0;
31,961,392✔
444
  if (listNEles(pBuf->freePgList) != 0) {
31,961,392!
445
    SListNode* pItem = tdListPopHead(pBuf->freePgList);
×
446
    pi = *(SPageInfo**)pItem->data;
×
447
    pi->used = true;
×
448
    *pageId = pi->pageId;
×
449
    taosMemoryFreeClear(pItem);
×
450
    code = lruListPushFront(pBuf->lruList, pi);
×
451
    if (TSDB_CODE_SUCCESS != code) {
×
452
      taosMemoryFree(pi);
743✔
453
      taosMemoryFree(availablePage);
×
454
      terrno = code;
×
455
      return NULL;
×
456
    }
457
  } else {  // create a new pageinfo
458
    // register new id in this group
459
    *pageId = (++pBuf->allocateId);
31,961,392✔
460

461
    // register page id info
462
    pi = registerNewPageInfo(pBuf, *pageId);
31,961,392✔
463
    if (pi == NULL) {
31,981,062!
UNCOV
464
      taosMemoryFree(availablePage);
×
UNCOV
465
      return NULL;
×
466
    }
467

468
    // add to hash map
469
    int32_t code = tSimpleHashPut(pBuf->all, pageId, sizeof(int32_t), &pi, POINTER_BYTES);
31,981,062✔
470

471
    if (TSDB_CODE_SUCCESS == code) {
31,982,345!
472
      // add to LRU list
473
      code = lruListPushFront(pBuf->lruList, pi);
31,982,535✔
474
    }
475
    if (TSDB_CODE_SUCCESS == code) {
31,979,122!
476
      pBuf->totalBufSize += pBuf->pageSize;
31,979,122✔
477
    } else {
UNCOV
478
      taosMemoryFree(availablePage);
×
UNCOV
479
      SPageInfo **pLast = taosArrayPop(pBuf->pIdList);
×
UNCOV
480
      int32_t ret = tSimpleHashRemove(pBuf->all, pageId, sizeof(int32_t));
×
UNCOV
481
      if (ret != TSDB_CODE_SUCCESS) {
×
UNCOV
482
        uError("%s failed to clear pageId %d from buf hash-set since %s", __func__, *pageId, tstrerror(ret));
×
483
      }
UNCOV
484
      taosMemoryFree(pi);
×
UNCOV
485
      terrno = code;
×
486
      return NULL;
84✔
487
    }
488
  }
489

490
  pi->pData = availablePage;
31,978,379✔
491

492
  ((void**)pi->pData)[0] = pi;
31,978,379✔
493
#ifdef BUF_PAGE_DEBUG
494
  uDebug("page_getNewBufPage , pi->pData:%p, pageId:%d, offset:%" PRId64, pi->pData, pi->pageId, pi->offset);
495
#endif
496

497
  return (void*)(GET_PAYLOAD_DATA(pi));
31,978,379✔
498
}
499

500
void* getBufPage(SDiskbasedBuf* pBuf, int32_t id) {
1,439,495,957✔
501
  if (id < 0) {
1,439,495,957!
502
    terrno = TSDB_CODE_INVALID_PARA;
×
503
    uError("invalid page id:%d, %s", id, pBuf->id);
×
504
    return NULL;
×
505
  }
506

507
  pBuf->statis.getPages += 1;
1,439,495,957✔
508

509
  SPageInfo** pi = tSimpleHashGet(pBuf->all, &id, sizeof(int32_t));
1,439,495,957✔
510
  if (pi == NULL || *pi == NULL) {
1,438,765,123!
511
    uError("failed to locate the buffer page:%d, %s", id, pBuf->id);
367,260!
512
    terrno = TSDB_CODE_INVALID_PARA;
367,260✔
513
    return NULL;
×
514
  }
515

516
  if (BUF_PAGE_IN_MEM(*pi)) {  // it is in memory
1,438,397,863✔
517
    // no need to update the LRU list if only one page exists
518
    if (pBuf->numOfPages == 1) {
1,429,627,178✔
519
      (*pi)->used = true;
167,633,217✔
520
      return (void*)(GET_PAYLOAD_DATA(*pi));
167,633,217✔
521
    }
522

523
    SPageInfo** pInfo = (SPageInfo**)((*pi)->pn->data);
1,261,993,961✔
524
    if (*pInfo != *pi) {
1,261,993,961!
525
      terrno = TSDB_CODE_APP_ERROR;
×
526
      uError("inconsistently data in paged buffer, pInfo:%p, pi:%p, %s", *pInfo, *pi, pBuf->id);
×
527
      return NULL;
×
528
    }
529

530
    lruListMoveToFront(pBuf->lruList, (*pi));
1,261,993,961✔
531
    (*pi)->used = true;
1,263,018,161✔
532

533
#ifdef BUF_PAGE_DEBUG
534
    uDebug("page_getBufPage1 pageId:%d, offset:%" PRId64, (*pi)->pageId, (*pi)->offset);
535
#endif
536
    return (void*)(GET_PAYLOAD_DATA(*pi));
1,263,018,161✔
537
  } else {  // not in memory
538

539
    (*pi)->pData = doExtractPage(pBuf);
8,770,685✔
540

541
    // failed to evict buffer page, return with error code.
542
    if ((*pi)->pData == NULL) {
9,571,603!
543
      return NULL;
×
544
    }
545

546
    // set the ptr to the new SPageInfo
547
    ((void**)((*pi)->pData))[0] = (*pi);
9,571,603✔
548

549
    int32_t code = lruListPushFront(pBuf->lruList, *pi);
9,571,603✔
550
    if (TSDB_CODE_SUCCESS != code) {
9,571,646!
551
      taosMemoryFree((*pi)->pData);
×
552
      (*pi)->pData = NULL;
×
553
      terrno = code;
×
554
      return NULL;
×
555
    }
556
    (*pi)->used = true;
9,571,646✔
557

558
    // some data has been flushed to disk, and needs to be loaded into buffer again.
559
    if (HAS_DATA_IN_DISK(*pi)) {
9,571,646!
560
      int32_t code = loadPageFromDisk(pBuf, *pi);
9,571,678✔
561
      if (code != 0) {
9,573,569!
562
        taosMemoryFree((*pi)->pData);
×
563
        (*pi)->pData = NULL;
×
564
        terrno = code;
×
565
        return NULL;
×
566
      }
567
    }
568
#ifdef BUF_PAGE_DEBUG
569
    uDebug("page_getBufPage2 pageId:%d, offset:%" PRId64, (*pi)->pageId, (*pi)->offset);
570
#endif
571
    return (void*)(GET_PAYLOAD_DATA(*pi));
9,573,619✔
572
  }
573
}
574

575
void releaseBufPage(SDiskbasedBuf* pBuf, void* page) {
1,168,765,310✔
576
  if (page == NULL) {
1,168,765,310!
577
    return;
×
578
  }
579

580
  SPageInfo* ppi = getPageInfoFromPayload(page);
1,168,765,310✔
581
  releaseBufPageInfo(pBuf, ppi);
1,165,815,319✔
582
}
583

584
void releaseBufPageInfo(SDiskbasedBuf* pBuf, SPageInfo* pi) {
1,165,849,887✔
585
#ifdef BUF_PAGE_DEBUG
586
  uDebug("page_releaseBufPageInfo pageId:%d, used:%d, offset:%" PRId64, pi->pageId, pi->used, pi->offset);
587
#endif
588

589
  if (pi == NULL) {
1,165,849,887!
590
    return;
×
591
  }
592

593
  if (pi->pData == NULL) {
1,165,849,887!
594
    uError("pi->pData (page data) is null");
×
595
    return;
×
596
  }
597

598
  pi->used = false;
1,165,849,887✔
599
  pBuf->statis.releasePages += 1;
1,165,849,887✔
600
}
601

602
size_t getTotalBufSize(const SDiskbasedBuf* pBuf) { return (size_t)pBuf->totalBufSize; }
×
603

604
SArray* getDataBufPagesIdList(SDiskbasedBuf* pBuf) { return pBuf->pIdList; }
×
605

606
void destroyDiskbasedBuf(SDiskbasedBuf* pBuf) {
11,566,950✔
607
  if (pBuf == NULL) {
11,566,950✔
608
    return;
2,447,122✔
609
  }
610

611
  dBufPrintStatis(pBuf);
9,119,828✔
612

613
  bool needRemoveFile = false;
9,119,983✔
614
  if (pBuf->pFile != NULL) {
9,119,983✔
615
    needRemoveFile = true;
10,881✔
616
    uDebug(
10,881✔
617
        "Paged buffer closed, total:%.2f Kb (%d Pages), inmem size:%.2f Kb (%d Pages), file size:%.2f Kb, page "
618
        "size:%.2f Kb, %s",
619
        pBuf->totalBufSize / 1024.0, pBuf->numOfPages, listNEles(pBuf->lruList) * pBuf->pageSize / 1024.0,
620
        listNEles(pBuf->lruList), pBuf->fileSize / 1024.0, pBuf->pageSize / 1024.0f, pBuf->id);
621

622
    int32_t code = taosCloseFile(&pBuf->pFile);
10,881✔
623
    if (TSDB_CODE_SUCCESS != code) {
10,881!
624
      uDebug("WARNING tPage failed to close file when destroy disk basebuf: %s", pBuf->path);
×
625
    }
626
  } else {
627
    uDebug("Paged buffer closed, total:%.2f Kb, no file created, %s", pBuf->totalBufSize / 1024.0, pBuf->id);
9,109,102✔
628
  }
629

630
  // print the statistics information
631
  {
632
    SDiskbasedBufStatis* ps = &pBuf->statis;
9,119,779✔
633
    if (ps->loadPages == 0) {
9,119,779✔
634
      uDebug("Get/Release pages:%d/%d, flushToDisk:%.2f Kb (%d Pages), loadFromDisk:%.2f Kb (%d Pages)", ps->getPages,
9,108,898✔
635
             ps->releasePages, ps->flushBytes / 1024.0f, ps->flushPages, ps->loadBytes / 1024.0f, ps->loadPages);
636
    } else {
637
      uDebug(
10,881✔
638
          "Get/Release pages:%d/%d, flushToDisk:%.2f Kb (%d Pages), loadFromDisk:%.2f Kb (%d Pages), avgPgSize:%.2f Kb",
639
          ps->getPages, ps->releasePages, ps->flushBytes / 1024.0f, ps->flushPages, ps->loadBytes / 1024.0f,
640
          ps->loadPages, ps->loadBytes / (1024.0 * ps->loadPages));
641
    }
642
  }
643

644
  if (needRemoveFile) {
9,119,779✔
645
    int32_t ret = taosRemoveFile(pBuf->path);
10,881✔
646
    if (ret != 0) {  // print the error and discard this error info
10,881!
647
      uDebug("WARNING tPage remove file failed. path=%s, code:%s", pBuf->path, strerror(errno));
10,881✔
648
    }
649
  }
650

651
  taosMemoryFreeClear(pBuf->path);
9,119,529✔
652

653
  size_t n = taosArrayGetSize(pBuf->pIdList);
9,119,529✔
654
  for (int32_t i = 0; i < n; ++i) {
39,816,893✔
655
    SPageInfo* pi = taosArrayGetP(pBuf->pIdList, i);
30,697,538✔
656
    taosMemoryFreeClear(pi->pData);
30,626,256✔
657
    taosMemoryFreeClear(pi);
30,683,628✔
658
  }
659

660
  taosArrayDestroy(pBuf->pIdList);
9,119,355✔
661

662
  pBuf->lruList = tdListFree(pBuf->lruList);
9,120,288✔
663
  pBuf->freePgList = tdListFree(pBuf->freePgList);
9,120,375✔
664

665
  taosArrayDestroy(pBuf->emptyDummyIdList);
9,120,440✔
666
  taosArrayDestroy(pBuf->pFree);
9,120,328✔
667

668
  tSimpleHashCleanup(pBuf->all);
9,120,180✔
669

670
  taosMemoryFreeClear(pBuf->id);
9,120,490!
671
  taosMemoryFreeClear(pBuf->assistBuf);
9,120,515!
672
  taosMemoryFreeClear(pBuf);
9,120,515!
673
}
674

675
SPageInfo* getLastPageInfo(SArray* pList) {
×
676
  size_t     size = taosArrayGetSize(pList);
×
677
  SPageInfo* pPgInfo = taosArrayGetP(pList, size - 1);
×
678
  return pPgInfo;
×
679
}
680

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

683
int32_t getBufPageSize(const SDiskbasedBuf* pBuf) { return pBuf->pageSize; }
661,951,603✔
684

685
int32_t getNumOfInMemBufPages(const SDiskbasedBuf* pBuf) { return pBuf->inMemPages; }
2,143,193✔
686

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

689
void setBufPageDirty(void* pPage, bool dirty) {
691,713,204✔
690
  SPageInfo* ppi = getPageInfoFromPayload(pPage);
691,713,204✔
691
  ppi->dirty = dirty;
690,987,001✔
692
}
690,987,001✔
693

694
int32_t setBufPageCompressOnDisk(SDiskbasedBuf* pBuf, bool comp) {
×
695
  pBuf->comp = comp;
×
696
  if (comp && (pBuf->assistBuf == NULL)) {
×
697
    pBuf->assistBuf = taosMemoryMalloc(pBuf->pageSize + 2);  // EXTRA BYTES
×
698
    if (pBuf->assistBuf) {
×
699
      return terrno;
×
700
    }
701
  }
702
  return TSDB_CODE_SUCCESS;
×
703
}
704

705
int32_t dBufSetBufPageRecycled(SDiskbasedBuf* pBuf, void* pPage) {
×
706
  SPageInfo* ppi = getPageInfoFromPayload(pPage);
×
707

708
  int32_t code = tdListAppend(pBuf->freePgList, &ppi);
×
709
  if (TSDB_CODE_SUCCESS != code) {
×
710
    return code;
×
711
  }
712

713
  ppi->used = false;
×
714
  ppi->dirty = false;
×
715

716
  // add this pageinfo into the free page info list
717
  SListNode* pNode = tdListPopNode(pBuf->lruList, ppi->pn);
×
718
  taosMemoryFreeClear(ppi->pData);
×
719
  taosMemoryFreeClear(pNode);
×
720
  ppi->pn = NULL;
×
721
  return TSDB_CODE_SUCCESS;
×
722
}
723

724
void dBufSetPrintInfo(SDiskbasedBuf* pBuf) { pBuf->printStatis = true; }
3,538,507✔
725

726
SDiskbasedBufStatis getDBufStatis(const SDiskbasedBuf* pBuf) { return pBuf->statis; }
2,276,014✔
727

728
void dBufPrintStatis(const SDiskbasedBuf* pBuf) {
9,119,924✔
729
  if (!pBuf->printStatis) {
9,119,924✔
730
    return;
5,580,595✔
731
  }
732

733
  const SDiskbasedBufStatis* ps = &pBuf->statis;
3,539,329✔
734

735
#if 0
736
  printf(
737
      "Paged buffer closed, total:%.2f Kb (%d Pages), inmem size:%.2f Kb (%d Pages), file size:%.2f Kb, page size:%.2f "
738
      "Kb, %s\n",
739
      pBuf->totalBufSize / 1024.0, pBuf->numOfPages, listNEles(pBuf->lruList) * pBuf->pageSize / 1024.0,
740
      listNEles(pBuf->lruList), pBuf->fileSize / 1024.0, pBuf->pageSize / 1024.0f, pBuf->id);
741
#endif
742

743
  if (ps->loadPages > 0) {
3,539,329✔
744
    (void)printf(
1,558✔
745
        "Get/Release pages:%d/%d, flushToDisk:%.2f Kb (%d Pages), loadFromDisk:%.2f Kb (%d Pages), avgPageSize:%.2f "
746
        "Kb\n",
747
        ps->getPages, ps->releasePages, ps->flushBytes / 1024.0f, ps->flushPages, ps->loadBytes / 1024.0f,
1,558✔
748
        ps->loadPages, ps->loadBytes / (1024.0 * ps->loadPages));
1,558✔
749
  } else {
750
    // printf("no page loaded\n");
751
  }
752
}
753

754
void clearDiskbasedBuf(SDiskbasedBuf* pBuf) {
28,851✔
755
  size_t n = taosArrayGetSize(pBuf->pIdList);
28,851✔
756
  for (int32_t i = 0; i < n; ++i) {
1,277,602✔
757
    SPageInfo* pi = taosArrayGetP(pBuf->pIdList, i);
1,248,747✔
758
    taosMemoryFreeClear(pi->pData);
1,248,585✔
759
    taosMemoryFreeClear(pi);
1,248,748✔
760
  }
761

762
  taosArrayClear(pBuf->pIdList);
28,855✔
763

764
  tdListEmpty(pBuf->lruList);
28,857✔
765
  tdListEmpty(pBuf->freePgList);
28,853✔
766

767
  taosArrayClear(pBuf->emptyDummyIdList);
28,856✔
768
  taosArrayClear(pBuf->pFree);
28,858✔
769

770
  tSimpleHashClear(pBuf->all);
28,857✔
771

772
  pBuf->numOfPages = 0;  // all pages are in buffer in the first place
28,855✔
773
  pBuf->totalBufSize = 0;
28,855✔
774
  pBuf->allocateId = -1;
28,855✔
775
  pBuf->fileSize = 0;
28,855✔
776
}
28,855✔
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