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

taosdata / TDengine / #4871

04 Dec 2025 01:55AM UTC coverage: 64.654% (+0.1%) from 64.545%
#4871

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

869 of 2219 new or added lines in 36 files covered. (39.16%)

441 existing lines in 120 files now uncovered.

159620 of 246882 relevant lines covered (64.65%)

110922946.31 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,146,118✔
114
  if (pMeta->pCache) {
8,146,118✔
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;
75,039,638✔
120
        taosMemoryFree(pEntry);
75,039,704✔
121
        pEntry = tEntry;
75,040,826✔
122
      }
123
    }
124
    taosMemoryFree(pMeta->pCache->sEntryCache.aBucket);
8,146,118✔
125
  }
126
}
8,146,118✔
127

128
static void statsCacheClose(SMeta* pMeta) {
8,146,118✔
129
  if (pMeta->pCache) {
8,146,118✔
130
    // close entry cache
131
    for (int32_t iBucket = 0; iBucket < pMeta->pCache->sStbStatsCache.nBucket; iBucket++) {
143,896,133✔
132
      SMetaStbStatsEntry* pEntry = pMeta->pCache->sStbStatsCache.aBucket[iBucket];
135,750,032✔
133
      while (pEntry) {
142,032,051✔
134
        SMetaStbStatsEntry* tEntry = pEntry->next;
6,282,036✔
135
        taosMemoryFree(pEntry);
6,282,036✔
136
        pEntry = tEntry;
6,282,416✔
137
      }
138
    }
139
    taosMemoryFree(pMeta->pCache->sStbStatsCache.aBucket);
8,146,118✔
140
  }
141
}
8,146,118✔
142

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

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

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

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

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

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

177
  // open entry cache
178
  pMeta->pCache->sEntryCache.nEntry = 0;
8,144,682✔
179
  pMeta->pCache->sEntryCache.nBucket = META_CACHE_BASE_BUCKET;
8,145,865✔
180
  pMeta->pCache->sEntryCache.aBucket =
8,157,927✔
181
      (SMetaCacheEntry**)taosMemoryCalloc(pMeta->pCache->sEntryCache.nBucket, sizeof(SMetaCacheEntry*));
8,145,119✔
182
  if (pMeta->pCache->sEntryCache.aBucket == NULL) {
8,146,118✔
183
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
184
  }
185

186
  // open stats cache
187
  pMeta->pCache->sStbStatsCache.nEntry = 0;
8,146,118✔
188
  pMeta->pCache->sStbStatsCache.nBucket = META_CACHE_STATS_BUCKET;
8,145,865✔
189
  pMeta->pCache->sStbStatsCache.aBucket =
8,157,831✔
190
      (SMetaStbStatsEntry**)taosMemoryCalloc(pMeta->pCache->sStbStatsCache.nBucket, sizeof(SMetaStbStatsEntry*));
8,145,865✔
191
  if (pMeta->pCache->sStbStatsCache.aBucket == NULL) {
8,145,865✔
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,146,118✔
197
  if (pMeta->pCache->sTagFilterResCache.pUidResCache == NULL) {
8,146,118✔
198
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
199
  }
200

201
  pMeta->pCache->sTagFilterResCache.accTimes = 0;
8,146,118✔
202
  pMeta->pCache->sTagFilterResCache.pTableEntry =
16,292,236✔
203
      taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), false, HASH_NO_LOCK);
16,280,270✔
204
  if (pMeta->pCache->sTagFilterResCache.pTableEntry == NULL) {
8,146,118✔
205
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
206
  }
207

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

211
  // open stable tag filter cache
212
  pMeta->pCache->sStableTagFilterResCache.accTimes = 0;
8,145,170✔
213
  pMeta->pCache->sStableTagFilterResCache.numTagDataEntries = 0;
8,146,118✔
214
  pMeta->pCache->sStableTagFilterResCache.pTableEntry =
16,291,515✔
215
    taosHashInit(1024,
16,278,725✔
216
      taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), false, HASH_NO_LOCK);
217
  if (pMeta->pCache->sStableTagFilterResCache.pTableEntry == NULL) {
8,145,994✔
218
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
219
  }
220
  taosHashSetFreeFp(
8,146,118✔
221
    pMeta->pCache->sStableTagFilterResCache.pTableEntry, freeTagCondsFp);
8,146,118✔
222

223
  TAOS_UNUSED(taosThreadRwlockInit(
8,146,118✔
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,146,118✔
228
  if (pMeta->pCache->STbGroupResCache.pResCache == NULL) {
8,146,118✔
229
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
230
  }
231

232
  pMeta->pCache->STbGroupResCache.accTimes = 0;
8,146,118✔
233
  pMeta->pCache->STbGroupResCache.pTableEntry =
16,292,236✔
234
      taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), false, HASH_NO_LOCK);
16,280,270✔
235
  if (pMeta->pCache->STbGroupResCache.pTableEntry == NULL) {
8,146,118✔
236
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
237
  }
238

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

242
  // open filter cache
243
  pMeta->pCache->STbFilterCache.pStb =
16,292,236✔
244
      taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
16,280,270✔
245
  if (pMeta->pCache->STbFilterCache.pStb == NULL) {
8,146,118✔
246
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
247
  }
248

249
  pMeta->pCache->STbFilterCache.pStbName =
16,292,236✔
250
      taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), false, HASH_NO_LOCK);
16,280,270✔
251
  if (pMeta->pCache->STbFilterCache.pStbName == NULL) {
8,146,118✔
252
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
253
  }
254

255
  // open ref db cache
256
  pMeta->pCache->STbRefDbCache.pStbRefs =
16,292,236✔
257
      taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
16,280,270✔
258
  if (pMeta->pCache->STbRefDbCache.pStbRefs == NULL) {
8,146,118✔
259
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
260
  }
261

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

265

266
_exit:
8,146,118✔
267
  if (code) {
8,146,118✔
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,146,118✔
272
  }
273
  return code;
8,146,118✔
274
}
275

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

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

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

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

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

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

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

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

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

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

321
  // rehash
322
  for (int32_t iBucket = 0; iBucket < pCache->sEntryCache.nBucket; iBucket++) {
66,938,724✔
323
    SMetaCacheEntry* pEntry = pCache->sEntryCache.aBucket[iBucket];
66,916,352✔
324

325
    while (pEntry) {
133,832,704✔
326
      SMetaCacheEntry* pTEntry = pEntry->next;
66,916,352✔
327

328
      pEntry->next = aBucket[TABS(pEntry->info.uid) % nBucket];
66,916,352✔
329
      aBucket[TABS(pEntry->info.uid) % nBucket] = pEntry;
66,916,352✔
330

331
      pEntry = pTEntry;
66,916,352✔
332
    }
333
  }
334

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

342
int32_t metaCacheUpsert(SMeta* pMeta, SMetaInfo* pInfo) {
97,777,619✔
343
  int32_t code = 0;
97,777,619✔
344

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

347
  // search
348
  SMetaCache*       pCache = pMeta->pCache;
97,777,619✔
349
  int32_t           iBucket = TABS(pInfo->uid) % pCache->sEntryCache.nBucket;
97,791,521✔
350
  SMetaCacheEntry** ppEntry = &pCache->sEntryCache.aBucket[iBucket];
97,793,733✔
351
  while (*ppEntry && (*ppEntry)->info.uid != pInfo->uid) {
112,049,870✔
352
    ppEntry = &(*ppEntry)->next;
14,285,066✔
353
  }
354

355
  if (*ppEntry) {  // update
97,777,992✔
356
    if (pInfo->suid != (*ppEntry)->info.suid) {
20,129,654✔
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,103,920✔
361
      (*ppEntry)->info.version = pInfo->version;
20,120,102✔
362
      (*ppEntry)->info.skmVer = pInfo->skmVer;
20,132,737✔
363
    }
364
  } else {  // insert
365
    if (pCache->sEntryCache.nEntry >= pCache->sEntryCache.nBucket) {
77,636,193✔
366
      metaRehashCache(pCache, 1);
22,372✔
367

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

371
    SMetaCacheEntry* pEntryNew = (SMetaCacheEntry*)taosMemoryMalloc(sizeof(*pEntryNew));
77,666,303✔
372
    if (pEntryNew == NULL) {
77,661,449✔
373
      code = terrno;
×
374
      goto _exit;
×
375
    }
376

377
    pEntryNew->info = *pInfo;
77,661,449✔
378
    pEntryNew->next = pCache->sEntryCache.aBucket[iBucket];
77,662,855✔
379
    pCache->sEntryCache.aBucket[iBucket] = pEntryNew;
77,662,404✔
380
    pCache->sEntryCache.nEntry++;
77,653,396✔
381
  }
382

383
_exit:
97,769,095✔
384
  return code;
97,769,095✔
385
}
386

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

390
  SMetaCache*       pCache = pMeta->pCache;
2,648,326✔
391
  int32_t           iBucket = TABS(uid) % pCache->sEntryCache.nBucket;
2,650,807✔
392
  SMetaCacheEntry** ppEntry = &pCache->sEntryCache.aBucket[iBucket];
2,650,498✔
393
  while (*ppEntry && (*ppEntry)->info.uid != uid) {
2,671,205✔
394
    ppEntry = &(*ppEntry)->next;
20,956✔
395
  }
396

397
  SMetaCacheEntry* pEntry = *ppEntry;
2,649,527✔
398
  if (pEntry) {
2,651,219✔
399
    *ppEntry = pEntry->next;
2,626,176✔
400
    taosMemoryFree(pEntry);
2,624,896✔
401
    pCache->sEntryCache.nEntry--;
2,625,813✔
402
    if (pCache->sEntryCache.nEntry < pCache->sEntryCache.nBucket / 4 &&
2,625,201✔
403
        pCache->sEntryCache.nBucket > META_CACHE_BASE_BUCKET) {
2,567,587✔
404
      metaRehashCache(pCache, 0);
×
405
    }
406
  } else {
407
    code = TSDB_CODE_NOT_FOUND;
25,043✔
408
  }
409

410
_exit:
2,651,219✔
411
  return code;
2,651,219✔
412
}
413

414
int32_t metaCacheGet(SMeta* pMeta, int64_t uid, SMetaInfo* pInfo) {
1,581,041,491✔
415
  int32_t code = 0;
1,581,041,491✔
416

417
  SMetaCache*      pCache = pMeta->pCache;
1,581,041,491✔
418
  int32_t          iBucket = TABS(uid) % pCache->sEntryCache.nBucket;
1,581,193,997✔
419
  SMetaCacheEntry* pEntry = pCache->sEntryCache.aBucket[iBucket];
1,581,185,484✔
420

421
  while (pEntry && pEntry->info.uid != uid) {
1,637,242,262✔
422
    pEntry = pEntry->next;
56,029,418✔
423
  }
424

425
  if (pEntry) {
1,581,212,051✔
426
    if (pInfo) {
1,495,151,493✔
427
      *pInfo = pEntry->info;
1,495,154,522✔
428
    }
429
  } else {
430
    code = TSDB_CODE_NOT_FOUND;
86,060,558✔
431
  }
432

433
  return code;
1,581,224,109✔
434
}
435

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

440
  if (expand) {
122,944✔
441
    nBucket = pCache->sStbStatsCache.nBucket * 2;
114,618✔
442
  } else {
443
    nBucket = pCache->sStbStatsCache.nBucket / 2;
8,326✔
444
  }
445

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

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

456
    while (pEntry) {
11,713,314✔
457
      SMetaStbStatsEntry* pTEntry = pEntry->next;
5,703,442✔
458

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

462
      pEntry = pTEntry;
5,703,442✔
463
    }
464
  }
465

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

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

475
int32_t metaStatsCacheUpsert(SMeta* pMeta, SMetaStbStats* pInfo) {
65,089,538✔
476
  int32_t code = 0;
65,089,538✔
477

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

480
  // search
481
  SMetaCache*          pCache = pMeta->pCache;
65,089,538✔
482
  int32_t              iBucket = TABS(pInfo->uid) % pCache->sStbStatsCache.nBucket;
65,097,717✔
483
  SMetaStbStatsEntry** ppEntry = &pCache->sStbStatsCache.aBucket[iBucket];
65,089,706✔
484
  while (*ppEntry && (*ppEntry)->info.uid != pInfo->uid) {
68,273,146✔
485
    ppEntry = &(*ppEntry)->next;
3,182,071✔
486
  }
487

488
  if (*ppEntry) {  // update
65,082,351✔
489
    (*ppEntry)->info.ctbNum = pInfo->ctbNum;
58,141,435✔
490
    (*ppEntry)->info.colNum = pInfo->colNum;
58,157,414✔
491
    (*ppEntry)->info.flags = pInfo->flags;
58,137,816✔
492
    (*ppEntry)->info.keep = pInfo->keep;
58,148,703✔
493
  } else {  // insert
494
    if (pCache->sStbStatsCache.nEntry >= pCache->sStbStatsCache.nBucket) {
6,942,778✔
495
      TAOS_UNUSED(metaRehashStatsCache(pCache, 1));
114,618✔
496
      iBucket = TABS(pInfo->uid) % pCache->sStbStatsCache.nBucket;
114,618✔
497
    }
498

499
    SMetaStbStatsEntry* pEntryNew = (SMetaStbStatsEntry*)taosMemoryMalloc(sizeof(*pEntryNew));
6,937,537✔
500
    if (pEntryNew == NULL) {
6,937,718✔
501
      code = terrno;
×
502
      goto _exit;
×
503
    }
504

505
    pEntryNew->info = *pInfo;
6,937,718✔
506
    pEntryNew->next = pCache->sStbStatsCache.aBucket[iBucket];
6,937,796✔
507
    pCache->sStbStatsCache.aBucket[iBucket] = pEntryNew;
6,938,082✔
508
    pCache->sStbStatsCache.nEntry++;
6,938,044✔
509
  }
510

511
_exit:
65,084,432✔
512
  return code;
65,084,432✔
513
}
514

515
int32_t metaStatsCacheDrop(SMeta* pMeta, int64_t uid) {
989,445✔
516
  int32_t code = 0;
989,445✔
517

518
  SMetaCache*          pCache = pMeta->pCache;
989,445✔
519
  int32_t              iBucket = TABS(uid) % pCache->sStbStatsCache.nBucket;
990,669✔
520
  SMetaStbStatsEntry** ppEntry = &pCache->sStbStatsCache.aBucket[iBucket];
990,057✔
521
  while (*ppEntry && (*ppEntry)->info.uid != uid) {
1,141,222✔
522
    ppEntry = &(*ppEntry)->next;
150,553✔
523
  }
524

525
  SMetaStbStatsEntry* pEntry = *ppEntry;
989,400✔
526
  if (pEntry) {
991,032✔
527
    *ppEntry = pEntry->next;
655,713✔
528
    taosMemoryFree(pEntry);
655,713✔
529
    pCache->sStbStatsCache.nEntry--;
654,738✔
530
    if (pCache->sStbStatsCache.nEntry < pCache->sStbStatsCache.nBucket / 4 &&
654,738✔
531
        pCache->sStbStatsCache.nBucket > META_CACHE_STATS_BUCKET) {
437,308✔
532
      TAOS_UNUSED(metaRehashStatsCache(pCache, 0));
8,326✔
533
    }
534
  } else {
535
    code = TSDB_CODE_NOT_FOUND;
335,319✔
536
  }
537

538
_exit:
990,057✔
539
  return code;
990,057✔
540
}
541

542
int32_t metaStatsCacheGet(SMeta* pMeta, int64_t uid, SMetaStbStats* pInfo) {
256,782,585✔
543
  int32_t code = TSDB_CODE_SUCCESS;
256,782,585✔
544

545
  SMetaCache*         pCache = pMeta->pCache;
256,782,585✔
546
  int32_t             iBucket = TABS(uid) % pCache->sStbStatsCache.nBucket;
256,799,440✔
547
  SMetaStbStatsEntry* pEntry = pCache->sStbStatsCache.aBucket[iBucket];
256,788,225✔
548

549
  while (pEntry && pEntry->info.uid != uid) {
291,701,041✔
550
    pEntry = pEntry->next;
34,893,791✔
551
  }
552

553
  if (pEntry) {
256,805,312✔
554
    if (pInfo) {
239,047,846✔
555
      *pInfo = pEntry->info;
239,046,795✔
556
    }
557
  } else {
558
    code = TSDB_CODE_NOT_FOUND;
17,757,466✔
559
  }
560

561
  return code;
256,804,850✔
562
}
563

564
static FORCE_INLINE void setMD5DigestInKey(uint64_t* pBuf, const char* key, int32_t keyLen) {
565
  memcpy(&pBuf[2], key, keyLen);
137,172,369✔
566
}
137,138,619✔
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) {
137,167,208✔
571
  buf[0] = (uint64_t)pHashMap;
137,167,208✔
572
  buf[1] = suid;
137,169,506✔
573
  setMD5DigestInKey(buf, key, keyLen);
574
}
137,168,817✔
575

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

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

586
  *acquireRes = 0;
62,644✔
587
  uint64_t key[4];
62,644✔
588
  initCacheKey(key, pTableMap, suid, (const char*)pKey, keyLen);
62,644✔
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);
62,644✔
597
  pMeta->pCache->sTagFilterResCache.accTimes += 1;
62,644✔
598

599
  LRUHandle* pHandle = taosLRUCacheLookup(pCache, key, TAG_FILTER_RES_KEY_LEN);
62,644✔
600
  if (pHandle == NULL) {
62,644✔
601
    (void)taosThreadMutexUnlock(pLock);
14,624✔
602
    return TSDB_CODE_SUCCESS;
14,624✔
603
  }
604

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

612
  *acquireRes = 1;
48,020✔
613

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

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

622
  (*pEntry)->hitTimes += 1;
48,020✔
623

624
  uint32_t acc = pMeta->pCache->sTagFilterResCache.accTimes;
48,020✔
625
  if ((*pEntry)->hitTimes % 5000 == 0 && (*pEntry)->hitTimes > 0) {
48,020✔
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);
48,020✔
631

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

637
int32_t metaStableTagFilterCacheGet(void* pVnode, tb_uid_t suid,
24,820✔
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;
24,820✔
642
  int32_t lino = 0;
24,820✔
643
  SMeta*  pMeta = ((SVnode*)pVnode)->pMeta;
24,820✔
644
  int32_t vgId = TD_VID(pMeta->pVnode);
24,820✔
645
  *acquireRes = 0;
24,820✔
646

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

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

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

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

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

666
  // set the result into the buffer
667
  *acquireRes = 1;
15,300✔
668
  TAOS_UNUSED(taosArrayAddBatch(
15,300✔
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;
15,300✔
673
  (*pFilterEntry)->hitTimes += 1;
15,300✔
674
  uint64_t hit = ++pMeta->pCache->sStableTagFilterResCache.hitTimes;
15,300✔
675
  uint64_t acc = pMeta->pCache->sStableTagFilterResCache.accTimes;
15,300✔
676
  if ((*pTagConds)->hitTimes % 1000 == 0 && (*pTagConds)->hitTimes > 0) {
15,300✔
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:
15,300✔
686
  if (TSDB_CODE_SUCCESS != code) {
24,820✔
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);
24,820✔
692
  if (TSDB_CODE_SUCCESS != code) {
24,820✔
693
    metaError("vgId:%d, %s unlock failed at %s:%d since %s",
×
694
      vgId, __func__, __FILE__, lino, tstrerror(code));
695
  }
696
  return code;
24,820✔
697
}
698

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

705
  const uint64_t* p = key;
14,624✔
706
  if (keyLen != sizeof(int64_t) * 4) {
14,624✔
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];
14,624✔
712

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

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

725
  taosMemoryFree(value);
14,624✔
726
}
727

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

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

742
_end:
10,088✔
743
  if (code != TSDB_CODE_SUCCESS) {
10,088✔
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;
10,088✔
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,
14,624✔
757
                              int32_t payloadLen, double selectivityRatio) {
758
  int32_t code = 0;
14,624✔
759
  SMeta*  pMeta = ((SVnode*)pVnode)->pMeta;
14,624✔
760
  int32_t vgId = TD_VID(pMeta->pVnode);
14,624✔
761

762
  if (selectivityRatio > tsSelectivityRatio) {
14,624✔
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) {
14,624✔
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;
14,624✔
779
  SHashObj*      pTableEntry = pMeta->pCache->sTagFilterResCache.pTableEntry;
14,624✔
780
  TdThreadMutex* pLock = &pMeta->pCache->sTagFilterResCache.lock;
14,624✔
781

782
  uint64_t key[4] = {0};
14,624✔
783
  initCacheKey(key, pTableEntry, suid, pKey, keyLen);
14,624✔
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);
14,624✔
792
  STagFilterResEntry** pEntry = taosHashGet(pTableEntry, &suid, sizeof(uint64_t));
14,624✔
793
  if (pEntry == NULL) {
14,624✔
794
    code = addNewEntry(pTableEntry, pKey, keyLen, suid);
9,748✔
795
    if (code != TSDB_CODE_SUCCESS) {
9,748✔
796
      goto _end;
×
797
    }
798
  } else {  // check if it exists or not
799
    code = taosHashPut((*pEntry)->set, pKey, keyLen, NULL, 0);
4,876✔
800
    if (code == TSDB_CODE_DUP_KEY) {
4,876✔
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,876✔
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,
14,624✔
812
                           TAOS_LRU_PRIORITY_LOW, NULL);
813
_end:
14,624✔
814
  (void)taosThreadMutexUnlock(pLock);
14,624✔
815
  metaDebug("vgId:%d, suid:%" PRIu64 " list cache added into cache, total:%d, tables:%d", vgId, suid,
14,624✔
816
            (int32_t)taosLRUCacheGetUsage(pCache), taosHashGetSize(pTableEntry));
817

818
  return code;
14,624✔
819
}
820

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

826
int32_t metaStableTagFilterCachePut(
9,520✔
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;
9,520✔
831
  int32_t lino = 0;
9,520✔
832
  SMeta*  pMeta = ((SVnode*)pVnode)->pMeta;
9,520✔
833
  int32_t vgId = TD_VID(pMeta->pVnode);
9,520✔
834

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

838
  code = taosThreadRwlockWrlock(pRwlock);
9,520✔
839
  TSDB_CHECK_CODE(code, lino, _end);
9,520✔
840

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

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

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

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

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

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

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

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

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

901
_end:
9,520✔
902
  if (TSDB_CODE_SUCCESS != code) {
9,520✔
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, "
9,520✔
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);
9,520✔
920
  if (TSDB_CODE_SUCCESS != code) {
9,520✔
921
    metaError("vgId:%d, %s unlock failed at %s:%d since %s",
×
922
      vgId, __func__, __FILE__, lino, tstrerror(code));
923
  }
924

925
  return code;
9,520✔
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(
31,960✔
969
  const SSchema* pTagSchemas, int32_t nTagCols, col_id_t cid) {
970
  for (int32_t i = 0; i < nTagCols; i++) {
176,120✔
971
    if (pTagSchemas[i].colId == cid) {
176,120✔
972
      return pTagSchemas[i].bytes;
31,960✔
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;
16,320✔
981

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

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

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

1002
static int32_t buildTagDataEntryKey(const SArray* pColIds, const STag* pTag,
78,540✔
1003
  const SSchemaWrapper* pTagScheam, T_MD5_CTX* pContext) {
1004
  int32_t code = TSDB_CODE_SUCCESS;
78,540✔
1005
  int32_t lino = 0;
78,540✔
1006
  int32_t keyLen = 0;
78,540✔
1007
  char* pKey = NULL;
78,540✔
1008
  // get length first
1009
  for (int32_t i = 0; i < taosArrayGetSize(pColIds); i++) {
231,540✔
1010
    STagVal pTagValue = {.cid = *(col_id_t*)taosArrayGet(pColIds, i)};
156,740✔
1011
    if (tTagGet(pTag, &pTagValue)) {
156,740✔
1012
      keyLen += sizeof(col_id_t);
153,000✔
1013
      if (IS_VAR_DATA_TYPE(pTagValue.type)) {
153,000✔
1014
        int32_t varLen = getTagColSize(
63,920✔
1015
          pTagScheam->pSchema, pTagScheam->nCols, pTagValue.cid);
31,960✔
1016
        code = varLen > 0 ? TSDB_CODE_SUCCESS : TSDB_CODE_NOT_FOUND;
31,960✔
1017
        QUERY_CHECK_CODE(code, lino, _end);
31,960✔
1018
        keyLen += varLen;
31,960✔
1019
      } else {
1020
        keyLen += tDataTypes[pTagValue.type].bytes;
121,040✔
1021
      }
1022
    } else {
1023
      // tag value not found
1024
      code = TSDB_CODE_NOT_FOUND;
3,740✔
1025
      QUERY_CHECK_CODE(code, lino, _end);
3,740✔
1026
    }
1027
  }
1028

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

1035
  // build the key
1036
  char* pStart = pKey;
74,800✔
1037
  for (int32_t i = 0; i < taosArrayGetSize(pColIds); i++) {
224,400✔
1038
    STagVal pTagValue = {.cid = *(col_id_t*)taosArrayGet(pColIds, i)};
149,600✔
1039
    if (tTagGet(pTag, &pTagValue)) {
149,600✔
1040
      // copy cid
1041
      memcpy(pStart, &pTagValue.cid, sizeof(col_id_t));
149,600✔
1042
      pStart += sizeof(col_id_t);
149,600✔
1043
      // copy value
1044
      if (IS_VAR_DATA_TYPE(pTagValue.type) && pTagValue.pData != NULL) {
149,600✔
1045
        if (TSDB_DATA_TYPE_NCHAR == pTagValue.type) {
31,960✔
1046
          // need to convert nchar to var
1047
          char *pVar = NULL;
16,320✔
1048
          code = ncharToVar((char *)pTagValue.pData, pTagValue.nData, &pVar);
16,320✔
1049
          QUERY_CHECK_CODE(code, lino, _end);
16,320✔
1050
          memcpy(pStart, varDataVal(pVar), varDataLen(pVar));
16,320✔
1051
          pStart += varDataLen(pVar);
16,320✔
1052
          taosMemoryFree(pVar);
16,320✔
1053
        } else {
1054
          memcpy(pStart, pTagValue.pData, pTagValue.nData);
15,640✔
1055
          pStart += pTagValue.nData;
15,640✔
1056
        }
1057
      } else {
1058
        memcpy(pStart, &pTagValue.i64, tDataTypes[pTagValue.type].bytes);
117,640✔
1059
        pStart += tDataTypes[pTagValue.type].bytes;
117,640✔
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);
74,800✔
1070
  tMD5Update(pContext, (uint8_t*)pKey, (uint32_t)keyLen);
74,800✔
1071
  tMD5Final(pContext);
74,800✔
1072

1073
_end:
78,540✔
1074
  taosMemFreeClear(pKey);
78,540✔
1075
  return code;
78,540✔
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,
76,542,581✔
1081
  const SMetaEntry* pChildTable, const SMetaEntry* pSuperTable,
1082
  ETagFilterCacheAction action) {
1083
  if (pMeta == NULL || pChildTable == NULL || pSuperTable == NULL) {
76,542,581✔
1084
    return TSDB_CODE_INVALID_PARA;
×
1085
  }
1086
  int32_t   lino = 0;
76,549,021✔
1087
  int32_t   code = TSDB_CODE_SUCCESS;
76,549,021✔
1088
  SHashObj* pTableEntry = pMeta->pCache->sStableTagFilterResCache.pTableEntry;
76,549,021✔
1089
  TdThreadRwlock* pRwlock = &pMeta->pCache->sStableTagFilterResCache.rwlock;
76,546,230✔
1090

1091
  code = taosThreadRwlockWrlock(pRwlock);
76,548,072✔
1092
  TSDB_CHECK_CODE(code, lino, _end);
76,545,070✔
1093

1094
  tb_uid_t suid = pChildTable->ctbEntry.suid;;
76,545,070✔
1095
  STagConds** pTagConds =
1096
    (STagConds**)taosHashGet(pTableEntry, &suid, sizeof(tb_uid_t));
76,544,724✔
1097
  if (pTagConds != NULL) {
76,550,462✔
1098
    STagCondFilterEntry** ppFilterEntry = NULL;
17,340✔
1099
    while ((ppFilterEntry = taosHashIterate((*pTagConds)->set, ppFilterEntry))) {
92,140✔
1100
      STagCondFilterEntry* pFilterEntry = *ppFilterEntry;
78,540✔
1101
      // rebuild the tagCondKey and check existence
1102
      SArray* pColIds = pFilterEntry->pColIds;
78,540✔
1103
      // rebuild the tagCondFilterKey
1104
      int32_t keyLen = 0;
78,540✔
1105
      char*   pKey = NULL;
78,540✔
1106
      T_MD5_CTX context = {0};
78,540✔
1107
      code = buildTagDataEntryKey(pColIds, (STag*)pChildTable->ctbEntry.pTags, 
78,540✔
1108
        &pSuperTable->stbEntry.schemaTag, &context);
1109
      if (code != TSDB_CODE_SUCCESS) {
78,540✔
1110
        metaError("vgId:%d, suid:%" PRIu64 " failed to build tag condition"
3,740✔
1111
          " key for dropped table uid:%" PRIu64 " since %s",
1112
          TD_VID(pMeta->pVnode), suid, pChildTable->uid, tstrerror(code));
1113
        goto _end;
3,740✔
1114
      }
1115

1116
      SArray** pArray = (SArray**)taosHashGet(
74,800✔
1117
        pFilterEntry->set, context.digest, tListLen(context.digest));
1118
      if (pArray != NULL) {
74,800✔
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) {
10,200✔
1122
          for (int32_t i = 0; i < taosArrayGetSize(*pArray); i++) {
10,200✔
1123
            uint64_t uid = *(uint64_t*)taosArrayGet(*pArray, i);
10,200✔
1124
            if (uid == pChildTable->uid) {
10,200✔
1125
              taosArrayRemove(*pArray, i);
5,100✔
1126
              metaDebug("vgId:%d, suid:%" PRIu64
5,100✔
1127
                " removed dropped table uid:%" PRIu64
1128
                " from stable tag filter cache",
1129
                TD_VID(pMeta->pVnode), suid, pChildTable->uid);
1130
              break;
5,100✔
1131
            } 
1132
          }
1133
        } else {
1134
          // STABLE_TAG_FILTER_CACHE_ADD_TABLE
1135
          void* _tmp = taosArrayPush(*pArray, &pChildTable->uid);
10,200✔
1136
        }
1137
      }
1138
    }
1139
  }
1140

1141
_end:
76,550,462✔
1142
  if (TSDB_CODE_SUCCESS != code) {
76,543,108✔
1143
    metaError("vgId:%d, %s failed at %s:%d since %s",
3,740✔
1144
      TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code));
1145
  } else {
1146
    metaDebug(
76,539,368✔
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);
76,543,252✔
1153
  if (TSDB_CODE_SUCCESS != code) {
76,545,045✔
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;
76,542,458✔
1158
}
1159

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

1170
  code = taosThreadRwlockWrlock(pRwlock);
314,032✔
1171
  TSDB_CHECK_CODE(code, lino, _end);
313,460✔
1172

1173
  STagConds** pTagConds =
1174
    (STagConds**)taosHashGet(pTableEntry, &suid, sizeof(tb_uid_t));
313,460✔
1175
  if (pTagConds != NULL) {
314,121✔
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
      }
UNCOV
1196
      pIter = taosHashIterate((*pTagConds)->set, pIter);
×
1197
    }
1198
  }
1199
_end:
314,121✔
1200
  if (TSDB_CODE_SUCCESS != code) {
314,121✔
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(
314,121✔
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);
314,121✔
1210
  if (TSDB_CODE_SUCCESS != code) {
314,032✔
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;
314,121✔
1215
}
1216

1217
void metaCacheClear(SMeta* pMeta) {
4,069,911✔
1218
  metaWLock(pMeta);
4,069,911✔
1219
  metaCacheClose(pMeta);
4,069,911✔
1220
  (void)metaCacheOpen(pMeta);
4,069,911✔
1221
  metaULock(pMeta);
4,069,911✔
1222
}
4,069,911✔
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) {
68,537,486✔
1226
  uint64_t  p[4] = {0};
68,537,486✔
1227
  int32_t   vgId = TD_VID(pMeta->pVnode);
68,539,666✔
1228
  SHashObj* pEntryHashMap = pMeta->pCache->sTagFilterResCache.pTableEntry;
68,541,490✔
1229

1230
  uint64_t dummy[2] = {0};
68,546,259✔
1231
  initCacheKey(p, pEntryHashMap, suid, (char*)&dummy[0], 16);
68,542,738✔
1232

1233
  TdThreadMutex* pLock = &pMeta->pCache->sTagFilterResCache.lock;
68,537,922✔
1234
  (void)taosThreadMutexLock(pLock);
68,542,040✔
1235

1236
  STagFilterResEntry** pEntry = taosHashGet(pEntryHashMap, &suid, sizeof(uint64_t));
68,542,199✔
1237
  if (pEntry == NULL || taosHashGetSize((*pEntry)->set) == 0) {
68,544,131✔
1238
    (void)taosThreadMutexUnlock(pLock);
68,540,579✔
1239
    return TSDB_CODE_SUCCESS;
68,544,033✔
1240
  }
1241

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

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

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

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

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

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

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

1273
  LRUHandle* pHandle = taosLRUCacheLookup(pCache, key, TAG_FILTER_RES_KEY_LEN);
1,360✔
1274
  if (pHandle == NULL) {
1,360✔
1275
    (void)taosThreadMutexUnlock(pLock);
1,360✔
1276
    return TSDB_CODE_SUCCESS;
1,360✔
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,360✔
1303
  (void)ud;
1304
  if (value == NULL) {
1,360✔
1305
    return;
×
1306
  }
1307

1308
  const uint64_t* p = key;
1,360✔
1309
  if (keyLen != sizeof(int64_t) * 4) {
1,360✔
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,360✔
1315

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

1318
  if (pEntry != NULL && (*pEntry) != NULL) {
1,360✔
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,360✔
1329
}
1330

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

1337
  if (payloadLen > tsTagFilterResCacheSize) {
1,360✔
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,360✔
1346
  SHashObj*      pTableEntry = pMeta->pCache->STbGroupResCache.pTableEntry;
1,360✔
1347
  TdThreadMutex* pLock = &pMeta->pCache->STbGroupResCache.lock;
1,360✔
1348

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

1352
  (void)taosThreadMutexLock(pLock);
1,360✔
1353
  STagFilterResEntry** pEntry = taosHashGet(pTableEntry, &suid, sizeof(uint64_t));
1,360✔
1354
  if (pEntry == NULL) {
1,360✔
1355
    code = addNewEntry(pTableEntry, pKey, keyLen, suid);
340✔
1356
    if (code != TSDB_CODE_SUCCESS) {
340✔
1357
      goto _end;
×
1358
    }
1359
  } else {  // check if it exists or not
1360
    code = taosHashPut((*pEntry)->set, pKey, keyLen, NULL, 0);
1,020✔
1361
    if (code == TSDB_CODE_DUP_KEY) {
1,020✔
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) {
1,020✔
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,360✔
1373
                           TAOS_LRU_PRIORITY_LOW, NULL);
1374
_end:
1,360✔
1375
  (void)taosThreadMutexUnlock(pLock);
1,360✔
1376
  metaDebug("vgId:%d, suid:%" PRIu64 " tb group added into cache, total:%d, tables:%d", vgId, suid,
1,360✔
1377
            (int32_t)taosLRUCacheGetUsage(pCache), taosHashGetSize(pTableEntry));
1378

1379
  return code;
1,360✔
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) {
68,537,656✔
1384
  uint64_t  p[4] = {0};
68,537,656✔
1385
  int32_t   vgId = TD_VID(pMeta->pVnode);
68,538,907✔
1386
  SHashObj* pEntryHashMap = pMeta->pCache->STbGroupResCache.pTableEntry;
68,541,931✔
1387

1388
  uint64_t dummy[2] = {0};
68,543,954✔
1389
  initCacheKey(p, pEntryHashMap, suid, (char*)&dummy[0], 16);
68,545,022✔
1390

1391
  TdThreadMutex* pLock = &pMeta->pCache->STbGroupResCache.lock;
68,545,443✔
1392
  (void)taosThreadMutexLock(pLock);
68,542,673✔
1393

1394
  STagFilterResEntry** pEntry = taosHashGet(pEntryHashMap, &suid, sizeof(uint64_t));
68,546,189✔
1395
  if (pEntry == NULL || taosHashGetSize((*pEntry)->set) == 0) {
68,546,611✔
1396
    (void)taosThreadMutexUnlock(pLock);
68,546,611✔
1397
    return TSDB_CODE_SUCCESS;
68,542,380✔
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) {
117,673,577✔
1416
  if (type == 0 && taosHashGet(pMeta->pCache->STbFilterCache.pStb, key, sizeof(tb_uid_t))) {
117,673,577✔
1417
    return true;
1,060✔
1418
  }
1419

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

1424
  return false;
117,681,926✔
1425
}
1426

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

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

1436
  return 0;
×
1437
}
1438

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

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

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

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

1473
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
193,226✔
1474
    return stats.keep;
167,929✔
1475
  }
1476

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

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

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

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

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

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

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

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

1519
  (void)taosThreadMutexLock(pLock);
961,908✔
1520

1521
  SHashObj** pEntry = taosHashGet(pTableMap, &suid, sizeof(uint64_t));
961,301✔
1522
  if (pEntry) {
961,309✔
1523
    void *iter = taosHashIterate(*pEntry, NULL);
938,705✔
1524
    while (iter != NULL) {
1,875,423✔
1525
      size_t   dbNameLen = 0;
936,119✔
1526
      char*    name = NULL;
936,119✔
1527
      char*    dbName = NULL;
936,119✔
1528
      name = taosHashGetKey(iter, &dbNameLen);
936,119✔
1529
      TSDB_CHECK_NULL(name, code, line, _return, terrno);
934,905✔
1530
      dbName = taosMemoryMalloc(dbNameLen + 1);
934,905✔
1531
      TSDB_CHECK_NULL(dbName, code, line, _return, terrno);
936,119✔
1532
      tstrncpy(dbName, name, dbNameLen + 1);
936,119✔
1533
      TSDB_CHECK_NULL(taosArrayPush(pList, &dbName), code, line, _return, terrno);
934,905✔
1534
      iter = taosHashIterate(*pEntry, iter);
934,905✔
1535
    }
1536
  }
1537

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

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

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

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

1557
  *pEntry = p;
22,604✔
1558

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

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

1573
  (void)taosThreadMutexLock(pLock);
25,789✔
1574

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

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

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

1599
  return code;
25,789✔
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