schedulerTests.cpp 11.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 25 26 27 28
/*
 * 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"
D
dapan1121 已提交
29 30
#include "catalog.h"
#include "scheduler.h"
D
dapan1121 已提交
31 32
#include "tep.h"
#include "trpc.h"
D
dapan 已提交
33 34 35
#include "schedulerInt.h"
#include "stub.h"
#include "addr_any.h"
D
dapan1121 已提交
36

D
dapan1121 已提交
37

D
dapan1121 已提交
38
namespace {
D
dapan 已提交
39

D
dapan1121 已提交
40
extern "C" int32_t schHandleResponseMsg(SSchJob *job, SSchTask *task, int32_t msgType, char *msg, int32_t msgSize, int32_t rspCode);
D
dapan 已提交
41

D
dapan1121 已提交
42

D
dapan1121 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
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 已提交
59
void schtBuildQueryDag(SQueryDag *dag) {
D
dapan 已提交
60
  uint64_t qId = 0x0000000000000001;
D
dapan1121 已提交
61 62 63 64
  
  dag->queryId = qId;
  dag->numOfSubplans = 2;
  dag->pSubplans = taosArrayInit(dag->numOfSubplans, POINTER_BYTES);
D
dapan1121 已提交
65 66
  SArray *scan = taosArrayInit(1, POINTER_BYTES);
  SArray *merge = taosArrayInit(1, POINTER_BYTES);
D
dapan1121 已提交
67
  
D
dapan1121 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  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;
  scanPlan->execNode.numOfEps = 1;
  scanPlan->execNode.nodeId = 1;
  scanPlan->execNode.inUse = 0;
  scanPlan->execNode.epAddr[0].port = 6030;
  strcpy(scanPlan->execNode.epAddr[0].fqdn, "ep0");
  scanPlan->pChildren = NULL;
  scanPlan->level = 1;
  scanPlan->pParents = taosArrayInit(1, POINTER_BYTES);
  scanPlan->pNode = (SPhyNode*)calloc(1, sizeof(SPhyNode));
D
dapan1121 已提交
84
  scanPlan->msgType = TDMT_VND_QUERY;
D
dapan1121 已提交
85 86 87 88 89 90 91 92 93 94

  mergePlan->id.queryId = qId;
  mergePlan->id.templateId = 0x4444444444;
  mergePlan->id.subplanId = 0x5555555555;
  mergePlan->type = QUERY_TYPE_MERGE;
  mergePlan->level = 0;
  mergePlan->execNode.numOfEps = 0;
  mergePlan->pChildren = taosArrayInit(1, POINTER_BYTES);
  mergePlan->pParents = NULL;
  mergePlan->pNode = (SPhyNode*)calloc(1, sizeof(SPhyNode));
D
dapan1121 已提交
95
  mergePlan->msgType = TDMT_VND_QUERY;
D
dapan1121 已提交
96 97 98 99

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

D
dapan1121 已提交
100 101
  taosArrayPush(mergePlan->pChildren, &scanPlan);
  taosArrayPush(scanPlan->pParents, &mergePlan);
D
dapan1121 已提交
102 103 104 105 106

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

D
dapan1121 已提交
107 108 109 110 111
void schtFreeQueryDag(SQueryDag *dag) {

}


D
dapan 已提交
112 113 114 115 116 117
void schtBuildInsertDag(SQueryDag *dag) {
  uint64_t qId = 0x0000000000000002;
  
  dag->queryId = qId;
  dag->numOfSubplans = 2;
  dag->pSubplans = taosArrayInit(1, POINTER_BYTES);
D
dapan1121 已提交
118
  SArray *inserta = taosArrayInit(dag->numOfSubplans, POINTER_BYTES);
D
dapan 已提交
119
  
D
dapan1121 已提交
120
  SSubplan *insertPlan = (SSubplan *)calloc(2, sizeof(SSubplan));
D
dapan 已提交
121 122 123 124 125 126

  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;
D
dapan1121 已提交
127 128 129 130 131
  insertPlan[0].execNode.numOfEps = 1;
  insertPlan[0].execNode.nodeId = 1;
  insertPlan[0].execNode.inUse = 0;
  insertPlan[0].execNode.epAddr[0].port = 6030;
  strcpy(insertPlan[0].execNode.epAddr[0].fqdn, "ep0");
H
Haojun Liao 已提交
132
  insertPlan[0].pChildren = NULL;
D
dapan 已提交
133 134 135
  insertPlan[0].pParents = NULL;
  insertPlan[0].pNode = NULL;
  insertPlan[0].pDataSink = (SDataSink*)calloc(1, sizeof(SDataSink));
D
dapan1121 已提交
136
  insertPlan[0].msgType = TDMT_VND_SUBMIT;
D
dapan 已提交
137 138 139 140 141 142

  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;
D
dapan1121 已提交
143 144 145 146 147
  insertPlan[1].execNode.numOfEps = 1;
  insertPlan[1].execNode.nodeId = 1;
  insertPlan[1].execNode.inUse = 1;
  insertPlan[1].execNode.epAddr[0].port = 6030;
  strcpy(insertPlan[1].execNode.epAddr[0].fqdn, "ep1");
H
Haojun Liao 已提交
148
  insertPlan[1].pChildren = NULL;
D
dapan 已提交
149 150 151
  insertPlan[1].pParents = NULL;
  insertPlan[1].pNode = NULL;
  insertPlan[1].pDataSink = (SDataSink*)calloc(1, sizeof(SDataSink));
D
dapan1121 已提交
152
  insertPlan[1].msgType = TDMT_VND_SUBMIT;
D
dapan 已提交
153

D
dapan1121 已提交
154 155 156
  taosArrayPush(inserta, &insertPlan);
  insertPlan += 1;
  taosArrayPush(inserta, &insertPlan);
D
dapan 已提交
157 158 159 160 161

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


D
dapan 已提交
162 163 164 165 166 167
int32_t schtPlanToString(const SSubplan *subplan, char** str, int32_t* len) {
  *str = (char *)calloc(1, 20);
  *len = 20;
  return 0;
}

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

D
dapan 已提交
170 171
}

D
dapan1121 已提交
172 173 174 175 176
void schtRpcSendRequest(void *shandle, const SEpSet *pEpSet, SRpcMsg *pMsg, int64_t *pRid) {

}


D
dapan 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

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 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216
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 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
int32_t schtAsyncSendMsgToServer(void *pTransporter, SEpSet* epSet, int64_t* pTransporterId, const SMsgSendInfo* pInfo) {
  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 已提交
235

D
dapan 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
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 已提交
253
    SShellSubmitRsp rsp = {0};
D
dapan 已提交
254
    rsp.affectedRows = 10;
D
dapan1121 已提交
255
    schHandleResponseMsg(job, task, TDMT_VND_SUBMIT_RSP, (char *)&rsp, sizeof(rsp), 0);
D
dapan 已提交
256 257 258 259 260 261 262
    
    pIter = taosHashIterate(job->execTasks, pIter);
  }    

  return NULL;
}

D
dapan1121 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
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;
  code = schHandleResponseMsg(job, job->fetchTask, TDMT_VND_FETCH_RSP, (char *)rsp, sizeof(rsp), 0);
    
  assert(code == 0);
}




D
dapan1121 已提交
280
struct SSchJob *pInsertJob = NULL;
D
dapan1121 已提交
281 282
}

D
dapan 已提交
283
TEST(queryTest, normalCase) {
D
dapan1121 已提交
284
  void *mockPointer = (void *)0x1;
D
dapan1121 已提交
285 286 287
  char *clusterId = "cluster1";
  char *dbname = "1.db1";
  char *tablename = "table1";
D
dapan1121 已提交
288
  SVgroupInfo vgInfo = {0};
D
dapan1121 已提交
289
  SSchJob *pJob = NULL;
D
dapan1121 已提交
290
  SQueryDag dag = {0};
D
dapan1121 已提交
291 292 293

  schtInitLogFile();

D
dapan1121 已提交
294
  SArray *qnodeList = taosArrayInit(1, sizeof(SEpAddr));
D
dapan 已提交
295 296 297 298 299

  SEpAddr qnodeAddr = {0};
  strcpy(qnodeAddr.fqdn, "qnode0.ep");
  qnodeAddr.port = 6031;
  taosArrayPush(qnodeList, &qnodeAddr);
D
dapan1121 已提交
300 301
  
  int32_t code = schedulerInit(NULL);
D
dapan1121 已提交
302
  ASSERT_EQ(code, 0);
D
dapan1121 已提交
303

D
dapan 已提交
304
  schtBuildQueryDag(&dag);
D
dapan 已提交
305 306 307

  schtSetPlanToString();
  schtSetExecNode();
D
dapan1121 已提交
308
  schtSetAsyncSendMsgToServer();
D
dapan1121 已提交
309 310
  
  code = scheduleAsyncExecJob(mockPointer, qnodeList, &dag, &pJob);
D
dapan1121 已提交
311
  ASSERT_EQ(code, 0);
D
dapan 已提交
312

D
dapan 已提交
313
  SSchJob *job = (SSchJob *)pJob;
D
dapan 已提交
314 315
  void *pIter = taosHashIterate(job->execTasks, NULL);
  while (pIter) {
D
dapan 已提交
316
    SSchTask *task = *(SSchTask **)pIter;
D
dapan 已提交
317 318

    SQueryTableRsp rsp = {0};
D
dapan1121 已提交
319
    code = schHandleResponseMsg(job, task, TDMT_VND_QUERY_RSP, (char *)&rsp, sizeof(rsp), 0);
D
dapan 已提交
320 321 322 323 324 325 326
    
    ASSERT_EQ(code, 0);
    pIter = taosHashIterate(job->execTasks, pIter);
  }    

  pIter = taosHashIterate(job->execTasks, NULL);
  while (pIter) {
D
dapan 已提交
327
    SSchTask *task = *(SSchTask **)pIter;
D
dapan 已提交
328 329

    SResReadyRsp rsp = {0};
D
dapan1121 已提交
330 331
    code = schHandleResponseMsg(job, task, TDMT_VND_RES_READY_RSP, (char *)&rsp, sizeof(rsp), 0);
    printf("code:%d", code);
D
dapan 已提交
332 333 334 335 336 337
    ASSERT_EQ(code, 0);
    pIter = taosHashIterate(job->execTasks, pIter);
  }  

  pIter = taosHashIterate(job->execTasks, NULL);
  while (pIter) {
D
dapan 已提交
338
    SSchTask *task = *(SSchTask **)pIter;
D
dapan 已提交
339 340

    SQueryTableRsp rsp = {0};
D
dapan1121 已提交
341
    code = schHandleResponseMsg(job, task, TDMT_VND_QUERY_RSP, (char *)&rsp, sizeof(rsp), 0);
D
dapan 已提交
342 343 344 345 346 347 348
    
    ASSERT_EQ(code, 0);
    pIter = taosHashIterate(job->execTasks, pIter);
  }    

  pIter = taosHashIterate(job->execTasks, NULL);
  while (pIter) {
D
dapan 已提交
349
    SSchTask *task = *(SSchTask **)pIter;
D
dapan 已提交
350 351

    SResReadyRsp rsp = {0};
D
dapan1121 已提交
352
    code = schHandleResponseMsg(job, task, TDMT_VND_RES_READY_RSP, (char *)&rsp, sizeof(rsp), 0);
D
dapan 已提交
353 354 355 356 357
    ASSERT_EQ(code, 0);
    
    pIter = taosHashIterate(job->execTasks, pIter);
  }  

D
dapan1121 已提交
358 359
  pthread_attr_t thattr;
  pthread_attr_init(&thattr);
D
dapan 已提交
360

D
dapan1121 已提交
361 362
  pthread_t thread1;
  pthread_create(&(thread1), &thattr, schtCreateFetchRspThread, job);
D
dapan 已提交
363

D
dapan1121 已提交
364
  void *data = NULL;  
D
dapan 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377
  code = scheduleFetchRows(job, &data);
  ASSERT_EQ(code, 0);

  SRetrieveTableRsp *pRsp = (SRetrieveTableRsp *)data;
  ASSERT_EQ(pRsp->completed, 1);
  ASSERT_EQ(pRsp->numOfRows, 10);

  data = NULL;
  code = scheduleFetchRows(job, &data);
  ASSERT_EQ(code, 0);
  ASSERT_EQ(data, (void*)NULL);

  scheduleFreeJob(pJob);
D
dapan1121 已提交
378 379

  schtFreeQueryDag(&dag);
D
dapan1121 已提交
380 381

  schedulerDestroy();
D
dapan1121 已提交
382
}
D
dapan1121 已提交
383 384


D
dapan 已提交
385 386 387 388 389 390 391 392 393 394


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 已提交
395 396

  schtInitLogFile();
H
Haojun Liao 已提交
397

D
dapan 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410
  SArray *qnodeList = taosArrayInit(1, sizeof(SEpAddr));

  SEpAddr qnodeAddr = {0};
  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 已提交
411
  schtSetAsyncSendMsgToServer();
D
dapan 已提交
412 413 414 415 416 417

  pthread_attr_t thattr;
  pthread_attr_init(&thattr);

  pthread_t thread1;
  pthread_create(&(thread1), &thattr, schtSendRsp, &pInsertJob);
D
dapan1121 已提交
418 419 420

  SQueryResult res = {0};
  code = scheduleExecJob(mockPointer, qnodeList, &dag, &pInsertJob, &res);
D
dapan 已提交
421
  ASSERT_EQ(code, 0);
D
dapan1121 已提交
422
  ASSERT_EQ(res.numOfRows, 20);
D
dapan 已提交
423 424

  scheduleFreeJob(pInsertJob);
D
dapan1121 已提交
425 426

  schedulerDestroy();  
D
dapan 已提交
427 428
}

D
dapan1121 已提交
429
TEST(multiThread, forceFree) {
D
dapan 已提交
430

D
dapan1121 已提交
431 432 433
  schtInitLogFile();

}
D
dapan 已提交
434 435


D
dapan1121 已提交
436 437 438 439 440 441 442
int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}



D
dapan1121 已提交
443