tqueue.c 12.8 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
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

413 414
  tsem_wait(&qset->sem);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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