qworker.c 30.4 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6
#include "taosmsg.h"
#include "query.h"
#include "qworker.h"
#include "qworkerInt.h"
#include "planner.h"

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

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

D
dapan1121 已提交
57
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
58

D
dapan1121 已提交
59
_return:
D
dapan1121 已提交
60

D
dapan1121 已提交
61 62
  qError("invalid task status:%d", oriStatus);
  QW_ERR_RET(code);
D
dapan1121 已提交
63 64
}

D
dapan1121 已提交
65 66
int32_t qwUpdateTaskInfo(SQWorkerTaskStatus *task, int8_t type, void *data) {
  int32_t code = 0;
D
dapan1121 已提交
67
  
D
dapan1121 已提交
68 69 70 71 72 73 74 75 76 77
  switch (type) {
    case QW_TASK_INFO_STATUS: {
      int8_t newStatus = *(int8_t *)data;
      QW_ERR_RET(qwCheckStatusSwitch(task->status, newStatus));
      task->status = newStatus;
      break;
    }
    default:
      qError("uknown task info type:%d", type);
      return TSDB_CODE_QRY_APP_ERROR;
D
dapan1121 已提交
78
  }
D
dapan1121 已提交
79
  
D
dapan1121 已提交
80 81 82 83 84 85 86 87 88
  return TSDB_CODE_SUCCESS;
}

int32_t qwAddTaskResult(SQWorkerMgmt *mgmt, uint64_t queryId, uint64_t taskId, void *data) {
  char id[sizeof(queryId) + sizeof(taskId)] = {0};
  QW_SET_QTID(id, queryId, taskId);

  SQWorkerResCache resCache = {0};
  resCache.data = data;
D
dapan1121 已提交
89 90

  QW_LOCK(QW_WRITE, &mgmt->resLock);
D
dapan1121 已提交
91
  if (0 != taosHashPut(mgmt->resHash, id, sizeof(id), &resCache, sizeof(SQWorkerResCache))) {
D
dapan1121 已提交
92
    QW_UNLOCK(QW_WRITE, &mgmt->resLock);
D
dapan1121 已提交
93 94 95 96
    qError("taosHashPut queryId[%"PRIx64"] taskId[%"PRIx64"] to resHash failed", queryId, taskId);
    return TSDB_CODE_QRY_APP_ERROR;
  }

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

D
dapan1121 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  return TSDB_CODE_SUCCESS;
}


int32_t qwGetTaskResult(SQWorkerMgmt *mgmt, uint64_t queryId, uint64_t taskId, void **data) {
  char id[sizeof(queryId) + sizeof(taskId)] = {0};
  QW_SET_QTID(id, queryId, taskId);

  SQWorkerResCache *resCache = taosHashGet(mgmt->resHash, id, sizeof(id));
  if (NULL == resCache) {
    qError("no task res for queryId[%"PRIx64"] taskId[%"PRIx64"]", queryId, taskId);
    return TSDB_CODE_QRY_APP_ERROR;
  }

  *data = resCache->data;

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
118 119 120 121 122 123 124

static FORCE_INLINE int32_t qwAcquireScheduler(int32_t rwType, SQWorkerMgmt *mgmt, uint64_t schedulerId, SQWorkerSchStatus **sch) {
  QW_LOCK(rwType, &mgmt->schLock);
  *sch = taosHashGet(mgmt->schHash, &schedulerId, sizeof(schedulerId));
  if (NULL == (*sch)) {
    QW_LOCK(rwType, &mgmt->schLock);
    return TSDB_CODE_QRY_SCH_NOT_EXIST;
D
dapan1121 已提交
125 126
  }

D
dapan1121 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  return TSDB_CODE_SUCCESS;
}


static FORCE_INLINE int32_t qwInsertAndAcquireScheduler(int32_t rwType, SQWorkerMgmt *mgmt, uint64_t schedulerId, SQWorkerSchStatus **sch) {
  SQWorkerSchStatus newSch = {0};
  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);
    int32_t code = taosHashPut(mgmt->schHash, &schedulerId, sizeof(schedulerId), &newSch, sizeof(newSch));
    if (0 != code) {
      if (!HASH_NODE_EXIST(code)) {
        QW_UNLOCK(QW_WRITE, &mgmt->schLock);
        qError("taosHashPut schedulerId[%"PRIx64"] to scheduleHash failed", schedulerId);
        taosHashCleanup(newSch.tasksHash);
        return TSDB_CODE_QRY_APP_ERROR;
      }
    }
    
    QW_UNLOCK(QW_WRITE, &mgmt->schLock);
    if (TSDB_CODE_SUCCESS == qwAcquireScheduler(rwType, mgmt, schedulerId, sch)) {
      taosHashCleanup(newSch.tasksHash);
      return TSDB_CODE_SUCCESS;
    }
  }
D
dapan1121 已提交
157 158 159 160

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174

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

static FORCE_INLINE int32_t qwAcquireTask(int32_t rwType, SQWorkerSchStatus *sch, uint64_t queryId, uint64_t taskId, SQWorkerTaskStatus **task) {
  char id[sizeof(queryId) + sizeof(taskId)] = {0};
  QW_SET_QTID(id, queryId, taskId);

  QW_LOCK(rwType, &sch->tasksLock);
  *task = taosHashGet(sch->tasksHash, id, sizeof(id));
  if (NULL == (*task)) {
    QW_UNLOCK(rwType, &sch->tasksLock);
    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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
static FORCE_INLINE int32_t qwInsertAndAcquireTask(int32_t rwType, SQWorkerSchStatus *sch, uint64_t queryId, uint64_t taskId, int8_t status, bool *inserted, SQWorkerTaskStatus **task) {
  char id[sizeof(queryId) + sizeof(taskId)] = {0};
  QW_SET_QTID(id, queryId, taskId);

  while (true) {
    *inserted = false;
    
    QW_LOCK(QW_WRITE, &sch->tasksLock);
    int32_t code = taosHashPut(sch->tasksHash, id, sizeof(id), &status, sizeof(status));
    if (0 != code) {
      QW_UNLOCK(QW_WRITE, &sch->tasksLock);
      if (HASH_NODE_EXIST(code)) {
        if (qwAcquireTask(rwType, sch, queryId, taskId, task)) {
          continue;
        }

        break;
      } else {
        qError("taosHashPut queryId[%"PRIx64"] taskId[%"PRIx64"] to scheduleHash failed", queryId, taskId);
        return TSDB_CODE_QRY_APP_ERROR;
      }
    }
    QW_UNLOCK(QW_WRITE, &sch->tasksLock);

    *inserted = true;
    
    if (TSDB_CODE_SUCCESS == qwAcquireTask(rwType, sch, queryId, taskId, task)) {
      return TSDB_CODE_SUCCESS;
    }
  }

  return TSDB_CODE_SUCCESS;
}


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

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);
}


int32_t qwGetSchTasksStatus(SQWorkerMgmt *mgmt, uint64_t schedulerId, SSchedulerStatusRsp **rsp) {
  SQWorkerSchStatus *schStatus = NULL;
  int32_t taskNum = 0;

  if (qwAcquireScheduler(QW_READ, mgmt, schedulerId, &schStatus)) {
    qWarn("no scheduler for schedulerId[%"PRIx64"]", schedulerId);
  } else {
    schStatus->lastAccessTs = taosGetTimestampSec();

    QW_LOCK(QW_READ, &schStatus->tasksLock);
    taskNum = taosHashGetSize(schStatus->tasksHash);
  }
  
D
dapan1121 已提交
251 252 253 254
  int32_t size = sizeof(SSchedulerStatusRsp) + sizeof((*rsp)->status[0]) * taskNum;
  *rsp = calloc(1, size);
  if (NULL == *rsp) {
    qError("calloc %d failed", size);
D
dapan1121 已提交
255 256 257 258 259
    if (schStatus) {
      QW_UNLOCK(QW_READ, &schStatus->tasksLock);
      qwReleaseScheduler(QW_READ, mgmt);
    }
    
D
dapan1121 已提交
260 261 262 263 264
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
  }

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

D
dapan1121 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  if (schStatus) {
    void *pIter = taosHashIterate(schStatus->tasksHash, NULL);
    while (pIter) {
      SQWorkerTaskStatus *taskStatus = (SQWorkerTaskStatus *)pIter;
      taosHashGetKey(pIter, &key, &keyLen);

      QW_GET_QTID(key, (*rsp)->status[i].queryId, (*rsp)->status[i].taskId);
      (*rsp)->status[i].status = taskStatus->status;
      
      pIter = taosHashIterate(schStatus->tasksHash, pIter);
    }  
  }

  if (schStatus) {
    QW_UNLOCK(QW_READ, &schStatus->tasksLock);
    qwReleaseScheduler(QW_READ, mgmt);
  }
D
dapan1121 已提交
284 285 286 287 288 289

  (*rsp)->num = taskNum;

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 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 423 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 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 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 554 555


int32_t qwUpdateSchLastAccess(SQWorkerMgmt *mgmt, uint64_t schedulerId) {
  SQWorkerSchStatus *schStatus = NULL;

  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, schedulerId, &schStatus));

  schStatus->lastAccessTs = taosGetTimestampSec();

  qwReleaseScheduler(QW_READ, mgmt);

  return TSDB_CODE_SUCCESS;
}


int32_t qwGetTaskStatus(SQWorkerMgmt *mgmt, uint64_t schedulerId, uint64_t queryId, uint64_t taskId, int8_t *taskStatus) {
  SQWorkerSchStatus *sch = NULL;
  SQWorkerTaskStatus *task = NULL;
  int32_t code = 0;
  
  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, schedulerId, &sch));

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

  *taskStatus = task->status;

_return:
  if (task) {
    qwReleaseTask(QW_READ, sch);
  }

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

  QW_RET(code);
}


int32_t qwSwitchTaskStatus(SQWorkerMgmt *mgmt, uint64_t schedulerId, uint64_t queryId, uint64_t taskId, int8_t taskStatus) {
  SQWorkerSchStatus *sch = NULL;
  SQWorkerTaskStatus *task = NULL;
  int32_t code = 0;
  bool inserted = false;
  
  if (qwAcquireScheduler(QW_READ, mgmt, schedulerId, &sch)) {
    if (qwCheckStatusSwitch(JOB_TASK_STATUS_NULL, taskStatus)) {
      qError("switch status error, not start to %d", taskStatus);
      QW_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
    }
    
    QW_ERR_RET(qwInsertAndAcquireScheduler(QW_READ, mgmt, schedulerId, &sch));
  }

  if (qwAcquireTask(QW_READ, sch, queryId, taskId, &task)) {
    if (qwCheckStatusSwitch(JOB_TASK_STATUS_NOT_START, taskStatus)) {
      qwReleaseScheduler(QW_READ, mgmt);        
      qError("switch status error, not start to %d", taskStatus);
      QW_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
    }
  
    QW_ERR_JRET(qwInsertAndAcquireTask(QW_READ, sch, queryId, taskId, taskStatus, &inserted, &task));
    
    if (inserted) {
      qwReleaseTask(QW_READ, sch);
      qwReleaseScheduler(QW_READ, mgmt);
      return TSDB_CODE_SUCCESS;
    }

    QW_LOCK(QW_WRITE, &task->lock);
    code = qwUpdateTaskInfo(task, QW_TASK_INFO_STATUS, &taskStatus);
    QW_UNLOCK(QW_WRITE, &task->lock);

    qwReleaseTask(QW_READ, sch);
    qwReleaseScheduler(QW_READ, mgmt);    
    
    QW_RET(code);
  }

  QW_LOCK(QW_WRITE, &task->lock);
  code = qwUpdateTaskInfo(task, QW_TASK_INFO_STATUS, &taskStatus);
  QW_UNLOCK(QW_WRITE, &task->lock);

_return:

  qwReleaseTask(QW_READ, sch);
  qwReleaseScheduler(QW_READ, mgmt);    
  
  QW_RET(code);
}


int32_t qwCancelTask(SQWorkerMgmt *mgmt, uint64_t schedulerId, uint64_t queryId, uint64_t taskId) {
  SQWorkerSchStatus *sch = NULL;
  SQWorkerTaskStatus *task = NULL;
  int32_t code = 0;

  if (TSDB_CODE_SUCCESS != qwAcquireScheduler(QW_READ, mgmt, schedulerId, &sch)) {
    QW_ERR_RET(qwSwitchTaskStatus(mgmt, schedulerId, queryId, taskId, JOB_TASK_STATUS_NOT_START));
    
    QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, schedulerId, &sch));
  }

  if (qwAcquireTask(QW_READ, sch, queryId, taskId, &task)) {
    code = qwSwitchTaskStatus(mgmt, schedulerId, queryId, taskId, JOB_TASK_STATUS_NOT_START);
    if (code) {
      qwReleaseScheduler(QW_READ, mgmt);
      QW_ERR_RET(code);
    }

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

  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);
  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 qwDropTask(SQWorkerMgmt *mgmt, uint64_t schedulerId, uint64_t queryId, uint64_t taskId) {
  SQWorkerSchStatus *sch = NULL;
  SQWorkerTaskStatus *task = NULL;
  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);
  
  if (TSDB_CODE_SUCCESS != qwAcquireScheduler(QW_WRITE, mgmt, schedulerId, &sch)) {
    qWarn("scheduler %"PRIx64" doesn't exist", schedulerId);
    return TSDB_CODE_SUCCESS;
  }

  if (qwAcquireTask(QW_WRITE, sch, queryId, taskId, &task)) {
    qwReleaseScheduler(QW_WRITE, mgmt);
    
    qWarn("scheduler %"PRIx64" queryId %"PRIx64" taskId:%"PRIx64" doesn't exist", schedulerId, queryId, taskId);
    return TSDB_CODE_SUCCESS;
  }

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

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


int32_t qwCancelDropTask(SQWorkerMgmt *mgmt, uint64_t schedulerId, uint64_t queryId, uint64_t taskId) {
  SQWorkerSchStatus *sch = NULL;
  SQWorkerTaskStatus *task = NULL;
  int32_t code = 0;

  if (TSDB_CODE_SUCCESS != qwAcquireScheduler(QW_READ, mgmt, schedulerId, &sch)) {
    qWarn("scheduler %"PRIx64" doesn't exist", schedulerId);
    return TSDB_CODE_SUCCESS;
  }

  if (qwAcquireTask(QW_READ, sch, queryId, taskId, &task)) {
    qwReleaseScheduler(QW_READ, mgmt);
    
    qWarn("scheduler %"PRIx64" queryId %"PRIx64" taskId:%"PRIx64" doesn't exist", schedulerId, queryId, taskId);
    return TSDB_CODE_SUCCESS;
  }

  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) {
    newStatus = JOB_TASK_STATUS_CANCELLING;
    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);
  
    QW_ERR_RET(qwDropTask(mgmt, schedulerId, queryId, taskId));
    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 已提交
556 557
  SQueryTableRsp *pRsp = (SQueryTableRsp *)rpcMallocCont(sizeof(SQueryTableRsp));
  pRsp->code = code;
D
dapan1121 已提交
558

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

  rpcSendResponse(&rpcRsp);

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
570 571 572
}

int32_t qwBuildAndSendReadyRsp(SRpcMsg *pMsg, int32_t code) {
D
dapan1121 已提交
573 574 575 576 577 578 579 580 581 582 583 584
  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 已提交
585

D
dapan1121 已提交
586
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
587 588 589
}

int32_t qwBuildAndSendStatusRsp(SRpcMsg *pMsg, SSchedulerStatusRsp *sStatus) {
D
dapan1121 已提交
590 591 592 593 594 595 596 597 598
  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 已提交
599

D
dapan1121 已提交
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
  if (sStatus) {
    memcpy(pRsp, sStatus, size);
  } else {
    pRsp->num = 0;
  }

  SRpcMsg rpcRsp = {
    .handle  = pMsg->handle,
    .ahandle = pMsg->ahandle,
    .pCont   = pRsp,
    .contLen = size,
    .code    = 0,
  };

  rpcSendResponse(&rpcRsp);

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

int32_t qwBuildAndSendFetchRsp(SRpcMsg *pMsg, void *data) {
D
dapan1121 已提交
620 621 622 623 624 625 626 627 628 629 630 631 632
  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 已提交
633

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

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
637 638 639
}


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

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

  rpcSendResponse(&rpcRsp);

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
655 656
}

D
dapan1121 已提交
657 658 659 660 661 662 663 664 665 666 667 668 669
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);
D
dapan1121 已提交
670

D
dapan1121 已提交
671
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
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 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 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 765 766 767 768 769 770 771 772 773 774 775 776 777 778 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
}



int32_t qwCheckAndSendReadyRsp(SQWorkerMgmt *mgmt, uint64_t schedulerId, uint64_t queryId, uint64_t taskId, SRpcMsg *pMsg, int32_t rspCode) {
  SQWorkerSchStatus *sch = NULL;
  SQWorkerTaskStatus *task = NULL;
  int32_t code = 0;

  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, schedulerId, &sch));

  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);
  }

  if (sch) {
    qwReleaseTask(QW_READ, sch);
  }

  qwReleaseScheduler(QW_READ, mgmt);

  QW_RET(code);
}

int32_t qwSetAndSendReadyRsp(SQWorkerMgmt *mgmt, uint64_t schedulerId, uint64_t queryId, uint64_t taskId, SRpcMsg *pMsg) {
  SQWorkerSchStatus *sch = NULL;
  SQWorkerTaskStatus *task = NULL;
  int32_t code = 0;

  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, schedulerId, &sch));

  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);
  }

  if (sch) {
    qwReleaseTask(QW_READ, sch);
  }

  qwReleaseScheduler(QW_READ, mgmt);

  QW_RET(code);
}

int32_t qwCheckTaskCancelDrop( SQWorkerMgmt *mgmt, uint64_t schedulerId, uint64_t queryId, uint64_t taskId, bool *needStop) {
  SQWorkerSchStatus *sch = NULL;
  SQWorkerTaskStatus *task = NULL;
  int32_t code = 0;
  int8_t status = JOB_TASK_STATUS_CANCELLED;

  *needStop = false;

  if (qwAcquireScheduler(QW_READ, mgmt, schedulerId, &sch)) {
    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)) {
    QW_UNLOCK(QW_READ, &task->lock);
    qwReleaseTask(QW_READ, sch);
    qwReleaseScheduler(QW_READ, mgmt);

    return TSDB_CODE_SUCCESS;
  }

  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);
  } else if (task->drop) {
    qwReleaseTask(QW_READ, sch);
    qwReleaseScheduler(QW_READ, mgmt);
    
    qwDropTask(mgmt, schedulerId, queryId, taskId);
  }

  return TSDB_CODE_SUCCESS;
}


int32_t qwHandleFetch(SQWorkerResCache *res, SQWorkerMgmt *mgmt, uint64_t schedulerId, uint64_t queryId, uint64_t taskId, SRpcMsg *pMsg) {
  SQWorkerSchStatus *sch = NULL;
  SQWorkerTaskStatus *task = NULL;
  int32_t code = 0;
D
dapan1121 已提交
809 810
  int32_t needRsp = true;
  void *data = NULL;
D
dapan1121 已提交
811

D
dapan1121 已提交
812
  QW_ERR_JRET(qwAcquireScheduler(QW_READ, mgmt, schedulerId, &sch));
D
dapan1121 已提交
813 814 815 816 817 818 819 820 821 822
  QW_ERR_JRET(qwAcquireTask(QW_READ, sch, queryId, taskId, &task));

  QW_LOCK(QW_READ, &task->lock);

  if (task->status != JOB_TASK_STATUS_EXECUTING && task->status != JOB_TASK_STATUS_PARTIAL_SUCCEED && task->status != JOB_TASK_STATUS_SUCCEED) {
    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 已提交
823
    data = res->data;
D
dapan1121 已提交
824 825 826 827 828 829 830 831 832 833 834 835
    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 已提交
836 837

    needRsp = false;
D
dapan1121 已提交
838 839 840 841 842 843 844 845 846
  }

_return:
  if (task) {
    QW_UNLOCK(QW_READ, &task->lock);
  }
  
  if (sch) {
    qwReleaseTask(QW_READ, sch);
D
dapan1121 已提交
847
    qwReleaseScheduler(QW_READ, mgmt);
D
dapan1121 已提交
848 849
  }

D
dapan1121 已提交
850 851 852
  if (needRsp) {
    qwBuildAndSendFetchRsp(pMsg, res->data);
  }
D
dapan1121 已提交
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898

  QW_RET(code);
}

int32_t qwQueryPostProcess(SQWorkerMgmt *mgmt, uint64_t schedulerId, uint64_t queryId, uint64_t taskId, int8_t status, int32_t errCode) {
  SQWorkerSchStatus *sch = NULL;
  SQWorkerTaskStatus *task = NULL;
  int32_t code = 0;
  int8_t newStatus = JOB_TASK_STATUS_CANCELLED;

  code = qwAcquireScheduler(QW_READ, mgmt, schedulerId, &sch);
  if (code) {
    qError("schedulerId:%"PRIx64" not in cache", schedulerId);
    QW_ERR_RET(code);
  }

  code = qwAcquireTask(QW_READ, sch, queryId, taskId, &task);
  if (code) {
    qwReleaseScheduler(QW_READ, mgmt);
    qError("schedulerId:%"PRIx64" queryId:%"PRIx64" taskId:%"PRIx64" not in cache", schedulerId, queryId, taskId);
    QW_ERR_RET(code);
  }

  if (task->cancel) {
    QW_LOCK(QW_WRITE, &task->lock);
    qwUpdateTaskInfo(task, QW_TASK_INFO_STATUS, &newStatus);
    QW_UNLOCK(QW_WRITE, &task->lock);
  } else if (task->drop) {
    qwReleaseTask(QW_READ, sch);
    qwReleaseScheduler(QW_READ, mgmt);
    
    qwDropTask(mgmt, schedulerId, queryId, taskId);

    return TSDB_CODE_SUCCESS;
  } else {
    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 已提交
899 900 901 902 903 904


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 已提交
905
    QW_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
906 907 908 909 910 911 912 913 914 915
  }

  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 已提交
916 917
  mgmt->schHash = taosHashInit(mgmt->cfg.maxSchedulerNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_NO_LOCK);
  if (NULL == mgmt->schHash) {
D
dapan1121 已提交
918 919 920 921
    tfree(mgmt);
    QW_ERR_LRET(TSDB_CODE_QRY_OUT_OF_MEMORY, "init %d schduler hash failed", mgmt->cfg.maxSchedulerNum);
  }

D
dapan1121 已提交
922
  mgmt->resHash = taosHashInit(mgmt->cfg.maxResCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
D
dapan1121 已提交
923
  if (NULL == mgmt->resHash) {
D
dapan1121 已提交
924 925
    taosHashCleanup(mgmt->schHash);
    mgmt->schHash = NULL;
D
dapan1121 已提交
926 927 928 929 930 931 932 933 934 935
    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 已提交
936 937
int32_t qWorkerProcessQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
938
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
939 940
  }

D
dapan1121 已提交
941 942
  SSubQueryMsg *msg = pMsg->pCont;
  if (NULL == msg || pMsg->contLen <= sizeof(*msg)) {
D
dapan1121 已提交
943
    qError("invalid query msg");
D
dapan1121 已提交
944 945 946 947 948 949
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }
  
  bool queryDone = false;
  bool queryRsp = false;
  bool needStop = false;
D
dapan1121 已提交
950
  SSubplan *plan = NULL;
D
dapan1121 已提交
951
  int32_t code = 0;
D
dapan1121 已提交
952

D
dapan1121 已提交
953 954 955
  QW_ERR_JRET(qwCheckTaskCancelDrop(qWorkerMgmt, msg->schedulerId, msg->queryId, msg->taskId, &needStop));
  if (needStop) {
    qWarn("task need stop");
D
dapan1121 已提交
956
    QW_ERR_JRET(TSDB_CODE_QRY_TASK_CANCELLED);
D
dapan1121 已提交
957 958 959
  }
  
  code = qStringToSubplan(msg->msg, &plan);
D
dapan1121 已提交
960 961
  if (TSDB_CODE_SUCCESS != code) {
    qError("schId:%"PRIx64",qId:%"PRIx64",taskId:%"PRIx64" string to subplan failed, code:%d", msg->schedulerId, msg->queryId, msg->taskId, code);
D
dapan1121 已提交
962
    QW_ERR_JRET(code);
D
dapan1121 已提交
963 964
  }

D
dapan1121 已提交
965 966 967 968 969 970 971 972 973
  //TODO call executer to init subquery
  code = 0; // return error directly
  //TODO call executer to init subquery
  
  if (code) {
    QW_ERR_JRET(code);
  } else {
    QW_ERR_JRET(qwSwitchTaskStatus(qWorkerMgmt, msg->schedulerId, msg->queryId, msg->taskId, JOB_TASK_STATUS_EXECUTING));
  }
D
dapan1121 已提交
974

D
dapan1121 已提交
975
  QW_ERR_JRET(qwBuildAndSendQueryRsp(pMsg, TSDB_CODE_SUCCESS));
D
dapan1121 已提交
976

D
dapan1121 已提交
977 978
  queryRsp = true;
 
D
dapan1121 已提交
979
  //TODO call executer to execute subquery
D
dapan1121 已提交
980
  code = 0; 
D
dapan1121 已提交
981
  void *data = NULL;
D
dapan1121 已提交
982
  queryDone = false;
D
dapan1121 已提交
983 984
  //TODO call executer to execute subquery

D
dapan1121 已提交
985 986 987 988
  if (code) {
    QW_ERR_JRET(code);
  } else {
    QW_ERR_JRET(qwAddTaskResult(qWorkerMgmt, msg->queryId, msg->taskId, data));
D
dapan1121 已提交
989

D
dapan1121 已提交
990 991
    QW_ERR_JRET(qwSwitchTaskStatus(qWorkerMgmt, msg->schedulerId, msg->queryId, msg->taskId, JOB_TASK_STATUS_PARTIAL_SUCCEED));
  }
D
dapan1121 已提交
992 993 994

_return:

D
dapan1121 已提交
995 996 997 998
  if (queryRsp) {
    code = qwCheckAndSendReadyRsp(qWorkerMgmt, msg->schedulerId, msg->queryId, msg->taskId, pMsg, code);
  } else {
    code = qwBuildAndSendQueryRsp(pMsg, code);
D
dapan1121 已提交
999
  }
D
dapan1121 已提交
1000 1001 1002 1003 1004 1005 1006 1007
  
  int8_t status = 0;
  if (TSDB_CODE_SUCCESS != code || queryDone) {
    if (code) {
      status = JOB_TASK_STATUS_FAILED; //TODO set CANCELLED from code
    } else {
      status = JOB_TASK_STATUS_SUCCEED;
    }
D
dapan1121 已提交
1008

D
dapan1121 已提交
1009 1010
    qwQueryPostProcess(qWorkerMgmt, msg->schedulerId, msg->queryId, msg->taskId, status, code);
  }
D
dapan1121 已提交
1011
  
D
dapan1121 已提交
1012
  QW_RET(code);
D
dapan1121 已提交
1013 1014
}

D
dapan1121 已提交
1015 1016
int32_t qWorkerProcessReadyMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg){
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
1017 1018 1019
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

D
dapan1121 已提交
1020 1021
  SResReadyMsg *msg = pMsg->pCont;
  if (NULL == msg || pMsg->contLen <= sizeof(*msg)) {
D
dapan1121 已提交
1022
    qError("invalid task status msg");  
D
dapan1121 已提交
1023 1024
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }  
D
dapan1121 已提交
1025

D
dapan1121 已提交
1026
  QW_ERR_RET(qwSetAndSendReadyRsp(qWorkerMgmt, msg->schedulerId, msg->queryId, msg->taskId, pMsg));
D
dapan1121 已提交
1027 1028 1029 1030
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1031 1032
int32_t qWorkerProcessStatusMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
1033 1034 1035
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

D
dapan1121 已提交
1036
  int32_t code = 0;
D
dapan1121 已提交
1037 1038
  SSchTasksStatusMsg *msg = pMsg->pCont;
  if (NULL == msg || pMsg->contLen <= sizeof(*msg)) {
D
dapan1121 已提交
1039
    qError("invalid task status msg");
D
dapan1121 已提交
1040 1041 1042
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }  

D
dapan1121 已提交
1043 1044
  SSchedulerStatusRsp *sStatus = NULL;
  
D
dapan1121 已提交
1045 1046 1047
  QW_ERR_JRET(qwGetSchTasksStatus(qWorkerMgmt, msg->schedulerId, &sStatus));

_return:
D
dapan1121 已提交
1048

D
dapan1121 已提交
1049 1050
  QW_ERR_RET(qwBuildAndSendStatusRsp(pMsg, sStatus));

D
dapan1121 已提交
1051 1052 1053
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1054 1055
int32_t qWorkerProcessFetchMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
1056 1057 1058
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

D
dapan1121 已提交
1059 1060 1061 1062 1063
  SResFetchMsg *msg = pMsg->pCont;
  if (NULL == msg || pMsg->contLen <= sizeof(*msg)) {
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }  

D
dapan1121 已提交
1064 1065 1066
  QW_ERR_RET(qwUpdateSchLastAccess(qWorkerMgmt, msg->schedulerId));

  void *data = NULL;
D
dapan1121 已提交
1067 1068
  SQWorkerResCache *res = NULL;
  int32_t code = 0;
D
dapan1121 已提交
1069
  
D
dapan1121 已提交
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
  QW_ERR_RET(qwAcquireTaskResCache(QW_READ, qWorkerMgmt, msg->queryId, msg->taskId, &res));

  QW_ERR_JRET(qwHandleFetch(res, qWorkerMgmt, msg->schedulerId, msg->queryId, msg->taskId, pMsg));

_return:

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

D
dapan1121 已提交
1081 1082
int32_t qWorkerProcessCancelMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
1083 1084 1085
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

D
dapan1121 已提交
1086
  int32_t code = 0;
D
dapan1121 已提交
1087 1088
  STaskCancelMsg *msg = pMsg->pCont;
  if (NULL == msg || pMsg->contLen <= sizeof(*msg)) {
D
dapan1121 已提交
1089
    qError("invalid task cancel msg");  
D
dapan1121 已提交
1090 1091 1092
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }  

D
dapan1121 已提交
1093
  QW_ERR_JRET(qwCancelTask(qWorkerMgmt, msg->schedulerId, msg->queryId, msg->taskId));
D
dapan1121 已提交
1094

D
dapan1121 已提交
1095 1096 1097
_return:

  QW_ERR_RET(qwBuildAndSendCancelRsp(pMsg, code));
D
dapan1121 已提交
1098 1099 1100 1101

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1102 1103
int32_t qWorkerProcessDropMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
1104 1105 1106
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

D
dapan1121 已提交
1107
  int32_t code = 0;
D
dapan1121 已提交
1108 1109
  STaskDropMsg *msg = pMsg->pCont;
  if (NULL == msg || pMsg->contLen <= sizeof(*msg)) {
D
dapan1121 已提交
1110
    qError("invalid task drop msg");
D
dapan1121 已提交
1111 1112 1113
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }  

D
dapan1121 已提交
1114 1115 1116
  QW_ERR_JRET(qwCancelDropTask(qWorkerMgmt, msg->schedulerId, msg->queryId, msg->taskId));

_return:
D
dapan1121 已提交
1117

D
dapan1121 已提交
1118
  QW_ERR_RET(qwBuildAndSendDropRsp(pMsg, code));
D
dapan1121 已提交
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138

  return TSDB_CODE_SUCCESS;
}


void qWorkerDestroy(void **qWorkerMgmt) {
  if (NULL == qWorkerMgmt || NULL == *qWorkerMgmt) {
    return;
  }

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

  //TODO FREE ALL

  tfree(*qWorkerMgmt);
}