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

D
dapan1121 已提交
11
int32_t qwValidateStatus(SQWorkerMgmt *mgmt, int8_t oriStatus, int8_t newStatus, uint64_t sId, uint64_t qId, uint64_t tId) {
D
dapan1121 已提交
12
  int32_t code = 0;
D
dapan1121 已提交
13

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

D
dapan1121 已提交
69
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
70

D
dapan1121 已提交
71
_return:
D
dapan1121 已提交
72

D
dapan1121 已提交
73 74
  QW_TASK_ELOG("invalid task status update from %d to %d", oriStatus, newStatus);
  QW_RET(code);
D
dapan1121 已提交
75 76
}

D
dapan1121 已提交
77
int32_t qwUpdateTaskInfo(SQWorkerMgmt *mgmt, SQWTaskStatus *task, int8_t type, void *data, uint64_t sId, uint64_t qId, uint64_t tId) {
D
dapan1121 已提交
78
  int32_t code = 0;
D
dapan1121 已提交
79
  int8_t origStatus = 0;
D
dapan1121 已提交
80
  
D
dapan1121 已提交
81 82 83
  switch (type) {
    case QW_TASK_INFO_STATUS: {
      int8_t newStatus = *(int8_t *)data;
D
dapan1121 已提交
84 85 86
      QW_ERR_RET(qwValidateStatus(mgmt, task->status, newStatus, QW_IDS()));
      
      origStatus = task->status;
D
dapan1121 已提交
87
      task->status = newStatus;
D
dapan1121 已提交
88 89
      
      QW_TASK_DLOG("task status updated from %d to %d", origStatus, newStatus);
D
dapan1121 已提交
90 91 92
      break;
    }
    default:
D
dapan1121 已提交
93
      QW_TASK_ELOG("unknown task info, type:%d", type);
D
dapan1121 已提交
94
      return TSDB_CODE_QRY_APP_ERROR;
D
dapan1121 已提交
95
  }
D
dapan1121 已提交
96
  
D
dapan1121 已提交
97 98 99
  return TSDB_CODE_SUCCESS;
}

100
int32_t qwAddTaskHandlesToCache(SQWorkerMgmt *mgmt, uint64_t qId, uint64_t tId, qTaskInfo_t taskHandle, DataSinkHandle sinkHandle) {
D
dapan1121 已提交
101 102
  char id[sizeof(qId) + sizeof(tId)] = {0};
  QW_SET_QTID(id, qId, tId);
D
dapan1121 已提交
103

D
dapan1121 已提交
104
  SQWTaskCtx resCache = {0};
105 106
  resCache.taskHandle = taskHandle;
  resCache.sinkHandle = sinkHandle;
D
dapan1121 已提交
107

D
dapan1121 已提交
108 109 110
  QW_LOCK(QW_WRITE, &mgmt->ctxLock);
  if (0 != taosHashPut(mgmt->ctxHash, id, sizeof(id), &resCache, sizeof(SQWTaskCtx))) {
    QW_UNLOCK(QW_WRITE, &mgmt->ctxLock);
D
dapan1121 已提交
111
    QW_TASK_ELOG("taosHashPut task ctx to ctxHash failed, taskHandle:%p, sinkHandle:%p", taskHandle, sinkHandle);
D
dapan1121 已提交
112 113 114
    return TSDB_CODE_QRY_APP_ERROR;
  }

D
dapan1121 已提交
115
  QW_UNLOCK(QW_WRITE, &mgmt->ctxLock);
D
dapan1121 已提交
116

D
dapan1121 已提交
117 118 119
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
120
int32_t qwAddScheduler(int32_t rwType, SQWorkerMgmt *mgmt, uint64_t sId, SQWSchStatus **sch) {
D
dapan1121 已提交
121
  SQWSchStatus newSch = {0};
D
dapan1121 已提交
122 123
  newSch.tasksHash = taosHashInit(mgmt->cfg.maxSchTaskNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
  if (NULL == newSch.tasksHash) {
D
dapan1121 已提交
124
    QW_SCH_DLOG("taosHashInit %d failed", mgmt->cfg.maxSchTaskNum);
D
dapan1121 已提交
125 126 127 128 129
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
  }

  while (true) {
    QW_LOCK(QW_WRITE, &mgmt->schLock);
D
dapan1121 已提交
130
    int32_t code = taosHashPut(mgmt->schHash, &sId, sizeof(sId), &newSch, sizeof(newSch));
D
dapan1121 已提交
131 132 133
    if (0 != code) {
      if (!HASH_NODE_EXIST(code)) {
        QW_UNLOCK(QW_WRITE, &mgmt->schLock);
D
dapan1121 已提交
134
        QW_SCH_ELOG("taosHashPut new sch to scheduleHash failed, errno:%d", errno);
D
dapan1121 已提交
135 136 137 138 139 140
        taosHashCleanup(newSch.tasksHash);
        return TSDB_CODE_QRY_APP_ERROR;
      }
    }
    
    QW_UNLOCK(QW_WRITE, &mgmt->schLock);
D
dapan1121 已提交
141 142 143 144 145
    if (TSDB_CODE_SUCCESS == qwAcquireScheduler(rwType, mgmt, sId, sch)) {
      if (code) {
        taosHashCleanup(newSch.tasksHash);
      }
      
D
dapan1121 已提交
146 147 148
      return TSDB_CODE_SUCCESS;
    }
  }
D
dapan1121 已提交
149 150 151 152

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
153
int32_t qwAcquireSchedulerImpl(int32_t rwType, SQWorkerMgmt *mgmt, uint64_t sId, SQWSchStatus **sch, int32_t nOpt) {
D
dapan1121 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
  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 已提交
171
int32_t qwAcquireAddScheduler(int32_t rwType, SQWorkerMgmt *mgmt, uint64_t sId, SQWSchStatus **sch) {
D
dapan1121 已提交
172 173 174
  return qwAcquireSchedulerImpl(rwType, mgmt, sId, sch, QW_NOT_EXIST_ADD);
}

D
dapan1121 已提交
175
int32_t qwAcquireScheduler(int32_t rwType, SQWorkerMgmt *mgmt, uint64_t sId, SQWSchStatus **sch) {
D
dapan1121 已提交
176 177 178
  return qwAcquireSchedulerImpl(rwType, mgmt, sId, sch, QW_NOT_EXIST_RET_ERR);
}

D
dapan1121 已提交
179
void qwReleaseScheduler(int32_t rwType, SQWorkerMgmt *mgmt) {
D
dapan1121 已提交
180 181 182
  QW_UNLOCK(rwType, &mgmt->schLock);
}

D
dapan1121 已提交
183 184 185
int32_t qwAddTaskImpl(SQWorkerMgmt *mgmt, SQWSchStatus *sch, int32_t rwType, uint64_t qId, uint64_t tId, int32_t status, int32_t eOpt, SQWTaskStatus **task) {
  int32_t code = 0;

D
dapan1121 已提交
186 187
  char id[sizeof(qId) + sizeof(tId)] = {0};
  QW_SET_QTID(id, qId, tId);
D
dapan1121 已提交
188

D
dapan1121 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  SQWTaskStatus ntask = {0};
  ntask.status = status;

  QW_LOCK(QW_WRITE, &sch->tasksLock);
  code = taosHashPut(sch->tasksHash, id, sizeof(id), &ntask, sizeof(ntask));
  if (0 != code) {
    QW_UNLOCK(QW_WRITE, &sch->tasksLock);
    if (HASH_NODE_EXIST(code)) {
      if (QW_EXIST_ACQUIRE == eOpt && rwType && task) {
        QW_ERR_RET(qwAcquireTask(mgmt, rwType, sch, qId, tId, task));
      } else if (QW_EXIST_RET_ERR == eOpt) {
        return TSDB_CODE_QRY_TASK_ALREADY_EXIST;
      } else {
        assert(0);
      }
    } else {
      qError("taosHashPut queryId[%"PRIx64"] taskId[%"PRIx64"] to scheduleHash failed", qId, tId);
      return TSDB_CODE_QRY_APP_ERROR;
    }
  }
  
  QW_UNLOCK(QW_WRITE, &sch->tasksLock);
D
dapan1121 已提交
211

D
dapan1121 已提交
212 213
  if (QW_EXIST_ACQUIRE == eOpt && rwType && task) {
    QW_ERR_RET(qwAcquireTask(mgmt, rwType, sch, qId, tId, task));
D
dapan1121 已提交
214 215
  }

D
dapan1121 已提交
216 217
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
218

D
dapan1121 已提交
219 220 221 222
int32_t qwAddTask(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t qId, uint64_t tId, int32_t status) {
  SQWSchStatus *tsch = NULL;
  int32_t code = 0;
  QW_ERR_RET(qwAcquireAddScheduler(QW_READ, mgmt, sId, &tsch));
D
dapan1121 已提交
223

D
dapan1121 已提交
224
  QW_ERR_JRET(qwAddTaskImpl(mgmt, tsch, 0, qId, tId, status, QW_EXIST_RET_ERR, NULL));
D
dapan1121 已提交
225 226 227 228 229

_return:

  qwReleaseScheduler(QW_READ, mgmt);
  QW_ERR_RET(code);
D
dapan1121 已提交
230 231 232
}


D
dapan1121 已提交
233
int32_t qwAcquireTaskImpl(SQWorkerMgmt *mgmt, int32_t rwType, SQWSchStatus *sch, uint64_t qId, uint64_t tId, int32_t status, int32_t nOpt, SQWTaskStatus **task) {
D
dapan1121 已提交
234 235 236
  char id[sizeof(qId) + sizeof(tId)] = {0};
  QW_SET_QTID(id, qId, tId);

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

D
dapan1121 已提交
242 243 244 245
    if (QW_NOT_EXIST_ADD == nOpt) {
      QW_ERR_RET(qwAddTaskImpl(mgmt, sch, rwType, qId, tId, status, QW_EXIST_ACQUIRE, task));
    } else if (QW_NOT_EXIST_RET_ERR == nOpt) {
      return TSDB_CODE_QRY_TASK_NOT_EXIST;
D
dapan1121 已提交
246
    } else {
D
dapan1121 已提交
247
      assert(0);
D
dapan1121 已提交
248
    }
D
dapan1121 已提交
249
  }
D
dapan1121 已提交
250 251 252 253

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
254 255 256
int32_t qwAcquireTask(SQWorkerMgmt *mgmt, int32_t rwType, SQWSchStatus *sch, uint64_t qId, uint64_t tId, SQWTaskStatus **task) {
  return qwAcquireTaskImpl(mgmt, rwType, sch, qId, tId, 0, QW_NOT_EXIST_RET_ERR, task);
}
D
dapan1121 已提交
257

D
dapan1121 已提交
258 259 260
int32_t qwAcquireAddTask(SQWorkerMgmt *mgmt, int32_t rwType, SQWSchStatus *sch, uint64_t qId, uint64_t tId, int32_t status, SQWTaskStatus **task) {
  return qwAcquireTaskImpl(mgmt, rwType, sch, qId, tId, status, QW_NOT_EXIST_ADD, task);
}
D
dapan1121 已提交
261 262


D
dapan1121 已提交
263 264
void qwReleaseTask(int32_t rwType, SQWSchStatus *sch) {
  QW_UNLOCK(rwType, &sch->tasksLock);
D
dapan1121 已提交
265 266
}

D
dapan1121 已提交
267 268

int32_t qwAcquireTaskCtx(int32_t rwType, SQWorkerMgmt *mgmt, uint64_t queryId, uint64_t taskId, SQWTaskCtx **handles) {
D
dapan1121 已提交
269 270 271
  char id[sizeof(queryId) + sizeof(taskId)] = {0};
  QW_SET_QTID(id, queryId, taskId);
  
D
dapan1121 已提交
272 273
  QW_LOCK(rwType, &mgmt->ctxLock);
  *handles = taosHashGet(mgmt->ctxHash, id, sizeof(id));
274
  if (NULL == (*handles)) {
D
dapan1121 已提交
275
    QW_UNLOCK(rwType, &mgmt->ctxLock);
D
dapan1121 已提交
276 277 278 279 280 281
    return TSDB_CODE_QRY_RES_CACHE_NOT_EXIST;
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
282
void qwReleaseTaskResCache(int32_t rwType, SQWorkerMgmt *mgmt) {
D
dapan1121 已提交
283
  QW_UNLOCK(rwType, &mgmt->ctxLock);
D
dapan1121 已提交
284 285 286
}


D
dapan1121 已提交
287 288
int32_t qwGetSchTasksStatus(SQWorkerMgmt *mgmt, uint64_t sId, SSchedulerStatusRsp **rsp) {
  SQWSchStatus *sch = NULL;
D
dapan1121 已提交
289 290
  int32_t taskNum = 0;

D
dapan1121 已提交
291
  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, sId, &sch));
D
dapan1121 已提交
292 293
  
  sch->lastAccessTs = taosGetTimestampSec();
D
dapan1121 已提交
294

D
dapan1121 已提交
295 296 297
  QW_LOCK(QW_READ, &sch->tasksLock);
  
  taskNum = taosHashGetSize(sch->tasksHash);
D
dapan1121 已提交
298
  
D
dapan1121 已提交
299 300 301 302
  int32_t size = sizeof(SSchedulerStatusRsp) + sizeof((*rsp)->status[0]) * taskNum;
  *rsp = calloc(1, size);
  if (NULL == *rsp) {
    qError("calloc %d failed", size);
D
dapan1121 已提交
303 304
    QW_UNLOCK(QW_READ, &sch->tasksLock);
    qwReleaseScheduler(QW_READ, mgmt);
D
dapan1121 已提交
305
    
D
dapan1121 已提交
306 307 308 309 310
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
  }

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

D
dapan1121 已提交
313 314 315 316
  void *pIter = taosHashIterate(sch->tasksHash, NULL);
  while (pIter) {
    SQWTaskStatus *taskStatus = (SQWTaskStatus *)pIter;
    taosHashGetKey(pIter, &key, &keyLen);
D
dapan1121 已提交
317

D
dapan1121 已提交
318 319 320 321 322
    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 已提交
323

D
dapan1121 已提交
324 325
  QW_UNLOCK(QW_READ, &sch->tasksLock);
  qwReleaseScheduler(QW_READ, mgmt);
D
dapan1121 已提交
326 327 328 329 330 331

  (*rsp)->num = taskNum;

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
332 333


D
dapan1121 已提交
334 335
int32_t qwUpdateSchLastAccess(SQWorkerMgmt *mgmt, uint64_t sId) {
  SQWSchStatus *sch = NULL;
D
dapan1121 已提交
336

D
dapan1121 已提交
337
  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, sId, &sch));
D
dapan1121 已提交
338

D
dapan1121 已提交
339
  sch->lastAccessTs = taosGetTimestampSec();
D
dapan1121 已提交
340 341 342 343 344 345

  qwReleaseScheduler(QW_READ, mgmt);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
346 347 348
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 已提交
349 350
  int32_t code = 0;

D
dapan1121 已提交
351
  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, sId, &sch));
D
dapan1121 已提交
352

D
dapan1121 已提交
353
  QW_ERR_JRET(qwAcquireTask(mgmt, QW_READ, sch, qId, tId, &task));
D
dapan1121 已提交
354

D
dapan1121 已提交
355
  QW_LOCK(QW_WRITE, &task->lock);
D
dapan1121 已提交
356
  qwUpdateTaskInfo(mgmt, task, QW_TASK_INFO_STATUS, &status, QW_IDS());
D
dapan1121 已提交
357 358
  QW_UNLOCK(QW_WRITE, &task->lock);
  
D
dapan1121 已提交
359 360
_return:

D
dapan1121 已提交
361 362
  qwReleaseTask(QW_READ, sch);
  qwReleaseScheduler(QW_READ, mgmt);
D
dapan1121 已提交
363 364 365 366 367

  QW_RET(code);
}


D
dapan1121 已提交
368 369 370
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 已提交
371 372
  int32_t code = 0;
  
D
dapan1121 已提交
373
  if (qwAcquireScheduler(QW_READ, mgmt, sId, &sch)) {
D
dapan1121 已提交
374 375
    *taskStatus = JOB_TASK_STATUS_NULL;
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
376 377
  }

D
dapan1121 已提交
378
  if (qwAcquireTask(mgmt, QW_READ, sch, queryId, taskId, &task)) {
D
dapan1121 已提交
379
    qwReleaseScheduler(QW_READ, mgmt);
D
dapan1121 已提交
380
    
D
dapan1121 已提交
381 382
    *taskStatus = JOB_TASK_STATUS_NULL;
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
383 384
  }

D
dapan1121 已提交
385
  *taskStatus = task->status;
D
dapan1121 已提交
386 387

  qwReleaseTask(QW_READ, sch);
D
dapan1121 已提交
388 389
  qwReleaseScheduler(QW_READ, mgmt);

D
dapan1121 已提交
390 391 392 393
  QW_RET(code);
}


D
dapan1121 已提交
394
int32_t qwCancelTask(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t qId, uint64_t tId) {
D
dapan1121 已提交
395 396
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
397 398
  int32_t code = 0;

D
dapan1121 已提交
399 400 401
  QW_ERR_RET(qwAcquireAddScheduler(QW_READ, mgmt, sId, &sch));

  QW_ERR_JRET(qwAcquireAddTask(mgmt, QW_READ, sch, qId, tId, JOB_TASK_STATUS_NOT_START, &task));
D
dapan1121 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419


  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;
D
dapan1121 已提交
420
    QW_ERR_JRET(qwUpdateTaskInfo(mgmt, task, QW_TASK_INFO_STATUS, &newStatus, QW_IDS()));
D
dapan1121 已提交
421 422
  } else {
    newStatus = JOB_TASK_STATUS_CANCELLING;
D
dapan1121 已提交
423
    QW_ERR_JRET(qwUpdateTaskInfo(mgmt, task, QW_TASK_INFO_STATUS, &newStatus, QW_IDS()));
D
dapan1121 已提交
424 425 426
  }

  QW_UNLOCK(QW_WRITE, &task->lock);
D
dapan1121 已提交
427
  
D
dapan1121 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
  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 已提交
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

// caller should make sure task is not running
int32_t qwDropTaskCtx(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t qId, uint64_t tId) {
  char id[sizeof(qId) + sizeof(tId)] = {0};
  QW_SET_QTID(id, qId, tId);

  QW_LOCK(QW_WRITE, &mgmt->ctxLock);
  SQWTaskCtx *ctx = taosHashGet(mgmt->ctxHash, id, sizeof(id));
  if (NULL == ctx) {
    QW_UNLOCK(QW_WRITE, &mgmt->ctxLock);
    return TSDB_CODE_QRY_RES_CACHE_NOT_EXIST;
  }

  if (ctx->taskHandle) {
    qDestroyTask(ctx->taskHandle);
    ctx->taskHandle = NULL;
  }

  if (ctx->sinkHandle) {
    dsDestroyDataSinker(ctx->sinkHandle);
    ctx->sinkHandle = NULL;
  }
  
  if (taosHashRemove(mgmt->ctxHash, id, sizeof(id))) {
    QW_TASK_ELOG("taosHashRemove from ctx hash failed, id:%s", id);
    
    QW_UNLOCK(QW_WRITE, &mgmt->ctxLock);
    return TSDB_CODE_QRY_RES_CACHE_NOT_EXIST;
  }
  
  QW_UNLOCK(QW_WRITE, &mgmt->ctxLock);

  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
488
int32_t qwDropTask(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t qId, uint64_t tId) {
D
dapan1121 已提交
489 490
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
491
  int32_t code = 0;
D
dapan1121 已提交
492 493 494
  
  char id[sizeof(qId) + sizeof(tId)] = {0};
  QW_SET_QTID(id, qId, tId);
D
dapan1121 已提交
495

D
dapan1121 已提交
496
  qwDropTaskCtx(mgmt, sId, qId, tId);
D
dapan1121 已提交
497
  
D
dapan1121 已提交
498 499
  if (qwAcquireScheduler(QW_WRITE, mgmt, sId, &sch)) {
    QW_TASK_WLOG("scheduler does not exist, sch:%p", sch);
D
dapan1121 已提交
500 501 502
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
503
  if (qwAcquireTask(mgmt, QW_WRITE, sch, qId, tId, &task)) {
D
dapan1121 已提交
504 505
    qwReleaseScheduler(QW_WRITE, mgmt);
    
D
dapan1121 已提交
506
    QW_TASK_WLOG("task does not exist, task:%p", task);
D
dapan1121 已提交
507 508 509
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
510 511 512 513 514 515 516 517
  QW_TASK_DLOG("drop task, status:%d, code:%x, ready:%d, cancel:%d, drop:%d", task->status, task->code, task->ready, task->cancel, task->drop);

  if (taosHashRemove(sch->tasksHash, id, sizeof(id))) {
    QW_TASK_ELOG("taosHashRemove task from hash failed, task:%p", task);
    QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
  }

_return:
D
dapan1121 已提交
518 519 520 521

  qwReleaseTask(QW_WRITE, sch);
  qwReleaseScheduler(QW_WRITE, mgmt);
  
D
dapan1121 已提交
522
  QW_RET(code);
D
dapan1121 已提交
523 524
}

D
dapan1121 已提交
525
int32_t qwCancelDropTask(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t qId, uint64_t tId) {
D
dapan1121 已提交
526 527
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
528 529
  int32_t code = 0;

D
dapan1121 已提交
530
  QW_ERR_RET(qwAcquireAddScheduler(QW_READ, mgmt, sId, &sch));
D
dapan1121 已提交
531

D
dapan1121 已提交
532
  QW_ERR_JRET(qwAcquireAddTask(mgmt, QW_READ, sch, qId, tId, JOB_TASK_STATUS_NOT_START, &task));
D
dapan1121 已提交
533 534 535 536 537 538 539 540 541

  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 已提交
542
    newStatus = JOB_TASK_STATUS_DROPPING;
D
dapan1121 已提交
543
    QW_ERR_JRET(qwUpdateTaskInfo(mgmt, task, QW_TASK_INFO_STATUS, &newStatus, QW_IDS()));
D
dapan1121 已提交
544 545 546 547 548 549 550 551 552 553 554
  } 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 已提交
555
    QW_ERR_RET(qwDropTask(mgmt, sId, qId, tId));
D
dapan1121 已提交
556 557 558 559
    return TSDB_CODE_SUCCESS;
  }

  QW_UNLOCK(QW_WRITE, &task->lock);
D
dapan1121 已提交
560
  
D
dapan1121 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
  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 已提交
586 587
  SQueryTableRsp *pRsp = (SQueryTableRsp *)rpcMallocCont(sizeof(SQueryTableRsp));
  pRsp->code = code;
D
dapan1121 已提交
588

D
dapan1121 已提交
589 590
  SRpcMsg rpcRsp = {
    .handle  = pMsg->handle,
D
dapan1121 已提交
591
    .ahandle = pMsg->ahandle,
D
dapan1121 已提交
592 593 594 595 596 597 598 599
    .pCont   = pRsp,
    .contLen = sizeof(*pRsp),
    .code    = code,
  };

  rpcSendResponse(&rpcRsp);

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
600 601 602
}

int32_t qwBuildAndSendReadyRsp(SRpcMsg *pMsg, int32_t code) {
D
dapan1121 已提交
603 604 605 606 607 608 609 610 611 612 613 614
  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 已提交
615

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

int32_t qwBuildAndSendStatusRsp(SRpcMsg *pMsg, SSchedulerStatusRsp *sStatus) {
D
dapan1121 已提交
620 621 622 623 624 625 626 627 628
  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 已提交
629

D
dapan1121 已提交
630 631 632 633 634 635 636
  if (sStatus) {
    memcpy(pRsp, sStatus, size);
  } else {
    pRsp->num = 0;
  }

  SRpcMsg rpcRsp = {
D
dapan1121 已提交
637
    .msgType = pMsg->msgType + 1,
D
dapan1121 已提交
638 639 640 641 642 643 644 645 646 647
    .handle  = pMsg->handle,
    .ahandle = pMsg->ahandle,
    .pCont   = pRsp,
    .contLen = size,
    .code    = 0,
  };

  rpcSendResponse(&rpcRsp);

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
648 649
}

650 651 652 653 654 655 656 657 658
int32_t qwInitFetchRsp(int32_t length, SRetrieveTableRsp **rsp) {
  int32_t msgSize = sizeof(SRetrieveTableRsp) + length;
  
  SRetrieveTableRsp *pRsp = (SRetrieveTableRsp *)rpcMallocCont(msgSize);
  if (NULL == pRsp) {
    qError("rpcMallocCont %d failed", msgSize);
    QW_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
659 660
  memset(pRsp, 0, sizeof(SRetrieveTableRsp));

D
dapan1121 已提交
661 662
  *rsp = pRsp;

663 664 665 666 667 668 669 670 671 672
  return TSDB_CODE_SUCCESS;
}


int32_t qwBuildAndSendFetchRsp(SRpcMsg *pMsg, SRetrieveTableRsp *pRsp, int32_t dataLength, int32_t code) {
  if (NULL == pRsp) {
    pRsp = (SRetrieveTableRsp *)rpcMallocCont(sizeof(SRetrieveTableRsp));
    memset(pRsp, 0, sizeof(SRetrieveTableRsp));
    dataLength = 0;
  }
D
dapan1121 已提交
673 674 675 676 677

  SRpcMsg rpcRsp = {
    .handle  = pMsg->handle,
    .ahandle = pMsg->ahandle,
    .pCont   = pRsp,
678 679
    .contLen = sizeof(*pRsp) + dataLength,
    .code    = code,
D
dapan1121 已提交
680
  };
D
dapan1121 已提交
681

D
dapan1121 已提交
682 683 684
  rpcSendResponse(&rpcRsp);

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
685 686
}

D
dapan1121 已提交
687 688 689
int32_t qwBuildAndSendCancelRsp(SRpcMsg *pMsg, int32_t code) {
  STaskCancelRsp *pRsp = (STaskCancelRsp *)rpcMallocCont(sizeof(STaskCancelRsp));
  pRsp->code = code;
D
dapan1121 已提交
690

D
dapan1121 已提交
691 692 693 694 695 696 697 698 699 700
  SRpcMsg rpcRsp = {
    .handle  = pMsg->handle,
    .ahandle = pMsg->ahandle,
    .pCont   = pRsp,
    .contLen = sizeof(*pRsp),
    .code    = code,
  };

  rpcSendResponse(&rpcRsp);
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
701 702
}

D
dapan1121 已提交
703 704 705 706 707 708 709 710 711 712 713 714 715
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 已提交
716 717 718 719 720
  return TSDB_CODE_SUCCESS;
}

int32_t qwBuildAndSendShowRsp(SRpcMsg *pMsg, int32_t code) {
  int32_t numOfCols = 6;
721 722 723
  int32_t msgSize = sizeof(SVShowTablesRsp) + sizeof(SSchema) * numOfCols;

  SVShowTablesRsp *pRsp = (SVShowTablesRsp *)rpcMallocCont(msgSize);
H
Haojun Liao 已提交
724 725 726 727 728

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

  const SSchema *s = tGetTbnameColumnSchema();
729
  *pSchema = createSchema(s->type, htonl(s->bytes), htonl(++cols), "name");
H
Haojun Liao 已提交
730 731 732
  pSchema++;

  int32_t type = TSDB_DATA_TYPE_TIMESTAMP;
733
  *pSchema = createSchema(type, htonl(tDataTypes[type].bytes), htonl(++cols), "created");
H
Haojun Liao 已提交
734 735 736
  pSchema++;

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

740
  *pSchema = createSchema(s->type, htonl(s->bytes), htonl(++cols), "stable");
H
Haojun Liao 已提交
741 742 743
  pSchema++;

  type = TSDB_DATA_TYPE_BIGINT;
744
  *pSchema = createSchema(type, htonl(tDataTypes[type].bytes), htonl(++cols), "uid");
H
Haojun Liao 已提交
745 746 747
  pSchema++;

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

750
  assert(cols == numOfCols);
H
Haojun Liao 已提交
751 752 753 754 755 756
  pRsp->metaInfo.numOfColumns = htonl(cols);

  SRpcMsg rpcMsg = {
      .handle  = pMsg->handle,
      .ahandle = pMsg->ahandle,
      .pCont   = pRsp,
757
      .contLen = msgSize,
H
Haojun Liao 已提交
758 759
      .code    = code,
  };
D
dapan1121 已提交
760

H
Haojun Liao 已提交
761
  rpcSendResponse(&rpcMsg);
D
dapan1121 已提交
762
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
763 764
}

H
Haojun Liao 已提交
765 766 767 768 769 770 771 772 773 774 775 776
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 已提交
777

H
Haojun Liao 已提交
778 779 780
  rpcSendResponse(&rpcMsg);
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
781

D
dapan1121 已提交
782
int32_t qwCheckAndSendReadyRsp(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t qId, uint64_t tId, SRpcMsg *pMsg) {
D
dapan1121 已提交
783 784
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
785 786
  int32_t code = 0;

D
dapan1121 已提交
787
  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, sId, &sch));
D
dapan1121 已提交
788

D
dapan1121 已提交
789
  QW_ERR_JRET(qwAcquireTask(mgmt, QW_READ, sch, qId, tId, &task));
D
dapan1121 已提交
790 791 792 793

  QW_LOCK(QW_WRITE, &task->lock);

  if (QW_READY_NOT_RECEIVED == task->ready) {
D
dapan1121 已提交
794 795 796 797 798 799
    QW_SCH_TASK_DLOG("ready not received, ready:%d", task->ready);
    goto _return;
  } else if (QW_READY_RECEIVED == task->ready) {
    task->ready = QW_READY_RESPONSED;
    int32_t rspCode = task->code;
    
D
dapan1121 已提交
800 801 802 803
    QW_UNLOCK(QW_WRITE, &task->lock);
    qwReleaseTask(QW_READ, sch);
    qwReleaseScheduler(QW_READ, mgmt);
    
D
dapan1121 已提交
804 805 806
    QW_ERR_RET(qwBuildAndSendReadyRsp(pMsg, rspCode));
    
    QW_SCH_TASK_DLOG("ready response sent, ready:%d", task->ready);
D
dapan1121 已提交
807

D
dapan1121 已提交
808
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
809
  } else if (QW_READY_RESPONSED == task->ready) {
D
dapan1121 已提交
810
    QW_SCH_TASK_ELOG("ready response already send, ready:%d", task->ready);
D
dapan1121 已提交
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
    QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
  } else {
    assert(0);
  }

_return:

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

  qwReleaseScheduler(QW_READ, mgmt);

  QW_RET(code);
}

D
dapan1121 已提交
828
int32_t qwSetAndSendReadyRsp(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t qId, uint64_t tId, SRpcMsg *pMsg) {
D
dapan1121 已提交
829 830
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
831 832
  int32_t code = 0;

D
dapan1121 已提交
833
  QW_ERR_RET(qwAcquireScheduler(QW_READ, mgmt, sId, &sch));
D
dapan1121 已提交
834

D
dapan1121 已提交
835
  QW_ERR_JRET(qwAcquireTask(mgmt, QW_READ, sch, qId, tId, &task));
D
dapan1121 已提交
836 837 838

  QW_LOCK(QW_WRITE, &task->lock);

D
dapan1121 已提交
839 840 841 842
  int8_t status = task->status;
  int32_t errCode = task->code;
  
  if (QW_TASK_READY(status)) {
D
dapan1121 已提交
843
    task->ready = QW_READY_RESPONSED;
D
dapan1121 已提交
844 845 846 847 848 849

    QW_UNLOCK(QW_WRITE, &task->lock);
    
    QW_ERR_JRET(qwBuildAndSendReadyRsp(pMsg, errCode));

    QW_SCH_TASK_DLOG("task ready responsed, status:%d", status);
D
dapan1121 已提交
850 851
  } else {
    task->ready = QW_READY_RECEIVED;
D
dapan1121 已提交
852

D
dapan1121 已提交
853 854
    QW_UNLOCK(QW_WRITE, &task->lock);

D
dapan1121 已提交
855
    QW_SCH_TASK_DLOG("task ready NOT responsed, status:%d", status);
D
dapan1121 已提交
856 857 858 859 860 861 862 863 864 865 866 867 868
  }

_return:

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

  qwReleaseScheduler(QW_READ, mgmt);

  QW_RET(code);
}

D
dapan1121 已提交
869
int32_t qwCheckAndProcessTaskDrop(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t qId, uint64_t tId, bool *needStop) {
D
dapan1121 已提交
870 871
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
872 873 874 875 876
  int32_t code = 0;
  int8_t status = JOB_TASK_STATUS_CANCELLED;

  *needStop = false;

D
dapan1121 已提交
877
  if (qwAcquireScheduler(QW_READ, mgmt, sId, &sch)) {
D
dapan1121 已提交
878 879 880
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
881
  if (qwAcquireTask(mgmt, QW_READ, sch, qId, tId, &task)) {
D
dapan1121 已提交
882 883 884 885
    qwReleaseScheduler(QW_READ, mgmt);
    return TSDB_CODE_SUCCESS;
  }
  
D
dapan1121 已提交
886 887
  if ((!atomic_load_8(&task->cancel)) && (!atomic_load_8(&task->drop))) {
    QW_TASK_ELOG("no cancel or drop but task exists, status:%d", atomic_load_8(&task->status));
D
dapan1121 已提交
888
    QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
D
dapan1121 已提交
889 890 891 892
  }

  *needStop = true;
  
D
dapan1121 已提交
893
  if (atomic_load_8(&task->cancel)) {
D
dapan1121 已提交
894
    QW_LOCK(QW_WRITE, &task->lock);
D
dapan1121 已提交
895
    code = qwUpdateTaskInfo(mgmt, task, QW_TASK_INFO_STATUS, &status, QW_IDS());
D
dapan1121 已提交
896
    QW_UNLOCK(QW_WRITE, &task->lock);
D
dapan1121 已提交
897 898
    
    QW_ERR_JRET(code);
D
dapan1121 已提交
899 900 901
  }

  if (task->drop) {
D
dapan1121 已提交
902 903 904
    qwReleaseTask(QW_READ, sch);
    qwReleaseScheduler(QW_READ, mgmt);
    
D
dapan1121 已提交
905
    QW_RET(qwDropTask(mgmt, sId, qId, tId));
D
dapan1121 已提交
906 907
  }

D
dapan1121 已提交
908 909
_return:

D
dapan1121 已提交
910 911 912
  qwReleaseTask(QW_READ, sch);
  qwReleaseScheduler(QW_READ, mgmt);

D
dapan1121 已提交
913 914 915
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
916 917 918 919 920 921 922

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;
  int32_t code = 0;
  int8_t newStatus = JOB_TASK_STATUS_CANCELLED;

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

D
dapan1121 已提交
929
  code = qwAcquireTask(mgmt, QW_READ, sch, qId, tId, &task);
D
dapan1121 已提交
930
  if (code) {
D
dapan1121 已提交
931 932
    QW_TASK_ELOG("sId:%"PRIx64" queryId:%"PRIx64" taskId:%"PRIx64" not in cache", sId, qId, tId);
    QW_ERR_RET(code);
D
dapan1121 已提交
933 934
  }

D
dapan1121 已提交
935 936
  QW_LOCK(QW_WRITE, &task->lock);

D
dapan1121 已提交
937
  if (task->cancel) {
D
dapan1121 已提交
938
    qwUpdateTaskInfo(mgmt, task, QW_TASK_INFO_STATUS, &newStatus, QW_IDS());
D
dapan1121 已提交
939 940 941
  }

  if (task->drop) {
D
dapan1121 已提交
942 943
    QW_UNLOCK(QW_WRITE, &task->lock);
    
D
dapan1121 已提交
944 945 946 947 948 949 950 951
    qwReleaseTask(QW_READ, sch);
    qwReleaseScheduler(QW_READ, mgmt);
    
    qwDropTask(mgmt, sId, qId, tId);

    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
952
  if (!(task->cancel || task->drop)) {
D
dapan1121 已提交
953
    qwUpdateTaskInfo(mgmt, task, QW_TASK_INFO_STATUS, &status, QW_IDS());
D
dapan1121 已提交
954 955
    task->code = errCode;
  }
D
dapan1121 已提交
956 957

  QW_UNLOCK(QW_WRITE, &task->lock);
D
dapan1121 已提交
958 959 960 961 962 963 964
  
  qwReleaseTask(QW_READ, sch);
  qwReleaseScheduler(QW_READ, mgmt);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
int32_t qwScheduleDataSink(SQWTaskCtx *handles, SQWorkerMgmt *mgmt, uint64_t sId, uint64_t queryId, uint64_t taskId, SRpcMsg *pMsg) {
  if (atomic_load_8(&handles->sinkScheduled)) {
    qDebug("data sink already scheduled");
    return TSDB_CODE_SUCCESS;
  }
  
  SSinkDataReq * req = (SSinkDataReq *)rpcMallocCont(sizeof(SSinkDataReq));
  if (NULL == req) {
    qError("rpcMallocCont %d failed", (int32_t)sizeof(SSinkDataReq));
    QW_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  req->header.vgId = mgmt->nodeId;
  req->sId = sId;
  req->queryId = queryId;
  req->taskId = taskId;

  SRpcMsg pNewMsg = {
    .handle = pMsg->handle,
    .ahandle = pMsg->ahandle, 
    .msgType = TDMT_VND_SCHEDULE_DATA_SINK,
    .pCont   = req,
    .contLen = sizeof(SSinkDataReq),
    .code    = 0,
  };

  int32_t code = (*mgmt->putToQueueFp)(mgmt->nodeObj, &pNewMsg);
  if (TSDB_CODE_SUCCESS != code) {
    qError("put data sink schedule msg to queue failed, code:%x", code);
    rpcFreeCont(req);
    QW_ERR_RET(code);
  }

  qDebug("put data sink schedule msg to query queue");

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1003
int32_t qwScheduleQuery(SQWTaskCtx *handles, SQWorkerMgmt *mgmt, uint64_t sId, uint64_t qId, uint64_t tId, SRpcMsg *pMsg) {
D
dapan1121 已提交
1004
  if (atomic_load_8(&handles->queryScheduled)) {
D
dapan1121 已提交
1005
    QW_SCH_TASK_ELOG("query already scheduled, queryScheduled:%d", handles->queryScheduled);
D
dapan1121 已提交
1006 1007 1008
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
1009
  QW_ERR_RET(qwUpdateTaskStatus(mgmt, sId, qId, tId, JOB_TASK_STATUS_EXECUTING));      
D
dapan1121 已提交
1010 1011 1012

  SQueryContinueReq * req = (SQueryContinueReq *)rpcMallocCont(sizeof(SQueryContinueReq));
  if (NULL == req) {
D
dapan1121 已提交
1013
    QW_SCH_TASK_ELOG("rpcMallocCont %d failed", (int32_t)sizeof(SQueryContinueReq));
D
dapan1121 已提交
1014 1015 1016 1017 1018
    QW_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  req->header.vgId = mgmt->nodeId;
  req->sId = sId;
D
dapan1121 已提交
1019 1020
  req->queryId = qId;
  req->taskId = tId;
D
dapan1121 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032

  SRpcMsg pNewMsg = {
    .handle = pMsg->handle,
    .ahandle = pMsg->ahandle,
    .msgType = TDMT_VND_QUERY_CONTINUE,
    .pCont   = req,
    .contLen = sizeof(SQueryContinueReq),
    .code    = 0,
  };

  int32_t code = (*mgmt->putToQueueFp)(mgmt->nodeObj, &pNewMsg);
  if (TSDB_CODE_SUCCESS != code) {
D
dapan1121 已提交
1033
    QW_SCH_TASK_ELOG("put query continue msg to queue failed, code:%x", code);
D
dapan1121 已提交
1034 1035 1036 1037
    rpcFreeCont(req);
    QW_ERR_RET(code);
  }

D
dapan1121 已提交
1038
  handles->queryScheduled = true;
D
dapan1121 已提交
1039

D
dapan1121 已提交
1040
  QW_SCH_TASK_DLOG("put query continue msg to query queue, vgId:%d", mgmt->nodeId);
D
dapan1121 已提交
1041 1042 1043 1044 1045

  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
1046

D
dapan1121 已提交
1047
int32_t qwHandleFetch(SQWorkerMgmt *mgmt, uint64_t sId, uint64_t qId, uint64_t tId, SRpcMsg *pMsg) {
D
dapan1121 已提交
1048 1049
  SQWSchStatus *sch = NULL;
  SQWTaskStatus *task = NULL;
D
dapan1121 已提交
1050
  int32_t code = 0;
D
dapan1121 已提交
1051 1052
  int32_t needRsp = true;
  void *data = NULL;
1053 1054 1055 1056
  int32_t sinkStatus = 0;
  int32_t dataLength = 0;
  SRetrieveTableRsp *rsp = NULL;
  bool queryEnd = false;
D
dapan1121 已提交
1057
  SQWTaskCtx *handles = NULL;
D
dapan1121 已提交
1058
  int8_t status = 0;
D
dapan1121 已提交
1059

D
dapan1121 已提交
1060 1061 1062 1063 1064 1065
  QW_ERR_JRET(qwAcquireTaskCtx(QW_READ, mgmt, qId, tId, &handles));
  QW_LOCK(QW_WRITE, &handles->lock);
 
  if (handles->needRsp) {
    QW_UNLOCK(QW_WRITE, &handles->lock);
    QW_SCH_TASK_ELOG("last fetch not responsed, needRsp:%d", handles->needRsp);
D
dapan1121 已提交
1066 1067
    QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
  }
D
dapan1121 已提交
1068

D
dapan1121 已提交
1069
  QW_UNLOCK(QW_WRITE, &handles->lock);
D
dapan1121 已提交
1070

D
dapan1121 已提交
1071 1072
  QW_ERR_JRET(qwAcquireScheduler(QW_READ, mgmt, sId, &sch));
  QW_ERR_JRET(qwAcquireTask(mgmt, QW_READ, sch, qId, tId, &task));
D
dapan1121 已提交
1073

D
dapan1121 已提交
1074
  if (task->cancel || task->drop) {
D
dapan1121 已提交
1075
    QW_SCH_TASK_ELOG("task is already cancelled or dropped, cancel:%d, drop:%d", task->cancel, task->drop);
D
dapan1121 已提交
1076 1077 1078 1079
    QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
  }

  if (task->status != JOB_TASK_STATUS_EXECUTING && task->status != JOB_TASK_STATUS_PARTIAL_SUCCEED) {
D
dapan1121 已提交
1080
    QW_SCH_TASK_ELOG("invalid status %d for fetch", task->status);
D
dapan1121 已提交
1081 1082
    QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
  }
1083

D
dapan1121 已提交
1084
  dsGetDataLength(handles->sinkHandle, &dataLength, &queryEnd);
D
dapan1121 已提交
1085
  
1086
  if (dataLength > 0) {
D
dapan1121 已提交
1087
    SOutputData output = {0};
D
dapan1121 已提交
1088 1089 1090

    QW_SCH_TASK_DLOG("task got data in sink, dataLength:%d", dataLength);
    
1091 1092 1093 1094 1095 1096 1097 1098 1099
    QW_ERR_JRET(qwInitFetchRsp(dataLength, &rsp));
    
    output.pData = rsp->data;
    
    code = dsGetDataBlock(handles->sinkHandle, &output);
    if (code) {
      qError("dsGetDataBlock failed, code:%x", code);
      QW_ERR_JRET(code);
    }
D
dapan1121 已提交
1100 1101 1102 1103 1104 1105 1106

    rsp->useconds = htobe64(output.useconds);
    rsp->completed = 0;
    rsp->precision = output.precision;
    rsp->compressed = output.compressed;
    rsp->compLen = htonl(dataLength);
    rsp->numOfRows = htonl(output.numOfRows);
1107 1108 1109
    
    if (DS_BUF_EMPTY == output.bufStatus && output.queryEnd) {
      rsp->completed = 1;
D
dapan1121 已提交
1110
      
D
dapan1121 已提交
1111 1112 1113 1114
      status = JOB_TASK_STATUS_SUCCEED;
      
      QW_SCH_TASK_DLOG("task all fetched, status:%d", status);
      QW_ERR_JRET(qwUpdateTaskInfo(mgmt, task, QW_TASK_INFO_STATUS, &status, QW_IDS()));
1115 1116
    }

D
dapan1121 已提交
1117
    // Note: schedule data sink firstly and will schedule query after it's done
1118
    if (output.needSchedule) {
D
dapan1121 已提交
1119 1120 1121 1122 1123
      QW_SCH_TASK_DLOG("sink need schedule, queryEnd:%d", output.queryEnd);
      QW_ERR_JRET(qwScheduleDataSink(handles, mgmt, sId, qId, tId, pMsg));
    } else if ((!output.queryEnd) && (DS_BUF_LOW == output.bufStatus || DS_BUF_EMPTY == output.bufStatus)) {    
      QW_SCH_TASK_DLOG("task not end, need to continue, bufStatus:%d", output.bufStatus);
      QW_ERR_JRET(qwScheduleQuery(handles, mgmt, sId, qId, tId, pMsg));
D
dapan1121 已提交
1124 1125
    }
  } else {
1126
    if (dataLength < 0) {
D
dapan1121 已提交
1127
      QW_SCH_TASK_ELOG("invalid length from dsGetDataLength, length:%d", dataLength);
1128
      QW_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
1129 1130
    }
    
1131
    if (queryEnd) {
D
dapan1121 已提交
1132 1133 1134 1135 1136
      status = JOB_TASK_STATUS_SUCCEED;

      QW_SCH_TASK_DLOG("no data in sink and query end, dataLength:%d", dataLength);
      
      QW_ERR_JRET(qwUpdateTaskInfo(mgmt, task, QW_TASK_INFO_STATUS, &status, QW_IDS()));
1137
    } else {
D
dapan1121 已提交
1138
      assert(0 == handles->needRsp);
D
dapan1121 已提交
1139 1140

      // MUST IN SCHEDULE OR IN SINK SCHEDULE
D
dapan1121 已提交
1141
      
D
dapan1121 已提交
1142
      QW_SCH_TASK_DLOG("no res data in sink, need response later, queryEnd:%d", queryEnd);
D
dapan1121 已提交
1143

1144 1145 1146
      QW_LOCK(QW_WRITE, &handles->lock);
      handles->needRsp = true;
      QW_UNLOCK(QW_WRITE, &handles->lock);
D
dapan1121 已提交
1147

1148 1149
      needRsp = false;
    }
D
dapan1121 已提交
1150 1151 1152
  }

_return:
1153

D
dapan1121 已提交
1154
  if (task) {
D
dapan1121 已提交
1155
    qwReleaseTask(QW_READ, sch);    
D
dapan1121 已提交
1156 1157 1158
  }
  
  if (sch) {
D
dapan1121 已提交
1159
    qwReleaseScheduler(QW_READ, mgmt);
D
dapan1121 已提交
1160 1161
  }

D
dapan1121 已提交
1162
  if (needRsp) {
1163
    qwBuildAndSendFetchRsp(pMsg, rsp, dataLength, code);
D
dapan1121 已提交
1164
  }
D
dapan1121 已提交
1165

D
dapan1121 已提交
1166 1167
  if (handles) {
    qwReleaseTaskResCache(QW_READ, mgmt);
D
dapan1121 已提交
1168 1169
  }
  
D
dapan1121 已提交
1170
  QW_RET(code);
D
dapan1121 已提交
1171
}
D
dapan1121 已提交
1172

D
dapan1121 已提交
1173 1174 1175 1176 1177 1178
int32_t qWorkerInit(int8_t nodeType, int32_t nodeId, SQWorkerCfg *cfg, void **qWorkerMgmt, void *nodeObj, putReqToQueryQFp fp) {
  if (NULL == qWorkerMgmt || NULL == nodeObj || NULL == fp) {
    qError("invalid param to init qworker");
    QW_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }
  
D
dapan1121 已提交
1179 1180 1181
  SQWorkerMgmt *mgmt = calloc(1, sizeof(SQWorkerMgmt));
  if (NULL == mgmt) {
    qError("calloc %d failed", (int32_t)sizeof(SQWorkerMgmt));
D
dapan1121 已提交
1182
    QW_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
1183 1184 1185 1186
  }

  if (cfg) {
    mgmt->cfg = *cfg;
D
dapan1121 已提交
1187 1188 1189 1190 1191 1192 1193 1194 1195
    if (0 == mgmt->cfg.maxSchedulerNum) {
      mgmt->cfg.maxSchedulerNum = QWORKER_DEFAULT_SCHEDULER_NUMBER;
    }
    if (0 == mgmt->cfg.maxTaskNum) {
      mgmt->cfg.maxTaskNum = QWORKER_DEFAULT_TASK_NUMBER;
    }
    if (0 == mgmt->cfg.maxSchTaskNum) {
      mgmt->cfg.maxSchTaskNum = QWORKER_DEFAULT_SCH_TASK_NUMBER;
    }
D
dapan1121 已提交
1196 1197
  } else {
    mgmt->cfg.maxSchedulerNum = QWORKER_DEFAULT_SCHEDULER_NUMBER;
D
dapan1121 已提交
1198
    mgmt->cfg.maxTaskNum = QWORKER_DEFAULT_TASK_NUMBER;
D
dapan1121 已提交
1199 1200 1201
    mgmt->cfg.maxSchTaskNum = QWORKER_DEFAULT_SCH_TASK_NUMBER;
  }

D
dapan1121 已提交
1202 1203
  mgmt->schHash = taosHashInit(mgmt->cfg.maxSchedulerNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_NO_LOCK);
  if (NULL == mgmt->schHash) {
D
dapan1121 已提交
1204
    tfree(mgmt);
D
dapan1121 已提交
1205 1206
    qError("init %d scheduler hash failed", mgmt->cfg.maxSchedulerNum);
    QW_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
1207 1208
  }

D
dapan1121 已提交
1209 1210
  mgmt->ctxHash = taosHashInit(mgmt->cfg.maxTaskNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
  if (NULL == mgmt->ctxHash) {
D
dapan1121 已提交
1211 1212
    taosHashCleanup(mgmt->schHash);
    mgmt->schHash = NULL;
D
dapan1121 已提交
1213
    tfree(mgmt);
D
dapan1121 已提交
1214 1215
    qError("init %d task ctx hash failed", mgmt->cfg.maxTaskNum);
    QW_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
1216 1217
  }

D
dapan1121 已提交
1218 1219 1220 1221 1222
  mgmt->nodeType = nodeType;
  mgmt->nodeId = nodeId;
  mgmt->nodeObj = nodeObj;
  mgmt->putToQueueFp = fp;

D
dapan1121 已提交
1223 1224
  *qWorkerMgmt = mgmt;

D
dapan1121 已提交
1225 1226
  qDebug("qworker initialized for node, type:%d, id:%d, handle:%p", mgmt->nodeType, mgmt->nodeId, mgmt);

D
dapan1121 已提交
1227 1228 1229
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1230 1231
int32_t qWorkerProcessQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
1232
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
1233 1234
  }

D
dapan1121 已提交
1235
  int32_t code = 0;
D
dapan1121 已提交
1236 1237 1238
  bool queryRsped = false;
  bool needStop = false;
  struct SSubplan *plan = NULL;
D
dapan1121 已提交
1239
  SSubQueryMsg *msg = pMsg->pCont;
D
dapan1121 已提交
1240
  SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt;
D
dapan1121 已提交
1241
  int32_t rspCode = 0;
D
dapan1121 已提交
1242
  
D
dapan1121 已提交
1243
  if (NULL == msg || pMsg->contLen <= sizeof(*msg)) {
D
dapan1121 已提交
1244
    QW_ELOG("invalid query msg, contLen:%d", pMsg->contLen);
D
dapan1121 已提交
1245
    QW_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
1246
  }
D
dapan 已提交
1247

D
dapan1121 已提交
1248 1249 1250
  msg->sId = be64toh(msg->sId);
  msg->queryId = be64toh(msg->queryId);
  msg->taskId = be64toh(msg->taskId);
D
dapan 已提交
1251
  msg->contentLen = ntohl(msg->contentLen);
D
dapan1121 已提交
1252
  
D
dapan1121 已提交
1253 1254 1255
  uint64_t sId = msg->sId;
  uint64_t qId = msg->queryId;
  uint64_t tId = msg->taskId;
D
dapan1121 已提交
1256

D
dapan1121 已提交
1257
  QW_ERR_JRET(qwCheckAndProcessTaskDrop(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId, &needStop));
D
dapan1121 已提交
1258
  if (needStop) {
D
dapan1121 已提交
1259
    QW_TASK_DLOG("task need stop, msgLen:%d", msg->contentLen);
D
dapan1121 已提交
1260 1261
    qwBuildAndSendQueryRsp(pMsg, TSDB_CODE_QRY_TASK_CANCELLED);
    QW_ERR_RET(TSDB_CODE_QRY_TASK_CANCELLED);
D
dapan1121 已提交
1262
  }
D
dapan1121 已提交
1263 1264

  QW_ERR_JRET(qwAddTask(qWorkerMgmt, sId, qId, tId, JOB_TASK_STATUS_EXECUTING));
D
dapan1121 已提交
1265 1266
  
  code = qStringToSubplan(msg->msg, &plan);
D
dapan1121 已提交
1267
  if (TSDB_CODE_SUCCESS != code) {
D
dapan1121 已提交
1268
    QW_TASK_ELOG("string to subplan failed, code:%d", code);
D
dapan1121 已提交
1269
    QW_ERR_JRET(code);
D
dapan1121 已提交
1270 1271
  }

1272 1273
  qTaskInfo_t pTaskInfo = NULL;
  code = qCreateExecTask(node, 0, (struct SSubplan *)plan, &pTaskInfo);
D
dapan1121 已提交
1274
  if (code) {
D
dapan1121 已提交
1275
    QW_TASK_ELOG("qCreateExecTask failed, code:%x", code);
D
dapan1121 已提交
1276 1277
    QW_ERR_JRET(code);
  }
D
dapan1121 已提交
1278
  
D
dapan1121 已提交
1279
  QW_ERR_JRET(qwBuildAndSendQueryRsp(pMsg, TSDB_CODE_SUCCESS));
D
dapan1121 已提交
1280

D
dapan1121 已提交
1281
  queryRsped = true;
H
Haojun Liao 已提交
1282

1283 1284
  DataSinkHandle sinkHandle = NULL;
  code = qExecTask(pTaskInfo, &sinkHandle);
D
dapan1121 已提交
1285
  if (code) {
D
dapan1121 已提交
1286
    QW_TASK_ELOG("qExecTask failed, code:%x", code);
D
dapan1121 已提交
1287
    QW_ERR_JRET(code);
D
dapan1121 已提交
1288 1289 1290
  }

  QW_ERR_JRET(qwAddTaskHandlesToCache(qWorkerMgmt, msg->queryId, msg->taskId, pTaskInfo, sinkHandle));
D
dapan1121 已提交
1291 1292

_return:
1293

D
dapan1121 已提交
1294 1295 1296 1297 1298 1299 1300 1301 1302
  if (code) {
    rspCode = code;
  }
  
  if (!queryRsped) {
    code = qwBuildAndSendQueryRsp(pMsg, rspCode);
    if (TSDB_CODE_SUCCESS == rspCode && code) {
      rspCode = code;
    }
D
dapan1121 已提交
1303
  }
D
dapan1121 已提交
1304 1305
  
  int8_t status = 0;
D
dapan1121 已提交
1306
  if (TSDB_CODE_SUCCESS != rspCode) {
D
dapan1121 已提交
1307 1308 1309
    status = JOB_TASK_STATUS_FAILED;
  } else {
    status = JOB_TASK_STATUS_PARTIAL_SUCCEED;
D
dapan1121 已提交
1310
  }
D
dapan1121 已提交
1311

D
dapan1121 已提交
1312
  qwQueryPostProcess(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId, status, rspCode);
D
dapan1121 已提交
1313

D
dapan1121 已提交
1314 1315 1316
  if (queryRsped) {
    qwCheckAndSendReadyRsp(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId, pMsg);
  }
D
dapan1121 已提交
1317
  
D
dapan1121 已提交
1318
  QW_RET(rspCode);
D
dapan1121 已提交
1319 1320
}

1321 1322 1323 1324
int32_t qWorkerProcessQueryContinueMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  int32_t code = 0;
  int8_t status = 0;
  bool queryDone = false;
D
dapan1121 已提交
1325 1326 1327
  SQueryContinueReq *req = (SQueryContinueReq *)pMsg->pCont;
  bool needStop = false;
  SQWTaskCtx *handles = NULL;
1328

D
dapan1121 已提交
1329
  QW_ERR_JRET(qwAcquireTaskCtx(QW_READ, qWorkerMgmt, req->queryId, req->taskId, &handles));
D
dapan1121 已提交
1330
  QW_LOCK(QW_WRITE, &handles->lock);
D
dapan1121 已提交
1331 1332 1333 1334

  qTaskInfo_t     taskHandle = handles->taskHandle;
  DataSinkHandle  sinkHandle = handles->sinkHandle;

D
dapan1121 已提交
1335
  QW_UNLOCK(QW_WRITE, &handles->lock);
D
dapan1121 已提交
1336 1337
  qwReleaseTaskResCache(QW_READ, qWorkerMgmt);
  
D
dapan1121 已提交
1338
  QW_ERR_JRET(qwCheckAndProcessTaskDrop(qWorkerMgmt, req->sId, req->queryId, req->taskId, &needStop));
D
dapan1121 已提交
1339 1340
  if (needStop) {
    qWarn("task need stop");
D
dapan1121 已提交
1341 1342 1343 1344

    QW_ERR_JRET(qwAcquireTaskCtx(QW_READ, qWorkerMgmt, req->queryId, req->taskId, &handles));
    QW_LOCK(QW_WRITE, &handles->lock);
    if (handles->needRsp) {
D
dapan1121 已提交
1345
      qwBuildAndSendQueryRsp(pMsg, TSDB_CODE_QRY_TASK_CANCELLED);
D
dapan1121 已提交
1346
      handles->needRsp = false;
D
dapan1121 已提交
1347
    }
D
dapan1121 已提交
1348 1349 1350
    QW_UNLOCK(QW_WRITE, &handles->lock);
    qwReleaseTaskResCache(QW_READ, qWorkerMgmt);

D
dapan1121 已提交
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
    QW_ERR_RET(TSDB_CODE_QRY_TASK_CANCELLED);
  }

  DataSinkHandle newHandle = NULL;
  code = qExecTask(taskHandle, &newHandle);
  if (code) {
    qError("qExecTask failed, code:%x", code);  
    QW_ERR_JRET(code);
  }
  
  if (sinkHandle != newHandle) {
    qError("data sink mis-match");
    QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
  }
  
_return:

D
dapan1121 已提交
1368 1369 1370 1371
  QW_ERR_JRET(qwAcquireTaskCtx(QW_READ, qWorkerMgmt, req->queryId, req->taskId, &handles));
  QW_LOCK(QW_WRITE, &handles->lock);

  if (handles->needRsp) {
D
dapan1121 已提交
1372
    code = qwBuildAndSendQueryRsp(pMsg, code);
D
dapan1121 已提交
1373
    handles->needRsp = false;
D
dapan1121 已提交
1374
  }
D
dapan1121 已提交
1375 1376 1377 1378 1379
  handles->queryScheduled = false;

  QW_UNLOCK(QW_WRITE, &handles->lock);
  qwReleaseTaskResCache(QW_READ, qWorkerMgmt);

1380 1381 1382 1383 1384 1385
  if (TSDB_CODE_SUCCESS != code) {
    status = JOB_TASK_STATUS_FAILED;
  } else {
    status = JOB_TASK_STATUS_PARTIAL_SUCCEED;
  }

D
dapan1121 已提交
1386 1387
  code = qwQueryPostProcess(qWorkerMgmt, req->sId, req->queryId, req->taskId, status, code);
  
1388 1389 1390 1391 1392
  QW_RET(code);
}



D
dapan1121 已提交
1393
int32_t qWorkerProcessDataSinkMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg){
1394 1395 1396 1397 1398 1399 1400 1401
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

  SSinkDataReq *msg = pMsg->pCont;
  if (NULL == msg || pMsg->contLen < sizeof(*msg)) {
    qError("invalid sink data msg");
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
1402
  }
1403

D
dapan1121 已提交
1404
  //dsScheduleProcess();
1405 1406 1407 1408 1409
  //TODO

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1410 1411
int32_t qWorkerProcessReadyMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg){
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
1412 1413 1414
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

S
Shengliang Guan 已提交
1415
  SResReadyReq *msg = pMsg->pCont;
D
dapan1121 已提交
1416
  if (NULL == msg || pMsg->contLen < sizeof(*msg)) {
D
dapan1121 已提交
1417
    qError("invalid task status msg");  
D
dapan1121 已提交
1418 1419
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }  
D
dapan1121 已提交
1420

D
dapan1121 已提交
1421 1422 1423 1424 1425
  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 已提交
1426 1427 1428 1429
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1430 1431
int32_t qWorkerProcessStatusMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
1432 1433 1434
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

D
dapan1121 已提交
1435
  int32_t code = 0;
S
Shengliang Guan 已提交
1436
  SSchTasksStatusReq *msg = pMsg->pCont;
D
dapan1121 已提交
1437
  if (NULL == msg || pMsg->contLen < sizeof(*msg)) {
D
dapan1121 已提交
1438
    qError("invalid task status msg");
D
dapan1121 已提交
1439 1440 1441
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }  

D
dapan1121 已提交
1442 1443
  msg->sId = htobe64(msg->sId);

D
dapan1121 已提交
1444 1445
  SSchedulerStatusRsp *sStatus = NULL;
  
D
dapan1121 已提交
1446
  QW_ERR_JRET(qwGetSchTasksStatus(qWorkerMgmt, msg->sId, &sStatus));
D
dapan1121 已提交
1447 1448

_return:
D
dapan1121 已提交
1449

D
dapan1121 已提交
1450 1451
  QW_ERR_RET(qwBuildAndSendStatusRsp(pMsg, sStatus));

D
dapan1121 已提交
1452 1453 1454
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1455 1456
int32_t qWorkerProcessFetchMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
1457 1458 1459
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

S
Shengliang Guan 已提交
1460
  SResFetchReq *msg = pMsg->pCont;
D
dapan1121 已提交
1461
  if (NULL == msg || pMsg->contLen < sizeof(*msg)) {
D
dapan1121 已提交
1462 1463 1464
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }  

D
dapan1121 已提交
1465 1466 1467 1468 1469
  msg->sId = htobe64(msg->sId);
  msg->queryId = htobe64(msg->queryId);
  msg->taskId = htobe64(msg->taskId);

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

  void *data = NULL;
D
dapan1121 已提交
1472
  int32_t code = 0;
D
dapan1121 已提交
1473
  
D
dapan1121 已提交
1474
  QW_ERR_RET(qwHandleFetch(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId, pMsg));
D
dapan1121 已提交
1475 1476 1477 1478

  QW_RET(code);
}

D
dapan1121 已提交
1479 1480
int32_t qWorkerProcessCancelMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
1481 1482 1483
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

D
dapan1121 已提交
1484
  int32_t code = 0;
S
Shengliang Guan 已提交
1485
  STaskCancelReq *msg = pMsg->pCont;
D
dapan1121 已提交
1486
  if (NULL == msg || pMsg->contLen < sizeof(*msg)) {
D
dapan1121 已提交
1487
    qError("invalid task cancel msg");  
D
dapan1121 已提交
1488 1489 1490
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }  

D
dapan1121 已提交
1491 1492 1493 1494 1495
  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 已提交
1496

D
dapan1121 已提交
1497 1498 1499
_return:

  QW_ERR_RET(qwBuildAndSendCancelRsp(pMsg, code));
D
dapan1121 已提交
1500 1501 1502 1503

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1504 1505
int32_t qWorkerProcessDropMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
1506 1507 1508
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

D
dapan1121 已提交
1509
  int32_t code = 0;
S
Shengliang Guan 已提交
1510
  STaskDropReq *msg = pMsg->pCont;
D
dapan1121 已提交
1511
  if (NULL == msg || pMsg->contLen < sizeof(*msg)) {
D
dapan1121 已提交
1512
    qError("invalid task drop msg");
D
dapan1121 已提交
1513 1514 1515
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }  

D
dapan1121 已提交
1516 1517 1518 1519 1520
  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 已提交
1521 1522

_return:
D
dapan1121 已提交
1523

D
dapan1121 已提交
1524
  QW_ERR_RET(qwBuildAndSendDropRsp(pMsg, code));
D
dapan1121 已提交
1525 1526 1527 1528

  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
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 已提交
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
void qWorkerDestroy(void **qWorkerMgmt) {
  if (NULL == qWorkerMgmt || NULL == *qWorkerMgmt) {
    return;
  }

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

  //TODO FREE ALL

  tfree(*qWorkerMgmt);
}