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

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

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

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

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

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

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

D
dapan1121 已提交
93 94 95
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 已提交
96 97 98

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

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

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

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

D
dapan1121 已提交
112 113
static int32_t qwAddScheduler(int32_t rwType, SQWorkerMgmt *mgmt, uint64_t sId, SQWSchStatus **sch) {
  SQWSchStatus newSch = {0};
D
dapan1121 已提交
114 115 116 117 118 119 120 121
  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 已提交
122
    int32_t code = taosHashPut(mgmt->schHash, &sId, sizeof(sId), &newSch, sizeof(newSch));
D
dapan1121 已提交
123 124 125
    if (0 != code) {
      if (!HASH_NODE_EXIST(code)) {
        QW_UNLOCK(QW_WRITE, &mgmt->schLock);
D
dapan1121 已提交
126
        qError("taosHashPut sId[%"PRIx64"] to scheduleHash failed", sId);
D
dapan1121 已提交
127 128 129 130 131 132
        taosHashCleanup(newSch.tasksHash);
        return TSDB_CODE_QRY_APP_ERROR;
      }
    }
    
    QW_UNLOCK(QW_WRITE, &mgmt->schLock);
D
dapan1121 已提交
133
    if (TSDB_CODE_SUCCESS == qwAcquireScheduler(rwType, mgmt, sId, sch, QW_NOT_EXIST_ADD)) {
D
dapan1121 已提交
134 135 136
      return TSDB_CODE_SUCCESS;
    }
  }
D
dapan1121 已提交
137 138 139 140

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
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 已提交
159 160 161 162
static FORCE_INLINE void qwReleaseScheduler(int32_t rwType, SQWorkerMgmt *mgmt) {
  QW_UNLOCK(rwType, &mgmt->schLock);
}

D
dapan1121 已提交
163 164 165
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 已提交
166 167 168 169 170

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

D
dapan1121 已提交
172
    return TSDB_CODE_QRY_TASK_NOT_EXIST;
D
dapan1121 已提交
173 174
  }

D
dapan1121 已提交
175 176
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
177

D
dapan1121 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
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 已提交
194 195 196

  while (true) {
    QW_LOCK(QW_WRITE, &sch->tasksLock);
D
dapan1121 已提交
197
    int32_t code = taosHashPut(sch->tasksHash, id, sizeof(id), &ntask, sizeof(ntask));
D
dapan1121 已提交
198 199 200
    if (0 != code) {
      QW_UNLOCK(QW_WRITE, &sch->tasksLock);
      if (HASH_NODE_EXIST(code)) {
D
dapan1121 已提交
201 202 203 204 205 206 207 208
        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 已提交
209 210 211 212
        }

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

D
dapan1121 已提交
220 221 222 223 224 225
    if (rwType && task) {
      if (TSDB_CODE_SUCCESS == qwAcquireTask(rwType, sch, qId, tId, task)) {
        return TSDB_CODE_SUCCESS;
      }
    } else {
      break;
D
dapan1121 已提交
226
    }
D
dapan1121 已提交
227
  }  
D
dapan1121 已提交
228 229 230 231

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
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 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
}

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 已提交
269 270
int32_t qwGetSchTasksStatus(SQWorkerMgmt *mgmt, uint64_t sId, SSchedulerStatusRsp **rsp) {
  SQWSchStatus *sch = NULL;
D
dapan1121 已提交
271 272
  int32_t taskNum = 0;

D
dapan1121 已提交
273 274 275
  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, sId, &sch, QW_NOT_EXIST_RET_ERR));
  
  sch->lastAccessTs = taosGetTimestampSec();
D
dapan1121 已提交
276

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

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

D
dapan1121 已提交
295 296 297 298
  void *pIter = taosHashIterate(sch->tasksHash, NULL);
  while (pIter) {
    SQWTaskStatus *taskStatus = (SQWTaskStatus *)pIter;
    taosHashGetKey(pIter, &key, &keyLen);
D
dapan1121 已提交
299

D
dapan1121 已提交
300 301 302 303 304
    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 已提交
305

D
dapan1121 已提交
306 307
  QW_UNLOCK(QW_READ, &sch->tasksLock);
  qwReleaseScheduler(QW_READ, mgmt);
D
dapan1121 已提交
308 309 310 311 312 313

  (*rsp)->num = taskNum;

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
314 315


D
dapan1121 已提交
316 317
int32_t qwUpdateSchLastAccess(SQWorkerMgmt *mgmt, uint64_t sId) {
  SQWSchStatus *sch = NULL;
D
dapan1121 已提交
318

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

D
dapan1121 已提交
321
  sch->lastAccessTs = taosGetTimestampSec();
D
dapan1121 已提交
322 323 324 325 326 327

  qwReleaseScheduler(QW_READ, mgmt);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
328 329 330
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 已提交
331 332
  int32_t code = 0;

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

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

D
dapan1121 已提交
337 338 339 340
  QW_LOCK(QW_WRITE, &task->lock);
  qwUpdateTaskInfo(task, QW_TASK_INFO_STATUS, &status);
  QW_UNLOCK(QW_WRITE, &task->lock);
  
D
dapan1121 已提交
341 342
_return:

D
dapan1121 已提交
343 344
  qwReleaseTask(QW_READ, sch);
  qwReleaseScheduler(QW_READ, mgmt);
D
dapan1121 已提交
345 346 347 348 349

  QW_RET(code);
}


D
dapan1121 已提交
350 351 352
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 已提交
353 354
  int32_t code = 0;
  
D
dapan1121 已提交
355 356 357
  if (qwAcquireScheduler(QW_READ, mgmt, sId, &sch, QW_NOT_EXIST_RET_ERR)) {
    *taskStatus = JOB_TASK_STATUS_NULL;
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
358 359 360
  }

  if (qwAcquireTask(QW_READ, sch, queryId, taskId, &task)) {
D
dapan1121 已提交
361
    qwReleaseScheduler(QW_READ, mgmt);
D
dapan1121 已提交
362
    
D
dapan1121 已提交
363 364
    *taskStatus = JOB_TASK_STATUS_NULL;
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
365 366
  }

D
dapan1121 已提交
367
  *taskStatus = task->status;
D
dapan1121 已提交
368 369

  qwReleaseTask(QW_READ, sch);
D
dapan1121 已提交
370 371
  qwReleaseScheduler(QW_READ, mgmt);

D
dapan1121 已提交
372 373 374 375
  QW_RET(code);
}


D
dapan1121 已提交
376 377 378
int32_t qwCancelTask(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t queryId, uint64_t taskId) {
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
379 380
  int32_t code = 0;

D
dapan1121 已提交
381
  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, sId, &sch, QW_NOT_EXIST_ADD));
D
dapan1121 已提交
382 383

  if (qwAcquireTask(QW_READ, sch, queryId, taskId, &task)) {
D
dapan1121 已提交
384 385 386
    qwReleaseScheduler(QW_READ, mgmt);
    
    code = qwAddTask(mgmt, sId, queryId, taskId, JOB_TASK_STATUS_NOT_START, QW_EXIST_ACQUIRE, &sch, &task);
D
dapan1121 已提交
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
    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 已提交
416
  
D
dapan1121 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
  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 已提交
441 442 443
int32_t qwDropTask(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t queryId, uint64_t taskId) {
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
444 445 446 447 448 449 450 451 452 453
  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 已提交
454 455
  if (TSDB_CODE_SUCCESS != qwAcquireScheduler(QW_WRITE, mgmt, sId, &sch, QW_NOT_EXIST_RET_ERR)) {
    qWarn("scheduler %"PRIx64" doesn't exist", sId);
D
dapan1121 已提交
456 457 458 459 460 461
    return TSDB_CODE_SUCCESS;
  }

  if (qwAcquireTask(QW_WRITE, sch, queryId, taskId, &task)) {
    qwReleaseScheduler(QW_WRITE, mgmt);
    
D
dapan1121 已提交
462
    qWarn("scheduler %"PRIx64" queryId %"PRIx64" taskId:%"PRIx64" doesn't exist", sId, queryId, taskId);
D
dapan1121 已提交
463 464 465 466 467 468 469 470 471 472 473
    return TSDB_CODE_SUCCESS;
  }

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

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

D
dapan1121 已提交
474 475 476
int32_t qwCancelDropTask(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t queryId, uint64_t taskId) {
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
477 478
  int32_t code = 0;

D
dapan1121 已提交
479
  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, sId, &sch, QW_NOT_EXIST_ADD));
D
dapan1121 已提交
480 481 482 483

  if (qwAcquireTask(QW_READ, sch, queryId, taskId, &task)) {
    qwReleaseScheduler(QW_READ, mgmt);
    
D
dapan1121 已提交
484 485 486 487 488
    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 已提交
489 490 491 492 493 494 495 496 497 498
  }

  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 已提交
499
    newStatus = JOB_TASK_STATUS_DROPPING;
D
dapan1121 已提交
500 501 502 503 504 505 506 507 508 509 510 511
    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 已提交
512
    QW_ERR_RET(qwDropTask(mgmt, sId, queryId, taskId));
D
dapan1121 已提交
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
    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 已提交
542 543
  SQueryTableRsp *pRsp = (SQueryTableRsp *)rpcMallocCont(sizeof(SQueryTableRsp));
  pRsp->code = code;
D
dapan1121 已提交
544

D
dapan1121 已提交
545 546
  SRpcMsg rpcRsp = {
    .handle  = pMsg->handle,
D
dapan1121 已提交
547
    .ahandle = pMsg->ahandle,
D
dapan1121 已提交
548 549 550 551 552 553 554 555
    .pCont   = pRsp,
    .contLen = sizeof(*pRsp),
    .code    = code,
  };

  rpcSendResponse(&rpcRsp);

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
556 557 558
}

int32_t qwBuildAndSendReadyRsp(SRpcMsg *pMsg, int32_t code) {
D
dapan1121 已提交
559 560 561 562 563 564 565 566 567 568 569 570
  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 已提交
571

D
dapan1121 已提交
572
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
573 574 575
}

int32_t qwBuildAndSendStatusRsp(SRpcMsg *pMsg, SSchedulerStatusRsp *sStatus) {
D
dapan1121 已提交
576 577 578 579 580 581 582 583 584
  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 已提交
585

D
dapan1121 已提交
586 587 588 589 590 591 592
  if (sStatus) {
    memcpy(pRsp, sStatus, size);
  } else {
    pRsp->num = 0;
  }

  SRpcMsg rpcRsp = {
D
dapan1121 已提交
593
    .msgType = pMsg->msgType + 1,
D
dapan1121 已提交
594 595 596 597 598 599 600 601 602 603
    .handle  = pMsg->handle,
    .ahandle = pMsg->ahandle,
    .pCont   = pRsp,
    .contLen = size,
    .code    = 0,
  };

  rpcSendResponse(&rpcRsp);

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
604 605 606
}

int32_t qwBuildAndSendFetchRsp(SRpcMsg *pMsg, void *data) {
D
dapan1121 已提交
607 608 609 610 611 612 613 614 615 616 617 618 619
  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 已提交
620

D
dapan1121 已提交
621 622 623
  rpcSendResponse(&rpcRsp);

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
624 625
}

D
dapan1121 已提交
626 627 628
int32_t qwBuildAndSendCancelRsp(SRpcMsg *pMsg, int32_t code) {
  STaskCancelRsp *pRsp = (STaskCancelRsp *)rpcMallocCont(sizeof(STaskCancelRsp));
  pRsp->code = code;
D
dapan1121 已提交
629

D
dapan1121 已提交
630 631 632 633 634 635 636 637 638 639
  SRpcMsg rpcRsp = {
    .handle  = pMsg->handle,
    .ahandle = pMsg->ahandle,
    .pCont   = pRsp,
    .contLen = sizeof(*pRsp),
    .code    = code,
  };

  rpcSendResponse(&rpcRsp);
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
640 641
}

D
dapan1121 已提交
642 643 644 645 646 647 648 649 650 651 652 653 654
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 已提交
655 656 657 658 659
  return TSDB_CODE_SUCCESS;
}

int32_t qwBuildAndSendShowRsp(SRpcMsg *pMsg, int32_t code) {
  int32_t numOfCols = 6;
660 661 662
  int32_t msgSize = sizeof(SVShowTablesRsp) + sizeof(SSchema) * numOfCols;

  SVShowTablesRsp *pRsp = (SVShowTablesRsp *)rpcMallocCont(msgSize);
H
Haojun Liao 已提交
663 664 665 666 667

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

  const SSchema *s = tGetTbnameColumnSchema();
668
  *pSchema = createSchema(s->type, htonl(s->bytes), htonl(++cols), "name");
H
Haojun Liao 已提交
669 670 671
  pSchema++;

  int32_t type = TSDB_DATA_TYPE_TIMESTAMP;
672
  *pSchema = createSchema(type, htonl(tDataTypes[type].bytes), htonl(++cols), "created");
H
Haojun Liao 已提交
673 674 675
  pSchema++;

  type = TSDB_DATA_TYPE_SMALLINT;
676
  *pSchema = createSchema(type, htonl(tDataTypes[type].bytes), htonl(++cols), "columns");
H
Haojun Liao 已提交
677 678
  pSchema++;

679
  *pSchema = createSchema(s->type, htonl(s->bytes), htonl(++cols), "stable");
H
Haojun Liao 已提交
680 681 682
  pSchema++;

  type = TSDB_DATA_TYPE_BIGINT;
683
  *pSchema = createSchema(type, htonl(tDataTypes[type].bytes), htonl(++cols), "uid");
H
Haojun Liao 已提交
684 685 686
  pSchema++;

  type = TSDB_DATA_TYPE_INT;
687
  *pSchema = createSchema(type, htonl(tDataTypes[type].bytes), htonl(++cols), "vgId");
H
Haojun Liao 已提交
688

689
  assert(cols == numOfCols);
H
Haojun Liao 已提交
690 691 692 693 694 695
  pRsp->metaInfo.numOfColumns = htonl(cols);

  SRpcMsg rpcMsg = {
      .handle  = pMsg->handle,
      .ahandle = pMsg->ahandle,
      .pCont   = pRsp,
696
      .contLen = msgSize,
H
Haojun Liao 已提交
697 698
      .code    = code,
  };
D
dapan1121 已提交
699

H
Haojun Liao 已提交
700
  rpcSendResponse(&rpcMsg);
D
dapan1121 已提交
701
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
702 703
}

H
Haojun Liao 已提交
704 705 706 707 708 709 710 711 712 713 714 715
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 已提交
716

H
Haojun Liao 已提交
717 718 719
  rpcSendResponse(&rpcMsg);
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
720

D
dapan1121 已提交
721 722 723
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 已提交
724 725
  int32_t code = 0;

D
dapan1121 已提交
726
  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, sId, &sch, QW_NOT_EXIST_RET_ERR));
D
dapan1121 已提交
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

  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 已提交
755

D
dapan1121 已提交
756 757 758 759 760 761 762
  }

  qwReleaseScheduler(QW_READ, mgmt);

  QW_RET(code);
}

D
dapan1121 已提交
763 764 765
int32_t qwSetAndSendReadyRsp(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t queryId, uint64_t taskId, SRpcMsg *pMsg) {
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
766 767
  int32_t code = 0;

D
dapan1121 已提交
768
  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, sId, &sch, QW_NOT_EXIST_RET_ERR));
D
dapan1121 已提交
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

  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 已提交
799 800 801
int32_t qwCheckTaskCancelDrop( SQWorkerMgmt *mgmt, uint64_t sId, uint64_t queryId, uint64_t taskId, bool *needStop) {
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
802 803 804 805 806
  int32_t code = 0;
  int8_t status = JOB_TASK_STATUS_CANCELLED;

  *needStop = false;

D
dapan1121 已提交
807
  if (qwAcquireScheduler(QW_READ, mgmt, sId, &sch, QW_NOT_EXIST_RET_ERR)) {
D
dapan1121 已提交
808 809 810 811 812 813 814 815 816 817 818
    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 已提交
819 820
    qError("no cancel or drop, but task:%"PRIx64" exists", taskId);
    
D
dapan1121 已提交
821 822 823 824
    QW_UNLOCK(QW_READ, &task->lock);
    qwReleaseTask(QW_READ, sch);
    qwReleaseScheduler(QW_READ, mgmt);

D
dapan1121 已提交
825
    QW_RET(TSDB_CODE_QRY_APP_ERROR);
D
dapan1121 已提交
826 827 828 829 830 831 832 833 834 835
  }

  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 已提交
836 837 838
  }

  if (task->drop) {
D
dapan1121 已提交
839 840 841
    qwReleaseTask(QW_READ, sch);
    qwReleaseScheduler(QW_READ, mgmt);
    
D
dapan1121 已提交
842
    return qwDropTask(mgmt, sId, queryId, taskId);
D
dapan1121 已提交
843 844
  }

D
dapan1121 已提交
845 846 847
  qwReleaseTask(QW_READ, sch);
  qwReleaseScheduler(QW_READ, mgmt);

D
dapan1121 已提交
848 849 850
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
851 852 853
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 已提交
854
  int32_t code = 0;
D
dapan1121 已提交
855 856
  int32_t needRsp = true;
  void *data = NULL;
D
dapan1121 已提交
857

D
dapan1121 已提交
858
  QW_ERR_JRET(qwAcquireScheduler(QW_READ, mgmt, sId, &sch, QW_NOT_EXIST_RET_ERR));
D
dapan1121 已提交
859 860 861 862
  QW_ERR_JRET(qwAcquireTask(QW_READ, sch, queryId, taskId, &task));

  QW_LOCK(QW_READ, &task->lock);

D
dapan1121 已提交
863 864 865 866 867 868
  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 已提交
869 870 871 872 873
    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 已提交
874
    data = res->data;
D
dapan1121 已提交
875 876 877 878 879 880 881 882 883 884 885 886
    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 已提交
887 888

    needRsp = false;
D
dapan1121 已提交
889 890 891 892 893
  }

_return:
  if (task) {
    QW_UNLOCK(QW_READ, &task->lock);
D
dapan1121 已提交
894
    qwReleaseTask(QW_READ, sch);    
D
dapan1121 已提交
895 896 897
  }
  
  if (sch) {
D
dapan1121 已提交
898
    qwReleaseScheduler(QW_READ, mgmt);
D
dapan1121 已提交
899 900
  }

D
dapan1121 已提交
901 902 903
  if (needRsp) {
    qwBuildAndSendFetchRsp(pMsg, res->data);
  }
D
dapan1121 已提交
904 905 906 907

  QW_RET(code);
}

D
dapan1121 已提交
908 909 910
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 已提交
911 912 913
  int32_t code = 0;
  int8_t newStatus = JOB_TASK_STATUS_CANCELLED;

D
dapan1121 已提交
914
  code = qwAcquireScheduler(QW_READ, mgmt, sId, &sch, QW_NOT_EXIST_ADD);
D
dapan1121 已提交
915
  if (code) {
D
dapan1121 已提交
916
    qError("sId:%"PRIx64" not in cache", sId);
D
dapan1121 已提交
917 918 919
    QW_ERR_RET(code);
  }

D
dapan1121 已提交
920
  code = qwAcquireTask(QW_READ, sch, qId, tId, &task);
D
dapan1121 已提交
921 922
  if (code) {
    qwReleaseScheduler(QW_READ, mgmt);
D
dapan1121 已提交
923 924 925 926 927 928 929
    
    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 已提交
930 931 932 933 934 935
  }

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

  if (task->drop) {
D
dapan1121 已提交
939 940 941
    qwReleaseTask(QW_READ, sch);
    qwReleaseScheduler(QW_READ, mgmt);
    
D
dapan1121 已提交
942
    qwDropTask(mgmt, sId, qId, tId);
D
dapan1121 已提交
943 944

    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
945 946 947
  }

  if (!(task->cancel || task->drop)) {
D
dapan1121 已提交
948 949 950 951 952 953 954 955 956 957 958
    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 已提交
959 960 961 962 963

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 已提交
964
    QW_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
965 966 967 968 969 970 971 972 973 974
  }

  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 已提交
975 976
  mgmt->schHash = taosHashInit(mgmt->cfg.maxSchedulerNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_NO_LOCK);
  if (NULL == mgmt->schHash) {
D
dapan1121 已提交
977 978 979 980
    tfree(mgmt);
    QW_ERR_LRET(TSDB_CODE_QRY_OUT_OF_MEMORY, "init %d schduler hash failed", mgmt->cfg.maxSchedulerNum);
  }

D
dapan1121 已提交
981
  mgmt->resHash = taosHashInit(mgmt->cfg.maxResCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
D
dapan1121 已提交
982
  if (NULL == mgmt->resHash) {
D
dapan1121 已提交
983 984
    taosHashCleanup(mgmt->schHash);
    mgmt->schHash = NULL;
D
dapan1121 已提交
985 986 987 988 989 990 991 992 993 994
    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 已提交
995 996
int32_t qWorkerProcessQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
997
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
998 999
  }

D
dapan1121 已提交
1000
  int32_t code = 0;
D
dapan1121 已提交
1001 1002
  SSubQueryMsg *msg = pMsg->pCont;
  if (NULL == msg || pMsg->contLen <= sizeof(*msg)) {
D
dapan1121 已提交
1003
    qError("invalid query msg");
D
dapan1121 已提交
1004
    QW_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
1005
  }
D
dapan 已提交
1006

D
dapan1121 已提交
1007
  msg->sId = htobe64(msg->sId);
D
dapan 已提交
1008 1009 1010
  msg->queryId = htobe64(msg->queryId);
  msg->taskId = htobe64(msg->taskId);
  msg->contentLen = ntohl(msg->contentLen);
D
dapan1121 已提交
1011 1012
  
  bool queryDone = false;
D
dapan1121 已提交
1013
  bool queryRsped = false;
D
dapan1121 已提交
1014
  bool needStop = false;
1015
  struct SSubplan *plan = NULL;
D
dapan1121 已提交
1016

D
dapan1121 已提交
1017
  QW_ERR_JRET(qwCheckTaskCancelDrop(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId, &needStop));
D
dapan1121 已提交
1018 1019
  if (needStop) {
    qWarn("task need stop");
D
dapan1121 已提交
1020
    QW_ERR_JRET(TSDB_CODE_QRY_TASK_CANCELLED);
D
dapan1121 已提交
1021 1022 1023
  }
  
  code = qStringToSubplan(msg->msg, &plan);
D
dapan1121 已提交
1024
  if (TSDB_CODE_SUCCESS != code) {
D
dapan1121 已提交
1025
    qError("schId:%"PRIx64",qId:%"PRIx64",taskId:%"PRIx64" string to subplan failed, code:%d", msg->sId, msg->queryId, msg->taskId, code);
D
dapan1121 已提交
1026
    QW_ERR_JRET(code);
D
dapan1121 已提交
1027 1028
  }

1029 1030
  qTaskInfo_t pTaskInfo = NULL;
  code = qCreateExecTask(node, 0, (struct SSubplan *)plan, &pTaskInfo);
D
dapan1121 已提交
1031 1032 1033 1034 1035
  //TODO call executer to init subquery
  
  if (code) {
    QW_ERR_JRET(code);
  } else {
D
dapan1121 已提交
1036
    QW_ERR_JRET(qwAddTask(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId, JOB_TASK_STATUS_EXECUTING, QW_EXIST_RET_ERR, NULL, NULL));
D
dapan1121 已提交
1037
  }
D
dapan1121 已提交
1038

D
dapan1121 已提交
1039
  QW_ERR_JRET(qwBuildAndSendQueryRsp(pMsg, TSDB_CODE_SUCCESS));
D
dapan1121 已提交
1040

D
dapan1121 已提交
1041
  queryRsped = true;
D
dapan1121 已提交
1042
 
D
dapan1121 已提交
1043
  //TODO call executer to execute subquery
1044
  code = qExecTask(pTaskInfo);
D
dapan1121 已提交
1045
  void *data = NULL;
D
dapan1121 已提交
1046
  queryDone = false;
D
dapan1121 已提交
1047 1048
  //TODO call executer to execute subquery

D
dapan1121 已提交
1049 1050 1051
  if (code) {
    QW_ERR_JRET(code);
  } else {
D
dapan1121 已提交
1052
    QW_ERR_JRET(qwAddTaskResCache(qWorkerMgmt, msg->queryId, msg->taskId, data));
D
dapan1121 已提交
1053

D
dapan1121 已提交
1054 1055
    QW_ERR_JRET(qwUpdateTaskStatus(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId, JOB_TASK_STATUS_PARTIAL_SUCCEED));
  } 
D
dapan1121 已提交
1056 1057 1058

_return:

D
dapan1121 已提交
1059 1060
  if (queryRsped) {
    code = qwCheckAndSendReadyRsp(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId, pMsg, code);
D
dapan1121 已提交
1061 1062
  } else {
    code = qwBuildAndSendQueryRsp(pMsg, code);
D
dapan1121 已提交
1063
  }
D
dapan1121 已提交
1064 1065
  
  int8_t status = 0;
D
dapan1121 已提交
1066 1067 1068 1069 1070 1071
  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 已提交
1072
  }
D
dapan1121 已提交
1073 1074

  qwQueryPostProcess(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId, status, code);
D
dapan1121 已提交
1075
  
D
dapan1121 已提交
1076
  QW_RET(code);
D
dapan1121 已提交
1077 1078
}

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

D
dapan1121 已提交
1084
  SResReadyMsg *msg = pMsg->pCont;
D
dapan1121 已提交
1085
  if (NULL == msg || pMsg->contLen < sizeof(*msg)) {
D
dapan1121 已提交
1086
    qError("invalid task status msg");  
D
dapan1121 已提交
1087 1088
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }  
D
dapan1121 已提交
1089

D
dapan1121 已提交
1090 1091 1092 1093 1094
  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 已提交
1095 1096 1097 1098
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1099 1100
int32_t qWorkerProcessStatusMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
1101 1102 1103
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

D
dapan1121 已提交
1104
  int32_t code = 0;
D
dapan1121 已提交
1105
  SSchTasksStatusMsg *msg = pMsg->pCont;
D
dapan1121 已提交
1106
  if (NULL == msg || pMsg->contLen < sizeof(*msg)) {
D
dapan1121 已提交
1107
    qError("invalid task status msg");
D
dapan1121 已提交
1108 1109 1110
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }  

D
dapan1121 已提交
1111 1112
  msg->sId = htobe64(msg->sId);

D
dapan1121 已提交
1113 1114
  SSchedulerStatusRsp *sStatus = NULL;
  
D
dapan1121 已提交
1115
  QW_ERR_JRET(qwGetSchTasksStatus(qWorkerMgmt, msg->sId, &sStatus));
D
dapan1121 已提交
1116 1117

_return:
D
dapan1121 已提交
1118

D
dapan1121 已提交
1119 1120
  QW_ERR_RET(qwBuildAndSendStatusRsp(pMsg, sStatus));

D
dapan1121 已提交
1121 1122 1123
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1124 1125
int32_t qWorkerProcessFetchMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
1126 1127 1128
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

D
dapan1121 已提交
1129
  SResFetchMsg *msg = pMsg->pCont;
D
dapan1121 已提交
1130
  if (NULL == msg || pMsg->contLen < sizeof(*msg)) {
D
dapan1121 已提交
1131 1132 1133
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }  

D
dapan1121 已提交
1134 1135 1136 1137 1138
  msg->sId = htobe64(msg->sId);
  msg->queryId = htobe64(msg->queryId);
  msg->taskId = htobe64(msg->taskId);

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

  void *data = NULL;
D
dapan1121 已提交
1141 1142
  SQWorkerResCache *res = NULL;
  int32_t code = 0;
D
dapan1121 已提交
1143
  
D
dapan1121 已提交
1144 1145
  QW_ERR_RET(qwAcquireTaskResCache(QW_READ, qWorkerMgmt, msg->queryId, msg->taskId, &res));

D
dapan1121 已提交
1146
  QW_ERR_JRET(qwHandleFetch(res, qWorkerMgmt, msg->sId, msg->queryId, msg->taskId, pMsg));
D
dapan1121 已提交
1147 1148 1149 1150 1151 1152 1153 1154

_return:

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

D
dapan1121 已提交
1155 1156
int32_t qWorkerProcessCancelMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
1157 1158 1159
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

D
dapan1121 已提交
1160
  int32_t code = 0;
D
dapan1121 已提交
1161
  STaskCancelMsg *msg = pMsg->pCont;
D
dapan1121 已提交
1162
  if (NULL == msg || pMsg->contLen < sizeof(*msg)) {
D
dapan1121 已提交
1163
    qError("invalid task cancel msg");  
D
dapan1121 已提交
1164 1165 1166
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }  

D
dapan1121 已提交
1167 1168 1169 1170 1171
  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 已提交
1172

D
dapan1121 已提交
1173 1174 1175
_return:

  QW_ERR_RET(qwBuildAndSendCancelRsp(pMsg, code));
D
dapan1121 已提交
1176 1177 1178 1179

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1180 1181
int32_t qWorkerProcessDropMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
1182 1183 1184
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

D
dapan1121 已提交
1185
  int32_t code = 0;
D
dapan1121 已提交
1186
  STaskDropMsg *msg = pMsg->pCont;
D
dapan1121 已提交
1187
  if (NULL == msg || pMsg->contLen < sizeof(*msg)) {
D
dapan1121 已提交
1188
    qError("invalid task drop msg");
D
dapan1121 已提交
1189 1190 1191
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }  

D
dapan1121 已提交
1192 1193 1194 1195 1196
  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 已提交
1197 1198

_return:
D
dapan1121 已提交
1199

D
dapan1121 已提交
1200
  QW_ERR_RET(qwBuildAndSendDropRsp(pMsg, code));
D
dapan1121 已提交
1201 1202 1203 1204

  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
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 已提交
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
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 已提交
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
void qWorkerDestroy(void **qWorkerMgmt) {
  if (NULL == qWorkerMgmt || NULL == *qWorkerMgmt) {
    return;
  }

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

  //TODO FREE ALL

  tfree(*qWorkerMgmt);
}