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

taosdata / TDengine / #4874

04 Dec 2025 01:55AM UTC coverage: 64.623% (+0.07%) from 64.558%
#4874

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

865 of 2219 new or added lines in 36 files covered. (38.98%)

6317 existing lines in 143 files now uncovered.

159543 of 246882 relevant lines covered (64.62%)

106415537.4 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

128
static void statsCacheClose(SMeta* pMeta) {
8,344,468✔
129
  if (pMeta->pCache) {
8,344,468✔
130
    // close entry cache
131
    for (int32_t iBucket = 0; iBucket < pMeta->pCache->sStbStatsCache.nBucket; iBucket++) {
147,354,672✔
132
      SMetaStbStatsEntry* pEntry = pMeta->pCache->sStbStatsCache.aBucket[iBucket];
139,009,769✔
133
      while (pEntry) {
145,422,948✔
134
        SMetaStbStatsEntry* tEntry = pEntry->next;
6,413,498✔
135
        taosMemoryFree(pEntry);
6,413,498✔
136
        pEntry = tEntry;
6,413,498✔
137
      }
138
    }
139
    taosMemoryFree(pMeta->pCache->sStbStatsCache.aBucket);
8,345,222✔
140
  }
141
}
8,345,222✔
142

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

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

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

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

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

172
  pMeta->pCache = (SMetaCache*)taosMemoryCalloc(1, sizeof(SMetaCache));
8,338,607✔
173
  if (pMeta->pCache == NULL) {
8,343,848✔
174
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
175
  }
176

177
  // open entry cache
178
  pMeta->pCache->sEntryCache.nEntry = 0;
8,344,859✔
179
  pMeta->pCache->sEntryCache.nBucket = META_CACHE_BASE_BUCKET;
8,343,285✔
180
  pMeta->pCache->sEntryCache.aBucket =
8,357,980✔
181
      (SMetaCacheEntry**)taosMemoryCalloc(pMeta->pCache->sEntryCache.nBucket, sizeof(SMetaCacheEntry*));
8,343,188✔
182
  if (pMeta->pCache->sEntryCache.aBucket == NULL) {
8,345,222✔
183
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
184
  }
185

186
  // open stats cache
187
  pMeta->pCache->sStbStatsCache.nEntry = 0;
8,345,222✔
188
  pMeta->pCache->sStbStatsCache.nBucket = META_CACHE_STATS_BUCKET;
8,344,254✔
189
  pMeta->pCache->sStbStatsCache.aBucket =
8,356,934✔
190
      (SMetaStbStatsEntry**)taosMemoryCalloc(pMeta->pCache->sStbStatsCache.nBucket, sizeof(SMetaStbStatsEntry*));
8,343,467✔
191
  if (pMeta->pCache->sStbStatsCache.aBucket == NULL) {
8,344,176✔
192
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
193
  }
194

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

201
  pMeta->pCache->sTagFilterResCache.accTimes = 0;
8,345,222✔
202
  pMeta->pCache->sTagFilterResCache.pTableEntry =
16,690,444✔
203
      taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), false, HASH_NO_LOCK);
16,677,686✔
204
  if (pMeta->pCache->sTagFilterResCache.pTableEntry == NULL) {
8,345,222✔
205
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
206
  }
207

208
  taosHashSetFreeFp(pMeta->pCache->sTagFilterResCache.pTableEntry, freeCacheEntryFp);
8,345,222✔
209
  (void)taosThreadMutexInit(&pMeta->pCache->sTagFilterResCache.lock, NULL);
8,345,222✔
210

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

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

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

232
  pMeta->pCache->STbGroupResCache.accTimes = 0;
8,345,222✔
233
  pMeta->pCache->STbGroupResCache.pTableEntry =
16,690,444✔
234
      taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), false, HASH_NO_LOCK);
16,677,686✔
235
  if (pMeta->pCache->STbGroupResCache.pTableEntry == NULL) {
8,345,222✔
236
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
237
  }
238

239
  taosHashSetFreeFp(pMeta->pCache->STbGroupResCache.pTableEntry, freeCacheEntryFp);
8,345,222✔
240
  (void)taosThreadMutexInit(&pMeta->pCache->STbGroupResCache.lock, NULL);
8,344,488✔
241

242
  // open filter cache
243
  pMeta->pCache->STbFilterCache.pStb =
16,690,444✔
244
      taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
16,677,686✔
245
  if (pMeta->pCache->STbFilterCache.pStb == NULL) {
8,345,222✔
246
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
247
  }
248

249
  pMeta->pCache->STbFilterCache.pStbName =
16,690,081✔
250
      taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), false, HASH_NO_LOCK);
16,677,323✔
251
  if (pMeta->pCache->STbFilterCache.pStbName == NULL) {
8,345,222✔
252
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
253
  }
254

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

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

265

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

276
void metaCacheClose(SMeta* pMeta) {
8,345,222✔
277
  if (pMeta->pCache) {
8,345,222✔
278
    entryCacheClose(pMeta);
8,345,222✔
279
    statsCacheClose(pMeta);
8,345,222✔
280

281
    taosHashClear(pMeta->pCache->sTagFilterResCache.pTableEntry);
8,345,222✔
282
    taosLRUCacheCleanup(pMeta->pCache->sTagFilterResCache.pUidResCache);
8,345,222✔
283
    (void)taosThreadMutexDestroy(&pMeta->pCache->sTagFilterResCache.lock);
8,345,222✔
284
    taosHashCleanup(pMeta->pCache->sTagFilterResCache.pTableEntry);
8,345,222✔
285

286
    (void)taosThreadRwlockDestroy(&pMeta->pCache->sStableTagFilterResCache.rwlock);
8,345,222✔
287
    taosHashCleanup(pMeta->pCache->sStableTagFilterResCache.pTableEntry);
8,344,488✔
288

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

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

297
    taosHashClear(pMeta->pCache->STbRefDbCache.pStbRefs);
8,344,675✔
298
    (void)taosThreadMutexDestroy(&pMeta->pCache->STbRefDbCache.lock);
8,345,222✔
299
    taosHashCleanup(pMeta->pCache->STbRefDbCache.pStbRefs);
8,345,222✔
300

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

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

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

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

321
  // rehash
322
  for (int32_t iBucket = 0; iBucket < pCache->sEntryCache.nBucket; iBucket++) {
68,113,735✔
323
    SMetaCacheEntry* pEntry = pCache->sEntryCache.aBucket[iBucket];
68,090,880✔
324

325
    while (pEntry) {
136,181,760✔
326
      SMetaCacheEntry* pTEntry = pEntry->next;
68,090,880✔
327

328
      pEntry->next = aBucket[TABS(pEntry->info.uid) % nBucket];
68,090,880✔
329
      aBucket[TABS(pEntry->info.uid) % nBucket] = pEntry;
68,090,880✔
330

331
      pEntry = pTEntry;
68,090,880✔
332
    }
333
  }
334

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

342
int32_t metaCacheUpsert(SMeta* pMeta, SMetaInfo* pInfo) {
101,308,582✔
343
  int32_t code = 0;
101,308,582✔
344

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

347
  // search
348
  SMetaCache*       pCache = pMeta->pCache;
101,308,582✔
349
  int32_t           iBucket = TABS(pInfo->uid) % pCache->sEntryCache.nBucket;
101,333,375✔
350
  SMetaCacheEntry** ppEntry = &pCache->sEntryCache.aBucket[iBucket];
101,313,458✔
351
  while (*ppEntry && (*ppEntry)->info.uid != pInfo->uid) {
115,953,654✔
352
    ppEntry = &(*ppEntry)->next;
14,667,408✔
353
  }
354

355
  if (*ppEntry) {  // update
101,260,741✔
356
    if (pInfo->suid != (*ppEntry)->info.suid) {
20,377,872✔
357
      metaError("meta/cache: suid should be same as the one in cache.");
×
358
      return TSDB_CODE_INVALID_PARA;
×
359
    }
360
    if (pInfo->version > (*ppEntry)->info.version) {
20,343,777✔
361
      (*ppEntry)->info.version = pInfo->version;
20,345,671✔
362
      (*ppEntry)->info.skmVer = pInfo->skmVer;
20,358,317✔
363
    }
364
  } else {  // insert
365
    if (pCache->sEntryCache.nEntry >= pCache->sEntryCache.nBucket) {
80,912,794✔
366
      metaRehashCache(pCache, 1);
22,855✔
367

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

371
    SMetaCacheEntry* pEntryNew = (SMetaCacheEntry*)taosMemoryMalloc(sizeof(*pEntryNew));
80,958,502✔
372
    if (pEntryNew == NULL) {
80,950,358✔
373
      code = terrno;
×
374
      goto _exit;
×
375
    }
376

377
    pEntryNew->info = *pInfo;
80,950,358✔
378
    pEntryNew->next = pCache->sEntryCache.aBucket[iBucket];
80,943,965✔
379
    pCache->sEntryCache.aBucket[iBucket] = pEntryNew;
80,925,704✔
380
    pCache->sEntryCache.nEntry++;
80,916,187✔
381
  }
382

383
_exit:
101,278,369✔
384
  return code;
101,278,369✔
385
}
386

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

390
  SMetaCache*       pCache = pMeta->pCache;
2,757,872✔
391
  int32_t           iBucket = TABS(uid) % pCache->sEntryCache.nBucket;
2,758,492✔
392
  SMetaCacheEntry** ppEntry = &pCache->sEntryCache.aBucket[iBucket];
2,757,196✔
393
  while (*ppEntry && (*ppEntry)->info.uid != uid) {
2,773,184✔
394
    ppEntry = &(*ppEntry)->next;
15,751✔
395
  }
396

397
  SMetaCacheEntry* pEntry = *ppEntry;
2,757,205✔
398
  if (pEntry) {
2,758,492✔
399
    *ppEntry = pEntry->next;
2,733,054✔
400
    taosMemoryFree(pEntry);
2,733,054✔
401
    pCache->sEntryCache.nEntry--;
2,732,387✔
402
    if (pCache->sEntryCache.nEntry < pCache->sEntryCache.nBucket / 4 &&
2,732,387✔
403
        pCache->sEntryCache.nBucket > META_CACHE_BASE_BUCKET) {
2,662,901✔
404
      metaRehashCache(pCache, 0);
×
405
    }
406
  } else {
407
    code = TSDB_CODE_NOT_FOUND;
25,438✔
408
  }
409

410
_exit:
2,758,492✔
411
  return code;
2,758,492✔
412
}
413

414
int32_t metaCacheGet(SMeta* pMeta, int64_t uid, SMetaInfo* pInfo) {
1,657,860,119✔
415
  int32_t code = 0;
1,657,860,119✔
416

417
  SMetaCache*      pCache = pMeta->pCache;
1,657,860,119✔
418
  int32_t          iBucket = TABS(uid) % pCache->sEntryCache.nBucket;
1,658,011,925✔
419
  SMetaCacheEntry* pEntry = pCache->sEntryCache.aBucket[iBucket];
1,658,002,624✔
420

421
  while (pEntry && pEntry->info.uid != uid) {
1,708,449,666✔
422
    pEntry = pEntry->next;
50,445,795✔
423
  }
424

425
  if (pEntry) {
1,658,013,249✔
426
    if (pInfo) {
1,568,166,193✔
427
      *pInfo = pEntry->info;
1,568,173,019✔
428
    }
429
  } else {
430
    code = TSDB_CODE_NOT_FOUND;
89,847,056✔
431
  }
432

433
  return code;
1,658,030,956✔
434
}
435

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

440
  if (expand) {
122,362✔
441
    nBucket = pCache->sStbStatsCache.nBucket * 2;
114,672✔
442
  } else {
443
    nBucket = pCache->sStbStatsCache.nBucket / 2;
7,690✔
444
  }
445

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

452
  // rehash
453
  for (int32_t iBucket = 0; iBucket < pCache->sStbStatsCache.nBucket; iBucket++) {
6,140,842✔
454
    SMetaStbStatsEntry* pEntry = pCache->sStbStatsCache.aBucket[iBucket];
6,018,480✔
455

456
    while (pEntry) {
11,764,310✔
457
      SMetaStbStatsEntry* pTEntry = pEntry->next;
5,745,830✔
458

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

462
      pEntry = pTEntry;
5,745,830✔
463
    }
464
  }
465

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

471
_exit:
122,362✔
472
  return code;
122,362✔
473
}
474

475
int32_t metaStatsCacheUpsert(SMeta* pMeta, SMetaStbStats* pInfo) {
67,729,763✔
476
  int32_t code = 0;
67,729,763✔
477

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

480
  // search
481
  SMetaCache*          pCache = pMeta->pCache;
67,729,763✔
482
  int32_t              iBucket = TABS(pInfo->uid) % pCache->sStbStatsCache.nBucket;
67,740,618✔
483
  SMetaStbStatsEntry** ppEntry = &pCache->sStbStatsCache.aBucket[iBucket];
67,731,842✔
484
  while (*ppEntry && (*ppEntry)->info.uid != pInfo->uid) {
70,716,730✔
485
    ppEntry = &(*ppEntry)->next;
2,992,137✔
486
  }
487

488
  if (*ppEntry) {  // update
67,734,009✔
489
    (*ppEntry)->info.ctbNum = pInfo->ctbNum;
60,632,928✔
490
    (*ppEntry)->info.colNum = pInfo->colNum;
60,633,640✔
491
    (*ppEntry)->info.flags = pInfo->flags;
60,619,214✔
492
    (*ppEntry)->info.keep = pInfo->keep;
60,628,016✔
493
  } else {  // insert
494
    if (pCache->sStbStatsCache.nEntry >= pCache->sStbStatsCache.nBucket) {
7,098,310✔
495
      TAOS_UNUSED(metaRehashStatsCache(pCache, 1));
114,672✔
496
      iBucket = TABS(pInfo->uid) % pCache->sStbStatsCache.nBucket;
114,672✔
497
    }
498

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

505
    pEntryNew->info = *pInfo;
7,093,022✔
506
    pEntryNew->next = pCache->sStbStatsCache.aBucket[iBucket];
7,093,007✔
507
    pCache->sStbStatsCache.aBucket[iBucket] = pEntryNew;
7,092,613✔
508
    pCache->sStbStatsCache.nEntry++;
7,092,206✔
509
  }
510

511
_exit:
67,698,393✔
512
  return code;
67,698,393✔
513
}
514

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

518
  SMetaCache*          pCache = pMeta->pCache;
1,015,767✔
519
  int32_t              iBucket = TABS(uid) % pCache->sStbStatsCache.nBucket;
1,015,767✔
520
  SMetaStbStatsEntry** ppEntry = &pCache->sStbStatsCache.aBucket[iBucket];
1,015,767✔
521
  while (*ppEntry && (*ppEntry)->info.uid != uid) {
1,161,467✔
522
    ppEntry = &(*ppEntry)->next;
146,367✔
523
  }
524

525
  SMetaStbStatsEntry* pEntry = *ppEntry;
1,015,767✔
526
  if (pEntry) {
1,015,767✔
527
    *ppEntry = pEntry->next;
679,796✔
528
    taosMemoryFree(pEntry);
679,796✔
529
    pCache->sStbStatsCache.nEntry--;
679,796✔
530
    if (pCache->sStbStatsCache.nEntry < pCache->sStbStatsCache.nBucket / 4 &&
679,796✔
531
        pCache->sStbStatsCache.nBucket > META_CACHE_STATS_BUCKET) {
434,386✔
532
      TAOS_UNUSED(metaRehashStatsCache(pCache, 0));
7,690✔
533
    }
534
  } else {
535
    code = TSDB_CODE_NOT_FOUND;
335,971✔
536
  }
537

538
_exit:
1,015,767✔
539
  return code;
1,015,767✔
540
}
541

542
int32_t metaStatsCacheGet(SMeta* pMeta, int64_t uid, SMetaStbStats* pInfo) {
263,871,338✔
543
  int32_t code = TSDB_CODE_SUCCESS;
263,871,338✔
544

545
  SMetaCache*         pCache = pMeta->pCache;
263,871,338✔
546
  int32_t             iBucket = TABS(uid) % pCache->sStbStatsCache.nBucket;
263,900,347✔
547
  SMetaStbStatsEntry* pEntry = pCache->sStbStatsCache.aBucket[iBucket];
263,893,953✔
548

549
  while (pEntry && pEntry->info.uid != uid) {
299,381,868✔
550
    pEntry = pEntry->next;
35,452,890✔
551
  }
552

553
  if (pEntry) {
263,923,230✔
554
    if (pInfo) {
245,759,302✔
555
      *pInfo = pEntry->info;
245,759,594✔
556
    }
557
  } else {
558
    code = TSDB_CODE_NOT_FOUND;
18,163,928✔
559
  }
560

561
  return code;
263,923,620✔
562
}
563

564
static FORCE_INLINE void setMD5DigestInKey(uint64_t* pBuf, const char* key, int32_t keyLen) {
565
  memcpy(&pBuf[2], key, keyLen);
142,446,841✔
566
}
142,411,227✔
567

568
// the format of key:
569
// hash table address(8bytes) + suid(8bytes) + MD5 digest(16bytes)
570
static void initCacheKey(uint64_t* buf, const SHashObj* pHashMap, uint64_t suid, const char* key, int32_t keyLen) {
142,435,420✔
571
  buf[0] = (uint64_t)pHashMap;
142,435,420✔
572
  buf[1] = suid;
142,439,636✔
573
  setMD5DigestInKey(buf, key, keyLen);
574
}
142,443,525✔
575

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

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

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

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

599
  LRUHandle* pHandle = taosLRUCacheLookup(pCache, key, TAG_FILTER_RES_KEY_LEN);
55,676✔
600
  if (pHandle == NULL) {
55,676✔
601
    (void)taosThreadMutexUnlock(pLock);
13,698✔
602
    return TSDB_CODE_SUCCESS;
13,698✔
603
  }
604

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

612
  *acquireRes = 1;
41,978✔
613

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

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

622
  (*pEntry)->hitTimes += 1;
41,978✔
623

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

630
  bool ret = taosLRUCacheRelease(pCache, pHandle, false);
41,978✔
631

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

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

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

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

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

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

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

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

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

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

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

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

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

711
  SHashObj* pHashObj = (SHashObj*)p[0];
13,698✔
712

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

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

725
  taosMemoryFree(value);
13,698✔
726
}
727

728
static int32_t addNewEntry(SHashObj* pTableEntry, const void* pKey, int32_t keyLen, uint64_t suid) {
9,258✔
729
  int32_t             code = TSDB_CODE_SUCCESS;
9,258✔
730
  int32_t             lino = 0;
9,258✔
731
  STagFilterResEntry* p = taosMemoryMalloc(sizeof(STagFilterResEntry));
9,258✔
732
  TSDB_CHECK_NULL(p, code, lino, _end, terrno);
9,258✔
733

734
  p->hitTimes = 0;
9,258✔
735
  p->set = taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
9,258✔
736
  TSDB_CHECK_NULL(p->set, code, lino, _end, terrno);
9,258✔
737
  code = taosHashPut(p->set, pKey, keyLen, NULL, 0);
9,258✔
738
  TSDB_CHECK_CODE(code, lino, _end);
9,258✔
739
  code = taosHashPut(pTableEntry, &suid, sizeof(uint64_t), &p, POINTER_BYTES);
9,258✔
740
  TSDB_CHECK_CODE(code, lino, _end);
9,258✔
741

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

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

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

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

778
  SLRUCache*     pCache = pMeta->pCache->sTagFilterResCache.pUidResCache;
13,698✔
779
  SHashObj*      pTableEntry = pMeta->pCache->sTagFilterResCache.pTableEntry;
13,698✔
780
  TdThreadMutex* pLock = &pMeta->pCache->sTagFilterResCache.lock;
13,698✔
781

782
  uint64_t key[4] = {0};
13,698✔
783
  initCacheKey(key, pTableEntry, suid, pKey, keyLen);
13,698✔
784

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

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

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

818
  return code;
13,698✔
819
}
820

821
static void freeSArrayPtr(void* pp) {
8,568✔
822
  SArray* pArray = *(SArray**)pp;
8,568✔
823
  taosArrayDestroy(pArray);
8,568✔
824
}
8,568✔
825

826
int32_t metaStableTagFilterCachePut(
8,568✔
827
  void* pVnode, uint64_t suid, const void* pTagCondKey, int32_t tagCondKeyLen,
828
  const void* pKey, int32_t keyLen, SArray* pUidList, SArray** pTagColIds) {
829

830
  int32_t code = TSDB_CODE_SUCCESS;
8,568✔
831
  int32_t lino = 0;
8,568✔
832
  SMeta*  pMeta = ((SVnode*)pVnode)->pMeta;
8,568✔
833
  int32_t vgId = TD_VID(pMeta->pVnode);
8,568✔
834

835
  SHashObj*       pTableEntry = pMeta->pCache->sStableTagFilterResCache.pTableEntry;
8,568✔
836
  TdThreadRwlock* pRwlock = &pMeta->pCache->sStableTagFilterResCache.rwlock;
8,568✔
837

838
  code = taosThreadRwlockWrlock(pRwlock);
8,568✔
839
  TSDB_CHECK_CODE(code, lino, _end);
8,568✔
840

841
  STagConds** pTagConds = 
842
    (STagConds**)taosHashGet(pTableEntry, &suid, sizeof(uint64_t));
8,568✔
843
  if (pTagConds == NULL) {
8,568✔
844
    // add new (suid -> tag conds) entry
845
    STagConds* pEntry = (STagConds*)taosMemoryMalloc(sizeof(STagConds));
1,224✔
846
    TSDB_CHECK_NULL(pEntry, code, lino, _end, terrno);
1,224✔
847

848
    pEntry->hitTimes = 0;
1,224✔
849
    pEntry->set = taosHashInit(
1,224✔
850
      1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY),
851
      false, HASH_NO_LOCK);
852
    taosHashSetFreeFp(pEntry->set, freeTagFilterEntryFp);
1,224✔
853
    TSDB_CHECK_NULL(pEntry->set, code, lino, _end, terrno);
1,224✔
854

855
    code = taosHashPut(
1,224✔
856
      pTableEntry, &suid, sizeof(uint64_t), &pEntry, POINTER_BYTES);
857
    TSDB_CHECK_CODE(code, lino, _end);
1,224✔
858

859
    pTagConds = (STagConds**)taosHashGet(pTableEntry, &suid, sizeof(uint64_t));
1,224✔
860
    TSDB_CHECK_NULL(pTagConds, code, lino, _end, TSDB_CODE_NOT_FOUND);
1,224✔
861
  }
862

863
  STagCondFilterEntry** pFilterEntry =
864
    (STagCondFilterEntry**)taosHashGet(
8,568✔
865
      (*pTagConds)->set, pTagCondKey, tagCondKeyLen);
8,568✔
866
  if (pFilterEntry == NULL) {
8,568✔
867
    // add new (tag cond -> filter entry) entry
868
    STagCondFilterEntry* pEntry = 
5,814✔
869
      (STagCondFilterEntry*)taosMemoryMalloc(sizeof(STagCondFilterEntry));
5,814✔
870
    TSDB_CHECK_NULL(pEntry, code, lino, _end, terrno);
5,814✔
871

872
    pEntry->hitTimes = 0;
5,814✔
873
    pEntry->set = taosHashInit(
5,814✔
874
      1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY),
875
      false, HASH_NO_LOCK);
876
    TSDB_CHECK_NULL(pEntry->set, code, lino, _end, terrno);
5,814✔
877
    taosHashSetFreeFp(pEntry->set, freeSArrayPtr);
5,814✔
878
    pEntry->pColIds = *pTagColIds;
5,814✔
879
    *pTagColIds = NULL;
5,814✔
880

881
    code = taosHashPut(
5,814✔
882
      (*pTagConds)->set, pTagCondKey, tagCondKeyLen, &pEntry, POINTER_BYTES);
5,814✔
883
    TSDB_CHECK_CODE(code, lino, _end);
5,814✔
884

885
    pFilterEntry = (STagCondFilterEntry**)taosHashGet(
5,814✔
886
      (*pTagConds)->set, pTagCondKey, tagCondKeyLen);
5,814✔
887
  } else {
888
    // pColIds is already set, so we can destroy the new one
889
    taosArrayDestroy(*pTagColIds);
2,754✔
890
    *pTagColIds = NULL;
2,754✔
891
  }
892

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

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

925
  return code;
8,568✔
926
}
927

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

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

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

968
static int32_t getTagColSize(
28,764✔
969
  const SSchema* pTagSchemas, int32_t nTagCols, col_id_t cid) {
970
  for (int32_t i = 0; i < nTagCols; i++) {
158,508✔
971
    if (pTagSchemas[i].colId == cid) {
158,508✔
972
      return pTagSchemas[i].bytes;
28,764✔
973
    }
974
  }
975
  return 0;
×
976
}
977

978
// when encode nchar tag into tag data entry key, need to convert it to var type
979
static FORCE_INLINE int32_t ncharToVar(char *pData, int32_t nData, char **ppOut) {
980
  int32_t code = TSDB_CODE_SUCCESS;
14,688✔
981

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

994
  *ppOut = taosMemoryCalloc(1, len + VARSTR_HEADER_SIZE);
14,688✔
995
  memcpy(*ppOut, t, len + VARSTR_HEADER_SIZE);
14,688✔
996

997
_return:
14,688✔
998
  taosMemoryFree(t);
14,688✔
999
  return code;
14,688✔
1000
}
1001

1002
static int32_t buildTagDataEntryKey(const SArray* pColIds, const STag* pTag,
70,686✔
1003
  const SSchemaWrapper* pTagScheam, T_MD5_CTX* pContext) {
1004
  int32_t code = TSDB_CODE_SUCCESS;
70,686✔
1005
  int32_t lino = 0;
70,686✔
1006
  int32_t keyLen = 0;
70,686✔
1007
  char* pKey = NULL;
70,686✔
1008
  // get length first
1009
  for (int32_t i = 0; i < taosArrayGetSize(pColIds); i++) {
208,386✔
1010
    STagVal pTagValue = {.cid = *(col_id_t*)taosArrayGet(pColIds, i)};
141,066✔
1011
    if (tTagGet(pTag, &pTagValue)) {
141,066✔
1012
      keyLen += sizeof(col_id_t);
137,700✔
1013
      if (IS_VAR_DATA_TYPE(pTagValue.type)) {
137,700✔
1014
        int32_t varLen = getTagColSize(
57,528✔
1015
          pTagScheam->pSchema, pTagScheam->nCols, pTagValue.cid);
28,764✔
1016
        code = varLen > 0 ? TSDB_CODE_SUCCESS : TSDB_CODE_NOT_FOUND;
28,764✔
1017
        QUERY_CHECK_CODE(code, lino, _end);
28,764✔
1018
        keyLen += varLen;
28,764✔
1019
      } else {
1020
        keyLen += tDataTypes[pTagValue.type].bytes;
108,936✔
1021
      }
1022
    } else {
1023
      // tag value not found
1024
      code = TSDB_CODE_NOT_FOUND;
3,366✔
1025
      QUERY_CHECK_CODE(code, lino, _end);
3,366✔
1026
    }
1027
  }
1028

1029
  pKey = taosMemoryCalloc(1, keyLen);
67,320✔
1030
  if (NULL == pKey) {
67,320✔
1031
    code = terrno;
×
1032
    return code;
×
1033
  }
1034

1035
  // build the key
1036
  char* pStart = pKey;
67,320✔
1037
  for (int32_t i = 0; i < taosArrayGetSize(pColIds); i++) {
201,960✔
1038
    STagVal pTagValue = {.cid = *(col_id_t*)taosArrayGet(pColIds, i)};
134,640✔
1039
    if (tTagGet(pTag, &pTagValue)) {
134,640✔
1040
      // copy cid
1041
      memcpy(pStart, &pTagValue.cid, sizeof(col_id_t));
134,640✔
1042
      pStart += sizeof(col_id_t);
134,640✔
1043
      // copy value
1044
      if (IS_VAR_DATA_TYPE(pTagValue.type) && pTagValue.pData != NULL) {
134,640✔
1045
        if (TSDB_DATA_TYPE_NCHAR == pTagValue.type) {
28,764✔
1046
          // need to convert nchar to var
1047
          char *pVar = NULL;
14,688✔
1048
          code = ncharToVar((char *)pTagValue.pData, pTagValue.nData, &pVar);
14,688✔
1049
          QUERY_CHECK_CODE(code, lino, _end);
14,688✔
1050
          memcpy(pStart, varDataVal(pVar), varDataLen(pVar));
14,688✔
1051
          pStart += varDataLen(pVar);
14,688✔
1052
          taosMemoryFree(pVar);
14,688✔
1053
        } else {
1054
          memcpy(pStart, pTagValue.pData, pTagValue.nData);
14,076✔
1055
          pStart += pTagValue.nData;
14,076✔
1056
        }
1057
      } else {
1058
        memcpy(pStart, &pTagValue.i64, tDataTypes[pTagValue.type].bytes);
105,876✔
1059
        pStart += tDataTypes[pTagValue.type].bytes;
105,876✔
1060
      }
1061
    } else {
1062
      // tag value not found
1063
      code = TSDB_CODE_NOT_FOUND;
×
1064
      QUERY_CHECK_CODE(code, lino, _end);
×
1065
    }
1066
  }
1067

1068
  // update MD5
1069
  tMD5Init(pContext);
67,320✔
1070
  tMD5Update(pContext, (uint8_t*)pKey, (uint32_t)keyLen);
67,320✔
1071
  tMD5Final(pContext);
67,320✔
1072

1073
_end:
70,686✔
1074
  taosMemFreeClear(pKey);
70,686✔
1075
  return code;
70,686✔
1076
}
1077

1078
// remove the dropped table uid from all cache entries
1079
// pDroppedTable is the dropped child table meta entry
1080
int32_t metaStableTagFilterCacheUpdateUid(SMeta* pMeta,
79,211,255✔
1081
  const SMetaEntry* pChildTable, const SMetaEntry* pSuperTable,
1082
  ETagFilterCacheAction action) {
1083
  if (pMeta == NULL || pChildTable == NULL || pSuperTable == NULL) {
79,211,255✔
1084
    return TSDB_CODE_INVALID_PARA;
×
1085
  }
1086
  int32_t   lino = 0;
79,222,145✔
1087
  int32_t   code = TSDB_CODE_SUCCESS;
79,222,145✔
1088
  SHashObj* pTableEntry = pMeta->pCache->sStableTagFilterResCache.pTableEntry;
79,222,145✔
1089
  TdThreadRwlock* pRwlock = &pMeta->pCache->sStableTagFilterResCache.rwlock;
79,215,009✔
1090

1091
  code = taosThreadRwlockWrlock(pRwlock);
79,221,720✔
1092
  TSDB_CHECK_CODE(code, lino, _end);
79,213,530✔
1093

1094
  tb_uid_t suid = pChildTable->ctbEntry.suid;;
79,213,530✔
1095
  STagConds** pTagConds =
1096
    (STagConds**)taosHashGet(pTableEntry, &suid, sizeof(tb_uid_t));
79,210,382✔
1097
  if (pTagConds != NULL) {
79,224,983✔
1098
    STagCondFilterEntry** ppFilterEntry = NULL;
15,606✔
1099
    while ((ppFilterEntry = taosHashIterate((*pTagConds)->set, ppFilterEntry))) {
82,926✔
1100
      STagCondFilterEntry* pFilterEntry = *ppFilterEntry;
70,686✔
1101
      // rebuild the tagCondKey and check existence
1102
      SArray* pColIds = pFilterEntry->pColIds;
70,686✔
1103
      // rebuild the tagCondFilterKey
1104
      int32_t keyLen = 0;
70,686✔
1105
      char*   pKey = NULL;
70,686✔
1106
      T_MD5_CTX context = {0};
70,686✔
1107
      code = buildTagDataEntryKey(pColIds, (STag*)pChildTable->ctbEntry.pTags, 
70,686✔
1108
        &pSuperTable->stbEntry.schemaTag, &context);
1109
      if (code != TSDB_CODE_SUCCESS) {
70,686✔
1110
        metaError("vgId:%d, suid:%" PRIu64 " failed to build tag condition"
3,366✔
1111
          " key for dropped table uid:%" PRIu64 " since %s",
1112
          TD_VID(pMeta->pVnode), suid, pChildTable->uid, tstrerror(code));
1113
        goto _end;
3,366✔
1114
      }
1115

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

1141
_end:
79,224,983✔
1142
  if (TSDB_CODE_SUCCESS != code) {
79,216,952✔
1143
    metaError("vgId:%d, %s failed at %s:%d since %s",
3,366✔
1144
      TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code));
1145
  } else {
1146
    metaDebug(
79,213,586✔
1147
      "vgId:%d, suid:%" PRIu64 " update table uid:%" PRIu64
1148
      " in stable tag filter cache, action:%d",
1149
      TD_VID(pMeta->pVnode),
1150
      pChildTable->ctbEntry.suid, pChildTable->uid, action);
1151
  }
1152
  code = taosThreadRwlockUnlock(pRwlock);
79,217,210✔
1153
  if (TSDB_CODE_SUCCESS != code) {
79,209,783✔
1154
    metaError("vgId:%d, %s unlock failed at %s:%d since %s",
×
1155
      TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code));
1156
  }
1157
  return code;
79,212,303✔
1158
}
1159

1160
int32_t metaStableTagFilterCacheDropTag(
301,754✔
1161
  SMeta* pMeta, tb_uid_t suid, col_id_t cid) {
1162
  if (pMeta == NULL) {
301,754✔
1163
    return TSDB_CODE_INVALID_PARA;
×
1164
  }
1165
  int32_t   lino = 0;
301,754✔
1166
  int32_t   code = TSDB_CODE_SUCCESS;
301,754✔
1167
  SHashObj* pTableEntry = pMeta->pCache->sStableTagFilterResCache.pTableEntry;
301,754✔
1168
  TdThreadRwlock* pRwlock = &pMeta->pCache->sStableTagFilterResCache.rwlock;
299,735✔
1169

1170
  code = taosThreadRwlockWrlock(pRwlock);
300,385✔
1171
  TSDB_CHECK_CODE(code, lino, _end);
301,959✔
1172

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

1217
void metaCacheClear(SMeta* pMeta) {
4,169,415✔
1218
  metaWLock(pMeta);
4,169,415✔
1219
  metaCacheClose(pMeta);
4,169,415✔
1220
  (void)metaCacheOpen(pMeta);
4,169,415✔
1221
  metaULock(pMeta);
4,169,415✔
1222
}
4,169,415✔
1223

1224
// remove the lru cache that are expired due to the tags value update, or creating, or dropping, of child tables
1225
int32_t metaUidCacheClear(SMeta* pMeta, uint64_t suid) {
71,178,949✔
1226
  uint64_t  p[4] = {0};
71,178,949✔
1227
  int32_t   vgId = TD_VID(pMeta->pVnode);
71,181,318✔
1228
  SHashObj* pEntryHashMap = pMeta->pCache->sTagFilterResCache.pTableEntry;
71,181,883✔
1229

1230
  uint64_t dummy[2] = {0};
71,181,563✔
1231
  initCacheKey(p, pEntryHashMap, suid, (char*)&dummy[0], 16);
71,189,648✔
1232

1233
  TdThreadMutex* pLock = &pMeta->pCache->sTagFilterResCache.lock;
71,180,113✔
1234
  (void)taosThreadMutexLock(pLock);
71,179,818✔
1235

1236
  STagFilterResEntry** pEntry = taosHashGet(pEntryHashMap, &suid, sizeof(uint64_t));
71,183,348✔
1237
  if (pEntry == NULL || taosHashGetSize((*pEntry)->set) == 0) {
71,186,829✔
1238
    (void)taosThreadMutexUnlock(pLock);
71,183,513✔
1239
    return TSDB_CODE_SUCCESS;
71,187,706✔
1240
  }
1241

1242
  (*pEntry)->hitTimes = 0;
3,316✔
1243

1244
  char *iter = taosHashIterate((*pEntry)->set, NULL);
3,316✔
1245
  while (iter != NULL) {
6,632✔
1246
    setMD5DigestInKey(p, iter, 2 * sizeof(uint64_t));
1247
    taosLRUCacheErase(pMeta->pCache->sTagFilterResCache.pUidResCache, p, TAG_FILTER_RES_KEY_LEN);
3,316✔
1248
    iter = taosHashIterate((*pEntry)->set, iter);
3,316✔
1249
  }
1250
  taosHashClear((*pEntry)->set);
3,316✔
1251
  (void)taosThreadMutexUnlock(pLock);
3,316✔
1252

1253
  metaDebug("vgId:%d suid:%" PRId64 " cached related tag filter uid list cleared", vgId, suid);
3,316✔
1254
  return TSDB_CODE_SUCCESS;
3,316✔
1255
}
1256

1257
int32_t metaGetCachedTbGroup(void* pVnode, tb_uid_t suid, const uint8_t* pKey, int32_t keyLen, SArray** pList) {
1,224✔
1258
  SMeta*  pMeta = ((SVnode*)pVnode)->pMeta;
1,224✔
1259
  int32_t vgId = TD_VID(pMeta->pVnode);
1,224✔
1260

1261
  // generate the composed key for LRU cache
1262
  SLRUCache*     pCache = pMeta->pCache->STbGroupResCache.pResCache;
1,224✔
1263
  SHashObj*      pTableMap = pMeta->pCache->STbGroupResCache.pTableEntry;
1,224✔
1264
  TdThreadMutex* pLock = &pMeta->pCache->STbGroupResCache.lock;
1,224✔
1265

1266
  *pList = NULL;
1,224✔
1267
  uint64_t key[4];
1,224✔
1268
  initCacheKey(key, pTableMap, suid, (const char*)pKey, keyLen);
1,224✔
1269

1270
  (void)taosThreadMutexLock(pLock);
1,224✔
1271
  pMeta->pCache->STbGroupResCache.accTimes += 1;
1,224✔
1272

1273
  LRUHandle* pHandle = taosLRUCacheLookup(pCache, key, TAG_FILTER_RES_KEY_LEN);
1,224✔
1274
  if (pHandle == NULL) {
1,224✔
1275
    (void)taosThreadMutexUnlock(pLock);
1,224✔
1276
    return TSDB_CODE_SUCCESS;
1,224✔
1277
  }
1278

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

1285
  *pList = taosArrayDup(taosLRUCacheValue(pCache, pHandle), NULL);
×
1286

1287
  (*pEntry)->hitTimes += 1;
×
1288

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

1295
  bool ret = taosLRUCacheRelease(pCache, pHandle, false);
×
1296

1297
  // unlock meta
1298
  (void)taosThreadMutexUnlock(pLock);
×
1299
  return TSDB_CODE_SUCCESS;
×
1300
}
1301

1302
static void freeTbGroupCachePayload(const void* key, size_t keyLen, void* value, void* ud) {
1,224✔
1303
  (void)ud;
1304
  if (value == NULL) {
1,224✔
1305
    return;
×
1306
  }
1307

1308
  const uint64_t* p = key;
1,224✔
1309
  if (keyLen != sizeof(int64_t) * 4) {
1,224✔
1310
    metaError("tb group key length is invalid, length:%d, expect:%d", (int32_t)keyLen, (int32_t)sizeof(uint64_t) * 2);
×
1311
    return;
×
1312
  }
1313

1314
  SHashObj* pHashObj = (SHashObj*)p[0];
1,224✔
1315

1316
  STagFilterResEntry** pEntry = taosHashGet(pHashObj, &p[1], sizeof(uint64_t));
1,224✔
1317

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

1328
  taosArrayDestroy((SArray*)value);
1,224✔
1329
}
1330

1331
int32_t metaPutTbGroupToCache(void* pVnode, uint64_t suid, const void* pKey, int32_t keyLen, void* pPayload,
1,224✔
1332
                              int32_t payloadLen) {
1333
  int32_t code = 0;
1,224✔
1334
  SMeta*  pMeta = ((SVnode*)pVnode)->pMeta;
1,224✔
1335
  int32_t vgId = TD_VID(pMeta->pVnode);
1,224✔
1336

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

1345
  SLRUCache*     pCache = pMeta->pCache->STbGroupResCache.pResCache;
1,224✔
1346
  SHashObj*      pTableEntry = pMeta->pCache->STbGroupResCache.pTableEntry;
1,224✔
1347
  TdThreadMutex* pLock = &pMeta->pCache->STbGroupResCache.lock;
1,224✔
1348

1349
  uint64_t key[4] = {0};
1,224✔
1350
  initCacheKey(key, pTableEntry, suid, pKey, keyLen);
1,224✔
1351

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

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

1379
  return code;
1,224✔
1380
}
1381

1382
// remove the lru cache that are expired due to the tags value update, or creating, or dropping, of child tables
1383
int32_t metaTbGroupCacheClear(SMeta* pMeta, uint64_t suid) {
71,176,154✔
1384
  uint64_t  p[4] = {0};
71,176,154✔
1385
  int32_t   vgId = TD_VID(pMeta->pVnode);
71,175,581✔
1386
  SHashObj* pEntryHashMap = pMeta->pCache->STbGroupResCache.pTableEntry;
71,173,647✔
1387

1388
  uint64_t dummy[2] = {0};
71,174,223✔
1389
  initCacheKey(p, pEntryHashMap, suid, (char*)&dummy[0], 16);
71,188,035✔
1390

1391
  TdThreadMutex* pLock = &pMeta->pCache->STbGroupResCache.lock;
71,187,390✔
1392
  (void)taosThreadMutexLock(pLock);
71,186,226✔
1393

1394
  STagFilterResEntry** pEntry = taosHashGet(pEntryHashMap, &suid, sizeof(uint64_t));
71,191,366✔
1395
  if (pEntry == NULL || taosHashGetSize((*pEntry)->set) == 0) {
71,190,822✔
1396
    (void)taosThreadMutexUnlock(pLock);
71,190,822✔
1397
    return TSDB_CODE_SUCCESS;
71,192,260✔
1398
  }
1399

1400
  (*pEntry)->hitTimes = 0;
×
1401

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

1411
  metaDebug("vgId:%d suid:%" PRId64 " cached related tb group cleared", vgId, suid);
×
1412
  return TSDB_CODE_SUCCESS;
×
1413
}
1414

1415
bool metaTbInFilterCache(SMeta* pMeta, const void* key, int8_t type) {
122,912,148✔
1416
  if (type == 0 && taosHashGet(pMeta->pCache->STbFilterCache.pStb, key, sizeof(tb_uid_t))) {
122,912,148✔
1417
    return true;
1,240✔
1418
  }
1419

1420
  if (type == 1 && taosHashGet(pMeta->pCache->STbFilterCache.pStbName, key, strlen(key))) {
122,910,908✔
1421
    return true;
×
1422
  }
1423

1424
  return false;
122,926,384✔
1425
}
1426

1427
int32_t metaPutTbToFilterCache(SMeta* pMeta, const void* key, int8_t type) {
7,440✔
1428
  if (type == 0) {
7,440✔
1429
    return taosHashPut(pMeta->pCache->STbFilterCache.pStb, key, sizeof(tb_uid_t), NULL, 0);
2,728✔
1430
  }
1431

1432
  if (type == 1) {
4,712✔
1433
    return taosHashPut(pMeta->pCache->STbFilterCache.pStbName, key, strlen(key), NULL, 0);
4,712✔
1434
  }
1435

1436
  return 0;
×
1437
}
1438

1439
int32_t metaSizeOfTbFilterCache(SMeta* pMeta, int8_t type) {
4,464✔
1440
  if (type == 0) {
4,464✔
1441
    return taosHashGetSize(pMeta->pCache->STbFilterCache.pStb);
4,464✔
1442
  }
1443
  return 0;
×
1444
}
1445

1446
int32_t metaInitTbFilterCache(SMeta* pMeta) {
4,174,966✔
1447
#ifdef TD_ENTERPRISE
1448
  int32_t      tbNum = 0;
4,174,966✔
1449
  const char** pTbArr = NULL;
4,174,966✔
1450
  const char*  dbName = NULL;
4,174,966✔
1451

1452
  if (!(dbName = strchr(pMeta->pVnode->config.dbname, '.'))) return 0;
4,174,966✔
1453
  if (0 == strncmp(++dbName, "log", TSDB_DB_NAME_LEN)) {
4,174,389✔
1454
    tbNum = tkLogStbNum;
248✔
1455
    pTbArr = (const char**)&tkLogStb;
248✔
1456
  } else if (0 == strncmp(dbName, "audit", TSDB_DB_NAME_LEN)) {
4,174,141✔
1457
    tbNum = tkAuditStbNum;
×
1458
    pTbArr = (const char**)&tkAuditStb;
×
1459
  }
1460
  if (tbNum && pTbArr) {
4,174,389✔
1461
    for (int32_t i = 0; i < tbNum; ++i) {
4,960✔
1462
      TAOS_CHECK_RETURN(metaPutTbToFilterCache(pMeta, pTbArr[i], 1));
4,712✔
1463
    }
1464
  }
1465
#else
1466
#endif
1467
  return 0;
4,174,389✔
1468
}
1469

1470
int64_t metaGetStbKeep(SMeta* pMeta, int64_t uid) {
190,526✔
1471
  SMetaStbStats stats = {0};
190,526✔
1472

1473
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
190,526✔
1474
    return stats.keep;
164,183✔
1475
  }
1476

1477
  SMetaEntry* pEntry = NULL;
25,679✔
1478
  if (metaFetchEntryByUid(pMeta, uid, &pEntry) == TSDB_CODE_SUCCESS) {
25,679✔
1479
    int64_t keep = -1;
25,047✔
1480
    if (pEntry->type == TSDB_SUPER_TABLE) {
25,047✔
1481
      keep = pEntry->stbEntry.keep;
25,047✔
1482
    }
1483
    metaFetchEntryFree(&pEntry);
25,047✔
1484
    return keep;
25,047✔
1485
  }
1486
  
1487
  return -1;
632✔
1488
}
1489

1490
int32_t metaRefDbsCacheClear(SMeta* pMeta, uint64_t suid) {
308,536✔
1491
  int32_t        code = TSDB_CODE_SUCCESS;
308,536✔
1492
  int32_t        vgId = TD_VID(pMeta->pVnode);
308,536✔
1493
  SHashObj*      pEntryHashMap = pMeta->pCache->STbRefDbCache.pStbRefs;
308,536✔
1494
  TdThreadMutex* pLock = &pMeta->pCache->STbRefDbCache.lock;
308,536✔
1495

1496
  (void)taosThreadMutexLock(pLock);
308,536✔
1497

1498
  SHashObj** pEntry = taosHashGet(pEntryHashMap, &suid, sizeof(uint64_t));
308,536✔
1499
  if (pEntry == NULL) {
308,536✔
1500
    goto _return;
305,646✔
1501
  }
1502

1503
  code = taosHashRemove(pEntryHashMap, &suid, sizeof(uint64_t));
2,890✔
1504

1505
  metaDebug("vgId:%d suid:%" PRId64 " cached virtual stable ref db cleared", vgId, suid);
2,890✔
1506

1507
_return:
2,086✔
1508
  (void)taosThreadMutexUnlock(pLock);
308,536✔
1509
  return code;
308,536✔
1510
}
1511

1512
int32_t metaGetCachedRefDbs(void* pVnode, tb_uid_t suid, SArray* pList) {
51,242✔
1513
  int32_t        code = TSDB_CODE_SUCCESS;
51,242✔
1514
  int32_t        line = 0;
51,242✔
1515
  SMeta*         pMeta = ((SVnode*)pVnode)->pMeta;
51,242✔
1516
  SHashObj*      pTableMap = pMeta->pCache->STbRefDbCache.pStbRefs;
51,242✔
1517
  TdThreadMutex* pLock = &pMeta->pCache->STbRefDbCache.lock;
51,242✔
1518

1519
  (void)taosThreadMutexLock(pLock);
51,242✔
1520

1521
  SHashObj** pEntry = taosHashGet(pTableMap, &suid, sizeof(uint64_t));
51,242✔
1522
  if (pEntry) {
51,242✔
1523
    void *iter = taosHashIterate(*pEntry, NULL);
31,846✔
1524
    while (iter != NULL) {
60,432✔
1525
      size_t   dbNameLen = 0;
28,586✔
1526
      char*    name = NULL;
28,586✔
1527
      char*    dbName = NULL;
28,586✔
1528
      name = taosHashGetKey(iter, &dbNameLen);
28,586✔
1529
      TSDB_CHECK_NULL(name, code, line, _return, terrno);
28,586✔
1530
      dbName = taosMemoryMalloc(dbNameLen + 1);
28,586✔
1531
      TSDB_CHECK_NULL(dbName, code, line, _return, terrno);
28,586✔
1532
      tstrncpy(dbName, name, dbNameLen + 1);
28,586✔
1533
      TSDB_CHECK_NULL(taosArrayPush(pList, &dbName), code, line, _return, terrno);
28,586✔
1534
      iter = taosHashIterate(*pEntry, iter);
28,586✔
1535
    }
1536
  }
1537

1538
_return:
51,242✔
1539
  if (code) {
51,242✔
1540
    metaError("%s failed at line %d since %s", __func__, line, tstrerror(code));
×
1541
  }
1542
  (void)taosThreadMutexUnlock(pLock);
51,242✔
1543
  return code;
51,242✔
1544
}
1545

1546
static int32_t addRefDbsCacheNewEntry(SHashObj* pRefDbs, uint64_t suid, SHashObj **pEntry) {
19,396✔
1547
  int32_t      code = TSDB_CODE_SUCCESS;
19,396✔
1548
  int32_t      lino = 0;
19,396✔
1549
  SHashObj*    p = NULL;
19,396✔
1550

1551
  p = taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
19,396✔
1552
  TSDB_CHECK_NULL(p, code, lino, _end, terrno);
19,396✔
1553

1554
  code = taosHashPut(pRefDbs, &suid, sizeof(uint64_t), &p, POINTER_BYTES);
19,396✔
1555
  TSDB_CHECK_CODE(code, lino, _end);
19,396✔
1556

1557
  *pEntry = p;
19,396✔
1558

1559
_end:
19,396✔
1560
  if (code != TSDB_CODE_SUCCESS) {
19,396✔
1561
    metaError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1562
  }
1563
  return code;
19,396✔
1564
}
1565

1566
int32_t metaPutRefDbsToCache(void* pVnode, tb_uid_t suid, SArray* pList) {
22,656✔
1567
  int32_t        code = 0;
22,656✔
1568
  int32_t        line = 0;
22,656✔
1569
  SMeta*         pMeta = ((SVnode*)pVnode)->pMeta;
22,656✔
1570
  SHashObj*      pStbRefs = pMeta->pCache->STbRefDbCache.pStbRefs;
22,656✔
1571
  TdThreadMutex* pLock = &pMeta->pCache->STbRefDbCache.lock;
22,656✔
1572

1573
  (void)taosThreadMutexLock(pLock);
22,656✔
1574

1575
  SHashObj*  pEntry = NULL;
22,656✔
1576
  SHashObj** find = taosHashGet(pStbRefs, &suid, sizeof(uint64_t));
22,656✔
1577
  if (find == NULL) {
22,656✔
1578
    code = addRefDbsCacheNewEntry(pStbRefs, suid, &pEntry);
19,396✔
1579
    TSDB_CHECK_CODE(code, line, _return);
19,396✔
1580
  } else {  // check if it exists or not
1581
    pEntry = *find;
3,260✔
1582
  }
1583

1584
  for (int32_t i = 0; i < taosArrayGetSize(pList); i++) {
39,478✔
1585
    char* dbName = taosArrayGetP(pList, i);
16,822✔
1586
    void* pItem = taosHashGet(pEntry, dbName, strlen(dbName));
16,822✔
1587
    if (pItem == NULL) {
16,822✔
1588
      code = taosHashPut(pEntry, dbName, strlen(dbName), NULL, 0);
16,822✔
1589
      TSDB_CHECK_CODE(code, line, _return);
16,822✔
1590
    }
1591
  }
1592

1593
_return:
22,656✔
1594
  if (code) {
22,656✔
1595
    metaError("%s failed at line %d since %s", __func__, line, tstrerror(code));
×
1596
  }
1597
  (void)taosThreadMutexUnlock(pLock);
22,656✔
1598

1599
  return code;
22,656✔
1600
}
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