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

taosdata / TDengine / #3817

01 Apr 2025 07:46AM UTC coverage: 34.08% (+0.04%) from 34.045%
#3817

push

travis-ci

happyguoxy
test:alter gcda dir

148556 of 599532 branches covered (24.78%)

Branch coverage included in aggregate %.

222572 of 489464 relevant lines covered (45.47%)

759283.34 hits per line

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

32.0
/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) {
276✔
89
  if (pMeta->pCache) {
276!
90
    // close entry cache
91
    for (int32_t iBucket = 0; iBucket < pMeta->pCache->sEntryCache.nBucket; iBucket++) {
282,576✔
92
      SMetaCacheEntry* pEntry = pMeta->pCache->sEntryCache.aBucket[iBucket];
282,300✔
93
      while (pEntry) {
285,730✔
94
        SMetaCacheEntry* tEntry = pEntry->next;
3,430✔
95
        taosMemoryFree(pEntry);
3,430!
96
        pEntry = tEntry;
3,430✔
97
      }
98
    }
99
    taosMemoryFree(pMeta->pCache->sEntryCache.aBucket);
276!
100
  }
101
}
277✔
102

103
static void statsCacheClose(SMeta* pMeta) {
276✔
104
  if (pMeta->pCache) {
276!
105
    // close entry cache
106
    for (int32_t iBucket = 0; iBucket < pMeta->pCache->sStbStatsCache.nBucket; iBucket++) {
4,708✔
107
      SMetaStbStatsEntry* pEntry = pMeta->pCache->sStbStatsCache.aBucket[iBucket];
4,431✔
108
      while (pEntry) {
4,741✔
109
        SMetaStbStatsEntry* tEntry = pEntry->next;
310✔
110
        taosMemoryFree(pEntry);
310!
111
        pEntry = tEntry;
310✔
112
      }
113
    }
114
    taosMemoryFree(pMeta->pCache->sStbStatsCache.aBucket);
277!
115
  }
116
}
276✔
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) {
×
125
  SHashObj** p = param;
×
126
  taosHashCleanup(*p);
×
127
  *p = NULL;
×
128
}
×
129

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

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

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

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

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

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

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

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

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

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

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

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

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

212

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

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

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

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

238
    taosHashCleanup(pMeta->pCache->STbFilterCache.pStb);
277✔
239
    taosHashCleanup(pMeta->pCache->STbFilterCache.pStbName);
277✔
240

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

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

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

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

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

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

269
    while (pEntry) {
×
270
      SMetaCacheEntry* pTEntry = pEntry->next;
×
271

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

275
      pEntry = pTEntry;
×
276
    }
277
  }
278

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

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

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

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

299
  if (*ppEntry) {  // update
3,935✔
300
    if (pInfo->suid != (*ppEntry)->info.suid) {
133!
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) {
133!
305
      (*ppEntry)->info.version = pInfo->version;
133✔
306
      (*ppEntry)->info.skmVer = pInfo->skmVer;
133✔
307
    }
308
  } else {  // insert
309
    if (pCache->sEntryCache.nEntry >= pCache->sEntryCache.nBucket) {
3,802!
310
      metaRehashCache(pCache, 1);
×
311

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

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

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

327
_exit:
3,938✔
328
  return code;
3,938✔
329
}
330

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

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

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

354
_exit:
377✔
355
  return code;
377✔
356
}
357

358
int32_t metaCacheGet(SMeta* pMeta, int64_t uid, SMetaInfo* pInfo) {
1,437,485✔
359
  int32_t code = 0;
1,437,485✔
360

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

365
  while (pEntry && pEntry->info.uid != uid) {
1,724,513✔
366
    pEntry = pEntry->next;
287,028✔
367
  }
368

369
  if (pEntry) {
1,437,485✔
370
    if (pInfo) {
1,432,833✔
371
      *pInfo = pEntry->info;
1,432,626✔
372
    }
373
  } else {
374
    code = TSDB_CODE_NOT_FOUND;
4,652✔
375
  }
376

377
  return code;
1,437,485✔
378
}
379

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

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

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

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

400
    while (pEntry) {
×
401
      SMetaStbStatsEntry* pTEntry = pEntry->next;
×
402

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

406
      pEntry = pTEntry;
×
407
    }
408
  }
409

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

415
_exit:
×
416
  return code;
×
417
}
418

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

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

424
  // search
425
  SMetaCache*          pCache = pMeta->pCache;
3,984✔
426
  int32_t              iBucket = TABS(pInfo->uid) % pCache->sStbStatsCache.nBucket;
3,984✔
427
  SMetaStbStatsEntry** ppEntry = &pCache->sStbStatsCache.aBucket[iBucket];
3,984✔
428
  while (*ppEntry && (*ppEntry)->info.uid != pInfo->uid) {
4,110✔
429
    ppEntry = &(*ppEntry)->next;
126✔
430
  }
431

432
  if (*ppEntry) {  // update
3,984✔
433
    (*ppEntry)->info.ctbNum = pInfo->ctbNum;
3,670✔
434
    (*ppEntry)->info.colNum = pInfo->colNum;
3,670✔
435
    (*ppEntry)->info.keep = pInfo->keep;
3,670✔
436
  } else {  // insert
437
    if (pCache->sStbStatsCache.nEntry >= pCache->sStbStatsCache.nBucket) {
314!
438
      TAOS_UNUSED(metaRehashStatsCache(pCache, 1));
×
439
      iBucket = TABS(pInfo->uid) % pCache->sStbStatsCache.nBucket;
×
440
    }
441

442
    SMetaStbStatsEntry* pEntryNew = (SMetaStbStatsEntry*)taosMemoryMalloc(sizeof(*pEntryNew));
314!
443
    if (pEntryNew == NULL) {
315!
444
      code = terrno;
×
445
      goto _exit;
×
446
    }
447

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

454
_exit:
3,985✔
455
  return code;
3,985✔
456
}
457

458
int32_t metaStatsCacheDrop(SMeta* pMeta, int64_t uid) {
10✔
459
  int32_t code = 0;
10✔
460

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

468
  SMetaStbStatsEntry* pEntry = *ppEntry;
10✔
469
  if (pEntry) {
10✔
470
    *ppEntry = pEntry->next;
5✔
471
    taosMemoryFree(pEntry);
5!
472
    pCache->sStbStatsCache.nEntry--;
5✔
473
    if (pCache->sStbStatsCache.nEntry < pCache->sStbStatsCache.nBucket / 4 &&
5!
474
        pCache->sStbStatsCache.nBucket > META_CACHE_STATS_BUCKET) {
5!
475
      TAOS_UNUSED(metaRehashStatsCache(pCache, 0));
×
476
    }
477
  } else {
478
    code = TSDB_CODE_NOT_FOUND;
5✔
479
  }
480

481
_exit:
10✔
482
  return code;
10✔
483
}
484

485
int32_t metaStatsCacheGet(SMeta* pMeta, int64_t uid, SMetaStbStats* pInfo) {
9,810✔
486
  int32_t code = TSDB_CODE_SUCCESS;
9,810✔
487

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

492
  while (pEntry && pEntry->info.uid != uid) {
10,136✔
493
    pEntry = pEntry->next;
326✔
494
  }
495

496
  if (pEntry) {
9,810✔
497
    if (pInfo) {
9,312!
498
      *pInfo = pEntry->info;
9,312✔
499
    }
500
  } else {
501
    code = TSDB_CODE_NOT_FOUND;
498✔
502
  }
503

504
  return code;
9,810✔
505
}
506

507
static FORCE_INLINE void setMD5DigestInKey(uint64_t* pBuf, const char* key, int32_t keyLen) {
508
  memcpy(&pBuf[2], key, keyLen);
7,486✔
509
}
×
510

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

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

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

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

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

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

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

549
  *acquireRes = 1;
×
550

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

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

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

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

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

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

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

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

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

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

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

600
  taosMemoryFree(value);
×
601
}
602

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

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

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

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

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

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

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

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

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

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

687
  return code;
×
688
}
689

690
// remove the lru cache that are expired due to the tags value update, or creating, or dropping, of child tables
691
int32_t metaUidCacheClear(SMeta* pMeta, uint64_t suid) {
3,743✔
692
  uint64_t  p[4] = {0};
3,743✔
693
  int32_t   vgId = TD_VID(pMeta->pVnode);
3,743✔
694
  SHashObj* pEntryHashMap = pMeta->pCache->sTagFilterResCache.pTableEntry;
3,743✔
695

696
  uint64_t dummy[2] = {0};
3,743✔
697
  initCacheKey(p, pEntryHashMap, suid, (char*)&dummy[0], 16);
3,743✔
698

699
  TdThreadMutex* pLock = &pMeta->pCache->sTagFilterResCache.lock;
3,745✔
700
  (void)taosThreadMutexLock(pLock);
3,745✔
701

702
  STagFilterResEntry** pEntry = taosHashGet(pEntryHashMap, &suid, sizeof(uint64_t));
3,747✔
703
  if (pEntry == NULL || taosHashGetSize((*pEntry)->set) == 0) {
3,744!
704
    (void)taosThreadMutexUnlock(pLock);
3,744✔
705
    return TSDB_CODE_SUCCESS;
3,747✔
706
  }
707

708
  (*pEntry)->hitTimes = 0;
×
709

710
  char *iter = taosHashIterate((*pEntry)->set, NULL);
×
711
  while (iter != NULL) {
×
712
    setMD5DigestInKey(p, iter, 2 * sizeof(uint64_t));
713
    taosLRUCacheErase(pMeta->pCache->sTagFilterResCache.pUidResCache, p, TAG_FILTER_RES_KEY_LEN);
×
714
    iter = taosHashIterate((*pEntry)->set, iter);
×
715
  }
716
  taosHashClear((*pEntry)->set);
×
717
  (void)taosThreadMutexUnlock(pLock);
×
718

719
  metaDebug("vgId:%d suid:%" PRId64 " cached related tag filter uid list cleared", vgId, suid);
×
720
  return TSDB_CODE_SUCCESS;
×
721
}
722

723
int32_t metaGetCachedTbGroup(void* pVnode, tb_uid_t suid, const uint8_t* pKey, int32_t keyLen, SArray** pList) {
×
724
  SMeta*  pMeta = ((SVnode*)pVnode)->pMeta;
×
725
  int32_t vgId = TD_VID(pMeta->pVnode);
×
726

727
  // generate the composed key for LRU cache
728
  SLRUCache*     pCache = pMeta->pCache->STbGroupResCache.pResCache;
×
729
  SHashObj*      pTableMap = pMeta->pCache->STbGroupResCache.pTableEntry;
×
730
  TdThreadMutex* pLock = &pMeta->pCache->STbGroupResCache.lock;
×
731

732
  *pList = NULL;
×
733
  uint64_t key[4];
734
  initCacheKey(key, pTableMap, suid, (const char*)pKey, keyLen);
×
735

736
  (void)taosThreadMutexLock(pLock);
×
737
  pMeta->pCache->STbGroupResCache.accTimes += 1;
×
738

739
  LRUHandle* pHandle = taosLRUCacheLookup(pCache, key, TAG_FILTER_RES_KEY_LEN);
×
740
  if (pHandle == NULL) {
×
741
    (void)taosThreadMutexUnlock(pLock);
×
742
    return TSDB_CODE_SUCCESS;
×
743
  }
744

745
  STagFilterResEntry** pEntry = taosHashGet(pTableMap, &suid, sizeof(uint64_t));
×
746
  if (NULL == pEntry) {
×
747
    metaDebug("suid %" PRIu64 " not in tb group cache", suid);
×
748
    return TSDB_CODE_NOT_FOUND;
×
749
  }
750

751
  *pList = taosArrayDup(taosLRUCacheValue(pCache, pHandle), NULL);
×
752

753
  (*pEntry)->hitTimes += 1;
×
754

755
  uint32_t acc = pMeta->pCache->STbGroupResCache.accTimes;
×
756
  if ((*pEntry)->hitTimes % 5000 == 0 && (*pEntry)->hitTimes > 0) {
×
757
    metaInfo("vgId:%d tb group cache hit:%d, total acc:%d, rate:%.2f", vgId, (*pEntry)->hitTimes, acc,
×
758
             ((double)(*pEntry)->hitTimes) / acc);
759
  }
760

761
  bool ret = taosLRUCacheRelease(pCache, pHandle, false);
×
762

763
  // unlock meta
764
  (void)taosThreadMutexUnlock(pLock);
×
765
  return TSDB_CODE_SUCCESS;
×
766
}
767

768
static void freeTbGroupCachePayload(const void* key, size_t keyLen, void* value, void* ud) {
×
769
  (void)ud;
770
  if (value == NULL) {
×
771
    return;
×
772
  }
773

774
  const uint64_t* p = key;
×
775
  if (keyLen != sizeof(int64_t) * 4) {
×
776
    metaError("tb group key length is invalid, length:%d, expect:%d", (int32_t)keyLen, (int32_t)sizeof(uint64_t) * 2);
×
777
    return;
×
778
  }
779

780
  SHashObj* pHashObj = (SHashObj*)p[0];
×
781

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

784
  if (pEntry != NULL && (*pEntry) != NULL) {
×
785
    int64_t st = taosGetTimestampUs();
×
786
    int32_t code = taosHashRemove((*pEntry)->set, &p[2], sizeof(uint64_t) * 2);
×
787
    if (code == TSDB_CODE_SUCCESS) {
×
788
      double el = (taosGetTimestampUs() - st) / 1000.0;
×
789
      metaDebug("clear one item in tb group cache, remain cached item:%d, elapsed time:%.2fms",
×
790
                taosHashGetSize((*pEntry)->set), el);
791
    }
792
  }
793

794
  taosArrayDestroy((SArray*)value);
×
795
}
796

797
int32_t metaPutTbGroupToCache(void* pVnode, uint64_t suid, const void* pKey, int32_t keyLen, void* pPayload,
×
798
                              int32_t payloadLen) {
799
  int32_t code = 0;
×
800
  SMeta*  pMeta = ((SVnode*)pVnode)->pMeta;
×
801
  int32_t vgId = TD_VID(pMeta->pVnode);
×
802

803
  if (payloadLen > tsTagFilterResCacheSize) {
×
804
    metaDebug("vgId:%d, suid:%" PRIu64
×
805
              " ignore to add to tb group cache, due to payload length %d greater than threshold %d",
806
              vgId, suid, payloadLen, tsTagFilterResCacheSize);
807
    taosArrayDestroy((SArray*)pPayload);
×
808
    return TSDB_CODE_SUCCESS;
×
809
  }
810

811
  SLRUCache*     pCache = pMeta->pCache->STbGroupResCache.pResCache;
×
812
  SHashObj*      pTableEntry = pMeta->pCache->STbGroupResCache.pTableEntry;
×
813
  TdThreadMutex* pLock = &pMeta->pCache->STbGroupResCache.lock;
×
814

815
  uint64_t key[4] = {0};
×
816
  initCacheKey(key, pTableEntry, suid, pKey, keyLen);
×
817

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

837
  // add to cache.
838
  (void)taosLRUCacheInsert(pCache, key, TAG_FILTER_RES_KEY_LEN, pPayload, payloadLen, freeTbGroupCachePayload, NULL, NULL,
×
839
                           TAOS_LRU_PRIORITY_LOW, NULL);
840
_end:
×
841
  (void)taosThreadMutexUnlock(pLock);
×
842
  metaDebug("vgId:%d, suid:%" PRIu64 " tb group added into cache, total:%d, tables:%d", vgId, suid,
×
843
            (int32_t)taosLRUCacheGetUsage(pCache), taosHashGetSize(pTableEntry));
844

845
  return code;
×
846
}
847

848
// remove the lru cache that are expired due to the tags value update, or creating, or dropping, of child tables
849
int32_t metaTbGroupCacheClear(SMeta* pMeta, uint64_t suid) {
3,744✔
850
  uint64_t  p[4] = {0};
3,744✔
851
  int32_t   vgId = TD_VID(pMeta->pVnode);
3,744✔
852
  SHashObj* pEntryHashMap = pMeta->pCache->STbGroupResCache.pTableEntry;
3,744✔
853

854
  uint64_t dummy[2] = {0};
3,744✔
855
  initCacheKey(p, pEntryHashMap, suid, (char*)&dummy[0], 16);
3,744✔
856

857
  TdThreadMutex* pLock = &pMeta->pCache->STbGroupResCache.lock;
3,745✔
858
  (void)taosThreadMutexLock(pLock);
3,745✔
859

860
  STagFilterResEntry** pEntry = taosHashGet(pEntryHashMap, &suid, sizeof(uint64_t));
3,746✔
861
  if (pEntry == NULL || taosHashGetSize((*pEntry)->set) == 0) {
3,747!
862
    (void)taosThreadMutexUnlock(pLock);
3,747✔
863
    return TSDB_CODE_SUCCESS;
3,747✔
864
  }
865

866
  (*pEntry)->hitTimes = 0;
×
867

868
  char *iter = taosHashIterate((*pEntry)->set, NULL);
×
869
  while (iter != NULL) {
×
870
    setMD5DigestInKey(p, iter, 2 * sizeof(uint64_t));
871
    taosLRUCacheErase(pMeta->pCache->STbGroupResCache.pResCache, p, TAG_FILTER_RES_KEY_LEN);
×
872
    iter = taosHashIterate((*pEntry)->set, iter);
×
873
  }
874
  taosHashClear((*pEntry)->set);
×
875
  (void)taosThreadMutexUnlock(pLock);
×
876

877
  metaDebug("vgId:%d suid:%" PRId64 " cached related tb group cleared", vgId, suid);
×
878
  return TSDB_CODE_SUCCESS;
×
879
}
880

881
bool metaTbInFilterCache(SMeta* pMeta, const void* key, int8_t type) {
7,161✔
882
  if (type == 0 && taosHashGet(pMeta->pCache->STbFilterCache.pStb, key, sizeof(tb_uid_t))) {
7,161!
883
    return true;
×
884
  }
885

886
  if (type == 1 && taosHashGet(pMeta->pCache->STbFilterCache.pStbName, key, strlen(key))) {
7,161!
887
    return true;
×
888
  }
889

890
  return false;
7,160✔
891
}
892

893
int32_t metaPutTbToFilterCache(SMeta* pMeta, const void* key, int8_t type) {
×
894
  if (type == 0) {
×
895
    return taosHashPut(pMeta->pCache->STbFilterCache.pStb, key, sizeof(tb_uid_t), NULL, 0);
×
896
  }
897

898
  if (type == 1) {
×
899
    return taosHashPut(pMeta->pCache->STbFilterCache.pStbName, key, strlen(key), NULL, 0);
×
900
  }
901

902
  return 0;
×
903
}
904

905
int32_t metaSizeOfTbFilterCache(SMeta* pMeta, int8_t type) {
×
906
  if (type == 0) {
×
907
    return taosHashGetSize(pMeta->pCache->STbFilterCache.pStb);
×
908
  }
909
  return 0;
×
910
}
911

912
int32_t metaInitTbFilterCache(SMeta* pMeta) {
276✔
913
#ifdef TD_ENTERPRISE
914
  int32_t      tbNum = 0;
276✔
915
  const char** pTbArr = NULL;
276✔
916
  const char*  dbName = NULL;
276✔
917

918
  if (!(dbName = strchr(pMeta->pVnode->config.dbname, '.'))) return 0;
276!
919
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
276!
920
    tbNum = tkLogStbNum;
×
921
    pTbArr = (const char**)&tkLogStb;
×
922
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
276!
923
    tbNum = tkAuditStbNum;
×
924
    pTbArr = (const char**)&tkAuditStb;
×
925
  }
926
  if (tbNum && pTbArr) {
276!
927
    for (int32_t i = 0; i < tbNum; ++i) {
×
928
      TAOS_CHECK_RETURN(metaPutTbToFilterCache(pMeta, pTbArr[i], 1));
×
929
    }
930
  }
931
#else
932
#endif
933
  return 0;
276✔
934
}
935

936
int64_t metaGetStbKeep(SMeta* pMeta, int64_t uid) {
×
937
  SMetaStbStats stats = {0};
×
938

939
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
×
940
    return stats.keep;
×
941
  }
942

943
  SMetaEntry* pEntry = NULL;
×
944
  if (metaFetchEntryByUid(pMeta, uid, &pEntry) == TSDB_CODE_SUCCESS) {
×
945
    int64_t keep = -1;
×
946
    if (pEntry->type == TSDB_SUPER_TABLE) {
×
947
      keep = pEntry->stbEntry.keep;
×
948
    }
949
    metaFetchEntryFree(&pEntry);
×
950
    return keep;
×
951
  }
952
  
953
  return -1;
×
954
}
955

956
int32_t metaRefDbsCacheClear(SMeta* pMeta, uint64_t suid) {
×
957
  int32_t        code = TSDB_CODE_SUCCESS;
×
958
  int32_t        vgId = TD_VID(pMeta->pVnode);
×
959
  SHashObj*      pEntryHashMap = pMeta->pCache->STbRefDbCache.pStbRefs;
×
960
  TdThreadMutex* pLock = &pMeta->pCache->STbRefDbCache.lock;
×
961

962
  (void)taosThreadMutexLock(pLock);
×
963

964
  SHashObj** pEntry = taosHashGet(pEntryHashMap, &suid, sizeof(uint64_t));
×
965
  if (pEntry == NULL) {
×
966
    goto _return;
×
967
  }
968

969
  taosHashRemove(pEntryHashMap, &suid, sizeof(uint64_t));
×
970

971
  metaDebug("vgId:%d suid:%" PRId64 " cached virtual stable ref db cleared", vgId, suid);
×
972

973
_return:
×
974
  (void)taosThreadMutexUnlock(pLock);
×
975
  return code;
×
976
}
977

978
int32_t metaGetCachedRefDbs(void* pVnode, tb_uid_t suid, SArray* pList) {
×
979
  int32_t        code = TSDB_CODE_SUCCESS;
×
980
  int32_t        line = 0;
×
981
  SMeta*         pMeta = ((SVnode*)pVnode)->pMeta;
×
982
  SHashObj*      pTableMap = pMeta->pCache->STbRefDbCache.pStbRefs;
×
983
  TdThreadMutex* pLock = &pMeta->pCache->STbRefDbCache.lock;
×
984

985
  (void)taosThreadMutexLock(pLock);
×
986

987
  SHashObj** pEntry = taosHashGet(pTableMap, &suid, sizeof(uint64_t));
×
988
  TSDB_CHECK_NULL(pEntry, code, line, _return, terrno);
×
989

990
  void *iter = taosHashIterate(*pEntry, NULL);
×
991
  while (iter != NULL) {
×
992
    size_t   dbNameLen = 0;
×
993
    char*    name = NULL;
×
994
    char*    dbName = NULL;
×
995
    name = taosHashGetKey(iter, &dbNameLen);
×
996
    TSDB_CHECK_NULL(name, code, line, _return, terrno);
×
997
    dbName = taosMemoryMalloc(dbNameLen + 1);
×
998
    TSDB_CHECK_NULL(dbName, code, line, _return, terrno);
×
999
    tstrncpy(dbName, name, dbNameLen + 1);
×
1000
    TSDB_CHECK_NULL(taosArrayPush(pList, &dbName), code, line, _return, terrno);
×
1001
    iter = taosHashIterate(*pEntry, iter);
×
1002
  }
1003

1004
_return:
×
1005
  if (code) {
×
1006
    metaError("%s failed at line %d since %s", __func__, line, tstrerror(code));
×
1007
  }
1008
  (void)taosThreadMutexUnlock(pLock);
×
1009
  return code;
×
1010
}
1011

1012
static int32_t addRefDbsCacheNewEntry(SHashObj* pRefDbs, uint64_t suid, SHashObj **pEntry) {
×
1013
  int32_t      code = TSDB_CODE_SUCCESS;
×
1014
  int32_t      lino = 0;
×
1015
  SHashObj*    p = NULL;
×
1016

1017
  p = taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
×
1018
  TSDB_CHECK_NULL(p, code, lino, _end, terrno);
×
1019

1020
  code = taosHashPut(pRefDbs, &suid, sizeof(uint64_t), &p, POINTER_BYTES);
×
1021
  TSDB_CHECK_CODE(code, lino, _end);
×
1022

1023
  *pEntry = p;
×
1024

1025
_end:
×
1026
  if (code != TSDB_CODE_SUCCESS) {
×
1027
    metaError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1028
  }
1029
  return code;
×
1030
}
1031

1032
int32_t metaPutRefDbsToCache(void* pVnode, tb_uid_t suid, SArray* pList) {
×
1033
  int32_t        code = 0;
×
1034
  int32_t        line = 0;
×
1035
  SMeta*         pMeta = ((SVnode*)pVnode)->pMeta;
×
1036
  SHashObj*      pStbRefs = pMeta->pCache->STbRefDbCache.pStbRefs;
×
1037
  TdThreadMutex* pLock = &pMeta->pCache->STbRefDbCache.lock;
×
1038

1039
  (void)taosThreadMutexLock(pLock);
×
1040

1041
  SHashObj*  pEntry = NULL;
×
1042
  SHashObj** find = taosHashGet(pStbRefs, &suid, sizeof(uint64_t));
×
1043
  if (find == NULL) {
×
1044
    code = addRefDbsCacheNewEntry(pStbRefs, suid, &pEntry);
×
1045
    TSDB_CHECK_CODE(code, line, _return);
×
1046
  } else {  // check if it exists or not
1047
    pEntry = *find;
×
1048
  }
1049

1050
  for (int32_t i = 0; i < taosArrayGetSize(pList); i++) {
×
1051
    char* dbName = taosArrayGetP(pList, i);
×
1052
    void* pItem = taosHashGet(pEntry, dbName, strlen(dbName));
×
1053
    if (pItem == NULL) {
×
1054
      code = taosHashPut(pEntry, dbName, strlen(dbName), NULL, 0);
×
1055
      TSDB_CHECK_CODE(code, line, _return);
×
1056
    }
1057
  }
1058

1059
_return:
×
1060
  if (code) {
×
1061
    metaError("%s failed at line %d since %s", __func__, line, tstrerror(code));
×
1062
  }
1063
  (void)taosThreadMutexUnlock(pLock);
×
1064

1065
  return code;
×
1066
}
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