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

taosdata / TDengine / #3535

23 Nov 2024 02:07AM UTC coverage: 60.85% (+0.03%) from 60.825%
#3535

push

travis-ci

web-flow
Merge pull request #28893 from taosdata/doc/internal

refact: rename taos lib name

120252 of 252737 branches covered (47.58%)

Branch coverage included in aggregate %.

201187 of 275508 relevant lines covered (73.02%)

15886166.19 hits per line

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

73.46
/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) {
19,802✔
28
  int32_t code = taosOpenQset(&pool->qset);
19,802✔
29
  if (code) return code;
19,802!
30

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

37
  (void)taosThreadMutexInit(&pool->mutex, NULL);
19,802✔
38

39
  for (int32_t i = 0; i < pool->max; ++i) {
139,312✔
40
    SQueueWorker *worker = pool->workers + i;
119,510✔
41
    worker->id = i;
119,510✔
42
    worker->pool = pool;
119,510✔
43
  }
44

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

49
void tQWorkerCleanup(SQWorkerPool *pool) {
19,802✔
50
  for (int32_t i = 0; i < pool->max; ++i) {
139,312✔
51
    SQueueWorker *worker = pool->workers + i;
119,510✔
52
    if (taosCheckPthreadValid(worker->thread)) {
119,510!
53
      taosQsetThreadResume(pool->qset);
119,510✔
54
    }
55
  }
56

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

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

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

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

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

85
  setThreadName(pool->name);
119,487✔
86
  worker->pid = taosGetSelfPthreadId();
119,468✔
87
  uInfo("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
119,475!
88

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

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

103
    if (qinfo.fp != NULL) {
4,099,799✔
104
      qinfo.workerId = worker->id;
4,099,710✔
105
      qinfo.threadNum = pool->num;
4,099,710✔
106
      (*((FItem)qinfo.fp))(&qinfo, msg);
4,099,710✔
107
    }
108

109
    taosUpdateItemSize(qinfo.queue, 1);
4,099,877✔
110
  }
111

112
  DestoryThreadLocalRegComp();
119,506✔
113

114
  return NULL;
119,506✔
115
}
116

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

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

127
  (void)taosThreadMutexLock(&pool->mutex);
19,802✔
128
  taosSetQueueFp(queue, fp, NULL);
19,802✔
129
  code = taosAddIntoQset(pool->qset, queue, ahandle);
19,802✔
130
  if (code) {
19,802!
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) {
19,802!
139
    do {
140
      SQueueWorker *worker = pool->workers + pool->num;
119,510✔
141

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

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

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

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

162
  return queue;
19,802✔
163
}
164

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

170
int32_t tAutoQWorkerInit(SAutoQWorkerPool *pool) {
2,400✔
171
  int32_t code;
172

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

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

184
  (void)taosThreadMutexInit(&pool->mutex, NULL);
2,400✔
185

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

190
void tAutoQWorkerCleanup(SAutoQWorkerPool *pool) {
2,400✔
191
  int32_t size = taosArrayGetSize(pool->workers);
2,400✔
192
  for (int32_t i = 0; i < size; ++i) {
8,166✔
193
    SQueueWorker *worker = taosArrayGetP(pool->workers, i);
5,766✔
194
    if (taosCheckPthreadValid(worker->thread)) {
5,766!
195
      taosQsetThreadResume(pool->qset);
5,766✔
196
    }
197
  }
198

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

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

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

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

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

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

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

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

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

252
    taosUpdateItemSize(qinfo.queue, 1);
419,275✔
253
  }
254
  DestoryThreadLocalRegComp();
5,766✔
255

256
  return NULL;
5,766✔
257
}
258

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

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

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

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

280
  int32_t queueNum = taosGetQueueNumber(pool->qset);
13,659✔
281
  int32_t curWorkerNum = taosArrayGetSize(pool->workers);
13,659✔
282
  int32_t dstWorkerNum = ceilf(queueNum * pool->ratio);
13,659✔
283
  if (dstWorkerNum < 2) dstWorkerNum = 2;
13,659✔
284

285
  // spawn a thread to process queue
286
  while (curWorkerNum < dstWorkerNum) {
19,425✔
287
    SQueueWorker *worker = taosMemoryCalloc(1, sizeof(SQueueWorker));
5,766✔
288
    if (worker == NULL || taosArrayPush(pool->workers, &worker) == NULL) {
11,532!
289
      uError("worker:%s:%d failed to create", pool->name, curWorkerNum);
×
290
      taosMemoryFree(worker);
×
291
      taosCloseQueue(queue);
×
292
      (void)taosThreadMutexUnlock(&pool->mutex);
×
293
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
294
      return NULL;
×
295
    }
296
    worker->id = curWorkerNum;
5,766✔
297
    worker->pool = pool;
5,766✔
298

299
    TdThreadAttr thAttr;
300
    (void)taosThreadAttrInit(&thAttr);
5,766✔
301
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
5,766✔
302

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

312
    (void)taosThreadAttrDestroy(&thAttr);
5,766✔
313
    int32_t numOfThreads = taosArrayGetSize(pool->workers);
5,766✔
314
    uInfo("worker:%s:%d is launched, total:%d, expect:%d", pool->name, worker->id, numOfThreads, dstWorkerNum);
5,766!
315

316
    curWorkerNum++;
5,766✔
317
  }
318

319
  (void)taosThreadMutexUnlock(&pool->mutex);
13,659✔
320
  uInfo("worker:%s, queue:%p is allocated, ahandle:%p", pool->name, queue, ahandle);
13,659!
321

322
  return queue;
13,659✔
323
}
324

325
void tAutoQWorkerFreeQueue(SAutoQWorkerPool *pool, STaosQueue *queue) {
13,659✔
326
  uInfo("worker:%s, queue:%p is freed", pool->name, queue);
13,659!
327
  taosCloseQueue(queue);
13,659✔
328
}
13,659✔
329

330
int32_t tWWorkerInit(SWWorkerPool *pool) {
57,184✔
331
  pool->nextId = 0;
57,184✔
332
  pool->workers = taosMemoryCalloc(pool->max, sizeof(SWWorker));
57,184✔
333
  if (pool->workers == NULL) {
57,184!
334
    return terrno;
×
335
  }
336

337
  (void)taosThreadMutexInit(&pool->mutex, NULL);
57,184✔
338

339
  for (int32_t i = 0; i < pool->max; ++i) {
135,967✔
340
    SWWorker *worker = pool->workers + i;
78,784✔
341
    worker->id = i;
78,784✔
342
    worker->qall = NULL;
78,784✔
343
    worker->qset = NULL;
78,784✔
344
    worker->pool = pool;
78,784✔
345
  }
346

347
  uInfo("worker:%s is initialized, max:%d", pool->name, pool->max);
57,183!
348
  return 0;
57,184✔
349
}
350

351
void tWWorkerCleanup(SWWorkerPool *pool) {
57,178✔
352
  for (int32_t i = 0; i < pool->max; ++i) {
135,958✔
353
    SWWorker *worker = pool->workers + i;
78,778✔
354
    if (taosCheckPthreadValid(worker->thread)) {
78,778✔
355
      if (worker->qset) {
64,202!
356
        taosQsetThreadResume(worker->qset);
64,202✔
357
      }
358
    }
359
  }
360

361
  for (int32_t i = 0; i < pool->max; ++i) {
135,964✔
362
    SWWorker *worker = pool->workers + i;
78,779✔
363
    if (taosCheckPthreadValid(worker->thread)) {
78,779✔
364
      uInfo("worker:%s:%d is stopping", pool->name, worker->id);
64,210!
365
      (void)taosThreadJoin(worker->thread, NULL);
64,215✔
366
      taosThreadClear(&worker->thread);
64,215✔
367
      taosFreeQall(worker->qall);
64,215✔
368
      taosCloseQset(worker->qset);
64,215✔
369
      uInfo("worker:%s:%d is stopped", pool->name, worker->id);
64,214!
370
    }
371
  }
372

373
  taosMemoryFreeClear(pool->workers);
57,185✔
374
  (void)taosThreadMutexDestroy(&pool->mutex);
57,183✔
375

376
  uInfo("worker:%s is closed", pool->name);
57,182!
377
}
57,184✔
378

379
static void *tWWorkerThreadFp(SWWorker *worker) {
64,214✔
380
  SWWorkerPool *pool = worker->pool;
64,214✔
381
  SQueueInfo    qinfo = {0};
64,214✔
382
  void         *msg = NULL;
64,214✔
383
  int32_t       code = 0;
64,214✔
384
  int32_t       numOfMsgs = 0;
64,214✔
385

386
  int32_t ret = taosBlockSIGPIPE();
64,214✔
387
  if (ret < 0) {
64,205!
388
    uError("worker:%s:%d failed to block SIGPIPE", pool->name, worker->id);
×
389
  }
390

391
  setThreadName(pool->name);
64,205✔
392
  worker->pid = taosGetSelfPthreadId();
64,211✔
393
  uInfo("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
64,211!
394

395
  while (1) {
396
    numOfMsgs = taosReadAllQitemsFromQset(worker->qset, worker->qall, &qinfo);
31,364,494✔
397
    if (numOfMsgs == 0) {
31,357,880✔
398
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, worker->qset,
63,888!
399
            worker->pid);
400
      break;
64,215✔
401
    }
402

403
    if (qinfo.timestamp != 0) {
31,293,992!
404
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
31,290,131✔
405
      if (cost > QUEUE_THRESHOLD) {
31,290,131✔
406
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pool->name, cost / QUEUE_THRESHOLD);
26!
407
      }
408
    }
409

410
    if (qinfo.fp != NULL) {
31,289,873!
411
      qinfo.workerId = worker->id;
31,292,560✔
412
      qinfo.threadNum = pool->num;
31,292,560✔
413
      (*((FItems)qinfo.fp))(&qinfo, worker->qall, numOfMsgs);
31,292,560✔
414
    }
415
    taosUpdateItemSize(qinfo.queue, numOfMsgs);
31,297,434✔
416
  }
417

418
  return NULL;
64,215✔
419
}
420

421
STaosQueue *tWWorkerAllocQueue(SWWorkerPool *pool, void *ahandle, FItems fp) {
68,443✔
422
  (void)taosThreadMutexLock(&pool->mutex);
68,443✔
423
  SWWorker   *worker = pool->workers + pool->nextId;
68,443✔
424
  int32_t     code = -1;
68,443✔
425
  STaosQueue *queue;
426

427
  code = taosOpenQueue(&queue);
68,443✔
428
  if (code) goto _OVER;
68,443!
429

430
  taosSetQueueFp(queue, NULL, fp);
68,443✔
431
  if (worker->qset == NULL) {
68,443✔
432
    code = taosOpenQset(&worker->qset);
64,215✔
433
    if (code) goto _OVER;
64,215!
434

435
    code = taosAddIntoQset(worker->qset, queue, ahandle);
64,215✔
436
    if (code) goto _OVER;
64,215!
437
    code = taosAllocateQall(&worker->qall);
64,215✔
438
    if (code) goto _OVER;
64,215!
439

440
    TdThreadAttr thAttr;
441
    (void)taosThreadAttrInit(&thAttr);
64,215✔
442
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
64,215✔
443
    code = taosThreadCreate(&worker->thread, &thAttr, (ThreadFp)tWWorkerThreadFp, worker);
64,215✔
444
    if ((code)) goto _OVER;
64,212!
445

446
    uInfo("worker:%s:%d is launched, max:%d", pool->name, worker->id, pool->max);
64,212!
447
    pool->nextId = (pool->nextId + 1) % pool->max;
64,215✔
448

449
    (void)taosThreadAttrDestroy(&thAttr);
64,215✔
450
    pool->num++;
64,215✔
451
    if (pool->num > pool->max) pool->num = pool->max;
64,215✔
452
  } else {
453
    code = taosAddIntoQset(worker->qset, queue, ahandle);
4,228✔
454
    if (code) goto _OVER;
4,228!
455
    pool->nextId = (pool->nextId + 1) % pool->max;
4,228✔
456
  }
457

458
_OVER:
68,443✔
459
  (void)taosThreadMutexUnlock(&pool->mutex);
68,443✔
460

461
  if (code) {
68,443!
462
    if (queue != NULL) taosCloseQueue(queue);
×
463
    if (worker->qset != NULL) taosCloseQset(worker->qset);
×
464
    if (worker->qall != NULL) taosFreeQall(worker->qall);
×
465
    terrno = code;
×
466
    return NULL;
×
467
  } else {
468
    while (worker->pid <= 0) taosMsleep(10);
120,130✔
469

470
    taosQueueSetThreadId(queue, worker->pid);
68,439✔
471
    uInfo("worker:%s, queue:%p is allocated, ahandle:%p thread:%08" PRId64, pool->name, queue, ahandle, worker->pid);
68,440!
472
    return queue;
68,443✔
473
  }
474
}
475

476
void tWWorkerFreeQueue(SWWorkerPool *pool, STaosQueue *queue) {
68,443✔
477
  uInfo("worker:%s, queue:%p is freed", pool->name, queue);
68,443!
478
  taosCloseQueue(queue);
68,443✔
479
}
68,442✔
480

481
int32_t tSingleWorkerInit(SSingleWorker *pWorker, const SSingleWorkerCfg *pCfg) {
22,338✔
482
  int32_t code;
483
  pWorker->poolType = pCfg->poolType;
22,338✔
484
  pWorker->name = pCfg->name;
22,338✔
485

486
  switch (pCfg->poolType) {
22,338!
487
    case QWORKER_POOL: {
19,802✔
488
      SQWorkerPool *pPool = taosMemoryCalloc(1, sizeof(SQWorkerPool));
19,802✔
489
      if (!pPool) {
19,802!
490
        return terrno;
×
491
      }
492
      pPool->name = pCfg->name;
19,802✔
493
      pPool->min = pCfg->min;
19,802✔
494
      pPool->max = pCfg->max;
19,802✔
495
      pWorker->pool = pPool;
19,802✔
496
      if ((code = tQWorkerInit(pPool))) {
19,802!
497
        return (terrno = code);
×
498
      }
499

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

515
      code = tQueryAutoQWorkerInit(pPool);
2,536✔
516
      if (code) return code;
2,536!
517

518
      pWorker->queue = tQueryAutoQWorkerAllocQueue(pPool, pCfg->param, pCfg->fp);
2,536✔
519
      if (!pWorker->queue) {
2,536!
520
        return terrno;
×
521
      }
522
    } break;
2,536✔
523
    default:
×
524
      return TSDB_CODE_INVALID_PARA;
×
525
  }
526
  return 0;
22,338✔
527
}
528

529
void tSingleWorkerCleanup(SSingleWorker *pWorker) {
22,338✔
530
  if (pWorker->queue == NULL) return;
22,338!
531
  while (!taosQueueEmpty(pWorker->queue)) {
24,498✔
532
    taosMsleep(10);
2,160✔
533
  }
534

535
  switch (pWorker->poolType) {
22,338!
536
    case QWORKER_POOL:
19,802✔
537
      tQWorkerCleanup(pWorker->pool);
19,802✔
538
      tQWorkerFreeQueue(pWorker->pool, pWorker->queue);
19,802✔
539
      taosMemoryFree(pWorker->pool);
19,802✔
540
      break;
19,802✔
541
    case QUERY_AUTO_QWORKER_POOL:
2,536✔
542
      tQueryAutoQWorkerCleanup(pWorker->pool);
2,536✔
543
      tQueryAutoQWorkerFreeQueue(pWorker->pool, pWorker->queue);
2,536✔
544
      taosMemoryFree(pWorker->pool);
2,536✔
545
      break;
2,536✔
546
    default:
×
547
      break;
×
548
  }
549
}
550

551
int32_t tMultiWorkerInit(SMultiWorker *pWorker, const SMultiWorkerCfg *pCfg) {
54,784✔
552
  SWWorkerPool *pPool = &pWorker->pool;
54,784✔
553
  pPool->name = pCfg->name;
54,784✔
554
  pPool->max = pCfg->max;
54,784✔
555

556
  int32_t code = tWWorkerInit(pPool);
54,784✔
557
  if (code) return code;
54,784!
558

559
  pWorker->queue = tWWorkerAllocQueue(pPool, pCfg->param, pCfg->fp);
54,784✔
560
  if (pWorker->queue == NULL) {
54,784!
561
    return terrno;
×
562
  }
563

564
  pWorker->name = pCfg->name;
54,784✔
565
  return 0;
54,784✔
566
}
567

568
void tMultiWorkerCleanup(SMultiWorker *pWorker) {
54,781✔
569
  if (pWorker->queue == NULL) return;
54,781!
570

571
  while (!taosQueueEmpty(pWorker->queue)) {
58,888✔
572
    taosMsleep(10);
4,114✔
573
  }
574

575
  tWWorkerCleanup(&pWorker->pool);
54,792✔
576
  tWWorkerFreeQueue(&pWorker->pool, pWorker->queue);
54,784✔
577
}
578

579
static int32_t tQueryAutoQWorkerAddWorker(SQueryAutoQWorkerPool *pool);
580
static int32_t tQueryAutoQWorkerBeforeBlocking(void *p);
581
static int32_t tQueryAutoQWorkerRecoverFromBlocking(void *p);
582
static void    tQueryAutoQWorkerWaitingCheck(SQueryAutoQWorkerPool *pPool);
583
static bool    tQueryAutoQWorkerTryRecycleWorker(SQueryAutoQWorkerPool *pPool, SQueryAutoQWorker *pWorker);
584

585
#define GET_ACTIVE_N(int64_val)  (int32_t)((int64_val) >> 32)
586
#define GET_RUNNING_N(int64_val) (int32_t)(int64_val & 0xFFFFFFFF)
587

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

595
static int32_t atomicFetchSubRunning(int64_t *ptr, int32_t val) { return GET_RUNNING_N(atomic_fetch_sub_64(ptr, val)); }
25,177,663✔
596

597
static int32_t atomicFetchAddActive(int64_t *ptr, int32_t val) {
578,704✔
598
  int64_t actualAddVal = val;
578,704✔
599
  actualAddVal <<= 32;
578,704✔
600
  int64_t newVal64 = atomic_fetch_add_64(ptr, actualAddVal);
578,704✔
601
  return GET_ACTIVE_N(newVal64);
578,704✔
602
}
603

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

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

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

637
static int64_t atomicCompareExchangeActiveAndRunning(int64_t *ptr, int32_t *expectedActive, int32_t newActive,
33,943,742✔
638
                                                     int32_t *expectedRunning, int32_t newRunning) {
639
  int64_t oldVal64 = *expectedActive, newVal64 = newActive;
33,943,742✔
640
  oldVal64 <<= 32;
33,943,742✔
641
  oldVal64 |= *expectedRunning;
33,943,742✔
642
  newVal64 <<= 32;
33,943,742✔
643
  newVal64 |= newRunning;
33,943,742✔
644
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
33,943,742✔
645
  if (actualNewVal64 == oldVal64) {
33,946,428✔
646
    return true;
33,850,594✔
647
  } else {
648
    *expectedActive = GET_ACTIVE_N(actualNewVal64);
95,834✔
649
    *expectedRunning = GET_RUNNING_N(actualNewVal64);
95,834✔
650
    return false;
95,834✔
651
  }
652
}
653

654
static void *tQueryAutoQWorkerThreadFp(SQueryAutoQWorker *worker) {
580,320✔
655
  SQueryAutoQWorkerPool *pool = worker->pool;
580,320✔
656
  SQueueInfo             qinfo = {0};
580,320✔
657
  void                  *msg = NULL;
580,320✔
658
  int32_t                code = 0;
580,320✔
659

660
  int32_t ret = taosBlockSIGPIPE();
580,320✔
661
  if (ret < 0) {
580,368!
662
    uError("worker:%s:%d failed to block SIGPIPE", pool->name, worker->id);
×
663
  }
664

665
  setThreadName(pool->name);
580,368✔
666
  worker->pid = taosGetSelfPthreadId();
580,283✔
667
  uDebug("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
580,213✔
668

669
  while (1) {
670
    if (taosReadQitemFromQset(pool->qset, (void **)&msg, &qinfo) == 0) {
25,759,437✔
671
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, pool->qset,
577,740!
672
            worker->pid);
673
      break;
578,494✔
674
    }
675

676
    if (qinfo.timestamp != 0) {
25,182,697!
677
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
25,181,433✔
678
      if (cost > QUEUE_THRESHOLD) {
25,181,433!
679
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pool->name, cost / QUEUE_THRESHOLD);
×
680
      }
681
    }
682

683
    tQueryAutoQWorkerWaitingCheck(pool);
25,180,894✔
684

685
    if (qinfo.fp != NULL) {
25,184,091!
686
      qinfo.workerId = worker->id;
25,184,110✔
687
      qinfo.threadNum = pool->num;
25,184,110✔
688
      qinfo.workerCb = pool->pCb;
25,184,110✔
689
      (*((FItem)qinfo.fp))(&qinfo, msg);
25,184,110✔
690
    }
691

692
    taosUpdateItemSize(qinfo.queue, 1);
25,172,159✔
693
    if (!tQueryAutoQWorkerTryRecycleWorker(pool, worker)) {
25,182,584✔
694
      uDebug("worker:%s:%d exited", pool->name, worker->id);
1,774✔
695
      break;
1,774✔
696
    }
697
  }
698

699
  DestoryThreadLocalRegComp();
580,268✔
700

701
  return NULL;
580,301✔
702
}
703

704
static bool tQueryAutoQWorkerTrySignalWaitingAfterBlock(void *p) {
29,513,858✔
705
  SQueryAutoQWorkerPool *pPool = p;
29,513,858✔
706
  bool                   ret = false;
29,513,858✔
707
  int32_t                waiting = pPool->waitingAfterBlockN;
29,513,858✔
708
  while (waiting > 0) {
29,513,858✔
709
    int32_t waitingNew = atomic_val_compare_exchange_32(&pPool->waitingAfterBlockN, waiting, waiting - 1);
64✔
710
    if (waitingNew == waiting) {
64!
711
      (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
64✔
712
      (void)taosThreadCondSignal(&pPool->waitingAfterBlockCond);
64✔
713
      (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
64✔
714
      ret = true;
620✔
715
      break;
620✔
716
    }
717
    waiting = waitingNew;
×
718
  }
719
  return ret;
29,514,414✔
720
}
721

722
static bool tQueryAutoQWorkerTrySignalWaitingBeforeProcess(void *p) {
29,514,059✔
723
  SQueryAutoQWorkerPool *pPool = p;
29,514,059✔
724
  bool                   ret = false;
29,514,059✔
725
  int32_t                waiting = pPool->waitingBeforeProcessMsgN;
29,514,059✔
726
  while (waiting > 0) {
29,514,059✔
727
    int32_t waitingNew = atomic_val_compare_exchange_32(&pPool->waitingBeforeProcessMsgN, waiting, waiting - 1);
21✔
728
    if (waitingNew == waiting) {
21!
729
      (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
21✔
730
      (void)taosThreadCondSignal(&pPool->waitingBeforeProcessMsgCond);
21✔
731
      (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
21✔
732
      ret = true;
478✔
733
      break;
478✔
734
    }
735
    waiting = waitingNew;
×
736
  }
737
  return ret;
29,514,516✔
738
}
739

740
static bool tQueryAutoQWorkerTryDecActive(void *p, int32_t minActive) {
29,512,342✔
741
  SQueryAutoQWorkerPool *pPool = p;
29,512,342✔
742
  bool                   ret = false;
29,512,342✔
743
  int64_t                val64 = pPool->activeRunningN;
29,512,342✔
744
  int32_t                active = GET_ACTIVE_N(val64), running = GET_RUNNING_N(val64);
29,512,342✔
745
  while (active > minActive) {
29,514,105✔
746
    if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active - 1, &running, running - 1))
4,334,940✔
747
      return true;
4,333,194✔
748
  }
749
  return false;
25,179,165✔
750
}
751

752
static void tQueryAutoQWorkerWaitingCheck(SQueryAutoQWorkerPool *pPool) {
25,181,572✔
753
  while (1) {
469✔
754
    int64_t val64 = pPool->activeRunningN;
25,181,572✔
755
    int32_t running = GET_RUNNING_N(val64), active = GET_ACTIVE_N(val64);
25,181,572✔
756
    while (running < pPool->num) {
25,267,997!
757
      if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active, &running, running + 1)) {
25,273,588✔
758
        return;
25,184,185✔
759
      }
760
    }
761
    if (atomicCompareExchangeActive(&pPool->activeRunningN, &active, active - 1)) {
×
762
      break;
×
763
    }
764
  }
765
  // to wait for process
766
  (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
×
767
  (void)atomic_fetch_add_32(&pPool->waitingBeforeProcessMsgN, 1);
21✔
768
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingBeforeProcessMsgCond, &pPool->waitingBeforeProcessMsgLock);
21!
769
  // recovered from waiting
770
  (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
21✔
771
  return;
21✔
772
}
773

774
bool tQueryAutoQWorkerTryRecycleWorker(SQueryAutoQWorkerPool *pPool, SQueryAutoQWorker *pWorker) {
25,181,433✔
775
  if (tQueryAutoQWorkerTrySignalWaitingAfterBlock(pPool) || tQueryAutoQWorkerTrySignalWaitingBeforeProcess(pPool) ||
50,361,147!
776
      tQueryAutoQWorkerTryDecActive(pPool, pPool->num)) {
25,180,616✔
777
    (void)taosThreadMutexLock(&pPool->poolLock);
2,110,148✔
778
    SListNode *pNode = listNode(pWorker);
2,109,408✔
779
    SListNode *tNode = tdListPopNode(pPool->workers, pNode);
2,109,408✔
780
    // reclaim some workers
781
    if (pWorker->id >= pPool->maxInUse) {
2,109,408!
782
      while (listNEles(pPool->exitedWorkers) > pPool->maxInUse - pPool->num) {
×
783
        SListNode         *head = tdListPopHead(pPool->exitedWorkers);
×
784
        SQueryAutoQWorker *pWorker = (SQueryAutoQWorker *)head->data;
×
785
        if (pWorker && taosCheckPthreadValid(pWorker->thread)) {
×
786
          (void)taosThreadJoin(pWorker->thread, NULL);
×
787
          taosThreadClear(&pWorker->thread);
×
788
        }
789
        taosMemoryFree(head);
×
790
      }
791
      tdListAppendNode(pPool->exitedWorkers, pNode);
×
792
      (void)taosThreadMutexUnlock(&pPool->poolLock);
×
793
      return false;
×
794
    }
795

796
    // put back to backup pool
797
    tdListAppendNode(pPool->backupWorkers, pNode);
2,109,408✔
798
    (void)taosThreadMutexUnlock(&pPool->poolLock);
2,109,408✔
799

800
    // start to wait at backup cond
801
    (void)taosThreadMutexLock(&pPool->backupLock);
2,109,408✔
802
    (void)atomic_fetch_add_32(&pPool->backupNum, 1);
2,109,408✔
803
    if (!pPool->exit) (void)taosThreadCondWait(&pPool->backupCond, &pPool->backupLock);
2,109,408!
804
    (void)taosThreadMutexUnlock(&pPool->backupLock);
2,109,408✔
805

806
    // recovered from backup
807
    (void)taosThreadMutexLock(&pPool->poolLock);
2,109,399✔
808
    if (pPool->exit) {
2,109,408✔
809
      (void)taosThreadMutexUnlock(&pPool->poolLock);
1,774✔
810
      return false;
1,773✔
811
    }
812
    SListNode *tNode1 = tdListPopNode(pPool->backupWorkers, pNode);
2,107,634✔
813
    tdListAppendNode(pPool->workers, pNode);
2,107,634✔
814
    (void)taosThreadMutexUnlock(&pPool->poolLock);
2,107,634✔
815

816
    return true;
2,107,634✔
817
  } else {
818
    (void)atomicFetchSubRunning(&pPool->activeRunningN, 1);
23,069,748✔
819
    return true;
23,072,028✔
820
  }
821
}
822

823
int32_t tQueryAutoQWorkerInit(SQueryAutoQWorkerPool *pool) {
9,018✔
824
  int32_t code;
825

826
  (void)taosThreadMutexInit(&pool->poolLock, NULL);
9,018✔
827
  (void)taosThreadMutexInit(&pool->backupLock, NULL);
9,018✔
828
  (void)taosThreadMutexInit(&pool->waitingAfterBlockLock, NULL);
9,018✔
829
  (void)taosThreadMutexInit(&pool->waitingBeforeProcessMsgLock, NULL);
9,018✔
830

831
  (void)taosThreadCondInit(&pool->waitingBeforeProcessMsgCond, NULL);
9,018✔
832
  (void)taosThreadCondInit(&pool->waitingAfterBlockCond, NULL);
9,018✔
833
  (void)taosThreadCondInit(&pool->backupCond, NULL);
9,018✔
834

835
  code = taosOpenQset(&pool->qset);
9,018✔
836
  if (code) return terrno = code;
9,018!
837
  pool->workers = tdListNew(sizeof(SQueryAutoQWorker));
9,018✔
838
  if (!pool->workers) return terrno;
9,018!
839
  pool->backupWorkers = tdListNew(sizeof(SQueryAutoQWorker));
9,018✔
840
  if (!pool->backupWorkers) return terrno;
9,018!
841
  pool->exitedWorkers = tdListNew(sizeof(SQueryAutoQWorker));
9,018✔
842
  if (!pool->exitedWorkers) return terrno;
9,018!
843
  pool->maxInUse = pool->max * 2 + 2;
9,018✔
844

845
  if (!pool->pCb) {
9,018!
846
    pool->pCb = taosMemoryCalloc(1, sizeof(SQueryAutoQWorkerPoolCB));
9,018✔
847
    if (!pool->pCb) return terrno;
9,018!
848
    pool->pCb->pPool = pool;
9,018✔
849
    pool->pCb->beforeBlocking = tQueryAutoQWorkerBeforeBlocking;
9,018✔
850
    pool->pCb->afterRecoverFromBlocking = tQueryAutoQWorkerRecoverFromBlocking;
9,018✔
851
  }
852
  return TSDB_CODE_SUCCESS;
9,018✔
853
}
854

855
void tQueryAutoQWorkerCleanup(SQueryAutoQWorkerPool *pPool) {
9,018✔
856
  (void)taosThreadMutexLock(&pPool->poolLock);
9,018✔
857
  pPool->exit = true;
9,018✔
858
  int32_t size = 0;
9,018✔
859
  if (pPool->workers) {
9,018!
860
    size = listNEles(pPool->workers);
9,018✔
861
  }
862
  if (pPool->backupWorkers) {
9,018!
863
    size += listNEles(pPool->backupWorkers);
9,018✔
864
  }
865
  if (pPool->qset) {
9,018!
866
    for (int32_t i = 0; i < size; ++i) {
589,496✔
867
      taosQsetThreadResume(pPool->qset);
580,478✔
868
    }
869
  }
870
  (void)taosThreadMutexUnlock(&pPool->poolLock);
9,018✔
871

872
  (void)taosThreadMutexLock(&pPool->backupLock);
9,018✔
873
  (void)taosThreadCondBroadcast(&pPool->backupCond);
9,018✔
874
  (void)taosThreadMutexUnlock(&pPool->backupLock);
9,018✔
875

876
  (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
9,018✔
877
  (void)taosThreadCondBroadcast(&pPool->waitingAfterBlockCond);
9,018✔
878
  (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
9,018✔
879

880
  (void)taosThreadMutexLock(&pPool->waitingBeforeProcessMsgLock);
9,018✔
881
  (void)taosThreadCondBroadcast(&pPool->waitingBeforeProcessMsgCond);
9,018✔
882
  (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
9,018✔
883

884
  int32_t            idx = 0;
9,018✔
885
  SQueryAutoQWorker *worker = NULL;
9,018✔
886
  while (pPool->workers) {
587,722!
887
    (void)taosThreadMutexLock(&pPool->poolLock);
587,722✔
888
    if (listNEles(pPool->workers) == 0) {
587,722✔
889
      (void)taosThreadMutexUnlock(&pPool->poolLock);
9,018✔
890
      break;
9,018✔
891
    }
892
    SListNode *pNode = tdListPopHead(pPool->workers);
578,704✔
893
    worker = (SQueryAutoQWorker *)pNode->data;
578,704✔
894
    (void)taosThreadMutexUnlock(&pPool->poolLock);
578,704✔
895
    if (worker && taosCheckPthreadValid(worker->thread)) {
578,704!
896
      (void)taosThreadJoin(worker->thread, NULL);
578,704✔
897
      taosThreadClear(&worker->thread);
578,704✔
898
    }
899
    taosMemoryFree(pNode);
578,704✔
900
  }
901

902
  while (pPool->backupWorkers && listNEles(pPool->backupWorkers) > 0) {
10,792!
903
    SListNode *pNode = tdListPopHead(pPool->backupWorkers);
1,774✔
904
    worker = (SQueryAutoQWorker *)pNode->data;
1,774✔
905
    if (worker && taosCheckPthreadValid(worker->thread)) {
1,774!
906
      (void)taosThreadJoin(worker->thread, NULL);
1,774✔
907
      taosThreadClear(&worker->thread);
1,774✔
908
    }
909
    taosMemoryFree(pNode);
1,774✔
910
  }
911

912
  while (pPool->exitedWorkers && listNEles(pPool->exitedWorkers) > 0) {
9,018!
913
    SListNode *pNode = tdListPopHead(pPool->exitedWorkers);
×
914
    worker = (SQueryAutoQWorker *)pNode->data;
×
915
    if (worker && taosCheckPthreadValid(worker->thread)) {
×
916
      (void)taosThreadJoin(worker->thread, NULL);
×
917
      taosThreadClear(&worker->thread);
×
918
    }
919
    taosMemoryFree(pNode);
×
920
  }
921

922
  pPool->workers = tdListFree(pPool->workers);
9,018✔
923
  pPool->backupWorkers = tdListFree(pPool->backupWorkers);
9,018✔
924
  pPool->exitedWorkers = tdListFree(pPool->exitedWorkers);
9,018✔
925
  taosMemoryFree(pPool->pCb);
9,018✔
926

927
  (void)taosThreadMutexDestroy(&pPool->poolLock);
9,018✔
928
  (void)taosThreadMutexDestroy(&pPool->backupLock);
9,018✔
929
  (void)taosThreadMutexDestroy(&pPool->waitingAfterBlockLock);
9,018✔
930
  (void)taosThreadMutexDestroy(&pPool->waitingBeforeProcessMsgLock);
9,018✔
931

932
  (void)taosThreadCondDestroy(&pPool->backupCond);
9,018✔
933
  (void)taosThreadCondDestroy(&pPool->waitingAfterBlockCond);
9,018✔
934
  (void)taosThreadCondDestroy(&pPool->waitingBeforeProcessMsgCond);
9,018✔
935
  taosCloseQset(pPool->qset);
9,018✔
936
}
9,018✔
937

938
STaosQueue *tQueryAutoQWorkerAllocQueue(SQueryAutoQWorkerPool *pool, void *ahandle, FItem fp) {
20,277✔
939
  STaosQueue *queue;
940
  int32_t     code = taosOpenQueue(&queue);
20,277✔
941
  if (code) {
20,277!
942
    terrno = code;
×
943
    return NULL;
×
944
  }
945

946
  (void)taosThreadMutexLock(&pool->poolLock);
20,277✔
947
  taosSetQueueFp(queue, fp, NULL);
20,277✔
948
  code = taosAddIntoQset(pool->qset, queue, ahandle);
20,277✔
949
  if (code) {
20,277!
950
    taosCloseQueue(queue);
×
951
    queue = NULL;
×
952
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
953
    return NULL;
×
954
  }
955
  SQueryAutoQWorker  worker = {0};
20,277✔
956
  SQueryAutoQWorker *pWorker = NULL;
20,277✔
957

958
  // spawn a thread to process queue
959
  if (pool->num < pool->max) {
20,277✔
960
    do {
961
      worker.id = listNEles(pool->workers);
578,704✔
962
      worker.backupIdx = -1;
578,704✔
963
      worker.pool = pool;
578,704✔
964
      SListNode *pNode = tdListAdd(pool->workers, &worker);
578,704✔
965
      if (!pNode) {
578,704!
966
        taosCloseQueue(queue);
×
967
        queue = NULL;
×
968
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
969
        break;
×
970
      }
971
      pWorker = (SQueryAutoQWorker *)pNode->data;
578,704✔
972

973
      TdThreadAttr thAttr;
974
      (void)taosThreadAttrInit(&thAttr);
578,704✔
975
      (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
578,704✔
976

977
      if (taosThreadCreate(&pWorker->thread, &thAttr, (ThreadFp)tQueryAutoQWorkerThreadFp, pWorker) != 0) {
578,704!
978
        taosCloseQueue(queue);
×
979
        queue = NULL;
×
980
        break;
×
981
      }
982

983
      (void)taosThreadAttrDestroy(&thAttr);
578,704✔
984
      pool->num++;
578,704✔
985
      (void)atomicFetchAddActive(&pool->activeRunningN, 1);
578,704✔
986
      uInfo("worker:%s:%d is launched, total:%d", pool->name, pWorker->id, pool->num);
578,704!
987
    } while (pool->num < pool->min);
578,704✔
988
  }
989

990
  (void)taosThreadMutexUnlock(&pool->poolLock);
20,277✔
991
  uInfo("worker:%s, queue:%p is allocated, ahandle:%p", pool->name, queue, ahandle);
20,277!
992

993
  return queue;
20,277✔
994
}
995

996
void tQueryAutoQWorkerFreeQueue(SQueryAutoQWorkerPool *pPool, STaosQueue *pQ) { taosCloseQueue(pQ); }
16,195✔
997

998
static int32_t tQueryAutoQWorkerAddWorker(SQueryAutoQWorkerPool *pool) {
2,109,294✔
999
  // try backup pool
1000
  int32_t backup = pool->backupNum;
2,109,294✔
1001
  while (backup > 0) {
2,109,547✔
1002
    int32_t backupNew = atomic_val_compare_exchange_32(&pool->backupNum, backup, backup - 1);
2,107,786✔
1003
    if (backupNew == backup) {
2,107,869✔
1004
      (void)taosThreadCondSignal(&pool->backupCond);
2,107,616✔
1005
      return TSDB_CODE_SUCCESS;
2,107,628✔
1006
    }
1007
    backup = backupNew;
253✔
1008
  }
1009
  // backup pool is empty, create new
1010
  SQueryAutoQWorker *pWorker = NULL;
1,761✔
1011
  SQueryAutoQWorker  worker = {0};
1,761✔
1012
  worker.pool = pool;
1,761✔
1013
  worker.backupIdx = -1;
1,761✔
1014
  (void)taosThreadMutexLock(&pool->poolLock);
1,761✔
1015
  worker.id = listNEles(pool->workers);
1,774✔
1016
  SListNode *pNode = tdListAdd(pool->workers, &worker);
1,774✔
1017
  if (!pNode) {
1,774!
1018
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
1019
    return terrno;
×
1020
  }
1021
  (void)taosThreadMutexUnlock(&pool->poolLock);
1,774✔
1022
  pWorker = (SQueryAutoQWorker *)pNode->data;
1,774✔
1023

1024
  TdThreadAttr thAttr;
1025
  (void)taosThreadAttrInit(&thAttr);
1,774✔
1026
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
1,774✔
1027

1028
  if (taosThreadCreate(&pWorker->thread, &thAttr, (ThreadFp)tQueryAutoQWorkerThreadFp, pWorker) != 0) {
1,774!
1029
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1030
    return terrno;
×
1031
  }
1032
  (void)taosThreadAttrDestroy(&thAttr);
1,774✔
1033

1034
  return TSDB_CODE_SUCCESS;
1,774✔
1035
}
1036

1037
static int32_t tQueryAutoQWorkerBeforeBlocking(void *p) {
4,333,058✔
1038
  SQueryAutoQWorkerPool *pPool = p;
4,333,058✔
1039
  if (tQueryAutoQWorkerTrySignalWaitingAfterBlock(p) || tQueryAutoQWorkerTrySignalWaitingBeforeProcess(p) ||
8,666,183!
1040
      tQueryAutoQWorkerTryDecActive(p, pPool->num)) {
4,333,090✔
1041
  } else {
1042
    int32_t code = tQueryAutoQWorkerAddWorker(pPool);
2,109,346✔
1043
    if (code != TSDB_CODE_SUCCESS) {
2,109,397!
1044
      return code;
×
1045
    }
1046
    (void)atomicFetchSubRunning(&pPool->activeRunningN, 1);
2,109,397✔
1047
  }
1048

1049
  return TSDB_CODE_SUCCESS;
4,333,269✔
1050
}
1051

1052
static int32_t tQueryAutoQWorkerRecoverFromBlocking(void *p) {
4,333,132✔
1053
  SQueryAutoQWorkerPool *pPool = p;
4,333,132✔
1054
  int64_t                val64 = pPool->activeRunningN;
4,333,132✔
1055
  int32_t                running = GET_RUNNING_N(val64), active = GET_ACTIVE_N(val64);
4,333,132✔
1056
  while (running < pPool->num) {
4,337,606!
1057
    if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active + 1, &running, running + 1)) {
4,337,690✔
1058
      return TSDB_CODE_SUCCESS;
4,333,208✔
1059
    }
1060
  }
1061
  (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
×
1062
  (void)atomic_fetch_add_32(&pPool->waitingAfterBlockN, 1);
64✔
1063
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingAfterBlockCond, &pPool->waitingAfterBlockLock);
64!
1064
  (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
64✔
1065
  if (pPool->exit) return TSDB_CODE_QRY_QWORKER_QUIT;
64!
1066
  return TSDB_CODE_SUCCESS;
64✔
1067
}
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