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

taosdata / TDengine / #4834

31 Oct 2025 03:37AM UTC coverage: 58.764% (+0.2%) from 58.533%
#4834

push

travis-ci

SallyHuo-TAOS
Merge remote-tracking branch 'origin/cover/3.0' into cover/3.0

# Conflicts:
#	test/ci/run.sh

149869 of 324176 branches covered (46.23%)

Branch coverage included in aggregate %.

199000 of 269498 relevant lines covered (73.84%)

239175663.62 hits per line

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

68.19
/source/util/src/tlrucache.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

16
#define _DEFAULT_SOURCE
17
#include "tlrucache.h"
18
#include "os.h"
19
#include "taoserror.h"
20
#include "tarray.h"
21
#include "tdef.h"
22
#include "tlog.h"
23
#include "tutil.h"
24

25
typedef struct SLRUEntry      SLRUEntry;
26
typedef struct SLRUEntryTable SLRUEntryTable;
27
typedef struct SLRUCacheShard SLRUCacheShard;
28
typedef struct SShardedCache  SShardedCache;
29

30
enum {
31
  TAOS_LRU_IN_CACHE = (1 << 0),  // Whether this entry is referenced by the hash table.
32

33
  TAOS_LRU_IS_HIGH_PRI = (1 << 1),  // Whether this entry is high priority entry.
34

35
  TAOS_LRU_IN_HIGH_PRI_POOL = (1 << 2),  // Whether this entry is in high-pri pool.
36

37
  TAOS_LRU_HAS_HIT = (1 << 3),  // Whether this entry has had any lookups (hits).
38
};
39

40
struct SLRUEntry {
41
  void                  *value;
42
  _taos_lru_deleter_t    deleter;
43
  _taos_lru_overwriter_t overwriter;
44
  void                  *ud;
45
  SLRUEntry             *nextHash;
46
  SLRUEntry             *next;
47
  SLRUEntry             *prev;
48
  size_t                 totalCharge;
49
  size_t                 keyLength;
50
  uint32_t               hash;
51
  uint32_t               refs;
52
  uint8_t                flags;
53
  char                   keyData[1];
54
};
55

56
#define TAOS_LRU_ENTRY_IN_CACHE(h)     ((h)->flags & TAOS_LRU_IN_CACHE)
57
#define TAOS_LRU_ENTRY_IN_HIGH_POOL(h) ((h)->flags & TAOS_LRU_IN_HIGH_PRI_POOL)
58
#define TAOS_LRU_ENTRY_IS_HIGH_PRI(h)  ((h)->flags & TAOS_LRU_IS_HIGH_PRI)
59
#define TAOS_LRU_ENTRY_HAS_HIT(h)      ((h)->flags & TAOS_LRU_HAS_HIT)
60

61
#define TAOS_LRU_ENTRY_SET_IN_CACHE(h, inCache) \
62
  do {                                          \
63
    if (inCache) {                              \
64
      (h)->flags |= TAOS_LRU_IN_CACHE;          \
65
    } else {                                    \
66
      (h)->flags &= ~TAOS_LRU_IN_CACHE;         \
67
    }                                           \
68
  } while (0)
69
#define TAOS_LRU_ENTRY_SET_IN_HIGH_POOL(h, inHigh) \
70
  do {                                             \
71
    if (inHigh) {                                  \
72
      (h)->flags |= TAOS_LRU_IN_HIGH_PRI_POOL;     \
73
    } else {                                       \
74
      (h)->flags &= ~TAOS_LRU_IN_HIGH_PRI_POOL;    \
75
    }                                              \
76
  } while (0)
77
#define TAOS_LRU_ENTRY_SET_PRIORITY(h, priority) \
78
  do {                                           \
79
    if (priority == TAOS_LRU_PRIORITY_HIGH) {    \
80
      (h)->flags |= TAOS_LRU_IS_HIGH_PRI;        \
81
    } else {                                     \
82
      (h)->flags &= ~TAOS_LRU_IS_HIGH_PRI;       \
83
    }                                            \
84
  } while (0)
85
#define TAOS_LRU_ENTRY_SET_HIT(h) ((h)->flags |= TAOS_LRU_HAS_HIT)
86

87
#define TAOS_LRU_ENTRY_HAS_REFS(h) ((h)->refs > 0)
88
#define TAOS_LRU_ENTRY_REF(h)      (++(h)->refs)
89

90
static bool taosLRUEntryUnref(SLRUEntry *entry) {
507,711,979✔
91
  --entry->refs;
507,711,979✔
92
  return entry->refs == 0;
507,864,271✔
93
}
94

95
static void taosLRUEntryFree(SLRUEntry *entry) {
1,238,665,415✔
96
  if (entry->deleter) {
1,238,665,415!
97
    (*entry->deleter)(entry->keyData, entry->keyLength, entry->value, entry->ud);
1,238,740,175✔
98
  }
99

100
  taosMemoryFree(entry);
1,238,727,654✔
101
}
1,238,727,957✔
102

103
typedef void (*_taos_lru_table_func_t)(SLRUEntry *entry);
104

105
struct SLRUEntryTable {
106
  int         lengthBits;
107
  SLRUEntry **list;
108
  uint32_t    elems;
109
  int         maxLengthBits;
110
};
111

112
static int taosLRUEntryTableInit(SLRUEntryTable *table, int maxUpperHashBits) {
1,463,841,307✔
113
  table->lengthBits = 16;
1,463,841,307✔
114
  table->list = taosMemoryCalloc(1 << table->lengthBits, sizeof(SLRUEntry *));
1,464,296,947!
115
  if (!table->list) {
1,465,258,238!
116
    TAOS_RETURN(terrno);
×
117
  }
118

119
  table->elems = 0;
1,465,253,200✔
120
  table->maxLengthBits = maxUpperHashBits;
1,465,257,920✔
121

122
  TAOS_RETURN(TSDB_CODE_SUCCESS);
1,465,244,041✔
123
}
124

125
static void taosLRUEntryTableApply(SLRUEntryTable *table, _taos_lru_table_func_t func, uint32_t begin, uint32_t end) {
1,464,833,979✔
126
  for (uint32_t i = begin; i < end; ++i) {
2,147,483,647✔
127
    SLRUEntry *h = table->list[i];
2,147,483,647✔
128
    while (h) {
2,147,483,647✔
129
      SLRUEntry *n = h->nextHash;
1,078,036,397✔
130
      func(h);
1,078,058,383✔
131
      h = n;
70✔
132
    }
133
  }
134
}
156,554✔
135

136
static void taosLRUEntryTableFree(SLRUEntry *entry) {
1,078,193,432✔
137
  if (!TAOS_LRU_ENTRY_HAS_REFS(entry)) {
1,078,193,432!
138
    taosLRUEntryFree(entry);
1,078,202,045✔
139
  }
140
}
1,078,316,716✔
141

142
static void taosLRUEntryTableCleanup(SLRUEntryTable *table) {
1,464,859,747✔
143
  taosLRUEntryTableApply(table, taosLRUEntryTableFree, 0, 1 << table->lengthBits);
1,464,859,747!
144

145
  taosMemoryFree(table->list);
1,465,145,492✔
146
}
1,465,161,598✔
147

148
static int taosLRUEntryTableApplyF(SLRUEntryTable *table, _taos_lru_functor_t functor, void *ud) {
2,999,801✔
149
  int      ret = 0;
2,999,801✔
150
  uint32_t end = 1 << table->lengthBits;
2,999,801!
151
  for (uint32_t i = 0; i < end; ++i) {
2,147,483,647!
152
    SLRUEntry *h = table->list[i];
2,147,483,647✔
153
    while (h) {
2,147,483,647✔
154
      SLRUEntry *n = h->nextHash;
100,033,561✔
155
      ret = functor(h->keyData, h->keyLength, h->value, ud);
100,033,561✔
156
      if (ret) {
100,043,276!
157
        return ret;
×
158
      }
159
      h = n;
100,043,276✔
160
    }
161
  }
162

163
  return ret;
×
164
}
165

166
static SLRUEntry **taosLRUEntryTableFindPtr(SLRUEntryTable *table, const void *key, size_t keyLen, uint32_t hash) {
2,147,483,647✔
167
  SLRUEntry **entry = &table->list[hash >> (32 - table->lengthBits)];
2,147,483,647!
168
  while (*entry && ((*entry)->hash != hash || memcmp(key, (*entry)->keyData, keyLen) != 0)) {
2,147,483,647!
169
    entry = &(*entry)->nextHash;
10,367,145✔
170
  }
171

172
  return entry;
2,147,483,647✔
173
}
174

175
static void taosLRUEntryTableResize(SLRUEntryTable *table) {
×
176
  int lengthBits = table->lengthBits;
×
177
  if (lengthBits >= table->maxLengthBits) {
×
178
    return;
×
179
  }
180

181
  if (lengthBits >= 31) {
×
182
    return;
×
183
  }
184

185
  uint32_t    oldLength = 1 << lengthBits;
×
186
  int         newLengthBits = lengthBits + 1;
×
187
  SLRUEntry **newList = taosMemoryCalloc(1 << newLengthBits, sizeof(SLRUEntry *));
×
188
  if (!newList) {
×
189
    return;
×
190
  }
191
  uint32_t count = 0;
×
192
  for (uint32_t i = 0; i < oldLength; ++i) {
×
193
    SLRUEntry *entry = table->list[i];
×
194
    while (entry) {
×
195
      SLRUEntry  *next = entry->nextHash;
×
196
      uint32_t    hash = entry->hash;
×
197
      SLRUEntry **ptr = &newList[hash >> (32 - newLengthBits)];
×
198
      entry->nextHash = *ptr;
×
199
      *ptr = entry;
×
200
      entry = next;
×
201
      ++count;
×
202
    }
203
  }
204

205
  taosMemoryFree(table->list);
×
206
  table->list = newList;
×
207
  table->lengthBits = newLengthBits;
×
208
}
209

210
static SLRUEntry *taosLRUEntryTableLookup(SLRUEntryTable *table, const void *key, size_t keyLen, uint32_t hash) {
1,699,281,627✔
211
  return *taosLRUEntryTableFindPtr(table, key, keyLen, hash);
1,699,281,627✔
212
}
213

214
static SLRUEntry *taosLRUEntryTableInsert(SLRUEntryTable *table, SLRUEntry *entry) {
1,238,573,333✔
215
  SLRUEntry **ptr = taosLRUEntryTableFindPtr(table, entry->keyData, entry->keyLength, entry->hash);
1,238,573,333✔
216
  SLRUEntry  *old = *ptr;
1,238,717,922✔
217
  entry->nextHash = (old == NULL) ? NULL : old->nextHash;
1,238,632,613✔
218
  *ptr = entry;
1,238,671,199✔
219
  if (old == NULL) {
1,238,667,153✔
220
    ++table->elems;
1,164,046,475✔
221
    if ((table->elems >> table->lengthBits) > 0) {
1,164,052,615!
222
      taosLRUEntryTableResize(table);
×
223
    }
224
  }
225

226
  return old;
1,238,726,876✔
227
}
228

229
static SLRUEntry *taosLRUEntryTableRemove(SLRUEntryTable *table, const void *key, size_t keyLen, uint32_t hash) {
87,349,210✔
230
  SLRUEntry **entry = taosLRUEntryTableFindPtr(table, key, keyLen, hash);
87,349,210✔
231
  SLRUEntry  *result = *entry;
87,349,210✔
232
  if (result) {
87,349,210✔
233
    *entry = result->nextHash;
85,685,415✔
234
    --table->elems;
85,685,415✔
235
  }
236

237
  return result;
87,349,210✔
238
}
239

240
struct SLRUCacheShard {
241
  size_t         capacity;
242
  size_t         highPriPoolUsage;
243
  bool           strictCapacity;
244
  double         highPriPoolRatio;
245
  double         highPriPoolCapacity;
246
  SLRUEntry      lru;
247
  SLRUEntry     *lruLowPri;
248
  SLRUEntryTable table;
249
  size_t         usage;     // Memory size for entries residing in the cache.
250
  size_t         lruUsage;  // Memory size for entries residing only in the LRU list.
251
  TdThreadMutex  mutex;
252
};
253

254
#define TAOS_LRU_CACHE_SHARD_HASH32(key, len) (MurmurHash3_32((key), (len)))
255

256
static void taosLRUCacheShardMaintainPoolSize(SLRUCacheShard *shard) {
432,492,628✔
257
  while (shard->highPriPoolUsage > shard->highPriPoolCapacity) {
432,492,628!
258
    shard->lruLowPri = shard->lruLowPri->next;
×
259
    TAOS_LRU_ENTRY_SET_IN_HIGH_POOL(shard->lruLowPri, false);
×
260

261
    shard->highPriPoolUsage -= shard->lruLowPri->totalCharge;
×
262
  }
263
}
432,548,229✔
264

265
static void taosLRUCacheShardLRUInsert(SLRUCacheShard *shard, SLRUEntry *e) {
1,671,046,817✔
266
  if (shard->highPriPoolRatio > 0 && (TAOS_LRU_ENTRY_IS_HIGH_PRI(e) || TAOS_LRU_ENTRY_HAS_HIT(e))) {
1,671,046,817!
267
    e->next = &shard->lru;
432,579,982✔
268
    e->prev = shard->lru.prev;
432,580,009✔
269

270
    e->prev->next = e;
432,584,954✔
271
    e->next->prev = e;
432,575,871✔
272

273
    TAOS_LRU_ENTRY_SET_IN_HIGH_POOL(e, true);
432,538,209✔
274
    shard->highPriPoolUsage += e->totalCharge;
432,584,251✔
275
    taosLRUCacheShardMaintainPoolSize(shard);
432,602,714✔
276
  } else {
277
    e->next = shard->lruLowPri->next;
1,238,663,669✔
278
    e->prev = shard->lruLowPri;
1,238,679,512✔
279

280
    e->prev->next = e;
1,238,711,430✔
281
    e->next->prev = e;
1,238,607,841✔
282

283
    TAOS_LRU_ENTRY_SET_IN_HIGH_POOL(e, false);
1,238,543,818✔
284
    shard->lruLowPri = e;
1,238,500,397✔
285
  }
286

287
  shard->lruUsage += e->totalCharge;
1,671,047,035✔
288
}
1,671,049,688✔
289

290
static void taosLRUCacheShardLRURemove(SLRUCacheShard *shard, SLRUEntry *e) {
592,924,680✔
291
  if (shard->lruLowPri == e) {
592,924,680✔
292
    shard->lruLowPri = e->prev;
39,351,460✔
293
  }
294
  e->next->prev = e->prev;
593,037,090✔
295
  e->prev->next = e->next;
593,022,573✔
296
  e->prev = e->next = NULL;
592,962,025✔
297

298
  shard->lruUsage -= e->totalCharge;
592,950,194✔
299
  if (TAOS_LRU_ENTRY_IN_HIGH_POOL(e)) {
592,987,487✔
300
    shard->highPriPoolUsage -= e->totalCharge;
380,974,614✔
301
  }
302
}
593,031,778✔
303

304
static void taosLRUCacheShardEvictLRU(SLRUCacheShard *shard, size_t charge, SArray *deleted) {
1,464,455,991✔
305
  while (shard->usage + charge > shard->capacity && shard->lru.next != &shard->lru) {
1,464,455,991!
306
    SLRUEntry *old = shard->lru.next;
×
307

308
    taosLRUCacheShardLRURemove(shard, old);
×
309
    SLRUEntry *tentry = taosLRUEntryTableRemove(&shard->table, old->keyData, old->keyLength, old->hash);
×
310
    TAOS_LRU_ENTRY_SET_IN_CACHE(old, false);
×
311
    shard->usage -= old->totalCharge;
×
312

313
    if (!taosArrayPush(deleted, &old)) {
×
314
      // ignore this round's eviting
315
    }
316
  }
317
}
1,464,508,282✔
318

319
static void taosLRUCacheShardSetCapacity(SLRUCacheShard *shard, size_t capacity) {
1,464,605,097✔
320
  SArray *lastReferenceList = taosArrayInit(16, POINTER_BYTES);
1,464,605,097✔
321
  if (!lastReferenceList) {
1,464,729,475!
322
    return;
×
323
  }
324

325
  (void)taosThreadMutexLock(&shard->mutex);
1,464,729,475✔
326

327
  shard->capacity = capacity;
1,465,279,756✔
328
  shard->highPriPoolCapacity = capacity * shard->highPriPoolRatio;
1,465,250,250✔
329
  taosLRUCacheShardEvictLRU(shard, 0, lastReferenceList);
1,464,528,447✔
330

331
  (void)taosThreadMutexUnlock(&shard->mutex);
1,464,034,324✔
332

333
  for (int i = 0; i < taosArrayGetSize(lastReferenceList); ++i) {
1,465,246,026!
334
    SLRUEntry *entry = taosArrayGetP(lastReferenceList, i);
×
335
    taosLRUEntryFree(entry);
×
336
  }
337
  taosArrayDestroy(lastReferenceList);
1,464,949,876✔
338
}
339

340
static int taosLRUCacheShardInit(SLRUCacheShard *shard, size_t capacity, bool strict, double highPriPoolRatio,
1,463,818,780✔
341
                                 int maxUpperHashBits) {
342
  TAOS_CHECK_RETURN(taosLRUEntryTableInit(&shard->table, maxUpperHashBits));
1,463,818,780!
343

344
  (void)taosThreadMutexInit(&shard->mutex, NULL);
1,465,242,186✔
345

346
  (void)taosThreadMutexLock(&shard->mutex);
1,465,080,853✔
347
  shard->capacity = 0;
1,465,192,412✔
348
  shard->highPriPoolUsage = 0;
1,465,196,634✔
349
  shard->strictCapacity = strict;
1,464,863,343✔
350
  shard->highPriPoolRatio = highPriPoolRatio;
1,465,084,066✔
351
  shard->highPriPoolCapacity = 0;
1,465,030,589✔
352

353
  shard->usage = 0;
1,464,897,494✔
354
  shard->lruUsage = 0;
1,464,727,907✔
355

356
  shard->lru.next = &shard->lru;
1,465,148,843✔
357
  shard->lru.prev = &shard->lru;
1,464,842,830✔
358
  shard->lruLowPri = &shard->lru;
1,465,049,016✔
359
  (void)taosThreadMutexUnlock(&shard->mutex);
1,464,842,768✔
360

361
  taosLRUCacheShardSetCapacity(shard, capacity);
1,464,690,404✔
362

363
  TAOS_RETURN(TSDB_CODE_SUCCESS);
1,464,467,864✔
364
}
365

366
static void taosLRUCacheShardCleanup(SLRUCacheShard *shard) {
1,464,864,514✔
367
  (void)taosThreadMutexDestroy(&shard->mutex);
1,464,864,514✔
368

369
  taosLRUEntryTableCleanup(&shard->table);
1,465,066,623✔
370
}
1,465,154,755✔
371

372
static LRUStatus taosLRUCacheShardInsertEntry(SLRUCacheShard *shard, SLRUEntry *e, LRUHandle **handle,
1,238,601,733✔
373
                                              bool freeOnFail) {
374
  LRUStatus  status = TAOS_LRU_STATUS_OK;
1,238,601,733✔
375
  SLRUEntry *toFree = NULL;
1,238,601,733✔
376
  SArray    *lastReferenceList = NULL;
1,238,601,733✔
377
  if (shard->usage + e->totalCharge > shard->capacity) {
1,238,601,733!
378
    lastReferenceList = taosArrayInit(16, POINTER_BYTES);
×
379
    if (!lastReferenceList) {
×
380
      taosLRUEntryFree(e);
×
381
      return TAOS_LRU_STATUS_FAIL;
×
382
    }
383
  }
384

385
  (void)taosThreadMutexLock(&shard->mutex);
1,238,681,576✔
386

387
  if (shard->usage + e->totalCharge > shard->capacity && shard->lru.next != &shard->lru) {
1,238,712,049!
388
    if (!lastReferenceList) {
×
389
      lastReferenceList = taosArrayInit(16, POINTER_BYTES);
×
390
      if (!lastReferenceList) {
×
391
        taosLRUEntryFree(e);
×
392
        (void)taosThreadMutexUnlock(&shard->mutex);
×
393
        return TAOS_LRU_STATUS_FAIL;
×
394
      }
395
    }
396
    taosLRUCacheShardEvictLRU(shard, e->totalCharge, lastReferenceList);
×
397
  }
398

399
  if (shard->usage + e->totalCharge > shard->capacity && (shard->strictCapacity || handle == NULL)) {
1,238,743,023!
400
    TAOS_LRU_ENTRY_SET_IN_CACHE(e, false);
×
401
    if (handle == NULL) {
×
402
      toFree = e;
×
403
    } else {
404
      if (freeOnFail) {
×
405
        taosLRUEntryFree(e);
×
406

407
        *handle = NULL;
×
408
      }
409

410
      status = TAOS_LRU_STATUS_INCOMPLETE;
×
411
    }
412
  } else {
413
    SLRUEntry *old = taosLRUEntryTableInsert(&shard->table, e);
1,238,617,129✔
414
    shard->usage += e->totalCharge;
1,238,745,200✔
415
    if (old != NULL) {
1,238,732,199✔
416
      status = TAOS_LRU_STATUS_OK_OVERWRITTEN;
74,715,803✔
417

418
      if (old->overwriter) {
74,715,803!
419
        (*old->overwriter)(old->keyData, old->keyLength, old->value, old->ud);
74,716,806✔
420
      }
421

422
      TAOS_LRU_ENTRY_SET_IN_CACHE(old, false);
74,715,803✔
423
      if (!TAOS_LRU_ENTRY_HAS_REFS(old)) {
74,716,806✔
424
        taosLRUCacheShardLRURemove(shard, old);
41,730✔
425
        shard->usage -= old->totalCharge;
41,730✔
426

427
        toFree = old;
×
428
      }
429
    }
430
    if (handle == NULL) {
1,238,599,452!
431
      taosLRUCacheShardLRUInsert(shard, e);
1,238,599,452✔
432
    } else {
433
      if (!TAOS_LRU_ENTRY_HAS_REFS(e)) {
×
434
        TAOS_LRU_ENTRY_REF(e);
×
435
      }
436

437
      *handle = (LRUHandle *)e;
×
438
    }
439
  }
440

441
_exit:
1,238,456,008✔
442
  (void)taosThreadMutexUnlock(&shard->mutex);
1,238,456,008✔
443

444
  if (toFree) {
1,238,658,915✔
445
    taosLRUEntryFree(toFree);
41,730✔
446
  }
447

448
  for (int i = 0; i < taosArrayGetSize(lastReferenceList); ++i) {
1,238,687,814!
449
    SLRUEntry *entry = taosArrayGetP(lastReferenceList, i);
×
450

451
    taosLRUEntryFree(entry);
×
452
  }
453
  taosArrayDestroy(lastReferenceList);
1,238,523,200✔
454

455
  return status;
1,238,545,541✔
456
}
457

458
static LRUStatus taosLRUCacheShardInsert(SLRUCacheShard *shard, const void *key, size_t keyLen, uint32_t hash,
1,238,660,222✔
459
                                         void *value, size_t charge, _taos_lru_deleter_t deleter,
460
                                         _taos_lru_overwriter_t overwriter, LRUHandle **handle, LRUPriority priority,
461
                                         void *ud) {
462
  SLRUEntry *e = taosMemoryCalloc(1, sizeof(SLRUEntry) - 1 + keyLen);
1,238,660,222✔
463
  if (!e) {
1,238,557,998!
464
    if (deleter) {
×
465
      (*deleter)(key, keyLen, value, ud);
×
466
    }
467
    return TAOS_LRU_STATUS_FAIL;
×
468
  }
469

470
  e->value = value;
1,238,557,998✔
471
  e->flags = 0;
1,238,555,336✔
472
  e->deleter = deleter;
1,238,615,071✔
473
  e->overwriter = overwriter;
1,238,545,366✔
474
  e->ud = ud;
1,238,557,644✔
475
  e->keyLength = keyLen;
1,238,640,301✔
476
  e->hash = hash;
1,238,601,188✔
477
  e->refs = 0;
1,238,574,500✔
478
  e->next = e->prev = NULL;
1,238,589,856✔
479
  TAOS_LRU_ENTRY_SET_IN_CACHE(e, true);
1,238,589,663✔
480

481
  TAOS_LRU_ENTRY_SET_PRIORITY(e, priority);
1,238,602,880!
482
  memcpy(e->keyData, key, keyLen);
1,238,687,056!
483
  // TODO: e->CalcTotalCharge(charge, metadataChargePolicy);
484
  e->totalCharge = charge;
1,238,636,195✔
485

486
  return taosLRUCacheShardInsertEntry(shard, e, handle, true);
1,238,607,559✔
487
}
488

489
static LRUHandle *taosLRUCacheShardLookup(SLRUCacheShard *shard, const void *key, size_t keyLen, uint32_t hash) {
1,699,083,680✔
490
  SLRUEntry *e = NULL;
1,699,083,680✔
491

492
  (void)taosThreadMutexLock(&shard->mutex);
1,699,083,680✔
493
  e = taosLRUEntryTableLookup(&shard->table, key, keyLen, hash);
1,699,949,785✔
494
  if (e != NULL) {
1,700,049,562✔
495
    if (!TAOS_LRU_ENTRY_HAS_REFS(e)) {
507,907,313✔
496
      taosLRUCacheShardLRURemove(shard, e);
507,910,967✔
497
    }
498
    TAOS_LRU_ENTRY_REF(e);
507,850,553✔
499
    TAOS_LRU_ENTRY_SET_HIT(e);
507,848,663✔
500
  }
501

502
  (void)taosThreadMutexUnlock(&shard->mutex);
1,699,987,719✔
503

504
  return (LRUHandle *)e;
1,700,063,302✔
505
}
506

507
static void taosLRUCacheShardErase(SLRUCacheShard *shard, const void *key, size_t keyLen, uint32_t hash) {
1,666,336✔
508
  bool lastReference = false;
1,666,336✔
509
  (void)taosThreadMutexLock(&shard->mutex);
1,666,336✔
510

511
  SLRUEntry *e = taosLRUEntryTableRemove(&shard->table, key, keyLen, hash);
1,666,336✔
512
  if (e != NULL) {
1,666,336✔
513
    TAOS_LRU_ENTRY_SET_IN_CACHE(e, false);
2,541✔
514
    if (!TAOS_LRU_ENTRY_HAS_REFS(e)) {
2,541!
515
      taosLRUCacheShardLRURemove(shard, e);
2,541✔
516

517
      shard->usage -= e->totalCharge;
2,541✔
518
      lastReference = true;
2,541✔
519
    }
520
  }
521

522
  (void)taosThreadMutexUnlock(&shard->mutex);
1,666,336✔
523

524
  if (lastReference) {
1,666,336✔
525
    taosLRUEntryFree(e);
2,541✔
526
  }
527
}
1,666,336✔
528

529
static int taosLRUCacheShardApply(SLRUCacheShard *shard, _taos_lru_functor_t functor, void *ud) {
3,001,243✔
530
  int ret;
531

532
  (void)taosThreadMutexLock(&shard->mutex);
3,001,243✔
533

534
  ret = taosLRUEntryTableApplyF(&shard->table, functor, ud);
3,004,553✔
535

536
  (void)taosThreadMutexUnlock(&shard->mutex);
3,004,553✔
537

538
  return ret;
3,004,553✔
539
}
540

541
static void taosLRUCacheShardEraseUnrefEntries(SLRUCacheShard *shard) {
238,125,333✔
542
  SArray *lastReferenceList = taosArrayInit(16, POINTER_BYTES);
238,125,333✔
543

544
  (void)taosThreadMutexLock(&shard->mutex);
238,122,235✔
545

546
  while (shard->lru.next != &shard->lru) {
323,240,196✔
547
    SLRUEntry *old = shard->lru.next;
85,113,466✔
548
    taosLRUCacheShardLRURemove(shard, old);
85,113,466✔
549
    SLRUEntry *tentry = taosLRUEntryTableRemove(&shard->table, old->keyData, old->keyLength, old->hash);
85,113,466✔
550
    TAOS_LRU_ENTRY_SET_IN_CACHE(old, false);
85,113,466✔
551
    shard->usage -= old->totalCharge;
85,113,466✔
552

553
    if (!taosArrayPush(lastReferenceList, &old)) {
85,113,466!
554
      taosLRUEntryFree(old);
×
555
      return;
×
556
    }
557
  }
558

559
  (void)taosThreadMutexUnlock(&shard->mutex);
238,122,685✔
560

561
  for (int i = 0; i < taosArrayGetSize(lastReferenceList); ++i) {
323,237,963✔
562
    SLRUEntry *entry = taosArrayGetP(lastReferenceList, i);
85,113,466✔
563

564
    taosLRUEntryFree(entry);
85,113,466✔
565
  }
566

567
  taosArrayDestroy(lastReferenceList);
238,120,499✔
568
}
569

570
static bool taosLRUCacheShardRef(SLRUCacheShard *shard, LRUHandle *handle) {
×
571
  SLRUEntry *e = (SLRUEntry *)handle;
×
572
  (void)taosThreadMutexLock(&shard->mutex);
×
573

574
  TAOS_LRU_ENTRY_REF(e);
×
575

576
  (void)taosThreadMutexUnlock(&shard->mutex);
×
577

578
  return true;
×
579
}
580

581
static bool taosLRUCacheShardRelease(SLRUCacheShard *shard, LRUHandle *handle, bool eraseIfLastRef) {
507,827,434✔
582
  if (handle == NULL) {
507,827,434!
583
    return false;
×
584
  }
585

586
  SLRUEntry *e = (SLRUEntry *)handle;
507,827,434✔
587
  bool       lastReference = false;
507,827,434✔
588

589
  (void)taosThreadMutexLock(&shard->mutex);
507,827,434✔
590

591
  lastReference = taosLRUEntryUnref(e);
507,926,469✔
592
  if (lastReference && TAOS_LRU_ENTRY_IN_CACHE(e)) {
507,827,503!
593
    if (shard->usage > shard->capacity || eraseIfLastRef) {
433,244,708✔
594
      SLRUEntry *tentry = taosLRUEntryTableRemove(&shard->table, e->keyData, e->keyLength, e->hash);
614,251✔
595
      TAOS_LRU_ENTRY_SET_IN_CACHE(e, false);
569,408✔
596
    } else {
597
      taosLRUCacheShardLRUInsert(shard, e);
432,530,219✔
598

599
      lastReference = false;
432,484,511✔
600
    }
601
  }
602

603
  if (lastReference && e->value) {
507,571,430!
604
    shard->usage -= e->totalCharge;
75,245,487✔
605
  }
606

607
  (void)taosThreadMutexUnlock(&shard->mutex);
507,571,430✔
608

609
  if (lastReference) {
507,837,635✔
610
    taosLRUEntryFree(e);
75,245,487✔
611
  }
612

613
  return lastReference;
507,873,368✔
614
}
615

616
static size_t taosLRUCacheShardGetUsage(SLRUCacheShard *shard) {
415,698,035✔
617
  size_t usage = 0;
415,698,035✔
618

619
  (void)taosThreadMutexLock(&shard->mutex);
415,698,035✔
620
  usage = shard->usage;
415,698,035✔
621
  (void)taosThreadMutexUnlock(&shard->mutex);
415,698,035✔
622

623
  return usage;
415,698,035✔
624
}
625

626
static int32_t taosLRUCacheShardGetElems(SLRUCacheShard *shard) {
415,698,035✔
627
  int32_t elems = 0;
415,698,035✔
628

629
  (void)taosThreadMutexLock(&shard->mutex);
415,698,035✔
630
  elems = shard->table.elems;
415,698,035✔
631
  (void)taosThreadMutexUnlock(&shard->mutex);
415,698,035✔
632

633
  return elems;
415,698,035✔
634
}
635

636
static size_t taosLRUCacheShardGetPinnedUsage(SLRUCacheShard *shard) {
×
637
  size_t usage = 0;
×
638

639
  (void)taosThreadMutexLock(&shard->mutex);
×
640

641
  usage = shard->usage - shard->lruUsage;
×
642

643
  (void)taosThreadMutexUnlock(&shard->mutex);
×
644

645
  return usage;
×
646
}
647

648
static void taosLRUCacheShardSetStrictCapacity(SLRUCacheShard *shard, bool strict) {
932,271,734✔
649
  (void)taosThreadMutexLock(&shard->mutex);
932,271,734✔
650

651
  shard->strictCapacity = strict;
933,000,173✔
652

653
  (void)taosThreadMutexUnlock(&shard->mutex);
932,927,717✔
654
}
932,361,438✔
655

656
struct SShardedCache {
657
  uint32_t      shardMask;
658
  TdThreadMutex capacityMutex;
659
  size_t        capacity;
660
  bool          strictCapacity;
661
  uint64_t      lastId;  // atomic var for last id
662
};
663

664
struct SLRUCache {
665
  SShardedCache   shardedCache;
666
  SLRUCacheShard *shards;
667
  int             numShards;
668
};
669

670
static int getDefaultCacheShardBits(size_t capacity) {
891,721,317✔
671
  int    numShardBits = 0;
891,721,317✔
672
  size_t minShardSize = 512 * 1024;
891,721,317✔
673
  size_t numShards = capacity / minShardSize;
891,721,317!
674
  while (numShards >>= 1) {
1,089,356,055✔
675
    if (++numShardBits >= 6) {
197,634,738!
676
      return numShardBits;
×
677
    }
678
  }
679

680
  return numShardBits;
891,721,317✔
681
}
682

683
SLRUCache *taosLRUCacheInit(size_t capacity, int numShardBits, double highPriPoolRatio) {
918,594,863✔
684
  if (numShardBits >= 20) {
918,594,863!
685
    return NULL;
×
686
  }
687
  if (highPriPoolRatio < 0.0 || highPriPoolRatio > 1.0) {
918,594,863!
688
    return NULL;
2,119,849✔
689
  }
690
  SLRUCache *cache = taosMemoryCalloc(1, sizeof(SLRUCache));
916,475,014✔
691
  if (!cache) {
916,999,937!
692
    return NULL;
×
693
  }
694

695
  if (numShardBits < 0) {
916,999,937✔
696
    numShardBits = getDefaultCacheShardBits(capacity);
890,609,482✔
697
  }
698

699
  int numShards = 1 << numShardBits;
917,765,975!
700
  cache->shards = taosMemoryCalloc(numShards, sizeof(SLRUCacheShard));
917,765,975✔
701
  if (!cache->shards) {
916,677,077!
702
    taosMemoryFree(cache);
×
703
    return NULL;
×
704
  }
705

706
  bool   strictCapacity = 1;
918,134,978✔
707
  size_t perShard = (capacity + (numShards - 1)) / numShards;
918,134,978!
708
  for (int i = 0; i < numShards; ++i) {
2,147,483,647✔
709
    if (TSDB_CODE_SUCCESS !=
1,464,385,765!
710
        taosLRUCacheShardInit(&cache->shards[i], perShard, strictCapacity, highPriPoolRatio, 32 - numShardBits)) {
1,463,574,243✔
711
      taosMemoryFree(cache->shards);
×
712
      taosMemoryFree(cache);
×
713
      return NULL;
×
714
    }
715
  }
716

717
  cache->numShards = numShards;
918,946,500✔
718

719
  cache->shardedCache.shardMask = (1 << numShardBits) - 1;
919,187,308!
720
  cache->shardedCache.strictCapacity = strictCapacity;
919,181,766✔
721
  cache->shardedCache.capacity = capacity;
918,910,581✔
722
  cache->shardedCache.lastId = 1;
919,069,369✔
723

724
  (void)taosThreadMutexInit(&cache->shardedCache.capacityMutex, NULL);
919,010,429✔
725

726
  return cache;
919,526,781✔
727
}
728

729
void taosLRUCacheCleanup(SLRUCache *cache) {
921,738,071✔
730
  if (cache) {
921,738,071✔
731
    if (cache->shards) {
919,715,584✔
732
      int numShards = cache->numShards;
919,811,004✔
733
      for (int i = 0; i < numShards; ++i) {
2,147,483,647✔
734
        taosLRUCacheShardCleanup(&cache->shards[i]);
1,464,900,778✔
735
      }
736
      taosMemoryFree(cache->shards);
919,884,941✔
737
      cache->shards = 0;
919,876,805✔
738
    }
739

740
    (void)taosThreadMutexDestroy(&cache->shardedCache.capacityMutex);
919,884,140✔
741

742
    taosMemoryFree(cache);
919,873,279✔
743
  }
744
}
921,897,090✔
745

746
LRUStatus taosLRUCacheInsert(SLRUCache *cache, const void *key, size_t keyLen, void *value, size_t charge,
1,238,654,135✔
747
                             _taos_lru_deleter_t deleter, _taos_lru_overwriter_t overwriter, LRUHandle **handle,
748
                             LRUPriority priority, void *ud) {
749
  uint32_t hash = TAOS_LRU_CACHE_SHARD_HASH32(key, keyLen);
1,238,654,135✔
750
  uint32_t shardIndex = hash & cache->shardedCache.shardMask;
1,238,723,360✔
751

752
  return taosLRUCacheShardInsert(&cache->shards[shardIndex], key, keyLen, hash, value, charge, deleter, overwriter,
1,238,732,518✔
753
                                 handle, priority, ud);
754
}
755

756
LRUHandle *taosLRUCacheLookup(SLRUCache *cache, const void *key, size_t keyLen) {
1,698,850,245✔
757
  uint32_t hash = TAOS_LRU_CACHE_SHARD_HASH32(key, keyLen);
1,698,850,245✔
758
  uint32_t shardIndex = hash & cache->shardedCache.shardMask;
1,699,586,299✔
759

760
  return taosLRUCacheShardLookup(&cache->shards[shardIndex], key, keyLen, hash);
1,699,777,543✔
761
}
762

763
void taosLRUCacheErase(SLRUCache *cache, const void *key, size_t keyLen) {
1,666,336✔
764
  uint32_t hash = TAOS_LRU_CACHE_SHARD_HASH32(key, keyLen);
1,666,336✔
765
  uint32_t shardIndex = hash & cache->shardedCache.shardMask;
1,666,336✔
766

767
  return taosLRUCacheShardErase(&cache->shards[shardIndex], key, keyLen, hash);
1,666,336✔
768
}
769

770
void taosLRUCacheApply(SLRUCache *cache, _taos_lru_functor_t functor, void *ud) {
3,002,751✔
771
  int numShards = cache->numShards;
3,002,751✔
772
  for (int i = 0; i < numShards; ++i) {
6,003,128✔
773
    if (taosLRUCacheShardApply(&cache->shards[i], functor, ud)) {
2,999,762!
774
      break;
×
775
    }
776
  }
777
}
3,003,366✔
778

779
void taosLRUCacheEraseUnrefEntries(SLRUCache *cache) {
38,743,734✔
780
  int numShards = cache->numShards;
38,743,734✔
781
  for (int i = 0; i < numShards; ++i) {
276,864,587✔
782
    taosLRUCacheShardEraseUnrefEntries(&cache->shards[i]);
238,121,084✔
783
  }
784
}
38,743,503✔
785

786
bool taosLRUCacheRef(SLRUCache *cache, LRUHandle *handle) {
×
787
  if (handle == NULL) {
×
788
    return false;
×
789
  }
790

791
  uint32_t hash = ((SLRUEntry *)handle)->hash;
×
792
  uint32_t shardIndex = hash & cache->shardedCache.shardMask;
×
793

794
  return taosLRUCacheShardRef(&cache->shards[shardIndex], handle);
×
795
}
796

797
bool taosLRUCacheRelease(SLRUCache *cache, LRUHandle *handle, bool eraseIfLastRef) {
507,892,211✔
798
  if (handle == NULL) {
507,892,211!
799
    return false;
×
800
  }
801

802
  uint32_t hash = ((SLRUEntry *)handle)->hash;
507,892,211✔
803
  uint32_t shardIndex = hash & cache->shardedCache.shardMask;
507,904,250✔
804

805
  return taosLRUCacheShardRelease(&cache->shards[shardIndex], handle, eraseIfLastRef);
507,874,044✔
806
}
807

808
void *taosLRUCacheValue(SLRUCache *cache, LRUHandle *handle) { return ((SLRUEntry *)handle)->value; }
507,188,395✔
809

810
void taosLRUCacheUpdate(SLRUCache *cache, LRUHandle *handle, void *value) { ((SLRUEntry *)handle)->value = value; }
×
811

812
size_t taosLRUCacheGetUsage(SLRUCache *cache) {
415,698,035✔
813
  size_t usage = 0;
415,698,035✔
814

815
  for (int i = 0; i < cache->numShards; ++i) {
831,396,070✔
816
    usage += taosLRUCacheShardGetUsage(&cache->shards[i]);
415,698,035✔
817
  }
818

819
  return usage;
415,698,035✔
820
}
821

822
int32_t taosLRUCacheGetElems(SLRUCache *cache) {
415,698,035✔
823
  int32_t elems = 0;
415,698,035✔
824

825
  for (int i = 0; i < cache->numShards; ++i) {
831,396,070✔
826
    elems += taosLRUCacheShardGetElems(&cache->shards[i]);
415,698,035✔
827
  }
828

829
  return elems;
415,698,035✔
830
}
831

832
size_t taosLRUCacheGetPinnedUsage(SLRUCache *cache) {
×
833
  size_t usage = 0;
×
834

835
  for (int i = 0; i < cache->numShards; ++i) {
×
836
    usage += taosLRUCacheShardGetPinnedUsage(&cache->shards[i]);
×
837
  }
838

839
  return usage;
×
840
}
841

842
void taosLRUCacheSetCapacity(SLRUCache *cache, size_t capacity) {
53,013✔
843
  uint32_t numShards = cache->numShards;
53,013✔
844
  size_t   perShard = (capacity + (numShards - 1)) / numShards;
53,013!
845

846
  (void)taosThreadMutexLock(&cache->shardedCache.capacityMutex);
53,013✔
847

848
  for (int i = 0; i < numShards; ++i) {
106,026✔
849
    taosLRUCacheShardSetCapacity(&cache->shards[i], perShard);
53,013✔
850
  }
851

852
  cache->shardedCache.capacity = capacity;
53,013✔
853

854
  (void)taosThreadMutexUnlock(&cache->shardedCache.capacityMutex);
53,013✔
855
}
53,013✔
856

857
size_t taosLRUCacheGetCapacity(SLRUCache *cache) {
×
858
  size_t capacity = 0;
×
859

860
  (void)taosThreadMutexLock(&cache->shardedCache.capacityMutex);
×
861

862
  capacity = cache->shardedCache.capacity;
×
863

864
  (void)taosThreadMutexUnlock(&cache->shardedCache.capacityMutex);
×
865

866
  return capacity;
×
867
}
868

869
void taosLRUCacheSetStrictCapacity(SLRUCache *cache, bool strict) {
732,939,930✔
870
  uint32_t numShards = cache->numShards;
732,939,930✔
871

872
  (void)taosThreadMutexLock(&cache->shardedCache.capacityMutex);
733,243,499✔
873

874
  for (int i = 0; i < numShards; ++i) {
1,665,355,659✔
875
    taosLRUCacheShardSetStrictCapacity(&cache->shards[i], strict);
932,264,358✔
876
  }
877

878
  cache->shardedCache.strictCapacity = strict;
733,091,301✔
879

880
  (void)taosThreadMutexUnlock(&cache->shardedCache.capacityMutex);
733,105,679✔
881
}
733,460,939✔
882

883
bool taosLRUCacheIsStrictCapacity(SLRUCache *cache) {
×
884
  bool strict = false;
×
885

886
  (void)taosThreadMutexLock(&cache->shardedCache.capacityMutex);
×
887

888
  strict = cache->shardedCache.strictCapacity;
×
889

890
  (void)taosThreadMutexUnlock(&cache->shardedCache.capacityMutex);
×
891

892
  return strict;
×
893
}
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