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

taosdata / TDengine / #3531

19 Nov 2024 10:42AM UTC coverage: 60.213% (-0.006%) from 60.219%
#3531

push

travis-ci

web-flow
Merge pull request #28777 from taosdata/fix/3.0/TD-32366

fix:TD-32366/stmt add geometry datatype check

118529 of 252344 branches covered (46.97%)

Branch coverage included in aggregate %.

7 of 48 new or added lines in 3 files covered. (14.58%)

2282 existing lines in 115 files now uncovered.

199096 of 275161 relevant lines covered (72.36%)

6067577.83 hits per line

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

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

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

70
  return TSDB_CODE_SUCCESS;
309✔
71
}
72

73
static char* doCompressData(void* data, int32_t srcSize, int32_t* dst, SDiskbasedBuf* pBuf) {  // do nothing
1,289,294✔
74
  if (!pBuf->comp) {
1,289,294!
75
    *dst = srcSize;
1,289,334✔
76
    return data;
1,289,334✔
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
1,127,210✔
86
  int32_t code = 0;
1,127,210✔
87
  if (!pBuf->comp) {
1,127,210!
88
    *dst = srcSize;
1,127,213✔
89
    return code;
1,127,213✔
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) {
1,265,386✔
103
  if (pBuf->pFree == NULL) {
1,265,386!
104
    return pBuf->nextPos;
×
105
  } else {
106
    int32_t offset = -1;
1,265,386✔
107

108
    size_t num = taosArrayGetSize(pBuf->pFree);
1,265,386✔
109
    for (int32_t i = 0; i < num; ++i) {
1,265,366!
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;
1,265,366✔
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); }
9,877,624✔
135

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

142
  ret = (int32_t)taosWriteFile(pBuf->pFile, pData, size);
1,289,565✔
143
  if (ret != size) {
1,289,530!
144
    return terrno;
×
145
  }
146

147
  // extend the file
148
  if (pBuf->fileSize < offset + size) {
1,289,546✔
149
    pBuf->fileSize = offset + size;
1,265,596✔
150
  }
151

152
  pBuf->statis.flushBytes += size;
1,289,546✔
153
  pBuf->statis.flushPages += 1;
1,289,546✔
154

155
  return TSDB_CODE_SUCCESS;
1,289,546✔
156
}
157

158
static char* doFlushBufPage(SDiskbasedBuf* pBuf, SPageInfo* pg) {
1,841,860✔
159
  if (pg->pData == NULL || pg->used) {
1,841,860!
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;
1,842,027✔
166
  int64_t offset = pg->offset;
1,842,027✔
167

168
  char* t = NULL;
1,842,027✔
169
  if ((!HAS_DATA_IN_DISK(pg)) || pg->dirty) {
1,842,027✔
170
    void* payload = GET_PAYLOAD_DATA(pg);
1,289,157✔
171
    t = doCompressData(payload, pBuf->pageSize + sizeof(SFilePage), &size, pBuf);
1,289,157✔
172
    if (size < 0) {
1,289,327!
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) {
1,842,197✔
181
    if (!HAS_DATA_IN_DISK(pg)) {
1,289,326✔
182
      offset = allocateNewPositionInFile(pBuf, size);
1,265,394✔
183
      pBuf->nextPos += size;
1,265,357✔
184

185
      int32_t code = doFlushBufPageImpl(pBuf, offset, t, size);
1,265,357✔
186
      if (code != TSDB_CODE_SUCCESS) {
1,265,587!
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) {
23,932!
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);
23,932✔
204
      if (code != TSDB_CODE_SUCCESS) {
23,944!
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;
552,871✔
210
  }
211

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

224
static char* flushBufPage(SDiskbasedBuf* pBuf, SPageInfo* pg) {
1,841,855✔
225
  int32_t ret = TSDB_CODE_SUCCESS;
1,841,855✔
226

227
  if (pBuf->pFile == NULL) {
1,841,855✔
228
    if ((ret = createDiskFile(pBuf)) != TSDB_CODE_SUCCESS) {
309!
229
      terrno = ret;
×
230
      return NULL;
×
231
    }
232
  }
233

234
  char* p = doFlushBufPage(pBuf, pg);
1,841,855✔
235
  CLEAR_BUF_PAGE_IN_MEM_FLAG(pg);
1,842,357✔
236

237
  pg->dirty = false;
1,842,357✔
238
  return p;
1,842,357✔
239
}
240

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

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

261
  pBuf->statis.loadBytes += pg->length;
1,127,214✔
262
  pBuf->statis.loadPages += 1;
1,127,214✔
263

264
  int32_t fullSize = 0;
1,127,214✔
265
  return doDecompressData(pPage, pg->length, &fullSize, pBuf);
1,127,214✔
266
}
267

268
static SPageInfo* registerNewPageInfo(SDiskbasedBuf* pBuf, int32_t pageId) {
8,759,553✔
269
  pBuf->numOfPages += 1;
8,759,553✔
270

271
  SPageInfo* ppi = taosMemoryMalloc(sizeof(SPageInfo));
8,759,553✔
272
  if (ppi == NULL) {
8,772,181!
273
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
274
    return NULL;
×
275
  }
276

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

285
  SPageInfo** pRet = taosArrayPush(pBuf->pIdList, &ppi);
8,772,181✔
286
  if (NULL == pRet) {
8,769,562!
287
    taosMemoryFree(ppi);
×
288
    return NULL;
×
289
  }
290
  return *pRet;
8,769,562✔
291
}
292

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

297
  SListNode* pn = NULL;
1,842,163✔
298
  while ((pn = tdListNext(&iter)) != NULL) {
2,091,038✔
299
    SPageInfo* pageInfo = *(SPageInfo**)pn->data;
2,091,167✔
300

301
    SPageInfo* p = *(SPageInfo**)(pageInfo->pData);
2,091,167✔
302

303
    if (!pageInfo->used) {
2,091,167✔
304
      break;
1,842,292✔
305
    }
306
  }
307

308
  return pn;
1,842,294✔
309
}
310

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

317
  terrno = 0;
1,842,263✔
318
  pn = tdListPopNode(pBuf->lruList, pn);
1,842,241✔
319

320
  SPageInfo* d = *(SPageInfo**)pn->data;
1,841,801✔
321

322
  d->pn = NULL;
1,841,801✔
323
  taosMemoryFreeClear(pn);
1,841,801!
324

325
  return flushBufPage(pBuf, d);
1,841,824✔
326
}
327

328
static int32_t lruListPushFront(SList* pList, SPageInfo* pi) {
9,895,708✔
329
  int32_t code = tdListPrepend(pList, &pi);
9,895,708✔
330
  if (TSDB_CODE_SUCCESS != code) {
9,897,087!
331
    return code;
×
332
  }
333
  SListNode* front = tdListGetHead(pList);
9,897,087✔
334
  pi->pn = front;
9,894,500✔
335
  return TSDB_CODE_SUCCESS;
9,894,500✔
336
}
337

338
static void lruListMoveToFront(SList* pList, SPageInfo* pi) {
317,342,084✔
339
  pi->pn = tdListPopNode(pList, pi->pn);
317,342,084✔
340
  tdListPrependNode(pList, pi->pn);
317,545,304✔
341
}
318,330,979✔
342

343
static SPageInfo* getPageInfoFromPayload(void* page) {
406,591,206✔
344
  char* p = (char*)page - POINTER_BYTES;
406,591,206✔
345

346
  SPageInfo* ppi = ((SPageInfo**)p)[0];
406,591,206✔
347
  return ppi;
406,591,206✔
348
}
349

350
int32_t createDiskbasedBuf(SDiskbasedBuf** pBuf, int32_t pagesize, int32_t inMemBufSize, const char* id,
1,648,364✔
351
                           const char* dir) {
352
  *pBuf = NULL;
1,648,364✔
353
  SDiskbasedBuf* pPBuf = taosMemoryCalloc(1, sizeof(SDiskbasedBuf));
1,648,364✔
354
  if (pPBuf == NULL) {
1,649,105!
355
    goto _error;
×
356
  }
357

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

374
  // at least more than 2 pages must be in memory
375
  if (inMemBufSize < pagesize * 2) {
1,648,954✔
376
    inMemBufSize = pagesize * 2;
4,304✔
377
  }
378

379
  pPBuf->inMemPages = inMemBufSize / pagesize;  // maximum allowed pages, it is a soft limit.
1,648,954✔
380
  pPBuf->lruList = tdListNew(POINTER_BYTES);
1,648,954✔
381
  if (pPBuf->lruList == NULL) {
1,649,351!
382
    goto _error;
×
383
  }
384

385
  // init id hash table
386
  _hash_fn_t fn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT);
1,649,351✔
387
  pPBuf->pIdList = taosArrayInit(4, POINTER_BYTES);
1,649,262✔
388
  if (pPBuf->pIdList == NULL) {
1,649,635!
389
    goto _error;
×
390
  }
391

392
  pPBuf->all = tSimpleHashInit(64, fn);
1,649,635✔
393
  if (pPBuf->all == NULL) {
1,648,876!
394
    goto _error;
×
395
  }
396

397
  pPBuf->prefix = (char*)dir;
1,648,876✔
398
  pPBuf->emptyDummyIdList = taosArrayInit(1, sizeof(int32_t));
1,648,876✔
399
  if (pPBuf->emptyDummyIdList == NULL) {
1,649,592!
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;
1,649,592✔
407
  return TSDB_CODE_SUCCESS;
1,649,592✔
408

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

415
static char* doExtractPage(SDiskbasedBuf* pBuf) {
9,877,397✔
416
  char* availablePage = NULL;
9,877,397✔
417
  if (NO_IN_MEM_AVAILABLE_PAGES(pBuf)) {
9,877,397✔
418
    availablePage = evictBufPage(pBuf);
1,842,175✔
419
    if (availablePage == NULL) {
1,842,324!
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.
16,070,444✔
426
    if (availablePage == NULL) {
8,043,830!
427
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
428
    }
429
  }
430

431
  return availablePage;
9,885,908✔
432
}
433

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

437
  char* availablePage = doExtractPage(pBuf);
8,750,653✔
438
  if (availablePage == NULL) {
8,759,116!
439
    return NULL;
×
440
  }
441

442
  SPageInfo* pi = NULL;
8,759,116✔
443
  int32_t    code = 0;
8,759,116✔
444
  if (listNEles(pBuf->freePgList) != 0) {
8,759,116!
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);
606✔
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);
8,759,116✔
460

461
    // register page id info
462
    pi = registerNewPageInfo(pBuf, *pageId);
8,759,116✔
463
    if (pi == NULL) {
8,768,507!
464
      taosMemoryFree(availablePage);
×
465
      return NULL;
×
466
    }
467

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

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

490
  pi->pData = availablePage;
8,766,878✔
491

492
  ((void**)pi->pData)[0] = pi;
8,766,878✔
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));
8,766,878✔
498
}
499

500
void* getBufPage(SDiskbasedBuf* pBuf, int32_t id) {
345,927,387✔
501
  if (id < 0) {
345,927,387!
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;
345,927,387✔
508

509
  SPageInfo** pi = tSimpleHashGet(pBuf->all, &id, sizeof(int32_t));
345,927,387✔
510
  if (pi == NULL || *pi == NULL) {
343,756,330!
UNCOV
511
    uError("failed to locate the buffer page:%d, %s", id, pBuf->id);
×
UNCOV
512
    terrno = TSDB_CODE_INVALID_PARA;
×
513
    return NULL;
×
514
  }
515

516
  if (BUF_PAGE_IN_MEM(*pi)) {  // it is in memory
344,245,267✔
517
    // no need to update the LRU list if only one page exists
518
    if (pBuf->numOfPages == 1) {
343,108,864✔
519
      (*pi)->used = true;
27,219,927✔
520
      return (void*)(GET_PAYLOAD_DATA(*pi));
27,219,927✔
521
    }
522

523
    SPageInfo** pInfo = (SPageInfo**)((*pi)->pn->data);
315,888,937✔
524
    if (*pInfo != *pi) {
315,888,937!
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));
315,888,937✔
531
    (*pi)->used = true;
318,501,131✔
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));
318,501,131✔
537
  } else {  // not in memory
538

539
    (*pi)->pData = doExtractPage(pBuf);
1,136,403✔
540

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

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

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

558
    // some data has been flushed to disk, and needs to be loaded into buffer again.
559
    if (HAS_DATA_IN_DISK(*pi)) {
1,127,027!
560
      int32_t code = loadPageFromDisk(pBuf, *pi);
1,127,040✔
561
      if (code != 0) {
1,127,201!
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));
1,127,202✔
572
  }
573
}
574

575
void releaseBufPage(SDiskbasedBuf* pBuf, void* page) {
243,154,757✔
576
  if (page == NULL) {
243,154,757!
577
    return;
×
578
  }
579

580
  SPageInfo* ppi = getPageInfoFromPayload(page);
243,154,757✔
581
  releaseBufPageInfo(pBuf, ppi);
241,848,172✔
582
}
583

584
void releaseBufPageInfo(SDiskbasedBuf* pBuf, SPageInfo* pi) {
241,897,073✔
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) {
241,897,073!
590
    return;
×
591
  }
592

593
  if (pi->pData == NULL) {
241,897,073!
594
    uError("pi->pData (page data) is null");
×
595
    return;
×
596
  }
597

598
  pi->used = false;
241,897,073✔
599
  pBuf->statis.releasePages += 1;
241,897,073✔
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) {
2,019,500✔
607
  if (pBuf == NULL) {
2,019,500✔
608
    return;
370,001✔
609
  }
610

611
  dBufPrintStatis(pBuf);
1,649,499✔
612

613
  bool needRemoveFile = false;
1,649,542✔
614
  if (pBuf->pFile != NULL) {
1,649,542✔
615
    needRemoveFile = true;
309✔
616
    uDebug(
309✔
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);
309✔
623
    if (TSDB_CODE_SUCCESS != code) {
309!
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);
1,649,233✔
628
  }
629

630
  // print the statistics information
631
  {
632
    SDiskbasedBufStatis* ps = &pBuf->statis;
1,649,538✔
633
    if (ps->loadPages == 0) {
1,649,538✔
634
      uDebug("Get/Release pages:%d/%d, flushToDisk:%.2f Kb (%d Pages), loadFromDisk:%.2f Kb (%d Pages)", ps->getPages,
1,649,229✔
635
             ps->releasePages, ps->flushBytes / 1024.0f, ps->flushPages, ps->loadBytes / 1024.0f, ps->loadPages);
636
    } else {
637
      uDebug(
309✔
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) {
1,649,539✔
645
    int32_t ret = taosRemoveFile(pBuf->path);
309✔
646
    if (ret != 0) {  // print the error and discard this error info
309!
647
      uDebug("WARNING tPage remove file failed. path=%s, code:%s", pBuf->path, strerror(errno));
309✔
648
    }
649
  }
650

651
  taosMemoryFreeClear(pBuf->path);
1,649,495✔
652

653
  size_t n = taosArrayGetSize(pBuf->pIdList);
1,649,495✔
654
  for (int32_t i = 0; i < n; ++i) {
9,221,448✔
655
    SPageInfo* pi = taosArrayGetP(pBuf->pIdList, i);
7,571,851✔
656
    taosMemoryFreeClear(pi->pData);
7,531,550✔
657
    taosMemoryFreeClear(pi);
7,589,552✔
658
  }
659

660
  taosArrayDestroy(pBuf->pIdList);
1,649,597✔
661

662
  pBuf->lruList = tdListFree(pBuf->lruList);
1,649,585✔
663
  pBuf->freePgList = tdListFree(pBuf->freePgList);
1,649,590✔
664

665
  taosArrayDestroy(pBuf->emptyDummyIdList);
1,649,604✔
666
  taosArrayDestroy(pBuf->pFree);
1,649,608✔
667

668
  tSimpleHashCleanup(pBuf->all);
1,649,616✔
669

670
  taosMemoryFreeClear(pBuf->id);
1,649,614!
671
  taosMemoryFreeClear(pBuf->assistBuf);
1,649,619!
672
  taosMemoryFreeClear(pBuf);
1,649,619✔
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; }
228,889,476✔
684

685
int32_t getNumOfInMemBufPages(const SDiskbasedBuf* pBuf) { return pBuf->inMemPages; }
74,970✔
686

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

689
void setBufPageDirty(void* pPage, bool dirty) {
173,415,500✔
690
  SPageInfo* ppi = getPageInfoFromPayload(pPage);
173,415,500✔
691
  ppi->dirty = dirty;
173,092,935✔
692
}
173,092,935✔
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; }
164,968✔
725

726
SDiskbasedBufStatis getDBufStatis(const SDiskbasedBuf* pBuf) { return pBuf->statis; }
75,556✔
727

728
void dBufPrintStatis(const SDiskbasedBuf* pBuf) {
1,649,522✔
729
  if (!pBuf->printStatis) {
1,649,522✔
730
    return;
1,484,588✔
731
  }
732

733
  const SDiskbasedBufStatis* ps = &pBuf->statis;
164,934✔
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) {
164,934✔
744
    (void)printf(
71✔
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,
71✔
748
        ps->loadPages, ps->loadBytes / (1024.0 * ps->loadPages));
71✔
749
  } else {
750
    // printf("no page loaded\n");
751
  }
752
}
753

754
void clearDiskbasedBuf(SDiskbasedBuf* pBuf) {
17,675✔
755
  size_t n = taosArrayGetSize(pBuf->pIdList);
17,675✔
756
  for (int32_t i = 0; i < n; ++i) {
1,179,759✔
757
    SPageInfo* pi = taosArrayGetP(pBuf->pIdList, i);
1,162,093✔
758
    taosMemoryFreeClear(pi->pData);
1,161,990✔
759
    taosMemoryFreeClear(pi);
1,162,082✔
760
  }
761

762
  taosArrayClear(pBuf->pIdList);
17,666✔
763

764
  tdListEmpty(pBuf->lruList);
17,675✔
765
  tdListEmpty(pBuf->freePgList);
17,674✔
766

767
  taosArrayClear(pBuf->emptyDummyIdList);
17,674✔
768
  taosArrayClear(pBuf->pFree);
17,674✔
769

770
  tSimpleHashClear(pBuf->all);
17,674✔
771

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