schedulerTests.cpp 17.4 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 69 70 71 72 73 74 75 76 77
void schtInitLogFile() {
  const char    *defaultLogFileNamePrefix = "taoslog";
  const int32_t  maxLogFileNum = 10;

  tsAsyncLog = 0;
  qDebugFlag = 159;

  char temp[128] = {0};
  sprintf(temp, "%s/%s", tsLogDir, defaultLogFileNamePrefix);
  if (taosInitLog(temp, tsNumOfLogLines, maxLogFileNum) < 0) {
    printf("failed to open log file in directory:%s\n", tsLogDir);
  }

}


D
dapan 已提交
78
void schtBuildQueryDag(SQueryDag *dag) {
D
dapan1121 已提交
79
  uint64_t qId = schtQueryId;
D
dapan1121 已提交
80 81 82 83
  
  dag->queryId = qId;
  dag->numOfSubplans = 2;
  dag->pSubplans = taosArrayInit(dag->numOfSubplans, POINTER_BYTES);
D
dapan1121 已提交
84 85
  SArray *scan = taosArrayInit(1, POINTER_BYTES);
  SArray *merge = taosArrayInit(1, POINTER_BYTES);
D
dapan1121 已提交
86
  
D
dapan1121 已提交
87 88 89 90 91 92 93
  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 已提交
94

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

D
dapan1121 已提交
99 100 101 102
  scanPlan->pChildren = NULL;
  scanPlan->level = 1;
  scanPlan->pParents = taosArrayInit(1, POINTER_BYTES);
  scanPlan->pNode = (SPhyNode*)calloc(1, sizeof(SPhyNode));
D
dapan1121 已提交
103
  scanPlan->msgType = TDMT_VND_QUERY;
D
dapan1121 已提交
104 105

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

D
dapan1121 已提交
112 113 114
  mergePlan->pChildren = taosArrayInit(1, POINTER_BYTES);
  mergePlan->pParents = NULL;
  mergePlan->pNode = (SPhyNode*)calloc(1, sizeof(SPhyNode));
D
dapan1121 已提交
115
  mergePlan->msgType = TDMT_VND_QUERY;
D
dapan1121 已提交
116 117 118 119

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

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

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

D
dapan1121 已提交
127 128 129 130 131
void schtFreeQueryDag(SQueryDag *dag) {

}


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

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

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

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

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

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

H
Haojun Liao 已提交
168
  insertPlan[1].pChildren = NULL;
D
dapan 已提交
169 170 171
  insertPlan[1].pParents = NULL;
  insertPlan[1].pNode = NULL;
  insertPlan[1].pDataSink = (SDataSink*)calloc(1, sizeof(SDataSink));
D
dapan1121 已提交
172
  insertPlan[1].msgType = TDMT_VND_SUBMIT;
D
dapan 已提交
173

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

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


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

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

D
dapan 已提交
190 191
}

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

}

D
dapan 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
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 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234
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 已提交
235 236 237 238 239 240
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 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
  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 已提交
258

D
dapan 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
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 已提交
276
    SShellSubmitRsp rsp = {0};
D
dapan 已提交
277
    rsp.affectedRows = 10;
D
dapan1121 已提交
278
    schHandleResponseMsg(job, task, TDMT_VND_SUBMIT_RSP, (char *)&rsp, sizeof(rsp), 0);
D
dapan 已提交
279 280 281 282 283 284 285
    
    pIter = taosHashIterate(job->execTasks, pIter);
  }    

  return NULL;
}

D
dapan1121 已提交
286 287 288 289 290 291 292 293 294
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 已提交
295
 
D
dapan1121 已提交
296
  code = schHandleResponseMsg(job, job->fetchTask, TDMT_VND_FETCH_RSP, (char *)rsp, sizeof(*rsp), 0);
D
dapan1121 已提交
297 298 299 300 301
    
  assert(code == 0);
}


D
dapan1121 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
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 已提交
337
    schedulerFreeJob(job);
D
dapan1121 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
    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};
  SQueryDag dag = {0};

  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 已提交
374
    SArray *qnodeList = taosArrayInit(1, sizeof(SEp));
D
dapan1121 已提交
375

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

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

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

    data = NULL;
D
dapan1121 已提交
485
    code = schedulerFetchRows(pQueryJob, &data);
D
dapan1121 已提交
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
    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 已提交
507
    usleep(rand() % 100);
D
dapan1121 已提交
508 509 510
    schtFreeQueryJob(1);
  }
}
D
dapan1121 已提交
511 512


D
dapan1121 已提交
513 514
}

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

  schtInitLogFile();

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

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

D
dapan 已提交
536
  schtBuildQueryDag(&dag);
D
dapan 已提交
537 538 539

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

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

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

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

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

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

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

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

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

D
dapan1121 已提交
590 591
  pthread_attr_t thattr;
  pthread_attr_init(&thattr);
D
dapan 已提交
592

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

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

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

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

D
dapan1121 已提交
610
  schedulerFreeJob(pJob);
D
dapan1121 已提交
611 612

  schtFreeQueryDag(&dag);
D
dapan1121 已提交
613 614

  schedulerDestroy();
D
dapan1121 已提交
615
}
D
dapan1121 已提交
616 617


D
dapan 已提交
618 619 620 621 622 623 624 625 626

TEST(insertTest, normalCase) {
  void *mockPointer = (void *)0x1;
  char *clusterId = "cluster1";
  char *dbname = "1.db1";
  char *tablename = "table1";
  SVgroupInfo vgInfo = {0};
  SQueryDag dag = {0};
  uint64_t numOfRows = 0;
D
dapan1121 已提交
627 628

  schtInitLogFile();
H
Haojun Liao 已提交
629

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

H
Haojun Liao 已提交
632
  SEp qnodeAddr = {0};
D
dapan 已提交
633 634 635 636 637 638 639 640 641 642
  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 已提交
643
  schtSetAsyncSendMsgToServer();
D
dapan 已提交
644 645 646 647 648 649

  pthread_attr_t thattr;
  pthread_attr_init(&thattr);

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

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

D
dapan1121 已提交
656
  schedulerFreeJob(pInsertJob);
D
dapan1121 已提交
657 658

  schedulerDestroy();  
D
dapan 已提交
659 660
}

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

D
dapan1121 已提交
665 666 667 668
  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 已提交
669

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

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

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