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

taosdata / TDengine / #5044

06 May 2026 02:35AM UTC coverage: 73.169% (+0.06%) from 73.107%
#5044

push

travis-ci

web-flow
feat: [6659794715] cpu limit (#35153)

244 of 275 new or added lines in 23 files covered. (88.73%)

526 existing lines in 141 files now uncovered.

277745 of 379596 relevant lines covered (73.17%)

133740972.66 hits per line

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

74.74
/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
#include "ttrace.h"
23
#include "tcurl.h"
24

25
#define QUEUE_THRESHOLD (1000 * 1000)
26

27
typedef void *(*ThreadFp)(void *param);
28

29
int32_t tQWorkerInit(SQWorkerPool *pool) {
6,012,664✔
30
  int32_t code = taosOpenQset(&pool->qset);
6,012,664✔
31
  if (code) return code;
6,012,664✔
32

33
  pool->workers = taosMemoryCalloc(pool->max, sizeof(SQueueWorker));
6,012,664✔
34
  if (pool->workers == NULL) {
6,012,664✔
35
    taosCloseQset(pool->qset);
×
36
    return terrno;
×
37
  }
38

39
  (void)taosThreadMutexInit(&pool->mutex, NULL);
6,012,664✔
40

41
  for (int32_t i = 0; i < pool->max; ++i) {
39,958,559✔
42
    SQueueWorker *worker = pool->workers + i;
33,945,895✔
43
    worker->id = i;
33,945,895✔
44
    worker->pool = pool;
33,945,895✔
45
  }
46

47
  uInfo("worker:%s is initialized, min:%d max:%d", pool->name, pool->min, pool->max);
6,012,664✔
48
  return 0;
6,012,664✔
49
}
50

51
void tQWorkerCleanup(SQWorkerPool *pool) {
6,012,664✔
52
  for (int32_t i = 0; i < pool->max; ++i) {
39,958,559✔
53
    SQueueWorker *worker = pool->workers + i;
33,945,895✔
54
    if (taosCheckPthreadValid(worker->thread)) {
33,945,895✔
55
      taosQsetThreadResume(pool->qset);
33,945,895✔
56
    }
57
  }
58

59
  for (int32_t i = 0; i < pool->max; ++i) {
39,958,559✔
60
    SQueueWorker *worker = pool->workers + i;
33,945,895✔
61
    if (taosCheckPthreadValid(worker->thread)) {
33,945,895✔
62
      uInfo("worker:%s:%d is stopping", pool->name, worker->id);
33,945,895✔
63
      (void)taosThreadJoin(worker->thread, NULL);
33,945,895✔
64
      taosThreadClear(&worker->thread);
33,945,895✔
65
      uInfo("worker:%s:%d is stopped", pool->name, worker->id);
33,945,895✔
66
    }
67
  }
68

69
  taosMemoryFreeClear(pool->workers);
6,012,664✔
70
  taosCloseQset(pool->qset);
6,012,664✔
71
  (void)taosThreadMutexDestroy(&pool->mutex);
6,012,664✔
72

73
  uInfo("worker:%s is closed", pool->name);
6,012,664✔
74
}
6,012,664✔
75

76
static void *tQWorkerThreadFp(SQueueWorker *worker) {
33,940,831✔
77
  SQWorkerPool *pool = worker->pool;
33,940,831✔
78
  SQueueInfo    qinfo = {0};
33,941,207✔
79
  void         *msg = NULL;
33,944,645✔
80
  int32_t       code = 0;
33,944,645✔
81

82
  int32_t ret = taosBlockSIGPIPE();
33,944,645✔
83
  if (ret < 0) {
33,941,635✔
84
    uError("worker:%s:%d failed to block SIGPIPE", pool->name, worker->id);
×
85
  }
86

87
  setThreadName(pool->name);
33,941,635✔
88
  if (pool->threadCategory >= 0) taosSetCpuAffinity((EThreadCategory)pool->threadCategory);
33,944,942✔
89
  worker->pid = taosGetSelfPthreadId();
33,936,940✔
90
  uInfo("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
33,938,706✔
91

92
  while (1) {
93
    if (taosReadQitemFromQset(pool->qset, (void **)&msg, &qinfo) == 0) {
519,847,350✔
94
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, pool->qset,
33,925,113✔
95
            worker->pid);
96
      break;
33,942,853✔
97
    }
98

99
    if (qinfo.timestamp != 0) {
485,903,161✔
100
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
485,904,253✔
101
      if (cost > QUEUE_THRESHOLD) {
485,904,253✔
102
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pool->name, cost / QUEUE_THRESHOLD);
1,108,458✔
103
      }
104
    }
105

106
    if (qinfo.fp != NULL) {
485,905,035✔
107
      qinfo.workerId = worker->id;
485,903,867✔
108
      qinfo.threadNum = pool->num;
485,903,239✔
109
      (*((FItem)qinfo.fp))(&qinfo, msg);
485,902,960✔
110
    }
111

112
    taosUpdateItemSize(qinfo.queue, 1);
485,901,291✔
113
  }
114

115
  DestoryThreadLocalRegComp();
33,942,853✔
116

117
  return NULL;
33,943,751✔
118
}
119

120
STaosQueue *tQWorkerAllocQueue(SQWorkerPool *pool, void *ahandle, FItem fp) {
6,012,664✔
121
  int32_t     code;
122
  STaosQueue *queue;
6,001,035✔
123

124
  code = taosOpenQueue(&queue);
6,012,664✔
125
  if (code) {
6,012,664✔
126
    terrno = code;
×
127
    return NULL;
×
128
  }
129

130
  (void)taosThreadMutexLock(&pool->mutex);
6,012,664✔
131
  taosSetQueueFp(queue, fp, NULL);
6,012,664✔
132
  code = taosAddIntoQset(pool->qset, queue, ahandle);
6,012,664✔
133
  if (code) {
6,012,664✔
134
    taosCloseQueue(queue);
×
135
    (void)taosThreadMutexUnlock(&pool->mutex);
×
136
    terrno = code;
×
137
    return NULL;
×
138
  }
139

140
  // spawn a thread to process queue
141
  if (pool->num < pool->max) {
6,012,664✔
142
    do {
143
      SQueueWorker *worker = pool->workers + pool->num;
33,945,895✔
144

145
      TdThreadAttr thAttr;
33,889,998✔
146
      (void)taosThreadAttrInit(&thAttr);
33,945,895✔
147
      (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
33,945,895✔
148

149
      if (taosThreadCreate(&worker->thread, &thAttr, (ThreadFp)tQWorkerThreadFp, worker) != 0) {
33,945,895✔
150
        taosCloseQueue(queue);
×
151
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
152
        queue = NULL;
×
153
        break;
×
154
      }
155

156
      (void)taosThreadAttrDestroy(&thAttr);
33,945,895✔
157
      pool->num++;
33,945,895✔
158
      uInfo("worker:%s:%d is launched, total:%d", pool->name, worker->id, pool->num);
33,945,895✔
159
    } while (pool->num < pool->min);
33,945,895✔
160
  }
161

162
  (void)taosThreadMutexUnlock(&pool->mutex);
6,012,664✔
163
  uInfo("worker:%s, queue:%p is allocated, ahandle:%p", pool->name, queue, ahandle);
6,012,664✔
164

165
  return queue;
6,012,664✔
166
}
167

168
void tQWorkerFreeQueue(SQWorkerPool *pool, STaosQueue *queue) {
6,012,664✔
169
  uInfo("worker:%s, queue:%p is freed", pool->name, queue);
6,012,664✔
170
  taosCloseQueue(queue);
6,012,664✔
171
}
6,012,664✔
172

173
int32_t tAutoQWorkerInit(SAutoQWorkerPool *pool) {
×
174
  int32_t code;
175

176
  code = taosOpenQset(&pool->qset);
×
177
  if (code) {
×
178
    return terrno = code;
×
179
  }
180

181
  pool->workers = taosArrayInit(2, sizeof(SQueueWorker *));
×
182
  if (pool->workers == NULL) {
×
183
    taosCloseQset(pool->qset);
×
184
    return terrno;
×
185
  }
186

187
  (void)taosThreadMutexInit(&pool->mutex, NULL);
×
188

189
  uInfo("worker:%s is initialized as auto", pool->name);
×
190
  return 0;
×
191
}
192

193
void tAutoQWorkerCleanup(SAutoQWorkerPool *pool) {
×
194
  int32_t size = taosArrayGetSize(pool->workers);
×
195
  for (int32_t i = 0; i < size; ++i) {
×
196
    SQueueWorker *worker = taosArrayGetP(pool->workers, i);
×
197
    if (taosCheckPthreadValid(worker->thread)) {
×
198
      taosQsetThreadResume(pool->qset);
×
199
    }
200
  }
201

202
  for (int32_t i = 0; i < size; ++i) {
×
203
    SQueueWorker *worker = taosArrayGetP(pool->workers, i);
×
204
    if (taosCheckPthreadValid(worker->thread)) {
×
205
      uInfo("worker:%s:%d is stopping", pool->name, worker->id);
×
206
      (void)taosThreadJoin(worker->thread, NULL);
×
207
      taosThreadClear(&worker->thread);
×
208
      uInfo("worker:%s:%d is stopped", pool->name, worker->id);
×
209
    }
210
    taosMemoryFree(worker);
×
211
  }
212

213
  taosArrayDestroy(pool->workers);
×
214
  taosCloseQset(pool->qset);
×
215
  (void)taosThreadMutexDestroy(&pool->mutex);
×
216

217
  uInfo("worker:%s is closed", pool->name);
×
218
}
×
219

220
static void *tAutoQWorkerThreadFp(SQueueWorker *worker) {
×
221
  SAutoQWorkerPool *pool = worker->pool;
×
222
  SQueueInfo        qinfo = {0};
×
223
  void             *msg = NULL;
×
224
  int32_t           code = 0;
×
225

226
  int32_t ret = taosBlockSIGPIPE();
×
227
  if (ret < 0) {
×
228
    uError("worker:%s:%d failed to block SIGPIPE", pool->name, worker->id);
×
229
  }
230

231
  setThreadName(pool->name);
×
NEW
232
  if (pool->threadCategory >= 0) taosSetCpuAffinity((EThreadCategory)pool->threadCategory);
×
233
  worker->pid = taosGetSelfPthreadId();
×
234
  uInfo("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
×
235

236
  while (1) {
237
    if (taosReadQitemFromQset(pool->qset, (void **)&msg, &qinfo) == 0) {
×
238
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, pool->qset,
×
239
            worker->pid);
240
      break;
×
241
    }
242

243
    if (qinfo.timestamp != 0) {
×
244
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
×
245
      if (cost > QUEUE_THRESHOLD) {
×
246
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pool->name, cost / QUEUE_THRESHOLD);
×
247
      }
248
    }
249

250
    if (qinfo.fp != NULL) {
×
251
      qinfo.workerId = worker->id;
×
252
      qinfo.threadNum = taosArrayGetSize(pool->workers);
×
253
      (*((FItem)qinfo.fp))(&qinfo, msg);
×
254
    }
255

256
    taosUpdateItemSize(qinfo.queue, 1);
×
257
  }
258
  DestoryThreadLocalRegComp();
×
259
  closeThreadNotificationConn();
×
260

261
  return NULL;
×
262
}
263

264
STaosQueue *tAutoQWorkerAllocQueue(SAutoQWorkerPool *pool, void *ahandle, FItem fp, int32_t minNum) {
×
265
  int32_t     code;
266
  STaosQueue *queue;
×
267

268
  code = taosOpenQueue(&queue);
×
269
  if (code) {
×
270
    terrno = code;
×
271
    return NULL;
×
272
  }
273

274
  (void)taosThreadMutexLock(&pool->mutex);
×
275
  taosSetQueueFp(queue, fp, NULL);
×
276

277
  code = taosAddIntoQset(pool->qset, queue, ahandle);
×
278
  if (code) {
×
279
    taosCloseQueue(queue);
×
280
    (void)taosThreadMutexUnlock(&pool->mutex);
×
281
    terrno = code;
×
282
    return NULL;
×
283
  }
284

285
  int32_t queueNum = taosGetQueueNumber(pool->qset);
×
286
  int32_t curWorkerNum = taosArrayGetSize(pool->workers);
×
287
  int32_t dstWorkerNum = ceilf(queueNum * pool->ratio);
×
288

289
  if (dstWorkerNum < minNum) {
×
290
    dstWorkerNum = minNum;
×
291
  }
292

293
  // spawn a thread to process queue
294
  while (curWorkerNum < dstWorkerNum) {
×
295
    SQueueWorker *worker = taosMemoryCalloc(1, sizeof(SQueueWorker));
×
296
    if (worker == NULL || taosArrayPush(pool->workers, &worker) == NULL) {
×
297
      uError("worker:%s:%d failed to create", pool->name, curWorkerNum);
×
298
      taosMemoryFree(worker);
×
299
      taosCloseQueue(queue);
×
300
      (void)taosThreadMutexUnlock(&pool->mutex);
×
301
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
302
      return NULL;
×
303
    }
304
    worker->id = curWorkerNum;
×
305
    worker->pool = pool;
×
306

307
    TdThreadAttr thAttr;
×
308
    (void)taosThreadAttrInit(&thAttr);
×
309
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
×
310

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

320
    (void)taosThreadAttrDestroy(&thAttr);
×
321
    int32_t numOfThreads = taosArrayGetSize(pool->workers);
×
322
    uInfo("worker:%s:%d is launched, total:%d, expect:%d", pool->name, worker->id, numOfThreads, dstWorkerNum);
×
323

324
    curWorkerNum++;
×
325
  }
326

327
  (void)taosThreadMutexUnlock(&pool->mutex);
×
328
  uInfo("worker:%s, queue:%p is allocated, ahandle:%p", pool->name, queue, ahandle);
×
329

330
  return queue;
×
331
}
332

333
void tAutoQWorkerFreeQueue(SAutoQWorkerPool *pool, STaosQueue *queue) {
×
334
  uInfo("worker:%s, queue:%p is freed", pool->name, queue);
×
335
  taosCloseQueue(queue);
×
336
}
×
337

338
int32_t tWWorkerInit(SWWorkerPool *pool) {
19,736,243✔
339
  pool->nextId = 0;
19,736,243✔
340
  pool->workers = taosMemoryCalloc(pool->max, sizeof(SWWorker));
19,735,554✔
341
  if (pool->workers == NULL) {
19,736,833✔
342
    return terrno;
×
343
  }
344

345
  (void)taosThreadMutexInit(&pool->mutex, NULL);
19,736,197✔
346

347
  for (int32_t i = 0; i < pool->max; ++i) {
46,065,881✔
348
    SWWorker *worker = pool->workers + i;
26,330,445✔
349
    worker->id = i;
26,329,809✔
350
    worker->qall = NULL;
26,329,039✔
351
    worker->qset = NULL;
26,329,120✔
352
    worker->pool = pool;
26,329,250✔
353
  }
354

355
  uInfo("worker:%s is initialized, max:%d", pool->name, pool->max);
19,735,105✔
356
  return 0;
19,736,371✔
357
}
358

359
void tWWorkerCleanup(SWWorkerPool *pool) {
19,736,377✔
360
  for (int32_t i = 0; i < pool->max; ++i) {
46,066,822✔
361
    SWWorker *worker = pool->workers + i;
26,329,497✔
362
    if (taosCheckPthreadValid(worker->thread)) {
26,330,445✔
363
      if (worker->qset) {
22,163,024✔
364
        taosQsetThreadResume(worker->qset);
22,163,024✔
365
      }
366
    }
367
  }
368

369
  for (int32_t i = 0; i < pool->max; ++i) {
46,067,585✔
370
    SWWorker *worker = pool->workers + i;
26,330,445✔
371
    if (taosCheckPthreadValid(worker->thread)) {
26,330,445✔
372
      uInfo("worker:%s:%d is stopping", pool->name, worker->id);
22,163,024✔
373
      (void)taosThreadJoin(worker->thread, NULL);
22,163,253✔
374
      taosThreadClear(&worker->thread);
22,163,024✔
375
      taosFreeQall(worker->qall);
22,163,024✔
376
      taosCloseQset(worker->qset);
22,163,024✔
377
      uInfo("worker:%s:%d is stopped", pool->name, worker->id);
22,162,255✔
378
    }
379
  }
380

381
  taosMemoryFreeClear(pool->workers);
19,737,029✔
382
  (void)taosThreadMutexDestroy(&pool->mutex);
19,736,788✔
383

384
  uInfo("worker:%s is closed", pool->name);
19,737,140✔
385
}
19,737,218✔
386

387
static void *tWWorkerThreadFp(SWWorker *worker) {
22,163,024✔
388
  SWWorkerPool *pool = worker->pool;
22,163,024✔
389
  SQueueInfo    qinfo = {0};
22,163,024✔
390
  void         *msg = NULL;
22,163,024✔
391
  int32_t       code = 0;
22,163,024✔
392
  int32_t       numOfMsgs = 0;
22,163,024✔
393

394
  int32_t ret = taosBlockSIGPIPE();
22,163,024✔
395
  if (ret < 0) {
22,154,651✔
396
    uError("worker:%s:%d failed to block SIGPIPE", pool->name, worker->id);
×
397
  }
398

399
  setThreadName(pool->name);
22,154,651✔
400
  if (pool->threadCategory >= 0) taosSetCpuAffinity((EThreadCategory)pool->threadCategory);
22,163,024✔
401
  worker->pid = taosGetSelfPthreadId();
22,142,013✔
402
  uInfo("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
22,149,087✔
403

404
  while (1) {
405
    numOfMsgs = taosReadAllQitemsFromQset(worker->qset, worker->qall, &qinfo);
2,147,483,647✔
406
    if (numOfMsgs == 0) {
2,147,483,647✔
407
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, worker->qset,
22,155,048✔
408
            worker->pid);
409
      break;
22,163,024✔
410
    }
411

412
    if (qinfo.timestamp != 0) {
2,147,483,647✔
413
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
2,147,483,647✔
414
      if (cost > QUEUE_THRESHOLD) {
2,147,483,647✔
415
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pool->name, cost / QUEUE_THRESHOLD);
46,534✔
416
      }
417
    }
418

419
    if (qinfo.fp != NULL) {
2,147,483,647✔
420
      qinfo.workerId = worker->id;
2,147,483,647✔
421
      qinfo.threadNum = pool->num;
2,147,483,647✔
422
      (*((FItems)qinfo.fp))(&qinfo, worker->qall, numOfMsgs);
2,147,483,647✔
423
    }
424
    taosUpdateItemSize(qinfo.queue, numOfMsgs);
2,147,483,647✔
425
  }
426

427
  return NULL;
22,163,024✔
428
}
429

430
STaosQueue *tWWorkerAllocQueue(SWWorkerPool *pool, void *ahandle, FItems fp) {
23,699,013✔
431
  (void)taosThreadMutexLock(&pool->mutex);
23,699,013✔
432
  SWWorker   *worker = pool->workers + pool->nextId;
23,699,722✔
433
  int32_t     code = -1;
23,699,722✔
434
  STaosQueue *queue;
23,684,708✔
435

436
  code = taosOpenQueue(&queue);
23,699,722✔
437
  if (code) goto _OVER;
23,699,722✔
438

439
  taosSetQueueFp(queue, NULL, fp);
23,699,722✔
440
  if (worker->qset == NULL) {
23,699,722✔
441
    code = taosOpenQset(&worker->qset);
22,163,024✔
442
    if (code) goto _OVER;
22,163,024✔
443

444
    code = taosAddIntoQset(worker->qset, queue, ahandle);
22,163,024✔
445
    if (code) goto _OVER;
22,163,024✔
446
    code = taosAllocateQall(&worker->qall);
22,163,024✔
447
    if (code) goto _OVER;
22,162,315✔
448

449
    TdThreadAttr thAttr;
22,148,815✔
450
    (void)taosThreadAttrInit(&thAttr);
22,163,024✔
451
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
22,163,024✔
452
    code = taosThreadCreate(&worker->thread, &thAttr, (ThreadFp)tWWorkerThreadFp, worker);
22,163,024✔
453
    if ((code)) goto _OVER;
22,163,024✔
454

455
    uInfo("worker:%s:%d is launched, max:%d", pool->name, worker->id, pool->max);
22,163,024✔
456
    pool->nextId = (pool->nextId + 1) % pool->max;
22,163,024✔
457

458
    (void)taosThreadAttrDestroy(&thAttr);
22,163,024✔
459
    pool->num++;
22,163,024✔
460
    if (pool->num > pool->max) pool->num = pool->max;
22,163,024✔
461
  } else {
462
    code = taosAddIntoQset(worker->qset, queue, ahandle);
1,536,698✔
463
    if (code) goto _OVER;
1,536,698✔
464
    pool->nextId = (pool->nextId + 1) % pool->max;
1,536,698✔
465
  }
466

467
_OVER:
23,699,722✔
468
  (void)taosThreadMutexUnlock(&pool->mutex);
23,698,924✔
469

470
  if (code) {
23,699,722✔
471
    if (queue != NULL) taosCloseQueue(queue);
×
472
    if (worker->qset != NULL) taosCloseQset(worker->qset);
×
473
    if (worker->qall != NULL) taosFreeQall(worker->qall);
×
474
    terrno = code;
×
475
    return NULL;
×
476
  } else {
477
    while (worker->pid <= 0) taosMsleep(10);
48,555,172✔
478

479
    taosQueueSetThreadId(queue, worker->pid);
23,699,180✔
480
    uInfo("worker:%s, queue:%p is allocated, ahandle:%p thread:%08" PRId64, pool->name, queue, ahandle, worker->pid);
23,698,103✔
481
    return queue;
23,699,722✔
482
  }
483
}
484

485
void tWWorkerFreeQueue(SWWorkerPool *pool, STaosQueue *queue) {
23,699,722✔
486
  uInfo("worker:%s, queue:%p is freed", pool->name, queue);
23,699,722✔
487
  taosCloseQueue(queue);
23,699,722✔
488
}
23,699,207✔
489

490
int32_t tSingleWorkerInit(SSingleWorker *pWorker, const SSingleWorkerCfg *pCfg) {
7,089,570✔
491
  int32_t code;
492
  pWorker->poolType = pCfg->poolType;
7,089,570✔
493
  pWorker->name = pCfg->name;
7,089,570✔
494
  pWorker->stopNoWaitQueue = pCfg->stopNoWaitQueue;
7,089,570✔
495

496
  switch (pCfg->poolType) {
7,089,570✔
497
    case QWORKER_POOL: {
6,012,664✔
498
      SQWorkerPool *pPool = taosMemoryCalloc(1, sizeof(SQWorkerPool));
6,012,664✔
499
      if (!pPool) {
6,012,664✔
500
        return terrno;
×
501
      }
502
      pPool->name = pCfg->name;
6,012,664✔
503
      pPool->min = pCfg->min;
6,012,664✔
504
      pPool->max = pCfg->max;
6,012,664✔
505
      pPool->threadCategory = pCfg->threadCategory;
6,012,664✔
506
      pWorker->pool = pPool;
6,012,664✔
507
      if ((code = tQWorkerInit(pPool))) {
6,012,664✔
508
        return (terrno = code);
×
509
      }
510

511
      pWorker->queue = tQWorkerAllocQueue(pPool, pCfg->param, pCfg->fp);
6,012,664✔
512
      if (pWorker->queue == NULL) {
6,012,664✔
513
        return terrno;
×
514
      }
515
    } break;
6,012,664✔
516
    case QUERY_AUTO_QWORKER_POOL: {
1,076,906✔
517
      SQueryAutoQWorkerPool *pPool = taosMemoryCalloc(1, sizeof(SQueryAutoQWorkerPool));
1,076,906✔
518
      if (!pPool) {
1,076,906✔
519
        return terrno;
×
520
      }
521
      pPool->name = pCfg->name;
1,076,906✔
522
      pPool->min = pCfg->min;
1,076,906✔
523
      pPool->max = pCfg->max;
1,076,906✔
524
      pPool->stopNoWaitQueue = pCfg->stopNoWaitQueue;
1,076,906✔
525
      pPool->threadCategory = pCfg->threadCategory;
1,076,906✔
526
      pWorker->pool = pPool;
1,076,906✔
527

528
      code = tQueryAutoQWorkerInit(pPool);
1,076,906✔
529
      if (code) return code;
1,076,906✔
530

531
      pWorker->queue = tQueryAutoQWorkerAllocQueue(pPool, pCfg->param, pCfg->fp);
1,076,906✔
532
      if (!pWorker->queue) {
1,076,906✔
533
        return terrno;
×
534
      }
535
    } break;
1,076,906✔
536
    default:
×
537
      return TSDB_CODE_INVALID_PARA;
×
538
  }
539
  return 0;
7,089,570✔
540
}
541

542
void tSingleWorkerCleanup(SSingleWorker *pWorker) {
7,089,570✔
543
  if (pWorker->queue == NULL) return;
7,089,570✔
544
  if (!pWorker->stopNoWaitQueue) {
7,089,570✔
545
    while (!taosQueueEmpty(pWorker->queue)) {
7,263,565✔
546
      taosMsleep(10);
252,666✔
547
    }
548
  }
549

550
  switch (pWorker->poolType) {
7,089,570✔
551
    case QWORKER_POOL:
6,012,664✔
552
      tQWorkerCleanup(pWorker->pool);
6,012,664✔
553
      tQWorkerFreeQueue(pWorker->pool, pWorker->queue);
6,012,664✔
554
      taosMemoryFree(pWorker->pool);
6,012,664✔
555
      break;
6,012,664✔
556
    case QUERY_AUTO_QWORKER_POOL:
1,076,906✔
557
      tQueryAutoQWorkerCleanup(pWorker->pool);
1,076,906✔
558
      tQueryAutoQWorkerFreeQueue(pWorker->pool, pWorker->queue);
1,076,906✔
559
      taosMemoryFree(pWorker->pool);
1,076,906✔
560
      break;
1,076,906✔
561
    default:
×
562
      break;
×
563
  }
564
}
565

566
int32_t tMultiWorkerInit(SMultiWorker *pWorker, const SMultiWorkerCfg *pCfg) {
18,562,474✔
567
  SWWorkerPool *pPool = &pWorker->pool;
18,562,474✔
568
  pPool->name = pCfg->name;
18,562,474✔
569
  pPool->max = pCfg->max;
18,562,908✔
570
  pPool->threadCategory = pCfg->threadCategory;
18,561,785✔
571

572
  int32_t code = tWWorkerInit(pPool);
18,562,092✔
573
  if (code) return code;
18,562,168✔
574

575
  pWorker->queue = tWWorkerAllocQueue(pPool, pCfg->param, pCfg->fp);
18,562,168✔
576
  if (pWorker->queue == NULL) {
18,562,908✔
577
    return terrno;
×
578
  }
579

580
  pWorker->name = pCfg->name;
18,562,908✔
581
  return 0;
18,562,908✔
582
}
583

584
void tMultiWorkerCleanup(SMultiWorker *pWorker) {
18,562,908✔
585
  if (pWorker->queue == NULL) return;
18,562,908✔
586

587
  while (!taosQueueEmpty(pWorker->queue)) {
28,022,364✔
588
    taosMsleep(10);
9,458,675✔
589
  }
590

591
  tWWorkerCleanup(&pWorker->pool);
18,562,908✔
592
  tWWorkerFreeQueue(&pWorker->pool, pWorker->queue);
18,562,908✔
593
}
594

595
static int32_t tQueryAutoQWorkerAddWorker(SQueryAutoQWorkerPool *pool);
596
static int32_t tQueryAutoQWorkerBeforeBlocking(void *p);
597
static int32_t tQueryAutoQWorkerRecoverFromBlocking(void *p);
598
static void    tQueryAutoQWorkerWaitingCheck(SQueryAutoQWorkerPool *pPool);
599
static bool    tQueryAutoQWorkerTryRecycleWorker(SQueryAutoQWorkerPool *pPool, SQueryAutoQWorker *pWorker);
600

601
#define GET_ACTIVE_N(int64_val)  (int32_t)((int64_val) >> 32)
602
#define GET_RUNNING_N(int64_val) (int32_t)(int64_val & 0xFFFFFFFF)
603

604
static int32_t atomicFetchSubActive(int64_t *ptr, int32_t val) {
×
605
  int64_t acutalSubVal = val;
×
606
  acutalSubVal <<= 32;
×
607
  int64_t newVal64 = atomic_fetch_sub_64(ptr, acutalSubVal);
×
608
  return GET_ACTIVE_N(newVal64);
×
609
}
610

611
static int32_t atomicFetchSubRunning(int64_t *ptr, int32_t val) { return GET_RUNNING_N(atomic_fetch_sub_64(ptr, val)); }
2,147,483,647✔
612

613
static int32_t atomicFetchAddActive(int64_t *ptr, int32_t val) {
197,764,710✔
614
  int64_t actualAddVal = val;
197,764,710✔
615
  actualAddVal <<= 32;
197,764,710✔
616
  int64_t newVal64 = atomic_fetch_add_64(ptr, actualAddVal);
197,764,710✔
617
  return GET_ACTIVE_N(newVal64);
197,764,710✔
618
}
619

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

622
static bool atomicCompareExchangeActive(int64_t *ptr, int32_t *expectedVal, int32_t newVal) {
435✔
623
  int64_t oldVal64 = *expectedVal, newVal64 = newVal;
435✔
624
  int32_t running = GET_RUNNING_N(*ptr);
435✔
625
  oldVal64 <<= 32;
435✔
626
  newVal64 <<= 32;
435✔
627
  oldVal64 |= running;
435✔
628
  newVal64 |= running;
435✔
629
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
435✔
630
  if (actualNewVal64 == oldVal64) {
435✔
631
    return true;
435✔
632
  } else {
633
    *expectedVal = GET_ACTIVE_N(actualNewVal64);
×
634
    return false;
×
635
  }
636
}
637

638
static int64_t atomicCompareExchangeRunning(int64_t *ptr, int32_t *expectedVal, int32_t newVal) {
×
639
  int64_t oldVal64 = *expectedVal, newVal64 = newVal;
×
640
  int64_t activeShifted = GET_ACTIVE_N(*ptr);
×
641
  activeShifted <<= 32;
×
642
  oldVal64 |= activeShifted;
×
643
  newVal64 |= activeShifted;
×
644
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
×
645
  if (actualNewVal64 == oldVal64) {
×
646
    return true;
×
647
  } else {
648
    *expectedVal = GET_RUNNING_N(actualNewVal64);
×
649
    return false;
×
650
  }
651
}
652

653
static int64_t atomicCompareExchangeActiveAndRunning(int64_t *ptr, int32_t *expectedActive, int32_t newActive,
2,147,483,647✔
654
                                                     int32_t *expectedRunning, int32_t newRunning) {
655
  int64_t oldVal64 = *expectedActive, newVal64 = newActive;
2,147,483,647✔
656
  oldVal64 <<= 32;
2,147,483,647✔
657
  oldVal64 |= *expectedRunning;
2,147,483,647✔
658
  newVal64 <<= 32;
2,147,483,647✔
659
  newVal64 |= newRunning;
2,147,483,647✔
660
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
2,147,483,647✔
661
  if (actualNewVal64 == oldVal64) {
2,147,483,647✔
662
    return true;
2,147,483,647✔
663
  } else {
664
    *expectedActive = GET_ACTIVE_N(actualNewVal64);
1,119,055✔
665
    *expectedRunning = GET_RUNNING_N(actualNewVal64);
1,119,055✔
666
    return false;
1,119,055✔
667
  }
668
}
669

670
static void *tQueryAutoQWorkerThreadFp(SQueryAutoQWorker *worker) {
198,684,356✔
671
  SQueryAutoQWorkerPool *pool = worker->pool;
198,684,356✔
672
  SQueueInfo             qinfo = {0};
198,686,712✔
673
  void                  *msg = NULL;
198,684,553✔
674
  int32_t                code = 0;
198,685,068✔
675

676
  int32_t ret = taosBlockSIGPIPE();
198,685,068✔
677
  if (ret < 0) {
198,666,751✔
678
    uError("worker:%s:%d failed to block SIGPIPE", pool->name, worker->id);
×
679
  }
680

681
  setThreadName(pool->name);
198,666,751✔
682
  if (pool->threadCategory >= 0) taosSetCpuAffinity((EThreadCategory)pool->threadCategory);
198,686,789✔
683
  worker->pid = taosGetSelfPthreadId();
198,626,606✔
684
  uDebug("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
198,663,768✔
685

686
  while (1) {
687
    if (taosReadQitemFromQset(pool->qset, (void **)&msg, &qinfo) == 0) {
2,147,483,647✔
688
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, pool->qset,
197,642,936✔
689
            worker->pid);
690
      break;
197,715,342✔
691
    }
692

693
    if (pool->stopNoWaitQueue && pool->exit) {
2,147,483,647✔
694
      uInfo("worker:%s:%d exit, thread:%08" PRId64, pool->name, worker->id, worker->pid);
×
695
      break;
×
696
    }
697

698
    if (qinfo.timestamp != 0) {
2,147,483,647✔
699
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
2,147,483,647✔
700
      if (cost > QUEUE_THRESHOLD) {
2,147,483,647✔
701
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pool->name, cost / QUEUE_THRESHOLD);
5,270,592✔
702
      }
703
    }
704

705
    tQueryAutoQWorkerWaitingCheck(pool);
2,147,483,647✔
706

707
    if (qinfo.fp != NULL) {
2,147,483,647✔
708
      qinfo.workerId = worker->id;
2,147,483,647✔
709
      qinfo.threadNum = pool->num;
2,147,483,647✔
710
      qinfo.workerCb = pool->pCb;
2,147,483,647✔
711
      (*((FItem)qinfo.fp))(&qinfo, msg);
2,147,483,647✔
712
    }
713

714
    taosUpdateItemSize(qinfo.queue, 1);
2,147,483,647✔
715
    if (!tQueryAutoQWorkerTryRecycleWorker(pool, worker)) {
2,147,483,647✔
716
      uDebug("worker:%s:%d exited", pool->name, worker->id);
928,702✔
717
      break;
928,702✔
718
    }
719
  }
720

721
  DestoryThreadLocalRegComp();
198,644,044✔
722
  closeThreadNotificationConn();
198,607,381✔
723

724
  return NULL;
198,626,363✔
725
}
726

727
static bool tQueryAutoQWorkerTrySignalWaitingAfterBlock(void *p) {
2,147,483,647✔
728
  SQueryAutoQWorkerPool *pPool = p;
2,147,483,647✔
729
  bool                   ret = false;
2,147,483,647✔
730
  int32_t                waiting = pPool->waitingAfterBlockN;
2,147,483,647✔
731
  while (waiting > 0) {
2,147,483,647✔
UNCOV
732
    int32_t waitingNew = atomic_val_compare_exchange_32(&pPool->waitingAfterBlockN, waiting, waiting - 1);
×
UNCOV
733
    if (waitingNew == waiting) {
×
UNCOV
734
      (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
×
UNCOV
735
      (void)taosThreadCondSignal(&pPool->waitingAfterBlockCond);
×
UNCOV
736
      (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
×
737
      ret = true;
5,575✔
738
      break;
5,575✔
739
    }
740
    waiting = waitingNew;
×
741
  }
742
  return ret;
2,147,483,647✔
743
}
744

745
static bool tQueryAutoQWorkerTrySignalWaitingBeforeProcess(void *p) {
2,147,483,647✔
746
  SQueryAutoQWorkerPool *pPool = p;
2,147,483,647✔
747
  bool                   ret = false;
2,147,483,647✔
748
  int32_t                waiting = pPool->waitingBeforeProcessMsgN;
2,147,483,647✔
749
  while (waiting > 0) {
2,147,483,647✔
750
    int32_t waitingNew = atomic_val_compare_exchange_32(&pPool->waitingBeforeProcessMsgN, waiting, waiting - 1);
435✔
751
    if (waitingNew == waiting) {
435✔
752
      (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
435✔
753
      (void)taosThreadCondSignal(&pPool->waitingBeforeProcessMsgCond);
435✔
754
      (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
435✔
755
      ret = true;
3,953✔
756
      break;
3,953✔
757
    }
758
    waiting = waitingNew;
×
759
  }
760
  return ret;
2,147,483,647✔
761
}
762

763
static bool tQueryAutoQWorkerTryDecActive(void *p, int32_t minActive) {
2,147,483,647✔
764
  SQueryAutoQWorkerPool *pPool = p;
2,147,483,647✔
765
  bool                   ret = false;
2,147,483,647✔
766
  int64_t                val64 = pPool->activeRunningN;
2,147,483,647✔
767
  int32_t                active = GET_ACTIVE_N(val64), running = GET_RUNNING_N(val64);
2,147,483,647✔
768
  while (active > minActive) {
2,147,483,647✔
769
    if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active - 1, &running, running - 1))
296,601,055✔
770
      return true;
296,572,294✔
771
  }
772
  return false;
2,147,483,647✔
773
}
774

775
static void tQueryAutoQWorkerWaitingCheck(SQueryAutoQWorkerPool *pPool) {
2,147,483,647✔
776
  while (1) {
14,380✔
777
    int64_t val64 = pPool->activeRunningN;
2,147,483,647✔
778
    int32_t running = GET_RUNNING_N(val64), active = GET_ACTIVE_N(val64);
2,147,483,647✔
779
    while (running < pPool->num) {
2,147,483,647✔
780
      if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active, &running, running + 1)) {
2,147,483,647✔
781
        return;
2,147,483,647✔
782
      }
783
    }
784
    if (atomicCompareExchangeActive(&pPool->activeRunningN, &active, active - 1)) {
4✔
785
      break;
435✔
786
    }
787
  }
788
  // to wait for process
789
  (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
435✔
790
  (void)atomic_fetch_add_32(&pPool->waitingBeforeProcessMsgN, 1);
435✔
791
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingBeforeProcessMsgCond, &pPool->waitingBeforeProcessMsgLock);
435✔
792
  // recovered from waiting
793
  (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
435✔
794
  return;
435✔
795
}
796

797
bool tQueryAutoQWorkerTryRecycleWorker(SQueryAutoQWorkerPool *pPool, SQueryAutoQWorker *pWorker) {
2,147,483,647✔
798
  if (tQueryAutoQWorkerTrySignalWaitingAfterBlock(pPool) || tQueryAutoQWorkerTrySignalWaitingBeforeProcess(pPool) ||
2,147,483,647✔
799
      tQueryAutoQWorkerTryDecActive(pPool, pPool->num)) {
2,147,483,647✔
800
    (void)taosThreadMutexLock(&pPool->poolLock);
130,745,848✔
801
    if (pPool->exit) {
130,703,666✔
802
      (void)taosThreadMutexUnlock(&pPool->poolLock);
2,438✔
803
      return false;
2,438✔
804
    }
805

806
    SListNode *pNode = listNode(pWorker);
130,701,228✔
807
    SListNode *tNode = tdListPopNode(pPool->workers, pNode);
130,701,228✔
808
    // reclaim some workers
809
    if (pWorker->id >= pPool->maxInUse) {
130,701,228✔
810
      while (listNEles(pPool->exitedWorkers) > pPool->maxInUse - pPool->num) {
×
811
        SListNode         *head = tdListPopHead(pPool->exitedWorkers);
×
812
        SQueryAutoQWorker *pWorker = (SQueryAutoQWorker *)head->data;
×
813
        if (pWorker && taosCheckPthreadValid(pWorker->thread)) {
×
814
          (void)taosThreadJoin(pWorker->thread, NULL);
×
815
          taosThreadClear(&pWorker->thread);
×
816
        }
817
        taosMemoryFree(head);
×
818
      }
819
      tdListAppendNode(pPool->exitedWorkers, pNode);
×
820
      (void)taosThreadMutexUnlock(&pPool->poolLock);
×
821
      return false;
×
822
    }
823

824
    // put back to backup pool
825
    tdListAppendNode(pPool->backupWorkers, pNode);
130,701,228✔
826
    (void)taosThreadMutexUnlock(&pPool->poolLock);
130,701,228✔
827

828
    // start to wait at backup cond
829
    (void)taosThreadMutexLock(&pPool->backupLock);
130,701,228✔
830
    (void)atomic_fetch_add_32(&pPool->backupNum, 1);
130,701,228✔
831
    if (!pPool->exit) (void)taosThreadCondWait(&pPool->backupCond, &pPool->backupLock);
130,701,228✔
832
    (void)taosThreadMutexUnlock(&pPool->backupLock);
130,701,228✔
833

834
    // recovered from backup
835
    (void)taosThreadMutexLock(&pPool->poolLock);
130,700,975✔
836
    if (pPool->exit) {
130,701,228✔
837
      (void)taosThreadMutexUnlock(&pPool->poolLock);
926,264✔
838
      return false;
926,264✔
839
    }
840
    SListNode *tNode1 = tdListPopNode(pPool->backupWorkers, pNode);
129,774,964✔
841
    tdListAppendNode(pPool->workers, pNode);
129,774,964✔
842
    (void)taosThreadMutexUnlock(&pPool->poolLock);
129,774,964✔
843

844
    return true;
129,774,964✔
845
  } else {
846
    (void)atomicFetchSubRunning(&pPool->activeRunningN, 1);
2,147,483,647✔
847
    return true;
2,147,483,647✔
848
  }
849
}
850

851
int32_t tQueryAutoQWorkerInit(SQueryAutoQWorkerPool *pool) {
3,989,309✔
852
  int32_t code;
853

854
  pool->exit = false;
3,989,309✔
855

856
  (void)taosThreadMutexInit(&pool->poolLock, NULL);
3,989,309✔
857
  (void)taosThreadMutexInit(&pool->backupLock, NULL);
3,989,309✔
858
  (void)taosThreadMutexInit(&pool->waitingAfterBlockLock, NULL);
3,989,309✔
859
  (void)taosThreadMutexInit(&pool->waitingBeforeProcessMsgLock, NULL);
3,989,309✔
860

861
  (void)taosThreadCondInit(&pool->waitingBeforeProcessMsgCond, NULL);
3,989,309✔
862
  (void)taosThreadCondInit(&pool->waitingAfterBlockCond, NULL);
3,989,309✔
863
  (void)taosThreadCondInit(&pool->backupCond, NULL);
3,989,309✔
864

865
  code = taosOpenQset(&pool->qset);
3,989,309✔
866
  if (code) return terrno = code;
3,989,309✔
867
  pool->workers = tdListNew(sizeof(SQueryAutoQWorker));
3,989,309✔
868
  if (!pool->workers) return terrno;
3,989,309✔
869
  pool->backupWorkers = tdListNew(sizeof(SQueryAutoQWorker));
3,989,309✔
870
  if (!pool->backupWorkers) return terrno;
3,989,309✔
871
  pool->exitedWorkers = tdListNew(sizeof(SQueryAutoQWorker));
3,989,309✔
872
  if (!pool->exitedWorkers) return terrno;
3,989,309✔
873
  pool->maxInUse = pool->max * 2 + 2;
3,989,309✔
874

875
  if (!pool->pCb) {
3,989,309✔
876
    pool->pCb = taosMemoryCalloc(1, sizeof(SQueryAutoQWorkerPoolCB));
3,989,309✔
877
    if (!pool->pCb) return terrno;
3,989,309✔
878
    pool->pCb->pPool = pool;
3,989,309✔
879
    pool->pCb->beforeBlocking = tQueryAutoQWorkerBeforeBlocking;
3,989,309✔
880
    pool->pCb->afterRecoverFromBlocking = tQueryAutoQWorkerRecoverFromBlocking;
3,989,309✔
881
  }
882
  return TSDB_CODE_SUCCESS;
3,989,309✔
883
}
884

885
void tQueryAutoQWorkerCleanup(SQueryAutoQWorkerPool *pPool) {
3,989,353✔
886
  (void)taosThreadMutexLock(&pPool->poolLock);
3,989,353✔
887
  pPool->exit = true;
3,989,353✔
888

889
  int32_t size = 0;
3,989,353✔
890
  if (pPool->workers) {
3,989,353✔
891
    size += listNEles(pPool->workers);
3,989,309✔
892
  }
893
  if (pPool->backupWorkers) {
3,989,353✔
894
    size += listNEles(pPool->backupWorkers);
3,989,309✔
895
  }
896
  if (pPool->qset) {
3,989,353✔
897
    for (int32_t i = 0; i < size; ++i) {
202,682,039✔
898
      taosQsetThreadResume(pPool->qset);
198,692,730✔
899
    }
900
  }
901
  (void)taosThreadMutexUnlock(&pPool->poolLock);
3,989,353✔
902

903
  (void)taosThreadMutexLock(&pPool->backupLock);
3,989,353✔
904
  (void)taosThreadCondBroadcast(&pPool->backupCond);
3,989,353✔
905
  (void)taosThreadMutexUnlock(&pPool->backupLock);
3,989,353✔
906

907
  (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
3,989,353✔
908
  (void)taosThreadCondBroadcast(&pPool->waitingAfterBlockCond);
3,989,353✔
909
  (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
3,989,353✔
910

911
  (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
3,989,353✔
912
  (void)taosThreadCondBroadcast(&pPool->waitingBeforeProcessMsgCond);
3,989,353✔
913
  (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
3,989,353✔
914

915
  int32_t            idx = 0;
3,989,353✔
916
  SQueryAutoQWorker *worker = NULL;
3,989,353✔
917
  while (pPool->workers) {
201,755,819✔
918
    (void)taosThreadMutexLock(&pPool->poolLock);
201,755,775✔
919
    if (listNEles(pPool->workers) <= 0) {
201,755,775✔
920
      (void)taosThreadMutexUnlock(&pPool->poolLock);
3,989,309✔
921
      break;
3,989,309✔
922
    }
923
    SListNode *pNode = tdListPopHead(pPool->workers);
197,766,466✔
924
    uDebug("0free worker node:%p, prev:%p, next:%p", pNode, TD_DLIST_NODE_PREV(pNode), TD_DLIST_NODE_NEXT(pNode));
197,766,466✔
925
    worker = pNode ? (SQueryAutoQWorker *)pNode->data : NULL;
197,766,466✔
926
    (void)taosThreadMutexUnlock(&pPool->poolLock);
197,766,466✔
927
    if (worker && taosCheckPthreadValid(worker->thread)) {
197,766,466✔
928
      (void)taosThreadJoin(worker->thread, NULL);
197,766,466✔
929
      taosThreadClear(&worker->thread);
197,766,466✔
930
    }
931
    uDebug("free worker node:%p, prev:%p, next:%p", pNode, TD_DLIST_NODE_PREV(pNode), TD_DLIST_NODE_NEXT(pNode));
197,766,466✔
932

933
    taosMemoryFree(pNode);
197,766,466✔
934
  }
935

936
  while (pPool->backupWorkers) {
4,915,617✔
937
    (void)taosThreadMutexLock(&pPool->poolLock);
4,915,573✔
938
    if (listNEles(pPool->backupWorkers) <= 0) {
4,915,573✔
939
      (void)taosThreadMutexUnlock(&pPool->poolLock);
3,989,309✔
940
      break;
3,989,309✔
941
    }
942
    uDebug("backupworker head:%p, prev:%p, next:%p", TD_DLIST_HEAD(pPool->backupWorkers), 
926,264✔
943
        TD_DLIST_HEAD(pPool->backupWorkers) ? TD_DLIST_NODE_PREV(TD_DLIST_HEAD(pPool->backupWorkers)) : NULL, 
944
        TD_DLIST_HEAD(pPool->backupWorkers) ? TD_DLIST_NODE_NEXT(TD_DLIST_HEAD(pPool->backupWorkers)) : NULL);
945
    SListNode *pNode = tdListPopHead(pPool->backupWorkers);
926,264✔
946
    worker = pNode ? (SQueryAutoQWorker *)pNode->data : NULL;
926,264✔
947
    (void)taosThreadMutexUnlock(&pPool->poolLock);
926,264✔
948

949
    if (worker && taosCheckPthreadValid(worker->thread)) {
926,264✔
950
      (void)taosThreadJoin(worker->thread, NULL);
926,264✔
951
      taosThreadClear(&worker->thread);
926,264✔
952
    }
953
    taosMemoryFree(pNode);
926,264✔
954
  }
955

956
  while (pPool->exitedWorkers) {
3,989,353✔
957
    (void)taosThreadMutexLock(&pPool->poolLock);
3,989,309✔
958
    if (listNEles(pPool->exitedWorkers) == 0) {
3,989,309✔
959
      (void)taosThreadMutexUnlock(&pPool->poolLock);
3,989,309✔
960
      break;
3,989,309✔
961
    }
962

963
    SListNode *pNode = tdListPopHead(pPool->exitedWorkers);
×
964
    worker = pNode ? (SQueryAutoQWorker *)pNode->data : NULL;
×
965
    (void)taosThreadMutexUnlock(&pPool->poolLock);
×
966

967
    if (worker && taosCheckPthreadValid(worker->thread)) {
×
968
      (void)taosThreadJoin(worker->thread, NULL);
×
969
      taosThreadClear(&worker->thread);
×
970
    }
971
    taosMemoryFree(pNode);
×
972
  }
973

974
  (void)taosThreadMutexLock(&pPool->poolLock);
3,989,353✔
975
  pPool->workers = tdListFree(pPool->workers);
3,989,353✔
976
  pPool->backupWorkers = tdListFree(pPool->backupWorkers);
3,989,353✔
977
  pPool->exitedWorkers = tdListFree(pPool->exitedWorkers);
3,989,353✔
978
  taosMemoryFree(pPool->pCb);
3,989,353✔
979
  (void)taosThreadMutexUnlock(&pPool->poolLock);
3,989,353✔
980

981
  (void)taosThreadMutexDestroy(&pPool->poolLock);
3,989,353✔
982
  (void)taosThreadMutexDestroy(&pPool->backupLock);
3,989,353✔
983
  (void)taosThreadMutexDestroy(&pPool->waitingAfterBlockLock);
3,989,353✔
984
  (void)taosThreadMutexDestroy(&pPool->waitingBeforeProcessMsgLock);
3,989,353✔
985

986
  (void)taosThreadCondDestroy(&pPool->backupCond);
3,989,353✔
987
  (void)taosThreadCondDestroy(&pPool->waitingAfterBlockCond);
3,989,353✔
988
  (void)taosThreadCondDestroy(&pPool->waitingBeforeProcessMsgCond);
3,989,353✔
989
  taosCloseQset(pPool->qset);
3,989,353✔
990
}
3,989,353✔
991

992
STaosQueue *tQueryAutoQWorkerAllocQueue(SQueryAutoQWorkerPool *pool, void *ahandle, FItem fp) {
11,913,887✔
993
  STaosQueue *queue;
11,889,110✔
994
  int32_t     code = taosOpenQueue(&queue);
11,913,887✔
995
  if (code) {
11,912,603✔
996
    terrno = code;
×
997
    return NULL;
×
998
  }
999

1000
  (void)taosThreadMutexLock(&pool->poolLock);
11,912,603✔
1001
  taosSetQueueFp(queue, fp, NULL);
11,913,267✔
1002
  code = taosAddIntoQset(pool->qset, queue, ahandle);
11,914,473✔
1003
  if (code) {
11,914,473✔
1004
    taosCloseQueue(queue);
×
1005
    queue = NULL;
×
1006
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
1007
    return NULL;
×
1008
  }
1009
  SQueryAutoQWorker  worker = {0};
11,914,473✔
1010
  SQueryAutoQWorker *pWorker = NULL;
11,914,473✔
1011

1012
  // spawn a thread to process queue
1013
  if (pool->num < pool->max) {
11,914,473✔
1014
    do {
1015
      worker.id = listNEles(pool->workers);
197,764,710✔
1016
      worker.backupIdx = -1;
197,764,710✔
1017
      worker.pool = pool;
197,764,710✔
1018
      SListNode *pNode = tdListAdd(pool->workers, &worker);
197,764,710✔
1019
      if (!pNode) {
197,764,710✔
1020
        taosCloseQueue(queue);
×
1021
        queue = NULL;
×
1022
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1023
        break;
×
1024
      }
1025
      pWorker = (SQueryAutoQWorker *)pNode->data;
197,764,710✔
1026

1027
      TdThreadAttr thAttr;
196,419,110✔
1028
      (void)taosThreadAttrInit(&thAttr);
197,764,710✔
1029
      (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
197,764,710✔
1030

1031
      if (taosThreadCreate(&pWorker->thread, &thAttr, (ThreadFp)tQueryAutoQWorkerThreadFp, pWorker) != 0) {
197,764,710✔
1032
        taosCloseQueue(queue);
×
1033
        queue = NULL;
×
1034
        break;
×
1035
      }
1036

1037
      (void)taosThreadAttrDestroy(&thAttr);
197,764,710✔
1038
      pool->num++;
197,764,710✔
1039
      (void)atomicFetchAddActive(&pool->activeRunningN, 1);
197,764,710✔
1040
      uInfo("worker:%s:%d is launched, total:%d", pool->name, pWorker->id, pool->num);
197,764,710✔
1041
    } while (pool->num < pool->min);
197,764,710✔
1042
  }
1043

1044
  (void)taosThreadMutexUnlock(&pool->poolLock);
11,914,473✔
1045
  uInfo("worker:%s, queue:%p is allocated, ahandle:%p", pool->name, queue, ahandle);
11,914,473✔
1046

1047
  return queue;
11,913,674✔
1048
}
1049

1050
void tQueryAutoQWorkerFreeQueue(SQueryAutoQWorkerPool *pPool, STaosQueue *pQ) { taosCloseQueue(pQ); }
10,358,283✔
1051

1052
static int32_t tQueryAutoQWorkerAddWorker(SQueryAutoQWorkerPool *pool) {
130,702,106✔
1053
  // try backup pool
1054
  int32_t backup = pool->backupNum;
130,702,106✔
1055
  while (backup > 0) {
130,718,924✔
1056
    int32_t backupNew = atomic_val_compare_exchange_32(&pool->backupNum, backup, backup - 1);
129,790,904✔
1057
    if (backupNew == backup) {
129,792,464✔
1058
      (void)taosThreadCondSignal(&pool->backupCond);
129,775,646✔
1059
      return TSDB_CODE_SUCCESS;
129,775,646✔
1060
    }
1061
    backup = backupNew;
16,818✔
1062
  }
1063
  // backup pool is empty, create new
1064
  SQueryAutoQWorker *pWorker = NULL;
928,020✔
1065
  SQueryAutoQWorker  worker = {0};
928,020✔
1066
  worker.pool = pool;
928,020✔
1067
  worker.backupIdx = -1;
928,020✔
1068
  (void)taosThreadMutexLock(&pool->poolLock);
928,020✔
1069
  if (pool->exit) {
928,020✔
1070
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
1071
    return TSDB_CODE_SUCCESS;
×
1072
  }
1073
  worker.id = listNEles(pool->workers);
928,020✔
1074
  SListNode *pNode = tdListAdd(pool->workers, &worker);
928,020✔
1075
  if (!pNode) {
928,020✔
1076
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
1077
    return terrno;
×
1078
  }
1079
  (void)taosThreadMutexUnlock(&pool->poolLock);
928,020✔
1080
  pWorker = (SQueryAutoQWorker *)pNode->data;
928,020✔
1081

1082
  TdThreadAttr thAttr;
926,634✔
1083
  (void)taosThreadAttrInit(&thAttr);
928,020✔
1084
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
928,020✔
1085

1086
  if (taosThreadCreate(&pWorker->thread, &thAttr, (ThreadFp)tQueryAutoQWorkerThreadFp, pWorker) != 0) {
928,020✔
1087
    uError("create queryAutoWorker thread failed, error:%s", tstrerror(terrno));
×
1088
    return terrno;
×
1089
  }
1090
  (void)taosThreadAttrDestroy(&thAttr);
928,020✔
1091

1092
  return TSDB_CODE_SUCCESS;
928,020✔
1093
}
1094

1095
static int32_t tQueryAutoQWorkerBeforeBlocking(void *p) {
296,571,169✔
1096
  SQueryAutoQWorkerPool *pPool = p;
296,571,169✔
1097
  if (tQueryAutoQWorkerTrySignalWaitingAfterBlock(p) || tQueryAutoQWorkerTrySignalWaitingBeforeProcess(p) ||
593,137,132✔
1098
      tQueryAutoQWorkerTryDecActive(p, pPool->num)) {
296,569,765✔
1099
  } else {
1100
    int32_t code = tQueryAutoQWorkerAddWorker(pPool);
130,696,723✔
1101
    if (code != TSDB_CODE_SUCCESS) {
130,703,087✔
1102
      return code;
×
1103
    }
1104
    (void)atomicFetchSubRunning(&pPool->activeRunningN, 1);
130,703,087✔
1105
  }
1106

1107
  return TSDB_CODE_SUCCESS;
296,572,729✔
1108
}
1109

1110
static int32_t tQueryAutoQWorkerRecoverFromBlocking(void *p) {
296,571,604✔
1111
  SQueryAutoQWorkerPool *pPool = p;
296,571,604✔
1112
  int64_t                val64 = pPool->activeRunningN;
296,571,604✔
1113
  int32_t                running = GET_RUNNING_N(val64), active = GET_ACTIVE_N(val64);
296,572,155✔
1114
  while (running < pPool->num) {
296,582,201✔
1115
    if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active + 1, &running, running + 1)) {
296,582,201✔
1116
      return TSDB_CODE_SUCCESS;
296,572,729✔
1117
    }
1118
  }
UNCOV
1119
  (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
×
UNCOV
1120
  (void)atomic_fetch_add_32(&pPool->waitingAfterBlockN, 1);
×
UNCOV
1121
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingAfterBlockCond, &pPool->waitingAfterBlockLock);
×
UNCOV
1122
  (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
×
UNCOV
1123
  if (pPool->exit) return TSDB_CODE_QRY_QWORKER_QUIT;
×
UNCOV
1124
  return TSDB_CODE_SUCCESS;
×
1125
}
1126

1127
int32_t tDispatchWorkerInit(SDispatchWorkerPool *pPool) {
1,256,481✔
1128
  int32_t code = 0;
1,256,481✔
1129
  pPool->num = 0;
1,256,481✔
1130
  pPool->pWorkers = taosMemCalloc(pPool->max, sizeof(SDispatchWorker));
1,256,481✔
1131
  if (!pPool->pWorkers) return terrno;
1,256,481✔
1132
  (void)taosThreadMutexInit(&pPool->poolLock, NULL);
1,256,481✔
1133
  return code;
1,256,481✔
1134
}
1135

1136
static void *tDispatchWorkerThreadFp(SDispatchWorker *pWorker) {
9,018,888✔
1137
  SDispatchWorkerPool *pPool = pWorker->pool;
9,018,888✔
1138
  SQueueInfo qinfo = {0};
9,023,334✔
1139
  int32_t code = 0;
9,023,013✔
1140
  void *msg = NULL;
9,023,013✔
1141

1142
  int32_t ret = taosBlockSIGPIPE();
9,023,013✔
1143
  if (ret < 0) {
9,020,940✔
1144
    uError("worker:%s:%d failed to block SIGPIPE", pPool->name, pWorker->id);
×
1145
  }
1146

1147
  setThreadName(pPool->name);
9,020,940✔
1148
  if (pPool->threadCategory >= 0) taosSetCpuAffinity((EThreadCategory)pPool->threadCategory);
9,022,753✔
1149
  pWorker->pid = taosGetSelfPthreadId();
9,022,260✔
1150
  uInfo("worker:%s:%d is running, thread:%d", pPool->name, pWorker->id, pWorker->pid);
9,022,175✔
1151

1152
  while (1) {
1153
    if (taosReadQitemFromQset(pWorker->qset, (void **)&msg, &qinfo) == 0) {
133,179,098✔
1154
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%d", pPool->name, pWorker->id,
9,006,182✔
1155
            pWorker->qset, pWorker->pid);
1156
      break;
9,023,039✔
1157
    }
1158

1159
    if (qinfo.timestamp != 0) {
124,156,077✔
1160
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
124,156,993✔
1161
      if (cost > QUEUE_THRESHOLD) {
124,156,993✔
1162
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pPool->name, cost / QUEUE_THRESHOLD);
545,920✔
1163
      }
1164
    }
1165

1166
    if (qinfo.fp != NULL) {
124,156,500✔
1167
      qinfo.workerId = pWorker->id;
124,153,756✔
1168
      qinfo.threadNum = pPool->num;
124,155,684✔
1169
      (*((FItem)qinfo.fp))(&qinfo, msg);
124,159,464✔
1170
    }
1171
  }
1172
  DestoryThreadLocalRegComp();
9,023,039✔
1173
  closeThreadNotificationConn();
9,023,240✔
1174
  return NULL;
9,022,776✔
1175
}
1176

1177
int32_t tDispatchWorkerAllocQueue(SDispatchWorkerPool *pPool, void *ahandle, FItem fp, DispatchFp dispatchFp) {
1,256,481✔
1178
  int32_t code = 0;
1,256,481✔
1179
  SDispatchWorker* pWorker = NULL;
1,256,481✔
1180
  (void)taosThreadMutexLock(&pPool->poolLock);
1,256,481✔
1181
  pPool->dispatchFp = dispatchFp;
1,256,481✔
1182
  for (int32_t i = pPool->num; i < pPool->max; ++i) {
10,279,815✔
1183
    pWorker = pPool->pWorkers + i;
9,023,334✔
1184
    pWorker->id = pPool->num;
9,023,334✔
1185
    pWorker->pool = pPool;
9,023,334✔
1186
    pPool->num++;
9,023,334✔
1187
    code = taosOpenQset(&pWorker->qset);
9,023,334✔
1188
    if (code != 0) break;
9,023,334✔
1189
    code = taosOpenQueue(&pWorker->queue);
9,023,334✔
1190
    if (code != 0) break;
9,023,334✔
1191
    taosSetQueueFp(pWorker->queue, fp, ahandle);
9,023,334✔
1192
    code = taosAddIntoQset(pWorker->qset, pWorker->queue, ahandle);
9,023,334✔
1193
    if (code != 0) break;
9,023,334✔
1194

1195
    TdThreadAttr thAttr;
9,012,470✔
1196
    (void)taosThreadAttrInit(&thAttr);
9,023,334✔
1197
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
9,023,334✔
1198

1199
    if (taosThreadCreate(&pWorker->thread, &thAttr, (ThreadFp)tDispatchWorkerThreadFp, pWorker) != 0) {
9,023,334✔
1200
      code = terrno;
×
1201
      (void)taosThreadAttrDestroy(&thAttr);
×
1202
      break;
×
1203
    }
1204
    (void)taosThreadAttrDestroy(&thAttr);
9,023,334✔
1205
    uInfo("worker:%s:%d is launched, threadId:%" PRId64 ", total:%d", pPool->name, pWorker->id, taosGetPthreadId(pWorker->thread), pPool->num);
9,023,334✔
1206
  }
1207

1208
  (void)taosThreadMutexUnlock(&pPool->poolLock);
1,256,481✔
1209
  if (code == 0) uInfo("worker:%s, queue:%p is allocated, ahandle:%p", pPool->name, pWorker->queue, ahandle);
1,256,481✔
1210
  return code;
1,256,481✔
1211
}
1212

1213
static void tDispatchWorkerFreeQueue(SDispatchWorkerPool *pPool) {
1,256,481✔
1214
  (void)taosThreadMutexLock(&pPool->poolLock);
1,256,481✔
1215
  if (!pPool->pWorkers) return;
1,256,481✔
1216
  for (int32_t i = 0; i < pPool->num; ++i) {
10,279,815✔
1217
    SDispatchWorker *pWorker = pPool->pWorkers + i;
9,023,334✔
1218
    if (pWorker->queue) {
9,023,334✔
1219
      taosCloseQueue(pWorker->queue);
9,023,334✔
1220
      pWorker->queue = NULL;
9,023,334✔
1221
    }
1222
    if (pWorker->qset) {
9,023,334✔
1223
      taosCloseQset(pWorker->qset);
9,023,334✔
1224
      pWorker->qset = NULL;
9,023,334✔
1225
    }
1226
  }
1227
  (void)taosThreadMutexUnlock(&pPool->poolLock);
1,256,481✔
1228
}
1229

1230
void tDispatchWorkerCleanup(SDispatchWorkerPool *pPool) {
1,256,481✔
1231
  (void)taosThreadMutexLock(&pPool->poolLock);
1,256,481✔
1232
  pPool->exit = true;
1,256,481✔
1233
  if (pPool->pWorkers) {
1,256,481✔
1234
    for (int32_t i = 0; i < pPool->num; ++i) {
10,279,815✔
1235
      SDispatchWorker *pWorker = pPool->pWorkers + i;
9,023,334✔
1236
      if (pWorker->qset) {
9,023,334✔
1237
        taosQsetThreadResume(pWorker->qset);
9,023,334✔
1238
      }
1239
    }
1240
  }
1241
  (void)taosThreadMutexUnlock(&pPool->poolLock);
1,256,481✔
1242

1243
  if (pPool->pWorkers) {
1,256,481✔
1244
    for (int32_t i = 0; i < pPool->num; ++i) {
10,279,815✔
1245
      SDispatchWorker *pWorker = pPool->pWorkers + i;
9,023,334✔
1246
      if (taosCheckPthreadValid(pWorker->thread)) {
9,023,334✔
1247
        (void)taosThreadJoin(pWorker->thread, NULL);
9,023,334✔
1248
        taosThreadClear(&pWorker->thread);
9,023,334✔
1249
      }
1250
    }
1251
  }
1252
  tDispatchWorkerFreeQueue(pPool);
1,256,481✔
1253
  taosMemoryFreeClear(pPool->pWorkers);
1,256,481✔
1254
  (void)taosThreadMutexDestroy(&pPool->poolLock);
1,256,481✔
1255
}
1,256,481✔
1256

1257
int32_t tAddTaskIntoDispatchWorkerPool(SDispatchWorkerPool *pPool, void *pMsg) {
124,157,299✔
1258
  int32_t code = 0;
124,157,299✔
1259
  int32_t idx = 0;
124,157,299✔
1260
  SDispatchWorker *pWorker = NULL;
124,159,212✔
1261
  (void)taosThreadMutexLock(&pPool->poolLock);
124,159,212✔
1262
  code = pPool->dispatchFp(pPool, pMsg, &idx);
124,162,561✔
1263
  if (code == 0) {
124,162,561✔
1264
    pWorker = pPool->pWorkers + idx;
124,162,561✔
1265
    if (pWorker->queue) {
124,162,561✔
1266
      code = taosWriteQitem(pWorker->queue, pMsg);
124,162,561✔
1267
    } else {
1268
      code = TSDB_CODE_INTERNAL_ERROR;
×
1269
    }
1270
  }
1271
  (void)taosThreadMutexUnlock(&pPool->poolLock);
124,162,561✔
1272
  if (code != 0) {
124,162,561✔
1273
    uError("worker:%s, failed to add task into dispatch worker pool, code:%d", pPool->name, code);
×
1274
  } else {
1275
    uDebug("msg %p dispatch to the %dth worker, threadId:%" PRId64, pMsg, idx, taosGetPthreadId(pWorker->thread));
124,162,561✔
1276
  }
1277
  return code;
124,161,762✔
1278
}
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