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

taosdata / TDengine / #3542

27 Nov 2024 02:52AM UTC coverage: 60.819% (+0.04%) from 60.776%
#3542

push

travis-ci

web-flow
Merge pull request #28931 from taosdata/enh/jdbc-demo-3.0

update jdbc demo, and version history

120305 of 252779 branches covered (47.59%)

Branch coverage included in aggregate %.

201010 of 275538 relevant lines covered (72.95%)

19989893.51 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

109
    taosUpdateItemSize(qinfo.queue, 1);
5,023,361✔
110
  }
111

112
  DestoryThreadLocalRegComp();
119,362✔
113

114
  return NULL;
119,355✔
115
}
116

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

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

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

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

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

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

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

162
  return queue;
19,786✔
163
}
164

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

252
    taosUpdateItemSize(qinfo.queue, 1);
359,995✔
253
  }
254
  DestoryThreadLocalRegComp();
5,757✔
255

256
  return NULL;
5,756✔
257
}
258

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

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

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

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

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

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

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

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

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

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

322
  return queue;
13,710✔
323
}
324

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

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

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

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

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

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

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

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

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

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

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

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

395
  while (1) {
396
    numOfMsgs = taosReadAllQitemsFromQset(worker->qset, worker->qall, &qinfo);
33,983,234✔
397
    if (numOfMsgs == 0) {
33,978,501✔
398
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, worker->qset,
64,186!
399
            worker->pid);
400
      break;
64,381✔
401
    }
402

403
    if (qinfo.timestamp != 0) {
33,914,315!
404
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
33,911,160✔
405
      if (cost > QUEUE_THRESHOLD) {
33,911,160✔
406
        uWarn("worker:%s,message has been queued for too long, cost: %" PRId64 "s", pool->name, cost / QUEUE_THRESHOLD);
51!
407
      }
408
    }
409

410
    if (qinfo.fp != NULL) {
33,910,388!
411
      qinfo.workerId = worker->id;
33,911,238✔
412
      qinfo.threadNum = pool->num;
33,911,238✔
413
      (*((FItems)qinfo.fp))(&qinfo, worker->qall, numOfMsgs);
33,911,238✔
414
    }
415
    taosUpdateItemSize(qinfo.queue, numOfMsgs);
33,916,717✔
416
  }
417

418
  return NULL;
64,381✔
419
}
420

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

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

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

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

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

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

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

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

461
  if (code) {
68,698!
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);
111,321✔
469

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

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

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

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

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

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

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

529
void tSingleWorkerCleanup(SSingleWorker *pWorker) {
22,320✔
530
  if (pWorker->queue == NULL) return;
22,320!
531
  while (!taosQueueEmpty(pWorker->queue)) {
22,552✔
532
    taosMsleep(10);
232✔
533
  }
534

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

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

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

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

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

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

571
  while (!taosQueueEmpty(pWorker->queue)) {
96,554✔
572
    taosMsleep(10);
41,566✔
573
  }
574

575
  tWWorkerCleanup(&pWorker->pool);
54,988✔
576
  tWWorkerFreeQueue(&pWorker->pool, pWorker->queue);
54,988✔
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)); }
26,552,456✔
596

597
static int32_t atomicFetchAddActive(int64_t *ptr, int32_t val) {
580,048✔
598
  int64_t actualAddVal = val;
580,048✔
599
  actualAddVal <<= 32;
580,048✔
600
  int64_t newVal64 = atomic_fetch_add_64(ptr, actualAddVal);
580,048✔
601
  return GET_ACTIVE_N(newVal64);
580,048✔
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) {
14✔
607
  int64_t oldVal64 = *expectedVal, newVal64 = newVal;
14✔
608
  int32_t running = GET_RUNNING_N(*ptr);
14✔
609
  oldVal64 <<= 32;
14✔
610
  newVal64 <<= 32;
14✔
611
  oldVal64 |= running;
14✔
612
  newVal64 |= running;
14✔
613
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
14✔
614
  if (actualNewVal64 == oldVal64) {
14!
615
    return true;
14✔
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,
38,001,559✔
638
                                                     int32_t *expectedRunning, int32_t newRunning) {
639
  int64_t oldVal64 = *expectedActive, newVal64 = newActive;
38,001,559✔
640
  oldVal64 <<= 32;
38,001,559✔
641
  oldVal64 |= *expectedRunning;
38,001,559✔
642
  newVal64 <<= 32;
38,001,559✔
643
  newVal64 |= newRunning;
38,001,559✔
644
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
38,001,559✔
645
  if (actualNewVal64 == oldVal64) {
38,003,744✔
646
    return true;
37,925,178✔
647
  } else {
648
    *expectedActive = GET_ACTIVE_N(actualNewVal64);
78,566✔
649
    *expectedRunning = GET_RUNNING_N(actualNewVal64);
78,566✔
650
    return false;
78,566✔
651
  }
652
}
653

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

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

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

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

676
    if (qinfo.timestamp != 0) {
26,557,293!
677
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
26,556,553✔
678
      if (cost > QUEUE_THRESHOLD) {
26,556,553!
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);
26,555,659✔
684

685
    if (qinfo.fp != NULL) {
26,558,923!
686
      qinfo.workerId = worker->id;
26,559,006✔
687
      qinfo.threadNum = pool->num;
26,559,006✔
688
      qinfo.workerCb = pool->pCb;
26,559,006✔
689
      (*((FItem)qinfo.fp))(&qinfo, msg);
26,559,006✔
690
    }
691

692
    taosUpdateItemSize(qinfo.queue, 1);
26,543,763✔
693
    if (!tQueryAutoQWorkerTryRecycleWorker(pool, worker)) {
26,557,511✔
694
      uDebug("worker:%s:%d exited", pool->name, worker->id);
1,813✔
695
      break;
1,814✔
696
    }
697
  }
698

699
  DestoryThreadLocalRegComp();
581,596✔
700

701
  return NULL;
581,489✔
702
}
703

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

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

740
static bool tQueryAutoQWorkerTryDecActive(void *p, int32_t minActive) {
32,236,858✔
741
  SQueryAutoQWorkerPool *pPool = p;
32,236,858✔
742
  bool                   ret = false;
32,236,858✔
743
  int64_t                val64 = pPool->activeRunningN;
32,236,858✔
744
  int32_t                active = GET_ACTIVE_N(val64), running = GET_RUNNING_N(val64);
32,236,858✔
745
  while (active > minActive) {
32,239,636✔
746
    if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active - 1, &running, running - 1))
5,685,795✔
747
      return true;
5,683,101✔
748
  }
749
  return false;
26,553,841✔
750
}
751

752
static void tQueryAutoQWorkerWaitingCheck(SQueryAutoQWorkerPool *pPool) {
26,556,667✔
753
  while (1) {
488✔
754
    int64_t val64 = pPool->activeRunningN;
26,556,667✔
755
    int32_t running = GET_RUNNING_N(val64), active = GET_ACTIVE_N(val64);
26,556,667✔
756
    while (running < pPool->num) {
26,626,735!
757
      if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active, &running, running + 1)) {
26,628,563✔
758
        return;
26,559,075✔
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);
14✔
768
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingBeforeProcessMsgCond, &pPool->waitingBeforeProcessMsgLock);
14!
769
  // recovered from waiting
770
  (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
14✔
771
  return;
14✔
772
}
773

774
bool tQueryAutoQWorkerTryRecycleWorker(SQueryAutoQWorkerPool *pPool, SQueryAutoQWorker *pWorker) {
26,556,418✔
775
  if (tQueryAutoQWorkerTrySignalWaitingAfterBlock(pPool) || tQueryAutoQWorkerTrySignalWaitingBeforeProcess(pPool) ||
53,111,311!
776
      tQueryAutoQWorkerTryDecActive(pPool, pPool->num)) {
26,555,184✔
777
    (void)taosThreadMutexLock(&pPool->poolLock);
2,473,395✔
778
    SListNode *pNode = listNode(pWorker);
2,472,525✔
779
    SListNode *tNode = tdListPopNode(pPool->workers, pNode);
2,472,525✔
780
    // reclaim some workers
781
    if (pWorker->id >= pPool->maxInUse) {
2,472,525!
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,472,525✔
798
    (void)taosThreadMutexUnlock(&pPool->poolLock);
2,472,525✔
799

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

806
    // recovered from backup
807
    (void)taosThreadMutexLock(&pPool->poolLock);
2,472,516✔
808
    if (pPool->exit) {
2,472,525✔
809
      (void)taosThreadMutexUnlock(&pPool->poolLock);
1,815✔
810
      return false;
1,815✔
811
    }
812
    SListNode *tNode1 = tdListPopNode(pPool->backupWorkers, pNode);
2,470,710✔
813
    tdListAppendNode(pPool->workers, pNode);
2,470,710✔
814
    (void)taosThreadMutexUnlock(&pPool->poolLock);
2,470,709✔
815

816
    return true;
2,470,710✔
817
  } else {
818
    (void)atomicFetchSubRunning(&pPool->activeRunningN, 1);
24,081,658✔
819
    return true;
24,083,378✔
820
  }
821
}
822

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

993
  return queue;
20,347✔
994
}
995

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

998
static int32_t tQueryAutoQWorkerAddWorker(SQueryAutoQWorkerPool *pool) {
2,472,337✔
999
  // try backup pool
1000
  int32_t backup = pool->backupNum;
2,472,337✔
1001
  while (backup > 0) {
2,472,829✔
1002
    int32_t backupNew = atomic_val_compare_exchange_32(&pool->backupNum, backup, backup - 1);
2,471,041✔
1003
    if (backupNew == backup) {
2,471,155✔
1004
      (void)taosThreadCondSignal(&pool->backupCond);
2,470,663✔
1005
      return TSDB_CODE_SUCCESS;
2,470,701✔
1006
    }
1007
    backup = backupNew;
492✔
1008
  }
1009
  // backup pool is empty, create new
1010
  SQueryAutoQWorker *pWorker = NULL;
1,788✔
1011
  SQueryAutoQWorker  worker = {0};
1,788✔
1012
  worker.pool = pool;
1,788✔
1013
  worker.backupIdx = -1;
1,788✔
1014
  (void)taosThreadMutexLock(&pool->poolLock);
1,788✔
1015
  worker.id = listNEles(pool->workers);
1,813✔
1016
  SListNode *pNode = tdListAdd(pool->workers, &worker);
1,813✔
1017
  if (!pNode) {
1,813!
1018
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
1019
    return terrno;
×
1020
  }
1021
  (void)taosThreadMutexUnlock(&pool->poolLock);
1,813✔
1022
  pWorker = (SQueryAutoQWorker *)pNode->data;
1,813✔
1023

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

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

1034
  return TSDB_CODE_SUCCESS;
1,813✔
1035
}
1036

1037
static int32_t tQueryAutoQWorkerBeforeBlocking(void *p) {
5,682,741✔
1038
  SQueryAutoQWorkerPool *pPool = p;
5,682,741✔
1039
  if (tQueryAutoQWorkerTrySignalWaitingAfterBlock(p) || tQueryAutoQWorkerTrySignalWaitingBeforeProcess(p) ||
11,365,610!
1040
      tQueryAutoQWorkerTryDecActive(p, pPool->num)) {
5,682,853✔
1041
  } else {
1042
    int32_t code = tQueryAutoQWorkerAddWorker(pPool);
2,472,416✔
1043
    if (code != TSDB_CODE_SUCCESS) {
2,472,504!
1044
      return code;
×
1045
    }
1046
    (void)atomicFetchSubRunning(&pPool->activeRunningN, 1);
2,472,504✔
1047
  }
1048

1049
  return TSDB_CODE_SUCCESS;
5,683,135✔
1050
}
1051

1052
static int32_t tQueryAutoQWorkerRecoverFromBlocking(void *p) {
5,682,891✔
1053
  SQueryAutoQWorkerPool *pPool = p;
5,682,891✔
1054
  int64_t                val64 = pPool->activeRunningN;
5,682,891✔
1055
  int32_t                running = GET_RUNNING_N(val64), active = GET_ACTIVE_N(val64);
5,682,891✔
1056
  while (running < pPool->num) {
5,688,921!
1057
    if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active + 1, &running, running + 1)) {
5,689,150✔
1058
      return TSDB_CODE_SUCCESS;
5,683,116✔
1059
    }
1060
  }
1061
  (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
×
1062
  (void)atomic_fetch_add_32(&pPool->waitingAfterBlockN, 1);
35✔
1063
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingAfterBlockCond, &pPool->waitingAfterBlockLock);
35!
1064
  (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
35✔
1065
  if (pPool->exit) return TSDB_CODE_QRY_QWORKER_QUIT;
35!
1066
  return TSDB_CODE_SUCCESS;
35✔
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