tqueue.c 12.9 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 {
L
Liu Jicong 已提交
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 {
L
Liu Jicong 已提交
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;
L
Liu Jicong 已提交
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);
118
  if (queue->head == NULL && queue->tail == NULL && queue->numOfItems == 0 && queue->memOfItems == 0) {
S
Shengliang Guan 已提交
119 120
    empty = true;
  }
wafwerar's avatar
wafwerar 已提交
121
  taosThreadMutexUnlock(&queue->mutex);
S
Shengliang Guan 已提交
122 123 124 125

  return empty;
}

126 127 128 129 130 131 132 133
void taosUpdateItemSize(STaosQueue *queue, int32_t items) {
  if (queue == NULL) return;

  taosThreadMutexLock(&queue->mutex);
  queue->numOfItems -= items;
  taosThreadMutexUnlock(&queue->mutex);
}

134
int32_t taosQueueItemSize(STaosQueue *queue) {
135 136
  if (queue == NULL) return 0;

wafwerar's avatar
wafwerar 已提交
137
  taosThreadMutexLock(&queue->mutex);
138
  int32_t numOfItems = queue->numOfItems;
wafwerar's avatar
wafwerar 已提交
139
  taosThreadMutexUnlock(&queue->mutex);
140 141 142
  return numOfItems;
}

143
int64_t taosQueueMemorySize(STaosQueue *queue) {
144 145
  if (queue == NULL) return 0;

146
  taosThreadMutexLock(&queue->mutex);
147
  int64_t memOfItems = queue->memOfItems;
148 149 150 151
  taosThreadMutexUnlock(&queue->mutex);
  return memOfItems;
}

152
void *taosAllocateQitem(int32_t size, EQItype itype) {
wafwerar's avatar
wafwerar 已提交
153
  STaosQnode *pNode = taosMemoryCalloc(1, sizeof(STaosQnode) + size);
154
  pNode->size = size;
155
  pNode->itype = itype;
D
dapan1121 已提交
156
  pNode->timestamp = taosGetTimestampUs();
S
Shengliang Guan 已提交
157 158 159 160 161

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

163 164
  if (itype == RPC_QITEM) {
    int64_t alloced = atomic_add_fetch_64(&tsRpcQueueMemoryUsed, size);
dengyihao's avatar
dengyihao 已提交
165
    if (alloced > tsRpcQueueMemoryAllowed) {
166 167 168 169 170 171 172 173 174
      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 已提交
175
  return pNode->item;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
176
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
177

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

181 182 183 184 185 186 187 188 189
  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) 已提交
190
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
191

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

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

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
198 199 200 201 202
  if (queue->tail) {
    queue->tail->next = pNode;
    queue->tail = pNode;
  } else {
    queue->head = pNode;
S
Shengliang Guan 已提交
203
    queue->tail = pNode;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
204 205 206
  }

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

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

213
  if (queue->qset) tsem_post(&queue->qset->sem);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
214 215
}

216
int32_t taosReadQitem(STaosQueue *queue, void **ppItem) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
217
  STaosQnode *pNode = NULL;
218
  int32_t     code = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
219

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

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
222
  if (queue->head) {
S
Shengliang Guan 已提交
223
    pNode = queue->head;
224
    *ppItem = pNode->item;
S
Shengliang Guan 已提交
225 226 227
    queue->head = pNode->next;
    if (queue->head == NULL) queue->tail = NULL;
    queue->numOfItems--;
228
    queue->memOfItems -= pNode->size;
S
Shengliang Guan 已提交
229 230
    if (queue->qset) atomic_sub_fetch_32(&queue->qset->numOfItems, 1);
    code = 1;
231 232
    uTrace("item:%p is read out from queue:%p, items:%d mem:%" PRId64, *ppItem, queue, queue->numOfItems,
           queue->memOfItems);
S
Shengliang Guan 已提交
233
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
234

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

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
237 238
  return code;
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
239

240 241 242 243 244 245 246
STaosQall *taosAllocateQall() {
  STaosQall *qall = taosMemoryCalloc(1, sizeof(STaosQall));
  if (qall != NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
  }
  return qall;
}
247

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

250 251 252
int32_t taosReadAllQitems(STaosQueue *queue, STaosQall *qall) {
  int32_t code = 0;
  bool    empty;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
253

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

J
Jun Li 已提交
256 257
  empty = queue->head == NULL;
  if (!empty) {
258 259 260 261 262 263 264 265 266
    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;
267
    queue->memOfItems = 0;
268
    uTrace("read %d items from queue:%p, items:%d mem:%" PRId64, code, queue, queue->numOfItems, queue->memOfItems);
269
    if (queue->qset) atomic_sub_fetch_32(&queue->qset->numOfItems, qall->numOfItems);
J
Jun Li 已提交
270
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
271

wafwerar's avatar
wafwerar 已提交
272
  taosThreadMutexUnlock(&queue->mutex);
J
Jun Li 已提交
273 274 275 276 277 278 279 280

  // 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) 已提交
281
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
282

283
int32_t taosGetQitem(STaosQall *qall, void **ppItem) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
284
  STaosQnode *pNode;
285
  int32_t     num = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
286

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

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
290
  if (pNode) {
291
    *ppItem = pNode->item;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
292
    num = 1;
293
    uTrace("item:%p is fetched", *ppItem);
L
Liu Jicong 已提交
294 295
  } else {
    *ppItem = NULL;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
296 297
  }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
298
  return num;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
299 300
}

C
Cary Xu 已提交
301 302
void    taosResetQitems(STaosQall *qall) { qall->current = qall->start; }
int32_t taosQallItemSize(STaosQall *qall) { return qall->numOfItems; }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
303

304
STaosQset *taosOpenQset() {
wafwerar's avatar
wafwerar 已提交
305
  STaosQset *qset = taosMemoryCalloc(sizeof(STaosQset), 1);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
306
  if (qset == NULL) {
S
Shengliang Guan 已提交
307
    terrno = TSDB_CODE_OUT_OF_MEMORY;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
308
    return NULL;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
309 310
  }

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

S
Shengliang Guan 已提交
314
  uDebug("qset:%p is opened", qset);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
315 316
  return qset;
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
317

318 319
void taosCloseQset(STaosQset *qset) {
  if (qset == NULL) return;
320 321

  // remove all the queues from qset
wafwerar's avatar
wafwerar 已提交
322
  taosThreadMutexLock(&qset->mutex);
323 324 325 326 327 328 329
  while (qset->head) {
    STaosQueue *queue = qset->head;
    qset->head = qset->head->next;

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

wafwerar's avatar
wafwerar 已提交
332
  taosThreadMutexDestroy(&qset->mutex);
333
  tsem_destroy(&qset->sem);
wafwerar's avatar
wafwerar 已提交
334
  taosMemoryFree(qset);
S
Shengliang Guan 已提交
335
  uDebug("qset:%p is closed", qset);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
336 337
}

338 339 340
// 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.
341
void taosQsetThreadResume(STaosQset *qset) {
S
TD-1670  
Shengliang Guan 已提交
342
  uDebug("qset:%p, it will exit", qset);
343 344 345
  tsem_post(&qset->sem);
}

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

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

  queue->next = qset->head;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
352
  queue->ahandle = ahandle;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
353 354 355
  qset->head = queue;
  qset->numOfQueues++;

wafwerar's avatar
wafwerar 已提交
356
  taosThreadMutexLock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
357 358
  atomic_add_fetch_32(&qset->numOfItems, queue->numOfItems);
  queue->qset = qset;
wafwerar's avatar
wafwerar 已提交
359
  taosThreadMutexUnlock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
360

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

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
363
  uTrace("queue:%p is added into qset:%p", queue, qset);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
364 365 366
  return 0;
}

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

wafwerar's avatar
wafwerar 已提交
370
  taosThreadMutexLock(&qset->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
371 372 373 374

  if (qset->head) {
    if (qset->head == queue) {
      qset->head = qset->head->next;
375
      tqueue = queue;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
376 377 378 379
    } else {
      STaosQueue *prev = qset->head;
      tqueue = qset->head->next;
      while (tqueue) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
380
        assert(tqueue->qset);
S
Shengliang Guan 已提交
381
        if (tqueue == queue) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
382
          prev->next = tqueue->next;
383
          break;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
384 385 386 387 388 389
        } else {
          prev = tqueue;
          tqueue = tqueue->next;
        }
      }
    }
390 391 392 393 394

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

wafwerar's avatar
wafwerar 已提交
395
      taosThreadMutexLock(&queue->mutex);
396 397
      atomic_sub_fetch_32(&qset->numOfItems, queue->numOfItems);
      queue->qset = NULL;
398
      queue->next = NULL;
wafwerar's avatar
wafwerar 已提交
399
      taosThreadMutexUnlock(&queue->mutex);
400
    }
S
Shengliang Guan 已提交
401 402
  }

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

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

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

410
int32_t taosReadQitemFromQset(STaosQset *qset, void **ppItem, SQueueInfo *qinfo) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
411
  STaosQnode *pNode = NULL;
412
  int32_t     code = 0;
S
Shengliang Guan 已提交
413

414 415
  tsem_wait(&qset->sem);

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

418
  for (int32_t i = 0; i < qset->numOfQueues; ++i) {
S
Shengliang Guan 已提交
419
    if (qset->current == NULL) qset->current = qset->head;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
420
    STaosQueue *queue = qset->current;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
421 422
    if (queue) qset->current = queue->next;
    if (queue == NULL) break;
J
jtao1735 已提交
423
    if (queue->head == NULL) continue;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
424

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

    if (queue->head) {
S
Shengliang Guan 已提交
428
      pNode = queue->head;
429
      *ppItem = pNode->item;
430 431 432 433
      qinfo->ahandle = queue->ahandle;
      qinfo->fp = queue->itemFp;
      qinfo->queue = queue;
      qinfo->timestamp = pNode->timestamp;
S
Shengliang Guan 已提交
434

S
Shengliang Guan 已提交
435 436
      queue->head = pNode->next;
      if (queue->head == NULL) queue->tail = NULL;
437
      // queue->numOfItems--;
438
      queue->memOfItems -= pNode->size;
S
Shengliang Guan 已提交
439 440
      atomic_sub_fetch_32(&qset->numOfItems, 1);
      code = 1;
441
      uTrace("item:%p is read out from queue:%p, items:%d mem:%" PRId64, *ppItem, queue, queue->numOfItems - 1,
442
             queue->memOfItems);
S
Shengliang Guan 已提交
443
    }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
444

wafwerar's avatar
wafwerar 已提交
445
    taosThreadMutexUnlock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
446 447 448
    if (pNode) break;
  }

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

S
Shengliang Guan 已提交
451
  return code;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
452 453
}

454
int32_t taosReadAllQitemsFromQset(STaosQset *qset, STaosQall *qall, SQueueInfo *qinfo) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
455
  STaosQueue *queue;
456
  int32_t     code = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
457

458
  tsem_wait(&qset->sem);
wafwerar's avatar
wafwerar 已提交
459
  taosThreadMutexLock(&qset->mutex);
J
Jeff Tao 已提交
460

461 462
  for (int32_t i = 0; i < qset->numOfQueues; ++i) {
    if (qset->current == NULL) qset->current = qset->head;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
463
    queue = qset->current;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
464 465
    if (queue) qset->current = queue->next;
    if (queue == NULL) break;
J
jtao1735 已提交
466
    if (queue->head == NULL) continue;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
467

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

    if (queue->head) {
471 472 473 474
      qall->current = queue->head;
      qall->start = queue->head;
      qall->numOfItems = queue->numOfItems;
      code = qall->numOfItems;
475 476 477
      qinfo->ahandle = queue->ahandle;
      qinfo->fp = queue->itemsFp;
      qinfo->queue = queue;
S
Shengliang Guan 已提交
478

479 480
      queue->head = NULL;
      queue->tail = NULL;
481
      // queue->numOfItems = 0;
482
      queue->memOfItems = 0;
483
      uTrace("read %d items from queue:%p, items:0 mem:%" PRId64, code, queue, queue->memOfItems);
484

485
      atomic_sub_fetch_32(&qset->numOfItems, qall->numOfItems);
S
Shengliang Guan 已提交
486 487 488
      for (int32_t j = 1; j < qall->numOfItems; ++j) {
        tsem_wait(&qset->sem);
      }
489
    }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
490

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

493
    if (code != 0) break;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
494
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
495

wafwerar's avatar
wafwerar 已提交
496
  taosThreadMutexUnlock(&qset->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
497 498
  return code;
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
499

S
Shengliang Guan 已提交
500 501 502 503
void taosResetQsetThread(STaosQset *qset, void *pItem) {
  if (pItem == NULL) return;
  STaosQnode *pNode = (STaosQnode *)((char *)pItem - sizeof(STaosQnode));

wafwerar's avatar
wafwerar 已提交
504
  taosThreadMutexLock(&qset->mutex);
S
Shengliang Guan 已提交
505 506 507
  for (int32_t i = 0; i < pNode->queue->numOfItems; ++i) {
    tsem_post(&qset->sem);
  }
wafwerar's avatar
wafwerar 已提交
508
  taosThreadMutexUnlock(&qset->mutex);
S
Shengliang Guan 已提交
509
}