tprocess.c 15.8 KB
Newer Older
S
shm  
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
#include "tprocess.h"
18
#include "taos.h"
S
shm  
Shengliang Guan 已提交
19
#include "taoserror.h"
20
#include "thash.h"
S
shm  
Shengliang Guan 已提交
21
#include "tlog.h"
S
shm  
Shengliang Guan 已提交
22
#include "tqueue.h"
S
shm  
Shengliang Guan 已提交
23 24

#define SHM_DEFAULT_SIZE (20 * 1024 * 1024)
S
shm  
Shengliang Guan 已提交
25
typedef void *(*ProcThreadFp)(void *param);
S
shm  
Shengliang Guan 已提交
26 27

typedef struct SProcQueue {
S
shm  
Shengliang Guan 已提交
28 29 30 31 32 33 34 35 36
  int32_t       head;
  int32_t       tail;
  int32_t       total;
  int32_t       avail;
  int32_t       items;
  char          name[8];
  TdThreadMutex mutex;
  tsem_t        sem;
  char          pBuffer[];
S
shm  
Shengliang Guan 已提交
37 38
} SProcQueue;

S
shm  
Shengliang Guan 已提交
39
typedef struct SProcObj {
S
shm  
Shengliang Guan 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52
  TdThread      thread;
  SProcQueue   *pChildQueue;
  SProcQueue   *pParentQueue;
  ProcConsumeFp childConsumeFp;
  ProcMallocFp  childMallocHeadFp;
  ProcFreeFp    childFreeHeadFp;
  ProcMallocFp  childMallocBodyFp;
  ProcFreeFp    childFreeBodyFp;
  ProcConsumeFp parentConsumeFp;
  ProcMallocFp  parentMallocHeadFp;
  ProcFreeFp    parentFreeHeadFp;
  ProcMallocFp  parentMallocBodyFp;
  ProcFreeFp    parentFreeBodyFp;
53
  void         *parent;
S
shm  
Shengliang Guan 已提交
54
  const char   *name;
55
  SHashObj     *hash;
S
shm  
Shengliang Guan 已提交
56 57 58
  int32_t       pid;
  bool          isChild;
  bool          stopFlag;
S
shm  
Shengliang Guan 已提交
59 60
} SProcObj;

S
shm  
Shengliang Guan 已提交
61 62 63 64 65
static inline int32_t CEIL8(int32_t v) {
  const int32_t c = ceil((float)(v) / 8) * 8;
  return c < 8 ? 8 : c;
}

S
shm  
Shengliang Guan 已提交
66
static int32_t taosProcInitMutex(SProcQueue *pQueue) {
S
Shengliang Guan 已提交
67
  TdThreadMutexAttr mattr = {0};
S
shm  
Shengliang Guan 已提交
68

wafwerar's avatar
wafwerar 已提交
69
  if (taosThreadMutexAttrInit(&mattr) != 0) {
S
shm  
Shengliang Guan 已提交
70 71
    terrno = TAOS_SYSTEM_ERROR(errno);
    uError("failed to init mutex while init attr since %s", terrstr());
S
shm  
Shengliang Guan 已提交
72
    return -1;
S
shm  
Shengliang Guan 已提交
73 74
  }

wafwerar's avatar
wafwerar 已提交
75
  if (taosThreadMutexAttrSetPshared(&mattr, PTHREAD_PROCESS_SHARED) != 0) {
76
    taosThreadMutexAttrDestroy(&mattr);
S
shm  
Shengliang Guan 已提交
77 78
    terrno = TAOS_SYSTEM_ERROR(errno);
    uError("failed to init mutex while set shared since %s", terrstr());
S
shm  
Shengliang Guan 已提交
79
    return -1;
S
shm  
Shengliang Guan 已提交
80 81
  }

S
shm  
Shengliang Guan 已提交
82
  if (taosThreadMutexInit(&pQueue->mutex, &mattr) != 0) {
S
Shengliang Guan 已提交
83
    taosThreadMutexAttrDestroy(&mattr);
S
shm  
Shengliang Guan 已提交
84 85
    terrno = TAOS_SYSTEM_ERROR(errno);
    uError("failed to init mutex since %s", terrstr());
S
shm  
Shengliang Guan 已提交
86
    return -1;
S
shm  
Shengliang Guan 已提交
87 88
  }

wafwerar's avatar
wafwerar 已提交
89
  taosThreadMutexAttrDestroy(&mattr);
S
shm  
Shengliang Guan 已提交
90
  return 0;
S
shm  
Shengliang Guan 已提交
91 92
}

S
shm  
Shengliang Guan 已提交
93 94
static int32_t taosProcInitSem(SProcQueue *pQueue) {
  if (tsem_init(&pQueue->sem, 1, 0) != 0) {
S
shm  
Shengliang Guan 已提交
95
    terrno = TAOS_SYSTEM_ERROR(errno);
S
shm  
Shengliang Guan 已提交
96
    uError("failed to init sem");
S
shm  
Shengliang Guan 已提交
97 98 99
    return -1;
  }

S
shm  
Shengliang Guan 已提交
100
  return 0;
S
shm  
Shengliang Guan 已提交
101 102
}

S
shm  
Shengliang Guan 已提交
103 104 105 106 107
static SProcQueue *taosProcInitQueue(const char *name, bool isChild, char *ptr, int32_t size) {
  int32_t bufSize = size - CEIL8(sizeof(SProcQueue));
  if (bufSize <= 1024) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
S
shm  
Shengliang Guan 已提交
108 109
  }

S
shm  
Shengliang Guan 已提交
110
  SProcQueue *pQueue = (SProcQueue *)(ptr);
S
shm  
Shengliang Guan 已提交
111

S
shm  
Shengliang Guan 已提交
112 113 114 115
  if (!isChild) {
    if (taosProcInitMutex(pQueue) != 0) {
      return NULL;
    }
S
shm  
Shengliang Guan 已提交
116

S
shm  
Shengliang Guan 已提交
117 118 119
    if (taosProcInitSem(pQueue) != 0) {
      return NULL;
    }
S
shm  
Shengliang Guan 已提交
120

S
shm  
Shengliang Guan 已提交
121 122 123 124 125 126
    tstrncpy(pQueue->name, name, sizeof(pQueue->name));
    pQueue->head = 0;
    pQueue->tail = 0;
    pQueue->total = bufSize;
    pQueue->avail = bufSize;
    pQueue->items = 0;
S
shm  
Shengliang Guan 已提交
127 128
  }

S
shm  
Shengliang Guan 已提交
129 130 131 132 133 134 135 136
  return pQueue;
}

#if 0
static void taosProcDestroyMutex(SProcQueue *pQueue) {
  if (pQueue->mutex != NULL) {
    taosThreadMutexDestroy(pQueue->mutex);
    pQueue->mutex = NULL;
S
shm  
Shengliang Guan 已提交
137
  }
S
shm  
Shengliang Guan 已提交
138
}
S
shm  
Shengliang Guan 已提交
139

S
shm  
Shengliang Guan 已提交
140 141 142 143
static void taosProcDestroySem(SProcQueue *pQueue) {
  if (pQueue->sem != NULL) {
    tsem_destroy(pQueue->sem);
    pQueue->sem = NULL;
S
shm  
Shengliang Guan 已提交
144
  }
S
shm  
Shengliang Guan 已提交
145
}
S
shm  
Shengliang Guan 已提交
146
#endif
S
shm  
Shengliang Guan 已提交
147

S
shm  
Shengliang Guan 已提交
148
static void taosProcCleanupQueue(SProcQueue *pQueue) {
S
shm  
Shengliang Guan 已提交
149
#if 0  
S
shm  
Shengliang Guan 已提交
150
  if (pQueue != NULL) {
S
shm  
Shengliang Guan 已提交
151 152
    taosProcDestroyMutex(pQueue);
    taosProcDestroySem(pQueue);
S
shm  
Shengliang Guan 已提交
153
  }
S
shm  
Shengliang Guan 已提交
154
#endif
S
shm  
Shengliang Guan 已提交
155
}
S
shm  
Shengliang Guan 已提交
156

157 158
static int32_t taosProcQueuePush(SProcObj *pProc, SProcQueue *pQueue, const char *pHead, int16_t rawHeadLen,
                                 const char *pBody, int32_t rawBodyLen, int64_t handle, ProcFuncType ftype) {
S
shm  
Shengliang Guan 已提交
159 160 161
  const int32_t headLen = CEIL8(rawHeadLen);
  const int32_t bodyLen = CEIL8(rawBodyLen);
  const int32_t fullLen = headLen + bodyLen + 8;
S
shm  
Shengliang Guan 已提交
162

S
shm  
Shengliang Guan 已提交
163
  taosThreadMutexLock(&pQueue->mutex);
S
shm  
Shengliang Guan 已提交
164
  if (fullLen > pQueue->avail) {
S
shm  
Shengliang Guan 已提交
165
    taosThreadMutexUnlock(&pQueue->mutex);
S
shm  
Shengliang Guan 已提交
166 167 168 169
    terrno = TSDB_CODE_OUT_OF_SHM_MEM;
    return -1;
  }

170 171 172 173 174 175 176 177
  if (handle != 0 && ftype == PROC_REQ) {
    if (taosHashPut(pProc->hash, &handle, sizeof(int64_t), &handle, sizeof(int64_t)) != 0) {
      taosThreadMutexUnlock(&pQueue->mutex);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
  }

S
Shengliang Guan 已提交
178
  const int32_t pos = pQueue->tail;
S
shm  
Shengliang Guan 已提交
179
  if (pQueue->tail < pQueue->total) {
S
Shengliang Guan 已提交
180 181 182
    *(int16_t *)(pQueue->pBuffer + pQueue->tail) = headLen;
    *(int8_t *)(pQueue->pBuffer + pQueue->tail + 2) = (int8_t)ftype;
    *(int32_t *)(pQueue->pBuffer + pQueue->tail + 4) = bodyLen;
S
shm  
Shengliang Guan 已提交
183
  } else {
S
Shengliang Guan 已提交
184
    *(int16_t *)(pQueue->pBuffer) = headLen;
S
Shengliang Guan 已提交
185
    *(int8_t *)(pQueue->pBuffer + 2) = (int8_t)ftype;
S
shm  
Shengliang Guan 已提交
186 187 188
    *(int32_t *)(pQueue->pBuffer + 4) = bodyLen;
  }

S
shm  
Shengliang Guan 已提交
189
  if (pQueue->tail < pQueue->head) {
S
shm  
Shengliang Guan 已提交
190 191 192
    memcpy(pQueue->pBuffer + pQueue->tail + 8, pHead, rawHeadLen);
    memcpy(pQueue->pBuffer + pQueue->tail + 8 + headLen, pBody, rawBodyLen);
    pQueue->tail = pQueue->tail + 8 + headLen + bodyLen;
S
shm  
Shengliang Guan 已提交
193 194
  } else {
    int32_t remain = pQueue->total - pQueue->tail;
S
shm  
Shengliang Guan 已提交
195 196 197 198 199 200 201 202 203
    if (remain == 0) {
      memcpy(pQueue->pBuffer + 8, pHead, rawHeadLen);
      memcpy(pQueue->pBuffer + 8 + headLen, pBody, rawBodyLen);
      pQueue->tail = 8 + headLen + bodyLen;
    } else if (remain == 8) {
      memcpy(pQueue->pBuffer, pHead, rawHeadLen);
      memcpy(pQueue->pBuffer + headLen, pBody, rawBodyLen);
      pQueue->tail = headLen + bodyLen;
    } else if (remain < 8 + headLen) {
S
Shengliang Guan 已提交
204
      memcpy(pQueue->pBuffer + pQueue->tail + 8, pHead, remain - 8);
S
shm  
Shengliang Guan 已提交
205 206 207
      memcpy(pQueue->pBuffer, pHead + remain - 8, rawHeadLen - (remain - 8));
      memcpy(pQueue->pBuffer + headLen - (remain - 8), pBody, rawBodyLen);
      pQueue->tail = headLen - (remain - 8) + bodyLen;
S
Shengliang Guan 已提交
208 209 210
    } else if (remain < 8 + headLen + bodyLen) {
      memcpy(pQueue->pBuffer + pQueue->tail + 8, pHead, rawHeadLen);
      memcpy(pQueue->pBuffer + pQueue->tail + 8 + headLen, pBody, remain - 8 - headLen);
S
shm  
Shengliang Guan 已提交
211 212
      memcpy(pQueue->pBuffer, pBody + remain - 8 - headLen, rawBodyLen - (remain - 8 - headLen));
      pQueue->tail = bodyLen - (remain - 8 - headLen);
S
shm  
Shengliang Guan 已提交
213
    } else {
S
Shengliang Guan 已提交
214 215 216
      memcpy(pQueue->pBuffer + pQueue->tail + 8, pHead, rawHeadLen);
      memcpy(pQueue->pBuffer + pQueue->tail + headLen + 8, pBody, rawBodyLen);
      pQueue->tail = pQueue->tail + headLen + bodyLen + 8;
S
shm  
Shengliang Guan 已提交
217 218 219 220 221
    }
  }

  pQueue->avail -= fullLen;
  pQueue->items++;
S
shm  
Shengliang Guan 已提交
222
  taosThreadMutexUnlock(&pQueue->mutex);
S
shm  
Shengliang Guan 已提交
223
  tsem_post(&pQueue->sem);
S
shm  
Shengliang Guan 已提交
224

S
Shengliang Guan 已提交
225 226
  uTrace("proc:%s, push msg at pos:%d ftype:%d remain:%d, head:%d %p body:%d %p", pQueue->name, pos, ftype,
         pQueue->items, headLen, pHead, bodyLen, pBody);
S
shm  
Shengliang Guan 已提交
227 228 229
  return 0;
}

S
shm  
Shengliang Guan 已提交
230 231 232
static int32_t taosProcQueuePop(SProcQueue *pQueue, void **ppHead, int16_t *pHeadLen, void **ppBody, int32_t *pBodyLen,
                                ProcFuncType *pFuncType, ProcMallocFp mallocHeadFp, ProcFreeFp freeHeadFp,
                                ProcMallocFp mallocBodyFp, ProcFreeFp freeBodyFp) {
S
shm  
Shengliang Guan 已提交
233 234
  tsem_wait(&pQueue->sem);

S
shm  
Shengliang Guan 已提交
235
  taosThreadMutexLock(&pQueue->mutex);
S
shm  
Shengliang Guan 已提交
236
  if (pQueue->total - pQueue->avail <= 0) {
S
shm  
Shengliang Guan 已提交
237
    taosThreadMutexUnlock(&pQueue->mutex);
S
shm  
Shengliang Guan 已提交
238
    terrno = TSDB_CODE_OUT_OF_SHM_MEM;
S
shm  
Shengliang Guan 已提交
239
    return 0;
S
shm  
Shengliang Guan 已提交
240 241
  }

S
Shengliang Guan 已提交
242 243
  int16_t headLen = 0;
  int8_t  ftype = 0;
S
shm  
Shengliang Guan 已提交
244 245
  int32_t bodyLen = 0;
  if (pQueue->head < pQueue->total) {
S
Shengliang Guan 已提交
246 247
    headLen = *(int16_t *)(pQueue->pBuffer + pQueue->head);
    ftype = *(int8_t *)(pQueue->pBuffer + pQueue->head + 2);
S
shm  
Shengliang Guan 已提交
248 249
    bodyLen = *(int32_t *)(pQueue->pBuffer + pQueue->head + 4);
  } else {
S
Shengliang Guan 已提交
250
    headLen = *(int16_t *)(pQueue->pBuffer);
251
    ftype = *(int8_t *)(pQueue->pBuffer + 2);
S
shm  
Shengliang Guan 已提交
252 253
    bodyLen = *(int32_t *)(pQueue->pBuffer + 4);
  }
S
shm  
Shengliang Guan 已提交
254

S
shm  
Shengliang Guan 已提交
255 256
  void *pHead = (*mallocHeadFp)(headLen);
  void *pBody = (*mallocBodyFp)(bodyLen);
S
shm  
Shengliang Guan 已提交
257
  if (pHead == NULL || pBody == NULL) {
S
shm  
Shengliang Guan 已提交
258
    taosThreadMutexUnlock(&pQueue->mutex);
S
shm  
Shengliang Guan 已提交
259
    tsem_post(&pQueue->sem);
S
shm  
Shengliang Guan 已提交
260 261
    (*freeHeadFp)(pHead);
    (*freeBodyFp)(pBody);
S
shm  
Shengliang Guan 已提交
262 263 264 265
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
266
  const int32_t pos = pQueue->head;
S
shm  
Shengliang Guan 已提交
267
  if (pQueue->head < pQueue->tail) {
S
shm  
Shengliang Guan 已提交
268 269 270
    memcpy(pHead, pQueue->pBuffer + pQueue->head + 8, headLen);
    memcpy(pBody, pQueue->pBuffer + pQueue->head + 8 + headLen, bodyLen);
    pQueue->head = pQueue->head + 8 + headLen + bodyLen;
S
shm  
Shengliang Guan 已提交
271 272
  } else {
    int32_t remain = pQueue->total - pQueue->head;
S
shm  
Shengliang Guan 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285
    if (remain == 0) {
      memcpy(pHead, pQueue->pBuffer + 8, headLen);
      memcpy(pBody, pQueue->pBuffer + 8 + headLen, bodyLen);
      pQueue->head = 8 + headLen + bodyLen;
    } else if (remain == 8) {
      memcpy(pHead, pQueue->pBuffer, headLen);
      memcpy(pBody, pQueue->pBuffer + headLen, bodyLen);
      pQueue->head = headLen + bodyLen;
    } else if (remain < 8 + headLen) {
      memcpy(pHead, pQueue->pBuffer + pQueue->head + 8, remain - 8);
      memcpy(pHead + remain - 8, pQueue->pBuffer, headLen - (remain - 8));
      memcpy(pBody, pQueue->pBuffer + headLen - (remain - 8), bodyLen);
      pQueue->head = headLen - (remain - 8) + bodyLen;
S
Shengliang Guan 已提交
286
    } else if (remain < 8 + headLen + bodyLen) {
S
shm  
Shengliang Guan 已提交
287 288 289 290
      memcpy(pHead, pQueue->pBuffer + pQueue->head + 8, headLen);
      memcpy(pBody, pQueue->pBuffer + pQueue->head + 8 + headLen, remain - 8 - headLen);
      memcpy(pBody + remain - 8 - headLen, pQueue->pBuffer, bodyLen - (remain - 8 - headLen));
      pQueue->head = bodyLen - (remain - 8 - headLen);
S
shm  
Shengliang Guan 已提交
291
    } else {
S
shm  
Shengliang Guan 已提交
292 293 294
      memcpy(pHead, pQueue->pBuffer + pQueue->head + 8, headLen);
      memcpy(pBody, pQueue->pBuffer + pQueue->head + headLen + 8, bodyLen);
      pQueue->head = pQueue->head + headLen + bodyLen + 8;
S
shm  
Shengliang Guan 已提交
295 296 297
    }
  }

S
shm  
Shengliang Guan 已提交
298
  pQueue->avail = pQueue->avail + headLen + bodyLen + 8;
S
shm  
Shengliang Guan 已提交
299
  pQueue->items--;
S
shm  
Shengliang Guan 已提交
300
  taosThreadMutexUnlock(&pQueue->mutex);
S
shm  
Shengliang Guan 已提交
301

S
shm  
Shengliang Guan 已提交
302 303 304 305
  *ppHead = pHead;
  *ppBody = pBody;
  *pHeadLen = headLen;
  *pBodyLen = bodyLen;
S
Shengliang Guan 已提交
306
  *pFuncType = (ProcFuncType)ftype;
S
shm  
Shengliang Guan 已提交
307

S
Shengliang Guan 已提交
308 309
  uTrace("proc:%s, pop msg at pos:%d ftype:%d remain:%d, head:%d %p body:%d %p", pQueue->name, pos, ftype,
         pQueue->items, headLen, pHead, bodyLen, pBody);
S
shm  
Shengliang Guan 已提交
310
  return 1;
S
shm  
Shengliang Guan 已提交
311 312 313
}

SProcObj *taosProcInit(const SProcCfg *pCfg) {
wafwerar's avatar
wafwerar 已提交
314
  SProcObj *pProc = taosMemoryCalloc(1, sizeof(SProcObj));
S
shm  
Shengliang Guan 已提交
315 316 317 318 319
  if (pProc == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
shm  
Shengliang Guan 已提交
320 321 322 323 324 325 326 327
  int32_t cstart = 0;
  int32_t csize = CEIL8(pCfg->shm.size / 2);
  int32_t pstart = csize;
  int32_t psize = CEIL8(pCfg->shm.size - pstart);
  if (pstart + psize > pCfg->shm.size) {
    psize -= 8;
  }

S
shm  
Shengliang Guan 已提交
328
  pProc->name = pCfg->name;
S
shm  
Shengliang Guan 已提交
329 330
  pProc->pChildQueue = taosProcInitQueue(pCfg->name, pCfg->isChild, (char *)pCfg->shm.ptr + cstart, csize);
  pProc->pParentQueue = taosProcInitQueue(pCfg->name, pCfg->isChild, (char *)pCfg->shm.ptr + pstart, psize);
331
  pProc->hash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
S
shm  
Shengliang Guan 已提交
332
  if (pProc->pChildQueue == NULL || pProc->pParentQueue == NULL) {
S
shm  
Shengliang Guan 已提交
333
    taosProcCleanupQueue(pProc->pChildQueue);
wafwerar's avatar
wafwerar 已提交
334
    taosMemoryFree(pProc);
S
shm  
Shengliang Guan 已提交
335 336 337
    return NULL;
  }

S
shm  
Shengliang Guan 已提交
338
  pProc->name = pCfg->name;
339
  pProc->parent = pCfg->parent;
S
shm  
Shengliang Guan 已提交
340 341 342 343 344 345 346 347 348 349 350 351
  pProc->childMallocHeadFp = pCfg->childMallocHeadFp;
  pProc->childFreeHeadFp = pCfg->childFreeHeadFp;
  pProc->childMallocBodyFp = pCfg->childMallocBodyFp;
  pProc->childFreeBodyFp = pCfg->childFreeBodyFp;
  pProc->childConsumeFp = pCfg->childConsumeFp;
  pProc->parentMallocHeadFp = pCfg->parentMallocHeadFp;
  pProc->parentFreeHeadFp = pCfg->parentFreeHeadFp;
  pProc->parentMallocBodyFp = pCfg->parentMallocBodyFp;
  pProc->parentFreeBodyFp = pCfg->parentFreeBodyFp;
  pProc->parentConsumeFp = pCfg->parentConsumeFp;
  pProc->isChild = pCfg->isChild;

S
shm  
Shengliang Guan 已提交
352
  uDebug("proc:%s, is initialized, isChild:%d child queue:%p parent queue:%p", pProc->name, pProc->isChild,
S
shm  
Shengliang Guan 已提交
353
         pProc->pChildQueue, pProc->pParentQueue);
S
shm  
Shengliang Guan 已提交
354

S
shm  
Shengliang Guan 已提交
355 356 357
  return pProc;
}

S
shm  
Shengliang Guan 已提交
358
static void taosProcThreadLoop(SProcObj *pProc) {
S
shm  
Shengliang Guan 已提交
359
  void         *pHead, *pBody;
S
Shengliang Guan 已提交
360 361 362
  int16_t       headLen;
  ProcFuncType  ftype;
  int32_t       bodyLen;
S
shm  
Shengliang Guan 已提交
363 364 365 366 367 368
  SProcQueue   *pQueue;
  ProcConsumeFp consumeFp;
  ProcMallocFp  mallocHeadFp;
  ProcFreeFp    freeHeadFp;
  ProcMallocFp  mallocBodyFp;
  ProcFreeFp    freeBodyFp;
S
shm  
Shengliang Guan 已提交
369

S
shm  
Shengliang Guan 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
  if (pProc->isChild) {
    pQueue = pProc->pChildQueue;
    consumeFp = pProc->childConsumeFp;
    mallocHeadFp = pProc->childMallocHeadFp;
    freeHeadFp = pProc->childFreeHeadFp;
    mallocBodyFp = pProc->childMallocBodyFp;
    freeBodyFp = pProc->childFreeBodyFp;
  } else {
    pQueue = pProc->pParentQueue;
    consumeFp = pProc->parentConsumeFp;
    mallocHeadFp = pProc->parentMallocHeadFp;
    freeHeadFp = pProc->parentFreeHeadFp;
    mallocBodyFp = pProc->parentMallocBodyFp;
    freeBodyFp = pProc->parentFreeBodyFp;
  }

S
shm  
Shengliang Guan 已提交
386
  uDebug("proc:%s, start to get msg from queue:%p", pProc->name, pQueue);
S
shm  
Shengliang Guan 已提交
387

S
shm  
Shengliang Guan 已提交
388
  while (1) {
S
shm  
Shengliang Guan 已提交
389 390
    int32_t numOfMsgs = taosProcQueuePop(pQueue, &pHead, &headLen, &pBody, &bodyLen, &ftype, mallocHeadFp, freeHeadFp,
                                         mallocBodyFp, freeBodyFp);
S
shm  
Shengliang Guan 已提交
391
    if (numOfMsgs == 0) {
S
shm  
Shengliang Guan 已提交
392
      uDebug("proc:%s, get no msg from queue:%p and exit the proc thread", pProc->name, pQueue);
S
shm  
Shengliang Guan 已提交
393
      break;
S
shm  
Shengliang Guan 已提交
394
    } else if (numOfMsgs < 0) {
S
Shengliang Guan 已提交
395
      uError("proc:%s, get no msg from queue:%p since %s", pProc->name, pQueue, terrstr());
S
shm  
Shengliang Guan 已提交
396 397 398
      taosMsleep(1);
      continue;
    } else {
399
      (*consumeFp)(pProc->parent, pHead, headLen, pBody, bodyLen, ftype);
S
shm  
Shengliang Guan 已提交
400 401 402 403
    }
  }
}

S
shm  
Shengliang Guan 已提交
404
int32_t taosProcRun(SProcObj *pProc) {
S
Shengliang Guan 已提交
405 406 407
  TdThreadAttr thAttr;
  taosThreadAttrInit(&thAttr);
  taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
S
shm  
Shengliang Guan 已提交
408

S
shm  
Shengliang Guan 已提交
409 410 411 412
  if (taosThreadCreate(&pProc->thread, &thAttr, (ProcThreadFp)taosProcThreadLoop, pProc) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    uError("failed to create thread since %s", terrstr());
    return -1;
S
shm  
Shengliang Guan 已提交
413 414
  }

S
shm  
Shengliang Guan 已提交
415
  uDebug("proc:%s, start to consume queue:%p, thread:%" PRId64, pProc->name, pProc->pChildQueue, pProc->thread);
S
shm  
Shengliang Guan 已提交
416 417 418
  return 0;
}

S
shm  
Shengliang Guan 已提交
419 420
static void taosProcStop(SProcObj *pProc) {
  if (!taosCheckPthreadValid(pProc->thread)) return;
S
shm  
Shengliang Guan 已提交
421

S
shm  
Shengliang Guan 已提交
422
  uDebug("proc:%s, start to join thread:%" PRId64, pProc->name, pProc->thread);
S
shm  
Shengliang Guan 已提交
423 424 425
  SProcQueue *pQueue;
  if (pProc->isChild) {
    pQueue = pProc->pChildQueue;
S
shm  
Shengliang Guan 已提交
426 427
  } else {
    pQueue = pProc->pParentQueue;
S
shm  
Shengliang Guan 已提交
428 429 430 431
  }
  tsem_post(&pQueue->sem);
  taosThreadJoin(pProc->thread, NULL);
}
S
shm  
Shengliang Guan 已提交
432

S
shm  
Shengliang Guan 已提交
433 434
void taosProcCleanup(SProcObj *pProc) {
  if (pProc != NULL) {
S
shm  
Shengliang Guan 已提交
435
    uDebug("proc:%s, start to clean up", pProc->name);
S
shm  
Shengliang Guan 已提交
436
    taosProcStop(pProc);
S
shm  
Shengliang Guan 已提交
437 438
    taosProcCleanupQueue(pProc->pChildQueue);
    taosProcCleanupQueue(pProc->pParentQueue);
439 440 441 442 443
    if (pProc->hash != NULL) {
      taosHashCleanup(pProc->hash);
      pProc->hash = NULL;
    }

S
shm  
Shengliang Guan 已提交
444
    uDebug("proc:%s, is cleaned up", pProc->name);
wafwerar's avatar
wafwerar 已提交
445
    taosMemoryFree(pProc);
S
shm  
Shengliang Guan 已提交
446 447 448
  }
}

S
shm  
Shengliang Guan 已提交
449
int32_t taosProcPutToChildQ(SProcObj *pProc, const void *pHead, int16_t headLen, const void *pBody, int32_t bodyLen,
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
                            void *handle, ProcFuncType ftype) {
  return taosProcQueuePush(pProc, pProc->pChildQueue, pHead, headLen, pBody, bodyLen, (int64_t)handle, ftype);
}

void taosProcRemoveHandle(SProcObj *pProc, void *handle) {
  int64_t h = (int64_t)handle;
  taosThreadMutexLock(&pProc->pChildQueue->mutex);
  taosHashRemove(pProc->hash, &h, sizeof(int64_t));
  taosThreadMutexUnlock(&pProc->pChildQueue->mutex);
}

void taosProcCloseHandles(SProcObj *pProc, void (*HandleFp)(void *handle)) {
  taosThreadMutexLock(&pProc->pChildQueue->mutex);
  void *h = taosHashIterate(pProc->hash, NULL);
  while (h != NULL) {
    void *handle = *((void **)h);
    (*HandleFp)(handle);
  }
  taosThreadMutexUnlock(&pProc->pChildQueue->mutex);
S
shm  
Shengliang Guan 已提交
469
}
S
shm  
Shengliang Guan 已提交
470

S
shm  
Shengliang Guan 已提交
471 472
void taosProcPutToParentQ(SProcObj *pProc, const void *pHead, int16_t headLen, const void *pBody, int32_t bodyLen,
                          ProcFuncType ftype) {
S
Shengliang Guan 已提交
473
  int32_t retry = 0;
474
  while (taosProcQueuePush(pProc, pProc->pParentQueue, pHead, headLen, pBody, bodyLen, 0, ftype) != 0) {
S
Shengliang Guan 已提交
475 476
    uInfo("proc:%s, failed to put msg to queue:%p since %s, retry:%d", pProc->name, pProc->pParentQueue, terrstr(),
          retry);
S
Shengliang Guan 已提交
477 478
    retry++;
    taosMsleep(retry);
S
shm  
Shengliang Guan 已提交
479
  }
S
shm  
Shengliang Guan 已提交
480
}