|
|
|
@ -14,14 +14,14 @@
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define _DEFAULT_SOURCE
|
|
|
|
|
#include "../../../include/client/taos.h"
|
|
|
|
|
#include "taos.h"
|
|
|
|
|
#include "cJSON.h"
|
|
|
|
|
#include "os.h"
|
|
|
|
|
#include "sim.h"
|
|
|
|
|
#include "taoserror.h"
|
|
|
|
|
#include "tglobal.h"
|
|
|
|
|
#include "tutil.h"
|
|
|
|
|
#undef TAOS_MEM_CHECK
|
|
|
|
|
#include "ttypes.h"
|
|
|
|
|
|
|
|
|
|
void simLogSql(char *sql, bool useSharp) {
|
|
|
|
|
static FILE *fp = NULL;
|
|
|
|
@ -466,10 +466,6 @@ void simVisuallizeOption(SScript *script, char *src, char *dst) {
|
|
|
|
|
strcpy(dst + dstLen, src);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void simCloseRestFulConnect(SScript *script) {
|
|
|
|
|
memset(script->auth, 0, sizeof(script->auth));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void simCloseNativeConnect(SScript *script) {
|
|
|
|
|
if (script->taos == NULL) return;
|
|
|
|
|
|
|
|
|
@ -480,166 +476,7 @@ void simCloseNativeConnect(SScript *script) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void simCloseTaosdConnect(SScript *script) {
|
|
|
|
|
if (simAsyncQuery) {
|
|
|
|
|
simCloseRestFulConnect(script);
|
|
|
|
|
} else {
|
|
|
|
|
simCloseNativeConnect(script);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// {"status":"succ","code":0,"desc":"/KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04"}
|
|
|
|
|
// {"status":"succ","head":["affected_rows"],"data":[[1]],"rows":1}
|
|
|
|
|
// {"status":"succ","head":["ts","i"],"data":[["2017-12-25 21:28:41.022",1],["2017-12-25 21:28:42.022",2],["2017-12-25 21:28:43.022",3],["2017-12-25 21:28:44.022",4],["2017-12-25 21:28:45.022",5],["2017-12-25 21:28:46.022",6],["2017-12-25 21:28:47.022",7],["2017-12-25 21:28:48.022",8],["2017-12-25 21:28:49.022",9],["2017-12-25 21:28:50.022",10]],"rows":10}
|
|
|
|
|
int32_t simParseHttpCommandResult(SScript *script, char *command) {
|
|
|
|
|
cJSON* root = cJSON_Parse(command);
|
|
|
|
|
if (root == NULL) {
|
|
|
|
|
simError("script:%s, failed to parse json, response:%s", script->fileName, command);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cJSON *status = cJSON_GetObjectItem(root, "status");
|
|
|
|
|
if (status == NULL) {
|
|
|
|
|
simError("script:%s, failed to parse json, status is null, response:%s", script->fileName, command);
|
|
|
|
|
cJSON_Delete(root);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status->valuestring == NULL || strlen(status->valuestring) == 0) {
|
|
|
|
|
simError("script:%s, failed to parse json, status value is null, response:%s", script->fileName, command);
|
|
|
|
|
cJSON_Delete(root);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strcmp(status->valuestring, "succ") != 0) {
|
|
|
|
|
cJSON *code = cJSON_GetObjectItem(root, "code");
|
|
|
|
|
if (code == NULL) {
|
|
|
|
|
simError("script:%s, failed to parse json, code is null, response:%s", script->fileName, command);
|
|
|
|
|
cJSON_Delete(root);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
int32_t retcode = (int32_t)code->valueint;
|
|
|
|
|
if (retcode != 1017) {
|
|
|
|
|
simError("script:%s, json:status:%s not equal to succ, response:%s", script->fileName, status->valuestring,
|
|
|
|
|
command);
|
|
|
|
|
cJSON_Delete(root);
|
|
|
|
|
return retcode;
|
|
|
|
|
} else {
|
|
|
|
|
simDebug("script:%s, json:status:%s not equal to succ, but code is %d, response:%s", script->fileName,
|
|
|
|
|
status->valuestring, retcode, command);
|
|
|
|
|
cJSON_Delete(root);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cJSON *desc = cJSON_GetObjectItem(root, "desc");
|
|
|
|
|
if (desc != NULL) {
|
|
|
|
|
if (desc->valuestring == NULL || strlen(desc->valuestring) == 0) {
|
|
|
|
|
simError("script:%s, failed to parse json, desc value is null, response:%s", script->fileName, command);
|
|
|
|
|
cJSON_Delete(root);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
strcpy(script->auth, desc->valuestring);
|
|
|
|
|
cJSON_Delete(root);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cJSON *data = cJSON_GetObjectItem(root, "data");
|
|
|
|
|
if (data == NULL) {
|
|
|
|
|
simError("script:%s, failed to parse json, data is null, response:%s", script->fileName, command);
|
|
|
|
|
cJSON_Delete(root);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t rowsize = cJSON_GetArraySize(data);
|
|
|
|
|
if (rowsize < 0) {
|
|
|
|
|
simError("script:%s, failed to parse json:data, data size %d, response:%s", script->fileName, rowsize, command);
|
|
|
|
|
cJSON_Delete(root);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t rowIndex = 0;
|
|
|
|
|
sprintf(script->rows, "%d", rowsize);
|
|
|
|
|
for (int32_t r = 0; r < rowsize; ++r) {
|
|
|
|
|
cJSON *row = cJSON_GetArrayItem(data, r);
|
|
|
|
|
if (row == NULL) continue;
|
|
|
|
|
if (rowIndex++ >= 10) break;
|
|
|
|
|
|
|
|
|
|
int32_t colsize = cJSON_GetArraySize(row);
|
|
|
|
|
if (colsize < 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
colsize = MIN(10, colsize);
|
|
|
|
|
for (int32_t c = 0; c < colsize; ++c) {
|
|
|
|
|
cJSON *col = cJSON_GetArrayItem(row, c);
|
|
|
|
|
if (col->valuestring != NULL) {
|
|
|
|
|
strcpy(script->data[r][c], col->valuestring);
|
|
|
|
|
} else {
|
|
|
|
|
if (col->numberstring[0] == 0) {
|
|
|
|
|
strcpy(script->data[r][c], "null");
|
|
|
|
|
} else {
|
|
|
|
|
strcpy(script->data[r][c], col->numberstring);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t simExecuteRestFulCommand(SScript *script, char *command) {
|
|
|
|
|
char buf[5000] = {0};
|
|
|
|
|
sprintf(buf, "%s 2>/dev/null", command);
|
|
|
|
|
|
|
|
|
|
FILE *fp = popen(buf, "r");
|
|
|
|
|
if (fp == NULL) {
|
|
|
|
|
simError("failed to execute %s", buf);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t mallocSize = 2000;
|
|
|
|
|
int32_t alreadyReadSize = 0;
|
|
|
|
|
char * content = malloc(mallocSize);
|
|
|
|
|
|
|
|
|
|
while (!feof(fp)) {
|
|
|
|
|
int32_t availSize = mallocSize - alreadyReadSize;
|
|
|
|
|
int32_t len = (int32_t)fread(content + alreadyReadSize, 1, availSize, fp);
|
|
|
|
|
if (len >= availSize) {
|
|
|
|
|
alreadyReadSize += len;
|
|
|
|
|
mallocSize *= 2;
|
|
|
|
|
content = realloc(content, mallocSize);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pclose(fp);
|
|
|
|
|
|
|
|
|
|
return simParseHttpCommandResult(script, content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool simCreateRestFulConnect(SScript *script, char *user, char *pass) {
|
|
|
|
|
char command[4096];
|
|
|
|
|
sprintf(command, "curl 127.0.0.1:6041/rest/login/%s/%s", user, pass);
|
|
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
|
for (int32_t attempt = 0; attempt < 10; ++attempt) {
|
|
|
|
|
success = simExecuteRestFulCommand(script, command) == 0;
|
|
|
|
|
if (!success) {
|
|
|
|
|
simDebug("script:%s, user:%s connect taosd failed:%s, attempt:%d", script->fileName, user, taos_errstr(NULL),
|
|
|
|
|
attempt);
|
|
|
|
|
taosMsleep(1000);
|
|
|
|
|
} else {
|
|
|
|
|
simDebug("script:%s, user:%s connect taosd successed, attempt:%d", script->fileName, user, attempt);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
|
sprintf(script->error, "lineNum:%d. connect taosd failed:%s", script->lines[script->linePos].lineNum,
|
|
|
|
|
taos_errstr(NULL));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
simDebug("script:%s, connect taosd successed, auth:%p", script->fileName, script->auth);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool simCreateNativeConnect(SScript *script, char *user, char *pass) {
|
|
|
|
@ -651,7 +488,7 @@ bool simCreateNativeConnect(SScript *script, char *user, char *pass) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
taos = taos_connect(NULL, user, pass, NULL, tsDnodeShellPort);
|
|
|
|
|
taos = taos_connect(NULL, user, pass, NULL, 0);
|
|
|
|
|
if (taos == NULL) {
|
|
|
|
|
simDebug("script:%s, user:%s connect taosd failed:%s, attempt:%d", script->fileName, user, taos_errstr(NULL),
|
|
|
|
|
attempt);
|
|
|
|
@ -684,11 +521,7 @@ bool simCreateTaosdConnect(SScript *script, char *rest) {
|
|
|
|
|
user = token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (simAsyncQuery) {
|
|
|
|
|
return simCreateRestFulConnect(script, user, TSDB_DEFAULT_PASS);
|
|
|
|
|
} else {
|
|
|
|
|
return simCreateNativeConnect(script, user, TSDB_DEFAULT_PASS);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool simExecuteNativeSqlCommand(SScript *script, char *rest, bool isSlow) {
|
|
|
|
@ -710,7 +543,7 @@ bool simExecuteNativeSqlCommand(SScript *script, char *rest, bool isSlow) {
|
|
|
|
|
pSql = taos_query(script->taos, rest);
|
|
|
|
|
ret = taos_errno(pSql);
|
|
|
|
|
|
|
|
|
|
if (ret == TSDB_CODE_MND_TABLE_ALREADY_EXIST || ret == TSDB_CODE_MND_DB_ALREADY_EXIST) {
|
|
|
|
|
if (ret == TSDB_CODE_MND_STB_ALREADY_EXIST || ret == TSDB_CODE_MND_DB_ALREADY_EXIST) {
|
|
|
|
|
simDebug("script:%s, taos:%p, %s success, ret:%d:%s", script->fileName, script->taos, rest, ret & 0XFFFF,
|
|
|
|
|
tstrerror(ret));
|
|
|
|
|
ret = 0;
|
|
|
|
@ -877,41 +710,6 @@ bool simExecuteNativeSqlCommand(SScript *script, char *rest, bool isSlow) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool simExecuteRestFulSqlCommand(SScript *script, char *rest) {
|
|
|
|
|
SCmdLine *line = &script->lines[script->linePos];
|
|
|
|
|
char command[4096];
|
|
|
|
|
sprintf(command, "curl -H 'Authorization: Taosd %s' -d \"%s\" 127.0.0.1:6041/rest/sql", script->auth, rest);
|
|
|
|
|
|
|
|
|
|
int32_t ret = -1;
|
|
|
|
|
for (int32_t attempt = 0; attempt < 10; ++attempt) {
|
|
|
|
|
ret = simExecuteRestFulCommand(script, command);
|
|
|
|
|
if (ret == TSDB_CODE_MND_TABLE_ALREADY_EXIST || ret == TSDB_CODE_MND_DB_ALREADY_EXIST) {
|
|
|
|
|
simDebug("script:%s, taos:%p, %s success, ret:%d:%s", script->fileName, script->taos, rest, ret & 0XFFFF,
|
|
|
|
|
tstrerror(ret));
|
|
|
|
|
ret = 0;
|
|
|
|
|
break;
|
|
|
|
|
} else if (ret != 0) {
|
|
|
|
|
simDebug("script:%s, taos:%p, %s failed, ret:%d", script->fileName, script->taos, rest, ret);
|
|
|
|
|
|
|
|
|
|
if (line->errorJump == SQL_JUMP_TRUE) {
|
|
|
|
|
script->linePos = line->jump;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
taosMsleep(1000);
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
|
sprintf(script->error, "lineNum:%d. sql:%s failed, ret:%d", line->lineNum, rest, ret);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
script->linePos++;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool simExecuteSqlImpCmd(SScript *script, char *rest, bool isSlow) {
|
|
|
|
|
char buf[3000];
|
|
|
|
|
SCmdLine *line = &script->lines[script->linePos];
|
|
|
|
@ -935,7 +733,7 @@ bool simExecuteSqlImpCmd(SScript *script, char *rest, bool isSlow) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((!simAsyncQuery && script->taos == NULL) || (simAsyncQuery && script->auth[0] == 0)) {
|
|
|
|
|
if (script->taos == NULL) {
|
|
|
|
|
if (!simCreateTaosdConnect(script, "connect root")) {
|
|
|
|
|
if (line->errorJump == SQL_JUMP_TRUE) {
|
|
|
|
|
script->linePos = line->jump;
|
|
|
|
@ -951,11 +749,7 @@ bool simExecuteSqlImpCmd(SScript *script, char *rest, bool isSlow) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (simAsyncQuery) {
|
|
|
|
|
return simExecuteRestFulSqlCommand(script, rest);
|
|
|
|
|
} else {
|
|
|
|
|
return simExecuteNativeSqlCommand(script, rest, isSlow);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool simExecuteSqlCmd(SScript *script, char *rest) {
|
|
|
|
@ -1009,6 +803,7 @@ bool simExecuteRestfulCmd(SScript *script, char *rest) {
|
|
|
|
|
return simExecuteSystemCmd(script, cmd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool simExecuteSqlErrorCmd(SScript *script, char *rest) {
|
|
|
|
|
char buf[3000];
|
|
|
|
|
SCmdLine *line = &script->lines[script->linePos];
|
|
|
|
@ -1032,7 +827,7 @@ bool simExecuteSqlErrorCmd(SScript *script, char *rest) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((!simAsyncQuery && script->taos == NULL) || (simAsyncQuery && script->auth[0] == 0)) {
|
|
|
|
|
if (script->taos == NULL) {
|
|
|
|
|
if (!simCreateTaosdConnect(script, "connect root")) {
|
|
|
|
|
if (line->errorJump == SQL_JUMP_TRUE) {
|
|
|
|
|
script->linePos = line->jump;
|
|
|
|
@ -1048,17 +843,9 @@ bool simExecuteSqlErrorCmd(SScript *script, char *rest) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t ret;
|
|
|
|
|
TAOS_RES *pSql = NULL;
|
|
|
|
|
if (simAsyncQuery) {
|
|
|
|
|
char command[4096];
|
|
|
|
|
sprintf(command, "curl -H 'Authorization: Taosd %s' -d '%s' 127.0.0.1:6041/rest/sql", script->auth, rest);
|
|
|
|
|
ret = simExecuteRestFulCommand(script, command);
|
|
|
|
|
} else {
|
|
|
|
|
pSql = taos_query(script->taos, rest);
|
|
|
|
|
ret = taos_errno(pSql);
|
|
|
|
|
TAOS_RES *pSql = pSql = taos_query(script->taos, rest);
|
|
|
|
|
int32_t ret = taos_errno(pSql);
|
|
|
|
|
taos_free_result(pSql);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ret != TSDB_CODE_SUCCESS) {
|
|
|
|
|
simDebug("script:%s, taos:%p, %s execute, expect failed, so success, ret:%d:%s", script->fileName, script->taos,
|
|
|
|
@ -1084,7 +871,11 @@ bool simExecuteLineInsertCmd(SScript *script, char *rest) {
|
|
|
|
|
simInfo("script:%s, %s", script->fileName, rest);
|
|
|
|
|
simLogSql(buf, true);
|
|
|
|
|
char * lines[] = {rest};
|
|
|
|
|
#if 0
|
|
|
|
|
int32_t ret = taos_insert_lines(script->taos, lines, 1);
|
|
|
|
|
#else
|
|
|
|
|
int32_t ret = 0;
|
|
|
|
|
#endif
|
|
|
|
|
if (ret == TSDB_CODE_SUCCESS) {
|
|
|
|
|
simDebug("script:%s, taos:%p, %s executed. success.", script->fileName, script->taos, rest);
|
|
|
|
|
script->linePos++;
|
|
|
|
@ -1107,7 +898,11 @@ bool simExecuteLineInsertErrorCmd(SScript *script, char *rest) {
|
|
|
|
|
simInfo("script:%s, %s", script->fileName, rest);
|
|
|
|
|
simLogSql(buf, true);
|
|
|
|
|
char * lines[] = {rest};
|
|
|
|
|
#if 0
|
|
|
|
|
int32_t ret = taos_insert_lines(script->taos, lines, 1);
|
|
|
|
|
#else
|
|
|
|
|
int32_t ret = 0;
|
|
|
|
|
#endif
|
|
|
|
|
if (ret == TSDB_CODE_SUCCESS) {
|
|
|
|
|
sprintf(script->error, "script:%s, taos:%p, %s executed. expect failed, but success.", script->fileName, script->taos, rest);
|
|
|
|
|
script->linePos++;
|
|
|
|
|