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

taosdata / TDengine / #4720

08 Sep 2025 08:43AM UTC coverage: 58.139% (-0.6%) from 58.762%
#4720

push

travis-ci

web-flow
Merge pull request #32881 from taosdata/enh/add-new-windows-ci

fix(ci): update workflow reference to use new Windows CI YAML

133181 of 292179 branches covered (45.58%)

Branch coverage included in aggregate %.

201691 of 283811 relevant lines covered (71.07%)

5442780.71 hits per line

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

57.6
/source/util/src/tcache.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 "tcache.h"
18
#include "osThread.h"
19
#include "taoserror.h"
20
#include "tlog.h"
21
#include "tutil.h"
22

23
#define CACHE_MAX_CAPACITY     1024 * 1024 * 16
24
#define CACHE_DEFAULT_CAPACITY 1024 * 4
25

26
static TdThread      cacheRefreshWorker = {0};
27
static TdThreadOnce  cacheThreadInit = PTHREAD_ONCE_INIT;
28
static TdThreadMutex guard;
29
static SArray       *pCacheArrayList = NULL;
30
static bool          stopRefreshWorker = false;
31
static bool          refreshWorkerNormalStopped = false;
32
static bool          refreshWorkerUnexpectedStopped = false;
33

34
typedef struct SCacheNode {
35
  uint64_t           addedTime;   // the added time when this element is added or updated into cache
36
  uint64_t           lifespan;    // life duration when this element should be remove from cache
37
  int64_t            expireTime;  // expire time
38
  void              *signature;
39
  struct STrashElem *pTNodeHeader;    // point to trash node head
40
  uint16_t           keyLen : 15;     // max key size: 32kb
41
  bool               inTrashcan : 1;  // denote if it is in trash or not
42
  uint32_t           size;            // allocated size for current SCacheNode
43
  uint32_t           dataLen;
44
  T_REF_DECLARE()
45
  struct SCacheNode *pNext;
46
  char              *key;
47
  char              *data;
48
} SCacheNode;
49

50
typedef struct SCacheEntry {
51
  int32_t     num;    // number of elements in current entry
52
  SRWLatch    latch;  // entry latch
53
  SCacheNode *next;
54
} SCacheEntry;
55

56
struct STrashElem {
57
  struct STrashElem *prev;
58
  struct STrashElem *next;
59
  SCacheNode        *pData;
60
};
61

62
struct SCacheIter {
63
  SCacheObj   *pCacheObj;
64
  SCacheNode **pCurrent;
65
  int32_t      entryIndex;
66
  int32_t      index;
67
  int32_t      numOfObj;
68
};
69

70
/*
71
 * to accommodate the old data which has the same key value of new one in hashList
72
 * when an new node is put into cache, if an existed one with the same key:
73
 * 1. if the old one does not be referenced, update it.
74
 * 2. otherwise, move the old one to pTrash, addedTime the new one.
75
 *
76
 * when the node in pTrash does not be referenced, it will be release at the expired expiredTime
77
 */
78
struct SCacheObj {
79
  int64_t      sizeInBytes;  // total allocated buffer in this hash table, SCacheObj is not included.
80
  int64_t      refreshTime;
81
  char        *name;
82
  SCacheStatis statistics;
83

84
  SCacheEntry      *pEntryList;
85
  size_t            capacity;    // number of slots
86
  size_t            numOfElems;  // number of elements in cache
87
  _hash_fn_t        hashFp;      // hash function
88
  __cache_free_fn_t freeFp;
89

90
  uint32_t    numOfElemsInTrash;  // number of element in trash
91
  STrashElem *pTrash;
92

93
  uint8_t  deleting;  // set the deleting flag to stop refreshing ASAP.
94
  TdThread refreshWorker;
95
  bool     extendLifespan;  // auto extend life span when one item is accessed.
96
  int64_t  checkTick;       // tick used to record the check times of the refresh threads
97
#if defined(LINUX)
98
  TdThreadRwlock lock;
99
#else
100
  TdThreadMutex lock;
101
#endif
102
};
103

104
typedef struct SCacheObjTravSup {
105
  SCacheObj        *pCacheObj;
106
  int64_t           time;
107
  __cache_trav_fn_t fp;
108
  void             *param1;
109
} SCacheObjTravSup;
110

111
static FORCE_INLINE void __trashcan_wr_lock(SCacheObj *pCacheObj) {
112
#if defined(LINUX)
113
  (void)taosThreadRwlockWrlock(&pCacheObj->lock);
9✔
114
#else
115
  (void)taosThreadMutexLock(&pCacheObj->lock);
116
#endif
117
}
46,883✔
118

119
static FORCE_INLINE void __trashcan_unlock(SCacheObj *pCacheObj) {
120
#if defined(LINUX)
121
  (void)taosThreadRwlockUnlock(&pCacheObj->lock);
46,874✔
122
#else
123
  (void)taosThreadMutexUnlock(&pCacheObj->lock);
124
#endif
125
}
46,883✔
126

127
static FORCE_INLINE int32_t __trashcan_lock_init(SCacheObj *pCacheObj) {
128
#if defined(LINUX)
129
  return taosThreadRwlockInit(&pCacheObj->lock, NULL);
5,787✔
130
#else
131
  return taosThreadMutexInit(&pCacheObj->lock, NULL);
132
#endif
133
}
134

135
static FORCE_INLINE void __trashcan_lock_destroy(SCacheObj *pCacheObj) {
136
#if defined(LINUX)
137
  (void)taosThreadRwlockDestroy(&pCacheObj->lock);
5,787✔
138
#else
139
  (void)taosThreadMutexDestroy(&pCacheObj->lock);
140
#endif
141
}
5,787✔
142

143
/**
144
 * do cleanup the taos cache
145
 * @param pCacheObj
146
 */
147
static void doCleanupDataCache(SCacheObj *pCacheObj);
148

149
/**
150
 * refresh cache to remove data in both hash list and trash, if any nodes' refcount == 0, every pCacheObj->refreshTime
151
 * @param handle   Cache object handle
152
 */
153
static void *taosCacheTimedRefresh(void *handle);
154

155
static void doInitRefreshThread(void) {
1,833✔
156
  pCacheArrayList = taosArrayInit(4, POINTER_BYTES);
1,833✔
157
  if (pCacheArrayList == NULL) {
1,833!
158
    uError("failed to allocate memory, reason:%s", strerror(ERRNO));
×
159
    return;
×
160
  }
161

162
  (void)taosThreadMutexInit(&guard, NULL);
1,833✔
163

164
  TdThreadAttr thattr;
165
  (void)taosThreadAttrInit(&thattr);
1,833✔
166
  (void)taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE);
1,833✔
167

168
  (void)taosThreadCreate(&cacheRefreshWorker, &thattr, taosCacheTimedRefresh, NULL);
1,833✔
169
  (void)taosThreadAttrDestroy(&thattr);
1,833✔
170
}
171

172
TdThread doRegisterCacheObj(SCacheObj *pCacheObj) {
5,787✔
173
  (void)taosThreadOnce(&cacheThreadInit, doInitRefreshThread);
5,787✔
174

175
  (void)taosThreadMutexLock(&guard);
5,787✔
176
  if (taosArrayPush(pCacheArrayList, &pCacheObj) == NULL) {
11,574!
177
    uError("failed to add cache object into array, reason:%s", strerror(ERRNO));
×
178
    (void)taosThreadMutexUnlock(&guard);
×
179
    return cacheRefreshWorker;
×
180
  }
181
  (void)taosThreadMutexUnlock(&guard);
5,787✔
182

183
  return cacheRefreshWorker;
5,787✔
184
}
185

186
/**
187
 * @param key      key of object for hash, usually a null-terminated string
188
 * @param keyLen   length of key
189
 * @param pData    actually data. required a consecutive memory block, no pointer is allowed
190
 *                 in pData. Pointer copy causes memory access error.
191
 * @param size     size of block
192
 * @param lifespan total survial expiredTime from now
193
 * @return         SCacheNode
194
 */
195
static SCacheNode *taosCreateCacheNode(const char *key, size_t keyLen, const char *pData, size_t size,
196
                                       uint64_t duration);
197

198
/**
199
 * addedTime object node into trash, and this object is closed for referencing if it is addedTime to trash
200
 * It will be removed until the pNode->refCount == 0
201
 * @param pCacheObj    Cache object
202
 * @param pNode   Cache slot object
203
 */
204
static void taosAddToTrashcan(SCacheObj *pCacheObj, SCacheNode *pNode);
205

206
/**
207
 * remove nodes in trash with refCount == 0 in cache
208
 * @param pNode
209
 * @param pCacheObj
210
 * @param force   force model, if true, remove data in trash without check refcount.
211
 *                may cause corruption. So, forece model only applys before cache is closed
212
 */
213
static void taosTrashcanEmpty(SCacheObj *pCacheObj, bool force);
214

215
/**
216
 * release node
217
 * @param pCacheObj      cache object
218
 * @param pNode          data node
219
 */
220
static FORCE_INLINE void taosCacheReleaseNode(SCacheObj *pCacheObj, SCacheNode *pNode) {
221
  if (pNode->signature != pNode) {
31,349!
222
    uError("key:%s, %p data is invalid, or has been released", pNode->key, pNode);
×
223
    return;
×
224
  }
225

226
  (void)atomic_sub_fetch_64(&pCacheObj->sizeInBytes, pNode->size);
31,349✔
227

228
  uDebug("cache:%s, key:%p, %p is destroyed from cache, size:%dbytes, total num:%d size:%" PRId64 "bytes",
31,349✔
229
         pCacheObj->name, pNode->key, pNode->data, pNode->size, (int)pCacheObj->numOfElems - 1, pCacheObj->sizeInBytes);
230

231
  if (pCacheObj->freeFp) {
31,349!
232
    pCacheObj->freeFp(pNode->data);
31,349✔
233
  }
234

235
  taosMemoryFree(pNode);
31,349!
236
}
237

238
static FORCE_INLINE STrashElem *doRemoveElemInTrashcan(SCacheObj *pCacheObj, STrashElem *pElem) {
239
  if (pElem->pData->signature != pElem->pData) {
9!
240
    uWarn("key:sig:0x%" PRIx64 " %p data has been released, ignore", (int64_t)pElem->pData->signature, pElem->pData);
×
241
    terrno = TSDB_CODE_INVALID_PARA;
×
242
    return NULL;
×
243
  }
244

245
  STrashElem *next = pElem->next;
9✔
246

247
  pCacheObj->numOfElemsInTrash--;
9✔
248
  if (pElem->prev) {
9!
249
    pElem->prev->next = pElem->next;
×
250
  } else {  // pnode is the header, update header
251
    pCacheObj->pTrash = pElem->next;
9✔
252
  }
253

254
  if (next) {
9!
255
    next->prev = pElem->prev;
4✔
256
  }
257
  return next;
9✔
258
}
259

260
static FORCE_INLINE void doDestroyTrashcanElem(SCacheObj *pCacheObj, STrashElem *pElem) {
261
  if (pCacheObj->freeFp) {
9!
262
    pCacheObj->freeFp(pElem->pData->data);
9✔
263
  }
264

265
  taosMemoryFree(pElem->pData);
9!
266
  taosMemoryFree(pElem);
9!
267
}
9✔
268

269
static void pushfrontNodeInEntryList(SCacheEntry *pEntry, SCacheNode *pNode) {
31,360✔
270
  pNode->pNext = pEntry->next;
31,360✔
271
  pEntry->next = pNode;
31,360✔
272
  pEntry->num += 1;
31,360✔
273
  // A S S E R T((pEntry->next && pEntry->num > 0) || (NULL == pEntry->next && pEntry->num == 0));
274
}
31,360✔
275

276
static void removeNodeInEntryList(SCacheEntry *pe, SCacheNode *prev, SCacheNode *pNode) {
6✔
277
  if (prev == NULL) {
6!
278
    pe->next = pNode->pNext;
6✔
279
  } else {
280
    prev->pNext = pNode->pNext;
×
281
  }
282

283
  pNode->pNext = NULL;
6✔
284
  pe->num -= 1;
6✔
285
  // A S S E R T((pe->next && pe->num > 0) || (NULL == pe->next && pe->num == 0));
286
}
6✔
287

288
static FORCE_INLINE SCacheEntry *doFindEntry(SCacheObj *pCacheObj, const void *key, size_t keyLen) {
289
  uint32_t hashVal = (*pCacheObj->hashFp)(key, keyLen);
31,361✔
290
  int32_t  slot = hashVal % pCacheObj->capacity;
143,078✔
291
  return &pCacheObj->pEntryList[slot];
143,078✔
292
}
293

294
static FORCE_INLINE SCacheNode *doSearchInEntryList(SCacheEntry *pe, const void *key, size_t keyLen,
295
                                                    SCacheNode **prev) {
296
  SCacheNode *pNode = pe->next;
143,080✔
297
  while (pNode) {
143,310!
298
    if ((pNode->keyLen == keyLen) && memcmp(pNode->key, key, keyLen) == 0) {
110,756!
299
      break;
110,526✔
300
    }
301
    *prev = pNode;
230✔
302
    pNode = pNode->pNext;
230✔
303
  }
304

305
  return pNode;
143,080✔
306
}
307

308
static bool doRemoveExpiredFn(void *param, SCacheNode *pNode) {
120,143✔
309
  SCacheObjTravSup *ps = (SCacheObjTravSup *)param;
120,143✔
310
  SCacheObj        *pCacheObj = ps->pCacheObj;
120,143✔
311

312
  if ((int64_t)pNode->expireTime < ps->time && T_REF_VAL_GET(pNode) <= 0) {
120,143!
313
    taosCacheReleaseNode(pCacheObj, pNode);
314

315
    // this node should be remove from hash table
316
    return false;
17,616✔
317
  }
318

319
  if (ps->fp) {
102,527!
320
    (ps->fp)(pNode->data, ps->param1);
×
321
  }
322

323
  // do not remove element in hash table
324
  return true;
102,527✔
325
}
326

327
static bool doRemoveNodeFn(void *param, SCacheNode *pNode) {
13,739✔
328
  SCacheObjTravSup *ps = (SCacheObjTravSup *)param;
13,739✔
329
  SCacheObj        *pCacheObj = ps->pCacheObj;
13,739✔
330

331
  if (T_REF_VAL_GET(pNode) == 0) {
13,739✔
332
    taosCacheReleaseNode(pCacheObj, pNode);
333
  } else {  // do add to trashcan
334
    taosAddToTrashcan(pCacheObj, pNode);
6✔
335
  }
336

337
  // this node should be remove from hash table
338
  return false;
13,739✔
339
}
340

341
static FORCE_INLINE int32_t getCacheCapacity(int32_t length) {
342
  int32_t len = 0;
343
  if (length < CACHE_DEFAULT_CAPACITY) {
344
    len = CACHE_DEFAULT_CAPACITY;
345
    return len;
346
  } else if (length > CACHE_MAX_CAPACITY) {
347
    len = CACHE_MAX_CAPACITY;
348
    return len;
349
  }
350

351
  len = CACHE_DEFAULT_CAPACITY;
352
  while (len < length && len < CACHE_MAX_CAPACITY) {
353
    len = (len << 1u);
354
  }
355

356
  return len > CACHE_MAX_CAPACITY ? CACHE_MAX_CAPACITY : len;
357
}
358

359
SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInMs, bool extendLifespan, __cache_free_fn_t fn,
5,787✔
360
                         const char *cacheName) {
361
  const int32_t SLEEP_DURATION = 500;  // 500 ms
5,787✔
362

363
  if (refreshTimeInMs <= 0) {
5,787!
364
    return NULL;
×
365
  }
366

367
  SCacheObj *pCacheObj = (SCacheObj *)taosMemoryCalloc(1, sizeof(SCacheObj));
5,787!
368
  if (pCacheObj == NULL) {
5,787!
369
    uError("failed to allocate memory, reason:%s", strerror(ERRNO));
×
370
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
371
    return NULL;
×
372
  }
373

374
  // TODO add the auto extend procedure
375
  pCacheObj->capacity = 4096;
5,787✔
376
  pCacheObj->pEntryList = taosMemoryCalloc(pCacheObj->capacity, sizeof(SCacheEntry));
5,787!
377
  if (pCacheObj->pEntryList == NULL) {
5,787!
378
    taosMemoryFree(pCacheObj);
×
379
    uError("failed to allocate memory, reason:%s", strerror(ERRNO));
×
380
    return NULL;
×
381
  }
382

383
  pCacheObj->name = taosStrdup(cacheName);
5,787!
384
  if (pCacheObj->name == NULL) {
5,787!
385
    taosMemoryFreeClear(pCacheObj->pEntryList);
×
386
    taosMemoryFree(pCacheObj);
×
387
    uError("failed to allocate memory, reason:%s", terrstr());
×
388
    return NULL;
×
389
  }
390

391
  // set free cache node callback function
392
  pCacheObj->hashFp = taosGetDefaultHashFunction(keyType);
5,787✔
393
  pCacheObj->freeFp = fn;
5,787✔
394
  pCacheObj->refreshTime = refreshTimeInMs;
5,787✔
395
  pCacheObj->checkTick = pCacheObj->refreshTime / SLEEP_DURATION;
5,787✔
396
  pCacheObj->extendLifespan = extendLifespan;  // the TTL after the last access
5,787✔
397

398
  if (__trashcan_lock_init(pCacheObj) != 0) {
5,787!
399
    taosMemoryFreeClear(pCacheObj->pEntryList);
×
400
    taosMemoryFreeClear(pCacheObj->name);
×
401
    taosMemoryFree(pCacheObj);
×
402

403
    uError("failed to init lock, reason:%s", strerror(ERRNO));
×
404
    return NULL;
×
405
  }
406

407
  TdThread refreshWorker = doRegisterCacheObj(pCacheObj);
5,787✔
408
  return pCacheObj;
5,787✔
409
}
410

411
void *taosCachePut(SCacheObj *pCacheObj, const void *key, size_t keyLen, const void *pData, size_t dataSize,
31,361✔
412
                   int32_t durationMS) {
413
  if (pCacheObj == NULL || pCacheObj->pEntryList == NULL || pCacheObj->deleting == 1) {
31,361!
414
    terrno = TSDB_CODE_INVALID_PARA;
×
415
    return NULL;
×
416
  }
417

418
  SCacheNode *pNode1 = taosCreateCacheNode(key, keyLen, pData, dataSize, durationMS);
31,361✔
419
  if (pNode1 == NULL) {
31,361!
420
    uError("cache:%s, key:%p, failed to added into cache, out of memory", pCacheObj->name, key);
×
421
    return NULL;
×
422
  }
423

424
  T_REF_INC(pNode1);
31,361✔
425

426
  SCacheEntry *pe = doFindEntry(pCacheObj, key, keyLen);
31,360✔
427

428
  taosWLockLatch(&pe->latch);
31,360✔
429

430
  SCacheNode *prev = NULL;
31,360✔
431
  SCacheNode *pNode = doSearchInEntryList(pe, key, keyLen, &prev);
31,360✔
432

433
  if (pNode == NULL) {
31,360✔
434
    pushfrontNodeInEntryList(pe, pNode1);
31,354✔
435
    (void)atomic_add_fetch_ptr(&pCacheObj->numOfElems, 1);
31,353✔
436
    (void)atomic_add_fetch_ptr(&pCacheObj->sizeInBytes, pNode1->size);
31,353✔
437
    uDebug("cache:%s, key:%p, %p added into cache, added:%" PRIu64 ", expire:%" PRIu64
31,353✔
438
           ", totalNum:%d sizeInBytes:%" PRId64 "bytes size:%" PRId64 "bytes",
439
           pCacheObj->name, key, pNode1->data, pNode1->addedTime, pNode1->expireTime, (int32_t)pCacheObj->numOfElems,
440
           pCacheObj->sizeInBytes, (int64_t)dataSize);
441
  } else {  // duplicated key exists
442
    // move current node to trashcan
443
    removeNodeInEntryList(pe, prev, pNode);
6✔
444

445
    if (T_REF_VAL_GET(pNode) == 0) {
6✔
446
      if (pCacheObj->freeFp) {
3!
447
        pCacheObj->freeFp(pNode->data);
3✔
448
      }
449

450
      (void)atomic_sub_fetch_64(&pCacheObj->sizeInBytes, pNode->size);
3✔
451
      taosMemoryFreeClear(pNode);
3!
452
    } else {
453
      taosAddToTrashcan(pCacheObj, pNode);
3✔
454
      uDebug("cache:%s, key:%p, %p exist in cache, updated old:%p", pCacheObj->name, key, pNode1->data, pNode->data);
3!
455
    }
456

457
    pushfrontNodeInEntryList(pe, pNode1);
6✔
458
    (void)atomic_add_fetch_64(&pCacheObj->sizeInBytes, pNode1->size);
6✔
459
    uDebug("cache:%s, key:%p, %p added into cache, added:%" PRIu64 ", expire:%" PRIu64
6!
460
           ", totalNum:%d sizeInBytes:%" PRId64 "bytes size:%" PRId64 "bytes",
461
           pCacheObj->name, key, pNode1->data, pNode1->addedTime, pNode1->expireTime, (int32_t)pCacheObj->numOfElems,
462
           pCacheObj->sizeInBytes, (int64_t)dataSize);
463
  }
464

465
  taosWUnLockLatch(&pe->latch);
31,359✔
466
  return pNode1->data;
31,360✔
467
}
468

469
void *taosCacheAcquireByKey(SCacheObj *pCacheObj, const void *key, size_t keyLen) {
114,075✔
470
  if (pCacheObj == NULL || pCacheObj->deleting == 1) {
114,075!
471
    return NULL;
1✔
472
  }
473

474
  if (pCacheObj->numOfElems == 0) {
114,074✔
475
    (void)atomic_add_fetch_64(&pCacheObj->statistics.missCount, 1);
2,355✔
476
    return NULL;
2,355✔
477
  }
478

479
  SCacheNode  *prev = NULL;
111,719✔
480
  SCacheEntry *pe = doFindEntry(pCacheObj, key, keyLen);
111,718✔
481

482
  taosRLockLatch(&pe->latch);
111,718✔
483

484
  SCacheNode *pNode = doSearchInEntryList(pe, key, keyLen, &prev);
111,720✔
485
  if (pNode != NULL) {
111,720✔
486
    int32_t ref = T_REF_INC(pNode);
110,520✔
487
  }
488

489
  taosRUnLockLatch(&pe->latch);
111,719✔
490

491
  void *pData = (pNode != NULL) ? pNode->data : NULL;
111,719✔
492
  if (pData != NULL) {
111,719✔
493
    (void)atomic_add_fetch_64(&pCacheObj->statistics.hitCount, 1);
110,521✔
494
    uDebug("cache:%s, key:%p, %p is retrieved from cache, refcnt:%d", pCacheObj->name, key, pData,
110,520✔
495
           T_REF_VAL_GET(pNode));
496
  } else {
497
    (void)atomic_add_fetch_64(&pCacheObj->statistics.missCount, 1);
1,198✔
498
    uDebug("cache:%s, key:%p, not in cache, retrieved failed", pCacheObj->name, key);
1,199✔
499
  }
500

501
  (void)atomic_add_fetch_64(&pCacheObj->statistics.totalAccess, 1);
111,719✔
502
  return pData;
111,719✔
503
}
504

505
void *taosCacheAcquireByData(SCacheObj *pCacheObj, void *data) {
×
506
  if (pCacheObj == NULL || data == NULL) return NULL;
×
507

508
  SCacheNode *ptNode = (SCacheNode *)((char *)data - sizeof(SCacheNode));
×
509
  if (ptNode->signature != ptNode) {
×
510
    uError("cache:%s, key: %p the data from cache is invalid", pCacheObj->name, ptNode);
×
511
    return NULL;
×
512
  }
513

514
  int32_t ref = T_REF_INC(ptNode);
×
515
  uDebug("cache:%s, data: %p acquired by data in cache, refcnt:%d", pCacheObj->name, ptNode->data, ref);
×
516

517
  // the data if referenced by at least one object, so the reference count must be greater than the value of 2.
518
  // A S S E R T(ref >= 2);
519
  return data;
×
520
}
521

522
void *taosCacheTransferData(SCacheObj *pCacheObj, void **data) {
×
523
  if (pCacheObj == NULL || data == NULL || (*data) == NULL) {
×
524
    terrno = TSDB_CODE_INVALID_PARA;
×
525
    return NULL;
×
526
  }
527

528
  SCacheNode *ptNode = (SCacheNode *)((char *)(*data) - sizeof(SCacheNode));
×
529
  if (ptNode->signature != ptNode) {
×
530
    uError("cache:%s, key: %p the data from cache is invalid", pCacheObj->name, ptNode);
×
531
    terrno = TSDB_CODE_INVALID_PARA;
×
532
    return NULL;
×
533
  }
534

535
  char *d = *data;
×
536

537
  // clear its reference to old area
538
  *data = NULL;
×
539
  return d;
×
540
}
541

542
void taosCacheRelease(SCacheObj *pCacheObj, void **data, bool _remove) {
143,033✔
543
  if (pCacheObj == NULL) {
143,033!
544
    return;
×
545
  }
546

547
  if ((*data) == NULL) {
143,033!
548
    uError("cache:%s, NULL data to release", pCacheObj->name);
×
549
    return;
×
550
  }
551

552
  // The operation of removal from hash table and addition to trashcan is not an atomic operation,
553
  // therefore the check for the empty of both the hash table and the trashcan has a race condition.
554
  // It happens when there is only one object in the cache, and two threads which has referenced this object
555
  // start to free the it simultaneously [TD-1569].
556
  SCacheNode *pNode = (SCacheNode *)((char *)(*data) - sizeof(SCacheNode));
143,033✔
557
  if (pNode->signature != pNode) {
143,033!
558
    uError("cache:%s, %p, release invalid cache data", pCacheObj->name, pNode);
×
559
    return;
×
560
  }
561

562
  *data = NULL;
143,033✔
563

564
  // note: extend lifespan before dec ref count
565
  bool inTrashcan = pNode->inTrashcan;
143,033✔
566

567
  if (pCacheObj->extendLifespan && (!inTrashcan) && (!_remove)) {
143,033!
568
    atomic_store_64(&pNode->expireTime, pNode->lifespan + taosGetTimestampMs());
149,827✔
569
    uDebug("cache:%s, data:%p extend expire time: %" PRId64, pCacheObj->name, pNode->data, pNode->expireTime);
74,913✔
570
  }
571

572
  if (_remove) {
143,031!
573
    // NOTE: once refcount is decrease, pNode may be freed by other thread immediately.
574
    char *key = pNode->key;
×
575
    char *d = pNode->data;
×
576

577
    int32_t ref = T_REF_VAL_GET(pNode);
×
578
    uDebug("cache:%s, key:%p, %p is released, refcnt:%d, in trashcan:%d", pCacheObj->name, key, d, ref - 1, inTrashcan);
×
579

580
    /*
581
     * If it is not referenced by other users, remove it immediately. Otherwise move this node to trashcan wait for all
582
     * users releasing this resources.
583
     *
584
     * NOTE: previous ref is 0, and current ref is still 0, remove it. If previous is not 0, there is another thread
585
     * that tries to do the same thing.
586
     */
587
    if (inTrashcan) {
×
588
      ref = T_REF_VAL_GET(pNode);
×
589

590
      if (ref == 1) {
×
591
        // If it is the last ref, remove it from trashcan linked-list first, and then destroy it.Otherwise, it may be
592
        // destroyed by refresh worker if decrease ref count before removing it from linked-list.
593
        // A S S E R T(pNode->pTNodeHeader->pData == pNode);
594

595
        __trashcan_wr_lock(pCacheObj);
596
        (void)doRemoveElemInTrashcan(pCacheObj, pNode->pTNodeHeader);
×
597
        __trashcan_unlock(pCacheObj);
598

599
        ref = T_REF_DEC(pNode);
×
600
        // A S S E R T(ref == 0);
601

602
        doDestroyTrashcanElem(pCacheObj, pNode->pTNodeHeader);
×
603
      } else {
604
        ref = T_REF_DEC(pNode);
×
605
        // A S S E R T(ref >= 0);
606
      }
607
    } else {
608
      // NOTE: remove it from hash in the first place, otherwise, the pNode may have been released by other thread
609
      // when reaches here.
610
      SCacheNode  *prev = NULL;
×
611
      SCacheEntry *pe = doFindEntry(pCacheObj, pNode->key, pNode->keyLen);
×
612

613
      taosWLockLatch(&pe->latch);
×
614
      ref = T_REF_DEC(pNode);
×
615

616
      SCacheNode *p = doSearchInEntryList(pe, pNode->key, pNode->keyLen, &prev);
×
617

618
      if (p != NULL) {
×
619
        // successfully remove from hash table, if failed, this node must have been move to trash already, do nothing.
620
        // note that the remove operation can be executed only once.
621
        if (p != pNode) {
×
622
          uDebug(
×
623
              "cache:%s, key:%p, a new entry:%p found, refcnt:%d, prev entry:%p, refcnt:%d has been removed by "
624
              "others already, prev must in trashcan",
625
              pCacheObj->name, pNode->key, p->data, T_REF_VAL_GET(p), pNode->data, T_REF_VAL_GET(pNode));
626

627
          // A S S E R T(p->pTNodeHeader == NULL && pNode->pTNodeHeader != NULL);
628
        } else {
629
          removeNodeInEntryList(pe, prev, p);
×
630
          uDebug("cache:%s, key:%p, %p successfully removed from hash table, refcnt:%d", pCacheObj->name, pNode->key,
×
631
                 pNode->data, ref);
632
          if (ref > 0) {
×
633
            taosAddToTrashcan(pCacheObj, pNode);
×
634
          } else {  // ref == 0
635
            (void)atomic_sub_fetch_64(&pCacheObj->sizeInBytes, pNode->size);
×
636

637
            int32_t size = (int32_t)pCacheObj->numOfElems;
×
638
            uDebug("cache:%s, key:%p, %p is destroyed from cache, size:%dbytes, totalNum:%d size:%" PRId64 "bytes",
×
639
                   pCacheObj->name, pNode->key, pNode->data, pNode->size, size, pCacheObj->sizeInBytes);
640

641
            if (pCacheObj->freeFp) {
×
642
              pCacheObj->freeFp(pNode->data);
×
643
            }
644

645
            taosMemoryFree(pNode);
×
646
          }
647
        }
648

649
        taosWUnLockLatch(&pe->latch);
×
650
      } else {
651
        uDebug("cache:%s, key:%p, %p has been removed from hash table by others already, refcnt:%d", pCacheObj->name,
×
652
               pNode->key, pNode->data, ref);
653
      }
654
    }
655

656
  } else {
657
    // NOTE: once refcount is decrease, pNode may be freed by other thread immediately.
658
    char *key = pNode->key;
143,031✔
659
    char *p = pNode->data;
143,031✔
660

661
    int32_t ref = T_REF_DEC(pNode);
143,031✔
662
    uDebug("cache:%s, key:%p, %p released, refcnt:%d, data in trashcan:%d", pCacheObj->name, key, p, ref, inTrashcan);
143,033✔
663
  }
664
}
665

666
void doTraverseElems(SCacheObj *pCacheObj, bool (*fp)(void *param, SCacheNode *pNode), SCacheObjTravSup *pSup) {
46,874✔
667
  int32_t numOfEntries = (int32_t)pCacheObj->capacity;
46,874✔
668
  for (int32_t i = 0; i < numOfEntries; ++i) {
192,042,778✔
669
    SCacheEntry *pEntry = &pCacheObj->pEntryList[i];
191,995,904✔
670
    if (pEntry->num == 0) {
191,995,904✔
671
      continue;
191,862,224✔
672
    }
673

674
    taosWLockLatch(&pEntry->latch);
133,680✔
675

676
    SCacheNode **pPre = &pEntry->next;
133,680✔
677
    SCacheNode  *pNode = pEntry->next;
133,680✔
678
    while (pNode != NULL) {
267,562✔
679
      SCacheNode *next = pNode->pNext;
133,882✔
680

681
      if (fp(pSup, pNode)) {
133,882✔
682
        pPre = &pNode->pNext;
102,527✔
683
        pNode = pNode->pNext;
102,527✔
684
      } else {
685
        *pPre = next;
31,355✔
686
        pEntry->num -= 1;
31,355✔
687
        // A S S E R T((pEntry->next && pEntry->num > 0) || (NULL == pEntry->next && pEntry->num == 0));
688

689
        (void)atomic_sub_fetch_ptr(&pCacheObj->numOfElems, 1);
31,355✔
690
        pNode = next;
31,355✔
691
      }
692
    }
693

694
    taosWUnLockLatch(&pEntry->latch);
133,680✔
695
  }
696
}
46,874✔
697

698
void taosCacheEmpty(SCacheObj *pCacheObj) {
×
699
  SCacheObjTravSup sup = {.pCacheObj = pCacheObj, .fp = NULL, .time = taosGetTimestampMs()};
×
700
  doTraverseElems(pCacheObj, doRemoveNodeFn, &sup);
×
701
  taosTrashcanEmpty(pCacheObj, false);
×
702
}
×
703

704
void taosCacheCleanup(SCacheObj *pCacheObj) {
5,787✔
705
  if (pCacheObj == NULL) {
5,787!
706
    return;
×
707
  }
708

709
  pCacheObj->deleting = 1;
5,787✔
710

711
  // wait for the refresh thread quit before destroying the cache object.
712
  // But in the dll, the child thread will be killed before atexit takes effect.
713
  while (atomic_load_8(&pCacheObj->deleting) != 0) {
54,323✔
714
    if (refreshWorkerNormalStopped) break;
48,536!
715
    if (refreshWorkerUnexpectedStopped) return;
48,536!
716
    taosMsleep(50);
48,536✔
717
  }
718

719
  uTrace("cache:%s will be cleaned up", pCacheObj->name);
5,787✔
720
  doCleanupDataCache(pCacheObj);
5,787✔
721
}
722

723
SCacheNode *taosCreateCacheNode(const char *key, size_t keyLen, const char *pData, size_t size, uint64_t duration) {
31,361✔
724
  size_t sizeInBytes = size + sizeof(SCacheNode) + keyLen;
31,361✔
725

726
  SCacheNode *pNewNode = taosMemoryCalloc(1, sizeInBytes);
31,361!
727
  if (pNewNode == NULL) {
31,361!
728
    uError("failed to allocate memory, reason:%s", strerror(ERRNO));
×
729
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
730
    return NULL;
×
731
  }
732

733
  pNewNode->data = (char *)pNewNode + sizeof(SCacheNode);
31,361✔
734
  pNewNode->dataLen = size;
31,361✔
735
  memcpy(pNewNode->data, pData, size);
31,361✔
736

737
  pNewNode->key = (char *)pNewNode + sizeof(SCacheNode) + size;
31,361✔
738
  pNewNode->keyLen = (uint16_t)keyLen;
31,361✔
739

740
  memcpy(pNewNode->key, key, keyLen);
31,361✔
741

742
  pNewNode->addedTime = (uint64_t)taosGetTimestampMs();
31,361✔
743
  pNewNode->lifespan = duration;
31,361✔
744
  pNewNode->expireTime = pNewNode->addedTime + pNewNode->lifespan;
31,361✔
745
  pNewNode->signature = pNewNode;
31,361✔
746
  pNewNode->size = (uint32_t)sizeInBytes;
31,361✔
747

748
  return pNewNode;
31,361✔
749
}
750

751
void taosAddToTrashcan(SCacheObj *pCacheObj, SCacheNode *pNode) {
9✔
752
  if (pNode->inTrashcan) { /* node is already in trash */
9!
753
    // A S S E R T(pNode->pTNodeHeader != NULL && pNode->pTNodeHeader->pData == pNode);
754
    return;
×
755
  }
756

757
  STrashElem *pElem = taosMemoryCalloc(1, sizeof(STrashElem));
9!
758
  if (!pElem) {
9!
759
    uError("cache:%s key:%p, %p move to trashcan failed since %s, numOfElem in trashcan:%d", pCacheObj->name,
×
760
           pNode->key, pNode->data, terrstr(), pCacheObj->numOfElemsInTrash);
761
    return;
×
762
  }
763
  __trashcan_wr_lock(pCacheObj);
764
  pElem->pData = pNode;
9✔
765
  pElem->prev = NULL;
9✔
766
  pElem->next = NULL;
9✔
767
  pNode->inTrashcan = true;
9✔
768
  pNode->pTNodeHeader = pElem;
9✔
769

770
  pElem->next = pCacheObj->pTrash;
9✔
771
  if (pCacheObj->pTrash) {
9✔
772
    pCacheObj->pTrash->prev = pElem;
4✔
773
  }
774

775
  pCacheObj->pTrash = pElem;
9✔
776
  pCacheObj->numOfElemsInTrash++;
9✔
777
  __trashcan_unlock(pCacheObj);
778

779
  uDebug("cache:%s key:%p, %p move to trashcan, pTrashElem:%p, numOfElem in trashcan:%d", pCacheObj->name, pNode->key,
9!
780
         pNode->data, pElem, pCacheObj->numOfElemsInTrash);
781
}
782

783
void taosTrashcanEmpty(SCacheObj *pCacheObj, bool force) {
46,874✔
784
  __trashcan_wr_lock(pCacheObj);
785

786
  if (pCacheObj->numOfElemsInTrash == 0) {
46,874✔
787
    if (pCacheObj->pTrash != NULL) {
46,869!
788
      pCacheObj->pTrash = NULL;
×
789
      uError("cache:%s, key:inconsistency data in cache, numOfElem in trashcan:%d", pCacheObj->name,
×
790
             pCacheObj->numOfElemsInTrash);
791
    }
792

793
    __trashcan_unlock(pCacheObj);
794
    return;
46,869✔
795
  }
796

797
  const char *stat[] = {"false", "true"};
5✔
798
  uDebug("cache:%s start to cleanup trashcan, numOfElem in trashcan:%d, free:%s", pCacheObj->name,
5!
799
         pCacheObj->numOfElemsInTrash, (force ? stat[1] : stat[0]));
800

801
  STrashElem *pElem = pCacheObj->pTrash;
5✔
802
  while (pElem) {
14✔
803
    if (force || (T_REF_VAL_GET(pElem->pData) == 0)) {
9!
804
      uDebug("cache:%s, key:%p, %p removed from trashcan. numOfElem in trashcan:%d", pCacheObj->name, pElem->pData->key,
9!
805
             pElem->pData->data, pCacheObj->numOfElemsInTrash - 1);
806

807
      (void)doRemoveElemInTrashcan(pCacheObj, pElem);
808
      doDestroyTrashcanElem(pCacheObj, pElem);
809
      pElem = pCacheObj->pTrash;
9✔
810
    } else {
811
      pElem = pElem->next;
×
812
    }
813
  }
814

815
  __trashcan_unlock(pCacheObj);
816
}
817

818
void doCleanupDataCache(SCacheObj *pCacheObj) {
5,787✔
819
  SCacheObjTravSup sup = {.pCacheObj = pCacheObj, .fp = NULL, .time = taosGetTimestampMs()};
5,787✔
820
  doTraverseElems(pCacheObj, doRemoveNodeFn, &sup);
5,787✔
821

822
  // todo memory leak if there are object with refcount greater than 0 in hash table?
823
  taosTrashcanEmpty(pCacheObj, true);
5,787✔
824

825
  __trashcan_lock_destroy(pCacheObj);
826

827
  taosMemoryFreeClear(pCacheObj->pEntryList);
5,787!
828
  taosMemoryFreeClear(pCacheObj->name);
5,787!
829
  taosMemoryFree(pCacheObj);
5,787!
830
}
5,787✔
831

832
static void doCacheRefresh(SCacheObj *pCacheObj, int64_t time, __cache_trav_fn_t fp, void *param1) {
41,087✔
833
  SCacheObjTravSup sup = {.pCacheObj = pCacheObj, .fp = fp, .time = time, .param1 = param1};
41,087✔
834
  doTraverseElems(pCacheObj, doRemoveExpiredFn, &sup);
41,087✔
835
}
41,087✔
836

837
void taosCacheRefreshWorkerUnexpectedStopped(void) {
×
838
  if (!refreshWorkerNormalStopped) {
×
839
    refreshWorkerUnexpectedStopped = true;
×
840
  }
841
}
×
842

843
void *taosCacheTimedRefresh(void *handle) {
1,833✔
844
  uDebug("cache refresh thread starts");
1,833✔
845
  setThreadName("cacheRefresh");
1,833✔
846

847
  const int32_t SLEEP_DURATION = 500;  // 500 ms
1,833✔
848
  int64_t       count = 0;
1,833✔
849
#ifdef WINDOWS
850
  if (taosCheckCurrentInDll()) {
851
    atexit(taosCacheRefreshWorkerUnexpectedStopped);
852
  }
853
#endif
854

855
  while (1) {
137,765✔
856
    taosMsleep(SLEEP_DURATION);
139,598✔
857
    if (stopRefreshWorker) {
139,598✔
858
      goto _end;
1,833✔
859
    }
860

861
    (void)taosThreadMutexLock(&guard);
137,765✔
862
    size_t size = taosArrayGetSize(pCacheArrayList);
137,765✔
863
    (void)taosThreadMutexUnlock(&guard);
137,765✔
864

865
    count += 1;
137,765✔
866

867
    for (int32_t i = 0; i < size; ++i) {
534,695✔
868
      (void)taosThreadMutexLock(&guard);
396,930✔
869
      SCacheObj *pCacheObj = taosArrayGetP(pCacheArrayList, i);
396,930✔
870

871
      if (pCacheObj == NULL) {
396,930!
872
        uError("object is destroyed. ignore and try next");
×
873
        (void)taosThreadMutexUnlock(&guard);
×
874
        continue;
×
875
      }
876

877
      // check if current cache object will be deleted every 500ms.
878
      if (pCacheObj->deleting) {
396,930✔
879
        taosArrayRemove(pCacheArrayList, i);
5,787✔
880
        size = taosArrayGetSize(pCacheArrayList);
5,787✔
881

882
        uDebug("%s is destroying, remove it from refresh list, remain cache obj:%" PRIzu, pCacheObj->name, size);
5,787✔
883
        pCacheObj->deleting = 0;  // reset the deleting flag to enable pCacheObj to continue releasing resources.
5,787✔
884

885
        (void)taosThreadMutexUnlock(&guard);
5,787✔
886
        continue;
5,787✔
887
      }
888

889
      (void)taosThreadMutexUnlock(&guard);
391,143✔
890

891
      if ((count % pCacheObj->checkTick) != 0) {
391,143✔
892
        continue;
336,964✔
893
      }
894

895
      size_t elemInHash = pCacheObj->numOfElems;
54,179✔
896
      if (elemInHash + pCacheObj->numOfElemsInTrash == 0) {
54,179✔
897
        continue;
13,092✔
898
      }
899

900
      uDebug("%s refresh thread scan", pCacheObj->name);
41,087✔
901
      pCacheObj->statistics.refreshCount++;
41,087✔
902

903
      // refresh data in hash table
904
      if (elemInHash > 0) {
41,087!
905
        int64_t now = taosGetTimestampMs();
41,087✔
906
        doCacheRefresh(pCacheObj, now, NULL, NULL);
41,087✔
907
      }
908

909
      taosTrashcanEmpty(pCacheObj, false);
41,087✔
910
    }
911
  }
912

913
_end:
1,833✔
914
  taosArrayDestroy(pCacheArrayList);
1,833✔
915

916
  pCacheArrayList = NULL;
1,833✔
917
  (void)taosThreadMutexDestroy(&guard);
1,833✔
918
  refreshWorkerNormalStopped = true;
1,833✔
919

920
  uDebug("cache refresh thread quits");
1,833✔
921
  return NULL;
1,833✔
922
}
923

924
void taosCacheRefresh(SCacheObj *pCacheObj, __cache_trav_fn_t fp, void *param1) {
×
925
  if (pCacheObj == NULL) {
×
926
    return;
×
927
  }
928

929
  int64_t now = taosGetTimestampMs();
×
930
  doCacheRefresh(pCacheObj, now, fp, param1);
×
931
}
932

933
void taosStopCacheRefreshWorker(void) {
2,387✔
934
  stopRefreshWorker = true;
2,387✔
935
  TdThreadOnce tmp = PTHREAD_ONCE_INIT;
2,387✔
936
  if (memcmp(&cacheThreadInit, &tmp, sizeof(TdThreadOnce)) != 0) (void)taosThreadJoin(cacheRefreshWorker, NULL);
2,387✔
937
  taosArrayDestroy(pCacheArrayList);
2,387✔
938
}
2,387✔
939

940
size_t taosCacheGetNumOfObj(const SCacheObj *pCacheObj) { return pCacheObj->numOfElems + pCacheObj->numOfElemsInTrash; }
1✔
941

942
SCacheIter *taosCacheCreateIter(const SCacheObj *pCacheObj) {
87✔
943
  SCacheIter *pIter = taosMemoryCalloc(1, sizeof(SCacheIter));
87!
944
  if (pIter) {
87!
945
    pIter->pCacheObj = (SCacheObj *)pCacheObj;
87✔
946
    pIter->entryIndex = -1;
87✔
947
    pIter->index = -1;
87✔
948
  }
949
  return pIter;
87✔
950
}
951

952
bool taosCacheIterNext(SCacheIter *pIter) {
1,244✔
953
  SCacheObj *pCacheObj = pIter->pCacheObj;
1,244✔
954

955
  if (pIter->index + 1 >= pIter->numOfObj) {
1,244!
956
    // release the reference for all objects in the snapshot
957
    for (int32_t i = 0; i < pIter->numOfObj; ++i) {
2,400✔
958
      char *p = pIter->pCurrent[i]->data;
1,157✔
959
      taosCacheRelease(pCacheObj, (void **)&p, false);
1,157✔
960
      pIter->pCurrent[i] = NULL;
1,156✔
961
    }
962

963
    if (pIter->entryIndex + 1 >= pCacheObj->capacity) {
1,243!
964
      return false;
×
965
    }
966

967
    while (1) {
354,545✔
968
      pIter->entryIndex++;
355,788✔
969
      if (pIter->entryIndex >= pCacheObj->capacity) {
355,788✔
970
        return false;
87✔
971
      }
972

973
      SCacheEntry *pEntry = &pCacheObj->pEntryList[pIter->entryIndex];
355,701✔
974
      taosRLockLatch(&pEntry->latch);
355,701✔
975

976
      if (pEntry->num == 0) {
356,163✔
977
        taosRUnLockLatch(&pEntry->latch);
355,005✔
978
        continue;
354,545✔
979
      }
980

981
      if (pIter->numOfObj < pEntry->num) {
1,158✔
982
        char *tmp = taosMemoryRealloc(pIter->pCurrent, pEntry->num * POINTER_BYTES);
85!
983
        if (tmp == NULL) {
85!
984
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
985
          taosRUnLockLatch(&pEntry->latch);
×
986
          return false;
×
987
        }
988

989
        pIter->pCurrent = (SCacheNode **)tmp;
85✔
990
      }
991

992
      SCacheNode *pNode = pEntry->next;
1,158✔
993
      for (int32_t i = 0; i < pEntry->num; ++i) {
2,316✔
994
        pIter->pCurrent[i] = pNode;
1,158✔
995
        int32_t ref = T_REF_INC(pIter->pCurrent[i]);
1,158✔
996
        pNode = pNode->pNext;
1,158✔
997
      }
998

999
      pIter->numOfObj = pEntry->num;
1,158✔
1000
      taosRUnLockLatch(&pEntry->latch);
1,158✔
1001

1002
      pIter->index = -1;
1,158✔
1003
      break;
1,158✔
1004
    }
1005
  }
1006

1007
  pIter->index += 1;
1,158✔
1008
  return true;
1,158✔
1009
}
1010

1011
void *taosCacheIterGetData(const SCacheIter *pIter, size_t *len) {
1,158✔
1012
  SCacheNode *pNode = pIter->pCurrent[pIter->index];
1,158✔
1013
  *len = pNode->dataLen;
1,158✔
1014
  return pNode->data;
1,158✔
1015
}
1016

1017
void *taosCacheIterGetKey(const SCacheIter *pIter, size_t *len) {
×
1018
  SCacheNode *pNode = pIter->pCurrent[pIter->index];
×
1019
  *len = pNode->keyLen;
×
1020
  return pNode->key;
×
1021
}
1022

1023
void taosCacheDestroyIter(SCacheIter *pIter) {
87✔
1024
  for (int32_t i = 0; i < pIter->numOfObj; ++i) {
172✔
1025
    if (!pIter->pCurrent[i]) continue;
85!
1026
    char *p = pIter->pCurrent[i]->data;
×
1027
    taosCacheRelease(pIter->pCacheObj, (void **)&p, false);
×
1028
    pIter->pCurrent[i] = NULL;
×
1029
  }
1030
  taosMemoryFreeClear(pIter->pCurrent);
87!
1031
  taosMemoryFreeClear(pIter);
87!
1032
}
87✔
1033

1034
void taosCacheTryExtendLifeSpan(SCacheObj *pCacheObj, void **data) {
66,972✔
1035
  if (!pCacheObj || !(*data)) return;
66,972!
1036

1037
  SCacheNode *pNode = (SCacheNode *)((char *)(*data) - sizeof(SCacheNode));
66,972✔
1038
  if (pNode->signature != pNode) return;
66,972!
1039

1040
  if (!pNode->inTrashcan) {
66,972!
1041
    atomic_store_64(&pNode->expireTime, pNode->lifespan + taosGetTimestampMs());
133,944✔
1042
    uDebug("cache:%s, data:%p extend expire time: %" PRId64, pCacheObj->name, pNode->data, pNode->expireTime);
66,972✔
1043
  }
1044
}
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

© 2025 Coveralls, Inc