qworkerTests.cpp 25.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 25 26 27 28 29 30
/*
 * 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>
#pragma GCC diagnostic ignored "-Wwrite-strings"

#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"
#include "os.h"

#include "taos.h"
#include "tdef.h"
#include "tvariant.h"
#include "tep.h"
#include "trpc.h"
D
dapan 已提交
31 32
#include "planner.h"
#include "qworker.h"
D
dapan1121 已提交
33 34
#include "stub.h"
#include "addr_any.h"
D
dapan1121 已提交
35 36
#include "executor.h"
#include "dataSinkMgt.h"
D
dapan1121 已提交
37 38 39 40


namespace {

D
dapan1121 已提交
41 42 43 44
#define qwtTestQueryQueueSize 1000
#define qwtTestFetchQueueSize 1000
#define qwtTestMaxExecTaskUsec 1000000

D
dapan1121 已提交
45 46 47 48 49 50 51
bool qwtTestEnableSleep = true;
bool qwtTestStop = false;
bool qwtTestDeadLoop = true;
int32_t qwtTestMTRunSec = 10;
int32_t qwtTestPrintNum = 100000;
int32_t qwtTestCaseIdx = 0;
int32_t qwtTestCaseNum = 4;
D
dapan1121 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
bool qwtTestCaseFinished = false;
tsem_t qwtTestQuerySem;
tsem_t qwtTestFetchSem;

int32_t qwtTestQueryQueueRIdx = 0;
int32_t qwtTestQueryQueueWIdx = 0;
int32_t qwtTestQueryQueueNum = 0;
SRWLatch qwtTestQueryQueueLock = 0;
struct SRpcMsg *qwtTestQueryQueue[qwtTestQueryQueueSize] = {0};

int32_t qwtTestFetchQueueRIdx = 0;
int32_t qwtTestFetchQueueWIdx = 0;
int32_t qwtTestFetchQueueNum = 0;
SRWLatch qwtTestFetchQueueLock = 0;
struct SRpcMsg *qwtTestFetchQueue[qwtTestFetchQueueSize] = {0};


int32_t qwtTestSinkBlockNum = 0;
int32_t qwtTestSinkMaxBlockNum = 0;
bool qwtTestSinkQueryEnd = false;
SRWLatch qwtTestSinkLock = 0;


SRpcMsg qwtfetchRpc = {0};
SResFetchReq qwtfetchMsg = {0};
SRpcMsg qwtreadyRpc = {0};
SResReadyReq qwtreadyMsg = {0};
SRpcMsg qwtdropRpc = {0};
STaskDropReq qwtdropMsg = {0};  

D
dapan1121 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

void qwtInitLogFile() {
  const char    *defaultLogFileNamePrefix = "taosdlog";
  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);
  }

}

void qwtBuildQueryReqMsg(SRpcMsg *queryRpc) {
  SSubQueryMsg *queryMsg = (SSubQueryMsg *)calloc(1, sizeof(SSubQueryMsg) + 100);
  queryMsg->queryId = htobe64(1);
  queryMsg->sId = htobe64(1);
  queryMsg->taskId = htobe64(1);
  queryMsg->contentLen = htonl(100);
  queryRpc->pCont = queryMsg;
  queryRpc->contLen = sizeof(SSubQueryMsg) + 100;
}

void qwtBuildReadyReqMsg(SResReadyReq *readyMsg, SRpcMsg *readyRpc) {
  readyMsg->sId = htobe64(1);
  readyMsg->queryId = htobe64(1);
  readyMsg->taskId = htobe64(1);
  readyRpc->pCont = readyMsg;
  readyRpc->contLen = sizeof(SResReadyReq);
}

void qwtBuildFetchReqMsg(SResFetchReq *fetchMsg, SRpcMsg *fetchRpc) {
  fetchMsg->sId = htobe64(1);
  fetchMsg->queryId = htobe64(1);
  fetchMsg->taskId = htobe64(1);
  fetchRpc->pCont = fetchMsg;
  fetchRpc->contLen = sizeof(SResFetchReq);
}

void qwtBuildDropReqMsg(STaskDropReq *dropMsg, SRpcMsg *dropRpc) {
  dropMsg->sId = htobe64(1);
  dropMsg->queryId = htobe64(1);
  dropMsg->taskId = htobe64(1);
  dropRpc->pCont = dropMsg;
  dropRpc->contLen = sizeof(STaskDropReq);
}

void qwtBuildStatusReqMsg(SSchTasksStatusReq *statusMsg, SRpcMsg *statusRpc) {
  statusMsg->sId = htobe64(1);
  statusRpc->pCont = statusMsg;
  statusRpc->contLen = sizeof(SSchTasksStatusReq);
  statusRpc->msgType = TDMT_VND_TASKS_STATUS;
}
D
dapan1121 已提交
138

D
dapan 已提交
139
int32_t qwtStringToPlan(const char* str, SSubplan** subplan) {
D
dapan1121 已提交
140
  *subplan = 0x1;
D
dapan 已提交
141
  return 0;
D
dapan1121 已提交
142 143
}

D
dapan1121 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
int32_t qwtPutReqToFetchQueue(void *node, struct SRpcMsg *pMsg) {
  taosWLockLatch(&qwtTestFetchQueueLock);
  qwtTestFetchQueue[qwtTestFetchQueueWIdx++] = pMsg;
  if (qwtTestFetchQueueWIdx >= qwtTestFetchQueueSize) {
    qwtTestFetchQueueWIdx = 0;
  }
  
  qwtTestFetchQueueNum++;

  if (qwtTestFetchQueueWIdx == qwtTestFetchQueueRIdx) {
    printf("Fetch queue is full");
    assert(0);
  }
  taosWUnLockLatch(&qwtTestFetchQueueLock);
  
  tsem_post(&qwtTestFetchSem);
  
  return 0;
}


D
dapan1121 已提交
165
int32_t qwtPutReqToQueue(void *node, struct SRpcMsg *pMsg) {
D
dapan1121 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  taosWLockLatch(&qwtTestQueryQueueLock);
  qwtTestQueryQueue[qwtTestQueryQueueWIdx++] = pMsg;
  if (qwtTestQueryQueueWIdx >= qwtTestQueryQueueSize) {
    qwtTestQueryQueueWIdx = 0;
  }
  
  qwtTestQueryQueueNum++;

  if (qwtTestQueryQueueWIdx == qwtTestQueryQueueRIdx) {
    printf("query queue is full");
    assert(0);
  }
  taosWUnLockLatch(&qwtTestQueryQueueLock);
  
  tsem_post(&qwtTestQuerySem);
  
D
dapan1121 已提交
182 183 184 185
  return 0;
}


D
dapan1121 已提交
186

D
dapan1121 已提交
187
void qwtRpcSendResponse(const SRpcMsg *pRsp) {
D
dapan1121 已提交
188 189 190 191 192 193 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 220 221 222 223 224 225 226 227 228 229 230 231 232 233

  switch (pRsp->msgType) {
    case TDMT_VND_QUERY_RSP: {
      SQueryTableRsp *rsp = (SQueryTableRsp *)pRsp->pCont;

      if (0 == pRsp->code) {
        qwtBuildReadyReqMsg(&qwtreadyMsg, &qwtreadyRpc);    
        qwtPutReqToFetchQueue(0x1, &qwtreadyRpc);
      } else {
        qwtBuildDropReqMsg(&qwtdropMsg, &qwtdropRpc);
        qwtPutReqToFetchQueue(0x1, &qwtdropRpc);
      }
      
      break;
    }
    case TDMT_VND_RES_READY_RSP: {
      SResReadyRsp *rsp = (SResReadyRsp *)pRsp->pCont;
      
      if (0 == pRsp->code) {
        qwtBuildFetchReqMsg(&qwtfetchMsg, &qwtfetchRpc);
        qwtPutReqToFetchQueue(0x1, &qwtfetchRpc);
      } else {
        qwtBuildDropReqMsg(&qwtdropMsg, &qwtdropRpc);
        qwtPutReqToFetchQueue(0x1, &qwtdropRpc);
      }
      break;
    }
    case TDMT_VND_FETCH_RSP: {
      SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)pRsp->pCont;
  
      if (0 == pRsp->code && 0 == rsp->completed) {
        qwtBuildFetchReqMsg(&qwtfetchMsg, &qwtfetchRpc);
        qwtPutReqToFetchQueue(0x1, &qwtfetchRpc);
        return;
      }

      qwtBuildDropReqMsg(&qwtdropMsg, &qwtdropRpc);
      qwtPutReqToFetchQueue(0x1, &qwtdropRpc);
      
      break;
    }
    case TDMT_VND_DROP_TASK: {
      STaskDropRsp *rsp = (STaskDropRsp *)pRsp->pCont;

      qwtTestCaseFinished = true;
      break;
D
dapan1121 已提交
234 235
    }
  }
D
dapan1121 已提交
236
  
D
dapan1121 已提交
237 238 239
  return;
}

D
dapan1121 已提交
240
int32_t qwtCreateExecTask(void* tsdb, int32_t vgId, struct SSubplan* pPlan, qTaskInfo_t* pTaskInfo, DataSinkHandle* handle) {
D
dapan1121 已提交
241 242 243 244 245
  int32_t idx = abs((++qwtTestCaseIdx) % qwtTestCaseNum);

  qwtTestSinkBlockNum = 0;
  qwtTestSinkMaxBlockNum = rand() % 100 + 1;
  qwtTestSinkQueryEnd = false;
D
dapan1121 已提交
246 247
  
  if (0 == idx) {
D
dapan1121 已提交
248 249
    *pTaskInfo = (qTaskInfo_t)qwtTestCaseIdx;
    *handle = (DataSinkHandle)qwtTestCaseIdx+1;
D
dapan1121 已提交
250 251 252 253
  } else if (1 == idx) {
    *pTaskInfo = NULL;
    *handle = NULL;
  } else if (2 == idx) {
D
dapan1121 已提交
254
    *pTaskInfo = (qTaskInfo_t)qwtTestCaseIdx;
D
dapan1121 已提交
255 256 257
    *handle = NULL;
  } else if (3 == idx) {
    *pTaskInfo = NULL;
D
dapan1121 已提交
258
    *handle = (DataSinkHandle)qwtTestCaseIdx;
D
dapan1121 已提交
259 260 261 262 263 264
  }
  
  return 0;
}

int32_t qwtExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t *useconds) {
D
dapan1121 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  int32_t endExec = 0;
  
  if (NULL == tinfo) {
    *pRes = NULL;
    *useconds = 0;
  } else {
    endExec = rand() % 5;
    
    if (endExec) {
      usleep(rand() % qwtTestMaxExecTaskUsec);
      
      *pRes = (SSDataBlock*)0x1;
    } else {
      *pRes = NULL;
      usleep(rand() % qwtTestMaxExecTaskUsec);
      *useconds = rand() % 10;
    }
  }
  
D
dapan1121 已提交
284 285 286 287 288 289 290 291 292 293 294 295
  return 0;
}

int32_t qwtKillTask(qTaskInfo_t qinfo) {
  return 0;
}

void qwtDestroyTask(qTaskInfo_t qHandle) {
}


int32_t qwtPutDataBlock(DataSinkHandle handle, const SInputData* pInput, bool* pContinue) {
D
dapan1121 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
  if (NULL == handle || NULL == pInput || NULL == pContinue) {
    assert(0);
  }

  taosWLockLatch(&qwtTestSinkLock);

  qwtTestSinkBlockNum++;

  if (qwtTestSinkBlockNum >= qwtTestSinkMaxBlockNum) {
    *pContinue = true;
  } else {
    *pContinue = false;
  }
  taosWUnLockLatch(&qwtTestSinkLock);
  
D
dapan1121 已提交
311 312 313 314
  return 0;
}

void qwtEndPut(DataSinkHandle handle, uint64_t useconds) {
D
dapan1121 已提交
315 316 317 318 319
  if (NULL == handle) {
    assert(0);
  }

  qwtTestSinkQueryEnd = true;
D
dapan1121 已提交
320 321 322
}

void qwtGetDataLength(DataSinkHandle handle, int32_t* pLen, bool* pQueryEnd) {
D
dapan1121 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
  static int32_t in = 0;

  if (in > 0) {
    assert(0);
  }

  atomic_add_fetch_32(&in, 1);
  
  if (NULL == handle) {
    assert(0);
  }

  taosWLockLatch(&qwtTestSinkLock);
  if (qwtTestSinkBlockNum > 0) {
    *pLen = rand() % 100 + 1;
    qwtTestSinkBlockNum--;
  } else {
    *pLen = 0;
  }
  taosWUnLockLatch(&qwtTestSinkLock);

  *pQueryEnd = qwtTestSinkQueryEnd;

  atomic_sub_fetch_32(&in, 1);
D
dapan1121 已提交
347 348 349
}

int32_t qwtGetDataBlock(DataSinkHandle handle, SOutputData* pOutput) {
D
dapan1121 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
  taosWLockLatch(&qwtTestSinkLock);
  if (qwtTestSinkBlockNum > 0) {
    qwtTestSinkBlockNum--;
    pOutput->numOfRows = rand() % 10 + 1;
    pOutput->compressed = 1;
    pOutput->pData = malloc(pOutput->numOfRows);
    pOutput->queryEnd = qwtTestSinkQueryEnd;
    if (qwtTestSinkBlockNum == 0) {
      pOutput->bufStatus = DS_BUF_EMPTY;
    } else if (qwtTestSinkBlockNum <= qwtTestSinkMaxBlockNum*0.5) {
      pOutput->bufStatus = DS_BUF_LOW;
    } else {
      pOutput->bufStatus = DS_BUF_FULL;
    }
    pOutput->useconds = rand() % 10 + 1;
    pOutput->precision = 1;
  } else {
    assert(0);
  }
  taosWUnLockLatch(&qwtTestSinkLock);
  
D
dapan1121 已提交
371 372 373 374 375 376 377
  return 0;
}

void qwtDestroyDataSinker(DataSinkHandle handle) {

}

D
dapan1121 已提交
378

D
dapan1121 已提交
379

D
dapan 已提交
380
void stubSetStringToPlan() {
D
dapan1121 已提交
381
  static Stub stub;
D
dapan 已提交
382
  stub.set(qStringToSubplan, qwtStringToPlan);
D
dapan1121 已提交
383
  {
D
dapan 已提交
384
    AddrAny any("libplanner.so");
D
dapan1121 已提交
385
    std::map<std::string,void*> result;
D
dapan 已提交
386
    any.get_global_func_addr_dynsym("^qStringToSubplan$", result);
D
dapan1121 已提交
387
    for (const auto& f : result) {
D
dapan 已提交
388
      stub.set(f.second, qwtStringToPlan);
D
dapan1121 已提交
389 390 391 392
    }
  }
}

D
dapan1121 已提交
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 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
void stubSetExecTask() {
  static Stub stub;
  stub.set(qExecTask, qwtExecTask);
  {
    AddrAny any("libexecutor.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^qExecTask$", result);
    for (const auto& f : result) {
      stub.set(f.second, qwtExecTask);
    }
  }
}



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

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

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


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

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

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

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

D
dapan1121 已提交
500 501 502 503
void stubSetRpcSendResponse() {
  static Stub stub;
  stub.set(rpcSendResponse, qwtRpcSendResponse);
  {
D
dapan1121 已提交
504
    AddrAny any("libtransport.so");
D
dapan1121 已提交
505 506 507 508 509 510 511 512
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^rpcSendResponse$", result);
    for (const auto& f : result) {
      stub.set(f.second, qwtRpcSendResponse);
    }
  }
}

D
dapan1121 已提交
513 514 515 516 517 518 519 520 521 522 523 524 525 526
void stubSetGetDataBlock() {
  static Stub stub;
  stub.set(dsGetDataBlock, qwtGetDataBlock);
  {
    AddrAny any("libtransport.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^dsGetDataBlock$", result);
    for (const auto& f : result) {
      stub.set(f.second, qwtGetDataBlock);
    }
  }
}


D
dapan1121 已提交
527 528 529 530 531 532 533
void *queryThread(void *param) {
  SRpcMsg queryRpc = {0};
  int32_t code = 0;
  uint32_t n = 0;
  void *mockPointer = (void *)0x1;    
  void *mgmt = param;

D
dapan1121 已提交
534 535 536 537 538 539 540 541
  while (!qwtTestStop) {
    qwtBuildQueryReqMsg(&queryRpc);
    qWorkerProcessQueryMsg(mockPointer, mgmt, &queryRpc);    
    free(queryRpc.pCont);
    if (qwtTestEnableSleep) {
      usleep(rand()%5);
    }
    if (++n % qwtTestPrintNum == 0) {
D
dapan1121 已提交
542 543 544 545 546 547 548 549 550 551 552 553 554
      printf("query:%d\n", n);
    }
  }

  return NULL;
}

void *readyThread(void *param) {
  SRpcMsg readyRpc = {0};
  int32_t code = 0;
  uint32_t n = 0;  
  void *mockPointer = (void *)0x1;    
  void *mgmt = param;
S
Shengliang Guan 已提交
555
  SResReadyReq readyMsg = {0};
D
dapan1121 已提交
556

D
dapan1121 已提交
557 558
  while (!qwtTestStop) {
    qwtBuildReadyReqMsg(&readyMsg, &readyRpc);
D
dapan1121 已提交
559
    code = qWorkerProcessReadyMsg(mockPointer, mgmt, &readyRpc);
D
dapan1121 已提交
560 561 562 563
    if (qwtTestEnableSleep) {
      usleep(rand()%5);
    }
    if (++n % qwtTestPrintNum == 0) {
D
dapan1121 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576
      printf("ready:%d\n", n);
    }    
  }

  return NULL;
}

void *fetchThread(void *param) {
  SRpcMsg fetchRpc = {0};
  int32_t code = 0;
  uint32_t n = 0;  
  void *mockPointer = (void *)0x1;    
  void *mgmt = param;
S
Shengliang Guan 已提交
577
  SResFetchReq fetchMsg = {0};
D
dapan1121 已提交
578

D
dapan1121 已提交
579 580
  while (!qwtTestStop) {
    qwtBuildFetchReqMsg(&fetchMsg, &fetchRpc);
D
dapan1121 已提交
581
    code = qWorkerProcessFetchMsg(mockPointer, mgmt, &fetchRpc);
D
dapan1121 已提交
582 583 584 585
    if (qwtTestEnableSleep) {
      usleep(rand()%5);
    }
    if (++n % qwtTestPrintNum == 0) {
D
dapan1121 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598
      printf("fetch:%d\n", n);
    }    
  }

  return NULL;
}

void *dropThread(void *param) {
  SRpcMsg dropRpc = {0};
  int32_t code = 0;
  uint32_t n = 0;  
  void *mockPointer = (void *)0x1;    
  void *mgmt = param;
S
Shengliang Guan 已提交
599
  STaskDropReq dropMsg = {0};  
D
dapan1121 已提交
600

D
dapan1121 已提交
601 602
  while (!qwtTestStop) {
    qwtBuildDropReqMsg(&dropMsg, &dropRpc);
D
dapan1121 已提交
603
    code = qWorkerProcessDropMsg(mockPointer, mgmt, &dropRpc);
D
dapan1121 已提交
604 605 606 607
    if (qwtTestEnableSleep) {
      usleep(rand()%5);
    }
    if (++n % qwtTestPrintNum == 0) {
D
dapan1121 已提交
608 609 610 611 612 613 614 615 616 617 618 619 620
      printf("drop:%d\n", n);
    }    
  }

  return NULL;
}

void *statusThread(void *param) {
  SRpcMsg statusRpc = {0};
  int32_t code = 0;
  uint32_t n = 0;  
  void *mockPointer = (void *)0x1;    
  void *mgmt = param;
S
Shengliang Guan 已提交
621
  SSchTasksStatusReq statusMsg = {0};
D
dapan1121 已提交
622

D
dapan1121 已提交
623 624
  while (!qwtTestStop) {
    qwtBuildStatusReqMsg(&statusMsg, &statusRpc);
D
dapan1121 已提交
625
    code = qWorkerProcessStatusMsg(mockPointer, mgmt, &statusRpc);
D
dapan1121 已提交
626 627 628 629
    if (qwtTestEnableSleep) {
      usleep(rand()%5);
    }
    if (++n % qwtTestPrintNum == 0) {
D
dapan1121 已提交
630 631 632 633 634 635 636 637
      printf("status:%d\n", n);
    }    
  }

  return NULL;
}


D
dapan1121 已提交
638
void *clientThread(void *param) {
D
dapan1121 已提交
639 640 641
  int32_t code = 0;
  uint32_t n = 0;
  void *mgmt = param;
D
dapan1121 已提交
642 643 644 645
  void *mockPointer = (void *)0x1;    
  SRpcMsg queryRpc = {0};

  sleep(1);
D
dapan1121 已提交
646 647

  while (!qwtTestStop) {
D
dapan1121 已提交
648 649
    qwtTestCaseFinished = false;
    
D
dapan1121 已提交
650
    qwtBuildQueryReqMsg(&queryRpc);
D
dapan1121 已提交
651 652 653 654 655 656
    qwtPutReqToQueue(0x1, &queryRpc);

    while (!qwtTestCaseFinished) {
      usleep(1);
    }
    
D
dapan1121 已提交
657
    free(queryRpc.pCont);
D
dapan1121 已提交
658
    
D
dapan1121 已提交
659 660 661
    if (qwtTestEnableSleep) {
      usleep(rand()%5);
    }
D
dapan1121 已提交
662
    
D
dapan1121 已提交
663 664 665 666 667 668 669
    if (++n % qwtTestPrintNum == 0) {
      printf("query:%d\n", n);
    }
  }

  return NULL;
}
D
dapan1121 已提交
670

D
dapan1121 已提交
671
void *queryQueueThread(void *param) {
D
dapan1121 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
  void *mockPointer = (void *)0x1;   
  SRpcMsg *queryRpc = NULL;
  void *mgmt = param;

  while (!qwtTestStop) {
    tsem_wait(&qwtTestQuerySem);

    taosWLockLatch(&qwtTestQueryQueueLock);
    if (qwtTestQueryQueueNum <= 0 || qwtTestQueryQueueRIdx == qwtTestQueryQueueWIdx) {
      printf("query queue is empty\n");
      assert(0);
    }
    
    queryRpc = qwtTestQueryQueue[qwtTestQueryQueueRIdx++];
    
    if (qwtTestQueryQueueRIdx >= qwtTestQueryQueueSize) {
      qwtTestQueryQueueRIdx = 0;
    }
    
    qwtTestQueryQueueNum--;
    taosWUnLockLatch(&qwtTestQueryQueueLock);

    if (TDMT_VND_QUERY == queryRpc->msgType) {
      qWorkerProcessQueryMsg(mockPointer, mgmt, &queryRpc);
    } else if (TDMT_VND_QUERY_CONTINUE == queryRpc->msgType) {
      qWorkerProcessCQueryMsg(mockPointer, mgmt, &queryRpc)
    } else {
      printf("unknown msg in query queue, type:%d\n", queryRpc->msgType);
      assert(0);
    }
  }
D
dapan1121 已提交
703 704 705 706

}

void *fetchQueueThread(void *param) {
D
dapan1121 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
  void *mockPointer = (void *)0x1;   
  SRpcMsg *fetchRpc = NULL;
  void *mgmt = param;

  while (!qwtTestStop) {
    tsem_wait(&qwtTestFetchSem);

    taosWLockLatch(&qwtTestFetchQueueLock);
    if (qwtTestFetchQueueNum <= 0 || qwtTestFetchQueueRIdx == qwtTestFetchQueueWIdx) {
      printf("Fetch queue is empty\n");
      assert(0);
    }
    
    fetchRpc = qwtTestFetchQueue[qwtTestFetchQueueRIdx++];
    
    if (qwtTestFetchQueueRIdx >= qwtTestFetchQueueSize) {
      qwtTestFetchQueueRIdx = 0;
    }
    
    qwtTestFetchQueueNum--;
    taosWUnLockLatch(&qwtTestFetchQueueLock);

    switch (fetchRpc->msgType) {
      case TDMT_VND_FETCH:
        qWorkerProcessFetchMsg(mockPointer, mgmt, fetchRpc);
      case TDMT_VND_RES_READY:
        qWorkerProcessReadyMsg(mockPointer, mgmt, fetchRpc);
      case TDMT_VND_TASKS_STATUS:
        qWorkerProcessStatusMsg(mockPointer, mgmt, fetchRpc);
      case TDMT_VND_CANCEL_TASK:
        qWorkerProcessCancelMsg(mockPointer, mgmt, fetchRpc);
      case TDMT_VND_DROP_TASK:
        qWorkerProcessDropMsg(mockPointer, mgmt, fetchRpc);
      default:
        printf("unknown msg type:%d in fetch queue", fetchRpc->msgType);
        assert(0);
    }
  }
D
dapan1121 已提交
745 746 747

}

D
dapan1121 已提交
748

D
dapan1121 已提交
749 750 751 752

}


D
dapan1121 已提交
753
TEST(seqTest, normalCase) {
D
dapan 已提交
754 755 756 757 758 759 760
  void *mgmt = NULL;
  int32_t code = 0;
  void *mockPointer = (void *)0x1;
  SRpcMsg queryRpc = {0};
  SRpcMsg readyRpc = {0};
  SRpcMsg fetchRpc = {0};
  SRpcMsg dropRpc = {0};
D
dapan1121 已提交
761
  SRpcMsg statusRpc = {0};
D
dapan1121 已提交
762 763

  qwtInitLogFile();
D
dapan1121 已提交
764
  
D
dapan 已提交
765 766
  SSubQueryMsg *queryMsg = (SSubQueryMsg *)calloc(1, sizeof(SSubQueryMsg) + 100);
  queryMsg->queryId = htobe64(1);
D
dapan1121 已提交
767
  queryMsg->sId = htobe64(1);
D
dapan 已提交
768 769 770
  queryMsg->taskId = htobe64(1);
  queryMsg->contentLen = htonl(100);
  queryRpc.pCont = queryMsg;
D
dapan1121 已提交
771
  queryRpc.contLen = sizeof(SSubQueryMsg) + 100;
D
dapan 已提交
772

S
Shengliang Guan 已提交
773
  SResReadyReq readyMsg = {0};
D
dapan1121 已提交
774
  readyMsg.sId = htobe64(1);
D
dapan 已提交
775 776 777
  readyMsg.queryId = htobe64(1);
  readyMsg.taskId = htobe64(1);
  readyRpc.pCont = &readyMsg;
S
Shengliang Guan 已提交
778
  readyRpc.contLen = sizeof(SResReadyReq);
D
dapan 已提交
779

S
Shengliang Guan 已提交
780
  SResFetchReq fetchMsg = {0};
D
dapan1121 已提交
781
  fetchMsg.sId = htobe64(1);
D
dapan 已提交
782 783 784
  fetchMsg.queryId = htobe64(1);
  fetchMsg.taskId = htobe64(1);
  fetchRpc.pCont = &fetchMsg;
S
Shengliang Guan 已提交
785
  fetchRpc.contLen = sizeof(SResFetchReq);
D
dapan 已提交
786

S
Shengliang Guan 已提交
787
  STaskDropReq dropMsg = {0};  
D
dapan1121 已提交
788
  dropMsg.sId = htobe64(1);
D
dapan 已提交
789 790 791
  dropMsg.queryId = htobe64(1);
  dropMsg.taskId = htobe64(1);
  dropRpc.pCont = &dropMsg;
S
Shengliang Guan 已提交
792
  dropRpc.contLen = sizeof(STaskDropReq);
D
dapan 已提交
793

S
Shengliang Guan 已提交
794
  SSchTasksStatusReq statusMsg = {0};
D
dapan1121 已提交
795 796
  statusMsg.sId = htobe64(1);
  statusRpc.pCont = &statusMsg;
S
Shengliang Guan 已提交
797
  statusRpc.contLen = sizeof(SSchTasksStatusReq);
D
dapan1121 已提交
798 799
  statusRpc.msgType = TDMT_VND_TASKS_STATUS;
  
D
dapan 已提交
800
  stubSetStringToPlan();
D
dapan1121 已提交
801
  stubSetRpcSendResponse();
D
dapan1121 已提交
802 803 804 805 806 807 808 809 810
  stubSetExecTask();
  stubSetCreateExecTask();
  stubSetAsyncKillTask();
  stubSetDestroyTask();
  stubSetDestroyDataSinker();
  stubSetGetDataLength();
  stubSetEndPut();
  stubSetPutDataBlock();
  stubSetGetDataBlock();
D
dapan 已提交
811
  
D
dapan1121 已提交
812
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, mockPointer, qwtPutReqToQueue);
D
dapan 已提交
813 814
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
815 816 817 818
  statusMsg.sId = htobe64(1);
  code = qWorkerProcessStatusMsg(mockPointer, mgmt, &statusRpc);
  ASSERT_EQ(code, 0);

D
dapan 已提交
819 820 821
  code = qWorkerProcessQueryMsg(mockPointer, mgmt, &queryRpc);
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
822 823 824 825
  statusMsg.sId = htobe64(1);
  code = qWorkerProcessStatusMsg(mockPointer, mgmt, &statusRpc);
  ASSERT_EQ(code, 0);

D
dapan 已提交
826 827 828
  code = qWorkerProcessReadyMsg(mockPointer, mgmt, &readyRpc);
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
829 830 831 832
  statusMsg.sId = htobe64(1);
  code = qWorkerProcessStatusMsg(mockPointer, mgmt, &statusRpc);
  ASSERT_EQ(code, 0);

D
dapan 已提交
833 834 835
  code = qWorkerProcessFetchMsg(mockPointer, mgmt, &fetchRpc);
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
  statusMsg.sId = htobe64(1);
  code = qWorkerProcessStatusMsg(mockPointer, mgmt, &statusRpc);
  ASSERT_EQ(code, 0);

  code = qWorkerProcessDropMsg(mockPointer, mgmt, &dropRpc);
  ASSERT_EQ(code, 0);

  statusMsg.sId = htobe64(1);
  code = qWorkerProcessStatusMsg(mockPointer, mgmt, &statusRpc);
  ASSERT_EQ(code, 0);

  qWorkerDestroy(&mgmt);
}

TEST(seqTest, cancelFirst) {
  void *mgmt = NULL;
  int32_t code = 0;
  void *mockPointer = (void *)0x1;
  SRpcMsg queryRpc = {0};
  SRpcMsg dropRpc = {0};
  SRpcMsg statusRpc = {0};
D
dapan1121 已提交
857 858

  qwtInitLogFile();
D
dapan1121 已提交
859 860 861 862 863 864 865 866 867
  
  SSubQueryMsg *queryMsg = (SSubQueryMsg *)calloc(1, sizeof(SSubQueryMsg) + 100);
  queryMsg->queryId = htobe64(1);
  queryMsg->sId = htobe64(1);
  queryMsg->taskId = htobe64(1);
  queryMsg->contentLen = htonl(100);
  queryRpc.pCont = queryMsg;
  queryRpc.contLen = sizeof(SSubQueryMsg) + 100;

S
Shengliang Guan 已提交
868
  STaskDropReq dropMsg = {0};  
D
dapan1121 已提交
869 870 871 872
  dropMsg.sId = htobe64(1);
  dropMsg.queryId = htobe64(1);
  dropMsg.taskId = htobe64(1);
  dropRpc.pCont = &dropMsg;
S
Shengliang Guan 已提交
873
  dropRpc.contLen = sizeof(STaskDropReq);
D
dapan1121 已提交
874

S
Shengliang Guan 已提交
875
  SSchTasksStatusReq statusMsg = {0};
D
dapan1121 已提交
876 877
  statusMsg.sId = htobe64(1);
  statusRpc.pCont = &statusMsg;
S
Shengliang Guan 已提交
878
  statusRpc.contLen = sizeof(SSchTasksStatusReq);
D
dapan1121 已提交
879 880 881 882 883
  statusRpc.msgType = TDMT_VND_TASKS_STATUS;

  stubSetStringToPlan();
  stubSetRpcSendResponse();
  
D
dapan1121 已提交
884
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, mockPointer, qwtPutReqToQueue);
D
dapan1121 已提交
885 886 887 888 889 890
  ASSERT_EQ(code, 0);

  statusMsg.sId = htobe64(1);
  code = qWorkerProcessStatusMsg(mockPointer, mgmt, &statusRpc);
  ASSERT_EQ(code, 0);

D
dapan 已提交
891 892
  code = qWorkerProcessDropMsg(mockPointer, mgmt, &dropRpc);
  ASSERT_EQ(code, 0);
D
dapan1121 已提交
893

D
dapan1121 已提交
894 895 896 897 898
  statusMsg.sId = htobe64(1);
  code = qWorkerProcessStatusMsg(mockPointer, mgmt, &statusRpc);
  ASSERT_EQ(code, 0);

  code = qWorkerProcessQueryMsg(mockPointer, mgmt, &queryRpc);
D
dapan1121 已提交
899
  ASSERT_EQ(code, TSDB_CODE_QRY_TASK_DROPPED);
D
dapan1121 已提交
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916

  statusMsg.sId = htobe64(1);
  code = qWorkerProcessStatusMsg(mockPointer, mgmt, &statusRpc);
  ASSERT_EQ(code, 0);

  qWorkerDestroy(&mgmt);
}

TEST(seqTest, randCase) {
  void *mgmt = NULL;
  int32_t code = 0;
  void *mockPointer = (void *)0x1;
  SRpcMsg queryRpc = {0};
  SRpcMsg readyRpc = {0};
  SRpcMsg fetchRpc = {0};
  SRpcMsg dropRpc = {0};
  SRpcMsg statusRpc = {0};
S
Shengliang Guan 已提交
917 918 919 920
  SResReadyReq readyMsg = {0};
  SResFetchReq fetchMsg = {0};
  STaskDropReq dropMsg = {0};  
  SSchTasksStatusReq statusMsg = {0};
D
dapan1121 已提交
921 922

  qwtInitLogFile();
D
dapan1121 已提交
923 924 925
  
  stubSetStringToPlan();
  stubSetRpcSendResponse();
D
dapan1121 已提交
926
  stubSetCreateExecTask();
D
dapan1121 已提交
927 928 929

  srand(time(NULL));
  
D
dapan1121 已提交
930
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, mockPointer, qwtPutReqToQueue);
D
dapan1121 已提交
931 932 933 934 935 936 937 938
  ASSERT_EQ(code, 0);

  int32_t t = 0;
  int32_t maxr = 10001;
  while (true) {
    int32_t r = rand() % maxr;
    
    if (r >= 0 && r < maxr/5) {
D
dapan1121 已提交
939 940
      printf("Query,%d\n", t++);      
      qwtBuildQueryReqMsg(&queryRpc);
D
dapan1121 已提交
941
      code = qWorkerProcessQueryMsg(mockPointer, mgmt, &queryRpc);
D
dapan1121 已提交
942
      free(queryRpc.pCont);
D
dapan1121 已提交
943 944
    } else if (r >= maxr/5 && r < maxr * 2/5) {
      printf("Ready,%d\n", t++);
D
dapan1121 已提交
945
      qwtBuildReadyReqMsg(&readyMsg, &readyRpc);
D
dapan1121 已提交
946 947 948
      code = qWorkerProcessReadyMsg(mockPointer, mgmt, &readyRpc);
    } else if (r >= maxr * 2/5 && r < maxr* 3/5) {
      printf("Fetch,%d\n", t++);
D
dapan1121 已提交
949
      qwtBuildFetchReqMsg(&fetchMsg, &fetchRpc);
D
dapan1121 已提交
950 951 952
      code = qWorkerProcessFetchMsg(mockPointer, mgmt, &fetchRpc);
    } else if (r >= maxr * 3/5 && r < maxr * 4/5) {
      printf("Drop,%d\n", t++);
D
dapan1121 已提交
953
      qwtBuildDropReqMsg(&dropMsg, &dropRpc);
D
dapan1121 已提交
954 955 956
      code = qWorkerProcessDropMsg(mockPointer, mgmt, &dropRpc);
    } else if (r >= maxr * 4/5 && r < maxr-1) {
      printf("Status,%d\n", t++);
D
dapan1121 已提交
957
      qwtBuildStatusReqMsg(&statusMsg, &statusRpc);
D
dapan1121 已提交
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
      code = qWorkerProcessStatusMsg(mockPointer, mgmt, &statusRpc);
      ASSERT_EQ(code, 0);
    } else {
      printf("QUIT RAND NOW");
      break;
    }
  }

  qWorkerDestroy(&mgmt);
}

TEST(seqTest, multithreadRand) {
  void *mgmt = NULL;
  int32_t code = 0;
  void *mockPointer = (void *)0x1;
D
dapan1121 已提交
973 974

  qwtInitLogFile();
D
dapan1121 已提交
975 976 977 978 979 980
  
  stubSetStringToPlan();
  stubSetRpcSendResponse();

  srand(time(NULL));
  
D
dapan1121 已提交
981
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, mockPointer, qwtPutReqToQueue);
D
dapan1121 已提交
982 983 984 985 986 987 988 989 990 991 992 993
  ASSERT_EQ(code, 0);

  pthread_attr_t thattr;
  pthread_attr_init(&thattr);

  pthread_t t1,t2,t3,t4,t5;
  pthread_create(&(t1), &thattr, queryThread, mgmt);
  pthread_create(&(t2), &thattr, readyThread, NULL);
  pthread_create(&(t3), &thattr, fetchThread, NULL);
  pthread_create(&(t4), &thattr, dropThread, NULL);
  pthread_create(&(t5), &thattr, statusThread, NULL);

D
dapan1121 已提交
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
  while (true) {
    if (qwtTestDeadLoop) {
      sleep(1);
    } else {
      sleep(qwtTestMTRunSec);
      break;
    }
  }
  
  qwtTestStop = true;
  sleep(3);
  
  qWorkerDestroy(&mgmt);
}

TEST(rcTest, multithread) {
  void *mgmt = NULL;
  int32_t code = 0;
  void *mockPointer = (void *)0x1;

  qwtInitLogFile();
  
  stubSetStringToPlan();
  stubSetRpcSendResponse();
  stubSetExecTask();
  stubSetCreateExecTask();
  stubSetAsyncKillTask();
  stubSetDestroyTask();
  stubSetDestroyDataSinker();
  stubSetGetDataLength();
  stubSetEndPut();
  stubSetPutDataBlock();
  stubSetGetDataBlock();

  srand(time(NULL));
  
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, mockPointer, qwtPutReqToQueue);
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
1033 1034 1035
  tsem_init(&qwtTestQuerySem, 0, 0);
  tsem_init(&qwtTestFetchSem, 0, 0);

D
dapan1121 已提交
1036 1037 1038 1039
  pthread_attr_t thattr;
  pthread_attr_init(&thattr);

  pthread_t t1,t2,t3,t4,t5;
D
dapan1121 已提交
1040 1041 1042
  pthread_create(&(t1), &thattr, clientThread, mgmt);
  pthread_create(&(t2), &thattr, queryQueueThread, mgmt);
  pthread_create(&(t3), &thattr, fetchQueueThread, mgmt);
D
dapan1121 已提交
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054

  while (true) {
    if (qwtTestDeadLoop) {
      sleep(1);
    } else {
      sleep(qwtTestMTRunSec);
      break;
    }
  }
  
  qwtTestStop = true;
  sleep(3);
D
dapan1121 已提交
1055
  
D
dapan1121 已提交
1056
  qWorkerDestroy(&mgmt);
D
dapan 已提交
1057 1058 1059
}


D
dapan1121 已提交
1060

D
dapan1121 已提交
1061
int main(int argc, char** argv) {
D
dapan1121 已提交
1062
  srand(time(NULL));
D
dapan1121 已提交
1063 1064 1065 1066 1067 1068
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}