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

taosdata / TDengine / #3612

14 Feb 2025 06:43AM UTC coverage: 63.513% (+0.06%) from 63.456%
#3612

push

travis-ci

web-flow
Merge pull request #29757 from taosdata/feat/TS-5486

feat: support interp function with time range and time interval

141465 of 286269 branches covered (49.42%)

Branch coverage included in aggregate %.

220292 of 283307 relevant lines covered (77.76%)

18570887.65 hits per line

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

69.2
/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) {
3,930,616✔
91
  --entry->refs;
3,930,616✔
92
  return entry->refs == 0;
3,930,616✔
93
}
94

95
static void taosLRUEntryFree(SLRUEntry *entry) {
2,126,938✔
96
  if (entry->deleter) {
2,126,938!
97
    (*entry->deleter)(entry->keyData, entry->keyLength, entry->value, entry->ud);
2,126,962✔
98
  }
99

100
  taosMemoryFree(entry);
2,128,505!
101
}
2,128,593✔
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) {
5,300,506✔
113
  table->lengthBits = 16;
5,300,506✔
114
  table->list = taosMemoryCalloc(1 << table->lengthBits, sizeof(SLRUEntry *));
5,300,506!
115
  if (!table->list) {
5,314,137!
116
    TAOS_RETURN(terrno);
×
117
  }
118

119
  table->elems = 0;
5,314,137✔
120
  table->maxLengthBits = maxUpperHashBits;
5,314,137✔
121

122
  TAOS_RETURN(TSDB_CODE_SUCCESS);
5,314,137✔
123
}
124

125
static void taosLRUEntryTableApply(SLRUEntryTable *table, _taos_lru_table_func_t func, uint32_t begin, uint32_t end) {
5,314,064✔
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;
2,070,679✔
130
      func(h);
2,070,679✔
131
      h = n;
2,071,700✔
132
    }
133
  }
134
}
5,315,085✔
135

136
static void taosLRUEntryTableFree(SLRUEntry *entry) {
2,070,711✔
137
  if (!TAOS_LRU_ENTRY_HAS_REFS(entry)) {
2,070,711!
138
    taosLRUEntryFree(entry);
2,070,796✔
139
  }
140
}
2,071,596✔
141

142
static void taosLRUEntryTableCleanup(SLRUEntryTable *table) {
5,314,354✔
143
  taosLRUEntryTableApply(table, taosLRUEntryTableFree, 0, 1 << table->lengthBits);
5,314,354✔
144

145
  taosMemoryFree(table->list);
5,315,705!
146
}
5,315,823✔
147

148
static int taosLRUEntryTableApplyF(SLRUEntryTable *table, _taos_lru_functor_t functor, void *ud) {
6,579✔
149
  int      ret = 0;
6,579✔
150
  uint32_t end = 1 << table->lengthBits;
6,579✔
151
  for (uint32_t i = 0; i < end; ++i) {
331,098,383✔
152
    SLRUEntry *h = table->list[i];
331,091,833✔
153
    while (h) {
331,409,211✔
154
      SLRUEntry *n = h->nextHash;
317,407✔
155
      ret = functor(h->keyData, h->keyLength, h->value, ud);
317,407✔
156
      if (ret) {
317,378!
157
        return ret;
×
158
      }
159
      h = n;
317,378✔
160
    }
161
  }
162

163
  return ret;
6,550✔
164
}
165

166
static SLRUEntry **taosLRUEntryTableFindPtr(SLRUEntryTable *table, const void *key, size_t keyLen, uint32_t hash) {
8,977,745✔
167
  SLRUEntry **entry = &table->list[hash >> (32 - table->lengthBits)];
8,977,745✔
168
  while (*entry && ((*entry)->hash != hash || memcmp(key, (*entry)->keyData, keyLen) != 0)) {
8,994,914✔
169
    entry = &(*entry)->nextHash;
17,169✔
170
  }
171

172
  return entry;
8,977,745✔
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) {
6,816,733✔
211
  return *taosLRUEntryTableFindPtr(table, key, keyLen, hash);
6,816,733✔
212
}
213

214
static SLRUEntry *taosLRUEntryTableInsert(SLRUEntryTable *table, SLRUEntry *entry) {
2,128,218✔
215
  SLRUEntry **ptr = taosLRUEntryTableFindPtr(table, entry->keyData, entry->keyLength, entry->hash);
2,128,218✔
216
  SLRUEntry  *old = *ptr;
2,128,317✔
217
  entry->nextHash = (old == NULL) ? NULL : old->nextHash;
2,128,317✔
218
  *ptr = entry;
2,128,317✔
219
  if (old == NULL) {
2,128,317✔
220
    ++table->elems;
2,104,360✔
221
    if ((table->elems >> table->lengthBits) > 0) {
2,104,360!
222
      taosLRUEntryTableResize(table);
×
223
    }
224
  }
225

226
  return old;
2,128,397✔
227
}
228

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

237
  return result;
33,310✔
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) {
3,906,375✔
257
  while (shard->highPriPoolUsage > shard->highPriPoolCapacity) {
3,906,375!
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
}
3,906,375✔
264

265
static void taosLRUCacheShardLRUInsert(SLRUCacheShard *shard, SLRUEntry *e) {
6,034,287✔
266
  if (shard->highPriPoolRatio > 0 && (TAOS_LRU_ENTRY_IS_HIGH_PRI(e) || TAOS_LRU_ENTRY_HAS_HIT(e))) {
6,034,287!
267
    e->next = &shard->lru;
3,906,386✔
268
    e->prev = shard->lru.prev;
3,906,386✔
269

270
    e->prev->next = e;
3,906,386✔
271
    e->next->prev = e;
3,906,386✔
272

273
    TAOS_LRU_ENTRY_SET_IN_HIGH_POOL(e, true);
3,906,386✔
274
    shard->highPriPoolUsage += e->totalCharge;
3,906,386✔
275
    taosLRUCacheShardMaintainPoolSize(shard);
3,906,386✔
276
  } else {
277
    e->next = shard->lruLowPri->next;
2,127,901✔
278
    e->prev = shard->lruLowPri;
2,127,901✔
279

280
    e->prev->next = e;
2,127,901✔
281
    e->next->prev = e;
2,127,901✔
282

283
    TAOS_LRU_ENTRY_SET_IN_HIGH_POOL(e, false);
2,127,901✔
284
    shard->lruLowPri = e;
2,127,901✔
285
  }
286

287
  shard->lruUsage += e->totalCharge;
6,034,285✔
288
}
6,034,285✔
289

290
static void taosLRUCacheShardLRURemove(SLRUCacheShard *shard, SLRUEntry *e) {
3,962,439✔
291
  if (shard->lruLowPri == e) {
3,962,439✔
292
    shard->lruLowPri = e->prev;
603,170✔
293
  }
294
  e->next->prev = e->prev;
3,962,439✔
295
  e->prev->next = e->next;
3,962,439✔
296
  e->prev = e->next = NULL;
3,962,439✔
297

298
  shard->lruUsage -= e->totalCharge;
3,962,439✔
299
  if (TAOS_LRU_ENTRY_IN_HIGH_POOL(e)) {
3,962,439✔
300
    shard->highPriPoolUsage -= e->totalCharge;
3,255,144✔
301
  }
302
}
3,962,439✔
303

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

319
static void taosLRUCacheShardSetCapacity(SLRUCacheShard *shard, size_t capacity) {
5,312,665✔
320
  SArray *lastReferenceList = taosArrayInit(16, POINTER_BYTES);
5,312,665✔
321
  if (!lastReferenceList) {
5,312,401!
322
    return;
×
323
  }
324

325
  (void)taosThreadMutexLock(&shard->mutex);
5,312,401✔
326

327
  shard->capacity = capacity;
5,315,684✔
328
  shard->highPriPoolCapacity = capacity * shard->highPriPoolRatio;
5,315,684✔
329
  taosLRUCacheShardEvictLRU(shard, 0, lastReferenceList);
5,315,684✔
330

331
  (void)taosThreadMutexUnlock(&shard->mutex);
5,312,888✔
332

333
  for (int i = 0; i < taosArrayGetSize(lastReferenceList); ++i) {
5,315,934!
334
    SLRUEntry *entry = taosArrayGetP(lastReferenceList, i);
×
335
    taosLRUEntryFree(entry);
×
336
  }
337
  taosArrayDestroy(lastReferenceList);
5,311,051✔
338
}
339

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

344
  (void)taosThreadMutexInit(&shard->mutex, NULL);
5,313,894✔
345

346
  (void)taosThreadMutexLock(&shard->mutex);
5,312,206✔
347
  shard->capacity = 0;
5,315,550✔
348
  shard->highPriPoolUsage = 0;
5,315,550✔
349
  shard->strictCapacity = strict;
5,315,550✔
350
  shard->highPriPoolRatio = highPriPoolRatio;
5,315,550✔
351
  shard->highPriPoolCapacity = 0;
5,315,550✔
352

353
  shard->usage = 0;
5,315,550✔
354
  shard->lruUsage = 0;
5,315,550✔
355

356
  shard->lru.next = &shard->lru;
5,315,550✔
357
  shard->lru.prev = &shard->lru;
5,315,550✔
358
  shard->lruLowPri = &shard->lru;
5,315,550✔
359
  (void)taosThreadMutexUnlock(&shard->mutex);
5,315,550✔
360

361
  taosLRUCacheShardSetCapacity(shard, capacity);
5,314,351✔
362

363
  TAOS_RETURN(TSDB_CODE_SUCCESS);
5,313,933✔
364
}
365

366
static void taosLRUCacheShardCleanup(SLRUCacheShard *shard) {
5,314,675✔
367
  (void)taosThreadMutexDestroy(&shard->mutex);
5,314,675✔
368

369
  taosLRUEntryTableCleanup(&shard->table);
5,315,359✔
370
}
5,315,813✔
371

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

387
  if (shard->usage + e->totalCharge > shard->capacity && shard->lru.next != &shard->lru) {
2,128,661!
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)) {
2,128,661!
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);
2,128,661✔
414
    shard->usage += e->totalCharge;
2,128,279✔
415
    if (old != NULL) {
2,128,279✔
416
      status = TAOS_LRU_STATUS_OK_OVERWRITTEN;
24,074✔
417

418
      if (old->overwriter) {
24,074!
419
        (*old->overwriter)(old->keyData, old->keyLength, old->value, old->ud);
24,074✔
420
      }
421

422
      TAOS_LRU_ENTRY_SET_IN_CACHE(old, false);
23,951✔
423
      if (!TAOS_LRU_ENTRY_HAS_REFS(old)) {
23,951✔
424
        taosLRUCacheShardLRURemove(shard, old);
48✔
425
        shard->usage -= old->totalCharge;
48✔
426

427
        toFree = old;
48✔
428
      }
429
    }
430
    if (handle == NULL) {
2,128,156!
431
      taosLRUCacheShardLRUInsert(shard, e);
2,128,156✔
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:
2,128,197✔
442
  (void)taosThreadMutexUnlock(&shard->mutex);
2,128,197✔
443

444
  if (toFree) {
2,128,630✔
445
    taosLRUEntryFree(toFree);
48✔
446
  }
447

448
  for (int i = 0; i < taosArrayGetSize(lastReferenceList); ++i) {
2,128,644!
449
    SLRUEntry *entry = taosArrayGetP(lastReferenceList, i);
×
450

451
    taosLRUEntryFree(entry);
×
452
  }
453
  taosArrayDestroy(lastReferenceList);
2,128,275✔
454

455
  return status;
2,128,137✔
456
}
457

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

470
  e->value = value;
2,128,480✔
471
  e->flags = 0;
2,128,480✔
472
  e->deleter = deleter;
2,128,480✔
473
  e->overwriter = overwriter;
2,128,480✔
474
  e->ud = ud;
2,128,480✔
475
  e->keyLength = keyLen;
2,128,480✔
476
  e->hash = hash;
2,128,480✔
477
  e->refs = 0;
2,128,480✔
478
  e->next = e->prev = NULL;
2,128,480✔
479
  TAOS_LRU_ENTRY_SET_IN_CACHE(e, true);
2,128,480✔
480

481
  TAOS_LRU_ENTRY_SET_PRIORITY(e, priority);
2,128,480!
482
  memcpy(e->keyData, key, keyLen);
2,128,480✔
483
  // TODO: e->CalcTotalCharge(charge, metadataChargePolicy);
484
  e->totalCharge = charge;
2,128,480✔
485

486
  return taosLRUCacheShardInsertEntry(shard, e, handle, true);
2,128,480✔
487
}
488

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

492
  (void)taosThreadMutexLock(&shard->mutex);
6,816,290✔
493
  e = taosLRUEntryTableLookup(&shard->table, key, keyLen, hash);
6,817,044✔
494
  if (e != NULL) {
6,816,696✔
495
    if (!TAOS_LRU_ENTRY_HAS_REFS(e)) {
3,930,630✔
496
      taosLRUCacheShardLRURemove(shard, e);
3,930,583✔
497
    }
498
    TAOS_LRU_ENTRY_REF(e);
3,930,420✔
499
    TAOS_LRU_ENTRY_SET_HIT(e);
3,930,420✔
500
  }
501

502
  (void)taosThreadMutexUnlock(&shard->mutex);
6,816,486✔
503

504
  return (LRUHandle *)e;
6,817,113✔
505
}
506

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

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

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

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

524
  if (lastReference) {
1,265✔
525
    taosLRUEntryFree(e);
3✔
526
  }
527
}
1,265✔
528

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

532
  (void)taosThreadMutexLock(&shard->mutex);
6,577✔
533

534
  ret = taosLRUEntryTableApplyF(&shard->table, functor, ud);
6,579✔
535

536
  (void)taosThreadMutexUnlock(&shard->mutex);
6,579✔
537

538
  return ret;
6,578✔
539
}
540

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

544
  (void)taosThreadMutexLock(&shard->mutex);
258,136✔
545

546
  while (shard->lru.next != &shard->lru) {
290,001✔
547
    SLRUEntry *old = shard->lru.next;
31,848✔
548
    taosLRUCacheShardLRURemove(shard, old);
31,848✔
549
    SLRUEntry *tentry = taosLRUEntryTableRemove(&shard->table, old->keyData, old->keyLength, old->hash);
31,840✔
550
    TAOS_LRU_ENTRY_SET_IN_CACHE(old, false);
31,848✔
551
    shard->usage -= old->totalCharge;
31,848✔
552

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

559
  (void)taosThreadMutexUnlock(&shard->mutex);
258,153✔
560

561
  for (int i = 0; i < taosArrayGetSize(lastReferenceList); ++i) {
290,015✔
562
    SLRUEntry *entry = taosArrayGetP(lastReferenceList, i);
31,861✔
563

564
    taosLRUEntryFree(entry);
31,861✔
565
  }
566

567
  taosArrayDestroy(lastReferenceList);
258,152✔
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) {
3,930,519✔
582
  if (handle == NULL) {
3,930,519!
583
    return false;
×
584
  }
585

586
  SLRUEntry *e = (SLRUEntry *)handle;
3,930,519✔
587
  bool       lastReference = false;
3,930,519✔
588

589
  (void)taosThreadMutexLock(&shard->mutex);
3,930,519✔
590

591
  lastReference = taosLRUEntryUnref(e);
3,930,712✔
592
  if (lastReference && TAOS_LRU_ENTRY_IN_CACHE(e)) {
3,930,628!
593
    if (shard->usage > shard->capacity || eraseIfLastRef) {
3,906,616!
594
      SLRUEntry *tentry = taosLRUEntryTableRemove(&shard->table, e->keyData, e->keyLength, e->hash);
185✔
595
      TAOS_LRU_ENTRY_SET_IN_CACHE(e, false);
196✔
596
    } else {
597
      taosLRUCacheShardLRUInsert(shard, e);
3,906,431✔
598

599
      lastReference = false;
3,906,363✔
600
    }
601
  }
602

603
  if (lastReference && e->value) {
3,930,571!
604
    shard->usage -= e->totalCharge;
24,223✔
605
  }
606

607
  (void)taosThreadMutexUnlock(&shard->mutex);
3,930,571✔
608

609
  if (lastReference) {
3,930,719✔
610
    taosLRUEntryFree(e);
24,223✔
611
  }
612

613
  return lastReference;
3,930,718✔
614
}
615

616
static size_t taosLRUCacheShardGetUsage(SLRUCacheShard *shard) {
1,063,082✔
617
  size_t usage = 0;
1,063,082✔
618

619
  (void)taosThreadMutexLock(&shard->mutex);
1,063,082✔
620
  usage = shard->usage;
1,063,082✔
621
  (void)taosThreadMutexUnlock(&shard->mutex);
1,063,082✔
622

623
  return usage;
1,063,082✔
624
}
625

626
static int32_t taosLRUCacheShardGetElems(SLRUCacheShard *shard) {
1,111,646✔
627
  int32_t elems = 0;
1,111,646✔
628

629
  (void)taosThreadMutexLock(&shard->mutex);
1,111,646✔
630
  elems = shard->table.elems;
1,111,646✔
631
  (void)taosThreadMutexUnlock(&shard->mutex);
1,111,646✔
632

633
  return elems;
1,111,646✔
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) {
3,085,418✔
649
  (void)taosThreadMutexLock(&shard->mutex);
3,085,418✔
650

651
  shard->strictCapacity = strict;
3,088,164✔
652

653
  (void)taosThreadMutexUnlock(&shard->mutex);
3,088,164✔
654
}
3,088,270✔
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) {
4,888,497✔
671
  int    numShardBits = 0;
4,888,497✔
672
  size_t minShardSize = 512 * 1024;
4,888,497✔
673
  size_t numShards = capacity / minShardSize;
4,888,497✔
674
  while (numShards >>= 1) {
5,009,605✔
675
    if (++numShardBits >= 6) {
121,108!
676
      return numShardBits;
×
677
    }
678
  }
679

680
  return numShardBits;
4,888,497✔
681
}
682

683
SLRUCache *taosLRUCacheInit(size_t capacity, int numShardBits, double highPriPoolRatio) {
4,933,318✔
684
  if (numShardBits >= 20) {
4,933,318!
685
    return NULL;
×
686
  }
687
  if (highPriPoolRatio < 0.0 || highPriPoolRatio > 1.0) {
4,933,318!
688
    return NULL;
×
689
  }
690
  SLRUCache *cache = taosMemoryCalloc(1, sizeof(SLRUCache));
4,939,566!
691
  if (!cache) {
4,947,189!
692
    return NULL;
×
693
  }
694

695
  if (numShardBits < 0) {
4,947,189✔
696
    numShardBits = getDefaultCacheShardBits(capacity);
4,897,522✔
697
  }
698

699
  int numShards = 1 << numShardBits;
4,940,616✔
700
  cache->shards = taosMemoryCalloc(numShards, sizeof(SLRUCacheShard));
4,940,616!
701
  if (!cache->shards) {
4,940,904!
702
    taosMemoryFree(cache);
×
703
    return NULL;
×
704
  }
705

706
  bool   strictCapacity = 1;
4,940,904✔
707
  size_t perShard = (capacity + (numShards - 1)) / numShards;
4,940,904✔
708
  for (int i = 0; i < numShards; ++i) {
10,252,225✔
709
    if (TSDB_CODE_SUCCESS !=
5,311,149!
710
        taosLRUCacheShardInit(&cache->shards[i], perShard, strictCapacity, highPriPoolRatio, 32 - numShardBits)) {
5,305,117✔
711
      taosMemoryFree(cache->shards);
×
712
      taosMemoryFree(cache);
×
713
      return NULL;
×
714
    }
715
  }
716

717
  cache->numShards = numShards;
4,947,108✔
718

719
  cache->shardedCache.shardMask = (1 << numShardBits) - 1;
4,947,108✔
720
  cache->shardedCache.strictCapacity = strictCapacity;
4,947,108✔
721
  cache->shardedCache.capacity = capacity;
4,947,108✔
722
  cache->shardedCache.lastId = 1;
4,947,108✔
723

724
  (void)taosThreadMutexInit(&cache->shardedCache.capacityMutex, NULL);
4,947,108✔
725

726
  return cache;
4,947,694✔
727
}
728

729
void taosLRUCacheCleanup(SLRUCache *cache) {
4,949,638✔
730
  if (cache) {
4,949,638!
731
    if (cache->shards) {
4,949,954!
732
      int numShards = cache->numShards;
4,950,351✔
733
      for (int i = 0; i < numShards; ++i) {
10,266,147✔
734
        taosLRUCacheShardCleanup(&cache->shards[i]);
5,315,459✔
735
      }
736
      taosMemoryFree(cache->shards);
4,950,688✔
737
      cache->shards = 0;
4,950,722✔
738
    }
739

740
    (void)taosThreadMutexDestroy(&cache->shardedCache.capacityMutex);
4,950,325✔
741

742
    taosMemoryFree(cache);
4,950,607!
743
  }
744
}
4,950,348✔
745

746
LRUStatus taosLRUCacheInsert(SLRUCache *cache, const void *key, size_t keyLen, void *value, size_t charge,
2,127,810✔
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);
2,127,810✔
750
  uint32_t shardIndex = hash & cache->shardedCache.shardMask;
2,128,194✔
751

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

756
LRUHandle *taosLRUCacheLookup(SLRUCache *cache, const void *key, size_t keyLen) {
6,815,864✔
757
  uint32_t hash = TAOS_LRU_CACHE_SHARD_HASH32(key, keyLen);
6,815,864✔
758
  uint32_t shardIndex = hash & cache->shardedCache.shardMask;
6,816,579✔
759

760
  return taosLRUCacheShardLookup(&cache->shards[shardIndex], key, keyLen, hash);
6,816,579✔
761
}
762

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

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

770
void taosLRUCacheApply(SLRUCache *cache, _taos_lru_functor_t functor, void *ud) {
6,577✔
771
  int numShards = cache->numShards;
6,577✔
772
  for (int i = 0; i < numShards; ++i) {
13,155✔
773
    if (taosLRUCacheShardApply(&cache->shards[i], functor, ud)) {
6,577!
774
      break;
×
775
    }
776
  }
777
}
6,578✔
778

779
void taosLRUCacheEraseUnrefEntries(SLRUCache *cache) {
62,466✔
780
  int numShards = cache->numShards;
62,466✔
781
  for (int i = 0; i < numShards; ++i) {
320,619✔
782
    taosLRUCacheShardEraseUnrefEntries(&cache->shards[i]);
258,147✔
783
  }
784
}
62,472✔
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) {
3,930,535✔
798
  if (handle == NULL) {
3,930,535!
799
    return false;
×
800
  }
801

802
  uint32_t hash = ((SLRUEntry *)handle)->hash;
3,930,535✔
803
  uint32_t shardIndex = hash & cache->shardedCache.shardMask;
3,930,535✔
804

805
  return taosLRUCacheShardRelease(&cache->shards[shardIndex], handle, eraseIfLastRef);
3,930,535✔
806
}
807

808
void *taosLRUCacheValue(SLRUCache *cache, LRUHandle *handle) { return ((SLRUEntry *)handle)->value; }
3,930,400✔
809

810
size_t taosLRUCacheGetUsage(SLRUCache *cache) {
1,063,082✔
811
  size_t usage = 0;
1,063,082✔
812

813
  for (int i = 0; i < cache->numShards; ++i) {
2,126,164✔
814
    usage += taosLRUCacheShardGetUsage(&cache->shards[i]);
1,063,082✔
815
  }
816

817
  return usage;
1,063,082✔
818
}
819

820
int32_t taosLRUCacheGetElems(SLRUCache *cache) {
1,111,646✔
821
  int32_t elems = 0;
1,111,646✔
822

823
  for (int i = 0; i < cache->numShards; ++i) {
2,223,292✔
824
    elems += taosLRUCacheShardGetElems(&cache->shards[i]);
1,111,646✔
825
  }
826

827
  return elems;
1,111,646✔
828
}
829

830
size_t taosLRUCacheGetPinnedUsage(SLRUCache *cache) {
×
831
  size_t usage = 0;
×
832

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

837
  return usage;
×
838
}
839

840
void taosLRUCacheSetCapacity(SLRUCache *cache, size_t capacity) {
254✔
841
  uint32_t numShards = cache->numShards;
254✔
842
  size_t   perShard = (capacity + (numShards - 1)) / numShards;
254✔
843

844
  (void)taosThreadMutexLock(&cache->shardedCache.capacityMutex);
254✔
845

846
  for (int i = 0; i < numShards; ++i) {
508✔
847
    taosLRUCacheShardSetCapacity(&cache->shards[i], perShard);
254✔
848
  }
849

850
  cache->shardedCache.capacity = capacity;
254✔
851

852
  (void)taosThreadMutexUnlock(&cache->shardedCache.capacityMutex);
254✔
853
}
254✔
854

855
size_t taosLRUCacheGetCapacity(SLRUCache *cache) {
×
856
  size_t capacity = 0;
×
857

858
  (void)taosThreadMutexLock(&cache->shardedCache.capacityMutex);
×
859

860
  capacity = cache->shardedCache.capacity;
×
861

862
  (void)taosThreadMutexUnlock(&cache->shardedCache.capacityMutex);
×
863

864
  return capacity;
×
865
}
866

867
void taosLRUCacheSetStrictCapacity(SLRUCache *cache, bool strict) {
2,887,982✔
868
  uint32_t numShards = cache->numShards;
2,887,982✔
869

870
  (void)taosThreadMutexLock(&cache->shardedCache.capacityMutex);
2,887,982✔
871

872
  for (int i = 0; i < numShards; ++i) {
5,980,924✔
873
    taosLRUCacheShardSetStrictCapacity(&cache->shards[i], strict);
3,088,222✔
874
  }
875

876
  cache->shardedCache.strictCapacity = strict;
2,892,702✔
877

878
  (void)taosThreadMutexUnlock(&cache->shardedCache.capacityMutex);
2,892,702✔
879
}
2,892,670✔
880

881
bool taosLRUCacheIsStrictCapacity(SLRUCache *cache) {
×
882
  bool strict = false;
×
883

884
  (void)taosThreadMutexLock(&cache->shardedCache.capacityMutex);
×
885

886
  strict = cache->shardedCache.strictCapacity;
×
887

888
  (void)taosThreadMutexUnlock(&cache->shardedCache.capacityMutex);
×
889

890
  return strict;
×
891
}
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