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

taosdata / TDengine / #4774

01 Oct 2025 04:06AM UTC coverage: 58.357% (-0.3%) from 58.689%
#4774

push

travis-ci

web-flow
Merge pull request #33171 from taosdata/merge/3.3.6tomain

merge: from 3.3.6 to main branch

138553 of 302743 branches covered (45.77%)

Branch coverage included in aggregate %.

15 of 20 new or added lines in 2 files covered. (75.0%)

2558 existing lines in 138 files now uncovered.

209925 of 294403 relevant lines covered (71.31%)

5595496.87 hits per line

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

53.94
/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) {
25,228✔
89
  if (pMeta->pCache) {
25,228!
90
    // close entry cache
91
    for (int32_t iBucket = 0; iBucket < pMeta->pCache->sEntryCache.nBucket; iBucket++) {
25,949,203✔
92
      SMetaCacheEntry* pEntry = pMeta->pCache->sEntryCache.aBucket[iBucket];
25,923,857✔
93
      while (pEntry) {
26,109,802✔
94
        SMetaCacheEntry* tEntry = pEntry->next;
185,827✔
95
        taosMemoryFree(pEntry);
185,827!
96
        pEntry = tEntry;
185,945✔
97
      }
98
    }
99
    taosMemoryFree(pMeta->pCache->sEntryCache.aBucket);
25,346!
100
  }
101
}
25,228✔
102

103
static void statsCacheClose(SMeta* pMeta) {
25,228✔
104
  if (pMeta->pCache) {
25,228!
105
    // close entry cache
106
    for (int32_t iBucket = 0; iBucket < pMeta->pCache->sStbStatsCache.nBucket; iBucket++) {
446,530✔
107
      SMetaStbStatsEntry* pEntry = pMeta->pCache->sStbStatsCache.aBucket[iBucket];
421,326✔
108
      while (pEntry) {
443,950✔
109
        SMetaStbStatsEntry* tEntry = pEntry->next;
22,648✔
110
        taosMemoryFree(pEntry);
22,648!
111
        pEntry = tEntry;
22,624✔
112
      }
113
    }
114
    taosMemoryFree(pMeta->pCache->sStbStatsCache.aBucket);
25,204!
115
  }
116
}
25,228✔
117

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

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

130
int32_t metaCacheOpen(SMeta* pMeta) {
25,179✔
131
  int32_t code = 0;
25,179✔
132
  int32_t lino;
133

134
  pMeta->pCache = (SMetaCache*)taosMemoryCalloc(1, sizeof(SMetaCache));
25,179!
135
  if (pMeta->pCache == NULL) {
25,225!
136
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
137
  }
138

139
  // open entry cache
140
  pMeta->pCache->sEntryCache.nEntry = 0;
25,225✔
141
  pMeta->pCache->sEntryCache.nBucket = META_CACHE_BASE_BUCKET;
25,225✔
142
  pMeta->pCache->sEntryCache.aBucket =
50,453✔
143
      (SMetaCacheEntry**)taosMemoryCalloc(pMeta->pCache->sEntryCache.nBucket, sizeof(SMetaCacheEntry*));
25,225!
144
  if (pMeta->pCache->sEntryCache.aBucket == NULL) {
25,228!
145
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
146
  }
147

148
  // open stats cache
149
  pMeta->pCache->sStbStatsCache.nEntry = 0;
25,228✔
150
  pMeta->pCache->sStbStatsCache.nBucket = META_CACHE_STATS_BUCKET;
25,228✔
151
  pMeta->pCache->sStbStatsCache.aBucket =
50,456✔
152
      (SMetaStbStatsEntry**)taosMemoryCalloc(pMeta->pCache->sStbStatsCache.nBucket, sizeof(SMetaStbStatsEntry*));
25,228!
153
  if (pMeta->pCache->sStbStatsCache.aBucket == NULL) {
25,228!
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);
25,228✔
159
  if (pMeta->pCache->sTagFilterResCache.pUidResCache == NULL) {
25,228!
160
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
161
  }
162

163
  pMeta->pCache->sTagFilterResCache.accTimes = 0;
25,228✔
164
  pMeta->pCache->sTagFilterResCache.pTableEntry =
50,455✔
165
      taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), false, HASH_NO_LOCK);
25,228✔
166
  if (pMeta->pCache->sTagFilterResCache.pTableEntry == NULL) {
25,227!
167
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
168
  }
169

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

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

179
  pMeta->pCache->STbGroupResCache.accTimes = 0;
25,227✔
180
  pMeta->pCache->STbGroupResCache.pTableEntry =
50,455✔
181
      taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), false, HASH_NO_LOCK);
25,227✔
182
  if (pMeta->pCache->STbGroupResCache.pTableEntry == NULL) {
25,228!
183
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
184
  }
185

186
  taosHashSetFreeFp(pMeta->pCache->STbGroupResCache.pTableEntry, freeCacheEntryFp);
25,228✔
187
  (void)taosThreadMutexInit(&pMeta->pCache->STbGroupResCache.lock, NULL);
25,228✔
188

189
  // open filter cache
190
  pMeta->pCache->STbFilterCache.pStb =
50,454✔
191
      taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
25,228✔
192
  if (pMeta->pCache->STbFilterCache.pStb == NULL) {
25,226!
193
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
194
  }
195

196
  pMeta->pCache->STbFilterCache.pStbName =
50,452✔
197
      taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), false, HASH_NO_LOCK);
25,226✔
198
  if (pMeta->pCache->STbFilterCache.pStbName == NULL) {
25,227!
199
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
200
  }
201

202
  // open ref db cache
203
  pMeta->pCache->STbRefDbCache.pStbRefs =
50,454✔
204
      taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
25,227✔
205
  if (pMeta->pCache->STbRefDbCache.pStbRefs == NULL) {
25,227!
206
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
207
  }
208

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

212

213
_exit:
25,227✔
214
  if (code) {
25,227!
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__);
25,227✔
219
  }
220
  return code;
25,228✔
221
}
222

223
void metaCacheClose(SMeta* pMeta) {
25,228✔
224
  if (pMeta->pCache) {
25,228!
225
    entryCacheClose(pMeta);
25,228✔
226
    statsCacheClose(pMeta);
25,228✔
227

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

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

238
    taosHashCleanup(pMeta->pCache->STbFilterCache.pStb);
25,227✔
239
    taosHashCleanup(pMeta->pCache->STbFilterCache.pStbName);
25,228✔
240

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

245
    taosMemoryFree(pMeta->pCache);
25,227!
246
    pMeta->pCache = NULL;
25,227✔
247
  }
248
}
25,227✔
249

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

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

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

265
  // rehash
266
  for (int32_t iBucket = 0; iBucket < pCache->sEntryCache.nBucket; iBucket++) {
96,287✔
267
    SMetaCacheEntry* pEntry = pCache->sEntryCache.aBucket[iBucket];
96,256✔
268

269
    while (pEntry) {
192,512✔
270
      SMetaCacheEntry* pTEntry = pEntry->next;
96,256✔
271

272
      pEntry->next = aBucket[TABS(pEntry->info.uid) % nBucket];
96,256✔
273
      aBucket[TABS(pEntry->info.uid) % nBucket] = pEntry;
96,256✔
274

275
      pEntry = pTEntry;
96,256✔
276
    }
277
  }
278

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

286
int32_t metaCacheUpsert(SMeta* pMeta, SMetaInfo* pInfo) {
241,839✔
287
  int32_t code = 0;
241,839✔
288

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

291
  // search
292
  SMetaCache*       pCache = pMeta->pCache;
241,839✔
293
  int32_t           iBucket = TABS(pInfo->uid) % pCache->sEntryCache.nBucket;
241,839✔
294
  SMetaCacheEntry** ppEntry = &pCache->sEntryCache.aBucket[iBucket];
241,839✔
295
  while (*ppEntry && (*ppEntry)->info.uid != pInfo->uid) {
265,043✔
296
    ppEntry = &(*ppEntry)->next;
23,204✔
297
  }
298

299
  if (*ppEntry) {  // update
241,839✔
300
    if (pInfo->suid != (*ppEntry)->info.suid) {
36,214!
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) {
36,214✔
305
      (*ppEntry)->info.version = pInfo->version;
36,212✔
306
      (*ppEntry)->info.skmVer = pInfo->skmVer;
36,212✔
307
    }
308
  } else {  // insert
309
    if (pCache->sEntryCache.nEntry >= pCache->sEntryCache.nBucket) {
205,625✔
310
      metaRehashCache(pCache, 1);
31✔
311

312
      iBucket = TABS(pInfo->uid) % pCache->sEntryCache.nBucket;
31✔
313
    }
314

315
    SMetaCacheEntry* pEntryNew = (SMetaCacheEntry*)taosMemoryMalloc(sizeof(*pEntryNew));
205,625!
316
    if (pEntryNew == NULL) {
205,661!
317
      code = terrno;
×
318
      goto _exit;
×
319
    }
320

321
    pEntryNew->info = *pInfo;
205,661✔
322
    pEntryNew->next = pCache->sEntryCache.aBucket[iBucket];
205,661✔
323
    pCache->sEntryCache.aBucket[iBucket] = pEntryNew;
205,661✔
324
    pCache->sEntryCache.nEntry++;
205,661✔
325
  }
326

327
_exit:
241,875✔
328
  return code;
241,875✔
329
}
330

331
int32_t metaCacheDrop(SMeta* pMeta, int64_t uid) {
19,901✔
332
  int32_t code = 0;
19,901✔
333

334
  SMetaCache*       pCache = pMeta->pCache;
19,901✔
335
  int32_t           iBucket = TABS(uid) % pCache->sEntryCache.nBucket;
19,901✔
336
  SMetaCacheEntry** ppEntry = &pCache->sEntryCache.aBucket[iBucket];
19,901✔
337
  while (*ppEntry && (*ppEntry)->info.uid != uid) {
19,964✔
338
    ppEntry = &(*ppEntry)->next;
63✔
339
  }
340

341
  SMetaCacheEntry* pEntry = *ppEntry;
19,901✔
342
  if (pEntry) {
19,901✔
343
    *ppEntry = pEntry->next;
19,638✔
344
    taosMemoryFree(pEntry);
19,638!
345
    pCache->sEntryCache.nEntry--;
19,639✔
346
    if (pCache->sEntryCache.nEntry < pCache->sEntryCache.nBucket / 4 &&
19,639✔
347
        pCache->sEntryCache.nBucket > META_CACHE_BASE_BUCKET) {
19,142!
348
      metaRehashCache(pCache, 0);
×
349
    }
350
  } else {
351
    code = TSDB_CODE_NOT_FOUND;
263✔
352
  }
353

354
_exit:
19,907✔
355
  return code;
19,907✔
356
}
357

358
int32_t metaCacheGet(SMeta* pMeta, int64_t uid, SMetaInfo* pInfo) {
7,182,888✔
359
  int32_t code = 0;
7,182,888✔
360

361
  SMetaCache*      pCache = pMeta->pCache;
7,182,888✔
362
  int32_t          iBucket = TABS(uid) % pCache->sEntryCache.nBucket;
7,182,888✔
363
  SMetaCacheEntry* pEntry = pCache->sEntryCache.aBucket[iBucket];
7,182,888✔
364

365
  while (pEntry && pEntry->info.uid != uid) {
7,311,718✔
366
    pEntry = pEntry->next;
128,830✔
367
  }
368

369
  if (pEntry) {
7,182,888✔
370
    if (pInfo) {
6,923,491✔
371
      *pInfo = pEntry->info;
6,923,476✔
372
    }
373
  } else {
374
    code = TSDB_CODE_NOT_FOUND;
259,397✔
375
  }
376

377
  return code;
7,182,888✔
378
}
379

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

384
  if (expand) {
378✔
385
    nBucket = pCache->sStbStatsCache.nBucket * 2;
361✔
386
  } else {
387
    nBucket = pCache->sStbStatsCache.nBucket / 2;
17✔
388
  }
389

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

396
  // rehash
397
  for (int32_t iBucket = 0; iBucket < pCache->sStbStatsCache.nBucket; iBucket++) {
19,306✔
398
    SMetaStbStatsEntry* pEntry = pCache->sStbStatsCache.aBucket[iBucket];
18,928✔
399

400
    while (pEntry) {
37,263✔
401
      SMetaStbStatsEntry* pTEntry = pEntry->next;
18,335✔
402

403
      pEntry->next = aBucket[TABS(pEntry->info.uid) % nBucket];
18,335✔
404
      aBucket[TABS(pEntry->info.uid) % nBucket] = pEntry;
18,335✔
405

406
      pEntry = pTEntry;
18,335✔
407
    }
408
  }
409

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

415
_exit:
378✔
416
  return code;
378✔
417
}
418

419
int32_t metaStatsCacheUpsert(SMeta* pMeta, SMetaStbStats* pInfo) {
187,452✔
420
  int32_t code = 0;
187,452✔
421

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

424
  // search
425
  SMetaCache*          pCache = pMeta->pCache;
187,452✔
426
  int32_t              iBucket = TABS(pInfo->uid) % pCache->sStbStatsCache.nBucket;
187,452✔
427
  SMetaStbStatsEntry** ppEntry = &pCache->sStbStatsCache.aBucket[iBucket];
187,452✔
428
  while (*ppEntry && (*ppEntry)->info.uid != pInfo->uid) {
202,413✔
429
    ppEntry = &(*ppEntry)->next;
14,961✔
430
  }
431

432
  if (*ppEntry) {  // update
187,452✔
433
    (*ppEntry)->info.ctbNum = pInfo->ctbNum;
163,411✔
434
    (*ppEntry)->info.colNum = pInfo->colNum;
163,411✔
435
    (*ppEntry)->info.flags = pInfo->flags;
163,411✔
436
    (*ppEntry)->info.keep = pInfo->keep;
163,411✔
437
  } else {  // insert
438
    if (pCache->sStbStatsCache.nEntry >= pCache->sStbStatsCache.nBucket) {
24,041✔
439
      TAOS_UNUSED(metaRehashStatsCache(pCache, 1));
361✔
440
      iBucket = TABS(pInfo->uid) % pCache->sStbStatsCache.nBucket;
361✔
441
    }
442

443
    SMetaStbStatsEntry* pEntryNew = (SMetaStbStatsEntry*)taosMemoryMalloc(sizeof(*pEntryNew));
24,041!
444
    if (pEntryNew == NULL) {
24,046!
445
      code = terrno;
×
446
      goto _exit;
×
447
    }
448

449
    pEntryNew->info = *pInfo;
24,046✔
450
    pEntryNew->next = pCache->sStbStatsCache.aBucket[iBucket];
24,046✔
451
    pCache->sStbStatsCache.aBucket[iBucket] = pEntryNew;
24,046✔
452
    pCache->sStbStatsCache.nEntry++;
24,046✔
453
  }
454

455
_exit:
187,457✔
456
  return code;
187,457✔
457
}
458

459
int32_t metaStatsCacheDrop(SMeta* pMeta, int64_t uid) {
2,039✔
460
  int32_t code = 0;
2,039✔
461

462
  SMetaCache*          pCache = pMeta->pCache;
2,039✔
463
  int32_t              iBucket = TABS(uid) % pCache->sStbStatsCache.nBucket;
2,039✔
464
  SMetaStbStatsEntry** ppEntry = &pCache->sStbStatsCache.aBucket[iBucket];
2,039✔
465
  while (*ppEntry && (*ppEntry)->info.uid != uid) {
2,332✔
466
    ppEntry = &(*ppEntry)->next;
293✔
467
  }
468

469
  SMetaStbStatsEntry* pEntry = *ppEntry;
2,039✔
470
  if (pEntry) {
2,039✔
471
    *ppEntry = pEntry->next;
1,323✔
472
    taosMemoryFree(pEntry);
1,323!
473
    pCache->sStbStatsCache.nEntry--;
1,323✔
474
    if (pCache->sStbStatsCache.nEntry < pCache->sStbStatsCache.nBucket / 4 &&
1,323✔
475
        pCache->sStbStatsCache.nBucket > META_CACHE_STATS_BUCKET) {
888✔
476
      TAOS_UNUSED(metaRehashStatsCache(pCache, 0));
17✔
477
    }
478
  } else {
479
    code = TSDB_CODE_NOT_FOUND;
716✔
480
  }
481

482
_exit:
2,039✔
483
  return code;
2,039✔
484
}
485

486
int32_t metaStatsCacheGet(SMeta* pMeta, int64_t uid, SMetaStbStats* pInfo) {
917,826✔
487
  int32_t code = TSDB_CODE_SUCCESS;
917,826✔
488

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

493
  while (pEntry && pEntry->info.uid != uid) {
1,105,622✔
494
    pEntry = pEntry->next;
187,796✔
495
  }
496

497
  if (pEntry) {
917,826✔
498
    if (pInfo) {
872,447✔
499
      *pInfo = pEntry->info;
872,442✔
500
    }
501
  } else {
502
    code = TSDB_CODE_NOT_FOUND;
45,379✔
503
  }
504

505
  return code;
917,826✔
506
}
507

508
static FORCE_INLINE void setMD5DigestInKey(uint64_t* pBuf, const char* key, int32_t keyLen) {
509
  memcpy(&pBuf[2], key, keyLen);
361,510✔
510
}
×
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) {
361,510✔
515
  buf[0] = (uint64_t)pHashMap;
361,510✔
516
  buf[1] = suid;
361,510✔
517
  setMD5DigestInKey(buf, key, keyLen);
518
}
361,510✔
519

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

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

530
  *acquireRes = 0;
×
531
  uint64_t key[4];
532
  initCacheKey(key, pTableMap, suid, (const char*)pKey, keyLen);
×
533

534
  (void)taosThreadMutexLock(pLock);
×
535
  pMeta->pCache->sTagFilterResCache.accTimes += 1;
×
536

537
  LRUHandle* pHandle = taosLRUCacheLookup(pCache, key, TAG_FILTER_RES_KEY_LEN);
×
538
  if (pHandle == NULL) {
×
539
    (void)taosThreadMutexUnlock(pLock);
×
540
    return TSDB_CODE_SUCCESS;
×
541
  }
542

543
  // do some book mark work after acquiring the filter result from cache
544
  STagFilterResEntry** pEntry = taosHashGet(pTableMap, &suid, sizeof(uint64_t));
×
545
  if (NULL == pEntry) {
×
546
    metaError("meta/cache: pEntry should not be NULL.");
×
547
    return TSDB_CODE_NOT_FOUND;
×
548
  }
549

550
  *acquireRes = 1;
×
551

552
  const char* p = taosLRUCacheValue(pCache, pHandle);
×
553
  int32_t     size = *(int32_t*)p;
×
554

555
  // set the result into the buffer
556
  if (taosArrayAddBatch(pList1, p + sizeof(int32_t), size) == NULL) {
×
557
    return terrno;
×
558
  }
559

560
  (*pEntry)->hitTimes += 1;
×
561

562
  uint32_t acc = pMeta->pCache->sTagFilterResCache.accTimes;
×
563
  if ((*pEntry)->hitTimes % 5000 == 0 && (*pEntry)->hitTimes > 0) {
×
564
    metaInfo("vgId:%d cache hit:%d, total acc:%d, rate:%.2f", vgId, (*pEntry)->hitTimes, acc,
×
565
             ((double)(*pEntry)->hitTimes) / acc);
566
  }
567

568
  bool ret = taosLRUCacheRelease(pCache, pHandle, false);
×
569

570
  // unlock meta
571
  (void)taosThreadMutexUnlock(pLock);
×
572
  return TSDB_CODE_SUCCESS;
×
573
}
574

575
static void freeUidCachePayload(const void* key, size_t keyLen, void* value, void* ud) {
×
576
  (void)ud;
577
  if (value == NULL) {
×
578
    return;
×
579
  }
580

581
  const uint64_t* p = key;
×
582
  if (keyLen != sizeof(int64_t) * 4) {
×
583
    metaError("key length is invalid, length:%d, expect:%d", (int32_t)keyLen, (int32_t)sizeof(uint64_t) * 2);
×
584
    return;
×
585
  }
586

587
  SHashObj* pHashObj = (SHashObj*)p[0];
×
588

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

591
  if (pEntry != NULL && (*pEntry) != NULL) {
×
592
    int64_t st = taosGetTimestampUs();
×
593
    int32_t code = taosHashRemove((*pEntry)->set, &p[2], sizeof(uint64_t) * 2);
×
594
    if (code == TSDB_CODE_SUCCESS) {
×
595
      double el = (taosGetTimestampUs() - st) / 1000.0;
×
596
      metaInfo("clear items in meta-cache, remain cached item:%d, elapsed time:%.2fms", taosHashGetSize((*pEntry)->set),
×
597
               el);
598
    }
599
  }
600

601
  taosMemoryFree(value);
×
602
}
603

604
static int32_t addNewEntry(SHashObj* pTableEntry, const void* pKey, int32_t keyLen, uint64_t suid) {
×
605
  int32_t             code = TSDB_CODE_SUCCESS;
×
606
  int32_t             lino = 0;
×
607
  STagFilterResEntry* p = taosMemoryMalloc(sizeof(STagFilterResEntry));
×
608
  TSDB_CHECK_NULL(p, code, lino, _end, terrno);
×
609

610
  p->hitTimes = 0;
×
611
  p->set = taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
×
612
  TSDB_CHECK_NULL(p->set, code, lino, _end, terrno);
×
613
  code = taosHashPut(p->set, pKey, keyLen, NULL, 0);
×
614
  TSDB_CHECK_CODE(code, lino, _end);
×
615
  code = taosHashPut(pTableEntry, &suid, sizeof(uint64_t), &p, POINTER_BYTES);
×
616
  TSDB_CHECK_CODE(code, lino, _end);
×
617

618
_end:
×
619
  if (code != TSDB_CODE_SUCCESS) {
×
620
    metaError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
621
    if (p != NULL) {
×
622
      if (p->set != NULL) {
×
623
        taosHashCleanup(p->set);
×
624
      }
625
      taosMemoryFree(p);
×
626
    }
627
  }
628
  return code;
×
629
}
630

631
// check both the payload size and selectivity ratio
632
int32_t metaUidFilterCachePut(void* pVnode, uint64_t suid, const void* pKey, int32_t keyLen, void* pPayload,
×
633
                              int32_t payloadLen, double selectivityRatio) {
634
  int32_t code = 0;
×
635
  SMeta*  pMeta = ((SVnode*)pVnode)->pMeta;
×
636
  int32_t vgId = TD_VID(pMeta->pVnode);
×
637

638
  if (selectivityRatio > tsSelectivityRatio) {
×
639
    metaDebug("vgId:%d, suid:%" PRIu64
×
640
              " failed to add to uid list cache, due to selectivity ratio %.2f less than threshold %.2f",
641
              vgId, suid, selectivityRatio, tsSelectivityRatio);
642
    taosMemoryFree(pPayload);
×
643
    return TSDB_CODE_SUCCESS;
×
644
  }
645

646
  if (payloadLen > tsTagFilterResCacheSize) {
×
647
    metaDebug("vgId:%d, suid:%" PRIu64
×
648
              " failed to add to uid list cache, due to payload length %d greater than threshold %d",
649
              vgId, suid, payloadLen, tsTagFilterResCacheSize);
650
    taosMemoryFree(pPayload);
×
651
    return TSDB_CODE_SUCCESS;
×
652
  }
653

654
  SLRUCache*     pCache = pMeta->pCache->sTagFilterResCache.pUidResCache;
×
655
  SHashObj*      pTableEntry = pMeta->pCache->sTagFilterResCache.pTableEntry;
×
656
  TdThreadMutex* pLock = &pMeta->pCache->sTagFilterResCache.lock;
×
657

658
  uint64_t key[4] = {0};
×
659
  initCacheKey(key, pTableEntry, suid, pKey, keyLen);
×
660

661
  (void)taosThreadMutexLock(pLock);
×
662
  STagFilterResEntry** pEntry = taosHashGet(pTableEntry, &suid, sizeof(uint64_t));
×
663
  if (pEntry == NULL) {
×
664
    code = addNewEntry(pTableEntry, pKey, keyLen, suid);
×
665
    if (code != TSDB_CODE_SUCCESS) {
×
666
      goto _end;
×
667
    }
668
  } else {  // check if it exists or not
669
    code = taosHashPut((*pEntry)->set, pKey, keyLen, NULL, 0);
×
670
    if (code == TSDB_CODE_DUP_KEY) {
×
671
      // we have already found the existed items, no need to added to cache anymore.
672
      (void)taosThreadMutexUnlock(pLock);
×
673
      return TSDB_CODE_SUCCESS;
×
674
    }
675
    if (code != TSDB_CODE_SUCCESS) {
×
676
      goto _end;
×
677
    }
678
  }
679

680
  // add to cache.
681
  (void)taosLRUCacheInsert(pCache, key, TAG_FILTER_RES_KEY_LEN, pPayload, payloadLen, freeUidCachePayload, NULL, NULL,
×
682
                           TAOS_LRU_PRIORITY_LOW, NULL);
683
_end:
×
684
  (void)taosThreadMutexUnlock(pLock);
×
685
  metaDebug("vgId:%d, suid:%" PRIu64 " list cache added into cache, total:%d, tables:%d", vgId, suid,
×
686
            (int32_t)taosLRUCacheGetUsage(pCache), taosHashGetSize(pTableEntry));
687

688
  return code;
×
689
}
690

691
void metaCacheClear(SMeta* pMeta) {
12,611✔
692
  metaWLock(pMeta);
12,611✔
693
  metaCacheClose(pMeta);
12,611✔
694
  metaCacheOpen(pMeta);
12,610✔
695
  metaULock(pMeta);
12,611✔
696
}
12,611✔
697

698
// remove the lru cache that are expired due to the tags value update, or creating, or dropping, of child tables
699
int32_t metaUidCacheClear(SMeta* pMeta, uint64_t suid) {
180,758✔
700
  uint64_t  p[4] = {0};
180,758✔
701
  int32_t   vgId = TD_VID(pMeta->pVnode);
180,758✔
702
  SHashObj* pEntryHashMap = pMeta->pCache->sTagFilterResCache.pTableEntry;
180,758✔
703

704
  uint64_t dummy[2] = {0};
180,758✔
705
  initCacheKey(p, pEntryHashMap, suid, (char*)&dummy[0], 16);
180,758✔
706

707
  TdThreadMutex* pLock = &pMeta->pCache->sTagFilterResCache.lock;
180,792✔
708
  (void)taosThreadMutexLock(pLock);
180,792✔
709

710
  STagFilterResEntry** pEntry = taosHashGet(pEntryHashMap, &suid, sizeof(uint64_t));
180,822✔
711
  if (pEntry == NULL || taosHashGetSize((*pEntry)->set) == 0) {
180,802!
712
    (void)taosThreadMutexUnlock(pLock);
180,802✔
713
    return TSDB_CODE_SUCCESS;
180,818✔
714
  }
715

716
  (*pEntry)->hitTimes = 0;
×
717

718
  char *iter = taosHashIterate((*pEntry)->set, NULL);
×
719
  while (iter != NULL) {
×
720
    setMD5DigestInKey(p, iter, 2 * sizeof(uint64_t));
721
    taosLRUCacheErase(pMeta->pCache->sTagFilterResCache.pUidResCache, p, TAG_FILTER_RES_KEY_LEN);
×
722
    iter = taosHashIterate((*pEntry)->set, iter);
×
723
  }
724
  taosHashClear((*pEntry)->set);
×
725
  (void)taosThreadMutexUnlock(pLock);
×
726

727
  metaDebug("vgId:%d suid:%" PRId64 " cached related tag filter uid list cleared", vgId, suid);
×
728
  return TSDB_CODE_SUCCESS;
×
729
}
730

731
int32_t metaGetCachedTbGroup(void* pVnode, tb_uid_t suid, const uint8_t* pKey, int32_t keyLen, SArray** pList) {
×
732
  SMeta*  pMeta = ((SVnode*)pVnode)->pMeta;
×
733
  int32_t vgId = TD_VID(pMeta->pVnode);
×
734

735
  // generate the composed key for LRU cache
736
  SLRUCache*     pCache = pMeta->pCache->STbGroupResCache.pResCache;
×
737
  SHashObj*      pTableMap = pMeta->pCache->STbGroupResCache.pTableEntry;
×
738
  TdThreadMutex* pLock = &pMeta->pCache->STbGroupResCache.lock;
×
739

740
  *pList = NULL;
×
741
  uint64_t key[4];
742
  initCacheKey(key, pTableMap, suid, (const char*)pKey, keyLen);
×
743

744
  (void)taosThreadMutexLock(pLock);
×
745
  pMeta->pCache->STbGroupResCache.accTimes += 1;
×
746

747
  LRUHandle* pHandle = taosLRUCacheLookup(pCache, key, TAG_FILTER_RES_KEY_LEN);
×
748
  if (pHandle == NULL) {
×
749
    (void)taosThreadMutexUnlock(pLock);
×
750
    return TSDB_CODE_SUCCESS;
×
751
  }
752

753
  STagFilterResEntry** pEntry = taosHashGet(pTableMap, &suid, sizeof(uint64_t));
×
754
  if (NULL == pEntry) {
×
755
    metaDebug("suid %" PRIu64 " not in tb group cache", suid);
×
756
    return TSDB_CODE_NOT_FOUND;
×
757
  }
758

759
  *pList = taosArrayDup(taosLRUCacheValue(pCache, pHandle), NULL);
×
760

761
  (*pEntry)->hitTimes += 1;
×
762

763
  uint32_t acc = pMeta->pCache->STbGroupResCache.accTimes;
×
764
  if ((*pEntry)->hitTimes % 5000 == 0 && (*pEntry)->hitTimes > 0) {
×
765
    metaInfo("vgId:%d tb group cache hit:%d, total acc:%d, rate:%.2f", vgId, (*pEntry)->hitTimes, acc,
×
766
             ((double)(*pEntry)->hitTimes) / acc);
767
  }
768

769
  bool ret = taosLRUCacheRelease(pCache, pHandle, false);
×
770

771
  // unlock meta
772
  (void)taosThreadMutexUnlock(pLock);
×
773
  return TSDB_CODE_SUCCESS;
×
774
}
775

776
static void freeTbGroupCachePayload(const void* key, size_t keyLen, void* value, void* ud) {
×
777
  (void)ud;
778
  if (value == NULL) {
×
779
    return;
×
780
  }
781

782
  const uint64_t* p = key;
×
783
  if (keyLen != sizeof(int64_t) * 4) {
×
784
    metaError("tb group key length is invalid, length:%d, expect:%d", (int32_t)keyLen, (int32_t)sizeof(uint64_t) * 2);
×
785
    return;
×
786
  }
787

788
  SHashObj* pHashObj = (SHashObj*)p[0];
×
789

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

792
  if (pEntry != NULL && (*pEntry) != NULL) {
×
793
    int64_t st = taosGetTimestampUs();
×
794
    int32_t code = taosHashRemove((*pEntry)->set, &p[2], sizeof(uint64_t) * 2);
×
795
    if (code == TSDB_CODE_SUCCESS) {
×
796
      double el = (taosGetTimestampUs() - st) / 1000.0;
×
797
      metaDebug("clear one item in tb group cache, remain cached item:%d, elapsed time:%.2fms",
×
798
                taosHashGetSize((*pEntry)->set), el);
799
    }
800
  }
801

802
  taosArrayDestroy((SArray*)value);
×
803
}
804

805
int32_t metaPutTbGroupToCache(void* pVnode, uint64_t suid, const void* pKey, int32_t keyLen, void* pPayload,
×
806
                              int32_t payloadLen) {
807
  int32_t code = 0;
×
808
  SMeta*  pMeta = ((SVnode*)pVnode)->pMeta;
×
809
  int32_t vgId = TD_VID(pMeta->pVnode);
×
810

811
  if (payloadLen > tsTagFilterResCacheSize) {
×
812
    metaDebug("vgId:%d, suid:%" PRIu64
×
813
              " ignore to add to tb group cache, due to payload length %d greater than threshold %d",
814
              vgId, suid, payloadLen, tsTagFilterResCacheSize);
815
    taosArrayDestroy((SArray*)pPayload);
×
816
    return TSDB_CODE_SUCCESS;
×
817
  }
818

819
  SLRUCache*     pCache = pMeta->pCache->STbGroupResCache.pResCache;
×
820
  SHashObj*      pTableEntry = pMeta->pCache->STbGroupResCache.pTableEntry;
×
821
  TdThreadMutex* pLock = &pMeta->pCache->STbGroupResCache.lock;
×
822

823
  uint64_t key[4] = {0};
×
824
  initCacheKey(key, pTableEntry, suid, pKey, keyLen);
×
825

826
  (void)taosThreadMutexLock(pLock);
×
827
  STagFilterResEntry** pEntry = taosHashGet(pTableEntry, &suid, sizeof(uint64_t));
×
828
  if (pEntry == NULL) {
×
829
    code = addNewEntry(pTableEntry, pKey, keyLen, suid);
×
830
    if (code != TSDB_CODE_SUCCESS) {
×
831
      goto _end;
×
832
    }
833
  } else {  // check if it exists or not
834
    code = taosHashPut((*pEntry)->set, pKey, keyLen, NULL, 0);
×
835
    if (code == TSDB_CODE_DUP_KEY) {
×
836
      // we have already found the existed items, no need to added to cache anymore.
837
      (void)taosThreadMutexUnlock(pLock);
×
838
      return TSDB_CODE_SUCCESS;
×
839
    }
840
    if (code != TSDB_CODE_SUCCESS) {
×
841
      goto _end;
×
842
    }
843
  }
844

845
  // add to cache.
846
  (void)taosLRUCacheInsert(pCache, key, TAG_FILTER_RES_KEY_LEN, pPayload, payloadLen, freeTbGroupCachePayload, NULL, NULL,
×
847
                           TAOS_LRU_PRIORITY_LOW, NULL);
848
_end:
×
849
  (void)taosThreadMutexUnlock(pLock);
×
850
  metaDebug("vgId:%d, suid:%" PRIu64 " tb group added into cache, total:%d, tables:%d", vgId, suid,
×
851
            (int32_t)taosLRUCacheGetUsage(pCache), taosHashGetSize(pTableEntry));
852

853
  return code;
×
854
}
855

856
// remove the lru cache that are expired due to the tags value update, or creating, or dropping, of child tables
857
int32_t metaTbGroupCacheClear(SMeta* pMeta, uint64_t suid) {
180,791✔
858
  uint64_t  p[4] = {0};
180,791✔
859
  int32_t   vgId = TD_VID(pMeta->pVnode);
180,791✔
860
  SHashObj* pEntryHashMap = pMeta->pCache->STbGroupResCache.pTableEntry;
180,791✔
861

862
  uint64_t dummy[2] = {0};
180,791✔
863
  initCacheKey(p, pEntryHashMap, suid, (char*)&dummy[0], 16);
180,791✔
864

865
  TdThreadMutex* pLock = &pMeta->pCache->STbGroupResCache.lock;
180,811✔
866
  (void)taosThreadMutexLock(pLock);
180,811✔
867

868
  STagFilterResEntry** pEntry = taosHashGet(pEntryHashMap, &suid, sizeof(uint64_t));
180,819✔
869
  if (pEntry == NULL || taosHashGetSize((*pEntry)->set) == 0) {
180,808!
870
    (void)taosThreadMutexUnlock(pLock);
180,808✔
871
    return TSDB_CODE_SUCCESS;
180,827✔
872
  }
873

874
  (*pEntry)->hitTimes = 0;
×
875

876
  char *iter = taosHashIterate((*pEntry)->set, NULL);
×
877
  while (iter != NULL) {
×
878
    setMD5DigestInKey(p, iter, 2 * sizeof(uint64_t));
879
    taosLRUCacheErase(pMeta->pCache->STbGroupResCache.pResCache, p, TAG_FILTER_RES_KEY_LEN);
×
880
    iter = taosHashIterate((*pEntry)->set, iter);
×
881
  }
882
  taosHashClear((*pEntry)->set);
×
883
  (void)taosThreadMutexUnlock(pLock);
×
884

885
  metaDebug("vgId:%d suid:%" PRId64 " cached related tb group cleared", vgId, suid);
×
886
  return TSDB_CODE_SUCCESS;
×
887
}
888

889
bool metaTbInFilterCache(SMeta* pMeta, const void* key, int8_t type) {
319,421✔
890
  if (type == 0 && taosHashGet(pMeta->pCache->STbFilterCache.pStb, key, sizeof(tb_uid_t))) {
319,421!
891
    return true;
10✔
892
  }
893

894
  if (type == 1 && taosHashGet(pMeta->pCache->STbFilterCache.pStbName, key, strlen(key))) {
319,411!
895
    return true;
×
896
  }
897

898
  return false;
319,406✔
899
}
900

901
int32_t metaPutTbToFilterCache(SMeta* pMeta, const void* key, int8_t type) {
58✔
902
  if (type == 0) {
58✔
903
    return taosHashPut(pMeta->pCache->STbFilterCache.pStb, key, sizeof(tb_uid_t), NULL, 0);
20✔
904
  }
905

906
  if (type == 1) {
38!
907
    return taosHashPut(pMeta->pCache->STbFilterCache.pStbName, key, strlen(key), NULL, 0);
38✔
908
  }
909

910
  return 0;
×
911
}
912

913
int32_t metaSizeOfTbFilterCache(SMeta* pMeta, int8_t type) {
18✔
914
  if (type == 0) {
18!
915
    return taosHashGetSize(pMeta->pCache->STbFilterCache.pStb);
18✔
916
  }
917
  return 0;
×
918
}
919

920
int32_t metaInitTbFilterCache(SMeta* pMeta) {
12,617✔
921
#ifdef TD_ENTERPRISE
922
  int32_t      tbNum = 0;
12,617✔
923
  const char** pTbArr = NULL;
12,617✔
924
  const char*  dbName = NULL;
12,617✔
925

926
  if (!(dbName = strchr(pMeta->pVnode->config.dbname, '.'))) return 0;
12,617✔
927
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
12,613✔
928
    tbNum = tkLogStbNum;
2✔
929
    pTbArr = (const char**)&tkLogStb;
2✔
930
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
12,611!
931
    tbNum = tkAuditStbNum;
×
932
    pTbArr = (const char**)&tkAuditStb;
×
933
  }
934
  if (tbNum && pTbArr) {
12,613!
935
    for (int32_t i = 0; i < tbNum; ++i) {
40✔
936
      TAOS_CHECK_RETURN(metaPutTbToFilterCache(pMeta, pTbArr[i], 1));
38!
937
    }
938
  }
939
#else
940
#endif
941
  return 0;
12,613✔
942
}
943

944
int64_t metaGetStbKeep(SMeta* pMeta, int64_t uid) {
220✔
945
  SMetaStbStats stats = {0};
220✔
946

947
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
220✔
948
    return stats.keep;
172✔
949
  }
950

951
  SMetaEntry* pEntry = NULL;
48✔
952
  if (metaFetchEntryByUid(pMeta, uid, &pEntry) == TSDB_CODE_SUCCESS) {
48!
953
    int64_t keep = -1;
48✔
954
    if (pEntry->type == TSDB_SUPER_TABLE) {
48!
955
      keep = pEntry->stbEntry.keep;
48✔
956
    }
957
    metaFetchEntryFree(&pEntry);
48✔
958
    return keep;
48✔
959
  }
960
  
961
  return -1;
×
962
}
963

964
int32_t metaRefDbsCacheClear(SMeta* pMeta, uint64_t suid) {
520✔
965
  int32_t        code = TSDB_CODE_SUCCESS;
520✔
966
  int32_t        vgId = TD_VID(pMeta->pVnode);
520✔
967
  SHashObj*      pEntryHashMap = pMeta->pCache->STbRefDbCache.pStbRefs;
520✔
968
  TdThreadMutex* pLock = &pMeta->pCache->STbRefDbCache.lock;
520✔
969

970
  (void)taosThreadMutexLock(pLock);
520✔
971

972
  SHashObj** pEntry = taosHashGet(pEntryHashMap, &suid, sizeof(uint64_t));
520✔
973
  if (pEntry == NULL) {
520✔
974
    goto _return;
516✔
975
  }
976

977
  taosHashRemove(pEntryHashMap, &suid, sizeof(uint64_t));
4✔
978

979
  metaDebug("vgId:%d suid:%" PRId64 " cached virtual stable ref db cleared", vgId, suid);
4✔
980

981
_return:
2✔
982
  (void)taosThreadMutexUnlock(pLock);
520✔
983
  return code;
520✔
984
}
985

986
int32_t metaGetCachedRefDbs(void* pVnode, tb_uid_t suid, SArray* pList) {
1,814✔
987
  int32_t        code = TSDB_CODE_SUCCESS;
1,814✔
988
  int32_t        line = 0;
1,814✔
989
  SMeta*         pMeta = ((SVnode*)pVnode)->pMeta;
1,814✔
990
  SHashObj*      pTableMap = pMeta->pCache->STbRefDbCache.pStbRefs;
1,814✔
991
  TdThreadMutex* pLock = &pMeta->pCache->STbRefDbCache.lock;
1,814✔
992

993
  (void)taosThreadMutexLock(pLock);
1,814✔
994

995
  SHashObj** pEntry = taosHashGet(pTableMap, &suid, sizeof(uint64_t));
1,816✔
996
  if (pEntry) {
1,815✔
997
    void *iter = taosHashIterate(*pEntry, NULL);
1,787✔
998
    while (iter != NULL) {
5,598✔
999
      size_t   dbNameLen = 0;
3,810✔
1000
      char*    name = NULL;
3,810✔
1001
      char*    dbName = NULL;
3,810✔
1002
      name = taosHashGetKey(iter, &dbNameLen);
3,810✔
1003
      TSDB_CHECK_NULL(name, code, line, _return, terrno);
3,810!
1004
      dbName = taosMemoryMalloc(dbNameLen + 1);
3,810!
1005
      TSDB_CHECK_NULL(dbName, code, line, _return, terrno);
3,810!
1006
      tstrncpy(dbName, name, dbNameLen + 1);
3,810✔
1007
      TSDB_CHECK_NULL(taosArrayPush(pList, &dbName), code, line, _return, terrno);
3,808!
1008
      iter = taosHashIterate(*pEntry, iter);
3,808✔
1009
    }
1010
  }
1011

1012
_return:
1,816✔
1013
  if (code) {
1,816!
1014
    metaError("%s failed at line %d since %s", __func__, line, tstrerror(code));
×
1015
  }
1016
  (void)taosThreadMutexUnlock(pLock);
1,816✔
1017
  return code;
1,816✔
1018
}
1019

1020
static int32_t addRefDbsCacheNewEntry(SHashObj* pRefDbs, uint64_t suid, SHashObj **pEntry) {
28✔
1021
  int32_t      code = TSDB_CODE_SUCCESS;
28✔
1022
  int32_t      lino = 0;
28✔
1023
  SHashObj*    p = NULL;
28✔
1024

1025
  p = taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
28✔
1026
  TSDB_CHECK_NULL(p, code, lino, _end, terrno);
28!
1027

1028
  code = taosHashPut(pRefDbs, &suid, sizeof(uint64_t), &p, POINTER_BYTES);
28✔
1029
  TSDB_CHECK_CODE(code, lino, _end);
28!
1030

1031
  *pEntry = p;
28✔
1032

1033
_end:
28✔
1034
  if (code != TSDB_CODE_SUCCESS) {
28!
1035
    metaError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1036
  }
1037
  return code;
28✔
1038
}
1039

1040
int32_t metaPutRefDbsToCache(void* pVnode, tb_uid_t suid, SArray* pList) {
28✔
1041
  int32_t        code = 0;
28✔
1042
  int32_t        line = 0;
28✔
1043
  SMeta*         pMeta = ((SVnode*)pVnode)->pMeta;
28✔
1044
  SHashObj*      pStbRefs = pMeta->pCache->STbRefDbCache.pStbRefs;
28✔
1045
  TdThreadMutex* pLock = &pMeta->pCache->STbRefDbCache.lock;
28✔
1046

1047
  (void)taosThreadMutexLock(pLock);
28✔
1048

1049
  SHashObj*  pEntry = NULL;
28✔
1050
  SHashObj** find = taosHashGet(pStbRefs, &suid, sizeof(uint64_t));
28✔
1051
  if (find == NULL) {
28!
1052
    code = addRefDbsCacheNewEntry(pStbRefs, suid, &pEntry);
28✔
1053
    TSDB_CHECK_CODE(code, line, _return);
28!
1054
  } else {  // check if it exists or not
UNCOV
1055
    pEntry = *find;
×
1056
  }
1057

1058
  for (int32_t i = 0; i < taosArrayGetSize(pList); i++) {
68✔
1059
    char* dbName = taosArrayGetP(pList, i);
40✔
1060
    void* pItem = taosHashGet(pEntry, dbName, strlen(dbName));
40✔
1061
    if (pItem == NULL) {
40!
1062
      code = taosHashPut(pEntry, dbName, strlen(dbName), NULL, 0);
40✔
1063
      TSDB_CHECK_CODE(code, line, _return);
40!
1064
    }
1065
  }
1066

1067
_return:
28✔
1068
  if (code) {
28!
1069
    metaError("%s failed at line %d since %s", __func__, line, tstrerror(code));
×
1070
  }
1071
  (void)taosThreadMutexUnlock(pLock);
28✔
1072

1073
  return code;
28✔
1074
}
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