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
  int32_t code = 0;
D
dapan1121 已提交
78
  int64_t ts = 0;
79 80 81

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

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

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

  return NULL;
}

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

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

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

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

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

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

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

  return queue;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  return NULL;
}

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

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

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

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
290
int32_t tSingleWorkerInit(SSingleWorker *pWorker, const SSingleWorkerCfg *pCfg) {
S
Shengliang Guan 已提交
291 292
  SQWorkerPool *pPool = &pWorker->pool;
  pPool->name = pCfg->name;
S
shm  
Shengliang Guan 已提交
293 294
  pPool->min = pCfg->min;
  pPool->max = pCfg->max;
S
Shengliang Guan 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307
  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 已提交
308
void tSingleWorkerCleanup(SSingleWorker *pWorker) {
S
Shengliang Guan 已提交
309 310 311 312 313 314 315 316 317 318
  if (pWorker->queue == NULL) return;

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

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

S
Shengliang Guan 已提交
319
int32_t tMultiWorkerInit(SMultiWorker *pWorker, const SMultiWorkerCfg *pCfg) {
S
Shengliang Guan 已提交
320 321
  SWWorkerPool *pPool = &pWorker->pool;
  pPool->name = pCfg->name;
S
shm  
Shengliang Guan 已提交
322
  pPool->max = pCfg->max;
S
Shengliang Guan 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335
  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 已提交
336
void tMultiWorkerCleanup(SMultiWorker *pWorker) {
S
Shengliang Guan 已提交
337 338 339 340 341 342 343 344 345
  if (pWorker->queue == NULL) return;

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

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