tworker.c 9.5 KB
Newer Older
S
TD-2393  
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 "tworker.h"
S
Shengliang Guan 已提交
18
#include "taoserror.h"
S
log  
Shengliang Guan 已提交
19
#include "tlog.h"
S
TD-2393  
Shengliang Guan 已提交
20

S
Shengliang Guan 已提交
21
typedef void *(*ThreadFp)(void *param);
22

S
Shengliang Guan 已提交
23
int32_t tQWorkerInit(SQWorkerPool *pool) {
24
  pool->qset = taosOpenQset();
wafwerar's avatar
wafwerar 已提交
25
  pool->workers = taosMemoryCalloc(pool->max, sizeof(SQWorker));
S
Shengliang Guan 已提交
26 27 28 29 30
  if (pool->workers == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

wafwerar's avatar
wafwerar 已提交
31
  if (taosThreadMutexInit(&pool->mutex, NULL)) {
S
Shengliang Guan 已提交
32
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
33 34 35 36 37
    return -1;
  }

  for (int32_t i = 0; i < pool->max; ++i) {
    SQWorker *worker = pool->workers + i;
38 39
    worker->id = i;
    worker->pool = pool;
S
TD-2393  
Shengliang Guan 已提交
40 41
  }

S
Shengliang Guan 已提交
42
  uDebug("worker:%s is initialized, min:%d max:%d", pool->name, pool->min, pool->max);
S
TD-2393  
Shengliang Guan 已提交
43 44 45
  return 0;
}

S
Shengliang Guan 已提交
46 47 48
void tQWorkerCleanup(SQWorkerPool *pool) {
  for (int32_t i = 0; i < pool->max; ++i) {
    SQWorker *worker = pool->workers + i;
S
Shengliang Guan 已提交
49
    if (worker == NULL) continue;
50
    if (taosCheckPthreadValid(worker->thread)) {
51
      taosQsetThreadResume(pool->qset);
S
TD-2393  
Shengliang Guan 已提交
52 53 54
    }
  }

S
Shengliang Guan 已提交
55 56
  for (int32_t i = 0; i < pool->max; ++i) {
    SQWorker *worker = pool->workers + i;
S
Shengliang Guan 已提交
57
    if (worker == NULL) continue;
58
    if (taosCheckPthreadValid(worker->thread)) {
wafwerar's avatar
wafwerar 已提交
59
      taosThreadJoin(worker->thread, NULL);
60
      taosThreadClear(&worker->thread);
S
TD-2393  
Shengliang Guan 已提交
61 62 63
    }
  }

wafwerar's avatar
wafwerar 已提交
64
  taosMemoryFreeClear(pool->workers);
65
  taosCloseQset(pool->qset);
wafwerar's avatar
wafwerar 已提交
66
  taosThreadMutexDestroy(&pool->mutex);
S
TD-2393  
Shengliang Guan 已提交
67

S
Shengliang Guan 已提交
68
  uDebug("worker:%s is closed", pool->name);
S
TD-2393  
Shengliang Guan 已提交
69 70
}

S
Shengliang Guan 已提交
71 72
static void *tQWorkerThreadFp(SQWorker *worker) {
  SQWorkerPool *pool = worker->pool;
S
Shengliang Guan 已提交
73
  FItem         fp = NULL;
74

S
Shengliang Guan 已提交
75 76
  void   *msg = NULL;
  void   *ahandle = NULL;
77 78 79 80
  int32_t code = 0;

  taosBlockSIGPIPE();
  setThreadName(pool->name);
S
Shengliang Guan 已提交
81
  uDebug("worker:%s:%d is running", pool->name, worker->id);
82 83

  while (1) {
S
Shengliang Guan 已提交
84
    if (taosReadQitemFromQset(pool->qset, (void **)&msg, &ahandle, &fp) == 0) {
S
Shengliang Guan 已提交
85
      uDebug("worker:%s:%d qset:%p, got no message and exiting", pool->name, worker->id, pool->qset);
86 87 88
      break;
    }

S
Shengliang Guan 已提交
89
    if (fp != NULL) {
S
Shengliang Guan 已提交
90 91
      SQueueInfo info = {.ahandle = ahandle, .workerId = worker->id, .threadNum = pool->num};
      (*fp)(&info, msg);
S
Shengliang Guan 已提交
92
    }
93 94 95 96 97
  }

  return NULL;
}

S
Shengliang Guan 已提交
98
STaosQueue *tQWorkerAllocQueue(SQWorkerPool *pool, void *ahandle, FItem fp) {
wafwerar's avatar
wafwerar 已提交
99
  taosThreadMutexLock(&pool->mutex);
100
  STaosQueue *queue = taosOpenQueue();
101
  if (queue == NULL) {
wafwerar's avatar
wafwerar 已提交
102
    taosThreadMutexUnlock(&pool->mutex);
S
Shengliang Guan 已提交
103
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
TD-2393  
Shengliang Guan 已提交
104 105 106
    return NULL;
  }

S
Shengliang Guan 已提交
107
  taosSetQueueFp(queue, fp, NULL);
108
  taosAddIntoQset(pool->qset, queue, ahandle);
S
TD-2393  
Shengliang Guan 已提交
109 110

  // spawn a thread to process queue
111
  if (pool->num < pool->max) {
S
TD-2393  
Shengliang Guan 已提交
112
    do {
S
Shengliang Guan 已提交
113
      SQWorker *worker = pool->workers + pool->num;
S
TD-2393  
Shengliang Guan 已提交
114

wafwerar's avatar
wafwerar 已提交
115 116 117
      TdThreadAttr thAttr;
      taosThreadAttrInit(&thAttr);
      taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
S
TD-2393  
Shengliang Guan 已提交
118

S
Shengliang Guan 已提交
119
      if (taosThreadCreate(&worker->thread, &thAttr, (ThreadFp)tQWorkerThreadFp, worker) != 0) {
S
Shengliang Guan 已提交
120
        uError("worker:%s:%d failed to create thread to process since %s", pool->name, worker->id, strerror(errno));
S
Shengliang Guan 已提交
121
        taosCloseQueue(queue);
S
Shengliang Guan 已提交
122
        terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
123 124
        queue = NULL;
        break;
S
TD-2393  
Shengliang Guan 已提交
125 126
      }

wafwerar's avatar
wafwerar 已提交
127
      taosThreadAttrDestroy(&thAttr);
128
      pool->num++;
S
Shengliang Guan 已提交
129
      uDebug("worker:%s:%d is launched, total:%d", pool->name, worker->id, pool->num);
130
    } while (pool->num < pool->min);
S
TD-2393  
Shengliang Guan 已提交
131 132
  }

wafwerar's avatar
wafwerar 已提交
133
  taosThreadMutexUnlock(&pool->mutex);
S
Shengliang Guan 已提交
134
  uDebug("worker:%s, queue:%p is allocated, ahandle:%p", pool->name, queue, ahandle);
135 136 137 138

  return queue;
}

S
Shengliang Guan 已提交
139
void tQWorkerFreeQueue(SQWorkerPool *pool, STaosQueue *queue) {
140
  taosCloseQueue(queue);
S
Shengliang Guan 已提交
141
  uDebug("worker:%s, queue:%p is freed", pool->name, queue);
142 143
}

S
Shengliang Guan 已提交
144
int32_t tWWorkerInit(SWWorkerPool *pool) {
145
  pool->nextId = 0;
wafwerar's avatar
wafwerar 已提交
146
  pool->workers = taosMemoryCalloc(pool->max, sizeof(SWWorker));
S
Shengliang Guan 已提交
147 148 149 150 151
  if (pool->workers == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

wafwerar's avatar
wafwerar 已提交
152
  if (taosThreadMutexInit(&pool->mutex, NULL) != 0) {
S
Shengliang Guan 已提交
153 154 155
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
156 157

  for (int32_t i = 0; i < pool->max; ++i) {
S
Shengliang Guan 已提交
158
    SWWorker *worker = pool->workers + i;
159 160 161 162 163 164
    worker->id = i;
    worker->qall = NULL;
    worker->qset = NULL;
    worker->pool = pool;
  }

S
Shengliang Guan 已提交
165
  uInfo("worker:%s is initialized, max:%d", pool->name, pool->max);
166 167 168
  return 0;
}

S
Shengliang Guan 已提交
169
void tWWorkerCleanup(SWWorkerPool *pool) {
170
  for (int32_t i = 0; i < pool->max; ++i) {
S
Shengliang Guan 已提交
171
    SWWorker *worker = pool->workers + i;
172
    if (taosCheckPthreadValid(worker->thread)) {
S
Shengliang Guan 已提交
173 174 175
      if (worker->qset) {
        taosQsetThreadResume(worker->qset);
      }
176 177 178 179
    }
  }

  for (int32_t i = 0; i < pool->max; ++i) {
S
Shengliang Guan 已提交
180
    SWWorker *worker = pool->workers + i;
181
    if (taosCheckPthreadValid(worker->thread)) {
wafwerar's avatar
wafwerar 已提交
182
      taosThreadJoin(worker->thread, NULL);
183
      taosThreadClear(&worker->thread);
184 185 186 187 188
      taosFreeQall(worker->qall);
      taosCloseQset(worker->qset);
    }
  }

wafwerar's avatar
wafwerar 已提交
189
  taosMemoryFreeClear(pool->workers);
wafwerar's avatar
wafwerar 已提交
190
  taosThreadMutexDestroy(&pool->mutex);
191

S
Shengliang Guan 已提交
192
  uInfo("worker:%s is closed", pool->name);
193 194
}

S
Shengliang Guan 已提交
195
static void *tWWorkerThreadFp(SWWorker *worker) {
S
Shengliang Guan 已提交
196
  SWWorkerPool *pool = worker->pool;
S
Shengliang Guan 已提交
197
  FItems        fp = NULL;
198

S
Shengliang Guan 已提交
199 200
  void   *msg = NULL;
  void   *ahandle = NULL;
201 202 203 204 205
  int32_t numOfMsgs = 0;
  int32_t qtype = 0;

  taosBlockSIGPIPE();
  setThreadName(pool->name);
S
Shengliang Guan 已提交
206
  uDebug("worker:%s:%d is running", pool->name, worker->id);
207 208

  while (1) {
S
Shengliang Guan 已提交
209
    numOfMsgs = taosReadAllQitemsFromQset(worker->qset, worker->qall, &ahandle, &fp);
210
    if (numOfMsgs == 0) {
S
Shengliang Guan 已提交
211
      uDebug("worker:%s:%d qset:%p, got no message and exiting", pool->name, worker->id, worker->qset);
212 213 214
      break;
    }

S
Shengliang Guan 已提交
215
    if (fp != NULL) {
S
Shengliang Guan 已提交
216
      SQueueInfo info = {.ahandle = ahandle, .workerId = worker->id, .threadNum = pool->num};
S
Shengliang Guan 已提交
217
      (*fp)(&info, worker->qall, numOfMsgs);
218 219 220 221 222 223
    }
  }

  return NULL;
}

S
Shengliang Guan 已提交
224
STaosQueue *tWWorkerAllocQueue(SWWorkerPool *pool, void *ahandle, FItems fp) {
wafwerar's avatar
wafwerar 已提交
225
  taosThreadMutexLock(&pool->mutex);
S
Shengliang Guan 已提交
226
  SWWorker *worker = pool->workers + pool->nextId;
227

228
  STaosQueue *queue = taosOpenQueue();
229
  if (queue == NULL) {
wafwerar's avatar
wafwerar 已提交
230
    taosThreadMutexUnlock(&pool->mutex);
S
Shengliang Guan 已提交
231
    terrno = TSDB_CODE_OUT_OF_MEMORY;
232 233 234
    return NULL;
  }

S
Shengliang Guan 已提交
235 236
  taosSetQueueFp(queue, NULL, fp);

237 238 239 240
  if (worker->qset == NULL) {
    worker->qset = taosOpenQset();
    if (worker->qset == NULL) {
      taosCloseQueue(queue);
wafwerar's avatar
wafwerar 已提交
241
      taosThreadMutexUnlock(&pool->mutex);
242 243 244 245 246 247 248 249
      return NULL;
    }

    taosAddIntoQset(worker->qset, queue, ahandle);
    worker->qall = taosAllocateQall();
    if (worker->qall == NULL) {
      taosCloseQset(worker->qset);
      taosCloseQueue(queue);
wafwerar's avatar
wafwerar 已提交
250
      taosThreadMutexUnlock(&pool->mutex);
S
Shengliang Guan 已提交
251
      terrno = TSDB_CODE_OUT_OF_MEMORY;
252 253
      return NULL;
    }
wafwerar's avatar
wafwerar 已提交
254 255 256
    TdThreadAttr thAttr;
    taosThreadAttrInit(&thAttr);
    taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
257

wafwerar's avatar
wafwerar 已提交
258
    if (taosThreadCreate(&worker->thread, &thAttr, (ThreadFp)tWWorkerThreadFp, worker) != 0) {
S
Shengliang Guan 已提交
259
      uError("worker:%s:%d failed to create thread to process since %s", pool->name, worker->id, strerror(errno));
260 261 262
      taosFreeQall(worker->qall);
      taosCloseQset(worker->qset);
      taosCloseQueue(queue);
S
Shengliang Guan 已提交
263
      terrno = TSDB_CODE_OUT_OF_MEMORY;
264 265
      queue = NULL;
    } else {
S
Shengliang Guan 已提交
266
      uDebug("worker:%s:%d is launched, max:%d", pool->name, worker->id, pool->max);
267 268 269
      pool->nextId = (pool->nextId + 1) % pool->max;
    }

wafwerar's avatar
wafwerar 已提交
270
    taosThreadAttrDestroy(&thAttr);
S
Shengliang Guan 已提交
271 272
    pool->num++;
    if (pool->num > pool->max) pool->num = pool->max;
273 274 275 276 277
  } else {
    taosAddIntoQset(worker->qset, queue, ahandle);
    pool->nextId = (pool->nextId + 1) % pool->max;
  }

wafwerar's avatar
wafwerar 已提交
278
  taosThreadMutexUnlock(&pool->mutex);
S
Shengliang Guan 已提交
279
  uDebug("worker:%s, queue:%p is allocated, ahandle:%p", pool->name, queue, ahandle);
S
TD-2393  
Shengliang Guan 已提交
280

281
  return queue;
S
TD-2393  
Shengliang Guan 已提交
282 283
}

S
Shengliang Guan 已提交
284
void tWWorkerFreeQueue(SWWorkerPool *pool, STaosQueue *queue) {
285
  taosCloseQueue(queue);
S
Shengliang Guan 已提交
286
  uDebug("worker:%s, queue:%p is freed", pool->name, queue);
S
TD-2393  
Shengliang Guan 已提交
287
}
S
Shengliang Guan 已提交
288

S
Shengliang Guan 已提交
289
int32_t tSingleWorkerInit(SSingleWorker *pWorker, const SSingleWorkerCfg *pCfg) {
S
Shengliang Guan 已提交
290 291
  SQWorkerPool *pPool = &pWorker->pool;
  pPool->name = pCfg->name;
S
shm  
Shengliang Guan 已提交
292 293
  pPool->min = pCfg->min;
  pPool->max = pCfg->max;
S
Shengliang Guan 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306
  if (tQWorkerInit(pPool) != 0) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  pWorker->queue = tQWorkerAllocQueue(pPool, pCfg->param, pCfg->fp);
  if (pWorker->queue == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  pWorker->name = pCfg->name;
  return 0;
}

S
Shengliang Guan 已提交
307
void tSingleWorkerCleanup(SSingleWorker *pWorker) {
S
Shengliang Guan 已提交
308 309 310 311 312 313 314 315 316 317
  if (pWorker->queue == NULL) return;

  while (!taosQueueEmpty(pWorker->queue)) {
    taosMsleep(10);
  }

  tQWorkerCleanup(&pWorker->pool);
  tQWorkerFreeQueue(&pWorker->pool, pWorker->queue);
}

S
Shengliang Guan 已提交
318
int32_t tMultiWorkerInit(SMultiWorker *pWorker, const SMultiWorkerCfg *pCfg) {
S
Shengliang Guan 已提交
319 320
  SWWorkerPool *pPool = &pWorker->pool;
  pPool->name = pCfg->name;
S
shm  
Shengliang Guan 已提交
321
  pPool->max = pCfg->max;
S
Shengliang Guan 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334
  if (tWWorkerInit(pPool) != 0) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  pWorker->queue = tWWorkerAllocQueue(pPool, pCfg->param, pCfg->fp);
  if (pWorker->queue == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  pWorker->name = pCfg->name;
  return 0;
}

S
Shengliang Guan 已提交
335
void tMultiWorkerCleanup(SMultiWorker *pWorker) {
S
Shengliang Guan 已提交
336 337 338 339 340 341 342 343 344
  if (pWorker->queue == NULL) return;

  while (!taosQueueEmpty(pWorker->queue)) {
    taosMsleep(10);
  }

  tWWorkerCleanup(&pWorker->pool);
  tWWorkerFreeQueue(&pWorker->pool, pWorker->queue);
}