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

taosdata / TDengine / #3530

16 Nov 2024 07:44AM UTC coverage: 60.219% (-0.7%) from 60.888%
#3530

push

travis-ci

web-flow
Update 03-ad.md

118417 of 252124 branches covered (46.97%)

Branch coverage included in aggregate %.

198982 of 274951 relevant lines covered (72.37%)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

109
    taosUpdateItemSize(qinfo.queue, 1);
2,557,757✔
110
  }
111

112
  DestoryThreadLocalRegComp();
116,299✔
113

114
  return NULL;
116,298✔
115
}
116

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

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

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

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

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

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

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

162
  return queue;
19,290✔
163
}
164

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

252
    taosUpdateItemSize(qinfo.queue, 1);
191,349✔
253
  }
254
  DestoryThreadLocalRegComp();
5,418✔
255

256
  return NULL;
5,418✔
257
}
258

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

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

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

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

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

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

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

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

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

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

322
  return queue;
12,271✔
323
}
324

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

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

337
  (void)taosThreadMutexInit(&pool->mutex, NULL);
51,472✔
338

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

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

351
void tWWorkerCleanup(SWWorkerPool *pool) {
51,467✔
352
  for (int32_t i = 0; i < pool->max; ++i) {
123,956✔
353
    SWWorker *worker = pool->workers + i;
72,491✔
354
    if (taosCheckPthreadValid(worker->thread)) {
72,491✔
355
      if (worker->qset) {
58,175!
356
        taosQsetThreadResume(worker->qset);
58,175✔
357
      }
358
    }
359
  }
360

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

373
  taosMemoryFreeClear(pool->workers);
51,472!
374
  (void)taosThreadMutexDestroy(&pool->mutex);
51,471✔
375

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

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

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

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

395
  while (1) {
396
    numOfMsgs = taosReadAllQitemsFromQset(worker->qset, worker->qall, &qinfo);
7,348,007✔
397
    if (numOfMsgs == 0) {
7,346,320✔
398
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, worker->qset,
57,855!
399
            worker->pid);
400
      break;
58,181✔
401
    }
402

403
    if (qinfo.timestamp != 0) {
7,288,465!
404
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
7,287,818✔
405
      if (cost > QUEUE_THRESHOLD) {
7,287,818✔
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) {
7,287,381✔
411
      qinfo.workerId = worker->id;
7,287,379✔
412
      qinfo.threadNum = pool->num;
7,287,379✔
413
      (*((FItems)qinfo.fp))(&qinfo, worker->qall, numOfMsgs);
7,287,379✔
414
    }
415
    taosUpdateItemSize(qinfo.queue, numOfMsgs);
7,289,374✔
416
  }
417

418
  return NULL;
58,181✔
419
}
420

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

427
  code = taosOpenQueue(&queue);
61,407✔
428
  if (code) goto _OVER;
61,406!
429

430
  taosSetQueueFp(queue, NULL, fp);
61,406✔
431
  if (worker->qset == NULL) {
61,407✔
432
    code = taosOpenQset(&worker->qset);
58,181✔
433
    if (code) goto _OVER;
58,180!
434

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

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

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

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

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

461
  if (code) {
61,407!
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);
108,019✔
469

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

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

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

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

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

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

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

529
void tSingleWorkerCleanup(SSingleWorker *pWorker) {
21,745✔
530
  if (pWorker->queue == NULL) return;
21,745!
531
  while (!taosQueueEmpty(pWorker->queue)) {
23,692✔
532
    taosMsleep(10);
1,947✔
533
  }
534

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

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

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

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

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

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

571
  while (!taosQueueEmpty(pWorker->queue)) {
53,137✔
572
    taosMsleep(10);
4,012✔
573
  }
574

575
  tWWorkerCleanup(&pWorker->pool);
49,134✔
576
  tWWorkerFreeQueue(&pWorker->pool, pWorker->queue);
49,135✔
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)); }
11,846,096✔
596

597
static int32_t atomicFetchAddActive(int64_t *ptr, int32_t val) {
507,136✔
598
  int64_t actualAddVal = val;
507,136✔
599
  actualAddVal <<= 32;
507,136✔
600
  int64_t newVal64 = atomic_fetch_add_64(ptr, actualAddVal);
507,136✔
601
  return GET_ACTIVE_N(newVal64);
507,136✔
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) {
11✔
607
  int64_t oldVal64 = *expectedVal, newVal64 = newVal;
11✔
608
  int32_t running = GET_RUNNING_N(*ptr);
11✔
609
  oldVal64 <<= 32;
11✔
610
  newVal64 <<= 32;
11✔
611
  oldVal64 |= running;
11✔
612
  newVal64 |= running;
11✔
613
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
11✔
614
  if (actualNewVal64 == oldVal64) {
11!
615
    return true;
11✔
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,
14,315,301✔
638
                                                     int32_t *expectedRunning, int32_t newRunning) {
639
  int64_t oldVal64 = *expectedActive, newVal64 = newActive;
14,315,301✔
640
  oldVal64 <<= 32;
14,315,301✔
641
  oldVal64 |= *expectedRunning;
14,315,301✔
642
  newVal64 <<= 32;
14,315,301✔
643
  newVal64 |= newRunning;
14,315,301✔
644
  int64_t actualNewVal64 = atomic_val_compare_exchange_64(ptr, oldVal64, newVal64);
14,315,301✔
645
  if (actualNewVal64 == oldVal64) {
14,315,847✔
646
    return true;
14,298,418✔
647
  } else {
648
    *expectedActive = GET_ACTIVE_N(actualNewVal64);
17,429✔
649
    *expectedRunning = GET_RUNNING_N(actualNewVal64);
17,429✔
650
    return false;
17,429✔
651
  }
652
}
653

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

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

665
  setThreadName(pool->name);
508,817✔
666
  worker->pid = taosGetSelfPthreadId();
508,698✔
667
  uDebug("worker:%s:%d is running, thread:%08" PRId64, pool->name, worker->id, worker->pid);
508,662✔
668

669
  while (1) {
670
    if (taosReadQitemFromQset(pool->qset, (void **)&msg, &qinfo) == 0) {
12,353,400✔
671
      uInfo("worker:%s:%d qset:%p, got no message and exiting, thread:%08" PRId64, pool->name, worker->id, pool->qset,
506,250!
672
            worker->pid);
673
      break;
507,028✔
674
    }
675

676
    if (qinfo.timestamp != 0) {
11,845,992!
677
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
11,846,034✔
678
      if (cost > QUEUE_THRESHOLD) {
11,846,034!
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);
11,845,759✔
684

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

692
    taosUpdateItemSize(qinfo.queue, 1);
11,845,312✔
693
    if (!tQueryAutoQWorkerTryRecycleWorker(pool, worker)) {
11,846,564✔
694
      uDebug("worker:%s:%d exited", pool->name, worker->id);
1,752✔
695
      break;
1,751✔
696
    }
697
  }
698

699
  DestoryThreadLocalRegComp();
508,779✔
700

701
  return NULL;
508,761✔
702
}
703

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

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

740
static bool tQueryAutoQWorkerTryDecActive(void *p, int32_t minActive) {
13,072,205✔
741
  SQueryAutoQWorkerPool *pPool = p;
13,072,205✔
742
  bool                   ret = false;
13,072,205✔
743
  int64_t                val64 = pPool->activeRunningN;
13,072,205✔
744
  int32_t                active = GET_ACTIVE_N(val64), running = GET_RUNNING_N(val64);
13,072,205✔
745
  while (active > minActive) {
13,072,663✔
746
    if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active - 1, &running, running - 1))
1,226,335✔
747
      return true;
1,225,905✔
748
  }
749
  return false;
11,846,328✔
750
}
751

752
static void tQueryAutoQWorkerWaitingCheck(SQueryAutoQWorkerPool *pPool) {
11,846,110✔
753
  while (1) {
141✔
754
    int64_t val64 = pPool->activeRunningN;
11,846,110✔
755
    int32_t running = GET_RUNNING_N(val64), active = GET_ACTIVE_N(val64);
11,846,110✔
756
    while (running < pPool->num) {
11,859,622!
757
      if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active, &running, running + 1)) {
11,860,211✔
758
        return;
11,846,599✔
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);
11✔
768
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingBeforeProcessMsgCond, &pPool->waitingBeforeProcessMsgLock);
11!
769
  // recovered from waiting
770
  (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
11✔
771
  return;
11✔
772
}
773

774
bool tQueryAutoQWorkerTryRecycleWorker(SQueryAutoQWorkerPool *pPool, SQueryAutoQWorker *pWorker) {
11,846,394✔
775
  if (tQueryAutoQWorkerTrySignalWaitingAfterBlock(pPool) || tQueryAutoQWorkerTrySignalWaitingBeforeProcess(pPool) ||
23,692,749!
776
      tQueryAutoQWorkerTryDecActive(pPool, pPool->num)) {
11,846,357✔
777
    (void)taosThreadMutexLock(&pPool->poolLock);
641,499✔
778
    SListNode *pNode = listNode(pWorker);
641,439✔
779
    SListNode *tNode = tdListPopNode(pPool->workers, pNode);
641,439✔
780
    // reclaim some workers
781
    if (pWorker->id >= pPool->maxInUse) {
641,439!
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);
641,439✔
798
    (void)taosThreadMutexUnlock(&pPool->poolLock);
641,439✔
799

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

806
    // recovered from backup
807
    (void)taosThreadMutexLock(&pPool->poolLock);
641,432✔
808
    if (pPool->exit) {
641,439✔
809
      (void)taosThreadMutexUnlock(&pPool->poolLock);
1,752✔
810
      return false;
1,751✔
811
    }
812
    SListNode *tNode1 = tdListPopNode(pPool->backupWorkers, pNode);
639,687✔
813
    tdListAppendNode(pPool->workers, pNode);
639,686✔
814
    (void)taosThreadMutexUnlock(&pPool->poolLock);
639,686✔
815

816
    return true;
639,687✔
817
  } else {
818
    (void)atomicFetchSubRunning(&pPool->activeRunningN, 1);
11,204,878✔
819
    return true;
11,205,073✔
820
  }
821
}
822

823
int32_t tQueryAutoQWorkerInit(SQueryAutoQWorkerPool *pool) {
8,079✔
824
  int32_t code;
825

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

993
  return queue;
18,014✔
994
}
995

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

998
static int32_t tQueryAutoQWorkerAddWorker(SQueryAutoQWorkerPool *pool) {
641,394✔
999
  // try backup pool
1000
  int32_t backup = pool->backupNum;
641,394✔
1001
  while (backup > 0) {
641,471✔
1002
    int32_t backupNew = atomic_val_compare_exchange_32(&pool->backupNum, backup, backup - 1);
639,721✔
1003
    if (backupNew == backup) {
639,761✔
1004
      (void)taosThreadCondSignal(&pool->backupCond);
639,684✔
1005
      return TSDB_CODE_SUCCESS;
639,683✔
1006
    }
1007
    backup = backupNew;
77✔
1008
  }
1009
  // backup pool is empty, create new
1010
  SQueryAutoQWorker *pWorker = NULL;
1,750✔
1011
  SQueryAutoQWorker  worker = {0};
1,750✔
1012
  worker.pool = pool;
1,750✔
1013
  worker.backupIdx = -1;
1,750✔
1014
  (void)taosThreadMutexLock(&pool->poolLock);
1,750✔
1015
  worker.id = listNEles(pool->workers);
1,751✔
1016
  SListNode *pNode = tdListAdd(pool->workers, &worker);
1,751✔
1017
  if (!pNode) {
1,751!
1018
    (void)taosThreadMutexUnlock(&pool->poolLock);
×
1019
    return terrno;
×
1020
  }
1021
  (void)taosThreadMutexUnlock(&pool->poolLock);
1,751✔
1022
  pWorker = (SQueryAutoQWorker *)pNode->data;
1,751✔
1023

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

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

1034
  return TSDB_CODE_SUCCESS;
1,751✔
1035
}
1036

1037
static int32_t tQueryAutoQWorkerBeforeBlocking(void *p) {
1,225,934✔
1038
  SQueryAutoQWorkerPool *pPool = p;
1,225,934✔
1039
  if (tQueryAutoQWorkerTrySignalWaitingAfterBlock(p) || tQueryAutoQWorkerTrySignalWaitingBeforeProcess(p) ||
2,451,875✔
1040
      tQueryAutoQWorkerTryDecActive(p, pPool->num)) {
1,225,916✔
1041
  } else {
1042
    int32_t code = tQueryAutoQWorkerAddWorker(pPool);
641,425✔
1043
    if (code != TSDB_CODE_SUCCESS) {
641,434!
1044
      return code;
×
1045
    }
1046
    (void)atomicFetchSubRunning(&pPool->activeRunningN, 1);
641,434✔
1047
  }
1048

1049
  return TSDB_CODE_SUCCESS;
1,225,989✔
1050
}
1051

1052
static int32_t tQueryAutoQWorkerRecoverFromBlocking(void *p) {
1,225,929✔
1053
  SQueryAutoQWorkerPool *pPool = p;
1,225,929✔
1054
  int64_t                val64 = pPool->activeRunningN;
1,225,929✔
1055
  int32_t                running = GET_RUNNING_N(val64), active = GET_ACTIVE_N(val64);
1,225,929✔
1056
  while (running < pPool->num) {
1,229,341✔
1057
    if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active + 1, &running, running + 1)) {
1,229,274✔
1058
      return TSDB_CODE_SUCCESS;
1,225,916✔
1059
    }
1060
  }
1061
  (void)taosThreadMutexLock(&pPool->waitingAfterBlockLock);
67✔
1062
  (void)atomic_fetch_add_32(&pPool->waitingAfterBlockN, 1);
74✔
1063
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingAfterBlockCond, &pPool->waitingAfterBlockLock);
74!
1064
  (void)taosThreadMutexUnlock(&pPool->waitingAfterBlockLock);
74✔
1065
  if (pPool->exit) return TSDB_CODE_QRY_QWORKER_QUIT;
74!
1066
  return TSDB_CODE_SUCCESS;
74✔
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