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

taosdata / TDengine / #3534

21 Nov 2024 07:36AM UTC coverage: 60.825% (+2.0%) from 58.848%
#3534

push

travis-ci

web-flow
Merge pull request #28810 from taosdata/ehn/add-sync-heartbeat-sent-time-to-log

ehn:add-sync-heartbeat-sent-time-to-log

120023 of 252376 branches covered (47.56%)

Branch coverage included in aggregate %.

43 of 47 new or added lines in 3 files covered. (91.49%)

2254 existing lines in 162 files now uncovered.

200876 of 275203 relevant lines covered (72.99%)

16110754.39 hits per line

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

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

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

70
  return TSDB_CODE_SUCCESS;
7,780✔
71
}
72

73
static char* doCompressData(void* data, int32_t srcSize, int32_t* dst, SDiskbasedBuf* pBuf) {  // do nothing
8,233,678✔
74
  if (!pBuf->comp) {
8,233,678!
75
    *dst = srcSize;
8,234,002✔
76
    return data;
8,234,002✔
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
7,747,799✔
86
  int32_t code = 0;
7,747,799✔
87
  if (!pBuf->comp) {
7,747,799!
88
    *dst = srcSize;
7,747,810✔
89
    return code;
7,747,810✔
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) {
7,789,655✔
103
  if (pBuf->pFree == NULL) {
7,789,655!
104
    return pBuf->nextPos;
×
105
  } else {
106
    int32_t offset = -1;
7,789,655✔
107

108
    size_t num = taosArrayGetSize(pBuf->pFree);
7,789,655✔
109
    for (int32_t i = 0; i < num; ++i) {
7,789,511!
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;
7,789,511✔
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,143,509✔
135

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

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

147
  // extend the file
148
  if (pBuf->fileSize < offset + size) {
8,235,716✔
149
    pBuf->fileSize = offset + size;
7,791,445✔
150
  }
151

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

155
  return TSDB_CODE_SUCCESS;
8,235,716✔
156
}
157

158
static char* doFlushBufPage(SDiskbasedBuf* pBuf, SPageInfo* pg) {
12,626,614✔
159
  if (pg->pData == NULL || pg->used) {
12,626,614!
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;
12,627,242✔
166
  int64_t offset = pg->offset;
12,627,242✔
167

168
  char* t = NULL;
12,627,242✔
169
  if ((!HAS_DATA_IN_DISK(pg)) || pg->dirty) {
12,627,242✔
170
    void* payload = GET_PAYLOAD_DATA(pg);
8,232,591✔
171
    t = doCompressData(payload, pBuf->pageSize + sizeof(SFilePage), &size, pBuf);
8,232,591✔
172
    if (size < 0) {
8,234,013!
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) {
12,628,664✔
181
    if (!HAS_DATA_IN_DISK(pg)) {
8,233,975✔
182
      offset = allocateNewPositionInFile(pBuf, size);
7,789,739✔
183
      pBuf->nextPos += size;
7,789,400✔
184

185
      int32_t code = doFlushBufPageImpl(pBuf, offset, t, size);
7,789,400✔
186
      if (code != TSDB_CODE_SUCCESS) {
7,791,023!
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) {
444,236!
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);
444,236✔
204
      if (code != TSDB_CODE_SUCCESS) {
444,265!
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;
4,394,689✔
210
  }
211

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

224
static char* flushBufPage(SDiskbasedBuf* pBuf, SPageInfo* pg) {
12,626,556✔
225
  int32_t ret = TSDB_CODE_SUCCESS;
12,626,556✔
226

227
  if (pBuf->pFile == NULL) {
12,626,556✔
228
    if ((ret = createDiskFile(pBuf)) != TSDB_CODE_SUCCESS) {
7,780!
229
      terrno = ret;
×
230
      return NULL;
×
231
    }
232
  }
233

234
  char* p = doFlushBufPage(pBuf, pg);
12,626,556✔
235
  CLEAR_BUF_PAGE_IN_MEM_FLAG(pg);
12,628,480✔
236

237
  pg->dirty = false;
12,628,480✔
238
  return p;
12,628,480✔
239
}
240

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

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

261
  pBuf->statis.loadBytes += pg->length;
7,747,875✔
262
  pBuf->statis.loadPages += 1;
7,747,875✔
263

264
  int32_t fullSize = 0;
7,747,875✔
265
  return doDecompressData(pPage, pg->length, &fullSize, pBuf);
7,747,875✔
266
}
267

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

271
  SPageInfo* ppi = taosMemoryMalloc(sizeof(SPageInfo));
31,409,815✔
272
  if (ppi == NULL) {
31,426,870!
UNCOV
273
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
274
    return NULL;
×
275
  }
276

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

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

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

297
  SListNode* pn = NULL;
12,628,250✔
298
  while ((pn = tdListNext(&iter)) != NULL) {
14,408,662!
299
    SPageInfo* pageInfo = *(SPageInfo**)pn->data;
14,408,652✔
300

301
    SPageInfo* p = *(SPageInfo**)(pageInfo->pData);
14,408,652✔
302

303
    if (!pageInfo->used) {
14,408,652✔
304
      break;
12,628,240✔
305
    }
306
  }
307

308
  return pn;
12,628,115✔
309
}
310

311
static char* evictBufPage(SDiskbasedBuf* pBuf) {
12,628,571✔
312
  SListNode* pn = getEldestUnrefedPage(pBuf);
12,628,571✔
313
  if (pn == NULL) {  // no available buffer pages now, return.
12,627,994!
314
    return NULL;
×
315
  }
316

317
  terrno = 0;
12,627,994✔
318
  pn = tdListPopNode(pBuf->lruList, pn);
12,627,844✔
319

320
  SPageInfo* d = *(SPageInfo**)pn->data;
12,625,051✔
321

322
  d->pn = NULL;
12,625,051✔
323
  taosMemoryFreeClear(pn);
12,625,051!
324

325
  return flushBufPage(pBuf, d);
12,625,864✔
326
}
327

328
static int32_t lruListPushFront(SList* pList, SPageInfo* pi) {
39,168,699✔
329
  int32_t code = tdListPrepend(pList, &pi);
39,168,699✔
330
  if (TSDB_CODE_SUCCESS != code) {
39,169,213!
UNCOV
331
    return code;
×
332
  }
333
  SListNode* front = tdListGetHead(pList);
39,169,213✔
334
  pi->pn = front;
39,164,614✔
335
  return TSDB_CODE_SUCCESS;
39,164,614✔
336
}
337

338
static void lruListMoveToFront(SList* pList, SPageInfo* pi) {
1,252,082,468✔
339
  pi->pn = tdListPopNode(pList, pi->pn);
1,252,082,468✔
340
  tdListPrependNode(pList, pi->pn);
1,248,987,916✔
341
}
1,247,972,694✔
342

343
static SPageInfo* getPageInfoFromPayload(void* page) {
1,809,774,846✔
344
  char* p = (char*)page - POINTER_BYTES;
1,809,774,846✔
345

346
  SPageInfo* ppi = ((SPageInfo**)p)[0];
1,809,774,846✔
347
  return ppi;
1,809,774,846✔
348
}
349

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

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

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

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

385
  // init id hash table
386
  _hash_fn_t fn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT);
9,235,692✔
387
  pPBuf->pIdList = taosArrayInit(4, POINTER_BYTES);
9,234,669✔
388
  if (pPBuf->pIdList == NULL) {
9,237,208!
UNCOV
389
    goto _error;
×
390
  }
391

392
  pPBuf->all = tSimpleHashInit(64, fn);
9,237,208✔
393
  if (pPBuf->all == NULL) {
9,231,306!
UNCOV
394
    goto _error;
×
395
  }
396

397
  pPBuf->prefix = (char*)dir;
9,231,306✔
398
  pPBuf->emptyDummyIdList = taosArrayInit(1, sizeof(int32_t));
9,231,306✔
399
  if (pPBuf->emptyDummyIdList == NULL) {
9,237,721!
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,237,721✔
407
  return TSDB_CODE_SUCCESS;
9,237,721✔
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) {
39,142,176✔
416
  char* availablePage = NULL;
39,142,176✔
417
  if (NO_IN_MEM_AVAILABLE_PAGES(pBuf)) {
39,142,176✔
418
    availablePage = evictBufPage(pBuf);
12,628,644✔
419
    if (availablePage == NULL) {
12,628,486!
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.
53,027,064✔
426
    if (availablePage == NULL) {
26,524,487!
UNCOV
427
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
428
    }
429
  }
430

431
  return availablePage;
39,151,272✔
432
}
433

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

437
  char* availablePage = doExtractPage(pBuf);
31,396,706✔
438
  if (availablePage == NULL) {
31,407,959!
UNCOV
439
    return NULL;
×
440
  }
441

442
  SPageInfo* pi = NULL;
31,407,959✔
443
  int32_t    code = 0;
31,407,959✔
444
  if (listNEles(pBuf->freePgList) != 0) {
31,407,959!
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);
1,408✔
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,407,959✔
460

461
    // register page id info
462
    pi = registerNewPageInfo(pBuf, *pageId);
31,407,959✔
463
    if (pi == NULL) {
31,424,063!
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,424,063✔
470

471
    if (TSDB_CODE_SUCCESS == code) {
31,425,815!
472
      // add to LRU list
473
      code = lruListPushFront(pBuf->lruList, pi);
31,425,999✔
474
    }
475
    if (TSDB_CODE_SUCCESS == code) {
31,420,287!
476
      pBuf->totalBufSize += pBuf->pageSize;
31,420,287✔
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;
142✔
487
    }
488
  }
489

490
  pi->pData = availablePage;
31,418,879✔
491

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

500
void* getBufPage(SDiskbasedBuf* pBuf, int32_t id) {
1,416,476,746✔
501
  if (id < 0) {
1,416,476,746!
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,416,476,746✔
508

509
  SPageInfo** pi = tSimpleHashGet(pBuf->all, &id, sizeof(int32_t));
1,416,476,746✔
510
  if (pi == NULL || *pi == NULL) {
1,417,022,837!
511
    uError("failed to locate the buffer page:%d, %s", id, pBuf->id);
321,620!
512
    terrno = TSDB_CODE_INVALID_PARA;
321,620✔
513
    return NULL;
×
514
  }
515

516
  if (BUF_PAGE_IN_MEM(*pi)) {  // it is in memory
1,416,701,217✔
517
    // no need to update the LRU list if only one page exists
518
    if (pBuf->numOfPages == 1) {
1,409,825,390✔
519
      (*pi)->used = true;
159,560,434✔
520
      return (void*)(GET_PAYLOAD_DATA(*pi));
159,560,434✔
521
    }
522

523
    SPageInfo** pInfo = (SPageInfo**)((*pi)->pn->data);
1,250,264,956✔
524
    if (*pInfo != *pi) {
1,250,264,956!
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,250,264,956✔
531
    (*pi)->used = true;
1,248,108,125✔
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,248,108,125✔
537
  } else {  // not in memory
538

539
    (*pi)->pData = doExtractPage(pBuf);
6,875,827✔
540

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

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

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

558
    // some data has been flushed to disk, and needs to be loaded into buffer again.
559
    if (HAS_DATA_IN_DISK(*pi)) {
7,745,908✔
560
      int32_t code = loadPageFromDisk(pBuf, *pi);
7,745,867✔
561
      if (code != 0) {
7,747,663✔
562
        taosMemoryFree((*pi)->pData);
18✔
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));
7,747,686✔
572
  }
573
}
574

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

580
  SPageInfo* ppi = getPageInfoFromPayload(page);
1,147,084,208✔
581
  releaseBufPageInfo(pBuf, ppi);
1,144,981,975✔
582
}
583

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

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

598
  pi->used = false;
1,145,095,292✔
599
  pBuf->statis.releasePages += 1;
1,145,095,292✔
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,374,089✔
607
  if (pBuf == NULL) {
11,374,089✔
608
    return;
2,136,017✔
609
  }
610

611
  dBufPrintStatis(pBuf);
9,238,072✔
612

613
  bool needRemoveFile = false;
9,238,200✔
614
  if (pBuf->pFile != NULL) {
9,238,200✔
615
    needRemoveFile = true;
7,780✔
616
    uDebug(
7,780✔
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);
7,780✔
623
    if (TSDB_CODE_SUCCESS != code) {
7,780!
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,230,420✔
628
  }
629

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

651
  taosMemoryFreeClear(pBuf->path);
9,237,832✔
652

653
  size_t n = taosArrayGetSize(pBuf->pIdList);
9,237,832✔
654
  for (int32_t i = 0; i < n; ++i) {
39,334,190✔
655
    SPageInfo* pi = taosArrayGetP(pBuf->pIdList, i);
30,097,094✔
656
    taosMemoryFreeClear(pi->pData);
29,996,758✔
657
    taosMemoryFreeClear(pi);
30,084,030✔
658
  }
659

660
  taosArrayDestroy(pBuf->pIdList);
9,237,096✔
661

662
  pBuf->lruList = tdListFree(pBuf->lruList);
9,238,480✔
663
  pBuf->freePgList = tdListFree(pBuf->freePgList);
9,238,591✔
664

665
  taosArrayDestroy(pBuf->emptyDummyIdList);
9,238,596✔
666
  taosArrayDestroy(pBuf->pFree);
9,238,517✔
667

668
  tSimpleHashCleanup(pBuf->all);
9,238,385✔
669

670
  taosMemoryFreeClear(pBuf->id);
9,238,653!
671
  taosMemoryFreeClear(pBuf->assistBuf);
9,238,684!
672
  taosMemoryFreeClear(pBuf);
9,238,684!
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; }
666,483,825✔
684

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

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

689
void setBufPageDirty(void* pPage, bool dirty) {
680,067,010✔
690
  SPageInfo* ppi = getPageInfoFromPayload(pPage);
680,067,010✔
691
  ppi->dirty = dirty;
679,557,727✔
692
}
679,557,727✔
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,765,020✔
725

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

728
void dBufPrintStatis(const SDiskbasedBuf* pBuf) {
9,238,132✔
729
  if (!pBuf->printStatis) {
9,238,132✔
730
    return;
5,472,578✔
731
  }
732

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

754
void clearDiskbasedBuf(SDiskbasedBuf* pBuf) {
28,968✔
755
  size_t n = taosArrayGetSize(pBuf->pIdList);
28,968✔
756
  for (int32_t i = 0; i < n; ++i) {
1,318,215✔
757
    SPageInfo* pi = taosArrayGetP(pBuf->pIdList, i);
1,289,244✔
758
    taosMemoryFreeClear(pi->pData);
1,289,182✔
759
    taosMemoryFreeClear(pi);
1,289,247✔
760
  }
761

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

764
  tdListEmpty(pBuf->lruList);
28,971✔
765
  tdListEmpty(pBuf->freePgList);
28,969✔
766

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

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

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