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

taosdata / TDengine / #4672

14 Aug 2025 01:04PM UTC coverage: 59.715% (+2.2%) from 57.533%
#4672

push

travis-ci

web-flow
fix(query): fix order by column check of union operator (#32524)

137065 of 292055 branches covered (46.93%)

Branch coverage included in aggregate %.

1 of 13 new or added lines in 1 file covered. (7.69%)

20958 existing lines in 205 files now uncovered.

207155 of 284385 relevant lines covered (72.84%)

20501687.34 hits per line

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

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

22
int64_t tsQueueMemoryAllowed = 0;
23
int64_t tsQueueMemoryUsed = 0;
24

25
int64_t tsApplyMemoryAllowed = 0;
26
int64_t tsApplyMemoryUsed = 0;
27
struct STaosQueue {
28
  STaosQnode   *head;
29
  STaosQnode   *tail;
30
  STaosQueue   *next;     // for queue set
31
  STaosQset    *qset;     // for queue set
32
  void         *ahandle;  // for queue set
33
  FItem         itemFp;
34
  FItems        itemsFp;
35
  int32_t       numOfItems;
36
  int64_t       memOfItems;
37
  int64_t       threadId;
38
  int64_t       memLimit;
39
  int64_t       itemLimit;
40
  TdThreadMutex mutex;
41
};
42

43
struct STaosQset {
44
  STaosQueue   *head;
45
  STaosQueue   *current;
46
  TdThreadMutex mutex;
47
  tsem_t        sem;
48
  int32_t       numOfQueues;
49
  int32_t       numOfItems;
50
};
51

52
struct STaosQall {
53
  STaosQnode *current;
54
  STaosQnode *start;
55
  int32_t     numOfItems;
56
  int64_t     memOfItems;
57
  int32_t     unAccessedNumOfItems;
58
  int64_t     unAccessMemOfItems;
59
};
60

61
void taosSetQueueMemoryCapacity(STaosQueue *queue, int64_t cap) { queue->memLimit = cap; }
×
62
void taosSetQueueCapacity(STaosQueue *queue, int64_t size) { queue->itemLimit = size; }
×
63

64
int32_t taosOpenQueue(STaosQueue **queue) {
13,606,657✔
65
  *queue = taosMemoryCalloc(1, sizeof(STaosQueue));
13,606,657!
66
  if (*queue == NULL) {
13,619,390!
67
    return terrno;
×
68
  }
69

70
  int32_t code = taosThreadMutexInit(&(*queue)->mutex, NULL);
13,619,390✔
71
  if (code) {
13,619,378!
72
    taosMemoryFreeClear(*queue);
×
73
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
74
  }
75

76
  uDebug("queue:%p is opened", queue);
13,619,378✔
77
  return 0;
13,609,320✔
78
}
79

80
void taosSetQueueFp(STaosQueue *queue, FItem itemFp, FItems itemsFp) {
169,826✔
81
  if (queue == NULL) return;
169,826!
82
  queue->itemFp = itemFp;
169,826✔
83
  queue->itemsFp = itemsFp;
169,826✔
84
}
85

86
void taosCloseQueue(STaosQueue *queue) {
13,620,073✔
87
  if (queue == NULL) return;
13,620,073✔
88
  STaosQnode *pTemp;
89
  STaosQset  *qset;
90

91
  (void)taosThreadMutexLock(&queue->mutex);
13,620,072✔
92
  STaosQnode *pNode = queue->head;
13,624,874✔
93
  queue->head = NULL;
13,624,874✔
94
  qset = queue->qset;
13,624,874✔
95
  (void)taosThreadMutexUnlock(&queue->mutex);
13,624,874✔
96

97
  if (queue->qset) {
13,624,554✔
98
    taosRemoveFromQset(qset, queue);
80,997✔
99
  }
100

101
  while (pNode) {
13,620,413✔
102
    pTemp = pNode;
147✔
103
    pNode = pNode->next;
147✔
104
    taosMemoryFree(pTemp);
147!
105
  }
106

107
  (void)taosThreadMutexDestroy(&queue->mutex);
13,620,266✔
108
  taosMemoryFree(queue);
13,615,494✔
109

110
  uDebug("queue:%p is closed", queue);
13,620,266✔
111
}
112

113
bool taosQueueEmpty(STaosQueue *queue) {
43,552,258✔
114
  if (queue == NULL) return true;
43,552,258!
115

116
  bool empty = false;
43,552,258✔
117
  (void)taosThreadMutexLock(&queue->mutex);
43,552,258✔
118
  if (queue->head == NULL && queue->tail == NULL && queue->numOfItems == 0 /*&& queue->memOfItems == 0*/) {
43,622,472✔
119
    empty = true;
17,656,332✔
120
  }
121
  (void)taosThreadMutexUnlock(&queue->mutex);
43,622,472✔
122

123
  return empty;
43,650,964✔
124
}
125

126
void taosUpdateItemSize(STaosQueue *queue, int32_t items) {
100,739,641✔
127
  if (queue == NULL) return;
100,739,641!
128

129
  (void)taosThreadMutexLock(&queue->mutex);
100,739,641✔
130
  queue->numOfItems -= items;
100,777,148✔
131
  (void)taosThreadMutexUnlock(&queue->mutex);
100,777,148✔
132
}
133

134
int32_t taosQueueItemSize(STaosQueue *queue) {
51,724,921✔
135
  if (queue == NULL) return 0;
51,724,921!
136

137
  (void)taosThreadMutexLock(&queue->mutex);
51,724,921✔
138
  int32_t numOfItems = queue->numOfItems;
51,764,904✔
139
  (void)taosThreadMutexUnlock(&queue->mutex);
51,764,904✔
140

141
  uTrace("queue:%p, numOfItems:%d memOfItems:%" PRId64, queue, queue->numOfItems, queue->memOfItems);
51,762,298✔
142
  return numOfItems;
51,763,551✔
143
}
144

145
int64_t taosQueueMemorySize(STaosQueue *queue) {
×
146
  (void)taosThreadMutexLock(&queue->mutex);
×
147
  int64_t memOfItems = queue->memOfItems;
×
148
  (void)taosThreadMutexUnlock(&queue->mutex);
×
149
  return memOfItems;
×
150
}
151

152
int32_t taosAllocateQitem(int32_t size, EQItype itype, int64_t dataSize, void **item) {
140,962,424✔
153
  int64_t alloced = -1;
140,962,424✔
154

155
  if (itype == RPC_QITEM) {
140,962,424✔
156
    alloced = atomic_add_fetch_64(&tsQueueMemoryUsed, size + dataSize);
90,174,936✔
157
    if (alloced > tsQueueMemoryAllowed) {
90,291,610!
158
      uError("failed to alloc qitem, size:%" PRId64 " alloc:%" PRId64 " allowed:%" PRId64, size + dataSize, alloced,
×
159
             tsQueueMemoryAllowed);
160
      (void)atomic_sub_fetch_64(&tsQueueMemoryUsed, size + dataSize);
×
161
      return (terrno = TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE);
×
162
    }
163
  } else if (itype == APPLY_QITEM) {
50,787,488✔
164
    alloced = atomic_add_fetch_64(&tsApplyMemoryUsed, size + dataSize);
6,014,839✔
165
    if (alloced > tsApplyMemoryAllowed) {
6,014,839!
166
      uDebug("failed to alloc qitem, size:%" PRId64 " alloc:%" PRId64 " allowed:%" PRId64, size + dataSize, alloced,
×
167
             tsApplyMemoryAllowed);
168
      (void)atomic_sub_fetch_64(&tsApplyMemoryUsed, size + dataSize);
×
169
      return (terrno = TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE);
×
170
    }
171
  }
172

173
  *item = NULL;
141,079,098✔
174
  STaosQnode *pNode = taosMemoryCalloc(1, sizeof(STaosQnode) + size);
141,079,098!
175
  if (pNode == NULL) {
141,039,966!
176
    if (itype == RPC_QITEM) {
×
177
      (void)atomic_sub_fetch_64(&tsQueueMemoryUsed, size + dataSize);
×
178
    } else if (itype == APPLY_QITEM) {
×
179
      (void)atomic_sub_fetch_64(&tsApplyMemoryUsed, size + dataSize);
×
180
    }
181
    return terrno;
×
182
  }
183

184
  pNode->dataSize = dataSize;
141,039,966✔
185
  pNode->size = size;
141,039,966✔
186
  pNode->itype = itype;
141,039,966✔
187
  pNode->timestamp = taosGetTimestampUs();
141,067,843✔
188
  uTrace("item:%p, node:%p is allocated, alloc:%" PRId64, pNode->item, pNode, alloced);
141,067,843✔
189
  *item = pNode->item;
141,059,650✔
190
  return 0;
141,059,650✔
191
}
192

193
void taosFreeQitem(void *pItem) {
141,081,846✔
194
  if (pItem == NULL) return;
141,081,846!
195

196
  STaosQnode *pNode = (STaosQnode *)((char *)pItem - sizeof(STaosQnode));
141,081,846✔
197
  int64_t     alloced = -1;
141,081,846✔
198
  if (pNode->itype == RPC_QITEM) {
141,081,846✔
199
    alloced = atomic_sub_fetch_64(&tsQueueMemoryUsed, pNode->size + pNode->dataSize);
90,293,689✔
200
  } else if (pNode->itype == APPLY_QITEM) {
50,788,157✔
201
    alloced = atomic_sub_fetch_64(&tsApplyMemoryUsed, pNode->size + pNode->dataSize);
6,014,838✔
202
  }
203
  uTrace("item:%p, node:%p is freed, alloc:%" PRId64, pItem, pNode, alloced);
141,099,468✔
204

205
  taosMemoryFree(pNode);
141,099,468✔
206
}
207

208
int32_t taosWriteQitem(STaosQueue *queue, void *pItem) {
140,972,334✔
209
  int32_t     code = 0;
140,972,334✔
210
  STaosQnode *pNode = (STaosQnode *)(((char *)pItem) - sizeof(STaosQnode));
140,972,334✔
211
  pNode->timestamp = taosGetTimestampUs();
141,037,371✔
212
  pNode->next = NULL;
141,037,371✔
213

214
  (void)taosThreadMutexLock(&queue->mutex);
141,037,371✔
215
  if (queue->memLimit > 0 && (queue->memOfItems + pNode->size + pNode->dataSize) > queue->memLimit) {
141,166,817!
216
    code = TSDB_CODE_UTIL_QUEUE_OUT_OF_MEMORY;
×
217
    uError("item:%p, failed to put into queue:%p, queue mem limit:%" PRId64 ", reason:%s" PRId64, pItem, queue,
×
218
           queue->memLimit, tstrerror(code));
219

220
    (void)taosThreadMutexUnlock(&queue->mutex);
×
221
    return code;
×
222
  } else if (queue->itemLimit > 0 && queue->numOfItems + 1 > queue->itemLimit) {
141,166,817!
223
    code = TSDB_CODE_UTIL_QUEUE_OUT_OF_MEMORY;
×
224
    uError("item:%p, failed to put into queue:%p, queue size limit:%" PRId64 ", reason:%s" PRId64, pItem, queue,
×
225
           queue->itemLimit, tstrerror(code));
226
    (void)taosThreadMutexUnlock(&queue->mutex);
×
227
    return code;
×
228
  }
229

230
  if (queue->tail) {
141,166,817✔
231
    queue->tail->next = pNode;
33,228,861✔
232
    queue->tail = pNode;
33,228,861✔
233
  } else {
234
    queue->head = pNode;
107,937,956✔
235
    queue->tail = pNode;
107,937,956✔
236
  }
237
  queue->numOfItems++;
141,166,817✔
238
  queue->memOfItems += (pNode->size + pNode->dataSize);
141,166,817✔
239
  if (queue->qset) {
141,166,817✔
240
    (void)atomic_add_fetch_32(&queue->qset->numOfItems, 1);
115,047,902✔
241
  }
242

243
  uTrace("item:%p, is put into queue:%p, items:%d mem:%" PRId64, pItem, queue, queue->numOfItems, queue->memOfItems);
141,240,022✔
244

245
  (void)taosThreadMutexUnlock(&queue->mutex);
141,240,023✔
246

247
  if (queue->qset) {
141,180,383✔
248
    if (tsem_post(&queue->qset->sem) != 0) {
115,119,511!
249
      uError("failed to post semaphore for queue set:%p", queue->qset);
×
250
    } else {
251
      uDebug("sem_post Qset %p, sem:%p", queue->qset, &queue->qset->sem);
115,082,705✔
252
    }
253
  } else {
254
    uDebug("empty qset");
26,060,872✔
255
  }
256
  return code;
141,133,392✔
257
}
258

259
void taosReadQitem(STaosQueue *queue, void **ppItem) {
27,062,504✔
260
  STaosQnode *pNode = NULL;
27,062,504✔
261

262
  (void)taosThreadMutexLock(&queue->mutex);
27,062,504✔
263

264
  if (queue->head) {
27,088,762✔
265
    pNode = queue->head;
25,990,267✔
266
    *ppItem = pNode->item;
25,990,267✔
267
    queue->head = pNode->next;
25,990,267✔
268
    if (queue->head == NULL) {
25,990,267✔
269
      queue->tail = NULL;
10,623,844✔
270
    }
271
    queue->numOfItems--;
25,990,267✔
272
    queue->memOfItems -= (pNode->size + pNode->dataSize);
25,990,267✔
273
    if (queue->qset) {
25,990,267!
UNCOV
274
      (void)atomic_sub_fetch_32(&queue->qset->numOfItems, 1);
×
275
    }
276
    uTrace("item:%p, is read out from queue:%p, items:%d mem:%" PRId64, *ppItem, queue, queue->numOfItems,
25,990,267✔
277
           queue->memOfItems);
278
  }
279

280
  (void)taosThreadMutexUnlock(&queue->mutex);
27,088,762✔
281
}
27,107,981✔
282

283
int32_t taosAllocateQall(STaosQall **qall) {
80,346✔
284
  *qall = taosMemoryCalloc(1, sizeof(STaosQall));
80,346!
285
  if (*qall == NULL) {
80,344!
UNCOV
286
    return terrno;
×
287
  }
288
  return 0;
80,344✔
289
}
290

291
void taosFreeQall(STaosQall *qall) { taosMemoryFree(qall); }
80,345!
292

293
int32_t taosReadAllQitems(STaosQueue *queue, STaosQall *qall) {
×
UNCOV
294
  int32_t numOfItems = 0;
×
295
  bool    empty;
296

297
  (void)taosThreadMutexLock(&queue->mutex);
×
298

299
  empty = queue->head == NULL;
×
300
  if (!empty) {
×
301
    memset(qall, 0, sizeof(STaosQall));
×
UNCOV
302
    qall->current = queue->head;
×
303
    qall->start = queue->head;
×
304
    qall->numOfItems = queue->numOfItems;
×
UNCOV
305
    qall->memOfItems = queue->memOfItems;
×
306

UNCOV
307
    qall->unAccessedNumOfItems = queue->numOfItems;
×
308
    qall->unAccessMemOfItems = queue->memOfItems;
×
309

310
    numOfItems = qall->numOfItems;
×
311

312
    queue->head = NULL;
×
UNCOV
313
    queue->tail = NULL;
×
314
    queue->numOfItems = 0;
×
315
    queue->memOfItems = 0;
×
UNCOV
316
    uTrace("read %d items from queue:%p, items:%d mem:%" PRId64, numOfItems, queue, queue->numOfItems,
×
317
           queue->memOfItems);
UNCOV
318
    if (queue->qset) {
×
319
      (void)atomic_sub_fetch_32(&queue->qset->numOfItems, qall->numOfItems);
×
320
    }
321
  }
322

323
  (void)taosThreadMutexUnlock(&queue->mutex);
×
324

325
  // if source queue is empty, we set destination qall to empty too.
UNCOV
326
  if (empty) {
×
327
    qall->current = NULL;
×
UNCOV
328
    qall->start = NULL;
×
UNCOV
329
    qall->numOfItems = 0;
×
330
  }
UNCOV
331
  return numOfItems;
×
332
}
333

334
int32_t taosGetQitem(STaosQall *qall, void **ppItem) {
71,569,373✔
335
  STaosQnode *pNode;
336
  int32_t     num = 0;
71,569,373✔
337

338
  pNode = qall->current;
71,569,373✔
339
  if (pNode) qall->current = pNode->next;
71,569,373!
340

341
  if (pNode) {
71,569,373!
342
    *ppItem = pNode->item;
71,588,036✔
343
    num = 1;
71,588,036✔
344

345
    qall->unAccessedNumOfItems -= 1;
71,588,036✔
346
    qall->unAccessMemOfItems -= pNode->dataSize;
71,588,036✔
347

348
    uTrace("item:%p, is fetched", *ppItem);
71,588,036✔
349
  } else {
UNCOV
350
    *ppItem = NULL;
×
351
  }
352

353
  return num;
71,585,862✔
354
}
355

356
int32_t taosOpenQset(STaosQset **qset) {
153,353✔
357
  *qset = taosMemoryCalloc(sizeof(STaosQset), 1);
153,353!
358
  if (*qset == NULL) {
153,356!
UNCOV
359
    return terrno;
×
360
  }
361

362
  (void)taosThreadMutexInit(&(*qset)->mutex, NULL);
153,356✔
363
  if (tsem_init(&(*qset)->sem, 0, 0) != 0) {
153,357!
UNCOV
364
    taosMemoryFree(*qset);
×
UNCOV
365
    return terrno;
×
366
  }
367

368
  uDebug("qset:%p, is opened", qset);
153,355✔
369
  return 0;
153,355✔
370
}
371

372
void taosCloseQset(STaosQset *qset) {
153,358✔
373
  if (qset == NULL) return;
153,358✔
374

375
  // remove all the queues from qset
376
  (void)taosThreadMutexLock(&qset->mutex);
153,357✔
377
  while (qset->head) {
242,193✔
378
    STaosQueue *queue = qset->head;
88,836✔
379
    qset->head = qset->head->next;
88,836✔
380

381
    queue->qset = NULL;
88,836✔
382
    queue->next = NULL;
88,836✔
383
  }
384
  (void)taosThreadMutexUnlock(&qset->mutex);
153,357✔
385

386
  (void)taosThreadMutexDestroy(&qset->mutex);
153,357✔
387
  if (tsem_destroy(&qset->sem) != 0) {
153,357!
UNCOV
388
    uError("failed to destroy semaphore for qset:%p", qset);
×
389
  }
390
  taosMemoryFree(qset);
153,357!
391
  uDebug("qset:%p, is closed", qset);
153,357✔
392
}
393

394
// tsem_post 'qset->sem', so that reader threads waiting for it
395
// resumes execution and return, should only be used to signal the
396
// thread to exit.
397
void taosQsetThreadResume(STaosQset *qset) {
894,686✔
398
  uDebug("qset:%p, it will exit", qset);
894,686✔
399
  if (tsem_post(&qset->sem) != 0) {
894,686!
UNCOV
400
    uError("failed to post semaphore for qset:%p", qset);
×
401
  }
402
}
894,685✔
403

404
int32_t taosAddIntoQset(STaosQset *qset, STaosQueue *queue, void *ahandle) {
169,831✔
405
  if (queue->qset) return TSDB_CODE_INVALID_PARA;
169,831!
406

407
  (void)taosThreadMutexLock(&qset->mutex);
169,831✔
408

409
  queue->next = qset->head;
169,834✔
410
  queue->ahandle = ahandle;
169,834✔
411
  qset->head = queue;
169,834✔
412
  qset->numOfQueues++;
169,834✔
413

414
  (void)taosThreadMutexLock(&queue->mutex);
169,834✔
415
  (void)atomic_add_fetch_32(&qset->numOfItems, queue->numOfItems);
169,833✔
416
  queue->qset = qset;
169,831✔
417
  (void)taosThreadMutexUnlock(&queue->mutex);
169,831✔
418

419
  (void)taosThreadMutexUnlock(&qset->mutex);
169,832✔
420

421
  uTrace("queue:%p, is added into qset:%p", queue, qset);
169,834✔
422
  return 0;
169,834✔
423
}
424

425
void taosRemoveFromQset(STaosQset *qset, STaosQueue *queue) {
80,996✔
426
  STaosQueue *tqueue = NULL;
80,996✔
427

428
  (void)taosThreadMutexLock(&qset->mutex);
80,996✔
429

430
  if (qset->head) {
80,994!
431
    if (qset->head == queue) {
80,997✔
432
      qset->head = qset->head->next;
74,012✔
433
      tqueue = queue;
74,012✔
434
    } else {
435
      STaosQueue *prev = qset->head;
6,985✔
436
      tqueue = qset->head->next;
6,985✔
437
      while (tqueue) {
24,656!
438
        if (tqueue == queue) {
24,656✔
439
          prev->next = tqueue->next;
6,985✔
440
          break;
6,985✔
441
        } else {
442
          prev = tqueue;
17,671✔
443
          tqueue = tqueue->next;
17,671✔
444
        }
445
      }
446
    }
447

448
    if (tqueue) {
80,997✔
449
      if (qset->current == queue) qset->current = tqueue->next;
80,994✔
450
      qset->numOfQueues--;
80,994✔
451

452
      (void)taosThreadMutexLock(&queue->mutex);
80,994✔
453
      (void)atomic_sub_fetch_32(&qset->numOfItems, queue->numOfItems);
80,994✔
454
      queue->qset = NULL;
80,994✔
455
      queue->next = NULL;
80,994✔
456
      (void)taosThreadMutexUnlock(&queue->mutex);
80,994✔
457
    }
458
  }
459

460
  (void)taosThreadMutexUnlock(&qset->mutex);
80,997✔
461

462
  uDebug("queue:%p, is removed from qset:%p", queue, qset);
80,995✔
463
}
80,995✔
464

465
int32_t taosReadQitemFromQset(STaosQset *qset, void **ppItem, SQueueInfo *qinfo) {
44,320,795✔
466
  STaosQnode *pNode = NULL;
44,320,795✔
467
  int32_t     code = 0;
44,320,795✔
468

469
  uDebug("start to waitfromQset %p, sem:%p, idx:%d", qset, &qset->sem, qinfo->workerId);
44,320,795✔
470
  if (tsem_wait(&qset->sem) != 0) {
44,320,807!
UNCOV
471
    uError("failed to wait semaphore for qset:%p", qset);
×
472
  }
473
  uDebug("end waitfromQset %p, sem:%p, idx:%d", qset, &qset->sem, qinfo->workerId);
44,290,503✔
474

475
  (void)taosThreadMutexLock(&qset->mutex);
44,291,333✔
476

477
  for (int32_t i = 0; i < qset->numOfQueues; ++i) {
307,583,788✔
478
    if (qset->current == NULL) qset->current = qset->head;
306,765,036✔
479
    STaosQueue *queue = qset->current;
306,765,036✔
480
    if (queue) qset->current = queue->next;
306,765,036✔
481
    if (queue == NULL) break;
306,765,036!
482
    if (queue->head == NULL) continue;
306,765,036✔
483

484
    (void)taosThreadMutexLock(&queue->mutex);
43,514,161✔
485

486
    if (queue->head) {
43,518,890!
487
      pNode = queue->head;
43,518,919✔
488
      *ppItem = pNode->item;
43,518,919✔
489
      qinfo->ahandle = queue->ahandle;
43,518,919✔
490
      qinfo->fp = queue->itemFp;
43,518,919✔
491
      qinfo->queue = queue;
43,518,919✔
492
      qinfo->timestamp = pNode->timestamp;
43,518,919✔
493

494
      queue->head = pNode->next;
43,518,919✔
495
      if (queue->head == NULL) queue->tail = NULL;
43,518,919✔
496
      // queue->numOfItems--;
497
      queue->memOfItems -= (pNode->size + pNode->dataSize);
43,518,919✔
498
      (void)atomic_sub_fetch_32(&qset->numOfItems, 1);
43,518,919✔
499
      code = 1;
43,519,096✔
500
      uTrace("item:%p, is read out from queue:%p, items:%d mem:%" PRId64, *ppItem, queue, queue->numOfItems - 1,
43,519,096✔
501
             queue->memOfItems);
502
    }
503

504
    (void)taosThreadMutexUnlock(&queue->mutex);
43,519,067✔
505
    if (pNode) break;
43,519,167!
506
  }
507

508
  (void)taosThreadMutexUnlock(&qset->mutex);
44,337,973✔
509

510
  return code;
44,331,207✔
511
}
512

513
int32_t taosReadAllQitemsFromQset(STaosQset *qset, STaosQall *qall, SQueueInfo *qinfo) {
57,735,638✔
514
  STaosQueue *queue;
515
  int32_t     code = 0;
57,735,638✔
516

517
  if (tsem_wait(&qset->sem) != 0) {
57,735,638!
UNCOV
518
    uError("failed to wait semaphore for qset:%p", qset);
×
519
  }
520
  (void)taosThreadMutexLock(&qset->mutex);
57,733,538✔
521

522
  for (int32_t i = 0; i < qset->numOfQueues; ++i) {
157,883,860✔
523
    if (qset->current == NULL) qset->current = qset->head;
157,740,500✔
524
    queue = qset->current;
157,740,500✔
525
    if (queue) qset->current = queue->next;
157,740,500!
526
    if (queue == NULL) break;
157,740,500!
527
    if (queue->head == NULL) continue;
157,740,500✔
528

529
    (void)taosThreadMutexLock(&queue->mutex);
57,604,505✔
530

531
    if (queue->head) {
57,664,380!
532
      qall->current = queue->head;
57,665,049✔
533
      qall->start = queue->head;
57,665,049✔
534
      qall->numOfItems = queue->numOfItems;
57,665,049✔
535
      qall->memOfItems = queue->memOfItems;
57,665,049✔
536
      qall->unAccessedNumOfItems = queue->numOfItems;
57,665,049✔
537
      qall->unAccessMemOfItems = queue->memOfItems;
57,665,049✔
538

539
      code = qall->numOfItems;
57,665,049✔
540
      qinfo->ahandle = queue->ahandle;
57,665,049✔
541
      qinfo->fp = queue->itemsFp;
57,665,049✔
542
      qinfo->queue = queue;
57,665,049✔
543
      qinfo->timestamp = queue->head->timestamp;
57,665,049✔
544

545
      queue->head = NULL;
57,665,049✔
546
      queue->tail = NULL;
57,665,049✔
547
      // queue->numOfItems = 0;
548
      queue->memOfItems = 0;
57,665,049✔
549
      uTrace("read %d items from queue:%p, items:0 mem:%" PRId64, code, queue, queue->memOfItems);
57,665,049✔
550

551
      (void)atomic_sub_fetch_32(&qset->numOfItems, qall->numOfItems);
57,665,049✔
552
      for (int32_t j = 1; j < qall->numOfItems; ++j) {
71,627,088✔
553
        if (tsem_wait(&qset->sem) != 0) {
13,946,546!
UNCOV
554
          uError("failed to wait semaphore for qset:%p", qset);
×
555
        }
556
      }
557
    }
558

559
    (void)taosThreadMutexUnlock(&queue->mutex);
57,679,873✔
560

561
    if (code != 0) break;
57,667,845✔
562
  }
563

564
  (void)taosThreadMutexUnlock(&qset->mutex);
57,807,903✔
565
  return code;
57,751,187✔
566
}
567

568
int32_t taosQallItemSize(STaosQall *qall) { return qall->numOfItems; }
×
569
int64_t taosQallMemSize(STaosQall *qall) { return qall->memOfItems; }
×
570

UNCOV
571
int64_t taosQallUnAccessedItemSize(STaosQall *qall) { return qall->unAccessedNumOfItems; }
×
UNCOV
572
int64_t taosQallUnAccessedMemSize(STaosQall *qall) { return qall->unAccessMemOfItems; }
×
573

UNCOV
574
void    taosResetQitems(STaosQall *qall) { qall->current = qall->start; }
×
UNCOV
575
int32_t taosGetQueueNumber(STaosQset *qset) { return qset->numOfQueues; }
×
576

577
void taosQueueSetThreadId(STaosQueue *pQueue, int64_t threadId) { pQueue->threadId = threadId; }
85,793✔
578

579
int64_t taosQueueGetThreadId(STaosQueue *pQueue) { return pQueue->threadId; }
152,931✔
580

581
#if 0
582

583
void taosResetQsetThread(STaosQset *qset, void *pItem) {
584
  if (pItem == NULL) return;
585
  STaosQnode *pNode = (STaosQnode *)((char *)pItem - sizeof(STaosQnode));
586

587
  (void)taosThreadMutexLock(&qset->mutex);
588
  for (int32_t i = 0; i < pNode->queue->numOfItems; ++i) {
589
    tsem_post(&qset->sem);
590
  }
591
  (void)taosThreadMutexUnlock(&qset->mutex);
592
}
593

594
#endif
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