diff --git a/CMakeLists.txt b/CMakeLists.txt index e16c93c1f1394969c945aba55188c262dfb4b961..ccad9e7a2db089749a4dd833c13a8e9671e8b626 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,14 +42,16 @@ execute_process(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" . execute_process(COMMAND "${CMAKE_COMMAND}" --build . WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/deps/deps-download") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -gdwarf-2 -msse4.2 -mfma") + # deps add_subdirectory(deps) # api -aux_source_directory(include/client API_SRC) -add_library(api INTERFACE ${API_SRC}) +add_library(api INTERFACE) +target_include_directories(api INTERFACE "include/client") # src add_subdirectory(source) -# tests (TODO) \ No newline at end of file +# tests (TODO) diff --git a/deps/CMakeLists.txt b/deps/CMakeLists.txt index 137ad31d8225c524687d574cf13cd944a1afad54..410e0c564eedc5ce7051773fd61fb2030517fd3a 100644 --- a/deps/CMakeLists.txt +++ b/deps/CMakeLists.txt @@ -8,7 +8,13 @@ target_include_directories( # see https://stackoverflow.com/questions/25676277/cmake-target-include-directories-prints-an-error-when-i-try-to-add-the-source PUBLIC $ ) + add_subdirectory(lz4/build/cmake) +target_include_directories( + lz4_static + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/lz4/lib +) + add_subdirectory(zlib) target_include_directories( zlib diff --git a/include/client/consumer/consumer.h b/include/client/consumer/consumer.h index 742e2c12fe08175f8dc0d05cce9b83976e01673a..8d1c9835e643d6f2fdddf32e5aef23ef1f0fcb44 100644 --- a/include/client/consumer/consumer.h +++ b/include/client/consumer/consumer.h @@ -16,6 +16,10 @@ #ifndef _TD_CONSUMER_H_ #define _TD_CONSUMER_H_ +#include "tlist.h" +#include "tarray.h" +#include "hash.h" + #ifdef __cplusplus extern "C" { #endif @@ -32,16 +36,15 @@ extern "C" { struct tmq_resp_err_t; typedef struct tmq_resp_err_t tmq_resp_err_t; - //topic list - //resouces are supposed to be free by users by calling tmq_list_destroy - struct tmq_topic_list_t; - typedef struct tmq_topic_list_t tmq_topic_list_t; - int32_t tmq_topic_list_add(tmq_topic_list_t*, const char*); - void tmq_topic_list_destroy(tmq_topic_list_t*); + struct tmq_message_t; + typedef struct tmq_message_t tmq_message_t; + + struct tmq_col_batch_t; + typedef struct tmq_col_batch_t tmq_col_batch_t; //get content of message - tmq_col_batch_t *tmq_get_msg_col_by_idx(tmq_message_t*, int32_t); - tmq_col_batch_t *tmq_get_msg_col_by_name(tmq_message_t*, const char*); + tmq_col_batch_t* tmq_get_msg_col_by_idx(tmq_message_t*, int32_t col_id); + tmq_col_batch_t* tmq_get_msg_col_by_name(tmq_message_t*, const char*); //consumer config int32_t tmq_conf_set(tmq_consumer_config_t* , const char* config_key, const char* config_value, char* errstr, int32_t errstr_cap); @@ -51,11 +54,12 @@ extern "C" { tmq_consumer_t* tmq_consumer_new(tmq_consumer_config_t* , char* errstr, int32_t errstr_cap); //subscribe - tmq_resp_err_t tmq_subscribe(tmq_consumer_t*, const tmq_topic_list_t*); + tmq_resp_err_t tmq_subscribe(tmq_consumer_t*, const SList*); + tmq_resp_err_t tmq_unsubscribe(tmq_consumer_t*); //consume //resouces are supposed to be free by users by calling tmq_message_destroy - tmq_message_t tmq_consume_poll(tmq_consumer_t*, int64_t blocking_time); + tmq_message_t* tmq_consume_poll(tmq_consumer_t*, int64_t blocking_time); //destroy message and free memory void tmq_message_destroy(tmq_message_t*); diff --git a/include/client/taos.h b/include/client/taos.h index edb1552b811a2ff4b8c78c19523cc6f2ad82ba74..6fa30737e71e8f40cee817386ad4d2c26661777f 100644 --- a/include/client/taos.h +++ b/include/client/taos.h @@ -172,10 +172,6 @@ DLL_EXPORT int taos_load_table_info(TAOS *taos, const char* tableNameList); DLL_EXPORT int taos_insert_lines(TAOS* taos, char* lines[], int numLines); -DLL_EXPORT int taos_insert_telnet_lines(TAOS* taos, char* lines[], int numLines); - -DLL_EXPORT int taos_insert_json_payload(TAOS* taos, char* payload); - #ifdef __cplusplus } #endif diff --git a/include/common/common.h b/include/common/common.h new file mode 100644 index 0000000000000000000000000000000000000000..b438316f57b381153cf37add6a2bbe93ab9a0ea4 --- /dev/null +++ b/include/common/common.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef TDENGINE_COMMON_H +#define TDENGINE_COMMON_H + +#include "taosdef.h" + +//typedef struct STimeWindow { +// TSKEY skey; +// TSKEY ekey; +//} STimeWindow; + +//typedef struct { +// int32_t dataLen; +// char name[TSDB_TABLE_FNAME_LEN]; +// char *data; +//} STagData; + +//typedef struct SSchema { +// uint8_t type; +// char name[TSDB_COL_NAME_LEN]; +// int16_t colId; +// int16_t bytes; +//} SSchema; + +#endif // TDENGINE_COMMON_H diff --git a/include/common/schema.h b/include/common/schema.h index 2b19eca76c972dbff713444beeb15b9e763b30af..ccc91f09ffdeab8d8fd5f35aa3b915635fd9dd72 100644 --- a/include/common/schema.h +++ b/include/common/schema.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef _TD_SCHEMA_H_ -#define _TD_SCHEMA_H_ +#ifndef _TD_COMMON_SCHEMA_H_ +#define _TD_COMMON_SCHEMA_H_ #include "os.h" @@ -100,4 +100,4 @@ STSchema *tdGetSchemaFromBuilder(STSchemaBuilder *pBuilder); } #endif -#endif /*_TD_SCHEMA_H_*/ \ No newline at end of file +#endif /*_TD_COMMON_SCHEMA_H_*/ \ No newline at end of file diff --git a/include/common/taosMsg.h b/include/common/taosMsg.h deleted file mode 100644 index 0d083a4ca50f8d68d87f4fb6eb66286eee726e97..0000000000000000000000000000000000000000 --- a/include/common/taosMsg.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef _TD_TAOS_MSG_H_ -#define _TD_TAOS_MSG_H_ - -typedef struct { - /* data */ -} SSubmitReq; - -typedef struct { - /* data */ -} SSubmitRsp; - -typedef struct { - /* data */ -} SSubmitReqReader; - -typedef struct { - /* data */ -} SCreateTableReq; - -typedef struct { - /* data */ -} SCreateTableRsp; - -typedef struct { - /* data */ -} SDropTableReq; - -typedef struct { - /* data */ -} SDropTableRsp; - -typedef struct { - /* data */ -} SAlterTableReq; - -typedef struct { - /* data */ -} SAlterTableRsp; - -#endif /*_TD_TAOS_MSG_H_*/ \ No newline at end of file diff --git a/include/common/taosdef.h b/include/common/taosdef.h new file mode 100644 index 0000000000000000000000000000000000000000..9b2bb897931c46900fec781660081eb12a6f37c2 --- /dev/null +++ b/include/common/taosdef.h @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef _TD_COMMON_TAOS_DEF_H_ +#define _TD_COMMON_TAOS_DEF_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "tdef.h" +#include "taos.h" + +#define TSWINDOW_INITIALIZER ((STimeWindow) {INT64_MIN, INT64_MAX}) +#define TSWINDOW_DESC_INITIALIZER ((STimeWindow) {INT64_MAX, INT64_MIN}) +#define IS_TSWINDOW_SPECIFIED(win) (((win).skey != INT64_MIN) || ((win).ekey != INT64_MAX)) + +typedef enum { + TAOS_QTYPE_RPC = 1, + TAOS_QTYPE_FWD = 2, + TAOS_QTYPE_WAL = 3, + TAOS_QTYPE_CQ = 4, + TAOS_QTYPE_QUERY = 5 +} EQType; + +typedef enum { + TSDB_SUPER_TABLE = 1, // super table + TSDB_CHILD_TABLE = 2, // table created from super table + TSDB_NORMAL_TABLE = 3, // ordinary table + TSDB_STREAM_TABLE = 4, // table created from stream computing + TSDB_TEMP_TABLE = 5, // temp table created by nest query + TSDB_TABLE_MAX = 6 +} ETableType; + +typedef enum { + TSDB_MOD_MNODE = 1, + TSDB_MOD_HTTP = 2, + TSDB_MOD_MONITOR = 3, + TSDB_MOD_MQTT = 4, + TSDB_MOD_MAX = 5 +} EModuleType; + +typedef enum { + TSDB_CHECK_ITEM_NETWORK, + TSDB_CHECK_ITEM_MEM, + TSDB_CHECK_ITEM_CPU, + TSDB_CHECK_ITEM_DISK, + TSDB_CHECK_ITEM_OS, + TSDB_CHECK_ITEM_ACCESS, + TSDB_CHECK_ITEM_VERSION, + TSDB_CHECK_ITEM_DATAFILE, + TSDB_CHECK_ITEM_MAX +} ECheckItemType; + +typedef enum { + TD_ROW_DISCARD_UPDATE = 0, + TD_ROW_OVERWRITE_UPDATE = 1, + TD_ROW_PARTIAL_UPDATE = 2 +} TDUpdateConfig; + +extern char *qtypeStr[]; + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_COMMON_TAOS_DEF_H_*/ diff --git a/src/inc/taosmsg.h b/include/common/taosmsg.h similarity index 95% rename from src/inc/taosmsg.h rename to include/common/taosmsg.h index bb93c52142a73d00ebde9e039143a5e124eb6e9a..72ce3a946cae3492012b15fc9c07b50e3382fcf8 100644 --- a/src/inc/taosmsg.h +++ b/include/common/taosmsg.h @@ -13,19 +13,15 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TAOSMSG_H -#define TDENGINE_TAOSMSG_H +#ifndef _TD_COMMON_TAOS_MSG_H_ +#define _TD_COMMON_TAOS_MSG_H_ #ifdef __cplusplus extern "C" { #endif -#include -#include - #include "taosdef.h" #include "taoserror.h" -#include "trpc.h" #include "tdataformat.h" // message type @@ -256,12 +252,14 @@ typedef struct { SShellSubmitRspBlock failedBlocks[]; } SShellSubmitRspMsg; +//#if 0 typedef struct SSchema { uint8_t type; char name[TSDB_COL_NAME_LEN]; int16_t colId; int16_t bytes; } SSchema; +//#endif typedef struct { int32_t contLen; @@ -339,6 +337,13 @@ typedef struct { int32_t pid; } SConnectMsg; +typedef struct SEpSet { + int8_t inUse; + int8_t numOfEps; + uint16_t port[TSDB_MAX_REPLICA]; + char fqdn[TSDB_MAX_REPLICA][TSDB_FQDN_LEN]; +} SEpSet; + typedef struct { char acctId[TSDB_ACCT_ID_LEN]; char serverVersion[TSDB_VERSION_LEN]; @@ -348,7 +353,7 @@ typedef struct { int8_t reserved1; int8_t reserved2; int32_t connId; - SRpcEpSet epSet; + SEpSet epSet; } SConnectRsp; typedef struct { @@ -465,6 +470,21 @@ typedef struct { int32_t tsOrder; // ts comp block order } STsBufInfo; +typedef struct SInterval { + int32_t tz; // query client timezone + char intervalUnit; + char slidingUnit; + char offsetUnit; + int64_t interval; + int64_t sliding; + int64_t offset; +} SInterval; + +typedef struct SSessionWindow { + int64_t gap; // gap between two session window(in microseconds) + int32_t primaryColId; // primary timestamp column +} SSessionWindow; + typedef struct { SMsgHead head; char version[TSDB_VERSION_LEN]; @@ -492,7 +512,6 @@ typedef struct { SSessionWindow sw; // session window uint16_t tagCondLen; // tag length in current query uint16_t colCondLen; // column length in current query - uint32_t tbnameCondLen; // table name filter condition string length int16_t numOfGroupCols; // num of group by columns int16_t orderByIdx; int16_t orderType; // used in group by xx order by xxx @@ -502,7 +521,6 @@ typedef struct { int64_t offset; uint32_t queryType; // denote another query process int16_t numOfOutput; // final output columns numbers - int16_t tagNameRelType; // relation of tag criteria and tbname criteria int16_t fillType; // interpolate type uint64_t fillVal; // default value array list int32_t secondStageOutput; @@ -631,7 +649,7 @@ typedef struct { char reserved[64]; } SVnodeStatisticInfo; -typedef struct { +typedef struct SVgroupAccess { int32_t vgId; int8_t accessState; } SVgroupAccess; @@ -660,7 +678,7 @@ typedef struct { char mnodeEp[TSDB_EP_LEN]; } SMInfo; -typedef struct { +typedef struct SMInfos { int8_t inUse; int8_t mnodeNum; SMInfo mnodeInfos[TSDB_MAX_REPLICA]; @@ -686,7 +704,7 @@ typedef struct { int8_t reserved[4]; } SClusterCfg; -typedef struct { +typedef struct SStatusMsg { uint32_t version; int32_t dnodeId; char dnodeEp[TSDB_EP_LEN]; @@ -904,7 +922,7 @@ typedef struct { uint32_t onlineDnodes; uint32_t connId; int8_t killConnection; - SRpcEpSet epSet; + SEpSet epSet; } SHeartBeatRsp; typedef struct { @@ -936,10 +954,46 @@ typedef struct { char reserved2[64]; } SStartupStep; +typedef struct { + /* data */ +} SSubmitReq; + +typedef struct { + /* data */ +} SSubmitRsp; + +typedef struct { + /* data */ +} SSubmitReqReader; + +typedef struct { + /* data */ +} SCreateTableReq; + +typedef struct { + /* data */ +} SCreateTableRsp; + +typedef struct { + /* data */ +} SDropTableReq; + +typedef struct { + /* data */ +} SDropTableRsp; + +typedef struct { + /* data */ +} SAlterTableReq; + +typedef struct { + /* data */ +} SAlterTableRsp; + #pragma pack(pop) #ifdef __cplusplus } #endif -#endif +#endif /*_TD_COMMON_TAOS_MSG_H_*/ diff --git a/src/common/inc/tdataformat.h b/include/common/tdataformat.h similarity index 90% rename from src/common/inc/tdataformat.h rename to include/common/tdataformat.h index cf2b90fd9e6ff537d3f2fd2b826db8ead4c5be0c..c4321eb9dfc474ed96f909c7deb81c62c514349d 100644 --- a/src/common/inc/tdataformat.h +++ b/include/common/tdataformat.h @@ -12,12 +12,12 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef _TD_DATA_FORMAT_H_ -#define _TD_DATA_FORMAT_H_ +#ifndef _TD_COMMON_DATA_FORMAT_H_ +#define _TD_COMMON_DATA_FORMAT_H_ #include "os.h" #include "talgo.h" -#include "ttype.h" +#include "ttypes.h" #include "tutil.h" #ifdef __cplusplus @@ -51,6 +51,78 @@ extern "C" { memcpy(varDataVal(x), (str), (_size)); \ } while (0); +// ----------------- TSDB COLUMN DEFINITION +typedef struct { + int8_t type; // Column type + int16_t colId; // column ID + int16_t bytes; // column bytes (restore to int16_t in case of misuse) + uint16_t offset; // point offset in SDataRow after the header part. +} STColumn; + +#define colType(col) ((col)->type) +#define colColId(col) ((col)->colId) +#define colBytes(col) ((col)->bytes) +#define colOffset(col) ((col)->offset) + +#define colSetType(col, t) (colType(col) = (t)) +#define colSetColId(col, id) (colColId(col) = (id)) +#define colSetBytes(col, b) (colBytes(col) = (b)) +#define colSetOffset(col, o) (colOffset(col) = (o)) + +// ----------------- TSDB SCHEMA DEFINITION +typedef struct { + int version; // version + int numOfCols; // Number of columns appended + int tlen; // maximum length of a SDataRow without the header part (sizeof(VarDataOffsetT) + sizeof(VarDataLenT) + (bytes)) + uint16_t flen; // First part length in a SDataRow after the header part + uint16_t vlen; // pure value part length, excluded the overhead (bytes only) + STColumn columns[]; +} STSchema; + +#define schemaNCols(s) ((s)->numOfCols) +#define schemaVersion(s) ((s)->version) +#define schemaTLen(s) ((s)->tlen) +#define schemaFLen(s) ((s)->flen) +#define schemaVLen(s) ((s)->vlen) +#define schemaColAt(s, i) ((s)->columns + i) +#define tdFreeSchema(s) tfree((s)) + +STSchema *tdDupSchema(STSchema *pSchema); +int tdEncodeSchema(void **buf, STSchema *pSchema); +void * tdDecodeSchema(void *buf, STSchema **pRSchema); + +static FORCE_INLINE int comparColId(const void *key1, const void *key2) { + if (*(int16_t *)key1 > ((STColumn *)key2)->colId) { + return 1; + } else if (*(int16_t *)key1 < ((STColumn *)key2)->colId) { + return -1; + } else { + return 0; + } +} + +static FORCE_INLINE STColumn *tdGetColOfID(STSchema *pSchema, int16_t colId) { + void *ptr = bsearch(&colId, (void *)pSchema->columns, schemaNCols(pSchema), sizeof(STColumn), comparColId); + if (ptr == NULL) return NULL; + return (STColumn *)ptr; +} + +// ----------------- SCHEMA BUILDER DEFINITION +typedef struct { + int tCols; + int nCols; + int tlen; + uint16_t flen; + uint16_t vlen; + int version; + STColumn *columns; +} STSchemaBuilder; + +int tdInitTSchemaBuilder(STSchemaBuilder *pBuilder, int32_t version); +void tdDestroyTSchemaBuilder(STSchemaBuilder *pBuilder); +void tdResetTSchemaBuilder(STSchemaBuilder *pBuilder, int32_t version); +int tdAddColToSchema(STSchemaBuilder *pBuilder, int8_t type, int16_t colId, int16_t bytes); +STSchema *tdGetSchemaFromBuilder(STSchemaBuilder *pBuilder); // ----------------- Semantic timestamp key definition typedef uint64_t TKEY; @@ -134,7 +206,7 @@ SDataRow tdDataRowDup(SDataRow row); // offset here not include dataRow header length static FORCE_INLINE int tdAppendDataColVal(SDataRow row, const void *value, bool isCopyVarData, int8_t type, int32_t offset) { - ASSERT(value != NULL); + assert(value != NULL); int32_t toffset = offset + TD_DATA_ROW_HEAD_SIZE; if (IS_VAR_DATA_TYPE(type)) { @@ -145,7 +217,7 @@ static FORCE_INLINE int tdAppendDataColVal(SDataRow row, const void *value, bool dataRowLen(row) += varDataTLen(value); } else { if (offset == 0) { - ASSERT(type == TSDB_DATA_TYPE_TIMESTAMP); + assert(type == TSDB_DATA_TYPE_TIMESTAMP); TKEY tvalue = tdGetTKEY(*(TSKEY *)value); memcpy(POINTER_SHIFT(row, toffset), (const void *)(&tvalue), TYPE_BYTES[type]); } else { @@ -199,7 +271,7 @@ static FORCE_INLINE void tdSetColOfRowNullBySchema(SDataRow row, STSchema *pSche static FORCE_INLINE void tdCopyColOfRowBySchema(SDataRow dst, STSchema *pDstSchema, int dstIdx, SDataRow src, STSchema *pSrcSchema, int srcIdx) { int8_t type = pDstSchema->columns[dstIdx].type; - ASSERT(type == pSrcSchema->columns[srcIdx].type); + assert(type == pSrcSchema->columns[srcIdx].type); void *pData = tdGetPtrToCol(dst, pDstSchema, dstIdx); void *value = tdGetPtrToCol(src, pSrcSchema, srcIdx); @@ -285,7 +357,7 @@ static FORCE_INLINE const void *tdGetColDataOfRow(SDataCol *pCol, int row) { } static FORCE_INLINE int32_t dataColGetNEleLen(SDataCol *pDataCol, int rows) { - ASSERT(rows > 0); + assert(rows > 0); if (IS_VAR_DATA_TYPE(pDataCol->type)) { return pDataCol->dataOff[rows - 1] + varDataTLen(tdGetColDataOfRow(pDataCol, rows - 1)); @@ -315,7 +387,7 @@ static FORCE_INLINE TKEY dataColsTKeyFirst(SDataCols *pCols) { } static FORCE_INLINE TSKEY dataColsKeyAtRow(SDataCols *pCols, int row) { - ASSERT(row < pCols->numOfRows); + assert(row < pCols->numOfRows); return dataColsKeyAt(pCols, row); } @@ -413,7 +485,7 @@ static FORCE_INLINE void *tdGetKVRowIdxOfCol(SKVRow row, int16_t colId) { // offset here not include kvRow header length static FORCE_INLINE int tdAppendKvColVal(SKVRow row, const void *value, bool isCopyValData, int16_t colId, int8_t type, int32_t offset) { - ASSERT(value != NULL); + assert(value != NULL); int32_t toffset = offset + TD_KV_ROW_HEAD_SIZE; SColIdx *pColIdx = (SColIdx *)POINTER_SHIFT(row, toffset); char * ptr = (char *)POINTER_SHIFT(row, kvRowLen(row)); @@ -428,7 +500,7 @@ static FORCE_INLINE int tdAppendKvColVal(SKVRow row, const void *value, bool isC kvRowLen(row) += varDataTLen(value); } else { if (offset == 0) { - ASSERT(type == TSDB_DATA_TYPE_TIMESTAMP); + assert(type == TSDB_DATA_TYPE_TIMESTAMP); TKEY tvalue = tdGetTKEY(*(TSKEY *)value); memcpy(ptr, (void *)(&tvalue), TYPE_BYTES[type]); } else { @@ -741,4 +813,4 @@ static FORCE_INLINE char *payloadNextCol(char *pCol) { return (char *)POINTER_SH } #endif -#endif // _TD_DATA_FORMAT_H_ +#endif /*_TD_COMMON_DATA_FORMAT_H_*/ diff --git a/src/common/inc/tglobal.h b/include/common/tglobal.h similarity index 83% rename from src/common/inc/tglobal.h rename to include/common/tglobal.h index 604ce89432bcf662b319fb2ec11f55026450a2be..ffabe0d935622bf6beffe9d94fc8c9e705a8857a 100644 --- a/src/common/inc/tglobal.h +++ b/include/common/tglobal.h @@ -13,15 +13,15 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_COMMON_GLOBAL_H -#define TDENGINE_COMMON_GLOBAL_H - -#include "taosdef.h" +#ifndef _TD_COMMON_GLOBAL_H_ +#define _TD_COMMON_GLOBAL_H_ #ifdef __cplusplus extern "C" { #endif +#include "tdef.h" + // cluster extern char tsFirst[]; extern char tsSecond[]; @@ -36,12 +36,9 @@ extern int32_t tsStatusInterval; extern int32_t tsNumOfMnodes; extern int8_t tsEnableVnodeBak; extern int8_t tsEnableTelemetryReporting; -extern char tsEmail[]; extern char tsArbitrator[]; extern int8_t tsArbOnline; extern int64_t tsArbOnlineTimestamp; -extern int32_t tsDnodeId; -extern int64_t tsDnodeStartTime; // common extern int tsRpcTimer; @@ -55,9 +52,6 @@ extern float tsNumOfThreadsPerCore; extern int32_t tsNumOfCommitThreads; extern float tsRatioOfQueryCores; extern int8_t tsDaylight; -extern char tsTimezone[]; -extern char tsLocale[]; -extern char tsCharset[]; // default encode string extern int8_t tsEnableCoreFile; extern int32_t tsCompressMsgSize; extern int32_t tsCompressColData; @@ -152,27 +146,16 @@ extern int32_t tsMonitorInterval; extern int8_t tsEnableStream; // internal -extern int8_t tsCompactMnodeWal; extern int8_t tsPrintAuth; extern int8_t tscEmbedded; -extern char configDir[]; extern char tsVnodeDir[]; -extern char tsDnodeDir[]; extern char tsMnodeDir[]; extern char tsMnodeBakDir[]; extern char tsMnodeTmpDir[]; -extern char tsDataDir[]; -extern char tsLogDir[]; -extern char tsScriptDir[]; extern int64_t tsTickPerDay[3]; extern int32_t tsTopicBianryLen; // system info -extern char tsOsName[]; -extern int64_t tsPageSize; -extern int64_t tsOpenMax; -extern int64_t tsStreamMax; -extern int32_t tsNumOfCores; extern float tsTotalLogDirGB; extern float tsTotalTmpDirGB; extern float tsTotalDataDirGB; @@ -183,7 +166,6 @@ extern float tsUsedDataDirGB; extern float tsMinimalLogDirGB; extern float tsReservedTmpDirectorySpace; extern float tsMinimalDataDirGB; -extern int32_t tsTotalMemoryMB; extern uint32_t tsVersion; // build info @@ -193,28 +175,6 @@ extern char gitinfo[]; extern char gitinfoOfInternal[]; extern char buildinfo[]; -// log -extern int8_t tsAsyncLog; -extern int32_t tsNumOfLogLines; -extern int32_t tsLogKeepDays; -extern int32_t dDebugFlag; -extern int32_t vDebugFlag; -extern int32_t mDebugFlag; -extern uint32_t cDebugFlag; -extern int32_t jniDebugFlag; -extern int32_t tmrDebugFlag; -extern int32_t sdbDebugFlag; -extern int32_t httpDebugFlag; -extern int32_t mqttDebugFlag; -extern int32_t monDebugFlag; -extern int32_t uDebugFlag; -extern int32_t rpcDebugFlag; -extern int32_t odbcDebugFlag; -extern uint32_t qDebugFlag; -extern int32_t wDebugFlag; -extern int32_t cqDebugFlag; -extern int32_t debugFlag; - #ifdef TD_TSZ // lossy extern char lossyColumns[]; @@ -239,7 +199,6 @@ extern SDiskCfg tsDiskCfg[]; void taosInitGlobalCfg(); int32_t taosCheckGlobalCfg(); -void taosSetAllDebugFlag(); bool taosCfgDynamicOptions(char *msg); int taosGetFqdnPortFromEp(const char *ep, char *fqdn, uint16_t *port); bool taosCheckBalanceCfgOptions(const char *option, int32_t *vnodeId, int32_t *dnodeId); @@ -251,4 +210,4 @@ void taosPrintDataDirCfg(); } #endif -#endif +#endif /*_TD_COMMON_GLOBAL_H_*/ diff --git a/src/common/inc/tlocale.h b/include/common/tlocale.h similarity index 88% rename from src/common/inc/tlocale.h rename to include/common/tlocale.h index 91ae3866226e65cc305b03338075380b3949dd96..d809c75d67983fb6ac844a656a361876ea7d3504 100644 --- a/src/common/inc/tlocale.h +++ b/include/common/tlocale.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_COMMON_LOCALE_H -#define TDENGINE_COMMON_LOCALE_H +#ifndef _TD_COMMON_LOCALE_H_ +#define _TD_COMMON_LOCALE_H_ #ifdef __cplusplus extern "C" { @@ -24,6 +24,6 @@ void tsSetLocale(); #ifdef __cplusplus } -#endif +#endif /*_TD_COMMON_LOCALE_H_*/ #endif diff --git a/src/common/inc/tcmdtype.h b/include/common/tmsgtype.h similarity index 95% rename from src/common/inc/tcmdtype.h rename to include/common/tmsgtype.h index 918763ebb4b92399872f10c0dd632689eaa08d1b..d357ca1f47727998588553ec516eda691c0f97db 100644 --- a/src/common/inc/tcmdtype.h +++ b/include/common/tmsgtype.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TSQLMSGTYPE_H -#define TDENGINE_TSQLMSGTYPE_H +#ifndef _TD_COMMON_SQLMSGTYPE_H_ +#define _TD_COMMON_SQLMSGTYPE_H_ #ifdef __cplusplus extern "C" { @@ -24,7 +24,7 @@ extern "C" { #ifdef TSDB_SQL_C #define TSDB_DEFINE_SQL_TYPE( name, msg ) msg, -char *sqlCmd[] = { +char *sqlMsgType[] = { "null", #else #define TSDB_DEFINE_SQL_TYPE( name, msg ) name, @@ -103,17 +103,15 @@ enum { }; // create table operation type -enum TSQL_TYPE { - TSQL_CREATE_TABLE = 0x1, +enum TSQL_CREATE_TABLE_TYPE { + TSQL_CREATE_TABLE = 0x1, TSQL_CREATE_STABLE = 0x2, - TSQL_CREATE_TABLE_FROM_STABLE = 0x3, + TSQL_CREATE_CTABLE = 0x3, TSQL_CREATE_STREAM = 0x4, }; -extern char *sqlCmd[]; - #ifdef __cplusplus } #endif -#endif // TDENGINE_TSQLMSGTYPE_H +#endif /*_TD_COMMON_SQLMSGTYPE_H_*/ diff --git a/include/common/tname.h b/include/common/tname.h new file mode 100644 index 0000000000000000000000000000000000000000..18526f54d404d59f11a2359e7d9584c6c9cab2e1 --- /dev/null +++ b/include/common/tname.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef TDENGINE_TNAME_H +#define TDENGINE_TNAME_H + +#define TSDB_DB_NAME_T 1 +#define TSDB_TABLE_NAME_T 2 + +#define T_NAME_ACCT 0x1u +#define T_NAME_DB 0x2u +#define T_NAME_TABLE 0x4u + +typedef struct SName { + uint8_t type; //db_name_t, table_name_t + char acctId[TSDB_ACCT_ID_LEN]; + char dbname[TSDB_DB_NAME_LEN]; + char tname[TSDB_TABLE_NAME_LEN]; +} SName; + +int32_t tNameExtractFullName(const SName* name, char* dst); + +int32_t tNameLen(const SName* name); + +SName* tNameDup(const SName* name); + +bool tIsValidName(const SName* name); + +const char* tNameGetTableName(const SName* name); + +int32_t tNameGetDbName(const SName* name, char* dst); + +int32_t tNameGetFullDbName(const SName* name, char* dst); + +bool tNameIsEmpty(const SName* name); + +void tNameAssign(SName* dst, const SName* src); + +int32_t tNameFromString(SName* dst, const char* str, uint32_t type); + +int32_t tNameSetAcctId(SName* dst, const char* acct); + +#if 0 +int32_t tNameSetDbName(SName* dst, const char* acct, SToken* dbToken); +#endif + +#endif // TDENGINE_TNAME_H diff --git a/include/common/trow.h b/include/common/trow.h index c6e95d5d61d6fbd7782bfb11d1bf361165810586..be4b7af32a7d92d313b7fdec83a2bceb600ec827 100644 --- a/include/common/trow.h +++ b/include/common/trow.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef _TD_TROW_H_ -#define _TD_TROW_H_ +#ifndef _TD_COMMON_ROW_H_ +#define _TD_COMMON_ROW_H_ #ifdef __cplusplus extern "C" { @@ -24,4 +24,4 @@ extern "C" { } #endif -#endif /*_TD_TROW_H_*/ \ No newline at end of file +#endif /*_TD_COMMON_ROW_H_*/ \ No newline at end of file diff --git a/src/os/inc/osTime.h b/include/common/ttime.h similarity index 52% rename from src/os/inc/osTime.h rename to include/common/ttime.h index dcb0e4c9b630216600c4f8f017b8154e9bb9dac4..0d9b89b6f9c22e484917a2af2d26a56f40094b1b 100644 --- a/src/os/inc/osTime.h +++ b/include/common/ttime.h @@ -13,54 +13,15 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_OS_TIME_H -#define TDENGINE_OS_TIME_H +#ifndef _TD_COMMON_TIME_H_ +#define _TD_COMMON_TIME_H_ #ifdef __cplusplus extern "C" { #endif -#include "os.h" #include "taosdef.h" - -#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) - #ifdef _TD_GO_DLL_ - #define MILLISECOND_PER_SECOND (1000LL) - #else - #define MILLISECOND_PER_SECOND (1000i64) - #endif -#else - #define MILLISECOND_PER_SECOND ((int64_t)1000L) -#endif - -#define MILLISECOND_PER_MINUTE (MILLISECOND_PER_SECOND * 60) -#define MILLISECOND_PER_HOUR (MILLISECOND_PER_MINUTE * 60) -#define MILLISECOND_PER_DAY (MILLISECOND_PER_HOUR * 24) -#define MILLISECOND_PER_WEEK (MILLISECOND_PER_DAY * 7) - -//@return timestamp in second -int32_t taosGetTimestampSec(); - -//@return timestamp in millisecond -static FORCE_INLINE int64_t taosGetTimestampMs() { - struct timeval systemTime; - gettimeofday(&systemTime, NULL); - return (int64_t)systemTime.tv_sec * 1000L + (int64_t)systemTime.tv_usec / 1000; -} - -//@return timestamp in microsecond -static FORCE_INLINE int64_t taosGetTimestampUs() { - struct timeval systemTime; - gettimeofday(&systemTime, NULL); - return (int64_t)systemTime.tv_sec * 1000000L + (int64_t)systemTime.tv_usec; -} - -//@return timestamp in nanosecond -static FORCE_INLINE int64_t taosGetTimestampNs() { - struct timespec systemTime = {0}; - clock_gettime(CLOCK_REALTIME, &systemTime); - return (int64_t)systemTime.tv_sec * 1000000000L + (int64_t)systemTime.tv_nsec; -} +#include "taosmsg.h" /* * @return timestamp decided by global conf variable, tsTimePrecision @@ -77,22 +38,6 @@ static FORCE_INLINE int64_t taosGetTimestamp(int32_t precision) { } } - -typedef struct SInterval { - int32_t tz; // query client timezone - char intervalUnit; - char slidingUnit; - char offsetUnit; - int64_t interval; - int64_t sliding; - int64_t offset; -} SInterval; - -typedef struct SSessionWindow { - int64_t gap; // gap between two session window(in microseconds) - int32_t primaryColId; // primary timestamp column -} SSessionWindow; - int64_t taosTimeAdd(int64_t t, int64_t duration, char unit, int32_t precision); int64_t taosTimeTruncate(int64_t t, const SInterval* pInterval, int32_t precision); int32_t taosTimeCountInterval(int64_t skey, int64_t ekey, int64_t interval, char unit, int32_t precision); @@ -104,8 +49,10 @@ int32_t taosParseTime(char* timestr, int64_t* time, int32_t len, int32_t timePre void deltaToUtcInitOnce(); int64_t convertTimePrecision(int64_t time, int32_t fromPrecision, int32_t toPrecision); + + #ifdef __cplusplus } #endif -#endif // TDENGINE_TTIME_H +#endif /*_TD_COMMON_TIME_H_*/ diff --git a/src/common/inc/ttimezone.h b/include/common/ttimezone.h similarity index 88% rename from src/common/inc/ttimezone.h rename to include/common/ttimezone.h index 0ae983dba054739bdd8521dfb2e1f6447205014c..cc50a29d57a2f1a308b8b236fc7bb80122adb387 100644 --- a/src/common/inc/ttimezone.h +++ b/include/common/ttimezone.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_COMMON_TIMEZONE_H -#define TDENGINE_COMMON_TIMEZONE_H +#ifndef _TD_COMMON_TIMEZONE_H_ +#define _TD_COMMON_TIMEZONE_H_ #ifdef __cplusplus extern "C" { @@ -26,4 +26,4 @@ void tsSetTimeZone(); } #endif -#endif +#endif /*_TD_COMMON_TIMEZONE_H_*/ diff --git a/include/common/ttokendef.h b/include/common/ttokendef.h new file mode 100644 index 0000000000000000000000000000000000000000..5f9fe5134bb9b6ac7cb5c8996b26e7a7e86017a2 --- /dev/null +++ b/include/common/ttokendef.h @@ -0,0 +1,228 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef _TD_COMMON_TOKEN_DEF_H_ +#define _TD_COMMON_TOKEN_DEF_H_ + +#define TK_ID 1 +#define TK_BOOL 2 +#define TK_TINYINT 3 +#define TK_SMALLINT 4 +#define TK_INTEGER 5 +#define TK_BIGINT 6 +#define TK_FLOAT 7 +#define TK_DOUBLE 8 +#define TK_STRING 9 +#define TK_TIMESTAMP 10 +#define TK_BINARY 11 +#define TK_NCHAR 12 +#define TK_OR 13 +#define TK_AND 14 +#define TK_NOT 15 +#define TK_EQ 16 +#define TK_NE 17 +#define TK_ISNULL 18 +#define TK_NOTNULL 19 +#define TK_IS 20 +#define TK_LIKE 21 +#define TK_MATCH 22 +#define TK_NMATCH 23 +#define TK_GLOB 24 +#define TK_BETWEEN 25 +#define TK_IN 26 +#define TK_GT 27 +#define TK_GE 28 +#define TK_LT 29 +#define TK_LE 30 +#define TK_BITAND 31 +#define TK_BITOR 32 +#define TK_LSHIFT 33 +#define TK_RSHIFT 34 +#define TK_PLUS 35 +#define TK_MINUS 36 +#define TK_DIVIDE 37 +#define TK_TIMES 38 +#define TK_STAR 39 +#define TK_SLASH 40 +#define TK_REM 41 +#define TK_CONCAT 42 +#define TK_UMINUS 43 +#define TK_UPLUS 44 +#define TK_BITNOT 45 +#define TK_SHOW 46 +#define TK_DATABASES 47 +#define TK_TOPICS 48 +#define TK_FUNCTIONS 49 +#define TK_MNODES 50 +#define TK_DNODES 51 +#define TK_ACCOUNTS 52 +#define TK_USERS 53 +#define TK_MODULES 54 +#define TK_QUERIES 55 +#define TK_CONNECTIONS 56 +#define TK_STREAMS 57 +#define TK_VARIABLES 58 +#define TK_SCORES 59 +#define TK_GRANTS 60 +#define TK_VNODES 61 +#define TK_DOT 62 +#define TK_CREATE 63 +#define TK_TABLE 64 +#define TK_STABLE 65 +#define TK_DATABASE 66 +#define TK_TABLES 67 +#define TK_STABLES 68 +#define TK_VGROUPS 69 +#define TK_DROP 70 +#define TK_TOPIC 71 +#define TK_FUNCTION 72 +#define TK_DNODE 73 +#define TK_USER 74 +#define TK_ACCOUNT 75 +#define TK_USE 76 +#define TK_DESCRIBE 77 +#define TK_DESC 78 +#define TK_ALTER 79 +#define TK_PASS 80 +#define TK_PRIVILEGE 81 +#define TK_LOCAL 82 +#define TK_COMPACT 83 +#define TK_LP 84 +#define TK_RP 85 +#define TK_IF 86 +#define TK_EXISTS 87 +#define TK_AS 88 +#define TK_OUTPUTTYPE 89 +#define TK_AGGREGATE 90 +#define TK_BUFSIZE 91 +#define TK_PPS 92 +#define TK_TSERIES 93 +#define TK_DBS 94 +#define TK_STORAGE 95 +#define TK_QTIME 96 +#define TK_CONNS 97 +#define TK_STATE 98 +#define TK_COMMA 99 +#define TK_KEEP 100 +#define TK_CACHE 101 +#define TK_REPLICA 102 +#define TK_QUORUM 103 +#define TK_DAYS 104 +#define TK_MINROWS 105 +#define TK_MAXROWS 106 +#define TK_BLOCKS 107 +#define TK_CTIME 108 +#define TK_WAL 109 +#define TK_FSYNC 110 +#define TK_COMP 111 +#define TK_PRECISION 112 +#define TK_UPDATE 113 +#define TK_CACHELAST 114 +#define TK_PARTITIONS 115 +#define TK_UNSIGNED 116 +#define TK_TAGS 117 +#define TK_USING 118 +#define TK_NULL 119 +#define TK_NOW 120 +#define TK_SELECT 121 +#define TK_UNION 122 +#define TK_ALL 123 +#define TK_DISTINCT 124 +#define TK_FROM 125 +#define TK_VARIABLE 126 +#define TK_INTERVAL 127 +#define TK_EVERY 128 +#define TK_SESSION 129 +#define TK_STATE_WINDOW 130 +#define TK_FILL 131 +#define TK_SLIDING 132 +#define TK_ORDER 133 +#define TK_BY 134 +#define TK_ASC 135 +#define TK_GROUP 136 +#define TK_HAVING 137 +#define TK_LIMIT 138 +#define TK_OFFSET 139 +#define TK_SLIMIT 140 +#define TK_SOFFSET 141 +#define TK_WHERE 142 +#define TK_RESET 143 +#define TK_QUERY 144 +#define TK_SYNCDB 145 +#define TK_ADD 146 +#define TK_COLUMN 147 +#define TK_MODIFY 148 +#define TK_TAG 149 +#define TK_CHANGE 150 +#define TK_SET 151 +#define TK_KILL 152 +#define TK_CONNECTION 153 +#define TK_STREAM 154 +#define TK_COLON 155 +#define TK_ABORT 156 +#define TK_AFTER 157 +#define TK_ATTACH 158 +#define TK_BEFORE 159 +#define TK_BEGIN 160 +#define TK_CASCADE 161 +#define TK_CLUSTER 162 +#define TK_CONFLICT 163 +#define TK_COPY 164 +#define TK_DEFERRED 165 +#define TK_DELIMITERS 166 +#define TK_DETACH 167 +#define TK_EACH 168 +#define TK_END 169 +#define TK_EXPLAIN 170 +#define TK_FAIL 171 +#define TK_FOR 172 +#define TK_IGNORE 173 +#define TK_IMMEDIATE 174 +#define TK_INITIALLY 175 +#define TK_INSTEAD 176 +#define TK_KEY 177 +#define TK_OF 178 +#define TK_RAISE 179 +#define TK_REPLACE 180 +#define TK_RESTRICT 181 +#define TK_ROW 182 +#define TK_STATEMENT 183 +#define TK_TRIGGER 184 +#define TK_VIEW 185 +#define TK_IPTOKEN 186 +#define TK_SEMI 187 +#define TK_NONE 188 +#define TK_PREV 189 +#define TK_LINEAR 190 +#define TK_IMPORT 191 +#define TK_TBNAME 192 +#define TK_JOIN 193 +#define TK_INSERT 194 +#define TK_INTO 195 +#define TK_VALUES 196 + + +#define TK_SPACE 300 +#define TK_COMMENT 301 +#define TK_ILLEGAL 302 +#define TK_HEX 303 // hex number 0x123 +#define TK_OCT 304 // oct number +#define TK_BIN 305 // bin format data 0b111 +#define TK_FILE 306 +#define TK_QUESTION 307 // denoting the placeholder of "?",when invoking statement bind query + +#endif /*_TD_COMMON_TOKEN_DEF_H_*/ + + diff --git a/src/inc/ttype.h b/include/common/ttypes.h similarity index 98% rename from src/inc/ttype.h rename to include/common/ttypes.h index 3f1606e957564f0a86988a389071957d2204d391..6fe6e11d0571d74f0e01f1bfe5d7bdbddd1220e1 100644 --- a/src/inc/ttype.h +++ b/include/common/ttypes.h @@ -5,8 +5,6 @@ extern "C" { #endif -#include -#include #include "taosdef.h" // ----------------- For variable data types such as TSDB_DATA_TYPE_BINARY and TSDB_DATA_TYPE_NCHAR @@ -207,7 +205,6 @@ void operateVal(void *dst, void *s1, void *s2, int32_t optr, int32_t type); void* getDataMin(int32_t type); void* getDataMax(int32_t type); -int32_t tStrToInteger(const char* z, int16_t type, int32_t n, int64_t* value, bool issigned); #define SET_DOUBLE_NULL(v) (*(uint64_t *)(v) = TSDB_DATA_DOUBLE_NULL) diff --git a/src/common/inc/tvariant.h b/include/common/tvariant.h similarity index 53% rename from src/common/inc/tvariant.h rename to include/common/tvariant.h index c69a662846e15d515136d51a95b39d488a282f5c..13c8aff8e724907217fff0c5629c991610c8d2c3 100644 --- a/src/common/inc/tvariant.h +++ b/include/common/tvariant.h @@ -16,46 +16,47 @@ #ifndef TDENGINE_TVARIANT_H #define TDENGINE_TVARIANT_H -#include "tarray.h" -#include "ttoken.h" - #ifdef __cplusplus extern "C" { #endif +#include "tarray.h" + // variant, each number/string/field_id has a corresponding struct during parsing sql -typedef struct tVariant { - uint32_t nType; - int32_t nLen; // only used for string, for number, it is useless +typedef struct SVariant { + uint32_t nType; + int32_t nLen; // only used for string, for number, it is useless union { int64_t i64; uint64_t u64; - double dKey; - char * pz; + double d; + char *pz; wchar_t *wpz; SArray *arr; // only for 'in' query to hold value list, not value for a field }; -} tVariant; +} SVariant; -bool tVariantIsValid(tVariant *pVar); +bool taosVariantIsValid(SVariant *pVar); -void tVariantCreate(tVariant *pVar, SStrToken *token); +void taosVariantCreate(SVariant *pVar, char* z, int32_t n, int32_t type); -void tVariantCreateFromBinary(tVariant *pVar, const char *pz, size_t len, uint32_t type); +void taosVariantCreateFromBinary(SVariant *pVar, const char *pz, size_t len, uint32_t type); -void tVariantDestroy(tVariant *pV); +void taosVariantDestroy(SVariant *pV); -void tVariantAssign(tVariant *pDst, const tVariant *pSrc); +void taosVariantAssign(SVariant *pDst, const SVariant *pSrc); -int32_t tVariantCompare(const tVariant* p1, const tVariant* p2); +int32_t taosVariantCompare(const SVariant* p1, const SVariant* p2); -int32_t tVariantToString(tVariant *pVar, char *dst); +int32_t taosVariantToString(SVariant *pVar, char *dst); -int32_t tVariantDump(tVariant *pVariant, char *payload, int16_t type, bool includeLengthPrefix); +int32_t taosVariantDump(SVariant *pVariant, char *payload, int16_t type, bool includeLengthPrefix); -int32_t tVariantDumpEx(tVariant *pVariant, char *payload, int16_t type, bool includeLengthPrefix, bool *converted, char *extInfo); +#if 0 +int32_t taosVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool includeLengthPrefix, bool *converted, char *extInfo); +#endif -int32_t tVariantTypeSetType(tVariant *pVariant, char type); +int32_t taosVariantTypeSetType(SVariant *pVariant, char type); #ifdef __cplusplus } diff --git a/include/libs/catalog/catalog.h b/include/libs/catalog/catalog.h index de37807cf6c3431a03b25fafa4b25ea0cf016fcd..c4a244adc41f2c4090e72cce7c2354cf1e348831 100644 --- a/include/libs/catalog/catalog.h +++ b/include/libs/catalog/catalog.h @@ -20,6 +20,91 @@ extern "C" { #endif +#include "os.h" +#include "thash.h" +#include "tarray.h" +#include "taosdef.h" +#include "transport.h" +#include "common.h" +#include "taosmsg.h" + +struct SCatalog; + +typedef struct SMetaReq { + char clusterId[TSDB_CLUSTER_ID_LEN]; + SArray *pTableName; // table full name + SArray *pVgroup; // vgroup id + SArray *pUdf; // udf name + bool qNodeEpset; // valid qnode +} SMetaReq; + +typedef struct SMetaData { + SArray *pTableMeta; // tableMeta + SArray *pVgroupInfo; // vgroupInfo list + SArray *pUdfList; // udf info list + SEpSet *pEpSet; // qnode epset list +} SMetaData; + +typedef struct STableComInfo { + uint8_t numOfTags; // the number of tags in schema + uint8_t precision; // the number of precision + int16_t numOfColumns; // the number of columns + int32_t rowSize; // row size of the schema +} STableComInfo; + +/* + * ASSERT(sizeof(SCTableMeta) == 24) + * ASSERT(tableType == TSDB_CHILD_TABLE) + * The cached child table meta info. For each child table, 24 bytes are required to keep the essential table info. + */ +typedef struct SCTableMeta { + int32_t vgId:24; + int8_t tableType; + uint32_t tid; + uint64_t uid; + uint64_t suid; +} SCTableMeta; + +/* + * Note that the first 24 bytes of STableMeta are identical to SCTableMeta, it is safe to cast a STableMeta to be a SCTableMeta. + */ +typedef struct STableMeta { + int32_t vgId:24; + int8_t tableType; + uint32_t tid; + uint64_t uid; + uint64_t suid; + // if the table is TSDB_CHILD_TABLE, the following information is acquired from the corresponding super table meta info + int16_t sversion; + int16_t tversion; + STableComInfo tableInfo; + SSchema schema[]; +} STableMeta; + +/** + * Catalog service object, which is utilized to hold tableMeta (meta/vgroupInfo/udfInfo) at the client-side. + * There is ONLY one SCatalog object for one process space, and this function returns a singleton. + * @param pMgmtEps + * @return + */ +struct SCatalog* getCatalogHandle(const SEpSet* pMgmtEps); + +/** + * Get the required meta data from mnode. + * Note that this is a synchronized API and is also thread-safety. + * @param pCatalog + * @param pMetaReq + * @param pMetaData + * @return + */ +int32_t catalogGetMetaData(struct SCatalog* pCatalog, const SMetaReq* pMetaReq, SMetaData* pMetaData); + +/** + * Destroy catalog service handle + * @param pCatalog + */ +void destroyCatalog(struct SCatalog* pCatalog); + #ifdef __cplusplus } #endif diff --git a/include/libs/executor/executor.h b/include/libs/executor/executor.h index 15416724c6491dfc2b3628513850526d44c031e9..c3c7d740f7a75ac27a20587f5d47300c085c4721 100644 --- a/include/libs/executor/executor.h +++ b/include/libs/executor/executor.h @@ -20,6 +20,170 @@ extern "C" { #endif +typedef void* qinfo_t; + +/** + * create the qinfo object according to QueryTableMsg + * @param tsdb + * @param pQueryTableMsg + * @param qinfo + * @return + */ +int32_t qCreateQueryInfo(void* tsdb, int32_t vgId, SQueryTableInfo* pQueryTableMsg, qinfo_t* qinfo, uint64_t qId); + +/** + * the main query execution function, including query on both table and multiple tables, + * which are decided according to the tag or table name query conditions + * + * @param qinfo + * @return + */ +bool qTableQuery(qinfo_t qinfo, uint64_t *qId); + +/** + * Retrieve the produced results information, if current query is not paused or completed, + * this function will be blocked to wait for the query execution completed or paused, + * in which case enough results have been produced already. + * + * @param qinfo + * @return + */ +int32_t qRetrieveQueryResultInfo(qinfo_t qinfo, bool* buildRes, void* pRspContext); + +/** + * + * Retrieve the actual results to fill the response message payload. + * Note that this function must be executed after qRetrieveQueryResultInfo is invoked. + * + * @param qinfo qinfo object + * @param pRsp response message + * @param contLen payload length + * @return + */ +int32_t qDumpRetrieveResult(qinfo_t qinfo, SRetrieveTableRsp** pRsp, int32_t* contLen, bool* continueExec); + +/** + * return the transporter context (RPC) + * @param qinfo + * @return + */ +void* qGetResultRetrieveMsg(qinfo_t qinfo); + +/** + * kill the ongoing query and free the query handle and corresponding resources automatically + * @param qinfo qhandle + * @return + */ +int32_t qKillQuery(qinfo_t qinfo); + +/** + * return whether query is completed or not + * @param qinfo + * @return + */ +int32_t qIsQueryCompleted(qinfo_t qinfo); + +/** + * destroy query info structure + * @param qHandle + */ +void qDestroyQueryInfo(qinfo_t qHandle); + +/** + * Get the queried table uid + * @param qHandle + * @return + */ +int64_t qGetQueriedTableUid(qinfo_t qHandle); + +/** + * Extract the qualified table id list, and than pass them to the TSDB driver to load the required table data blocks. + * + * @param iter the table iterator to traverse all tables belongs to a super table, or an invert index + * @return + */ +int32_t qGetQualifiedTableIdList(void* pTableList, const char* tagCond, int32_t tagCondLen, SArray* pTableIdList); + +/** + * Create the table group according to the group by tags info + * @param pTableIdList + * @param skey + * @param groupInfo + * @param groupByIndex + * @param numOfIndex + * @return + */ +int32_t qCreateTableGroupByGroupExpr(SArray* pTableIdList, TSKEY skey, STableGroupInfo groupInfo, SColIndex* groupByIndex, int32_t numOfIndex); + +/** + * Update the table id list of a given query. + * @param uid child table uid + * @param type operation type: ADD|DROP + * @return + */ +int32_t qUpdateQueriedTableIdList(qinfo_t qinfo, int64_t uid, int32_t type); + +//================================================================================================ +// query handle management +/** + * Query handle mgmt object + * @param vgId + * @return + */ +void* qOpenQueryMgmt(int32_t vgId); + +/** + * broadcast the close information and wait for all query stop. + * @param pExecutor + */ +void qQueryMgmtNotifyClosed(void* pExecutor); + +/** + * Re-open the query handle management module when opening the vnode again. + * @param pExecutor + */ +void qQueryMgmtReOpen(void *pExecutor); + +/** + * Close query mgmt and clean up resources. + * @param pExecutor + */ +void qCleanupQueryMgmt(void* pExecutor); + +/** + * Add the query into the query mgmt object + * @param pMgmt + * @param qId + * @param qInfo + * @return + */ +void** qRegisterQInfo(void* pMgmt, uint64_t qId, void *qInfo); + +/** + * acquire the query handle according to the key from query mgmt object. + * @param pMgmt + * @param key + * @return + */ +void** qAcquireQInfo(void* pMgmt, uint64_t key); + +/** + * release the query handle and decrease the reference count in cache + * @param pMgmt + * @param pQInfo + * @param freeHandle + * @return + */ +void** qReleaseQInfo(void* pMgmt, void* pQInfo); + +/** + * De-register the query handle from the management module and free it immediately. + * @param pMgmt + * @param pQInfo + * @return + */ +void** qDeregisterQInfo(void* pMgmt, void* pQInfo); + #ifdef __cplusplus } #endif diff --git a/include/libs/parser/parser.h b/include/libs/parser/parser.h index 082dd4bcdd545997ed17da6db467eeec9a502893..eeb5f4cd8e8abbecf0dce2f7d4867668f0b534d3 100644 --- a/include/libs/parser/parser.h +++ b/include/libs/parser/parser.h @@ -20,6 +20,192 @@ extern "C" { #endif +#include "catalog.h" +#include "common.h" +#include "tname.h" + +//typedef struct SInterval { +// int32_t tz; // query client timezone +// char intervalUnit; +// char slidingUnit; +// char offsetUnit; +// int64_t interval; +// int64_t sliding; +// int64_t offset; +//} SInterval; +// +//typedef struct SSessionWindow { +// int64_t gap; // gap between two session window(in microseconds) +// int32_t primaryColId; // primary timestamp column +//} SSessionWindow; + +typedef struct SGroupbyExpr { + int16_t tableIndex; + SArray* columnInfo; // SArray, group by columns information + int16_t orderIndex; // order by column index + int16_t orderType; // order by type: asc/desc +} SGroupbyExpr; + +typedef struct SField { + char name[TSDB_COL_NAME_LEN]; + uint8_t type; + int16_t bytes; +} SField; + +typedef struct SFieldInfo { + int16_t numOfOutput; // number of column in result + SField *final; + SArray *internalField; // SArray +} SFieldInfo; + +typedef struct SLimit { + int64_t limit; + int64_t offset; +} SLimit; + +typedef struct SOrder { + uint32_t order; + int32_t orderColId; +} SOrder; + +typedef struct SCond { + uint64_t uid; + int32_t len; // length of tag query condition data + char * cond; +} SCond; + +typedef struct SJoinNode { + uint64_t uid; + int16_t tagColId; + SArray* tsJoin; + SArray* tagJoin; +} SJoinNode; + +typedef struct SJoinInfo { + bool hasJoin; + SJoinNode *joinTables[TSDB_MAX_JOIN_TABLE_NUM]; +} SJoinInfo; + +typedef struct STagCond { + int16_t relType; // relation between tbname list and query condition, including : TK_AND or TK_OR + SCond tbnameCond; // tbname query condition, only support tbname query condition on one table + SJoinInfo joinInfo; // join condition, only support two tables join currently + SArray *pCond; // for different table, the query condition must be seperated +} STagCond; + +typedef struct STableMetaInfo { + STableMeta *pTableMeta; // table meta, cached in client side and acquired by name + uint32_t tableMetaSize; + size_t tableMetaCapacity; + SVgroupsInfo *vgroupList; + SArray *pVgroupTables; // SArray + + /* + * 1. keep the vgroup index during the multi-vnode super table projection query + * 2. keep the vgroup index for multi-vnode insertion + */ + int32_t vgroupIndex; + SName name; + char aliasName[TSDB_TABLE_NAME_LEN]; // alias name of table specified in query sql + SArray *tagColList; // SArray, involved tag columns +} STableMetaInfo; + +typedef struct SQueryStmtInfo { + int16_t command; // the command may be different for each subclause, so keep it seperately. + uint32_t type; // query/insert type + STimeWindow window; // the whole query time window + SInterval interval; // tumble time window + SSessionWindow sessionWindow; // session time window + SGroupbyExpr groupbyExpr; // groupby tags info + SArray * colList; // SArray + SFieldInfo fieldsInfo; + SArray * exprList; // SArray + SArray * exprList1; // final exprlist in case of arithmetic expression exists + SLimit limit; + SLimit slimit; + STagCond tagCond; + SArray * colCond; + SOrder order; + int16_t numOfTables; + int16_t curTableIdx; + STableMetaInfo **pTableMetaInfo; + struct STSBuf *tsBuf; + + int16_t fillType; // final result fill type + int64_t * fillVal; // default value for fill + int32_t numOfFillVal; // fill value size + + char * msg; // pointer to the pCmd->payload to keep error message temporarily + int64_t clauseLimit; // limit for current sub clause + + int64_t prjOffset; // offset value in the original sql expression, only applied at client side + int64_t vgroupLimit; // table limit in case of super table projection query + global order + limit + + int32_t udColumnId; // current user-defined constant output field column id, monotonically decreases from TSDB_UD_COLUMN_INDEX + bool distinct; // distinct tag or not + bool onlyHasTagCond; + int32_t bufLen; + char* buf; + SArray *pUdfInfo; + + struct SQueryStmtInfo *sibling; // sibling + SArray *pUpstream; // SArray + struct SQueryStmtInfo *pDownstream; + int32_t havingFieldNum; + bool stableQuery; + bool groupbyColumn; + bool simpleAgg; + bool arithmeticOnAgg; + bool projectionQuery; + bool hasFilter; + bool onlyTagQuery; + bool orderProjectQuery; + bool stateWindow; + bool globalMerge; + bool multigroupResult; +} SQueryStmtInfo; + +struct SInsertStmtInfo; + +/** + * True will be returned if the input sql string is insert, false otherwise. + * @param pStr sql string + * @param length length of the sql string + * @return + */ +bool qIsInsertSql(const char* pStr, size_t length); + +/** + * Parse the sql statement and then return the SQueryStmtInfo as the result of bounded AST. + * @param pSql sql string + * @param length length of the sql string + * @param id operator id, generated by uuid generator + * @param msg extended error message if exists. + * @return error code + */ +int32_t qParseQuerySql(const char* pStr, size_t length, struct SQueryStmtInfo** pQueryInfo, int64_t id, char* msg, int32_t msgLen); + +/** + * Parse the insert sql statement. + * @param pStr sql string + * @param length length of the sql string + * @param pInsertParam data in binary format to submit to vnode directly. + * @param id operator id, generated by uuid generator. + * @param msg extended error message if exists to help avoid the problem in sql statement. + * @return + */ +int32_t qParseInsertSql(const char* pStr, size_t length, struct SInsertStmtInfo** pInsertInfo, int64_t id, char* msg, int32_t msgLen); + +/** + * Convert a normal sql statement to only query tags information to enable that the subscribe client can be aware quickly of the true vgroup ids that + * involved in the subscribe procedure. + * @param pSql + * @param length + * @param pConvertSql + * @return + */ +int32_t qParserConvertSql(const char* pStr, size_t length, char** pConvertSql); + #ifdef __cplusplus } #endif diff --git a/include/libs/planner/planner.h b/include/libs/planner/planner.h index 6de7a5965391897aeace7d9e124d39cb5926feef..cbd9b6f89e557636d338669e35fce6335876afde 100644 --- a/include/libs/planner/planner.h +++ b/include/libs/planner/planner.h @@ -20,6 +20,96 @@ extern "C" { #endif +#define QUERY_TYPE_MERGE 1 +#define QUERY_TYPE_PARTIAL 2 + +struct SEpSet; +struct SQueryNode; +struct SQueryPhyNode; +struct SQueryStmtInfo; + +typedef struct SSubquery { + int64_t queryId; // the subquery id created by qnode + int32_t type; // QUERY_TYPE_MERGE|QUERY_TYPE_PARTIAL + int32_t level; // the execution level of current subquery, starting from 0. + SArray *pUpstream; // the upstream,from which to fetch the result + struct SQueryPhyNode *pNode; // physical plan of current subquery +} SSubquery; + +typedef struct SQueryJob { + SArray **pSubqueries; + int32_t numOfLevels; + int32_t currentLevel; +} SQueryJob; + + +/** + * Optimize the query execution plan, currently not implement yet. + * @param pQueryNode + * @return + */ +int32_t qOptimizeQueryPlan(struct SQueryNode* pQueryNode); + +/** + * Create the query plan according to the bound AST, which is in the form of pQueryInfo + * @param pQueryInfo + * @param pQueryNode + * @return + */ +int32_t qCreateQueryPlan(const struct SQueryStmtInfo* pQueryInfo, struct SQueryNode* pQueryNode); + +/** + * Convert the query plan to string, in order to display it in the shell. + * @param pQueryNode + * @return + */ +int32_t qQueryPlanToString(struct SQueryNode* pQueryNode, char** str); + +/** + * Restore the SQL statement according to the logic query plan. + * @param pQueryNode + * @param sql + * @return + */ +int32_t qQueryPlanToSql(struct SQueryNode* pQueryNode, char** sql); + +/** + * Create the physical plan for the query, according to the logic plan. + * @param pQueryNode + * @param pPhyNode + * @return + */ +int32_t qCreatePhysicalPlan(struct SQueryNode* pQueryNode, struct SEpSet* pQnode, struct SQueryPhyNode *pPhyNode); + +/** + * Convert to physical plan to string to enable to print it out in the shell. + * @param pPhyNode + * @param str + * @return + */ +int32_t qPhyPlanToString(struct SQueryPhyNode *pPhyNode, char** str); + +/** + * Destroy the query plan object. + * @return + */ +void* qDestroyQueryPlan(struct SQueryNode* pQueryNode); + +/** + * Destroy the physical plan. + * @param pQueryPhyNode + * @return + */ +void* qDestroyQueryPhyPlan(struct SQueryPhyNode* pQueryPhyNode); + +/** + * Create the query job from the physical execution plan + * @param pPhyNode + * @param pJob + * @return + */ +int32_t qCreateQueryJob(const struct SQueryPhyNode* pPhyNode, struct SQueryJob** pJob); + #ifdef __cplusplus } #endif diff --git a/include/libs/scheduler/scheduler.h b/include/libs/scheduler/scheduler.h index 205d88970f8ef87e54300dd0a5ab3500f28ba0d4..d9653046cfadc84683e8922b2ad94ebe2149017b 100644 --- a/include/libs/scheduler/scheduler.h +++ b/include/libs/scheduler/scheduler.h @@ -20,6 +20,32 @@ extern "C" { #endif +struct SQueryJob; + +/** + * Process the query job, generated according to the query physical plan. + * This is a synchronized API, and is also thread-safety. + * @param pJob + * @return + */ +int32_t qProcessQueryJob(struct SQueryJob* pJob); + +/** + * The SSqlObj should not be here???? + * @param pSql + * @param pVgroupId + * @param pRetVgroupId + * @return + */ +//SArray* qGetInvolvedVgroupIdList(struct SSqlObj* pSql, SArray* pVgroupId, SArray* pRetVgroupId); + +/** + * Cancel query job + * @param pJob + * @return + */ +int32_t qKillQueryJob(struct SQueryJob* pJob); + #ifdef __cplusplus } #endif diff --git a/include/libs/transport/transport.h b/include/libs/transport/transport.h index 27331088250b1ede4e63c7ca0c8e24c3178fadef..a05a76931a03803e88ce9613550620aaab203d47 100644 --- a/include/libs/transport/transport.h +++ b/include/libs/transport/transport.h @@ -20,6 +20,22 @@ extern "C" { #endif +//typedef struct SEpAddr { +// char fqdn[TSDB_FQDN_LEN]; +// uint16_t port; +//} SEpAddr; +// +//typedef struct SVgroup { +// int32_t vgId; +// int8_t numOfEps; +// SEpAddr epAddr[TSDB_MAX_REPLICA]; +//} SVgroup; +// +//typedef struct SVgroupsInfo { +// int32_t numOfVgroups; +// SVgroup vgroups[]; +//} SVgroupsInfo; + #ifdef __cplusplus } #endif diff --git a/src/inc/trpc.h b/include/libs/transport/trpc.h similarity index 100% rename from src/inc/trpc.h rename to include/libs/transport/trpc.h diff --git a/include/libs/wal/wal.h b/include/libs/wal/wal.h index 8f6d09f8d6251edd9a5398ab1752f366a88e2849..e59d60f7dc2f7e816cd302cfd24be164098d781f 100644 --- a/include/libs/wal/wal.h +++ b/include/libs/wal/wal.h @@ -15,13 +15,16 @@ #ifndef _TD_WAL_H_ #define _TD_WAL_H_ +#include "os.h" + #ifdef __cplusplus extern "C" { #endif typedef enum { TAOS_WAL_NOLOG = 0, - TAOS_WAL_WRITE = 1 + TAOS_WAL_WRITE = 1, + TAOS_WAL_FSYNC = 2 } EWalType; typedef struct { @@ -55,8 +58,8 @@ void walStop(twalh); void walClose(twalh); //write -int32_t walWrite(twalh, int8_t msgType, void* body, uint32_t bodyLen); -void walWaitFsync(twalh, bool forceHint); +int64_t walWrite(twalh, int8_t msgType, void* body, uint32_t bodyLen); +void walFsync(twalh, bool forceHint); //int32_t walCommit(twalh, uint64_t ver); //int32_t walRollback(twalh, uint64_t ver); @@ -67,7 +70,7 @@ int32_t walReadWithFp(twalh, FWalWrite writeFp, int64_t verStart, int readNum); //life cycle int32_t walDataPersisted(twalh, int64_t ver); int32_t walFirstVer(twalh); -int32_t walLastVer(twal); +int32_t walLastVer(twalh); //int32_t walDataCorrupted(twalh); #ifdef __cplusplus diff --git a/include/os/os.h b/include/os/os.h index 259aef814366810aac7bec8ba720d111e63023eb..e5731c79c73f9b443be7f0effd7c637ebd50ebc5 100644 --- a/include/os/os.h +++ b/include/os/os.h @@ -21,17 +21,52 @@ extern "C" { #endif #include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include +#include #include #include #include #include +#include +#include +#include #include +#include +#include +#include +#include #include "osAtomic.h" #include "osDef.h" +#include "osDir.h" #include "osEndian.h" +#include "osEnv.h" +#include "osFile.h" +#include "osLz4.h" +#include "osMath.h" #include "osMemory.h" +#include "osRand.h" +#include "osSemaphore.h" +#include "osSignal.h" +#include "osSleep.h" +#include "osSocket.h" +#include "osString.h" +#include "osSysinfo.h" +#include "osSystem.h" +#include "osThread.h" +#include "osTime.h" +#include "osTimer.h" + +void osInit(); #ifdef __cplusplus } diff --git a/include/os/osAtomic.h b/include/os/osAtomic.h index 7affa444ee44c8c7e6de209f2b4ea23238c26d3b..bd3d372fc0cfb0138fc63b870c427179c4cac7db 100644 --- a/include/os/osAtomic.h +++ b/include/os/osAtomic.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_OS_ATOMIC_H -#define TDENGINE_OS_ATOMIC_H +#ifndef _TD_OS_ATOMIC_H_ +#define _TD_OS_ATOMIC_H_ #ifdef __cplusplus extern "C" { @@ -355,4 +355,4 @@ extern "C" { } #endif -#endif +#endif /*_TD_OS_ATOMIC_H_*/ diff --git a/include/os/osDef.h b/include/os/osDef.h index a68e50e3b516aa0bf2791cca71553f7654f84e91..2b3678ac6785f2f41eded008a80dda62a9f93362 100644 --- a/include/os/osDef.h +++ b/include/os/osDef.h @@ -20,12 +20,164 @@ extern "C" { #endif +#if defined(_TD_DARWIN_64) + // specific + typedef int(*__compar_fn_t)(const void *, const void *); + + // for send function in tsocket.c + #if defined(MSG_NOSIGNAL) + #undef MSG_NOSIGNAL + #endif + + #define MSG_NOSIGNAL 0 + + #define SO_NO_CHECK 0x1234 + #define SOL_TCP 0x1234 + #define TCP_KEEPIDLE 0x1234 + + #ifndef PTHREAD_MUTEX_RECURSIVE_NP + #define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE + #endif +#endif + +#if defined(_ALPINE) + typedef int(*__compar_fn_t)(const void *, const void *); + void error (int, int, const char *); + #ifndef PTHREAD_MUTEX_RECURSIVE_NP + #define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE + #endif +#endif + + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + char *stpcpy (char *dest, const char *src); + char *stpncpy (char *dest, const char *src, size_t n); + + // specific + typedef int (*__compar_fn_t)(const void *, const void *); + #define ssize_t int + #define bzero(ptr, size) memset((ptr), 0, (size)) + #define strcasecmp _stricmp + #define strncasecmp _strnicmp + #define wcsncasecmp _wcsnicmp + #define strtok_r strtok_s + #define snprintf _snprintf + #define in_addr_t unsigned long + #define socklen_t int + + struct tm *localtime_r(const time_t *timep, struct tm *result); + char * strptime(const char *buf, const char *fmt, struct tm *tm); + char * strsep(char **stringp, const char *delim); + char * getpass(const char *prefix); + char * strndup(const char *s, size_t n); + +#endif + +#define POINTER_SHIFT(p, b) ((void *)((char *)(p) + (b))) +#define POINTER_DISTANCE(p1, p2) ((char *)(p1) - (char *)(p2)) + +#ifndef NDEBUG +#define ASSERT(x) assert(x) +#else +#define ASSERT(x) +#endif + +#ifndef UNUSED +#define UNUSED(x) ((void)(x)) +#endif + +#ifdef UNUSED_FUNC +#undefine UNUSED_FUNC +#endif + +#ifdef UNUSED_PARAM +#undef UNUSED_PARAM +#endif + +#if defined(__GNUC__) +#define UNUSED_PARAM(x) _UNUSED##x __attribute__((unused)) +#define UNUSED_FUNC __attribute__((unused)) +#else +#define UNUSED_PARAM(x) x +#define UNUSED_FUNC +#endif + +#ifdef tListLen +#undefine tListLen +#endif +#define tListLen(x) (sizeof(x) / sizeof((x)[0])) + #if defined(__GNUC__) #define FORCE_INLINE inline __attribute__((always_inline)) #else #define FORCE_INLINE #endif +#define DEFAULT_UNICODE_ENCODEC "UCS-4LE" + +#define DEFAULT_COMP(x, y) \ + do { \ + if ((x) == (y)) { \ + return 0; \ + } else { \ + return (x) < (y) ? -1 : 1; \ + } \ + } while (0) + +#define DEFAULT_DOUBLE_COMP(x, y) \ + do { \ + if (isnan(x) && isnan(y)) { return 0; } \ + if (isnan(x)) { return -1; } \ + if (isnan(y)) { return 1; } \ + if ((x) == (y)) { \ + return 0; \ + } else { \ + return (x) < (y) ? -1 : 1; \ + } \ + } while (0) + +#define DEFAULT_FLOAT_COMP(x, y) DEFAULT_DOUBLE_COMP(x, y) + +#define ALIGN_NUM(n, align) (((n) + ((align)-1)) & (~((align)-1))) + +// align to 8bytes +#define ALIGN8(n) ALIGN_NUM(n, 8) + +#undef threadlocal +#ifdef _ISOC11_SOURCE + #define threadlocal _Thread_local +#elif defined(__APPLE__) + #define threadlocal __thread +#elif defined(__GNUC__) && !defined(threadlocal) + #define threadlocal __thread +#else + #define threadlocal __declspec( thread ) +#endif + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + #define PRIzu "ld" +#else + #define PRIzu "zu" +#endif + +#if defined(_TD_LINUX_64) || defined(_TD_LINUX_32) || defined(_TD_MIPS_64) || defined(_TD_ARM_32) || defined(_TD_ARM_64) || defined(_TD_DARWIN_64) + #if defined(_TD_DARWIN_64) + // MacOS + #if !defined(_GNU_SOURCE) + #define setThreadName(name) do { pthread_setname_np((name)); } while (0) + #else + // pthread_setname_np not defined + #define setThreadName(name) + #endif + #else + // Linux, length of name must <= 16 (the last '\0' included) + #define setThreadName(name) do { prctl(PR_SET_NAME, (name)); } while (0) + #endif +#else + // Windows + #define setThreadName(name) +#endif + #ifdef __cplusplus } #endif diff --git a/src/dnode/inc/dnodeStep.h b/include/os/osDir.h similarity index 67% rename from src/dnode/inc/dnodeStep.h rename to include/os/osDir.h index e181e19c4633aa6c844d92db15cee1a9930a41ac..32733753a8a1de2dcdd5fe1a0244533aae4dc19c 100644 --- a/src/dnode/inc/dnodeStep.h +++ b/include/os/osDir.h @@ -13,21 +13,22 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_DNODE_STEP_H -#define TDENGINE_DNODE_STEP_H +#ifndef _TD_OS_DIR_H_ +#define _TD_OS_DIR_H_ #ifdef __cplusplus extern "C" { #endif -#include "dnodeInt.h" -int32_t dnodeStepInit(SStep *pSteps, int32_t stepSize); -void dnodeStepCleanup(SStep *pSteps, int32_t stepSize); -void dnodeReportStep(char *name, char *desc, int8_t finished); -void dnodeSendStartupStep(SRpcMsg *pMsg); +void taosRemoveDir(char *dirname); +bool taosDirExist(char *dirname); +bool taosMkDir(char *dirname); +void taosRemoveOldFiles(char *dirname, int32_t keepDays); +bool taosExpandDir(char *dirname, char *outname, int32_t maxlen); +bool taosRealPath(char *dirname, int32_t maxlen); #ifdef __cplusplus } #endif -#endif \ No newline at end of file +#endif /*_TD_OS_DIR_H_*/ diff --git a/src/dnode/inc/dnodeMain.h b/include/os/osEnv.h similarity index 76% rename from src/dnode/inc/dnodeMain.h rename to include/os/osEnv.h index ca79d53afdc0b211f3903b9323f7e567d0045093..b857670214699c52975fff1020e548f6b40242a6 100644 --- a/src/dnode/inc/dnodeMain.h +++ b/include/os/osEnv.h @@ -13,19 +13,22 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_DNODE_MAIN_H -#define TDENGINE_DNODE_MAIN_H +#ifndef _TD_OS_ENV_H_ +#define _TD_OS_ENV_H_ #ifdef __cplusplus extern "C" { #endif -#include "dnodeInt.h" -int32_t dnodeInitSystem(); -void dnodeCleanUpSystem(); +extern char tsOsName[]; +extern char tsDnodeDir[]; +extern char tsDataDir[]; +extern char tsLogDir[]; +extern char tsScriptDir[]; +extern char configDir[]; #ifdef __cplusplus } #endif -#endif +#endif /*_TD_OS_ENV_H_*/ \ No newline at end of file diff --git a/include/os/osFile.h b/include/os/osFile.h new file mode 100644 index 0000000000000000000000000000000000000000..8d03759d820e5ed24ab286fbb5e8b9ddcf81576e --- /dev/null +++ b/include/os/osFile.h @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef _TD_OS_FILE_H_ +#define _TD_OS_FILE_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "osSocket.h" + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +typedef int32_t FileFd; +#else +typedef int32_t FileFd; +#endif + +#define FD_INITIALIZER ((int32_t)-1) + +#ifndef PATH_MAX +#define PATH_MAX 256 +#endif + +int32_t taosLockFile(FileFd fd); +int32_t taosUnLockFile(FileFd fd); + +int32_t taosUmaskFile(FileFd fd); + +int32_t taosStatFile(const char *path, int64_t *size, int32_t *mtime); +int32_t taosFStatFile(FileFd fd, int64_t *size, int32_t *mtime); + +FileFd taosOpenFileWrite(const char *path); +FileFd taosOpenFileCreateWrite(const char *path); +FileFd taosOpenFileCreateWriteTrunc(const char *path); +FileFd taosOpenFileCreateWriteAppend(const char *path); +FileFd taosOpenFileRead(const char *path); +FileFd taosOpenFileReadWrite(const char *path); + +int64_t taosLSeekFile(FileFd fd, int64_t offset, int32_t whence); +int32_t taosFtruncateFile(FileFd fd, int64_t length); +int32_t taosFsyncFile(FileFd fd); + +int64_t taosReadFile(FileFd fd, void *buf, int64_t count); +int64_t taosWriteFile(FileFd fd, void *buf, int64_t count); + +void taosCloseFile(FileFd fd); + +int32_t taosRenameFile(char *oldName, char *newName); +int64_t taosCopyFile(char *from, char *to); + +void taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, char *dstPath); + +int64_t taosSendFile(SocketFd dfd, FileFd sfd, int64_t *offset, int64_t size); +int64_t taosFSendFile(FILE *outfile, FILE *infile, int64_t *offset, int64_t size); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_OS_FILE_H_*/ diff --git a/src/os/inc/osLz4.h b/include/os/osLz4.h similarity index 95% rename from src/os/inc/osLz4.h rename to include/os/osLz4.h index 8363861e0348141243c49c9df72b4aac7b6c199d..09aaa553acc66fe71bd4e34576cf6911799ecbd4 100644 --- a/src/os/inc/osLz4.h +++ b/include/os/osLz4.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_OS_LZ4_H -#define TDENGINE_OS_LZ4_H +#ifndef _TD_OS_LZ4_H_ +#define _TD_OS_LZ4_H_ #ifdef __cplusplus extern "C" { @@ -46,4 +46,4 @@ extern "C" { } #endif -#endif +#endif /*_TD_OS_LZ4_H_*/ diff --git a/src/os/inc/osMath.h b/include/os/osMath.h similarity index 95% rename from src/os/inc/osMath.h rename to include/os/osMath.h index 65e8a1f6feaf32bff7f413b7085fc64835ba70fb..440d3bc787576c418edbf1b38c8e85d377557370 100644 --- a/src/os/inc/osMath.h +++ b/include/os/osMath.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_OS_MATH_H -#define TDENGINE_OS_MATH_H +#ifndef _TD_OS_MATH_H_ +#define _TD_OS_MATH_H_ #ifdef __cplusplus extern "C" { @@ -62,4 +62,4 @@ extern "C" { } #endif -#endif +#endif /*_TD_OS_MATH_H_*/ diff --git a/src/os/inc/osRand.h b/include/os/osRand.h similarity index 91% rename from src/os/inc/osRand.h rename to include/os/osRand.h index e08768c2cc6b379877fb8be7d3541c13bc431f98..422ea92a71feabfae497e319879704af89bd655d 100644 --- a/src/os/inc/osRand.h +++ b/include/os/osRand.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_OS_RAND_H -#define TDENGINE_OS_RAND_H +#ifndef _TD_OS_RAND_H_ +#define _TD_OS_RAND_H_ #ifdef __cplusplus extern "C" { @@ -28,4 +28,4 @@ uint32_t taosSafeRand(void); } #endif -#endif +#endif /*_TD_OS_RAND_H_*/ diff --git a/src/os/inc/osSemaphore.h b/include/os/osSemaphore.h similarity index 95% rename from src/os/inc/osSemaphore.h rename to include/os/osSemaphore.h index 10d14700e013f66e6d98208f0e65fe1ca5fc3874..86a9f10819f11d54cd860bfbc01ebb6ba9d34129 100644 --- a/src/os/inc/osSemaphore.h +++ b/include/os/osSemaphore.h @@ -13,13 +13,15 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_OS_SEMAPHORE_H -#define TDENGINE_OS_SEMAPHORE_H +#ifndef _TD_OS_SEMPHONE_H_ +#define _TD_OS_SEMPHONE_H_ #ifdef __cplusplus extern "C" { #endif +#include + #if defined (_TD_DARWIN_64) typedef struct tsem_s *tsem_t; int tsem_init(tsem_t *sem, int pshared, unsigned int value); @@ -61,4 +63,4 @@ int32_t taosGetCurrentAPPName(char* name, int32_t* len); } #endif -#endif +#endif /*_TD_OS_SEMPHONE_H_*/ diff --git a/src/os/inc/osSignal.h b/include/os/osSignal.h similarity index 91% rename from src/os/inc/osSignal.h rename to include/os/osSignal.h index eca1d3b3f6b78a773822e5f2acb652493b5d262f..ab4b6fa242cce2a7250ec6e7f3c669d62dd0ac21 100644 --- a/src/os/inc/osSignal.h +++ b/include/os/osSignal.h @@ -13,15 +13,13 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_OS_SIGNAL_H -#define TDENGINE_OS_SIGNAL_H +#ifndef _TD_OS_SIGNAL_H_ +#define _TD_OS_SIGNAL_H_ #ifdef __cplusplus extern "C" { #endif -#include - #ifndef SIGALRM #define SIGALRM 1234 #endif @@ -55,4 +53,4 @@ void taosDflSignal(int32_t signum); } #endif -#endif // TDENGINE_OS_SIGNAL_H +#endif /*_TD_OS_SIGNAL_H_*/ diff --git a/src/os/inc/osSleep.h b/include/os/osSleep.h similarity index 90% rename from src/os/inc/osSleep.h rename to include/os/osSleep.h index e42da8d5a64bbc484b15beea19433a710578ff3f..686bdd292e376f55182caea52189a678bfdc3b14 100644 --- a/src/os/inc/osSleep.h +++ b/include/os/osSleep.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_OS_SLEEP_H -#define TDENGINE_OS_SLEEP_H +#ifndef _TD_OS_SLEEP_H_ +#define _TD_OS_SLEEP_H_ #ifdef __cplusplus extern "C" { @@ -26,4 +26,4 @@ void taosMsleep(int32_t ms); } #endif -#endif \ No newline at end of file +#endif /*_TD_OS_SLEEP_H_*/ \ No newline at end of file diff --git a/include/os/osSocket.h b/include/os/osSocket.h new file mode 100644 index 0000000000000000000000000000000000000000..d76b286d651b0124fd5db4fd4018f566f498a0d0 --- /dev/null +++ b/include/os/osSocket.h @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef _TD_OS_SOCKET_H_ +#define _TD_OS_SOCKET_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + #include "winsock2.h" + #include + #include + #include +#else + #include + #include +#endif + +#define TAOS_EPOLL_WAIT_TIME 500 +typedef int32_t SOCKET; +typedef SOCKET EpollFd; +#define EpollClose(pollFd) taosCloseSocket(pollFd) + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +typedef SOCKET SocketFd; +#else +typedef int32_t SocketFd; +#endif + +int32_t taosSendto(SocketFd fd, void * msg, int len, unsigned int flags, const struct sockaddr * to, int tolen); +int32_t taosWriteSocket(SocketFd fd, void *msg, int len); +int32_t taosReadSocket(SocketFd fd, void *msg, int len); +int32_t taosCloseSocketNoCheck(SocketFd fd); +int32_t taosCloseSocket(SocketFd fd); +void taosShutDownSocketRD(SOCKET fd); +void taosShutDownSocketWR(SOCKET fd); +int32_t taosSetNonblocking(SOCKET sock, int32_t on); +void taosIgnSIGPIPE(); +void taosBlockSIGPIPE(); +void taosSetMaskSIGPIPE(); +int32_t taosSetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *optval, int32_t optlen); +int32_t taosGetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *optval, int32_t *optlen); + +uint32_t taosInetAddr(char *ipAddr); +const char *taosInetNtoa(struct in_addr ipInt); + +#if (defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)) + #define htobe64 htonll + #if defined(_TD_GO_DLL_) + uint64_t htonll(uint64_t val); + #endif +#endif + +#if defined(_TD_DARWIN_64) + #define htobe64 htonll +#endif + +int32_t taosReadn(SOCKET sock, char *buffer, int32_t len); +int32_t taosWriteMsg(SOCKET fd, void *ptr, int32_t nbytes); +int32_t taosReadMsg(SOCKET fd, void *ptr, int32_t nbytes); +int32_t taosNonblockwrite(SOCKET fd, char *ptr, int32_t nbytes); +int64_t taosCopyFds(SOCKET sfd, int32_t dfd, int64_t len); +int32_t taosSetNonblocking(SOCKET sock, int32_t on); + +SOCKET taosOpenUdpSocket(uint32_t localIp, uint16_t localPort); +SOCKET taosOpenTcpClientSocket(uint32_t ip, uint16_t port, uint32_t localIp); +SOCKET taosOpenTcpServerSocket(uint32_t ip, uint16_t port); +int32_t taosKeepTcpAlive(SOCKET sockFd); + +int32_t taosGetFqdn(char *); +uint32_t taosGetIpv4FromFqdn(const char *); +void tinet_ntoa(char *ipstr, uint32_t ip); +uint32_t ip2uint(const char *const ip_addr); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_OS_SOCKET_H_*/ diff --git a/src/os/inc/osString.h b/include/os/osString.h similarity index 94% rename from src/os/inc/osString.h rename to include/os/osString.h index 33f858485c4291f7c7c9098685c8c70a02799fe6..358324075fab292657a207c37c3d1292a137b0d9 100644 --- a/src/os/inc/osString.h +++ b/include/os/osString.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_OS_STRING_H -#define TDENGINE_OS_STRING_H +#ifndef _TD_OS_STRING_H_ +#define _TD_OS_STRING_H_ #ifdef __cplusplus extern "C" { @@ -43,7 +43,7 @@ int64_t taosStr2int64(char *str); // USE_LIBICONV int32_t taosUcs4ToMbs(void *ucs4, int32_t ucs4_max_len, char *mbs); bool taosMbsToUcs4(char *mbs, size_t mbs_len, char *ucs4, int32_t ucs4_max_len, int32_t *len); -int32_t tasoUcs4Compare(void *f1_ucs4, void *f2_ucs4, int32_t bytes); +int32_t tasoUcs4Compare(void *f1_ucs4, void *f2_ucs4, int32_t bytes, int8_t ncharSize); bool taosValidateEncodec(const char *encodec); char * taosCharsetReplace(char *charsetstr); @@ -51,4 +51,4 @@ char * taosCharsetReplace(char *charsetstr); } #endif -#endif +#endif /*_TD_OS_STRING_H_*/ diff --git a/include/os/osSysinfo.h b/include/os/osSysinfo.h new file mode 100644 index 0000000000000000000000000000000000000000..a3919890bdc241be47ad7793c245ba286a29f54e --- /dev/null +++ b/include/os/osSysinfo.h @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef _TD_OS_SYSINFO_H_ +#define _TD_OS_SYSINFO_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#define TSDB_LOCALE_LEN 64 +#define TSDB_TIMEZONE_LEN 96 + +extern int64_t tsPageSize; +extern int64_t tsOpenMax; +extern int64_t tsStreamMax; +extern int32_t tsNumOfCores; +extern int32_t tsTotalMemoryMB; +extern char tsTimezone[]; +extern char tsLocale[]; +extern char tsCharset[]; // default encode string + +typedef struct { + int64_t tsize; + int64_t used; + int64_t avail; +} SysDiskSize; + +int32_t taosGetDiskSize(char *dataDir, SysDiskSize *diskSize); +int32_t taosGetCpuCores(); +void taosGetSystemInfo(); +bool taosReadProcIO(int64_t *rchars, int64_t *wchars); +bool taosGetProcIO(float *readKB, float *writeKB); +bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes); +bool taosGetBandSpeed(float *bandSpeedKb); +void taosGetDisk(); +bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage); +bool taosGetProcMemory(float *memoryUsedMB); +bool taosGetSysMemory(float *memoryUsedMB); +void taosPrintOsInfo(); +int taosSystem(const char *cmd); +void taosKillSystem(); +bool taosGetSystemUid(char *uid, int32_t uidlen); +char * taosGetCmdlineByPID(int pid); +void taosSetCoreDump(bool enable); + +typedef struct { + const char *sysname; + const char *nodename; + const char *release; + const char *version; + const char *machine; +} SysNameInfo; + +SysNameInfo taosGetSysNameInfo(); + +int64_t taosGetPid(); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/os/inc/osSystem.h b/include/os/osSystem.h similarity index 79% rename from src/os/inc/osSystem.h rename to include/os/osSystem.h index 4b7925074075b7eef04d2281d3ba7649740bae1e..8554c4ad12f8737aac63a1815b63acf9cf0f69ff 100644 --- a/src/os/inc/osSystem.h +++ b/include/os/osSystem.h @@ -13,21 +13,21 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_OS_SYSTEM_H -#define TDENGINE_OS_SYSTEM_H +#ifndef _TD_OS_SYSTEM_H_ +#define _TD_OS_SYSTEM_H_ #ifdef __cplusplus extern "C" { #endif -void* taosLoadDll(const char *filename); +void* taosLoadDll(const char* filename); void* taosLoadSym(void* handle, char* name); -void taosCloseDll(void *handle); +void taosCloseDll(void* handle); -int taosSetConsoleEcho(bool on); +int32_t taosSetConsoleEcho(bool on); #ifdef __cplusplus } #endif -#endif +#endif /*_TD_OS_SYSTEM_H_*/ diff --git a/src/inc/module.h b/include/os/osThread.h similarity index 87% rename from src/inc/module.h rename to include/os/osThread.h index b9b64c493ec5777ddd6981ebf3212ffd0ff0114d..79834dc9a59f00c0aefa452c2427ea45ac35c478 100644 --- a/src/inc/module.h +++ b/include/os/osThread.h @@ -13,18 +13,17 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_MODULE -#define TDENGINE_MODULE +#ifndef _TD_OS_THREAD_H_ +#define _TD_OS_THREAD_H_ #ifdef __cplusplus extern "C" { #endif -int32_t moduleStart(); -void moduleStop(); +#include #ifdef __cplusplus } #endif -#endif +#endif /*_TD_OS_THREAD_H_*/ diff --git a/include/os/osTime.h b/include/os/osTime.h new file mode 100644 index 0000000000000000000000000000000000000000..62ecd5477f4d27c0214140f014aa95fc8b5ef605 --- /dev/null +++ b/include/os/osTime.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef _TD_OS_TIME_H_ +#define _TD_OS_TIME_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + #ifdef _TD_GO_DLL_ + #define MILLISECOND_PER_SECOND (1000LL) + #else + #define MILLISECOND_PER_SECOND (1000i64) + #endif +#else + #define MILLISECOND_PER_SECOND ((int64_t)1000L) +#endif + +#define MILLISECOND_PER_MINUTE (MILLISECOND_PER_SECOND * 60) +#define MILLISECOND_PER_HOUR (MILLISECOND_PER_MINUTE * 60) +#define MILLISECOND_PER_DAY (MILLISECOND_PER_HOUR * 24) +#define MILLISECOND_PER_WEEK (MILLISECOND_PER_DAY * 7) + +int32_t taosGetTimeOfDay(struct timeval *tv); + +//@return timestamp in second +int32_t taosGetTimestampSec(); + +//@return timestamp in millisecond +static FORCE_INLINE int64_t taosGetTimestampMs() { + struct timeval systemTime; + taosGetTimeOfDay(&systemTime); + return (int64_t)systemTime.tv_sec * 1000L + (int64_t)systemTime.tv_usec / 1000; +} + +//@return timestamp in microsecond +static FORCE_INLINE int64_t taosGetTimestampUs() { + struct timeval systemTime; + taosGetTimeOfDay(&systemTime); + return (int64_t)systemTime.tv_sec * 1000000L + (int64_t)systemTime.tv_usec; +} + +//@return timestamp in nanosecond +static FORCE_INLINE int64_t taosGetTimestampNs() { + struct timespec systemTime = {0}; + clock_gettime(CLOCK_REALTIME, &systemTime); + return (int64_t)systemTime.tv_sec * 1000000000L + (int64_t)systemTime.tv_nsec; +} + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_OS_TIME_H_*/ diff --git a/src/os/inc/osTimer.h b/include/os/osTimer.h similarity index 73% rename from src/os/inc/osTimer.h rename to include/os/osTimer.h index 72da19cd899a708dc36c91ac25159995fe0da51c..4b5db895a2f4d190138c1c79c873336f163382e2 100644 --- a/src/os/inc/osTimer.h +++ b/include/os/osTimer.h @@ -13,18 +13,22 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_OS_TIMER_H -#define TDENGINE_OS_TIMER_H +#ifndef _TD_OS_TIMER_H_ +#define _TD_OS_TIMER_H_ #ifdef __cplusplus extern "C" { #endif -int taosInitTimer(void (*callback)(int), int ms); -void taosUninitTimer(); +#define MSECONDS_PER_TICK 5 + +int32_t taosInitTimer(void (*callback)(int32_t), int32_t ms); +void taosUninitTimer(); +int64_t taosGetMonotonicMs(); +const char *taosMonotonicInit(); #ifdef __cplusplus } #endif -#endif +#endif /*_TD_OS_TIMER_H_*/ diff --git a/include/server/dnode/dnode.h b/include/server/dnode/dnode.h index fae60495e9a230a46567c5b7ff32a3152c6bb323..d7aaa0e00801eadaac2ec18089adb260037e59fe 100644 --- a/include/server/dnode/dnode.h +++ b/include/server/dnode/dnode.h @@ -20,56 +20,55 @@ extern "C" { #endif -struct SRpcMsg; struct SRpcEpSet; - +struct SRpcMsg; /** - * Initialize and start the dnode module - * - * @return Error Code + * Initialize and start the dnode module. + * + * @return Error code. */ int32_t dnodeInit(); /** - * Stop and cleanup dnode module + * Stop and cleanup dnode module. */ void dnodeCleanup(); /** * Send messages to other dnodes, such as create vnode message. * - * @param epSet The endpoint list of the dnodes. - * @param rpcMsg Message to be sent. + * @param epSet, the endpoint list of the dnodes. + * @param rpcMsg, message to be sent. */ -void dnodeSendMsgToDnode(SRpcEpSet *epSet, SRpcMsg *rpcMsg); +void dnodeSendMsgToDnode(struct SRpcEpSet *epSet, struct SRpcMsg *rpcMsg); /** * Send messages to mnode, such as config message. * - * @param rpcMsg Message to be sent. + * @param rpcMsg, message to be sent. */ -void dnodeSendMsgToMnode(SRpcMsg *rpcMsg); +void dnodeSendMsgToMnode(struct SRpcMsg *rpcMsg); /** - * Get the corresponding endpoint information from dnodeId. + * Send redirect message to dnode or shell. * - * @param dnodeId - * @param ep The endpoint of dnode - * @param fqdn The fqdn of dnode - * @param port The port of dnode + * @param rpcMsg, message to be sent. + * @param forShell, used to identify whether to send to shell or dnode. */ -void dnodeGetDnodeEp(int32_t dnodeId, char *ep, char *fqdn, uint16_t *port); +void dnodeSendRedirectMsg(struct SRpcMsg *rpcMsg, bool forShell); /** - * Report to dnode the start-up steps of other modules + * Get the corresponding endpoint information from dnodeId. * - * @param name Name of the start-up phase. - * @param desc Description of the start-up phase. + * @param dnodeId, the id ot dnode. + * @param ep, the endpoint of dnode. + * @param fqdn, the fqdn of dnode. + * @param port, the port of dnode. */ -void dnodeReportStartup(char *name, char *desc); +void dnodeGetDnodeEp(int32_t dnodeId, char *ep, char *fqdn, uint16_t *port); #ifdef __cplusplus } #endif -#endif /*_TD_DNODE_H_*/ \ No newline at end of file +#endif /*_TD_DNODE_H_*/ diff --git a/include/server/mnode/mnode.h b/include/server/mnode/mnode.h index 0d2fd00ba1e85daf9c10c19c5b464e3b08bc0605..7ea50a92cefe8869c00c84a85fe7b9c90347c168 100644 --- a/include/server/mnode/mnode.h +++ b/include/server/mnode/mnode.h @@ -20,49 +20,125 @@ extern "C" { #endif -struct SRpcMsg; +typedef struct { + /** + * Send messages to other dnodes, such as create vnode message. + * + * @param epSet, the endpoint list of the dnodes. + * @param rpcMsg, message to be sent. + */ + void (*SendMsgToDnode)(struct SRpcEpSet *epSet, struct SRpcMsg *rpcMsg); + + /** + * Send messages to mnode, such as config message. + * + * @param rpcMsg, message to be sent. + */ + void (*SendMsgToMnode)(struct SRpcMsg *rpcMsg); + + /** + * Send redirect message to dnode or shell. + * + * @param rpcMsg, message to be sent. + * @param forShell, used to identify whether to send to shell or dnode. + */ + void (*SendRedirectMsg)(struct SRpcMsg *rpcMsg, bool forShell); + + /** + * Get the corresponding endpoint information from dnodeId. + * + * @param dnode, the instance of dDnode module. + * @param dnodeId, the id ot dnode. + * @param ep, the endpoint of dnode. + * @param fqdn, the fqdn of dnode. + * @param port, the port of dnode. + */ + void (*GetDnodeEp)(int32_t dnodeId, char *ep, char *fqdn, uint16_t *port); + +} SMnodeFp; + +typedef struct { + SMnodeFp fp; + char clusterId[TSDB_CLUSTER_ID_LEN]; + int32_t dnodeId; +} SMnodePara; + +/** + * Initialize and start mnode module. + * + * @param para, initialization parameters. + * @return Error code. + */ +int32_t mnodeInit(SMnodePara para); /** - * Deploy Mnode instances in Dnode. - * + * Stop and cleanup mnode module. + */ +void mnodeCleanup(); + +/** + * Deploy mnode instances in dnode. + * + * @param minfos, server information used to deploy the mnode instance. * @return Error Code. */ -int32_t mnodeDeploy(); +int32_t mnodeDeploy(struct SMInfos *minfos); /** - * Delete the Mnode instance deployed in Dnode. + * Delete the mnode instance deployed in dnode. */ void mnodeUnDeploy(); /** - * Start Mnode service. - * - * @return Error Code. + * Whether the mnode is in service. + * + * @return Server status. */ -int32_t mnodeStart(); +bool mnodeIsServing(); + +typedef struct { + int64_t numOfDnode; + int64_t numOfMnode; + int64_t numOfVgroup; + int64_t numOfDatabase; + int64_t numOfSuperTable; + int64_t numOfChildTable; + int64_t numOfColumn; + int64_t totalPoints; + int64_t totalStorage; + int64_t compStorage; +} SMnodeStat; /** - * Stop Mnode service. + * Get the statistical information of Mnode. + * + * @param stat, statistical information. + * @return Error Code. */ -int32_t mnodeStop(); +int32_t mnodeGetStatistics(SMnodeStat *stat); /** - * Interface for processing messages. - * - * @param pMsg Message to be processed. - * @return Error code + * Get the auth information. + * + * @param user, username. + * @param spi, security parameter index. + * @param encrypt, encrypt algorithm. + * @param secret, key for authentication. + * @param ckey, ciphering key. + * @return Error Code. */ -int32_t mnodeProcessMsg(SRpcMsg *pMsg); +int32_t mnodeRetriveAuth(char *user, char *spi, char *encrypt, char *secret, char *ckey); /** - * Whether the Mnode is in service. - * - * @return Server status. + * Interface for processing messages. + * + * @param rpcMsg, message to be processed. + * @return Error code. */ -bool mnodeIsServing(); +void mnodeProcessMsg(SRpcMsg *rpcMsg); #ifdef __cplusplus } #endif -#endif /*_TD_MNODE_H_*/ \ No newline at end of file +#endif /*_TD_MNODE_H_*/ diff --git a/include/server/qnode/qnode.h b/include/server/qnode/qnode.h index 00daf2b0514d077766001c436d248527bb06a7d6..65779b209938bc9766f2d0d11881c2354d0c5092 100644 --- a/include/server/qnode/qnode.h +++ b/include/server/qnode/qnode.h @@ -20,6 +20,94 @@ extern "C" { #endif + +typedef struct { + uint64_t numOfStartTask; + uint64_t numOfStopTask; + uint64_t numOfRecvedFetch; + uint64_t numOfSentHb; + uint64_t numOfSentFetch; + uint64_t numOfTaskInQueue; + uint64_t numOfFetchInQueue; + uint64_t numOfErrors; +} SQnodeStat; + +/* start Task msg */ +typedef struct { + uint32_t schedulerIp; + uint16_t schedulerPort; + int64_t taskId; + int64_t queryId; + uint32_t srcIp; + uint16_t srcPort; +} SQnodeStartTaskMsg; + +/* stop Task msg */ +typedef struct { + int64_t taskId; +} SQnodeStopTaskMsg; + +/* start/stop Task msg response */ +typedef struct { + int64_t taskId; + int32_t code; +} SQnodeTaskRespMsg; + +/* Task status msg */ +typedef struct { + int64_t taskId; + int32_t status; + int64_t queryId; +} SQnodeTaskStatusMsg; + +/* Qnode/Scheduler heartbeat msg */ +typedef struct { + int32_t status; + int32_t load; + +} SQnodeHeartbeatMsg; + +/* Qnode sent/received msg */ +typedef struct { + int8_t msgType; + int32_t msgLen; + char msg[]; +} SQnodeMsg; + + +/** + * Start one Qnode in Dnode. + * @return Error Code. + */ +int32_t qnodeStart(); + +/** + * Stop Qnode in Dnode. + * + * @param qnodeId Qnode ID to stop, -1 for all Qnodes. + */ +void qnodeStop(int64_t qnodeId); + + +/** + * Get the statistical information of Qnode + * + * @param qnodeId Qnode ID to get statistics, -1 for all + * @param stat Statistical information. + * @return Error Code. + */ +int32_t qnodeGetStatistics(int64_t qnodeId, SQnodeStat *stat); + +/** + * Interface for processing Qnode messages. + * + * @param pMsg Message to be processed. + * @return Error code + */ +void qnodeProcessReq(SRpcMsg *pMsg); + + + #ifdef __cplusplus } #endif diff --git a/include/server/vnode/meta/meta.h b/include/server/vnode/meta/meta.h index 259fd1fbd08264050f972d8f5a190a0d4d62a42f..4167dedde6e57f0d090117735fb0d1109597f53a 100644 --- a/include/server/vnode/meta/meta.h +++ b/include/server/vnode/meta/meta.h @@ -16,7 +16,7 @@ #ifndef _TD_META_H_ #define _TD_META_H_ -#include "taosMsg.h" +#include "taosmsg.h" #include "os.h" @@ -59,4 +59,4 @@ void metaQueryOptionsDestroy(SMetaQueryOptions *); } #endif -#endif /*_TD_META_H_*/ \ No newline at end of file +#endif /*_TD_META_H_*/ diff --git a/include/server/vnode/tq/tq.h b/include/server/vnode/tq/tq.h index 4c626a1e25af0ddcef4018766a06ff594b013740..91688e890df0c699de8f9e0ab4004d5bbd3da182 100644 --- a/include/server/vnode/tq/tq.h +++ b/include/server/vnode/tq/tq.h @@ -16,17 +16,35 @@ #ifndef _TD_TQ_H_ #define _TD_TQ_H_ +#include "os.h" + #ifdef __cplusplus extern "C" { #endif typedef struct STQ STQ; -int tqPushMsg(void *); -int tqCommit(STQ *pTQ); +STQ* tqInit(void* ref_func(void*), void* unref_func(void*)); +void tqCleanUp(STQ* pTq); + +//create persistent storage for meta info such as consuming offset +//return value > 0: cgId +//return value <= 0: error code +int tqCreateGroup(STQ*); +//create ring buffer in memory and load consuming offset +int tqOpenGroup(STQ*, int cgId); +//destroy ring buffer and persist consuming offset +int tqCloseGroup(STQ*, int cgId); +//delete persistent storage for meta info +int tqDropGroup(STQ*, int cgId); + +int tqPushMsg(STQ*, void *, int64_t version); +int tqCommit(STQ*); + +int tqHandleMsg(STQ*, void *msg); #ifdef __cplusplus } #endif -#endif /*_TD_TQ_H_*/ \ No newline at end of file +#endif /*_TD_TQ_H_*/ diff --git a/include/server/vnode/tsdb/tsdb.h b/include/server/vnode/tsdb/tsdb.h index 8b8877ff1d0e7e5fc6e0dfbb865e7d78f3aeaa4e..a25cb479855fefca2bacaba410b724160790de5e 100644 --- a/include/server/vnode/tsdb/tsdb.h +++ b/include/server/vnode/tsdb/tsdb.h @@ -17,7 +17,7 @@ #define _TD_TSDB_H_ #include "os.h" -#include "taosMsg.h" +#include "taosmsg.h" #ifdef __cplusplus extern "C" { @@ -82,4 +82,4 @@ void tsdbSnapshotDestroy(STsdbSnapshot *); } #endif -#endif /*_TD_TSDB_H_*/ \ No newline at end of file +#endif /*_TD_TSDB_H_*/ diff --git a/include/server/vnode/vnode.h b/include/server/vnode/vnode.h index 4a848add59ed5f68d292472531f0897a05f169cf..8fd4fd433f045042145ccfa2fd8339534aef5275 100644 --- a/include/server/vnode/vnode.h +++ b/include/server/vnode/vnode.h @@ -20,43 +20,87 @@ extern "C" { #endif -struct SRpcMsg; +typedef struct { + /** + * Send messages to other dnodes, such as create vnode message. + * + * @param epSet, the endpoint list of dnodes. + * @param rpcMsg, message to be sent. + */ + void (*SendMsgToDnode)(struct SRpcEpSet *epSet, struct SRpcMsg *rpcMsg); + + /** + * Send messages to mnode, such as config message. + * + * @param rpcMsg, message to be sent. + */ + void (*SendMsgToMnode)(struct SRpcMsg *rpcMsg); + + /** + * Get the corresponding endpoint information from dnodeId. + * + * @param dnodeId, the id ot dnode. + * @param ep, the endpoint of dnode. + * @param fqdn, the fqdn of dnode. + * @param port, the port of dnode. + */ + void (*GetDnodeEp)(int32_t dnodeId, char *ep, char *fqdn, uint16_t *port); + +} SVnodeFp; + +typedef struct { + SVnodeFp fp; +} SVnodePara; /** - * Start Initialize Vnode module. + * Start initialize vnode module. * - * @return Error Code. + * @param para, initialization parameters. + * @return Error code. */ -int32_t vnodeInit(); - +int32_t vnodeInit(SVnodePara para); + /** - * Cleanup Vnode module. + * Cleanup vnode module. */ void vnodeCleanup(); - + typedef struct { - int64_t queryMsgCount; - int64_t writeMsgCount; + int32_t unused; } SVnodeStat; - + /** - * Get the statistical information of Vnode + * Get the statistical information of vnode. * - * @param stat Statistical information. + * @param stat, statistical information. * @return Error Code. */ int32_t vnodeGetStatistics(SVnodeStat *stat); -/** +/** + * Get the status of all vnodes. + * + * @param status, status msg. + */ +void vnodeGetStatus(struct SStatusMsg *status); + +/** + * Set access permissions for all vnodes. + * + * @param access, access permissions of vnodes. + * @param numOfVnodes, the size of vnodes. + */ +void vnodeSetAccess(struct SVgroupAccess *access, int32_t numOfVnodes); + +/** * Interface for processing messages. * - * @param pMsg Message to be processed. - * @return Error code + * @param msg, message to be processed. */ -int32_t vnodeProcessMsg(SRpcMsg *pMsg); +void vnodeProcessMsg(SRpcMsg *msg); #ifdef __cplusplus } #endif -#endif /*_TD_VNODE_H_*/ \ No newline at end of file +#endif /*_TD_VNODE_H_*/ diff --git a/src/util/inc/exception.h b/include/util/exception.h similarity index 96% rename from src/util/inc/exception.h rename to include/util/exception.h index 38f94bfe283e41e4418ed6ab1673efb82acc44a0..8012e3431591099a645373092fb34ac0fb66ea77 100644 --- a/src/util/inc/exception.h +++ b/include/util/exception.h @@ -13,13 +13,10 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_EXCEPTION_H -#define TDENGINE_EXCEPTION_H +#ifndef _TD_UTIL_EXCEPTION_H +#define _TD_UTIL_EXCEPTION_H -#include -#include -#include -#include +#include "os.h" #ifdef __cplusplus extern "C" { @@ -125,4 +122,4 @@ void exceptionThrow( int32_t code ); } #endif -#endif +#endif /*_TD_UTIL_EXCEPTION_H*/ diff --git a/include/util/talgo.h b/include/util/talgo.h index 4aa54306052bfe224d81ac90f8310de7ac85f8eb..575f98b486f90b2eb4a830d0bb39868fcdc0c137 100644 --- a/include/util/talgo.h +++ b/include/util/talgo.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TALGO_H -#define TDENGINE_TALGO_H +#ifndef _TD_UTIL_TALGO_H +#define _TD_UTIL_TALGO_H #ifdef __cplusplus extern "C" { @@ -95,4 +95,4 @@ void taosheapsort(void *base, int32_t size, int32_t len, const void *parcompar, #ifdef __cplusplus } #endif -#endif // TDENGINE_TALGO_H +#endif /*_TD_UTIL_TALGO_H*/ diff --git a/src/inc/taoserror.h b/include/util/taoserror.h similarity index 99% rename from src/inc/taoserror.h rename to include/util/taoserror.h index d59b88c7e698b3e965b5923efdc760e0289f7250..c3f3e3b98b5414ae0f7b08e0b977b70764d8216f 100644 --- a/src/inc/taoserror.h +++ b/include/util/taoserror.h @@ -13,16 +13,13 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TAOSERROR_H -#define TDENGINE_TAOSERROR_H +#ifndef _TD_COMMON_TAOS_ERROR_H_ +#define _TD_COMMON_TAOS_ERROR_H_ #ifdef __cplusplus extern "C" { #endif -#include -#include - #define TAOS_DEF_ERROR_CODE(mod, code) ((int32_t)((0x80000000 | ((mod)<<16) | (code)))) #define TAOS_SYSTEM_ERROR(code) (0x80ff0000 | (code)) @@ -463,4 +460,4 @@ int32_t* taosGetErrno(); } #endif -#endif //TDENGINE_TAOSERROR_H +#endif /*_TD_COMMON_TAOS_ERROR_H_*/ diff --git a/include/util/tarray.h b/include/util/tarray.h index 2da74eac820e28206cb3e2b7cb6f2c4fb9f481b8..5807c980e0692449e2159973416762cff5e213c9 100644 --- a/include/util/tarray.h +++ b/include/util/tarray.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TAOSARRAY_H -#define TDENGINE_TAOSARRAY_H +#ifndef _TD_UTIL_ARRAY_H +#define _TD_UTIL_ARRAY_H #ifdef __cplusplus extern "C" { @@ -228,6 +228,4 @@ void taosArraySortPWithExt(SArray* pArray, __ext_compar_fn_t fn, const void *par } #endif - - -#endif // TDENGINE_TAOSARRAY_H +#endif /*_TD_UTIL_ARRAY_H*/ diff --git a/src/util/inc/tbuffer.h b/include/util/tbuffer.h similarity index 98% rename from src/util/inc/tbuffer.h rename to include/util/tbuffer.h index b19984b4be047108fd9848a2a7eddbac2c0b78a4..d1d403e996cb9add581255d0544dfa9ef6c450d9 100644 --- a/src/util/inc/tbuffer.h +++ b/include/util/tbuffer.h @@ -13,11 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TBUFFER_H -#define TDENGINE_TBUFFER_H - -#include -#include +#ifndef _TD_UTIL_BUFFER_H +#define _TD_UTIL_BUFFER_H #ifdef __cplusplus extern "C" { @@ -175,4 +172,4 @@ void tbufWriteDoubleAt( SBufferWriter* buf, size_t pos, double data ); } #endif -#endif +#endif /*_TD_UTIL_BUFFER_H*/ diff --git a/src/util/inc/tcache.h b/include/util/tcache.h similarity index 98% rename from src/util/inc/tcache.h rename to include/util/tcache.h index 40069d7d273caa14ce3b80467b25d68ea476fb75..11f9044d2838e8f73e9fcb01f6dc554b1de6aeff 100644 --- a/src/util/inc/tcache.h +++ b/include/util/tcache.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TCACHE_H -#define TDENGINE_TCACHE_H +#ifndef _TD_UTIL_CACHE_H +#define _TD_UTIL_CACHE_H #ifdef __cplusplus extern "C" { @@ -22,7 +22,7 @@ extern "C" { #include "os.h" #include "tlockfree.h" -#include "hash.h" +#include "thash.h" #if defined(_TD_ARM_32) #define TSDB_CACHE_PTR_KEY TSDB_DATA_TYPE_INT @@ -188,4 +188,4 @@ void taosStopCacheRefreshWorker(); } #endif -#endif // TDENGINE_TCACHE_H +#endif /*_TD_UTIL_CACHE_H*/ diff --git a/src/util/inc/tchecksum.h b/include/util/tchecksum.h similarity index 86% rename from src/util/inc/tchecksum.h rename to include/util/tchecksum.h index 12ca3a54432187ea5af0ac24208e81d5028359bb..c1907b0c464c657ee2aeebba7871d1dd786e7d40 100644 --- a/src/util/inc/tchecksum.h +++ b/include/util/tchecksum.h @@ -13,22 +13,14 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TCHECKSUM_H -#define TDENGINE_TCHECKSUM_H +#ifndef _TD_UTIL_CHECKSUM_H +#define _TD_UTIL_CHECKSUM_H #ifdef __cplusplus extern "C" { #endif -#include -#include -#include -#include -#include -#include -#include -#include - +#include "os.h" #include "tcrc32c.h" #include "tutil.h" @@ -64,4 +56,4 @@ static FORCE_INLINE int taosCheckChecksumWhole(const uint8_t *stream, uint32_t s } #endif -#endif // TDENGINE_TCHECKSUM_H +#endif /*_TD_UTIL_CHECKSUM_H*/ diff --git a/include/util/tcoding.h b/include/util/tcoding.h index 3ccf51c2dd774581693e0fc90fe7ffed217636d7..a11768a1b08c1655238c87e918281d16c5f9084c 100644 --- a/include/util/tcoding.h +++ b/include/util/tcoding.h @@ -12,14 +12,14 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef _TD_CODING_H_ -#define _TD_CODING_H_ +#ifndef _TD_UTIL_CODING_H +#define _TD_UTIL_CODING_H #ifdef __cplusplus extern "C" { #endif -#include "os.h " +#include "os.h" #define ENCODE_LIMIT (((uint8_t)1) << 7) #define ZIGZAGE(T, v) ((u##T)((v) >> (sizeof(T) * 8 - 1))) ^ (((u##T)(v)) << 1) // zigzag encode @@ -361,4 +361,4 @@ static FORCE_INLINE void *taosDecodeString(void *buf, char **value) { } #endif -#endif \ No newline at end of file +#endif /*_TD_UTIL_CODING_H*/ \ No newline at end of file diff --git a/src/util/inc/tcompare.h b/include/util/tcompare.h similarity index 97% rename from src/util/inc/tcompare.h rename to include/util/tcompare.h index 1125516d34c65da1b5d0c47dadd126aa0b1959fa..4b2c583b4baf0b338d5e7383a94703bd64df258b 100644 --- a/src/util/inc/tcompare.h +++ b/include/util/tcompare.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TCOMPARE_H -#define TDENGINE_TCOMPARE_H +#ifndef _TD_UTIL_COMPARE_H +#define _TD_UTIL_COMPARE_H #ifdef __cplusplus extern "C" { @@ -93,4 +93,4 @@ int32_t compareWStrPatternComp(const void* pLeft, const void* pRight); } #endif -#endif // TDENGINE_TCOMPARE_H +#endif /*_TD_UTIL_COMPARE_H*/ diff --git a/src/util/inc/tscompression.h b/include/util/tcompression.h similarity index 99% rename from src/util/inc/tscompression.h rename to include/util/tcompression.h index 3f2160bbf26da0ff6eb839ddb4c303d221bce7c2..1e76b5198ab41f874c4d89f8831bd95a2c952273 100644 --- a/src/util/inc/tscompression.h +++ b/include/util/tcompression.h @@ -13,14 +13,14 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TSCOMPRESSION_H -#define TDENGINE_TSCOMPRESSION_H +#ifndef _TD_UTIL_COMPRESSION_H +#define _TD_UTIL_COMPRESSION_H #ifdef __cplusplus extern "C" { #endif -#include "taosdef.h" +#include "tdef.h" #include "tutil.h" #define COMP_OVERFLOW_BYTES 2 @@ -366,4 +366,4 @@ static FORCE_INLINE int tsDecompressTimestamp(const char *const input, int compr } #endif -#endif // TDENGINE_TSCOMPRESSION_H \ No newline at end of file +#endif /*_TD_UTIL_COMPRESSION_H*/ \ No newline at end of file diff --git a/src/util/inc/tconfig.h b/include/util/tconfig.h similarity index 93% rename from src/util/inc/tconfig.h rename to include/util/tconfig.h index cf8977ce06c898fcdee8d21eedf5ed8d0f47f263..bc56d21b3ac09e58aa9e427914d8513b7f3b6b08 100644 --- a/src/util/inc/tconfig.h +++ b/include/util/tconfig.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_CFG_H -#define TDENGINE_CFG_H +#ifndef _TD_UTIL_CONFIG_H +#define _TD_UTIL_CONFIG_H #ifdef __cplusplus extern "C" { @@ -87,11 +87,11 @@ bool taosReadGlobalCfg(); void taosPrintGlobalCfg(); void taosDumpGlobalCfg(); -void taosInitConfigOption(SGlobalCfg cfg); -SGlobalCfg * taosGetConfigOption(const char *option); +void taosInitConfigOption(SGlobalCfg cfg); +SGlobalCfg *taosGetConfigOption(const char *option); #ifdef __cplusplus } #endif -#endif // TDENGINE_TGLOBALCFG_H +#endif /*_TD_UTIL_CONFIG_H*/ diff --git a/src/util/inc/tcrc32c.h b/include/util/tcrc32c.h similarity index 92% rename from src/util/inc/tcrc32c.h rename to include/util/tcrc32c.h index 78e52e226fc212e43c65e4f0dd7a9d4cd4d12757..200103b173d44d73d0c640588e521ba9ae6f0ca0 100644 --- a/src/util/inc/tcrc32c.h +++ b/include/util/tcrc32c.h @@ -18,10 +18,8 @@ 3. This notice may not be removed or altered from any source distribution. */ -#ifndef TDENGINE_TCRC32C_H -#define TDENGINE_TCRC32C_H - -#include +#ifndef _TD_UTIL_CRC32_H +#define _TD_UTIL_CRC32_H #ifdef __cplusplus extern "C" { @@ -41,4 +39,4 @@ void taosResolveCRC(); } #endif -#endif // TDENGINE_TCRC32C_H +#endif /*_TD_UTIL_CRC32_H*/ diff --git a/src/inc/taosdef.h b/include/util/tdef.h similarity index 86% rename from src/inc/taosdef.h rename to include/util/tdef.h index 89de733ba123157f6454fb35b26f06202a2095c4..a24d2bc9119dc16148c250050ee43cf519fd45e0 100644 --- a/src/inc/taosdef.h +++ b/include/util/tdef.h @@ -13,17 +13,15 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TAOS_DEF_H -#define TDENGINE_TAOS_DEF_H +#ifndef _TD_UTIL_DEF_H +#define _TD_UTIL_DEF_H + +#include "os.h" #ifdef __cplusplus extern "C" { #endif -#include -#include -#include "taos.h" - #define TSDB__packed #ifdef TSKEY32 @@ -32,10 +30,6 @@ extern "C" { #define TSKEY int64_t #endif -#define TSWINDOW_INITIALIZER ((STimeWindow) {INT64_MIN, INT64_MAX}) -#define TSWINDOW_DESC_INITIALIZER ((STimeWindow) {INT64_MAX, INT64_MIN}) -#define IS_TSWINDOW_SPECIFIED(win) (((win).skey != INT64_MIN) || ((win).ekey != INT64_MAX)) - #define TSKEY_INITIAL_VAL INT64_MIN // Bytes for each type. @@ -51,12 +45,7 @@ extern const int32_t TYPE_BYTES[15]; #define POINTER_BYTES sizeof(void *) // 8 by default assert(sizeof(ptrdiff_t) == sizseof(void*) #define TSDB_KEYSIZE sizeof(TSKEY) - -#if LINUX -#define TSDB_NCHAR_SIZE sizeof(wchar_t) -#else #define TSDB_NCHAR_SIZE sizeof(int32_t) -#endif // NULL definition #define TSDB_DATA_BOOL_NULL 0x02 @@ -84,6 +73,8 @@ extern const int32_t TYPE_BYTES[15]; #define TSDB_DEFAULT_PASS "powerdb" #elif (_TD_TQ_ == true) #define TSDB_DEFAULT_PASS "tqueue" +#elif (_TD_PRO_ == true) +#define TSDB_DEFAULT_PASS "prodb" #else #define TSDB_DEFAULT_PASS "taosdata" #endif @@ -219,8 +210,6 @@ do { \ #define TSDB_AUTH_LEN 16 #define TSDB_KEY_LEN 16 #define TSDB_VERSION_LEN 12 -#define TSDB_LOCALE_LEN 64 -#define TSDB_TIMEZONE_LEN 96 #define TSDB_LABEL_LEN 8 #define TSDB_CLUSTER_ID_LEN 40 @@ -403,54 +392,63 @@ do { \ #define TSDB_ARB_DUMMY_TIME 4765104000000 // 2121-01-01 00:00:00.000, :P -typedef enum { - TAOS_QTYPE_RPC = 0, - TAOS_QTYPE_FWD = 1, - TAOS_QTYPE_WAL = 2, - TAOS_QTYPE_CQ = 3, - TAOS_QTYPE_QUERY = 4 -} EQType; - #define TSDB_MAX_TIERS 3 #define TSDB_MAX_DISKS_PER_TIER 16 #define TSDB_MAX_DISKS (TSDB_MAX_TIERS * TSDB_MAX_DISKS_PER_TIER) -typedef enum { - TSDB_SUPER_TABLE = 0, // super table - TSDB_CHILD_TABLE = 1, // table created from super table - TSDB_NORMAL_TABLE = 2, // ordinary table - TSDB_STREAM_TABLE = 3, // table created from stream computing - TSDB_TEMP_TABLE = 4, // temp table created by nest query - TSDB_TABLE_MAX = 5 -} ETableType; - -typedef enum { - TSDB_MOD_MNODE = 0, - TSDB_MOD_HTTP = 1, - TSDB_MOD_MONITOR = 2, - TSDB_MOD_MQTT = 3, - TSDB_MOD_MAX = 4 -} EModuleType; - -typedef enum { - TSDB_CHECK_ITEM_NETWORK, - TSDB_CHECK_ITEM_MEM, - TSDB_CHECK_ITEM_CPU, - TSDB_CHECK_ITEM_DISK, - TSDB_CHECK_ITEM_OS, - TSDB_CHECK_ITEM_ACCESS, - TSDB_CHECK_ITEM_VERSION, - TSDB_CHECK_ITEM_DATAFILE, - TSDB_CHECK_ITEM_MAX -} ECheckItemType; - -typedef enum { - TD_ROW_DISCARD_UPDATE = 0, - TD_ROW_OVERWRITE_UPDATE = 1, - TD_ROW_PARTIAL_UPDATE = 2 -} TDUpdateConfig; - -extern char *qtypeStr[]; +#define TSDB_DATA_TYPE_NULL 0 // 1 bytes +#define TSDB_DATA_TYPE_BOOL 1 // 1 bytes +#define TSDB_DATA_TYPE_TINYINT 2 // 1 byte +#define TSDB_DATA_TYPE_SMALLINT 3 // 2 bytes +#define TSDB_DATA_TYPE_INT 4 // 4 bytes +#define TSDB_DATA_TYPE_BIGINT 5 // 8 bytes +#define TSDB_DATA_TYPE_FLOAT 6 // 4 bytes +#define TSDB_DATA_TYPE_DOUBLE 7 // 8 bytes +#define TSDB_DATA_TYPE_BINARY 8 // string +#define TSDB_DATA_TYPE_TIMESTAMP 9 // 8 bytes +#define TSDB_DATA_TYPE_NCHAR 10 // unicode string +#define TSDB_DATA_TYPE_UTINYINT 11 // 1 byte +#define TSDB_DATA_TYPE_USMALLINT 12 // 2 bytes +#define TSDB_DATA_TYPE_UINT 13 // 4 bytes +#define TSDB_DATA_TYPE_UBIGINT 14 // 8 bytes + +// ----------------- For variable data types such as TSDB_DATA_TYPE_BINARY and TSDB_DATA_TYPE_NCHAR + +//typedef int32_t VarDataOffsetT; +//typedef int16_t VarDataLenT; // maxVarDataLen: 32767 +//typedef uint16_t TDRowLenT; // not including overhead: 0 ~ 65535 +//typedef uint32_t TDRowTLenT; // total length, including overhead +// +//typedef struct tstr { +// VarDataLenT len; +// char data[]; +//} tstr; +// +//#pragma pack(push, 1) +//typedef struct { +// VarDataLenT len; +// uint8_t data; +//} SBinaryNullT; +// +//typedef struct { +// VarDataLenT len; +// uint32_t data; +//} SNCharNullT; +//#pragma pack(pop) +// +//#define VARSTR_HEADER_SIZE sizeof(VarDataLenT) +// +//#define varDataLen(v) ((VarDataLenT *)(v))[0] +//#define varDataTLen(v) (sizeof(VarDataLenT) + varDataLen(v)) +//#define varDataVal(v) ((void *)((char *)v + VARSTR_HEADER_SIZE)) +//#define varDataCopy(dst, v) memcpy((dst), (void*) (v), varDataTLen(v)) +//#define varDataLenByData(v) (*(VarDataLenT *)(((char*)(v)) - VARSTR_HEADER_SIZE)) +//#define varDataSetLen(v, _len) (((VarDataLenT *)(v))[0] = (VarDataLenT) (_len)) +//#define IS_VAR_DATA_TYPE(t) (((t) == TSDB_DATA_TYPE_BINARY) || ((t) == TSDB_DATA_TYPE_NCHAR)) +// +//#define varDataNetLen(v) (htons(((VarDataLenT *)(v))[0])) +//#define varDataNetTLen(v) (sizeof(VarDataLenT) + varDataNetLen(v)) + #ifdef __cplusplus } diff --git a/src/util/inc/tfile.h b/include/util/tfile.h similarity index 84% rename from src/util/inc/tfile.h rename to include/util/tfile.h index 066040170e44c24539e29b5de5acd438e8b9b9d0..ff62c9e34131256b20419816e6cef7eda47008ab 100644 --- a/src/util/inc/tfile.h +++ b/include/util/tfile.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TFILE_H -#define TDENGINE_TFILE_H +#ifndef _TD_UTIL_FILE_H +#define _TD_UTIL_FILE_H #ifdef __cplusplus extern "C" { @@ -28,8 +28,10 @@ void tfCleanup(); // the same syntax as UNIX standard open/close/read/write // but FD is int64_t and will never be reused -int64_t tfOpen(const char *pathname, int32_t flags); -int64_t tfOpenM(const char *pathname, int32_t flags, mode_t mode); +int64_t tfOpenReadWrite(const char *pathname); +int64_t tfOpenCreateWrite(const char *pathname); +int64_t tfOpenCreateWriteAppend(const char *pathname); + int64_t tfClose(int64_t tfd); int64_t tfWrite(int64_t tfd, void *buf, int64_t count); int64_t tfRead(int64_t tfd, void *buf, int64_t count); @@ -42,4 +44,4 @@ int32_t tfFtruncate(int64_t tfd, int64_t length); } #endif -#endif // TDENGINE_TFILE_H +#endif /*_TD_UTIL_FILE_H*/ diff --git a/src/util/inc/tfunctional.h b/include/util/tfunctional.h similarity index 94% rename from src/util/inc/tfunctional.h rename to include/util/tfunctional.h index 70f54e921da3166645bd43f4e3fe58bc144fe714..c96f997f06c363bfb06b8177a50b801a0cfbaea7 100644 --- a/src/util/inc/tfunctional.h +++ b/include/util/tfunctional.h @@ -12,8 +12,8 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef TD_TFUNCTIONAL_H -#define TD_TFUNCTIONAL_H +#ifndef _TD_UTIL_FUNCTIONAL_H +#define _TD_UTIL_FUNCTIONAL_H #ifdef __cplusplus extern "C" { @@ -53,4 +53,4 @@ void voidInvoke(tVoidSavedFunc* const pSavedFunc); } #endif -#endif +#endif /*_TD_UTIL_FUNCTIONAL_H*/ diff --git a/src/util/inc/hash.h b/include/util/thash.h similarity index 70% rename from src/util/inc/hash.h rename to include/util/thash.h index 7a93745dc800df3d6395e756cc8433cf4062af8a..d5038802c079f3ed3cddcef6c30c219459fc9917 100644 --- a/src/util/inc/hash.h +++ b/include/util/thash.h @@ -21,22 +21,45 @@ extern "C" { #endif #include "tarray.h" -#include "hashfunc.h" #include "tlockfree.h" +typedef uint32_t (*_hash_fn_t)(const char *, uint32_t); +typedef int32_t (*_equal_fn_t)(const void*, const void*, uint32_t len); +typedef void (*_hash_before_fn_t)(void *); +typedef void (*_hash_free_fn_t)(void *); + #define HASH_MAX_CAPACITY (1024 * 1024 * 16) #define HASH_DEFAULT_LOAD_FACTOR (0.75) + #define HASH_INDEX(v, c) ((v) & ((c)-1)) -typedef void (*_hash_free_fn_t)(void *param); +/** + * murmur hash algorithm + * @key usually string + * @len key length + * @seed hash seed + * @out an int32 value + */ +uint32_t MurmurHash3_32(const char *key, uint32_t len); + +/** + * + * @param key + * @param len + * @return + */ +uint32_t taosIntHash_32(const char *key, uint32_t len); +uint32_t taosIntHash_64(const char *key, uint32_t len); + +_hash_fn_t taosGetDefaultHashFunction(int32_t type); typedef struct SHashNode { struct SHashNode *next; uint32_t hashVal; // the hash value of key uint32_t dataLen; // length of data uint32_t keyLen; // length of the key + uint16_t count; // reference count int8_t removed; // flag to indicate removed - int8_t count; // reference count char data[]; } SHashNode; @@ -57,11 +80,13 @@ typedef struct SHashEntry { typedef struct SHashObj { SHashEntry **hashList; - size_t capacity; // number of slots - size_t size; // number of elements in hash table + uint32_t capacity; // number of slots + uint32_t size; // number of elements in hash table + _hash_fn_t hashFp; // hash function _hash_free_fn_t freeFp; // hash node free callback function - _equal_fn_t equalFp; // equal function + _equal_fn_t equalFp; // equal function + _hash_before_fn_t callbackFp; // function invoked before return the value to caller SRWLatch lock; // read-write spin lock SHashLockTypeE type; // lock type @@ -79,15 +104,6 @@ typedef struct SHashObj { */ SHashObj *taosHashInit(size_t capacity, _hash_fn_t fn, bool update, SHashLockTypeE type); - -/** - * set equal func of the hash table - * @param pHashObj - * @param equalFp - * @return - */ -void taosHashSetEqualFp(SHashObj *pHashObj, _equal_fn_t fp); - /** * return the size of hash table * @param pHashObj @@ -117,26 +133,15 @@ int32_t taosHashPut(SHashObj *pHashObj, const void *key, size_t keyLen, void *da void *taosHashGet(SHashObj *pHashObj, const void *key, size_t keyLen); /** - * apply the udf before return the result + * Clone the result to destination buffer * @param pHashObj * @param key * @param keyLen - * @param fp - * @param d + * @param destBuf * @return */ -void* taosHashGetClone(SHashObj *pHashObj, const void *key, size_t keyLen, void (*fp)(void *), void* d); +void *taosHashGetClone(SHashObj *pHashObj, const void *key, size_t keyLen, void* destBuf); -/** - * @param pHashObj - * @param key - * @param keyLen - * @param fp - * @param d - * @param sz - * @return - */ -void* taosHashGetCloneExt(SHashObj *pHashObj, const void *key, size_t keyLen, void (*fp)(void *), void** d, size_t *sz); /** * remove item with the specified key * @param pHashObj @@ -145,34 +150,54 @@ void* taosHashGetCloneExt(SHashObj *pHashObj, const void *key, size_t keyLen, vo */ int32_t taosHashRemove(SHashObj *pHashObj, const void *key, size_t keyLen); -int32_t taosHashRemoveWithData(SHashObj *pHashObj, const void *key, size_t keyLen, void* data, size_t dsize); - -int32_t taosHashCondTraverse(SHashObj *pHashObj, bool (*fp)(void *, void *), void *param); - +/** + * Clear the hash table. + * @param pHashObj + */ void taosHashClear(SHashObj *pHashObj); /** - * clean up hash table + * Clean up hash table and release all allocated resources. * @param handle */ void taosHashCleanup(SHashObj *pHashObj); /** - * + * Get the max overflow link list length * @param pHashObj * @return */ int32_t taosHashGetMaxOverflowLinkLength(const SHashObj *pHashObj); +/** + * Get the hash table size + * @param pHashObj + * @return + */ size_t taosHashGetMemSize(const SHashObj *pHashObj); +/** + * Create the hash table iterator + * @param pHashObj + * @param p + * @return + */ void *taosHashIterate(SHashObj *pHashObj, void *p); +/** + * Cancel the hash table iterator + * @param pHashObj + * @param p + */ void taosHashCancelIterate(SHashObj *pHashObj, void *p); -void *taosHashGetDataKey(SHashObj *pHashObj, void *data); - -uint32_t taosHashGetDataKeyLen(SHashObj *pHashObj, void *data); +/** + * Get the corresponding key information for a given data in hash table + * @param pHashObj + * @param data + * @return + */ +int32_t taosHashGetKey(SHashObj *pHashObj, void *data, void** key, size_t* keyLen); #ifdef __cplusplus } diff --git a/src/util/inc/tidpool.h b/include/util/tidpool.h similarity index 92% rename from src/util/inc/tidpool.h rename to include/util/tidpool.h index e4439439ced6522e26c8db4a560c50f5b0cb8a16..1a977fd04c27b14b3ce0ce242b3d523e1d67d688 100644 --- a/src/util/inc/tidpool.h +++ b/include/util/tidpool.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TIDPOOL_H -#define TDENGINE_TIDPOOL_H +#ifndef _TD_UTIL_IDPOOL_H +#define _TD_UTIL_IDPOOL_H #ifdef __cplusplus extern "C" { @@ -40,4 +40,4 @@ bool taosIdPoolMarkStatus(void *handle, int id); } #endif -#endif +#endif /*_TD_UTIL_IDPOOL_H*/ diff --git a/include/util/tkey.h b/include/util/tkey.h index b71cdb286814dd7b2afe0b4e41cebef66efac3d9..197cfff958416dc49db20b59dc8a2b6fe3216200 100644 --- a/include/util/tkey.h +++ b/include/util/tkey.h @@ -12,8 +12,8 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef TDENGINE_TKEY_H -#define TDENGINE_TKEY_H +#ifndef _TD_UTIL_KEY_H +#define _TD_UTIL_KEY_H #ifdef __cplusplus extern "C" { @@ -28,4 +28,4 @@ char * taosDesDecode(int64_t key, char *src, int len); } #endif -#endif \ No newline at end of file +#endif /*_TD_UTIL_KEY_H*/ \ No newline at end of file diff --git a/include/util/tlist.h b/include/util/tlist.h index 758190454030a7287c3ca95418e7a8fea3618cc5..8246205e9b46fab0f6a363d61c19dfd59bdb8bc5 100644 --- a/include/util/tlist.h +++ b/include/util/tlist.h @@ -12,8 +12,8 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef _TD_LIST_ -#define _TD_LIST_ +#ifndef _TD_UTIL_LIST_H +#define _TD_UTIL_LIST_H #ifdef __cplusplus extern "C" { @@ -69,4 +69,4 @@ SListNode *tdListNext(SListIter *pIter); } #endif -#endif \ No newline at end of file +#endif /*_TD_UTIL_LIST_H*/ \ No newline at end of file diff --git a/include/util/tlockfree.h b/include/util/tlockfree.h index 4660dd1cbd8e871885af1e6c0cf1218aa9e32cf1..fec346cd0290b914df038d3a965603b8b27b3fad 100644 --- a/include/util/tlockfree.h +++ b/include/util/tlockfree.h @@ -12,8 +12,8 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef __TD_LOCK_FREE_H__ -#define __TD_LOCK_FREE_H__ +#ifndef _TD_UTIL_LOCK_FREE_H +#define _TD_UTIL_LOCK_FREE_H #include "os.h" @@ -21,7 +21,6 @@ extern "C" { #endif - // reference counting typedef void (*_ref_fn_t)(const void* pObj); @@ -106,9 +105,8 @@ void taosRUnLockLatch(SRWLatch *pLatch); break; \ } - #ifdef __cplusplus } #endif -#endif +#endif /*_TD_UTIL_LOCK_FREE_H*/ diff --git a/include/util/tlog.h b/include/util/tlog.h index 50bc45e7dd5b7d9aa5bba75d270718175d9e328f..5c6e59b103f442a1d6e92c443f13575a9d432dda 100644 --- a/include/util/tlog.h +++ b/include/util/tlog.h @@ -13,13 +13,37 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TLOG_H -#define TDENGINE_TLOG_H +#ifndef _TD_UTIL_LOG_H +#define _TD_UTIL_LOG_H #ifdef __cplusplus extern "C" { #endif +// log +extern int8_t tsAsyncLog; +extern int32_t tsNumOfLogLines; +extern int32_t tsLogKeepDays; +extern int32_t dDebugFlag; +extern int32_t vDebugFlag; +extern int32_t mDebugFlag; +extern int32_t cDebugFlag; +extern int32_t jniDebugFlag; +extern int32_t tmrDebugFlag; +extern int32_t sdbDebugFlag; +extern int32_t httpDebugFlag; +extern int32_t mqttDebugFlag; +extern int32_t monDebugFlag; +extern int32_t uDebugFlag; +extern int32_t rpcDebugFlag; +extern int32_t odbcDebugFlag; +extern int32_t qDebugFlag; +extern int32_t wDebugFlag; +extern int32_t sDebugFlag; +extern int32_t tsdbDebugFlag; +extern int32_t cqDebugFlag; +extern int32_t debugFlag; + #define DEBUG_FATAL 1U #define DEBUG_ERROR DEBUG_FATAL #define DEBUG_WARN 2U @@ -53,4 +77,4 @@ void taosDumpData(unsigned char *msg, int32_t len); } #endif -#endif +#endif /*_TD_UTIL_LOG_H*/ diff --git a/src/util/inc/tlosertree.h b/include/util/tlosertree.h similarity index 92% rename from src/util/inc/tlosertree.h rename to include/util/tlosertree.h index 58f2ca8c5c81408b35c2c9435357deeb2b0f13a4..d6ffde82ca0febcce8183f6d552d124de25119fc 100644 --- a/src/util/inc/tlosertree.h +++ b/include/util/tlosertree.h @@ -13,15 +13,13 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TLOSERTREE_H -#define TDENGINE_TLOSERTREE_H +#ifndef _TD_UTIL_LOSERTREE_H +#define _TD_UTIL_LOSERTREE_H #ifdef __cplusplus extern "C" { #endif -#include - typedef int (*__merge_compare_fn_t)(const void *, const void *, void *param); typedef struct SLoserTreeNode { @@ -51,4 +49,4 @@ void tLoserTreeDisplay(SLoserTreeInfo *pTree); } #endif -#endif // TDENGINE_TLOSERTREE_H +#endif /*_TD_UTIL_LOSERTREE_H*/ diff --git a/src/util/inc/tmd5.h b/include/util/tmd5.h similarity index 96% rename from src/util/inc/tmd5.h rename to include/util/tmd5.h index d7fd038f37c9e1698394c90e07aaf9ac6a23590c..329f4acf1105433f31d2aea02767dc1c693f2b06 100644 --- a/src/util/inc/tmd5.h +++ b/include/util/tmd5.h @@ -22,10 +22,8 @@ *********************************************************************** */ -#ifndef _taos_md5_header_ -#define _taos_md5_header_ - -#include +#ifndef _TD_UTIL_MD5_H +#define _TD_UTIL_MD5_H typedef struct { uint32_t i[2]; /* number of _bits_ handled mod 2^64 */ @@ -38,4 +36,4 @@ void MD5Init(MD5_CTX *mdContext); void MD5Update(MD5_CTX *mdContext, uint8_t *inBuf, unsigned int inLen); void MD5Final(MD5_CTX *mdContext); -#endif +#endif /*_TD_UTIL_MD5_H*/ diff --git a/src/util/inc/tmempool.h b/include/util/tmempool.h similarity index 91% rename from src/util/inc/tmempool.h rename to include/util/tmempool.h index f2c6a0ef006cc77ba8a865994aaf5fb31d0c21a1..3e3db738a9f372082d785bedb28da776c23ba59f 100644 --- a/src/util/inc/tmempool.h +++ b/include/util/tmempool.h @@ -12,8 +12,8 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef TDENGINE_TMEMPOOL_H -#define TDENGINE_TMEMPOOL_H +#ifndef _TD_UTIL_MEMPOOL_H +#define _TD_UTIL_MEMPOOL_H #ifdef __cplusplus extern "C" { @@ -33,4 +33,4 @@ void taosMemPoolCleanUp(mpool_h handle); } #endif -#endif +#endif /*_TD_UTIL_MEMPOOL_H*/ diff --git a/src/util/inc/tnote.h b/include/util/tnote.h similarity index 95% rename from src/util/inc/tnote.h rename to include/util/tnote.h index 7511b61f417c76b312faeb3da0855119658bdd68..e4f28d8cff8c0fd5c0333b2e8eced98fd1599998 100644 --- a/src/util/inc/tnote.h +++ b/include/util/tnote.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TNOTE_H -#define TDENGINE_TNOTE_H +#ifndef _TD_UTIL_NOTE_H +#define _TD_UTIL_NOTE_H #ifdef __cplusplus extern "C" { @@ -61,4 +61,4 @@ void taosNotePrintBuffer(SNoteObj *pNote, char *buffer, int32_t len); } #endif -#endif +#endif /*_TD_UTIL_NOTE_H*/ diff --git a/src/util/inc/tqueue.h b/include/util/tqueue.h similarity index 96% rename from src/util/inc/tqueue.h rename to include/util/tqueue.h index c3051464e556860178be36f3473f5e4686f6082e..cd897435fabeaa050b77ae916c9f18d6eb2aea43 100644 --- a/src/util/inc/tqueue.h +++ b/include/util/tqueue.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TAOS_QUEUE_H -#define TAOS_QUEUE_H +#ifndef _TD_UTIL_QUEUE_H +#define _TD_UTIL_QUEUE_H #ifdef __cplusplus extern "C" { @@ -71,6 +71,6 @@ int taosGetQsetItemsNumber(taos_qset param); } #endif -#endif +#endif /*_TD_UTIL_QUEUE_H*/ diff --git a/src/util/inc/tref.h b/include/util/tref.h similarity index 97% rename from src/util/inc/tref.h rename to include/util/tref.h index 085c10c55198fc92bfacb0628c38f05da508bfb9..cc7d075f5209dfb3ab463a1c2cdb4ec39d0bbcf2 100644 --- a/src/util/inc/tref.h +++ b/include/util/tref.h @@ -14,8 +14,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TREF_H -#define TDENGINE_TREF_H +#ifndef _TD_UTIL_REF_H +#define _TD_UTIL_REF_H #ifdef __cplusplus extern "C" { @@ -74,4 +74,4 @@ void demoIterateRefs(int rsetId) { } #endif -#endif // TDENGINE_TREF_H +#endif /*_TD_UTIL_REF_H*/ diff --git a/src/util/inc/tsched.h b/include/util/tsched.h similarity index 96% rename from src/util/inc/tsched.h rename to include/util/tsched.h index a1591512c1f87f524837a7986e3c8b3e14e25924..d60c0c5ad4aa7a7e3e314d53765347b5209c8b84 100644 --- a/src/util/inc/tsched.h +++ b/include/util/tsched.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TSCHED_H -#define TDENGINE_TSCHED_H +#ifndef _TD_UTIL_SCHED_H +#define _TD_UTIL_SCHED_H #ifdef __cplusplus extern "C" { @@ -68,4 +68,4 @@ void taosScheduleTask(void *queueScheduler, SSchedMsg *pMsg); } #endif -#endif // TDENGINE_TSCHED_H +#endif /*_TD_UTIL_SCHED_H*/ diff --git a/src/util/inc/tskiplist.h b/include/util/tskiplist.h similarity index 98% rename from src/util/inc/tskiplist.h rename to include/util/tskiplist.h index d9dc001ccd072e9803d7241bd7e0e3e029a77b3e..8e991727a7de94b802674ccc90153585188f733b 100644 --- a/src/util/inc/tskiplist.h +++ b/include/util/tskiplist.h @@ -13,15 +13,15 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TSKIPLIST_H -#define TDENGINE_TSKIPLIST_H +#ifndef _TD_UTIL_SKILIST_H +#define _TD_UTIL_SKILIST_H #ifdef __cplusplus extern "C" { #endif #include "os.h" -#include "taosdef.h" +#include "tdef.h" #include "tarray.h" #include "tfunctional.h" @@ -159,4 +159,4 @@ void tSkipListRemoveNode(SSkipList *pSkipList, SSkipListNode *pNod } #endif -#endif // TDENGINE_TSKIPLIST_H +#endif /*_TD_UTIL_SKILIST_H*/ diff --git a/src/os/inc/osDir.h b/include/util/tstep.h similarity index 59% rename from src/os/inc/osDir.h rename to include/util/tstep.h index 7afe1264512bbffd34d9278ffb37034a473b827f..87e95edd97e6de0a80dc6ac71678b3c481a76f24 100644 --- a/src/os/inc/osDir.h +++ b/include/util/tstep.h @@ -13,22 +13,24 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_OS_DIR_H -#define TDENGINE_OS_DIR_H +#ifndef _TD_UTIL_STEP_H_ +#define _TD_UTIL_STEP_H_ #ifdef __cplusplus extern "C" { #endif -void taosRemoveDir(char *rootDir); -bool taosDirExist(const char* dirname); -int32_t taosMkDir(const char *pathname, mode_t mode); -void taosRemoveOldLogFiles(char *rootDir, int32_t keepDays); -int32_t taosRename(char *oldName, char *newName); -int32_t taosCompressFile(char *srcFileName, char *destFileName); +typedef int32_t (*InitFp)(void **obj); +typedef void (*CleanupFp)(void **obj); +typedef void (*ReportFp)(char *name, char *desc); + +struct SSteps *taosStepInit(int32_t maxsize, ReportFp fp); +int32_t taosStepExec(struct SSteps *steps); +void taosStepCleanup(struct SSteps *steps); +int32_t taosStepAdd(struct SSteps *steps, char *name, void **obj, InitFp initFp, CleanupFp cleanupFp); #ifdef __cplusplus } #endif -#endif +#endif /*_TD_UTIL_STEP_H_*/ diff --git a/src/util/inc/tstrbuild.h b/include/util/tstrbuild.h similarity index 93% rename from src/util/inc/tstrbuild.h rename to include/util/tstrbuild.h index 68d1914be3c216a05a03fe6b80490ca1ee7bea4f..48a302531cf9c80cc9c8b4ceff3b4ba714127dd1 100644 --- a/src/util/inc/tstrbuild.h +++ b/include/util/tstrbuild.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_STRING_BUILDER_H -#define TDENGINE_STRING_BUILDER_H +#ifndef _TD_UTIL_STRING_BUILDER_H +#define _TD_UTIL_STRING_BUILDER_H #include #include @@ -49,4 +49,4 @@ void taosStringBuilderAppendDouble(SStringBuilder* sb, double v); } #endif -#endif \ No newline at end of file +#endif /*_TD_UTIL_STRING_BUILDER_H*/ \ No newline at end of file diff --git a/src/util/inc/tthread.h b/include/util/tthread.h similarity index 90% rename from src/util/inc/tthread.h rename to include/util/tthread.h index 7443ad706dcbef529d857fe823cddd0cc1efbdd3..0ff267dd1fef8e2e52d5032d15441604110655c0 100644 --- a/src/util/inc/tthread.h +++ b/include/util/tthread.h @@ -13,15 +13,15 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TTHREAD_H -#define TDENGINE_TTHREAD_H +#ifndef _TD_UTIL_THREAD_H +#define _TD_UTIL_THREAD_H #ifdef __cplusplus extern "C" { #endif #include "os.h" -#include "taosdef.h" +#include "tdef.h" // create new thread pthread_t* taosCreateThread( void *(*__start_routine) (void *), void* param); @@ -34,4 +34,4 @@ bool taosThreadRunning(pthread_t* pthread); } #endif -#endif // TDENGINE_TTHREAD_H +#endif /*_TD_UTIL_THREAD_H*/ diff --git a/src/util/inc/ttimer.h b/include/util/ttimer.h similarity index 94% rename from src/util/inc/ttimer.h rename to include/util/ttimer.h index 6992f5ab6ea792f6cdadd0af217fa5cb8b02dbc5..987d3f3cdc95f983b6d9bb54168da11aa5f8340a 100644 --- a/src/util/inc/ttimer.h +++ b/include/util/ttimer.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TTIMER_H -#define TDENGINE_TTIMER_H +#ifndef _TD_UTIL_TIMER_H +#define _TD_UTIL_TIMER_H #ifdef __cplusplus extern "C" { @@ -48,4 +48,4 @@ void taosUninitTimer(); } #endif -#endif // TDENGINE_TTIMER_H +#endif /*_TD_UTIL_TIMER_H*/ diff --git a/src/util/inc/tutil.h b/include/util/tutil.h similarity index 87% rename from src/util/inc/tutil.h rename to include/util/tutil.h index 6bcfb5de295c5719032b81c23d16ec2b1476349e..202c55ab9cdb0bf2f9e5ea6e0286efc6a8d039e2 100644 --- a/src/util/inc/tutil.h +++ b/include/util/tutil.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TUTIL_H -#define TDENGINE_TUTIL_H +#ifndef _TD_UTIL_UTIL_H +#define _TD_UTIL_UTIL_H #ifdef __cplusplus extern "C" { @@ -23,7 +23,7 @@ extern "C" { #include "os.h" #include "tmd5.h" #include "tcrc32c.h" -#include "taosdef.h" +#include "tdef.h" int32_t strdequote(char *src); int32_t strRmquote(char *z, int32_t len); @@ -40,8 +40,8 @@ char * paGetToken(char *src, char **token, int32_t *tokenLen); int32_t taosByteArrayToHexStr(char bytes[], int32_t len, char hexstr[]); int32_t taosHexStrToByteArray(char hexstr[], char bytes[]); -bool taosGetVersionNumber(char *versionStr, int *versionNubmer); -int taosCheckVersion(char *input_client_version, char *input_server_version, int compared_segments); +//bool taosGetVersionNumber(char *versionStr, int *versionNubmer); +//int taosCheckVersion(char *input_client_version, char *input_server_version, int compared_segments); char * taosIpStr(uint32_t ipInt); uint32_t ip2uint(const char *const ip_addr); @@ -58,4 +58,4 @@ static FORCE_INLINE void taosEncryptPass(uint8_t *inBuf, size_t inLen, char *tar } #endif -#endif // TDENGINE_TUTIL_H +#endif /*_TD_UTIL_UTIL_H*/ diff --git a/src/util/inc/tworker.h b/include/util/tworker.h similarity index 62% rename from src/util/inc/tworker.h rename to include/util/tworker.h index 7bc1eba2fdcca0409737d9305911cd2987afe1ab..156ced383e0a9d81cbddd22c293853c81a41e88c 100644 --- a/src/util/inc/tworker.h +++ b/include/util/tworker.h @@ -13,30 +13,33 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TWORKER_H -#define TDENGINE_TWORKER_H +#ifndef _TD_UTIL_WORKER_H +#define _TD_UTIL_WORKER_H #ifdef __cplusplus extern "C" { #endif -typedef void *(*FWorkerThread)(void *pWorker); +typedef int32_t (*ProcessReqFp)(void *ahandle, void *msg); +typedef void (*SendRspFp)(void *ahandle, void *msg, int32_t qtype, int32_t code); + struct SWorkerPool; typedef struct { - pthread_t thread; // thread - int32_t id; // worker ID - struct SWorkerPool *pPool; + pthread_t thread; // thread + int32_t id; // worker ID + struct SWorkerPool *pool; } SWorker; typedef struct SWorkerPool { - int32_t max; // max number of workers - int32_t min; // min number of workers - int32_t num; // current number of workers - void * qset; - char * name; - SWorker *worker; - FWorkerThread workerFp; + int32_t max; // max number of workers + int32_t min; // min number of workers + int32_t num; // current number of workers + void * qset; + const char * name; + SWorker * workers; + ProcessReqFp reqFp; + SendRspFp rspFp; pthread_mutex_t mutex; } SWorkerPool; @@ -49,4 +52,4 @@ void tWorkerFreeQueue(SWorkerPool *pPool, void *pQueue); } #endif -#endif +#endif /*_TD_UTIL_WORKER_H*/ diff --git a/src/common/inc/tulog.h b/include/util/ulog.h similarity index 95% rename from src/common/inc/tulog.h rename to include/util/ulog.h index 566da40a10e078b9789e2e1b76a8d82fe89aef46..ba59f4eb791b449345295cb836af0ddc83094a55 100644 --- a/src/common/inc/tulog.h +++ b/include/util/ulog.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_COMMON_ULOG_H -#define TDENGINE_COMMON_ULOG_H +#ifndef _TD_UTIL_ULOG_H +#define _TD_UTIL_ULOG_H #ifdef __cplusplus extern "C" { @@ -39,4 +39,4 @@ extern int8_t tscEmbedded; } #endif -#endif +#endif /*_TD_UTIL_ULOG_H*/ diff --git a/source/client/consumer/consumer.c b/source/client/consumer/consumer.c index 6dea4a4e57392be988126c579648f39a8270b9bf..4ba1f9514444f8972bcb4c72cdb8a3c3c681eeec 100644 --- a/source/client/consumer/consumer.c +++ b/source/client/consumer/consumer.c @@ -11,4 +11,6 @@ * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . - */ \ No newline at end of file + */ + +#include "consumer.h" diff --git a/source/client/src/client.c b/source/client/src/client.c index 6dea4a4e57392be988126c579648f39a8270b9bf..77c4aa1b2d80c1344704c71f869f510e657b7a7e 100644 --- a/source/client/src/client.c +++ b/source/client/src/client.c @@ -11,4 +11,11 @@ * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . - */ \ No newline at end of file + */ + +//#include "taos.h" + +//TAOS_RES *taos_query(TAOS *taos, const char *sql) { +// +//} + diff --git a/source/common/CMakeLists.txt b/source/common/CMakeLists.txt index 1273a61c02c84e649d787c07674372b5f068129a..d03231fcfbcbdd9fdedab59c0b7c82e8195d5936 100644 --- a/source/common/CMakeLists.txt +++ b/source/common/CMakeLists.txt @@ -4,4 +4,10 @@ target_include_directories( common PUBLIC "${CMAKE_SOURCE_DIR}/include/common" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" +) +target_link_libraries( + common + PUBLIC os + PUBLIC util + INTERFACE api ) \ No newline at end of file diff --git a/src/common/src/sqlcmdstr.c b/source/common/src/sqlcmdstr.c similarity index 96% rename from src/common/src/sqlcmdstr.c rename to source/common/src/sqlcmdstr.c index 672106523e7c7eab8c606db1940dd9485e7f4c8f..6e845e7e9e4f1a7c4b0474e51e11c6e6e3d8f6c9 100644 --- a/src/common/src/sqlcmdstr.c +++ b/source/common/src/sqlcmdstr.c @@ -15,4 +15,4 @@ #define TSDB_SQL_C -#include "tcmdtype.h" +#include "tmsgtype.h" diff --git a/src/util/src/tcompare.c b/source/common/src/tcompare.c similarity index 97% rename from src/util/src/tcompare.c rename to source/common/src/tcompare.c index 482dd8a6a15d1c7bf6aca76159b95b71ef244dd2..e8e9ae3d688aa27d866f5bdc9d3336993b0b4896 100644 --- a/src/util/src/tcompare.c +++ b/source/common/src/tcompare.c @@ -17,12 +17,12 @@ #define _XOPEN_SOURCE #define _DEFAULT_SOURCE +#include "os.h" #include "tcompare.h" -#include "tulog.h" -#include "hash.h" +#include "ulog.h" +#include "thash.h" #include "regex.h" -#include "os.h" -#include "ttype.h" +#include "ttypes.h" int32_t setCompareBytes1(const void *pLeft, const void *pRight) { return NULL != taosHashGet((SHashObj *)pRight, pLeft, 1) ? 1 : 0; @@ -233,14 +233,20 @@ int patternMatch(const char *patterStr, const char *str, size_t size, const SPat int32_t i = 0; int32_t j = 0; + int32_t o = 0; + int32_t m = 0; while ((c = patterStr[i++]) != 0) { if (c == pInfo->matchAll) { /* Match "*" */ while ((c = patterStr[i++]) == pInfo->matchAll || c == pInfo->matchOne) { - if (c == pInfo->matchOne && (j > size || str[j++] == 0)) { - // empty string, return not match - return TSDB_PATTERN_NOWILDCARDMATCH; + if (c == pInfo->matchOne) { + if (j > size || str[j++] == 0) { + // empty string, return not match + return TSDB_PATTERN_NOWILDCARDMATCH; + } else { + ++o; + } } } @@ -249,9 +255,10 @@ int patternMatch(const char *patterStr, const char *str, size_t size, const SPat } char next[3] = {toupper(c), tolower(c), 0}; + m = o; while (1) { - size_t n = strcspn(str, next); - str += n; + size_t n = strcspn(str + m, next); + str += m + n; if (str[0] == 0 || (n >= size)) { break; @@ -261,12 +268,14 @@ int patternMatch(const char *patterStr, const char *str, size_t size, const SPat if (ret != TSDB_PATTERN_NOMATCH) { return ret; } + m = 0; } return TSDB_PATTERN_NOWILDCARDMATCH; } c1 = str[j++]; - + ++o; + if (j <= size) { if (c == '\\' && patterStr[i] == '_' && c1 == '_') { i++; continue; } if (c == c1 || tolower(c) == tolower(c1) || (c == pInfo->matchOne && c1 != 0)) { @@ -292,7 +301,7 @@ int WCSPatternMatch(const wchar_t *patterStr, const wchar_t *str, size_t size, c if (c == matchAll) { /* Match "%" */ while ((c = patterStr[i++]) == matchAll || c == matchOne) { - if (c == matchOne && (j > size || str[j++] == 0)) { + if (c == matchOne && (j >= size || str[j++] == 0)) { return TSDB_PATTERN_NOWILDCARDMATCH; } } diff --git a/src/common/src/tdataformat.c b/source/common/src/tdataformat.c similarity index 99% rename from src/common/src/tdataformat.c rename to source/common/src/tdataformat.c index 61378c79c4b5c44ffa11ae9132aa6f8b89ab5f71..6e76e3a8d085adadb4d9d92867cc0ca0ccd646a7 100644 --- a/src/common/src/tdataformat.c +++ b/source/common/src/tdataformat.c @@ -13,7 +13,7 @@ * along with this program. If not, see . */ #include "tdataformat.h" -#include "tulog.h" +#include "ulog.h" #include "talgo.h" #include "tcoding.h" #include "wchar.h" diff --git a/src/util/src/terror.c b/source/common/src/terror.c similarity index 100% rename from src/util/src/terror.c rename to source/common/src/terror.c diff --git a/src/common/src/tglobal.c b/source/common/src/tglobal.c similarity index 97% rename from src/common/src/tglobal.c rename to source/common/src/tglobal.c index 339fa35bb3009db96c9c6e0cabea6b60881f05c5..9eab95cd41243d62fc9a51dbc402d4b08efdcdc9 100644 --- a/src/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -17,15 +17,14 @@ #include "os.h" #include "taosdef.h" #include "taoserror.h" -#include "tulog.h" +#include "ulog.h" +#include "tlog.h" #include "tconfig.h" #include "tglobal.h" -#include "monitor.h" -#include "tsocket.h" +#include "tcompare.h" #include "tutil.h" -#include "tlocale.h" #include "ttimezone.h" -#include "tcompare.h" +#include "tlocale.h" // cluster char tsFirst[TSDB_EP_LEN] = {0}; @@ -59,9 +58,6 @@ float tsNumOfThreadsPerCore = 1.0f; int32_t tsNumOfCommitThreads = 4; float tsRatioOfQueryCores = 1.0f; int8_t tsDaylight = 0; -char tsTimezone[TSDB_TIMEZONE_LEN] = {0}; -char tsLocale[TSDB_LOCALE_LEN] = {0}; -char tsCharset[TSDB_LOCALE_LEN] = {0}; // default encode string int8_t tsEnableCoreFile = 0; int32_t tsMaxBinaryDisplayWidth = 30; @@ -201,15 +197,11 @@ int8_t tsEnableStream = 1; int8_t tsCompactMnodeWal = 0; int8_t tsPrintAuth = 0; int8_t tscEmbedded = 0; -char configDir[PATH_MAX] = {0}; char tsVnodeDir[PATH_MAX] = {0}; char tsDnodeDir[PATH_MAX] = {0}; char tsMnodeDir[PATH_MAX] = {0}; char tsMnodeTmpDir[PATH_MAX] = {0}; char tsMnodeBakDir[PATH_MAX] = {0}; -char tsDataDir[PATH_MAX] = {0}; -char tsScriptDir[PATH_MAX] = {0}; -char tsTempDir[PATH_MAX] = "/tmp/"; int32_t tsDiskCfgNum = 0; int32_t tsTopicBianryLen = 16000; @@ -229,11 +221,6 @@ SDiskCfg tsDiskCfg[TSDB_MAX_DISKS]; int64_t tsTickPerDay[] = {86400000L, 86400000000L, 86400000000000L}; // system info -char tsOsName[10] = "Linux"; -int64_t tsPageSize; -int64_t tsOpenMax; -int64_t tsStreamMax; -int32_t tsNumOfCores = 1; float tsTotalTmpDirGB = 0; float tsTotalDataDirGB = 0; float tsAvailTmpDirectorySpace = 0; @@ -244,28 +231,6 @@ float tsMinimalDataDirGB = 2.0f; int32_t tsTotalMemoryMB = 0; uint32_t tsVersion = 0; -// log -int32_t tsNumOfLogLines = 10000000; -int32_t mDebugFlag = 131; -int32_t sdbDebugFlag = 131; -int32_t dDebugFlag = 135; -int32_t vDebugFlag = 135; -uint32_t cDebugFlag = 131; -int32_t jniDebugFlag = 131; -int32_t odbcDebugFlag = 131; -int32_t httpDebugFlag = 131; -int32_t mqttDebugFlag = 131; -int32_t monDebugFlag = 131; -uint32_t qDebugFlag = 131; -int32_t rpcDebugFlag = 131; -int32_t uDebugFlag = 131; -int32_t debugFlag = 0; -int32_t sDebugFlag = 135; -int32_t wDebugFlag = 135; -int32_t tsdbDebugFlag = 131; -int32_t cqDebugFlag = 131; -int32_t fsDebugFlag = 135; - #ifdef TD_TSZ // // lossy compress 6 @@ -419,23 +384,6 @@ static void taosCheckDataDirCfg() { } } -static int32_t taosCheckTmpDir(void) { - if (strlen(tsTempDir) <= 0){ - uError("tempDir is not set"); - return -1; - } - - DIR *dir = opendir(tsTempDir); - if (dir == NULL) { - uError("can not open tempDir:%s, error:%s", tsTempDir, strerror(errno)); - return -1; - } - - closedir(dir); - - return 0; -} - static void doInitGlobalConfig(void) { osInit(); srand(taosSafeRand()); @@ -1096,7 +1044,7 @@ static void doInitGlobalConfig(void) { cfg.cfgType = TSDB_CFG_CTYPE_B_CONFIG | TSDB_CFG_CTYPE_B_SHOW | TSDB_CFG_CTYPE_B_CLIENT; cfg.minValue = 0; cfg.maxValue = 0; - cfg.ptrLength = tListLen(tsTimezone); + cfg.ptrLength = TSDB_TIMEZONE_LEN; cfg.unitType = TAOS_CFG_UTYPE_NONE; taosInitConfigOption(cfg); @@ -1106,7 +1054,7 @@ static void doInitGlobalConfig(void) { cfg.cfgType = TSDB_CFG_CTYPE_B_CONFIG | TSDB_CFG_CTYPE_B_SHOW | TSDB_CFG_CTYPE_B_CLIENT; cfg.minValue = 0; cfg.maxValue = 0; - cfg.ptrLength = tListLen(tsLocale); + cfg.ptrLength = TSDB_LOCALE_LEN; cfg.unitType = TAOS_CFG_UTYPE_NONE; taosInitConfigOption(cfg); @@ -1116,7 +1064,7 @@ static void doInitGlobalConfig(void) { cfg.cfgType = TSDB_CFG_CTYPE_B_CONFIG | TSDB_CFG_CTYPE_B_SHOW | TSDB_CFG_CTYPE_B_CLIENT; cfg.minValue = 0; cfg.maxValue = 0; - cfg.ptrLength = tListLen(tsCharset); + cfg.ptrLength = TSDB_LOCALE_LEN; cfg.unitType = TAOS_CFG_UTYPE_NONE; taosInitConfigOption(cfg); @@ -1612,7 +1560,7 @@ static void doInitGlobalConfig(void) { cfg.cfgType = TSDB_CFG_CTYPE_B_CONFIG | TSDB_CFG_CTYPE_B_CLIENT; cfg.minValue = 0; cfg.maxValue = 0; - cfg.ptrLength = tListLen(tsTempDir); + cfg.ptrLength = PATH_MAX; cfg.unitType = TAOS_CFG_UTYPE_NONE; taosInitConfigOption(cfg); @@ -1732,7 +1680,7 @@ int32_t taosCheckGlobalCfg() { taosCheckDataDirCfg(); - if (taosCheckTmpDir()) { + if (!taosDirExist(tsTempDir)) { return -1; } diff --git a/src/common/src/tlocale.c b/source/common/src/tlocale.c similarity index 98% rename from src/common/src/tlocale.c rename to source/common/src/tlocale.c index 46a3d951db8473cbb1b6346a66938312d4e41fb1..a98a46b28a658924042b35df93d17db18b2c2f54 100644 --- a/src/common/src/tlocale.c +++ b/source/common/src/tlocale.c @@ -15,7 +15,7 @@ #define _DEFAULT_SOURCE #include "os.h" -#include "tulog.h" +#include "ulog.h" #include "tglobal.h" #include "tconfig.h" #include "tutil.h" diff --git a/src/common/src/tmessage.c b/source/common/src/tmessage.c similarity index 100% rename from src/common/src/tmessage.c rename to source/common/src/tmessage.c diff --git a/src/common/src/tname.c b/source/common/src/tname.c similarity index 83% rename from src/common/src/tname.c rename to source/common/src/tname.c index 532333651df89ab16ce092e1b3d7c92806b8c883..c5bbda3c8c4a95de834114c9e8458d3478092b1b 100644 --- a/src/common/src/tname.c +++ b/source/common/src/tname.c @@ -2,22 +2,13 @@ #include "tutil.h" #include "tname.h" -#include "ttoken.h" -#include "tvariant.h" +#include "taosmsg.h" #define VALIDNUMOFCOLS(x) ((x) >= TSDB_MIN_COLUMNS && (x) <= TSDB_MAX_COLUMNS) #define VALIDNUMOFTAGS(x) ((x) >= 0 && (x) <= TSDB_MAX_TAGS) #define VALID_NAME_TYPE(x) ((x) == TSDB_DB_NAME_T || (x) == TSDB_TABLE_NAME_T) -//TODO remove it -void extractTableName(const char* tableId, char* name) { - size_t s1 = strcspn(tableId, &TS_PATH_DELIMITER[0]); - size_t s2 = strcspn(&tableId[s1 + 1], &TS_PATH_DELIMITER[0]); - - tstrncpy(name, &tableId[s1 + s2 + 2], TSDB_TABLE_NAME_LEN); -} - char* extractDBName(const char* tableId, char* name) { size_t offset1 = strcspn(tableId, &TS_PATH_DELIMITER[0]); size_t len = strcspn(&tableId[offset1 + 1], &TS_PATH_DELIMITER[0]); @@ -33,28 +24,6 @@ size_t tableIdPrefix(const char* name, char* prefix, int32_t len) { return strlen(prefix); } -SSchema tGetUserSpecifiedColumnSchema(tVariant* pVal, SStrToken* exprStr, const char* name) { - SSchema s = {0}; - - s.type = pVal->nType; - if (s.type == TSDB_DATA_TYPE_BINARY || s.type == TSDB_DATA_TYPE_NCHAR) { - s.bytes = (int16_t)(pVal->nLen + VARSTR_HEADER_SIZE); - } else { - s.bytes = tDataTypes[pVal->nType].bytes; - } - - s.colId = TSDB_UD_COLUMN_INDEX; - if (name != NULL) { - tstrncpy(s.name, name, sizeof(s.name)); - } else { - size_t tlen = MIN(sizeof(s.name), exprStr->n + 1); - tstrncpy(s.name, exprStr->z, tlen); - strdequote(s.name); - } - - return s; -} - bool tscValidateTableNameLength(size_t len) { return len < TSDB_TABLE_NAME_LEN; } @@ -155,30 +124,30 @@ int64_t taosGetIntervalStartTimestamp(int64_t startTime, int64_t slidingTime, in * tablePrefix.columnName * extract table name and save it in pTable, with only column name in pToken */ -void extractTableNameFromToken(SStrToken* pToken, SStrToken* pTable) { - const char sep = TS_PATH_DELIMITER[0]; - - if (pToken == pTable || pToken == NULL || pTable == NULL) { - return; - } - - char* r = strnchr(pToken->z, sep, pToken->n, false); - - if (r != NULL) { // record the table name token - pTable->n = (uint32_t)(r - pToken->z); - pTable->z = pToken->z; - - r += 1; - pToken->n -= (uint32_t)(r - pToken->z); - pToken->z = r; - } -} +//void extractTableNameFromToken(SStrToken* pToken, SStrToken* pTable) { +// const char sep = TS_PATH_DELIMITER[0]; +// +// if (pToken == pTable || pToken == NULL || pTable == NULL) { +// return; +// } +// +// char* r = strnchr(pToken->z, sep, pToken->n, false); +// +// if (r != NULL) { // record the table name token +// pTable->n = (uint32_t)(r - pToken->z); +// pTable->z = pToken->z; +// +// r += 1; +// pToken->n -= (uint32_t)(r - pToken->z); +// pToken->z = r; +// } +//} static struct SSchema _s = { .colId = TSDB_TBNAME_COLUMN_INDEX, .type = TSDB_DATA_TYPE_BINARY, .bytes = TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE, - .name = TSQL_TBNAME_L, + .name = "tbname", }; SSchema* tGetTbnameColumnSchema() { @@ -337,19 +306,19 @@ void tNameAssign(SName* dst, const SName* src) { memcpy(dst, src, sizeof(SName)); } -int32_t tNameSetDbName(SName* dst, const char* acct, SStrToken* dbToken) { - assert(dst != NULL && dbToken != NULL && acct != NULL); - - // too long account id or too long db name - if (strlen(acct) >= tListLen(dst->acctId) || dbToken->n >= tListLen(dst->dbname)) { - return -1; - } - - dst->type = TSDB_DB_NAME_T; - tstrncpy(dst->acctId, acct, tListLen(dst->acctId)); - tstrncpy(dst->dbname, dbToken->z, dbToken->n + 1); - return 0; -} +//int32_t tNameSetDbName(SName* dst, const char* acct, SStrToken* dbToken) { +// assert(dst != NULL && dbToken != NULL && acct != NULL); +// +// // too long account id or too long db name +// if (strlen(acct) >= tListLen(dst->acctId) || dbToken->n >= tListLen(dst->dbname)) { +// return -1; +// } +// +// dst->type = TSDB_DB_NAME_T; +// tstrncpy(dst->acctId, acct, tListLen(dst->acctId)); +// tstrncpy(dst->dbname, dbToken->z, dbToken->n + 1); +// return 0; +//} int32_t tNameSetAcctId(SName* dst, const char* acct) { assert(dst != NULL && acct != NULL); diff --git a/src/os/src/detail/osTime.c b/source/common/src/ttime.c similarity index 99% rename from src/os/src/detail/osTime.c rename to source/common/src/ttime.c index 5b2a01edc3e04ae5b9bb8e9df9c222368aac5c1b..0f15dcc13bb4d6465fc61ef4a15b08d04ae08677 100644 --- a/src/os/src/detail/osTime.c +++ b/source/common/src/ttime.c @@ -25,6 +25,7 @@ #include "os.h" #include "taosdef.h" +#include "ttime.h" #include "tutil.h" /* diff --git a/src/common/src/ttimezone.c b/source/common/src/ttimezone.c similarity index 99% rename from src/common/src/ttimezone.c rename to source/common/src/ttimezone.c index 62d4768db8a368daab105ac5abf6cf7607d5e52b..c45e39c20d458bff58d35c8e76848051617da8eb 100644 --- a/src/common/src/ttimezone.c +++ b/source/common/src/ttimezone.c @@ -15,7 +15,7 @@ #define _DEFAULT_SOURCE #include "os.h" -#include "tulog.h" +#include "ulog.h" #include "tglobal.h" #include "tconfig.h" #include "tutil.h" diff --git a/src/common/src/ttypes.c b/source/common/src/ttypes.c similarity index 90% rename from src/common/src/ttypes.c rename to source/common/src/ttypes.c index 08bfc2e9aa6f0b9337d484c725f2737cbbacaac0..82f6a8e49a5946882a4dbaa2f3b47900d9db6487 100644 --- a/src/common/src/ttypes.c +++ b/source/common/src/ttypes.c @@ -12,27 +12,27 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#include "os.h" -#include "ttype.h" -#include "ttokendef.h" -#include "tscompression.h" +#include "ttypes.h" +#include "../../../include/client/taos.h" +#include "os.h" +#include "tcompression.h" const int32_t TYPE_BYTES[15] = { -1, // TSDB_DATA_TYPE_NULL - sizeof(int8_t), // TSDB_DATA_TYPE_BOOL - sizeof(int8_t), // TSDB_DATA_TYPE_TINYINT - sizeof(int16_t), // TSDB_DATA_TYPE_SMALLINT - sizeof(int32_t), // TSDB_DATA_TYPE_INT + CHAR_BYTES, // TSDB_DATA_TYPE_BOOL + CHAR_BYTES, // TSDB_DATA_TYPE_TINYINT + SHORT_BYTES, // TSDB_DATA_TYPE_SMALLINT + INT_BYTES, // TSDB_DATA_TYPE_INT sizeof(int64_t), // TSDB_DATA_TYPE_BIGINT - sizeof(float), // TSDB_DATA_TYPE_FLOAT - sizeof(double), // TSDB_DATA_TYPE_DOUBLE + FLOAT_BYTES, // TSDB_DATA_TYPE_FLOAT + DOUBLE_BYTES, // TSDB_DATA_TYPE_DOUBLE sizeof(VarDataOffsetT), // TSDB_DATA_TYPE_BINARY sizeof(TSKEY), // TSDB_DATA_TYPE_TIMESTAMP sizeof(VarDataOffsetT), // TSDB_DATA_TYPE_NCHAR - sizeof(uint8_t), // TSDB_DATA_TYPE_UTINYINT - sizeof(uint16_t), // TSDB_DATA_TYPE_USMALLINT - sizeof(uint32_t), // TSDB_DATA_TYPE_UINT + CHAR_BYTES, // TSDB_DATA_TYPE_UTINYINT + SHORT_BYTES, // TSDB_DATA_TYPE_USMALLINT + INT_BYTES, // TSDB_DATA_TYPE_UINT sizeof(uint64_t), // TSDB_DATA_TYPE_UBIGINT }; @@ -58,7 +58,7 @@ static void getStatics_bool(const void *pData, int32_t numOfRow, int64_t *min, i *minIndex = 0; *maxIndex = 0; - ASSERT(numOfRow <= INT16_MAX); + assert(numOfRow <= INT16_MAX); for (int32_t i = 0; i < numOfRow; ++i) { if (data[i] == TSDB_DATA_BOOL_NULL) { @@ -78,7 +78,7 @@ static void getStatics_i8(const void *pData, int32_t numOfRow, int64_t *min, int *minIndex = 0; *maxIndex = 0; - ASSERT(numOfRow <= INT16_MAX); + assert(numOfRow <= INT16_MAX); for (int32_t i = 0; i < numOfRow; ++i) { if (((uint8_t)data[i]) == TSDB_DATA_TINYINT_NULL) { @@ -100,7 +100,7 @@ static void getStatics_u8(const void *pData, int32_t numOfRow, int64_t *min, int *minIndex = 0; *maxIndex = 0; - ASSERT(numOfRow <= INT16_MAX); + assert(numOfRow <= INT16_MAX); for (int32_t i = 0; i < numOfRow; ++i) { if (((uint8_t)data[i]) == TSDB_DATA_UTINYINT_NULL) { @@ -124,7 +124,7 @@ static void getStatics_i16(const void *pData, int32_t numOfRow, int64_t *min, in *minIndex = 0; *maxIndex = 0; - ASSERT(numOfRow <= INT16_MAX); + assert(numOfRow <= INT16_MAX); for (int32_t i = 0; i < numOfRow; ++i) { if (((uint16_t)data[i]) == TSDB_DATA_SMALLINT_NULL) { @@ -147,7 +147,7 @@ static void getStatics_u16(const void *pData, int32_t numOfRow, int64_t *min, in *minIndex = 0; *maxIndex = 0; - ASSERT(numOfRow <= INT16_MAX); + assert(numOfRow <= INT16_MAX); for (int32_t i = 0; i < numOfRow; ++i) { if (((uint16_t)data[i]) == TSDB_DATA_USMALLINT_NULL) { @@ -171,7 +171,7 @@ static void getStatics_i32(const void *pData, int32_t numOfRow, int64_t *min, in *minIndex = 0; *maxIndex = 0; - ASSERT(numOfRow <= INT16_MAX); + assert(numOfRow <= INT16_MAX); for (int32_t i = 0; i < numOfRow; ++i) { if (((uint32_t)data[i]) == TSDB_DATA_INT_NULL) { @@ -193,7 +193,7 @@ static void getStatics_u32(const void *pData, int32_t numOfRow, int64_t *min, in *minIndex = 0; *maxIndex = 0; - ASSERT(numOfRow <= INT16_MAX); + assert(numOfRow <= INT16_MAX); for (int32_t i = 0; i < numOfRow; ++i) { if (((uint32_t)data[i]) == TSDB_DATA_UINT_NULL) { @@ -217,7 +217,7 @@ static void getStatics_i64(const void *pData, int32_t numOfRow, int64_t *min, in *minIndex = 0; *maxIndex = 0; - ASSERT(numOfRow <= INT16_MAX); + assert(numOfRow <= INT16_MAX); for (int32_t i = 0; i < numOfRow; ++i) { if (((uint64_t)data[i]) == TSDB_DATA_BIGINT_NULL) { @@ -239,7 +239,7 @@ static void getStatics_u64(const void *pData, int32_t numOfRow, int64_t *min, in *minIndex = 0; *maxIndex = 0; - ASSERT(numOfRow <= INT16_MAX); + assert(numOfRow <= INT16_MAX); for (int32_t i = 0; i < numOfRow; ++i) { if (((uint64_t)data[i]) == TSDB_DATA_UBIGINT_NULL) { @@ -264,7 +264,7 @@ static void getStatics_f(const void *pData, int32_t numOfRow, int64_t *min, int6 *minIndex = 0; *maxIndex = 0; - ASSERT(numOfRow <= INT16_MAX); + assert(numOfRow <= INT16_MAX); for (int32_t i = 0; i < numOfRow; ++i) { if ((*(uint32_t*)&(data[i])) == TSDB_DATA_FLOAT_NULL) { @@ -300,7 +300,7 @@ static void getStatics_d(const void *pData, int32_t numOfRow, int64_t *min, int6 *minIndex = 0; *maxIndex = 0; - ASSERT(numOfRow <= INT16_MAX); + assert(numOfRow <= INT16_MAX); for (int32_t i = 0; i < numOfRow; ++i) { if ((*(uint64_t*)&(data[i])) == TSDB_DATA_DOUBLE_NULL) { @@ -330,7 +330,7 @@ static void getStatics_d(const void *pData, int32_t numOfRow, int64_t *min, int6 static void getStatics_bin(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, int64_t *sum, int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { const char* data = pData; - ASSERT(numOfRow <= INT16_MAX); + assert(numOfRow <= INT16_MAX); for (int32_t i = 0; i < numOfRow; ++i) { if (isNull(data, TSDB_DATA_TYPE_BINARY)) { @@ -350,7 +350,7 @@ static void getStatics_bin(const void *pData, int32_t numOfRow, int64_t *min, in static void getStatics_nchr(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, int64_t *sum, int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { const char* data = pData; - ASSERT(numOfRow <= INT16_MAX); + assert(numOfRow <= INT16_MAX); for (int32_t i = 0; i < numOfRow; ++i) { if (isNull(data, TSDB_DATA_TYPE_NCHAR)) { @@ -426,7 +426,6 @@ FORCE_INLINE void* getDataMax(int32_t type) { } } - bool isValidDataType(int32_t type) { return type >= TSDB_DATA_TYPE_NULL && type <= TSDB_DATA_TYPE_UBIGINT; } @@ -445,6 +444,8 @@ void setVardataNull(void* val, int32_t type) { void setNull(void *val, int32_t type, int32_t bytes) { setNullN(val, type, bytes, 1); } +#define POINTER_SHIFT(p, b) ((void *)((char *)(p) + (b))) + void setNullN(void *val, int32_t type, int32_t bytes, int32_t numOfElems) { switch (type) { case TSDB_DATA_TYPE_BOOL: @@ -532,11 +533,6 @@ static uint64_t nullBigIntu = TSDB_DATA_UBIGINT_NULL; static SBinaryNullT nullBinary = {1, TSDB_DATA_BINARY_NULL}; static SNCharNullT nullNchar = {4, TSDB_DATA_NCHAR_NULL}; -// static union { -// tstr str; -// char pad[sizeof(tstr) + 4]; -// } nullBinary = {.str = {.len = 1}}, nullNchar = {.str = {.len = 4}}; - static const void *nullValues[] = { &nullBool, &nullTinyInt, &nullSmallInt, &nullInt, &nullBigInt, &nullFloat, &nullDouble, &nullBinary, &nullBigInt, &nullNchar, @@ -634,6 +630,13 @@ void operateVal(void *dst, void *s1, void *s2, int32_t optr, int32_t type) { } } +#define SWAP(a, b, c) \ + do { \ + typeof(a) __tmp = (a); \ + (a) = (b); \ + (b) = __tmp; \ + } while (0) + void tsDataSwap(void *pLeft, void *pRight, int32_t type, int32_t size, void* buf) { switch (type) { @@ -679,49 +682,3 @@ void tsDataSwap(void *pLeft, void *pRight, int32_t type, int32_t size, void* buf } } } - -int32_t tStrToInteger(const char* z, int16_t type, int32_t n, int64_t* value, bool issigned) { - errno = 0; - int32_t ret = 0; - - char* endPtr = NULL; - if (type == TK_FLOAT) { - double v = strtod(z, &endPtr); - if ((errno == ERANGE && v == HUGE_VALF) || isinf(v) || isnan(v)) { - ret = -1; - } else if ((issigned && (v < INT64_MIN || v > INT64_MAX)) || ((!issigned) && (v < 0 || v > UINT64_MAX))) { - ret = -1; - } else { - *value = (int64_t) round(v); - } - - errno = 0; - return ret; - } - - int32_t radix = 10; - if (type == TK_HEX) { - radix = 16; - } else if (type == TK_BIN) { - radix = 2; - } - - // the string may be overflow according to errno - if (!issigned) { - const char *p = z; - while(*p != 0 && *p == ' ') p++; - if (*p != 0 && *p == '-') { return -1;} - - *value = strtoull(z, &endPtr, radix); - } else { - *value = strtoll(z, &endPtr, radix); - } - - // not a valid integer number, return error - if (endPtr - z != n || errno == ERANGE) { - ret = -1; - } - - errno = 0; - return ret; -} diff --git a/src/common/src/tvariant.c b/source/common/src/tvariant.c similarity index 89% rename from src/common/src/tvariant.c rename to source/common/src/tvariant.c index ca3bb956a2fef4fa98450181b4378025013bb735..9216183b88f2578e8b0ac2dfc840a6cab760dbe0 100644 --- a/src/common/src/tvariant.c +++ b/source/common/src/tvariant.c @@ -14,12 +14,12 @@ */ #include "os.h" -#include "hash.h" #include "taos.h" +#include "thash.h" #include "taosdef.h" -#include "ttoken.h" +#include "ttime.h" #include "ttokendef.h" -#include "ttype.h" +#include "ttypes.h" #include "tutil.h" #include "tvariant.h" @@ -30,17 +30,15 @@ assert(0); \ } while (0) -void tVariantCreate(tVariant *pVar, SStrToken *token) { +void taosVariantCreate(SVariant *pVar, char* z, int32_t n, int32_t type) { int32_t ret = 0; - int32_t type = token->type; + memset(pVar, 0, sizeof(SVariant)); - memset(pVar, 0, sizeof(tVariant)); - - switch (token->type) { + switch (type) { case TSDB_DATA_TYPE_BOOL: { - if (strncasecmp(token->z, "true", 4) == 0) { + if (strncasecmp(z, "true", 4) == 0) { pVar->i64 = TSDB_TRUE; - } else if (strncasecmp(token->z, "false", 5) == 0) { + } else if (strncasecmp(z, "false", 5) == 0) { pVar->i64 = TSDB_FALSE; } else { return; @@ -53,35 +51,35 @@ void tVariantCreate(tVariant *pVar, SStrToken *token) { case TSDB_DATA_TYPE_SMALLINT: case TSDB_DATA_TYPE_BIGINT: case TSDB_DATA_TYPE_INT:{ - ret = tStrToInteger(token->z, token->type, token->n, &pVar->i64, true); - if (ret != 0) { - SStrToken t = {0}; - tGetToken(token->z, &t.type); - if (t.type == TK_MINUS) { // it is a signed number which is greater than INT64_MAX or less than INT64_MIN - pVar->nType = -1; // -1 means error type - return; - } - - // data overflow, try unsigned parse the input number - ret = tStrToInteger(token->z, token->type, token->n, &pVar->i64, false); - if (ret != 0) { - pVar->nType = -1; // -1 means error type - return; - } - } +// ret = tStrToInteger(token->z, token->type, token->n, &pVar->i64, true); +// if (ret != 0) { +// SToken t = {0}; +// tGetToken(token->z, &t.type); +// if (t.type == TK_MINUS) { // it is a signed number which is greater than INT64_MAX or less than INT64_MIN +// pVar->nType = -1; // -1 means error type +// return; +// } +// +// // data overflow, try unsigned parse the input number +// ret = tStrToInteger(token->z, token->type, token->n, &pVar->i64, false); +// if (ret != 0) { +// pVar->nType = -1; // -1 means error type +// return; +// } +// } break; } case TSDB_DATA_TYPE_DOUBLE: case TSDB_DATA_TYPE_FLOAT: { - pVar->dKey = strtod(token->z, NULL); + pVar->d = strtod(z, NULL); break; } case TSDB_DATA_TYPE_BINARY: { - pVar->pz = strndup(token->z, token->n); - pVar->nLen = strRmquote(pVar->pz, token->n); + pVar->pz = strndup(z, n); + pVar->nLen = strRmquote(pVar->pz, n); break; } case TSDB_DATA_TYPE_TIMESTAMP: { @@ -97,14 +95,15 @@ void tVariantCreate(tVariant *pVar, SStrToken *token) { pVar->nType = type; } + /** - * create tVariant from binary string, not ascii data + * create SVariant from binary string, not ascii data * @param pVar * @param pz * @param len * @param type */ -void tVariantCreateFromBinary(tVariant *pVar, const char *pz, size_t len, uint32_t type) { +void taosVariantCreateFromBinary(SVariant *pVar, const char *pz, size_t len, uint32_t type) { switch (type) { case TSDB_DATA_TYPE_BOOL: case TSDB_DATA_TYPE_TINYINT: { @@ -150,12 +149,12 @@ void tVariantCreateFromBinary(tVariant *pVar, const char *pz, size_t len, uint32 } case TSDB_DATA_TYPE_DOUBLE: { pVar->nLen = tDataTypes[type].bytes; - pVar->dKey = GET_DOUBLE_VAL(pz); + pVar->d = GET_DOUBLE_VAL(pz); break; } case TSDB_DATA_TYPE_FLOAT: { pVar->nLen = tDataTypes[type].bytes; - pVar->dKey = GET_FLOAT_VAL(pz); + pVar->d = GET_FLOAT_VAL(pz); break; } case TSDB_DATA_TYPE_NCHAR: { // here we get the nchar length from raw binary bits length @@ -182,7 +181,7 @@ void tVariantCreateFromBinary(tVariant *pVar, const char *pz, size_t len, uint32 pVar->nType = type; } -void tVariantDestroy(tVariant *pVar) { +void taosVariantDestroy(SVariant *pVar) { if (pVar == NULL) return; if (pVar->nType == TSDB_DATA_TYPE_BINARY || pVar->nType == TSDB_DATA_TYPE_NCHAR) { @@ -205,12 +204,12 @@ void tVariantDestroy(tVariant *pVar) { } } -bool tVariantIsValid(tVariant *pVar) { +bool taosVariantIsValid(SVariant *pVar) { assert(pVar != NULL); return isValidDataType(pVar->nType); } -void tVariantAssign(tVariant *pDst, const tVariant *pSrc) { +void taosVariantAssign(SVariant *pDst, const SVariant *pSrc) { if (pSrc == NULL || pDst == NULL) return; pDst->nType = pSrc->nType; @@ -254,7 +253,7 @@ void tVariantAssign(tVariant *pDst, const tVariant *pSrc) { } } -int32_t tVariantCompare(const tVariant* p1, const tVariant* p2) { +int32_t taosVariantCompare(const SVariant* p1, const SVariant* p2) { if (p1->nType == TSDB_DATA_TYPE_NULL && p2->nType == TSDB_DATA_TYPE_NULL) { return 0; } @@ -274,10 +273,10 @@ int32_t tVariantCompare(const tVariant* p1, const tVariant* p2) { return p1->nLen > p2->nLen? 1:-1; } } else if (p1->nType == TSDB_DATA_TYPE_FLOAT || p1->nType == TSDB_DATA_TYPE_DOUBLE) { - if (p1->dKey == p2->dKey) { + if (p1->d == p2->d) { return 0; } else { - return p1->dKey > p2->dKey? 1:-1; + return p1->d > p2->d? 1:-1; } } else if (IS_UNSIGNED_NUMERIC_TYPE(p1->nType)) { if (p1->u64 == p2->u64) { @@ -294,7 +293,7 @@ int32_t tVariantCompare(const tVariant* p1, const tVariant* p2) { } } -int32_t tVariantToString(tVariant *pVar, char *dst) { +int32_t taosVariantToString(SVariant *pVar, char *dst) { if (pVar == NULL || dst == NULL) return 0; switch (pVar->nType) { @@ -328,7 +327,7 @@ int32_t tVariantToString(tVariant *pVar, char *dst) { return sprintf(dst, "%" PRIu64, pVar->u64); case TSDB_DATA_TYPE_FLOAT: case TSDB_DATA_TYPE_DOUBLE: - return sprintf(dst, "%.9lf", pVar->dKey); + return sprintf(dst, "%.9lf", pVar->d); default: return 0; @@ -359,11 +358,11 @@ static FORCE_INLINE int32_t wcsconvertToBoolImpl(wchar_t *pstr, int32_t len) { } } -static int32_t toBinary(tVariant *pVariant, char **pDest, int32_t *pDestSize) { +static int32_t toBinary(SVariant *pVariant, char **pDest, int32_t *pDestSize) { const int32_t INITIAL_ALLOC_SIZE = 40; char * pBuf = NULL; - // it is a in-place convert type for tVariant, local buffer is needed + // it is a in-place convert type for SVariant, local buffer is needed if (*pDest == pVariant->pz) { pBuf = calloc(1, INITIAL_ALLOC_SIZE); } @@ -386,7 +385,7 @@ static int32_t toBinary(tVariant *pVariant, char **pDest, int32_t *pDestSize) { if (IS_SIGNED_NUMERIC_TYPE(pVariant->nType)) { sprintf(pBuf == NULL ? *pDest : pBuf, "%" PRId64, pVariant->i64); } else if (pVariant->nType == TSDB_DATA_TYPE_DOUBLE || pVariant->nType == TSDB_DATA_TYPE_FLOAT) { - sprintf(pBuf == NULL ? *pDest : pBuf, "%lf", pVariant->dKey); + sprintf(pBuf == NULL ? *pDest : pBuf, "%lf", pVariant->d); } else if (pVariant->nType == TSDB_DATA_TYPE_BOOL) { sprintf(pBuf == NULL ? *pDest : pBuf, "%s", (pVariant->i64 == TSDB_TRUE) ? "TRUE" : "FALSE"); } else if (pVariant->nType == 0) { // null data @@ -402,7 +401,7 @@ static int32_t toBinary(tVariant *pVariant, char **pDest, int32_t *pDestSize) { return 0; } -static int32_t toNchar(tVariant *pVariant, char **pDest, int32_t *pDestSize) { +static int32_t toNchar(SVariant *pVariant, char **pDest, int32_t *pDestSize) { char tmpBuf[40] = {0}; char * pDst = tmpBuf; @@ -414,7 +413,7 @@ static int32_t toNchar(tVariant *pVariant, char **pDest, int32_t *pDestSize) { } else if (IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) { nLen = sprintf(pDst, "%"PRIu64, pVariant->u64); } else if (pVariant->nType == TSDB_DATA_TYPE_DOUBLE || pVariant->nType == TSDB_DATA_TYPE_FLOAT) { - nLen = sprintf(pDst, "%lf", pVariant->dKey); + nLen = sprintf(pDst, "%lf", pVariant->d); } else if (pVariant->nType == TSDB_DATA_TYPE_BINARY) { pDst = pVariant->pz; nLen = pVariant->nLen; @@ -460,30 +459,30 @@ static int32_t toNchar(tVariant *pVariant, char **pDest, int32_t *pDestSize) { } static FORCE_INLINE int32_t convertToDouble(char *pStr, int32_t len, double *value) { - SStrToken stoken = {.z = pStr, .n = len}; - if (TK_ILLEGAL == tGetNumericStringType(&stoken)) { - return -1; - } - - *value = strtod(pStr, NULL); +// SToken stoken = {.z = pStr, .n = len}; +// if (TK_ILLEGAL == tGetNumericStringType(&stoken)) { +// return -1; +// } +// +// *value = strtod(pStr, NULL); return 0; } -static FORCE_INLINE int32_t convertToInteger(tVariant *pVariant, int64_t *result, int32_t type, bool issigned, bool releaseVariantPtr, bool *converted) { +static FORCE_INLINE int32_t convertToInteger(SVariant *pVariant, int64_t *result, int32_t type, bool issigned, bool releaseVariantPtr, bool *converted) { if (pVariant->nType == TSDB_DATA_TYPE_NULL) { setNull((char *)result, type, tDataTypes[type].bytes); return 0; } - +#if 0 errno = 0; if (IS_SIGNED_NUMERIC_TYPE(pVariant->nType) || (pVariant->nType == TSDB_DATA_TYPE_BOOL)) { *result = pVariant->i64; } else if (IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) { *result = pVariant->u64; } else if (IS_FLOAT_TYPE(pVariant->nType)) { - *result = (int64_t) pVariant->dKey; + *result = (int64_t) pVariant->d; } else if (pVariant->nType == TSDB_DATA_TYPE_BINARY) { - SStrToken token = {.z = pVariant->pz, .n = pVariant->nLen}; + SToken token = {.z = pVariant->pz, .n = pVariant->nLen}; /*int32_t n = */tGetToken(pVariant->pz, &token.type); if (token.type == TK_NULL) { @@ -518,7 +517,7 @@ static FORCE_INLINE int32_t convertToInteger(tVariant *pVariant, int64_t *result errno = 0; wchar_t *endPtr = NULL; - SStrToken token = {0}; + SToken token = {0}; token.n = tGetToken(pVariant->pz, &token.type); if (token.type == TK_MINUS || token.type == TK_PLUS) { @@ -589,16 +588,19 @@ static FORCE_INLINE int32_t convertToInteger(tVariant *pVariant, int64_t *result code = IS_VALID_UBIGINT(ui); break; } + return code? 0:-1; +#endif + return 0; } -static int32_t convertToBool(tVariant *pVariant, int64_t *pDest) { +static int32_t convertToBool(SVariant *pVariant, int64_t *pDest) { if (pVariant->nType == TSDB_DATA_TYPE_BOOL) { *pDest = pVariant->i64; // in order to be compatible to null of bool } else if (IS_NUMERIC_TYPE(pVariant->nType)) { *pDest = ((pVariant->i64 != 0) ? TSDB_TRUE : TSDB_FALSE); } else if (pVariant->nType == TSDB_DATA_TYPE_FLOAT || pVariant->nType == TSDB_DATA_TYPE_DOUBLE) { - *pDest = ((pVariant->dKey != 0) ? TSDB_TRUE : TSDB_FALSE); + *pDest = ((pVariant->d != 0) ? TSDB_TRUE : TSDB_FALSE); } else if (pVariant->nType == TSDB_DATA_TYPE_BINARY) { int32_t ret = 0; if ((ret = convertToBoolImpl(pVariant->pz, pVariant->nLen)) < 0) { @@ -624,15 +626,7 @@ static int32_t convertToBool(tVariant *pVariant, int64_t *pDest) { * transfer data from variant serve as the implicit data conversion: from input sql string pVariant->nType * to column type defined in schema */ -int32_t tVariantDump(tVariant *pVariant, char *payload, int16_t type, bool includeLengthPrefix) { - return tVariantDumpEx(pVariant, payload, type, includeLengthPrefix, NULL, NULL); -} - -/* - * transfer data from variant serve as the implicit data conversion: from input sql string pVariant->nType - * to column type defined in schema - */ -int32_t tVariantDumpEx(tVariant *pVariant, char *payload, int16_t type, bool includeLengthPrefix, bool *converted, char *extInfo) { +int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool includeLengthPrefix, bool *converted, char *extInfo) { if (converted) { *converted = false; } @@ -765,12 +759,12 @@ int32_t tVariantDumpEx(tVariant *pVariant, char *payload, int16_t type, bool inc *converted = true; } - if (pVariant->dKey > FLT_MAX || pVariant->dKey < -FLT_MAX) { - SET_EXT_INFO(converted, pVariant->dKey, -FLT_MAX, FLT_MAX, extInfo); + if (pVariant->d > FLT_MAX || pVariant->d < -FLT_MAX) { + SET_EXT_INFO(converted, pVariant->d, -FLT_MAX, FLT_MAX, extInfo); return -1; } - SET_FLOAT_VAL(payload, pVariant->dKey); + SET_FLOAT_VAL(payload, pVariant->d); } else if (pVariant->nType == TSDB_DATA_TYPE_NULL) { *((uint32_t *)payload) = TSDB_DATA_FLOAT_NULL; return 0; @@ -801,7 +795,7 @@ int32_t tVariantDumpEx(tVariant *pVariant, char *payload, int16_t type, bool inc } else if (pVariant->nType == TSDB_DATA_TYPE_BOOL || IS_SIGNED_NUMERIC_TYPE(pVariant->nType) || IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) { SET_DOUBLE_VAL(payload, pVariant->i64); } else if (IS_FLOAT_TYPE(pVariant->nType)) { - SET_DOUBLE_VAL(payload, pVariant->dKey); + SET_DOUBLE_VAL(payload, pVariant->d); } else if (pVariant->nType == TSDB_DATA_TYPE_NULL) { *((int64_t *)payload) = TSDB_DATA_DOUBLE_NULL; return 0; @@ -893,6 +887,13 @@ int32_t tVariantDumpEx(tVariant *pVariant, char *payload, int16_t type, bool inc return 0; } +/* + * transfer data from variant serve as the implicit data conversion: from input sql string pVariant->nType + * to column type defined in schema + */ +int32_t taosVariantDump(SVariant *pVariant, char *payload, int16_t type, bool includeLengthPrefix) { + return tVariantDumpEx(pVariant, payload, type, includeLengthPrefix, NULL, NULL); +} /* * In variant, bool/smallint/tinyint/int/bigint share the same attribution of @@ -900,7 +901,7 @@ int32_t tVariantDumpEx(tVariant *pVariant, char *payload, int16_t type, bool inc * * It is actually the bigint/binary/bool/nchar type transfer */ -int32_t tVariantTypeSetType(tVariant *pVariant, char type) { +int32_t tVariantTypeSetType(SVariant *pVariant, char type) { if (pVariant == NULL || pVariant->nType == 0) { // value is not set return 0; } @@ -933,7 +934,7 @@ int32_t tVariantTypeSetType(tVariant *pVariant, char type) { } free(pVariant->pz); - pVariant->dKey = v; + pVariant->d = v; } else if (pVariant->nType == TSDB_DATA_TYPE_NCHAR) { errno = 0; double v = wcstod(pVariant->wpz, NULL); @@ -943,10 +944,10 @@ int32_t tVariantTypeSetType(tVariant *pVariant, char type) { } free(pVariant->pz); - pVariant->dKey = v; + pVariant->d = v; } else if (pVariant->nType >= TSDB_DATA_TYPE_BOOL && pVariant->nType <= TSDB_DATA_TYPE_BIGINT) { double tmp = (double) pVariant->i64; - pVariant->dKey = tmp; + pVariant->d = tmp; } pVariant->nType = TSDB_DATA_TYPE_DOUBLE; @@ -971,4 +972,4 @@ int32_t tVariantTypeSetType(tVariant *pVariant, char type) { } return 0; -} +} \ No newline at end of file diff --git a/source/libs/catalog/CMakeLists.txt b/source/libs/catalog/CMakeLists.txt index ae331bf3926b462bc069d7e013828e8195b4f02c..ff3e62700a7a9acc1a89bece0c5e288c67c014df 100644 --- a/source/libs/catalog/CMakeLists.txt +++ b/source/libs/catalog/CMakeLists.txt @@ -4,4 +4,9 @@ target_include_directories( catalog PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/catalog" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" +) + +target_link_libraries( + catalog + PRIVATE os util common transport ) \ No newline at end of file diff --git a/source/libs/catalog/inc/catalogInt.h b/source/libs/catalog/inc/catalogInt.h index f6f8a61fafce0a6fe3eeaa48e93755ec9b0de3be..5b50bbff4ccde745697d68223d2299b94f0bc41b 100644 --- a/source/libs/catalog/inc/catalogInt.h +++ b/source/libs/catalog/inc/catalogInt.h @@ -20,6 +20,13 @@ extern "C" { #endif +#include "catalog.h" + +typedef struct SCatalog { + void *pMsgSender; // used to send messsage to mnode to fetch necessary metadata + SHashObj *pData; // items cached for each cluster, the hash key is the cluster-id, returned by mgmt node +} SCatalog; + #ifdef __cplusplus } #endif diff --git a/source/libs/catalog/src/catalog.c b/source/libs/catalog/src/catalog.c index 6dea4a4e57392be988126c579648f39a8270b9bf..c553e1bfbf8539c5921fd84954f61899446e90c1 100644 --- a/source/libs/catalog/src/catalog.c +++ b/source/libs/catalog/src/catalog.c @@ -11,4 +11,6 @@ * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . - */ \ No newline at end of file + */ + +#include "catalogInt.h" diff --git a/source/libs/executor/CMakeLists.txt b/source/libs/executor/CMakeLists.txt index ae0cc6b1a89a6346e149c07a57ae2ab7ff897b85..9f02f4a4c76f861a1a08e824bfd90a4a35465742 100644 --- a/source/libs/executor/CMakeLists.txt +++ b/source/libs/executor/CMakeLists.txt @@ -4,4 +4,9 @@ target_include_directories( executor PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/executor" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" +) + +target_link_libraries( + executor + PRIVATE os util common ) \ No newline at end of file diff --git a/src/common/inc/tarithoperator.h b/source/libs/executor/inc/tarithoperator.h similarity index 88% rename from src/common/inc/tarithoperator.h rename to source/libs/executor/inc/tarithoperator.h index 27e8871e2f7b32bf7772dfa0cbc9c3164a312f2d..e47cb5c1cb41c8ff763fd0b9efa6f1343b523fb8 100644 --- a/src/common/inc/tarithoperator.h +++ b/source/libs/executor/inc/tarithoperator.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_QARITHMETICOPERATOR_H -#define TDENGINE_QARITHMETICOPERATOR_H +#ifndef _TD_COMMON_QARITHMETICOPERATOR_H_ +#define _TD_COMMON_QARITHMETICOPERATOR_H_ #ifdef __cplusplus extern "C" { @@ -29,4 +29,4 @@ _arithmetic_operator_fn_t getArithmeticOperatorFn(int32_t arithmeticOptr); } #endif -#endif // TDENGINE_TSYNTAXTREEFUNCTION_H +#endif /*_TD_COMMON_QARITHMETICOPERATOR_H_*/ diff --git a/src/common/inc/texpr.h b/source/libs/executor/inc/texpr.h similarity index 94% rename from src/common/inc/texpr.h rename to source/libs/executor/inc/texpr.h index 0ede2e1e6f71c69aa6aaed2c05d671840679ce5c..d723549d49f9bb1eafa16f9a965d3d162c90582f 100644 --- a/src/common/inc/texpr.h +++ b/source/libs/executor/inc/texpr.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TEXPR_H -#define TDENGINE_TEXPR_H +#ifndef _TD_COMMON_EXPR_H_ +#define _TD_COMMON_EXPR_H_ #ifdef __cplusplus extern "C" { @@ -26,7 +26,6 @@ extern "C" { #include "taosdef.h" #include "tskiplist.h" #include "tbuffer.h" -#include "tvariant.h" struct tExprNode; struct SSchema; @@ -73,8 +72,8 @@ typedef struct tExprNode { struct tExprNode *pRight; // right child pointer } _node; - struct SSchema *pSchema; - tVariant *pVal; + SSchema *pSchema; + struct SVariant *pVal; }; } tExprNode; @@ -104,4 +103,4 @@ void buildFilterSetFromBinary(void **q, const char *buf, int32_t len); } #endif -#endif // TDENGINE_TEXPR_H +#endif /*_TD_COMMON_EXPR_H_*/ diff --git a/src/common/src/tarithoperator.c b/source/libs/executor/src/tarithoperator.c similarity index 99% rename from src/common/src/tarithoperator.c rename to source/libs/executor/src/tarithoperator.c index 000ef79fcf9b5ee9e52dae65b99f719cec6a8059..6789eb59166b1af5b6935b90998b16a9035bfd85 100644 --- a/src/common/src/tarithoperator.c +++ b/source/libs/executor/src/tarithoperator.c @@ -15,8 +15,7 @@ #include "os.h" -#include "ttype.h" -#include "tutil.h" +#include "ttypes.h" #include "tarithoperator.h" #include "tcompare.h" diff --git a/src/common/src/texpr.c b/source/libs/executor/src/texpr.c similarity index 94% rename from src/common/src/texpr.c rename to source/libs/executor/src/texpr.c index 6823de631fe65a27f527be011def4819dd77c73a..7e2797743d4788a8eb9b254c740802916f140813 100644 --- a/src/common/src/texpr.c +++ b/source/libs/executor/src/texpr.c @@ -13,7 +13,6 @@ * along with this program. If not, see . */ -#include #include "os.h" #include "texpr.h" @@ -23,10 +22,12 @@ #include "tarray.h" #include "tbuffer.h" #include "tcompare.h" -#include "tsdb.h" +#include "tname.h" +#include "thash.h" #include "tskiplist.h" #include "texpr.h" #include "tarithoperator.h" +#include "tvariant.h" static uint8_t UNUSED_FUNC isQueryOnPrimaryKey(const char *primaryColumnName, const tExprNode *pLeft, const tExprNode *pRight) { if (pLeft->nodeType == TSQL_NODE_COL) { @@ -116,7 +117,7 @@ void tExprTreeDestroy(tExprNode *pNode, void (*fp)(void *)) { if (pNode->nodeType == TSQL_NODE_EXPR) { doExprTreeDestroy(&pNode, fp); } else if (pNode->nodeType == TSQL_NODE_VALUE) { - tVariantDestroy(pNode->pVal); + taosVariantDestroy(pNode->pVal); } else if (pNode->nodeType == TSQL_NODE_COL) { tfree(pNode->pSchema); } @@ -137,7 +138,7 @@ static void doExprTreeDestroy(tExprNode **pExpr, void (*fp)(void *)) { fp((*pExpr)->_node.info); } } else if ((*pExpr)->nodeType == TSQL_NODE_VALUE) { - tVariantDestroy((*pExpr)->pVal); + taosVariantDestroy((*pExpr)->pVal); free((*pExpr)->pVal); } else if ((*pExpr)->nodeType == TSQL_NODE_COL) { free((*pExpr)->pSchema); @@ -285,7 +286,7 @@ static void exprTreeToBinaryImpl(SBufferWriter* bw, tExprNode* expr) { tbufWriteUint8(bw, expr->nodeType); if (expr->nodeType == TSQL_NODE_VALUE) { - tVariant* pVal = expr->pVal; + SVariant* pVal = expr->pVal; tbufWriteUint32(bw, pVal->nType); if (pVal->nType == TSDB_DATA_TYPE_BINARY) { @@ -353,7 +354,7 @@ static tExprNode* exprTreeFromBinaryImpl(SBufferReader* br) { pExpr->nodeType = tbufReadUint8(br); if (pExpr->nodeType == TSQL_NODE_VALUE) { - tVariant* pVal = exception_calloc(1, sizeof(tVariant)); + SVariant* pVal = exception_calloc(1, sizeof(SVariant)); pExpr->pVal = pVal; pVal->nType = tbufReadUint32(br); @@ -414,7 +415,7 @@ tExprNode* exprTreeFromTableName(const char* tbnameCond) { SSchema* pSchema = exception_calloc(1, sizeof(SSchema)); left->pSchema = pSchema; - *pSchema = *tGetTbnameColumnSchema(); +// *pSchema = NULL;//*tGetTbnameColumnSchema(); tExprNode* right = exception_calloc(1, sizeof(tExprNode)); expr->_node.pRight = right; @@ -422,7 +423,7 @@ tExprNode* exprTreeFromTableName(const char* tbnameCond) { if (strncmp(tbnameCond, QUERY_COND_REL_PREFIX_LIKE, QUERY_COND_REL_PREFIX_LIKE_LEN) == 0) { right->nodeType = TSQL_NODE_VALUE; expr->_node.optr = TSDB_RELATION_LIKE; - tVariant* pVal = exception_calloc(1, sizeof(tVariant)); + SVariant* pVal = exception_calloc(1, sizeof(SVariant)); right->pVal = pVal; size_t len = strlen(tbnameCond + QUERY_COND_REL_PREFIX_LIKE_LEN) + 1; pVal->pz = exception_malloc(len); @@ -433,7 +434,7 @@ tExprNode* exprTreeFromTableName(const char* tbnameCond) { } else if (strncmp(tbnameCond, QUERY_COND_REL_PREFIX_MATCH, QUERY_COND_REL_PREFIX_MATCH_LEN) == 0) { right->nodeType = TSQL_NODE_VALUE; expr->_node.optr = TSDB_RELATION_MATCH; - tVariant* pVal = exception_calloc(1, sizeof(tVariant)); + SVariant* pVal = exception_calloc(1, sizeof(SVariant)); right->pVal = pVal; size_t len = strlen(tbnameCond + QUERY_COND_REL_PREFIX_MATCH_LEN) + 1; pVal->pz = exception_malloc(len); @@ -443,7 +444,7 @@ tExprNode* exprTreeFromTableName(const char* tbnameCond) { } else if (strncmp(tbnameCond, QUERY_COND_REL_PREFIX_NMATCH, QUERY_COND_REL_PREFIX_NMATCH_LEN) == 0) { right->nodeType = TSQL_NODE_VALUE; expr->_node.optr = TSDB_RELATION_NMATCH; - tVariant* pVal = exception_calloc(1, sizeof(tVariant)); + SVariant* pVal = exception_calloc(1, sizeof(SVariant)); right->pVal = pVal; size_t len = strlen(tbnameCond + QUERY_COND_REL_PREFIX_NMATCH_LEN) + 1; pVal->pz = exception_malloc(len); @@ -453,7 +454,7 @@ tExprNode* exprTreeFromTableName(const char* tbnameCond) { } else if (strncmp(tbnameCond, QUERY_COND_REL_PREFIX_IN, QUERY_COND_REL_PREFIX_IN_LEN) == 0) { right->nodeType = TSQL_NODE_VALUE; expr->_node.optr = TSDB_RELATION_IN; - tVariant* pVal = exception_calloc(1, sizeof(tVariant)); + SVariant* pVal = exception_calloc(1, sizeof(SVariant)); right->pVal = pVal; pVal->nType = TSDB_DATA_TYPE_POINTER_ARRAY; pVal->arr = taosArrayInit(2, POINTER_BYTES); @@ -491,8 +492,8 @@ void buildFilterSetFromBinary(void **q, const char *buf, int32_t len) { uint32_t type = tbufReadUint32(&br); SHashObj *pObj = taosHashInit(256, taosGetDefaultHashFunction(type), true, false); - taosHashSetEqualFp(pObj, taosGetDefaultEqualFunction(type)); - +// taosHashSetEqualFp(pObj, taosGetDefaultEqualFunction(type)); + int dummy = -1; int32_t sz = tbufReadInt32(&br); for (int32_t i = 0; i < sz; i++) { @@ -527,10 +528,10 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t uint32_t sType = tbufReadUint32(&br); SHashObj *pObj = taosHashInit(256, taosGetDefaultHashFunction(tType), true, false); - taosHashSetEqualFp(pObj, taosGetDefaultEqualFunction(tType)); +// taosHashSetEqualFp(pObj, taosGetDefaultEqualFunction(tType)); int dummy = -1; - tVariant tmpVar = {0}; + SVariant tmpVar = {0}; size_t t = 0; int32_t sz = tbufReadInt32(&br); void *pvar = NULL; @@ -602,7 +603,7 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t return; } - tVariantCreateFromBinary(&tmpVar, (char *)pvar, t, sType); + taosVariantCreateFromBinary(&tmpVar, (char *)pvar, t, sType); if (bufLen < t) { tmp = realloc(tmp, t * TSDB_NCHAR_SIZE); @@ -613,7 +614,7 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t case TSDB_DATA_TYPE_BOOL: case TSDB_DATA_TYPE_UTINYINT: case TSDB_DATA_TYPE_TINYINT: { - if (tVariantDump(&tmpVar, (char *)&val, tType, false)) { + if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) { goto err_ret; } pvar = &val; @@ -622,7 +623,7 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t } case TSDB_DATA_TYPE_USMALLINT: case TSDB_DATA_TYPE_SMALLINT: { - if (tVariantDump(&tmpVar, (char *)&val, tType, false)) { + if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) { goto err_ret; } pvar = &val; @@ -631,7 +632,7 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t } case TSDB_DATA_TYPE_UINT: case TSDB_DATA_TYPE_INT: { - if (tVariantDump(&tmpVar, (char *)&val, tType, false)) { + if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) { goto err_ret; } pvar = &val; @@ -641,7 +642,7 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t case TSDB_DATA_TYPE_TIMESTAMP: case TSDB_DATA_TYPE_UBIGINT: case TSDB_DATA_TYPE_BIGINT: { - if (tVariantDump(&tmpVar, (char *)&val, tType, false)) { + if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) { goto err_ret; } pvar = &val; @@ -649,7 +650,7 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t break; } case TSDB_DATA_TYPE_DOUBLE: { - if (tVariantDump(&tmpVar, (char *)&val, tType, false)) { + if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) { goto err_ret; } pvar = &val; @@ -657,7 +658,7 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t break; } case TSDB_DATA_TYPE_FLOAT: { - if (tVariantDump(&tmpVar, (char *)&val, tType, false)) { + if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) { goto err_ret; } pvar = &val; @@ -665,7 +666,7 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t break; } case TSDB_DATA_TYPE_BINARY: { - if (tVariantDump(&tmpVar, tmp, tType, true)) { + if (taosVariantDump(&tmpVar, tmp, tType, true)) { goto err_ret; } t = varDataLen(tmp); @@ -673,7 +674,7 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t break; } case TSDB_DATA_TYPE_NCHAR: { - if (tVariantDump(&tmpVar, tmp, tType, true)) { + if (taosVariantDump(&tmpVar, tmp, tType, true)) { goto err_ret; } t = varDataLen(tmp); @@ -685,7 +686,7 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t } taosHashPut(pObj, (char *)pvar, t, &dummy, sizeof(dummy)); - tVariantDestroy(&tmpVar); + taosVariantDestroy(&tmpVar); memset(&tmpVar, 0, sizeof(tmpVar)); } @@ -693,7 +694,7 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t pObj = NULL; err_ret: - tVariantDestroy(&tmpVar); + taosVariantDestroy(&tmpVar); taosHashCleanup(pObj); tfree(tmp); } @@ -714,8 +715,8 @@ tExprNode* exprdup(tExprNode* pNode) { pCloned->_node.optr = pNode->_node.optr; pCloned->_node.hasPK = pNode->_node.hasPK; } else if (pNode->nodeType == TSQL_NODE_VALUE) { - pCloned->pVal = calloc(1, sizeof(tVariant)); - tVariantAssign(pCloned->pVal, pNode->pVal); + pCloned->pVal = calloc(1, sizeof(SVariant)); + taosVariantAssign(pCloned->pVal, pNode->pVal); } else if (pNode->nodeType == TSQL_NODE_COL) { pCloned->pSchema = calloc(1, sizeof(SSchema)); *pCloned->pSchema = *pNode->pSchema; diff --git a/source/libs/parser/CMakeLists.txt b/source/libs/parser/CMakeLists.txt index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..8f6d8d2880890a59e03ef2ec1fde7e49ebf76719 100644 --- a/source/libs/parser/CMakeLists.txt +++ b/source/libs/parser/CMakeLists.txt @@ -0,0 +1,12 @@ +aux_source_directory(src PARSER_SRC) +add_library(parser ${PARSER_SRC}) +target_include_directories( + parser + PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/parser" + PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" +) + +target_link_libraries( + parser + PRIVATE os util common catalog transport +) \ No newline at end of file diff --git a/source/libs/parser/inc/astGenerator.h b/source/libs/parser/inc/astGenerator.h new file mode 100644 index 0000000000000000000000000000000000000000..af489a4e37e14020ad6d301300679962b7dfc963 --- /dev/null +++ b/source/libs/parser/inc/astGenerator.h @@ -0,0 +1,360 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef TDENGINE_ASTGENERATOR_H +#define TDENGINE_ASTGENERATOR_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ttoken.h" +#include "tvariant.h" +#include "parser.h" + +#define ParseTOKENTYPE SToken + +#define NON_ARITHMEIC_EXPR 0 +#define NORMAL_ARITHMETIC 1 +#define AGG_ARIGHTMEIC 2 + +enum SQL_NODE_TYPE { + SQL_NODE_TABLE_COLUMN= 1, + SQL_NODE_SQLFUNCTION = 2, + SQL_NODE_VALUE = 3, + SQL_NODE_EXPR = 4, +}; + +enum SQL_NODE_FROM_TYPE { + SQL_NODE_FROM_SUBQUERY = 1, + SQL_NODE_FROM_TABLELIST = 2, +}; + +//enum SQL_EXPR_FLAG { +// EXPR_FLAG_TS_ERROR = 1, +// EXPR_FLAG_NS_TIMESTAMP = 2, +// EXPR_FLAG_TIMESTAMP_VAR = 3, +//}; + +extern char tTokenTypeSwitcher[13]; + +#define toTSDBType(x) \ + do { \ + if ((x) >= tListLen(tTokenTypeSwitcher)) { \ + (x) = TSDB_DATA_TYPE_BINARY; \ + } else { \ + (x) = tTokenTypeSwitcher[(x)]; \ + } \ + } while (0) + +#define TPARSER_HAS_TOKEN(_t) ((_t).n > 0) +#define TPARSER_SET_NONE_TOKEN(_t) ((_t).n = 0) + +typedef struct SListItem { + SVariant pVar; + uint8_t sortOrder; +} SListItem; + +typedef struct SIntervalVal { + int32_t token; + SToken interval; + SToken offset; +} SIntervalVal; + +typedef struct SSessionWindowVal { + SToken col; + SToken gap; +} SSessionWindowVal; + +typedef struct SWindowStateVal { + SToken col; +} SWindowStateVal; + +struct SRelationInfo; + +typedef struct SSqlNode { + struct SArray *pSelNodeList; // select clause + struct SRelationInfo *from; // from clause SArray + struct tSqlExpr *pWhere; // where clause [optional] + SArray *pGroupby; // groupby clause, only for tags[optional], SArray + SArray *pSortOrder; // orderby [optional], SArray + SArray *fillType; // fill type[optional], SArray + SIntervalVal interval; // (interval, interval_offset) [optional] + SSessionWindowVal sessionVal; // session window [optional] + SWindowStateVal windowstateVal; // window_state(col) [optional] + SToken sliding; // sliding window [optional] + SLimit limit; // limit offset [optional] + SLimit slimit; // group limit offset [optional] + SToken sqlstr; // sql string in select clause + struct tSqlExpr *pHaving; // having clause [optional] +} SSqlNode; + +typedef struct SRelElementPair { + union { + SToken tableName; + SArray *pSubquery; + }; + + SToken aliasName; +} SRelElementPair; + +typedef struct SRelationInfo { + int32_t type; // nested query|table name list + SArray *list; // SArray +} SRelationInfo; + +typedef struct SCreatedTableInfo { + SToken name; // table name token + SToken stableName; // super table name token , for using clause + SArray *pTagNames; // create by using super table, tag name + SArray *pTagVals; // create by using super table, tag value + char *fullname; // table full name + STagData tagdata; // true tag data, super table full name is in STagData + int8_t igExist; // ignore if exists +} SCreatedTableInfo; + +typedef struct SCreateTableSql { + SToken name; // table name, create table [name] xxx + int8_t type; // create normal table/from super table/ stream + bool existCheck; + + struct { + SArray *pTagColumns; // SArray + SArray *pColumns; // SArray + } colInfo; + + SArray *childTableInfo; // SArray + SSqlNode *pSelect; +} SCreateTableSql; + +typedef struct SAlterTableInfo { + SToken name; + int16_t tableType; + int16_t type; + STagData tagData; + SArray *pAddColumns; // SArray + SArray *varList; // set t=val or: change src dst, SArray +} SAlterTableInfo; + +typedef struct SCreateDbInfo { + SToken dbname; + int32_t replica; + int32_t cacheBlockSize; + int32_t maxTablesPerVnode; + int32_t numOfBlocks; + int32_t daysPerFile; + int32_t minRowsPerBlock; + int32_t maxRowsPerBlock; + int32_t fsyncPeriod; + int64_t commitTime; + int32_t walLevel; + int32_t quorum; + int32_t compressionLevel; + SToken precision; + bool ignoreExists; + int8_t update; + int8_t cachelast; + SArray *keep; + int8_t dbType; + int16_t partitions; +} SCreateDbInfo; + +typedef struct SCreateFuncInfo { + SToken name; + SToken path; + int32_t type; + int32_t bufSize; + SField output; +} SCreateFuncInfo; + +typedef struct SCreateAcctInfo { + int32_t maxUsers; + int32_t maxDbs; + int32_t maxTimeSeries; + int32_t maxStreams; + int32_t maxPointsPerSecond; + int64_t maxStorage; + int64_t maxQueryTime; + int32_t maxConnections; + SToken stat; +} SCreateAcctInfo; + +typedef struct SShowInfo { + uint8_t showType; + SToken prefix; + SToken pattern; +} SShowInfo; + +typedef struct SUserInfo { + SToken user; + SToken passwd; + SToken privilege; + int16_t type; +} SUserInfo; + +typedef struct SMiscInfo { + SArray *a; // SArray + bool existsCheck; + int16_t dbType; + int16_t tableType; + SUserInfo user; + union { + SCreateDbInfo dbOpt; + SCreateAcctInfo acctOpt; + SCreateFuncInfo funcOpt; + SShowInfo showOpt; + SToken id; + }; +} SMiscInfo; + +typedef struct SSqlInfo { + int32_t type; + bool valid; + SArray *list; // todo refactor + char msg[256]; + SArray *funcs; + union { + SCreateTableSql *pCreateTableInfo; + SAlterTableInfo *pAlterInfo; + SMiscInfo *pMiscInfo; + }; +} SSqlInfo; + +typedef struct tSqlExpr { + uint16_t type; // sql node type + uint32_t tokenId; // TK_LE: less than(binary expr) + + // The complete string of the function(col, param), and the function name is kept in exprToken + struct { + SToken operand; + struct SArray *paramList; // function parameters list + } Expr; + + SToken columnName; // table column info + SVariant value; // the use input value + SToken exprToken; // original sql expr string or function name of sql function + struct tSqlExpr *pLeft; // the left child + struct tSqlExpr *pRight; // the right child +} tSqlExpr; + +// used in select clause. select from xxx +typedef struct tSqlExprItem { + tSqlExpr *pNode; // The list of expressions + char *aliasName; // alias name, null-terminated string + bool distinct; +} tSqlExprItem; + +SArray *tListItemAppend(SArray *pList, SVariant *pVar, uint8_t sortOrder); +SArray *tListItemInsert(SArray *pList, SVariant *pVar, uint8_t sortOrder, int32_t index); +SArray *tListItemAppendToken(SArray *pList, SToken *pAliasToken, uint8_t sortOrder); + +SRelationInfo *setTableNameList(SRelationInfo *pRelationInfo, SToken *pName, SToken *pAlias); +void * destroyRelationInfo(SRelationInfo *pFromInfo); +SRelationInfo *addSubquery(SRelationInfo *pRelationInfo, SArray *pSub, SToken *pAlias); + +// sql expr leaf node +tSqlExpr *tSqlExprCreateIdValue(SToken *pToken, int32_t optrType); +tSqlExpr *tSqlExprCreateFunction(SArray *pParam, SToken *pFuncToken, SToken *endToken, int32_t optType); +SArray * tAppendFuncName(SArray *pList, SToken *pToken); + +tSqlExpr *tSqlExprCreate(tSqlExpr *pLeft, tSqlExpr *pRight, int32_t optrType); +tSqlExpr *tSqlExprClone(tSqlExpr *pSrc); +void tSqlExprCompact(tSqlExpr **pExpr); +bool tSqlExprIsLeaf(tSqlExpr *pExpr); +bool tSqlExprIsParentOfLeaf(tSqlExpr *pExpr); +void tSqlExprDestroy(tSqlExpr *pExpr); +SArray * tSqlExprListAppend(SArray *pList, tSqlExpr *pNode, SToken *pDistinct, SToken *pToken); +void tSqlExprListDestroy(SArray *pList); + +SSqlNode *tSetQuerySqlNode(SToken *pSelectToken, SArray *pSelNodeList, SRelationInfo *pFrom, tSqlExpr *pWhere, + SArray *pGroupby, SArray *pSortOrder, SIntervalVal *pInterval, SSessionWindowVal *ps, + SWindowStateVal *pw, SToken *pSliding, SArray *pFill, SLimit *pLimit, SLimit *pgLimit, tSqlExpr *pHaving); +int32_t tSqlExprCompare(tSqlExpr *left, tSqlExpr *right); + +SCreateTableSql *tSetCreateTableInfo(SArray *pCols, SArray *pTags, SSqlNode *pSelect, int32_t type); + +SAlterTableInfo * tSetAlterTableInfo(SToken *pTableName, SArray *pCols, SArray *pVals, int32_t type, int16_t tableType); +SCreatedTableInfo createNewChildTableInfo(SToken *pTableName, SArray *pTagNames, SArray *pTagVals, SToken *pToken, + SToken *igExists); + +void destroyAllSqlNode(SArray *pSqlNode); +void destroySqlNode(SSqlNode *pSql); +void freeCreateTableInfo(void* p); + +SSqlInfo *setSqlInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SToken *pTableName, int32_t type); +SArray *setSubclause(SArray *pList, void *pSqlNode); +SArray *appendSelectClause(SArray *pList, void *pSubclause); + +void setCreatedTableName(SSqlInfo *pInfo, SToken *pTableNameToken, SToken *pIfNotExists); +void* destroyCreateTableSql(SCreateTableSql* pCreate); + +void SqlInfoDestroy(SSqlInfo *pInfo); + +void setDCLSqlElems(SSqlInfo *pInfo, int32_t type, int32_t nParams, ...); +void setDropDbTableInfo(SSqlInfo *pInfo, int32_t type, SToken* pToken, SToken* existsCheck,int16_t dbType,int16_t tableType); +void setShowOptions(SSqlInfo *pInfo, int32_t type, SToken* prefix, SToken* pPatterns); + +void setCreateDbInfo(SSqlInfo *pInfo, int32_t type, SToken *pToken, SCreateDbInfo *pDB, SToken *pIgExists); + +void setCreateAcctSql(SSqlInfo *pInfo, int32_t type, SToken *pName, SToken *pPwd, SCreateAcctInfo *pAcctInfo); +void setCreateUserSql(SSqlInfo *pInfo, SToken *pName, SToken *pPasswd); +void setKillSql(SSqlInfo *pInfo, int32_t type, SToken *ip); +void setAlterUserSql(SSqlInfo *pInfo, int16_t type, SToken *pName, SToken* pPwd, SToken *pPrivilege); + +void setCompactVnodeSql(SSqlInfo *pInfo, int32_t type, SArray *pParam); + +void setDefaultCreateDbOption(SCreateDbInfo *pDBInfo); +void setDefaultCreateTopicOption(SCreateDbInfo *pDBInfo); + +// prefix show db.tables; +void tSetDbName(SToken *pCpxName, SToken *pDb); + +void tSetColumnInfo(struct SField *pField, SToken *pName, struct SField *pType); +void tSetColumnType(struct SField *pField, SToken *type); + +/** + * The main parse function. + * @param yyp The parser + * @param yymajor The major token code number + * @param yyminor The value for the token + */ +void Parse(void *yyp, int yymajor, ParseTOKENTYPE yyminor, SSqlInfo *); + +/** + * Free the allocated resources in case of failure. + * @param p The parser to be deleted + * @param freeProc Function used to reclaim memory + */ +void ParseFree(void *p, void (*freeProc)(void *)); + +/** + * Allocated callback function. + * @param mallocProc The parser allocator + * @return + */ +void *ParseAlloc(void *(*mallocProc)(size_t)); + +/** + * + * @param str sql string + * @return sql ast + */ +SSqlInfo doGenerateAST(const char *str); + +#ifdef __cplusplus +} +#endif + +#endif // TDENGINE_ASTGENERATOR_H diff --git a/src/os/src/windows/wSysLog.c b/source/libs/parser/inc/insertParser.h similarity index 86% rename from src/os/src/windows/wSysLog.c rename to source/libs/parser/inc/insertParser.h index 866cacbabaa870a6fbe58f1a3dc363dbfdd80949..49e678cd541eae0e6b1af9473d7405928089fbfe 100644 --- a/src/os/src/windows/wSysLog.c +++ b/source/libs/parser/inc/insertParser.h @@ -13,7 +13,7 @@ * along with this program. If not, see . */ -#define _DEFAULT_SOURCE -#include "os.h" +#ifndef TDENGINE_INSERTPARSER_H +#define TDENGINE_INSERTPARSER_H -void syslog(int unused, const char *format, ...) {} \ No newline at end of file +#endif // TDENGINE_INSERTPARSER_H diff --git a/source/libs/parser/inc/parserInt.h b/source/libs/parser/inc/parserInt.h index 2c76f1bb96bd64e8288d42095e195eb796441892..ce1504b4e2103514c6e0784c2941e35c4a6e5017 100644 --- a/source/libs/parser/inc/parserInt.h +++ b/source/libs/parser/inc/parserInt.h @@ -20,6 +20,40 @@ extern "C" { #endif +#include "catalog.h" +#include "tname.h" +#include "astGenerator.h" + +struct SSqlNode; + +typedef struct SInsertStmtInfo { + SHashObj *pTableBlockHashList; // data block for each table + SArray *pDataBlocks; // SArray. Merged submit block for each vgroup + int8_t schemaAttached; // denote if submit block is built with table schema or not + uint8_t payloadType; // EPayloadType. 0: K-V payload for non-prepare insert, 1: rawPayload for prepare insert + uint32_t insertType; // insert data from [file|sql statement| bound statement] + char *sql; // current sql statement position +} SInsertStmtInfo; + +/** + * Validate the sql info, according to the corresponding metadata info from catalog. + * @param pCatalog + * @param pSqlInfo + * @param pQueryInfo a bounded AST with essential meta data from local buffer or mgmt node + * @param id + * @param msg + * @return + */ +int32_t qParserValidateSqlNode(struct SCatalog* pCatalog, SSqlInfo* pSqlInfo, SQueryStmtInfo* pQueryInfo, int64_t id, char* msg); + +/** + * + * @param pSqlNode + * @param pMetaInfo + * @return + */ +int32_t qParserExtractRequestedMetaInfo(const struct SSqlNode* pSqlNode, SMetaReq* pMetaInfo); + #ifdef __cplusplus } #endif diff --git a/source/libs/parser/inc/parserUtil.h b/source/libs/parser/inc/parserUtil.h new file mode 100644 index 0000000000000000000000000000000000000000..4e6af0a9010ba57de579c2f79cd52c8c4aa98490 --- /dev/null +++ b/source/libs/parser/inc/parserUtil.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef TDENGINE_PARSERUTIL_H +#define TDENGINE_PARSERUTIL_H + +#ifdef __cplusplus +extern "C" { +#endif + + +#ifdef __cplusplus +} +#endif + +#endif // TDENGINE_PARSERUTIL_H diff --git a/source/libs/parser/inc/sql.y b/source/libs/parser/inc/sql.y new file mode 100644 index 0000000000000000000000000000000000000000..efad59f780e894b44846fd074f78077b3b73d592 --- /dev/null +++ b/source/libs/parser/inc/sql.y @@ -0,0 +1,924 @@ +//lemon parser file to generate sql parse by using finite-state-machine code used to parse sql +//usage: lemon sql.y +%token_prefix TK_ + +%token_type {SToken} +%default_type {SToken} +%extra_argument {SSqlInfo* pInfo} + +%fallback ID BOOL TINYINT SMALLINT INTEGER BIGINT FLOAT DOUBLE STRING TIMESTAMP BINARY NCHAR. + +%left OR. +%left AND. +%right NOT. +%left EQ NE ISNULL NOTNULL IS LIKE MATCH NMATCH GLOB BETWEEN IN. +%left GT GE LT LE. +%left BITAND BITOR LSHIFT RSHIFT. +%left PLUS MINUS. +%left DIVIDE TIMES. +%left STAR SLASH REM. +%left CONCAT. +%right UMINUS UPLUS BITNOT. + +%include { +#include +#include +#include +#include +#include +#include "astGenerator.h" +#include "tmsgtype.h" +#include "ttoken.h" +#include "ttokendef.h" +//#include "tutil.h" +#include "tvariant.h" +} + +%syntax_error { + pInfo->valid = false; + int32_t outputBufLen = tListLen(pInfo->msg); + int32_t len = 0; + + if(TOKEN.z) { + char msg[] = "syntax error near \"%s\""; + int32_t sqlLen = strlen(&TOKEN.z[0]); + + if (sqlLen + sizeof(msg)/sizeof(msg[0]) + 1 > outputBufLen) { + char tmpstr[128] = {0}; + memcpy(tmpstr, &TOKEN.z[0], sizeof(tmpstr)/sizeof(tmpstr[0]) - 1); + len = sprintf(pInfo->msg, msg, tmpstr); + } else { + len = sprintf(pInfo->msg, msg, &TOKEN.z[0]); + } + + } else { + len = sprintf(pInfo->msg, "Incomplete SQL statement"); + } + + assert(len <= outputBufLen); +} + +%parse_accept {} + +program ::= cmd. {} + +//////////////////////////////////THE SHOW STATEMENT/////////////////////////////////////////// +cmd ::= SHOW DATABASES. { setShowOptions(pInfo, TSDB_MGMT_TABLE_DB, 0, 0);} +cmd ::= SHOW TOPICS. { setShowOptions(pInfo, TSDB_MGMT_TABLE_TP, 0, 0);} +cmd ::= SHOW FUNCTIONS. { setShowOptions(pInfo, TSDB_MGMT_TABLE_FUNCTION, 0, 0);} +cmd ::= SHOW MNODES. { setShowOptions(pInfo, TSDB_MGMT_TABLE_MNODE, 0, 0);} +cmd ::= SHOW DNODES. { setShowOptions(pInfo, TSDB_MGMT_TABLE_DNODE, 0, 0);} +cmd ::= SHOW ACCOUNTS. { setShowOptions(pInfo, TSDB_MGMT_TABLE_ACCT, 0, 0);} +cmd ::= SHOW USERS. { setShowOptions(pInfo, TSDB_MGMT_TABLE_USER, 0, 0);} + +cmd ::= SHOW MODULES. { setShowOptions(pInfo, TSDB_MGMT_TABLE_MODULE, 0, 0); } +cmd ::= SHOW QUERIES. { setShowOptions(pInfo, TSDB_MGMT_TABLE_QUERIES, 0, 0); } +cmd ::= SHOW CONNECTIONS.{ setShowOptions(pInfo, TSDB_MGMT_TABLE_CONNS, 0, 0);} +cmd ::= SHOW STREAMS. { setShowOptions(pInfo, TSDB_MGMT_TABLE_STREAMS, 0, 0); } +cmd ::= SHOW VARIABLES. { setShowOptions(pInfo, TSDB_MGMT_TABLE_VARIABLES, 0, 0); } +cmd ::= SHOW SCORES. { setShowOptions(pInfo, TSDB_MGMT_TABLE_SCORES, 0, 0); } +cmd ::= SHOW GRANTS. { setShowOptions(pInfo, TSDB_MGMT_TABLE_GRANTS, 0, 0); } + +cmd ::= SHOW VNODES. { setShowOptions(pInfo, TSDB_MGMT_TABLE_VNODES, 0, 0); } +cmd ::= SHOW VNODES ids(X). { setShowOptions(pInfo, TSDB_MGMT_TABLE_VNODES, &X, 0); } + + +%type dbPrefix {SToken} +dbPrefix(A) ::=. {A.n = 0; A.type = 0;} +dbPrefix(A) ::= ids(X) DOT. {A = X; } + +%type cpxName {SToken} +cpxName(A) ::= . {A.n = 0; } +cpxName(A) ::= DOT ids(Y). {A = Y; A.n += 1; } + +cmd ::= SHOW CREATE TABLE ids(X) cpxName(Y). { + X.n += Y.n; + setDCLSqlElems(pInfo, TSDB_SQL_SHOW_CREATE_TABLE, 1, &X); +} +cmd ::= SHOW CREATE STABLE ids(X) cpxName(Y). { + X.n += Y.n; + setDCLSqlElems(pInfo, TSDB_SQL_SHOW_CREATE_STABLE, 1, &X); +} + +cmd ::= SHOW CREATE DATABASE ids(X). { + setDCLSqlElems(pInfo, TSDB_SQL_SHOW_CREATE_DATABASE, 1, &X); +} + +cmd ::= SHOW dbPrefix(X) TABLES. { + setShowOptions(pInfo, TSDB_MGMT_TABLE_TABLE, &X, 0); +} + +cmd ::= SHOW dbPrefix(X) TABLES LIKE ids(Y). { + setShowOptions(pInfo, TSDB_MGMT_TABLE_TABLE, &X, &Y); +} + +cmd ::= SHOW dbPrefix(X) STABLES. { + setShowOptions(pInfo, TSDB_MGMT_TABLE_METRIC, &X, 0); +} + +cmd ::= SHOW dbPrefix(X) STABLES LIKE ids(Y). { + SToken token; + tSetDbName(&token, &X); + setShowOptions(pInfo, TSDB_MGMT_TABLE_METRIC, &token, &Y); +} + +cmd ::= SHOW dbPrefix(X) VGROUPS. { + SToken token; + tSetDbName(&token, &X); + setShowOptions(pInfo, TSDB_MGMT_TABLE_VGROUP, &token, 0); +} + +cmd ::= SHOW dbPrefix(X) VGROUPS ids(Y). { + SToken token; + tSetDbName(&token, &X); + setShowOptions(pInfo, TSDB_MGMT_TABLE_VGROUP, &token, &Y); +} + +//drop configure for tables +cmd ::= DROP TABLE ifexists(Y) ids(X) cpxName(Z). { + X.n += Z.n; + setDropDbTableInfo(pInfo, TSDB_SQL_DROP_TABLE, &X, &Y, -1, -1); +} + +//drop stable +cmd ::= DROP STABLE ifexists(Y) ids(X) cpxName(Z). { + X.n += Z.n; + setDropDbTableInfo(pInfo, TSDB_SQL_DROP_TABLE, &X, &Y, -1, TSDB_SUPER_TABLE); +} + +cmd ::= DROP DATABASE ifexists(Y) ids(X). { setDropDbTableInfo(pInfo, TSDB_SQL_DROP_DB, &X, &Y, TSDB_DB_TYPE_DEFAULT, -1); } +cmd ::= DROP TOPIC ifexists(Y) ids(X). { setDropDbTableInfo(pInfo, TSDB_SQL_DROP_DB, &X, &Y, TSDB_DB_TYPE_TOPIC, -1); } +cmd ::= DROP FUNCTION ids(X). { setDropFuncInfo(pInfo, TSDB_SQL_DROP_FUNCTION, &X); } + +cmd ::= DROP DNODE ids(X). { setDCLSqlElems(pInfo, TSDB_SQL_DROP_DNODE, 1, &X); } +cmd ::= DROP USER ids(X). { setDCLSqlElems(pInfo, TSDB_SQL_DROP_USER, 1, &X); } +cmd ::= DROP ACCOUNT ids(X). { setDCLSqlElems(pInfo, TSDB_SQL_DROP_ACCT, 1, &X); } + +/////////////////////////////////THE USE STATEMENT////////////////////////////////////////// +cmd ::= USE ids(X). { setDCLSqlElems(pInfo, TSDB_SQL_USE_DB, 1, &X);} + +/////////////////////////////////THE DESCRIBE STATEMENT///////////////////////////////////// +cmd ::= DESCRIBE ids(X) cpxName(Y). { + X.n += Y.n; + setDCLSqlElems(pInfo, TSDB_SQL_DESCRIBE_TABLE, 1, &X); +} +cmd ::= DESC ids(X) cpxName(Y). { + X.n += Y.n; + setDCLSqlElems(pInfo, TSDB_SQL_DESCRIBE_TABLE, 1, &X); +} +/////////////////////////////////THE ALTER STATEMENT//////////////////////////////////////// +cmd ::= ALTER USER ids(X) PASS ids(Y). { setAlterUserSql(pInfo, TSDB_ALTER_USER_PASSWD, &X, &Y, NULL); } +cmd ::= ALTER USER ids(X) PRIVILEGE ids(Y). { setAlterUserSql(pInfo, TSDB_ALTER_USER_PRIVILEGES, &X, NULL, &Y);} +cmd ::= ALTER DNODE ids(X) ids(Y). { setDCLSqlElems(pInfo, TSDB_SQL_CFG_DNODE, 2, &X, &Y); } +cmd ::= ALTER DNODE ids(X) ids(Y) ids(Z). { setDCLSqlElems(pInfo, TSDB_SQL_CFG_DNODE, 3, &X, &Y, &Z); } +cmd ::= ALTER LOCAL ids(X). { setDCLSqlElems(pInfo, TSDB_SQL_CFG_LOCAL, 1, &X); } +cmd ::= ALTER LOCAL ids(X) ids(Y). { setDCLSqlElems(pInfo, TSDB_SQL_CFG_LOCAL, 2, &X, &Y); } +cmd ::= ALTER DATABASE ids(X) alter_db_optr(Y). { SToken t = {0}; setCreateDbInfo(pInfo, TSDB_SQL_ALTER_DB, &X, &Y, &t);} +cmd ::= ALTER TOPIC ids(X) alter_topic_optr(Y). { SToken t = {0}; setCreateDbInfo(pInfo, TSDB_SQL_ALTER_DB, &X, &Y, &t);} + +cmd ::= ALTER ACCOUNT ids(X) acct_optr(Z). { setCreateAcctSql(pInfo, TSDB_SQL_ALTER_ACCT, &X, NULL, &Z);} +cmd ::= ALTER ACCOUNT ids(X) PASS ids(Y) acct_optr(Z). { setCreateAcctSql(pInfo, TSDB_SQL_ALTER_ACCT, &X, &Y, &Z);} + +////////////////////////////// COMPACT STATEMENT ////////////////////////////////////////////// + +cmd ::= COMPACT VNODES IN LP exprlist(Y) RP. { setCompactVnodeSql(pInfo, TSDB_SQL_COMPACT_VNODE, Y);} + +// An IDENTIFIER can be a generic identifier, or one of several keywords. +// Any non-standard keyword can also be an identifier. +// And "ids" is an identifer-or-string. +%type ids {SToken} +ids(A) ::= ID(X). {A = X; } +ids(A) ::= STRING(X). {A = X; } + +%type ifexists {SToken} +ifexists(X) ::= IF EXISTS. { X.n = 1;} +ifexists(X) ::= . { X.n = 0;} + +%type ifnotexists {SToken} +ifnotexists(X) ::= IF NOT EXISTS. { X.n = 1;} +ifnotexists(X) ::= . { X.n = 0;} + +/////////////////////////////////THE CREATE STATEMENT/////////////////////////////////////// +//create option for dnode/db/user/account +cmd ::= CREATE DNODE ids(X). { setDCLSqlElems(pInfo, TSDB_SQL_CREATE_DNODE, 1, &X);} +cmd ::= CREATE ACCOUNT ids(X) PASS ids(Y) acct_optr(Z). + { setCreateAcctSql(pInfo, TSDB_SQL_CREATE_ACCT, &X, &Y, &Z);} +cmd ::= CREATE DATABASE ifnotexists(Z) ids(X) db_optr(Y). { setCreateDbInfo(pInfo, TSDB_SQL_CREATE_DB, &X, &Y, &Z);} +cmd ::= CREATE TOPIC ifnotexists(Z) ids(X) topic_optr(Y). { setCreateDbInfo(pInfo, TSDB_SQL_CREATE_DB, &X, &Y, &Z);} +cmd ::= CREATE FUNCTION ids(X) AS ids(Y) OUTPUTTYPE typename(Z) bufsize(B). { setCreateFuncInfo(pInfo, TSDB_SQL_CREATE_FUNCTION, &X, &Y, &Z, &B, 1);} +cmd ::= CREATE AGGREGATE FUNCTION ids(X) AS ids(Y) OUTPUTTYPE typename(Z) bufsize(B). { setCreateFuncInfo(pInfo, TSDB_SQL_CREATE_FUNCTION, &X, &Y, &Z, &B, 2);} +cmd ::= CREATE USER ids(X) PASS ids(Y). { setCreateUserSql(pInfo, &X, &Y);} + +bufsize(Y) ::= . { Y.n = 0; } +bufsize(Y) ::= BUFSIZE INTEGER(X). { Y = X; } + +pps(Y) ::= . { Y.n = 0; } +pps(Y) ::= PPS INTEGER(X). { Y = X; } + +tseries(Y) ::= . { Y.n = 0; } +tseries(Y) ::= TSERIES INTEGER(X). { Y = X; } + +dbs(Y) ::= . { Y.n = 0; } +dbs(Y) ::= DBS INTEGER(X). { Y = X; } + +streams(Y) ::= . { Y.n = 0; } +streams(Y) ::= STREAMS INTEGER(X). { Y = X; } + +storage(Y) ::= . { Y.n = 0; } +storage(Y) ::= STORAGE INTEGER(X). { Y = X; } + +qtime(Y) ::= . { Y.n = 0; } +qtime(Y) ::= QTIME INTEGER(X). { Y = X; } + +users(Y) ::= . { Y.n = 0; } +users(Y) ::= USERS INTEGER(X). { Y = X; } + +conns(Y) ::= . { Y.n = 0; } +conns(Y) ::= CONNS INTEGER(X). { Y = X; } + +state(Y) ::= . { Y.n = 0; } +state(Y) ::= STATE ids(X). { Y = X; } + +%type acct_optr {SCreateAcctInfo} +acct_optr(Y) ::= pps(C) tseries(D) storage(P) streams(F) qtime(Q) dbs(E) users(K) conns(L) state(M). { + Y.maxUsers = (K.n>0)?atoi(K.z):-1; + Y.maxDbs = (E.n>0)?atoi(E.z):-1; + Y.maxTimeSeries = (D.n>0)?atoi(D.z):-1; + Y.maxStreams = (F.n>0)?atoi(F.z):-1; + Y.maxPointsPerSecond = (C.n>0)?atoi(C.z):-1; + Y.maxStorage = (P.n>0)?strtoll(P.z, NULL, 10):-1; + Y.maxQueryTime = (Q.n>0)?strtoll(Q.z, NULL, 10):-1; + Y.maxConnections = (L.n>0)?atoi(L.z):-1; + Y.stat = M; +} + +%type intitemlist {SArray*} +%destructor intitemlist {taosArrayDestroy($$);} + +%type intitem {SVariant} +intitemlist(A) ::= intitemlist(X) COMMA intitem(Y). { A = tListItemAppend(X, &Y, -1); } +intitemlist(A) ::= intitem(X). { A = tListItemAppend(NULL, &X, -1); } + +intitem(A) ::= INTEGER(X). { toTSDBType(X.type); taosVariantCreate(&A, X.z, X.n, X.type); } + +%type keep {SArray*} +%destructor keep {taosArrayDestroy($$);} +keep(Y) ::= KEEP intitemlist(X). { Y = X; } + +cache(Y) ::= CACHE INTEGER(X). { Y = X; } +replica(Y) ::= REPLICA INTEGER(X). { Y = X; } +quorum(Y) ::= QUORUM INTEGER(X). { Y = X; } +days(Y) ::= DAYS INTEGER(X). { Y = X; } +minrows(Y) ::= MINROWS INTEGER(X). { Y = X; } +maxrows(Y) ::= MAXROWS INTEGER(X). { Y = X; } +blocks(Y) ::= BLOCKS INTEGER(X). { Y = X; } +ctime(Y) ::= CTIME INTEGER(X). { Y = X; } +wal(Y) ::= WAL INTEGER(X). { Y = X; } +fsync(Y) ::= FSYNC INTEGER(X). { Y = X; } +comp(Y) ::= COMP INTEGER(X). { Y = X; } +prec(Y) ::= PRECISION STRING(X). { Y = X; } +update(Y) ::= UPDATE INTEGER(X). { Y = X; } +cachelast(Y) ::= CACHELAST INTEGER(X). { Y = X; } +partitions(Y) ::= PARTITIONS INTEGER(X). { Y = X; } + +%type db_optr {SCreateDbInfo} +db_optr(Y) ::= . {setDefaultCreateDbOption(&Y); Y.dbType = TSDB_DB_TYPE_DEFAULT;} + +db_optr(Y) ::= db_optr(Z) cache(X). { Y = Z; Y.cacheBlockSize = strtol(X.z, NULL, 10); } +db_optr(Y) ::= db_optr(Z) replica(X). { Y = Z; Y.replica = strtol(X.z, NULL, 10); } +db_optr(Y) ::= db_optr(Z) quorum(X). { Y = Z; Y.quorum = strtol(X.z, NULL, 10); } +db_optr(Y) ::= db_optr(Z) days(X). { Y = Z; Y.daysPerFile = strtol(X.z, NULL, 10); } +db_optr(Y) ::= db_optr(Z) minrows(X). { Y = Z; Y.minRowsPerBlock = strtod(X.z, NULL); } +db_optr(Y) ::= db_optr(Z) maxrows(X). { Y = Z; Y.maxRowsPerBlock = strtod(X.z, NULL); } +db_optr(Y) ::= db_optr(Z) blocks(X). { Y = Z; Y.numOfBlocks = strtol(X.z, NULL, 10); } +db_optr(Y) ::= db_optr(Z) ctime(X). { Y = Z; Y.commitTime = strtol(X.z, NULL, 10); } +db_optr(Y) ::= db_optr(Z) wal(X). { Y = Z; Y.walLevel = strtol(X.z, NULL, 10); } +db_optr(Y) ::= db_optr(Z) fsync(X). { Y = Z; Y.fsyncPeriod = strtol(X.z, NULL, 10); } +db_optr(Y) ::= db_optr(Z) comp(X). { Y = Z; Y.compressionLevel = strtol(X.z, NULL, 10); } +db_optr(Y) ::= db_optr(Z) prec(X). { Y = Z; Y.precision = X; } +db_optr(Y) ::= db_optr(Z) keep(X). { Y = Z; Y.keep = X; } +db_optr(Y) ::= db_optr(Z) update(X). { Y = Z; Y.update = strtol(X.z, NULL, 10); } +db_optr(Y) ::= db_optr(Z) cachelast(X). { Y = Z; Y.cachelast = strtol(X.z, NULL, 10); } + +%type topic_optr {SCreateDbInfo} + +topic_optr(Y) ::= db_optr(Z). { Y = Z; Y.dbType = TSDB_DB_TYPE_TOPIC; } +topic_optr(Y) ::= topic_optr(Z) partitions(X). { Y = Z; Y.partitions = strtol(X.z, NULL, 10); } + +%type alter_db_optr {SCreateDbInfo} +alter_db_optr(Y) ::= . { setDefaultCreateDbOption(&Y); Y.dbType = TSDB_DB_TYPE_DEFAULT;} + +alter_db_optr(Y) ::= alter_db_optr(Z) replica(X). { Y = Z; Y.replica = strtol(X.z, NULL, 10); } +alter_db_optr(Y) ::= alter_db_optr(Z) quorum(X). { Y = Z; Y.quorum = strtol(X.z, NULL, 10); } +alter_db_optr(Y) ::= alter_db_optr(Z) keep(X). { Y = Z; Y.keep = X; } +alter_db_optr(Y) ::= alter_db_optr(Z) blocks(X). { Y = Z; Y.numOfBlocks = strtol(X.z, NULL, 10); } +alter_db_optr(Y) ::= alter_db_optr(Z) comp(X). { Y = Z; Y.compressionLevel = strtol(X.z, NULL, 10); } +alter_db_optr(Y) ::= alter_db_optr(Z) update(X). { Y = Z; Y.update = strtol(X.z, NULL, 10); } +alter_db_optr(Y) ::= alter_db_optr(Z) cachelast(X). { Y = Z; Y.cachelast = strtol(X.z, NULL, 10); } + +// dynamically update the following two parameters are not allowed. +//alter_db_optr(Y) ::= alter_db_optr(Z) fsync(X). { Y = Z; Y.fsyncPeriod = strtol(X.z, NULL, 10); } +//alter_db_optr(Y) ::= alter_db_optr(Z) wal(X). { Y = Z; Y.walLevel = strtol(X.z, NULL, 10); } not support yet + +%type alter_topic_optr {SCreateDbInfo} + +alter_topic_optr(Y) ::= alter_db_optr(Z). { Y = Z; Y.dbType = TSDB_DB_TYPE_TOPIC; } +alter_topic_optr(Y) ::= alter_topic_optr(Z) partitions(X). { Y = Z; Y.partitions = strtol(X.z, NULL, 10); } + +%type typename {TAOS_FIELD} +typename(A) ::= ids(X). { + X.type = 0; + tSetColumnType (&A, &X); +} + +//define binary type, e.g., binary(10), nchar(10) +typename(A) ::= ids(X) LP signed(Y) RP. { + if (Y <= 0) { + X.type = 0; + tSetColumnType(&A, &X); + } else { + X.type = -Y; // negative value of name length + tSetColumnType(&A, &X); + } +} + +// define the unsigned number type +typename(A) ::= ids(X) UNSIGNED(Z). { + X.type = 0; + X.n = ((Z.z + Z.n) - X.z); + tSetColumnType (&A, &X); +} + +%type signed {int64_t} +signed(A) ::= INTEGER(X). { A = strtol(X.z, NULL, 10); } +signed(A) ::= PLUS INTEGER(X). { A = strtol(X.z, NULL, 10); } +signed(A) ::= MINUS INTEGER(X). { A = -strtol(X.z, NULL, 10);} + +////////////////////////////////// The CREATE TABLE statement /////////////////////////////// +cmd ::= CREATE TABLE create_table_args. {} +cmd ::= CREATE TABLE create_stable_args. {} +cmd ::= CREATE STABLE create_stable_args. {} +cmd ::= CREATE TABLE create_table_list(Z). { pInfo->type = TSDB_SQL_CREATE_TABLE; pInfo->pCreateTableInfo = Z;} + +%type create_table_list{SCreateTableSql*} +%destructor create_table_list{destroyCreateTableSql($$);} +create_table_list(A) ::= create_from_stable(Z). { + SCreateTableSql* pCreateTable = calloc(1, sizeof(SCreateTableSql)); + pCreateTable->childTableInfo = taosArrayInit(4, sizeof(SCreatedTableInfo)); + + taosArrayPush(pCreateTable->childTableInfo, &Z); + pCreateTable->type = TSQL_CREATE_CTABLE; + A = pCreateTable; +} + +create_table_list(A) ::= create_table_list(X) create_from_stable(Z). { + taosArrayPush(X->childTableInfo, &Z); + A = X; +} + +%type create_table_args{SCreateTableSql*} +create_table_args(A) ::= ifnotexists(U) ids(V) cpxName(Z) LP columnlist(X) RP. { + A = tSetCreateTableInfo(X, NULL, NULL, TSQL_CREATE_TABLE); + setSqlInfo(pInfo, A, NULL, TSDB_SQL_CREATE_TABLE); + + V.n += Z.n; + setCreatedTableName(pInfo, &V, &U); +} + +// create super table +%type create_stable_args{SCreateTableSql*} +create_stable_args(A) ::= ifnotexists(U) ids(V) cpxName(Z) LP columnlist(X) RP TAGS LP columnlist(Y) RP. { + A = tSetCreateTableInfo(X, Y, NULL, TSQL_CREATE_STABLE); + setSqlInfo(pInfo, A, NULL, TSDB_SQL_CREATE_TABLE); + + V.n += Z.n; + setCreatedTableName(pInfo, &V, &U); +} + +// create table by using super table +// create table table_name using super_table_name tags(tag_values1, tag_values2) +%type create_from_stable{SCreatedTableInfo} +create_from_stable(A) ::= ifnotexists(U) ids(V) cpxName(Z) USING ids(X) cpxName(F) TAGS LP tagitemlist(Y) RP. { + X.n += F.n; + V.n += Z.n; + A = createNewChildTableInfo(&X, NULL, Y, &V, &U); +} + +create_from_stable(A) ::= ifnotexists(U) ids(V) cpxName(Z) USING ids(X) cpxName(F) LP tagNamelist(P) RP TAGS LP tagitemlist(Y) RP. { + X.n += F.n; + V.n += Z.n; + A = createNewChildTableInfo(&X, P, Y, &V, &U); +} + +%type tagNamelist{SArray*} +%destructor tagNamelist {taosArrayDestroy($$);} +tagNamelist(A) ::= tagNamelist(X) COMMA ids(Y). {taosArrayPush(X, &Y); A = X; } +tagNamelist(A) ::= ids(X). {A = taosArrayInit(4, sizeof(SToken)); taosArrayPush(A, &X);} + +// create stream +// create table table_name as select count(*) from super_table_name interval(time) +create_table_args(A) ::= ifnotexists(U) ids(V) cpxName(Z) AS select(S). { + A = tSetCreateTableInfo(NULL, NULL, S, TSQL_CREATE_STREAM); + setSqlInfo(pInfo, A, NULL, TSDB_SQL_CREATE_TABLE); + + V.n += Z.n; + setCreatedTableName(pInfo, &V, &U); +} + +%type column{TAOS_FIELD} +%type columnlist{SArray*} +%destructor columnlist {taosArrayDestroy($$);} +columnlist(A) ::= columnlist(X) COMMA column(Y). {taosArrayPush(X, &Y); A = X; } +columnlist(A) ::= column(X). {A = taosArrayInit(4, sizeof(TAOS_FIELD)); taosArrayPush(A, &X);} + +// The information used for a column is the name and type of column: +// tinyint smallint int bigint float double bool timestamp binary(x) nchar(x) +column(A) ::= ids(X) typename(Y). { + tSetColumnInfo(&A, &X, &Y); +} + +%type tagitemlist {SArray*} +%destructor tagitemlist {taosArrayDestroy($$);} + +%type tagitem {SVariant} +tagitemlist(A) ::= tagitemlist(X) COMMA tagitem(Y). { A = tListItemAppend(X, &Y, -1); } +tagitemlist(A) ::= tagitem(X). { A = tListItemAppend(NULL, &X, -1); } + +tagitem(A) ::= INTEGER(X). { toTSDBType(X.type); taosVariantCreate(&A, X.z, X.n, X.type); } +tagitem(A) ::= FLOAT(X). { toTSDBType(X.type); taosVariantCreate(&A, X.z, X.n, X.type); } +tagitem(A) ::= STRING(X). { toTSDBType(X.type); taosVariantCreate(&A, X.z, X.n, X.type); } +tagitem(A) ::= BOOL(X). { toTSDBType(X.type); taosVariantCreate(&A, X.z, X.n, X.type); } +tagitem(A) ::= NULL(X). { X.type = 0; taosVariantCreate(&A, X.z, X.n, X.type); } +tagitem(A) ::= NOW(X). { X.type = TSDB_DATA_TYPE_TIMESTAMP; taosVariantCreate(&A, X.z, X.n, X.type);} + +tagitem(A) ::= MINUS(X) INTEGER(Y).{ + X.n += Y.n; + X.type = Y.type; + toTSDBType(X.type); + taosVariantCreate(&A, X.z, X.n, X.type); +} + +tagitem(A) ::= MINUS(X) FLOAT(Y). { + X.n += Y.n; + X.type = Y.type; + toTSDBType(X.type); + taosVariantCreate(&A, X.z, X.n, X.type); +} + +tagitem(A) ::= PLUS(X) INTEGER(Y). { + X.n += Y.n; + X.type = Y.type; + toTSDBType(X.type); + taosVariantCreate(&A, X.z, X.n, X.type); +} + +tagitem(A) ::= PLUS(X) FLOAT(Y). { + X.n += Y.n; + X.type = Y.type; + toTSDBType(X.type); + taosVariantCreate(&A, X.z, X.n, X.type); +} + +//////////////////////// The SELECT statement ///////////////////////////////// +%type select {SSqlNode*} +%destructor select {destroySqlNode($$);} +select(A) ::= SELECT(T) selcollist(W) from(X) where_opt(Y) interval_option(K) sliding_opt(S) session_option(H) windowstate_option(D) fill_opt(F)groupby_opt(P) having_opt(N) orderby_opt(Z) slimit_opt(G) limit_opt(L). { + A = tSetQuerySqlNode(&T, W, X, Y, P, Z, &K, &H, &D, &S, F, &L, &G, N); +} + +select(A) ::= LP select(B) RP. {A = B;} + +%type union {SArray*} +%destructor union {destroyAllSqlNode($$);} +union(Y) ::= select(X). { Y = setSubclause(NULL, X); } +union(Y) ::= union(Z) UNION ALL select(X). { Y = appendSelectClause(Z, X); } + +cmd ::= union(X). { setSqlInfo(pInfo, X, NULL, TSDB_SQL_SELECT); } + +// Support for the SQL exprssion without from & where subclauses, e.g., +// select database() +// select server_version() +// select client_version() +// select server_state() +select(A) ::= SELECT(T) selcollist(W). { + A = tSetQuerySqlNode(&T, W, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); +} + +// selcollist is a list of expressions that are to become the return +// values of the SELECT statement. The "*" in statements like +// "SELECT * FROM ..." is encoded as a special expression with an opcode of TK_ALL. +%type selcollist {SArray*} +%destructor selcollist {tSqlExprListDestroy($$);} + +%type sclp {SArray*} +%destructor sclp {tSqlExprListDestroy($$);} +sclp(A) ::= selcollist(X) COMMA. {A = X;} +sclp(A) ::= . {A = 0;} +selcollist(A) ::= sclp(P) distinct(Z) expr(X) as(Y). { + A = tSqlExprListAppend(P, X, Z.n? &Z:0, Y.n?&Y:0); +} + +selcollist(A) ::= sclp(P) STAR. { + tSqlExpr *pNode = tSqlExprCreateIdValue(NULL, TK_ALL); + A = tSqlExprListAppend(P, pNode, 0, 0); +} + +// An option "AS " phrase that can follow one of the expressions that +// define the result set, or one of the tables in the FROM clause. +%type as {SToken} +as(X) ::= AS ids(Y). { X = Y; } +as(X) ::= ids(Y). { X = Y; } +as(X) ::= . { X.n = 0; } + +%type distinct {SToken} +distinct(X) ::= DISTINCT(Y). { X = Y; } +distinct(X) ::= . { X.n = 0;} + +// A complete FROM clause. +%type from {SRelationInfo*} +%destructor from {destroyRelationInfo($$);} +from(A) ::= FROM tablelist(X). {A = X;} +from(A) ::= FROM sub(X). {A = X;} + +%type sub {SRelationInfo*} +%destructor sub {destroyRelationInfo($$);} +sub(A) ::= LP union(Y) RP. {A = addSubquery(NULL, Y, NULL);} +sub(A) ::= LP union(Y) RP ids(Z). {A = addSubquery(NULL, Y, &Z);} +sub(A) ::= sub(X) COMMA LP union(Y) RP ids(Z).{A = addSubquery(X, Y, &Z);} + +%type tablelist {SRelationInfo*} +%destructor tablelist {destroyRelationInfo($$);} +tablelist(A) ::= ids(X) cpxName(Y). { + X.n += Y.n; + A = setTableNameList(NULL, &X, NULL); +} + +tablelist(A) ::= ids(X) cpxName(Y) ids(Z). { + X.n += Y.n; + A = setTableNameList(NULL, &X, &Z); +} + +tablelist(A) ::= tablelist(Y) COMMA ids(X) cpxName(Z). { + X.n += Z.n; + A = setTableNameList(Y, &X, NULL); +} + +tablelist(A) ::= tablelist(Y) COMMA ids(X) cpxName(Z) ids(F). { + X.n += Z.n; + A = setTableNameList(Y, &X, &F); +} + +// The value of interval should be the form of "number+[a,s,m,h,d,n,y]" or "now" +%type tmvar {SToken} +tmvar(A) ::= VARIABLE(X). {A = X;} + +%type interval_option {SIntervalVal} +interval_option(N) ::= intervalKey(A) LP tmvar(E) RP. {N.interval = E; N.offset.n = 0; N.token = A;} +interval_option(N) ::= intervalKey(A) LP tmvar(E) COMMA tmvar(X) RP. {N.interval = E; N.offset = X; N.token = A;} +interval_option(N) ::= . {memset(&N, 0, sizeof(N));} + +%type intervalKey {int32_t} +intervalKey(A) ::= INTERVAL. {A = TK_INTERVAL;} +intervalKey(A) ::= EVERY. {A = TK_EVERY; } + +%type session_option {SSessionWindowVal} +session_option(X) ::= . {X.col.n = 0; X.gap.n = 0;} +session_option(X) ::= SESSION LP ids(V) cpxName(Z) COMMA tmvar(Y) RP. { + V.n += Z.n; + X.col = V; + X.gap = Y; +} + +%type windowstate_option {SWindowStateVal} +windowstate_option(X) ::= . { X.col.n = 0; X.col.z = NULL;} +windowstate_option(X) ::= STATE_WINDOW LP ids(V) RP. { X.col = V; } + +%type fill_opt {SArray*} +%destructor fill_opt {taosArrayDestroy($$);} +fill_opt(N) ::= . { N = 0; } +fill_opt(N) ::= FILL LP ID(Y) COMMA tagitemlist(X) RP. { + SVariant A = {0}; + toTSDBType(Y.type); + taosVariantCreate(&A, Y.z, Y.n, Y.type); + + tVariantListInsert(X, &A, -1, 0); + N = X; +} + +fill_opt(N) ::= FILL LP ID(Y) RP. { + toTSDBType(Y.type); + N = tListItemAppendToken(NULL, &Y, -1); +} + +%type sliding_opt {SToken} +sliding_opt(K) ::= SLIDING LP tmvar(E) RP. {K = E; } +sliding_opt(K) ::= . {K.n = 0; K.z = NULL; K.type = 0; } + +%type orderby_opt {SArray*} +%destructor orderby_opt {taosArrayDestroy($$);} + +%type sortlist {SArray*} +%destructor sortlist {taosArrayDestroy($$);} + +%type sortitem {SVariant} +%destructor sortitem {taosVariantDestroy(&$$);} + +orderby_opt(A) ::= . {A = 0;} +orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;} + +sortlist(A) ::= sortlist(X) COMMA item(Y) sortorder(Z). { + A = tListItemAppend(X, &Y, Z); +} + +sortlist(A) ::= item(Y) sortorder(Z). { + A = tListItemAppend(NULL, &Y, Z); +} + +%type item {SVariant} +item(A) ::= ids(X) cpxName(Y). { + toTSDBType(X.type); + X.n += Y.n; + + taosVariantCreate(&A, X.z, X.n, X.type); +} + +%type sortorder {int} +sortorder(A) ::= ASC. { A = TSDB_ORDER_ASC; } +sortorder(A) ::= DESC. { A = TSDB_ORDER_DESC;} +sortorder(A) ::= . { A = TSDB_ORDER_ASC; } // Ascending order by default + +//group by clause +%type groupby_opt {SArray*} +%destructor groupby_opt {taosArrayDestroy($$);} +%type grouplist {SArray*} +%destructor grouplist {taosArrayDestroy($$);} + +groupby_opt(A) ::= . { A = 0;} +groupby_opt(A) ::= GROUP BY grouplist(X). { A = X;} + +grouplist(A) ::= grouplist(X) COMMA item(Y). { + A = tListItemAppend(X, &Y, -1); +} + +grouplist(A) ::= item(X). { + A = tListItemAppend(NULL, &X, -1); +} + +//having clause, ignore the input condition in having +%type having_opt {tSqlExpr*} +%destructor having_opt {tSqlExprDestroy($$);} +having_opt(A) ::=. {A = 0;} +having_opt(A) ::= HAVING expr(X). {A = X;} + +//limit-offset subclause +%type limit_opt {SLimit} +limit_opt(A) ::= . {A.limit = -1; A.offset = 0;} +limit_opt(A) ::= LIMIT signed(X). {A.limit = X; A.offset = 0;} +limit_opt(A) ::= LIMIT signed(X) OFFSET signed(Y). + { A.limit = X; A.offset = Y;} +limit_opt(A) ::= LIMIT signed(X) COMMA signed(Y). + { A.limit = Y; A.offset = X;} + +%type slimit_opt {SLimit} +slimit_opt(A) ::= . {A.limit = -1; A.offset = 0;} +slimit_opt(A) ::= SLIMIT signed(X). {A.limit = X; A.offset = 0;} +slimit_opt(A) ::= SLIMIT signed(X) SOFFSET signed(Y). + {A.limit = X; A.offset = Y;} +slimit_opt(A) ::= SLIMIT signed(X) COMMA signed(Y). + {A.limit = Y; A.offset = X;} + +%type where_opt {tSqlExpr*} +%destructor where_opt {tSqlExprDestroy($$);} + +where_opt(A) ::= . {A = 0;} +where_opt(A) ::= WHERE expr(X). {A = X;} + +/////////////////////////// Expression Processing ///////////////////////////// +// +%type expr {tSqlExpr*} +%destructor expr {tSqlExprDestroy($$);} + +expr(A) ::= LP(X) expr(Y) RP(Z). {A = Y; A->exprToken.z = X.z; A->exprToken.n = (Z.z - X.z + 1);} + +expr(A) ::= ID(X). { A = tSqlExprCreateIdValue(&X, TK_ID);} +expr(A) ::= ID(X) DOT ID(Y). { X.n += (1+Y.n); A = tSqlExprCreateIdValue(&X, TK_ID);} +expr(A) ::= ID(X) DOT STAR(Y). { X.n += (1+Y.n); A = tSqlExprCreateIdValue(&X, TK_ALL);} + +expr(A) ::= INTEGER(X). { A = tSqlExprCreateIdValue(&X, TK_INTEGER);} +expr(A) ::= MINUS(X) INTEGER(Y). { X.n += Y.n; X.type = TK_INTEGER; A = tSqlExprCreateIdValue(&X, TK_INTEGER);} +expr(A) ::= PLUS(X) INTEGER(Y). { X.n += Y.n; X.type = TK_INTEGER; A = tSqlExprCreateIdValue(&X, TK_INTEGER);} +expr(A) ::= FLOAT(X). { A = tSqlExprCreateIdValue(&X, TK_FLOAT);} +expr(A) ::= MINUS(X) FLOAT(Y). { X.n += Y.n; X.type = TK_FLOAT; A = tSqlExprCreateIdValue(&X, TK_FLOAT);} +expr(A) ::= PLUS(X) FLOAT(Y). { X.n += Y.n; X.type = TK_FLOAT; A = tSqlExprCreateIdValue(&X, TK_FLOAT);} +expr(A) ::= STRING(X). { A = tSqlExprCreateIdValue(&X, TK_STRING);} +expr(A) ::= NOW(X). { A = tSqlExprCreateIdValue(&X, TK_NOW); } +expr(A) ::= VARIABLE(X). { A = tSqlExprCreateIdValue(&X, TK_VARIABLE);} +expr(A) ::= PLUS(X) VARIABLE(Y). { X.n += Y.n; X.type = TK_VARIABLE; A = tSqlExprCreateIdValue(&X, TK_VARIABLE);} +expr(A) ::= MINUS(X) VARIABLE(Y). { X.n += Y.n; X.type = TK_VARIABLE; A = tSqlExprCreateIdValue(&X, TK_VARIABLE);} +expr(A) ::= BOOL(X). { A = tSqlExprCreateIdValue(&X, TK_BOOL);} +expr(A) ::= NULL(X). { A = tSqlExprCreateIdValue(&X, TK_NULL);} + +// ordinary functions: min(x), max(x), top(k, 20) +expr(A) ::= ID(X) LP exprlist(Y) RP(E). { tAppendFuncName(pInfo->funcs, &X); A = tSqlExprCreateFunction(Y, &X, &E, X.type); } + +// for parsing sql functions with wildcard for parameters. e.g., count(*)/first(*)/last(*) operation +expr(A) ::= ID(X) LP STAR RP(Y). { tAppendFuncName(pInfo->funcs, &X); A = tSqlExprCreateFunction(NULL, &X, &Y, X.type); } + +// is (not) null expression +expr(A) ::= expr(X) IS NULL. {A = tSqlExprCreate(X, NULL, TK_ISNULL);} +expr(A) ::= expr(X) IS NOT NULL. {A = tSqlExprCreate(X, NULL, TK_NOTNULL);} + +// relational expression +expr(A) ::= expr(X) LT expr(Y). {A = tSqlExprCreate(X, Y, TK_LT);} +expr(A) ::= expr(X) GT expr(Y). {A = tSqlExprCreate(X, Y, TK_GT);} +expr(A) ::= expr(X) LE expr(Y). {A = tSqlExprCreate(X, Y, TK_LE);} +expr(A) ::= expr(X) GE expr(Y). {A = tSqlExprCreate(X, Y, TK_GE);} +expr(A) ::= expr(X) NE expr(Y). {A = tSqlExprCreate(X, Y, TK_NE);} +expr(A) ::= expr(X) EQ expr(Y). {A = tSqlExprCreate(X, Y, TK_EQ);} + +expr(A) ::= expr(X) BETWEEN expr(Y) AND expr(Z). { tSqlExpr* X2 = tSqlExprClone(X); A = tSqlExprCreate(tSqlExprCreate(X, Y, TK_GE), tSqlExprCreate(X2, Z, TK_LE), TK_AND);} + +expr(A) ::= expr(X) AND expr(Y). {A = tSqlExprCreate(X, Y, TK_AND);} +expr(A) ::= expr(X) OR expr(Y). {A = tSqlExprCreate(X, Y, TK_OR); } + +// binary arithmetic expression +expr(A) ::= expr(X) PLUS expr(Y). {A = tSqlExprCreate(X, Y, TK_PLUS); } +expr(A) ::= expr(X) MINUS expr(Y). {A = tSqlExprCreate(X, Y, TK_MINUS); } +expr(A) ::= expr(X) STAR expr(Y). {A = tSqlExprCreate(X, Y, TK_STAR); } +expr(A) ::= expr(X) SLASH expr(Y). {A = tSqlExprCreate(X, Y, TK_DIVIDE);} +expr(A) ::= expr(X) REM expr(Y). {A = tSqlExprCreate(X, Y, TK_REM); } + +// like expression +expr(A) ::= expr(X) LIKE expr(Y). {A = tSqlExprCreate(X, Y, TK_LIKE); } + +// match expression +expr(A) ::= expr(X) MATCH expr(Y). {A = tSqlExprCreate(X, Y, TK_MATCH); } +expr(A) ::= expr(X) NMATCH expr(Y). {A = tSqlExprCreate(X, Y, TK_NMATCH); } + +//in expression +expr(A) ::= expr(X) IN LP exprlist(Y) RP. {A = tSqlExprCreate(X, (tSqlExpr*)Y, TK_IN); } + +%type exprlist {SArray*} +%destructor exprlist {tSqlExprListDestroy($$);} + +%type expritem {tSqlExpr*} +%destructor expritem {tSqlExprDestroy($$);} + +exprlist(A) ::= exprlist(X) COMMA expritem(Y). {A = tSqlExprListAppend(X,Y,0, 0);} +exprlist(A) ::= expritem(X). {A = tSqlExprListAppend(0,X,0, 0);} +expritem(A) ::= expr(X). {A = X;} +expritem(A) ::= . {A = 0;} + +///////////////////////////////////reset query cache////////////////////////////////////// +cmd ::= RESET QUERY CACHE. { setDCLSqlElems(pInfo, TSDB_SQL_RESET_CACHE, 0);} + +///////////////////////////////////sync replica database////////////////////////////////// +cmd ::= SYNCDB ids(X) REPLICA.{ setDCLSqlElems(pInfo, TSDB_SQL_SYNC_DB_REPLICA, 1, &X);} + +///////////////////////////////////ALTER TABLE statement////////////////////////////////// +cmd ::= ALTER TABLE ids(X) cpxName(F) ADD COLUMN columnlist(A). { + X.n += F.n; + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, A, NULL, TSDB_ALTER_TABLE_ADD_COLUMN, -1); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + +cmd ::= ALTER TABLE ids(X) cpxName(F) DROP COLUMN ids(A). { + X.n += F.n; + + toTSDBType(A.type); + SArray* K = tListItemAppendToken(NULL, &A, -1); + + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, K, TSDB_ALTER_TABLE_DROP_COLUMN, -1); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + +cmd ::= ALTER TABLE ids(X) cpxName(F) MODIFY COLUMN columnlist(A). { + X.n += F.n; + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, A, NULL, TSDB_ALTER_TABLE_CHANGE_COLUMN, -1); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + +//////////////////////////////////ALTER TAGS statement///////////////////////////////////// +cmd ::= ALTER TABLE ids(X) cpxName(Y) ADD TAG columnlist(A). { + X.n += Y.n; + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, A, NULL, TSDB_ALTER_TABLE_ADD_TAG_COLUMN, -1); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} +cmd ::= ALTER TABLE ids(X) cpxName(Z) DROP TAG ids(Y). { + X.n += Z.n; + + toTSDBType(Y.type); + SArray* A = tListItemAppendToken(NULL, &Y, -1); + + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, A, TSDB_ALTER_TABLE_DROP_TAG_COLUMN, -1); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + +cmd ::= ALTER TABLE ids(X) cpxName(F) CHANGE TAG ids(Y) ids(Z). { + X.n += F.n; + + toTSDBType(Y.type); + SArray* A = tListItemAppendToken(NULL, &Y, -1); + + toTSDBType(Z.type); + A = tListItemAppendToken(A, &Z, -1); + + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, A, TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN, -1); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + +cmd ::= ALTER TABLE ids(X) cpxName(F) SET TAG ids(Y) EQ tagitem(Z). { + X.n += F.n; + + toTSDBType(Y.type); + SArray* A = tListItemAppendToken(NULL, &Y, -1); + A = tListItemAppend(A, &Z, -1); + + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, A, TSDB_ALTER_TABLE_UPDATE_TAG_VAL, -1); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + +cmd ::= ALTER TABLE ids(X) cpxName(F) MODIFY TAG columnlist(A). { + X.n += F.n; + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, A, NULL, TSDB_ALTER_TABLE_MODIFY_TAG_COLUMN, -1); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + +///////////////////////////////////ALTER STABLE statement////////////////////////////////// +cmd ::= ALTER STABLE ids(X) cpxName(F) ADD COLUMN columnlist(A). { + X.n += F.n; + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, A, NULL, TSDB_ALTER_TABLE_ADD_COLUMN, TSDB_SUPER_TABLE); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + +cmd ::= ALTER STABLE ids(X) cpxName(F) DROP COLUMN ids(A). { + X.n += F.n; + + toTSDBType(A.type); + SArray* K = tListItemAppendToken(NULL, &A, -1); + + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, K, TSDB_ALTER_TABLE_DROP_COLUMN, TSDB_SUPER_TABLE); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + +cmd ::= ALTER STABLE ids(X) cpxName(F) MODIFY COLUMN columnlist(A). { + X.n += F.n; + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, A, NULL, TSDB_ALTER_TABLE_CHANGE_COLUMN, TSDB_SUPER_TABLE); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + +//////////////////////////////////ALTER TAGS statement///////////////////////////////////// +cmd ::= ALTER STABLE ids(X) cpxName(Y) ADD TAG columnlist(A). { + X.n += Y.n; + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, A, NULL, TSDB_ALTER_TABLE_ADD_TAG_COLUMN, TSDB_SUPER_TABLE); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} +cmd ::= ALTER STABLE ids(X) cpxName(Z) DROP TAG ids(Y). { + X.n += Z.n; + + toTSDBType(Y.type); + SArray* A = tListItemAppendToken(NULL, &Y, -1); + + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, A, TSDB_ALTER_TABLE_DROP_TAG_COLUMN, TSDB_SUPER_TABLE); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + +cmd ::= ALTER STABLE ids(X) cpxName(F) CHANGE TAG ids(Y) ids(Z). { + X.n += F.n; + + toTSDBType(Y.type); + SArray* A = tListItemAppendToken(NULL, &Y, -1); + + toTSDBType(Z.type); + A = tListItemAppendToken(A, &Z, -1); + + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, A, TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN, TSDB_SUPER_TABLE); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + +cmd ::= ALTER STABLE ids(X) cpxName(F) SET TAG ids(Y) EQ tagitem(Z). { + X.n += F.n; + + toTSDBType(Y.type); + SArray* A = tListItemAppendToken(NULL, &Y, -1); + A = tListItemAppend(A, &Z, -1); + + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, NULL, A, TSDB_ALTER_TABLE_UPDATE_TAG_VAL, TSDB_SUPER_TABLE); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + +cmd ::= ALTER STABLE ids(X) cpxName(F) MODIFY TAG columnlist(A). { + X.n += F.n; + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&X, A, NULL, TSDB_ALTER_TABLE_MODIFY_TAG_COLUMN, TSDB_SUPER_TABLE); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + +////////////////////////////////////////kill statement/////////////////////////////////////// +cmd ::= KILL CONNECTION INTEGER(Y). {setKillSql(pInfo, TSDB_SQL_KILL_CONNECTION, &Y);} +cmd ::= KILL STREAM INTEGER(X) COLON(Z) INTEGER(Y). {X.n += (Z.n + Y.n); setKillSql(pInfo, TSDB_SQL_KILL_STREAM, &X);} +cmd ::= KILL QUERY INTEGER(X) COLON(Z) INTEGER(Y). {X.n += (Z.n + Y.n); setKillSql(pInfo, TSDB_SQL_KILL_QUERY, &X);} + +%fallback ID ABORT AFTER ASC ATTACH BEFORE BEGIN CASCADE CLUSTER CONFLICT COPY DATABASE DEFERRED + DELIMITERS DESC DETACH EACH END EXPLAIN FAIL FOR GLOB IGNORE IMMEDIATE INITIALLY INSTEAD + LIKE MATCH NMATCH KEY OF OFFSET RAISE REPLACE RESTRICT ROW STATEMENT TRIGGER VIEW ALL + NOW IPTOKEN SEMI NONE PREV LINEAR IMPORT TBNAME JOIN STABLE NULL INSERT INTO VALUES. diff --git a/src/util/inc/ttoken.h b/source/libs/parser/inc/ttoken.h similarity index 88% rename from src/util/inc/ttoken.h rename to source/libs/parser/inc/ttoken.h index f62329183f82a57bcf65d136aaec65d8babd5f71..bacabe299e8c7c6dff957e9dd9addc10e2b2aef7 100644 --- a/src/util/inc/ttoken.h +++ b/source/libs/parser/inc/ttoken.h @@ -21,21 +21,14 @@ extern "C" { #endif #include "os.h" -#include "tutil.h" #include "ttokendef.h" -#define TSQL_TBNAME "TBNAME" -#define TSQL_TBNAME_L "tbname" - -#define TSQL_BLOCK_DIST "_BLOCK_DIST" -#define TSQL_BLOCK_DIST_L "_block_dist" - // used to denote the minimum unite in sql parsing -typedef struct SStrToken { +typedef struct SToken { uint32_t n; uint32_t type; char *z; -} SStrToken; +} SToken; /** * check if it is a number or not @@ -61,7 +54,7 @@ uint32_t tGetToken(char *z, uint32_t *tokenType); * @param isPrevOptr * @return */ -SStrToken tStrGetToken(char *str, int32_t *i, bool isPrevOptr); +SToken tStrGetToken(char *str, int32_t *i, bool isPrevOptr); /** * check if it is a keyword or not @@ -76,7 +69,7 @@ bool taosIsKeyWordToken(const char *z, int32_t len); * @param pToken * @return token type, if it is not a number, TK_ILLEGAL will return */ -static FORCE_INLINE int32_t tGetNumericStringType(const SStrToken* pToken) { +static FORCE_INLINE int32_t tGetNumericStringType(const SToken* pToken) { const char* z = pToken->z; int32_t type = TK_ILLEGAL; @@ -181,9 +174,9 @@ static FORCE_INLINE int32_t tGetNumericStringType(const SStrToken* pToken) { void taosCleanupKeywordsTable(); -SStrToken tscReplaceStrToken(char **str, SStrToken *token, const char* newToken); +SToken tscReplaceStrToken(char **str, SToken *token, const char* newToken); -SStrToken taosTokenDup(SStrToken* pToken, char* buf, int32_t len); +SToken taosTokenDup(SToken* pToken, char* buf, int32_t len); #ifdef __cplusplus } diff --git a/src/inc/ttokendef.h b/source/libs/parser/inc/ttokendef.h similarity index 100% rename from src/inc/ttokendef.h rename to source/libs/parser/inc/ttokendef.h diff --git a/source/libs/parser/src/astGenerator.c b/source/libs/parser/src/astGenerator.c new file mode 100644 index 0000000000000000000000000000000000000000..bc4d13e02aab4998565db9671205707ab06cc16e --- /dev/null +++ b/source/libs/parser/src/astGenerator.c @@ -0,0 +1,1177 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#include "taos.h" +#include "os.h" +#include "astGenerator.h" +#include "tmsgtype.h" + +int32_t tStrToInteger(const char* z, int16_t type, int32_t n, int64_t* value, bool issigned) { + errno = 0; + int32_t ret = 0; + + char* endPtr = NULL; + if (type == TK_FLOAT) { + double v = strtod(z, &endPtr); + if ((errno == ERANGE && v == HUGE_VALF) || isinf(v) || isnan(v)) { + ret = -1; + } else if ((issigned && (v < INT64_MIN || v > INT64_MAX)) || ((!issigned) && (v < 0 || v > UINT64_MAX))) { + ret = -1; + } else { + *value = (int64_t) round(v); + } + + errno = 0; + return ret; + } + + int32_t radix = 10; + if (type == TK_HEX) { + radix = 16; + } else if (type == TK_BIN) { + radix = 2; + } + + // the string may be overflow according to errno + if (!issigned) { + const char *p = z; + while(*p != 0 && *p == ' ') p++; + if (*p != 0 && *p == '-') { return -1;} + + *value = strtoull(z, &endPtr, radix); + } else { + *value = strtoll(z, &endPtr, radix); + } + + // not a valid integer number, return error + if (endPtr - z != n || errno == ERANGE) { + ret = -1; + } + + errno = 0; + return ret; +} + +SArray *tListItemAppend(SArray *pList, SVariant *pVar, uint8_t sortOrder) { + if (pList == NULL) { + pList = taosArrayInit(4, sizeof(SListItem)); + } + + if (pVar == NULL) { + return pList; + } + + /* + * Here we do not employ the assign function, since we need the pz attribute of structure , which is the point to char string. + * Otherwise, the original pointer may be lost, which causes memory leak. + */ + SListItem item; + item.pVar = *pVar; + item.sortOrder = sortOrder; + + taosArrayPush(pList, &item); + return pList; +} + +SArray *tListItemInsert(SArray *pList, SVariant *pVar, uint8_t sortOrder, int32_t index) { + if (pList == NULL || pVar == NULL || index >= taosArrayGetSize(pList)) { + return tListItemAppend(pList, pVar, sortOrder); + } + + SListItem item; + item.pVar = *pVar; + item.sortOrder = sortOrder; + + taosArrayInsert(pList, index, &item); + return pList; +} + +SArray *tListItemAppendToken(SArray *pList, SToken *pAliasToken, uint8_t sortOrder) { + if (pList == NULL) { + pList = taosArrayInit(4, sizeof(SListItem)); + } + + if (pAliasToken) { + SListItem item; + assert(0); +// taosVariantCreate(&item.pVar, pAliasToken); + item.sortOrder = sortOrder; + + taosArrayPush(pList, &item); + } + + return pList; +} + +SRelationInfo *setTableNameList(SRelationInfo *pRelationInfo, SToken *pName, SToken *pAlias) { + if (pRelationInfo == NULL) { + pRelationInfo = calloc(1, sizeof(SRelationInfo)); + pRelationInfo->list = taosArrayInit(4, sizeof(SRelElementPair)); + } + + pRelationInfo->type = SQL_NODE_FROM_TABLELIST; + SRelElementPair p = {.tableName = *pName}; + if (pAlias != NULL) { + p.aliasName = *pAlias; + } else { + TPARSER_SET_NONE_TOKEN(p.aliasName); + } + + taosArrayPush(pRelationInfo->list, &p); + return pRelationInfo; +} + +void *destroyRelationInfo(SRelationInfo *pRelationInfo) { + if (pRelationInfo == NULL) { + return NULL; + } + + if (pRelationInfo->type == SQL_NODE_FROM_TABLELIST) { + taosArrayDestroy(pRelationInfo->list); + } else { + size_t size = taosArrayGetSize(pRelationInfo->list); + for(int32_t i = 0; i < size; ++i) { + SArray* pa = taosArrayGetP(pRelationInfo->list, i); + destroyAllSqlNode(pa); + } + taosArrayDestroy(pRelationInfo->list); + } + + tfree(pRelationInfo); + return NULL; +} + +SRelationInfo *addSubquery(SRelationInfo *pRelationInfo, SArray *pSub, SToken *pAlias) { + if (pRelationInfo == NULL) { + pRelationInfo = calloc(1, sizeof(SRelationInfo)); + pRelationInfo->list = taosArrayInit(4, sizeof(SRelElementPair)); + } + + pRelationInfo->type = SQL_NODE_FROM_SUBQUERY; + + SRelElementPair p = {.pSubquery = pSub}; + if (pAlias != NULL) { + p.aliasName = *pAlias; + } else { + TPARSER_SET_NONE_TOKEN(p.aliasName); + } + + taosArrayPush(pRelationInfo->list, &p); + return pRelationInfo; +} + +// sql expr leaf node +// todo Evalute the value during the validation process of AST. +tSqlExpr *tSqlExprCreateIdValue(SToken *pToken, int32_t optrType) { + tSqlExpr *pSqlExpr = calloc(1, sizeof(tSqlExpr)); + + if (pToken != NULL) { + pSqlExpr->exprToken = *pToken; + } + + if (optrType == TK_NULL) { +// if (pToken) { +// pToken->type = TSDB_DATA_TYPE_NULL; +// tVariantCreate(&pSqlExpr->value, pToken); +// } + pSqlExpr->tokenId = optrType; + pSqlExpr->type = SQL_NODE_VALUE; + } else if (optrType == TK_INTEGER || optrType == TK_STRING || optrType == TK_FLOAT || optrType == TK_BOOL) { +// if (pToken) { +// toTSDBType(pToken->type); +// tVariantCreate(&pSqlExpr->value, pToken); +// } + pSqlExpr->tokenId = optrType; + pSqlExpr->type = SQL_NODE_VALUE; + } else if (optrType == TK_NOW) { + // use nanosecond by default TODO set value after getting database precision +// pSqlExpr->value.i64 = taosGetTimestamp(TSDB_TIME_PRECISION_NANO); +// pSqlExpr->value.nType = TSDB_DATA_TYPE_BIGINT; + pSqlExpr->tokenId = TK_TIMESTAMP; // TK_TIMESTAMP used to denote the time value is in microsecond + pSqlExpr->type = SQL_NODE_VALUE; +// pSqlExpr->flags |= 1 << EXPR_FLAG_NS_TIMESTAMP; + } else if (optrType == TK_VARIABLE) { + // use nanosecond by default + // TODO set value after getting database precision +// if (pToken) { +// char unit = 0; +// int32_t ret = parseAbsoluteDuration(pToken->z, pToken->n, &pSqlExpr->value.i64, &unit, TSDB_TIME_PRECISION_NANO); +// if (ret != TSDB_CODE_SUCCESS) { +// terrno = TSDB_CODE_TSC_SQL_SYNTAX_ERROR; +// } +// } + +// pSqlExpr->flags |= 1 << EXPR_FLAG_NS_TIMESTAMP; +// pSqlExpr->flags |= 1 << EXPR_FLAG_TIMESTAMP_VAR; +// pSqlExpr->value.nType = TSDB_DATA_TYPE_BIGINT; + pSqlExpr->tokenId = TK_TIMESTAMP; + pSqlExpr->type = SQL_NODE_VALUE; + } else { + // Here it must be the column name (tk_id) if it is not a number or string. + assert(optrType == TK_ID || optrType == TK_ALL); + if (pToken != NULL) { + pSqlExpr->columnName = *pToken; + } + + pSqlExpr->tokenId = optrType; + pSqlExpr->type = SQL_NODE_TABLE_COLUMN; + } + + return pSqlExpr; +} + +tSqlExpr *tSqlExprCreateFunction(SArray *pParam, SToken *pFuncToken, SToken *endToken, int32_t optType) { + if (pFuncToken == NULL) { + return NULL; + } + + tSqlExpr *pExpr = calloc(1, sizeof(tSqlExpr)); + pExpr->tokenId = optType; + pExpr->type = SQL_NODE_SQLFUNCTION; + pExpr->Expr.paramList = pParam; + + int32_t len = (int32_t)((endToken->z + endToken->n) - pFuncToken->z); + pExpr->Expr.operand = (*pFuncToken); + + pExpr->exprToken.n = len; + pExpr->exprToken.z = pFuncToken->z; + pExpr->exprToken.type = pFuncToken->type; + + return pExpr; +} + +SArray *tAppendFuncName(SArray *pList, SToken *pToken) { + assert(pList != NULL && pToken != NULL); + taosArrayPush(pList, pToken); + return pList; +} + +tSqlExpr *tSqlExprCreate(tSqlExpr *pLeft, tSqlExpr *pRight, int32_t optrType) { + tSqlExpr *pExpr = calloc(1, sizeof(tSqlExpr)); + pExpr->type = SQL_NODE_EXPR; + + if (pLeft != NULL && pRight != NULL && (optrType != TK_IN)) { + char* endPos = pRight->exprToken.z + pRight->exprToken.n; + pExpr->exprToken.z = pLeft->exprToken.z; + pExpr->exprToken.n = (uint32_t)(endPos - pExpr->exprToken.z); + pExpr->exprToken.type = pLeft->exprToken.type; + } + + if ((pLeft != NULL && pRight != NULL) && + (optrType == TK_PLUS || optrType == TK_MINUS || optrType == TK_STAR || optrType == TK_DIVIDE || optrType == TK_REM)) { + /* + * if a exprToken is noted as the TK_TIMESTAMP, the time precision is microsecond + * Otherwise, the time precision is adaptive, determined by the time precision from databases. + */ + if ((pLeft->tokenId == TK_INTEGER && pRight->tokenId == TK_INTEGER) || + (pLeft->tokenId == TK_TIMESTAMP && pRight->tokenId == TK_TIMESTAMP)) { + pExpr->value.nType = TSDB_DATA_TYPE_BIGINT; + pExpr->tokenId = pLeft->tokenId; + pExpr->type = SQL_NODE_VALUE; + + switch (optrType) { + case TK_PLUS: { + pExpr->value.i64 = pLeft->value.i64 + pRight->value.i64; + break; + } + + case TK_MINUS: { + pExpr->value.i64 = pLeft->value.i64 - pRight->value.i64; + break; + } + case TK_STAR: { + pExpr->value.i64 = pLeft->value.i64 * pRight->value.i64; + break; + } + case TK_DIVIDE: { + pExpr->tokenId = TK_FLOAT; + pExpr->value.nType = TSDB_DATA_TYPE_DOUBLE; + pExpr->value.d = (double)pLeft->value.i64 / pRight->value.i64; + break; + } + case TK_REM: { + pExpr->value.i64 = pLeft->value.i64 % pRight->value.i64; + break; + } + } + + tSqlExprDestroy(pLeft); + tSqlExprDestroy(pRight); + } else if ((pLeft->tokenId == TK_FLOAT && pRight->tokenId == TK_INTEGER) || + (pLeft->tokenId == TK_INTEGER && pRight->tokenId == TK_FLOAT) || + (pLeft->tokenId == TK_FLOAT && pRight->tokenId == TK_FLOAT)) { + pExpr->value.nType = TSDB_DATA_TYPE_DOUBLE; + pExpr->tokenId = TK_FLOAT; + pExpr->type = SQL_NODE_VALUE; + + double left = (pLeft->value.nType == TSDB_DATA_TYPE_DOUBLE) ? pLeft->value.d : pLeft->value.i64; + double right = (pRight->value.nType == TSDB_DATA_TYPE_DOUBLE) ? pRight->value.d : pRight->value.i64; + + switch (optrType) { + case TK_PLUS: { + pExpr->value.d = left + right; + break; + } + case TK_MINUS: { + pExpr->value.d = left - right; + break; + } + case TK_STAR: { + pExpr->value.d = left * right; + break; + } + case TK_DIVIDE: { + pExpr->value.d = left / right; + break; + } + case TK_REM: { + pExpr->value.d = left - ((int64_t)(left / right)) * right; + break; + } + } + + tSqlExprDestroy(pLeft); + tSqlExprDestroy(pRight); + + } else { + pExpr->tokenId = optrType; + pExpr->pLeft = pLeft; + pExpr->pRight = pRight; + } + } else if (optrType == TK_IN) { + pExpr->tokenId = optrType; + pExpr->pLeft = pLeft; + + tSqlExpr *pRSub = calloc(1, sizeof(tSqlExpr)); + pRSub->tokenId = TK_SET; // TODO refactor ..... + pRSub->Expr.paramList = (SArray *)pRight; + + pExpr->pRight = pRSub; + } else { + pExpr->tokenId = optrType; + pExpr->pLeft = pLeft; + + if (pLeft != NULL && pRight == NULL) { + pRight = calloc(1, sizeof(tSqlExpr)); + } + + pExpr->pRight = pRight; + } + + return pExpr; +} + +tSqlExpr *tSqlExprClone(tSqlExpr *pSrc) { + tSqlExpr *pExpr = malloc(sizeof(tSqlExpr)); + memcpy(pExpr, pSrc, sizeof(*pSrc)); + + if (pSrc->pLeft) { + pExpr->pLeft = tSqlExprClone(pSrc->pLeft); + } + + if (pSrc->pRight) { + pExpr->pRight = tSqlExprClone(pSrc->pRight); + } + + memset(&pExpr->value, 0, sizeof(pExpr->value)); + taosVariantAssign(&pExpr->value, &pSrc->value); + + //we don't clone paramList now because clone is only used for between/and + assert(pSrc->Expr.paramList == NULL); + return pExpr; +} + +void tSqlExprCompact(tSqlExpr **pExpr) { + + if (*pExpr == NULL || tSqlExprIsParentOfLeaf(*pExpr)) { + return; + } + + if ((*pExpr)->pLeft) { + tSqlExprCompact(&(*pExpr)->pLeft); + } + + if ((*pExpr)->pRight) { + tSqlExprCompact(&(*pExpr)->pRight); + } + + if ((*pExpr)->pLeft == NULL && (*pExpr)->pRight == NULL && ((*pExpr)->tokenId == TK_OR || (*pExpr)->tokenId == TK_AND)) { + tSqlExprDestroy(*pExpr); + *pExpr = NULL; + } else if ((*pExpr)->pLeft == NULL && (*pExpr)->pRight != NULL) { + tSqlExpr* tmpPtr = (*pExpr)->pRight; + (*pExpr)->pRight = NULL; + + tSqlExprDestroy(*pExpr); + (*pExpr) = tmpPtr; + } else if ((*pExpr)->pRight == NULL && (*pExpr)->pLeft != NULL) { + tSqlExpr* tmpPtr = (*pExpr)->pLeft; + (*pExpr)->pLeft = NULL; + + tSqlExprDestroy(*pExpr); + (*pExpr) = tmpPtr; + } +} + +bool tSqlExprIsLeaf(tSqlExpr *pExpr) { + return (pExpr->pRight == NULL && pExpr->pLeft == NULL) && + (pExpr->tokenId == 0 || + (pExpr->tokenId == TK_ID) || + (pExpr->tokenId >= TK_BOOL && pExpr->tokenId <= TK_NCHAR) || + (pExpr->tokenId == TK_NULL) || + (pExpr->tokenId == TK_SET)); +} + +bool tSqlExprIsParentOfLeaf(tSqlExpr *pExpr) { + return (pExpr->pLeft != NULL && pExpr->pRight != NULL) && + (tSqlExprIsLeaf(pExpr->pLeft) && tSqlExprIsLeaf(pExpr->pRight)); +} + +static void doDestroySqlExprNode(tSqlExpr *pExpr) { + if (pExpr == NULL) { + return; + } + + taosVariantDestroy(&pExpr->value); + tSqlExprListDestroy(pExpr->Expr.paramList); + free(pExpr); +} + +void tSqlExprDestroy(tSqlExpr *pExpr) { + if (pExpr == NULL) { + return; + } + + tSqlExprDestroy(pExpr->pLeft); + pExpr->pLeft = NULL; + tSqlExprDestroy(pExpr->pRight); + pExpr->pRight = NULL; + + doDestroySqlExprNode(pExpr); +} + +SArray * tSqlExprListAppend(SArray *pList, tSqlExpr *pNode, SToken *pDistinct, SToken *pToken) { + + if (pList == NULL) { + pList = taosArrayInit(4, sizeof(tSqlExprItem)); + } + + if (pNode || pToken) { + struct tSqlExprItem item = {0}; + + item.pNode = pNode; + item.distinct = (pDistinct != NULL); + + if (pToken) { // set the as clause + item.aliasName = malloc(pToken->n + 1); + strncpy(item.aliasName, pToken->z, pToken->n); + item.aliasName[pToken->n] = 0; + + strdequote(item.aliasName); + } + + taosArrayPush(pList, &item); + } + + return pList; +} + +static void freeExprElem(void* item) { + tSqlExprItem* exprItem = item; + + tfree(exprItem->aliasName); + tSqlExprDestroy(exprItem->pNode); +} + +void tSqlExprListDestroy(SArray *pList) { + if (pList == NULL) { + return; + } + taosArrayDestroyEx(pList, freeExprElem); +} + +SSqlNode *tSetQuerySqlNode(SToken *pSelectToken, SArray *pSelNodeList, SRelationInfo *pFrom, tSqlExpr *pWhere, + SArray *pGroupby, SArray *pSortOrder, SIntervalVal *pInterval, + SSessionWindowVal *pSession, SWindowStateVal *pWindowStateVal, SToken *pSliding, SArray *pFill, SLimit *pLimit, + SLimit *psLimit, tSqlExpr *pHaving) { + assert(pSelNodeList != NULL); + + SSqlNode *pSqlNode = calloc(1, sizeof(SSqlNode)); + + // all later sql string are belonged to the stream sql + pSqlNode->sqlstr = *pSelectToken; + pSqlNode->sqlstr.n = (uint32_t)strlen(pSqlNode->sqlstr.z); + + pSqlNode->pSelNodeList = pSelNodeList; + pSqlNode->from = pFrom; + pSqlNode->pGroupby = pGroupby; + pSqlNode->pSortOrder = pSortOrder; + pSqlNode->pWhere = pWhere; + pSqlNode->fillType = pFill; + pSqlNode->pHaving = pHaving; + + if (pLimit != NULL) { + pSqlNode->limit = *pLimit; + } else { + pSqlNode->limit.limit = -1; + pSqlNode->limit.offset = 0; + } + + if (psLimit != NULL) { + pSqlNode->slimit = *psLimit; + } else { + pSqlNode->slimit.limit = -1; + pSqlNode->slimit.offset = 0; + } + + if (pInterval != NULL) { + pSqlNode->interval = *pInterval; + } else { + TPARSER_SET_NONE_TOKEN(pSqlNode->interval.interval); + TPARSER_SET_NONE_TOKEN(pSqlNode->interval.offset); + } + + if (pSliding != NULL) { + pSqlNode->sliding = *pSliding; + } else { + TPARSER_SET_NONE_TOKEN(pSqlNode->sliding); + } + + if (pSession != NULL) { + pSqlNode->sessionVal = *pSession; + } else { + TPARSER_SET_NONE_TOKEN(pSqlNode->sessionVal.gap); + TPARSER_SET_NONE_TOKEN(pSqlNode->sessionVal.col); + } + + if (pWindowStateVal != NULL) { + pSqlNode->windowstateVal = *pWindowStateVal; + } else { + TPARSER_SET_NONE_TOKEN(pSqlNode->windowstateVal.col); + } + + return pSqlNode; +} + +static FORCE_INLINE int32_t tStrTokenCompare(SToken* left, SToken* right) { + return (left->type == right->type && left->n == right->n && strncasecmp(left->z, right->z, left->n) == 0) ? 0 : 1; +} + +int32_t tSqlExprCompare(tSqlExpr *left, tSqlExpr *right) { + if ((left == NULL && right) || (left && right == NULL) || (left == NULL && right == NULL)) { + return 1; + } + + if (left->type != right->type) { + return 1; + } + + if (left->tokenId != right->tokenId) { + return 1; + } + + if ((left->pLeft && right->pLeft == NULL) + || (left->pLeft == NULL && right->pLeft) + || (left->pRight && right->pRight == NULL) + || (left->pRight == NULL && right->pRight) + || (left->Expr.paramList && right->Expr.paramList == NULL) + || (left->Expr.paramList == NULL && right->Expr.paramList)) { + return 1; + } + + if (taosVariantCompare(&left->value, &right->value)) { + return 1; + } + + if (tStrTokenCompare(&left->columnName, &right->columnName)) { + return 1; + } + + if (right->Expr.paramList && left->Expr.paramList) { + size_t size = taosArrayGetSize(right->Expr.paramList); + if (left->Expr.paramList && taosArrayGetSize(left->Expr.paramList) != size) { + return 1; + } + + for (int32_t i = 0; i < size; i++) { + tSqlExprItem* pLeftElem = taosArrayGet(left->Expr.paramList, i); + tSqlExpr* pSubLeft = pLeftElem->pNode; + tSqlExprItem* pRightElem = taosArrayGet(right->Expr.paramList, i); + tSqlExpr* pSubRight = pRightElem->pNode; + + if (tSqlExprCompare(pSubLeft, pSubRight)) { + return 1; + } + } + } + + if (left->pLeft && tSqlExprCompare(left->pLeft, right->pLeft)) { + return 1; + } + + if (left->pRight && tSqlExprCompare(left->pRight, right->pRight)) { + return 1; + } + + return 0; +} + +SCreateTableSql *tSetCreateTableInfo(SArray *pCols, SArray *pTags, SSqlNode *pSelect, int32_t type) { + SCreateTableSql *pCreate = calloc(1, sizeof(SCreateTableSql)); + + switch (type) { + case TSQL_CREATE_TABLE: { + pCreate->colInfo.pColumns = pCols; + assert(pTags == NULL); + break; + } + case TSQL_CREATE_STABLE: { + pCreate->colInfo.pColumns = pCols; + pCreate->colInfo.pTagColumns = pTags; + assert(pTags != NULL && pCols != NULL); + break; + } + case TSQL_CREATE_STREAM: { + pCreate->pSelect = pSelect; + break; + } + + case TSQL_CREATE_CTABLE: { + assert(0); + } + + default: + assert(false); + } + + pCreate->type = type; + return pCreate; +} + +SAlterTableInfo *tSetAlterTableInfo(SToken *pTableName, SArray *pCols, SArray *pVals, int32_t type, int16_t tableType) { + SAlterTableInfo *pAlterTable = calloc(1, sizeof(SAlterTableInfo)); + + pAlterTable->name = *pTableName; + pAlterTable->type = type; + pAlterTable->tableType = tableType; + + if (type == TSDB_ALTER_TABLE_ADD_COLUMN || type == TSDB_ALTER_TABLE_ADD_TAG_COLUMN || type == TSDB_ALTER_TABLE_CHANGE_COLUMN || type == TSDB_ALTER_TABLE_MODIFY_TAG_COLUMN) { + pAlterTable->pAddColumns = pCols; + assert(pVals == NULL); + } else { + /* + * ALTER_TABLE_TAGS_CHG, ALTER_TABLE_TAGS_SET, ALTER_TABLE_TAGS_DROP, + * ALTER_TABLE_DROP_COLUMN + */ + pAlterTable->varList = pVals; + assert(pCols == NULL); + } + + return pAlterTable; +} +SCreatedTableInfo createNewChildTableInfo(SToken *pTableName, SArray *pTagNames, SArray *pTagVals, SToken *pToken, SToken* igExists) { + SCreatedTableInfo info; + memset(&info, 0, sizeof(SCreatedTableInfo)); + + info.name = *pToken; + info.pTagNames = pTagNames; + info.pTagVals = pTagVals; + info.stableName = *pTableName; + info.igExist = (igExists->n > 0)? 1:0; + + return info; +} + +void destroyAllSqlNode(SArray *pList) { + if (pList == NULL) { + return; + } + + size_t size = taosArrayGetSize(pList); + for(int32_t i = 0; i < size; ++i) { + SSqlNode *pNode = taosArrayGetP(pList, i); + destroySqlNode(pNode); + } + + taosArrayDestroy(pList); +} + +static void freeItem(void *pItem) { + SListItem* p = (SListItem*) pItem; + taosVariantDestroy(&p->pVar); +} + +void destroySqlNode(SSqlNode *pSqlNode) { + if (pSqlNode == NULL) { + return; + } + + tSqlExprListDestroy(pSqlNode->pSelNodeList); + pSqlNode->pSelNodeList = NULL; + + tSqlExprDestroy(pSqlNode->pWhere); + pSqlNode->pWhere = NULL; + + taosArrayDestroyEx(pSqlNode->pSortOrder, freeItem); + pSqlNode->pSortOrder = NULL; + + taosArrayDestroyEx(pSqlNode->pGroupby, freeItem); + pSqlNode->pGroupby = NULL; + + pSqlNode->from = destroyRelationInfo(pSqlNode->from); + + taosArrayDestroyEx(pSqlNode->fillType, freeItem); + pSqlNode->fillType = NULL; + + tSqlExprDestroy(pSqlNode->pHaving); + free(pSqlNode); +} + +void freeCreateTableInfo(void* p) { + SCreatedTableInfo* pInfo = (SCreatedTableInfo*) p; + taosArrayDestroy(pInfo->pTagNames); + taosArrayDestroyEx(pInfo->pTagVals, freeItem); + tfree(pInfo->fullname); + tfree(pInfo->tagdata.data); +} + +SSqlInfo* setSqlInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SToken *pTableName, int32_t type) { + pInfo->type = type; + + if (type == TSDB_SQL_SELECT) { + pInfo->list = (SArray*) pSqlExprInfo; + } else { + pInfo->pCreateTableInfo = pSqlExprInfo; + } + + if (pTableName != NULL) { + pInfo->pCreateTableInfo->name = *pTableName; + } + + return pInfo; +} +SArray* setSubclause(SArray* pList, void *pSqlNode) { + if (pList == NULL) { + pList = taosArrayInit(1, POINTER_BYTES); + } + + taosArrayPush(pList, &pSqlNode); + return pList; +} +SArray* appendSelectClause(SArray *pList, void *pSubclause) { + taosArrayPush(pList, &pSubclause); + return pList; +} + +void setCreatedTableName(SSqlInfo *pInfo, SToken *pTableNameToken, SToken *pIfNotExists) { + pInfo->pCreateTableInfo->name = *pTableNameToken; + pInfo->pCreateTableInfo->existCheck = (pIfNotExists->n != 0); +} + +void* destroyCreateTableSql(SCreateTableSql* pCreate) { + destroySqlNode(pCreate->pSelect); + + taosArrayDestroy(pCreate->colInfo.pColumns); + taosArrayDestroy(pCreate->colInfo.pTagColumns); + + taosArrayDestroyEx(pCreate->childTableInfo, freeCreateTableInfo); + tfree(pCreate); + + return NULL; +} + +void SqlInfoDestroy(SSqlInfo *pInfo) { + if (pInfo == NULL) return;; + taosArrayDestroy(pInfo->funcs); + if (pInfo->type == TSDB_SQL_SELECT) { + destroyAllSqlNode(pInfo->list); + } else if (pInfo->type == TSDB_SQL_CREATE_TABLE) { + pInfo->pCreateTableInfo = destroyCreateTableSql(pInfo->pCreateTableInfo); + } else if (pInfo->type == TSDB_SQL_ALTER_TABLE) { + taosArrayDestroyEx(pInfo->pAlterInfo->varList, freeItem); + taosArrayDestroy(pInfo->pAlterInfo->pAddColumns); + tfree(pInfo->pAlterInfo->tagData.data); + tfree(pInfo->pAlterInfo); + } else if (pInfo->type == TSDB_SQL_COMPACT_VNODE) { + tSqlExprListDestroy(pInfo->list); + } else { + if (pInfo->pMiscInfo != NULL) { + taosArrayDestroy(pInfo->pMiscInfo->a); + } + + if (pInfo->pMiscInfo != NULL && (pInfo->type == TSDB_SQL_CREATE_DB || pInfo->type == TSDB_SQL_ALTER_DB)) { + taosArrayDestroyEx(pInfo->pMiscInfo->dbOpt.keep, freeItem); + } + + tfree(pInfo->pMiscInfo); + } +} + +void setDCLSqlElems(SSqlInfo *pInfo, int32_t type, int32_t nParam, ...) { + pInfo->type = type; + if (nParam == 0) { + return; + } + + if (pInfo->pMiscInfo == NULL) { + pInfo->pMiscInfo = (SMiscInfo *)calloc(1, sizeof(SMiscInfo)); + pInfo->pMiscInfo->a = taosArrayInit(4, sizeof(SToken)); + } + + va_list va; + va_start(va, nParam); + + while ((nParam--) > 0) { + SToken *pToken = va_arg(va, SToken *); + taosArrayPush(pInfo->pMiscInfo->a, pToken); + } + + va_end(va); +} +void setDropDbTableInfo(SSqlInfo *pInfo, int32_t type, SToken* pToken, SToken* existsCheck, int16_t dbType, int16_t tableType) { + pInfo->type = type; + + if (pInfo->pMiscInfo == NULL) { + pInfo->pMiscInfo = (SMiscInfo *)calloc(1, sizeof(SMiscInfo)); + pInfo->pMiscInfo->a = taosArrayInit(4, sizeof(SToken)); + } + + taosArrayPush(pInfo->pMiscInfo->a, pToken); + + pInfo->pMiscInfo->existsCheck = (existsCheck->n == 1); + pInfo->pMiscInfo->dbType = dbType; + pInfo->pMiscInfo->tableType = tableType; +} +void setShowOptions(SSqlInfo *pInfo, int32_t type, SToken* prefix, SToken* pPatterns) { + if (pInfo->pMiscInfo == NULL) { + pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo)); + } + + pInfo->type = TSDB_SQL_SHOW; + + SShowInfo* pShowInfo = &pInfo->pMiscInfo->showOpt; + pShowInfo->showType = type; + + if (prefix != NULL && prefix->type != 0) { + pShowInfo->prefix = *prefix; + } else { + pShowInfo->prefix.type = 0; + } + + if (pPatterns != NULL && pPatterns->type != 0) { + pShowInfo->pattern = *pPatterns; + } else { + pShowInfo->pattern.type = 0; + } +} + +void setCreateDbInfo(SSqlInfo *pInfo, int32_t type, SToken *pToken, SCreateDbInfo *pDB, SToken *pIgExists) { + pInfo->type = type; + if (pInfo->pMiscInfo == NULL) { + pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo)); + } + + pInfo->pMiscInfo->dbOpt = *pDB; + pInfo->pMiscInfo->dbOpt.dbname = *pToken; + pInfo->pMiscInfo->dbOpt.ignoreExists = pIgExists->n; // sql.y has: ifnotexists(X) ::= IF NOT EXISTS. {X.n = 1;} +} + +void setCreateAcctSql(SSqlInfo *pInfo, int32_t type, SToken *pName, SToken *pPwd, SCreateAcctInfo *pAcctInfo) { + pInfo->type = type; + if (pInfo->pMiscInfo == NULL) { + pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo)); + } + + pInfo->pMiscInfo->acctOpt = *pAcctInfo; + + assert(pName != NULL); + pInfo->pMiscInfo->user.user = *pName; + + if (pPwd != NULL) { + pInfo->pMiscInfo->user.passwd = *pPwd; + } +} +void setCreateUserSql(SSqlInfo *pInfo, SToken *pName, SToken *pPasswd) { + pInfo->type = TSDB_SQL_CREATE_USER; + if (pInfo->pMiscInfo == NULL) { + pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo)); + } + + assert(pName != NULL && pPasswd != NULL); + + pInfo->pMiscInfo->user.user = *pName; + pInfo->pMiscInfo->user.passwd = *pPasswd; +} +void setKillSql(SSqlInfo *pInfo, int32_t type, SToken *id) { + pInfo->type = type; + if (pInfo->pMiscInfo == NULL) { + pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo)); + } + + assert(id != NULL); + pInfo->pMiscInfo->id = *id; +} + +void setAlterUserSql(SSqlInfo *pInfo, int16_t type, SToken *pName, SToken* pPwd, SToken *pPrivilege) { + pInfo->type = TSDB_SQL_ALTER_USER; + if (pInfo->pMiscInfo == NULL) { + pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo)); + } + + assert(pName != NULL); + + SUserInfo* pUser = &pInfo->pMiscInfo->user; + pUser->type = type; + pUser->user = *pName; + + if (pPwd != NULL) { + pUser->passwd = *pPwd; + } else { + pUser->passwd.type = TSDB_DATA_TYPE_NULL; + } + + if (pPrivilege != NULL) { + pUser->privilege = *pPrivilege; + } else { + pUser->privilege.type = TSDB_DATA_TYPE_NULL; + } +} + +void setCompactVnodeSql(SSqlInfo *pInfo, int32_t type, SArray *pParam) { + pInfo->type = type; + pInfo->list = pParam; +} + +void setDefaultCreateDbOption(SCreateDbInfo *pDBInfo) { + pDBInfo->compressionLevel = -1; + + pDBInfo->walLevel = -1; + pDBInfo->fsyncPeriod = -1; + pDBInfo->commitTime = -1; + pDBInfo->maxTablesPerVnode = -1; + + pDBInfo->cacheBlockSize = -1; + pDBInfo->numOfBlocks = -1; + pDBInfo->maxRowsPerBlock = -1; + pDBInfo->minRowsPerBlock = -1; + pDBInfo->daysPerFile = -1; + + pDBInfo->replica = -1; + pDBInfo->quorum = -1; + pDBInfo->keep = NULL; + + pDBInfo->update = -1; + pDBInfo->cachelast = -1; + + pDBInfo->dbType = -1; + pDBInfo->partitions = -1; + + memset(&pDBInfo->precision, 0, sizeof(SToken)); +} +void setDefaultCreateTopicOption(SCreateDbInfo *pDBInfo); + +// prefix show db.tables; +void tSetDbName(SToken *pCpxName, SToken *pDb) { + pCpxName->type = pDb->type; + pCpxName->z = pDb->z; + pCpxName->n = pDb->n; +} + +void tSetColumnInfo(SField *pField, SToken *pName, SField *pType) { + int32_t maxLen = sizeof(pField->name) / sizeof(pField->name[0]); + + // column name is too long, set the it to be invalid. + if ((int32_t) pName->n >= maxLen) { + pName->n = -1; + } else { + strncpy(pField->name, pName->z, pName->n); + pField->name[pName->n] = 0; + } + + pField->type = pType->type; + if(!isValidDataType(pField->type)){ + pField->bytes = 0; + } else { + pField->bytes = pType->bytes; + } +} + +static int32_t tryParseNameTwoParts(SToken *type) { + int32_t t = -1; + + char* str = strndup(type->z, type->n); + if (str == NULL) { + return t; + } + + char* p = strtok(str, " "); + if (p == NULL) { + tfree(str); + return t; + } else { + char* unsign = strtok(NULL, " "); + if (unsign == NULL) { + tfree(str); + return t; + } + + if (strncasecmp(unsign, "UNSIGNED", 8) == 0) { + for(int32_t j = TSDB_DATA_TYPE_TINYINT; j <= TSDB_DATA_TYPE_BIGINT; ++j) { + if (strcasecmp(p, tDataTypes[j].name) == 0) { + t = j; + break; + } + } + + tfree(str); + + if (t == -1) { + return -1; + } + + switch(t) { + case TSDB_DATA_TYPE_TINYINT: return TSDB_DATA_TYPE_UTINYINT; + case TSDB_DATA_TYPE_SMALLINT: return TSDB_DATA_TYPE_USMALLINT; + case TSDB_DATA_TYPE_INT: return TSDB_DATA_TYPE_UINT; + case TSDB_DATA_TYPE_BIGINT: return TSDB_DATA_TYPE_UBIGINT; + default: + return -1; + } + + } else { + tfree(str); + return -1; + } + } +} + +void tSetColumnType(SField *pField, SToken *type) { + // set the field type invalid + pField->type = -1; + pField->name[0] = 0; + + int32_t i = 0; + while (i < tListLen(tDataTypes)) { + if ((type->n == tDataTypes[i].nameLen) && + (strncasecmp(type->z, tDataTypes[i].name, tDataTypes[i].nameLen) == 0)) { + break; + } + + i += 1; + } + + // no qualified data type found, try unsigned data type + if (i == tListLen(tDataTypes)) { + i = tryParseNameTwoParts(type); + if (i == -1) { + return; + } + } + + pField->type = i; + pField->bytes = tDataTypes[i].bytes; + + if (i == TSDB_DATA_TYPE_NCHAR) { + /* + * for nchar, the TOKENTYPE is the number of character, so the length is the + * number of bytes in UCS-4 format, which is 4 times larger than the number of characters + */ + if (type->type == 0) { + pField->bytes = 0; + } else { + int32_t bytes = -(int32_t)(type->type); + if (bytes > (TSDB_MAX_NCHAR_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) { + // overflowed. set bytes to -1 so that error can be reported + bytes = -1; + } else { + bytes = bytes * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE; + } + pField->bytes = (int16_t)bytes; + } + } else if (i == TSDB_DATA_TYPE_BINARY) { + /* for binary, the TOKENTYPE is the length of binary */ + if (type->type == 0) { + pField->bytes = 0; + } else { + int32_t bytes = -(int32_t)(type->type); + if (bytes > TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE) { + // overflowed. set bytes to -1 so that error can be reported + bytes = -1; + } else { + bytes += VARSTR_HEADER_SIZE; + } + + pField->bytes = (int16_t)bytes; + } + } +} + +SSqlInfo doGenerateAST(const char *pStr) { + void *pParser = ParseAlloc(malloc); + + SSqlInfo sqlInfo = {0}; + sqlInfo.valid = true; + sqlInfo.funcs = taosArrayInit(4, sizeof(SToken)); + + int32_t i = 0; + while (1) { + SToken t0 = {0}; + + if (pStr[i] == 0) { + Parse(pParser, 0, t0, &sqlInfo); + goto abort_parse; + } + + t0.n = tGetToken((char *)&pStr[i], &t0.type); + t0.z = (char *)(pStr + i); + i += t0.n; + + switch (t0.type) { + case TK_SPACE: + case TK_COMMENT: { + break; + } + case TK_SEMI: { + Parse(pParser, 0, t0, &sqlInfo); + goto abort_parse; + } + + case TK_QUESTION: + case TK_ILLEGAL: { + snprintf(sqlInfo.msg, tListLen(sqlInfo.msg), "unrecognized token: \"%s\"", t0.z); + sqlInfo.valid = false; + goto abort_parse; + } + + case TK_HEX: + case TK_OCT: + case TK_BIN: { + snprintf(sqlInfo.msg, tListLen(sqlInfo.msg), "unsupported token: \"%s\"", t0.z); + sqlInfo.valid = false; + goto abort_parse; + } + + default: + Parse(pParser, t0.type, t0, &sqlInfo); + if (sqlInfo.valid == false) { + goto abort_parse; + } + } + } + +abort_parse: + ParseFree(pParser, free); + return sqlInfo; +} diff --git a/src/dnode/source/dnode.c b/source/libs/parser/src/astValidate.c similarity index 71% rename from src/dnode/source/dnode.c rename to source/libs/parser/src/astValidate.c index 75449ecd1a1c3b5589e801d52e1ed2928114e8ac..7718dc07588f7e5769290bc2fb38a7e952110868 100644 --- a/src/dnode/source/dnode.c +++ b/source/libs/parser/src/astValidate.c @@ -12,9 +12,15 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#include "os.h" -int main(int argc, char const *argv[]) { - printf("Hello world!\n"); + +#include "parserInt.h" + +int32_t qParserValidateSqlNode(struct SCatalog* pCatalog, SSqlInfo* pInfo, SQueryStmtInfo* pQueryInfo, int64_t id, char* msg) { + //1. if it is a query, get the meta info and continue. + + + +// qParserExtractRequestedMetaInfo(pInfo->) return 0; } diff --git a/source/libs/parser/src/parser.c b/source/libs/parser/src/parser.c index 6dea4a4e57392be988126c579648f39a8270b9bf..45215d7866eb312334dc44661a1b3d23a691e2d9 100644 --- a/source/libs/parser/src/parser.c +++ b/source/libs/parser/src/parser.c @@ -11,4 +11,45 @@ * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . - */ \ No newline at end of file + */ + +#include "parserInt.h" +#include "ttoken.h" +#include "astGenerator.h" + +bool qIsInsertSql(const char* pStr, size_t length) { + return false; +} + +int32_t qParseQuerySql(const char* pStr, size_t length, struct SQueryStmtInfo** pQueryInfo, int64_t id, char* msg, int32_t msgLen) { + *pQueryInfo = calloc(1, sizeof(SQueryStmtInfo)); + if (*pQueryInfo == NULL) { + return -1; // set correct error code. + } + + SSqlInfo info = doGenerateAST(pStr); + if (!info.valid) { + strcpy(msg, info.msg); + return -1; // set correct error code. + } + + struct SCatalog* pCatalog = getCatalogHandle(NULL); + int32_t code = qParserValidateSqlNode(pCatalog, &info, *pQueryInfo, id, msg); + if (code != 0) { + return code; + } + + return 0; +} + +int32_t qParseInsertSql(const char* pStr, size_t length, struct SInsertStmtInfo** pInsertInfo, int64_t id, char* msg, int32_t msgLen) { + return 0; +} + +int32_t qParserConvertSql(const char* pStr, size_t length, char** pConvertSql) { + return 0; +} + +int32_t qParserExtractRequestedMetaInfo(const struct SSqlNode* pSqlNode, SMetaReq* pMetaInfo) { + return 0; +} \ No newline at end of file diff --git a/source/libs/parser/src/sql.c b/source/libs/parser/src/sql.c new file mode 100644 index 0000000000000000000000000000000000000000..11f1f7ad04c73fc1d76e5b7e58287a8e41e86321 --- /dev/null +++ b/source/libs/parser/src/sql.c @@ -0,0 +1,3609 @@ +/* +** 2000-05-29 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** Driver template for the LEMON parser generator. +** +** The "lemon" program processes an LALR(1) input grammar file, then uses +** this template to construct a parser. The "lemon" program inserts text +** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the +** interstitial "-" characters) contained in this template is changed into +** the value of the %name directive from the grammar. Otherwise, the content +** of this template is copied straight through into the generate parser +** source file. +** +** The following is the concatenation of all %include directives from the +** input grammar file: +*/ +#include +/************ Begin %include sections from the grammar ************************/ + +#include +#include +#include +#include +#include +#include "astGenerator.h" +#include "tmsgtype.h" +#include "ttoken.h" +#include "ttokendef.h" +//#include "tutil.h" +#include "tvariant.h" +/**************** End of %include directives **********************************/ +/* These constants specify the various numeric values for terminal symbols +** in a format understandable to "makeheaders". This section is blank unless +** "lemon" is run with the "-m" command-line option. +***************** Begin makeheaders token definitions *************************/ +/**************** End makeheaders token definitions ***************************/ + +/* The next sections is a series of control #defines. +** various aspects of the generated parser. +** YYCODETYPE is the data type used to store the integer codes +** that represent terminal and non-terminal symbols. +** "unsigned char" is used if there are fewer than +** 256 symbols. Larger types otherwise. +** YYNOCODE is a number of type YYCODETYPE that is not used for +** any terminal or nonterminal symbol. +** YYFALLBACK If defined, this indicates that one or more tokens +** (also known as: "terminal symbols") have fall-back +** values which should be used if the original symbol +** would not parse. This permits keywords to sometimes +** be used as identifiers, for example. +** YYACTIONTYPE is the data type used for "action codes" - numbers +** that indicate what to do in response to the next +** token. +** ParseTOKENTYPE is the data type used for minor type for terminal +** symbols. Background: A "minor type" is a semantic +** value associated with a terminal or non-terminal +** symbols. For example, for an "ID" terminal symbol, +** the minor type might be the name of the identifier. +** Each non-terminal can have a different minor type. +** Terminal symbols all have the same minor type, though. +** This macros defines the minor type for terminal +** symbols. +** YYMINORTYPE is the data type used for all minor types. +** This is typically a union of many types, one of +** which is ParseTOKENTYPE. The entry in the union +** for terminal symbols is called "yy0". +** YYSTACKDEPTH is the maximum depth of the parser's stack. If +** zero the stack is dynamically sized using realloc() +** ParseARG_SDECL A static variable declaration for the %extra_argument +** ParseARG_PDECL A parameter declaration for the %extra_argument +** ParseARG_STORE Code to store %extra_argument into yypParser +** ParseARG_FETCH Code to extract %extra_argument from yypParser +** YYERRORSYMBOL is the code number of the error symbol. If not +** defined, then do no error processing. +** YYNSTATE the combined number of states. +** YYNRULE the number of rules in the grammar +** YYNTOKEN Number of terminal symbols +** YY_MAX_SHIFT Maximum value for shift actions +** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions +** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions +** YY_ERROR_ACTION The yy_action[] code for syntax error +** YY_ACCEPT_ACTION The yy_action[] code for accept +** YY_NO_ACTION The yy_action[] code for no-op +** YY_MIN_REDUCE Minimum value for reduce actions +** YY_MAX_REDUCE Maximum value for reduce actions +*/ +#ifndef INTERFACE +# define INTERFACE 1 +#endif +/************* Begin control #defines *****************************************/ +#define YYCODETYPE unsigned short int +#define YYNOCODE 280 +#define YYACTIONTYPE unsigned short int +#define ParseTOKENTYPE SToken +typedef union { + int yyinit; + ParseTOKENTYPE yy0; + SCreatedTableInfo yy78; + SCreateTableSql* yy110; + int yy130; + SArray* yy135; + SIntervalVal yy160; + TAOS_FIELD yy181; + SVariant yy191; + SLimit yy247; + SCreateDbInfo yy256; + SWindowStateVal yy258; + int32_t yy262; + SCreateAcctInfo yy277; + SRelationInfo* yy460; + SSqlNode* yy488; + SSessionWindowVal yy511; + tSqlExpr* yy526; + int64_t yy531; +} YYMINORTYPE; +#ifndef YYSTACKDEPTH +#define YYSTACKDEPTH 100 +#endif +#define ParseARG_SDECL SSqlInfo* pInfo; +#define ParseARG_PDECL ,SSqlInfo* pInfo +#define ParseARG_FETCH SSqlInfo* pInfo = yypParser->pInfo +#define ParseARG_STORE yypParser->pInfo = pInfo +#define YYFALLBACK 1 +#define YYNSTATE 368 +#define YYNRULE 294 +#define YYNTOKEN 197 +#define YY_MAX_SHIFT 367 +#define YY_MIN_SHIFTREDUCE 576 +#define YY_MAX_SHIFTREDUCE 869 +#define YY_ERROR_ACTION 870 +#define YY_ACCEPT_ACTION 871 +#define YY_NO_ACTION 872 +#define YY_MIN_REDUCE 873 +#define YY_MAX_REDUCE 1166 +/************* End control #defines *******************************************/ + +/* Define the yytestcase() macro to be a no-op if is not already defined +** otherwise. +** +** Applications can choose to define yytestcase() in the %include section +** to a macro that can assist in verifying code coverage. For production +** code the yytestcase() macro should be turned off. But it is useful +** for testing. +*/ +#ifndef yytestcase +# define yytestcase(X) +#endif + + +/* Next are the tables used to determine what action to take based on the +** current state and lookahead token. These tables are used to implement +** functions that take a state number and lookahead value and return an +** action integer. +** +** Suppose the action integer is N. Then the action is determined as +** follows +** +** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead +** token onto the stack and goto state N. +** +** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then +** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE. +** +** N == YY_ERROR_ACTION A syntax error has occurred. +** +** N == YY_ACCEPT_ACTION The parser accepts its input. +** +** N == YY_NO_ACTION No such action. Denotes unused +** slots in the yy_action[] table. +** +** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE +** and YY_MAX_REDUCE +** +** The action table is constructed as a single large table named yy_action[]. +** Given state S and lookahead X, the action is computed as either: +** +** (A) N = yy_action[ yy_shift_ofst[S] + X ] +** (B) N = yy_default[S] +** +** The (A) formula is preferred. The B formula is used instead if +** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X. +** +** The formulas above are for computing the action when the lookahead is +** a terminal symbol. If the lookahead is a non-terminal (as occurs after +** a reduce action) then the yy_reduce_ofst[] array is used in place of +** the yy_shift_ofst[] array. +** +** The following are the tables generated in this section: +** +** yy_action[] A single table containing all actions. +** yy_lookahead[] A table containing the lookahead for each entry in +** yy_action. Used to detect hash collisions. +** yy_shift_ofst[] For each state, the offset into yy_action for +** shifting terminals. +** yy_reduce_ofst[] For each state, the offset into yy_action for +** shifting non-terminals after a reduce. +** yy_default[] Default action for each state. +** +*********** Begin parsing tables **********************************************/ +#define YY_ACTTAB_COUNT (773) +static const YYACTIONTYPE yy_action[] = { + /* 0 */ 23, 628, 366, 235, 1051, 208, 241, 712, 211, 629, + /* 10 */ 1029, 871, 367, 59, 60, 173, 63, 64, 1042, 1142, + /* 20 */ 255, 53, 52, 51, 628, 62, 324, 67, 65, 68, + /* 30 */ 66, 157, 629, 286, 238, 58, 57, 344, 343, 56, + /* 40 */ 55, 54, 59, 60, 247, 63, 64, 252, 1029, 255, + /* 50 */ 53, 52, 51, 664, 62, 324, 67, 65, 68, 66, + /* 60 */ 999, 1042, 997, 998, 58, 57, 752, 1000, 56, 55, + /* 70 */ 54, 1001, 1048, 1002, 1003, 58, 57, 277, 1015, 56, + /* 80 */ 55, 54, 59, 60, 164, 63, 64, 38, 82, 255, + /* 90 */ 53, 52, 51, 88, 62, 324, 67, 65, 68, 66, + /* 100 */ 284, 283, 249, 322, 58, 57, 1029, 211, 56, 55, + /* 110 */ 54, 38, 59, 61, 806, 63, 64, 1042, 1143, 255, + /* 120 */ 53, 52, 51, 628, 62, 324, 67, 65, 68, 66, + /* 130 */ 45, 629, 237, 239, 58, 57, 1026, 164, 56, 55, + /* 140 */ 54, 60, 1023, 63, 64, 771, 772, 255, 53, 52, + /* 150 */ 51, 756, 62, 324, 67, 65, 68, 66, 164, 1090, + /* 160 */ 1025, 296, 58, 57, 322, 100, 56, 55, 54, 577, + /* 170 */ 578, 579, 580, 581, 582, 583, 584, 585, 586, 587, + /* 180 */ 588, 589, 590, 155, 354, 236, 63, 64, 209, 248, + /* 190 */ 255, 53, 52, 51, 628, 62, 324, 67, 65, 68, + /* 200 */ 66, 1017, 629, 164, 1028, 58, 57, 362, 960, 56, + /* 210 */ 55, 54, 1089, 44, 320, 361, 360, 319, 318, 317, + /* 220 */ 359, 316, 315, 314, 358, 313, 357, 356, 154, 152, + /* 230 */ 151, 298, 24, 93, 991, 979, 980, 981, 982, 983, + /* 240 */ 984, 985, 986, 987, 988, 989, 990, 992, 993, 214, + /* 250 */ 749, 254, 821, 922, 125, 810, 222, 813, 215, 816, + /* 260 */ 192, 97, 139, 138, 137, 221, 354, 254, 821, 329, + /* 270 */ 88, 810, 251, 813, 269, 816, 9, 29, 94, 67, + /* 280 */ 65, 68, 66, 273, 272, 233, 234, 58, 57, 325, + /* 290 */ 81, 56, 55, 54, 1012, 1013, 35, 1016, 812, 216, + /* 300 */ 815, 233, 234, 258, 5, 41, 182, 45, 56, 55, + /* 310 */ 54, 181, 106, 111, 102, 110, 38, 263, 736, 38, + /* 320 */ 932, 733, 217, 734, 1014, 735, 95, 192, 256, 276, + /* 330 */ 309, 80, 211, 38, 69, 123, 117, 128, 229, 811, + /* 340 */ 83, 814, 127, 1143, 133, 136, 126, 202, 200, 198, + /* 350 */ 69, 260, 261, 130, 197, 143, 142, 141, 140, 38, + /* 360 */ 44, 245, 361, 360, 246, 1026, 788, 359, 1026, 822, + /* 370 */ 817, 358, 38, 357, 356, 38, 818, 38, 333, 259, + /* 380 */ 38, 257, 1026, 332, 331, 822, 817, 1137, 211, 365, + /* 390 */ 364, 148, 818, 265, 38, 262, 38, 339, 338, 1143, + /* 400 */ 264, 264, 264, 14, 334, 85, 76, 96, 1026, 923, + /* 410 */ 86, 178, 179, 1027, 3, 193, 192, 335, 278, 819, + /* 420 */ 336, 1026, 340, 787, 1026, 341, 1026, 1, 180, 1026, + /* 430 */ 768, 73, 39, 326, 778, 737, 738, 99, 779, 342, + /* 440 */ 722, 346, 280, 1026, 159, 1026, 77, 280, 70, 301, + /* 450 */ 724, 303, 26, 723, 39, 34, 808, 844, 823, 253, + /* 460 */ 820, 627, 1136, 39, 70, 98, 79, 70, 16, 1100, + /* 470 */ 15, 25, 25, 74, 116, 25, 115, 18, 741, 17, + /* 480 */ 742, 6, 739, 20, 740, 19, 1135, 122, 304, 121, + /* 490 */ 22, 231, 21, 232, 809, 135, 134, 212, 711, 213, + /* 500 */ 274, 218, 210, 219, 220, 224, 225, 1162, 226, 223, + /* 510 */ 207, 1154, 1099, 243, 1096, 1095, 244, 345, 156, 48, + /* 520 */ 1082, 1081, 174, 1050, 1061, 153, 1058, 1043, 1059, 281, + /* 530 */ 1024, 1063, 310, 285, 240, 158, 287, 825, 163, 292, + /* 540 */ 165, 175, 1022, 1040, 176, 167, 177, 937, 306, 767, + /* 550 */ 307, 308, 311, 166, 312, 289, 46, 205, 42, 323, + /* 560 */ 931, 330, 1161, 113, 78, 75, 299, 1160, 50, 1157, + /* 570 */ 183, 297, 168, 295, 337, 1153, 293, 119, 1152, 1149, + /* 580 */ 184, 957, 291, 43, 40, 47, 206, 919, 129, 917, + /* 590 */ 131, 132, 915, 914, 266, 195, 196, 288, 911, 910, + /* 600 */ 909, 908, 907, 906, 905, 199, 201, 902, 900, 898, + /* 610 */ 896, 203, 893, 204, 889, 355, 49, 124, 279, 84, + /* 620 */ 347, 89, 290, 1083, 348, 349, 350, 351, 352, 353, + /* 630 */ 230, 363, 250, 869, 267, 268, 305, 868, 270, 271, + /* 640 */ 867, 850, 227, 849, 280, 275, 228, 107, 936, 935, + /* 650 */ 10, 108, 300, 744, 282, 87, 30, 90, 769, 913, + /* 660 */ 912, 160, 904, 187, 191, 144, 958, 185, 186, 188, + /* 670 */ 189, 145, 146, 190, 903, 2, 780, 995, 147, 161, + /* 680 */ 169, 170, 959, 171, 172, 33, 895, 894, 774, 162, + /* 690 */ 4, 1005, 91, 242, 776, 92, 294, 31, 11, 32, + /* 700 */ 13, 12, 27, 302, 28, 99, 101, 642, 36, 103, + /* 710 */ 104, 37, 105, 677, 675, 674, 673, 671, 670, 669, + /* 720 */ 666, 321, 109, 632, 7, 826, 824, 8, 328, 327, + /* 730 */ 112, 114, 71, 72, 118, 714, 39, 120, 713, 710, + /* 740 */ 658, 656, 648, 654, 650, 652, 646, 644, 680, 679, + /* 750 */ 678, 676, 672, 668, 667, 194, 630, 594, 873, 872, + /* 760 */ 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, + /* 770 */ 872, 149, 150, +}; +static const YYCODETYPE yy_lookahead[] = { + /* 0 */ 267, 1, 200, 201, 200, 267, 246, 5, 267, 9, + /* 10 */ 250, 198, 199, 13, 14, 254, 16, 17, 248, 278, + /* 20 */ 20, 21, 22, 23, 1, 25, 26, 27, 28, 29, + /* 30 */ 30, 200, 9, 272, 264, 35, 36, 35, 36, 39, + /* 40 */ 40, 41, 13, 14, 246, 16, 17, 207, 250, 20, + /* 50 */ 21, 22, 23, 5, 25, 26, 27, 28, 29, 30, + /* 60 */ 224, 248, 226, 227, 35, 36, 39, 231, 39, 40, + /* 70 */ 41, 235, 268, 237, 238, 35, 36, 264, 0, 39, + /* 80 */ 40, 41, 13, 14, 200, 16, 17, 200, 88, 20, + /* 90 */ 21, 22, 23, 84, 25, 26, 27, 28, 29, 30, + /* 100 */ 269, 270, 246, 86, 35, 36, 250, 267, 39, 40, + /* 110 */ 41, 200, 13, 14, 85, 16, 17, 248, 278, 20, + /* 120 */ 21, 22, 23, 1, 25, 26, 27, 28, 29, 30, + /* 130 */ 121, 9, 245, 264, 35, 36, 249, 200, 39, 40, + /* 140 */ 41, 14, 200, 16, 17, 127, 128, 20, 21, 22, + /* 150 */ 23, 124, 25, 26, 27, 28, 29, 30, 200, 275, + /* 160 */ 249, 277, 35, 36, 86, 208, 39, 40, 41, 47, + /* 170 */ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, + /* 180 */ 58, 59, 60, 61, 92, 63, 16, 17, 267, 247, + /* 190 */ 20, 21, 22, 23, 1, 25, 26, 27, 28, 29, + /* 200 */ 30, 244, 9, 200, 250, 35, 36, 222, 223, 39, + /* 210 */ 40, 41, 275, 100, 101, 102, 103, 104, 105, 106, + /* 220 */ 107, 108, 109, 110, 111, 112, 113, 114, 64, 65, + /* 230 */ 66, 273, 46, 275, 224, 225, 226, 227, 228, 229, + /* 240 */ 230, 231, 232, 233, 234, 235, 236, 237, 238, 63, + /* 250 */ 99, 1, 2, 206, 80, 5, 70, 7, 267, 9, + /* 260 */ 213, 208, 76, 77, 78, 79, 92, 1, 2, 83, + /* 270 */ 84, 5, 207, 7, 144, 9, 125, 84, 275, 27, + /* 280 */ 28, 29, 30, 153, 154, 35, 36, 35, 36, 39, + /* 290 */ 208, 39, 40, 41, 241, 242, 243, 244, 5, 267, + /* 300 */ 7, 35, 36, 70, 64, 65, 66, 121, 39, 40, + /* 310 */ 41, 71, 72, 73, 74, 75, 200, 70, 2, 200, + /* 320 */ 206, 5, 267, 7, 242, 9, 251, 213, 207, 143, + /* 330 */ 90, 145, 267, 200, 84, 64, 65, 66, 152, 5, + /* 340 */ 265, 7, 71, 278, 73, 74, 75, 64, 65, 66, + /* 350 */ 84, 35, 36, 82, 71, 72, 73, 74, 75, 200, + /* 360 */ 100, 245, 102, 103, 245, 249, 78, 107, 249, 119, + /* 370 */ 120, 111, 200, 113, 114, 200, 126, 200, 245, 146, + /* 380 */ 200, 148, 249, 150, 151, 119, 120, 267, 267, 67, + /* 390 */ 68, 69, 126, 146, 200, 148, 200, 150, 151, 278, + /* 400 */ 200, 200, 200, 84, 245, 85, 99, 88, 249, 206, + /* 410 */ 85, 211, 211, 211, 204, 205, 213, 245, 85, 126, + /* 420 */ 245, 249, 245, 135, 249, 245, 249, 209, 210, 249, + /* 430 */ 85, 99, 99, 15, 85, 119, 120, 118, 85, 245, + /* 440 */ 85, 245, 122, 249, 99, 249, 139, 122, 99, 85, + /* 450 */ 85, 85, 99, 85, 99, 84, 1, 85, 85, 62, + /* 460 */ 126, 85, 267, 99, 99, 99, 84, 99, 147, 240, + /* 470 */ 149, 99, 99, 141, 147, 99, 149, 147, 5, 149, + /* 480 */ 7, 84, 5, 147, 7, 149, 267, 147, 117, 149, + /* 490 */ 147, 267, 149, 267, 39, 80, 81, 267, 116, 267, + /* 500 */ 200, 267, 267, 267, 267, 267, 267, 250, 267, 267, + /* 510 */ 267, 250, 240, 240, 240, 240, 240, 240, 200, 266, + /* 520 */ 276, 276, 252, 200, 200, 62, 200, 248, 200, 248, + /* 530 */ 248, 200, 91, 271, 271, 200, 271, 119, 200, 200, + /* 540 */ 262, 200, 200, 263, 200, 260, 200, 200, 200, 126, + /* 550 */ 200, 200, 200, 261, 200, 271, 200, 200, 200, 200, + /* 560 */ 200, 200, 200, 200, 138, 140, 133, 200, 137, 200, + /* 570 */ 200, 136, 259, 131, 200, 200, 130, 200, 200, 200, + /* 580 */ 200, 200, 129, 200, 200, 200, 200, 200, 200, 200, + /* 590 */ 200, 200, 200, 200, 200, 200, 200, 132, 200, 200, + /* 600 */ 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, + /* 610 */ 200, 200, 200, 200, 200, 115, 142, 98, 202, 202, + /* 620 */ 97, 202, 202, 202, 53, 94, 96, 57, 95, 93, + /* 630 */ 202, 86, 202, 5, 155, 5, 202, 5, 155, 5, + /* 640 */ 5, 102, 202, 101, 122, 144, 202, 208, 212, 212, + /* 650 */ 84, 208, 117, 85, 99, 123, 84, 99, 85, 202, + /* 660 */ 202, 84, 202, 215, 214, 203, 221, 220, 219, 218, + /* 670 */ 216, 203, 203, 217, 202, 209, 85, 239, 203, 84, + /* 680 */ 258, 257, 223, 256, 255, 253, 202, 202, 85, 99, + /* 690 */ 204, 239, 84, 1, 85, 84, 84, 99, 134, 99, + /* 700 */ 84, 134, 84, 117, 84, 118, 80, 5, 89, 88, + /* 710 */ 72, 89, 88, 9, 5, 5, 5, 5, 5, 5, + /* 720 */ 5, 15, 80, 87, 84, 119, 85, 84, 61, 26, + /* 730 */ 149, 149, 16, 16, 149, 5, 99, 149, 5, 85, + /* 740 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + /* 750 */ 5, 5, 5, 5, 5, 99, 87, 62, 0, 279, + /* 760 */ 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, + /* 770 */ 279, 21, 21, 279, 279, 279, 279, 279, 279, 279, + /* 780 */ 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, + /* 790 */ 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, + /* 800 */ 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, + /* 810 */ 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, + /* 820 */ 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, + /* 830 */ 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, + /* 840 */ 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, + /* 850 */ 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, + /* 860 */ 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, + /* 870 */ 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, + /* 880 */ 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, + /* 890 */ 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, + /* 900 */ 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, + /* 910 */ 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, + /* 920 */ 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, + /* 930 */ 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, + /* 940 */ 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, + /* 950 */ 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, + /* 960 */ 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, +}; +#define YY_SHIFT_COUNT (367) +#define YY_SHIFT_MIN (0) +#define YY_SHIFT_MAX (758) +static const unsigned short int yy_shift_ofst[] = { + /* 0 */ 186, 113, 113, 260, 260, 17, 250, 266, 266, 193, + /* 10 */ 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, + /* 20 */ 23, 23, 23, 0, 122, 266, 316, 316, 316, 9, + /* 30 */ 9, 23, 23, 18, 23, 78, 23, 23, 23, 23, + /* 40 */ 174, 17, 92, 92, 48, 773, 773, 773, 266, 266, + /* 50 */ 266, 266, 266, 266, 266, 266, 266, 266, 266, 266, + /* 60 */ 266, 266, 266, 266, 266, 266, 266, 266, 266, 266, + /* 70 */ 316, 316, 316, 2, 2, 2, 2, 2, 2, 2, + /* 80 */ 23, 23, 23, 27, 23, 23, 23, 9, 9, 23, + /* 90 */ 23, 23, 23, 288, 288, 151, 9, 23, 23, 23, + /* 100 */ 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, + /* 110 */ 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, + /* 120 */ 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, + /* 130 */ 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, + /* 140 */ 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, + /* 150 */ 23, 23, 23, 23, 23, 23, 463, 463, 463, 423, + /* 160 */ 423, 423, 423, 463, 463, 426, 425, 433, 431, 435, + /* 170 */ 442, 446, 453, 465, 474, 463, 463, 463, 441, 441, + /* 180 */ 500, 17, 17, 463, 463, 519, 523, 571, 531, 530, + /* 190 */ 570, 533, 536, 500, 48, 463, 463, 545, 545, 463, + /* 200 */ 545, 463, 545, 463, 463, 773, 773, 29, 69, 69, + /* 210 */ 99, 69, 127, 170, 240, 252, 252, 252, 252, 252, + /* 220 */ 252, 271, 283, 40, 40, 40, 40, 233, 247, 130, + /* 230 */ 319, 269, 269, 293, 334, 322, 164, 333, 320, 325, + /* 240 */ 345, 349, 353, 332, 307, 355, 364, 365, 366, 368, + /* 250 */ 371, 372, 373, 455, 397, 418, 376, 321, 327, 330, + /* 260 */ 473, 477, 336, 340, 382, 343, 415, 628, 479, 630, + /* 270 */ 632, 483, 634, 635, 539, 542, 501, 522, 535, 566, + /* 280 */ 532, 568, 572, 555, 558, 573, 577, 591, 595, 603, + /* 290 */ 590, 608, 609, 611, 692, 612, 598, 564, 600, 567, + /* 300 */ 616, 535, 618, 586, 620, 587, 626, 619, 621, 638, + /* 310 */ 702, 622, 624, 704, 709, 710, 711, 712, 713, 714, + /* 320 */ 715, 636, 706, 642, 640, 641, 606, 643, 703, 667, + /* 330 */ 716, 581, 582, 637, 637, 637, 637, 717, 585, 588, + /* 340 */ 637, 637, 637, 730, 733, 654, 637, 735, 736, 737, + /* 350 */ 738, 739, 740, 741, 742, 743, 744, 745, 746, 747, + /* 360 */ 748, 749, 656, 669, 750, 751, 695, 758, +}; +#define YY_REDUCE_COUNT (206) +#define YY_REDUCE_MIN (-267) +#define YY_REDUCE_MAX (486) +static const short yy_reduce_ofst[] = { + /* 0 */ -187, 10, 10, -164, -164, 53, -160, 65, 121, -169, + /* 10 */ -113, -116, -42, 116, 119, 133, 159, 172, 175, 177, + /* 20 */ 180, 194, 196, -196, -198, -259, -240, -202, -144, -230, + /* 30 */ -131, -63, 3, -239, -58, -43, 200, 201, 202, -89, + /* 40 */ 47, 82, 114, 203, -15, 75, 218, 210, -267, -262, + /* 50 */ -79, -9, 32, 55, 120, 195, 219, 224, 226, 230, + /* 60 */ 232, 234, 235, 236, 237, 238, 239, 241, 242, 243, + /* 70 */ -46, 257, 261, 229, 272, 273, 274, 275, 276, 277, + /* 80 */ 300, 318, 323, 253, 324, 326, 328, 279, 281, 331, + /* 90 */ 335, 338, 339, 244, 245, 270, 282, 341, 342, 344, + /* 100 */ 346, 347, 348, 350, 351, 352, 354, 356, 357, 358, + /* 110 */ 359, 360, 361, 362, 363, 367, 369, 370, 374, 375, + /* 120 */ 377, 378, 379, 380, 381, 383, 384, 385, 386, 387, + /* 130 */ 388, 389, 390, 391, 392, 393, 394, 395, 396, 398, + /* 140 */ 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, + /* 150 */ 409, 410, 411, 412, 413, 414, 416, 417, 419, 262, + /* 160 */ 263, 265, 284, 420, 421, 280, 278, 292, 285, 313, + /* 170 */ 422, 424, 427, 429, 432, 428, 430, 434, 436, 437, + /* 180 */ 438, 439, 443, 440, 444, 445, 447, 449, 448, 451, + /* 190 */ 454, 456, 450, 452, 459, 457, 458, 462, 468, 460, + /* 200 */ 469, 472, 475, 484, 485, 466, 486, +}; +static const YYACTIONTYPE yy_default[] = { + /* 0 */ 870, 994, 933, 1004, 920, 930, 1145, 1145, 1145, 870, + /* 10 */ 870, 870, 870, 870, 870, 870, 870, 870, 870, 870, + /* 20 */ 870, 870, 870, 1052, 890, 1145, 870, 870, 870, 870, + /* 30 */ 870, 870, 870, 1067, 870, 930, 870, 870, 870, 870, + /* 40 */ 940, 930, 940, 940, 870, 1047, 978, 996, 870, 870, + /* 50 */ 870, 870, 870, 870, 870, 870, 870, 870, 870, 870, + /* 60 */ 870, 870, 870, 870, 870, 870, 870, 870, 870, 870, + /* 70 */ 870, 870, 870, 870, 870, 870, 870, 870, 870, 870, + /* 80 */ 870, 870, 870, 1054, 1060, 1057, 870, 870, 870, 1062, + /* 90 */ 870, 870, 870, 1086, 1086, 1045, 870, 870, 870, 870, + /* 100 */ 870, 870, 870, 870, 870, 870, 870, 870, 870, 870, + /* 110 */ 870, 870, 870, 870, 870, 870, 870, 870, 870, 870, + /* 120 */ 870, 870, 870, 870, 870, 870, 870, 870, 870, 918, + /* 130 */ 870, 916, 870, 870, 870, 870, 870, 870, 870, 870, + /* 140 */ 870, 870, 870, 870, 870, 870, 870, 870, 901, 870, + /* 150 */ 870, 870, 870, 870, 870, 888, 892, 892, 892, 870, + /* 160 */ 870, 870, 870, 892, 892, 1093, 1097, 1079, 1091, 1087, + /* 170 */ 1074, 1072, 1070, 1078, 1101, 892, 892, 892, 938, 938, + /* 180 */ 934, 930, 930, 892, 892, 956, 954, 952, 944, 950, + /* 190 */ 946, 948, 942, 921, 870, 892, 892, 928, 928, 892, + /* 200 */ 928, 892, 928, 892, 892, 978, 996, 870, 1102, 1092, + /* 210 */ 870, 1144, 1132, 1131, 870, 1140, 1139, 1138, 1130, 1129, + /* 220 */ 1128, 870, 870, 1124, 1127, 1126, 1125, 870, 870, 870, + /* 230 */ 870, 1134, 1133, 870, 870, 870, 870, 870, 870, 870, + /* 240 */ 870, 870, 870, 1098, 1094, 870, 870, 870, 870, 870, + /* 250 */ 870, 870, 870, 870, 1104, 870, 870, 870, 870, 870, + /* 260 */ 870, 870, 870, 870, 1006, 870, 870, 870, 870, 870, + /* 270 */ 870, 870, 870, 870, 870, 870, 870, 1044, 870, 870, + /* 280 */ 870, 870, 870, 1056, 1055, 870, 870, 870, 870, 870, + /* 290 */ 870, 870, 870, 870, 870, 870, 1088, 870, 1080, 870, + /* 300 */ 870, 1018, 870, 870, 870, 870, 870, 870, 870, 870, + /* 310 */ 870, 870, 870, 870, 870, 870, 870, 870, 870, 870, + /* 320 */ 870, 870, 870, 870, 870, 870, 870, 870, 870, 870, + /* 330 */ 870, 870, 870, 1163, 1158, 1159, 1156, 870, 870, 870, + /* 340 */ 1155, 1150, 1151, 870, 870, 870, 1148, 870, 870, 870, + /* 350 */ 870, 870, 870, 870, 870, 870, 870, 870, 870, 870, + /* 360 */ 870, 870, 962, 870, 899, 897, 870, 870, +}; +/********** End of lemon-generated parsing tables *****************************/ + +/* The next table maps tokens (terminal symbols) into fallback tokens. +** If a construct like the following: +** +** %fallback ID X Y Z. +** +** appears in the grammar, then ID becomes a fallback token for X, Y, +** and Z. Whenever one of the tokens X, Y, or Z is input to the parser +** but it does not parse, the type of the token is changed to ID and +** the parse is retried before an error is thrown. +** +** This feature can be used, for example, to cause some keywords in a language +** to revert to identifiers if they keyword does not apply in the context where +** it appears. +*/ +#ifdef YYFALLBACK +static const YYCODETYPE yyFallback[] = { + 0, /* $ => nothing */ + 0, /* ID => nothing */ + 1, /* BOOL => ID */ + 1, /* TINYINT => ID */ + 1, /* SMALLINT => ID */ + 1, /* INTEGER => ID */ + 1, /* BIGINT => ID */ + 1, /* FLOAT => ID */ + 1, /* DOUBLE => ID */ + 1, /* STRING => ID */ + 1, /* TIMESTAMP => ID */ + 1, /* BINARY => ID */ + 1, /* NCHAR => ID */ + 0, /* OR => nothing */ + 0, /* AND => nothing */ + 0, /* NOT => nothing */ + 0, /* EQ => nothing */ + 0, /* NE => nothing */ + 0, /* ISNULL => nothing */ + 0, /* NOTNULL => nothing */ + 0, /* IS => nothing */ + 1, /* LIKE => ID */ + 1, /* MATCH => ID */ + 1, /* NMATCH => ID */ + 1, /* GLOB => ID */ + 0, /* BETWEEN => nothing */ + 0, /* IN => nothing */ + 0, /* GT => nothing */ + 0, /* GE => nothing */ + 0, /* LT => nothing */ + 0, /* LE => nothing */ + 0, /* BITAND => nothing */ + 0, /* BITOR => nothing */ + 0, /* LSHIFT => nothing */ + 0, /* RSHIFT => nothing */ + 0, /* PLUS => nothing */ + 0, /* MINUS => nothing */ + 0, /* DIVIDE => nothing */ + 0, /* TIMES => nothing */ + 0, /* STAR => nothing */ + 0, /* SLASH => nothing */ + 0, /* REM => nothing */ + 0, /* CONCAT => nothing */ + 0, /* UMINUS => nothing */ + 0, /* UPLUS => nothing */ + 0, /* BITNOT => nothing */ + 0, /* SHOW => nothing */ + 0, /* DATABASES => nothing */ + 0, /* TOPICS => nothing */ + 0, /* FUNCTIONS => nothing */ + 0, /* MNODES => nothing */ + 0, /* DNODES => nothing */ + 0, /* ACCOUNTS => nothing */ + 0, /* USERS => nothing */ + 0, /* MODULES => nothing */ + 0, /* QUERIES => nothing */ + 0, /* CONNECTIONS => nothing */ + 0, /* STREAMS => nothing */ + 0, /* VARIABLES => nothing */ + 0, /* SCORES => nothing */ + 0, /* GRANTS => nothing */ + 0, /* VNODES => nothing */ + 0, /* DOT => nothing */ + 0, /* CREATE => nothing */ + 0, /* TABLE => nothing */ + 1, /* STABLE => ID */ + 1, /* DATABASE => ID */ + 0, /* TABLES => nothing */ + 0, /* STABLES => nothing */ + 0, /* VGROUPS => nothing */ + 0, /* DROP => nothing */ + 0, /* TOPIC => nothing */ + 0, /* FUNCTION => nothing */ + 0, /* DNODE => nothing */ + 0, /* USER => nothing */ + 0, /* ACCOUNT => nothing */ + 0, /* USE => nothing */ + 0, /* DESCRIBE => nothing */ + 1, /* DESC => ID */ + 0, /* ALTER => nothing */ + 0, /* PASS => nothing */ + 0, /* PRIVILEGE => nothing */ + 0, /* LOCAL => nothing */ + 0, /* COMPACT => nothing */ + 0, /* LP => nothing */ + 0, /* RP => nothing */ + 0, /* IF => nothing */ + 0, /* EXISTS => nothing */ + 0, /* AS => nothing */ + 0, /* OUTPUTTYPE => nothing */ + 0, /* AGGREGATE => nothing */ + 0, /* BUFSIZE => nothing */ + 0, /* PPS => nothing */ + 0, /* TSERIES => nothing */ + 0, /* DBS => nothing */ + 0, /* STORAGE => nothing */ + 0, /* QTIME => nothing */ + 0, /* CONNS => nothing */ + 0, /* STATE => nothing */ + 0, /* COMMA => nothing */ + 0, /* KEEP => nothing */ + 0, /* CACHE => nothing */ + 0, /* REPLICA => nothing */ + 0, /* QUORUM => nothing */ + 0, /* DAYS => nothing */ + 0, /* MINROWS => nothing */ + 0, /* MAXROWS => nothing */ + 0, /* BLOCKS => nothing */ + 0, /* CTIME => nothing */ + 0, /* WAL => nothing */ + 0, /* FSYNC => nothing */ + 0, /* COMP => nothing */ + 0, /* PRECISION => nothing */ + 0, /* UPDATE => nothing */ + 0, /* CACHELAST => nothing */ + 0, /* PARTITIONS => nothing */ + 0, /* UNSIGNED => nothing */ + 0, /* TAGS => nothing */ + 0, /* USING => nothing */ + 1, /* NULL => ID */ + 1, /* NOW => ID */ + 0, /* SELECT => nothing */ + 0, /* UNION => nothing */ + 1, /* ALL => ID */ + 0, /* DISTINCT => nothing */ + 0, /* FROM => nothing */ + 0, /* VARIABLE => nothing */ + 0, /* INTERVAL => nothing */ + 0, /* EVERY => nothing */ + 0, /* SESSION => nothing */ + 0, /* STATE_WINDOW => nothing */ + 0, /* FILL => nothing */ + 0, /* SLIDING => nothing */ + 0, /* ORDER => nothing */ + 0, /* BY => nothing */ + 1, /* ASC => ID */ + 0, /* GROUP => nothing */ + 0, /* HAVING => nothing */ + 0, /* LIMIT => nothing */ + 1, /* OFFSET => ID */ + 0, /* SLIMIT => nothing */ + 0, /* SOFFSET => nothing */ + 0, /* WHERE => nothing */ + 0, /* RESET => nothing */ + 0, /* QUERY => nothing */ + 0, /* SYNCDB => nothing */ + 0, /* ADD => nothing */ + 0, /* COLUMN => nothing */ + 0, /* MODIFY => nothing */ + 0, /* TAG => nothing */ + 0, /* CHANGE => nothing */ + 0, /* SET => nothing */ + 0, /* KILL => nothing */ + 0, /* CONNECTION => nothing */ + 0, /* STREAM => nothing */ + 0, /* COLON => nothing */ + 1, /* ABORT => ID */ + 1, /* AFTER => ID */ + 1, /* ATTACH => ID */ + 1, /* BEFORE => ID */ + 1, /* BEGIN => ID */ + 1, /* CASCADE => ID */ + 1, /* CLUSTER => ID */ + 1, /* CONFLICT => ID */ + 1, /* COPY => ID */ + 1, /* DEFERRED => ID */ + 1, /* DELIMITERS => ID */ + 1, /* DETACH => ID */ + 1, /* EACH => ID */ + 1, /* END => ID */ + 1, /* EXPLAIN => ID */ + 1, /* FAIL => ID */ + 1, /* FOR => ID */ + 1, /* IGNORE => ID */ + 1, /* IMMEDIATE => ID */ + 1, /* INITIALLY => ID */ + 1, /* INSTEAD => ID */ + 1, /* KEY => ID */ + 1, /* OF => ID */ + 1, /* RAISE => ID */ + 1, /* REPLACE => ID */ + 1, /* RESTRICT => ID */ + 1, /* ROW => ID */ + 1, /* STATEMENT => ID */ + 1, /* TRIGGER => ID */ + 1, /* VIEW => ID */ + 1, /* IPTOKEN => ID */ + 1, /* SEMI => ID */ + 1, /* NONE => ID */ + 1, /* PREV => ID */ + 1, /* LINEAR => ID */ + 1, /* IMPORT => ID */ + 1, /* TBNAME => ID */ + 1, /* JOIN => ID */ + 1, /* INSERT => ID */ + 1, /* INTO => ID */ + 1, /* VALUES => ID */ +}; +#endif /* YYFALLBACK */ + +/* The following structure represents a single element of the +** parser's stack. Information stored includes: +** +** + The state number for the parser at this level of the stack. +** +** + The value of the token stored at this level of the stack. +** (In other words, the "major" token.) +** +** + The semantic value stored at this level of the stack. This is +** the information used by the action routines in the grammar. +** It is sometimes called the "minor" token. +** +** After the "shift" half of a SHIFTREDUCE action, the stateno field +** actually contains the reduce action for the second half of the +** SHIFTREDUCE. +*/ +struct yyStackEntry { + YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */ + YYCODETYPE major; /* The major token value. This is the code + ** number for the token at this stack level */ + YYMINORTYPE minor; /* The user-supplied minor token value. This + ** is the value of the token */ +}; +typedef struct yyStackEntry yyStackEntry; + +/* The state of the parser is completely contained in an instance of +** the following structure */ +struct yyParser { + yyStackEntry *yytos; /* Pointer to top element of the stack */ +#ifdef YYTRACKMAXSTACKDEPTH + int yyhwm; /* High-water mark of the stack */ +#endif +#ifndef YYNOERRORRECOVERY + int yyerrcnt; /* Shifts left before out of the error */ +#endif + ParseARG_SDECL /* A place to hold %extra_argument */ +#if YYSTACKDEPTH<=0 + int yystksz; /* Current side of the stack */ + yyStackEntry *yystack; /* The parser's stack */ + yyStackEntry yystk0; /* First stack entry */ +#else + yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */ + yyStackEntry *yystackEnd; /* Last entry in the stack */ +#endif +}; +typedef struct yyParser yyParser; + +#ifndef NDEBUG +#include +static FILE *yyTraceFILE = 0; +static char *yyTracePrompt = 0; +#endif /* NDEBUG */ + +#ifndef NDEBUG +/* +** Turn parser tracing on by giving a stream to which to write the trace +** and a prompt to preface each trace message. Tracing is turned off +** by making either argument NULL +** +** Inputs: +**
    +**
  • A FILE* to which trace output should be written. +** If NULL, then tracing is turned off. +**
  • A prefix string written at the beginning of every +** line of trace output. If NULL, then tracing is +** turned off. +**
+** +** Outputs: +** None. +*/ +void ParseTrace(FILE *TraceFILE, char *zTracePrompt){ + yyTraceFILE = TraceFILE; + yyTracePrompt = zTracePrompt; + if( yyTraceFILE==0 ) yyTracePrompt = 0; + else if( yyTracePrompt==0 ) yyTraceFILE = 0; +} +#endif /* NDEBUG */ + +#if defined(YYCOVERAGE) || !defined(NDEBUG) +/* For tracing shifts, the names of all terminals and nonterminals +** are required. The following table supplies these names */ +static const char *const yyTokenName[] = { + /* 0 */ "$", + /* 1 */ "ID", + /* 2 */ "BOOL", + /* 3 */ "TINYINT", + /* 4 */ "SMALLINT", + /* 5 */ "INTEGER", + /* 6 */ "BIGINT", + /* 7 */ "FLOAT", + /* 8 */ "DOUBLE", + /* 9 */ "STRING", + /* 10 */ "TIMESTAMP", + /* 11 */ "BINARY", + /* 12 */ "NCHAR", + /* 13 */ "OR", + /* 14 */ "AND", + /* 15 */ "NOT", + /* 16 */ "EQ", + /* 17 */ "NE", + /* 18 */ "ISNULL", + /* 19 */ "NOTNULL", + /* 20 */ "IS", + /* 21 */ "LIKE", + /* 22 */ "MATCH", + /* 23 */ "NMATCH", + /* 24 */ "GLOB", + /* 25 */ "BETWEEN", + /* 26 */ "IN", + /* 27 */ "GT", + /* 28 */ "GE", + /* 29 */ "LT", + /* 30 */ "LE", + /* 31 */ "BITAND", + /* 32 */ "BITOR", + /* 33 */ "LSHIFT", + /* 34 */ "RSHIFT", + /* 35 */ "PLUS", + /* 36 */ "MINUS", + /* 37 */ "DIVIDE", + /* 38 */ "TIMES", + /* 39 */ "STAR", + /* 40 */ "SLASH", + /* 41 */ "REM", + /* 42 */ "CONCAT", + /* 43 */ "UMINUS", + /* 44 */ "UPLUS", + /* 45 */ "BITNOT", + /* 46 */ "SHOW", + /* 47 */ "DATABASES", + /* 48 */ "TOPICS", + /* 49 */ "FUNCTIONS", + /* 50 */ "MNODES", + /* 51 */ "DNODES", + /* 52 */ "ACCOUNTS", + /* 53 */ "USERS", + /* 54 */ "MODULES", + /* 55 */ "QUERIES", + /* 56 */ "CONNECTIONS", + /* 57 */ "STREAMS", + /* 58 */ "VARIABLES", + /* 59 */ "SCORES", + /* 60 */ "GRANTS", + /* 61 */ "VNODES", + /* 62 */ "DOT", + /* 63 */ "CREATE", + /* 64 */ "TABLE", + /* 65 */ "STABLE", + /* 66 */ "DATABASE", + /* 67 */ "TABLES", + /* 68 */ "STABLES", + /* 69 */ "VGROUPS", + /* 70 */ "DROP", + /* 71 */ "TOPIC", + /* 72 */ "FUNCTION", + /* 73 */ "DNODE", + /* 74 */ "USER", + /* 75 */ "ACCOUNT", + /* 76 */ "USE", + /* 77 */ "DESCRIBE", + /* 78 */ "DESC", + /* 79 */ "ALTER", + /* 80 */ "PASS", + /* 81 */ "PRIVILEGE", + /* 82 */ "LOCAL", + /* 83 */ "COMPACT", + /* 84 */ "LP", + /* 85 */ "RP", + /* 86 */ "IF", + /* 87 */ "EXISTS", + /* 88 */ "AS", + /* 89 */ "OUTPUTTYPE", + /* 90 */ "AGGREGATE", + /* 91 */ "BUFSIZE", + /* 92 */ "PPS", + /* 93 */ "TSERIES", + /* 94 */ "DBS", + /* 95 */ "STORAGE", + /* 96 */ "QTIME", + /* 97 */ "CONNS", + /* 98 */ "STATE", + /* 99 */ "COMMA", + /* 100 */ "KEEP", + /* 101 */ "CACHE", + /* 102 */ "REPLICA", + /* 103 */ "QUORUM", + /* 104 */ "DAYS", + /* 105 */ "MINROWS", + /* 106 */ "MAXROWS", + /* 107 */ "BLOCKS", + /* 108 */ "CTIME", + /* 109 */ "WAL", + /* 110 */ "FSYNC", + /* 111 */ "COMP", + /* 112 */ "PRECISION", + /* 113 */ "UPDATE", + /* 114 */ "CACHELAST", + /* 115 */ "PARTITIONS", + /* 116 */ "UNSIGNED", + /* 117 */ "TAGS", + /* 118 */ "USING", + /* 119 */ "NULL", + /* 120 */ "NOW", + /* 121 */ "SELECT", + /* 122 */ "UNION", + /* 123 */ "ALL", + /* 124 */ "DISTINCT", + /* 125 */ "FROM", + /* 126 */ "VARIABLE", + /* 127 */ "INTERVAL", + /* 128 */ "EVERY", + /* 129 */ "SESSION", + /* 130 */ "STATE_WINDOW", + /* 131 */ "FILL", + /* 132 */ "SLIDING", + /* 133 */ "ORDER", + /* 134 */ "BY", + /* 135 */ "ASC", + /* 136 */ "GROUP", + /* 137 */ "HAVING", + /* 138 */ "LIMIT", + /* 139 */ "OFFSET", + /* 140 */ "SLIMIT", + /* 141 */ "SOFFSET", + /* 142 */ "WHERE", + /* 143 */ "RESET", + /* 144 */ "QUERY", + /* 145 */ "SYNCDB", + /* 146 */ "ADD", + /* 147 */ "COLUMN", + /* 148 */ "MODIFY", + /* 149 */ "TAG", + /* 150 */ "CHANGE", + /* 151 */ "SET", + /* 152 */ "KILL", + /* 153 */ "CONNECTION", + /* 154 */ "STREAM", + /* 155 */ "COLON", + /* 156 */ "ABORT", + /* 157 */ "AFTER", + /* 158 */ "ATTACH", + /* 159 */ "BEFORE", + /* 160 */ "BEGIN", + /* 161 */ "CASCADE", + /* 162 */ "CLUSTER", + /* 163 */ "CONFLICT", + /* 164 */ "COPY", + /* 165 */ "DEFERRED", + /* 166 */ "DELIMITERS", + /* 167 */ "DETACH", + /* 168 */ "EACH", + /* 169 */ "END", + /* 170 */ "EXPLAIN", + /* 171 */ "FAIL", + /* 172 */ "FOR", + /* 173 */ "IGNORE", + /* 174 */ "IMMEDIATE", + /* 175 */ "INITIALLY", + /* 176 */ "INSTEAD", + /* 177 */ "KEY", + /* 178 */ "OF", + /* 179 */ "RAISE", + /* 180 */ "REPLACE", + /* 181 */ "RESTRICT", + /* 182 */ "ROW", + /* 183 */ "STATEMENT", + /* 184 */ "TRIGGER", + /* 185 */ "VIEW", + /* 186 */ "IPTOKEN", + /* 187 */ "SEMI", + /* 188 */ "NONE", + /* 189 */ "PREV", + /* 190 */ "LINEAR", + /* 191 */ "IMPORT", + /* 192 */ "TBNAME", + /* 193 */ "JOIN", + /* 194 */ "INSERT", + /* 195 */ "INTO", + /* 196 */ "VALUES", + /* 197 */ "error", + /* 198 */ "program", + /* 199 */ "cmd", + /* 200 */ "ids", + /* 201 */ "dbPrefix", + /* 202 */ "cpxName", + /* 203 */ "ifexists", + /* 204 */ "alter_db_optr", + /* 205 */ "alter_topic_optr", + /* 206 */ "acct_optr", + /* 207 */ "exprlist", + /* 208 */ "ifnotexists", + /* 209 */ "db_optr", + /* 210 */ "topic_optr", + /* 211 */ "typename", + /* 212 */ "bufsize", + /* 213 */ "pps", + /* 214 */ "tseries", + /* 215 */ "dbs", + /* 216 */ "streams", + /* 217 */ "storage", + /* 218 */ "qtime", + /* 219 */ "users", + /* 220 */ "conns", + /* 221 */ "state", + /* 222 */ "intitemlist", + /* 223 */ "intitem", + /* 224 */ "keep", + /* 225 */ "cache", + /* 226 */ "replica", + /* 227 */ "quorum", + /* 228 */ "days", + /* 229 */ "minrows", + /* 230 */ "maxrows", + /* 231 */ "blocks", + /* 232 */ "ctime", + /* 233 */ "wal", + /* 234 */ "fsync", + /* 235 */ "comp", + /* 236 */ "prec", + /* 237 */ "update", + /* 238 */ "cachelast", + /* 239 */ "partitions", + /* 240 */ "signed", + /* 241 */ "create_table_args", + /* 242 */ "create_stable_args", + /* 243 */ "create_table_list", + /* 244 */ "create_from_stable", + /* 245 */ "columnlist", + /* 246 */ "tagitemlist", + /* 247 */ "tagNamelist", + /* 248 */ "select", + /* 249 */ "column", + /* 250 */ "tagitem", + /* 251 */ "selcollist", + /* 252 */ "from", + /* 253 */ "where_opt", + /* 254 */ "interval_option", + /* 255 */ "sliding_opt", + /* 256 */ "session_option", + /* 257 */ "windowstate_option", + /* 258 */ "fill_opt", + /* 259 */ "groupby_opt", + /* 260 */ "having_opt", + /* 261 */ "orderby_opt", + /* 262 */ "slimit_opt", + /* 263 */ "limit_opt", + /* 264 */ "union", + /* 265 */ "sclp", + /* 266 */ "distinct", + /* 267 */ "expr", + /* 268 */ "as", + /* 269 */ "tablelist", + /* 270 */ "sub", + /* 271 */ "tmvar", + /* 272 */ "intervalKey", + /* 273 */ "sortlist", + /* 274 */ "sortitem", + /* 275 */ "item", + /* 276 */ "sortorder", + /* 277 */ "grouplist", + /* 278 */ "expritem", +}; +#endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */ + +#ifndef NDEBUG +/* For tracing reduce actions, the names of all rules are required. +*/ +static const char *const yyRuleName[] = { + /* 0 */ "program ::= cmd", + /* 1 */ "cmd ::= SHOW DATABASES", + /* 2 */ "cmd ::= SHOW TOPICS", + /* 3 */ "cmd ::= SHOW FUNCTIONS", + /* 4 */ "cmd ::= SHOW MNODES", + /* 5 */ "cmd ::= SHOW DNODES", + /* 6 */ "cmd ::= SHOW ACCOUNTS", + /* 7 */ "cmd ::= SHOW USERS", + /* 8 */ "cmd ::= SHOW MODULES", + /* 9 */ "cmd ::= SHOW QUERIES", + /* 10 */ "cmd ::= SHOW CONNECTIONS", + /* 11 */ "cmd ::= SHOW STREAMS", + /* 12 */ "cmd ::= SHOW VARIABLES", + /* 13 */ "cmd ::= SHOW SCORES", + /* 14 */ "cmd ::= SHOW GRANTS", + /* 15 */ "cmd ::= SHOW VNODES", + /* 16 */ "cmd ::= SHOW VNODES ids", + /* 17 */ "dbPrefix ::=", + /* 18 */ "dbPrefix ::= ids DOT", + /* 19 */ "cpxName ::=", + /* 20 */ "cpxName ::= DOT ids", + /* 21 */ "cmd ::= SHOW CREATE TABLE ids cpxName", + /* 22 */ "cmd ::= SHOW CREATE STABLE ids cpxName", + /* 23 */ "cmd ::= SHOW CREATE DATABASE ids", + /* 24 */ "cmd ::= SHOW dbPrefix TABLES", + /* 25 */ "cmd ::= SHOW dbPrefix TABLES LIKE ids", + /* 26 */ "cmd ::= SHOW dbPrefix STABLES", + /* 27 */ "cmd ::= SHOW dbPrefix STABLES LIKE ids", + /* 28 */ "cmd ::= SHOW dbPrefix VGROUPS", + /* 29 */ "cmd ::= SHOW dbPrefix VGROUPS ids", + /* 30 */ "cmd ::= DROP TABLE ifexists ids cpxName", + /* 31 */ "cmd ::= DROP STABLE ifexists ids cpxName", + /* 32 */ "cmd ::= DROP DATABASE ifexists ids", + /* 33 */ "cmd ::= DROP TOPIC ifexists ids", + /* 34 */ "cmd ::= DROP FUNCTION ids", + /* 35 */ "cmd ::= DROP DNODE ids", + /* 36 */ "cmd ::= DROP USER ids", + /* 37 */ "cmd ::= DROP ACCOUNT ids", + /* 38 */ "cmd ::= USE ids", + /* 39 */ "cmd ::= DESCRIBE ids cpxName", + /* 40 */ "cmd ::= DESC ids cpxName", + /* 41 */ "cmd ::= ALTER USER ids PASS ids", + /* 42 */ "cmd ::= ALTER USER ids PRIVILEGE ids", + /* 43 */ "cmd ::= ALTER DNODE ids ids", + /* 44 */ "cmd ::= ALTER DNODE ids ids ids", + /* 45 */ "cmd ::= ALTER LOCAL ids", + /* 46 */ "cmd ::= ALTER LOCAL ids ids", + /* 47 */ "cmd ::= ALTER DATABASE ids alter_db_optr", + /* 48 */ "cmd ::= ALTER TOPIC ids alter_topic_optr", + /* 49 */ "cmd ::= ALTER ACCOUNT ids acct_optr", + /* 50 */ "cmd ::= ALTER ACCOUNT ids PASS ids acct_optr", + /* 51 */ "cmd ::= COMPACT VNODES IN LP exprlist RP", + /* 52 */ "ids ::= ID", + /* 53 */ "ids ::= STRING", + /* 54 */ "ifexists ::= IF EXISTS", + /* 55 */ "ifexists ::=", + /* 56 */ "ifnotexists ::= IF NOT EXISTS", + /* 57 */ "ifnotexists ::=", + /* 58 */ "cmd ::= CREATE DNODE ids", + /* 59 */ "cmd ::= CREATE ACCOUNT ids PASS ids acct_optr", + /* 60 */ "cmd ::= CREATE DATABASE ifnotexists ids db_optr", + /* 61 */ "cmd ::= CREATE TOPIC ifnotexists ids topic_optr", + /* 62 */ "cmd ::= CREATE FUNCTION ids AS ids OUTPUTTYPE typename bufsize", + /* 63 */ "cmd ::= CREATE AGGREGATE FUNCTION ids AS ids OUTPUTTYPE typename bufsize", + /* 64 */ "cmd ::= CREATE USER ids PASS ids", + /* 65 */ "bufsize ::=", + /* 66 */ "bufsize ::= BUFSIZE INTEGER", + /* 67 */ "pps ::=", + /* 68 */ "pps ::= PPS INTEGER", + /* 69 */ "tseries ::=", + /* 70 */ "tseries ::= TSERIES INTEGER", + /* 71 */ "dbs ::=", + /* 72 */ "dbs ::= DBS INTEGER", + /* 73 */ "streams ::=", + /* 74 */ "streams ::= STREAMS INTEGER", + /* 75 */ "storage ::=", + /* 76 */ "storage ::= STORAGE INTEGER", + /* 77 */ "qtime ::=", + /* 78 */ "qtime ::= QTIME INTEGER", + /* 79 */ "users ::=", + /* 80 */ "users ::= USERS INTEGER", + /* 81 */ "conns ::=", + /* 82 */ "conns ::= CONNS INTEGER", + /* 83 */ "state ::=", + /* 84 */ "state ::= STATE ids", + /* 85 */ "acct_optr ::= pps tseries storage streams qtime dbs users conns state", + /* 86 */ "intitemlist ::= intitemlist COMMA intitem", + /* 87 */ "intitemlist ::= intitem", + /* 88 */ "intitem ::= INTEGER", + /* 89 */ "keep ::= KEEP intitemlist", + /* 90 */ "cache ::= CACHE INTEGER", + /* 91 */ "replica ::= REPLICA INTEGER", + /* 92 */ "quorum ::= QUORUM INTEGER", + /* 93 */ "days ::= DAYS INTEGER", + /* 94 */ "minrows ::= MINROWS INTEGER", + /* 95 */ "maxrows ::= MAXROWS INTEGER", + /* 96 */ "blocks ::= BLOCKS INTEGER", + /* 97 */ "ctime ::= CTIME INTEGER", + /* 98 */ "wal ::= WAL INTEGER", + /* 99 */ "fsync ::= FSYNC INTEGER", + /* 100 */ "comp ::= COMP INTEGER", + /* 101 */ "prec ::= PRECISION STRING", + /* 102 */ "update ::= UPDATE INTEGER", + /* 103 */ "cachelast ::= CACHELAST INTEGER", + /* 104 */ "partitions ::= PARTITIONS INTEGER", + /* 105 */ "db_optr ::=", + /* 106 */ "db_optr ::= db_optr cache", + /* 107 */ "db_optr ::= db_optr replica", + /* 108 */ "db_optr ::= db_optr quorum", + /* 109 */ "db_optr ::= db_optr days", + /* 110 */ "db_optr ::= db_optr minrows", + /* 111 */ "db_optr ::= db_optr maxrows", + /* 112 */ "db_optr ::= db_optr blocks", + /* 113 */ "db_optr ::= db_optr ctime", + /* 114 */ "db_optr ::= db_optr wal", + /* 115 */ "db_optr ::= db_optr fsync", + /* 116 */ "db_optr ::= db_optr comp", + /* 117 */ "db_optr ::= db_optr prec", + /* 118 */ "db_optr ::= db_optr keep", + /* 119 */ "db_optr ::= db_optr update", + /* 120 */ "db_optr ::= db_optr cachelast", + /* 121 */ "topic_optr ::= db_optr", + /* 122 */ "topic_optr ::= topic_optr partitions", + /* 123 */ "alter_db_optr ::=", + /* 124 */ "alter_db_optr ::= alter_db_optr replica", + /* 125 */ "alter_db_optr ::= alter_db_optr quorum", + /* 126 */ "alter_db_optr ::= alter_db_optr keep", + /* 127 */ "alter_db_optr ::= alter_db_optr blocks", + /* 128 */ "alter_db_optr ::= alter_db_optr comp", + /* 129 */ "alter_db_optr ::= alter_db_optr update", + /* 130 */ "alter_db_optr ::= alter_db_optr cachelast", + /* 131 */ "alter_topic_optr ::= alter_db_optr", + /* 132 */ "alter_topic_optr ::= alter_topic_optr partitions", + /* 133 */ "typename ::= ids", + /* 134 */ "typename ::= ids LP signed RP", + /* 135 */ "typename ::= ids UNSIGNED", + /* 136 */ "signed ::= INTEGER", + /* 137 */ "signed ::= PLUS INTEGER", + /* 138 */ "signed ::= MINUS INTEGER", + /* 139 */ "cmd ::= CREATE TABLE create_table_args", + /* 140 */ "cmd ::= CREATE TABLE create_stable_args", + /* 141 */ "cmd ::= CREATE STABLE create_stable_args", + /* 142 */ "cmd ::= CREATE TABLE create_table_list", + /* 143 */ "create_table_list ::= create_from_stable", + /* 144 */ "create_table_list ::= create_table_list create_from_stable", + /* 145 */ "create_table_args ::= ifnotexists ids cpxName LP columnlist RP", + /* 146 */ "create_stable_args ::= ifnotexists ids cpxName LP columnlist RP TAGS LP columnlist RP", + /* 147 */ "create_from_stable ::= ifnotexists ids cpxName USING ids cpxName TAGS LP tagitemlist RP", + /* 148 */ "create_from_stable ::= ifnotexists ids cpxName USING ids cpxName LP tagNamelist RP TAGS LP tagitemlist RP", + /* 149 */ "tagNamelist ::= tagNamelist COMMA ids", + /* 150 */ "tagNamelist ::= ids", + /* 151 */ "create_table_args ::= ifnotexists ids cpxName AS select", + /* 152 */ "columnlist ::= columnlist COMMA column", + /* 153 */ "columnlist ::= column", + /* 154 */ "column ::= ids typename", + /* 155 */ "tagitemlist ::= tagitemlist COMMA tagitem", + /* 156 */ "tagitemlist ::= tagitem", + /* 157 */ "tagitem ::= INTEGER", + /* 158 */ "tagitem ::= FLOAT", + /* 159 */ "tagitem ::= STRING", + /* 160 */ "tagitem ::= BOOL", + /* 161 */ "tagitem ::= NULL", + /* 162 */ "tagitem ::= NOW", + /* 163 */ "tagitem ::= MINUS INTEGER", + /* 164 */ "tagitem ::= MINUS FLOAT", + /* 165 */ "tagitem ::= PLUS INTEGER", + /* 166 */ "tagitem ::= PLUS FLOAT", + /* 167 */ "select ::= SELECT selcollist from where_opt interval_option sliding_opt session_option windowstate_option fill_opt groupby_opt having_opt orderby_opt slimit_opt limit_opt", + /* 168 */ "select ::= LP select RP", + /* 169 */ "union ::= select", + /* 170 */ "union ::= union UNION ALL select", + /* 171 */ "cmd ::= union", + /* 172 */ "select ::= SELECT selcollist", + /* 173 */ "sclp ::= selcollist COMMA", + /* 174 */ "sclp ::=", + /* 175 */ "selcollist ::= sclp distinct expr as", + /* 176 */ "selcollist ::= sclp STAR", + /* 177 */ "as ::= AS ids", + /* 178 */ "as ::= ids", + /* 179 */ "as ::=", + /* 180 */ "distinct ::= DISTINCT", + /* 181 */ "distinct ::=", + /* 182 */ "from ::= FROM tablelist", + /* 183 */ "from ::= FROM sub", + /* 184 */ "sub ::= LP union RP", + /* 185 */ "sub ::= LP union RP ids", + /* 186 */ "sub ::= sub COMMA LP union RP ids", + /* 187 */ "tablelist ::= ids cpxName", + /* 188 */ "tablelist ::= ids cpxName ids", + /* 189 */ "tablelist ::= tablelist COMMA ids cpxName", + /* 190 */ "tablelist ::= tablelist COMMA ids cpxName ids", + /* 191 */ "tmvar ::= VARIABLE", + /* 192 */ "interval_option ::= intervalKey LP tmvar RP", + /* 193 */ "interval_option ::= intervalKey LP tmvar COMMA tmvar RP", + /* 194 */ "interval_option ::=", + /* 195 */ "intervalKey ::= INTERVAL", + /* 196 */ "intervalKey ::= EVERY", + /* 197 */ "session_option ::=", + /* 198 */ "session_option ::= SESSION LP ids cpxName COMMA tmvar RP", + /* 199 */ "windowstate_option ::=", + /* 200 */ "windowstate_option ::= STATE_WINDOW LP ids RP", + /* 201 */ "fill_opt ::=", + /* 202 */ "fill_opt ::= FILL LP ID COMMA tagitemlist RP", + /* 203 */ "fill_opt ::= FILL LP ID RP", + /* 204 */ "sliding_opt ::= SLIDING LP tmvar RP", + /* 205 */ "sliding_opt ::=", + /* 206 */ "orderby_opt ::=", + /* 207 */ "orderby_opt ::= ORDER BY sortlist", + /* 208 */ "sortlist ::= sortlist COMMA item sortorder", + /* 209 */ "sortlist ::= item sortorder", + /* 210 */ "item ::= ids cpxName", + /* 211 */ "sortorder ::= ASC", + /* 212 */ "sortorder ::= DESC", + /* 213 */ "sortorder ::=", + /* 214 */ "groupby_opt ::=", + /* 215 */ "groupby_opt ::= GROUP BY grouplist", + /* 216 */ "grouplist ::= grouplist COMMA item", + /* 217 */ "grouplist ::= item", + /* 218 */ "having_opt ::=", + /* 219 */ "having_opt ::= HAVING expr", + /* 220 */ "limit_opt ::=", + /* 221 */ "limit_opt ::= LIMIT signed", + /* 222 */ "limit_opt ::= LIMIT signed OFFSET signed", + /* 223 */ "limit_opt ::= LIMIT signed COMMA signed", + /* 224 */ "slimit_opt ::=", + /* 225 */ "slimit_opt ::= SLIMIT signed", + /* 226 */ "slimit_opt ::= SLIMIT signed SOFFSET signed", + /* 227 */ "slimit_opt ::= SLIMIT signed COMMA signed", + /* 228 */ "where_opt ::=", + /* 229 */ "where_opt ::= WHERE expr", + /* 230 */ "expr ::= LP expr RP", + /* 231 */ "expr ::= ID", + /* 232 */ "expr ::= ID DOT ID", + /* 233 */ "expr ::= ID DOT STAR", + /* 234 */ "expr ::= INTEGER", + /* 235 */ "expr ::= MINUS INTEGER", + /* 236 */ "expr ::= PLUS INTEGER", + /* 237 */ "expr ::= FLOAT", + /* 238 */ "expr ::= MINUS FLOAT", + /* 239 */ "expr ::= PLUS FLOAT", + /* 240 */ "expr ::= STRING", + /* 241 */ "expr ::= NOW", + /* 242 */ "expr ::= VARIABLE", + /* 243 */ "expr ::= PLUS VARIABLE", + /* 244 */ "expr ::= MINUS VARIABLE", + /* 245 */ "expr ::= BOOL", + /* 246 */ "expr ::= NULL", + /* 247 */ "expr ::= ID LP exprlist RP", + /* 248 */ "expr ::= ID LP STAR RP", + /* 249 */ "expr ::= expr IS NULL", + /* 250 */ "expr ::= expr IS NOT NULL", + /* 251 */ "expr ::= expr LT expr", + /* 252 */ "expr ::= expr GT expr", + /* 253 */ "expr ::= expr LE expr", + /* 254 */ "expr ::= expr GE expr", + /* 255 */ "expr ::= expr NE expr", + /* 256 */ "expr ::= expr EQ expr", + /* 257 */ "expr ::= expr BETWEEN expr AND expr", + /* 258 */ "expr ::= expr AND expr", + /* 259 */ "expr ::= expr OR expr", + /* 260 */ "expr ::= expr PLUS expr", + /* 261 */ "expr ::= expr MINUS expr", + /* 262 */ "expr ::= expr STAR expr", + /* 263 */ "expr ::= expr SLASH expr", + /* 264 */ "expr ::= expr REM expr", + /* 265 */ "expr ::= expr LIKE expr", + /* 266 */ "expr ::= expr MATCH expr", + /* 267 */ "expr ::= expr NMATCH expr", + /* 268 */ "expr ::= expr IN LP exprlist RP", + /* 269 */ "exprlist ::= exprlist COMMA expritem", + /* 270 */ "exprlist ::= expritem", + /* 271 */ "expritem ::= expr", + /* 272 */ "expritem ::=", + /* 273 */ "cmd ::= RESET QUERY CACHE", + /* 274 */ "cmd ::= SYNCDB ids REPLICA", + /* 275 */ "cmd ::= ALTER TABLE ids cpxName ADD COLUMN columnlist", + /* 276 */ "cmd ::= ALTER TABLE ids cpxName DROP COLUMN ids", + /* 277 */ "cmd ::= ALTER TABLE ids cpxName MODIFY COLUMN columnlist", + /* 278 */ "cmd ::= ALTER TABLE ids cpxName ADD TAG columnlist", + /* 279 */ "cmd ::= ALTER TABLE ids cpxName DROP TAG ids", + /* 280 */ "cmd ::= ALTER TABLE ids cpxName CHANGE TAG ids ids", + /* 281 */ "cmd ::= ALTER TABLE ids cpxName SET TAG ids EQ tagitem", + /* 282 */ "cmd ::= ALTER TABLE ids cpxName MODIFY TAG columnlist", + /* 283 */ "cmd ::= ALTER STABLE ids cpxName ADD COLUMN columnlist", + /* 284 */ "cmd ::= ALTER STABLE ids cpxName DROP COLUMN ids", + /* 285 */ "cmd ::= ALTER STABLE ids cpxName MODIFY COLUMN columnlist", + /* 286 */ "cmd ::= ALTER STABLE ids cpxName ADD TAG columnlist", + /* 287 */ "cmd ::= ALTER STABLE ids cpxName DROP TAG ids", + /* 288 */ "cmd ::= ALTER STABLE ids cpxName CHANGE TAG ids ids", + /* 289 */ "cmd ::= ALTER STABLE ids cpxName SET TAG ids EQ tagitem", + /* 290 */ "cmd ::= ALTER STABLE ids cpxName MODIFY TAG columnlist", + /* 291 */ "cmd ::= KILL CONNECTION INTEGER", + /* 292 */ "cmd ::= KILL STREAM INTEGER COLON INTEGER", + /* 293 */ "cmd ::= KILL QUERY INTEGER COLON INTEGER", +}; +#endif /* NDEBUG */ + + +#if YYSTACKDEPTH<=0 +/* +** Try to increase the size of the parser stack. Return the number +** of errors. Return 0 on success. +*/ +static int yyGrowStack(yyParser *p){ + int newSize; + int idx; + yyStackEntry *pNew; + + newSize = p->yystksz*2 + 100; + idx = p->yytos ? (int)(p->yytos - p->yystack) : 0; + if( p->yystack==&p->yystk0 ){ + pNew = malloc(newSize*sizeof(pNew[0])); + if( pNew ) pNew[0] = p->yystk0; + }else{ + pNew = realloc(p->yystack, newSize*sizeof(pNew[0])); + } + if( pNew ){ + p->yystack = pNew; + p->yytos = &p->yystack[idx]; +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sStack grows from %d to %d entries.\n", + yyTracePrompt, p->yystksz, newSize); + } +#endif + p->yystksz = newSize; + } + return pNew==0; +} +#endif + +/* Datatype of the argument to the memory allocated passed as the +** second argument to ParseAlloc() below. This can be changed by +** putting an appropriate #define in the %include section of the input +** grammar. +*/ +#ifndef YYMALLOCARGTYPE +# define YYMALLOCARGTYPE size_t +#endif + +/* Initialize a new parser that has already been allocated. +*/ +void ParseInit(void *yypParser){ + yyParser *pParser = (yyParser*)yypParser; +#ifdef YYTRACKMAXSTACKDEPTH + pParser->yyhwm = 0; +#endif +#if YYSTACKDEPTH<=0 + pParser->yytos = NULL; + pParser->yystack = NULL; + pParser->yystksz = 0; + if( yyGrowStack(pParser) ){ + pParser->yystack = &pParser->yystk0; + pParser->yystksz = 1; + } +#endif +#ifndef YYNOERRORRECOVERY + pParser->yyerrcnt = -1; +#endif + pParser->yytos = pParser->yystack; + pParser->yystack[0].stateno = 0; + pParser->yystack[0].major = 0; +#if YYSTACKDEPTH>0 + pParser->yystackEnd = &pParser->yystack[YYSTACKDEPTH-1]; +#endif +} + +#ifndef Parse_ENGINEALWAYSONSTACK +/* +** This function allocates a new parser. +** The only argument is a pointer to a function which works like +** malloc. +** +** Inputs: +** A pointer to the function used to allocate memory. +** +** Outputs: +** A pointer to a parser. This pointer is used in subsequent calls +** to Parse and ParseFree. +*/ +void *ParseAlloc(void *(*mallocProc)(YYMALLOCARGTYPE)){ + yyParser *pParser; + pParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) ); + if( pParser ) ParseInit(pParser); + return pParser; +} +#endif /* Parse_ENGINEALWAYSONSTACK */ + + +/* The following function deletes the "minor type" or semantic value +** associated with a symbol. The symbol can be either a terminal +** or nonterminal. "yymajor" is the symbol code, and "yypminor" is +** a pointer to the value to be deleted. The code used to do the +** deletions is derived from the %destructor and/or %token_destructor +** directives of the input grammar. +*/ +static void yy_destructor( + yyParser *yypParser, /* The parser */ + YYCODETYPE yymajor, /* Type code for object to destroy */ + YYMINORTYPE *yypminor /* The object to be destroyed */ +){ + ParseARG_FETCH; + switch( yymajor ){ + /* Here is inserted the actions which take place when a + ** terminal or non-terminal is destroyed. This can happen + ** when the symbol is popped from the stack during a + ** reduce or during error processing or when a parser is + ** being destroyed before it is finished parsing. + ** + ** Note: during a reduce, the only symbols destroyed are those + ** which appear on the RHS of the rule, but which are *not* used + ** inside the C code. + */ +/********* Begin destructor definitions ***************************************/ + case 207: /* exprlist */ + case 251: /* selcollist */ + case 265: /* sclp */ +{ +tSqlExprListDestroy((yypminor->yy135)); +} + break; + case 222: /* intitemlist */ + case 224: /* keep */ + case 245: /* columnlist */ + case 246: /* tagitemlist */ + case 247: /* tagNamelist */ + case 258: /* fill_opt */ + case 259: /* groupby_opt */ + case 261: /* orderby_opt */ + case 273: /* sortlist */ + case 277: /* grouplist */ +{ +taosArrayDestroy((yypminor->yy135)); +} + break; + case 243: /* create_table_list */ +{ +destroyCreateTableSql((yypminor->yy110)); +} + break; + case 248: /* select */ +{ +destroySqlNode((yypminor->yy488)); +} + break; + case 252: /* from */ + case 269: /* tablelist */ + case 270: /* sub */ +{ +destroyRelationInfo((yypminor->yy460)); +} + break; + case 253: /* where_opt */ + case 260: /* having_opt */ + case 267: /* expr */ + case 278: /* expritem */ +{ +tSqlExprDestroy((yypminor->yy526)); +} + break; + case 264: /* union */ +{ +destroyAllSqlNode((yypminor->yy135)); +} + break; + case 274: /* sortitem */ +{ +taosVariantDestroy(&(yypminor->yy191)); +} + break; +/********* End destructor definitions *****************************************/ + default: break; /* If no destructor action specified: do nothing */ + } +} + +/* +** Pop the parser's stack once. +** +** If there is a destructor routine associated with the token which +** is popped from the stack, then call it. +*/ +static void yy_pop_parser_stack(yyParser *pParser){ + yyStackEntry *yytos; + assert( pParser->yytos!=0 ); + assert( pParser->yytos > pParser->yystack ); + yytos = pParser->yytos--; +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sPopping %s\n", + yyTracePrompt, + yyTokenName[yytos->major]); + } +#endif + yy_destructor(pParser, yytos->major, &yytos->minor); +} + +/* +** Clear all secondary memory allocations from the parser +*/ +void ParseFinalize(void *p){ + yyParser *pParser = (yyParser*)p; + while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser); +#if YYSTACKDEPTH<=0 + if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack); +#endif +} + +#ifndef Parse_ENGINEALWAYSONSTACK +/* +** Deallocate and destroy a parser. Destructors are called for +** all stack elements before shutting the parser down. +** +** If the YYPARSEFREENEVERNULL macro exists (for example because it +** is defined in a %include section of the input grammar) then it is +** assumed that the input pointer is never NULL. +*/ +void ParseFree( + void *p, /* The parser to be deleted */ + void (*freeProc)(void*) /* Function used to reclaim memory */ +){ +#ifndef YYPARSEFREENEVERNULL + if( p==0 ) return; +#endif + ParseFinalize(p); + (*freeProc)(p); +} +#endif /* Parse_ENGINEALWAYSONSTACK */ + +/* +** Return the peak depth of the stack for a parser. +*/ +#ifdef YYTRACKMAXSTACKDEPTH +int ParseStackPeak(void *p){ + yyParser *pParser = (yyParser*)p; + return pParser->yyhwm; +} +#endif + +/* This array of booleans keeps track of the parser statement +** coverage. The element yycoverage[X][Y] is set when the parser +** is in state X and has a lookahead token Y. In a well-tested +** systems, every element of this matrix should end up being set. +*/ +#if defined(YYCOVERAGE) +static unsigned char yycoverage[YYNSTATE][YYNTOKEN]; +#endif + +/* +** Write into out a description of every state/lookahead combination that +** +** (1) has not been used by the parser, and +** (2) is not a syntax error. +** +** Return the number of missed state/lookahead combinations. +*/ +#if defined(YYCOVERAGE) +int ParseCoverage(FILE *out){ + int stateno, iLookAhead, i; + int nMissed = 0; + for(stateno=0; statenoyytos->stateno; + + if( stateno>YY_MAX_SHIFT ) return stateno; + assert( stateno <= YY_SHIFT_COUNT ); +#if defined(YYCOVERAGE) + yycoverage[stateno][iLookAhead] = 1; +#endif + do{ + i = yy_shift_ofst[stateno]; + assert( i>=0 && i+YYNTOKEN<=sizeof(yy_lookahead)/sizeof(yy_lookahead[0]) ); + assert( iLookAhead!=YYNOCODE ); + assert( iLookAhead < YYNTOKEN ); + i += iLookAhead; + if( yy_lookahead[i]!=iLookAhead ){ +#ifdef YYFALLBACK + YYCODETYPE iFallback; /* Fallback token */ + if( iLookAhead %s\n", + yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]); + } +#endif + assert( yyFallback[iFallback]==0 ); /* Fallback loop must terminate */ + iLookAhead = iFallback; + continue; + } +#endif +#ifdef YYWILDCARD + { + int j = i - iLookAhead + YYWILDCARD; + if( +#if YY_SHIFT_MIN+YYWILDCARD<0 + j>=0 && +#endif +#if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT + j0 + ){ +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n", + yyTracePrompt, yyTokenName[iLookAhead], + yyTokenName[YYWILDCARD]); + } +#endif /* NDEBUG */ + return yy_action[j]; + } + } +#endif /* YYWILDCARD */ + return yy_default[stateno]; + }else{ + return yy_action[i]; + } + }while(1); +} + +/* +** Find the appropriate action for a parser given the non-terminal +** look-ahead token iLookAhead. +*/ +static int yy_find_reduce_action( + int stateno, /* Current state number */ + YYCODETYPE iLookAhead /* The look-ahead token */ +){ + int i; +#ifdef YYERRORSYMBOL + if( stateno>YY_REDUCE_COUNT ){ + return yy_default[stateno]; + } +#else + assert( stateno<=YY_REDUCE_COUNT ); +#endif + i = yy_reduce_ofst[stateno]; + assert( iLookAhead!=YYNOCODE ); + i += iLookAhead; +#ifdef YYERRORSYMBOL + if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){ + return yy_default[stateno]; + } +#else + assert( i>=0 && iyytos>yypParser->yystack ) yy_pop_parser_stack(yypParser); + /* Here code is inserted which will execute if the parser + ** stack every overflows */ +/******** Begin %stack_overflow code ******************************************/ +/******** End %stack_overflow code ********************************************/ + ParseARG_STORE; /* Suppress warning about unused %extra_argument var */ +} + +/* +** Print tracing information for a SHIFT action +*/ +#ifndef NDEBUG +static void yyTraceShift(yyParser *yypParser, int yyNewState, const char *zTag){ + if( yyTraceFILE ){ + if( yyNewStateyytos->major], + yyNewState); + }else{ + fprintf(yyTraceFILE,"%s%s '%s', pending reduce %d\n", + yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major], + yyNewState - YY_MIN_REDUCE); + } + } +} +#else +# define yyTraceShift(X,Y,Z) +#endif + +/* +** Perform a shift action. +*/ +static void yy_shift( + yyParser *yypParser, /* The parser to be shifted */ + int yyNewState, /* The new state to shift in */ + int yyMajor, /* The major token to shift in */ + ParseTOKENTYPE yyMinor /* The minor token to shift in */ +){ + yyStackEntry *yytos; + yypParser->yytos++; +#ifdef YYTRACKMAXSTACKDEPTH + if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){ + yypParser->yyhwm++; + assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) ); + } +#endif +#if YYSTACKDEPTH>0 + if( yypParser->yytos>yypParser->yystackEnd ){ + yypParser->yytos--; + yyStackOverflow(yypParser); + return; + } +#else + if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){ + if( yyGrowStack(yypParser) ){ + yypParser->yytos--; + yyStackOverflow(yypParser); + return; + } + } +#endif + if( yyNewState > YY_MAX_SHIFT ){ + yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE; + } + yytos = yypParser->yytos; + yytos->stateno = (YYACTIONTYPE)yyNewState; + yytos->major = (YYCODETYPE)yyMajor; + yytos->minor.yy0 = yyMinor; + yyTraceShift(yypParser, yyNewState, "Shift"); +} + +/* The following table contains information about every rule that +** is used during the reduce. +*/ +static const struct { + YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */ + signed char nrhs; /* Negative of the number of RHS symbols in the rule */ +} yyRuleInfo[] = { + { 198, -1 }, /* (0) program ::= cmd */ + { 199, -2 }, /* (1) cmd ::= SHOW DATABASES */ + { 199, -2 }, /* (2) cmd ::= SHOW TOPICS */ + { 199, -2 }, /* (3) cmd ::= SHOW FUNCTIONS */ + { 199, -2 }, /* (4) cmd ::= SHOW MNODES */ + { 199, -2 }, /* (5) cmd ::= SHOW DNODES */ + { 199, -2 }, /* (6) cmd ::= SHOW ACCOUNTS */ + { 199, -2 }, /* (7) cmd ::= SHOW USERS */ + { 199, -2 }, /* (8) cmd ::= SHOW MODULES */ + { 199, -2 }, /* (9) cmd ::= SHOW QUERIES */ + { 199, -2 }, /* (10) cmd ::= SHOW CONNECTIONS */ + { 199, -2 }, /* (11) cmd ::= SHOW STREAMS */ + { 199, -2 }, /* (12) cmd ::= SHOW VARIABLES */ + { 199, -2 }, /* (13) cmd ::= SHOW SCORES */ + { 199, -2 }, /* (14) cmd ::= SHOW GRANTS */ + { 199, -2 }, /* (15) cmd ::= SHOW VNODES */ + { 199, -3 }, /* (16) cmd ::= SHOW VNODES ids */ + { 201, 0 }, /* (17) dbPrefix ::= */ + { 201, -2 }, /* (18) dbPrefix ::= ids DOT */ + { 202, 0 }, /* (19) cpxName ::= */ + { 202, -2 }, /* (20) cpxName ::= DOT ids */ + { 199, -5 }, /* (21) cmd ::= SHOW CREATE TABLE ids cpxName */ + { 199, -5 }, /* (22) cmd ::= SHOW CREATE STABLE ids cpxName */ + { 199, -4 }, /* (23) cmd ::= SHOW CREATE DATABASE ids */ + { 199, -3 }, /* (24) cmd ::= SHOW dbPrefix TABLES */ + { 199, -5 }, /* (25) cmd ::= SHOW dbPrefix TABLES LIKE ids */ + { 199, -3 }, /* (26) cmd ::= SHOW dbPrefix STABLES */ + { 199, -5 }, /* (27) cmd ::= SHOW dbPrefix STABLES LIKE ids */ + { 199, -3 }, /* (28) cmd ::= SHOW dbPrefix VGROUPS */ + { 199, -4 }, /* (29) cmd ::= SHOW dbPrefix VGROUPS ids */ + { 199, -5 }, /* (30) cmd ::= DROP TABLE ifexists ids cpxName */ + { 199, -5 }, /* (31) cmd ::= DROP STABLE ifexists ids cpxName */ + { 199, -4 }, /* (32) cmd ::= DROP DATABASE ifexists ids */ + { 199, -4 }, /* (33) cmd ::= DROP TOPIC ifexists ids */ + { 199, -3 }, /* (34) cmd ::= DROP FUNCTION ids */ + { 199, -3 }, /* (35) cmd ::= DROP DNODE ids */ + { 199, -3 }, /* (36) cmd ::= DROP USER ids */ + { 199, -3 }, /* (37) cmd ::= DROP ACCOUNT ids */ + { 199, -2 }, /* (38) cmd ::= USE ids */ + { 199, -3 }, /* (39) cmd ::= DESCRIBE ids cpxName */ + { 199, -3 }, /* (40) cmd ::= DESC ids cpxName */ + { 199, -5 }, /* (41) cmd ::= ALTER USER ids PASS ids */ + { 199, -5 }, /* (42) cmd ::= ALTER USER ids PRIVILEGE ids */ + { 199, -4 }, /* (43) cmd ::= ALTER DNODE ids ids */ + { 199, -5 }, /* (44) cmd ::= ALTER DNODE ids ids ids */ + { 199, -3 }, /* (45) cmd ::= ALTER LOCAL ids */ + { 199, -4 }, /* (46) cmd ::= ALTER LOCAL ids ids */ + { 199, -4 }, /* (47) cmd ::= ALTER DATABASE ids alter_db_optr */ + { 199, -4 }, /* (48) cmd ::= ALTER TOPIC ids alter_topic_optr */ + { 199, -4 }, /* (49) cmd ::= ALTER ACCOUNT ids acct_optr */ + { 199, -6 }, /* (50) cmd ::= ALTER ACCOUNT ids PASS ids acct_optr */ + { 199, -6 }, /* (51) cmd ::= COMPACT VNODES IN LP exprlist RP */ + { 200, -1 }, /* (52) ids ::= ID */ + { 200, -1 }, /* (53) ids ::= STRING */ + { 203, -2 }, /* (54) ifexists ::= IF EXISTS */ + { 203, 0 }, /* (55) ifexists ::= */ + { 208, -3 }, /* (56) ifnotexists ::= IF NOT EXISTS */ + { 208, 0 }, /* (57) ifnotexists ::= */ + { 199, -3 }, /* (58) cmd ::= CREATE DNODE ids */ + { 199, -6 }, /* (59) cmd ::= CREATE ACCOUNT ids PASS ids acct_optr */ + { 199, -5 }, /* (60) cmd ::= CREATE DATABASE ifnotexists ids db_optr */ + { 199, -5 }, /* (61) cmd ::= CREATE TOPIC ifnotexists ids topic_optr */ + { 199, -8 }, /* (62) cmd ::= CREATE FUNCTION ids AS ids OUTPUTTYPE typename bufsize */ + { 199, -9 }, /* (63) cmd ::= CREATE AGGREGATE FUNCTION ids AS ids OUTPUTTYPE typename bufsize */ + { 199, -5 }, /* (64) cmd ::= CREATE USER ids PASS ids */ + { 212, 0 }, /* (65) bufsize ::= */ + { 212, -2 }, /* (66) bufsize ::= BUFSIZE INTEGER */ + { 213, 0 }, /* (67) pps ::= */ + { 213, -2 }, /* (68) pps ::= PPS INTEGER */ + { 214, 0 }, /* (69) tseries ::= */ + { 214, -2 }, /* (70) tseries ::= TSERIES INTEGER */ + { 215, 0 }, /* (71) dbs ::= */ + { 215, -2 }, /* (72) dbs ::= DBS INTEGER */ + { 216, 0 }, /* (73) streams ::= */ + { 216, -2 }, /* (74) streams ::= STREAMS INTEGER */ + { 217, 0 }, /* (75) storage ::= */ + { 217, -2 }, /* (76) storage ::= STORAGE INTEGER */ + { 218, 0 }, /* (77) qtime ::= */ + { 218, -2 }, /* (78) qtime ::= QTIME INTEGER */ + { 219, 0 }, /* (79) users ::= */ + { 219, -2 }, /* (80) users ::= USERS INTEGER */ + { 220, 0 }, /* (81) conns ::= */ + { 220, -2 }, /* (82) conns ::= CONNS INTEGER */ + { 221, 0 }, /* (83) state ::= */ + { 221, -2 }, /* (84) state ::= STATE ids */ + { 206, -9 }, /* (85) acct_optr ::= pps tseries storage streams qtime dbs users conns state */ + { 222, -3 }, /* (86) intitemlist ::= intitemlist COMMA intitem */ + { 222, -1 }, /* (87) intitemlist ::= intitem */ + { 223, -1 }, /* (88) intitem ::= INTEGER */ + { 224, -2 }, /* (89) keep ::= KEEP intitemlist */ + { 225, -2 }, /* (90) cache ::= CACHE INTEGER */ + { 226, -2 }, /* (91) replica ::= REPLICA INTEGER */ + { 227, -2 }, /* (92) quorum ::= QUORUM INTEGER */ + { 228, -2 }, /* (93) days ::= DAYS INTEGER */ + { 229, -2 }, /* (94) minrows ::= MINROWS INTEGER */ + { 230, -2 }, /* (95) maxrows ::= MAXROWS INTEGER */ + { 231, -2 }, /* (96) blocks ::= BLOCKS INTEGER */ + { 232, -2 }, /* (97) ctime ::= CTIME INTEGER */ + { 233, -2 }, /* (98) wal ::= WAL INTEGER */ + { 234, -2 }, /* (99) fsync ::= FSYNC INTEGER */ + { 235, -2 }, /* (100) comp ::= COMP INTEGER */ + { 236, -2 }, /* (101) prec ::= PRECISION STRING */ + { 237, -2 }, /* (102) update ::= UPDATE INTEGER */ + { 238, -2 }, /* (103) cachelast ::= CACHELAST INTEGER */ + { 239, -2 }, /* (104) partitions ::= PARTITIONS INTEGER */ + { 209, 0 }, /* (105) db_optr ::= */ + { 209, -2 }, /* (106) db_optr ::= db_optr cache */ + { 209, -2 }, /* (107) db_optr ::= db_optr replica */ + { 209, -2 }, /* (108) db_optr ::= db_optr quorum */ + { 209, -2 }, /* (109) db_optr ::= db_optr days */ + { 209, -2 }, /* (110) db_optr ::= db_optr minrows */ + { 209, -2 }, /* (111) db_optr ::= db_optr maxrows */ + { 209, -2 }, /* (112) db_optr ::= db_optr blocks */ + { 209, -2 }, /* (113) db_optr ::= db_optr ctime */ + { 209, -2 }, /* (114) db_optr ::= db_optr wal */ + { 209, -2 }, /* (115) db_optr ::= db_optr fsync */ + { 209, -2 }, /* (116) db_optr ::= db_optr comp */ + { 209, -2 }, /* (117) db_optr ::= db_optr prec */ + { 209, -2 }, /* (118) db_optr ::= db_optr keep */ + { 209, -2 }, /* (119) db_optr ::= db_optr update */ + { 209, -2 }, /* (120) db_optr ::= db_optr cachelast */ + { 210, -1 }, /* (121) topic_optr ::= db_optr */ + { 210, -2 }, /* (122) topic_optr ::= topic_optr partitions */ + { 204, 0 }, /* (123) alter_db_optr ::= */ + { 204, -2 }, /* (124) alter_db_optr ::= alter_db_optr replica */ + { 204, -2 }, /* (125) alter_db_optr ::= alter_db_optr quorum */ + { 204, -2 }, /* (126) alter_db_optr ::= alter_db_optr keep */ + { 204, -2 }, /* (127) alter_db_optr ::= alter_db_optr blocks */ + { 204, -2 }, /* (128) alter_db_optr ::= alter_db_optr comp */ + { 204, -2 }, /* (129) alter_db_optr ::= alter_db_optr update */ + { 204, -2 }, /* (130) alter_db_optr ::= alter_db_optr cachelast */ + { 205, -1 }, /* (131) alter_topic_optr ::= alter_db_optr */ + { 205, -2 }, /* (132) alter_topic_optr ::= alter_topic_optr partitions */ + { 211, -1 }, /* (133) typename ::= ids */ + { 211, -4 }, /* (134) typename ::= ids LP signed RP */ + { 211, -2 }, /* (135) typename ::= ids UNSIGNED */ + { 240, -1 }, /* (136) signed ::= INTEGER */ + { 240, -2 }, /* (137) signed ::= PLUS INTEGER */ + { 240, -2 }, /* (138) signed ::= MINUS INTEGER */ + { 199, -3 }, /* (139) cmd ::= CREATE TABLE create_table_args */ + { 199, -3 }, /* (140) cmd ::= CREATE TABLE create_stable_args */ + { 199, -3 }, /* (141) cmd ::= CREATE STABLE create_stable_args */ + { 199, -3 }, /* (142) cmd ::= CREATE TABLE create_table_list */ + { 243, -1 }, /* (143) create_table_list ::= create_from_stable */ + { 243, -2 }, /* (144) create_table_list ::= create_table_list create_from_stable */ + { 241, -6 }, /* (145) create_table_args ::= ifnotexists ids cpxName LP columnlist RP */ + { 242, -10 }, /* (146) create_stable_args ::= ifnotexists ids cpxName LP columnlist RP TAGS LP columnlist RP */ + { 244, -10 }, /* (147) create_from_stable ::= ifnotexists ids cpxName USING ids cpxName TAGS LP tagitemlist RP */ + { 244, -13 }, /* (148) create_from_stable ::= ifnotexists ids cpxName USING ids cpxName LP tagNamelist RP TAGS LP tagitemlist RP */ + { 247, -3 }, /* (149) tagNamelist ::= tagNamelist COMMA ids */ + { 247, -1 }, /* (150) tagNamelist ::= ids */ + { 241, -5 }, /* (151) create_table_args ::= ifnotexists ids cpxName AS select */ + { 245, -3 }, /* (152) columnlist ::= columnlist COMMA column */ + { 245, -1 }, /* (153) columnlist ::= column */ + { 249, -2 }, /* (154) column ::= ids typename */ + { 246, -3 }, /* (155) tagitemlist ::= tagitemlist COMMA tagitem */ + { 246, -1 }, /* (156) tagitemlist ::= tagitem */ + { 250, -1 }, /* (157) tagitem ::= INTEGER */ + { 250, -1 }, /* (158) tagitem ::= FLOAT */ + { 250, -1 }, /* (159) tagitem ::= STRING */ + { 250, -1 }, /* (160) tagitem ::= BOOL */ + { 250, -1 }, /* (161) tagitem ::= NULL */ + { 250, -1 }, /* (162) tagitem ::= NOW */ + { 250, -2 }, /* (163) tagitem ::= MINUS INTEGER */ + { 250, -2 }, /* (164) tagitem ::= MINUS FLOAT */ + { 250, -2 }, /* (165) tagitem ::= PLUS INTEGER */ + { 250, -2 }, /* (166) tagitem ::= PLUS FLOAT */ + { 248, -14 }, /* (167) select ::= SELECT selcollist from where_opt interval_option sliding_opt session_option windowstate_option fill_opt groupby_opt having_opt orderby_opt slimit_opt limit_opt */ + { 248, -3 }, /* (168) select ::= LP select RP */ + { 264, -1 }, /* (169) union ::= select */ + { 264, -4 }, /* (170) union ::= union UNION ALL select */ + { 199, -1 }, /* (171) cmd ::= union */ + { 248, -2 }, /* (172) select ::= SELECT selcollist */ + { 265, -2 }, /* (173) sclp ::= selcollist COMMA */ + { 265, 0 }, /* (174) sclp ::= */ + { 251, -4 }, /* (175) selcollist ::= sclp distinct expr as */ + { 251, -2 }, /* (176) selcollist ::= sclp STAR */ + { 268, -2 }, /* (177) as ::= AS ids */ + { 268, -1 }, /* (178) as ::= ids */ + { 268, 0 }, /* (179) as ::= */ + { 266, -1 }, /* (180) distinct ::= DISTINCT */ + { 266, 0 }, /* (181) distinct ::= */ + { 252, -2 }, /* (182) from ::= FROM tablelist */ + { 252, -2 }, /* (183) from ::= FROM sub */ + { 270, -3 }, /* (184) sub ::= LP union RP */ + { 270, -4 }, /* (185) sub ::= LP union RP ids */ + { 270, -6 }, /* (186) sub ::= sub COMMA LP union RP ids */ + { 269, -2 }, /* (187) tablelist ::= ids cpxName */ + { 269, -3 }, /* (188) tablelist ::= ids cpxName ids */ + { 269, -4 }, /* (189) tablelist ::= tablelist COMMA ids cpxName */ + { 269, -5 }, /* (190) tablelist ::= tablelist COMMA ids cpxName ids */ + { 271, -1 }, /* (191) tmvar ::= VARIABLE */ + { 254, -4 }, /* (192) interval_option ::= intervalKey LP tmvar RP */ + { 254, -6 }, /* (193) interval_option ::= intervalKey LP tmvar COMMA tmvar RP */ + { 254, 0 }, /* (194) interval_option ::= */ + { 272, -1 }, /* (195) intervalKey ::= INTERVAL */ + { 272, -1 }, /* (196) intervalKey ::= EVERY */ + { 256, 0 }, /* (197) session_option ::= */ + { 256, -7 }, /* (198) session_option ::= SESSION LP ids cpxName COMMA tmvar RP */ + { 257, 0 }, /* (199) windowstate_option ::= */ + { 257, -4 }, /* (200) windowstate_option ::= STATE_WINDOW LP ids RP */ + { 258, 0 }, /* (201) fill_opt ::= */ + { 258, -6 }, /* (202) fill_opt ::= FILL LP ID COMMA tagitemlist RP */ + { 258, -4 }, /* (203) fill_opt ::= FILL LP ID RP */ + { 255, -4 }, /* (204) sliding_opt ::= SLIDING LP tmvar RP */ + { 255, 0 }, /* (205) sliding_opt ::= */ + { 261, 0 }, /* (206) orderby_opt ::= */ + { 261, -3 }, /* (207) orderby_opt ::= ORDER BY sortlist */ + { 273, -4 }, /* (208) sortlist ::= sortlist COMMA item sortorder */ + { 273, -2 }, /* (209) sortlist ::= item sortorder */ + { 275, -2 }, /* (210) item ::= ids cpxName */ + { 276, -1 }, /* (211) sortorder ::= ASC */ + { 276, -1 }, /* (212) sortorder ::= DESC */ + { 276, 0 }, /* (213) sortorder ::= */ + { 259, 0 }, /* (214) groupby_opt ::= */ + { 259, -3 }, /* (215) groupby_opt ::= GROUP BY grouplist */ + { 277, -3 }, /* (216) grouplist ::= grouplist COMMA item */ + { 277, -1 }, /* (217) grouplist ::= item */ + { 260, 0 }, /* (218) having_opt ::= */ + { 260, -2 }, /* (219) having_opt ::= HAVING expr */ + { 263, 0 }, /* (220) limit_opt ::= */ + { 263, -2 }, /* (221) limit_opt ::= LIMIT signed */ + { 263, -4 }, /* (222) limit_opt ::= LIMIT signed OFFSET signed */ + { 263, -4 }, /* (223) limit_opt ::= LIMIT signed COMMA signed */ + { 262, 0 }, /* (224) slimit_opt ::= */ + { 262, -2 }, /* (225) slimit_opt ::= SLIMIT signed */ + { 262, -4 }, /* (226) slimit_opt ::= SLIMIT signed SOFFSET signed */ + { 262, -4 }, /* (227) slimit_opt ::= SLIMIT signed COMMA signed */ + { 253, 0 }, /* (228) where_opt ::= */ + { 253, -2 }, /* (229) where_opt ::= WHERE expr */ + { 267, -3 }, /* (230) expr ::= LP expr RP */ + { 267, -1 }, /* (231) expr ::= ID */ + { 267, -3 }, /* (232) expr ::= ID DOT ID */ + { 267, -3 }, /* (233) expr ::= ID DOT STAR */ + { 267, -1 }, /* (234) expr ::= INTEGER */ + { 267, -2 }, /* (235) expr ::= MINUS INTEGER */ + { 267, -2 }, /* (236) expr ::= PLUS INTEGER */ + { 267, -1 }, /* (237) expr ::= FLOAT */ + { 267, -2 }, /* (238) expr ::= MINUS FLOAT */ + { 267, -2 }, /* (239) expr ::= PLUS FLOAT */ + { 267, -1 }, /* (240) expr ::= STRING */ + { 267, -1 }, /* (241) expr ::= NOW */ + { 267, -1 }, /* (242) expr ::= VARIABLE */ + { 267, -2 }, /* (243) expr ::= PLUS VARIABLE */ + { 267, -2 }, /* (244) expr ::= MINUS VARIABLE */ + { 267, -1 }, /* (245) expr ::= BOOL */ + { 267, -1 }, /* (246) expr ::= NULL */ + { 267, -4 }, /* (247) expr ::= ID LP exprlist RP */ + { 267, -4 }, /* (248) expr ::= ID LP STAR RP */ + { 267, -3 }, /* (249) expr ::= expr IS NULL */ + { 267, -4 }, /* (250) expr ::= expr IS NOT NULL */ + { 267, -3 }, /* (251) expr ::= expr LT expr */ + { 267, -3 }, /* (252) expr ::= expr GT expr */ + { 267, -3 }, /* (253) expr ::= expr LE expr */ + { 267, -3 }, /* (254) expr ::= expr GE expr */ + { 267, -3 }, /* (255) expr ::= expr NE expr */ + { 267, -3 }, /* (256) expr ::= expr EQ expr */ + { 267, -5 }, /* (257) expr ::= expr BETWEEN expr AND expr */ + { 267, -3 }, /* (258) expr ::= expr AND expr */ + { 267, -3 }, /* (259) expr ::= expr OR expr */ + { 267, -3 }, /* (260) expr ::= expr PLUS expr */ + { 267, -3 }, /* (261) expr ::= expr MINUS expr */ + { 267, -3 }, /* (262) expr ::= expr STAR expr */ + { 267, -3 }, /* (263) expr ::= expr SLASH expr */ + { 267, -3 }, /* (264) expr ::= expr REM expr */ + { 267, -3 }, /* (265) expr ::= expr LIKE expr */ + { 267, -3 }, /* (266) expr ::= expr MATCH expr */ + { 267, -3 }, /* (267) expr ::= expr NMATCH expr */ + { 267, -5 }, /* (268) expr ::= expr IN LP exprlist RP */ + { 207, -3 }, /* (269) exprlist ::= exprlist COMMA expritem */ + { 207, -1 }, /* (270) exprlist ::= expritem */ + { 278, -1 }, /* (271) expritem ::= expr */ + { 278, 0 }, /* (272) expritem ::= */ + { 199, -3 }, /* (273) cmd ::= RESET QUERY CACHE */ + { 199, -3 }, /* (274) cmd ::= SYNCDB ids REPLICA */ + { 199, -7 }, /* (275) cmd ::= ALTER TABLE ids cpxName ADD COLUMN columnlist */ + { 199, -7 }, /* (276) cmd ::= ALTER TABLE ids cpxName DROP COLUMN ids */ + { 199, -7 }, /* (277) cmd ::= ALTER TABLE ids cpxName MODIFY COLUMN columnlist */ + { 199, -7 }, /* (278) cmd ::= ALTER TABLE ids cpxName ADD TAG columnlist */ + { 199, -7 }, /* (279) cmd ::= ALTER TABLE ids cpxName DROP TAG ids */ + { 199, -8 }, /* (280) cmd ::= ALTER TABLE ids cpxName CHANGE TAG ids ids */ + { 199, -9 }, /* (281) cmd ::= ALTER TABLE ids cpxName SET TAG ids EQ tagitem */ + { 199, -7 }, /* (282) cmd ::= ALTER TABLE ids cpxName MODIFY TAG columnlist */ + { 199, -7 }, /* (283) cmd ::= ALTER STABLE ids cpxName ADD COLUMN columnlist */ + { 199, -7 }, /* (284) cmd ::= ALTER STABLE ids cpxName DROP COLUMN ids */ + { 199, -7 }, /* (285) cmd ::= ALTER STABLE ids cpxName MODIFY COLUMN columnlist */ + { 199, -7 }, /* (286) cmd ::= ALTER STABLE ids cpxName ADD TAG columnlist */ + { 199, -7 }, /* (287) cmd ::= ALTER STABLE ids cpxName DROP TAG ids */ + { 199, -8 }, /* (288) cmd ::= ALTER STABLE ids cpxName CHANGE TAG ids ids */ + { 199, -9 }, /* (289) cmd ::= ALTER STABLE ids cpxName SET TAG ids EQ tagitem */ + { 199, -7 }, /* (290) cmd ::= ALTER STABLE ids cpxName MODIFY TAG columnlist */ + { 199, -3 }, /* (291) cmd ::= KILL CONNECTION INTEGER */ + { 199, -5 }, /* (292) cmd ::= KILL STREAM INTEGER COLON INTEGER */ + { 199, -5 }, /* (293) cmd ::= KILL QUERY INTEGER COLON INTEGER */ +}; + +static void yy_accept(yyParser*); /* Forward Declaration */ + +/* +** Perform a reduce action and the shift that must immediately +** follow the reduce. +** +** The yyLookahead and yyLookaheadToken parameters provide reduce actions +** access to the lookahead token (if any). The yyLookahead will be YYNOCODE +** if the lookahead token has already been consumed. As this procedure is +** only called from one place, optimizing compilers will in-line it, which +** means that the extra parameters have no performance impact. +*/ +static void yy_reduce( + yyParser *yypParser, /* The parser */ + unsigned int yyruleno, /* Number of the rule by which to reduce */ + int yyLookahead, /* Lookahead token, or YYNOCODE if none */ + ParseTOKENTYPE yyLookaheadToken /* Value of the lookahead token */ +){ + int yygoto; /* The next state */ + int yyact; /* The next action */ + yyStackEntry *yymsp; /* The top of the parser's stack */ + int yysize; /* Amount to pop the stack */ + ParseARG_FETCH; + (void)yyLookahead; + (void)yyLookaheadToken; + yymsp = yypParser->yytos; +#ifndef NDEBUG + if( yyTraceFILE && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){ + yysize = yyRuleInfo[yyruleno].nrhs; + if( yysize ){ + fprintf(yyTraceFILE, "%sReduce %d [%s], go to state %d.\n", + yyTracePrompt, + yyruleno, yyRuleName[yyruleno], yymsp[yysize].stateno); + }else{ + fprintf(yyTraceFILE, "%sReduce %d [%s].\n", + yyTracePrompt, yyruleno, yyRuleName[yyruleno]); + } + } +#endif /* NDEBUG */ + + /* Check that the stack is large enough to grow by a single entry + ** if the RHS of the rule is empty. This ensures that there is room + ** enough on the stack to push the LHS value */ + if( yyRuleInfo[yyruleno].nrhs==0 ){ +#ifdef YYTRACKMAXSTACKDEPTH + if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){ + yypParser->yyhwm++; + assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack)); + } +#endif +#if YYSTACKDEPTH>0 + if( yypParser->yytos>=yypParser->yystackEnd ){ + yyStackOverflow(yypParser); + return; + } +#else + if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){ + if( yyGrowStack(yypParser) ){ + yyStackOverflow(yypParser); + return; + } + yymsp = yypParser->yytos; + } +#endif + } + + switch( yyruleno ){ + /* Beginning here are the reduction cases. A typical example + ** follows: + ** case 0: + ** #line + ** { ... } // User supplied code + ** #line + ** break; + */ +/********** Begin reduce actions **********************************************/ + YYMINORTYPE yylhsminor; + case 0: /* program ::= cmd */ + case 139: /* cmd ::= CREATE TABLE create_table_args */ yytestcase(yyruleno==139); + case 140: /* cmd ::= CREATE TABLE create_stable_args */ yytestcase(yyruleno==140); + case 141: /* cmd ::= CREATE STABLE create_stable_args */ yytestcase(yyruleno==141); +{} + break; + case 1: /* cmd ::= SHOW DATABASES */ +{ setShowOptions(pInfo, TSDB_MGMT_TABLE_DB, 0, 0);} + break; + case 2: /* cmd ::= SHOW TOPICS */ +{ setShowOptions(pInfo, TSDB_MGMT_TABLE_TP, 0, 0);} + break; + case 3: /* cmd ::= SHOW FUNCTIONS */ +{ setShowOptions(pInfo, TSDB_MGMT_TABLE_FUNCTION, 0, 0);} + break; + case 4: /* cmd ::= SHOW MNODES */ +{ setShowOptions(pInfo, TSDB_MGMT_TABLE_MNODE, 0, 0);} + break; + case 5: /* cmd ::= SHOW DNODES */ +{ setShowOptions(pInfo, TSDB_MGMT_TABLE_DNODE, 0, 0);} + break; + case 6: /* cmd ::= SHOW ACCOUNTS */ +{ setShowOptions(pInfo, TSDB_MGMT_TABLE_ACCT, 0, 0);} + break; + case 7: /* cmd ::= SHOW USERS */ +{ setShowOptions(pInfo, TSDB_MGMT_TABLE_USER, 0, 0);} + break; + case 8: /* cmd ::= SHOW MODULES */ +{ setShowOptions(pInfo, TSDB_MGMT_TABLE_MODULE, 0, 0); } + break; + case 9: /* cmd ::= SHOW QUERIES */ +{ setShowOptions(pInfo, TSDB_MGMT_TABLE_QUERIES, 0, 0); } + break; + case 10: /* cmd ::= SHOW CONNECTIONS */ +{ setShowOptions(pInfo, TSDB_MGMT_TABLE_CONNS, 0, 0);} + break; + case 11: /* cmd ::= SHOW STREAMS */ +{ setShowOptions(pInfo, TSDB_MGMT_TABLE_STREAMS, 0, 0); } + break; + case 12: /* cmd ::= SHOW VARIABLES */ +{ setShowOptions(pInfo, TSDB_MGMT_TABLE_VARIABLES, 0, 0); } + break; + case 13: /* cmd ::= SHOW SCORES */ +{ setShowOptions(pInfo, TSDB_MGMT_TABLE_SCORES, 0, 0); } + break; + case 14: /* cmd ::= SHOW GRANTS */ +{ setShowOptions(pInfo, TSDB_MGMT_TABLE_GRANTS, 0, 0); } + break; + case 15: /* cmd ::= SHOW VNODES */ +{ setShowOptions(pInfo, TSDB_MGMT_TABLE_VNODES, 0, 0); } + break; + case 16: /* cmd ::= SHOW VNODES ids */ +{ setShowOptions(pInfo, TSDB_MGMT_TABLE_VNODES, &yymsp[0].minor.yy0, 0); } + break; + case 17: /* dbPrefix ::= */ +{yymsp[1].minor.yy0.n = 0; yymsp[1].minor.yy0.type = 0;} + break; + case 18: /* dbPrefix ::= ids DOT */ +{yylhsminor.yy0 = yymsp[-1].minor.yy0; } + yymsp[-1].minor.yy0 = yylhsminor.yy0; + break; + case 19: /* cpxName ::= */ +{yymsp[1].minor.yy0.n = 0; } + break; + case 20: /* cpxName ::= DOT ids */ +{yymsp[-1].minor.yy0 = yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n += 1; } + break; + case 21: /* cmd ::= SHOW CREATE TABLE ids cpxName */ +{ + yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; + setDCLSqlElems(pInfo, TSDB_SQL_SHOW_CREATE_TABLE, 1, &yymsp[-1].minor.yy0); +} + break; + case 22: /* cmd ::= SHOW CREATE STABLE ids cpxName */ +{ + yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; + setDCLSqlElems(pInfo, TSDB_SQL_SHOW_CREATE_STABLE, 1, &yymsp[-1].minor.yy0); +} + break; + case 23: /* cmd ::= SHOW CREATE DATABASE ids */ +{ + setDCLSqlElems(pInfo, TSDB_SQL_SHOW_CREATE_DATABASE, 1, &yymsp[0].minor.yy0); +} + break; + case 24: /* cmd ::= SHOW dbPrefix TABLES */ +{ + setShowOptions(pInfo, TSDB_MGMT_TABLE_TABLE, &yymsp[-1].minor.yy0, 0); +} + break; + case 25: /* cmd ::= SHOW dbPrefix TABLES LIKE ids */ +{ + setShowOptions(pInfo, TSDB_MGMT_TABLE_TABLE, &yymsp[-3].minor.yy0, &yymsp[0].minor.yy0); +} + break; + case 26: /* cmd ::= SHOW dbPrefix STABLES */ +{ + setShowOptions(pInfo, TSDB_MGMT_TABLE_METRIC, &yymsp[-1].minor.yy0, 0); +} + break; + case 27: /* cmd ::= SHOW dbPrefix STABLES LIKE ids */ +{ + SToken token; + tSetDbName(&token, &yymsp[-3].minor.yy0); + setShowOptions(pInfo, TSDB_MGMT_TABLE_METRIC, &token, &yymsp[0].minor.yy0); +} + break; + case 28: /* cmd ::= SHOW dbPrefix VGROUPS */ +{ + SToken token; + tSetDbName(&token, &yymsp[-1].minor.yy0); + setShowOptions(pInfo, TSDB_MGMT_TABLE_VGROUP, &token, 0); +} + break; + case 29: /* cmd ::= SHOW dbPrefix VGROUPS ids */ +{ + SToken token; + tSetDbName(&token, &yymsp[-2].minor.yy0); + setShowOptions(pInfo, TSDB_MGMT_TABLE_VGROUP, &token, &yymsp[0].minor.yy0); +} + break; + case 30: /* cmd ::= DROP TABLE ifexists ids cpxName */ +{ + yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; + setDropDbTableInfo(pInfo, TSDB_SQL_DROP_TABLE, &yymsp[-1].minor.yy0, &yymsp[-2].minor.yy0, -1, -1); +} + break; + case 31: /* cmd ::= DROP STABLE ifexists ids cpxName */ +{ + yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; + setDropDbTableInfo(pInfo, TSDB_SQL_DROP_TABLE, &yymsp[-1].minor.yy0, &yymsp[-2].minor.yy0, -1, TSDB_SUPER_TABLE); +} + break; + case 32: /* cmd ::= DROP DATABASE ifexists ids */ +{ setDropDbTableInfo(pInfo, TSDB_SQL_DROP_DB, &yymsp[0].minor.yy0, &yymsp[-1].minor.yy0, TSDB_DB_TYPE_DEFAULT, -1); } + break; + case 33: /* cmd ::= DROP TOPIC ifexists ids */ +{ setDropDbTableInfo(pInfo, TSDB_SQL_DROP_DB, &yymsp[0].minor.yy0, &yymsp[-1].minor.yy0, TSDB_DB_TYPE_TOPIC, -1); } + break; + case 34: /* cmd ::= DROP FUNCTION ids */ +{ setDropFuncInfo(pInfo, TSDB_SQL_DROP_FUNCTION, &yymsp[0].minor.yy0); } + break; + case 35: /* cmd ::= DROP DNODE ids */ +{ setDCLSqlElems(pInfo, TSDB_SQL_DROP_DNODE, 1, &yymsp[0].minor.yy0); } + break; + case 36: /* cmd ::= DROP USER ids */ +{ setDCLSqlElems(pInfo, TSDB_SQL_DROP_USER, 1, &yymsp[0].minor.yy0); } + break; + case 37: /* cmd ::= DROP ACCOUNT ids */ +{ setDCLSqlElems(pInfo, TSDB_SQL_DROP_ACCT, 1, &yymsp[0].minor.yy0); } + break; + case 38: /* cmd ::= USE ids */ +{ setDCLSqlElems(pInfo, TSDB_SQL_USE_DB, 1, &yymsp[0].minor.yy0);} + break; + case 39: /* cmd ::= DESCRIBE ids cpxName */ + case 40: /* cmd ::= DESC ids cpxName */ yytestcase(yyruleno==40); +{ + yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; + setDCLSqlElems(pInfo, TSDB_SQL_DESCRIBE_TABLE, 1, &yymsp[-1].minor.yy0); +} + break; + case 41: /* cmd ::= ALTER USER ids PASS ids */ +{ setAlterUserSql(pInfo, TSDB_ALTER_USER_PASSWD, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, NULL); } + break; + case 42: /* cmd ::= ALTER USER ids PRIVILEGE ids */ +{ setAlterUserSql(pInfo, TSDB_ALTER_USER_PRIVILEGES, &yymsp[-2].minor.yy0, NULL, &yymsp[0].minor.yy0);} + break; + case 43: /* cmd ::= ALTER DNODE ids ids */ +{ setDCLSqlElems(pInfo, TSDB_SQL_CFG_DNODE, 2, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0); } + break; + case 44: /* cmd ::= ALTER DNODE ids ids ids */ +{ setDCLSqlElems(pInfo, TSDB_SQL_CFG_DNODE, 3, &yymsp[-2].minor.yy0, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0); } + break; + case 45: /* cmd ::= ALTER LOCAL ids */ +{ setDCLSqlElems(pInfo, TSDB_SQL_CFG_LOCAL, 1, &yymsp[0].minor.yy0); } + break; + case 46: /* cmd ::= ALTER LOCAL ids ids */ +{ setDCLSqlElems(pInfo, TSDB_SQL_CFG_LOCAL, 2, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0); } + break; + case 47: /* cmd ::= ALTER DATABASE ids alter_db_optr */ + case 48: /* cmd ::= ALTER TOPIC ids alter_topic_optr */ yytestcase(yyruleno==48); +{ SToken t = {0}; setCreateDbInfo(pInfo, TSDB_SQL_ALTER_DB, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy256, &t);} + break; + case 49: /* cmd ::= ALTER ACCOUNT ids acct_optr */ +{ setCreateAcctSql(pInfo, TSDB_SQL_ALTER_ACCT, &yymsp[-1].minor.yy0, NULL, &yymsp[0].minor.yy277);} + break; + case 50: /* cmd ::= ALTER ACCOUNT ids PASS ids acct_optr */ +{ setCreateAcctSql(pInfo, TSDB_SQL_ALTER_ACCT, &yymsp[-3].minor.yy0, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy277);} + break; + case 51: /* cmd ::= COMPACT VNODES IN LP exprlist RP */ +{ setCompactVnodeSql(pInfo, TSDB_SQL_COMPACT_VNODE, yymsp[-1].minor.yy135);} + break; + case 52: /* ids ::= ID */ + case 53: /* ids ::= STRING */ yytestcase(yyruleno==53); +{yylhsminor.yy0 = yymsp[0].minor.yy0; } + yymsp[0].minor.yy0 = yylhsminor.yy0; + break; + case 54: /* ifexists ::= IF EXISTS */ +{ yymsp[-1].minor.yy0.n = 1;} + break; + case 55: /* ifexists ::= */ + case 57: /* ifnotexists ::= */ yytestcase(yyruleno==57); + case 181: /* distinct ::= */ yytestcase(yyruleno==181); +{ yymsp[1].minor.yy0.n = 0;} + break; + case 56: /* ifnotexists ::= IF NOT EXISTS */ +{ yymsp[-2].minor.yy0.n = 1;} + break; + case 58: /* cmd ::= CREATE DNODE ids */ +{ setDCLSqlElems(pInfo, TSDB_SQL_CREATE_DNODE, 1, &yymsp[0].minor.yy0);} + break; + case 59: /* cmd ::= CREATE ACCOUNT ids PASS ids acct_optr */ +{ setCreateAcctSql(pInfo, TSDB_SQL_CREATE_ACCT, &yymsp[-3].minor.yy0, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy277);} + break; + case 60: /* cmd ::= CREATE DATABASE ifnotexists ids db_optr */ + case 61: /* cmd ::= CREATE TOPIC ifnotexists ids topic_optr */ yytestcase(yyruleno==61); +{ setCreateDbInfo(pInfo, TSDB_SQL_CREATE_DB, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy256, &yymsp[-2].minor.yy0);} + break; + case 62: /* cmd ::= CREATE FUNCTION ids AS ids OUTPUTTYPE typename bufsize */ +{ setCreateFuncInfo(pInfo, TSDB_SQL_CREATE_FUNCTION, &yymsp[-5].minor.yy0, &yymsp[-3].minor.yy0, &yymsp[-1].minor.yy181, &yymsp[0].minor.yy0, 1);} + break; + case 63: /* cmd ::= CREATE AGGREGATE FUNCTION ids AS ids OUTPUTTYPE typename bufsize */ +{ setCreateFuncInfo(pInfo, TSDB_SQL_CREATE_FUNCTION, &yymsp[-5].minor.yy0, &yymsp[-3].minor.yy0, &yymsp[-1].minor.yy181, &yymsp[0].minor.yy0, 2);} + break; + case 64: /* cmd ::= CREATE USER ids PASS ids */ +{ setCreateUserSql(pInfo, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);} + break; + case 65: /* bufsize ::= */ + case 67: /* pps ::= */ yytestcase(yyruleno==67); + case 69: /* tseries ::= */ yytestcase(yyruleno==69); + case 71: /* dbs ::= */ yytestcase(yyruleno==71); + case 73: /* streams ::= */ yytestcase(yyruleno==73); + case 75: /* storage ::= */ yytestcase(yyruleno==75); + case 77: /* qtime ::= */ yytestcase(yyruleno==77); + case 79: /* users ::= */ yytestcase(yyruleno==79); + case 81: /* conns ::= */ yytestcase(yyruleno==81); + case 83: /* state ::= */ yytestcase(yyruleno==83); +{ yymsp[1].minor.yy0.n = 0; } + break; + case 66: /* bufsize ::= BUFSIZE INTEGER */ + case 68: /* pps ::= PPS INTEGER */ yytestcase(yyruleno==68); + case 70: /* tseries ::= TSERIES INTEGER */ yytestcase(yyruleno==70); + case 72: /* dbs ::= DBS INTEGER */ yytestcase(yyruleno==72); + case 74: /* streams ::= STREAMS INTEGER */ yytestcase(yyruleno==74); + case 76: /* storage ::= STORAGE INTEGER */ yytestcase(yyruleno==76); + case 78: /* qtime ::= QTIME INTEGER */ yytestcase(yyruleno==78); + case 80: /* users ::= USERS INTEGER */ yytestcase(yyruleno==80); + case 82: /* conns ::= CONNS INTEGER */ yytestcase(yyruleno==82); + case 84: /* state ::= STATE ids */ yytestcase(yyruleno==84); +{ yymsp[-1].minor.yy0 = yymsp[0].minor.yy0; } + break; + case 85: /* acct_optr ::= pps tseries storage streams qtime dbs users conns state */ +{ + yylhsminor.yy277.maxUsers = (yymsp[-2].minor.yy0.n>0)?atoi(yymsp[-2].minor.yy0.z):-1; + yylhsminor.yy277.maxDbs = (yymsp[-3].minor.yy0.n>0)?atoi(yymsp[-3].minor.yy0.z):-1; + yylhsminor.yy277.maxTimeSeries = (yymsp[-7].minor.yy0.n>0)?atoi(yymsp[-7].minor.yy0.z):-1; + yylhsminor.yy277.maxStreams = (yymsp[-5].minor.yy0.n>0)?atoi(yymsp[-5].minor.yy0.z):-1; + yylhsminor.yy277.maxPointsPerSecond = (yymsp[-8].minor.yy0.n>0)?atoi(yymsp[-8].minor.yy0.z):-1; + yylhsminor.yy277.maxStorage = (yymsp[-6].minor.yy0.n>0)?strtoll(yymsp[-6].minor.yy0.z, NULL, 10):-1; + yylhsminor.yy277.maxQueryTime = (yymsp[-4].minor.yy0.n>0)?strtoll(yymsp[-4].minor.yy0.z, NULL, 10):-1; + yylhsminor.yy277.maxConnections = (yymsp[-1].minor.yy0.n>0)?atoi(yymsp[-1].minor.yy0.z):-1; + yylhsminor.yy277.stat = yymsp[0].minor.yy0; +} + yymsp[-8].minor.yy277 = yylhsminor.yy277; + break; + case 86: /* intitemlist ::= intitemlist COMMA intitem */ + case 155: /* tagitemlist ::= tagitemlist COMMA tagitem */ yytestcase(yyruleno==155); +{ yylhsminor.yy135 = tListItemAppend(yymsp[-2].minor.yy135, &yymsp[0].minor.yy191, -1); } + yymsp[-2].minor.yy135 = yylhsminor.yy135; + break; + case 87: /* intitemlist ::= intitem */ + case 156: /* tagitemlist ::= tagitem */ yytestcase(yyruleno==156); +{ yylhsminor.yy135 = tListItemAppend(NULL, &yymsp[0].minor.yy191, -1); } + yymsp[0].minor.yy135 = yylhsminor.yy135; + break; + case 88: /* intitem ::= INTEGER */ + case 157: /* tagitem ::= INTEGER */ yytestcase(yyruleno==157); + case 158: /* tagitem ::= FLOAT */ yytestcase(yyruleno==158); + case 159: /* tagitem ::= STRING */ yytestcase(yyruleno==159); + case 160: /* tagitem ::= BOOL */ yytestcase(yyruleno==160); +{ toTSDBType(yymsp[0].minor.yy0.type); taosVariantCreate(&yylhsminor.yy191, yymsp[0].minor.yy0.z, yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.type); } + yymsp[0].minor.yy191 = yylhsminor.yy191; + break; + case 89: /* keep ::= KEEP intitemlist */ +{ yymsp[-1].minor.yy135 = yymsp[0].minor.yy135; } + break; + case 90: /* cache ::= CACHE INTEGER */ + case 91: /* replica ::= REPLICA INTEGER */ yytestcase(yyruleno==91); + case 92: /* quorum ::= QUORUM INTEGER */ yytestcase(yyruleno==92); + case 93: /* days ::= DAYS INTEGER */ yytestcase(yyruleno==93); + case 94: /* minrows ::= MINROWS INTEGER */ yytestcase(yyruleno==94); + case 95: /* maxrows ::= MAXROWS INTEGER */ yytestcase(yyruleno==95); + case 96: /* blocks ::= BLOCKS INTEGER */ yytestcase(yyruleno==96); + case 97: /* ctime ::= CTIME INTEGER */ yytestcase(yyruleno==97); + case 98: /* wal ::= WAL INTEGER */ yytestcase(yyruleno==98); + case 99: /* fsync ::= FSYNC INTEGER */ yytestcase(yyruleno==99); + case 100: /* comp ::= COMP INTEGER */ yytestcase(yyruleno==100); + case 101: /* prec ::= PRECISION STRING */ yytestcase(yyruleno==101); + case 102: /* update ::= UPDATE INTEGER */ yytestcase(yyruleno==102); + case 103: /* cachelast ::= CACHELAST INTEGER */ yytestcase(yyruleno==103); + case 104: /* partitions ::= PARTITIONS INTEGER */ yytestcase(yyruleno==104); +{ yymsp[-1].minor.yy0 = yymsp[0].minor.yy0; } + break; + case 105: /* db_optr ::= */ +{setDefaultCreateDbOption(&yymsp[1].minor.yy256); yymsp[1].minor.yy256.dbType = TSDB_DB_TYPE_DEFAULT;} + break; + case 106: /* db_optr ::= db_optr cache */ +{ yylhsminor.yy256 = yymsp[-1].minor.yy256; yylhsminor.yy256.cacheBlockSize = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy256 = yylhsminor.yy256; + break; + case 107: /* db_optr ::= db_optr replica */ + case 124: /* alter_db_optr ::= alter_db_optr replica */ yytestcase(yyruleno==124); +{ yylhsminor.yy256 = yymsp[-1].minor.yy256; yylhsminor.yy256.replica = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy256 = yylhsminor.yy256; + break; + case 108: /* db_optr ::= db_optr quorum */ + case 125: /* alter_db_optr ::= alter_db_optr quorum */ yytestcase(yyruleno==125); +{ yylhsminor.yy256 = yymsp[-1].minor.yy256; yylhsminor.yy256.quorum = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy256 = yylhsminor.yy256; + break; + case 109: /* db_optr ::= db_optr days */ +{ yylhsminor.yy256 = yymsp[-1].minor.yy256; yylhsminor.yy256.daysPerFile = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy256 = yylhsminor.yy256; + break; + case 110: /* db_optr ::= db_optr minrows */ +{ yylhsminor.yy256 = yymsp[-1].minor.yy256; yylhsminor.yy256.minRowsPerBlock = strtod(yymsp[0].minor.yy0.z, NULL); } + yymsp[-1].minor.yy256 = yylhsminor.yy256; + break; + case 111: /* db_optr ::= db_optr maxrows */ +{ yylhsminor.yy256 = yymsp[-1].minor.yy256; yylhsminor.yy256.maxRowsPerBlock = strtod(yymsp[0].minor.yy0.z, NULL); } + yymsp[-1].minor.yy256 = yylhsminor.yy256; + break; + case 112: /* db_optr ::= db_optr blocks */ + case 127: /* alter_db_optr ::= alter_db_optr blocks */ yytestcase(yyruleno==127); +{ yylhsminor.yy256 = yymsp[-1].minor.yy256; yylhsminor.yy256.numOfBlocks = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy256 = yylhsminor.yy256; + break; + case 113: /* db_optr ::= db_optr ctime */ +{ yylhsminor.yy256 = yymsp[-1].minor.yy256; yylhsminor.yy256.commitTime = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy256 = yylhsminor.yy256; + break; + case 114: /* db_optr ::= db_optr wal */ +{ yylhsminor.yy256 = yymsp[-1].minor.yy256; yylhsminor.yy256.walLevel = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy256 = yylhsminor.yy256; + break; + case 115: /* db_optr ::= db_optr fsync */ +{ yylhsminor.yy256 = yymsp[-1].minor.yy256; yylhsminor.yy256.fsyncPeriod = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy256 = yylhsminor.yy256; + break; + case 116: /* db_optr ::= db_optr comp */ + case 128: /* alter_db_optr ::= alter_db_optr comp */ yytestcase(yyruleno==128); +{ yylhsminor.yy256 = yymsp[-1].minor.yy256; yylhsminor.yy256.compressionLevel = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy256 = yylhsminor.yy256; + break; + case 117: /* db_optr ::= db_optr prec */ +{ yylhsminor.yy256 = yymsp[-1].minor.yy256; yylhsminor.yy256.precision = yymsp[0].minor.yy0; } + yymsp[-1].minor.yy256 = yylhsminor.yy256; + break; + case 118: /* db_optr ::= db_optr keep */ + case 126: /* alter_db_optr ::= alter_db_optr keep */ yytestcase(yyruleno==126); +{ yylhsminor.yy256 = yymsp[-1].minor.yy256; yylhsminor.yy256.keep = yymsp[0].minor.yy135; } + yymsp[-1].minor.yy256 = yylhsminor.yy256; + break; + case 119: /* db_optr ::= db_optr update */ + case 129: /* alter_db_optr ::= alter_db_optr update */ yytestcase(yyruleno==129); +{ yylhsminor.yy256 = yymsp[-1].minor.yy256; yylhsminor.yy256.update = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy256 = yylhsminor.yy256; + break; + case 120: /* db_optr ::= db_optr cachelast */ + case 130: /* alter_db_optr ::= alter_db_optr cachelast */ yytestcase(yyruleno==130); +{ yylhsminor.yy256 = yymsp[-1].minor.yy256; yylhsminor.yy256.cachelast = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy256 = yylhsminor.yy256; + break; + case 121: /* topic_optr ::= db_optr */ + case 131: /* alter_topic_optr ::= alter_db_optr */ yytestcase(yyruleno==131); +{ yylhsminor.yy256 = yymsp[0].minor.yy256; yylhsminor.yy256.dbType = TSDB_DB_TYPE_TOPIC; } + yymsp[0].minor.yy256 = yylhsminor.yy256; + break; + case 122: /* topic_optr ::= topic_optr partitions */ + case 132: /* alter_topic_optr ::= alter_topic_optr partitions */ yytestcase(yyruleno==132); +{ yylhsminor.yy256 = yymsp[-1].minor.yy256; yylhsminor.yy256.partitions = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[-1].minor.yy256 = yylhsminor.yy256; + break; + case 123: /* alter_db_optr ::= */ +{ setDefaultCreateDbOption(&yymsp[1].minor.yy256); yymsp[1].minor.yy256.dbType = TSDB_DB_TYPE_DEFAULT;} + break; + case 133: /* typename ::= ids */ +{ + yymsp[0].minor.yy0.type = 0; + tSetColumnType (&yylhsminor.yy181, &yymsp[0].minor.yy0); +} + yymsp[0].minor.yy181 = yylhsminor.yy181; + break; + case 134: /* typename ::= ids LP signed RP */ +{ + if (yymsp[-1].minor.yy531 <= 0) { + yymsp[-3].minor.yy0.type = 0; + tSetColumnType(&yylhsminor.yy181, &yymsp[-3].minor.yy0); + } else { + yymsp[-3].minor.yy0.type = -yymsp[-1].minor.yy531; // negative value of name length + tSetColumnType(&yylhsminor.yy181, &yymsp[-3].minor.yy0); + } +} + yymsp[-3].minor.yy181 = yylhsminor.yy181; + break; + case 135: /* typename ::= ids UNSIGNED */ +{ + yymsp[-1].minor.yy0.type = 0; + yymsp[-1].minor.yy0.n = ((yymsp[0].minor.yy0.z + yymsp[0].minor.yy0.n) - yymsp[-1].minor.yy0.z); + tSetColumnType (&yylhsminor.yy181, &yymsp[-1].minor.yy0); +} + yymsp[-1].minor.yy181 = yylhsminor.yy181; + break; + case 136: /* signed ::= INTEGER */ +{ yylhsminor.yy531 = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + yymsp[0].minor.yy531 = yylhsminor.yy531; + break; + case 137: /* signed ::= PLUS INTEGER */ +{ yymsp[-1].minor.yy531 = strtol(yymsp[0].minor.yy0.z, NULL, 10); } + break; + case 138: /* signed ::= MINUS INTEGER */ +{ yymsp[-1].minor.yy531 = -strtol(yymsp[0].minor.yy0.z, NULL, 10);} + break; + case 142: /* cmd ::= CREATE TABLE create_table_list */ +{ pInfo->type = TSDB_SQL_CREATE_TABLE; pInfo->pCreateTableInfo = yymsp[0].minor.yy110;} + break; + case 143: /* create_table_list ::= create_from_stable */ +{ + SCreateTableSql* pCreateTable = calloc(1, sizeof(SCreateTableSql)); + pCreateTable->childTableInfo = taosArrayInit(4, sizeof(SCreatedTableInfo)); + + taosArrayPush(pCreateTable->childTableInfo, &yymsp[0].minor.yy78); + pCreateTable->type = TSQL_CREATE_CTABLE; + yylhsminor.yy110 = pCreateTable; +} + yymsp[0].minor.yy110 = yylhsminor.yy110; + break; + case 144: /* create_table_list ::= create_table_list create_from_stable */ +{ + taosArrayPush(yymsp[-1].minor.yy110->childTableInfo, &yymsp[0].minor.yy78); + yylhsminor.yy110 = yymsp[-1].minor.yy110; +} + yymsp[-1].minor.yy110 = yylhsminor.yy110; + break; + case 145: /* create_table_args ::= ifnotexists ids cpxName LP columnlist RP */ +{ + yylhsminor.yy110 = tSetCreateTableInfo(yymsp[-1].minor.yy135, NULL, NULL, TSQL_CREATE_TABLE); + setSqlInfo(pInfo, yylhsminor.yy110, NULL, TSDB_SQL_CREATE_TABLE); + + yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; + setCreatedTableName(pInfo, &yymsp[-4].minor.yy0, &yymsp[-5].minor.yy0); +} + yymsp[-5].minor.yy110 = yylhsminor.yy110; + break; + case 146: /* create_stable_args ::= ifnotexists ids cpxName LP columnlist RP TAGS LP columnlist RP */ +{ + yylhsminor.yy110 = tSetCreateTableInfo(yymsp[-5].minor.yy135, yymsp[-1].minor.yy135, NULL, TSQL_CREATE_STABLE); + setSqlInfo(pInfo, yylhsminor.yy110, NULL, TSDB_SQL_CREATE_TABLE); + + yymsp[-8].minor.yy0.n += yymsp[-7].minor.yy0.n; + setCreatedTableName(pInfo, &yymsp[-8].minor.yy0, &yymsp[-9].minor.yy0); +} + yymsp[-9].minor.yy110 = yylhsminor.yy110; + break; + case 147: /* create_from_stable ::= ifnotexists ids cpxName USING ids cpxName TAGS LP tagitemlist RP */ +{ + yymsp[-5].minor.yy0.n += yymsp[-4].minor.yy0.n; + yymsp[-8].minor.yy0.n += yymsp[-7].minor.yy0.n; + yylhsminor.yy78 = createNewChildTableInfo(&yymsp[-5].minor.yy0, NULL, yymsp[-1].minor.yy135, &yymsp[-8].minor.yy0, &yymsp[-9].minor.yy0); +} + yymsp[-9].minor.yy78 = yylhsminor.yy78; + break; + case 148: /* create_from_stable ::= ifnotexists ids cpxName USING ids cpxName LP tagNamelist RP TAGS LP tagitemlist RP */ +{ + yymsp[-8].minor.yy0.n += yymsp[-7].minor.yy0.n; + yymsp[-11].minor.yy0.n += yymsp[-10].minor.yy0.n; + yylhsminor.yy78 = createNewChildTableInfo(&yymsp[-8].minor.yy0, yymsp[-5].minor.yy135, yymsp[-1].minor.yy135, &yymsp[-11].minor.yy0, &yymsp[-12].minor.yy0); +} + yymsp[-12].minor.yy78 = yylhsminor.yy78; + break; + case 149: /* tagNamelist ::= tagNamelist COMMA ids */ +{taosArrayPush(yymsp[-2].minor.yy135, &yymsp[0].minor.yy0); yylhsminor.yy135 = yymsp[-2].minor.yy135; } + yymsp[-2].minor.yy135 = yylhsminor.yy135; + break; + case 150: /* tagNamelist ::= ids */ +{yylhsminor.yy135 = taosArrayInit(4, sizeof(SToken)); taosArrayPush(yylhsminor.yy135, &yymsp[0].minor.yy0);} + yymsp[0].minor.yy135 = yylhsminor.yy135; + break; + case 151: /* create_table_args ::= ifnotexists ids cpxName AS select */ +{ + yylhsminor.yy110 = tSetCreateTableInfo(NULL, NULL, yymsp[0].minor.yy488, TSQL_CREATE_STREAM); + setSqlInfo(pInfo, yylhsminor.yy110, NULL, TSDB_SQL_CREATE_TABLE); + + yymsp[-3].minor.yy0.n += yymsp[-2].minor.yy0.n; + setCreatedTableName(pInfo, &yymsp[-3].minor.yy0, &yymsp[-4].minor.yy0); +} + yymsp[-4].minor.yy110 = yylhsminor.yy110; + break; + case 152: /* columnlist ::= columnlist COMMA column */ +{taosArrayPush(yymsp[-2].minor.yy135, &yymsp[0].minor.yy181); yylhsminor.yy135 = yymsp[-2].minor.yy135; } + yymsp[-2].minor.yy135 = yylhsminor.yy135; + break; + case 153: /* columnlist ::= column */ +{yylhsminor.yy135 = taosArrayInit(4, sizeof(TAOS_FIELD)); taosArrayPush(yylhsminor.yy135, &yymsp[0].minor.yy181);} + yymsp[0].minor.yy135 = yylhsminor.yy135; + break; + case 154: /* column ::= ids typename */ +{ + tSetColumnInfo(&yylhsminor.yy181, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy181); +} + yymsp[-1].minor.yy181 = yylhsminor.yy181; + break; + case 161: /* tagitem ::= NULL */ +{ yymsp[0].minor.yy0.type = 0; taosVariantCreate(&yylhsminor.yy191, yymsp[0].minor.yy0.z, yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.type); } + yymsp[0].minor.yy191 = yylhsminor.yy191; + break; + case 162: /* tagitem ::= NOW */ +{ yymsp[0].minor.yy0.type = TSDB_DATA_TYPE_TIMESTAMP; taosVariantCreate(&yylhsminor.yy191, yymsp[0].minor.yy0.z, yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.type);} + yymsp[0].minor.yy191 = yylhsminor.yy191; + break; + case 163: /* tagitem ::= MINUS INTEGER */ + case 164: /* tagitem ::= MINUS FLOAT */ yytestcase(yyruleno==164); + case 165: /* tagitem ::= PLUS INTEGER */ yytestcase(yyruleno==165); + case 166: /* tagitem ::= PLUS FLOAT */ yytestcase(yyruleno==166); +{ + yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; + yymsp[-1].minor.yy0.type = yymsp[0].minor.yy0.type; + toTSDBType(yymsp[-1].minor.yy0.type); + taosVariantCreate(&yylhsminor.yy191, yymsp[-1].minor.yy0.z, yymsp[-1].minor.yy0.n, yymsp[-1].minor.yy0.type); +} + yymsp[-1].minor.yy191 = yylhsminor.yy191; + break; + case 167: /* select ::= SELECT selcollist from where_opt interval_option sliding_opt session_option windowstate_option fill_opt groupby_opt having_opt orderby_opt slimit_opt limit_opt */ +{ + yylhsminor.yy488 = tSetQuerySqlNode(&yymsp[-13].minor.yy0, yymsp[-12].minor.yy135, yymsp[-11].minor.yy460, yymsp[-10].minor.yy526, yymsp[-4].minor.yy135, yymsp[-2].minor.yy135, &yymsp[-9].minor.yy160, &yymsp[-7].minor.yy511, &yymsp[-6].minor.yy258, &yymsp[-8].minor.yy0, yymsp[-5].minor.yy135, &yymsp[0].minor.yy247, &yymsp[-1].minor.yy247, yymsp[-3].minor.yy526); +} + yymsp[-13].minor.yy488 = yylhsminor.yy488; + break; + case 168: /* select ::= LP select RP */ +{yymsp[-2].minor.yy488 = yymsp[-1].minor.yy488;} + break; + case 169: /* union ::= select */ +{ yylhsminor.yy135 = setSubclause(NULL, yymsp[0].minor.yy488); } + yymsp[0].minor.yy135 = yylhsminor.yy135; + break; + case 170: /* union ::= union UNION ALL select */ +{ yylhsminor.yy135 = appendSelectClause(yymsp[-3].minor.yy135, yymsp[0].minor.yy488); } + yymsp[-3].minor.yy135 = yylhsminor.yy135; + break; + case 171: /* cmd ::= union */ +{ setSqlInfo(pInfo, yymsp[0].minor.yy135, NULL, TSDB_SQL_SELECT); } + break; + case 172: /* select ::= SELECT selcollist */ +{ + yylhsminor.yy488 = tSetQuerySqlNode(&yymsp[-1].minor.yy0, yymsp[0].minor.yy135, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); +} + yymsp[-1].minor.yy488 = yylhsminor.yy488; + break; + case 173: /* sclp ::= selcollist COMMA */ +{yylhsminor.yy135 = yymsp[-1].minor.yy135;} + yymsp[-1].minor.yy135 = yylhsminor.yy135; + break; + case 174: /* sclp ::= */ + case 206: /* orderby_opt ::= */ yytestcase(yyruleno==206); +{yymsp[1].minor.yy135 = 0;} + break; + case 175: /* selcollist ::= sclp distinct expr as */ +{ + yylhsminor.yy135 = tSqlExprListAppend(yymsp[-3].minor.yy135, yymsp[-1].minor.yy526, yymsp[-2].minor.yy0.n? &yymsp[-2].minor.yy0:0, yymsp[0].minor.yy0.n?&yymsp[0].minor.yy0:0); +} + yymsp[-3].minor.yy135 = yylhsminor.yy135; + break; + case 176: /* selcollist ::= sclp STAR */ +{ + tSqlExpr *pNode = tSqlExprCreateIdValue(NULL, TK_ALL); + yylhsminor.yy135 = tSqlExprListAppend(yymsp[-1].minor.yy135, pNode, 0, 0); +} + yymsp[-1].minor.yy135 = yylhsminor.yy135; + break; + case 177: /* as ::= AS ids */ +{ yymsp[-1].minor.yy0 = yymsp[0].minor.yy0; } + break; + case 178: /* as ::= ids */ +{ yylhsminor.yy0 = yymsp[0].minor.yy0; } + yymsp[0].minor.yy0 = yylhsminor.yy0; + break; + case 179: /* as ::= */ +{ yymsp[1].minor.yy0.n = 0; } + break; + case 180: /* distinct ::= DISTINCT */ +{ yylhsminor.yy0 = yymsp[0].minor.yy0; } + yymsp[0].minor.yy0 = yylhsminor.yy0; + break; + case 182: /* from ::= FROM tablelist */ + case 183: /* from ::= FROM sub */ yytestcase(yyruleno==183); +{yymsp[-1].minor.yy460 = yymsp[0].minor.yy460;} + break; + case 184: /* sub ::= LP union RP */ +{yymsp[-2].minor.yy460 = addSubquery(NULL, yymsp[-1].minor.yy135, NULL);} + break; + case 185: /* sub ::= LP union RP ids */ +{yymsp[-3].minor.yy460 = addSubquery(NULL, yymsp[-2].minor.yy135, &yymsp[0].minor.yy0);} + break; + case 186: /* sub ::= sub COMMA LP union RP ids */ +{yylhsminor.yy460 = addSubquery(yymsp[-5].minor.yy460, yymsp[-2].minor.yy135, &yymsp[0].minor.yy0);} + yymsp[-5].minor.yy460 = yylhsminor.yy460; + break; + case 187: /* tablelist ::= ids cpxName */ +{ + yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; + yylhsminor.yy460 = setTableNameList(NULL, &yymsp[-1].minor.yy0, NULL); +} + yymsp[-1].minor.yy460 = yylhsminor.yy460; + break; + case 188: /* tablelist ::= ids cpxName ids */ +{ + yymsp[-2].minor.yy0.n += yymsp[-1].minor.yy0.n; + yylhsminor.yy460 = setTableNameList(NULL, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0); +} + yymsp[-2].minor.yy460 = yylhsminor.yy460; + break; + case 189: /* tablelist ::= tablelist COMMA ids cpxName */ +{ + yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; + yylhsminor.yy460 = setTableNameList(yymsp[-3].minor.yy460, &yymsp[-1].minor.yy0, NULL); +} + yymsp[-3].minor.yy460 = yylhsminor.yy460; + break; + case 190: /* tablelist ::= tablelist COMMA ids cpxName ids */ +{ + yymsp[-2].minor.yy0.n += yymsp[-1].minor.yy0.n; + yylhsminor.yy460 = setTableNameList(yymsp[-4].minor.yy460, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0); +} + yymsp[-4].minor.yy460 = yylhsminor.yy460; + break; + case 191: /* tmvar ::= VARIABLE */ +{yylhsminor.yy0 = yymsp[0].minor.yy0;} + yymsp[0].minor.yy0 = yylhsminor.yy0; + break; + case 192: /* interval_option ::= intervalKey LP tmvar RP */ +{yylhsminor.yy160.interval = yymsp[-1].minor.yy0; yylhsminor.yy160.offset.n = 0; yylhsminor.yy160.token = yymsp[-3].minor.yy262;} + yymsp[-3].minor.yy160 = yylhsminor.yy160; + break; + case 193: /* interval_option ::= intervalKey LP tmvar COMMA tmvar RP */ +{yylhsminor.yy160.interval = yymsp[-3].minor.yy0; yylhsminor.yy160.offset = yymsp[-1].minor.yy0; yylhsminor.yy160.token = yymsp[-5].minor.yy262;} + yymsp[-5].minor.yy160 = yylhsminor.yy160; + break; + case 194: /* interval_option ::= */ +{memset(&yymsp[1].minor.yy160, 0, sizeof(yymsp[1].minor.yy160));} + break; + case 195: /* intervalKey ::= INTERVAL */ +{yymsp[0].minor.yy262 = TK_INTERVAL;} + break; + case 196: /* intervalKey ::= EVERY */ +{yymsp[0].minor.yy262 = TK_EVERY; } + break; + case 197: /* session_option ::= */ +{yymsp[1].minor.yy511.col.n = 0; yymsp[1].minor.yy511.gap.n = 0;} + break; + case 198: /* session_option ::= SESSION LP ids cpxName COMMA tmvar RP */ +{ + yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; + yymsp[-6].minor.yy511.col = yymsp[-4].minor.yy0; + yymsp[-6].minor.yy511.gap = yymsp[-1].minor.yy0; +} + break; + case 199: /* windowstate_option ::= */ +{ yymsp[1].minor.yy258.col.n = 0; yymsp[1].minor.yy258.col.z = NULL;} + break; + case 200: /* windowstate_option ::= STATE_WINDOW LP ids RP */ +{ yymsp[-3].minor.yy258.col = yymsp[-1].minor.yy0; } + break; + case 201: /* fill_opt ::= */ +{ yymsp[1].minor.yy135 = 0; } + break; + case 202: /* fill_opt ::= FILL LP ID COMMA tagitemlist RP */ +{ + SVariant A = {0}; + toTSDBType(yymsp[-3].minor.yy0.type); + taosVariantCreate(&A, yymsp[-3].minor.yy0.z, yymsp[-3].minor.yy0.n, yymsp[-3].minor.yy0.type); + + tVariantListInsert(yymsp[-1].minor.yy135, &A, -1, 0); + yymsp[-5].minor.yy135 = yymsp[-1].minor.yy135; +} + break; + case 203: /* fill_opt ::= FILL LP ID RP */ +{ + toTSDBType(yymsp[-1].minor.yy0.type); + yymsp[-3].minor.yy135 = tListItemAppendToken(NULL, &yymsp[-1].minor.yy0, -1); +} + break; + case 204: /* sliding_opt ::= SLIDING LP tmvar RP */ +{yymsp[-3].minor.yy0 = yymsp[-1].minor.yy0; } + break; + case 205: /* sliding_opt ::= */ +{yymsp[1].minor.yy0.n = 0; yymsp[1].minor.yy0.z = NULL; yymsp[1].minor.yy0.type = 0; } + break; + case 207: /* orderby_opt ::= ORDER BY sortlist */ +{yymsp[-2].minor.yy135 = yymsp[0].minor.yy135;} + break; + case 208: /* sortlist ::= sortlist COMMA item sortorder */ +{ + yylhsminor.yy135 = tListItemAppend(yymsp[-3].minor.yy135, &yymsp[-1].minor.yy191, yymsp[0].minor.yy130); +} + yymsp[-3].minor.yy135 = yylhsminor.yy135; + break; + case 209: /* sortlist ::= item sortorder */ +{ + yylhsminor.yy135 = tListItemAppend(NULL, &yymsp[-1].minor.yy191, yymsp[0].minor.yy130); +} + yymsp[-1].minor.yy135 = yylhsminor.yy135; + break; + case 210: /* item ::= ids cpxName */ +{ + toTSDBType(yymsp[-1].minor.yy0.type); + yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; + + taosVariantCreate(&yylhsminor.yy191, yymsp[-1].minor.yy0.z, yymsp[-1].minor.yy0.n, yymsp[-1].minor.yy0.type); +} + yymsp[-1].minor.yy191 = yylhsminor.yy191; + break; + case 211: /* sortorder ::= ASC */ +{ yymsp[0].minor.yy130 = TSDB_ORDER_ASC; } + break; + case 212: /* sortorder ::= DESC */ +{ yymsp[0].minor.yy130 = TSDB_ORDER_DESC;} + break; + case 213: /* sortorder ::= */ +{ yymsp[1].minor.yy130 = TSDB_ORDER_ASC; } + break; + case 214: /* groupby_opt ::= */ +{ yymsp[1].minor.yy135 = 0;} + break; + case 215: /* groupby_opt ::= GROUP BY grouplist */ +{ yymsp[-2].minor.yy135 = yymsp[0].minor.yy135;} + break; + case 216: /* grouplist ::= grouplist COMMA item */ +{ + yylhsminor.yy135 = tListItemAppend(yymsp[-2].minor.yy135, &yymsp[0].minor.yy191, -1); +} + yymsp[-2].minor.yy135 = yylhsminor.yy135; + break; + case 217: /* grouplist ::= item */ +{ + yylhsminor.yy135 = tListItemAppend(NULL, &yymsp[0].minor.yy191, -1); +} + yymsp[0].minor.yy135 = yylhsminor.yy135; + break; + case 218: /* having_opt ::= */ + case 228: /* where_opt ::= */ yytestcase(yyruleno==228); + case 272: /* expritem ::= */ yytestcase(yyruleno==272); +{yymsp[1].minor.yy526 = 0;} + break; + case 219: /* having_opt ::= HAVING expr */ + case 229: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==229); +{yymsp[-1].minor.yy526 = yymsp[0].minor.yy526;} + break; + case 220: /* limit_opt ::= */ + case 224: /* slimit_opt ::= */ yytestcase(yyruleno==224); +{yymsp[1].minor.yy247.limit = -1; yymsp[1].minor.yy247.offset = 0;} + break; + case 221: /* limit_opt ::= LIMIT signed */ + case 225: /* slimit_opt ::= SLIMIT signed */ yytestcase(yyruleno==225); +{yymsp[-1].minor.yy247.limit = yymsp[0].minor.yy531; yymsp[-1].minor.yy247.offset = 0;} + break; + case 222: /* limit_opt ::= LIMIT signed OFFSET signed */ +{ yymsp[-3].minor.yy247.limit = yymsp[-2].minor.yy531; yymsp[-3].minor.yy247.offset = yymsp[0].minor.yy531;} + break; + case 223: /* limit_opt ::= LIMIT signed COMMA signed */ +{ yymsp[-3].minor.yy247.limit = yymsp[0].minor.yy531; yymsp[-3].minor.yy247.offset = yymsp[-2].minor.yy531;} + break; + case 226: /* slimit_opt ::= SLIMIT signed SOFFSET signed */ +{yymsp[-3].minor.yy247.limit = yymsp[-2].minor.yy531; yymsp[-3].minor.yy247.offset = yymsp[0].minor.yy531;} + break; + case 227: /* slimit_opt ::= SLIMIT signed COMMA signed */ +{yymsp[-3].minor.yy247.limit = yymsp[0].minor.yy531; yymsp[-3].minor.yy247.offset = yymsp[-2].minor.yy531;} + break; + case 230: /* expr ::= LP expr RP */ +{yylhsminor.yy526 = yymsp[-1].minor.yy526; yylhsminor.yy526->exprToken.z = yymsp[-2].minor.yy0.z; yylhsminor.yy526->exprToken.n = (yymsp[0].minor.yy0.z - yymsp[-2].minor.yy0.z + 1);} + yymsp[-2].minor.yy526 = yylhsminor.yy526; + break; + case 231: /* expr ::= ID */ +{ yylhsminor.yy526 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_ID);} + yymsp[0].minor.yy526 = yylhsminor.yy526; + break; + case 232: /* expr ::= ID DOT ID */ +{ yymsp[-2].minor.yy0.n += (1+yymsp[0].minor.yy0.n); yylhsminor.yy526 = tSqlExprCreateIdValue(&yymsp[-2].minor.yy0, TK_ID);} + yymsp[-2].minor.yy526 = yylhsminor.yy526; + break; + case 233: /* expr ::= ID DOT STAR */ +{ yymsp[-2].minor.yy0.n += (1+yymsp[0].minor.yy0.n); yylhsminor.yy526 = tSqlExprCreateIdValue(&yymsp[-2].minor.yy0, TK_ALL);} + yymsp[-2].minor.yy526 = yylhsminor.yy526; + break; + case 234: /* expr ::= INTEGER */ +{ yylhsminor.yy526 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_INTEGER);} + yymsp[0].minor.yy526 = yylhsminor.yy526; + break; + case 235: /* expr ::= MINUS INTEGER */ + case 236: /* expr ::= PLUS INTEGER */ yytestcase(yyruleno==236); +{ yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; yymsp[-1].minor.yy0.type = TK_INTEGER; yylhsminor.yy526 = tSqlExprCreateIdValue(&yymsp[-1].minor.yy0, TK_INTEGER);} + yymsp[-1].minor.yy526 = yylhsminor.yy526; + break; + case 237: /* expr ::= FLOAT */ +{ yylhsminor.yy526 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_FLOAT);} + yymsp[0].minor.yy526 = yylhsminor.yy526; + break; + case 238: /* expr ::= MINUS FLOAT */ + case 239: /* expr ::= PLUS FLOAT */ yytestcase(yyruleno==239); +{ yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; yymsp[-1].minor.yy0.type = TK_FLOAT; yylhsminor.yy526 = tSqlExprCreateIdValue(&yymsp[-1].minor.yy0, TK_FLOAT);} + yymsp[-1].minor.yy526 = yylhsminor.yy526; + break; + case 240: /* expr ::= STRING */ +{ yylhsminor.yy526 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_STRING);} + yymsp[0].minor.yy526 = yylhsminor.yy526; + break; + case 241: /* expr ::= NOW */ +{ yylhsminor.yy526 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_NOW); } + yymsp[0].minor.yy526 = yylhsminor.yy526; + break; + case 242: /* expr ::= VARIABLE */ +{ yylhsminor.yy526 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_VARIABLE);} + yymsp[0].minor.yy526 = yylhsminor.yy526; + break; + case 243: /* expr ::= PLUS VARIABLE */ + case 244: /* expr ::= MINUS VARIABLE */ yytestcase(yyruleno==244); +{ yymsp[-1].minor.yy0.n += yymsp[0].minor.yy0.n; yymsp[-1].minor.yy0.type = TK_VARIABLE; yylhsminor.yy526 = tSqlExprCreateIdValue(&yymsp[-1].minor.yy0, TK_VARIABLE);} + yymsp[-1].minor.yy526 = yylhsminor.yy526; + break; + case 245: /* expr ::= BOOL */ +{ yylhsminor.yy526 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_BOOL);} + yymsp[0].minor.yy526 = yylhsminor.yy526; + break; + case 246: /* expr ::= NULL */ +{ yylhsminor.yy526 = tSqlExprCreateIdValue(&yymsp[0].minor.yy0, TK_NULL);} + yymsp[0].minor.yy526 = yylhsminor.yy526; + break; + case 247: /* expr ::= ID LP exprlist RP */ +{ tAppendFuncName(pInfo->funcs, &yymsp[-3].minor.yy0); yylhsminor.yy526 = tSqlExprCreateFunction(yymsp[-1].minor.yy135, &yymsp[-3].minor.yy0, &yymsp[0].minor.yy0, yymsp[-3].minor.yy0.type); } + yymsp[-3].minor.yy526 = yylhsminor.yy526; + break; + case 248: /* expr ::= ID LP STAR RP */ +{ tAppendFuncName(pInfo->funcs, &yymsp[-3].minor.yy0); yylhsminor.yy526 = tSqlExprCreateFunction(NULL, &yymsp[-3].minor.yy0, &yymsp[0].minor.yy0, yymsp[-3].minor.yy0.type); } + yymsp[-3].minor.yy526 = yylhsminor.yy526; + break; + case 249: /* expr ::= expr IS NULL */ +{yylhsminor.yy526 = tSqlExprCreate(yymsp[-2].minor.yy526, NULL, TK_ISNULL);} + yymsp[-2].minor.yy526 = yylhsminor.yy526; + break; + case 250: /* expr ::= expr IS NOT NULL */ +{yylhsminor.yy526 = tSqlExprCreate(yymsp[-3].minor.yy526, NULL, TK_NOTNULL);} + yymsp[-3].minor.yy526 = yylhsminor.yy526; + break; + case 251: /* expr ::= expr LT expr */ +{yylhsminor.yy526 = tSqlExprCreate(yymsp[-2].minor.yy526, yymsp[0].minor.yy526, TK_LT);} + yymsp[-2].minor.yy526 = yylhsminor.yy526; + break; + case 252: /* expr ::= expr GT expr */ +{yylhsminor.yy526 = tSqlExprCreate(yymsp[-2].minor.yy526, yymsp[0].minor.yy526, TK_GT);} + yymsp[-2].minor.yy526 = yylhsminor.yy526; + break; + case 253: /* expr ::= expr LE expr */ +{yylhsminor.yy526 = tSqlExprCreate(yymsp[-2].minor.yy526, yymsp[0].minor.yy526, TK_LE);} + yymsp[-2].minor.yy526 = yylhsminor.yy526; + break; + case 254: /* expr ::= expr GE expr */ +{yylhsminor.yy526 = tSqlExprCreate(yymsp[-2].minor.yy526, yymsp[0].minor.yy526, TK_GE);} + yymsp[-2].minor.yy526 = yylhsminor.yy526; + break; + case 255: /* expr ::= expr NE expr */ +{yylhsminor.yy526 = tSqlExprCreate(yymsp[-2].minor.yy526, yymsp[0].minor.yy526, TK_NE);} + yymsp[-2].minor.yy526 = yylhsminor.yy526; + break; + case 256: /* expr ::= expr EQ expr */ +{yylhsminor.yy526 = tSqlExprCreate(yymsp[-2].minor.yy526, yymsp[0].minor.yy526, TK_EQ);} + yymsp[-2].minor.yy526 = yylhsminor.yy526; + break; + case 257: /* expr ::= expr BETWEEN expr AND expr */ +{ tSqlExpr* X2 = tSqlExprClone(yymsp[-4].minor.yy526); yylhsminor.yy526 = tSqlExprCreate(tSqlExprCreate(yymsp[-4].minor.yy526, yymsp[-2].minor.yy526, TK_GE), tSqlExprCreate(X2, yymsp[0].minor.yy526, TK_LE), TK_AND);} + yymsp[-4].minor.yy526 = yylhsminor.yy526; + break; + case 258: /* expr ::= expr AND expr */ +{yylhsminor.yy526 = tSqlExprCreate(yymsp[-2].minor.yy526, yymsp[0].minor.yy526, TK_AND);} + yymsp[-2].minor.yy526 = yylhsminor.yy526; + break; + case 259: /* expr ::= expr OR expr */ +{yylhsminor.yy526 = tSqlExprCreate(yymsp[-2].minor.yy526, yymsp[0].minor.yy526, TK_OR); } + yymsp[-2].minor.yy526 = yylhsminor.yy526; + break; + case 260: /* expr ::= expr PLUS expr */ +{yylhsminor.yy526 = tSqlExprCreate(yymsp[-2].minor.yy526, yymsp[0].minor.yy526, TK_PLUS); } + yymsp[-2].minor.yy526 = yylhsminor.yy526; + break; + case 261: /* expr ::= expr MINUS expr */ +{yylhsminor.yy526 = tSqlExprCreate(yymsp[-2].minor.yy526, yymsp[0].minor.yy526, TK_MINUS); } + yymsp[-2].minor.yy526 = yylhsminor.yy526; + break; + case 262: /* expr ::= expr STAR expr */ +{yylhsminor.yy526 = tSqlExprCreate(yymsp[-2].minor.yy526, yymsp[0].minor.yy526, TK_STAR); } + yymsp[-2].minor.yy526 = yylhsminor.yy526; + break; + case 263: /* expr ::= expr SLASH expr */ +{yylhsminor.yy526 = tSqlExprCreate(yymsp[-2].minor.yy526, yymsp[0].minor.yy526, TK_DIVIDE);} + yymsp[-2].minor.yy526 = yylhsminor.yy526; + break; + case 264: /* expr ::= expr REM expr */ +{yylhsminor.yy526 = tSqlExprCreate(yymsp[-2].minor.yy526, yymsp[0].minor.yy526, TK_REM); } + yymsp[-2].minor.yy526 = yylhsminor.yy526; + break; + case 265: /* expr ::= expr LIKE expr */ +{yylhsminor.yy526 = tSqlExprCreate(yymsp[-2].minor.yy526, yymsp[0].minor.yy526, TK_LIKE); } + yymsp[-2].minor.yy526 = yylhsminor.yy526; + break; + case 266: /* expr ::= expr MATCH expr */ +{yylhsminor.yy526 = tSqlExprCreate(yymsp[-2].minor.yy526, yymsp[0].minor.yy526, TK_MATCH); } + yymsp[-2].minor.yy526 = yylhsminor.yy526; + break; + case 267: /* expr ::= expr NMATCH expr */ +{yylhsminor.yy526 = tSqlExprCreate(yymsp[-2].minor.yy526, yymsp[0].minor.yy526, TK_NMATCH); } + yymsp[-2].minor.yy526 = yylhsminor.yy526; + break; + case 268: /* expr ::= expr IN LP exprlist RP */ +{yylhsminor.yy526 = tSqlExprCreate(yymsp[-4].minor.yy526, (tSqlExpr*)yymsp[-1].minor.yy135, TK_IN); } + yymsp[-4].minor.yy526 = yylhsminor.yy526; + break; + case 269: /* exprlist ::= exprlist COMMA expritem */ +{yylhsminor.yy135 = tSqlExprListAppend(yymsp[-2].minor.yy135,yymsp[0].minor.yy526,0, 0);} + yymsp[-2].minor.yy135 = yylhsminor.yy135; + break; + case 270: /* exprlist ::= expritem */ +{yylhsminor.yy135 = tSqlExprListAppend(0,yymsp[0].minor.yy526,0, 0);} + yymsp[0].minor.yy135 = yylhsminor.yy135; + break; + case 271: /* expritem ::= expr */ +{yylhsminor.yy526 = yymsp[0].minor.yy526;} + yymsp[0].minor.yy526 = yylhsminor.yy526; + break; + case 273: /* cmd ::= RESET QUERY CACHE */ +{ setDCLSqlElems(pInfo, TSDB_SQL_RESET_CACHE, 0);} + break; + case 274: /* cmd ::= SYNCDB ids REPLICA */ +{ setDCLSqlElems(pInfo, TSDB_SQL_SYNC_DB_REPLICA, 1, &yymsp[-1].minor.yy0);} + break; + case 275: /* cmd ::= ALTER TABLE ids cpxName ADD COLUMN columnlist */ +{ + yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, yymsp[0].minor.yy135, NULL, TSDB_ALTER_TABLE_ADD_COLUMN, -1); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + break; + case 276: /* cmd ::= ALTER TABLE ids cpxName DROP COLUMN ids */ +{ + yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; + + toTSDBType(yymsp[0].minor.yy0.type); + SArray* K = tListItemAppendToken(NULL, &yymsp[0].minor.yy0, -1); + + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, NULL, K, TSDB_ALTER_TABLE_DROP_COLUMN, -1); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + break; + case 277: /* cmd ::= ALTER TABLE ids cpxName MODIFY COLUMN columnlist */ +{ + yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, yymsp[0].minor.yy135, NULL, TSDB_ALTER_TABLE_CHANGE_COLUMN, -1); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + break; + case 278: /* cmd ::= ALTER TABLE ids cpxName ADD TAG columnlist */ +{ + yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, yymsp[0].minor.yy135, NULL, TSDB_ALTER_TABLE_ADD_TAG_COLUMN, -1); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + break; + case 279: /* cmd ::= ALTER TABLE ids cpxName DROP TAG ids */ +{ + yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; + + toTSDBType(yymsp[0].minor.yy0.type); + SArray* A = tListItemAppendToken(NULL, &yymsp[0].minor.yy0, -1); + + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, NULL, A, TSDB_ALTER_TABLE_DROP_TAG_COLUMN, -1); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + break; + case 280: /* cmd ::= ALTER TABLE ids cpxName CHANGE TAG ids ids */ +{ + yymsp[-5].minor.yy0.n += yymsp[-4].minor.yy0.n; + + toTSDBType(yymsp[-1].minor.yy0.type); + SArray* A = tListItemAppendToken(NULL, &yymsp[-1].minor.yy0, -1); + + toTSDBType(yymsp[0].minor.yy0.type); + A = tListItemAppendToken(A, &yymsp[0].minor.yy0, -1); + + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-5].minor.yy0, NULL, A, TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN, -1); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + break; + case 281: /* cmd ::= ALTER TABLE ids cpxName SET TAG ids EQ tagitem */ +{ + yymsp[-6].minor.yy0.n += yymsp[-5].minor.yy0.n; + + toTSDBType(yymsp[-2].minor.yy0.type); + SArray* A = tListItemAppendToken(NULL, &yymsp[-2].minor.yy0, -1); + A = tListItemAppend(A, &yymsp[0].minor.yy191, -1); + + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-6].minor.yy0, NULL, A, TSDB_ALTER_TABLE_UPDATE_TAG_VAL, -1); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + break; + case 282: /* cmd ::= ALTER TABLE ids cpxName MODIFY TAG columnlist */ +{ + yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, yymsp[0].minor.yy135, NULL, TSDB_ALTER_TABLE_MODIFY_TAG_COLUMN, -1); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + break; + case 283: /* cmd ::= ALTER STABLE ids cpxName ADD COLUMN columnlist */ +{ + yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, yymsp[0].minor.yy135, NULL, TSDB_ALTER_TABLE_ADD_COLUMN, TSDB_SUPER_TABLE); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + break; + case 284: /* cmd ::= ALTER STABLE ids cpxName DROP COLUMN ids */ +{ + yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; + + toTSDBType(yymsp[0].minor.yy0.type); + SArray* K = tListItemAppendToken(NULL, &yymsp[0].minor.yy0, -1); + + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, NULL, K, TSDB_ALTER_TABLE_DROP_COLUMN, TSDB_SUPER_TABLE); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + break; + case 285: /* cmd ::= ALTER STABLE ids cpxName MODIFY COLUMN columnlist */ +{ + yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, yymsp[0].minor.yy135, NULL, TSDB_ALTER_TABLE_CHANGE_COLUMN, TSDB_SUPER_TABLE); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + break; + case 286: /* cmd ::= ALTER STABLE ids cpxName ADD TAG columnlist */ +{ + yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, yymsp[0].minor.yy135, NULL, TSDB_ALTER_TABLE_ADD_TAG_COLUMN, TSDB_SUPER_TABLE); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + break; + case 287: /* cmd ::= ALTER STABLE ids cpxName DROP TAG ids */ +{ + yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; + + toTSDBType(yymsp[0].minor.yy0.type); + SArray* A = tListItemAppendToken(NULL, &yymsp[0].minor.yy0, -1); + + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, NULL, A, TSDB_ALTER_TABLE_DROP_TAG_COLUMN, TSDB_SUPER_TABLE); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + break; + case 288: /* cmd ::= ALTER STABLE ids cpxName CHANGE TAG ids ids */ +{ + yymsp[-5].minor.yy0.n += yymsp[-4].minor.yy0.n; + + toTSDBType(yymsp[-1].minor.yy0.type); + SArray* A = tListItemAppendToken(NULL, &yymsp[-1].minor.yy0, -1); + + toTSDBType(yymsp[0].minor.yy0.type); + A = tListItemAppendToken(A, &yymsp[0].minor.yy0, -1); + + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-5].minor.yy0, NULL, A, TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN, TSDB_SUPER_TABLE); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + break; + case 289: /* cmd ::= ALTER STABLE ids cpxName SET TAG ids EQ tagitem */ +{ + yymsp[-6].minor.yy0.n += yymsp[-5].minor.yy0.n; + + toTSDBType(yymsp[-2].minor.yy0.type); + SArray* A = tListItemAppendToken(NULL, &yymsp[-2].minor.yy0, -1); + A = tListItemAppend(A, &yymsp[0].minor.yy191, -1); + + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-6].minor.yy0, NULL, A, TSDB_ALTER_TABLE_UPDATE_TAG_VAL, TSDB_SUPER_TABLE); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + break; + case 290: /* cmd ::= ALTER STABLE ids cpxName MODIFY TAG columnlist */ +{ + yymsp[-4].minor.yy0.n += yymsp[-3].minor.yy0.n; + SAlterTableInfo* pAlterTable = tSetAlterTableInfo(&yymsp[-4].minor.yy0, yymsp[0].minor.yy135, NULL, TSDB_ALTER_TABLE_MODIFY_TAG_COLUMN, TSDB_SUPER_TABLE); + setSqlInfo(pInfo, pAlterTable, NULL, TSDB_SQL_ALTER_TABLE); +} + break; + case 291: /* cmd ::= KILL CONNECTION INTEGER */ +{setKillSql(pInfo, TSDB_SQL_KILL_CONNECTION, &yymsp[0].minor.yy0);} + break; + case 292: /* cmd ::= KILL STREAM INTEGER COLON INTEGER */ +{yymsp[-2].minor.yy0.n += (yymsp[-1].minor.yy0.n + yymsp[0].minor.yy0.n); setKillSql(pInfo, TSDB_SQL_KILL_STREAM, &yymsp[-2].minor.yy0);} + break; + case 293: /* cmd ::= KILL QUERY INTEGER COLON INTEGER */ +{yymsp[-2].minor.yy0.n += (yymsp[-1].minor.yy0.n + yymsp[0].minor.yy0.n); setKillSql(pInfo, TSDB_SQL_KILL_QUERY, &yymsp[-2].minor.yy0);} + break; + default: + break; +/********** End reduce actions ************************************************/ + }; + assert( yyrulenoYY_MAX_SHIFT && yyact<=YY_MAX_SHIFTREDUCE) ); + + /* It is not possible for a REDUCE to be followed by an error */ + assert( yyact!=YY_ERROR_ACTION ); + + yymsp += yysize+1; + yypParser->yytos = yymsp; + yymsp->stateno = (YYACTIONTYPE)yyact; + yymsp->major = (YYCODETYPE)yygoto; + yyTraceShift(yypParser, yyact, "... then shift"); +} + +/* +** The following code executes when the parse fails +*/ +#ifndef YYNOERRORRECOVERY +static void yy_parse_failed( + yyParser *yypParser /* The parser */ +){ + ParseARG_FETCH; +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt); + } +#endif + while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser); + /* Here code is inserted which will be executed whenever the + ** parser fails */ +/************ Begin %parse_failure code ***************************************/ +/************ End %parse_failure code *****************************************/ + ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ +} +#endif /* YYNOERRORRECOVERY */ + +/* +** The following code executes when a syntax error first occurs. +*/ +static void yy_syntax_error( + yyParser *yypParser, /* The parser */ + int yymajor, /* The major type of the error token */ + ParseTOKENTYPE yyminor /* The minor type of the error token */ +){ + ParseARG_FETCH; +#define TOKEN yyminor +/************ Begin %syntax_error code ****************************************/ + + pInfo->valid = false; + int32_t outputBufLen = tListLen(pInfo->msg); + int32_t len = 0; + + if(TOKEN.z) { + char msg[] = "syntax error near \"%s\""; + int32_t sqlLen = strlen(&TOKEN.z[0]); + + if (sqlLen + sizeof(msg)/sizeof(msg[0]) + 1 > outputBufLen) { + char tmpstr[128] = {0}; + memcpy(tmpstr, &TOKEN.z[0], sizeof(tmpstr)/sizeof(tmpstr[0]) - 1); + len = sprintf(pInfo->msg, msg, tmpstr); + } else { + len = sprintf(pInfo->msg, msg, &TOKEN.z[0]); + } + + } else { + len = sprintf(pInfo->msg, "Incomplete SQL statement"); + } + + assert(len <= outputBufLen); +/************ End %syntax_error code ******************************************/ + ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ +} + +/* +** The following is executed when the parser accepts +*/ +static void yy_accept( + yyParser *yypParser /* The parser */ +){ + ParseARG_FETCH; +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt); + } +#endif +#ifndef YYNOERRORRECOVERY + yypParser->yyerrcnt = -1; +#endif + assert( yypParser->yytos==yypParser->yystack ); + /* Here code is inserted which will be executed whenever the + ** parser accepts */ +/*********** Begin %parse_accept code *****************************************/ + +/*********** End %parse_accept code *******************************************/ + ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ +} + +/* The main parser program. +** The first argument is a pointer to a structure obtained from +** "ParseAlloc" which describes the current state of the parser. +** The second argument is the major token number. The third is +** the minor token. The fourth optional argument is whatever the +** user wants (and specified in the grammar) and is available for +** use by the action routines. +** +** Inputs: +**
    +**
  • A pointer to the parser (an opaque structure.) +**
  • The major token number. +**
  • The minor token number. +**
  • An option argument of a grammar-specified type. +**
+** +** Outputs: +** None. +*/ +void Parse( + void *yyp, /* The parser */ + int yymajor, /* The major token code number */ + ParseTOKENTYPE yyminor /* The value for the token */ + ParseARG_PDECL /* Optional %extra_argument parameter */ +){ + YYMINORTYPE yyminorunion; + unsigned int yyact; /* The parser action. */ +#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY) + int yyendofinput; /* True if we are at the end of input */ +#endif +#ifdef YYERRORSYMBOL + int yyerrorhit = 0; /* True if yymajor has invoked an error */ +#endif + yyParser *yypParser; /* The parser */ + + yypParser = (yyParser*)yyp; + assert( yypParser->yytos!=0 ); +#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY) + yyendofinput = (yymajor==0); +#endif + ParseARG_STORE; + +#ifndef NDEBUG + if( yyTraceFILE ){ + int stateno = yypParser->yytos->stateno; + if( stateno < YY_MIN_REDUCE ){ + fprintf(yyTraceFILE,"%sInput '%s' in state %d\n", + yyTracePrompt,yyTokenName[yymajor],stateno); + }else{ + fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n", + yyTracePrompt,yyTokenName[yymajor],stateno-YY_MIN_REDUCE); + } + } +#endif + + do{ + yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor); + if( yyact >= YY_MIN_REDUCE ){ + yy_reduce(yypParser,yyact-YY_MIN_REDUCE,yymajor,yyminor); + }else if( yyact <= YY_MAX_SHIFTREDUCE ){ + yy_shift(yypParser,yyact,yymajor,yyminor); +#ifndef YYNOERRORRECOVERY + yypParser->yyerrcnt--; +#endif + yymajor = YYNOCODE; + }else if( yyact==YY_ACCEPT_ACTION ){ + yypParser->yytos--; + yy_accept(yypParser); + return; + }else{ + assert( yyact == YY_ERROR_ACTION ); + yyminorunion.yy0 = yyminor; +#ifdef YYERRORSYMBOL + int yymx; +#endif +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt); + } +#endif +#ifdef YYERRORSYMBOL + /* A syntax error has occurred. + ** The response to an error depends upon whether or not the + ** grammar defines an error token "ERROR". + ** + ** This is what we do if the grammar does define ERROR: + ** + ** * Call the %syntax_error function. + ** + ** * Begin popping the stack until we enter a state where + ** it is legal to shift the error symbol, then shift + ** the error symbol. + ** + ** * Set the error count to three. + ** + ** * Begin accepting and shifting new tokens. No new error + ** processing will occur until three tokens have been + ** shifted successfully. + ** + */ + if( yypParser->yyerrcnt<0 ){ + yy_syntax_error(yypParser,yymajor,yyminor); + } + yymx = yypParser->yytos->major; + if( yymx==YYERRORSYMBOL || yyerrorhit ){ +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sDiscard input token %s\n", + yyTracePrompt,yyTokenName[yymajor]); + } +#endif + yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion); + yymajor = YYNOCODE; + }else{ + while( yypParser->yytos >= yypParser->yystack + && yymx != YYERRORSYMBOL + && (yyact = yy_find_reduce_action( + yypParser->yytos->stateno, + YYERRORSYMBOL)) >= YY_MIN_REDUCE + ){ + yy_pop_parser_stack(yypParser); + } + if( yypParser->yytos < yypParser->yystack || yymajor==0 ){ + yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); + yy_parse_failed(yypParser); +#ifndef YYNOERRORRECOVERY + yypParser->yyerrcnt = -1; +#endif + yymajor = YYNOCODE; + }else if( yymx!=YYERRORSYMBOL ){ + yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor); + } + } + yypParser->yyerrcnt = 3; + yyerrorhit = 1; +#elif defined(YYNOERRORRECOVERY) + /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to + ** do any kind of error recovery. Instead, simply invoke the syntax + ** error routine and continue going as if nothing had happened. + ** + ** Applications can set this macro (for example inside %include) if + ** they intend to abandon the parse upon the first syntax error seen. + */ + yy_syntax_error(yypParser,yymajor, yyminor); + yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); + yymajor = YYNOCODE; + +#else /* YYERRORSYMBOL is not defined */ + /* This is what we do if the grammar does not define ERROR: + ** + ** * Report an error message, and throw away the input token. + ** + ** * If the input token is $, then fail the parse. + ** + ** As before, subsequent error messages are suppressed until + ** three input tokens have been successfully shifted. + */ + if( yypParser->yyerrcnt<=0 ){ + yy_syntax_error(yypParser,yymajor, yyminor); + } + yypParser->yyerrcnt = 3; + yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); + if( yyendofinput ){ + yy_parse_failed(yypParser); +#ifndef YYNOERRORRECOVERY + yypParser->yyerrcnt = -1; +#endif + } + yymajor = YYNOCODE; +#endif + } + }while( yymajor!=YYNOCODE && yypParser->yytos>yypParser->yystack ); +#ifndef NDEBUG + if( yyTraceFILE ){ + yyStackEntry *i; + char cDiv = '['; + fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt); + for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){ + fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]); + cDiv = ' '; + } + fprintf(yyTraceFILE,"]\n"); + } +#endif + return; +} diff --git a/src/util/src/ttokenizer.c b/source/libs/parser/src/ttokenizer.c similarity index 98% rename from src/util/src/ttokenizer.c rename to source/libs/parser/src/ttokenizer.c index 289c4a6ef5d5db1a04fdb33985ed4de959375f8d..5e3cefea10386fb5ba09047182c8abe824b49d81 100644 --- a/src/util/src/ttokenizer.c +++ b/source/libs/parser/src/ttokenizer.c @@ -15,12 +15,10 @@ #include "os.h" -#include "hash.h" -#include "hashfunc.h" +#include "thash.h" #include "taosdef.h" #include "ttoken.h" #include "ttokendef.h" -#include "tutil.h" // All the keywords of the SQL language are stored in a hash table typedef struct SKeyword { @@ -575,12 +573,12 @@ uint32_t tGetToken(char* z, uint32_t* tokenId) { return 0; } -SStrToken tscReplaceStrToken(char **str, SStrToken *token, const char* newToken) { +SToken tscReplaceStrToken(char **str, SToken *token, const char* newToken) { char *src = *str; size_t nsize = strlen(newToken); int32_t size = (int32_t)strlen(*str) - token->n + (int32_t)nsize + 1; int32_t bsize = (int32_t)((uint64_t)token->z - (uint64_t)src); - SStrToken ntoken; + SToken ntoken; *str = calloc(1, size); @@ -596,8 +594,8 @@ SStrToken tscReplaceStrToken(char **str, SStrToken *token, const char* newToken) return ntoken; } -SStrToken tStrGetToken(char* str, int32_t* i, bool isPrevOptr) { - SStrToken t0 = {0}; +SToken tStrGetToken(char* str, int32_t* i, bool isPrevOptr) { + SToken t0 = {0}; // here we reach the end of sql string, null-terminated string if (str[*i] == 0) { @@ -689,9 +687,9 @@ void taosCleanupKeywordsTable() { } } -SStrToken taosTokenDup(SStrToken* pToken, char* buf, int32_t len) { +SToken taosTokenDup(SToken* pToken, char* buf, int32_t len) { assert(pToken != NULL && buf != NULL); - SStrToken token = *pToken; + SToken token = *pToken; token.z = buf; assert(len > token.n); diff --git a/source/libs/parser/test/parserTests.cpp b/source/libs/parser/test/parserTests.cpp index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..6dea4a4e57392be988126c579648f39a8270b9bf 100644 --- a/source/libs/parser/test/parserTests.cpp +++ b/source/libs/parser/test/parserTests.cpp @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ \ No newline at end of file diff --git a/source/libs/planner/CMakeLists.txt b/source/libs/planner/CMakeLists.txt index 8b3eb96c14de69cfa46bea7c0c236c4e7db4dfd0..b00d92336dc592b984d23fb426959ce6fb68f560 100644 --- a/source/libs/planner/CMakeLists.txt +++ b/source/libs/planner/CMakeLists.txt @@ -4,4 +4,9 @@ target_include_directories( planner PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/planner" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" +) + +target_link_libraries( + planner + PRIVATE os util common catalog parser transport ) \ No newline at end of file diff --git a/source/libs/planner/inc/plannerInt.h b/source/libs/planner/inc/plannerInt.h index 4005508ed72a4e5b9a00a4477b3b241d9bf5e944..a673f8e48691a506d670e28676e1e988ea7f20f0 100644 --- a/source/libs/planner/inc/plannerInt.h +++ b/source/libs/planner/inc/plannerInt.h @@ -20,6 +20,40 @@ extern "C" { #endif +#include "common.h" +#include "tarray.h" +#include "planner.h" +#include "taosmsg.h" + +typedef struct SQueryNodeBasicInfo { + int32_t type; + char *name; +} SQueryNodeBasicInfo; + +typedef struct SQueryTableInfo { + char *tableName; + uint64_t uid; + int32_t tid; +} SQueryTableInfo; + +typedef struct SQueryNode { + SQueryNodeBasicInfo info; + SQueryTableInfo tableInfo; + SSchema *pSchema; // the schema of the input SSDatablock + int32_t numOfCols; // number of input columns + struct SExprInfo *pExpr; // the query functions or sql aggregations + int32_t numOfOutput; // number of result columns, which is also the number of pExprs + void *pExtInfo; // additional information + // previous operator to generated result for current node to process + // in case of join, multiple prev nodes exist. + SArray *pPrevNodes; // upstream nodes + struct SQueryNode *nextNode; +} SQueryNode; + +typedef struct SQueryPhyNode { + +} SQueryPhyNode; + #ifdef __cplusplus } #endif diff --git a/source/libs/planner/src/planner.c b/source/libs/planner/src/planner.c index 6dea4a4e57392be988126c579648f39a8270b9bf..8e72fb2758c93d299f07f7326233e4c5d46a4ad4 100644 --- a/source/libs/planner/src/planner.c +++ b/source/libs/planner/src/planner.c @@ -11,4 +11,44 @@ * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . - */ \ No newline at end of file + */ + +#include "os.h" +#include "plannerInt.h" +#include "parser.h" + +int32_t qOptimizeQueryPlan(struct SQueryNode* pQueryNode) { + return 0; +} + +int32_t qCreateQueryPlan(const struct SQueryStmtInfo* pQueryInfo, struct SQueryNode* pQueryNode) { + return 0; +} + +int32_t qQueryPlanToString(struct SQueryNode* pQueryNode, char** str) { + return 0; +} + +int32_t qQueryPlanToSql(struct SQueryNode* pQueryNode, char** sql) { + return 0; +} + +int32_t qCreatePhysicalPlan(struct SQueryNode* pQueryNode, struct SEpSet* pQnode, struct SQueryPhyNode *pPhyNode) { + return 0; +} + +int32_t qPhyPlanToString(struct SQueryPhyNode *pPhyNode, char** str) { + return 0; +} + +void* qDestroyQueryPlan(struct SQueryNode* pQueryNode) { + return NULL; +} + +void* qDestroyQueryPhyPlan(struct SQueryPhyNode* pQueryPhyNode) { + return NULL; +} + +int32_t qCreateQueryJob(const struct SQueryPhyNode* pPhyNode, struct SQueryJob** pJob) { + return 0; +} \ No newline at end of file diff --git a/source/libs/scheduler/CMakeLists.txt b/source/libs/scheduler/CMakeLists.txt index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..770a6b02c29763b80436f0f91c2ea9dd36e1e844 100644 --- a/source/libs/scheduler/CMakeLists.txt +++ b/source/libs/scheduler/CMakeLists.txt @@ -0,0 +1,13 @@ +aux_source_directory(src SCHEDULER_SRC) +add_library(scheduler ${SCHEDULER_SRC}) + +target_include_directories( + scheduler + PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/scheduler" + PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" +) + +target_link_libraries( + scheduler + PRIVATE os util planner +) \ No newline at end of file diff --git a/source/libs/scheduler/inc/schedulerInt.h b/source/libs/scheduler/inc/schedulerInt.h index 3e2cf2d37f9a24d0a2a98f612277c00d885fd4fa..b1b128e2000f2453c092b7c1d6a5290c29bfdc2e 100644 --- a/source/libs/scheduler/inc/schedulerInt.h +++ b/source/libs/scheduler/inc/schedulerInt.h @@ -20,6 +20,17 @@ extern "C" { #endif +#include "os.h" +#include "tarray.h" +#include "planner.h" +#include "scheduler.h" + +typedef struct SQuery { + SArray **pSubquery; + int32_t numOfLevels; + int32_t currentLevel; +} SQuery; + #ifdef __cplusplus } #endif diff --git a/source/libs/scheduler/src/scheduler.c b/source/libs/scheduler/src/scheduler.c index 6dea4a4e57392be988126c579648f39a8270b9bf..37f6240f9be700bf73f5f10c55d9444d4e55ef19 100644 --- a/source/libs/scheduler/src/scheduler.c +++ b/source/libs/scheduler/src/scheduler.c @@ -11,4 +11,6 @@ * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . - */ \ No newline at end of file + */ + +#include "schedulerInt.h" \ No newline at end of file diff --git a/source/libs/transport/CMakeLists.txt b/source/libs/transport/CMakeLists.txt index c01996cb8cff7e7510707af1f31d6395594b1c7b..98a380dc8f79c11073d162b78d4817a567e42511 100644 --- a/source/libs/transport/CMakeLists.txt +++ b/source/libs/transport/CMakeLists.txt @@ -4,4 +4,12 @@ target_include_directories( transport PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/transport" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" +) + +target_link_libraries( + transport + PUBLIC lz4_static + PUBLIC os + PUBLIC util + PUBLIC common ) \ No newline at end of file diff --git a/src/rpc/inc/rpcCache.h b/source/libs/transport/inc/rpcCache.h similarity index 100% rename from src/rpc/inc/rpcCache.h rename to source/libs/transport/inc/rpcCache.h diff --git a/src/rpc/inc/rpcHead.h b/source/libs/transport/inc/rpcHead.h similarity index 100% rename from src/rpc/inc/rpcHead.h rename to source/libs/transport/inc/rpcHead.h diff --git a/src/rpc/inc/rpcLog.h b/source/libs/transport/inc/rpcLog.h similarity index 100% rename from src/rpc/inc/rpcLog.h rename to source/libs/transport/inc/rpcLog.h diff --git a/src/rpc/inc/rpcTcp.h b/source/libs/transport/inc/rpcTcp.h similarity index 100% rename from src/rpc/inc/rpcTcp.h rename to source/libs/transport/inc/rpcTcp.h diff --git a/src/rpc/inc/rpcUdp.h b/source/libs/transport/inc/rpcUdp.h similarity index 100% rename from src/rpc/inc/rpcUdp.h rename to source/libs/transport/inc/rpcUdp.h diff --git a/src/rpc/src/rpcCache.c b/source/libs/transport/src/rpcCache.c similarity index 99% rename from src/rpc/src/rpcCache.c rename to source/libs/transport/src/rpcCache.c index 60a12c26b78626ed81cbc182d76c836c6ee74498..7de7aa341b90ed753b63d20b29979f7f72b53734 100644 --- a/src/rpc/src/rpcCache.c +++ b/source/libs/transport/src/rpcCache.c @@ -14,6 +14,7 @@ */ #include "os.h" +#include "taosdef.h" #include "tglobal.h" #include "tmempool.h" #include "ttimer.h" diff --git a/src/rpc/src/rpcMain.c b/source/libs/transport/src/rpcMain.c similarity index 99% rename from src/rpc/src/rpcMain.c rename to source/libs/transport/src/rpcMain.c index 9ea5fd539244820f111a3fbb3c60aee088e727c5..fb555ea33f250b47390009036f9717b40285777e 100644 --- a/src/rpc/src/rpcMain.c +++ b/source/libs/transport/src/rpcMain.c @@ -22,11 +22,10 @@ #include "lz4.h" #include "tref.h" #include "taoserror.h" -#include "tsocket.h" #include "tglobal.h" #include "taosmsg.h" #include "trpc.h" -#include "hash.h" +#include "thash.h" #include "rpcLog.h" #include "rpcUdp.h" #include "rpcCache.h" @@ -694,7 +693,7 @@ static SRpcConn *rpcAllocateClientConn(SRpcInfo *pRpc) { pConn->sid = sid; pConn->tranId = (uint16_t)(taosRand() & 0xFFFF); pConn->ownId = htonl(pConn->sid); - pConn->linkUid = (uint32_t)((int64_t)pConn + (int64_t)getpid() + (int64_t)pConn->tranId); + pConn->linkUid = (uint32_t)((int64_t)pConn + taosGetPid() + (int64_t)pConn->tranId); pConn->spi = pRpc->spi; pConn->encrypt = pRpc->encrypt; if (pConn->spi) memcpy(pConn->secret, pRpc->secret, TSDB_KEY_LEN); diff --git a/src/rpc/src/rpcTcp.c b/source/libs/transport/src/rpcTcp.c similarity index 98% rename from src/rpc/src/rpcTcp.c rename to source/libs/transport/src/rpcTcp.c index 25495182498f7c1a82f9f9459290e44f082f5eb2..d0710c883fe3f80bf213bcdb3ae5ce7f81f3e7bf 100644 --- a/src/rpc/src/rpcTcp.c +++ b/source/libs/transport/src/rpcTcp.c @@ -14,7 +14,6 @@ */ #include "os.h" -#include "tsocket.h" #include "tutil.h" #include "taosdef.h" #include "taoserror.h" @@ -195,16 +194,7 @@ void taosStopTcpServer(void *handle) { pServerObj->stop = 1; if (pServerObj->fd >= 0) { -#ifdef WINDOWS - closesocket(pServerObj->fd); -#elif defined(__APPLE__) - if (pServerObj->fd!=-1) { - close(pServerObj->fd); - pServerObj->fd = -1; - } -#else - shutdown(pServerObj->fd, SHUT_RD); -#endif + taosShutDownSocketRD(pServerObj->fd); } if (taosCheckPthreadValid(pServerObj->thread)) { if (taosComparePthread(pServerObj->thread, pthread_self())) { @@ -436,7 +426,7 @@ void taosCloseTcpConnection(void *chandle) { // pFdObj->thandle = NULL; pFdObj->closedByApp = 1; - shutdown(pFdObj->fd, SHUT_WR); + taosShutDownSocketWR(pFdObj->fd); } int taosSendTcpData(uint32_t ip, uint16_t port, void *data, int len, void *chandle) { @@ -456,7 +446,7 @@ static void taosReportBrokenLink(SFdObj *pFdObj) { // notify the upper layer, so it will clean the associated context if (pFdObj->closedByApp == 0) { - shutdown(pFdObj->fd, SHUT_WR); + taosShutDownSocketWR(pFdObj->fd); SRecvInfo recvInfo; recvInfo.msg = NULL; diff --git a/src/rpc/src/rpcUdp.c b/source/libs/transport/src/rpcUdp.c similarity index 99% rename from src/rpc/src/rpcUdp.c rename to source/libs/transport/src/rpcUdp.c index 086a390cb8ea2a95f576cb1bff81dfc79769863a..5bc31c189a0eef9dbd145e23aea5be4e0b9b0f1d 100644 --- a/src/rpc/src/rpcUdp.c +++ b/source/libs/transport/src/rpcUdp.c @@ -14,7 +14,6 @@ */ #include "os.h" -#include "tsocket.h" #include "ttimer.h" #include "tutil.h" #include "taosdef.h" diff --git a/source/libs/wal/CMakeLists.txt b/source/libs/wal/CMakeLists.txt index 1e195862763a63f5bc9c92aa5044c11334090aab..fbcdff59eeb243cfe0c4569eae63ca8b0d8043b4 100644 --- a/source/libs/wal/CMakeLists.txt +++ b/source/libs/wal/CMakeLists.txt @@ -4,4 +4,9 @@ target_include_directories( wal PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/wal" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" -) \ No newline at end of file + PRIVATE "${CMAKE_SOURCE_DIR}/include/os" +) + +target_link_libraries( + os +) diff --git a/source/os/CMakeLists.txt b/source/os/CMakeLists.txt index 9e5d6126baf1899fb284150da9f03b9e37348613..0eda9d637df074d3a4ad9a522ce8ce9d63bc8a94 100644 --- a/source/os/CMakeLists.txt +++ b/source/os/CMakeLists.txt @@ -4,4 +4,11 @@ target_include_directories( os PUBLIC "${CMAKE_SOURCE_DIR}/include/os" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include" +) +target_link_libraries( + os + PUBLIC pthread + PUBLIC dl + PUBLIC rt + PUBLIC m ) \ No newline at end of file diff --git a/source/os/src/os.c b/source/os/src/os.c deleted file mode 100644 index 6dea4a4e57392be988126c579648f39a8270b9bf..0000000000000000000000000000000000000000 --- a/source/os/src/os.c +++ /dev/null @@ -1,14 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ \ No newline at end of file diff --git a/src/os/src/windows/wAtomic.c b/source/os/src/osAtomic.c similarity index 81% rename from src/os/src/windows/wAtomic.c rename to source/os/src/osAtomic.c index b645893030b852428bff13744050b1f6f8ef1c42..6429cdc9d3d5dea6d1418994aadabcf2db3dfeb0 100644 --- a/src/os/src/windows/wAtomic.c +++ b/source/os/src/osAtomic.c @@ -13,13 +13,9 @@ * along with this program. If not, see . */ -#define _DEFAULT_SOURCE #include "os.h" -#include "taosdef.h" -#include "tglobal.h" -#include "ttimer.h" -#include "tulog.h" -#include "tutil.h" + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) // add char interlocked_add_fetch_8(char volatile* ptr, char val) { @@ -162,3 +158,42 @@ __int64 interlocked_fetch_xor_64(__int64 volatile* ptr, __int64 val) { #endif } +#endif + +#ifdef _TD_NINGSI_60 +void* atomic_exchange_ptr_impl(void** ptr, void* val ) { + void *old; + do { + old = *ptr; + } while( !__sync_bool_compare_and_swap(ptr, old, val) ); + return old; +} +int8_t atomic_exchange_8_impl(int8_t* ptr, int8_t val ) { + int8_t old; + do { + old = *ptr; + } while( !__sync_bool_compare_and_swap(ptr, old, val) ); + return old; +} +int16_t atomic_exchange_16_impl(int16_t* ptr, int16_t val ) { + int16_t old; + do { + old = *ptr; + } while( !__sync_bool_compare_and_swap(ptr, old, val) ); + return old; +} +int32_t atomic_exchange_32_impl(int32_t* ptr, int32_t val ) { + int32_t old; + do { + old = *ptr; + } while( !__sync_bool_compare_and_swap(ptr, old, val) ); + return old; +} +int64_t atomic_exchange_64_impl(int64_t* ptr, int64_t val ) { + int64_t old; + do { + old = *ptr; + } while( !__sync_bool_compare_and_swap(ptr, old, val) ); + return old; +} +#endif diff --git a/src/os/src/detail/osDir.c b/source/os/src/osDir.c similarity index 52% rename from src/os/src/detail/osDir.c rename to source/os/src/osDir.c index c467c64872d4b660af5cebb19b017f1528b55055..0526dffe9e799616a0d1b091e2765b02807e64cd 100644 --- a/src/os/src/detail/osDir.c +++ b/source/os/src/osDir.c @@ -13,16 +13,29 @@ * along with this program. If not, see . */ -#define _DEFAULT_SOURCE #include "os.h" -#include "tglobal.h" -#include "tulog.h" -#include "zlib.h" +#include "osString.h" -#define COMPRESS_STEP_SIZE 163840 +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +/* + * windows implementation + */ + +// todo + +#else +/* + * linux implementation + */ -void taosRemoveDir(char *rootDir) { - DIR *dir = opendir(rootDir); +#include +#include +#include +#include +#include + +void taosRemoveDir(char *dirname) { + DIR *dir = opendir(dirname); if (dir == NULL) return; struct dirent *de = NULL; @@ -30,33 +43,34 @@ void taosRemoveDir(char *rootDir) { if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue; char filename[1024]; - snprintf(filename, 1023, "%s/%s", rootDir, de->d_name); + snprintf(filename, sizeof(filename), "%s/%s", dirname, de->d_name); if (de->d_type & DT_DIR) { taosRemoveDir(filename); } else { (void)remove(filename); - uInfo("file:%s is removed", filename); + printf("file:%s is removed", filename); } } closedir(dir); - rmdir(rootDir); + rmdir(dirname); - uInfo("dir:%s is removed", rootDir); + printf("dir:%s is removed", dirname); } -bool taosDirExist(const char* dirname) { - return access(dirname, F_OK) == 0; -} +bool taosDirExist(char *dirname) { return access(dirname, F_OK) == 0; } -int taosMkDir(const char *path, mode_t mode) { - int code = mkdir(path, 0755); - if (code < 0 && errno == EEXIST) code = 0; - return code; +bool taosMkDir(char *dirname) { + int32_t code = mkdir(dirname, 0755); + if (code < 0 && errno == EEXIST) { + return true; + } + + return code == 0; } -void taosRemoveOldLogFiles(char *rootDir, int32_t keepDays) { - DIR *dir = opendir(rootDir); +void taosRemoveOldFiles(char *dirname, int32_t keepDays) { + DIR *dir = opendir(dirname); if (dir == NULL) return; int64_t sec = taosGetTimestampSec(); @@ -66,7 +80,7 @@ void taosRemoveOldLogFiles(char *rootDir, int32_t keepDays) { if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue; char filename[1024]; - snprintf(filename, 1023, "%s/%s", rootDir, de->d_name); + snprintf(filename, sizeof(filename), "%s/%s", dirname, de->d_name); if (de->d_type & DT_DIR) { continue; } else { @@ -76,7 +90,7 @@ void taosRemoveOldLogFiles(char *rootDir, int32_t keepDays) { } int64_t fileSec = 0; - for (int i = len - 1; i >= 0; i--) { + for (int32_t i = len - 1; i >= 0; i--) { if (filename[i] == '.') { fileSec = atoll(filename + i + 1); break; @@ -87,56 +101,41 @@ void taosRemoveOldLogFiles(char *rootDir, int32_t keepDays) { int32_t days = (int32_t)(ABS(sec - fileSec) / 86400 + 1); if (days > keepDays) { (void)remove(filename); - uInfo("file:%s is removed, days:%d keepDays:%d", filename, days, keepDays); + printf("file:%s is removed, days:%d keepDays:%d", filename, days, keepDays); } else { - uTrace("file:%s won't be removed, days:%d keepDays:%d", filename, days, keepDays); + printf("file:%s won't be removed, days:%d keepDays:%d", filename, days, keepDays); } } } closedir(dir); - rmdir(rootDir); + rmdir(dirname); } -int32_t taosCompressFile(char *srcFileName, char *destFileName) { - int32_t ret = 0; - int32_t len = 0; - char * data = malloc(COMPRESS_STEP_SIZE); - FILE * srcFp = NULL; - gzFile dstFp = NULL; - - srcFp = fopen(srcFileName, "r"); - if (srcFp == NULL) { - ret = -1; - goto cmp_end; +bool taosExpandDir(char *dirname, char *outname, int32_t maxlen) { + wordexp_t full_path; + if (0 != wordexp(dirname, &full_path, 0)) { + printf("failed to expand path:%s since %s", dirname, strerror(errno)); + wordfree(&full_path); + return false; } - int32_t fd = open(destFileName, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRWXU | S_IRWXG | S_IRWXO); - if (fd < 0) { - ret = -2; - goto cmp_end; + if (full_path.we_wordv != NULL && full_path.we_wordv[0] != NULL) { + strncpy(outname, full_path.we_wordv[0], maxlen); } - dstFp = gzdopen(fd, "wb6f"); - if (dstFp == NULL) { - ret = -3; - close(fd); - goto cmp_end; - } + wordfree(&full_path); - while (!feof(srcFp)) { - len = (int32_t)fread(data, 1, COMPRESS_STEP_SIZE, srcFp); - (void)gzwrite(dstFp, data, len); - } + return true; +} -cmp_end: - if (srcFp) { - fclose(srcFp); - } - if (dstFp) { - gzclose(dstFp); +bool taosRealPath(char *dirname, int32_t maxlen) { + char tmp[PATH_MAX] = {0}; + if (realpath(dirname, tmp) != NULL) { + strncpy(dirname, tmp, maxlen); } - free(data); - return ret; + return true; } + +#endif \ No newline at end of file diff --git a/source/os/src/osEnv.c b/source/os/src/osEnv.c new file mode 100644 index 0000000000000000000000000000000000000000..7bc5e0a7fcfa759444d0d6d445a9b2eb1175f8e1 --- /dev/null +++ b/source/os/src/osEnv.c @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + + +#define _DEFAULT_SOURCE +#include "os.h" + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + +char tsOsName[10] = "Windows"; +char configDir[PATH_MAX] = "C:/TDengine/cfg"; +char tsDataDir[PATH_MAX] = "C:/TDengine/data"; +char tsLogDir[PATH_MAX] = "C:/TDengine/log"; +char tsScriptDir[PATH_MAX] = "C:/TDengine/script"; +char tsTempDir[PATH_MAX] = "C:\\Windows\\Temp"; + +extern taosWinSocketInit(); + +void osInit() { + taosWinSocketInit(); + + const char *tmpDir = getenv("tmp"); + if (tmpDir == NULL) { + tmpDir = getenv("temp"); + } + + if (tmpDir != NULL) { + strcpy(tsTempDir, tmpDir); + } +} + +#elif defined(_TD_DARWIN_64) + +char tsOsName[10] = "Darwin"; +char configDir[PATH_MAX] = "/usr/local/etc/taos"; +char tsDataDir[PATH_MAX] = "/usr/local/var/lib/taos"; +char tsLogDir[PATH_MAX] = "/usr/local/var/log/taos"; +char tsScriptDir[PATH_MAX] = "/usr/local/etc/taos"; +char tsTempDir[PATH_MAX] = "/tmp/taosd"; + +void osInit() {} + +#else + +char tsOsName[10] = "Linux"; +char configDir[PATH_MAX] = "/etc/taos"; +char tsDataDir[PATH_MAX] = "/var/lib/taos"; +char tsLogDir[PATH_MAX] = "/var/log/taos"; +char tsScriptDir[PATH_MAX] = "/etc/taos"; +char tsTempDir[PATH_MAX] = "/tmp/"; + +void osInit() {} + +#endif diff --git a/src/os/src/detail/osFile.c b/source/os/src/osFile.c similarity index 64% rename from src/os/src/detail/osFile.c rename to source/os/src/osFile.c index cc12968c72eef5b3970ca68cf660de502b402e1e..b5d30d7c25d462ef87a7bf5799dba3a1552df44e 100644 --- a/src/os/src/detail/osFile.c +++ b/source/os/src/osFile.c @@ -13,24 +13,45 @@ * along with this program. If not, see . */ -#define _DEFAULT_SOURCE #include "os.h" -#include "tglobal.h" -#include "tulog.h" -void taosClose(FileFd fd) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +#include + +#if defined(_MSDOS) +#define open _open +#endif + +#if defined(_WIN32) +extern int openA(const char *, int, ...); /* MsvcLibX ANSI version of open */ +extern int openU(const char *, int, ...); /* MsvcLibX UTF-8 version of open */ +#if defined(_UTF8_SOURCE) || defined(_BSD_SOURCE) || defined(_GNU_SOURCE) +#define open openU +#else /* _ANSI_SOURCE */ +#define open openA +#endif /* defined(_UTF8_SOURCE) */ +#endif /* defined(_WIN32) */ + +#else +#include +#include +#include +#include +#include +#endif + +void taosCloseFile(FileFd fd) { close(fd); fd = FD_INITIALIZER; } +void taosGetTmpfilePath(const char * inputTmpDir, const char *fileNamePrefix, char *dstPath) { #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) - -void taosGetTmpfilePath(const char *fileNamePrefix, char *dstPath) { const char *tdengineTmpFileNamePrefix = "tdengine-"; char tmpPath[PATH_MAX]; - int32_t len = (int32_t)strlen(tsTempDir); - memcpy(tmpPath, tsTempDir, len); + int32_t len = (int32_t)strlen(inputTmpDir); + memcpy(tmpPath, inputTmpDir, len); if (tmpPath[len - 1] != '/' && tmpPath[len - 1] != '\\') { tmpPath[len++] = '\\'; @@ -46,16 +67,14 @@ void taosGetTmpfilePath(const char *fileNamePrefix, char *dstPath) { char rand[8] = {0}; taosRandStr(rand, tListLen(rand) - 1); snprintf(dstPath, PATH_MAX, tmpPath, getpid(), rand); -} #else -void taosGetTmpfilePath(const char *fileNamePrefix, char *dstPath) { const char *tdengineTmpFileNamePrefix = "tdengine-"; - char tmpPath[PATH_MAX]; - int32_t len = strlen(tsTempDir); - memcpy(tmpPath, tsTempDir, len); + char tmpPath[PATH_MAX]; + int32_t len = strlen(inputTmpDir); + memcpy(tmpPath, inputTmpDir, len); static uint64_t seqId = 0; if (tmpPath[len - 1] != '/') { @@ -73,11 +92,11 @@ void taosGetTmpfilePath(const char *fileNamePrefix, char *dstPath) { sprintf(rand, "%" PRIu64, atomic_add_fetch_64(&seqId, 1)); snprintf(dstPath, PATH_MAX, tmpPath, getpid(), rand); -} #endif +} -int64_t taosRead(FileFd fd, void *buf, int64_t count) { +int64_t taosReadFile(FileFd fd, void *buf, int64_t count) { int64_t leftbytes = count; int64_t readbytes; char * tbuf = (char *)buf; @@ -101,7 +120,7 @@ int64_t taosRead(FileFd fd, void *buf, int64_t count) { return count; } -int64_t taosWrite(FileFd fd, void *buf, int64_t n) { +int64_t taosWriteFile(FileFd fd, void *buf, int64_t n) { int64_t nleft = n; int64_t nwritten = 0; char * tbuf = (char *)buf; @@ -121,42 +140,46 @@ int64_t taosWrite(FileFd fd, void *buf, int64_t n) { return n; } -int64_t taosLSeek(FileFd fd, int64_t offset, int32_t whence) { return (int64_t)lseek(fd, (long)offset, whence); } +int64_t taosLSeekFile(FileFd fd, int64_t offset, int32_t whence) { return (int64_t)lseek(fd, (long)offset, whence); } -int64_t taosCopy(char *from, char *to) { +int64_t taosCopyFile(char *from, char *to) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + return 0; +#else char buffer[4096]; int fidto = -1, fidfrom = -1; int64_t size = 0; int64_t bytes; - fidfrom = open(from, O_RDONLY | O_BINARY); + fidfrom = open(from, O_RDONLY); if (fidfrom < 0) goto _err; - fidto = open(to, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0755); + fidto = open(to, O_WRONLY | O_CREAT | O_EXCL, 0755); if (fidto < 0) goto _err; while (true) { - bytes = taosRead(fidfrom, buffer, sizeof(buffer)); + bytes = taosReadFile(fidfrom, buffer, sizeof(buffer)); if (bytes < 0) goto _err; if (bytes == 0) break; size += bytes; - if (taosWrite(fidto, (void *)buffer, bytes) < bytes) goto _err; + if (taosWriteFile(fidto, (void *)buffer, bytes) < bytes) goto _err; if (bytes < sizeof(buffer)) break; } - taosFsync(fidto); + taosFsyncFile(fidto); - taosClose(fidfrom); - taosClose(fidto); + taosCloseFile(fidfrom); + taosCloseFile(fidto); return size; _err: - if (fidfrom >= 0) taosClose(fidfrom); - if (fidto >= 0) taosClose(fidto); + if (fidfrom >= 0) taosCloseFile(fidfrom); + if (fidto >= 0) taosCloseFile(fidto); remove(to); return -1; +#endif } #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) @@ -306,9 +329,8 @@ int64_t taosFSendFile(FILE *outfile, FILE *infile, int64_t *offset, int64_t size #endif +int32_t taosFtruncateFile(FileFd fd, int64_t l_size) { #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) - -int32_t taosFtruncate(int32_t fd, int64_t l_size) { if (fd < 0) { errno = EBADF; uError("%s\n", "fd arg was negative"); @@ -357,9 +379,13 @@ int32_t taosFtruncate(int32_t fd, int64_t l_size) { } return 0; +#else + return ftruncate(fd, l_size); +#endif } -int32_t taosFsync(FileFd fd) { +int32_t taosFsyncFile(FileFd fd) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) if (fd < 0) { errno = EBADF; uError("%s\n", "fd arg was negative"); @@ -369,33 +395,143 @@ int32_t taosFsync(FileFd fd) { HANDLE h = (HANDLE)_get_osfhandle(fd); return FlushFileBuffers(h); +#else + return fsync(fd); +#endif } -int32_t taosRename(char *oldName, char *newName) { +int32_t taosRenameFile(char *oldName, char *newName) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) int32_t code = MoveFileEx(oldName, newName, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED); if (code < 0) { - uError("failed to rename file %s to %s, reason:%s", oldName, newName, strerror(errno)); - } else { - uTrace("successfully to rename file %s to %s", oldName, newName); + printf("failed to rename file %s to %s, reason:%s", oldName, newName, strerror(errno)); + } + + return code; +#else + int32_t code = rename(oldName, newName); + if (code < 0) { + printf("failed to rename file %s to %s, reason:%s", oldName, newName, strerror(errno)); } return code; +#endif } +int32_t taosLockFile(int32_t fd) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + return 0; #else + return (int32_t)flock(fd, LOCK_EX | LOCK_NB); +#endif +} + +int32_t taosUnLockFile(int32_t fd) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + return 0; +#else + return (int32_t)flock(fd, LOCK_UN | LOCK_NB); +#endif +} -int32_t taosFtruncate(FileFd fd, int64_t length) { return ftruncate(fd, length); } -int32_t taosFsync(FileFd fd) { return fsync(fd); } +int32_t taosUmaskFile(int32_t val) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + return 0; +#else + return umask(val); +#endif +} -int32_t taosRename(char *oldName, char *newName) { - int32_t code = rename(oldName, newName); +int32_t taosStatFile(const char *path, int64_t *size, int32_t *mtime) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + return 0; +#else + struct stat fileStat; + int32_t code = stat(path, &fileStat); if (code < 0) { - uError("failed to rename file %s to %s, reason:%s", oldName, newName, strerror(errno)); - } else { - uTrace("successfully to rename file %s to %s", oldName, newName); + return code; } - return code; + if (size != NULL) { + *size = fileStat.st_size; + } + + if (mtime != NULL) { + *mtime = fileStat.st_mtime; + } + + return 0; +#endif +} + +int32_t taosFStatFile(int32_t fd, int64_t *size, int32_t *mtime) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + return 0; +#else + struct stat fileStat; + int32_t code = fstat(fd, &fileStat); + if (code < 0) { + return code; + } + + if (size != NULL) { + *size = fileStat.st_size; + } + + if (mtime != NULL) { + *mtime = fileStat.st_mtime; + } + + return 0; +#endif +} + +int32_t taosOpenFileWrite(const char *path) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + return 0; +#else + return open(path, O_WRONLY, S_IRWXU | S_IRWXG | S_IRWXO); +#endif +} + +int32_t taosOpenFileCreateWrite(const char *path) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + return 0; +#else + return open(path, O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); +#endif +} + +int32_t taosOpenFileCreateWriteTrunc(const char *path) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + return 0; +#else + return open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO); +#endif +} + +int32_t taosOpenFileCreateWriteAppend(const char *path) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + return 0; +#else + return open(path, O_WRONLY | O_CREAT | O_APPEND, S_IRWXU | S_IRWXG | S_IRWXO); +#endif } +FileFd taosOpenFileRead(const char *path) { + #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + return 0; +#else + return open(path, O_RDONLY, S_IRWXU | S_IRWXG | S_IRWXO); #endif +} + +FileFd taosOpenFileReadWrite(const char *path) { + #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + return 0; +#else + return open(path, O_RDWR, S_IRWXU | S_IRWXG | S_IRWXO); +#endif +} + + diff --git a/src/os/src/windows/wLz4.c b/source/os/src/osLz4.c similarity index 94% rename from src/os/src/windows/wLz4.c rename to source/os/src/osLz4.c index 631a22e5727695499febffd0ff212532f88413e1..29d219f37b879abe675508af5b26acec0abadddd 100644 --- a/src/os/src/windows/wLz4.c +++ b/source/os/src/osLz4.c @@ -15,11 +15,12 @@ #define _DEFAULT_SOURCE #include "os.h" -#include "taosdef.h" -#include "tglobal.h" -#include "ttimer.h" -#include "tulog.h" -#include "tutil.h" + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + +/* + * windows implementation + */ unsigned char _MyBitScanForward64(unsigned long *ret, uint64_t x) { unsigned long x0 = (unsigned long)x, top, bottom; @@ -67,4 +68,6 @@ int32_t BUILDIN_CTZ(uint32_t val) { unsigned long r = 0; _BitScanForward(&r, val); return (int)(r >> 3); -} \ No newline at end of file +} + +#endif diff --git a/src/os/src/detail/osRand.c b/source/os/src/osRand.c similarity index 91% rename from src/os/src/detail/osRand.c rename to source/os/src/osRand.c index 0dda908bb35c68513dba150e8380846c36aa2893..973323a346b4d49179abf88a5c3f1950e688d8ad 100644 --- a/src/os/src/detail/osRand.c +++ b/source/os/src/osRand.c @@ -15,6 +15,11 @@ #define _DEFAULT_SOURCE #include "os.h" +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +#else +#include +#include +#endif uint32_t taosRand(void) { return rand(); } diff --git a/src/os/src/darwin/dwSemaphore.c b/source/os/src/osSemaphore.c similarity index 66% rename from src/os/src/darwin/dwSemaphore.c rename to source/os/src/osSemaphore.c index 25cb28cff1b6f9c83cab43faf68641717450c0ea..2385d102858393ef426ac54d4999c413a3f639a5 100644 --- a/src/os/src/darwin/dwSemaphore.c +++ b/source/os/src/osSemaphore.c @@ -13,13 +13,66 @@ * along with this program. If not, see . */ -// fail-fast or let-it-crash philosophy -// https://en.wikipedia.org/wiki/Fail-fast -// https://stackoverflow.com/questions/4393197/erlangs-let-it-crash-philosophy-applicable-elsewhere -// experimentally, we follow log-and-crash here - #define _DEFAULT_SOURCE #include "os.h" + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + +/* + * windows implementation + */ + +#include + +bool taosCheckPthreadValid(pthread_t thread) { return thread.p != NULL; } + +void taosResetPthread(pthread_t* thread) { thread->p = 0; } + +int64_t taosGetPthreadId(pthread_t thread) { +#ifdef PTW32_VERSION + return pthread_getw32threadid_np(thread); +#else + return (int64_t)thread; +#endif +} + +int64_t taosGetSelfPthreadId() { return GetCurrentThreadId(); } + +bool taosComparePthread(pthread_t first, pthread_t second) { return first.p == second.p; } + +int32_t taosGetPId() { return GetCurrentProcessId(); } + +int32_t taosGetCurrentAPPName(char* name, int32_t* len) { + char filepath[1024] = {0}; + + GetModuleFileName(NULL, filepath, MAX_PATH); + char* sub = strrchr(filepath, '.'); + if (sub != NULL) { + *sub = '\0'; + } + strcpy(name, filepath); + + if (len != NULL) { + *len = (int32_t)strlen(filepath); + } + + return 0; +} + +int32_t tsem_wait(tsem_t* sem) { + int ret = 0; + do { + ret = sem_wait(sem); + } while (ret != 0 && errno == EINTR); + return ret; +} + +#elif defined(_TD_DARWIN_64) + +/* + * darwin implementation + */ + #include // #define SEM_USE_PTHREAD @@ -27,19 +80,19 @@ #define SEM_USE_SEM #ifdef SEM_USE_SEM -#include #include +#include #include #include #include -static pthread_t sem_thread; -static pthread_once_t sem_once; -static task_t sem_port; -static volatile int sem_inited = 0; -static semaphore_t sem_exit; +static pthread_t sem_thread; +static pthread_once_t sem_once; +static task_t sem_port; +static volatile int sem_inited = 0; +static semaphore_t sem_exit; -static void* sem_thread_routine(void *arg) { +static void *sem_thread_routine(void *arg) { (void)arg; setThreadName("sem_thrd"); @@ -62,7 +115,7 @@ static void once_init(void) { fprintf(stderr, "==%s[%d]%s()==failed to create thread\n", basename(__FILE__), __LINE__, __func__); return; } - while (sem_inited==0) { + while (sem_inited == 0) { ; } } @@ -70,19 +123,19 @@ static void once_init(void) { struct tsem_s { #ifdef SEM_USE_PTHREAD - pthread_mutex_t lock; - pthread_cond_t cond; - volatile int64_t val; + pthread_mutex_t lock; + pthread_cond_t cond; + volatile int64_t val; #elif defined(SEM_USE_POSIX) - size_t id; - sem_t *sem; + size_t id; + sem_t * sem; #elif defined(SEM_USE_SEM) - semaphore_t sem; -#else // SEM_USE_PTHREAD - dispatch_semaphore_t sem; -#endif // SEM_USE_PTHREAD + semaphore_t sem; +#else // SEM_USE_PTHREAD + dispatch_semaphore_t sem; +#endif // SEM_USE_PTHREAD - volatile unsigned int valid:1; + volatile unsigned int valid : 1; }; int tsem_init(tsem_t *sem, int pshared, unsigned int value) { @@ -91,7 +144,7 @@ int tsem_init(tsem_t *sem, int pshared, unsigned int value) { fprintf(stderr, "==%s[%d]%s():[%p]==already initialized\n", basename(__FILE__), __LINE__, __func__, sem); abort(); } - struct tsem_s *p = (struct tsem_s*)calloc(1, sizeof(*p)); + struct tsem_s *p = (struct tsem_s *)calloc(1, sizeof(*p)); if (!p) { fprintf(stderr, "==%s[%d]%s():[%p]==out of memory\n", basename(__FILE__), __LINE__, __func__, sem); abort(); @@ -116,24 +169,25 @@ int tsem_init(tsem_t *sem, int pshared, unsigned int value) { static size_t tick = 0; do { size_t id = atomic_add_fetch_64(&tick, 1); - if (id==SEM_VALUE_MAX) { + if (id == SEM_VALUE_MAX) { atomic_store_64(&tick, 0); id = 0; } - char name[NAME_MAX-4]; + char name[NAME_MAX - 4]; snprintf(name, sizeof(name), "/t%ld", id); - p->sem = sem_open(name, O_CREAT|O_EXCL, pshared, value); - p->id = id; - if (p->sem!=SEM_FAILED) break; + p->sem = sem_open(name, O_CREAT | O_EXCL, pshared, value); + p->id = id; + if (p->sem != SEM_FAILED) break; int e = errno; - if (e==EEXIST) continue; - if (e==EINTR) continue; - fprintf(stderr, "==%s[%d]%s():[%p]==not created[%d]%s\n", basename(__FILE__), __LINE__, __func__, sem, e, strerror(e)); + if (e == EEXIST) continue; + if (e == EINTR) continue; + fprintf(stderr, "==%s[%d]%s():[%p]==not created[%d]%s\n", basename(__FILE__), __LINE__, __func__, sem, e, + strerror(e)); abort(); - } while (p->sem==SEM_FAILED); + } while (p->sem == SEM_FAILED); #elif defined(SEM_USE_SEM) pthread_once(&sem_once, once_init); - if (sem_inited!=1) { + if (sem_inited != 1) { fprintf(stderr, "==%s[%d]%s():[%p]==internal resource init failed\n", basename(__FILE__), __LINE__, __func__, sem); errno = ENOMEM; return -1; @@ -144,13 +198,13 @@ int tsem_init(tsem_t *sem, int pshared, unsigned int value) { // we fail-fast here, because we have less-doc about semaphore_create for the moment abort(); } -#else // SEM_USE_PTHREAD +#else // SEM_USE_PTHREAD p->sem = dispatch_semaphore_create(value); if (p->sem == NULL) { fprintf(stderr, "==%s[%d]%s():[%p]==not created\n", basename(__FILE__), __LINE__, __func__, sem); abort(); } -#endif // SEM_USE_PTHREAD +#endif // SEM_USE_PTHREAD p->valid = 1; @@ -190,9 +244,9 @@ int tsem_wait(tsem_t *sem) { return sem_wait(p->sem); #elif defined(SEM_USE_SEM) return semaphore_wait(p->sem); -#else // SEM_USE_PTHREAD +#else // SEM_USE_PTHREAD return dispatch_semaphore_wait(p->sem, DISPATCH_TIME_FOREVER); -#endif // SEM_USE_PTHREAD +#endif // SEM_USE_PTHREAD } int tsem_post(tsem_t *sem) { @@ -226,9 +280,9 @@ int tsem_post(tsem_t *sem) { return sem_post(p->sem); #elif defined(SEM_USE_SEM) return semaphore_signal(p->sem); -#else // SEM_USE_PTHREAD +#else // SEM_USE_PTHREAD return dispatch_semaphore_signal(p->sem); -#endif // SEM_USE_PTHREAD +#endif // SEM_USE_PTHREAD } int tsem_destroy(tsem_t *sem) { @@ -263,18 +317,19 @@ int tsem_destroy(tsem_t *sem) { abort(); } #elif defined(SEM_USE_POSIX) - char name[NAME_MAX-4]; + char name[NAME_MAX - 4]; snprintf(name, sizeof(name), "/t%ld", p->id); int r = sem_unlink(name); if (r) { int e = errno; - fprintf(stderr, "==%s[%d]%s():[%p]==unlink failed[%d]%s\n", basename(__FILE__), __LINE__, __func__, sem, e, strerror(e)); + fprintf(stderr, "==%s[%d]%s():[%p]==unlink failed[%d]%s\n", basename(__FILE__), __LINE__, __func__, sem, e, + strerror(e)); abort(); } #elif defined(SEM_USE_SEM) semaphore_destroy(sem_port, p->sem); -#else // SEM_USE_PTHREAD -#endif // SEM_USE_PTHREAD +#else // SEM_USE_PTHREAD +#endif // SEM_USE_PTHREAD p->valid = 0; free(p); @@ -285,36 +340,28 @@ int tsem_destroy(tsem_t *sem) { bool taosCheckPthreadValid(pthread_t thread) { uint64_t id = 0; - int r = pthread_threadid_np(thread, &id); + int r = pthread_threadid_np(thread, &id); return r ? false : true; } int64_t taosGetSelfPthreadId() { uint64_t id; pthread_threadid_np(0, &id); - return (int64_t) id; + return (int64_t)id; } -int64_t taosGetPthreadId(pthread_t thread) { - return (int64_t)thread; -} +int64_t taosGetPthreadId(pthread_t thread) { return (int64_t)thread; } -void taosResetPthread(pthread_t* thread) { - *thread = NULL; -} +void taosResetPthread(pthread_t *thread) { *thread = NULL; } -bool taosComparePthread(pthread_t first, pthread_t second) { - return pthread_equal(first, second) ? true : false; -} +bool taosComparePthread(pthread_t first, pthread_t second) { return pthread_equal(first, second) ? true : false; } -int32_t taosGetPId() { - return (int32_t)getpid(); -} +int32_t taosGetPId() { return (int32_t)getpid(); } -int32_t taosGetCurrentAPPName(char* name, int32_t* len) { - char buf[PATH_MAX+1]; +int32_t taosGetCurrentAPPName(char *name, int32_t *len) { + char buf[PATH_MAX + 1]; buf[0] = '\0'; - proc_name(getpid(), buf, sizeof(buf)-1); + proc_name(getpid(), buf, sizeof(buf) - 1); buf[PATH_MAX] = '\0'; size_t n = strlen(buf); if (len) *len = n; @@ -322,4 +369,60 @@ int32_t taosGetCurrentAPPName(char* name, int32_t* len) { return 0; } +#else + +/* + * linux implementation + */ + +#include +#include + +bool taosCheckPthreadValid(pthread_t thread) { return thread != 0; } + +int64_t taosGetSelfPthreadId() { + static __thread int id = 0; + if (id != 0) return id; + id = syscall(SYS_gettid); + return id; +} + +int64_t taosGetPthreadId(pthread_t thread) { return (int64_t)thread; } +void taosResetPthread(pthread_t* thread) { *thread = 0; } +bool taosComparePthread(pthread_t first, pthread_t second) { return first == second; } +int32_t taosGetPId() { return getpid(); } + +int32_t taosGetCurrentAPPName(char* name, int32_t* len) { + const char* self = "/proc/self/exe"; + char path[PATH_MAX] = {0}; + if (readlink(self, path, PATH_MAX) <= 0) { + return -1; + } + + path[PATH_MAX - 1] = 0; + char* end = strrchr(path, '/'); + if (end == NULL) { + return -1; + } + + ++end; + + strcpy(name, end); + + if (len != NULL) { + *len = strlen(name); + } + + return 0; +} + +int32_t tsem_wait(tsem_t* sem) { + int ret = 0; + do { + ret = sem_wait(sem); + } while (ret != 0 && errno == EINTR); + return ret; +} + +#endif diff --git a/src/os/src/windows/wSignal.c b/source/os/src/osSignal.c similarity index 64% rename from src/os/src/windows/wSignal.c rename to source/os/src/osSignal.c index 9de6b5f3434dc792c40b330ec37c7edd180b999e..12721a17f55a3f716c4a65f41121280d6c56cbe3 100644 --- a/src/os/src/windows/wSignal.c +++ b/source/os/src/osSignal.c @@ -15,7 +15,13 @@ #define _DEFAULT_SOURCE #include "os.h" -#include + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + +/* + * windows implementation + */ + #include typedef void (*FWinSignalHandler)(int32_t signum); @@ -27,7 +33,7 @@ void taosSetSignal(int32_t signum, FSignalHandler sigfp) { if (signum == SIGHUP) { SetConsoleCtrlHandler((PHANDLER_ROUTINE)sigfp, TRUE); } else { - signal(signum, (FWinSignalHandler)sigfp); + signal(signum, (FWinSignalHandler)sigfp); } } @@ -40,3 +46,29 @@ void taosDflSignal(int32_t signum) { if (signum == SIGUSR1 || signum == SIGHUP) return; signal(signum, SIG_DFL); } + +#else + +/* + * linux and darwin implementation + */ + +typedef void (*FLinuxSignalHandler)(int32_t signum, siginfo_t *sigInfo, void *context); + +void taosSetSignal(int32_t signum, FSignalHandler sigfp) { + struct sigaction act; + memset(&act, 0, sizeof(act)); +#if 1 + act.sa_flags = SA_SIGINFO; + act.sa_sigaction = (FLinuxSignalHandler)sigfp; +#else + act.sa_handler = sigfp; +#endif + sigaction(signum, &act, NULL); +} + +void taosIgnSignal(int32_t signum) { signal(signum, SIG_IGN); } + +void taosDflSignal(int32_t signum) { signal(signum, SIG_DFL); } + +#endif diff --git a/src/os/src/detail/osSleep.c b/source/os/src/osSleep.c similarity index 98% rename from src/os/src/detail/osSleep.c rename to source/os/src/osSleep.c index 9c3231fd13064fc1d3d072d1a20d205b48e56ec6..3b90fbdad88059e370673b150070785823eec571 100644 --- a/src/os/src/detail/osSleep.c +++ b/source/os/src/osSleep.c @@ -22,6 +22,8 @@ void taosMsleep(int32_t ms) { Sleep(ms); } #else +#include + /* to make taosMsleep work, signal SIGALRM shall be blocked in the calling thread, diff --git a/src/util/src/tsocket.c b/source/os/src/osSocket.c similarity index 59% rename from src/util/src/tsocket.c rename to source/os/src/osSocket.c index 8d69a87e77bff594e7a99b2a63d4d849214eebe9..fb1aeebe1c7b6c34cf2787a61708ba2568f29e16 100644 --- a/src/util/src/tsocket.c +++ b/source/os/src/osSocket.c @@ -13,10 +13,259 @@ * along with this program. If not, see . */ +#define _DEFAULT_SOURCE #include "os.h" -#include "tulog.h" -#include "tsocket.h" -#include "taoserror.h" + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + #include "winsock2.h" + #include + #include + #include +#else + #include + #include + #include + #include + #include + #include + #include + #include + #include +#endif + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + +#define taosSend(sockfd, buf, len, flags) send((SOCKET)sockfd, buf, len, flags) +int32_t taosSendto(SocketFd fd, void *buf, int len, unsigned int flags, const struct sockaddr *to, int tolen) { + return sendto((SOCKET)sockfd, buf, len, flags, dest_addr, addrlen); +} +int32_t taosWriteSocket(SocketFd fd, void *buf, int len) { return send((SOCKET)fd, buf, len, 0); } +int32_t taosReadSocket(SocketFd fd, void *buf, int len) { return recv((SOCKET)fd, buf, len, 0)(); } +int32_t taosCloseSocketNoCheck(SocketFd fd) { return closesocket((SOCKET)fd); } +int32_t taosCloseSocket(SocketFd fd) { closesocket((SOCKET)fd) } + +#else + + #define taosSend(sockfd, buf, len, flags) send(sockfd, buf, len, flags) + int32_t taosSendto(SocketFd fd, void * buf, int len, unsigned int flags, const struct sockaddr * dest_addr, int addrlen) { + return sendto(fd, buf, len, flags, dest_addr, addrlen); + } + + int32_t taosWriteSocket(SocketFd fd, void *buf, int len) { + return write(fd, buf, len); + } + + int32_t taosReadSocket(SocketFd fd, void *buf, int len) { + return read(fd, buf, len); + } + + int32_t taosCloseSocketNoCheck(SocketFd fd) { + return close(fd); + } + + int32_t taosCloseSocket(SocketFd fd) { + if (fd > -1) { + close(fd); + } + } +#endif + +void taosShutDownSocketRD(SOCKET fd) { +#ifdef WINDOWS + closesocket(fd); +#elif __APPLE__ + close(fd); +#else + shutdown(fd, SHUT_RD); +#endif +} + +void taosShutDownSocketWR(SOCKET fd) { +#ifdef WINDOWS + closesocket(fd); +#elif __APPLE__ + close(fd); +#else + shutdown(fd, SHUT_WR); +#endif +} + +#if !(defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)) + +int32_t taosSetNonblocking(SOCKET sock, int32_t on) { + int32_t flags = 0; + if ((flags = fcntl(sock, F_GETFL, 0)) < 0) { + printf("fcntl(F_GETFL) error: %d (%s)\n", errno, strerror(errno)); + return 1; + } + + if (on) + flags |= O_NONBLOCK; + else + flags &= ~O_NONBLOCK; + + if ((flags = fcntl(sock, F_SETFL, flags)) < 0) { + printf("fcntl(F_SETFL) error: %d (%s)\n", errno, strerror(errno)); + return 1; + } + + return 0; +} + +void taosIgnSIGPIPE() { signal(SIGPIPE, SIG_IGN); } + +void taosBlockSIGPIPE() { + sigset_t signal_mask; + sigemptyset(&signal_mask); + sigaddset(&signal_mask, SIGPIPE); + int32_t rc = pthread_sigmask(SIG_BLOCK, &signal_mask, NULL); + if (rc != 0) { + printf("failed to block SIGPIPE"); + } +} + +void taosSetMaskSIGPIPE() { + sigset_t signal_mask; + sigemptyset(&signal_mask); + sigaddset(&signal_mask, SIGPIPE); + int32_t rc = pthread_sigmask(SIG_SETMASK, &signal_mask, NULL); + if (rc != 0) { + printf("failed to setmask SIGPIPE"); + } +} + +#endif + +#if !(defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) || defined(_TD_DARWIN_32)) + +int32_t taosSetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *optval, int32_t optlen) { + return setsockopt(socketfd, level, optname, optval, (socklen_t)optlen); +} + +int32_t taosGetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *optval, int32_t *optlen) { + return getsockopt(socketfd, level, optname, optval, (socklen_t *)optlen); +} + +#endif + +#if !((defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)) && defined(_MSC_VER)) + +uint32_t taosInetAddr(char *ipAddr) { return inet_addr(ipAddr); } + +const char *taosInetNtoa(struct in_addr ipInt) { return inet_ntoa(ipInt); } + +#endif + +#if defined(_TD_DARWIN_64) + +/* + * darwin implementation + */ + +int taosSetSockOpt(SOCKET socketfd, int level, int optname, void *optval, int optlen) { + if (level == SOL_SOCKET && optname == SO_SNDBUF) { + return 0; + } + + if (level == SOL_SOCKET && optname == SO_RCVBUF) { + return 0; + } + + return setsockopt(socketfd, level, optname, optval, (socklen_t)optlen); +} +#endif + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + +/* + * windows implementation + */ + +#include +#include +#include +#include +#include +#include +#include + +void taosWinSocketInit() { + static char flag = 0; + if (flag == 0) { + WORD wVersionRequested; + WSADATA wsaData; + wVersionRequested = MAKEWORD(1, 1); + if (WSAStartup(wVersionRequested, &wsaData) == 0) { + flag = 1; + } + } +} + +int32_t taosSetNonblocking(SOCKET sock, int32_t on) { + u_long mode; + if (on) { + mode = 1; + ioctlsocket(sock, FIONBIO, &mode); + } else { + mode = 0; + ioctlsocket(sock, FIONBIO, &mode); + } + return 0; +} + +void taosIgnSIGPIPE() {} +void taosBlockSIGPIPE() {} +void taosSetMaskSIGPIPE() {} + +int32_t taosSetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *optval, int32_t optlen) { + if (level == SOL_SOCKET && optname == TCP_KEEPCNT) { + return 0; + } + + if (level == SOL_TCP && optname == TCP_KEEPIDLE) { + return 0; + } + + if (level == SOL_TCP && optname == TCP_KEEPINTVL) { + return 0; + } + + if (level == SOL_TCP && optname == TCP_KEEPCNT) { + return 0; + } + + return setsockopt(socketfd, level, optname, optval, optlen); +} + +#ifdef _MSC_VER +//#if _MSC_VER >= 1900 + +uint32_t taosInetAddr(char *ipAddr) { + uint32_t value; + int32_t ret = inet_pton(AF_INET, ipAddr, &value); + if (ret <= 0) { + return INADDR_NONE; + } else { + return value; + } +} + +const char *taosInetNtoa(struct in_addr ipInt) { + // not thread safe, only for debug usage while print log + static char tmpDstStr[16]; + return inet_ntop(AF_INET, &ipInt, tmpDstStr, INET6_ADDRSTRLEN); +} + +//#endif +#endif + +#if defined(_TD_GO_DLL_) + +uint64_t htonll(uint64_t val) { return (((uint64_t)htonl(val)) << 32) + htonl(val >> 32); } + +#endif + +#endif #ifndef SIGPIPE #define SIGPIPE EPIPE @@ -28,7 +277,7 @@ int32_t taosGetFqdn(char *fqdn) { char hostname[1024]; hostname[1023] = '\0'; if (gethostname(hostname, 1023) == -1) { - uError("failed to get hostname, reason:%s", strerror(errno)); + printf("failed to get hostname, reason:%s", strerror(errno)); return -1; } @@ -45,7 +294,7 @@ int32_t taosGetFqdn(char *fqdn) { #endif // __APPLE__ int32_t ret = getaddrinfo(hostname, NULL, &hints, &result); if (!result) { - uError("failed to get fqdn, code:%d, reason:%s", ret, gai_strerror(ret)); + printf("failed to get fqdn, code:%d, reason:%s", ret, gai_strerror(ret)); return -1; } @@ -77,13 +326,12 @@ uint32_t taosGetIpv4FromFqdn(const char *fqdn) { } else { #ifdef EAI_SYSTEM if (ret == EAI_SYSTEM) { - uError("failed to get the ip address, fqdn:%s, since:%s", fqdn, strerror(errno)); - terrno = TAOS_SYSTEM_ERROR(errno); + printf("failed to get the ip address, fqdn:%s, since:%s", fqdn, strerror(errno)); } else { - uError("failed to get the ip address, fqdn:%s, since:%s", fqdn, gai_strerror(ret)); + printf("failed to get the ip address, fqdn:%s, since:%s", fqdn, gai_strerror(ret)); } #else - uError("failed to get the ip address, fqdn:%s, since:%s", fqdn, gai_strerror(ret)); + printf("failed to get the ip address, fqdn:%s, since:%s", fqdn, gai_strerror(ret)); #endif return 0xFFFFFFFF; } @@ -189,13 +437,13 @@ int32_t taosNonblockwrite(SOCKET fd, char *ptr, int32_t nbytes) { FD_SET(fd, &fset); if ((nready = select((int32_t)(fd + 1), NULL, &fset, NULL, &tv)) == 0) { errno = ETIMEDOUT; - uError("fd %d timeout, no enough space to write", fd); + printf("fd %d timeout, no enough space to write", fd); break; } else if (nready < 0) { if (errno == EINTR) continue; - uError("select error, %d (%s)", errno, strerror(errno)); + printf("select error, %d (%s)", errno, strerror(errno)); return -1; } @@ -203,7 +451,7 @@ int32_t taosNonblockwrite(SOCKET fd, char *ptr, int32_t nbytes) { if (nwritten <= 0) { if (errno == EAGAIN || errno == EINTR) continue; - uError("write error, %d (%s)", errno, strerror(errno)); + printf("write error, %d (%s)", errno, strerror(errno)); return -1; } @@ -229,21 +477,21 @@ int32_t taosReadn(SOCKET fd, char *ptr, int32_t nbytes) { FD_SET(fd, &fset); if ((nready = select((int32_t)(fd + 1), NULL, &fset, NULL, &tv)) == 0) { errno = ETIMEDOUT; - uError("fd %d timeout\n", fd); + printf("fd %d timeout\n", fd); break; } else if (nready < 0) { if (errno == EINTR) continue; - uError("select error, %d (%s)", errno, strerror(errno)); + printf("select error, %d (%s)", errno, strerror(errno)); return -1; } if ((nread = (int32_t)taosReadSocket(fd, ptr, (size_t)nleft)) < 0) { if (errno == EINTR) continue; - uError("read error, %d (%s)", errno, strerror(errno)); + printf("read error, %d (%s)", errno, strerror(errno)); return -1; } else if (nread == 0) { - uError("fd %d EOF", fd); + printf("fd %d EOF", fd); break; // EOF } @@ -259,7 +507,7 @@ SOCKET taosOpenUdpSocket(uint32_t ip, uint16_t port) { SOCKET sockFd; int32_t bufSize = 1024000; - uDebug("open udp socket:0x%x:%hu", ip, port); + printf("open udp socket:0x%x:%hu", ip, port); memset((char *)&localAddr, 0, sizeof(localAddr)); localAddr.sin_family = AF_INET; @@ -267,26 +515,26 @@ SOCKET taosOpenUdpSocket(uint32_t ip, uint16_t port) { localAddr.sin_port = (uint16_t)htons(port); if ((sockFd = socket(AF_INET, SOCK_DGRAM, 0)) <= 2) { - uError("failed to open udp socket: %d (%s)", errno, strerror(errno)); + printf("failed to open udp socket: %d (%s)", errno, strerror(errno)); taosCloseSocketNoCheck(sockFd); return -1; } if (taosSetSockOpt(sockFd, SOL_SOCKET, SO_SNDBUF, (void *)&bufSize, sizeof(bufSize)) != 0) { - uError("failed to set the send buffer size for UDP socket\n"); + printf("failed to set the send buffer size for UDP socket\n"); taosCloseSocket(sockFd); return -1; } if (taosSetSockOpt(sockFd, SOL_SOCKET, SO_RCVBUF, (void *)&bufSize, sizeof(bufSize)) != 0) { - uError("failed to set the receive buffer size for UDP socket\n"); + printf("failed to set the receive buffer size for UDP socket\n"); taosCloseSocket(sockFd); return -1; } /* bind socket to local address */ if (bind(sockFd, (struct sockaddr *)&localAddr, sizeof(localAddr)) < 0) { - uError("failed to bind udp socket: %d (%s), 0x%x:%hu", errno, strerror(errno), ip, port); + printf("failed to bind udp socket: %d (%s), 0x%x:%hu", errno, strerror(errno), ip, port); taosCloseSocket(sockFd); return -1; } @@ -303,7 +551,7 @@ SOCKET taosOpenTcpClientSocket(uint32_t destIp, uint16_t destPort, uint32_t clie sockFd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (sockFd <= 2) { - uError("failed to open the socket: %d (%s)", errno, strerror(errno)); + printf("failed to open the socket: %d (%s)", errno, strerror(errno)); taosCloseSocketNoCheck(sockFd); return -1; } @@ -311,19 +559,19 @@ SOCKET taosOpenTcpClientSocket(uint32_t destIp, uint16_t destPort, uint32_t clie /* set REUSEADDR option, so the portnumber can be re-used */ int32_t reuse = 1; if (taosSetSockOpt(sockFd, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(reuse)) < 0) { - uError("setsockopt SO_REUSEADDR failed: %d (%s)", errno, strerror(errno)); + printf("setsockopt SO_REUSEADDR failed: %d (%s)", errno, strerror(errno)); taosCloseSocket(sockFd); return -1; } if (taosSetSockOpt(sockFd, SOL_SOCKET, SO_SNDBUF, (void *)&bufSize, sizeof(bufSize)) != 0) { - uError("failed to set the send buffer size for TCP socket\n"); + printf("failed to set the send buffer size for TCP socket\n"); taosCloseSocket(sockFd); return -1; } if (taosSetSockOpt(sockFd, SOL_SOCKET, SO_RCVBUF, (void *)&bufSize, sizeof(bufSize)) != 0) { - uError("failed to set the receive buffer size for TCP socket\n"); + printf("failed to set the receive buffer size for TCP socket\n"); taosCloseSocket(sockFd); return -1; } @@ -336,7 +584,7 @@ SOCKET taosOpenTcpClientSocket(uint32_t destIp, uint16_t destPort, uint32_t clie /* bind socket to client address */ if (bind(sockFd, (struct sockaddr *)&clientAddr, sizeof(clientAddr)) < 0) { - uError("bind tcp client socket failed, client(0x%x:0), dest(0x%x:%d), reason:(%s)", clientIp, destIp, destPort, + printf("bind tcp client socket failed, client(0x%x:0), dest(0x%x:%d), reason:(%s)", clientIp, destIp, destPort, strerror(errno)); taosCloseSocket(sockFd); return -1; @@ -353,7 +601,7 @@ SOCKET taosOpenTcpClientSocket(uint32_t destIp, uint16_t destPort, uint32_t clie ret = connect(sockFd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)); if (ret == -1) { if (errno == EHOSTUNREACH) { - uError("failed to connect socket, ip:0x%x, port:%hu(%s)", destIp, destPort, strerror(errno)); + printf("failed to connect socket, ip:0x%x, port:%hu(%s)", destIp, destPort, strerror(errno)); taosCloseSocket(sockFd); return -1; } else if (errno == EINPROGRESS || errno == EAGAIN || errno == EWOULDBLOCK) { @@ -364,19 +612,19 @@ SOCKET taosOpenTcpClientSocket(uint32_t destIp, uint16_t destPort, uint32_t clie int res = poll(wfd, 1, TCP_CONN_TIMEOUT); if (res == -1 || res == 0) { - uError("failed to connect socket, ip:0x%x, port:%hu(poll error/conn timeout)", destIp, destPort); + printf("failed to connect socket, ip:0x%x, port:%hu(poll error/conn timeout)", destIp, destPort); taosCloseSocket(sockFd); // return -1; } int optVal = -1, optLen = sizeof(int); if ((0 != taosGetSockOpt(sockFd, SOL_SOCKET, SO_ERROR, &optVal, &optLen)) || (optVal != 0)) { - uError("failed to connect socket, ip:0x%x, port:%hu(connect host error)", destIp, destPort); + printf("failed to connect socket, ip:0x%x, port:%hu(connect host error)", destIp, destPort); taosCloseSocket(sockFd); // return -1; } ret = 0; } else { // Other error - uError("failed to connect socket, ip:0x%x, port:%hu(target host cannot be reached)", destIp, destPort); + printf("failed to connect socket, ip:0x%x, port:%hu(target host cannot be reached)", destIp, destPort); taosCloseSocket(sockFd); // return -1; } @@ -388,7 +636,7 @@ SOCKET taosOpenTcpClientSocket(uint32_t destIp, uint16_t destPort, uint32_t clie #endif if (ret != 0) { - uError("failed to connect socket, ip:0x%x, port:%hu(%s)", destIp, destPort, strerror(errno)); + printf("failed to connect socket, ip:0x%x, port:%hu(%s)", destIp, destPort, strerror(errno)); taosCloseSocket(sockFd); sockFd = -1; } else { @@ -401,7 +649,7 @@ SOCKET taosOpenTcpClientSocket(uint32_t destIp, uint16_t destPort, uint32_t clie int32_t taosKeepTcpAlive(SOCKET sockFd) { int32_t alive = 1; if (taosSetSockOpt(sockFd, SOL_SOCKET, SO_KEEPALIVE, (void *)&alive, sizeof(alive)) < 0) { - uError("fd:%d setsockopt SO_KEEPALIVE failed: %d (%s)", sockFd, errno, strerror(errno)); + printf("fd:%d setsockopt SO_KEEPALIVE failed: %d (%s)", sockFd, errno, strerror(errno)); taosCloseSocket(sockFd); return -1; } @@ -410,21 +658,21 @@ int32_t taosKeepTcpAlive(SOCKET sockFd) { // all fails on macosx int32_t probes = 3; if (taosSetSockOpt(sockFd, SOL_TCP, TCP_KEEPCNT, (void *)&probes, sizeof(probes)) < 0) { - uError("fd:%d setsockopt SO_KEEPCNT failed: %d (%s)", sockFd, errno, strerror(errno)); + printf("fd:%d setsockopt SO_KEEPCNT failed: %d (%s)", sockFd, errno, strerror(errno)); taosCloseSocket(sockFd); return -1; } int32_t alivetime = 10; if (taosSetSockOpt(sockFd, SOL_TCP, TCP_KEEPIDLE, (void *)&alivetime, sizeof(alivetime)) < 0) { - uError("fd:%d setsockopt SO_KEEPIDLE failed: %d (%s)", sockFd, errno, strerror(errno)); + printf("fd:%d setsockopt SO_KEEPIDLE failed: %d (%s)", sockFd, errno, strerror(errno)); taosCloseSocket(sockFd); return -1; } int32_t interval = 3; if (taosSetSockOpt(sockFd, SOL_TCP, TCP_KEEPINTVL, (void *)&interval, sizeof(interval)) < 0) { - uError("fd:%d setsockopt SO_KEEPINTVL failed: %d (%s)", sockFd, errno, strerror(errno)); + printf("fd:%d setsockopt SO_KEEPINTVL failed: %d (%s)", sockFd, errno, strerror(errno)); taosCloseSocket(sockFd); return -1; } @@ -432,7 +680,7 @@ int32_t taosKeepTcpAlive(SOCKET sockFd) { int32_t nodelay = 1; if (taosSetSockOpt(sockFd, IPPROTO_TCP, TCP_NODELAY, (void *)&nodelay, sizeof(nodelay)) < 0) { - uError("fd:%d setsockopt TCP_NODELAY failed %d (%s)", sockFd, errno, strerror(errno)); + printf("fd:%d setsockopt TCP_NODELAY failed %d (%s)", sockFd, errno, strerror(errno)); taosCloseSocket(sockFd); return -1; } @@ -441,7 +689,7 @@ int32_t taosKeepTcpAlive(SOCKET sockFd) { linger.l_onoff = 1; linger.l_linger = 3; if (taosSetSockOpt(sockFd, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger)) < 0) { - uError("setsockopt SO_LINGER failed: %d (%s)", errno, strerror(errno)); + printf("setsockopt SO_LINGER failed: %d (%s)", errno, strerror(errno)); taosCloseSocket(sockFd); return -1; } @@ -454,7 +702,7 @@ SOCKET taosOpenTcpServerSocket(uint32_t ip, uint16_t port) { SOCKET sockFd; int32_t reuse; - uDebug("open tcp server socket:0x%x:%hu", ip, port); + printf("open tcp server socket:0x%x:%hu", ip, port); bzero((char *)&serverAdd, sizeof(serverAdd)); serverAdd.sin_family = AF_INET; @@ -462,7 +710,7 @@ SOCKET taosOpenTcpServerSocket(uint32_t ip, uint16_t port) { serverAdd.sin_port = (uint16_t)htons(port); if ((sockFd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) <= 2) { - uError("failed to open TCP socket: %d (%s)", errno, strerror(errno)); + printf("failed to open TCP socket: %d (%s)", errno, strerror(errno)); taosCloseSocketNoCheck(sockFd); return -1; } @@ -470,26 +718,26 @@ SOCKET taosOpenTcpServerSocket(uint32_t ip, uint16_t port) { /* set REUSEADDR option, so the portnumber can be re-used */ reuse = 1; if (taosSetSockOpt(sockFd, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(reuse)) < 0) { - uError("setsockopt SO_REUSEADDR failed: %d (%s)", errno, strerror(errno)); + printf("setsockopt SO_REUSEADDR failed: %d (%s)", errno, strerror(errno)); taosCloseSocket(sockFd); return -1; } /* bind socket to server address */ if (bind(sockFd, (struct sockaddr *)&serverAdd, sizeof(serverAdd)) < 0) { - uError("bind tcp server socket failed, 0x%x:%hu(%s)", ip, port, strerror(errno)); + printf("bind tcp server socket failed, 0x%x:%hu(%s)", ip, port, strerror(errno)); taosCloseSocket(sockFd); return -1; } if (taosKeepTcpAlive(sockFd) < 0) { - uError("failed to set tcp server keep-alive option, 0x%x:%hu(%s)", ip, port, strerror(errno)); + printf("failed to set tcp server keep-alive option, 0x%x:%hu(%s)", ip, port, strerror(errno)); taosCloseSocket(sockFd); return -1; } if (listen(sockFd, 1024) < 0) { - uError("listen tcp server socket failed, 0x%x:%hu(%s)", ip, port, strerror(errno)); + printf("listen tcp server socket failed, 0x%x:%hu(%s)", ip, port, strerror(errno)); taosCloseSocket(sockFd); return -1; } @@ -519,7 +767,7 @@ int64_t taosCopyFds(SOCKET sfd, int32_t dfd, int64_t len) { int64_t retLen = taosReadMsg(sfd, temp, (int32_t)readLen); if (readLen != retLen) { - uError("read error, readLen:%" PRId64 " retLen:%" PRId64 " len:%" PRId64 " leftLen:%" PRId64 ", reason:%s", + printf("read error, readLen:%" PRId64 " retLen:%" PRId64 " len:%" PRId64 " leftLen:%" PRId64 ", reason:%s", readLen, retLen, len, leftLen, strerror(errno)); return -1; } @@ -527,7 +775,7 @@ int64_t taosCopyFds(SOCKET sfd, int32_t dfd, int64_t len) { writeLen = taosWriteMsg(dfd, temp, (int32_t)readLen); if (readLen != writeLen) { - uError("copy error, readLen:%" PRId64 " writeLen:%" PRId64 " len:%" PRId64 " leftLen:%" PRId64 ", reason:%s", + printf("copy error, readLen:%" PRId64 " writeLen:%" PRId64 " len:%" PRId64 " leftLen:%" PRId64 ", reason:%s", readLen, writeLen, len, leftLen, strerror(errno)); return -1; } diff --git a/source/os/src/osString.c b/source/os/src/osString.c new file mode 100644 index 0000000000000000000000000000000000000000..10606a3d7bc3823e2d504fe38409ad1b861fe6c2 --- /dev/null +++ b/source/os/src/osString.c @@ -0,0 +1,382 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "os.h" +#include +#include + +typedef struct CharsetPair { + char *oldCharset; + char *newCharset; +} CharsetPair; + +char *taosCharsetReplace(char *charsetstr) { + CharsetPair charsetRep[] = { + { "utf8", "UTF-8" }, { "936", "CP936" }, + }; + + for (int32_t i = 0; i < tListLen(charsetRep); ++i) { + if (strcasecmp(charsetRep[i].oldCharset, charsetstr) == 0) { + return strdup(charsetRep[i].newCharset); + } + } + + return strdup(charsetstr); +} + +int64_t taosStr2int64(char *str) { + char *endptr = NULL; + return strtoll(str, &endptr, 10); +} + +#ifdef USE_LIBICONV +#include "iconv.h" + +int32_t taosUcs4ToMbs(void *ucs4, int32_t ucs4_max_len, char *mbs) { + iconv_t cd = iconv_open(tsCharset, DEFAULT_UNICODE_ENCODEC); + size_t ucs4_input_len = ucs4_max_len; + size_t outLen = ucs4_max_len; + if (iconv(cd, (char **)&ucs4, &ucs4_input_len, &mbs, &outLen) == -1) { + iconv_close(cd); + return -1; + } + + iconv_close(cd); + return (int32_t)(ucs4_max_len - outLen); +} + +bool taosMbsToUcs4(char *mbs, size_t mbsLength, char *ucs4, int32_t ucs4_max_len, int32_t *len) { + memset(ucs4, 0, ucs4_max_len); + iconv_t cd = iconv_open(DEFAULT_UNICODE_ENCODEC, tsCharset); + size_t ucs4_input_len = mbsLength; + size_t outLeft = ucs4_max_len; + if (iconv(cd, &mbs, &ucs4_input_len, &ucs4, &outLeft) == -1) { + iconv_close(cd); + return false; + } + + iconv_close(cd); + if (len != NULL) { + *len = (int32_t)(ucs4_max_len - outLeft); + if (*len < 0) { + return false; + } + } + + return true; +} + +bool taosValidateEncodec(const char *encodec) { + iconv_t cd = iconv_open(encodec, DEFAULT_UNICODE_ENCODEC); + if (cd == (iconv_t)(-1)) { + return false; + } + + iconv_close(cd); + return true; +} + +#else + +int32_t taosUcs4ToMbs(void *ucs4, int32_t ucs4_max_len, char *mbs) { + mbstate_t state = {0}; + int32_t len = (int32_t)wcsnrtombs(NULL, (const wchar_t **)&ucs4, ucs4_max_len / 4, 0, &state); + if (len < 0) { + return -1; + } + + memset(&state, 0, sizeof(state)); + len = wcsnrtombs(mbs, (const wchar_t **)&ucs4, ucs4_max_len / 4, (size_t)len, &state); + if (len < 0) { + return -1; + } + + return len; +} + +bool taosMbsToUcs4(char *mbs, size_t mbsLength, char *ucs4, int32_t ucs4_max_len, int32_t *len) { + memset(ucs4, 0, ucs4_max_len); + mbstate_t state = {0}; + int32_t retlen = mbsnrtowcs((wchar_t *)ucs4, (const char **)&mbs, mbsLength, ucs4_max_len / 4, &state); + *len = retlen; + + return retlen >= 0; +} + +bool taosValidateEncodec(const char *encodec) { + return true; +} + +#endif + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + +/* + * windows implementation + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include + +#if STDC_HEADERS +#include +#else +char *malloc(), *realloc(); +#endif + +/* Always add at least this many bytes when extending the buffer. */ +#define MIN_CHUNK 64 + +/* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR ++ OFFSET (and null-terminate it). *LINEPTR is a pointer returned from +malloc (or NULL), pointing to *N characters of space. It is realloc'd +as necessary. Return the number of characters read (not including the +null terminator), or -1 on error or EOF. On a -1 return, the caller +should check feof(), if not then errno has been set to indicate +the error. */ + +int32_t getstr(char **lineptr, size_t *n, FILE *stream, char terminator, int32_t offset) { + int32_t nchars_avail; /* Allocated but unused chars in *LINEPTR. */ + char * read_pos; /* Where we're reading into *LINEPTR. */ + int32_t ret; + + if (!lineptr || !n || !stream) { + errno = EINVAL; + return -1; + } + + if (!*lineptr) { + *n = MIN_CHUNK; + *lineptr = malloc(*n); + if (!*lineptr) { + errno = ENOMEM; + return -1; + } + } + + nchars_avail = (int32_t)(*n - offset); + read_pos = *lineptr + offset; + + for (;;) { + int32_t save_errno; + register int32_t c = getc(stream); + + save_errno = errno; + + /* We always want at least one char left in the buffer, since we + always (unless we get an error while reading the first char) + NUL-terminate the line buffer. */ + + assert((*lineptr + *n) == (read_pos + nchars_avail)); + if (nchars_avail < 2) { + if (*n > MIN_CHUNK) + *n *= 2; + else + *n += MIN_CHUNK; + + nchars_avail = (int32_t)(*n + *lineptr - read_pos); + char* lineptr1 = realloc(*lineptr, *n); + if (!lineptr1) { + errno = ENOMEM; + return -1; + } + *lineptr = lineptr1; + + read_pos = *n - nchars_avail + *lineptr; + assert((*lineptr + *n) == (read_pos + nchars_avail)); + } + + if (ferror(stream)) { + /* Might like to return partial line, but there is no + place for us to store errno. And we don't want to just + lose errno. */ + errno = save_errno; + return -1; + } + + if (c == EOF) { + /* Return partial line, if any. */ + if (read_pos == *lineptr) + return -1; + else + break; + } + + *read_pos++ = c; + nchars_avail--; + + if (c == terminator) /* Return the line. */ + break; + } + + /* Done - NUL terminate and return the number of chars read. */ + *read_pos = '\0'; + + ret = (int32_t)(read_pos - (*lineptr + offset)); + return ret; +} + +int32_t tgetline(char **lineptr, size_t *n, FILE *stream) { return getstr(lineptr, n, stream, '\n', 0); } + + +/* + * Get next token from string *stringp, where tokens are possibly-empty + * strings separated by characters from delim. + * + * Writes NULs into the string at *stringp to end tokens. + * delim need not remain constant from call to call. + * On return, *stringp points past the last NUL written (if there might + * be further tokens), or is NULL (if there are definitely no moretokens). + * + * If *stringp is NULL, strsep returns NULL. + */ +char *strsep(char **stringp, const char *delim) { + char * s; + const char *spanp; + int32_t c, sc; + char *tok; + if ((s = *stringp) == NULL) + return (NULL); + for (tok = s;;) { + c = *s++; + spanp = delim; + do { + if ((sc = *spanp++) == c) { + if (c == 0) + s = NULL; + else + s[-1] = 0; + *stringp = s; + return (tok); + } + } while (sc != 0); + } + /* NOTREACHED */ +} + +char *getpass(const char *prefix) { + static char passwd[TSDB_KEY_LEN] = {0}; + memset(passwd, 0, TSDB_KEY_LEN); + printf("%s", prefix); + + int32_t index = 0; + char ch; + while (index < TSDB_KEY_LEN) { + ch = getch(); + if (ch == '\n' || ch == '\r') { + break; + } else { + passwd[index++] = ch; + } + } + + return passwd; +} + +int32_t twcslen(const wchar_t *wcs) { + int32_t *wstr = (int32_t *)wcs; + if (NULL == wstr) { + return 0; + } + + int32_t n = 0; + while (1) { + if (0 == *wstr++) { + break; + } + n++; + } + + return n; +} + +int32_t tasoUcs4Compare(void *f1_ucs4, void *f2_ucs4, int32_t bytes) { + for (int32_t i = 0; i < bytes; ++i) { + int32_t f1 = *(int32_t *)((char *)f1_ucs4 + i * 4); + int32_t f2 = *(int32_t *)((char *)f2_ucs4 + i * 4); + + if ((f1 == 0 && f2 != 0) || (f1 != 0 && f2 == 0)) { + return f1 - f2; + } else if (f1 == 0 && f2 == 0) { + return 0; + } + + if (f1 != f2) { + return f1 - f2; + } + } + + return 0; + +#if 0 + int32_t ucs4_max_len = bytes + 4; + char *f1_mbs = calloc(bytes, 1); + char *f2_mbs = calloc(bytes, 1); + if (taosUcs4ToMbs(f1_ucs4, ucs4_max_len, f1_mbs) < 0) { + return -1; + } + if (taosUcs4ToMbs(f2_ucs4, ucs4_max_len, f2_mbs) < 0) { + return -1; + } + int32_t ret = strcmp(f1_mbs, f2_mbs); + free(f1_mbs); + free(f2_mbs); + return ret; +#endif +} + +/* Copy memory to memory until the specified number of bytes +has been copied, return pointer to following byte. +Overlap is NOT handled correctly. */ +void *mempcpy(void *dest, const void *src, size_t len) { + return (char*)memcpy(dest, src, len) + len; +} + +/* Copy SRC to DEST, returning the address of the terminating '\0' in DEST. */ +char *stpcpy (char *dest, const char *src) { + size_t len = strlen (src); + return (char*)memcpy(dest, src, len + 1) + len; +} + +/* Copy no more than N characters of SRC to DEST, returning the address of + the terminating '\0' in DEST, if any, or else DEST + N. */ +char *stpncpy (char *dest, const char *src, size_t n) { + size_t size = strnlen (src, n); + memcpy (dest, src, size); + dest += size; + if (size == n) + return dest; + return memset (dest, '\0', n - size); +} + +#else + +/* + * linux and darwin implementation + */ + +int32_t tasoUcs4Compare(void *f1_ucs4, void *f2_ucs4, int32_t bytes, int8_t ncharSize) { + return wcsncmp((wchar_t *)f1_ucs4, (wchar_t *)f2_ucs4, bytes / ncharSize); +} + +#endif \ No newline at end of file diff --git a/src/os/src/windows/wStrptime.c b/source/os/src/osStrptime.c similarity index 99% rename from src/os/src/windows/wStrptime.c rename to source/os/src/osStrptime.c index 24018182740edee5debdccfb6e36864266fc5061..e8c873bc492ee33f1833ca38bcd7111bd6838d1b 100644 --- a/src/os/src/windows/wStrptime.c +++ b/source/os/src/osStrptime.c @@ -37,6 +37,9 @@ */ // //#include "lukemftp.h" + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + #include #include #include @@ -395,4 +398,6 @@ conv_num(const char **buf, int *dest, int llim, int ulim) *dest = result; return (1); -} \ No newline at end of file +} + +#endif \ No newline at end of file diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c new file mode 100644 index 0000000000000000000000000000000000000000..0344507f5eb1fc4bc77a96c9eb726a2a89a03ef1 --- /dev/null +++ b/source/os/src/osSysinfo.c @@ -0,0 +1,1136 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "os.h" + +int32_t tsTotalMemoryMB = 0; +int64_t tsPageSize = 0; +int64_t tsOpenMax = 0; +int64_t tsStreamMax = 0; +int32_t tsNumOfCores = 1; +char tsTimezone[TSDB_TIMEZONE_LEN] = {0}; +char tsLocale[TSDB_LOCALE_LEN] = {0}; +char tsCharset[TSDB_LOCALE_LEN] = {0}; // default encode string + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + +/* + * windows implementation + */ + +#if (_WIN64) +#include +#include +#include +#include +#include +#include +#pragma comment(lib, "Mswsock.lib ") +#endif + +#include + +#pragma warning(push) +#pragma warning(disable : 4091) +#include +#pragma warning(pop) + +static int32_t taosGetTotalMemory() { + MEMORYSTATUSEX memsStat; + memsStat.dwLength = sizeof(memsStat); + if (!GlobalMemoryStatusEx(&memsStat)) { + return 0; + } + + float nMemTotal = memsStat.ullTotalPhys / (1024.0f * 1024.0f); + return (int32_t)nMemTotal; +} + +bool taosGetSysMemory(float *memoryUsedMB) { + MEMORYSTATUSEX memsStat; + memsStat.dwLength = sizeof(memsStat); + if (!GlobalMemoryStatusEx(&memsStat)) { + return false; + } + + float nMemFree = memsStat.ullAvailPhys / (1024.0f * 1024.0f); + float nMemTotal = memsStat.ullTotalPhys / (1024.0f * 1024.0f); + + *memoryUsedMB = nMemTotal - nMemFree; + return true; +} + +bool taosGetProcMemory(float *memoryUsedMB) { + unsigned bytes_used = 0; + +#if defined(_WIN64) && defined(_MSC_VER) + PROCESS_MEMORY_COUNTERS pmc; + HANDLE cur_proc = GetCurrentProcess(); + + if (GetProcessMemoryInfo(cur_proc, &pmc, sizeof(pmc))) { + bytes_used = (unsigned)(pmc.WorkingSetSize + pmc.PagefileUsage); + } +#endif + + *memoryUsedMB = (float)bytes_used / 1024 / 1024; + return true; +} + +static void taosGetSystemTimezone() { + // get and set default timezone + char *tz = getenv("TZ"); + if (tz == NULL || strlen(tz) == 0) { + strcpy(tsTimezone, "not configured"); + } else { + strcpy(tsTimezone, tz); + } +} + +static void taosGetSystemLocale() { + // get and set default locale + char *locale = setlocale(LC_CTYPE, "chs"); + if (locale != NULL) { + tstrncpy(tsLocale, locale, TSDB_LOCALE_LEN); + } + + strcpy(tsCharset, "cp936"); +} + +int32_t taosGetCpuCores() { + SYSTEM_INFO info; + GetSystemInfo(&info); + return (int32_t)info.dwNumberOfProcessors; +} + +bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { + *sysCpuUsage = 0; + *procCpuUsage = 0; + return true; +} + +int32_t taosGetDiskSize(char *dataDir, SysDiskSize *diskSize) { + unsigned _int64 i64FreeBytesToCaller; + unsigned _int64 i64TotalBytes; + unsigned _int64 i64FreeBytes; + + BOOL fResult = GetDiskFreeSpaceExA(dataDir, (PULARGE_INTEGER)&i64FreeBytesToCaller, (PULARGE_INTEGER)&i64TotalBytes, + (PULARGE_INTEGER)&i64FreeBytes); + if (fResult) { + diskSize->tsize = (int64_t)(i64TotalBytes); + diskSize->avail = (int64_t)(i64FreeBytesToCaller); + diskSize->used = (int64_t)(i64TotalBytes - i64FreeBytes); + return 0; + } else { + printf("failed to get disk size, dataDir:%s errno:%s", tsDataDir, strerror(errno)); + terrno = TAOS_SYSTEM_ERROR(errno); + return -1; + } +} + +bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { + if (bytes) *bytes = 0; + if (rbytes) *rbytes = 0; + if (tbytes) *tbytes = 0; + return true; +} + +bool taosGetBandSpeed(float *bandSpeedKb) { + *bandSpeedKb = 0; + return true; +} + +bool taosReadProcIO(int64_t *readbyte, int64_t *writebyte) { + IO_COUNTERS io_counter; + if (GetProcessIoCounters(GetCurrentProcess(), &io_counter)) { + if (readbyte) *readbyte = io_counter.ReadTransferCount; + if (writebyte) *writebyte = io_counter.WriteTransferCount; + return true; + } + return false; +} + +bool taosGetProcIO(float *readKB, float *writeKB) { + static int64_t lastReadbyte = -1; + static int64_t lastWritebyte = -1; + + int64_t curReadbyte = 0; + int64_t curWritebyte = 0; + + if (!taosReadProcIO(&curReadbyte, &curWritebyte)) { + return false; + } + + if (lastReadbyte == -1 || lastWritebyte == -1) { + lastReadbyte = curReadbyte; + lastWritebyte = curWritebyte; + return false; + } + + *readKB = (float)((double)(curReadbyte - lastReadbyte) / 1024); + *writeKB = (float)((double)(curWritebyte - lastWritebyte) / 1024); + if (*readKB < 0) *readKB = 0; + if (*writeKB < 0) *writeKB = 0; + + lastReadbyte = curReadbyte; + lastWritebyte = curWritebyte; + + return true; +} + +void taosGetSystemInfo() { + tsNumOfCores = taosGetCpuCores(); + tsTotalMemoryMB = taosGetTotalMemory(); + + float tmp1, tmp2; + // taosGetDisk(); + taosGetBandSpeed(&tmp1); + taosGetCpuUsage(&tmp1, &tmp2); + taosGetProcIO(&tmp1, &tmp2); + + taosGetSystemTimezone(); + taosGetSystemLocale(); +} + +void taosKillSystem() { + printf("function taosKillSystem, exit!"); + exit(0); +} + +int taosSystem(const char *cmd) { + printf("taosSystem not support"); + return -1; +} + +LONG WINAPI FlCrashDump(PEXCEPTION_POINTERS ep) { + typedef BOOL(WINAPI * FxMiniDumpWriteDump)(IN HANDLE hProcess, IN DWORD ProcessId, IN HANDLE hFile, + IN MINIDUMP_TYPE DumpType, + IN CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, + IN CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, + IN CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam); + + HMODULE dll = LoadLibrary("dbghelp.dll"); + if (dll == NULL) return EXCEPTION_CONTINUE_SEARCH; + FxMiniDumpWriteDump mdwd = (FxMiniDumpWriteDump)(GetProcAddress(dll, "MiniDumpWriteDump")); + if (mdwd == NULL) { + FreeLibrary(dll); + return EXCEPTION_CONTINUE_SEARCH; + } + + TCHAR path[MAX_PATH]; + DWORD len = GetModuleFileName(NULL, path, _countof(path)); + path[len - 3] = 'd'; + path[len - 2] = 'm'; + path[len - 1] = 'p'; + + HANDLE file = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + + MINIDUMP_EXCEPTION_INFORMATION mei; + mei.ThreadId = GetCurrentThreadId(); + mei.ExceptionPointers = ep; + mei.ClientPointers = FALSE; + + (*mdwd)(GetCurrentProcess(), GetCurrentProcessId(), file, MiniDumpWithHandleData, &mei, NULL, NULL); + + CloseHandle(file); + FreeLibrary(dll); + + return EXCEPTION_CONTINUE_SEARCH; +} + +void taosSetCoreDump() { SetUnhandledExceptionFilter(&FlCrashDump); } + +bool taosGetSystemUid(char *uid) { + GUID guid; + CoCreateGuid(&guid); + + sprintf(uid, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], + guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]); + + return true; +} + +char *taosGetCmdlineByPID(int pid) { return ""; } + +#elif defined(_TD_DARWIN_64) + +/* + * darwin implementation + */ + +#include +#include + +static void taosGetSystemTimezone() { + /* load time zone string from /etc/localtime */ + char buf[4096]; + char *tz = NULL; + { + int n = readlink("/etc/localtime", buf, sizeof(buf)); + if (n < 0) { + printf("read /etc/localtime error, reason:%s", strerror(errno)); + return; + } + buf[n] = '\0'; + for (int i = n - 1; i >= 0; --i) { + if (buf[i] == '/') { + if (tz) { + tz = buf + i + 1; + break; + } + tz = buf + i + 1; + } + } + if (!tz || 0 == strchr(tz, '/')) { + printf("parsing /etc/localtime failed"); + return; + } + + setenv("TZ", tz, 1); + tzset(); + } + + /* + * NOTE: do not remove it. + * Enforce set the correct daylight saving time(DST) flag according + * to current time + */ + time_t tx1 = time(NULL); + struct tm tm1; + localtime_r(&tx1, &tm1); + + /* + * format example: + * + * Asia/Shanghai (CST, +0800) + * Europe/London (BST, +0100) + */ + snprintf(tsTimezone, TSDB_TIMEZONE_LEN, "%s (%s, %+03ld00)", tz, tm1.tm_isdst ? tzname[daylight] : tzname[0], + -timezone / 3600); + + // cfg_timezone->cfgStatus = TAOS_CFG_CSTATUS_DEFAULT; + printf("timezone not configured, set to system default:%s", tsTimezone); +} + +/* + * originally from src/os/src/detail/osSysinfo.c + * POSIX format locale string: + * (Language Strings)_(Country/Region Strings).(code_page) + * + * example: en_US.UTF-8, zh_CN.GB18030, zh_CN.UTF-8, + * + * if user does not specify the locale in taos.cfg the program use default LC_CTYPE as system locale. + * + * In case of some CentOS systems, their default locale is "en_US.utf8", which is not valid code_page + * for libiconv that is employed to convert string in this system. This program will automatically use + * UTF-8 instead as the charset. + * + * In case of windows client, the locale string is not valid POSIX format, user needs to set the + * correct code_page for libiconv. Usually, the code_page of windows system with simple chinese is + * CP936, CP437 for English charset. + * + */ +static void taosGetSystemLocale() { // get and set default locale + char sep = '.'; + char *locale = NULL; + + locale = setlocale(LC_CTYPE, ""); + if (locale == NULL) { + printf("can't get locale from system, set it to en_US.UTF-8 since error:%d:%s", errno, strerror(errno)); + strcpy(tsLocale, "en_US.UTF-8"); + } else { + tstrncpy(tsLocale, locale, TSDB_LOCALE_LEN); + printf("locale not configured, set to system default:%s", tsLocale); + } + + /* if user does not specify the charset, extract it from locale */ + char *str = strrchr(tsLocale, sep); + if (str != NULL) { + str++; + + char *revisedCharset = taosCharsetReplace(str); + tstrncpy(tsCharset, revisedCharset, TSDB_LOCALE_LEN); + + free(revisedCharset); + printf("charset not configured, set to system default:%s", tsCharset); + } else { + strcpy(tsCharset, "UTF-8"); + printf("can't get locale and charset from system, set it to UTF-8"); + } +} + +void taosKillSystem() { + printf("function taosKillSystem, exit!"); + exit(0); +} + +int32_t taosGetCpuCores() { return sysconf(_SC_NPROCESSORS_ONLN); } + +void taosGetSystemInfo() { + // taosGetProcInfos(); + + tsNumOfCores = sysconf(_SC_NPROCESSORS_ONLN); + long physical_pages = sysconf(_SC_PHYS_PAGES); + long page_size = sysconf(_SC_PAGESIZE); + tsTotalMemoryMB = physical_pages * page_size / (1024 * 1024); + tsPageSize = page_size; + + taosGetSystemTimezone(); + taosGetSystemLocale(); +} + +bool taosReadProcIO(int64_t *rchars, int64_t *wchars) { + if (rchars) *rchars = 0; + if (wchars) *wchars = 0; + return true; +} + +bool taosGetProcIO(float *readKB, float *writeKB) { + *readKB = 0; + *writeKB = 0; + return true; +} + +bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { + if (bytes) *bytes = 0; + if (rbytes) *rbytes = 0; + if (tbytes) *tbytes = 0; + return true; +} + +bool taosGetBandSpeed(float *bandSpeedKb) { + *bandSpeedKb = 0; + return true; +} + +bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { + *sysCpuUsage = 0; + *procCpuUsage = 0; + return true; +} + +bool taosGetProcMemory(float *memoryUsedMB) { + *memoryUsedMB = 0; + return true; +} + +bool taosGetSysMemory(float *memoryUsedMB) { + *memoryUsedMB = 0; + return true; +} + +int taosSystem(const char *cmd) { + printf("un support funtion"); + return -1; +} + +void taosSetCoreDump() {} + +int32_t taosGetDiskSize(char *dataDir, SysDiskSize *diskSize) { + struct statvfs info; + if (statvfs(dataDir, &info)) { + printf("failed to get disk size, dataDir:%s errno:%s", tsDataDir, strerror(errno)); + terrno = TAOS_SYSTEM_ERROR(errno); + return -1; + } else { + diskSize->tsize = info.f_blocks * info.f_frsize; + diskSize->avail = info.f_bavail * info.f_frsize; + diskSize->used = (info.f_blocks - info.f_bfree) * info.f_frsize; + return 0; + } +} + +bool taosGetSystemUid(char *uid) { + uuid_t uuid = {0}; + uuid_generate(uuid); + // it's caller's responsibility to make enough space for `uid`, that's 36-char + 1-null + uuid_unparse_lower(uuid, uid); + return true; +} + +char *taosGetCmdlineByPID(int pid) { + static char cmdline[1024]; + errno = 0; + + if (proc_pidpath(pid, cmdline, sizeof(cmdline)) <= 0) { + fprintf(stderr, "PID is %d, %s", pid, strerror(errno)); + return strerror(errno); + } + + return cmdline; +} + +#else + +/* + * linux implementation + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define PROCESS_ITEM 12 + +typedef struct { + uint64_t user; + uint64_t nice; + uint64_t system; + uint64_t idle; +} SysCpuInfo; + +typedef struct { + uint64_t utime; // user time + uint64_t stime; // kernel time + uint64_t cutime; // all user time + uint64_t cstime; // all dead time +} ProcCpuInfo; + +static pid_t tsProcId; +static char tsSysNetFile[] = "/proc/net/dev"; +static char tsSysCpuFile[] = "/proc/stat"; +static char tsProcCpuFile[25] = {0}; +static char tsProcMemFile[25] = {0}; +static char tsProcIOFile[25] = {0}; +static float tsPageSizeKB = 0; + +static void taosGetProcInfos() { + tsPageSize = sysconf(_SC_PAGESIZE); + tsOpenMax = sysconf(_SC_OPEN_MAX); + tsStreamMax = sysconf(_SC_STREAM_MAX); + + tsProcId = (pid_t)syscall(SYS_gettid); + tsPageSizeKB = (float)(sysconf(_SC_PAGESIZE)) / 1024; + + snprintf(tsProcMemFile, 25, "/proc/%d/status", tsProcId); + snprintf(tsProcCpuFile, 25, "/proc/%d/stat", tsProcId); + snprintf(tsProcIOFile, 25, "/proc/%d/io", tsProcId); +} + +static int32_t taosGetTotalMemory() { return (int32_t)((float)sysconf(_SC_PHYS_PAGES) * tsPageSizeKB / 1024); } + +bool taosGetSysMemory(float *memoryUsedMB) { + float memoryAvailMB = (float)sysconf(_SC_AVPHYS_PAGES) * tsPageSizeKB / 1024; + *memoryUsedMB = (float)tsTotalMemoryMB - memoryAvailMB; + return true; +} + +bool taosGetProcMemory(float *memoryUsedMB) { + FILE *fp = fopen(tsProcMemFile, "r"); + if (fp == NULL) { + printf("open file:%s failed", tsProcMemFile); + return false; + } + + ssize_t _bytes = 0; + size_t len; + char * line = NULL; + while (!feof(fp)) { + tfree(line); + len = 0; + _bytes = getline(&line, &len, fp); + if ((_bytes < 0) || (line == NULL)) { + break; + } + if (strstr(line, "VmRSS:") != NULL) { + break; + } + } + + if (line == NULL) { + printf("read file:%s failed", tsProcMemFile); + fclose(fp); + return false; + } + + int64_t memKB = 0; + char tmp[10]; + sscanf(line, "%s %" PRId64, tmp, &memKB); + *memoryUsedMB = (float)((double)memKB / 1024); + + tfree(line); + fclose(fp); + return true; +} + +static bool taosGetSysCpuInfo(SysCpuInfo *cpuInfo) { + FILE *fp = fopen(tsSysCpuFile, "r"); + if (fp == NULL) { + printf("open file:%s failed", tsSysCpuFile); + return false; + } + + size_t len; + char * line = NULL; + ssize_t _bytes = getline(&line, &len, fp); + if ((_bytes < 0) || (line == NULL)) { + printf("read file:%s failed", tsSysCpuFile); + fclose(fp); + return false; + } + + char cpu[10] = {0}; + sscanf(line, "%s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, cpu, &cpuInfo->user, &cpuInfo->nice, &cpuInfo->system, + &cpuInfo->idle); + + tfree(line); + fclose(fp); + return true; +} + +static bool taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) { + FILE *fp = fopen(tsProcCpuFile, "r"); + if (fp == NULL) { + printf("open file:%s failed", tsProcCpuFile); + return false; + } + + size_t len = 0; + char * line = NULL; + ssize_t _bytes = getline(&line, &len, fp); + if ((_bytes < 0) || (line == NULL)) { + printf("read file:%s failed", tsProcCpuFile); + fclose(fp); + return false; + } + + for (int i = 0, blank = 0; line[i] != 0; ++i) { + if (line[i] == ' ') blank++; + if (blank == PROCESS_ITEM) { + sscanf(line + i + 1, "%" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, &cpuInfo->utime, &cpuInfo->stime, + &cpuInfo->cutime, &cpuInfo->cstime); + break; + } + } + + tfree(line); + fclose(fp); + return true; +} + +static void taosGetSystemTimezone() { + /* + * NOTE: do not remove it. + * Enforce set the correct daylight saving time(DST) flag according + * to current time + */ + time_t tx1 = time(NULL); + struct tm tm1; + localtime_r(&tx1, &tm1); + + /* load time zone string from /etc/timezone */ + FILE *f = fopen("/etc/timezone", "r"); + char buf[68] = {0}; + if (f != NULL) { + int len = fread(buf, 64, 1, f); + if (len < 64 && ferror(f)) { + fclose(f); + printf("read /etc/timezone error, reason:%s", strerror(errno)); + return; + } + + fclose(f); + + buf[sizeof(buf) - 1] = 0; + char *lineEnd = strstr(buf, "\n"); + if (lineEnd != NULL) { + *lineEnd = 0; + } + + // for CentOS system, /etc/timezone does not exist. Ignore the TZ environment variables + if (strlen(buf) > 0) { + setenv("TZ", buf, 1); + } + } + // get and set default timezone + tzset(); + + /* + * get CURRENT time zone. + * system current time zone is affected by daylight saving time(DST) + * + * e.g., the local time zone of London in DST is GMT+01:00, + * otherwise is GMT+00:00 + */ + int32_t tz = (-timezone * MILLISECOND_PER_SECOND) / MILLISECOND_PER_HOUR; + tz += daylight; + + /* + * format example: + * + * Asia/Shanghai (CST, +0800) + * Europe/London (BST, +0100) + */ + snprintf(tsTimezone, TSDB_TIMEZONE_LEN, "%s (%s, %s%02d00)", buf, tzname[daylight], tz >= 0 ? "+" : "-", abs(tz)); + + // cfg_timezone->cfgStatus = TAOS_CFG_CSTATUS_DEFAULT; + printf("timezone not configured, set to system default:%s", tsTimezone); +} + +/* + * POSIX format locale string: + * (Language Strings)_(Country/Region Strings).(code_page) + * + * example: en_US.UTF-8, zh_CN.GB18030, zh_CN.UTF-8, + * + * if user does not specify the locale in taos.cfg the program use default LC_CTYPE as system locale. + * + * In case of some CentOS systems, their default locale is "en_US.utf8", which is not valid code_page + * for libiconv that is employed to convert string in this system. This program will automatically use + * UTF-8 instead as the charset. + * + * In case of windows client, the locale string is not valid POSIX format, user needs to set the + * correct code_page for libiconv. Usually, the code_page of windows system with simple chinese is + * CP936, CP437 for English charset. + * + */ +static void taosGetSystemLocale() { // get and set default locale + char sep = '.'; + char *locale = NULL; + + locale = setlocale(LC_CTYPE, ""); + if (locale == NULL) { + printf("can't get locale from system, set it to en_US.UTF-8 since error:%d:%s", errno, strerror(errno)); + strcpy(tsLocale, "en_US.UTF-8"); + } else { + tstrncpy(tsLocale, locale, TSDB_LOCALE_LEN); + printf("locale not configured, set to system default:%s", tsLocale); + } + + /* if user does not specify the charset, extract it from locale */ + char *str = strrchr(tsLocale, sep); + if (str != NULL) { + str++; + + char *revisedCharset = taosCharsetReplace(str); + tstrncpy(tsCharset, revisedCharset, TSDB_LOCALE_LEN); + + free(revisedCharset); + printf("charset not configured, set to system default:%s", tsCharset); + } else { + strcpy(tsCharset, "UTF-8"); + printf("can't get locale and charset from system, set it to UTF-8"); + } +} + +int32_t taosGetCpuCores() { return (int32_t)sysconf(_SC_NPROCESSORS_ONLN); } + +bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { + static uint64_t lastSysUsed = 0; + static uint64_t lastSysTotal = 0; + static uint64_t lastProcTotal = 0; + + SysCpuInfo sysCpu; + ProcCpuInfo procCpu; + if (!taosGetSysCpuInfo(&sysCpu)) { + return false; + } + if (!taosGetProcCpuInfo(&procCpu)) { + return false; + } + + uint64_t curSysUsed = sysCpu.user + sysCpu.nice + sysCpu.system; + uint64_t curSysTotal = curSysUsed + sysCpu.idle; + uint64_t curProcTotal = procCpu.utime + procCpu.stime + procCpu.cutime + procCpu.cstime; + + if (lastSysUsed == 0 || lastSysTotal == 0 || lastProcTotal == 0) { + lastSysUsed = curSysUsed > 1 ? curSysUsed : 1; + lastSysTotal = curSysTotal > 1 ? curSysTotal : 1; + lastProcTotal = curProcTotal > 1 ? curProcTotal : 1; + return false; + } + + if (curSysTotal == lastSysTotal) { + return false; + } + + *sysCpuUsage = (float)((double)(curSysUsed - lastSysUsed) / (double)(curSysTotal - lastSysTotal) * 100); + *procCpuUsage = (float)((double)(curProcTotal - lastProcTotal) / (double)(curSysTotal - lastSysTotal) * 100); + + lastSysUsed = curSysUsed; + lastSysTotal = curSysTotal; + lastProcTotal = curProcTotal; + + return true; +} + +int32_t taosGetDiskSize(char *dataDir, SysDiskSize *diskSize) { + struct statvfs info; + if (statvfs(dataDir, &info)) { + printf("failed to get disk size, dataDir:%s errno:%s", dataDir, strerror(errno)); + return -1; + } else { + diskSize->tsize = info.f_blocks * info.f_frsize; + diskSize->avail = info.f_bavail * info.f_frsize; + diskSize->used = (info.f_blocks - info.f_bfree) * info.f_frsize; + return 0; + } +} + +bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { + *bytes = 0; + FILE *fp = fopen(tsSysNetFile, "r"); + if (fp == NULL) { + printf("open file:%s failed", tsSysNetFile); + return false; + } + + ssize_t _bytes = 0; + size_t len = 2048; + char * line = calloc(1, len); + + while (!feof(fp)) { + memset(line, 0, len); + + int64_t o_rbytes = 0; + int64_t rpackts = 0; + int64_t o_tbytes = 0; + int64_t tpackets = 0; + int64_t nouse1 = 0; + int64_t nouse2 = 0; + int64_t nouse3 = 0; + int64_t nouse4 = 0; + int64_t nouse5 = 0; + int64_t nouse6 = 0; + char nouse0[200] = {0}; + + _bytes = getline(&line, &len, fp); + if (_bytes < 0) { + break; + } + + line[len - 1] = 0; + + if (strstr(line, "lo:") != NULL) { + continue; + } + + sscanf(line, + "%s %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 + " %" PRId64, + nouse0, &o_rbytes, &rpackts, &nouse1, &nouse2, &nouse3, &nouse4, &nouse5, &nouse6, &o_tbytes, &tpackets); + if (rbytes) *rbytes = o_rbytes; + if (tbytes) *tbytes = o_tbytes; + *bytes += (o_rbytes + o_tbytes); + } + + tfree(line); + fclose(fp); + + return true; +} + +bool taosGetBandSpeed(float *bandSpeedKb) { + static int64_t lastBytes = 0; + static time_t lastTime = 0; + int64_t curBytes = 0; + time_t curTime = time(NULL); + + if (!taosGetCardInfo(&curBytes, NULL, NULL)) { + return false; + } + + if (lastTime == 0 || lastBytes == 0) { + lastTime = curTime; + lastBytes = curBytes; + *bandSpeedKb = 0; + return true; + } + + if (lastTime >= curTime || lastBytes > curBytes) { + lastTime = curTime; + lastBytes = curBytes; + *bandSpeedKb = 0; + return true; + } + + double totalBytes = (double)(curBytes - lastBytes) / 1024 * 8; // Kb + *bandSpeedKb = (float)(totalBytes / (double)(curTime - lastTime)); + + // printf("bandwidth lastBytes:%ld, lastTime:%ld, curBytes:%ld, curTime:%ld, + // speed:%f", lastBytes, lastTime, curBytes, curTime, *bandSpeed); + + lastTime = curTime; + lastBytes = curBytes; + + return true; +} + +bool taosReadProcIO(int64_t *rchars, int64_t *wchars) { + FILE *fp = fopen(tsProcIOFile, "r"); + if (fp == NULL) { + printf("open file:%s failed", tsProcIOFile); + return false; + } + + ssize_t _bytes = 0; + size_t len; + char * line = NULL; + char tmp[10]; + int readIndex = 0; + + while (!feof(fp)) { + tfree(line); + len = 0; + _bytes = getline(&line, &len, fp); + if ((_bytes < 0) || (line == NULL)) { + break; + } + if (strstr(line, "rchar:") != NULL) { + sscanf(line, "%s %" PRId64, tmp, rchars); + readIndex++; + } else if (strstr(line, "wchar:") != NULL) { + sscanf(line, "%s %" PRId64, tmp, wchars); + readIndex++; + } else { + } + + if (readIndex >= 2) break; + } + + tfree(line); + fclose(fp); + + if (readIndex < 2) { + printf("read file:%s failed", tsProcIOFile); + return false; + } + + return true; +} + +bool taosGetProcIO(float *readKB, float *writeKB) { + static int64_t lastReadbyte = -1; + static int64_t lastWritebyte = -1; + + int64_t curReadbyte = 0; + int64_t curWritebyte = 0; + + if (!taosReadProcIO(&curReadbyte, &curWritebyte)) { + return false; + } + + if (lastReadbyte == -1 || lastWritebyte == -1) { + lastReadbyte = curReadbyte; + lastWritebyte = curWritebyte; + return false; + } + + *readKB = (float)((double)(curReadbyte - lastReadbyte) / 1024); + *writeKB = (float)((double)(curWritebyte - lastWritebyte) / 1024); + if (*readKB < 0) *readKB = 0; + if (*writeKB < 0) *writeKB = 0; + + lastReadbyte = curReadbyte; + lastWritebyte = curWritebyte; + + return true; +} + +void taosGetSystemInfo() { + taosGetProcInfos(); + + tsNumOfCores = taosGetCpuCores(); + tsTotalMemoryMB = taosGetTotalMemory(); + + float tmp1, tmp2; + taosGetSysMemory(&tmp1); + taosGetProcMemory(&tmp2); + // taosGetDisk(); + taosGetBandSpeed(&tmp1); + taosGetCpuUsage(&tmp1, &tmp2); + taosGetProcIO(&tmp1, &tmp2); + + taosGetSystemTimezone(); + taosGetSystemLocale(); +} + +void taosKillSystem() { + // SIGINT + printf("taosd will shut down soon"); + kill(tsProcId, 2); +} + +int taosSystem(const char *cmd) { + FILE *fp; + int res; + char buf[1024]; + if (cmd == NULL) { + printf("taosSystem cmd is NULL!"); + return -1; + } + + if ((fp = popen(cmd, "r")) == NULL) { + printf("popen cmd:%s error: %s", cmd, strerror(errno)); + return -1; + } else { + while (fgets(buf, sizeof(buf), fp)) { + printf("popen result:%s", buf); + } + + if ((res = pclose(fp)) == -1) { + printf("close popen file pointer fp error!"); + } else { + printf("popen res is :%d", res); + } + + return res; + } +} + +void taosSetCoreDump(bool enable) { + if (!enable) return; + + // 1. set ulimit -c unlimited + struct rlimit rlim; + struct rlimit rlim_new; + if (getrlimit(RLIMIT_CORE, &rlim) == 0) { +#ifndef _ALPINE + printf("the old unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max); +#else + printf("the old unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max); +#endif + rlim_new.rlim_cur = RLIM_INFINITY; + rlim_new.rlim_max = RLIM_INFINITY; + if (setrlimit(RLIMIT_CORE, &rlim_new) != 0) { + printf("set unlimited fail, error: %s", strerror(errno)); + rlim_new.rlim_cur = rlim.rlim_max; + rlim_new.rlim_max = rlim.rlim_max; + (void)setrlimit(RLIMIT_CORE, &rlim_new); + } + } + + if (getrlimit(RLIMIT_CORE, &rlim) == 0) { +#ifndef _ALPINE + printf("the new unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max); +#else + printf("the new unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max); +#endif + } + +#ifndef _TD_ARM_ + // 2. set the path for saving core file + struct __sysctl_args args; + + int old_usespid = 0; + size_t old_len = 0; + int new_usespid = 1; + size_t new_len = sizeof(new_usespid); + + int name[] = {CTL_KERN, KERN_CORE_USES_PID}; + + memset(&args, 0, sizeof(struct __sysctl_args)); + args.name = name; + args.nlen = sizeof(name) / sizeof(name[0]); + args.oldval = &old_usespid; + args.oldlenp = &old_len; + args.newval = &new_usespid; + args.newlen = new_len; + + old_len = sizeof(old_usespid); + + if (syscall(SYS__sysctl, &args) == -1) { + printf("_sysctl(kern_core_uses_pid) set fail: %s", strerror(errno)); + } + + printf("The old core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid); + + old_usespid = 0; + old_len = 0; + memset(&args, 0, sizeof(struct __sysctl_args)); + args.name = name; + args.nlen = sizeof(name) / sizeof(name[0]); + args.oldval = &old_usespid; + args.oldlenp = &old_len; + + old_len = sizeof(old_usespid); + + if (syscall(SYS__sysctl, &args) == -1) { + printf("_sysctl(kern_core_uses_pid) get fail: %s", strerror(errno)); + } + + printf("The new core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid); +#endif +} + +bool taosGetSystemUid(char *uid, int32_t uidlen) { + int fd; + int len = 0; + + fd = open("/proc/sys/kernel/random/uuid", 0); + if (fd < 0) { + return false; + } else { + len = read(fd, uid, uidlen); + close(fd); + } + + if (len >= 36) { + uid[36] = 0; + return true; + } + return false; +} + +char *taosGetCmdlineByPID(int pid) { + static char cmdline[1024]; + sprintf(cmdline, "/proc/%d/cmdline", pid); + + int fd = open(cmdline, O_RDONLY); + if (fd >= 0) { + int n = read(fd, cmdline, sizeof(cmdline) - 1); + if (n < 0) n = 0; + + if (n > 0 && cmdline[n - 1] == '\n') --n; + + cmdline[n] = 0; + + close(fd); + } else { + cmdline[0] = 0; + } + + return cmdline; +} + +#endif + +#if !(defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)) + +SysNameInfo taosGetSysNameInfo() { + SysNameInfo info = {0}; + + struct utsname buf; + if (!uname(&buf)) { + info.sysname = buf.sysname; + info.sysname == buf.nodename; + info.sysname = buf.release; + info.sysname = buf.version; + info.sysname = buf.machine; + } + + return info; +} + +int64_t taosGetPid() { + getpid(); +} + +#endif \ No newline at end of file diff --git a/source/os/src/osSystem.c b/source/os/src/osSystem.c new file mode 100644 index 0000000000000000000000000000000000000000..1a57e88c58c78ee5b0b9743346b1cec445848237 --- /dev/null +++ b/source/os/src/osSystem.c @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "os.h" + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + +/* + * windows implementation + */ + +void* taosLoadDll(const char* filename) { return NULL; } +void* taosLoadSym(void* handle, char* name) { return NULL; } +void taosCloseDll(void* handle) {} + +int taosSetConsoleEcho(bool on) { + HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); + DWORD mode = 0; + GetConsoleMode(hStdin, &mode); + if (on) { + mode |= ENABLE_ECHO_INPUT; + } else { + mode &= ~ENABLE_ECHO_INPUT; + } + SetConsoleMode(hStdin, mode); + + return 0; +} + +#elif defined(_TD_DARWIN_64) + +/* + * darwin implementation + */ + +void* taosLoadDll(const char* filename) { return NULL; } +void* taosLoadSym(void* handle, char* name) { return NULL; } +void taosCloseDll(void* handle) {} + +int taosSetConsoleEcho(bool on) { +#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL) + int err; + struct termios term; + + if (tcgetattr(STDIN_FILENO, &term) == -1) { + perror("Cannot get the attribution of the terminal"); + return -1; + } + + if (on) + term.c_lflag |= ECHOFLAGS; + else + term.c_lflag &= ~ECHOFLAGS; + + err = tcsetattr(STDIN_FILENO, TCSAFLUSH, &term); + if (err == -1 && err == EINTR) { + perror("Cannot set the attribution of the terminal"); + return -1; + } + + return 0; +} + +#else + +/* + * linux implementation + */ + +#include +#include +#include + +void* taosLoadDll(const char* filename) { + void* handle = dlopen(filename, RTLD_LAZY); + if (!handle) { + printf("load dll:%s failed, error:%s", filename, dlerror()); + return NULL; + } + + printf("dll %s loaded", filename); + + return handle; +} + +void* taosLoadSym(void* handle, char* name) { + void* sym = dlsym(handle, name); + char* error = NULL; + + if ((error = dlerror()) != NULL) { + printf("load sym:%s failed, error:%s", name, dlerror()); + return NULL; + } + + printf("sym %s loaded", name); + + return sym; +} + +void taosCloseDll(void* handle) { + if (handle) { + dlclose(handle); + } +} + +int taosSetConsoleEcho(bool on) { +#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL) + int err; + struct termios term; + + if (tcgetattr(STDIN_FILENO, &term) == -1) { + perror("Cannot get the attribution of the terminal"); + return -1; + } + + if (on) + term.c_lflag |= ECHOFLAGS; + else + term.c_lflag &= ~ECHOFLAGS; + + err = tcsetattr(STDIN_FILENO, TCSAFLUSH, &term); + if (err == -1 || err == EINTR) { + printf("Cannot set the attribution of the terminal"); + return -1; + } + + return 0; +} + +#endif diff --git a/src/os/src/windows/wTime.c b/source/os/src/osTime.c similarity index 65% rename from src/os/src/windows/wTime.c rename to source/os/src/osTime.c index 1484b13843fb7dc38f18e5d46bb1c2c6af3e1255..bf0585e86d7401c1a71c374cd0c32fb679576759 100644 --- a/src/os/src/windows/wTime.c +++ b/source/os/src/osTime.c @@ -13,10 +13,27 @@ * along with this program. If not, see . */ +#define _BSD_SOURCE + +#ifdef DARWIN +#define _XOPEN_SOURCE +#else +#define _XOPEN_SOURCE 500 +#endif + +#define _DEFAULT_SOURCE + +#include "os.h" + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +/* + * windows implementation + */ + #include #include -int gettimeofday(struct timeval *tv, struct timezone *tz) { +int taosGetTimeOfDay(struct timeval *tv, struct timezone *tz) { time_t t; t = time(NULL); SYSTEMTIME st; @@ -31,4 +48,19 @@ int gettimeofday(struct timeval *tv, struct timezone *tz) { struct tm *localtime_r(const time_t *timep, struct tm *result) { localtime_s(result, timep); return result; -} \ No newline at end of file +} + +#else + +/* + * linux and darwin implementation + */ + +#include +// #include "monotonic.h" + +FORCE_INLINE int32_t taosGetTimeOfDay(struct timeval *tv) { + return gettimeofday(tv, NULL); +} + +#endif diff --git a/source/os/src/osTimer.c b/source/os/src/osTimer.c new file mode 100644 index 0000000000000000000000000000000000000000..b1bf1bcd2d14d58a7f5df5bc8dd561e76df3b578 --- /dev/null +++ b/source/os/src/osTimer.c @@ -0,0 +1,243 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "os.h" + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + +/* + * windows implementation + */ + + +#include +#include +#include +#include + +#pragma warning( disable : 4244 ) + +typedef void (*win_timer_f)(int signo); + +void WINAPI taosWinOnTimer(UINT wTimerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR dwl, DWORD_PTR dw2) { + win_timer_f callback = *((win_timer_f *)&dwUser); + if (callback != NULL) { + callback(0); + } +} + +static MMRESULT timerId; +int taosInitTimer(win_timer_f callback, int ms) { + DWORD_PTR param = *((int64_t *) & callback); + + timerId = timeSetEvent(ms, 1, (LPTIMECALLBACK)taosWinOnTimer, param, TIME_PERIODIC); + if (timerId == 0) { + return -1; + } + return 0; +} + +void taosUninitTimer() { + timeKillEvent(timerId); +} + +#elif defined(_TD_DARWIN_64) + +/* + * darwin implementation + */ + +#include +#include +#include + +static void (*timer_callback)(int); +static int timer_ms = 0; +static pthread_t timer_thread; +static int timer_kq = -1; +static volatile int timer_stop = 0; + +static void* timer_routine(void *arg) { + (void)arg; + setThreadName("timer"); + + int r = 0; + struct timespec to = {0}; + to.tv_sec = timer_ms / 1000; + to.tv_nsec = (timer_ms % 1000) * 1000000; + while (!timer_stop) { + struct kevent64_s kev[10] = {0}; + r = kevent64(timer_kq, NULL, 0, kev, sizeof(kev)/sizeof(kev[0]), 0, &to); + if (r!=0) { + fprintf(stderr, "==%s[%d]%s()==kevent64 failed\n", basename(__FILE__), __LINE__, __func__); + abort(); + } + timer_callback(SIGALRM); // just mock + } + + return NULL; +} + +int taosInitTimer(void (*callback)(int), int ms) { + int r = 0; + timer_ms = ms; + timer_callback = callback; + + timer_kq = kqueue(); + if (timer_kq==-1) { + fprintf(stderr, "==%s[%d]%s()==failed to create timer kq\n", basename(__FILE__), __LINE__, __func__); + // since no caller of this func checks the return value for the moment + abort(); + } + + r = pthread_create(&timer_thread, NULL, timer_routine, NULL); + if (r) { + fprintf(stderr, "==%s[%d]%s()==failed to create timer thread\n", basename(__FILE__), __LINE__, __func__); + // since no caller of this func checks the return value for the moment + abort(); + } + return 0; +} + +void taosUninitTimer() { + int r = 0; + timer_stop = 1; + r = pthread_join(timer_thread, NULL); + if (r) { + fprintf(stderr, "==%s[%d]%s()==failed to join timer thread\n", basename(__FILE__), __LINE__, __func__); + // since no caller of this func checks the return value for the moment + abort(); + } + close(timer_kq); + timer_kq = -1; +} + +void taos_block_sigalrm(void) { + // we don't know if there's any specific API for SIGALRM to deliver to specific thread + // this implementation relies on kqueue rather than SIGALRM +} + +#else + +/* + * linux implementation + */ + +#include +#include + +static void taosDeleteTimer(void *tharg) { + timer_t *pTimer = tharg; + timer_delete(*pTimer); +} + +static pthread_t timerThread; +static timer_t timerId; +static volatile bool stopTimer = false; +static void *taosProcessAlarmSignal(void *tharg) { + // Block the signal + sigset_t sigset; + sigemptyset(&sigset); + sigaddset(&sigset, SIGALRM); + sigprocmask(SIG_BLOCK, &sigset, NULL); + void (*callback)(int) = tharg; + + struct sigevent sevent = {{0}}; + + setThreadName("tmr"); + + #ifdef _ALPINE + sevent.sigev_notify = SIGEV_THREAD; + sevent.sigev_value.sival_int = syscall(__NR_gettid); + #else + sevent.sigev_notify = SIGEV_THREAD_ID; + sevent._sigev_un._tid = syscall(__NR_gettid); + #endif + + sevent.sigev_signo = SIGALRM; + + if (timer_create(CLOCK_REALTIME, &sevent, &timerId) == -1) { + printf("Failed to create timer"); + } + + pthread_cleanup_push(taosDeleteTimer, &timerId); + + struct itimerspec ts; + ts.it_value.tv_sec = 0; + ts.it_value.tv_nsec = 1000000 * MSECONDS_PER_TICK; + ts.it_interval.tv_sec = 0; + ts.it_interval.tv_nsec = 1000000 * MSECONDS_PER_TICK; + + if (timer_settime(timerId, 0, &ts, NULL)) { + printf("Failed to init timer"); + return NULL; + } + + int signo; + while (!stopTimer) { + if (sigwait(&sigset, &signo)) { + printf("Failed to wait signal: number %d", signo); + continue; + } + /* printf("Signal handling: number %d ......\n", signo); */ + + callback(0); + } + + pthread_cleanup_pop(1); + + return NULL; +} + +int taosInitTimer(void (*callback)(int), int ms) { + pthread_attr_t tattr; + pthread_attr_init(&tattr); + int code = pthread_create(&timerThread, &tattr, taosProcessAlarmSignal, callback); + pthread_attr_destroy(&tattr); + if (code != 0) { + printf("failed to create timer thread"); + return -1; + } else { + printf("timer thread:0x%08" PRIx64 " is created", taosGetPthreadId(timerThread)); + } + + return 0; +} + +void taosUninitTimer() { + stopTimer = true; + + printf("join timer thread:0x%08" PRIx64, taosGetPthreadId(timerThread)); + pthread_join(timerThread, NULL); +} + +int64_t taosGetMonotonicMs() { +#if 0 + return getMonotonicUs() / 1000; +#else + return taosGetTimestampMs(); +#endif +} + +const char *taosMonotonicInit() { +#if 0 + return monotonicInit(); +#else + return NULL; +#endif +} + +#endif diff --git a/source/server/CMakeLists.txt b/source/server/CMakeLists.txt index ab88d501910345135505097c87ef441bf3c74913..1f6da54ebe4964adf651afbb59011913cc95a5ed 100644 --- a/source/server/CMakeLists.txt +++ b/source/server/CMakeLists.txt @@ -1,10 +1,11 @@ add_subdirectory(mnode) add_subdirectory(vnode) add_subdirectory(qnode) +add_subdirectory(dnode) -aux_source_directory(dnode TAOSD_SRC) +aux_source_directory(. TAOSD_SRC) add_executable(taosd ${TAOSD_SRC}) target_link_libraries( taosd - PUBLIC os + PUBLIC dnode ) \ No newline at end of file diff --git a/source/server/dnode/CMakeLists.txt b/source/server/dnode/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..e627ca94e815baf5781f50b51a9c8d2decd797d7 --- /dev/null +++ b/source/server/dnode/CMakeLists.txt @@ -0,0 +1,13 @@ +aux_source_directory(src DNODE_SRC) +add_library(dnode ${DNODE_SRC}) +target_link_libraries( + dnode + PUBLIC cjson + PUBLIC mnode + PUBLIC vnode +) +target_include_directories( + dnode + PUBLIC "${CMAKE_SOURCE_DIR}/include/server/dnode" + private "${CMAKE_CURRENT_SOURCE_DIR}/inc" +) \ No newline at end of file diff --git a/source/server/dnode/inc/dnodeCfg.h b/source/server/dnode/inc/dnodeCfg.h new file mode 100644 index 0000000000000000000000000000000000000000..1565e7649ac6b1c5be7fc52719323a048293e217 --- /dev/null +++ b/source/server/dnode/inc/dnodeCfg.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef _TD_DNODE_CFG_H_ +#define _TD_DNODE_CFG_H_ + +#ifdef __cplusplus +extern "C" { +#endif +#include "dnodeInt.h" + +typedef struct SDnCfg { + int32_t dnodeId; + int32_t dropped; + char clusterId[TSDB_CLUSTER_ID_LEN]; + char file[PATH_MAX + 20]; + pthread_mutex_t mutex; +} SDnCfg; + +int32_t dnodeInitCfg(SDnCfg **cfg); +void dnodeCleanupCfg(SDnCfg **cfg); +void dnodeUpdateCfg(SDnCfg *cfg, SDnodeCfg *data); +int32_t dnodeGetDnodeId(SDnCfg *cfg); +void dnodeGetClusterId(SDnCfg *cfg, char *clusterId); +void dnodeGetCfg(SDnCfg *cfg, int32_t *dnodeId, char *clusterId); +void dnodeSetDropped(SDnCfg *cfg); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_DNODE_CFG_H_*/ diff --git a/src/dnode/inc/dnodeCheck.h b/source/server/dnode/inc/dnodeCheck.h similarity index 77% rename from src/dnode/inc/dnodeCheck.h rename to source/server/dnode/inc/dnodeCheck.h index c94b9e93197f4bfc9a175ee5825fd2c1c25d7072..29172ba4dfc7a19eded484e8cfc45982f562d172 100644 --- a/src/dnode/inc/dnodeCheck.h +++ b/source/server/dnode/inc/dnodeCheck.h @@ -13,19 +13,22 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_DNODE_CHECK_H -#define TDENGINE_DNODE_CHECK_H +#ifndef _TD_DNODE_CHECK_H_ +#define _TD_DNODE_CHECK_H_ #ifdef __cplusplus extern "C" { #endif #include "dnodeInt.h" -int32_t dnodeInitCheck(); -void dnodeCleanupCheck(); +typedef struct SDnCheck { +} SDnCheck; + +int32_t dnodeInitCheck(SDnCheck **check); +void dnodeCleanupCheck(SDnCheck **check); #ifdef __cplusplus } #endif -#endif +#endif /*_TD_DNODE_CHECK_H_*/ diff --git a/src/dnode/inc/dnodeCfg.h b/source/server/dnode/inc/dnodeEps.h similarity index 54% rename from src/dnode/inc/dnodeCfg.h rename to source/server/dnode/inc/dnodeEps.h index 99733e46ef10ca7c17c1f49b344414ce7f564574..8019a819337caa4989fb5559e08f561fa4d08b9f 100644 --- a/src/dnode/inc/dnodeCfg.h +++ b/source/server/dnode/inc/dnodeEps.h @@ -13,24 +13,32 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_DNODE_CFG_H -#define TDENGINE_DNODE_CFG_H +#ifndef _TD_DNODE_EPS_H_ +#define _TD_DNODE_EPS_H_ #ifdef __cplusplus extern "C" { #endif +#include "thash.h" #include "dnodeInt.h" -int32_t dnodeInitCfg(); -void dnodeCleanupCfg(); -void dnodeUpdateCfg(SDnodeCfg *cfg); -int32_t dnodeGetDnodeId(); -void dnodeGetClusterId(char *clusterId); -void dnodeGetCfg(int32_t *dnodeId, char *clusterId); -void dnodeSetDropped(); +typedef struct SDnEps { + int32_t dnodeId; + int32_t dnodeNum; + SDnodeEp * dnodeList; + SHashObj * dnodeHash; + char file[PATH_MAX + 20]; + pthread_mutex_t mutex; +} SDnEps; + +int32_t dnodeInitEps(SDnEps **eps); +void dnodeCleanupEps(SDnEps **eps); +void dnodeUpdateEps(SDnEps *eps, SDnodeEps *data); +bool dnodeIsDnodeEpChanged(SDnEps *eps, int32_t dnodeId, char *epstr); +void dnodeGetDnodeEp(int32_t dnodeId, char *epstr, char *fqdn, uint16_t *port); #ifdef __cplusplus } #endif -#endif +#endif /*_TD_DNODE_EPS_H_*/ \ No newline at end of file diff --git a/src/dnode/inc/dnodeInt.h b/source/server/dnode/inc/dnodeInt.h similarity index 73% rename from src/dnode/inc/dnodeInt.h rename to source/server/dnode/inc/dnodeInt.h index 1327cd4433fd2e2157becaaf5cb52e2ca0ffe6ef..9b56147ca0d94bbeca0c93b3b593e479ee50b6c4 100644 --- a/src/dnode/inc/dnodeInt.h +++ b/source/server/dnode/inc/dnodeInt.h @@ -13,21 +13,42 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_DNODE_LOG_H -#define TDENGINE_DNODE_LOG_H +#ifndef _TD_DNODE_INT_H_ +#define _TD_DNODE_INT_H_ #ifdef __cplusplus extern "C" { #endif #include "taoserror.h" #include "taosmsg.h" +#include "tglobal.h" #include "tlog.h" #include "trpc.h" -#include "tglobal.h" +#include "tstep.h" #include "dnode.h" -#include "vnode.h" -extern int32_t dDebugFlag; +struct SDnCfg; +struct SDnCheck; +struct SDnEps; +struct SDnMnEps; +struct SDnStatus; +struct SDnTelem; +struct SDnTrans; +struct SDnMain; + +typedef struct SDnode { + struct SSteps* steps; + struct SDnCfg* cfg; + struct SDnCheck* check; + struct SDnEps* eps; + struct SDnMnEps* meps; + struct SDnStatus* status; + struct SDnTelem* telem; + struct SDnTrans* trans; + struct SDnMain* main; +} SDnode; + +SDnode* dnodeInst(); #define dFatal(...) { if (dDebugFlag & DEBUG_FATAL) { taosPrintLog("DND FATAL ", 255, __VA_ARGS__); }} #define dError(...) { if (dDebugFlag & DEBUG_ERROR) { taosPrintLog("DND ERROR ", 255, __VA_ARGS__); }} @@ -36,16 +57,8 @@ extern int32_t dDebugFlag; #define dDebug(...) { if (dDebugFlag & DEBUG_DEBUG) { taosPrintLog("DND ", dDebugFlag, __VA_ARGS__); }} #define dTrace(...) { if (dDebugFlag & DEBUG_TRACE) { taosPrintLog("DND ", dDebugFlag, __VA_ARGS__); }} -typedef enum { - TSDB_RUN_STATUS_INITIALIZE, - TSDB_RUN_STATUS_RUNING, - TSDB_RUN_STATUS_STOPPED -} SRunStatus; - -SRunStatus dnodeGetRunStatus(); - #ifdef __cplusplus } #endif -#endif +#endif /*_TD_DNODE_INT_H_*/ \ No newline at end of file diff --git a/source/server/dnode/inc/dnodeMain.h b/source/server/dnode/inc/dnodeMain.h new file mode 100644 index 0000000000000000000000000000000000000000..08f6e108307306b6473548c22ceb8c74e576f896 --- /dev/null +++ b/source/server/dnode/inc/dnodeMain.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef _TD_DNODE_MAIN_H_ +#define _TD_DNODE_MAIN_H_ + +#ifdef __cplusplus +extern "C" { +#endif +#include "dnodeInt.h" + +typedef enum { + TD_RUN_STAT_INIT, + TD_RUN_STAT_RUNNING, + TD_RUN_STAT_STOPPED +} RunStat; + +typedef struct SDnMain { + RunStat runStatus; + void * dnodeTimer; + SStartupStep startup; +} SDnMain; + +int32_t dnodeInitMain(SDnMain **main); +void dnodeCleanupMain(SDnMain **main); +int32_t dnodeInitStorage(); +void dnodeCleanupStorage(); +void dnodeReportStartup(char *name, char *desc); +void dnodeReportStartupFinished(char *name, char *desc); +void dnodeProcessStartupReq(SRpcMsg *pMsg); +void dnodeProcessCreateMnodeReq(SRpcMsg *pMsg); +void dnodeProcessConfigDnodeReq(SRpcMsg *pMsg); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_DNODE_MAIN_H_*/ diff --git a/src/dnode/inc/dnodeMInfos.h b/source/server/dnode/inc/dnodeMnodeEps.h similarity index 56% rename from src/dnode/inc/dnodeMInfos.h rename to source/server/dnode/inc/dnodeMnodeEps.h index f05e2b6f7b710f9ebe547e27bf524c13ffcfaea8..a70c621046ce2e4c34032fa6a1a21455a6d25539 100644 --- a/src/dnode/inc/dnodeMInfos.h +++ b/source/server/dnode/inc/dnodeMnodeEps.h @@ -13,27 +13,31 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_DNODE_MINFOS_H -#define TDENGINE_DNODE_MINFOS_H +#ifndef _TD_DNODE_MNODE_EP_H_ +#define _TD_DNODE_MNODE_EP_H_ #ifdef __cplusplus extern "C" { #endif #include "dnodeInt.h" -int32_t dnodeInitMInfos(); -void dnodeCleanupMInfos(); -void dnodeUpdateMInfos(SMInfos *pMinfos); -void dnodeUpdateEpSetForPeer(SRpcEpSet *pEpSet); -void dnodeGetMInfos(SMInfos *pMinfos); -bool dnodeIsMasterEp(char *ep); +typedef struct SDnMnEps { + SRpcEpSet mnodeEpSet; + SMInfos mnodeInfos; + char file[PATH_MAX + 20]; + pthread_mutex_t mutex; +} SDnMnEps; +int32_t dnodeInitMnodeEps(SDnMnEps **meps); +void dnodeCleanupMnodeEps(SDnMnEps **meps); +void dnodeUpdateMnodeFromStatus(SDnMnEps *meps, SMInfos *pMinfos); +void dnodeUpdateMnodeFromPeer(SDnMnEps *meps, SRpcEpSet *pEpSet); +void dnodeGetEpSetForPeer(SDnMnEps *meps, SRpcEpSet *epSet); +void dnodeGetEpSetForShell(SDnMnEps *meps, SRpcEpSet *epSet); void dnodeSendRedirectMsg(SRpcMsg *rpcMsg, bool forShell); -void dnodeGetEpSetForPeer(SRpcEpSet *epSet); -void dnodeGetEpSetForShell(SRpcEpSet *epSet); #ifdef __cplusplus } #endif -#endif +#endif /*_TD_DNODE_MNODE_EP_H_*/ diff --git a/src/dnode/inc/dnodeEps.h b/source/server/dnode/inc/dnodeStatus.h similarity index 68% rename from src/dnode/inc/dnodeEps.h rename to source/server/dnode/inc/dnodeStatus.h index a5840997b0f9623c8ed504fa1c2eb8209216ea29..65a349ba6d03e83f49d8cd5b225477a1124c996e 100644 --- a/src/dnode/inc/dnodeEps.h +++ b/source/server/dnode/inc/dnodeStatus.h @@ -13,22 +13,26 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_DNODE_EP_H -#define TDENGINE_DNODE_EP_H +#ifndef _TD_DNODE_STATUS_H_ +#define _TD_DNODE_STATUS_H_ #ifdef __cplusplus extern "C" { #endif #include "dnodeInt.h" -int32_t dnodeInitEps(); -void dnodeCleanupEps(); -void dnodeUpdateEps(SDnodeEps *eps); -void dnodeUpdateEp(int32_t dnodeId, char *epstr, char *fqdn, uint16_t *port); -bool dnodeCheckEpChanged(int32_t dnodeId, char *epstr); +typedef struct SDnStatus { + void * dnodeTimer; + void * statusTimer; + uint32_t rebootTime; +} SDnStatus; + +int32_t dnodeInitStatus(SDnStatus **status); +void dnodeCleanupStatus(SDnStatus **status); +void dnodeProcessStatusRsp(SRpcMsg *pMsg); #ifdef __cplusplus } #endif -#endif +#endif /*_TD_DNODE_STATUS_H_*/ \ No newline at end of file diff --git a/src/dnode/inc/dnodeTelemetry.h b/source/server/dnode/inc/dnodeTelem.h similarity index 58% rename from src/dnode/inc/dnodeTelemetry.h rename to source/server/dnode/inc/dnodeTelem.h index e4fd5a0376e3e7fdca0bbe1be15fa1cd70fd7695..94356fffba9319c1df8cf22dc545d4ecee2e7626 100644 --- a/src/dnode/inc/dnodeTelemetry.h +++ b/source/server/dnode/inc/dnodeTelem.h @@ -13,19 +13,32 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_DNODE_TELEMETRY_H -#define TDENGINE_DNODE_TELEMETRY_H +#ifndef _TD_DNODE_TELEMETRY_H_ +#define _TD_DNODE_TELEMETRY_H_ #ifdef __cplusplus extern "C" { #endif #include "dnodeInt.h" -int32_t dnodeInitTelemetry(); -void dnodeCleanupTelemetry(); +/* + * sem_timedwait is NOT implemented on MacOSX + * thus we use pthread_mutex_t/pthread_cond_t to simulate + */ +typedef struct SDnTelem { + bool enable; + pthread_mutex_t lock; + pthread_cond_t cond; + volatile int32_t exit; + pthread_t thread; + char email[TSDB_FQDN_LEN]; +} SDnTelem; + +int32_t dnodeInitTelem(SDnTelem **telem); +void dnodeCleanupTelem(SDnTelem **telem); #ifdef __cplusplus } #endif -#endif +#endif /*_TD_DNODE_TELEMETRY_H_*/ diff --git a/source/server/dnode/inc/dnodeTrans.h b/source/server/dnode/inc/dnodeTrans.h new file mode 100644 index 0000000000000000000000000000000000000000..d9016f0c7bc689262de8d83644044a27b4f4cc98 --- /dev/null +++ b/source/server/dnode/inc/dnodeTrans.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef _TD_DNODE_TRANS_H_ +#define _TD_DNODE_TRANS_H_ + +#ifdef __cplusplus +extern "C" { +#endif +#include "dnodeInt.h" + +typedef void (*RpcMsgFp)( SRpcMsg *pMsg); + +typedef struct SDnTrans { + void * serverRpc; + void * clientRpc; + void * shellRpc; + int32_t queryReqNum; + int32_t submitReqNum; + RpcMsgFp peerMsgFp[TSDB_MSG_TYPE_MAX]; + RpcMsgFp shellMsgFp[TSDB_MSG_TYPE_MAX]; +} SDnTrans; + +int32_t dnodeInitTrans(SDnTrans **rans); +void dnodeCleanupTrans(SDnTrans **trans); +void dnodeSendMsgToMnode(SRpcMsg *rpcMsg); +void dnodeSendMsgToDnode(SRpcEpSet *epSet, SRpcMsg *rpcMsg); +void dnodeSendMsgToDnodeRecv(SRpcMsg *rpcMsg, SRpcMsg *rpcRsp, SRpcEpSet *epSet); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_DNODE_TRANS_H_*/ diff --git a/source/server/dnode/main.c b/source/server/dnode/main.c deleted file mode 100644 index 75449ecd1a1c3b5589e801d52e1ed2928114e8ac..0000000000000000000000000000000000000000 --- a/source/server/dnode/main.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ -#include "os.h" - -int main(int argc, char const *argv[]) { - printf("Hello world!\n"); - return 0; -} diff --git a/source/server/dnode/src/dnodeCfg.c b/source/server/dnode/src/dnodeCfg.c new file mode 100644 index 0000000000000000000000000000000000000000..ee8a17db082de9ada1da9028feb562dc0ebb32c1 --- /dev/null +++ b/source/server/dnode/src/dnodeCfg.c @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "os.h" +#include "cJSON.h" +#include "dnodeCfg.h" + +static int32_t dnodeReadCfg(SDnCfg *cfg) { + int32_t len = 0; + int32_t maxLen = 200; + char * content = calloc(1, maxLen + 1); + cJSON * root = NULL; + FILE * fp = NULL; + + fp = fopen(cfg->file, "r"); + if (!fp) { + dDebug("file %s not exist", cfg->file); + goto PARSE_CFG_OVER; + } + + len = (int32_t)fread(content, 1, maxLen, fp); + if (len <= 0) { + dError("failed to read %s since content is null", cfg->file); + goto PARSE_CFG_OVER; + } + + content[len] = 0; + root = cJSON_Parse(content); + if (root == NULL) { + dError("failed to read %s since invalid json format", cfg->file); + goto PARSE_CFG_OVER; + } + + cJSON *dnodeId = cJSON_GetObjectItem(root, "dnodeId"); + if (!dnodeId || dnodeId->type != cJSON_Number) { + dError("failed to read %s since dnodeId not found", cfg->file); + goto PARSE_CFG_OVER; + } + cfg->dnodeId = (int32_t)dnodeId->valueint; + + cJSON *dropped = cJSON_GetObjectItem(root, "dropped"); + if (!dropped || dropped->type != cJSON_Number) { + dError("failed to read %s since dropped not found", cfg->file); + goto PARSE_CFG_OVER; + } + cfg->dropped = (int32_t)dropped->valueint; + + cJSON *clusterId = cJSON_GetObjectItem(root, "clusterId"); + if (!clusterId || clusterId->type != cJSON_String) { + dError("failed to read %s since clusterId not found", cfg->file); + goto PARSE_CFG_OVER; + } + tstrncpy(cfg->clusterId, clusterId->valuestring, TSDB_CLUSTER_ID_LEN); + + dInfo("successed to read %s", cfg->file); + +PARSE_CFG_OVER: + if (content != NULL) free(content); + if (root != NULL) cJSON_Delete(root); + if (fp != NULL) fclose(fp); + terrno = 0; + + return 0; +} + +static int32_t dnodeWriteCfg(SDnCfg *cfg) { + FILE *fp = fopen(cfg->file, "w"); + if (!fp) { + dError("failed to write %s since %s", cfg->file, strerror(errno)); + return -1; + } + + int32_t len = 0; + int32_t maxLen = 200; + char * content = calloc(1, maxLen + 1); + + len += snprintf(content + len, maxLen - len, "{\n"); + len += snprintf(content + len, maxLen - len, " \"dnodeId\": %d,\n", cfg->dnodeId); + len += snprintf(content + len, maxLen - len, " \"dropped\": %d,\n", cfg->dropped); + len += snprintf(content + len, maxLen - len, " \"clusterId\": \"%s\"\n", cfg->clusterId); + len += snprintf(content + len, maxLen - len, "}\n"); + + fwrite(content, 1, len, fp); + taosFsyncFile(fileno(fp)); + fclose(fp); + free(content); + terrno = 0; + + dInfo("successed to write %s", cfg->file); + return 0; +} + +int32_t dnodeInitCfg(SDnCfg **out) { + SDnCfg* cfg = calloc(1, sizeof(SDnCfg)); + if (cfg == NULL) return -1; + + cfg->dnodeId = 0; + cfg->dropped = 0; + cfg->clusterId[0] = 0; + snprintf(cfg->file, sizeof(cfg->file), "%s/dnodeCfg.json", tsDnodeDir); + pthread_mutex_init(&cfg->mutex, NULL); + *out = cfg; + + int32_t ret = dnodeReadCfg(cfg); + if (ret == 0) { + dInfo("dnode cfg is initialized"); + } + + if (cfg->dropped) { + dInfo("dnode is dropped and start to exit"); + return -1; + } + + return ret; +} + +void dnodeCleanupCfg(SDnCfg **out) { + SDnCfg* cfg = *out; + *out = NULL; + + pthread_mutex_destroy(&cfg->mutex); + free(cfg); +} + +void dnodeUpdateCfg(SDnCfg *cfg, SDnodeCfg *data) { + if (cfg == NULL || cfg->dnodeId == 0) return; + + pthread_mutex_lock(&cfg->mutex); + + cfg->dnodeId = data->dnodeId; + tstrncpy(cfg->clusterId, data->clusterId, TSDB_CLUSTER_ID_LEN); + dInfo("dnodeId is set to %d, clusterId is set to %s", cfg->dnodeId, cfg->clusterId); + + dnodeWriteCfg(cfg); + pthread_mutex_unlock(&cfg->mutex); +} + +void dnodeSetDropped(SDnCfg *cfg) { + pthread_mutex_lock(&cfg->mutex); + cfg->dropped = 1; + dnodeWriteCfg(cfg); + pthread_mutex_unlock(&cfg->mutex); +} + +int32_t dnodeGetDnodeId(SDnCfg *cfg) { + int32_t dnodeId = 0; + pthread_mutex_lock(&cfg->mutex); + dnodeId = cfg->dnodeId; + pthread_mutex_unlock(&cfg->mutex); + return dnodeId; +} + +void dnodeGetClusterId(SDnCfg *cfg, char *clusterId) { + pthread_mutex_lock(&cfg->mutex); + tstrncpy(clusterId, cfg->clusterId, TSDB_CLUSTER_ID_LEN); + pthread_mutex_unlock(&cfg->mutex); +} + +void dnodeGetCfg(SDnCfg *cfg, int32_t *dnodeId, char *clusterId) { + pthread_mutex_lock(&cfg->mutex); + *dnodeId = cfg->dnodeId; + tstrncpy(clusterId, cfg->clusterId, TSDB_CLUSTER_ID_LEN); + pthread_mutex_unlock(&cfg->mutex); +} diff --git a/source/server/dnode/src/dnodeCheck.c b/source/server/dnode/src/dnodeCheck.c new file mode 100644 index 0000000000000000000000000000000000000000..8f561b1ea05174c11d3ba79970b098cf36325a22 --- /dev/null +++ b/source/server/dnode/src/dnodeCheck.c @@ -0,0 +1,203 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "os.h" +#include "tglobal.h" +#include "dnodeCheck.h" + +#define MIN_AVAIL_MEMORY_MB 32 + +static int32_t dnodeBindTcpPort(uint16_t port) { +#if 0 + SOCKET serverSocket; + struct sockaddr_in server_addr; + + if ((serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { + dError("failed to create tcp socket since %s", strerror(errno)); + return -1; + } + + bzero(&server_addr, sizeof(server_addr)); + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(port); + server_addr.sin_addr.s_addr = htonl(INADDR_ANY); + + if (bind(serverSocket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + dError("failed to bind tcp port:%d since %s", port, strerror(errno)); + taosCloseSocket(serverSocket); + return -1; + } + + if (listen(serverSocket, 5) < 0) { + dError("failed to listen tcp port:%d since %s", port, strerror(errno)); + taosCloseSocket(serverSocket); + return -1; + } + + taosCloseSocket(serverSocket); +#endif + return 0; +} + +static int32_t dnodeBindUdpPort(int16_t port) { +#if 0 + SOCKET serverSocket; + struct sockaddr_in server_addr; + + if ((serverSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { + dError("failed to create udp socket since %s", strerror(errno)); + return -1; + } + + bzero(&server_addr, sizeof(server_addr)); + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(port); + server_addr.sin_addr.s_addr = htonl(INADDR_ANY); + + if (bind(serverSocket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + dError("failed to bind udp port:%d since %s", port, strerror(errno)); + taosCloseSocket(serverSocket); + return -1; + } + + taosCloseSocket(serverSocket); +#endif + return 0; +} + +static int32_t dnodeCheckNetwork() { + int32_t ret; + uint16_t startPort = tsServerPort; + + for (uint16_t port = startPort; port < startPort + 12; port++) { + ret = dnodeBindTcpPort(port); + if (0 != ret) { + dError("failed to bind tcp port:%d", port); + return -1; + } + ret = dnodeBindUdpPort(port); + if (0 != ret) { + dError("failed to bind udp port:%d", port); + return -1; + } + } + + return 0; +} + +static int32_t dnodeCheckMem() { +#if 0 + float memoryUsedMB; + float memoryAvailMB; + if (true != taosGetSysMemory(&memoryUsedMB)) { + dError("failed to get system memory since %s, errno:%u,", strerror(errno), errno); + return -1; + } + + memoryAvailMB = (float)tsTotalMemoryMB - memoryUsedMB; + + if (memoryAvailMB < MIN_AVAIL_MEMORY_MB) { + dError("available memory %fMB less than the threshold %dMB", memoryAvailMB, MIN_AVAIL_MEMORY_MB); + return -1; + } +#endif + return 0; +} + +static int32_t dnodeCheckDisk() { +#if 0 + taosGetDisk(); + + if (tsAvailDataDirGB < tsMinimalDataDirGB) { + dError("dataDir disk size:%fGB less than threshold %fGB ", tsAvailDataDirGB, tsMinimalDataDirGB); + return -1; + } + + if (tsAvailLogDirGB < tsMinimalLogDirGB) { + dError("logDir disk size:%fGB less than threshold %fGB", tsAvailLogDirGB, tsMinimalLogDirGB); + return -1; + } + + if (tsAvailTmpDirectorySpace < tsReservedTmpDirectorySpace) { + dError("tmpDir disk size:%fGB less than threshold %fGB", tsAvailTmpDirectorySpace, tsReservedTmpDirectorySpace); + return -1; + } +#endif + return 0; +} + +static int32_t dnodeCheckCpu() { return 0; } +static int32_t dnodeCheckOs() { return 0; } +static int32_t dnodeCheckAccess() { return 0; } +static int32_t dnodeCheckVersion() { return 0; } +static int32_t dnodeCheckDatafile() { return 0; } + +int32_t dnodeInitCheck(SDnCheck **out) { + SDnCheck *check = calloc(1, sizeof(SDnCheck)); + if (check == NULL) return -1; + + *out = check; + + if (dnodeCheckNetwork() != 0) { + dError("failed to check network"); + return -1; + } + + if (dnodeCheckMem() != 0) { + dError("failed to check memory"); + return -1; + } + + if (dnodeCheckCpu() != 0) { + dError("failed to check cpu"); + return -1; + } + + if (dnodeCheckDisk() != 0) { + dError("failed to check disk"); + return -1; + } + + if (dnodeCheckOs() != 0) { + dError("failed to check os"); + return -1; + } + + if (dnodeCheckAccess() != 0) { + dError("failed to check access"); + return -1; + } + + if (dnodeCheckVersion() != 0) { + dError("failed to check version"); + return -1; + } + + if (dnodeCheckDatafile() != 0) { + dError("failed to check datafile"); + return -1; + } + + dInfo("dnode check is finished"); + return 0; +} + +void dnodeCleanupCheck(SDnCheck **out) { + SDnCheck *check = *out; + *out = NULL; + + free(check); +} \ No newline at end of file diff --git a/source/server/dnode/src/dnodeEps.c b/source/server/dnode/src/dnodeEps.c new file mode 100644 index 0000000000000000000000000000000000000000..317a9968fa4155cea5aa821a7b459e3621543d54 --- /dev/null +++ b/source/server/dnode/src/dnodeEps.c @@ -0,0 +1,283 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "os.h" +#include "cJSON.h" +#include "tglobal.h" +#include "dnodeEps.h" +#include "dnodeCfg.h" + +static void dnodePrintEps(SDnEps *eps) { + dDebug("print dnodeEp, dnodeNum:%d", eps->dnodeNum); + for (int32_t i = 0; i < eps->dnodeNum; i++) { + SDnodeEp *ep = &eps->dnodeList[i]; + dDebug("dnode:%d, dnodeFqdn:%s dnodePort:%u", ep->dnodeId, ep->dnodeFqdn, ep->dnodePort); + } +} + +static void dnodeResetEps(SDnEps *eps, SDnodeEps *data) { + assert(data != NULL); + + if (data->dnodeNum > eps->dnodeNum) { + SDnodeEp *tmp = calloc(data->dnodeNum, sizeof(SDnodeEp)); + if (tmp == NULL) return; + + tfree(eps->dnodeList); + eps->dnodeList = tmp; + eps->dnodeNum = data->dnodeNum; + memcpy(eps->dnodeList, data->dnodeEps, eps->dnodeNum * sizeof(SDnodeEp)); + dnodePrintEps(eps); + + for (int32_t i = 0; i < eps->dnodeNum; ++i) { + SDnodeEp *ep = &eps->dnodeList[i]; + taosHashPut(eps->dnodeHash, &ep->dnodeId, sizeof(int32_t), ep, sizeof(SDnodeEp)); + } + } +} + +static int32_t dnodeReadEps(SDnEps *eps) { + int32_t len = 0; + int32_t maxLen = 30000; + char * content = calloc(1, maxLen + 1); + cJSON * root = NULL; + FILE * fp = NULL; + + fp = fopen(eps->file, "r"); + if (!fp) { + dDebug("file %s not exist", eps->file); + goto PRASE_EPS_OVER; + } + + len = (int32_t)fread(content, 1, maxLen, fp); + if (len <= 0) { + dError("failed to read %s since content is null", eps->file); + goto PRASE_EPS_OVER; + } + + content[len] = 0; + root = cJSON_Parse(content); + if (root == NULL) { + dError("failed to read %s since invalid json format", eps->file); + goto PRASE_EPS_OVER; + } + + cJSON *dnodeNum = cJSON_GetObjectItem(root, "dnodeNum"); + if (!dnodeNum || dnodeNum->type != cJSON_Number) { + dError("failed to read %s since dnodeNum not found", eps->file); + goto PRASE_EPS_OVER; + } + + cJSON *dnodeInfos = cJSON_GetObjectItem(root, "dnodeInfos"); + if (!dnodeInfos || dnodeInfos->type != cJSON_Array) { + dError("failed to read %s since dnodeInfos not found", eps->file); + goto PRASE_EPS_OVER; + } + + int32_t dnodeInfosSize = cJSON_GetArraySize(dnodeInfos); + if (dnodeInfosSize != dnodeNum->valueint) { + dError("failed to read %s since dnodeInfos size:%d not matched dnodeNum:%d", eps->file, dnodeInfosSize, + (int32_t)dnodeNum->valueint); + goto PRASE_EPS_OVER; + } + + eps->dnodeNum = dnodeInfosSize; + eps->dnodeList = calloc(dnodeInfosSize, sizeof(SDnodeEp)); + if (eps->dnodeList == NULL) { + dError("failed to calloc dnodeEpList since %s", strerror(errno)); + goto PRASE_EPS_OVER; + } + + for (int32_t i = 0; i < dnodeInfosSize; ++i) { + cJSON *dnodeInfo = cJSON_GetArrayItem(dnodeInfos, i); + if (dnodeInfo == NULL) break; + + SDnodeEp *ep = &eps->dnodeList[i]; + + cJSON *dnodeId = cJSON_GetObjectItem(dnodeInfo, "dnodeId"); + if (!dnodeId || dnodeId->type != cJSON_Number) { + dError("failed to read %s, dnodeId not found", eps->file); + goto PRASE_EPS_OVER; + } + ep->dnodeId = (int32_t)dnodeId->valueint; + + cJSON *dnodeFqdn = cJSON_GetObjectItem(dnodeInfo, "dnodeFqdn"); + if (!dnodeFqdn || dnodeFqdn->type != cJSON_String || dnodeFqdn->valuestring == NULL) { + dError("failed to read %s, dnodeFqdn not found", eps->file); + goto PRASE_EPS_OVER; + } + tstrncpy(ep->dnodeFqdn, dnodeFqdn->valuestring, TSDB_FQDN_LEN); + + cJSON *dnodePort = cJSON_GetObjectItem(dnodeInfo, "dnodePort"); + if (!dnodePort || dnodePort->type != cJSON_Number) { + dError("failed to read %s, dnodePort not found", eps->file); + goto PRASE_EPS_OVER; + } + ep->dnodePort = (uint16_t)dnodePort->valueint; + } + + dInfo("succcessed to read file %s", eps->file); + dnodePrintEps(eps); + +PRASE_EPS_OVER: + if (content != NULL) free(content); + if (root != NULL) cJSON_Delete(root); + if (fp != NULL) fclose(fp); + + if (dnodeIsDnodeEpChanged(eps, eps->dnodeId, tsLocalEp)) { + dError("dnode:%d, localEp different from %s dnodeEps.json and need reconfigured", eps->dnodeId, tsLocalEp); + return -1; + } + + terrno = 0; + return 0; +} + +static int32_t dnodeWriteEps(SDnEps *eps) { + FILE *fp = fopen(eps->file, "w"); + if (!fp) { + dError("failed to write %s since %s", eps->file, strerror(errno)); + return -1; + } + + int32_t len = 0; + int32_t maxLen = 30000; + char * content = calloc(1, maxLen + 1); + + len += snprintf(content + len, maxLen - len, "{\n"); + len += snprintf(content + len, maxLen - len, " \"dnodeNum\": %d,\n", eps->dnodeNum); + len += snprintf(content + len, maxLen - len, " \"dnodeInfos\": [{\n"); + for (int32_t i = 0; i < eps->dnodeNum; ++i) { + SDnodeEp *ep = &eps->dnodeList[i]; + len += snprintf(content + len, maxLen - len, " \"dnodeId\": %d,\n", ep->dnodeId); + len += snprintf(content + len, maxLen - len, " \"dnodeFqdn\": \"%s\",\n", ep->dnodeFqdn); + len += snprintf(content + len, maxLen - len, " \"dnodePort\": %u\n", ep->dnodePort); + if (i < eps->dnodeNum - 1) { + len += snprintf(content + len, maxLen - len, " },{\n"); + } else { + len += snprintf(content + len, maxLen - len, " }]\n"); + } + } + len += snprintf(content + len, maxLen - len, "}\n"); + + fwrite(content, 1, len, fp); + taosFsyncFile(fileno(fp)); + fclose(fp); + free(content); + terrno = 0; + + dInfo("successed to write %s", eps->file); + return 0; +} + +int32_t dnodeInitEps(SDnEps **out) { + SDnEps *eps = calloc(1, sizeof(SDnEps)); + if (eps == NULL) return -1; + + eps->dnodeHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK); + if (eps->dnodeHash == NULL) return -1; + + eps->dnodeId = dnodeInst()->cfg->dnodeId; + eps->dnodeNum = 0; + snprintf(eps->file, sizeof(eps->file), "%s/dnodeEps.json", tsDnodeDir); + pthread_mutex_init(&eps->mutex, NULL); + *out = eps; + + int32_t ret = dnodeReadEps(eps); + if (ret == 0) { + dInfo("dnode eps is initialized"); + } + + return ret; +} + +void dnodeCleanupEps(SDnEps **out) { + SDnEps *eps = *out; + *out = NULL; + + pthread_mutex_lock(&eps->mutex); + + if (eps->dnodeList != NULL) { + free(eps->dnodeList); + eps->dnodeList = NULL; + } + + if (eps->dnodeHash) { + taosHashCleanup(eps->dnodeHash); + eps->dnodeHash = NULL; + } + + eps->dnodeNum = 0; + pthread_mutex_unlock(&eps->mutex); + pthread_mutex_destroy(&eps->mutex); + free(eps); +} + +void dnodeUpdateEps(SDnEps *eps, SDnodeEps *data) { + if (data == NULL || data->dnodeNum <= 0) return; + + data->dnodeNum = htonl(data->dnodeNum); + for (int32_t i = 0; i < data->dnodeNum; ++i) { + data->dnodeEps[i].dnodeId = htonl(data->dnodeEps[i].dnodeId); + data->dnodeEps[i].dnodePort = htons(data->dnodeEps[i].dnodePort); + } + + pthread_mutex_lock(&eps->mutex); + + if (data->dnodeNum != eps->dnodeNum) { + dnodeResetEps(eps, data); + dnodeWriteEps(eps); + } else { + int32_t size = data->dnodeNum * sizeof(SDnodeEp); + if (memcmp(eps->dnodeList, data->dnodeEps, size) != 0) { + dnodeResetEps(eps, data); + dnodeWriteEps(eps); + } + } + + pthread_mutex_unlock(&eps->mutex); +} + +bool dnodeIsDnodeEpChanged(SDnEps *eps, int32_t dnodeId, char *epstr) { + bool changed = false; + + pthread_mutex_lock(&eps->mutex); + + SDnodeEp *ep = taosHashGet(eps->dnodeHash, &dnodeId, sizeof(int32_t)); + if (ep != NULL) { + char epSaved[TSDB_EP_LEN + 1]; + snprintf(epSaved, TSDB_EP_LEN, "%s:%u", ep->dnodeFqdn, ep->dnodePort); + changed = strcmp(epstr, epSaved) != 0; + tstrncpy(epstr, epSaved, TSDB_EP_LEN); + } + + pthread_mutex_unlock(&eps->mutex); + + return changed; +} + +void dnodeGetDnodeEp(int32_t dnodeId, char *epstr, char *fqdn, uint16_t *port) { + SDnEps *eps = dnodeInst()->eps; + pthread_mutex_lock(&eps->mutex); + + SDnodeEp *ep = taosHashGet(eps->dnodeHash, &dnodeId, sizeof(int32_t)); + if (ep != NULL) { + if (port) *port = ep->dnodePort; + if (fqdn) tstrncpy(fqdn, ep->dnodeFqdn, TSDB_FQDN_LEN); + if (epstr) snprintf(epstr, TSDB_EP_LEN, "%s:%u", ep->dnodeFqdn, ep->dnodePort); + } + + pthread_mutex_unlock(&eps->mutex); +} diff --git a/source/server/dnode/src/dnodeInt.c b/source/server/dnode/src/dnodeInt.c new file mode 100644 index 0000000000000000000000000000000000000000..abb552cb02b9d629de5a14647165eedf6c5b7605 --- /dev/null +++ b/source/server/dnode/src/dnodeInt.c @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "os.h" +#if 0 +#include "qScript.h" +#include "tfile.h" +#include "tsync.h" +#include "twal.h" +#endif +#include "tstep.h" +#include "dnodeCfg.h" +#include "dnodeCheck.h" +#include "dnodeEps.h" +#include "dnodeMain.h" +#include "dnodeMnodeEps.h" +#include "dnodeStatus.h" +#include "dnodeTelem.h" +#include "dnodeTrans.h" +#include "mnode.h" +#include "vnode.h" + +SDnode *dnodeInst() { + static SDnode inst = {0}; + return &inst; +} + +static int32_t dnodeInitVnodeModule(void **unused) { + SVnodePara para; + para.fp.GetDnodeEp = dnodeGetDnodeEp; + para.fp.SendMsgToDnode = dnodeSendMsgToDnode; + para.fp.SendMsgToMnode = dnodeSendMsgToMnode; + + return vnodeInit(para); +} + +static int32_t dnodeInitMnodeModule(void **unused) { + SDnode *dnode = dnodeInst(); + + SMnodePara para; + para.fp.GetDnodeEp = dnodeGetDnodeEp; + para.fp.SendMsgToDnode = dnodeSendMsgToDnode; + para.fp.SendMsgToMnode = dnodeSendMsgToMnode; + para.fp.SendRedirectMsg = dnodeSendRedirectMsg; + para.dnodeId = dnode->cfg->dnodeId; + strncpy(para.clusterId, dnode->cfg->clusterId, sizeof(para.clusterId)); + + return mnodeInit(para); +} + +int32_t dnodeInit() { + struct SSteps *steps = taosStepInit(24, dnodeReportStartup); + if (steps == NULL) return -1; + + SDnode *dnode = dnodeInst(); + + taosStepAdd(steps, "dnode-main", (void **)&dnode->main, (InitFp)dnodeInitMain, (CleanupFp)dnodeCleanupMain); + taosStepAdd(steps, "dnode-storage", NULL, (InitFp)dnodeInitStorage, (CleanupFp)dnodeCleanupStorage); + //taosStepAdd(steps, "dnode-tfs", NULL, (InitFp)tfInit, (CleanupFp)tfCleanup); + taosStepAdd(steps, "dnode-rpc", NULL, (InitFp)rpcInit, (CleanupFp)rpcCleanup); + taosStepAdd(steps, "dnode-check", (void **)&dnode->check, (InitFp)dnodeInitCheck, (CleanupFp)dnodeCleanupCheck); + taosStepAdd(steps, "dnode-cfg", (void **)&dnode->cfg, (InitFp)dnodeInitCfg, (CleanupFp)dnodeCleanupCfg); + taosStepAdd(steps, "dnode-deps", (void **)&dnode->eps, (InitFp)dnodeInitEps, (CleanupFp)dnodeCleanupEps); + taosStepAdd(steps, "dnode-meps", (void **)&dnode->meps, (InitFp)dnodeInitMnodeEps, (CleanupFp)dnodeCleanupMnodeEps); + //taosStepAdd(steps, "dnode-wal", NULL, (InitFp)walInit, (CleanupFp)walCleanUp); + //taosStepAdd(steps, "dnode-sync", NULL, (InitFp)syncInit, (CleanupFp)syncCleanUp); + taosStepAdd(steps, "dnode-vnode", NULL, (InitFp)dnodeInitVnodeModule, (CleanupFp)vnodeCleanup); + taosStepAdd(steps, "dnode-mnode", NULL, (InitFp)dnodeInitMnodeModule, (CleanupFp)mnodeCleanup); + taosStepAdd(steps, "dnode-trans", (void **)&dnode->trans, (InitFp)dnodeInitTrans, (CleanupFp)dnodeCleanupTrans); + taosStepAdd(steps, "dnode-status", (void **)&dnode->status, (InitFp)dnodeInitStatus, (CleanupFp)dnodeCleanupStatus); + taosStepAdd(steps, "dnode-telem", (void **)&dnode->telem, (InitFp)dnodeInitTelem, (CleanupFp)dnodeCleanupTelem); + //taosStepAdd(steps, "dnode-script", NULL, (InitFp)scriptEnvPoolInit, (CleanupFp)scriptEnvPoolCleanup); + + dnode->steps = steps; + taosStepExec(dnode->steps); + + if (dnode->main) { + dnode->main->runStatus = TD_RUN_STAT_RUNNING; + dnodeReportStartupFinished("TDengine", "initialized successfully"); + dInfo("TDengine is initialized successfully"); + } + + return 0; +} + +void dnodeCleanup() { + SDnode *dnode = dnodeInst(); + if (dnode->main->runStatus != TD_RUN_STAT_STOPPED) { + dnode->main->runStatus = TD_RUN_STAT_STOPPED; + taosStepCleanup(dnode->steps); + } +} diff --git a/source/server/dnode/src/dnodeMain.c b/source/server/dnode/src/dnodeMain.c new file mode 100644 index 0000000000000000000000000000000000000000..410cb41eed25938eeafe818a3c955b8be502e5ed --- /dev/null +++ b/source/server/dnode/src/dnodeMain.c @@ -0,0 +1,280 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "os.h" +#include "tcache.h" +#include "tconfig.h" +#if 0 +#include "tfs.h" +#endif +#include "dnodeCfg.h" +#include "dnodeMain.h" +#include "mnode.h" +#include "tcompression.h" +#include "tnote.h" +#include "ttimer.h" + +static void dnodeCheckDataDirOpenned(char *dir) { +#if 0 + char filepath[256] = {0}; + snprintf(filepath, sizeof(filepath), "%s/.running", dir); + + int32_t fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO); + if (fd < 0) { + dError("failed to open lock file:%s, reason: %s, quit", filepath, strerror(errno)); + exit(0); + } + + int32_t ret = flock(fd, LOCK_EX | LOCK_NB); + if (ret != 0) { + dError("failed to lock file:%s ret:%d since %s, database may be running, quit", filepath, ret, strerror(errno)); + close(fd); + exit(0); + } +#endif +} + +void dnodePrintDiskInfo() { + dInfo("=================================="); + dInfo(" os totalDisk: %f(GB)", tsTotalDataDirGB); + dInfo(" os usedDisk: %f(GB)", tsUsedDataDirGB); + dInfo(" os availDisk: %f(GB)", tsAvailDataDirGB); + dInfo("=================================="); +} + +int32_t dnodeInitMain(SDnMain **out) { + SDnMain* main = calloc(1, sizeof(SDnMain)); + if (main == NULL) return -1; + + main->runStatus = TD_RUN_STAT_STOPPED; + main->dnodeTimer = taosTmrInit(100, 200, 60000, "DND-TMR"); + if (main->dnodeTimer == NULL) { + dError("failed to init dnode timer"); + return -1; + } + + *out = main; + + tscEmbedded = 1; + taosIgnSIGPIPE(); + taosBlockSIGPIPE(); + taosResolveCRC(); + taosInitGlobalCfg(); + taosReadGlobalLogCfg(); + taosSetCoreDump(tsEnableCoreFile); + + + if (!taosMkDir(tsLogDir)) { + printf("failed to create dir: %s, reason: %s\n", tsLogDir, strerror(errno)); + return -1; + } + + char temp[TSDB_FILENAME_LEN]; + sprintf(temp, "%s/taosdlog", tsLogDir); + if (taosInitLog(temp, tsNumOfLogLines, 1) < 0) { + printf("failed to init log file\n"); + } + + if (!taosReadGlobalCfg()) { + taosPrintGlobalCfg(); + dError("TDengine read global config failed"); + return -1; + } + + dInfo("start to initialize TDengine"); + + taosInitNotes(); + + return taosCheckGlobalCfg(); +} + +void dnodeCleanupMain(SDnMain **out) { + SDnMain *main = *out; + *out = NULL; + + if (main->dnodeTimer != NULL) { + taosTmrCleanUp(main->dnodeTimer); + main->dnodeTimer = NULL; + } + +#if 0 + taos_cleanup(); +#endif + taosCloseLog(); + taosStopCacheRefreshWorker(); + + free(main); +} + +int32_t dnodeInitStorage() { +#ifdef TD_TSZ + // compress module init + tsCompressInit(); +#endif + + // storage module init + if (tsDiskCfgNum == 1 && !taosMkDir(tsDataDir)) { + dError("failed to create dir:%s since %s", tsDataDir, strerror(errno)); + return -1; + } + +#if 0 + if (tfsInit(tsDiskCfg, tsDiskCfgNum) < 0) { + dError("failed to init TFS since %s", tstrerror(terrno)); + return -1; + } + + strncpy(tsDataDir, TFS_PRIMARY_PATH(), TSDB_FILENAME_LEN); +#endif + sprintf(tsMnodeDir, "%s/mnode", tsDataDir); + sprintf(tsVnodeDir, "%s/vnode", tsDataDir); + sprintf(tsDnodeDir, "%s/dnode", tsDataDir); + + if (!taosMkDir(tsMnodeDir)) { + dError("failed to create dir:%s since %s", tsMnodeDir, strerror(errno)); + return -1; + } + + if (!taosMkDir(tsDnodeDir)) { + dError("failed to create dir:%s since %s", tsDnodeDir, strerror(errno)); + return -1; + } + +#if 0 + if (tfsMkdir("vnode") < 0) { + dError("failed to create vnode dir since %s", tstrerror(terrno)); + return -1; + } + + if (tfsMkdir("vnode_bak") < 0) { + dError("failed to create vnode_bak dir since %s", tstrerror(terrno)); + return -1; + } + + + TDIR *tdir = tfsOpendir("vnode_bak/.staging"); + bool stagingNotEmpty = tfsReaddir(tdir) != NULL; + tfsClosedir(tdir); + + if (stagingNotEmpty) { + dError("vnode_bak/.staging dir not empty, fix it first."); + return -1; + } + + if (tfsMkdir("vnode_bak/.staging") < 0) { + dError("failed to create vnode_bak/.staging dir since %s", tstrerror(terrno)); + return -1; + } + + dnodeCheckDataDirOpenned(tsDnodeDir); + + taosGetDisk(); + dnodePrintDiskInfo(); +#endif + + dInfo("dnode storage is initialized at %s", tsDnodeDir); + return 0; +} + +void dnodeCleanupStorage() { +#if 0 + // storage destroy + tfsDestroy(); + + #ifdef TD_TSZ + // compress destroy + tsCompressExit(); + #endif +#endif +} + +void dnodeReportStartup(char *name, char *desc) { + SDnode *dnode = dnodeInst(); + if (dnode->main != NULL) { + SStartupStep *startup = &dnode->main->startup; + tstrncpy(startup->name, name, strlen(startup->name)); + tstrncpy(startup->desc, desc, strlen(startup->desc)); + startup->finished = 0; + } +} + +void dnodeReportStartupFinished(char *name, char *desc) { + SDnode *dnode = dnodeInst(); + SStartupStep *startup = &dnode->main->startup; + tstrncpy(startup->name, name, strlen(startup->name)); + tstrncpy(startup->desc, desc, strlen(startup->desc)); + startup->finished = 1; +} + +void dnodeProcessStartupReq(SRpcMsg *pMsg) { + dInfo("startup msg is received, cont:%s", (char *)pMsg->pCont); + + SDnode *dnode = dnodeInst(); + SStartupStep *pStep = rpcMallocCont(sizeof(SStartupStep)); + memcpy(pStep, &dnode->main->startup, sizeof(SStartupStep)); + + dDebug("startup msg is sent, step:%s desc:%s finished:%d", pStep->name, pStep->desc, pStep->finished); + + SRpcMsg rpcRsp = {.handle = pMsg->handle, .pCont = pStep, .contLen = sizeof(SStartupStep)}; + rpcSendResponse(&rpcRsp); + rpcFreeCont(pMsg->pCont); +} + +static int32_t dnodeStartMnode(SRpcMsg *pMsg) { + SDnode *dnode = dnodeInst(); + SCreateMnodeMsg *pCfg = pMsg->pCont; + pCfg->dnodeId = htonl(pCfg->dnodeId); + if (pCfg->dnodeId != dnode->cfg->dnodeId) { + dDebug("dnode:%d, in create meps msg is not equal with saved dnodeId:%d", pCfg->dnodeId, + dnodeGetDnodeId(dnode->cfg)); + return TSDB_CODE_MND_DNODE_ID_NOT_CONFIGURED; + } + + if (strcmp(pCfg->dnodeEp, tsLocalEp) != 0) { + dDebug("dnodeEp:%s, in create meps msg is not equal with saved dnodeEp:%s", pCfg->dnodeEp, tsLocalEp); + return TSDB_CODE_MND_DNODE_EP_NOT_CONFIGURED; + } + + dDebug("dnode:%d, create meps msg is received from mnodes, numOfMnodes:%d", pCfg->dnodeId, pCfg->mnodes.mnodeNum); + for (int32_t i = 0; i < pCfg->mnodes.mnodeNum; ++i) { + pCfg->mnodes.mnodeInfos[i].mnodeId = htonl(pCfg->mnodes.mnodeInfos[i].mnodeId); + dDebug("meps index:%d, meps:%d:%s", i, pCfg->mnodes.mnodeInfos[i].mnodeId, pCfg->mnodes.mnodeInfos[i].mnodeEp); + } + + if (mnodeIsServing()) return 0; + + return mnodeDeploy(&pCfg->mnodes); +} + +void dnodeProcessCreateMnodeReq(SRpcMsg *pMsg) { + int32_t code = dnodeStartMnode(pMsg); + + SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0, .code = code}; + + rpcSendResponse(&rspMsg); + rpcFreeCont(pMsg->pCont); +} + +void dnodeProcessConfigDnodeReq(SRpcMsg *pMsg) { + SCfgDnodeMsg *pCfg = pMsg->pCont; + + int32_t code = taosCfgDynamicOptions(pCfg->config); + + SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0, .code = code}; + + rpcSendResponse(&rspMsg); + rpcFreeCont(pMsg->pCont); +} \ No newline at end of file diff --git a/source/server/dnode/src/dnodeMnodeEps.c b/source/server/dnode/src/dnodeMnodeEps.c new file mode 100644 index 0000000000000000000000000000000000000000..ab5c76b5806ecc3cb75d15a2cdb22755d5ee0e1a --- /dev/null +++ b/source/server/dnode/src/dnodeMnodeEps.c @@ -0,0 +1,316 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "os.h" +#include "cJSON.h" +#include "tglobal.h" +#include "dnodeCfg.h" +#include "dnodeEps.h" +#include "dnodeMnodeEps.h" +#include "mnode.h" + +static void dnodePrintMnodeEps(SDnMnEps *meps) { + SRpcEpSet *epset = &meps->mnodeEpSet; + dInfo("print mnode eps, num:%d inuse:%d", epset->numOfEps, epset->inUse); + for (int32_t i = 0; i < epset->numOfEps; i++) { + dInfo("ep index:%d, %s:%u", i, epset->fqdn[i], epset->port[i]); + } +} + +static void dnodeResetMnodeEps(SDnMnEps *meps, SMInfos *mInfos) { + if (mInfos == NULL || mInfos->mnodeNum == 0) { + meps->mnodeEpSet.numOfEps = 1; + taosGetFqdnPortFromEp(tsFirst, meps->mnodeEpSet.fqdn[0], &meps->mnodeEpSet.port[0]); + + if (strcmp(tsSecond, tsFirst) != 0) { + meps->mnodeEpSet.numOfEps = 2; + taosGetFqdnPortFromEp(tsSecond, meps->mnodeEpSet.fqdn[1], &meps->mnodeEpSet.port[1]); + } + dnodePrintMnodeEps(meps); + return; + } + + int32_t size = sizeof(SMInfos); + memcpy(&meps->mnodeInfos, mInfos, size); + + meps->mnodeEpSet.inUse = meps->mnodeInfos.inUse; + meps->mnodeEpSet.numOfEps = meps->mnodeInfos.mnodeNum; + for (int32_t i = 0; i < meps->mnodeInfos.mnodeNum; i++) { + taosGetFqdnPortFromEp(meps->mnodeInfos.mnodeInfos[i].mnodeEp, meps->mnodeEpSet.fqdn[i], &meps->mnodeEpSet.port[i]); + } + + dnodePrintMnodeEps(meps); +} + +static int32_t dnodeWriteMnodeEps(SDnMnEps *meps) { + FILE *fp = fopen(meps->file, "w"); + if (!fp) { + dError("failed to write %s since %s", meps->file, strerror(errno)); + return -1; + } + + int32_t len = 0; + int32_t maxLen = 2000; + char * content = calloc(1, maxLen + 1); + + len += snprintf(content + len, maxLen - len, "{\n"); + len += snprintf(content + len, maxLen - len, " \"inUse\": %d,\n", meps->mnodeInfos.inUse); + len += snprintf(content + len, maxLen - len, " \"nodeNum\": %d,\n", meps->mnodeInfos.mnodeNum); + len += snprintf(content + len, maxLen - len, " \"nodeInfos\": [{\n"); + for (int32_t i = 0; i < meps->mnodeInfos.mnodeNum; i++) { + len += snprintf(content + len, maxLen - len, " \"nodeId\": %d,\n", meps->mnodeInfos.mnodeInfos[i].mnodeId); + len += snprintf(content + len, maxLen - len, " \"nodeEp\": \"%s\"\n", meps->mnodeInfos.mnodeInfos[i].mnodeEp); + if (i < meps->mnodeInfos.mnodeNum - 1) { + len += snprintf(content + len, maxLen - len, " },{\n"); + } else { + len += snprintf(content + len, maxLen - len, " }]\n"); + } + } + len += snprintf(content + len, maxLen - len, "}\n"); + + fwrite(content, 1, len, fp); + taosFsyncFile(fileno(fp)); + fclose(fp); + free(content); + terrno = 0; + + dInfo("successed to write %s", meps->file); + return 0; +} + +static int32_t dnodeReadMnodeEps(SDnMnEps *meps, SDnEps *deps) { + int32_t len = 0; + int32_t maxLen = 2000; + char * content = calloc(1, maxLen + 1); + cJSON * root = NULL; + FILE * fp = NULL; + SMInfos mInfos = {0}; + bool nodeChanged = false; + + fp = fopen(meps->file, "r"); + if (!fp) { + dDebug("file %s not exist", meps->file); + goto PARSE_MINFOS_OVER; + } + + len = (int32_t)fread(content, 1, maxLen, fp); + if (len <= 0) { + dError("failed to read %s since content is null", meps->file); + goto PARSE_MINFOS_OVER; + } + + content[len] = 0; + root = cJSON_Parse(content); + if (root == NULL) { + dError("failed to read %s since invalid json format", meps->file); + goto PARSE_MINFOS_OVER; + } + + cJSON *inUse = cJSON_GetObjectItem(root, "inUse"); + if (!inUse || inUse->type != cJSON_Number) { + dError("failed to read mnodeEpSet.json since inUse not found"); + goto PARSE_MINFOS_OVER; + } + meps->mnodeInfos.inUse = (int8_t)inUse->valueint; + + cJSON *nodeNum = cJSON_GetObjectItem(root, "nodeNum"); + if (!nodeNum || nodeNum->type != cJSON_Number) { + dError("failed to read mnodeEpSet.json since nodeNum not found"); + goto PARSE_MINFOS_OVER; + } + mInfos.mnodeNum = (int8_t)nodeNum->valueint; + + cJSON *nodeInfos = cJSON_GetObjectItem(root, "nodeInfos"); + if (!nodeInfos || nodeInfos->type != cJSON_Array) { + dError("failed to read mnodeEpSet.json since nodeInfos not found"); + goto PARSE_MINFOS_OVER; + } + + int32_t size = cJSON_GetArraySize(nodeInfos); + if (size != mInfos.mnodeNum) { + dError("failed to read mnodeEpSet.json since nodeInfos size not matched"); + goto PARSE_MINFOS_OVER; + } + + for (int32_t i = 0; i < size; ++i) { + cJSON *nodeInfo = cJSON_GetArrayItem(nodeInfos, i); + if (nodeInfo == NULL) continue; + + cJSON *nodeId = cJSON_GetObjectItem(nodeInfo, "nodeId"); + if (!nodeId || nodeId->type != cJSON_Number) { + dError("failed to read mnodeEpSet.json since nodeId not found"); + goto PARSE_MINFOS_OVER; + } + + cJSON *nodeEp = cJSON_GetObjectItem(nodeInfo, "nodeEp"); + if (!nodeEp || nodeEp->type != cJSON_String || nodeEp->valuestring == NULL) { + dError("failed to read mnodeEpSet.json since nodeName not found"); + goto PARSE_MINFOS_OVER; + } + + SMInfo *mInfo = &mInfos.mnodeInfos[i]; + mInfo->mnodeId = (int32_t)nodeId->valueint; + tstrncpy(mInfo->mnodeEp, nodeEp->valuestring, TSDB_EP_LEN); + + bool changed = dnodeIsDnodeEpChanged(deps, mInfo->mnodeId, mInfo->mnodeEp); + if (changed) nodeChanged = changed; + } + + dInfo("successed to read file %s", meps->file); + +PARSE_MINFOS_OVER: + if (content != NULL) free(content); + if (root != NULL) cJSON_Delete(root); + if (fp != NULL) fclose(fp); + terrno = 0; + + for (int32_t i = 0; i < mInfos.mnodeNum; ++i) { + SMInfo *mInfo = &mInfos.mnodeInfos[i]; + dnodeGetDnodeEp(mInfo->mnodeId, mInfo->mnodeEp, NULL, NULL); + } + + dnodeResetMnodeEps(meps, &mInfos); + + if (nodeChanged) { + dnodeWriteMnodeEps(meps); + } + + return 0; +} + +void dnodeSendRedirectMsg(SRpcMsg *rpcMsg, bool forShell) { + SDnMnEps *meps = dnodeInst()->meps; + SRpcConnInfo connInfo = {0}; + rpcGetConnInfo(rpcMsg->handle, &connInfo); + + SRpcEpSet epSet = {0}; + if (forShell) { + dnodeGetEpSetForShell(meps, &epSet); + } else { + dnodeGetEpSetForPeer(meps, &epSet); + } + + dDebug("msg:%s will be redirected, dnodeIp:%s user:%s, numOfEps:%d inUse:%d", taosMsg[rpcMsg->msgType], + taosIpStr(connInfo.clientIp), connInfo.user, epSet.numOfEps, epSet.inUse); + + for (int32_t i = 0; i < epSet.numOfEps; ++i) { + dDebug("mnode index:%d %s:%d", i, epSet.fqdn[i], epSet.port[i]); + if (strcmp(epSet.fqdn[i], tsLocalFqdn) == 0) { + if ((epSet.port[i] == tsServerPort + TSDB_PORT_DNODEDNODE && !forShell) || + (epSet.port[i] == tsServerPort && forShell)) { + epSet.inUse = (i + 1) % epSet.numOfEps; + dDebug("mnode index:%d %s:%d set inUse to %d", i, epSet.fqdn[i], epSet.port[i], epSet.inUse); + } + } + + epSet.port[i] = htons(epSet.port[i]); + } + + rpcSendRedirectRsp(rpcMsg->handle, &epSet); +} + +int32_t dnodeInitMnodeEps(SDnMnEps **out) { + SDnMnEps *meps = calloc(1, sizeof(SDnMnEps)); + if (meps == NULL) return -1; + + snprintf(meps->file, sizeof(meps->file), "%s/mnodeEpSet.json", tsDnodeDir); + pthread_mutex_init(&meps->mutex, NULL); + *out = meps; + + dnodeResetMnodeEps(meps, NULL); + int32_t ret = dnodeReadMnodeEps(meps, dnodeInst()->eps); + if (ret == 0) { + dInfo("dnode mInfos is initialized"); + } + + return ret; +} + +void dnodeCleanupMnodeEps(SDnMnEps **out) { + SDnMnEps *meps = *out; + *out = NULL; + + if (meps != NULL) { + pthread_mutex_destroy(&meps->mutex); + free(meps); + } +} + +void dnodeUpdateMnodeFromStatus(SDnMnEps *meps, SMInfos *mInfos) { + if (mInfos->mnodeNum <= 0 || mInfos->mnodeNum > TSDB_MAX_REPLICA) { + dError("invalid mInfos since num:%d invalid", mInfos->mnodeNum); + return; + } + + for (int32_t i = 0; i < mInfos->mnodeNum; ++i) { + SMInfo *minfo = &mInfos->mnodeInfos[i]; + minfo->mnodeId = htonl(minfo->mnodeId); + if (minfo->mnodeId <= 0 || strlen(minfo->mnodeEp) <= 5) { + dError("invalid mInfo:%d since id:%d and ep:%s invalid", i, minfo->mnodeId, minfo->mnodeEp); + return; + } + } + + pthread_mutex_lock(&meps->mutex); + if (mInfos->mnodeNum != meps->mnodeInfos.mnodeNum) { + dnodeResetMnodeEps(meps, mInfos); + dnodeWriteMnodeEps(meps); + } else { + int32_t size = sizeof(SMInfos); + if (memcmp(mInfos, &meps->mnodeInfos, size) != 0) { + dnodeResetMnodeEps(meps, mInfos); + dnodeWriteMnodeEps(meps); + } + } + pthread_mutex_unlock(&meps->mutex); +} + +void dnodeUpdateMnodeFromPeer(SDnMnEps *meps, SRpcEpSet *ep) { + if (ep->numOfEps <= 0) { + dError("mInfos is changed, but content is invalid, discard it"); + return; + } + + pthread_mutex_lock(&meps->mutex); + + dInfo("mInfos is changed, numOfEps:%d inUse:%d", ep->numOfEps, ep->inUse); + for (int32_t i = 0; i < ep->numOfEps; ++i) { + ep->port[i] -= TSDB_PORT_DNODEDNODE; + dInfo("minfo:%d %s:%u", i, ep->fqdn[i], ep->port[i]); + } + meps->mnodeEpSet = *ep; + + pthread_mutex_unlock(&meps->mutex); +} + +void dnodeGetEpSetForPeer(SDnMnEps *meps, SRpcEpSet *epSet) { + pthread_mutex_lock(&meps->mutex); + + *epSet = meps->mnodeEpSet; + for (int32_t i = 0; i < epSet->numOfEps; ++i) { + epSet->port[i] += TSDB_PORT_DNODEDNODE; + } + + pthread_mutex_unlock(&meps->mutex); +} + +void dnodeGetEpSetForShell(SDnMnEps *meps, SRpcEpSet *epSet) { + pthread_mutex_lock(&meps->mutex); + + *epSet = meps->mnodeEpSet; + + pthread_mutex_unlock(&meps->mutex); +} diff --git a/source/server/dnode/src/dnodeStatus.c b/source/server/dnode/src/dnodeStatus.c new file mode 100644 index 0000000000000000000000000000000000000000..6abc886147ae4d98455e5dbe1305182703f9cf56 --- /dev/null +++ b/source/server/dnode/src/dnodeStatus.c @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "os.h" +#include "ttime.h" +#include "ttimer.h" +#include "tglobal.h" +#include "dnodeCfg.h" +#include "dnodeEps.h" +#include "dnodeMnodeEps.h" +#include "dnodeStatus.h" +#include "dnodeMain.h" +#include "vnode.h" + +static void dnodeSendStatusMsg(void *handle, void *tmrId) { + SDnStatus *status = handle; + if (status->dnodeTimer == NULL) { + dError("dnode timer is already released"); + return; + } + + if (status->statusTimer == NULL) { + taosTmrReset(dnodeSendStatusMsg, tsStatusInterval * 1000, status, status->dnodeTimer, &status->statusTimer); + dError("failed to start status timer"); + return; + } + + int32_t contLen = sizeof(SStatusMsg) + TSDB_MAX_VNODES * sizeof(SVnodeLoad); + SStatusMsg *pStatus = rpcMallocCont(contLen); + if (pStatus == NULL) { + taosTmrReset(dnodeSendStatusMsg, tsStatusInterval * 1000, status, status->dnodeTimer, &status->statusTimer); + dError("failed to malloc status message"); + return; + } + + SDnode *dnode = dnodeInst(); + dnodeGetCfg(dnode->cfg, &pStatus->dnodeId, pStatus->clusterId); + pStatus->dnodeId = htonl(dnodeGetDnodeId(dnode->cfg)); + pStatus->version = htonl(tsVersion); + pStatus->lastReboot = htonl(status->rebootTime); + pStatus->numOfCores = htons((uint16_t)tsNumOfCores); + pStatus->diskAvailable = tsAvailDataDirGB; + pStatus->alternativeRole = tsAlternativeRole; + tstrncpy(pStatus->dnodeEp, tsLocalEp, TSDB_EP_LEN); + + // fill cluster cfg parameters + pStatus->clusterCfg.numOfMnodes = htonl(tsNumOfMnodes); + pStatus->clusterCfg.mnodeEqualVnodeNum = htonl(tsMnodeEqualVnodeNum); + pStatus->clusterCfg.offlineThreshold = htonl(tsOfflineThreshold); + pStatus->clusterCfg.statusInterval = htonl(tsStatusInterval); + pStatus->clusterCfg.maxtablesPerVnode = htonl(tsMaxTablePerVnode); + pStatus->clusterCfg.maxVgroupsPerDb = htonl(tsMaxVgroupsPerDb); + tstrncpy(pStatus->clusterCfg.arbitrator, tsArbitrator, TSDB_EP_LEN); + tstrncpy(pStatus->clusterCfg.timezone, tsTimezone, 64); + pStatus->clusterCfg.checkTime = 0; + char timestr[32] = "1970-01-01 00:00:00.00"; + (void)taosParseTime(timestr, &pStatus->clusterCfg.checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, 0); + tstrncpy(pStatus->clusterCfg.locale, tsLocale, TSDB_LOCALE_LEN); + tstrncpy(pStatus->clusterCfg.charset, tsCharset, TSDB_LOCALE_LEN); + + pStatus->clusterCfg.enableBalance = tsEnableBalance; + pStatus->clusterCfg.flowCtrl = tsEnableFlowCtrl; + pStatus->clusterCfg.slaveQuery = tsEnableSlaveQuery; + pStatus->clusterCfg.adjustMaster = tsEnableAdjustMaster; + + vnodeGetStatus(pStatus); + contLen = sizeof(SStatusMsg) + pStatus->openVnodes * sizeof(SVnodeLoad); + pStatus->openVnodes = htons(pStatus->openVnodes); + + SRpcMsg rpcMsg = {.ahandle = status, .pCont = pStatus, .contLen = contLen, .msgType = TSDB_MSG_TYPE_DM_STATUS}; + + dnodeSendMsgToMnode(&rpcMsg); +} + +void dnodeProcessStatusRsp(SRpcMsg *pMsg) { + SDnode *dnode = dnodeInst(); + SDnStatus *status = pMsg->ahandle; + + if (pMsg->code != TSDB_CODE_SUCCESS) { + dError("status rsp is received, error:%s", tstrerror(pMsg->code)); + if (pMsg->code == TSDB_CODE_MND_DNODE_NOT_EXIST) { + char clusterId[TSDB_CLUSTER_ID_LEN]; + dnodeGetClusterId(dnode->cfg, clusterId); + if (clusterId[0] != '\0') { + dnodeSetDropped(dnode->cfg); + dError("exit zombie dropped dnode"); + exit(EXIT_FAILURE); + } + } + + taosTmrReset(dnodeSendStatusMsg, tsStatusInterval * 1000, status, status->dnodeTimer, &status->statusTimer); + return; + } + + SStatusRsp *pStatusRsp = pMsg->pCont; + SMInfos * minfos = &pStatusRsp->mnodes; + dnodeUpdateMnodeFromStatus(dnode->meps, minfos); + + SDnodeCfg *pCfg = &pStatusRsp->dnodeCfg; + pCfg->numOfVnodes = htonl(pCfg->numOfVnodes); + pCfg->moduleStatus = htonl(pCfg->moduleStatus); + pCfg->dnodeId = htonl(pCfg->dnodeId); + dnodeUpdateCfg(dnode->cfg, pCfg); + + vnodeSetAccess(pStatusRsp->vgAccess, pCfg->numOfVnodes); + + SDnodeEps *pEps = (SDnodeEps *)((char *)pStatusRsp->vgAccess + pCfg->numOfVnodes * sizeof(SVgroupAccess)); + dnodeUpdateEps(dnode->eps, pEps); + + taosTmrReset(dnodeSendStatusMsg, tsStatusInterval * 1000, status, status->dnodeTimer, &status->statusTimer); +} + +int32_t dnodeInitStatus(SDnStatus **out) { + SDnStatus *status = calloc(1, sizeof(SDnStatus)); + if (status == NULL) return -1; + status->statusTimer = NULL; + status->dnodeTimer = dnodeInst()->main->dnodeTimer; + status->rebootTime = taosGetTimestampSec(); + taosTmrReset(dnodeSendStatusMsg, 500, status, status->dnodeTimer, &status->statusTimer); + *out = status; + dInfo("dnode status timer is initialized"); + return TSDB_CODE_SUCCESS; +} + +void dnodeCleanupStatus(SDnStatus **out) { + SDnStatus *status = *out; + *out = NULL; + + if (status->statusTimer != NULL) { + taosTmrStopA(&status->statusTimer); + status->statusTimer = NULL; + } + + free(status); +} diff --git a/source/server/dnode/src/dnodeTelem.c b/source/server/dnode/src/dnodeTelem.c new file mode 100644 index 0000000000000000000000000000000000000000..b221746c833b4f79401275745e1ae2c2b867a361 --- /dev/null +++ b/source/server/dnode/src/dnodeTelem.c @@ -0,0 +1,321 @@ +/* + * Copyright (c) 2020 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "os.h" +#include "tbuffer.h" +#include "tglobal.h" +#include "dnodeCfg.h" +#include "dnodeTelem.h" +#include "mnode.h" + +#define TELEMETRY_SERVER "telemetry.taosdata.com" +#define TELEMETRY_PORT 80 +#define REPORT_INTERVAL 86400 + +static void dnodeBeginObject(SBufferWriter* bw) { tbufWriteChar(bw, '{'); } + +static void dnodeCloseObject(SBufferWriter* bw) { + size_t len = tbufTell(bw); + if (tbufGetData(bw, false)[len - 1] == ',') { + tbufWriteCharAt(bw, len - 1, '}'); + } else { + tbufWriteChar(bw, '}'); + } + tbufWriteChar(bw, ','); +} + +#if 0 +static void beginArray(SBufferWriter* bw) { + tbufWriteChar(bw, '['); +} + +static void closeArray(SBufferWriter* bw) { + size_t len = tbufTell(bw); + if (tbufGetData(bw, false)[len - 1] == ',') { + tbufWriteCharAt(bw, len - 1, ']'); + } else { + tbufWriteChar(bw, ']'); + } + tbufWriteChar(bw, ','); +} +#endif + +static void dnodeWriteString(SBufferWriter* bw, const char* str) { + tbufWriteChar(bw, '"'); + tbufWrite(bw, str, strlen(str)); + tbufWriteChar(bw, '"'); +} + +static void dnodeAddIntField(SBufferWriter* bw, const char* k, int64_t v) { + dnodeWriteString(bw, k); + tbufWriteChar(bw, ':'); + char buf[32]; + sprintf(buf, "%" PRId64, v); + tbufWrite(bw, buf, strlen(buf)); + tbufWriteChar(bw, ','); +} + +static void dnodeAddStringField(SBufferWriter* bw, const char* k, const char* v) { + dnodeWriteString(bw, k); + tbufWriteChar(bw, ':'); + dnodeWriteString(bw, v); + tbufWriteChar(bw, ','); +} + +static void dnodeAddCpuInfo(SBufferWriter* bw) { + char* line = NULL; + size_t size = 0; + int32_t done = 0; + + FILE* fp = fopen("/proc/cpuinfo", "r"); + if (fp == NULL) { + return; + } + + while (done != 3 && (size = tgetline(&line, &size, fp)) != -1) { + line[size - 1] = '\0'; + if (((done & 1) == 0) && strncmp(line, "model name", 10) == 0) { + const char* v = strchr(line, ':') + 2; + dnodeAddStringField(bw, "cpuModel", v); + done |= 1; + } else if (((done & 2) == 0) && strncmp(line, "cpu cores", 9) == 0) { + const char* v = strchr(line, ':') + 2; + dnodeWriteString(bw, "numOfCpu"); + tbufWriteChar(bw, ':'); + tbufWrite(bw, v, strlen(v)); + tbufWriteChar(bw, ','); + done |= 2; + } + } + + free(line); + fclose(fp); +} + +static void dnodeAddOsInfo(SBufferWriter* bw) { + char* line = NULL; + size_t size = 0; + + FILE* fp = fopen("/etc/os-release", "r"); + if (fp == NULL) { + return; + } + + while ((size = tgetline(&line, &size, fp)) != -1) { + line[size - 1] = '\0'; + if (strncmp(line, "PRETTY_NAME", 11) == 0) { + const char* p = strchr(line, '=') + 1; + if (*p == '"') { + p++; + line[size - 2] = 0; + } + dnodeAddStringField(bw, "os", p); + break; + } + } + + free(line); + fclose(fp); +} + +static void dnodeAddMemoryInfo(SBufferWriter* bw) { + char* line = NULL; + size_t size = 0; + + FILE* fp = fopen("/proc/meminfo", "r"); + if (fp == NULL) { + return; + } + + while ((size = tgetline(&line, &size, fp)) != -1) { + line[size - 1] = '\0'; + if (strncmp(line, "MemTotal", 8) == 0) { + const char* p = strchr(line, ':') + 1; + while (*p == ' ') p++; + dnodeAddStringField(bw, "memory", p); + break; + } + } + + free(line); + fclose(fp); +} + +static void dnodeAddVersionInfo(SDnTelem* telem, SBufferWriter* bw) { + dnodeAddStringField(bw, "version", version); + dnodeAddStringField(bw, "buildInfo", buildinfo); + dnodeAddStringField(bw, "gitInfo", gitinfo); + dnodeAddStringField(bw, "email", telem->email); +} + +static void dnodeAddRuntimeInfo(SDnTelem* telem, SBufferWriter* bw) { + SMnodeStat stat = {0}; + if (mnodeGetStatistics(&stat) != 0) { + return; + } + + dnodeAddIntField(bw, "numOfDnode", stat.numOfDnode); + dnodeAddIntField(bw, "numOfMnode", stat.numOfMnode); + dnodeAddIntField(bw, "numOfVgroup", stat.numOfVgroup); + dnodeAddIntField(bw, "numOfDatabase", stat.numOfDatabase); + dnodeAddIntField(bw, "numOfSuperTable", stat.numOfSuperTable); + dnodeAddIntField(bw, "numOfChildTable", stat.numOfChildTable); + dnodeAddIntField(bw, "numOfColumn", stat.numOfColumn); + dnodeAddIntField(bw, "numOfPoint", stat.totalPoints); + dnodeAddIntField(bw, "totalStorage", stat.totalStorage); + dnodeAddIntField(bw, "compStorage", stat.compStorage); +} + +static void dnodeSendTelemetryReport(SDnTelem* telem) { + char buf[128] = {0}; + uint32_t ip = taosGetIpv4FromFqdn(TELEMETRY_SERVER); + if (ip == 0xffffffff) { + dTrace("failed to get IP address of " TELEMETRY_SERVER ", reason:%s", strerror(errno)); + return; + } + SOCKET fd = taosOpenTcpClientSocket(ip, TELEMETRY_PORT, 0); + if (fd < 0) { + dTrace("failed to create socket for telemetry, reason:%s", strerror(errno)); + return; + } + + SDnode *dnode = dnodeInst(); + SBufferWriter bw = tbufInitWriter(NULL, false); + dnodeBeginObject(&bw); + dnodeAddStringField(&bw, "instanceId", dnode->cfg->clusterId); + dnodeAddIntField(&bw, "reportVersion", 1); + dnodeAddOsInfo(&bw); + dnodeAddCpuInfo(&bw); + dnodeAddMemoryInfo(&bw); + dnodeAddVersionInfo(telem, &bw); + dnodeAddRuntimeInfo(telem, &bw); + dnodeCloseObject(&bw); + + const char* header = + "POST /report HTTP/1.1\n" + "Host: " TELEMETRY_SERVER + "\n" + "Content-Type: application/json\n" + "Content-Length: "; + + taosWriteSocket(fd, (void*)header, (int32_t)strlen(header)); + int32_t contLen = (int32_t)(tbufTell(&bw) - 1); + sprintf(buf, "%d\n\n", contLen); + taosWriteSocket(fd, buf, (int32_t)strlen(buf)); + taosWriteSocket(fd, tbufGetData(&bw, false), contLen); + tbufCloseWriter(&bw); + + // read something to avoid nginx error 499 + if (taosReadSocket(fd, buf, 10) < 0) { + dTrace("failed to receive response since %s", strerror(errno)); + } + + taosCloseSocket(fd); +} + +static void* dnodeTelemThreadFp(void* param) { + SDnTelem* telem = param; + + struct timespec end = {0}; + clock_gettime(CLOCK_REALTIME, &end); + end.tv_sec += 300; // wait 5 minutes before send first report + + setThreadName("dnode-telem"); + + while (!telem->exit) { + int32_t r = 0; + struct timespec ts = end; + pthread_mutex_lock(&telem->lock); + r = pthread_cond_timedwait(&telem->cond, &telem->lock, &ts); + pthread_mutex_unlock(&telem->lock); + if (r == 0) break; + if (r != ETIMEDOUT) continue; + + if (mnodeIsServing()) { + dnodeSendTelemetryReport(telem); + } + end.tv_sec += REPORT_INTERVAL; + } + + return NULL; +} + +static void dnodeGetEmail(SDnTelem* telem, char* filepath) { + int32_t fd = taosOpenFileRead(filepath); + if (fd < 0) { + return; + } + + if (taosReadFile(fd, (void*)telem->email, TSDB_FQDN_LEN) < 0) { + dError("failed to read %d bytes from file %s since %s", TSDB_FQDN_LEN, filepath, strerror(errno)); + } + + taosCloseFile(fd); +} + +int32_t dnodeInitTelem(SDnTelem** out) { + SDnTelem* telem = calloc(1, sizeof(SDnTelem)); + if (telem == NULL) return -1; + + telem->enable = tsEnableTelemetryReporting; + *out = telem; + + if (!telem->enable) return 0; + + telem->exit = 0; + pthread_mutex_init(&telem->lock, NULL); + pthread_cond_init(&telem->cond, NULL); + telem->email[0] = 0; + + dnodeGetEmail(telem, "/usr/local/taos/email"); + + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + + int32_t code = pthread_create(&telem->thread, &attr, dnodeTelemThreadFp, telem); + pthread_attr_destroy(&attr); + if (code != 0) { + dTrace("failed to create telemetry thread since :%s", strerror(code)); + } + + dInfo("dnode telemetry is initialized"); + return 0; +} + +void dnodeCleanupTelem(SDnTelem** out) { + SDnTelem* telem = *out; + *out = NULL; + + if (!telem->enable) { + free(telem); + return; + } + + if (taosCheckPthreadValid(telem->thread)) { + pthread_mutex_lock(&telem->lock); + telem->exit = 1; + pthread_cond_signal(&telem->cond); + pthread_mutex_unlock(&telem->lock); + + pthread_join(telem->thread, NULL); + } + + pthread_mutex_destroy(&telem->lock); + pthread_cond_destroy(&telem->cond); + + free(telem); +} diff --git a/source/server/dnode/src/dnodeTrans.c b/source/server/dnode/src/dnodeTrans.c new file mode 100644 index 0000000000000000000000000000000000000000..7a870f22c85ed59c10401e98d57e9324d81f0193 --- /dev/null +++ b/source/server/dnode/src/dnodeTrans.c @@ -0,0 +1,416 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +/* this file is mainly responsible for the communication between DNODEs. Each + * dnode works as both server and client. SDnode may send status, grant, config + * messages to mnode, mnode may send create/alter/drop table/vnode messages + * to dnode. All theses messages are handled from here + */ + +#define _DEFAULT_SOURCE +#include "os.h" +#include "tglobal.h" +#include "dnodeMain.h" +#include "dnodeMnodeEps.h" +#include "dnodeStatus.h" +#include "dnodeTrans.h" +#include "vnode.h" +#include "mnode.h" + +static void dnodeProcessPeerReq(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { + SDnode * dnode = dnodeInst(); + SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0}; + + if (pMsg->pCont == NULL) return; + if (pMsg->msgType == TSDB_MSG_TYPE_NETWORK_TEST) { + dnodeProcessStartupReq(pMsg); + return; + } + + if (dnode->main->runStatus != TD_RUN_STAT_RUNNING) { + rspMsg.code = TSDB_CODE_APP_NOT_READY; + rpcSendResponse(&rspMsg); + rpcFreeCont(pMsg->pCont); + dTrace("RPC %p, msg:%s is ignored since dnode not running", pMsg->handle, taosMsg[pMsg->msgType]); + return; + } + + if (pMsg->pCont == NULL) { + rspMsg.code = TSDB_CODE_DND_INVALID_MSG_LEN; + rpcSendResponse(&rspMsg); + return; + } + + RpcMsgFp fp = dnode->trans->peerMsgFp[pMsg->msgType]; + if (fp != NULL) { + (*fp)(pMsg); + } else { + dDebug("RPC %p, peer req:%s not processed", pMsg->handle, taosMsg[pMsg->msgType]); + rspMsg.code = TSDB_CODE_DND_MSG_NOT_PROCESSED; + rpcSendResponse(&rspMsg); + rpcFreeCont(pMsg->pCont); + } +} + +int32_t dnodeInitServer(SDnTrans *trans) { + trans->peerMsgFp[TSDB_MSG_TYPE_MD_CREATE_TABLE] = vnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_MD_DROP_TABLE] = vnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_MD_ALTER_TABLE] = vnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_MD_DROP_STABLE] = vnodeProcessMsg; + + trans->peerMsgFp[TSDB_MSG_TYPE_MD_CREATE_VNODE] = vnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_MD_ALTER_VNODE] = vnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_MD_SYNC_VNODE] = vnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_MD_COMPACT_VNODE] = vnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_MD_DROP_VNODE] = vnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_MD_ALTER_STREAM] = vnodeProcessMsg; + + trans->peerMsgFp[TSDB_MSG_TYPE_MD_CONFIG_DNODE] = dnodeProcessConfigDnodeReq; + trans->peerMsgFp[TSDB_MSG_TYPE_MD_CREATE_MNODE] = dnodeProcessCreateMnodeReq; + + trans->peerMsgFp[TSDB_MSG_TYPE_DM_CONFIG_TABLE] = mnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_DM_CONFIG_VNODE] = mnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_DM_AUTH] = mnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_DM_GRANT] = mnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_DM_STATUS] = mnodeProcessMsg; + + SRpcInit rpcInit; + memset(&rpcInit, 0, sizeof(rpcInit)); + rpcInit.localPort = tsDnodeDnodePort; + rpcInit.label = "DND-S"; + rpcInit.numOfThreads = 1; + rpcInit.cfp = dnodeProcessPeerReq; + rpcInit.sessions = TSDB_MAX_VNODES << 4; + rpcInit.connType = TAOS_CONN_SERVER; + rpcInit.idleTime = tsShellActivityTimer * 1000; + + trans->serverRpc = rpcOpen(&rpcInit); + if (trans->serverRpc == NULL) { + dError("failed to init peer rpc server"); + return -1; + } + + dInfo("dnode peer rpc server is initialized"); + return 0; +} + +void dnodeCleanupServer(SDnTrans *trans) { + if (trans->serverRpc) { + rpcClose(trans->serverRpc); + trans->serverRpc = NULL; + dInfo("dnode peer server is closed"); + } +} + +static void dnodeProcessRspFromPeer(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { + SDnode *dnode = dnodeInst(); + if (dnode->main->runStatus == TD_RUN_STAT_STOPPED) { + if (pMsg == NULL || pMsg->pCont == NULL) return; + dTrace("msg:%p is ignored since dnode is stopping", pMsg); + rpcFreeCont(pMsg->pCont); + return; + } + + if (pMsg->msgType == TSDB_MSG_TYPE_DM_STATUS_RSP && pEpSet) { + dnodeUpdateMnodeFromPeer(dnode->meps, pEpSet); + } + + RpcMsgFp fp = dnode->trans->peerMsgFp[pMsg->msgType]; + if (fp != NULL) { + (*fp)(pMsg); + } else { + dDebug("RPC %p, peer rsp:%s not processed", pMsg->handle, taosMsg[pMsg->msgType]); + SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0}; + rspMsg.code = TSDB_CODE_DND_MSG_NOT_PROCESSED; + rpcSendResponse(&rspMsg); + rpcFreeCont(pMsg->pCont); + } + + rpcFreeCont(pMsg->pCont); +} + +int32_t dnodeInitClient(SDnTrans *trans) { + trans->peerMsgFp[TSDB_MSG_TYPE_MD_CREATE_TABLE_RSP] = mnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_MD_DROP_TABLE_RSP] = mnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_MD_ALTER_TABLE_RSP] = mnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_MD_DROP_STABLE_RSP] = mnodeProcessMsg; + + trans->peerMsgFp[TSDB_MSG_TYPE_MD_CREATE_VNODE_RSP] = mnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_MD_ALTER_VNODE_RSP] = mnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_MD_SYNC_VNODE_RSP] = mnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_MD_COMPACT_VNODE_RSP] = mnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_MD_DROP_VNODE_RSP] = mnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_MD_ALTER_STREAM_RSP] = mnodeProcessMsg; + + trans->peerMsgFp[TSDB_MSG_TYPE_MD_CONFIG_DNODE_RSP] = mnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_MD_CREATE_MNODE_RSP] = mnodeProcessMsg; + + trans->peerMsgFp[TSDB_MSG_TYPE_DM_CONFIG_TABLE_RSP] = mnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_DM_CONFIG_VNODE_RSP] = mnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_DM_AUTH_RSP] = mnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_DM_GRANT_RSP] = mnodeProcessMsg; + trans->peerMsgFp[TSDB_MSG_TYPE_DM_STATUS_RSP] = dnodeProcessStatusRsp; + + char secret[TSDB_KEY_LEN] = "secret"; + SRpcInit rpcInit; + memset(&rpcInit, 0, sizeof(rpcInit)); + rpcInit.label = "DND-C"; + rpcInit.numOfThreads = 1; + rpcInit.cfp = dnodeProcessRspFromPeer; + rpcInit.sessions = TSDB_MAX_VNODES << 4; + rpcInit.connType = TAOS_CONN_CLIENT; + rpcInit.idleTime = tsShellActivityTimer * 1000; + rpcInit.user = "t"; + rpcInit.ckey = "key"; + rpcInit.secret = secret; + + trans->clientRpc = rpcOpen(&rpcInit); + if (trans->clientRpc == NULL) { + dError("failed to init peer rpc client"); + return -1; + } + + dInfo("dnode peer rpc client is initialized"); + return 0; +} + +void dnodeCleanupClient(SDnTrans *trans) { + if (trans->clientRpc) { + rpcClose(trans->clientRpc); + trans->clientRpc = NULL; + dInfo("dnode peer rpc client is closed"); + } +} + +static void dnodeProcessMsgFromShell(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { + SDnode * dnode = dnodeInst(); + SRpcMsg rpcMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0}; + + if (pMsg->pCont == NULL) return; + if (dnode->main->runStatus == TD_RUN_STAT_STOPPED) { + dError("RPC %p, shell msg:%s is ignored since dnode exiting", pMsg->handle, taosMsg[pMsg->msgType]); + rpcMsg.code = TSDB_CODE_DND_EXITING; + rpcSendResponse(&rpcMsg); + rpcFreeCont(pMsg->pCont); + return; + } else if (dnode->main->runStatus != TD_RUN_STAT_RUNNING) { + dError("RPC %p, shell msg:%s is ignored since dnode not running", pMsg->handle, taosMsg[pMsg->msgType]); + rpcMsg.code = TSDB_CODE_APP_NOT_READY; + rpcSendResponse(&rpcMsg); + rpcFreeCont(pMsg->pCont); + return; + } + + SDnTrans *trans = dnode->trans; + if (pMsg->msgType == TSDB_MSG_TYPE_QUERY) { + atomic_fetch_add_32(&trans->queryReqNum, 1); + } else if (pMsg->msgType == TSDB_MSG_TYPE_SUBMIT) { + atomic_fetch_add_32(&trans->submitReqNum, 1); + } else {} + + RpcMsgFp fp = trans->shellMsgFp[pMsg->msgType]; + if (fp != NULL) { + (*fp)(pMsg); + } else { + dError("RPC %p, shell req:%s is not processed", pMsg->handle, taosMsg[pMsg->msgType]); + rpcMsg.code = TSDB_CODE_DND_MSG_NOT_PROCESSED; + rpcSendResponse(&rpcMsg); + rpcFreeCont(pMsg->pCont); + } +} + +static int32_t dnodeAuthNetTest(char *user, char *spi, char *encrypt, char *secret, char *ckey) { + if (strcmp(user, "nettestinternal") == 0) { + char pass[32] = {0}; + taosEncryptPass((uint8_t *)user, strlen(user), pass); + *spi = 0; + *encrypt = 0; + *ckey = 0; + memcpy(secret, pass, TSDB_KEY_LEN); + dTrace("nettest user is authorized"); + return 0; + } + + return -1; +} + +void dnodeSendMsgToDnode(SRpcEpSet *epSet, SRpcMsg *rpcMsg) { + SDnode *dnode = dnodeInst(); + rpcSendRequest(dnode->trans->clientRpc, epSet, rpcMsg, NULL); +} + +void dnodeSendMsgToMnode(SRpcMsg *rpcMsg) { + SDnode * dnode = dnodeInst(); + SRpcEpSet epSet = {0}; + dnodeGetEpSetForPeer(dnode->meps, &epSet); + dnodeSendMsgToDnode(&epSet, rpcMsg); +} + +void dnodeSendMsgToMnodeRecv(SRpcMsg *rpcMsg, SRpcMsg *rpcRsp) { + SDnode * dnode = dnodeInst(); + SRpcEpSet epSet = {0}; + dnodeGetEpSetForPeer(dnode->meps, &epSet); + rpcSendRecv(dnode->trans->clientRpc, &epSet, rpcMsg, rpcRsp); +} + +void dnodeSendMsgToDnodeRecv(SRpcMsg *rpcMsg, SRpcMsg *rpcRsp, SRpcEpSet *epSet) { + SDnode *dnode = dnodeInst(); + rpcSendRecv(dnode->trans->clientRpc, epSet, rpcMsg, rpcRsp); +} + +static int32_t dnodeRetrieveUserAuthInfo(char *user, char *spi, char *encrypt, char *secret, char *ckey) { + if (dnodeAuthNetTest(user, spi, encrypt, secret, ckey) == 0) return 0; + + int32_t code = mnodeRetriveAuth(user, spi, encrypt, secret, ckey); + if (code != TSDB_CODE_APP_NOT_READY) return code; + + SAuthMsg *pMsg = rpcMallocCont(sizeof(SAuthMsg)); + tstrncpy(pMsg->user, user, sizeof(pMsg->user)); + + SRpcMsg rpcMsg = {0}; + rpcMsg.pCont = pMsg; + rpcMsg.contLen = sizeof(SAuthMsg); + rpcMsg.msgType = TSDB_MSG_TYPE_DM_AUTH; + + dDebug("user:%s, send auth msg to mnodes", user); + SRpcMsg rpcRsp = {0}; + dnodeSendMsgToMnodeRecv(&rpcMsg, &rpcRsp); + + if (rpcRsp.code != 0) { + dError("user:%s, auth msg received from mnodes, error:%s", user, tstrerror(rpcRsp.code)); + } else { + SAuthRsp *pRsp = rpcRsp.pCont; + dDebug("user:%s, auth msg received from mnodes", user); + memcpy(secret, pRsp->secret, TSDB_KEY_LEN); + memcpy(ckey, pRsp->ckey, TSDB_KEY_LEN); + *spi = pRsp->spi; + *encrypt = pRsp->encrypt; + } + + rpcFreeCont(rpcRsp.pCont); + return rpcRsp.code; +} + +int32_t dnodeInitShell(SDnTrans *trans) { + trans->shellMsgFp[TSDB_MSG_TYPE_SUBMIT] = vnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_QUERY] = vnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_FETCH] = vnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_UPDATE_TAG_VAL] = vnodeProcessMsg; + + // the following message shall be treated as mnode write + trans->shellMsgFp[TSDB_MSG_TYPE_CM_CREATE_ACCT] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_ALTER_ACCT] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_DROP_ACCT] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_CREATE_USER] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_ALTER_USER] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_DROP_USER] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_CREATE_DNODE] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_DROP_DNODE] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_CREATE_DB] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_CREATE_TP] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_CREATE_FUNCTION] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_DROP_DB] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_SYNC_DB] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_DROP_TP] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_DROP_FUNCTION] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_ALTER_DB] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_ALTER_TP] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_CREATE_TABLE] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_DROP_TABLE] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_ALTER_TABLE] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_ALTER_STREAM] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_KILL_QUERY] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_KILL_STREAM] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_KILL_CONN] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_CONFIG_DNODE] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_COMPACT_VNODE] = mnodeProcessMsg; + + // the following message shall be treated as mnode query + trans->shellMsgFp[TSDB_MSG_TYPE_CM_HEARTBEAT] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_CONNECT] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_USE_DB] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_TABLE_META] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_STABLE_VGROUP] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_TABLES_META] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_SHOW] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_RETRIEVE] = mnodeProcessMsg; + trans->shellMsgFp[TSDB_MSG_TYPE_CM_RETRIEVE_FUNC] = mnodeProcessMsg; + + trans->shellMsgFp[TSDB_MSG_TYPE_NETWORK_TEST] = dnodeProcessStartupReq; + + int32_t numOfThreads = (int32_t)((tsNumOfCores * tsNumOfThreadsPerCore) / 2.0); + if (numOfThreads < 1) { + numOfThreads = 1; + } + + SRpcInit rpcInit; + memset(&rpcInit, 0, sizeof(rpcInit)); + rpcInit.localPort = tsDnodeShellPort; + rpcInit.label = "SHELL"; + rpcInit.numOfThreads = numOfThreads; + rpcInit.cfp = dnodeProcessMsgFromShell; + rpcInit.sessions = tsMaxShellConns; + rpcInit.connType = TAOS_CONN_SERVER; + rpcInit.idleTime = tsShellActivityTimer * 1000; + rpcInit.afp = dnodeRetrieveUserAuthInfo; + + trans->shellRpc = rpcOpen(&rpcInit); + if (trans->shellRpc == NULL) { + dError("failed to init shell rpc server"); + return -1; + } + + dInfo("dnode shell rpc server is initialized"); + return 0; +} + +void dnodeCleanupShell(SDnTrans *trans) { + if (trans->shellRpc) { + rpcClose(trans->shellRpc); + trans->shellRpc = NULL; + } +} + +int32_t dnodeInitTrans(SDnTrans **out) { + SDnTrans *trans = calloc(1, sizeof(SDnTrans)); + if (trans == NULL) return -1; + + *out = trans; + + if (dnodeInitClient(trans) != 0) { + return -1; + } + + if (dnodeInitServer(trans) != 0) { + return -1; + } + + if (dnodeInitShell(trans) != 0) { + return -1; + } + + return 0; +} + +void dnodeCleanupTrans(SDnTrans **out) { + SDnTrans* trans = *out; + *out = NULL; + + dnodeCleanupShell(trans); + dnodeCleanupServer(trans); + dnodeCleanupClient(trans); + + free(trans); +} diff --git a/source/server/mnode/CMakeLists.txt b/source/server/mnode/CMakeLists.txt index 689d6f7f2a9a614d279a5971c1ecbd176d872445..331a717bf581616c8e2a4a6c9b610d69c7c6f12d 100644 --- a/source/server/mnode/CMakeLists.txt +++ b/source/server/mnode/CMakeLists.txt @@ -4,4 +4,8 @@ target_include_directories( mnode PUBLIC "${CMAKE_SOURCE_DIR}/include/server/mnode" private "${CMAKE_CURRENT_SOURCE_DIR}/inc" +) +target_link_libraries( + mnode + PUBLIC transport ) \ No newline at end of file diff --git a/source/server/mnode/inc/mnodeInt.h b/source/server/mnode/inc/mnodeInt.h index 60b748f0ea3f4f68fdce1cd51a234534d275d987..646331b076b5a9f3dc9b3d898ebc72d1f6289c47 100644 --- a/source/server/mnode/inc/mnodeInt.h +++ b/source/server/mnode/inc/mnodeInt.h @@ -20,6 +20,11 @@ extern "C" { #endif +#include "os.h" +#include "taosmsg.h" +#include "trpc.h" +#include "mnode.h" + #ifdef __cplusplus } #endif diff --git a/source/server/mnode/src/mnode.c b/source/server/mnode/src/mnode.c deleted file mode 100644 index 6dea4a4e57392be988126c579648f39a8270b9bf..0000000000000000000000000000000000000000 --- a/source/server/mnode/src/mnode.c +++ /dev/null @@ -1,14 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ \ No newline at end of file diff --git a/source/server/mnode/src/mnodeMain.c b/source/server/mnode/src/mnodeMain.c new file mode 100644 index 0000000000000000000000000000000000000000..8e5b4692e3a4f46c8e6eb62f9fa72a28f3b7b3f2 --- /dev/null +++ b/source/server/mnode/src/mnodeMain.c @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#include "mnodeInt.h" + +int32_t mnodeInit(SMnodePara para) { return 0; } + +void mnodeCleanup() {} + +int32_t mnodeDeploy(struct SMInfos *minfos) { return 0; } + +void mnodeUnDeploy() {} + +bool mnodeIsServing() { return false; } + +int32_t mnodeGetStatistics(SMnodeStat *stat) { return 0; } + +int32_t mnodeRetriveAuth(char *user, char *spi, char *encrypt, char *secret, char *ckey) { return 0; } + +void mnodeProcessMsg(SRpcMsg *rpcMsg) {} diff --git a/source/server/server.c b/source/server/server.c new file mode 100644 index 0000000000000000000000000000000000000000..67e8e7bc5851de5c2f653065fc7a7825a6578df7 --- /dev/null +++ b/source/server/server.c @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ +#include "os.h" +#include "ulog.h" +#include "dnode.h" + +static bool stop = false; +static void sigintHandler(int32_t signum, void *info, void *ctx) { stop = true; } +static void setSignalHandler() { + taosSetSignal(SIGTERM, sigintHandler); + taosSetSignal(SIGHUP, sigintHandler); + taosSetSignal(SIGINT, sigintHandler); + taosSetSignal(SIGABRT, sigintHandler); + taosSetSignal(SIGBREAK, sigintHandler); +} + +int main(int argc, char const *argv[]) { + setSignalHandler(); + + int32_t code = dnodeInit(); + if (code != 0) { + uInfo("Failed to start TDengine, please check the log at:%s", tsLogDir); + exit(EXIT_FAILURE); + } + + uInfo("Started TDengine service successfully."); + + while (!stop) { + taosMsleep(100); + } + + uInfo("TDengine is shut down!"); + dnodeCleanup(); + + return 0; +} diff --git a/source/server/vnode/CMakeLists.txt b/source/server/vnode/CMakeLists.txt index 73605adc3fd299190db3a95d90fcebe4dc6fc65b..03421b26282a0123dd80b5773717f8233cffbff8 100644 --- a/source/server/vnode/CMakeLists.txt +++ b/source/server/vnode/CMakeLists.txt @@ -11,10 +11,8 @@ target_include_directories( ) target_link_libraries( vnode + PUBLIC transport PUBLIC meta PUBLIC tq PUBLIC tsdb - PUBLIC os - PUBLIC common - PUBLIC util ) \ No newline at end of file diff --git a/source/server/vnode/inc/vnodeInt.h b/source/server/vnode/inc/vnodeInt.h index 3d218df55e781c1a9e2620474c8504a85d4b9237..545d376f498c7e23efa615b3762a81ab2c36bea0 100644 --- a/source/server/vnode/inc/vnodeInt.h +++ b/source/server/vnode/inc/vnodeInt.h @@ -16,10 +16,15 @@ #ifndef _TD_VNODE_INT_H_ #define _TD_VNODE_INT_H_ + +#include "os.h" #include "amalloc.h" +#include "meta.h" +#include "taosmsg.h" #include "tq.h" +#include "trpc.h" #include "tsdb.h" -#include "meta.h" +#include "vnode.h" #ifdef __cplusplus extern "C" { diff --git a/source/server/vnode/src/vnode.c b/source/server/vnode/src/vnode.c deleted file mode 100644 index 6dea4a4e57392be988126c579648f39a8270b9bf..0000000000000000000000000000000000000000 --- a/source/server/vnode/src/vnode.c +++ /dev/null @@ -1,14 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ \ No newline at end of file diff --git a/source/server/vnode/src/vnodeInt.c b/source/server/vnode/src/vnodeInt.c new file mode 100644 index 0000000000000000000000000000000000000000..ac14d2a75643432c4df2584cf6416cafb1ee223c --- /dev/null +++ b/source/server/vnode/src/vnodeInt.c @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#include "vnodeInt.h" + +int32_t vnodeInit(SVnodePara para) { return 0; } + +void vnodeCleanup() {} + +int32_t vnodeGetStatistics(SVnodeStat *stat) { return 0; } + +void vnodeGetStatus(struct SStatusMsg *status) {} + +void vnodeSetAccess(struct SVgroupAccess *access, int32_t numOfVnodes) {} + +void vnodeProcessMsg(SRpcMsg *msg) {} diff --git a/source/server/vnode/tq/CMakeLists.txt b/source/server/vnode/tq/CMakeLists.txt index f4102b8d4b3e598c2afadbbc2476d487530f8fb2..9577007400b58e712f5996aa1378fc9a2c4e7a08 100644 --- a/source/server/vnode/tq/CMakeLists.txt +++ b/source/server/vnode/tq/CMakeLists.txt @@ -4,4 +4,9 @@ target_include_directories( tq PUBLIC "${CMAKE_SOURCE_DIR}/include/server/vnode/tq" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" -) \ No newline at end of file + PRIVATE "${CMAKE_SOURCE_DIR}/include/os" +) + +target_link_libraries( + os +) diff --git a/source/server/vnode/tq/inc/tqInt.h b/source/server/vnode/tq/inc/tqInt.h index 9a4b9731065270b66f2b75d3a372874b941eaa55..416a915456e78b1746e4a3cc5a4cde5336b42f97 100644 --- a/source/server/vnode/tq/inc/tqInt.h +++ b/source/server/vnode/tq/inc/tqInt.h @@ -16,12 +16,17 @@ #ifndef _TD_TQ_INT_H_ #define _TD_TQ_INT_H_ +#include "tq.h" + #ifdef __cplusplus extern "C" { #endif +//implement the array index +//implement the ring buffer + #ifdef __cplusplus } #endif -#endif /*_TD_TQ_INT_H_*/ \ No newline at end of file +#endif /*_TD_TQ_INT_H_*/ diff --git a/source/server/vnode/tq/src/tq.c b/source/server/vnode/tq/src/tq.c index 6dea4a4e57392be988126c579648f39a8270b9bf..3255f3fb3a1c4723186daab7b3b6fdb5faedb683 100644 --- a/source/server/vnode/tq/src/tq.c +++ b/source/server/vnode/tq/src/tq.c @@ -11,4 +11,23 @@ * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . - */ \ No newline at end of file + */ + +#include "tqInt.h" + +//static +//read next version data +// +//send to fetch queue +// +//handle management message + +int tqPushMsg(STQ* pTq , void* p, int64_t version) { + //add reference + // + return 0; +} + +int tqCommit(STQ* pTq) { + return 0; +} diff --git a/source/server/vnode/tsdb/CMakeLists.txt b/source/server/vnode/tsdb/CMakeLists.txt index 4a1e154cd52375ad9049ceb1bbc84097d46d341b..426fa2a317e0fd3c0027284215649845cc40cc82 100644 --- a/source/server/vnode/tsdb/CMakeLists.txt +++ b/source/server/vnode/tsdb/CMakeLists.txt @@ -5,8 +5,8 @@ target_include_directories( PUBLIC "${CMAKE_SOURCE_DIR}/include/server/vnode/tsdb" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) -target_include_directories( +target_link_libraries( tsdb - PRIVATE os + PUBLIC os PRIVATE common ) \ No newline at end of file diff --git a/source/server/vnode/tsdb/src/tsdb.c b/source/server/vnode/tsdb/src/tsdb.c index 6dea4a4e57392be988126c579648f39a8270b9bf..b79d6a850e43ec1b607a3a415c3fa978b2bc642a 100644 --- a/source/server/vnode/tsdb/src/tsdb.c +++ b/source/server/vnode/tsdb/src/tsdb.c @@ -11,4 +11,9 @@ * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . - */ \ No newline at end of file + */ + +#include "tsdb.h" + +int tsdbInsert(STsdb *pTsdb, SSubmitReq *pMsg) { return 0; } +int tsdbCommit(STsdb *pTsdb) { return 0; } \ No newline at end of file diff --git a/source/util/CMakeLists.txt b/source/util/CMakeLists.txt index c53be0c59bf537f096e16daf6d86579044e730ac..09b877ea8eb53f6429f60e0da61763885ea7e544 100644 --- a/source/util/CMakeLists.txt +++ b/source/util/CMakeLists.txt @@ -3,9 +3,14 @@ add_library(util ${UTIL_SRC}) target_include_directories( util PUBLIC "${CMAKE_SOURCE_DIR}/include/util" - PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include" + PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) target_link_libraries( util PRIVATE os -) \ No newline at end of file + PUBLIC zlib + PUBLIC lz4_static + PUBLIC api +) + +CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/src/version.c.in" "${CMAKE_CURRENT_SOURCE_DIR}/src/version.c") diff --git a/src/util/src/exception.c b/source/util/src/exception.c similarity index 100% rename from src/util/src/exception.c rename to source/util/src/exception.c diff --git a/source/util/src/talgo.c b/source/util/src/talgo.c index b9aabd7c4ae210dd3ab1c0136220bd0df4dcf6a1..f8520b0742951cb6f63b6fc626326578282791e3 100644 --- a/source/util/src/talgo.c +++ b/source/util/src/talgo.c @@ -14,7 +14,6 @@ */ #include "os.h" - #include "talgo.h" #define doswap(__left, __right, __size, __buf) do {\ diff --git a/src/util/src/tbase64.c b/source/util/src/tbase64.c similarity index 100% rename from src/util/src/tbase64.c rename to source/util/src/tbase64.c diff --git a/src/util/src/tbuffer.c b/source/util/src/tbuffer.c similarity index 98% rename from src/util/src/tbuffer.c rename to source/util/src/tbuffer.c index c06d1e59bd537bc009b462f0b17b9cdd00321d20..7c1eeaaf4f8e37cf7b5adbbf7e8a342218520868 100644 --- a/src/util/src/tbuffer.c +++ b/source/util/src/tbuffer.c @@ -16,7 +16,7 @@ #include "os.h" #include "tbuffer.h" #include "exception.h" -#include "taoserror.h" +//#include "taoserror.h" typedef union Un4B { uint32_t ui; @@ -41,7 +41,7 @@ static_assert(sizeof(Un8B) == sizeof(double), "sizeof(Un8B) must equal to sizeof size_t tbufSkip(SBufferReader* buf, size_t size) { if( (buf->pos + size) > buf->size ) { - THROW( TSDB_CODE_COM_MEMORY_CORRUPTED ); + THROW( -1 ); } size_t old = buf->pos; buf->pos += size; @@ -73,7 +73,7 @@ const char* tbufReadString( SBufferReader* buf, size_t* len ) { const char* ret = buf->data + buf->pos; tbufSkip( buf, l + 1 ); if( ret[l] != 0 ) { - THROW( TSDB_CODE_COM_MEMORY_CORRUPTED ); + THROW( -1 ); } if( len != NULL ) { *len = l; @@ -228,7 +228,7 @@ void tbufEnsureCapacity( SBufferWriter* buf, size_t size ) { char* data = (*buf->allocator)( buf->data, nsize ); // TODO: the exception should be thrown by the allocator function if( data == NULL ) { - THROW( TSDB_CODE_COM_OUT_OF_MEMORY ); + THROW( -1 ); } buf->data = data; buf->size = nsize; diff --git a/src/util/src/tcache.c b/source/util/src/tcache.c similarity index 96% rename from src/util/src/tcache.c rename to source/util/src/tcache.c index 589d3d4fa57c42b472319673a72d2e7ab599689f..621f103207e0161a1189a63d0fc9f108f322afca 100644 --- a/src/util/src/tcache.c +++ b/source/util/src/tcache.c @@ -15,12 +15,10 @@ #define _DEFAULT_SOURCE #include "os.h" -#include "tulog.h" +#include "ulog.h" #include "ttimer.h" #include "tutil.h" #include "tcache.h" -#include "hash.h" -#include "hashfunc.h" static FORCE_INLINE void __cache_wr_lock(SCacheObj *pCacheObj) { #if defined(LINUX) @@ -245,7 +243,8 @@ void *taosCachePut(SCacheObj *pCacheObj, const void *key, size_t keyLen, const v } else { // duplicated key exists while (1) { SCacheDataNode* p = NULL; - int32_t ret = taosHashRemoveWithData(pCacheObj->pHashTable, key, keyLen, (void*) &p, sizeof(void*)); +// int32_t ret = taosHashRemoveWithData(pCacheObj->pHashTable, key, keyLen, (void*) &p, sizeof(void*)); + int32_t ret = taosHashRemove(pCacheObj->pHashTable, key, keyLen); // add to trashcan if (ret == 0) { @@ -305,7 +304,8 @@ void *taosCacheAcquireByKey(SCacheObj *pCacheObj, const void *key, size_t keyLen } SCacheDataNode* ptNode = NULL; - taosHashGetClone(pCacheObj->pHashTable, key, keyLen, incRefFn, &ptNode); + taosHashGetClone(pCacheObj->pHashTable, key, keyLen, &ptNode); +// taosHashGetClone(pCacheObj->pHashTable, key, keyLen, incRefFn, &ptNode); void* pData = (ptNode != NULL)? ptNode->data:NULL; @@ -433,7 +433,8 @@ void taosCacheRelease(SCacheObj *pCacheObj, void **data, bool _remove) { // NOTE: remove it from hash in the first place, otherwise, the pNode may have been released by other thread // when reaches here. SCacheDataNode *p = NULL; - int32_t ret = taosHashRemoveWithData(pCacheObj->pHashTable, pNode->key, pNode->keySize, &p, sizeof(void *)); + int32_t ret = taosHashRemove(pCacheObj->pHashTable, pNode->key, pNode->keySize); +// int32_t ret = taosHashRemoveWithData(pCacheObj->pHashTable, pNode->key, pNode->keySize, &p, sizeof(void *)); ref = T_REF_DEC(pNode); // successfully remove from hash table, if failed, this node must have been move to trash already, do nothing. @@ -528,7 +529,7 @@ static bool travHashTableEmptyFn(void* param, void* data) { void taosCacheEmpty(SCacheObj *pCacheObj) { SHashTravSupp sup = {.pCacheObj = pCacheObj, .fp = NULL, .time = taosGetTimestampMs()}; - taosHashCondTraverse(pCacheObj->pHashTable, travHashTableEmptyFn, &sup); +// taosHashCondTraverse(pCacheObj->pHashTable, travHashTableEmptyFn, &sup); taosTrashcanEmpty(pCacheObj, false); } @@ -641,8 +642,8 @@ void taosTrashcanEmpty(SCacheObj *pCacheObj, bool force) { } void doCleanupDataCache(SCacheObj *pCacheObj) { - SHashTravSupp sup = {.pCacheObj = pCacheObj, .fp = NULL, .time = taosGetTimestampMs()}; - taosHashCondTraverse(pCacheObj->pHashTable, travHashTableEmptyFn, &sup); +// SHashTravSupp sup = {.pCacheObj = pCacheObj, .fp = NULL, .time = taosGetTimestampMs()}; +// taosHashCondTraverse(pCacheObj->pHashTable, travHashTableEmptyFn, &sup); // todo memory leak if there are object with refcount greater than 0 in hash table? taosHashCleanup(pCacheObj->pHashTable); @@ -679,7 +680,7 @@ static void doCacheRefresh(SCacheObj* pCacheObj, int64_t time, __cache_trav_fn_t assert(pCacheObj != NULL); SHashTravSupp sup = {.pCacheObj = pCacheObj, .fp = fp, .time = time, .param1 = param1}; - taosHashCondTraverse(pCacheObj->pHashTable, travHashTableFn, &sup); +// taosHashCondTraverse(pCacheObj->pHashTable, travHashTableFn, &sup); } void taosCacheRefreshWorkerUnexpectedStopped(void) { diff --git a/src/util/src/tcompression.c b/source/util/src/tcompression.c similarity index 99% rename from src/util/src/tcompression.c rename to source/util/src/tcompression.c index 48bba75926415752cfd777242a55ef71c5c96c2c..b0dcff88e7e0b946b9118094aa08d4a4ec04ae8d 100644 --- a/src/util/src/tcompression.c +++ b/source/util/src/tcompression.c @@ -52,11 +52,8 @@ #ifdef TD_TSZ #include "td_sz.h" #endif -#include "taosdef.h" -#include "tscompression.h" -#include "tulog.h" -#include "tglobal.h" - +#include "tcompression.h" +#include "ulog.h" static const int TEST_NUMBER = 1; #define is_bigendian() ((*(char *)&TEST_NUMBER) == 0) diff --git a/src/util/src/tconfig.c b/source/util/src/tconfig.c similarity index 87% rename from src/util/src/tconfig.c rename to source/util/src/tconfig.c index 5a3dc3f9bcdee41f974e48f22b27beb2a1eb5a35..6b3f08a446786c039b24befcbc9d2ec5f8f20a44 100644 --- a/src/util/src/tconfig.c +++ b/source/util/src/tconfig.c @@ -15,12 +15,8 @@ #define _DEFAULT_SOURCE #include "os.h" -#include "taosdef.h" -#include "taoserror.h" #include "tconfig.h" -#include "tglobal.h" -#include "tulog.h" -#include "tsocket.h" +#include "ulog.h" #include "tutil.h" SGlobalCfg tsGlobalConfig[TSDB_CFG_MAX_NUM] = {{0}}; @@ -78,7 +74,6 @@ static void taosReadDoubleConfig(SGlobalCfg *cfg, char *input_value) { } } - static void taosReadInt32Config(SGlobalCfg *cfg, char *input_value) { int32_t value = atoi(input_value); int32_t *option = (int32_t *)cfg->ptr; @@ -156,27 +151,10 @@ static bool taosReadDirectoryConfig(SGlobalCfg *cfg, char *input_value) { return false; } else { if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_FILE) { - wordexp_t full_path; - if (0 != wordexp(input_value, &full_path, 0)) { - printf("\nconfig dir: %s wordexp fail! reason:%s\n", input_value, strerror(errno)); - wordfree(&full_path); - return false; - } - - if (full_path.we_wordv != NULL && full_path.we_wordv[0] != NULL) { - strcpy(option, full_path.we_wordv[0]); - } + taosExpandDir(input_value, option, cfg->ptrLength); + taosRealPath(option, cfg->ptrLength); - wordfree(&full_path); - - char tmp[PATH_MAX] = {0}; - if (realpath(option, tmp) != NULL) { - strcpy(option, tmp); - } - - int code = taosMkDir(option, 0755); - if (code != 0) { - terrno = TAOS_SYSTEM_ERROR(errno); + if (!taosMkDir(option)) { uError("config option:%s, input value:%s, directory not exist, create fail:%s", cfg->option, input_value, strerror(errno)); return false; @@ -234,9 +212,9 @@ static void taosReadLogOption(char *option, char *value) { switch (cfg->valType) { case TAOS_CFG_VTYPE_INT32: taosReadInt32Config(cfg, value); - if (strcasecmp(cfg->option, "debugFlag") == 0) { - taosSetAllDebugFlag(); - } + // if (strcasecmp(cfg->option, "debugFlag") == 0) { + // taosSetAllDebugFlag(); + // } break; case TAOS_CFG_VTYPE_DIRECTORY: taosReadDirectoryConfig(cfg, value); @@ -249,7 +227,6 @@ static void taosReadLogOption(char *option, char *value) { } SGlobalCfg *taosGetConfigOption(const char *option) { - taosInitGlobalCfg(); for (int i = 0; i < tsGlobalConfigNum; ++i) { SGlobalCfg *cfg = tsGlobalConfig + i; if (strcasecmp(cfg->option, option) != 0) continue; @@ -294,7 +271,7 @@ static void taosReadConfigOption(const char *option, char *value, char *value2, break; case TAOS_CFG_VTYPE_DATA_DIRCTORY: if (taosReadDirectoryConfig(cfg, value)) { - taosReadDataDirCfg(value, value2, value3); + // taosReadDataDirCfg(value, value2, value3); } break; default: @@ -315,34 +292,7 @@ void taosReadGlobalLogCfg() { int olen, vlen; char fileName[PATH_MAX] = {0}; - wordexp_t full_path; - if ( 0 != wordexp(configDir, &full_path, 0)) { - printf("\nconfig file: %s wordexp fail! reason:%s\n", configDir, strerror(errno)); - wordfree(&full_path); - return; - } - - if (full_path.we_wordv != NULL && full_path.we_wordv[0] != NULL) { - if (strlen(full_path.we_wordv[0]) >= TSDB_FILENAME_LEN) { - printf("\nconfig file: %s path overflow max len %d, all variables are set to default\n", full_path.we_wordv[0], TSDB_FILENAME_LEN - 1); - wordfree(&full_path); - return; - } - strcpy(configDir, full_path.we_wordv[0]); - } else { - #ifdef _TD_POWER_ - printf("configDir:%s not there, use default value: /etc/power", configDir); - strcpy(configDir, "/etc/power"); - #elif (_TD_TQ_ == true) - printf("configDir:%s not there, use default value: /etc/tq", configDir); - strcpy(configDir, "/etc/tq"); - #else - printf("configDir:%s not there, use default value: /etc/taos", configDir); - strcpy(configDir, "/etc/taos"); - #endif - } - wordfree(&full_path); - + taosExpandDir(configDir, configDir, PATH_MAX); taosReadLogOption("logDir", tsLogDir); sprintf(fileName, "%s/taos.cfg", configDir); @@ -391,13 +341,8 @@ bool taosReadGlobalCfg() { char fileName[PATH_MAX] = {0}; sprintf(fileName, "%s/taos.cfg", configDir); - FILE* fp = fopen(fileName, "r"); + FILE *fp = fopen(fileName, "r"); if (fp == NULL) { - struct stat s; - if (stat(configDir, &s) != 0 || (!S_ISREG(s.st_mode) && !S_ISLNK(s.st_mode))) { - //return true to follow behavior before file support - return true; - } fp = fopen(configDir, "r"); if (fp == NULL) { return false; @@ -444,9 +389,9 @@ bool taosReadGlobalCfg() { tfree(line); - if (debugFlag & DEBUG_TRACE || debugFlag & DEBUG_DEBUG || debugFlag & DEBUG_DUMP) { - taosSetAllDebugFlag(); - } + // if (debugFlag & DEBUG_TRACE || debugFlag & DEBUG_DEBUG || debugFlag & DEBUG_DUMP) { + // taosSetAllDebugFlag(); + // } return true; } @@ -498,7 +443,7 @@ void taosPrintGlobalCfg() { } taosPrintOsInfo(); - taosPrintDataDirCfg(); + // taosPrintDataDirCfg(); uInfo("=================================="); } diff --git a/src/util/src/tcrc32c.c b/source/util/src/tcrc32c.c similarity index 99% rename from src/util/src/tcrc32c.c rename to source/util/src/tcrc32c.c index d2b63eb9ee7dd9a3119866bcf2d61b893192b14b..73bba4480e99f45a41ce937e9ea99bcd9077e995 100644 --- a/src/util/src/tcrc32c.c +++ b/source/util/src/tcrc32c.c @@ -18,14 +18,15 @@ 3. This notice may not be removed or altered from any source distribution. */ +#include "os.h" +#include "tcrc32c.h" +#include "tdef.h" + #if !defined(_TD_ARM_) && !defined(_TD_MIPS_) #include #endif -#include -#include -#include "tcrc32c.h" #define POLY 0x82f63b78 #define LONG_SHIFT 8192 diff --git a/src/util/src/tdes.c b/source/util/src/tdes.c similarity index 100% rename from src/util/src/tdes.c rename to source/util/src/tdes.c diff --git a/src/util/src/tfile.c b/source/util/src/tfile.c similarity index 79% rename from src/util/src/tfile.c rename to source/util/src/tfile.c index 455c885e753e35724d27ec223f86ebf04751286f..5b61186b127603c513624a60f4993196b3516b5e 100644 --- a/src/util/src/tfile.c +++ b/source/util/src/tfile.c @@ -16,14 +16,14 @@ #define _DEFAULT_SOURCE #include "os.h" #include "taoserror.h" -#include "tulog.h" +#include "ulog.h" #include "tutil.h" #include "tref.h" static int32_t tsFileRsetId = -1; static void tfCloseFile(void *p) { - close((int32_t)(uintptr_t)p); + taosCloseFile((int32_t)(uintptr_t)p); } int32_t tfInit() { @@ -48,18 +48,23 @@ static int64_t tfOpenImp(int32_t fd) { void * p = (void *)(int64_t)fd; int64_t rid = taosAddRef(tsFileRsetId, p); - if (rid < 0) close(fd); + if (rid < 0) taosCloseFile(fd); return rid; } -int64_t tfOpen(const char *pathname, int32_t flags) { - int32_t fd = open(pathname, flags | O_BINARY); +int64_t tfOpenReadWrite(const char *pathname, int32_t flags) { + int32_t fd = taosOpenFileReadWrite(pathname); return tfOpenImp(fd); } -int64_t tfOpenM(const char *pathname, int32_t flags, mode_t mode) { - int32_t fd = open(pathname, flags | O_BINARY, mode); +int64_t tfOpenCreateWrite(const char *pathname, int32_t flags, mode_t mode) { + int32_t fd = taosOpenFileCreateWrite(pathname); + return tfOpenImp(fd); +} + +int64_t tfOpenCreateWriteAppend(const char *pathname, int32_t flags, mode_t mode) { + int32_t fd = taosOpenFileCreateWriteAppend(pathname); return tfOpenImp(fd); } @@ -73,7 +78,7 @@ int64_t tfWrite(int64_t tfd, void *buf, int64_t count) { int32_t fd = (int32_t)(uintptr_t)p; - int64_t ret = taosWrite(fd, buf, count); + int64_t ret = taosWriteFile(fd, buf, count); if (ret < 0) terrno = TAOS_SYSTEM_ERROR(errno); taosReleaseRef(tsFileRsetId, tfd); @@ -86,7 +91,7 @@ int64_t tfRead(int64_t tfd, void *buf, int64_t count) { int32_t fd = (int32_t)(uintptr_t)p; - int64_t ret = taosRead(fd, buf, count); + int64_t ret = taosReadFile(fd, buf, count); if (ret < 0) terrno = TAOS_SYSTEM_ERROR(errno); taosReleaseRef(tsFileRsetId, tfd); @@ -98,7 +103,7 @@ int32_t tfFsync(int64_t tfd) { if (p == NULL) return -1; int32_t fd = (int32_t)(uintptr_t)p; - int32_t code = taosFsync(fd); + int32_t code = taosFsyncFile(fd); taosReleaseRef(tsFileRsetId, tfd); return code; @@ -117,7 +122,7 @@ int64_t tfLseek(int64_t tfd, int64_t offset, int32_t whence) { if (p == NULL) return -1; int32_t fd = (int32_t)(uintptr_t)p; - int64_t ret = taosLSeek(fd, offset, whence); + int64_t ret = taosLSeekFile(fd, offset, whence); taosReleaseRef(tsFileRsetId, tfd); return ret; @@ -128,7 +133,7 @@ int32_t tfFtruncate(int64_t tfd, int64_t length) { if (p == NULL) return -1; int32_t fd = (int32_t)(uintptr_t)p; - int32_t code = taosFtruncate(fd, length); + int32_t code = taosFtruncateFile(fd, length); taosReleaseRef(tsFileRsetId, tfd); return code; diff --git a/src/util/src/tfunctional.c b/source/util/src/tfunctional.c similarity index 100% rename from src/util/src/tfunctional.c rename to source/util/src/tfunctional.c diff --git a/src/util/src/hash.c b/source/util/src/thash.c similarity index 97% rename from src/util/src/hash.c rename to source/util/src/thash.c index 6577a0a0f4710951cf240f792d68f1afb2d37569..448fc0ab2ea4b75278285459e066c1cee7e2fdac 100644 --- a/src/util/src/hash.c +++ b/source/util/src/thash.c @@ -14,9 +14,9 @@ */ #include "os.h" -#include "hash.h" -#include "tulog.h" -#include "taosdef.h" +#include "thash.h" +#include "ulog.h" +#include "tdef.h" #define EXT_SIZE 1024 @@ -24,7 +24,7 @@ #define DO_FREE_HASH_NODE(_n) \ do { \ - tfree(_n); \ + tfree(_n); \ } while (0) #define FREE_HASH_NODE(_h, _n) \ @@ -47,7 +47,6 @@ static FORCE_INLINE void __rd_lock(void *lock, int32_t type) { if (type == HASH_NO_LOCK) { return; } - taosRLockLatch(lock); } @@ -296,8 +295,9 @@ int32_t taosHashPut(SHashObj *pHashObj, const void *key, size_t keyLen, void *da } void *taosHashGet(SHashObj *pHashObj, const void *key, size_t keyLen) { - return taosHashGetClone(pHashObj, key, keyLen, NULL, NULL); + return taosHashGetClone(pHashObj, key, keyLen, NULL); } + //TODO(yihaoDeng), merge with taosHashGetClone void* taosHashGetCloneExt(SHashObj *pHashObj, const void *key, size_t keyLen, void (*fp)(void *), void** d, size_t *sz) { if (taosHashTableEmpty(pHashObj) || keyLen == 0 || key == NULL) { @@ -361,7 +361,7 @@ void* taosHashGetCloneExt(SHashObj *pHashObj, const void *key, size_t keyLen, vo return data; } -void* taosHashGetClone(SHashObj *pHashObj, const void *key, size_t keyLen, void (*fp)(void *), void* d) { +void* taosHashGetClone(SHashObj *pHashObj, const void *key, size_t keyLen, void* d) { if (taosHashTableEmpty(pHashObj) || keyLen == 0 || key == NULL) { return NULL; } @@ -395,8 +395,8 @@ void* taosHashGetClone(SHashObj *pHashObj, const void *key, size_t keyLen, void SHashNode *pNode = doSearchInEntryList(pHashObj, pe, key, keyLen, hashVal); if (pNode != NULL) { - if (fp != NULL) { - fp(GET_HASH_NODE_DATA(pNode)); + if (pHashObj->callbackFp != NULL) { + pHashObj->callbackFp(GET_HASH_NODE_DATA(pNode)); } if (d != NULL) { @@ -414,11 +414,7 @@ void* taosHashGetClone(SHashObj *pHashObj, const void *key, size_t keyLen, void return data; } -int32_t taosHashRemove(SHashObj *pHashObj, const void *key, size_t keyLen) { - return taosHashRemoveWithData(pHashObj, key, keyLen, NULL, 0); -} - -int32_t taosHashRemoveWithData(SHashObj *pHashObj, const void *key, size_t keyLen, void *data, size_t dsize) { +int32_t taosHashRemove(SHashObj *pHashObj, const void *key, size_t keyLen/*, void *data, size_t dsize*/) { if (pHashObj == NULL || taosHashTableEmpty(pHashObj)) { return -1; } @@ -449,13 +445,14 @@ int32_t taosHashRemoveWithData(SHashObj *pHashObj, const void *key, size_t keyLe SHashNode *prevNode = NULL; while (pNode) { - if ((pNode->keyLen == keyLen) && ((*(pHashObj->equalFp))(GET_HASH_NODE_KEY(pNode), key, keyLen) == 0) && pNode->removed == 0) + if ((pNode->keyLen == keyLen) && ((*(pHashObj->equalFp))(GET_HASH_NODE_KEY(pNode), key, keyLen) == 0) && pNode->removed == 0) break; prevNode = pNode; pNode = pNode->next; } + if (pNode) { code = 0; // it is found @@ -467,14 +464,14 @@ int32_t taosHashRemoveWithData(SHashObj *pHashObj, const void *key, size_t keyLe } else { pe->next = pNode->next; } - - if (data) memcpy(data, GET_HASH_NODE_DATA(pNode), dsize); + +// if (data) memcpy(data, GET_HASH_NODE_DATA(pNode), dsize); pe->num--; atomic_sub_fetch_64(&pHashObj->size, 1); FREE_HASH_NODE(pHashObj, pNode); } - } + } if (pHashObj->type == HASH_ENTRY_LOCK) { taosWUnLockLatch(&pe->latch); diff --git a/src/util/src/thashutil.c b/source/util/src/thashutil.c similarity index 88% rename from src/util/src/thashutil.c rename to source/util/src/thashutil.c index 4a0208a3d0bf22f21b5f6a05513f435664e746af..83f85bfdbcf2746913692f30f0afe125f4da559f 100644 --- a/src/util/src/thashutil.c +++ b/source/util/src/thashutil.c @@ -14,9 +14,9 @@ */ #include "os.h" -#include "hashfunc.h" -#include "tutil.h" +#include "thash.h" #include "tcompare.h" +#include "tdef.h" #define ROTL32(x, r) ((x) << (r) | (x) >> (32u - (r))) @@ -163,21 +163,20 @@ _hash_fn_t taosGetDefaultHashFunction(int32_t type) { return fn; } -int32_t taosFloatEqual(const void *a, const void *b, size_t UNUSED_PARAM(sz)) { - return getComparFunc(TSDB_DATA_TYPE_FLOAT, -1)(a, b); -} - -int32_t taosDoubleEqual(const void *a, const void *b, size_t UNUSED_PARAM(sz)) { - return getComparFunc(TSDB_DATA_TYPE_DOUBLE, -1)(a, b); -} +//int32_t taosFloatEqual(const void *a, const void *b, size_t UNUSED_PARAM(sz)) { +// return getComparFunc(TSDB_DATA_TYPE_FLOAT, -1)(a, b); +//} +// +//int32_t taosDoubleEqual(const void *a, const void *b, size_t UNUSED_PARAM(sz)) { +// return getComparFunc(TSDB_DATA_TYPE_DOUBLE, -1)(a, b); +//} _equal_fn_t taosGetDefaultEqualFunction(int32_t type) { _equal_fn_t fn = NULL; - switch (type) { - case TSDB_DATA_TYPE_FLOAT: fn = taosFloatEqual; break; - case TSDB_DATA_TYPE_DOUBLE: fn = taosDoubleEqual; break; - default: fn = memcmp; break; - } +// switch (type) { +// case TSDB_DATA_TYPE_FLOAT: fn = taosFloatEqual; break; +// case TSDB_DATA_TYPE_DOUBLE: fn = taosDoubleEqual; break; +// default: fn = memcmp; break; +// } return fn; - } diff --git a/src/util/src/tidpool.c b/source/util/src/tidpool.c similarity index 99% rename from src/util/src/tidpool.c rename to source/util/src/tidpool.c index 61cecf54c0e14a4435ba72d317715cdec902068b..00c43bb25fdf6cea9d618b0ad5c48a0951a93b35 100644 --- a/src/util/src/tidpool.c +++ b/source/util/src/tidpool.c @@ -14,7 +14,7 @@ */ #include "os.h" -#include "tulog.h" +#include "ulog.h" typedef struct { int maxId; diff --git a/src/util/src/tlog.c b/source/util/src/tlog.c similarity index 81% rename from src/util/src/tlog.c rename to source/util/src/tlog.c index 1ce3eadf58432337511d0d600848ad334b96fc91..2fb84656b60129247dc76f0efa7e96260e0a5399 100644 --- a/src/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -15,11 +15,12 @@ #define _DEFAULT_SOURCE #include "os.h" -#include "tulog.h" +#include "ulog.h" #include "tlog.h" #include "tnote.h" #include "tutil.h" - +#include "zlib.h" + #define MAX_LOGLINE_SIZE (1000) #define MAX_LOGLINE_BUFFER_SIZE (MAX_LOGLINE_SIZE + 10) #define MAX_LOGLINE_CONTENT_SIZE (MAX_LOGLINE_SIZE - 100) @@ -75,20 +76,34 @@ float tsMinimalLogDirGB = 1.0f; int64_t asyncLogLostLines = 0; int32_t writeInterval = DEFAULT_LOG_INTERVAL; +// log +int32_t tsNumOfLogLines = 10000000; +int32_t mDebugFlag = 131; +int32_t sdbDebugFlag = 131; +int32_t dDebugFlag = 135; +int32_t vDebugFlag = 135; +int32_t cDebugFlag = 131; +int32_t jniDebugFlag = 131; +int32_t odbcDebugFlag = 131; +int32_t httpDebugFlag = 131; +int32_t mqttDebugFlag = 131; +int32_t monDebugFlag = 131; +int32_t qDebugFlag = 131; +int32_t rpcDebugFlag = 131; +int32_t uDebugFlag = 131; +int32_t debugFlag = 0; +int32_t sDebugFlag = 135; +int32_t wDebugFlag = 135; +int32_t tsdbDebugFlag = 131; +int32_t cqDebugFlag = 131; +int32_t fsDebugFlag = 135; + int64_t dbgEmptyW = 0; int64_t dbgWN = 0; int64_t dbgSmallWN = 0; int64_t dbgBigWN = 0; int64_t dbgWSize = 0; -#ifdef _TD_POWER_ -char tsLogDir[TSDB_FILENAME_LEN] = "/var/log/power"; -#elif (_TD_TQ_ == true) -char tsLogDir[TSDB_FILENAME_LEN] = "/var/log/tq"; -#else -char tsLogDir[PATH_MAX] = "/var/log/taos"; -#endif - static SLogObj tsLogObj = { .fileNum = 1 }; static void * taosAsyncOutputLog(void *param); static int32_t taosPushLogBuffer(SLogBuff *tLogBuff, char *msg, int32_t msgLen); @@ -96,6 +111,7 @@ static SLogBuff *taosLogBuffNew(int32_t bufSize); static void taosCloseLogByFd(int32_t oldFd); static int32_t taosOpenLogFile(char *fn, int32_t maxLines, int32_t maxFileNum); extern void taosPrintGlobalCfg(); +static int32_t taosCompressFile(char *srcFileName, char *destFileName); static int32_t taosStartLog() { pthread_attr_t threadAttr; @@ -134,11 +150,11 @@ void taosCloseLog() { // taosCloseLog(); } -static bool taosLockFile(int32_t fd) { +static bool taosLockLogFile(int32_t fd) { if (fd < 0) return false; if (tsLogObj.fileNum > 1) { - int32_t ret = flock(fd, LOCK_EX | LOCK_NB); + int32_t ret = taosLockFile(fd); if (ret == 0) { return true; } @@ -147,11 +163,11 @@ static bool taosLockFile(int32_t fd) { return false; } -static void taosUnLockFile(int32_t fd) { +static void taosUnLockLogFile(int32_t fd) { if (fd < 0) return; if (tsLogObj.fileNum > 1) { - flock(fd, LOCK_UN | LOCK_NB); + taosUnLockFile(fd); } } @@ -162,7 +178,7 @@ static void taosKeepOldLog(char *oldName) { char fileName[LOG_FILE_NAME_LEN + 20]; snprintf(fileName, LOG_FILE_NAME_LEN + 20, "%s.%" PRId64, tsLogObj.logName, fileSec); - taosRename(oldName, fileName); + taosRenameFile(oldName, fileName); if (tsLogKeepDays < 0) { char compressFileName[LOG_FILE_NAME_LEN + 20]; snprintf(compressFileName, LOG_FILE_NAME_LEN + 20, "%s.%" PRId64 ".gz", tsLogObj.logName, fileSec); @@ -171,7 +187,7 @@ static void taosKeepOldLog(char *oldName) { } } - taosRemoveOldLogFiles(tsLogDir, ABS(tsLogKeepDays)); + taosRemoveOldFiles(tsLogDir, ABS(tsLogKeepDays)); } static void *taosThreadToOpenNewFile(void *param) { @@ -183,9 +199,9 @@ static void *taosThreadToOpenNewFile(void *param) { char name[LOG_FILE_NAME_LEN + 20]; sprintf(name, "%s.%d", tsLogObj.logName, tsLogObj.flag); - umask(0); + taosUmaskFile(0); - int32_t fd = open(name, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO); + int32_t fd = taosOpenFileCreateWriteTrunc(name); if (fd < 0) { tsLogObj.openInProgress = 0; tsLogObj.lines = tsLogObj.maxLines - 1000; @@ -193,8 +209,8 @@ static void *taosThreadToOpenNewFile(void *param) { return NULL; } - taosLockFile(fd); - (void)lseek(fd, 0, SEEK_SET); + taosLockLogFile(fd); + (void)taosLSeekFile(fd, 0, SEEK_SET); int32_t oldFd = tsLogObj.logHandle->fd; tsLogObj.logHandle->fd = fd; @@ -246,7 +262,7 @@ void taosResetLog() { } static bool taosCheckFileIsOpen(char *logFileName) { - int32_t fd = open(logFileName, O_WRONLY, S_IRWXU | S_IRWXG | S_IRWXO); + int32_t fd = taosOpenFileWrite(logFileName); if (fd < 0) { if (errno == ENOENT) { return false; @@ -256,12 +272,12 @@ static bool taosCheckFileIsOpen(char *logFileName) { } } - if (taosLockFile(fd)) { - taosUnLockFile(fd); - taosClose(fd); + if (taosLockLogFile(fd)) { + taosUnLockLogFile(fd); + taosCloseFile(fd); return false; } else { - taosClose(fd); + taosCloseFile(fd); return true; } } @@ -298,9 +314,9 @@ static int32_t taosOpenLogFile(char *fn, int32_t maxLines, int32_t maxFileNum) { maxFileNum = 1; #endif - char name[LOG_FILE_NAME_LEN + 50] = "\0"; - struct stat logstat0, logstat1; - int32_t size; + char name[LOG_FILE_NAME_LEN + 50] = "\0"; + int32_t logstat0_mtime, logstat1_mtime; + int32_t size; tsLogObj.maxLines = maxLines; tsLogObj.fileNum = maxFileNum; @@ -310,14 +326,14 @@ static int32_t taosOpenLogFile(char *fn, int32_t maxLines, int32_t maxFileNum) { strcpy(name, fn); strcat(name, ".0"); } - bool log0Exist = stat(name, &logstat0) >= 0; + bool log0Exist = taosStatFile(name, NULL, &logstat0_mtime) >= 0; if (strlen(fn) < LOG_FILE_NAME_LEN + 50 - 2) { strcpy(name, fn); strcat(name, ".1"); } - bool log1Exist = stat(name, &logstat1) >= 0; - + bool log1Exist = taosStatFile(name, NULL, &logstat1_mtime) >= 0; + // if none of the log files exist, open 0, if both exists, open the old one if (!log0Exist && !log1Exist) { tsLogObj.flag = 0; @@ -326,39 +342,39 @@ static int32_t taosOpenLogFile(char *fn, int32_t maxLines, int32_t maxFileNum) { } else if (!log0Exist) { tsLogObj.flag = 1; } else { - tsLogObj.flag = (logstat0.st_mtime > logstat1.st_mtime) ? 0 : 1; + tsLogObj.flag = (logstat0_mtime > logstat1_mtime) ? 0 : 1; } char fileName[LOG_FILE_NAME_LEN + 50] = "\0"; sprintf(fileName, "%s.%d", tsLogObj.logName, tsLogObj.flag); pthread_mutex_init(&tsLogObj.logMutex, NULL); - umask(0); - tsLogObj.logHandle->fd = open(fileName, O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); + taosUmaskFile(0); + tsLogObj.logHandle->fd = taosOpenFileCreateWrite(fileName); if (tsLogObj.logHandle->fd < 0) { printf("\nfailed to open log file:%s, reason:%s\n", fileName, strerror(errno)); return -1; } - taosLockFile(tsLogObj.logHandle->fd); + taosLockLogFile(tsLogObj.logHandle->fd); // only an estimate for number of lines - struct stat filestat; - if (fstat(tsLogObj.logHandle->fd, &filestat) < 0) { + int64_t filesize = 0; + if (taosFStatFile(tsLogObj.logHandle->fd, &filesize, NULL) < 0) { printf("\nfailed to fstat log file:%s, reason:%s\n", fileName, strerror(errno)); return -1; } - size = (int32_t)filestat.st_size; + size = (int32_t)filesize; tsLogObj.lines = size / 60; - lseek(tsLogObj.logHandle->fd, 0, SEEK_END); + taosLSeekFile(tsLogObj.logHandle->fd, 0, SEEK_END); sprintf(name, "==================================================\n"); - taosWrite(tsLogObj.logHandle->fd, name, (uint32_t)strlen(name)); + taosWriteFile(tsLogObj.logHandle->fd, name, (uint32_t)strlen(name)); sprintf(name, " new log file \n"); - taosWrite(tsLogObj.logHandle->fd, name, (uint32_t)strlen(name)); + taosWriteFile(tsLogObj.logHandle->fd, name, (uint32_t)strlen(name)); sprintf(name, "==================================================\n"); - taosWrite(tsLogObj.logHandle->fd, name, (uint32_t)strlen(name)); + taosWriteFile(tsLogObj.logHandle->fd, name, (uint32_t)strlen(name)); return 0; } @@ -377,7 +393,7 @@ void taosPrintLog(const char *flags, int32_t dflag, const char *format, ...) { struct timeval timeSecs; time_t curTime; - gettimeofday(&timeSecs, NULL); + taosGetTimeOfDay(&timeSecs); curTime = timeSecs.tv_sec; ptm = localtime_r(&curTime, &Tm); @@ -408,7 +424,7 @@ void taosPrintLog(const char *flags, int32_t dflag, const char *format, ...) { if (tsAsyncLog) { taosPushLogBuffer(tsLogObj.logHandle, buffer, len); } else { - taosWrite(tsLogObj.logHandle->fd, buffer, len); + taosWriteFile(tsLogObj.logHandle->fd, buffer, len); } if (tsLogObj.maxLines > 0) { @@ -419,7 +435,7 @@ void taosPrintLog(const char *flags, int32_t dflag, const char *format, ...) { } if (dflag & DEBUG_SCREEN) - taosWrite(1, buffer, (uint32_t)len); + taosWriteFile(1, buffer, (uint32_t)len); if (dflag == 255) nInfo(buffer, len); } @@ -439,7 +455,7 @@ void taosDumpData(unsigned char *msg, int32_t len) { pos += 3; if (c >= 16) { temp[pos++] = '\n'; - taosWrite(tsLogObj.logHandle->fd, temp, (uint32_t)pos); + taosWriteFile(tsLogObj.logHandle->fd, temp, (uint32_t)pos); c = 0; pos = 0; } @@ -447,7 +463,7 @@ void taosDumpData(unsigned char *msg, int32_t len) { temp[pos++] = '\n'; - taosWrite(tsLogObj.logHandle->fd, temp, (uint32_t)pos); + taosWriteFile(tsLogObj.logHandle->fd, temp, (uint32_t)pos); } void taosPrintLongString(const char *flags, int32_t dflag, const char *format, ...) { @@ -464,7 +480,7 @@ void taosPrintLongString(const char *flags, int32_t dflag, const char *format, . struct timeval timeSecs; time_t curTime; - gettimeofday(&timeSecs, NULL); + taosGetTimeOfDay(&timeSecs); curTime = timeSecs.tv_sec; ptm = localtime_r(&curTime, &Tm); @@ -485,7 +501,7 @@ void taosPrintLongString(const char *flags, int32_t dflag, const char *format, . if (tsAsyncLog) { taosPushLogBuffer(tsLogObj.logHandle, buffer, len); } else { - taosWrite(tsLogObj.logHandle->fd, buffer, len); + taosWriteFile(tsLogObj.logHandle->fd, buffer, len); } if (tsLogObj.maxLines > 0) { @@ -495,7 +511,7 @@ void taosPrintLongString(const char *flags, int32_t dflag, const char *format, . } } - if (dflag & DEBUG_SCREEN) taosWrite(1, buffer, (uint32_t)len); + if (dflag & DEBUG_SCREEN) taosWriteFile(1, buffer, (uint32_t)len); } #if 0 @@ -506,8 +522,8 @@ void taosCloseLog() { static void taosCloseLogByFd(int32_t fd) { if (fd >= 0) { - taosUnLockFile(fd); - taosClose(fd); + taosUnLockLogFile(fd); + taosCloseFile(fd); } } @@ -644,12 +660,12 @@ static void taosWriteLog(SLogBuff *tLogBuff) { } if (start < end) { - taosWrite(tLogBuff->fd, LOG_BUF_BUFFER(tLogBuff) + start, pollSize); + taosWriteFile(tLogBuff->fd, LOG_BUF_BUFFER(tLogBuff) + start, pollSize); } else { int32_t tsize = LOG_BUF_SIZE(tLogBuff) - start; - taosWrite(tLogBuff->fd, LOG_BUF_BUFFER(tLogBuff) + start, tsize); + taosWriteFile(tLogBuff->fd, LOG_BUF_BUFFER(tLogBuff) + start, tsize); - taosWrite(tLogBuff->fd, LOG_BUF_BUFFER(tLogBuff), end); + taosWriteFile(tLogBuff->fd, LOG_BUF_BUFFER(tLogBuff), end); } dbgWN++; @@ -700,3 +716,62 @@ static void *taosAsyncOutputLog(void *param) { return NULL; } + +int32_t taosCompressFile(char *srcFileName, char *destFileName) { + int32_t compressSize = 163840; + int32_t ret = 0; + int32_t len = 0; + char * data = malloc(compressSize); + FILE * srcFp = NULL; + gzFile dstFp = NULL; + + srcFp = fopen(srcFileName, "r"); + if (srcFp == NULL) { + ret = -1; + goto cmp_end; + } + + int32_t fd = taosOpenFileCreateWriteTrunc(destFileName); + if (fd < 0) { + ret = -2; + goto cmp_end; + } + + dstFp = gzdopen(fd, "wb6f"); + if (dstFp == NULL) { + ret = -3; + close(fd); + goto cmp_end; + } + + while (!feof(srcFp)) { + len = (int32_t)fread(data, 1, compressSize, srcFp); + (void)gzwrite(dstFp, data, len); + } + +cmp_end: + if (srcFp) { + fclose(srcFp); + } + if (dstFp) { + gzclose(dstFp); + } + free(data); + + return ret; +} + +void taosPrintOsInfo() { + SysNameInfo info = taosGetSysNameInfo(); + + uInfo(" os pageSize: %" PRId64 "(KB)", tsPageSize); + uInfo(" os openMax: %" PRId64, tsOpenMax); + uInfo(" os streamMax: %" PRId64, tsStreamMax); + uInfo(" os numOfCores: %d", tsNumOfCores); + uInfo(" os totalMemory: %d(MB)", tsTotalMemoryMB); + uInfo(" os sysname: %s", info.sysname); + uInfo(" os nodename: %s", info.nodename); + uInfo(" os release: %s", info.release); + uInfo(" os version: %s", info.version); + uInfo(" os machine: %s", info.machine); +} diff --git a/src/util/src/tlosertree.c b/source/util/src/tlosertree.c similarity index 97% rename from src/util/src/tlosertree.c rename to source/util/src/tlosertree.c index 0f104c4b63a36880a79ad564a0f837f9b09e7819..6155ba4c1add9f5ead6acbe702c4cfd7d55419e2 100644 --- a/src/util/src/tlosertree.c +++ b/source/util/src/tlosertree.c @@ -15,8 +15,7 @@ #include "os.h" #include "tlosertree.h" -#include "taosmsg.h" -#include "tulog.h" +#include "ulog.h" // set initial value for loser tree void tLoserTreeInit(SLoserTreeInfo* pTree) { @@ -46,7 +45,7 @@ uint32_t tLoserTreeCreate(SLoserTreeInfo** pTree, int32_t numOfEntries, void* pa *pTree = (SLoserTreeInfo*)calloc(1, sizeof(SLoserTreeInfo) + sizeof(SLoserTreeNode) * totalEntries); if ((*pTree) == NULL) { uError("allocate memory for loser-tree failed. reason:%s", strerror(errno)); - return TSDB_CODE_QRY_OUT_OF_MEMORY; + return -1; } (*pTree)->pNode = (SLoserTreeNode*)(((char*)(*pTree)) + sizeof(SLoserTreeInfo)); @@ -74,7 +73,7 @@ uint32_t tLoserTreeCreate(SLoserTreeInfo** pTree, int32_t numOfEntries, void* pa printf("initialize local reducer completed!\n"); #endif - return TSDB_CODE_SUCCESS; + return 0; } void tLoserTreeAdjust(SLoserTreeInfo* pTree, int32_t idx) { diff --git a/src/util/src/tmd5.c b/source/util/src/tmd5.c similarity index 99% rename from src/util/src/tmd5.c rename to source/util/src/tmd5.c index a1fdcc6a0539ad8f7bd804ab039e0e639d20ad3e..9cc4b3b9d5fc91a4795b4c473c9fe326f9c09358 100644 --- a/src/util/src/tmd5.c +++ b/source/util/src/tmd5.c @@ -35,7 +35,6 @@ #include "os.h" #include "tmd5.h" -#include "taosdef.h" /* forward declaration */ static void Transform(uint32_t *buf, uint32_t *in); diff --git a/src/util/src/tmempool.c b/source/util/src/tmempool.c similarity index 99% rename from src/util/src/tmempool.c rename to source/util/src/tmempool.c index 678c965eb1a7315977616778c0e4b39ceb4c7525..a61c4abb146ec7497fca7499ac06773812cdea90 100644 --- a/src/util/src/tmempool.c +++ b/source/util/src/tmempool.c @@ -14,7 +14,7 @@ */ #include "os.h" -#include "tulog.h" +#include "ulog.h" #include "tmempool.h" #include "tutil.h" diff --git a/src/util/src/tnote.c b/source/util/src/tnote.c similarity index 88% rename from src/util/src/tnote.c rename to source/util/src/tnote.c index b691abc5b9f6f828edcc46ec3a5989baa083f443..50b1e0002d9c30f3ccd76406c1f45bb90e817e6b 100644 --- a/src/util/src/tnote.c +++ b/source/util/src/tnote.c @@ -16,7 +16,7 @@ #define _DEFAULT_SOURCE #include "os.h" #include "tutil.h" -#include "tglobal.h" +#include "tdef.h" #include "tnote.h" SNoteObj tsHttpNote; @@ -43,6 +43,7 @@ static void taosInitNote(int32_t numOfLines, int32_t maxNotes, SNoteObj *pNote, void taosInitNotes() { char name[TSDB_FILENAME_LEN * 2] = {0}; +#if 0 if (tsTscEnableRecordSql) { snprintf(name, TSDB_FILENAME_LEN * 2, "%s/tscsql-%d", tsLogDir, taosGetPId()); taosInitNote(tsNumOfLogLines, 1, &tsTscNote, name); @@ -57,13 +58,14 @@ void taosInitNotes() { snprintf(name, TSDB_FILENAME_LEN * 2, "%s/taosinfo", tsLogDir); taosInitNote(tsNumOfLogLines, 1, &tsInfoNote, name); } +#endif } static bool taosLockNote(int32_t fd, SNoteObj *pNote) { if (fd < 0) return false; if (pNote->fileNum > 1) { - int32_t ret = (int32_t)(flock(fd, LOCK_EX | LOCK_NB)); + int32_t ret = (int32_t)taosLockFile(fd); if (ret == 0) { return true; } @@ -76,7 +78,7 @@ static void taosUnLockNote(int32_t fd, SNoteObj *pNote) { if (fd < 0) return; if (pNote->fileNum > 1) { - flock(fd, LOCK_UN | LOCK_NB); + taosUnLockFile(fd); } } @@ -90,15 +92,15 @@ static void *taosThreadToOpenNewNote(void *param) { pNote->lines = 0; sprintf(name, "%s.%d", pNote->name, pNote->flag); - umask(0); + taosUmaskFile(0); - int32_t fd = open(name, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO); + int32_t fd = taosOpenFileCreateWriteTrunc(name); if (fd < 0) { return NULL; } taosLockNote(fd, pNote); - (void)lseek(fd, 0, SEEK_SET); + (void)taosLSeekFile(fd, 0, SEEK_SET); int32_t oldFd = pNote->fd; pNote->fd = fd; @@ -132,7 +134,7 @@ static int32_t taosOpenNewNote(SNoteObj *pNote) { } static bool taosCheckNoteIsOpen(char *noteName, SNoteObj *pNote) { - int32_t fd = open(noteName, O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); + int32_t fd = taosOpenFileCreateWrite(noteName); if (fd < 0) { fprintf(stderr, "failed to open note:%s reason:%s\n", noteName, strerror(errno)); return true; @@ -140,10 +142,10 @@ static bool taosCheckNoteIsOpen(char *noteName, SNoteObj *pNote) { if (taosLockNote(fd, pNote)) { taosUnLockNote(fd, pNote); - close(fd); + taosCloseFile(fd); return false; } else { - close(fd); + taosCloseFile(fd); return true; } } @@ -174,7 +176,7 @@ static void taosGetNoteName(char *fn, SNoteObj *pNote) { static int32_t taosOpenNoteWithMaxLines(char *fn, int32_t maxLines, int32_t maxNoteNum, SNoteObj *pNote) { char name[NOTE_FILE_NAME_LEN * 2] = {0}; int32_t size; - struct stat logstat0, logstat1; + int32_t logstat0_mtime, logstat1_mtime; pNote->maxLines = maxLines; pNote->fileNum = maxNoteNum; @@ -184,13 +186,13 @@ static int32_t taosOpenNoteWithMaxLines(char *fn, int32_t maxLines, int32_t maxN strcpy(name, fn); strcat(name, ".0"); } - bool log0Exist = stat(name, &logstat0) >= 0; + bool log0Exist = taosStatFile(name, NULL, &logstat0_mtime) >= 0; if (strlen(fn) < NOTE_FILE_NAME_LEN + 50 - 2) { strcpy(name, fn); strcat(name, ".1"); } - bool log1Exist = stat(name, &logstat1) >= 0; + bool log1Exist = taosStatFile(name, NULL, &logstat1_mtime) >= 0; if (!log0Exist && !log1Exist) { pNote->flag = 0; @@ -199,15 +201,15 @@ static int32_t taosOpenNoteWithMaxLines(char *fn, int32_t maxLines, int32_t maxN } else if (!log0Exist) { pNote->flag = 1; } else { - pNote->flag = (logstat0.st_mtime > logstat1.st_mtime) ? 0 : 1; + pNote->flag = (logstat0_mtime > logstat1_mtime) ? 0 : 1; } char noteName[NOTE_FILE_NAME_LEN * 2] = {0}; sprintf(noteName, "%s.%d", pNote->name, pNote->flag); pthread_mutex_init(&pNote->mutex, NULL); - umask(0); - pNote->fd = open(noteName, O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); + taosUmaskFile(0); + pNote->fd = taosOpenFileCreateWrite(noteName); if (pNote->fd < 0) { fprintf(stderr, "failed to open note file:%s reason:%s\n", noteName, strerror(errno)); @@ -216,22 +218,22 @@ static int32_t taosOpenNoteWithMaxLines(char *fn, int32_t maxLines, int32_t maxN taosLockNote(pNote->fd, pNote); // only an estimate for number of lines - struct stat filestat; - if (fstat(pNote->fd, &filestat) < 0) { + int64_t filestat_size; + if (taosFStatFile(pNote->fd, &filestat_size, NULL) < 0) { fprintf(stderr, "failed to fstat note file:%s reason:%s\n", noteName, strerror(errno)); return -1; } - size = (int32_t)filestat.st_size; + size = (int32_t)filestat_size; pNote->lines = size / 60; - lseek(pNote->fd, 0, SEEK_END); + taosLSeekFile(pNote->fd, 0, SEEK_END); return 0; } void taosNotePrintBuffer(SNoteObj *pNote, char *buffer, int32_t len) { if (pNote->fd <= 0) return; - taosWrite(pNote->fd, buffer, len); + taosWriteFile(pNote->fd, buffer, len); if (pNote->maxLines > 0) { pNote->lines++; @@ -247,7 +249,7 @@ void taosNotePrint(SNoteObj *pNote, const char *const format, ...) { struct timeval timeSecs; time_t curTime; - gettimeofday(&timeSecs, NULL); + taosGetTimeOfDay(&timeSecs); curTime = timeSecs.tv_sec; ptm = localtime_r(&curTime, &Tm); len = sprintf(buffer, "%02d/%02d %02d:%02d:%02d.%06d %08" PRId64 " ", ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, @@ -269,6 +271,6 @@ void taosNotePrint(SNoteObj *pNote, const char *const format, ...) { static void taosCloseNoteByFd(int32_t fd, SNoteObj *pNote) { if (fd >= 0) { taosUnLockNote(fd, pNote); - close(fd); + taosCloseFile(fd); } } diff --git a/src/util/src/tqueue.c b/source/util/src/tqueue.c similarity index 99% rename from src/util/src/tqueue.c rename to source/util/src/tqueue.c index 6a37f11ecef376e70f4eefbf6446150bd350cf07..da1fb1837f9da1b2ff0f4eb37892ad88356f4865 100644 --- a/src/util/src/tqueue.c +++ b/source/util/src/tqueue.c @@ -14,7 +14,7 @@ */ #include "os.h" -#include "tulog.h" +#include "ulog.h" #include "taoserror.h" #include "tqueue.h" diff --git a/src/util/src/tref.c b/source/util/src/tref.c similarity index 99% rename from src/util/src/tref.c rename to source/util/src/tref.c index 7d64bd1f83fb8d235c825057251a5e76e0b96b2a..68f161bd3dc6c12f121855c6edf04f62a2f285cb 100644 --- a/src/util/src/tref.c +++ b/source/util/src/tref.c @@ -15,7 +15,7 @@ #include "os.h" #include "taoserror.h" -#include "tulog.h" +#include "ulog.h" #include "tutil.h" #define TSDB_REF_OBJECTS 50 diff --git a/src/util/src/tsched.c b/source/util/src/tsched.c similarity index 99% rename from src/util/src/tsched.c rename to source/util/src/tsched.c index b86ebb38bcd6446b56357f9667636403e14d688c..915edc45ceb99d75a36bf31eb93f550ccc98d561 100644 --- a/src/util/src/tsched.c +++ b/source/util/src/tsched.c @@ -14,9 +14,9 @@ */ #include "os.h" -#include "taosdef.h" +#include "tdef.h" #include "tutil.h" -#include "tulog.h" +#include "ulog.h" #include "tsched.h" #include "ttimer.h" diff --git a/src/util/src/tskiplist.c b/source/util/src/tskiplist.c similarity index 99% rename from src/util/src/tskiplist.c rename to source/util/src/tskiplist.c index 98fd9c094cba3e779c9f203fdacc548a3bda5ef4..d02b148863145acda026b3d605c4ca44e643108a 100644 --- a/src/util/src/tskiplist.c +++ b/source/util/src/tskiplist.c @@ -16,8 +16,7 @@ #include "tskiplist.h" #include "os.h" #include "tcompare.h" -#include "tdataformat.h" -#include "tulog.h" +#include "ulog.h" #include "tutil.h" static int initForwardBackwardPtr(SSkipList *pSkipList); diff --git a/source/util/src/tstep.c b/source/util/src/tstep.c new file mode 100644 index 0000000000000000000000000000000000000000..b04135194a235723ab5a1774e492afd01d55645a --- /dev/null +++ b/source/util/src/tstep.c @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "os.h" +#include "ulog.h" +#include "tstep.h" + +typedef struct SStepObj { + char * name; + void ** self; + InitFp initFp; + CleanupFp cleanupFp; +} SStep; + +typedef struct SSteps { + int32_t cursize; + int32_t maxsize; + SStep * steps; + ReportFp reportFp; +} SSteps; + +SSteps *taosStepInit(int32_t maxsize, ReportFp fp) { + SSteps *steps = calloc(1, sizeof(SSteps)); + if (steps == NULL) return NULL; + + steps->maxsize = maxsize; + steps->cursize = 0; + steps->steps = calloc(maxsize, sizeof(SStep)); + steps->reportFp = fp; + + return steps; +} + +int32_t taosStepAdd(struct SSteps *steps, char *name, void **obj, InitFp initFp, CleanupFp cleanupFp) { + if (steps == NULL) return -1; + if (steps->cursize >= steps->maxsize) { + uError("failed to add step since up to the maxsize"); + return -1; + } + + SStep step = {.name = name, .self = obj, .initFp = initFp, .cleanupFp = cleanupFp}; + steps->steps[steps->cursize++] = step; + return 0; +} + +static void taosStepCleanupImp(SSteps *steps, int32_t pos) { + for (int32_t s = pos; s >= 0; s--) { + SStep *step = steps->steps + s; + uDebug("step:%s will cleanup", step->name); + if (step->cleanupFp != NULL) { + (*step->cleanupFp)(step->self); + } + } +} + +int32_t taosStepExec(SSteps *steps) { + if (steps == NULL) return -1; + + for (int32_t s = 0; s < steps->cursize; s++) { + SStep *step = steps->steps + s; + if (step->initFp == NULL) continue; + + if (steps->reportFp != NULL) { + (*steps->reportFp)(step->name, "start initialize"); + } + + int32_t code = (*step->initFp)(step->self); + if (code != 0) { + uDebug("step:%s will cleanup", step->name); + taosStepCleanupImp(steps, s); + return code; + } + + uInfo("step:%s is initialized", step->name); + + if (steps->reportFp != NULL) { + (*steps->reportFp)(step->name, "initialize completed"); + } + } + + return 0; +} + +void taosStepCleanup(SSteps *steps) { + if (steps == NULL) return; + taosStepCleanupImp(steps, steps->cursize - 1); +} \ No newline at end of file diff --git a/src/util/src/tstrbuild.c b/source/util/src/tstrbuild.c similarity index 100% rename from src/util/src/tstrbuild.c rename to source/util/src/tstrbuild.c diff --git a/src/util/src/tthread.c b/source/util/src/tthread.c similarity index 96% rename from src/util/src/tthread.c rename to source/util/src/tthread.c index 043b2de2f241297d209041294428dde2c55e974e..5ed7fb5aa00d1a5c5c3fffae2de46dbea57e011f 100644 --- a/src/util/src/tthread.c +++ b/source/util/src/tthread.c @@ -15,10 +15,9 @@ #include "os.h" #include "tthread.h" -#include "tglobal.h" -#include "taosdef.h" +#include "tdef.h" #include "tutil.h" -#include "tulog.h" +#include "ulog.h" #include "taoserror.h" // create new thread diff --git a/src/util/src/ttimer.c b/source/util/src/ttimer.c similarity index 98% rename from src/util/src/ttimer.c rename to source/util/src/ttimer.c index 865e1159c1995b2796682d64ee06de02442b7a25..4aac7b7abf2e50626158514ac01969130b2fb637 100644 --- a/src/util/src/ttimer.c +++ b/source/util/src/ttimer.c @@ -18,7 +18,6 @@ #include "tsched.h" #include "ttimer.h" #include "tutil.h" -#include "monotonic.h" extern int8_t tscEmbedded; @@ -187,10 +186,6 @@ static void removeTimer(uintptr_t id) { unlockTimerList(list); } -static int64_t getMonotonicMs(void) { - return (int64_t) getMonotonicUs() / 1000; -} - static void addToWheel(tmr_obj_t* timer, uint32_t delay) { timerAddRef(timer); // select a wheel for the timer, we are not an accurate timer, @@ -206,7 +201,7 @@ static void addToWheel(tmr_obj_t* timer, uint32_t delay) { time_wheel_t* wheel = wheels + timer->wheel; timer->prev = NULL; - timer->expireAt = getMonotonicMs() + delay; + timer->expireAt = taosGetMonotonicMs() + delay; pthread_mutex_lock(&wheel->mutex); @@ -339,7 +334,7 @@ tmr_h taosTmrStart(TAOS_TMR_CALLBACK fp, int mseconds, void* param, void* handle } static void taosTimerLoopFunc(int signo) { - int64_t now = getMonotonicMs(); + int64_t now = taosGetMonotonicMs(); for (int i = 0; i < tListLen(wheels); i++) { // `expried` is a temporary expire list. @@ -506,7 +501,7 @@ static void taosTmrModuleInit(void) { pthread_mutex_init(&tmrCtrlMutex, NULL); - int64_t now = getMonotonicMs(); + int64_t now = taosGetMonotonicMs(); for (int i = 0; i < tListLen(wheels); i++) { time_wheel_t* wheel = wheels + i; if (pthread_mutex_init(&wheel->mutex, NULL) != 0) { @@ -537,7 +532,7 @@ static void taosTmrModuleInit(void) { } void* taosTmrInit(int maxNumOfTmrs, int resolution, int longest, const char* label) { - const char* ret = monotonicInit(); + const char* ret = taosMonotonicInit(); tmrDebug("ttimer monotonic clock source:%s", ret); pthread_once(&tmrModuleInit, taosTmrModuleInit); diff --git a/src/util/src/tutil.c b/source/util/src/tutil.c similarity index 99% rename from src/util/src/tutil.c rename to source/util/src/tutil.c index 5f8c92898fc5f0abc4c733c0558befd68ac3cac7..c5027af7c7978d48b602dbe7d977c21138a8aa37 100644 --- a/src/util/src/tutil.c +++ b/source/util/src/tutil.c @@ -15,10 +15,9 @@ #include "os.h" #include "tcrc32c.h" -#include "tglobal.h" -#include "taosdef.h" +#include "tdef.h" #include "tutil.h" -#include "tulog.h" +#include "ulog.h" #include "taoserror.h" int32_t strdequote(char *z) { diff --git a/source/util/src/tworker.c b/source/util/src/tworker.c new file mode 100644 index 0000000000000000000000000000000000000000..5370e78c090b28580a28e04dbb92b67ffaed3cb1 --- /dev/null +++ b/source/util/src/tworker.c @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "os.h" +#include "ulog.h" +#include "tqueue.h" +#include "tworker.h" + +static void *taosWorkerThreadFp(void *wparam) { + SWorker * worker = wparam; + SWorkerPool *pool = worker->pool; + void * msg = NULL; + int32_t qtype = 0; + void * ahandle = NULL; + int32_t code = 0; + + setThreadName(pool->name); + + while (1) { + if (taosReadQitemFromQset(pool->qset, &qtype, (void **)&msg, &ahandle) == 0) { + uDebug("pool:%s, worker:%d qset:%p, got no message and exiting", pool->name, worker->id, pool->qset); + break; + } + + code = (*pool->reqFp)(ahandle, msg); + (*pool->rspFp)(ahandle, msg, qtype, code); + } + + return NULL; +} + +int32_t tWorkerInit(SWorkerPool *pool) { + pool->qset = taosOpenQset(); + pool->workers = calloc(sizeof(SWorker), pool->max); + pthread_mutex_init(&pool->mutex, NULL); + for (int i = 0; i < pool->max; ++i) { + SWorker *pWorker = pool->workers + i; + pWorker->id = i; + pWorker->pool = pool; + } + + uInfo("worker:%s is initialized, min:%d max:%d", pool->name, pool->min, pool->max); + return 0; +} + +void tWorkerCleanup(SWorkerPool *pool) { + for (int i = 0; i < pool->max; ++i) { + SWorker *pWorker = pool->workers + i; + if(taosCheckPthreadValid(pWorker->thread)) { + taosQsetThreadResume(pool->qset); + } + } + + for (int i = 0; i < pool->max; ++i) { + SWorker *pWorker = pool->workers + i; + if (taosCheckPthreadValid(pWorker->thread)) { + pthread_join(pWorker->thread, NULL); + } + } + + free(pool->workers); + taosCloseQset(pool->qset); + pthread_mutex_destroy(&pool->mutex); + + uInfo("worker:%s is closed", pool->name); +} + +void *tWorkerAllocQueue(SWorkerPool *pool, void *ahandle) { + pthread_mutex_lock(&pool->mutex); + taos_queue pQueue = taosOpenQueue(); + if (pQueue == NULL) { + pthread_mutex_unlock(&pool->mutex); + return NULL; + } + + taosAddIntoQset(pool->qset, pQueue, ahandle); + + // spawn a thread to process queue + if (pool->num < pool->max) { + do { + SWorker *pWorker = pool->workers + pool->num; + + pthread_attr_t thAttr; + pthread_attr_init(&thAttr); + pthread_attr_setdetachstate(&thAttr, PTHREAD_CREATE_JOINABLE); + + if (pthread_create(&pWorker->thread, &thAttr, taosWorkerThreadFp, pWorker) != 0) { + uError("workers:%s:%d failed to create thread to process since %s", pool->name, pWorker->id, strerror(errno)); + } + + pthread_attr_destroy(&thAttr); + pool->num++; + uDebug("workers:%s:%d is launched, total:%d", pool->name, pWorker->id, pool->num); + } while (pool->num < pool->min); + } + + pthread_mutex_unlock(&pool->mutex); + uDebug("workers:%s, queue:%p is allocated, ahandle:%p", pool->name, pQueue, ahandle); + + return pQueue; +} + +void tWorkerFreeQueue(SWorkerPool *pool, void *pQueue) { + taosCloseQueue(pQueue); + uDebug("workers:%s, queue:%p is freed", pool->name, pQueue); +} diff --git a/src/util/src/version.c.in b/source/util/src/version.c.in similarity index 100% rename from src/util/src/version.c.in rename to source/util/src/version.c.in diff --git a/src/util/tests/CMakeLists.txt b/source/util/test/CMakeLists.txt similarity index 100% rename from src/util/tests/CMakeLists.txt rename to source/util/test/CMakeLists.txt diff --git a/src/util/tests/arrayTest.cpp b/source/util/test/arrayTest.cpp similarity index 100% rename from src/util/tests/arrayTest.cpp rename to source/util/test/arrayTest.cpp diff --git a/src/util/tests/cacheTest.cpp b/source/util/test/cacheTest.cpp similarity index 100% rename from src/util/tests/cacheTest.cpp rename to source/util/test/cacheTest.cpp diff --git a/src/util/tests/codingTests.cpp b/source/util/test/codingTests.cpp similarity index 100% rename from src/util/tests/codingTests.cpp rename to source/util/test/codingTests.cpp diff --git a/src/util/tests/hashTest.cpp b/source/util/test/hashTest.cpp similarity index 100% rename from src/util/tests/hashTest.cpp rename to source/util/test/hashTest.cpp diff --git a/src/util/tests/skiplistTest.cpp b/source/util/test/skiplistTest.cpp similarity index 100% rename from src/util/tests/skiplistTest.cpp rename to source/util/test/skiplistTest.cpp diff --git a/src/util/tests/stringTest.cpp b/source/util/test/stringTest.cpp similarity index 100% rename from src/util/tests/stringTest.cpp rename to source/util/test/stringTest.cpp diff --git a/src/util/tests/taosbsearchTest.cpp b/source/util/test/taosbsearchTest.cpp similarity index 100% rename from src/util/tests/taosbsearchTest.cpp rename to source/util/test/taosbsearchTest.cpp diff --git a/src/util/tests/trefTest.c b/source/util/test/trefTest.c similarity index 99% rename from src/util/tests/trefTest.c rename to source/util/test/trefTest.c index e01da070afd3333cf02c25b51d2e9711c1616fb0..54aa6a5f346b481e387593fcbd40077ca1a979ec 100644 --- a/src/util/tests/trefTest.c +++ b/source/util/test/trefTest.c @@ -8,7 +8,7 @@ #include "tlog.h" #include "tglobal.h" #include "taoserror.h" -#include "tulog.h" +#include "ulog.h" typedef struct { int refNum; diff --git a/src/client/inc/tsclient.h b/src/client/inc/tsclient.h index ff796cdcbf5cdc7a3d9fbc313226cb9e013b0eda..653c7770ef323f8b8608c6a99ccc734ed871ca6e 100644 --- a/src/client/inc/tsclient.h +++ b/src/client/inc/tsclient.h @@ -22,8 +22,8 @@ extern "C" { #include "os.h" +#include "../../../include/client/taos.h" #include "qAggMain.h" -#include "taos.h" #include "taosdef.h" #include "taosmsg.h" #include "tarray.h" @@ -36,7 +36,7 @@ extern "C" { #include "qSqlparser.h" #include "qTsbuf.h" #include "qUtil.h" -#include "tcmdtype.h" +#include "tmsgtype.h" typedef enum { TAOS_REQ_FROM_SHELL, diff --git a/src/client/src/TSDBJNIConnector.c b/src/client/src/TSDBJNIConnector.c index 506c8d64b9f4213713656ecd08612a103e0b1b2d..0d22b44f5af7ccf1aa1ba1cc624385d480f5bd2a 100644 --- a/src/client/src/TSDBJNIConnector.c +++ b/src/client/src/TSDBJNIConnector.c @@ -13,8 +13,8 @@ * along with this program. If not, see . */ +#include "../../../include/client/taos.h" #include "os.h" -#include "taos.h" #include "tlog.h" #include "tscUtil.h" diff --git a/src/client/src/tscLocal.c b/src/client/src/tscLocal.c index 07db18b498873f4a023d8ea76aadd7e76a4cd8d2..0b4f399a1f4a06d588fe0e92c1d311e325d4404b 100644 --- a/src/client/src/tscLocal.c +++ b/src/client/src/tscLocal.c @@ -16,14 +16,14 @@ #include "os.h" #include "taosmsg.h" +#include "../../../include/client/taos.h" +#include "qTableMeta.h" #include "taosdef.h" #include "tname.h" #include "tscLog.h" +#include "tscSubquery.h" #include "tscUtil.h" -#include "qTableMeta.h" #include "tsclient.h" -#include "taos.h" -#include "tscSubquery.h" #define STR_NOCASE_EQUAL(str1, len1, str2, len2) ((len1 == len2) && 0 == strncasecmp(str1, str2, len1)) diff --git a/src/client/src/tscParseLineProtocol.c b/src/client/src/tscParseLineProtocol.c index e26e439492cec9c83b624c2bbb2bbc3a95de97b0..20d52fbd3ef2cee53e231f91ae32dfc4fb360c35 100644 --- a/src/client/src/tscParseLineProtocol.c +++ b/src/client/src/tscParseLineProtocol.c @@ -16,7 +16,7 @@ #include "tsclient.h" #include "tscLog.h" -#include "taos.h" +#include "../../../include/client/taos.h" #include "tscParseLine.h" typedef struct { diff --git a/src/client/src/tscParseOpenTSDB.c b/src/client/src/tscParseOpenTSDB.c index 12f8ec84fd301d2d8851e92826cfcb3d860c1ceb..1086f91bfdc507e6240faf5f5cfd802b17c17aa9 100644 --- a/src/client/src/tscParseOpenTSDB.c +++ b/src/client/src/tscParseOpenTSDB.c @@ -3,9 +3,9 @@ #include #include +#include "../../../include/client/taos.h" #include "cJSON.h" #include "hash.h" -#include "taos.h" #include "tscUtil.h" #include "tsclient.h" diff --git a/src/client/src/tscPrepare.c b/src/client/src/tscPrepare.c index d0ac0ccf4ee4bfa381a78090409a761717ceb4b0..17742d71e1e31f2f04e6ed10ae1b77cba1561c5d 100644 --- a/src/client/src/tscPrepare.c +++ b/src/client/src/tscPrepare.c @@ -14,14 +14,14 @@ */ #include "os.h" -#include "taos.h" -#include "tsclient.h" -#include "tscUtil.h" -#include "ttimer.h" +#include "../../../include/client/taos.h" #include "taosmsg.h" -#include "tstrbuild.h" #include "tscLog.h" #include "tscSubquery.h" +#include "tscUtil.h" +#include "tsclient.h" +#include "tstrbuild.h" +#include "ttimer.h" int tsParseInsertSql(SSqlObj *pSql); int32_t tsCheckTimestamp(STableDataBlocks *pDataBlocks, const char *start); diff --git a/src/client/src/tscProfile.c b/src/client/src/tscProfile.c index b00138b4c46943933145241b3ca9e7ef47c4fcfe..a9f65b84ce6d60f423d2958c9ff3ecbbee631187 100644 --- a/src/client/src/tscProfile.c +++ b/src/client/src/tscProfile.c @@ -21,7 +21,7 @@ #include "taosmsg.h" #include "tcq.h" -#include "taos.h" +#include "../../../include/client/taos.h" #include "tscUtil.h" void tscSaveSlowQueryFp(void *handle, void *tmrId); diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 9746f79d9ae51845c9b997929916b6f83a0cc140..5b7ab5db65a3c0171bbccc0e5461a41abadc3f6f 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -21,13 +21,15 @@ #endif // __APPLE__ #include +#include "../../../include/client/taos.h" #include "os.h" -#include "regex.h" +#include "qFilter.h" #include "qPlan.h" +#include "qScript.h" #include "qSqlparser.h" #include "qTableMeta.h" #include "qUtil.h" -#include "taos.h" +#include "regex.h" #include "taosmsg.h" #include "tcompare.h" #include "texpr.h" @@ -38,9 +40,7 @@ #include "tstrbuild.h" #include "ttoken.h" #include "ttokendef.h" -#include "qScript.h" #include "ttype.h" -#include "qFilter.h" #define DEFAULT_PRIMARY_TIMESTAMP_COL_NAME "_c0" @@ -2232,6 +2232,28 @@ static int32_t doAddProjectionExprAndResultFields(SQueryInfo* pQueryInfo, SColum return numOfTotalColumns; } +SSchema tGetUserSpecifiedColumnSchema(SVariant* pVal, SStrToken* exprStr, const char* name) { + SSchema s = {0}; + + s.type = pVal->nType; + if (s.type == TSDB_DATA_TYPE_BINARY || s.type == TSDB_DATA_TYPE_NCHAR) { + s.bytes = (int16_t)(pVal->nLen + VARSTR_HEADER_SIZE); + } else { + s.bytes = tDataTypes[pVal->nType].bytes; + } + + s.colId = TSDB_UD_COLUMN_INDEX; + if (name != NULL) { + tstrncpy(s.name, name, sizeof(s.name)); + } else { + size_t tlen = MIN(sizeof(s.name), exprStr->n + 1); + tstrncpy(s.name, exprStr->z, tlen); + strdequote(s.name); + } + + return s; +} + int32_t addProjectionExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExprItem* pItem, bool outerQuery) { const char* msg1 = "tag for normal table query is not allowed"; const char* msg2 = "invalid column name"; diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index b87ec92ff1f056fdc5eeb8992cec418d07158b0b..308898950ab570ed694b43674b9f6db756066f84 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -13,12 +13,12 @@ * along with this program. If not, see . */ -#include +#include #include "os.h" #include "qPlan.h" #include "qTableMeta.h" -#include "tcmdtype.h" #include "tlockfree.h" +#include "tmsgtype.h" #include "trpc.h" #include "tscGlobalmerge.h" #include "tscLog.h" diff --git a/src/client/src/tscSub.c b/src/client/src/tscSub.c index 52ba424fa5adcd43ac5b624b7f486c06df71f2c4..a0af98a9d2568b09d2abb4509a0316df7f8a533a 100644 --- a/src/client/src/tscSub.c +++ b/src/client/src/tscSub.c @@ -13,17 +13,17 @@ * along with this program. If not, see . */ +#include "../../../include/client/taos.h" #include "os.h" -#include "taos.h" +#include "tcache.h" #include "trpc.h" +#include "tscLog.h" +#include "tscProfile.h" +#include "tscUtil.h" #include "tsclient.h" #include "tsocket.h" #include "ttimer.h" #include "tutil.h" -#include "tscLog.h" -#include "tscUtil.h" -#include "tcache.h" -#include "tscProfile.h" typedef struct SSubscriptionProgress { int64_t uid; @@ -371,7 +371,7 @@ void tscSaveSubscriptionProgress(void* sub) { char path[256]; sprintf(path, "%s/subscribe", tsDataDir); - if (taosMkDir(path, 0777) != 0) { + if (!taosMkDir(path)) { tscError("failed to create subscribe dir: %s", path); } diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt deleted file mode 100644 index 57d4890f49ecc58b28cfaabddc14dc1c3f4e0c06..0000000000000000000000000000000000000000 --- a/src/common/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -aux_source_directory(source COMMON_SRC) -add_library(common ${COMMON_SRC}) -target_include_directories( - common - PUBLIC "${CMAKE_SOURCE_DIR}/include/common" - PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include" -) -target_include_directories( - common - PRIVATE util -) \ No newline at end of file diff --git a/src/common/inc/tname.h b/src/common/inc/tname.h deleted file mode 100644 index b29a535ec2c80f7fb058e3d1c55e5d16ed71c3c4..0000000000000000000000000000000000000000 --- a/src/common/inc/tname.h +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_NAME_H -#define TDENGINE_NAME_H - -#include "os.h" -#include "taosmsg.h" -#include "ttoken.h" -#include "tvariant.h" - -typedef struct SDataStatis { - int16_t colId; - int64_t sum; - int64_t max; - int64_t min; - int16_t maxIndex; - int16_t minIndex; - int16_t numOfNull; -} SDataStatis; - -typedef struct SColumnInfoData { - SColumnInfo info; - char* pData; // the corresponding block data in memory -} SColumnInfoData; - -typedef struct SResPair { - TSKEY key; - double avg; -} SResPair; - -// the structure for sql function in select clause -typedef struct SSqlExpr { - char aliasName[TSDB_COL_NAME_LEN]; // as aliasName - char token[TSDB_COL_NAME_LEN]; // original token - SColIndex colInfo; - uint64_t uid; // table uid, todo refactor use the pointer - - int16_t functionId; // function id in aAgg array - - int16_t resType; // return value type - int16_t resBytes; // length of return value - int32_t interBytes; // inter result buffer size - - int16_t colType; // table column type - int16_t colBytes; // table column bytes - - int16_t numOfParams; // argument value of each function - tVariant param[3]; // parameters are not more than 3 - int32_t offset; // sub result column value of arithmetic expression. - int16_t resColId; // result column id - - SColumnFilterList flist; -} SSqlExpr; - -typedef struct SExprInfo { - SSqlExpr base; - struct tExprNode *pExpr; -} SExprInfo; - -#define TSDB_DB_NAME_T 1 -#define TSDB_TABLE_NAME_T 2 - -#define T_NAME_ACCT 0x1u -#define T_NAME_DB 0x2u -#define T_NAME_TABLE 0x4u - -typedef struct SName { - uint8_t type; //db_name_t, table_name_t - char acctId[TSDB_ACCT_ID_LEN]; - char dbname[TSDB_DB_NAME_LEN]; - char tname[TSDB_TABLE_NAME_LEN]; -} SName; - -void extractTableName(const char *tableId, char *name); - -char* extractDBName(const char *tableId, char *name); - -size_t tableIdPrefix(const char* name, char* prefix, int32_t len); - -void extractTableNameFromToken(SStrToken *pToken, SStrToken* pTable); - -SSchema tGetUserSpecifiedColumnSchema(tVariant* pVal, SStrToken* exprStr, const char* name); - -bool tscValidateTableNameLength(size_t len); - -SColumnFilterInfo* tFilterInfoDup(const SColumnFilterInfo* src, int32_t numOfFilters); - -SSchema* tGetTbnameColumnSchema(); - -/** - * check if the schema is valid or not, including following aspects: - * 1. number of columns - * 2. column types - * 3. column length - * 4. column names - * 5. total length - * - * @param pSchema - * @param numOfCols - * @return - */ -bool tIsValidSchema(struct SSchema* pSchema, int32_t numOfCols, int32_t numOfTags); - -int32_t tNameExtractFullName(const SName* name, char* dst); -int32_t tNameLen(const SName* name); - -SName* tNameDup(const SName* name); - -bool tIsValidName(const SName* name); - -const char* tNameGetTableName(const SName* name); - -int32_t tNameGetDbName(const SName* name, char* dst); -int32_t tNameGetFullDbName(const SName* name, char* dst); - -bool tNameIsEmpty(const SName* name); - -void tNameAssign(SName* dst, const SName* src); - -int32_t tNameFromString(SName* dst, const char* str, uint32_t type); - -int32_t tNameSetAcctId(SName* dst, const char* acct); - -int32_t tNameSetDbName(SName* dst, const char* acct, SStrToken* dbToken); - -#endif // TDENGINE_NAME_H diff --git a/src/common/source/common.c b/src/common/source/common.c deleted file mode 100644 index 6dea4a4e57392be988126c579648f39a8270b9bf..0000000000000000000000000000000000000000 --- a/src/common/source/common.c +++ /dev/null @@ -1,14 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ \ No newline at end of file diff --git a/src/connector/odbc/examples/c/main.c b/src/connector/odbc/examples/c/main.c index de01d2b85eda0a6851ceede8b1932ce6d9231595..45edf6be5c5856f0541c835f7d814f840e2fa53d 100644 --- a/src/connector/odbc/examples/c/main.c +++ b/src/connector/odbc/examples/c/main.c @@ -9,7 +9,7 @@ #include #include -#include "taos.h" +#include "../../../../../include/client/taos.h" #include "taoserror.h" #include diff --git a/src/connector/odbc/src/base.h b/src/connector/odbc/src/base.h index 1ec9827075441358969b2471bcc15f5ca60fd0fb..2a61e20e7992eafe33467c3ffb29286dcab76965 100644 --- a/src/connector/odbc/src/base.h +++ b/src/connector/odbc/src/base.h @@ -20,7 +20,7 @@ #include "todbc_iconv.h" #include "todbc_log.h" -#include "taos.h" +#include "../../../../include/client/taos.h" #include "taoserror.h" #include diff --git a/src/connector/odbc/src/todbc_tls.h b/src/connector/odbc/src/todbc_tls.h index e636f6ae6c9c9051fe4a77d5e9d1e082e2dcfdb0..eb72a80eaedfa5ab6f5a9306ee06c022be18b85c 100644 --- a/src/connector/odbc/src/todbc_tls.h +++ b/src/connector/odbc/src/todbc_tls.h @@ -18,7 +18,7 @@ // !!! functions exported in this header file are all non-thread-safe !!! -#include "taos.h" +#include "../../../../include/client/taos.h" #include "todbc_buf.h" #include "todbc_iconv.h" diff --git a/src/cq/src/cqMain.c b/src/cq/src/cqMain.c index aac5a1c665c1417069b3978d10c7b1406a6b02a4..d2558e4650a45b1e831deaca144bf99e7a9af40d 100644 --- a/src/cq/src/cqMain.c +++ b/src/cq/src/cqMain.c @@ -20,15 +20,15 @@ #include #include -#include "taos.h" -#include "tsclient.h" +#include "../../../include/client/taos.h" #include "taosdef.h" #include "taosmsg.h" -#include "ttimer.h" #include "tcq.h" #include "tdataformat.h" #include "tglobal.h" #include "tlog.h" +#include "tsclient.h" +#include "ttimer.h" #include "twal.h" #define cFatal(...) { if (cqDebugFlag & DEBUG_FATAL) { taosPrintLog("CQ FATAL ", 255, __VA_ARGS__); }} diff --git a/src/dnode/CMakeLists.txt b/src/dnode/CMakeLists.txt deleted file mode 100644 index 1141d2e452164e2c9e2a824ad28f282bc2fba637..0000000000000000000000000000000000000000 --- a/src/dnode/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -aux_source_directory(source DNODE_SRC) -add_executable(taosd ${DNODE_SRC}) -target_link_libraries( - taosd - PRIVATE os -) \ No newline at end of file diff --git a/src/dnode/CMakeLists.txt.in b/src/dnode/CMakeLists.txt.in deleted file mode 100644 index 47186130ead0d1ee3f4593b7ef346f8cc47f7cba..0000000000000000000000000000000000000000 --- a/src/dnode/CMakeLists.txt.in +++ /dev/null @@ -1,63 +0,0 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.8...3.20) -PROJECT(TDengine) - -INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/query/inc) -INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/mnode/inc) -INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/tsdb/inc) -INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/sync/inc) -INCLUDE_DIRECTORIES(${TD_ENTERPRISE_DIR}/src/inc) - -INCLUDE_DIRECTORIES(inc) -AUX_SOURCE_DIRECTORY(src SRC) - -IF (TD_LINUX_64 AND JEMALLOC_ENABLED) - ADD_DEFINITIONS(-DTD_JEMALLOC_ENABLED -I${CMAKE_BINARY_DIR}/build/include -L${CMAKE_BINARY_DIR}/build/lib -Wl,-rpath,${CMAKE_BINARY_DIR}/build/lib -ljemalloc) - SET(LINK_JEMALLOC "-L${CMAKE_BINARY_DIR}/build/lib -ljemalloc") -ELSE () - SET(LINK_JEMALLOC "") -ENDIF () - -ADD_EXECUTABLE(taosd ${SRC}) -TARGET_LINK_LIBRARIES(taosd mnode monitor http tsdb twal vnode cJson lua lz4 balance sync ${LINK_JEMALLOC}) - -IF (TD_SOMODE_STATIC) - TARGET_LINK_LIBRARIES(taosd taos_static) -ELSE () - TARGET_LINK_LIBRARIES(taosd taos) -ENDIF () - -IF (TD_ACCOUNT) - TARGET_LINK_LIBRARIES(taosd account) -ENDIF () - -IF (TD_GRANT) - TARGET_LINK_LIBRARIES(taosd grant) -ENDIF () - -IF (TD_MQTT) - TARGET_LINK_LIBRARIES(taosd mqtt) -ENDIF () - -IF (TD_TOPIC) - TARGET_LINK_LIBRARIES(taosd topic) -ENDIF () - -IF (TD_MODULE AND TD_LINUX) - TARGET_LINK_LIBRARIES(taosd module dl) -ENDIF () - -SET(PREPARE_ENV_CMD "prepare_env_cmd") -SET(PREPARE_ENV_TARGET "prepare_env_target") -ADD_CUSTOM_COMMAND(OUTPUT ${PREPARE_ENV_CMD} - POST_BUILD - COMMAND echo "make test directory" - DEPENDS taosd - COMMAND ${CMAKE_COMMAND} -E make_directory ${TD_TESTS_OUTPUT_DIR}/cfg/ - COMMAND ${CMAKE_COMMAND} -E make_directory ${TD_TESTS_OUTPUT_DIR}/log/ - COMMAND ${CMAKE_COMMAND} -E make_directory ${TD_TESTS_OUTPUT_DIR}/data/ - COMMAND ${CMAKE_COMMAND} -E echo dataDir ${TD_TESTS_OUTPUT_DIR}/data > ${TD_TESTS_OUTPUT_DIR}/cfg/taos.cfg - COMMAND ${CMAKE_COMMAND} -E echo logDir ${TD_TESTS_OUTPUT_DIR}/log >> ${TD_TESTS_OUTPUT_DIR}/cfg/taos.cfg - COMMAND ${CMAKE_COMMAND} -E echo charset UTF-8 >> ${TD_TESTS_OUTPUT_DIR}/cfg/taos.cfg - COMMAND ${CMAKE_COMMAND} -E echo monitor 0 >> ${TD_TESTS_OUTPUT_DIR}/cfg/taos.cfg - COMMENT "prepare taosd environment") -ADD_CUSTOM_TARGET(${PREPARE_ENV_TARGET} ALL WORKING_DIRECTORY ${TD_EXECUTABLE_OUTPUT_PATH} DEPENDS ${PREPARE_ENV_CMD}) diff --git a/src/dnode/inc/dnodeMPeer.h b/src/dnode/inc/dnodeMPeer.h deleted file mode 100644 index b7e566d7e4952d6afbd88e64b36197de4f0f4401..0000000000000000000000000000000000000000 --- a/src/dnode/inc/dnodeMPeer.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_DNODE_MPEER_H -#define TDENGINE_DNODE_MPEER_H - -#ifdef __cplusplus -extern "C" { -#endif -#include "dnodeInt.h" - -int32_t dnodeInitMPeer(); -void dnodeCleanupMPeer(); -int32_t dnodeAllocateMPeerQueue(); -void dnodeFreeMPeerQueue(); -void dnodeDispatchToMPeerQueue(SRpcMsg *pMsg); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/dnode/inc/dnodeMRead.h b/src/dnode/inc/dnodeMRead.h deleted file mode 100644 index 279098d30e137bc6b57ef09ce93b8d12fd421082..0000000000000000000000000000000000000000 --- a/src/dnode/inc/dnodeMRead.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_DNODE_MREAD_H -#define TDENGINE_DNODE_MREAD_H - -#ifdef __cplusplus -extern "C" { -#endif -#include "dnodeInt.h" - -int32_t dnodeInitMRead(); -void dnodeCleanupMRead(); -int32_t dnodeAllocMReadQueue(); -void dnodeFreeMReadQueue(); -void dnodeDispatchToMReadQueue(SRpcMsg *rpcMsg); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/dnode/inc/dnodeMWrite.h b/src/dnode/inc/dnodeMWrite.h deleted file mode 100644 index 8d4fcce3befbad35b47b4817c02662adc37addf8..0000000000000000000000000000000000000000 --- a/src/dnode/inc/dnodeMWrite.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_DNODE_MWRITE_H -#define TDENGINE_DNODE_MWRITE_H - -#ifdef __cplusplus -extern "C" { -#endif -#include "dnodeInt.h" - -int32_t dnodeInitMWrite(); -void dnodeCleanupMWrite(); -int32_t dnodeAllocMWritequeue(); -void dnodeFreeMWritequeue(); -void dnodeDispatchToMWriteQueue(SRpcMsg *pMsg); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/dnode/inc/dnodeModule.h b/src/dnode/inc/dnodeModule.h deleted file mode 100644 index 863ea433c46c175ee7cb6ad46e6dd38f164d36b1..0000000000000000000000000000000000000000 --- a/src/dnode/inc/dnodeModule.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_DNODE_MODULE_H -#define TDENGINE_DNODE_MODULE_H - -#ifdef __cplusplus -extern "C" { -#endif -#include "dnodeInt.h" - -int32_t dnodeInitModules(); -void dnodeCleanupModules(); -int32_t dnodeStartMnode(SMInfos *pMinfos); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/dnode/inc/dnodePeer.h b/src/dnode/inc/dnodePeer.h deleted file mode 100644 index 6d337ef6dc65d717c382ebaf59a50cf334686a64..0000000000000000000000000000000000000000 --- a/src/dnode/inc/dnodePeer.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_DNODE_PEER_H -#define TDENGINE_DNODE_PEER_H - -#ifdef __cplusplus -extern "C" { -#endif -#include "dnodeInt.h" - -int32_t dnodeInitServer(); -void dnodeCleanupServer(); -int32_t dnodeInitClient(); -void dnodeCleanupClient(); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/dnode/inc/dnodeShell.h b/src/dnode/inc/dnodeShell.h deleted file mode 100644 index 3fa66d6a3bc24d253ede6076f9b6bdacdb14db7d..0000000000000000000000000000000000000000 --- a/src/dnode/inc/dnodeShell.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_DNODE_SHELL_H -#define TDENGINE_DNODE_SHELL_H - -#ifdef __cplusplus -extern "C" { -#endif -#include "dnodeInt.h" - -int32_t dnodeInitShell(); -void dnodeCleanupShell(); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/dnode/inc/dnodeVMgmt.h b/src/dnode/inc/dnodeVMgmt.h deleted file mode 100644 index 821196defc9b827961ec4ae8140be82186192f62..0000000000000000000000000000000000000000 --- a/src/dnode/inc/dnodeVMgmt.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_DNODE_VMGMT_H -#define TDENGINE_DNODE_VMGMT_H - -#ifdef __cplusplus -extern "C" { -#endif -#include "dnodeInt.h" - -int32_t dnodeInitVMgmt(); -void dnodeCleanupVMgmt(); -void dnodeDispatchToVMgmtQueue(SRpcMsg *rpcMsg); - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file diff --git a/src/dnode/inc/dnodeVRead.h b/src/dnode/inc/dnodeVRead.h deleted file mode 100644 index 9c88886f88bedaa63a5071b9dd2d773d4ff1cc0c..0000000000000000000000000000000000000000 --- a/src/dnode/inc/dnodeVRead.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_DNODE_VREAD_H -#define TDENGINE_DNODE_VREAD_H - -#ifdef __cplusplus -extern "C" { -#endif -#include "dnodeInt.h" - -int32_t dnodeInitVRead(); -void dnodeCleanupVRead(); -void dnodeDispatchToVReadQueue(SRpcMsg *pMsg); -void * dnodeAllocVQueryQueue(void *pVnode); -void * dnodeAllocVFetchQueue(void *pVnode); -void dnodeFreeVQueryQueue(void *pQqueue); -void dnodeFreeVFetchQueue(void *pFqueue); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/dnode/inc/dnodeVWrite.h b/src/dnode/inc/dnodeVWrite.h deleted file mode 100644 index 2ddff210f8620cfe0544cb5925f633cab8d496af..0000000000000000000000000000000000000000 --- a/src/dnode/inc/dnodeVWrite.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_DNODE_VWRITE_H -#define TDENGINE_DNODE_VWRITE_H - -#ifdef __cplusplus -extern "C" { -#endif -#include "dnodeInt.h" - -int32_t dnodeInitVWrite(); -void dnodeCleanupVWrite(); -void dnodeDispatchToVWriteQueue(SRpcMsg *pMsg); -void * dnodeAllocVWriteQueue(void *pVnode); -void dnodeFreeVWriteQueue(void *pWqueue); -void dnodeSendRpcVWriteRsp(void *pVnode, void *pWrite, int32_t code); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/dnode/inc/dnodeVnodes.h b/src/dnode/inc/dnodeVnodes.h deleted file mode 100644 index e60dd290ce478f214c26410194f345fd4840dfb8..0000000000000000000000000000000000000000 --- a/src/dnode/inc/dnodeVnodes.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_DNODE_VNODES_H -#define TDENGINE_DNODE_VNODES_H - -#ifdef __cplusplus -extern "C" { -#endif -#include "dnodeInt.h" - -int32_t dnodeInitVnodes(); -void dnodeCleanupVnodes(); -int32_t dnodeInitStatusTimer(); -void dnodeCleanupStatusTimer(); -void dnodeSendStatusMsgToMnode(); - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file diff --git a/src/dnode/src/dnodeCfg.c b/src/dnode/src/dnodeCfg.c deleted file mode 100644 index 4269c77bf33eb9aa5fc0edafde39f9b4772d53d9..0000000000000000000000000000000000000000 --- a/src/dnode/src/dnodeCfg.c +++ /dev/null @@ -1,190 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "cJSON.h" -#include "dnodeCfg.h" -#include "tglobal.h" - -static SDnodeCfg tsCfg = {0}; -static pthread_mutex_t tsCfgMutex; -static int32_t tsDnodeDropped; - -static int32_t dnodeReadCfg(); -static int32_t dnodeWriteCfg(); -static void dnodeResetCfg(SDnodeCfg *cfg); -static void dnodePrintCfg(SDnodeCfg *cfg); - -int32_t dnodeInitCfg() { - pthread_mutex_init(&tsCfgMutex, NULL); - dnodeResetCfg(NULL); - int32_t ret = dnodeReadCfg(); - if (ret == 0) { - dInfo("dnode cfg is initialized"); - } - if (tsDnodeDropped) { - dInfo("dnode is dropped, exiting"); - return -1; - } - return ret; -} - -void dnodeCleanupCfg() { pthread_mutex_destroy(&tsCfgMutex); } - -void dnodeUpdateCfg(SDnodeCfg *cfg) { - if (tsCfg.dnodeId != 0) return; - dnodeResetCfg(cfg); -} - -void dnodeSetDropped() { - pthread_mutex_lock(&tsCfgMutex); - tsDnodeDropped = 1; - - dnodeWriteCfg(); - pthread_mutex_unlock(&tsCfgMutex); -} - -int32_t dnodeGetDnodeId() { - int32_t dnodeId = 0; - pthread_mutex_lock(&tsCfgMutex); - dnodeId = tsCfg.dnodeId; - pthread_mutex_unlock(&tsCfgMutex); - return dnodeId; -} - -void dnodeGetClusterId(char *clusterId) { - pthread_mutex_lock(&tsCfgMutex); - tstrncpy(clusterId, tsCfg.clusterId, TSDB_CLUSTER_ID_LEN); - pthread_mutex_unlock(&tsCfgMutex); -} - -void dnodeGetCfg(int32_t *dnodeId, char *clusterId) { - pthread_mutex_lock(&tsCfgMutex); - *dnodeId = tsCfg.dnodeId; - tstrncpy(clusterId, tsCfg.clusterId, TSDB_CLUSTER_ID_LEN); - pthread_mutex_unlock(&tsCfgMutex); -} - -static void dnodeResetCfg(SDnodeCfg *cfg) { - if (cfg == NULL) return; - if (cfg->dnodeId == 0) return; - - pthread_mutex_lock(&tsCfgMutex); - tsCfg.dnodeId = cfg->dnodeId; - tsDnodeId = cfg->dnodeId; - tstrncpy(tsCfg.clusterId, cfg->clusterId, TSDB_CLUSTER_ID_LEN); - dnodePrintCfg(cfg); - dnodeWriteCfg(); - pthread_mutex_unlock(&tsCfgMutex); -} - -static void dnodePrintCfg(SDnodeCfg *cfg) { - dInfo("dnodeId is set to %d, clusterId is set to %s", cfg->dnodeId, cfg->clusterId); -} - -static int32_t dnodeReadCfg() { - int32_t len = 0; - int32_t maxLen = 200; - char * content = calloc(1, maxLen + 1); - cJSON * root = NULL; - FILE * fp = NULL; - SDnodeCfg cfg = {0}; - - char file[TSDB_FILENAME_LEN + 20] = {0}; - sprintf(file, "%s/dnodeCfg.json", tsDnodeDir); - - fp = fopen(file, "r"); - if (!fp) { - dDebug("failed to read %s, file not exist", file); - goto PARSE_CFG_OVER; - } - - len = (int32_t)fread(content, 1, maxLen, fp); - if (len <= 0) { - dError("failed to read %s, content is null", file); - goto PARSE_CFG_OVER; - } - - content[len] = 0; - root = cJSON_Parse(content); - if (root == NULL) { - dError("failed to read %s, invalid json format", file); - goto PARSE_CFG_OVER; - } - - cJSON *dnodeId = cJSON_GetObjectItem(root, "dnodeId"); - if (!dnodeId || dnodeId->type != cJSON_Number) { - dError("failed to read %s, dnodeId not found", file); - goto PARSE_CFG_OVER; - } - cfg.dnodeId = (int32_t)dnodeId->valueint; - - cJSON *dnodeDropped = cJSON_GetObjectItem(root, "dnodeDropped"); - if (!dnodeDropped || dnodeDropped->type != cJSON_Number) { - dError("failed to read %s, dnodeDropped not found", file); - //goto PARSE_CFG_OVER; - } else { - tsDnodeDropped = (int32_t)dnodeDropped->valueint; - } - - cJSON *clusterId = cJSON_GetObjectItem(root, "clusterId"); - if (!clusterId || clusterId->type != cJSON_String) { - dError("failed to read %s, clusterId not found", file); - goto PARSE_CFG_OVER; - } - tstrncpy(cfg.clusterId, clusterId->valuestring, TSDB_CLUSTER_ID_LEN); - - dInfo("read file %s successed", file); - -PARSE_CFG_OVER: - if (content != NULL) free(content); - if (root != NULL) cJSON_Delete(root); - if (fp != NULL) fclose(fp); - terrno = 0; - - dnodeResetCfg(&cfg); - return 0; -} - -static int32_t dnodeWriteCfg() { - char file[TSDB_FILENAME_LEN + 20] = {0}; - sprintf(file, "%s/dnodeCfg.json", tsDnodeDir); - - FILE *fp = fopen(file, "w"); - if (!fp) { - dError("failed to write %s, reason:%s", file, strerror(errno)); - return -1; - } - - int32_t len = 0; - int32_t maxLen = 200; - char * content = calloc(1, maxLen + 1); - - len += snprintf(content + len, maxLen - len, "{\n"); - len += snprintf(content + len, maxLen - len, " \"dnodeId\": %d,\n", tsCfg.dnodeId); - len += snprintf(content + len, maxLen - len, " \"dnodeDropped\": %d,\n", tsDnodeDropped); - len += snprintf(content + len, maxLen - len, " \"clusterId\": \"%s\"\n", tsCfg.clusterId); - len += snprintf(content + len, maxLen - len, "}\n"); - - fwrite(content, 1, len, fp); - taosFsync(fileno(fp)); - fclose(fp); - free(content); - terrno = 0; - - dInfo("successed to write %s", file); - return 0; -} diff --git a/src/dnode/src/dnodeCheck.c b/src/dnode/src/dnodeCheck.c deleted file mode 100644 index 87baff30673afc68eb23a00bef279433a422ba67..0000000000000000000000000000000000000000 --- a/src/dnode/src/dnodeCheck.c +++ /dev/null @@ -1,265 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "dnodeCheck.h" - -typedef struct { - bool enable; - char * name; - int32_t (*initFp)(); - int32_t (*startFp)(); - void (*cleanUpFp)(); - void (*stopFp)(); -} SCheckItem; - -static SCheckItem tsCheckItem[TSDB_CHECK_ITEM_MAX] = {{0}}; -int64_t tsMinFreeMemSizeForStart = 0; - -static int32_t bindTcpPort(uint16_t port) { - SOCKET serverSocket; - struct sockaddr_in server_addr; - - if ((serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { - dError("socket() fail: %s", strerror(errno)); - return -1; - } - - bzero(&server_addr, sizeof(server_addr)); - server_addr.sin_family = AF_INET; - server_addr.sin_port = htons(port); - server_addr.sin_addr.s_addr = htonl(INADDR_ANY); - - if (bind(serverSocket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { - dError("port:%d tcp bind() fail: %s", port, strerror(errno)); - taosCloseSocket(serverSocket); - return -1; - } - - if (listen(serverSocket, 5) < 0) { - dError("port:%d listen() fail: %s", port, strerror(errno)); - taosCloseSocket(serverSocket); - return -1; - } - - taosCloseSocket(serverSocket); - return 0; -} - -static int32_t bindUdpPort(int16_t port) { - SOCKET serverSocket; - struct sockaddr_in server_addr; - - if ((serverSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { - dError("socket() fail: %s", strerror(errno)); - return -1; - } - - bzero(&server_addr, sizeof(server_addr)); - server_addr.sin_family = AF_INET; - server_addr.sin_port = htons(port); - server_addr.sin_addr.s_addr = htonl(INADDR_ANY); - - if (bind(serverSocket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { - dError("port:%d udp bind() fail: %s", port, strerror(errno)); - taosCloseSocket(serverSocket); - return -1; - } - - taosCloseSocket(serverSocket); - return 0; -} - -static int32_t dnodeCheckNetwork() { - int32_t ret; - uint16_t startPort = tsServerPort; - - for (uint16_t port = startPort; port < startPort + 12; port++) { - ret = bindTcpPort(port); - if (0 != ret) { - dError("failed to tcp bind port %d, quit", port); - return -1; - } - ret = bindUdpPort(port); - if (0 != ret) { - dError("failed to udp bind port %d, quit", port); - return -1; - } - } - - return 0; -} - -static int32_t dnodeCheckMem() { - float memoryUsedMB; - float memoryAvailMB; - if (true != taosGetSysMemory(&memoryUsedMB)) { - dError("failed to get system mem infomation, errno:%u, reason:%s", errno, strerror(errno)); - return -1; - } - - memoryAvailMB = (float)tsTotalMemoryMB - memoryUsedMB; - - if (memoryAvailMB < tsMinFreeMemSizeForStart) { - dError("free mem %f too little, quit", memoryAvailMB); - return -1; - } - - return 0; -} - -static int32_t dnodeCheckCpu() { - // TODO: - return 0; -} - -static int32_t dnodeCheckDisk() { - taosGetDisk(); - - if (tsAvailDataDirGB < tsMinimalDataDirGB) { - dError("free disk size: %f GB, too little, quit", tsAvailDataDirGB); - return -1; - } - - if (tsAvailLogDirGB < tsMinimalLogDirGB) { - dError("free disk size: %f GB, too little, quit", tsAvailLogDirGB); - return -1; - } - - if (tsAvailTmpDirectorySpace < tsReservedTmpDirectorySpace) { - dError("free disk size: %f GB, too little, quit", tsAvailTmpDirectorySpace); - return -1; - } - - return 0; -} - -static int32_t dnodeCheckOs() { - // TODO: - - return 0; -} -static int32_t dnodeCheckAccess() { - // TODO: - - return 0; -} - -static int32_t dnodeCheckVersion() { - // TODO: - - return 0; -} - -static int32_t dnodeCheckDatafile() { - // TODO: - - return 0; -} - -static void dnodeAllocCheckItem() { - tsCheckItem[TSDB_CHECK_ITEM_NETWORK].enable = false; - tsCheckItem[TSDB_CHECK_ITEM_NETWORK].name = "network"; - tsCheckItem[TSDB_CHECK_ITEM_NETWORK].initFp = NULL; - tsCheckItem[TSDB_CHECK_ITEM_NETWORK].cleanUpFp = NULL; - tsCheckItem[TSDB_CHECK_ITEM_NETWORK].startFp = dnodeCheckNetwork; - tsCheckItem[TSDB_CHECK_ITEM_NETWORK].stopFp = NULL; - - tsCheckItem[TSDB_CHECK_ITEM_MEM].enable = true; - tsCheckItem[TSDB_CHECK_ITEM_MEM].name = "mem"; - tsCheckItem[TSDB_CHECK_ITEM_MEM].initFp = NULL; - tsCheckItem[TSDB_CHECK_ITEM_MEM].cleanUpFp = NULL; - tsCheckItem[TSDB_CHECK_ITEM_MEM].startFp = dnodeCheckMem; - tsCheckItem[TSDB_CHECK_ITEM_MEM].stopFp = NULL; - - tsCheckItem[TSDB_CHECK_ITEM_CPU].enable = true; - tsCheckItem[TSDB_CHECK_ITEM_CPU].name = "cpu"; - tsCheckItem[TSDB_CHECK_ITEM_CPU].initFp = NULL; - tsCheckItem[TSDB_CHECK_ITEM_CPU].cleanUpFp = NULL; - tsCheckItem[TSDB_CHECK_ITEM_CPU].startFp = dnodeCheckCpu; - tsCheckItem[TSDB_CHECK_ITEM_CPU].stopFp = NULL; - - tsCheckItem[TSDB_CHECK_ITEM_DISK].enable = true; - tsCheckItem[TSDB_CHECK_ITEM_DISK].name = "disk"; - tsCheckItem[TSDB_CHECK_ITEM_DISK].initFp = NULL; - tsCheckItem[TSDB_CHECK_ITEM_DISK].cleanUpFp = NULL; - tsCheckItem[TSDB_CHECK_ITEM_DISK].startFp = dnodeCheckDisk; - tsCheckItem[TSDB_CHECK_ITEM_DISK].stopFp = NULL; - - tsCheckItem[TSDB_CHECK_ITEM_OS].enable = true; - tsCheckItem[TSDB_CHECK_ITEM_OS].name = "os"; - tsCheckItem[TSDB_CHECK_ITEM_OS].initFp = NULL; - tsCheckItem[TSDB_CHECK_ITEM_OS].cleanUpFp = NULL; - tsCheckItem[TSDB_CHECK_ITEM_OS].startFp = dnodeCheckOs; - tsCheckItem[TSDB_CHECK_ITEM_OS].stopFp = NULL; - - tsCheckItem[TSDB_CHECK_ITEM_ACCESS].enable = true; - tsCheckItem[TSDB_CHECK_ITEM_ACCESS].name = "access"; - tsCheckItem[TSDB_CHECK_ITEM_ACCESS].initFp = NULL; - tsCheckItem[TSDB_CHECK_ITEM_ACCESS].cleanUpFp = NULL; - tsCheckItem[TSDB_CHECK_ITEM_ACCESS].startFp = dnodeCheckAccess; - tsCheckItem[TSDB_CHECK_ITEM_ACCESS].stopFp = NULL; - - tsCheckItem[TSDB_CHECK_ITEM_VERSION].enable = true; - tsCheckItem[TSDB_CHECK_ITEM_VERSION].name = "version"; - tsCheckItem[TSDB_CHECK_ITEM_VERSION].initFp = NULL; - tsCheckItem[TSDB_CHECK_ITEM_VERSION].cleanUpFp = NULL; - tsCheckItem[TSDB_CHECK_ITEM_VERSION].startFp = dnodeCheckVersion; - tsCheckItem[TSDB_CHECK_ITEM_VERSION].stopFp = NULL; - - tsCheckItem[TSDB_CHECK_ITEM_DATAFILE].enable = true; - tsCheckItem[TSDB_CHECK_ITEM_DATAFILE].name = "datafile"; - tsCheckItem[TSDB_CHECK_ITEM_DATAFILE].initFp = NULL; - tsCheckItem[TSDB_CHECK_ITEM_DATAFILE].cleanUpFp = NULL; - tsCheckItem[TSDB_CHECK_ITEM_DATAFILE].startFp = dnodeCheckDatafile; - tsCheckItem[TSDB_CHECK_ITEM_DATAFILE].stopFp = NULL; -} - -void dnodeCleanupCheck() { - for (ECheckItemType index = 0; index < TSDB_CHECK_ITEM_MAX; ++index) { - if (tsCheckItem[index].enable && tsCheckItem[index].stopFp) { - (*tsCheckItem[index].stopFp)(); - } - if (tsCheckItem[index].cleanUpFp) { - (*tsCheckItem[index].cleanUpFp)(); - } - } -} - -int32_t dnodeInitCheck() { - dnodeAllocCheckItem(); - - for (ECheckItemType index = 0; index < TSDB_CHECK_ITEM_MAX; ++index) { - if (tsCheckItem[index].initFp) { - if ((*tsCheckItem[index].initFp)() != 0) { - dError("failed to init check item:%s", tsCheckItem[index].name); - return -1; - } - } - } - - for (ECheckItemType index = 0; index < TSDB_CHECK_ITEM_MAX; ++index) { - if (tsCheckItem[index].enable && tsCheckItem[index].startFp) { - if ((*tsCheckItem[index].startFp)() != 0) { - dError("failed to check item:%s", tsCheckItem[index].name); - exit(-1); - } - } - } - - dInfo("dnode check is initialized"); - return 0; -} diff --git a/src/dnode/src/dnodeEps.c b/src/dnode/src/dnodeEps.c deleted file mode 100644 index 9b1535364744613f92f8f690d4d6fd5893e191cc..0000000000000000000000000000000000000000 --- a/src/dnode/src/dnodeEps.c +++ /dev/null @@ -1,287 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "cJSON.h" -#include "hash.h" -#include "dnodeEps.h" - -static SDnodeEps *tsEps = NULL; -static SHashObj * tsEpsHash = NULL; -static pthread_mutex_t tsEpsMutex; - -static int32_t dnodeReadEps(); -static int32_t dnodeWriteEps(); -static void dnodeResetEps(SDnodeEps *eps); -static void dnodePrintEps(SDnodeEps *eps); - -int32_t dnodeInitEps() { - pthread_mutex_init(&tsEpsMutex, NULL); - tsEpsHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK); - dnodeResetEps(NULL); - int32_t ret = dnodeReadEps(); - if (ret == 0) { - dInfo("dnode eps is initialized"); - } - return ret; -} - -void dnodeCleanupEps() { - pthread_mutex_lock(&tsEpsMutex); - if (tsEps) { - free(tsEps); - tsEps = NULL; - } - if (tsEpsHash) { - taosHashCleanup(tsEpsHash); - tsEpsHash = NULL; - } - pthread_mutex_unlock(&tsEpsMutex); - pthread_mutex_destroy(&tsEpsMutex); -} - -void dnodeUpdateEps(SDnodeEps *eps) { - if (eps == NULL) return; - - eps->dnodeNum = htonl(eps->dnodeNum); - for (int32_t i = 0; i < eps->dnodeNum; ++i) { - eps->dnodeEps[i].dnodeId = htonl(eps->dnodeEps[i].dnodeId); - eps->dnodeEps[i].dnodePort = htons(eps->dnodeEps[i].dnodePort); - } - - pthread_mutex_lock(&tsEpsMutex); - if (eps->dnodeNum != tsEps->dnodeNum) { - dnodeResetEps(eps); - dnodeWriteEps(); - } else { - int32_t size = sizeof(SDnodeEps) + eps->dnodeNum * sizeof(SDnodeEp); - if (memcmp(eps, tsEps, size) != 0) { - dnodeResetEps(eps); - dnodeWriteEps(); - } - } - pthread_mutex_unlock(&tsEpsMutex); -} - -bool dnodeCheckEpChanged(int32_t dnodeId, char *epstr) { - bool changed = false; - pthread_mutex_lock(&tsEpsMutex); - SDnodeEp *ep = taosHashGet(tsEpsHash, &dnodeId, sizeof(int32_t)); - if (ep != NULL) { - char epSaved[TSDB_EP_LEN + 1]; - snprintf(epSaved, TSDB_EP_LEN, "%s:%u", ep->dnodeFqdn, ep->dnodePort); - changed = strcmp(epstr, epSaved) != 0; - tstrncpy(epstr, epSaved, TSDB_EP_LEN); - } - pthread_mutex_unlock(&tsEpsMutex); - return changed; -} - -void dnodeUpdateEp(int32_t dnodeId, char *epstr, char *fqdn, uint16_t *port) { - pthread_mutex_lock(&tsEpsMutex); - SDnodeEp *ep = taosHashGet(tsEpsHash, &dnodeId, sizeof(int32_t)); - if (ep != NULL) { - if (port) *port = ep->dnodePort; - if (fqdn) tstrncpy(fqdn, ep->dnodeFqdn, TSDB_FQDN_LEN); - if (epstr) snprintf(epstr, TSDB_EP_LEN, "%s:%u", ep->dnodeFqdn, ep->dnodePort); - } - pthread_mutex_unlock(&tsEpsMutex); -} - -static void dnodeResetEps(SDnodeEps *eps) { - if (eps == NULL) { - int32_t size = sizeof(SDnodeEps) + sizeof(SDnodeEp); - if (tsEps == NULL) { - tsEps = calloc(1, size); - } else { - tsEps->dnodeNum = 0; - } - } else { - assert(tsEps); - - int32_t size = sizeof(SDnodeEps) + sizeof(SDnodeEp) * eps->dnodeNum; - if (eps->dnodeNum > tsEps->dnodeNum) { - tsEps = realloc(tsEps, size); - } - memcpy(tsEps, eps, size); - dnodePrintEps(eps); - } - - for (int32_t i = 0; i < tsEps->dnodeNum; ++i) { - SDnodeEp *ep = &tsEps->dnodeEps[i]; - taosHashPut(tsEpsHash, &ep->dnodeId, sizeof(int32_t), ep, sizeof(SDnodeEp)); - } -} - -static void dnodePrintEps(SDnodeEps *eps) { - dDebug("print dnodeEp, dnodeNum:%d", eps->dnodeNum); - for (int32_t i = 0; i < eps->dnodeNum; i++) { - SDnodeEp *ep = &eps->dnodeEps[i]; - dDebug("dnode:%d, dnodeFqdn:%s dnodePort:%u", ep->dnodeId, ep->dnodeFqdn, ep->dnodePort); - } -} - -static int32_t dnodeReadEps() { - int32_t ret = -1; - int32_t len = 0; - int32_t maxLen = 30000; - char * content = calloc(1, maxLen + 1); - cJSON * root = NULL; - FILE * fp = NULL; - SDnodeEps *eps = NULL; - - char file[TSDB_FILENAME_LEN + 20] = {0}; - sprintf(file, "%s/dnodeEps.json", tsDnodeDir); - - fp = fopen(file, "r"); - if (!fp) { - dDebug("failed to read %s, file not exist", file); - goto PRASE_EPS_OVER; - } - - len = (int32_t)fread(content, 1, maxLen, fp); - if (len <= 0) { - dError("failed to read %s, content is null", file); - goto PRASE_EPS_OVER; - } - - content[len] = 0; - root = cJSON_Parse(content); - if (root == NULL) { - dError("failed to read %s, invalid json format", file); - goto PRASE_EPS_OVER; - } - - cJSON *dnodeNum = cJSON_GetObjectItem(root, "dnodeNum"); - if (!dnodeNum || dnodeNum->type != cJSON_Number) { - dError("failed to read %s, dnodeNum not found", file); - goto PRASE_EPS_OVER; - } - - cJSON *dnodeInfos = cJSON_GetObjectItem(root, "dnodeInfos"); - if (!dnodeInfos || dnodeInfos->type != cJSON_Array) { - dError("failed to read %s, dnodeInfos not found", file); - goto PRASE_EPS_OVER; - } - - int32_t dnodeInfosSize = cJSON_GetArraySize(dnodeInfos); - if (dnodeInfosSize != dnodeNum->valueint) { - dError("failed to read %s, dnodeInfos size:%d not matched dnodeNum:%d", file, dnodeInfosSize, - (int32_t)dnodeNum->valueint); - goto PRASE_EPS_OVER; - } - - int32_t epsSize = sizeof(SDnodeEps) + dnodeInfosSize * sizeof(SDnodeEp); - eps = calloc(1, epsSize); - eps->dnodeNum = dnodeInfosSize; - - for (int32_t i = 0; i < dnodeInfosSize; ++i) { - cJSON *dnodeInfo = cJSON_GetArrayItem(dnodeInfos, i); - if (dnodeInfo == NULL) break; - - SDnodeEp *ep = &eps->dnodeEps[i]; - - cJSON *dnodeId = cJSON_GetObjectItem(dnodeInfo, "dnodeId"); - if (!dnodeId || dnodeId->type != cJSON_Number) { - dError("failed to read %s, dnodeId not found", file); - goto PRASE_EPS_OVER; - } - ep->dnodeId = (int32_t)dnodeId->valueint; - - cJSON *dnodeFqdn = cJSON_GetObjectItem(dnodeInfo, "dnodeFqdn"); - if (!dnodeFqdn || dnodeFqdn->type != cJSON_String || dnodeFqdn->valuestring == NULL) { - dError("failed to read %s, dnodeFqdn not found", file); - goto PRASE_EPS_OVER; - } - strncpy(ep->dnodeFqdn, dnodeFqdn->valuestring, TSDB_FQDN_LEN); - - cJSON *dnodePort = cJSON_GetObjectItem(dnodeInfo, "dnodePort"); - if (!dnodePort || dnodePort->type != cJSON_Number) { - dError("failed to read %s, dnodePort not found", file); - goto PRASE_EPS_OVER; - } - ep->dnodePort = (uint16_t)dnodePort->valueint; - } - - ret = 0; - - dInfo("read file %s successed", file); - dnodePrintEps(eps); - -PRASE_EPS_OVER: - if (content != NULL) free(content); - if (root != NULL) cJSON_Delete(root); - if (fp != NULL) fclose(fp); - if (ret != 0) { - if (eps) free(eps); - eps = NULL; - } - - dnodeResetEps(eps); - if (eps) free(eps); - -#if 0 - dnodeUpdateEp(dnodeGetDnodeId(), tsLocalEp, tsLocalFqdn, &tsServerPort); -#else - if (dnodeCheckEpChanged(dnodeGetDnodeId(), tsLocalEp)) { - dError("dnode:%d, localEp is different from %s in dnodeEps.json and need reconfigured", dnodeGetDnodeId(), tsLocalEp); - return -1; - } -#endif - - terrno = 0; - return 0; -} - -static int32_t dnodeWriteEps() { - char file[TSDB_FILENAME_LEN + 20] = {0}; - sprintf(file, "%s/dnodeEps.json", tsDnodeDir); - - FILE *fp = fopen(file, "w"); - if (!fp) { - dError("failed to write %s, reason:%s", file, strerror(errno)); - return -1; - } - - int32_t len = 0; - int32_t maxLen = 30000; - char * content = calloc(1, maxLen + 1); - - len += snprintf(content + len, maxLen - len, "{\n"); - len += snprintf(content + len, maxLen - len, " \"dnodeNum\": %d,\n", tsEps->dnodeNum); - len += snprintf(content + len, maxLen - len, " \"dnodeInfos\": [{\n"); - for (int32_t i = 0; i < tsEps->dnodeNum; ++i) { - SDnodeEp *ep = &tsEps->dnodeEps[i]; - len += snprintf(content + len, maxLen - len, " \"dnodeId\": %d,\n", ep->dnodeId); - len += snprintf(content + len, maxLen - len, " \"dnodeFqdn\": \"%s\",\n", ep->dnodeFqdn); - len += snprintf(content + len, maxLen - len, " \"dnodePort\": %u\n", ep->dnodePort); - if (i < tsEps->dnodeNum - 1) { - len += snprintf(content + len, maxLen - len, " },{\n"); - } else { - len += snprintf(content + len, maxLen - len, " }]\n"); - } - } - len += snprintf(content + len, maxLen - len, "}\n"); - - fwrite(content, 1, len, fp); - taosFsync(fileno(fp)); - fclose(fp); - free(content); - terrno = 0; - - dInfo("successed to write %s", file); - return 0; -} diff --git a/src/dnode/src/dnodeMInfos.c b/src/dnode/src/dnodeMInfos.c deleted file mode 100644 index 611c30b84344eac4306b73eb49e1405cc83289b6..0000000000000000000000000000000000000000 --- a/src/dnode/src/dnodeMInfos.c +++ /dev/null @@ -1,326 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "cJSON.h" -#include "mnode.h" -#include "dnodeMInfos.h" - -static SMInfos tsMInfos; -static SRpcEpSet tsMEpSet; -static pthread_mutex_t tsMInfosMutex; - -static void dnodeResetMInfos(SMInfos *minfos); -static void dnodePrintMInfos(SMInfos *minfos); -static int32_t dnodeReadMInfos(); -static int32_t dnodeWriteMInfos(); - -int32_t dnodeInitMInfos() { - pthread_mutex_init(&tsMInfosMutex, NULL); - dnodeResetMInfos(NULL); - int32_t ret = dnodeReadMInfos(); - if (ret == 0) { - dInfo("dnode minfos is initialized"); - } - - return ret; -} - -void dnodeCleanupMInfos() { pthread_mutex_destroy(&tsMInfosMutex); } - -void dnodeUpdateMInfos(SMInfos *pMinfos) { - if (pMinfos->mnodeNum <= 0 || pMinfos->mnodeNum > 3) { - dError("invalid mnode infos, mnodeNum:%d", pMinfos->mnodeNum); - return; - } - - for (int32_t i = 0; i < pMinfos->mnodeNum; ++i) { - SMInfo *minfo = &pMinfos->mnodeInfos[i]; - minfo->mnodeId = htonl(minfo->mnodeId); - if (minfo->mnodeId <= 0 || strlen(minfo->mnodeEp) <= 5) { - dError("invalid mnode info:%d, mnodeId:%d mnodeEp:%s", i, minfo->mnodeId, minfo->mnodeEp); - return; - } - } - - pthread_mutex_lock(&tsMInfosMutex); - if (pMinfos->mnodeNum != tsMInfos.mnodeNum) { - dnodeResetMInfos(pMinfos); - dnodeWriteMInfos(); - sdbUpdateAsync(); - } else { - int32_t size = sizeof(SMInfos); - if (memcmp(pMinfos, &tsMInfos, size) != 0) { - dnodeResetMInfos(pMinfos); - dnodeWriteMInfos(); - sdbUpdateAsync(); - } - } - pthread_mutex_unlock(&tsMInfosMutex); -} - -void dnodeUpdateEpSetForPeer(SRpcEpSet *ep) { - if (ep->numOfEps <= 0) { - dError("minfos is changed, but content is invalid, discard it"); - return; - } - - pthread_mutex_lock(&tsMInfosMutex); - dInfo("minfos is changed, numOfEps:%d inUse:%d", ep->numOfEps, ep->inUse); - for (int32_t i = 0; i < ep->numOfEps; ++i) { - ep->port[i] -= TSDB_PORT_DNODEDNODE; - dInfo("minfo:%d %s:%u", i, ep->fqdn[i], ep->port[i]); - } - tsMEpSet = *ep; - pthread_mutex_unlock(&tsMInfosMutex); -} - -bool dnodeIsMasterEp(char *ep) { - pthread_mutex_lock(&tsMInfosMutex); - bool isMaster = strcmp(ep, tsMInfos.mnodeInfos[tsMEpSet.inUse].mnodeEp) == 0; - pthread_mutex_unlock(&tsMInfosMutex); - - return isMaster; -} - -void dnodeGetMInfos(SMInfos *pMinfos) { - pthread_mutex_lock(&tsMInfosMutex); - memcpy(pMinfos, &tsMInfos, sizeof(SMInfos)); - for (int32_t i = 0; i < tsMInfos.mnodeNum; ++i) { - pMinfos->mnodeInfos[i].mnodeId = htonl(tsMInfos.mnodeInfos[i].mnodeId); - } - pthread_mutex_unlock(&tsMInfosMutex); -} - -void dnodeGetEpSetForPeer(SRpcEpSet *epSet) { - pthread_mutex_lock(&tsMInfosMutex); - *epSet = tsMEpSet; - for (int32_t i = 0; i < epSet->numOfEps; ++i) { - epSet->port[i] += TSDB_PORT_DNODEDNODE; - } - pthread_mutex_unlock(&tsMInfosMutex); -} - -void dnodeGetEpSetForShell(SRpcEpSet *epSet) { - pthread_mutex_lock(&tsMInfosMutex); - *epSet = tsMEpSet; - pthread_mutex_unlock(&tsMInfosMutex); -} - -static void dnodePrintMInfos(SMInfos *pMinfos) { - dInfo("print minfos, mnodeNum:%d inUse:%d", pMinfos->mnodeNum, pMinfos->inUse); - for (int32_t i = 0; i < pMinfos->mnodeNum; i++) { - dInfo("mnode index:%d, %s", pMinfos->mnodeInfos[i].mnodeId, pMinfos->mnodeInfos[i].mnodeEp); - } -} - -static void dnodeResetMInfos(SMInfos *pMinfos) { - if (pMinfos == NULL) { - tsMEpSet.numOfEps = 1; - taosGetFqdnPortFromEp(tsFirst, tsMEpSet.fqdn[0], &tsMEpSet.port[0]); - - if (strcmp(tsSecond, tsFirst) != 0) { - tsMEpSet.numOfEps = 2; - taosGetFqdnPortFromEp(tsSecond, tsMEpSet.fqdn[1], &tsMEpSet.port[1]); - } - return; - } - - if (pMinfos->mnodeNum == 0) return; - - int32_t size = sizeof(SMInfos); - memcpy(&tsMInfos, pMinfos, size); - - tsMEpSet.inUse = tsMInfos.inUse; - tsMEpSet.numOfEps = tsMInfos.mnodeNum; - for (int32_t i = 0; i < tsMInfos.mnodeNum; i++) { - taosGetFqdnPortFromEp(tsMInfos.mnodeInfos[i].mnodeEp, tsMEpSet.fqdn[i], &tsMEpSet.port[i]); - } - - dnodePrintMInfos(pMinfos); -} - -static int32_t dnodeReadMInfos() { - int32_t len = 0; - int32_t maxLen = 2000; - char * content = calloc(1, maxLen + 1); - cJSON * root = NULL; - FILE * fp = NULL; - SMInfos minfos = {0}; - bool nodeChanged = false; - - char file[TSDB_FILENAME_LEN + 20] = {0}; - sprintf(file, "%s/mnodeEpSet.json", tsDnodeDir); - - fp = fopen(file, "r"); - if (!fp) { - dDebug("failed to read %s, file not exist", file); - goto PARSE_MINFOS_OVER; - } - - len = (int32_t)fread(content, 1, maxLen, fp); - if (len <= 0) { - dError("failed to read %s, content is null", file); - goto PARSE_MINFOS_OVER; - } - - content[len] = 0; - root = cJSON_Parse(content); - if (root == NULL) { - dError("failed to read %s, invalid json format", file); - goto PARSE_MINFOS_OVER; - } - - cJSON *inUse = cJSON_GetObjectItem(root, "inUse"); - if (!inUse || inUse->type != cJSON_Number) { - dError("failed to read mnodeEpSet.json, inUse not found"); - goto PARSE_MINFOS_OVER; - } - tsMInfos.inUse = (int8_t)inUse->valueint; - - cJSON *nodeNum = cJSON_GetObjectItem(root, "nodeNum"); - if (!nodeNum || nodeNum->type != cJSON_Number) { - dError("failed to read mnodeEpSet.json, nodeNum not found"); - goto PARSE_MINFOS_OVER; - } - minfos.mnodeNum = (int8_t)nodeNum->valueint; - - cJSON *nodeInfos = cJSON_GetObjectItem(root, "nodeInfos"); - if (!nodeInfos || nodeInfos->type != cJSON_Array) { - dError("failed to read mnodeEpSet.json, nodeInfos not found"); - goto PARSE_MINFOS_OVER; - } - - int32_t size = cJSON_GetArraySize(nodeInfos); - if (size != minfos.mnodeNum) { - dError("failed to read mnodeEpSet.json, nodeInfos size not matched"); - goto PARSE_MINFOS_OVER; - } - - for (int32_t i = 0; i < size; ++i) { - cJSON *nodeInfo = cJSON_GetArrayItem(nodeInfos, i); - if (nodeInfo == NULL) continue; - - cJSON *nodeId = cJSON_GetObjectItem(nodeInfo, "nodeId"); - if (!nodeId || nodeId->type != cJSON_Number) { - dError("failed to read mnodeEpSet.json, nodeId not found"); - goto PARSE_MINFOS_OVER; - } - - cJSON *nodeEp = cJSON_GetObjectItem(nodeInfo, "nodeEp"); - if (!nodeEp || nodeEp->type != cJSON_String || nodeEp->valuestring == NULL) { - dError("failed to read mnodeEpSet.json, nodeName not found"); - goto PARSE_MINFOS_OVER; - } - - SMInfo *pMinfo = &minfos.mnodeInfos[i]; - pMinfo->mnodeId = (int32_t)nodeId->valueint; - tstrncpy(pMinfo->mnodeEp, nodeEp->valuestring, TSDB_EP_LEN); - - bool changed = dnodeCheckEpChanged(pMinfo->mnodeId, pMinfo->mnodeEp); - if (changed) nodeChanged = changed; - } - - dInfo("read file %s successed", file); - dnodePrintMInfos(&minfos); - -PARSE_MINFOS_OVER: - if (content != NULL) free(content); - if (root != NULL) cJSON_Delete(root); - if (fp != NULL) fclose(fp); - terrno = 0; - - for (int32_t i = 0; i < minfos.mnodeNum; ++i) { - SMInfo *mInfo = &minfos.mnodeInfos[i]; - dnodeUpdateEp(mInfo->mnodeId, mInfo->mnodeEp, NULL, NULL); - } - dnodeResetMInfos(&minfos); - - if (nodeChanged) { - dnodeWriteMInfos(); - } - - return 0; -} - -static int32_t dnodeWriteMInfos() { - char file[TSDB_FILENAME_LEN + 20] = {0}; - sprintf(file, "%s/mnodeEpSet.json", tsDnodeDir); - - FILE *fp = fopen(file, "w"); - if (!fp) { - dError("failed to write %s, reason:%s", file, strerror(errno)); - return -1; - } - - int32_t len = 0; - int32_t maxLen = 2000; - char * content = calloc(1, maxLen + 1); - - len += snprintf(content + len, maxLen - len, "{\n"); - len += snprintf(content + len, maxLen - len, " \"inUse\": %d,\n", tsMInfos.inUse); - len += snprintf(content + len, maxLen - len, " \"nodeNum\": %d,\n", tsMInfos.mnodeNum); - len += snprintf(content + len, maxLen - len, " \"nodeInfos\": [{\n"); - for (int32_t i = 0; i < tsMInfos.mnodeNum; i++) { - len += snprintf(content + len, maxLen - len, " \"nodeId\": %d,\n", tsMInfos.mnodeInfos[i].mnodeId); - len += snprintf(content + len, maxLen - len, " \"nodeEp\": \"%s\"\n", tsMInfos.mnodeInfos[i].mnodeEp); - if (i < tsMInfos.mnodeNum - 1) { - len += snprintf(content + len, maxLen - len, " },{\n"); - } else { - len += snprintf(content + len, maxLen - len, " }]\n"); - } - } - len += snprintf(content + len, maxLen - len, "}\n"); - - fwrite(content, 1, len, fp); - taosFsync(fileno(fp)); - fclose(fp); - free(content); - terrno = 0; - - dInfo("successed to write %s", file); - return 0; -} - -void dnodeSendRedirectMsg(SRpcMsg *rpcMsg, bool forShell) { - SRpcConnInfo connInfo = {0}; - rpcGetConnInfo(rpcMsg->handle, &connInfo); - - SRpcEpSet epSet = {0}; - if (forShell) { - dnodeGetEpSetForShell(&epSet); - } else { - dnodeGetEpSetForPeer(&epSet); - } - - dDebug("msg:%s will be redirected, dnodeIp:%s user:%s, numOfEps:%d inUse:%d", taosMsg[rpcMsg->msgType], - taosIpStr(connInfo.clientIp), connInfo.user, epSet.numOfEps, epSet.inUse); - - for (int32_t i = 0; i < epSet.numOfEps; ++i) { - dDebug("mnode index:%d %s:%d", i, epSet.fqdn[i], epSet.port[i]); - if (strcmp(epSet.fqdn[i], tsLocalFqdn) == 0) { - if ((epSet.port[i] == tsServerPort + TSDB_PORT_DNODEDNODE && !forShell) || - (epSet.port[i] == tsServerPort && forShell)) { - epSet.inUse = (i + 1) % epSet.numOfEps; - dDebug("mnode index:%d %s:%d set inUse to %d", i, epSet.fqdn[i], epSet.port[i], epSet.inUse); - } - } - - epSet.port[i] = htons(epSet.port[i]); - } - - rpcSendRedirectRsp(rpcMsg->handle, &epSet); -} \ No newline at end of file diff --git a/src/dnode/src/dnodeMPeer.c b/src/dnode/src/dnodeMPeer.c deleted file mode 100644 index 8aa28d1618efe871de7795b0d403ee060cf5941c..0000000000000000000000000000000000000000 --- a/src/dnode/src/dnodeMPeer.c +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "tqueue.h" -#include "twal.h" -#include "mnode.h" -#include "dnodeVMgmt.h" -#include "dnodeMInfos.h" -#include "dnodeMWrite.h" - -typedef struct { - pthread_t thread; - int32_t workerId; -} SMPeerWorker; - -typedef struct { - int32_t curNum; - int32_t maxNum; - SMPeerWorker *worker; -} SMPeerWorkerPool; - -static SMPeerWorkerPool tsMPeerWP; -static taos_qset tsMPeerQset; -static taos_queue tsMPeerQueue; - -static void *dnodeProcessMPeerQueue(void *param); - -int32_t dnodeInitMPeer() { - tsMPeerQset = taosOpenQset(); - - tsMPeerWP.maxNum = 1; - tsMPeerWP.curNum = 0; - tsMPeerWP.worker = (SMPeerWorker *)calloc(sizeof(SMPeerWorker), tsMPeerWP.maxNum); - - if (tsMPeerWP.worker == NULL) return -1; - for (int32_t i = 0; i < tsMPeerWP.maxNum; ++i) { - SMPeerWorker *pWorker = tsMPeerWP.worker + i; - pWorker->workerId = i; - dDebug("dnode mpeer worker:%d is created", i); - } - - dDebug("dnode mpeer is initialized, workers:%d qset:%p", tsMPeerWP.maxNum, tsMPeerQset); - return 0; -} - -void dnodeCleanupMPeer() { - for (int32_t i = 0; i < tsMPeerWP.maxNum; ++i) { - SMPeerWorker *pWorker = tsMPeerWP.worker + i; - if (taosCheckPthreadValid(pWorker->thread)) { - taosQsetThreadResume(tsMPeerQset); - } - dDebug("dnode mpeer worker:%d is closed", i); - } - - for (int32_t i = 0; i < tsMPeerWP.maxNum; ++i) { - SMPeerWorker *pWorker = tsMPeerWP.worker + i; - dDebug("dnode mpeer worker:%d start to join", i); - if (taosCheckPthreadValid(pWorker->thread)) { - pthread_join(pWorker->thread, NULL); - } - dDebug("dnode mpeer worker:%d join success", i); - } - - dDebug("dnode mpeer is closed, qset:%p", tsMPeerQset); - - taosCloseQset(tsMPeerQset); - tsMPeerQset = NULL; - tfree(tsMPeerWP.worker); -} - -int32_t dnodeAllocateMPeerQueue() { - tsMPeerQueue = taosOpenQueue(); - if (tsMPeerQueue == NULL) return TSDB_CODE_DND_OUT_OF_MEMORY; - - taosAddIntoQset(tsMPeerQset, tsMPeerQueue, NULL); - - for (int32_t i = tsMPeerWP.curNum; i < tsMPeerWP.maxNum; ++i) { - SMPeerWorker *pWorker = tsMPeerWP.worker + i; - pWorker->workerId = i; - - pthread_attr_t thAttr; - pthread_attr_init(&thAttr); - pthread_attr_setdetachstate(&thAttr, PTHREAD_CREATE_JOINABLE); - - if (pthread_create(&pWorker->thread, &thAttr, dnodeProcessMPeerQueue, pWorker) != 0) { - dError("failed to create thread to process mpeer queue, reason:%s", strerror(errno)); - } - - pthread_attr_destroy(&thAttr); - - tsMPeerWP.curNum = i + 1; - dDebug("dnode mpeer worker:%d is launched, total:%d", pWorker->workerId, tsMPeerWP.maxNum); - } - - dDebug("dnode mpeer queue:%p is allocated", tsMPeerQueue); - return TSDB_CODE_SUCCESS; -} - -void dnodeFreeMPeerQueue() { - dDebug("dnode mpeer queue:%p is freed", tsMPeerQueue); - taosCloseQueue(tsMPeerQueue); - tsMPeerQueue = NULL; -} - -void dnodeDispatchToMPeerQueue(SRpcMsg *pMsg) { - if (!mnodeIsRunning() || tsMPeerQueue == NULL) { - dnodeSendRedirectMsg(pMsg, false); - } else { - SMnodeMsg *pPeer = mnodeCreateMsg(pMsg); - taosWriteQitem(tsMPeerQueue, TAOS_QTYPE_RPC, pPeer); - } - - rpcFreeCont(pMsg->pCont); -} - -static void dnodeFreeMPeerMsg(SMnodeMsg *pPeer) { - mnodeCleanupMsg(pPeer); - taosFreeQitem(pPeer); -} - -static void dnodeSendRpcMPeerRsp(SMnodeMsg *pPeer, int32_t code) { - if (code == TSDB_CODE_MND_ACTION_IN_PROGRESS) return; - - SRpcMsg rpcRsp = { - .handle = pPeer->rpcMsg.handle, - .pCont = pPeer->rpcRsp.rsp, - .contLen = pPeer->rpcRsp.len, - .code = code, - }; - - rpcSendResponse(&rpcRsp); - dnodeFreeMPeerMsg(pPeer); -} - -static void *dnodeProcessMPeerQueue(void *param) { - SMnodeMsg *pPeerMsg; - int32_t type; - void * unUsed; - - setThreadName("dnodeMPeerQ"); - - while (1) { - if (taosReadQitemFromQset(tsMPeerQset, &type, (void **)&pPeerMsg, &unUsed) == 0) { - dDebug("qset:%p, mnode peer got no message from qset, exiting", tsMPeerQset); - break; - } - - dTrace("msg:%s will be processed in mpeer queue", taosMsg[pPeerMsg->rpcMsg.msgType]); - int32_t code = mnodeProcessPeerReq(pPeerMsg); - dnodeSendRpcMPeerRsp(pPeerMsg, code); - } - - return NULL; -} diff --git a/src/dnode/src/dnodeMRead.c b/src/dnode/src/dnodeMRead.c deleted file mode 100644 index 184a6b743afdd5f4284a5acffc8d518356be4ee4..0000000000000000000000000000000000000000 --- a/src/dnode/src/dnodeMRead.c +++ /dev/null @@ -1,173 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "tqueue.h" -#include "mnode.h" -#include "dnodeMInfos.h" -#include "dnodeMRead.h" - -typedef struct { - pthread_t thread; - int32_t workerId; -} SMReadWorker; - -typedef struct { - int32_t curNum; - int32_t maxNum; - SMReadWorker *worker; -} SMReadWorkerPool; - -static SMReadWorkerPool tsMReadWP; -static taos_qset tsMReadQset; -static taos_queue tsMReadQueue; - -static void *dnodeProcessMReadQueue(void *param); - -int32_t dnodeInitMRead() { - tsMReadQset = taosOpenQset(); - - tsMReadWP.maxNum = (int32_t)(tsNumOfCores * tsNumOfThreadsPerCore / 2); - tsMReadWP.maxNum = MAX(2, tsMReadWP.maxNum); - tsMReadWP.maxNum = MIN(4, tsMReadWP.maxNum); - tsMReadWP.curNum = 0; - tsMReadWP.worker = (SMReadWorker *)calloc(sizeof(SMReadWorker), tsMReadWP.maxNum); - - if (tsMReadWP.worker == NULL) return -1; - for (int32_t i = 0; i < tsMReadWP.maxNum; ++i) { - SMReadWorker *pWorker = tsMReadWP.worker + i; - pWorker->workerId = i; - dDebug("dnode mread worker:%d is created", i); - } - - dDebug("dnode mread is initialized, workers:%d qset:%p", tsMReadWP.maxNum, tsMReadQset); - return 0; -} - -void dnodeCleanupMRead() { - for (int32_t i = 0; i < tsMReadWP.maxNum; ++i) { - SMReadWorker *pWorker = tsMReadWP.worker + i; - if (taosCheckPthreadValid(pWorker->thread)) { - taosQsetThreadResume(tsMReadQset); - } - dDebug("dnode mread worker:%d is closed", i); - } - - for (int32_t i = 0; i < tsMReadWP.maxNum; ++i) { - SMReadWorker *pWorker = tsMReadWP.worker + i; - dDebug("dnode mread worker:%d start to join", i); - if (taosCheckPthreadValid(pWorker->thread)) { - pthread_join(pWorker->thread, NULL); - } - dDebug("dnode mread worker:%d start to join", i); - } - - dDebug("dnode mread is closed, qset:%p", tsMReadQset); - - taosCloseQset(tsMReadQset); - tsMReadQset = NULL; - free(tsMReadWP.worker); -} - -int32_t dnodeAllocMReadQueue() { - tsMReadQueue = taosOpenQueue(); - if (tsMReadQueue == NULL) return TSDB_CODE_DND_OUT_OF_MEMORY; - - taosAddIntoQset(tsMReadQset, tsMReadQueue, NULL); - - for (int32_t i = tsMReadWP.curNum; i < tsMReadWP.maxNum; ++i) { - SMReadWorker *pWorker = tsMReadWP.worker + i; - pWorker->workerId = i; - - pthread_attr_t thAttr; - pthread_attr_init(&thAttr); - pthread_attr_setdetachstate(&thAttr, PTHREAD_CREATE_JOINABLE); - - if (pthread_create(&pWorker->thread, &thAttr, dnodeProcessMReadQueue, pWorker) != 0) { - dError("failed to create thread to process mread queue, reason:%s", strerror(errno)); - } - - pthread_attr_destroy(&thAttr); - tsMReadWP.curNum = i + 1; - dDebug("dnode mread worker:%d is launched, total:%d", pWorker->workerId, tsMReadWP.maxNum); - } - - dDebug("dnode mread queue:%p is allocated", tsMReadQueue); - return TSDB_CODE_SUCCESS; -} - -void dnodeFreeMReadQueue() { - dDebug("dnode mread queue:%p is freed", tsMReadQueue); - taosCloseQueue(tsMReadQueue); - tsMReadQueue = NULL; -} - -void dnodeDispatchToMReadQueue(SRpcMsg *pMsg) { - if (!mnodeIsRunning() || tsMReadQueue == NULL) { - dnodeSendRedirectMsg(pMsg, true); - } else { - SMnodeMsg *pRead = mnodeCreateMsg(pMsg); - taosWriteQitem(tsMReadQueue, TAOS_QTYPE_RPC, pRead); - } - - rpcFreeCont(pMsg->pCont); -} - -static void dnodeFreeMReadMsg(SMnodeMsg *pRead) { - mnodeCleanupMsg(pRead); - taosFreeQitem(pRead); -} - -static void dnodeSendRpcMReadRsp(SMnodeMsg *pRead, int32_t code) { - if (code == TSDB_CODE_MND_ACTION_IN_PROGRESS) return; - if (code == TSDB_CODE_MND_ACTION_NEED_REPROCESSED) { - // may be a auto create req, should put into write queue - dnodeReprocessMWriteMsg(pRead); - return; - } - - SRpcMsg rpcRsp = { - .handle = pRead->rpcMsg.handle, - .pCont = pRead->rpcRsp.rsp, - .contLen = pRead->rpcRsp.len, - .code = code, - }; - - rpcSendResponse(&rpcRsp); - dnodeFreeMReadMsg(pRead); -} - -static void *dnodeProcessMReadQueue(void *param) { - SMnodeMsg *pRead; - int32_t type; - void * unUsed; - - setThreadName("dnodeMReadQ"); - - while (1) { - if (taosReadQitemFromQset(tsMReadQset, &type, (void **)&pRead, &unUsed) == 0) { - dDebug("qset:%p, mnode read got no message from qset, exiting", tsMReadQset); - break; - } - - dTrace("msg:%p, app:%p type:%s will be processed in mread queue", pRead->rpcMsg.ahandle, pRead, - taosMsg[pRead->rpcMsg.msgType]); - int32_t code = mnodeProcessRead(pRead); - dnodeSendRpcMReadRsp(pRead, code); - } - - return NULL; -} diff --git a/src/dnode/src/dnodeMWrite.c b/src/dnode/src/dnodeMWrite.c deleted file mode 100644 index 904ddc21d019343fa3f679db4d25cd9b01e1d97b..0000000000000000000000000000000000000000 --- a/src/dnode/src/dnodeMWrite.c +++ /dev/null @@ -1,227 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "ttimer.h" -#include "tqueue.h" -#include "mnode.h" -#include "dnodeMInfos.h" -#include "dnodeMWrite.h" - -typedef struct { - pthread_t thread; - int32_t workerId; -} SMWriteWorker; - -typedef struct { - int32_t curNum; - int32_t maxNum; - SMWriteWorker *worker; -} SMWriteWorkerPool; - -static SMWriteWorkerPool tsMWriteWP; -static taos_qset tsMWriteQset; -static taos_queue tsMWriteQueue; -extern void * tsDnodeTmr; - -static void *dnodeProcessMWriteQueue(void *param); - -int32_t dnodeInitMWrite() { - tsMWriteQset = taosOpenQset(); - - tsMWriteWP.maxNum = 1; - tsMWriteWP.curNum = 0; - tsMWriteWP.worker = (SMWriteWorker *)calloc(sizeof(SMWriteWorker), tsMWriteWP.maxNum); - - if (tsMWriteWP.worker == NULL) return -1; - for (int32_t i = 0; i < tsMWriteWP.maxNum; ++i) { - SMWriteWorker *pWorker = tsMWriteWP.worker + i; - pWorker->workerId = i; - dDebug("dnode mwrite worker:%d is created", i); - } - - dDebug("dnode mwrite is initialized, workers:%d qset:%p", tsMWriteWP.maxNum, tsMWriteQset); - return 0; -} - -void dnodeCleanupMWrite() { - for (int32_t i = 0; i < tsMWriteWP.maxNum; ++i) { - SMWriteWorker *pWorker = tsMWriteWP.worker + i; - if (taosCheckPthreadValid(pWorker->thread)) { - taosQsetThreadResume(tsMWriteQset); - } - dDebug("dnode mwrite worker:%d is closed", i); - } - - for (int32_t i = 0; i < tsMWriteWP.maxNum; ++i) { - SMWriteWorker *pWorker = tsMWriteWP.worker + i; - dDebug("dnode mwrite worker:%d start to join", i); - if (taosCheckPthreadValid(pWorker->thread)) { - pthread_join(pWorker->thread, NULL); - } - dDebug("dnode mwrite worker:%d join success", i); - } - - dDebug("dnode mwrite is closed, qset:%p", tsMWriteQset); - - taosCloseQset(tsMWriteQset); - tsMWriteQset = NULL; - tfree(tsMWriteWP.worker); -} - -int32_t dnodeAllocMWritequeue() { - tsMWriteQueue = taosOpenQueue(); - if (tsMWriteQueue == NULL) return TSDB_CODE_DND_OUT_OF_MEMORY; - - taosAddIntoQset(tsMWriteQset, tsMWriteQueue, NULL); - - for (int32_t i = tsMWriteWP.curNum; i < tsMWriteWP.maxNum; ++i) { - SMWriteWorker *pWorker = tsMWriteWP.worker + i; - pWorker->workerId = i; - - pthread_attr_t thAttr; - pthread_attr_init(&thAttr); - pthread_attr_setdetachstate(&thAttr, PTHREAD_CREATE_JOINABLE); - - if (pthread_create(&pWorker->thread, &thAttr, dnodeProcessMWriteQueue, pWorker) != 0) { - dError("failed to create thread to process mwrite queue, reason:%s", strerror(errno)); - } - - pthread_attr_destroy(&thAttr); - tsMWriteWP.curNum = i + 1; - dDebug("dnode mwrite worker:%d is launched, total:%d", pWorker->workerId, tsMWriteWP.maxNum); - } - - dDebug("dnode mwrite queue:%p is allocated", tsMWriteQueue); - return TSDB_CODE_SUCCESS; -} - -void dnodeFreeMWritequeue() { - dDebug("dnode mwrite queue:%p is freed", tsMWriteQueue); - taosCloseQueue(tsMWriteQueue); - tsMWriteQueue = NULL; -} - -void dnodeDispatchToMWriteQueue(SRpcMsg *pMsg) { - if (!mnodeIsRunning() || tsMWriteQueue == NULL) { - dnodeSendRedirectMsg(pMsg, true); - } else { - SMnodeMsg *pWrite = mnodeCreateMsg(pMsg); - dTrace("msg:%p, app:%p type:%s is put into mwrite queue:%p", pWrite, pWrite->rpcMsg.ahandle, - taosMsg[pWrite->rpcMsg.msgType], tsMWriteQueue); - taosWriteQitem(tsMWriteQueue, TAOS_QTYPE_RPC, pWrite); - } - - rpcFreeCont(pMsg->pCont); -} - -static void dnodeFreeMWriteMsg(SMnodeMsg *pWrite) { - dTrace("msg:%p, app:%p type:%s is freed from mwrite queue:%p", pWrite, pWrite->rpcMsg.ahandle, - taosMsg[pWrite->rpcMsg.msgType], tsMWriteQueue); - - mnodeCleanupMsg(pWrite); - taosFreeQitem(pWrite); -} - -void dnodeSendRpcMWriteRsp(void *pMsg, int32_t code) { - SMnodeMsg *pWrite = pMsg; - if (pWrite == NULL) return; - if (code == TSDB_CODE_MND_ACTION_IN_PROGRESS) return; - if (code == TSDB_CODE_MND_ACTION_NEED_REPROCESSED) { - dnodeReprocessMWriteMsg(pWrite); - return; - } - - dTrace("msg:%p, app:%p type:%s master:%p will be responsed", pWrite, pWrite->rpcMsg.ahandle, - taosMsg[pWrite->rpcMsg.msgType], pWrite->pBatchMasterMsg); - if (pWrite->pBatchMasterMsg && pWrite != pWrite->pBatchMasterMsg) { - dError("msg:%p, app:%p type:%s master:%p sub message should not response!", pWrite, pWrite->rpcMsg.ahandle, - taosMsg[pWrite->rpcMsg.msgType], pWrite->pBatchMasterMsg); - return; - } - - SRpcMsg rpcRsp = { - .handle = pWrite->rpcMsg.handle, - .pCont = pWrite->rpcRsp.rsp, - .contLen = pWrite->rpcRsp.len, - .code = code, - }; - - rpcSendResponse(&rpcRsp); - dnodeFreeMWriteMsg(pWrite); -} - -static void *dnodeProcessMWriteQueue(void *param) { - SMnodeMsg *pWrite; - int32_t type; - void * unUsed; - - setThreadName("dnodeMWriteQ"); - - while (1) { - if (taosReadQitemFromQset(tsMWriteQset, &type, (void **)&pWrite, &unUsed) == 0) { - dDebug("qset:%p, mnode write got no message from qset, exiting", tsMWriteQset); - break; - } - - dTrace("msg:%p, app:%p type:%s will be processed in mwrite queue", pWrite, pWrite->rpcMsg.ahandle, - taosMsg[pWrite->rpcMsg.msgType]); - - int32_t code = mnodeProcessWrite(pWrite); - dnodeSendRpcMWriteRsp(pWrite, code); - } - - return NULL; -} - -void dnodeReprocessMWriteMsg(void *pMsg) { - SMnodeMsg *pWrite = pMsg; - - if (!mnodeIsRunning() || tsMWriteQueue == NULL) { - dDebug("msg:%p, app:%p type:%s is redirected for mnode not running, retry times:%d", pWrite, pWrite->rpcMsg.ahandle, - taosMsg[pWrite->rpcMsg.msgType], pWrite->retry); - - if (pWrite->pBatchMasterMsg) { - ++pWrite->pBatchMasterMsg->received; - if (pWrite->pBatchMasterMsg->successed + pWrite->pBatchMasterMsg->received - >= pWrite->pBatchMasterMsg->expected) { - dnodeSendRedirectMsg(&pWrite->pBatchMasterMsg->rpcMsg, true); - dnodeFreeMWriteMsg(pWrite->pBatchMasterMsg); - } - - mnodeDestroySubMsg(pWrite); - - return; - } - dnodeSendRedirectMsg(&pWrite->rpcMsg, true); - dnodeFreeMWriteMsg(pWrite); - } else { - dDebug("msg:%p, app:%p type:%s is reput into mwrite queue:%p, retry times:%d", pWrite, pWrite->rpcMsg.ahandle, - taosMsg[pWrite->rpcMsg.msgType], tsMWriteQueue, pWrite->retry); - - taosWriteQitem(tsMWriteQueue, TAOS_QTYPE_RPC, pWrite); - } -} - -static void dnodeDoDelayReprocessMWriteMsg(void *param, void *tmrId) { - dnodeReprocessMWriteMsg(param); -} - -void dnodeDelayReprocessMWriteMsg(void *pMsg) { - SMnodeMsg *mnodeMsg = pMsg; - void *unUsed = NULL; - taosTmrReset(dnodeDoDelayReprocessMWriteMsg, 300, mnodeMsg, tsDnodeTmr, &unUsed); -} diff --git a/src/dnode/src/dnodeMain.c b/src/dnode/src/dnodeMain.c deleted file mode 100644 index c6f6e976f65e66360e750ebdb5e056d79c8923a8..0000000000000000000000000000000000000000 --- a/src/dnode/src/dnodeMain.c +++ /dev/null @@ -1,337 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "taos.h" -#include "tnote.h" -#include "ttimer.h" -#include "tconfig.h" -#include "tfile.h" -#include "twal.h" -#include "tfs.h" -#include "tsync.h" -#include "dnodeStep.h" -#include "dnodePeer.h" -#include "dnodeModule.h" -#include "dnodeEps.h" -#include "dnodeMInfos.h" -#include "dnodeCfg.h" -#include "dnodeCheck.h" -#include "dnodeVRead.h" -#include "dnodeVWrite.h" -#include "dnodeVMgmt.h" -#include "dnodeVnodes.h" -#include "dnodeMRead.h" -#include "dnodeMWrite.h" -#include "dnodeMPeer.h" -#include "dnodeShell.h" -#include "dnodeTelemetry.h" -#include "module.h" -#include "mnode.h" -#include "qScript.h" -#include "tcache.h" -#include "tscompression.h" - -#if !defined(_MODULE) || !defined(_TD_LINUX) -int32_t moduleStart() { return 0; } -void moduleStop() {} -#endif - - -void *tsDnodeTmr = NULL; -static SRunStatus tsRunStatus = TSDB_RUN_STATUS_STOPPED; - -static int32_t dnodeInitStorage(); -static void dnodeCleanupStorage(); -static void dnodeSetRunStatus(SRunStatus status); -static void dnodeCheckDataDirOpenned(char *dir); -static int dnodeCreateDir(const char *dir); - -static SStep tsDnodeSteps[] = { - {"dnode-tfile", tfInit, tfCleanup}, - {"dnode-rpc", rpcInit, rpcCleanup}, - {"dnode-globalcfg", taosCheckGlobalCfg, NULL}, - {"dnode-storage", dnodeInitStorage, dnodeCleanupStorage}, - {"dnode-cfg", dnodeInitCfg, dnodeCleanupCfg}, - {"dnode-eps", dnodeInitEps, dnodeCleanupEps}, - {"dnode-minfos", dnodeInitMInfos, dnodeCleanupMInfos}, - {"dnode-wal", walInit, walCleanUp}, - {"dnode-sync", syncInit, syncCleanUp}, - {"dnode-check", dnodeInitCheck, dnodeCleanupCheck}, // NOTES: dnodeInitCheck must be behind the dnodeinitStorage component !!! - {"dnode-vread", dnodeInitVRead, dnodeCleanupVRead}, - {"dnode-vwrite", dnodeInitVWrite, dnodeCleanupVWrite}, - {"dnode-vmgmt", dnodeInitVMgmt, dnodeCleanupVMgmt}, - {"dnode-mread", dnodeInitMRead, NULL}, - {"dnode-mwrite", dnodeInitMWrite, NULL}, - {"dnode-mpeer", dnodeInitMPeer, NULL}, - {"dnode-client", dnodeInitClient, dnodeCleanupClient}, - {"dnode-server", dnodeInitServer, dnodeCleanupServer}, - {"dnode-vnodes", dnodeInitVnodes, dnodeCleanupVnodes}, - {"dnode-modules", dnodeInitModules, dnodeCleanupModules}, - {"dnode-mread", NULL, dnodeCleanupMRead}, - {"dnode-mwrite", NULL, dnodeCleanupMWrite}, - {"dnode-mpeer", NULL, dnodeCleanupMPeer}, - {"dnode-shell", dnodeInitShell, dnodeCleanupShell}, - {"dnode-statustmr", dnodeInitStatusTimer,dnodeCleanupStatusTimer}, - {"dnode-telemetry", dnodeInitTelemetry, dnodeCleanupTelemetry}, - {"dnode-script", scriptEnvPoolInit, scriptEnvPoolCleanup}, -}; - -static SStep tsDnodeCompactSteps[] = { - {"dnode-tfile", tfInit, tfCleanup}, - {"dnode-globalcfg", taosCheckGlobalCfg, NULL}, - {"dnode-storage", dnodeInitStorage, dnodeCleanupStorage}, - {"dnode-cfg", dnodeInitCfg, dnodeCleanupCfg}, - {"dnode-eps", dnodeInitEps, dnodeCleanupEps}, - {"dnode-minfos", dnodeInitMInfos, dnodeCleanupMInfos}, - {"dnode-wal", walInit, walCleanUp}, - {"dnode-sync", syncInit, syncCleanUp}, - {"dnode-vread", dnodeInitVRead, dnodeCleanupVRead}, - {"dnode-vwrite", dnodeInitVWrite, dnodeCleanupVWrite}, - {"dnode-vmgmt", dnodeInitVMgmt, dnodeCleanupVMgmt}, - {"dnode-mread", dnodeInitMRead, NULL}, - {"dnode-mwrite", dnodeInitMWrite, NULL}, - {"dnode-mpeer", dnodeInitMPeer, NULL}, - {"dnode-vnodes", dnodeInitVnodes, dnodeCleanupVnodes}, - {"dnode-modules", dnodeInitModules, dnodeCleanupModules}, - {"dnode-mread", NULL, dnodeCleanupMRead}, - {"dnode-mwrite", NULL, dnodeCleanupMWrite}, - {"dnode-mpeer", NULL, dnodeCleanupMPeer}, -}; - -static int dnodeCreateDir(const char *dir) { - if (mkdir(dir, 0755) != 0 && errno != EEXIST) { - return -1; - } - - return 0; -} - -static void dnodeCleanupComponents() { - if (!tsCompactMnodeWal) { - int32_t stepSize = sizeof(tsDnodeSteps) / sizeof(SStep); - dnodeStepCleanup(tsDnodeSteps, stepSize); - } else { - int32_t stepSize = sizeof(tsDnodeCompactSteps) / sizeof(SStep); - dnodeStepCleanup(tsDnodeCompactSteps, stepSize); - } -} - -static int32_t dnodeInitComponents() { - if (!tsCompactMnodeWal) { - int32_t stepSize = sizeof(tsDnodeSteps) / sizeof(SStep); - return dnodeStepInit(tsDnodeSteps, stepSize); - } else { - int32_t stepSize = sizeof(tsDnodeCompactSteps) / sizeof(SStep); - return dnodeStepInit(tsDnodeCompactSteps, stepSize); - } -} - -static int32_t dnodeInitTmr() { - tsDnodeTmr = taosTmrInit(100, 200, 60000, "DND-DM"); - if (tsDnodeTmr == NULL) { - dError("failed to init dnode timer"); - return -1; - } - - return 0; -} - -static void dnodeCleanupTmr() { - if (tsDnodeTmr != NULL) { - taosTmrCleanUp(tsDnodeTmr); - tsDnodeTmr = NULL; - } -} - -int32_t dnodeInitSystem() { - dnodeSetRunStatus(TSDB_RUN_STATUS_INITIALIZE); - tscEmbedded = 1; - taosIgnSIGPIPE(); - taosBlockSIGPIPE(); - taosResolveCRC(); - taosInitGlobalCfg(); - taosReadGlobalLogCfg(); - taosSetCoreDump(); - dnodeInitTmr(); - - if (dnodeCreateDir(tsLogDir) < 0) { - printf("failed to create dir: %s, reason: %s\n", tsLogDir, strerror(errno)); - return -1; - } - - char temp[TSDB_FILENAME_LEN]; - sprintf(temp, "%s/taosdlog", tsLogDir); - if (taosInitLog(temp, tsNumOfLogLines, 1) < 0) { - printf("failed to init log file\n"); - } - - if (!taosReadGlobalCfg()) { - taosPrintGlobalCfg(); - dError("TDengine read global config failed"); - return -1; - } - - dInfo("start to initialize TDengine"); - - taosInitNotes(); - - if (dnodeInitComponents() != 0) { - return -1; - } - - dnodeSetRunStatus(TSDB_RUN_STATUS_RUNING); - moduleStart(); - - tsDnodeStartTime = taosGetTimestampMs(); - dnodeReportStep("TDengine", "initialized successfully", 1); - dInfo("TDengine is initialized successfully"); - - return 0; -} - -void dnodeCleanUpSystem() { - if (dnodeGetRunStatus() != TSDB_RUN_STATUS_STOPPED) { - moduleStop(); - dnodeSetRunStatus(TSDB_RUN_STATUS_STOPPED); - dnodeCleanupTmr(); - dnodeCleanupComponents(); - taos_cleanup(); - taosCloseLog(); - taosStopCacheRefreshWorker(); - } -} - -SRunStatus dnodeGetRunStatus() { - return tsRunStatus; -} - -static void dnodeSetRunStatus(SRunStatus status) { - tsRunStatus = status; -} - -static void dnodeCheckDataDirOpenned(char *dir) { - char filepath[256] = {0}; - sprintf(filepath, "%s/.running", dir); - - int fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO); - if (fd < 0) { - dError("failed to open lock file:%s, reason: %s, quit", filepath, strerror(errno)); - exit(0); - } - int32_t ret = flock(fd, LOCK_EX | LOCK_NB); - if (ret != 0) { - dError("failed to lock file:%s ret:%d[%s], database may be running, quit", filepath, ret, strerror(errno)); - close(fd); - exit(0); - } -} - -static int32_t dnodeInitStorage() { -#ifdef TD_TSZ - // compress module init - tsCompressInit(); -#endif - - // storage module init - if (tsDiskCfgNum == 1 && dnodeCreateDir(tsDataDir) < 0) { - dError("failed to create dir: %s, reason: %s", tsDataDir, strerror(errno)); - return -1; - } - - if (tfsInit(tsDiskCfg, tsDiskCfgNum) < 0) { - dError("failed to init TFS since %s", tstrerror(terrno)); - return -1; - } - strncpy(tsDataDir, TFS_PRIMARY_PATH(), TSDB_FILENAME_LEN); - sprintf(tsMnodeDir, "%s/mnode", tsDataDir); - sprintf(tsVnodeDir, "%s/vnode", tsDataDir); - sprintf(tsDnodeDir, "%s/dnode", tsDataDir); - // sprintf(tsVnodeBakDir, "%s/vnode_bak", tsDataDir); - - if (tsCompactMnodeWal == 1) { - sprintf(tsMnodeTmpDir, "%s/mnode_tmp", tsDataDir); - if (taosDirExist(tsMnodeTmpDir)) { - dError("mnode_tmp dir already exist in %s,quit compact job", tsMnodeTmpDir); - return -1; - } - if (dnodeCreateDir(tsMnodeTmpDir) < 0) { - dError("failed to create dir: %s, reason: %s", tsMnodeTmpDir, strerror(errno)); - return -1; - } - - sprintf(tsMnodeBakDir, "%s/mnode_bak", tsDataDir); - if (taosDirExist(tsMnodeBakDir)) { - dError("mnode_bak dir already exist in %s,quit compact job", tsMnodeBakDir); - return -1; - } - } - //TODO(dengyihao): no need to init here - if (dnodeCreateDir(tsMnodeDir) < 0) { - dError("failed to create dir: %s, reason: %s", tsMnodeDir, strerror(errno)); - return -1; - } - - if (dnodeCreateDir(tsDnodeDir) < 0) { - dError("failed to create dir: %s, reason: %s", tsDnodeDir, strerror(errno)); - return -1; - } - - if (tfsMkdir("vnode") < 0) { - dError("failed to create vnode dir since %s", tstrerror(terrno)); - return -1; - } - - if (tfsMkdir("vnode_bak") < 0) { - dError("failed to create vnode_bak dir since %s", tstrerror(terrno)); - return -1; - } - - TDIR *tdir = tfsOpendir("vnode_bak/.staging"); - bool stagingNotEmpty = tfsReaddir(tdir) != NULL; - tfsClosedir(tdir); - - if (stagingNotEmpty) { - dError("vnode_bak/.staging dir not empty, fix it first."); - return -1; - } - - if (tfsMkdir("vnode_bak/.staging") < 0) { - dError("failed to create vnode_bak/.staging dir since %s", tstrerror(terrno)); - return -1; - } - - dnodeCheckDataDirOpenned(tsDnodeDir); - - taosGetDisk(); - taosPrintDiskInfo(); - dInfo("dnode storage is initialized at %s", tsDnodeDir); - return 0; -} - -static void dnodeCleanupStorage() { - // storage destroy - tfsDestroy(); - - #ifdef TD_TSZ - // compress destroy - tsCompressExit(); - #endif -} - -bool dnodeIsFirstDeploy() { - return strcmp(tsFirst, tsLocalEp) == 0; -} diff --git a/src/dnode/src/dnodeModule.c b/src/dnode/src/dnodeModule.c deleted file mode 100644 index a661585b3b39df986ac7866a255472e47e789fe6..0000000000000000000000000000000000000000 --- a/src/dnode/src/dnodeModule.c +++ /dev/null @@ -1,174 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "mnode.h" -#include "http.h" -#include "tmqtt.h" -#include "monitor.h" -#include "dnodeModule.h" - -typedef struct { - bool enable; - char * name; - int32_t (*initFp)(); - int32_t (*startFp)(); - void (*cleanUpFp)(); - void (*stopFp)(); -} SModule; - -static SModule tsModule[TSDB_MOD_MAX] = {{0}}; -static uint32_t tsModuleStatus = 0; - -static void dnodeSetModuleStatus(int32_t module) { - tsModuleStatus |= (1 << module); -} - -static void dnodeUnSetModuleStatus(int32_t module) { - tsModuleStatus &= ~(1 << module); -} - -static void dnodeAllocModules() { - tsModule[TSDB_MOD_MNODE].enable = false; - tsModule[TSDB_MOD_MNODE].name = "mnode"; - tsModule[TSDB_MOD_MNODE].initFp = mnodeInitSystem; - tsModule[TSDB_MOD_MNODE].cleanUpFp = mnodeCleanupSystem; - tsModule[TSDB_MOD_MNODE].startFp = mnodeStartSystem; - tsModule[TSDB_MOD_MNODE].stopFp = mnodeStopSystem; - - tsModule[TSDB_MOD_HTTP].enable = (tsEnableHttpModule == 1); - tsModule[TSDB_MOD_HTTP].name = "http"; - tsModule[TSDB_MOD_HTTP].initFp = httpInitSystem; - tsModule[TSDB_MOD_HTTP].cleanUpFp = httpCleanUpSystem; - tsModule[TSDB_MOD_HTTP].startFp = httpStartSystem; - tsModule[TSDB_MOD_HTTP].stopFp = httpStopSystem; - if (tsEnableHttpModule) { - dnodeSetModuleStatus(TSDB_MOD_HTTP); - } - -#ifdef _MQTT - tsModule[TSDB_MOD_MQTT].enable = (tsEnableMqttModule == 1); - tsModule[TSDB_MOD_MQTT].name = "mqtt"; - tsModule[TSDB_MOD_MQTT].initFp = mqttInitSystem; - tsModule[TSDB_MOD_MQTT].cleanUpFp = mqttCleanUpSystem; - tsModule[TSDB_MOD_MQTT].startFp = mqttStartSystem; - tsModule[TSDB_MOD_MQTT].stopFp = mqttStopSystem; - if (tsEnableMqttModule) { - dnodeSetModuleStatus(TSDB_MOD_MQTT); - } -#endif - - tsModule[TSDB_MOD_MONITOR].enable = (tsEnableMonitorModule == 1); - tsModule[TSDB_MOD_MONITOR].name = "monitor"; - tsModule[TSDB_MOD_MONITOR].initFp = monInitSystem; - tsModule[TSDB_MOD_MONITOR].cleanUpFp = monCleanupSystem; - tsModule[TSDB_MOD_MONITOR].startFp = monStartSystem; - tsModule[TSDB_MOD_MONITOR].stopFp = monStopSystem; - if (tsEnableMonitorModule) { - dnodeSetModuleStatus(TSDB_MOD_MONITOR); - } -} - -void dnodeCleanupModules() { - for (EModuleType module = 1; module < TSDB_MOD_MAX; ++module) { - if (tsModule[module].enable && tsModule[module].stopFp) { - (*tsModule[module].stopFp)(); - } - if (tsModule[module].cleanUpFp) { - (*tsModule[module].cleanUpFp)(); - } - } - - if (tsModule[TSDB_MOD_MNODE].cleanUpFp) { - (*tsModule[TSDB_MOD_MNODE].cleanUpFp)(); - } -} - -static int32_t dnodeStartModules() { - for (EModuleType module = 1; module < TSDB_MOD_MAX; ++module) { - if (tsModule[module].enable && tsModule[module].startFp) { - int32_t code = (*tsModule[module].startFp)(); - if (code != 0) { - dError("failed to start module:%s, code:%d", tsModule[module].name, code); - return code; - } - } - } - - return 0; -} - -int32_t dnodeInitModules() { - dnodeAllocModules(); - - for (EModuleType module = 0; module < TSDB_MOD_MAX; ++module) { - if (tsModule[module].initFp) { - if ((*tsModule[module].initFp)() != 0) { - dError("failed to init module:%s", tsModule[module].name); - return -1; - } - } - } - - dInfo("dnode modules is initialized"); - return dnodeStartModules(); -} - -int32_t dnodeProcessModuleStatus(uint32_t moduleStatus) { - int32_t code = 0; - - for (int32_t module = TSDB_MOD_MNODE; module < TSDB_MOD_HTTP; ++module) { - bool enableModule = moduleStatus & (1 << module); - if (!tsModule[module].enable && enableModule) { - dInfo("module status:%u is set, start %s module", moduleStatus, tsModule[module].name); - tsModule[module].enable = true; - dnodeSetModuleStatus(module); - code = (*tsModule[module].startFp)(); - } - - if (tsModule[module].enable && !enableModule) { - dInfo("module status:%u is set, stop %s module", moduleStatus, tsModule[module].name); - tsModule[module].enable = false; - dnodeUnSetModuleStatus(module); - (*tsModule[module].stopFp)(); - } - } - - return code; -} - -int32_t dnodeStartMnode(SMInfos *pMinfos) { - if (tsModuleStatus & (1 << TSDB_MOD_MNODE)) { - dDebug("mnode module is already started, module status:%d", tsModuleStatus); - return 0; - } - - uint32_t moduleStatus = tsModuleStatus | (1 << TSDB_MOD_MNODE); - dInfo("start mnode module, module status:%d, new status:%d", tsModuleStatus, moduleStatus); - - int32_t code = dnodeProcessModuleStatus(moduleStatus); - if (code == 0) { - code = sdbUpdateSync(pMinfos); - } - - if (code != 0) { - dError("failed to start mnode module since %s", tstrerror(code)); - moduleStatus = tsModuleStatus & ~(1 << TSDB_MOD_MNODE); - dnodeProcessModuleStatus(moduleStatus); - } - - return code; -} diff --git a/src/dnode/src/dnodePeer.c b/src/dnode/src/dnodePeer.c deleted file mode 100644 index 08269c0bf6141974366936660bee326682cd90f5..0000000000000000000000000000000000000000 --- a/src/dnode/src/dnodePeer.c +++ /dev/null @@ -1,195 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -/* this file is mainly responsible for the communication between DNODEs. Each - * dnode works as both server and client. Dnode may send status, grant, config - * messages to mnode, mnode may send create/alter/drop table/vnode messages - * to dnode. All theses messages are handled from here - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "mnode.h" -#include "dnodeVMgmt.h" -#include "dnodeVWrite.h" -#include "dnodeMPeer.h" -#include "dnodeMInfos.h" -#include "dnodeStep.h" - -static void (*dnodeProcessReqMsgFp[TSDB_MSG_TYPE_MAX])(SRpcMsg *); -static void dnodeProcessReqMsgFromDnode(SRpcMsg *pMsg, SRpcEpSet *); -static void (*dnodeProcessRspMsgFp[TSDB_MSG_TYPE_MAX])(SRpcMsg *rpcMsg); -static void dnodeProcessRspFromDnode(SRpcMsg *pMsg, SRpcEpSet *pEpSet); -static void *tsServerRpc = NULL; -static void *tsClientRpc = NULL; - -int32_t dnodeInitServer() { - dnodeProcessReqMsgFp[TSDB_MSG_TYPE_MD_CREATE_TABLE] = dnodeDispatchToVWriteQueue; - dnodeProcessReqMsgFp[TSDB_MSG_TYPE_MD_DROP_TABLE] = dnodeDispatchToVWriteQueue; - dnodeProcessReqMsgFp[TSDB_MSG_TYPE_MD_ALTER_TABLE] = dnodeDispatchToVWriteQueue; - dnodeProcessReqMsgFp[TSDB_MSG_TYPE_MD_DROP_STABLE] = dnodeDispatchToVWriteQueue; - - dnodeProcessReqMsgFp[TSDB_MSG_TYPE_MD_CREATE_VNODE] = dnodeDispatchToVMgmtQueue; - dnodeProcessReqMsgFp[TSDB_MSG_TYPE_MD_ALTER_VNODE] = dnodeDispatchToVMgmtQueue; - dnodeProcessReqMsgFp[TSDB_MSG_TYPE_MD_SYNC_VNODE] = dnodeDispatchToVMgmtQueue; - dnodeProcessReqMsgFp[TSDB_MSG_TYPE_MD_DROP_VNODE] = dnodeDispatchToVMgmtQueue; - dnodeProcessReqMsgFp[TSDB_MSG_TYPE_MD_ALTER_STREAM] = dnodeDispatchToVMgmtQueue; - dnodeProcessReqMsgFp[TSDB_MSG_TYPE_MD_CONFIG_DNODE] = dnodeDispatchToVMgmtQueue; - dnodeProcessReqMsgFp[TSDB_MSG_TYPE_MD_CREATE_MNODE] = dnodeDispatchToVMgmtQueue; - dnodeProcessReqMsgFp[TSDB_MSG_TYPE_MD_COMPACT_VNODE] = dnodeDispatchToVMgmtQueue; - - dnodeProcessReqMsgFp[TSDB_MSG_TYPE_DM_CONFIG_TABLE] = dnodeDispatchToMPeerQueue; - dnodeProcessReqMsgFp[TSDB_MSG_TYPE_DM_CONFIG_VNODE] = dnodeDispatchToMPeerQueue; - dnodeProcessReqMsgFp[TSDB_MSG_TYPE_DM_AUTH] = dnodeDispatchToMPeerQueue; - dnodeProcessReqMsgFp[TSDB_MSG_TYPE_DM_GRANT] = dnodeDispatchToMPeerQueue; - dnodeProcessReqMsgFp[TSDB_MSG_TYPE_DM_STATUS] = dnodeDispatchToMPeerQueue; - - SRpcInit rpcInit; - memset(&rpcInit, 0, sizeof(rpcInit)); - rpcInit.localPort = tsDnodeDnodePort; - rpcInit.label = "DND-S"; - rpcInit.numOfThreads = 1; - rpcInit.cfp = dnodeProcessReqMsgFromDnode; - rpcInit.sessions = TSDB_MAX_VNODES << 4; - rpcInit.connType = TAOS_CONN_SERVER; - rpcInit.idleTime = tsShellActivityTimer * 1000; - - tsServerRpc = rpcOpen(&rpcInit); - if (tsServerRpc == NULL) { - dError("failed to init inter-dnodes RPC server"); - return -1; - } - - dInfo("dnode inter-dnodes RPC server is initialized"); - return 0; -} - -void dnodeCleanupServer() { - if (tsServerRpc) { - rpcClose(tsServerRpc); - tsServerRpc = NULL; - dInfo("inter-dnodes RPC server is closed"); - } -} - -static void dnodeProcessReqMsgFromDnode(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { - SRpcMsg rspMsg = { - .handle = pMsg->handle, - .pCont = NULL, - .contLen = 0 - }; - - if (pMsg->pCont == NULL) return; - if (pMsg->msgType == TSDB_MSG_TYPE_NETWORK_TEST) { - dnodeSendStartupStep(pMsg); - return; - } - - if (dnodeGetRunStatus() != TSDB_RUN_STATUS_RUNING) { - rspMsg.code = TSDB_CODE_APP_NOT_READY; - rpcSendResponse(&rspMsg); - rpcFreeCont(pMsg->pCont); - dTrace("RPC %p, msg:%s is ignored since dnode not running", pMsg->handle, taosMsg[pMsg->msgType]); - return; - } - - if (pMsg->pCont == NULL) { - rspMsg.code = TSDB_CODE_DND_INVALID_MSG_LEN; - rpcSendResponse(&rspMsg); - return; - } - - if (dnodeProcessReqMsgFp[pMsg->msgType]) { - (*dnodeProcessReqMsgFp[pMsg->msgType])(pMsg); - } else { - dDebug("RPC %p, message:%s not processed", pMsg->handle, taosMsg[pMsg->msgType]); - rspMsg.code = TSDB_CODE_DND_MSG_NOT_PROCESSED; - rpcSendResponse(&rspMsg); - rpcFreeCont(pMsg->pCont); - } -} - -int32_t dnodeInitClient() { - char secret[TSDB_KEY_LEN] = "secret"; - SRpcInit rpcInit; - memset(&rpcInit, 0, sizeof(rpcInit)); - rpcInit.label = "DND-C"; - rpcInit.numOfThreads = 1; - rpcInit.cfp = dnodeProcessRspFromDnode; - rpcInit.sessions = TSDB_MAX_VNODES << 4; - rpcInit.connType = TAOS_CONN_CLIENT; - rpcInit.idleTime = tsShellActivityTimer * 1000; - rpcInit.user = "t"; - rpcInit.ckey = "key"; - rpcInit.secret = secret; - - tsClientRpc = rpcOpen(&rpcInit); - if (tsClientRpc == NULL) { - dError("failed to init mnode rpc client"); - return -1; - } - - dInfo("dnode inter-dnodes rpc client is initialized"); - return 0; -} - -void dnodeCleanupClient() { - if (tsClientRpc) { - rpcClose(tsClientRpc); - tsClientRpc = NULL; - dInfo("dnode inter-dnodes rpc client is closed"); - } -} - -static void dnodeProcessRspFromDnode(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { - if (dnodeGetRunStatus() == TSDB_RUN_STATUS_STOPPED) { - if (pMsg == NULL || pMsg->pCont == NULL) return; - dTrace("msg:%p is ignored since dnode is stopping", pMsg); - rpcFreeCont(pMsg->pCont); - return; - } - - if (pMsg->msgType == TSDB_MSG_TYPE_DM_STATUS_RSP && pEpSet) { - dnodeUpdateEpSetForPeer(pEpSet); - } - - if (dnodeProcessRspMsgFp[pMsg->msgType]) { - (*dnodeProcessRspMsgFp[pMsg->msgType])(pMsg); - } else { - mnodeProcessPeerRsp(pMsg); - } - - rpcFreeCont(pMsg->pCont); -} - -void dnodeAddClientRspHandle(uint8_t msgType, void (*fp)(SRpcMsg *rpcMsg)) { - dnodeProcessRspMsgFp[msgType] = fp; -} - -void dnodeSendMsgToDnode(SRpcEpSet *epSet, SRpcMsg *rpcMsg) { - rpcSendRequest(tsClientRpc, epSet, rpcMsg, NULL); -} - -void dnodeSendMsgToMnodeRecv(SRpcMsg *rpcMsg, SRpcMsg *rpcRsp) { - SRpcEpSet epSet = {0}; - dnodeGetEpSetForPeer(&epSet); - - assert(tsClientRpc != 0); - rpcSendRecv(tsClientRpc, &epSet, rpcMsg, rpcRsp); -} - -void dnodeSendMsgToDnodeRecv(SRpcMsg *rpcMsg, SRpcMsg *rpcRsp, SRpcEpSet *epSet) { - rpcSendRecv(tsClientRpc, epSet, rpcMsg, rpcRsp); -} diff --git a/src/dnode/src/dnodeShell.c b/src/dnode/src/dnodeShell.c deleted file mode 100644 index 5606681f0f931070e9cbf21d6b98b0d2eb51bdfa..0000000000000000000000000000000000000000 --- a/src/dnode/src/dnodeShell.c +++ /dev/null @@ -1,249 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "http.h" -#include "mnode.h" -#include "dnodeVRead.h" -#include "dnodeVWrite.h" -#include "dnodeMRead.h" -#include "dnodeMWrite.h" -#include "dnodeShell.h" -#include "dnodeStep.h" - -static void (*dnodeProcessShellMsgFp[TSDB_MSG_TYPE_MAX])(SRpcMsg *); -static void dnodeProcessMsgFromShell(SRpcMsg *pMsg, SRpcEpSet *); -static int dnodeRetrieveUserAuthInfo(char *user, char *spi, char *encrypt, char *secret, char *ckey); -static void * tsShellRpc = NULL; -static int32_t tsQueryReqNum = 0; -static int32_t tsSubmitReqNum = 0; - -int32_t dnodeInitShell() { - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_SUBMIT] = dnodeDispatchToVWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_QUERY] = dnodeDispatchToVReadQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_FETCH] = dnodeDispatchToVReadQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_UPDATE_TAG_VAL] = dnodeDispatchToVWriteQueue; - - // the following message shall be treated as mnode write - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_CREATE_ACCT] = dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_ALTER_ACCT] = dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_DROP_ACCT] = dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_CREATE_USER] = dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_ALTER_USER] = dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_DROP_USER] = dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_CREATE_DNODE]= dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_DROP_DNODE] = dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_CREATE_DB] = dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_CREATE_TP] = dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_CREATE_FUNCTION] = dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_DROP_DB] = dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_SYNC_DB] = dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_DROP_TP] = dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_DROP_FUNCTION] = dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_ALTER_DB] = dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_ALTER_TP] = dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_CREATE_TABLE]= dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_DROP_TABLE] = dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_ALTER_TABLE] = dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_ALTER_STREAM]= dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_KILL_QUERY] = dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_KILL_STREAM] = dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_KILL_CONN] = dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_CONFIG_DNODE]= dnodeDispatchToMWriteQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_COMPACT_VNODE]= dnodeDispatchToMWriteQueue; - - // the following message shall be treated as mnode query - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_HEARTBEAT] = dnodeDispatchToMReadQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_CONNECT] = dnodeDispatchToMReadQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_USE_DB] = dnodeDispatchToMReadQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_TABLE_META] = dnodeDispatchToMReadQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_STABLE_VGROUP]= dnodeDispatchToMReadQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_TABLES_META] = dnodeDispatchToMReadQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_SHOW] = dnodeDispatchToMReadQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_RETRIEVE] = dnodeDispatchToMReadQueue; - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_CM_RETRIEVE_FUNC] = dnodeDispatchToMReadQueue; - - dnodeProcessShellMsgFp[TSDB_MSG_TYPE_NETWORK_TEST] = dnodeSendStartupStep; - - int32_t numOfThreads = (int32_t)((tsNumOfCores * tsNumOfThreadsPerCore) / 2.0); - if (numOfThreads < 1) { - numOfThreads = 1; - } - - SRpcInit rpcInit; - memset(&rpcInit, 0, sizeof(rpcInit)); - rpcInit.localPort = tsDnodeShellPort; - rpcInit.label = "SHELL"; - rpcInit.numOfThreads = numOfThreads; - rpcInit.cfp = dnodeProcessMsgFromShell; - rpcInit.sessions = tsMaxShellConns; - rpcInit.connType = TAOS_CONN_SERVER; - rpcInit.idleTime = tsShellActivityTimer * 1000; - rpcInit.afp = dnodeRetrieveUserAuthInfo; - - tsShellRpc = rpcOpen(&rpcInit); - if (tsShellRpc == NULL) { - dError("failed to init shell rpc server"); - return -1; - } - - dInfo("dnode shell rpc server is initialized"); - return 0; -} - -void dnodeCleanupShell() { - if (tsShellRpc) { - rpcClose(tsShellRpc); - tsShellRpc = NULL; - } -} - -static void dnodeProcessMsgFromShell(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { - SRpcMsg rpcMsg = { - .handle = pMsg->handle, - .pCont = NULL, - .contLen = 0 - }; - - if (pMsg->pCont == NULL) return; - - SRunStatus dnodeStatus = dnodeGetRunStatus(); - if (dnodeStatus == TSDB_RUN_STATUS_STOPPED) { - dError("RPC %p, shell msg:%s is ignored since dnode exiting", pMsg->handle, taosMsg[pMsg->msgType]); - rpcMsg.code = TSDB_CODE_DND_EXITING; - rpcSendResponse(&rpcMsg); - rpcFreeCont(pMsg->pCont); - return; - } else if (dnodeStatus != TSDB_RUN_STATUS_RUNING) { - dError("RPC %p, shell msg:%s is ignored since dnode not running", pMsg->handle, taosMsg[pMsg->msgType]); - rpcMsg.code = TSDB_CODE_APP_NOT_READY; - rpcSendResponse(&rpcMsg); - rpcFreeCont(pMsg->pCont); - return; - } - - if (pMsg->msgType == TSDB_MSG_TYPE_QUERY) { - atomic_fetch_add_32(&tsQueryReqNum, 1); - } else if (pMsg->msgType == TSDB_MSG_TYPE_SUBMIT) { - atomic_fetch_add_32(&tsSubmitReqNum, 1); - } else {} - - if ( dnodeProcessShellMsgFp[pMsg->msgType] ) { - (*dnodeProcessShellMsgFp[pMsg->msgType])(pMsg); - } else { - dError("RPC %p, shell msg:%s is not processed", pMsg->handle, taosMsg[pMsg->msgType]); - rpcMsg.code = TSDB_CODE_DND_MSG_NOT_PROCESSED; - rpcSendResponse(&rpcMsg); - rpcFreeCont(pMsg->pCont); - return; - } -} - -static int32_t dnodeAuthNettestUser(char *user, char *spi, char *encrypt, char *secret, char *ckey) { - if (strcmp(user, "nettestinternal") == 0) { - char pass[32] = {0}; - taosEncryptPass((uint8_t *)user, strlen(user), pass); - *spi = 0; - *encrypt = 0; - *ckey = 0; - memcpy(secret, pass, TSDB_KEY_LEN); - dTrace("nettest user is authorized"); - return 0; - } - - return -1; -} - -static int dnodeRetrieveUserAuthInfo(char *user, char *spi, char *encrypt, char *secret, char *ckey) { - if (dnodeAuthNettestUser(user, spi, encrypt, secret, ckey) == 0) return 0; - int code = mnodeRetriveAuth(user, spi, encrypt, secret, ckey); - if (code != TSDB_CODE_APP_NOT_READY) return code; - - SAuthMsg *pMsg = rpcMallocCont(sizeof(SAuthMsg)); - tstrncpy(pMsg->user, user, sizeof(pMsg->user)); - - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pMsg; - rpcMsg.contLen = sizeof(SAuthMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_DM_AUTH; - - dDebug("user:%s, send auth msg to mnodes", user); - SRpcMsg rpcRsp = {0}; - dnodeSendMsgToMnodeRecv(&rpcMsg, &rpcRsp); - - if (rpcRsp.code != 0) { - dError("user:%s, auth msg received from mnodes, error:%s", user, tstrerror(rpcRsp.code)); - } else { - SAuthRsp *pRsp = rpcRsp.pCont; - dDebug("user:%s, auth msg received from mnodes", user); - memcpy(secret, pRsp->secret, TSDB_KEY_LEN); - memcpy(ckey, pRsp->ckey, TSDB_KEY_LEN); - *spi = pRsp->spi; - *encrypt = pRsp->encrypt; - } - - rpcFreeCont(rpcRsp.pCont); - return rpcRsp.code; -} - -void *dnodeSendCfgTableToRecv(int32_t vgId, int32_t tid) { - dDebug("vgId:%d, tid:%d send config table msg to mnode", vgId, tid); - - int32_t contLen = sizeof(SConfigTableMsg); - SConfigTableMsg *pMsg = rpcMallocCont(contLen); - - pMsg->dnodeId = htonl(dnodeGetDnodeId()); - pMsg->vgId = htonl(vgId); - pMsg->tid = htonl(tid); - - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pMsg; - rpcMsg.contLen = contLen; - rpcMsg.msgType = TSDB_MSG_TYPE_DM_CONFIG_TABLE; - - SRpcMsg rpcRsp = {0}; - dnodeSendMsgToMnodeRecv(&rpcMsg, &rpcRsp); - terrno = rpcRsp.code; - - if (rpcRsp.code != 0) { - rpcFreeCont(rpcRsp.pCont); - dError("vgId:%d, tid:%d failed to config table from mnode", vgId, tid); - return NULL; - } else { - dInfo("vgId:%d, tid:%d config table msg is received", vgId, tid); - - // delete this after debug finished - SMDCreateTableMsg *pTable = rpcRsp.pCont; - int16_t numOfColumns = htons(pTable->numOfColumns); - int16_t numOfTags = htons(pTable->numOfTags); - int32_t tableId = htonl(pTable->tid); - uint64_t uid = htobe64(pTable->uid); - dInfo("table:%s, numOfColumns:%d numOfTags:%d tid:%d uid:%" PRIu64, pTable->tableFname, numOfColumns, numOfTags, tableId, uid); - - return rpcRsp.pCont; - } -} - -SStatisInfo dnodeGetStatisInfo() { - SStatisInfo info = {0}; - if (dnodeGetRunStatus() == TSDB_RUN_STATUS_RUNING) { - info.httpReqNum = httpGetReqCount(); - info.queryReqNum = atomic_exchange_32(&tsQueryReqNum, 0); - info.submitReqNum = atomic_exchange_32(&tsSubmitReqNum, 0); - } - - return info; -} diff --git a/src/dnode/src/dnodeStep.c b/src/dnode/src/dnodeStep.c deleted file mode 100644 index eda6fb227dd6bf29499933383bb82f83ec851ee4..0000000000000000000000000000000000000000 --- a/src/dnode/src/dnodeStep.c +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "dnodeStep.h" - -static SStartupStep tsStartupStep; - -void dnodeReportStep(char *name, char *desc, int8_t finished) { - tstrncpy(tsStartupStep.name, name, sizeof(tsStartupStep.name)); - tstrncpy(tsStartupStep.desc, desc, sizeof(tsStartupStep.desc)); - tsStartupStep.finished = finished; -} - -void dnodeSendStartupStep(SRpcMsg *pMsg) { - dInfo("nettest msg is received, cont:%s", (char *)pMsg->pCont); - - SStartupStep *pStep = rpcMallocCont(sizeof(SStartupStep)); - memcpy(pStep, &tsStartupStep, sizeof(SStartupStep)); - - dDebug("startup msg is sent, step:%s desc:%s finished:%d", pStep->name, pStep->desc, pStep->finished); - - SRpcMsg rpcRsp = {.handle = pMsg->handle, .pCont = pStep, .contLen = sizeof(SStartupStep)}; - rpcSendResponse(&rpcRsp); - rpcFreeCont(pMsg->pCont); -} - -void taosStepCleanupImp(SStep *pSteps, int32_t stepId) { - for (int32_t step = stepId; step >= 0; step--) { - SStep *pStep = pSteps + step; - dDebug("step:%s will cleanup", pStep->name); - if (pStep->cleanupFp != NULL) { - (*pStep->cleanupFp)(); - } - } -} - -int32_t dnodeStepInit(SStep *pSteps, int32_t stepSize) { - for (int32_t step = 0; step < stepSize; step++) { - SStep *pStep = pSteps + step; - if (pStep->initFp == NULL) continue; - - dnodeReportStep(pStep->name, "Start initialization", 0); - - int32_t code = (*pStep->initFp)(); - if (code != 0) { - dDebug("step:%s will cleanup", pStep->name); - taosStepCleanupImp(pSteps, step); - return code; - } - dInfo("step:%s is initialized", pStep->name); - - dnodeReportStep(pStep->name, "Initialization complete", 0); - } - - return 0; -} - -void dnodeStepCleanup(SStep *pSteps, int32_t stepSize) { - taosStepCleanupImp(pSteps, stepSize - 1); -} diff --git a/src/dnode/src/dnodeSystem.c b/src/dnode/src/dnodeSystem.c deleted file mode 100644 index 2f77788025e6d5f36460ceb866b64d54736af6a1..0000000000000000000000000000000000000000 --- a/src/dnode/src/dnodeSystem.c +++ /dev/null @@ -1,188 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "tgrant.h" -#include "tconfig.h" -#include "dnodeMain.h" - -static tsem_t exitSem; -static void siguser1Handler(int32_t signum, void *sigInfo, void *context); -static void siguser2Handler(int32_t signum, void *sigInfo, void *context); -static void sigintHandler(int32_t signum, void *sigInfo, void *context); - -int32_t main(int32_t argc, char *argv[]) { - int dump_config = 0; - - // Set global configuration file - for (int32_t i = 1; i < argc; ++i) { - if (strcmp(argv[i], "-c") == 0) { - if (i < argc - 1) { - if (strlen(argv[++i]) >= TSDB_FILENAME_LEN) { - printf("config file path overflow"); - exit(EXIT_FAILURE); - } - tstrncpy(configDir, argv[i], TSDB_FILENAME_LEN); - } else { - printf("'-c' requires a parameter, default:%s\n", configDir); - exit(EXIT_FAILURE); - } - } else if (strcmp(argv[i], "-C") == 0) { - dump_config = 1; - } else if (strcmp(argv[i], "--force-keep-file") == 0) { - tsdbForceKeepFile = true; - } else if (strcmp(argv[i], "--compact-mnode-wal") == 0) { - tsCompactMnodeWal = 1; - } else if (strcmp(argv[i], "-V") == 0) { -#ifdef _ACCT - char *versionStr = "enterprise"; -#else - char *versionStr = "community"; -#endif - printf("%s version: %s compatible_version: %s\n", versionStr, version, compatible_version); - printf("gitinfo: %s\n", gitinfo); - printf("gitinfoI: %s\n", gitinfoOfInternal); - printf("buildinfo: %s\n", buildinfo); - exit(EXIT_SUCCESS); - } else if (strcmp(argv[i], "-k") == 0) { - grantParseParameter(); - exit(EXIT_SUCCESS); - } else if (strcmp(argv[i], "-A") == 0) { - tsPrintAuth = 1; - } -#ifdef TAOS_MEM_CHECK - else if (strcmp(argv[i], "--alloc-random-fail") == 0) { - if ((i < argc - 1) && (argv[i + 1][0] != '-')) { - taosSetAllocMode(TAOS_ALLOC_MODE_RANDOM_FAIL, argv[++i], true); - } else { - taosSetAllocMode(TAOS_ALLOC_MODE_RANDOM_FAIL, NULL, true); - } - } else if (strcmp(argv[i], "--detect-mem-leak") == 0) { - if ((i < argc - 1) && (argv[i + 1][0] != '-')) { - taosSetAllocMode(TAOS_ALLOC_MODE_DETECT_LEAK, argv[++i], true); - } else { - taosSetAllocMode(TAOS_ALLOC_MODE_DETECT_LEAK, NULL, true); - } - } -#endif -#ifdef TAOS_RANDOM_FILE_FAIL - else if (strcmp(argv[i], "--random-file-fail-output") == 0) { - if ((i < argc - 1) && (argv[i + 1][0] != '-')) { - taosSetRandomFileFailOutput(argv[++i]); - } else { - taosSetRandomFileFailOutput(NULL); - } - } else if (strcmp(argv[i], "--random-file-fail-factor") == 0) { - if ((i + 1) < argc) { - int factor = atoi(argv[i + 1]); - printf("The factor of random failure is %d\n", factor); - taosSetRandomFileFailFactor(factor); - } else { - printf("Please specify a number for random failure factor!"); - exit(EXIT_FAILURE); - } - } -#endif - } - - if (0 != dump_config) { - tscEmbedded = 1; - taosInitGlobalCfg(); - taosReadGlobalLogCfg(); - - if (!taosReadGlobalCfg()) { - printf("TDengine read global config failed"); - exit(EXIT_FAILURE); - } - - taosDumpGlobalCfg(); - exit(EXIT_SUCCESS); - } - - if (tsem_init(&exitSem, 0, 0) != 0) { - printf("failed to create exit semphore\n"); - exit(EXIT_FAILURE); - } - - /* Set termination handler. */ - taosSetSignal(SIGUSR1, siguser1Handler); - taosSetSignal(SIGUSR2, siguser2Handler); - taosSetSignal(SIGTERM, sigintHandler); - taosSetSignal(SIGHUP, sigintHandler); - taosSetSignal(SIGINT, sigintHandler); - taosSetSignal(SIGABRT, sigintHandler); - taosSetSignal(SIGBREAK, sigintHandler); - - // Open /var/log/syslog file to record information. - openlog("TDengine:", LOG_PID | LOG_CONS | LOG_NDELAY, LOG_LOCAL1); - syslog(LOG_INFO, "Starting TDengine service..."); - - // Initialize the system - if (dnodeInitSystem() < 0) { - syslog(LOG_ERR, "Error initialize TDengine system"); - dInfo("Failed to start TDengine, please check the log at:%s", tsLogDir); - closelog(); - exit(EXIT_FAILURE); - } - - syslog(LOG_INFO, "Started TDengine service successfully."); - - if (tsem_wait(&exitSem) != 0) { - syslog(LOG_ERR, "failed to wait exit semphore: %s", strerror(errno)); - } - - dnodeCleanUpSystem(); - // close the syslog - syslog(LOG_INFO, "Shut down TDengine service successfully"); - dInfo("TDengine is shut down!"); - closelog(); - -#ifdef WINDOWS - tsem_post(&exitSem); -#endif - return EXIT_SUCCESS; -} - -static void siguser1Handler(int32_t signum, void *sigInfo, void *context) { taosCfgDynamicOptions("debugFlag 143"); } - -static void siguser2Handler(int32_t signum, void *sigInfo, void *context) { taosCfgDynamicOptions("resetlog"); } - -static void sigintHandler(int32_t signum, void *sigInfo, void *context) { - // protect the application from receive another signal - taosIgnSignal(SIGUSR1); - taosIgnSignal(SIGUSR2); - taosIgnSignal(SIGTERM); - taosIgnSignal(SIGHUP); - taosIgnSignal(SIGINT); - taosIgnSignal(SIGABRT); - taosIgnSignal(SIGBREAK); - - // clean the system. - dInfo("shut down signal is %d", signum); - -#ifndef WINDOWS - dInfo("sender PID:%d cmdline:%s", ((siginfo_t *)sigInfo)->si_pid, taosGetCmdlineByPID(((siginfo_t *)sigInfo)->si_pid)); -#endif - - syslog(LOG_INFO, "Shut down signal is %d", signum); - syslog(LOG_INFO, "Shutting down TDengine service..."); - - // inform main thread to exit - tsem_post(&exitSem); -#ifdef WINDOWS - tsem_wait(&exitSem); -#endif -} diff --git a/src/dnode/src/dnodeTelemetry.c b/src/dnode/src/dnodeTelemetry.c deleted file mode 100644 index 22a6dc5b1993b6d15510b078ac4245909221ae78..0000000000000000000000000000000000000000 --- a/src/dnode/src/dnodeTelemetry.c +++ /dev/null @@ -1,315 +0,0 @@ -/* - * Copyright (c) 2020 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "osTime.h" -#include "tsocket.h" -#include "tbuffer.h" -#include "mnode.h" -#include "mnodeDef.h" -#include "mnodeDb.h" -#include "mnodeDnode.h" -#include "mnodeCluster.h" -#include "mnodeDnode.h" -#include "mnodeVgroup.h" -#include "mnodeMnode.h" -#include "mnodeTable.h" -#include "mnodeSdb.h" -#include "mnodeAcct.h" -#include "dnodeTelemetry.h" - -// sem_timedwait is NOT implemented on MacOSX -// thus, we use pthread_mutex_t/pthread_cond_t to simulate -static pthread_mutex_t tsExitLock = PTHREAD_MUTEX_INITIALIZER; -static pthread_cond_t tsExitCond = PTHREAD_COND_INITIALIZER; -static volatile int tsExit = 0; -static pthread_t tsTelemetryThread; - -#define TELEMETRY_SERVER "telemetry.taosdata.com" -#define TELEMETRY_PORT 80 -#define REPORT_INTERVAL 86400 - -static void beginObject(SBufferWriter* bw) { - tbufWriteChar(bw, '{'); -} - -static void closeObject(SBufferWriter* bw) { - size_t len = tbufTell(bw); - if (tbufGetData(bw, false)[len - 1] == ',') { - tbufWriteCharAt(bw, len - 1, '}'); - } else { - tbufWriteChar(bw, '}'); - } - tbufWriteChar(bw, ','); -} - -#if 0 -static void beginArray(SBufferWriter* bw) { - tbufWriteChar(bw, '['); -} - -static void closeArray(SBufferWriter* bw) { - size_t len = tbufTell(bw); - if (tbufGetData(bw, false)[len - 1] == ',') { - tbufWriteCharAt(bw, len - 1, ']'); - } else { - tbufWriteChar(bw, ']'); - } - tbufWriteChar(bw, ','); -} -#endif - -static void writeString(SBufferWriter* bw, const char* str) { - tbufWriteChar(bw, '"'); - tbufWrite(bw, str, strlen(str)); - tbufWriteChar(bw, '"'); -} - -static void addIntField(SBufferWriter* bw, const char* k, int64_t v) { - writeString(bw, k); - tbufWriteChar(bw, ':'); - char buf[32]; - sprintf(buf, "%" PRId64, v); - tbufWrite(bw, buf, strlen(buf)); - tbufWriteChar(bw, ','); -} - -static void addStringField(SBufferWriter* bw, const char* k, const char* v) { - writeString(bw, k); - tbufWriteChar(bw, ':'); - writeString(bw, v); - tbufWriteChar(bw, ','); -} - -static void addCpuInfo(SBufferWriter* bw) { - char * line = NULL; - size_t size = 0; - int32_t done = 0; - - FILE* fp = fopen("/proc/cpuinfo", "r"); - if (fp == NULL) { - return; - } - - while (done != 3 && (size = tgetline(&line, &size, fp)) != -1) { - line[size - 1] = '\0'; - if (((done&1) == 0) && strncmp(line, "model name", 10) == 0) { - const char* v = strchr(line, ':') + 2; - addStringField(bw, "cpuModel", v); - done |= 1; - } else if (((done&2)==0) && strncmp(line, "cpu cores", 9) == 0) { - const char* v = strchr(line, ':') + 2; - writeString(bw, "numOfCpu"); - tbufWriteChar(bw, ':'); - tbufWrite(bw, v, strlen(v)); - tbufWriteChar(bw, ','); - done |= 2; - } - } - - free(line); - fclose(fp); -} - -static void addOsInfo(SBufferWriter* bw) { - char * line = NULL; - size_t size = 0; - - FILE* fp = fopen("/etc/os-release", "r"); - if (fp == NULL) { - return; - } - - while ((size = tgetline(&line, &size, fp)) != -1) { - line[size - 1] = '\0'; - if (strncmp(line, "PRETTY_NAME", 11) == 0) { - const char* p = strchr(line, '=') + 1; - if (*p == '"') { - p++; - line[size - 2] = 0; - } - addStringField(bw, "os", p); - break; - } - } - - free(line); - fclose(fp); -} - -static void addMemoryInfo(SBufferWriter* bw) { - char * line = NULL; - size_t size = 0; - - FILE* fp = fopen("/proc/meminfo", "r"); - if (fp == NULL) { - return; - } - - while ((size = tgetline(&line, &size, fp)) != -1) { - line[size - 1] = '\0'; - if (strncmp(line, "MemTotal", 8) == 0) { - const char* p = strchr(line, ':') + 1; - while (*p == ' ') p++; - addStringField(bw, "memory", p); - break; - } - } - - free(line); - fclose(fp); -} - -static void addVersionInfo(SBufferWriter* bw) { - addStringField(bw, "version", version); - addStringField(bw, "buildInfo", buildinfo); - addStringField(bw, "gitInfo", gitinfo); - addStringField(bw, "email", tsEmail); -} - -static void addRuntimeInfo(SBufferWriter* bw) { - addIntField(bw, "numOfDnode", mnodeGetDnodesNum()); - addIntField(bw, "numOfMnode", mnodeGetMnodesNum()); - addIntField(bw, "numOfVgroup", mnodeGetVgroupNum()); - addIntField(bw, "numOfDatabase", mnodeGetDbNum()); - addIntField(bw, "numOfSuperTable", mnodeGetSuperTableNum()); - addIntField(bw, "numOfChildTable", mnodeGetChildTableNum()); - - SAcctInfo info; - mnodeGetStatOfAllAcct(&info); - addIntField(bw, "numOfColumn", info.numOfTimeSeries); - addIntField(bw, "numOfPoint", info.totalPoints); - addIntField(bw, "totalStorage", info.totalStorage); - addIntField(bw, "compStorage", info.compStorage); - // addStringField(bw, "installTime", "2020-08-01T00:00:00Z"); -} - -static void sendTelemetryReport() { - char buf[128]; - uint32_t ip = taosGetIpv4FromFqdn(TELEMETRY_SERVER); - if (ip == 0xffffffff) { - dTrace("failed to get IP address of " TELEMETRY_SERVER ", reason:%s", strerror(errno)); - return; - } - SOCKET fd = taosOpenTcpClientSocket(ip, TELEMETRY_PORT, 0); - if (fd < 0) { - dTrace("failed to create socket for telemetry, reason:%s", strerror(errno)); - return; - } - - SBufferWriter bw = tbufInitWriter(NULL, false); - beginObject(&bw); - addStringField(&bw, "instanceId", mnodeGetClusterId()); - addIntField(&bw, "reportVersion", 1); - addOsInfo(&bw); - addCpuInfo(&bw); - addMemoryInfo(&bw); - addVersionInfo(&bw); - addRuntimeInfo(&bw); - closeObject(&bw); - - const char* header = "POST /report HTTP/1.1\n" - "Host: " TELEMETRY_SERVER "\n" - "Content-Type: application/json\n" - "Content-Length: "; - - taosWriteSocket(fd, header, (int32_t)strlen(header)); - int32_t contLen = (int32_t)(tbufTell(&bw) - 1); - sprintf(buf, "%d\n\n", contLen); - taosWriteSocket(fd, buf, (int32_t)strlen(buf)); - taosWriteSocket(fd, tbufGetData(&bw, false), contLen); - tbufCloseWriter(&bw); - - // read something to avoid nginx error 499 - if (taosReadSocket(fd, buf, 10) < 0) { - dTrace("failed to receive response, reason:%s", strerror(errno)); - } - taosCloseSocket(fd); -} - -static void* telemetryThread(void* param) { - struct timespec end = {0}; - clock_gettime(CLOCK_REALTIME, &end); - end.tv_sec += 300; // wait 5 minutes before send first report - - setThreadName("telemetry"); - - while (!tsExit) { - int r = 0; - struct timespec ts = end; - pthread_mutex_lock(&tsExitLock); - r = pthread_cond_timedwait(&tsExitCond, &tsExitLock, &ts); - pthread_mutex_unlock(&tsExitLock); - if (r==0) break; - if (r!=ETIMEDOUT) continue; - - if (sdbIsMaster()) { - sendTelemetryReport(); - } - end.tv_sec += REPORT_INTERVAL; - } - - return NULL; -} - -static void dnodeGetEmail(char* filepath) { - int32_t fd = open(filepath, O_RDONLY); - if (fd < 0) { - return; - } - - if (taosRead(fd, (void *)tsEmail, TSDB_FQDN_LEN) < 0) { - dError("failed to read %d bytes from file %s since %s", TSDB_FQDN_LEN, filepath, strerror(errno)); - } - - taosClose(fd); -} - -int32_t dnodeInitTelemetry() { - if (!tsEnableTelemetryReporting) { - return 0; - } - - dnodeGetEmail("/usr/local/taos/email"); - - pthread_attr_t attr; - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); - - int32_t code = pthread_create(&tsTelemetryThread, &attr, telemetryThread, NULL); - pthread_attr_destroy(&attr); - if (code != 0) { - dTrace("failed to create telemetry thread, reason:%s", strerror(code)); - } - - dInfo("dnode telemetry is initialized"); - return 0; -} - -void dnodeCleanupTelemetry() { - if (!tsEnableTelemetryReporting) { - return; - } - - if (taosCheckPthreadValid(tsTelemetryThread)) { - pthread_mutex_lock(&tsExitLock); - tsExit = 1; - pthread_cond_signal(&tsExitCond); - pthread_mutex_unlock(&tsExitLock); - - pthread_join(tsTelemetryThread, NULL); - } -} diff --git a/src/dnode/src/dnodeVMgmt.c b/src/dnode/src/dnodeVMgmt.c deleted file mode 100644 index c1bfb1460b4b3058434c628f503a4775c4c24701..0000000000000000000000000000000000000000 --- a/src/dnode/src/dnodeVMgmt.c +++ /dev/null @@ -1,236 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "tqueue.h" -#include "tworker.h" -#include "dnodeVMgmt.h" - -typedef struct { - SRpcMsg rpcMsg; - char pCont[]; -} SMgmtMsg; - -static SWorkerPool tsVMgmtWP; -static taos_queue tsVMgmtQueue = NULL; - -static void * dnodeProcessMgmtQueue(void *param); -static int32_t dnodeProcessCreateVnodeMsg(SRpcMsg *pMsg); -static int32_t dnodeProcessAlterVnodeMsg(SRpcMsg *pMsg); -static int32_t dnodeProcessSyncVnodeMsg(SRpcMsg *pMsg); -static int32_t dnodeProcessCompactVnodeMsg(SRpcMsg *pMsg); -static int32_t dnodeProcessDropVnodeMsg(SRpcMsg *pMsg); -static int32_t dnodeProcessAlterStreamMsg(SRpcMsg *pMsg); -static int32_t dnodeProcessConfigDnodeMsg(SRpcMsg *pMsg); -static int32_t dnodeProcessCreateMnodeMsg(SRpcMsg *pMsg); -static int32_t (*dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MAX])(SRpcMsg *pMsg); - -int32_t dnodeInitVMgmt() { - dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_CREATE_VNODE] = dnodeProcessCreateVnodeMsg; - dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_ALTER_VNODE] = dnodeProcessAlterVnodeMsg; - dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_SYNC_VNODE] = dnodeProcessSyncVnodeMsg; - dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_COMPACT_VNODE]= dnodeProcessCompactVnodeMsg; - dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_DROP_VNODE] = dnodeProcessDropVnodeMsg; - dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_ALTER_STREAM] = dnodeProcessAlterStreamMsg; - dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_CONFIG_DNODE] = dnodeProcessConfigDnodeMsg; - dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_CREATE_MNODE] = dnodeProcessCreateMnodeMsg; - - int32_t code = vnodeInitMgmt(); - if (code != TSDB_CODE_SUCCESS) return -1; - - tsVMgmtWP.name = "vmgmt"; - tsVMgmtWP.workerFp = dnodeProcessMgmtQueue; - tsVMgmtWP.min = 1; - tsVMgmtWP.max = 1; - if (tWorkerInit(&tsVMgmtWP) != 0) return -1; - - tsVMgmtQueue = tWorkerAllocQueue(&tsVMgmtWP, NULL); - - dInfo("dnode vmgmt is initialized"); - return TSDB_CODE_SUCCESS; -} - -void dnodeCleanupVMgmt() { - tWorkerFreeQueue(&tsVMgmtWP, tsVMgmtQueue); - tWorkerCleanup(&tsVMgmtWP); - - tsVMgmtQueue = NULL; - vnodeCleanupMgmt(); -} - -static int32_t dnodeWriteToMgmtQueue(SRpcMsg *pMsg) { - int32_t size = sizeof(SMgmtMsg) + pMsg->contLen; - SMgmtMsg *pMgmt = taosAllocateQitem(size); - if (pMgmt == NULL) return TSDB_CODE_DND_OUT_OF_MEMORY; - - pMgmt->rpcMsg = *pMsg; - pMgmt->rpcMsg.pCont = pMgmt->pCont; - memcpy(pMgmt->pCont, pMsg->pCont, pMsg->contLen); - taosWriteQitem(tsVMgmtQueue, TAOS_QTYPE_RPC, pMgmt); - - return TSDB_CODE_SUCCESS; -} - -void dnodeDispatchToVMgmtQueue(SRpcMsg *pMsg) { - int32_t code = dnodeWriteToMgmtQueue(pMsg); - if (code != TSDB_CODE_SUCCESS) { - SRpcMsg rsp = {.handle = pMsg->handle, .code = code}; - rpcSendResponse(&rsp); - } - - rpcFreeCont(pMsg->pCont); -} - -static void *dnodeProcessMgmtQueue(void *wparam) { - SWorker * pWorker = wparam; - SWorkerPool *pPool = pWorker->pPool; - SMgmtMsg * pMgmt; - SRpcMsg * pMsg; - SRpcMsg rsp = {0}; - int32_t qtype; - void * handle; - - setThreadName("dnodeMgmtQ"); - - while (1) { - if (taosReadQitemFromQset(pPool->qset, &qtype, (void **)&pMgmt, &handle) == 0) { - dDebug("qdnode mgmt got no message from qset:%p, , exit", pPool->qset); - break; - } - - pMsg = &pMgmt->rpcMsg; - dTrace("msg:%p, ahandle:%p type:%s will be processed", pMgmt, pMsg->ahandle, taosMsg[pMsg->msgType]); - if (dnodeProcessMgmtMsgFp[pMsg->msgType]) { - rsp.code = (*dnodeProcessMgmtMsgFp[pMsg->msgType])(pMsg); - } else { - rsp.code = TSDB_CODE_DND_MSG_NOT_PROCESSED; - } - - dTrace("msg:%p, is processed, code:0x%x", pMgmt, rsp.code); - if (rsp.code != TSDB_CODE_DND_ACTION_IN_PROGRESS) { - rsp.handle = pMsg->handle; - rsp.pCont = NULL; - rpcSendResponse(&rsp); - } - - taosFreeQitem(pMsg); - } - - return NULL; -} - -static SCreateVnodeMsg* dnodeParseVnodeMsg(SRpcMsg *rpcMsg) { - SCreateVnodeMsg *pCreate = rpcMsg->pCont; - pCreate->cfg.vgId = htonl(pCreate->cfg.vgId); - pCreate->cfg.dbCfgVersion = htonl(pCreate->cfg.dbCfgVersion); - pCreate->cfg.vgCfgVersion = htonl(pCreate->cfg.vgCfgVersion); - pCreate->cfg.maxTables = htonl(pCreate->cfg.maxTables); - pCreate->cfg.cacheBlockSize = htonl(pCreate->cfg.cacheBlockSize); - pCreate->cfg.totalBlocks = htonl(pCreate->cfg.totalBlocks); - pCreate->cfg.daysPerFile = htonl(pCreate->cfg.daysPerFile); - pCreate->cfg.daysToKeep1 = htonl(pCreate->cfg.daysToKeep1); - pCreate->cfg.daysToKeep2 = htonl(pCreate->cfg.daysToKeep2); - pCreate->cfg.daysToKeep = htonl(pCreate->cfg.daysToKeep); - pCreate->cfg.minRowsPerFileBlock = htonl(pCreate->cfg.minRowsPerFileBlock); - pCreate->cfg.maxRowsPerFileBlock = htonl(pCreate->cfg.maxRowsPerFileBlock); - pCreate->cfg.fsyncPeriod = htonl(pCreate->cfg.fsyncPeriod); - pCreate->cfg.commitTime = htonl(pCreate->cfg.commitTime); - - for (int32_t j = 0; j < pCreate->cfg.vgReplica; ++j) { - pCreate->nodes[j].nodeId = htonl(pCreate->nodes[j].nodeId); - } - - return pCreate; -} - -static int32_t dnodeProcessCreateVnodeMsg(SRpcMsg *rpcMsg) { - SCreateVnodeMsg *pCreate = dnodeParseVnodeMsg(rpcMsg); - void *pVnode = vnodeAcquire(pCreate->cfg.vgId); - if (pVnode != NULL) { - dDebug("vgId:%d, already exist, return success", pCreate->cfg.vgId); - vnodeRelease(pVnode); - return TSDB_CODE_SUCCESS; - } else { - dDebug("vgId:%d, create vnode msg is received", pCreate->cfg.vgId); - return vnodeCreate(pCreate); - } -} - -static int32_t dnodeProcessAlterVnodeMsg(SRpcMsg *rpcMsg) { - SAlterVnodeMsg *pAlter = dnodeParseVnodeMsg(rpcMsg); - - void *pVnode = vnodeAcquireNotClose(pAlter->cfg.vgId); - if (pVnode != NULL) { - dDebug("vgId:%d, alter vnode msg is received", pAlter->cfg.vgId); - int32_t code = vnodeAlter(pVnode, pAlter); - vnodeRelease(pVnode); - return code; - } else { - dInfo("vgId:%d, vnode not exist, can't alter it", pAlter->cfg.vgId); - return TSDB_CODE_VND_INVALID_VGROUP_ID; - } -} - -static int32_t dnodeProcessSyncVnodeMsg(SRpcMsg *rpcMsg) { - SSyncVnodeMsg *pSyncVnode = rpcMsg->pCont; - pSyncVnode->vgId = htonl(pSyncVnode->vgId); - - return vnodeSync(pSyncVnode->vgId); -} - -static int32_t dnodeProcessCompactVnodeMsg(SRpcMsg *rpcMsg) { - SCompactVnodeMsg *pCompactVnode = rpcMsg->pCont; - pCompactVnode->vgId = htonl(pCompactVnode->vgId); - return vnodeCompact(pCompactVnode->vgId); -} - -static int32_t dnodeProcessDropVnodeMsg(SRpcMsg *rpcMsg) { - SDropVnodeMsg *pDrop = rpcMsg->pCont; - pDrop->vgId = htonl(pDrop->vgId); - - return vnodeDrop(pDrop->vgId); -} - -static int32_t dnodeProcessAlterStreamMsg(SRpcMsg *pMsg) { - return 0; -} - -static int32_t dnodeProcessConfigDnodeMsg(SRpcMsg *pMsg) { - SCfgDnodeMsg *pCfg = pMsg->pCont; - return taosCfgDynamicOptions(pCfg->config); -} - -static int32_t dnodeProcessCreateMnodeMsg(SRpcMsg *pMsg) { - SCreateMnodeMsg *pCfg = pMsg->pCont; - pCfg->dnodeId = htonl(pCfg->dnodeId); - if (pCfg->dnodeId != dnodeGetDnodeId()) { - dDebug("dnode:%d, in create mnode msg is not equal with saved dnodeId:%d", pCfg->dnodeId, dnodeGetDnodeId()); - return TSDB_CODE_MND_DNODE_ID_NOT_CONFIGURED; - } - - if (strcmp(pCfg->dnodeEp, tsLocalEp) != 0) { - dDebug("dnodeEp:%s, in create mnode msg is not equal with saved dnodeEp:%s", pCfg->dnodeEp, tsLocalEp); - return TSDB_CODE_MND_DNODE_EP_NOT_CONFIGURED; - } - - dDebug("dnode:%d, create mnode msg is received from mnodes, numOfMnodes:%d", pCfg->dnodeId, pCfg->mnodes.mnodeNum); - for (int i = 0; i < pCfg->mnodes.mnodeNum; ++i) { - pCfg->mnodes.mnodeInfos[i].mnodeId = htonl(pCfg->mnodes.mnodeInfos[i].mnodeId); - dDebug("mnode index:%d, mnode:%d:%s", i, pCfg->mnodes.mnodeInfos[i].mnodeId, pCfg->mnodes.mnodeInfos[i].mnodeEp); - } - - return dnodeStartMnode(&pCfg->mnodes); -} diff --git a/src/dnode/src/dnodeVRead.c b/src/dnode/src/dnodeVRead.c deleted file mode 100644 index c404ab1a55c3788f5756c99f7914764e6e9af295..0000000000000000000000000000000000000000 --- a/src/dnode/src/dnodeVRead.c +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "tqueue.h" -#include "tworker.h" -#include "dnodeVRead.h" - -static void *dnodeProcessReadQueue(void *pWorker); - -// module global variable -static SWorkerPool tsVQueryWP; -static SWorkerPool tsVFetchWP; - -int32_t dnodeInitVRead() { - const int32_t maxFetchThreads = 4; - - // calculate the available query thread - float threadsForQuery = MAX(tsNumOfCores * tsRatioOfQueryCores, 1); - - tsVQueryWP.name = "vquery"; - tsVQueryWP.workerFp = dnodeProcessReadQueue; - tsVQueryWP.min = (int32_t) threadsForQuery; - tsVQueryWP.max = tsVQueryWP.min; - if (tWorkerInit(&tsVQueryWP) != 0) return -1; - - tsVFetchWP.name = "vfetch"; - tsVFetchWP.workerFp = dnodeProcessReadQueue; - tsVFetchWP.min = MIN(maxFetchThreads, tsNumOfCores); - tsVFetchWP.max = tsVFetchWP.min; - if (tWorkerInit(&tsVFetchWP) != 0) return -1; - - return 0; -} - -void dnodeCleanupVRead() { - tWorkerCleanup(&tsVFetchWP); - tWorkerCleanup(&tsVQueryWP); -} - -void dnodeDispatchToVReadQueue(SRpcMsg *pMsg) { - int32_t queuedMsgNum = 0; - int32_t leftLen = pMsg->contLen; - int32_t code = TSDB_CODE_VND_INVALID_VGROUP_ID; - char * pCont = pMsg->pCont; - - while (leftLen > 0) { - SMsgHead *pHead = (SMsgHead *)pCont; - pHead->vgId = htonl(pHead->vgId); - pHead->contLen = htonl(pHead->contLen); - - assert(pHead->contLen > 0); - void *pVnode = vnodeAcquireNotClose(pHead->vgId); - if (pVnode != NULL) { - code = vnodeWriteToRQueue(pVnode, pCont, pHead->contLen, TAOS_QTYPE_RPC, pMsg); - if (code == TSDB_CODE_SUCCESS) queuedMsgNum++; - vnodeRelease(pVnode); - } - - leftLen -= pHead->contLen; - pCont -= pHead->contLen; - } - - if (queuedMsgNum == 0) { - SRpcMsg rpcRsp = {.handle = pMsg->handle, .code = code}; - rpcSendResponse(&rpcRsp); - } - - rpcFreeCont(pMsg->pCont); -} - -void *dnodeAllocVQueryQueue(void *pVnode) { - return tWorkerAllocQueue(&tsVQueryWP, pVnode); -} - -void *dnodeAllocVFetchQueue(void *pVnode) { - return tWorkerAllocQueue(&tsVFetchWP, pVnode); -} - -void dnodeFreeVQueryQueue(void *pQqueue) { - tWorkerFreeQueue(&tsVQueryWP, pQqueue); -} - -void dnodeFreeVFetchQueue(void *pFqueue) { - tWorkerFreeQueue(&tsVFetchWP, pFqueue); -} - -void dnodeSendRpcVReadRsp(void *pVnode, SVReadMsg *pRead, int32_t code) { - SRpcMsg rpcRsp = { - .handle = pRead->rpcHandle, - .pCont = pRead->rspRet.rsp, - .contLen = pRead->rspRet.len, - .code = code, - }; - - rpcSendResponse(&rpcRsp); -} - -void dnodeDispatchNonRspMsg(void *pVnode, SVReadMsg *pRead, int32_t code) { -} - -static void *dnodeProcessReadQueue(void *wparam) { - SWorker * pWorker = wparam; - SWorkerPool *pPool = pWorker->pPool; - SVReadMsg * pRead; - int32_t qtype; - void * pVnode; - - char* threadname = strcmp(pPool->name, "vquery") == 0? "dnodeQueryQ":"dnodeFetchQ"; - - char name[16] = {0}; - snprintf(name, tListLen(name), "%s", threadname); - setThreadName(name); - - while (1) { - if (taosReadQitemFromQset(pPool->qset, &qtype, (void **)&pRead, &pVnode) == 0) { - dDebug("dnode vquery got no message from qset:%p, exiting", pPool->qset); - break; - } - - dTrace("msg:%p, app:%p type:%s will be processed in vquery queue, qtype:%d", pRead, pRead->rpcAhandle, - taosMsg[pRead->msgType], qtype); - - int32_t code = vnodeProcessRead(pVnode, pRead); - - if (qtype == TAOS_QTYPE_RPC && code != TSDB_CODE_QRY_NOT_READY) { - dnodeSendRpcVReadRsp(pVnode, pRead, code); - } else { - if (code == TSDB_CODE_QRY_HAS_RSP) { - dnodeSendRpcVReadRsp(pVnode, pRead, pRead->code); - } else { // code == TSDB_CODE_QRY_NOT_READY, do not return msg to client - assert(pRead->rpcHandle == NULL || (pRead->rpcHandle != NULL && pRead->msgType == 5)); - dnodeDispatchNonRspMsg(pVnode, pRead, code); - } - } - - vnodeFreeFromRQueue(pVnode, pRead); - } - - return NULL; -} diff --git a/src/dnode/src/dnodeVWrite.c b/src/dnode/src/dnodeVWrite.c deleted file mode 100644 index ed2a6e210939e907e89dd07de27481d385e4ef24..0000000000000000000000000000000000000000 --- a/src/dnode/src/dnodeVWrite.c +++ /dev/null @@ -1,238 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "tqueue.h" -#include "dnodeVWrite.h" - -typedef struct { - taos_qall qall; - taos_qset qset; // queue set - int32_t workerId; // worker ID - pthread_t thread; // thread -} SVWriteWorker; - -typedef struct { - int32_t max; // max number of workers - int32_t nextId; // from 0 to max-1, cyclic - SVWriteWorker * worker; - pthread_mutex_t mutex; -} SVWriteWorkerPool; - -static SVWriteWorkerPool tsVWriteWP; -static void *dnodeProcessVWriteQueue(void *pWorker); - -int32_t dnodeInitVWrite() { - tsVWriteWP.max = tsNumOfCores; - tsVWriteWP.worker = tcalloc(sizeof(SVWriteWorker), tsVWriteWP.max); - if (tsVWriteWP.worker == NULL) return -1; - pthread_mutex_init(&tsVWriteWP.mutex, NULL); - - for (int32_t i = 0; i < tsVWriteWP.max; ++i) { - tsVWriteWP.worker[i].workerId = i; - } - - dInfo("dnode vwrite is initialized, max worker %d", tsVWriteWP.max); - return 0; -} - -void dnodeCleanupVWrite() { - for (int32_t i = 0; i < tsVWriteWP.max; ++i) { - SVWriteWorker *pWorker = tsVWriteWP.worker + i; - if (taosCheckPthreadValid(pWorker->thread)) { - if (pWorker->qset) taosQsetThreadResume(pWorker->qset); - } - } - - for (int32_t i = 0; i < tsVWriteWP.max; ++i) { - SVWriteWorker *pWorker = tsVWriteWP.worker + i; - if (taosCheckPthreadValid(pWorker->thread)) { - pthread_join(pWorker->thread, NULL); - taosFreeQall(pWorker->qall); - taosCloseQset(pWorker->qset); - } - } - - pthread_mutex_destroy(&tsVWriteWP.mutex); - tfree(tsVWriteWP.worker); - dInfo("dnode vwrite is closed"); -} - -void dnodeDispatchToVWriteQueue(SRpcMsg *pRpcMsg) { - int32_t code; - char *pCont = pRpcMsg->pCont; - - if (pRpcMsg->msgType == TSDB_MSG_TYPE_SUBMIT) { - SMsgDesc *pDesc = (SMsgDesc *)pCont; - pDesc->numOfVnodes = htonl(pDesc->numOfVnodes); - pCont += sizeof(SMsgDesc); - } - - SMsgHead *pMsg = (SMsgHead *)pCont; - pMsg->vgId = htonl(pMsg->vgId); - pMsg->contLen = htonl(pMsg->contLen); - - void *pVnode = vnodeAcquireNotClose(pMsg->vgId); - if (pVnode == NULL) { - code = TSDB_CODE_VND_INVALID_VGROUP_ID; - } else { - SWalHead *pHead = (SWalHead *)(pCont - sizeof(SWalHead)); - pHead->msgType = pRpcMsg->msgType; - pHead->version = 0; - pHead->len = pMsg->contLen; - code = vnodeWriteToWQueue(pVnode, pHead, TAOS_QTYPE_RPC, pRpcMsg); - } - - if (code != TSDB_CODE_SUCCESS) { - SRpcMsg rpcRsp = {.handle = pRpcMsg->handle, .code = code}; - rpcSendResponse(&rpcRsp); - } - - vnodeRelease(pVnode); - rpcFreeCont(pRpcMsg->pCont); -} - -void *dnodeAllocVWriteQueue(void *pVnode) { - pthread_mutex_lock(&tsVWriteWP.mutex); - SVWriteWorker *pWorker = tsVWriteWP.worker + tsVWriteWP.nextId; - taos_queue *queue = taosOpenQueue(); - if (queue == NULL) { - pthread_mutex_unlock(&tsVWriteWP.mutex); - return NULL; - } - - if (pWorker->qset == NULL) { - pWorker->qset = taosOpenQset(); - if (pWorker->qset == NULL) { - taosCloseQueue(queue); - pthread_mutex_unlock(&tsVWriteWP.mutex); - return NULL; - } - - taosAddIntoQset(pWorker->qset, queue, pVnode); - pWorker->qall = taosAllocateQall(); - if (pWorker->qall == NULL) { - taosCloseQset(pWorker->qset); - taosCloseQueue(queue); - pthread_mutex_unlock(&tsVWriteWP.mutex); - return NULL; - } - pthread_attr_t thAttr; - pthread_attr_init(&thAttr); - pthread_attr_setdetachstate(&thAttr, PTHREAD_CREATE_JOINABLE); - - if (pthread_create(&pWorker->thread, &thAttr, dnodeProcessVWriteQueue, pWorker) != 0) { - dError("failed to create thread to process vwrite queue since %s", strerror(errno)); - taosFreeQall(pWorker->qall); - taosCloseQset(pWorker->qset); - taosCloseQueue(queue); - queue = NULL; - } else { - dDebug("dnode vwrite worker:%d is launched", pWorker->workerId); - tsVWriteWP.nextId = (tsVWriteWP.nextId + 1) % tsVWriteWP.max; - } - - pthread_attr_destroy(&thAttr); - } else { - taosAddIntoQset(pWorker->qset, queue, pVnode); - tsVWriteWP.nextId = (tsVWriteWP.nextId + 1) % tsVWriteWP.max; - } - - pthread_mutex_unlock(&tsVWriteWP.mutex); - dDebug("pVnode:%p, dnode vwrite queue:%p is allocated", pVnode, queue); - - return queue; -} - -void dnodeFreeVWriteQueue(void *pWqueue) { - taosCloseQueue(pWqueue); -} - -void dnodeSendRpcVWriteRsp(void *pVnode, void *wparam, int32_t code) { - if (wparam == NULL) return; - SVWriteMsg *pWrite = wparam; - - if (code < 0) pWrite->code = code; - int32_t count = atomic_add_fetch_32(&pWrite->processedCount, 1); - - if (count <= 1) return; - - SRpcMsg rpcRsp = { - .handle = pWrite->rpcMsg.handle, - .pCont = pWrite->rspRet.rsp, - .contLen = pWrite->rspRet.len, - .code = pWrite->code, - }; - - rpcSendResponse(&rpcRsp); - vnodeFreeFromWQueue(pVnode, pWrite); -} - -static void *dnodeProcessVWriteQueue(void *wparam) { - SVWriteWorker *pWorker = wparam; - SVWriteMsg * pWrite; - void * pVnode; - int32_t numOfMsgs; - int32_t qtype; - - taosBlockSIGPIPE(); - dDebug("dnode vwrite worker:%d is running", pWorker->workerId); - - setThreadName("dnodeWriteQ"); - - while (1) { - numOfMsgs = taosReadAllQitemsFromQset(pWorker->qset, pWorker->qall, &pVnode); - if (numOfMsgs == 0) { - dDebug("qset:%p, dnode vwrite got no message from qset, exiting", pWorker->qset); - break; - } - - bool forceFsync = false; - for (int32_t i = 0; i < numOfMsgs; ++i) { - taosGetQitem(pWorker->qall, &qtype, (void **)&pWrite); - dTrace("msg:%p, app:%p type:%s will be processed in vwrite queue, qtype:%s hver:%" PRIu64, pWrite, - pWrite->rpcMsg.ahandle, taosMsg[pWrite->walHead.msgType], qtypeStr[qtype], pWrite->walHead.version); - - pWrite->code = vnodeProcessWrite(pVnode, &pWrite->walHead, qtype, pWrite); - if (pWrite->code <= 0) atomic_add_fetch_32(&pWrite->processedCount, 1); - if (pWrite->code > 0) pWrite->code = 0; - if (pWrite->code == 0 && pWrite->walHead.msgType != TSDB_MSG_TYPE_SUBMIT) forceFsync = true; - - dTrace("msg:%p is processed in vwrite queue, code:0x%x", pWrite, pWrite->code); - } - - walFsync(vnodeGetWal(pVnode), forceFsync); - - // browse all items, and process them one by one - taosResetQitems(pWorker->qall); - for (int32_t i = 0; i < numOfMsgs; ++i) { - taosGetQitem(pWorker->qall, &qtype, (void **)&pWrite); - if (qtype == TAOS_QTYPE_RPC) { - dnodeSendRpcVWriteRsp(pVnode, pWrite, pWrite->code); - } else { - if (qtype == TAOS_QTYPE_FWD) { - vnodeConfirmForward(pVnode, pWrite->walHead.version, pWrite->code, pWrite->walHead.msgType != TSDB_MSG_TYPE_SUBMIT); - } - if (pWrite->rspRet.rsp) { - rpcFreeCont(pWrite->rspRet.rsp); - } - vnodeFreeFromWQueue(pVnode, pWrite); - } - } - } - - return NULL; -} diff --git a/src/dnode/src/dnodeVnodes.c b/src/dnode/src/dnodeVnodes.c deleted file mode 100644 index a5b0e9fe30e88f89af2e79af16602dac9500a305..0000000000000000000000000000000000000000 --- a/src/dnode/src/dnodeVnodes.c +++ /dev/null @@ -1,303 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "ttimer.h" -#include "dnodeEps.h" -#include "dnodeCfg.h" -#include "dnodeMInfos.h" -#include "dnodeVnodes.h" - -typedef struct { - pthread_t thread; - int32_t threadIndex; - int32_t failed; - int32_t opened; - int32_t vnodeNum; - int32_t * vnodeList; -} SOpenVnodeThread; - -extern void * tsDnodeTmr; -static void * tsStatusTimer = NULL; -static uint32_t tsRebootTime = 0; -static int32_t tsOpenVnodes = 0; -static int32_t tsTotalVnodes = 0; - -static void dnodeSendStatusMsg(void *handle, void *tmrId); -static void dnodeProcessStatusRsp(SRpcMsg *pMsg); - -int32_t dnodeInitStatusTimer() { - dnodeAddClientRspHandle(TSDB_MSG_TYPE_DM_STATUS_RSP, dnodeProcessStatusRsp); - - tsRebootTime = taosGetTimestampSec(); - taosTmrReset(dnodeSendStatusMsg, 500, NULL, tsDnodeTmr, &tsStatusTimer); - - dInfo("dnode status timer is initialized"); - return TSDB_CODE_SUCCESS; -} - -void dnodeCleanupStatusTimer() { - if (tsStatusTimer != NULL) { - taosTmrStopA(&tsStatusTimer); - tsStatusTimer = NULL; - } -} - -static int32_t dnodeGetVnodeList(int32_t vnodeList[], int32_t *numOfVnodes) { - DIR *dir = opendir(tsVnodeDir); - if (dir == NULL) return TSDB_CODE_DND_NO_WRITE_ACCESS; - - *numOfVnodes = 0; - struct dirent *de = NULL; - while ((de = readdir(dir)) != NULL) { - if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue; - if (de->d_type & DT_DIR) { - if (strncmp("vnode", de->d_name, 5) != 0) continue; - int32_t vnode = atoi(de->d_name + 5); - if (vnode == 0) continue; - - (*numOfVnodes)++; - - if (*numOfVnodes >= TSDB_MAX_VNODES) { - dError("vgId:%d, too many vnode directory in disk, exist:%d max:%d", vnode, *numOfVnodes, TSDB_MAX_VNODES); - closedir(dir); - return TSDB_CODE_DND_TOO_MANY_VNODES; - } else { - vnodeList[*numOfVnodes - 1] = vnode; - } - } - } - closedir(dir); - - return TSDB_CODE_SUCCESS; -} - -static void *dnodeOpenVnode(void *param) { - SOpenVnodeThread *pThread = param; - char stepDesc[TSDB_STEP_DESC_LEN] = {0}; - - dDebug("thread:%d, start to open %d vnodes", pThread->threadIndex, pThread->vnodeNum); - setThreadName("dnodeOpenVnode"); - - for (int32_t v = 0; v < pThread->vnodeNum; ++v) { - int32_t vgId = pThread->vnodeList[v]; - snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to restore, %d of %d have been opened", vgId, tsOpenVnodes, tsTotalVnodes); - dnodeReportStep("open-vnodes", stepDesc, 0); - - if (vnodeOpen(vgId) < 0) { - dError("vgId:%d, failed to open vnode by thread:%d", vgId, pThread->threadIndex); - pThread->failed++; - } else { - dDebug("vgId:%d, is opened by thread:%d", vgId, pThread->threadIndex); - pThread->opened++; - } - - atomic_add_fetch_32(&tsOpenVnodes, 1); - } - - dDebug("thread:%d, total vnodes:%d, opened:%d failed:%d", pThread->threadIndex, pThread->vnodeNum, pThread->opened, - pThread->failed); - return NULL; -} - -int32_t dnodeInitVnodes() { - int32_t vnodeList[TSDB_MAX_VNODES] = {0}; - int32_t numOfVnodes = 0; - int32_t status = dnodeGetVnodeList(vnodeList, &numOfVnodes); - tsTotalVnodes = numOfVnodes; - - if (status != TSDB_CODE_SUCCESS) { - dInfo("get dnode list failed"); - return status; - } - - int32_t threadNum = tsNumOfCores; - int32_t vnodesPerThread = numOfVnodes / threadNum + 1; - SOpenVnodeThread *threads = calloc(threadNum, sizeof(SOpenVnodeThread)); - for (int32_t t = 0; t < threadNum; ++t) { - threads[t].threadIndex = t; - threads[t].vnodeList = calloc(vnodesPerThread, sizeof(int32_t)); - } - - for (int32_t v = 0; v < numOfVnodes; ++v) { - int32_t t = v % threadNum; - SOpenVnodeThread *pThread = &threads[t]; - pThread->vnodeList[pThread->vnodeNum++] = vnodeList[v]; - } - - dInfo("start %d threads to open %d vnodes", threadNum, numOfVnodes); - - for (int32_t t = 0; t < threadNum; ++t) { - SOpenVnodeThread *pThread = &threads[t]; - if (pThread->vnodeNum == 0) continue; - - pthread_attr_t thAttr; - pthread_attr_init(&thAttr); - pthread_attr_setdetachstate(&thAttr, PTHREAD_CREATE_JOINABLE); - if (pthread_create(&pThread->thread, &thAttr, dnodeOpenVnode, pThread) != 0) { - dError("thread:%d, failed to create thread to open vnode, reason:%s", pThread->threadIndex, strerror(errno)); - } - - pthread_attr_destroy(&thAttr); - } - - int32_t openVnodes = 0; - int32_t failedVnodes = 0; - for (int32_t t = 0; t < threadNum; ++t) { - SOpenVnodeThread *pThread = &threads[t]; - if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) { - pthread_join(pThread->thread, NULL); - } - openVnodes += pThread->opened; - failedVnodes += pThread->failed; - free(pThread->vnodeList); - } - - free(threads); - dInfo("there are total vnodes:%d, opened:%d", numOfVnodes, openVnodes); - - if (failedVnodes != 0) { - dError("there are total vnodes:%d, failed:%d", numOfVnodes, failedVnodes); - return -1; - } - - return TSDB_CODE_SUCCESS; -} - -void dnodeCleanupVnodes() { - int32_t vnodeList[TSDB_MAX_VNODES]= {0}; - int32_t numOfVnodes = 0; - int32_t status; - - status = vnodeGetVnodeList(vnodeList, &numOfVnodes); - - if (status != TSDB_CODE_SUCCESS) { - dInfo("get dnode list failed"); - return; - } - - for (int32_t i = 0; i < numOfVnodes; ++i) { - vnodeClose(vnodeList[i]); - } - - dInfo("total vnodes:%d are all closed", numOfVnodes); -} - -static void dnodeProcessStatusRsp(SRpcMsg *pMsg) { - if (pMsg->code != TSDB_CODE_SUCCESS) { - dError("status rsp is received, error:%s", tstrerror(pMsg->code)); - if (pMsg->code == TSDB_CODE_MND_DNODE_NOT_EXIST) { - char clusterId[TSDB_CLUSTER_ID_LEN]; - dnodeGetClusterId(clusterId); - if (clusterId[0] != '\0') { - dnodeSetDropped(); - dError("exit zombie dropped dnode"); - exit(EXIT_FAILURE); - } - } - - taosTmrReset(dnodeSendStatusMsg, tsStatusInterval * 1000, NULL, tsDnodeTmr, &tsStatusTimer); - return; - } - - SStatusRsp *pStatusRsp = pMsg->pCont; - SMInfos *minfos = &pStatusRsp->mnodes; - dnodeUpdateMInfos(minfos); - - SDnodeCfg *pCfg = &pStatusRsp->dnodeCfg; - pCfg->numOfVnodes = htonl(pCfg->numOfVnodes); - pCfg->moduleStatus = htonl(pCfg->moduleStatus); - pCfg->dnodeId = htonl(pCfg->dnodeId); - dnodeUpdateCfg(pCfg); - - vnodeSetAccess(pStatusRsp->vgAccess, pCfg->numOfVnodes); - - SDnodeEps *pEps = (SDnodeEps *)((char *)pStatusRsp->vgAccess + pCfg->numOfVnodes * sizeof(SVgroupAccess)); - dnodeUpdateEps(pEps); - - taosTmrReset(dnodeSendStatusMsg, tsStatusInterval * 1000, NULL, tsDnodeTmr, &tsStatusTimer); -} - -static void dnodeSendStatusMsg(void *handle, void *tmrId) { - if (tsDnodeTmr == NULL) { - dError("dnode timer is already released"); - return; - } - - if (tsStatusTimer == NULL) { - taosTmrReset(dnodeSendStatusMsg, tsStatusInterval * 1000, NULL, tsDnodeTmr, &tsStatusTimer); - dError("failed to start status timer"); - return; - } - - int32_t contLen = sizeof(SStatusMsg) + TSDB_MAX_VNODES * sizeof(SVnodeLoad); - SStatusMsg *pStatus = rpcMallocCont(contLen); - if (pStatus == NULL) { - taosTmrReset(dnodeSendStatusMsg, tsStatusInterval * 1000, NULL, tsDnodeTmr, &tsStatusTimer); - dError("failed to malloc status message"); - return; - } - - dnodeGetCfg(&pStatus->dnodeId, pStatus->clusterId); - pStatus->dnodeId = htonl(dnodeGetDnodeId()); - pStatus->version = htonl(tsVersion); - pStatus->lastReboot = htonl(tsRebootTime); - pStatus->numOfCores = htons((uint16_t) tsNumOfCores); - pStatus->diskAvailable = tsAvailDataDirGB; - pStatus->alternativeRole = tsAlternativeRole; - tstrncpy(pStatus->dnodeEp, tsLocalEp, TSDB_EP_LEN); - - // fill cluster cfg parameters - pStatus->clusterCfg.numOfMnodes = htonl(tsNumOfMnodes); - pStatus->clusterCfg.mnodeEqualVnodeNum = htonl(tsMnodeEqualVnodeNum); - pStatus->clusterCfg.offlineThreshold = htonl(tsOfflineThreshold); - pStatus->clusterCfg.statusInterval = htonl(tsStatusInterval); - pStatus->clusterCfg.maxtablesPerVnode = htonl(tsMaxTablePerVnode); - pStatus->clusterCfg.maxVgroupsPerDb = htonl(tsMaxVgroupsPerDb); - tstrncpy(pStatus->clusterCfg.arbitrator, tsArbitrator, TSDB_EP_LEN); - tstrncpy(pStatus->clusterCfg.timezone, tsTimezone, 64); - pStatus->clusterCfg.checkTime = 0; - char timestr[32] = "1970-01-01 00:00:00.00"; - (void)taosParseTime(timestr, &pStatus->clusterCfg.checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, 0); - tstrncpy(pStatus->clusterCfg.locale, tsLocale, TSDB_LOCALE_LEN); - tstrncpy(pStatus->clusterCfg.charset, tsCharset, TSDB_LOCALE_LEN); - - pStatus->clusterCfg.enableBalance = tsEnableBalance; - pStatus->clusterCfg.flowCtrl = tsEnableFlowCtrl; - pStatus->clusterCfg.slaveQuery = tsEnableSlaveQuery; - pStatus->clusterCfg.adjustMaster = tsEnableAdjustMaster; - - vnodeBuildStatusMsg(pStatus); - contLen = sizeof(SStatusMsg) + pStatus->openVnodes * sizeof(SVnodeLoad); - pStatus->openVnodes = htons(pStatus->openVnodes); - - SRpcMsg rpcMsg = { - .pCont = pStatus, - .contLen = contLen, - .msgType = TSDB_MSG_TYPE_DM_STATUS - }; - - SRpcEpSet epSet; - dnodeGetEpSetForPeer(&epSet); - dnodeSendMsgToDnode(&epSet, &rpcMsg); -} - -void dnodeSendStatusMsgToMnode() { - if (tsDnodeTmr != NULL && tsStatusTimer != NULL) { - dInfo("force send status msg to mnode"); - taosTmrReset(dnodeSendStatusMsg, 3, NULL, tsDnodeTmr, &tsStatusTimer); - } -} diff --git a/src/inc/dnode.h b/src/inc/dnode.h deleted file mode 100644 index 5ecaf19f61a022bae849c2f946acb0ee693aeb59..0000000000000000000000000000000000000000 --- a/src/inc/dnode.h +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_DNODE_H -#define TDENGINE_DNODE_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include "trpc.h" -#include "taosmsg.h" - -typedef struct { - int32_t queryReqNum; - int32_t submitReqNum; - int32_t httpReqNum; -} SStatisInfo; - -SStatisInfo dnodeGetStatisInfo(); - -bool dnodeIsFirstDeploy(); -bool dnodeIsMasterEp(char *ep); -void dnodeGetEpSetForPeer(SRpcEpSet *epSet); -void dnodeGetEpSetForShell(SRpcEpSet *epSet); -int32_t dnodeGetDnodeId(); -void dnodeGetClusterId(char *clusterId); - -void dnodeUpdateEp(int32_t dnodeId, char *ep, char *fqdn, uint16_t *port); -bool dnodeCheckEpChanged(int32_t dnodeId, char *epstr); -int32_t dnodeStartMnode(SMInfos *pMinfos); - -void dnodeAddClientRspHandle(uint8_t msgType, void (*fp)(SRpcMsg *rpcMsg)); -void dnodeSendMsgToDnode(SRpcEpSet *epSet, SRpcMsg *rpcMsg); -void dnodeSendMsgToMnodeRecv(SRpcMsg *rpcMsg, SRpcMsg *rpcRsp); -void dnodeSendMsgToDnodeRecv(SRpcMsg *rpcMsg, SRpcMsg *rpcRsp, SRpcEpSet *epSet); -void *dnodeSendCfgTableToRecv(int32_t vgId, int32_t tid); - -void *dnodeAllocVWriteQueue(void *pVnode); -void dnodeFreeVWriteQueue(void *pWqueue); -void dnodeSendRpcVWriteRsp(void *pVnode, void *pWrite, int32_t code); -void *dnodeAllocVQueryQueue(void *pVnode); -void *dnodeAllocVFetchQueue(void *pVnode); -void dnodeFreeVQueryQueue(void *pQqueue); -void dnodeFreeVFetchQueue(void *pFqueue); - -int32_t dnodeAllocateMPeerQueue(); -void dnodeFreeMPeerQueue(); -int32_t dnodeAllocMReadQueue(); -void dnodeFreeMReadQueue(); -int32_t dnodeAllocMWritequeue(); -void dnodeFreeMWritequeue(); -void dnodeSendRpcMWriteRsp(void *pMsg, int32_t code); -void dnodeReprocessMWriteMsg(void *pMsg); -void dnodeDelayReprocessMWriteMsg(void *pMsg); - -void dnodeSendStatusMsgToMnode(); - -typedef struct { - char *name; - int32_t (*initFp)(); - void (*cleanupFp)(); -} SStep; - -int32_t dnodeStepInit(SStep *pSteps, int32_t stepSize); -void dnodeStepCleanup(SStep *pSteps, int32_t stepSize); -void dnodeReportStep(char *name, char *desc, int8_t finished); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/inc/mnode.h b/src/inc/mnode.h deleted file mode 100644 index 203ac57469c5bf7e1aa226c63d9bd32ffe068b46..0000000000000000000000000000000000000000 --- a/src/inc/mnode.h +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_MNODE_H -#define TDENGINE_MNODE_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include "trpc.h" - -struct SAcctObj; -struct SDnodeObj; -struct SUserObj; -struct SDbObj; -struct SVgObj; -struct STableObj; - -typedef struct { - int32_t len; - void * rsp; -} SMnodeRsp; - -typedef struct SMnodeMsg { - struct SAcctObj * pAcct; - struct SDnodeObj *pDnode; - struct SUserObj * pUser; - struct SDbObj * pDb; - struct SVgObj * pVgroup; - struct STableObj *pTable; - struct SSTableObj*pSTable; - struct SMnodeMsg *pBatchMasterMsg; - SMnodeRsp rpcRsp; - int16_t received; - int16_t successed; - int16_t expected; - int16_t retry; - int32_t incomingTs; - int32_t code; - void * pObj; - SRpcMsg rpcMsg; - char pCont[]; -} SMnodeMsg; - -void * mnodeCreateMsg(SRpcMsg *pRpcMsg); -int32_t mnodeInitMsg(SMnodeMsg *pMsg); -void mnodeCleanupMsg(SMnodeMsg *pMsg); -void mnodeDestroySubMsg(SMnodeMsg *pSubMsg); - -int32_t mnodeInitSystem(); -int32_t mnodeStartSystem(); -void mnodeCleanupSystem(); -void mnodeStopSystem(); -void sdbUpdateAsync(); -int32_t sdbUpdateSync(void *pMnodes); -bool mnodeIsRunning(); -int32_t mnodeProcessRead(SMnodeMsg *pMsg); -int32_t mnodeProcessWrite(SMnodeMsg *pMsg); -int32_t mnodeProcessPeerReq(SMnodeMsg *pMsg); -void mnodeProcessPeerRsp(SRpcMsg *pMsg); -int32_t mnodeRetriveAuth(char *user, char *spi, char *encrypt, char *secret, char *ckey); - -int32_t mnodeCompactWal(); -int32_t mnodeCompactComponents(); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/inc/vnode.h b/src/inc/vnode.h deleted file mode 100644 index b3291645c00be17283f7d078acb2d4c9a2629ece..0000000000000000000000000000000000000000 --- a/src/inc/vnode.h +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_VNODE_H -#define TDENGINE_VNODE_H - -#ifdef __cplusplus -extern "C" { -#endif -#include "trpc.h" -#include "twal.h" - -typedef struct { - int32_t len; - void * rsp; - void * qhandle; // used by query and retrieve msg -} SRspRet; - -typedef struct { - int32_t code; - int32_t contLen; - void * rpcHandle; - void * rpcAhandle; - void * qhandle; - void * pVnode; - int8_t qtype; - int8_t msgType; - SRspRet rspRet; - char pCont[]; -} SVReadMsg; - -typedef struct { - int32_t code; - int32_t processedCount; - int32_t qtype; - void * pVnode; - SRpcMsg rpcMsg; - SRspRet rspRet; - char reserveForSync[24]; - SWalHead walHead; -} SVWriteMsg; - -// vnodeStatus -extern char *vnodeStatus[]; - -// vnodeMain -int32_t vnodeCreate(SCreateVnodeMsg *pVnodeCfg); -int32_t vnodeDrop(int32_t vgId); -int32_t vnodeOpen(int32_t vgId); -int32_t vnodeAlter(void *pVnode, SCreateVnodeMsg *pVnodeCfg); -int32_t vnodeSync(int32_t vgId); -int32_t vnodeClose(int32_t vgId); -int32_t vnodeCompact(int32_t vgId); - -// vnodeMgmt -int32_t vnodeInitMgmt(); -void vnodeCleanupMgmt(); -void* vnodeAcquire(int32_t vgId); -void vnodeRelease(void *pVnode); -void* vnodeAcquireNotClose(int32_t vgId); -void* vnodeGetWal(void *pVnode); -int32_t vnodeGetVnodeList(int32_t vnodeList[], int32_t *numOfVnodes); -void vnodeBuildStatusMsg(void *pStatus); -void vnodeSetAccess(SVgroupAccess *pAccess, int32_t numOfVnodes); - -// vnodeWrite -int32_t vnodeWriteToWQueue(void *pVnode, void *pHead, int32_t qtype, void *pRpcMsg); -void vnodeFreeFromWQueue(void *pVnode, SVWriteMsg *pWrite); -int32_t vnodeProcessWrite(void *pVnode, void *pHead, int32_t qtype, void *pRspRet); - -// vnodeSync -void vnodeConfirmForward(void *pVnode, uint64_t version, int32_t code, bool force); - -// vnodeRead -int32_t vnodeWriteToRQueue(void *pVnode, void *pCont, int32_t contLen, int8_t qtype, void *rparam); -void vnodeFreeFromRQueue(void *pVnode, SVReadMsg *pRead); -int32_t vnodeProcessRead(void *pVnode, SVReadMsg *pRead); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/kit/shell/inc/shell.h b/src/kit/shell/inc/shell.h index f207a866ddc712165340c06b026aa99081f91c81..10221a409a9a44f07b16a6799fc779c1bcc75122 100644 --- a/src/kit/shell/inc/shell.h +++ b/src/kit/shell/inc/shell.h @@ -16,10 +16,9 @@ #ifndef __SHELL__ #define __SHELL__ +#include "../../../../include/client/taos.h" #include "stdbool.h" -#include "taos.h" #include "taosdef.h" -#include "stdbool.h" #include "tsclient.h" #define MAX_USERNAME_SIZE 64 diff --git a/src/util/inc/tnettest.h b/src/kit/shell/inc/tnettest.h similarity index 100% rename from src/util/inc/tnettest.h rename to src/kit/shell/inc/tnettest.h diff --git a/src/kit/shell/src/shellWindows.c b/src/kit/shell/src/shellWindows.c index cb707d9331d3f1f87227e5096b6d7f047d350ebf..1244c7b06022a53380c1a84304a4a2cdb76a17d2 100644 --- a/src/kit/shell/src/shellWindows.c +++ b/src/kit/shell/src/shellWindows.c @@ -12,9 +12,9 @@ #include #include #include +#include "../../../../include/client/taos.h" #include "os.h" #include "shell.h" -#include "taos.h" #include "shellCommand.h" extern char configDir[]; diff --git a/src/util/src/tnettest.c b/src/kit/shell/src/tnettest.c similarity index 100% rename from src/util/src/tnettest.c rename to src/kit/shell/src/tnettest.c diff --git a/src/kit/taosdemo/taosdemo.c b/src/kit/taosdemo/taosdemo.c index 9b3be1556a47b52142f41b84ca385108088a9018..858ae2264d68b05feb12e3fb12999856f6460790 100644 --- a/src/kit/taosdemo/taosdemo.c +++ b/src/kit/taosdemo/taosdemo.c @@ -19,8 +19,8 @@ */ #include -#include #include +#include "../../../include/client/taos.h" #define _GNU_SOURCE #define CURL_STATICLIB @@ -49,8 +49,8 @@ #include #include "cJSON.h" +#include "../../../include/client/taos.h" #include "os.h" -#include "taos.h" #include "taoserror.h" #include "tutil.h" diff --git a/src/kit/taosdump/taosdump.c b/src/kit/taosdump/taosdump.c index ef9e584978f12e636bccca28689062388ffd595c..3d59dfff4e06358481d60b04de3373fe91396006 100644 --- a/src/kit/taosdump/taosdump.c +++ b/src/kit/taosdump/taosdump.c @@ -17,15 +17,14 @@ #include #include +#include "../../../include/client/taos.h" #include "os.h" -#include "taos.h" #include "taosdef.h" #include "taosmsg.h" #include "tglobal.h" #include "tsclient.h" #include "tsdb.h" #include "tutil.h" -#include #define TSDB_SUPPORT_NANOSECOND 1 diff --git a/src/mnode/src/mnodeTable.c b/src/mnode/src/mnodeTable.c index a6158906a7cc77b57244594fe51881e5df0b68c8..44dae0aed182fc93241c6d25ea55ea4991fbf0ac 100644 --- a/src/mnode/src/mnodeTable.c +++ b/src/mnode/src/mnodeTable.c @@ -14,35 +14,34 @@ */ #define _DEFAULT_SOURCE -#include "os.h" -#include "taosmsg.h" -#include "tutil.h" -#include "taoserror.h" -#include "taosmsg.h" -#include "tscompression.h" -#include "tname.h" -#include "tidpool.h" -#include "tglobal.h" -#include "tcompare.h" -#include "tdataformat.h" -#include "tgrant.h" -#include "tqueue.h" +#include "mnodeTable.h" +#include "dnode.h" #include "hash.h" #include "mnode.h" -#include "dnode.h" -#include "mnodeDef.h" -#include "mnodeInt.h" #include "mnodeAcct.h" #include "mnodeDb.h" +#include "mnodeDef.h" #include "mnodeDnode.h" +#include "mnodeFunc.h" +#include "mnodeInt.h" +#include "mnodePeer.h" +#include "mnodeRead.h" #include "mnodeSdb.h" #include "mnodeShow.h" -#include "mnodeTable.h" #include "mnodeVgroup.h" #include "mnodeWrite.h" -#include "mnodeRead.h" -#include "mnodePeer.h" -#include "mnodeFunc.h" +#include "os.h" +#include "taoserror.h" +#include "taosmsg.h" +#include "tcompare.h" +#include "tcompression.h" +#include "tdataformat.h" +#include "tglobal.h" +#include "tgrant.h" +#include "tidpool.h" +#include "tname.h" +#include "tqueue.h" +#include "tutil.h" #define ALTER_CTABLE_RETRY_TIMES 3 #define CREATE_CTABLE_RETRY_TIMES 10 diff --git a/src/os/CMakeLists.txt b/src/os/CMakeLists.txt deleted file mode 100644 index 4d3bf4d7dc5b04844863851b321a14c7423ca26b..0000000000000000000000000000000000000000 --- a/src/os/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -aux_source_directory(source OS_SRC) -add_library(os ${OS_SRC}) -target_include_directories( - os - PUBLIC "${CMAKE_SOURCE_DIR}/include/os" - PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include" -) \ No newline at end of file diff --git a/src/os/inc/osArm32.h b/src/os/inc/osArm32.h deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/src/os/inc/osArm64.h b/src/os/inc/osArm64.h deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/src/os/inc/osDef.h b/src/os/inc/osDef.h deleted file mode 100644 index 54a4f98254039761d3486267ab2081ac77444489..0000000000000000000000000000000000000000 --- a/src/os/inc/osDef.h +++ /dev/null @@ -1,236 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_OS_DEF_H -#define TDENGINE_OS_DEF_H - -#ifdef __cplusplus -extern "C" { -#endif - -#if defined(_TD_DARWIN_64) - // specific - typedef int(*__compar_fn_t)(const void *, const void *); - - // for send function in tsocket.c - #if defined(MSG_NOSIGNAL) - #undef MSG_NOSIGNAL - #endif - - #define MSG_NOSIGNAL 0 - - #define SO_NO_CHECK 0x1234 - #define SOL_TCP 0x1234 - #define TCP_KEEPIDLE 0x1234 - - #ifndef PTHREAD_MUTEX_RECURSIVE_NP - #define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE - #endif -#endif - -#if defined(_ALPINE) - typedef int(*__compar_fn_t)(const void *, const void *); - void error (int, int, const char *); - #ifndef PTHREAD_MUTEX_RECURSIVE_NP - #define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE - #endif -#endif - -#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) - char *stpcpy (char *dest, const char *src); - char *stpncpy (char *dest, const char *src, size_t n); - - // specific - typedef int (*__compar_fn_t)(const void *, const void *); - #define ssize_t int - #define bzero(ptr, size) memset((ptr), 0, (size)) - #define strcasecmp _stricmp - #define strncasecmp _strnicmp - #define wcsncasecmp _wcsnicmp - #define strtok_r strtok_s - #define snprintf _snprintf - #define in_addr_t unsigned long - #define socklen_t int - - struct tm *localtime_r(const time_t *timep, struct tm *result); - char * strptime(const char *buf, const char *fmt, struct tm *tm); - char * strsep(char **stringp, const char *delim); - char * getpass(const char *prefix); - int flock(int fd, int option); - char * strndup(const char *s, size_t n); - int gettimeofday(struct timeval *ptv, void *pTimeZone); - - // for send function in tsocket.c - #define MSG_NOSIGNAL 0 - #define SO_NO_CHECK 0x1234 - #define SOL_TCP 0x1234 - - #ifndef TCP_KEEPCNT - #define TCP_KEEPCNT 0x1234 - #endif - - #ifndef TCP_KEEPIDLE - #define TCP_KEEPIDLE 0x1234 - #endif - - #ifndef TCP_KEEPINTVL - #define TCP_KEEPINTVL 0x1234 - #endif - - #define SHUT_RDWR SD_BOTH - #define SHUT_RD SD_RECEIVE - #define SHUT_WR SD_SEND - - #define LOCK_EX 1 - #define LOCK_NB 2 - #define LOCK_UN 3 - - #ifndef PATH_MAX - #define PATH_MAX 256 - #endif - - typedef struct { - int we_wordc; - char *we_wordv[1]; - int we_offs; - char wordPos[1025]; - } wordexp_t; - int wordexp(char *words, wordexp_t *pwordexp, int flags); - void wordfree(wordexp_t *pwordexp); - - #define openlog(a, b, c) - #define closelog() - #define LOG_ERR 0 - #define LOG_INFO 1 - void syslog(int unused, const char *format, ...); -#endif - -#ifndef WINDOWS - #ifndef O_BINARY - #define O_BINARY 0 - #endif -#endif - -#define POINTER_SHIFT(p, b) ((void *)((char *)(p) + (b))) -#define POINTER_DISTANCE(p1, p2) ((char *)(p1) - (char *)(p2)) - -#ifndef NDEBUG -#define ASSERT(x) assert(x) -#else -#define ASSERT(x) -#endif - -#ifndef UNUSED -#define UNUSED(x) ((void)(x)) -#endif - -#ifdef UNUSED_FUNC -#undefine UNUSED_FUNC -#endif - -#ifdef UNUSED_PARAM -#undef UNUSED_PARAM -#endif - -#if defined(__GNUC__) -#define UNUSED_PARAM(x) _UNUSED##x __attribute__((unused)) -#define UNUSED_FUNC __attribute__((unused)) -#else -#define UNUSED_PARAM(x) x -#define UNUSED_FUNC -#endif - -#ifdef tListLen -#undefine tListLen -#endif -#define tListLen(x) (sizeof(x) / sizeof((x)[0])) - -#if defined(__GNUC__) -#define FORCE_INLINE inline __attribute__((always_inline)) -#else -#define FORCE_INLINE -#endif - -#define DEFAULT_UNICODE_ENCODEC "UCS-4LE" - -#define DEFAULT_COMP(x, y) \ - do { \ - if ((x) == (y)) { \ - return 0; \ - } else { \ - return (x) < (y) ? -1 : 1; \ - } \ - } while (0) - -#define DEFAULT_DOUBLE_COMP(x, y) \ - do { \ - if (isnan(x) && isnan(y)) { return 0; } \ - if (isnan(x)) { return -1; } \ - if (isnan(y)) { return 1; } \ - if ((x) == (y)) { \ - return 0; \ - } else { \ - return (x) < (y) ? -1 : 1; \ - } \ - } while (0) - -#define DEFAULT_FLOAT_COMP(x, y) DEFAULT_DOUBLE_COMP(x, y) - -#define ALIGN_NUM(n, align) (((n) + ((align)-1)) & (~((align)-1))) - -// align to 8bytes -#define ALIGN8(n) ALIGN_NUM(n, 8) - -#undef threadlocal -#ifdef _ISOC11_SOURCE - #define threadlocal _Thread_local -#elif defined(__APPLE__) - #define threadlocal __thread -#elif defined(__GNUC__) && !defined(threadlocal) - #define threadlocal __thread -#else - #define threadlocal __declspec( thread ) -#endif - -#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) - #define PRIzu "ld" -#else - #define PRIzu "zu" -#endif - - -#if defined(_TD_LINUX_64) || defined(_TD_LINUX_32) || defined(_TD_MIPS_64) || defined(_TD_ARM_32) || defined(_TD_ARM_64) || defined(_TD_DARWIN_64) - #if defined(_TD_DARWIN_64) - // MacOS - #if !defined(_GNU_SOURCE) - #define setThreadName(name) do { pthread_setname_np((name)); } while (0) - #else - // pthread_setname_np not defined - #define setThreadName(name) - #endif - #else - // Linux, length of name must <= 16 (the last '\0' included) - #define setThreadName(name) do { prctl(PR_SET_NAME, (name)); } while (0) - #endif -#else - // Windows - #define setThreadName(name) -#endif - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/os/inc/osEok.h b/src/os/inc/osEok.h deleted file mode 100644 index 3ca476f840c1fc35bc796ecb4de67f6e0a156ca9..0000000000000000000000000000000000000000 --- a/src/os/inc/osEok.h +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_OS_EOK_H -#define TDENGINE_OS_EOK_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef __APPLE__ - -enum EPOLL_EVENTS - { - EPOLLIN = 0x001, -#define EPOLLIN EPOLLIN - EPOLLPRI = 0x002, -#define EPOLLPRI EPOLLPRI - EPOLLOUT = 0x004, -#define EPOLLOUT EPOLLOUT - EPOLLRDNORM = 0x040, -#define EPOLLRDNORM EPOLLRDNORM - EPOLLRDBAND = 0x080, -#define EPOLLRDBAND EPOLLRDBAND - EPOLLWRNORM = 0x100, -#define EPOLLWRNORM EPOLLWRNORM - EPOLLWRBAND = 0x200, -#define EPOLLWRBAND EPOLLWRBAND - EPOLLMSG = 0x400, -#define EPOLLMSG EPOLLMSG - EPOLLERR = 0x008, -#define EPOLLERR EPOLLERR - EPOLLHUP = 0x010, -#define EPOLLHUP EPOLLHUP - EPOLLRDHUP = 0x2000, -#define EPOLLRDHUP EPOLLRDHUP - EPOLLEXCLUSIVE = 1u << 28, -#define EPOLLEXCLUSIVE EPOLLEXCLUSIVE - EPOLLWAKEUP = 1u << 29, -#define EPOLLWAKEUP EPOLLWAKEUP - EPOLLONESHOT = 1u << 30, -#define EPOLLONESHOT EPOLLONESHOT - EPOLLET = 1u << 31 -#define EPOLLET EPOLLET - }; - -/* Valid opcodes ( "op" parameter ) to issue to epoll_ctl(). */ -#define EPOLL_CTL_ADD 1 /* Add a file descriptor to the interface. */ -#define EPOLL_CTL_DEL 2 /* Remove a file descriptor from the interface. */ -#define EPOLL_CTL_MOD 3 /* Change file descriptor epoll_event structure. */ - - -typedef union epoll_data -{ - void *ptr; - int fd; - uint32_t u32; - uint64_t u64; -} epoll_data_t; - -struct epoll_event -{ - uint32_t events; /* Epoll events */ - epoll_data_t data; /* User data variable */ -}; - -int epoll_create(int size); -int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); -int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout); -int epoll_close(int epfd); - -#endif // __APPLE__ - -#ifdef __cplusplus -} -#endif - -#endif // _eok_h_fd274616_996c_400e_9023_ae70be881fa3_ - diff --git a/src/os/inc/osFile.h b/src/os/inc/osFile.h deleted file mode 100644 index 86c08c0a3aca1f2e8434aee84c819d0c17060494..0000000000000000000000000000000000000000 --- a/src/os/inc/osFile.h +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_OS_FILE_H -#define TDENGINE_OS_FILE_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include "osSocket.h" - -#define FD_VALID(x) ((x) > STDERR_FILENO) -#define FD_INITIALIZER ((int32_t)-1) - -#ifndef PATH_MAX - #define PATH_MAX 256 -#endif - -#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) -typedef int32_t FileFd; -typedef SOCKET SocketFd; -#else -typedef int32_t FileFd; -typedef int32_t SocketFd; -#endif - -int64_t taosRead(FileFd fd, void *buf, int64_t count); -int64_t taosWrite(FileFd fd, void *buf, int64_t count); - -int64_t taosLSeek(FileFd fd, int64_t offset, int32_t whence); -int32_t taosFtruncate(FileFd fd, int64_t length); -int32_t taosFsync(FileFd fd); - -int32_t taosRename(char* oldName, char *newName); -int64_t taosCopy(char *from, char *to); - -int64_t taosSendFile(SocketFd dfd, FileFd sfd, int64_t *offset, int64_t size); -int64_t taosFSendFile(FILE *outfile, FILE *infile, int64_t *offset, int64_t size); - -void taosGetTmpfilePath(const char *fileNamePrefix, char *dstPath); -void taosClose(FileFd fd); - -#ifdef TAOS_RANDOM_FILE_FAIL - void taosSetRandomFileFailFactor(int32_t factor); - void taosSetRandomFileFailOutput(const char *path); - #ifdef TAOS_RANDOM_FILE_FAIL_TEST - int64_t taosReadFileRandomFail(int32_t fd, void *buf, int32_t count, const char *file, uint32_t line); - int64_t taosWriteFileRandomFail(int32_t fd, void *buf, int32_t count, const char *file, uint32_t line); - int64_t taosLSeekRandomFail(int32_t fd, int64_t offset, int32_t whence, const char *file, uint32_t line); - #undef taosRead - #undef taosWrite - #undef taosLSeek - #define taosRead(fd, buf, count) taosReadFileRandomFail(fd, buf, count, __FILE__, __LINE__) - #define taosWrite(fd, buf, count) taosWriteFileRandomFail(fd, buf, count, __FILE__, __LINE__) - #define taosLSeek(fd, offset, whence) taosLSeekRandomFail(fd, offset, whence, __FILE__, __LINE__) - #endif -#endif - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/os/inc/osInc.h b/src/os/inc/osInc.h deleted file mode 100644 index 9b78110833e73274f82d051cbaa6fd35c90f2a08..0000000000000000000000000000000000000000 --- a/src/os/inc/osInc.h +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_OS_INC_H -#define TDENGINE_OS_INC_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#if defined(_TD_LINUX_64) || defined(_TD_LINUX_32) || defined(_TD_MIPS_64) || defined(_TD_ARM_32) || defined(_TD_ARM_64) || defined(_TD_DARWIN_64) - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - - #if defined(_TD_DARWIN_64) - #include - #include "osEok.h" - #else - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - - #if !(defined(_ALPINE)) - #include - #endif - #endif -#endif - -#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) - #include - #include - #include "winsock2.h" - #include - #include - #include - #include - #include - #include "msvcProcess.h" - #include "msvcDirect.h" - #include "msvcFcntl.h" - #include "msvcLibgen.h" - #include "msvcStdio.h" - #include "msvcUnistd.h" - #include "msvcLibgen.h" - #include "sys/msvcStat.h" - #include "sys/msvcTypes.h" -#endif - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/os/inc/osLinux32.h b/src/os/inc/osLinux32.h deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/src/os/inc/osLinux64.h b/src/os/inc/osLinux64.h deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/src/os/inc/osMemory.h b/src/os/inc/osMemory.h deleted file mode 100644 index 793992c197a475b68046296b6683b5b2843b9d68..0000000000000000000000000000000000000000 --- a/src/os/inc/osMemory.h +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_OS_MEMORY_H -#define TDENGINE_OS_MEMORY_H - -#include "osString.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef TD_JEMALLOC_ENABLED -#include -#endif - -typedef enum { - TAOS_ALLOC_MODE_DEFAULT = 0, - TAOS_ALLOC_MODE_RANDOM_FAIL = 1, - TAOS_ALLOC_MODE_DETECT_LEAK = 2 -} ETaosMemoryAllocMode; - -void taosSetAllocMode(int mode, const char *path, bool autoDump); -void taosDumpMemoryLeak(); - -// used in tsdb module -void * taosTMalloc(size_t size); -void * taosTCalloc(size_t nmemb, size_t size); -void * taosTRealloc(void *ptr, size_t size); -void * taosTZfree(void *ptr); -size_t taosTSizeof(void *ptr); -void taosTMemset(void *ptr, int c); - -// used in other module -#define tmalloc(size) malloc(size) -#define tcalloc(num, size) calloc(num, size) -#define trealloc(ptr, size) realloc(ptr, size) -#define tfree(x) \ - do { \ - if (x) { \ - free((void *)(x)); \ - x = 0; \ - } \ - } while (0) - -#ifdef TAOS_MEM_CHECK - #ifdef TAOS_MEM_CHECK_TEST - void * taosMallocMem(size_t size, const char *file, uint32_t line); - void * taosCallocMem(size_t num, size_t size, const char *file, uint32_t line); - void * taosReallocMem(void *ptr, size_t size, const char *file, uint32_t line); - void taosFreeMem(void *ptr, const char *file, uint32_t line); - char * taosStrdupMem(const char *str, const char *file, uint32_t line); - char * taosStrndupMem(const char *str, size_t size, const char *file, uint32_t line); - ssize_t taosGetlineMem(char **lineptr, size_t *n, FILE *stream, const char *file, uint32_t line); - #undef tmalloc - #undef tcalloc - #undef trealloc - #undef tfree - #define tmalloc(size) taosMallocMem(size, __FILE__, __LINE__) - #define tcalloc(num, size) taosCallocMem(num, size, __FILE__, __LINE__) - #define trealloc(ptr, size) taosReallocMem(ptr, size, __FILE__, __LINE__) - #define tfree(ptr) taosFreeMem(ptr, __FILE__, __LINE__) - #endif -#endif - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/os/inc/osMips64.h b/src/os/inc/osMips64.h deleted file mode 100644 index ed7b08a31116d20a8eaaaacfa3a9a64ea75dbcee..0000000000000000000000000000000000000000 --- a/src/os/inc/osMips64.h +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_OS_MIPS64_H -#define TDENGINE_OS_MIPS64_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/os/inc/osSocket.h b/src/os/inc/osSocket.h deleted file mode 100644 index a172c0bf3465257fe5590ae0a7552cf699a61c10..0000000000000000000000000000000000000000 --- a/src/os/inc/osSocket.h +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_OS_SOCKET_H -#define TDENGINE_OS_SOCKET_H - -#ifdef __cplusplus -extern "C" { -#endif - -#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) - #define taosSend(sockfd, buf, len, flags) send((SOCKET)sockfd, buf, len, flags) - #define taosSendto(sockfd, buf, len, flags, dest_addr, addrlen) sendto((SOCKET)sockfd, buf, len, flags, dest_addr, addrlen) - #define taosWriteSocket(fd, buf, len) send((SOCKET)fd, buf, len, 0) - #define taosReadSocket(fd, buf, len) recv((SOCKET)fd, buf, len, 0) - #define taosCloseSocketNoCheck(fd) closesocket((SOCKET)fd) - #define taosCloseSocket(fd) closesocket((SOCKET)fd) -#else - #define taosSend(sockfd, buf, len, flags) send(sockfd, buf, len, flags) - #define taosSendto(sockfd, buf, len, flags, dest_addr, addrlen) sendto(sockfd, buf, len, flags, dest_addr, addrlen) - #define taosReadSocket(fd, buf, len) read(fd, buf, len) - #define taosWriteSocket(fd, buf, len) write(fd, buf, len) - #define taosCloseSocketNoCheck(x) close(x) - #define taosCloseSocket(x) \ - { \ - if (FD_VALID(x)) { \ - close(x); \ - x = FD_INITIALIZER; \ - } \ - } -#endif - -#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) - #define TAOS_EPOLL_WAIT_TIME 100 - typedef SOCKET eventfd_t; - #define eventfd(a, b) -1 - typedef SOCKET EpollFd; - #define EpollClose(pollFd) epoll_close(pollFd) - #ifndef EPOLLWAKEUP - #define EPOLLWAKEUP (1u << 29) - #endif -#elif defined(_TD_DARWIN_64) - #define TAOS_EPOLL_WAIT_TIME 500 - typedef int32_t SOCKET; - typedef SOCKET EpollFd; - #define EpollClose(pollFd) epoll_close(pollFd) -#else - #define TAOS_EPOLL_WAIT_TIME 500 - typedef int32_t SOCKET; - typedef SOCKET EpollFd; - #define EpollClose(pollFd) taosCloseSocket(pollFd) -#endif - -#ifdef TAOS_RANDOM_NETWORK_FAIL - #ifdef TAOS_RANDOM_NETWORK_FAIL_TEST - int64_t taosSendRandomFail(int32_t sockfd, const void *buf, size_t len, int32_t flags); - int64_t taosSendToRandomFail(int32_t sockfd, const void *buf, size_t len, int32_t flags, const struct sockaddr *dest_addr, socklen_t addrlen); - int64_t taosReadSocketRandomFail(int32_t fd, void *buf, size_t count); - int64_t taosWriteSocketRandomFail(int32_t fd, const void *buf, size_t count); - #undef taosSend - #undef taosSendto - #undef taosReadSocket - #undef taosWriteSocket - #define taosSend(sockfd, buf, len, flags) taosSendRandomFail(sockfd, buf, len, flags) - #define taosSendto(sockfd, buf, len, flags, dest_addr, addrlen) taosSendToRandomFail(sockfd, buf, len, flags, dest_addr, addrlen) - #define taosReadSocket(fd, buf, len) taosReadSocketRandomFail(fd, buf, len) - #define taosWriteSocket(fd, buf, len) taosWriteSocketRandomFail(fd, buf, len) - #endif -#endif - -int32_t taosSetNonblocking(SOCKET sock, int32_t on); -void taosIgnSIGPIPE(); -void taosBlockSIGPIPE(); -void taosSetMaskSIGPIPE(); -int32_t taosSetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *optval, int32_t optlen); -int32_t taosGetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *optval, int32_t* optlen); - -uint32_t taosInetAddr(char *ipAddr); -const char *taosInetNtoa(struct in_addr ipInt); - -#if (defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)) - #define htobe64 htonll - #if defined(_TD_GO_DLL_) - uint64_t htonll(uint64_t val); - #endif -#endif - -#if defined(_TD_DARWIN_64) - #define htobe64 htonll -#endif - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/os/inc/osSysinfo.h b/src/os/inc/osSysinfo.h deleted file mode 100644 index 0320ab0f7fe5f72e70fd6d12d8d2b0a27dec3754..0000000000000000000000000000000000000000 --- a/src/os/inc/osSysinfo.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_OS_SYSINFO_H -#define TDENGINE_OS_SYSINFO_H - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct { - int64_t tsize; - int64_t used; - int64_t avail; -} SysDiskSize; - -int32_t taosGetDiskSize(char *dataDir, SysDiskSize *diskSize); - -int32_t taosGetCpuCores(); -void taosGetSystemInfo(); -bool taosReadProcIO(int64_t* rchars, int64_t* wchars); -bool taosGetProcIO(float *readKB, float *writeKB); -bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes); -bool taosGetBandSpeed(float *bandSpeedKb); -void taosGetDisk(); -bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) ; -bool taosGetProcMemory(float *memoryUsedMB) ; -bool taosGetSysMemory(float *memoryUsedMB); -void taosPrintOsInfo(); -void taosPrintDiskInfo(); -int taosSystem(const char * cmd) ; -void taosKillSystem(); -bool taosGetSystemUid(char *uid); -char *taosGetCmdlineByPID(int pid); - -void taosSetCoreDump(); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/os/source/os.c b/src/os/source/os.c deleted file mode 100644 index 6dea4a4e57392be988126c579648f39a8270b9bf..0000000000000000000000000000000000000000 --- a/src/os/source/os.c +++ /dev/null @@ -1,14 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ \ No newline at end of file diff --git a/src/os/src/darwin/CMakeLists.txt b/src/os/src/darwin/CMakeLists.txt deleted file mode 100644 index ed75cac03da112348ff153005d5330786f6386ac..0000000000000000000000000000000000000000 --- a/src/os/src/darwin/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.8...3.20) -PROJECT(TDengine) - -AUX_SOURCE_DIRECTORY(. SRC) -ADD_LIBRARY(osdarwin ${SRC}) diff --git a/src/os/src/darwin/darwinSystem.c b/src/os/src/darwin/darwinSystem.c deleted file mode 100644 index f152e36d7b9f41e5ddf97db3f5d0c4cf2d714632..0000000000000000000000000000000000000000 --- a/src/os/src/darwin/darwinSystem.c +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "tglobal.h" -#include "tulog.h" - -void* taosLoadDll(const char *filename) { - return NULL; -} - -void* taosLoadSym(void* handle, char* name) { - return NULL; -} - -void taosCloseDll(void *handle) { -} - -int taosSetConsoleEcho(bool on) -{ -#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL) - int err; - struct termios term; - - if (tcgetattr(STDIN_FILENO, &term) == -1) { - perror("Cannot get the attribution of the terminal"); - return -1; - } - - if (on) - term.c_lflag|=ECHOFLAGS; - else - term.c_lflag &=~ECHOFLAGS; - - err = tcsetattr(STDIN_FILENO,TCSAFLUSH,&term); - if (err == -1 && err == EINTR) { - perror("Cannot set the attribution of the terminal"); - return -1; - } - - return 0; -} - diff --git a/src/os/src/darwin/dwEnv.c b/src/os/src/darwin/dwEnv.c deleted file mode 100644 index 8ac97eacfbc53eb67fa141f8bdddc79d8a65777c..0000000000000000000000000000000000000000 --- a/src/os/src/darwin/dwEnv.c +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "tglobal.h" - -static const char* expand_like_shell(const char *path) { - static __thread char buf[TSDB_FILENAME_LEN]; - buf[0] = '\0'; - wordexp_t we; - if (wordexp(path, &we, 0)) return "/tmp/taosd"; - if (sizeof(buf)<=snprintf(buf, sizeof(buf), "%s", we.we_wordv[0])) return "/tmp/taosd"; - wordfree(&we); - return buf; -} - -void osInit() { - if (configDir[0] == 0) { - strcpy(configDir, expand_like_shell("/usr/local/etc/taos")); - } - strcpy(tsDataDir, expand_like_shell("/usr/local/var/lib/taos")); - strcpy(tsLogDir, expand_like_shell("/usr/local/var/log/taos")); - strcpy(tsScriptDir, expand_like_shell("/usr/local/etc/taos")); - - strcpy(tsVnodeDir, ""); - strcpy(tsDnodeDir, ""); - strcpy(tsMnodeDir, ""); - strcpy(tsOsName, "Darwin"); -} - diff --git a/src/os/src/darwin/dwEok.c b/src/os/src/darwin/dwEok.c deleted file mode 100644 index 22d92607635ab81793dd7c7d7e2303a8a9d76477..0000000000000000000000000000000000000000 --- a/src/os/src/darwin/dwEok.c +++ /dev/null @@ -1,891 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -// fail-fast or let-it-crash philosophy -// https://en.wikipedia.org/wiki/Fail-fast -// https://stackoverflow.com/questions/4393197/erlangs-let-it-crash-philosophy-applicable-elsewhere -// experimentally, we follow log-and-crash here - -#define _DEFAULT_SOURCE -#include "os.h" -#include "osEok.h" - -#include - -// #define BALANCE_CHECK_WHEN_CLOSE - -#ifdef ENABLE_LOG -#define D(fmt, ...) fprintf(stderr, "%s[%d]%s(): " fmt "\n", basename(__FILE__), __LINE__, __func__, ##__VA_ARGS__) -#define E(fmt, ...) do { \ - fprintf(stderr, "%s[%d]%s(): %d[%s]: " fmt "\n", \ - basename(__FILE__), __LINE__, __func__, \ - errno, strerror(errno), \ - ##__VA_ARGS__); \ -} while (0) -#else // !ENABLE_LOG -#define D(fmt, ...) (void)fmt -#define E(fmt, ...) (void)fmt -#endif // ENABLE_LOG - -#define A(statement, fmt, ...) do { \ - if (statement) break; \ - fprintf(stderr, "%s[%d]%s(): assert [%s] failed: %d[%s]: " fmt "\n", \ - basename(__FILE__), __LINE__, __func__, \ - #statement, errno, strerror(errno), \ - ##__VA_ARGS__); \ - abort(); \ -} while (0) - -static int eok_dummy = 0; - -typedef struct ep_over_kq_s ep_over_kq_t; -typedef struct eok_event_s eok_event_t; - -struct ep_over_kq_s { - int kq; - - // !!! - // idx in the eoks list - // used as pseudo-file-desciptor - // must be 'closed' with epoll_close - int idx; - - ep_over_kq_t *next; - - int sv[2]; // 0 for read, 1 for write - - // all registered 'epoll events, key by fd' - int evs_count; - eok_event_t *evs_head; - eok_event_t *evs_tail; - eok_event_t *evs_free; - - // all kev changes list pending to be processed by kevent64 - // key by tuple (ident,filter), ident === fd in this case - struct kevent64_s *kchanges; - int nchanges; - int ichanges; - - // kev eventslist for kevent64 to store active events - // they remain alive among kevent64 calls - struct kevent64_s *kevslist; - int nevslist; - - pthread_mutex_t lock; - - volatile unsigned int lock_valid:1; - volatile unsigned int waiting:1; - volatile unsigned int changed:1; - volatile unsigned int wakenup:1; - volatile unsigned int stopping:1; -}; - -struct eok_event_s { - int fd; - struct epoll_event epev; - volatile unsigned int changed; // 0:registered;1:add;2:mod;3:del - - eok_event_t *next; - eok_event_t *prev; -}; - -typedef struct eoks_s eoks_t; -struct eoks_s { - pthread_mutex_t lock; - ep_over_kq_t **eoks; // note: this memory leaks when process terminates - int neoks; // we can add an extra api to let user clean - ep_over_kq_t *eoks_free_list; // currently, we just keep it simple stupid -}; - -static eoks_t eoks = { - .lock = PTHREAD_MUTEX_INITIALIZER, - .eoks = NULL, - .neoks = 0, - .eoks_free_list = NULL, -}; - -#ifdef ENABLE_LOG -static const char* op_str(int op) { - switch (op) { - case EPOLL_CTL_ADD: return "EPOLL_CTL_ADD"; - case EPOLL_CTL_MOD: return "EPOLL_CTL_MOD"; - case EPOLL_CTL_DEL: return "EPOLL_CTL_DEL"; - default: return "UNKNOWN"; - } -} - -static __thread char buf_slots[10][1024] = {0}; -static __thread int buf_slots_linelen = sizeof(buf_slots[0])/sizeof(buf_slots[0][0]); -static __thread int buf_slots_count = sizeof(buf_slots)/(sizeof(buf_slots[0])/sizeof(buf_slots[0][0])); - -static const char* events_str(uint32_t events, int slots) { - A(slots>=0 && slots on linux - // EPOLLIN = 0x001, - // #define EPOLLIN EPOLLIN - // EPOLLPRI = 0x002, - // #define EPOLLPRI EPOLLPRI - // EPOLLOUT = 0x004, - // #define EPOLLOUT EPOLLOUT - // EPOLLRDNORM = 0x040, - // #define EPOLLRDNORM EPOLLRDNORM - // EPOLLRDBAND = 0x080, - // #define EPOLLRDBAND EPOLLRDBAND - // EPOLLWRNORM = 0x100, - // #define EPOLLWRNORM EPOLLWRNORM - // EPOLLWRBAND = 0x200, - // #define EPOLLWRBAND EPOLLWRBAND - // EPOLLMSG = 0x400, - // #define EPOLLMSG EPOLLMSG - // EPOLLERR = 0x008, - // #define EPOLLERR EPOLLERR - // EPOLLHUP = 0x010, - // #define EPOLLHUP EPOLLHUP - // EPOLLRDHUP = 0x2000, - // #define EPOLLRDHUP EPOLLRDHUP - // EPOLLEXCLUSIVE = 1u << 28, - // #define EPOLLEXCLUSIVE EPOLLEXCLUSIVE - // EPOLLWAKEUP = 1u << 29, - // #define EPOLLWAKEUP EPOLLWAKEUP - // EPOLLONESHOT = 1u << 30, - // #define EPOLLONESHOT EPOLLONESHOT - // EPOLLET = 1u << 31 - // #define EPOLLET EPOLLET -#define CHK_EV(ev) \ - if (len>0 && (events & (ev))==(ev)) { \ - n = snprintf(p, len, "%s%s", p!=buf ? "|" : "", #ev); \ - p += n; \ - len -= n; \ - } - CHK_EV(EPOLLIN); - CHK_EV(EPOLLPRI); - CHK_EV(EPOLLOUT); - CHK_EV(EPOLLRDNORM); - CHK_EV(EPOLLRDBAND); - CHK_EV(EPOLLWRNORM); - CHK_EV(EPOLLWRBAND); - CHK_EV(EPOLLMSG); - CHK_EV(EPOLLERR); - CHK_EV(EPOLLHUP); - CHK_EV(EPOLLRDHUP); - CHK_EV(EPOLLEXCLUSIVE); - CHK_EV(EPOLLWAKEUP); - CHK_EV(EPOLLONESHOT); - CHK_EV(EPOLLET); -#undef CHK_EV - return buf; -} - -static const char* kev_flags_str(uint16_t flags, int slots) { - A(slots>=0 && slots - // #define EV_ADD 0x0001 /* add event to kq (implies enable) */ - // #define EV_DELETE 0x0002 /* delete event from kq */ - // #define EV_ENABLE 0x0004 /* enable event */ - // #define EV_DISABLE 0x0008 /* disable event (not reported) */ - // /* flags */ - // #define EV_ONESHOT 0x0010 /* only report one occurrence */ - // #define EV_CLEAR 0x0020 /* clear event state after reporting */ - // #define EV_RECEIPT 0x0040 /* force immediate event output */ - // /* ... with or without EV_ERROR */ - // /* ... use KEVENT_FLAG_ERROR_EVENTS */ - // /* on syscalls supporting flags */ - // #define EV_DISPATCH 0x0080 /* disable event after reporting */ - // #define EV_UDATA_SPECIFIC 0x0100 /* unique kevent per udata value */ - // #define EV_DISPATCH2 (EV_DISPATCH | EV_UDATA_SPECIFIC) - // /* ... in combination with EV_DELETE */ - // /* will defer delete until udata-specific */ - // /* event enabled. EINPROGRESS will be */ - // /* returned to indicate the deferral */ - // #define EV_VANISHED 0x0200 /* report that source has vanished */ - // /* ... only valid with EV_DISPATCH2 */ - // #define EV_SYSFLAGS 0xF000 /* reserved by system */ - // #define EV_FLAG0 0x1000 /* filter-specific flag */ - // #define EV_FLAG1 0x2000 /* filter-specific flag */ - // /* returned values */ - // #define EV_EOF 0x8000 /* EOF detected */ - // #define EV_ERROR 0x4000 /* error, data contains errno */ -#define CHK_EV(ev) \ - if (len>0 && (flags & (ev))==(ev)) { \ - n = snprintf(p, len, "%s%s", p!=buf ? "|" : "", #ev); \ - p += n; \ - len -= n; \ - } - CHK_EV(EV_ADD); - CHK_EV(EV_DELETE); - CHK_EV(EV_ENABLE); - CHK_EV(EV_DISABLE); - CHK_EV(EV_ONESHOT); - CHK_EV(EV_CLEAR); - CHK_EV(EV_RECEIPT); - CHK_EV(EV_DISPATCH); - CHK_EV(EV_UDATA_SPECIFIC); - CHK_EV(EV_DISPATCH2); - CHK_EV(EV_VANISHED); - CHK_EV(EV_SYSFLAGS); - CHK_EV(EV_FLAG0); - CHK_EV(EV_FLAG1); - CHK_EV(EV_EOF); - CHK_EV(EV_ERROR); -#undef CHK_EV - return buf; -} -#endif // ENABLE_LOG - -static ep_over_kq_t* eoks_alloc(void); -static void eoks_free(ep_over_kq_t *eok); -static ep_over_kq_t* eoks_find(int epfd); - -static eok_event_t* eok_find_ev(ep_over_kq_t *eok, int fd); -static eok_event_t* eok_calloc_ev(ep_over_kq_t *eok); -static void eok_free_ev(ep_over_kq_t *eok, eok_event_t *ev); -static void eok_wakeup(ep_over_kq_t *eok); - -static int eok_chgs_refresh(ep_over_kq_t *eok, eok_event_t *oev, eok_event_t *ev, struct kevent64_s *krev, struct kevent64_s *kwev); - -static struct kevent64_s* eok_alloc_eventslist(ep_over_kq_t *eok, int maxevents); - -int epoll_create(int size) { - (void)size; - int e = 0; - ep_over_kq_t *eok = eoks_alloc(); - if (!eok) return -1; - - A(eok->kq==-1, "internal logic error"); - A(eok->lock_valid, "internal logic error"); - A(eok->idx>=0 && eok->idxnext==NULL, "internal logic error"); - A(eok->sv[0]==-1, "internal logic error"); - A(eok->sv[1]==-1, "internal logic error"); - - eok->kq = kqueue(); - if (eok->kq==-1) { - e = errno; - eoks_free(eok); - errno = e; - return -1; - } - - if (socketpair(AF_LOCAL, SOCK_STREAM, 0, eok->sv)) { - e = errno; - eoks_free(eok); - errno = e; - return -1; - } - - struct epoll_event ev = {0}; - ev.events = EPOLLIN; - ev.data.ptr = &eok_dummy; - D("epoll_create epfd:[%d] and sv0[%d]", eok->idx, eok->sv[0]); - if (epoll_ctl(eok->idx, EPOLL_CTL_ADD, eok->sv[0], &ev)) { - e = errno; - epoll_close(eok->idx); - errno = e; - return -1; - } - - return eok->idx; -} - -int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) { - D("epoll_ctling epfd:[%d], op:[%s], fd:[%d], events:[%04x:%s]", - epfd, op_str(op), fd, - event ? event->events : 0, - event ? events_str(event->events, 0) : "NULL"); - int e = 0; - if (epfd<0 || epfd>=eoks.neoks) { - errno = EBADF; - return -1; - } - if (fd==-1) { - errno = EBADF; - return -1; - } - if (event && !(event->events & (EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP | EPOLLOUT))) { - e = ENOTSUP; - return -1; - } - - ep_over_kq_t *eok = eoks_find(epfd); - if (!eok) { - errno = EBADF; - return -1; - } - - A(0==pthread_mutex_lock(&eok->lock), ""); - do { - eok_event_t* oev = eok_find_ev(eok, fd); - if (op==EPOLL_CTL_ADD && oev) { - e = EEXIST; - break; - } - if (op!=EPOLL_CTL_ADD && !oev) { - e = ENOENT; - break; - } - if (op!=EPOLL_CTL_DEL && !event) { - e = EINVAL; - break; - } - - // prepare krev/kwev - struct kevent64_s krev = {0}; - struct kevent64_s kwev = {0}; - krev.ident = -1; - kwev.ident = -1; - uint16_t flags = 0; - // prepare internal eok event - eok_event_t ev = {0}; - ev.fd = fd; - if (event) ev.epev = *event; - struct epoll_event *pev = event; - switch (op) { - case EPOLL_CTL_ADD: { - flags = EV_ADD; - ev.changed = 1; - } break; - case EPOLL_CTL_MOD: { - flags = EV_ADD; - ev.changed = 2; - } break; - case EPOLL_CTL_DEL: { - // event is ignored - // pev points to registered epoll_event - pev = &oev->epev; - flags = EV_DELETE; - ev.changed = 3; - } break; - default: { - e = ENOTSUP; - } break; - } - - if (e) break; - - // udata will be delayed to be set - if (pev->events & (EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP)) { - flags |= EV_EOF; - EV_SET64(&krev, ev.fd, EVFILT_READ, flags, 0, 0, -1, 0, 0); - } - if (pev->events & EPOLLOUT) { - EV_SET64(&kwev, ev.fd, EVFILT_WRITE, flags, 0, 0, -1, 0, 0); - } - - // refresh registered evlist and changelist in a transaction way - if (eok_chgs_refresh(eok, oev, &ev, &krev, &kwev)) { - e = errno; - A(e, "internal logic error"); - break; - } - eok->changed = 1; - eok_wakeup(eok); - } while (0); - A(0==pthread_mutex_unlock(&eok->lock), ""); - - if (e) { - errno = e; - return -1; - } - - return 0; -} - -static struct timespec do_timespec_diff(struct timespec *from, struct timespec *to); - -int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) { - int e = 0; - if (!events) { - errno = EINVAL; - E("epoll_waiting epfd:[%d], maxevents:[%d], timeout:[%d] failed", epfd, maxevents, timeout); - return -1; - } - if (maxevents<=0) { - errno = EINVAL; - E("epoll_waiting epfd:[%d], maxevents:[%d], timeout:[%d] failed", epfd, maxevents, timeout); - return -1; - } - - struct timespec abstime = {0}; - A(TIME_UTC==timespec_get(&abstime, TIME_UTC), "internal logic error"); - - if (timeout!=-1) { - if (timeout<0) timeout = 0; - int64_t t = abstime.tv_nsec + timeout * 1000000; - abstime.tv_sec += t / 1000000000; - abstime.tv_nsec = t % 1000000000; - } - - int r = 0; - - ep_over_kq_t *eok = eoks_find(epfd); - if (!eok) { - errno = EBADF; - E("epoll_waiting epfd:[%d], maxevents:[%d], timeout:[%d] failed", epfd, maxevents, timeout); - errno = EBADF; - return -1; - } - - int cnts = 0; - A(0==pthread_mutex_lock(&eok->lock), ""); - do { - cnts = 0; - A(eok->waiting==0, "internal logic error"); - struct kevent64_s *eventslist = eok_alloc_eventslist(eok, maxevents); - if (!eventslist) { - e = ENOMEM; - E("epoll_waiting epfd:[%d], maxevents:[%d], timeout:[%d] failed", epfd, maxevents, timeout); - break; - } - memset(eventslist, 0, maxevents * sizeof(*eventslist)); - - struct timespec now = {0}; - A(TIME_UTC==timespec_get(&now, TIME_UTC), "internal logic error"); - struct timespec to = do_timespec_diff(&now, &abstime); - struct timespec *pto = &to; - if (timeout==-1) { - pto = NULL; - } - - // taking the changelist - struct kevent64_s *kchanges = eok->kchanges; - int nchanges = eok->nchanges; - int ichanges = eok->ichanges; - // let outside world to add changes - eok->kchanges = NULL; - eok->nchanges = 0; - eok->ichanges = 0; - - eok->changed = 0; - eok->wakenup = 0; - eok->waiting = 1; - - A(0==pthread_mutex_unlock(&eok->lock), ""); - if (ichanges>0) { - D("kevent64 epfd[%d] changing [%d] changes and waiting...", eok->idx, ichanges); - } - errno = 0; - r = kevent64(eok->kq, kchanges, ichanges, eventslist, maxevents, 0, pto); - e = errno; - if (e) { - E("kevent64 epfd[%d] waiting done, with r[%d]", eok->idx, r); - } - A(0==pthread_mutex_lock(&eok->lock), ""); - - eok->waiting = 0; - - if (kchanges) { - if (eok->kchanges==NULL) { - // reuse - A(eok->nchanges==0 && eok->ichanges==0, "internal logic error"); - eok->kchanges = kchanges; - eok->nchanges = nchanges; - } else { - free(kchanges); - kchanges = NULL; - } - nchanges = 0; - ichanges = 0; - } - - if (r==0) { - A(timeout!=-1, "internal logic error"); - } - for (int i=0; iudata && eok->evs_head && eok->evs_tail, "internal logic error"); - eok_event_t *ev = (eok_event_t*)kev->udata; - A(kev->ident == ev->fd, "internal logic error"); - if (kev->flags & EV_ERROR) { - D("epfd[%d] error when processing change list for fd[%d], error[%s], kev_flags:[%04x:%s]", - epfd, ev->fd, strerror(kev->data), kev->flags, kev_flags_str(kev->flags, 0)); - } - switch (kev->filter) { - case EVFILT_READ: { - A((ev->epev.events & EPOLLIN), "internal logic errro"); - if (ev->epev.data.ptr==&eok_dummy) { - // it's coming from wakeup socket pair - char c = '\0'; - A(1==recv(kev->ident, &c, 1, 0), "internal logic error"); - A(0==memcmp(&c, "1", 1), "internal logic error"); - D("epfd[%d] wokenup", epfd); - continue; - } else { - if (ev->changed==3) { - D("epfd[%d] already requested to delete for fd[%d]", epfd, ev->fd); - // TODO: write a unit test for this case - // EV_DELETE? - continue; - } - // converting to epoll_event - // we shall collect all kevents for the uniq fd into one epoll_evnt - // but currently, taos never use EPOLLOUT - // just let it this way for the moment - struct epoll_event pev = {0}; - pev.data.ptr = ev->epev.data.ptr; - pev.events = EPOLLIN; - if (kev->flags & EV_EOF) { - // take all these as EOF for the moment - pev.events |= (EPOLLHUP | EPOLLERR | EPOLLRDHUP); - } - // rounded to what user care - pev.events = pev.events & ev->epev.events; - D("epfd[%d] events found for fd[%d]: [%04x:%s], which was registered: [%04x:%s], kev_flags: [%04x:%s]", - epfd, - ev->fd, pev.events, events_str(pev.events, 0), - ev->epev.events, events_str(ev->epev.events, 1), - kev->flags, kev_flags_str(kev->flags, 2)); - // now we get ev and store it - events[cnts++] = pev; - } - } break; - case EVFILT_WRITE: { - A(0, "not implemented yet"); - } break; - default: { - A(0, "internal logic error"); - } break; - } - } - if (r>=0) { - // we can safely rule out delete-requested-events from the regitered evlists - // if only changelist are correctly registered - eok_event_t *p = eok->evs_head; - while (p) { - eok_event_t *next = p->next; - if (p->changed==3) { - D("epfd[%d] removing registered event for fd[%d]: [%04x:%s]", epfd, p->fd, p->epev.events, events_str(p->epev.events, 0)); - eok_free_ev(eok, p); - } - p = next; - } - } - if (cnts==0) { - // if no user-cared-events is up - // we check to see if time is up - if (timeout!=-1) { - A(TIME_UTC==timespec_get(&now, TIME_UTC), "internal logic error"); - to = do_timespec_diff(&now, &abstime); - if (to.tv_sec==0 && to.tv_nsec==0) break; - } - // time is not up yet, continue loop - } - } while (cnts==0); - if (cnts>0) { - D("kevent64 epfd[%d] waiting done with [%d] events", epfd, cnts); - } - A(0==pthread_mutex_unlock(&eok->lock), ""); - - if (e) { - errno = e; - E("epfd[%d] epoll_wait failed", epfd); - return -1; - } - - // tell user how many events are valid - return cnts; -} - -static struct timespec do_timespec_diff(struct timespec *from, struct timespec *to) { - struct timespec delta; - delta.tv_sec = to->tv_sec - from->tv_sec; - delta.tv_nsec = to->tv_nsec - from->tv_nsec; - // norm and round up - while (delta.tv_nsec<0) { - delta.tv_sec -= 1; - delta.tv_nsec += 1000000000; - } - if (delta.tv_sec < 0) { - delta.tv_sec = 0; - delta.tv_nsec = 0; - } - return delta; -} - -int epoll_close(int epfd) { - D("epoll_closing epfd: [%d]", epfd); - ep_over_kq_t *eok = eoks_find(epfd); - if (!eok) { - errno = EBADF; - return -1; - } - - A(0==pthread_mutex_lock(&eok->lock), ""); - do { - // panic if it would be double-closed - A(eok->stopping==0, "internal logic error"); - eok->stopping = 1; - // panic if epoll_wait is pending - A(eok->waiting==0, "internal logic error"); - - if (eok->kq!=-1) { - close(eok->kq); - eok->kq = -1; - } - } while (0); - A(0==pthread_mutex_unlock(&eok->lock), ""); - eoks_free(eok); - - return 0; -} - -static struct kevent64_s* eok_alloc_eventslist(ep_over_kq_t *eok, int maxevents) { - if (maxevents<=eok->nevslist) return eok->kevslist; - struct kevent64_s *p = (struct kevent64_s*)realloc(eok->kevslist, sizeof(*p)*maxevents); - if (!p) return NULL; - eok->kevslist = p; - eok->nevslist = maxevents; - return p; -} - -static eok_event_t* eok_find_ev(ep_over_kq_t *eok, int fd) { - eok_event_t *p = eok->evs_head; - while (p) { - if (p->fd == fd) return p; - p = p->next; - } - errno = ENOENT; - return NULL; -} - -static eok_event_t* eok_calloc_ev(ep_over_kq_t *eok) { - eok_event_t *p = NULL; - if (eok->evs_free) { - p = eok->evs_free; - eok->evs_free = p->next; - p->next = NULL; - A(p->prev==NULL, "internal logic error"); - } else { - p = (eok_event_t*)calloc(1, sizeof(*p)); - if (!p) return NULL; - A(p->next==NULL, "internal logic error"); - A(p->prev==NULL, "internal logic error"); - } - // dirty link - p->prev = eok->evs_tail; - if (eok->evs_tail) eok->evs_tail->next = p; - else eok->evs_head = p; - eok->evs_tail = p; - - eok->evs_count += 1; - - return p; -} - -static void eok_free_ev(ep_over_kq_t *eok, eok_event_t *ev) { - if (ev->prev) ev->prev->next = ev->next; - else eok->evs_head = ev->next; - if (ev->next) ev->next->prev = ev->prev; - else eok->evs_tail = ev->prev; - ev->prev = NULL; - ev->next = eok->evs_free; - eok->evs_free = ev; - - eok->evs_count -= 1; -} - -static void eok_wakeup(ep_over_kq_t *eok) { - if (!eok->waiting) return; - if (eok->wakenup) return; - eok->wakenup = 1; - A(1==send(eok->sv[1], "1", 1, 0), ""); -} - -static int eok_chgs_refresh(ep_over_kq_t *eok, eok_event_t *oev, eok_event_t *ev, struct kevent64_s *krev, struct kevent64_s *kwev) { - if (!oev) oev = eok_calloc_ev(eok); - if (!oev) return -1; - int n = 0; - if (krev->ident==ev->fd) ++n; - if (kwev->ident==ev->fd) ++n; - A(n, "internal logic error"); - if (eok->ichanges+n>eok->nchanges) { - struct kevent64_s *p = (struct kevent64_s*)realloc(eok->kchanges, sizeof(*p) * (eok->nchanges + 10)); - if (!p) { - if (ev->changed==1) { - // roll back - A(oev, "internal logic error"); - eok_free_ev(eok, oev); - } - errno = ENOMEM; - return -1; - } - eok->kchanges = p; - eok->nchanges += 10; - } - - // copy to registered event slot - oev->fd = ev->fd; - if (ev->changed!=3) { - // if add/mod, copy epoll_event - oev->epev = ev->epev; - } - oev->changed = ev->changed; - - // copy to changes list - n = 0; - if (krev->ident==ev->fd) { - krev->udata = (uint64_t)oev; - eok->kchanges[eok->ichanges++] = *krev; - ++n; - } - if (kwev->ident==ev->fd) { - kwev->udata = (uint64_t)oev; - eok->kchanges[eok->ichanges++] = *kwev; - ++n; - } - D("epfd[%d]: add #changes[%d] for fd[%d], and now #changes/registers [%d/%d]", eok->idx, n, ev->fd, eok->ichanges, eok->evs_count); - return 0; -} - -static ep_over_kq_t* eoks_alloc(void) { - ep_over_kq_t *eok = NULL; - - A(0==pthread_mutex_lock(&eoks.lock), ""); - do { - if (eoks.eoks_free_list) { - eok = eoks.eoks_free_list; - eoks.eoks_free_list = eok->next; - A(eoks.eoks, "internal logic error"); - A(eok->idx>=0 && eok->idxidx)==NULL, "internal logic error"); - *(eoks.eoks + eok->idx) = eok; - eok->next = NULL; - eok->stopping = 0; - break; - } - eok = (ep_over_kq_t*)calloc(1, sizeof(*eok)); - if (!eok) break; - eok->idx = -1; - ep_over_kq_t **ar = (ep_over_kq_t**)realloc(eoks.eoks, sizeof(**ar) * (eoks.neoks+1)); - if (!ar) break; - eoks.eoks = ar; - *(eoks.eoks + eoks.neoks) = eok; - eok->idx = eoks.neoks; - eok->kq = -1; - eok->sv[0] = -1; - eok->sv[1] = -1; - eoks.neoks += 1; - } while (0); - A(0==pthread_mutex_unlock(&eoks.lock), ""); - - if (!eok) { - errno = ENOMEM; - return NULL; - } - if (eok->idx==-1) { - free(eok); - errno = ENOMEM; - return NULL; - } - if (eok->lock_valid) { - return eok; - } - - if (0==pthread_mutex_init(&eok->lock, NULL)) { - eok->lock_valid = 1; - return eok; - } - - eoks_free(eok); - errno = ENOMEM; - return NULL; -} - -static void eoks_free(ep_over_kq_t *eok) { - A(0==pthread_mutex_lock(&eoks.lock), ""); - do { - A(eok->idx>=0 && eok->idxnext==NULL, "internal logic error"); - - // leave eok->kchanges as is - A(eok->ichanges==0, "internal logic error"); - - A(eok->waiting==0, "internal logic error"); - eok_event_t *ev = eok->evs_head; - int sv_closed = 0; - while (ev) { - eok_event_t *next = ev->next; - if (ev->fd==eok->sv[0]) { - // fd is critical system resource - A(sv_closed==0, "internal logic error"); - close(eok->sv[0]); - eok->sv[0] = -1; - close(eok->sv[1]); - eok->sv[1] = -1; - eok_free_ev(eok, ev); - } else { - // user forget calling epoll_ctl(EPOLL_CTL_DEL) before calling epoll_close/close? - // calling close(ev->fd) here smells really bad -#ifndef BALANCE_CHECK_WHEN_CLOSE - // we just let it be and reclaim ev - eok_free_ev(eok, ev); -#else - // panic otherwise, if BALANCE_CHECK_WHEN_CLOSE is defined - A(eok->evs_head==NULL && eok->evs_tail==NULL && eok->evs_count==0, - "epfd[%d] fd[%d]: internal logic error: have you epoll_ctl(EPOLL_CTL_DEL) everything before calling epoll_close?", - eok->idx, ev->fd); -#endif - } - ev = next; - } - // if (eok->evs_count==1) { - // A(eok->evs_head && eok->evs_tail && eok->evs_head==eok->evs_tail, "internal logic error"); - // A(eok->evs_head->fd==eok->sv[0] && eok->sv[0]!=-1 && eok->sv[1]!=-1, "internal logic error"); - // // fd is critical system resource - // close(eok->sv[0]); - // eok->sv[0] = -1; - // close(eok->sv[1]); - // eok->sv[1] = -1; - // eok_free_ev(eok, eok->evs_head); - // } - A(eok->evs_head==NULL && eok->evs_tail==NULL && eok->evs_count==0, - "internal logic error: have you epoll_ctl(EPOLL_CTL_DEL) everything before calling epoll_close?"); - A(eok->sv[0]==-1 && eok->sv[1]==-1, "internal logic error"); - if (eok->kq!=-1) { - close(eok->kq); - eok->kq = -1; - } - eok->next = eoks.eoks_free_list; - eoks.eoks_free_list = eok; - *(eoks.eoks + eok->idx) = NULL; - } while (0); - A(0==pthread_mutex_unlock(&eoks.lock), ""); -} - -static ep_over_kq_t* eoks_find(int epfd) { - ep_over_kq_t *eok = NULL; - A(0==pthread_mutex_lock(&eoks.lock), ""); - do { - if (epfd<0 || epfd>=eoks.neoks) { - break; - } - A(eoks.eoks, "internal logic error"); - eok = *(eoks.eoks + epfd); - A(eok->next==NULL, "internal logic error"); - A(eok->lock_valid, "internal logic error"); - } while (0); - A(0==pthread_mutex_unlock(&eoks.lock), ""); - return eok; -} - diff --git a/src/os/src/darwin/dwSocket.c b/src/os/src/darwin/dwSocket.c deleted file mode 100644 index 69a4666d34394ef71da1d18559f8ba2f8f9cb290..0000000000000000000000000000000000000000 --- a/src/os/src/darwin/dwSocket.c +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" - -int taosSetSockOpt(SOCKET socketfd, int level, int optname, void *optval, int optlen) { - if (level == SOL_SOCKET && optname == SO_SNDBUF) { - return 0; - } - - if (level == SOL_SOCKET && optname == SO_RCVBUF) { - return 0; - } - - return setsockopt(socketfd, level, optname, optval, (socklen_t)optlen); -} diff --git a/src/os/src/darwin/dwSysInfo.c b/src/os/src/darwin/dwSysInfo.c deleted file mode 100644 index 54c6fb1d32a124171d121cbc5bc8f2e9ac5661ac..0000000000000000000000000000000000000000 --- a/src/os/src/darwin/dwSysInfo.c +++ /dev/null @@ -1,273 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "tconfig.h" -#include "tglobal.h" -#include "tulog.h" -#include "taoserror.h" -#include -#include - -static void taosGetSystemTimezone() { - SGlobalCfg *cfg_timezone = taosGetConfigOption("timezone"); - if (cfg_timezone == NULL) return; - if (cfg_timezone->cfgStatus >= TAOS_CFG_CSTATUS_DEFAULT) { - return; - } - - /* load time zone string from /etc/localtime */ - char buf[4096]; - char *tz = NULL; { - int n = readlink("/etc/localtime", buf, sizeof(buf)); - if (n<0) { - uError("read /etc/localtime error, reason:%s", strerror(errno)); - return; - } - buf[n] = '\0'; - for(int i=n-1; i>=0; --i) { - if (buf[i]=='/') { - if (tz) { - tz = buf + i + 1; - break; - } - tz = buf + i + 1; - } - } - if (!tz || 0==strchr(tz, '/')) { - uError("parsing /etc/localtime failed"); - return; - } - - setenv("TZ", tz, 1); - tzset(); - } - - /* - * NOTE: do not remove it. - * Enforce set the correct daylight saving time(DST) flag according - * to current time - */ - time_t tx1 = time(NULL); - struct tm tm1; - localtime_r(&tx1, &tm1); - - /* - * format example: - * - * Asia/Shanghai (CST, +0800) - * Europe/London (BST, +0100) - */ - snprintf(tsTimezone, TSDB_TIMEZONE_LEN, "%s (%s, %+03ld00)", - tz, tm1.tm_isdst ? tzname[daylight] : tzname[0], -timezone/3600); - - // cfg_timezone->cfgStatus = TAOS_CFG_CSTATUS_DEFAULT; - uWarn("timezone not configured, set to system default:%s", tsTimezone); -} - -/* - * originally from src/os/src/detail/osSysinfo.c - * POSIX format locale string: - * (Language Strings)_(Country/Region Strings).(code_page) - * - * example: en_US.UTF-8, zh_CN.GB18030, zh_CN.UTF-8, - * - * if user does not specify the locale in taos.cfg the program use default LC_CTYPE as system locale. - * - * In case of some CentOS systems, their default locale is "en_US.utf8", which is not valid code_page - * for libiconv that is employed to convert string in this system. This program will automatically use - * UTF-8 instead as the charset. - * - * In case of windows client, the locale string is not valid POSIX format, user needs to set the - * correct code_page for libiconv. Usually, the code_page of windows system with simple chinese is - * CP936, CP437 for English charset. - * - */ -static void taosGetSystemLocale() { // get and set default locale - char sep = '.'; - char *locale = NULL; - - SGlobalCfg *cfg_locale = taosGetConfigOption("locale"); - if (cfg_locale && cfg_locale->cfgStatus < TAOS_CFG_CSTATUS_DEFAULT) { - locale = setlocale(LC_CTYPE, ""); - if (locale == NULL) { - uError("can't get locale from system, set it to en_US.UTF-8 since error:%d:%s", errno, strerror(errno)); - strcpy(tsLocale, "en_US.UTF-8"); - } else { - tstrncpy(tsLocale, locale, TSDB_LOCALE_LEN); - uWarn("locale not configured, set to system default:%s", tsLocale); - } - } - - /* if user does not specify the charset, extract it from locale */ - SGlobalCfg *cfg_charset = taosGetConfigOption("charset"); - if (cfg_charset && cfg_charset->cfgStatus < TAOS_CFG_CSTATUS_DEFAULT) { - char *str = strrchr(tsLocale, sep); - if (str != NULL) { - str++; - - char *revisedCharset = taosCharsetReplace(str); - tstrncpy(tsCharset, revisedCharset, TSDB_LOCALE_LEN); - - free(revisedCharset); - uWarn("charset not configured, set to system default:%s", tsCharset); - } else { - strcpy(tsCharset, "UTF-8"); - uWarn("can't get locale and charset from system, set it to UTF-8"); - } - } -} - -void taosPrintOsInfo() { - uInfo(" os pageSize: %" PRId64 "(KB)", tsPageSize / 1024); - // uInfo(" os openMax: %" PRId64, tsOpenMax); - // uInfo(" os streamMax: %" PRId64, tsStreamMax); - uInfo(" os numOfCores: %d", tsNumOfCores); - uInfo(" os totalMemory: %d(MB)", tsTotalMemoryMB); - - struct utsname buf; - if (uname(&buf)) { - uInfo(" can't fetch os info"); - return; - } - uInfo(" os sysname: %s", buf.sysname); - uInfo(" os nodename: %s", buf.nodename); - uInfo(" os release: %s", buf.release); - uInfo(" os version: %s", buf.version); - uInfo(" os machine: %s", buf.machine); - uInfo("=================================="); -} - -void taosPrintDiskInfo() { - uInfo("=================================="); - uInfo(" os totalDisk: %f(GB)", tsTotalDataDirGB); - uInfo(" os usedDisk: %f(GB)", tsUsedDataDirGB); - uInfo(" os availDisk: %f(GB)", tsAvailDataDirGB); - uInfo("=================================="); -} - -void taosKillSystem() { - uError("function taosKillSystem, exit!"); - exit(0); -} - -int32_t taosGetCpuCores() { - return sysconf(_SC_NPROCESSORS_ONLN); -} - -void taosGetSystemInfo() { - // taosGetProcInfos(); - - tsNumOfCores = sysconf(_SC_NPROCESSORS_ONLN); - long physical_pages = sysconf(_SC_PHYS_PAGES); - long page_size = sysconf(_SC_PAGESIZE); - tsTotalMemoryMB = physical_pages * page_size / (1024 * 1024); - tsPageSize = page_size; - - // float tmp1, tmp2; - // taosGetSysMemory(&tmp1); - // taosGetProcMemory(&tmp2); - // taosGetDisk(); - // taosGetBandSpeed(&tmp1); - // taosGetCpuUsage(&tmp1, &tmp2); - // taosGetProcIO(&tmp1, &tmp2); - - taosGetSystemTimezone(); - taosGetSystemLocale(); -} - -bool taosReadProcIO(int64_t *rchars, int64_t *wchars) { - if (rchars) *rchars = 0; - if (wchars) *wchars = 0; - return true; -} - -bool taosGetProcIO(float *readKB, float *writeKB) { - *readKB = 0; - *writeKB = 0; - return true; -} - -bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { - if (bytes) *bytes = 0; - if (rbytes) *rbytes = 0; - if (tbytes) *tbytes = 0; - return true; -} - -bool taosGetBandSpeed(float *bandSpeedKb) { - *bandSpeedKb = 0; - return true; -} - -bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { - *sysCpuUsage = 0; - *procCpuUsage = 0; - return true; -} - -bool taosGetProcMemory(float *memoryUsedMB) { - *memoryUsedMB = 0; - return true; -} - -bool taosGetSysMemory(float *memoryUsedMB) { - *memoryUsedMB = 0; - return true; -} - -int taosSystem(const char *cmd) { - uError("un support funtion"); - return -1; -} - -void taosSetCoreDump() {} - -int32_t taosGetDiskSize(char *dataDir, SysDiskSize *diskSize) { - struct statvfs info; - if (statvfs(dataDir, &info)) { - uError("failed to get disk size, dataDir:%s errno:%s", tsDataDir, strerror(errno)); - terrno = TAOS_SYSTEM_ERROR(errno); - return -1; - } else { - diskSize->tsize = info.f_blocks * info.f_frsize; - diskSize->avail = info.f_bavail * info.f_frsize; - diskSize->used = (info.f_blocks - info.f_bfree) * info.f_frsize; - return 0; - } -} - -char cmdline[1024]; - -char *taosGetCmdlineByPID(int pid) { - errno = 0; - - if (proc_pidpath(pid, cmdline, sizeof(cmdline)) <= 0) { - fprintf(stderr, "PID is %d, %s", pid, strerror(errno)); - return strerror(errno); - } - - return cmdline; -} - -bool taosGetSystemUid(char *uid) { - uuid_t uuid = {0}; - uuid_generate(uuid); - // it's caller's responsibility to make enough space for `uid`, that's 36-char + 1-null - uuid_unparse_lower(uuid, uid); - return true; -} - diff --git a/src/os/src/darwin/dwTimer.c b/src/os/src/darwin/dwTimer.c deleted file mode 100644 index d395a7f53f2baa9a806ab8cbe96253deb7334f6e..0000000000000000000000000000000000000000 --- a/src/os/src/darwin/dwTimer.c +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -// fail-fast or let-it-crash philosophy -// https://en.wikipedia.org/wiki/Fail-fast -// https://stackoverflow.com/questions/4393197/erlangs-let-it-crash-philosophy-applicable-elsewhere -// experimentally, we follow log-and-crash here - -#define _DEFAULT_SOURCE -#include "os.h" - -#if 1 -#include - -static void (*timer_callback)(int); -static int timer_ms = 0; -static pthread_t timer_thread; -static int timer_kq = -1; -static volatile int timer_stop = 0; - -static void* timer_routine(void *arg) { - (void)arg; - setThreadName("timer"); - - int r = 0; - struct timespec to = {0}; - to.tv_sec = timer_ms / 1000; - to.tv_nsec = (timer_ms % 1000) * 1000000; - while (!timer_stop) { - struct kevent64_s kev[10] = {0}; - r = kevent64(timer_kq, NULL, 0, kev, sizeof(kev)/sizeof(kev[0]), 0, &to); - if (r!=0) { - fprintf(stderr, "==%s[%d]%s()==kevent64 failed\n", basename(__FILE__), __LINE__, __func__); - abort(); - } - timer_callback(SIGALRM); // just mock - } - - return NULL; -} - -int taosInitTimer(void (*callback)(int), int ms) { - int r = 0; - timer_ms = ms; - timer_callback = callback; - - timer_kq = kqueue(); - if (timer_kq==-1) { - fprintf(stderr, "==%s[%d]%s()==failed to create timer kq\n", basename(__FILE__), __LINE__, __func__); - // since no caller of this func checks the return value for the moment - abort(); - } - - r = pthread_create(&timer_thread, NULL, timer_routine, NULL); - if (r) { - fprintf(stderr, "==%s[%d]%s()==failed to create timer thread\n", basename(__FILE__), __LINE__, __func__); - // since no caller of this func checks the return value for the moment - abort(); - } - return 0; -} - -void taosUninitTimer() { - int r = 0; - timer_stop = 1; - r = pthread_join(timer_thread, NULL); - if (r) { - fprintf(stderr, "==%s[%d]%s()==failed to join timer thread\n", basename(__FILE__), __LINE__, __func__); - // since no caller of this func checks the return value for the moment - abort(); - } - close(timer_kq); - timer_kq = -1; -} - -void taos_block_sigalrm(void) { - // we don't know if there's any specific API for SIGALRM to deliver to specific thread - // this implementation relies on kqueue rather than SIGALRM -} -#else -int taosInitTimer(void (*callback)(int), int ms) { - signal(SIGALRM, callback); - - struct itimerval tv; - tv.it_interval.tv_sec = 0; /* my timer resolution */ - tv.it_interval.tv_usec = 1000 * ms; // resolution is in msecond - tv.it_value = tv.it_interval; - - setitimer(ITIMER_REAL, &tv, NULL); - - return 0; -} - -void taosUninitTimer() { - struct itimerval tv = { 0 }; - setitimer(ITIMER_REAL, &tv, NULL); -} - -void taos_block_sigalrm(void) { - // since SIGALRM has been used - // consideration: any better solution? - static __thread int already_set = 0; - if (!already_set) { - sigset_t set; - sigemptyset(&set); - sigaddset(&set, SIGALRM); - pthread_sigmask(SIG_BLOCK, &set, NULL); - already_set = 1; - } -} -#endif - diff --git a/src/os/src/detail/CMakeLists.txt b/src/os/src/detail/CMakeLists.txt deleted file mode 100644 index ac68cf4cd8cbd217da8aa2d4a41a5aa159562868..0000000000000000000000000000000000000000 --- a/src/os/src/detail/CMakeLists.txt +++ /dev/null @@ -1,21 +0,0 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.8...3.20) -PROJECT(TDengine) - -INCLUDE_DIRECTORIES(.) -INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/zlib-1.2.11/inc) -AUX_SOURCE_DIRECTORY(. SRC) -SET_SOURCE_FILES_PROPERTIES(osSysinfo.c PROPERTIES COMPILE_FLAGS -w) -SET_SOURCE_FILES_PROPERTIES(osCoredump.c PROPERTIES COMPILE_FLAGS -w) - -ADD_LIBRARY(os ${SRC}) - -IF (TD_LINUX) - TARGET_LINK_LIBRARIES(os oslinux) - IF (TD_ARM_32 OR TD_LINUX_32) - TARGET_LINK_LIBRARIES(os atomic) - ENDIF () -ELSEIF (TD_DARWIN) - TARGET_LINK_LIBRARIES(os osdarwin) -ELSEIF (TD_WINDOWS) - TARGET_LINK_LIBRARIES(os oswindows) -ENDIF () diff --git a/src/os/src/detail/osAtomic.c b/src/os/src/detail/osAtomic.c deleted file mode 100644 index ecda57352897921f8744de3be4acd52d474ffd36..0000000000000000000000000000000000000000 --- a/src/os/src/detail/osAtomic.c +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" - -#ifdef _TD_NINGSI_60 -void* atomic_exchange_ptr_impl(void** ptr, void* val ) { - void *old; - do { - old = *ptr; - } while( !__sync_bool_compare_and_swap(ptr, old, val) ); - return old; -} -int8_t atomic_exchange_8_impl(int8_t* ptr, int8_t val ) { - int8_t old; - do { - old = *ptr; - } while( !__sync_bool_compare_and_swap(ptr, old, val) ); - return old; -} -int16_t atomic_exchange_16_impl(int16_t* ptr, int16_t val ) { - int16_t old; - do { - old = *ptr; - } while( !__sync_bool_compare_and_swap(ptr, old, val) ); - return old; -} -int32_t atomic_exchange_32_impl(int32_t* ptr, int32_t val ) { - int32_t old; - do { - old = *ptr; - } while( !__sync_bool_compare_and_swap(ptr, old, val) ); - return old; -} -int64_t atomic_exchange_64_impl(int64_t* ptr, int64_t val ) { - int64_t old; - do { - old = *ptr; - } while( !__sync_bool_compare_and_swap(ptr, old, val) ); - return old; -} -#endif diff --git a/src/os/src/detail/osFail.c b/src/os/src/detail/osFail.c deleted file mode 100644 index 6ddeefc521264ac23d2fdf2961e20bc8c85a43b1..0000000000000000000000000000000000000000 --- a/src/os/src/detail/osFail.c +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" - -#define RANDOM_NETWORK_FAIL_FACTOR 20 - -#ifdef TAOS_RANDOM_NETWORK_FAIL - -int64_t taosSendRandomFail(int32_t sockfd, const void *buf, size_t len, int32_t flags) { - if (rand() % RANDOM_NETWORK_FAIL_FACTOR == 0) { - errno = ECONNRESET; - return -1; - } - - return send(sockfd, buf, len, flags); -} - -int64_t taosSendToRandomFail(int32_t sockfd, const void *buf, size_t len, int32_t flags, - const struct sockaddr *dest_addr, socklen_t addrlen) { - if (rand() % RANDOM_NETWORK_FAIL_FACTOR == 0) { - errno = ECONNRESET; - return -1; - } - - return sendto(sockfd, buf, len, flags, dest_addr, addrlen); -} - -int64_t taosReadSocketRandomFail(int32_t fd, void *buf, size_t count) { - if (rand() % RANDOM_NETWORK_FAIL_FACTOR == 0) { - errno = ECONNRESET; - return -1; - } - - return read(fd, buf, count); -} - -int64_t taosWriteSocketRandomFail(int32_t fd, const void *buf, size_t count) { - if (rand() % RANDOM_NETWORK_FAIL_FACTOR == 0) { - errno = EINTR; - return -1; - } - - return write(fd, buf, count); -} - -#endif //TAOS_RANDOM_NETWORK_FAIL - -#ifdef TAOS_RANDOM_FILE_FAIL - -static int32_t random_file_fail_factor = 20; -static FILE *fpRandomFileFailOutput = NULL; - -void taosSetRandomFileFailFactor(int32_t factor) { - random_file_fail_factor = factor; -} - -static void close_random_file_fail_output() { - if (fpRandomFileFailOutput != NULL) { - if (fpRandomFileFailOutput != stdout) { - fclose(fpRandomFileFailOutput); - } - fpRandomFileFailOutput = NULL; - } -} - -static void random_file_fail_output_sig(int32_t sig) { - fprintf(fpRandomFileFailOutput, "signal %d received.\n", sig); - - struct sigaction act = {0}; - act.sa_handler = SIG_DFL; - sigaction(sig, &act, NULL); - - close_random_file_fail_output(); - exit(EXIT_FAILURE); -} - -void taosSetRandomFileFailOutput(const char *path) { - if (path == NULL) { - fpRandomFileFailOutput = stdout; - } else if ((fpRandomFileFailOutput = fopen(path, "w")) != NULL) { - atexit(close_random_file_fail_output); - } else { - printf("failed to open random file fail log file '%s', errno=%d\n", path, errno); - return; - } - - struct sigaction act = {0}; - act.sa_handler = random_file_fail_output_sig; - sigaction(SIGFPE, &act, NULL); - sigaction(SIGSEGV, &act, NULL); - sigaction(SIGILL, &act, NULL); -} - -int64_t taosReadFileRandomFail(int32_t fd, void *buf, int32_t count, const char *file, uint32_t line) { - if (random_file_fail_factor > 0) { - if (rand() % random_file_fail_factor == 0) { - errno = EIO; - return -1; - } - } - - return taosRead(fd, buf, count); -} - -int64_t taosWriteFileRandomFail(int32_t fd, void *buf, int32_t count, const char *file, uint32_t line) { - if (random_file_fail_factor > 0) { - if (rand() % random_file_fail_factor == 0) { - errno = EIO; - return -1; - } - } - - return taosWrite(fd, buf, count); -} - -int64_t taosLSeekRandomFail(int32_t fd, int64_t offset, int32_t whence, const char *file, uint32_t line) { - if (random_file_fail_factor > 0) { - if (rand() % random_file_fail_factor == 0) { - errno = EIO; - return -1; - } - } - - return taosLSeek(fd, offset, whence); -} - -#endif //TAOS_RANDOM_FILE_FAIL diff --git a/src/os/src/detail/osMemory.c b/src/os/src/detail/osMemory.c deleted file mode 100644 index 22954f1523dc229b8446e339a2d142a0b4b7b023..0000000000000000000000000000000000000000 --- a/src/os/src/detail/osMemory.c +++ /dev/null @@ -1,521 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "tulog.h" - -#ifdef TAOS_MEM_CHECK - -static ETaosMemoryAllocMode allocMode = TAOS_ALLOC_MODE_DEFAULT; -static FILE* fpAllocLog = NULL; - -//////////////////////////////////////////////////////////////////////////////// -// memory allocator which fails randomly - -extern int32_t taosGetTimestampSec(); -static int32_t startTime = INT32_MAX; - -static bool taosRandomAllocFail(size_t size, const char* file, uint32_t line) { - if (taosGetTimestampSec() < startTime) { - return false; - } - - if (size < 100 * (size_t)1024) { - return false; - } - - if (rand() % 20 != 0) { - return false; - } - - if (fpAllocLog != NULL) { - fprintf(fpAllocLog, "%s:%d: memory allocation of %" PRIzu " bytes will fail.\n", file, line, size); - } - - return true; -} - -static void* taosRandmoMalloc(size_t size, const char* file, uint32_t line) { - return taosRandomAllocFail(size, file, line) ? NULL : malloc(size); -} - -static void* taosRandomCalloc(size_t num, size_t size, const char* file, uint32_t line) { - return taosRandomAllocFail(num * size, file, line) ? NULL : calloc(num, size); -} - -static void* taosRandomRealloc(void* ptr, size_t size, const char* file, uint32_t line) { - return taosRandomAllocFail(size, file, line) ? NULL : realloc(ptr, size); -} - -static char* taosRandomStrdup(const char* str, const char* file, uint32_t line) { - size_t len = strlen(str); - return taosRandomAllocFail(len + 1, file, line) ? NULL : tstrdup(str); -} - -static char* taosRandomStrndup(const char* str, size_t size, const char* file, uint32_t line) { - size_t len = strlen(str); - if (len > size) { - len = size; - } - return taosRandomAllocFail(len + 1, file, line) ? NULL : tstrndup(str, len); -} - -static ssize_t taosRandomGetline(char **lineptr, size_t *n, FILE *stream, const char* file, uint32_t line) { - return taosRandomAllocFail(*n, file, line) ? -1 : tgetline(lineptr, n, stream); -} - -//////////////////////////////////////////////////////////////////////////////// -// memory allocator with leak detection - -#define MEMBLK_MAGIC 0x55AA - -typedef struct SMemBlock { - const char* file; - uint16_t line; - uint16_t magic; - uint32_t size; - struct SMemBlock* prev; - struct SMemBlock* next; - // TODO: need pading in 32bit platform - char data[0]; -} SMemBlock; - -static SMemBlock *blocks = NULL; -static uintptr_t lock = 0; - -static void taosAddMemBlock(SMemBlock* blk) { - blk->prev = NULL; - while (atomic_val_compare_exchange_ptr(&lock, 0, 1) != 0); - blk->next = blocks; - if (blocks != NULL) { - blocks->prev = blk; - } - blocks = blk; - atomic_store_ptr(&lock, 0); -} - -static void taosRemoveMemBlock(SMemBlock* blk) { - while (atomic_val_compare_exchange_ptr(&lock, 0, 1) != 0); - - if (blocks == blk) { - blocks = blk->next; - } - if (blk->prev != NULL) { - blk->prev->next = blk->next; - } - if (blk->next != NULL) { - blk->next->prev = blk->prev; - } - - atomic_store_ptr(&lock, 0); - - blk->prev = NULL; - blk->next = NULL; -} - -static void taosFreeDetectLeak(void* ptr, const char* file, uint32_t line) { - if (ptr == NULL) { - return; - } - - SMemBlock* blk = (SMemBlock*)(((char*)ptr) - sizeof(SMemBlock)); - if (blk->magic != MEMBLK_MAGIC) { - if (fpAllocLog != NULL) { - fprintf(fpAllocLog, "%s:%d: memory is allocated by default allocator.\n", file, line); - } - free(ptr); - return; - } - - taosRemoveMemBlock(blk); - free(blk); -} - -static void* taosMallocDetectLeak(size_t size, const char* file, uint32_t line) { - if (size == 0) { - return NULL; - } - - SMemBlock *blk = (SMemBlock*)malloc(size + sizeof(SMemBlock)); - if (blk == NULL) { - return NULL; - } - - if (line > UINT16_MAX && fpAllocLog != NULL) { - fprintf(fpAllocLog, "%s:%d: line number too large.\n", file, line); - } - - if (size > UINT32_MAX && fpAllocLog != NULL) { - fprintf(fpAllocLog, "%s:%d: size too large: %" PRIzu ".\n", file, line, size); - } - - blk->file = file; - blk->line = (uint16_t)line; - blk->magic = MEMBLK_MAGIC; - blk->size = size; - taosAddMemBlock(blk); - - return blk->data; -} - -static void* taosCallocDetectLeak(size_t num, size_t size, const char* file, uint32_t line) { - size *= num; - void* p = taosMallocDetectLeak(size, file, line); - if (p != NULL) { - memset(p, 0, size); - } - return p; -} - -static void* taosReallocDetectLeak(void* ptr, size_t size, const char* file, uint32_t line) { - if (size == 0) { - taosFreeDetectLeak(ptr, file, line); - return NULL; - } - - if (ptr == NULL) { - return taosMallocDetectLeak(size, file, line); - } - - SMemBlock* blk = (SMemBlock *)((char*)ptr) - sizeof(SMemBlock); - if (blk->magic != MEMBLK_MAGIC) { - if (fpAllocLog != NULL) { - fprintf(fpAllocLog, "%s:%d: memory is allocated by default allocator.\n", file, line); - } - return realloc(ptr, size); - } - - taosRemoveMemBlock(blk); - - void* p = realloc(blk, size + sizeof(SMemBlock)); - if (p == NULL) { - taosAddMemBlock(blk); - return NULL; - } - - if (size > UINT32_MAX && fpAllocLog != NULL) { - fprintf(fpAllocLog, "%s:%d: size too large: %" PRIzu ".\n", file, line, size); - } - - blk = (SMemBlock*)p; - blk->size = size; - taosAddMemBlock(blk); - return blk->data; -} - -static char* taosStrdupDetectLeak(const char* str, const char* file, uint32_t line) { - size_t len = strlen(str); - char *p = taosMallocDetectLeak(len + 1, file, line); - if (p != NULL) { - memcpy(p, str, len); - p[len] = 0; - } - return p; -} - -static char* taosStrndupDetectLeak(const char* str, size_t size, const char* file, uint32_t line) { - size_t len = strlen(str); - if (len > size) { - len = size; - } - char *p = taosMallocDetectLeak(len + 1, file, line); - if (p != NULL) { - memcpy(p, str, len); - p[len] = 0; - } - return p; -} - -static ssize_t taosGetlineDetectLeak(char **lineptr, size_t *n, FILE *stream, const char* file, uint32_t line) { - char* buf = NULL; - size_t bufSize = 0; - ssize_t size = tgetline(&buf, &bufSize, stream); - if (size != -1) { - if (*n < size + 1) { - void* p = taosReallocDetectLeak(*lineptr, size + 1, file, line); - if (p == NULL) { - free(buf); - return -1; - } - *lineptr = (char*)p; - *n = size + 1; - } - memcpy(*lineptr, buf, size + 1); - } - - free(buf); - return size; -} - -static void taosDumpMemoryLeakImp() { - const char* hex = "0123456789ABCDEF"; - const char* fmt = ":%d: addr=%p, size=%d, content(first 16 bytes)="; - size_t numOfBlk = 0, totalSize = 0; - - if (fpAllocLog == NULL) { - return; - } - - fputs("memory blocks allocated but not freed before exit:\n", fpAllocLog); - - while (atomic_val_compare_exchange_ptr(&lock, 0, 1) != 0); - - for (SMemBlock* blk = blocks; blk != NULL; blk = blk->next) { - ++numOfBlk; - totalSize += blk->size; - - fputs(blk->file, fpAllocLog); - fprintf(fpAllocLog, fmt, blk->line, blk->data, blk->size); - - char sep = '\''; - size_t size = blk->size > 16 ? 16 : blk->size; - for (size_t i = 0; i < size; ++i) { - uint8_t c = (uint8_t)(blk->data[i]); - fputc(sep, fpAllocLog); - sep = ' '; - fputc(hex[c >> 4], fpAllocLog); - fputc(hex[c & 0x0f], fpAllocLog); - } - - fputs("'\n", fpAllocLog); - } - - atomic_store_ptr(&lock, 0); - - fprintf(fpAllocLog, "\nnumber of blocks: %" PRIzu ", total bytes: %" PRIzu "\n", numOfBlk, totalSize); - fflush(fpAllocLog); -} - -static void taosDumpMemoryLeakOnSig(int sig) { - fprintf(fpAllocLog, "signal %d received.\n", sig); - - // restore default signal handler - struct sigaction act = {0}; - act.sa_handler = SIG_DFL; - sigaction(sig, &act, NULL); - - taosDumpMemoryLeakImp(); -} - -//////////////////////////////////////////////////////////////////////////////// -// interface functions - -void* taosMallocMem(size_t size, const char* file, uint32_t line) { - switch (allocMode) { - case TAOS_ALLOC_MODE_DEFAULT: - return malloc(size); - - case TAOS_ALLOC_MODE_RANDOM_FAIL: - return taosRandmoMalloc(size, file, line); - - case TAOS_ALLOC_MODE_DETECT_LEAK: - return taosMallocDetectLeak(size, file, line); - } - return malloc(size); -} - -void* taosCallocMem(size_t num, size_t size, const char* file, uint32_t line) { - switch (allocMode) { - case TAOS_ALLOC_MODE_DEFAULT: - return calloc(num, size); - - case TAOS_ALLOC_MODE_RANDOM_FAIL: - return taosRandomCalloc(num, size, file, line); - - case TAOS_ALLOC_MODE_DETECT_LEAK: - return taosCallocDetectLeak(num, size, file, line); - } - return calloc(num, size); -} - -void* taosReallocMem(void* ptr, size_t size, const char* file, uint32_t line) { - switch (allocMode) { - case TAOS_ALLOC_MODE_DEFAULT: - return realloc(ptr, size); - - case TAOS_ALLOC_MODE_RANDOM_FAIL: - return taosRandomRealloc(ptr, size, file, line); - - case TAOS_ALLOC_MODE_DETECT_LEAK: - return taosReallocDetectLeak(ptr, size, file, line); - } - return realloc(ptr, size); -} - -void taosFreeMem(void* ptr, const char* file, uint32_t line) { - switch (allocMode) { - case TAOS_ALLOC_MODE_DEFAULT: - return free(ptr); - - case TAOS_ALLOC_MODE_RANDOM_FAIL: - return free(ptr); - - case TAOS_ALLOC_MODE_DETECT_LEAK: - return taosFreeDetectLeak(ptr, file, line); - } - return free(ptr); -} - -char* taosStrdupMem(const char* str, const char* file, uint32_t line) { - switch (allocMode) { - case TAOS_ALLOC_MODE_DEFAULT: - return tstrdup(str); - - case TAOS_ALLOC_MODE_RANDOM_FAIL: - return taosRandomStrdup(str, file, line); - - case TAOS_ALLOC_MODE_DETECT_LEAK: - return taosStrdupDetectLeak(str, file, line); - } - return tstrdup(str); -} - -char* taosStrndupMem(const char* str, size_t size, const char* file, uint32_t line) { - switch (allocMode) { - case TAOS_ALLOC_MODE_DEFAULT: - return tstrndup(str, size); - - case TAOS_ALLOC_MODE_RANDOM_FAIL: - return taosRandomStrndup(str, size, file, line); - - case TAOS_ALLOC_MODE_DETECT_LEAK: - return taosStrndupDetectLeak(str, size, file, line); - } - return tstrndup(str, size); -} - -ssize_t taosGetlineMem(char **lineptr, size_t *n, FILE *stream, const char* file, uint32_t line) { - switch (allocMode) { - case TAOS_ALLOC_MODE_DEFAULT: - return tgetline(lineptr, n, stream); - - case TAOS_ALLOC_MODE_RANDOM_FAIL: - return taosRandomGetline(lineptr, n, stream, file, line); - - case TAOS_ALLOC_MODE_DETECT_LEAK: - return taosGetlineDetectLeak(lineptr, n, stream, file, line); - } - return tgetline(lineptr, n, stream); -} - -static void taosCloseAllocLog() { - if (fpAllocLog != NULL) { - if (fpAllocLog != stdout) { - fclose(fpAllocLog); - } - fpAllocLog = NULL; - } -} - -void taosSetAllocMode(int mode, const char* path, bool autoDump) { - assert(mode >= TAOS_ALLOC_MODE_DEFAULT); - assert(mode <= TAOS_ALLOC_MODE_DETECT_LEAK); - - if (fpAllocLog != NULL || allocMode != TAOS_ALLOC_MODE_DEFAULT) { - printf("memory allocation mode can only be set once.\n"); - return; - } - - if (path == NULL || path[0] == 0) { - fpAllocLog = stdout; - } else if ((fpAllocLog = fopen(path, "w")) != NULL) { - atexit(taosCloseAllocLog); - } else { - printf("failed to open memory allocation log file '%s', errno=%d\n", path, errno); - return; - } - - allocMode = mode; - - if (mode == TAOS_ALLOC_MODE_RANDOM_FAIL) { - startTime = taosGetTimestampSec() + 10; - return; - } - - if (autoDump && mode == TAOS_ALLOC_MODE_DETECT_LEAK) { - atexit(taosDumpMemoryLeakImp); - - struct sigaction act = {0}; - act.sa_handler = taosDumpMemoryLeakOnSig; - sigaction(SIGFPE, &act, NULL); - sigaction(SIGSEGV, &act, NULL); - sigaction(SIGILL, &act, NULL); - } -} - -void taosDumpMemoryLeak() { - taosDumpMemoryLeakImp(); - taosCloseAllocLog(); -} - -#else // 'TAOS_MEM_CHECK' not defined - -void taosSetAllocMode(int mode, const char* path, bool autoDump) { - // do nothing -} - -void taosDumpMemoryLeak() { - // do nothing -} - -#endif // TAOS_MEM_CHECK - -void *taosTMalloc(size_t size) { - if (size <= 0) return NULL; - - void *ret = malloc(size + sizeof(size_t)); - if (ret == NULL) return NULL; - - *(size_t *)ret = size; - - return (void *)((char *)ret + sizeof(size_t)); -} - -void *taosTCalloc(size_t nmemb, size_t size) { - size_t tsize = nmemb * size; - void * ret = taosTMalloc(tsize); - if (ret == NULL) return NULL; - - taosTMemset(ret, 0); - return ret; -} - -size_t taosTSizeof(void *ptr) { return (ptr) ? (*(size_t *)((char *)ptr - sizeof(size_t))) : 0; } - -void taosTMemset(void *ptr, int c) { memset(ptr, c, taosTSizeof(ptr)); } - -void * taosTRealloc(void *ptr, size_t size) { - if (ptr == NULL) return taosTMalloc(size); - - if (size <= taosTSizeof(ptr)) return ptr; - - void * tptr = (void *)((char *)ptr - sizeof(size_t)); - size_t tsize = size + sizeof(size_t); - void* tptr1 = realloc(tptr, tsize); - if (tptr1 == NULL) return NULL; - tptr = tptr1; - - *(size_t *)tptr = size; - - return (void *)((char *)tptr + sizeof(size_t)); -} - -void* taosTZfree(void* ptr) { - if (ptr) { - free((void*)((char*)ptr - sizeof(size_t))); - } - return NULL; -} diff --git a/src/os/src/detail/osSemaphore.c b/src/os/src/detail/osSemaphore.c deleted file mode 100644 index 06907d52582e8cdb5cfdbe6da4724c4cf2bc8151..0000000000000000000000000000000000000000 --- a/src/os/src/detail/osSemaphore.c +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" - -#if !defined (_TD_DARWIN_64) - -int32_t tsem_wait(tsem_t* sem) { - int ret = 0; - do { - ret = sem_wait(sem); - } while (ret != 0 && errno == EINTR); - return ret; -} - -#endif - -#if !(defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) || defined (_TD_DARWIN_64)) - -bool taosCheckPthreadValid(pthread_t thread) { return thread != 0; } - -int64_t taosGetSelfPthreadId() { - static __thread int id = 0; - if (id != 0) return id; - id = syscall(SYS_gettid); - return id; -} - -int64_t taosGetPthreadId(pthread_t thread) { return (int64_t)thread; } -void taosResetPthread(pthread_t *thread) { *thread = 0; } -bool taosComparePthread(pthread_t first, pthread_t second) { return first == second; } -int32_t taosGetPId() { return getpid(); } - -int32_t taosGetCurrentAPPName(char *name, int32_t* len) { - const char* self = "/proc/self/exe"; - char path[PATH_MAX] = {0}; - - if (readlink(self, path, PATH_MAX) <= 0) { - return -1; - } - - path[PATH_MAX - 1] = 0; - char* end = strrchr(path, '/'); - if (end == NULL) { - return -1; - } - - ++end; - - strcpy(name, end); - - if (len != NULL) { - *len = strlen(name); - } - - return 0; -} - -#endif diff --git a/src/os/src/detail/osSignal.c b/src/os/src/detail/osSignal.c deleted file mode 100644 index 33cc39e112da1f9348dbefe85a7f9ec879c88cd1..0000000000000000000000000000000000000000 --- a/src/os/src/detail/osSignal.c +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "taosdef.h" -#include "tconfig.h" -#include "tglobal.h" -#include "tulog.h" - -#if !(defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)) - -typedef void (*FLinuxSignalHandler)(int32_t signum, siginfo_t *sigInfo, void *context); - -void taosSetSignal(int32_t signum, FSignalHandler sigfp) { - struct sigaction act; memset(&act, 0, sizeof(act)); -#if 1 - act.sa_flags = SA_SIGINFO; - act.sa_sigaction = (FLinuxSignalHandler)sigfp; -#else - act.sa_handler = sigfp; -#endif - sigaction(signum, &act, NULL); -} - -void taosIgnSignal(int32_t signum) { - signal(signum, SIG_IGN); -} - -void taosDflSignal(int32_t signum) { - signal(signum, SIG_DFL); -} - -#endif diff --git a/src/os/src/detail/osSocket.c b/src/os/src/detail/osSocket.c deleted file mode 100644 index 7ce9d6eb06621f0a691699020cff13b9c15a9a88..0000000000000000000000000000000000000000 --- a/src/os/src/detail/osSocket.c +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "tulog.h" - -#if !(defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)) - -int32_t taosSetNonblocking(SOCKET sock, int32_t on) { - int32_t flags = 0; - if ((flags = fcntl(sock, F_GETFL, 0)) < 0) { - uError("fcntl(F_GETFL) error: %d (%s)\n", errno, strerror(errno)); - return 1; - } - - if (on) - flags |= O_NONBLOCK; - else - flags &= ~O_NONBLOCK; - - if ((flags = fcntl(sock, F_SETFL, flags)) < 0) { - uError("fcntl(F_SETFL) error: %d (%s)\n", errno, strerror(errno)); - return 1; - } - - return 0; -} - -void taosIgnSIGPIPE() { - signal(SIGPIPE, SIG_IGN); -} - -void taosBlockSIGPIPE() { - sigset_t signal_mask; - sigemptyset(&signal_mask); - sigaddset(&signal_mask, SIGPIPE); - int32_t rc = pthread_sigmask(SIG_BLOCK, &signal_mask, NULL); - if (rc != 0) { - uError("failed to block SIGPIPE"); - } -} - -void taosSetMaskSIGPIPE() { - sigset_t signal_mask; - sigemptyset(&signal_mask); - sigaddset(&signal_mask, SIGPIPE); - int32_t rc = pthread_sigmask(SIG_SETMASK, &signal_mask, NULL); - if (rc != 0) { - uError("failed to setmask SIGPIPE"); - } -} - -#endif - -#if !(defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) || defined(_TD_DARWIN_32)) - -int32_t taosSetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *optval, int32_t optlen) { - return setsockopt(socketfd, level, optname, optval, (socklen_t)optlen); -} - -int32_t taosGetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *optval, int32_t* optlen) { - return getsockopt(socketfd, level, optname, optval, (socklen_t *)optlen); -} - -#endif - -#if !( (defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)) && defined(_MSC_VER) ) - -uint32_t taosInetAddr(char *ipAddr) { - return inet_addr(ipAddr); -} - -const char *taosInetNtoa(struct in_addr ipInt) { - return inet_ntoa(ipInt); -} - -#endif diff --git a/src/os/src/detail/osString.c b/src/os/src/detail/osString.c deleted file mode 100644 index fdc70b667e08face21554c99e2109bc49cf41cbf..0000000000000000000000000000000000000000 --- a/src/os/src/detail/osString.c +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "tglobal.h" - -int64_t taosStr2int64(char *str) { - char *endptr = NULL; - return strtoll(str, &endptr, 10); -} - -#if !(defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)) - -int32_t tasoUcs4Compare(void *f1_ucs4, void *f2_ucs4, int32_t bytes) { - return wcsncmp((wchar_t *)f1_ucs4, (wchar_t *)f2_ucs4, bytes / TSDB_NCHAR_SIZE); -} - -#endif - -#ifdef USE_LIBICONV -#include "iconv.h" - -int32_t taosUcs4ToMbs(void *ucs4, int32_t ucs4_max_len, char *mbs) { - iconv_t cd = iconv_open(tsCharset, DEFAULT_UNICODE_ENCODEC); - size_t ucs4_input_len = ucs4_max_len; - size_t outLen = ucs4_max_len; - if (iconv(cd, (char **)&ucs4, &ucs4_input_len, &mbs, &outLen) == -1) { - iconv_close(cd); - return -1; - } - - iconv_close(cd); - return (int32_t)(ucs4_max_len - outLen); -} - -bool taosMbsToUcs4(char *mbs, size_t mbsLength, char *ucs4, int32_t ucs4_max_len, int32_t *len) { - memset(ucs4, 0, ucs4_max_len); - iconv_t cd = iconv_open(DEFAULT_UNICODE_ENCODEC, tsCharset); - size_t ucs4_input_len = mbsLength; - size_t outLeft = ucs4_max_len; - if (iconv(cd, &mbs, &ucs4_input_len, &ucs4, &outLeft) == -1) { - iconv_close(cd); - return false; - } - - iconv_close(cd); - if (len != NULL) { - *len = (int32_t)(ucs4_max_len - outLeft); - if (*len < 0) { - return false; - } - } - - return true; -} - -bool taosValidateEncodec(const char *encodec) { - iconv_t cd = iconv_open(encodec, DEFAULT_UNICODE_ENCODEC); - if (cd == (iconv_t)(-1)) { - return false; - } - - iconv_close(cd); - return true; -} - -#else - -int32_t taosUcs4ToMbs(void *ucs4, int32_t ucs4_max_len, char *mbs) { - mbstate_t state = {0}; - int32_t len = (int32_t)wcsnrtombs(NULL, (const wchar_t **)&ucs4, ucs4_max_len / 4, 0, &state); - if (len < 0) { - return -1; - } - - memset(&state, 0, sizeof(state)); - len = wcsnrtombs(mbs, (const wchar_t **)&ucs4, ucs4_max_len / 4, (size_t)len, &state); - if (len < 0) { - return -1; - } - - return len; -} - -bool taosMbsToUcs4(char *mbs, size_t mbsLength, char *ucs4, int32_t ucs4_max_len, int32_t *len) { - memset(ucs4, 0, ucs4_max_len); - mbstate_t state = {0}; - int32_t retlen = mbsnrtowcs((wchar_t *)ucs4, (const char **)&mbs, mbsLength, ucs4_max_len / 4, &state); - *len = retlen; - - return retlen >= 0; -} - -bool taosValidateEncodec(const char *encodec) { - return true; -} - -#endif - -typedef struct CharsetPair { - char *oldCharset; - char *newCharset; -} CharsetPair; - -char *taosCharsetReplace(char *charsetstr) { - CharsetPair charsetRep[] = { - { "utf8", "UTF-8" }, { "936", "CP936" }, - }; - - for (int32_t i = 0; i < tListLen(charsetRep); ++i) { - if (strcasecmp(charsetRep[i].oldCharset, charsetstr) == 0) { - return strdup(charsetRep[i].newCharset); - } - } - - return strdup(charsetstr); -} diff --git a/src/os/src/detail/osSysinfo.c b/src/os/src/detail/osSysinfo.c deleted file mode 100644 index 8094358853947c22064c30f877baea1e5938b3d3..0000000000000000000000000000000000000000 --- a/src/os/src/detail/osSysinfo.c +++ /dev/null @@ -1,670 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "tconfig.h" -#include "tglobal.h" -#include "tulog.h" -#include "taoserror.h" - -#if !(defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) || defined (_TD_DARWIN_64)) - -#define PROCESS_ITEM 12 - -typedef struct { - uint64_t user; - uint64_t nice; - uint64_t system; - uint64_t idle; -} SysCpuInfo; - -typedef struct { - uint64_t utime; // user time - uint64_t stime; // kernel time - uint64_t cutime; // all user time - uint64_t cstime; // all dead time -} ProcCpuInfo; - -static pid_t tsProcId; -static char tsSysNetFile[] = "/proc/net/dev"; -static char tsSysCpuFile[] = "/proc/stat"; -static char tsProcCpuFile[25] = {0}; -static char tsProcMemFile[25] = {0}; -static char tsProcIOFile[25] = {0}; -static float tsPageSizeKB = 0; - -static void taosGetProcInfos() { - tsPageSize = sysconf(_SC_PAGESIZE); - tsOpenMax = sysconf(_SC_OPEN_MAX); - tsStreamMax = sysconf(_SC_STREAM_MAX); - - tsProcId = (pid_t)syscall(SYS_gettid); - tsPageSizeKB = (float)(sysconf(_SC_PAGESIZE)) / 1024; - - snprintf(tsProcMemFile, 25, "/proc/%d/status", tsProcId); - snprintf(tsProcCpuFile, 25, "/proc/%d/stat", tsProcId); - snprintf(tsProcIOFile, 25, "/proc/%d/io", tsProcId); -} - -static int32_t taosGetTotalMemory() { return (int32_t)((float)sysconf(_SC_PHYS_PAGES) * tsPageSizeKB / 1024); } - -bool taosGetSysMemory(float *memoryUsedMB) { - float memoryAvailMB = (float)sysconf(_SC_AVPHYS_PAGES) * tsPageSizeKB / 1024; - *memoryUsedMB = (float)tsTotalMemoryMB - memoryAvailMB; - return true; -} - -bool taosGetProcMemory(float *memoryUsedMB) { - FILE *fp = fopen(tsProcMemFile, "r"); - if (fp == NULL) { - uError("open file:%s failed", tsProcMemFile); - return false; - } - - ssize_t _bytes = 0; - size_t len; - char * line = NULL; - while (!feof(fp)) { - tfree(line); - len = 0; - _bytes = getline(&line, &len, fp); - if ((_bytes < 0) || (line == NULL)) { - break; - } - if (strstr(line, "VmRSS:") != NULL) { - break; - } - } - - if (line == NULL) { - uError("read file:%s failed", tsProcMemFile); - fclose(fp); - return false; - } - - int64_t memKB = 0; - char tmp[10]; - sscanf(line, "%s %" PRId64, tmp, &memKB); - *memoryUsedMB = (float)((double)memKB / 1024); - - tfree(line); - fclose(fp); - return true; -} - -static bool taosGetSysCpuInfo(SysCpuInfo *cpuInfo) { - FILE *fp = fopen(tsSysCpuFile, "r"); - if (fp == NULL) { - uError("open file:%s failed", tsSysCpuFile); - return false; - } - - size_t len; - char * line = NULL; - ssize_t _bytes = getline(&line, &len, fp); - if ((_bytes < 0) || (line == NULL)) { - uError("read file:%s failed", tsSysCpuFile); - fclose(fp); - return false; - } - - char cpu[10] = {0}; - sscanf(line, "%s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, cpu, &cpuInfo->user, &cpuInfo->nice, &cpuInfo->system, - &cpuInfo->idle); - - tfree(line); - fclose(fp); - return true; -} - -static bool taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) { - FILE *fp = fopen(tsProcCpuFile, "r"); - if (fp == NULL) { - uError("open file:%s failed", tsProcCpuFile); - return false; - } - - size_t len = 0; - char * line = NULL; - ssize_t _bytes = getline(&line, &len, fp); - if ((_bytes < 0) || (line == NULL)) { - uError("read file:%s failed", tsProcCpuFile); - fclose(fp); - return false; - } - - for (int i = 0, blank = 0; line[i] != 0; ++i) { - if (line[i] == ' ') blank++; - if (blank == PROCESS_ITEM) { - sscanf(line + i + 1, "%" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, &cpuInfo->utime, &cpuInfo->stime, - &cpuInfo->cutime, &cpuInfo->cstime); - break; - } - } - - tfree(line); - fclose(fp); - return true; -} - -static void taosGetSystemTimezone() { - SGlobalCfg *cfg_timezone = taosGetConfigOption("timezone"); - if (cfg_timezone == NULL) return; - if (cfg_timezone->cfgStatus >= TAOS_CFG_CSTATUS_DEFAULT) { - return; - } - - /* - * NOTE: do not remove it. - * Enforce set the correct daylight saving time(DST) flag according - * to current time - */ - time_t tx1 = time(NULL); - struct tm tm1; - localtime_r(&tx1, &tm1); - - /* load time zone string from /etc/timezone */ - FILE *f = fopen("/etc/timezone", "r"); - char buf[68] = {0}; - if (f != NULL) { - int len = fread(buf, 64, 1, f); - if (len < 64 && ferror(f)) { - fclose(f); - uError("read /etc/timezone error, reason:%s", strerror(errno)); - return; - } - - fclose(f); - - buf[sizeof(buf) - 1] = 0; - char *lineEnd = strstr(buf, "\n"); - if (lineEnd != NULL) { - *lineEnd = 0; - } - - // for CentOS system, /etc/timezone does not exist. Ignore the TZ environment variables - if (strlen(buf) > 0) { - setenv("TZ", buf, 1); - } - } - // get and set default timezone - tzset(); - - /* - * get CURRENT time zone. - * system current time zone is affected by daylight saving time(DST) - * - * e.g., the local time zone of London in DST is GMT+01:00, - * otherwise is GMT+00:00 - */ - int32_t tz = (-timezone * MILLISECOND_PER_SECOND) / MILLISECOND_PER_HOUR; - tz += daylight; - - /* - * format example: - * - * Asia/Shanghai (CST, +0800) - * Europe/London (BST, +0100) - */ - snprintf(tsTimezone, TSDB_TIMEZONE_LEN, "%s (%s, %s%02d00)", buf, tzname[daylight], tz >= 0 ? "+" : "-", abs(tz)); - - // cfg_timezone->cfgStatus = TAOS_CFG_CSTATUS_DEFAULT; - uWarn("timezone not configured, set to system default:%s", tsTimezone); -} - -/* - * POSIX format locale string: - * (Language Strings)_(Country/Region Strings).(code_page) - * - * example: en_US.UTF-8, zh_CN.GB18030, zh_CN.UTF-8, - * - * if user does not specify the locale in taos.cfg the program use default LC_CTYPE as system locale. - * - * In case of some CentOS systems, their default locale is "en_US.utf8", which is not valid code_page - * for libiconv that is employed to convert string in this system. This program will automatically use - * UTF-8 instead as the charset. - * - * In case of windows client, the locale string is not valid POSIX format, user needs to set the - * correct code_page for libiconv. Usually, the code_page of windows system with simple chinese is - * CP936, CP437 for English charset. - * - */ -static void taosGetSystemLocale() { // get and set default locale - char sep = '.'; - char *locale = NULL; - - SGlobalCfg *cfg_locale = taosGetConfigOption("locale"); - if (cfg_locale && cfg_locale->cfgStatus < TAOS_CFG_CSTATUS_DEFAULT) { - locale = setlocale(LC_CTYPE, ""); - if (locale == NULL) { - uError("can't get locale from system, set it to en_US.UTF-8 since error:%d:%s", errno, strerror(errno)); - strcpy(tsLocale, "en_US.UTF-8"); - } else { - tstrncpy(tsLocale, locale, TSDB_LOCALE_LEN); - uWarn("locale not configured, set to system default:%s", tsLocale); - } - } - - /* if user does not specify the charset, extract it from locale */ - SGlobalCfg *cfg_charset = taosGetConfigOption("charset"); - if (cfg_charset && cfg_charset->cfgStatus < TAOS_CFG_CSTATUS_DEFAULT) { - char *str = strrchr(tsLocale, sep); - if (str != NULL) { - str++; - - char *revisedCharset = taosCharsetReplace(str); - tstrncpy(tsCharset, revisedCharset, TSDB_LOCALE_LEN); - - free(revisedCharset); - uWarn("charset not configured, set to system default:%s", tsCharset); - } else { - strcpy(tsCharset, "UTF-8"); - uWarn("can't get locale and charset from system, set it to UTF-8"); - } - } -} - -int32_t taosGetCpuCores() { return (int32_t)sysconf(_SC_NPROCESSORS_ONLN); } - -bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { - static uint64_t lastSysUsed = 0; - static uint64_t lastSysTotal = 0; - static uint64_t lastProcTotal = 0; - - SysCpuInfo sysCpu; - ProcCpuInfo procCpu; - if (!taosGetSysCpuInfo(&sysCpu)) { - return false; - } - if (!taosGetProcCpuInfo(&procCpu)) { - return false; - } - - uint64_t curSysUsed = sysCpu.user + sysCpu.nice + sysCpu.system; - uint64_t curSysTotal = curSysUsed + sysCpu.idle; - uint64_t curProcTotal = procCpu.utime + procCpu.stime + procCpu.cutime + procCpu.cstime; - - if (lastSysUsed == 0 || lastSysTotal == 0 || lastProcTotal == 0) { - lastSysUsed = curSysUsed > 1 ? curSysUsed : 1; - lastSysTotal = curSysTotal > 1 ? curSysTotal : 1; - lastProcTotal = curProcTotal > 1 ? curProcTotal : 1; - return false; - } - - if (curSysTotal == lastSysTotal) { - return false; - } - - *sysCpuUsage = (float)((double)(curSysUsed - lastSysUsed) / (double)(curSysTotal - lastSysTotal) * 100); - *procCpuUsage = (float)((double)(curProcTotal - lastProcTotal) / (double)(curSysTotal - lastSysTotal) * 100); - - lastSysUsed = curSysUsed; - lastSysTotal = curSysTotal; - lastProcTotal = curProcTotal; - - return true; -} - -int32_t taosGetDiskSize(char *dataDir, SysDiskSize *diskSize) { - struct statvfs info; - if (statvfs(dataDir, &info)) { - uError("failed to get disk size, dataDir:%s errno:%s", tsDataDir, strerror(errno)); - terrno = TAOS_SYSTEM_ERROR(errno); - return -1; - } else { - diskSize->tsize = info.f_blocks * info.f_frsize; - diskSize->avail = info.f_bavail * info.f_frsize; - diskSize->used = (info.f_blocks - info.f_bfree) * info.f_frsize; - return 0; - } -} - -bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { - *bytes = 0; - FILE *fp = fopen(tsSysNetFile, "r"); - if (fp == NULL) { - uError("open file:%s failed", tsSysNetFile); - return false; - } - - ssize_t _bytes = 0; - size_t len = 2048; - char * line = calloc(1, len); - - while (!feof(fp)) { - memset(line, 0, len); - - int64_t o_rbytes = 0; - int64_t rpackts = 0; - int64_t o_tbytes = 0; - int64_t tpackets = 0; - int64_t nouse1 = 0; - int64_t nouse2 = 0; - int64_t nouse3 = 0; - int64_t nouse4 = 0; - int64_t nouse5 = 0; - int64_t nouse6 = 0; - char nouse0[200] = {0}; - - _bytes = getline(&line, &len, fp); - if (_bytes < 0) - { - break; - } - - line[len - 1] = 0; - - if (strstr(line, "lo:") != NULL) { - continue; - } - - sscanf(line, - "%s %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 - " %" PRId64, - nouse0, &o_rbytes, &rpackts, &nouse1, &nouse2, &nouse3, &nouse4, &nouse5, &nouse6, &o_tbytes, &tpackets); - if (rbytes) *rbytes = o_rbytes; - if (tbytes) *tbytes = o_tbytes; - *bytes += (o_rbytes + o_tbytes); - } - - tfree(line); - fclose(fp); - - return true; -} - -bool taosGetBandSpeed(float *bandSpeedKb) { - static int64_t lastBytes = 0; - static time_t lastTime = 0; - int64_t curBytes = 0; - time_t curTime = time(NULL); - - if (!taosGetCardInfo(&curBytes, NULL, NULL)) { - return false; - } - - if (lastTime == 0 || lastBytes == 0) { - lastTime = curTime; - lastBytes = curBytes; - *bandSpeedKb = 0; - return true; - } - - if (lastTime >= curTime || lastBytes > curBytes) { - lastTime = curTime; - lastBytes = curBytes; - *bandSpeedKb = 0; - return true; - } - - double totalBytes = (double)(curBytes - lastBytes) / 1024 * 8; // Kb - *bandSpeedKb = (float)(totalBytes / (double)(curTime - lastTime)); - - // uInfo("bandwidth lastBytes:%ld, lastTime:%ld, curBytes:%ld, curTime:%ld, - // speed:%f", lastBytes, lastTime, curBytes, curTime, *bandSpeed); - - lastTime = curTime; - lastBytes = curBytes; - - return true; -} - -bool taosReadProcIO(int64_t *rchars, int64_t *wchars) { - FILE *fp = fopen(tsProcIOFile, "r"); - if (fp == NULL) { - uError("open file:%s failed", tsProcIOFile); - return false; - } - - ssize_t _bytes = 0; - size_t len; - char * line = NULL; - char tmp[10]; - int readIndex = 0; - - while (!feof(fp)) { - tfree(line); - len = 0; - _bytes = getline(&line, &len, fp); - if ((_bytes < 0) || (line == NULL)) { - break; - } - if (strstr(line, "rchar:") != NULL) { - sscanf(line, "%s %" PRId64, tmp, rchars); - readIndex++; - } else if (strstr(line, "wchar:") != NULL) { - sscanf(line, "%s %" PRId64, tmp, wchars); - readIndex++; - } else { - } - - if (readIndex >= 2) break; - } - - tfree(line); - fclose(fp); - - if (readIndex < 2) { - uError("read file:%s failed", tsProcIOFile); - return false; - } - - return true; -} - -bool taosGetProcIO(float *readKB, float *writeKB) { - static int64_t lastReadbyte = -1; - static int64_t lastWritebyte = -1; - - int64_t curReadbyte = 0; - int64_t curWritebyte = 0; - - if (!taosReadProcIO(&curReadbyte, &curWritebyte)) { - return false; - } - - if (lastReadbyte == -1 || lastWritebyte == -1) { - lastReadbyte = curReadbyte; - lastWritebyte = curWritebyte; - return false; - } - - *readKB = (float)((double)(curReadbyte - lastReadbyte) / 1024); - *writeKB = (float)((double)(curWritebyte - lastWritebyte) / 1024); - if (*readKB < 0) *readKB = 0; - if (*writeKB < 0) *writeKB = 0; - - lastReadbyte = curReadbyte; - lastWritebyte = curWritebyte; - - return true; -} - -void taosGetSystemInfo() { - taosGetProcInfos(); - - tsNumOfCores = taosGetCpuCores(); - tsTotalMemoryMB = taosGetTotalMemory(); - - float tmp1, tmp2; - taosGetSysMemory(&tmp1); - taosGetProcMemory(&tmp2); - // taosGetDisk(); - taosGetBandSpeed(&tmp1); - taosGetCpuUsage(&tmp1, &tmp2); - taosGetProcIO(&tmp1, &tmp2); - - taosGetSystemTimezone(); - taosGetSystemLocale(); -} - -void taosPrintOsInfo() { - uInfo(" os pageSize: %" PRId64 "(KB)", tsPageSize); - uInfo(" os openMax: %" PRId64, tsOpenMax); - uInfo(" os streamMax: %" PRId64, tsStreamMax); - uInfo(" os numOfCores: %d", tsNumOfCores); - uInfo(" os totalMemory: %d(MB)", tsTotalMemoryMB); - - struct utsname buf; - if (uname(&buf)) { - uInfo(" can't fetch os info"); - return; - } - uInfo(" os sysname: %s", buf.sysname); - uInfo(" os nodename: %s", buf.nodename); - uInfo(" os release: %s", buf.release); - uInfo(" os version: %s", buf.version); - uInfo(" os machine: %s", buf.machine); -} - -void taosPrintDiskInfo() { - uInfo("=================================="); - uInfo(" os totalDisk: %f(GB)", tsTotalDataDirGB); - uInfo(" os usedDisk: %f(GB)", tsUsedDataDirGB); - uInfo(" os availDisk: %f(GB)", tsAvailDataDirGB); - uInfo("=================================="); -} - -void taosKillSystem() { - // SIGINT - uInfo("taosd will shut down soon"); - kill(tsProcId, 2); -} - -int taosSystem(const char *cmd) { - FILE *fp; - int res; - char buf[1024]; - if (cmd == NULL) { - uError("taosSystem cmd is NULL!"); - return -1; - } - - if ((fp = popen(cmd, "r")) == NULL) { - uError("popen cmd:%s error: %s", cmd, strerror(errno)); - return -1; - } else { - while (fgets(buf, sizeof(buf), fp)) { - uDebug("popen result:%s", buf); - } - - if ((res = pclose(fp)) == -1) { - uError("close popen file pointer fp error!"); - } else { - uDebug("popen res is :%d", res); - } - - return res; - } -} - -void taosSetCoreDump() { - if (0 == tsEnableCoreFile) { - return; - } - - // 1. set ulimit -c unlimited - struct rlimit rlim; - struct rlimit rlim_new; - if (getrlimit(RLIMIT_CORE, &rlim) == 0) { -#ifndef _ALPINE - uInfo("the old unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max); -#else - uInfo("the old unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max); -#endif - rlim_new.rlim_cur = RLIM_INFINITY; - rlim_new.rlim_max = RLIM_INFINITY; - if (setrlimit(RLIMIT_CORE, &rlim_new) != 0) { - uInfo("set unlimited fail, error: %s", strerror(errno)); - rlim_new.rlim_cur = rlim.rlim_max; - rlim_new.rlim_max = rlim.rlim_max; - (void)setrlimit(RLIMIT_CORE, &rlim_new); - } - } - - if (getrlimit(RLIMIT_CORE, &rlim) == 0) { -#ifndef _ALPINE - uInfo("the new unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max); -#else - uInfo("the new unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max); -#endif - } - -#ifndef _TD_ARM_ - // 2. set the path for saving core file - struct __sysctl_args args; - - int old_usespid = 0; - size_t old_len = 0; - int new_usespid = 1; - size_t new_len = sizeof(new_usespid); - - int name[] = {CTL_KERN, KERN_CORE_USES_PID}; - - memset(&args, 0, sizeof(struct __sysctl_args)); - args.name = name; - args.nlen = sizeof(name) / sizeof(name[0]); - args.oldval = &old_usespid; - args.oldlenp = &old_len; - args.newval = &new_usespid; - args.newlen = new_len; - - old_len = sizeof(old_usespid); - - if (syscall(SYS__sysctl, &args) == -1) { - uInfo("_sysctl(kern_core_uses_pid) set fail: %s", strerror(errno)); - } - - uInfo("The old core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid); - - old_usespid = 0; - old_len = 0; - memset(&args, 0, sizeof(struct __sysctl_args)); - args.name = name; - args.nlen = sizeof(name) / sizeof(name[0]); - args.oldval = &old_usespid; - args.oldlenp = &old_len; - - old_len = sizeof(old_usespid); - - if (syscall(SYS__sysctl, &args) == -1) { - uInfo("_sysctl(kern_core_uses_pid) get fail: %s", strerror(errno)); - } - - uInfo("The new core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid); -#endif -} - -bool taosGetSystemUid(char *uid) { - int fd; - int len = 0; - - fd = open("/proc/sys/kernel/random/uuid", 0); - if (fd < 0) { - return false; - } else { - len = read(fd, uid, TSDB_CLUSTER_ID_LEN); - close(fd); - } - - if (len >= 36) { - uid[36] = 0; - return true; - } - return false; -} - -#endif diff --git a/src/os/src/detail/osTimer.c b/src/os/src/detail/osTimer.c deleted file mode 100644 index 618df8a8bad451984fafd022a33a799986a48422..0000000000000000000000000000000000000000 --- a/src/os/src/detail/osTimer.c +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "ttimer.h" -#include "tulog.h" - -#if !(defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) || defined(_TD_DARWIN_64)) - -static void taosDeleteTimer(void *tharg) { - timer_t *pTimer = tharg; - timer_delete(*pTimer); -} - -static pthread_t timerThread; -static timer_t timerId; -static volatile bool stopTimer = false; -static void *taosProcessAlarmSignal(void *tharg) { - // Block the signal - sigset_t sigset; - sigemptyset(&sigset); - sigaddset(&sigset, SIGALRM); - sigprocmask(SIG_BLOCK, &sigset, NULL); - void (*callback)(int) = tharg; - - struct sigevent sevent = {{0}}; - - setThreadName("tmr"); - - #ifdef _ALPINE - sevent.sigev_notify = SIGEV_THREAD; - sevent.sigev_value.sival_int = syscall(__NR_gettid); - #else - sevent.sigev_notify = SIGEV_THREAD_ID; - sevent._sigev_un._tid = syscall(__NR_gettid); - #endif - - sevent.sigev_signo = SIGALRM; - - if (timer_create(CLOCK_REALTIME, &sevent, &timerId) == -1) { - uError("Failed to create timer"); - } - - pthread_cleanup_push(taosDeleteTimer, &timerId); - - struct itimerspec ts; - ts.it_value.tv_sec = 0; - ts.it_value.tv_nsec = 1000000 * MSECONDS_PER_TICK; - ts.it_interval.tv_sec = 0; - ts.it_interval.tv_nsec = 1000000 * MSECONDS_PER_TICK; - - if (timer_settime(timerId, 0, &ts, NULL)) { - uError("Failed to init timer"); - return NULL; - } - - int signo; - while (!stopTimer) { - if (sigwait(&sigset, &signo)) { - uError("Failed to wait signal: number %d", signo); - continue; - } - /* printf("Signal handling: number %d ......\n", signo); */ - - callback(0); - } - - pthread_cleanup_pop(1); - - return NULL; -} - -int taosInitTimer(void (*callback)(int), int ms) { - pthread_attr_t tattr; - pthread_attr_init(&tattr); - int code = pthread_create(&timerThread, &tattr, taosProcessAlarmSignal, callback); - pthread_attr_destroy(&tattr); - if (code != 0) { - uError("failed to create timer thread"); - return -1; - } else { - uDebug("timer thread:0x%08" PRIx64 " is created", taosGetPthreadId(timerThread)); - } - - return 0; -} - -void taosUninitTimer() { - stopTimer = true; - - uDebug("join timer thread:0x%08" PRIx64, taosGetPthreadId(timerThread)); - pthread_join(timerThread, NULL); -} - -#endif diff --git a/src/os/src/linux/CMakeLists.txt b/src/os/src/linux/CMakeLists.txt deleted file mode 100644 index f60c10b65a004735e4b76f5d170a65afc6508c36..0000000000000000000000000000000000000000 --- a/src/os/src/linux/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.8...3.20) -PROJECT(TDengine) - -AUX_SOURCE_DIRECTORY(. SRC) -ADD_LIBRARY(oslinux ${SRC}) - -TARGET_LINK_LIBRARIES(oslinux m rt z dl) \ No newline at end of file diff --git a/src/os/src/linux/linuxEnv.c b/src/os/src/linux/linuxEnv.c deleted file mode 100644 index 650a45aae42c8d2dfba63d8f4e7e6ec35b385ae8..0000000000000000000000000000000000000000 --- a/src/os/src/linux/linuxEnv.c +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "tglobal.h" - -void osInit() { -#ifdef _TD_POWER_ - if (configDir[0] == 0) { - strcpy(configDir, "/etc/power"); - } - strcpy(tsDataDir, "/var/lib/power"); - strcpy(tsLogDir, "/var/log/power"); - strcpy(tsScriptDir, "/etc/power"); -#elif (_TD_TQ_ == true) - if (configDir[0] == 0) { - strcpy(configDir, "/etc/tq"); - } - strcpy(tsDataDir, "/var/lib/tq"); - strcpy(tsLogDir, "/var/log/tq"); - strcpy(tsScriptDir, "/etc/tq"); -#else - if (configDir[0] == 0) { - strcpy(configDir, "/etc/taos"); - } - strcpy(tsDataDir, "/var/lib/taos"); - strcpy(tsLogDir, "/var/log/taos"); - strcpy(tsScriptDir, "/etc/taos"); -#endif - - strcpy(tsVnodeDir, ""); - strcpy(tsDnodeDir, ""); - strcpy(tsMnodeDir, ""); - strcpy(tsOsName, "Linux"); -} - -char* taosGetCmdlineByPID(int pid) { - static char cmdline[1024]; - sprintf(cmdline, "/proc/%d/cmdline", pid); - - int fd = open(cmdline, O_RDONLY); - if (fd >= 0) { - int n = read(fd, cmdline, sizeof(cmdline) - 1); - if (n < 0) n = 0; - - if (n > 0 && cmdline[n - 1] == '\n') --n; - - cmdline[n] = 0; - - close(fd); - } else { - cmdline[0] = 0; - } - - return cmdline; -} diff --git a/src/os/src/linux/osSystem.c b/src/os/src/linux/osSystem.c deleted file mode 100644 index a82149dccb1f71e6fbdc2b62d066f04ff52c251d..0000000000000000000000000000000000000000 --- a/src/os/src/linux/osSystem.c +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "tconfig.h" -#include "tglobal.h" -#include "tulog.h" - -void* taosLoadDll(const char *filename) { - void *handle = dlopen (filename, RTLD_LAZY); - if (!handle) { - uError("load dll:%s failed, error:%s", filename, dlerror()); - return NULL; - } - - uDebug("dll %s loaded", filename); - - return handle; -} - -void* taosLoadSym(void* handle, char* name) { - void* sym = dlsym(handle, name); - char* error = NULL; - - if ((error = dlerror()) != NULL) { - uWarn("load sym:%s failed, error:%s", name, dlerror()); - return NULL; - } - - uDebug("sym %s loaded", name) - - return sym; -} - -void taosCloseDll(void *handle) { - if (handle) { - dlclose(handle); - } -} - -int taosSetConsoleEcho(bool on) -{ -#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL) - int err; - struct termios term; - - if (tcgetattr(STDIN_FILENO, &term) == -1) { - perror("Cannot get the attribution of the terminal"); - return -1; - } - - if (on) - term.c_lflag |= ECHOFLAGS; - else - term.c_lflag &= ~ECHOFLAGS; - - err = tcsetattr(STDIN_FILENO, TCSAFLUSH, &term); - if (err == -1 || err == EINTR) { - perror("Cannot set the attribution of the terminal"); - return -1; - } - - return 0; -} - diff --git a/src/os/src/windows/CMakeLists.txt b/src/os/src/windows/CMakeLists.txt deleted file mode 100644 index 83012d6e3e5a2e11655f4a1c0742cdd25cccddf2..0000000000000000000000000000000000000000 --- a/src/os/src/windows/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.8...3.20) -PROJECT(TDengine) - -AUX_SOURCE_DIRECTORY(. SRC) -ADD_LIBRARY(oswindows ${SRC}) - -TARGET_LINK_LIBRARIES(oswindows winmm IPHLPAPI ws2_32 MsvcLibXw) \ No newline at end of file diff --git a/src/os/src/windows/wEnv.c b/src/os/src/windows/wEnv.c deleted file mode 100644 index b35cb8f040aec5ff4b4fb12665d0842e72958ba1..0000000000000000000000000000000000000000 --- a/src/os/src/windows/wEnv.c +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "tglobal.h" -#include "tulog.h" - -extern void taosWinSocketInit(); - -void osInit() { - taosSetCoreDump(); -#ifdef _TD_POWER_ - if (configDir[0] == 0) { - strcpy(configDir, "C:/PowerDB/cfg"); - } - - strcpy(tsVnodeDir, "C:/PowerDB/data"); - strcpy(tsDataDir, "C:/PowerDB/data"); - strcpy(tsLogDir, "C:/PowerDB/log"); - strcpy(tsScriptDir, "C:/PowerDB/script"); -#elif (_TD_TQ_ == true) - if (configDir[0] == 0) { - strcpy(configDir, "C:/TQ/cfg"); - } - strcpy(tsVnodeDir, "C:/TQ/data"); - strcpy(tsDataDir, "C:/TQ/data"); - strcpy(tsLogDir, "C:/TQ/log"); - strcpy(tsScriptDir, "C:/TQ/script"); -#else - if (configDir[0] == 0) { - strcpy(configDir, "C:/TDengine/cfg"); - } - - strcpy(tsVnodeDir, "C:/TDengine/data"); - strcpy(tsDataDir, "C:/TDengine/data"); - strcpy(tsLogDir, "C:/TDengine/log"); - strcpy(tsScriptDir, "C:/TDengine/script"); -#endif - - strcpy(tsDnodeDir, ""); - strcpy(tsMnodeDir, ""); - strcpy(tsOsName, "Windows"); - - const char *tmpDir = getenv("tmp"); - if (tmpDir == NULL) { - tmpDir = getenv("temp"); - } - - if (tmpDir != NULL) { - strcpy(tsTempDir, tmpDir); - } else { - strcpy(tsTempDir, "C:\\Windows\\Temp"); - } - - taosWinSocketInit(); -} diff --git a/src/os/src/windows/wGetline.c b/src/os/src/windows/wGetline.c deleted file mode 100644 index aa458548843b977ac7fed4070047ac8d693b2d31..0000000000000000000000000000000000000000 --- a/src/os/src/windows/wGetline.c +++ /dev/null @@ -1,125 +0,0 @@ -/* getline.c -- Replacement for GNU C library function getline - -Copyright (C) 1993 Free Software Foundation, Inc. - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License as -published by the Free Software Foundation; either version 2 of the -License, or (at your option) any later version. - -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. See the GNU -General Public License for more details. */ - -/* Written by Jan Brittenson, bson@gnu.ai.mit.edu. */ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include -#include -#include -#include -#include - -#if STDC_HEADERS -#include -#else -char *malloc(), *realloc(); -#endif - -/* Always add at least this many bytes when extending the buffer. */ -#define MIN_CHUNK 64 - -/* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR -+ OFFSET (and null-terminate it). *LINEPTR is a pointer returned from -malloc (or NULL), pointing to *N characters of space. It is realloc'd -as necessary. Return the number of characters read (not including the -null terminator), or -1 on error or EOF. On a -1 return, the caller -should check feof(), if not then errno has been set to indicate -the error. */ - -int32_t getstr(char **lineptr, size_t *n, FILE *stream, char terminator, int32_t offset) { - int32_t nchars_avail; /* Allocated but unused chars in *LINEPTR. */ - char * read_pos; /* Where we're reading into *LINEPTR. */ - int32_t ret; - - if (!lineptr || !n || !stream) { - errno = EINVAL; - return -1; - } - - if (!*lineptr) { - *n = MIN_CHUNK; - *lineptr = malloc(*n); - if (!*lineptr) { - errno = ENOMEM; - return -1; - } - } - - nchars_avail = (int32_t)(*n - offset); - read_pos = *lineptr + offset; - - for (;;) { - int32_t save_errno; - register int32_t c = getc(stream); - - save_errno = errno; - - /* We always want at least one char left in the buffer, since we - always (unless we get an error while reading the first char) - NUL-terminate the line buffer. */ - - assert((*lineptr + *n) == (read_pos + nchars_avail)); - if (nchars_avail < 2) { - if (*n > MIN_CHUNK) - *n *= 2; - else - *n += MIN_CHUNK; - - nchars_avail = (int32_t)(*n + *lineptr - read_pos); - char* lineptr1 = realloc(*lineptr, *n); - if (!lineptr1) { - errno = ENOMEM; - return -1; - } - *lineptr = lineptr1; - - read_pos = *n - nchars_avail + *lineptr; - assert((*lineptr + *n) == (read_pos + nchars_avail)); - } - - if (ferror(stream)) { - /* Might like to return partial line, but there is no - place for us to store errno. And we don't want to just - lose errno. */ - errno = save_errno; - return -1; - } - - if (c == EOF) { - /* Return partial line, if any. */ - if (read_pos == *lineptr) - return -1; - else - break; - } - - *read_pos++ = c; - nchars_avail--; - - if (c == terminator) /* Return the line. */ - break; - } - - /* Done - NUL terminate and return the number of chars read. */ - *read_pos = '\0'; - - ret = (int32_t)(read_pos - (*lineptr + offset)); - return ret; -} - -int32_t tgetline(char **lineptr, size_t *n, FILE *stream) { return getstr(lineptr, n, stream, '\n', 0); } \ No newline at end of file diff --git a/src/os/src/windows/wSemaphore.c b/src/os/src/windows/wSemaphore.c deleted file mode 100644 index 878ceba791c04277a05328ec160a43bb5a0e6efa..0000000000000000000000000000000000000000 --- a/src/os/src/windows/wSemaphore.c +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE - -#include "os.h" -#include "taosdef.h" -#include "tglobal.h" -#include "ttimer.h" -#include "tulog.h" -#include "tutil.h" -#include - -bool taosCheckPthreadValid(pthread_t thread) { return thread.p != NULL; } - -void taosResetPthread(pthread_t* thread) { thread->p = 0; } - -int64_t taosGetPthreadId(pthread_t thread) { -#ifdef PTW32_VERSION - return pthread_getw32threadid_np(thread); -#else - return (int64_t)thread; -#endif -} - -int64_t taosGetSelfPthreadId() { return GetCurrentThreadId(); } - -bool taosComparePthread(pthread_t first, pthread_t second) { return first.p == second.p; } - -int32_t taosGetPId() { return GetCurrentProcessId(); } - -int32_t taosGetCurrentAPPName(char* name, int32_t* len) { - char filepath[1024] = {0}; - - GetModuleFileName(NULL, filepath, MAX_PATH); - char* sub = strrchr(filepath, '.'); - if (sub != NULL) { - *sub = '\0'; - } - strcpy(name, filepath); - - if (len != NULL) { - *len = (int32_t)strlen(filepath); - } - - return 0; -} diff --git a/src/os/src/windows/wSocket.c b/src/os/src/windows/wSocket.c deleted file mode 100644 index ad0d87418dba082735a0e7bd25b8ae15660f9c7a..0000000000000000000000000000000000000000 --- a/src/os/src/windows/wSocket.c +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#include "os.h" -#include -#include -#include -#include -#include -#include -#include - -void taosWinSocketInit() { - static char flag = 0; - if (flag == 0) { - WORD wVersionRequested; - WSADATA wsaData; - wVersionRequested = MAKEWORD(1, 1); - if (WSAStartup(wVersionRequested, &wsaData) == 0) { - flag = 1; - } - } -} - -int32_t taosSetNonblocking(SOCKET sock, int32_t on) { - u_long mode; - if (on) { - mode = 1; - ioctlsocket(sock, FIONBIO, &mode); - } else { - mode = 0; - ioctlsocket(sock, FIONBIO, &mode); - } - return 0; -} - -void taosIgnSIGPIPE() {} -void taosBlockSIGPIPE() {} -void taosSetMaskSIGPIPE() {} - -int32_t taosSetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *optval, int32_t optlen) { - if (level == SOL_SOCKET && optname == TCP_KEEPCNT) { - return 0; - } - - if (level == SOL_TCP && optname == TCP_KEEPIDLE) { - return 0; - } - - if (level == SOL_TCP && optname == TCP_KEEPINTVL) { - return 0; - } - - if (level == SOL_TCP && optname == TCP_KEEPCNT) { - return 0; - } - - return setsockopt(socketfd, level, optname, optval, optlen); -} - -#ifdef _MSC_VER -//#if _MSC_VER >= 1900 - -uint32_t taosInetAddr(char *ipAddr) { - uint32_t value; - int32_t ret = inet_pton(AF_INET, ipAddr, &value); - if (ret <= 0) { - return INADDR_NONE; - } else { - return value; - } -} - -const char *taosInetNtoa(struct in_addr ipInt) { - // not thread safe, only for debug usage while print log - static char tmpDstStr[16]; - return inet_ntop(AF_INET, &ipInt, tmpDstStr, INET6_ADDRSTRLEN); -} - -//#endif -#endif - -#if defined(_TD_GO_DLL_) - -uint64_t htonll(uint64_t val) { return (((uint64_t)htonl(val)) << 32) + htonl(val >> 32); } - -#endif diff --git a/src/os/src/windows/wString.c b/src/os/src/windows/wString.c deleted file mode 100644 index 96a604ae6e3bfb2088a376894c7083a9514ae67f..0000000000000000000000000000000000000000 --- a/src/os/src/windows/wString.c +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "taosdef.h" -#include "tglobal.h" -#include "ttimer.h" -#include "tulog.h" -#include "tutil.h" - -/* - * Get next token from string *stringp, where tokens are possibly-empty - * strings separated by characters from delim. - * - * Writes NULs into the string at *stringp to end tokens. - * delim need not remain constant from call to call. - * On return, *stringp points past the last NUL written (if there might - * be further tokens), or is NULL (if there are definitely no moretokens). - * - * If *stringp is NULL, strsep returns NULL. - */ -char *strsep(char **stringp, const char *delim) { - char * s; - const char *spanp; - int32_t c, sc; - char *tok; - if ((s = *stringp) == NULL) - return (NULL); - for (tok = s;;) { - c = *s++; - spanp = delim; - do { - if ((sc = *spanp++) == c) { - if (c == 0) - s = NULL; - else - s[-1] = 0; - *stringp = s; - return (tok); - } - } while (sc != 0); - } - /* NOTREACHED */ -} - -char *getpass(const char *prefix) { - static char passwd[TSDB_KEY_LEN] = {0}; - memset(passwd, 0, TSDB_KEY_LEN); - printf("%s", prefix); - - int32_t index = 0; - char ch; - while (index < TSDB_KEY_LEN) { - ch = getch(); - if (ch == '\n' || ch == '\r') { - break; - } else { - passwd[index++] = ch; - } - } - - return passwd; -} - -int32_t twcslen(const wchar_t *wcs) { - int32_t *wstr = (int32_t *)wcs; - if (NULL == wstr) { - return 0; - } - - int32_t n = 0; - while (1) { - if (0 == *wstr++) { - break; - } - n++; - } - - return n; -} - -int32_t tasoUcs4Compare(void *f1_ucs4, void *f2_ucs4, int32_t bytes) { - for (int32_t i = 0; i < bytes; ++i) { - int32_t f1 = *(int32_t *)((char *)f1_ucs4 + i * 4); - int32_t f2 = *(int32_t *)((char *)f2_ucs4 + i * 4); - - if ((f1 == 0 && f2 != 0) || (f1 != 0 && f2 == 0)) { - return f1 - f2; - } else if (f1 == 0 && f2 == 0) { - return 0; - } - - if (f1 != f2) { - return f1 - f2; - } - } - - return 0; - -#if 0 - int32_t ucs4_max_len = bytes + 4; - char *f1_mbs = calloc(bytes, 1); - char *f2_mbs = calloc(bytes, 1); - if (taosUcs4ToMbs(f1_ucs4, ucs4_max_len, f1_mbs) < 0) { - return -1; - } - if (taosUcs4ToMbs(f2_ucs4, ucs4_max_len, f2_mbs) < 0) { - return -1; - } - int32_t ret = strcmp(f1_mbs, f2_mbs); - free(f1_mbs); - free(f2_mbs); - return ret; -#endif -} - -/* Copy memory to memory until the specified number of bytes -has been copied, return pointer to following byte. -Overlap is NOT handled correctly. */ -void *mempcpy(void *dest, const void *src, size_t len) { - return (char*)memcpy(dest, src, len) + len; -} - -/* Copy SRC to DEST, returning the address of the terminating '\0' in DEST. */ -char *stpcpy (char *dest, const char *src) { - size_t len = strlen (src); - return (char*)memcpy(dest, src, len + 1) + len; -} - -/* Copy no more than N characters of SRC to DEST, returning the address of - the terminating '\0' in DEST, if any, or else DEST + N. */ -char *stpncpy (char *dest, const char *src, size_t n) { - size_t size = strnlen (src, n); - memcpy (dest, src, size); - dest += size; - if (size == n) - return dest; - return memset (dest, '\0', n - size); -} \ No newline at end of file diff --git a/src/os/src/windows/wSysinfo.c b/src/os/src/windows/wSysinfo.c deleted file mode 100644 index 89101ee148b41c2aae667b04cf769f7e8503af08..0000000000000000000000000000000000000000 --- a/src/os/src/windows/wSysinfo.c +++ /dev/null @@ -1,293 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "taosdef.h" -#include "tconfig.h" -#include "tglobal.h" -#include "ttimer.h" -#include "tulog.h" -#include "tutil.h" -#include "taoserror.h" -#if (_WIN64) -#include -#include -#include -#include -#include -#include -#pragma comment(lib, "Mswsock.lib ") -#endif - -#include - -#pragma warning(push) -#pragma warning(disable : 4091) -#include -#pragma warning(pop) - -static int32_t taosGetTotalMemory() { - MEMORYSTATUSEX memsStat; - memsStat.dwLength = sizeof(memsStat); - if (!GlobalMemoryStatusEx(&memsStat)) { - return 0; - } - - float nMemTotal = memsStat.ullTotalPhys / (1024.0f * 1024.0f); - return (int32_t)nMemTotal; -} - -bool taosGetSysMemory(float *memoryUsedMB) { - MEMORYSTATUSEX memsStat; - memsStat.dwLength = sizeof(memsStat); - if (!GlobalMemoryStatusEx(&memsStat)) { - return false; - } - - float nMemFree = memsStat.ullAvailPhys / (1024.0f * 1024.0f); - float nMemTotal = memsStat.ullTotalPhys / (1024.0f * 1024.0f); - - *memoryUsedMB = nMemTotal - nMemFree; - return true; -} - -bool taosGetProcMemory(float *memoryUsedMB) { - unsigned bytes_used = 0; - -#if defined(_WIN64) && defined(_MSC_VER) - PROCESS_MEMORY_COUNTERS pmc; - HANDLE cur_proc = GetCurrentProcess(); - - if (GetProcessMemoryInfo(cur_proc, &pmc, sizeof(pmc))) { - bytes_used = (unsigned)(pmc.WorkingSetSize + pmc.PagefileUsage); - } -#endif - - *memoryUsedMB = (float)bytes_used / 1024 / 1024; - return true; -} - -static void taosGetSystemTimezone() { - // get and set default timezone - SGlobalCfg *cfg_timezone = taosGetConfigOption("timezone"); - if (cfg_timezone && cfg_timezone->cfgStatus < TAOS_CFG_CSTATUS_DEFAULT) { - char *tz = getenv("TZ"); - if (tz == NULL || strlen(tz) == 0) { - strcpy(tsTimezone, "not configured"); - } else { - strcpy(tsTimezone, tz); - } - cfg_timezone->cfgStatus = TAOS_CFG_CSTATUS_DEFAULT; - uInfo("timezone not configured, use default"); - } -} - -static void taosGetSystemLocale() { - // get and set default locale - SGlobalCfg *cfg_locale = taosGetConfigOption("locale"); - if (cfg_locale && cfg_locale->cfgStatus < TAOS_CFG_CSTATUS_DEFAULT) { - char *locale = setlocale(LC_CTYPE, "chs"); - if (locale != NULL) { - tstrncpy(tsLocale, locale, TSDB_LOCALE_LEN); - cfg_locale->cfgStatus = TAOS_CFG_CSTATUS_DEFAULT; - uInfo("locale not configured, set to default:%s", tsLocale); - } - } - - SGlobalCfg *cfg_charset = taosGetConfigOption("charset"); - if (cfg_charset && cfg_charset->cfgStatus < TAOS_CFG_CSTATUS_DEFAULT) { - strcpy(tsCharset, "cp936"); - cfg_charset->cfgStatus = TAOS_CFG_CSTATUS_DEFAULT; - uInfo("charset not configured, set to default:%s", tsCharset); - } -} - -int32_t taosGetCpuCores() { - SYSTEM_INFO info; - GetSystemInfo(&info); - return (int32_t)info.dwNumberOfProcessors; -} - -bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { - *sysCpuUsage = 0; - *procCpuUsage = 0; - return true; -} - -int32_t taosGetDiskSize(char *dataDir, SysDiskSize *diskSize) { - unsigned _int64 i64FreeBytesToCaller; - unsigned _int64 i64TotalBytes; - unsigned _int64 i64FreeBytes; - - BOOL fResult = GetDiskFreeSpaceExA(dataDir, (PULARGE_INTEGER)&i64FreeBytesToCaller, (PULARGE_INTEGER)&i64TotalBytes, - (PULARGE_INTEGER)&i64FreeBytes); - if (fResult) { - diskSize->tsize = (int64_t)(i64TotalBytes); - diskSize->avail = (int64_t)(i64FreeBytesToCaller); - diskSize->used = (int64_t)(i64TotalBytes - i64FreeBytes); - return 0; - } else { - uError("failed to get disk size, dataDir:%s errno:%s", tsDataDir, strerror(errno)); - terrno = TAOS_SYSTEM_ERROR(errno); - return -1; - } -} - -bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { - if (bytes) *bytes = 0; - if (rbytes) *rbytes = 0; - if (tbytes) *tbytes = 0; - return true; -} - -bool taosGetBandSpeed(float *bandSpeedKb) { - *bandSpeedKb = 0; - return true; -} - -bool taosReadProcIO(int64_t *readbyte, int64_t *writebyte) { - IO_COUNTERS io_counter; - if (GetProcessIoCounters(GetCurrentProcess(), &io_counter)) { - if (readbyte) *readbyte = io_counter.ReadTransferCount; - if (writebyte) *writebyte = io_counter.WriteTransferCount; - return true; - } - return false; -} - -bool taosGetProcIO(float *readKB, float *writeKB) { - static int64_t lastReadbyte = -1; - static int64_t lastWritebyte = -1; - - int64_t curReadbyte = 0; - int64_t curWritebyte = 0; - - if (!taosReadProcIO(&curReadbyte, &curWritebyte)) { - return false; - } - - if (lastReadbyte == -1 || lastWritebyte == -1) { - lastReadbyte = curReadbyte; - lastWritebyte = curWritebyte; - return false; - } - - *readKB = (float)((double)(curReadbyte - lastReadbyte) / 1024); - *writeKB = (float)((double)(curWritebyte - lastWritebyte) / 1024); - if (*readKB < 0) *readKB = 0; - if (*writeKB < 0) *writeKB = 0; - - lastReadbyte = curReadbyte; - lastWritebyte = curWritebyte; - - return true; -} - -void taosGetSystemInfo() { - tsNumOfCores = taosGetCpuCores(); - tsTotalMemoryMB = taosGetTotalMemory(); - - float tmp1, tmp2; - // taosGetDisk(); - taosGetBandSpeed(&tmp1); - taosGetCpuUsage(&tmp1, &tmp2); - taosGetProcIO(&tmp1, &tmp2); - - taosGetSystemTimezone(); - taosGetSystemLocale(); -} - -void taosPrintOsInfo() { - uInfo(" os numOfCores: %d", tsNumOfCores); - uInfo(" os totalMemory: %d(MB)", tsTotalMemoryMB); - uInfo("=================================="); -} - -void taosPrintDiskInfo() { - uInfo("=================================="); - uInfo(" os totalDisk: %f(GB)", tsTotalDataDirGB); - uInfo(" os usedDisk: %f(GB)", tsUsedDataDirGB); - uInfo(" os availDisk: %f(GB)", tsAvailDataDirGB); - uInfo("=================================="); -} - -void taosKillSystem() { - uError("function taosKillSystem, exit!"); - exit(0); -} - -int taosSystem(const char *cmd) { - uError("taosSystem not support"); - return -1; -} - -int flock(int fd, int option) { return 0; } - -LONG WINAPI FlCrashDump(PEXCEPTION_POINTERS ep) { - typedef BOOL(WINAPI * FxMiniDumpWriteDump)(IN HANDLE hProcess, IN DWORD ProcessId, IN HANDLE hFile, - IN MINIDUMP_TYPE DumpType, - IN CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, - IN CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, - IN CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam); - - HMODULE dll = LoadLibrary("dbghelp.dll"); - if (dll == NULL) return EXCEPTION_CONTINUE_SEARCH; - FxMiniDumpWriteDump mdwd = (FxMiniDumpWriteDump)(GetProcAddress(dll, "MiniDumpWriteDump")); - if (mdwd == NULL) { - FreeLibrary(dll); - return EXCEPTION_CONTINUE_SEARCH; - } - - TCHAR path[MAX_PATH]; - DWORD len = GetModuleFileName(NULL, path, _countof(path)); - path[len - 3] = 'd'; - path[len - 2] = 'm'; - path[len - 1] = 'p'; - - HANDLE file = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); - - MINIDUMP_EXCEPTION_INFORMATION mei; - mei.ThreadId = GetCurrentThreadId(); - mei.ExceptionPointers = ep; - mei.ClientPointers = FALSE; - - (*mdwd)(GetCurrentProcess(), GetCurrentProcessId(), file, MiniDumpWithHandleData, &mei, NULL, NULL); - - CloseHandle(file); - FreeLibrary(dll); - - return EXCEPTION_CONTINUE_SEARCH; -} - -void taosSetCoreDump() { SetUnhandledExceptionFilter(&FlCrashDump); } - -bool taosGetSystemUid(char *uid) { - GUID guid; - CoCreateGuid(&guid); - - sprintf( - uid, - "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", - guid.Data1, guid.Data2, guid.Data3, - guid.Data4[0], guid.Data4[1], - guid.Data4[2], guid.Data4[3], - guid.Data4[4], guid.Data4[5], - guid.Data4[6], guid.Data4[7]); - - return true; -} - -char *taosGetCmdlineByPID(int pid) { return ""; } diff --git a/src/os/src/windows/wSystem.c b/src/os/src/windows/wSystem.c deleted file mode 100644 index 564005f79be71c9e8649442e4b29651cdfd993fd..0000000000000000000000000000000000000000 --- a/src/os/src/windows/wSystem.c +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "tglobal.h" -#include "tulog.h" - -void* taosLoadDll(const char *filename) { - return NULL; -} - -void* taosLoadSym(void* handle, char* name) { - return NULL; -} - -void taosCloseDll(void *handle) { -} - - -int taosSetConsoleEcho(bool on) -{ - HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); - DWORD mode = 0; - GetConsoleMode(hStdin, &mode ); - if (on) { - mode |= ENABLE_ECHO_INPUT; - } else { - mode &= ~ENABLE_ECHO_INPUT; - } - SetConsoleMode(hStdin, mode); - - return 0; -} diff --git a/src/os/src/windows/wTimer.c b/src/os/src/windows/wTimer.c deleted file mode 100644 index c5984f915a1baeb33a64f9eb46359b324c5df6e6..0000000000000000000000000000000000000000 --- a/src/os/src/windows/wTimer.c +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#include -#include -#include -#include - -#pragma warning( disable : 4244 ) - -typedef void (*win_timer_f)(int signo); - -void WINAPI taosWinOnTimer(UINT wTimerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR dwl, DWORD_PTR dw2) { - win_timer_f callback = *((win_timer_f *)&dwUser); - if (callback != NULL) { - callback(0); - } -} - -static MMRESULT timerId; -int taosInitTimer(win_timer_f callback, int ms) { - DWORD_PTR param = *((int64_t *) & callback); - - timerId = timeSetEvent(ms, 1, (LPTIMECALLBACK)taosWinOnTimer, param, TIME_PERIODIC); - if (timerId == 0) { - return -1; - } - return 0; -} - -void taosUninitTimer() { - timeKillEvent(timerId); -} diff --git a/src/os/src/windows/wWordexp.c b/src/os/src/windows/wWordexp.c deleted file mode 100644 index febe22ac8fa8566607fe2bf9b0be9cc7443c8012..0000000000000000000000000000000000000000 --- a/src/os/src/windows/wWordexp.c +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "taosdef.h" -#include "tglobal.h" -#include "ttimer.h" -#include "tulog.h" -#include "tutil.h" - -int wordexp(char *words, wordexp_t *pwordexp, int flags) { - pwordexp->we_offs = 0; - pwordexp->we_wordc = 1; - pwordexp->we_wordv[0] = pwordexp->wordPos; - - memset(pwordexp->wordPos, 0, 1025); - if (_fullpath(pwordexp->wordPos, words, 1024) == NULL) { - pwordexp->we_wordv[0] = words; - uError("failed to parse relative path:%s to abs path", words); - return -1; - } - - uTrace("parse relative path:%s to abs path:%s", words, pwordexp->wordPos); - return 0; -} - -void wordfree(wordexp_t *pwordexp) {} diff --git a/src/os/tests/CMakeLists.txt b/src/os/tests/CMakeLists.txt deleted file mode 100644 index 3c477641899994bf34237e93122c3d83f0365fad..0000000000000000000000000000000000000000 --- a/src/os/tests/CMakeLists.txt +++ /dev/null @@ -1,21 +0,0 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.8...3.20) -PROJECT(TDengine) - -FIND_PATH(HEADER_GTEST_INCLUDE_DIR gtest.h /usr/include/gtest /usr/local/include/gtest) -FIND_LIBRARY(LIB_GTEST_STATIC_DIR libgtest.a /usr/lib/ /usr/local/lib /usr/lib64) -FIND_LIBRARY(LIB_GTEST_SHARED_DIR libgtest.so /usr/lib/ /usr/local/lib /usr/lib64) - -IF (HEADER_GTEST_INCLUDE_DIR AND (LIB_GTEST_STATIC_DIR OR LIB_GTEST_SHARED_DIR)) - MESSAGE(STATUS "gTest library found, build unit test") - - # GoogleTest requires at least C++11 - SET(CMAKE_CXX_STANDARD 11) - - INCLUDE_DIRECTORIES(/usr/include /usr/local/include) - LINK_DIRECTORIES(/usr/lib /usr/local/lib) - - AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} SOURCE_LIST) - - ADD_EXECUTABLE(osTest ${SOURCE_LIST}) - TARGET_LINK_LIBRARIES(osTest taos os tutil common gtest pthread) -ENDIF() diff --git a/src/os/tests/test.cpp b/src/os/tests/test.cpp deleted file mode 100644 index 6b04540615da139f9dc9472115b9d46ec52b634a..0000000000000000000000000000000000000000 --- a/src/os/tests/test.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "os.h" -#include -#include -#include - -#include "taos.h" -#include "ttoken.h" -#include "tutil.h" - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} - -// test function in os module -TEST(testCase, parse_time) { - taos_options(TSDB_OPTION_TIMEZONE, "GMT-8"); - deltaToUtcInitOnce(); - - // window: 1500000001000, 1500002000000 - // pQueryAttr->interval: interval: 86400000, sliding:3600000 - int64_t key = 1500000001000; - SInterval interval = {0}; - interval.interval = 86400000; - interval.intervalUnit = 'd'; - interval.sliding = 3600000; - interval.slidingUnit = 'h'; - - int64_t s = taosTimeTruncate(key, &interval, TSDB_TIME_PRECISION_MILLI); - ASSERT_TRUE(s + interval.interval >= key); -} - - - diff --git a/src/plugins/http/inc/httpGcJson.h b/src/plugins/http/inc/httpGcJson.h index 0ba860687dfe370d5b418a7c6d7aa28d128d7a5d..eb79528c711521aed5e3f3bcf6eacd93f64f01ce 100644 --- a/src/plugins/http/inc/httpGcJson.h +++ b/src/plugins/http/inc/httpGcJson.h @@ -15,9 +15,9 @@ #ifndef TDENGINE_GC_JSON_H #define TDENGINE_GC_JSON_H +#include "../../../../include/client/taos.h" #include "httpHandle.h" #include "httpJson.h" -#include "taos.h" void gcInitQueryJson(HttpContext *pContext); void gcCleanQueryJson(HttpContext *pContext); diff --git a/src/plugins/http/inc/httpRestJson.h b/src/plugins/http/inc/httpRestJson.h index 5f19983826a4f90bc3498ca82cc9fd495b4f300c..d1f5a4552114b400b4eb4b1b5b614202001cdeb9 100644 --- a/src/plugins/http/inc/httpRestJson.h +++ b/src/plugins/http/inc/httpRestJson.h @@ -16,9 +16,9 @@ #ifndef TDENGINE_REST_JSON_H #define TDENGINE_REST_JSON_H #include +#include "../../../../include/client/taos.h" #include "httpHandle.h" #include "httpJson.h" -#include "taos.h" #define REST_JSON_SUCCESS "succ" #define REST_JSON_SUCCESS_LEN 4 diff --git a/src/plugins/http/inc/httpTgJson.h b/src/plugins/http/inc/httpTgJson.h index 6b7d0681b601fea07aa6d15c8a8a4dbd675b9b34..a7ec4f8cb66b6686c8e86ec217286494bc55734b 100644 --- a/src/plugins/http/inc/httpTgJson.h +++ b/src/plugins/http/inc/httpTgJson.h @@ -16,9 +16,9 @@ #ifndef TDENGINE_TG_JSON_H #define TDENGINE_TG_JSON_H +#include "../../../../include/client/taos.h" #include "httpHandle.h" #include "httpJson.h" -#include "taos.h" void tgInitQueryJson(HttpContext *pContext); void tgCleanQueryJson(HttpContext *pContext); diff --git a/src/plugins/http/src/httpQueue.c b/src/plugins/http/src/httpQueue.c index 677ab0c91d4a6e446eae6dd5213788166b2c40bf..917179078d4f33662b2c150e65626c825eb2a1b0 100644 --- a/src/plugins/http/src/httpQueue.c +++ b/src/plugins/http/src/httpQueue.c @@ -14,18 +14,18 @@ */ #define _DEFAULT_SOURCE -#include "os.h" -#include "tqueue.h" -#include "tnote.h" -#include "taos.h" -#include "tsclient.h" -#include "httpInt.h" +#include "httpQueue.h" +#include "../../../../include/client/taos.h" +#include "httpAuth.h" #include "httpContext.h" -#include "httpSql.h" +#include "httpInt.h" #include "httpResp.h" -#include "httpAuth.h" #include "httpSession.h" -#include "httpQueue.h" +#include "httpSql.h" +#include "os.h" +#include "tnote.h" +#include "tqueue.h" +#include "tsclient.h" typedef struct { pthread_t thread; diff --git a/src/plugins/http/src/httpSession.c b/src/plugins/http/src/httpSession.c index 2e1ee7df2fe53d835ee5f3b61bd7619a9121edce..17c12d36a39748bc7d15b019c2d43106524acaa6 100644 --- a/src/plugins/http/src/httpSession.c +++ b/src/plugins/http/src/httpSession.c @@ -14,14 +14,14 @@ */ #define _DEFAULT_SOURCE +#include "httpSession.h" +#include "../../../../include/client/taos.h" +#include "httpContext.h" +#include "httpInt.h" #include "os.h" -#include "taos.h" #include "taoserror.h" -#include "tglobal.h" #include "tcache.h" -#include "httpInt.h" -#include "httpContext.h" -#include "httpSession.h" +#include "tglobal.h" void httpCreateSession(HttpContext *pContext, void *taos) { HttpServer *server = &tsHttpServer; diff --git a/src/plugins/http/src/httpSql.c b/src/plugins/http/src/httpSql.c index 602767a6563b3ca3430501c0dbcee65333f1d44b..28127f582aa6e0f4e85e80e4cd5c9612139ffd15 100644 --- a/src/plugins/http/src/httpSql.c +++ b/src/plugins/http/src/httpSql.c @@ -14,18 +14,18 @@ */ #define _DEFAULT_SOURCE -#include "os.h" -#include "tnote.h" -#include "taos.h" -#include "taoserror.h" -#include "tsclient.h" -#include "httpInt.h" -#include "httpContext.h" #include "httpSql.h" -#include "httpResp.h" +#include "../../../../include/client/taos.h" #include "httpAuth.h" -#include "httpSession.h" +#include "httpContext.h" +#include "httpInt.h" #include "httpQueue.h" +#include "httpResp.h" +#include "httpSession.h" +#include "os.h" +#include "taoserror.h" +#include "tnote.h" +#include "tsclient.h" void httpProcessMultiSql(HttpContext *pContext); diff --git a/src/plugins/http/src/httpSystem.c b/src/plugins/http/src/httpSystem.c index 085863f4e410ec7f80e531db6045594fbed37201..878a3251c030610b5866fa148c11a93465becd31 100644 --- a/src/plugins/http/src/httpSystem.c +++ b/src/plugins/http/src/httpSystem.c @@ -14,23 +14,23 @@ */ #define _DEFAULT_SOURCE -#include "os.h" -#include "taos.h" -#include "tglobal.h" -#include "tsocket.h" -#include "ttimer.h" -#include "tadmin.h" -#include "httpInt.h" +#include "../../../../include/client/taos.h" #include "httpContext.h" -#include "httpSession.h" -#include "httpServer.h" -#include "httpResp.h" +#include "httpGcHandle.h" #include "httpHandle.h" +#include "httpInt.h" +#include "httpMetricsHandle.h" #include "httpQueue.h" -#include "httpGcHandle.h" +#include "httpResp.h" #include "httpRestHandle.h" +#include "httpServer.h" +#include "httpSession.h" #include "httpTgHandle.h" -#include "httpMetricsHandle.h" +#include "os.h" +#include "tadmin.h" +#include "tglobal.h" +#include "tsocket.h" +#include "ttimer.h" #ifndef _ADMIN void adminInitHandle(HttpServer* pServer) {} diff --git a/src/plugins/http/src/httpUtil.c b/src/plugins/http/src/httpUtil.c index f30ac7326eef20f4abf5558b288f16f6ee313b42..12f3a6efd8168d9badcf62f3322e27c1dec769a7 100644 --- a/src/plugins/http/src/httpUtil.c +++ b/src/plugins/http/src/httpUtil.c @@ -14,13 +14,13 @@ */ #define _DEFAULT_SOURCE -#include "os.h" -#include "tmd5.h" -#include "taos.h" +#include "httpUtil.h" +#include "../../../../include/client/taos.h" #include "httpInt.h" #include "httpResp.h" #include "httpSql.h" -#include "httpUtil.h" +#include "os.h" +#include "tmd5.h" #include "ttoken.h" bool httpCheckUsedbSql(char *sql) { diff --git a/src/plugins/mqtt/src/mqttSystem.c b/src/plugins/mqtt/src/mqttSystem.c index e0f2f393bb10706daadca35715d7e0ae68d7c436..d0c33d43a83b7546ced3a3fb80cb370b4d99112d 100644 --- a/src/plugins/mqtt/src/mqttSystem.c +++ b/src/plugins/mqtt/src/mqttSystem.c @@ -14,16 +14,16 @@ */ #define _DEFAULT_SOURCE -#include "os.h" +#include "../../../../include/client/taos.h" #include "mqtt.h" #include "mqttInit.h" #include "mqttLog.h" #include "mqttPayload.h" -#include "tmqtt.h" +#include "os.h" #include "posix_sockets.h" -#include "taos.h" -#include "tglobal.h" #include "taoserror.h" +#include "tglobal.h" +#include "tmqtt.h" struct SMqttReconnectState tsMqttStatus = {0}; struct mqtt_client tsMqttClient = {0}; diff --git a/src/query/inc/qSqlparser.h b/src/query/inc/qSqlparser.h index c231c90996e00d84a70c7141eac69c5a59e20254..445380f40aa090e75f80e2f5b75032a48a37180b 100644 --- a/src/query/inc/qSqlparser.h +++ b/src/query/inc/qSqlparser.h @@ -20,7 +20,7 @@ extern "C" { #endif -#include "taos.h" +#include "../../../include/client/taos.h" #include "taosmsg.h" #include "tstrbuild.h" #include "ttoken.h" diff --git a/src/query/inc/sql.y b/src/query/inc/sql.y index 336e8620f210351471bddb9c94d56fcaa7f8a0fc..800ee034414e2d282bd1aa506b4e440a48f4a6d1 100644 --- a/src/query/inc/sql.y +++ b/src/query/inc/sql.y @@ -259,7 +259,7 @@ acct_optr(Y) ::= pps(C) tseries(D) storage(P) streams(F) qtime(Q) dbs(E) users(K intitemlist(A) ::= intitemlist(X) COMMA intitem(Y). { A = tVariantListAppend(X, &Y, -1); } intitemlist(A) ::= intitem(X). { A = tVariantListAppend(NULL, &X, -1); } -intitem(A) ::= INTEGER(X). { toTSDBType(X.type); tVariantCreate(&A, &X); } +intitem(A) ::= INTEGER(X). { toTSDBType(X.type); taosVariantCreate(&A, &X); } %type keep {SArray*} %destructor keep {taosArrayDestroy($$);} @@ -444,39 +444,39 @@ column(A) ::= ids(X) typename(Y). { tagitemlist(A) ::= tagitemlist(X) COMMA tagitem(Y). { A = tVariantListAppend(X, &Y, -1); } tagitemlist(A) ::= tagitem(X). { A = tVariantListAppend(NULL, &X, -1); } -tagitem(A) ::= INTEGER(X). { toTSDBType(X.type); tVariantCreate(&A, &X); } -tagitem(A) ::= FLOAT(X). { toTSDBType(X.type); tVariantCreate(&A, &X); } -tagitem(A) ::= STRING(X). { toTSDBType(X.type); tVariantCreate(&A, &X); } -tagitem(A) ::= BOOL(X). { toTSDBType(X.type); tVariantCreate(&A, &X); } -tagitem(A) ::= NULL(X). { X.type = 0; tVariantCreate(&A, &X); } -tagitem(A) ::= NOW(X). { X.type = TSDB_DATA_TYPE_TIMESTAMP; tVariantCreate(&A, &X);} +tagitem(A) ::= INTEGER(X). { toTSDBType(X.type); taosVariantCreate(&A, &X); } +tagitem(A) ::= FLOAT(X). { toTSDBType(X.type); taosVariantCreate(&A, &X); } +tagitem(A) ::= STRING(X). { toTSDBType(X.type); taosVariantCreate(&A, &X); } +tagitem(A) ::= BOOL(X). { toTSDBType(X.type); taosVariantCreate(&A, &X); } +tagitem(A) ::= NULL(X). { X.type = 0; taosVariantCreate(&A, &X); } +tagitem(A) ::= NOW(X). { X.type = TSDB_DATA_TYPE_TIMESTAMP; taosVariantCreate(&A, &X);} tagitem(A) ::= MINUS(X) INTEGER(Y).{ X.n += Y.n; X.type = Y.type; toTSDBType(X.type); - tVariantCreate(&A, &X); + taosVariantCreate(&A, &X); } tagitem(A) ::= MINUS(X) FLOAT(Y). { X.n += Y.n; X.type = Y.type; toTSDBType(X.type); - tVariantCreate(&A, &X); + taosVariantCreate(&A, &X); } tagitem(A) ::= PLUS(X) INTEGER(Y). { X.n += Y.n; X.type = Y.type; toTSDBType(X.type); - tVariantCreate(&A, &X); + taosVariantCreate(&A, &X); } tagitem(A) ::= PLUS(X) FLOAT(Y). { X.n += Y.n; X.type = Y.type; toTSDBType(X.type); - tVariantCreate(&A, &X); + taosVariantCreate(&A, &X); } //////////////////////// The SELECT statement ///////////////////////////////// @@ -599,7 +599,7 @@ fill_opt(N) ::= . { N = 0; } fill_opt(N) ::= FILL LP ID(Y) COMMA tagitemlist(X) RP. { tVariant A = {0}; toTSDBType(Y.type); - tVariantCreate(&A, &Y); + taosVariantCreate(&A, &Y); tVariantListInsert(X, &A, -1, 0); N = X; @@ -639,7 +639,7 @@ item(A) ::= ids(X) cpxName(Y). { toTSDBType(X.type); X.n += Y.n; - tVariantCreate(&A, &X); + taosVariantCreate(&A, &X); } %type sortorder {int} @@ -719,7 +719,7 @@ expr(A) ::= BOOL(X). { A = tSqlExprCreateIdValue(&X, TK_BOOL);} expr(A) ::= NULL(X). { A = tSqlExprCreateIdValue(&X, TK_NULL);} // ordinary functions: min(x), max(x), top(k, 20) -expr(A) ::= ID(X) LP exprlist(Y) RP(E). { tStrTokenAppend(pInfo->funcs, &X); A = tSqlExprCreateFunction(Y, &X, &E, X.type); } +expr(A) ::= ID(X) LP exprlist(Y) RP(E). { (pInfo->funcs, &X); A = tSqlExprCreateFunction(Y, &X, &E, X.type); } // for parsing sql functions with wildcard for parameters. e.g., count(*)/first(*)/last(*) operation expr(A) ::= ID(X) LP STAR RP(Y). { tStrTokenAppend(pInfo->funcs, &X); A = tSqlExprCreateFunction(NULL, &X, &Y, X.type); } diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index 54a5423219c8822e42c20a1d9a0d392913f8fdef..ec576eec8ba5842ca339f7f2e9f574c8f9c88100 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -19,17 +19,17 @@ #include "exception.h" #include "hash.h" -#include "texpr.h" #include "qExecutor.h" #include "qResultbuf.h" +#include "qScript.h" #include "qUtil.h" #include "queryLog.h" -#include "tlosertree.h" -#include "ttype.h" #include "tcompare.h" -#include "tscompression.h" -#include "qScript.h" +#include "tcompression.h" +#include "texpr.h" +#include "tlosertree.h" #include "tscLog.h" +#include "ttype.h" #define IS_MASTER_SCAN(runtime) ((runtime)->scanFlag == MASTER_SCAN) #define IS_REVERSE_SCAN(runtime) ((runtime)->scanFlag == REVERSE_SCAN) diff --git a/src/query/src/qExtbuffer.c b/src/query/src/qExtbuffer.c index 5994099a0d1ad6b1a87aa19edb6151680128f6df..35cd7eeac272762ea53ebb7468ba7c38407332b3 100644 --- a/src/query/src/qExtbuffer.c +++ b/src/query/src/qExtbuffer.c @@ -12,16 +12,16 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ +#include "qExtbuffer.h" +#include "../../../include/client/taos.h" #include "os.h" #include "qAggMain.h" +#include "qExecutor.h" #include "queryLog.h" -#include "taos.h" #include "taosdef.h" #include "taosmsg.h" -#include "tulog.h" -#include "qExecutor.h" -#include "qExtbuffer.h" #include "tcompare.h" +#include "tulog.h" #define COLMODEL_GET_VAL(data, schema, allrow, rowId, colId) \ (data + (schema)->pFields[colId].offset * (allrow) + (rowId) * (schema)->pFields[colId].field.bytes) @@ -398,7 +398,7 @@ int32_t columnValueAscendingComparator(char *f1, char *f2, int32_t type, int32_t if (len1 != len2) { return len1 > len2 ? 1 : -1; } else { - int32_t ret = tasoUcs4Compare(varDataVal(f1), varDataVal(f2), len1); + int32_t ret = tasoUcs4Compare(varDataVal(f1), varDataVal(f2), len1, TSDB_NCHAR_SIZE); if (ret == 0) { return 0; } diff --git a/src/query/src/qResultbuf.c b/src/query/src/qResultbuf.c index 63eba51d6b5f9832d162336574c59aa964634bf3..3724362e2991bac86ca15d299b651f8b11668fdc 100644 --- a/src/query/src/qResultbuf.c +++ b/src/query/src/qResultbuf.c @@ -1,10 +1,10 @@ #include "qResultbuf.h" -#include "stddef.h" -#include "tscompression.h" #include "hash.h" #include "qExtbuffer.h" #include "queryLog.h" +#include "stddef.h" #include "taoserror.h" +#include "tcompression.h" #define GET_DATA_PAYLOAD(_p) ((char *)(_p)->pData + POINTER_BYTES) #define NO_IN_MEM_AVAILABLE_PAGES(_b) (listNEles((_b)->lruList) >= (_b)->inMemPages) diff --git a/src/query/src/qSqlParser.c b/src/query/src/qSqlParser.c index d156230efbc75c46205637747bb58f86d13763fe..c6c5690b2c1083f4e829c017e6b5915c645ac7dd 100644 --- a/src/query/src/qSqlParser.c +++ b/src/query/src/qSqlParser.c @@ -17,7 +17,7 @@ #include "os.h" #include "taosdef.h" #include "taosmsg.h" -#include "tcmdtype.h" +#include "tmsgtype.h" #include "tstrbuild.h" #include "ttoken.h" #include "ttokendef.h" diff --git a/src/query/src/qTsbuf.c b/src/query/src/qTsbuf.c index 4cf05dd2c7703c7879410faa2632e17a16d595fd..92ca8412722cc17614019edcabe027f8598dca5d 100644 --- a/src/query/src/qTsbuf.c +++ b/src/query/src/qTsbuf.c @@ -1,8 +1,8 @@ #include "qTsbuf.h" +#include "queryLog.h" #include "taoserror.h" -#include "tscompression.h" +#include "tcompression.h" #include "tutil.h" -#include "queryLog.h" static int32_t getDataStartOffset(); static void TSBufUpdateGroupInfo(STSBuf* pTSBuf, int32_t index, STSGroupBlockInfo* pBlockInfo); diff --git a/src/query/src/qUtil.c b/src/query/src/qUtil.c index bc27e094db3dcb85ffa73810e922d73cd42ab3a0..f52d6de5d2d584d7909ffb3c1f1379ff75d490c0 100644 --- a/src/query/src/qUtil.c +++ b/src/query/src/qUtil.c @@ -19,10 +19,10 @@ #include "qExecutor.h" #include "qUtil.h" +#include "queryLog.h" #include "tbuffer.h" +#include "tcompression.h" #include "tlosertree.h" -#include "queryLog.h" -#include "tscompression.h" typedef struct SCompSupporter { STableQueryInfo **pTableQueryInfo; diff --git a/src/query/src/sql.c b/src/query/src/sql.c index e89b6232f7e42b764df7660f06dcd207bfe6e4de..4fe3e860a6a90dbdd96bd95faacb5d48df2ee541 100644 --- a/src/query/src/sql.c +++ b/src/query/src/sql.c @@ -27,13 +27,13 @@ /************ Begin %include sections from the grammar ************************/ #line 23 "sql.y" +#include +#include #include #include #include -#include -#include #include "qSqlparser.h" -#include "tcmdtype.h" +#include "tmsgtype.h" #include "ttoken.h" #include "ttokendef.h" #include "tutil.h" diff --git a/src/rpc/CMakeLists.txt b/src/rpc/CMakeLists.txt deleted file mode 100644 index 14b77356baa4b87a201e6ff10e785db99cbd47a6..0000000000000000000000000000000000000000 --- a/src/rpc/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.8...3.20) -PROJECT(TDengine) - -INCLUDE_DIRECTORIES(inc) -AUX_SOURCE_DIRECTORY(src SRC) - -ADD_LIBRARY(trpc ${SRC}) -TARGET_LINK_LIBRARIES(trpc tutil lz4 common) - -ADD_SUBDIRECTORY(test) diff --git a/src/rpc/test/CMakeLists.txt b/src/rpc/test/CMakeLists.txt deleted file mode 100644 index a32ac9943d08fe00427ec58520809b4f04657315..0000000000000000000000000000000000000000 --- a/src/rpc/test/CMakeLists.txt +++ /dev/null @@ -1,32 +0,0 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.8...3.20) -PROJECT(TDengine) - -INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/rpc/inc) - -IF (TD_LINUX) - LIST(APPEND CLIENT_SRC ./rclient.c) - ADD_EXECUTABLE(rclient ${CLIENT_SRC}) - TARGET_LINK_LIBRARIES(rclient trpc) - - LIST(APPEND SCLIENT_SRC ./rsclient.c) - ADD_EXECUTABLE(rsclient ${SCLIENT_SRC}) - TARGET_LINK_LIBRARIES(rsclient trpc) - - LIST(APPEND SERVER_SRC ./rserver.c) - ADD_EXECUTABLE(rserver ${SERVER_SRC}) - TARGET_LINK_LIBRARIES(rserver trpc) -ENDIF () - -IF (TD_DARWIN) - LIST(APPEND CLIENT_SRC ./rclient.c) - ADD_EXECUTABLE(rclient ${CLIENT_SRC}) - TARGET_LINK_LIBRARIES(rclient trpc) - - LIST(APPEND SCLIENT_SRC ./rsclient.c) - ADD_EXECUTABLE(rsclient ${SCLIENT_SRC}) - TARGET_LINK_LIBRARIES(rsclient trpc) - - LIST(APPEND SERVER_SRC ./rserver.c) - ADD_EXECUTABLE(rserver ${SERVER_SRC}) - TARGET_LINK_LIBRARIES(rserver trpc) -ENDIF () diff --git a/src/rpc/test/rclient.c b/src/rpc/test/rclient.c deleted file mode 100644 index 2f4433f1bb32e965de66a40d7d6ae36c6804a06c..0000000000000000000000000000000000000000 --- a/src/rpc/test/rclient.c +++ /dev/null @@ -1,199 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#include "os.h" -#include "tutil.h" -#include "tglobal.h" -#include "rpcLog.h" -#include "trpc.h" -#include "taoserror.h" - -typedef struct { - int index; - SRpcEpSet epSet; - int num; - int numOfReqs; - int msgSize; - tsem_t rspSem; - tsem_t *pOverSem; - pthread_t thread; - void *pRpc; -} SInfo; - -static void processResponse(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { - SInfo *pInfo = (SInfo *)pMsg->ahandle; - tDebug("thread:%d, response is received, type:%d contLen:%d code:0x%x", pInfo->index, pMsg->msgType, pMsg->contLen, pMsg->code); - - if (pEpSet) pInfo->epSet = *pEpSet; - - rpcFreeCont(pMsg->pCont); - tsem_post(&pInfo->rspSem); -} - -static int tcount = 0; - -static void *sendRequest(void *param) { - SInfo *pInfo = (SInfo *)param; - SRpcMsg rpcMsg = {0}; - - tDebug("thread:%d, start to send request", pInfo->index); - - while ( pInfo->numOfReqs == 0 || pInfo->num < pInfo->numOfReqs) { - pInfo->num++; - rpcMsg.pCont = rpcMallocCont(pInfo->msgSize); - rpcMsg.contLen = pInfo->msgSize; - rpcMsg.ahandle = pInfo; - rpcMsg.msgType = 1; - tDebug("thread:%d, send request, contLen:%d num:%d", pInfo->index, pInfo->msgSize, pInfo->num); - rpcSendRequest(pInfo->pRpc, &pInfo->epSet, &rpcMsg, NULL); - if ( pInfo->num % 20000 == 0 ) - tInfo("thread:%d, %d requests have been sent", pInfo->index, pInfo->num); - tsem_wait(&pInfo->rspSem); - } - - tDebug("thread:%d, it is over", pInfo->index); - tcount++; - - return NULL; -} - -int main(int argc, char *argv[]) { - SRpcInit rpcInit; - SRpcEpSet epSet; - int msgSize = 128; - int numOfReqs = 0; - int appThreads = 1; - char serverIp[40] = "127.0.0.1"; - char secret[TSDB_KEY_LEN] = "mypassword"; - struct timeval systemTime; - int64_t startTime, endTime; - pthread_attr_t thattr; - - // server info - epSet.numOfEps = 1; - epSet.inUse = 0; - epSet.port[0] = 7000; - epSet.port[1] = 7000; - strcpy(epSet.fqdn[0], serverIp); - strcpy(epSet.fqdn[1], "192.168.0.1"); - - // client info - memset(&rpcInit, 0, sizeof(rpcInit)); - rpcInit.localPort = 0; - rpcInit.label = "APP"; - rpcInit.numOfThreads = 1; - rpcInit.cfp = processResponse; - rpcInit.sessions = 100; - rpcInit.idleTime = tsShellActivityTimer*1000; - rpcInit.user = "michael"; - rpcInit.secret = secret; - rpcInit.ckey = "key"; - rpcInit.spi = 1; - rpcInit.connType = TAOS_CONN_CLIENT; - - for (int i=1; iindex = i; - pInfo->epSet = epSet; - pInfo->numOfReqs = numOfReqs; - pInfo->msgSize = msgSize; - tsem_init(&pInfo->rspSem, 0, 0); - pInfo->pRpc = pRpc; - pthread_create(&pInfo->thread, &thattr, sendRequest, pInfo); - pInfo++; - } - - do { - usleep(1); - } while ( tcount < appThreads); - - gettimeofday(&systemTime, NULL); - endTime = systemTime.tv_sec*1000000 + systemTime.tv_usec; - float usedTime = (endTime - startTime)/1000.0f; // mseconds - - tInfo("it takes %.3f mseconds to send %d requests to server", usedTime, numOfReqs*appThreads); - tInfo("Performance: %.3f requests per second, msgSize:%d bytes", 1000.0*numOfReqs*appThreads/usedTime, msgSize); - - int ch = getchar(); - UNUSED(ch); - - taosCloseLog(); - - return 0; -} - - diff --git a/src/rpc/test/rsclient.c b/src/rpc/test/rsclient.c deleted file mode 100644 index 65170d4abb2745a469dfda3e4146c2ea85405b33..0000000000000000000000000000000000000000 --- a/src/rpc/test/rsclient.c +++ /dev/null @@ -1,196 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - - -#include "os.h" -#include "tutil.h" -#include "tglobal.h" -#include "rpcLog.h" -#include "trpc.h" -#include "taoserror.h" - -typedef struct { - int index; - SRpcEpSet epSet; - int num; - int numOfReqs; - int msgSize; - tsem_t rspSem; - tsem_t *pOverSem; - pthread_t thread; - void *pRpc; -} SInfo; - - -static int tcount = 0; -static int terror = 0; - -static void *sendRequest(void *param) { - SInfo *pInfo = (SInfo *)param; - SRpcMsg rpcMsg, rspMsg; - - tDebug("thread:%d, start to send request", pInfo->index); - - while ( pInfo->numOfReqs == 0 || pInfo->num < pInfo->numOfReqs) { - pInfo->num++; - rpcMsg.pCont = rpcMallocCont(pInfo->msgSize); - rpcMsg.contLen = pInfo->msgSize; - rpcMsg.handle = pInfo; - rpcMsg.msgType = 1; - tDebug("thread:%d, send request, contLen:%d num:%d", pInfo->index, pInfo->msgSize, pInfo->num); - - rpcSendRecv(pInfo->pRpc, &pInfo->epSet, &rpcMsg, &rspMsg); - - // handle response - if (rspMsg.code != 0) terror++; - - tDebug("thread:%d, rspLen:%d code:%d", pInfo->index, rspMsg.contLen, rspMsg.code); - - rpcFreeCont(rspMsg.pCont); - - if ( pInfo->num % 20000 == 0 ) - tInfo("thread:%d, %d requests have been sent", pInfo->index, pInfo->num); - } - - tDebug("thread:%d, it is over", pInfo->index); - tcount++; - - return NULL; -} - -int main(int argc, char *argv[]) { - SRpcInit rpcInit; - SRpcEpSet epSet; - int msgSize = 128; - int numOfReqs = 0; - int appThreads = 1; - char serverIp[40] = "127.0.0.1"; - char secret[TSDB_KEY_LEN] = "mypassword"; - struct timeval systemTime; - int64_t startTime, endTime; - pthread_attr_t thattr; - - // server info - epSet.numOfEps = 1; - epSet.inUse = 0; - epSet.port[0] = 7000; - epSet.port[1] = 7000; - strcpy(epSet.fqdn[0], serverIp); - strcpy(epSet.fqdn[1], "192.168.0.1"); - - // client info - memset(&rpcInit, 0, sizeof(rpcInit)); - //rpcInit.localIp = "0.0.0.0"; - rpcInit.localPort = 0; - rpcInit.label = "APP"; - rpcInit.numOfThreads = 1; - rpcInit.sessions = 100; - rpcInit.idleTime = tsShellActivityTimer*1000; - rpcInit.user = "michael"; - rpcInit.secret = secret; - rpcInit.ckey = "key"; - rpcInit.spi = 1; - rpcInit.connType = TAOS_CONN_CLIENT; - - for (int i=1; iindex = i; - pInfo->epSet = epSet; - pInfo->numOfReqs = numOfReqs; - pInfo->msgSize = msgSize; - tsem_init(&pInfo->rspSem, 0, 0); - pInfo->pRpc = pRpc; - pthread_create(&pInfo->thread, &thattr, sendRequest, pInfo); - pInfo++; - } - - do { - usleep(1); - } while ( tcount < appThreads); - - gettimeofday(&systemTime, NULL); - endTime = systemTime.tv_sec*1000000 + systemTime.tv_usec; - float usedTime = (endTime - startTime)/1000.0; // mseconds - - tInfo("it takes %.3f mseconds to send %d requests to server, error num:%d", usedTime, numOfReqs*appThreads, terror); - tInfo("Performance: %.3f requests per second, msgSize:%d bytes", 1000.0*numOfReqs*appThreads/usedTime, msgSize); - - taosCloseLog(); - - return 0; -} - - diff --git a/src/rpc/test/rserver.c b/src/rpc/test/rserver.c deleted file mode 100644 index 64960db0446413ebce1978b7fe310b6a34c34f1c..0000000000000000000000000000000000000000 --- a/src/rpc/test/rserver.c +++ /dev/null @@ -1,192 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -//#define _DEFAULT_SOURCE -#include "os.h" -#include "tglobal.h" -#include "rpcLog.h" -#include "trpc.h" -#include "tqueue.h" - -int msgSize = 128; -int commit = 0; -int dataFd = -1; -void *qhandle = NULL; -void *qset = NULL; - -void processShellMsg() { - static int num = 0; - taos_qall qall; - SRpcMsg *pRpcMsg, rpcMsg; - int type; - void *pvnode; - - qall = taosAllocateQall(); - - while (1) { - int numOfMsgs = taosReadAllQitemsFromQset(qset, qall, &pvnode); - tDebug("%d shell msgs are received", numOfMsgs); - if (numOfMsgs <= 0) break; - - for (int i=0; i=0) { - if ( write(dataFd, pRpcMsg->pCont, pRpcMsg->contLen) <0 ) { - tInfo("failed to write data file, reason:%s", strerror(errno)); - } - } - } - - if (commit >=2) { - num += numOfMsgs; - if ( taosFsync(dataFd) < 0 ) { - tInfo("failed to flush data to file, reason:%s", strerror(errno)); - } - - if (num % 10000 == 0) { - tInfo("%d request have been written into disk", num); - } - } - - taosResetQitems(qall); - for (int i=0; ipCont); - - memset(&rpcMsg, 0, sizeof(rpcMsg)); - rpcMsg.pCont = rpcMallocCont(msgSize); - rpcMsg.contLen = msgSize; - rpcMsg.handle = pRpcMsg->handle; - rpcMsg.code = 0; - rpcSendResponse(&rpcMsg); - - taosFreeQitem(pRpcMsg); - } - - } - - taosFreeQall(qall); - -} - -int retrieveAuthInfo(char *meterId, char *spi, char *encrypt, char *secret, char *ckey) { - // app shall retrieve the auth info based on meterID from DB or a data file - // demo code here only for simple demo - int ret = 0; - - if (strcmp(meterId, "michael") == 0) { - *spi = 1; - *encrypt = 0; - strcpy(secret, "mypassword"); - strcpy(ckey, "key"); - } else if (strcmp(meterId, "jeff") == 0) { - *spi = 0; - *encrypt = 0; - } else { - ret = -1; // user not there - } - - return ret; -} - -void processRequestMsg(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { - SRpcMsg *pTemp; - - pTemp = taosAllocateQitem(sizeof(SRpcMsg)); - memcpy(pTemp, pMsg, sizeof(SRpcMsg)); - - tDebug("request is received, type:%d, contLen:%d, item:%p", pMsg->msgType, pMsg->contLen, pTemp); - taosWriteQitem(qhandle, TAOS_QTYPE_RPC, pTemp); -} - -int main(int argc, char *argv[]) { - SRpcInit rpcInit; - char dataName[20] = "server.data"; - - taosBlockSIGPIPE(); - - memset(&rpcInit, 0, sizeof(rpcInit)); - rpcInit.localPort = 7000; - rpcInit.label = "SER"; - rpcInit.numOfThreads = 1; - rpcInit.cfp = processRequestMsg; - rpcInit.sessions = 1000; - rpcInit.idleTime = tsShellActivityTimer*1500; - rpcInit.afp = retrieveAuthInfo; - - for (int i=1; i= 0) { - close(dataFd); - remove(dataName); - } - - return 0; -} diff --git a/src/tfs/src/tfs.c b/src/tfs/src/tfs.c index 43ccb324b235c2bfed6b121ed2049474cc5ecb74..b8e8972d934ae568cc5bd1f09489094b616c901b 100644 --- a/src/tfs/src/tfs.c +++ b/src/tfs/src/tfs.c @@ -252,7 +252,7 @@ int tfsMkdirAt(const char *rname, int level, int id) { char aname[TMPNAME_LEN]; snprintf(aname, TMPNAME_LEN, "%s/%s", DISK_DIR(pDisk), rname); - if (taosMkDir(aname, 0755) != 0) { + if (!taosMkDir(aname)) { terrno = TAOS_SYSTEM_ERROR(errno); return -1; } diff --git a/src/tsdb/inc/tsdbint.h b/src/tsdb/inc/tsdbint.h index 80e92975799f47d68ff72ef80a52efb6fe901b5e..2354269c6ee861632ff0d88c2efe52470a689a48 100644 --- a/src/tsdb/inc/tsdbint.h +++ b/src/tsdb/inc/tsdbint.h @@ -26,20 +26,20 @@ // #include // #include +#include "hash.h" #include "os.h" -#include "tlog.h" #include "taosdef.h" #include "taoserror.h" +#include "tarray.h" #include "tchecksum.h" -#include "tskiplist.h" -#include "tdataformat.h" #include "tcoding.h" -#include "tscompression.h" -#include "tlockfree.h" -#include "tlist.h" -#include "hash.h" -#include "tarray.h" +#include "tcompression.h" +#include "tdataformat.h" #include "tfs.h" +#include "tlist.h" +#include "tlockfree.h" +#include "tlog.h" +#include "tskiplist.h" #include "tsocket.h" #include "tsdb.h" diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt deleted file mode 100644 index 42d0bb6316b84db7025d35afa1cbbdf331e7b1cd..0000000000000000000000000000000000000000 --- a/src/util/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -aux_source_directory(source UTIL_SRC) -add_library(util ${UTIL_SRC}) -target_include_directories( - util - PUBLIC "${CMAKE_SOURCE_DIR}/include/util" - PRIVATE "${CMAKE_SOURCE_DIR}/include" -) -target_link_libraries( - util - PRIVATE os -) \ No newline at end of file diff --git a/src/util/CMakeLists.txt.in b/src/util/CMakeLists.txt.in deleted file mode 100644 index ef304d2fcbcb3a823e2c8253ca578de551499151..0000000000000000000000000000000000000000 --- a/src/util/CMakeLists.txt.in +++ /dev/null @@ -1,44 +0,0 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.8...3.20) -PROJECT(TDengine) - -INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/rpc/inc) -INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/sync/inc) -INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/rmonotonic/inc) -INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/TSZ/sz/include) - -AUX_SOURCE_DIRECTORY(src SRC) -ADD_LIBRARY(tutil ${SRC}) - -TARGET_LINK_LIBRARIES(tutil pthread os lz4 z rmonotonic ${VAR_TSZ} ) - - - -IF (TD_LINUX) - TARGET_LINK_LIBRARIES(tutil m rt) - ADD_SUBDIRECTORY(tests) - - FIND_PATH(ICONV_INCLUDE_EXIST iconv.h /usr/include/ /usr/local/include/) - IF (ICONV_INCLUDE_EXIST) - FIND_PATH(ICONV_LIBRARY_A_EXIST libiconv.a /usr/lib/ /usr/local/lib/ /lib64) - FIND_PATH(ICONV_LIBRARY_SO_EXIST libiconv.so /usr/lib/ /usr/local/lib/ /lib64) - IF (ICONV_LIBRARY_A_EXIST OR ICONV_LIBRARY_SO_EXIST) - MESSAGE(STATUS "Use the installed libiconv library") - TARGET_LINK_LIBRARIES(tutil iconv) - ELSE () - # libiconv library is already included in GLIBC, - MESSAGE(STATUS "Use the iconv functions in GLIBC") - ENDIF () - ELSE () - MESSAGE(STATUS "Failed to find iconv, use default encoding method") - ENDIF () - -ELSEIF (TD_WINDOWS) - TARGET_LINK_LIBRARIES(tutil iconv regex winmm IPHLPAPI ws2_32 wepoll) -ELSEIF(TD_DARWIN) - TARGET_LINK_LIBRARIES(tutil m) - TARGET_LINK_LIBRARIES(tutil iconv) -ENDIF() - -IF (TD_STORAGE) - TARGET_LINK_LIBRARIES(tutil storage) -ENDIF () diff --git a/src/util/inc/hashfunc.h b/src/util/inc/hashfunc.h deleted file mode 100644 index a9563d03941496a42e71a91fc7b7aab014ff4f50..0000000000000000000000000000000000000000 --- a/src/util/inc/hashfunc.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_HASHUTIL_H -#define TDENGINE_HASHUTIL_H - -#include "os.h" - -typedef uint32_t (*_hash_fn_t)(const char *, uint32_t); - -typedef int32_t (*_equal_fn_t)(const void *a, const void *b, size_t sz); - -/** - * murmur hash algorithm - * @key usually string - * @len key length - * @seed hash seed - * @out an int32 value - */ -uint32_t MurmurHash3_32(const char *key, uint32_t len); - -/** - * - * @param key - * @param len - * @return - */ -uint32_t taosIntHash_32(const char *key, uint32_t len); -uint32_t taosIntHash_64(const char *key, uint32_t len); - - -int32_t taosFloatEqual(const void *a, const void *b, size_t sz); -int32_t taosDoubleEqual(const void *a,const void *b, size_t sz); - -_hash_fn_t taosGetDefaultHashFunction(int32_t type); - -_equal_fn_t taosGetDefaultEqualFunction(int32_t type); - -#endif //TDENGINE_HASHUTIL_H diff --git a/src/util/inc/tlog.h b/src/util/inc/tlog.h deleted file mode 100644 index 1f6a81d4b49a8a44f5c60da9a6c901be96e5d8f6..0000000000000000000000000000000000000000 --- a/src/util/inc/tlog.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_TLOG_H -#define TDENGINE_TLOG_H - -#ifdef __cplusplus -extern "C" { -#endif - -#define DEBUG_FATAL 1U -#define DEBUG_ERROR DEBUG_FATAL -#define DEBUG_WARN 2U -#define DEBUG_INFO DEBUG_WARN -#define DEBUG_DEBUG 4U -#define DEBUG_TRACE 8U -#define DEBUG_DUMP 16U - -#define DEBUG_SCREEN 64U -#define DEBUG_FILE 128U - -int32_t taosInitLog(char *logName, int32_t numOfLogLines, int32_t maxFiles); -void taosCloseLog(); -void taosResetLog(); - -void taosPrintLog(const char *flags, int32_t dflag, const char *format, ...) -#ifdef __GNUC__ - __attribute__((format(printf, 3, 4))) -#endif -; - -void taosPrintLongString(const char * flags, int32_t dflag, const char *format, ...) -#ifdef __GNUC__ - __attribute__((format(printf, 3, 4))) -#endif -; - -void taosDumpData(unsigned char *msg, int32_t len); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/util/inc/tsocket.h b/src/util/inc/tsocket.h deleted file mode 100644 index b4f55169594589b83c299694723cb234bd21ed7d..0000000000000000000000000000000000000000 --- a/src/util/inc/tsocket.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_TSOCKET_H -#define TDENGINE_TSOCKET_H - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef WINDOWS -#include "wepoll.h" -#endif - -int32_t taosReadn(SOCKET sock, char *buffer, int32_t len); -int32_t taosWriteMsg(SOCKET fd, void *ptr, int32_t nbytes); -int32_t taosReadMsg(SOCKET fd, void *ptr, int32_t nbytes); -int32_t taosNonblockwrite(SOCKET fd, char *ptr, int32_t nbytes); -int64_t taosCopyFds(SOCKET sfd, int32_t dfd, int64_t len); -int32_t taosSetNonblocking(SOCKET sock, int32_t on); - -SOCKET taosOpenUdpSocket(uint32_t localIp, uint16_t localPort); -SOCKET taosOpenTcpClientSocket(uint32_t ip, uint16_t port, uint32_t localIp); -SOCKET taosOpenTcpServerSocket(uint32_t ip, uint16_t port); -int32_t taosKeepTcpAlive(SOCKET sockFd); - -int32_t taosGetFqdn(char *); -uint32_t taosGetIpv4FromFqdn(const char *); -void tinet_ntoa(char *ipstr, uint32_t ip); -uint32_t ip2uint(const char *const ip_addr); - -#ifdef __cplusplus -} -#endif - -#endif // TDENGINE_TSOCKET_H diff --git a/src/util/src/tworker.c b/src/util/src/tworker.c deleted file mode 100644 index 8b4053bccd1ce8d9d3f58328d838f4ba5132a100..0000000000000000000000000000000000000000 --- a/src/util/src/tworker.c +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "tulog.h" -#include "tqueue.h" -#include "tworker.h" - -int32_t tWorkerInit(SWorkerPool *pPool) { - pPool->qset = taosOpenQset(); - pPool->worker = calloc(sizeof(SWorker), pPool->max); - pthread_mutex_init(&pPool->mutex, NULL); - for (int i = 0; i < pPool->max; ++i) { - SWorker *pWorker = pPool->worker + i; - pWorker->id = i; - pWorker->pPool = pPool; - } - - uInfo("worker:%s is initialized, min:%d max:%d", pPool->name, pPool->min, pPool->max); - return 0; -} - -void tWorkerCleanup(SWorkerPool *pPool) { - for (int i = 0; i < pPool->max; ++i) { - SWorker *pWorker = pPool->worker + i; - if(taosCheckPthreadValid(pWorker->thread)) { - taosQsetThreadResume(pPool->qset); - } - } - - for (int i = 0; i < pPool->max; ++i) { - SWorker *pWorker = pPool->worker + i; - if (taosCheckPthreadValid(pWorker->thread)) { - pthread_join(pWorker->thread, NULL); - } - } - - free(pPool->worker); - taosCloseQset(pPool->qset); - pthread_mutex_destroy(&pPool->mutex); - - uInfo("worker:%s is closed", pPool->name); -} - -void *tWorkerAllocQueue(SWorkerPool *pPool, void *ahandle) { - pthread_mutex_lock(&pPool->mutex); - taos_queue pQueue = taosOpenQueue(); - if (pQueue == NULL) { - pthread_mutex_unlock(&pPool->mutex); - return NULL; - } - - taosAddIntoQset(pPool->qset, pQueue, ahandle); - - // spawn a thread to process queue - if (pPool->num < pPool->max) { - do { - SWorker *pWorker = pPool->worker + pPool->num; - - pthread_attr_t thAttr; - pthread_attr_init(&thAttr); - pthread_attr_setdetachstate(&thAttr, PTHREAD_CREATE_JOINABLE); - - if (pthread_create(&pWorker->thread, &thAttr, pPool->workerFp, pWorker) != 0) { - uError("worker:%s:%d failed to create thread to process since %s", pPool->name, pWorker->id, strerror(errno)); - } - - pthread_attr_destroy(&thAttr); - pPool->num++; - uDebug("worker:%s:%d is launched, total:%d", pPool->name, pWorker->id, pPool->num); - } while (pPool->num < pPool->min); - } - - pthread_mutex_unlock(&pPool->mutex); - uDebug("worker:%s, queue:%p is allocated, ahandle:%p", pPool->name, pQueue, ahandle); - - return pQueue; -} - -void tWorkerFreeQueue(SWorkerPool *pPool, void *pQueue) { - taosCloseQueue(pQueue); - uDebug("worker:%s, queue:%p is freed", pPool->name, pQueue); -} diff --git a/src/wal/src/walMgmt.c b/src/wal/src/walMgmt.c index 05324d31eec56ee74b81c70dc451eadf83d518d2..dbff08d73039863313bc6b5cf9ec15faa5f5e432 100644 --- a/src/wal/src/walMgmt.c +++ b/src/wal/src/walMgmt.c @@ -139,7 +139,7 @@ void walClose(void *handle) { } static int32_t walInitObj(SWal *pWal) { - if (taosMkDir(pWal->path, 0755) != 0) { + if (!taosMkDir(pWal->path)) { wError("vgId:%d, path:%s, failed to create directory since %s", pWal->vgId, pWal->path, strerror(errno)); return TAOS_SYSTEM_ERROR(errno); } diff --git a/src/wal/src/walWrite.c b/src/wal/src/walWrite.c index e991bf02aa68c92d7cf4dfdb09982ebaa6541bdc..cae4291eb8011d72fd1010c9024fbec270571c62 100644 --- a/src/wal/src/walWrite.c +++ b/src/wal/src/walWrite.c @@ -51,7 +51,7 @@ int32_t walRenew(void *handle) { } snprintf(pWal->name, sizeof(pWal->name), "%s/%s%" PRId64, pWal->path, WAL_PREFIX, pWal->fileId); - pWal->tfd = tfOpenM(pWal->name, O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); + pWal->tfd = tfOpenCreateWrite(pWal->name); if (!tfValid(pWal->tfd)) { code = TAOS_SYSTEM_ERROR(errno); @@ -220,7 +220,7 @@ int32_t walRestore(void *handle, void *pVnode, FWalWrite writeFp) { // open the existing WAL file in append mode pWal->fileId = 0; snprintf(pWal->name, sizeof(pWal->name), "%s/%s%" PRId64, pWal->path, WAL_PREFIX, pWal->fileId); - pWal->tfd = tfOpenM(pWal->name, O_WRONLY | O_CREAT | O_APPEND, S_IRWXU | S_IRWXG | S_IRWXO); + pWal->tfd = tfOpenCreateWriteAppend(pWal->name); if (!tfValid(pWal->tfd)) { wError("vgId:%d, file:%s, failed to open since %s", pWal->vgId, pWal->name, strerror(errno)); return TAOS_SYSTEM_ERROR(errno); @@ -425,7 +425,7 @@ static int32_t walRestoreWalFile(SWal *pWal, void *pVnode, FWalWrite writeFp, ch return TAOS_SYSTEM_ERROR(errno); } - int64_t tfd = tfOpen(name, O_RDWR); + int64_t tfd = tfOpenReadWrite(name); if (!tfValid(tfd)) { wError("vgId:%d, file:%s, failed to open for restore since %s", pWal->vgId, name, strerror(errno)); tfree(buffer); diff --git a/tests/comparisonTest/tdengine/tdengineTest.c b/tests/comparisonTest/tdengine/tdengineTest.c index 0de419e036f6af1e1808492fbb9f7b41d785b8df..21863b3234dbfecd0f90de4a76da3409abf1d4b7 100644 --- a/tests/comparisonTest/tdengine/tdengineTest.c +++ b/tests/comparisonTest/tdengine/tdengineTest.c @@ -1,13 +1,13 @@ +#include +#include +#include #include #include #include -#include -#include -#include -#include -#include #include -#include +#include +#include +#include "../../../include/client/taos.h" typedef struct { char sql[256]; diff --git a/tests/examples/c/apitest.c b/tests/examples/c/apitest.c index 28e8d3f012dd4d6b4b12f2a01132b00a2ae7225c..9f4c7678ec059dbb3f0d4d43f0fc5adb277243d3 100644 --- a/tests/examples/c/apitest.c +++ b/tests/examples/c/apitest.c @@ -7,9 +7,8 @@ #include #include #include -#include #include - +#include "../../../include/client/taos.h" static void prepare_data(TAOS* taos) { TAOS_RES *result; diff --git a/tests/examples/c/asyncdemo.c b/tests/examples/c/asyncdemo.c index f2a96dd8256782960f9ad114229cd47714c9d1d9..9e214e0966f7848e1ea158a50882fa8c4b4739ca 100644 --- a/tests/examples/c/asyncdemo.c +++ b/tests/examples/c/asyncdemo.c @@ -24,7 +24,7 @@ #include #include -#include +#include "../../../include/client/taos.h" int points = 5; int numOfTables = 3; diff --git a/tests/examples/c/demo.c b/tests/examples/c/demo.c index f8c69d0043591afa8f5e32e80bd35e9413e60e76..c3d9a5e96a9dbd3cc2f33e047f3befabfcaf9ecb 100644 --- a/tests/examples/c/demo.c +++ b/tests/examples/c/demo.c @@ -16,11 +16,11 @@ // TAOS standard API example. The same syntax as MySQL, but only a subset // to compile: gcc -o demo demo.c -ltaos +#include #include #include #include -#include -#include // TAOS header file +#include "../../../include/client/taos.h" // TAOS header file static void queryDB(TAOS *taos, char *command) { int i; diff --git a/tests/examples/c/prepare.c b/tests/examples/c/prepare.c index 723b340a923c0bf326599e8090f8c6142a249053..f0341cfe127d4eadae29ae40a43c35c35ec2f492 100644 --- a/tests/examples/c/prepare.c +++ b/tests/examples/c/prepare.c @@ -4,8 +4,7 @@ #include #include #include -#include "taos.h" - +#include "../../../include/client/taos.h" void taosMsleep(int mseconds); diff --git a/tests/examples/c/schemaless.c b/tests/examples/c/schemaless.c index 1a551cc5f7bd600ccaf87701953f7109743e8302..21f39213cd557f3784a9c1b83172b978e3982683 100644 --- a/tests/examples/c/schemaless.c +++ b/tests/examples/c/schemaless.c @@ -1,6 +1,6 @@ -#include "taos.h" -#include "taoserror.h" +#include "../../../include/client/taos.h" #include "os.h" +#include "taoserror.h" #include #include diff --git a/tests/examples/c/stream.c b/tests/examples/c/stream.c index 30a790f061cd8ef2b870a371c2cadfb0e2a413c1..41365813aeecc042d736fab8694642937abd27e4 100644 --- a/tests/examples/c/stream.c +++ b/tests/examples/c/stream.c @@ -13,12 +13,12 @@ * along with this program. If not, see . */ +#include #include #include #include -#include #include -#include // include TDengine header file +#include "../../../include/client/taos.h" // include TDengine header file typedef struct { char server_ip[64]; diff --git a/tests/examples/c/subscribe.c b/tests/examples/c/subscribe.c index ad12f0e7a55b0f471f249f92f30cf659c94586a5..66d64d295ce5c2700088842dd2c3ce013225f3bd 100644 --- a/tests/examples/c/subscribe.c +++ b/tests/examples/c/subscribe.c @@ -4,8 +4,8 @@ #include #include #include -#include // include TDengine header file #include +#include "../../../include/client/taos.h" // include TDengine header file int nTotalRows; diff --git a/tests/examples/lua/lua51/lua_connector51.c b/tests/examples/lua/lua51/lua_connector51.c index fe2152945dc1915dca5de31458a8cbb2f007f4f2..578622bf1fb50f428a4ba44b3b02c4eeed2508b2 100644 --- a/tests/examples/lua/lua51/lua_connector51.c +++ b/tests/examples/lua/lua51/lua_connector51.c @@ -1,11 +1,11 @@ -#include #include #include +#include #include -#include "lua.h" +#include "../../../../include/client/taos.h" #include "lauxlib.h" +#include "lua.h" #include "lualib.h" -#include struct cb_param{ lua_State* state; diff --git a/tests/examples/lua/lua_connector.c b/tests/examples/lua/lua_connector.c index 8c2ea3e9e83237fc8ed9ebce687f5131352e4d14..76634ed2545fb0ce0b43291dbf1b486086b34669 100644 --- a/tests/examples/lua/lua_connector.c +++ b/tests/examples/lua/lua_connector.c @@ -1,11 +1,11 @@ -#include +#include +#include +#include #include #include +#include #include -#include -#include -#include -#include +#include "../../../include/client/taos.h" struct cb_param{ lua_State* state; diff --git a/tests/robust/robust.c b/tests/robust/robust.c index 3488ca1b9be8b6cca4d04fe5ba5841f00527b25d..1e2eb5c7be614391559bf33fe8fdfc3f1812d3f4 100644 --- a/tests/robust/robust.c +++ b/tests/robust/robust.c @@ -1,10 +1,10 @@ +#include #include #include #include -#include -#include #include #include +#include "../../include/client/taos.h" typedef struct { char querySQL[256]; diff --git a/tests/script/api/batchprepare.c b/tests/script/api/batchprepare.c index 72bb9471db8e2c3043306c332c608f1b4f1df836..386df15d864ef6917168fd731543caca8b1a17a5 100644 --- a/tests/script/api/batchprepare.c +++ b/tests/script/api/batchprepare.c @@ -1,13 +1,13 @@ // TAOS standard API example. The same syntax as MySQL, but only a subet // to compile: gcc -o prepare prepare.c -ltaos +#include #include #include #include -#include "taos.h" #include -#include #include +#include "../../../include/client/taos.h" typedef struct { TAOS *taos; diff --git a/tests/script/api/stmtBatchTest.c b/tests/script/api/stmtBatchTest.c index c488f2fa89714da3b20d9eafa4b7ff2c89580e0c..eb7845714eb9b333f4183c25380820d0f95ceedd 100644 --- a/tests/script/api/stmtBatchTest.c +++ b/tests/script/api/stmtBatchTest.c @@ -1,14 +1,14 @@ // TAOS standard API example. The same syntax as MySQL, but only a subet // to compile: gcc -o prepare prepare.c -ltaos +#include #include #include #include -#include "taos.h" -#include "taoserror.h" #include -#include #include +#include "../../../include/client/taos.h" +#include "taoserror.h" #define MAX_ROWS_OF_PER_COLUMN 32770 #define MAX_BINARY_DEF_LEN (1024*16) diff --git a/tests/script/api/stmtTest.c b/tests/script/api/stmtTest.c index 9595fe5b2d72e3291959828badf45abc2f7cb71e..c69de46f8456b50e575db6b55e9bc9a7fa179543 100644 --- a/tests/script/api/stmtTest.c +++ b/tests/script/api/stmtTest.c @@ -1,10 +1,10 @@ +#include #include #include #include -#include "taos.h" #include -#include #include +#include "../../../include/client/taos.h" #define PRINT_ERROR printf("\033[31m"); #define PRINT_SUCCESS printf("\033[32m"); diff --git a/tests/script/api/stmt_function.c b/tests/script/api/stmt_function.c index 64573ec9948fb1c6bbadd9f084c3a5a21adb1fa7..4521ad4fa300e1ad9caca8cbaddbbbfd376e0045 100644 --- a/tests/script/api/stmt_function.c +++ b/tests/script/api/stmt_function.c @@ -1,11 +1,11 @@ +#include +#include #include #include #include -#include "taos.h" #include -#include #include -#include +#include "../../../include/client/taos.h" void execute_simple_sql(void *taos, char *sql) { TAOS_RES *result = taos_query(taos, sql); diff --git a/tests/test/c/cacheTest.c b/tests/test/c/cacheTest.c index 54aca0038ed39d8d0294a5f88acd271c10f42319..ce5d7d1421d9d55d6bfab4a690d87f94c17ded29 100644 --- a/tests/test/c/cacheTest.c +++ b/tests/test/c/cacheTest.c @@ -15,8 +15,8 @@ #define _DEFAULT_SOURCE #include +#include "../../../include/client/taos.h" #include "os.h" -#include "taos.h" #include "tcache.h" #include "tulog.h" #include "tutil.h" diff --git a/tests/test/c/createNormalTable.c b/tests/test/c/createNormalTable.c index 60253e2add1ebaa1e6c2c00b073cf13672789346..f9e4aa5a3a8e1a60b77facd97e7e9bb4617294a5 100644 --- a/tests/test/c/createNormalTable.c +++ b/tests/test/c/createNormalTable.c @@ -14,13 +14,13 @@ */ #define _DEFAULT_SOURCE +#include "../../../include/client/taos.h" +#include "hash.h" #include "os.h" #include "taoserror.h" -#include "taos.h" +#include "tglobal.h" #include "tulog.h" #include "tutil.h" -#include "tglobal.h" -#include "hash.h" #define MAX_RANDOM_POINTS 20000 #define GREEN "\033[1;32m" diff --git a/tests/test/c/createTablePerformance.c b/tests/test/c/createTablePerformance.c index b94c687f2cba7310949b0a3b12b6f4fc007e5a9a..1a59657f1a48ef5ac58e788b2bc2bdf5f6327da1 100644 --- a/tests/test/c/createTablePerformance.c +++ b/tests/test/c/createTablePerformance.c @@ -14,13 +14,13 @@ */ #define _DEFAULT_SOURCE +#include "../../../include/client/taos.h" +#include "hash.h" #include "os.h" #include "taoserror.h" -#include "taos.h" +#include "tglobal.h" #include "tulog.h" #include "tutil.h" -#include "tglobal.h" -#include "hash.h" #define MAX_RANDOM_POINTS 20000 #define GREEN "\033[1;32m" diff --git a/tests/test/c/hashIterator.c b/tests/test/c/hashIterator.c index cbd8a0895ebedd4fb0e3c448e4b6865fc0d8a454..0d3dbb64d724c09c135eec5abee1486f05695e88 100644 --- a/tests/test/c/hashIterator.c +++ b/tests/test/c/hashIterator.c @@ -14,11 +14,11 @@ */ #define _DEFAULT_SOURCE +#include "../../../include/client/taos.h" +#include "hash.h" #include "os.h" -#include "taos.h" #include "tulog.h" #include "tutil.h" -#include "hash.h" typedef struct HashTestRow { int32_t keySize; diff --git a/tests/test/c/hashPerformance.c b/tests/test/c/hashPerformance.c index 111ea25a09823fbbeeda3c990d86af849c3821d8..0f14fbff6a3bb491b43e0f96f11136e21e1638ce 100644 --- a/tests/test/c/hashPerformance.c +++ b/tests/test/c/hashPerformance.c @@ -14,11 +14,11 @@ */ #define _DEFAULT_SOURCE +#include "../../../include/client/taos.h" +#include "hash.h" #include "os.h" -#include "taos.h" #include "tulog.h" #include "tutil.h" -#include "hash.h" #define MAX_RANDOM_POINTS 20000 #define GREEN "\033[1;32m" diff --git a/tests/test/c/importOneRow.c b/tests/test/c/importOneRow.c index 858926106c14afa6fb35bb66d44075c04add89f0..510f780163f9c04bca31a3ec97b8e5447d55f182 100644 --- a/tests/test/c/importOneRow.c +++ b/tests/test/c/importOneRow.c @@ -14,12 +14,12 @@ */ #define _DEFAULT_SOURCE +#include "../../../include/client/taos.h" #include "os.h" -#include "taos.h" -#include "tulog.h" +#include "tglobal.h" #include "ttimer.h" +#include "tulog.h" #include "tutil.h" -#include "tglobal.h" #define MAX_RANDOM_POINTS 20000 #define GREEN "\033[1;32m" diff --git a/tests/test/c/importPerTable.c b/tests/test/c/importPerTable.c index 68f78980509d5956f6fc177d7e940af5345b68a7..7aef676c973dee349498a16e5ec87a96d8ca60e1 100644 --- a/tests/test/c/importPerTable.c +++ b/tests/test/c/importPerTable.c @@ -14,13 +14,13 @@ */ #define _DEFAULT_SOURCE +#include "../../../include/client/taos.h" #include "os.h" -#include "taos.h" -#include "tulog.h" +#include "osTime.h" +#include "tglobal.h" #include "ttimer.h" +#include "tulog.h" #include "tutil.h" -#include "tglobal.h" -#include "osTime.h" #define MAX_RANDOM_POINTS 20000 #define GREEN "\033[1;32m" diff --git a/tests/test/c/insertPerRow.c b/tests/test/c/insertPerRow.c index 824c1b925a1b155acfbd22694cf95651ed88f1b4..bdf27180b2c896762339f436aae132eb95feaca4 100644 --- a/tests/test/c/insertPerRow.c +++ b/tests/test/c/insertPerRow.c @@ -14,12 +14,12 @@ */ #define _DEFAULT_SOURCE +#include "../../../include/client/taos.h" #include "os.h" -#include "taos.h" -#include "tulog.h" +#include "tglobal.h" #include "ttimer.h" +#include "tulog.h" #include "tutil.h" -#include "tglobal.h" #define MAX_RANDOM_POINTS 20000 #define GREEN "\033[1;32m" diff --git a/tests/test/c/insertPerTable.c b/tests/test/c/insertPerTable.c index d79df0f4d11e4bc97283f7996cab0d3c26e776e9..ac3c06f0f314d6c5839bf8cfd26419c3988a6ed6 100644 --- a/tests/test/c/insertPerTable.c +++ b/tests/test/c/insertPerTable.c @@ -14,12 +14,12 @@ */ #define _DEFAULT_SOURCE +#include "../../../include/client/taos.h" #include "os.h" -#include "taos.h" -#include "tulog.h" +#include "tglobal.h" #include "ttimer.h" +#include "tulog.h" #include "tutil.h" -#include "tglobal.h" #define MAX_RANDOM_POINTS 20000 #define GREEN "\033[1;32m" diff --git a/tests/test/c/invalidTableId.c b/tests/test/c/invalidTableId.c index 581387a4ee9dc8d72538e175d674bd0fbe83072c..286d0f12919608a6594b3f8fdc33fb863c93b3bd 100644 --- a/tests/test/c/invalidTableId.c +++ b/tests/test/c/invalidTableId.c @@ -14,13 +14,13 @@ */ #define _DEFAULT_SOURCE +#include "../../../include/client/taos.h" +#include "hash.h" #include "os.h" #include "taoserror.h" -#include "taos.h" +#include "tglobal.h" #include "tulog.h" #include "tutil.h" -#include "tglobal.h" -#include "hash.h" #define MAX_RANDOM_POINTS 20000 #define GREEN "\033[1;32m" diff --git a/tests/test/c/queryPerformance.c b/tests/test/c/queryPerformance.c index eda082dd4f293f0879603f6b71cc59150d6cfb3d..afe5cd0ccbf11a2ba8595666614d7faf81817fa3 100644 --- a/tests/test/c/queryPerformance.c +++ b/tests/test/c/queryPerformance.c @@ -14,12 +14,12 @@ */ #define _DEFAULT_SOURCE +#include "../../../include/client/taos.h" #include "os.h" -#include "taos.h" -#include "tulog.h" +#include "tglobal.h" #include "ttimer.h" +#include "tulog.h" #include "tutil.h" -#include "tglobal.h" #define MAX_RANDOM_POINTS 20000 #define GREEN "\033[1;32m" diff --git a/tests/tsim/inc/sim.h b/tests/tsim/inc/sim.h index 2e19dde3d9c52c20705d131f471a2e0e389589e4..39c6f6ac36a80ecf14b0f5a1029f3ef2ce75082e 100644 --- a/tests/tsim/inc/sim.h +++ b/tests/tsim/inc/sim.h @@ -20,7 +20,7 @@ #include #include -#include "taos.h" +#include "../../../include/client/taos.h" #include "tidpool.h" #include "tlog.h" #include "tutil.h" diff --git a/tests/tsim/src/simExe.c b/tests/tsim/src/simExe.c index 397accfea57fec92dbe2b7f9b5c4b730a91e9cbd..7a75dd7d8579537b8778819ec4b43086bdc28dc1 100644 --- a/tests/tsim/src/simExe.c +++ b/tests/tsim/src/simExe.c @@ -14,13 +14,13 @@ */ #define _DEFAULT_SOURCE +#include "../../../include/client/taos.h" +#include "cJSON.h" #include "os.h" #include "sim.h" -#include "taos.h" #include "taoserror.h" #include "tglobal.h" #include "tutil.h" -#include "cJSON.h" #undef TAOS_MEM_CHECK void simLogSql(char *sql, bool useSharp) { diff --git a/tests/tsim/src/simSystem.c b/tests/tsim/src/simSystem.c index 0879e371ef62fee81786728e2b980442567fbaa1..65612930ef3288cd260a795a6f4f037559457593 100644 --- a/tests/tsim/src/simSystem.c +++ b/tests/tsim/src/simSystem.c @@ -14,14 +14,14 @@ */ #define _DEFAULT_SOURCE +#include "../../../include/client/taos.h" #include "os.h" #include "sim.h" -#include "taos.h" +#include "taoserror.h" #include "tglobal.h" +#include "tsocket.h" #include "ttimer.h" #include "tutil.h" -#include "tsocket.h" -#include "taoserror.h" #undef TAOS_MEM_CHECK SScript *simScriptList[MAX_MAIN_SCRIPT_NUM];