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

taosdata / TDengine / #5034

24 Apr 2026 11:25AM UTC coverage: 73.058%. Remained the same
#5034

push

travis-ci

web-flow
merge: from main to 3.0 branch #35224

merge: from main to 3.0 branch[manual-only]

1336 of 1975 new or added lines in 48 files covered. (67.65%)

14149 existing lines in 164 files now uncovered.

275896 of 377640 relevant lines covered (73.06%)

132944440.29 hits per line

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

77.1
/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) {
5,842,671✔
30
  int32_t code = taosOpenQset(&pool->qset);
5,842,671✔
31
  if (code) return code;
5,842,671✔
32

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

39
  (void)taosThreadMutexInit(&pool->mutex, NULL);
5,842,671✔
40

41
  for (int32_t i = 0; i < pool->max; ++i) {
38,856,025✔
42
    SQueueWorker *worker = pool->workers + i;
33,013,354✔
43
    worker->id = i;
33,013,354✔
44
    worker->pool = pool;
33,013,354✔
45
  }
46

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

51
void tQWorkerCleanup(SQWorkerPool *pool) {
5,842,671✔
52
  for (int32_t i = 0; i < pool->max; ++i) {
38,856,025✔
53
    SQueueWorker *worker = pool->workers + i;
33,013,354✔
54
    if (taosCheckPthreadValid(worker->thread)) {
33,013,354✔
55
      taosQsetThreadResume(pool->qset);
33,013,354✔
56
    }
57
  }
58

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

69
  taosMemoryFreeClear(pool->workers);
5,842,671✔
70
  taosCloseQset(pool->qset);
5,842,671✔
71
  (void)taosThreadMutexDestroy(&pool->mutex);
5,842,671✔
72

73
  uInfo("worker:%s is closed", pool->name);
5,842,671✔
74
}
5,842,671✔
75

76
static void *tQWorkerThreadFp(SQueueWorker *worker) {
33,012,609✔
77
  SQWorkerPool *pool = worker->pool;
33,012,609✔
78
  SQueueInfo    qinfo = {0};
33,013,166✔
79
  void         *msg = NULL;
33,013,071✔
80
  int32_t       code = 0;
33,013,162✔
81

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

87
  setThreadName(pool->name);
33,011,728✔
88
  worker->pid = taosGetSelfPthreadId();
33,012,602✔
89
  uInfo("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
33,006,412✔
90

91
  while (1) {
92
    if (taosReadQitemFromQset(pool->qset, (void **)&msg, &qinfo) == 0) {
509,477,329✔
93
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, pool->qset,
32,980,773✔
94
            worker->pid);
95
      break;
32,975,502✔
96
    }
97

98
    if (qinfo.timestamp != 0) {
476,467,386✔
99
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
476,467,717✔
100
      if (cost > QUEUE_THRESHOLD) {
476,467,717✔
101
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pool->name, cost / QUEUE_THRESHOLD);
896,369✔
102
      }
103
    }
104

105
    if (qinfo.fp != NULL) {
476,466,424✔
106
      qinfo.workerId = worker->id;
476,465,881✔
107
      qinfo.threadNum = pool->num;
476,466,645✔
108
      (*((FItem)qinfo.fp))(&qinfo, msg);
476,467,184✔
109
    }
110

111
    taosUpdateItemSize(qinfo.queue, 1);
476,463,673✔
112
  }
113

114
  DestoryThreadLocalRegComp();
32,975,502✔
115

116
  return NULL;
33,012,099✔
117
}
118

119
STaosQueue *tQWorkerAllocQueue(SQWorkerPool *pool, void *ahandle, FItem fp) {
5,842,671✔
120
  int32_t     code;
121
  STaosQueue *queue;
5,822,897✔
122

123
  code = taosOpenQueue(&queue);
5,842,671✔
124
  if (code) {
5,842,671✔
125
    terrno = code;
×
126
    return NULL;
×
127
  }
128

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

139
  // spawn a thread to process queue
140
  if (pool->num < pool->max) {
5,842,671✔
141
    do {
142
      SQueueWorker *worker = pool->workers + pool->num;
33,013,354✔
143

144
      TdThreadAttr thAttr;
32,921,250✔
145
      (void)taosThreadAttrInit(&thAttr);
33,013,354✔
146
      (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
33,013,354✔
147

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

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

161
  (void)taosThreadMutexUnlock(&pool->mutex);
5,842,671✔
162
  uInfo("worker:%s, queue:%p is allocated, ahandle:%p", pool->name, queue, ahandle);
5,842,671✔
163

164
  return queue;
5,842,671✔
165
}
166

167
void tQWorkerFreeQueue(SQWorkerPool *pool, STaosQueue *queue) {
5,842,671✔
168
  uInfo("worker:%s, queue:%p is freed", pool->name, queue);
5,842,671✔
169
  taosCloseQueue(queue);
5,842,671✔
170
}
5,842,671✔
171

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

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

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

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

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

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

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

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

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

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

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

230
  setThreadName(pool->name);
×
231
  worker->pid = taosGetSelfPthreadId();
×
232
  uInfo("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
×
233

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

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

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

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

259
  return NULL;
×
260
}
261

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

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

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

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

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

287
  if (dstWorkerNum < minNum) {
×
288
    dstWorkerNum = minNum;
×
289
  }
290

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

305
    TdThreadAttr thAttr;
×
306
    (void)taosThreadAttrInit(&thAttr);
×
307
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
×
308

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

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

322
    curWorkerNum++;
×
323
  }
324

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

328
  return queue;
×
329
}
330

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

336
int32_t tWWorkerInit(SWWorkerPool *pool) {
19,379,309✔
337
  pool->nextId = 0;
19,379,309✔
338
  pool->workers = taosMemoryCalloc(pool->max, sizeof(SWWorker));
19,379,692✔
339
  if (pool->workers == NULL) {
19,379,999✔
340
    return terrno;
×
341
  }
342

343
  (void)taosThreadMutexInit(&pool->mutex, NULL);
19,380,304✔
344

345
  for (int32_t i = 0; i < pool->max; ++i) {
45,168,320✔
346
    SWWorker *worker = pool->workers + i;
25,789,542✔
347
    worker->id = i;
25,789,081✔
348
    worker->qall = NULL;
25,788,444✔
349
    worker->qset = NULL;
25,788,399✔
350
    worker->pool = pool;
25,788,101✔
351
  }
352

353
  uInfo("worker:%s is initialized, max:%d", pool->name, pool->max);
19,378,521✔
354
  return 0;
19,380,671✔
355
}
356

357
void tWWorkerCleanup(SWWorkerPool *pool) {
19,380,671✔
358
  for (int32_t i = 0; i < pool->max; ++i) {
45,170,170✔
359
    SWWorker *worker = pool->workers + i;
25,789,366✔
360
    if (taosCheckPthreadValid(worker->thread)) {
25,789,474✔
361
      if (worker->qset) {
21,758,999✔
362
        taosQsetThreadResume(worker->qset);
21,759,366✔
363
      }
364
    }
365
  }
366

367
  for (int32_t i = 0; i < pool->max; ++i) {
45,169,784✔
368
    SWWorker *worker = pool->workers + i;
25,789,499✔
369
    if (taosCheckPthreadValid(worker->thread)) {
25,789,499✔
370
      uInfo("worker:%s:%d is stopping", pool->name, worker->id);
21,759,397✔
371
      (void)taosThreadJoin(worker->thread, NULL);
21,759,440✔
372
      taosThreadClear(&worker->thread);
21,759,248✔
373
      taosFreeQall(worker->qall);
21,759,440✔
374
      taosCloseQset(worker->qset);
21,759,440✔
375
      uInfo("worker:%s:%d is stopped", pool->name, worker->id);
21,759,440✔
376
    }
377
  }
378

379
  taosMemoryFreeClear(pool->workers);
19,380,671✔
380
  (void)taosThreadMutexDestroy(&pool->mutex);
19,380,671✔
381

382
  uInfo("worker:%s is closed", pool->name);
19,380,671✔
383
}
19,380,671✔
384

385
static void *tWWorkerThreadFp(SWWorker *worker) {
21,759,341✔
386
  SWWorkerPool *pool = worker->pool;
21,759,341✔
387
  SQueueInfo    qinfo = {0};
21,759,341✔
388
  void         *msg = NULL;
21,759,341✔
389
  int32_t       code = 0;
21,759,341✔
390
  int32_t       numOfMsgs = 0;
21,759,341✔
391

392
  int32_t ret = taosBlockSIGPIPE();
21,759,341✔
393
  if (ret < 0) {
21,756,885✔
394
    uError("worker:%s:%d failed to block SIGPIPE", pool->name, worker->id);
×
395
  }
396

397
  setThreadName(pool->name);
21,756,885✔
398
  worker->pid = taosGetSelfPthreadId();
21,759,440✔
399
  uInfo("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
21,756,800✔
400

401
  while (1) {
402
    numOfMsgs = taosReadAllQitemsFromQset(worker->qset, worker->qall, &qinfo);
2,147,483,647✔
403
    if (numOfMsgs == 0) {
2,147,483,647✔
404
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, worker->qset,
21,754,012✔
405
            worker->pid);
406
      break;
21,759,440✔
407
    }
408

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

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

424
  return NULL;
21,759,440✔
425
}
426

427
STaosQueue *tWWorkerAllocQueue(SWWorkerPool *pool, void *ahandle, FItems fp) {
23,280,708✔
428
  (void)taosThreadMutexLock(&pool->mutex);
23,280,708✔
429
  SWWorker   *worker = pool->workers + pool->nextId;
23,280,708✔
430
  int32_t     code = -1;
23,280,708✔
431
  STaosQueue *queue;
23,255,596✔
432

433
  code = taosOpenQueue(&queue);
23,280,708✔
434
  if (code) goto _OVER;
23,280,708✔
435

436
  taosSetQueueFp(queue, NULL, fp);
23,280,708✔
437
  if (worker->qset == NULL) {
23,280,305✔
438
    code = taosOpenQset(&worker->qset);
21,759,440✔
439
    if (code) goto _OVER;
21,757,641✔
440

441
    code = taosAddIntoQset(worker->qset, queue, ahandle);
21,757,641✔
442
    if (code) goto _OVER;
21,759,440✔
443
    code = taosAllocateQall(&worker->qall);
21,759,440✔
444
    if (code) goto _OVER;
21,759,440✔
445

446
    TdThreadAttr thAttr;
21,737,186✔
447
    (void)taosThreadAttrInit(&thAttr);
21,759,401✔
448
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
21,758,586✔
449
    code = taosThreadCreate(&worker->thread, &thAttr, (ThreadFp)tWWorkerThreadFp, worker);
21,757,875✔
450
    if ((code)) goto _OVER;
21,759,440✔
451

452
    uInfo("worker:%s:%d is launched, max:%d", pool->name, worker->id, pool->max);
21,759,440✔
453
    pool->nextId = (pool->nextId + 1) % pool->max;
21,759,440✔
454

455
    (void)taosThreadAttrDestroy(&thAttr);
21,759,440✔
456
    pool->num++;
21,759,440✔
457
    if (pool->num > pool->max) pool->num = pool->max;
21,759,440✔
458
  } else {
459
    code = taosAddIntoQset(worker->qset, queue, ahandle);
1,521,268✔
460
    if (code) goto _OVER;
1,521,268✔
461
    pool->nextId = (pool->nextId + 1) % pool->max;
1,521,268✔
462
  }
463

464
_OVER:
23,280,708✔
465
  (void)taosThreadMutexUnlock(&pool->mutex);
23,280,708✔
466

467
  if (code) {
23,280,708✔
468
    if (queue != NULL) taosCloseQueue(queue);
×
469
    if (worker->qset != NULL) taosCloseQset(worker->qset);
×
470
    if (worker->qall != NULL) taosFreeQall(worker->qall);
×
471
    terrno = code;
×
472
    return NULL;
×
473
  } else {
474
    while (worker->pid <= 0) taosMsleep(10);
48,617,053✔
475

476
    taosQueueSetThreadId(queue, worker->pid);
23,279,745✔
477
    uInfo("worker:%s, queue:%p is allocated, ahandle:%p thread:%08" PRId64, pool->name, queue, ahandle, worker->pid);
23,275,599✔
478
    return queue;
23,280,708✔
479
  }
480
}
481

482
void tWWorkerFreeQueue(SWWorkerPool *pool, STaosQueue *queue) {
23,280,441✔
483
  uInfo("worker:%s, queue:%p is freed", pool->name, queue);
23,280,441✔
484
  taosCloseQueue(queue);
23,280,441✔
485
}
23,280,577✔
486

487
int32_t tSingleWorkerInit(SSingleWorker *pWorker, const SSingleWorkerCfg *pCfg) {
6,880,074✔
488
  int32_t code;
489
  pWorker->poolType = pCfg->poolType;
6,880,074✔
490
  pWorker->name = pCfg->name;
6,880,074✔
491
  pWorker->stopNoWaitQueue = pCfg->stopNoWaitQueue;
6,880,074✔
492

493
  switch (pCfg->poolType) {
6,880,074✔
494
    case QWORKER_POOL: {
5,842,671✔
495
      SQWorkerPool *pPool = taosMemoryCalloc(1, sizeof(SQWorkerPool));
5,842,671✔
496
      if (!pPool) {
5,842,671✔
497
        return terrno;
×
498
      }
499
      pPool->name = pCfg->name;
5,842,671✔
500
      pPool->min = pCfg->min;
5,842,671✔
501
      pPool->max = pCfg->max;
5,842,671✔
502
      pWorker->pool = pPool;
5,842,671✔
503
      if ((code = tQWorkerInit(pPool))) {
5,842,671✔
504
        return (terrno = code);
×
505
      }
506

507
      pWorker->queue = tQWorkerAllocQueue(pPool, pCfg->param, pCfg->fp);
5,842,671✔
508
      if (pWorker->queue == NULL) {
5,842,671✔
509
        return terrno;
×
510
      }
511
    } break;
5,842,671✔
512
    case QUERY_AUTO_QWORKER_POOL: {
1,037,403✔
513
      SQueryAutoQWorkerPool *pPool = taosMemoryCalloc(1, sizeof(SQueryAutoQWorkerPool));
1,037,403✔
514
      if (!pPool) {
1,037,403✔
515
        return terrno;
×
516
      }
517
      pPool->name = pCfg->name;
1,037,403✔
518
      pPool->min = pCfg->min;
1,037,403✔
519
      pPool->max = pCfg->max;
1,037,403✔
520
      pPool->stopNoWaitQueue = pCfg->stopNoWaitQueue;
1,037,403✔
521
      pWorker->pool = pPool;
1,037,403✔
522

523
      code = tQueryAutoQWorkerInit(pPool);
1,037,403✔
524
      if (code) return code;
1,037,403✔
525

526
      pWorker->queue = tQueryAutoQWorkerAllocQueue(pPool, pCfg->param, pCfg->fp);
1,037,403✔
527
      if (!pWorker->queue) {
1,037,403✔
528
        return terrno;
×
529
      }
530
    } break;
1,037,403✔
531
    default:
×
532
      return TSDB_CODE_INVALID_PARA;
×
533
  }
534
  return 0;
6,880,074✔
535
}
536

537
void tSingleWorkerCleanup(SSingleWorker *pWorker) {
6,880,074✔
538
  if (pWorker->queue == NULL) return;
6,880,074✔
539
  if (!pWorker->stopNoWaitQueue) {
6,880,074✔
540
    while (!taosQueueEmpty(pWorker->queue)) {
7,010,570✔
541
      taosMsleep(10);
198,411✔
542
    }
543
  }
544

545
  switch (pWorker->poolType) {
6,880,074✔
546
    case QWORKER_POOL:
5,842,671✔
547
      tQWorkerCleanup(pWorker->pool);
5,842,671✔
548
      tQWorkerFreeQueue(pWorker->pool, pWorker->queue);
5,842,671✔
549
      taosMemoryFree(pWorker->pool);
5,842,671✔
550
      break;
5,842,671✔
551
    case QUERY_AUTO_QWORKER_POOL:
1,037,403✔
552
      tQueryAutoQWorkerCleanup(pWorker->pool);
1,037,403✔
553
      tQueryAutoQWorkerFreeQueue(pWorker->pool, pWorker->queue);
1,037,403✔
554
      taosMemoryFree(pWorker->pool);
1,037,403✔
555
      break;
1,037,403✔
556
    default:
×
557
      break;
×
558
  }
559
}
560

561
int32_t tMultiWorkerInit(SMultiWorker *pWorker, const SMultiWorkerCfg *pCfg) {
18,237,242✔
562
  SWWorkerPool *pPool = &pWorker->pool;
18,237,242✔
563
  pPool->name = pCfg->name;
18,237,818✔
564
  pPool->max = pCfg->max;
18,239,164✔
565

566
  int32_t code = tWWorkerInit(pPool);
18,238,185✔
567
  if (code) return code;
18,239,164✔
568

569
  pWorker->queue = tWWorkerAllocQueue(pPool, pCfg->param, pCfg->fp);
18,239,164✔
570
  if (pWorker->queue == NULL) {
18,239,164✔
571
    return terrno;
×
572
  }
573

574
  pWorker->name = pCfg->name;
18,239,164✔
575
  return 0;
18,238,482✔
576
}
577

578
void tMultiWorkerCleanup(SMultiWorker *pWorker) {
18,239,164✔
579
  if (pWorker->queue == NULL) return;
18,239,164✔
580

581
  while (!taosQueueEmpty(pWorker->queue)) {
23,469,323✔
582
    taosMsleep(10);
5,229,701✔
583
  }
584

585
  tWWorkerCleanup(&pWorker->pool);
18,239,164✔
586
  tWWorkerFreeQueue(&pWorker->pool, pWorker->queue);
18,239,164✔
587
}
588

589
static int32_t tQueryAutoQWorkerAddWorker(SQueryAutoQWorkerPool *pool);
590
static int32_t tQueryAutoQWorkerBeforeBlocking(void *p);
591
static int32_t tQueryAutoQWorkerRecoverFromBlocking(void *p);
592
static void    tQueryAutoQWorkerWaitingCheck(SQueryAutoQWorkerPool *pPool);
593
static bool    tQueryAutoQWorkerTryRecycleWorker(SQueryAutoQWorkerPool *pPool, SQueryAutoQWorker *pWorker);
594

595
#define GET_ACTIVE_N(int64_val)  (int32_t)((int64_val) >> 32)
596
#define GET_RUNNING_N(int64_val) (int32_t)(int64_val & 0xFFFFFFFF)
597

598
static int32_t atomicFetchSubActive(int64_t *ptr, int32_t val) {
×
599
  int64_t acutalSubVal = val;
×
600
  acutalSubVal <<= 32;
×
601
  int64_t newVal64 = atomic_fetch_sub_64(ptr, acutalSubVal);
×
602
  return GET_ACTIVE_N(newVal64);
×
603
}
604

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

607
static int32_t atomicFetchAddActive(int64_t *ptr, int32_t val) {
195,512,894✔
608
  int64_t actualAddVal = val;
195,512,894✔
609
  actualAddVal <<= 32;
195,512,894✔
610
  int64_t newVal64 = atomic_fetch_add_64(ptr, actualAddVal);
195,512,894✔
611
  return GET_ACTIVE_N(newVal64);
195,512,894✔
612
}
613

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

616
static bool atomicCompareExchangeActive(int64_t *ptr, int32_t *expectedVal, int32_t newVal) {
434✔
617
  int64_t oldVal64 = *expectedVal, newVal64 = newVal;
434✔
618
  int32_t running = GET_RUNNING_N(*ptr);
434✔
619
  oldVal64 <<= 32;
434✔
620
  newVal64 <<= 32;
434✔
621
  oldVal64 |= running;
434✔
622
  newVal64 |= running;
434✔
623
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
434✔
624
  if (actualNewVal64 == oldVal64) {
434✔
625
    return true;
434✔
626
  } else {
627
    *expectedVal = GET_ACTIVE_N(actualNewVal64);
×
628
    return false;
×
629
  }
630
}
631

632
static int64_t atomicCompareExchangeRunning(int64_t *ptr, int32_t *expectedVal, int32_t newVal) {
×
633
  int64_t oldVal64 = *expectedVal, newVal64 = newVal;
×
634
  int64_t activeShifted = GET_ACTIVE_N(*ptr);
×
635
  activeShifted <<= 32;
×
636
  oldVal64 |= activeShifted;
×
637
  newVal64 |= activeShifted;
×
638
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
×
639
  if (actualNewVal64 == oldVal64) {
×
640
    return true;
×
641
  } else {
642
    *expectedVal = GET_RUNNING_N(actualNewVal64);
×
643
    return false;
×
644
  }
645
}
646

647
static int64_t atomicCompareExchangeActiveAndRunning(int64_t *ptr, int32_t *expectedActive, int32_t newActive,
2,147,483,647✔
648
                                                     int32_t *expectedRunning, int32_t newRunning) {
649
  int64_t oldVal64 = *expectedActive, newVal64 = newActive;
2,147,483,647✔
650
  oldVal64 <<= 32;
2,147,483,647✔
651
  oldVal64 |= *expectedRunning;
2,147,483,647✔
652
  newVal64 <<= 32;
2,147,483,647✔
653
  newVal64 |= newRunning;
2,147,483,647✔
654
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
2,147,483,647✔
655
  if (actualNewVal64 == oldVal64) {
2,147,483,647✔
656
    return true;
2,147,483,647✔
657
  } else {
658
    *expectedActive = GET_ACTIVE_N(actualNewVal64);
1,039,256✔
659
    *expectedRunning = GET_RUNNING_N(actualNewVal64);
1,039,256✔
660
    return false;
1,039,256✔
661
  }
662
}
663

664
static void *tQueryAutoQWorkerThreadFp(SQueryAutoQWorker *worker) {
196,406,407✔
665
  SQueryAutoQWorkerPool *pool = worker->pool;
196,406,407✔
666
  SQueueInfo             qinfo = {0};
196,416,075✔
667
  void                  *msg = NULL;
196,414,651✔
668
  int32_t                code = 0;
196,415,946✔
669

670
  int32_t ret = taosBlockSIGPIPE();
196,415,946✔
671
  if (ret < 0) {
196,408,421✔
672
    uError("worker:%s:%d failed to block SIGPIPE", pool->name, worker->id);
×
673
  }
674

675
  setThreadName(pool->name);
196,408,421✔
676
  worker->pid = taosGetSelfPthreadId();
196,412,274✔
677
  uDebug("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
196,400,125✔
678

679
  while (1) {
680
    if (taosReadQitemFromQset(pool->qset, (void **)&msg, &qinfo) == 0) {
2,147,483,647✔
681
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, pool->qset,
195,425,209✔
682
            worker->pid);
683
      break;
195,489,284✔
684
    }
685

686
    if (pool->stopNoWaitQueue && pool->exit) {
2,147,483,647✔
687
      uInfo("worker:%s:%d exit, thread:%08" PRId64, pool->name, worker->id, worker->pid);
×
688
      break;
×
689
    }
690

691
    if (qinfo.timestamp != 0) {
2,147,483,647✔
692
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
2,147,483,647✔
693
      if (cost > QUEUE_THRESHOLD) {
2,147,483,647✔
694
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pool->name, cost / QUEUE_THRESHOLD);
3,542,800✔
695
      }
696
    }
697

698
    tQueryAutoQWorkerWaitingCheck(pool);
2,147,483,647✔
699

700
    if (qinfo.fp != NULL) {
2,147,483,647✔
701
      qinfo.workerId = worker->id;
2,147,483,647✔
702
      qinfo.threadNum = pool->num;
2,147,483,647✔
703
      qinfo.workerCb = pool->pCb;
2,147,483,647✔
704
      (*((FItem)qinfo.fp))(&qinfo, msg);
2,147,483,647✔
705
    }
706

707
    taosUpdateItemSize(qinfo.queue, 1);
2,147,483,647✔
708
    if (!tQueryAutoQWorkerTryRecycleWorker(pool, worker)) {
2,147,483,647✔
709
      uDebug("worker:%s:%d exited", pool->name, worker->id);
906,375✔
710
      break;
906,375✔
711
    }
712
  }
713

714
  DestoryThreadLocalRegComp();
196,395,659✔
715
  closeThreadNotificationConn();
196,376,631✔
716

717
  return NULL;
196,378,490✔
718
}
719

720
static bool tQueryAutoQWorkerTrySignalWaitingAfterBlock(void *p) {
2,147,483,647✔
721
  SQueryAutoQWorkerPool *pPool = p;
2,147,483,647✔
722
  bool                   ret = false;
2,147,483,647✔
723
  int32_t                waiting = pPool->waitingAfterBlockN;
2,147,483,647✔
724
  while (waiting > 0) {
2,147,483,647✔
725
    int32_t waitingNew = atomic_val_compare_exchange_32(&pPool->waitingAfterBlockN, waiting, waiting - 1);
2,109✔
726
    if (waitingNew == waiting) {
2,109✔
727
      (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
2,109✔
728
      (void)taosThreadCondSignal(&pPool->waitingAfterBlockCond);
2,109✔
729
      (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
2,109✔
730
      ret = true;
8,400✔
731
      break;
8,400✔
732
    }
733
    waiting = waitingNew;
×
734
  }
735
  return ret;
2,147,483,647✔
736
}
737

738
static bool tQueryAutoQWorkerTrySignalWaitingBeforeProcess(void *p) {
2,147,483,647✔
739
  SQueryAutoQWorkerPool *pPool = p;
2,147,483,647✔
740
  bool                   ret = false;
2,147,483,647✔
741
  int32_t                waiting = pPool->waitingBeforeProcessMsgN;
2,147,483,647✔
742
  while (waiting > 0) {
2,147,483,647✔
743
    int32_t waitingNew = atomic_val_compare_exchange_32(&pPool->waitingBeforeProcessMsgN, waiting, waiting - 1);
434✔
744
    if (waitingNew == waiting) {
434✔
745
      (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
434✔
746
      (void)taosThreadCondSignal(&pPool->waitingBeforeProcessMsgCond);
434✔
747
      (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
434✔
748
      ret = true;
6,344✔
749
      break;
6,344✔
750
    }
751
    waiting = waitingNew;
×
752
  }
753
  return ret;
2,147,483,647✔
754
}
755

756
static bool tQueryAutoQWorkerTryDecActive(void *p, int32_t minActive) {
2,147,483,647✔
757
  SQueryAutoQWorkerPool *pPool = p;
2,147,483,647✔
758
  bool                   ret = false;
2,147,483,647✔
759
  int64_t                val64 = pPool->activeRunningN;
2,147,483,647✔
760
  int32_t                active = GET_ACTIVE_N(val64), running = GET_RUNNING_N(val64);
2,147,483,647✔
761
  while (active > minActive) {
2,147,483,647✔
762
    if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active - 1, &running, running - 1))
292,534,859✔
763
      return true;
292,514,751✔
764
  }
765
  return false;
2,147,483,647✔
766
}
767

768
static void tQueryAutoQWorkerWaitingCheck(SQueryAutoQWorkerPool *pPool) {
2,147,483,647✔
769
  while (1) {
10,412✔
770
    int64_t val64 = pPool->activeRunningN;
2,147,483,647✔
771
    int32_t running = GET_RUNNING_N(val64), active = GET_ACTIVE_N(val64);
2,147,483,647✔
772
    while (running < pPool->num) {
2,147,483,647✔
773
      if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active, &running, running + 1)) {
2,147,483,647✔
774
        return;
2,147,483,647✔
775
      }
776
    }
UNCOV
777
    if (atomicCompareExchangeActive(&pPool->activeRunningN, &active, active - 1)) {
×
778
      break;
434✔
779
    }
780
  }
781
  // to wait for process
782
  (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
434✔
783
  (void)atomic_fetch_add_32(&pPool->waitingBeforeProcessMsgN, 1);
434✔
784
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingBeforeProcessMsgCond, &pPool->waitingBeforeProcessMsgLock);
434✔
785
  // recovered from waiting
786
  (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
434✔
787
  return;
434✔
788
}
789

790
bool tQueryAutoQWorkerTryRecycleWorker(SQueryAutoQWorkerPool *pPool, SQueryAutoQWorker *pWorker) {
2,147,483,647✔
791
  if (tQueryAutoQWorkerTrySignalWaitingAfterBlock(pPool) || tQueryAutoQWorkerTrySignalWaitingBeforeProcess(pPool) ||
2,147,483,647✔
792
      tQueryAutoQWorkerTryDecActive(pPool, pPool->num)) {
2,147,483,647✔
793
    (void)taosThreadMutexLock(&pPool->poolLock);
126,745,796✔
794
    if (pPool->exit) {
126,711,435✔
795
      (void)taosThreadMutexUnlock(&pPool->poolLock);
836✔
796
      return false;
836✔
797
    }
798

799
    SListNode *pNode = listNode(pWorker);
126,710,599✔
800
    SListNode *tNode = tdListPopNode(pPool->workers, pNode);
126,710,599✔
801
    // reclaim some workers
802
    if (pWorker->id >= pPool->maxInUse) {
126,710,599✔
803
      while (listNEles(pPool->exitedWorkers) > pPool->maxInUse - pPool->num) {
217✔
804
        SListNode         *head = tdListPopHead(pPool->exitedWorkers);
×
805
        SQueryAutoQWorker *pWorker = (SQueryAutoQWorker *)head->data;
×
806
        if (pWorker && taosCheckPthreadValid(pWorker->thread)) {
×
807
          (void)taosThreadJoin(pWorker->thread, NULL);
×
808
          taosThreadClear(&pWorker->thread);
×
809
        }
810
        taosMemoryFree(head);
×
811
      }
812
      tdListAppendNode(pPool->exitedWorkers, pNode);
217✔
813
      (void)taosThreadMutexUnlock(&pPool->poolLock);
217✔
814
      return false;
217✔
815
    }
816

817
    // put back to backup pool
818
    tdListAppendNode(pPool->backupWorkers, pNode);
126,710,382✔
819
    (void)taosThreadMutexUnlock(&pPool->poolLock);
126,710,382✔
820

821
    // start to wait at backup cond
822
    (void)taosThreadMutexLock(&pPool->backupLock);
126,710,382✔
823
    (void)atomic_fetch_add_32(&pPool->backupNum, 1);
126,710,382✔
824
    if (!pPool->exit) (void)taosThreadCondWait(&pPool->backupCond, &pPool->backupLock);
126,710,382✔
825
    (void)taosThreadMutexUnlock(&pPool->backupLock);
126,710,382✔
826

827
    // recovered from backup
828
    (void)taosThreadMutexLock(&pPool->poolLock);
126,710,122✔
829
    if (pPool->exit) {
126,710,382✔
830
      (void)taosThreadMutexUnlock(&pPool->poolLock);
905,551✔
831
      return false;
905,551✔
832
    }
833
    SListNode *tNode1 = tdListPopNode(pPool->backupWorkers, pNode);
125,804,831✔
834
    tdListAppendNode(pPool->workers, pNode);
125,804,831✔
835
    (void)taosThreadMutexUnlock(&pPool->poolLock);
125,804,831✔
836

837
    return true;
125,804,831✔
838
  } else {
839
    (void)atomicFetchSubRunning(&pPool->activeRunningN, 1);
2,147,483,647✔
840
    return true;
2,147,483,647✔
841
  }
842
}
843

844
int32_t tQueryAutoQWorkerInit(SQueryAutoQWorkerPool *pool) {
3,914,129✔
845
  int32_t code;
846

847
  pool->exit = false;
3,914,129✔
848

849
  (void)taosThreadMutexInit(&pool->poolLock, NULL);
3,914,129✔
850
  (void)taosThreadMutexInit(&pool->backupLock, NULL);
3,914,129✔
851
  (void)taosThreadMutexInit(&pool->waitingAfterBlockLock, NULL);
3,914,129✔
852
  (void)taosThreadMutexInit(&pool->waitingBeforeProcessMsgLock, NULL);
3,914,129✔
853

854
  (void)taosThreadCondInit(&pool->waitingBeforeProcessMsgCond, NULL);
3,914,129✔
855
  (void)taosThreadCondInit(&pool->waitingAfterBlockCond, NULL);
3,914,129✔
856
  (void)taosThreadCondInit(&pool->backupCond, NULL);
3,914,129✔
857

858
  code = taosOpenQset(&pool->qset);
3,914,129✔
859
  if (code) return terrno = code;
3,914,129✔
860
  pool->workers = tdListNew(sizeof(SQueryAutoQWorker));
3,914,129✔
861
  if (!pool->workers) return terrno;
3,914,129✔
862
  pool->backupWorkers = tdListNew(sizeof(SQueryAutoQWorker));
3,914,129✔
863
  if (!pool->backupWorkers) return terrno;
3,914,129✔
864
  pool->exitedWorkers = tdListNew(sizeof(SQueryAutoQWorker));
3,914,129✔
865
  if (!pool->exitedWorkers) return terrno;
3,914,129✔
866
  pool->maxInUse = pool->max * 2 + 2;
3,914,129✔
867

868
  if (!pool->pCb) {
3,914,129✔
869
    pool->pCb = taosMemoryCalloc(1, sizeof(SQueryAutoQWorkerPoolCB));
3,914,129✔
870
    if (!pool->pCb) return terrno;
3,914,129✔
871
    pool->pCb->pPool = pool;
3,914,129✔
872
    pool->pCb->beforeBlocking = tQueryAutoQWorkerBeforeBlocking;
3,914,129✔
873
    pool->pCb->afterRecoverFromBlocking = tQueryAutoQWorkerRecoverFromBlocking;
3,914,129✔
874
  }
875
  return TSDB_CODE_SUCCESS;
3,914,129✔
876
}
877

878
void tQueryAutoQWorkerCleanup(SQueryAutoQWorkerPool *pPool) {
3,914,172✔
879
  (void)taosThreadMutexLock(&pPool->poolLock);
3,914,172✔
880
  pPool->exit = true;
3,914,172✔
881

882
  int32_t size = 0;
3,914,172✔
883
  if (pPool->workers) {
3,914,172✔
884
    size += listNEles(pPool->workers);
3,914,129✔
885
  }
886
  if (pPool->backupWorkers) {
3,914,172✔
887
    size += listNEles(pPool->backupWorkers);
3,914,129✔
888
  }
889
  if (pPool->qset) {
3,914,172✔
890
    for (int32_t i = 0; i < size; ++i) {
200,333,410✔
891
      taosQsetThreadResume(pPool->qset);
196,419,281✔
892
    }
893
  }
894
  (void)taosThreadMutexUnlock(&pPool->poolLock);
3,914,172✔
895

896
  (void)taosThreadMutexLock(&pPool->backupLock);
3,914,172✔
897
  (void)taosThreadCondBroadcast(&pPool->backupCond);
3,914,172✔
898
  (void)taosThreadMutexUnlock(&pPool->backupLock);
3,914,172✔
899

900
  (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
3,914,172✔
901
  (void)taosThreadCondBroadcast(&pPool->waitingAfterBlockCond);
3,914,172✔
902
  (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
3,914,172✔
903

904
  (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
3,914,172✔
905
  (void)taosThreadCondBroadcast(&pPool->waitingBeforeProcessMsgCond);
3,914,172✔
906
  (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
3,914,172✔
907

908
  int32_t            idx = 0;
3,914,172✔
909
  SQueryAutoQWorker *worker = NULL;
3,914,172✔
910
  while (pPool->workers) {
199,427,902✔
911
    (void)taosThreadMutexLock(&pPool->poolLock);
199,427,859✔
912
    if (listNEles(pPool->workers) <= 0) {
199,427,859✔
913
      (void)taosThreadMutexUnlock(&pPool->poolLock);
3,914,129✔
914
      break;
3,914,129✔
915
    }
916
    SListNode *pNode = tdListPopHead(pPool->workers);
195,513,730✔
917
    uDebug("0free worker node:%p, prev:%p, next:%p", pNode, TD_DLIST_NODE_PREV(pNode), TD_DLIST_NODE_NEXT(pNode));
195,513,730✔
918
    worker = pNode ? (SQueryAutoQWorker *)pNode->data : NULL;
195,513,730✔
919
    (void)taosThreadMutexUnlock(&pPool->poolLock);
195,513,730✔
920
    if (worker && taosCheckPthreadValid(worker->thread)) {
195,513,730✔
921
      (void)taosThreadJoin(worker->thread, NULL);
195,513,730✔
922
      taosThreadClear(&worker->thread);
195,513,730✔
923
    }
924
    uDebug("free worker node:%p, prev:%p, next:%p", pNode, TD_DLIST_NODE_PREV(pNode), TD_DLIST_NODE_NEXT(pNode));
195,513,730✔
925

926
    taosMemoryFree(pNode);
195,513,730✔
927
  }
928

929
  while (pPool->backupWorkers) {
4,819,723✔
930
    (void)taosThreadMutexLock(&pPool->poolLock);
4,819,680✔
931
    if (listNEles(pPool->backupWorkers) <= 0) {
4,819,680✔
932
      (void)taosThreadMutexUnlock(&pPool->poolLock);
3,914,129✔
933
      break;
3,914,129✔
934
    }
935
    uDebug("backupworker head:%p, prev:%p, next:%p", TD_DLIST_HEAD(pPool->backupWorkers), 
905,551✔
936
        TD_DLIST_HEAD(pPool->backupWorkers) ? TD_DLIST_NODE_PREV(TD_DLIST_HEAD(pPool->backupWorkers)) : NULL, 
937
        TD_DLIST_HEAD(pPool->backupWorkers) ? TD_DLIST_NODE_NEXT(TD_DLIST_HEAD(pPool->backupWorkers)) : NULL);
938
    SListNode *pNode = tdListPopHead(pPool->backupWorkers);
905,551✔
939
    worker = pNode ? (SQueryAutoQWorker *)pNode->data : NULL;
905,551✔
940
    (void)taosThreadMutexUnlock(&pPool->poolLock);
905,551✔
941

942
    if (worker && taosCheckPthreadValid(worker->thread)) {
905,551✔
943
      (void)taosThreadJoin(worker->thread, NULL);
905,551✔
944
      taosThreadClear(&worker->thread);
905,551✔
945
    }
946
    taosMemoryFree(pNode);
905,551✔
947
  }
948

949
  while (pPool->exitedWorkers) {
3,914,389✔
950
    (void)taosThreadMutexLock(&pPool->poolLock);
3,914,346✔
951
    if (listNEles(pPool->exitedWorkers) == 0) {
3,914,346✔
952
      (void)taosThreadMutexUnlock(&pPool->poolLock);
3,914,129✔
953
      break;
3,914,129✔
954
    }
955

956
    SListNode *pNode = tdListPopHead(pPool->exitedWorkers);
217✔
957
    worker = pNode ? (SQueryAutoQWorker *)pNode->data : NULL;
217✔
958
    (void)taosThreadMutexUnlock(&pPool->poolLock);
217✔
959

960
    if (worker && taosCheckPthreadValid(worker->thread)) {
217✔
961
      (void)taosThreadJoin(worker->thread, NULL);
217✔
962
      taosThreadClear(&worker->thread);
217✔
963
    }
964
    taosMemoryFree(pNode);
217✔
965
  }
966

967
  (void)taosThreadMutexLock(&pPool->poolLock);
3,914,172✔
968
  pPool->workers = tdListFree(pPool->workers);
3,914,172✔
969
  pPool->backupWorkers = tdListFree(pPool->backupWorkers);
3,914,172✔
970
  pPool->exitedWorkers = tdListFree(pPool->exitedWorkers);
3,914,172✔
971
  taosMemoryFree(pPool->pCb);
3,914,172✔
972
  (void)taosThreadMutexUnlock(&pPool->poolLock);
3,914,172✔
973

974
  (void)taosThreadMutexDestroy(&pPool->poolLock);
3,914,172✔
975
  (void)taosThreadMutexDestroy(&pPool->backupLock);
3,914,172✔
976
  (void)taosThreadMutexDestroy(&pPool->waitingAfterBlockLock);
3,914,172✔
977
  (void)taosThreadMutexDestroy(&pPool->waitingBeforeProcessMsgLock);
3,914,172✔
978

979
  (void)taosThreadCondDestroy(&pPool->backupCond);
3,914,172✔
980
  (void)taosThreadCondDestroy(&pPool->waitingAfterBlockCond);
3,914,172✔
981
  (void)taosThreadCondDestroy(&pPool->waitingBeforeProcessMsgCond);
3,914,172✔
982
  taosCloseQset(pPool->qset);
3,914,172✔
983
}
3,914,172✔
984

985
STaosQueue *tQueryAutoQWorkerAllocQueue(SQueryAutoQWorkerPool *pool, void *ahandle, FItem fp) {
11,714,203✔
986
  STaosQueue *queue;
11,683,126✔
987
  int32_t     code = taosOpenQueue(&queue);
11,714,203✔
988
  if (code) {
11,714,203✔
989
    terrno = code;
×
990
    return NULL;
×
991
  }
992

993
  (void)taosThreadMutexLock(&pool->poolLock);
11,714,203✔
994
  taosSetQueueFp(queue, fp, NULL);
11,714,102✔
995
  code = taosAddIntoQset(pool->qset, queue, ahandle);
11,714,203✔
996
  if (code) {
11,714,203✔
997
    taosCloseQueue(queue);
×
998
    queue = NULL;
×
999
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
1000
    return NULL;
×
1001
  }
1002
  SQueryAutoQWorker  worker = {0};
11,714,203✔
1003
  SQueryAutoQWorker *pWorker = NULL;
11,714,203✔
1004

1005
  // spawn a thread to process queue
1006
  if (pool->num < pool->max) {
11,714,203✔
1007
    do {
1008
      worker.id = listNEles(pool->workers);
195,512,894✔
1009
      worker.backupIdx = -1;
195,512,894✔
1010
      worker.pool = pool;
195,512,894✔
1011
      SListNode *pNode = tdListAdd(pool->workers, &worker);
195,512,894✔
1012
      if (!pNode) {
195,512,894✔
1013
        taosCloseQueue(queue);
×
1014
        queue = NULL;
×
1015
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1016
        break;
×
1017
      }
1018
      pWorker = (SQueryAutoQWorker *)pNode->data;
195,512,894✔
1019

1020
      TdThreadAttr thAttr;
194,135,958✔
1021
      (void)taosThreadAttrInit(&thAttr);
195,512,894✔
1022
      (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
195,512,894✔
1023

1024
      if (taosThreadCreate(&pWorker->thread, &thAttr, (ThreadFp)tQueryAutoQWorkerThreadFp, pWorker) != 0) {
195,512,894✔
1025
        taosCloseQueue(queue);
×
1026
        queue = NULL;
×
1027
        break;
×
1028
      }
1029

1030
      (void)taosThreadAttrDestroy(&thAttr);
195,512,894✔
1031
      pool->num++;
195,512,894✔
1032
      (void)atomicFetchAddActive(&pool->activeRunningN, 1);
195,512,894✔
1033
      uInfo("worker:%s:%d is launched, total:%d", pool->name, pWorker->id, pool->num);
195,512,894✔
1034
    } while (pool->num < pool->min);
195,512,894✔
1035
  }
1036

1037
  (void)taosThreadMutexUnlock(&pool->poolLock);
11,714,203✔
1038
  uInfo("worker:%s, queue:%p is allocated, ahandle:%p", pool->name, queue, ahandle);
11,714,203✔
1039

1040
  return queue;
11,711,247✔
1041
}
1042

1043
void tQueryAutoQWorkerFreeQueue(SQueryAutoQWorkerPool *pPool, STaosQueue *pQ) { taosCloseQueue(pQ); }
10,156,288✔
1044

1045
static int32_t tQueryAutoQWorkerAddWorker(SQueryAutoQWorkerPool *pool) {
126,707,711✔
1046
  // try backup pool
1047
  int32_t backup = pool->backupNum;
126,707,711✔
1048
  while (backup > 0) {
126,733,181✔
1049
    int32_t backupNew = atomic_val_compare_exchange_32(&pool->backupNum, backup, backup - 1);
125,826,577✔
1050
    if (backupNew == backup) {
125,829,734✔
1051
      (void)taosThreadCondSignal(&pool->backupCond);
125,804,264✔
1052
      return TSDB_CODE_SUCCESS;
125,804,831✔
1053
    }
1054
    backup = backupNew;
25,470✔
1055
  }
1056
  // backup pool is empty, create new
1057
  SQueryAutoQWorker *pWorker = NULL;
906,604✔
1058
  SQueryAutoQWorker  worker = {0};
906,604✔
1059
  worker.pool = pool;
906,604✔
1060
  worker.backupIdx = -1;
906,604✔
1061
  (void)taosThreadMutexLock(&pool->poolLock);
906,604✔
1062
  if (pool->exit) {
906,604✔
1063
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
1064
    return TSDB_CODE_SUCCESS;
×
1065
  }
1066
  worker.id = listNEles(pool->workers);
906,604✔
1067
  SListNode *pNode = tdListAdd(pool->workers, &worker);
906,604✔
1068
  if (!pNode) {
906,604✔
1069
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
1070
    return terrno;
×
1071
  }
1072
  (void)taosThreadMutexUnlock(&pool->poolLock);
906,604✔
1073
  pWorker = (SQueryAutoQWorker *)pNode->data;
906,604✔
1074

1075
  TdThreadAttr thAttr;
905,321✔
1076
  (void)taosThreadAttrInit(&thAttr);
906,604✔
1077
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
906,604✔
1078

1079
  if (taosThreadCreate(&pWorker->thread, &thAttr, (ThreadFp)tQueryAutoQWorkerThreadFp, pWorker) != 0) {
906,604✔
1080
    uError("create queryAutoWorker thread failed, error:%s", tstrerror(terrno));
×
1081
    return terrno;
×
1082
  }
1083
  (void)taosThreadAttrDestroy(&thAttr);
906,604✔
1084

1085
  return TSDB_CODE_SUCCESS;
906,604✔
1086
}
1087

1088
static int32_t tQueryAutoQWorkerBeforeBlocking(void *p) {
292,513,831✔
1089
  SQueryAutoQWorkerPool *pPool = p;
292,513,831✔
1090
  if (tQueryAutoQWorkerTrySignalWaitingAfterBlock(p) || tQueryAutoQWorkerTrySignalWaitingBeforeProcess(p) ||
585,022,154✔
1091
      tQueryAutoQWorkerTryDecActive(p, pPool->num)) {
292,508,516✔
1092
  } else {
1093
    int32_t code = tQueryAutoQWorkerAddWorker(pPool);
126,704,112✔
1094
    if (code != TSDB_CODE_SUCCESS) {
126,711,435✔
1095
      return code;
×
1096
    }
1097
    (void)atomicFetchSubRunning(&pPool->activeRunningN, 1);
126,711,435✔
1098
  }
1099

1100
  return TSDB_CODE_SUCCESS;
292,516,731✔
1101
}
1102

1103
static int32_t tQueryAutoQWorkerRecoverFromBlocking(void *p) {
292,515,908✔
1104
  SQueryAutoQWorkerPool *pPool = p;
292,515,908✔
1105
  int64_t                val64 = pPool->activeRunningN;
292,515,908✔
1106
  int32_t                running = GET_RUNNING_N(val64), active = GET_ACTIVE_N(val64);
292,516,324✔
1107
  while (running < pPool->num) {
292,526,770✔
1108
    if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active + 1, &running, running + 1)) {
292,524,661✔
1109
      return TSDB_CODE_SUCCESS;
292,515,185✔
1110
    }
1111
  }
1112
  (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
1,892✔
1113
  (void)atomic_fetch_add_32(&pPool->waitingAfterBlockN, 1);
2,109✔
1114
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingAfterBlockCond, &pPool->waitingAfterBlockLock);
2,109✔
1115
  (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
2,109✔
1116
  if (pPool->exit) return TSDB_CODE_QRY_QWORKER_QUIT;
2,109✔
1117
  return TSDB_CODE_SUCCESS;
2,109✔
1118
}
1119

1120
int32_t tDispatchWorkerInit(SDispatchWorkerPool *pPool) {
1,213,027✔
1121
  int32_t code = 0;
1,213,027✔
1122
  pPool->num = 0;
1,213,027✔
1123
  pPool->pWorkers = taosMemCalloc(pPool->max, sizeof(SDispatchWorker));
1,213,027✔
1124
  if (!pPool->pWorkers) return terrno;
1,213,027✔
1125
  (void)taosThreadMutexInit(&pPool->poolLock, NULL);
1,213,027✔
1126
  return code;
1,213,027✔
1127
}
1128

1129
static void *tDispatchWorkerThreadFp(SDispatchWorker *pWorker) {
8,420,079✔
1130
  SDispatchWorkerPool *pPool = pWorker->pool;
8,420,079✔
1131
  SQueueInfo qinfo = {0};
8,425,073✔
1132
  int32_t code = 0;
8,425,073✔
1133
  void *msg = NULL;
8,425,073✔
1134

1135
  int32_t ret = taosBlockSIGPIPE();
8,425,073✔
1136
  if (ret < 0) {
8,423,946✔
1137
    uError("worker:%s:%d failed to block SIGPIPE", pPool->name, pWorker->id);
×
1138
  }
1139

1140
  setThreadName(pPool->name);
8,423,946✔
1141
  pWorker->pid = taosGetSelfPthreadId();
8,425,172✔
1142
  uInfo("worker:%s:%d is running, thread:%d", pPool->name, pWorker->id, pWorker->pid);
8,424,449✔
1143

1144
  while (1) {
1145
    if (taosReadQitemFromQset(pWorker->qset, (void **)&msg, &qinfo) == 0) {
110,602,940✔
1146
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%d", pPool->name, pWorker->id,
8,409,435✔
1147
            pWorker->qset, pWorker->pid);
1148
      break;
8,425,172✔
1149
    }
1150

1151
    if (qinfo.timestamp != 0) {
102,179,740✔
1152
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
102,182,033✔
1153
      if (cost > QUEUE_THRESHOLD) {
102,182,033✔
1154
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pPool->name, cost / QUEUE_THRESHOLD);
262,564✔
1155
      }
1156
    }
1157

1158
    if (qinfo.fp != NULL) {
102,180,182✔
1159
      qinfo.workerId = pWorker->id;
102,181,377✔
1160
      qinfo.threadNum = pPool->num;
102,182,025✔
1161
      (*((FItem)qinfo.fp))(&qinfo, msg);
102,183,388✔
1162
    }
1163
  }
1164
  DestoryThreadLocalRegComp();
8,425,172✔
1165
  closeThreadNotificationConn();
8,425,172✔
1166
  return NULL;
8,425,172✔
1167
}
1168

1169
int32_t tDispatchWorkerAllocQueue(SDispatchWorkerPool *pPool, void *ahandle, FItem fp, DispatchFp dispatchFp) {
1,213,027✔
1170
  int32_t code = 0;
1,213,027✔
1171
  SDispatchWorker* pWorker = NULL;
1,213,027✔
1172
  (void)taosThreadMutexLock(&pPool->poolLock);
1,213,027✔
1173
  pPool->dispatchFp = dispatchFp;
1,213,027✔
1174
  for (int32_t i = pPool->num; i < pPool->max; ++i) {
9,638,199✔
1175
    pWorker = pPool->pWorkers + i;
8,425,172✔
1176
    pWorker->id = pPool->num;
8,425,172✔
1177
    pWorker->pool = pPool;
8,425,172✔
1178
    pPool->num++;
8,425,172✔
1179
    code = taosOpenQset(&pWorker->qset);
8,425,172✔
1180
    if (code != 0) break;
8,425,172✔
1181
    code = taosOpenQueue(&pWorker->queue);
8,425,172✔
1182
    if (code != 0) break;
8,425,172✔
1183
    taosSetQueueFp(pWorker->queue, fp, ahandle);
8,425,172✔
1184
    code = taosAddIntoQset(pWorker->qset, pWorker->queue, ahandle);
8,425,172✔
1185
    if (code != 0) break;
8,425,172✔
1186

1187
    TdThreadAttr thAttr;
8,411,302✔
1188
    (void)taosThreadAttrInit(&thAttr);
8,425,172✔
1189
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
8,425,172✔
1190

1191
    if (taosThreadCreate(&pWorker->thread, &thAttr, (ThreadFp)tDispatchWorkerThreadFp, pWorker) != 0) {
8,425,172✔
1192
      code = terrno;
×
1193
      (void)taosThreadAttrDestroy(&thAttr);
×
1194
      break;
×
1195
    }
1196
    (void)taosThreadAttrDestroy(&thAttr);
8,425,172✔
1197
    uInfo("worker:%s:%d is launched, threadId:%" PRId64 ", total:%d", pPool->name, pWorker->id, taosGetPthreadId(pWorker->thread), pPool->num);
8,425,172✔
1198
  }
1199

1200
  (void)taosThreadMutexUnlock(&pPool->poolLock);
1,213,027✔
1201
  if (code == 0) uInfo("worker:%s, queue:%p is allocated, ahandle:%p", pPool->name, pWorker->queue, ahandle);
1,213,027✔
1202
  return code;
1,213,027✔
1203
}
1204

1205
static void tDispatchWorkerFreeQueue(SDispatchWorkerPool *pPool) {
1,213,027✔
1206
  (void)taosThreadMutexLock(&pPool->poolLock);
1,213,027✔
1207
  if (!pPool->pWorkers) return;
1,213,027✔
1208
  for (int32_t i = 0; i < pPool->num; ++i) {
9,638,199✔
1209
    SDispatchWorker *pWorker = pPool->pWorkers + i;
8,425,172✔
1210
    if (pWorker->queue) {
8,425,172✔
1211
      taosCloseQueue(pWorker->queue);
8,425,172✔
1212
      pWorker->queue = NULL;
8,425,172✔
1213
    }
1214
    if (pWorker->qset) {
8,425,172✔
1215
      taosCloseQset(pWorker->qset);
8,425,172✔
1216
      pWorker->qset = NULL;
8,425,172✔
1217
    }
1218
  }
1219
  (void)taosThreadMutexUnlock(&pPool->poolLock);
1,213,027✔
1220
}
1221

1222
void tDispatchWorkerCleanup(SDispatchWorkerPool *pPool) {
1,213,027✔
1223
  (void)taosThreadMutexLock(&pPool->poolLock);
1,213,027✔
1224
  pPool->exit = true;
1,213,027✔
1225
  if (pPool->pWorkers) {
1,213,027✔
1226
    for (int32_t i = 0; i < pPool->num; ++i) {
9,638,199✔
1227
      SDispatchWorker *pWorker = pPool->pWorkers + i;
8,425,172✔
1228
      if (pWorker->qset) {
8,425,172✔
1229
        taosQsetThreadResume(pWorker->qset);
8,425,172✔
1230
      }
1231
    }
1232
  }
1233
  (void)taosThreadMutexUnlock(&pPool->poolLock);
1,213,027✔
1234

1235
  if (pPool->pWorkers) {
1,213,027✔
1236
    for (int32_t i = 0; i < pPool->num; ++i) {
9,638,199✔
1237
      SDispatchWorker *pWorker = pPool->pWorkers + i;
8,425,172✔
1238
      if (taosCheckPthreadValid(pWorker->thread)) {
8,425,172✔
1239
        (void)taosThreadJoin(pWorker->thread, NULL);
8,425,172✔
1240
        taosThreadClear(&pWorker->thread);
8,425,172✔
1241
      }
1242
    }
1243
  }
1244
  tDispatchWorkerFreeQueue(pPool);
1,213,027✔
1245
  taosMemoryFreeClear(pPool->pWorkers);
1,213,027✔
1246
  (void)taosThreadMutexDestroy(&pPool->poolLock);
1,213,027✔
1247
}
1,213,027✔
1248

1249
int32_t tAddTaskIntoDispatchWorkerPool(SDispatchWorkerPool *pPool, void *pMsg) {
102,181,451✔
1250
  int32_t code = 0;
102,181,451✔
1251
  int32_t idx = 0;
102,181,451✔
1252
  SDispatchWorker *pWorker = NULL;
102,182,420✔
1253
  (void)taosThreadMutexLock(&pPool->poolLock);
102,182,420✔
1254
  code = pPool->dispatchFp(pPool, pMsg, &idx);
102,185,958✔
1255
  if (code == 0) {
102,185,958✔
1256
    pWorker = pPool->pWorkers + idx;
102,185,958✔
1257
    if (pWorker->queue) {
102,185,958✔
1258
      code = taosWriteQitem(pWorker->queue, pMsg);
102,185,958✔
1259
    } else {
1260
      code = TSDB_CODE_INTERNAL_ERROR;
×
1261
    }
1262
  }
1263
  (void)taosThreadMutexUnlock(&pPool->poolLock);
102,185,958✔
1264
  if (code != 0) {
102,185,958✔
1265
    uError("worker:%s, failed to add task into dispatch worker pool, code:%d", pPool->name, code);
×
1266
  } else {
1267
    uDebug("msg %p dispatch to the %dth worker, threadId:%" PRId64, pMsg, idx, taosGetPthreadId(pWorker->thread));
102,185,958✔
1268
  }
1269
  return code;
102,186,711✔
1270
}
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