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

taosdata / TDengine / #3826

01 Apr 2025 03:27PM UTC coverage: 34.075% (+0.008%) from 34.067%
#3826

push

travis-ci

happyguoxy
test:alter gcda dir

148619 of 599532 branches covered (24.79%)

Branch coverage included in aggregate %.

222450 of 489439 relevant lines covered (45.45%)

761864.29 hits per line

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

45.63
/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
struct {
122
  const char *label;
123
  SVAsync    *async;
124
} GVnodeAsyncs[] = {
125
    [0] = {NULL, NULL},
126
    [1] = {"vnode-commit", NULL},
127
    [2] = {"vnode-merge", NULL},
128
    [3] = {"vnode-compact", NULL},
129
    [4] = {"vnode-retention", NULL},
130
};
131

132
#define MIN_ASYNC_ID 1
133
#define MAX_ASYNC_ID (sizeof(GVnodeAsyncs) / sizeof(GVnodeAsyncs[0]) - 1)
134

135
static void vnodeAsyncTaskDone(SVAsync *async, SVATask *task) {
1,014✔
136
  int32_t ret;
137

138
  if (task->channel != NULL && task->channel->scheduled == task) {
1,014!
139
    task->channel->scheduled = NULL;
×
140
    if (task->channel->state == EVA_CHANNEL_STATE_CLOSE) {
×
141
      taosMemoryFree(task->channel);
×
142
    } else {
143
      for (int32_t i = 0; i < EVA_PRIORITY_MAX; i++) {
×
144
        SVATask *nextTask = task->channel->queue[i].next;
×
145
        if (nextTask != &task->channel->queue[i]) {
×
146
          if (task->channel->scheduled == NULL) {
×
147
            task->channel->scheduled = nextTask;
×
148
            nextTask->next->prev = nextTask->prev;
×
149
            nextTask->prev->next = nextTask->next;
×
150
          } else {
151
            nextTask->priorScore++;
×
152
            int32_t newPriority = VATASK_PIORITY(nextTask);
×
153
            if (newPriority != i) {
×
154
              // remove from current priority queue
155
              nextTask->prev->next = nextTask->next;
×
156
              nextTask->next->prev = nextTask->prev;
×
157
              // add to new priority queue
158
              nextTask->next = &task->channel->queue[newPriority];
×
159
              nextTask->prev = task->channel->queue[newPriority].prev;
×
160
              nextTask->next->prev = nextTask;
×
161
              nextTask->prev->next = nextTask;
×
162
            }
163
          }
164
        }
165
      }
166

167
      if (task->channel->scheduled != NULL) {
×
168
        int32_t priority = VATASK_PIORITY(task->channel->scheduled);
×
169
        task->channel->scheduled->next = &async->queue[priority];
×
170
        task->channel->scheduled->prev = async->queue[priority].prev;
×
171
        task->channel->scheduled->next->prev = task->channel->scheduled;
×
172
        task->channel->scheduled->prev->next = task->channel->scheduled;
×
173
      }
174
    }
175
  }
176

177
  ret = vHashDrop(async->taskTable, task);
1,014✔
178
  TAOS_UNUSED(ret);
179
  async->numTasks--;
1,014✔
180

181
  if (task->numWait == 0) {
1,014✔
182
    (void)taosThreadCondDestroy(&task->waitCond);
1,000✔
183
    taosMemoryFree(task);
1,000!
184
  } else if (task->numWait == 1) {
14!
185
    (void)taosThreadCondSignal(&task->waitCond);
14✔
186
  } else {
187
    (void)taosThreadCondBroadcast(&task->waitCond);
×
188
  }
189
}
1,014✔
190

191
static void vnodeAsyncCancelAllTasks(SVAsync *async, SArray *cancelArray) {
123✔
192
  while (async->queue[0].next != &async->queue[0] || async->queue[1].next != &async->queue[1] ||
123!
193
         async->queue[2].next != &async->queue[2]) {
123!
194
    for (int32_t i = 0; i < EVA_PRIORITY_MAX; i++) {
×
195
      while (async->queue[i].next != &async->queue[i]) {
×
196
        SVATask *task = async->queue[i].next;
×
197
        task->prev->next = task->next;
×
198
        task->next->prev = task->prev;
×
199
        if (task->cancel) {
×
200
          if (taosArrayPush(cancelArray, &(SVATaskCancelInfo){
×
201
                                             .cancel = task->cancel,
×
202
                                             .arg = task->arg,
×
203
                                         }) == NULL) {
204
            vError("failed to push cancel task into array");
×
205
          };
206
        }
207
        vnodeAsyncTaskDone(async, task);
×
208
      }
209
    }
210
  }
211
}
123✔
212

213
static void *vnodeAsyncLoop(void *arg) {
123✔
214
  SVWorker *worker = (SVWorker *)arg;
123✔
215
  SVAsync  *async = worker->async;
123✔
216
  SArray   *cancelArray = taosArrayInit(0, sizeof(SVATaskCancelInfo));
123✔
217
  if (cancelArray == NULL) {
123!
218
    return NULL;
×
219
  }
220

221
  setThreadName(async->label);
123✔
222

223
  for (;;) {
1,014✔
224
    (void)taosThreadMutexLock(&async->mutex);
1,137✔
225

226
    // finish last running task
227
    if (worker->runningTask != NULL) {
1,137✔
228
      vnodeAsyncTaskDone(async, worker->runningTask);
1,014✔
229
      worker->runningTask = NULL;
1,014✔
230
    }
231

232
    for (;;) {
233
      if (async->stop || worker->workerId >= async->numWorkers) {
2,137!
234
        if (async->stop) {  // cancel all tasks
123!
235
          vnodeAsyncCancelAllTasks(async, cancelArray);
123✔
236
        }
237
        worker->state = EVA_WORKER_STATE_STOP;
123✔
238
        async->numLaunchWorkers--;
123✔
239
        (void)taosThreadMutexUnlock(&async->mutex);
123✔
240
        goto _exit;
123✔
241
      }
242

243
      for (int32_t i = 0; i < EVA_PRIORITY_MAX; i++) {
8,056✔
244
        SVATask *task = async->queue[i].next;
6,042✔
245
        if (task != &async->queue[i]) {
6,042✔
246
          if (worker->runningTask == NULL) {
1,014!
247
            worker->runningTask = task;
1,014✔
248
            task->prev->next = task->next;
1,014✔
249
            task->next->prev = task->prev;
1,014✔
250
          } else {  // promote priority
251
            task->priorScore++;
×
252
            int32_t priority = VATASK_PIORITY(task);
×
253
            if (priority != i) {
×
254
              // remove from current priority queue
255
              task->prev->next = task->next;
×
256
              task->next->prev = task->prev;
×
257
              // add to new priority queue
258
              task->next = &async->queue[priority];
×
259
              task->prev = async->queue[priority].prev;
×
260
              task->next->prev = task;
×
261
              task->prev->next = task;
×
262
            }
263
          }
264
        }
265
      }
266

267
      if (worker->runningTask == NULL) {
2,014✔
268
        worker->state = EVA_WORKER_STATE_IDLE;
1,000✔
269
        async->numIdleWorkers++;
1,000✔
270
        (void)taosThreadCondWait(&async->hasTask, &async->mutex);
1,000✔
271
        async->numIdleWorkers--;
1,000✔
272
        worker->state = EVA_WORKER_STATE_ACTIVE;
1,000✔
273
      } else {
274
        worker->runningTask->state = EVA_TASK_STATE_RUNNING;
1,014✔
275
        break;
1,014✔
276
      }
277
    }
278

279
    (void)taosThreadMutexUnlock(&async->mutex);
1,014✔
280

281
    // do run the task
282
    int32_t code = worker->runningTask->execute(worker->runningTask->arg);
1,014✔
283
    TAOS_UNUSED(code);
284
  }
285

286
_exit:
123✔
287
  for (int32_t i = 0; i < taosArrayGetSize(cancelArray); i++) {
123!
288
    SVATaskCancelInfo *cancel = (SVATaskCancelInfo *)taosArrayGet(cancelArray, i);
×
289
    cancel->cancel(cancel->arg);
×
290
  }
291
  taosArrayDestroy(cancelArray);
123✔
292
  return NULL;
122✔
293
}
294

295
static uint32_t vnodeAsyncTaskHash(const void *obj) {
3,069✔
296
  SVATask *task = (SVATask *)obj;
3,069✔
297
  return MurmurHash3_32((const char *)(&task->taskId), sizeof(task->taskId));
3,069✔
298
}
299

300
static int32_t vnodeAsyncTaskCompare(const void *obj1, const void *obj2) {
1,033✔
301
  SVATask *task1 = (SVATask *)obj1;
1,033✔
302
  SVATask *task2 = (SVATask *)obj2;
1,033✔
303
  if (task1->taskId < task2->taskId) {
1,033!
304
    return -1;
×
305
  } else if (task1->taskId > task2->taskId) {
1,033!
306
    return 1;
×
307
  }
308
  return 0;
1,033✔
309
}
310

311
static uint32_t vnodeAsyncChannelHash(const void *obj) {
×
312
  SVAChannel *channel = (SVAChannel *)obj;
×
313
  return MurmurHash3_32((const char *)(&channel->channelId), sizeof(channel->channelId));
×
314
}
315

316
static int32_t vnodeAsyncChannelCompare(const void *obj1, const void *obj2) {
×
317
  SVAChannel *channel1 = (SVAChannel *)obj1;
×
318
  SVAChannel *channel2 = (SVAChannel *)obj2;
×
319
  if (channel1->channelId < channel2->channelId) {
×
320
    return -1;
×
321
  } else if (channel1->channelId > channel2->channelId) {
×
322
    return 1;
×
323
  }
324
  return 0;
×
325
}
326

327
static int32_t vnodeAsyncInit(SVAsync **async, const char *label) {
280✔
328
  int32_t ret;
329

330
  if (async == NULL) {
280!
331
    return TSDB_CODE_INVALID_PARA;
×
332
  }
333

334
  if (label == NULL) {
280!
335
    label = "anonymous";
×
336
  }
337

338
  (*async) = (SVAsync *)taosMemoryCalloc(1, sizeof(SVAsync) + strlen(label) + 1);
280!
339
  if ((*async) == NULL) {
280!
340
    return terrno;
×
341
  }
342

343
  memcpy((char *)((*async) + 1), label, strlen(label) + 1);
280✔
344
  (*async)->label = (const char *)((*async) + 1);
280✔
345

346
  (void)taosThreadMutexInit(&(*async)->mutex, NULL);
280✔
347
  (void)taosThreadCondInit(&(*async)->hasTask, NULL);
280✔
348
  (*async)->stop = false;
280✔
349

350
  // worker
351
  (*async)->numWorkers = VNODE_ASYNC_DEFAULT_WORKERS;
280✔
352
  (*async)->numLaunchWorkers = 0;
280✔
353
  (*async)->numIdleWorkers = 0;
280✔
354
  for (int32_t i = 0; i < VNODE_ASYNC_MAX_WORKERS; i++) {
71,960✔
355
    (*async)->workers[i].async = (*async);
71,680✔
356
    (*async)->workers[i].workerId = i;
71,680✔
357
    (*async)->workers[i].state = EVA_WORKER_STATE_UINIT;
71,680✔
358
    (*async)->workers[i].runningTask = NULL;
71,680✔
359
  }
360

361
  // channel
362
  (*async)->nextChannelId = 0;
280✔
363
  (*async)->numChannels = 0;
280✔
364
  (*async)->chList.prev = &(*async)->chList;
280✔
365
  (*async)->chList.next = &(*async)->chList;
280✔
366
  ret = vHashInit(&(*async)->channelTable, vnodeAsyncChannelHash, vnodeAsyncChannelCompare);
280✔
367
  if (ret != 0) {
280!
368
    (void)taosThreadMutexDestroy(&(*async)->mutex);
×
369
    (void)taosThreadCondDestroy(&(*async)->hasTask);
×
370
    taosMemoryFree(*async);
×
371
    return ret;
×
372
  }
373

374
  // task
375
  (*async)->nextTaskId = 0;
280✔
376
  (*async)->numTasks = 0;
280✔
377
  for (int32_t i = 0; i < EVA_PRIORITY_MAX; i++) {
1,120✔
378
    (*async)->queue[i].next = &(*async)->queue[i];
840✔
379
    (*async)->queue[i].prev = &(*async)->queue[i];
840✔
380
  }
381
  ret = vHashInit(&(*async)->taskTable, vnodeAsyncTaskHash, vnodeAsyncTaskCompare);
280✔
382
  if (ret != 0) {
280!
383
    vHashDestroy(&(*async)->channelTable);
×
384
    (void)taosThreadMutexDestroy(&(*async)->mutex);
×
385
    (void)taosThreadCondDestroy(&(*async)->hasTask);
×
386
    taosMemoryFree(*async);
×
387
    return ret;
×
388
  }
389

390
  return 0;
280✔
391
}
392

393
static int32_t vnodeAsyncDestroy(SVAsync **async) {
280✔
394
  if ((*async) == NULL) {
280!
395
    return TSDB_CODE_INVALID_PARA;
×
396
  }
397

398
  // set stop and broadcast
399
  (void)taosThreadMutexLock(&(*async)->mutex);
280✔
400
  (*async)->stop = true;
280✔
401
  (void)taosThreadCondBroadcast(&(*async)->hasTask);
280✔
402
  (void)taosThreadMutexUnlock(&(*async)->mutex);
280✔
403

404
  // join all workers
405
  for (int32_t i = 0; i < VNODE_ASYNC_MAX_WORKERS; i++) {
71,960✔
406
    (void)taosThreadMutexLock(&(*async)->mutex);
71,680✔
407
    EVWorkerState state = (*async)->workers[i].state;
71,680✔
408
    (void)taosThreadMutexUnlock(&(*async)->mutex);
71,680✔
409

410
    if (state == EVA_WORKER_STATE_UINIT) {
71,680✔
411
      continue;
71,557✔
412
    }
413

414
    (void)taosThreadJoin((*async)->workers[i].thread, NULL);
123✔
415
    (*async)->workers[i].state = EVA_WORKER_STATE_UINIT;
123✔
416
  }
417

418
  // close all channels
419
  for (SVAChannel *channel = (*async)->chList.next; channel != &(*async)->chList; channel = (*async)->chList.next) {
280!
420
    channel->next->prev = channel->prev;
×
421
    channel->prev->next = channel->next;
×
422

423
    int32_t ret = vHashDrop((*async)->channelTable, channel);
×
424
    TAOS_UNUSED(ret);
425
    (*async)->numChannels--;
×
426
    taosMemoryFree(channel);
×
427
  }
428

429
  (void)taosThreadMutexDestroy(&(*async)->mutex);
280✔
430
  (void)taosThreadCondDestroy(&(*async)->hasTask);
280✔
431

432
  vHashDestroy(&(*async)->channelTable);
280✔
433
  vHashDestroy(&(*async)->taskTable);
280✔
434
  taosMemoryFree(*async);
280!
435
  *async = NULL;
280✔
436

437
  return 0;
280✔
438
}
439

440
static void vnodeAsyncLaunchWorker(SVAsync *async) {
123✔
441
  TdThreadAttr thAttr;
442
  (void)taosThreadAttrInit(&thAttr);
123✔
443
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
123✔
444
#ifdef TD_COMPACT_OS
445
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
446
#endif
447
  for (int32_t i = 0; i < async->numWorkers; i++) {
217!
448
    if (async->workers[i].state == EVA_WORKER_STATE_ACTIVE) {
217✔
449
      continue;
94✔
450
    } else if (async->workers[i].state == EVA_WORKER_STATE_STOP) {
123!
451
      int32_t ret = taosThreadJoin(async->workers[i].thread, NULL);
×
452
      async->workers[i].state = EVA_WORKER_STATE_UINIT;
×
453
    }
454

455
    int32_t ret = taosThreadCreate(&async->workers[i].thread, &thAttr, vnodeAsyncLoop, &async->workers[i]);
123✔
456
    if (ret) {
123!
457
      vError("failed to create worker thread since %s", tstrerror(ret));
×
458
    } else {
459
      async->workers[i].state = EVA_WORKER_STATE_ACTIVE;
123✔
460
      async->numLaunchWorkers++;
123✔
461
    }
462
    break;
123✔
463
  }
464
#ifdef TD_ASTRA
465
  (void)taosThreadAttrDestroy(&thAttr);
466
#endif
467
}
123✔
468

469
int32_t vnodeAsyncOpen() {
70✔
470
  int32_t code = 0;
70✔
471
  int32_t lino = 0;
70✔
472

473
  int32_t numOfThreads[] = {
70✔
474
      0,                        //
475
      tsNumOfCommitThreads,     // vnode-commit
476
      tsNumOfCommitThreads,     // vnode-merge
477
      tsNumOfCompactThreads,    // vnode-compact
478
      tsNumOfRetentionThreads,  // vnode-retention
479
  };
480

481
  for (int32_t i = 1; i < sizeof(GVnodeAsyncs) / sizeof(GVnodeAsyncs[0]); i++) {
350✔
482
    code = vnodeAsyncInit(&GVnodeAsyncs[i].async, GVnodeAsyncs[i].label);
280✔
483
    TSDB_CHECK_CODE(code, lino, _exit);
280!
484

485
    code = vnodeAsyncSetWorkers(i, numOfThreads[i]);
280✔
486
    TSDB_CHECK_CODE(code, lino, _exit);
280!
487
  }
488

489
_exit:
70✔
490
  return code;
70✔
491
}
492

493
void vnodeAsyncClose() {
70✔
494
  for (int32_t i = 1; i < sizeof(GVnodeAsyncs) / sizeof(GVnodeAsyncs[0]); i++) {
350✔
495
    int32_t ret = vnodeAsyncDestroy(&GVnodeAsyncs[i].async);
280✔
496
  }
497
}
70✔
498

499
int32_t vnodeAsync(int64_t async, EVAPriority priority, int32_t (*execute)(void *), void (*complete)(void *), void *arg,
1,013✔
500
                   SVATaskID *taskID) {
501
  SVAChannelID channelID = {
1,013✔
502
      .async = async,
503
      .id = 0,
504
  };
505
  return vnodeAsyncC(&channelID, priority, execute, complete, arg, taskID);
1,013✔
506
}
507

508
int32_t vnodeAsyncC(SVAChannelID *channelID, EVAPriority priority, int32_t (*execute)(void *), void (*cancel)(void *),
1,013✔
509
                    void *arg, SVATaskID *taskID) {
510
  if (channelID == NULL || channelID->async < MIN_ASYNC_ID || channelID->async > MAX_ASYNC_ID || execute == NULL ||
1,013!
511
      channelID->id < 0) {
1,013!
512
    return TSDB_CODE_INVALID_PARA;
×
513
  }
514

515
  int32_t  ret;
516
  int64_t  id;
517
  SVAsync *async = GVnodeAsyncs[channelID->async].async;
1,013✔
518

519
  // create task object
520
  SVATask *task = (SVATask *)taosMemoryCalloc(1, sizeof(SVATask));
1,013!
521
  if (task == NULL) {
1,012!
522
    return terrno;
×
523
  }
524

525
  task->priority = priority;
1,012✔
526
  task->priorScore = 0;
1,012✔
527
  task->execute = execute;
1,012✔
528
  task->cancel = cancel;
1,012✔
529
  task->arg = arg;
1,012✔
530
  task->state = EVA_TASK_STATE_WAITTING;
1,012✔
531
  task->numWait = 0;
1,012✔
532
  (void)taosThreadCondInit(&task->waitCond, NULL);
1,012✔
533

534
  // schedule task
535
  (void)taosThreadMutexLock(&async->mutex);
1,012✔
536

537
  if (channelID->id == 0) {
1,014!
538
    task->channel = NULL;
1,014✔
539
  } else {
540
    SVAChannel channel = {
×
541
        .channelId = channelID->id,
×
542
    };
543
    ret = vHashGet(async->channelTable, &channel, (void **)&task->channel);
×
544
    TAOS_UNUSED(ret);
545
    if (task->channel == NULL) {
×
546
      (void)taosThreadMutexUnlock(&async->mutex);
×
547
      (void)taosThreadCondDestroy(&task->waitCond);
×
548
      taosMemoryFree(task);
×
549
      return TSDB_CODE_INVALID_PARA;
×
550
    }
551
  }
552

553
  task->taskId = id = ++async->nextTaskId;
1,014✔
554

555
  // add task to hash table
556
  ret = vHashPut(async->taskTable, task);
1,014✔
557
  if (ret != 0) {
1,014!
558
    (void)taosThreadMutexUnlock(&async->mutex);
×
559
    (void)taosThreadCondDestroy(&task->waitCond);
×
560
    taosMemoryFree(task);
×
561
    return ret;
×
562
  }
563

564
  async->numTasks++;
1,014✔
565

566
  // add task to queue
567
  if (task->channel == NULL || task->channel->scheduled == NULL) {
1,014!
568
    // add task to async->queue
569
    if (task->channel) {
1,014!
570
      task->channel->scheduled = task;
×
571
    }
572

573
    task->next = &async->queue[priority];
1,014✔
574
    task->prev = async->queue[priority].prev;
1,014✔
575
    task->next->prev = task;
1,014✔
576
    task->prev->next = task;
1,014✔
577

578
    // signal worker or launch new worker
579
    if (async->numIdleWorkers > 0) {
1,014✔
580
      (void)taosThreadCondSignal(&(async->hasTask));
880✔
581
    } else if (async->numLaunchWorkers < async->numWorkers) {
134✔
582
      vnodeAsyncLaunchWorker(async);
123✔
583
    }
584
  } else if (task->channel->scheduled->state == EVA_TASK_STATE_RUNNING ||
×
585
             priority >= VATASK_PIORITY(task->channel->scheduled)) {
×
586
    // add task to task->channel->queue
587
    task->next = &task->channel->queue[priority];
×
588
    task->prev = task->channel->queue[priority].prev;
×
589
    task->next->prev = task;
×
590
    task->prev->next = task;
×
591
  } else {
592
    // remove task->channel->scheduled from queue
593
    task->channel->scheduled->prev->next = task->channel->scheduled->next;
×
594
    task->channel->scheduled->next->prev = task->channel->scheduled->prev;
×
595

596
    // promote priority and add task->channel->scheduled to task->channel->queue
597
    task->channel->scheduled->priorScore++;
×
598
    int32_t newPriority = VATASK_PIORITY(task->channel->scheduled);
×
599
    task->channel->scheduled->next = &task->channel->queue[newPriority];
×
600
    task->channel->scheduled->prev = task->channel->queue[newPriority].prev;
×
601
    task->channel->scheduled->next->prev = task->channel->scheduled;
×
602
    task->channel->scheduled->prev->next = task->channel->scheduled;
×
603

604
    // add task to queue
605
    task->channel->scheduled = task;
×
606
    task->next = &async->queue[priority];
×
607
    task->prev = async->queue[priority].prev;
×
608
    task->next->prev = task;
×
609
    task->prev->next = task;
×
610
  }
611

612
  (void)taosThreadMutexUnlock(&async->mutex);
1,014✔
613

614
  if (taskID != NULL) {
1,014!
615
    taskID->async = channelID->async;
1,014✔
616
    taskID->id = id;
1,014✔
617
  }
618

619
  return 0;
1,014✔
620
}
621

622
void vnodeAWait(SVATaskID *taskID) {
1,556✔
623
  if (taskID == NULL || taskID->async < MIN_ASYNC_ID || taskID->async > MAX_ASYNC_ID || taskID->id <= 0) {
1,556!
624
    return;
813✔
625
  }
626

627
  SVAsync *async = GVnodeAsyncs[taskID->async].async;
743✔
628
  SVATask *task = NULL;
743✔
629
  SVATask  task2 = {
743✔
630
       .taskId = taskID->id,
743✔
631
  };
632

633
  (void)taosThreadMutexLock(&async->mutex);
743✔
634

635
  int32_t ret = vHashGet(async->taskTable, &task2, (void **)&task);
743✔
636
  if (task) {
743✔
637
    task->numWait++;
14✔
638
    (void)taosThreadCondWait(&task->waitCond, &async->mutex);
14✔
639
    task->numWait--;
14✔
640

641
    if (task->numWait == 0) {
14!
642
      (void)taosThreadCondDestroy(&task->waitCond);
14✔
643
      taosMemoryFree(task);
14!
644
    }
645
  }
646

647
  (void)taosThreadMutexUnlock(&async->mutex);
743✔
648
}
649

650
int32_t vnodeACancel(SVATaskID *taskID) {
558✔
651
  if (taskID == NULL || taskID->async < MIN_ASYNC_ID || taskID->async > MAX_ASYNC_ID || taskID->id <= 0) {
558!
652
    return TSDB_CODE_INVALID_PARA;
534✔
653
  }
654

655
  int32_t  ret = 0;
24✔
656
  SVAsync *async = GVnodeAsyncs[taskID->async].async;
24✔
657
  SVATask *task = NULL;
24✔
658
  SVATask  task2 = {
24✔
659
       .taskId = taskID->id,
24✔
660
  };
661
  void (*cancel)(void *) = NULL;
24✔
662
  void *arg = NULL;
24✔
663

664
  (void)taosThreadMutexLock(&async->mutex);
24✔
665

666
  ret = vHashGet(async->taskTable, &task2, (void **)&task);
24✔
667
  if (task) {
24✔
668
    if (task->state == EVA_TASK_STATE_WAITTING) {
2!
669
      cancel = task->cancel;
×
670
      arg = task->arg;
×
671
      task->next->prev = task->prev;
×
672
      task->prev->next = task->next;
×
673
      vnodeAsyncTaskDone(async, task);
×
674
    } else {
675
      ret = TSDB_CODE_FAILED;
2✔
676
    }
677
  }
678

679
  (void)taosThreadMutexUnlock(&async->mutex);
24✔
680

681
  if (cancel) {
24!
682
    cancel(arg);
×
683
  }
684

685
  return ret;
24✔
686
}
687

688
int32_t vnodeAsyncSetWorkers(int64_t asyncID, int32_t numWorkers) {
280✔
689
  if (asyncID < MIN_ASYNC_ID || asyncID > MAX_ASYNC_ID || numWorkers <= 0 || numWorkers > VNODE_ASYNC_MAX_WORKERS) {
280!
690
    return TSDB_CODE_INVALID_PARA;
×
691
  }
692
  int32_t  ret;
693
  SVAsync *async = GVnodeAsyncs[asyncID].async;
280✔
694
  (void)taosThreadMutexLock(&async->mutex);
280✔
695
  async->numWorkers = numWorkers;
280✔
696
  if (async->numIdleWorkers > 0) {
280!
697
    (void)taosThreadCondBroadcast(&async->hasTask);
×
698
  }
699
  (void)taosThreadMutexUnlock(&async->mutex);
280✔
700

701
  return 0;
280✔
702
}
703

704
int32_t vnodeAChannelInit(int64_t asyncID, SVAChannelID *channelID) {
×
705
  if (channelID == NULL || asyncID < MIN_ASYNC_ID || asyncID > MAX_ASYNC_ID) {
×
706
    return TSDB_CODE_INVALID_PARA;
×
707
  }
708

709
  SVAsync *async = GVnodeAsyncs[asyncID].async;
×
710

711
  // create channel object
712
  SVAChannel *channel = (SVAChannel *)taosMemoryMalloc(sizeof(SVAChannel));
×
713
  if (channel == NULL) {
×
714
    return terrno;
×
715
  }
716
  channel->state = EVA_CHANNEL_STATE_OPEN;
×
717
  for (int32_t i = 0; i < EVA_PRIORITY_MAX; i++) {
×
718
    channel->queue[i].next = &channel->queue[i];
×
719
    channel->queue[i].prev = &channel->queue[i];
×
720
  }
721
  channel->scheduled = NULL;
×
722

723
  // register channel
724
  (void)taosThreadMutexLock(&async->mutex);
×
725

726
  channel->channelId = channelID->id = ++async->nextChannelId;
×
727

728
  // add to hash table
729
  int32_t ret = vHashPut(async->channelTable, channel);
×
730
  if (ret != 0) {
×
731
    (void)taosThreadMutexUnlock(&async->mutex);
×
732
    taosMemoryFree(channel);
×
733
    return ret;
×
734
  }
735

736
  // add to list
737
  channel->next = &async->chList;
×
738
  channel->prev = async->chList.prev;
×
739
  channel->next->prev = channel;
×
740
  channel->prev->next = channel;
×
741

742
  async->numChannels++;
×
743

744
  (void)taosThreadMutexUnlock(&async->mutex);
×
745

746
  channelID->async = asyncID;
×
747
  return 0;
×
748
}
749

750
int32_t vnodeAChannelDestroy(SVAChannelID *channelID, bool waitRunning) {
×
751
  if (channelID == NULL || channelID->async < MIN_ASYNC_ID || channelID->async > MAX_ASYNC_ID || channelID->id <= 0) {
×
752
    return TSDB_CODE_INVALID_PARA;
×
753
  }
754

755
  SVAsync    *async = GVnodeAsyncs[channelID->async].async;
×
756
  SVAChannel *channel = NULL;
×
757
  SVAChannel  channel2 = {
×
758
       .channelId = channelID->id,
×
759
  };
760
  SArray *cancelArray = taosArrayInit(0, sizeof(SVATaskCancelInfo));
×
761
  if (cancelArray == NULL) {
×
762
    return terrno;
×
763
  }
764

765
  (void)taosThreadMutexLock(&async->mutex);
×
766

767
  int32_t ret = vHashGet(async->channelTable, &channel2, (void **)&channel);
×
768
  TAOS_UNUSED(ret);
769
  if (channel) {
×
770
    // unregister channel
771
    channel->next->prev = channel->prev;
×
772
    channel->prev->next = channel->next;
×
773
    ret = vHashDrop(async->channelTable, channel);
×
774
    async->numChannels--;
×
775

776
    // cancel all waiting tasks
777
    for (int32_t i = 0; i < EVA_PRIORITY_MAX; i++) {
×
778
      while (channel->queue[i].next != &channel->queue[i]) {
×
779
        SVATask *task = channel->queue[i].next;
×
780
        task->prev->next = task->next;
×
781
        task->next->prev = task->prev;
×
782
        if (task->cancel) {
×
783
          if (taosArrayPush(cancelArray, &(SVATaskCancelInfo){
×
784
                                             .cancel = task->cancel,
×
785
                                             .arg = task->arg,
×
786
                                         }) == NULL) {
787
            vError("failed to push cancel info");
×
788
          };
789
        }
790
        vnodeAsyncTaskDone(async, task);
×
791
      }
792
    }
793

794
    // cancel or wait the scheduled task
795
    if (channel->scheduled == NULL || channel->scheduled->state == EVA_TASK_STATE_WAITTING) {
×
796
      if (channel->scheduled) {
×
797
        channel->scheduled->prev->next = channel->scheduled->next;
×
798
        channel->scheduled->next->prev = channel->scheduled->prev;
×
799
        if (channel->scheduled->cancel) {
×
800
          if (taosArrayPush(cancelArray, &(SVATaskCancelInfo){
×
801
                                             .cancel = channel->scheduled->cancel,
×
802
                                             .arg = channel->scheduled->arg,
×
803
                                         }) == NULL) {
804
            vError("failed to push cancel info");
×
805
          }
806
        }
807
        vnodeAsyncTaskDone(async, channel->scheduled);
×
808
      }
809
      taosMemoryFree(channel);
×
810
    } else {
811
      if (waitRunning) {
×
812
        // wait task
813
        SVATask *task = channel->scheduled;
×
814
        task->numWait++;
×
815
        (void)taosThreadCondWait(&task->waitCond, &async->mutex);
×
816
        task->numWait--;
×
817
        if (task->numWait == 0) {
×
818
          (void)taosThreadCondDestroy(&task->waitCond);
×
819
          taosMemoryFree(task);
×
820
        }
821

822
        taosMemoryFree(channel);
×
823
      } else {
824
        channel->state = EVA_CHANNEL_STATE_CLOSE;
×
825
      }
826
    }
827
  }
828

829
  (void)taosThreadMutexUnlock(&async->mutex);
×
830
  for (int32_t i = 0; i < taosArrayGetSize(cancelArray); i++) {
×
831
    SVATaskCancelInfo *cancel = (SVATaskCancelInfo *)taosArrayGet(cancelArray, i);
×
832
    cancel->cancel(cancel->arg);
×
833
  }
834
  taosArrayDestroy(cancelArray);
×
835

836
  channelID->async = 0;
×
837
  channelID->id = 0;
×
838
  return 0;
×
839
}
840

841
const char *vnodeGetATaskName(EVATaskT taskType) {
1,764✔
842
  switch (taskType) {
1,764!
843
    case EVA_TASK_COMMIT:
1,174✔
844
      return "vnode-commit";
1,174✔
845
    case EVA_TASK_MERGE:
590✔
846
      return "vnode-merge";
590✔
847
    case EVA_TASK_COMPACT:
×
848
      return "vnode-compact";
×
849
    case EVA_TASK_RETENTION:
×
850
      return "vnode-retention";
×
851
    default:
×
852
      return "unknown";
×
853
  }
854
}
855

856
bool vnodeATaskValid(SVATaskID *taskID) {
298✔
857
  if (taskID == NULL || taskID->async < MIN_ASYNC_ID || taskID->async > MAX_ASYNC_ID || taskID->id <= 0) {
298!
858
    return false;
24✔
859
  }
860

861
  SVAsync *async = GVnodeAsyncs[taskID->async].async;
274✔
862
  SVATask  task2 = {
274✔
863
       .taskId = taskID->id,
274✔
864
  };
865
  SVATask *task = NULL;
274✔
866

867
  (void)taosThreadMutexLock(&async->mutex);
274✔
868
  int32_t ret = vHashGet(async->taskTable, &task2, (void **)&task);
274✔
869
  (void)taosThreadMutexUnlock(&async->mutex);
274✔
870

871
  return ret == 0 && task != NULL;
274!
872
}
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