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

taosdata / TDengine / #4976

06 Mar 2026 09:48AM UTC coverage: 68.446% (+0.08%) from 68.37%
#4976

push

travis-ci

web-flow
feat(TDgpt): support multiple input data columns for anomaly detection. (#34606)

0 of 93 new or added lines in 9 files covered. (0.0%)

5718 existing lines in 144 files now uncovered.

211146 of 308486 relevant lines covered (68.45%)

136170362.0 hits per line

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

75.68
/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) {
4,776,115✔
30
  int32_t code = taosOpenQset(&pool->qset);
4,776,115✔
31
  if (code) return code;
4,776,115✔
32

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

39
  (void)taosThreadMutexInit(&pool->mutex, NULL);
4,776,115✔
40

41
  for (int32_t i = 0; i < pool->max; ++i) {
33,860,630✔
42
    SQueueWorker *worker = pool->workers + i;
29,084,515✔
43
    worker->id = i;
29,084,515✔
44
    worker->pool = pool;
29,084,515✔
45
  }
46

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

51
void tQWorkerCleanup(SQWorkerPool *pool) {
4,776,115✔
52
  for (int32_t i = 0; i < pool->max; ++i) {
33,860,630✔
53
    SQueueWorker *worker = pool->workers + i;
29,084,515✔
54
    if (taosCheckPthreadValid(worker->thread)) {
29,084,515✔
55
      taosQsetThreadResume(pool->qset);
29,084,515✔
56
    }
57
  }
58

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

69
  taosMemoryFreeClear(pool->workers);
4,776,115✔
70
  taosCloseQset(pool->qset);
4,776,115✔
71
  (void)taosThreadMutexDestroy(&pool->mutex);
4,776,115✔
72

73
  uInfo("worker:%s is closed", pool->name);
4,776,115✔
74
}
4,776,115✔
75

76
static void *tQWorkerThreadFp(SQueueWorker *worker) {
29,082,786✔
77
  SQWorkerPool *pool = worker->pool;
29,082,786✔
78
  SQueueInfo    qinfo = {0};
29,084,515✔
79
  void         *msg = NULL;
29,084,515✔
80
  int32_t       code = 0;
29,084,515✔
81

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

87
  setThreadName(pool->name);
29,082,751✔
88
  worker->pid = taosGetSelfPthreadId();
29,084,073✔
89
  uInfo("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
29,082,075✔
90

91
  while (1) {
92
    if (taosReadQitemFromQset(pool->qset, (void **)&msg, &qinfo) == 0) {
442,066,231✔
93
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, pool->qset,
29,064,097✔
94
            worker->pid);
95
      break;
29,082,528✔
96
    }
97

98
    if (qinfo.timestamp != 0) {
412,984,781✔
99
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
412,985,204✔
100
      if (cost > QUEUE_THRESHOLD) {
412,985,204✔
101
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pool->name, cost / QUEUE_THRESHOLD);
1,206,935✔
102
      }
103
    }
104

105
    if (qinfo.fp != NULL) {
412,984,369✔
106
      qinfo.workerId = worker->id;
412,982,228✔
107
      qinfo.threadNum = pool->num;
412,984,469✔
108
      (*((FItem)qinfo.fp))(&qinfo, msg);
412,985,588✔
109
    }
110

111
    taosUpdateItemSize(qinfo.queue, 1);
412,974,657✔
112
  }
113

114
  DestoryThreadLocalRegComp();
29,082,528✔
115

116
  return NULL;
29,082,899✔
117
}
118

119
STaosQueue *tQWorkerAllocQueue(SQWorkerPool *pool, void *ahandle, FItem fp) {
4,776,115✔
120
  int32_t     code;
121
  STaosQueue *queue;
4,774,453✔
122

123
  code = taosOpenQueue(&queue);
4,776,115✔
124
  if (code) {
4,776,115✔
125
    terrno = code;
×
126
    return NULL;
×
127
  }
128

129
  (void)taosThreadMutexLock(&pool->mutex);
4,776,115✔
130
  taosSetQueueFp(queue, fp, NULL);
4,776,115✔
131
  code = taosAddIntoQset(pool->qset, queue, ahandle);
4,776,115✔
132
  if (code) {
4,776,115✔
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) {
4,776,115✔
141
    do {
142
      SQueueWorker *worker = pool->workers + pool->num;
29,084,515✔
143

144
      TdThreadAttr thAttr;
29,073,655✔
145
      (void)taosThreadAttrInit(&thAttr);
29,084,515✔
146
      (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
29,084,515✔
147

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

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

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

164
  return queue;
4,776,115✔
165
}
166

167
void tQWorkerFreeQueue(SQWorkerPool *pool, STaosQueue *queue) {
4,776,115✔
168
  uInfo("worker:%s, queue:%p is freed", pool->name, queue);
4,776,115✔
169
  taosCloseQueue(queue);
4,776,115✔
170
}
4,776,115✔
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) {
16,937,141✔
337
  pool->nextId = 0;
16,937,141✔
338
  pool->workers = taosMemoryCalloc(pool->max, sizeof(SWWorker));
16,938,219✔
339
  if (pool->workers == NULL) {
16,938,496✔
340
    return terrno;
×
341
  }
342

343
  (void)taosThreadMutexInit(&pool->mutex, NULL);
16,937,664✔
344

345
  for (int32_t i = 0; i < pool->max; ++i) {
39,613,666✔
346
    SWWorker *worker = pool->workers + i;
22,678,433✔
347
    worker->id = i;
22,676,351✔
348
    worker->qall = NULL;
22,677,324✔
349
    worker->qset = NULL;
22,676,400✔
350
    worker->pool = pool;
22,677,242✔
351
  }
352

353
  uInfo("worker:%s is initialized, max:%d", pool->name, pool->max);
16,935,441✔
354
  return 0;
16,938,306✔
355
}
356

357
void tWWorkerCleanup(SWWorkerPool *pool) {
16,938,476✔
358
  for (int32_t i = 0; i < pool->max; ++i) {
39,617,166✔
359
    SWWorker *worker = pool->workers + i;
22,678,142✔
360
    if (taosCheckPthreadValid(worker->thread)) {
22,677,880✔
361
      if (worker->qset) {
19,020,848✔
362
        taosQsetThreadResume(worker->qset);
19,020,848✔
363
      }
364
    }
365
  }
366

367
  for (int32_t i = 0; i < pool->max; ++i) {
39,617,166✔
368
    SWWorker *worker = pool->workers + i;
22,678,690✔
369
    if (taosCheckPthreadValid(worker->thread)) {
22,678,690✔
370
      uInfo("worker:%s:%d is stopping", pool->name, worker->id);
19,020,848✔
371
      (void)taosThreadJoin(worker->thread, NULL);
19,020,117✔
372
      taosThreadClear(&worker->thread);
19,020,848✔
373
      taosFreeQall(worker->qall);
19,020,521✔
374
      taosCloseQset(worker->qset);
19,020,848✔
375
      uInfo("worker:%s:%d is stopped", pool->name, worker->id);
19,020,848✔
376
    }
377
  }
378

379
  taosMemoryFreeClear(pool->workers);
16,938,476✔
380
  (void)taosThreadMutexDestroy(&pool->mutex);
16,938,432✔
381

382
  uInfo("worker:%s is closed", pool->name);
16,938,432✔
383
}
16,938,279✔
384

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

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

397
  setThreadName(pool->name);
19,017,475✔
398
  worker->pid = taosGetSelfPthreadId();
19,020,856✔
399
  uInfo("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
19,018,997✔
400

401
  while (1) {
402
    numOfMsgs = taosReadAllQitemsFromQset(worker->qset, worker->qall, &qinfo);
2,124,723,335✔
403
    if (numOfMsgs == 0) {
2,124,591,390✔
404
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, worker->qset,
19,012,956✔
405
            worker->pid);
406
      break;
19,020,848✔
407
    }
408

409
    if (qinfo.timestamp != 0) {
2,105,578,434✔
410
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
2,105,495,921✔
411
      if (cost > QUEUE_THRESHOLD) {
2,105,495,921✔
412
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pool->name, cost / QUEUE_THRESHOLD);
19,771✔
413
      }
414
    }
415

416
    if (qinfo.fp != NULL) {
2,105,478,774✔
417
      qinfo.workerId = worker->id;
2,105,534,890✔
418
      qinfo.threadNum = pool->num;
2,105,569,329✔
419
      (*((FItems)qinfo.fp))(&qinfo, worker->qall, numOfMsgs);
2,105,608,980✔
420
    }
421
    taosUpdateItemSize(qinfo.queue, numOfMsgs);
2,105,479,629✔
422
  }
423

424
  return NULL;
19,020,848✔
425
}
426

427
STaosQueue *tWWorkerAllocQueue(SWWorkerPool *pool, void *ahandle, FItems fp) {
20,328,115✔
428
  (void)taosThreadMutexLock(&pool->mutex);
20,328,115✔
429
  SWWorker   *worker = pool->workers + pool->nextId;
20,328,115✔
430
  int32_t     code = -1;
20,328,025✔
431
  STaosQueue *queue;
20,325,834✔
432

433
  code = taosOpenQueue(&queue);
20,328,025✔
434
  if (code) goto _OVER;
20,328,115✔
435

436
  taosSetQueueFp(queue, NULL, fp);
20,328,115✔
437
  if (worker->qset == NULL) {
20,327,943✔
438
    code = taosOpenQset(&worker->qset);
19,018,994✔
439
    if (code) goto _OVER;
19,019,540✔
440

441
    code = taosAddIntoQset(worker->qset, queue, ahandle);
19,019,540✔
442
    if (code) goto _OVER;
19,020,510✔
443
    code = taosAllocateQall(&worker->qall);
19,020,510✔
444
    if (code) goto _OVER;
19,020,598✔
445

446
    TdThreadAttr thAttr;
19,018,407✔
447
    (void)taosThreadAttrInit(&thAttr);
19,020,185✔
448
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
19,020,245✔
449
    code = taosThreadCreate(&worker->thread, &thAttr, (ThreadFp)tWWorkerThreadFp, worker);
19,017,783✔
450
    if ((code)) goto _OVER;
19,020,868✔
451

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

455
    (void)taosThreadAttrDestroy(&thAttr);
19,020,868✔
456
    pool->num++;
19,020,109✔
457
    if (pool->num > pool->max) pool->num = pool->max;
19,020,109✔
458
  } else {
459
    code = taosAddIntoQset(worker->qset, queue, ahandle);
1,307,247✔
460
    if (code) goto _OVER;
1,307,247✔
461
    pool->nextId = (pool->nextId + 1) % pool->max;
1,307,247✔
462
  }
463

464
_OVER:
20,327,356✔
465
  (void)taosThreadMutexUnlock(&pool->mutex);
20,327,184✔
466

467
  if (code) {
20,328,115✔
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);
41,487,546✔
475

476
    taosQueueSetThreadId(queue, worker->pid);
20,326,851✔
477
    uInfo("worker:%s, queue:%p is allocated, ahandle:%p thread:%08" PRId64, pool->name, queue, ahandle, worker->pid);
20,326,402✔
478
    return queue;
20,328,115✔
479
  }
480
}
481

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

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

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

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

523
      code = tQueryAutoQWorkerInit(pPool);
927,240✔
524
      if (code) return code;
927,240✔
525

526
      pWorker->queue = tQueryAutoQWorkerAllocQueue(pPool, pCfg->param, pCfg->fp);
927,240✔
527
      if (!pWorker->queue) {
927,240✔
528
        return terrno;
×
529
      }
530
    } break;
927,240✔
531
    default:
×
532
      return TSDB_CODE_INVALID_PARA;
×
533
  }
534
  return 0;
5,703,355✔
535
}
536

537
void tSingleWorkerCleanup(SSingleWorker *pWorker) {
5,703,355✔
538
  if (pWorker->queue == NULL) return;
5,703,355✔
539
  if (!pWorker->stopNoWaitQueue) {
5,703,355✔
540
    while (!taosQueueEmpty(pWorker->queue)) {
6,037,508✔
541
      taosMsleep(10);
400,586✔
542
    }
543
  }
544

545
  switch (pWorker->poolType) {
5,703,355✔
546
    case QWORKER_POOL:
4,776,115✔
547
      tQWorkerCleanup(pWorker->pool);
4,776,115✔
548
      tQWorkerFreeQueue(pWorker->pool, pWorker->queue);
4,776,115✔
549
      taosMemoryFree(pWorker->pool);
4,776,115✔
550
      break;
4,776,115✔
551
    case QUERY_AUTO_QWORKER_POOL:
927,240✔
552
      tQueryAutoQWorkerCleanup(pWorker->pool);
927,240✔
553
      tQueryAutoQWorkerFreeQueue(pWorker->pool, pWorker->queue);
927,240✔
554
      taosMemoryFree(pWorker->pool);
927,240✔
555
      break;
927,240✔
556
    default:
×
557
      break;
×
558
  }
559
}
560

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

566
  int32_t code = tWWorkerInit(pPool);
15,918,376✔
567
  if (code) return code;
15,919,912✔
568

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

574
  pWorker->name = pCfg->name;
15,919,357✔
575
  return 0;
15,919,912✔
576
}
577

578
void tMultiWorkerCleanup(SMultiWorker *pWorker) {
15,919,892✔
579
  if (pWorker->queue == NULL) return;
15,919,892✔
580

581
  while (!taosQueueEmpty(pWorker->queue)) {
20,671,240✔
582
    taosMsleep(10);
4,750,797✔
583
  }
584

585
  tWWorkerCleanup(&pWorker->pool);
15,919,892✔
586
  tWWorkerFreeQueue(&pWorker->pool, pWorker->queue);
15,919,702✔
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) {
168,737,316✔
608
  int64_t actualAddVal = val;
168,737,316✔
609
  actualAddVal <<= 32;
168,737,316✔
610
  int64_t newVal64 = atomic_fetch_add_64(ptr, actualAddVal);
168,737,316✔
611
  return GET_ACTIVE_N(newVal64);
168,737,316✔
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) {
788✔
617
  int64_t oldVal64 = *expectedVal, newVal64 = newVal;
788✔
618
  int32_t running = GET_RUNNING_N(*ptr);
788✔
619
  oldVal64 <<= 32;
788✔
620
  newVal64 <<= 32;
788✔
621
  oldVal64 |= running;
788✔
622
  newVal64 |= running;
788✔
623
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
788✔
624
  if (actualNewVal64 == oldVal64) {
788✔
625
    return true;
788✔
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,393,378✔
659
    *expectedRunning = GET_RUNNING_N(actualNewVal64);
1,393,378✔
660
    return false;
1,393,378✔
661
  }
662
}
663

664
static void *tQueryAutoQWorkerThreadFp(SQueryAutoQWorker *worker) {
169,115,496✔
665
  SQueryAutoQWorkerPool *pool = worker->pool;
169,115,496✔
666
  SQueueInfo             qinfo = {0};
169,118,246✔
667
  void                  *msg = NULL;
169,115,098✔
668
  int32_t                code = 0;
169,115,270✔
669

670
  int32_t ret = taosBlockSIGPIPE();
169,115,270✔
671
  if (ret < 0) {
169,106,244✔
672
    uError("worker:%s:%d failed to block SIGPIPE", pool->name, worker->id);
×
673
  }
674

675
  setThreadName(pool->name);
169,106,244✔
676
  worker->pid = taosGetSelfPthreadId();
169,109,313✔
677
  uDebug("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
169,109,157✔
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,
168,654,874✔
682
            worker->pid);
683
      break;
168,706,201✔
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);
5,278,004✔
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);
383,538✔
710
      break;
383,280✔
711
    }
712
  }
713

714
  DestoryThreadLocalRegComp();
169,089,481✔
715
  closeThreadNotificationConn();
169,039,485✔
716

717
  return NULL;
169,054,140✔
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);
1,576✔
726
    if (waitingNew == waiting) {
1,576✔
727
      (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
1,576✔
728
      (void)taosThreadCondSignal(&pPool->waitingAfterBlockCond);
1,576✔
729
      (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
1,576✔
UNCOV
730
      ret = true;
×
UNCOV
731
      break;
×
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);
788✔
744
    if (waitingNew == waiting) {
788✔
745
      (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
788✔
746
      (void)taosThreadCondSignal(&pPool->waitingBeforeProcessMsgCond);
788✔
747
      (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
788✔
748
      ret = true;
477✔
749
      break;
477✔
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))
259,240,712✔
763
      return true;
259,215,973✔
764
  }
765
  return false;
2,147,483,647✔
766
}
767

768
static void tQueryAutoQWorkerWaitingCheck(SQueryAutoQWorkerPool *pPool) {
2,147,483,647✔
769
  while (1) {
12,785✔
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)) {
2✔
778
      break;
788✔
779
    }
780
  }
781
  // to wait for process
782
  (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
788✔
783
  (void)atomic_fetch_add_32(&pPool->waitingBeforeProcessMsgN, 1);
788✔
784
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingBeforeProcessMsgCond, &pPool->waitingBeforeProcessMsgLock);
788✔
785
  // recovered from waiting
786
  (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
788✔
787
  return;
788✔
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);
112,317,390✔
794
    if (pPool->exit) {
112,279,635✔
795
      (void)taosThreadMutexUnlock(&pPool->poolLock);
1,258✔
796
      return false;
1,258✔
797
    }
798

799
    SListNode *pNode = listNode(pWorker);
112,278,377✔
800
    SListNode *tNode = tdListPopNode(pPool->workers, pNode);
112,278,377✔
801
    // reclaim some workers
802
    if (pWorker->id >= pPool->maxInUse) {
112,278,377✔
UNCOV
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
      }
UNCOV
812
      tdListAppendNode(pPool->exitedWorkers, pNode);
×
UNCOV
813
      (void)taosThreadMutexUnlock(&pPool->poolLock);
×
UNCOV
814
      return false;
×
815
    }
816

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

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

827
    // recovered from backup
828
    (void)taosThreadMutexLock(&pPool->poolLock);
112,278,263✔
829
    if (pPool->exit) {
112,278,377✔
830
      (void)taosThreadMutexUnlock(&pPool->poolLock);
382,280✔
831
      return false;
382,280✔
832
    }
833
    SListNode *tNode1 = tdListPopNode(pPool->backupWorkers, pNode);
111,896,097✔
834
    tdListAppendNode(pPool->workers, pNode);
111,896,097✔
835
    (void)taosThreadMutexUnlock(&pPool->poolLock);
111,896,097✔
836

837
    return true;
111,896,097✔
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,419,995✔
845
  int32_t code;
846

847
  pool->exit = false;
3,419,995✔
848

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

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

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

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

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

882
  int32_t size = 0;
3,420,039✔
883
  if (pPool->workers) {
3,420,039✔
884
    size += listNEles(pPool->workers);
3,419,995✔
885
  }
886
  if (pPool->backupWorkers) {
3,420,039✔
887
    size += listNEles(pPool->backupWorkers);
3,419,995✔
888
  }
889
  if (pPool->qset) {
3,420,039✔
890
    for (int32_t i = 0; i < size; ++i) {
172,540,849✔
891
      taosQsetThreadResume(pPool->qset);
169,120,854✔
892
    }
893
  }
894
  (void)taosThreadMutexUnlock(&pPool->poolLock);
3,420,039✔
895

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

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

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

908
  int32_t            idx = 0;
3,420,039✔
909
  SQueryAutoQWorker *worker = NULL;
3,420,039✔
910
  while (pPool->workers) {
172,158,613✔
911
    (void)taosThreadMutexLock(&pPool->poolLock);
172,158,569✔
912
    if (listNEles(pPool->workers) <= 0) {
172,158,569✔
913
      (void)taosThreadMutexUnlock(&pPool->poolLock);
3,419,995✔
914
      break;
3,419,995✔
915
    }
916
    SListNode *pNode = tdListPopHead(pPool->workers);
168,738,574✔
917
    uDebug("0free worker node:%p, prev:%p, next:%p", pNode, TD_DLIST_NODE_PREV(pNode), TD_DLIST_NODE_NEXT(pNode));
168,738,574✔
918
    worker = pNode ? (SQueryAutoQWorker *)pNode->data : NULL;
168,738,574✔
919
    (void)taosThreadMutexUnlock(&pPool->poolLock);
168,738,574✔
920
    if (worker && taosCheckPthreadValid(worker->thread)) {
168,738,574✔
921
      (void)taosThreadJoin(worker->thread, NULL);
168,738,574✔
922
      taosThreadClear(&worker->thread);
168,738,574✔
923
    }
924
    uDebug("free worker node:%p, prev:%p, next:%p", pNode, TD_DLIST_NODE_PREV(pNode), TD_DLIST_NODE_NEXT(pNode));
168,738,574✔
925

926
    taosMemoryFree(pNode);
168,738,574✔
927
  }
928

929
  while (pPool->backupWorkers) {
3,802,319✔
930
    (void)taosThreadMutexLock(&pPool->poolLock);
3,802,275✔
931
    if (listNEles(pPool->backupWorkers) <= 0) {
3,802,275✔
932
      (void)taosThreadMutexUnlock(&pPool->poolLock);
3,419,995✔
933
      break;
3,419,995✔
934
    }
935
    uDebug("backupworker head:%p, prev:%p, next:%p", TD_DLIST_HEAD(pPool->backupWorkers), 
382,280✔
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);
382,280✔
939
    worker = pNode ? (SQueryAutoQWorker *)pNode->data : NULL;
382,280✔
940
    (void)taosThreadMutexUnlock(&pPool->poolLock);
382,280✔
941

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

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

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

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

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

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

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

985
STaosQueue *tQueryAutoQWorkerAllocQueue(SQueryAutoQWorkerPool *pool, void *ahandle, FItem fp) {
10,199,233✔
986
  STaosQueue *queue;
10,185,202✔
987
  int32_t     code = taosOpenQueue(&queue);
10,199,233✔
988
  if (code) {
10,198,503✔
989
    terrno = code;
×
UNCOV
990
    return NULL;
×
991
  }
992

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

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

1020
      TdThreadAttr thAttr;
167,684,596✔
1021
      (void)taosThreadAttrInit(&thAttr);
168,737,316✔
1022
      (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
168,737,316✔
1023

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

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

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

1040
  return queue;
10,199,233✔
1041
}
1042

1043
void tQueryAutoQWorkerFreeQueue(SQueryAutoQWorkerPool *pPool, STaosQueue *pQ) { taosCloseQueue(pQ); }
8,886,455✔
1044

1045
static int32_t tQueryAutoQWorkerAddWorker(SQueryAutoQWorkerPool *pool) {
112,275,441✔
1046
  // try backup pool
1047
  int32_t backup = pool->backupNum;
112,275,441✔
1048
  while (backup > 0) {
112,304,753✔
1049
    int32_t backupNew = atomic_val_compare_exchange_32(&pool->backupNum, backup, backup - 1);
111,921,215✔
1050
    if (backupNew == backup) {
111,925,286✔
1051
      (void)taosThreadCondSignal(&pool->backupCond);
111,896,097✔
1052
      return TSDB_CODE_SUCCESS;
111,895,035✔
1053
    }
1054
    backup = backupNew;
29,189✔
1055
  }
1056
  // backup pool is empty, create new
1057
  SQueryAutoQWorker *pWorker = NULL;
383,538✔
1058
  SQueryAutoQWorker  worker = {0};
383,538✔
1059
  worker.pool = pool;
383,538✔
1060
  worker.backupIdx = -1;
383,538✔
1061
  (void)taosThreadMutexLock(&pool->poolLock);
383,538✔
1062
  if (pool->exit) {
383,538✔
1063
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
UNCOV
1064
    return TSDB_CODE_SUCCESS;
×
1065
  }
1066
  worker.id = listNEles(pool->workers);
383,538✔
1067
  SListNode *pNode = tdListAdd(pool->workers, &worker);
383,538✔
1068
  if (!pNode) {
383,538✔
1069
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
UNCOV
1070
    return terrno;
×
1071
  }
1072
  (void)taosThreadMutexUnlock(&pool->poolLock);
383,538✔
1073
  pWorker = (SQueryAutoQWorker *)pNode->data;
383,538✔
1074

1075
  TdThreadAttr thAttr;
382,461✔
1076
  (void)taosThreadAttrInit(&thAttr);
383,538✔
1077
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
383,538✔
1078

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

1085
  return TSDB_CODE_SUCCESS;
383,538✔
1086
}
1087

1088
static int32_t tQueryAutoQWorkerBeforeBlocking(void *p) {
259,215,151✔
1089
  SQueryAutoQWorkerPool *pPool = p;
259,215,151✔
1090
  if (tQueryAutoQWorkerTrySignalWaitingAfterBlock(p) || tQueryAutoQWorkerTrySignalWaitingBeforeProcess(p) ||
518,426,590✔
1091
      tQueryAutoQWorkerTryDecActive(p, pPool->num)) {
259,206,128✔
1092
  } else {
1093
    int32_t code = tQueryAutoQWorkerAddWorker(pPool);
112,273,490✔
1094
    if (code != TSDB_CODE_SUCCESS) {
112,279,635✔
UNCOV
1095
      return code;
×
1096
    }
1097
    (void)atomicFetchSubRunning(&pPool->activeRunningN, 1);
112,279,635✔
1098
  }
1099

1100
  return TSDB_CODE_SUCCESS;
259,215,859✔
1101
}
1102

1103
static int32_t tQueryAutoQWorkerRecoverFromBlocking(void *p) {
259,218,337✔
1104
  SQueryAutoQWorkerPool *pPool = p;
259,218,337✔
1105
  int64_t                val64 = pPool->activeRunningN;
259,218,337✔
1106
  int32_t                running = GET_RUNNING_N(val64), active = GET_ACTIVE_N(val64);
259,218,337✔
1107
  while (running < pPool->num) {
259,229,020✔
1108
    if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active + 1, &running, running + 1)) {
259,227,444✔
1109
      return TSDB_CODE_SUCCESS;
259,216,761✔
1110
    }
1111
  }
1112
  (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
1,576✔
1113
  (void)atomic_fetch_add_32(&pPool->waitingAfterBlockN, 1);
1,576✔
1114
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingAfterBlockCond, &pPool->waitingAfterBlockLock);
1,576✔
1115
  (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
1,576✔
1116
  if (pPool->exit) return TSDB_CODE_QRY_QWORKER_QUIT;
1,576✔
1117
  return TSDB_CODE_SUCCESS;
1,576✔
1118
}
1119

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

1129
static void *tDispatchWorkerThreadFp(SDispatchWorker *pWorker) {
7,752,277✔
1130
  SDispatchWorkerPool *pPool = pWorker->pool;
7,752,277✔
1131
  SQueueInfo qinfo = {0};
7,759,174✔
1132
  int32_t code = 0;
7,759,174✔
1133
  void *msg = NULL;
7,759,174✔
1134

1135
  int32_t ret = taosBlockSIGPIPE();
7,759,174✔
1136
  if (ret < 0) {
7,759,074✔
UNCOV
1137
    uError("worker:%s:%d failed to block SIGPIPE", pPool->name, pWorker->id);
×
1138
  }
1139

1140
  setThreadName(pPool->name);
7,759,074✔
1141
  pWorker->pid = taosGetSelfPthreadId();
7,759,174✔
1142
  uInfo("worker:%s:%d is running, thread:%d", pPool->name, pWorker->id, pWorker->pid);
7,757,964✔
1143

1144
  while (1) {
1145
    if (taosReadQitemFromQset(pWorker->qset, (void **)&msg, &qinfo) == 0) {
90,774,435✔
1146
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%d", pPool->name, pWorker->id,
7,748,580✔
1147
            pWorker->qset, pWorker->pid);
1148
      break;
7,758,658✔
1149
    }
1150

1151
    if (qinfo.timestamp != 0) {
83,022,240✔
1152
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
83,022,028✔
1153
      if (cost > QUEUE_THRESHOLD) {
83,022,028✔
1154
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pPool->name, cost / QUEUE_THRESHOLD);
10,304✔
1155
      }
1156
    }
1157

1158
    if (qinfo.fp != NULL) {
83,022,222✔
1159
      qinfo.workerId = pWorker->id;
83,020,921✔
1160
      qinfo.threadNum = pPool->num;
83,021,531✔
1161
      (*((FItem)qinfo.fp))(&qinfo, msg);
83,021,579✔
1162
    }
1163
  }
1164
  DestoryThreadLocalRegComp();
7,758,658✔
1165
  closeThreadNotificationConn();
7,758,916✔
1166
  return NULL;
7,759,174✔
1167
}
1168

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

1187
    TdThreadAttr thAttr;
7,753,624✔
1188
    (void)taosThreadAttrInit(&thAttr);
7,759,174✔
1189
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
7,759,174✔
1190

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

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

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

1222
void tDispatchWorkerCleanup(SDispatchWorkerPool *pPool) {
1,088,123✔
1223
  (void)taosThreadMutexLock(&pPool->poolLock);
1,088,123✔
1224
  pPool->exit = true;
1,088,123✔
1225
  if (pPool->pWorkers) {
1,088,123✔
1226
    for (int32_t i = 0; i < pPool->num; ++i) {
8,847,297✔
1227
      SDispatchWorker *pWorker = pPool->pWorkers + i;
7,759,174✔
1228
      if (pWorker->qset) {
7,759,174✔
1229
        taosQsetThreadResume(pWorker->qset);
7,759,174✔
1230
      }
1231
    }
1232
  }
1233
  (void)taosThreadMutexUnlock(&pPool->poolLock);
1,088,123✔
1234

1235
  if (pPool->pWorkers) {
1,088,123✔
1236
    for (int32_t i = 0; i < pPool->num; ++i) {
8,847,297✔
1237
      SDispatchWorker *pWorker = pPool->pWorkers + i;
7,759,174✔
1238
      if (taosCheckPthreadValid(pWorker->thread)) {
7,759,174✔
1239
        (void)taosThreadJoin(pWorker->thread, NULL);
7,759,174✔
1240
        taosThreadClear(&pWorker->thread);
7,759,174✔
1241
      }
1242
    }
1243
  }
1244
  tDispatchWorkerFreeQueue(pPool);
1,088,123✔
1245
  taosMemoryFreeClear(pPool->pWorkers);
1,088,123✔
1246
  (void)taosThreadMutexDestroy(&pPool->poolLock);
1,088,123✔
1247
}
1,088,123✔
1248

1249
int32_t tAddTaskIntoDispatchWorkerPool(SDispatchWorkerPool *pPool, void *pMsg) {
83,015,579✔
1250
  int32_t code = 0;
83,015,579✔
1251
  int32_t idx = 0;
83,015,579✔
1252
  SDispatchWorker *pWorker = NULL;
83,018,015✔
1253
  (void)taosThreadMutexLock(&pPool->poolLock);
83,018,015✔
1254
  code = pPool->dispatchFp(pPool, pMsg, &idx);
83,024,480✔
1255
  if (code == 0) {
83,024,480✔
1256
    pWorker = pPool->pWorkers + idx;
83,024,480✔
1257
    if (pWorker->queue) {
83,024,480✔
1258
      code = taosWriteQitem(pWorker->queue, pMsg);
83,024,480✔
1259
    } else {
UNCOV
1260
      code = TSDB_CODE_INTERNAL_ERROR;
×
1261
    }
1262
  }
1263
  (void)taosThreadMutexUnlock(&pPool->poolLock);
83,024,480✔
1264
  if (code != 0) {
83,024,480✔
UNCOV
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));
83,024,480✔
1268
  }
1269
  return code;
83,024,480✔
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