simSystem.c 5.0 KB
Newer Older
S
[TD-73]  
slguan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

16
#define _DEFAULT_SOURCE
S
slguan 已提交
17 18 19
#include "os.h"
#include "sim.h"
#include "taos.h"
S
slguan 已提交
20
#include "tglobal.h"
S
slguan 已提交
21 22
#include "ttimer.h"
#include "tutil.h"
S
slguan 已提交
23
#include "tsocket.h"
S
TD-2808  
Shengliang Guan 已提交
24
#include "taoserror.h"
S
Shengliang Guan 已提交
25
#undef TAOS_MEM_CHECK
S
slguan 已提交
26 27 28

SScript *simScriptList[MAX_MAIN_SCRIPT_NUM];
SCommand simCmdList[SIM_CMD_END];
29 30 31 32 33
int32_t  simScriptPos = -1;
int32_t  simScriptSucced = 0;
int32_t  simDebugFlag = 135;
void     simCloseTaosdConnect(SScript *script);
char     simHostName[128];
S
slguan 已提交
34

S
TD-2808  
Shengliang Guan 已提交
35 36
extern bool simExecSuccess;

H
Hui Li 已提交
37 38 39 40 41 42
char *simParseArbitratorName(char *varName) {
  static char hostName[140];
  sprintf(hostName, "%s:%d", simHostName, 8000);
  return hostName;
}

S
slguan 已提交
43 44 45
char *simParseHostName(char *varName) {
  static char hostName[140];

46 47
  int32_t index = atoi(varName + 8);
  int32_t port = 7100;
S
slguan 已提交
48 49 50 51 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
  switch (index) {
    case 1:
      port = 7100;
      break;
    case 2:
      port = 7200;
      break;
    case 3:
      port = 7300;
      break;
    case 4:
      port = 7400;
      break;
    case 5:
      port = 7500;
      break;
    case 6:
      port = 7600;
      break;
    case 7:
      port = 7700;
      break;
    case 8:
      port = 7800;
      break;
    case 9:
      port = 7900;
      break;
  }
77

S
slguan 已提交
78
  sprintf(hostName, "'%s:%d'", simHostName, port);
79
  // simInfo("hostName:%s", hostName);
S
slguan 已提交
80 81
  return hostName;
}
S
slguan 已提交
82 83

bool simSystemInit() {
D
fix bug  
dapan1121 已提交
84 85 86
  if (taos_init()) {
    return false;
  }
S
TD-1207  
Shengliang Guan 已提交
87
  taosGetFqdn(simHostName);
S
slguan 已提交
88 89 90 91 92 93 94 95 96
  simInitsimCmdList();
  memset(simScriptList, 0, sizeof(SScript *) * MAX_MAIN_SCRIPT_NUM);
  return true;
}

void simSystemCleanUp() {}

void simFreeScript(SScript *script) {
  if (script->type == SIM_SCRIPT_TYPE_MAIN) {
97 98 99
    simInfo("script:%s, background script num:%d, stop them", script->fileName, script->bgScriptLen);

    for (int32_t i = 0; i < script->bgScriptLen; ++i) {
S
slguan 已提交
100
      SScript *bgScript = script->bgScripts[i];
P
TD-2652  
Ping Xiao 已提交
101
      simDebug("script:%s, is background script, set stop flag", bgScript->fileName);
S
slguan 已提交
102
      bgScript->killed = true;
S
Shengliang Guan 已提交
103
      if (taosCheckPthreadValid(bgScript->bgPid)) {
104 105
        pthread_join(bgScript->bgPid, NULL);
      }
P
TD-2652  
Ping Xiao 已提交
106 107 108 109 110 111

      simDebug("script:%s, background thread joined", bgScript->fileName);
      taos_close(bgScript->taos);
      tfree(bgScript->lines);
      tfree(bgScript->optionBuffer);
      tfree(bgScript);
S
slguan 已提交
112 113
    }

P
TD-2652  
Ping Xiao 已提交
114 115 116 117 118 119
    simDebug("script:%s, is cleaned", script->fileName);
    taos_close(script->taos);
    tfree(script->lines);
    tfree(script->optionBuffer);
    tfree(script);
  }
S
slguan 已提交
120 121 122 123
}

SScript *simProcessCallOver(SScript *script) {
  if (script->type == SIM_SCRIPT_TYPE_MAIN) {
P
TD-2652  
Ping Xiao 已提交
124
    simDebug("script:%s, is main script, set stop flag", script->fileName);
S
slguan 已提交
125
    if (script->killed) {
S
TD-2808  
Shengliang Guan 已提交
126
      simExecSuccess = false;
127 128
      simInfo("script:" FAILED_PREFIX "%s" FAILED_POSTFIX ", " FAILED_PREFIX "failed" FAILED_POSTFIX ", error:%s",
              script->fileName, script->error);
129
      return NULL;
S
slguan 已提交
130
    } else {
S
TD-2808  
Shengliang Guan 已提交
131
      simExecSuccess = true;
132 133
      simInfo("script:" SUCCESS_PREFIX "%s" SUCCESS_POSTFIX ", " SUCCESS_PREFIX "success" SUCCESS_POSTFIX,
              script->fileName);
S
slguan 已提交
134 135 136
      simCloseTaosdConnect(script);
      simScriptSucced++;
      simScriptPos--;
137 138

      simFreeScript(script);
S
slguan 已提交
139
      if (simScriptPos == -1) {
S
Shengliang Guan 已提交
140 141
        simInfo("----------------------------------------------------------------------");
        simInfo("Simulation Test Done, " SUCCESS_PREFIX "%d" SUCCESS_POSTFIX " Passed:\n", simScriptSucced);
142
        return NULL;
S
slguan 已提交
143 144
      }

S
TD-1952  
Shengliang Guan 已提交
145
      return simScriptList[simScriptPos];
S
slguan 已提交
146 147
    }
  } else {
P
TD-2652  
Ping Xiao 已提交
148
    simDebug("script:%s,  is stopped", script->fileName);
S
slguan 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161
    simFreeScript(script);
    return NULL;
  }
}

void *simExecuteScript(void *inputScript) {
  SScript *script = (SScript *)inputScript;

  while (1) {
    if (script->type == SIM_SCRIPT_TYPE_MAIN) {
      script = simScriptList[simScriptPos];
    }

162 163 164 165
    if (abortExecution) {
      script->killed = true;
    }

S
slguan 已提交
166 167
    if (script->killed || script->linePos >= script->numOfLines) {
      script = simProcessCallOver(script);
168
      if (script == NULL) {
H
Haojun Liao 已提交
169
        simDebug("sim test abort now!");
170 171
        break;
      }
S
slguan 已提交
172 173
    } else {
      SCmdLine *line = &script->lines[script->linePos];
174
      char *    option = script->optionBuffer + line->optionOffset;
S
Shengliang Guan 已提交
175
      simDebug("script:%s, line:%d with option \"%s\"", script->fileName, line->lineNum, option);
S
slguan 已提交
176 177

      SCommand *cmd = &simCmdList[line->cmdno];
178
      int32_t   ret = (*(cmd->executeCmd))(script, option);
S
slguan 已提交
179 180 181 182 183 184
      if (!ret) {
        script->killed = true;
      }
    }
  }

P
TD-2652  
Ping Xiao 已提交
185
  simInfo("thread is stopped");
S
slguan 已提交
186 187
  return NULL;
}