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

taosdata / TDengine / #3522

07 Nov 2024 05:59AM UTC coverage: 58.216% (+1.3%) from 56.943%
#3522

push

travis-ci

web-flow
Merge pull request #28663 from taosdata/fix/3_liaohj

fix(stream): stop the underlying scan operations for stream

111884 of 248391 branches covered (45.04%)

Branch coverage included in aggregate %.

3 of 4 new or added lines in 1 file covered. (75.0%)

1164 existing lines in 134 files now uncovered.

191720 of 273118 relevant lines covered (70.2%)

13088725.13 hits per line

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

69.66
/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) {
11,180,422✔
91
  --entry->refs;
11,180,422✔
92
  return entry->refs == 0;
11,180,422✔
93
}
94

95
static void taosLRUEntryFree(SLRUEntry *entry) {
9,909,702✔
96
  if (entry->deleter) {
9,909,702!
97
    (*entry->deleter)(entry->keyData, entry->keyLength, entry->value, entry->ud);
9,910,053✔
98
  }
99

100
  taosMemoryFree(entry);
9,915,833✔
101
}
9,916,600✔
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) {
3,925,842✔
113
  table->lengthBits = 16;
3,925,842✔
114
  table->list = taosMemoryCalloc(1 << table->lengthBits, sizeof(SLRUEntry *));
3,925,842✔
115
  if (!table->list) {
3,935,229!
116
    TAOS_RETURN(terrno);
×
117
  }
118

119
  table->elems = 0;
3,935,229✔
120
  table->maxLengthBits = maxUpperHashBits;
3,935,229✔
121

122
  TAOS_RETURN(TSDB_CODE_SUCCESS);
3,935,229✔
123
}
124

125
static void taosLRUEntryTableApply(SLRUEntryTable *table, _taos_lru_table_func_t func, uint32_t begin, uint32_t end) {
3,937,089✔
126
  for (uint32_t i = begin; i < end; ++i) {
2,147,483,647✔
127
    SLRUEntry *h = table->list[i];
2,147,483,647✔
128
    while (h) {
2,147,483,647✔
129
      SLRUEntry *n = h->nextHash;
1,252,231✔
130
      func(h);
1,252,231✔
131
      h = n;
1,252,611✔
132
    }
133
  }
134
}
3,937,469✔
135

136
static void taosLRUEntryTableFree(SLRUEntry *entry) {
1,252,215✔
137
  if (!TAOS_LRU_ENTRY_HAS_REFS(entry)) {
1,252,215!
138
    taosLRUEntryFree(entry);
1,252,234✔
139
  }
140
}
1,252,626✔
141

142
static void taosLRUEntryTableCleanup(SLRUEntryTable *table) {
3,937,466✔
143
  taosLRUEntryTableApply(table, taosLRUEntryTableFree, 0, 1 << table->lengthBits);
3,937,466✔
144

145
  taosMemoryFree(table->list);
3,938,284✔
146
}
3,938,323✔
147

148
static int taosLRUEntryTableApplyF(SLRUEntryTable *table, _taos_lru_functor_t functor, void *ud) {
56,503✔
149
  int      ret = 0;
56,503✔
150
  uint32_t end = 1 << table->lengthBits;
56,503✔
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;
509,551✔
155
      ret = functor(h->keyData, h->keyLength, h->value, ud);
509,551✔
156
      if (ret) {
509,517!
157
        return ret;
×
158
      }
159
      h = n;
509,517✔
160
    }
161
  }
162

163
  return ret;
56,469✔
164
}
165

166
static SLRUEntry **taosLRUEntryTableFindPtr(SLRUEntryTable *table, const void *key, size_t keyLen, uint32_t hash) {
22,875,961✔
167
  SLRUEntry **entry = &table->list[hash >> (32 - table->lengthBits)];
22,875,961✔
168
  while (*entry && ((*entry)->hash != hash || memcmp(key, (*entry)->keyData, keyLen) != 0)) {
22,910,390✔
169
    entry = &(*entry)->nextHash;
34,429✔
170
  }
171

172
  return entry;
22,875,961✔
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) {
12,949,309✔
211
  return *taosLRUEntryTableFindPtr(table, key, keyLen, hash);
12,949,309✔
212
}
213

214
static SLRUEntry *taosLRUEntryTableInsert(SLRUEntryTable *table, SLRUEntry *entry) {
9,914,489✔
215
  SLRUEntry **ptr = taosLRUEntryTableFindPtr(table, entry->keyData, entry->keyLength, entry->hash);
9,914,489✔
216
  SLRUEntry  *old = *ptr;
9,910,067✔
217
  entry->nextHash = (old == NULL) ? NULL : old->nextHash;
9,910,067✔
218
  *ptr = entry;
9,910,067✔
219
  if (old == NULL) {
9,910,067✔
220
    ++table->elems;
1,282,520✔
221
    if ((table->elems >> table->lengthBits) > 0) {
1,282,520!
222
      taosLRUEntryTableResize(table);
×
223
    }
224
  }
225

226
  return old;
9,910,160✔
227
}
228

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

237
  return result;
29,738✔
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) {
2,553,503✔
257
  while (shard->highPriPoolUsage > shard->highPriPoolCapacity) {
2,553,503!
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
}
2,553,503✔
264

265
static void taosLRUCacheShardLRUInsert(SLRUCacheShard *shard, SLRUEntry *e) {
12,448,650✔
266
  if (shard->highPriPoolRatio > 0 && (TAOS_LRU_ENTRY_IS_HIGH_PRI(e) || TAOS_LRU_ENTRY_HAS_HIT(e))) {
12,448,650!
267
    e->next = &shard->lru;
2,553,505✔
268
    e->prev = shard->lru.prev;
2,553,505✔
269

270
    e->prev->next = e;
2,553,505✔
271
    e->next->prev = e;
2,553,505✔
272

273
    TAOS_LRU_ENTRY_SET_IN_HIGH_POOL(e, true);
2,553,505✔
274
    shard->highPriPoolUsage += e->totalCharge;
2,553,505✔
275
    taosLRUCacheShardMaintainPoolSize(shard);
2,553,505✔
276
  } else {
277
    e->next = shard->lruLowPri->next;
9,895,145✔
278
    e->prev = shard->lruLowPri;
9,895,145✔
279

280
    e->prev->next = e;
9,895,145✔
281
    e->next->prev = e;
9,895,145✔
282

283
    TAOS_LRU_ENTRY_SET_IN_HIGH_POOL(e, false);
9,895,145✔
284
    shard->lruLowPri = e;
9,895,145✔
285
  }
286

287
  shard->lruUsage += e->totalCharge;
12,448,646✔
288
}
12,448,646✔
289

290
static void taosLRUCacheShardLRURemove(SLRUCacheShard *shard, SLRUEntry *e) {
11,199,816✔
291
  if (shard->lruLowPri == e) {
11,199,816✔
292
    shard->lruLowPri = e->prev;
619,720✔
293
  }
294
  e->next->prev = e->prev;
11,199,816✔
295
  e->prev->next = e->next;
11,199,816✔
296
  e->prev = e->next = NULL;
11,199,816✔
297

298
  shard->lruUsage -= e->totalCharge;
11,199,816✔
299
  if (TAOS_LRU_ENTRY_IN_HIGH_POOL(e)) {
11,199,816✔
300
    shard->highPriPoolUsage -= e->totalCharge;
1,899,083✔
301
  }
302
}
11,199,816✔
303

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

319
static void taosLRUCacheShardSetCapacity(SLRUCacheShard *shard, size_t capacity) {
3,936,143✔
320
  SArray *lastReferenceList = taosArrayInit(16, POINTER_BYTES);
3,936,143✔
321
  if (!lastReferenceList) {
3,936,666!
322
    return;
×
323
  }
324

325
  (void)taosThreadMutexLock(&shard->mutex);
3,936,666✔
326

327
  shard->capacity = capacity;
3,938,388✔
328
  shard->highPriPoolCapacity = capacity * shard->highPriPoolRatio;
3,938,388✔
329
  taosLRUCacheShardEvictLRU(shard, 0, lastReferenceList);
3,938,388✔
330

331
  (void)taosThreadMutexUnlock(&shard->mutex);
3,936,641✔
332

333
  for (int i = 0; i < taosArrayGetSize(lastReferenceList); ++i) {
3,938,339!
334
    SLRUEntry *entry = taosArrayGetP(lastReferenceList, i);
×
335
    taosLRUEntryFree(entry);
×
336
  }
337
  taosArrayDestroy(lastReferenceList);
3,936,550✔
338
}
339

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

344
  (void)taosThreadMutexInit(&shard->mutex, NULL);
3,935,242✔
345

346
  (void)taosThreadMutexLock(&shard->mutex);
3,935,182✔
347
  shard->capacity = 0;
3,937,963✔
348
  shard->highPriPoolUsage = 0;
3,937,963✔
349
  shard->strictCapacity = strict;
3,937,963✔
350
  shard->highPriPoolRatio = highPriPoolRatio;
3,937,963✔
351
  shard->highPriPoolCapacity = 0;
3,937,963✔
352

353
  shard->usage = 0;
3,937,963✔
354
  shard->lruUsage = 0;
3,937,963✔
355

356
  shard->lru.next = &shard->lru;
3,937,963✔
357
  shard->lru.prev = &shard->lru;
3,937,963✔
358
  shard->lruLowPri = &shard->lru;
3,937,963✔
359
  (void)taosThreadMutexUnlock(&shard->mutex);
3,937,963✔
360

361
  taosLRUCacheShardSetCapacity(shard, capacity);
3,937,095✔
362

363
  TAOS_RETURN(TSDB_CODE_SUCCESS);
3,937,192✔
364
}
365

366
static void taosLRUCacheShardCleanup(SLRUCacheShard *shard) {
3,937,751✔
367
  (void)taosThreadMutexDestroy(&shard->mutex);
3,937,751✔
368

369
  taosLRUEntryTableCleanup(&shard->table);
3,937,878✔
370
}
3,938,313✔
371

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

387
  if (shard->usage + e->totalCharge > shard->capacity && shard->lru.next != &shard->lru) {
9,918,543!
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)) {
9,918,543!
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);
9,918,543✔
414
    shard->usage += e->totalCharge;
9,908,747✔
415
    if (old != NULL) {
9,908,747✔
416
      status = TAOS_LRU_STATUS_OK_OVERWRITTEN;
8,627,062✔
417

418
      if (old->overwriter) {
8,627,062!
419
        (*old->overwriter)(old->keyData, old->keyLength, old->value, old->ud);
8,627,399✔
420
      }
421

422
      TAOS_LRU_ENTRY_SET_IN_CACHE(old, false);
8,614,191✔
423
      if (!TAOS_LRU_ENTRY_HAS_REFS(old)) {
8,614,191✔
424
        taosLRUCacheShardLRURemove(shard, old);
38✔
425
        shard->usage -= old->totalCharge;
38✔
426

427
        toFree = old;
38✔
428
      }
429
    }
430
    if (handle == NULL) {
9,895,876!
431
      taosLRUCacheShardLRUInsert(shard, e);
9,895,876✔
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:
9,892,212✔
442
  (void)taosThreadMutexUnlock(&shard->mutex);
9,892,212✔
443

444
  if (toFree) {
9,916,501✔
445
    taosLRUEntryFree(toFree);
38✔
446
  }
447

448
  for (int i = 0; i < taosArrayGetSize(lastReferenceList); ++i) {
9,916,917!
449
    SLRUEntry *entry = taosArrayGetP(lastReferenceList, i);
×
450

451
    taosLRUEntryFree(entry);
×
452
  }
453
  taosArrayDestroy(lastReferenceList);
9,912,701✔
454

455
  return status;
9,909,539✔
456
}
457

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

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

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

486
  return taosLRUCacheShardInsertEntry(shard, e, handle, true);
9,921,572✔
487
}
488

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

492
  (void)taosThreadMutexLock(&shard->mutex);
12,945,764✔
493
  e = taosLRUEntryTableLookup(&shard->table, key, keyLen, hash);
12,952,700✔
494
  if (e != NULL) {
12,937,287✔
495
    if (!TAOS_LRU_ENTRY_HAS_REFS(e)) {
11,170,911!
496
      taosLRUCacheShardLRURemove(shard, e);
11,171,311✔
497
    }
498
    TAOS_LRU_ENTRY_REF(e);
11,160,241✔
499
    TAOS_LRU_ENTRY_SET_HIT(e);
11,160,241✔
500
  }
501

502
  (void)taosThreadMutexUnlock(&shard->mutex);
12,926,617✔
503

504
  return (LRUHandle *)e;
12,954,431✔
505
}
506

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

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

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

522
  (void)taosThreadMutexUnlock(&shard->mutex);
6✔
523

524
  if (lastReference) {
6!
525
    taosLRUEntryFree(e);
×
526
  }
527
}
6✔
528

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

532
  (void)taosThreadMutexLock(&shard->mutex);
56,504✔
533

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

536
  (void)taosThreadMutexUnlock(&shard->mutex);
56,505✔
537

538
  return ret;
56,505✔
539
}
540

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

544
  (void)taosThreadMutexLock(&shard->mutex);
118,152✔
545

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

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

559
  (void)taosThreadMutexUnlock(&shard->mutex);
118,155✔
560

561
  for (int i = 0; i < taosArrayGetSize(lastReferenceList); ++i) {
147,879✔
562
    SLRUEntry *entry = taosArrayGetP(lastReferenceList, i);
29,726✔
563

564
    taosLRUEntryFree(entry);
29,726✔
565
  }
566

567
  taosArrayDestroy(lastReferenceList);
118,154✔
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) {
11,169,501✔
582
  if (handle == NULL) {
11,169,501!
583
    return false;
×
584
  }
585

586
  SLRUEntry *e = (SLRUEntry *)handle;
11,169,501✔
587
  bool       lastReference = false;
11,169,501✔
588

589
  (void)taosThreadMutexLock(&shard->mutex);
11,169,501✔
590

591
  lastReference = taosLRUEntryUnref(e);
11,183,338✔
592
  if (lastReference && TAOS_LRU_ENTRY_IN_CACHE(e)) {
11,176,373!
593
    if (shard->usage > shard->capacity || eraseIfLastRef) {
2,553,523!
594
      SLRUEntry *tentry = taosLRUEntryTableRemove(&shard->table, e->keyData, e->keyLength, e->hash);
1✔
595
      TAOS_LRU_ENTRY_SET_IN_CACHE(e, false);
6✔
596
    } else {
597
      taosLRUCacheShardLRUInsert(shard, e);
2,553,522✔
598

599
      lastReference = false;
2,553,492✔
600
    }
601
  }
602

603
  if (lastReference && e->value) {
11,176,348!
604
    shard->usage -= e->totalCharge;
8,623,584✔
605
  }
606

607
  (void)taosThreadMutexUnlock(&shard->mutex);
11,176,348✔
608

609
  if (lastReference) {
11,184,614✔
610
    taosLRUEntryFree(e);
8,631,550✔
611
  }
612

613
  return lastReference;
11,186,758✔
614
}
615

616
static size_t taosLRUCacheShardGetUsage(SLRUCacheShard *shard) {
838,703✔
617
  size_t usage = 0;
838,703✔
618

619
  (void)taosThreadMutexLock(&shard->mutex);
838,703✔
620
  usage = shard->usage;
838,703✔
621
  (void)taosThreadMutexUnlock(&shard->mutex);
838,703✔
622

623
  return usage;
838,703✔
624
}
625

626
static int32_t taosLRUCacheShardGetElems(SLRUCacheShard *shard) {
860,979✔
627
  int32_t elems = 0;
860,979✔
628

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

633
  return elems;
860,979✔
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) {
2,060,424✔
649
  (void)taosThreadMutexLock(&shard->mutex);
2,060,424✔
650

651
  shard->strictCapacity = strict;
2,062,030✔
652

653
  (void)taosThreadMutexUnlock(&shard->mutex);
2,062,030✔
654
}
2,062,076✔
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) {
3,739,753✔
671
  int    numShardBits = 0;
3,739,753✔
672
  size_t minShardSize = 512 * 1024;
3,739,753✔
673
  size_t numShards = capacity / minShardSize;
3,739,753✔
674
  while (numShards >>= 1) {
3,795,065✔
675
    if (++numShardBits >= 6) {
55,312!
676
      return numShardBits;
×
677
    }
678
  }
679

680
  return numShardBits;
3,739,753✔
681
}
682

683
SLRUCache *taosLRUCacheInit(size_t capacity, int numShardBits, double highPriPoolRatio) {
3,758,694✔
684
  if (numShardBits >= 20) {
3,758,694!
685
    return NULL;
×
686
  }
687
  if (highPriPoolRatio < 0.0 || highPriPoolRatio > 1.0) {
3,758,694!
688
    return NULL;
×
689
  }
690
  SLRUCache *cache = taosMemoryCalloc(1, sizeof(SLRUCache));
3,763,585✔
691
  if (!cache) {
3,768,662!
692
    return NULL;
×
693
  }
694

695
  if (numShardBits < 0) {
3,768,662✔
696
    numShardBits = getDefaultCacheShardBits(capacity);
3,744,873✔
697
  }
698

699
  int numShards = 1 << numShardBits;
3,765,156✔
700
  cache->shards = taosMemoryCalloc(numShards, sizeof(SLRUCacheShard));
3,765,156✔
701
  if (!cache->shards) {
3,763,461!
702
    taosMemoryFree(cache);
×
703
    return NULL;
×
704
  }
705

706
  bool   strictCapacity = 1;
3,765,227✔
707
  size_t perShard = (capacity + (numShards - 1)) / numShards;
3,765,227✔
708
  for (int i = 0; i < numShards; ++i) {
7,702,078✔
709
    if (TSDB_CODE_SUCCESS !=
3,936,880✔
710
        taosLRUCacheShardInit(&cache->shards[i], perShard, strictCapacity, highPriPoolRatio, 32 - numShardBits)) {
3,931,711✔
711
      taosMemoryFree(cache->shards);
29✔
712
      taosMemoryFree(cache);
×
713
      return NULL;
×
714
    }
715
  }
716

717
  cache->numShards = numShards;
3,770,367✔
718

719
  cache->shardedCache.shardMask = (1 << numShardBits) - 1;
3,770,367✔
720
  cache->shardedCache.strictCapacity = strictCapacity;
3,770,367✔
721
  cache->shardedCache.capacity = capacity;
3,770,367✔
722
  cache->shardedCache.lastId = 1;
3,770,367✔
723

724
  (void)taosThreadMutexInit(&cache->shardedCache.capacityMutex, NULL);
3,770,367✔
725

726
  return cache;
3,770,359✔
727
}
728

729
void taosLRUCacheCleanup(SLRUCache *cache) {
3,770,964✔
730
  if (cache) {
3,770,964!
731
    if (cache->shards) {
3,771,050!
732
      int numShards = cache->numShards;
3,771,242✔
733
      for (int i = 0; i < numShards; ++i) {
7,709,553✔
734
        taosLRUCacheShardCleanup(&cache->shards[i]);
3,938,102✔
735
      }
736
      taosMemoryFree(cache->shards);
3,771,451✔
737
      cache->shards = 0;
3,771,489✔
738
    }
739

740
    (void)taosThreadMutexDestroy(&cache->shardedCache.capacityMutex);
3,771,297✔
741

742
    taosMemoryFree(cache);
3,771,356✔
743
  }
744
}
3,771,339✔
745

746
LRUStatus taosLRUCacheInsert(SLRUCache *cache, const void *key, size_t keyLen, void *value, size_t charge,
9,905,736✔
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);
9,905,736✔
750
  uint32_t shardIndex = hash & cache->shardedCache.shardMask;
9,917,373✔
751

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

756
LRUHandle *taosLRUCacheLookup(SLRUCache *cache, const void *key, size_t keyLen) {
12,937,213✔
757
  uint32_t hash = TAOS_LRU_CACHE_SHARD_HASH32(key, keyLen);
12,937,213✔
758
  uint32_t shardIndex = hash & cache->shardedCache.shardMask;
12,948,777✔
759

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

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

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

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

779
void taosLRUCacheEraseUnrefEntries(SLRUCache *cache) {
28,686✔
780
  int numShards = cache->numShards;
28,686✔
781
  for (int i = 0; i < numShards; ++i) {
146,841✔
782
    taosLRUCacheShardEraseUnrefEntries(&cache->shards[i]);
118,155✔
783
  }
784
}
28,686✔
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) {
11,170,802✔
798
  if (handle == NULL) {
11,170,802!
799
    return false;
×
800
  }
801

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

805
  return taosLRUCacheShardRelease(&cache->shards[shardIndex], handle, eraseIfLastRef);
11,170,802✔
806
}
807

808
void *taosLRUCacheValue(SLRUCache *cache, LRUHandle *handle) { return ((SLRUEntry *)handle)->value; }
11,180,649✔
809

810
size_t taosLRUCacheGetUsage(SLRUCache *cache) {
838,703✔
811
  size_t usage = 0;
838,703✔
812

813
  for (int i = 0; i < cache->numShards; ++i) {
1,677,406✔
814
    usage += taosLRUCacheShardGetUsage(&cache->shards[i]);
838,703✔
815
  }
816

817
  return usage;
838,703✔
818
}
819

820
int32_t taosLRUCacheGetElems(SLRUCache *cache) {
860,979✔
821
  int32_t elems = 0;
860,979✔
822

823
  for (int i = 0; i < cache->numShards; ++i) {
1,721,958✔
824
    elems += taosLRUCacheShardGetElems(&cache->shards[i]);
860,979✔
825
  }
826

827
  return elems;
860,979✔
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) {
242✔
841
  uint32_t numShards = cache->numShards;
242✔
842
  size_t   perShard = (capacity + (numShards - 1)) / numShards;
242✔
843

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

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

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

852
  (void)taosThreadMutexUnlock(&cache->shardedCache.capacityMutex);
242✔
853
}
242✔
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) {
1,970,792✔
868
  uint32_t numShards = cache->numShards;
1,970,792✔
869

870
  (void)taosThreadMutexLock(&cache->shardedCache.capacityMutex);
1,970,792✔
871

872
  for (int i = 0; i < numShards; ++i) {
4,034,646✔
873
    taosLRUCacheShardSetStrictCapacity(&cache->shards[i], strict);
2,061,920✔
874
  }
875

876
  cache->shardedCache.strictCapacity = strict;
1,972,726✔
877

878
  (void)taosThreadMutexUnlock(&cache->shardedCache.capacityMutex);
1,972,726✔
879
}
1,972,690✔
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