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

taosdata / TDengine / #5052

13 May 2026 12:00PM UTC coverage: 73.338% (-0.02%) from 73.358%
#5052

push

travis-ci

web-flow
feat: taosdump support stream backup/restore (#35326)

139 of 170 new or added lines in 3 files covered. (81.76%)

761 existing lines in 163 files now uncovered.

281469 of 383795 relevant lines covered (73.34%)

134502812.98 hits per line

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

72.16
/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,445,584✔
30
  int32_t code = taosOpenQset(&pool->qset);
6,445,584✔
31
  if (code) return code;
6,445,584✔
32

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

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

41
  for (int32_t i = 0; i < pool->max; ++i) {
42,788,752✔
42
    SQueueWorker *worker = pool->workers + i;
36,343,168✔
43
    worker->id = i;
36,343,168✔
44
    worker->pool = pool;
36,343,168✔
45
  }
46

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

51
void tQWorkerCleanup(SQWorkerPool *pool) {
6,445,584✔
52
  for (int32_t i = 0; i < pool->max; ++i) {
42,788,752✔
53
    SQueueWorker *worker = pool->workers + i;
36,343,168✔
54
    if (taosCheckPthreadValid(worker->thread)) {
36,343,168✔
55
      taosQsetThreadResume(pool->qset);
36,343,168✔
56
    }
57
  }
58

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

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

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

76
static void *tQWorkerThreadFp(SQueueWorker *worker) {
36,341,908✔
77
  SQWorkerPool *pool = worker->pool;
36,341,908✔
78
  SQueueInfo    qinfo = {0};
36,342,801✔
79
  void         *msg = NULL;
36,341,690✔
80
  int32_t       code = 0;
36,342,211✔
81

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

87
  setThreadName(pool->name);
36,342,492✔
88
  if (pool->threadCategory >= 0) taosSetCpuAffinity((EThreadCategory)pool->threadCategory);
36,341,400✔
89
  worker->pid = taosGetSelfPthreadId();
36,334,166✔
90
  uInfo("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
36,339,737✔
91

92
  while (1) {
93
    if (taosReadQitemFromQset(pool->qset, (void **)&msg, &qinfo) == 0) {
558,257,467✔
94
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, pool->qset,
36,328,426✔
95
            worker->pid);
96
      break;
36,339,153✔
97
    }
98

99
    if (qinfo.timestamp != 0) {
521,915,712✔
100
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
521,916,816✔
101
      if (cost > QUEUE_THRESHOLD) {
521,916,816✔
102
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pool->name, cost / QUEUE_THRESHOLD);
1,172,488✔
103
      }
104
    }
105

106
    if (qinfo.fp != NULL) {
521,916,478✔
107
      qinfo.workerId = worker->id;
521,915,064✔
108
      qinfo.threadNum = pool->num;
521,915,064✔
109
      (*((FItem)qinfo.fp))(&qinfo, msg);
521,915,541✔
110
    }
111

112
    taosUpdateItemSize(qinfo.queue, 1);
521,906,282✔
113
  }
114

115
  DestoryThreadLocalRegComp();
36,339,153✔
116

117
  return NULL;
36,342,985✔
118
}
119

120
STaosQueue *tQWorkerAllocQueue(SQWorkerPool *pool, void *ahandle, FItem fp) {
6,445,584✔
121
  int32_t     code;
122
  STaosQueue *queue;
6,425,619✔
123

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

130
  (void)taosThreadMutexLock(&pool->mutex);
6,445,584✔
131
  taosSetQueueFp(queue, fp, NULL);
6,445,584✔
132
  code = taosAddIntoQset(pool->qset, queue, ahandle);
6,445,584✔
133
  if (code) {
6,445,584✔
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,445,584✔
142
    do {
143
      SQueueWorker *worker = pool->workers + pool->num;
36,343,168✔
144

145
      TdThreadAttr thAttr;
36,250,207✔
146
      (void)taosThreadAttrInit(&thAttr);
36,343,168✔
147
      (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
36,343,168✔
148

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

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

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

165
  return queue;
6,445,584✔
166
}
167

168
void tQWorkerFreeQueue(SQWorkerPool *pool, STaosQueue *queue) {
6,445,584✔
169
  uInfo("worker:%s, queue:%p is freed", pool->name, queue);
6,445,584✔
170
  taosCloseQueue(queue);
6,445,584✔
171
}
6,445,584✔
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);
×
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) {
20,920,409✔
339
  pool->nextId = 0;
20,920,409✔
340
  pool->workers = taosMemoryCalloc(pool->max, sizeof(SWWorker));
20,922,040✔
341
  if (pool->workers == NULL) {
20,923,134✔
342
    return terrno;
×
343
  }
344

345
  (void)taosThreadMutexInit(&pool->mutex, NULL);
20,922,146✔
346

347
  for (int32_t i = 0; i < pool->max; ++i) {
48,896,115✔
348
    SWWorker *worker = pool->workers + i;
27,976,352✔
349
    worker->id = i;
27,974,162✔
350
    worker->qall = NULL;
27,974,612✔
351
    worker->qset = NULL;
27,973,645✔
352
    worker->pool = pool;
27,973,019✔
353
  }
354

355
  uInfo("worker:%s is initialized, max:%d", pool->name, pool->max);
20,918,536✔
356
  return 0;
20,922,723✔
357
}
358

359
void tWWorkerCleanup(SWWorkerPool *pool) {
20,923,000✔
360
  for (int32_t i = 0; i < pool->max; ++i) {
48,898,771✔
361
    SWWorker *worker = pool->workers + i;
27,975,805✔
362
    if (taosCheckPthreadValid(worker->thread)) {
27,975,865✔
363
      if (worker->qset) {
23,487,438✔
364
        taosQsetThreadResume(worker->qset);
23,487,438✔
365
      }
366
    }
367
  }
368

369
  for (int32_t i = 0; i < pool->max; ++i) {
48,899,404✔
370
    SWWorker *worker = pool->workers + i;
27,976,263✔
371
    if (taosCheckPthreadValid(worker->thread)) {
27,975,892✔
372
      uInfo("worker:%s:%d is stopping", pool->name, worker->id);
23,487,238✔
373
      (void)taosThreadJoin(worker->thread, NULL);
23,489,027✔
374
      taosThreadClear(&worker->thread);
23,487,859✔
375
      taosFreeQall(worker->qall);
23,487,859✔
376
      taosCloseQset(worker->qset);
23,487,859✔
377
      uInfo("worker:%s:%d is stopped", pool->name, worker->id);
23,487,859✔
378
    }
379
  }
380

381
  taosMemoryFreeClear(pool->workers);
20,923,005✔
382
  (void)taosThreadMutexDestroy(&pool->mutex);
20,922,246✔
383

384
  uInfo("worker:%s is closed", pool->name);
20,922,289✔
385
}
20,923,134✔
386

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

394
  int32_t ret = taosBlockSIGPIPE();
23,487,859✔
395
  if (ret < 0) {
23,483,663✔
396
    uError("worker:%s:%d failed to block SIGPIPE", pool->name, worker->id);
×
397
  }
398

399
  setThreadName(pool->name);
23,483,663✔
400
  if (pool->threadCategory >= 0) taosSetCpuAffinity((EThreadCategory)pool->threadCategory);
23,487,859✔
401
  worker->pid = taosGetSelfPthreadId();
23,463,111✔
402
  uInfo("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
23,479,960✔
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,
23,481,129✔
408
            worker->pid);
409
      break;
23,487,859✔
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);
63,601✔
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;
23,487,859✔
428
}
429

430
STaosQueue *tWWorkerAllocQueue(SWWorkerPool *pool, void *ahandle, FItems fp) {
25,113,626✔
431
  (void)taosThreadMutexLock(&pool->mutex);
25,113,626✔
432
  SWWorker   *worker = pool->workers + pool->nextId;
25,113,626✔
433
  int32_t     code = -1;
25,113,626✔
434
  STaosQueue *queue;
25,088,292✔
435

436
  code = taosOpenQueue(&queue);
25,113,626✔
437
  if (code) goto _OVER;
25,113,528✔
438

439
  taosSetQueueFp(queue, NULL, fp);
25,113,528✔
440
  if (worker->qset == NULL) {
25,113,110✔
441
    code = taosOpenQset(&worker->qset);
23,487,343✔
442
    if (code) goto _OVER;
23,487,253✔
443

444
    code = taosAddIntoQset(worker->qset, queue, ahandle);
23,487,253✔
445
    if (code) goto _OVER;
23,487,061✔
446
    code = taosAllocateQall(&worker->qall);
23,487,061✔
447
    if (code) goto _OVER;
23,487,334✔
448

449
    TdThreadAttr thAttr;
23,464,886✔
450
    (void)taosThreadAttrInit(&thAttr);
23,487,859✔
451
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
23,487,769✔
452
    code = taosThreadCreate(&worker->thread, &thAttr, (ThreadFp)tWWorkerThreadFp, worker);
23,487,146✔
453
    if ((code)) goto _OVER;
23,487,859✔
454

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

458
    (void)taosThreadAttrDestroy(&thAttr);
23,487,799✔
459
    pool->num++;
23,487,798✔
460
    if (pool->num > pool->max) pool->num = pool->max;
23,487,859✔
461
  } else {
462
    code = taosAddIntoQset(worker->qset, queue, ahandle);
1,625,767✔
463
    if (code) goto _OVER;
1,625,767✔
464
    pool->nextId = (pool->nextId + 1) % pool->max;
1,625,767✔
465
  }
466

467
_OVER:
25,113,565✔
468
  (void)taosThreadMutexUnlock(&pool->mutex);
25,113,626✔
469

470
  if (code) {
25,113,626✔
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);
51,568,167✔
478

479
    taosQueueSetThreadId(queue, worker->pid);
25,113,400✔
480
    uInfo("worker:%s, queue:%p is allocated, ahandle:%p thread:%08" PRId64, pool->name, queue, ahandle, worker->pid);
25,112,344✔
481
    return queue;
25,113,626✔
482
  }
483
}
484

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

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

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

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

528
      code = tQueryAutoQWorkerInit(pPool);
1,161,029✔
529
      if (code) return code;
1,161,029✔
530

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

542
void tSingleWorkerCleanup(SSingleWorker *pWorker) {
7,606,613✔
543
  if (pWorker->queue == NULL) return;
7,606,613✔
544
  if (!pWorker->stopNoWaitQueue) {
7,606,613✔
545
    while (!taosQueueEmpty(pWorker->queue)) {
7,921,555✔
546
      taosMsleep(10);
404,478✔
547
    }
548
  }
549

550
  switch (pWorker->poolType) {
7,606,613✔
551
    case QWORKER_POOL:
6,445,584✔
552
      tQWorkerCleanup(pWorker->pool);
6,445,584✔
553
      tQWorkerFreeQueue(pWorker->pool, pWorker->queue);
6,445,584✔
554
      taosMemoryFree(pWorker->pool);
6,445,584✔
555
      break;
6,445,584✔
556
    case QUERY_AUTO_QWORKER_POOL:
1,161,029✔
557
      tQueryAutoQWorkerCleanup(pWorker->pool);
1,161,029✔
558
      tQueryAutoQWorkerFreeQueue(pWorker->pool, pWorker->queue);
1,161,029✔
559
      taosMemoryFree(pWorker->pool);
1,161,029✔
560
      break;
1,161,029✔
561
    default:
×
562
      break;
×
563
  }
564
}
565

566
int32_t tMultiWorkerInit(SMultiWorker *pWorker, const SMultiWorkerCfg *pCfg) {
19,662,443✔
567
  SWWorkerPool *pPool = &pWorker->pool;
19,662,443✔
568
  pPool->name = pCfg->name;
19,663,238✔
569
  pPool->max = pCfg->max;
19,664,196✔
570
  pPool->threadCategory = pCfg->threadCategory;
19,663,170✔
571

572
  int32_t code = tWWorkerInit(pPool);
19,664,801✔
573
  if (code) return code;
19,664,757✔
574

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

580
  pWorker->name = pCfg->name;
19,664,770✔
581
  return 0;
19,664,770✔
582
}
583

584
void tMultiWorkerCleanup(SMultiWorker *pWorker) {
19,665,168✔
585
  if (pWorker->queue == NULL) return;
19,665,168✔
586

587
  while (!taosQueueEmpty(pWorker->queue)) {
28,638,310✔
588
    taosMsleep(10);
8,969,546✔
589
  }
590

591
  tWWorkerCleanup(&pWorker->pool);
19,665,100✔
592
  tWWorkerFreeQueue(&pWorker->pool, pWorker->queue);
19,664,973✔
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) {
208,930,778✔
614
  int64_t actualAddVal = val;
208,930,778✔
615
  actualAddVal <<= 32;
208,930,778✔
616
  int64_t newVal64 = atomic_fetch_add_64(ptr, actualAddVal);
208,930,778✔
617
  return GET_ACTIVE_N(newVal64);
208,930,778✔
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) {
×
623
  int64_t oldVal64 = *expectedVal, newVal64 = newVal;
×
624
  int32_t running = GET_RUNNING_N(*ptr);
×
625
  oldVal64 <<= 32;
×
626
  newVal64 <<= 32;
×
627
  oldVal64 |= running;
×
628
  newVal64 |= running;
×
629
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
×
630
  if (actualNewVal64 == oldVal64) {
×
631
    return true;
×
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,218,476✔
665
    *expectedRunning = GET_RUNNING_N(actualNewVal64);
1,218,476✔
666
    return false;
1,218,476✔
667
  }
668
}
669

670
static void *tQueryAutoQWorkerThreadFp(SQueryAutoQWorker *worker) {
209,915,896✔
671
  SQueryAutoQWorkerPool *pool = worker->pool;
209,915,896✔
672
  SQueueInfo             qinfo = {0};
209,920,925✔
673
  void                  *msg = NULL;
209,919,720✔
674
  int32_t                code = 0;
209,920,388✔
675

676
  int32_t ret = taosBlockSIGPIPE();
209,920,388✔
677
  if (ret < 0) {
209,904,351✔
678
    uError("worker:%s:%d failed to block SIGPIPE", pool->name, worker->id);
×
679
  }
680

681
  setThreadName(pool->name);
209,904,351✔
682
  if (pool->threadCategory >= 0) taosSetCpuAffinity((EThreadCategory)pool->threadCategory);
209,914,547✔
683
  worker->pid = taosGetSelfPthreadId();
209,853,214✔
684
  uDebug("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
209,887,151✔
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,
208,737,619✔
689
            worker->pid);
690
      break;
208,813,729✔
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,172,336✔
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);
995,418✔
717
      break;
995,418✔
718
    }
719
  }
720

721
  DestoryThreadLocalRegComp();
209,809,147✔
722
  closeThreadNotificationConn();
209,798,931✔
723

724
  return NULL;
209,786,151✔
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✔
732
    int32_t waitingNew = atomic_val_compare_exchange_32(&pPool->waitingAfterBlockN, waiting, waiting - 1);
×
733
    if (waitingNew == waiting) {
×
734
      (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
×
735
      (void)taosThreadCondSignal(&pPool->waitingAfterBlockCond);
×
736
      (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
×
737
      ret = true;
615✔
738
      break;
615✔
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);
×
751
    if (waitingNew == waiting) {
×
752
      (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
×
753
      (void)taosThreadCondSignal(&pPool->waitingBeforeProcessMsgCond);
×
754
      (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
×
755
      ret = true;
7,692✔
756
      break;
7,692✔
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))
311,956,607✔
770
      return true;
311,931,555✔
771
  }
772
  return false;
2,147,483,647✔
773
}
774

775
static void tQueryAutoQWorkerWaitingCheck(SQueryAutoQWorkerPool *pPool) {
2,147,483,647✔
776
  while (1) {
8,404✔
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
    }
UNCOV
784
    if (atomicCompareExchangeActive(&pPool->activeRunningN, &active, active - 1)) {
×
785
      break;
×
786
    }
787
  }
788
  // to wait for process
789
  (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
×
790
  (void)atomic_fetch_add_32(&pPool->waitingBeforeProcessMsgN, 1);
×
791
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingBeforeProcessMsgCond, &pPool->waitingBeforeProcessMsgLock);
×
792
  // recovered from waiting
793
  (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
×
794
  return;
×
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);
137,049,569✔
801
    if (pPool->exit) {
137,018,161✔
802
      (void)taosThreadMutexUnlock(&pPool->poolLock);
483✔
803
      return false;
483✔
804
    }
805

806
    SListNode *pNode = listNode(pWorker);
137,017,678✔
807
    SListNode *tNode = tdListPopNode(pPool->workers, pNode);
137,017,678✔
808
    // reclaim some workers
809
    if (pWorker->id >= pPool->maxInUse) {
137,017,678✔
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);
137,017,678✔
826
    (void)taosThreadMutexUnlock(&pPool->poolLock);
137,017,678✔
827

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

834
    // recovered from backup
835
    (void)taosThreadMutexLock(&pPool->poolLock);
137,017,404✔
836
    if (pPool->exit) {
137,017,678✔
837
      (void)taosThreadMutexUnlock(&pPool->poolLock);
994,935✔
838
      return false;
994,935✔
839
    }
840
    SListNode *tNode1 = tdListPopNode(pPool->backupWorkers, pNode);
136,022,743✔
841
    tdListAppendNode(pPool->workers, pNode);
136,022,743✔
842
    (void)taosThreadMutexUnlock(&pPool->poolLock);
136,022,743✔
843

844
    return true;
136,022,743✔
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) {
4,243,784✔
852
  int32_t code;
853

854
  pool->exit = false;
4,243,784✔
855

856
  (void)taosThreadMutexInit(&pool->poolLock, NULL);
4,243,784✔
857
  (void)taosThreadMutexInit(&pool->backupLock, NULL);
4,243,784✔
858
  (void)taosThreadMutexInit(&pool->waitingAfterBlockLock, NULL);
4,243,784✔
859
  (void)taosThreadMutexInit(&pool->waitingBeforeProcessMsgLock, NULL);
4,243,784✔
860

861
  (void)taosThreadCondInit(&pool->waitingBeforeProcessMsgCond, NULL);
4,243,784✔
862
  (void)taosThreadCondInit(&pool->waitingAfterBlockCond, NULL);
4,243,784✔
863
  (void)taosThreadCondInit(&pool->backupCond, NULL);
4,243,784✔
864

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

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

885
void tQueryAutoQWorkerCleanup(SQueryAutoQWorkerPool *pPool) {
4,242,996✔
886
  (void)taosThreadMutexLock(&pPool->poolLock);
4,242,996✔
887
  pPool->exit = true;
4,242,996✔
888

889
  int32_t size = 0;
4,242,996✔
890
  if (pPool->workers) {
4,242,996✔
891
    size += listNEles(pPool->workers);
4,242,931✔
892
  }
893
  if (pPool->backupWorkers) {
4,242,996✔
894
    size += listNEles(pPool->backupWorkers);
4,242,931✔
895
  }
896
  if (pPool->qset) {
4,242,996✔
897
    for (int32_t i = 0; i < size; ++i) {
214,100,887✔
898
      taosQsetThreadResume(pPool->qset);
209,857,956✔
899
    }
900
  }
901
  (void)taosThreadMutexUnlock(&pPool->poolLock);
4,242,996✔
902

903
  (void)taosThreadMutexLock(&pPool->backupLock);
4,242,996✔
904
  (void)taosThreadCondBroadcast(&pPool->backupCond);
4,242,996✔
905
  (void)taosThreadMutexUnlock(&pPool->backupLock);
4,242,996✔
906

907
  (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
4,242,996✔
908
  (void)taosThreadCondBroadcast(&pPool->waitingAfterBlockCond);
4,242,996✔
909
  (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
4,242,996✔
910

911
  (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
4,242,996✔
912
  (void)taosThreadCondBroadcast(&pPool->waitingBeforeProcessMsgCond);
4,242,996✔
913
  (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
4,242,996✔
914

915
  int32_t            idx = 0;
4,242,996✔
916
  SQueryAutoQWorker *worker = NULL;
4,242,996✔
917
  while (pPool->workers) {
213,106,017✔
918
    (void)taosThreadMutexLock(&pPool->poolLock);
213,105,952✔
919
    if (listNEles(pPool->workers) <= 0) {
213,105,952✔
920
      (void)taosThreadMutexUnlock(&pPool->poolLock);
4,242,931✔
921
      break;
4,242,931✔
922
    }
923
    SListNode *pNode = tdListPopHead(pPool->workers);
208,863,021✔
924
    uDebug("0free worker node:%p, prev:%p, next:%p", pNode, TD_DLIST_NODE_PREV(pNode), TD_DLIST_NODE_NEXT(pNode));
208,863,021✔
925
    worker = pNode ? (SQueryAutoQWorker *)pNode->data : NULL;
208,863,021✔
926
    (void)taosThreadMutexUnlock(&pPool->poolLock);
208,863,021✔
927
    if (worker && taosCheckPthreadValid(worker->thread)) {
208,863,021✔
928
      (void)taosThreadJoin(worker->thread, NULL);
208,863,021✔
929
      taosThreadClear(&worker->thread);
208,863,021✔
930
    }
931
    uDebug("free worker node:%p, prev:%p, next:%p", pNode, TD_DLIST_NODE_PREV(pNode), TD_DLIST_NODE_NEXT(pNode));
208,863,021✔
932

933
    taosMemoryFree(pNode);
208,863,021✔
934
  }
935

936
  while (pPool->backupWorkers) {
5,237,931✔
937
    (void)taosThreadMutexLock(&pPool->poolLock);
5,237,866✔
938
    if (listNEles(pPool->backupWorkers) <= 0) {
5,237,866✔
939
      (void)taosThreadMutexUnlock(&pPool->poolLock);
4,242,931✔
940
      break;
4,242,931✔
941
    }
942
    uDebug("backupworker head:%p, prev:%p, next:%p", TD_DLIST_HEAD(pPool->backupWorkers), 
994,935✔
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);
994,935✔
946
    worker = pNode ? (SQueryAutoQWorker *)pNode->data : NULL;
994,935✔
947
    (void)taosThreadMutexUnlock(&pPool->poolLock);
994,935✔
948

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

956
  while (pPool->exitedWorkers) {
4,242,996✔
957
    (void)taosThreadMutexLock(&pPool->poolLock);
4,242,931✔
958
    if (listNEles(pPool->exitedWorkers) == 0) {
4,242,931✔
959
      (void)taosThreadMutexUnlock(&pPool->poolLock);
4,242,931✔
960
      break;
4,242,931✔
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);
4,242,996✔
975
  pPool->workers = tdListFree(pPool->workers);
4,242,996✔
976
  pPool->backupWorkers = tdListFree(pPool->backupWorkers);
4,242,996✔
977
  pPool->exitedWorkers = tdListFree(pPool->exitedWorkers);
4,242,996✔
978
  taosMemoryFree(pPool->pCb);
4,242,996✔
979
  (void)taosThreadMutexUnlock(&pPool->poolLock);
4,242,996✔
980

981
  (void)taosThreadMutexDestroy(&pPool->poolLock);
4,242,996✔
982
  (void)taosThreadMutexDestroy(&pPool->backupLock);
4,242,996✔
983
  (void)taosThreadMutexDestroy(&pPool->waitingAfterBlockLock);
4,242,996✔
984
  (void)taosThreadMutexDestroy(&pPool->waitingBeforeProcessMsgLock);
4,242,996✔
985

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

992
STaosQueue *tQueryAutoQWorkerAllocQueue(SQueryAutoQWorkerPool *pool, void *ahandle, FItem fp) {
12,624,768✔
993
  STaosQueue *queue;
12,592,973✔
994
  int32_t     code = taosOpenQueue(&queue);
12,624,768✔
995
  if (code) {
12,624,704✔
996
    terrno = code;
×
997
    return NULL;
×
998
  }
999

1000
  (void)taosThreadMutexLock(&pool->poolLock);
12,624,704✔
1001
  taosSetQueueFp(queue, fp, NULL);
12,624,768✔
1002
  code = taosAddIntoQset(pool->qset, queue, ahandle);
12,624,768✔
1003
  if (code) {
12,624,431✔
1004
    taosCloseQueue(queue);
×
1005
    queue = NULL;
×
1006
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
1007
    return NULL;
×
1008
  }
1009
  SQueryAutoQWorker  worker = {0};
12,624,431✔
1010
  SQueryAutoQWorker *pWorker = NULL;
12,624,431✔
1011

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

1027
      TdThreadAttr thAttr;
207,506,650✔
1028
      (void)taosThreadAttrInit(&thAttr);
208,930,778✔
1029
      (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
208,930,778✔
1030

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

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

1044
  (void)taosThreadMutexUnlock(&pool->poolLock);
12,623,699✔
1045
  uInfo("worker:%s, queue:%p is allocated, ahandle:%p", pool->name, queue, ahandle);
12,624,768✔
1046

1047
  return queue;
12,624,768✔
1048
}
1049

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

1052
static int32_t tQueryAutoQWorkerAddWorker(SQueryAutoQWorkerPool *pool) {
137,016,128✔
1053
  // try backup pool
1054
  int32_t backup = pool->backupNum;
137,016,128✔
1055
  while (backup > 0) {
137,032,525✔
1056
    int32_t backupNew = atomic_val_compare_exchange_32(&pool->backupNum, backup, backup - 1);
136,037,107✔
1057
    if (backupNew == backup) {
136,039,140✔
1058
      (void)taosThreadCondSignal(&pool->backupCond);
136,022,743✔
1059
      return TSDB_CODE_SUCCESS;
136,022,743✔
1060
    }
1061
    backup = backupNew;
16,397✔
1062
  }
1063
  // backup pool is empty, create new
1064
  SQueryAutoQWorker *pWorker = NULL;
995,418✔
1065
  SQueryAutoQWorker  worker = {0};
995,418✔
1066
  worker.pool = pool;
995,418✔
1067
  worker.backupIdx = -1;
995,418✔
1068
  (void)taosThreadMutexLock(&pool->poolLock);
995,418✔
1069
  if (pool->exit) {
995,418✔
1070
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
1071
    return TSDB_CODE_SUCCESS;
×
1072
  }
1073
  worker.id = listNEles(pool->workers);
995,418✔
1074
  SListNode *pNode = tdListAdd(pool->workers, &worker);
995,418✔
1075
  if (!pNode) {
995,418✔
1076
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
1077
    return terrno;
×
1078
  }
1079
  (void)taosThreadMutexUnlock(&pool->poolLock);
995,418✔
1080
  pWorker = (SQueryAutoQWorker *)pNode->data;
995,418✔
1081

1082
  TdThreadAttr thAttr;
994,130✔
1083
  (void)taosThreadAttrInit(&thAttr);
995,418✔
1084
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
995,418✔
1085

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

1092
  return TSDB_CODE_SUCCESS;
995,418✔
1093
}
1094

1095
static int32_t tQueryAutoQWorkerBeforeBlocking(void *p) {
311,928,020✔
1096
  SQueryAutoQWorkerPool *pPool = p;
311,928,020✔
1097
  if (tQueryAutoQWorkerTrySignalWaitingAfterBlock(p) || tQueryAutoQWorkerTrySignalWaitingBeforeProcess(p) ||
623,856,332✔
1098
      tQueryAutoQWorkerTryDecActive(p, pPool->num)) {
311,925,249✔
1099
  } else {
1100
    int32_t code = tQueryAutoQWorkerAddWorker(pPool);
137,014,918✔
1101
    if (code != TSDB_CODE_SUCCESS) {
137,018,161✔
1102
      return code;
×
1103
    }
1104
    (void)atomicFetchSubRunning(&pPool->activeRunningN, 1);
137,018,161✔
1105
  }
1106

1107
  return TSDB_CODE_SUCCESS;
311,930,950✔
1108
}
1109

1110
static int32_t tQueryAutoQWorkerRecoverFromBlocking(void *p) {
311,930,375✔
1111
  SQueryAutoQWorkerPool *pPool = p;
311,930,375✔
1112
  int64_t                val64 = pPool->activeRunningN;
311,930,375✔
1113
  int32_t                running = GET_RUNNING_N(val64), active = GET_ACTIVE_N(val64);
311,931,269✔
1114
  while (running < pPool->num) {
311,943,929✔
1115
    if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active + 1, &running, running + 1)) {
311,943,929✔
1116
      return TSDB_CODE_SUCCESS;
311,931,555✔
1117
    }
1118
  }
1119
  (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
×
1120
  (void)atomic_fetch_add_32(&pPool->waitingAfterBlockN, 1);
×
1121
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingAfterBlockCond, &pPool->waitingAfterBlockLock);
×
1122
  (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
×
1123
  if (pPool->exit) return TSDB_CODE_QRY_QWORKER_QUIT;
×
1124
  return TSDB_CODE_SUCCESS;
×
1125
}
1126

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

1136
static void *tDispatchWorkerThreadFp(SDispatchWorker *pWorker) {
9,869,793✔
1137
  SDispatchWorkerPool *pPool = pWorker->pool;
9,869,793✔
1138
  SQueueInfo qinfo = {0};
9,872,381✔
1139
  int32_t code = 0;
9,872,693✔
1140
  void *msg = NULL;
9,872,693✔
1141

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

1147
  setThreadName(pPool->name);
9,871,791✔
1148
  if (pPool->threadCategory >= 0) taosSetCpuAffinity((EThreadCategory)pPool->threadCategory);
9,872,727✔
1149
  pWorker->pid = taosGetSelfPthreadId();
9,871,616✔
1150
  uInfo("worker:%s:%d is running, thread:%d", pPool->name, pWorker->id, pWorker->pid);
9,870,793✔
1151

1152
  while (1) {
1153
    if (taosReadQitemFromQset(pWorker->qset, (void **)&msg, &qinfo) == 0) {
144,441,489✔
1154
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%d", pPool->name, pWorker->id,
9,857,242✔
1155
            pWorker->qset, pWorker->pid);
1156
      break;
9,871,329✔
1157
    }
1158

1159
    if (qinfo.timestamp != 0) {
134,570,207✔
1160
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
134,576,393✔
1161
      if (cost > QUEUE_THRESHOLD) {
134,576,393✔
1162
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pPool->name, cost / QUEUE_THRESHOLD);
364,707✔
1163
      }
1164
    }
1165

1166
    if (qinfo.fp != NULL) {
134,571,966✔
1167
      qinfo.workerId = pWorker->id;
134,572,968✔
1168
      qinfo.threadNum = pPool->num;
134,574,343✔
1169
      (*((FItem)qinfo.fp))(&qinfo, msg);
134,576,838✔
1170
    }
1171
  }
1172
  DestoryThreadLocalRegComp();
9,871,329✔
1173
  closeThreadNotificationConn();
9,872,092✔
1174
  return NULL;
9,869,491✔
1175
}
1176

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

1195
    TdThreadAttr thAttr;
9,858,775✔
1196
    (void)taosThreadAttrInit(&thAttr);
9,872,727✔
1197
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
9,872,727✔
1198

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

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

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

1230
void tDispatchWorkerCleanup(SDispatchWorkerPool *pPool) {
1,351,401✔
1231
  (void)taosThreadMutexLock(&pPool->poolLock);
1,351,401✔
1232
  pPool->exit = true;
1,351,401✔
1233
  if (pPool->pWorkers) {
1,351,401✔
1234
    for (int32_t i = 0; i < pPool->num; ++i) {
11,224,128✔
1235
      SDispatchWorker *pWorker = pPool->pWorkers + i;
9,872,727✔
1236
      if (pWorker->qset) {
9,872,727✔
1237
        taosQsetThreadResume(pWorker->qset);
9,872,727✔
1238
      }
1239
    }
1240
  }
1241
  (void)taosThreadMutexUnlock(&pPool->poolLock);
1,351,401✔
1242

1243
  if (pPool->pWorkers) {
1,351,401✔
1244
    for (int32_t i = 0; i < pPool->num; ++i) {
11,224,128✔
1245
      SDispatchWorker *pWorker = pPool->pWorkers + i;
9,872,727✔
1246
      if (taosCheckPthreadValid(pWorker->thread)) {
9,872,727✔
1247
        (void)taosThreadJoin(pWorker->thread, NULL);
9,872,727✔
1248
        taosThreadClear(&pWorker->thread);
9,872,727✔
1249
      }
1250
    }
1251
  }
1252
  tDispatchWorkerFreeQueue(pPool);
1,351,401✔
1253
  taosMemoryFreeClear(pPool->pWorkers);
1,351,401✔
1254
  (void)taosThreadMutexDestroy(&pPool->poolLock);
1,351,401✔
1255
}
1,351,401✔
1256

1257
int32_t tAddTaskIntoDispatchWorkerPool(SDispatchWorkerPool *pPool, void *pMsg) {
134,571,338✔
1258
  int32_t code = 0;
134,571,338✔
1259
  int32_t idx = 0;
134,571,338✔
1260
  SDispatchWorker *pWorker = NULL;
134,575,155✔
1261
  (void)taosThreadMutexLock(&pPool->poolLock);
134,575,155✔
1262
  code = pPool->dispatchFp(pPool, pMsg, &idx);
134,581,068✔
1263
  if (code == 0) {
134,581,068✔
1264
    pWorker = pPool->pWorkers + idx;
134,581,068✔
1265
    if (pWorker->queue) {
134,581,068✔
1266
      code = taosWriteQitem(pWorker->queue, pMsg);
134,581,068✔
1267
    } else {
1268
      code = TSDB_CODE_INTERNAL_ERROR;
×
1269
    }
1270
  }
1271
  (void)taosThreadMutexUnlock(&pPool->poolLock);
134,581,068✔
1272
  if (code != 0) {
134,581,068✔
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));
134,581,068✔
1276
  }
1277
  return code;
134,577,498✔
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