schedulerTests.cpp 17.3 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <gtest/gtest.h>
#include <tglobal.h>
#include <iostream>

#include "os.h"

#include "taos.h"
#include "tdef.h"
#include "tvariant.h"
D
dapan1121 已提交
25 26
#include "catalog.h"
#include "scheduler.h"
D
dapan1121 已提交
27 28
#include "tep.h"
#include "trpc.h"
S
Shengliang Guan 已提交
29 30 31 32 33 34 35 36 37

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwrite-strings"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wreturn-type"
#pragma GCC diagnostic ignored "-Wformat"

D
dapan 已提交
38 39 40
#include "schedulerInt.h"
#include "stub.h"
#include "addr_any.h"
D
dapan1121 已提交
41

D
dapan1121 已提交
42

D
dapan1121 已提交
43
namespace {
D
dapan 已提交
44

D
dapan1121 已提交
45
extern "C" int32_t schHandleResponseMsg(SSchJob *job, SSchTask *task, int32_t msgType, char *msg, int32_t msgSize, int32_t rspCode);
D
dapan1121 已提交
46 47 48 49 50 51 52 53 54 55 56
extern "C" int32_t schHandleCallback(void* param, const SDataBuf* pMsg, int32_t msgType, int32_t rspCode);

struct SSchJob *pInsertJob = NULL;
struct SSchJob *pQueryJob = NULL;

uint64_t schtMergeTemplateId = 0x4;
uint64_t schtFetchTaskId = 0;
uint64_t schtQueryId = 1;

bool schtTestStop = false;
bool schtTestDeadLoop = false;
D
dapan1121 已提交
57
int32_t schtTestMTRunSec = 10;
D
dapan1121 已提交
58 59
int32_t schtTestPrintNum = 1000;
int32_t schtStartFetch = 0;
D
dapan 已提交
60

D
dapan1121 已提交
61

D
dapan1121 已提交
62 63 64 65 66 67 68
void schtInitLogFile() {
  const char    *defaultLogFileNamePrefix = "taoslog";
  const int32_t  maxLogFileNum = 10;

  tsAsyncLog = 0;
  qDebugFlag = 159;

S
Shengliang Guan 已提交
69
  if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) {
S
os env  
Shengliang Guan 已提交
70
    printf("failed to open log file in directory:%s\n", tsLogDir);
D
dapan1121 已提交
71 72 73 74 75
  }

}


X
Xiaoyu Wang 已提交
76
void schtBuildQueryDag(SQueryPlan *dag) {
D
dapan1121 已提交
77
  uint64_t qId = schtQueryId;
D
dapan1121 已提交
78 79 80 81
  
  dag->queryId = qId;
  dag->numOfSubplans = 2;
  dag->pSubplans = taosArrayInit(dag->numOfSubplans, POINTER_BYTES);
D
dapan1121 已提交
82 83
  SArray *scan = taosArrayInit(1, POINTER_BYTES);
  SArray *merge = taosArrayInit(1, POINTER_BYTES);
D
dapan1121 已提交
84
  
D
dapan1121 已提交
85 86 87 88 89 90 91
  SSubplan *scanPlan = (SSubplan *)calloc(1, sizeof(SSubplan));
  SSubplan *mergePlan = (SSubplan *)calloc(1, sizeof(SSubplan));

  scanPlan->id.queryId = qId;
  scanPlan->id.templateId = 0x0000000000000002;
  scanPlan->id.subplanId = 0x0000000000000003;
  scanPlan->type = QUERY_TYPE_SCAN;
H
Haojun Liao 已提交
92

D
dapan1121 已提交
93
  scanPlan->execNode.nodeId = 1;
H
Haojun Liao 已提交
94 95 96
  scanPlan->execNode.epset.inUse = 0;
  addEpIntoEpSet(&scanPlan->execNode.epset, "ep0", 6030);

D
dapan1121 已提交
97 98 99
  scanPlan->pChildren = NULL;
  scanPlan->level = 1;
  scanPlan->pParents = taosArrayInit(1, POINTER_BYTES);
X
Xiaoyu Wang 已提交
100
  scanPlan->pNode = (SPhysiNode*)calloc(1, sizeof(SPhysiNode));
D
dapan1121 已提交
101
  scanPlan->msgType = TDMT_VND_QUERY;
D
dapan1121 已提交
102 103

  mergePlan->id.queryId = qId;
D
dapan1121 已提交
104
  mergePlan->id.templateId = schtMergeTemplateId;
D
dapan1121 已提交
105 106 107
  mergePlan->id.subplanId = 0x5555555555;
  mergePlan->type = QUERY_TYPE_MERGE;
  mergePlan->level = 0;
H
Haojun Liao 已提交
108 109
  mergePlan->execNode.epset.numOfEps = 0;

D
dapan1121 已提交
110 111
  mergePlan->pChildren = taosArrayInit(1, POINTER_BYTES);
  mergePlan->pParents = NULL;
X
Xiaoyu Wang 已提交
112
  mergePlan->pNode = (SPhysiNode*)calloc(1, sizeof(SPhysiNode));
D
dapan1121 已提交
113
  mergePlan->msgType = TDMT_VND_QUERY;
D
dapan1121 已提交
114 115 116 117

  SSubplan *mergePointer = (SSubplan *)taosArrayPush(merge, &mergePlan);
  SSubplan *scanPointer = (SSubplan *)taosArrayPush(scan, &scanPlan);

D
dapan1121 已提交
118 119
  taosArrayPush(mergePlan->pChildren, &scanPlan);
  taosArrayPush(scanPlan->pParents, &mergePlan);
D
dapan1121 已提交
120 121 122 123 124

  taosArrayPush(dag->pSubplans, &merge);  
  taosArrayPush(dag->pSubplans, &scan);
}

X
Xiaoyu Wang 已提交
125
void schtFreeQueryDag(SQueryPlan *dag) {
D
dapan1121 已提交
126 127 128 129

}


X
Xiaoyu Wang 已提交
130
void schtBuildInsertDag(SQueryPlan *dag) {
D
dapan 已提交
131 132 133 134 135
  uint64_t qId = 0x0000000000000002;
  
  dag->queryId = qId;
  dag->numOfSubplans = 2;
  dag->pSubplans = taosArrayInit(1, POINTER_BYTES);
D
dapan1121 已提交
136
  SArray *inserta = taosArrayInit(dag->numOfSubplans, POINTER_BYTES);
D
dapan 已提交
137
  
D
dapan1121 已提交
138
  SSubplan *insertPlan = (SSubplan *)calloc(2, sizeof(SSubplan));
D
dapan 已提交
139 140 141 142 143 144

  insertPlan[0].id.queryId = qId;
  insertPlan[0].id.templateId = 0x0000000000000003;
  insertPlan[0].id.subplanId = 0x0000000000000004;
  insertPlan[0].type = QUERY_TYPE_MODIFY;
  insertPlan[0].level = 0;
H
Haojun Liao 已提交
145

D
dapan1121 已提交
146
  insertPlan[0].execNode.nodeId = 1;
H
Haojun Liao 已提交
147 148 149
  insertPlan[0].execNode.epset.inUse = 0;
  addEpIntoEpSet(&insertPlan[0].execNode.epset, "ep0", 6030);

H
Haojun Liao 已提交
150
  insertPlan[0].pChildren = NULL;
D
dapan 已提交
151 152
  insertPlan[0].pParents = NULL;
  insertPlan[0].pNode = NULL;
X
Xiaoyu Wang 已提交
153
  insertPlan[0].pDataSink = (SDataSinkNode*)calloc(1, sizeof(SDataSinkNode));
D
dapan1121 已提交
154
  insertPlan[0].msgType = TDMT_VND_SUBMIT;
D
dapan 已提交
155 156 157 158 159 160

  insertPlan[1].id.queryId = qId;
  insertPlan[1].id.templateId = 0x0000000000000003;
  insertPlan[1].id.subplanId = 0x0000000000000005;
  insertPlan[1].type = QUERY_TYPE_MODIFY;
  insertPlan[1].level = 0;
H
Haojun Liao 已提交
161

D
dapan1121 已提交
162
  insertPlan[1].execNode.nodeId = 1;
H
Haojun Liao 已提交
163 164 165
  insertPlan[1].execNode.epset.inUse = 0;
  addEpIntoEpSet(&insertPlan[1].execNode.epset, "ep0", 6030);

H
Haojun Liao 已提交
166
  insertPlan[1].pChildren = NULL;
D
dapan 已提交
167 168
  insertPlan[1].pParents = NULL;
  insertPlan[1].pNode = NULL;
X
Xiaoyu Wang 已提交
169
  insertPlan[1].pDataSink = (SDataSinkNode*)calloc(1, sizeof(SDataSinkNode));
D
dapan1121 已提交
170
  insertPlan[1].msgType = TDMT_VND_SUBMIT;
D
dapan 已提交
171

D
dapan1121 已提交
172 173 174
  taosArrayPush(inserta, &insertPlan);
  insertPlan += 1;
  taosArrayPush(inserta, &insertPlan);
D
dapan 已提交
175 176 177 178 179

  taosArrayPush(dag->pSubplans, &inserta);  
}


D
dapan 已提交
180 181 182 183 184 185
int32_t schtPlanToString(const SSubplan *subplan, char** str, int32_t* len) {
  *str = (char *)calloc(1, 20);
  *len = 20;
  return 0;
}

D
dapan1121 已提交
186
void schtExecNode(SSubplan* subplan, uint64_t templateId, SQueryNodeAddr* ep) {
H
Haojun Liao 已提交
187

D
dapan 已提交
188 189
}

D
dapan1121 已提交
190 191 192 193
void schtRpcSendRequest(void *shandle, const SEpSet *pEpSet, SRpcMsg *pMsg, int64_t *pRid) {

}

D
dapan 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
void schtSetPlanToString() {
  static Stub stub;
  stub.set(qSubPlanToString, schtPlanToString);
  {
    AddrAny any("libplanner.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^qSubPlanToString$", result);
    for (const auto& f : result) {
      stub.set(f.second, schtPlanToString);
    }
  }
}

void schtSetExecNode() {
  static Stub stub;
  stub.set(qSetSubplanExecutionNode, schtExecNode);
  {
    AddrAny any("libplanner.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^qSetSubplanExecutionNode$", result);
    for (const auto& f : result) {
      stub.set(f.second, schtExecNode);
    }
  }
}

D
dapan1121 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232
void schtSetRpcSendRequest() {
  static Stub stub;
  stub.set(rpcSendRequest, schtRpcSendRequest);
  {
    AddrAny any("libtransport.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^rpcSendRequest$", result);
    for (const auto& f : result) {
      stub.set(f.second, schtRpcSendRequest);
    }
  }
}

D
dapan1121 已提交
233 234 235 236 237 238
int32_t schtAsyncSendMsgToServer(void *pTransporter, SEpSet* epSet, int64_t* pTransporterId, SMsgSendInfo* pInfo) {
  if (pInfo) {
    tfree(pInfo->param);
    tfree(pInfo->msgInfo.pData);
    free(pInfo);
  }
D
dapan1121 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
  return 0;
}


void schtSetAsyncSendMsgToServer() {
  static Stub stub;
  stub.set(asyncSendMsgToServer, schtAsyncSendMsgToServer);
  {
    AddrAny any("libtransport.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^asyncSendMsgToServer$", result);
    for (const auto& f : result) {
      stub.set(f.second, schtAsyncSendMsgToServer);
    }
  }
}

D
dapan1121 已提交
256

D
dapan 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
void *schtSendRsp(void *param) {
  SSchJob *job = NULL;
  int32_t code = 0;

  while (true) {
    job = *(SSchJob **)param;
    if (job) {
      break;
    }

    usleep(1000);
  }
  
  void *pIter = taosHashIterate(job->execTasks, NULL);
  while (pIter) {
    SSchTask *task = *(SSchTask **)pIter;

S
Shengliang Guan 已提交
274
    SSubmitRsp rsp = {0};
D
dapan 已提交
275
    rsp.affectedRows = 10;
D
dapan1121 已提交
276
    schHandleResponseMsg(job, task, TDMT_VND_SUBMIT_RSP, (char *)&rsp, sizeof(rsp), 0);
D
dapan 已提交
277 278 279 280 281 282 283
    
    pIter = taosHashIterate(job->execTasks, pIter);
  }    

  return NULL;
}

D
dapan1121 已提交
284 285 286 287 288 289 290 291 292
void *schtCreateFetchRspThread(void *param) {
  struct SSchJob* job = (struct SSchJob*)param;

  sleep(1);

  int32_t code = 0;
  SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)calloc(1, sizeof(SRetrieveTableRsp));
  rsp->completed = 1;
  rsp->numOfRows = 10;
D
dapan1121 已提交
293
 
D
dapan1121 已提交
294
  code = schHandleResponseMsg(job, job->fetchTask, TDMT_VND_FETCH_RSP, (char *)rsp, sizeof(*rsp), 0);
D
dapan1121 已提交
295 296 297 298 299
    
  assert(code == 0);
}


D
dapan1121 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
void *schtFetchRspThread(void *aa) {
  SDataBuf dataBuf = {0};
  SSchCallbackParam* param = NULL;

  while (!schtTestStop) {
    if (0 == atomic_val_compare_exchange_32(&schtStartFetch, 1, 0)) {
      continue;
    }

    usleep(1);
    
    param = (SSchCallbackParam *)calloc(1, sizeof(*param));

    param->queryId = schtQueryId;  
    param->taskId = schtFetchTaskId;

    int32_t code = 0;
    SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)calloc(1, sizeof(SRetrieveTableRsp));
    rsp->completed = 1;
    rsp->numOfRows = 10;

    dataBuf.pData = rsp;
    dataBuf.len = sizeof(*rsp);

    code = schHandleCallback(param, &dataBuf, TDMT_VND_FETCH_RSP, 0);
      
    assert(code == 0 || code);
  }
}

void schtFreeQueryJob(int32_t freeThread) {
  static uint32_t freeNum = 0;
  SSchJob *job = atomic_load_ptr(&pQueryJob);
  
  if (job && atomic_val_compare_exchange_ptr(&pQueryJob, job, NULL)) {
D
dapan1121 已提交
335
    schedulerFreeJob(job);
D
dapan1121 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349
    if (freeThread) {
      if (++freeNum % schtTestPrintNum == 0) {
        printf("FreeNum:%d\n", freeNum);
      }
    }
  }
}

void* schtRunJobThread(void *aa) {
  void *mockPointer = (void *)0x1;
  char *clusterId = "cluster1";
  char *dbname = "1.db1";
  char *tablename = "table1";
  SVgroupInfo vgInfo = {0};
X
Xiaoyu Wang 已提交
350
  SQueryPlan dag = {0};
D
dapan1121 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371

  schtInitLogFile();

  
  int32_t code = schedulerInit(NULL);
  assert(code == 0);


  schtSetPlanToString();
  schtSetExecNode();
  schtSetAsyncSendMsgToServer();

  SSchJob *job = NULL;
  SSchCallbackParam *param = NULL;
  SHashObj *execTasks = NULL;
  SDataBuf dataBuf = {0};
  uint32_t jobFinished = 0;

  while (!schtTestStop) {
    schtBuildQueryDag(&dag);

H
Haojun Liao 已提交
372
    SArray *qnodeList = taosArrayInit(1, sizeof(SEp));
D
dapan1121 已提交
373

H
Haojun Liao 已提交
374
    SEp qnodeAddr = {0};
D
dapan1121 已提交
375 376 377 378
    strcpy(qnodeAddr.fqdn, "qnode0.ep");
    qnodeAddr.port = 6031;
    taosArrayPush(qnodeList, &qnodeAddr);

379
    code = schedulerAsyncExecJob(mockPointer, qnodeList, &dag, "select * from tb", &job);
D
dapan1121 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
    assert(code == 0);

    execTasks = taosHashInit(5, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_ENTRY_LOCK);
    void *pIter = taosHashIterate(job->execTasks, NULL);
    while (pIter) {
      SSchTask *task = *(SSchTask **)pIter;
      schtFetchTaskId = task->taskId - 1;
      
      taosHashPut(execTasks, &task->taskId, sizeof(task->taskId), task, sizeof(*task));
      pIter = taosHashIterate(job->execTasks, pIter);
    }    

    param = (SSchCallbackParam *)calloc(1, sizeof(*param));
    param->queryId = schtQueryId;
    
    pQueryJob = job;
    

    pIter = taosHashIterate(execTasks, NULL);
    while (pIter) {
      SSchTask *task = (SSchTask *)pIter;

      param->taskId = task->taskId;
      SQueryTableRsp rsp = {0};
      dataBuf.pData = &rsp;
      dataBuf.len = sizeof(rsp);
      
      code = schHandleCallback(param, &dataBuf, TDMT_VND_QUERY_RSP, 0);
      assert(code == 0 || code);

      pIter = taosHashIterate(execTasks, pIter);
    }    


    param = (SSchCallbackParam *)calloc(1, sizeof(*param));
    param->queryId = schtQueryId;

    pIter = taosHashIterate(execTasks, NULL);
    while (pIter) {
      SSchTask *task = (SSchTask *)pIter;

      param->taskId = task->taskId;
      SResReadyRsp rsp = {0};
      dataBuf.pData = &rsp;
      dataBuf.len = sizeof(rsp);
      
      code = schHandleCallback(param, &dataBuf, TDMT_VND_RES_READY_RSP, 0);
      assert(code == 0 || code);
      
      pIter = taosHashIterate(execTasks, pIter);
    }  


    param = (SSchCallbackParam *)calloc(1, sizeof(*param));
    param->queryId = schtQueryId;

    pIter = taosHashIterate(execTasks, NULL);
    while (pIter) {
      SSchTask *task = (SSchTask *)pIter;

      param->taskId = task->taskId - 1;
      SQueryTableRsp rsp = {0};
      dataBuf.pData = &rsp;
      dataBuf.len = sizeof(rsp);
      
      code = schHandleCallback(param, &dataBuf, TDMT_VND_QUERY_RSP, 0);
      assert(code == 0 || code);
      
      pIter = taosHashIterate(execTasks, pIter);
    }    


    param = (SSchCallbackParam *)calloc(1, sizeof(*param));
    param->queryId = schtQueryId;

    pIter = taosHashIterate(execTasks, NULL);
    while (pIter) {
      SSchTask *task = (SSchTask *)pIter;

      param->taskId = task->taskId - 1;
      SResReadyRsp rsp = {0};
      dataBuf.pData = &rsp;
      dataBuf.len = sizeof(rsp);
      
      code = schHandleCallback(param, &dataBuf, TDMT_VND_RES_READY_RSP, 0);
      assert(code == 0 || code);
      
      pIter = taosHashIterate(execTasks, pIter);
    }  

    atomic_store_32(&schtStartFetch, 1);

    void *data = NULL;  
D
dapan1121 已提交
473
    code = schedulerFetchRows(pQueryJob, &data);
D
dapan1121 已提交
474 475 476 477 478 479 480 481 482
    assert(code == 0 || code);

    if (0 == code) {
      SRetrieveTableRsp *pRsp = (SRetrieveTableRsp *)data;
      assert(pRsp->completed == 1);
      assert(pRsp->numOfRows == 10);
    }

    data = NULL;
D
dapan1121 已提交
483
    code = schedulerFetchRows(pQueryJob, &data);
D
dapan1121 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
    assert(code == 0 || code);
    
    schtFreeQueryJob(0);

    taosHashCleanup(execTasks);

    schtFreeQueryDag(&dag);

    if (++jobFinished % schtTestPrintNum == 0) {
      printf("jobFinished:%d\n", jobFinished);
    }

    ++schtQueryId;
  }

  schedulerDestroy();

}

void* schtFreeJobThread(void *aa) {
  while (!schtTestStop) {
D
dapan1121 已提交
505
    usleep(rand() % 100);
D
dapan1121 已提交
506 507 508
    schtFreeQueryJob(1);
  }
}
D
dapan1121 已提交
509 510


D
dapan1121 已提交
511 512
}

D
dapan 已提交
513
TEST(queryTest, normalCase) {
D
dapan1121 已提交
514
  void *mockPointer = (void *)0x1;
D
dapan1121 已提交
515 516 517
  char *clusterId = "cluster1";
  char *dbname = "1.db1";
  char *tablename = "table1";
D
dapan1121 已提交
518
  SVgroupInfo vgInfo = {0};
D
dapan1121 已提交
519
  SSchJob *pJob = NULL;
X
Xiaoyu Wang 已提交
520
  SQueryPlan dag = {0};
D
dapan1121 已提交
521 522 523

  schtInitLogFile();

H
Haojun Liao 已提交
524
  SArray *qnodeList = taosArrayInit(1, sizeof(SEp));
D
dapan 已提交
525

H
Haojun Liao 已提交
526
  SEp qnodeAddr = {0};
D
dapan 已提交
527 528 529
  strcpy(qnodeAddr.fqdn, "qnode0.ep");
  qnodeAddr.port = 6031;
  taosArrayPush(qnodeList, &qnodeAddr);
D
dapan1121 已提交
530 531
  
  int32_t code = schedulerInit(NULL);
D
dapan1121 已提交
532
  ASSERT_EQ(code, 0);
D
dapan1121 已提交
533

D
dapan 已提交
534
  schtBuildQueryDag(&dag);
D
dapan 已提交
535 536 537

  schtSetPlanToString();
  schtSetExecNode();
D
dapan1121 已提交
538
  schtSetAsyncSendMsgToServer();
D
dapan1121 已提交
539
  
540
  code = schedulerAsyncExecJob(mockPointer, qnodeList, &dag, "select * from tb", &pJob);
D
dapan1121 已提交
541
  ASSERT_EQ(code, 0);
D
dapan 已提交
542

D
dapan 已提交
543
  SSchJob *job = (SSchJob *)pJob;
D
dapan 已提交
544 545
  void *pIter = taosHashIterate(job->execTasks, NULL);
  while (pIter) {
D
dapan 已提交
546
    SSchTask *task = *(SSchTask **)pIter;
D
dapan 已提交
547 548

    SQueryTableRsp rsp = {0};
D
dapan1121 已提交
549
    code = schHandleResponseMsg(job, task, TDMT_VND_QUERY_RSP, (char *)&rsp, sizeof(rsp), 0);
D
dapan 已提交
550 551 552 553 554 555 556
    
    ASSERT_EQ(code, 0);
    pIter = taosHashIterate(job->execTasks, pIter);
  }    

  pIter = taosHashIterate(job->execTasks, NULL);
  while (pIter) {
D
dapan 已提交
557
    SSchTask *task = *(SSchTask **)pIter;
D
dapan 已提交
558 559

    SResReadyRsp rsp = {0};
D
dapan1121 已提交
560 561
    code = schHandleResponseMsg(job, task, TDMT_VND_RES_READY_RSP, (char *)&rsp, sizeof(rsp), 0);
    printf("code:%d", code);
D
dapan 已提交
562 563 564 565 566 567
    ASSERT_EQ(code, 0);
    pIter = taosHashIterate(job->execTasks, pIter);
  }  

  pIter = taosHashIterate(job->execTasks, NULL);
  while (pIter) {
D
dapan 已提交
568
    SSchTask *task = *(SSchTask **)pIter;
D
dapan 已提交
569 570

    SQueryTableRsp rsp = {0};
D
dapan1121 已提交
571
    code = schHandleResponseMsg(job, task, TDMT_VND_QUERY_RSP, (char *)&rsp, sizeof(rsp), 0);
D
dapan 已提交
572 573 574 575 576 577 578
    
    ASSERT_EQ(code, 0);
    pIter = taosHashIterate(job->execTasks, pIter);
  }    

  pIter = taosHashIterate(job->execTasks, NULL);
  while (pIter) {
D
dapan 已提交
579
    SSchTask *task = *(SSchTask **)pIter;
D
dapan 已提交
580 581

    SResReadyRsp rsp = {0};
D
dapan1121 已提交
582
    code = schHandleResponseMsg(job, task, TDMT_VND_RES_READY_RSP, (char *)&rsp, sizeof(rsp), 0);
D
dapan 已提交
583 584 585 586 587
    ASSERT_EQ(code, 0);
    
    pIter = taosHashIterate(job->execTasks, pIter);
  }  

D
dapan1121 已提交
588 589
  pthread_attr_t thattr;
  pthread_attr_init(&thattr);
D
dapan 已提交
590

D
dapan1121 已提交
591 592
  pthread_t thread1;
  pthread_create(&(thread1), &thattr, schtCreateFetchRspThread, job);
D
dapan 已提交
593

D
dapan1121 已提交
594
  void *data = NULL;  
D
dapan1121 已提交
595
  code = schedulerFetchRows(job, &data);
D
dapan 已提交
596 597 598 599 600
  ASSERT_EQ(code, 0);

  SRetrieveTableRsp *pRsp = (SRetrieveTableRsp *)data;
  ASSERT_EQ(pRsp->completed, 1);
  ASSERT_EQ(pRsp->numOfRows, 10);
D
dapan1121 已提交
601
  tfree(data);
D
dapan 已提交
602 603

  data = NULL;
D
dapan1121 已提交
604
  code = schedulerFetchRows(job, &data);
D
dapan 已提交
605
  ASSERT_EQ(code, 0);
D
dapan1121 已提交
606
  ASSERT_TRUE(data);
D
dapan 已提交
607

D
dapan1121 已提交
608
  schedulerFreeJob(pJob);
D
dapan1121 已提交
609 610

  schtFreeQueryDag(&dag);
D
dapan1121 已提交
611 612

  schedulerDestroy();
D
dapan1121 已提交
613
}
D
dapan1121 已提交
614 615


D
dapan 已提交
616 617 618 619 620 621 622

TEST(insertTest, normalCase) {
  void *mockPointer = (void *)0x1;
  char *clusterId = "cluster1";
  char *dbname = "1.db1";
  char *tablename = "table1";
  SVgroupInfo vgInfo = {0};
X
Xiaoyu Wang 已提交
623
  SQueryPlan dag = {0};
D
dapan 已提交
624
  uint64_t numOfRows = 0;
D
dapan1121 已提交
625 626

  schtInitLogFile();
H
Haojun Liao 已提交
627

H
Haojun Liao 已提交
628
  SArray *qnodeList = taosArrayInit(1, sizeof(SEp));
D
dapan 已提交
629

H
Haojun Liao 已提交
630
  SEp qnodeAddr = {0};
D
dapan 已提交
631 632 633 634 635 636 637 638 639 640
  strcpy(qnodeAddr.fqdn, "qnode0.ep");
  qnodeAddr.port = 6031;
  taosArrayPush(qnodeList, &qnodeAddr);
  
  int32_t code = schedulerInit(NULL);
  ASSERT_EQ(code, 0);

  schtBuildInsertDag(&dag);

  schtSetPlanToString();
D
dapan1121 已提交
641
  schtSetAsyncSendMsgToServer();
D
dapan 已提交
642 643 644 645 646 647

  pthread_attr_t thattr;
  pthread_attr_init(&thattr);

  pthread_t thread1;
  pthread_create(&(thread1), &thattr, schtSendRsp, &pInsertJob);
D
dapan1121 已提交
648 649

  SQueryResult res = {0};
650
  code = schedulerExecJob(mockPointer, qnodeList, &dag, &pInsertJob, "insert into tb values(now,1)", &res);
D
dapan 已提交
651
  ASSERT_EQ(code, 0);
D
dapan1121 已提交
652
  ASSERT_EQ(res.numOfRows, 20);
D
dapan 已提交
653

D
dapan1121 已提交
654
  schedulerFreeJob(pInsertJob);
D
dapan1121 已提交
655 656

  schedulerDestroy();  
D
dapan 已提交
657 658
}

D
dapan1121 已提交
659
TEST(multiThread, forceFree) {
D
dapan1121 已提交
660 661
  pthread_attr_t thattr;
  pthread_attr_init(&thattr);
D
dapan 已提交
662

D
dapan1121 已提交
663 664 665 666
  pthread_t thread1, thread2, thread3;
  pthread_create(&(thread1), &thattr, schtRunJobThread, NULL);
  pthread_create(&(thread2), &thattr, schtFreeJobThread, NULL);
  pthread_create(&(thread3), &thattr, schtFetchRspThread, NULL);
D
dapan1121 已提交
667

D
dapan1121 已提交
668 669 670 671 672 673 674 675 676 677 678
  while (true) {
    if (schtTestDeadLoop) {
      sleep(1);
    } else {
      sleep(schtTestMTRunSec);
      break;
    }
  }
  
  schtTestStop = true;
  sleep(3);
D
dapan1121 已提交
679
}
D
dapan 已提交
680

D
dapan1121 已提交
681
int main(int argc, char** argv) {
D
dapan1121 已提交
682
  srand(time(NULL));
D
dapan1121 已提交
683 684 685 686
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

S
Shengliang Guan 已提交
687
#pragma GCC diagnostic pop