schedulerTests.cpp 17.9 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
#include "tref.h"
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
extern "C" int32_t schHandleCallback(void* param, const SDataBuf* pMsg, int32_t msgType, int32_t rspCode);

D
dapan1121 已提交
48 49
int64_t insertJobRefId = 0;
int64_t queryJobRefId = 0;
D
dapan1121 已提交
50 51 52 53 54 55 56

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
void schtInitLogFile() {
  const char    *defaultLogFileNamePrefix = "taoslog";
  const int32_t  maxLogFileNum = 10;

  tsAsyncLog = 0;
  qDebugFlag = 159;
D
dapan1121 已提交
68
  strcpy(tsLogDir, "/var/log/taos");
D
dapan1121 已提交
69

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

}


X
Xiaoyu Wang 已提交
77
void schtBuildQueryDag(SQueryPlan *dag) {
D
dapan1121 已提交
78
  uint64_t qId = schtQueryId;
D
dapan1121 已提交
79 80 81
  
  dag->queryId = qId;
  dag->numOfSubplans = 2;
X
Xiaoyu Wang 已提交
82 83 84
  dag->pSubplans = nodesMakeList();
  SNodeListNode *scan = (SNodeListNode*)nodesMakeNode(QUERY_NODE_NODE_LIST);
  SNodeListNode *merge = (SNodeListNode*)nodesMakeNode(QUERY_NODE_NODE_LIST);
D
dapan1121 已提交
85
  
D
dapan1121 已提交
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;
X
Xiaoyu Wang 已提交
92
  scanPlan->subplanType = SUBPLAN_TYPE_SCAN;
H
Haojun Liao 已提交
93

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

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

  mergePlan->id.queryId = qId;
D
dapan1121 已提交
105
  mergePlan->id.templateId = schtMergeTemplateId;
X
Xiaoyu Wang 已提交
106 107
  mergePlan->id.subplanId = 0x5555;
  mergePlan->subplanType = SUBPLAN_TYPE_MERGE;
D
dapan1121 已提交
108
  mergePlan->level = 0;
H
Haojun Liao 已提交
109 110
  mergePlan->execNode.epset.numOfEps = 0;

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

X
Xiaoyu Wang 已提交
116 117
  nodesListAppend(merge->pNodeList, (SNode*)mergePlan);
  nodesListAppend(scan->pNodeList, (SNode*)scanPlan);
D
dapan1121 已提交
118

X
Xiaoyu Wang 已提交
119 120
  nodesListAppend(mergePlan->pChildren, (SNode*)scanPlan);
  nodesListAppend(scanPlan->pParents, (SNode*)mergePlan);
D
dapan1121 已提交
121

X
Xiaoyu Wang 已提交
122 123
  nodesListAppend(dag->pSubplans, (SNode*)merge);  
  nodesListAppend(dag->pSubplans, (SNode*)scan);
D
dapan1121 已提交
124 125
}

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

}


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

  insertPlan[0].id.queryId = qId;
  insertPlan[0].id.templateId = 0x0000000000000003;
  insertPlan[0].id.subplanId = 0x0000000000000004;
X
Xiaoyu Wang 已提交
144
  insertPlan[0].subplanType = SUBPLAN_TYPE_MODIFY;
D
dapan 已提交
145
  insertPlan[0].level = 0;
H
Haojun Liao 已提交
146

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

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

  insertPlan[1].id.queryId = qId;
  insertPlan[1].id.templateId = 0x0000000000000003;
  insertPlan[1].id.subplanId = 0x0000000000000005;
X
Xiaoyu Wang 已提交
160
  insertPlan[1].subplanType = SUBPLAN_TYPE_MODIFY;
D
dapan 已提交
161
  insertPlan[1].level = 0;
H
Haojun Liao 已提交
162

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

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

X
Xiaoyu Wang 已提交
173
  nodesListAppend(inserta->pNodeList, (SNode*)insertPlan);
D
dapan1121 已提交
174
  insertPlan += 1;
X
Xiaoyu Wang 已提交
175
  nodesListAppend(inserta->pNodeList, (SNode*)insertPlan);
D
dapan 已提交
176

X
Xiaoyu Wang 已提交
177
  nodesListAppend(dag->pSubplans, (SNode*)inserta);  
D
dapan 已提交
178 179 180
}


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

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

D
dapan 已提交
189 190
}

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

}

D
dapan 已提交
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 220
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 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233
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 已提交
234 235 236 237 238 239
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 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
  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 已提交
257

D
dapan 已提交
258
void *schtSendRsp(void *param) {
D
dapan1121 已提交
259 260
  SSchJob *pJob = NULL;
  int64_t job = 0;
D
dapan 已提交
261 262 263
  int32_t code = 0;

  while (true) {
D
dapan1121 已提交
264
    job = *(int64_t *)param;
D
dapan 已提交
265 266 267 268 269 270
    if (job) {
      break;
    }

    usleep(1000);
  }
D
dapan1121 已提交
271 272

  pJob = schAcquireJob(job);
D
dapan 已提交
273
  
D
dapan1121 已提交
274
  void *pIter = taosHashIterate(pJob->execTasks, NULL);
D
dapan 已提交
275 276 277
  while (pIter) {
    SSchTask *task = *(SSchTask **)pIter;

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

D
dapan1121 已提交
285 286
  schReleaseJob(job);

D
dapan 已提交
287 288 289
  return NULL;
}

D
dapan1121 已提交
290
void *schtCreateFetchRspThread(void *param) {
D
dapan1121 已提交
291 292
  int64_t job = *(int64_t *)param;
  SSchJob* pJob = schAcquireJob(job);
D
dapan1121 已提交
293 294 295 296 297 298 299

  sleep(1);

  int32_t code = 0;
  SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)calloc(1, sizeof(SRetrieveTableRsp));
  rsp->completed = 1;
  rsp->numOfRows = 10;
D
dapan1121 已提交
300
 
D
dapan1121 已提交
301 302 303 304
  code = schHandleResponseMsg(pJob, pJob->fetchTask, TDMT_VND_FETCH_RSP, (char *)rsp, sizeof(*rsp), 0);

  schReleaseJob(job);
  
D
dapan1121 已提交
305 306 307 308
  assert(code == 0);
}


D
dapan1121 已提交
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 337 338 339 340
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;
D
dapan1121 已提交
341
  int64_t job = queryJobRefId;
D
dapan1121 已提交
342
  
D
dapan1121 已提交
343
  if (job && atomic_val_compare_exchange_64(&queryJobRefId, job, 0)) {
D
dapan1121 已提交
344
    schedulerFreeJob(job);
D
dapan1121 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358
    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 已提交
359
  SQueryPlan dag;
D
dapan1121 已提交
360 361 362 363 364 365 366 367 368 369 370 371

  schtInitLogFile();

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


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

D
dapan1121 已提交
372
  SSchJob *pJob = NULL;
D
dapan1121 已提交
373 374 375 376 377 378 379 380
  SSchCallbackParam *param = NULL;
  SHashObj *execTasks = NULL;
  SDataBuf dataBuf = {0};
  uint32_t jobFinished = 0;

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

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

H
Haojun Liao 已提交
383
    SEp qnodeAddr = {0};
D
dapan1121 已提交
384 385 386 387
    strcpy(qnodeAddr.fqdn, "qnode0.ep");
    qnodeAddr.port = 6031;
    taosArrayPush(qnodeList, &qnodeAddr);

D
dapan1121 已提交
388
    code = schedulerAsyncExecJob(mockPointer, qnodeList, &dag, "select * from tb", &queryJobRefId);
D
dapan1121 已提交
389 390
    assert(code == 0);

D
dapan1121 已提交
391 392 393 394 395 396 397
    pJob = schAcquireJob(queryJobRefId);
    if (NULL == pJob) {
      taosArrayDestroy(qnodeList);
      schtFreeQueryDag(&dag);
      continue;
    }
    
D
dapan1121 已提交
398
    execTasks = taosHashInit(5, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_ENTRY_LOCK);
D
dapan1121 已提交
399
    void *pIter = taosHashIterate(pJob->execTasks, NULL);
D
dapan1121 已提交
400 401 402 403 404
    while (pIter) {
      SSchTask *task = *(SSchTask **)pIter;
      schtFetchTaskId = task->taskId - 1;
      
      taosHashPut(execTasks, &task->taskId, sizeof(task->taskId), task, sizeof(*task));
D
dapan1121 已提交
405
      pIter = taosHashIterate(pJob->execTasks, pIter);
D
dapan1121 已提交
406 407 408
    }    

    param = (SSchCallbackParam *)calloc(1, sizeof(*param));
D
dapan1121 已提交
409 410
    param->refId = queryJobRefId;
    param->queryId = pJob->queryId;   
D
dapan1121 已提交
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428

    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));
D
dapan1121 已提交
429 430 431
    param->refId = queryJobRefId;
    param->queryId = pJob->queryId;   
    
D
dapan1121 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
    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));
D
dapan1121 已提交
449 450
    param->refId = queryJobRefId;
    param->queryId = pJob->queryId;   
D
dapan1121 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468

    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));
D
dapan1121 已提交
469 470
    param->refId = queryJobRefId;
    param->queryId = pJob->queryId;   
D
dapan1121 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489

    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 已提交
490
    code = schedulerFetchRows(queryJobRefId, &data);
D
dapan1121 已提交
491 492 493 494 495 496 497 498 499
    assert(code == 0 || code);

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

    data = NULL;
D
dapan1121 已提交
500
    code = schedulerFetchRows(queryJobRefId, &data);
D
dapan1121 已提交
501 502 503 504 505
    assert(code == 0 || code);
    
    schtFreeQueryJob(0);

    taosHashCleanup(execTasks);
D
dapan1121 已提交
506
    taosArrayDestroy(qnodeList);
D
dapan1121 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

    schtFreeQueryDag(&dag);

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

    ++schtQueryId;
  }

  schedulerDestroy();

}

void* schtFreeJobThread(void *aa) {
  while (!schtTestStop) {
D
dapan1121 已提交
523
    usleep(rand() % 100);
D
dapan1121 已提交
524 525 526
    schtFreeQueryJob(1);
  }
}
D
dapan1121 已提交
527 528


D
dapan1121 已提交
529 530
}

D
dapan 已提交
531
TEST(queryTest, normalCase) {
D
dapan1121 已提交
532
  void *mockPointer = (void *)0x1;
D
dapan1121 已提交
533 534 535
  char *clusterId = "cluster1";
  char *dbname = "1.db1";
  char *tablename = "table1";
D
dapan1121 已提交
536
  SVgroupInfo vgInfo = {0};
D
dapan1121 已提交
537
  int64_t job = 0;
D
dapan1121 已提交
538
  SQueryPlan dag;
D
dapan1121 已提交
539 540 541

  schtInitLogFile();

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

H
Haojun Liao 已提交
544
  SEp qnodeAddr = {0};
D
dapan 已提交
545 546 547
  strcpy(qnodeAddr.fqdn, "qnode0.ep");
  qnodeAddr.port = 6031;
  taosArrayPush(qnodeList, &qnodeAddr);
D
dapan1121 已提交
548 549
  
  int32_t code = schedulerInit(NULL);
D
dapan1121 已提交
550
  ASSERT_EQ(code, 0);
D
dapan1121 已提交
551

D
dapan 已提交
552
  schtBuildQueryDag(&dag);
D
dapan 已提交
553 554 555

  schtSetPlanToString();
  schtSetExecNode();
D
dapan1121 已提交
556
  schtSetAsyncSendMsgToServer();
D
dapan1121 已提交
557
  
D
dapan1121 已提交
558
  code = schedulerAsyncExecJob(mockPointer, qnodeList, &dag, "select * from tb", &job);
D
dapan1121 已提交
559
  ASSERT_EQ(code, 0);
D
dapan 已提交
560

D
dapan1121 已提交
561 562 563 564
  
  SSchJob *pJob = schAcquireJob(job);
  
  void *pIter = taosHashIterate(pJob->execTasks, NULL);
D
dapan 已提交
565
  while (pIter) {
D
dapan 已提交
566
    SSchTask *task = *(SSchTask **)pIter;
D
dapan 已提交
567 568

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

D
dapan1121 已提交
575
  pIter = taosHashIterate(pJob->execTasks, NULL);
D
dapan 已提交
576
  while (pIter) {
D
dapan 已提交
577
    SSchTask *task = *(SSchTask **)pIter;
D
dapan 已提交
578 579

    SResReadyRsp rsp = {0};
D
dapan1121 已提交
580
    code = schHandleResponseMsg(pJob, task, TDMT_VND_RES_READY_RSP, (char *)&rsp, sizeof(rsp), 0);
D
dapan1121 已提交
581
    printf("code:%d", code);
D
dapan 已提交
582
    ASSERT_EQ(code, 0);
D
dapan1121 已提交
583
    pIter = taosHashIterate(pJob->execTasks, pIter);
D
dapan 已提交
584 585
  }  

D
dapan1121 已提交
586
  pIter = taosHashIterate(pJob->execTasks, NULL);
D
dapan 已提交
587
  while (pIter) {
D
dapan 已提交
588
    SSchTask *task = *(SSchTask **)pIter;
D
dapan 已提交
589 590

    SQueryTableRsp rsp = {0};
D
dapan1121 已提交
591
    code = schHandleResponseMsg(pJob, task, TDMT_VND_QUERY_RSP, (char *)&rsp, sizeof(rsp), 0);
D
dapan 已提交
592 593
    
    ASSERT_EQ(code, 0);
D
dapan1121 已提交
594
    pIter = taosHashIterate(pJob->execTasks, pIter);
D
dapan 已提交
595 596
  }    

D
dapan1121 已提交
597
  pIter = taosHashIterate(pJob->execTasks, NULL);
D
dapan 已提交
598
  while (pIter) {
D
dapan 已提交
599
    SSchTask *task = *(SSchTask **)pIter;
D
dapan 已提交
600 601

    SResReadyRsp rsp = {0};
D
dapan1121 已提交
602
    code = schHandleResponseMsg(pJob, task, TDMT_VND_RES_READY_RSP, (char *)&rsp, sizeof(rsp), 0);
D
dapan 已提交
603 604
    ASSERT_EQ(code, 0);
    
D
dapan1121 已提交
605
    pIter = taosHashIterate(pJob->execTasks, pIter);
D
dapan 已提交
606 607
  }  

D
dapan1121 已提交
608 609
  pthread_attr_t thattr;
  pthread_attr_init(&thattr);
D
dapan 已提交
610

D
dapan1121 已提交
611
  pthread_t thread1;
D
dapan1121 已提交
612
  pthread_create(&(thread1), &thattr, schtCreateFetchRspThread, &job);
D
dapan 已提交
613

D
dapan1121 已提交
614
  void *data = NULL;  
D
dapan1121 已提交
615
  code = schedulerFetchRows(job, &data);
D
dapan 已提交
616 617 618 619 620
  ASSERT_EQ(code, 0);

  SRetrieveTableRsp *pRsp = (SRetrieveTableRsp *)data;
  ASSERT_EQ(pRsp->completed, 1);
  ASSERT_EQ(pRsp->numOfRows, 10);
D
dapan1121 已提交
621
  tfree(data);
D
dapan 已提交
622 623

  data = NULL;
D
dapan1121 已提交
624
  code = schedulerFetchRows(job, &data);
D
dapan 已提交
625
  ASSERT_EQ(code, 0);
D
dapan1121 已提交
626 627 628
  ASSERT_TRUE(data == NULL);

  schReleaseJob(job);
D
dapan 已提交
629

D
dapan1121 已提交
630
  schedulerFreeJob(job);
D
dapan1121 已提交
631 632

  schtFreeQueryDag(&dag);
D
dapan1121 已提交
633 634

  schedulerDestroy();
D
dapan1121 已提交
635
}
D
dapan1121 已提交
636 637


D
dapan 已提交
638 639 640 641 642 643 644

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

  schtInitLogFile();
H
Haojun Liao 已提交
649

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

H
Haojun Liao 已提交
652
  SEp qnodeAddr = {0};
D
dapan 已提交
653 654 655 656 657 658 659 660 661 662
  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 已提交
663
  schtSetAsyncSendMsgToServer();
D
dapan 已提交
664 665 666 667 668

  pthread_attr_t thattr;
  pthread_attr_init(&thattr);

  pthread_t thread1;
D
dapan1121 已提交
669
  pthread_create(&(thread1), &thattr, schtSendRsp, &insertJobRefId);
D
dapan1121 已提交
670 671

  SQueryResult res = {0};
D
dapan1121 已提交
672
  code = schedulerExecJob(mockPointer, qnodeList, &dag, &insertJobRefId, "insert into tb values(now,1)", &res);
D
dapan 已提交
673
  ASSERT_EQ(code, 0);
D
dapan1121 已提交
674
  ASSERT_EQ(res.numOfRows, 20);
D
dapan 已提交
675

D
dapan1121 已提交
676
  schedulerFreeJob(insertJobRefId);
D
dapan1121 已提交
677 678

  schedulerDestroy();  
D
dapan 已提交
679 680
}

D
dapan1121 已提交
681
TEST(multiThread, forceFree) {
D
dapan1121 已提交
682 683
  pthread_attr_t thattr;
  pthread_attr_init(&thattr);
D
dapan 已提交
684

D
dapan1121 已提交
685 686 687 688
  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 已提交
689

D
dapan1121 已提交
690 691 692 693 694 695 696 697 698 699 700
  while (true) {
    if (schtTestDeadLoop) {
      sleep(1);
    } else {
      sleep(schtTestMTRunSec);
      break;
    }
  }
  
  schtTestStop = true;
  sleep(3);
D
dapan1121 已提交
701
}
D
dapan 已提交
702

D
dapan1121 已提交
703
int main(int argc, char** argv) {
D
dapan1121 已提交
704
  srand(time(NULL));
D
dapan1121 已提交
705 706 707 708
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

D
dapan1121 已提交
709
#pragma GCC diagnostic pop