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

taosdata / TDengine / #4975

05 Mar 2026 08:43AM UTC coverage: 68.37% (+0.7%) from 67.664%
#4975

push

travis-ci

web-flow
merge: from main to 3.0 branch #34682

250 of 345 new or added lines in 28 files covered. (72.46%)

446 existing lines in 120 files now uncovered.

210600 of 308032 relevant lines covered (68.37%)

130326818.69 hits per line

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

79.14
/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) {
62,979,652✔
91
  --entry->refs;
62,979,652✔
92
  return entry->refs == 0;
62,980,386✔
93
}
94

95
static void taosLRUEntryFree(SLRUEntry *entry) {
107,585,676✔
96
  if (entry->deleter) {
107,585,676✔
97
    (*entry->deleter)(entry->keyData, entry->keyLength, entry->value, entry->ud);
107,587,944✔
98
  }
99

100
  taosMemoryFree(entry);
107,571,601✔
101
}
107,580,354✔
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) {
218,242,720✔
113
  table->lengthBits = 16;
218,242,720✔
114
  table->list = taosMemoryCalloc(1 << table->lengthBits, sizeof(SLRUEntry *));
218,246,159✔
115
  if (!table->list) {
218,255,899✔
116
    TAOS_RETURN(terrno);
×
117
  }
118

119
  table->elems = 0;
218,255,629✔
120
  table->maxLengthBits = maxUpperHashBits;
218,255,629✔
121

122
  TAOS_RETURN(TSDB_CODE_SUCCESS);
218,255,629✔
123
}
124

125
static void taosLRUEntryTableApply(SLRUEntryTable *table, _taos_lru_table_func_t func, uint32_t begin, uint32_t end) {
218,229,169✔
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;
6,582✔
130
      func(h);
6,582✔
131
      h = n;
2,147,483,647✔
132
    }
133
  }
134
}
3,529,262✔
135

136
static void taosLRUEntryTableFree(SLRUEntry *entry) {
6,582✔
137
  if (!TAOS_LRU_ENTRY_HAS_REFS(entry)) {
6,582✔
138
    taosLRUEntryFree(entry);
6,582✔
139
  }
140
}
6,582✔
141

142
static void taosLRUEntryTableCleanup(SLRUEntryTable *table) {
218,233,322✔
143
  taosLRUEntryTableApply(table, taosLRUEntryTableFree, 0, 1 << table->lengthBits);
218,233,322✔
144

145
  taosMemoryFree(table->list);
218,222,195✔
146
}
218,232,521✔
147

148
static int taosLRUEntryTableApplyF(SLRUEntryTable *table, _taos_lru_functor_t functor, void *ud) {
1,011,685✔
149
  int      ret = 0;
1,011,685✔
150
  uint32_t end = 1 << table->lengthBits;
1,011,685✔
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;
55,963,032✔
155
      ret = functor(h->keyData, h->keyLength, h->value, ud);
55,964,499✔
156
      if (ret) {
55,922,226✔
157
        return ret;
×
158
      }
159
      h = n;
55,922,226✔
160
    }
161
  }
162

UNCOV
163
  return ret;
×
164
}
165

166
static SLRUEntry **taosLRUEntryTableFindPtr(SLRUEntryTable *table, const void *key, size_t keyLen, uint32_t hash) {
260,598,125✔
167
  SLRUEntry **entry = &table->list[hash >> (32 - table->lengthBits)];
260,598,125✔
168
  while (*entry && ((*entry)->hash != hash || memcmp(key, (*entry)->keyData, keyLen) != 0)) {
268,490,556✔
169
    entry = &(*entry)->nextHash;
7,853,029✔
170
  }
171

172
  return entry;
260,628,818✔
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) {
93,038,009✔
211
  return *taosLRUEntryTableFindPtr(table, key, keyLen, hash);
93,038,009✔
212
}
213

214
static SLRUEntry *taosLRUEntryTableInsert(SLRUEntryTable *table, SLRUEntry *entry) {
107,537,428✔
215
  SLRUEntry **ptr = taosLRUEntryTableFindPtr(table, entry->keyData, entry->keyLength, entry->hash);
107,537,428✔
216
  SLRUEntry  *old = *ptr;
107,582,173✔
217
  entry->nextHash = (old == NULL) ? NULL : old->nextHash;
107,581,048✔
218
  *ptr = entry;
107,581,043✔
219
  if (old == NULL) {
107,580,390✔
220
    ++table->elems;
59,917,676✔
221
    if ((table->elems >> table->lengthBits) > 0) {
59,917,296✔
222
      taosLRUEntryTableResize(table);
×
223
    }
224
  }
225

226
  return old;
107,581,196✔
227
}
228

229
static SLRUEntry *taosLRUEntryTableRemove(SLRUEntryTable *table, const void *key, size_t keyLen, uint32_t hash) {
60,020,233✔
230
  SLRUEntry **entry = taosLRUEntryTableFindPtr(table, key, keyLen, hash);
60,020,233✔
231
  SLRUEntry  *result = *entry;
60,024,109✔
232
  if (result) {
60,024,755✔
233
    *entry = result->nextHash;
59,888,375✔
234
    --table->elems;
59,889,667✔
235
  }
236

237
  return result;
60,028,631✔
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) {
5,697,926✔
257
  while (shard->highPriPoolUsage > shard->highPriPoolCapacity) {
6,145,532✔
258
    shard->lruLowPri = shard->lruLowPri->next;
447,606✔
259
    TAOS_LRU_ENTRY_SET_IN_HIGH_POOL(shard->lruLowPri, false);
447,606✔
260

261
    shard->highPriPoolUsage -= shard->lruLowPri->totalCharge;
447,606✔
262
  }
263
}
5,698,572✔
264

265
static void taosLRUCacheShardLRUInsert(SLRUCacheShard *shard, SLRUEntry *e) {
113,278,358✔
266
  if (shard->highPriPoolRatio > 0 && (TAOS_LRU_ENTRY_IS_HIGH_PRI(e) || TAOS_LRU_ENTRY_HAS_HIT(e))) {
113,278,358✔
267
    e->next = &shard->lru;
5,697,070✔
268
    e->prev = shard->lru.prev;
5,698,341✔
269

270
    e->prev->next = e;
5,698,987✔
271
    e->next->prev = e;
5,698,341✔
272

273
    TAOS_LRU_ENTRY_SET_IN_HIGH_POOL(e, true);
5,698,341✔
274
    shard->highPriPoolUsage += e->totalCharge;
5,698,341✔
275
    taosLRUCacheShardMaintainPoolSize(shard);
5,698,341✔
276
  } else {
277
    e->next = shard->lruLowPri->next;
107,577,746✔
278
    e->prev = shard->lruLowPri;
107,580,136✔
279

280
    e->prev->next = e;
107,588,260✔
281
    e->next->prev = e;
107,587,617✔
282

283
    TAOS_LRU_ENTRY_SET_IN_HIGH_POOL(e, false);
107,580,362✔
284
    shard->lruLowPri = e;
107,582,357✔
285
  }
286

287
  shard->lruUsage += e->totalCharge;
113,284,754✔
288
}
113,287,834✔
289

290
static void taosLRUCacheShardLRURemove(SLRUCacheShard *shard, SLRUEntry *e) {
113,260,952✔
291
  if (shard->lruLowPri == e) {
113,260,952✔
292
    shard->lruLowPri = e->prev;
131,311✔
293
  }
294
  e->next->prev = e->prev;
113,262,570✔
295
  e->prev->next = e->next;
113,264,838✔
296
  e->prev = e->next = NULL;
113,198,136✔
297

298
  shard->lruUsage -= e->totalCharge;
113,200,401✔
299
  if (TAOS_LRU_ENTRY_IN_HIGH_POOL(e)) {
113,203,557✔
300
    shard->highPriPoolUsage -= e->totalCharge;
5,246,624✔
301
  }
302
}
113,211,325✔
303

304
static void taosLRUCacheShardEvictLRU(SLRUCacheShard *shard, size_t charge, SArray *deleted) {
222,797,703✔
305
  while (shard->usage + charge > shard->capacity && shard->lru.next != &shard->lru) {
227,337,915✔
306
    SLRUEntry *old = shard->lru.next;
4,540,212✔
307

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

313
    if (!taosArrayPush(deleted, &old)) {
4,540,212✔
314
      // ignore this round's eviting
315
    }
316
  }
317
}
222,799,668✔
318

319
static void taosLRUCacheShardSetCapacity(SLRUCacheShard *shard, size_t capacity) {
218,257,671✔
320
  SArray *lastReferenceList = taosArrayInit(16, POINTER_BYTES);
218,257,671✔
321
  if (!lastReferenceList) {
218,243,851✔
322
    return;
×
323
  }
324

325
  (void)taosThreadMutexLock(&shard->mutex);
218,243,851✔
326

327
  shard->capacity = capacity;
218,258,185✔
328
  shard->highPriPoolCapacity = capacity * shard->highPriPoolRatio;
218,258,371✔
329
  taosLRUCacheShardEvictLRU(shard, 0, lastReferenceList);
218,259,068✔
330

331
  (void)taosThreadMutexUnlock(&shard->mutex);
218,254,688✔
332

333
  for (int i = 0; i < taosArrayGetSize(lastReferenceList); ++i) {
218,257,816✔
334
    SLRUEntry *entry = taosArrayGetP(lastReferenceList, i);
×
335
    taosLRUEntryFree(entry);
×
336
  }
337
  taosArrayDestroy(lastReferenceList);
218,256,085✔
338
}
339

340
static int taosLRUCacheShardInit(SLRUCacheShard *shard, size_t capacity, bool strict, double highPriPoolRatio,
218,242,529✔
341
                                 int maxUpperHashBits) {
342
  TAOS_CHECK_RETURN(taosLRUEntryTableInit(&shard->table, maxUpperHashBits));
218,242,529✔
343

344
  (void)taosThreadMutexInit(&shard->mutex, NULL);
218,251,769✔
345

346
  (void)taosThreadMutexLock(&shard->mutex);
218,253,997✔
347
  shard->capacity = 0;
218,254,816✔
348
  shard->highPriPoolUsage = 0;
218,254,816✔
349
  shard->strictCapacity = strict;
218,254,400✔
350
  shard->highPriPoolRatio = highPriPoolRatio;
218,254,403✔
351
  shard->highPriPoolCapacity = 0;
218,252,424✔
352

353
  shard->usage = 0;
218,253,735✔
354
  shard->lruUsage = 0;
218,251,716✔
355

356
  shard->lru.next = &shard->lru;
218,252,213✔
357
  shard->lru.prev = &shard->lru;
218,252,336✔
358
  shard->lruLowPri = &shard->lru;
218,251,253✔
359
  (void)taosThreadMutexUnlock(&shard->mutex);
218,253,261✔
360

361
  taosLRUCacheShardSetCapacity(shard, capacity);
218,252,648✔
362

363
  TAOS_RETURN(TSDB_CODE_SUCCESS);
218,247,438✔
364
}
365

366
static void taosLRUCacheShardCleanup(SLRUCacheShard *shard) {
218,230,827✔
367
  (void)taosThreadMutexDestroy(&shard->mutex);
218,230,827✔
368

369
  taosLRUEntryTableCleanup(&shard->table);
218,233,429✔
370
}
218,232,720✔
371

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

385
  (void)taosThreadMutexLock(&shard->mutex);
107,592,949✔
386

387
  if (shard->usage + e->totalCharge > shard->capacity && shard->lru.next != &shard->lru) {
107,596,108✔
388
    if (!lastReferenceList) {
4,540,212✔
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);
4,540,212✔
397
  }
398

399
  if (shard->usage + e->totalCharge > shard->capacity && (shard->strictCapacity || handle == NULL)) {
107,596,164✔
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);
107,594,873✔
414
    shard->usage += e->totalCharge;
107,579,346✔
415
    if (old != NULL) {
107,579,012✔
416
      status = TAOS_LRU_STATUS_OK_OVERWRITTEN;
47,663,729✔
417

418
      if (old->overwriter) {
47,663,729✔
419
        (*old->overwriter)(old->keyData, old->keyLength, old->value, old->ud);
47,664,377✔
420
      }
421

422
      TAOS_LRU_ENTRY_SET_IN_CACHE(old, false);
47,664,071✔
423
      if (!TAOS_LRU_ENTRY_HAS_REFS(old)) {
47,665,372✔
424
        taosLRUCacheShardLRURemove(shard, old);
548✔
425
        shard->usage -= old->totalCharge;
548✔
426

427
        toFree = old;
553✔
428
      }
429
    }
430
    if (handle == NULL) {
107,580,336✔
431
      taosLRUCacheShardLRUInsert(shard, e);
98,113,542✔
432
    } else {
433
      if (!TAOS_LRU_ENTRY_HAS_REFS(e)) {
9,466,794✔
434
        TAOS_LRU_ENTRY_REF(e);
9,466,794✔
435
      }
436

437
      *handle = (LRUHandle *)e;
9,466,794✔
438
    }
439
  }
440

441
_exit:
107,587,615✔
442
  (void)taosThreadMutexUnlock(&shard->mutex);
107,587,615✔
443

444
  if (toFree) {
107,592,727✔
445
    taosLRUEntryFree(toFree);
548✔
446
  }
447

448
  for (int i = 0; i < taosArrayGetSize(lastReferenceList); ++i) {
112,133,000✔
449
    SLRUEntry *entry = taosArrayGetP(lastReferenceList, i);
4,540,212✔
450

451
    taosLRUEntryFree(entry);
4,540,212✔
452
  }
453
  taosArrayDestroy(lastReferenceList);
107,591,903✔
454

455
  return status;
107,588,913✔
456
}
457

458
static LRUStatus taosLRUCacheShardInsert(SLRUCacheShard *shard, const void *key, size_t keyLen, uint32_t hash,
107,592,669✔
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);
107,592,669✔
463
  if (!e) {
107,561,900✔
464
    if (deleter) {
×
465
      (*deleter)(key, keyLen, value, ud);
×
466
    }
467
    return TAOS_LRU_STATUS_FAIL;
×
468
  }
469

470
  e->value = value;
107,561,900✔
471
  e->flags = 0;
107,562,285✔
472
  e->deleter = deleter;
107,563,581✔
473
  e->overwriter = overwriter;
107,564,225✔
474
  e->ud = ud;
107,567,463✔
475
  e->keyLength = keyLen;
107,567,134✔
476
  e->hash = hash;
107,568,764✔
477
  e->refs = 0;
107,571,246✔
478
  e->next = e->prev = NULL;
107,575,958✔
479
  TAOS_LRU_ENTRY_SET_IN_CACHE(e, true);
107,586,477✔
480

481
  TAOS_LRU_ENTRY_SET_PRIORITY(e, priority);
107,585,500✔
482
  memcpy(e->keyData, key, keyLen);
107,587,235✔
483
  // TODO: e->CalcTotalCharge(charge, metadataChargePolicy);
484
  e->totalCharge = charge;
107,580,487✔
485

486
  return taosLRUCacheShardInsertEntry(shard, e, handle, true);
107,591,837✔
487
}
488

489
static LRUHandle *taosLRUCacheShardLookup(SLRUCacheShard *shard, const void *key, size_t keyLen, uint32_t hash) {
93,036,088✔
490
  SLRUEntry *e = NULL;
93,036,088✔
491

492
  (void)taosThreadMutexLock(&shard->mutex);
93,036,088✔
493
  e = taosLRUEntryTableLookup(&shard->table, key, keyLen, hash);
93,038,031✔
494
  if (e != NULL) {
93,030,723✔
495
    if (!TAOS_LRU_ENTRY_HAS_REFS(e)) {
53,509,572✔
496
      taosLRUCacheShardLRURemove(shard, e);
53,504,088✔
497
    }
498
    TAOS_LRU_ENTRY_REF(e);
53,499,131✔
499
    TAOS_LRU_ENTRY_SET_HIT(e);
53,502,695✔
500
  }
501

502
  (void)taosThreadMutexUnlock(&shard->mutex);
93,025,640✔
503

504
  return (LRUHandle *)e;
93,036,833✔
505
}
506

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

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

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

522
  (void)taosThreadMutexUnlock(&shard->mutex);
139,588✔
523

524
  if (lastReference) {
139,588✔
525
    taosLRUEntryFree(e);
1,916✔
526
  }
527
}
139,588✔
528

529
static int taosLRUCacheShardApply(SLRUCacheShard *shard, _taos_lru_functor_t functor, void *ud) {
1,012,492✔
530
  int ret;
531

532
  (void)taosThreadMutexLock(&shard->mutex);
1,012,492✔
533

534
  ret = taosLRUEntryTableApplyF(&shard->table, functor, ud);
1,013,071✔
535

536
  (void)taosThreadMutexUnlock(&shard->mutex);
1,013,748✔
537

538
  return ret;
1,013,748✔
539
}
540

541
static void taosLRUCacheShardEraseUnrefEntries(SLRUCacheShard *shard) {
78,363,971✔
542
  SArray *lastReferenceList = taosArrayInit(16, POINTER_BYTES);
78,363,971✔
543

544
  (void)taosThreadMutexLock(&shard->mutex);
78,362,138✔
545

546
  while (shard->lru.next != &shard->lru) {
133,585,104✔
547
    SLRUEntry *old = shard->lru.next;
55,226,663✔
548
    taosLRUCacheShardLRURemove(shard, old);
55,226,017✔
549
    SLRUEntry *tentry = taosLRUEntryTableRemove(&shard->table, old->keyData, old->keyLength, old->hash);
55,193,071✔
550
    TAOS_LRU_ENTRY_SET_IN_CACHE(old, false);
55,209,867✔
551
    shard->usage -= old->totalCharge;
55,210,513✔
552

553
    if (!taosArrayPush(lastReferenceList, &old)) {
55,220,203✔
554
      taosLRUEntryFree(old);
×
555
      return;
×
556
    }
557
  }
558

559
  (void)taosThreadMutexUnlock(&shard->mutex);
78,364,074✔
560

561
  for (int i = 0; i < taosArrayGetSize(lastReferenceList); ++i) {
133,601,484✔
562
    SLRUEntry *entry = taosArrayGetP(lastReferenceList, i);
55,236,353✔
563

564
    taosLRUEntryFree(entry);
55,236,353✔
565
  }
566

567
  taosArrayDestroy(lastReferenceList);
78,364,901✔
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) {
62,977,781✔
582
  if (handle == NULL) {
62,977,781✔
583
    return false;
×
584
  }
585

586
  SLRUEntry *e = (SLRUEntry *)handle;
62,977,781✔
587
  bool       lastReference = false;
62,977,781✔
588

589
  (void)taosThreadMutexLock(&shard->mutex);
62,977,781✔
590

591
  lastReference = taosLRUEntryUnref(e);
62,982,185✔
592
  if (lastReference && TAOS_LRU_ENTRY_IN_CACHE(e)) {
62,977,831✔
593
    if (shard->usage > shard->capacity || eraseIfLastRef) {
15,302,392✔
594
      SLRUEntry *tentry = taosLRUEntryTableRemove(&shard->table, e->keyData, e->keyLength, e->hash);
138,087✔
595
      TAOS_LRU_ENTRY_SET_IN_CACHE(e, false);
137,672✔
596
    } else {
597
      taosLRUCacheShardLRUInsert(shard, e);
15,164,720✔
598

599
      lastReference = false;
15,165,135✔
600
    }
601
  }
602

603
  if (lastReference && e->value) {
62,978,246✔
604
    shard->usage -= e->totalCharge;
47,808,456✔
605
  }
606

607
  (void)taosThreadMutexUnlock(&shard->mutex);
62,978,741✔
608

609
  if (lastReference) {
62,982,675✔
610
    taosLRUEntryFree(e);
47,811,086✔
611
  }
612

613
  return lastReference;
62,974,927✔
614
}
615

616
static size_t taosLRUCacheShardGetUsage(SLRUCacheShard *shard) {
131,919,161✔
617
  size_t usage = 0;
131,919,161✔
618

619
  (void)taosThreadMutexLock(&shard->mutex);
131,919,161✔
620
  usage = shard->usage;
131,919,161✔
621
  (void)taosThreadMutexUnlock(&shard->mutex);
131,919,161✔
622

623
  return usage;
131,919,161✔
624
}
625

626
static int32_t taosLRUCacheShardGetElems(SLRUCacheShard *shard) {
141,396,001✔
627
  int32_t elems = 0;
141,396,001✔
628

629
  (void)taosThreadMutexLock(&shard->mutex);
141,396,001✔
630
  elems = shard->table.elems;
141,396,001✔
631
  (void)taosThreadMutexUnlock(&shard->mutex);
141,396,001✔
632

633
  return elems;
141,396,001✔
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) {
78,383,692✔
649
  (void)taosThreadMutexLock(&shard->mutex);
78,383,692✔
650

651
  shard->strictCapacity = strict;
78,384,671✔
652

653
  (void)taosThreadMutexUnlock(&shard->mutex);
78,384,671✔
654
}
78,384,898✔
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) {
20,530,815✔
671
  int    numShardBits = 0;
20,530,815✔
672
  size_t minShardSize = 512 * 1024;
20,530,815✔
673
  size_t numShards = capacity / minShardSize;
20,530,815✔
674
  while (numShards >>= 1) {
86,495,470✔
675
    if (++numShardBits >= 6) {
66,111,787✔
676
      return numShardBits;
147,132✔
677
    }
678
  }
679

680
  return numShardBits;
20,383,683✔
681
}
682

683
SLRUCache *taosLRUCacheInit(size_t capacity, int numShardBits, double highPriPoolRatio) {
29,158,643✔
684
  if (numShardBits >= 20) {
29,158,643✔
685
    return NULL;
×
686
  }
687
  if (highPriPoolRatio < 0.0 || highPriPoolRatio > 1.0) {
29,158,643✔
688
    return NULL;
40✔
689
  }
690
  SLRUCache *cache = taosMemoryCalloc(1, sizeof(SLRUCache));
29,159,364✔
691
  if (!cache) {
29,158,931✔
692
    return NULL;
×
693
  }
694

695
  if (numShardBits < 0) {
29,158,931✔
696
    numShardBits = getDefaultCacheShardBits(capacity);
20,530,992✔
697
  }
698

699
  int numShards = 1 << numShardBits;
29,159,520✔
700
  cache->shards = taosMemoryCalloc(numShards, sizeof(SLRUCacheShard));
29,159,520✔
701
  if (!cache->shards) {
29,158,571✔
702
    taosMemoryFree(cache);
×
703
    return NULL;
×
704
  }
705

706
  bool   strictCapacity = 1;
29,159,583✔
707
  size_t perShard = (capacity + (numShards - 1)) / numShards;
29,159,583✔
708
  for (int i = 0; i < numShards; ++i) {
247,404,276✔
709
    if (TSDB_CODE_SUCCESS !=
218,244,693✔
710
        taosLRUCacheShardInit(&cache->shards[i], perShard, strictCapacity, highPriPoolRatio, 32 - numShardBits)) {
218,240,933✔
711
      taosMemoryFree(cache->shards);
×
712
      taosMemoryFree(cache);
×
713
      return NULL;
×
714
    }
715
  }
716

717
  cache->numShards = numShards;
29,163,343✔
718

719
  cache->shardedCache.shardMask = (1 << numShardBits) - 1;
29,161,222✔
720
  cache->shardedCache.strictCapacity = strictCapacity;
29,161,739✔
721
  cache->shardedCache.capacity = capacity;
29,161,222✔
722
  cache->shardedCache.lastId = 1;
29,161,222✔
723

724
  (void)taosThreadMutexInit(&cache->shardedCache.capacityMutex, NULL);
29,161,222✔
725

726
  return cache;
29,161,739✔
727
}
728

729
void taosLRUCacheCleanup(SLRUCache *cache) {
225,941,602✔
730
  if (cache) {
225,941,602✔
731
    if (cache->shards) {
29,150,974✔
732
      int numShards = cache->numShards;
29,150,597✔
733
      for (int i = 0; i < numShards; ++i) {
247,383,160✔
734
        taosLRUCacheShardCleanup(&cache->shards[i]);
218,231,557✔
735
      }
736
      taosMemoryFree(cache->shards);
29,151,603✔
737
      cache->shards = 0;
29,151,603✔
738
    }
739

740
    (void)taosThreadMutexDestroy(&cache->shardedCache.capacityMutex);
29,152,163✔
741

742
    taosMemoryFree(cache);
29,151,603✔
743
  }
744
}
225,942,231✔
745

746
LRUStatus taosLRUCacheInsert(SLRUCache *cache, const void *key, size_t keyLen, void *value, size_t charge,
107,583,480✔
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);
107,583,480✔
750
  uint32_t shardIndex = hash & cache->shardedCache.shardMask;
107,592,957✔
751

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

756
LRUHandle *taosLRUCacheLookup(SLRUCache *cache, const void *key, size_t keyLen) {
93,031,763✔
757
  uint32_t hash = TAOS_LRU_CACHE_SHARD_HASH32(key, keyLen);
93,031,763✔
758
  uint32_t shardIndex = hash & cache->shardedCache.shardMask;
93,037,190✔
759

760
  return taosLRUCacheShardLookup(&cache->shards[shardIndex], key, keyLen, hash);
93,037,514✔
761
}
762

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

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

770
void taosLRUCacheApply(SLRUCache *cache, _taos_lru_functor_t functor, void *ud) {
1,012,492✔
771
  int numShards = cache->numShards;
1,012,492✔
772
  for (int i = 0; i < numShards; ++i) {
2,026,436✔
773
    if (taosLRUCacheShardApply(&cache->shards[i], functor, ud)) {
1,012,688✔
774
      break;
×
775
    }
776
  }
777
}
1,013,748✔
778

779
void taosLRUCacheEraseUnrefEntries(SLRUCache *cache) {
12,697,373✔
780
  int numShards = cache->numShards;
12,697,373✔
781
  for (int i = 0; i < numShards; ++i) {
91,061,484✔
782
    taosLRUCacheShardEraseUnrefEntries(&cache->shards[i]);
78,364,024✔
783
  }
784
}
12,697,460✔
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) {
62,975,301✔
798
  if (handle == NULL) {
62,975,301✔
799
    return false;
×
800
  }
801

802
  uint32_t hash = ((SLRUEntry *)handle)->hash;
62,975,301✔
803
  uint32_t shardIndex = hash & cache->shardedCache.shardMask;
62,975,858✔
804

805
  return taosLRUCacheShardRelease(&cache->shards[shardIndex], handle, eraseIfLastRef);
62,975,534✔
806
}
807

808
void *taosLRUCacheValue(SLRUCache *cache, LRUHandle *handle) { return ((SLRUEntry *)handle)->value; }
53,377,239✔
809

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

812
size_t taosLRUCacheGetUsage(SLRUCache *cache) {
131,863,651✔
813
  size_t usage = 0;
131,863,651✔
814

815
  for (int i = 0; i < cache->numShards; ++i) {
263,782,812✔
816
    usage += taosLRUCacheShardGetUsage(&cache->shards[i]);
131,919,161✔
817
  }
818

819
  return usage;
131,863,651✔
820
}
821

822
int32_t taosLRUCacheGetElems(SLRUCache *cache) {
132,090,901✔
823
  int32_t elems = 0;
132,090,901✔
824

825
  for (int i = 0; i < cache->numShards; ++i) {
273,486,902✔
826
    elems += taosLRUCacheShardGetElems(&cache->shards[i]);
141,396,001✔
827
  }
828

829
  return elems;
132,090,901✔
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) {
5,496✔
843
  uint32_t numShards = cache->numShards;
5,496✔
844
  size_t   perShard = (capacity + (numShards - 1)) / numShards;
5,496✔
845

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

848
  for (int i = 0; i < numShards; ++i) {
10,992✔
849
    taosLRUCacheShardSetCapacity(&cache->shards[i], perShard);
5,496✔
850
  }
851

852
  cache->shardedCache.capacity = capacity;
5,496✔
853

854
  (void)taosThreadMutexUnlock(&cache->shardedCache.capacityMutex);
5,496✔
855
}
5,496✔
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) {
12,707,851✔
870
  uint32_t numShards = cache->numShards;
12,707,851✔
871

872
  (void)taosThreadMutexLock(&cache->shardedCache.capacityMutex);
12,707,851✔
873

874
  for (int i = 0; i < numShards; ++i) {
91,090,586✔
875
    taosLRUCacheShardSetStrictCapacity(&cache->shards[i], strict);
78,382,735✔
876
  }
877

878
  cache->shardedCache.strictCapacity = strict;
12,707,851✔
879

880
  (void)taosThreadMutexUnlock(&cache->shardedCache.capacityMutex);
12,707,851✔
881
}
12,707,851✔
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