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

taosdata / TDengine / #4469

08 Jul 2025 09:38AM UTC coverage: 62.22% (-1.2%) from 63.381%
#4469

push

travis-ci

web-flow
Merge pull request #31712 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

153678 of 316510 branches covered (48.55%)

Branch coverage included in aggregate %.

56 of 60 new or added lines in 13 files covered. (93.33%)

5035 existing lines in 221 files now uncovered.

238955 of 314529 relevant lines covered (75.97%)

6273248.0 hits per line

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

71.56
/source/util/src/tworker.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 "tworker.h"
18
#include "taoserror.h"
19
#include "tcompare.h"
20
#include "tgeosctx.h"
21
#include "tlog.h"
22

23
#define QUEUE_THRESHOLD (1000 * 1000)
24

25
typedef void *(*ThreadFp)(void *param);
26

27
int32_t tQWorkerInit(SQWorkerPool *pool) {
26,949✔
28
  int32_t code = taosOpenQset(&pool->qset);
26,949✔
29
  if (code) return code;
26,949!
30

31
  pool->workers = taosMemoryCalloc(pool->max, sizeof(SQueueWorker));
26,949!
32
  if (pool->workers == NULL) {
26,949!
33
    taosCloseQset(pool->qset);
×
34
    return terrno;
×
35
  }
36

37
  (void)taosThreadMutexInit(&pool->mutex, NULL);
26,949✔
38

39
  for (int32_t i = 0; i < pool->max; ++i) {
179,316✔
40
    SQueueWorker *worker = pool->workers + i;
152,367✔
41
    worker->id = i;
152,367✔
42
    worker->pool = pool;
152,367✔
43
  }
44

45
  uInfo("worker:%s is initialized, min:%d max:%d", pool->name, pool->min, pool->max);
26,949!
46
  return 0;
26,949✔
47
}
48

49
void tQWorkerCleanup(SQWorkerPool *pool) {
26,949✔
50
  for (int32_t i = 0; i < pool->max; ++i) {
179,316✔
51
    SQueueWorker *worker = pool->workers + i;
152,367✔
52
    if (taosCheckPthreadValid(worker->thread)) {
152,367!
53
      taosQsetThreadResume(pool->qset);
152,367✔
54
    }
55
  }
56

57
  for (int32_t i = 0; i < pool->max; ++i) {
179,316✔
58
    SQueueWorker *worker = pool->workers + i;
152,367✔
59
    if (taosCheckPthreadValid(worker->thread)) {
152,367!
60
      uInfo("worker:%s:%d is stopping", pool->name, worker->id);
152,367!
61
      (void)taosThreadJoin(worker->thread, NULL);
152,367✔
62
      taosThreadClear(&worker->thread);
152,367✔
63
      uInfo("worker:%s:%d is stopped", pool->name, worker->id);
152,367!
64
    }
65
  }
66

67
  taosMemoryFreeClear(pool->workers);
26,949!
68
  taosCloseQset(pool->qset);
26,949✔
69
  (void)taosThreadMutexDestroy(&pool->mutex);
26,949✔
70

71
  uInfo("worker:%s is closed", pool->name);
26,949!
72
}
26,949✔
73

74
static void *tQWorkerThreadFp(SQueueWorker *worker) {
152,334✔
75
  SQWorkerPool *pool = worker->pool;
152,334✔
76
  SQueueInfo    qinfo = {0};
152,334✔
77
  void         *msg = NULL;
152,334✔
78
  int32_t       code = 0;
152,334✔
79

80
  int32_t ret = taosBlockSIGPIPE();
152,334✔
81
  if (ret < 0) {
152,336!
82
    uError("worker:%s:%d failed to block SIGPIPE", pool->name, worker->id);
×
83
  }
84

85
  setThreadName(pool->name);
152,336✔
86
  worker->pid = taosGetSelfPthreadId();
152,328✔
87
  uInfo("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
152,319!
88

89
  while (1) {
90
    if (taosReadQitemFromQset(pool->qset, (void **)&msg, &qinfo) == 0) {
2,060,760✔
91
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, pool->qset,
152,188!
92
            worker->pid);
93
      break;
152,362✔
94
    }
95

96
    if (qinfo.timestamp != 0) {
1,907,741!
97
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
1,908,280✔
98
      if (cost > QUEUE_THRESHOLD) {
1,908,280✔
99
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pool->name, cost / QUEUE_THRESHOLD);
2,561!
100
      }
101
    }
102

103
    if (qinfo.fp != NULL) {
1,908,203✔
104
      qinfo.workerId = worker->id;
1,908,077✔
105
      qinfo.threadNum = pool->num;
1,908,077✔
106
      (*((FItem)qinfo.fp))(&qinfo, msg);
1,908,077✔
107
    }
108

109
    taosUpdateItemSize(qinfo.queue, 1);
1,908,508✔
110
  }
111

112
  DestoryThreadLocalRegComp();
152,362✔
113

114
  return NULL;
152,354✔
115
}
116

117
STaosQueue *tQWorkerAllocQueue(SQWorkerPool *pool, void *ahandle, FItem fp) {
26,949✔
118
  int32_t     code;
119
  STaosQueue *queue;
120

121
  code = taosOpenQueue(&queue);
26,949✔
122
  if (code) {
26,949!
123
    terrno = code;
×
124
    return NULL;
×
125
  }
126

127
  (void)taosThreadMutexLock(&pool->mutex);
26,949✔
128
  taosSetQueueFp(queue, fp, NULL);
26,949✔
129
  code = taosAddIntoQset(pool->qset, queue, ahandle);
26,949✔
130
  if (code) {
26,949!
131
    taosCloseQueue(queue);
×
132
    (void)taosThreadMutexUnlock(&pool->mutex);
×
133
    terrno = code;
×
134
    return NULL;
×
135
  }
136

137
  // spawn a thread to process queue
138
  if (pool->num < pool->max) {
26,949!
139
    do {
140
      SQueueWorker *worker = pool->workers + pool->num;
152,367✔
141

142
      TdThreadAttr thAttr;
143
      (void)taosThreadAttrInit(&thAttr);
152,367✔
144
      (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
152,367✔
145

146
      if (taosThreadCreate(&worker->thread, &thAttr, (ThreadFp)tQWorkerThreadFp, worker) != 0) {
152,367!
147
        taosCloseQueue(queue);
×
148
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
149
        queue = NULL;
×
150
        break;
×
151
      }
152

153
      (void)taosThreadAttrDestroy(&thAttr);
152,367✔
154
      pool->num++;
152,367✔
155
      uInfo("worker:%s:%d is launched, total:%d", pool->name, worker->id, pool->num);
152,367!
156
    } while (pool->num < pool->min);
152,367✔
157
  }
158

159
  (void)taosThreadMutexUnlock(&pool->mutex);
26,949✔
160
  uInfo("worker:%s, queue:%p is allocated, ahandle:%p", pool->name, queue, ahandle);
26,949!
161

162
  return queue;
26,949✔
163
}
164

165
void tQWorkerFreeQueue(SQWorkerPool *pool, STaosQueue *queue) {
26,949✔
166
  uInfo("worker:%s, queue:%p is freed", pool->name, queue);
26,949!
167
  taosCloseQueue(queue);
26,949✔
168
}
26,949✔
169

170
int32_t tAutoQWorkerInit(SAutoQWorkerPool *pool) {
6,050✔
171
  int32_t code;
172

173
  code = taosOpenQset(&pool->qset);
6,050✔
174
  if (code) {
6,050!
175
    return terrno = code;
×
176
  }
177

178
  pool->workers = taosArrayInit(2, sizeof(SQueueWorker *));
6,050✔
179
  if (pool->workers == NULL) {
6,050!
180
    taosCloseQset(pool->qset);
×
181
    return terrno;
×
182
  }
183

184
  (void)taosThreadMutexInit(&pool->mutex, NULL);
6,050✔
185

186
  uInfo("worker:%s is initialized as auto", pool->name);
6,050!
187
  return 0;
6,050✔
188
}
189

190
void tAutoQWorkerCleanup(SAutoQWorkerPool *pool) {
6,050✔
191
  int32_t size = taosArrayGetSize(pool->workers);
6,050✔
192
  for (int32_t i = 0; i < size; ++i) {
15,820✔
193
    SQueueWorker *worker = taosArrayGetP(pool->workers, i);
9,770✔
194
    if (taosCheckPthreadValid(worker->thread)) {
9,770!
195
      taosQsetThreadResume(pool->qset);
9,770✔
196
    }
197
  }
198

199
  for (int32_t i = 0; i < size; ++i) {
15,820✔
200
    SQueueWorker *worker = taosArrayGetP(pool->workers, i);
9,770✔
201
    if (taosCheckPthreadValid(worker->thread)) {
9,770!
202
      uInfo("worker:%s:%d is stopping", pool->name, worker->id);
9,770!
203
      (void)taosThreadJoin(worker->thread, NULL);
9,770✔
204
      taosThreadClear(&worker->thread);
9,770✔
205
      uInfo("worker:%s:%d is stopped", pool->name, worker->id);
9,770!
206
    }
207
    taosMemoryFree(worker);
9,770!
208
  }
209

210
  taosArrayDestroy(pool->workers);
6,050✔
211
  taosCloseQset(pool->qset);
6,050✔
212
  (void)taosThreadMutexDestroy(&pool->mutex);
6,050✔
213

214
  uInfo("worker:%s is closed", pool->name);
6,050!
215
}
6,050✔
216

217
static void *tAutoQWorkerThreadFp(SQueueWorker *worker) {
9,770✔
218
  SAutoQWorkerPool *pool = worker->pool;
9,770✔
219
  SQueueInfo        qinfo = {0};
9,770✔
220
  void             *msg = NULL;
9,770✔
221
  int32_t           code = 0;
9,770✔
222

223
  int32_t ret = taosBlockSIGPIPE();
9,770✔
224
  if (ret < 0) {
9,770!
225
    uError("worker:%s:%d failed to block SIGPIPE", pool->name, worker->id);
×
226
  }
227

228
  setThreadName(pool->name);
9,770✔
229
  worker->pid = taosGetSelfPthreadId();
9,770✔
230
  uInfo("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
9,770!
231

232
  while (1) {
233
    if (taosReadQitemFromQset(pool->qset, (void **)&msg, &qinfo) == 0) {
117,411✔
234
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, pool->qset,
9,770!
235
            worker->pid);
236
      break;
9,770✔
237
    }
238

239
    if (qinfo.timestamp != 0) {
107,641!
240
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
107,640✔
241
      if (cost > QUEUE_THRESHOLD) {
107,640✔
242
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pool->name, cost / QUEUE_THRESHOLD);
314!
243
      }
244
    }
245

246
    if (qinfo.fp != NULL) {
107,640!
247
      qinfo.workerId = worker->id;
107,641✔
248
      qinfo.threadNum = taosArrayGetSize(pool->workers);
107,641✔
249
      (*((FItem)qinfo.fp))(&qinfo, msg);
107,637✔
250
    }
251

252
    taosUpdateItemSize(qinfo.queue, 1);
107,639✔
253
  }
254
  DestoryThreadLocalRegComp();
9,770✔
255

256
  return NULL;
9,770✔
257
}
258

259
STaosQueue *tAutoQWorkerAllocQueue(SAutoQWorkerPool *pool, void *ahandle, FItem fp, int32_t minNum) {
29,092✔
260
  int32_t     code;
261
  STaosQueue *queue;
262

263
  code = taosOpenQueue(&queue);
29,092✔
264
  if (code) {
29,092!
265
    terrno = code;
×
266
    return NULL;
×
267
  }
268

269
  (void)taosThreadMutexLock(&pool->mutex);
29,092✔
270
  taosSetQueueFp(queue, fp, NULL);
29,092✔
271

272
  code = taosAddIntoQset(pool->qset, queue, ahandle);
29,092✔
273
  if (code) {
29,092!
274
    taosCloseQueue(queue);
×
275
    (void)taosThreadMutexUnlock(&pool->mutex);
×
276
    terrno = code;
×
277
    return NULL;
×
278
  }
279

280
  int32_t queueNum = taosGetQueueNumber(pool->qset);
29,092✔
281
  int32_t curWorkerNum = taosArrayGetSize(pool->workers);
29,092✔
282
  int32_t dstWorkerNum = ceilf(queueNum * pool->ratio);
29,092✔
283

284
  if (dstWorkerNum < minNum) {
29,092✔
285
    dstWorkerNum = minNum;
8,895✔
286
  }
287

288
  // spawn a thread to process queue
289
  while (curWorkerNum < dstWorkerNum) {
38,862✔
290
    SQueueWorker *worker = taosMemoryCalloc(1, sizeof(SQueueWorker));
9,770!
291
    if (worker == NULL || taosArrayPush(pool->workers, &worker) == NULL) {
19,540!
292
      uError("worker:%s:%d failed to create", pool->name, curWorkerNum);
×
293
      taosMemoryFree(worker);
×
294
      taosCloseQueue(queue);
×
295
      (void)taosThreadMutexUnlock(&pool->mutex);
×
296
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
297
      return NULL;
×
298
    }
299
    worker->id = curWorkerNum;
9,770✔
300
    worker->pool = pool;
9,770✔
301

302
    TdThreadAttr thAttr;
303
    (void)taosThreadAttrInit(&thAttr);
9,770✔
304
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
9,770✔
305

306
    if (taosThreadCreate(&worker->thread, &thAttr, (ThreadFp)tAutoQWorkerThreadFp, worker) != 0) {
9,770!
307
      uError("worker:%s:%d failed to create thread, total:%d", pool->name, worker->id, curWorkerNum);
×
308
      void *tmp = taosArrayPop(pool->workers);
×
309
      taosMemoryFree(worker);
×
310
      taosCloseQueue(queue);
×
311
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
312
      return NULL;
×
313
    }
314

315
    (void)taosThreadAttrDestroy(&thAttr);
9,770✔
316
    int32_t numOfThreads = taosArrayGetSize(pool->workers);
9,770✔
317
    uInfo("worker:%s:%d is launched, total:%d, expect:%d", pool->name, worker->id, numOfThreads, dstWorkerNum);
9,770!
318

319
    curWorkerNum++;
9,770✔
320
  }
321

322
  (void)taosThreadMutexUnlock(&pool->mutex);
29,092✔
323
  uInfo("worker:%s, queue:%p is allocated, ahandle:%p", pool->name, queue, ahandle);
29,092!
324

325
  return queue;
29,092✔
326
}
327

328
void tAutoQWorkerFreeQueue(SAutoQWorkerPool *pool, STaosQueue *queue) {
29,092✔
329
  uInfo("worker:%s, queue:%p is freed", pool->name, queue);
29,092!
330
  taosCloseQueue(queue);
29,092✔
331
}
29,089✔
332

333
int32_t tWWorkerInit(SWWorkerPool *pool) {
67,339✔
334
  pool->nextId = 0;
67,339✔
335
  pool->workers = taosMemoryCalloc(pool->max, sizeof(SWWorker));
67,339!
336
  if (pool->workers == NULL) {
67,338!
337
    return terrno;
×
338
  }
339

340
  (void)taosThreadMutexInit(&pool->mutex, NULL);
67,338✔
341

342
  for (int32_t i = 0; i < pool->max; ++i) {
170,928✔
343
    SWWorker *worker = pool->workers + i;
103,590✔
344
    worker->id = i;
103,590✔
345
    worker->qall = NULL;
103,590✔
346
    worker->qset = NULL;
103,590✔
347
    worker->pool = pool;
103,590✔
348
  }
349

350
  uInfo("worker:%s is initialized, max:%d", pool->name, pool->max);
67,338!
351
  return 0;
67,339✔
352
}
353

354
void tWWorkerCleanup(SWWorkerPool *pool) {
67,334✔
355
  for (int32_t i = 0; i < pool->max; ++i) {
170,918✔
356
    SWWorker *worker = pool->workers + i;
103,586✔
357
    if (taosCheckPthreadValid(worker->thread)) {
103,586✔
358
      if (worker->qset) {
79,318!
359
        taosQsetThreadResume(worker->qset);
79,320✔
360
      }
361
    }
362
  }
363

364
  for (int32_t i = 0; i < pool->max; ++i) {
170,919✔
365
    SWWorker *worker = pool->workers + i;
103,581✔
366
    if (taosCheckPthreadValid(worker->thread)) {
103,581✔
367
      uInfo("worker:%s:%d is stopping", pool->name, worker->id);
79,315!
368
      (void)taosThreadJoin(worker->thread, NULL);
79,325✔
369
      taosThreadClear(&worker->thread);
79,325✔
370
      taosFreeQall(worker->qall);
79,325✔
371
      taosCloseQset(worker->qset);
79,325✔
372
      uInfo("worker:%s:%d is stopped", pool->name, worker->id);
79,324!
373
    }
374
  }
375

376
  taosMemoryFreeClear(pool->workers);
67,338!
377
  (void)taosThreadMutexDestroy(&pool->mutex);
67,338✔
378

379
  uInfo("worker:%s is closed", pool->name);
67,337!
380
}
67,337✔
381

382
static void *tWWorkerThreadFp(SWWorker *worker) {
79,321✔
383
  SWWorkerPool *pool = worker->pool;
79,321✔
384
  SQueueInfo    qinfo = {0};
79,321✔
385
  void         *msg = NULL;
79,321✔
386
  int32_t       code = 0;
79,321✔
387
  int32_t       numOfMsgs = 0;
79,321✔
388

389
  int32_t ret = taosBlockSIGPIPE();
79,321✔
390
  if (ret < 0) {
79,309!
391
    uError("worker:%s:%d failed to block SIGPIPE", pool->name, worker->id);
×
392
  }
393

394
  setThreadName(pool->name);
79,309✔
395
  worker->pid = taosGetSelfPthreadId();
79,317✔
396
  uInfo("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
79,313!
397

398
  while (1) {
399
    numOfMsgs = taosReadAllQitemsFromQset(worker->qset, worker->qall, &qinfo);
8,917,423✔
400
    if (numOfMsgs == 0) {
8,915,711✔
401
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, worker->qset,
78,992!
402
            worker->pid);
403
      break;
79,323✔
404
    }
405

406
    if (qinfo.timestamp != 0) {
8,836,719!
407
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
8,838,341✔
408
      if (cost > QUEUE_THRESHOLD) {
8,838,341✔
409
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pool->name, cost / QUEUE_THRESHOLD);
51!
410
      }
411
    }
412

413
    if (qinfo.fp != NULL) {
8,837,532✔
414
      qinfo.workerId = worker->id;
8,836,934✔
415
      qinfo.threadNum = pool->num;
8,836,934✔
416
      (*((FItems)qinfo.fp))(&qinfo, worker->qall, numOfMsgs);
8,836,934✔
417
    }
418
    taosUpdateItemSize(qinfo.queue, numOfMsgs);
8,838,649✔
419
  }
420

421
  return NULL;
79,323✔
422
}
423

424
STaosQueue *tWWorkerAllocQueue(SWWorkerPool *pool, void *ahandle, FItems fp) {
101,901✔
425
  (void)taosThreadMutexLock(&pool->mutex);
101,901✔
426
  SWWorker   *worker = pool->workers + pool->nextId;
101,902✔
427
  int32_t     code = -1;
101,902✔
428
  STaosQueue *queue;
429

430
  code = taosOpenQueue(&queue);
101,902✔
431
  if (code) goto _OVER;
101,901!
432

433
  taosSetQueueFp(queue, NULL, fp);
101,901✔
434
  if (worker->qset == NULL) {
101,901✔
435
    code = taosOpenQset(&worker->qset);
79,324✔
436
    if (code) goto _OVER;
79,325!
437

438
    code = taosAddIntoQset(worker->qset, queue, ahandle);
79,325✔
439
    if (code) goto _OVER;
79,325!
440
    code = taosAllocateQall(&worker->qall);
79,325✔
441
    if (code) goto _OVER;
79,324!
442

443
    TdThreadAttr thAttr;
444
    (void)taosThreadAttrInit(&thAttr);
79,324✔
445
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
79,324✔
446
    code = taosThreadCreate(&worker->thread, &thAttr, (ThreadFp)tWWorkerThreadFp, worker);
79,324✔
447
    if ((code)) goto _OVER;
79,324!
448

449
    uInfo("worker:%s:%d is launched, max:%d", pool->name, worker->id, pool->max);
79,324!
450
    pool->nextId = (pool->nextId + 1) % pool->max;
79,325✔
451

452
    (void)taosThreadAttrDestroy(&thAttr);
79,325✔
453
    pool->num++;
79,325✔
454
    if (pool->num > pool->max) pool->num = pool->max;
79,325✔
455
  } else {
456
    code = taosAddIntoQset(worker->qset, queue, ahandle);
22,577✔
457
    if (code) goto _OVER;
22,577!
458
    pool->nextId = (pool->nextId + 1) % pool->max;
22,577✔
459
  }
460

461
_OVER:
101,902✔
462
  (void)taosThreadMutexUnlock(&pool->mutex);
101,902✔
463

464
  if (code) {
101,902!
465
    if (queue != NULL) taosCloseQueue(queue);
×
466
    if (worker->qset != NULL) taosCloseQset(worker->qset);
×
467
    if (worker->qall != NULL) taosFreeQall(worker->qall);
×
468
    terrno = code;
×
469
    return NULL;
×
470
  } else {
471
    while (worker->pid <= 0) taosMsleep(10);
162,914✔
472

473
    taosQueueSetThreadId(queue, worker->pid);
101,892✔
474
    uInfo("worker:%s, queue:%p is allocated, ahandle:%p thread:%08" PRId64, pool->name, queue, ahandle, worker->pid);
101,902!
475
    return queue;
101,902✔
476
  }
477
}
478

479
void tWWorkerFreeQueue(SWWorkerPool *pool, STaosQueue *queue) {
101,891✔
480
  uInfo("worker:%s, queue:%p is freed", pool->name, queue);
101,891!
481
  taosCloseQueue(queue);
101,901✔
482
}
101,888✔
483

484
int32_t tSingleWorkerInit(SSingleWorker *pWorker, const SSingleWorkerCfg *pCfg) {
29,954✔
485
  int32_t code;
486
  pWorker->poolType = pCfg->poolType;
29,954✔
487
  pWorker->name = pCfg->name;
29,954✔
488

489
  switch (pCfg->poolType) {
29,954!
490
    case QWORKER_POOL: {
26,949✔
491
      SQWorkerPool *pPool = taosMemoryCalloc(1, sizeof(SQWorkerPool));
26,949!
492
      if (!pPool) {
26,949!
493
        return terrno;
×
494
      }
495
      pPool->name = pCfg->name;
26,949✔
496
      pPool->min = pCfg->min;
26,949✔
497
      pPool->max = pCfg->max;
26,949✔
498
      pWorker->pool = pPool;
26,949✔
499
      if ((code = tQWorkerInit(pPool))) {
26,949!
500
        return (terrno = code);
×
501
      }
502

503
      pWorker->queue = tQWorkerAllocQueue(pPool, pCfg->param, pCfg->fp);
26,949✔
504
      if (pWorker->queue == NULL) {
26,949!
505
        return terrno;
×
506
      }
507
    } break;
26,949✔
508
    case QUERY_AUTO_QWORKER_POOL: {
3,005✔
509
      SQueryAutoQWorkerPool *pPool = taosMemoryCalloc(1, sizeof(SQueryAutoQWorkerPool));
3,005!
510
      if (!pPool) {
3,005!
511
        return terrno;
×
512
      }
513
      pPool->name = pCfg->name;
3,005✔
514
      pPool->min = pCfg->min;
3,005✔
515
      pPool->max = pCfg->max;
3,005✔
516
      pWorker->pool = pPool;
3,005✔
517

518
      code = tQueryAutoQWorkerInit(pPool);
3,005✔
519
      if (code) return code;
3,005!
520

521
      pWorker->queue = tQueryAutoQWorkerAllocQueue(pPool, pCfg->param, pCfg->fp);
3,005✔
522
      if (!pWorker->queue) {
3,005!
523
        return terrno;
×
524
      }
525
    } break;
3,005✔
526
    default:
×
527
      return TSDB_CODE_INVALID_PARA;
×
528
  }
529
  return 0;
29,954✔
530
}
531

532
void tSingleWorkerCleanup(SSingleWorker *pWorker) {
29,954✔
533
  if (pWorker->queue == NULL) return;
29,954!
534
  while (!taosQueueEmpty(pWorker->queue)) {
32,590✔
535
    taosMsleep(10);
2,636✔
536
  }
537

538
  switch (pWorker->poolType) {
29,954!
539
    case QWORKER_POOL:
26,949✔
540
      tQWorkerCleanup(pWorker->pool);
26,949✔
541
      tQWorkerFreeQueue(pWorker->pool, pWorker->queue);
26,949✔
542
      taosMemoryFree(pWorker->pool);
26,949!
543
      break;
26,949✔
544
    case QUERY_AUTO_QWORKER_POOL:
3,005✔
545
      tQueryAutoQWorkerCleanup(pWorker->pool);
3,005✔
546
      tQueryAutoQWorkerFreeQueue(pWorker->pool, pWorker->queue);
3,005✔
547
      taosMemoryFree(pWorker->pool);
3,005!
548
      break;
3,005✔
549
    default:
×
550
      break;
×
551
  }
552
}
553

554
int32_t tMultiWorkerInit(SMultiWorker *pWorker, const SMultiWorkerCfg *pCfg) {
58,264✔
555
  SWWorkerPool *pPool = &pWorker->pool;
58,264✔
556
  pPool->name = pCfg->name;
58,264✔
557
  pPool->max = pCfg->max;
58,264✔
558

559
  int32_t code = tWWorkerInit(pPool);
58,264✔
560
  if (code) return code;
58,264!
561

562
  pWorker->queue = tWWorkerAllocQueue(pPool, pCfg->param, pCfg->fp);
58,264✔
563
  if (pWorker->queue == NULL) {
58,264!
564
    return terrno;
×
565
  }
566

567
  pWorker->name = pCfg->name;
58,264✔
568
  return 0;
58,264✔
569
}
570

571
void tMultiWorkerCleanup(SMultiWorker *pWorker) {
58,262✔
572
  if (pWorker->queue == NULL) return;
58,262!
573

574
  while (!taosQueueEmpty(pWorker->queue)) {
62,954✔
575
    taosMsleep(10);
4,696✔
576
  }
577

578
  tWWorkerCleanup(&pWorker->pool);
58,264✔
579
  tWWorkerFreeQueue(&pWorker->pool, pWorker->queue);
58,264✔
580
}
581

582
static int32_t tQueryAutoQWorkerAddWorker(SQueryAutoQWorkerPool *pool);
583
static int32_t tQueryAutoQWorkerBeforeBlocking(void *p);
584
static int32_t tQueryAutoQWorkerRecoverFromBlocking(void *p);
585
static void    tQueryAutoQWorkerWaitingCheck(SQueryAutoQWorkerPool *pPool);
586
static bool    tQueryAutoQWorkerTryRecycleWorker(SQueryAutoQWorkerPool *pPool, SQueryAutoQWorker *pWorker);
587

588
#define GET_ACTIVE_N(int64_val)  (int32_t)((int64_val) >> 32)
589
#define GET_RUNNING_N(int64_val) (int32_t)(int64_val & 0xFFFFFFFF)
590

591
static int32_t atomicFetchSubActive(int64_t *ptr, int32_t val) {
×
592
  int64_t acutalSubVal = val;
×
593
  acutalSubVal <<= 32;
×
594
  int64_t newVal64 = atomic_fetch_sub_64(ptr, acutalSubVal);
×
595
  return GET_ACTIVE_N(newVal64);
×
596
}
597

598
static int32_t atomicFetchSubRunning(int64_t *ptr, int32_t val) { return GET_RUNNING_N(atomic_fetch_sub_64(ptr, val)); }
11,904,980✔
599

600
static int32_t atomicFetchAddActive(int64_t *ptr, int32_t val) {
1,656,208✔
601
  int64_t actualAddVal = val;
1,656,208✔
602
  actualAddVal <<= 32;
1,656,208✔
603
  int64_t newVal64 = atomic_fetch_add_64(ptr, actualAddVal);
1,656,208✔
604
  return GET_ACTIVE_N(newVal64);
1,656,208✔
605
}
606

607
static int32_t atomicFetchAddRunning(int64_t *ptr, int32_t val) { return GET_RUNNING_N(atomic_fetch_add_64(ptr, val)); }
×
608

609
static bool atomicCompareExchangeActive(int64_t *ptr, int32_t *expectedVal, int32_t newVal) {
11✔
610
  int64_t oldVal64 = *expectedVal, newVal64 = newVal;
11✔
611
  int32_t running = GET_RUNNING_N(*ptr);
11✔
612
  oldVal64 <<= 32;
11✔
613
  newVal64 <<= 32;
11✔
614
  oldVal64 |= running;
11✔
615
  newVal64 |= running;
11✔
616
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
11✔
617
  if (actualNewVal64 == oldVal64) {
11!
618
    return true;
11✔
619
  } else {
620
    *expectedVal = GET_ACTIVE_N(actualNewVal64);
×
621
    return false;
×
622
  }
623
}
624

625
static int64_t atomicCompareExchangeRunning(int64_t *ptr, int32_t *expectedVal, int32_t newVal) {
×
626
  int64_t oldVal64 = *expectedVal, newVal64 = newVal;
×
627
  int64_t activeShifted = GET_ACTIVE_N(*ptr);
×
628
  activeShifted <<= 32;
×
629
  oldVal64 |= activeShifted;
×
630
  newVal64 |= activeShifted;
×
631
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
×
632
  if (actualNewVal64 == oldVal64) {
×
633
    return true;
×
634
  } else {
635
    *expectedVal = GET_RUNNING_N(actualNewVal64);
×
636
    return false;
×
637
  }
638
}
639

640
static int64_t atomicCompareExchangeActiveAndRunning(int64_t *ptr, int32_t *expectedActive, int32_t newActive,
14,491,229✔
641
                                                     int32_t *expectedRunning, int32_t newRunning) {
642
  int64_t oldVal64 = *expectedActive, newVal64 = newActive;
14,491,229✔
643
  oldVal64 <<= 32;
14,491,229✔
644
  oldVal64 |= *expectedRunning;
14,491,229✔
645
  newVal64 <<= 32;
14,491,229✔
646
  newVal64 |= newRunning;
14,491,229✔
647
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
14,491,229✔
648
  if (actualNewVal64 == oldVal64) {
14,492,334✔
649
    return true;
14,465,926✔
650
  } else {
651
    *expectedActive = GET_ACTIVE_N(actualNewVal64);
26,408✔
652
    *expectedRunning = GET_RUNNING_N(actualNewVal64);
26,408✔
653
    return false;
26,408✔
654
  }
655
}
656

657
static void *tQueryAutoQWorkerThreadFp(SQueryAutoQWorker *worker) {
1,657,536✔
658
  SQueryAutoQWorkerPool *pool = worker->pool;
1,657,536✔
659
  SQueueInfo             qinfo = {0};
1,657,536✔
660
  void                  *msg = NULL;
1,657,536✔
661
  int32_t                code = 0;
1,657,536✔
662

663
  int32_t ret = taosBlockSIGPIPE();
1,657,536✔
664
  if (ret < 0) {
1,657,803!
665
    uError("worker:%s:%d failed to block SIGPIPE", pool->name, worker->id);
×
666
  }
667

668
  setThreadName(pool->name);
1,657,803✔
669
  worker->pid = taosGetSelfPthreadId();
1,657,272✔
670
  uDebug("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
1,656,637✔
671

672
  while (1) {
673
    if (taosReadQitemFromQset(pool->qset, (void **)&msg, &qinfo) == 0) {
13,560,683✔
674
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, pool->qset,
1,653,667!
675
            worker->pid);
676
      break;
1,652,632✔
677
    }
678

679
    if (qinfo.timestamp != 0) {
11,903,658!
680
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
11,905,158✔
681
      if (cost > QUEUE_THRESHOLD) {
11,905,158!
682
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pool->name, cost / QUEUE_THRESHOLD);
×
683
      }
684
    }
685

686
    tQueryAutoQWorkerWaitingCheck(pool);
11,903,090✔
687

688
    if (qinfo.fp != NULL) {
11,906,247!
689
      qinfo.workerId = worker->id;
11,906,255✔
690
      qinfo.threadNum = pool->num;
11,906,255✔
691
      qinfo.workerCb = pool->pCb;
11,906,255✔
692
      (*((FItem)qinfo.fp))(&qinfo, msg);
11,906,255✔
693
    }
694

695
    taosUpdateItemSize(qinfo.queue, 1);
11,903,691✔
696
    if (!tQueryAutoQWorkerTryRecycleWorker(pool, worker)) {
11,906,232✔
697
      uDebug("worker:%s:%d exited", pool->name, worker->id);
1,993✔
698
      break;
1,993✔
699
    }
700
  }
701

702
  DestoryThreadLocalRegComp();
1,654,625✔
703

704
  return NULL;
1,653,344✔
705
}
706

707
static bool tQueryAutoQWorkerTrySignalWaitingAfterBlock(void *p) {
13,185,436✔
708
  SQueryAutoQWorkerPool *pPool = p;
13,185,436✔
709
  bool                   ret = false;
13,185,436✔
710
  int32_t                waiting = pPool->waitingAfterBlockN;
13,185,436✔
711
  while (waiting > 0) {
13,185,436✔
712
    int32_t waitingNew = atomic_val_compare_exchange_32(&pPool->waitingAfterBlockN, waiting, waiting - 1);
67✔
713
    if (waitingNew == waiting) {
67!
714
      (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
67✔
715
      (void)taosThreadCondSignal(&pPool->waitingAfterBlockCond);
67✔
716
      (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
67✔
717
      ret = true;
79✔
718
      break;
79✔
719
    }
UNCOV
720
    waiting = waitingNew;
×
721
  }
722
  return ret;
13,185,448✔
723
}
724

725
static bool tQueryAutoQWorkerTrySignalWaitingBeforeProcess(void *p) {
13,185,562✔
726
  SQueryAutoQWorkerPool *pPool = p;
13,185,562✔
727
  bool                   ret = false;
13,185,562✔
728
  int32_t                waiting = pPool->waitingBeforeProcessMsgN;
13,185,562✔
729
  while (waiting > 0) {
13,185,562✔
730
    int32_t waitingNew = atomic_val_compare_exchange_32(&pPool->waitingBeforeProcessMsgN, waiting, waiting - 1);
11✔
731
    if (waitingNew == waiting) {
11!
732
      (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
11✔
733
      (void)taosThreadCondSignal(&pPool->waitingBeforeProcessMsgCond);
11✔
734
      (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
11✔
735
      ret = true;
21✔
736
      break;
21✔
737
    }
738
    waiting = waitingNew;
×
739
  }
740
  return ret;
13,185,572✔
741
}
742

743
static bool tQueryAutoQWorkerTryDecActive(void *p, int32_t minActive) {
13,185,496✔
744
  SQueryAutoQWorkerPool *pPool = p;
13,185,496✔
745
  bool                   ret = false;
13,185,496✔
746
  int64_t                val64 = pPool->activeRunningN;
13,185,496✔
747
  int32_t                active = GET_ACTIVE_N(val64), running = GET_RUNNING_N(val64);
13,185,496✔
748
  while (active > minActive) {
13,185,851✔
749
    if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active - 1, &running, running - 1))
1,280,152✔
750
      return true;
1,279,824✔
751
  }
752
  return false;
11,905,699✔
753
}
754

755
static void tQueryAutoQWorkerWaitingCheck(SQueryAutoQWorkerPool *pPool) {
11,905,196✔
756
  while (1) {
149✔
757
    int64_t val64 = pPool->activeRunningN;
11,905,196✔
758
    int32_t running = GET_RUNNING_N(val64), active = GET_ACTIVE_N(val64);
11,905,196✔
759
    while (running < pPool->num) {
11,929,844!
760
      if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active, &running, running + 1)) {
11,931,111✔
761
        return;
11,906,266✔
762
      }
763
    }
764
    if (atomicCompareExchangeActive(&pPool->activeRunningN, &active, active - 1)) {
×
765
      break;
×
766
    }
767
  }
768
  // to wait for process
769
  (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
×
770
  (void)atomic_fetch_add_32(&pPool->waitingBeforeProcessMsgN, 1);
11✔
771
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingBeforeProcessMsgCond, &pPool->waitingBeforeProcessMsgLock);
11!
772
  // recovered from waiting
773
  (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
11✔
774
  return;
11✔
775
}
776

777
bool tQueryAutoQWorkerTryRecycleWorker(SQueryAutoQWorkerPool *pPool, SQueryAutoQWorker *pWorker) {
11,905,758✔
778
  if (tQueryAutoQWorkerTrySignalWaitingAfterBlock(pPool) || tQueryAutoQWorkerTrySignalWaitingBeforeProcess(pPool) ||
23,811,516!
779
      tQueryAutoQWorkerTryDecActive(pPool, pPool->num)) {
11,905,733✔
780
    (void)taosThreadMutexLock(&pPool->poolLock);
685,087✔
781
    SListNode *pNode = listNode(pWorker);
684,721✔
782
    SListNode *tNode = tdListPopNode(pPool->workers, pNode);
684,721✔
783
    // reclaim some workers
784
    if (pWorker->id >= pPool->maxInUse) {
684,721!
785
      while (listNEles(pPool->exitedWorkers) > pPool->maxInUse - pPool->num) {
×
786
        SListNode         *head = tdListPopHead(pPool->exitedWorkers);
×
787
        SQueryAutoQWorker *pWorker = (SQueryAutoQWorker *)head->data;
×
788
        if (pWorker && taosCheckPthreadValid(pWorker->thread)) {
×
789
          (void)taosThreadJoin(pWorker->thread, NULL);
×
790
          taosThreadClear(&pWorker->thread);
×
791
        }
792
        taosMemoryFree(head);
×
793
      }
794
      tdListAppendNode(pPool->exitedWorkers, pNode);
×
795
      (void)taosThreadMutexUnlock(&pPool->poolLock);
×
796
      return false;
×
797
    }
798

799
    // put back to backup pool
800
    tdListAppendNode(pPool->backupWorkers, pNode);
684,721✔
801
    (void)taosThreadMutexUnlock(&pPool->poolLock);
684,720✔
802

803
    // start to wait at backup cond
804
    (void)taosThreadMutexLock(&pPool->backupLock);
684,721✔
805
    (void)atomic_fetch_add_32(&pPool->backupNum, 1);
684,721✔
806
    if (!pPool->exit) (void)taosThreadCondWait(&pPool->backupCond, &pPool->backupLock);
684,721!
807
    (void)taosThreadMutexUnlock(&pPool->backupLock);
684,719✔
808

809
    // recovered from backup
810
    (void)taosThreadMutexLock(&pPool->poolLock);
684,719✔
811
    if (pPool->exit) {
684,721✔
812
      (void)taosThreadMutexUnlock(&pPool->poolLock);
1,993✔
813
      return false;
1,993✔
814
    }
815
    SListNode *tNode1 = tdListPopNode(pPool->backupWorkers, pNode);
682,728✔
816
    tdListAppendNode(pPool->workers, pNode);
682,728✔
817
    (void)taosThreadMutexUnlock(&pPool->poolLock);
682,728✔
818

819
    return true;
682,728✔
820
  } else {
821
    (void)atomicFetchSubRunning(&pPool->activeRunningN, 1);
11,220,666✔
822
    return true;
11,221,384✔
823
  }
824
}
825

826
int32_t tQueryAutoQWorkerInit(SQueryAutoQWorkerPool *pool) {
23,001✔
827
  int32_t code;
828

829
  pool->exit = false;
23,001✔
830

831
  (void)taosThreadMutexInit(&pool->poolLock, NULL);
23,001✔
832
  (void)taosThreadMutexInit(&pool->backupLock, NULL);
23,001✔
833
  (void)taosThreadMutexInit(&pool->waitingAfterBlockLock, NULL);
23,001✔
834
  (void)taosThreadMutexInit(&pool->waitingBeforeProcessMsgLock, NULL);
23,001✔
835

836
  (void)taosThreadCondInit(&pool->waitingBeforeProcessMsgCond, NULL);
23,001✔
837
  (void)taosThreadCondInit(&pool->waitingAfterBlockCond, NULL);
23,001✔
838
  (void)taosThreadCondInit(&pool->backupCond, NULL);
23,001✔
839

840
  code = taosOpenQset(&pool->qset);
23,001✔
841
  if (code) return terrno = code;
23,001!
842
  pool->workers = tdListNew(sizeof(SQueryAutoQWorker));
23,001✔
843
  if (!pool->workers) return terrno;
23,001!
844
  pool->backupWorkers = tdListNew(sizeof(SQueryAutoQWorker));
23,001✔
845
  if (!pool->backupWorkers) return terrno;
23,001!
846
  pool->exitedWorkers = tdListNew(sizeof(SQueryAutoQWorker));
23,001✔
847
  if (!pool->exitedWorkers) return terrno;
23,001!
848
  pool->maxInUse = pool->max * 2 + 2;
23,001✔
849

850
  if (!pool->pCb) {
23,001!
851
    pool->pCb = taosMemoryCalloc(1, sizeof(SQueryAutoQWorkerPoolCB));
23,001!
852
    if (!pool->pCb) return terrno;
23,001!
853
    pool->pCb->pPool = pool;
23,001✔
854
    pool->pCb->beforeBlocking = tQueryAutoQWorkerBeforeBlocking;
23,001✔
855
    pool->pCb->afterRecoverFromBlocking = tQueryAutoQWorkerRecoverFromBlocking;
23,001✔
856
  }
857
  return TSDB_CODE_SUCCESS;
23,001✔
858
}
859

860
void tQueryAutoQWorkerCleanup(SQueryAutoQWorkerPool *pPool) {
23,001✔
861
  (void)taosThreadMutexLock(&pPool->poolLock);
23,001✔
862
  pPool->exit = true;
23,001✔
863
  int32_t size = 0;
23,001✔
864
  if (pPool->workers) {
23,001!
865
    size = listNEles(pPool->workers);
23,001✔
866
  }
867
  if (pPool->backupWorkers) {
23,001!
868
    size += listNEles(pPool->backupWorkers);
23,001✔
869
  }
870
  if (pPool->qset) {
23,001!
871
    for (int32_t i = 0; i < size; ++i) {
1,681,199✔
872
      taosQsetThreadResume(pPool->qset);
1,658,198✔
873
    }
874
  }
875
  (void)taosThreadMutexUnlock(&pPool->poolLock);
23,001✔
876

877
  (void)taosThreadMutexLock(&pPool->backupLock);
23,001✔
878
  (void)taosThreadCondBroadcast(&pPool->backupCond);
23,001✔
879
  (void)taosThreadMutexUnlock(&pPool->backupLock);
23,001✔
880

881
  (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
23,001✔
882
  (void)taosThreadCondBroadcast(&pPool->waitingAfterBlockCond);
23,001✔
883
  (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
23,001✔
884

885
  (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
23,001✔
886
  (void)taosThreadCondBroadcast(&pPool->waitingBeforeProcessMsgCond);
23,001✔
887
  (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
23,001✔
888

889
  int32_t            idx = 0;
23,001✔
890
  SQueryAutoQWorker *worker = NULL;
23,001✔
891
  while (pPool->workers) {
1,679,206!
892
    (void)taosThreadMutexLock(&pPool->poolLock);
1,679,206✔
893
    if (listNEles(pPool->workers) == 0) {
1,679,206✔
894
      (void)taosThreadMutexUnlock(&pPool->poolLock);
23,001✔
895
      break;
23,001✔
896
    }
897
    SListNode *pNode = tdListPopHead(pPool->workers);
1,656,205✔
898
    worker = (SQueryAutoQWorker *)pNode->data;
1,656,205✔
899
    (void)taosThreadMutexUnlock(&pPool->poolLock);
1,656,205✔
900
    if (worker && taosCheckPthreadValid(worker->thread)) {
1,656,205!
901
      (void)taosThreadJoin(worker->thread, NULL);
1,656,205✔
902
      taosThreadClear(&worker->thread);
1,656,205✔
903
    }
904
    taosMemoryFree(pNode);
1,656,205!
905
  }
906

907
  while (pPool->backupWorkers && listNEles(pPool->backupWorkers) > 0) {
24,994!
908
    SListNode *pNode = tdListPopHead(pPool->backupWorkers);
1,993✔
909
    worker = (SQueryAutoQWorker *)pNode->data;
1,993✔
910
    if (worker && taosCheckPthreadValid(worker->thread)) {
1,993!
911
      (void)taosThreadJoin(worker->thread, NULL);
1,993✔
912
      taosThreadClear(&worker->thread);
1,993✔
913
    }
914
    taosMemoryFree(pNode);
1,993!
915
  }
916

917
  while (pPool->exitedWorkers && listNEles(pPool->exitedWorkers) > 0) {
23,001!
918
    SListNode *pNode = tdListPopHead(pPool->exitedWorkers);
×
919
    worker = (SQueryAutoQWorker *)pNode->data;
×
920
    if (worker && taosCheckPthreadValid(worker->thread)) {
×
921
      (void)taosThreadJoin(worker->thread, NULL);
×
922
      taosThreadClear(&worker->thread);
×
923
    }
924
    taosMemoryFree(pNode);
×
925
  }
926

927
  pPool->workers = tdListFree(pPool->workers);
23,001✔
928
  pPool->backupWorkers = tdListFree(pPool->backupWorkers);
23,001✔
929
  pPool->exitedWorkers = tdListFree(pPool->exitedWorkers);
23,001✔
930
  taosMemoryFree(pPool->pCb);
23,001!
931

932
  (void)taosThreadMutexDestroy(&pPool->poolLock);
23,001✔
933
  (void)taosThreadMutexDestroy(&pPool->backupLock);
23,001✔
934
  (void)taosThreadMutexDestroy(&pPool->waitingAfterBlockLock);
23,001✔
935
  (void)taosThreadMutexDestroy(&pPool->waitingBeforeProcessMsgLock);
23,001✔
936

937
  (void)taosThreadCondDestroy(&pPool->backupCond);
23,001✔
938
  (void)taosThreadCondDestroy(&pPool->waitingAfterBlockCond);
23,001✔
939
  (void)taosThreadCondDestroy(&pPool->waitingBeforeProcessMsgCond);
23,001✔
940
  taosCloseQset(pPool->qset);
23,001✔
941
}
23,001✔
942

943
STaosQueue *tQueryAutoQWorkerAllocQueue(SQueryAutoQWorkerPool *pool, void *ahandle, FItem fp) {
34,522✔
944
  STaosQueue *queue;
945
  int32_t     code = taosOpenQueue(&queue);
34,522✔
946
  if (code) {
34,522!
947
    terrno = code;
×
948
    return NULL;
×
949
  }
950

951
  (void)taosThreadMutexLock(&pool->poolLock);
34,522✔
952
  taosSetQueueFp(queue, fp, NULL);
34,522✔
953
  code = taosAddIntoQset(pool->qset, queue, ahandle);
34,522✔
954
  if (code) {
34,522!
955
    taosCloseQueue(queue);
×
956
    queue = NULL;
×
957
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
958
    return NULL;
×
959
  }
960
  SQueryAutoQWorker  worker = {0};
34,522✔
961
  SQueryAutoQWorker *pWorker = NULL;
34,522✔
962

963
  // spawn a thread to process queue
964
  if (pool->num < pool->max) {
34,522✔
965
    do {
966
      worker.id = listNEles(pool->workers);
1,656,208✔
967
      worker.backupIdx = -1;
1,656,208✔
968
      worker.pool = pool;
1,656,208✔
969
      SListNode *pNode = tdListAdd(pool->workers, &worker);
1,656,208✔
970
      if (!pNode) {
1,656,208!
971
        taosCloseQueue(queue);
×
972
        queue = NULL;
×
973
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
974
        break;
×
975
      }
976
      pWorker = (SQueryAutoQWorker *)pNode->data;
1,656,208✔
977

978
      TdThreadAttr thAttr;
979
      (void)taosThreadAttrInit(&thAttr);
1,656,208✔
980
      (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
1,656,208✔
981

982
      if (taosThreadCreate(&pWorker->thread, &thAttr, (ThreadFp)tQueryAutoQWorkerThreadFp, pWorker) != 0) {
1,656,208!
983
        taosCloseQueue(queue);
×
984
        queue = NULL;
×
985
        break;
×
986
      }
987

988
      (void)taosThreadAttrDestroy(&thAttr);
1,656,208✔
989
      pool->num++;
1,656,208✔
990
      (void)atomicFetchAddActive(&pool->activeRunningN, 1);
1,656,208✔
991
      uInfo("worker:%s:%d is launched, total:%d", pool->name, pWorker->id, pool->num);
1,656,208!
992
    } while (pool->num < pool->min);
1,656,208✔
993
  }
994

995
  (void)taosThreadMutexUnlock(&pool->poolLock);
34,522✔
996
  uInfo("worker:%s, queue:%p is allocated, ahandle:%p", pool->name, queue, ahandle);
34,522!
997

998
  return queue;
34,522✔
999
}
1000

1001
void tQueryAutoQWorkerFreeQueue(SQueryAutoQWorkerPool *pPool, STaosQueue *pQ) { taosCloseQueue(pQ); }
17,550✔
1002

1003
static int32_t tQueryAutoQWorkerAddWorker(SQueryAutoQWorkerPool *pool) {
684,661✔
1004
  // try backup pool
1005
  int32_t backup = pool->backupNum;
684,661✔
1006
  while (backup > 0) {
684,763✔
1007
    int32_t backupNew = atomic_val_compare_exchange_32(&pool->backupNum, backup, backup - 1);
682,765✔
1008
    if (backupNew == backup) {
682,831✔
1009
      (void)taosThreadCondSignal(&pool->backupCond);
682,729✔
1010
      return TSDB_CODE_SUCCESS;
682,729✔
1011
    }
1012
    backup = backupNew;
102✔
1013
  }
1014
  // backup pool is empty, create new
1015
  SQueryAutoQWorker *pWorker = NULL;
1,998✔
1016
  SQueryAutoQWorker  worker = {0};
1,998✔
1017
  worker.pool = pool;
1,998✔
1018
  worker.backupIdx = -1;
1,998✔
1019
  (void)taosThreadMutexLock(&pool->poolLock);
1,998✔
1020
  worker.id = listNEles(pool->workers);
1,990✔
1021
  SListNode *pNode = tdListAdd(pool->workers, &worker);
1,990✔
1022
  if (!pNode) {
1,990!
1023
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
1024
    return terrno;
×
1025
  }
1026
  (void)taosThreadMutexUnlock(&pool->poolLock);
1,990✔
1027
  pWorker = (SQueryAutoQWorker *)pNode->data;
1,990✔
1028

1029
  TdThreadAttr thAttr;
1030
  (void)taosThreadAttrInit(&thAttr);
1,990✔
1031
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
1,990✔
1032

1033
  if (taosThreadCreate(&pWorker->thread, &thAttr, (ThreadFp)tQueryAutoQWorkerThreadFp, pWorker) != 0) {
1,990!
1034
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1035
    return terrno;
×
1036
  }
1037
  (void)taosThreadAttrDestroy(&thAttr);
1,990✔
1038

1039
  return TSDB_CODE_SUCCESS;
1,990✔
1040
}
1041

1042
static int32_t tQueryAutoQWorkerBeforeBlocking(void *p) {
1,279,804✔
1043
  SQueryAutoQWorkerPool *pPool = p;
1,279,804✔
1044
  if (tQueryAutoQWorkerTrySignalWaitingAfterBlock(p) || tQueryAutoQWorkerTrySignalWaitingBeforeProcess(p) ||
2,559,652!
1045
      tQueryAutoQWorkerTryDecActive(p, pPool->num)) {
1,279,822✔
1046
  } else {
1047
    int32_t code = tQueryAutoQWorkerAddWorker(pPool);
684,704✔
1048
    if (code != TSDB_CODE_SUCCESS) {
684,719!
1049
      return code;
×
1050
    }
1051
    (void)atomicFetchSubRunning(&pPool->activeRunningN, 1);
684,719✔
1052
  }
1053

1054
  return TSDB_CODE_SUCCESS;
1,279,902✔
1055
}
1056

1057
static int32_t tQueryAutoQWorkerRecoverFromBlocking(void *p) {
1,279,816✔
1058
  SQueryAutoQWorkerPool *pPool = p;
1,279,816✔
1059
  int64_t                val64 = pPool->activeRunningN;
1,279,816✔
1060
  int32_t                running = GET_RUNNING_N(val64), active = GET_ACTIVE_N(val64);
1,279,816✔
1061
  while (running < pPool->num) {
1,280,854✔
1062
    if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active + 1, &running, running + 1)) {
1,280,815✔
1063
      return TSDB_CODE_SUCCESS;
1,279,836✔
1064
    }
1065
  }
1066
  (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
39✔
1067
  (void)atomic_fetch_add_32(&pPool->waitingAfterBlockN, 1);
67✔
1068
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingAfterBlockCond, &pPool->waitingAfterBlockLock);
67!
1069
  (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
67✔
1070
  if (pPool->exit) return TSDB_CODE_QRY_QWORKER_QUIT;
67!
1071
  return TSDB_CODE_SUCCESS;
67✔
1072
}
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