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

taosdata / TDengine / #3522

07 Nov 2024 05:59AM UTC coverage: 58.216% (+1.3%) from 56.943%
#3522

push

travis-ci

web-flow
Merge pull request #28663 from taosdata/fix/3_liaohj

fix(stream): stop the underlying scan operations for stream

111884 of 248391 branches covered (45.04%)

Branch coverage included in aggregate %.

3 of 4 new or added lines in 1 file covered. (75.0%)

1164 existing lines in 134 files now uncovered.

191720 of 273118 relevant lines covered (70.2%)

13088725.13 hits per line

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

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

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

37
  (void)taosThreadMutexInit(&pool->mutex, NULL);
9,017✔
38

39
  for (int32_t i = 0; i < pool->max; ++i) {
65,167✔
40
    SQueueWorker *worker = pool->workers + i;
56,150✔
41
    worker->id = i;
56,150✔
42
    worker->pool = pool;
56,150✔
43
  }
44

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

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

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

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

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

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

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

85
  setThreadName(pool->name);
56,141✔
86
  worker->pid = taosGetSelfPthreadId();
56,136✔
87
  uInfo("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
56,135!
88

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

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

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

109
    taosUpdateItemSize(qinfo.queue, 1);
6,167,385✔
110
  }
111

112
  DestoryThreadLocalRegComp();
56,150✔
113

114
  return NULL;
56,150✔
115
}
116

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

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

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

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

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

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

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

162
  return queue;
9,017✔
163
}
164

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

170
int32_t tAutoQWorkerInit(SAutoQWorkerPool *pool) {
1,136✔
171
  int32_t code;
172

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

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

184
  (void)taosThreadMutexInit(&pool->mutex, NULL);
1,136✔
185

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

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

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

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

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

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

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

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

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

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

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

252
    taosUpdateItemSize(qinfo.queue, 1);
255,258✔
253
  }
254
  DestoryThreadLocalRegComp();
2,401✔
255

256
  return NULL;
2,401✔
257
}
258

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

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

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

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

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

285
  // spawn a thread to process queue
286
  while (curWorkerNum < dstWorkerNum) {
7,936✔
287
    SQueueWorker *worker = taosMemoryCalloc(1, sizeof(SQueueWorker));
2,401✔
288
    if (worker == NULL || taosArrayPush(pool->workers, &worker) == NULL) {
4,802!
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;
2,401✔
297
    worker->pool = pool;
2,401✔
298

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

303
    if (taosThreadCreate(&worker->thread, &thAttr, (ThreadFp)tAutoQWorkerThreadFp, worker) != 0) {
2,401!
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);
2,401✔
313
    int32_t numOfThreads = taosArrayGetSize(pool->workers);
2,401✔
314
    uInfo("worker:%s:%d is launched, total:%d, expect:%d", pool->name, worker->id, numOfThreads, dstWorkerNum);
2,401!
315

316
    curWorkerNum++;
2,401✔
317
  }
318

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

322
  return queue;
5,535✔
323
}
324

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

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

337
  (void)taosThreadMutexInit(&pool->mutex, NULL);
23,412✔
338

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

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

351
void tWWorkerCleanup(SWWorkerPool *pool) {
23,411✔
352
  for (int32_t i = 0; i < pool->max; ++i) {
57,045✔
353
    SWWorker *worker = pool->workers + i;
33,636✔
354
    if (taosCheckPthreadValid(worker->thread)) {
33,636✔
355
      if (worker->qset) {
25,885!
356
        taosQsetThreadResume(worker->qset);
25,885✔
357
      }
358
    }
359
  }
360

361
  for (int32_t i = 0; i < pool->max; ++i) {
57,045✔
362
    SWWorker *worker = pool->workers + i;
33,634✔
363
    if (taosCheckPthreadValid(worker->thread)) {
33,634✔
364
      uInfo("worker:%s:%d is stopping", pool->name, worker->id);
25,884!
365
      (void)taosThreadJoin(worker->thread, NULL);
25,886✔
366
      taosThreadClear(&worker->thread);
25,886✔
367
      taosFreeQall(worker->qall);
25,886✔
368
      taosCloseQset(worker->qset);
25,886✔
369
      uInfo("worker:%s:%d is stopped", pool->name, worker->id);
25,886!
370
    }
371
  }
372

373
  taosMemoryFreeClear(pool->workers);
23,411!
374
  (void)taosThreadMutexDestroy(&pool->mutex);
23,410✔
375

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

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

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

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

395
  while (1) {
396
    numOfMsgs = taosReadAllQitemsFromQset(worker->qset, worker->qall, &qinfo);
24,734,122✔
397
    if (numOfMsgs == 0) {
24,727,108✔
398
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, worker->qset,
25,786!
399
            worker->pid);
400
      break;
25,886✔
401
    }
402

403
    if (qinfo.timestamp != 0) {
24,701,322✔
404
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
24,703,343✔
405
      if (cost > QUEUE_THRESHOLD) {
24,703,343✔
406
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pool->name, cost / QUEUE_THRESHOLD);
8!
407
      }
408
    }
409

410
    if (qinfo.fp != NULL) {
24,703,670✔
411
      qinfo.workerId = worker->id;
24,698,189✔
412
      qinfo.threadNum = pool->num;
24,698,189✔
413
      (*((FItems)qinfo.fp))(&qinfo, worker->qall, numOfMsgs);
24,698,189✔
414
    }
415
    taosUpdateItemSize(qinfo.queue, numOfMsgs);
24,712,686✔
416
  }
417

418
  return NULL;
25,886✔
419
}
420

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

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

430
  taosSetQueueFp(queue, NULL, fp);
27,811✔
431
  if (worker->qset == NULL) {
27,811✔
432
    code = taosOpenQset(&worker->qset);
25,886✔
433
    if (code) goto _OVER;
25,886!
434

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

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

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

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

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

461
  if (code) {
27,811!
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);
48,335✔
469

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

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

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

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

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

515
      code = tQueryAutoQWorkerInit(pPool);
1,000✔
516
      if (code) return code;
1,000!
517

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

529
void tSingleWorkerCleanup(SSingleWorker *pWorker) {
10,017✔
530
  if (pWorker->queue == NULL) return;
10,017!
531
  while (!taosQueueEmpty(pWorker->queue)) {
11,543✔
532
    taosMsleep(10);
1,526✔
533
  }
534

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

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

556
  int32_t code = tWWorkerInit(pPool);
22,275✔
557
  if (code) return code;
22,276!
558

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

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

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

571
  while (!taosQueueEmpty(pWorker->queue)) {
23,612✔
572
    taosMsleep(10);
1,337✔
573
  }
574

575
  tWWorkerCleanup(&pWorker->pool);
22,275✔
576
  tWWorkerFreeQueue(&pWorker->pool, pWorker->queue);
22,276✔
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)); }
24,561,468✔
596

597
static int32_t atomicFetchAddActive(int64_t *ptr, int32_t val) {
428,864✔
598
  int64_t actualAddVal = val;
428,864✔
599
  actualAddVal <<= 32;
428,864✔
600
  int64_t newVal64 = atomic_fetch_add_64(ptr, actualAddVal);
428,864✔
601
  return GET_ACTIVE_N(newVal64);
428,864✔
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) {
×
607
  int64_t oldVal64 = *expectedVal, newVal64 = newVal;
×
608
  int32_t running = GET_RUNNING_N(*ptr);
×
609
  oldVal64 <<= 32;
×
610
  newVal64 <<= 32;
×
611
  oldVal64 |= running;
×
612
  newVal64 |= running;
×
613
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
×
614
  if (actualNewVal64 == oldVal64) {
×
615
    return true;
×
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,
31,229,947✔
638
                                                     int32_t *expectedRunning, int32_t newRunning) {
639
  int64_t oldVal64 = *expectedActive, newVal64 = newActive;
31,229,947✔
640
  oldVal64 <<= 32;
31,229,947✔
641
  oldVal64 |= *expectedRunning;
31,229,947✔
642
  newVal64 <<= 32;
31,229,947✔
643
  newVal64 |= newRunning;
31,229,947✔
644
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
31,229,947✔
645
  if (actualNewVal64 == oldVal64) {
31,231,533✔
646
    return true;
31,163,416✔
647
  } else {
648
    *expectedActive = GET_ACTIVE_N(actualNewVal64);
68,117✔
649
    *expectedRunning = GET_RUNNING_N(actualNewVal64);
68,117✔
650
    return false;
68,117✔
651
  }
652
}
653

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

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

665
  setThreadName(pool->name);
429,431✔
666
  worker->pid = taosGetSelfPthreadId();
429,269✔
667
  uDebug("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
429,199✔
668

669
  while (1) {
670
    if (taosReadQitemFromQset(pool->qset, (void **)&msg, &qinfo) == 0) {
24,991,755✔
671
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, pool->qset,
428,166!
672
            worker->pid);
673
      break;
428,761✔
674
    }
675

676
    if (qinfo.timestamp != 0) {
24,566,817!
677
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
24,565,825✔
678
      if (cost > QUEUE_THRESHOLD) {
24,565,825!
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);
24,565,406✔
684

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

692
    taosUpdateItemSize(qinfo.queue, 1);
24,557,324✔
693
    if (!tQueryAutoQWorkerTryRecycleWorker(pool, worker)) {
24,566,596✔
694
      uDebug("worker:%s:%d exited", pool->name, worker->id);
636✔
695
      break;
636✔
696
    }
697
  }
698

699
  DestoryThreadLocalRegComp();
429,397✔
700

701
  return NULL;
429,354✔
702
}
703

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

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

740
static bool tQueryAutoQWorkerTryDecActive(void *p, int32_t minActive) {
27,861,440✔
741
  SQueryAutoQWorkerPool *pPool = p;
27,861,440✔
742
  bool                   ret = false;
27,861,440✔
743
  int64_t                val64 = pPool->activeRunningN;
27,861,440✔
744
  int32_t                active = GET_ACTIVE_N(val64), running = GET_RUNNING_N(val64);
27,861,440✔
745
  while (active > minActive) {
27,863,458✔
746
    if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active - 1, &running, running - 1))
3,299,430✔
747
      return true;
3,297,643✔
748
  }
749
  return false;
24,564,028✔
750
}
751

752
static void tQueryAutoQWorkerWaitingCheck(SQueryAutoQWorkerPool *pPool) {
24,565,861✔
753
  while (1) {
386✔
754
    int64_t val64 = pPool->activeRunningN;
24,565,861✔
755
    int32_t running = GET_RUNNING_N(val64), active = GET_ACTIVE_N(val64);
24,565,861✔
756
    while (running < pPool->num) {
24,624,988!
757
      if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active, &running, running + 1)) {
24,629,249✔
758
        return;
24,568,061✔
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);
×
768
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingBeforeProcessMsgCond, &pPool->waitingBeforeProcessMsgLock);
×
769
  // recovered from waiting
770
  (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
×
771
  return;
×
772
}
773

774
bool tQueryAutoQWorkerTryRecycleWorker(SQueryAutoQWorkerPool *pPool, SQueryAutoQWorker *pWorker) {
24,564,938✔
775
  if (tQueryAutoQWorkerTrySignalWaitingAfterBlock(pPool) || tQueryAutoQWorkerTrySignalWaitingBeforeProcess(pPool) ||
49,128,918!
776
      tQueryAutoQWorkerTryDecActive(pPool, pPool->num)) {
24,564,555✔
777
    (void)taosThreadMutexLock(&pPool->poolLock);
1,611,441✔
778
    SListNode *pNode = listNode(pWorker);
1,611,150✔
779
    SListNode *tNode = tdListPopNode(pPool->workers, pNode);
1,611,150✔
780
    // reclaim some workers
781
    if (pWorker->id >= pPool->maxInUse) {
1,611,150!
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);
1,611,150✔
798
    (void)taosThreadMutexUnlock(&pPool->poolLock);
1,611,150✔
799

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

806
    // recovered from backup
807
    (void)taosThreadMutexLock(&pPool->poolLock);
1,611,142✔
808
    if (pPool->exit) {
1,611,150✔
809
      (void)taosThreadMutexUnlock(&pPool->poolLock);
637✔
810
      return false;
637✔
811
    }
812
    SListNode *tNode1 = tdListPopNode(pPool->backupWorkers, pNode);
1,610,513✔
813
    tdListAppendNode(pPool->workers, pNode);
1,610,513✔
814
    (void)taosThreadMutexUnlock(&pPool->poolLock);
1,610,513✔
815

816
    return true;
1,610,513✔
817
  } else {
818
    (void)atomicFetchSubRunning(&pPool->activeRunningN, 1);
22,951,874✔
819
    return true;
22,952,640✔
820
  }
821
}
822

823
int32_t tQueryAutoQWorkerInit(SQueryAutoQWorkerPool *pool) {
6,196✔
824
  int32_t code;
825

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

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

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

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

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

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

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

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

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

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

912
  while (pPool->exitedWorkers && listNEles(pPool->exitedWorkers) > 0) {
6,196!
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);
6,196✔
923
  pPool->backupWorkers = tdListFree(pPool->backupWorkers);
6,196✔
924
  pPool->exitedWorkers = tdListFree(pPool->exitedWorkers);
6,196✔
925
  taosMemoryFree(pPool->pCb);
6,196✔
926

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

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

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

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

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

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

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

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

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

993
  return queue;
10,595✔
994
}
995

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

998
static int32_t tQueryAutoQWorkerAddWorker(SQueryAutoQWorkerPool *pool) {
1,611,067✔
999
  // try backup pool
1000
  int32_t backup = pool->backupNum;
1,611,067✔
1001
  while (backup > 0) {
1,611,303✔
1002
    int32_t backupNew = atomic_val_compare_exchange_32(&pool->backupNum, backup, backup - 1);
1,610,673✔
1003
    if (backupNew == backup) {
1,610,734✔
1004
      (void)taosThreadCondSignal(&pool->backupCond);
1,610,498✔
1005
      return TSDB_CODE_SUCCESS;
1,610,508✔
1006
    }
1007
    backup = backupNew;
236✔
1008
  }
1009
  // backup pool is empty, create new
1010
  SQueryAutoQWorker *pWorker = NULL;
630✔
1011
  SQueryAutoQWorker  worker = {0};
630✔
1012
  worker.pool = pool;
630✔
1013
  worker.backupIdx = -1;
630✔
1014
  (void)taosThreadMutexLock(&pool->poolLock);
630✔
1015
  worker.id = listNEles(pool->workers);
637✔
1016
  SListNode *pNode = tdListAdd(pool->workers, &worker);
637✔
1017
  if (!pNode) {
637!
1018
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
1019
    return terrno;
×
1020
  }
1021
  (void)taosThreadMutexUnlock(&pool->poolLock);
637✔
1022
  pWorker = (SQueryAutoQWorker *)pNode->data;
637✔
1023

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

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

1034
  return TSDB_CODE_SUCCESS;
637✔
1035
}
1036

1037
static int32_t tQueryAutoQWorkerBeforeBlocking(void *p) {
3,297,450✔
1038
  SQueryAutoQWorkerPool *pPool = p;
3,297,450✔
1039
  if (tQueryAutoQWorkerTrySignalWaitingAfterBlock(p) || tQueryAutoQWorkerTrySignalWaitingBeforeProcess(p) ||
6,595,008!
1040
      tQueryAutoQWorkerTryDecActive(p, pPool->num)) {
3,297,467✔
1041
  } else {
1042
    int32_t code = tQueryAutoQWorkerAddWorker(pPool);
1,611,096✔
1043
    if (code != TSDB_CODE_SUCCESS) {
1,611,145!
1044
      return code;
×
1045
    }
1046
    (void)atomicFetchSubRunning(&pPool->activeRunningN, 1);
1,611,145✔
1047
  }
1048

1049
  return TSDB_CODE_SUCCESS;
3,297,638✔
1050
}
1051

1052
static int32_t tQueryAutoQWorkerRecoverFromBlocking(void *p) {
3,297,563✔
1053
  SQueryAutoQWorkerPool *pPool = p;
3,297,563✔
1054
  int64_t                val64 = pPool->activeRunningN;
3,297,563✔
1055
  int32_t                running = GET_RUNNING_N(val64), active = GET_ACTIVE_N(val64);
3,297,563✔
1056
  while (running < pPool->num) {
3,302,498!
1057
    if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active + 1, &running, running + 1)) {
3,302,545✔
1058
      return TSDB_CODE_SUCCESS;
3,297,638✔
1059
    }
1060
  }
UNCOV
1061
  (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
×
1062
  (void)atomic_fetch_add_32(&pPool->waitingAfterBlockN, 1);
×
1063
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingAfterBlockCond, &pPool->waitingAfterBlockLock);
×
1064
  (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
×
1065
  if (pPool->exit) return TSDB_CODE_QRY_QWORKER_QUIT;
×
1066
  return TSDB_CODE_SUCCESS;
×
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