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

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

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

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

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

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

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

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

  (*rsp)->num = taskNum;

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
313 314


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

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

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

  qwReleaseScheduler(QW_READ, mgmt);

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

  QW_RET(code);
}


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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

  rpcSendResponse(&rpcRsp);

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

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

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

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

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

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

  rpcSendResponse(&rpcRsp);

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

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

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

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
623 624
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  qwReleaseScheduler(QW_READ, mgmt);

  QW_RET(code);
}

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

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

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

  *needStop = false;

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

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

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

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

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

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

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

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

  QW_LOCK(QW_READ, &task->lock);

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

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

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

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

  QW_RET(code);
}

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

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

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

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

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

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

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

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

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

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

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

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

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

D
dapan1121 已提交
1028 1029 1030 1031 1032 1033 1034
  //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 已提交
1035
    QW_ERR_JRET(qwAddTask(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId, JOB_TASK_STATUS_EXECUTING, QW_EXIST_RET_ERR, NULL, NULL));
D
dapan1121 已提交
1036
  }
D
dapan1121 已提交
1037

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

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

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

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

_return:

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

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

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

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

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

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

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

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

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

_return:
D
dapan1121 已提交
1117

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

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

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

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

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

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

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

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

_return:

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

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

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

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

D
dapan1121 已提交
1172 1173 1174
_return:

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

  return TSDB_CODE_SUCCESS;
}

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

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

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

_return:
D
dapan1121 已提交
1198

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

  return TSDB_CODE_SUCCESS;
}

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

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

  //TODO FREE ALL

  tfree(*qWorkerMgmt);
}