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

taosdata / TDengine / #3572

02 Jan 2025 08:57AM UTC coverage: 63.077% (-0.2%) from 63.276%
#3572

push

travis-ci

web-flow
Merge pull request #29450 from taosdata/fix/TS-5651-skip-sync-heartbeat

fix:[TS-5651]skip-sync-heartbeat

139525 of 284348 branches covered (49.07%)

Branch coverage included in aggregate %.

217427 of 281548 relevant lines covered (77.23%)

18571459.85 hits per line

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

75.94
/source/libs/stream/src/streamQueue.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 "streamInt.h"
17

18
#define MAX_STREAM_EXEC_BATCH_NUM 32
19
#define MAX_SMOOTH_BURST_RATIO    5  // 5 sec
20

21
// todo refactor:
22
// read data from input queue
23
typedef struct SQueueReader {
24
  SStreamQueue* pQueue;
25
  int32_t       taskLevel;
26
  int32_t       maxBlocks;     // maximum block in one batch
27
  int32_t       waitDuration;  // maximum wait time to format several block into a batch to process, unit: ms
28
} SQueueReader;
29

30
#define streamQueueCurItem(_q) ((_q)->qItem)
31

32
static bool streamTaskExtractAvailableToken(STokenBucket* pBucket, const char* id);
33
static void streamTaskPutbackToken(STokenBucket* pBucket);
34
static void streamTaskConsumeQuota(STokenBucket* pBucket, int32_t bytes);
35

36
static void streamQueueCleanup(SStreamQueue* pQueue) {
29,361✔
37
  SStreamQueueItem* qItem = NULL;
29,361✔
38
  while (1) {
39
    streamQueueNextItem(pQueue, &qItem);
30,087✔
40
    if (qItem == NULL) {
30,106✔
41
      break;
29,379✔
42
    }
43
    streamFreeQitem(qItem);
727✔
44
  }
45
  pQueue->status = STREAM_QUEUE__SUCESS;
29,379✔
46
}
29,379✔
47

48
int32_t streamQueueOpen(int64_t cap, SStreamQueue** pQ) {
29,868✔
49
  *pQ = NULL;
29,868✔
50
  int32_t code = 0;
29,868✔
51

52
  SStreamQueue* pQueue = taosMemoryCalloc(1, sizeof(SStreamQueue));
29,868!
53
  if (pQueue == NULL) {
29,891!
54
    return terrno;
×
55
  }
56

57
  code = taosOpenQueue(&pQueue->pQueue);
29,891✔
58
  if (code) {
29,875!
59
    taosMemoryFreeClear(pQueue);
×
60
    return code;
×
61
  }
62

63
  code = taosAllocateQall(&pQueue->qall);
29,875✔
64
  if (code) {
29,895!
65
    taosCloseQueue(pQueue->pQueue);
×
66
    taosMemoryFree(pQueue);
×
67
    return code;
×
68
  }
69

70
  pQueue->status = STREAM_QUEUE__SUCESS;
29,895✔
71
  taosSetQueueCapacity(pQueue->pQueue, cap);
29,895✔
72
  taosSetQueueMemoryCapacity(pQueue->pQueue, cap * 1024);
29,886✔
73

74
  *pQ = pQueue;
29,894✔
75
  return code;
29,894✔
76
}
77

78
void streamQueueClose(SStreamQueue* pQueue, int32_t taskId) {
29,361✔
79
  stDebug("s-task:0x%x free the queue:%p, items in queue:%d", taskId, pQueue->pQueue,
29,361✔
80
          taosQueueItemSize(pQueue->pQueue));
81
  streamQueueCleanup(pQueue);
29,361✔
82

83
  taosFreeQall(pQueue->qall);
29,368✔
84
  taosCloseQueue(pQueue->pQueue);
29,383✔
85
  taosMemoryFree(pQueue);
29,383!
86
}
29,383✔
87

88
void streamQueueNextItem(SStreamQueue* pQueue, SStreamQueueItem** pItem) {
575,721✔
89
  *pItem = NULL;
575,721✔
90
  int8_t flag = atomic_exchange_8(&pQueue->status, STREAM_QUEUE__PROCESSING);
575,721✔
91

92
  if (flag == STREAM_QUEUE__FAILED) {
575,897✔
93
    *pItem = streamQueueCurItem(pQueue);
3,297✔
94
  } else {
95
    pQueue->qItem = NULL;
572,600✔
96
    (void) taosGetQitem(pQueue->qall, &pQueue->qItem);
572,600✔
97
    if (pQueue->qItem == NULL) {
572,563✔
98
      (void) taosReadAllQitems(pQueue->pQueue, pQueue->qall);
231,310✔
99
      (void) taosGetQitem(pQueue->qall, &pQueue->qItem);
231,361✔
100
    }
101

102
    *pItem = streamQueueCurItem(pQueue);
572,567✔
103
  }
104
}
575,864✔
105

106
void streamQueueProcessSuccess(SStreamQueue* queue) {
371,772✔
107
  if (atomic_load_8(&queue->status) != STREAM_QUEUE__PROCESSING) {
371,772!
108
    stError("invalid queue status:%d, expect:%d", atomic_load_8(&queue->status), STREAM_QUEUE__PROCESSING);
×
109
    return;
×
110
  }
111

112
  queue->qItem = NULL;
371,776✔
113
  atomic_store_8(&queue->status, STREAM_QUEUE__SUCESS);
371,776✔
114
}
115

116
void streamQueueProcessFail(SStreamQueue* queue) {
3,296✔
117
  if (atomic_load_8(&queue->status) != STREAM_QUEUE__PROCESSING) {
3,296!
118
    stError("invalid queue status:%d, expect:%d", atomic_load_8(&queue->status), STREAM_QUEUE__PROCESSING);
×
119
    return;
×
120
  }
121
  atomic_store_8(&queue->status, STREAM_QUEUE__FAILED);
3,296✔
122
}
123

124
bool streamQueueIsFull(const SStreamQueue* pQueue) {
620,297✔
125
  int32_t numOfItems = streamQueueGetNumOfItems(pQueue);
620,297✔
126
  if (numOfItems >= STREAM_TASK_QUEUE_CAPACITY) {
620,409✔
127
    return true;
45✔
128
  }
129

130
  return (SIZE_IN_MiB(taosQueueMemorySize(pQueue->pQueue)) >= STREAM_TASK_QUEUE_CAPACITY_IN_SIZE);
620,364✔
131
}
132

133
int32_t streamQueueGetNumOfItems(const SStreamQueue* pQueue) {
1,235,901✔
134
  int32_t numOfItems1 = taosQueueItemSize(pQueue->pQueue);
1,235,901✔
135
  int32_t numOfItems2 = taosQallItemSize(pQueue->qall);
1,236,286✔
136

137
  return numOfItems1 + numOfItems2;
1,236,224✔
138
}
139

140
int32_t streamQueueGetNumOfUnAccessedItems(const SStreamQueue* pQueue) {
48,574✔
141
  int32_t numOfItems1 = taosQueueItemSize(pQueue->pQueue);
48,574✔
142
  int32_t numOfItems2 = taosQallUnAccessedItemSize(pQueue->qall);
48,581✔
143

144
  return numOfItems1 + numOfItems2;
48,579✔
145
}
146

147
int32_t streamQueueGetItemSize(const SStreamQueue* pQueue) {
60,170✔
148
  return taosQueueMemorySize(pQueue->pQueue) + taosQallUnAccessedMemSize(pQueue->qall);
60,170✔
149
}
150

151
int32_t streamQueueItemGetSize(const SStreamQueueItem* pItem) {
371,769✔
152
  STaosQnode* p = (STaosQnode*)((char*)pItem - sizeof(STaosQnode));
371,769✔
153
  return p->dataSize;
371,769✔
154
}
155

156
void streamQueueItemIncSize(const SStreamQueueItem* pItem, int32_t size) {
320,976✔
157
  STaosQnode* p = (STaosQnode*)((char*)pItem - sizeof(STaosQnode));
320,976✔
158
  p->dataSize += size;
320,976✔
159
}
320,976✔
160

161
const char* streamQueueItemGetTypeStr(int32_t type) {
69,908✔
162
  switch (type) {
69,908✔
163
    case STREAM_INPUT__CHECKPOINT:
4,620✔
164
      return "checkpoint";
4,620✔
165
    case STREAM_INPUT__CHECKPOINT_TRIGGER:
18,344✔
166
      return "checkpoint-trigger";
18,344✔
167
    case STREAM_INPUT__TRANS_STATE:
16,137✔
168
      return "trans-state";
16,137✔
169
    case STREAM_INPUT__REF_DATA_BLOCK:
5,995✔
170
      return "ref-block";
5,995✔
171
    default:
24,812✔
172
      return "datablock";
24,812✔
173
  }
174
}
175

176
EExtractDataCode streamTaskGetDataFromInputQ(SStreamTask* pTask, SStreamQueueItem** pInput, int32_t* numOfBlocks,
140,618✔
177
                                             int32_t* blockSize) {
178
  const char* id = pTask->id.idStr;
140,618✔
179
  int32_t     taskLevel = pTask->info.taskLevel;
140,618✔
180

181
  *pInput = NULL;
140,618✔
182
  *numOfBlocks = 0;
140,618✔
183
  *blockSize = 0;
140,618✔
184

185
  // no available token in bucket for sink task, let's wait for a little bit
186
  if (taskLevel == TASK_LEVEL__SINK && (!streamTaskExtractAvailableToken(pTask->outputInfo.pTokenBucket, id))) {
140,618!
187
    stDebug("s-task:%s no available token in bucket for sink data, wait for 10ms", id);
×
188
    return EXEC_AFTER_IDLE;
×
189
  }
190

191
  while (1) {
363,722✔
192
    if (streamTaskShouldPause(pTask) || streamTaskShouldStop(pTask)) {
504,328!
193
      stDebug("s-task:%s task should pause, extract input blocks:%d", id, *numOfBlocks);
×
194
      return EXEC_CONTINUE;
140,627✔
195
    }
196

197
    SStreamQueueItem* qItem = NULL;
504,244✔
198
    streamQueueNextItem(pTask->inputq.queue, (SStreamQueueItem**)&qItem);
504,244✔
199
    if (qItem == NULL) {
504,329✔
200
      // restore the token to bucket
201
      if (*numOfBlocks > 0) {
103,427✔
202
        *blockSize = streamQueueItemGetSize(*pInput);
39,409✔
203
        if (taskLevel == TASK_LEVEL__SINK) {
39,407✔
204
          streamTaskConsumeQuota(pTask->outputInfo.pTokenBucket, *blockSize);
13,626✔
205
        }
206
      } else {
207
        streamTaskPutbackToken(pTask->outputInfo.pTokenBucket);
64,018✔
208
      }
209

210
      return EXEC_CONTINUE;
103,408✔
211
    }
212

213
    // do not merge blocks for sink node and check point data block
214
    int8_t type = qItem->type;
400,902✔
215
    if (type == STREAM_INPUT__CHECKPOINT || type == STREAM_INPUT__CHECKPOINT_TRIGGER ||
400,902✔
216
        type == STREAM_INPUT__TRANS_STATE || type == STREAM_INPUT__REF_DATA_BLOCK) {
376,514✔
217
      const char* p = streamQueueItemGetTypeStr(type);
28,979✔
218

219
      if (*pInput == NULL) {
28,985✔
220
        stDebug("s-task:%s %s msg extracted, start to process immediately", id, p);
25,824✔
221

222
        // restore the token to bucket in case of checkpoint/trans-state msg
223
        streamTaskPutbackToken(pTask->outputInfo.pTokenBucket);
25,824✔
224
        *blockSize = 0;
25,820✔
225
        *numOfBlocks = 1;
25,820✔
226
        *pInput = qItem;
25,820✔
227
        return EXEC_CONTINUE;
25,820✔
228
      } else {  // previous existed blocks needs to be handle, before handle the checkpoint msg block
229
        stDebug("s-task:%s %s msg extracted, handle previous blocks, numOfBlocks:%d", id, p, *numOfBlocks);
3,161✔
230
        *blockSize = streamQueueItemGetSize(*pInput);
3,161✔
231
        if (taskLevel == TASK_LEVEL__SINK) {
3,161✔
232
          streamTaskConsumeQuota(pTask->outputInfo.pTokenBucket, *blockSize);
851✔
233
        }
234

235
        streamQueueProcessFail(pTask->inputq.queue);
3,161✔
236
        return EXEC_CONTINUE;
3,162✔
237
      }
238
    } else {
239
      if (*pInput == NULL) {
371,923✔
240
        *pInput = qItem;
50,796✔
241
      } else { // merge current block failed, let's handle the already merged blocks.
242
        void*   newRet = NULL;
321,127✔
243
        int32_t code = streamQueueMergeQueueItem(*pInput, qItem, (SStreamQueueItem**)&newRet);
321,127✔
244
        if (newRet == NULL) {
321,127✔
245
          if (code != -1) {
137!
246
            stError("s-task:%s failed to merge blocks from inputQ, numOfBlocks:%d, code:%s", id, *numOfBlocks,
×
247
                    tstrerror(code));
248
          }
249

250
          *blockSize = streamQueueItemGetSize(*pInput);
137✔
251
          if (taskLevel == TASK_LEVEL__SINK) {
135!
252
            streamTaskConsumeQuota(pTask->outputInfo.pTokenBucket, *blockSize);
×
253
          }
254

255
          streamQueueProcessFail(pTask->inputq.queue);
135✔
256
          return EXEC_CONTINUE;
135✔
257
        }
258

259
        *pInput = newRet;
320,990✔
260
      }
261

262
      *numOfBlocks += 1;
371,786✔
263
      streamQueueProcessSuccess(pTask->inputq.queue);
371,786✔
264

265
      if (*numOfBlocks >= MAX_STREAM_EXEC_BATCH_NUM) {
371,821✔
266
        stDebug("s-task:%s batch size limit:%d reached, start to process blocks", id, MAX_STREAM_EXEC_BATCH_NUM);
8,099✔
267

268
        *blockSize = streamQueueItemGetSize(*pInput);
8,099✔
269
        if (taskLevel == TASK_LEVEL__SINK) {
8,101✔
270
          streamTaskConsumeQuota(pTask->outputInfo.pTokenBucket, *blockSize);
32✔
271
        }
272

273
        return EXEC_CONTINUE;
8,101✔
274
      }
275
    }
276
  }
277
}
278

279
int32_t streamTaskPutDataIntoInputQ(SStreamTask* pTask, SStreamQueueItem* pItem) {
398,258✔
280
  int8_t      type = pItem->type;
398,258✔
281
  STaosQueue* pQueue = pTask->inputq.queue->pQueue;
398,258✔
282
  int32_t     total = streamQueueGetNumOfItems(pTask->inputq.queue) + 1;
398,258✔
283

284
  if (type == STREAM_INPUT__DATA_SUBMIT) {
398,292✔
285
    SStreamDataSubmit* px = (SStreamDataSubmit*)pItem;
332,636✔
286
    if ((pTask->info.taskLevel == TASK_LEVEL__SOURCE) && streamQueueIsFull(pTask->inputq.queue)) {
332,636!
287
      double size = SIZE_IN_MiB(taosQueueMemorySize(pQueue));
15✔
288
      stTrace(
15!
289
          "s-task:%s inputQ is full, capacity(size:%d num:%dMiB), current(blocks:%d, size:%.2fMiB) stop to push data",
290
          pTask->id.idStr, STREAM_TASK_QUEUE_CAPACITY, STREAM_TASK_QUEUE_CAPACITY_IN_SIZE, total, size);
291
      streamDataSubmitDestroy(px);
15✔
292
      return TSDB_CODE_STREAM_INPUTQ_FULL;
15✔
293
    }
294

295
    int32_t msgLen = px->submit.msgLen;
332,627✔
296
    int64_t ver = px->submit.ver;
332,627✔
297

298
    int32_t code = taosWriteQitem(pQueue, pItem);
332,627✔
299
    if (code != TSDB_CODE_SUCCESS) {
332,621!
300
      streamDataSubmitDestroy(px);
×
301
      return code;
×
302
    }
303

304
    double size = SIZE_IN_MiB(taosQueueMemorySize(pQueue));
332,621✔
305

306
    // use the local variable to avoid the pItem be freed by other threads, since it has been put into queue already.
307
    stDebug("s-task:%s submit enqueue msgLen:%d ver:%" PRId64 ", total in queue:%d, size:%.2fMiB", pTask->id.idStr,
332,617✔
308
            msgLen, ver, total, size + SIZE_IN_MiB(msgLen));
309
  } else if (type == STREAM_INPUT__DATA_BLOCK || type == STREAM_INPUT__REF_DATA_BLOCK) {
102,883✔
310
    if (streamQueueIsFull(pTask->inputq.queue)) {
37,234!
311
      double size = SIZE_IN_MiB(taosQueueMemorySize(pQueue));
×
312

313
      stTrace("s-task:%s input queue is full, capacity:%d size:%d MiB, current(blocks:%d, size:%.2fMiB) abort",
×
314
              pTask->id.idStr, STREAM_TASK_QUEUE_CAPACITY, STREAM_TASK_QUEUE_CAPACITY_IN_SIZE, total, size);
315
      streamFreeQitem(pItem);
×
316
      return TSDB_CODE_STREAM_INPUTQ_FULL;
×
317
    }
318

319
    int32_t code = taosWriteQitem(pQueue, pItem);
37,227✔
320
    if (code != TSDB_CODE_SUCCESS) {
37,225!
321
      streamFreeQitem(pItem);
×
322
      return code;
×
323
    }
324

325
    double size = SIZE_IN_MiB(taosQueueMemorySize(pQueue));
37,225✔
326
    stDebug("s-task:%s blockdata enqueue, total in queue:%d, size:%.2fMiB", pTask->id.idStr, total, size);
37,227✔
327
  } else if (type == STREAM_INPUT__CHECKPOINT || type == STREAM_INPUT__CHECKPOINT_TRIGGER ||
28,422✔
328
             type == STREAM_INPUT__TRANS_STATE || type == STREAM_INPUT__DATA_RETRIEVE) {
28,987✔
329
    int32_t code = taosWriteQitem(pQueue, pItem);
23,937✔
330
    if (code != TSDB_CODE_SUCCESS) {
23,939!
331
      streamFreeQitem(pItem);
×
332
      return code;
×
333
    }
334

335
    double size = SIZE_IN_MiB(taosQueueMemorySize(pQueue));
23,939✔
336
    stDebug("s-task:%s level:%d %s blockdata enqueue, total in queue:%d, size:%.2fMiB", pTask->id.idStr,
23,951✔
337
            pTask->info.taskLevel, streamQueueItemGetTypeStr(type), total, size);
338
  } else if (type == STREAM_INPUT__GET_RES) {
4,485!
339
    // use the default memory limit, refactor later.
340
    int32_t code = taosWriteQitem(pQueue, pItem);
4,485✔
341
    if (code != TSDB_CODE_SUCCESS) {
4,485!
342
      streamFreeQitem(pItem);
×
343
      return code;
×
344
    }
345

346
    double size = SIZE_IN_MiB(taosQueueMemorySize(pQueue));
4,485✔
347
    stDebug("s-task:%s data res enqueue, current(blocks:%d, size:%.2fMiB)", pTask->id.idStr, total, size);
4,485✔
348
  } else {
349
    stError("s-task:%s invalid type:%d to put in inputQ", pTask->id.idStr, type);
×
350
    return TSDB_CODE_INVALID_PARA;
×
351
  }
352

353
  if (type != STREAM_INPUT__GET_RES && type != STREAM_INPUT__CHECKPOINT && type != STREAM_INPUT__CHECKPOINT_TRIGGER &&
398,274✔
354
      (pTask->info.delaySchedParam != 0)) {
379,493✔
355
    (void)atomic_val_compare_exchange_8(&pTask->schedInfo.status, TASK_TRIGGER_STATUS__INACTIVE,
11,453✔
356
                                        TASK_TRIGGER_STATUS__MAY_ACTIVE);
357
    stDebug("s-task:%s new data arrived, active the sched-trigger, triggerStatus:%d", pTask->id.idStr,
11,453✔
358
            pTask->schedInfo.status);
359
  }
360

361
  return 0;
398,267✔
362
}
363

364
int32_t streamTaskPutTranstateIntoInputQ(SStreamTask* pTask) {
2,242✔
365
  int32_t           code = 0;
2,242✔
366
  SStreamDataBlock* pTranstate = NULL;
2,242✔
367
  SSDataBlock*      pBlock = NULL;
2,242✔
368

369
  code = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, sizeof(SSDataBlock), (void**)&pTranstate);
2,242✔
370
  if (code) {
2,242!
371
    return code;
×
372
  }
373

374
  pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
2,242!
375
  if (pBlock == NULL) {
2,242!
376
    code = terrno;
×
377
    goto _err;
×
378
  }
379

380
  pTranstate->type = STREAM_INPUT__TRANS_STATE;
2,242✔
381

382
  pBlock->info.type = STREAM_TRANS_STATE;
2,242✔
383
  pBlock->info.rows = 1;
2,242✔
384
  pBlock->info.childId = pTask->info.selfChildId;
2,242✔
385

386
  pTranstate->blocks = taosArrayInit(4, sizeof(SSDataBlock));  // pBlock;
2,242✔
387
  if (pTranstate->blocks == NULL) {
2,242!
388
    code = terrno;
×
389
    goto _err;
×
390
  }
391

392
  void* p = taosArrayPush(pTranstate->blocks, pBlock);
2,242✔
393
  if (p == NULL) {
2,242!
394
    code = terrno;
×
395
    goto _err;
×
396
  }
397

398
  taosMemoryFree(pBlock);
2,242!
399
  if (streamTaskPutDataIntoInputQ(pTask, (SStreamQueueItem*)pTranstate) < 0) {
2,242!
400
    code = TSDB_CODE_OUT_OF_MEMORY;
×
401
    goto _err;
×
402
  }
403

404
  pTask->status.appendTranstateBlock = true;
2,242✔
405
  return TSDB_CODE_SUCCESS;
2,242✔
406

407
_err:
×
408
  taosMemoryFree(pBlock);
×
409
  taosFreeQitem(pTranstate);
×
410
  return code;
×
411
}
412

413
// the result should be put into the outputQ in any cases, the result may be lost otherwise.
414
int32_t streamTaskPutDataIntoOutputQ(SStreamTask* pTask, SStreamDataBlock* pBlock) {
20,141✔
415
  STaosQueue* pQueue = pTask->outputq.queue->pQueue;
20,141✔
416
  int32_t     code = taosWriteQitem(pQueue, pBlock);
20,141✔
417

418
  int32_t total = streamQueueGetNumOfItems(pTask->outputq.queue);
20,141✔
419
  double  size = SIZE_IN_MiB(taosQueueMemorySize(pQueue));
20,142✔
420
  if (code != 0) {
20,141!
421
    stError("s-task:%s failed to put res into outputQ, outputQ items:%d, size:%.2fMiB code:%s, result lost",
×
422
            pTask->id.idStr, total + 1, size, tstrerror(code));
423
  } else {
424
    if (streamQueueIsFull(pTask->outputq.queue)) {
20,141!
425
      stWarn(
×
426
          "s-task:%s outputQ is full(outputQ items:%d, size:%.2fMiB), set the output status BLOCKING, wait for 500ms "
427
          "after handle this batch of blocks",
428
          pTask->id.idStr, total, size);
429
    } else {
430
      stDebug("s-task:%s data put into outputQ, outputQ items:%d, size:%.2fMiB", pTask->id.idStr, total, size);
20,142✔
431
    }
432
  }
433

434
  return code;
20,140✔
435
}
436

437
int32_t streamTaskInitTokenBucket(STokenBucket* pBucket, int32_t numCap, int32_t numRate, float quotaRate,
14,931✔
438
                                  const char* id) {
439
  if (numCap < 10 || numRate < 10 || pBucket == NULL) {
14,931!
440
    stError("failed to init sink task bucket, cap:%d, rate:%d", numCap, numRate);
×
441
    return TSDB_CODE_INVALID_PARA;
×
442
  }
443

444
  pBucket->numCapacity = numCap;
14,944✔
445
  pBucket->numOfToken = numCap;
14,944✔
446
  pBucket->numRate = numRate;
14,944✔
447

448
  pBucket->quotaRate = quotaRate;
14,944✔
449
  pBucket->quotaCapacity = quotaRate * MAX_SMOOTH_BURST_RATIO;
14,944✔
450
  pBucket->quotaRemain = pBucket->quotaCapacity;
14,944✔
451

452
  pBucket->tokenFillTimestamp = taosGetTimestampMs();
14,941✔
453
  pBucket->quotaFillTimestamp = taosGetTimestampMs();
14,944✔
454
  stDebug("s-task:%s sink quotaRate:%.2fMiB, numRate:%d", id, quotaRate, numRate);
14,944✔
455
  return TSDB_CODE_SUCCESS;
14,943✔
456
}
457

458
static void fillTokenBucket(STokenBucket* pBucket, const char* id) {
51,811✔
459
  int64_t now = taosGetTimestampMs();
51,814✔
460

461
  int64_t deltaToken = now - pBucket->tokenFillTimestamp;
51,814✔
462
  if (pBucket->numOfToken < 0) {
51,814!
463
    return;
×
464
  }
465

466
  int32_t incNum = (deltaToken / 1000.0) * pBucket->numRate;
51,814✔
467
  if (incNum > 0) {
51,814✔
468
    pBucket->numOfToken = TMIN(pBucket->numOfToken + incNum, pBucket->numCapacity);
27,063✔
469
    pBucket->tokenFillTimestamp = now;
27,063✔
470
  }
471

472
  // increase the new available quota as time goes on
473
  int64_t deltaQuota = now - pBucket->quotaFillTimestamp;
51,814✔
474
  double  incSize = (deltaQuota / 1000.0) * pBucket->quotaRate;
51,814✔
475
  if (incSize > 0) {
51,814✔
476
    pBucket->quotaRemain = TMIN(pBucket->quotaRemain + incSize, pBucket->quotaCapacity);
33,716✔
477
    pBucket->quotaFillTimestamp = now;
33,716✔
478
  }
479

480
  if (incNum > 0 || incSize > 0) {
51,814✔
481
    stTrace("token/quota available, token:%d inc:%d, token_TsDelta:%" PRId64
33,696✔
482
            ", quota:%.2fMiB inc:%.3fMiB quotaTs:%" PRId64 " now:%" PRId64 "ms, %s",
483
            pBucket->numOfToken, incNum, deltaToken, pBucket->quotaRemain, incSize, deltaQuota, now, id);
484
  }
485
}
486

487
bool streamTaskExtractAvailableToken(STokenBucket* pBucket, const char* id) {
51,817✔
488
  fillTokenBucket(pBucket, id);
51,817✔
489

490
  if (pBucket->numOfToken > 0) {
51,817!
491
    if (pBucket->quotaRemain > 0) {
51,819!
492
      pBucket->numOfToken -= 1;
51,819✔
493
      return true;
51,819✔
494
    } else {  // no available size quota now
495
      return false;
×
496
    }
497
  } else {
498
    return false;
×
499
  }
500
}
501

502
void streamTaskPutbackToken(STokenBucket* pBucket) {
89,808✔
503
  pBucket->numOfToken = TMIN(pBucket->numOfToken + 1, pBucket->numCapacity);
89,808✔
504
}
89,808✔
505

506
// size in KB
507
void streamTaskConsumeQuota(STokenBucket* pBucket, int32_t bytes) { pBucket->quotaRemain -= SIZE_IN_MiB(bytes); }
14,509✔
508

509
void streamTaskInputFail(SStreamTask* pTask) { atomic_store_8(&pTask->inputq.status, TASK_INPUT_STATUS__FAILED); }
×
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