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

taosdata / TDengine / #3842

07 Apr 2025 11:21AM UTC coverage: 62.696% (-0.3%) from 63.027%
#3842

push

travis-ci

web-flow
merge: from main to 3.0 branch (#30679)

154855 of 315075 branches covered (49.15%)

Branch coverage included in aggregate %.

6 of 8 new or added lines in 5 files covered. (75.0%)

2309 existing lines in 130 files now uncovered.

240176 of 314995 relevant lines covered (76.25%)

19119980.29 hits per line

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

71.88
/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

23
#define QUEUE_THRESHOLD (1000 * 1000)
24

25
typedef void *(*ThreadFp)(void *param);
26

27
int32_t tQWorkerInit(SQWorkerPool *pool) {
20,732✔
28
  int32_t code = taosOpenQset(&pool->qset);
20,732✔
29
  if (code) return code;
20,732!
30

31
  pool->workers = taosMemoryCalloc(pool->max, sizeof(SQueueWorker));
20,732!
32
  if (pool->workers == NULL) {
20,732!
33
    taosCloseQset(pool->qset);
×
34
    return terrno;
×
35
  }
36

37
  (void)taosThreadMutexInit(&pool->mutex, NULL);
20,732✔
38

39
  for (int32_t i = 0; i < pool->max; ++i) {
137,017✔
40
    SQueueWorker *worker = pool->workers + i;
116,285✔
41
    worker->id = i;
116,285✔
42
    worker->pool = pool;
116,285✔
43
  }
44

45
  uInfo("worker:%s is initialized, min:%d max:%d", pool->name, pool->min, pool->max);
20,732!
46
  return 0;
20,732✔
47
}
48

49
void tQWorkerCleanup(SQWorkerPool *pool) {
20,732✔
50
  for (int32_t i = 0; i < pool->max; ++i) {
137,017✔
51
    SQueueWorker *worker = pool->workers + i;
116,285✔
52
    if (taosCheckPthreadValid(worker->thread)) {
116,285!
53
      taosQsetThreadResume(pool->qset);
116,285✔
54
    }
55
  }
56

57
  for (int32_t i = 0; i < pool->max; ++i) {
137,017✔
58
    SQueueWorker *worker = pool->workers + i;
116,285✔
59
    if (taosCheckPthreadValid(worker->thread)) {
116,285!
60
      uInfo("worker:%s:%d is stopping", pool->name, worker->id);
116,285!
61
      (void)taosThreadJoin(worker->thread, NULL);
116,285✔
62
      taosThreadClear(&worker->thread);
116,285✔
63
      uInfo("worker:%s:%d is stopped", pool->name, worker->id);
116,285!
64
    }
65
  }
66

67
  taosMemoryFreeClear(pool->workers);
20,732!
68
  taosCloseQset(pool->qset);
20,732✔
69
  (void)taosThreadMutexDestroy(&pool->mutex);
20,732✔
70

71
  uInfo("worker:%s is closed", pool->name);
20,732!
72
}
20,732✔
73

74
static void *tQWorkerThreadFp(SQueueWorker *worker) {
116,253✔
75
  SQWorkerPool *pool = worker->pool;
116,253✔
76
  SQueueInfo    qinfo = {0};
116,253✔
77
  void         *msg = NULL;
116,253✔
78
  int32_t       code = 0;
116,253✔
79

80
  int32_t ret = taosBlockSIGPIPE();
116,253✔
81
  if (ret < 0) {
116,258!
82
    uError("worker:%s:%d failed to block SIGPIPE", pool->name, worker->id);
×
83
  }
84

85
  setThreadName(pool->name);
116,258✔
86
  worker->pid = taosGetSelfPthreadId();
116,251✔
87
  uInfo("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
116,250!
88

89
  while (1) {
90
    if (taosReadQitemFromQset(pool->qset, (void **)&msg, &qinfo) == 0) {
6,251,286✔
91
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, pool->qset,
116,157!
92
            worker->pid);
93
      break;
116,283✔
94
    }
95

96
    if (qinfo.timestamp != 0) {
6,133,806!
97
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
6,134,593✔
98
      if (cost > QUEUE_THRESHOLD) {
6,134,593✔
99
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pool->name, cost / QUEUE_THRESHOLD);
2,668!
100
      }
101
    }
102

103
    if (qinfo.fp != NULL) {
6,134,530✔
104
      qinfo.workerId = worker->id;
6,133,971✔
105
      qinfo.threadNum = pool->num;
6,133,971✔
106
      (*((FItem)qinfo.fp))(&qinfo, msg);
6,133,971✔
107
    }
108

109
    taosUpdateItemSize(qinfo.queue, 1);
6,135,392✔
110
  }
111

112
  DestoryThreadLocalRegComp();
116,283✔
113

114
  return NULL;
116,281✔
115
}
116

117
STaosQueue *tQWorkerAllocQueue(SQWorkerPool *pool, void *ahandle, FItem fp) {
20,732✔
118
  int32_t     code;
119
  STaosQueue *queue;
120

121
  code = taosOpenQueue(&queue);
20,732✔
122
  if (code) {
20,732!
123
    terrno = code;
×
124
    return NULL;
×
125
  }
126

127
  (void)taosThreadMutexLock(&pool->mutex);
20,732✔
128
  taosSetQueueFp(queue, fp, NULL);
20,732✔
129
  code = taosAddIntoQset(pool->qset, queue, ahandle);
20,732✔
130
  if (code) {
20,732!
131
    taosCloseQueue(queue);
×
132
    (void)taosThreadMutexUnlock(&pool->mutex);
×
133
    terrno = code;
×
134
    return NULL;
×
135
  }
136

137
  // spawn a thread to process queue
138
  if (pool->num < pool->max) {
20,732!
139
    do {
140
      SQueueWorker *worker = pool->workers + pool->num;
116,285✔
141

142
      TdThreadAttr thAttr;
143
      (void)taosThreadAttrInit(&thAttr);
116,285✔
144
      (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
116,285✔
145

146
      if (taosThreadCreate(&worker->thread, &thAttr, (ThreadFp)tQWorkerThreadFp, worker) != 0) {
116,285!
147
        taosCloseQueue(queue);
×
148
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
149
        queue = NULL;
×
150
        break;
×
151
      }
152

153
      (void)taosThreadAttrDestroy(&thAttr);
116,285✔
154
      pool->num++;
116,285✔
155
      uInfo("worker:%s:%d is launched, total:%d", pool->name, worker->id, pool->num);
116,285!
156
    } while (pool->num < pool->min);
116,285✔
157
  }
158

159
  (void)taosThreadMutexUnlock(&pool->mutex);
20,732✔
160
  uInfo("worker:%s, queue:%p is allocated, ahandle:%p", pool->name, queue, ahandle);
20,732!
161

162
  return queue;
20,732✔
163
}
164

165
void tQWorkerFreeQueue(SQWorkerPool *pool, STaosQueue *queue) {
20,732✔
166
  uInfo("worker:%s, queue:%p is freed", pool->name, queue);
20,732!
167
  taosCloseQueue(queue);
20,732✔
168
}
20,732✔
169

170
int32_t tAutoQWorkerInit(SAutoQWorkerPool *pool) {
4,606✔
171
  int32_t code;
172

173
  code = taosOpenQset(&pool->qset);
4,606✔
174
  if (code) {
4,606!
175
    return terrno = code;
×
176
  }
177

178
  pool->workers = taosArrayInit(2, sizeof(SQueueWorker *));
4,606✔
179
  if (pool->workers == NULL) {
4,606!
180
    taosCloseQset(pool->qset);
×
181
    return terrno;
×
182
  }
183

184
  (void)taosThreadMutexInit(&pool->mutex, NULL);
4,606✔
185

186
  uInfo("worker:%s is initialized as auto", pool->name);
4,606!
187
  return 0;
4,606✔
188
}
189

190
void tAutoQWorkerCleanup(SAutoQWorkerPool *pool) {
4,606✔
191
  int32_t size = taosArrayGetSize(pool->workers);
4,606✔
192
  for (int32_t i = 0; i < size; ++i) {
12,385✔
193
    SQueueWorker *worker = taosArrayGetP(pool->workers, i);
7,779✔
194
    if (taosCheckPthreadValid(worker->thread)) {
7,779!
195
      taosQsetThreadResume(pool->qset);
7,779✔
196
    }
197
  }
198

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

210
  taosArrayDestroy(pool->workers);
4,606✔
211
  taosCloseQset(pool->qset);
4,606✔
212
  (void)taosThreadMutexDestroy(&pool->mutex);
4,606✔
213

214
  uInfo("worker:%s is closed", pool->name);
4,606!
215
}
4,606✔
216

217
static void *tAutoQWorkerThreadFp(SQueueWorker *worker) {
7,779✔
218
  SAutoQWorkerPool *pool = worker->pool;
7,779✔
219
  SQueueInfo        qinfo = {0};
7,779✔
220
  void             *msg = NULL;
7,779✔
221
  int32_t           code = 0;
7,779✔
222

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

228
  setThreadName(pool->name);
7,779✔
229
  worker->pid = taosGetSelfPthreadId();
7,779✔
230
  uInfo("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
7,779!
231

232
  while (1) {
233
    if (taosReadQitemFromQset(pool->qset, (void **)&msg, &qinfo) == 0) {
380,919✔
234
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, pool->qset,
7,772!
235
            worker->pid);
236
      break;
7,779✔
237
    }
238

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

246
    if (qinfo.fp != NULL) {
373,163✔
247
      qinfo.workerId = worker->id;
373,142✔
248
      qinfo.threadNum = taosArrayGetSize(pool->workers);
373,142✔
249
      (*((FItem)qinfo.fp))(&qinfo, msg);
373,112✔
250
    }
251

252
    taosUpdateItemSize(qinfo.queue, 1);
373,167✔
253
  }
254
  DestoryThreadLocalRegComp();
7,779✔
255

256
  return NULL;
7,779✔
257
}
258

259
STaosQueue *tAutoQWorkerAllocQueue(SAutoQWorkerPool *pool, void *ahandle, FItem fp, int32_t minNum) {
24,900✔
260
  int32_t     code;
261
  STaosQueue *queue;
262

263
  code = taosOpenQueue(&queue);
24,900✔
264
  if (code) {
24,900!
265
    terrno = code;
×
266
    return NULL;
×
267
  }
268

269
  (void)taosThreadMutexLock(&pool->mutex);
24,900✔
270
  taosSetQueueFp(queue, fp, NULL);
24,900✔
271

272
  code = taosAddIntoQset(pool->qset, queue, ahandle);
24,900✔
273
  if (code) {
24,900!
274
    taosCloseQueue(queue);
×
275
    (void)taosThreadMutexUnlock(&pool->mutex);
×
276
    terrno = code;
×
277
    return NULL;
×
278
  }
279

280
  int32_t queueNum = taosGetQueueNumber(pool->qset);
24,900✔
281
  int32_t curWorkerNum = taosArrayGetSize(pool->workers);
24,900✔
282
  int32_t dstWorkerNum = ceilf(queueNum * pool->ratio);
24,900✔
283

284
  if (dstWorkerNum < minNum) {
24,900✔
285
    dstWorkerNum = minNum;
7,035✔
286
  }
287

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

302
    TdThreadAttr thAttr;
303
    (void)taosThreadAttrInit(&thAttr);
7,779✔
304
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
7,779✔
305

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

315
    (void)taosThreadAttrDestroy(&thAttr);
7,779✔
316
    int32_t numOfThreads = taosArrayGetSize(pool->workers);
7,779✔
317
    uInfo("worker:%s:%d is launched, total:%d, expect:%d", pool->name, worker->id, numOfThreads, dstWorkerNum);
7,779!
318

319
    curWorkerNum++;
7,779✔
320
  }
321

322
  (void)taosThreadMutexUnlock(&pool->mutex);
24,900✔
323
  uInfo("worker:%s, queue:%p is allocated, ahandle:%p", pool->name, queue, ahandle);
24,900!
324

325
  return queue;
24,900✔
326
}
327

328
void tAutoQWorkerFreeQueue(SAutoQWorkerPool *pool, STaosQueue *queue) {
24,898✔
329
  uInfo("worker:%s, queue:%p is freed", pool->name, queue);
24,898!
330
  taosCloseQueue(queue);
24,900✔
331
}
24,898✔
332

333
int32_t tWWorkerInit(SWWorkerPool *pool) {
56,865✔
334
  pool->nextId = 0;
56,865✔
335
  pool->workers = taosMemoryCalloc(pool->max, sizeof(SWWorker));
56,865!
336
  if (pool->workers == NULL) {
56,865!
337
    return terrno;
×
338
  }
339

340
  (void)taosThreadMutexInit(&pool->mutex, NULL);
56,865✔
341

342
  for (int32_t i = 0; i < pool->max; ++i) {
141,318✔
343
    SWWorker *worker = pool->workers + i;
84,453✔
344
    worker->id = i;
84,453✔
345
    worker->qall = NULL;
84,453✔
346
    worker->qset = NULL;
84,453✔
347
    worker->pool = pool;
84,453✔
348
  }
349

350
  uInfo("worker:%s is initialized, max:%d", pool->name, pool->max);
56,865!
351
  return 0;
56,865✔
352
}
353

354
void tWWorkerCleanup(SWWorkerPool *pool) {
56,859✔
355
  for (int32_t i = 0; i < pool->max; ++i) {
141,308✔
356
    SWWorker *worker = pool->workers + i;
84,448✔
357
    if (taosCheckPthreadValid(worker->thread)) {
84,448✔
358
      if (worker->qset) {
66,733✔
359
        taosQsetThreadResume(worker->qset);
66,732✔
360
      }
361
    }
362
  }
363

364
  for (int32_t i = 0; i < pool->max; ++i) {
141,311✔
365
    SWWorker *worker = pool->workers + i;
84,446✔
366
    if (taosCheckPthreadValid(worker->thread)) {
84,446✔
367
      uInfo("worker:%s:%d is stopping", pool->name, worker->id);
66,737!
368
      (void)taosThreadJoin(worker->thread, NULL);
66,741✔
369
      taosThreadClear(&worker->thread);
66,742✔
370
      taosFreeQall(worker->qall);
66,742✔
371
      taosCloseQset(worker->qset);
66,742✔
372
      uInfo("worker:%s:%d is stopped", pool->name, worker->id);
66,742!
373
    }
374
  }
375

376
  taosMemoryFreeClear(pool->workers);
56,865!
377
  (void)taosThreadMutexDestroy(&pool->mutex);
56,859✔
378

379
  uInfo("worker:%s is closed", pool->name);
56,857!
380
}
56,864✔
381

382
static void *tWWorkerThreadFp(SWWorker *worker) {
66,736✔
383
  SWWorkerPool *pool = worker->pool;
66,736✔
384
  SQueueInfo    qinfo = {0};
66,736✔
385
  void         *msg = NULL;
66,736✔
386
  int32_t       code = 0;
66,736✔
387
  int32_t       numOfMsgs = 0;
66,736✔
388

389
  int32_t ret = taosBlockSIGPIPE();
66,736✔
390
  if (ret < 0) {
66,723!
391
    uError("worker:%s:%d failed to block SIGPIPE", pool->name, worker->id);
×
392
  }
393

394
  setThreadName(pool->name);
66,723✔
395
  worker->pid = taosGetSelfPthreadId();
66,716✔
396
  uInfo("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
66,734!
397

398
  while (1) {
399
    numOfMsgs = taosReadAllQitemsFromQset(worker->qset, worker->qall, &qinfo);
31,992,452✔
400
    if (numOfMsgs == 0) {
31,981,844✔
401
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, worker->qset,
66,363!
402
            worker->pid);
403
      break;
66,742✔
404
    }
405

406
    if (qinfo.timestamp != 0) {
31,915,481!
407
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
31,916,704✔
408
      if (cost > QUEUE_THRESHOLD) {
31,916,704✔
409
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pool->name, cost / QUEUE_THRESHOLD);
84!
410
      }
411
    }
412

413
    if (qinfo.fp != NULL) {
31,916,438✔
414
      qinfo.workerId = worker->id;
31,909,097✔
415
      qinfo.threadNum = pool->num;
31,909,097✔
416
      (*((FItems)qinfo.fp))(&qinfo, worker->qall, numOfMsgs);
31,909,097✔
417
    }
418
    taosUpdateItemSize(qinfo.queue, numOfMsgs);
31,928,607✔
419
  }
420

421
  return NULL;
66,742✔
422
}
423

424
STaosQueue *tWWorkerAllocQueue(SWWorkerPool *pool, void *ahandle, FItems fp) {
87,306✔
425
  (void)taosThreadMutexLock(&pool->mutex);
87,306✔
426
  SWWorker   *worker = pool->workers + pool->nextId;
87,306✔
427
  int32_t     code = -1;
87,306✔
428
  STaosQueue *queue;
429

430
  code = taosOpenQueue(&queue);
87,306✔
431
  if (code) goto _OVER;
87,305!
432

433
  taosSetQueueFp(queue, NULL, fp);
87,305✔
434
  if (worker->qset == NULL) {
87,305✔
435
    code = taosOpenQset(&worker->qset);
66,741✔
436
    if (code) goto _OVER;
66,742!
437

438
    code = taosAddIntoQset(worker->qset, queue, ahandle);
66,742✔
439
    if (code) goto _OVER;
66,742!
440
    code = taosAllocateQall(&worker->qall);
66,742✔
441
    if (code) goto _OVER;
66,742!
442

443
    TdThreadAttr thAttr;
444
    (void)taosThreadAttrInit(&thAttr);
66,742✔
445
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
66,742✔
446
    code = taosThreadCreate(&worker->thread, &thAttr, (ThreadFp)tWWorkerThreadFp, worker);
66,742✔
447
    if ((code)) goto _OVER;
66,742!
448

449
    uInfo("worker:%s:%d is launched, max:%d", pool->name, worker->id, pool->max);
66,742!
450
    pool->nextId = (pool->nextId + 1) % pool->max;
66,742✔
451

452
    (void)taosThreadAttrDestroy(&thAttr);
66,742✔
453
    pool->num++;
66,742✔
454
    if (pool->num > pool->max) pool->num = pool->max;
66,742✔
455
  } else {
456
    code = taosAddIntoQset(worker->qset, queue, ahandle);
20,564✔
457
    if (code) goto _OVER;
20,564!
458
    pool->nextId = (pool->nextId + 1) % pool->max;
20,564✔
459
  }
460

461
_OVER:
87,306✔
462
  (void)taosThreadMutexUnlock(&pool->mutex);
87,306✔
463

464
  if (code) {
87,306!
465
    if (queue != NULL) taosCloseQueue(queue);
×
466
    if (worker->qset != NULL) taosCloseQset(worker->qset);
×
467
    if (worker->qall != NULL) taosFreeQall(worker->qall);
×
468
    terrno = code;
×
469
    return NULL;
×
470
  } else {
471
    while (worker->pid <= 0) taosMsleep(10);
142,482✔
472

473
    taosQueueSetThreadId(queue, worker->pid);
87,298✔
474
    uInfo("worker:%s, queue:%p is allocated, ahandle:%p thread:%08" PRId64, pool->name, queue, ahandle, worker->pid);
87,297!
475
    return queue;
87,306✔
476
  }
477
}
478

479
void tWWorkerFreeQueue(SWWorkerPool *pool, STaosQueue *queue) {
87,297✔
480
  uInfo("worker:%s, queue:%p is freed", pool->name, queue);
87,297!
481
  taosCloseQueue(queue);
87,304✔
482
}
87,295✔
483

484
int32_t tSingleWorkerInit(SSingleWorker *pWorker, const SSingleWorkerCfg *pCfg) {
23,198✔
485
  int32_t code;
486
  pWorker->poolType = pCfg->poolType;
23,198✔
487
  pWorker->name = pCfg->name;
23,198✔
488

489
  switch (pCfg->poolType) {
23,198!
490
    case QWORKER_POOL: {
20,732✔
491
      SQWorkerPool *pPool = taosMemoryCalloc(1, sizeof(SQWorkerPool));
20,732!
492
      if (!pPool) {
20,732!
493
        return terrno;
×
494
      }
495
      pPool->name = pCfg->name;
20,732✔
496
      pPool->min = pCfg->min;
20,732✔
497
      pPool->max = pCfg->max;
20,732✔
498
      pWorker->pool = pPool;
20,732✔
499
      if ((code = tQWorkerInit(pPool))) {
20,732!
500
        return (terrno = code);
×
501
      }
502

503
      pWorker->queue = tQWorkerAllocQueue(pPool, pCfg->param, pCfg->fp);
20,732✔
504
      if (pWorker->queue == NULL) {
20,732!
505
        return terrno;
×
506
      }
507
    } break;
20,732✔
508
    case QUERY_AUTO_QWORKER_POOL: {
2,466✔
509
      SQueryAutoQWorkerPool *pPool = taosMemoryCalloc(1, sizeof(SQueryAutoQWorkerPool));
2,466!
510
      if (!pPool) {
2,466!
511
        return terrno;
×
512
      }
513
      pPool->name = pCfg->name;
2,466✔
514
      pPool->min = pCfg->min;
2,466✔
515
      pPool->max = pCfg->max;
2,466✔
516
      pWorker->pool = pPool;
2,466✔
517

518
      code = tQueryAutoQWorkerInit(pPool);
2,466✔
519
      if (code) return code;
2,466!
520

521
      pWorker->queue = tQueryAutoQWorkerAllocQueue(pPool, pCfg->param, pCfg->fp);
2,466✔
522
      if (!pWorker->queue) {
2,466!
523
        return terrno;
×
524
      }
525
    } break;
2,466✔
526
    default:
×
527
      return TSDB_CODE_INVALID_PARA;
×
528
  }
529
  return 0;
23,198✔
530
}
531

532
void tSingleWorkerCleanup(SSingleWorker *pWorker) {
23,198✔
533
  if (pWorker->queue == NULL) return;
23,198!
534
  while (!taosQueueEmpty(pWorker->queue)) {
25,781✔
535
    taosMsleep(10);
2,583✔
536
  }
537

538
  switch (pWorker->poolType) {
23,198!
539
    case QWORKER_POOL:
20,732✔
540
      tQWorkerCleanup(pWorker->pool);
20,732✔
541
      tQWorkerFreeQueue(pWorker->pool, pWorker->queue);
20,732✔
542
      taosMemoryFree(pWorker->pool);
20,732!
543
      break;
20,732✔
544
    case QUERY_AUTO_QWORKER_POOL:
2,466✔
545
      tQueryAutoQWorkerCleanup(pWorker->pool);
2,466✔
546
      tQueryAutoQWorkerFreeQueue(pWorker->pool, pWorker->queue);
2,466✔
547
      taosMemoryFree(pWorker->pool);
2,466!
548
      break;
2,466✔
549
    default:
×
550
      break;
×
551
  }
552
}
553

554
int32_t tMultiWorkerInit(SMultiWorker *pWorker, const SMultiWorkerCfg *pCfg) {
49,956✔
555
  SWWorkerPool *pPool = &pWorker->pool;
49,956✔
556
  pPool->name = pCfg->name;
49,956✔
557
  pPool->max = pCfg->max;
49,956✔
558

559
  int32_t code = tWWorkerInit(pPool);
49,956✔
560
  if (code) return code;
49,956!
561

562
  pWorker->queue = tWWorkerAllocQueue(pPool, pCfg->param, pCfg->fp);
49,956✔
563
  if (pWorker->queue == NULL) {
49,956!
564
    return terrno;
×
565
  }
566

567
  pWorker->name = pCfg->name;
49,956✔
568
  return 0;
49,956✔
569
}
570

571
void tMultiWorkerCleanup(SMultiWorker *pWorker) {
49,955✔
572
  if (pWorker->queue == NULL) return;
49,955!
573

574
  while (!taosQueueEmpty(pWorker->queue)) {
54,007✔
575
    taosMsleep(10);
4,054✔
576
  }
577

578
  tWWorkerCleanup(&pWorker->pool);
49,954✔
579
  tWWorkerFreeQueue(&pWorker->pool, pWorker->queue);
49,955✔
580
}
581

582
static int32_t tQueryAutoQWorkerAddWorker(SQueryAutoQWorkerPool *pool);
583
static int32_t tQueryAutoQWorkerBeforeBlocking(void *p);
584
static int32_t tQueryAutoQWorkerRecoverFromBlocking(void *p);
585
static void    tQueryAutoQWorkerWaitingCheck(SQueryAutoQWorkerPool *pPool);
586
static bool    tQueryAutoQWorkerTryRecycleWorker(SQueryAutoQWorkerPool *pPool, SQueryAutoQWorker *pWorker);
587

588
#define GET_ACTIVE_N(int64_val)  (int32_t)((int64_val) >> 32)
589
#define GET_RUNNING_N(int64_val) (int32_t)(int64_val & 0xFFFFFFFF)
590

591
static int32_t atomicFetchSubActive(int64_t *ptr, int32_t val) {
×
592
  int64_t acutalSubVal = val;
×
593
  acutalSubVal <<= 32;
×
594
  int64_t newVal64 = atomic_fetch_sub_64(ptr, acutalSubVal);
×
595
  return GET_ACTIVE_N(newVal64);
×
596
}
597

598
static int32_t atomicFetchSubRunning(int64_t *ptr, int32_t val) { return GET_RUNNING_N(atomic_fetch_sub_64(ptr, val)); }
35,435,386✔
599

600
static int32_t atomicFetchAddActive(int64_t *ptr, int32_t val) {
1,674,160✔
601
  int64_t actualAddVal = val;
1,674,160✔
602
  actualAddVal <<= 32;
1,674,160✔
603
  int64_t newVal64 = atomic_fetch_add_64(ptr, actualAddVal);
1,674,160✔
604
  return GET_ACTIVE_N(newVal64);
1,674,160✔
605
}
606

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

609
static bool atomicCompareExchangeActive(int64_t *ptr, int32_t *expectedVal, int32_t newVal) {
18✔
610
  int64_t oldVal64 = *expectedVal, newVal64 = newVal;
18✔
611
  int32_t running = GET_RUNNING_N(*ptr);
18✔
612
  oldVal64 <<= 32;
18✔
613
  newVal64 <<= 32;
18✔
614
  oldVal64 |= running;
18✔
615
  newVal64 |= running;
18✔
616
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
18✔
617
  if (actualNewVal64 == oldVal64) {
18!
618
    return true;
18✔
619
  } else {
620
    *expectedVal = GET_ACTIVE_N(actualNewVal64);
×
621
    return false;
×
622
  }
623
}
624

625
static int64_t atomicCompareExchangeRunning(int64_t *ptr, int32_t *expectedVal, int32_t newVal) {
×
626
  int64_t oldVal64 = *expectedVal, newVal64 = newVal;
×
627
  int64_t activeShifted = GET_ACTIVE_N(*ptr);
×
628
  activeShifted <<= 32;
×
629
  oldVal64 |= activeShifted;
×
630
  newVal64 |= activeShifted;
×
631
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
×
632
  if (actualNewVal64 == oldVal64) {
×
633
    return true;
×
634
  } else {
635
    *expectedVal = GET_RUNNING_N(actualNewVal64);
×
636
    return false;
×
637
  }
638
}
639

640
static int64_t atomicCompareExchangeActiveAndRunning(int64_t *ptr, int32_t *expectedActive, int32_t newActive,
46,929,026✔
641
                                                     int32_t *expectedRunning, int32_t newRunning) {
642
  int64_t oldVal64 = *expectedActive, newVal64 = newActive;
46,929,026✔
643
  oldVal64 <<= 32;
46,929,026✔
644
  oldVal64 |= *expectedRunning;
46,929,026✔
645
  newVal64 <<= 32;
46,929,026✔
646
  newVal64 |= newRunning;
46,929,026✔
647
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
46,929,026✔
648
  if (actualNewVal64 == oldVal64) {
46,933,890✔
649
    return true;
46,815,224✔
650
  } else {
651
    *expectedActive = GET_ACTIVE_N(actualNewVal64);
118,666✔
652
    *expectedRunning = GET_RUNNING_N(actualNewVal64);
118,666✔
653
    return false;
118,666✔
654
  }
655
}
656

657
static void *tQueryAutoQWorkerThreadFp(SQueryAutoQWorker *worker) {
1,675,329✔
658
  SQueryAutoQWorkerPool *pool = worker->pool;
1,675,329✔
659
  SQueueInfo             qinfo = {0};
1,675,329✔
660
  void                  *msg = NULL;
1,675,329✔
661
  int32_t                code = 0;
1,675,329✔
662

663
  int32_t ret = taosBlockSIGPIPE();
1,675,329✔
664
  if (ret < 0) {
1,675,587!
665
    uError("worker:%s:%d failed to block SIGPIPE", pool->name, worker->id);
×
666
  }
667

668
  setThreadName(pool->name);
1,675,587✔
669
  worker->pid = taosGetSelfPthreadId();
1,674,663✔
670
  uDebug("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
1,674,258✔
671

672
  while (1) {
673
    if (taosReadQitemFromQset(pool->qset, (void **)&msg, &qinfo) == 0) {
37,112,405✔
674
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, pool->qset,
1,671,386!
675
            worker->pid);
676
      break;
1,670,380✔
677
    }
678

679
    if (qinfo.timestamp != 0) {
35,439,012!
680
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
35,438,983✔
681
      if (cost > QUEUE_THRESHOLD) {
35,438,983!
682
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pool->name, cost / QUEUE_THRESHOLD);
×
683
      }
684
    }
685

686
    tQueryAutoQWorkerWaitingCheck(pool);
35,435,687✔
687

688
    if (qinfo.fp != NULL) {
35,443,285!
689
      qinfo.workerId = worker->id;
35,443,355✔
690
      qinfo.threadNum = pool->num;
35,443,355✔
691
      qinfo.workerCb = pool->pCb;
35,443,355✔
692
      (*((FItem)qinfo.fp))(&qinfo, msg);
35,443,355✔
693
    }
694

695
    taosUpdateItemSize(qinfo.queue, 1);
35,430,429✔
696
    if (!tQueryAutoQWorkerTryRecycleWorker(pool, worker)) {
35,441,638✔
697
      uDebug("worker:%s:%d exited", pool->name, worker->id);
1,641✔
698
      break;
1,640✔
699
    }
700
  }
701

702
  DestoryThreadLocalRegComp();
1,672,020✔
703

704
  return NULL;
1,670,637✔
705
}
706

707
static bool tQueryAutoQWorkerTrySignalWaitingAfterBlock(void *p) {
41,124,418✔
708
  SQueryAutoQWorkerPool *pPool = p;
41,124,418✔
709
  bool                   ret = false;
41,124,418✔
710
  int32_t                waiting = pPool->waitingAfterBlockN;
41,124,418✔
711
  while (waiting > 0) {
41,124,418✔
712
    int32_t waitingNew = atomic_val_compare_exchange_32(&pPool->waitingAfterBlockN, waiting, waiting - 1);
59✔
713
    if (waitingNew == waiting) {
59!
714
      (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
59✔
715
      (void)taosThreadCondSignal(&pPool->waitingAfterBlockCond);
59✔
716
      (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
59✔
717
      ret = true;
219✔
718
      break;
219✔
719
    }
720
    waiting = waitingNew;
×
721
  }
722
  return ret;
41,124,578✔
723
}
724

725
static bool tQueryAutoQWorkerTrySignalWaitingBeforeProcess(void *p) {
41,123,538✔
726
  SQueryAutoQWorkerPool *pPool = p;
41,123,538✔
727
  bool                   ret = false;
41,123,538✔
728
  int32_t                waiting = pPool->waitingBeforeProcessMsgN;
41,123,538✔
729
  while (waiting > 0) {
41,123,538✔
730
    int32_t waitingNew = atomic_val_compare_exchange_32(&pPool->waitingBeforeProcessMsgN, waiting, waiting - 1);
18✔
731
    if (waitingNew == waiting) {
18!
732
      (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
18✔
733
      (void)taosThreadCondSignal(&pPool->waitingBeforeProcessMsgCond);
18✔
734
      (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
18✔
735
      ret = true;
675✔
736
      break;
675✔
737
    }
738
    waiting = waitingNew;
×
739
  }
740
  return ret;
41,124,195✔
741
}
742

743
static bool tQueryAutoQWorkerTryDecActive(void *p, int32_t minActive) {
41,122,680✔
744
  SQueryAutoQWorkerPool *pPool = p;
41,122,680✔
745
  bool                   ret = false;
41,122,680✔
746
  int64_t                val64 = pPool->activeRunningN;
41,122,680✔
747
  int32_t                active = GET_ACTIVE_N(val64), running = GET_RUNNING_N(val64);
41,122,680✔
748
  while (active > minActive) {
41,128,817✔
749
    if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active - 1, &running, running - 1))
5,691,929✔
750
      return true;
5,685,853✔
751
  }
752
  return false;
35,436,888✔
753
}
754

755
static void tQueryAutoQWorkerWaitingCheck(SQueryAutoQWorkerPool *pPool) {
35,439,498✔
756
  while (1) {
902✔
757
    int64_t val64 = pPool->activeRunningN;
35,439,498✔
758
    int32_t running = GET_RUNNING_N(val64), active = GET_ACTIVE_N(val64);
35,439,498✔
759
    while (running < pPool->num) {
35,545,219!
760
      if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active, &running, running + 1)) {
35,547,423✔
761
        return;
35,443,496✔
762
      }
763
    }
764
    if (atomicCompareExchangeActive(&pPool->activeRunningN, &active, active - 1)) {
×
765
      break;
×
766
    }
767
  }
768
  // to wait for process
769
  (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
×
770
  (void)atomic_fetch_add_32(&pPool->waitingBeforeProcessMsgN, 1);
18✔
771
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingBeforeProcessMsgCond, &pPool->waitingBeforeProcessMsgLock);
18!
772
  // recovered from waiting
773
  (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
18✔
774
  return;
18✔
775
}
776

777
bool tQueryAutoQWorkerTryRecycleWorker(SQueryAutoQWorkerPool *pPool, SQueryAutoQWorker *pWorker) {
35,440,305✔
778
  if (tQueryAutoQWorkerTrySignalWaitingAfterBlock(pPool) || tQueryAutoQWorkerTrySignalWaitingBeforeProcess(pPool) ||
70,878,724!
779
      tQueryAutoQWorkerTryDecActive(pPool, pPool->num)) {
35,439,279✔
780
    (void)taosThreadMutexLock(&pPool->poolLock);
2,452,306✔
781
    SListNode *pNode = listNode(pWorker);
2,453,050✔
782
    SListNode *tNode = tdListPopNode(pPool->workers, pNode);
2,453,050✔
783
    // reclaim some workers
784
    if (pWorker->id >= pPool->maxInUse) {
2,453,050!
785
      while (listNEles(pPool->exitedWorkers) > pPool->maxInUse - pPool->num) {
×
786
        SListNode         *head = tdListPopHead(pPool->exitedWorkers);
×
787
        SQueryAutoQWorker *pWorker = (SQueryAutoQWorker *)head->data;
×
788
        if (pWorker && taosCheckPthreadValid(pWorker->thread)) {
×
789
          (void)taosThreadJoin(pWorker->thread, NULL);
×
790
          taosThreadClear(&pWorker->thread);
×
791
        }
792
        taosMemoryFree(head);
×
793
      }
794
      tdListAppendNode(pPool->exitedWorkers, pNode);
×
795
      (void)taosThreadMutexUnlock(&pPool->poolLock);
×
796
      return false;
×
797
    }
798

799
    // put back to backup pool
800
    tdListAppendNode(pPool->backupWorkers, pNode);
2,453,050✔
801
    (void)taosThreadMutexUnlock(&pPool->poolLock);
2,453,050✔
802

803
    // start to wait at backup cond
804
    (void)taosThreadMutexLock(&pPool->backupLock);
2,453,049✔
805
    (void)atomic_fetch_add_32(&pPool->backupNum, 1);
2,453,049✔
806
    if (!pPool->exit) (void)taosThreadCondWait(&pPool->backupCond, &pPool->backupLock);
2,453,050!
807
    (void)taosThreadMutexUnlock(&pPool->backupLock);
2,453,049✔
808

809
    // recovered from backup
810
    (void)taosThreadMutexLock(&pPool->poolLock);
2,453,044✔
811
    if (pPool->exit) {
2,453,050✔
812
      (void)taosThreadMutexUnlock(&pPool->poolLock);
1,641✔
813
      return false;
1,640✔
814
    }
815
    SListNode *tNode1 = tdListPopNode(pPool->backupWorkers, pNode);
2,451,409✔
816
    tdListAppendNode(pPool->workers, pNode);
2,451,408✔
817
    (void)taosThreadMutexUnlock(&pPool->poolLock);
2,451,409✔
818

819
    return true;
2,451,409✔
820
  } else {
821
    (void)atomicFetchSubRunning(&pPool->activeRunningN, 1);
32,984,959✔
822
    return true;
32,986,203✔
823
  }
824
}
825

826
int32_t tQueryAutoQWorkerInit(SQueryAutoQWorkerPool *pool) {
22,626✔
827
  int32_t code;
828

829
  pool->exit = false;
22,626✔
830

831
  (void)taosThreadMutexInit(&pool->poolLock, NULL);
22,626✔
832
  (void)taosThreadMutexInit(&pool->backupLock, NULL);
22,626✔
833
  (void)taosThreadMutexInit(&pool->waitingAfterBlockLock, NULL);
22,626✔
834
  (void)taosThreadMutexInit(&pool->waitingBeforeProcessMsgLock, NULL);
22,626✔
835

836
  (void)taosThreadCondInit(&pool->waitingBeforeProcessMsgCond, NULL);
22,626✔
837
  (void)taosThreadCondInit(&pool->waitingAfterBlockCond, NULL);
22,626✔
838
  (void)taosThreadCondInit(&pool->backupCond, NULL);
22,626✔
839

840
  code = taosOpenQset(&pool->qset);
22,626✔
841
  if (code) return terrno = code;
22,626!
842
  pool->workers = tdListNew(sizeof(SQueryAutoQWorker));
22,626✔
843
  if (!pool->workers) return terrno;
22,626!
844
  pool->backupWorkers = tdListNew(sizeof(SQueryAutoQWorker));
22,626✔
845
  if (!pool->backupWorkers) return terrno;
22,626!
846
  pool->exitedWorkers = tdListNew(sizeof(SQueryAutoQWorker));
22,626✔
847
  if (!pool->exitedWorkers) return terrno;
22,626!
848
  pool->maxInUse = pool->max * 2 + 2;
22,626✔
849

850
  if (!pool->pCb) {
22,626!
851
    pool->pCb = taosMemoryCalloc(1, sizeof(SQueryAutoQWorkerPoolCB));
22,626!
852
    if (!pool->pCb) return terrno;
22,626!
853
    pool->pCb->pPool = pool;
22,626✔
854
    pool->pCb->beforeBlocking = tQueryAutoQWorkerBeforeBlocking;
22,626✔
855
    pool->pCb->afterRecoverFromBlocking = tQueryAutoQWorkerRecoverFromBlocking;
22,626✔
856
  }
857
  return TSDB_CODE_SUCCESS;
22,626✔
858
}
859

860
void tQueryAutoQWorkerCleanup(SQueryAutoQWorkerPool *pPool) {
22,627✔
861
  (void)taosThreadMutexLock(&pPool->poolLock);
22,627✔
862
  pPool->exit = true;
22,627✔
863
  int32_t size = 0;
22,627✔
864
  if (pPool->workers) {
22,627✔
865
    size = listNEles(pPool->workers);
22,626✔
866
  }
867
  if (pPool->backupWorkers) {
22,627✔
868
    size += listNEles(pPool->backupWorkers);
22,626✔
869
  }
870
  if (pPool->qset) {
22,627✔
871
    for (int32_t i = 0; i < size; ++i) {
1,698,425✔
872
      taosQsetThreadResume(pPool->qset);
1,675,799✔
873
    }
874
  }
875
  (void)taosThreadMutexUnlock(&pPool->poolLock);
22,627✔
876

877
  (void)taosThreadMutexLock(&pPool->backupLock);
22,627✔
878
  (void)taosThreadCondBroadcast(&pPool->backupCond);
22,627✔
879
  (void)taosThreadMutexUnlock(&pPool->backupLock);
22,627✔
880

881
  (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
22,627✔
882
  (void)taosThreadCondBroadcast(&pPool->waitingAfterBlockCond);
22,627✔
883
  (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
22,627✔
884

885
  (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
22,627✔
886
  (void)taosThreadCondBroadcast(&pPool->waitingBeforeProcessMsgCond);
22,627✔
887
  (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
22,627✔
888

889
  int32_t            idx = 0;
22,627✔
890
  SQueryAutoQWorker *worker = NULL;
22,627✔
891
  while (pPool->workers) {
1,696,785✔
892
    (void)taosThreadMutexLock(&pPool->poolLock);
1,696,784✔
893
    if (listNEles(pPool->workers) == 0) {
1,696,784✔
894
      (void)taosThreadMutexUnlock(&pPool->poolLock);
22,626✔
895
      break;
22,626✔
896
    }
897
    SListNode *pNode = tdListPopHead(pPool->workers);
1,674,158✔
898
    worker = (SQueryAutoQWorker *)pNode->data;
1,674,158✔
899
    (void)taosThreadMutexUnlock(&pPool->poolLock);
1,674,158✔
900
    if (worker && taosCheckPthreadValid(worker->thread)) {
1,674,158!
901
      (void)taosThreadJoin(worker->thread, NULL);
1,674,158✔
902
      taosThreadClear(&worker->thread);
1,674,158✔
903
    }
904
    taosMemoryFree(pNode);
1,674,158!
905
  }
906

907
  while (pPool->backupWorkers && listNEles(pPool->backupWorkers) > 0) {
24,268✔
908
    SListNode *pNode = tdListPopHead(pPool->backupWorkers);
1,641✔
909
    worker = (SQueryAutoQWorker *)pNode->data;
1,641✔
910
    if (worker && taosCheckPthreadValid(worker->thread)) {
1,641!
911
      (void)taosThreadJoin(worker->thread, NULL);
1,641✔
912
      taosThreadClear(&worker->thread);
1,641✔
913
    }
914
    taosMemoryFree(pNode);
1,641!
915
  }
916

917
  while (pPool->exitedWorkers && listNEles(pPool->exitedWorkers) > 0) {
22,627!
918
    SListNode *pNode = tdListPopHead(pPool->exitedWorkers);
×
919
    worker = (SQueryAutoQWorker *)pNode->data;
×
920
    if (worker && taosCheckPthreadValid(worker->thread)) {
×
921
      (void)taosThreadJoin(worker->thread, NULL);
×
922
      taosThreadClear(&worker->thread);
×
923
    }
924
    taosMemoryFree(pNode);
×
925
  }
926

927
  pPool->workers = tdListFree(pPool->workers);
22,627✔
928
  pPool->backupWorkers = tdListFree(pPool->backupWorkers);
22,627✔
929
  pPool->exitedWorkers = tdListFree(pPool->exitedWorkers);
22,627✔
930
  taosMemoryFree(pPool->pCb);
22,627!
931

932
  (void)taosThreadMutexDestroy(&pPool->poolLock);
22,627✔
933
  (void)taosThreadMutexDestroy(&pPool->backupLock);
22,627✔
934
  (void)taosThreadMutexDestroy(&pPool->waitingAfterBlockLock);
22,627✔
935
  (void)taosThreadMutexDestroy(&pPool->waitingBeforeProcessMsgLock);
22,627✔
936

937
  (void)taosThreadCondDestroy(&pPool->backupCond);
22,627✔
938
  (void)taosThreadCondDestroy(&pPool->waitingAfterBlockCond);
22,627✔
939
  (void)taosThreadCondDestroy(&pPool->waitingBeforeProcessMsgCond);
22,627✔
940
  taosCloseQset(pPool->qset);
22,627✔
941
}
22,627✔
942

943
STaosQueue *tQueryAutoQWorkerAllocQueue(SQueryAutoQWorkerPool *pool, void *ahandle, FItem fp) {
32,773✔
944
  STaosQueue *queue;
945
  int32_t     code = taosOpenQueue(&queue);
32,773✔
946
  if (code) {
32,773!
947
    terrno = code;
×
948
    return NULL;
×
949
  }
950

951
  (void)taosThreadMutexLock(&pool->poolLock);
32,773✔
952
  taosSetQueueFp(queue, fp, NULL);
32,773✔
953
  code = taosAddIntoQset(pool->qset, queue, ahandle);
32,773✔
954
  if (code) {
32,773!
955
    taosCloseQueue(queue);
×
956
    queue = NULL;
×
957
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
958
    return NULL;
×
959
  }
960
  SQueryAutoQWorker  worker = {0};
32,773✔
961
  SQueryAutoQWorker *pWorker = NULL;
32,773✔
962

963
  // spawn a thread to process queue
964
  if (pool->num < pool->max) {
32,773✔
965
    do {
966
      worker.id = listNEles(pool->workers);
1,674,160✔
967
      worker.backupIdx = -1;
1,674,160✔
968
      worker.pool = pool;
1,674,160✔
969
      SListNode *pNode = tdListAdd(pool->workers, &worker);
1,674,160✔
970
      if (!pNode) {
1,674,160!
971
        taosCloseQueue(queue);
×
972
        queue = NULL;
×
973
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
974
        break;
×
975
      }
976
      pWorker = (SQueryAutoQWorker *)pNode->data;
1,674,160✔
977

978
      TdThreadAttr thAttr;
979
      (void)taosThreadAttrInit(&thAttr);
1,674,160✔
980
      (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
1,674,160✔
981

982
      if (taosThreadCreate(&pWorker->thread, &thAttr, (ThreadFp)tQueryAutoQWorkerThreadFp, pWorker) != 0) {
1,674,160!
983
        taosCloseQueue(queue);
×
984
        queue = NULL;
×
985
        break;
×
986
      }
987

988
      (void)taosThreadAttrDestroy(&thAttr);
1,674,160✔
989
      pool->num++;
1,674,160✔
990
      (void)atomicFetchAddActive(&pool->activeRunningN, 1);
1,674,160✔
991
      uInfo("worker:%s:%d is launched, total:%d", pool->name, pWorker->id, pool->num);
1,674,160!
992
    } while (pool->num < pool->min);
1,674,160✔
993
  }
994

995
  (void)taosThreadMutexUnlock(&pool->poolLock);
32,773✔
996
  uInfo("worker:%s, queue:%p is allocated, ahandle:%p", pool->name, queue, ahandle);
32,773!
997

998
  return queue;
32,773✔
999
}
1000

1001
void tQueryAutoQWorkerFreeQueue(SQueryAutoQWorkerPool *pPool, STaosQueue *pQ) { taosCloseQueue(pQ); }
14,914✔
1002

1003
static int32_t tQueryAutoQWorkerAddWorker(SQueryAutoQWorkerPool *pool) {
2,452,926✔
1004
  // try backup pool
1005
  int32_t backup = pool->backupNum;
2,452,926✔
1006
  while (backup > 0) {
2,453,371✔
1007
    int32_t backupNew = atomic_val_compare_exchange_32(&pool->backupNum, backup, backup - 1);
2,451,731✔
1008
    if (backupNew == backup) {
2,451,831✔
1009
      (void)taosThreadCondSignal(&pool->backupCond);
2,451,386✔
1010
      return TSDB_CODE_SUCCESS;
2,451,402✔
1011
    }
1012
    backup = backupNew;
445✔
1013
  }
1014
  // backup pool is empty, create new
1015
  SQueryAutoQWorker *pWorker = NULL;
1,640✔
1016
  SQueryAutoQWorker  worker = {0};
1,640✔
1017
  worker.pool = pool;
1,640✔
1018
  worker.backupIdx = -1;
1,640✔
1019
  (void)taosThreadMutexLock(&pool->poolLock);
1,640✔
1020
  worker.id = listNEles(pool->workers);
1,639✔
1021
  SListNode *pNode = tdListAdd(pool->workers, &worker);
1,639✔
1022
  if (!pNode) {
1,639!
1023
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
1024
    return terrno;
×
1025
  }
1026
  (void)taosThreadMutexUnlock(&pool->poolLock);
1,639✔
1027
  pWorker = (SQueryAutoQWorker *)pNode->data;
1,639✔
1028

1029
  TdThreadAttr thAttr;
1030
  (void)taosThreadAttrInit(&thAttr);
1,639✔
1031
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
1,639✔
1032

1033
  if (taosThreadCreate(&pWorker->thread, &thAttr, (ThreadFp)tQueryAutoQWorkerThreadFp, pWorker) != 0) {
1,639!
1034
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1035
    return terrno;
×
1036
  }
1037
  (void)taosThreadAttrDestroy(&thAttr);
1,639✔
1038

1039
  return TSDB_CODE_SUCCESS;
1,639✔
1040
}
1041

1042
static int32_t tQueryAutoQWorkerBeforeBlocking(void *p) {
5,684,890✔
1043
  SQueryAutoQWorkerPool *pPool = p;
5,684,890✔
1044
  if (tQueryAutoQWorkerTrySignalWaitingAfterBlock(p) || tQueryAutoQWorkerTrySignalWaitingBeforeProcess(p) ||
11,370,454!
1045
      tQueryAutoQWorkerTryDecActive(p, pPool->num)) {
5,685,108✔
1046
  } else {
1047
    int32_t code = tQueryAutoQWorkerAddWorker(pPool);
2,452,967✔
1048
    if (code != TSDB_CODE_SUCCESS) {
2,453,038!
1049
      return code;
×
1050
    }
1051
    (void)atomicFetchSubRunning(&pPool->activeRunningN, 1);
2,453,038✔
1052
  }
1053

1054
  return TSDB_CODE_SUCCESS;
5,685,904✔
1055
}
1056

1057
static int32_t tQueryAutoQWorkerRecoverFromBlocking(void *p) {
5,685,682✔
1058
  SQueryAutoQWorkerPool *pPool = p;
5,685,682✔
1059
  int64_t                val64 = pPool->activeRunningN;
5,685,682✔
1060
  int32_t                running = GET_RUNNING_N(val64), active = GET_ACTIVE_N(val64);
5,685,682✔
1061
  while (running < pPool->num) {
5,691,760!
1062
    if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active + 1, &running, running + 1)) {
5,692,175✔
1063
      return TSDB_CODE_SUCCESS;
5,685,881✔
1064
    }
1065
  }
UNCOV
1066
  (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
×
1067
  (void)atomic_fetch_add_32(&pPool->waitingAfterBlockN, 1);
59✔
1068
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingAfterBlockCond, &pPool->waitingAfterBlockLock);
59!
1069
  (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
59✔
1070
  if (pPool->exit) return TSDB_CODE_QRY_QWORKER_QUIT;
59!
1071
  return TSDB_CODE_SUCCESS;
59✔
1072
}
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