tpagedbuf.c 20.1 KB
Newer Older
S
Shengliang Guan 已提交
1
#define _DEFAULT_SOURCE
H
Haojun Liao 已提交
2
#include "tpagedbuf.h"
3 4
#include "taoserror.h"
#include "tcompression.h"
H
Haojun Liao 已提交
5
#include "thash.h"
S
Shengliang Guan 已提交
6
#include "tlog.h"
7

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

14
typedef struct SPageDiskInfo {
S
Shengliang Guan 已提交
15 16
  int64_t offset;
  int32_t length;
H
Haojun Liao 已提交
17
} SPageDiskInfo, SFreeListItem;
18

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

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

H
Hongze Cheng 已提交
49 50
  char*               id;           // for debug purpose
  bool                printStatis;  // Print statistics info when closing this buffer.
H
Haojun Liao 已提交
51
  SDiskbasedBufStatis statis;
H
Haojun Liao 已提交
52
};
53

H
Haojun Liao 已提交
54
static int32_t createDiskFile(SDiskbasedBuf* pBuf) {
H
Haojun Liao 已提交
55
  if (pBuf->path == NULL) { // prepare the file name when needed it
56 57 58 59 60
    char path[PATH_MAX] = {0};
    taosGetTmpfilePath(pBuf->prefix, "paged-buf", path);
    pBuf->path = taosMemoryStrDup(path);
  }

H
Hongze Cheng 已提交
61 62
  pBuf->pFile =
      taosOpenFile(pBuf->path, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_READ | TD_FILE_TRUNC | TD_FILE_AUTO_DEL);
63
  if (pBuf->pFile == NULL) {
64 65 66 67 68 69
    return TAOS_SYSTEM_ERROR(errno);
  }

  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
70
static char* doCompressData(void* data, int32_t srcSize, int32_t* dst, SDiskbasedBuf* pBuf) {  // do nothing
H
Haojun Liao 已提交
71
  if (!pBuf->comp) {
72 73 74 75
    *dst = srcSize;
    return data;
  }

H
Haojun Liao 已提交
76
  *dst = tsCompressString(data, srcSize, 1, pBuf->assistBuf, srcSize, ONE_STAGE_COMP, NULL, 0);
77

H
Haojun Liao 已提交
78
  memcpy(data, pBuf->assistBuf, *dst);
79 80 81
  return data;
}

S
Shengliang Guan 已提交
82
static char* doDecompressData(void* data, int32_t srcSize, int32_t* dst, SDiskbasedBuf* pBuf) {  // do nothing
H
Haojun Liao 已提交
83
  if (!pBuf->comp) {
84 85 86 87
    *dst = srcSize;
    return data;
  }

H
Haojun Liao 已提交
88
  *dst = tsDecompressString(data, srcSize, 1, pBuf->assistBuf, pBuf->pageSize, ONE_STAGE_COMP, NULL, 0);
89
  if (*dst > 0) {
H
Haojun Liao 已提交
90
    memcpy(data, pBuf->assistBuf, *dst);
91 92 93 94
  }
  return data;
}

95
static uint64_t allocateNewPositionInFile(SDiskbasedBuf* pBuf, size_t size) {
H
Haojun Liao 已提交
96 97
  if (pBuf->pFree == NULL) {
    return pBuf->nextPos;
98 99 100
  } else {
    int32_t offset = -1;

H
Haojun Liao 已提交
101
    size_t num = taosArrayGetSize(pBuf->pFree);
S
Shengliang Guan 已提交
102
    for (int32_t i = 0; i < num; ++i) {
H
Haojun Liao 已提交
103
      SFreeListItem* pi = taosArrayGet(pBuf->pFree, i);
H
Haojun Liao 已提交
104
      if (pi->length >= size) {
105 106
        offset = pi->offset;
        pi->offset += (int32_t)size;
H
Haojun Liao 已提交
107
        pi->length -= (int32_t)size;
108 109 110 111 112 113

        return offset;
      }
    }

    // no available recycle space, allocate new area in file
H
Haojun Liao 已提交
114
    return pBuf->nextPos;
115 116 117
  }
}

H
Haojun Liao 已提交
118 119 120 121 122 123 124 125
/**
 *   +--------------------------+-------------------+--------------+
 *   | PTR to SPageInfo (8bytes)| Payload (PageSize)| 2 Extra Bytes|
 *   +--------------------------+-------------------+--------------+
 * @param pBuf
 * @param pg
 * @return
 */
126 127 128

static FORCE_INLINE size_t getAllocPageSize(int32_t pageSize) { return pageSize + POINTER_BYTES + sizeof(SFilePage); }

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
static int32_t doFlushBufPageImpl(SDiskbasedBuf* pBuf, int64_t offset, const char* pData, int32_t size) {
  int32_t ret = taosLSeekFile(pBuf->pFile, offset, SEEK_SET);
  if (ret == -1) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return terrno;
  }

  ret = (int32_t)taosWriteFile(pBuf->pFile, pData, size);
  if (ret != size) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return terrno;
  }

  // extend the file
  if (pBuf->fileSize < offset + size) {
    pBuf->fileSize = offset + size;
  }

  pBuf->statis.flushBytes += size;
  pBuf->statis.flushPages += 1;

  return TSDB_CODE_SUCCESS;
}

153
static char* doFlushBufPage(SDiskbasedBuf* pBuf, SPageInfo* pg) {
H
Haojun Liao 已提交
154 155 156 157 158
  if (pg->pData == NULL || pg->used) {
    uError("invalid params in paged buffer process when flushing buf to disk, %s", pBuf->id);
    terrno = TSDB_CODE_INVALID_PARA;
    return NULL;
  }
159

H
Haojun Liao 已提交
160
  int32_t size = pBuf->pageSize;
161 162 163
  int64_t offset = pg->offset;

  char* t = NULL;
164
  if ((!HAS_DATA_IN_DISK(pg)) || pg->dirty) {
165
    void* payload = GET_PAYLOAD_DATA(pg);
H
Haojun Liao 已提交
166
    t = doCompressData(payload, pBuf->pageSize, &size, pBuf);
167 168 169 170
    if (size < 0) {
      uError("failed to compress data when flushing data to disk, %s", pBuf->id);
      return NULL;
    }
H
Haojun Liao 已提交
171
  }
172 173

  // this page is flushed to disk for the first time
H
Haojun Liao 已提交
174
  if (pg->dirty) {
175
    if (!HAS_DATA_IN_DISK(pg)) {
176
      offset = allocateNewPositionInFile(pBuf, size);
H
Haojun Liao 已提交
177
      pBuf->nextPos += size;
178

179 180
      int32_t code = doFlushBufPageImpl(pBuf, offset, t, size);
      if (code != TSDB_CODE_SUCCESS) {
181 182
        return NULL;
      }
H
Haojun Liao 已提交
183 184 185 186
    } else {
      // length becomes greater, current space is not enough, allocate new place, otherwise, do nothing
      if (pg->length < size) {
        // 1. add current space to free list
187
        SPageDiskInfo dinfo = {.length = pg->length, .offset = offset};
H
Haojun Liao 已提交
188 189 190
        taosArrayPush(pBuf->pFree, &dinfo);

        // 2. allocate new position, and update the info
191
        offset = allocateNewPositionInFile(pBuf, size);
H
Haojun Liao 已提交
192 193
        pBuf->nextPos += size;
      }
194

195 196
      int32_t code = doFlushBufPageImpl(pBuf, offset, t, size);
      if (code != TSDB_CODE_SUCCESS) {
197 198
        return NULL;
      }
199
    }
200
  } else {  // NOTE: the size may be -1, the this recycle page has not been flushed to disk yet.
H
Haojun Liao 已提交
201
    size = pg->length;
202 203
  }

H
Haojun Liao 已提交
204
  char* pDataBuf = pg->pData;
H
Haojun Liao 已提交
205
  memset(pDataBuf, 0, getAllocPageSize(pBuf->pageSize));
206

207
#ifdef BUF_PAGE_DEBUG
208
  uDebug("page_flush %p, pageId:%d, offset:%d", pDataBuf, pg->pageId, offset);
209
#endif
210

211
  pg->offset = offset;
H
Haojun Liao 已提交
212
  pg->length = size;  // on disk size
H
Haojun Liao 已提交
213
  return pDataBuf;
214 215
}

216
static char* flushBufPage(SDiskbasedBuf* pBuf, SPageInfo* pg) {
217 218
  int32_t ret = TSDB_CODE_SUCCESS;

219
  if (pBuf->pFile == NULL) {
H
Haojun Liao 已提交
220
    if ((ret = createDiskFile(pBuf)) != TSDB_CODE_SUCCESS) {
221 222 223 224 225
      terrno = ret;
      return NULL;
    }
  }

226
  char* p = doFlushBufPage(pBuf, pg);
227 228
  CLEAR_BUF_PAGE_IN_MEM_FLAG(pg);

H
Haojun Liao 已提交
229 230
  pg->dirty = false;
  return p;
231 232 233
}

// load file block data in disk
H
Haojun Liao 已提交
234
static int32_t loadPageFromDisk(SDiskbasedBuf* pBuf, SPageInfo* pg) {
235 236 237 238 239
  if (pg->offset < 0 || pg->length <= 0) {
    uError("failed to load buf page from disk, offset:%"PRId64", length:%d, %s", pg->offset, pg->length, pBuf->id);
    return TSDB_CODE_INVALID_PARA;
  }

240
  int32_t ret = taosLSeekFile(pBuf->pFile, pg->offset, SEEK_SET);
241
  if (ret == -1) {
H
Haojun Liao 已提交
242 243 244 245
    ret = TAOS_SYSTEM_ERROR(errno);
    return ret;
  }

246
  void* pPage = (void*)GET_PAYLOAD_DATA(pg);
H
Haojun Liao 已提交
247
  ret = (int32_t)taosReadFile(pBuf->pFile, pPage, pg->length);
248
  if (ret != pg->length) {
H
Haojun Liao 已提交
249 250
    ret = TAOS_SYSTEM_ERROR(errno);
    return ret;
251 252
  }

H
Haojun Liao 已提交
253 254
  pBuf->statis.loadBytes += pg->length;
  pBuf->statis.loadPages += 1;
255 256

  int32_t fullSize = 0;
H
Haojun Liao 已提交
257
  doDecompressData(pPage, pg->length, &fullSize, pBuf);
H
Haojun Liao 已提交
258
  return 0;
259 260
}

261
static SPageInfo* registerNewPageInfo(SDiskbasedBuf* pBuf, int32_t pageId) {
H
Haojun Liao 已提交
262
  pBuf->numOfPages += 1;
263

wafwerar's avatar
wafwerar 已提交
264
  SPageInfo* ppi = taosMemoryMalloc(sizeof(SPageInfo));
265 266 267 268
  if (ppi == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
269 270

  ppi->pageId = pageId;
H
Hongze Cheng 已提交
271
  ppi->pData = NULL;
272 273
  ppi->offset = -1;
  ppi->length = -1;
H
Hongze Cheng 已提交
274 275 276
  ppi->used = true;
  ppi->pn = NULL;
  ppi->dirty = false;
277

278
  return *(SPageInfo**)taosArrayPush(pBuf->pIdList, &ppi);
279 280
}

H
Haojun Liao 已提交
281
static SListNode* getEldestUnrefedPage(SDiskbasedBuf* pBuf) {
282
  SListIter iter = {0};
H
Haojun Liao 已提交
283
  tdListInitIter(pBuf->lruList, &iter, TD_LIST_BACKWARD);
284 285

  SListNode* pn = NULL;
S
Shengliang Guan 已提交
286 287
  while ((pn = tdListNext(&iter)) != NULL) {
    SPageInfo* pageInfo = *(SPageInfo**)pn->data;
288 289 290

    SPageInfo* p = *(SPageInfo**)(pageInfo->pData);
    ASSERT(pageInfo->pageId >= 0 && pageInfo->pn == pn && p == pageInfo);
291 292 293 294 295 296 297 298 299

    if (!pageInfo->used) {
      break;
    }
  }

  return pn;
}

300
static char* evictBufPage(SDiskbasedBuf* pBuf) {
H
Haojun Liao 已提交
301
  SListNode* pn = getEldestUnrefedPage(pBuf);
302 303 304
  if (pn == NULL) {  // no available buffer pages now, return.
    return NULL;
  }
305

306 307
  terrno = 0;
  tdListPopNode(pBuf->lruList, pn);
308

309
  SPageInfo* d = *(SPageInfo**)pn->data;
310

311 312
  d->pn = NULL;
  taosMemoryFreeClear(pn);
313

314
  return flushBufPage(pBuf, d);
315 316
}

S
Shengliang Guan 已提交
317
static void lruListPushFront(SList* pList, SPageInfo* pi) {
318 319 320 321 322
  tdListPrepend(pList, &pi);
  SListNode* front = tdListGetHead(pList);
  pi->pn = front;
}

S
Shengliang Guan 已提交
323
static void lruListMoveToFront(SList* pList, SPageInfo* pi) {
324 325 326 327
  tdListPopNode(pList, pi->pn);
  tdListPrependNode(pList, pi->pn);
}

H
Haojun Liao 已提交
328
static SPageInfo* getPageInfoFromPayload(void* page) {
H
Hongze Cheng 已提交
329
  char* p = (char*)page - POINTER_BYTES;
H
Haojun Liao 已提交
330

331
  SPageInfo* ppi = ((SPageInfo**)p)[0];
H
Haojun Liao 已提交
332
  return ppi;
333 334
}

H
Haojun Liao 已提交
335
int32_t createDiskbasedBuf(SDiskbasedBuf** pBuf, int32_t pagesize, int32_t inMemBufSize, const char* id,
336
                           const char* dir) {
wafwerar's avatar
wafwerar 已提交
337
  *pBuf = taosMemoryCalloc(1, sizeof(SDiskbasedBuf));
H
Haojun Liao 已提交
338

H
Haojun Liao 已提交
339 340
  SDiskbasedBuf* pPBuf = *pBuf;
  if (pPBuf == NULL) {
341
    goto _error;
H
Haojun Liao 已提交
342 343
  }

344
  pPBuf->pageSize = pagesize;
H
Hongze Cheng 已提交
345
  pPBuf->numOfPages = 0;  // all pages are in buffer in the first place
H
Haojun Liao 已提交
346
  pPBuf->totalBufSize = 0;
347
  pPBuf->allocateId = -1;
H
Hongze Cheng 已提交
348 349
  pPBuf->pFile = NULL;
  pPBuf->id = strdup(id);
350 351 352
  pPBuf->fileSize = 0;
  pPBuf->pFree = taosArrayInit(4, sizeof(SFreeListItem));
  pPBuf->freePgList = tdListNew(POINTER_BYTES);
H
Haojun Liao 已提交
353 354

  // at least more than 2 pages must be in memory
H
Haojun Liao 已提交
355 356 357
  if (inMemBufSize < pagesize * 2) {
    inMemBufSize = pagesize * 2;
  }
H
Haojun Liao 已提交
358

H
Haojun Liao 已提交
359
  pPBuf->inMemPages = inMemBufSize / pagesize;  // maximum allowed pages, it is a soft limit.
H
Haojun Liao 已提交
360
  pPBuf->lruList = tdListNew(POINTER_BYTES);
H
Haojun Liao 已提交
361 362 363
  if (pPBuf->lruList == NULL) {
    goto _error;
  }
H
Haojun Liao 已提交
364 365 366

  // init id hash table
  _hash_fn_t fn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT);
367
  pPBuf->pIdList = taosArrayInit(4, POINTER_BYTES);
H
Haojun Liao 已提交
368 369 370
  if (pPBuf->pIdList == NULL) {
    goto _error;
  }
371

wafwerar's avatar
wafwerar 已提交
372
  pPBuf->assistBuf = taosMemoryMalloc(pPBuf->pageSize + 2);  // EXTRA BYTES
373 374 375 376
  if (pPBuf->assistBuf == NULL) {
    goto _error;
  }

H
Haojun Liao 已提交
377
  pPBuf->all = taosHashInit(10, fn, true, false);
378 379 380
  if (pPBuf->all == NULL) {
    goto _error;
  }
H
Haojun Liao 已提交
381

382
  pPBuf->prefix = (char*) dir;
H
Haojun Liao 已提交
383
  pPBuf->emptyDummyIdList = taosArrayInit(1, sizeof(int32_t));
H
Haojun Liao 已提交
384

385
  //  qDebug("QInfo:0x%"PRIx64" create resBuf for output, page size:%d, inmem buf pages:%d, file:%s", qId,
386
  //  pPBuf->pageSize, pPBuf->inMemPages, pPBuf->path);
H
Haojun Liao 已提交
387 388

  return TSDB_CODE_SUCCESS;
389 390 391
  _error:
  destroyDiskbasedBuf(pPBuf);
  return TSDB_CODE_OUT_OF_MEMORY;
392 393
}

394
static char* doExtractPage(SDiskbasedBuf* pBuf) {
395
  char* availablePage = NULL;
H
Haojun Liao 已提交
396
  if (NO_IN_MEM_AVAILABLE_PAGES(pBuf)) {
397
    availablePage = evictBufPage(pBuf);
398
    if (availablePage == NULL) {
399
      terrno = TSDB_CODE_OUT_OF_MEMORY;
H
Haojun Liao 已提交
400
      uWarn("no available buf pages, current:%d, max:%d, %s", listNEles(pBuf->lruList), pBuf->inMemPages, pBuf->id);
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
    }
  } else {
    availablePage = taosMemoryCalloc(1, getAllocPageSize(pBuf->pageSize));  // add extract bytes in case of zipped buffer increased.
    if (availablePage == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
    }
  }

  return availablePage;
}

void* getNewBufPage(SDiskbasedBuf* pBuf, int32_t* pageId) {
  pBuf->statis.getPages += 1;

  char* availablePage = doExtractPage(pBuf);
  if (availablePage == NULL) {
    return NULL;
H
Haojun Liao 已提交
418 419
  }

H
Haojun Liao 已提交
420 421 422
  SPageInfo* pi = NULL;
  if (listNEles(pBuf->freePgList) != 0) {
    SListNode* pItem = tdListPopHead(pBuf->freePgList);
423
    pi = *(SPageInfo**)pItem->data;
H
Haojun Liao 已提交
424
    pi->used = true;
425
    *pageId = pi->pageId;
wafwerar's avatar
wafwerar 已提交
426
    taosMemoryFreeClear(pItem);
427
  } else {  // create a new pageinfo
H
Haojun Liao 已提交
428 429 430 431
    // register new id in this group
    *pageId = (++pBuf->allocateId);

    // register page id info
432
    pi = registerNewPageInfo(pBuf, *pageId);
433 434 435
    if (pi == NULL) {
      return NULL;
    }
H
Haojun Liao 已提交
436 437 438 439 440

    // add to hash map
    taosHashPut(pBuf->all, pageId, sizeof(int32_t), &pi, POINTER_BYTES);
    pBuf->totalBufSize += pBuf->pageSize;
  }
441 442

  // add to LRU list
H
Haojun Liao 已提交
443
  lruListPushFront(pBuf->lruList, pi);
444
  pi->pData = availablePage;
445 446

  ((void**)pi->pData)[0] = pi;
447
#ifdef BUF_PAGE_DEBUG
H
Hongze Cheng 已提交
448
  uDebug("page_getNewBufPage , pi->pData:%p, pageId:%d, offset:%" PRId64, pi->pData, pi->pageId, pi->offset);
449
#endif
450 451

  return (void*)(GET_PAYLOAD_DATA(pi));
452 453
}

H
Haojun Liao 已提交
454
void* getBufPage(SDiskbasedBuf* pBuf, int32_t id) {
455
  if (id < 0) {
456 457
    terrno = TSDB_CODE_INVALID_PARA;
    uError("invalid page id:%d, %s", id, pBuf->id);
458 459 460
    return NULL;
  }

H
Haojun Liao 已提交
461
  pBuf->statis.getPages += 1;
462

H
Haojun Liao 已提交
463
  SPageInfo** pi = taosHashGet(pBuf->all, &id, sizeof(int32_t));
464 465 466 467 468
  if (pi == NULL || *pi == NULL) {
    uError("failed to locate the buffer page:%d, %s", id, pBuf->id);
    terrno = TSDB_CODE_INVALID_PARA;
    return NULL;
  }
469

470
  if (BUF_PAGE_IN_MEM(*pi)) {  // it is in memory
471
    // no need to update the LRU list if only one page exists
H
Haojun Liao 已提交
472
    if (pBuf->numOfPages == 1) {
473
      (*pi)->used = true;
474
      return (void*)(GET_PAYLOAD_DATA(*pi));
475 476
    }

S
Shengliang Guan 已提交
477
    SPageInfo** pInfo = (SPageInfo**)((*pi)->pn->data);
478 479 480 481
    if (*pInfo != *pi) {
      uError("inconsistently data in paged buffer, pInfo:%p, pi:%p, %s", *pInfo, *pi, pBuf->id);
      return NULL;
    }
482

H
Haojun Liao 已提交
483
    lruListMoveToFront(pBuf->lruList, (*pi));
484
    (*pi)->used = true;
485

486
#ifdef BUF_PAGE_DEBUG
H
Hongze Cheng 已提交
487
    uDebug("page_getBufPage1 pageId:%d, offset:%" PRId64, (*pi)->pageId, (*pi)->offset);
488
#endif
489
    return (void*)(GET_PAYLOAD_DATA(*pi));
S
Shengliang Guan 已提交
490
  } else {  // not in memory
491
    ASSERT((!BUF_PAGE_IN_MEM(*pi)) && (*pi)->pn == NULL &&
H
Hongze Cheng 已提交
492
           (((*pi)->length >= 0 && (*pi)->offset >= 0) || ((*pi)->length == -1 && (*pi)->offset == -1)));
493

494
    (*pi)->pData = doExtractPage(pBuf);
495 496

    // failed to evict buffer page, return with error code.
497 498
    if ((*pi)->pData == NULL) {
      return NULL;
499 500
    }

H
Haojun Liao 已提交
501
    // set the ptr to the new SPageInfo
502 503
    ((void**)((*pi)->pData))[0] = (*pi);

H
Haojun Liao 已提交
504
    lruListPushFront(pBuf->lruList, *pi);
505 506
    (*pi)->used = true;

507
    // some data has been flushed to disk, and needs to be loaded into buffer again.
508
    if (HAS_DATA_IN_DISK(*pi)) {
509 510
      int32_t code = loadPageFromDisk(pBuf, *pi);
      if (code != 0) {
511
        terrno = code;
512 513
        return NULL;
      }
H
Haojun Liao 已提交
514
    }
515
#ifdef BUF_PAGE_DEBUG
H
Hongze Cheng 已提交
516
    uDebug("page_getBufPage2 pageId:%d, offset:%" PRId64, (*pi)->pageId, (*pi)->offset);
517
#endif
518
    return (void*)(GET_PAYLOAD_DATA(*pi));
519 520 521
  }
}

H
Haojun Liao 已提交
522
void releaseBufPage(SDiskbasedBuf* pBuf, void* page) {
523
  if (page == NULL) {
524 525
    return;
  }
526

H
Haojun Liao 已提交
527
  SPageInfo* ppi = getPageInfoFromPayload(page);
H
Haojun Liao 已提交
528
  releaseBufPageInfo(pBuf, ppi);
529 530
}

H
Haojun Liao 已提交
531
void releaseBufPageInfo(SDiskbasedBuf* pBuf, SPageInfo* pi) {
532
#ifdef BUF_PAGE_DEBUG
H
Hongze Cheng 已提交
533
  uDebug("page_releaseBufPageInfo pageId:%d, used:%d, offset:%" PRId64, pi->pageId, pi->used, pi->offset);
534
#endif
535

H
Haojun Liao 已提交
536 537 538 539 540 541
  if (pi == NULL) {
    return;
  }

  if (pi->pData == NULL) {
    uError("pi->pData (page data) is null");
542 543 544
    return;
  }

545
  pi->used = false;
H
Haojun Liao 已提交
546
  pBuf->statis.releasePages += 1;
547 548
}

H
Haojun Liao 已提交
549
size_t getTotalBufSize(const SDiskbasedBuf* pBuf) { return (size_t)pBuf->totalBufSize; }
550

551
SArray* getDataBufPagesIdList(SDiskbasedBuf* pBuf) {
552
  return pBuf->pIdList;
553 554
}

H
Haojun Liao 已提交
555
void destroyDiskbasedBuf(SDiskbasedBuf* pBuf) {
H
Haojun Liao 已提交
556
  if (pBuf == NULL) {
557 558 559
    return;
  }

H
Haojun Liao 已提交
560
  dBufPrintStatis(pBuf);
H
Haojun Liao 已提交
561

562
  bool needRemoveFile = false;
563
  if (pBuf->pFile != NULL) {
564
    needRemoveFile = true;
S
Shengliang Guan 已提交
565 566
    uDebug(
        "Paged buffer closed, total:%.2f Kb (%d Pages), inmem size:%.2f Kb (%d Pages), file size:%.2f Kb, page "
H
Haojun Liao 已提交
567
        "size:%.2f Kb, %s",
S
Shengliang Guan 已提交
568
        pBuf->totalBufSize / 1024.0, pBuf->numOfPages, listNEles(pBuf->lruList) * pBuf->pageSize / 1024.0,
H
Haojun Liao 已提交
569
        listNEles(pBuf->lruList), pBuf->fileSize / 1024.0, pBuf->pageSize / 1024.0f, pBuf->id);
570

S
Shengliang Guan 已提交
571
    taosCloseFile(&pBuf->pFile);
572
  } else {
H
Haojun Liao 已提交
573
    uDebug("Paged buffer closed, total:%.2f Kb, no file created, %s", pBuf->totalBufSize / 1024.0, pBuf->id);
574 575
  }

H
Haojun Liao 已提交
576 577
  // print the statistics information
  {
S
Shengliang Guan 已提交
578
    SDiskbasedBufStatis* ps = &pBuf->statis;
579
    if (ps->loadPages == 0) {
H
Hongze Cheng 已提交
580 581
      uDebug("Get/Release pages:%d/%d, flushToDisk:%.2f Kb (%d Pages), loadFromDisk:%.2f Kb (%d Pages)", ps->getPages,
             ps->releasePages, ps->flushBytes / 1024.0f, ps->flushPages, ps->loadBytes / 1024.0f, ps->loadPages);
582 583
    } else {
      uDebug(
H
Haojun Liao 已提交
584
          "Get/Release pages:%d/%d, flushToDisk:%.2f Kb (%d Pages), loadFromDisk:%.2f Kb (%d Pages), avgPgSize:%.2f Kb",
585 586 587
          ps->getPages, ps->releasePages, ps->flushBytes / 1024.0f, ps->flushPages, ps->loadBytes / 1024.0f,
          ps->loadPages, ps->loadBytes / (1024.0 * ps->loadPages));
    }
H
Haojun Liao 已提交
588
  }
589

590 591 592
  if (needRemoveFile) {
    int32_t ret = taosRemoveFile(pBuf->path);
    if (ret != 0) {  // print the error and discard this error info
H
Haojun Liao 已提交
593
      uDebug("WARNING tPage remove file failed. path=%s, code:%s", pBuf->path, strerror(errno));
594
    }
595
  }
596

wafwerar's avatar
wafwerar 已提交
597
  taosMemoryFreeClear(pBuf->path);
H
Haojun Liao 已提交
598

599 600 601 602 603
  size_t n = taosArrayGetSize(pBuf->pIdList);
  for (int32_t i = 0; i < n; ++i) {
    SPageInfo* pi = taosArrayGetP(pBuf->pIdList, i);
    taosMemoryFreeClear(pi->pData);
    taosMemoryFreeClear(pi);
604 605
  }

606 607
  taosArrayDestroy(pBuf->pIdList);

H
Haojun Liao 已提交
608
  tdListFree(pBuf->lruList);
H
Haojun Liao 已提交
609 610
  tdListFree(pBuf->freePgList);

H
Haojun Liao 已提交
611
  taosArrayDestroy(pBuf->emptyDummyIdList);
H
Haojun Liao 已提交
612 613
  taosArrayDestroy(pBuf->pFree);

H
Haojun Liao 已提交
614
  taosHashCleanup(pBuf->all);
615

wafwerar's avatar
wafwerar 已提交
616 617 618
  taosMemoryFreeClear(pBuf->id);
  taosMemoryFreeClear(pBuf->assistBuf);
  taosMemoryFreeClear(pBuf);
619 620
}

621
SPageInfo* getLastPageInfo(SArray* pList) {
S
Shengliang Guan 已提交
622
  size_t     size = taosArrayGetSize(pList);
623 624 625 626
  SPageInfo* pPgInfo = taosArrayGetP(pList, size - 1);
  return pPgInfo;
}

627
int32_t getPageId(const SPageInfo* pPgInfo) {
628 629 630
  return pPgInfo->pageId;
}

S
Shengliang Guan 已提交
631
int32_t getBufPageSize(const SDiskbasedBuf* pBuf) { return pBuf->pageSize; }
H
Haojun Liao 已提交
632

S
Shengliang Guan 已提交
633
int32_t getNumOfInMemBufPages(const SDiskbasedBuf* pBuf) { return pBuf->inMemPages; }
H
Haojun Liao 已提交
634

S
Shengliang Guan 已提交
635
bool isAllDataInMemBuf(const SDiskbasedBuf* pBuf) { return pBuf->fileSize == 0; }
H
Haojun Liao 已提交
636

H
Haojun Liao 已提交
637
void setBufPageDirty(void* pPage, bool dirty) {
H
Haojun Liao 已提交
638
  SPageInfo* ppi = getPageInfoFromPayload(pPage);
H
Haojun Liao 已提交
639
  ppi->dirty = dirty;
640 641
}

642
void setBufPageCompressOnDisk(SDiskbasedBuf* pBuf, bool comp) { pBuf->comp = comp; }
H
Haojun Liao 已提交
643

644
void dBufSetBufPageRecycled(SDiskbasedBuf* pBuf, void* pPage) {
H
Haojun Liao 已提交
645 646
  SPageInfo* ppi = getPageInfoFromPayload(pPage);

647
  ppi->used = false;
H
Haojun Liao 已提交
648 649
  ppi->dirty = false;

H
Haojun Liao 已提交
650 651
  // add this pageinfo into the free page info list
  SListNode* pNode = tdListPopNode(pBuf->lruList, ppi->pn);
wafwerar's avatar
wafwerar 已提交
652 653
  taosMemoryFreeClear(ppi->pData);
  taosMemoryFreeClear(pNode);
654
  ppi->pn = NULL;
H
Haojun Liao 已提交
655 656

  tdListAppend(pBuf->freePgList, &ppi);
H
Haojun Liao 已提交
657
}
H
Haojun Liao 已提交
658

659
void dBufSetPrintInfo(SDiskbasedBuf* pBuf) { pBuf->printStatis = true; }
660

S
Shengliang Guan 已提交
661
SDiskbasedBufStatis getDBufStatis(const SDiskbasedBuf* pBuf) { return pBuf->statis; }
H
Haojun Liao 已提交
662

H
Haojun Liao 已提交
663
void dBufPrintStatis(const SDiskbasedBuf* pBuf) {
H
Haojun Liao 已提交
664 665 666 667 668 669
  if (!pBuf->printStatis) {
    return;
  }

  const SDiskbasedBufStatis* ps = &pBuf->statis;

H
Haojun Liao 已提交
670
#if 0
H
Haojun Liao 已提交
671 672
  printf(
      "Paged buffer closed, total:%.2f Kb (%d Pages), inmem size:%.2f Kb (%d Pages), file size:%.2f Kb, page size:%.2f "
H
Haojun Liao 已提交
673
      "Kb, %s\n",
H
Haojun Liao 已提交
674
      pBuf->totalBufSize / 1024.0, pBuf->numOfPages, listNEles(pBuf->lruList) * pBuf->pageSize / 1024.0,
H
Haojun Liao 已提交
675
      listNEles(pBuf->lruList), pBuf->fileSize / 1024.0, pBuf->pageSize / 1024.0f, pBuf->id);
H
Haojun Liao 已提交
676
#endif
H
Haojun Liao 已提交
677

678 679
  if (ps->loadPages > 0) {
    printf(
H
Hongze Cheng 已提交
680 681
        "Get/Release pages:%d/%d, flushToDisk:%.2f Kb (%d Pages), loadFromDisk:%.2f Kb (%d Pages), avgPageSize:%.2f "
        "Kb\n",
682 683 684
        ps->getPages, ps->releasePages, ps->flushBytes / 1024.0f, ps->flushPages, ps->loadBytes / 1024.0f,
        ps->loadPages, ps->loadBytes / (1024.0 * ps->loadPages));
  } else {
H
Haojun Liao 已提交
685
    //printf("no page loaded\n");
686
  }
687
}
5
54liuyao 已提交
688 689

void clearDiskbasedBuf(SDiskbasedBuf* pBuf) {
690 691 692 693 694
  size_t n = taosArrayGetSize(pBuf->pIdList);
  for (int32_t i = 0; i < n; ++i) {
    SPageInfo* pi = taosArrayGetP(pBuf->pIdList, i);
    taosMemoryFreeClear(pi->pData);
    taosMemoryFreeClear(pi);
5
54liuyao 已提交
695 696
  }

697
  taosArrayClear(pBuf->pIdList);
698

5
54liuyao 已提交
699 700 701 702 703 704 705 706
  tdListEmpty(pBuf->lruList);
  tdListEmpty(pBuf->freePgList);

  taosArrayClear(pBuf->emptyDummyIdList);
  taosArrayClear(pBuf->pFree);

  taosHashClear(pBuf->all);

H
Hongze Cheng 已提交
707
  pBuf->numOfPages = 0;  // all pages are in buffer in the first place
5
54liuyao 已提交
708 709 710
  pBuf->totalBufSize = 0;
  pBuf->allocateId = -1;
  pBuf->fileSize = 0;
711
}