tqueue.c 12.3 KB
Newer Older
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
17
#include "tqueue.h"
S
Shengliang Guan 已提交
18
#include "taoserror.h"
S
log  
Shengliang Guan 已提交
19
#include "tlog.h"
20

21 22 23
int64_t tsRpcQueueMemoryAllowed = 0;
int64_t tsRpcQueueMemoryUsed = 0;

24
typedef struct STaosQnode STaosQnode;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
25

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
26
typedef struct STaosQnode {
27
  STaosQnode *next;
S
Shengliang Guan 已提交
28
  STaosQueue *queue;
D
dapan1121 已提交
29
  int64_t     timestamp;
30
  int32_t     size;
31 32
  int8_t      itype;
  int8_t      reserved[3];
33
  char        item[];
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
34 35
} STaosQnode;

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
36
typedef struct STaosQueue {
dengyihao's avatar
dengyihao 已提交
37 38 39 40 41
  STaosQnode *  head;
  STaosQnode *  tail;
  STaosQueue *  next;     // for queue set
  STaosQset *   qset;     // for queue set
  void *        ahandle;  // for queue set
42 43
  FItem         itemFp;
  FItems        itemsFp;
wafwerar's avatar
wafwerar 已提交
44
  TdThreadMutex mutex;
45 46
  int64_t       memOfItems;
  int32_t       numOfItems;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
47 48
} STaosQueue;

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
49
typedef struct STaosQset {
dengyihao's avatar
dengyihao 已提交
50 51
  STaosQueue *  head;
  STaosQueue *  current;
wafwerar's avatar
wafwerar 已提交
52
  TdThreadMutex mutex;
53
  tsem_t        sem;
54 55
  int32_t       numOfQueues;
  int32_t       numOfItems;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
56 57
} STaosQset;

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
58
typedef struct STaosQall {
S
Shengliang Guan 已提交
59 60 61 62 63
  STaosQnode *current;
  STaosQnode *start;
  int32_t     numOfItems;
} STaosQall;

64
STaosQueue *taosOpenQueue() {
wafwerar's avatar
wafwerar 已提交
65
  STaosQueue *queue = taosMemoryCalloc(1, sizeof(STaosQueue));
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
66
  if (queue == NULL) {
S
Shengliang Guan 已提交
67
    terrno = TSDB_CODE_OUT_OF_MEMORY;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
68
    return NULL;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
69 70
  }

wafwerar's avatar
wafwerar 已提交
71
  if (taosThreadMutexInit(&queue->mutex, NULL) != 0) {
S
Shengliang Guan 已提交
72 73 74
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
75

S
Shengliang Guan 已提交
76
  uDebug("queue:%p is opened", queue);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
77 78
  return queue;
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
79

S
Shengliang Guan 已提交
80
void taosSetQueueFp(STaosQueue *queue, FItem itemFp, FItems itemsFp) {
81
  if (queue == NULL) return;
S
Shengliang Guan 已提交
82 83
  queue->itemFp = itemFp;
  queue->itemsFp = itemsFp;
S
Shengliang Guan 已提交
84 85
}

86 87
void taosCloseQueue(STaosQueue *queue) {
  if (queue == NULL) return;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
88
  STaosQnode *pTemp;
dengyihao's avatar
dengyihao 已提交
89
  STaosQset * qset;
90

wafwerar's avatar
wafwerar 已提交
91
  taosThreadMutexLock(&queue->mutex);
S
Shengliang Guan 已提交
92
  STaosQnode *pNode = queue->head;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
93
  queue->head = NULL;
94
  qset = queue->qset;
wafwerar's avatar
wafwerar 已提交
95
  taosThreadMutexUnlock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
96

S
Shengliang Guan 已提交
97 98 99
  if (queue->qset) {
    taosRemoveFromQset(qset, queue);
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
100

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
101 102 103
  while (pNode) {
    pTemp = pNode;
    pNode = pNode->next;
wafwerar's avatar
wafwerar 已提交
104
    taosMemoryFree(pTemp);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
105 106
  }

wafwerar's avatar
wafwerar 已提交
107
  taosThreadMutexDestroy(&queue->mutex);
wafwerar's avatar
wafwerar 已提交
108
  taosMemoryFree(queue);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
109

S
Shengliang Guan 已提交
110
  uDebug("queue:%p is closed", queue);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
111
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
112

113 114
bool taosQueueEmpty(STaosQueue *queue) {
  if (queue == NULL) return true;
S
Shengliang Guan 已提交
115 116

  bool empty = false;
wafwerar's avatar
wafwerar 已提交
117
  taosThreadMutexLock(&queue->mutex);
S
Shengliang Guan 已提交
118 119 120
  if (queue->head == NULL && queue->tail == NULL) {
    empty = true;
  }
wafwerar's avatar
wafwerar 已提交
121
  taosThreadMutexUnlock(&queue->mutex);
S
Shengliang Guan 已提交
122 123 124 125

  return empty;
}

126
int32_t taosQueueItemSize(STaosQueue *queue) {
127 128
  if (queue == NULL) return 0;

wafwerar's avatar
wafwerar 已提交
129
  taosThreadMutexLock(&queue->mutex);
130
  int32_t numOfItems = queue->numOfItems;
wafwerar's avatar
wafwerar 已提交
131
  taosThreadMutexUnlock(&queue->mutex);
132 133 134
  return numOfItems;
}

135
int64_t taosQueueMemorySize(STaosQueue *queue) {
136 137
  if (queue == NULL) return 0;

138
  taosThreadMutexLock(&queue->mutex);
139
  int64_t memOfItems = queue->memOfItems;
140 141 142 143
  taosThreadMutexUnlock(&queue->mutex);
  return memOfItems;
}

144
void *taosAllocateQitem(int32_t size, EQItype itype) {
wafwerar's avatar
wafwerar 已提交
145
  STaosQnode *pNode = taosMemoryCalloc(1, sizeof(STaosQnode) + size);
146
  pNode->size = size;
147
  pNode->itype = itype;
D
dapan1121 已提交
148
  pNode->timestamp = taosGetTimestampUs();
S
Shengliang Guan 已提交
149 150 151 152 153

  if (pNode == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
S
Shengliang Guan 已提交
154

155 156
  if (itype == RPC_QITEM) {
    int64_t alloced = atomic_add_fetch_64(&tsRpcQueueMemoryUsed, size);
dengyihao's avatar
dengyihao 已提交
157
    if (alloced > tsRpcQueueMemoryAllowed) {
158 159 160 161 162 163 164 165 166
      taosMemoryFree(pNode);
      terrno = TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE;
      return NULL;
    }
    uTrace("item:%p, node:%p is allocated, alloc:%" PRId64, pNode->item, pNode, alloced);
  } else {
    uTrace("item:%p, node:%p is allocated", pNode->item, pNode);
  }

S
Shengliang Guan 已提交
167
  return pNode->item;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
168
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
169

S
Shengliang Guan 已提交
170 171
void taosFreeQitem(void *pItem) {
  if (pItem == NULL) return;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
172

173 174 175 176 177 178 179 180 181
  STaosQnode *pNode = (STaosQnode *)((char *)pItem - sizeof(STaosQnode));
  if (pNode->itype > 0) {
    int64_t alloced = atomic_sub_fetch_64(&tsRpcQueueMemoryUsed, pNode->size);
    uTrace("item:%p, node:%p is freed, alloc:%" PRId64, pItem, pNode, alloced);
  } else {
    uTrace("item:%p, node:%p is freed", pItem, pNode);
  }

  taosMemoryFree(pNode);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
182
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
183

S
Shengliang Guan 已提交
184
void taosWriteQitem(STaosQueue *queue, void *pItem) {
185
  STaosQnode *pNode = (STaosQnode *)(((char *)pItem) - sizeof(STaosQnode));
186
  pNode->next = NULL;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
187

wafwerar's avatar
wafwerar 已提交
188
  taosThreadMutexLock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
189

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
190 191 192 193 194
  if (queue->tail) {
    queue->tail->next = pNode;
    queue->tail = pNode;
  } else {
    queue->head = pNode;
S
Shengliang Guan 已提交
195
    queue->tail = pNode;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
196 197 198
  }

  queue->numOfItems++;
199
  queue->memOfItems += pNode->size;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
200
  if (queue->qset) atomic_add_fetch_32(&queue->qset->numOfItems, 1);
201
  uTrace("item:%p is put into queue:%p, items:%d mem:%" PRId64, pItem, queue, queue->numOfItems, queue->memOfItems);
202

wafwerar's avatar
wafwerar 已提交
203
  taosThreadMutexUnlock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
204

205
  if (queue->qset) tsem_post(&queue->qset->sem);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
206 207
}

208
int32_t taosReadQitem(STaosQueue *queue, void **ppItem) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
209
  STaosQnode *pNode = NULL;
210
  int32_t     code = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
211

wafwerar's avatar
wafwerar 已提交
212
  taosThreadMutexLock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
213

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
214
  if (queue->head) {
S
Shengliang Guan 已提交
215
    pNode = queue->head;
216
    *ppItem = pNode->item;
S
Shengliang Guan 已提交
217 218 219
    queue->head = pNode->next;
    if (queue->head == NULL) queue->tail = NULL;
    queue->numOfItems--;
220
    queue->memOfItems -= pNode->size;
S
Shengliang Guan 已提交
221 222
    if (queue->qset) atomic_sub_fetch_32(&queue->qset->numOfItems, 1);
    code = 1;
223 224
    uTrace("item:%p is read out from queue:%p, items:%d mem:%" PRId64, *ppItem, queue, queue->numOfItems,
           queue->memOfItems);
S
Shengliang Guan 已提交
225
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
226

wafwerar's avatar
wafwerar 已提交
227
  taosThreadMutexUnlock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
228

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
229 230
  return code;
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
231

232 233 234 235 236 237 238
STaosQall *taosAllocateQall() {
  STaosQall *qall = taosMemoryCalloc(1, sizeof(STaosQall));
  if (qall != NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
  }
  return qall;
}
239

wafwerar's avatar
wafwerar 已提交
240
void taosFreeQall(STaosQall *qall) { taosMemoryFree(qall); }
241

242 243 244
int32_t taosReadAllQitems(STaosQueue *queue, STaosQall *qall) {
  int32_t code = 0;
  bool    empty;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
245

wafwerar's avatar
wafwerar 已提交
246
  taosThreadMutexLock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
247

J
Jun Li 已提交
248 249
  empty = queue->head == NULL;
  if (!empty) {
250 251 252 253 254 255 256 257 258
    memset(qall, 0, sizeof(STaosQall));
    qall->current = queue->head;
    qall->start = queue->head;
    qall->numOfItems = queue->numOfItems;
    code = qall->numOfItems;

    queue->head = NULL;
    queue->tail = NULL;
    queue->numOfItems = 0;
259
    queue->memOfItems = 0;
260
    if (queue->qset) atomic_sub_fetch_32(&queue->qset->numOfItems, qall->numOfItems);
J
Jun Li 已提交
261
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
262

wafwerar's avatar
wafwerar 已提交
263
  taosThreadMutexUnlock(&queue->mutex);
J
Jun Li 已提交
264 265 266 267 268 269 270 271

  // if source queue is empty, we set destination qall to empty too.
  if (empty) {
    qall->current = NULL;
    qall->start = NULL;
    qall->numOfItems = 0;
  }
  return code;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
272
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
273

274
int32_t taosGetQitem(STaosQall *qall, void **ppItem) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
275
  STaosQnode *pNode;
276
  int32_t     num = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
277

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
278
  pNode = qall->current;
S
Shengliang Guan 已提交
279 280
  if (pNode) qall->current = pNode->next;

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
281
  if (pNode) {
282
    *ppItem = pNode->item;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
283
    num = 1;
284
    uTrace("item:%p is fetched", *ppItem);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
285 286
  }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
287
  return num;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
288 289
}

290
void taosResetQitems(STaosQall *qall) { qall->current = qall->start; }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
291

292
STaosQset *taosOpenQset() {
wafwerar's avatar
wafwerar 已提交
293
  STaosQset *qset = taosMemoryCalloc(sizeof(STaosQset), 1);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
294
  if (qset == NULL) {
S
Shengliang Guan 已提交
295
    terrno = TSDB_CODE_OUT_OF_MEMORY;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
296
    return NULL;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
297 298
  }

wafwerar's avatar
wafwerar 已提交
299
  taosThreadMutexInit(&qset->mutex, NULL);
300
  tsem_init(&qset->sem, 0, 0);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
301

S
Shengliang Guan 已提交
302
  uDebug("qset:%p is opened", qset);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
303 304
  return qset;
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
305

306 307
void taosCloseQset(STaosQset *qset) {
  if (qset == NULL) return;
308 309

  // remove all the queues from qset
wafwerar's avatar
wafwerar 已提交
310
  taosThreadMutexLock(&qset->mutex);
311 312 313 314 315 316 317
  while (qset->head) {
    STaosQueue *queue = qset->head;
    qset->head = qset->head->next;

    queue->qset = NULL;
    queue->next = NULL;
  }
wafwerar's avatar
wafwerar 已提交
318
  taosThreadMutexUnlock(&qset->mutex);
319

wafwerar's avatar
wafwerar 已提交
320
  taosThreadMutexDestroy(&qset->mutex);
321
  tsem_destroy(&qset->sem);
wafwerar's avatar
wafwerar 已提交
322
  taosMemoryFree(qset);
S
Shengliang Guan 已提交
323
  uDebug("qset:%p is closed", qset);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
324 325
}

326 327 328
// tsem_post 'qset->sem', so that reader threads waiting for it
// resumes execution and return, should only be used to signal the
// thread to exit.
329
void taosQsetThreadResume(STaosQset *qset) {
S
TD-1670  
Shengliang Guan 已提交
330
  uDebug("qset:%p, it will exit", qset);
331 332 333
  tsem_post(&qset->sem);
}

334
int32_t taosAddIntoQset(STaosQset *qset, STaosQueue *queue, void *ahandle) {
S
Shengliang Guan 已提交
335
  if (queue->qset) return -1;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
336

wafwerar's avatar
wafwerar 已提交
337
  taosThreadMutexLock(&qset->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
338 339

  queue->next = qset->head;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
340
  queue->ahandle = ahandle;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
341 342 343
  qset->head = queue;
  qset->numOfQueues++;

wafwerar's avatar
wafwerar 已提交
344
  taosThreadMutexLock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
345 346
  atomic_add_fetch_32(&qset->numOfItems, queue->numOfItems);
  queue->qset = qset;
wafwerar's avatar
wafwerar 已提交
347
  taosThreadMutexUnlock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
348

wafwerar's avatar
wafwerar 已提交
349
  taosThreadMutexUnlock(&qset->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
350

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
351
  uTrace("queue:%p is added into qset:%p", queue, qset);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
352 353 354
  return 0;
}

355
void taosRemoveFromQset(STaosQset *qset, STaosQueue *queue) {
356
  STaosQueue *tqueue = NULL;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
357

wafwerar's avatar
wafwerar 已提交
358
  taosThreadMutexLock(&qset->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
359 360 361 362

  if (qset->head) {
    if (qset->head == queue) {
      qset->head = qset->head->next;
363
      tqueue = queue;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
364 365 366 367
    } else {
      STaosQueue *prev = qset->head;
      tqueue = qset->head->next;
      while (tqueue) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
368
        assert(tqueue->qset);
S
Shengliang Guan 已提交
369
        if (tqueue == queue) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
370
          prev->next = tqueue->next;
371
          break;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
372 373 374 375 376 377
        } else {
          prev = tqueue;
          tqueue = tqueue->next;
        }
      }
    }
378 379 380 381 382

    if (tqueue) {
      if (qset->current == queue) qset->current = tqueue->next;
      qset->numOfQueues--;

wafwerar's avatar
wafwerar 已提交
383
      taosThreadMutexLock(&queue->mutex);
384 385
      atomic_sub_fetch_32(&qset->numOfItems, queue->numOfItems);
      queue->qset = NULL;
386
      queue->next = NULL;
wafwerar's avatar
wafwerar 已提交
387
      taosThreadMutexUnlock(&queue->mutex);
388
    }
S
Shengliang Guan 已提交
389 390
  }

wafwerar's avatar
wafwerar 已提交
391
  taosThreadMutexUnlock(&qset->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
392

S
Shengliang Guan 已提交
393
  uDebug("queue:%p is removed from qset:%p", queue, qset);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
394 395
}

396
int32_t taosGetQueueNumber(STaosQset *qset) { return qset->numOfQueues; }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
397

D
dapan1121 已提交
398
int32_t taosReadQitemFromQset(STaosQset *qset, void **ppItem, int64_t *ts, void **ahandle, FItem *itemFp) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
399
  STaosQnode *pNode = NULL;
400
  int32_t     code = 0;
S
Shengliang Guan 已提交
401

402 403
  tsem_wait(&qset->sem);

wafwerar's avatar
wafwerar 已提交
404
  taosThreadMutexLock(&qset->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
405

406
  for (int32_t i = 0; i < qset->numOfQueues; ++i) {
S
Shengliang Guan 已提交
407
    if (qset->current == NULL) qset->current = qset->head;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
408
    STaosQueue *queue = qset->current;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
409 410
    if (queue) qset->current = queue->next;
    if (queue == NULL) break;
J
jtao1735 已提交
411
    if (queue->head == NULL) continue;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
412

wafwerar's avatar
wafwerar 已提交
413
    taosThreadMutexLock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
414 415

    if (queue->head) {
S
Shengliang Guan 已提交
416
      pNode = queue->head;
417
      *ppItem = pNode->item;
S
Shengliang Guan 已提交
418
      if (ahandle) *ahandle = queue->ahandle;
S
Shengliang Guan 已提交
419
      if (itemFp) *itemFp = queue->itemFp;
D
dapan1121 已提交
420
      if (ts) *ts = pNode->timestamp;
S
Shengliang Guan 已提交
421

S
Shengliang Guan 已提交
422 423 424
      queue->head = pNode->next;
      if (queue->head == NULL) queue->tail = NULL;
      queue->numOfItems--;
425
      queue->memOfItems -= pNode->size;
S
Shengliang Guan 已提交
426 427
      atomic_sub_fetch_32(&qset->numOfItems, 1);
      code = 1;
428 429
      uTrace("item:%p is read out from queue:%p, items:%d mem:%" PRId64, *ppItem, queue, queue->numOfItems,
             queue->memOfItems);
S
Shengliang Guan 已提交
430
    }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
431

wafwerar's avatar
wafwerar 已提交
432
    taosThreadMutexUnlock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
433 434 435
    if (pNode) break;
  }

wafwerar's avatar
wafwerar 已提交
436
  taosThreadMutexUnlock(&qset->mutex);
J
Jeff Tao 已提交
437

S
Shengliang Guan 已提交
438
  return code;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
439 440
}

S
Shengliang Guan 已提交
441
int32_t taosReadAllQitemsFromQset(STaosQset *qset, STaosQall *qall, void **ahandle, FItems *itemsFp) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
442
  STaosQueue *queue;
443
  int32_t     code = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
444

445
  tsem_wait(&qset->sem);
wafwerar's avatar
wafwerar 已提交
446
  taosThreadMutexLock(&qset->mutex);
J
Jeff Tao 已提交
447

448 449
  for (int32_t i = 0; i < qset->numOfQueues; ++i) {
    if (qset->current == NULL) qset->current = qset->head;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
450
    queue = qset->current;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
451 452
    if (queue) qset->current = queue->next;
    if (queue == NULL) break;
J
jtao1735 已提交
453
    if (queue->head == NULL) continue;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
454

wafwerar's avatar
wafwerar 已提交
455
    taosThreadMutexLock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
456 457

    if (queue->head) {
458 459 460 461
      qall->current = queue->head;
      qall->start = queue->head;
      qall->numOfItems = queue->numOfItems;
      code = qall->numOfItems;
S
Shengliang Guan 已提交
462
      if (ahandle) *ahandle = queue->ahandle;
S
Shengliang Guan 已提交
463
      if (itemsFp) *itemsFp = queue->itemsFp;
S
Shengliang Guan 已提交
464

465 466 467
      queue->head = NULL;
      queue->tail = NULL;
      queue->numOfItems = 0;
468
      queue->memOfItems = 0;
469
      atomic_sub_fetch_32(&qset->numOfItems, qall->numOfItems);
S
Shengliang Guan 已提交
470 471 472
      for (int32_t j = 1; j < qall->numOfItems; ++j) {
        tsem_wait(&qset->sem);
      }
473
    }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
474

wafwerar's avatar
wafwerar 已提交
475
    taosThreadMutexUnlock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
476

477
    if (code != 0) break;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
478
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
479

wafwerar's avatar
wafwerar 已提交
480
  taosThreadMutexUnlock(&qset->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
481 482
  return code;
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
483

S
Shengliang Guan 已提交
484 485 486 487
void taosResetQsetThread(STaosQset *qset, void *pItem) {
  if (pItem == NULL) return;
  STaosQnode *pNode = (STaosQnode *)((char *)pItem - sizeof(STaosQnode));

wafwerar's avatar
wafwerar 已提交
488
  taosThreadMutexLock(&qset->mutex);
S
Shengliang Guan 已提交
489 490 491
  for (int32_t i = 0; i < pNode->queue->numOfItems; ++i) {
    tsem_post(&qset->sem);
  }
wafwerar's avatar
wafwerar 已提交
492
  taosThreadMutexUnlock(&qset->mutex);
S
Shengliang Guan 已提交
493
}