qworker.c 33.4 KB
Newer Older
D
dapan1121 已提交
1
#include "qworker.h"
H
Haojun Liao 已提交
2
#include "tname.h"
D
dapan1121 已提交
3
#include "planner.h"
H
Haojun Liao 已提交
4 5 6
#include "query.h"
#include "qworkerInt.h"
#include "tmsg.h"
D
dapan1121 已提交
7

D
dapan1121 已提交
8
int32_t qwValidateStatus(int8_t oriStatus, int8_t newStatus) {
D
dapan1121 已提交
9
  int32_t code = 0;
D
dapan1121 已提交
10

D
dapan1121 已提交
11 12 13 14 15 16
  if (oriStatus == newStatus) {
    QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
  }
  
  switch (oriStatus) {
    case JOB_TASK_STATUS_NULL:
D
dapan1121 已提交
17 18 19
      if (newStatus != JOB_TASK_STATUS_EXECUTING 
       && newStatus != JOB_TASK_STATUS_FAILED 
       && newStatus != JOB_TASK_STATUS_NOT_START) {
D
dapan1121 已提交
20 21 22 23 24
        QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
      }
      
      break;
    case JOB_TASK_STATUS_NOT_START:
D
dapan1121 已提交
25
      if (newStatus != JOB_TASK_STATUS_CANCELLED) {
D
dapan1121 已提交
26 27 28 29 30
        QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
      }
      
      break;
    case JOB_TASK_STATUS_EXECUTING:
D
dapan1121 已提交
31 32 33 34 35
      if (newStatus != JOB_TASK_STATUS_PARTIAL_SUCCEED 
       && newStatus != JOB_TASK_STATUS_FAILED 
       && newStatus != JOB_TASK_STATUS_CANCELLING 
       && newStatus != JOB_TASK_STATUS_CANCELLED 
       && newStatus != JOB_TASK_STATUS_DROPPING) {
D
dapan1121 已提交
36 37 38 39 40
        QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
      }
      
      break;
    case JOB_TASK_STATUS_PARTIAL_SUCCEED:
D
dapan1121 已提交
41 42 43
      if (newStatus != JOB_TASK_STATUS_EXECUTING 
       && newStatus != JOB_TASK_STATUS_SUCCEED
       && newStatus != JOB_TASK_STATUS_CANCELLED) {
D
dapan1121 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56
        QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
      }
      
      break;
    case JOB_TASK_STATUS_SUCCEED:
    case JOB_TASK_STATUS_FAILED:
    case JOB_TASK_STATUS_CANCELLING:
      if (newStatus != JOB_TASK_STATUS_CANCELLED) {
        QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
      }
      
      break;
    case JOB_TASK_STATUS_CANCELLED:
D
dapan1121 已提交
57 58 59 60
    case JOB_TASK_STATUS_DROPPING:
      QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
      break;
      
D
dapan1121 已提交
61 62
    default:
      qError("invalid task status:%d", oriStatus);
D
dapan1121 已提交
63 64 65
      return TSDB_CODE_QRY_APP_ERROR;
  }

D
dapan1121 已提交
66
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
67

D
dapan1121 已提交
68
_return:
D
dapan1121 已提交
69

D
dapan1121 已提交
70
  qError("invalid task status, from %d to %d", oriStatus, newStatus);
D
dapan1121 已提交
71
  QW_ERR_RET(code);
D
dapan1121 已提交
72 73
}

D
dapan1121 已提交
74
int32_t qwUpdateTaskInfo(SQWTaskStatus *task, int8_t type, void *data) {
D
dapan1121 已提交
75
  int32_t code = 0;
D
dapan1121 已提交
76
  
D
dapan1121 已提交
77 78 79
  switch (type) {
    case QW_TASK_INFO_STATUS: {
      int8_t newStatus = *(int8_t *)data;
D
dapan1121 已提交
80
      QW_ERR_RET(qwValidateStatus(task->status, newStatus));
D
dapan1121 已提交
81 82 83 84 85 86
      task->status = newStatus;
      break;
    }
    default:
      qError("uknown task info type:%d", type);
      return TSDB_CODE_QRY_APP_ERROR;
D
dapan1121 已提交
87
  }
D
dapan1121 已提交
88
  
D
dapan1121 已提交
89 90 91
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
92 93 94
int32_t qwAddTaskResCache(SQWorkerMgmt *mgmt, uint64_t qId, uint64_t tId, void *data) {
  char id[sizeof(qId) + sizeof(tId)] = {0};
  QW_SET_QTID(id, qId, tId);
D
dapan1121 已提交
95 96 97

  SQWorkerResCache resCache = {0};
  resCache.data = data;
D
dapan1121 已提交
98 99

  QW_LOCK(QW_WRITE, &mgmt->resLock);
D
dapan1121 已提交
100
  if (0 != taosHashPut(mgmt->resHash, id, sizeof(id), &resCache, sizeof(SQWorkerResCache))) {
D
dapan1121 已提交
101
    QW_UNLOCK(QW_WRITE, &mgmt->resLock);
D
dapan1121 已提交
102
    qError("taosHashPut queryId[%"PRIx64"] taskId[%"PRIx64"] to resHash failed", qId, tId);
D
dapan1121 已提交
103 104 105
    return TSDB_CODE_QRY_APP_ERROR;
  }

D
dapan1121 已提交
106 107
  QW_UNLOCK(QW_WRITE, &mgmt->resLock);

D
dapan1121 已提交
108 109 110
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
111 112
static int32_t qwAddScheduler(int32_t rwType, SQWorkerMgmt *mgmt, uint64_t sId, SQWSchStatus **sch) {
  SQWSchStatus newSch = {0};
D
dapan1121 已提交
113 114 115 116 117 118 119 120
  newSch.tasksHash = taosHashInit(mgmt->cfg.maxSchTaskNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
  if (NULL == newSch.tasksHash) {
    qError("taosHashInit %d failed", mgmt->cfg.maxSchTaskNum);
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
  }

  while (true) {
    QW_LOCK(QW_WRITE, &mgmt->schLock);
D
dapan1121 已提交
121
    int32_t code = taosHashPut(mgmt->schHash, &sId, sizeof(sId), &newSch, sizeof(newSch));
D
dapan1121 已提交
122 123 124
    if (0 != code) {
      if (!HASH_NODE_EXIST(code)) {
        QW_UNLOCK(QW_WRITE, &mgmt->schLock);
D
dapan1121 已提交
125
        qError("taosHashPut sId[%"PRIx64"] to scheduleHash failed", sId);
D
dapan1121 已提交
126 127 128 129 130 131
        taosHashCleanup(newSch.tasksHash);
        return TSDB_CODE_QRY_APP_ERROR;
      }
    }
    
    QW_UNLOCK(QW_WRITE, &mgmt->schLock);
D
dapan1121 已提交
132
    if (TSDB_CODE_SUCCESS == qwAcquireScheduler(rwType, mgmt, sId, sch, QW_NOT_EXIST_ADD)) {
D
dapan1121 已提交
133 134 135
      return TSDB_CODE_SUCCESS;
    }
  }
D
dapan1121 已提交
136 137 138 139

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
140

D
dapan1121 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
static int32_t qwAcquireScheduler(int32_t rwType, SQWorkerMgmt *mgmt, uint64_t sId, SQWSchStatus **sch, int32_t nOpt) {
  QW_LOCK(rwType, &mgmt->schLock);
  *sch = taosHashGet(mgmt->schHash, &sId, sizeof(sId));
  if (NULL == (*sch)) {
    QW_UNLOCK(rwType, &mgmt->schLock);
    
    if (QW_NOT_EXIST_ADD == nOpt) {
      return qwAddScheduler(rwType, mgmt, sId, sch);
    } else if (QW_NOT_EXIST_RET_ERR == nOpt) {
      return TSDB_CODE_QRY_SCH_NOT_EXIST;
    } else {
      assert(0);
    }
  }

  return TSDB_CODE_SUCCESS;
}



D
dapan1121 已提交
161 162 163 164
static FORCE_INLINE void qwReleaseScheduler(int32_t rwType, SQWorkerMgmt *mgmt) {
  QW_UNLOCK(rwType, &mgmt->schLock);
}

D
dapan1121 已提交
165 166 167
static int32_t qwAcquireTaskImpl(int32_t rwType, SQWSchStatus *sch, uint64_t qId, uint64_t tId, SQWTaskStatus **task) {
  char id[sizeof(qId) + sizeof(tId)] = {0};
  QW_SET_QTID(id, qId, tId);
D
dapan1121 已提交
168 169 170 171 172

  QW_LOCK(rwType, &sch->tasksLock);
  *task = taosHashGet(sch->tasksHash, id, sizeof(id));
  if (NULL == (*task)) {
    QW_UNLOCK(rwType, &sch->tasksLock);
D
dapan1121 已提交
173

D
dapan1121 已提交
174
    return TSDB_CODE_QRY_TASK_NOT_EXIST;
D
dapan1121 已提交
175 176
  }

D
dapan1121 已提交
177 178
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
179

D
dapan1121 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
static int32_t qwAcquireTask(int32_t rwType, SQWSchStatus *sch, uint64_t qId, uint64_t tId, SQWTaskStatus **task) {
  return qwAcquireTaskImpl(rwType, sch, qId, tId, task);
}


static FORCE_INLINE void qwReleaseTask(int32_t rwType, SQWSchStatus *sch) {
  QW_UNLOCK(rwType, &sch->tasksLock);
}


int32_t qwAddTaskToSch(int32_t rwType, SQWSchStatus *sch, uint64_t qId, uint64_t tId, int8_t status, int32_t eOpt, SQWTaskStatus **task) {
  int32_t code = 0;

  char id[sizeof(qId) + sizeof(tId)] = {0};
  QW_SET_QTID(id, qId, tId);

  SQWTaskStatus ntask = {0};
  ntask.status = status;
D
dapan1121 已提交
198 199 200

  while (true) {
    QW_LOCK(QW_WRITE, &sch->tasksLock);
D
dapan1121 已提交
201
    int32_t code = taosHashPut(sch->tasksHash, id, sizeof(id), &ntask, sizeof(ntask));
D
dapan1121 已提交
202 203 204
    if (0 != code) {
      QW_UNLOCK(QW_WRITE, &sch->tasksLock);
      if (HASH_NODE_EXIST(code)) {
D
dapan1121 已提交
205 206 207 208 209 210 211 212
        if (QW_EXIST_ACQUIRE == eOpt && rwType && task) {
          if (qwAcquireTask(rwType, sch, qId, tId, task)) {
            continue;
          }
        } else if (QW_EXIST_RET_ERR == eOpt) {
          return TSDB_CODE_QRY_TASK_ALREADY_EXIST;
        } else {
          assert(0);
D
dapan1121 已提交
213 214 215 216
        }

        break;
      } else {
D
dapan1121 已提交
217
        qError("taosHashPut queryId[%"PRIx64"] taskId[%"PRIx64"] to scheduleHash failed", qId, tId);
D
dapan1121 已提交
218 219 220
        return TSDB_CODE_QRY_APP_ERROR;
      }
    }
D
dapan1121 已提交
221
    
D
dapan1121 已提交
222 223
    QW_UNLOCK(QW_WRITE, &sch->tasksLock);

D
dapan1121 已提交
224 225 226 227 228 229
    if (rwType && task) {
      if (TSDB_CODE_SUCCESS == qwAcquireTask(rwType, sch, qId, tId, task)) {
        return TSDB_CODE_SUCCESS;
      }
    } else {
      break;
D
dapan1121 已提交
230
    }
D
dapan1121 已提交
231
  }  
D
dapan1121 已提交
232 233 234 235 236

  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
static int32_t qwAddTask(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t qId, uint64_t tId, int32_t status, int32_t eOpt, SQWSchStatus **sch, SQWTaskStatus **task) {
  SQWSchStatus *tsch = NULL;
  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, sId, &tsch, QW_NOT_EXIST_ADD));

  int32_t code = qwAddTaskToSch(QW_READ, tsch, qId, tId, status, eOpt, task);
  if (code) {
    qwReleaseScheduler(QW_WRITE, mgmt);
  }

  if (NULL == task) {
    qwReleaseScheduler(QW_READ, mgmt);
  } else if (sch) {
    *sch = tsch;
  }

  QW_RET(code);
D
dapan1121 已提交
253 254
}

D
dapan1121 已提交
255 256


D
dapan1121 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
static FORCE_INLINE int32_t qwAcquireTaskResCache(int32_t rwType, SQWorkerMgmt *mgmt, uint64_t queryId, uint64_t taskId, SQWorkerResCache **res) {
  char id[sizeof(queryId) + sizeof(taskId)] = {0};
  QW_SET_QTID(id, queryId, taskId);
  
  QW_LOCK(rwType, &mgmt->resLock);
  *res = taosHashGet(mgmt->resHash, id, sizeof(id));
  if (NULL == (*res)) {
    QW_UNLOCK(rwType, &mgmt->resLock);
    return TSDB_CODE_QRY_RES_CACHE_NOT_EXIST;
  }

  return TSDB_CODE_SUCCESS;
}

static FORCE_INLINE void qwReleaseTaskResCache(int32_t rwType, SQWorkerMgmt *mgmt) {
  QW_UNLOCK(rwType, &mgmt->resLock);
}


D
dapan1121 已提交
276 277
int32_t qwGetSchTasksStatus(SQWorkerMgmt *mgmt, uint64_t sId, SSchedulerStatusRsp **rsp) {
  SQWSchStatus *sch = NULL;
D
dapan1121 已提交
278 279
  int32_t taskNum = 0;

D
dapan1121 已提交
280 281 282
  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, sId, &sch, QW_NOT_EXIST_RET_ERR));
  
  sch->lastAccessTs = taosGetTimestampSec();
D
dapan1121 已提交
283

D
dapan1121 已提交
284 285 286
  QW_LOCK(QW_READ, &sch->tasksLock);
  
  taskNum = taosHashGetSize(sch->tasksHash);
D
dapan1121 已提交
287
  
D
dapan1121 已提交
288 289 290 291
  int32_t size = sizeof(SSchedulerStatusRsp) + sizeof((*rsp)->status[0]) * taskNum;
  *rsp = calloc(1, size);
  if (NULL == *rsp) {
    qError("calloc %d failed", size);
D
dapan1121 已提交
292 293
    QW_UNLOCK(QW_READ, &sch->tasksLock);
    qwReleaseScheduler(QW_READ, mgmt);
D
dapan1121 已提交
294
    
D
dapan1121 已提交
295 296 297 298 299
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
  }

  void *key = NULL;
  size_t keyLen = 0;
D
dapan1121 已提交
300
  int32_t i = 0;
D
dapan1121 已提交
301

D
dapan1121 已提交
302 303 304 305
  void *pIter = taosHashIterate(sch->tasksHash, NULL);
  while (pIter) {
    SQWTaskStatus *taskStatus = (SQWTaskStatus *)pIter;
    taosHashGetKey(pIter, &key, &keyLen);
D
dapan1121 已提交
306

D
dapan1121 已提交
307 308 309 310 311
    QW_GET_QTID(key, (*rsp)->status[i].queryId, (*rsp)->status[i].taskId);
    (*rsp)->status[i].status = taskStatus->status;
    
    pIter = taosHashIterate(sch->tasksHash, pIter);
  }  
D
dapan1121 已提交
312

D
dapan1121 已提交
313 314
  QW_UNLOCK(QW_READ, &sch->tasksLock);
  qwReleaseScheduler(QW_READ, mgmt);
D
dapan1121 已提交
315 316 317 318 319 320

  (*rsp)->num = taskNum;

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
321 322


D
dapan1121 已提交
323 324
int32_t qwUpdateSchLastAccess(SQWorkerMgmt *mgmt, uint64_t sId) {
  SQWSchStatus *sch = NULL;
D
dapan1121 已提交
325

D
dapan1121 已提交
326
  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, sId, &sch, QW_NOT_EXIST_RET_ERR));
D
dapan1121 已提交
327

D
dapan1121 已提交
328
  sch->lastAccessTs = taosGetTimestampSec();
D
dapan1121 已提交
329 330 331 332 333 334

  qwReleaseScheduler(QW_READ, mgmt);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
335 336 337
int32_t qwUpdateTaskStatus(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t qId, uint64_t tId, int8_t status) {
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
338 339
  int32_t code = 0;

D
dapan1121 已提交
340
  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, sId, &sch, QW_NOT_EXIST_RET_ERR));
D
dapan1121 已提交
341

D
dapan1121 已提交
342
  QW_ERR_JRET(qwAcquireTask(QW_READ, sch, qId, tId, &task));
D
dapan1121 已提交
343

D
dapan1121 已提交
344 345 346 347
  QW_LOCK(QW_WRITE, &task->lock);
  qwUpdateTaskInfo(task, QW_TASK_INFO_STATUS, &status);
  QW_UNLOCK(QW_WRITE, &task->lock);
  
D
dapan1121 已提交
348 349
_return:

D
dapan1121 已提交
350 351
  qwReleaseTask(QW_READ, sch);
  qwReleaseScheduler(QW_READ, mgmt);
D
dapan1121 已提交
352 353 354 355 356

  QW_RET(code);
}


D
dapan1121 已提交
357 358 359
int32_t qwGetTaskStatus(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t queryId, uint64_t taskId, int8_t *taskStatus) {
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
360 361
  int32_t code = 0;
  
D
dapan1121 已提交
362 363 364
  if (qwAcquireScheduler(QW_READ, mgmt, sId, &sch, QW_NOT_EXIST_RET_ERR)) {
    *taskStatus = JOB_TASK_STATUS_NULL;
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
365 366 367
  }

  if (qwAcquireTask(QW_READ, sch, queryId, taskId, &task)) {
D
dapan1121 已提交
368
    qwReleaseScheduler(QW_READ, mgmt);
D
dapan1121 已提交
369
    
D
dapan1121 已提交
370 371
    *taskStatus = JOB_TASK_STATUS_NULL;
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
372 373
  }

D
dapan1121 已提交
374
  *taskStatus = task->status;
D
dapan1121 已提交
375 376

  qwReleaseTask(QW_READ, sch);
D
dapan1121 已提交
377 378
  qwReleaseScheduler(QW_READ, mgmt);

D
dapan1121 已提交
379 380 381 382
  QW_RET(code);
}


D
dapan1121 已提交
383 384 385
int32_t qwCancelTask(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t queryId, uint64_t taskId) {
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
386 387
  int32_t code = 0;

D
dapan1121 已提交
388
  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, sId, &sch, QW_NOT_EXIST_ADD));
D
dapan1121 已提交
389 390

  if (qwAcquireTask(QW_READ, sch, queryId, taskId, &task)) {
D
dapan1121 已提交
391 392 393
    qwReleaseScheduler(QW_READ, mgmt);
    
    code = qwAddTask(mgmt, sId, queryId, taskId, JOB_TASK_STATUS_NOT_START, QW_EXIST_ACQUIRE, &sch, &task);
D
dapan1121 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
    if (code) {
      qwReleaseScheduler(QW_READ, mgmt);
      QW_ERR_RET(code);
    }
  }

  QW_LOCK(QW_WRITE, &task->lock);

  task->cancel = true;
  
  int8_t oriStatus = task->status;
  int8_t newStatus = 0;
  
  if (task->status == JOB_TASK_STATUS_CANCELLED || task->status == JOB_TASK_STATUS_NOT_START || task->status == JOB_TASK_STATUS_CANCELLING || task->status == JOB_TASK_STATUS_DROPPING) {
    QW_UNLOCK(QW_WRITE, &task->lock);

    qwReleaseTask(QW_READ, sch);
    qwReleaseScheduler(QW_READ, mgmt);
    
    return TSDB_CODE_SUCCESS;
  } else if (task->status == JOB_TASK_STATUS_FAILED || task->status == JOB_TASK_STATUS_SUCCEED || task->status == JOB_TASK_STATUS_PARTIAL_SUCCEED) {
    newStatus = JOB_TASK_STATUS_CANCELLED;
    QW_ERR_JRET(qwUpdateTaskInfo(task, QW_TASK_INFO_STATUS, &newStatus));
  } else {
    newStatus = JOB_TASK_STATUS_CANCELLING;
    QW_ERR_JRET(qwUpdateTaskInfo(task, QW_TASK_INFO_STATUS, &newStatus));
  }

  QW_UNLOCK(QW_WRITE, &task->lock);
D
dapan1121 已提交
423
  
D
dapan1121 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
  qwReleaseTask(QW_READ, sch);
  qwReleaseScheduler(QW_READ, mgmt);

  if (oriStatus == JOB_TASK_STATUS_EXECUTING) {
    //TODO call executer to cancel subquery async
  }
  
  return TSDB_CODE_SUCCESS;

_return:

  if (task) {
    QW_UNLOCK(QW_WRITE, &task->lock);
    
    qwReleaseTask(QW_READ, sch);
  }

  if (sch) {
    qwReleaseScheduler(QW_READ, mgmt);
  }

  QW_RET(code);
}



D
dapan1121 已提交
450 451 452
int32_t qwDropTask(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t queryId, uint64_t taskId) {
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
453 454 455 456 457 458 459 460 461 462
  int32_t code = 0;
  char id[sizeof(queryId) + sizeof(taskId)] = {0};
  QW_SET_QTID(id, queryId, taskId);

  QW_LOCK(QW_WRITE, &mgmt->resLock);
  if (mgmt->resHash) {
    taosHashRemove(mgmt->resHash, id, sizeof(id));
  }
  QW_UNLOCK(QW_WRITE, &mgmt->resLock);
  
D
dapan1121 已提交
463 464
  if (TSDB_CODE_SUCCESS != qwAcquireScheduler(QW_WRITE, mgmt, sId, &sch, QW_NOT_EXIST_RET_ERR)) {
    qWarn("scheduler %"PRIx64" doesn't exist", sId);
D
dapan1121 已提交
465 466 467 468 469 470
    return TSDB_CODE_SUCCESS;
  }

  if (qwAcquireTask(QW_WRITE, sch, queryId, taskId, &task)) {
    qwReleaseScheduler(QW_WRITE, mgmt);
    
D
dapan1121 已提交
471
    qWarn("scheduler %"PRIx64" queryId %"PRIx64" taskId:%"PRIx64" doesn't exist", sId, queryId, taskId);
D
dapan1121 已提交
472 473 474 475 476 477 478 479 480 481 482 483
    return TSDB_CODE_SUCCESS;
  }

  taosHashRemove(sch->tasksHash, id, sizeof(id));

  qwReleaseTask(QW_WRITE, sch);
  qwReleaseScheduler(QW_WRITE, mgmt);
  
  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
484 485 486
int32_t qwCancelDropTask(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t queryId, uint64_t taskId) {
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
487 488
  int32_t code = 0;

D
dapan1121 已提交
489
  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, sId, &sch, QW_NOT_EXIST_ADD));
D
dapan1121 已提交
490 491 492 493

  if (qwAcquireTask(QW_READ, sch, queryId, taskId, &task)) {
    qwReleaseScheduler(QW_READ, mgmt);
    
D
dapan1121 已提交
494 495 496 497 498
    code = qwAddTask(mgmt, sId, queryId, taskId, JOB_TASK_STATUS_NOT_START, QW_EXIST_ACQUIRE, &sch, &task);
    if (code) {
      qwReleaseScheduler(QW_READ, mgmt);
      QW_ERR_RET(code);
    }
D
dapan1121 已提交
499 500 501 502 503 504 505 506 507 508
  }

  QW_LOCK(QW_WRITE, &task->lock);

  task->drop = true;

  int8_t oriStatus = task->status;
  int8_t newStatus = 0;
  
  if (task->status == JOB_TASK_STATUS_EXECUTING) {
D
dapan1121 已提交
509
    newStatus = JOB_TASK_STATUS_DROPPING;
D
dapan1121 已提交
510 511 512 513 514 515 516 517 518 519 520 521
    QW_ERR_JRET(qwUpdateTaskInfo(task, QW_TASK_INFO_STATUS, &newStatus));
  } else if (task->status == JOB_TASK_STATUS_CANCELLING || task->status == JOB_TASK_STATUS_DROPPING || task->status == JOB_TASK_STATUS_NOT_START) {    
    QW_UNLOCK(QW_WRITE, &task->lock);
    qwReleaseTask(QW_READ, sch);
    qwReleaseScheduler(QW_READ, mgmt);
    
    return TSDB_CODE_SUCCESS;
  } else {
    QW_UNLOCK(QW_WRITE, &task->lock);
    qwReleaseTask(QW_READ, sch);
    qwReleaseScheduler(QW_READ, mgmt);
  
D
dapan1121 已提交
522
    QW_ERR_RET(qwDropTask(mgmt, sId, queryId, taskId));
D
dapan1121 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
    return TSDB_CODE_SUCCESS;
  }

  QW_UNLOCK(QW_WRITE, &task->lock);
  qwReleaseTask(QW_READ, sch);
  qwReleaseScheduler(QW_READ, mgmt);

  if (oriStatus == JOB_TASK_STATUS_EXECUTING) {
    //TODO call executer to cancel subquery async
  }
  
  return TSDB_CODE_SUCCESS;

_return:

  if (task) {
    QW_UNLOCK(QW_WRITE, &task->lock);
    
    qwReleaseTask(QW_READ, sch);
  }

  if (sch) {
    qwReleaseScheduler(QW_READ, mgmt);
  }

  QW_RET(code);
}



int32_t qwBuildAndSendQueryRsp(SRpcMsg *pMsg, int32_t code) {
D
dapan1121 已提交
554 555
  SQueryTableRsp *pRsp = (SQueryTableRsp *)rpcMallocCont(sizeof(SQueryTableRsp));
  pRsp->code = code;
D
dapan1121 已提交
556

D
dapan1121 已提交
557 558
  SRpcMsg rpcRsp = {
    .handle  = pMsg->handle,
D
dapan1121 已提交
559
    .ahandle = pMsg->ahandle,
D
dapan1121 已提交
560 561 562 563 564 565 566 567
    .pCont   = pRsp,
    .contLen = sizeof(*pRsp),
    .code    = code,
  };

  rpcSendResponse(&rpcRsp);

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
568 569 570
}

int32_t qwBuildAndSendReadyRsp(SRpcMsg *pMsg, int32_t code) {
D
dapan1121 已提交
571 572 573 574 575 576 577 578 579 580 581 582
  SResReadyRsp *pRsp = (SResReadyRsp *)rpcMallocCont(sizeof(SResReadyRsp));
  pRsp->code = code;

  SRpcMsg rpcRsp = {
    .handle  = pMsg->handle,
    .ahandle = pMsg->ahandle,
    .pCont   = pRsp,
    .contLen = sizeof(*pRsp),
    .code    = code,
  };

  rpcSendResponse(&rpcRsp);
D
dapan1121 已提交
583

D
dapan1121 已提交
584
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
585 586 587
}

int32_t qwBuildAndSendStatusRsp(SRpcMsg *pMsg, SSchedulerStatusRsp *sStatus) {
D
dapan1121 已提交
588 589 590 591 592 593 594 595 596
  int32_t size = 0;
  
  if (sStatus) {
    size = sizeof(SSchedulerStatusRsp) + sizeof(sStatus->status[0]) * sStatus->num;
  } else {
    size = sizeof(SSchedulerStatusRsp);
  }
  
  SSchedulerStatusRsp *pRsp = (SSchedulerStatusRsp *)rpcMallocCont(size);
D
dapan1121 已提交
597

D
dapan1121 已提交
598 599 600 601 602 603 604
  if (sStatus) {
    memcpy(pRsp, sStatus, size);
  } else {
    pRsp->num = 0;
  }

  SRpcMsg rpcRsp = {
D
dapan1121 已提交
605
    .msgType = pMsg->msgType + 1,
D
dapan1121 已提交
606 607 608 609 610 611 612 613 614 615
    .handle  = pMsg->handle,
    .ahandle = pMsg->ahandle,
    .pCont   = pRsp,
    .contLen = size,
    .code    = 0,
  };

  rpcSendResponse(&rpcRsp);

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
616 617 618
}

int32_t qwBuildAndSendFetchRsp(SRpcMsg *pMsg, void *data) {
D
dapan1121 已提交
619 620 621 622 623 624 625 626 627 628 629 630 631
  SRetrieveTableRsp *pRsp = (SRetrieveTableRsp *)rpcMallocCont(sizeof(SRetrieveTableRsp));
  memset(pRsp, 0, sizeof(SRetrieveTableRsp));

  //TODO fill msg
  pRsp->completed = true;

  SRpcMsg rpcRsp = {
    .handle  = pMsg->handle,
    .ahandle = pMsg->ahandle,
    .pCont   = pRsp,
    .contLen = sizeof(*pRsp),
    .code    = 0,
  };
D
dapan1121 已提交
632

D
dapan1121 已提交
633 634 635
  rpcSendResponse(&rpcRsp);

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
636 637
}

D
dapan1121 已提交
638 639 640
int32_t qwBuildAndSendCancelRsp(SRpcMsg *pMsg, int32_t code) {
  STaskCancelRsp *pRsp = (STaskCancelRsp *)rpcMallocCont(sizeof(STaskCancelRsp));
  pRsp->code = code;
D
dapan1121 已提交
641

D
dapan1121 已提交
642 643 644 645 646 647 648 649 650 651 652
  SRpcMsg rpcRsp = {
    .handle  = pMsg->handle,
    .ahandle = pMsg->ahandle,
    .pCont   = pRsp,
    .contLen = sizeof(*pRsp),
    .code    = code,
  };

  rpcSendResponse(&rpcRsp);

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
653 654
}

D
dapan1121 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667
int32_t qwBuildAndSendDropRsp(SRpcMsg *pMsg, int32_t code) {
  STaskDropRsp *pRsp = (STaskDropRsp *)rpcMallocCont(sizeof(STaskDropRsp));
  pRsp->code = code;

  SRpcMsg rpcRsp = {
    .handle  = pMsg->handle,
    .ahandle = pMsg->ahandle,
    .pCont   = pRsp,
    .contLen = sizeof(*pRsp),
    .code    = code,
  };

  rpcSendResponse(&rpcRsp);
H
Haojun Liao 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
  return TSDB_CODE_SUCCESS;
}

int32_t qwBuildAndSendShowRsp(SRpcMsg *pMsg, int32_t code) {
  int32_t numOfCols = 6;
  SVShowTablesRsp *pRsp = (SVShowTablesRsp *)rpcMallocCont(sizeof(SVShowTablesRsp) + sizeof(SSchema) * numOfCols);

  int32_t  cols = 0;
  SSchema *pSchema = pRsp->metaInfo.pSchema;

  const SSchema *s = tGetTbnameColumnSchema();
  *pSchema = createSchema(s->type, htonl(s->bytes), htonl(cols++), "name");
  pSchema++;

  int32_t type = TSDB_DATA_TYPE_TIMESTAMP;
  *pSchema = createSchema(type, htonl(tDataTypes[type].bytes), htonl(cols++), "created");
  pSchema++;

  type = TSDB_DATA_TYPE_SMALLINT;
  *pSchema = createSchema(type, htonl(tDataTypes[type].bytes), htonl(cols++), "columns");
  pSchema++;

  *pSchema = createSchema(s->type, htonl(s->bytes), htonl(cols++), "stable");
  pSchema++;

  type = TSDB_DATA_TYPE_BIGINT;
  *pSchema = createSchema(type, htonl(tDataTypes[type].bytes), htonl(cols++), "uid");
  pSchema++;

  type = TSDB_DATA_TYPE_INT;
  *pSchema = createSchema(type, htonl(tDataTypes[type].bytes), htonl(cols++), "vgId");

  pRsp->metaInfo.numOfColumns = htonl(cols);

  SRpcMsg rpcMsg = {
      .handle  = pMsg->handle,
      .ahandle = pMsg->ahandle,
      .pCont   = pRsp,
      .contLen = sizeof(*pRsp),
      .code    = code,
  };
D
dapan1121 已提交
709

H
Haojun Liao 已提交
710
  rpcSendResponse(&rpcMsg);
D
dapan1121 已提交
711
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
712 713
}

H
Haojun Liao 已提交
714 715 716 717 718 719 720 721 722 723 724 725
int32_t qwBuildAndSendShowFetchRsp(SRpcMsg *pMsg, SVShowTablesFetchReq* pFetchReq) {
  SVShowTablesFetchRsp *pRsp = (SVShowTablesFetchRsp *)rpcMallocCont(sizeof(SVShowTablesFetchRsp));
  int32_t handle = htonl(pFetchReq->id);

  pRsp->numOfRows = 0;
  SRpcMsg rpcMsg = {
      .handle  = pMsg->handle,
      .ahandle = pMsg->ahandle,
      .pCont   = pRsp,
      .contLen = sizeof(*pRsp),
      .code    = 0,
  };
D
dapan1121 已提交
726

H
Haojun Liao 已提交
727 728 729
  rpcSendResponse(&rpcMsg);
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
730

D
dapan1121 已提交
731 732 733
int32_t qwCheckAndSendReadyRsp(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t queryId, uint64_t taskId, SRpcMsg *pMsg, int32_t rspCode) {
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
734 735
  int32_t code = 0;

D
dapan1121 已提交
736
  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, sId, &sch, QW_NOT_EXIST_RET_ERR));
D
dapan1121 已提交
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764

  QW_ERR_JRET(qwAcquireTask(QW_READ, sch, queryId, taskId, &task));

  QW_LOCK(QW_WRITE, &task->lock);

  if (QW_READY_NOT_RECEIVED == task->ready) {
    QW_UNLOCK(QW_WRITE, &task->lock);

    qwReleaseTask(QW_READ, sch);
    qwReleaseScheduler(QW_READ, mgmt);
    
    return TSDB_CODE_SUCCESS;
  } else if (QW_READY_RECEIVED == task->ready) {
    QW_ERR_JRET(qwBuildAndSendReadyRsp(pMsg, rspCode));

    task->ready = QW_READY_RESPONSED;
  } else if (QW_READY_RESPONSED == task->ready) {
    qError("query response already send");
    QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
  } else {
    assert(0);
  }

_return:

  if (task) {
    QW_UNLOCK(QW_WRITE, &task->lock);
    qwReleaseTask(QW_READ, sch);
D
dapan1121 已提交
765

D
dapan1121 已提交
766 767 768 769 770 771 772
  }

  qwReleaseScheduler(QW_READ, mgmt);

  QW_RET(code);
}

D
dapan1121 已提交
773 774 775
int32_t qwSetAndSendReadyRsp(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t queryId, uint64_t taskId, SRpcMsg *pMsg) {
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
776 777
  int32_t code = 0;

D
dapan1121 已提交
778
  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, sId, &sch, QW_NOT_EXIST_RET_ERR));
D
dapan1121 已提交
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808

  QW_ERR_JRET(qwAcquireTask(QW_READ, sch, queryId, taskId, &task));

  QW_LOCK(QW_WRITE, &task->lock);
  if (QW_TASK_READY_RESP(task->status)) {
    QW_ERR_JRET(qwBuildAndSendReadyRsp(pMsg, task->code));

    task->ready = QW_READY_RESPONSED;
  } else {
    task->ready = QW_READY_RECEIVED;
    QW_UNLOCK(QW_WRITE, &task->lock);

    qwReleaseTask(QW_READ, sch);
    qwReleaseScheduler(QW_READ, mgmt);
    
    return TSDB_CODE_SUCCESS;
  }

_return:

  if (task) {
    QW_UNLOCK(QW_WRITE, &task->lock);
    qwReleaseTask(QW_READ, sch);
  }

  qwReleaseScheduler(QW_READ, mgmt);

  QW_RET(code);
}

D
dapan1121 已提交
809 810 811
int32_t qwCheckTaskCancelDrop( SQWorkerMgmt *mgmt, uint64_t sId, uint64_t queryId, uint64_t taskId, bool *needStop) {
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
812 813 814 815 816
  int32_t code = 0;
  int8_t status = JOB_TASK_STATUS_CANCELLED;

  *needStop = false;

D
dapan1121 已提交
817
  if (qwAcquireScheduler(QW_READ, mgmt, sId, &sch, QW_NOT_EXIST_RET_ERR)) {
D
dapan1121 已提交
818 819 820 821 822 823 824 825 826 827 828
    return TSDB_CODE_SUCCESS;
  }

  if (qwAcquireTask(QW_READ, sch, queryId, taskId, &task)) {
    qwReleaseScheduler(QW_READ, mgmt);
    return TSDB_CODE_SUCCESS;
  }

  QW_LOCK(QW_READ, &task->lock);
  
  if ((!task->cancel) && (!task->drop)) {
D
dapan1121 已提交
829 830
    qError("no cancel or drop, but task:%"PRIx64" exists", taskId);
    
D
dapan1121 已提交
831 832 833 834
    QW_UNLOCK(QW_READ, &task->lock);
    qwReleaseTask(QW_READ, sch);
    qwReleaseScheduler(QW_READ, mgmt);

D
dapan1121 已提交
835
    QW_RET(TSDB_CODE_QRY_APP_ERROR);
D
dapan1121 已提交
836 837 838 839 840 841 842 843 844 845
  }

  QW_UNLOCK(QW_READ, &task->lock);

  *needStop = true;
  
  if (task->cancel) {
    QW_LOCK(QW_WRITE, &task->lock);
    qwUpdateTaskInfo(task, QW_TASK_INFO_STATUS, &status);
    QW_UNLOCK(QW_WRITE, &task->lock);
D
dapan1121 已提交
846 847 848
  }

  if (task->drop) {
D
dapan1121 已提交
849 850 851
    qwReleaseTask(QW_READ, sch);
    qwReleaseScheduler(QW_READ, mgmt);
    
D
dapan1121 已提交
852
    return qwDropTask(mgmt, sId, queryId, taskId);
D
dapan1121 已提交
853 854
  }

D
dapan1121 已提交
855 856 857
  qwReleaseTask(QW_READ, sch);
  qwReleaseScheduler(QW_READ, mgmt);

D
dapan1121 已提交
858 859 860
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
861 862 863
int32_t qwHandleFetch(SQWorkerResCache *res, SQWorkerMgmt *mgmt, uint64_t sId, uint64_t queryId, uint64_t taskId, SRpcMsg *pMsg) {
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
864
  int32_t code = 0;
D
dapan1121 已提交
865 866
  int32_t needRsp = true;
  void *data = NULL;
D
dapan1121 已提交
867

D
dapan1121 已提交
868
  QW_ERR_JRET(qwAcquireScheduler(QW_READ, mgmt, sId, &sch, QW_NOT_EXIST_RET_ERR));
D
dapan1121 已提交
869 870 871 872
  QW_ERR_JRET(qwAcquireTask(QW_READ, sch, queryId, taskId, &task));

  QW_LOCK(QW_READ, &task->lock);

D
dapan1121 已提交
873 874 875 876 877 878
  if (task->cancel || task->drop) {
    qError("task is already cancelled or dropped");
    QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
  }

  if (task->status != JOB_TASK_STATUS_EXECUTING && task->status != JOB_TASK_STATUS_PARTIAL_SUCCEED) {
D
dapan1121 已提交
879 880 881 882 883
    qError("invalid status %d for fetch", task->status);
    QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
  }
  
  if (QW_GOT_RES_DATA(res->data)) {
D
dapan1121 已提交
884
    data = res->data;
D
dapan1121 已提交
885 886 887 888 889 890 891 892 893 894 895 896
    if (QW_LOW_RES_DATA(res->data)) {
      if (task->status == JOB_TASK_STATUS_PARTIAL_SUCCEED) {
        //TODO add query back to queue
      }
    }
  } else {
    if (task->status != JOB_TASK_STATUS_EXECUTING) {
      qError("invalid status %d for fetch without res", task->status);
      QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
    }
    
    //TODO SET FLAG FOR QUERY TO SEND RSP WHEN RES READY
D
dapan1121 已提交
897 898

    needRsp = false;
D
dapan1121 已提交
899 900 901 902 903
  }

_return:
  if (task) {
    QW_UNLOCK(QW_READ, &task->lock);
D
dapan1121 已提交
904
    qwReleaseTask(QW_READ, sch);    
D
dapan1121 已提交
905 906 907
  }
  
  if (sch) {
D
dapan1121 已提交
908
    qwReleaseScheduler(QW_READ, mgmt);
D
dapan1121 已提交
909 910
  }

D
dapan1121 已提交
911 912 913
  if (needRsp) {
    qwBuildAndSendFetchRsp(pMsg, res->data);
  }
D
dapan1121 已提交
914 915 916 917

  QW_RET(code);
}

D
dapan1121 已提交
918 919 920
int32_t qwQueryPostProcess(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t qId, uint64_t tId, int8_t status, int32_t errCode) {
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
921 922 923
  int32_t code = 0;
  int8_t newStatus = JOB_TASK_STATUS_CANCELLED;

D
dapan1121 已提交
924
  code = qwAcquireScheduler(QW_READ, mgmt, sId, &sch, QW_NOT_EXIST_ADD);
D
dapan1121 已提交
925
  if (code) {
D
dapan1121 已提交
926
    qError("sId:%"PRIx64" not in cache", sId);
D
dapan1121 已提交
927 928 929
    QW_ERR_RET(code);
  }

D
dapan1121 已提交
930
  code = qwAcquireTask(QW_READ, sch, qId, tId, &task);
D
dapan1121 已提交
931 932
  if (code) {
    qwReleaseScheduler(QW_READ, mgmt);
D
dapan1121 已提交
933 934 935 936 937 938 939
    
    if (JOB_TASK_STATUS_PARTIAL_SUCCEED == status || JOB_TASK_STATUS_SUCCEED == status) {
      qError("sId:%"PRIx64" queryId:%"PRIx64" taskId:%"PRIx64" not in cache", sId, qId, tId);
      QW_ERR_RET(code);
    }

    QW_ERR_RET(qwAddTask(mgmt, sId, qId, tId, status, QW_EXIST_ACQUIRE, &sch, &task));
D
dapan1121 已提交
940 941 942 943 944 945
  }

  if (task->cancel) {
    QW_LOCK(QW_WRITE, &task->lock);
    qwUpdateTaskInfo(task, QW_TASK_INFO_STATUS, &newStatus);
    QW_UNLOCK(QW_WRITE, &task->lock);
D
dapan1121 已提交
946 947 948
  }

  if (task->drop) {
D
dapan1121 已提交
949 950 951
    qwReleaseTask(QW_READ, sch);
    qwReleaseScheduler(QW_READ, mgmt);
    
D
dapan1121 已提交
952
    qwDropTask(mgmt, sId, qId, tId);
D
dapan1121 已提交
953 954

    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
955 956 957
  }

  if (!(task->cancel || task->drop)) {
D
dapan1121 已提交
958 959 960 961 962 963 964 965 966 967 968
    QW_LOCK(QW_WRITE, &task->lock);
    qwUpdateTaskInfo(task, QW_TASK_INFO_STATUS, &status);
    task->code = errCode;
    QW_UNLOCK(QW_WRITE, &task->lock);
  }
  
  qwReleaseTask(QW_READ, sch);
  qwReleaseScheduler(QW_READ, mgmt);

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
969 970 971 972 973

int32_t qWorkerInit(SQWorkerCfg *cfg, void **qWorkerMgmt) {
  SQWorkerMgmt *mgmt = calloc(1, sizeof(SQWorkerMgmt));
  if (NULL == mgmt) {
    qError("calloc %d failed", (int32_t)sizeof(SQWorkerMgmt));
D
dapan1121 已提交
974
    QW_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
975 976 977 978 979 980 981 982 983 984
  }

  if (cfg) {
    mgmt->cfg = *cfg;
  } else {
    mgmt->cfg.maxSchedulerNum = QWORKER_DEFAULT_SCHEDULER_NUMBER;
    mgmt->cfg.maxResCacheNum = QWORKER_DEFAULT_RES_CACHE_NUMBER;
    mgmt->cfg.maxSchTaskNum = QWORKER_DEFAULT_SCH_TASK_NUMBER;
  }

D
dapan1121 已提交
985 986
  mgmt->schHash = taosHashInit(mgmt->cfg.maxSchedulerNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_NO_LOCK);
  if (NULL == mgmt->schHash) {
D
dapan1121 已提交
987 988 989 990
    tfree(mgmt);
    QW_ERR_LRET(TSDB_CODE_QRY_OUT_OF_MEMORY, "init %d schduler hash failed", mgmt->cfg.maxSchedulerNum);
  }

D
dapan1121 已提交
991
  mgmt->resHash = taosHashInit(mgmt->cfg.maxResCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
D
dapan1121 已提交
992
  if (NULL == mgmt->resHash) {
D
dapan1121 已提交
993 994
    taosHashCleanup(mgmt->schHash);
    mgmt->schHash = NULL;
D
dapan1121 已提交
995 996 997 998 999 1000 1001 1002 1003 1004
    tfree(mgmt);
    
    QW_ERR_LRET(TSDB_CODE_QRY_OUT_OF_MEMORY, "init %d res cache hash failed", mgmt->cfg.maxResCacheNum);
  }

  *qWorkerMgmt = mgmt;

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1005 1006
int32_t qWorkerProcessQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
1007
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
1008 1009
  }

D
dapan1121 已提交
1010
  int32_t code = 0;
D
dapan1121 已提交
1011 1012
  SSubQueryMsg *msg = pMsg->pCont;
  if (NULL == msg || pMsg->contLen <= sizeof(*msg)) {
D
dapan1121 已提交
1013
    qError("invalid query msg");
D
dapan1121 已提交
1014
    QW_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
1015
  }
D
dapan 已提交
1016

D
dapan1121 已提交
1017
  msg->sId = htobe64(msg->sId);
D
dapan 已提交
1018 1019 1020
  msg->queryId = htobe64(msg->queryId);
  msg->taskId = htobe64(msg->taskId);
  msg->contentLen = ntohl(msg->contentLen);
D
dapan1121 已提交
1021 1022
  
  bool queryDone = false;
D
dapan1121 已提交
1023
  bool queryRsped = false;
D
dapan1121 已提交
1024
  bool needStop = false;
D
dapan1121 已提交
1025 1026
  SSubplan *plan = NULL;

D
dapan1121 已提交
1027
  QW_ERR_JRET(qwCheckTaskCancelDrop(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId, &needStop));
D
dapan1121 已提交
1028 1029
  if (needStop) {
    qWarn("task need stop");
D
dapan1121 已提交
1030
    QW_ERR_JRET(TSDB_CODE_QRY_TASK_CANCELLED);
D
dapan1121 已提交
1031 1032 1033
  }
  
  code = qStringToSubplan(msg->msg, &plan);
D
dapan1121 已提交
1034
  if (TSDB_CODE_SUCCESS != code) {
D
dapan1121 已提交
1035
    qError("schId:%"PRIx64",qId:%"PRIx64",taskId:%"PRIx64" string to subplan failed, code:%d", msg->sId, msg->queryId, msg->taskId, code);
D
dapan1121 已提交
1036
    QW_ERR_JRET(code);
D
dapan1121 已提交
1037 1038
  }

D
dapan1121 已提交
1039 1040 1041 1042 1043 1044 1045
  //TODO call executer to init subquery
  code = 0; // return error directly
  //TODO call executer to init subquery
  
  if (code) {
    QW_ERR_JRET(code);
  } else {
D
dapan1121 已提交
1046
    QW_ERR_JRET(qwAddTask(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId, JOB_TASK_STATUS_EXECUTING, QW_EXIST_RET_ERR, NULL, NULL));
D
dapan1121 已提交
1047
  }
D
dapan1121 已提交
1048

D
dapan1121 已提交
1049
  QW_ERR_JRET(qwBuildAndSendQueryRsp(pMsg, TSDB_CODE_SUCCESS));
D
dapan1121 已提交
1050

D
dapan1121 已提交
1051
  queryRsped = true;
D
dapan1121 已提交
1052
 
D
dapan1121 已提交
1053
  //TODO call executer to execute subquery
D
dapan1121 已提交
1054
  code = 0; 
D
dapan1121 已提交
1055
  void *data = NULL;
D
dapan1121 已提交
1056
  queryDone = false;
D
dapan1121 已提交
1057 1058
  //TODO call executer to execute subquery

D
dapan1121 已提交
1059 1060 1061
  if (code) {
    QW_ERR_JRET(code);
  } else {
D
dapan1121 已提交
1062
    QW_ERR_JRET(qwAddTaskResCache(qWorkerMgmt, msg->queryId, msg->taskId, data));
D
dapan1121 已提交
1063

D
dapan1121 已提交
1064 1065
    QW_ERR_JRET(qwUpdateTaskStatus(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId, JOB_TASK_STATUS_PARTIAL_SUCCEED));
  } 
D
dapan1121 已提交
1066 1067 1068

_return:

D
dapan1121 已提交
1069 1070
  if (queryRsped) {
    code = qwCheckAndSendReadyRsp(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId, pMsg, code);
D
dapan1121 已提交
1071 1072
  } else {
    code = qwBuildAndSendQueryRsp(pMsg, code);
D
dapan1121 已提交
1073
  }
D
dapan1121 已提交
1074 1075
  
  int8_t status = 0;
D
dapan1121 已提交
1076 1077 1078 1079 1080 1081
  if (TSDB_CODE_SUCCESS != code) {
    status = JOB_TASK_STATUS_FAILED;
  } else if (queryDone) {
    status = JOB_TASK_STATUS_SUCCEED;
  } else {
    status = JOB_TASK_STATUS_PARTIAL_SUCCEED;
D
dapan1121 已提交
1082
  }
D
dapan1121 已提交
1083 1084

  qwQueryPostProcess(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId, status, code);
D
dapan1121 已提交
1085
  
D
dapan1121 已提交
1086
  QW_RET(code);
D
dapan1121 已提交
1087 1088
}

D
dapan1121 已提交
1089 1090
int32_t qWorkerProcessReadyMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg){
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
1091 1092 1093
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

D
dapan1121 已提交
1094
  SResReadyMsg *msg = pMsg->pCont;
D
dapan1121 已提交
1095
  if (NULL == msg || pMsg->contLen < sizeof(*msg)) {
D
dapan1121 已提交
1096
    qError("invalid task status msg");  
D
dapan1121 已提交
1097 1098
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }  
D
dapan1121 已提交
1099

D
dapan1121 已提交
1100 1101 1102 1103 1104
  msg->sId = htobe64(msg->sId);
  msg->queryId = htobe64(msg->queryId);
  msg->taskId = htobe64(msg->taskId);

  QW_ERR_RET(qwSetAndSendReadyRsp(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId, pMsg));
D
dapan1121 已提交
1105 1106 1107 1108
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1109 1110
int32_t qWorkerProcessStatusMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
1111 1112 1113
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

D
dapan1121 已提交
1114
  int32_t code = 0;
D
dapan1121 已提交
1115
  SSchTasksStatusMsg *msg = pMsg->pCont;
D
dapan1121 已提交
1116
  if (NULL == msg || pMsg->contLen < sizeof(*msg)) {
D
dapan1121 已提交
1117
    qError("invalid task status msg");
D
dapan1121 已提交
1118 1119 1120
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }  

D
dapan1121 已提交
1121 1122
  msg->sId = htobe64(msg->sId);

D
dapan1121 已提交
1123 1124
  SSchedulerStatusRsp *sStatus = NULL;
  
D
dapan1121 已提交
1125
  QW_ERR_JRET(qwGetSchTasksStatus(qWorkerMgmt, msg->sId, &sStatus));
D
dapan1121 已提交
1126 1127

_return:
D
dapan1121 已提交
1128

D
dapan1121 已提交
1129 1130
  QW_ERR_RET(qwBuildAndSendStatusRsp(pMsg, sStatus));

D
dapan1121 已提交
1131 1132 1133
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1134 1135
int32_t qWorkerProcessFetchMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
1136 1137 1138
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

D
dapan1121 已提交
1139
  SResFetchMsg *msg = pMsg->pCont;
D
dapan1121 已提交
1140
  if (NULL == msg || pMsg->contLen < sizeof(*msg)) {
D
dapan1121 已提交
1141 1142 1143
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }  

D
dapan1121 已提交
1144 1145 1146 1147 1148
  msg->sId = htobe64(msg->sId);
  msg->queryId = htobe64(msg->queryId);
  msg->taskId = htobe64(msg->taskId);

  QW_ERR_RET(qwUpdateSchLastAccess(qWorkerMgmt, msg->sId));
D
dapan1121 已提交
1149 1150

  void *data = NULL;
D
dapan1121 已提交
1151 1152
  SQWorkerResCache *res = NULL;
  int32_t code = 0;
D
dapan1121 已提交
1153
  
D
dapan1121 已提交
1154 1155
  QW_ERR_RET(qwAcquireTaskResCache(QW_READ, qWorkerMgmt, msg->queryId, msg->taskId, &res));

D
dapan1121 已提交
1156
  QW_ERR_JRET(qwHandleFetch(res, qWorkerMgmt, msg->sId, msg->queryId, msg->taskId, pMsg));
D
dapan1121 已提交
1157 1158 1159 1160 1161 1162 1163 1164

_return:

  qwReleaseTaskResCache(QW_READ, qWorkerMgmt);
  
  QW_RET(code);
}

D
dapan1121 已提交
1165 1166
int32_t qWorkerProcessCancelMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
1167 1168 1169
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

D
dapan1121 已提交
1170
  int32_t code = 0;
D
dapan1121 已提交
1171
  STaskCancelMsg *msg = pMsg->pCont;
D
dapan1121 已提交
1172
  if (NULL == msg || pMsg->contLen < sizeof(*msg)) {
D
dapan1121 已提交
1173
    qError("invalid task cancel msg");  
D
dapan1121 已提交
1174 1175 1176
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }  

D
dapan1121 已提交
1177 1178 1179 1180 1181
  msg->sId = htobe64(msg->sId);
  msg->queryId = htobe64(msg->queryId);
  msg->taskId = htobe64(msg->taskId);

  QW_ERR_JRET(qwCancelTask(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId));
D
dapan1121 已提交
1182

D
dapan1121 已提交
1183 1184 1185
_return:

  QW_ERR_RET(qwBuildAndSendCancelRsp(pMsg, code));
D
dapan1121 已提交
1186 1187 1188 1189

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1190 1191
int32_t qWorkerProcessDropMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
1192 1193 1194
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

D
dapan1121 已提交
1195
  int32_t code = 0;
D
dapan1121 已提交
1196
  STaskDropMsg *msg = pMsg->pCont;
D
dapan1121 已提交
1197
  if (NULL == msg || pMsg->contLen < sizeof(*msg)) {
D
dapan1121 已提交
1198
    qError("invalid task drop msg");
D
dapan1121 已提交
1199 1200 1201
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }  

D
dapan1121 已提交
1202 1203 1204 1205 1206
  msg->sId = htobe64(msg->sId);
  msg->queryId = htobe64(msg->queryId);
  msg->taskId = htobe64(msg->taskId);

  QW_ERR_JRET(qwCancelDropTask(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId));
D
dapan1121 已提交
1207 1208

_return:
D
dapan1121 已提交
1209

D
dapan1121 已提交
1210
  QW_ERR_RET(qwBuildAndSendDropRsp(pMsg, code));
D
dapan1121 已提交
1211 1212 1213 1214

  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
int32_t qWorkerProcessShowMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

  int32_t code = 0;
  SVShowTablesReq *pReq = pMsg->pCont;
  QW_ERR_RET(qwBuildAndSendShowRsp(pMsg, code));
}

int32_t qWorkerProcessShowFetchMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

  SVShowTablesFetchReq *pFetchReq = pMsg->pCont;
  QW_ERR_RET(qwBuildAndSendShowFetchRsp(pMsg, pFetchReq));
}

D
dapan1121 已提交
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
int32_t qWorkerContinueQuery(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  int32_t code = 0;
  int8_t status = 0;
  bool queryDone = false;
  uint64_t sId, qId, tId;

  //TODO call executer to continue execute subquery
  code = 0; 
  void *data = NULL;
  queryDone = false;
  //TODO call executer to continue execute subquery
  
  if (TSDB_CODE_SUCCESS != code) {
    status = JOB_TASK_STATUS_FAILED;
  } else if (queryDone) {
    status = JOB_TASK_STATUS_SUCCEED;
  } else {
    status = JOB_TASK_STATUS_PARTIAL_SUCCEED;
  }

  code = qwQueryPostProcess(qWorkerMgmt, sId, qId, tId, status, code);

  QW_RET(code);
}

D
dapan1121 已提交
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
void qWorkerDestroy(void **qWorkerMgmt) {
  if (NULL == qWorkerMgmt || NULL == *qWorkerMgmt) {
    return;
  }

  SQWorkerMgmt *mgmt = *qWorkerMgmt;
  
  //TODO STOP ALL QUERY

  //TODO FREE ALL

  tfree(*qWorkerMgmt);
}