提交 8f61346d 编写于 作者: M Minglei Jin

meta/cache: separate entry cache to parepare for a new stats cache

上级 f0a74302
...@@ -26,9 +26,12 @@ struct SMetaCacheEntry { ...@@ -26,9 +26,12 @@ struct SMetaCacheEntry {
}; };
struct SMetaCache { struct SMetaCache {
int32_t nEntry; // child, normal, super, table entry cache
int32_t nBucket; struct SEntryCache {
SMetaCacheEntry** aBucket; int32_t nEntry;
int32_t nBucket;
SMetaCacheEntry** aBucket;
} sEntryCache;
}; };
int32_t metaCacheOpen(SMeta* pMeta) { int32_t metaCacheOpen(SMeta* pMeta) {
...@@ -41,10 +44,12 @@ int32_t metaCacheOpen(SMeta* pMeta) { ...@@ -41,10 +44,12 @@ int32_t metaCacheOpen(SMeta* pMeta) {
goto _err; goto _err;
} }
pCache->nEntry = 0; // open entry cache
pCache->nBucket = META_CACHE_BASE_BUCKET; pCache->sEntryCache.nEntry = 0;
pCache->aBucket = (SMetaCacheEntry**)taosMemoryCalloc(pCache->nBucket, sizeof(SMetaCacheEntry*)); pCache->sEntryCache.nBucket = META_CACHE_BASE_BUCKET;
if (pCache->aBucket == NULL) { pCache->sEntryCache.aBucket =
(SMetaCacheEntry**)taosMemoryCalloc(pCache->sEntryCache.nBucket, sizeof(SMetaCacheEntry*));
if (pCache->sEntryCache.aBucket == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY; code = TSDB_CODE_OUT_OF_MEMORY;
taosMemoryFree(pCache); taosMemoryFree(pCache);
goto _err; goto _err;
...@@ -62,15 +67,16 @@ _err: ...@@ -62,15 +67,16 @@ _err:
void metaCacheClose(SMeta* pMeta) { void metaCacheClose(SMeta* pMeta) {
if (pMeta->pCache) { if (pMeta->pCache) {
for (int32_t iBucket = 0; iBucket < pMeta->pCache->nBucket; iBucket++) { // close entry cache
SMetaCacheEntry* pEntry = pMeta->pCache->aBucket[iBucket]; for (int32_t iBucket = 0; iBucket < pMeta->pCache->sEntryCache.nBucket; iBucket++) {
SMetaCacheEntry* pEntry = pMeta->pCache->sEntryCache.aBucket[iBucket];
while (pEntry) { while (pEntry) {
SMetaCacheEntry* tEntry = pEntry->next; SMetaCacheEntry* tEntry = pEntry->next;
taosMemoryFree(pEntry); taosMemoryFree(pEntry);
pEntry = tEntry; pEntry = tEntry;
} }
} }
taosMemoryFree(pMeta->pCache->aBucket); taosMemoryFree(pMeta->pCache->sEntryCache.aBucket);
taosMemoryFree(pMeta->pCache); taosMemoryFree(pMeta->pCache);
pMeta->pCache = NULL; pMeta->pCache = NULL;
} }
...@@ -81,9 +87,9 @@ static int32_t metaRehashCache(SMetaCache* pCache, int8_t expand) { ...@@ -81,9 +87,9 @@ static int32_t metaRehashCache(SMetaCache* pCache, int8_t expand) {
int32_t nBucket; int32_t nBucket;
if (expand) { if (expand) {
nBucket = pCache->nBucket * 2; nBucket = pCache->sEntryCache.nBucket * 2;
} else { } else {
nBucket = pCache->nBucket / 2; nBucket = pCache->sEntryCache.nBucket / 2;
} }
SMetaCacheEntry** aBucket = (SMetaCacheEntry**)taosMemoryCalloc(nBucket, sizeof(SMetaCacheEntry*)); SMetaCacheEntry** aBucket = (SMetaCacheEntry**)taosMemoryCalloc(nBucket, sizeof(SMetaCacheEntry*));
...@@ -93,8 +99,8 @@ static int32_t metaRehashCache(SMetaCache* pCache, int8_t expand) { ...@@ -93,8 +99,8 @@ static int32_t metaRehashCache(SMetaCache* pCache, int8_t expand) {
} }
// rehash // rehash
for (int32_t iBucket = 0; iBucket < pCache->nBucket; iBucket++) { for (int32_t iBucket = 0; iBucket < pCache->sEntryCache.nBucket; iBucket++) {
SMetaCacheEntry* pEntry = pCache->aBucket[iBucket]; SMetaCacheEntry* pEntry = pCache->sEntryCache.aBucket[iBucket];
while (pEntry) { while (pEntry) {
SMetaCacheEntry* pTEntry = pEntry->next; SMetaCacheEntry* pTEntry = pEntry->next;
...@@ -107,9 +113,9 @@ static int32_t metaRehashCache(SMetaCache* pCache, int8_t expand) { ...@@ -107,9 +113,9 @@ static int32_t metaRehashCache(SMetaCache* pCache, int8_t expand) {
} }
// final set // final set
taosMemoryFree(pCache->aBucket); taosMemoryFree(pCache->sEntryCache.aBucket);
pCache->nBucket = nBucket; pCache->sEntryCache.nBucket = nBucket;
pCache->aBucket = aBucket; pCache->sEntryCache.aBucket = aBucket;
_exit: _exit:
return code; return code;
...@@ -122,8 +128,8 @@ int32_t metaCacheUpsert(SMeta* pMeta, SMetaInfo* pInfo) { ...@@ -122,8 +128,8 @@ int32_t metaCacheUpsert(SMeta* pMeta, SMetaInfo* pInfo) {
// search // search
SMetaCache* pCache = pMeta->pCache; SMetaCache* pCache = pMeta->pCache;
int32_t iBucket = TABS(pInfo->uid) % pCache->nBucket; int32_t iBucket = TABS(pInfo->uid) % pCache->sEntryCache.nBucket;
SMetaCacheEntry** ppEntry = &pCache->aBucket[iBucket]; SMetaCacheEntry** ppEntry = &pCache->sEntryCache.aBucket[iBucket];
while (*ppEntry && (*ppEntry)->info.uid != pInfo->uid) { while (*ppEntry && (*ppEntry)->info.uid != pInfo->uid) {
ppEntry = &(*ppEntry)->next; ppEntry = &(*ppEntry)->next;
} }
...@@ -135,11 +141,11 @@ int32_t metaCacheUpsert(SMeta* pMeta, SMetaInfo* pInfo) { ...@@ -135,11 +141,11 @@ int32_t metaCacheUpsert(SMeta* pMeta, SMetaInfo* pInfo) {
(*ppEntry)->info.skmVer = pInfo->skmVer; (*ppEntry)->info.skmVer = pInfo->skmVer;
} }
} else { // insert } else { // insert
if (pCache->nEntry >= pCache->nBucket) { if (pCache->sEntryCache.nEntry >= pCache->sEntryCache.nBucket) {
code = metaRehashCache(pCache, 1); code = metaRehashCache(pCache, 1);
if (code) goto _exit; if (code) goto _exit;
iBucket = TABS(pInfo->uid) % pCache->nBucket; iBucket = TABS(pInfo->uid) % pCache->sEntryCache.nBucket;
} }
SMetaCacheEntry* pEntryNew = (SMetaCacheEntry*)taosMemoryMalloc(sizeof(*pEntryNew)); SMetaCacheEntry* pEntryNew = (SMetaCacheEntry*)taosMemoryMalloc(sizeof(*pEntryNew));
...@@ -149,9 +155,9 @@ int32_t metaCacheUpsert(SMeta* pMeta, SMetaInfo* pInfo) { ...@@ -149,9 +155,9 @@ int32_t metaCacheUpsert(SMeta* pMeta, SMetaInfo* pInfo) {
} }
pEntryNew->info = *pInfo; pEntryNew->info = *pInfo;
pEntryNew->next = pCache->aBucket[iBucket]; pEntryNew->next = pCache->sEntryCache.aBucket[iBucket];
pCache->aBucket[iBucket] = pEntryNew; pCache->sEntryCache.aBucket[iBucket] = pEntryNew;
pCache->nEntry++; pCache->sEntryCache.nEntry++;
} }
_exit: _exit:
...@@ -162,8 +168,8 @@ int32_t metaCacheDrop(SMeta* pMeta, int64_t uid) { ...@@ -162,8 +168,8 @@ int32_t metaCacheDrop(SMeta* pMeta, int64_t uid) {
int32_t code = 0; int32_t code = 0;
SMetaCache* pCache = pMeta->pCache; SMetaCache* pCache = pMeta->pCache;
int32_t iBucket = TABS(uid) % pCache->nBucket; int32_t iBucket = TABS(uid) % pCache->sEntryCache.nBucket;
SMetaCacheEntry** ppEntry = &pCache->aBucket[iBucket]; SMetaCacheEntry** ppEntry = &pCache->sEntryCache.aBucket[iBucket];
while (*ppEntry && (*ppEntry)->info.uid != uid) { while (*ppEntry && (*ppEntry)->info.uid != uid) {
ppEntry = &(*ppEntry)->next; ppEntry = &(*ppEntry)->next;
} }
...@@ -172,8 +178,9 @@ int32_t metaCacheDrop(SMeta* pMeta, int64_t uid) { ...@@ -172,8 +178,9 @@ int32_t metaCacheDrop(SMeta* pMeta, int64_t uid) {
if (pEntry) { if (pEntry) {
*ppEntry = pEntry->next; *ppEntry = pEntry->next;
taosMemoryFree(pEntry); taosMemoryFree(pEntry);
pCache->nEntry--; pCache->sEntryCache.nEntry--;
if (pCache->nEntry < pCache->nBucket / 4 && pCache->nBucket > META_CACHE_BASE_BUCKET) { if (pCache->sEntryCache.nEntry < pCache->sEntryCache.nBucket / 4 &&
pCache->sEntryCache.nBucket > META_CACHE_BASE_BUCKET) {
code = metaRehashCache(pCache, 0); code = metaRehashCache(pCache, 0);
if (code) goto _exit; if (code) goto _exit;
} }
...@@ -189,8 +196,8 @@ int32_t metaCacheGet(SMeta* pMeta, int64_t uid, SMetaInfo* pInfo) { ...@@ -189,8 +196,8 @@ int32_t metaCacheGet(SMeta* pMeta, int64_t uid, SMetaInfo* pInfo) {
int32_t code = 0; int32_t code = 0;
SMetaCache* pCache = pMeta->pCache; SMetaCache* pCache = pMeta->pCache;
int32_t iBucket = TABS(uid) % pCache->nBucket; int32_t iBucket = TABS(uid) % pCache->sEntryCache.nBucket;
SMetaCacheEntry* pEntry = pCache->aBucket[iBucket]; SMetaCacheEntry* pEntry = pCache->sEntryCache.aBucket[iBucket];
while (pEntry && pEntry->info.uid != uid) { while (pEntry && pEntry->info.uid != uid) {
pEntry = pEntry->next; pEntry = pEntry->next;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册