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

taosdata / TDengine / #3528

13 Nov 2024 02:14AM UTC coverage: 60.905% (+0.09%) from 60.819%
#3528

push

travis-ci

web-flow
Merge pull request #28748 from taosdata/test/chr-3.0-TD14758

test:add docs ci in jenkinsfile2

118800 of 249004 branches covered (47.71%)

Branch coverage included in aggregate %.

199361 of 273386 relevant lines covered (72.92%)

14738389.65 hits per line

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

72.88
/source/dnode/vnode/src/vnd/vnodeAsync.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
#include "vnd.h"
17
#include "vnodeHash.h"
18

19
typedef struct SVAsync    SVAsync;
20
typedef struct SVATask    SVATask;
21
typedef struct SVAChannel SVAChannel;
22

23
#define VNODE_ASYNC_DEFAULT_WORKERS 4
24
#define VNODE_ASYNC_MAX_WORKERS     256
25

26
// priority
27

28
#define EVA_PRIORITY_MAX (EVA_PRIORITY_LOW + 1)
29

30
// worker
31
typedef enum {
32
  EVA_WORKER_STATE_UINIT = 0,
33
  EVA_WORKER_STATE_ACTIVE,
34
  EVA_WORKER_STATE_IDLE,
35
  EVA_WORKER_STATE_STOP,
36
} EVWorkerState;
37

38
typedef struct {
39
  SVAsync      *async;
40
  int32_t       workerId;
41
  EVWorkerState state;
42
  TdThread      thread;
43
  SVATask      *runningTask;
44
} SVWorker;
45

46
// task
47
typedef enum {
48
  EVA_TASK_STATE_WAITTING = 0,
49
  EVA_TASK_STATE_RUNNING,
50
} EVATaskState;
51

52
struct SVATask {
53
  int64_t     taskId;
54
  EVAPriority priority;
55
  int32_t     priorScore;
56
  SVAChannel *channel;
57
  int32_t (*execute)(void *);
58
  void (*cancel)(void *);
59
  void        *arg;
60
  EVATaskState state;
61

62
  // wait
63
  int32_t      numWait;
64
  TdThreadCond waitCond;
65

66
  // queue
67
  struct SVATask *prev;
68
  struct SVATask *next;
69
};
70

71
typedef struct {
72
  void (*cancel)(void *);
73
  void *arg;
74
} SVATaskCancelInfo;
75

76
#define VATASK_PIORITY(task_) ((task_)->priority - ((task_)->priorScore / 4))
77

78
// async channel
79
typedef enum {
80
  EVA_CHANNEL_STATE_OPEN = 0,
81
  EVA_CHANNEL_STATE_CLOSE,
82
} EVAChannelState;
83

84
struct SVAChannel {
85
  int64_t         channelId;
86
  EVAChannelState state;
87
  SVATask         queue[EVA_PRIORITY_MAX];
88
  SVATask        *scheduled;
89

90
  SVAChannel *prev;
91
  SVAChannel *next;
92
};
93

94
// async handle
95
struct SVAsync {
96
  const char *label;
97

98
  TdThreadMutex mutex;
99
  TdThreadCond  hasTask;
100
  bool          stop;
101

102
  // worker
103
  int32_t  numWorkers;
104
  int32_t  numLaunchWorkers;
105
  int32_t  numIdleWorkers;
106
  SVWorker workers[VNODE_ASYNC_MAX_WORKERS];
107

108
  // channel
109
  int64_t      nextChannelId;
110
  int32_t      numChannels;
111
  SVAChannel   chList;
112
  SVHashTable *channelTable;
113

114
  // task
115
  int64_t      nextTaskId;
116
  int32_t      numTasks;
117
  SVATask      queue[EVA_PRIORITY_MAX];
118
  SVHashTable *taskTable;
119
};
120

121
SVAsync *vnodeAsyncs[3];
122
#define MIN_ASYNC_ID 1
123
#define MAX_ASYNC_ID (sizeof(vnodeAsyncs) / sizeof(vnodeAsyncs[0]) - 1)
124

125
static void vnodeAsyncTaskDone(SVAsync *async, SVATask *task) {
38,105✔
126
  int32_t ret;
127

128
  if (task->channel != NULL && task->channel->scheduled == task) {
38,105!
129
    task->channel->scheduled = NULL;
38,102✔
130
    if (task->channel->state == EVA_CHANNEL_STATE_CLOSE) {
38,102!
131
      taosMemoryFree(task->channel);
×
132
    } else {
133
      for (int32_t i = 0; i < EVA_PRIORITY_MAX; i++) {
152,408✔
134
        SVATask *nextTask = task->channel->queue[i].next;
114,306✔
135
        if (nextTask != &task->channel->queue[i]) {
114,306✔
136
          if (task->channel->scheduled == NULL) {
1,355✔
137
            task->channel->scheduled = nextTask;
1,350✔
138
            nextTask->next->prev = nextTask->prev;
1,350✔
139
            nextTask->prev->next = nextTask->next;
1,350✔
140
          } else {
141
            nextTask->priorScore++;
5✔
142
            int32_t newPriority = VATASK_PIORITY(nextTask);
5✔
143
            if (newPriority != i) {
5!
144
              // remove from current priority queue
145
              nextTask->prev->next = nextTask->next;
×
146
              nextTask->next->prev = nextTask->prev;
×
147
              // add to new priority queue
148
              nextTask->next = &task->channel->queue[newPriority];
×
149
              nextTask->prev = task->channel->queue[newPriority].prev;
×
150
              nextTask->next->prev = nextTask;
×
151
              nextTask->prev->next = nextTask;
×
152
            }
153
          }
154
        }
155
      }
156

157
      if (task->channel->scheduled != NULL) {
38,102✔
158
        int32_t priority = VATASK_PIORITY(task->channel->scheduled);
1,350✔
159
        task->channel->scheduled->next = &async->queue[priority];
1,350✔
160
        task->channel->scheduled->prev = async->queue[priority].prev;
1,350✔
161
        task->channel->scheduled->next->prev = task->channel->scheduled;
1,350✔
162
        task->channel->scheduled->prev->next = task->channel->scheduled;
1,350✔
163
      }
164
    }
165
  }
166

167
  ret = vHashDrop(async->taskTable, task);
38,105✔
168
  TAOS_UNUSED(ret);
169
  async->numTasks--;
38,105✔
170

171
  if (task->numWait == 0) {
38,105✔
172
    (void)taosThreadCondDestroy(&task->waitCond);
35,546✔
173
    taosMemoryFree(task);
35,545✔
174
  } else if (task->numWait == 1) {
2,559!
175
    (void)taosThreadCondSignal(&task->waitCond);
2,559✔
176
  } else {
177
    (void)taosThreadCondBroadcast(&task->waitCond);
×
178
  }
179
}
38,105✔
180

181
static void vnodeAsyncCancelAllTasks(SVAsync *async, SArray *cancelArray) {
3,535✔
182
  while (async->queue[0].next != &async->queue[0] || async->queue[1].next != &async->queue[1] ||
3,535!
183
         async->queue[2].next != &async->queue[2]) {
3,535!
184
    for (int32_t i = 0; i < EVA_PRIORITY_MAX; i++) {
×
185
      while (async->queue[i].next != &async->queue[i]) {
×
186
        SVATask *task = async->queue[i].next;
×
187
        task->prev->next = task->next;
×
188
        task->next->prev = task->prev;
×
189
        if (task->cancel) {
×
190
          if (taosArrayPush(cancelArray, &(SVATaskCancelInfo){
×
191
                                             .cancel = task->cancel,
×
192
                                             .arg = task->arg,
×
193
                                         }) == NULL) {
194
            vError("failed to push cancel task into array");
×
195
          };
196
        }
197
        vnodeAsyncTaskDone(async, task);
×
198
      }
199
    }
200
  }
201
}
3,535✔
202

203
static void *vnodeAsyncLoop(void *arg) {
3,535✔
204
  SVWorker *worker = (SVWorker *)arg;
3,535✔
205
  SVAsync  *async = worker->async;
3,535✔
206
  SArray   *cancelArray = taosArrayInit(0, sizeof(SVATaskCancelInfo));
3,535✔
207
  if (cancelArray == NULL) {
3,535!
208
    return NULL;
×
209
  }
210

211
  setThreadName(async->label);
3,535✔
212

213
  for (;;) {
38,082✔
214
    (void)taosThreadMutexLock(&async->mutex);
41,616✔
215

216
    // finish last running task
217
    if (worker->runningTask != NULL) {
41,618✔
218
      vnodeAsyncTaskDone(async, worker->runningTask);
38,083✔
219
      worker->runningTask = NULL;
38,083✔
220
    }
221

222
    for (;;) {
223
      if (async->stop || worker->workerId >= async->numWorkers) {
76,684!
224
        if (async->stop) {  // cancel all tasks
3,535!
225
          vnodeAsyncCancelAllTasks(async, cancelArray);
3,535✔
226
        }
227
        worker->state = EVA_WORKER_STATE_STOP;
3,535✔
228
        async->numLaunchWorkers--;
3,535✔
229
        (void)taosThreadMutexUnlock(&async->mutex);
3,535✔
230
        goto _exit;
3,532✔
231
      }
232

233
      for (int32_t i = 0; i < EVA_PRIORITY_MAX; i++) {
292,596✔
234
        SVATask *task = async->queue[i].next;
219,447✔
235
        if (task != &async->queue[i]) {
219,447✔
236
          if (worker->runningTask == NULL) {
38,100✔
237
            worker->runningTask = task;
38,083✔
238
            task->prev->next = task->next;
38,083✔
239
            task->next->prev = task->prev;
38,083✔
240
          } else {  // promote priority
241
            task->priorScore++;
17✔
242
            int32_t priority = VATASK_PIORITY(task);
17✔
243
            if (priority != i) {
17✔
244
              // remove from current priority queue
245
              task->prev->next = task->next;
4✔
246
              task->next->prev = task->prev;
4✔
247
              // add to new priority queue
248
              task->next = &async->queue[priority];
4✔
249
              task->prev = async->queue[priority].prev;
4✔
250
              task->next->prev = task;
4✔
251
              task->prev->next = task;
4✔
252
            }
253
          }
254
        }
255
      }
256

257
      if (worker->runningTask == NULL) {
73,149✔
258
        worker->state = EVA_WORKER_STATE_IDLE;
35,066✔
259
        async->numIdleWorkers++;
35,066✔
260
        (void)taosThreadCondWait(&async->hasTask, &async->mutex);
35,066✔
261
        async->numIdleWorkers--;
35,066✔
262
        worker->state = EVA_WORKER_STATE_ACTIVE;
35,066✔
263
      } else {
264
        worker->runningTask->state = EVA_TASK_STATE_RUNNING;
38,083✔
265
        break;
38,083✔
266
      }
267
    }
268

269
    (void)taosThreadMutexUnlock(&async->mutex);
38,083✔
270

271
    // do run the task
272
    int32_t code = worker->runningTask->execute(worker->runningTask->arg);
38,080✔
273
    TAOS_UNUSED(code);
274
  }
275

276
_exit:
3,532✔
277
  for (int32_t i = 0; i < taosArrayGetSize(cancelArray); i++) {
3,532!
278
    SVATaskCancelInfo *cancel = (SVATaskCancelInfo *)taosArrayGet(cancelArray, i);
×
279
    cancel->cancel(cancel->arg);
×
280
  }
281
  taosArrayDestroy(cancelArray);
3,533✔
282
  return NULL;
3,529✔
283
}
284

285
static uint32_t vnodeAsyncTaskHash(const void *obj) {
107,401✔
286
  SVATask *task = (SVATask *)obj;
107,401✔
287
  return MurmurHash3_32((const char *)(&task->taskId), sizeof(task->taskId));
107,401✔
288
}
289

290
static int32_t vnodeAsyncTaskCompare(const void *obj1, const void *obj2) {
40,710✔
291
  SVATask *task1 = (SVATask *)obj1;
40,710✔
292
  SVATask *task2 = (SVATask *)obj2;
40,710✔
293
  if (task1->taskId < task2->taskId) {
40,710✔
294
    return -1;
42✔
295
  } else if (task1->taskId > task2->taskId) {
40,668✔
296
    return 1;
36✔
297
  }
298
  return 0;
40,632✔
299
}
300

301
static uint32_t vnodeAsyncChannelHash(const void *obj) {
84,635✔
302
  SVAChannel *channel = (SVAChannel *)obj;
84,635✔
303
  return MurmurHash3_32((const char *)(&channel->channelId), sizeof(channel->channelId));
84,635✔
304
}
305

306
static int32_t vnodeAsyncChannelCompare(const void *obj1, const void *obj2) {
69,280✔
307
  SVAChannel *channel1 = (SVAChannel *)obj1;
69,280✔
308
  SVAChannel *channel2 = (SVAChannel *)obj2;
69,280✔
309
  if (channel1->channelId < channel2->channelId) {
69,280✔
310
    return -1;
65✔
311
  } else if (channel1->channelId > channel2->channelId) {
69,215✔
312
    return 1;
90✔
313
  }
314
  return 0;
69,125✔
315
}
316

317
static int32_t vnodeAsyncInit(SVAsync **async, const char *label) {
4,726✔
318
  int32_t ret;
319

320
  if (async == NULL) {
4,726!
321
    return TSDB_CODE_INVALID_PARA;
×
322
  }
323

324
  if (label == NULL) {
4,726!
325
    label = "anonymous";
×
326
  }
327

328
  (*async) = (SVAsync *)taosMemoryCalloc(1, sizeof(SVAsync) + strlen(label) + 1);
4,726✔
329
  if ((*async) == NULL) {
4,726!
330
    return terrno;
×
331
  }
332

333
  strcpy((char *)((*async) + 1), label);
4,726✔
334
  (*async)->label = (const char *)((*async) + 1);
4,726✔
335

336
  (void)taosThreadMutexInit(&(*async)->mutex, NULL);
4,726✔
337
  (void)taosThreadCondInit(&(*async)->hasTask, NULL);
4,726✔
338
  (*async)->stop = false;
4,726✔
339

340
  // worker
341
  (*async)->numWorkers = VNODE_ASYNC_DEFAULT_WORKERS;
4,726✔
342
  (*async)->numLaunchWorkers = 0;
4,726✔
343
  (*async)->numIdleWorkers = 0;
4,726✔
344
  for (int32_t i = 0; i < VNODE_ASYNC_MAX_WORKERS; i++) {
1,214,582✔
345
    (*async)->workers[i].async = (*async);
1,209,856✔
346
    (*async)->workers[i].workerId = i;
1,209,856✔
347
    (*async)->workers[i].state = EVA_WORKER_STATE_UINIT;
1,209,856✔
348
    (*async)->workers[i].runningTask = NULL;
1,209,856✔
349
  }
350

351
  // channel
352
  (*async)->nextChannelId = 0;
4,726✔
353
  (*async)->numChannels = 0;
4,726✔
354
  (*async)->chList.prev = &(*async)->chList;
4,726✔
355
  (*async)->chList.next = &(*async)->chList;
4,726✔
356
  ret = vHashInit(&(*async)->channelTable, vnodeAsyncChannelHash, vnodeAsyncChannelCompare);
4,726✔
357
  if (ret != 0) {
4,726!
358
    (void)taosThreadMutexDestroy(&(*async)->mutex);
×
359
    (void)taosThreadCondDestroy(&(*async)->hasTask);
×
360
    taosMemoryFree(*async);
×
361
    return ret;
×
362
  }
363

364
  // task
365
  (*async)->nextTaskId = 0;
4,726✔
366
  (*async)->numTasks = 0;
4,726✔
367
  for (int32_t i = 0; i < EVA_PRIORITY_MAX; i++) {
18,904✔
368
    (*async)->queue[i].next = &(*async)->queue[i];
14,178✔
369
    (*async)->queue[i].prev = &(*async)->queue[i];
14,178✔
370
  }
371
  ret = vHashInit(&(*async)->taskTable, vnodeAsyncTaskHash, vnodeAsyncTaskCompare);
4,726✔
372
  if (ret != 0) {
4,726!
373
    vHashDestroy(&(*async)->channelTable);
×
374
    (void)taosThreadMutexDestroy(&(*async)->mutex);
×
375
    (void)taosThreadCondDestroy(&(*async)->hasTask);
×
376
    taosMemoryFree(*async);
×
377
    return ret;
×
378
  }
379

380
  return 0;
4,726✔
381
}
382

383
static int32_t vnodeAsyncDestroy(SVAsync **async) {
4,726✔
384
  if ((*async) == NULL) {
4,726!
385
    return TSDB_CODE_INVALID_PARA;
×
386
  }
387

388
  // set stop and broadcast
389
  (void)taosThreadMutexLock(&(*async)->mutex);
4,726✔
390
  (*async)->stop = true;
4,726✔
391
  (void)taosThreadCondBroadcast(&(*async)->hasTask);
4,726✔
392
  (void)taosThreadMutexUnlock(&(*async)->mutex);
4,726✔
393

394
  // join all workers
395
  for (int32_t i = 0; i < VNODE_ASYNC_MAX_WORKERS; i++) {
1,214,582✔
396
    (void)taosThreadMutexLock(&(*async)->mutex);
1,209,856✔
397
    EVWorkerState state = (*async)->workers[i].state;
1,209,856✔
398
    (void)taosThreadMutexUnlock(&(*async)->mutex);
1,209,856✔
399

400
    if (state == EVA_WORKER_STATE_UINIT) {
1,209,856✔
401
      continue;
1,206,321✔
402
    }
403

404
    (void)taosThreadJoin((*async)->workers[i].thread, NULL);
3,535✔
405
    (*async)->workers[i].state = EVA_WORKER_STATE_UINIT;
3,535✔
406
  }
407

408
  // close all channels
409
  for (SVAChannel *channel = (*async)->chList.next; channel != &(*async)->chList; channel = (*async)->chList.next) {
4,726!
410
    channel->next->prev = channel->prev;
×
411
    channel->prev->next = channel->next;
×
412

413
    int32_t ret = vHashDrop((*async)->channelTable, channel);
×
414
    TAOS_UNUSED(ret);
415
    (*async)->numChannels--;
×
416
    taosMemoryFree(channel);
×
417
  }
418

419
  (void)taosThreadMutexDestroy(&(*async)->mutex);
4,726✔
420
  (void)taosThreadCondDestroy(&(*async)->hasTask);
4,726✔
421

422
  vHashDestroy(&(*async)->channelTable);
4,726✔
423
  vHashDestroy(&(*async)->taskTable);
4,726✔
424
  taosMemoryFree(*async);
4,726✔
425
  *async = NULL;
4,726✔
426

427
  return 0;
4,726✔
428
}
429

430
static void vnodeAsyncLaunchWorker(SVAsync *async) {
3,535✔
431
  for (int32_t i = 0; i < async->numWorkers; i++) {
6,819!
432
    if (async->workers[i].state == EVA_WORKER_STATE_ACTIVE) {
6,819✔
433
      continue;
3,284✔
434
    } else if (async->workers[i].state == EVA_WORKER_STATE_STOP) {
3,535!
435
      int32_t ret = taosThreadJoin(async->workers[i].thread, NULL);
×
436
      async->workers[i].state = EVA_WORKER_STATE_UINIT;
×
437
    }
438

439
    int32_t ret = taosThreadCreate(&async->workers[i].thread, NULL, vnodeAsyncLoop, &async->workers[i]);
3,535✔
440
    if (ret) {
3,535!
441
      vError("failed to create worker thread since %s", tstrerror(ret));
×
442
    } else {
443
      async->workers[i].state = EVA_WORKER_STATE_ACTIVE;
3,535✔
444
      async->numLaunchWorkers++;
3,535✔
445
    }
446
    break;
3,535✔
447
  }
448
}
3,535✔
449

450
int32_t vnodeAsyncOpen(int32_t numOfThreads) {
2,363✔
451
  int32_t code = 0;
2,363✔
452
  int32_t lino = 0;
2,363✔
453

454
  // vnode-commit
455
  code = vnodeAsyncInit(&vnodeAsyncs[1], "vnode-commit");
2,363✔
456
  TSDB_CHECK_CODE(code, lino, _exit);
2,363!
457

458
  code = vnodeAsyncSetWorkers(1, numOfThreads);
2,363✔
459
  TSDB_CHECK_CODE(code, lino, _exit);
2,363!
460

461
  // vnode-merge
462
  code = vnodeAsyncInit(&vnodeAsyncs[2], "vnode-merge");
2,363✔
463
  TSDB_CHECK_CODE(code, lino, _exit);
2,363!
464

465
  code = vnodeAsyncSetWorkers(2, numOfThreads);
2,363✔
466
  TSDB_CHECK_CODE(code, lino, _exit);
2,363!
467

468
_exit:
2,363✔
469
  return code;
2,363✔
470
}
471

472
void vnodeAsyncClose() {
2,363✔
473
  int32_t ret;
474
  ret = vnodeAsyncDestroy(&vnodeAsyncs[1]);
2,363✔
475
  ret = vnodeAsyncDestroy(&vnodeAsyncs[2]);
2,363✔
476
}
2,363✔
477

478
int32_t vnodeAsync(SVAChannelID *channelID, EVAPriority priority, int32_t (*execute)(void *), void (*cancel)(void *),
38,102✔
479
                   void *arg, SVATaskID *taskID) {
480
  if (channelID == NULL || channelID->async < MIN_ASYNC_ID || channelID->async > MAX_ASYNC_ID || execute == NULL ||
38,102!
481
      channelID->id < 0) {
38,103!
482
    return TSDB_CODE_INVALID_PARA;
×
483
  }
484

485
  int32_t  ret;
486
  int64_t  id;
487
  SVAsync *async = vnodeAsyncs[channelID->async];
38,103✔
488

489
  // create task object
490
  SVATask *task = (SVATask *)taosMemoryCalloc(1, sizeof(SVATask));
38,103✔
491
  if (task == NULL) {
38,102!
492
    return terrno;
×
493
  }
494

495
  task->priority = priority;
38,102✔
496
  task->priorScore = 0;
38,102✔
497
  task->execute = execute;
38,102✔
498
  task->cancel = cancel;
38,102✔
499
  task->arg = arg;
38,102✔
500
  task->state = EVA_TASK_STATE_WAITTING;
38,102✔
501
  task->numWait = 0;
38,102✔
502
  (void)taosThreadCondInit(&task->waitCond, NULL);
38,102✔
503

504
  // schedule task
505
  (void)taosThreadMutexLock(&async->mutex);
38,097✔
506

507
  if (channelID->id == 0) {
38,105!
508
    task->channel = NULL;
×
509
  } else {
510
    SVAChannel channel = {
38,105✔
511
        .channelId = channelID->id,
38,105✔
512
    };
513
    ret = vHashGet(async->channelTable, &channel, (void **)&task->channel);
38,105✔
514
    TAOS_UNUSED(ret);
515
    if (task->channel == NULL) {
38,105!
516
      (void)taosThreadMutexUnlock(&async->mutex);
×
517
      (void)taosThreadCondDestroy(&task->waitCond);
×
518
      taosMemoryFree(task);
×
519
      return TSDB_CODE_INVALID_PARA;
×
520
    }
521
  }
522

523
  task->taskId = id = ++async->nextTaskId;
38,105✔
524

525
  // add task to hash table
526
  ret = vHashPut(async->taskTable, task);
38,105✔
527
  if (ret != 0) {
38,105!
528
    (void)taosThreadMutexUnlock(&async->mutex);
×
529
    (void)taosThreadCondDestroy(&task->waitCond);
×
530
    taosMemoryFree(task);
×
531
    return ret;
×
532
  }
533

534
  async->numTasks++;
38,105✔
535

536
  // add task to queue
537
  if (task->channel == NULL || task->channel->scheduled == NULL) {
38,105!
538
    // add task to async->queue
539
    if (task->channel) {
36,752!
540
      task->channel->scheduled = task;
36,752✔
541
    }
542

543
    task->next = &async->queue[priority];
36,752✔
544
    task->prev = async->queue[priority].prev;
36,752✔
545
    task->next->prev = task;
36,752✔
546
    task->prev->next = task;
36,752✔
547

548
    // signal worker or launch new worker
549
    if (async->numIdleWorkers > 0) {
36,752✔
550
      (void)taosThreadCondSignal(&(async->hasTask));
31,930✔
551
    } else if (async->numLaunchWorkers < async->numWorkers) {
4,822✔
552
      vnodeAsyncLaunchWorker(async);
3,535✔
553
    }
554
  } else if (task->channel->scheduled->state == EVA_TASK_STATE_RUNNING ||
1,353✔
555
             priority >= VATASK_PIORITY(task->channel->scheduled)) {
5!
556
    // add task to task->channel->queue
557
    task->next = &task->channel->queue[priority];
1,348✔
558
    task->prev = task->channel->queue[priority].prev;
1,348✔
559
    task->next->prev = task;
1,348✔
560
    task->prev->next = task;
1,348✔
561
  } else {
562
    // remove task->channel->scheduled from queue
563
    task->channel->scheduled->prev->next = task->channel->scheduled->next;
5✔
564
    task->channel->scheduled->next->prev = task->channel->scheduled->prev;
5✔
565

566
    // promote priority and add task->channel->scheduled to task->channel->queue
567
    task->channel->scheduled->priorScore++;
5✔
568
    int32_t newPriority = VATASK_PIORITY(task->channel->scheduled);
5✔
569
    task->channel->scheduled->next = &task->channel->queue[newPriority];
5✔
570
    task->channel->scheduled->prev = task->channel->queue[newPriority].prev;
5✔
571
    task->channel->scheduled->next->prev = task->channel->scheduled;
5✔
572
    task->channel->scheduled->prev->next = task->channel->scheduled;
5✔
573

574
    // add task to queue
575
    task->channel->scheduled = task;
5✔
576
    task->next = &async->queue[priority];
5✔
577
    task->prev = async->queue[priority].prev;
5✔
578
    task->next->prev = task;
5✔
579
    task->prev->next = task;
5✔
580
  }
581

582
  (void)taosThreadMutexUnlock(&async->mutex);
38,105✔
583

584
  if (taskID != NULL) {
38,105✔
585
    taskID->async = channelID->async;
31,156✔
586
    taskID->id = id;
31,156✔
587
  }
588

589
  return 0;
38,105✔
590
}
591

592
void vnodeAWait(SVATaskID *taskID) {
44,650✔
593
  if (taskID == NULL || taskID->async < MIN_ASYNC_ID || taskID->async > MAX_ASYNC_ID || taskID->id <= 0) {
44,650!
594
    return;
13,463✔
595
  }
596

597
  SVAsync *async = vnodeAsyncs[taskID->async];
31,187✔
598
  SVATask *task = NULL;
31,187✔
599
  SVATask  task2 = {
31,187✔
600
       .taskId = taskID->id,
31,187✔
601
  };
602

603
  (void)taosThreadMutexLock(&async->mutex);
31,187✔
604

605
  int32_t ret = vHashGet(async->taskTable, &task2, (void **)&task);
31,191✔
606
  if (task) {
31,191✔
607
    task->numWait++;
2,527✔
608
    (void)taosThreadCondWait(&task->waitCond, &async->mutex);
2,527✔
609
    task->numWait--;
2,527✔
610

611
    if (task->numWait == 0) {
2,527!
612
      (void)taosThreadCondDestroy(&task->waitCond);
2,527✔
613
      taosMemoryFree(task);
2,527✔
614
    }
615
  }
616

617
  (void)taosThreadMutexUnlock(&async->mutex);
31,191✔
618
}
619

620
int32_t vnodeACancel(SVATaskID *taskID) {
×
621
  if (taskID == NULL || taskID->async < MIN_ASYNC_ID || taskID->async > MAX_ASYNC_ID || taskID->id <= 0) {
×
622
    return TSDB_CODE_INVALID_PARA;
×
623
  }
624

625
  int32_t  ret = 0;
×
626
  SVAsync *async = vnodeAsyncs[taskID->async];
×
627
  SVATask *task = NULL;
×
628
  SVATask  task2 = {
×
629
       .taskId = taskID->id,
×
630
  };
631
  void (*cancel)(void *) = NULL;
×
632
  void *arg = NULL;
×
633

634
  (void)taosThreadMutexLock(&async->mutex);
×
635

636
  ret = vHashGet(async->taskTable, &task2, (void **)&task);
×
637
  if (task) {
×
638
    if (task->state == EVA_TASK_STATE_WAITTING) {
×
639
      cancel = task->cancel;
×
640
      arg = task->arg;
×
641
      task->next->prev = task->prev;
×
642
      task->prev->next = task->next;
×
643
      vnodeAsyncTaskDone(async, task);
×
644
    } else {
645
      ret = TSDB_CODE_FAILED;
×
646
    }
647
  }
648

649
  (void)taosThreadMutexUnlock(&async->mutex);
×
650

651
  if (cancel) {
×
652
    cancel(arg);
×
653
  }
654

655
  return ret;
×
656
}
657

658
int32_t vnodeAsyncSetWorkers(int64_t asyncID, int32_t numWorkers) {
4,726✔
659
  if (asyncID < MIN_ASYNC_ID || asyncID > MAX_ASYNC_ID || numWorkers <= 0 || numWorkers > VNODE_ASYNC_MAX_WORKERS) {
4,726!
660
    return TSDB_CODE_INVALID_PARA;
×
661
  }
662
  int32_t  ret;
663
  SVAsync *async = vnodeAsyncs[asyncID];
4,726✔
664
  (void)taosThreadMutexLock(&async->mutex);
4,726✔
665
  async->numWorkers = numWorkers;
4,726✔
666
  if (async->numIdleWorkers > 0) {
4,726!
667
    (void)taosThreadCondBroadcast(&async->hasTask);
×
668
  }
669
  (void)taosThreadMutexUnlock(&async->mutex);
4,726✔
670

671
  return 0;
4,726✔
672
}
673

674
int32_t vnodeAChannelInit(int64_t asyncID, SVAChannelID *channelID) {
15,460✔
675
  if (channelID == NULL || asyncID < MIN_ASYNC_ID || asyncID > MAX_ASYNC_ID) {
15,460!
676
    return TSDB_CODE_INVALID_PARA;
×
677
  }
678

679
  SVAsync *async = vnodeAsyncs[asyncID];
15,499✔
680

681
  // create channel object
682
  SVAChannel *channel = (SVAChannel *)taosMemoryMalloc(sizeof(SVAChannel));
15,499✔
683
  if (channel == NULL) {
15,494✔
684
    return terrno;
15✔
685
  }
686
  channel->state = EVA_CHANNEL_STATE_OPEN;
15,479✔
687
  for (int32_t i = 0; i < EVA_PRIORITY_MAX; i++) {
61,902✔
688
    channel->queue[i].next = &channel->queue[i];
46,423✔
689
    channel->queue[i].prev = &channel->queue[i];
46,423✔
690
  }
691
  channel->scheduled = NULL;
15,479✔
692

693
  // register channel
694
  (void)taosThreadMutexLock(&async->mutex);
15,479✔
695

696
  channel->channelId = channelID->id = ++async->nextChannelId;
15,510✔
697

698
  // add to hash table
699
  int32_t ret = vHashPut(async->channelTable, channel);
15,510✔
700
  if (ret != 0) {
15,510!
701
    (void)taosThreadMutexUnlock(&async->mutex);
×
702
    taosMemoryFree(channel);
×
703
    return ret;
×
704
  }
705

706
  // add to list
707
  channel->next = &async->chList;
15,510✔
708
  channel->prev = async->chList.prev;
15,510✔
709
  channel->next->prev = channel;
15,510✔
710
  channel->prev->next = channel;
15,510✔
711

712
  async->numChannels++;
15,510✔
713

714
  (void)taosThreadMutexUnlock(&async->mutex);
15,510✔
715

716
  channelID->async = asyncID;
15,509✔
717
  return 0;
15,509✔
718
}
719

720
int32_t vnodeAChannelDestroy(SVAChannelID *channelID, bool waitRunning) {
15,510✔
721
  if (channelID == NULL || channelID->async < MIN_ASYNC_ID || channelID->async > MAX_ASYNC_ID || channelID->id <= 0) {
15,510!
722
    return TSDB_CODE_INVALID_PARA;
×
723
  }
724

725
  SVAsync    *async = vnodeAsyncs[channelID->async];
15,510✔
726
  SVAChannel *channel = NULL;
15,510✔
727
  SVAChannel  channel2 = {
15,510✔
728
       .channelId = channelID->id,
15,510✔
729
  };
730
  SArray *cancelArray = taosArrayInit(0, sizeof(SVATaskCancelInfo));
15,510✔
731
  if (cancelArray == NULL) {
15,509!
732
    return terrno;
×
733
  }
734

735
  (void)taosThreadMutexLock(&async->mutex);
15,509✔
736

737
  int32_t ret = vHashGet(async->channelTable, &channel2, (void **)&channel);
15,510✔
738
  TAOS_UNUSED(ret);
739
  if (channel) {
15,510!
740
    // unregister channel
741
    channel->next->prev = channel->prev;
15,510✔
742
    channel->prev->next = channel->next;
15,510✔
743
    ret = vHashDrop(async->channelTable, channel);
15,510✔
744
    async->numChannels--;
15,510✔
745

746
    // cancel all waiting tasks
747
    for (int32_t i = 0; i < EVA_PRIORITY_MAX; i++) {
62,040✔
748
      while (channel->queue[i].next != &channel->queue[i]) {
46,533✔
749
        SVATask *task = channel->queue[i].next;
3✔
750
        task->prev->next = task->next;
3✔
751
        task->next->prev = task->prev;
3✔
752
        if (task->cancel) {
3!
753
          if (taosArrayPush(cancelArray, &(SVATaskCancelInfo){
3!
754
                                             .cancel = task->cancel,
3✔
755
                                             .arg = task->arg,
3✔
756
                                         }) == NULL) {
757
            vError("failed to push cancel info");
×
758
          };
759
        }
760
        vnodeAsyncTaskDone(async, task);
3✔
761
      }
762
    }
763

764
    // cancel or wait the scheduled task
765
    if (channel->scheduled == NULL || channel->scheduled->state == EVA_TASK_STATE_WAITTING) {
15,510✔
766
      if (channel->scheduled) {
15,478✔
767
        channel->scheduled->prev->next = channel->scheduled->next;
19✔
768
        channel->scheduled->next->prev = channel->scheduled->prev;
19✔
769
        if (channel->scheduled->cancel) {
19!
770
          if (taosArrayPush(cancelArray, &(SVATaskCancelInfo){
19!
771
                                             .cancel = channel->scheduled->cancel,
19✔
772
                                             .arg = channel->scheduled->arg,
19✔
773
                                         }) == NULL) {
774
            vError("failed to push cancel info");
×
775
          }
776
        }
777
        vnodeAsyncTaskDone(async, channel->scheduled);
19✔
778
      }
779
      taosMemoryFree(channel);
15,478✔
780
    } else {
781
      if (waitRunning) {
32!
782
        // wait task
783
        SVATask *task = channel->scheduled;
32✔
784
        task->numWait++;
32✔
785
        (void)taosThreadCondWait(&task->waitCond, &async->mutex);
32✔
786
        task->numWait--;
32✔
787
        if (task->numWait == 0) {
32!
788
          (void)taosThreadCondDestroy(&task->waitCond);
32✔
789
          taosMemoryFree(task);
32✔
790
        }
791

792
        taosMemoryFree(channel);
32✔
793
      } else {
794
        channel->state = EVA_CHANNEL_STATE_CLOSE;
×
795
      }
796
    }
797
  }
798

799
  (void)taosThreadMutexUnlock(&async->mutex);
15,510✔
800
  for (int32_t i = 0; i < taosArrayGetSize(cancelArray); i++) {
15,532✔
801
    SVATaskCancelInfo *cancel = (SVATaskCancelInfo *)taosArrayGet(cancelArray, i);
22✔
802
    cancel->cancel(cancel->arg);
22✔
803
  }
804
  taosArrayDestroy(cancelArray);
15,510✔
805

806
  channelID->async = 0;
15,510✔
807
  channelID->id = 0;
15,510✔
808
  return 0;
15,510✔
809
}
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