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

taosdata / TDengine / #4869

26 Nov 2025 05:46AM UTC coverage: 64.539% (-0.09%) from 64.629%
#4869

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

771 of 945 new or added lines in 33 files covered. (81.59%)

3214 existing lines in 124 files now uncovered.

158203 of 245129 relevant lines covered (64.54%)

113224023.06 hits per line

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

75.27
/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) {
47,810,651✔
91
  --entry->refs;
47,810,651✔
92
  return entry->refs == 0;
47,814,237✔
93
}
94

95
static void taosLRUEntryFree(SLRUEntry *entry) {
201,810,162✔
96
  if (entry->deleter) {
201,810,162✔
97
    (*entry->deleter)(entry->keyData, entry->keyLength, entry->value, entry->ud);
201,816,171✔
98
  }
99

100
  taosMemoryFree(entry);
201,819,329✔
101
}
201,818,869✔
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) {
340,196,781✔
113
  table->lengthBits = 16;
340,196,781✔
114
  table->list = taosMemoryCalloc(1 << table->lengthBits, sizeof(SLRUEntry *));
340,220,528✔
115
  if (!table->list) {
340,266,189✔
116
    TAOS_RETURN(terrno);
×
117
  }
118

119
  table->elems = 0;
340,265,836✔
120
  table->maxLengthBits = maxUpperHashBits;
340,266,327✔
121

122
  TAOS_RETURN(TSDB_CODE_SUCCESS);
340,257,884✔
123
}
124

125
static void taosLRUEntryTableApply(SLRUEntryTable *table, _taos_lru_table_func_t func, uint32_t begin, uint32_t end) {
340,224,897✔
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;
177,977,382✔
130
      func(h);
178,005,896✔
131
      h = n;
4,750,903✔
132
    }
133
  }
134
}
329,078✔
135

136
static void taosLRUEntryTableFree(SLRUEntry *entry) {
178,014,434✔
137
  if (!TAOS_LRU_ENTRY_HAS_REFS(entry)) {
178,014,434✔
138
    taosLRUEntryFree(entry);
178,015,975✔
139
  }
140
}
178,028,346✔
141

142
static void taosLRUEntryTableCleanup(SLRUEntryTable *table) {
340,228,856✔
143
  taosLRUEntryTableApply(table, taosLRUEntryTableFree, 0, 1 << table->lengthBits);
340,228,856✔
144

145
  taosMemoryFree(table->list);
340,239,776✔
146
}
340,240,287✔
147

148
static int taosLRUEntryTableApplyF(SLRUEntryTable *table, _taos_lru_functor_t functor, void *ud) {
1,090,969✔
149
  int      ret = 0;
1,090,969✔
150
  uint32_t end = 1 << table->lengthBits;
1,090,969✔
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;
23,352,225✔
155
      ret = functor(h->keyData, h->keyLength, h->value, ud);
23,352,225✔
156
      if (ret) {
23,355,579✔
157
        return ret;
×
158
      }
159
      h = n;
23,355,579✔
160
    }
161
  }
162

UNCOV
163
  return ret;
×
164
}
165

166
static SLRUEntry **taosLRUEntryTableFindPtr(SLRUEntryTable *table, const void *key, size_t keyLen, uint32_t hash) {
461,686,713✔
167
  SLRUEntry **entry = &table->list[hash >> (32 - table->lengthBits)];
461,686,713✔
168
  while (*entry && ((*entry)->hash != hash || memcmp(key, (*entry)->keyData, keyLen) != 0)) {
462,967,895✔
169
    entry = &(*entry)->nextHash;
1,033,495✔
170
  }
171

172
  return entry;
461,853,281✔
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) {
246,520,548✔
211
  return *taosLRUEntryTableFindPtr(table, key, keyLen, hash);
246,520,548✔
212
}
213

214
static SLRUEntry *taosLRUEntryTableInsert(SLRUEntryTable *table, SLRUEntry *entry) {
201,800,326✔
215
  SLRUEntry **ptr = taosLRUEntryTableFindPtr(table, entry->keyData, entry->keyLength, entry->hash);
201,800,326✔
216
  SLRUEntry  *old = *ptr;
201,812,076✔
217
  entry->nextHash = (old == NULL) ? NULL : old->nextHash;
201,802,906✔
218
  *ptr = entry;
201,812,545✔
219
  if (old == NULL) {
201,811,729✔
220
    ++table->elems;
191,264,948✔
221
    if ((table->elems >> table->lengthBits) > 0) {
191,266,712✔
222
      taosLRUEntryTableResize(table);
×
223
    }
224
  }
225

226
  return old;
201,813,871✔
227
}
228

229
static SLRUEntry *taosLRUEntryTableRemove(SLRUEntryTable *table, const void *key, size_t keyLen, uint32_t hash) {
13,456,062✔
230
  SLRUEntry **entry = taosLRUEntryTableFindPtr(table, key, keyLen, hash);
13,456,062✔
231
  SLRUEntry  *result = *entry;
13,456,062✔
232
  if (result) {
13,456,062✔
233
    *entry = result->nextHash;
13,239,239✔
234
    --table->elems;
13,239,239✔
235
  }
236

237
  return result;
13,456,062✔
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) {
37,070,740✔
257
  while (shard->highPriPoolUsage > shard->highPriPoolCapacity) {
37,070,740✔
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
}
37,073,675✔
264

265
static void taosLRUCacheShardLRUInsert(SLRUCacheShard *shard, SLRUEntry *e) {
238,874,075✔
266
  if (shard->highPriPoolRatio > 0 && (TAOS_LRU_ENTRY_IS_HIGH_PRI(e) || TAOS_LRU_ENTRY_HAS_HIT(e))) {
238,874,075✔
267
    e->next = &shard->lru;
37,086,796✔
268
    e->prev = shard->lru.prev;
37,074,028✔
269

270
    e->prev->next = e;
37,072,618✔
271
    e->next->prev = e;
37,073,100✔
272

273
    TAOS_LRU_ENTRY_SET_IN_HIGH_POOL(e, true);
37,073,311✔
274
    shard->highPriPoolUsage += e->totalCharge;
37,074,923✔
275
    taosLRUCacheShardMaintainPoolSize(shard);
37,073,251✔
276
  } else {
277
    e->next = shard->lruLowPri->next;
201,807,202✔
278
    e->prev = shard->lruLowPri;
201,807,734✔
279

280
    e->prev->next = e;
201,814,962✔
281
    e->next->prev = e;
201,803,377✔
282

283
    TAOS_LRU_ENTRY_SET_IN_HIGH_POOL(e, false);
201,805,412✔
284
    shard->lruLowPri = e;
201,794,453✔
285
  }
286

287
  shard->lruUsage += e->totalCharge;
238,876,562✔
288
}
238,868,365✔
289

290
static void taosLRUCacheShardLRURemove(SLRUCacheShard *shard, SLRUEntry *e) {
60,864,737✔
291
  if (shard->lruLowPri == e) {
60,864,737✔
292
    shard->lruLowPri = e->prev;
2,260,938✔
293
  }
294
  e->next->prev = e->prev;
60,866,521✔
295
  e->prev->next = e->next;
60,865,175✔
296
  e->prev = e->next = NULL;
60,863,609✔
297

298
  shard->lruUsage -= e->totalCharge;
60,862,710✔
299
  if (TAOS_LRU_ENTRY_IN_HIGH_POOL(e)) {
60,863,618✔
300
    shard->highPriPoolUsage -= e->totalCharge;
34,506,880✔
301
  }
302
}
60,866,788✔
303

304
static void taosLRUCacheShardEvictLRU(SLRUCacheShard *shard, size_t charge, SArray *deleted) {
340,204,559✔
305
  while (shard->usage + charge > shard->capacity && shard->lru.next != &shard->lru) {
340,204,559✔
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
}
340,211,890✔
318

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

325
  (void)taosThreadMutexLock(&shard->mutex);
340,202,320✔
326

327
  shard->capacity = capacity;
340,269,132✔
328
  shard->highPriPoolCapacity = capacity * shard->highPriPoolRatio;
340,268,051✔
329
  taosLRUCacheShardEvictLRU(shard, 0, lastReferenceList);
340,223,057✔
330

331
  (void)taosThreadMutexUnlock(&shard->mutex);
340,162,455✔
332

333
  for (int i = 0; i < taosArrayGetSize(lastReferenceList); ++i) {
340,264,775✔
334
    SLRUEntry *entry = taosArrayGetP(lastReferenceList, i);
×
335
    taosLRUEntryFree(entry);
×
336
  }
337
  taosArrayDestroy(lastReferenceList);
340,254,719✔
338
}
339

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

344
  (void)taosThreadMutexInit(&shard->mutex, NULL);
340,264,101✔
345

346
  (void)taosThreadMutexLock(&shard->mutex);
340,248,590✔
347
  shard->capacity = 0;
340,264,450✔
348
  shard->highPriPoolUsage = 0;
340,264,253✔
349
  shard->strictCapacity = strict;
340,231,273✔
350
  shard->highPriPoolRatio = highPriPoolRatio;
340,246,697✔
351
  shard->highPriPoolCapacity = 0;
340,235,755✔
352

353
  shard->usage = 0;
340,234,274✔
354
  shard->lruUsage = 0;
340,223,158✔
355

356
  shard->lru.next = &shard->lru;
340,256,194✔
357
  shard->lru.prev = &shard->lru;
340,223,131✔
358
  shard->lruLowPri = &shard->lru;
340,243,300✔
359
  (void)taosThreadMutexUnlock(&shard->mutex);
340,223,927✔
360

361
  taosLRUCacheShardSetCapacity(shard, capacity);
340,220,570✔
362

363
  TAOS_RETURN(TSDB_CODE_SUCCESS);
340,187,363✔
364
}
365

366
static void taosLRUCacheShardCleanup(SLRUCacheShard *shard) {
340,228,469✔
367
  (void)taosThreadMutexDestroy(&shard->mutex);
340,228,469✔
368

369
  taosLRUEntryTableCleanup(&shard->table);
340,235,786✔
370
}
340,237,728✔
371

372
static LRUStatus taosLRUCacheShardInsertEntry(SLRUCacheShard *shard, SLRUEntry *e, LRUHandle **handle,
201,803,640✔
373
                                              bool freeOnFail) {
374
  LRUStatus  status = TAOS_LRU_STATUS_OK;
201,803,640✔
375
  SLRUEntry *toFree = NULL;
201,803,640✔
376
  SArray    *lastReferenceList = NULL;
201,803,640✔
377
  if (shard->usage + e->totalCharge > shard->capacity) {
201,803,640✔
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);
201,812,269✔
386

387
  if (shard->usage + e->totalCharge > shard->capacity && shard->lru.next != &shard->lru) {
201,816,035✔
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)) {
201,819,426✔
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);
201,791,751✔
414
    shard->usage += e->totalCharge;
201,815,214✔
415
    if (old != NULL) {
201,814,127✔
416
      status = TAOS_LRU_STATUS_OK_OVERWRITTEN;
10,550,705✔
417

418
      if (old->overwriter) {
10,550,705✔
419
        (*old->overwriter)(old->keyData, old->keyLength, old->value, old->ud);
10,550,705✔
420
      }
421

422
      TAOS_LRU_ENTRY_SET_IN_CACHE(old, false);
10,550,705✔
423
      if (!TAOS_LRU_ENTRY_HAS_REFS(old)) {
10,550,705✔
424
        taosLRUCacheShardLRURemove(shard, old);
2,050✔
425
        shard->usage -= old->totalCharge;
2,050✔
426

427
        toFree = old;
744✔
428
      }
429
    }
430
    if (handle == NULL) {
201,809,749✔
431
      taosLRUCacheShardLRUInsert(shard, e);
201,809,749✔
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:
201,789,102✔
442
  (void)taosThreadMutexUnlock(&shard->mutex);
201,789,102✔
443

444
  if (toFree) {
201,810,580✔
445
    taosLRUEntryFree(toFree);
2,050✔
446
  }
447

448
  for (int i = 0; i < taosArrayGetSize(lastReferenceList); ++i) {
201,811,876✔
449
    SLRUEntry *entry = taosArrayGetP(lastReferenceList, i);
×
450

451
    taosLRUEntryFree(entry);
×
452
  }
453
  taosArrayDestroy(lastReferenceList);
201,797,539✔
454

455
  return status;
201,801,442✔
456
}
457

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

470
  e->value = value;
201,807,811✔
471
  e->flags = 0;
201,806,592✔
472
  e->deleter = deleter;
201,812,607✔
473
  e->overwriter = overwriter;
201,809,109✔
474
  e->ud = ud;
201,797,863✔
475
  e->keyLength = keyLen;
201,812,686✔
476
  e->hash = hash;
201,809,723✔
477
  e->refs = 0;
201,805,454✔
478
  e->next = e->prev = NULL;
201,804,415✔
479
  TAOS_LRU_ENTRY_SET_IN_CACHE(e, true);
201,806,177✔
480

481
  TAOS_LRU_ENTRY_SET_PRIORITY(e, priority);
201,796,237✔
482
  memcpy(e->keyData, key, keyLen);
201,809,887✔
483
  // TODO: e->CalcTotalCharge(charge, metadataChargePolicy);
484
  e->totalCharge = charge;
201,807,845✔
485

486
  return taosLRUCacheShardInsertEntry(shard, e, handle, true);
201,806,318✔
487
}
488

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

492
  (void)taosThreadMutexLock(&shard->mutex);
246,484,350✔
493
  e = taosLRUEntryTableLookup(&shard->table, key, keyLen, hash);
246,567,209✔
494
  if (e != NULL) {
246,589,244✔
495
    if (!TAOS_LRU_ENTRY_HAS_REFS(e)) {
47,814,813✔
496
      taosLRUCacheShardLRURemove(shard, e);
47,792,860✔
497
    }
498
    TAOS_LRU_ENTRY_REF(e);
47,814,042✔
499
    TAOS_LRU_ENTRY_SET_HIT(e);
47,813,789✔
500
  }
501

502
  (void)taosThreadMutexUnlock(&shard->mutex);
246,587,726✔
503

504
  return (LRUHandle *)e;
246,590,357✔
505
}
506

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

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

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

522
  (void)taosThreadMutexUnlock(&shard->mutex);
220,558✔
523

524
  if (lastReference) {
220,558✔
525
    taosLRUEntryFree(e);
3,735✔
526
  }
527
}
220,558✔
528

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

532
  (void)taosThreadMutexLock(&shard->mutex);
1,091,609✔
533

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

536
  (void)taosThreadMutexUnlock(&shard->mutex);
1,092,839✔
537

538
  return ret;
1,092,839✔
539
}
540

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

544
  (void)taosThreadMutexLock(&shard->mutex);
78,128,238✔
545

546
  while (shard->lru.next != &shard->lru) {
91,196,901✔
547
    SLRUEntry *old = shard->lru.next;
13,067,948✔
548
    taosLRUCacheShardLRURemove(shard, old);
13,067,948✔
549
    SLRUEntry *tentry = taosLRUEntryTableRemove(&shard->table, old->keyData, old->keyLength, old->hash);
13,067,948✔
550
    TAOS_LRU_ENTRY_SET_IN_CACHE(old, false);
13,067,948✔
551
    shard->usage -= old->totalCharge;
13,067,948✔
552

553
    if (!taosArrayPush(lastReferenceList, &old)) {
13,067,948✔
554
      taosLRUEntryFree(old);
×
555
      return;
×
556
    }
557
  }
558

559
  (void)taosThreadMutexUnlock(&shard->mutex);
78,128,978✔
560

561
  for (int i = 0; i < taosArrayGetSize(lastReferenceList); ++i) {
91,196,229✔
562
    SLRUEntry *entry = taosArrayGetP(lastReferenceList, i);
13,067,948✔
563

564
    taosLRUEntryFree(entry);
13,067,471✔
565
  }
566

567
  taosArrayDestroy(lastReferenceList);
78,128,390✔
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) {
47,813,194✔
582
  if (handle == NULL) {
47,813,194✔
583
    return false;
×
584
  }
585

586
  SLRUEntry *e = (SLRUEntry *)handle;
47,813,194✔
587
  bool       lastReference = false;
47,813,194✔
588

589
  (void)taosThreadMutexLock(&shard->mutex);
47,813,194✔
590

591
  lastReference = taosLRUEntryUnref(e);
47,814,908✔
592
  if (lastReference && TAOS_LRU_ENTRY_IN_CACHE(e)) {
47,814,053✔
593
    if (shard->usage > shard->capacity || eraseIfLastRef) {
37,244,647✔
594
      SLRUEntry *tentry = taosLRUEntryTableRemove(&shard->table, e->keyData, e->keyLength, e->hash);
169,219✔
595
      TAOS_LRU_ENTRY_SET_IN_CACHE(e, false);
167,556✔
596
    } else {
597
      taosLRUCacheShardLRUInsert(shard, e);
37,073,746✔
598

599
      lastReference = false;
37,072,493✔
600
    }
601
  }
602

603
  if (lastReference && e->value) {
47,806,753✔
604
    shard->usage -= e->totalCharge;
10,716,211✔
605
  }
606

607
  (void)taosThreadMutexUnlock(&shard->mutex);
47,806,753✔
608

609
  if (lastReference) {
47,812,270✔
610
    taosLRUEntryFree(e);
10,716,211✔
611
  }
612

613
  return lastReference;
47,813,644✔
614
}
615

616
static size_t taosLRUCacheShardGetUsage(SLRUCacheShard *shard) {
114,486,511✔
617
  size_t usage = 0;
114,486,511✔
618

619
  (void)taosThreadMutexLock(&shard->mutex);
114,486,511✔
620
  usage = shard->usage;
114,486,511✔
621
  (void)taosThreadMutexUnlock(&shard->mutex);
114,486,511✔
622

623
  return usage;
114,486,511✔
624
}
625

626
static int32_t taosLRUCacheShardGetElems(SLRUCacheShard *shard) {
123,193,663✔
627
  int32_t elems = 0;
123,193,663✔
628

629
  (void)taosThreadMutexLock(&shard->mutex);
123,193,663✔
630
  elems = shard->table.elems;
123,193,663✔
631
  (void)taosThreadMutexUnlock(&shard->mutex);
123,193,663✔
632

633
  return elems;
123,193,663✔
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) {
181,438,274✔
649
  (void)taosThreadMutexLock(&shard->mutex);
181,438,274✔
650

651
  shard->strictCapacity = strict;
181,492,536✔
652

653
  (void)taosThreadMutexUnlock(&shard->mutex);
181,489,406✔
654
}
181,448,675✔
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) {
143,842,847✔
671
  int    numShardBits = 0;
143,842,847✔
672
  size_t minShardSize = 512 * 1024;
143,842,847✔
673
  size_t numShards = capacity / minShardSize;
143,842,847✔
674
  while (numShards >>= 1) {
209,494,917✔
675
    if (++numShardBits >= 6) {
65,787,497✔
676
      return numShardBits;
135,427✔
677
    }
678
  }
679

680
  return numShardBits;
143,707,420✔
681
}
682

683
SLRUCache *taosLRUCacheInit(size_t capacity, int numShardBits, double highPriPoolRatio) {
152,473,116✔
684
  if (numShardBits >= 20) {
152,473,116✔
685
    return NULL;
×
686
  }
687
  if (highPriPoolRatio < 0.0 || highPriPoolRatio > 1.0) {
152,473,116✔
688
    return NULL;
159,302✔
689
  }
690
  SLRUCache *cache = taosMemoryCalloc(1, sizeof(SLRUCache));
152,313,814✔
691
  if (!cache) {
152,368,766✔
692
    return NULL;
×
693
  }
694

695
  if (numShardBits < 0) {
152,368,766✔
696
    numShardBits = getDefaultCacheShardBits(capacity);
143,775,347✔
697
  }
698

699
  int numShards = 1 << numShardBits;
152,390,197✔
700
  cache->shards = taosMemoryCalloc(numShards, sizeof(SLRUCacheShard));
152,390,197✔
701
  if (!cache->shards) {
152,363,383✔
702
    taosMemoryFree(cache);
×
703
    return NULL;
×
704
  }
705

706
  bool   strictCapacity = 1;
152,421,259✔
707
  size_t perShard = (capacity + (numShards - 1)) / numShards;
152,421,259✔
708
  for (int i = 0; i < numShards; ++i) {
492,610,097✔
709
    if (TSDB_CODE_SUCCESS !=
340,188,838✔
710
        taosLRUCacheShardInit(&cache->shards[i], perShard, strictCapacity, highPriPoolRatio, 32 - numShardBits)) {
340,190,202✔
711
      taosMemoryFree(cache->shards);
×
712
      taosMemoryFree(cache);
×
713
      return NULL;
×
714
    }
715
  }
716

717
  cache->numShards = numShards;
152,419,895✔
718

719
  cache->shardedCache.shardMask = (1 << numShardBits) - 1;
152,469,914✔
720
  cache->shardedCache.strictCapacity = strictCapacity;
152,485,888✔
721
  cache->shardedCache.capacity = capacity;
152,447,958✔
722
  cache->shardedCache.lastId = 1;
152,475,650✔
723

724
  (void)taosThreadMutexInit(&cache->shardedCache.capacityMutex, NULL);
152,437,619✔
725

726
  return cache;
152,471,196✔
727
}
728

729
void taosLRUCacheCleanup(SLRUCache *cache) {
152,919,998✔
730
  if (cache) {
152,919,998✔
731
    if (cache->shards) {
152,516,454✔
732
      int numShards = cache->numShards;
152,514,986✔
733
      for (int i = 0; i < numShards; ++i) {
492,758,217✔
734
        taosLRUCacheShardCleanup(&cache->shards[i]);
340,230,950✔
735
      }
736
      taosMemoryFree(cache->shards);
152,527,267✔
737
      cache->shards = 0;
152,527,764✔
738
    }
739

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

742
    taosMemoryFree(cache);
152,527,450✔
743
  }
744
}
152,930,186✔
745

746
LRUStatus taosLRUCacheInsert(SLRUCache *cache, const void *key, size_t keyLen, void *value, size_t charge,
201,808,734✔
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);
201,808,734✔
750
  uint32_t shardIndex = hash & cache->shardedCache.shardMask;
201,813,607✔
751

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

756
LRUHandle *taosLRUCacheLookup(SLRUCache *cache, const void *key, size_t keyLen) {
246,422,641✔
757
  uint32_t hash = TAOS_LRU_CACHE_SHARD_HASH32(key, keyLen);
246,422,641✔
758
  uint32_t shardIndex = hash & cache->shardedCache.shardMask;
246,513,273✔
759

760
  return taosLRUCacheShardLookup(&cache->shards[shardIndex], key, keyLen, hash);
246,531,198✔
761
}
762

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

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

770
void taosLRUCacheApply(SLRUCache *cache, _taos_lru_functor_t functor, void *ud) {
1,091,609✔
771
  int numShards = cache->numShards;
1,091,609✔
772
  for (int i = 0; i < numShards; ++i) {
2,183,096✔
773
    if (taosLRUCacheShardApply(&cache->shards[i], functor, ud)) {
1,090,873✔
774
      break;
×
775
    }
776
  }
777
}
1,092,223✔
778

779
void taosLRUCacheEraseUnrefEntries(SLRUCache *cache) {
12,646,824✔
780
  int numShards = cache->numShards;
12,646,824✔
781
  for (int i = 0; i < numShards; ++i) {
90,774,747✔
782
    taosLRUCacheShardEraseUnrefEntries(&cache->shards[i]);
78,128,232✔
783
  }
784
}
12,646,515✔
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) {
47,814,698✔
798
  if (handle == NULL) {
47,814,698✔
799
    return false;
×
800
  }
801

802
  uint32_t hash = ((SLRUEntry *)handle)->hash;
47,814,698✔
803
  uint32_t shardIndex = hash & cache->shardedCache.shardMask;
47,814,033✔
804

805
  return taosLRUCacheShardRelease(&cache->shards[shardIndex], handle, eraseIfLastRef);
47,813,940✔
806
}
807

808
void *taosLRUCacheValue(SLRUCache *cache, LRUHandle *handle) { return ((SLRUEntry *)handle)->value; }
47,644,528✔
809

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

812
size_t taosLRUCacheGetUsage(SLRUCache *cache) {
114,460,429✔
813
  size_t usage = 0;
114,460,429✔
814

815
  for (int i = 0; i < cache->numShards; ++i) {
228,946,940✔
816
    usage += taosLRUCacheShardGetUsage(&cache->shards[i]);
114,486,511✔
817
  }
818

819
  return usage;
114,460,429✔
820
}
821

822
int32_t taosLRUCacheGetElems(SLRUCache *cache) {
114,593,218✔
823
  int32_t elems = 0;
114,593,218✔
824

825
  for (int i = 0; i < cache->numShards; ++i) {
237,786,881✔
826
    elems += taosLRUCacheShardGetElems(&cache->shards[i]);
123,193,663✔
827
  }
828

829
  return elems;
114,593,218✔
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) {
4,042✔
843
  uint32_t numShards = cache->numShards;
4,042✔
844
  size_t   perShard = (capacity + (numShards - 1)) / numShards;
4,042✔
845

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

848
  for (int i = 0; i < numShards; ++i) {
8,084✔
849
    taosLRUCacheShardSetCapacity(&cache->shards[i], perShard);
4,042✔
850
  }
851

852
  cache->shardedCache.capacity = capacity;
4,042✔
853

854
  (void)taosThreadMutexUnlock(&cache->shardedCache.capacityMutex);
4,042✔
855
}
4,042✔
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) {
115,952,764✔
870
  uint32_t numShards = cache->numShards;
115,952,764✔
871

872
  (void)taosThreadMutexLock(&cache->shardedCache.capacityMutex);
115,980,691✔
873

874
  for (int i = 0; i < numShards; ++i) {
297,409,010✔
875
    taosLRUCacheShardSetStrictCapacity(&cache->shards[i], strict);
181,442,092✔
876
  }
877

878
  cache->shardedCache.strictCapacity = strict;
115,966,918✔
879

880
  (void)taosThreadMutexUnlock(&cache->shardedCache.capacityMutex);
115,968,229✔
881
}
115,982,310✔
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