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

taosdata / TDengine / #3531

19 Nov 2024 10:42AM UTC coverage: 60.213% (-0.006%) from 60.219%
#3531

push

travis-ci

web-flow
Merge pull request #28777 from taosdata/fix/3.0/TD-32366

fix:TD-32366/stmt add geometry datatype check

118529 of 252344 branches covered (46.97%)

Branch coverage included in aggregate %.

7 of 48 new or added lines in 3 files covered. (14.58%)

2282 existing lines in 115 files now uncovered.

199096 of 275161 relevant lines covered (72.36%)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

109
    taosUpdateItemSize(qinfo.queue, 1);
2,542,399✔
110
  }
111

112
  DestoryThreadLocalRegComp();
116,349✔
113

114
  return NULL;
116,349✔
115
}
116

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

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

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

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

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

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

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

162
  return queue;
19,299✔
163
}
164

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

252
    taosUpdateItemSize(qinfo.queue, 1);
182,445✔
253
  }
254
  DestoryThreadLocalRegComp();
5,427✔
255

256
  return NULL;
5,427✔
257
}
258

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

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

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

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

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

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

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

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

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

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

322
  return queue;
12,269✔
323
}
324

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

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

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

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

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

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

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

373
  taosMemoryFreeClear(pool->workers);
51,467✔
374
  (void)taosThreadMutexDestroy(&pool->mutex);
51,464✔
375

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

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

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

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

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

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

410
    if (qinfo.fp != NULL) {
7,208,478!
411
      qinfo.workerId = worker->id;
7,208,480✔
412
      qinfo.threadNum = pool->num;
7,208,480✔
413
      (*((FItems)qinfo.fp))(&qinfo, worker->qall, numOfMsgs);
7,208,480✔
414
    }
415
    taosUpdateItemSize(qinfo.queue, numOfMsgs);
7,209,420✔
416
  }
417

418
  return NULL;
58,172✔
419
}
420

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

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

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

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

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

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

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

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

461
  if (code) {
61,397!
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,032✔
469

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

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

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

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

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

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

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

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

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

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

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

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

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

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

571
  while (!taosQueueEmpty(pWorker->queue)) {
52,918✔
572
    taosMsleep(10);
3,801✔
573
  }
574

575
  tWWorkerCleanup(&pWorker->pool);
49,127✔
576
  tWWorkerFreeQueue(&pWorker->pool, pWorker->queue);
49,128✔
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,778,922✔
596

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

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

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

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

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

676
    if (qinfo.timestamp != 0) {
11,778,766!
677
      int64_t cost = taosGetTimestampUs() - qinfo.timestamp;
11,778,890✔
678
      if (cost > QUEUE_THRESHOLD) {
11,778,890!
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,778,573✔
684

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

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

699
  DestoryThreadLocalRegComp();
508,958✔
700

701
  return NULL;
508,929✔
702
}
703

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

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

740
static bool tQueryAutoQWorkerTryDecActive(void *p, int32_t minActive) {
12,980,134✔
741
  SQueryAutoQWorkerPool *pPool = p;
12,980,134✔
742
  bool                   ret = false;
12,980,134✔
743
  int64_t                val64 = pPool->activeRunningN;
12,980,134✔
744
  int32_t                active = GET_ACTIVE_N(val64), running = GET_RUNNING_N(val64);
12,980,134✔
745
  while (active > minActive) {
12,980,605✔
746
    if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active - 1, &running, running - 1))
1,201,503✔
747
      return true;
1,201,038✔
748
  }
749
  return false;
11,779,102✔
750
}
751

752
static void tQueryAutoQWorkerWaitingCheck(SQueryAutoQWorkerPool *pPool) {
11,778,947✔
753
  while (1) {
106✔
754
    int64_t val64 = pPool->activeRunningN;
11,778,947✔
755
    int32_t running = GET_RUNNING_N(val64), active = GET_ACTIVE_N(val64);
11,778,947✔
756
    while (running < pPool->num) {
11,790,743!
757
      if (atomicCompareExchangeActiveAndRunning(&pPool->activeRunningN, &active, active, &running, running + 1)) {
11,790,818✔
758
        return;
11,779,406✔
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);
7✔
768
  if (!pPool->exit) (void)taosThreadCondWait(&pPool->waitingBeforeProcessMsgCond, &pPool->waitingBeforeProcessMsgLock);
7!
769
  // recovered from waiting
770
  (void)taosThreadMutexUnlock(&pPool->waitingBeforeProcessMsgLock);
7✔
771
  return;
7✔
772
}
773

774
bool tQueryAutoQWorkerTryRecycleWorker(SQueryAutoQWorkerPool *pPool, SQueryAutoQWorker *pWorker) {
11,779,279✔
775
  if (tQueryAutoQWorkerTrySignalWaitingAfterBlock(pPool) || tQueryAutoQWorkerTrySignalWaitingBeforeProcess(pPool) ||
23,558,456!
776
      tQueryAutoQWorkerTryDecActive(pPool, pPool->num)) {
11,779,205✔
777
    (void)taosThreadMutexLock(&pPool->poolLock);
631,370✔
778
    SListNode *pNode = listNode(pWorker);
631,290✔
779
    SListNode *tNode = tdListPopNode(pPool->workers, pNode);
631,290✔
780
    // reclaim some workers
781
    if (pWorker->id >= pPool->maxInUse) {
631,290!
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);
631,290✔
798
    (void)taosThreadMutexUnlock(&pPool->poolLock);
631,290✔
799

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

806
    // recovered from backup
807
    (void)taosThreadMutexLock(&pPool->poolLock);
631,287✔
808
    if (pPool->exit) {
631,290✔
809
      (void)taosThreadMutexUnlock(&pPool->poolLock);
1,751✔
810
      return false;
1,751✔
811
    }
812
    SListNode *tNode1 = tdListPopNode(pPool->backupWorkers, pNode);
629,539✔
813
    tdListAppendNode(pPool->workers, pNode);
629,539✔
814
    (void)taosThreadMutexUnlock(&pPool->poolLock);
629,539✔
815

816
    return true;
629,539✔
817
  } else {
818
    (void)atomicFetchSubRunning(&pPool->activeRunningN, 1);
11,147,847✔
819
    return true;
11,147,977✔
820
  }
821
}
822

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

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

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

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

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

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

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

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

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

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

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

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

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

932
  (void)taosThreadCondDestroy(&pPool->backupCond);
8,082✔
933
  (void)taosThreadCondDestroy(&pPool->waitingAfterBlockCond);
8,082✔
934
  (void)taosThreadCondDestroy(&pPool->waitingBeforeProcessMsgCond);
8,082✔
935
  taosCloseQset(pPool->qset);
8,082✔
936
}
8,082✔
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,312✔
962
      worker.backupIdx = -1;
507,312✔
963
      worker.pool = pool;
507,312✔
964
      SListNode *pNode = tdListAdd(pool->workers, &worker);
507,312✔
965
      if (!pNode) {
507,312!
966
        taosCloseQueue(queue);
×
967
        queue = NULL;
×
968
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
969
        break;
×
970
      }
971
      pWorker = (SQueryAutoQWorker *)pNode->data;
507,312✔
972

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

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

983
      (void)taosThreadAttrDestroy(&thAttr);
507,312✔
984
      pool->num++;
507,312✔
985
      (void)atomicFetchAddActive(&pool->activeRunningN, 1);
507,312✔
986
      uInfo("worker:%s:%d is launched, total:%d", pool->name, pWorker->id, pool->num);
507,312!
987
    } while (pool->num < pool->min);
507,312✔
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,724✔
997

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

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

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

1034
  return TSDB_CODE_SUCCESS;
1,750✔
1035
}
1036

1037
static int32_t tQueryAutoQWorkerBeforeBlocking(void *p) {
1,201,004✔
1038
  SQueryAutoQWorkerPool *pPool = p;
1,201,004✔
1039
  if (tQueryAutoQWorkerTrySignalWaitingAfterBlock(p) || tQueryAutoQWorkerTrySignalWaitingBeforeProcess(p) ||
2,402,036!
1040
      tQueryAutoQWorkerTryDecActive(p, pPool->num)) {
1,201,027✔
1041
  } else {
1042
    int32_t code = tQueryAutoQWorkerAddWorker(pPool);
631,270✔
1043
    if (code != TSDB_CODE_SUCCESS) {
631,286!
1044
      return code;
×
1045
    }
1046
    (void)atomicFetchSubRunning(&pPool->activeRunningN, 1);
631,286✔
1047
  }
1048

1049
  return TSDB_CODE_SUCCESS;
1,201,083✔
1050
}
1051

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