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

taosdata / TDengine / #5006

29 Mar 2026 04:32AM UTC coverage: 72.274% (+0.1%) from 72.152%
#5006

push

travis-ci

web-flow
refactor: do some internal refactor for TDgpt. (#34955)

253711 of 351039 relevant lines covered (72.27%)

131490495.89 hits per line

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

73.32
/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,548,558✔
30
  int32_t code = taosOpenQset(&pool->qset);
5,548,558✔
31
  if (code) return code;
5,548,558✔
32

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

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

41
  for (int32_t i = 0; i < pool->max; ++i) {
36,989,799✔
42
    SQueueWorker *worker = pool->workers + i;
31,441,241✔
43
    worker->id = i;
31,441,241✔
44
    worker->pool = pool;
31,441,241✔
45
  }
46

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

51
void tQWorkerCleanup(SQWorkerPool *pool) {
5,548,558✔
52
  for (int32_t i = 0; i < pool->max; ++i) {
36,989,799✔
53
    SQueueWorker *worker = pool->workers + i;
31,441,241✔
54
    if (taosCheckPthreadValid(worker->thread)) {
31,441,241✔
55
      taosQsetThreadResume(pool->qset);
31,441,241✔
56
    }
57
  }
58

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

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

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

76
static void *tQWorkerThreadFp(SQueueWorker *worker) {
31,440,107✔
77
  SQWorkerPool *pool = worker->pool;
31,440,107✔
78
  SQueueInfo    qinfo = {0};
31,440,405✔
79
  void         *msg = NULL;
31,440,810✔
80
  int32_t       code = 0;
31,440,511✔
81

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

87
  setThreadName(pool->name);
31,439,714✔
88
  worker->pid = taosGetSelfPthreadId();
31,440,370✔
89
  uInfo("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
31,435,712✔
90

91
  while (1) {
92
    if (taosReadQitemFromQset(pool->qset, (void **)&msg, &qinfo) == 0) {
464,181,167✔
93
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, pool->qset,
31,429,467✔
94
            worker->pid);
95
      break;
31,440,781✔
96
    }
97

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

105
    if (qinfo.fp != NULL) {
432,744,925✔
106
      qinfo.workerId = worker->id;
432,741,324✔
107
      qinfo.threadNum = pool->num;
432,741,994✔
108
      (*((FItem)qinfo.fp))(&qinfo, msg);
432,743,587✔
109
    }
110

111
    taosUpdateItemSize(qinfo.queue, 1);
432,743,907✔
112
  }
113

114
  DestoryThreadLocalRegComp();
31,440,781✔
115

116
  return NULL;
31,440,308✔
117
}
118

119
STaosQueue *tQWorkerAllocQueue(SQWorkerPool *pool, void *ahandle, FItem fp) {
5,548,558✔
120
  int32_t     code;
121
  STaosQueue *queue;
5,529,088✔
122

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

129
  (void)taosThreadMutexLock(&pool->mutex);
5,548,558✔
130
  taosSetQueueFp(queue, fp, NULL);
5,548,558✔
131
  code = taosAddIntoQset(pool->qset, queue, ahandle);
5,548,558✔
132
  if (code) {
5,548,558✔
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,548,558✔
141
    do {
142
      SQueueWorker *worker = pool->workers + pool->num;
31,441,241✔
143

144
      TdThreadAttr thAttr;
31,351,172✔
145
      (void)taosThreadAttrInit(&thAttr);
31,441,241✔
146
      (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
31,441,241✔
147

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

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

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

164
  return queue;
5,548,558✔
165
}
166

167
void tQWorkerFreeQueue(SQWorkerPool *pool, STaosQueue *queue) {
5,548,558✔
168
  uInfo("worker:%s, queue:%p is freed", pool->name, queue);
5,548,558✔
169
  taosCloseQueue(queue);
5,548,558✔
170
}
5,548,558✔
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) {
18,164,132✔
337
  pool->nextId = 0;
18,164,132✔
338
  pool->workers = taosMemoryCalloc(pool->max, sizeof(SWWorker));
18,164,132✔
339
  if (pool->workers == NULL) {
18,164,456✔
340
    return terrno;
×
341
  }
342

343
  (void)taosThreadMutexInit(&pool->mutex, NULL);
18,164,456✔
344

345
  for (int32_t i = 0; i < pool->max; ++i) {
42,435,243✔
346
    SWWorker *worker = pool->workers + i;
24,270,787✔
347
    worker->id = i;
24,270,356✔
348
    worker->qall = NULL;
24,270,787✔
349
    worker->qset = NULL;
24,270,787✔
350
    worker->pool = pool;
24,270,787✔
351
  }
352

353
  uInfo("worker:%s is initialized, max:%d", pool->name, pool->max);
18,164,414✔
354
  return 0;
18,164,456✔
355
}
356

357
void tWWorkerCleanup(SWWorkerPool *pool) {
18,164,456✔
358
  for (int32_t i = 0; i < pool->max; ++i) {
42,434,774✔
359
    SWWorker *worker = pool->workers + i;
24,269,238✔
360
    if (taosCheckPthreadValid(worker->thread)) {
24,270,787✔
361
      if (worker->qset) {
20,392,255✔
362
        taosQsetThreadResume(worker->qset);
20,392,255✔
363
      }
364
    }
365
  }
366

367
  for (int32_t i = 0; i < pool->max; ++i) {
42,435,243✔
368
    SWWorker *worker = pool->workers + i;
24,270,787✔
369
    if (taosCheckPthreadValid(worker->thread)) {
24,270,787✔
370
      uInfo("worker:%s:%d is stopping", pool->name, worker->id);
20,391,877✔
371
      (void)taosThreadJoin(worker->thread, NULL);
20,391,877✔
372
      taosThreadClear(&worker->thread);
20,392,255✔
373
      taosFreeQall(worker->qall);
20,392,255✔
374
      taosCloseQset(worker->qset);
20,392,255✔
375
      uInfo("worker:%s:%d is stopped", pool->name, worker->id);
20,392,255✔
376
    }
377
  }
378

379
  taosMemoryFreeClear(pool->workers);
18,164,456✔
380
  (void)taosThreadMutexDestroy(&pool->mutex);
18,164,456✔
381

382
  uInfo("worker:%s is closed", pool->name);
18,164,456✔
383
}
18,164,456✔
384

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

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

397
  setThreadName(pool->name);
20,386,260✔
398
  worker->pid = taosGetSelfPthreadId();
20,392,255✔
399
  uInfo("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
20,390,350✔
400

401
  while (1) {
402
    numOfMsgs = taosReadAllQitemsFromQset(worker->qset, worker->qall, &qinfo);
2,062,018,396✔
403
    if (numOfMsgs == 0) {
2,061,899,017✔
404
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, worker->qset,
20,386,754✔
405
            worker->pid);
406
      break;
20,392,255✔
407
    }
408

409
    if (qinfo.timestamp != 0) {
2,041,512,263✔
410
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
2,041,408,115✔
411
      if (cost > QUEUE_THRESHOLD) {
2,041,408,115✔
412
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pool->name, cost / QUEUE_THRESHOLD);
18,330✔
413
      }
414
    }
415

416
    if (qinfo.fp != NULL) {
2,041,406,461✔
417
      qinfo.workerId = worker->id;
2,041,470,089✔
418
      qinfo.threadNum = pool->num;
2,041,493,450✔
419
      (*((FItems)qinfo.fp))(&qinfo, worker->qall, numOfMsgs);
2,041,507,911✔
420
    }
421
    taosUpdateItemSize(qinfo.queue, numOfMsgs);
2,041,474,223✔
422
  }
423

424
  return NULL;
20,392,255✔
425
}
426

427
STaosQueue *tWWorkerAllocQueue(SWWorkerPool *pool, void *ahandle, FItems fp) {
21,805,272✔
428
  (void)taosThreadMutexLock(&pool->mutex);
21,805,272✔
429
  SWWorker   *worker = pool->workers + pool->nextId;
21,805,272✔
430
  int32_t     code = -1;
21,805,272✔
431
  STaosQueue *queue;
21,780,616✔
432

433
  code = taosOpenQueue(&queue);
21,805,272✔
434
  if (code) goto _OVER;
21,805,272✔
435

436
  taosSetQueueFp(queue, NULL, fp);
21,805,272✔
437
  if (worker->qset == NULL) {
21,805,272✔
438
    code = taosOpenQset(&worker->qset);
20,392,255✔
439
    if (code) goto _OVER;
20,392,255✔
440

441
    code = taosAddIntoQset(worker->qset, queue, ahandle);
20,392,255✔
442
    if (code) goto _OVER;
20,392,255✔
443
    code = taosAllocateQall(&worker->qall);
20,392,255✔
444
    if (code) goto _OVER;
20,392,043✔
445

446
    TdThreadAttr thAttr;
20,370,211✔
447
    (void)taosThreadAttrInit(&thAttr);
20,392,043✔
448
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
20,391,792✔
449
    code = taosThreadCreate(&worker->thread, &thAttr, (ThreadFp)tWWorkerThreadFp, worker);
20,391,635✔
450
    if ((code)) goto _OVER;
20,392,255✔
451

452
    uInfo("worker:%s:%d is launched, max:%d", pool->name, worker->id, pool->max);
20,392,255✔
453
    pool->nextId = (pool->nextId + 1) % pool->max;
20,392,294✔
454

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

464
_OVER:
21,805,272✔
465
  (void)taosThreadMutexUnlock(&pool->mutex);
21,805,272✔
466

467
  if (code) {
21,805,272✔
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);
46,244,447✔
475

476
    taosQueueSetThreadId(queue, worker->pid);
21,802,572✔
477
    uInfo("worker:%s, queue:%p is allocated, ahandle:%p thread:%08" PRId64, pool->name, queue, ahandle, worker->pid);
21,802,886✔
478
    return queue;
21,805,272✔
479
  }
480
}
481

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

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

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

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

523
      code = tQueryAutoQWorkerInit(pPool);
989,925✔
524
      if (code) return code;
989,925✔
525

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

537
void tSingleWorkerCleanup(SSingleWorker *pWorker) {
6,538,483✔
538
  if (pWorker->queue == NULL) return;
6,538,483✔
539
  if (!pWorker->stopNoWaitQueue) {
6,538,483✔
540
    while (!taosQueueEmpty(pWorker->queue)) {
6,654,211✔
541
      taosMsleep(10);
186,748✔
542
    }
543
  }
544

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

561
int32_t tMultiWorkerInit(SMultiWorker *pWorker, const SMultiWorkerCfg *pCfg) {
17,078,936✔
562
  SWWorkerPool *pPool = &pWorker->pool;
17,078,936✔
563
  pPool->name = pCfg->name;
17,078,936✔
564
  pPool->max = pCfg->max;
17,078,936✔
565

566
  int32_t code = tWWorkerInit(pPool);
17,078,936✔
567
  if (code) return code;
17,078,936✔
568

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

574
  pWorker->name = pCfg->name;
17,078,936✔
575
  return 0;
17,078,936✔
576
}
577

578
void tMultiWorkerCleanup(SMultiWorker *pWorker) {
17,078,936✔
579
  if (pWorker->queue == NULL) return;
17,078,936✔
580

581
  while (!taosQueueEmpty(pWorker->queue)) {
19,624,557✔
582
    taosMsleep(10);
2,545,621✔
583
  }
584

585
  tWWorkerCleanup(&pWorker->pool);
17,078,922✔
586
  tWWorkerFreeQueue(&pWorker->pool, pWorker->queue);
17,078,441✔
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) {
179,905,182✔
608
  int64_t actualAddVal = val;
179,905,182✔
609
  actualAddVal <<= 32;
179,905,182✔
610
  int64_t newVal64 = atomic_fetch_add_64(ptr, actualAddVal);
179,905,182✔
611
  return GET_ACTIVE_N(newVal64);
179,905,182✔
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) {
×
617
  int64_t oldVal64 = *expectedVal, newVal64 = newVal;
×
618
  int32_t running = GET_RUNNING_N(*ptr);
×
619
  oldVal64 <<= 32;
×
620
  newVal64 <<= 32;
×
621
  oldVal64 |= running;
×
622
  newVal64 |= running;
×
623
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
×
624
  if (actualNewVal64 == oldVal64) {
×
625
    return true;
×
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);
806,666✔
659
    *expectedRunning = GET_RUNNING_N(actualNewVal64);
806,666✔
660
    return false;
806,666✔
661
  }
662
}
663

664
static void *tQueryAutoQWorkerThreadFp(SQueryAutoQWorker *worker) {
180,356,174✔
665
  SQueryAutoQWorkerPool *pool = worker->pool;
180,356,174✔
666
  SQueueInfo             qinfo = {0};
180,369,760✔
667
  void                  *msg = NULL;
180,366,833✔
668
  int32_t                code = 0;
180,366,833✔
669

670
  int32_t ret = taosBlockSIGPIPE();
180,366,833✔
671
  if (ret < 0) {
180,354,891✔
672
    uError("worker:%s:%d failed to block SIGPIPE", pool->name, worker->id);
×
673
  }
674

675
  setThreadName(pool->name);
180,354,891✔
676
  worker->pid = taosGetSelfPthreadId();
180,362,822✔
677
  uDebug("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
180,354,873✔
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,
179,824,591✔
682
            worker->pid);
683
      break;
179,889,401✔
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,670,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);
466,737✔
710
      break;
466,737✔
711
    }
712
  }
713

714
  DestoryThreadLocalRegComp();
180,356,138✔
715
  closeThreadNotificationConn();
180,341,821✔
716

717
  return NULL;
180,329,387✔
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);
207✔
726
    if (waitingNew == waiting) {
207✔
727
      (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
207✔
728
      (void)taosThreadCondSignal(&pPool->waitingAfterBlockCond);
207✔
729
      (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
207✔
730
      ret = true;
1,728✔
731
      break;
1,728✔
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);
×
744
    if (waitingNew == waiting) {
×
745
      (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
×
746
      (void)taosThreadCondSignal(&pPool->waitingBeforeProcessMsgCond);
×
747
      (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
×
748
      ret = true;
7,134✔
749
      break;
7,134✔
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))
258,444,856✔
763
      return true;
258,429,078✔
764
  }
765
  return false;
2,147,483,647✔
766
}
767

768
static void tQueryAutoQWorkerWaitingCheck(SQueryAutoQWorkerPool *pPool) {
2,147,483,647✔
769
  while (1) {
11,720✔
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
    }
777
    if (atomicCompareExchangeActive(&pPool->activeRunningN, &active, active - 1)) {
×
778
      break;
×
779
    }
780
  }
781
  // to wait for process
782
  (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
×
783
  (void)atomic_fetch_add_32(&pPool->waitingBeforeProcessMsgN, 1);
×
784
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingBeforeProcessMsgCond, &pPool->waitingBeforeProcessMsgLock);
×
785
  // recovered from waiting
786
  (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
×
787
  return;
×
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);
116,101,489✔
794
    if (pPool->exit) {
116,096,935✔
795
      (void)taosThreadMutexUnlock(&pPool->poolLock);
546✔
796
      return false;
546✔
797
    }
798

799
    SListNode *pNode = listNode(pWorker);
116,096,389✔
800
    SListNode *tNode = tdListPopNode(pPool->workers, pNode);
116,096,389✔
801
    // reclaim some workers
802
    if (pWorker->id >= pPool->maxInUse) {
116,096,389✔
803
      while (listNEles(pPool->exitedWorkers) > pPool->maxInUse - pPool->num) {
×
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);
×
813
      (void)taosThreadMutexUnlock(&pPool->poolLock);
×
814
      return false;
×
815
    }
816

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

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

827
    // recovered from backup
828
    (void)taosThreadMutexLock(&pPool->poolLock);
116,096,389✔
829
    if (pPool->exit) {
116,096,389✔
830
      (void)taosThreadMutexUnlock(&pPool->poolLock);
466,191✔
831
      return false;
466,191✔
832
    }
833
    SListNode *tNode1 = tdListPopNode(pPool->backupWorkers, pNode);
115,630,198✔
834
    tdListAppendNode(pPool->workers, pNode);
115,630,198✔
835
    (void)taosThreadMutexUnlock(&pPool->poolLock);
115,630,198✔
836

837
    return true;
115,630,198✔
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,650,488✔
845
  int32_t code;
846

847
  pool->exit = false;
3,650,488✔
848

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

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

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

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

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

882
  int32_t size = 0;
3,650,530✔
883
  if (pPool->workers) {
3,650,530✔
884
    size += listNEles(pPool->workers);
3,650,488✔
885
  }
886
  if (pPool->backupWorkers) {
3,650,530✔
887
    size += listNEles(pPool->backupWorkers);
3,650,488✔
888
  }
889
  if (pPool->qset) {
3,650,530✔
890
    for (int32_t i = 0; i < size; ++i) {
184,022,407✔
891
      taosQsetThreadResume(pPool->qset);
180,371,919✔
892
    }
893
  }
894
  (void)taosThreadMutexUnlock(&pPool->poolLock);
3,650,530✔
895

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

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

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

908
  int32_t            idx = 0;
3,650,530✔
909
  SQueryAutoQWorker *worker = NULL;
3,650,530✔
910
  while (pPool->workers) {
183,556,258✔
911
    (void)taosThreadMutexLock(&pPool->poolLock);
183,556,216✔
912
    if (listNEles(pPool->workers) <= 0) {
183,556,216✔
913
      (void)taosThreadMutexUnlock(&pPool->poolLock);
3,650,488✔
914
      break;
3,650,488✔
915
    }
916
    SListNode *pNode = tdListPopHead(pPool->workers);
179,905,728✔
917
    uDebug("0free worker node:%p, prev:%p, next:%p", pNode, TD_DLIST_NODE_PREV(pNode), TD_DLIST_NODE_NEXT(pNode));
179,905,728✔
918
    worker = pNode ? (SQueryAutoQWorker *)pNode->data : NULL;
179,905,728✔
919
    (void)taosThreadMutexUnlock(&pPool->poolLock);
179,905,728✔
920
    if (worker && taosCheckPthreadValid(worker->thread)) {
179,905,728✔
921
      (void)taosThreadJoin(worker->thread, NULL);
179,905,728✔
922
      taosThreadClear(&worker->thread);
179,905,728✔
923
    }
924
    uDebug("free worker node:%p, prev:%p, next:%p", pNode, TD_DLIST_NODE_PREV(pNode), TD_DLIST_NODE_NEXT(pNode));
179,905,728✔
925

926
    taosMemoryFree(pNode);
179,905,728✔
927
  }
928

929
  while (pPool->backupWorkers) {
4,116,721✔
930
    (void)taosThreadMutexLock(&pPool->poolLock);
4,116,679✔
931
    if (listNEles(pPool->backupWorkers) <= 0) {
4,116,679✔
932
      (void)taosThreadMutexUnlock(&pPool->poolLock);
3,650,488✔
933
      break;
3,650,488✔
934
    }
935
    uDebug("backupworker head:%p, prev:%p, next:%p", TD_DLIST_HEAD(pPool->backupWorkers), 
466,191✔
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);
466,191✔
939
    worker = pNode ? (SQueryAutoQWorker *)pNode->data : NULL;
466,191✔
940
    (void)taosThreadMutexUnlock(&pPool->poolLock);
466,191✔
941

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

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

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

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

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

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

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

985
STaosQueue *tQueryAutoQWorkerAllocQueue(SQueryAutoQWorkerPool *pool, void *ahandle, FItem fp) {
10,932,120✔
986
  STaosQueue *queue;
10,901,474✔
987
  int32_t     code = taosOpenQueue(&queue);
10,932,120✔
988
  if (code) {
10,932,120✔
989
    terrno = code;
×
990
    return NULL;
×
991
  }
992

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

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

1020
      TdThreadAttr thAttr;
178,546,958✔
1021
      (void)taosThreadAttrInit(&thAttr);
179,905,182✔
1022
      (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
179,905,182✔
1023

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

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

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

1040
  return queue;
10,932,120✔
1041
}
1042

1043
void tQueryAutoQWorkerFreeQueue(SQueryAutoQWorkerPool *pPool, STaosQueue *pQ) { taosCloseQueue(pQ); }
9,529,393✔
1044

1045
static int32_t tQueryAutoQWorkerAddWorker(SQueryAutoQWorkerPool *pool) {
116,094,828✔
1046
  // try backup pool
1047
  int32_t backup = pool->backupNum;
116,094,828✔
1048
  while (backup > 0) {
116,104,102✔
1049
    int32_t backupNew = atomic_val_compare_exchange_32(&pool->backupNum, backup, backup - 1);
115,637,365✔
1050
    if (backupNew == backup) {
115,639,472✔
1051
      (void)taosThreadCondSignal(&pool->backupCond);
115,630,198✔
1052
      return TSDB_CODE_SUCCESS;
115,629,167✔
1053
    }
1054
    backup = backupNew;
9,274✔
1055
  }
1056
  // backup pool is empty, create new
1057
  SQueryAutoQWorker *pWorker = NULL;
466,737✔
1058
  SQueryAutoQWorker  worker = {0};
466,737✔
1059
  worker.pool = pool;
466,737✔
1060
  worker.backupIdx = -1;
466,737✔
1061
  (void)taosThreadMutexLock(&pool->poolLock);
466,737✔
1062
  if (pool->exit) {
466,737✔
1063
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
1064
    return TSDB_CODE_SUCCESS;
×
1065
  }
1066
  worker.id = listNEles(pool->workers);
466,737✔
1067
  SListNode *pNode = tdListAdd(pool->workers, &worker);
466,737✔
1068
  if (!pNode) {
466,737✔
1069
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
1070
    return terrno;
×
1071
  }
1072
  (void)taosThreadMutexUnlock(&pool->poolLock);
466,737✔
1073
  pWorker = (SQueryAutoQWorker *)pNode->data;
466,737✔
1074

1075
  TdThreadAttr thAttr;
465,555✔
1076
  (void)taosThreadAttrInit(&thAttr);
466,737✔
1077
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
466,737✔
1078

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

1085
  return TSDB_CODE_SUCCESS;
466,737✔
1086
}
1087

1088
static int32_t tQueryAutoQWorkerBeforeBlocking(void *p) {
258,426,569✔
1089
  SQueryAutoQWorkerPool *pPool = p;
258,426,569✔
1090
  if (tQueryAutoQWorkerTrySignalWaitingAfterBlock(p) || tQueryAutoQWorkerTrySignalWaitingBeforeProcess(p) ||
516,850,953✔
1091
      tQueryAutoQWorkerTryDecActive(p, pPool->num)) {
258,427,801✔
1092
  } else {
1093
    int32_t code = tQueryAutoQWorkerAddWorker(pPool);
116,093,156✔
1094
    if (code != TSDB_CODE_SUCCESS) {
116,096,456✔
1095
      return code;
×
1096
    }
1097
    (void)atomicFetchSubRunning(&pPool->activeRunningN, 1);
116,096,456✔
1098
  }
1099

1100
  return TSDB_CODE_SUCCESS;
258,427,629✔
1101
}
1102

1103
static int32_t tQueryAutoQWorkerRecoverFromBlocking(void *p) {
258,428,747✔
1104
  SQueryAutoQWorkerPool *pPool = p;
258,428,747✔
1105
  int64_t                val64 = pPool->activeRunningN;
258,428,747✔
1106
  int32_t                running = GET_RUNNING_N(val64), active = GET_ACTIVE_N(val64);
258,428,747✔
1107
  while (running < pPool->num) {
258,437,215✔
1108
    if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active + 1, &running, running + 1)) {
258,437,008✔
1109
      return TSDB_CODE_SUCCESS;
258,429,078✔
1110
    }
1111
  }
1112
  (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
207✔
1113
  (void)atomic_fetch_add_32(&pPool->waitingAfterBlockN, 1);
207✔
1114
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingAfterBlockCond, &pPool->waitingAfterBlockLock);
207✔
1115
  (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
207✔
1116
  if (pPool->exit) return TSDB_CODE_QRY_QWORKER_QUIT;
207✔
1117
  return TSDB_CODE_SUCCESS;
207✔
1118
}
1119

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

1129
static void *tDispatchWorkerThreadFp(SDispatchWorker *pWorker) {
8,261,602✔
1130
  SDispatchWorkerPool *pPool = pWorker->pool;
8,261,602✔
1131
  SQueueInfo qinfo = {0};
8,269,086✔
1132
  int32_t code = 0;
8,269,158✔
1133
  void *msg = NULL;
8,269,158✔
1134

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

1140
  setThreadName(pPool->name);
8,268,278✔
1141
  pWorker->pid = taosGetSelfPthreadId();
8,268,145✔
1142
  uInfo("worker:%s:%d is running, thread:%d", pPool->name, pWorker->id, pWorker->pid);
8,267,707✔
1143

1144
  while (1) {
1145
    if (taosReadQitemFromQset(pWorker->qset, (void **)&msg, &qinfo) == 0) {
98,237,484✔
1146
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%d", pPool->name, pWorker->id,
8,254,715✔
1147
            pWorker->qset, pWorker->pid);
1148
      break;
8,269,285✔
1149
    }
1150

1151
    if (qinfo.timestamp != 0) {
89,968,309✔
1152
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
89,966,346✔
1153
      if (cost > QUEUE_THRESHOLD) {
89,966,346✔
1154
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pPool->name, cost / QUEUE_THRESHOLD);
19,360✔
1155
      }
1156
    }
1157

1158
    if (qinfo.fp != NULL) {
89,965,679✔
1159
      qinfo.workerId = pWorker->id;
89,964,498✔
1160
      qinfo.threadNum = pPool->num;
89,964,498✔
1161
      (*((FItem)qinfo.fp))(&qinfo, msg);
89,969,051✔
1162
    }
1163
  }
1164
  DestoryThreadLocalRegComp();
8,269,285✔
1165
  closeThreadNotificationConn();
8,269,285✔
1166
  return NULL;
8,268,548✔
1167
}
1168

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

1187
    TdThreadAttr thAttr;
8,255,852✔
1188
    (void)taosThreadAttrInit(&thAttr);
8,269,285✔
1189
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
8,269,285✔
1190

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

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

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

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

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

1249
int32_t tAddTaskIntoDispatchWorkerPool(SDispatchWorkerPool *pPool, void *pMsg) {
89,969,482✔
1250
  int32_t code = 0;
89,969,482✔
1251
  int32_t idx = 0;
89,969,482✔
1252
  SDispatchWorker *pWorker = NULL;
89,970,186✔
1253
  (void)taosThreadMutexLock(&pPool->poolLock);
89,970,186✔
1254
  code = pPool->dispatchFp(pPool, pMsg, &idx);
89,973,352✔
1255
  if (code == 0) {
89,973,352✔
1256
    pWorker = pPool->pWorkers + idx;
89,973,352✔
1257
    if (pWorker->queue) {
89,973,352✔
1258
      code = taosWriteQitem(pWorker->queue, pMsg);
89,973,352✔
1259
    } else {
1260
      code = TSDB_CODE_INTERNAL_ERROR;
×
1261
    }
1262
  }
1263
  (void)taosThreadMutexUnlock(&pPool->poolLock);
89,973,352✔
1264
  if (code != 0) {
89,972,586✔
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));
89,972,586✔
1268
  }
1269
  return code;
89,972,586✔
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