• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4890

19 Dec 2025 11:37AM UTC coverage: 62.824% (-2.7%) from 65.487%
#4890

push

travis-ci

web-flow
feat: support TOTP code login and password expired tip (#33969)

22 of 26 new or added lines in 3 files covered. (84.62%)

1989 existing lines in 120 files now uncovered.

63068 of 100389 relevant lines covered (62.82%)

286770988.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

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

17
#ifdef TD_ENTERPRISE
18
extern const char* tkLogStb[];
19
extern const char* tkAuditStb[];
20
extern const int   tkLogStbNum;
21
extern const int   tkAuditStbNum;
22
#endif
23

24
#define TAG_FILTER_RES_KEY_LEN  32
25
#define META_CACHE_BASE_BUCKET  1024
26
#define META_CACHE_STATS_BUCKET 16
27

28
// (uid , suid) : child table
29
// (uid,     0) : normal table
30
// (suid, suid) : super table
31
typedef struct SMetaCacheEntry SMetaCacheEntry;
32
struct SMetaCacheEntry {
33
  SMetaCacheEntry* next;
34
  SMetaInfo        info;
35
};
36

37
typedef struct SMetaStbStatsEntry {
38
  struct SMetaStbStatsEntry* next;
39
  SMetaStbStats              info;
40
} SMetaStbStatsEntry;
41

42
typedef struct STagFilterResEntry {
43
  SHashObj *set;    // the set of md5 digest, extracted from the serialized tag query condition
44
  uint32_t hitTimes;  // queried times for current super table
45
} STagFilterResEntry;
46

47
typedef struct STagCondFilterEntry {
48
  SArray*   pColIds;   // SArray<col_id_t>
49
  SHashObj* set;       // SHashObj<digest, SArray<uid>>
50
  uint32_t  hitTimes;  // queried times for current tag filter condition
51
} STagCondFilterEntry;
52

53
typedef struct STagConds {
54
  SHashObj* set;       // SHashObj<tagColIdStr, STagCondFilterEntry>
55
  uint32_t  hitTimes;  // queried times for current super table
56
  uint32_t  numTagDataEntries; // total num of tag data entries in this stable
57
} STagConds;
58

59
struct SMetaCache {
60
  // child, normal, super, table entry cache
61
  struct SEntryCache {
62
    int32_t           nEntry;
63
    int32_t           nBucket;
64
    SMetaCacheEntry** aBucket;
65
  } sEntryCache;
66

67
  // stable stats cache
68
  struct SStbStatsCache {
69
    int32_t              nEntry;
70
    int32_t              nBucket;
71
    SMetaStbStatsEntry** aBucket;
72
  } sStbStatsCache;
73

74
  // query cache
75
  struct STagFilterResCache {
76
    TdThreadMutex lock;
77
    uint32_t      accTimes;
78
    SHashObj*     pTableEntry;
79
    SLRUCache*    pUidResCache;
80
  } sTagFilterResCache;
81

82
  // cache table list for tag filter conditions
83
  // that match format "tag1 = v1 AND tag2 = v2 AND ..."
84
  struct SStableTagFilterResCache {
85
    TdThreadRwlock rwlock;
86
    SHashObj*      pTableEntry;  // HashObj<suid, STagConds>
87
    // access times
88
    uint64_t       accTimes;
89
    // hit times
90
    uint64_t       hitTimes;
91
    // total num of tag data entries in all stables
92
    uint32_t       numTagDataEntries;
93
  } sStableTagFilterResCache;
94

95
  struct STbGroupResCache {
96
    TdThreadMutex lock;
97
    uint32_t      accTimes;
98
    SHashObj*     pTableEntry;
99
    SLRUCache*    pResCache;
100
  } STbGroupResCache;
101

102
  struct STbFilterCache {
103
    SHashObj* pStb;
104
    SHashObj* pStbName;
105
  } STbFilterCache;
106

107
  struct STbRefDbCache {
108
    TdThreadMutex lock;
109
    SHashObj*     pStbRefs; // key: suid, value: SHashObj<dbName, refTimes>
110
  } STbRefDbCache;
111
};
112

113
static void entryCacheClose(SMeta* pMeta) {
8,451,286✔
114
  if (pMeta->pCache) {
8,451,286✔
115
    // close entry cache
116
    for (int32_t iBucket = 0; iBucket < pMeta->pCache->sEntryCache.nBucket; iBucket++) {
2,147,483,647✔
117
      SMetaCacheEntry* pEntry = pMeta->pCache->sEntryCache.aBucket[iBucket];
2,147,483,647✔
118
      while (pEntry) {
2,147,483,647✔
119
        SMetaCacheEntry* tEntry = pEntry->next;
95,150,060✔
120
        taosMemoryFree(pEntry);
95,150,060✔
121
        pEntry = tEntry;
95,150,060✔
122
      }
123
    }
124
    taosMemoryFree(pMeta->pCache->sEntryCache.aBucket);
8,451,256✔
125
  }
126
}
8,451,286✔
127

128
static void statsCacheClose(SMeta* pMeta) {
8,451,286✔
129
  if (pMeta->pCache) {
8,451,286✔
130
    // close entry cache
131
    for (int32_t iBucket = 0; iBucket < pMeta->pCache->sStbStatsCache.nBucket; iBucket++) {
149,046,872✔
132
      SMetaStbStatsEntry* pEntry = pMeta->pCache->sStbStatsCache.aBucket[iBucket];
140,595,896✔
133
      while (pEntry) {
147,008,941✔
134
        SMetaStbStatsEntry* tEntry = pEntry->next;
6,413,355✔
135
        taosMemoryFree(pEntry);
6,413,355✔
136
        pEntry = tEntry;
6,413,355✔
137
      }
138
    }
139
    taosMemoryFree(pMeta->pCache->sStbStatsCache.aBucket);
8,451,250✔
140
  }
141
}
8,451,280✔
142

143
static void freeCacheEntryFp(void* param) {
10,120✔
144
  STagFilterResEntry** p = param;
10,120✔
145
  taosHashCleanup((*p)->set);
10,120✔
146
  taosMemoryFreeClear(*p);
10,120✔
147
}
10,120✔
148

149
static void freeTagFilterEntryFp(void* param) {
6,688✔
150
  STagCondFilterEntry** p = param;
6,688✔
151
  taosArrayDestroy((*p)->pColIds);
6,688✔
152
  taosHashCleanup((*p)->set);
6,688✔
153
  taosMemoryFreeClear(*p);
6,688✔
154
}
6,688✔
155

156
static void freeTagCondsFp(void* param) {
1,408✔
157
  STagConds** p = param;
1,408✔
158
  taosHashCleanup((*p)->set);
1,408✔
159
  taosMemoryFreeClear(*p);
1,408✔
160
}
1,408✔
161

162
static void freeRefDbFp(void* param) {
22,039✔
163
  SHashObj** p = param;
22,039✔
164
  taosHashCleanup(*p);
22,039✔
165
  *p = NULL;
22,039✔
166
}
22,039✔
167

168
int32_t metaCacheOpen(SMeta* pMeta) {
8,431,704✔
169
  int32_t code = 0;
8,431,704✔
170
  int32_t lino;
171

172
  pMeta->pCache = (SMetaCache*)taosMemoryCalloc(1, sizeof(SMetaCache));
8,431,704✔
173
  if (pMeta->pCache == NULL) {
8,446,144✔
174
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
175
  }
176

177
  // open entry cache
178
  pMeta->pCache->sEntryCache.nEntry = 0;
8,447,169✔
179
  pMeta->pCache->sEntryCache.nBucket = META_CACHE_BASE_BUCKET;
8,447,358✔
180
  pMeta->pCache->sEntryCache.aBucket =
8,452,930✔
181
      (SMetaCacheEntry**)taosMemoryCalloc(pMeta->pCache->sEntryCache.nBucket, sizeof(SMetaCacheEntry*));
8,449,321✔
182
  if (pMeta->pCache->sEntryCache.aBucket == NULL) {
8,451,286✔
183
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
184
  }
185

186
  // open stats cache
187
  pMeta->pCache->sStbStatsCache.nEntry = 0;
8,451,286✔
188
  pMeta->pCache->sStbStatsCache.nBucket = META_CACHE_STATS_BUCKET;
8,451,286✔
189
  pMeta->pCache->sStbStatsCache.aBucket =
8,452,214✔
190
      (SMetaStbStatsEntry**)taosMemoryCalloc(pMeta->pCache->sStbStatsCache.nBucket, sizeof(SMetaStbStatsEntry*));
8,450,960✔
191
  if (pMeta->pCache->sStbStatsCache.aBucket == NULL) {
8,450,960✔
192
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
193
  }
194

195
  // open tag filter cache
196
  pMeta->pCache->sTagFilterResCache.pUidResCache = taosLRUCacheInit(5 * 1024 * 1024, -1, 0.5);
8,450,570✔
197
  if (pMeta->pCache->sTagFilterResCache.pUidResCache == NULL) {
8,451,286✔
198
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
199
  }
200

201
  pMeta->pCache->sTagFilterResCache.accTimes = 0;
8,451,286✔
202
  pMeta->pCache->sTagFilterResCache.pTableEntry =
16,901,776✔
203
      taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), false, HASH_NO_LOCK);
16,900,928✔
204
  if (pMeta->pCache->sTagFilterResCache.pTableEntry == NULL) {
8,451,286✔
205
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
206
  }
207

208
  taosHashSetFreeFp(pMeta->pCache->sTagFilterResCache.pTableEntry, freeCacheEntryFp);
8,451,286✔
209
  (void)taosThreadMutexInit(&pMeta->pCache->sTagFilterResCache.lock, NULL);
8,450,490✔
210

211
  // open stable tag filter cache
212
  pMeta->pCache->sStableTagFilterResCache.accTimes = 0;
8,450,490✔
213
  pMeta->pCache->sStableTagFilterResCache.numTagDataEntries = 0;
8,450,490✔
214
  pMeta->pCache->sStableTagFilterResCache.pTableEntry =
16,901,776✔
215
    taosHashInit(1024,
16,899,336✔
216
      taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), false, HASH_NO_LOCK);
217
  if (pMeta->pCache->sStableTagFilterResCache.pTableEntry == NULL) {
8,451,286✔
218
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
219
  }
220
  taosHashSetFreeFp(
8,451,286✔
221
    pMeta->pCache->sStableTagFilterResCache.pTableEntry, freeTagCondsFp);
8,451,286✔
222

223
  TAOS_UNUSED(taosThreadRwlockInit(
8,451,286✔
224
    &pMeta->pCache->sStableTagFilterResCache.rwlock, NULL));
225

226
  // open group res cache
227
  pMeta->pCache->STbGroupResCache.pResCache = taosLRUCacheInit(5 * 1024 * 1024, -1, 0.5);
8,450,490✔
228
  if (pMeta->pCache->STbGroupResCache.pResCache == NULL) {
8,451,286✔
229
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
230
  }
231

232
  pMeta->pCache->STbGroupResCache.accTimes = 0;
8,451,286✔
233
  pMeta->pCache->STbGroupResCache.pTableEntry =
16,901,927✔
234
      taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), false, HASH_NO_LOCK);
16,900,430✔
235
  if (pMeta->pCache->STbGroupResCache.pTableEntry == NULL) {
8,450,641✔
236
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
237
  }
238

239
  taosHashSetFreeFp(pMeta->pCache->STbGroupResCache.pTableEntry, freeCacheEntryFp);
8,451,286✔
240
  (void)taosThreadMutexInit(&pMeta->pCache->STbGroupResCache.lock, NULL);
8,451,204✔
241

242
  // open filter cache
243
  pMeta->pCache->STbFilterCache.pStb =
16,902,490✔
244
      taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
16,900,764✔
245
  if (pMeta->pCache->STbFilterCache.pStb == NULL) {
8,451,286✔
246
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
247
  }
248

249
  pMeta->pCache->STbFilterCache.pStbName =
16,902,572✔
250
      taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), false, HASH_NO_LOCK);
16,900,283✔
251
  if (pMeta->pCache->STbFilterCache.pStbName == NULL) {
8,451,286✔
252
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
253
  }
254

255
  // open ref db cache
256
  pMeta->pCache->STbRefDbCache.pStbRefs =
16,902,048✔
257
      taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
16,899,677✔
258
  if (pMeta->pCache->STbRefDbCache.pStbRefs == NULL) {
8,451,286✔
259
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
260
  }
261

262
  taosHashSetFreeFp(pMeta->pCache->STbRefDbCache.pStbRefs, freeRefDbFp);
8,451,286✔
263
  (void)taosThreadMutexInit(&pMeta->pCache->STbRefDbCache.lock, NULL);
8,451,286✔
264

265

266
_exit:
8,451,286✔
267
  if (code) {
8,451,286✔
268
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
269
    metaCacheClose(pMeta);
×
270
  } else {
271
    metaDebug("vgId:%d, %s success", TD_VID(pMeta->pVnode), __func__);
8,451,286✔
272
  }
273
  return code;
8,448,883✔
274
}
275

276
void metaCacheClose(SMeta* pMeta) {
8,451,286✔
277
  if (pMeta->pCache) {
8,451,286✔
278
    entryCacheClose(pMeta);
8,451,286✔
279
    statsCacheClose(pMeta);
8,450,575✔
280

281
    taosHashClear(pMeta->pCache->sTagFilterResCache.pTableEntry);
8,451,280✔
282
    taosLRUCacheCleanup(pMeta->pCache->sTagFilterResCache.pUidResCache);
8,451,286✔
283
    (void)taosThreadMutexDestroy(&pMeta->pCache->sTagFilterResCache.lock);
8,451,286✔
284
    taosHashCleanup(pMeta->pCache->sTagFilterResCache.pTableEntry);
8,450,993✔
285

286
    (void)taosThreadRwlockDestroy(&pMeta->pCache->sStableTagFilterResCache.rwlock);
8,451,286✔
287
    taosHashCleanup(pMeta->pCache->sStableTagFilterResCache.pTableEntry);
8,450,880✔
288

289
    taosHashClear(pMeta->pCache->STbGroupResCache.pTableEntry);
8,451,286✔
290
    taosLRUCacheCleanup(pMeta->pCache->STbGroupResCache.pResCache);
8,451,286✔
291
    (void)taosThreadMutexDestroy(&pMeta->pCache->STbGroupResCache.lock);
8,451,286✔
292
    taosHashCleanup(pMeta->pCache->STbGroupResCache.pTableEntry);
8,451,286✔
293

294
    taosHashCleanup(pMeta->pCache->STbFilterCache.pStb);
8,451,286✔
295
    taosHashCleanup(pMeta->pCache->STbFilterCache.pStbName);
8,451,286✔
296

297
    taosHashClear(pMeta->pCache->STbRefDbCache.pStbRefs);
8,451,286✔
298
    (void)taosThreadMutexDestroy(&pMeta->pCache->STbRefDbCache.lock);
8,451,286✔
299
    taosHashCleanup(pMeta->pCache->STbRefDbCache.pStbRefs);
8,451,286✔
300

301
    taosMemoryFree(pMeta->pCache);
8,451,286✔
302
    pMeta->pCache = NULL;
8,451,286✔
303
  }
304
}
8,451,286✔
305

306
static void metaRehashCache(SMetaCache* pCache, int8_t expand) {
27,415✔
307
  int32_t code = 0;
27,415✔
308
  int32_t nBucket;
309

310
  if (expand) {
27,415✔
311
    nBucket = pCache->sEntryCache.nBucket * 2;
27,415✔
312
  } else {
313
    nBucket = pCache->sEntryCache.nBucket / 2;
×
314
  }
315

316
  SMetaCacheEntry** aBucket = (SMetaCacheEntry**)taosMemoryCalloc(nBucket, sizeof(SMetaCacheEntry*));
27,415✔
317
  if (aBucket == NULL) {
27,415✔
318
    return;
×
319
  }
320

321
  // rehash
322
  for (int32_t iBucket = 0; iBucket < pCache->sEntryCache.nBucket; iBucket++) {
92,587,799✔
323
    SMetaCacheEntry* pEntry = pCache->sEntryCache.aBucket[iBucket];
92,560,384✔
324

325
    while (pEntry) {
185,120,768✔
326
      SMetaCacheEntry* pTEntry = pEntry->next;
92,560,384✔
327

328
      pEntry->next = aBucket[TABS(pEntry->info.uid) % nBucket];
92,560,384✔
329
      aBucket[TABS(pEntry->info.uid) % nBucket] = pEntry;
92,560,384✔
330

331
      pEntry = pTEntry;
92,560,384✔
332
    }
333
  }
334

335
  // final set
336
  taosMemoryFree(pCache->sEntryCache.aBucket);
27,415✔
337
  pCache->sEntryCache.nBucket = nBucket;
27,415✔
338
  pCache->sEntryCache.aBucket = aBucket;
27,415✔
339
  return;
27,415✔
340
}
341

342
int32_t metaCacheUpsert(SMeta* pMeta, SMetaInfo* pInfo) {
119,041,469✔
343
  int32_t code = 0;
119,041,469✔
344

345
  // meta is wlocked for calling this func.
346

347
  // search
348
  SMetaCache*       pCache = pMeta->pCache;
119,041,469✔
349
  int32_t           iBucket = TABS(pInfo->uid) % pCache->sEntryCache.nBucket;
119,087,326✔
350
  SMetaCacheEntry** ppEntry = &pCache->sEntryCache.aBucket[iBucket];
119,052,359✔
351
  while (*ppEntry && (*ppEntry)->info.uid != pInfo->uid) {
142,002,536✔
352
    ppEntry = &(*ppEntry)->next;
22,983,175✔
353
  }
354

355
  if (*ppEntry) {  // update
119,004,702✔
356
    if (pInfo->suid != (*ppEntry)->info.suid) {
21,126,505✔
357
      metaError("meta/cache: suid should be same as the one in cache.");
×
358
      return TSDB_CODE_INVALID_PARA;
×
359
    }
360
    if (pInfo->version > (*ppEntry)->info.version) {
21,076,262✔
361
      (*ppEntry)->info.version = pInfo->version;
21,088,953✔
362
      (*ppEntry)->info.skmVer = pInfo->skmVer;
21,085,758✔
363
    }
364
  } else {  // insert
365
    if (pCache->sEntryCache.nEntry >= pCache->sEntryCache.nBucket) {
97,914,765✔
366
      metaRehashCache(pCache, 1);
27,415✔
367

368
      iBucket = TABS(pInfo->uid) % pCache->sEntryCache.nBucket;
27,415✔
369
    }
370

371
    SMetaCacheEntry* pEntryNew = (SMetaCacheEntry*)taosMemoryMalloc(sizeof(*pEntryNew));
97,943,164✔
372
    if (pEntryNew == NULL) {
97,925,049✔
373
      code = terrno;
×
374
      goto _exit;
×
375
    }
376

377
    pEntryNew->info = *pInfo;
97,925,049✔
378
    pEntryNew->next = pCache->sEntryCache.aBucket[iBucket];
97,923,974✔
379
    pCache->sEntryCache.aBucket[iBucket] = pEntryNew;
97,915,003✔
380
    pCache->sEntryCache.nEntry++;
97,907,369✔
381
  }
382

383
_exit:
119,026,103✔
384
  return code;
119,026,103✔
385
}
386

387
int32_t metaCacheDrop(SMeta* pMeta, int64_t uid) {
2,834,274✔
388
  int32_t code = 0;
2,834,274✔
389

390
  SMetaCache*       pCache = pMeta->pCache;
2,834,274✔
391
  int32_t           iBucket = TABS(uid) % pCache->sEntryCache.nBucket;
2,833,995✔
392
  SMetaCacheEntry** ppEntry = &pCache->sEntryCache.aBucket[iBucket];
2,834,664✔
393
  while (*ppEntry && (*ppEntry)->info.uid != uid) {
2,852,848✔
394
    ppEntry = &(*ppEntry)->next;
18,877✔
395
  }
396

397
  SMetaCacheEntry* pEntry = *ppEntry;
2,834,688✔
398
  if (pEntry) {
2,834,688✔
399
    *ppEntry = pEntry->next;
2,807,303✔
400
    taosMemoryFree(pEntry);
2,807,303✔
401
    pCache->sEntryCache.nEntry--;
2,806,610✔
402
    if (pCache->sEntryCache.nEntry < pCache->sEntryCache.nBucket / 4 &&
2,806,610✔
403
        pCache->sEntryCache.nBucket > META_CACHE_BASE_BUCKET) {
2,739,343✔
404
      metaRehashCache(pCache, 0);
×
405
    }
406
  } else {
407
    code = TSDB_CODE_NOT_FOUND;
27,385✔
408
  }
409

410
_exit:
2,833,258✔
411
  return code;
2,833,258✔
412
}
413

414
int32_t metaCacheGet(SMeta* pMeta, int64_t uid, SMetaInfo* pInfo) {
1,773,862,275✔
415
  int32_t code = 0;
1,773,862,275✔
416

417
  SMetaCache*      pCache = pMeta->pCache;
1,773,862,275✔
418
  int32_t          iBucket = TABS(uid) % pCache->sEntryCache.nBucket;
1,774,106,154✔
419
  SMetaCacheEntry* pEntry = pCache->sEntryCache.aBucket[iBucket];
1,774,113,270✔
420

421
  while (pEntry && pEntry->info.uid != uid) {
1,847,050,298✔
422
    pEntry = pEntry->next;
72,898,286✔
423
  }
424

425
  if (pEntry) {
1,774,150,310✔
426
    if (pInfo) {
1,667,701,828✔
427
      *pInfo = pEntry->info;
1,667,713,049✔
428
    }
429
  } else {
430
    code = TSDB_CODE_NOT_FOUND;
106,448,482✔
431
  }
432

433
  return code;
1,774,177,476✔
434
}
435

436
static int32_t metaRehashStatsCache(SMetaCache* pCache, int8_t expand) {
121,538✔
437
  int32_t code = 0;
121,538✔
438
  int32_t nBucket;
439

440
  if (expand) {
121,538✔
441
    nBucket = pCache->sStbStatsCache.nBucket * 2;
114,028✔
442
  } else {
443
    nBucket = pCache->sStbStatsCache.nBucket / 2;
7,510✔
444
  }
445

446
  SMetaStbStatsEntry** aBucket = (SMetaStbStatsEntry**)taosMemoryCalloc(nBucket, sizeof(SMetaStbStatsEntry*));
121,538✔
447
  if (aBucket == NULL) {
121,538✔
448
    code = terrno;
×
449
    goto _exit;
×
450
  }
451

452
  // rehash
453
  for (int32_t iBucket = 0; iBucket < pCache->sStbStatsCache.nBucket; iBucket++) {
6,079,282✔
454
    SMetaStbStatsEntry* pEntry = pCache->sStbStatsCache.aBucket[iBucket];
5,957,744✔
455

456
    while (pEntry) {
11,622,474✔
457
      SMetaStbStatsEntry* pTEntry = pEntry->next;
5,664,730✔
458

459
      pEntry->next = aBucket[TABS(pEntry->info.uid) % nBucket];
5,664,730✔
460
      aBucket[TABS(pEntry->info.uid) % nBucket] = pEntry;
5,664,730✔
461

462
      pEntry = pTEntry;
5,664,730✔
463
    }
464
  }
465

466
  // final set
467
  taosMemoryFree(pCache->sStbStatsCache.aBucket);
121,538✔
468
  pCache->sStbStatsCache.nBucket = nBucket;
121,538✔
469
  pCache->sStbStatsCache.aBucket = aBucket;
121,538✔
470

471
_exit:
121,538✔
472
  return code;
121,538✔
473
}
474

475
int32_t metaStatsCacheUpsert(SMeta* pMeta, SMetaStbStats* pInfo) {
68,300,357✔
476
  int32_t code = 0;
68,300,357✔
477

478
  // meta is wlocked for calling this func.
479

480
  // search
481
  SMetaCache*          pCache = pMeta->pCache;
68,300,357✔
482
  int32_t              iBucket = TABS(pInfo->uid) % pCache->sStbStatsCache.nBucket;
68,319,541✔
483
  SMetaStbStatsEntry** ppEntry = &pCache->sStbStatsCache.aBucket[iBucket];
68,306,016✔
484
  while (*ppEntry && (*ppEntry)->info.uid != pInfo->uid) {
71,546,242✔
485
    ppEntry = &(*ppEntry)->next;
3,270,290✔
486
  }
487

488
  if (*ppEntry) {  // update
68,310,113✔
489
    (*ppEntry)->info.ctbNum = pInfo->ctbNum;
61,204,466✔
490
    (*ppEntry)->info.colNum = pInfo->colNum;
61,191,019✔
491
    (*ppEntry)->info.flags = pInfo->flags;
61,193,035✔
492
    (*ppEntry)->info.keep = pInfo->keep;
61,201,440✔
493
  } else {  // insert
494
    if (pCache->sStbStatsCache.nEntry >= pCache->sStbStatsCache.nBucket) {
7,096,737✔
495
      TAOS_UNUSED(metaRehashStatsCache(pCache, 1));
114,028✔
496
      iBucket = TABS(pInfo->uid) % pCache->sStbStatsCache.nBucket;
114,028✔
497
    }
498

499
    SMetaStbStatsEntry* pEntryNew = (SMetaStbStatsEntry*)taosMemoryMalloc(sizeof(*pEntryNew));
7,092,401✔
500
    if (pEntryNew == NULL) {
7,092,421✔
501
      code = terrno;
×
502
      goto _exit;
×
503
    }
504

505
    pEntryNew->info = *pInfo;
7,092,421✔
506
    pEntryNew->next = pCache->sStbStatsCache.aBucket[iBucket];
7,092,664✔
507
    pCache->sStbStatsCache.aBucket[iBucket] = pEntryNew;
7,091,702✔
508
    pCache->sStbStatsCache.nEntry++;
7,092,264✔
509
  }
510

511
_exit:
68,263,907✔
512
  return code;
68,263,907✔
513
}
514

515
int32_t metaStatsCacheDrop(SMeta* pMeta, int64_t uid) {
1,035,758✔
516
  int32_t code = 0;
1,035,758✔
517

518
  SMetaCache*          pCache = pMeta->pCache;
1,035,758✔
519
  int32_t              iBucket = TABS(uid) % pCache->sStbStatsCache.nBucket;
1,035,758✔
520
  SMetaStbStatsEntry** ppEntry = &pCache->sStbStatsCache.aBucket[iBucket];
1,035,758✔
521
  while (*ppEntry && (*ppEntry)->info.uid != uid) {
1,179,790✔
522
    ppEntry = &(*ppEntry)->next;
144,056✔
523
  }
524

525
  SMetaStbStatsEntry* pEntry = *ppEntry;
1,035,734✔
526
  if (pEntry) {
1,035,758✔
527
    *ppEntry = pEntry->next;
679,333✔
528
    taosMemoryFree(pEntry);
679,309✔
529
    pCache->sStbStatsCache.nEntry--;
679,309✔
530
    if (pCache->sStbStatsCache.nEntry < pCache->sStbStatsCache.nBucket / 4 &&
678,616✔
531
        pCache->sStbStatsCache.nBucket > META_CACHE_STATS_BUCKET) {
433,093✔
532
      TAOS_UNUSED(metaRehashStatsCache(pCache, 0));
7,510✔
533
    }
534
  } else {
535
    code = TSDB_CODE_NOT_FOUND;
356,425✔
536
  }
537

538
_exit:
1,035,065✔
539
  return code;
1,035,065✔
540
}
541

542
int32_t metaStatsCacheGet(SMeta* pMeta, int64_t uid, SMetaStbStats* pInfo) {
266,865,959✔
543
  int32_t code = TSDB_CODE_SUCCESS;
266,865,959✔
544

545
  SMetaCache*         pCache = pMeta->pCache;
266,865,959✔
546
  int32_t             iBucket = TABS(uid) % pCache->sStbStatsCache.nBucket;
266,912,761✔
547
  SMetaStbStatsEntry* pEntry = pCache->sStbStatsCache.aBucket[iBucket];
266,891,031✔
548

549
  while (pEntry && pEntry->info.uid != uid) {
306,893,161✔
550
    pEntry = pEntry->next;
39,967,117✔
551
  }
552

553
  if (pEntry) {
266,924,721✔
554
    if (pInfo) {
248,601,113✔
555
      *pInfo = pEntry->info;
248,604,332✔
556
    }
557
  } else {
558
    code = TSDB_CODE_NOT_FOUND;
18,323,608✔
559
  }
560

561
  return code;
266,926,372✔
562
}
563

564
static FORCE_INLINE void setMD5DigestInKey(uint64_t* pBuf, const char* key, int32_t keyLen) {
565
  memcpy(&pBuf[2], key, keyLen);
143,434,079✔
566
}
143,429,547✔
567

568
// the format of key:
569
// hash table address(8bytes) + suid(8bytes) + MD5 digest(16bytes)
570
static void initCacheKey(uint64_t* buf, const SHashObj* pHashMap, uint64_t suid, const char* key, int32_t keyLen) {
143,416,953✔
571
  buf[0] = (uint64_t)pHashMap;
143,416,953✔
572
  buf[1] = suid;
143,417,474✔
573
  setMD5DigestInKey(buf, key, keyLen);
574
}
143,430,031✔
575

576
int32_t metaGetCachedTableUidList(void* pVnode, tb_uid_t suid, const uint8_t* pKey, int32_t keyLen, SArray* pList1,
66,088✔
577
                                  bool* acquireRes) {
578
  SMeta*  pMeta = ((SVnode*)pVnode)->pMeta;
66,088✔
579
  int32_t vgId = TD_VID(pMeta->pVnode);
66,088✔
580

581
  // generate the composed key for LRU cache
582
  SLRUCache*     pCache = pMeta->pCache->sTagFilterResCache.pUidResCache;
66,088✔
583
  SHashObj*      pTableMap = pMeta->pCache->sTagFilterResCache.pTableEntry;
66,088✔
584
  TdThreadMutex* pLock = &pMeta->pCache->sTagFilterResCache.lock;
66,088✔
585

586
  *acquireRes = 0;
66,088✔
587
  uint64_t key[4];
66,088✔
588
  initCacheKey(key, pTableMap, suid, (const char*)pKey, keyLen);
66,088✔
589
  
590
  // void* tmp = NULL;
591
  // uint32_t len = 0;
592
  // (void)taosAscii2Hex((const char*)key, 32, &tmp, &len);
593
  // qDebug("metaGetCachedTableUidList %p %"PRId64" key: %s", pTableMap, suid, tmp);
594
  // taosMemoryFree(tmp);
595

596
  (void)taosThreadMutexLock(pLock);
66,088✔
597
  pMeta->pCache->sTagFilterResCache.accTimes += 1;
66,088✔
598

599
  LRUHandle* pHandle = taosLRUCacheLookup(pCache, key, TAG_FILTER_RES_KEY_LEN);
66,088✔
600
  if (pHandle == NULL) {
66,088✔
601
    (void)taosThreadMutexUnlock(pLock);
15,576✔
602
    return TSDB_CODE_SUCCESS;
15,576✔
603
  }
604

605
  // do some book mark work after acquiring the filter result from cache
606
  STagFilterResEntry** pEntry = taosHashGet(pTableMap, &suid, sizeof(uint64_t));
50,512✔
607
  if (NULL == pEntry) {
50,512✔
608
    metaError("meta/cache: pEntry should not be NULL.");
×
609
    return TSDB_CODE_NOT_FOUND;
×
610
  }
611

612
  *acquireRes = 1;
50,512✔
613

614
  const char* p = taosLRUCacheValue(pCache, pHandle);
50,512✔
615
  int32_t     size = *(int32_t*)p;
50,512✔
616

617
  // set the result into the buffer
618
  if (taosArrayAddBatch(pList1, p + sizeof(int32_t), size) == NULL) {
50,512✔
619
    return terrno;
×
620
  }
621

622
  (*pEntry)->hitTimes += 1;
50,512✔
623

624
  uint32_t acc = pMeta->pCache->sTagFilterResCache.accTimes;
50,512✔
625
  if ((*pEntry)->hitTimes % 5000 == 0 && (*pEntry)->hitTimes > 0) {
50,512✔
626
    metaInfo("vgId:%d cache hit:%d, total acc:%d, rate:%.2f", vgId, (*pEntry)->hitTimes, acc,
×
627
             ((double)(*pEntry)->hitTimes) / acc);
628
  }
629

630
  bool ret = taosLRUCacheRelease(pCache, pHandle, false);
50,512✔
631

632
  // unlock meta
633
  (void)taosThreadMutexUnlock(pLock);
50,512✔
634
  return TSDB_CODE_SUCCESS;
50,512✔
635
}
636

637
int32_t metaStableTagFilterCacheGet(void* pVnode, tb_uid_t suid,
22,528✔
638
  const uint8_t* pTagCondKey, int32_t tagCondKeyLen,
639
  const uint8_t* pKey, int32_t keyLen, SArray* pList1, bool* acquireRes) {
640

641
  int32_t code = TSDB_CODE_SUCCESS;
22,528✔
642
  int32_t lino = 0;
22,528✔
643
  SMeta*  pMeta = ((SVnode*)pVnode)->pMeta;
22,528✔
644
  int32_t vgId = TD_VID(pMeta->pVnode);
22,528✔
645
  *acquireRes = 0;
22,528✔
646

647
  // generate the composed key for LRU cache
648
  SHashObj*       pTableMap = pMeta->pCache->sStableTagFilterResCache.pTableEntry;
22,528✔
649
  TdThreadRwlock* pRwlock = &pMeta->pCache->sStableTagFilterResCache.rwlock;
22,528✔
650

651
  code = taosThreadRwlockRdlock(pRwlock);
22,528✔
652
  TSDB_CHECK_CODE(code, lino, _end);
22,528✔
653
  pMeta->pCache->sStableTagFilterResCache.accTimes += 1;
22,528✔
654

655
  STagConds** pTagConds =
656
    (STagConds**)taosHashGet(pTableMap, &suid, sizeof(tb_uid_t));
22,528✔
657
  TSDB_CHECK_NULL(pTagConds, code, lino, _end, TSDB_CODE_SUCCESS);
22,528✔
658

659
  STagCondFilterEntry** pFilterEntry = (STagCondFilterEntry**)taosHashGet(
20,064✔
660
    (*pTagConds)->set, pTagCondKey, tagCondKeyLen);
20,064✔
661
  TSDB_CHECK_NULL(pFilterEntry, code, lino, _end, TSDB_CODE_SUCCESS);
20,064✔
662

663
  SArray** pArray = (SArray**)taosHashGet((*pFilterEntry)->set, pKey, keyLen);
15,136✔
664
  TSDB_CHECK_NULL(pArray, code, lino, _end, TSDB_CODE_SUCCESS);
15,136✔
665

666
  // set the result into the buffer
667
  *acquireRes = 1;
12,672✔
668
  TAOS_UNUSED(taosArrayAddBatch(
12,672✔
669
    pList1, TARRAY_GET_ELEM(*pArray, 0), taosArrayGetSize(*pArray)));
670

671
  // do some bookmark work after acquiring the filter result from cache
672
  (*pTagConds)->hitTimes += 1;
12,672✔
673
  (*pFilterEntry)->hitTimes += 1;
12,672✔
674
  uint64_t hit = ++pMeta->pCache->sStableTagFilterResCache.hitTimes;
12,672✔
675
  uint64_t acc = pMeta->pCache->sStableTagFilterResCache.accTimes;
12,672✔
676
  if ((*pTagConds)->hitTimes % 1000 == 0 && (*pTagConds)->hitTimes > 0) {
12,672✔
677
    metaDebug(
×
678
      "vgId:%d, suid:%" PRIu64 
679
      ", current stable cache hit:%" PRIu32 ", this tag condition hit:%" PRIu32
680
      ", total cache hit:%" PRIu64 ", acc:%" PRIu64 ", hit rate:%.2f%%",
681
      vgId, suid, (*pTagConds)->hitTimes, (*pFilterEntry)->hitTimes, hit, acc,
682
      ((double)hit / acc * 100));
683
  }
684

685
_end:
12,672✔
686
  if (TSDB_CODE_SUCCESS != code) {
22,528✔
687
    metaError("vgId:%d, %s failed at %s:%d since %s",
×
688
      vgId, __func__, __FILE__, lino, tstrerror(code));
689
  }
690
  // unlock meta
691
  code = taosThreadRwlockUnlock(pRwlock);
22,528✔
692
  if (TSDB_CODE_SUCCESS != code) {
22,528✔
693
    metaError("vgId:%d, %s unlock failed at %s:%d since %s",
×
694
      vgId, __func__, __FILE__, lino, tstrerror(code));
695
  }
696
  return code;
22,528✔
697
}
698

699
static void freeUidCachePayload(const void* key, size_t keyLen, void* value, void* ud) {
15,576✔
700
  (void)ud;
701
  if (value == NULL) {
15,576✔
702
    return;
×
703
  }
704

705
  const uint64_t* p = key;
15,576✔
706
  if (keyLen != sizeof(int64_t) * 4) {
15,576✔
707
    metaError("key length is invalid, length:%d, expect:%d", (int32_t)keyLen, (int32_t)sizeof(uint64_t) * 2);
×
708
    return;
×
709
  }
710

711
  SHashObj* pHashObj = (SHashObj*)p[0];
15,576✔
712

713
  STagFilterResEntry** pEntry = taosHashGet(pHashObj, &p[1], sizeof(uint64_t));
15,576✔
714

715
  if (pEntry != NULL && (*pEntry) != NULL) {
15,576✔
716
    int64_t st = taosGetTimestampUs();
4,048✔
717
    int32_t code = taosHashRemove((*pEntry)->set, &p[2], sizeof(uint64_t) * 2);
4,048✔
718
    if (code == TSDB_CODE_SUCCESS) {
4,048✔
719
      double el = (taosGetTimestampUs() - st) / 1000.0;
4,048✔
720
      metaInfo("clear items in meta-cache, remain cached item:%d, elapsed time:%.2fms", taosHashGetSize((*pEntry)->set),
4,048✔
721
               el);
722
    }
723
  }
724

725
  metaDebug("free uid cache payload in lru, suid: %" PRIu64
15,576✔
726
            " origin key:%" PRIu64 ",%" PRIu64,
727
            p[1], p[2], p[3]);
728
  taosMemoryFree(value);
15,576✔
729
}
730

731
static int32_t addNewEntry(SHashObj* pTableEntry, const void* pKey, int32_t keyLen, uint64_t suid) {
10,120✔
732
  int32_t             code = TSDB_CODE_SUCCESS;
10,120✔
733
  int32_t             lino = 0;
10,120✔
734
  STagFilterResEntry* p = taosMemoryMalloc(sizeof(STagFilterResEntry));
10,120✔
735
  TSDB_CHECK_NULL(p, code, lino, _end, terrno);
10,120✔
736

737
  p->hitTimes = 0;
10,120✔
738
  p->set = taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
10,120✔
739
  TSDB_CHECK_NULL(p->set, code, lino, _end, terrno);
10,120✔
740
  code = taosHashPut(p->set, pKey, keyLen, NULL, 0);
10,120✔
741
  TSDB_CHECK_CODE(code, lino, _end);
10,120✔
742
  code = taosHashPut(pTableEntry, &suid, sizeof(uint64_t), &p, POINTER_BYTES);
10,120✔
743
  TSDB_CHECK_CODE(code, lino, _end);
10,120✔
744

745
_end:
10,120✔
746
  if (code != TSDB_CODE_SUCCESS) {
10,120✔
747
    metaError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
748
    if (p != NULL) {
×
749
      if (p->set != NULL) {
×
750
        taosHashCleanup(p->set);
×
751
      }
752
      taosMemoryFree(p);
×
753
    }
754
  }
755
  return code;
10,120✔
756
}
757

758
// check both the payload size and selectivity ratio
759
int32_t metaUidFilterCachePut(void* pVnode, uint64_t suid, const void* pKey, int32_t keyLen, void* pPayload,
15,576✔
760
                              int32_t payloadLen, double selectivityRatio) {
761
  int32_t code = 0;
15,576✔
762
  SMeta*  pMeta = ((SVnode*)pVnode)->pMeta;
15,576✔
763
  int32_t vgId = TD_VID(pMeta->pVnode);
15,576✔
764

765
  if (selectivityRatio > tsSelectivityRatio) {
15,576✔
766
    metaDebug("vgId:%d, suid:%" PRIu64
×
767
              " failed to add to uid list cache, due to selectivity ratio %.2f less than threshold %.2f",
768
              vgId, suid, selectivityRatio, tsSelectivityRatio);
769
    taosMemoryFree(pPayload);
×
770
    return TSDB_CODE_SUCCESS;
×
771
  }
772

773
  if (payloadLen > tsTagFilterResCacheSize) {
15,576✔
774
    metaDebug("vgId:%d, suid:%" PRIu64
×
775
              " failed to add to uid list cache, due to payload length %d greater than threshold %d",
776
              vgId, suid, payloadLen, tsTagFilterResCacheSize);
777
    taosMemoryFree(pPayload);
×
778
    return TSDB_CODE_SUCCESS;
×
779
  }
780

781
  SLRUCache*     pCache = pMeta->pCache->sTagFilterResCache.pUidResCache;
15,576✔
782
  SHashObj*      pTableEntry = pMeta->pCache->sTagFilterResCache.pTableEntry;
15,576✔
783
  TdThreadMutex* pLock = &pMeta->pCache->sTagFilterResCache.lock;
15,576✔
784

785
  uint64_t key[4] = {0};
15,576✔
786
  initCacheKey(key, pTableEntry, suid, pKey, keyLen);
15,576✔
787

788
  // void* tmp = NULL;
789
  // uint32_t len = 0;
790
  // (void)taosAscii2Hex((const char*)key, 32, &tmp, &len);
791
  // qDebug("metaUidFilterCachePut %p %"PRId64" key: %s", pTableEntry, suid, tmp);
792
  // taosMemoryFree(tmp);
793

794
  (void)taosThreadMutexLock(pLock);
15,576✔
795
  STagFilterResEntry** pEntry = taosHashGet(pTableEntry, &suid, sizeof(uint64_t));
15,576✔
796
  if (pEntry == NULL) {
15,576✔
797
    code = addNewEntry(pTableEntry, pKey, keyLen, suid);
10,120✔
798
    if (code != TSDB_CODE_SUCCESS) {
10,120✔
799
      goto _end;
×
800
    }
801
  } else {  // check if it exists or not
802
    code = taosHashPut((*pEntry)->set, pKey, keyLen, NULL, 0);
5,456✔
803
    if (code == TSDB_CODE_DUP_KEY) {
5,456✔
804
      // we have already found the existed items, no need to added to cache anymore.
805
      (void)taosThreadMutexUnlock(pLock);
×
806
      return TSDB_CODE_DUP_KEY;
×
807
    }
808
    if (code != TSDB_CODE_SUCCESS) {
5,456✔
809
      goto _end;
×
810
    }
811
  }
812

813
  // add to cache.
814
  (void)taosLRUCacheInsert(pCache, key, TAG_FILTER_RES_KEY_LEN, pPayload, payloadLen, freeUidCachePayload, NULL, NULL,
15,576✔
815
                           TAOS_LRU_PRIORITY_LOW, NULL);
816
_end:
15,576✔
817
  (void)taosThreadMutexUnlock(pLock);
15,576✔
818
  metaDebug("vgId:%d, suid:%" PRIu64 " list cache added into cache, total:%d, tables:%d", vgId, suid,
15,576✔
819
            (int32_t)taosLRUCacheGetUsage(pCache), taosHashGetSize(pTableEntry));
820

821
  return code;
15,576✔
822
}
823

824
static void freeSArrayPtr(void* pp) {
9,856✔
825
  SArray* pArray = *(SArray**)pp;
9,856✔
826
  taosArrayDestroy(pArray);
9,856✔
827
}
9,856✔
828

829
int32_t metaStableTagFilterCachePut(
9,856✔
830
  void* pVnode, uint64_t suid, const void* pTagCondKey, int32_t tagCondKeyLen,
831
  const void* pKey, int32_t keyLen, SArray* pUidList, SArray** pTagColIds) {
832

833
  int32_t code = TSDB_CODE_SUCCESS;
9,856✔
834
  int32_t lino = 0;
9,856✔
835
  SMeta*  pMeta = ((SVnode*)pVnode)->pMeta;
9,856✔
836
  int32_t vgId = TD_VID(pMeta->pVnode);
9,856✔
837

838
  SHashObj*       pTableEntry = pMeta->pCache->sStableTagFilterResCache.pTableEntry;
9,856✔
839
  TdThreadRwlock* pRwlock = &pMeta->pCache->sStableTagFilterResCache.rwlock;
9,856✔
840

841
  code = taosThreadRwlockWrlock(pRwlock);
9,856✔
842
  TSDB_CHECK_CODE(code, lino, _end);
9,856✔
843

844
  STagConds** pTagConds = 
845
    (STagConds**)taosHashGet(pTableEntry, &suid, sizeof(uint64_t));
9,856✔
846
  if (pTagConds == NULL) {
9,856✔
847
    // add new (suid -> tag conds) entry
848
    STagConds* pEntry = (STagConds*)taosMemoryMalloc(sizeof(STagConds));
1,408✔
849
    TSDB_CHECK_NULL(pEntry, code, lino, _end, terrno);
1,408✔
850

851
    pEntry->hitTimes = 0;
1,408✔
852
    pEntry->set = taosHashInit(
1,408✔
853
      1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY),
854
      false, HASH_NO_LOCK);
855
    taosHashSetFreeFp(pEntry->set, freeTagFilterEntryFp);
1,408✔
856
    TSDB_CHECK_NULL(pEntry->set, code, lino, _end, terrno);
1,408✔
857

858
    code = taosHashPut(
1,408✔
859
      pTableEntry, &suid, sizeof(uint64_t), &pEntry, POINTER_BYTES);
860
    TSDB_CHECK_CODE(code, lino, _end);
1,408✔
861

862
    pTagConds = (STagConds**)taosHashGet(pTableEntry, &suid, sizeof(uint64_t));
1,408✔
863
    TSDB_CHECK_NULL(pTagConds, code, lino, _end, TSDB_CODE_NOT_FOUND);
1,408✔
864
  }
865

866
  STagCondFilterEntry** pFilterEntry =
867
    (STagCondFilterEntry**)taosHashGet(
9,856✔
868
      (*pTagConds)->set, pTagCondKey, tagCondKeyLen);
9,856✔
869
  if (pFilterEntry == NULL) {
9,856✔
870
    // add new (tag cond -> filter entry) entry
871
    STagCondFilterEntry* pEntry = 
6,688✔
872
      (STagCondFilterEntry*)taosMemoryMalloc(sizeof(STagCondFilterEntry));
6,688✔
873
    TSDB_CHECK_NULL(pEntry, code, lino, _end, terrno);
6,688✔
874

875
    pEntry->hitTimes = 0;
6,688✔
876
    pEntry->set = taosHashInit(
6,688✔
877
      1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY),
878
      false, HASH_NO_LOCK);
879
    TSDB_CHECK_NULL(pEntry->set, code, lino, _end, terrno);
6,688✔
880
    taosHashSetFreeFp(pEntry->set, freeSArrayPtr);
6,688✔
881
    pEntry->pColIds = *pTagColIds;
6,688✔
882
    *pTagColIds = NULL;
6,688✔
883

884
    code = taosHashPut(
6,688✔
885
      (*pTagConds)->set, pTagCondKey, tagCondKeyLen, &pEntry, POINTER_BYTES);
6,688✔
886
    TSDB_CHECK_CODE(code, lino, _end);
6,688✔
887

888
    pFilterEntry = (STagCondFilterEntry**)taosHashGet(
6,688✔
889
      (*pTagConds)->set, pTagCondKey, tagCondKeyLen);
6,688✔
890
  } else {
891
    // pColIds is already set, so we can destroy the new one
892
    taosArrayDestroy(*pTagColIds);
3,168✔
893
    *pTagColIds = NULL;
3,168✔
894
  }
895

896
  // add to cache.
897
  SArray* pPayload = taosArrayDup(pUidList, NULL);
9,856✔
898
  code = taosHashPut(
9,856✔
899
    (*pFilterEntry)->set, pKey, keyLen, &pPayload, POINTER_BYTES);
9,856✔
900
  TSDB_CHECK_CODE(code, lino, _end);
9,856✔
901
  pMeta->pCache->sStableTagFilterResCache.numTagDataEntries += 1;
9,856✔
902
  (*pTagConds)->numTagDataEntries += 1;
9,856✔
903

904
_end:
9,856✔
905
  if (TSDB_CODE_SUCCESS != code) {
9,856✔
906
    metaError("vgId:%d, %s failed at %s:%d since %s",
×
907
      vgId, __func__, __FILE__, lino, tstrerror(code));
908
  } else {
909
    metaDebug("vgId:%d, suid:%" PRIu64 " new tag data filter entry added, "
9,856✔
910
      "uid num:%d, current stable tag conditions num:%d, "
911
      "this tag condition data entries num:%d, "
912
      "cache stable num:%d, total tag data entries num:%" PRIu32 ", "
913
      "total tag data entries num:%" PRIu32,
914
      vgId, suid, (int32_t)taosArrayGetSize(pUidList), 
915
      pTagConds ? (int32_t)taosHashGetSize((*pTagConds)->set) : 0,
916
      pFilterEntry ? (int32_t)taosHashGetSize((*pFilterEntry)->set) : 0,
917
      (int32_t)taosHashGetSize(pTableEntry),
918
      pMeta->pCache->sStableTagFilterResCache.numTagDataEntries,
919
      (*pTagConds)->numTagDataEntries);
920
  }
921
  // unlock meta
922
  code = taosThreadRwlockUnlock(pRwlock);
9,856✔
923
  if (TSDB_CODE_SUCCESS != code) {
9,856✔
924
    metaError("vgId:%d, %s unlock failed at %s:%d since %s",
×
925
      vgId, __func__, __FILE__, lino, tstrerror(code));
926
  }
927

928
  return code;
9,856✔
929
}
930

931
// drop all the cache entries for a super table 
932
int32_t metaStableTagFilterCacheDropSTable(
×
933
  SMeta* pMeta, tb_uid_t suid) {
934
  if (pMeta == NULL) {
×
935
    return TSDB_CODE_INVALID_PARA;
×
936
  }
937
  int32_t   lino = 0;
×
938
  int32_t   code = TSDB_CODE_SUCCESS;
×
939
  SHashObj* pTableEntry = pMeta->pCache->sStableTagFilterResCache.pTableEntry;
×
940
  TdThreadRwlock* pRwlock = &pMeta->pCache->sStableTagFilterResCache.rwlock;
×
941

942
  code = taosThreadRwlockWrlock(pRwlock);
×
943
  TSDB_CHECK_CODE(code, lino, _end);
×
944
  STagConds** pTagConds = taosHashGet(pTableEntry, &suid, sizeof(tb_uid_t));
×
945
  if (pTagConds != NULL) {
×
946
    pMeta->pCache->sStableTagFilterResCache.
×
947
      numTagDataEntries -= (*pTagConds)->numTagDataEntries;
×
948
  }
949
  code = taosHashRemove(pTableEntry, &suid, sizeof(tb_uid_t));
×
950
  TSDB_CHECK_CODE(code, lino, _end);
×
951

952
_end:
×
953
  if (TSDB_CODE_SUCCESS != code) {
×
954
    metaError("vgId:%d, %s failed at %s:%d since %s",
×
955
      TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code));
956
  } else {
957
    metaDebug(
×
958
      "vgId:%d, suid:%" PRIu64 " stable tag filter cache dropped from cache"
959
      "left stable num:%d, tag conditions num:%" PRIu32,
960
      TD_VID(pMeta->pVnode), suid, (int32_t)taosHashGetSize(pTableEntry),
961
      pMeta->pCache->sStableTagFilterResCache.numTagDataEntries);
962
  }
963
  code = taosThreadRwlockUnlock(pRwlock);
×
964
  if (TSDB_CODE_SUCCESS != code) {
×
965
    metaError("vgId:%d, %s unlock failed at %s:%d since %s",
×
966
      TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code));
967
  }
968
  return code;
×
969
}
970

971
static int32_t getTagColSize(
33,088✔
972
  const SSchema* pTagSchemas, int32_t nTagCols, col_id_t cid) {
973
  for (int32_t i = 0; i < nTagCols; i++) {
182,336✔
974
    if (pTagSchemas[i].colId == cid) {
182,336✔
975
      return pTagSchemas[i].bytes;
33,088✔
976
    }
977
  }
978
  return 0;
×
979
}
980

981
// when encode nchar tag into tag data entry key, need to convert it to var type
982
static FORCE_INLINE int32_t ncharToVar(char *pData, int32_t nData, char **ppOut) {
983
  int32_t code = TSDB_CODE_SUCCESS;
16,896✔
984

985
  char *t = taosMemoryCalloc(1, nData + VARSTR_HEADER_SIZE);
16,896✔
986
  if (NULL == t) {
16,896✔
987
    return terrno;
×
988
  }
989
  int32_t len = taosUcs4ToMbs(
16,896✔
990
    (TdUcs4 *)pData, nData, varDataVal(t), NULL);
991
  if (len < 0) {
16,896✔
992
    taosMemoryFree(t);
×
993
    return TSDB_CODE_SCALAR_CONVERT_ERROR;
×
994
  }
995
  varDataSetLen(t, len);
16,896✔
996

997
  *ppOut = taosMemoryCalloc(1, len + VARSTR_HEADER_SIZE);
16,896✔
998
  memcpy(*ppOut, t, len + VARSTR_HEADER_SIZE);
16,896✔
999

1000
_return:
16,896✔
1001
  taosMemoryFree(t);
16,896✔
1002
  return code;
16,896✔
1003
}
1004

1005
static int32_t buildTagDataEntryKey(const SArray* pColIds, const STag* pTag,
81,312✔
1006
  const SSchemaWrapper* pTagScheam, T_MD5_CTX* pContext) {
1007
  int32_t code = TSDB_CODE_SUCCESS;
81,312✔
1008
  int32_t lino = 0;
81,312✔
1009
  int32_t keyLen = 0;
81,312✔
1010
  char* pKey = NULL;
81,312✔
1011
  // get length first
1012
  for (int32_t i = 0; i < taosArrayGetSize(pColIds); i++) {
239,712✔
1013
    STagVal pTagValue = {.cid = *(col_id_t*)taosArrayGet(pColIds, i)};
162,272✔
1014
    if (tTagGet(pTag, &pTagValue)) {
162,272✔
1015
      keyLen += sizeof(col_id_t);
158,400✔
1016
      if (IS_VAR_DATA_TYPE(pTagValue.type)) {
158,400✔
1017
        int32_t varLen = getTagColSize(
66,176✔
1018
          pTagScheam->pSchema, pTagScheam->nCols, pTagValue.cid);
33,088✔
1019
        code = varLen > 0 ? TSDB_CODE_SUCCESS : TSDB_CODE_NOT_FOUND;
33,088✔
1020
        QUERY_CHECK_CODE(code, lino, _end);
33,088✔
1021
        keyLen += varLen;
33,088✔
1022
      } else {
1023
        keyLen += tDataTypes[pTagValue.type].bytes;
125,312✔
1024
      }
1025
    } else {
1026
      // tag value not found
1027
      code = TSDB_CODE_NOT_FOUND;
3,872✔
1028
      QUERY_CHECK_CODE(code, lino, _end);
3,872✔
1029
    }
1030
  }
1031

1032
  pKey = taosMemoryCalloc(1, keyLen);
77,440✔
1033
  if (NULL == pKey) {
77,440✔
1034
    code = terrno;
×
1035
    return code;
×
1036
  }
1037

1038
  // build the key
1039
  char* pStart = pKey;
77,440✔
1040
  for (int32_t i = 0; i < taosArrayGetSize(pColIds); i++) {
232,320✔
1041
    STagVal pTagValue = {.cid = *(col_id_t*)taosArrayGet(pColIds, i)};
154,880✔
1042
    if (tTagGet(pTag, &pTagValue)) {
154,880✔
1043
      // copy cid
1044
      memcpy(pStart, &pTagValue.cid, sizeof(col_id_t));
154,880✔
1045
      pStart += sizeof(col_id_t);
154,880✔
1046
      // copy value
1047
      if (IS_VAR_DATA_TYPE(pTagValue.type) && pTagValue.pData != NULL) {
154,880✔
1048
        if (TSDB_DATA_TYPE_NCHAR == pTagValue.type) {
33,088✔
1049
          // need to convert nchar to var
1050
          char *pVar = NULL;
16,896✔
1051
          code = ncharToVar((char *)pTagValue.pData, pTagValue.nData, &pVar);
16,896✔
1052
          QUERY_CHECK_CODE(code, lino, _end);
16,896✔
1053
          memcpy(pStart, varDataVal(pVar), varDataLen(pVar));
16,896✔
1054
          pStart += varDataLen(pVar);
16,896✔
1055
          taosMemoryFree(pVar);
16,896✔
1056
        } else {
1057
          memcpy(pStart, pTagValue.pData, pTagValue.nData);
16,192✔
1058
          pStart += pTagValue.nData;
16,192✔
1059
        }
1060
      } else {
1061
        memcpy(pStart, &pTagValue.i64, tDataTypes[pTagValue.type].bytes);
121,792✔
1062
        pStart += tDataTypes[pTagValue.type].bytes;
121,792✔
1063
      }
1064
    } else {
1065
      // tag value not found
1066
      code = TSDB_CODE_NOT_FOUND;
×
1067
      QUERY_CHECK_CODE(code, lino, _end);
×
1068
    }
1069
  }
1070

1071
  // update MD5
1072
  tMD5Init(pContext);
77,440✔
1073
  tMD5Update(pContext, (uint8_t*)pKey, (uint32_t)keyLen);
77,440✔
1074
  tMD5Final(pContext);
77,440✔
1075

1076
_end:
81,312✔
1077
  taosMemFreeClear(pKey);
81,312✔
1078
  return code;
81,312✔
1079
}
1080

1081
// remove the dropped table uid from all cache entries
1082
// pDroppedTable is the dropped child table meta entry
1083
int32_t metaStableTagFilterCacheUpdateUid(SMeta* pMeta,
79,874,080✔
1084
  const SMetaEntry* pChildTable, const SMetaEntry* pSuperTable,
1085
  ETagFilterCacheAction action) {
1086
  if (pMeta == NULL || pChildTable == NULL || pSuperTable == NULL) {
79,874,080✔
1087
    return TSDB_CODE_INVALID_PARA;
19✔
1088
  }
1089
  int32_t   lino = 0;
79,903,496✔
1090
  int32_t   code = TSDB_CODE_SUCCESS;
79,903,496✔
1091
  SHashObj* pTableEntry = pMeta->pCache->sStableTagFilterResCache.pTableEntry;
79,903,496✔
1092
  TdThreadRwlock* pRwlock = &pMeta->pCache->sStableTagFilterResCache.rwlock;
79,894,035✔
1093

1094
  code = taosThreadRwlockWrlock(pRwlock);
79,904,132✔
1095
  TSDB_CHECK_CODE(code, lino, _end);
79,896,706✔
1096

1097
  tb_uid_t suid = pChildTable->ctbEntry.suid;;
79,896,706✔
1098
  STagConds** pTagConds =
1099
    (STagConds**)taosHashGet(pTableEntry, &suid, sizeof(tb_uid_t));
79,891,768✔
1100
  if (pTagConds != NULL) {
79,904,101✔
1101
    STagCondFilterEntry** ppFilterEntry = NULL;
17,952✔
1102
    while ((ppFilterEntry = taosHashIterate((*pTagConds)->set, ppFilterEntry))) {
95,392✔
1103
      STagCondFilterEntry* pFilterEntry = *ppFilterEntry;
81,312✔
1104
      // rebuild the tagCondKey and check existence
1105
      SArray* pColIds = pFilterEntry->pColIds;
81,312✔
1106
      // rebuild the tagCondFilterKey
1107
      int32_t keyLen = 0;
81,312✔
1108
      char*   pKey = NULL;
81,312✔
1109
      T_MD5_CTX context = {0};
81,312✔
1110
      code = buildTagDataEntryKey(pColIds, (STag*)pChildTable->ctbEntry.pTags, 
81,312✔
1111
        &pSuperTable->stbEntry.schemaTag, &context);
1112
      if (code != TSDB_CODE_SUCCESS) {
81,312✔
1113
        metaError("vgId:%d, suid:%" PRIu64 " failed to build tag condition"
3,872✔
1114
          " key for dropped table uid:%" PRIu64 " since %s",
1115
          TD_VID(pMeta->pVnode), suid, pChildTable->uid, tstrerror(code));
1116
        goto _end;
3,872✔
1117
      }
1118

1119
      SArray** pArray = (SArray**)taosHashGet(
77,440✔
1120
        pFilterEntry->set, context.digest, tListLen(context.digest));
1121
      if (pArray != NULL) {
77,440✔
1122
        // check and remove the dropped table uid from the array
1123
        // TODO(Tony Zhang): optimize this scan
1124
        if (action == STABLE_TAG_FILTER_CACHE_DROP_TABLE) {
10,560✔
1125
          for (int32_t i = 0; i < taosArrayGetSize(*pArray); i++) {
10,560✔
1126
            uint64_t uid = *(uint64_t*)taosArrayGet(*pArray, i);
10,560✔
1127
            if (uid == pChildTable->uid) {
10,560✔
1128
              taosArrayRemove(*pArray, i);
5,280✔
1129
              metaDebug("vgId:%d, suid:%" PRIu64
5,280✔
1130
                " removed dropped table uid:%" PRIu64
1131
                " from stable tag filter cache",
1132
                TD_VID(pMeta->pVnode), suid, pChildTable->uid);
1133
              break;
5,280✔
1134
            } 
1135
          }
1136
        } else {
1137
          // STABLE_TAG_FILTER_CACHE_ADD_TABLE
1138
          void* _tmp = taosArrayPush(*pArray, &pChildTable->uid);
10,560✔
1139
        }
1140
      }
1141
    }
1142
  }
1143

1144
_end:
79,904,101✔
1145
  if (TSDB_CODE_SUCCESS != code) {
79,898,815✔
1146
    metaError("vgId:%d, %s failed at %s:%d since %s",
3,872✔
1147
      TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code));
1148
  } else {
1149
    metaDebug(
79,894,943✔
1150
      "vgId:%d, suid:%" PRIu64 " update table uid:%" PRIu64
1151
      " in stable tag filter cache, action:%d",
1152
      TD_VID(pMeta->pVnode),
1153
      pChildTable->ctbEntry.suid, pChildTable->uid, action);
1154
  }
1155
  code = taosThreadRwlockUnlock(pRwlock);
79,899,143✔
1156
  if (TSDB_CODE_SUCCESS != code) {
79,893,244✔
1157
    metaError("vgId:%d, %s unlock failed at %s:%d since %s",
×
1158
      TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code));
1159
  }
1160
  return code;
79,886,065✔
1161
}
1162

1163
int32_t metaStableTagFilterCacheDropTag(
307,077✔
1164
  SMeta* pMeta, tb_uid_t suid, col_id_t cid) {
1165
  if (pMeta == NULL) {
307,077✔
1166
    return TSDB_CODE_INVALID_PARA;
×
1167
  }
1168
  int32_t   lino = 0;
307,077✔
1169
  int32_t   code = TSDB_CODE_SUCCESS;
307,077✔
1170
  SHashObj* pTableEntry = pMeta->pCache->sStableTagFilterResCache.pTableEntry;
307,077✔
1171
  TdThreadRwlock* pRwlock = &pMeta->pCache->sStableTagFilterResCache.rwlock;
307,759✔
1172

1173
  code = taosThreadRwlockWrlock(pRwlock);
308,441✔
1174
  TSDB_CHECK_CODE(code, lino, _end);
309,123✔
1175

1176
  STagConds** pTagConds =
1177
    (STagConds**)taosHashGet(pTableEntry, &suid, sizeof(tb_uid_t));
309,123✔
1178
  if (pTagConds != NULL) {
309,230✔
1179
    void* pIter = taosHashIterate((*pTagConds)->set, NULL);
×
1180
    while (pIter) {
×
1181
      STagCondFilterEntry* pFilterEntry = *(STagCondFilterEntry**)pIter;
×
1182
      bool found = false;
×
1183
      for (int32_t i = 0; i < taosArrayGetSize(pFilterEntry->pColIds); i++) {
×
1184
        col_id_t existCid = *(col_id_t*)taosArrayGet(pFilterEntry->pColIds, i);
×
1185
        if (existCid == cid) {
×
1186
          found = true;
×
1187
          break;
×
1188
        }
1189
      }
1190
      if (found) {
×
1191
        uint32_t numEntries = taosHashGetSize(pFilterEntry->set);
×
1192
        size_t keyLen = 0;
×
1193
        char  *key = (char *)taosHashGetKey(pIter, &keyLen);
×
1194
        code = taosHashRemove((*pTagConds)->set, key, keyLen);
×
1195
        TSDB_CHECK_CODE(code, lino, _end);
×
1196
        (*pTagConds)->numTagDataEntries -= numEntries;
×
1197
        pMeta->pCache->sStableTagFilterResCache.numTagDataEntries -= numEntries;
×
1198
      }
1199
      pIter = taosHashIterate((*pTagConds)->set, pIter);
3,517✔
1200
    }
1201
  }
1202
_end:
309,230✔
1203
  if (TSDB_CODE_SUCCESS != code) {
305,713✔
1204
    metaError("vgId:%d, %s failed at %s:%d since %s",
×
1205
      TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code));
1206
  } else {
1207
    metaDebug(
305,713✔
1208
      "vgId:%d, suid:%" PRIu64 " dropped tag cid:%d "
1209
      "from stable tag filter cache",
1210
      TD_VID(pMeta->pVnode), suid, cid);
1211
  }
1212
  code = taosThreadRwlockUnlock(pRwlock);
305,713✔
1213
  if (TSDB_CODE_SUCCESS != code) {
305,606✔
1214
    metaError("vgId:%d, %s unlock failed at %s:%d since %s",
×
1215
      TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code));
1216
  }
1217
  return code;
306,395✔
1218
}
1219

1220
void metaCacheClear(SMeta* pMeta) {
4,222,391✔
1221
  metaWLock(pMeta);
4,222,391✔
1222
  metaCacheClose(pMeta);
4,222,391✔
1223
  (void)metaCacheOpen(pMeta);
4,222,391✔
1224
  metaULock(pMeta);
4,222,391✔
1225
}
4,222,391✔
1226

1227
// remove the lru cache that are expired due to the tags value update, or creating, or dropping, of child tables
1228
int32_t metaUidCacheClear(SMeta* pMeta, uint64_t suid) {
71,670,262✔
1229
  uint64_t  p[4] = {0};
71,670,262✔
1230
  int32_t   vgId = TD_VID(pMeta->pVnode);
71,670,299✔
1231
  SHashObj* pEntryHashMap = pMeta->pCache->sTagFilterResCache.pTableEntry;
71,659,426✔
1232

1233
  uint64_t dummy[2] = {0};
71,667,794✔
1234
  initCacheKey(p, pEntryHashMap, suid, (char*)&dummy[0], 16);
71,673,959✔
1235

1236
  TdThreadMutex* pLock = &pMeta->pCache->sTagFilterResCache.lock;
71,664,635✔
1237
  (void)taosThreadMutexLock(pLock);
71,667,378✔
1238

1239
  STagFilterResEntry** pEntry = taosHashGet(pEntryHashMap, &suid, sizeof(uint64_t));
71,674,982✔
1240
  if (pEntry == NULL || taosHashGetSize((*pEntry)->set) == 0) {
71,676,891✔
1241
    (void)taosThreadMutexUnlock(pLock);
71,672,843✔
1242
    return TSDB_CODE_SUCCESS;
71,677,178✔
1243
  }
1244

1245
  (*pEntry)->hitTimes = 0;
4,048✔
1246

1247
  char *iter = taosHashIterate((*pEntry)->set, NULL);
4,048✔
1248
  while (iter != NULL) {
8,096✔
1249
    setMD5DigestInKey(p, iter, 2 * sizeof(uint64_t));
1250
    taosLRUCacheErase(pMeta->pCache->sTagFilterResCache.pUidResCache, p, TAG_FILTER_RES_KEY_LEN);
4,048✔
1251
    iter = taosHashIterate((*pEntry)->set, iter);
4,048✔
1252
  }
1253
  taosHashClear((*pEntry)->set);
4,048✔
1254
  (void)taosThreadMutexUnlock(pLock);
4,048✔
1255

1256
  metaDebug("vgId:%d suid:%" PRId64 " cached related tag filter uid list cleared", vgId, suid);
4,048✔
1257
  return TSDB_CODE_SUCCESS;
4,048✔
1258
}
1259

UNCOV
1260
int32_t metaGetCachedTbGroup(void* pVnode, tb_uid_t suid, const uint8_t* pKey, int32_t keyLen, SArray** pList) {
×
UNCOV
1261
  SMeta*  pMeta = ((SVnode*)pVnode)->pMeta;
×
UNCOV
1262
  int32_t vgId = TD_VID(pMeta->pVnode);
×
1263

1264
  // generate the composed key for LRU cache
UNCOV
1265
  SLRUCache*     pCache = pMeta->pCache->STbGroupResCache.pResCache;
×
UNCOV
1266
  SHashObj*      pTableMap = pMeta->pCache->STbGroupResCache.pTableEntry;
×
UNCOV
1267
  TdThreadMutex* pLock = &pMeta->pCache->STbGroupResCache.lock;
×
1268

UNCOV
1269
  *pList = NULL;
×
UNCOV
1270
  uint64_t key[4];
×
UNCOV
1271
  initCacheKey(key, pTableMap, suid, (const char*)pKey, keyLen);
×
1272

UNCOV
1273
  (void)taosThreadMutexLock(pLock);
×
UNCOV
1274
  pMeta->pCache->STbGroupResCache.accTimes += 1;
×
1275

UNCOV
1276
  LRUHandle* pHandle = taosLRUCacheLookup(pCache, key, TAG_FILTER_RES_KEY_LEN);
×
UNCOV
1277
  if (pHandle == NULL) {
×
UNCOV
1278
    (void)taosThreadMutexUnlock(pLock);
×
UNCOV
1279
    return TSDB_CODE_SUCCESS;
×
1280
  }
1281

1282
  STagFilterResEntry** pEntry = taosHashGet(pTableMap, &suid, sizeof(uint64_t));
×
1283
  if (NULL == pEntry) {
×
1284
    metaDebug("suid %" PRIu64 " not in tb group cache", suid);
×
1285
    return TSDB_CODE_NOT_FOUND;
×
1286
  }
1287

1288
  *pList = taosArrayDup(taosLRUCacheValue(pCache, pHandle), NULL);
×
1289

1290
  (*pEntry)->hitTimes += 1;
×
1291

1292
  uint32_t acc = pMeta->pCache->STbGroupResCache.accTimes;
×
1293
  if ((*pEntry)->hitTimes % 5000 == 0 && (*pEntry)->hitTimes > 0) {
×
1294
    metaInfo("vgId:%d tb group cache hit:%d, total acc:%d, rate:%.2f", vgId, (*pEntry)->hitTimes, acc,
×
1295
             ((double)(*pEntry)->hitTimes) / acc);
1296
  }
1297

1298
  bool ret = taosLRUCacheRelease(pCache, pHandle, false);
×
1299

1300
  // unlock meta
1301
  (void)taosThreadMutexUnlock(pLock);
×
1302
  return TSDB_CODE_SUCCESS;
×
1303
}
1304

UNCOV
1305
static void freeTbGroupCachePayload(const void* key, size_t keyLen, void* value, void* ud) {
×
1306
  (void)ud;
UNCOV
1307
  if (value == NULL) {
×
1308
    return;
×
1309
  }
1310

UNCOV
1311
  const uint64_t* p = key;
×
UNCOV
1312
  if (keyLen != sizeof(int64_t) * 4) {
×
1313
    metaError("tb group key length is invalid, length:%d, expect:%d", (int32_t)keyLen, (int32_t)sizeof(uint64_t) * 2);
×
1314
    return;
×
1315
  }
1316

UNCOV
1317
  SHashObj* pHashObj = (SHashObj*)p[0];
×
1318

UNCOV
1319
  STagFilterResEntry** pEntry = taosHashGet(pHashObj, &p[1], sizeof(uint64_t));
×
1320

UNCOV
1321
  if (pEntry != NULL && (*pEntry) != NULL) {
×
1322
    int64_t st = taosGetTimestampUs();
×
1323
    int32_t code = taosHashRemove((*pEntry)->set, &p[2], sizeof(uint64_t) * 2);
×
1324
    if (code == TSDB_CODE_SUCCESS) {
×
1325
      double el = (taosGetTimestampUs() - st) / 1000.0;
×
1326
      metaDebug("clear one item in tb group cache, remain cached item:%d, elapsed time:%.2fms",
×
1327
                taosHashGetSize((*pEntry)->set), el);
1328
    }
1329
  }
1330

UNCOV
1331
  taosArrayDestroy((SArray*)value);
×
1332
}
1333

UNCOV
1334
int32_t metaPutTbGroupToCache(void* pVnode, uint64_t suid, const void* pKey, int32_t keyLen, void* pPayload,
×
1335
                              int32_t payloadLen) {
UNCOV
1336
  int32_t code = 0;
×
UNCOV
1337
  SMeta*  pMeta = ((SVnode*)pVnode)->pMeta;
×
UNCOV
1338
  int32_t vgId = TD_VID(pMeta->pVnode);
×
1339

UNCOV
1340
  if (payloadLen > tsTagFilterResCacheSize) {
×
1341
    metaDebug("vgId:%d, suid:%" PRIu64
×
1342
              " ignore to add to tb group cache, due to payload length %d greater than threshold %d",
1343
              vgId, suid, payloadLen, tsTagFilterResCacheSize);
1344
    taosArrayDestroy((SArray*)pPayload);
×
1345
    return TSDB_CODE_SUCCESS;
×
1346
  }
1347

UNCOV
1348
  SLRUCache*     pCache = pMeta->pCache->STbGroupResCache.pResCache;
×
UNCOV
1349
  SHashObj*      pTableEntry = pMeta->pCache->STbGroupResCache.pTableEntry;
×
UNCOV
1350
  TdThreadMutex* pLock = &pMeta->pCache->STbGroupResCache.lock;
×
1351

UNCOV
1352
  uint64_t key[4] = {0};
×
UNCOV
1353
  initCacheKey(key, pTableEntry, suid, pKey, keyLen);
×
1354

UNCOV
1355
  (void)taosThreadMutexLock(pLock);
×
UNCOV
1356
  STagFilterResEntry** pEntry = taosHashGet(pTableEntry, &suid, sizeof(uint64_t));
×
UNCOV
1357
  if (pEntry == NULL) {
×
UNCOV
1358
    code = addNewEntry(pTableEntry, pKey, keyLen, suid);
×
UNCOV
1359
    if (code != TSDB_CODE_SUCCESS) {
×
1360
      goto _end;
×
1361
    }
1362
  } else {  // check if it exists or not
UNCOV
1363
    code = taosHashPut((*pEntry)->set, pKey, keyLen, NULL, 0);
×
UNCOV
1364
    if (code == TSDB_CODE_DUP_KEY) {
×
1365
      // we have already found the existed items, no need to added to cache anymore.
1366
      (void)taosThreadMutexUnlock(pLock);
×
1367
      return TSDB_CODE_SUCCESS;
×
1368
    }
UNCOV
1369
    if (code != TSDB_CODE_SUCCESS) {
×
1370
      goto _end;
×
1371
    }
1372
  }
1373

1374
  // add to cache.
UNCOV
1375
  (void)taosLRUCacheInsert(pCache, key, TAG_FILTER_RES_KEY_LEN, pPayload, payloadLen, freeTbGroupCachePayload, NULL, NULL,
×
1376
                           TAOS_LRU_PRIORITY_LOW, NULL);
UNCOV
1377
_end:
×
UNCOV
1378
  (void)taosThreadMutexUnlock(pLock);
×
UNCOV
1379
  metaDebug("vgId:%d, suid:%" PRIu64 " tb group added into cache, total:%d, tables:%d", vgId, suid,
×
1380
            (int32_t)taosLRUCacheGetUsage(pCache), taosHashGetSize(pTableEntry));
1381

UNCOV
1382
  return code;
×
1383
}
1384

1385
// remove the lru cache that are expired due to the tags value update, or creating, or dropping, of child tables
1386
int32_t metaTbGroupCacheClear(SMeta* pMeta, uint64_t suid) {
71,665,213✔
1387
  uint64_t  p[4] = {0};
71,665,213✔
1388
  int32_t   vgId = TD_VID(pMeta->pVnode);
71,664,263✔
1389
  SHashObj* pEntryHashMap = pMeta->pCache->STbGroupResCache.pTableEntry;
71,662,479✔
1390

1391
  uint64_t dummy[2] = {0};
71,662,192✔
1392
  initCacheKey(p, pEntryHashMap, suid, (char*)&dummy[0], 16);
71,673,562✔
1393

1394
  TdThreadMutex* pLock = &pMeta->pCache->STbGroupResCache.lock;
71,674,785✔
1395
  (void)taosThreadMutexLock(pLock);
71,669,614✔
1396

1397
  STagFilterResEntry** pEntry = taosHashGet(pEntryHashMap, &suid, sizeof(uint64_t));
71,681,492✔
1398
  if (pEntry == NULL || taosHashGetSize((*pEntry)->set) == 0) {
71,682,556✔
1399
    (void)taosThreadMutexUnlock(pLock);
71,682,556✔
1400
    return TSDB_CODE_SUCCESS;
71,682,483✔
1401
  }
1402

1403
  (*pEntry)->hitTimes = 0;
×
1404

1405
  char *iter = taosHashIterate((*pEntry)->set, NULL);
×
1406
  while (iter != NULL) {
×
1407
    setMD5DigestInKey(p, iter, 2 * sizeof(uint64_t));
1408
    taosLRUCacheErase(pMeta->pCache->STbGroupResCache.pResCache, p, TAG_FILTER_RES_KEY_LEN);
×
1409
    iter = taosHashIterate((*pEntry)->set, iter);
×
1410
  }
1411
  taosHashClear((*pEntry)->set);
×
1412
  (void)taosThreadMutexUnlock(pLock);
×
1413

1414
  metaDebug("vgId:%d suid:%" PRId64 " cached related tb group cleared", vgId, suid);
×
1415
  return TSDB_CODE_SUCCESS;
×
1416
}
1417

1418
bool metaTbInFilterCache(SMeta* pMeta, const void* key, int8_t type) {
123,735,402✔
1419
  if (type == 0 && taosHashGet(pMeta->pCache->STbFilterCache.pStb, key, sizeof(tb_uid_t))) {
123,735,402✔
1420
    return true;
1,280✔
1421
  }
1422

1423
  if (type == 1 && taosHashGet(pMeta->pCache->STbFilterCache.pStbName, key, strlen(key))) {
123,734,122✔
1424
    return true;
×
1425
  }
1426

1427
  return false;
123,750,120✔
1428
}
1429

1430
int32_t metaPutTbToFilterCache(SMeta* pMeta, const void* key, int8_t type) {
8,218✔
1431
  if (type == 0) {
8,218✔
1432
    return taosHashPut(pMeta->pCache->STbFilterCache.pStb, key, sizeof(tb_uid_t), NULL, 0);
3,072✔
1433
  }
1434

1435
  if (type == 1) {
5,146✔
1436
    return taosHashPut(pMeta->pCache->STbFilterCache.pStbName, key, strlen(key), NULL, 0);
5,146✔
1437
  }
1438

1439
  return 0;
×
1440
}
1441

1442
int32_t metaSizeOfTbFilterCache(SMeta* pMeta, int8_t type) {
7,172✔
1443
  if (type == 0) {
7,172✔
1444
    return taosHashGetSize(pMeta->pCache->STbFilterCache.pStb);
7,172✔
1445
  }
1446
  return 0;
×
1447
}
1448

1449
int32_t metaInitTbFilterCache(SMeta* pMeta) {
4,226,009✔
1450
#ifdef TD_ENTERPRISE
1451
  int32_t      tbNum = 0;
4,226,009✔
1452
  const char** pTbArr = NULL;
4,226,009✔
1453
  const char*  dbName = NULL;
4,226,009✔
1454

1455
  if (!(dbName = strchr(pMeta->pVnode->config.dbname, '.'))) return 0;
4,226,009✔
1456
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
4,227,354✔
1457
    tbNum = tkLogStbNum;
256✔
1458
    pTbArr = (const char**)&tkLogStb;
256✔
1459
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
4,227,098✔
1460
    tbNum = tkAuditStbNum;
282✔
1461
    pTbArr = (const char**)&tkAuditStb;
282✔
1462
  }
1463
  if (tbNum && pTbArr) {
4,227,354✔
1464
    for (int32_t i = 0; i < tbNum; ++i) {
5,684✔
1465
      TAOS_CHECK_RETURN(metaPutTbToFilterCache(pMeta, pTbArr[i], 1));
5,146✔
1466
    }
1467
  }
1468
#else
1469
#endif
1470
  return 0;
4,227,354✔
1471
}
1472

1473
int64_t metaGetStbKeep(SMeta* pMeta, int64_t uid) {
145,081✔
1474
  SMetaStbStats stats = {0};
145,081✔
1475

1476
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
145,758✔
1477
    return stats.keep;
125,931✔
1478
  }
1479

1480
  SMetaEntry* pEntry = NULL;
19,150✔
1481
  if (metaFetchEntryByUid(pMeta, uid, &pEntry) == TSDB_CODE_SUCCESS) {
19,150✔
1482
    int64_t keep = -1;
19,150✔
1483
    if (pEntry->type == TSDB_SUPER_TABLE) {
19,150✔
1484
      keep = pEntry->stbEntry.keep;
19,150✔
1485
    }
1486
    metaFetchEntryFree(&pEntry);
19,150✔
1487
    return keep;
19,150✔
1488
  }
1489
  
UNCOV
1490
  return -1;
×
1491
}
1492

1493
int32_t metaRefDbsCacheClear(SMeta* pMeta, uint64_t suid) {
317,429✔
1494
  int32_t        code = TSDB_CODE_SUCCESS;
317,429✔
1495
  int32_t        vgId = TD_VID(pMeta->pVnode);
317,429✔
1496
  SHashObj*      pEntryHashMap = pMeta->pCache->STbRefDbCache.pStbRefs;
317,429✔
1497
  TdThreadMutex* pLock = &pMeta->pCache->STbRefDbCache.lock;
317,429✔
1498

1499
  (void)taosThreadMutexLock(pLock);
317,429✔
1500

1501
  SHashObj** pEntry = taosHashGet(pEntryHashMap, &suid, sizeof(uint64_t));
317,429✔
1502
  if (pEntry == NULL) {
317,429✔
1503
    goto _return;
314,464✔
1504
  }
1505

1506
  code = taosHashRemove(pEntryHashMap, &suid, sizeof(uint64_t));
2,965✔
1507

1508
  metaDebug("vgId:%d suid:%" PRId64 " cached virtual stable ref db cleared", vgId, suid);
2,965✔
1509

1510
_return:
2,153✔
1511
  (void)taosThreadMutexUnlock(pLock);
317,429✔
1512
  return code;
317,429✔
1513
}
1514

1515
int32_t metaGetCachedRefDbs(void* pVnode, tb_uid_t suid, SArray* pList) {
54,169✔
1516
  int32_t        code = TSDB_CODE_SUCCESS;
54,169✔
1517
  int32_t        line = 0;
54,169✔
1518
  SMeta*         pMeta = ((SVnode*)pVnode)->pMeta;
54,169✔
1519
  SHashObj*      pTableMap = pMeta->pCache->STbRefDbCache.pStbRefs;
54,169✔
1520
  TdThreadMutex* pLock = &pMeta->pCache->STbRefDbCache.lock;
54,169✔
1521

1522
  (void)taosThreadMutexLock(pLock);
54,169✔
1523

1524
  SHashObj** pEntry = taosHashGet(pTableMap, &suid, sizeof(uint64_t));
54,169✔
1525
  if (pEntry) {
54,169✔
1526
    void *iter = taosHashIterate(*pEntry, NULL);
32,130✔
1527
    while (iter != NULL) {
60,875✔
1528
      size_t   dbNameLen = 0;
28,745✔
1529
      char*    name = NULL;
28,745✔
1530
      char*    dbName = NULL;
28,745✔
1531
      name = taosHashGetKey(iter, &dbNameLen);
28,745✔
1532
      TSDB_CHECK_NULL(name, code, line, _return, terrno);
28,745✔
1533
      dbName = taosMemoryMalloc(dbNameLen + 1);
28,745✔
1534
      TSDB_CHECK_NULL(dbName, code, line, _return, terrno);
28,745✔
1535
      tstrncpy(dbName, name, dbNameLen + 1);
28,745✔
1536
      TSDB_CHECK_NULL(taosArrayPush(pList, &dbName), code, line, _return, terrno);
28,745✔
1537
      iter = taosHashIterate(*pEntry, iter);
28,745✔
1538
    }
1539
  }
1540

1541
_return:
54,169✔
1542
  if (code) {
54,169✔
1543
    metaError("%s failed at line %d since %s", __func__, line, tstrerror(code));
×
1544
  }
1545
  (void)taosThreadMutexUnlock(pLock);
54,169✔
1546
  return code;
54,169✔
1547
}
1548

1549
static int32_t addRefDbsCacheNewEntry(SHashObj* pRefDbs, uint64_t suid, SHashObj **pEntry) {
22,039✔
1550
  int32_t      code = TSDB_CODE_SUCCESS;
22,039✔
1551
  int32_t      lino = 0;
22,039✔
1552
  SHashObj*    p = NULL;
22,039✔
1553

1554
  p = taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
22,039✔
1555
  TSDB_CHECK_NULL(p, code, lino, _end, terrno);
22,039✔
1556

1557
  code = taosHashPut(pRefDbs, &suid, sizeof(uint64_t), &p, POINTER_BYTES);
22,039✔
1558
  TSDB_CHECK_CODE(code, lino, _end);
22,039✔
1559

1560
  *pEntry = p;
22,039✔
1561

1562
_end:
22,039✔
1563
  if (code != TSDB_CODE_SUCCESS) {
22,039✔
1564
    metaError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1565
  }
1566
  return code;
22,039✔
1567
}
1568

1569
int32_t metaPutRefDbsToCache(void* pVnode, tb_uid_t suid, SArray* pList) {
24,652✔
1570
  int32_t        code = 0;
24,652✔
1571
  int32_t        line = 0;
24,652✔
1572
  SMeta*         pMeta = ((SVnode*)pVnode)->pMeta;
24,652✔
1573
  SHashObj*      pStbRefs = pMeta->pCache->STbRefDbCache.pStbRefs;
25,424✔
1574
  TdThreadMutex* pLock = &pMeta->pCache->STbRefDbCache.lock;
25,424✔
1575

1576
  (void)taosThreadMutexLock(pLock);
25,424✔
1577

1578
  SHashObj*  pEntry = NULL;
25,424✔
1579
  SHashObj** find = taosHashGet(pStbRefs, &suid, sizeof(uint64_t));
25,424✔
1580
  if (find == NULL) {
25,424✔
1581
    code = addRefDbsCacheNewEntry(pStbRefs, suid, &pEntry);
22,039✔
1582
    TSDB_CHECK_CODE(code, line, _return);
22,039✔
1583
  } else {  // check if it exists or not
1584
    pEntry = *find;
3,385✔
1585
  }
1586

1587
  for (int32_t i = 0; i < taosArrayGetSize(pList); i++) {
44,323✔
1588
    char* dbName = taosArrayGetP(pList, i);
18,899✔
1589
    void* pItem = taosHashGet(pEntry, dbName, strlen(dbName));
18,899✔
1590
    if (pItem == NULL) {
18,899✔
1591
      code = taosHashPut(pEntry, dbName, strlen(dbName), NULL, 0);
18,899✔
1592
      TSDB_CHECK_CODE(code, line, _return);
18,899✔
1593
    }
1594
  }
1595

1596
_return:
25,424✔
1597
  if (code) {
25,424✔
1598
    metaError("%s failed at line %d since %s", __func__, line, tstrerror(code));
×
1599
  }
1600
  (void)taosThreadMutexUnlock(pLock);
25,424✔
1601

1602
  return code;
25,424✔
1603
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc