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

taosdata / TDengine / #4837

07 Nov 2025 09:40AM UTC coverage: 58.963% (+0.2%) from 58.728%
#4837

push

travis-ci

DuanKuanJun
coverity: cases_other.task add -R -Q2 -Q3 -Q4

150245 of 324452 branches covered (46.31%)

Branch coverage included in aggregate %.

200054 of 269646 relevant lines covered (74.19%)

317833830.25 hits per line

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

66.78
/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
struct SMetaCache {
48
  // child, normal, super, table entry cache
49
  struct SEntryCache {
50
    int32_t           nEntry;
51
    int32_t           nBucket;
52
    SMetaCacheEntry** aBucket;
53
  } sEntryCache;
54

55
  // stable stats cache
56
  struct SStbStatsCache {
57
    int32_t              nEntry;
58
    int32_t              nBucket;
59
    SMetaStbStatsEntry** aBucket;
60
  } sStbStatsCache;
61

62
  // query cache
63
  struct STagFilterResCache {
64
    TdThreadMutex lock;
65
    uint32_t      accTimes;
66
    SHashObj*     pTableEntry;
67
    SLRUCache*    pUidResCache;
68
  } sTagFilterResCache;
69

70
  struct STbGroupResCache {
71
    TdThreadMutex lock;
72
    uint32_t      accTimes;
73
    SHashObj*     pTableEntry;
74
    SLRUCache*    pResCache;
75
  } STbGroupResCache;
76

77
  struct STbFilterCache {
78
    SHashObj* pStb;
79
    SHashObj* pStbName;
80
  } STbFilterCache;
81

82
  struct STbRefDbCache {
83
    TdThreadMutex lock;
84
    SHashObj*     pStbRefs; // key: suid, value: SHashObj<dbName, refTimes>
85
  } STbRefDbCache;
86
};
87

88
static void entryCacheClose(SMeta* pMeta) {
76,848,716✔
89
  if (pMeta->pCache) {
76,848,716✔
90
    // close entry cache
91
    for (int32_t iBucket = 0; iBucket < pMeta->pCache->sEntryCache.nBucket; iBucket++) {
2,147,483,647✔
92
      SMetaCacheEntry* pEntry = pMeta->pCache->sEntryCache.aBucket[iBucket];
2,147,483,647✔
93
      while (pEntry) {
2,147,483,647✔
94
        SMetaCacheEntry* tEntry = pEntry->next;
843,589,414✔
95
        taosMemoryFree(pEntry);
843,589,414!
96
        pEntry = tEntry;
843,604,050✔
97
      }
98
    }
99
    taosMemoryFree(pMeta->pCache->sEntryCache.aBucket);
76,859,532!
100
  }
101
}
76,859,704✔
102

103
static void statsCacheClose(SMeta* pMeta) {
76,859,532✔
104
  if (pMeta->pCache) {
76,859,532!
105
    // close entry cache
106
    for (int32_t iBucket = 0; iBucket < pMeta->pCache->sStbStatsCache.nBucket; iBucket++) {
1,370,551,494✔
107
      SMetaStbStatsEntry* pEntry = pMeta->pCache->sStbStatsCache.aBucket[iBucket];
1,293,693,883✔
108
      while (pEntry) {
1,365,007,839✔
109
        SMetaStbStatsEntry* tEntry = pEntry->next;
71,315,877✔
110
        taosMemoryFree(pEntry);
71,315,877!
111
        pEntry = tEntry;
71,311,460✔
112
      }
113
    }
114
    taosMemoryFree(pMeta->pCache->sStbStatsCache.aBucket);
76,858,032!
115
  }
116
}
76,858,383✔
117

118
static void freeCacheEntryFp(void* param) {
11,016✔
119
  STagFilterResEntry** p = param;
11,016✔
120
  taosHashCleanup((*p)->set);
11,016✔
121
  taosMemoryFreeClear(*p);
11,016!
122
}
11,016✔
123

124
static void freeRefDbFp(void* param) {
187,977✔
125
  SHashObj** p = param;
187,977✔
126
  taosHashCleanup(*p);
187,977✔
127
  *p = NULL;
187,977✔
128
}
187,977✔
129

130
int32_t metaCacheOpen(SMeta* pMeta) {
76,811,065✔
131
  int32_t code = 0;
76,811,065✔
132
  int32_t lino;
133

134
  pMeta->pCache = (SMetaCache*)taosMemoryCalloc(1, sizeof(SMetaCache));
76,811,065!
135
  if (pMeta->pCache == NULL) {
76,854,442!
136
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
137
  }
138

139
  // open entry cache
140
  pMeta->pCache->sEntryCache.nEntry = 0;
76,853,487✔
141
  pMeta->pCache->sEntryCache.nBucket = META_CACHE_BASE_BUCKET;
76,856,682✔
142
  pMeta->pCache->sEntryCache.aBucket =
76,866,655✔
143
      (SMetaCacheEntry**)taosMemoryCalloc(pMeta->pCache->sEntryCache.nBucket, sizeof(SMetaCacheEntry*));
76,858,866!
144
  if (pMeta->pCache->sEntryCache.aBucket == NULL) {
76,858,872!
145
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
146
  }
147

148
  // open stats cache
149
  pMeta->pCache->sStbStatsCache.nEntry = 0;
76,858,872✔
150
  pMeta->pCache->sStbStatsCache.nBucket = META_CACHE_STATS_BUCKET;
76,856,947✔
151
  pMeta->pCache->sStbStatsCache.aBucket =
76,863,373✔
152
      (SMetaStbStatsEntry**)taosMemoryCalloc(pMeta->pCache->sStbStatsCache.nBucket, sizeof(SMetaStbStatsEntry*));
76,856,947!
153
  if (pMeta->pCache->sStbStatsCache.aBucket == NULL) {
76,853,229!
154
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
155
  }
156

157
  // open tag filter cache
158
  pMeta->pCache->sTagFilterResCache.pUidResCache = taosLRUCacheInit(5 * 1024 * 1024, -1, 0.5);
76,857,016✔
159
  if (pMeta->pCache->sTagFilterResCache.pUidResCache == NULL) {
76,859,532!
160
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
161
  }
162

163
  pMeta->pCache->sTagFilterResCache.accTimes = 0;
76,859,532✔
164
  pMeta->pCache->sTagFilterResCache.pTableEntry =
153,716,949✔
165
      taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), false, HASH_NO_LOCK);
153,708,260✔
166
  if (pMeta->pCache->sTagFilterResCache.pTableEntry == NULL) {
76,859,532!
167
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
168
  }
169

170
  taosHashSetFreeFp(pMeta->pCache->sTagFilterResCache.pTableEntry, freeCacheEntryFp);
76,859,532✔
171
  (void)taosThreadMutexInit(&pMeta->pCache->sTagFilterResCache.lock, NULL);
76,859,532✔
172

173
  // open group res cache
174
  pMeta->pCache->STbGroupResCache.pResCache = taosLRUCacheInit(5 * 1024 * 1024, -1, 0.5);
76,859,532✔
175
  if (pMeta->pCache->STbGroupResCache.pResCache == NULL) {
76,859,532!
176
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
177
  }
178

179
  pMeta->pCache->STbGroupResCache.accTimes = 0;
76,859,532✔
180
  pMeta->pCache->STbGroupResCache.pTableEntry =
153,719,064✔
181
      taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), false, HASH_NO_LOCK);
153,708,260✔
182
  if (pMeta->pCache->STbGroupResCache.pTableEntry == NULL) {
76,859,532!
183
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
184
  }
185

186
  taosHashSetFreeFp(pMeta->pCache->STbGroupResCache.pTableEntry, freeCacheEntryFp);
76,859,532✔
187
  (void)taosThreadMutexInit(&pMeta->pCache->STbGroupResCache.lock, NULL);
76,858,232✔
188

189
  // open filter cache
190
  pMeta->pCache->STbFilterCache.pStb =
153,717,764✔
191
      taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
153,705,660✔
192
  if (pMeta->pCache->STbFilterCache.pStb == NULL) {
76,859,532!
193
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
194
  }
195

196
  pMeta->pCache->STbFilterCache.pStbName =
153,717,937✔
197
      taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), false, HASH_NO_LOCK);
153,708,252✔
198
  if (pMeta->pCache->STbFilterCache.pStbName == NULL) {
76,859,532!
199
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
200
  }
201

202
  // open ref db cache
203
  pMeta->pCache->STbRefDbCache.pStbRefs =
153,714,195✔
204
      taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
153,703,391✔
205
  if (pMeta->pCache->STbRefDbCache.pStbRefs == NULL) {
76,859,532!
206
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
207
  }
208

209
  taosHashSetFreeFp(pMeta->pCache->STbRefDbCache.pStbRefs, freeRefDbFp);
76,859,532✔
210
  (void)taosThreadMutexInit(&pMeta->pCache->STbRefDbCache.lock, NULL);
76,859,532✔
211

212

213
_exit:
76,859,532✔
214
  if (code) {
76,859,532!
215
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
216
    metaCacheClose(pMeta);
×
217
  } else {
218
    metaDebug("vgId:%d, %s success", TD_VID(pMeta->pVnode), __func__);
76,859,532✔
219
  }
220
  return code;
76,859,532✔
221
}
222

223
void metaCacheClose(SMeta* pMeta) {
76,859,532✔
224
  if (pMeta->pCache) {
76,859,532✔
225
    entryCacheClose(pMeta);
76,858,383✔
226
    statsCacheClose(pMeta);
76,859,532✔
227

228
    taosHashClear(pMeta->pCache->sTagFilterResCache.pTableEntry);
76,858,383✔
229
    taosLRUCacheCleanup(pMeta->pCache->sTagFilterResCache.pUidResCache);
76,859,532✔
230
    (void)taosThreadMutexDestroy(&pMeta->pCache->sTagFilterResCache.lock);
76,859,532✔
231
    taosHashCleanup(pMeta->pCache->sTagFilterResCache.pTableEntry);
76,859,532✔
232

233
    taosHashClear(pMeta->pCache->STbGroupResCache.pTableEntry);
76,859,532✔
234
    taosLRUCacheCleanup(pMeta->pCache->STbGroupResCache.pResCache);
76,859,532✔
235
    (void)taosThreadMutexDestroy(&pMeta->pCache->STbGroupResCache.lock);
76,859,532✔
236
    taosHashCleanup(pMeta->pCache->STbGroupResCache.pTableEntry);
76,859,532✔
237

238
    taosHashCleanup(pMeta->pCache->STbFilterCache.pStb);
76,857,486✔
239
    taosHashCleanup(pMeta->pCache->STbFilterCache.pStbName);
76,857,486✔
240

241
    taosHashClear(pMeta->pCache->STbRefDbCache.pStbRefs);
76,859,532✔
242
    (void)taosThreadMutexDestroy(&pMeta->pCache->STbRefDbCache.lock);
76,859,532✔
243
    taosHashCleanup(pMeta->pCache->STbRefDbCache.pStbRefs);
76,859,532✔
244

245
    taosMemoryFree(pMeta->pCache);
76,859,532!
246
    pMeta->pCache = NULL;
76,859,430✔
247
  }
248
}
76,860,579✔
249

250
static void metaRehashCache(SMetaCache* pCache, int8_t expand) {
227,955✔
251
  int32_t code = 0;
227,955✔
252
  int32_t nBucket;
253

254
  if (expand) {
227,955!
255
    nBucket = pCache->sEntryCache.nBucket * 2;
227,955✔
256
  } else {
257
    nBucket = pCache->sEntryCache.nBucket / 2;
×
258
  }
259

260
  SMetaCacheEntry** aBucket = (SMetaCacheEntry**)taosMemoryCalloc(nBucket, sizeof(SMetaCacheEntry*));
227,955!
261
  if (aBucket == NULL) {
227,955!
262
    return;
×
263
  }
264

265
  // rehash
266
  for (int32_t iBucket = 0; iBucket < pCache->sEntryCache.nBucket; iBucket++) {
615,174,771✔
267
    SMetaCacheEntry* pEntry = pCache->sEntryCache.aBucket[iBucket];
614,946,816✔
268

269
    while (pEntry) {
1,229,893,632✔
270
      SMetaCacheEntry* pTEntry = pEntry->next;
614,946,816✔
271

272
      pEntry->next = aBucket[TABS(pEntry->info.uid) % nBucket];
614,946,816!
273
      aBucket[TABS(pEntry->info.uid) % nBucket] = pEntry;
614,946,816!
274

275
      pEntry = pTEntry;
614,946,816✔
276
    }
277
  }
278

279
  // final set
280
  taosMemoryFree(pCache->sEntryCache.aBucket);
227,955!
281
  pCache->sEntryCache.nBucket = nBucket;
227,955✔
282
  pCache->sEntryCache.aBucket = aBucket;
227,955✔
283
  return;
227,955✔
284
}
285

286
int32_t metaCacheUpsert(SMeta* pMeta, SMetaInfo* pInfo) {
1,002,307,544✔
287
  int32_t code = 0;
1,002,307,544✔
288

289
  // meta is wlocked for calling this func.
290

291
  // search
292
  SMetaCache*       pCache = pMeta->pCache;
1,002,307,544✔
293
  int32_t           iBucket = TABS(pInfo->uid) % pCache->sEntryCache.nBucket;
1,002,658,252!
294
  SMetaCacheEntry** ppEntry = &pCache->sEntryCache.aBucket[iBucket];
1,002,598,611✔
295
  while (*ppEntry && (*ppEntry)->info.uid != pInfo->uid) {
1,173,726,057✔
296
    ppEntry = &(*ppEntry)->next;
171,167,863✔
297
  }
298

299
  if (*ppEntry) {  // update
1,002,123,026✔
300
    if (pInfo->suid != (*ppEntry)->info.suid) {
132,747,745!
301
      metaError("meta/cache: suid should be same as the one in cache.");
×
302
      return TSDB_CODE_INVALID_PARA;
×
303
    }
304
    if (pInfo->version > (*ppEntry)->info.version) {
132,470,389✔
305
      (*ppEntry)->info.version = pInfo->version;
132,749,189✔
306
      (*ppEntry)->info.skmVer = pInfo->skmVer;
132,517,698✔
307
    }
308
  } else {  // insert
309
    if (pCache->sEntryCache.nEntry >= pCache->sEntryCache.nBucket) {
869,768,957✔
310
      metaRehashCache(pCache, 1);
227,955✔
311

312
      iBucket = TABS(pInfo->uid) % pCache->sEntryCache.nBucket;
227,955!
313
    }
314

315
    SMetaCacheEntry* pEntryNew = (SMetaCacheEntry*)taosMemoryMalloc(sizeof(*pEntryNew));
869,892,199✔
316
    if (pEntryNew == NULL) {
869,607,841!
317
      code = terrno;
×
318
      goto _exit;
×
319
    }
320

321
    pEntryNew->info = *pInfo;
869,607,841✔
322
    pEntryNew->next = pCache->sEntryCache.aBucket[iBucket];
869,680,229✔
323
    pCache->sEntryCache.aBucket[iBucket] = pEntryNew;
869,086,010✔
324
    pCache->sEntryCache.nEntry++;
869,693,189✔
325
  }
326

327
_exit:
1,002,505,166✔
328
  return code;
1,002,505,166✔
329
}
330

331
int32_t metaCacheDrop(SMeta* pMeta, int64_t uid) {
27,278,850✔
332
  int32_t code = 0;
27,278,850✔
333

334
  SMetaCache*       pCache = pMeta->pCache;
27,278,850✔
335
  int32_t           iBucket = TABS(uid) % pCache->sEntryCache.nBucket;
27,282,670!
336
  SMetaCacheEntry** ppEntry = &pCache->sEntryCache.aBucket[iBucket];
27,287,317✔
337
  while (*ppEntry && (*ppEntry)->info.uid != uid) {
27,433,681✔
338
    ppEntry = &(*ppEntry)->next;
146,505✔
339
  }
340

341
  SMetaCacheEntry* pEntry = *ppEntry;
27,285,604✔
342
  if (pEntry) {
27,287,969✔
343
    *ppEntry = pEntry->next;
26,508,206✔
344
    taosMemoryFree(pEntry);
26,511,001!
345
    pCache->sEntryCache.nEntry--;
26,502,136✔
346
    if (pCache->sEntryCache.nEntry < pCache->sEntryCache.nBucket / 4 &&
26,507,362✔
347
        pCache->sEntryCache.nBucket > META_CACHE_BASE_BUCKET) {
24,211,991!
348
      metaRehashCache(pCache, 0);
×
349
    }
350
  } else {
351
    code = TSDB_CODE_NOT_FOUND;
779,763✔
352
  }
353

354
_exit:
27,278,145✔
355
  return code;
27,278,145✔
356
}
357

358
int32_t metaCacheGet(SMeta* pMeta, int64_t uid, SMetaInfo* pInfo) {
2,147,483,647✔
359
  int32_t code = 0;
2,147,483,647✔
360

361
  SMetaCache*      pCache = pMeta->pCache;
2,147,483,647✔
362
  int32_t          iBucket = TABS(uid) % pCache->sEntryCache.nBucket;
2,147,483,647!
363
  SMetaCacheEntry* pEntry = pCache->sEntryCache.aBucket[iBucket];
2,147,483,647✔
364

365
  while (pEntry && pEntry->info.uid != uid) {
2,147,483,647✔
366
    pEntry = pEntry->next;
556,784,106✔
367
  }
368

369
  if (pEntry) {
2,147,483,647✔
370
    if (pInfo) {
2,147,483,647✔
371
      *pInfo = pEntry->info;
2,147,483,647✔
372
    }
373
  } else {
374
    code = TSDB_CODE_NOT_FOUND;
964,467,852✔
375
  }
376

377
  return code;
2,147,483,647✔
378
}
379

380
static int32_t metaRehashStatsCache(SMetaCache* pCache, int8_t expand) {
1,392,439✔
381
  int32_t code = 0;
1,392,439✔
382
  int32_t nBucket;
383

384
  if (expand) {
1,392,439✔
385
    nBucket = pCache->sStbStatsCache.nBucket * 2;
1,333,694✔
386
  } else {
387
    nBucket = pCache->sStbStatsCache.nBucket / 2;
58,745✔
388
  }
389

390
  SMetaStbStatsEntry** aBucket = (SMetaStbStatsEntry**)taosMemoryCalloc(nBucket, sizeof(SMetaStbStatsEntry*));
1,392,439!
391
  if (aBucket == NULL) {
1,392,439!
392
    code = terrno;
×
393
    goto _exit;
×
394
  }
395

396
  // rehash
397
  for (int32_t iBucket = 0; iBucket < pCache->sStbStatsCache.nBucket; iBucket++) {
69,711,463✔
398
    SMetaStbStatsEntry* pEntry = pCache->sStbStatsCache.aBucket[iBucket];
68,319,024✔
399

400
    while (pEntry) {
134,486,647✔
401
      SMetaStbStatsEntry* pTEntry = pEntry->next;
66,167,623✔
402

403
      pEntry->next = aBucket[TABS(pEntry->info.uid) % nBucket];
66,167,623!
404
      aBucket[TABS(pEntry->info.uid) % nBucket] = pEntry;
66,167,623!
405

406
      pEntry = pTEntry;
66,167,623✔
407
    }
408
  }
409

410
  // final set
411
  taosMemoryFree(pCache->sStbStatsCache.aBucket);
1,392,439!
412
  pCache->sStbStatsCache.nBucket = nBucket;
1,392,439✔
413
  pCache->sStbStatsCache.aBucket = aBucket;
1,392,439✔
414

415
_exit:
1,392,439✔
416
  return code;
1,392,439✔
417
}
418

419
int32_t metaStatsCacheUpsert(SMeta* pMeta, SMetaStbStats* pInfo) {
770,016,623✔
420
  int32_t code = 0;
770,016,623✔
421

422
  // meta is wlocked for calling this func.
423

424
  // search
425
  SMetaCache*          pCache = pMeta->pCache;
770,016,623✔
426
  int32_t              iBucket = TABS(pInfo->uid) % pCache->sStbStatsCache.nBucket;
770,158,043!
427
  SMetaStbStatsEntry** ppEntry = &pCache->sStbStatsCache.aBucket[iBucket];
770,169,574✔
428
  while (*ppEntry && (*ppEntry)->info.uid != pInfo->uid) {
806,182,939✔
429
    ppEntry = &(*ppEntry)->next;
36,212,345✔
430
  }
431

432
  if (*ppEntry) {  // update
769,807,763✔
433
    (*ppEntry)->info.ctbNum = pInfo->ctbNum;
693,646,799✔
434
    (*ppEntry)->info.colNum = pInfo->colNum;
693,729,054✔
435
    (*ppEntry)->info.flags = pInfo->flags;
693,774,899✔
436
    (*ppEntry)->info.keep = pInfo->keep;
693,617,160✔
437
  } else {  // insert
438
    if (pCache->sStbStatsCache.nEntry >= pCache->sStbStatsCache.nBucket) {
76,324,900✔
439
      TAOS_UNUSED(metaRehashStatsCache(pCache, 1));
1,333,694✔
440
      iBucket = TABS(pInfo->uid) % pCache->sStbStatsCache.nBucket;
1,333,694!
441
    }
442

443
    SMetaStbStatsEntry* pEntryNew = (SMetaStbStatsEntry*)taosMemoryMalloc(sizeof(*pEntryNew));
76,324,946!
444
    if (pEntryNew == NULL) {
76,324,149!
445
      code = terrno;
×
446
      goto _exit;
×
447
    }
448

449
    pEntryNew->info = *pInfo;
76,324,149✔
450
    pEntryNew->next = pCache->sStbStatsCache.aBucket[iBucket];
76,327,733✔
451
    pCache->sStbStatsCache.aBucket[iBucket] = pEntryNew;
76,320,149✔
452
    pCache->sStbStatsCache.nEntry++;
76,326,048✔
453
  }
454

455
_exit:
769,857,352✔
456
  return code;
769,857,352✔
457
}
458

459
int32_t metaStatsCacheDrop(SMeta* pMeta, int64_t uid) {
7,462,021✔
460
  int32_t code = 0;
7,462,021✔
461

462
  SMetaCache*          pCache = pMeta->pCache;
7,462,021✔
463
  int32_t              iBucket = TABS(uid) % pCache->sStbStatsCache.nBucket;
7,463,077!
464
  SMetaStbStatsEntry** ppEntry = &pCache->sStbStatsCache.aBucket[iBucket];
7,463,077✔
465
  while (*ppEntry && (*ppEntry)->info.uid != uid) {
8,563,147✔
466
    ppEntry = &(*ppEntry)->next;
1,101,126✔
467
  }
468

469
  SMetaStbStatsEntry* pEntry = *ppEntry;
7,462,021✔
470
  if (pEntry) {
7,465,662✔
471
    *ppEntry = pEntry->next;
4,970,435✔
472
    taosMemoryFree(pEntry);
4,969,379!
473
    pCache->sStbStatsCache.nEntry--;
4,969,379✔
474
    if (pCache->sStbStatsCache.nEntry < pCache->sStbStatsCache.nBucket / 4 &&
4,969,379✔
475
        pCache->sStbStatsCache.nBucket > META_CACHE_STATS_BUCKET) {
3,127,189✔
476
      TAOS_UNUSED(metaRehashStatsCache(pCache, 0));
58,745✔
477
    }
478
  } else {
479
    code = TSDB_CODE_NOT_FOUND;
2,495,227✔
480
  }
481

482
_exit:
7,464,606✔
483
  return code;
7,464,606✔
484
}
485

486
int32_t metaStatsCacheGet(SMeta* pMeta, int64_t uid, SMetaStbStats* pInfo) {
2,147,483,647✔
487
  int32_t code = TSDB_CODE_SUCCESS;
2,147,483,647✔
488

489
  SMetaCache*         pCache = pMeta->pCache;
2,147,483,647✔
490
  int32_t             iBucket = TABS(uid) % pCache->sStbStatsCache.nBucket;
2,147,483,647!
491
  SMetaStbStatsEntry* pEntry = pCache->sStbStatsCache.aBucket[iBucket];
2,147,483,647✔
492

493
  while (pEntry && pEntry->info.uid != uid) {
2,147,483,647✔
494
    pEntry = pEntry->next;
338,197,711✔
495
  }
496

497
  if (pEntry) {
2,147,483,647✔
498
    if (pInfo) {
2,147,483,647✔
499
      *pInfo = pEntry->info;
2,147,483,647✔
500
    }
501
  } else {
502
    code = TSDB_CODE_NOT_FOUND;
149,985,959✔
503
  }
504

505
  return code;
2,147,483,647✔
506
}
507

508
static FORCE_INLINE void setMD5DigestInKey(uint64_t* pBuf, const char* key, int32_t keyLen) {
509
  memcpy(&pBuf[2], key, keyLen);
1,503,515,533!
510
}
1,503,482,117✔
511

512
// the format of key:
513
// hash table address(8bytes) + suid(8bytes) + MD5 digest(16bytes)
514
static void initCacheKey(uint64_t* buf, const SHashObj* pHashMap, uint64_t suid, const char* key, int32_t keyLen) {
1,503,341,959✔
515
  buf[0] = (uint64_t)pHashMap;
1,503,341,959✔
516
  buf[1] = suid;
1,503,391,965✔
517
  setMD5DigestInKey(buf, key, keyLen);
518
}
1,503,508,189✔
519

520
int32_t metaGetCachedTableUidList(void* pVnode, tb_uid_t suid, const uint8_t* pKey, int32_t keyLen, SArray* pList1,
77,112✔
521
                                  bool* acquireRes) {
522
  SMeta*  pMeta = ((SVnode*)pVnode)->pMeta;
77,112✔
523
  int32_t vgId = TD_VID(pMeta->pVnode);
77,112✔
524

525
  // generate the composed key for LRU cache
526
  SLRUCache*     pCache = pMeta->pCache->sTagFilterResCache.pUidResCache;
77,112✔
527
  SHashObj*      pTableMap = pMeta->pCache->sTagFilterResCache.pTableEntry;
77,112✔
528
  TdThreadMutex* pLock = &pMeta->pCache->sTagFilterResCache.lock;
77,112✔
529

530
  *acquireRes = 0;
77,112✔
531
  uint64_t key[4];
77,112✔
532
  initCacheKey(key, pTableMap, suid, (const char*)pKey, keyLen);
77,112✔
533
  
534
  // void* tmp = NULL;
535
  // uint32_t len = 0;
536
  // (void)taosAscii2Hex((const char*)key, 32, &tmp, &len);
537
  // qDebug("metaGetCachedTableUidList %p %"PRId64" key: %s", pTableMap, suid, tmp);
538
  // taosMemoryFree(tmp);
539

540
  (void)taosThreadMutexLock(pLock);
77,112✔
541
  pMeta->pCache->sTagFilterResCache.accTimes += 1;
77,112✔
542

543
  LRUHandle* pHandle = taosLRUCacheLookup(pCache, key, TAG_FILTER_RES_KEY_LEN);
77,112✔
544
  if (pHandle == NULL) {
77,112✔
545
    (void)taosThreadMutexUnlock(pLock);
29,376✔
546
    return TSDB_CODE_SUCCESS;
29,376✔
547
  }
548

549
  // do some book mark work after acquiring the filter result from cache
550
  STagFilterResEntry** pEntry = taosHashGet(pTableMap, &suid, sizeof(uint64_t));
47,736✔
551
  if (NULL == pEntry) {
47,736!
552
    metaError("meta/cache: pEntry should not be NULL.");
×
553
    return TSDB_CODE_NOT_FOUND;
×
554
  }
555

556
  *acquireRes = 1;
47,736✔
557

558
  const char* p = taosLRUCacheValue(pCache, pHandle);
47,736✔
559
  int32_t     size = *(int32_t*)p;
47,736✔
560

561
  // set the result into the buffer
562
  if (taosArrayAddBatch(pList1, p + sizeof(int32_t), size) == NULL) {
47,736!
563
    return terrno;
×
564
  }
565

566
  (*pEntry)->hitTimes += 1;
47,736✔
567

568
  uint32_t acc = pMeta->pCache->sTagFilterResCache.accTimes;
47,736✔
569
  if ((*pEntry)->hitTimes % 5000 == 0 && (*pEntry)->hitTimes > 0) {
47,736!
570
    metaInfo("vgId:%d cache hit:%d, total acc:%d, rate:%.2f", vgId, (*pEntry)->hitTimes, acc,
×
571
             ((double)(*pEntry)->hitTimes) / acc);
572
  }
573

574
  bool ret = taosLRUCacheRelease(pCache, pHandle, false);
47,736✔
575

576
  // unlock meta
577
  (void)taosThreadMutexUnlock(pLock);
47,736✔
578
  return TSDB_CODE_SUCCESS;
47,736✔
579
}
580

581
static void freeUidCachePayload(const void* key, size_t keyLen, void* value, void* ud) {
29,376✔
582
  (void)ud;
583
  if (value == NULL) {
29,376!
584
    return;
×
585
  }
586

587
  const uint64_t* p = key;
29,376✔
588
  if (keyLen != sizeof(int64_t) * 4) {
29,376!
589
    metaError("key length is invalid, length:%d, expect:%d", (int32_t)keyLen, (int32_t)sizeof(uint64_t) * 2);
×
590
    return;
×
591
  }
592

593
  SHashObj* pHashObj = (SHashObj*)p[0];
29,376✔
594

595
  STagFilterResEntry** pEntry = taosHashGet(pHashObj, &p[1], sizeof(uint64_t));
29,376✔
596

597
  if (pEntry != NULL && (*pEntry) != NULL) {
29,376!
598
    int64_t st = taosGetTimestampUs();
7,344✔
599
    int32_t code = taosHashRemove((*pEntry)->set, &p[2], sizeof(uint64_t) * 2);
7,344✔
600
    if (code == TSDB_CODE_SUCCESS) {
7,344!
601
      double el = (taosGetTimestampUs() - st) / 1000.0;
7,344✔
602
      metaInfo("clear items in meta-cache, remain cached item:%d, elapsed time:%.2fms", taosHashGetSize((*pEntry)->set),
7,344!
603
               el);
604
    }
605
  }
606

607
  taosMemoryFree(value);
29,376!
608
}
609

610
static int32_t addNewEntry(SHashObj* pTableEntry, const void* pKey, int32_t keyLen, uint64_t suid) {
11,016✔
611
  int32_t             code = TSDB_CODE_SUCCESS;
11,016✔
612
  int32_t             lino = 0;
11,016✔
613
  STagFilterResEntry* p = taosMemoryMalloc(sizeof(STagFilterResEntry));
11,016!
614
  TSDB_CHECK_NULL(p, code, lino, _end, terrno);
11,016!
615

616
  p->hitTimes = 0;
11,016✔
617
  p->set = taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
11,016✔
618
  TSDB_CHECK_NULL(p->set, code, lino, _end, terrno);
11,016!
619
  code = taosHashPut(p->set, pKey, keyLen, NULL, 0);
11,016✔
620
  TSDB_CHECK_CODE(code, lino, _end);
11,016!
621
  code = taosHashPut(pTableEntry, &suid, sizeof(uint64_t), &p, POINTER_BYTES);
11,016✔
622
  TSDB_CHECK_CODE(code, lino, _end);
11,016!
623

624
_end:
11,016✔
625
  if (code != TSDB_CODE_SUCCESS) {
11,016!
626
    metaError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
627
    if (p != NULL) {
×
628
      if (p->set != NULL) {
×
629
        taosHashCleanup(p->set);
×
630
      }
631
      taosMemoryFree(p);
×
632
    }
633
  }
634
  return code;
11,016✔
635
}
636

637
// check both the payload size and selectivity ratio
638
int32_t metaUidFilterCachePut(void* pVnode, uint64_t suid, const void* pKey, int32_t keyLen, void* pPayload,
29,376✔
639
                              int32_t payloadLen, double selectivityRatio) {
640
  int32_t code = 0;
29,376✔
641
  SMeta*  pMeta = ((SVnode*)pVnode)->pMeta;
29,376✔
642
  int32_t vgId = TD_VID(pMeta->pVnode);
29,376✔
643

644
  if (selectivityRatio > tsSelectivityRatio) {
29,376!
645
    metaDebug("vgId:%d, suid:%" PRIu64
×
646
              " failed to add to uid list cache, due to selectivity ratio %.2f less than threshold %.2f",
647
              vgId, suid, selectivityRatio, tsSelectivityRatio);
648
    taosMemoryFree(pPayload);
×
649
    return TSDB_CODE_SUCCESS;
×
650
  }
651

652
  if (payloadLen > tsTagFilterResCacheSize) {
29,376!
653
    metaDebug("vgId:%d, suid:%" PRIu64
×
654
              " failed to add to uid list cache, due to payload length %d greater than threshold %d",
655
              vgId, suid, payloadLen, tsTagFilterResCacheSize);
656
    taosMemoryFree(pPayload);
×
657
    return TSDB_CODE_SUCCESS;
×
658
  }
659

660
  SLRUCache*     pCache = pMeta->pCache->sTagFilterResCache.pUidResCache;
29,376✔
661
  SHashObj*      pTableEntry = pMeta->pCache->sTagFilterResCache.pTableEntry;
29,376✔
662
  TdThreadMutex* pLock = &pMeta->pCache->sTagFilterResCache.lock;
29,376✔
663

664
  uint64_t key[4] = {0};
29,376✔
665
  initCacheKey(key, pTableEntry, suid, pKey, keyLen);
29,376✔
666

667
  // void* tmp = NULL;
668
  // uint32_t len = 0;
669
  // (void)taosAscii2Hex((const char*)key, 32, &tmp, &len);
670
  // qDebug("metaUidFilterCachePut %p %"PRId64" key: %s", pTableEntry, suid, tmp);
671
  // taosMemoryFree(tmp);
672

673
  (void)taosThreadMutexLock(pLock);
29,376✔
674
  STagFilterResEntry** pEntry = taosHashGet(pTableEntry, &suid, sizeof(uint64_t));
29,376✔
675
  if (pEntry == NULL) {
29,376✔
676
    code = addNewEntry(pTableEntry, pKey, keyLen, suid);
11,016✔
677
    if (code != TSDB_CODE_SUCCESS) {
11,016!
678
      goto _end;
×
679
    }
680
  } else {  // check if it exists or not
681
    code = taosHashPut((*pEntry)->set, pKey, keyLen, NULL, 0);
18,360✔
682
    if (code == TSDB_CODE_DUP_KEY) {
18,360!
683
      // we have already found the existed items, no need to added to cache anymore.
684
      (void)taosThreadMutexUnlock(pLock);
×
685
      return TSDB_CODE_SUCCESS;
×
686
    }
687
    if (code != TSDB_CODE_SUCCESS) {
18,360!
688
      goto _end;
×
689
    }
690
  }
691

692
  // add to cache.
693
  (void)taosLRUCacheInsert(pCache, key, TAG_FILTER_RES_KEY_LEN, pPayload, payloadLen, freeUidCachePayload, NULL, NULL,
29,376✔
694
                           TAOS_LRU_PRIORITY_LOW, NULL);
695
_end:
29,376✔
696
  (void)taosThreadMutexUnlock(pLock);
29,376✔
697
  metaDebug("vgId:%d, suid:%" PRIu64 " list cache added into cache, total:%d, tables:%d", vgId, suid,
29,376!
698
            (int32_t)taosLRUCacheGetUsage(pCache), taosHashGetSize(pTableEntry));
699

700
  return code;
29,376✔
701
}
702

703
void metaCacheClear(SMeta* pMeta) {
38,417,780✔
704
  metaWLock(pMeta);
38,417,780✔
705
  metaCacheClose(pMeta);
38,416,048✔
706
  (void)metaCacheOpen(pMeta);
38,418,558✔
707
  metaULock(pMeta);
38,418,558✔
708
}
38,418,558✔
709

710
// remove the lru cache that are expired due to the tags value update, or creating, or dropping, of child tables
711
int32_t metaUidCacheClear(SMeta* pMeta, uint64_t suid) {
751,619,253✔
712
  uint64_t  p[4] = {0};
751,619,253✔
713
  int32_t   vgId = TD_VID(pMeta->pVnode);
751,710,732✔
714
  SHashObj* pEntryHashMap = pMeta->pCache->sTagFilterResCache.pTableEntry;
751,730,583✔
715

716
  uint64_t dummy[2] = {0};
751,765,053✔
717
  initCacheKey(p, pEntryHashMap, suid, (char*)&dummy[0], 16);
751,755,645✔
718

719
  TdThreadMutex* pLock = &pMeta->pCache->sTagFilterResCache.lock;
751,683,431✔
720
  (void)taosThreadMutexLock(pLock);
751,752,849✔
721

722
  STagFilterResEntry** pEntry = taosHashGet(pEntryHashMap, &suid, sizeof(uint64_t));
751,820,559✔
723
  if (pEntry == NULL || taosHashGetSize((*pEntry)->set) == 0) {
751,748,815!
724
    (void)taosThreadMutexUnlock(pLock);
751,741,471✔
725
    return TSDB_CODE_SUCCESS;
751,751,190✔
726
  }
727

728
  (*pEntry)->hitTimes = 0;
7,344✔
729

730
  char *iter = taosHashIterate((*pEntry)->set, NULL);
7,344✔
731
  while (iter != NULL) {
14,688✔
732
    setMD5DigestInKey(p, iter, 2 * sizeof(uint64_t));
733
    taosLRUCacheErase(pMeta->pCache->sTagFilterResCache.pUidResCache, p, TAG_FILTER_RES_KEY_LEN);
7,344✔
734
    iter = taosHashIterate((*pEntry)->set, iter);
7,344✔
735
  }
736
  taosHashClear((*pEntry)->set);
7,344✔
737
  (void)taosThreadMutexUnlock(pLock);
7,344✔
738

739
  metaDebug("vgId:%d suid:%" PRId64 " cached related tag filter uid list cleared", vgId, suid);
7,344!
740
  return TSDB_CODE_SUCCESS;
7,344✔
741
}
742

743
int32_t metaGetCachedTbGroup(void* pVnode, tb_uid_t suid, const uint8_t* pKey, int32_t keyLen, SArray** pList) {
×
744
  SMeta*  pMeta = ((SVnode*)pVnode)->pMeta;
×
745
  int32_t vgId = TD_VID(pMeta->pVnode);
×
746

747
  // generate the composed key for LRU cache
748
  SLRUCache*     pCache = pMeta->pCache->STbGroupResCache.pResCache;
×
749
  SHashObj*      pTableMap = pMeta->pCache->STbGroupResCache.pTableEntry;
×
750
  TdThreadMutex* pLock = &pMeta->pCache->STbGroupResCache.lock;
×
751

752
  *pList = NULL;
×
753
  uint64_t key[4];
×
754
  initCacheKey(key, pTableMap, suid, (const char*)pKey, keyLen);
×
755

756
  (void)taosThreadMutexLock(pLock);
×
757
  pMeta->pCache->STbGroupResCache.accTimes += 1;
×
758

759
  LRUHandle* pHandle = taosLRUCacheLookup(pCache, key, TAG_FILTER_RES_KEY_LEN);
×
760
  if (pHandle == NULL) {
×
761
    (void)taosThreadMutexUnlock(pLock);
×
762
    return TSDB_CODE_SUCCESS;
×
763
  }
764

765
  STagFilterResEntry** pEntry = taosHashGet(pTableMap, &suid, sizeof(uint64_t));
×
766
  if (NULL == pEntry) {
×
767
    metaDebug("suid %" PRIu64 " not in tb group cache", suid);
×
768
    return TSDB_CODE_NOT_FOUND;
×
769
  }
770

771
  *pList = taosArrayDup(taosLRUCacheValue(pCache, pHandle), NULL);
×
772

773
  (*pEntry)->hitTimes += 1;
×
774

775
  uint32_t acc = pMeta->pCache->STbGroupResCache.accTimes;
×
776
  if ((*pEntry)->hitTimes % 5000 == 0 && (*pEntry)->hitTimes > 0) {
×
777
    metaInfo("vgId:%d tb group cache hit:%d, total acc:%d, rate:%.2f", vgId, (*pEntry)->hitTimes, acc,
×
778
             ((double)(*pEntry)->hitTimes) / acc);
779
  }
780

781
  bool ret = taosLRUCacheRelease(pCache, pHandle, false);
×
782

783
  // unlock meta
784
  (void)taosThreadMutexUnlock(pLock);
×
785
  return TSDB_CODE_SUCCESS;
×
786
}
787

788
static void freeTbGroupCachePayload(const void* key, size_t keyLen, void* value, void* ud) {
×
789
  (void)ud;
790
  if (value == NULL) {
×
791
    return;
×
792
  }
793

794
  const uint64_t* p = key;
×
795
  if (keyLen != sizeof(int64_t) * 4) {
×
796
    metaError("tb group key length is invalid, length:%d, expect:%d", (int32_t)keyLen, (int32_t)sizeof(uint64_t) * 2);
×
797
    return;
×
798
  }
799

800
  SHashObj* pHashObj = (SHashObj*)p[0];
×
801

802
  STagFilterResEntry** pEntry = taosHashGet(pHashObj, &p[1], sizeof(uint64_t));
×
803

804
  if (pEntry != NULL && (*pEntry) != NULL) {
×
805
    int64_t st = taosGetTimestampUs();
×
806
    int32_t code = taosHashRemove((*pEntry)->set, &p[2], sizeof(uint64_t) * 2);
×
807
    if (code == TSDB_CODE_SUCCESS) {
×
808
      double el = (taosGetTimestampUs() - st) / 1000.0;
×
809
      metaDebug("clear one item in tb group cache, remain cached item:%d, elapsed time:%.2fms",
×
810
                taosHashGetSize((*pEntry)->set), el);
811
    }
812
  }
813

814
  taosArrayDestroy((SArray*)value);
×
815
}
816

817
int32_t metaPutTbGroupToCache(void* pVnode, uint64_t suid, const void* pKey, int32_t keyLen, void* pPayload,
×
818
                              int32_t payloadLen) {
819
  int32_t code = 0;
×
820
  SMeta*  pMeta = ((SVnode*)pVnode)->pMeta;
×
821
  int32_t vgId = TD_VID(pMeta->pVnode);
×
822

823
  if (payloadLen > tsTagFilterResCacheSize) {
×
824
    metaDebug("vgId:%d, suid:%" PRIu64
×
825
              " ignore to add to tb group cache, due to payload length %d greater than threshold %d",
826
              vgId, suid, payloadLen, tsTagFilterResCacheSize);
827
    taosArrayDestroy((SArray*)pPayload);
×
828
    return TSDB_CODE_SUCCESS;
×
829
  }
830

831
  SLRUCache*     pCache = pMeta->pCache->STbGroupResCache.pResCache;
×
832
  SHashObj*      pTableEntry = pMeta->pCache->STbGroupResCache.pTableEntry;
×
833
  TdThreadMutex* pLock = &pMeta->pCache->STbGroupResCache.lock;
×
834

835
  uint64_t key[4] = {0};
×
836
  initCacheKey(key, pTableEntry, suid, pKey, keyLen);
×
837

838
  (void)taosThreadMutexLock(pLock);
×
839
  STagFilterResEntry** pEntry = taosHashGet(pTableEntry, &suid, sizeof(uint64_t));
×
840
  if (pEntry == NULL) {
×
841
    code = addNewEntry(pTableEntry, pKey, keyLen, suid);
×
842
    if (code != TSDB_CODE_SUCCESS) {
×
843
      goto _end;
×
844
    }
845
  } else {  // check if it exists or not
846
    code = taosHashPut((*pEntry)->set, pKey, keyLen, NULL, 0);
×
847
    if (code == TSDB_CODE_DUP_KEY) {
×
848
      // we have already found the existed items, no need to added to cache anymore.
849
      (void)taosThreadMutexUnlock(pLock);
×
850
      return TSDB_CODE_SUCCESS;
×
851
    }
852
    if (code != TSDB_CODE_SUCCESS) {
×
853
      goto _end;
×
854
    }
855
  }
856

857
  // add to cache.
858
  (void)taosLRUCacheInsert(pCache, key, TAG_FILTER_RES_KEY_LEN, pPayload, payloadLen, freeTbGroupCachePayload, NULL, NULL,
×
859
                           TAOS_LRU_PRIORITY_LOW, NULL);
860
_end:
×
861
  (void)taosThreadMutexUnlock(pLock);
×
862
  metaDebug("vgId:%d, suid:%" PRIu64 " tb group added into cache, total:%d, tables:%d", vgId, suid,
×
863
            (int32_t)taosLRUCacheGetUsage(pCache), taosHashGetSize(pTableEntry));
864

865
  return code;
×
866
}
867

868
// remove the lru cache that are expired due to the tags value update, or creating, or dropping, of child tables
869
int32_t metaTbGroupCacheClear(SMeta* pMeta, uint64_t suid) {
751,589,570✔
870
  uint64_t  p[4] = {0};
751,589,570✔
871
  int32_t   vgId = TD_VID(pMeta->pVnode);
751,660,091✔
872
  SHashObj* pEntryHashMap = pMeta->pCache->STbGroupResCache.pTableEntry;
751,562,057✔
873

874
  uint64_t dummy[2] = {0};
751,691,901✔
875
  initCacheKey(p, pEntryHashMap, suid, (char*)&dummy[0], 16);
751,726,185✔
876

877
  TdThreadMutex* pLock = &pMeta->pCache->STbGroupResCache.lock;
751,686,912✔
878
  (void)taosThreadMutexLock(pLock);
751,645,173✔
879

880
  STagFilterResEntry** pEntry = taosHashGet(pEntryHashMap, &suid, sizeof(uint64_t));
751,649,702✔
881
  if (pEntry == NULL || taosHashGetSize((*pEntry)->set) == 0) {
751,805,754!
882
    (void)taosThreadMutexUnlock(pLock);
751,805,754✔
883
    return TSDB_CODE_SUCCESS;
751,852,024✔
884
  }
885

886
  (*pEntry)->hitTimes = 0;
×
887

888
  char *iter = taosHashIterate((*pEntry)->set, NULL);
×
889
  while (iter != NULL) {
×
890
    setMD5DigestInKey(p, iter, 2 * sizeof(uint64_t));
891
    taosLRUCacheErase(pMeta->pCache->STbGroupResCache.pResCache, p, TAG_FILTER_RES_KEY_LEN);
×
892
    iter = taosHashIterate((*pEntry)->set, iter);
×
893
  }
894
  taosHashClear((*pEntry)->set);
×
895
  (void)taosThreadMutexUnlock(pLock);
×
896

897
  metaDebug("vgId:%d suid:%" PRId64 " cached related tb group cleared", vgId, suid);
×
898
  return TSDB_CODE_SUCCESS;
×
899
}
900

901
bool metaTbInFilterCache(SMeta* pMeta, const void* key, int8_t type) {
1,390,765,445✔
902
  if (type == 0 && taosHashGet(pMeta->pCache->STbFilterCache.pStb, key, sizeof(tb_uid_t))) {
1,390,765,445!
903
    return true;
38,670✔
904
  }
905

906
  if (type == 1 && taosHashGet(pMeta->pCache->STbFilterCache.pStbName, key, strlen(key))) {
1,390,726,775!
907
    return true;
×
908
  }
909

910
  return false;
1,390,951,739✔
911
}
912

913
int32_t metaPutTbToFilterCache(SMeta* pMeta, const void* key, int8_t type) {
239,754✔
914
  if (type == 0) {
239,754✔
915
    return taosHashPut(pMeta->pCache->STbFilterCache.pStb, key, sizeof(tb_uid_t), NULL, 0);
92,808✔
916
  }
917

918
  if (type == 1) {
146,946!
919
    return taosHashPut(pMeta->pCache->STbFilterCache.pStbName, key, strlen(key), NULL, 0);
146,946!
920
  }
921

922
  return 0;
×
923
}
924

925
int32_t metaSizeOfTbFilterCache(SMeta* pMeta, int8_t type) {
141,618✔
926
  if (type == 0) {
141,618!
927
    return taosHashGetSize(pMeta->pCache->STbFilterCache.pStb);
141,618✔
928
  }
929
  return 0;
×
930
}
931

932
int32_t metaInitTbFilterCache(SMeta* pMeta) {
38,435,514✔
933
#ifdef TD_ENTERPRISE
934
  int32_t      tbNum = 0;
38,435,514✔
935
  const char** pTbArr = NULL;
38,435,514✔
936
  const char*  dbName = NULL;
38,435,514✔
937

938
  if (!(dbName = strchr(pMeta->pVnode->config.dbname, '.'))) return 0;
38,435,514!
939
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
38,427,056!
940
    tbNum = tkLogStbNum;
7,734✔
941
    pTbArr = (const char**)&tkLogStb;
7,734✔
942
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
38,419,322!
943
    tbNum = tkAuditStbNum;
×
944
    pTbArr = (const char**)&tkAuditStb;
×
945
  }
946
  if (tbNum && pTbArr) {
38,427,056!
947
    for (int32_t i = 0; i < tbNum; ++i) {
154,680✔
948
      TAOS_CHECK_RETURN(metaPutTbToFilterCache(pMeta, pTbArr[i], 1));
146,946!
949
    }
950
  }
951
#else
952
#endif
953
  return 0;
38,427,056✔
954
}
955

956
int64_t metaGetStbKeep(SMeta* pMeta, int64_t uid) {
993,210✔
957
  SMetaStbStats stats = {0};
993,210✔
958

959
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
993,210✔
960
    return stats.keep;
812,000✔
961
  }
962

963
  SMetaEntry* pEntry = NULL;
181,210✔
964
  if (metaFetchEntryByUid(pMeta, uid, &pEntry) == TSDB_CODE_SUCCESS) {
181,210✔
965
    int64_t keep = -1;
177,150✔
966
    if (pEntry->type == TSDB_SUPER_TABLE) {
177,150!
967
      keep = pEntry->stbEntry.keep;
177,150✔
968
    }
969
    metaFetchEntryFree(&pEntry);
177,150✔
970
    return keep;
177,150✔
971
  }
972
  
973
  return -1;
4,060✔
974
}
975

976
int32_t metaRefDbsCacheClear(SMeta* pMeta, uint64_t suid) {
2,236,714✔
977
  int32_t        code = TSDB_CODE_SUCCESS;
2,236,714✔
978
  int32_t        vgId = TD_VID(pMeta->pVnode);
2,236,714✔
979
  SHashObj*      pEntryHashMap = pMeta->pCache->STbRefDbCache.pStbRefs;
2,236,714✔
980
  TdThreadMutex* pLock = &pMeta->pCache->STbRefDbCache.lock;
2,236,714✔
981

982
  (void)taosThreadMutexLock(pLock);
2,236,714✔
983

984
  SHashObj** pEntry = taosHashGet(pEntryHashMap, &suid, sizeof(uint64_t));
2,236,714✔
985
  if (pEntry == NULL) {
2,236,714✔
986
    goto _return;
2,214,112✔
987
  }
988

989
  code = taosHashRemove(pEntryHashMap, &suid, sizeof(uint64_t));
22,602✔
990

991
  metaDebug("vgId:%d suid:%" PRId64 " cached virtual stable ref db cleared", vgId, suid);
22,602✔
992

993
_return:
15,372✔
994
  (void)taosThreadMutexUnlock(pLock);
2,236,714✔
995
  return code;
2,236,714✔
996
}
997

998
int32_t metaGetCachedRefDbs(void* pVnode, tb_uid_t suid, SArray* pList) {
14,526,791✔
999
  int32_t        code = TSDB_CODE_SUCCESS;
14,526,791✔
1000
  int32_t        line = 0;
14,526,791✔
1001
  SMeta*         pMeta = ((SVnode*)pVnode)->pMeta;
14,526,791✔
1002
  SHashObj*      pTableMap = pMeta->pCache->STbRefDbCache.pStbRefs;
14,533,214✔
1003
  TdThreadMutex* pLock = &pMeta->pCache->STbRefDbCache.lock;
14,551,830✔
1004

1005
  (void)taosThreadMutexLock(pLock);
14,551,622✔
1006

1007
  SHashObj** pEntry = taosHashGet(pTableMap, &suid, sizeof(uint64_t));
14,554,026✔
1008
  if (pEntry) {
14,548,418✔
1009
    void *iter = taosHashIterate(*pEntry, NULL);
14,360,441✔
1010
    while (iter != NULL) {
49,995,993✔
1011
      size_t   dbNameLen = 0;
35,629,944✔
1012
      char*    name = NULL;
35,633,939✔
1013
      char*    dbName = NULL;
35,633,939✔
1014
      name = taosHashGetKey(iter, &dbNameLen);
35,637,906✔
1015
      TSDB_CHECK_NULL(name, code, line, _return, terrno);
35,635,552!
1016
      dbName = taosMemoryMalloc(dbNameLen + 1);
35,635,552!
1017
      TSDB_CHECK_NULL(dbName, code, line, _return, terrno);
35,602,804!
1018
      tstrncpy(dbName, name, dbNameLen + 1);
35,602,804!
1019
      TSDB_CHECK_NULL(taosArrayPush(pList, &dbName), code, line, _return, terrno);
35,642,692!
1020
      iter = taosHashIterate(*pEntry, iter);
35,642,692✔
1021
    }
1022
  }
1023

1024
_return:
14,554,026✔
1025
  if (code) {
14,554,715!
1026
    metaError("%s failed at line %d since %s", __func__, line, tstrerror(code));
×
1027
  }
1028
  (void)taosThreadMutexUnlock(pLock);
14,554,715✔
1029
  return code;
14,554,026✔
1030
}
1031

1032
static int32_t addRefDbsCacheNewEntry(SHashObj* pRefDbs, uint64_t suid, SHashObj **pEntry) {
187,977✔
1033
  int32_t      code = TSDB_CODE_SUCCESS;
187,977✔
1034
  int32_t      lino = 0;
187,977✔
1035
  SHashObj*    p = NULL;
187,977✔
1036

1037
  p = taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
187,977✔
1038
  TSDB_CHECK_NULL(p, code, lino, _end, terrno);
187,977!
1039

1040
  code = taosHashPut(pRefDbs, &suid, sizeof(uint64_t), &p, POINTER_BYTES);
187,977✔
1041
  TSDB_CHECK_CODE(code, lino, _end);
187,977!
1042

1043
  *pEntry = p;
187,977✔
1044

1045
_end:
187,977✔
1046
  if (code != TSDB_CODE_SUCCESS) {
187,977!
1047
    metaError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1048
  }
1049
  return code;
187,977✔
1050
}
1051

1052
int32_t metaPutRefDbsToCache(void* pVnode, tb_uid_t suid, SArray* pList) {
208,437✔
1053
  int32_t        code = 0;
208,437✔
1054
  int32_t        line = 0;
208,437✔
1055
  SMeta*         pMeta = ((SVnode*)pVnode)->pMeta;
208,437✔
1056
  SHashObj*      pStbRefs = pMeta->pCache->STbRefDbCache.pStbRefs;
208,437✔
1057
  TdThreadMutex* pLock = &pMeta->pCache->STbRefDbCache.lock;
208,437✔
1058

1059
  (void)taosThreadMutexLock(pLock);
208,437✔
1060

1061
  SHashObj*  pEntry = NULL;
208,437✔
1062
  SHashObj** find = taosHashGet(pStbRefs, &suid, sizeof(uint64_t));
208,437✔
1063
  if (find == NULL) {
208,437✔
1064
    code = addRefDbsCacheNewEntry(pStbRefs, suid, &pEntry);
187,977✔
1065
    TSDB_CHECK_CODE(code, line, _return);
187,977!
1066
  } else {  // check if it exists or not
1067
    pEntry = *find;
20,460✔
1068
  }
1069

1070
  for (int32_t i = 0; i < taosArrayGetSize(pList); i++) {
475,896✔
1071
    char* dbName = taosArrayGetP(pList, i);
267,459✔
1072
    void* pItem = taosHashGet(pEntry, dbName, strlen(dbName));
267,459!
1073
    if (pItem == NULL) {
267,459!
1074
      code = taosHashPut(pEntry, dbName, strlen(dbName), NULL, 0);
267,459!
1075
      TSDB_CHECK_CODE(code, line, _return);
267,459!
1076
    }
1077
  }
1078

1079
_return:
208,437✔
1080
  if (code) {
208,437!
1081
    metaError("%s failed at line %d since %s", __func__, line, tstrerror(code));
×
1082
  }
1083
  (void)taosThreadMutexUnlock(pLock);
208,437✔
1084

1085
  return code;
208,437✔
1086
}
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