tdbPCache.c 9.3 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
H
Hongze Cheng 已提交
15 16
#include "tdbInt.h"

H
Hongze Cheng 已提交
17 18 19
#include <sys/types.h>
#include <unistd.h>

H
more  
Hongze Cheng 已提交
20
struct SPCache {
H
Hongze Cheng 已提交
21 22 23
  int         szPage;
  int         nPages;
  SPage     **aPage;
H
Hongze Cheng 已提交
24 25 26 27 28 29 30 31
  tdb_mutex_t mutex;
  int         nFree;
  SPage      *pFree;
  int         nPage;
  int         nHash;
  SPage     **pgHash;
  int         nRecyclable;
  SPage       lru;
H
Hongze Cheng 已提交
32 33
};

wafwerar's avatar
wafwerar 已提交
34 35 36
static inline uint32_t tdbPCachePageHash(const SPgid *pPgid) {
  uint32_t *t = (uint32_t *)((pPgid)->fileid);
  return (uint32_t)(t[0] + t[1] + t[2] + t[3] + t[4] + t[5] + (pPgid)->pgno);
wafwerar's avatar
wafwerar 已提交
37
}
H
Hongze Cheng 已提交
38

H
refact  
Hongze Cheng 已提交
39
static int    tdbPCacheOpenImpl(SPCache *pCache);
H
Hongze Cheng 已提交
40
static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, TXN *pTxn);
H
Hongze Cheng 已提交
41 42 43 44
static void   tdbPCachePinPage(SPCache *pCache, SPage *pPage);
static void   tdbPCacheRemovePageFromHash(SPCache *pCache, SPage *pPage);
static void   tdbPCacheAddPageToHash(SPCache *pCache, SPage *pPage);
static void   tdbPCacheUnpinPage(SPCache *pCache, SPage *pPage);
H
Hongze Cheng 已提交
45 46 47 48 49 50
static int    tdbPCacheCloseImpl(SPCache *pCache);

static void tdbPCacheInitLock(SPCache *pCache) { tdbMutexInit(&(pCache->mutex), NULL); }
static void tdbPCacheDestroyLock(SPCache *pCache) { tdbMutexDestroy(&(pCache->mutex)); }
static void tdbPCacheLock(SPCache *pCache) { tdbMutexLock(&(pCache->mutex)); }
static void tdbPCacheUnlock(SPCache *pCache) { tdbMutexUnlock(&(pCache->mutex)); }
H
Hongze Cheng 已提交
51

H
Hongze Cheng 已提交
52
int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache) {
H
more  
Hongze Cheng 已提交
53
  SPCache *pCache;
H
Hongze Cheng 已提交
54 55
  void    *pPtr;
  SPage   *pPgHdr;
H
Hongze Cheng 已提交
56

H
Hongze Cheng 已提交
57
  pCache = (SPCache *)tdbOsCalloc(1, sizeof(*pCache) + sizeof(SPage *) * cacheSize);
H
more  
Hongze Cheng 已提交
58
  if (pCache == NULL) {
H
Hongze Cheng 已提交
59 60 61
    return -1;
  }

H
Hongze Cheng 已提交
62 63 64
  pCache->szPage = pageSize;
  pCache->nPages = cacheSize;
  pCache->aPage = (SPage **)&pCache[1];
H
Hongze Cheng 已提交
65

H
more  
Hongze Cheng 已提交
66
  if (tdbPCacheOpenImpl(pCache) < 0) {
H
Hongze Cheng 已提交
67
    tdbOsFree(pCache);
H
more  
Hongze Cheng 已提交
68
    return -1;
H
Hongze Cheng 已提交
69 70
  }

H
more  
Hongze Cheng 已提交
71
  *ppCache = pCache;
H
Hongze Cheng 已提交
72 73 74
  return 0;
}

H
Hongze Cheng 已提交
75
int tdbPCacheClose(SPCache *pCache) {
H
Hongze Cheng 已提交
76 77 78 79
  if (pCache) {
    tdbPCacheCloseImpl(pCache);
    tdbOsFree(pCache);
  }
H
Hongze Cheng 已提交
80
  return 0;
H
Hongze Cheng 已提交
81 82
}

H
Hongze Cheng 已提交
83
SPage *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, TXN *pTxn) {
H
refact  
Hongze Cheng 已提交
84
  SPage *pPage;
H
Hongze Cheng 已提交
85
  i32    nRef;
H
more  
Hongze Cheng 已提交
86

H
Hongze Cheng 已提交
87
  tdbPCacheLock(pCache);
H
Hongze Cheng 已提交
88

H
Hongze Cheng 已提交
89
  pPage = tdbPCacheFetchImpl(pCache, pPgid, pTxn);
H
Hongze Cheng 已提交
90
  if (pPage) {
H
Hongze Cheng 已提交
91
    nRef = tdbRefPage(pPage);
H
Hongze Cheng 已提交
92 93
  }

H
Hongze Cheng 已提交
94 95
  ASSERT(pPage);

H
Hongze Cheng 已提交
96
  tdbPCacheUnlock(pCache);
H
more  
Hongze Cheng 已提交
97

H
Hongze Cheng 已提交
98 99 100
  // printf("thread %" PRId64 " fetch page %d pgno %d pPage %p nRef %d\n", taosGetSelfPthreadId(), pPage->id,
  //        TDB_PAGE_PGNO(pPage), pPage, nRef);

H
more  
Hongze Cheng 已提交
101
  return pPage;
H
Hongze Cheng 已提交
102 103
}

H
Hongze Cheng 已提交
104
void tdbPCacheRelease(SPCache *pCache, SPage *pPage, TXN *pTxn) {
H
Hongze Cheng 已提交
105 106
  i32 nRef;

H
Hongze Cheng 已提交
107 108
  ASSERT(pTxn);

H
Hongze Cheng 已提交
109 110
  // nRef = tdbUnrefPage(pPage);
  // ASSERT(nRef >= 0);
H
Hongze Cheng 已提交
111

H
Hongze Cheng 已提交
112 113
  tdbPCacheLock(pCache);
  nRef = tdbUnrefPage(pPage);
H
Hongze Cheng 已提交
114
  if (nRef == 0) {
H
Hongze Cheng 已提交
115 116
    // test the nRef again to make sure
    // it is safe th handle the page
H
Hongze Cheng 已提交
117 118 119 120 121 122 123 124
    // nRef = tdbGetPageRef(pPage);
    // if (nRef == 0) {
    if (pPage->isLocal) {
      tdbPCacheUnpinPage(pCache, pPage);
    } else {
      if (TDB_TXN_IS_WRITE(pTxn)) {
        // remove from hash
        tdbPCacheRemovePageFromHash(pCache, pPage);
H
Hongze Cheng 已提交
125 126
      }

H
Hongze Cheng 已提交
127 128 129
      tdbPageDestroy(pPage, pTxn->xFree, pTxn->xArg);
    }
    // }
H
more  
Hongze Cheng 已提交
130
  }
H
Hongze Cheng 已提交
131 132 133
  tdbPCacheUnlock(pCache);
  // printf("thread %" PRId64 " relas page %d pgno %d pPage %p nRef %d\n", taosGetSelfPthreadId(), pPage->id,
  //        TDB_PAGE_PGNO(pPage), pPage, nRef);
H
Hongze Cheng 已提交
134 135
}

H
Hongze Cheng 已提交
136
int tdbPCacheGetPageSize(SPCache *pCache) { return pCache->szPage; }
H
more  
Hongze Cheng 已提交
137

H
Hongze Cheng 已提交
138
static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, TXN *pTxn) {
H
Hongze Cheng 已提交
139 140 141 142 143
  int    ret = 0;
  SPage *pPage = NULL;
  SPage *pPageH = NULL;

  ASSERT(pTxn);
H
more  
Hongze Cheng 已提交
144 145

  // 1. Search the hash table
wafwerar's avatar
wafwerar 已提交
146
  pPage = pCache->pgHash[tdbPCachePageHash(pPgid) % pCache->nHash];
H
more  
Hongze Cheng 已提交
147
  while (pPage) {
H
Hongze Cheng 已提交
148
    if (memcmp(pPage->pgid.fileid, pPgid->fileid, TDB_FILE_ID_LEN) == 0 && pPage->pgid.pgno == pPgid->pgno) break;
H
more  
Hongze Cheng 已提交
149 150 151
    pPage = pPage->pHashNext;
  }

H
refact  
Hongze Cheng 已提交
152
  if (pPage) {
H
Hongze Cheng 已提交
153 154 155 156
    if (pPage->isLocal || TDB_TXN_IS_WRITE(pTxn)) {
      tdbPCachePinPage(pCache, pPage);
      return pPage;
    }
H
more  
Hongze Cheng 已提交
157 158
  }

H
Hongze Cheng 已提交
159 160 161 162 163
  // 1. pPage == NULL
  // 2. pPage && pPage->isLocal == 0 && !TDB_TXN_IS_WRITE(pTxn)
  pPageH = pPage;
  pPage = NULL;

H
more  
Hongze Cheng 已提交
164 165 166 167 168 169 170 171 172 173 174
  // 2. Try to allocate a new page from the free list
  if (pCache->pFree) {
    pPage = pCache->pFree;
    pCache->pFree = pPage->pFreeNext;
    pCache->nFree--;
    pPage->pLruNext = NULL;
  }

  // 3. Try to Recycle a page
  if (!pPage && !pCache->lru.pLruPrev->isAnchor) {
    pPage = pCache->lru.pLruPrev;
H
Hongze Cheng 已提交
175 176
    tdbPCacheRemovePageFromHash(pCache, pPage);
    tdbPCachePinPage(pCache, pPage);
H
more  
Hongze Cheng 已提交
177 178
  }

H
Hongze Cheng 已提交
179
  // 4. Try a create new page
H
Hongze Cheng 已提交
180
  if (!pPage) {
H
Hongze Cheng 已提交
181
    ret = tdbPageCreate(pCache->szPage, &pPage, pTxn->xMalloc, pTxn->xArg);
H
Hongze Cheng 已提交
182 183 184 185 186 187 188 189 190
    if (ret < 0) {
      // TODO
      ASSERT(0);
      return NULL;
    }

    // init the page fields
    pPage->isAnchor = 0;
    pPage->isLocal = 0;
H
Hongze Cheng 已提交
191 192
    pPage->nRef = 0;
    pPage->id = -1;
H
Hongze Cheng 已提交
193
  }
H
more  
Hongze Cheng 已提交
194 195 196 197 198

  // 5. Page here are just created from a free list
  // or by recycling or allocated streesly,
  // need to initialize it
  if (pPage) {
H
Hongze Cheng 已提交
199 200 201 202 203 204 205
    if (pPageH) {
      // copy the page content
      memcpy(&(pPage->pgid), pPgid, sizeof(*pPgid));
      pPage->pLruNext = NULL;
      pPage->pPager = pPageH->pPager;

      memcpy(pPage->pData, pPageH->pData, pPage->pageSize);
H
Hongze Cheng 已提交
206
      tdbPageInit(pPage, pPageH->pPageHdr - pPageH->pData, pPageH->xCellSize);
H
Hongze Cheng 已提交
207 208 209 210
      pPage->kLen = pPageH->kLen;
      pPage->vLen = pPageH->vLen;
      pPage->maxLocal = pPageH->maxLocal;
      pPage->minLocal = pPageH->minLocal;
H
Hongze Cheng 已提交
211 212 213 214 215 216 217 218 219
    } else {
      memcpy(&(pPage->pgid), pPgid, sizeof(*pPgid));
      pPage->pLruNext = NULL;
      pPage->pPager = NULL;

      if (pPage->isLocal || TDB_TXN_IS_WRITE(pTxn)) {
        tdbPCacheAddPageToHash(pCache, pPage);
      }
    }
H
more  
Hongze Cheng 已提交
220 221
  }

H
more  
Hongze Cheng 已提交
222
  return pPage;
H
more  
Hongze Cheng 已提交
223 224
}

H
Hongze Cheng 已提交
225
static void tdbPCachePinPage(SPCache *pCache, SPage *pPage) {
H
Hongze Cheng 已提交
226 227 228
  if (pPage->pLruNext != NULL) {
    ASSERT(tdbGetPageRef(pPage) == 0);

H
more  
Hongze Cheng 已提交
229 230 231 232 233
    pPage->pLruPrev->pLruNext = pPage->pLruNext;
    pPage->pLruNext->pLruPrev = pPage->pLruPrev;
    pPage->pLruNext = NULL;

    pCache->nRecyclable--;
H
Hongze Cheng 已提交
234

H
Hongze Cheng 已提交
235
    // printf("pin page %d pgno %d pPage %p\n", pPage->id, TDB_PAGE_PGNO(pPage), pPage);
H
Hongze Cheng 已提交
236
    tdbTrace("pin page %d", pPage->id);
H
more  
Hongze Cheng 已提交
237
  }
H
more  
Hongze Cheng 已提交
238 239
}

H
Hongze Cheng 已提交
240 241
static void tdbPCacheUnpinPage(SPCache *pCache, SPage *pPage) {
  i32 nRef;
H
Hongze Cheng 已提交
242

H
Hongze Cheng 已提交
243
  ASSERT(pPage->isLocal);
H
Hongze Cheng 已提交
244
  ASSERT(!pPage->isDirty);
H
Hongze Cheng 已提交
245
  ASSERT(tdbGetPageRef(pPage) == 0);
H
Hongze Cheng 已提交
246

H
Hongze Cheng 已提交
247
  ASSERT(pPage->pLruNext == NULL);
H
more  
Hongze Cheng 已提交
248

H
Hongze Cheng 已提交
249 250 251 252
  pPage->pLruPrev = &(pCache->lru);
  pPage->pLruNext = pCache->lru.pLruNext;
  pCache->lru.pLruNext->pLruPrev = pPage;
  pCache->lru.pLruNext = pPage;
H
more  
Hongze Cheng 已提交
253 254

  pCache->nRecyclable++;
H
Hongze Cheng 已提交
255

H
Hongze Cheng 已提交
256
  // printf("unpin page %d pgno %d pPage %p\n", pPage->id, TDB_PAGE_PGNO(pPage), pPage);
H
Hongze Cheng 已提交
257
  tdbTrace("unpin page %d", pPage->id);
H
more  
Hongze Cheng 已提交
258 259
}

H
Hongze Cheng 已提交
260 261 262
static void tdbPCacheRemovePageFromHash(SPCache *pCache, SPage *pPage) {
  SPage **ppPage;
  int     h;
H
more  
Hongze Cheng 已提交
263

wafwerar's avatar
wafwerar 已提交
264
  h = tdbPCachePageHash(&(pPage->pgid));
H
fix  
Hongze Cheng 已提交
265
  for (ppPage = &(pCache->pgHash[h % pCache->nHash]); (*ppPage) && *ppPage != pPage; ppPage = &((*ppPage)->pHashNext))
H
more  
Hongze Cheng 已提交
266 267
    ;

H
Hongze Cheng 已提交
268 269 270 271 272
  if (*ppPage) {
    *ppPage = pPage->pHashNext;
    pCache->nPage--;
    // printf("rmv page %d to hash, pgno %d, pPage %p\n", pPage->id, TDB_PAGE_PGNO(pPage), pPage);
  }
H
Hongze Cheng 已提交
273

H
Hongze Cheng 已提交
274
  tdbTrace("remove page %d to hash", pPage->id);
H
more  
Hongze Cheng 已提交
275 276
}

H
Hongze Cheng 已提交
277 278
static void tdbPCacheAddPageToHash(SPCache *pCache, SPage *pPage) {
  int h;
H
more  
Hongze Cheng 已提交
279

wafwerar's avatar
wafwerar 已提交
280
  h = tdbPCachePageHash(&(pPage->pgid)) % pCache->nHash;
H
more  
Hongze Cheng 已提交
281 282 283 284 285

  pPage->pHashNext = pCache->pgHash[h];
  pCache->pgHash[h] = pPage;

  pCache->nPage++;
H
Hongze Cheng 已提交
286

H
Hongze Cheng 已提交
287
  // printf("add page %d to hash, pgno %d, pPage %p\n", pPage->id, TDB_PAGE_PGNO(pPage), pPage);
H
Hongze Cheng 已提交
288
  tdbTrace("add page %d to hash", pPage->id);
H
more  
Hongze Cheng 已提交
289 290 291
}

static int tdbPCacheOpenImpl(SPCache *pCache) {
H
refact  
Hongze Cheng 已提交
292
  SPage *pPage;
H
Hongze Cheng 已提交
293
  u8    *pPtr;
H
refact  
Hongze Cheng 已提交
294
  int    tsize;
H
Hongze Cheng 已提交
295
  int    ret;
H
more  
Hongze Cheng 已提交
296 297 298 299 300 301

  tdbPCacheInitLock(pCache);

  // Open the free list
  pCache->nFree = 0;
  pCache->pFree = NULL;
H
Hongze Cheng 已提交
302 303
  for (int i = 0; i < pCache->nPages; i++) {
    ret = tdbPageCreate(pCache->szPage, &pPage, tdbDefaultMalloc, NULL);
H
Hongze Cheng 已提交
304 305
    if (ret < 0) {
      // TODO: handle error
H
more  
Hongze Cheng 已提交
306 307 308 309 310
      return -1;
    }

    // pPage->pgid = 0;
    pPage->isAnchor = 0;
H
Hongze Cheng 已提交
311
    pPage->isLocal = 1;
H
Hongze Cheng 已提交
312
    pPage->nRef = 0;
H
more  
Hongze Cheng 已提交
313 314 315
    pPage->pHashNext = NULL;
    pPage->pLruNext = NULL;
    pPage->pLruPrev = NULL;
H
more  
Hongze Cheng 已提交
316
    pPage->pDirtyNext = NULL;
H
more  
Hongze Cheng 已提交
317

H
Hongze Cheng 已提交
318
    // add page to free list
H
more  
Hongze Cheng 已提交
319 320 321
    pPage->pFreeNext = pCache->pFree;
    pCache->pFree = pPage;
    pCache->nFree++;
H
Hongze Cheng 已提交
322 323

    // add to local list
H
Hongze Cheng 已提交
324 325
    pPage->id = i;
    pCache->aPage[i] = pPage;
H
more  
Hongze Cheng 已提交
326 327 328 329
  }

  // Open the hash table
  pCache->nPage = 0;
H
Hongze Cheng 已提交
330
  pCache->nHash = pCache->nPages < 8 ? 8 : pCache->nPages;
H
Hongze Cheng 已提交
331
  pCache->pgHash = (SPage **)tdbOsCalloc(pCache->nHash, sizeof(SPage *));
H
more  
Hongze Cheng 已提交
332 333 334 335 336 337 338 339 340 341 342 343
  if (pCache->pgHash == NULL) {
    // TODO
    return -1;
  }

  // Open LRU list
  pCache->nRecyclable = 0;
  pCache->lru.isAnchor = 1;
  pCache->lru.pLruNext = &(pCache->lru);
  pCache->lru.pLruPrev = &(pCache->lru);

  return 0;
H
Hongze Cheng 已提交
344 345
}

H
Hongze Cheng 已提交
346
static int tdbPCacheCloseImpl(SPCache *pCache) {
H
Hongze Cheng 已提交
347 348
  for (i32 iPage = 0; iPage < pCache->nPages; iPage++) {
    if (pCache->aPage[iPage]) {
H
Hongze Cheng 已提交
349
      tdbPageDestroy(pCache->aPage[iPage], tdbDefaultFree, NULL);
H
Hongze Cheng 已提交
350 351
      pCache->aPage[iPage] = NULL;
    }
H
Hongze Cheng 已提交
352 353
  }

H
Hongze Cheng 已提交
354
  tdbOsFree(pCache->pgHash);
H
Hongze Cheng 已提交
355
  tdbPCacheDestroyLock(pCache);
wmmhello's avatar
wmmhello 已提交
356
  return 0;
H
Hongze Cheng 已提交
357
}