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

taosdata / TDengine / #4857

17 Nov 2025 09:53AM UTC coverage: 64.135% (-0.2%) from 64.286%
#4857

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

218 of 311 new or added lines in 32 files covered. (70.1%)

5044 existing lines in 121 files now uncovered.

151302 of 235910 relevant lines covered (64.14%)

116627960.99 hits per line

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

75.05
/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) {
58,202,081✔
91
  --entry->refs;
58,202,081✔
92
  return entry->refs == 0;
58,204,685✔
93
}
94

95
static void taosLRUEntryFree(SLRUEntry *entry) {
212,139,796✔
96
  if (entry->deleter) {
212,139,796✔
97
    (*entry->deleter)(entry->keyData, entry->keyLength, entry->value, entry->ud);
212,148,358✔
98
  }
99

100
  taosMemoryFree(entry);
212,148,214✔
101
}
212,146,164✔
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) {
352,221,071✔
113
  table->lengthBits = 16;
352,221,071✔
114
  table->list = taosMemoryCalloc(1 << table->lengthBits, sizeof(SLRUEntry *));
352,241,798✔
115
  if (!table->list) {
352,315,139✔
116
    TAOS_RETURN(terrno);
×
117
  }
118

119
  table->elems = 0;
352,315,119✔
120
  table->maxLengthBits = maxUpperHashBits;
352,313,922✔
121

122
  TAOS_RETURN(TSDB_CODE_SUCCESS);
352,314,982✔
123
}
124

125
static void taosLRUEntryTableApply(SLRUEntryTable *table, _taos_lru_table_func_t func, uint32_t begin, uint32_t end) {
352,244,887✔
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;
186,405,011✔
130
      func(h);
186,407,359✔
131
      h = n;
2,147,483,647✔
132
    }
133
  }
134
}
362,663✔
135

136
static void taosLRUEntryTableFree(SLRUEntry *entry) {
186,417,618✔
137
  if (!TAOS_LRU_ENTRY_HAS_REFS(entry)) {
186,417,618✔
138
    taosLRUEntryFree(entry);
186,418,654✔
139
  }
140
}
186,430,727✔
141

142
static void taosLRUEntryTableCleanup(SLRUEntryTable *table) {
352,251,045✔
143
  taosLRUEntryTableApply(table, taosLRUEntryTableFree, 0, 1 << table->lengthBits);
352,251,045✔
144

145
  taosMemoryFree(table->list);
352,266,775✔
146
}
352,275,158✔
147

148
static int taosLRUEntryTableApplyF(SLRUEntryTable *table, _taos_lru_functor_t functor, void *ud) {
1,116,672✔
149
  int      ret = 0;
1,116,672✔
150
  uint32_t end = 1 << table->lengthBits;
1,116,672✔
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;
22,636,168✔
155
      ret = functor(h->keyData, h->keyLength, h->value, ud);
22,636,168✔
156
      if (ret) {
22,638,962✔
157
        return ret;
×
158
      }
159
      h = n;
22,638,962✔
160
    }
161
  }
162

163
  return ret;
×
164
}
165

166
static SLRUEntry **taosLRUEntryTableFindPtr(SLRUEntryTable *table, const void *key, size_t keyLen, uint32_t hash) {
492,241,499✔
167
  SLRUEntry **entry = &table->list[hash >> (32 - table->lengthBits)];
492,241,499✔
168
  while (*entry && ((*entry)->hash != hash || memcmp(key, (*entry)->keyData, keyLen) != 0)) {
494,181,755✔
169
    entry = &(*entry)->nextHash;
1,748,661✔
170
  }
171

172
  return entry;
492,385,609✔
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) {
265,859,796✔
211
  return *taosLRUEntryTableFindPtr(table, key, keyLen, hash);
265,859,796✔
212
}
213

214
static SLRUEntry *taosLRUEntryTableInsert(SLRUEntryTable *table, SLRUEntry *entry) {
212,132,320✔
215
  SLRUEntry **ptr = taosLRUEntryTableFindPtr(table, entry->keyData, entry->keyLength, entry->hash);
212,132,320✔
216
  SLRUEntry  *old = *ptr;
212,132,997✔
217
  entry->nextHash = (old == NULL) ? NULL : old->nextHash;
212,116,707✔
218
  *ptr = entry;
212,124,245✔
219
  if (old == NULL) {
212,108,804✔
220
    ++table->elems;
200,494,400✔
221
    if ((table->elems >> table->lengthBits) > 0) {
200,485,783✔
222
      taosLRUEntryTableResize(table);
×
223
    }
224
  }
225

226
  return old;
212,128,961✔
227
}
228

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

237
  return result;
14,318,555✔
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) {
46,355,979✔
257
  while (shard->highPriPoolUsage > shard->highPriPoolCapacity) {
46,355,979✔
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
}
46,360,086✔
264

265
static void taosLRUCacheShardLRUInsert(SLRUCacheShard *shard, SLRUEntry *e) {
258,477,698✔
266
  if (shard->highPriPoolRatio > 0 && (TAOS_LRU_ENTRY_IS_HIGH_PRI(e) || TAOS_LRU_ENTRY_HAS_HIT(e))) {
258,477,698✔
267
    e->next = &shard->lru;
46,371,711✔
268
    e->prev = shard->lru.prev;
46,364,882✔
269

270
    e->prev->next = e;
46,360,975✔
271
    e->next->prev = e;
46,363,892✔
272

273
    TAOS_LRU_ENTRY_SET_IN_HIGH_POOL(e, true);
46,362,319✔
274
    shard->highPriPoolUsage += e->totalCharge;
46,361,513✔
275
    taosLRUCacheShardMaintainPoolSize(shard);
46,360,603✔
276
  } else {
277
    e->next = shard->lruLowPri->next;
212,124,407✔
278
    e->prev = shard->lruLowPri;
212,126,167✔
279

280
    e->prev->next = e;
212,124,477✔
281
    e->next->prev = e;
212,123,599✔
282

283
    TAOS_LRU_ENTRY_SET_IN_HIGH_POOL(e, false);
212,121,405✔
284
    shard->lruLowPri = e;
212,125,640✔
285
  }
286

287
  shard->lruUsage += e->totalCharge;
258,488,351✔
288
}
258,478,643✔
289

290
static void taosLRUCacheShardLRURemove(SLRUCacheShard *shard, SLRUEntry *e) {
72,077,187✔
291
  if (shard->lruLowPri == e) {
72,077,187✔
292
    shard->lruLowPri = e->prev;
2,776,987✔
293
  }
294
  e->next->prev = e->prev;
72,080,109✔
295
  e->prev->next = e->next;
72,077,518✔
296
  e->prev = e->next = NULL;
72,073,962✔
297

298
  shard->lruUsage -= e->totalCharge;
72,070,610✔
299
  if (TAOS_LRU_ENTRY_IN_HIGH_POOL(e)) {
72,071,484✔
300
    shard->highPriPoolUsage -= e->totalCharge;
43,052,721✔
301
  }
302
}
72,076,024✔
303

304
static void taosLRUCacheShardEvictLRU(SLRUCacheShard *shard, size_t charge, SArray *deleted) {
352,241,084✔
305
  while (shard->usage + charge > shard->capacity && shard->lru.next != &shard->lru) {
352,241,084✔
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
}
352,249,900✔
318

319
static void taosLRUCacheShardSetCapacity(SLRUCacheShard *shard, size_t capacity) {
352,244,138✔
320
  SArray *lastReferenceList = taosArrayInit(16, POINTER_BYTES);
352,244,138✔
321
  if (!lastReferenceList) {
352,246,385✔
322
    return;
×
323
  }
324

325
  (void)taosThreadMutexLock(&shard->mutex);
352,246,385✔
326

327
  shard->capacity = capacity;
352,316,348✔
328
  shard->highPriPoolCapacity = capacity * shard->highPriPoolRatio;
352,316,274✔
329
  taosLRUCacheShardEvictLRU(shard, 0, lastReferenceList);
352,292,786✔
330

331
  (void)taosThreadMutexUnlock(&shard->mutex);
352,164,747✔
332

333
  for (int i = 0; i < taosArrayGetSize(lastReferenceList); ++i) {
352,306,725✔
334
    SLRUEntry *entry = taosArrayGetP(lastReferenceList, i);
×
335
    taosLRUEntryFree(entry);
×
336
  }
337
  taosArrayDestroy(lastReferenceList);
352,258,163✔
338
}
339

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

344
  (void)taosThreadMutexInit(&shard->mutex, NULL);
352,312,409✔
345

346
  (void)taosThreadMutexLock(&shard->mutex);
352,289,652✔
347
  shard->capacity = 0;
352,311,715✔
348
  shard->highPriPoolUsage = 0;
352,312,027✔
349
  shard->strictCapacity = strict;
352,309,895✔
350
  shard->highPriPoolRatio = highPriPoolRatio;
352,286,271✔
351
  shard->highPriPoolCapacity = 0;
352,270,308✔
352

353
  shard->usage = 0;
352,300,608✔
354
  shard->lruUsage = 0;
352,278,379✔
355

356
  shard->lru.next = &shard->lru;
352,269,626✔
357
  shard->lru.prev = &shard->lru;
352,274,633✔
358
  shard->lruLowPri = &shard->lru;
352,257,466✔
359
  (void)taosThreadMutexUnlock(&shard->mutex);
352,268,903✔
360

361
  taosLRUCacheShardSetCapacity(shard, capacity);
352,244,857✔
362

363
  TAOS_RETURN(TSDB_CODE_SUCCESS);
352,231,011✔
364
}
365

366
static void taosLRUCacheShardCleanup(SLRUCacheShard *shard) {
352,253,463✔
367
  (void)taosThreadMutexDestroy(&shard->mutex);
352,253,463✔
368

369
  taosLRUEntryTableCleanup(&shard->table);
352,265,103✔
370
}
352,267,894✔
371

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

387
  if (shard->usage + e->totalCharge > shard->capacity && shard->lru.next != &shard->lru) {
212,140,424✔
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)) {
212,133,753✔
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);
212,132,540✔
414
    shard->usage += e->totalCharge;
212,128,263✔
415
    if (old != NULL) {
212,133,041✔
416
      status = TAOS_LRU_STATUS_OK_OVERWRITTEN;
11,645,528✔
417

418
      if (old->overwriter) {
11,645,528✔
419
        (*old->overwriter)(old->keyData, old->keyLength, old->value, old->ud);
11,645,528✔
420
      }
421

422
      TAOS_LRU_ENTRY_SET_IN_CACHE(old, false);
11,644,128✔
423
      if (!TAOS_LRU_ENTRY_HAS_REFS(old)) {
11,644,208✔
424
        taosLRUCacheShardLRURemove(shard, old);
2,407✔
425
        shard->usage -= old->totalCharge;
2,407✔
426

UNCOV
427
        toFree = old;
×
428
      }
429
    }
430
    if (handle == NULL) {
212,120,989✔
431
      taosLRUCacheShardLRUInsert(shard, e);
212,120,989✔
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:
212,104,343✔
442
  (void)taosThreadMutexUnlock(&shard->mutex);
212,104,343✔
443

444
  if (toFree) {
212,129,785✔
445
    taosLRUEntryFree(toFree);
2,407✔
446
  }
447

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

451
    taosLRUEntryFree(entry);
×
452
  }
453
  taosArrayDestroy(lastReferenceList);
212,114,020✔
454

455
  return status;
212,109,644✔
456
}
457

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

470
  e->value = value;
212,132,494✔
471
  e->flags = 0;
212,132,658✔
472
  e->deleter = deleter;
212,133,110✔
473
  e->overwriter = overwriter;
212,133,205✔
474
  e->ud = ud;
212,138,086✔
475
  e->keyLength = keyLen;
212,131,964✔
476
  e->hash = hash;
212,124,332✔
477
  e->refs = 0;
212,129,211✔
478
  e->next = e->prev = NULL;
212,123,165✔
479
  TAOS_LRU_ENTRY_SET_IN_CACHE(e, true);
212,135,142✔
480

481
  TAOS_LRU_ENTRY_SET_PRIORITY(e, priority);
212,124,246✔
482
  memcpy(e->keyData, key, keyLen);
212,132,755✔
483
  // TODO: e->CalcTotalCharge(charge, metadataChargePolicy);
484
  e->totalCharge = charge;
212,125,899✔
485

486
  return taosLRUCacheShardInsertEntry(shard, e, handle, true);
212,135,595✔
487
}
488

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

492
  (void)taosThreadMutexLock(&shard->mutex);
265,877,194✔
493
  e = taosLRUEntryTableLookup(&shard->table, key, keyLen, hash);
265,905,855✔
494
  if (e != NULL) {
265,933,078✔
495
    if (!TAOS_LRU_ENTRY_HAS_REFS(e)) {
58,205,507✔
496
      taosLRUCacheShardLRURemove(shard, e);
58,181,851✔
497
    }
498
    TAOS_LRU_ENTRY_REF(e);
58,203,133✔
499
    TAOS_LRU_ENTRY_SET_HIT(e);
58,201,956✔
500
  }
501

502
  (void)taosThreadMutexUnlock(&shard->mutex);
265,930,071✔
503

504
  return (LRUHandle *)e;
265,942,034✔
505
}
506

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

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

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

522
  (void)taosThreadMutexUnlock(&shard->mutex);
250,028✔
523

524
  if (lastReference) {
250,028✔
525
    taosLRUEntryFree(e);
1,087✔
526
  }
527
}
250,028✔
528

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

532
  (void)taosThreadMutexLock(&shard->mutex);
1,118,643✔
533

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

536
  (void)taosThreadMutexUnlock(&shard->mutex);
1,119,893✔
537

538
  return ret;
1,119,893✔
539
}
540

541
static void taosLRUCacheShardEraseUnrefEntries(SLRUCacheShard *shard) {
81,103,637✔
542
  SArray *lastReferenceList = taosArrayInit(16, POINTER_BYTES);
81,103,637✔
543

544
  (void)taosThreadMutexLock(&shard->mutex);
81,103,492✔
545

546
  while (shard->lru.next != &shard->lru) {
94,999,026✔
547
    SLRUEntry *old = shard->lru.next;
13,894,715✔
548
    taosLRUCacheShardLRURemove(shard, old);
13,894,715✔
549
    SLRUEntry *tentry = taosLRUEntryTableRemove(&shard->table, old->keyData, old->keyLength, old->hash);
13,894,715✔
550
    TAOS_LRU_ENTRY_SET_IN_CACHE(old, false);
13,894,715✔
551
    shard->usage -= old->totalCharge;
13,894,715✔
552

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

559
  (void)taosThreadMutexUnlock(&shard->mutex);
81,104,425✔
560

561
  for (int i = 0; i < taosArrayGetSize(lastReferenceList); ++i) {
94,998,494✔
562
    SLRUEntry *entry = taosArrayGetP(lastReferenceList, i);
13,894,715✔
563

564
    taosLRUEntryFree(entry);
13,894,715✔
565
  }
566

567
  taosArrayDestroy(lastReferenceList);
81,104,235✔
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) {
58,199,814✔
582
  if (handle == NULL) {
58,199,814✔
583
    return false;
×
584
  }
585

586
  SLRUEntry *e = (SLRUEntry *)handle;
58,199,814✔
587
  bool       lastReference = false;
58,199,814✔
588

589
  (void)taosThreadMutexLock(&shard->mutex);
58,199,814✔
590

591
  lastReference = taosLRUEntryUnref(e);
58,207,098✔
592
  if (lastReference && TAOS_LRU_ENTRY_IN_CACHE(e)) {
58,204,351✔
593
    if (shard->usage > shard->capacity || eraseIfLastRef) {
46,537,462✔
594
      SLRUEntry *tentry = taosLRUEntryTableRemove(&shard->table, e->keyData, e->keyLength, e->hash);
171,787✔
595
      TAOS_LRU_ENTRY_SET_IN_CACHE(e, false);
173,812✔
596
    } else {
597
      taosLRUCacheShardLRUInsert(shard, e);
46,360,974✔
598

599
      lastReference = false;
46,355,116✔
600
    }
601
  }
602

603
  if (lastReference && e->value) {
58,194,312✔
604
    shard->usage -= e->totalCharge;
11,817,013✔
605
  }
606

607
  (void)taosThreadMutexUnlock(&shard->mutex);
58,194,272✔
608

609
  if (lastReference) {
58,207,554✔
610
    taosLRUEntryFree(e);
11,817,053✔
611
  }
612

613
  return lastReference;
58,206,857✔
614
}
615

616
static size_t taosLRUCacheShardGetUsage(SLRUCacheShard *shard) {
119,552,569✔
617
  size_t usage = 0;
119,552,569✔
618

619
  (void)taosThreadMutexLock(&shard->mutex);
119,552,569✔
620
  usage = shard->usage;
119,552,569✔
621
  (void)taosThreadMutexUnlock(&shard->mutex);
119,552,569✔
622

623
  return usage;
119,552,569✔
624
}
625

626
static int32_t taosLRUCacheShardGetElems(SLRUCacheShard *shard) {
128,355,017✔
627
  int32_t elems = 0;
128,355,017✔
628

629
  (void)taosThreadMutexLock(&shard->mutex);
128,355,017✔
630
  elems = shard->table.elems;
128,355,017✔
631
  (void)taosThreadMutexUnlock(&shard->mutex);
128,355,017✔
632

633
  return elems;
128,355,017✔
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) {
188,606,591✔
649
  (void)taosThreadMutexLock(&shard->mutex);
188,606,591✔
650

651
  shard->strictCapacity = strict;
188,684,344✔
652

653
  (void)taosThreadMutexUnlock(&shard->mutex);
188,662,110✔
654
}
188,689,255✔
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) {
148,576,128✔
671
  int    numShardBits = 0;
148,576,128✔
672
  size_t minShardSize = 512 * 1024;
148,576,128✔
673
  size_t numShards = capacity / minShardSize;
148,576,128✔
674
  while (numShards >>= 1) {
216,709,639✔
675
    if (++numShardBits >= 6) {
68,270,329✔
676
      return numShardBits;
136,818✔
677
    }
678
  }
679

680
  return numShardBits;
148,439,310✔
681
}
682

683
SLRUCache *taosLRUCacheInit(size_t capacity, int numShardBits, double highPriPoolRatio) {
157,571,870✔
684
  if (numShardBits >= 20) {
157,571,870✔
685
    return NULL;
×
686
  }
687
  if (highPriPoolRatio < 0.0 || highPriPoolRatio > 1.0) {
157,571,870✔
688
    return NULL;
159,931✔
689
  }
690
  SLRUCache *cache = taosMemoryCalloc(1, sizeof(SLRUCache));
157,411,939✔
691
  if (!cache) {
157,438,263✔
692
    return NULL;
×
693
  }
694

695
  if (numShardBits < 0) {
157,438,263✔
696
    numShardBits = getDefaultCacheShardBits(capacity);
148,518,444✔
697
  }
698

699
  int numShards = 1 << numShardBits;
157,472,839✔
700
  cache->shards = taosMemoryCalloc(numShards, sizeof(SLRUCacheShard));
157,472,839✔
701
  if (!cache->shards) {
157,442,917✔
702
    taosMemoryFree(cache);
×
703
    return NULL;
×
704
  }
705

706
  bool   strictCapacity = 1;
157,474,649✔
707
  size_t perShard = (capacity + (numShards - 1)) / numShards;
157,474,649✔
708
  for (int i = 0; i < numShards; ++i) {
509,679,101✔
709
    if (TSDB_CODE_SUCCESS !=
352,204,452✔
710
        taosLRUCacheShardInit(&cache->shards[i], perShard, strictCapacity, highPriPoolRatio, 32 - numShardBits)) {
352,197,109✔
711
      taosMemoryFree(cache->shards);
×
712
      taosMemoryFree(cache);
×
713
      return NULL;
×
714
    }
715
  }
716

717
  cache->numShards = numShards;
157,481,992✔
718

719
  cache->shardedCache.shardMask = (1 << numShardBits) - 1;
157,596,728✔
720
  cache->shardedCache.strictCapacity = strictCapacity;
157,570,181✔
721
  cache->shardedCache.capacity = capacity;
157,573,285✔
722
  cache->shardedCache.lastId = 1;
157,511,466✔
723

724
  (void)taosThreadMutexInit(&cache->shardedCache.capacityMutex, NULL);
157,557,838✔
725

726
  return cache;
157,554,696✔
727
}
728

729
void taosLRUCacheCleanup(SLRUCache *cache) {
158,051,405✔
730
  if (cache) {
158,051,405✔
731
    if (cache->shards) {
157,618,931✔
732
      int numShards = cache->numShards;
157,624,110✔
733
      for (int i = 0; i < numShards; ++i) {
509,879,700✔
734
        taosLRUCacheShardCleanup(&cache->shards[i]);
352,250,747✔
735
      }
736
      taosMemoryFree(cache->shards);
157,628,953✔
737
      cache->shards = 0;
157,628,150✔
738
    }
739

740
    (void)taosThreadMutexDestroy(&cache->shardedCache.capacityMutex);
157,630,859✔
741

742
    taosMemoryFree(cache);
157,629,287✔
743
  }
744
}
158,061,942✔
745

746
LRUStatus taosLRUCacheInsert(SLRUCache *cache, const void *key, size_t keyLen, void *value, size_t charge,
212,138,631✔
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);
212,138,631✔
750
  uint32_t shardIndex = hash & cache->shardedCache.shardMask;
212,145,171✔
751

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

756
LRUHandle *taosLRUCacheLookup(SLRUCache *cache, const void *key, size_t keyLen) {
265,780,475✔
757
  uint32_t hash = TAOS_LRU_CACHE_SHARD_HASH32(key, keyLen);
265,780,475✔
758
  uint32_t shardIndex = hash & cache->shardedCache.shardMask;
265,890,709✔
759

760
  return taosLRUCacheShardLookup(&cache->shards[shardIndex], key, keyLen, hash);
265,900,303✔
761
}
762

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

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

770
void taosLRUCacheApply(SLRUCache *cache, _taos_lru_functor_t functor, void *ud) {
1,118,643✔
771
  int numShards = cache->numShards;
1,118,643✔
772
  for (int i = 0; i < numShards; ++i) {
2,239,137✔
773
    if (taosLRUCacheShardApply(&cache->shards[i], functor, ud)) {
1,119,268✔
774
      break;
×
775
    }
776
  }
777
}
1,119,869✔
778

779
void taosLRUCacheEraseUnrefEntries(SLRUCache *cache) {
13,124,991✔
780
  int numShards = cache->numShards;
13,124,991✔
781
  for (int i = 0; i < numShards; ++i) {
94,228,333✔
782
    taosLRUCacheShardEraseUnrefEntries(&cache->shards[i]);
81,103,742✔
783
  }
784
}
13,124,591✔
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) {
58,206,482✔
798
  if (handle == NULL) {
58,206,482✔
799
    return false;
×
800
  }
801

802
  uint32_t hash = ((SLRUEntry *)handle)->hash;
58,206,482✔
803
  uint32_t shardIndex = hash & cache->shardedCache.shardMask;
58,205,131✔
804

805
  return taosLRUCacheShardRelease(&cache->shards[shardIndex], handle, eraseIfLastRef);
58,204,163✔
806
}
807

808
void *taosLRUCacheValue(SLRUCache *cache, LRUHandle *handle) { return ((SLRUEntry *)handle)->value; }
58,027,656✔
809

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

812
size_t taosLRUCacheGetUsage(SLRUCache *cache) {
119,525,479✔
813
  size_t usage = 0;
119,525,479✔
814

815
  for (int i = 0; i < cache->numShards; ++i) {
239,078,048✔
816
    usage += taosLRUCacheShardGetUsage(&cache->shards[i]);
119,552,569✔
817
  }
818

819
  return usage;
119,525,479✔
820
}
821

822
int32_t taosLRUCacheGetElems(SLRUCache *cache) {
119,659,631✔
823
  int32_t elems = 0;
119,659,631✔
824

825
  for (int i = 0; i < cache->numShards; ++i) {
248,014,648✔
826
    elems += taosLRUCacheShardGetElems(&cache->shards[i]);
128,355,017✔
827
  }
828

829
  return elems;
119,659,631✔
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,580✔
843
  uint32_t numShards = cache->numShards;
5,580✔
844
  size_t   perShard = (capacity + (numShards - 1)) / numShards;
5,580✔
845

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

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

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

854
  (void)taosThreadMutexUnlock(&cache->shardedCache.capacityMutex);
5,580✔
855
}
5,580✔
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) {
120,631,897✔
870
  uint32_t numShards = cache->numShards;
120,631,897✔
871

872
  (void)taosThreadMutexLock(&cache->shardedCache.capacityMutex);
120,671,137✔
873

874
  for (int i = 0; i < numShards; ++i) {
309,302,459✔
875
    taosLRUCacheShardSetStrictCapacity(&cache->shards[i], strict);
188,613,823✔
876
  }
877

878
  cache->shardedCache.strictCapacity = strict;
120,688,636✔
879

880
  (void)taosThreadMutexUnlock(&cache->shardedCache.capacityMutex);
120,672,961✔
881
}
120,685,179✔
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