metaCache.c 16.8 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */
#include "meta.h"

17 18
#define META_CACHE_BASE_BUCKET  1024
#define META_CACHE_STATS_BUCKET 16
H
Hongze Cheng 已提交
19 20 21 22 23 24 25 26 27 28

// (uid , suid) : child table
// (uid,     0) : normal table
// (suid, suid) : super table
typedef struct SMetaCacheEntry SMetaCacheEntry;
struct SMetaCacheEntry {
  SMetaCacheEntry* next;
  SMetaInfo        info;
};

29 30 31 32 33
typedef struct SMetaStbStatsEntry {
  struct SMetaStbStatsEntry* next;
  SMetaStbStats              info;
} SMetaStbStatsEntry;

34
typedef struct STagFilterResEntry {
dengyihao's avatar
dengyihao 已提交
35 36 37
  uint64_t suid;    // uid for super table
  SList    list;    // the linked list of md5 digest, extracted from the serialized tag query condition
  uint32_t qTimes;  // queried times for current super table
38 39
} STagFilterResEntry;

H
Hongze Cheng 已提交
40
struct SMetaCache {
41 42 43 44 45 46
  // child, normal, super, table entry cache
  struct SEntryCache {
    int32_t           nEntry;
    int32_t           nBucket;
    SMetaCacheEntry** aBucket;
  } sEntryCache;
47 48 49 50 51 52 53 54 55

  // stable stats cache
  struct SStbStatsCache {
    int32_t              nEntry;
    int32_t              nBucket;
    SMetaStbStatsEntry** aBucket;
  } sStbStatsCache;

  // query cache
56 57 58
  struct STagFilterResCache {
    SHashObj*  pTableEntry;
    SLRUCache* pUidResCache;
H
Haojun Liao 已提交
59
    uint64_t   keyBuf[3];
60
  } sTagFilterResCache;
H
Hongze Cheng 已提交
61 62
};

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
static void entryCacheClose(SMeta* pMeta) {
  if (pMeta->pCache) {
    // close entry cache
    for (int32_t iBucket = 0; iBucket < pMeta->pCache->sEntryCache.nBucket; iBucket++) {
      SMetaCacheEntry* pEntry = pMeta->pCache->sEntryCache.aBucket[iBucket];
      while (pEntry) {
        SMetaCacheEntry* tEntry = pEntry->next;
        taosMemoryFree(pEntry);
        pEntry = tEntry;
      }
    }
    taosMemoryFree(pMeta->pCache->sEntryCache.aBucket);
  }
}

static void statsCacheClose(SMeta* pMeta) {
  if (pMeta->pCache) {
    // close entry cache
    for (int32_t iBucket = 0; iBucket < pMeta->pCache->sStbStatsCache.nBucket; iBucket++) {
      SMetaStbStatsEntry* pEntry = pMeta->pCache->sStbStatsCache.aBucket[iBucket];
      while (pEntry) {
        SMetaStbStatsEntry* tEntry = pEntry->next;
        taosMemoryFree(pEntry);
        pEntry = tEntry;
      }
    }
    taosMemoryFree(pMeta->pCache->sStbStatsCache.aBucket);
  }
}

H
Haojun Liao 已提交
93 94 95 96 97 98
static void freeCacheEntryFp(void* param) {
  STagFilterResEntry** p = param;
  tdListEmpty(&(*p)->list);
  taosMemoryFreeClear(*p);
}

H
Hongze Cheng 已提交
99 100 101 102 103 104 105 106 107 108
int32_t metaCacheOpen(SMeta* pMeta) {
  int32_t     code = 0;
  SMetaCache* pCache = NULL;

  pCache = (SMetaCache*)taosMemoryMalloc(sizeof(SMetaCache));
  if (pCache == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

109 110 111 112 113 114
  // open entry cache
  pCache->sEntryCache.nEntry = 0;
  pCache->sEntryCache.nBucket = META_CACHE_BASE_BUCKET;
  pCache->sEntryCache.aBucket =
      (SMetaCacheEntry**)taosMemoryCalloc(pCache->sEntryCache.nBucket, sizeof(SMetaCacheEntry*));
  if (pCache->sEntryCache.aBucket == NULL) {
H
Hongze Cheng 已提交
115 116 117 118
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

119 120 121 122 123 124 125 126 127 128
  // open stats cache
  pCache->sStbStatsCache.nEntry = 0;
  pCache->sStbStatsCache.nBucket = META_CACHE_STATS_BUCKET;
  pCache->sStbStatsCache.aBucket =
      (SMetaStbStatsEntry**)taosMemoryCalloc(pCache->sStbStatsCache.nBucket, sizeof(SMetaStbStatsEntry*));
  if (pCache->sStbStatsCache.aBucket == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err2;
  }

dengyihao's avatar
dengyihao 已提交
129
  pCache->sTagFilterResCache.pUidResCache = taosLRUCacheInit(5 * 1024 * 1024, -1, 0.5);
H
Haojun Liao 已提交
130 131 132 133
  if (pCache->sTagFilterResCache.pUidResCache == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err2;
  }
H
Hongze Cheng 已提交
134

dengyihao's avatar
dengyihao 已提交
135 136
  pCache->sTagFilterResCache.pTableEntry =
      taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), false, HASH_NO_LOCK);
H
Haojun Liao 已提交
137 138 139 140 141
  if (pCache->sTagFilterResCache.pTableEntry == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err2;
  }

H
Haojun Liao 已提交
142
  taosHashSetFreeFp(pCache->sTagFilterResCache.pTableEntry, freeCacheEntryFp);
H
Haojun Liao 已提交
143
  pMeta->pCache = pCache;
H
Hongze Cheng 已提交
144 145
  return code;

146 147 148
_err2:
  entryCacheClose(pMeta);

H
Hongze Cheng 已提交
149
_err:
150
  taosMemoryFree(pCache);
S
Shengliang Guan 已提交
151
  metaError("vgId:%d, meta open cache failed since %s", TD_VID(pMeta->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
152 153 154 155 156
  return code;
}

void metaCacheClose(SMeta* pMeta) {
  if (pMeta->pCache) {
157 158
    entryCacheClose(pMeta);
    statsCacheClose(pMeta);
H
Haojun Liao 已提交
159 160 161

    taosHashCleanup(pMeta->pCache->sTagFilterResCache.pTableEntry);
    taosLRUCacheCleanup(pMeta->pCache->sTagFilterResCache.pUidResCache);
H
Hongze Cheng 已提交
162 163 164 165 166 167 168 169 170 171
    taosMemoryFree(pMeta->pCache);
    pMeta->pCache = NULL;
  }
}

static int32_t metaRehashCache(SMetaCache* pCache, int8_t expand) {
  int32_t code = 0;
  int32_t nBucket;

  if (expand) {
172
    nBucket = pCache->sEntryCache.nBucket * 2;
H
Hongze Cheng 已提交
173
  } else {
174
    nBucket = pCache->sEntryCache.nBucket / 2;
H
Hongze Cheng 已提交
175 176 177 178 179 180 181 182 183
  }

  SMetaCacheEntry** aBucket = (SMetaCacheEntry**)taosMemoryCalloc(nBucket, sizeof(SMetaCacheEntry*));
  if (aBucket == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  // rehash
184 185
  for (int32_t iBucket = 0; iBucket < pCache->sEntryCache.nBucket; iBucket++) {
    SMetaCacheEntry* pEntry = pCache->sEntryCache.aBucket[iBucket];
H
Hongze Cheng 已提交
186 187 188 189 190 191 192 193 194 195 196 197

    while (pEntry) {
      SMetaCacheEntry* pTEntry = pEntry->next;

      pEntry->next = aBucket[TABS(pEntry->info.uid) % nBucket];
      aBucket[TABS(pEntry->info.uid) % nBucket] = pEntry;

      pEntry = pTEntry;
    }
  }

  // final set
198 199 200
  taosMemoryFree(pCache->sEntryCache.aBucket);
  pCache->sEntryCache.nBucket = nBucket;
  pCache->sEntryCache.aBucket = aBucket;
H
Hongze Cheng 已提交
201 202 203 204 205 206 207 208

_exit:
  return code;
}

int32_t metaCacheUpsert(SMeta* pMeta, SMetaInfo* pInfo) {
  int32_t code = 0;

209
  // ASSERT(metaIsWLocked(pMeta));
H
Hongze Cheng 已提交
210 211 212

  // search
  SMetaCache*       pCache = pMeta->pCache;
213 214
  int32_t           iBucket = TABS(pInfo->uid) % pCache->sEntryCache.nBucket;
  SMetaCacheEntry** ppEntry = &pCache->sEntryCache.aBucket[iBucket];
H
Hongze Cheng 已提交
215 216 217 218 219
  while (*ppEntry && (*ppEntry)->info.uid != pInfo->uid) {
    ppEntry = &(*ppEntry)->next;
  }

  if (*ppEntry) {  // update
220
    ASSERT(pInfo->suid == (*ppEntry)->info.suid);
H
Hongze Cheng 已提交
221 222 223 224 225
    if (pInfo->version > (*ppEntry)->info.version) {
      (*ppEntry)->info.version = pInfo->version;
      (*ppEntry)->info.skmVer = pInfo->skmVer;
    }
  } else {  // insert
226
    if (pCache->sEntryCache.nEntry >= pCache->sEntryCache.nBucket) {
H
Hongze Cheng 已提交
227 228 229
      code = metaRehashCache(pCache, 1);
      if (code) goto _exit;

230
      iBucket = TABS(pInfo->uid) % pCache->sEntryCache.nBucket;
H
Hongze Cheng 已提交
231 232 233 234 235 236 237 238 239
    }

    SMetaCacheEntry* pEntryNew = (SMetaCacheEntry*)taosMemoryMalloc(sizeof(*pEntryNew));
    if (pEntryNew == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }

    pEntryNew->info = *pInfo;
240 241 242
    pEntryNew->next = pCache->sEntryCache.aBucket[iBucket];
    pCache->sEntryCache.aBucket[iBucket] = pEntryNew;
    pCache->sEntryCache.nEntry++;
H
Hongze Cheng 已提交
243 244 245 246 247 248 249 250 251 252
  }

_exit:
  return code;
}

int32_t metaCacheDrop(SMeta* pMeta, int64_t uid) {
  int32_t code = 0;

  SMetaCache*       pCache = pMeta->pCache;
253 254
  int32_t           iBucket = TABS(uid) % pCache->sEntryCache.nBucket;
  SMetaCacheEntry** ppEntry = &pCache->sEntryCache.aBucket[iBucket];
H
Hongze Cheng 已提交
255 256 257 258 259 260 261 262
  while (*ppEntry && (*ppEntry)->info.uid != uid) {
    ppEntry = &(*ppEntry)->next;
  }

  SMetaCacheEntry* pEntry = *ppEntry;
  if (pEntry) {
    *ppEntry = pEntry->next;
    taosMemoryFree(pEntry);
263 264 265
    pCache->sEntryCache.nEntry--;
    if (pCache->sEntryCache.nEntry < pCache->sEntryCache.nBucket / 4 &&
        pCache->sEntryCache.nBucket > META_CACHE_BASE_BUCKET) {
H
Hongze Cheng 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
      code = metaRehashCache(pCache, 0);
      if (code) goto _exit;
    }
  } else {
    code = TSDB_CODE_NOT_FOUND;
  }

_exit:
  return code;
}

int32_t metaCacheGet(SMeta* pMeta, int64_t uid, SMetaInfo* pInfo) {
  int32_t code = 0;

  SMetaCache*      pCache = pMeta->pCache;
281 282
  int32_t          iBucket = TABS(uid) % pCache->sEntryCache.nBucket;
  SMetaCacheEntry* pEntry = pCache->sEntryCache.aBucket[iBucket];
H
Hongze Cheng 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295

  while (pEntry && pEntry->info.uid != uid) {
    pEntry = pEntry->next;
  }

  if (pEntry) {
    *pInfo = pEntry->info;
  } else {
    code = TSDB_CODE_NOT_FOUND;
  }

  return code;
}
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338

static int32_t metaRehashStatsCache(SMetaCache* pCache, int8_t expand) {
  int32_t code = 0;
  int32_t nBucket;

  if (expand) {
    nBucket = pCache->sStbStatsCache.nBucket * 2;
  } else {
    nBucket = pCache->sStbStatsCache.nBucket / 2;
  }

  SMetaStbStatsEntry** aBucket = (SMetaStbStatsEntry**)taosMemoryCalloc(nBucket, sizeof(SMetaStbStatsEntry*));
  if (aBucket == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  // rehash
  for (int32_t iBucket = 0; iBucket < pCache->sStbStatsCache.nBucket; iBucket++) {
    SMetaStbStatsEntry* pEntry = pCache->sStbStatsCache.aBucket[iBucket];

    while (pEntry) {
      SMetaStbStatsEntry* pTEntry = pEntry->next;

      pEntry->next = aBucket[TABS(pEntry->info.uid) % nBucket];
      aBucket[TABS(pEntry->info.uid) % nBucket] = pEntry;

      pEntry = pTEntry;
    }
  }

  // final set
  taosMemoryFree(pCache->sStbStatsCache.aBucket);
  pCache->sStbStatsCache.nBucket = nBucket;
  pCache->sStbStatsCache.aBucket = aBucket;

_exit:
  return code;
}

int32_t metaStatsCacheUpsert(SMeta* pMeta, SMetaStbStats* pInfo) {
  int32_t code = 0;

339
  // ASSERT(metaIsWLocked(pMeta));
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421

  // search
  SMetaCache*          pCache = pMeta->pCache;
  int32_t              iBucket = TABS(pInfo->uid) % pCache->sStbStatsCache.nBucket;
  SMetaStbStatsEntry** ppEntry = &pCache->sStbStatsCache.aBucket[iBucket];
  while (*ppEntry && (*ppEntry)->info.uid != pInfo->uid) {
    ppEntry = &(*ppEntry)->next;
  }

  if (*ppEntry) {  // update
    (*ppEntry)->info.ctbNum = pInfo->ctbNum;
  } else {  // insert
    if (pCache->sStbStatsCache.nEntry >= pCache->sStbStatsCache.nBucket) {
      code = metaRehashStatsCache(pCache, 1);
      if (code) goto _exit;

      iBucket = TABS(pInfo->uid) % pCache->sStbStatsCache.nBucket;
    }

    SMetaStbStatsEntry* pEntryNew = (SMetaStbStatsEntry*)taosMemoryMalloc(sizeof(*pEntryNew));
    if (pEntryNew == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }

    pEntryNew->info = *pInfo;
    pEntryNew->next = pCache->sStbStatsCache.aBucket[iBucket];
    pCache->sStbStatsCache.aBucket[iBucket] = pEntryNew;
    pCache->sStbStatsCache.nEntry++;
  }

_exit:
  return code;
}

int32_t metaStatsCacheDrop(SMeta* pMeta, int64_t uid) {
  int32_t code = 0;

  SMetaCache*          pCache = pMeta->pCache;
  int32_t              iBucket = TABS(uid) % pCache->sStbStatsCache.nBucket;
  SMetaStbStatsEntry** ppEntry = &pCache->sStbStatsCache.aBucket[iBucket];
  while (*ppEntry && (*ppEntry)->info.uid != uid) {
    ppEntry = &(*ppEntry)->next;
  }

  SMetaStbStatsEntry* pEntry = *ppEntry;
  if (pEntry) {
    *ppEntry = pEntry->next;
    taosMemoryFree(pEntry);
    pCache->sStbStatsCache.nEntry--;
    if (pCache->sStbStatsCache.nEntry < pCache->sStbStatsCache.nBucket / 4 &&
        pCache->sStbStatsCache.nBucket > META_CACHE_STATS_BUCKET) {
      code = metaRehashStatsCache(pCache, 0);
      if (code) goto _exit;
    }
  } else {
    code = TSDB_CODE_NOT_FOUND;
  }

_exit:
  return code;
}

int32_t metaStatsCacheGet(SMeta* pMeta, int64_t uid, SMetaStbStats* pInfo) {
  int32_t code = TSDB_CODE_SUCCESS;

  SMetaCache*         pCache = pMeta->pCache;
  int32_t             iBucket = TABS(uid) % pCache->sStbStatsCache.nBucket;
  SMetaStbStatsEntry* pEntry = pCache->sStbStatsCache.aBucket[iBucket];

  while (pEntry && pEntry->info.uid != uid) {
    pEntry = pEntry->next;
  }

  if (pEntry) {
    *pInfo = pEntry->info;
  } else {
    code = TSDB_CODE_NOT_FOUND;
  }

  return code;
}
422

dengyihao's avatar
dengyihao 已提交
423 424
int32_t metaGetCachedTableUidList(SMeta* pMeta, tb_uid_t suid, const uint8_t* pKey, int32_t keyLen, SArray* pList1,
                                  bool* acquireRes) {
H
Haojun Liao 已提交
425 426
  uint64_t* pBuf = pMeta->pCache->sTagFilterResCache.keyBuf;

427
  // generate the composed key for LRU cache
H
Haojun Liao 已提交
428 429 430 431
  SLRUCache* pCache = pMeta->pCache->sTagFilterResCache.pUidResCache;

  pBuf[0] = suid;
  memcpy(&pBuf[1], pKey, keyLen);
432

dengyihao's avatar
dengyihao 已提交
433 434
  int32_t    len = keyLen + sizeof(uint64_t);
  LRUHandle* pHandle = taosLRUCacheLookup(pCache, pBuf, len);
H
Haojun Liao 已提交
435 436
  if (pHandle == NULL) {
    *acquireRes = 0;
437 438
    return TSDB_CODE_SUCCESS;
  } else {  // do some book mark work after acquiring the filter result from cache
H
Haojun Liao 已提交
439
    STagFilterResEntry** pEntry = taosHashGet(pMeta->pCache->sTagFilterResCache.pTableEntry, &suid, sizeof(uint64_t));
440
    ASSERT(pEntry != NULL);
H
Haojun Liao 已提交
441 442 443
    *acquireRes = 1;

    const char* p = taosLRUCacheValue(pMeta->pCache->sTagFilterResCache.pUidResCache, pHandle);
dengyihao's avatar
dengyihao 已提交
444
    int32_t     size = *(int32_t*)p;
H
Haojun Liao 已提交
445
    taosArrayAddBatch(pList1, p + sizeof(int32_t), size);
446

H
Haojun Liao 已提交
447
    (*pEntry)->qTimes += 1;
H
Haojun Liao 已提交
448
    taosLRUCacheRelease(pCache, pHandle, false);
449 450

    // check if scanning all items are necessary or not
H
Haojun Liao 已提交
451
    if ((*pEntry)->qTimes >= 5000 && TD_DLIST_NELES(&(*pEntry)->list) > 10) {
452 453 454
      SArray* pList = taosArrayInit(64, POINTER_BYTES);

      SListIter iter = {0};
H
Haojun Liao 已提交
455
      tdListInitIter(&(*pEntry)->list, &iter, TD_LIST_FORWARD);
456 457 458

      SListNode* pNode = NULL;
      while ((pNode = tdListNext(&iter)) != NULL) {
459
        memcpy(&pBuf[1], pNode->data, keyLen);
460 461

        // check whether it is existed in LRU cache, and remove it from linked list if not.
H
Haojun Liao 已提交
462
        LRUHandle* pRes = taosLRUCacheLookup(pCache, pBuf, len);
463 464
        if (pRes == NULL) {  // remove the item in the linked list
          taosArrayPush(pList, &pNode);
H
Haojun Liao 已提交
465 466
        } else {
          taosLRUCacheRelease(pCache, pRes, false);
467 468 469 470 471
        }
      }

      // remove the keys, of which query uid lists have been replaced already.
      size_t s = taosArrayGetSize(pList);
dengyihao's avatar
dengyihao 已提交
472
      for (int32_t i = 0; i < s; ++i) {
473
        SListNode** p1 = taosArrayGet(pList, i);
H
Haojun Liao 已提交
474
        tdListPopNode(&(*pEntry)->list, *p1);
dengyihao's avatar
dengyihao 已提交
475
        taosMemoryFree(*p1);
476 477
      }

dengyihao's avatar
dengyihao 已提交
478 479 480
      (*pEntry)->qTimes = 0;  // reset the query times

      taosArrayDestroy(pList);
H
Haojun Liao 已提交
481
    }
482 483 484 485 486
  }

  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
487 488 489 490 491 492 493
static void freePayload(const void* key, size_t keyLen, void* value) {
  if (value == NULL) {
    return;
  }
  taosMemoryFree(value);
}

494
// check both the payload size and selectivity ratio
dengyihao's avatar
dengyihao 已提交
495 496
int32_t metaUidFilterCachePut(SMeta* pMeta, uint64_t suid, const void* pKey, int32_t keyLen, void* pPayload,
                              int32_t payloadLen, double selectivityRatio) {
H
Haojun Liao 已提交
497
  if (selectivityRatio > tsSelectivityRatio) {
H
Haojun Liao 已提交
498 499 500
    metaDebug("vgId:%d, suid:%" PRIu64
              " failed to add to uid list cache, due to selectivity ratio %.2f less than threshold %.2f",
              TD_VID(pMeta->pVnode), suid, selectivityRatio, tsSelectivityRatio);
H
Haojun Liao 已提交
501
    taosMemoryFree(pPayload);
H
Haojun Liao 已提交
502 503 504 505
    return TSDB_CODE_SUCCESS;
  }

  if (payloadLen > tsTagFilterResCacheSize) {
H
Haojun Liao 已提交
506 507 508
    metaDebug("vgId:%d, suid:%" PRIu64
              " failed to add to uid list cache, due to payload length %d greater than threshold %d",
              TD_VID(pMeta->pVnode), suid, payloadLen, tsTagFilterResCacheSize);
H
Haojun Liao 已提交
509
    taosMemoryFree(pPayload);
H
Haojun Liao 已提交
510 511 512 513
    return TSDB_CODE_SUCCESS;
  }

  SLRUCache* pCache = pMeta->pCache->sTagFilterResCache.pUidResCache;
H
Haojun Liao 已提交
514
  SHashObj*  pTableEntry = pMeta->pCache->sTagFilterResCache.pTableEntry;
H
Haojun Liao 已提交
515

H
Haojun Liao 已提交
516
  STagFilterResEntry** pEntry = taosHashGet(pTableEntry, &suid, sizeof(uint64_t));
H
Haojun Liao 已提交
517 518 519 520
  if (pEntry == NULL) {
    STagFilterResEntry* p = taosMemoryMalloc(sizeof(STagFilterResEntry));
    p->qTimes = 0;
    tdListInit(&p->list, keyLen);
521 522 523 524
    taosHashPut(pTableEntry, &suid, sizeof(uint64_t), &p, POINTER_BYTES);
    tdListAppend(&p->list, pKey);
  } else {
    tdListAppend(&(*pEntry)->list, pKey);
H
Haojun Liao 已提交
525 526 527 528 529 530
  }

  uint64_t* pBuf = pMeta->pCache->sTagFilterResCache.keyBuf;
  pBuf[0] = suid;

  memcpy(&pBuf[1], pKey, keyLen);
531
  ASSERT(sizeof(uint64_t) + keyLen == 24);
H
Haojun Liao 已提交
532 533

  // add to cache.
dengyihao's avatar
dengyihao 已提交
534 535 536 537
  taosLRUCacheInsert(pCache, pBuf, sizeof(uint64_t) + keyLen, pPayload, payloadLen, freePayload, NULL,
                     TAOS_LRU_PRIORITY_LOW);
  metaDebug("vgId:%d, suid:%" PRIu64 " list cache added into cache, total:%d, tables:%d", TD_VID(pMeta->pVnode), suid,
            (int32_t)taosLRUCacheGetUsage(pCache), taosHashGetSize(pTableEntry));
H
Haojun Liao 已提交
538

539 540 541 542 543
  return TSDB_CODE_SUCCESS;
}

// remove the lru cache that are expired due to the tags value update, or creating, or dropping, of child tables
int32_t metaUidCacheClear(SMeta* pMeta, uint64_t suid) {
H
Haojun Liao 已提交
544 545
  STagFilterResEntry** pEntry = taosHashGet(pMeta->pCache->sTagFilterResCache.pTableEntry, &suid, sizeof(uint64_t));
  if (pEntry == NULL || listNEles(&(*pEntry)->list) == 0) {
546 547 548
    return TSDB_CODE_SUCCESS;
  }

dengyihao's avatar
dengyihao 已提交
549
  int32_t  keyLen = sizeof(uint64_t) * 3;
550 551
  uint64_t p[3] = {0};
  p[0] = suid;
552 553

  SListIter iter = {0};
H
Haojun Liao 已提交
554
  tdListInitIter(&(*pEntry)->list, &iter, TD_LIST_FORWARD);
555 556 557

  SListNode* pNode = NULL;
  while ((pNode = tdListNext(&iter)) != NULL) {
558
    memcpy(&p[1], pNode->data, 16);
559 560 561
    taosLRUCacheErase(pMeta->pCache->sTagFilterResCache.pUidResCache, p, keyLen);
  }

H
Haojun Liao 已提交
562 563
  (*pEntry)->qTimes = 0;
  tdListEmpty(&(*pEntry)->list);
H
Haojun Liao 已提交
564

565 566
  return TSDB_CODE_SUCCESS;
}