metaCache.c 19.6 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 {
X
Xiaoyu Wang 已提交
35
  SList    list;      // the linked list of md5 digest, extracted from the serialized tag query condition
36 37
  uint32_t hitTimes;  // queried times for current super table
  uint32_t accTime;
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
  struct STagFilterResCache {
H
Haojun Liao 已提交
57
    TdThreadMutex lock;
X
Xiaoyu Wang 已提交
58
    uint32_t      accTimes;
59 60
    SHashObj*     pTableEntry;
    SLRUCache*    pUidResCache;
61
  } sTagFilterResCache;
H
Hongze Cheng 已提交
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 93
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 已提交
94 95 96 97 98 99
static void freeCacheEntryFp(void* param) {
  STagFilterResEntry** p = param;
  tdListEmpty(&(*p)->list);
  taosMemoryFreeClear(*p);
}

H
Hongze Cheng 已提交
100 101 102 103 104 105 106 107 108 109
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;
  }

110 111 112 113 114 115
  // 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 已提交
116 117 118 119
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

120 121 122 123 124 125 126 127 128 129
  // 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 已提交
130
  pCache->sTagFilterResCache.pUidResCache = taosLRUCacheInit(5 * 1024 * 1024, -1, 0.5);
H
Haojun Liao 已提交
131 132 133 134
  if (pCache->sTagFilterResCache.pUidResCache == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err2;
  }
H
Hongze Cheng 已提交
135

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

H
Haojun Liao 已提交
144
  taosHashSetFreeFp(pCache->sTagFilterResCache.pTableEntry, freeCacheEntryFp);
H
Haojun Liao 已提交
145 146
  taosThreadMutexInit(&pCache->sTagFilterResCache.lock, NULL);

H
Haojun Liao 已提交
147
  pMeta->pCache = pCache;
H
Hongze Cheng 已提交
148 149
  return code;

150 151 152
_err2:
  entryCacheClose(pMeta);

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

void metaCacheClose(SMeta* pMeta) {
  if (pMeta->pCache) {
161 162
    entryCacheClose(pMeta);
    statsCacheClose(pMeta);
H
Haojun Liao 已提交
163 164

    taosLRUCacheCleanup(pMeta->pCache->sTagFilterResCache.pUidResCache);
H
Haojun Liao 已提交
165
    taosThreadMutexDestroy(&pMeta->pCache->sTagFilterResCache.lock);
H
Haojun Liao 已提交
166
    taosHashCleanup(pMeta->pCache->sTagFilterResCache.pTableEntry);
H
Haojun Liao 已提交
167

H
Hongze Cheng 已提交
168 169 170 171 172 173 174 175 176 177
    taosMemoryFree(pMeta->pCache);
    pMeta->pCache = NULL;
  }
}

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

  if (expand) {
178
    nBucket = pCache->sEntryCache.nBucket * 2;
H
Hongze Cheng 已提交
179
  } else {
180
    nBucket = pCache->sEntryCache.nBucket / 2;
H
Hongze Cheng 已提交
181 182 183 184 185 186 187 188 189
  }

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

  // rehash
190 191
  for (int32_t iBucket = 0; iBucket < pCache->sEntryCache.nBucket; iBucket++) {
    SMetaCacheEntry* pEntry = pCache->sEntryCache.aBucket[iBucket];
H
Hongze Cheng 已提交
192 193 194 195 196 197 198 199 200 201 202 203

    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
204 205 206
  taosMemoryFree(pCache->sEntryCache.aBucket);
  pCache->sEntryCache.nBucket = nBucket;
  pCache->sEntryCache.aBucket = aBucket;
H
Hongze Cheng 已提交
207 208 209 210 211 212 213 214

_exit:
  return code;
}

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

215
  // meta is wlocked for calling this func.
H
Hongze Cheng 已提交
216 217 218

  // search
  SMetaCache*       pCache = pMeta->pCache;
219 220
  int32_t           iBucket = TABS(pInfo->uid) % pCache->sEntryCache.nBucket;
  SMetaCacheEntry** ppEntry = &pCache->sEntryCache.aBucket[iBucket];
H
Hongze Cheng 已提交
221 222 223 224 225
  while (*ppEntry && (*ppEntry)->info.uid != pInfo->uid) {
    ppEntry = &(*ppEntry)->next;
  }

  if (*ppEntry) {  // update
226 227 228 229
    if (pInfo->suid != (*ppEntry)->info.suid) {
      metaError("meta/cache: suid should be same as the one in cache.");
      return TSDB_CODE_FAILED;
    }
H
Hongze Cheng 已提交
230 231 232 233 234
    if (pInfo->version > (*ppEntry)->info.version) {
      (*ppEntry)->info.version = pInfo->version;
      (*ppEntry)->info.skmVer = pInfo->skmVer;
    }
  } else {  // insert
235
    if (pCache->sEntryCache.nEntry >= pCache->sEntryCache.nBucket) {
H
Hongze Cheng 已提交
236 237 238
      code = metaRehashCache(pCache, 1);
      if (code) goto _exit;

239
      iBucket = TABS(pInfo->uid) % pCache->sEntryCache.nBucket;
H
Hongze Cheng 已提交
240 241 242 243 244 245 246 247 248
    }

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

    pEntryNew->info = *pInfo;
249 250 251
    pEntryNew->next = pCache->sEntryCache.aBucket[iBucket];
    pCache->sEntryCache.aBucket[iBucket] = pEntryNew;
    pCache->sEntryCache.nEntry++;
H
Hongze Cheng 已提交
252 253 254 255 256 257 258 259 260 261
  }

_exit:
  return code;
}

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

  SMetaCache*       pCache = pMeta->pCache;
262 263
  int32_t           iBucket = TABS(uid) % pCache->sEntryCache.nBucket;
  SMetaCacheEntry** ppEntry = &pCache->sEntryCache.aBucket[iBucket];
H
Hongze Cheng 已提交
264 265 266 267 268 269 270 271
  while (*ppEntry && (*ppEntry)->info.uid != uid) {
    ppEntry = &(*ppEntry)->next;
  }

  SMetaCacheEntry* pEntry = *ppEntry;
  if (pEntry) {
    *ppEntry = pEntry->next;
    taosMemoryFree(pEntry);
272 273 274
    pCache->sEntryCache.nEntry--;
    if (pCache->sEntryCache.nEntry < pCache->sEntryCache.nBucket / 4 &&
        pCache->sEntryCache.nBucket > META_CACHE_BASE_BUCKET) {
H
Hongze Cheng 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
      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;
290 291
  int32_t          iBucket = TABS(uid) % pCache->sEntryCache.nBucket;
  SMetaCacheEntry* pEntry = pCache->sEntryCache.aBucket[iBucket];
H
Hongze Cheng 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304

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

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

  return code;
}
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 339 340 341 342 343 344 345 346 347

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;

348
  // meta is wlocked for calling this func.
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 422 423 424 425 426 427 428 429 430

  // 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;
}
431

X
Xiaoyu Wang 已提交
432 433
static int checkAllEntriesInCache(const STagFilterResEntry* pEntry, SArray* pInvalidRes, int32_t keyLen,
                                  SLRUCache* pCache, uint64_t suid) {
434 435 436 437
  SListIter iter = {0};
  tdListInitIter((SList*)&(pEntry->list), &iter, TD_LIST_FORWARD);

  SListNode* pNode = NULL;
X
Xiaoyu Wang 已提交
438
  uint64_t   buf[3];
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
  buf[0] = suid;

  int32_t len = sizeof(uint64_t) * tListLen(buf);

  while ((pNode = tdListNext(&iter)) != NULL) {
    memcpy(&buf[1], pNode->data, keyLen);

    // check whether it is existed in LRU cache, and remove it from linked list if not.
    LRUHandle* pRes = taosLRUCacheLookup(pCache, buf, len);
    if (pRes == NULL) {  // remove the item in the linked list
      taosArrayPush(pInvalidRes, &pNode);
    } else {
      taosLRUCacheRelease(pCache, pRes, false);
    }
  }

  return 0;
}

dengyihao's avatar
dengyihao 已提交
458 459
int32_t metaGetCachedTableUidList(SMeta* pMeta, tb_uid_t suid, const uint8_t* pKey, int32_t keyLen, SArray* pList1,
                                  bool* acquireRes) {
460
  // generate the composed key for LRU cache
H
Haojun Liao 已提交
461 462 463
  SLRUCache*     pCache = pMeta->pCache->sTagFilterResCache.pUidResCache;
  SHashObj*      pTableMap = pMeta->pCache->sTagFilterResCache.pTableEntry;
  TdThreadMutex* pLock = &pMeta->pCache->sTagFilterResCache.lock;
H
Haojun Liao 已提交
464

465
  uint64_t buf[4];
H
Haojun Liao 已提交
466

H
Haojun Liao 已提交
467
  *acquireRes = 0;
468

X
Xiaoyu Wang 已提交
469
  buf[0] = (uint64_t)pTableMap;
470 471
  buf[1] = suid;
  memcpy(&buf[2], pKey, keyLen);
472

H
Haojun Liao 已提交
473
  taosThreadMutexLock(pLock);
474
  pMeta->pCache->sTagFilterResCache.accTimes += 1;
H
Haojun Liao 已提交
475

476
  int32_t    len = keyLen + sizeof(uint64_t) * 2;
H
Haojun Liao 已提交
477
  LRUHandle* pHandle = taosLRUCacheLookup(pCache, buf, len);
H
Haojun Liao 已提交
478
  if (pHandle == NULL) {
H
Haojun Liao 已提交
479
    taosThreadMutexUnlock(pLock);
480
    return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
481
  }
482

H
Haojun Liao 已提交
483 484
  // do some book mark work after acquiring the filter result from cache
  STagFilterResEntry** pEntry = taosHashGet(pTableMap, &suid, sizeof(uint64_t));
485 486 487 488 489
  if (NULL == pEntry) {
    metaError("meta/cache: pEntry should not be NULL.");
    return TSDB_CODE_FAILED;
  }

H
Haojun Liao 已提交
490 491 492 493 494 495 496 497
  *acquireRes = 1;

  const char* p = taosLRUCacheValue(pCache, pHandle);
  int32_t     size = *(int32_t*)p;

  // set the result into the buffer
  taosArrayAddBatch(pList1, p + sizeof(int32_t), size);

498 499
  (*pEntry)->hitTimes += 1;

H
Haojun Liao 已提交
500
  uint32_t acc = pMeta->pCache->sTagFilterResCache.accTimes;
H
Haojun Liao 已提交
501 502
  if ((*pEntry)->hitTimes % 5000 == 0 && (*pEntry)->hitTimes > 0) {
    metaInfo("cache hit:%d, total acc:%d, rate:%.2f", (*pEntry)->hitTimes, acc, ((double)(*pEntry)->hitTimes) / acc);
503 504
  }

H
Haojun Liao 已提交
505 506 507
  taosLRUCacheRelease(pCache, pHandle, false);

  // unlock meta
H
Haojun Liao 已提交
508
  taosThreadMutexUnlock(pLock);
509 510 511
  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
512 513 514 515
static void freePayload(const void* key, size_t keyLen, void* value) {
  if (value == NULL) {
    return;
  }
516 517 518

  const uint64_t* p = key;
  if (keyLen != sizeof(int64_t) * 4) {
519
    metaError("key length is invalid, length:%d, expect:%d", (int32_t)keyLen, (int32_t)sizeof(uint64_t) * 2);
520 521
    return;
  }
522

523
  SHashObj*            pHashObj = (SHashObj*)p[0];
524
  STagFilterResEntry** pEntry = taosHashGet(pHashObj, &p[1], sizeof(uint64_t));
dengyihao's avatar
dengyihao 已提交
525

526 527
  {
    int64_t st = taosGetTimestampUs();
H
Haojun Liao 已提交
528 529

    SListIter iter = {0};
530
    tdListInitIter((SList*)&((*pEntry)->list), &iter, TD_LIST_FORWARD);
H
Haojun Liao 已提交
531 532 533

    SListNode* pNode = NULL;
    while ((pNode = tdListNext(&iter)) != NULL) {
534 535
      uint64_t* digest = (uint64_t*)pNode->data;
      if (digest[0] == p[2] && digest[1] == p[3]) {
536 537
        void* tmp = tdListPopNode(&((*pEntry)->list), pNode);
        taosMemoryFree(tmp);
538 539

        int64_t et = taosGetTimestampUs();
540
        metaInfo("clear items in cache, remain cached item:%d, elapsed time:%.2fms", listNEles(&((*pEntry)->list)),
541 542
                 (et - st) / 1000.0);
        break;
H
Haojun Liao 已提交
543 544
      }
    }
545 546
  }

H
Haojun Liao 已提交
547
  taosMemoryFree(value);
548 549
}

550 551 552 553
static int32_t addNewEntry(SHashObj* pTableEntry, const void* pKey, int32_t keyLen, uint64_t suid) {
  STagFilterResEntry* p = taosMemoryMalloc(sizeof(STagFilterResEntry));
  if (p == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
H
Haojun Liao 已提交
554
  }
555

556
  p->hitTimes = 0;
557 558 559 560
  tdListInit(&p->list, keyLen);
  taosHashPut(pTableEntry, &suid, sizeof(uint64_t), &p, POINTER_BYTES);
  tdListAppend(&p->list, pKey);
  return 0;
H
Haojun Liao 已提交
561 562
}

563
// check both the payload size and selectivity ratio
dengyihao's avatar
dengyihao 已提交
564 565
int32_t metaUidFilterCachePut(SMeta* pMeta, uint64_t suid, const void* pKey, int32_t keyLen, void* pPayload,
                              int32_t payloadLen, double selectivityRatio) {
H
Haojun Liao 已提交
566
  if (selectivityRatio > tsSelectivityRatio) {
H
Haojun Liao 已提交
567 568 569
    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 已提交
570
    taosMemoryFree(pPayload);
H
Haojun Liao 已提交
571 572 573 574
    return TSDB_CODE_SUCCESS;
  }

  if (payloadLen > tsTagFilterResCacheSize) {
H
Haojun Liao 已提交
575 576 577
    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 已提交
578
    taosMemoryFree(pPayload);
H
Haojun Liao 已提交
579 580 581
    return TSDB_CODE_SUCCESS;
  }

H
Haojun Liao 已提交
582 583 584
  SLRUCache*     pCache = pMeta->pCache->sTagFilterResCache.pUidResCache;
  SHashObj*      pTableEntry = pMeta->pCache->sTagFilterResCache.pTableEntry;
  TdThreadMutex* pLock = &pMeta->pCache->sTagFilterResCache.lock;
H
Haojun Liao 已提交
585

586 587 588 589
  // the format of key:
  // hash table address(8bytes) + suid(8bytes) + MD5 digest(16bytes)

  uint64_t buf[4] = {0};
X
Xiaoyu Wang 已提交
590
  buf[0] = (uint64_t)pTableEntry;
591 592 593
  buf[1] = suid;
  memcpy(&buf[2], pKey, keyLen);
  ASSERT(keyLen == 16);
594

595
  int32_t code = 0;
H
Haojun Liao 已提交
596
  taosThreadMutexLock(pLock);
H
Haojun Liao 已提交
597

H
Haojun Liao 已提交
598
  STagFilterResEntry** pEntry = taosHashGet(pTableEntry, &suid, sizeof(uint64_t));
H
Haojun Liao 已提交
599
  if (pEntry == NULL) {
600 601 602 603
    code = addNewEntry(pTableEntry, pKey, keyLen, suid);
    if (code != TSDB_CODE_SUCCESS) {
      goto _end;
    }
604
  } else {
605
    // check if it exists or not
H
Haojun Liao 已提交
606 607 608
    size_t size = listNEles(&(*pEntry)->list);
    if (size == 0) {
      tdListAppend(&(*pEntry)->list, pKey);
609
    } else {
H
Haojun Liao 已提交
610
      SListNode* pNode = listHead(&(*pEntry)->list);
611
      uint64_t*  p = (uint64_t*)pNode->data;
H
Haojun Liao 已提交
612
      if (p[1] == ((uint64_t*)pKey)[1] && p[0] == ((uint64_t*)pKey)[0]) {
613
        // we have already found the existed items, no need to added to cache anymore.
614 615
        taosThreadMutexUnlock(pLock);
        return TSDB_CODE_SUCCESS;
616
      } else {  // not equal, append it
H
Haojun Liao 已提交
617 618 619
        tdListAppend(&(*pEntry)->list, pKey);
      }
    }
620
  }
H
Haojun Liao 已提交
621 622

  // add to cache.
X
Xiaoyu Wang 已提交
623
  taosLRUCacheInsert(pCache, buf, sizeof(uint64_t) * 2 + keyLen, pPayload, payloadLen, freePayload, NULL,
dengyihao's avatar
dengyihao 已提交
624
                     TAOS_LRU_PRIORITY_LOW);
X
Xiaoyu Wang 已提交
625
_end:
H
Haojun Liao 已提交
626
  taosThreadMutexUnlock(pLock);
H
Haojun Liao 已提交
627

dengyihao's avatar
dengyihao 已提交
628 629
  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 已提交
630

631
  return code;
632 633 634 635
}

// 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 已提交
636
  int32_t  keyLen = sizeof(uint64_t) * 3;
637 638
  uint64_t p[4] = {0};

X
Xiaoyu Wang 已提交
639
  p[0] = (uint64_t)pMeta->pCache->sTagFilterResCache.pTableEntry;
640
  p[1] = suid;
H
Haojun Liao 已提交
641 642 643 644

  TdThreadMutex* pLock = &pMeta->pCache->sTagFilterResCache.lock;

  taosThreadMutexLock(pLock);
H
Haojun Liao 已提交
645 646
  STagFilterResEntry** pEntry = taosHashGet(pMeta->pCache->sTagFilterResCache.pTableEntry, &suid, sizeof(uint64_t));
  if (pEntry == NULL || listNEles(&(*pEntry)->list) == 0) {
H
Haojun Liao 已提交
647
    taosThreadMutexUnlock(pLock);
648 649 650 651
    return TSDB_CODE_SUCCESS;
  }

  SListIter iter = {0};
H
Haojun Liao 已提交
652
  tdListInitIter(&(*pEntry)->list, &iter, TD_LIST_FORWARD);
653 654 655

  SListNode* pNode = NULL;
  while ((pNode = tdListNext(&iter)) != NULL) {
656
    memcpy(&p[2], pNode->data, 16);
657 658 659
    taosLRUCacheErase(pMeta->pCache->sTagFilterResCache.pUidResCache, p, keyLen);
  }

660
  (*pEntry)->hitTimes = 0;
H
Haojun Liao 已提交
661
  tdListEmpty(&(*pEntry)->list);
H
Haojun Liao 已提交
662

H
Haojun Liao 已提交
663
  taosThreadMutexUnlock(pLock);
664 665
  return TSDB_CODE_SUCCESS;
}