diff --git a/.gitmodules b/.gitmodules index 07e4bb2b9c..bc38453f19 100644 --- a/.gitmodules +++ b/.gitmodules @@ -13,3 +13,6 @@ [submodule "examples/rust"] path = examples/rust url = https://github.com/songtianyi/tdengine-rust-bindings.git +[submodule "tools/taos-tools"] + path = tools/taos-tools + url = https://github.com/taosdata/taos-tools diff --git a/CMakeLists.txt b/CMakeLists.txt index df3aab5cb3..9d64966861 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,32 @@ project( DESCRIPTION "An open-source big data platform designed and optimized for the Internet of Things(IOT)" ) +IF ("${BUILD_TOOLS}" STREQUAL "") + IF (TD_LINUX) + IF (TD_ARM_32) + SET(BUILD_TOOLS "false") + ELSEIF (TD_ARM_64) + SET(BUILD_TOOLS "false") + ELSE () + SET(BUILD_TOOLS "false") + ENDIF () + ELSEIF (TD_DARWIN) + SET(BUILD_TOOLS "false") + ELSE () + SET(BUILD_TOOLS "false") + ENDIF () +ENDIF () + +IF ("${BUILD_TOOLS}" MATCHES "false") + MESSAGE("${Yellow} Will _not_ build taos_tools! ${ColourReset}") + SET(TD_TAOS_TOOLS FALSE) +ELSE () + MESSAGE("") + MESSAGE("${Green} Will build taos_tools! ${ColourReset}") + MESSAGE("") + SET(TD_TAOS_TOOLS TRUE) +ENDIF () + set(CMAKE_SUPPORT_DIR "${CMAKE_SOURCE_DIR}/cmake") set(CMAKE_CONTRIB_DIR "${CMAKE_SOURCE_DIR}/contrib") include(${CMAKE_SUPPORT_DIR}/cmake.options) diff --git a/contrib/test/craft/raftMain.c b/contrib/test/craft/raftMain.c index b28adfaaca..12be3deb2e 100644 --- a/contrib/test/craft/raftMain.c +++ b/contrib/test/craft/raftMain.c @@ -377,7 +377,7 @@ void printConf(SRaftServerConfig *pConf) { int main(int argc, char **argv) { - srand(time(NULL)); + taosSeedRand(time(NULL)); int32_t ret; exe_name = argv[0]; diff --git a/contrib/test/craft/simulate_vnode.c b/contrib/test/craft/simulate_vnode.c index 668fe638b7..7ee9b9f8f0 100644 --- a/contrib/test/craft/simulate_vnode.c +++ b/contrib/test/craft/simulate_vnode.c @@ -132,7 +132,7 @@ static void proposeValue(struct raft *r) { buf.base = raft_malloc(buf.len); // mock ts value - int vid = rand() % VNODE_COUNT; + int vid = taosRand() % VNODE_COUNT; snprintf(buf.base, buf.len, "%d:value_%ld", vid, time(NULL)); printf("propose value: %s \n", (char*)buf.base); @@ -174,7 +174,7 @@ void usage() { } int main(int argc, char **argv) { - srand(time(NULL)); + taosSeedRand(time(NULL)); exe_name = argv[0]; if (argc < 2) { diff --git a/examples/c/schemaless.c b/examples/c/schemaless.c index 21f39213cd..99aa361b0a 100644 --- a/examples/c/schemaless.c +++ b/examples/c/schemaless.c @@ -19,7 +19,7 @@ void shuffle(char**lines, size_t n) size_t i; for (i = 0; i < n - 1; i++) { - size_t j = i + rand() / (RAND_MAX / (n - i) + 1); + size_t j = i + taosRand() / (RAND_MAX / (n - i) + 1); char* t = lines[j]; lines[j] = lines[i]; lines[i] = t; diff --git a/include/client/taos.h b/include/client/taos.h index 8b1517c6ff..5da5908779 100644 --- a/include/client/taos.h +++ b/include/client/taos.h @@ -256,6 +256,10 @@ DLL_EXPORT void tmq_conf_set_offset_commit_cb(tmq_conf_t *conf, tmq_co void tmqShowMsg(tmq_message_t *tmq_message); int32_t tmqGetSkipLogNum(tmq_message_t *tmq_message); +typedef void (*TAOS_SUBSCRIBE_CALLBACK)(TAOS_SUB* tsub, TAOS_RES *res, void* param, int code); + +DLL_EXPORT int taos_stmt_affected_rows(TAOS_STMT* stmt); + #ifdef __cplusplus } #endif diff --git a/include/common/taosdef.h b/include/common/taosdef.h index 99360b8d3f..9e5e5ebdcf 100644 --- a/include/common/taosdef.h +++ b/include/common/taosdef.h @@ -63,6 +63,8 @@ typedef enum { extern char *qtypeStr[]; +#define TSDB_PORT_HTTP 11 + #ifdef __cplusplus } #endif diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 238753e5b7..bbb3f6e22d 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -1856,19 +1856,27 @@ typedef enum { TD_TIME_UNIT_MICROSEC = 9, TD_TIME_UNIT_NANOSEC = 10 } ETDTimeUnit; + typedef struct { - uint8_t version; // for compatibility - uint8_t intervalUnit; - uint8_t slidingUnit; - char indexName[TSDB_INDEX_NAME_LEN + 1]; - col_id_t numOfColIds; - uint16_t numOfFuncIds; - uint64_t tableUid; // super/common table uid - int64_t interval; - int64_t sliding; - col_id_t* colIds; // sorted column ids - uint16_t* funcIds; // sorted sma function ids -} STSma; // Time-range-wise SMA + uint16_t funcId; + uint16_t nColIds; + col_id_t* colIds; // sorted colIds +} SFuncColIds; + +typedef struct { + uint8_t version; // for compatibility + uint8_t intervalUnit; + uint8_t slidingUnit; + char indexName[TSDB_INDEX_NAME_LEN]; + char timezone[TD_TIMEZONE_LEN]; + uint16_t nFuncColIds; + uint16_t tagsFilterLen; + tb_uid_t tableUid; // super/common table uid + int64_t interval; + int64_t sliding; + SFuncColIds* funcColIds; // sorted funcIds + char* tagsFilter; +} STSma; // Time-range-wise SMA typedef struct { int64_t ver; // use a general definition @@ -1877,13 +1885,13 @@ typedef struct { typedef struct { int8_t type; // 0 status report, 1 update data - char indexName[TSDB_INDEX_NAME_LEN + 1]; // + char indexName[TSDB_INDEX_NAME_LEN]; // STimeWindow windows; } STSmaMsg; typedef struct { int64_t ver; // use a general definition - char indexName[TSDB_INDEX_NAME_LEN + 1]; + char indexName[TSDB_INDEX_NAME_LEN]; } SVDropTSmaReq; typedef struct { } SVCreateTSmaRsp, SVDropTSmaRsp; @@ -1934,8 +1942,14 @@ typedef struct { static FORCE_INLINE void tdDestroyTSma(STSma* pSma) { if (pSma) { - tfree(pSma->colIds); - tfree(pSma->funcIds); + if (pSma->funcColIds != NULL) { + for (uint16_t i = 0; i < pSma->nFuncColIds; ++i) { + tfree((pSma->funcColIds + i)->colIds); + } + tfree(pSma->funcColIds); + } + + tfree(pSma->tagsFilter); } } @@ -1957,18 +1971,24 @@ static FORCE_INLINE int32_t tEncodeTSma(void** buf, const STSma* pSma) { tlen += taosEncodeFixedU8(buf, pSma->intervalUnit); tlen += taosEncodeFixedU8(buf, pSma->slidingUnit); tlen += taosEncodeString(buf, pSma->indexName); - tlen += taosEncodeFixedU16(buf, pSma->numOfColIds); - tlen += taosEncodeFixedU16(buf, pSma->numOfFuncIds); - tlen += taosEncodeFixedU64(buf, pSma->tableUid); + tlen += taosEncodeString(buf, pSma->timezone); + tlen += taosEncodeFixedU16(buf, pSma->nFuncColIds); + tlen += taosEncodeFixedU16(buf, pSma->tagsFilterLen); + tlen += taosEncodeFixedI64(buf, pSma->tableUid); tlen += taosEncodeFixedI64(buf, pSma->interval); tlen += taosEncodeFixedI64(buf, pSma->sliding); - for (col_id_t i = 0; i < pSma->numOfColIds; ++i) { - tlen += taosEncodeFixedU16(buf, *(pSma->colIds + i)); + for (uint16_t i = 0; i < pSma->nFuncColIds; ++i) { + SFuncColIds* funcColIds = pSma->funcColIds + i; + tlen += taosEncodeFixedU16(buf, funcColIds->funcId); + tlen += taosEncodeFixedU16(buf, funcColIds->nColIds); + for (uint16_t j = 0; j < funcColIds->nColIds; ++j) { + tlen += taosEncodeFixedU16(buf, *(funcColIds->colIds + j)); + } } - for (uint16_t i = 0; i < pSma->numOfFuncIds; ++i) { - tlen += taosEncodeFixedU16(buf, *(pSma->funcIds + i)); + if (pSma->tagsFilterLen > 0) { + tlen += taosEncodeString(buf, pSma->tagsFilter); } return tlen; @@ -1989,34 +2009,52 @@ static FORCE_INLINE void* tDecodeTSma(void* buf, STSma* pSma) { buf = taosDecodeFixedU8(buf, &pSma->intervalUnit); buf = taosDecodeFixedU8(buf, &pSma->slidingUnit); buf = taosDecodeStringTo(buf, pSma->indexName); - buf = taosDecodeFixedU16(buf, &pSma->numOfColIds); - buf = taosDecodeFixedU16(buf, &pSma->numOfFuncIds); - buf = taosDecodeFixedU64(buf, &pSma->tableUid); + buf = taosDecodeStringTo(buf, pSma->timezone); + buf = taosDecodeFixedU16(buf, &pSma->nFuncColIds); + buf = taosDecodeFixedU16(buf, &pSma->tagsFilterLen); + buf = taosDecodeFixedI64(buf, &pSma->tableUid); buf = taosDecodeFixedI64(buf, &pSma->interval); buf = taosDecodeFixedI64(buf, &pSma->sliding); - if (pSma->numOfColIds > 0) { - pSma->colIds = (col_id_t*)calloc(pSma->numOfColIds, sizeof(STSma)); - if (pSma->colIds == NULL) { + if (pSma->nFuncColIds > 0) { + pSma->funcColIds = (SFuncColIds*)calloc(pSma->nFuncColIds, sizeof(SFuncColIds)); + if (pSma->funcColIds == NULL) { + tdDestroyTSma(pSma); return NULL; } - for (uint16_t i = 0; i < pSma->numOfColIds; ++i) { - buf = taosDecodeFixedU16(buf, pSma->colIds + i); + for (uint16_t i = 0; i < pSma->nFuncColIds; ++i) { + SFuncColIds* funcColIds = pSma->funcColIds + i; + buf = taosDecodeFixedU16(buf, &funcColIds->funcId); + buf = taosDecodeFixedU16(buf, &funcColIds->nColIds); + if (funcColIds->nColIds > 0) { + funcColIds->colIds = (col_id_t*)calloc(funcColIds->nColIds, sizeof(col_id_t)); + if (funcColIds->colIds != NULL) { + for (uint16_t j = 0; j < funcColIds->nColIds; ++j) { + buf = taosDecodeFixedU16(buf, funcColIds->colIds + j); + } + } else { + tdDestroyTSma(pSma); + return NULL; + } + } else { + funcColIds->colIds = NULL; + } } } else { - pSma->colIds = NULL; + pSma->funcColIds = NULL; } - if (pSma->numOfFuncIds > 0) { - pSma->funcIds = (uint16_t*)calloc(pSma->numOfFuncIds, sizeof(STSma)); - if (pSma->funcIds == NULL) { + if (pSma->tagsFilterLen > 0) { + pSma->tagsFilter = (char*)calloc(pSma->tagsFilterLen, 1); + if (pSma->tagsFilter != NULL) { + buf = taosDecodeStringTo(buf, pSma->tagsFilter); + } else { + tdDestroyTSma(pSma); return NULL; } - for (uint16_t i = 0; i < pSma->numOfFuncIds; ++i) { - buf = taosDecodeFixedU16(buf, pSma->funcIds + i); - } + } else { - pSma->funcIds = NULL; + pSma->tagsFilter = NULL; } return buf; diff --git a/include/os/osRand.h b/include/os/osRand.h index 422ea92a71..09e1f1b41d 100644 --- a/include/os/osRand.h +++ b/include/os/osRand.h @@ -20,7 +20,16 @@ extern "C" { #endif +// If the error is in a third-party library, place this header file under the third-party library header file. +#ifndef ALLOW_FORBID_FUNC + #define rand RAND_FUNC_TAOS_FORBID + #define srand SRAND_FUNC_TAOS_FORBID + #define rand_r RANDR_FUNC_TAOS_FORBID +#endif + +void taosSeedRand(uint32_t seed); uint32_t taosRand(void); +uint32_t taosRandR(uint32_t *pSeed); void taosRandStr(char* str, int32_t size); uint32_t taosSafeRand(void); diff --git a/include/os/osSleep.h b/include/os/osSleep.h index 686bdd292e..feb9472995 100644 --- a/include/os/osSleep.h +++ b/include/os/osSleep.h @@ -20,7 +20,17 @@ extern "C" { #endif +// If the error is in a third-party library, place this header file under the third-party library header file. +#ifndef ALLOW_FORBID_FUNC + #define Sleep SLEEP_FUNC_TAOS_FORBID + #define sleep SLEEP_FUNC_TAOS_FORBID + #define usleep USLEEP_FUNC_TAOS_FORBID + #define nanosleep NANOSLEEP_FUNC_TAOS_FORBID +#endif + +void taosSsleep(int32_t s); void taosMsleep(int32_t ms); +void taosUsleep(int32_t us); #ifdef __cplusplus } diff --git a/include/util/tdef.h b/include/util/tdef.h index 664588f68f..902ed871f0 100644 --- a/include/util/tdef.h +++ b/include/util/tdef.h @@ -207,7 +207,7 @@ typedef enum ELogicConditionType { #define TSDB_FUNC_TYPE_AGGREGATE 2 #define TSDB_FUNC_MAX_RETRIEVE 1024 -#define TSDB_INDEX_NAME_LEN 32 +#define TSDB_INDEX_NAME_LEN 33 // 32 + 1 '\0' #define TSDB_TYPE_STR_MAX_LEN 32 #define TSDB_TABLE_FNAME_LEN (TSDB_DB_FNAME_LEN + TSDB_TABLE_NAME_LEN + TSDB_NAME_DELIMITER_LEN) #define TSDB_TOPIC_FNAME_LEN TSDB_TABLE_FNAME_LEN diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index 08285c9d26..f5cc034e09 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -208,7 +208,7 @@ void taos_init_imp(void) { atexit(taos_cleanup); errno = TSDB_CODE_SUCCESS; - srand(taosGetTimestampSec()); + taosSeedRand(taosGetTimestampSec()); deltaToUtcInitOnce(); diff --git a/source/client/src/clientHb.c b/source/client/src/clientHb.c index b1e9de8000..88763b8faf 100644 --- a/source/client/src/clientHb.c +++ b/source/client/src/clientHb.c @@ -449,7 +449,7 @@ static void hbStopThread() { } while (2 != atomic_load_8(&clientHbMgr.threadStop)) { - usleep(10); + taosUsleep(10); } tscDebug("hb thread stopped"); diff --git a/source/client/src/clientMain.c b/source/client/src/clientMain.c index 5bcc287116..09a0233e04 100644 --- a/source/client/src/clientMain.c +++ b/source/client/src/clientMain.c @@ -343,3 +343,77 @@ void taos_query_a(TAOS *taos, const char *sql, __taos_async_fn_t fp, void *param void taos_fetch_rows_a(TAOS_RES *res, __taos_async_fn_t fp, void *param) { // TODO } + +TAOS_SUB *taos_subscribe(TAOS *taos, int restart, const char* topic, const char *sql, TAOS_SUBSCRIBE_CALLBACK fp, void *param, int interval) { + // TODO + return NULL; +} + +TAOS_RES *taos_consume(TAOS_SUB *tsub) { + // TODO + return NULL; +} + +void taos_unsubscribe(TAOS_SUB *tsub, int keepProgress) { + // TODO +} + +TAOS_STMT* taos_stmt_init(TAOS* taos) { + // TODO + return NULL; +} + +int taos_stmt_close(TAOS_STMT* stmt) { + // TODO + return -1; +} + +int taos_stmt_execute(TAOS_STMT* stmt) { + // TODO + return -1; +} + +char *taos_stmt_errstr(TAOS_STMT *stmt) { + // TODO + return NULL; +} + +int taos_stmt_affected_rows(TAOS_STMT* stmt) { + // TODO + return -1; +} + +TAOS_RES* taos_schemaless_insert(TAOS* taos, char* lines[], int numLines, int protocol, int precision) { + // TODO + return NULL; +} + +int taos_stmt_bind_param(TAOS_STMT* stmt, TAOS_BIND* bind) { + // TODO + return -1; +} + +int taos_stmt_prepare(TAOS_STMT* stmt, const char* sql, unsigned long length) { + // TODO + return -1; +} + +int taos_stmt_set_tbname_tags(TAOS_STMT* stmt, const char* name, TAOS_BIND* tags) { + // TODO + return -1; +} + +int taos_stmt_set_tbname(TAOS_STMT* stmt, const char* name) { + // TODO + return -1; +} + +int taos_stmt_add_batch(TAOS_STMT* stmt) { + // TODO + return -1; +} + +int taos_stmt_bind_param_batch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind) { + // TODO + return -1; +} diff --git a/source/client/src/tmq.c b/source/client/src/tmq.c index 60103cc9c5..c60d898a9e 100644 --- a/source/client/src/tmq.c +++ b/source/client/src/tmq.c @@ -1146,13 +1146,13 @@ tmq_message_t* tmq_consumer_poll(tmq_t* tmq, int64_t blocking_time) { if (taosArrayGetSize(tmq->clientTopics) == 0) { tscDebug("consumer:%ld poll but not assigned", tmq->consumerId); /*printf("over1\n");*/ - usleep(blocking_time * 1000); + taosMsleep(blocking_time); return NULL; } SMqClientTopic* pTopic = taosArrayGet(tmq->clientTopics, tmq->nextTopicIdx); if (taosArrayGetSize(pTopic->vgs) == 0) { /*printf("over2\n");*/ - usleep(blocking_time * 1000); + taosMsleep(blocking_time); return NULL; } @@ -1165,14 +1165,14 @@ tmq_message_t* tmq_consumer_poll(tmq_t* tmq, int64_t blocking_time) { SMqConsumeReq* pReq = tmqBuildConsumeReqImpl(tmq, blocking_time, pTopic, pVg); if (pReq == NULL) { ASSERT(false); - usleep(blocking_time * 1000); + taosMsleep(blocking_time); return NULL; } SMqPollCbParam* param = malloc(sizeof(SMqPollCbParam)); if (param == NULL) { ASSERT(false); - usleep(blocking_time * 1000); + taosMsleep(blocking_time); return NULL; } param->tmq = tmq; @@ -1204,7 +1204,7 @@ tmq_message_t* tmq_consumer_poll(tmq_t* tmq, int64_t blocking_time) { if (tmq_message == NULL) { if (beginVgIdx == pTopic->nextVgIdx) { - usleep(blocking_time * 1000); + taosMsleep(blocking_time); } else { continue; } diff --git a/source/dnode/vnode/src/inc/tsdbFS.h b/source/dnode/vnode/src/inc/tsdbFS.h index 173e991631..a7275f57fd 100644 --- a/source/dnode/vnode/src/inc/tsdbFS.h +++ b/source/dnode/vnode/src/inc/tsdbFS.h @@ -43,12 +43,30 @@ typedef struct { STsdbFSMeta meta; // FS meta SArray * df; // data file array - // SArray * v2f100.tsma.index_name + // SArray * v2t100.index_name - SArray * smaf; // sma data file array v2f1900.tsma.index_name + SArray * smaf; // sma data file array v2t1900.index_name } SFSStatus; -typedef struct { +/** + * @brief Directory structure of .tsma data files. + * + * root@cary /vnode2/tsdb $ tree .tsma/ + * .tsma/ + * ├── v2t100.index_name_1 + * ├── v2t101.index_name_1 + * ├── v2t102.index_name_1 + * ├── v2t1900.index_name_3 + * ├── v2t1901.index_name_3 + * ├── v2t1902.index_name_3 + * ├── v2t200.index_name_2 + * ├── v2t201.index_name_2 + * └── v2t202.index_name_2 + * + * 0 directories, 9 files + */ + + typedef struct { pthread_rwlock_t lock; SFSStatus *cstatus; // current status diff --git a/source/dnode/vnode/src/inc/tsdbFile.h b/source/dnode/vnode/src/inc/tsdbFile.h index 1034ae015a..d6ce33c389 100644 --- a/source/dnode/vnode/src/inc/tsdbFile.h +++ b/source/dnode/vnode/src/inc/tsdbFile.h @@ -56,10 +56,11 @@ typedef enum { TSDB_FILE_SMAL, // .smal(Block-wise SMA) TSDB_FILE_MAX, // TSDB_FILE_META, // meta - TSDB_FILE_TSMA, // .tsma.${sma_index_name}, Time-range-wise SMA - TSDB_FILE_RSMA, // .rsma.${sma_index_name}, Time-range-wise Rollup SMA -} TSDB_FILE_T; + TSDB_FILE_TSMA, // v2t100.${sma_index_name}, Time-range-wise SMA + TSDB_FILE_RSMA, // v2r100.${sma_index_name}, Time-range-wise Rollup SMA +} E_TSDB_FILE_T; +typedef int32_t TSDB_FILE_T; typedef enum { TSDB_FS_VER_0 = 0, TSDB_FS_VER_MAX, diff --git a/source/dnode/vnode/src/inc/tsdbSma.h b/source/dnode/vnode/src/inc/tsdbSma.h index 6e4ad909ae..1a5ccbdc21 100644 --- a/source/dnode/vnode/src/inc/tsdbSma.h +++ b/source/dnode/vnode/src/inc/tsdbSma.h @@ -31,7 +31,7 @@ int32_t tsdbGetTSmaDataImpl(STsdb *pTsdb, STSma *param, STSmaData *pData, STimeW int32_t tsdbUpdateExpiredWindow(STsdb *pTsdb, char *msg); int32_t tsdbGetTSmaStatus(STsdb *pTsdb, STSma *param, void *result); int32_t tsdbRemoveTSmaData(STsdb *pTsdb, STSma *param, STimeWindow *pWin); -int32_t tsdbFreeSmaState(SSmaStat *pSmaStat); +int32_t tsdbDestroySmaState(SSmaStat *pSmaStat); // internal func diff --git a/source/dnode/vnode/src/tsdb/tsdbFile.c b/source/dnode/vnode/src/tsdb/tsdbFile.c index 00e97c7b61..b756cc9862 100644 --- a/source/dnode/vnode/src/tsdb/tsdbFile.c +++ b/source/dnode/vnode/src/tsdb/tsdbFile.c @@ -23,8 +23,8 @@ static const char *TSDB_FNAME_SUFFIX[] = { "smal", // TSDB_FILE_SMAL "", // TSDB_FILE_MAX "meta", // TSDB_FILE_META - "tsma", // TSDB_FILE_TSMA - "rsma", // TSDB_FILE_RSMA + "sma", // TSDB_FILE_TSMA(directory name) + "sma", // TSDB_FILE_RSMA(directory name) }; static void tsdbGetFilename(int vid, int fid, uint32_t ver, TSDB_FILE_T ftype, char *fname); diff --git a/source/dnode/vnode/src/tsdb/tsdbMain.c b/source/dnode/vnode/src/tsdb/tsdbMain.c index 1b3e00f090..0c82911226 100644 --- a/source/dnode/vnode/src/tsdb/tsdbMain.c +++ b/source/dnode/vnode/src/tsdb/tsdbMain.c @@ -89,7 +89,7 @@ static STsdb *tsdbNew(const char *path, int32_t vgId, const STsdbCfg *pTsdbCfg, static void tsdbFree(STsdb *pTsdb) { if (pTsdb) { tsdbFreeFS(pTsdb->fs); - tsdbFreeSmaState(pTsdb->pSmaStat); + tsdbDestroySmaState(pTsdb->pSmaStat); tfree(pTsdb->path); free(pTsdb); } diff --git a/source/dnode/vnode/src/tsdb/tsdbSma.c b/source/dnode/vnode/src/tsdb/tsdbSma.c index c5f1261282..51abcbd9dd 100644 --- a/source/dnode/vnode/src/tsdb/tsdbSma.c +++ b/source/dnode/vnode/src/tsdb/tsdbSma.c @@ -128,7 +128,7 @@ static SSmaStatItem *tsdbNewSmaStatItem(int8_t state) { return pItem; } -int32_t tsdbFreeSmaState(SSmaStat *pSmaStat) { +int32_t tsdbDestroySmaState(SSmaStat *pSmaStat) { if (pSmaStat) { // TODO: use taosHashSetFreeFp when taosHashSetFreeFp is ready. SSmaStatItem *item = taosHashIterate(pSmaStat->smaStatItems, NULL); @@ -203,6 +203,23 @@ int32_t tsdbUpdateExpiredWindow(STsdb *pTsdb, char *msg) { return TSDB_CODE_SUCCESS; } +static int32_t tsdbResetExpiredWindow(STsdb *pTsdb, const char *indexName, void *timeWindow) { + SSmaStatItem *pItem = NULL; + + if (pTsdb->pSmaStat && pTsdb->pSmaStat->smaStatItems) { + pItem = (SSmaStatItem *)taosHashGet(pTsdb->pSmaStat->smaStatItems, indexName, strlen(indexName)); + } + + if (pItem != NULL) { + // TODO: reset time windows for the sma data blocks + while (true) { + TSKEY thisWindow = 0; + taosHashRemove(pItem->expiredWindows, &thisWindow, sizeof(thisWindow)); + } + } + return TSDB_CODE_SUCCESS; +} + /** * @brief Judge the tSma storage level * @@ -387,7 +404,7 @@ static int32_t tsdbInsertTSmaDataSection(STSmaWriteH *pSmaH, STSmaData *pData, i static int32_t tsdbInitTSmaWriteH(STSmaWriteH *pSmaH, STsdb *pTsdb, STSma *param, STSmaData *pData) { pSmaH->pTsdb = pTsdb; pSmaH->interval = tsdbGetIntervalByPrecision(param->interval, param->intervalUnit, REPO_CFG(pTsdb)->precision); - pSmaH->blockSize = param->numOfFuncIds * sizeof(int64_t); + // pSmaH->blockSize = param->numOfFuncIds * sizeof(int64_t); } static int32_t tsdbSetTSmaDataFile(STSmaWriteH *pSmaH, STSma *param, STSmaData *pData, int32_t storageLevel, @@ -495,6 +512,9 @@ int32_t tsdbInsertTSmaDataImpl(STsdb *pTsdb, STSma *param, STSmaData *pData) { return terrno; } + // reset the SSmaStat + tsdbResetExpiredWindow(pTsdb, param->indexName, &pData->tsWindow); + return TSDB_CODE_SUCCESS; } @@ -542,6 +562,10 @@ int32_t tsdbInsertRSmaDataImpl(STsdb *pTsdb, SRSma *param, STSmaData *pData) { TASSERT(0); return TSDB_CODE_INVALID_PARA; } + + // reset the SSmaStat + tsdbResetExpiredWindow(pTsdb, param->tsma.indexName, &pData->tsWindow); + // Step 4: finish return TSDB_CODE_SUCCESS; } @@ -558,7 +582,7 @@ int32_t tsdbInsertRSmaDataImpl(STsdb *pTsdb, SRSma *param, STSmaData *pData) { static int32_t tsdbInitTSmaReadH(STSmaReadH *pSmaH, STsdb *pTsdb, STSma *param, STSmaData *pData) { pSmaH->pTsdb = pTsdb; pSmaH->interval = tsdbGetIntervalByPrecision(param->interval, param->intervalUnit, REPO_CFG(pTsdb)->precision); - pSmaH->blockSize = param->numOfFuncIds * sizeof(int64_t); + // pSmaH->blockSize = param->numOfFuncIds * sizeof(int64_t); } /** diff --git a/source/dnode/vnode/test/tqMetaTest.cpp b/source/dnode/vnode/test/tqMetaTest.cpp index d3c9b50e4a..4f15185254 100644 --- a/source/dnode/vnode/test/tqMetaTest.cpp +++ b/source/dnode/vnode/test/tqMetaTest.cpp @@ -168,10 +168,10 @@ TEST_F(TqMetaUpdateAppendTest, intxnPersist) { } TEST_F(TqMetaUpdateAppendTest, multiplePage) { - srand(0); + taosSeedRand(0); std::vector v; for (int i = 0; i < 1000; i++) { - v.push_back(rand()); + v.push_back(taosRand()); Foo foo; foo.a = v[i]; tqHandleCopyPut(pMeta, i, &foo, sizeof(Foo)); @@ -202,10 +202,10 @@ TEST_F(TqMetaUpdateAppendTest, multiplePage) { } TEST_F(TqMetaUpdateAppendTest, multipleRewrite) { - srand(0); + taosSeedRand(0); std::vector v; for (int i = 0; i < 1000; i++) { - v.push_back(rand()); + v.push_back(taosRand()); Foo foo; foo.a = v[i]; tqHandleCopyPut(pMeta, i, &foo, sizeof(Foo)); @@ -213,14 +213,14 @@ TEST_F(TqMetaUpdateAppendTest, multipleRewrite) { for (int i = 0; i < 500; i++) { tqHandleCommit(pMeta, i); - v[i] = rand(); + v[i] = taosRand(); Foo foo; foo.a = v[i]; tqHandleCopyPut(pMeta, i, &foo, sizeof(Foo)); } for (int i = 500; i < 1000; i++) { - v[i] = rand(); + v[i] = taosRand(); Foo foo; foo.a = v[i]; tqHandleCopyPut(pMeta, i, &foo, sizeof(Foo)); @@ -235,7 +235,7 @@ TEST_F(TqMetaUpdateAppendTest, multipleRewrite) { ASSERT(pMeta); for (int i = 500; i < 1000; i++) { - v[i] = rand(); + v[i] = taosRand(); Foo foo; foo.a = v[i]; tqHandleCopyPut(pMeta, i, &foo, sizeof(Foo)); @@ -250,10 +250,10 @@ TEST_F(TqMetaUpdateAppendTest, multipleRewrite) { } TEST_F(TqMetaUpdateAppendTest, dupCommit) { - srand(0); + taosSeedRand(0); std::vector v; for (int i = 0; i < 1000; i++) { - v.push_back(rand()); + v.push_back(taosRand()); Foo foo; foo.a = v[i]; tqHandleCopyPut(pMeta, i, &foo, sizeof(Foo)); diff --git a/source/dnode/vnode/test/tsdbSmaTest.cpp b/source/dnode/vnode/test/tsdbSmaTest.cpp index f3a61bdfa4..5a5c5e1530 100644 --- a/source/dnode/vnode/test/tsdbSmaTest.cpp +++ b/source/dnode/vnode/test/tsdbSmaTest.cpp @@ -20,6 +20,7 @@ #include #include +#include #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wwrite-strings" @@ -41,17 +42,20 @@ TEST(testCase, tSmaEncodeDecodeTest) { tSma.slidingUnit = TD_TIME_UNIT_HOUR; tSma.sliding = 0; tstrncpy(tSma.indexName, "sma_index_test", TSDB_INDEX_NAME_LEN); + tstrncpy(tSma.timezone, "Asia/Shanghai", TD_TIMEZONE_LEN); tSma.tableUid = 1234567890; - tSma.numOfColIds = 2; - tSma.numOfFuncIds = 5; // sum/min/max/avg/last - tSma.colIds = (col_id_t *)calloc(tSma.numOfColIds, sizeof(col_id_t)); - tSma.funcIds = (uint16_t *)calloc(tSma.numOfFuncIds, sizeof(uint16_t)); - - for (int32_t i = 0; i < tSma.numOfColIds; ++i) { - *(tSma.colIds + i) = (i + PRIMARYKEY_TIMESTAMP_COL_ID); - } - for (int32_t i = 0; i < tSma.numOfFuncIds; ++i) { - *(tSma.funcIds + i) = (i + 2); + tSma.nFuncColIds = 5; + tSma.funcColIds = (SFuncColIds *)calloc(tSma.nFuncColIds, sizeof(SFuncColIds)); + ASSERT(tSma.funcColIds != NULL); + for (int32_t n = 0; n < tSma.nFuncColIds; ++n) { + SFuncColIds *funcColIds = tSma.funcColIds + n; + funcColIds->funcId = n; + funcColIds->nColIds = 10; + funcColIds->colIds = (col_id_t *)calloc(funcColIds->nColIds, sizeof(col_id_t)); + ASSERT(funcColIds->colIds != NULL); + for (int32_t i = 0; i < funcColIds->nColIds; ++i) { + *(funcColIds->colIds + i) = (i + PRIMARYKEY_TIMESTAMP_COL_ID); + } } STSmaWrapper tSmaWrapper = {.number = 1, .tSma = &tSma}; @@ -80,16 +84,21 @@ TEST(testCase, tSmaEncodeDecodeTest) { EXPECT_EQ(pSma->intervalUnit, qSma->intervalUnit); EXPECT_EQ(pSma->slidingUnit, qSma->slidingUnit); EXPECT_STRCASEEQ(pSma->indexName, qSma->indexName); - EXPECT_EQ(pSma->numOfColIds, qSma->numOfColIds); - EXPECT_EQ(pSma->numOfFuncIds, qSma->numOfFuncIds); + EXPECT_STRCASEEQ(pSma->timezone, qSma->timezone); + EXPECT_EQ(pSma->nFuncColIds, qSma->nFuncColIds); EXPECT_EQ(pSma->tableUid, qSma->tableUid); EXPECT_EQ(pSma->interval, qSma->interval); EXPECT_EQ(pSma->sliding, qSma->sliding); - for (uint32_t j = 0; j < pSma->numOfColIds; ++j) { - EXPECT_EQ(*(col_id_t *)(pSma->colIds + j), *(col_id_t *)(qSma->colIds + j)); - } - for (uint32_t j = 0; j < pSma->numOfFuncIds; ++j) { - EXPECT_EQ(*(uint16_t *)(pSma->funcIds + j), *(uint16_t *)(qSma->funcIds + j)); + EXPECT_EQ(pSma->tagsFilterLen, qSma->tagsFilterLen); + EXPECT_STRCASEEQ(pSma->tagsFilter, qSma->tagsFilter); + for (uint32_t j = 0; j < pSma->nFuncColIds; ++j) { + SFuncColIds *pFuncColIds = pSma->funcColIds + j; + SFuncColIds *qFuncColIds = qSma->funcColIds + j; + EXPECT_EQ(pFuncColIds->funcId, qFuncColIds->funcId); + EXPECT_EQ(pFuncColIds->nColIds, qFuncColIds->nColIds); + for (uint32_t k = 0; k < pFuncColIds->nColIds; ++k) { + EXPECT_EQ(*(pFuncColIds->colIds + k), *(qFuncColIds->colIds + k)); + } } } @@ -99,9 +108,11 @@ TEST(testCase, tSmaEncodeDecodeTest) { } TEST(testCase, tSma_DB_Put_Get_Del_Test) { - const char *smaIndexName1 = "sma_index_test_1"; - const char *smaIndexName2 = "sma_index_test_2"; - const char *smaTestDir = "./smaTest"; + const char * smaIndexName1 = "sma_index_test_1"; + const char * smaIndexName2 = "sma_index_test_2"; + const char * timeZone = "Asia/Shanghai"; + const char * tagsFilter = "I'm tags filter"; + const char * smaTestDir = "./smaTest"; const uint64_t tbUid = 1234567890; const uint32_t nCntTSma = 2; // encode @@ -112,21 +123,27 @@ TEST(testCase, tSma_DB_Put_Get_Del_Test) { tSma.slidingUnit = TD_TIME_UNIT_HOUR; tSma.sliding = 0; tstrncpy(tSma.indexName, smaIndexName1, TSDB_INDEX_NAME_LEN); + tstrncpy(tSma.timezone, timeZone, TD_TIMEZONE_LEN); tSma.tableUid = tbUid; - tSma.numOfColIds = 2; - tSma.numOfFuncIds = 5; // sum/min/max/avg/last - tSma.colIds = (col_id_t *)calloc(tSma.numOfColIds, sizeof(col_id_t)); - tSma.funcIds = (uint16_t *)calloc(tSma.numOfFuncIds, sizeof(uint16_t)); - - for (int32_t i = 0; i < tSma.numOfColIds; ++i) { - *(tSma.colIds + i) = (i + PRIMARYKEY_TIMESTAMP_COL_ID); - } - for (int32_t i = 0; i < tSma.numOfFuncIds; ++i) { - *(tSma.funcIds + i) = (i + 2); + tSma.nFuncColIds = 5; + tSma.funcColIds = (SFuncColIds *)calloc(tSma.nFuncColIds, sizeof(SFuncColIds)); + ASSERT(tSma.funcColIds != NULL); + for (int32_t n = 0; n < tSma.nFuncColIds; ++n) { + SFuncColIds *funcColIds = tSma.funcColIds + n; + funcColIds->funcId = n; + funcColIds->nColIds = 10; + funcColIds->colIds = (col_id_t *)calloc(funcColIds->nColIds, sizeof(col_id_t)); + ASSERT(funcColIds->colIds != NULL); + for (int32_t i = 0; i < funcColIds->nColIds; ++i) { + *(funcColIds->colIds + i) = (i + PRIMARYKEY_TIMESTAMP_COL_ID); + } } + tSma.tagsFilterLen = strlen(tagsFilter); + tSma.tagsFilter = (char *)calloc(tSma.tagsFilterLen + 1, 1); + tstrncpy(tSma.tagsFilter, tagsFilter, tSma.tagsFilterLen + 1); SMeta * pMeta = NULL; - STSma * pSmaCfg = &tSma; + STSma * pSmaCfg = &tSma; const SMetaCfg *pMetaCfg = &defaultMetaOptions; taosRemoveDir(smaTestDir); @@ -151,6 +168,8 @@ TEST(testCase, tSma_DB_Put_Get_Del_Test) { qSmaCfg = metaGetSmaInfoByName(pMeta, smaIndexName1); assert(qSmaCfg != NULL); printf("name1 = %s\n", qSmaCfg->indexName); + printf("timezone1 = %s\n", qSmaCfg->timezone); + printf("tagsFilter1 = %s\n", qSmaCfg->tagsFilter != NULL ? qSmaCfg->tagsFilter : ""); EXPECT_STRCASEEQ(qSmaCfg->indexName, smaIndexName1); EXPECT_EQ(qSmaCfg->tableUid, tSma.tableUid); tdDestroyTSma(qSmaCfg); @@ -159,6 +178,8 @@ TEST(testCase, tSma_DB_Put_Get_Del_Test) { qSmaCfg = metaGetSmaInfoByName(pMeta, smaIndexName2); assert(qSmaCfg != NULL); printf("name2 = %s\n", qSmaCfg->indexName); + printf("timezone2 = %s\n", qSmaCfg->timezone); + printf("tagsFilter2 = %s\n", qSmaCfg->tagsFilter != NULL ? qSmaCfg->tagsFilter : ""); EXPECT_STRCASEEQ(qSmaCfg->indexName, smaIndexName2); EXPECT_EQ(qSmaCfg->interval, tSma.interval); tdDestroyTSma(qSmaCfg); @@ -169,7 +190,7 @@ TEST(testCase, tSma_DB_Put_Get_Del_Test) { assert(pSmaCur != NULL); uint32_t indexCnt = 0; while (1) { - const char* indexName = metaSmaCursorNext(pSmaCur); + const char *indexName = metaSmaCursorNext(pSmaCur); if (indexName == NULL) { break; } @@ -184,10 +205,14 @@ TEST(testCase, tSma_DB_Put_Get_Del_Test) { assert(pSW != NULL); EXPECT_EQ(pSW->number, nCntTSma); EXPECT_STRCASEEQ(pSW->tSma->indexName, smaIndexName1); + EXPECT_STRCASEEQ(pSW->tSma->timezone, timeZone); + EXPECT_STRCASEEQ(pSW->tSma->tagsFilter, tagsFilter); EXPECT_EQ(pSW->tSma->tableUid, tSma.tableUid); EXPECT_STRCASEEQ((pSW->tSma + 1)->indexName, smaIndexName2); + EXPECT_STRCASEEQ((pSW->tSma + 1)->timezone, timeZone); + EXPECT_STRCASEEQ((pSW->tSma + 1)->tagsFilter, tagsFilter); EXPECT_EQ((pSW->tSma + 1)->tableUid, tSma.tableUid); - + tdDestroyTSmaWrapper(pSW); tfree(pSW); @@ -211,9 +236,9 @@ TEST(testCase, tSma_DB_Put_Get_Del_Test) { #if 0 TEST(testCase, tSmaInsertTest) { - STSma tSma = {0}; - STSmaData* pSmaData = NULL; - STsdb tsdb = {0}; + STSma tSma = {0}; + STSmaData *pSmaData = NULL; + STsdb tsdb = {0}; // init tSma.intervalUnit = TD_TIME_UNIT_DAY; @@ -226,7 +251,7 @@ TEST(testCase, tSmaInsertTest) { int32_t dataLen = numOfColIds * numOfBlocks * blockSize; - pSmaData = (STSmaData*)malloc(sizeof(STSmaData) + dataLen); + pSmaData = (STSmaData *)malloc(sizeof(STSmaData) + dataLen); ASSERT_EQ(pSmaData != NULL, true); pSmaData->tableUid = 3232329230; pSmaData->numOfColIds = numOfColIds; @@ -234,7 +259,7 @@ TEST(testCase, tSmaInsertTest) { pSmaData->dataLen = dataLen; pSmaData->tsWindow.skey = 1640000000; pSmaData->tsWindow.ekey = 1645788649; - pSmaData->colIds = (col_id_t*)malloc(sizeof(col_id_t) * numOfColIds); + pSmaData->colIds = (col_id_t *)malloc(sizeof(col_id_t) * numOfColIds); ASSERT_EQ(pSmaData->colIds != NULL, true); for (int32_t i = 0; i < numOfColIds; ++i) { diff --git a/source/libs/catalog/src/catalog.c b/source/libs/catalog/src/catalog.c index 77d25fa164..cd4f5438d5 100644 --- a/source/libs/catalog/src/catalog.c +++ b/source/libs/catalog/src/catalog.c @@ -2635,7 +2635,7 @@ void catalogDestroy(void) { tsem_post(&gCtgMgmt.sem); while (CTG_IS_LOCKED(&gCtgMgmt.lock)) { - usleep(1); + taosUsleep(1); } CTG_LOCK(CTG_WRITE, &gCtgMgmt.lock); diff --git a/source/libs/catalog/test/catalogTests.cpp b/source/libs/catalog/test/catalogTests.cpp index c7867c4da5..cc0e5bb1a9 100644 --- a/source/libs/catalog/test/catalogTests.cpp +++ b/source/libs/catalog/test/catalogTests.cpp @@ -723,7 +723,7 @@ void *ctgTestGetDbVgroupThread(void *param) { } if (ctgTestEnableSleep) { - usleep(rand() % 5); + taosUsleep(taosRand() % 5); } if (++n % ctgTestPrintNum == 0) { printf("Get:%d\n", n); @@ -747,7 +747,7 @@ void *ctgTestSetSameDbVgroupThread(void *param) { } if (ctgTestEnableSleep) { - usleep(rand() % 5); + taosUsleep(taosRand() % 5); } if (++n % ctgTestPrintNum == 0) { printf("Set:%d\n", n); @@ -771,7 +771,7 @@ void *ctgTestSetDiffDbVgroupThread(void *param) { } if (ctgTestEnableSleep) { - usleep(rand() % 5); + taosUsleep(taosRand() % 5); } if (++n % ctgTestPrintNum == 0) { printf("Set:%d\n", n); @@ -801,7 +801,7 @@ void *ctgTestGetCtableMetaThread(void *param) { tfree(tbMeta); if (ctgTestEnableSleep) { - usleep(rand() % 5); + taosUsleep(taosRand() % 5); } if (++n % ctgTestPrintNum == 0) { @@ -838,7 +838,7 @@ void *ctgTestSetCtableMetaThread(void *param) { } if (ctgTestEnableSleep) { - usleep(rand() % 5); + taosUsleep(taosRand() % 5); } if (++n % ctgTestPrintNum == 0) { printf("Set:%d\n", n); @@ -880,7 +880,7 @@ TEST(tableMeta, normalTable) { ASSERT_EQ(vgInfo.epSet.numOfEps, 3); while (0 == ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_DB_NUM)) { - usleep(50000); + taosMsleep(50); } ctgTestSetRspTableMeta(); @@ -901,7 +901,7 @@ TEST(tableMeta, normalTable) { while (true) { uint32_t n = ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_META_NUM); if (0 == n) { - usleep(50000); + taosMsleep(50); } else { break; } @@ -949,7 +949,7 @@ TEST(tableMeta, normalTable) { allDbNum += dbNum; allStbNum += stbNum; - sleep(2); + taosSsleep(2); } ASSERT_EQ(allDbNum, 1); @@ -996,7 +996,7 @@ TEST(tableMeta, childTableCase) { while (true) { uint32_t n = ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_META_NUM); if (0 == n) { - usleep(50000); + taosMsleep(50); } else { break; } @@ -1058,7 +1058,7 @@ TEST(tableMeta, childTableCase) { allDbNum += dbNum; allStbNum += stbNum; - sleep(2); + taosSsleep(2); } ASSERT_EQ(allDbNum, 1); @@ -1105,7 +1105,7 @@ TEST(tableMeta, superTableCase) { while (true) { uint32_t n = ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_META_NUM); if (0 == n) { - usleep(50000); + taosMsleep(50); } else { break; } @@ -1132,7 +1132,7 @@ TEST(tableMeta, superTableCase) { while (true) { uint32_t n = ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_META_NUM); if (2 != n) { - usleep(50000); + taosMsleep(50); } else { break; } @@ -1181,7 +1181,7 @@ TEST(tableMeta, superTableCase) { allDbNum += dbNum; allStbNum += stbNum; - sleep(2); + taosSsleep(2); } ASSERT_EQ(allDbNum, 1); @@ -1230,7 +1230,7 @@ TEST(tableMeta, rmStbMeta) { while (true) { uint32_t n = ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_META_NUM); if (0 == n) { - usleep(50000); + taosMsleep(50); } else { break; } @@ -1244,7 +1244,7 @@ TEST(tableMeta, rmStbMeta) { int32_t n = ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_META_NUM); int32_t m = ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_STB_RENT_NUM); if (n || m) { - usleep(50000); + taosMsleep(50); } else { break; } @@ -1300,7 +1300,7 @@ TEST(tableMeta, updateStbMeta) { while (true) { uint32_t n = ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_META_NUM); if (0 == n) { - usleep(50000); + taosMsleep(50); } else { break; } @@ -1320,7 +1320,7 @@ TEST(tableMeta, updateStbMeta) { uint64_t n = 0; ctgDbgGetStatNum("runtime.qDoneNum", (void *)&n); if (n != 3) { - usleep(50000); + taosMsleep(50); } else { break; } @@ -1392,7 +1392,7 @@ TEST(refreshGetMeta, normal2normal) { if (n > 0) { break; } - usleep(50000); + taosMsleep(50); } STableMeta *tableMeta = NULL; @@ -1410,7 +1410,7 @@ TEST(refreshGetMeta, normal2normal) { tfree(tableMeta); while (0 == ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) { - usleep(50000); + taosMsleep(50); } code = catalogRefreshGetTableMeta(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &tableMeta, 0); @@ -1471,7 +1471,7 @@ TEST(refreshGetMeta, normal2notexist) { if (n > 0) { break; } - usleep(50000); + taosMsleep(50); } STableMeta *tableMeta = NULL; @@ -1489,7 +1489,7 @@ TEST(refreshGetMeta, normal2notexist) { tfree(tableMeta); while (0 == ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) { - usleep(50000); + taosMsleep(50); } code = catalogRefreshGetTableMeta(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &tableMeta, 0); @@ -1545,7 +1545,7 @@ TEST(refreshGetMeta, normal2child) { if (n > 0) { break; } - usleep(50000); + taosMsleep(50); } STableMeta *tableMeta = NULL; @@ -1563,7 +1563,7 @@ TEST(refreshGetMeta, normal2child) { tfree(tableMeta); while (0 == ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) { - usleep(50000); + taosMsleep(50); } code = catalogRefreshGetTableMeta(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &tableMeta, 0); @@ -1629,7 +1629,7 @@ TEST(refreshGetMeta, stable2child) { if (n > 0) { break; } - usleep(50000); + taosMsleep(50); } STableMeta *tableMeta = NULL; @@ -1648,7 +1648,7 @@ TEST(refreshGetMeta, stable2child) { tfree(tableMeta); while (0 == ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) { - usleep(50000); + taosMsleep(50); } ctgTestCurrentSTableName = ctgTestSTablename; @@ -1714,7 +1714,7 @@ TEST(refreshGetMeta, stable2stable) { if (n > 0) { break; } - usleep(50000); + taosMsleep(50); } STableMeta *tableMeta = NULL; @@ -1733,7 +1733,7 @@ TEST(refreshGetMeta, stable2stable) { tfree(tableMeta); while (0 == ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) { - usleep(50000); + taosMsleep(50); } code = catalogRefreshGetTableMeta(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &tableMeta, 0); @@ -1802,7 +1802,7 @@ TEST(refreshGetMeta, child2stable) { if (n > 0) { break; } - usleep(50000); + taosMsleep(50); } STableMeta *tableMeta = NULL; @@ -1819,7 +1819,7 @@ TEST(refreshGetMeta, child2stable) { tfree(tableMeta); while (2 != ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) { - usleep(50000); + taosMsleep(50); } ctgTestCurrentSTableName = ctgTestTablename; @@ -2019,7 +2019,7 @@ TEST(dbVgroup, getSetDbVgroupCase) { if (n > 0) { break; } - usleep(50000); + taosMsleep(50); } code = catalogGetTableHashVgroup(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &vgInfo); @@ -2043,7 +2043,7 @@ TEST(dbVgroup, getSetDbVgroupCase) { uint64_t n = 0; ctgDbgGetStatNum("runtime.qDoneNum", (void *)&n); if (n != 3) { - usleep(50000); + taosMsleep(50); } else { break; } @@ -2100,20 +2100,20 @@ TEST(multiThread, getSetRmSameDbVgroup) { pthread_t thread1, thread2; pthread_create(&(thread1), &thattr, ctgTestSetSameDbVgroupThread, pCtg); - sleep(1); + taosSsleep(1); pthread_create(&(thread2), &thattr, ctgTestGetDbVgroupThread, pCtg); while (true) { if (ctgTestDeadLoop) { - sleep(1); + taosSsleep(1); } else { - sleep(ctgTestMTRunSec); + taosSsleep(ctgTestMTRunSec); break; } } ctgTestStop = true; - sleep(1); + taosSsleep(1); catalogDestroy(); memset(&gCtgMgmt, 0, sizeof(gCtgMgmt)); @@ -2152,20 +2152,20 @@ TEST(multiThread, getSetRmDiffDbVgroup) { pthread_t thread1, thread2; pthread_create(&(thread1), &thattr, ctgTestSetDiffDbVgroupThread, pCtg); - sleep(1); + taosSsleep(1); pthread_create(&(thread2), &thattr, ctgTestGetDbVgroupThread, pCtg); while (true) { if (ctgTestDeadLoop) { - sleep(1); + taosSsleep(1); } else { - sleep(ctgTestMTRunSec); + taosSsleep(ctgTestMTRunSec); break; } } ctgTestStop = true; - sleep(1); + taosSsleep(1); catalogDestroy(); memset(&gCtgMgmt, 0, sizeof(gCtgMgmt)); @@ -2203,20 +2203,20 @@ TEST(multiThread, ctableMeta) { pthread_t thread1, thread2; pthread_create(&(thread1), &thattr, ctgTestSetCtableMetaThread, pCtg); - sleep(1); + taosSsleep(1); pthread_create(&(thread1), &thattr, ctgTestGetCtableMetaThread, pCtg); while (true) { if (ctgTestDeadLoop) { - sleep(1); + taosSsleep(1); } else { - sleep(ctgTestMTRunSec); + taosSsleep(ctgTestMTRunSec); break; } } ctgTestStop = true; - sleep(2); + taosSsleep(2); catalogDestroy(); memset(&gCtgMgmt, 0, sizeof(gCtgMgmt)); @@ -2267,7 +2267,7 @@ TEST(rentTest, allRent) { ASSERT_EQ(tableMeta->tableInfo.rowSize, 12); while (ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_META_NUM) < i) { - usleep(50000); + taosMsleep(50); } code = catalogGetExpiredDBs(pCtg, &dbs, &num); @@ -2292,7 +2292,7 @@ TEST(rentTest, allRent) { } printf("*************************************************\n"); - sleep(2); + taosSsleep(2); } catalogDestroy(); diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index eeb48a1f3d..e30b51cbdf 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -61,7 +61,7 @@ typedef enum SResultTsInterpType { #if 0 static UNUSED_FUNC void *u_malloc (size_t __size) { - uint32_t v = rand(); + uint32_t v = taosRand(); if (v % 1000 <= 0) { return NULL; @@ -71,7 +71,7 @@ static UNUSED_FUNC void *u_malloc (size_t __size) { } static UNUSED_FUNC void* u_calloc(size_t num, size_t __size) { - uint32_t v = rand(); + uint32_t v = taosRand(); if (v % 1000 <= 0) { return NULL; } else { @@ -80,7 +80,7 @@ static UNUSED_FUNC void* u_calloc(size_t num, size_t __size) { } static UNUSED_FUNC void* u_realloc(void* p, size_t __size) { - uint32_t v = rand(); + uint32_t v = taosRand(); if (v % 5 <= 1) { return NULL; } else { diff --git a/source/libs/executor/test/executorTests.cpp b/source/libs/executor/test/executorTests.cpp index ff29d5f355..72ef13124a 100644 --- a/source/libs/executor/test/executorTests.cpp +++ b/source/libs/executor/test/executorTests.cpp @@ -869,7 +869,7 @@ TEST(testCase, external_sort_Test) { #if 0 su* v = static_cast(calloc(1000000, sizeof(su))); for(int32_t i = 0; i < 1000000; ++i) { - v[i].v = rand(); + v[i].v = taosRand(); v[i].c = static_cast(malloc(4)); *(int32_t*) v[i].c = i; } @@ -882,7 +882,7 @@ TEST(testCase, external_sort_Test) { return; #endif - srand(time(NULL)); + taosSeedRand(time(NULL)); SArray* pOrderVal = taosArrayInit(4, sizeof(SOrder)); SOrder o = {0}; @@ -943,7 +943,7 @@ TEST(testCase, external_sort_Test) { } TEST(testCase, sorted_merge_Test) { - srand(time(NULL)); + taosSeedRand(time(NULL)); SArray* pOrderVal = taosArrayInit(4, sizeof(SOrder)); SOrder o = {0}; @@ -1015,7 +1015,7 @@ TEST(testCase, sorted_merge_Test) { } TEST(testCase, time_interval_Operator_Test) { - srand(time(NULL)); + taosSeedRand(time(NULL)); SArray* pOrderVal = taosArrayInit(4, sizeof(SOrder)); SOrder o = {0}; diff --git a/source/libs/executor/test/lhashTests.cpp b/source/libs/executor/test/lhashTests.cpp index 66ef3b0877..88cf713727 100644 --- a/source/libs/executor/test/lhashTests.cpp +++ b/source/libs/executor/test/lhashTests.cpp @@ -25,7 +25,7 @@ #pragma GCC diagnostic ignored "-Wsign-compare" TEST(testCase, linear_hash_Tests) { - srand(time(NULL)); + taosSeedRand(time(NULL)); _hash_fn_t fn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT); #if 0 diff --git a/source/libs/index/test/indexTests.cc b/source/libs/index/test/indexTests.cc index ce3f7fe25e..699c785be5 100644 --- a/source/libs/index/test/indexTests.cc +++ b/source/libs/index/test/indexTests.cc @@ -699,7 +699,7 @@ class IndexObj { for (int i = 0; i < numOfTable; i++) { for (int k = 0; k < 10 && k < colVal.size(); k++) { // opt - tColVal[rand() % colValSize] = 'a' + k % 26; + tColVal[taosRand() % colValSize] = 'a' + k % 26; } SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), tColVal.c_str(), tColVal.size()); diff --git a/source/libs/qcom/test/queryTest.cpp b/source/libs/qcom/test/queryTest.cpp index 9ca7442d55..72ce0f7c37 100644 --- a/source/libs/qcom/test/queryTest.cpp +++ b/source/libs/qcom/test/queryTest.cpp @@ -63,7 +63,7 @@ int main(int argc, char** argv) { TEST(testCase, async_task_test) { SParam* p = (SParam*)calloc(1, sizeof(SParam)); taosAsyncExec(testPrint, p, NULL); - usleep(5000); + taosMsleep(5); } TEST(testCase, many_async_task_test) { @@ -73,14 +73,14 @@ TEST(testCase, many_async_task_test) { taosAsyncExec(testPrint, p, NULL); } - usleep(10000); + taosMsleep(10); } TEST(testCase, error_in_async_test) { int32_t code = 0; SParam* p = (SParam*) calloc(1, sizeof(SParam)); taosAsyncExec(testPrintError, p, &code); - usleep(1000); + taosMsleep(1); printf("Error code:%d after asynchronously exec function\n", code); } diff --git a/source/libs/qworker/test/qworkerTests.cpp b/source/libs/qworker/test/qworkerTests.cpp index 231f0c7fff..2e262abcd0 100644 --- a/source/libs/qworker/test/qworkerTests.cpp +++ b/source/libs/qworker/test/qworkerTests.cpp @@ -266,7 +266,7 @@ int32_t qwtCreateExecTask(void* tsdb, int32_t vgId, struct SSubplan* pPlan, qTas int32_t idx = abs((++qwtTestCaseIdx) % qwtTestCaseNum); qwtTestSinkBlockNum = 0; - qwtTestSinkMaxBlockNum = rand() % 100 + 1; + qwtTestSinkMaxBlockNum = taosRand() % 100 + 1; qwtTestSinkQueryEnd = false; if (0 == idx) { @@ -295,29 +295,29 @@ int32_t qwtExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t *useconds) { } else { if (qwtTestSinkQueryEnd) { *pRes = NULL; - *useconds = rand() % 10; + *useconds = taosRand() % 10; return 0; } - endExec = rand() % 5; + endExec = taosRand() % 5; int32_t runTime = 0; if (qwtTestEnableSleep && qwtTestMaxExecTaskUsec > 0) { - runTime = rand() % qwtTestMaxExecTaskUsec; + runTime = taosRand() % qwtTestMaxExecTaskUsec; } if (qwtTestEnableSleep) { if (runTime) { - usleep(runTime); + taosUsleep(runTime); } } if (endExec) { *pRes = (SSDataBlock*)calloc(1, sizeof(SSDataBlock)); - (*pRes)->info.rows = rand() % 1000; + (*pRes)->info.rows = taosRand() % 1000; } else { *pRes = NULL; - *useconds = rand() % 10; + *useconds = taosRand() % 10; } } @@ -376,7 +376,7 @@ void qwtGetDataLength(DataSinkHandle handle, int32_t* pLen, bool* pQueryEnd) { taosWLockLatch(&qwtTestSinkLock); if (qwtTestSinkBlockNum > 0) { - *pLen = rand() % 100 + 1; + *pLen = taosRand() % 100 + 1; qwtTestSinkBlockNum--; } else { *pLen = 0; @@ -392,7 +392,7 @@ void qwtGetDataLength(DataSinkHandle handle, int32_t* pLen, bool* pQueryEnd) { int32_t qwtGetDataBlock(DataSinkHandle handle, SOutputData* pOutput) { taosWLockLatch(&qwtTestSinkLock); if (qwtTestSinkLastLen > 0) { - pOutput->numOfRows = rand() % 10 + 1; + pOutput->numOfRows = taosRand() % 10 + 1; pOutput->compressed = 1; pOutput->queryEnd = qwtTestSinkQueryEnd; if (qwtTestSinkBlockNum == 0) { @@ -402,7 +402,7 @@ int32_t qwtGetDataBlock(DataSinkHandle handle, SOutputData* pOutput) { } else { pOutput->bufStatus = DS_BUF_FULL; } - pOutput->useconds = rand() % 10 + 1; + pOutput->useconds = taosRand() % 10 + 1; pOutput->precision = 1; } else if (qwtTestSinkLastLen == 0) { pOutput->numOfRows = 0; @@ -416,7 +416,7 @@ int32_t qwtGetDataBlock(DataSinkHandle handle, SOutputData* pOutput) { } else { pOutput->bufStatus = DS_BUF_FULL; } - pOutput->useconds = rand() % 10 + 1; + pOutput->useconds = taosRand() % 10 + 1; pOutput->precision = 1; } else { assert(0); @@ -590,7 +590,7 @@ void *queryThread(void *param) { qwtBuildQueryReqMsg(&queryRpc); qWorkerProcessQueryMsg(mockPointer, mgmt, &queryRpc); if (qwtTestEnableSleep) { - usleep(rand()%5); + taosUsleep(taosRand()%5); } if (++n % qwtTestPrintNum == 0) { printf("query:%d\n", n); @@ -612,7 +612,7 @@ void *readyThread(void *param) { qwtBuildReadyReqMsg(&readyMsg, &readyRpc); code = qWorkerProcessReadyMsg(mockPointer, mgmt, &readyRpc); if (qwtTestEnableSleep) { - usleep(rand()%5); + taosUsleep(taosRand()%5); } if (++n % qwtTestPrintNum == 0) { printf("ready:%d\n", n); @@ -634,7 +634,7 @@ void *fetchThread(void *param) { qwtBuildFetchReqMsg(&fetchMsg, &fetchRpc); code = qWorkerProcessFetchMsg(mockPointer, mgmt, &fetchRpc); if (qwtTestEnableSleep) { - usleep(rand()%5); + taosUsleep(taosRand()%5); } if (++n % qwtTestPrintNum == 0) { printf("fetch:%d\n", n); @@ -656,7 +656,7 @@ void *dropThread(void *param) { qwtBuildDropReqMsg(&dropMsg, &dropRpc); code = qWorkerProcessDropMsg(mockPointer, mgmt, &dropRpc); if (qwtTestEnableSleep) { - usleep(rand()%5); + taosUsleep(taosRand()%5); } if (++n % qwtTestPrintNum == 0) { printf("drop:%d\n", n); @@ -678,7 +678,7 @@ void *statusThread(void *param) { qwtBuildStatusReqMsg(&statusMsg, &statusRpc); code = qWorkerProcessStatusMsg(mockPointer, mgmt, &statusRpc); if (qwtTestEnableSleep) { - usleep(rand()%5); + taosUsleep(taosRand()%5); } if (++n % qwtTestPrintNum == 0) { printf("status:%d\n", n); @@ -696,7 +696,7 @@ void *qwtclientThread(void *param) { void *mockPointer = (void *)0x1; SRpcMsg queryRpc = {0}; - sleep(1); + taosSsleep(1); while (!qwtTestStop) { qwtTestCaseFinished = false; @@ -705,7 +705,7 @@ void *qwtclientThread(void *param) { qwtPutReqToQueue((void *)0x1, &queryRpc); while (!qwtTestCaseFinished) { - usleep(1); + taosUsleep(1); } @@ -748,10 +748,10 @@ void *queryQueueThread(void *param) { if (qwtTestEnableSleep && qwtTestReqMaxDelayUsec > 0) { - int32_t delay = rand() % qwtTestReqMaxDelayUsec; + int32_t delay = taosRand() % qwtTestReqMaxDelayUsec; if (delay) { - usleep(delay); + taosUsleep(delay); } } @@ -804,10 +804,10 @@ void *fetchQueueThread(void *param) { taosWUnLockLatch(&qwtTestFetchQueueLock); if (qwtTestEnableSleep && qwtTestReqMaxDelayUsec > 0) { - int32_t delay = rand() % qwtTestReqMaxDelayUsec; + int32_t delay = taosRand() % qwtTestReqMaxDelayUsec; if (delay) { - usleep(delay); + taosUsleep(delay); } } @@ -963,7 +963,7 @@ TEST(seqTest, randCase) { stubSetRpcSendResponse(); stubSetCreateExecTask(); - srand(time(NULL)); + taosSeedRand(time(NULL)); code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, mockPointer, qwtPutReqToQueue); ASSERT_EQ(code, 0); @@ -971,7 +971,7 @@ TEST(seqTest, randCase) { int32_t t = 0; int32_t maxr = 10001; while (true) { - int32_t r = rand() % maxr; + int32_t r = taosRand() % maxr; if (r >= 0 && r < maxr/5) { printf("Query,%d\n", t++); @@ -982,21 +982,21 @@ TEST(seqTest, randCase) { qwtBuildReadyReqMsg(&readyMsg, &readyRpc); code = qWorkerProcessReadyMsg(mockPointer, mgmt, &readyRpc); if (qwtTestEnableSleep) { - usleep(1); + taosUsleep(1); } } else if (r >= maxr * 2/5 && r < maxr* 3/5) { printf("Fetch,%d\n", t++); qwtBuildFetchReqMsg(&fetchMsg, &fetchRpc); code = qWorkerProcessFetchMsg(mockPointer, mgmt, &fetchRpc); if (qwtTestEnableSleep) { - usleep(1); + taosUsleep(1); } } else if (r >= maxr * 3/5 && r < maxr * 4/5) { printf("Drop,%d\n", t++); qwtBuildDropReqMsg(&dropMsg, &dropRpc); code = qWorkerProcessDropMsg(mockPointer, mgmt, &dropRpc); if (qwtTestEnableSleep) { - usleep(1); + taosUsleep(1); } } else if (r >= maxr * 4/5 && r < maxr-1) { printf("Status,%d\n", t++); @@ -1004,7 +1004,7 @@ TEST(seqTest, randCase) { code = qWorkerProcessStatusMsg(mockPointer, mgmt, &statusRpc); ASSERT_EQ(code, 0); if (qwtTestEnableSleep) { - usleep(1); + taosUsleep(1); } } else { printf("QUIT RAND NOW"); @@ -1025,7 +1025,7 @@ TEST(seqTest, multithreadRand) { stubSetStringToPlan(); stubSetRpcSendResponse(); - srand(time(NULL)); + taosSeedRand(time(NULL)); code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, mockPointer, qwtPutReqToQueue); ASSERT_EQ(code, 0); @@ -1042,15 +1042,15 @@ TEST(seqTest, multithreadRand) { while (true) { if (qwtTestDeadLoop) { - sleep(1); + taosSsleep(1); } else { - sleep(qwtTestMTRunSec); + taosSsleep(qwtTestMTRunSec); break; } } qwtTestStop = true; - sleep(3); + taosSsleep(3); qWorkerDestroy(&mgmt); } @@ -1076,7 +1076,7 @@ TEST(rcTest, shortExecshortDelay) { stubSetPutDataBlock(); stubSetGetDataBlock(); - srand(time(NULL)); + taosSeedRand(time(NULL)); qwtTestStop = false; qwtTestQuitThreadNum = 0; @@ -1099,9 +1099,9 @@ TEST(rcTest, shortExecshortDelay) { while (true) { if (qwtTestDeadLoop) { - sleep(1); + taosSsleep(1); } else { - sleep(qwtTestMTRunSec); + taosSsleep(qwtTestMTRunSec); break; } } @@ -1113,14 +1113,14 @@ TEST(rcTest, shortExecshortDelay) { break; } - sleep(1); + taosSsleep(1); if (qwtTestCaseFinished) { if (qwtTestQuitThreadNum < 3) { tsem_post(&qwtTestQuerySem); tsem_post(&qwtTestFetchSem); - usleep(10); + taosUsleep(10); } } @@ -1157,7 +1157,7 @@ TEST(rcTest, longExecshortDelay) { stubSetPutDataBlock(); stubSetGetDataBlock(); - srand(time(NULL)); + taosSeedRand(time(NULL)); qwtTestStop = false; qwtTestQuitThreadNum = 0; @@ -1180,9 +1180,9 @@ TEST(rcTest, longExecshortDelay) { while (true) { if (qwtTestDeadLoop) { - sleep(1); + taosSsleep(1); } else { - sleep(qwtTestMTRunSec); + taosSsleep(qwtTestMTRunSec); break; } } @@ -1195,14 +1195,14 @@ TEST(rcTest, longExecshortDelay) { break; } - sleep(1); + taosSsleep(1); if (qwtTestCaseFinished) { if (qwtTestQuitThreadNum < 3) { tsem_post(&qwtTestQuerySem); tsem_post(&qwtTestFetchSem); - usleep(10); + taosUsleep(10); } } @@ -1240,7 +1240,7 @@ TEST(rcTest, shortExeclongDelay) { stubSetPutDataBlock(); stubSetGetDataBlock(); - srand(time(NULL)); + taosSeedRand(time(NULL)); qwtTestStop = false; qwtTestQuitThreadNum = 0; @@ -1263,9 +1263,9 @@ TEST(rcTest, shortExeclongDelay) { while (true) { if (qwtTestDeadLoop) { - sleep(1); + taosSsleep(1); } else { - sleep(qwtTestMTRunSec); + taosSsleep(qwtTestMTRunSec); break; } } @@ -1278,14 +1278,14 @@ TEST(rcTest, shortExeclongDelay) { break; } - sleep(1); + taosSsleep(1); if (qwtTestCaseFinished) { if (qwtTestQuitThreadNum < 3) { tsem_post(&qwtTestQuerySem); tsem_post(&qwtTestFetchSem); - usleep(10); + taosUsleep(10); } } @@ -1324,7 +1324,7 @@ TEST(rcTest, dropTest) { stubSetPutDataBlock(); stubSetGetDataBlock(); - srand(time(NULL)); + taosSeedRand(time(NULL)); code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, mockPointer, qwtPutReqToQueue); ASSERT_EQ(code, 0); @@ -1342,15 +1342,15 @@ TEST(rcTest, dropTest) { while (true) { if (qwtTestDeadLoop) { - sleep(1); + taosSsleep(1); } else { - sleep(qwtTestMTRunSec); + taosSsleep(qwtTestMTRunSec); break; } } qwtTestStop = true; - sleep(3); + taosSsleep(3); qWorkerDestroy(&mgmt); } @@ -1358,7 +1358,7 @@ TEST(rcTest, dropTest) { int main(int argc, char** argv) { - srand(time(NULL)); + taosSeedRand(time(NULL)); testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } diff --git a/source/libs/scalar/test/filter/filterTests.cpp b/source/libs/scalar/test/filter/filterTests.cpp index 08210aa2f0..c69a1eb247 100644 --- a/source/libs/scalar/test/filter/filterTests.cpp +++ b/source/libs/scalar/test/filter/filterTests.cpp @@ -1286,7 +1286,7 @@ TEST(scalarModelogicTest, diff_columns_or_and_or) { int main(int argc, char** argv) { - srand(time(NULL)); + taosSeedRand(time(NULL)); testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } diff --git a/source/libs/scalar/test/scalar/scalarTests.cpp b/source/libs/scalar/test/scalar/scalarTests.cpp index faf13f0a82..6518dbec87 100644 --- a/source/libs/scalar/test/scalar/scalarTests.cpp +++ b/source/libs/scalar/test/scalar/scalarTests.cpp @@ -1427,7 +1427,7 @@ TEST(columnTest, greater_and_lower) { int main(int argc, char** argv) { - srand(time(NULL)); + taosSeedRand(time(NULL)); testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } diff --git a/source/libs/scheduler/test/schedulerTests.cpp b/source/libs/scheduler/test/schedulerTests.cpp index 8ed963d875..77353607fa 100644 --- a/source/libs/scheduler/test/schedulerTests.cpp +++ b/source/libs/scheduler/test/schedulerTests.cpp @@ -278,7 +278,7 @@ void *schtSendRsp(void *param) { break; } - usleep(1000); + taosMsleep(1); } pJob = schAcquireJob(job); @@ -303,7 +303,7 @@ void *schtCreateFetchRspThread(void *param) { int64_t job = *(int64_t *)param; SSchJob* pJob = schAcquireJob(job); - sleep(1); + taosSsleep(1); int32_t code = 0; SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)calloc(1, sizeof(SRetrieveTableRsp)); @@ -327,7 +327,7 @@ void *schtFetchRspThread(void *aa) { continue; } - usleep(1); + taosUsleep(1); param = (SSchCallbackParam *)calloc(1, sizeof(*param)); @@ -532,7 +532,7 @@ void* schtRunJobThread(void *aa) { void* schtFreeJobThread(void *aa) { while (!schtTestStop) { - usleep(rand() % 100); + taosUsleep(taosRand() % 100); schtFreeQueryJob(1); } } @@ -701,19 +701,19 @@ TEST(multiThread, forceFree) { while (true) { if (schtTestDeadLoop) { - sleep(1); + taosSsleep(1); } else { - sleep(schtTestMTRunSec); + taosSsleep(schtTestMTRunSec); break; } } schtTestStop = true; - sleep(3); + taosSsleep(3); } int main(int argc, char** argv) { - srand(time(NULL)); + taosSeedRand(time(NULL)); testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } diff --git a/source/libs/sync/src/syncEnv.c b/source/libs/sync/src/syncEnv.c index 6917df1597..fa58bda76f 100644 --- a/source/libs/sync/src/syncEnv.c +++ b/source/libs/sync/src/syncEnv.c @@ -28,7 +28,7 @@ static void doSyncEnvStopTimer(SSyncEnv *pSyncEnv, tmr_h *pTimer); int32_t syncEnvStart() { int32_t ret; - srand(time(NULL)); + taosSeedRand(time(NULL)); gSyncEnv = (SSyncEnv *)malloc(sizeof(SSyncEnv)); assert(gSyncEnv != NULL); ret = doSyncEnvStart(gSyncEnv); diff --git a/source/libs/sync/src/syncIO.c b/source/libs/sync/src/syncIO.c index 2cc56443ce..9d2afb03ca 100644 --- a/source/libs/sync/src/syncIO.c +++ b/source/libs/sync/src/syncIO.c @@ -44,7 +44,7 @@ int32_t syncIOStart(char *host, uint16_t port) { gSyncIO = syncIOCreate(host, port); assert(gSyncIO != NULL); - srand(time(NULL)); + taosSeedRand(time(NULL)); int32_t ret = syncIOStartInternal(gSyncIO); assert(ret == 0); diff --git a/source/libs/sync/src/syncUtil.c b/source/libs/sync/src/syncUtil.c index 736260cafc..b78971bf37 100644 --- a/source/libs/sync/src/syncUtil.c +++ b/source/libs/sync/src/syncUtil.c @@ -95,7 +95,7 @@ void syncUtilbufCopyDeep(const SSyncBuffer* src, SSyncBuffer* dest) { // ---- misc ---- -int32_t syncUtilRand(int32_t max) { return rand() % max; } +int32_t syncUtilRand(int32_t max) { return taosRand() % max; } int32_t syncUtilElectRandomMS() { return ELECT_TIMER_MS_MIN + syncUtilRand(ELECT_TIMER_MS_RANGE); } diff --git a/source/libs/sync/test/syncIOSendMsgClientTest.cpp b/source/libs/sync/test/syncIOSendMsgClientTest.cpp index 83ac724789..250054fd5a 100644 --- a/source/libs/sync/test/syncIOSendMsgClientTest.cpp +++ b/source/libs/sync/test/syncIOSendMsgClientTest.cpp @@ -39,11 +39,11 @@ int main() { rpcMsg.msgType = 77; syncIOSendMsg(gSyncIO->clientRpc, &epSet, &rpcMsg); - sleep(1); + taosSsleep(1); } while (1) { - sleep(1); + taosSsleep(1); } return 0; diff --git a/source/libs/sync/test/syncIOSendMsgServerTest.cpp b/source/libs/sync/test/syncIOSendMsgServerTest.cpp index b0f177962f..1d7402e461 100644 --- a/source/libs/sync/test/syncIOSendMsgServerTest.cpp +++ b/source/libs/sync/test/syncIOSendMsgServerTest.cpp @@ -26,7 +26,7 @@ int main() { assert(ret == 0); while (1) { - sleep(1); + taosSsleep(1); } return 0; diff --git a/source/libs/sync/test/syncIOSendMsgTest.cpp b/source/libs/sync/test/syncIOSendMsgTest.cpp index c25ad3b1dd..ed88fbb03e 100644 --- a/source/libs/sync/test/syncIOSendMsgTest.cpp +++ b/source/libs/sync/test/syncIOSendMsgTest.cpp @@ -39,11 +39,11 @@ int main() { rpcMsg.msgType = 77; syncIOSendMsg(gSyncIO->clientRpc, &epSet, &rpcMsg); - sleep(1); + taosSsleep(1); } while (1) { - sleep(1); + taosSsleep(1); } return 0; diff --git a/source/libs/sync/test/syncIOTickPingTest.cpp b/source/libs/sync/test/syncIOTickPingTest.cpp index 777fc035e2..8be93e6fc0 100644 --- a/source/libs/sync/test/syncIOTickPingTest.cpp +++ b/source/libs/sync/test/syncIOTickPingTest.cpp @@ -29,7 +29,7 @@ int main() { assert(ret == 0); while (1) { - sleep(1); + taosSsleep(1); } return 0; } diff --git a/source/libs/sync/test/syncIOTickQTest.cpp b/source/libs/sync/test/syncIOTickQTest.cpp index 5615058cc3..76f5e33e82 100644 --- a/source/libs/sync/test/syncIOTickQTest.cpp +++ b/source/libs/sync/test/syncIOTickQTest.cpp @@ -29,7 +29,7 @@ int main() { assert(ret == 0); while (1) { - sleep(1); + taosSsleep(1); } return 0; } diff --git a/source/libs/tdb/src/db/tdbUtil.c b/source/libs/tdb/src/db/tdbUtil.c index fe0f3befd6..237a39e47d 100644 --- a/source/libs/tdb/src/db/tdbUtil.c +++ b/source/libs/tdb/src/db/tdbUtil.c @@ -27,7 +27,7 @@ int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique) { ((uint64_t *)fileid)[0] = stDev; ((uint64_t *)fileid)[1] = stIno; if (unique) { - ((uint64_t *)fileid)[2] = rand(); + ((uint64_t *)fileid)[2] = taosRand(); } return 0; diff --git a/source/libs/transport/src/rpcMain.c b/source/libs/transport/src/rpcMain.c index e1319da162..615c576a9b 100644 --- a/source/libs/transport/src/rpcMain.c +++ b/source/libs/transport/src/rpcMain.c @@ -749,7 +749,7 @@ static SRpcConn *rpcAllocateServerConn(SRpcInfo *pRpc, SRecvInfo *pRecv) { memcpy(pConn->user, pHead->user, tListLen(pConn->user)); pConn->pRpc = pRpc; pConn->sid = sid; - pConn->tranId = (uint16_t)(rand() & 0xFFFF); + pConn->tranId = (uint16_t)(taosRand() & 0xFFFF); pConn->ownId = htonl(pConn->sid); pConn->linkUid = pHead->linkUid; if (pRpc->afp) { diff --git a/source/libs/transport/test/pushClient.c b/source/libs/transport/test/pushClient.c index f4babc9980..bdb754ea14 100644 --- a/source/libs/transport/test/pushClient.c +++ b/source/libs/transport/test/pushClient.c @@ -107,7 +107,7 @@ static void *sendRequest(void *param) { tDebug("recv response succefully"); - // usleep(100000000); + // taosSsleep(100); } tError("send and recv sum: %d, %d, %d, %d", u100, u500, u1000, u10000); @@ -223,7 +223,7 @@ int main(int argc, char *argv[]) { } do { - usleep(1); + taosUsleep(1); } while (tcount < appThreads); gettimeofday(&systemTime, NULL); diff --git a/source/libs/transport/test/pushServer.c b/source/libs/transport/test/pushServer.c index c763b7bd8a..f4ad73f743 100644 --- a/source/libs/transport/test/pushServer.c +++ b/source/libs/transport/test/pushServer.c @@ -77,7 +77,7 @@ void processShellMsg() { taosFreeQitem(pRpcMsg); { - // sleep(1); + // taosSsleep(1); SRpcMsg nRpcMsg = {0}; nRpcMsg.pCont = rpcMallocCont(msgSize); nRpcMsg.contLen = msgSize; @@ -176,7 +176,7 @@ int main(int argc, char *argv[]) { tError("failed to start RPC server"); return -1; } - // sleep(5); + // taosSsleep(5); tInfo("RPC server is running, ctrl-c to exit"); diff --git a/source/libs/transport/test/rclient.c b/source/libs/transport/test/rclient.c index 6fc935cb61..f3940e34f9 100644 --- a/source/libs/transport/test/rclient.c +++ b/source/libs/transport/test/rclient.c @@ -84,7 +84,7 @@ static void *sendRequest(void *param) { tDebug("recv response succefully"); - // usleep(100000000); + // taosSsleep(100); } tError("send and recv sum: %d, %d, %d, %d", u100, u500, u1000, u10000); @@ -200,7 +200,7 @@ int main(int argc, char *argv[]) { } do { - usleep(1); + taosUsleep(1); } while (tcount < appThreads); gettimeofday(&systemTime, NULL); diff --git a/source/libs/transport/test/rsclient.c b/source/libs/transport/test/rsclient.c index 26a02eb05b..f2a963b83f 100644 --- a/source/libs/transport/test/rsclient.c +++ b/source/libs/transport/test/rsclient.c @@ -178,7 +178,7 @@ int main(int argc, char *argv[]) { } do { - usleep(1); + taosUsleep(1); } while ( tcount < appThreads); gettimeofday(&systemTime, NULL); diff --git a/source/libs/transport/test/syncClient.c b/source/libs/transport/test/syncClient.c index f43fa7aae6..f84ced5374 100644 --- a/source/libs/transport/test/syncClient.c +++ b/source/libs/transport/test/syncClient.c @@ -85,7 +85,7 @@ static void *sendRequest(void *param) { tDebug("recv response succefully"); - // usleep(100000000); + // taosSsleep(100); } tError("send and recv sum: %d, %d, %d, %d", u100, u500, u1000, u10000); @@ -201,7 +201,7 @@ int main(int argc, char *argv[]) { } do { - usleep(1); + taosUsleep(1); } while (tcount < appThreads); gettimeofday(&systemTime, NULL); diff --git a/source/libs/wal/test/walMetaTest.cpp b/source/libs/wal/test/walMetaTest.cpp index 5bfea9ab5e..f44fc71964 100644 --- a/source/libs/wal/test/walMetaTest.cpp +++ b/source/libs/wal/test/walMetaTest.cpp @@ -300,7 +300,7 @@ TEST_F(WalKeepEnv, readHandleRead) { ASSERT_EQ(code, 0); } for (int i = 0; i < 1000; i++) { - int ver = rand() % 100; + int ver = taosRand() % 100; code = walReadWithHandle(pRead, ver); ASSERT_EQ(code, 0); @@ -352,7 +352,7 @@ TEST_F(WalRetentionEnv, repairMeta1) { ASSERT(pRead != NULL); for (int i = 0; i < 1000; i++) { - int ver = rand() % 100; + int ver = taosRand() % 100; code = walReadWithHandle(pRead, ver); ASSERT_EQ(code, 0); @@ -382,7 +382,7 @@ TEST_F(WalRetentionEnv, repairMeta1) { } for (int i = 0; i < 1000; i++) { - int ver = rand() % 200; + int ver = taosRand() % 200; code = walReadWithHandle(pRead, ver); ASSERT_EQ(code, 0); diff --git a/source/os/src/osEnv.c b/source/os/src/osEnv.c index 0b2fe904b3..63fa600217 100644 --- a/source/os/src/osEnv.c +++ b/source/os/src/osEnv.c @@ -38,7 +38,7 @@ float tsNumOfCores = 0; int64_t tsTotalMemoryKB = 0; void osInit() { - srand(taosSafeRand()); + taosSeedRand(taosSafeRand()); taosGetSystemLocale(tsLocale, tsCharset); taosGetSystemTimezone(tsTimezone); taosSetSystemTimezone(tsTimezone, tsTimezone, &tsDaylight); diff --git a/source/os/src/osRand.c b/source/os/src/osRand.c index b81e41b3cf..f3dd9b74c5 100644 --- a/source/os/src/osRand.c +++ b/source/os/src/osRand.c @@ -12,7 +12,7 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ - +#define ALLOW_FORBID_FUNC #define _DEFAULT_SOURCE #include "os.h" #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) @@ -21,8 +21,12 @@ #include #endif +void taosSeedRand(uint32_t seed) { return srand(seed); } + uint32_t taosRand(void) { return rand(); } +uint32_t taosRandR(uint32_t *pSeed) { return rand_r(pSeed); } + uint32_t taosSafeRand(void) { TdFilePtr pFile; int seed; diff --git a/source/os/src/osSleep.c b/source/os/src/osSleep.c index 3b90fbdad8..724347b0bc 100644 --- a/source/os/src/osSleep.c +++ b/source/os/src/osSleep.c @@ -13,47 +13,35 @@ * along with this program. If not, see . */ +#define ALLOW_FORBID_FUNC #define _DEFAULT_SOURCE #include "os.h" -#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) - -void taosMsleep(int32_t ms) { Sleep(ms); } - -#else +#if !(defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)) #include +#endif -/* - to make taosMsleep work, - signal SIGALRM shall be blocked in the calling thread, - - sigset_t set; - sigemptyset(&set); - sigaddset(&set, SIGALRM); - pthread_sigmask(SIG_BLOCK, &set, NULL); -*/ -void taosMsleep(int32_t mseconds) { -#if 1 - usleep(mseconds * 1000); +void taosSsleep(int32_t s) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + Sleep(1000 * s); #else - struct timeval timeout; - int32_t seconds, useconds; - - seconds = mseconds / 1000; - useconds = (mseconds % 1000) * 1000; - timeout.tv_sec = seconds; - timeout.tv_usec = useconds; - - /* sigset_t set; */ - /* sigemptyset(&set); */ - /* sigaddset(&set, SIGALRM); */ - /* pthread_sigmask(SIG_BLOCK, &set, NULL); */ - - select(0, NULL, NULL, NULL, &timeout); - -/* pthread_sigmask(SIG_UNBLOCK, &set, NULL); */ + sleep(s); #endif } +void taosMsleep(int32_t ms) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + Sleep(ms); +#else + usleep(ms * 1000); #endif +} + +void taosUsleep(int32_t us) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + nanosleep(1000 * us); +#else + usleep(us); +#endif +} diff --git a/source/os/src/osString.c b/source/os/src/osString.c index 88ea4b3e15..0fab7a941e 100644 --- a/source/os/src/osString.c +++ b/source/os/src/osString.c @@ -291,11 +291,10 @@ int32_t twcslen(const wchar_t *wcs) { 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); + for (int32_t i = 0; i < bytes; i += TSDB_NCHAR_SIZE) { + int32_t f1 = *(int32_t *)((char *)f1_ucs4 + i); + int32_t f2 = *(int32_t *)((char *)f2_ucs4 + i); if ((f1 == 0 && f2 != 0) || (f1 != 0 && f2 == 0)) { return f1 - f2; diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index 02d7e6c0e9..b24a20d8c0 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -16,15 +16,6 @@ #define _DEFAULT_SOURCE #include "os.h" -bool taosCheckSystemIsSmallEnd() { - union check{ - int16_t i; - char ch[2]; - }c; - c.i=1; - return c.ch[0]==1; -} - #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) /* @@ -48,115 +39,6 @@ bool taosCheckSystemIsSmallEnd() { #include #pragma warning(pop) -int32_t taosGetTotalMemory(int64_t *totalKB) { - MEMORYSTATUSEX memsStat; - memsStat.dwLength = sizeof(memsStat); - if (!GlobalMemoryStatusEx(&memsStat)) { - return -1; - } - - *totalKB = memsStat.ullTotalPhys / 1024; - return 0; -} - -int32_t taosGetSysMemory(int64_t *usedKB) { - MEMORYSTATUSEX memsStat; - memsStat.dwLength = sizeof(memsStat); - if (!GlobalMemoryStatusEx(&memsStat)) { - return -1; - } - - int64_t nMemFree = memsStat.ullAvailPhys / 1024; - int64_t nMemTotal = memsStat.ullTotalPhys / 1024.0; - - *usedKB = nMemTotal - nMemFree; - return 0; -} - -int32_t taosGetProcMemory(int64_t *usedKB) { - 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 - - *usedKB = bytes_used / 1024; - return 0; -} - -int32_t taosGetCpuCores(float *numOfCores) { - SYSTEM_INFO info; - GetSystemInfo(&info); - *numOfCores = info.dwNumberOfProcessors; - return 0; -} - -int32_t taosGetCpuUsage(double *sysCpuUsage, double *procCpuUsage) { - *sysCpuUsage = 0; - *procCpuUsage = 0; - return 0; -} - -int32_t taosGetDiskSize(char *dataDir, SDiskSize *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; - } -} - -int32_t taosGetCardInfo(int64_t *receive_bytes, int64_t *transmit_bytes) { - *receive_bytes = 0; - *transmit_bytes = 0; - return 0; -} - -int32_t taosGetProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes) { - IO_COUNTERS io_counter; - if (GetProcessIoCounters(GetCurrentProcess(), &io_counter)) { - if (rchars) *rchars = io_counter.ReadTransferCount; - if (wchars) *wchars = io_counter.WriteTransferCount; - if (read_bytes) *read_bytes = 0; - if (write_bytes) *write_bytes = 0; - return 0; - } - return -1; -} - -void taosGetSystemInfo() { - taosGetCpuCores(&tsNumOfCores); - taosGetTotalMemory(&tsTotalMemoryKB); - - double tmp1, tmp2, tmp3, tmp4; - taosGetCpuUsage(&tmp1, &tmp2); -} - -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, @@ -193,124 +75,13 @@ LONG WINAPI FlCrashDump(PEXCEPTION_POINTERS ep) { return EXCEPTION_CONTINUE_SEARCH; } -void taosSetCoreDump() { SetUnhandledExceptionFilter(&FlCrashDump); } - -int32_t taosGetSystemUUID(char *uid, int32_t uidlen) { - 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 0; -} - -char *taosGetCmdlineByPID(int pid) { return ""; } - #elif defined(_TD_DARWIN_64) -/* - * darwin implementation - */ - #include #include -void taosKillSystem() { - // printf("function taosKillSystem, exit!"); - exit(0); -} - -int32_t taosGetCpuCores(float *numOfCores) { - *numOfCores = sysconf(_SC_NPROCESSORS_ONLN); - return 0; -} - -void taosGetSystemInfo() { - long physical_pages = sysconf(_SC_PHYS_PAGES); - long page_size = sysconf(_SC_PAGESIZE); - tsTotalMemoryKB = physical_pages * page_size / 1024; - tsPageSizeKB = page_size / 1024; - tsNumOfCores = sysconf(_SC_NPROCESSORS_ONLN); -} - -int32_t taosGetProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes) { - if (rchars) *rchars = 0; - if (wchars) *wchars = 0; - if (read_bytes) *read_bytes = 0; - if (write_bytes) *write_bytes = 0; - return 0; -} - -int32_t taosGetCardInfo(int64_t *receive_bytes, int64_t *transmit_bytes) { - *receive_bytes = 0; - *transmit_bytes = 0; - return 0; -} - -int32_t taosGetCpuUsage(double *sysCpuUsage, double *procCpuUsage) { - *sysCpuUsage = 0; - *procCpuUsage = 0; - return 0; -} - -int32_t taosGetProcMemory(int64_t *usedKB) { - *usedKB = 0; - return 0; -} - -int32_t taosGetSysMemory(int64_t *usedKB) { - *usedKB = 0; - return 0; -} - -int taosSystem(const char *cmd) { - // printf("un support funtion"); - return -1; -} - -void taosSetCoreDump() {} - -int32_t taosGetDiskSize(char *dataDir, SDiskSize *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; - } -} - -int32_t taosGetSystemUUID(char *uid, int32_t uidlen) { - 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 0; -} - -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 @@ -354,49 +125,6 @@ static void taosGetProcIOnfos() { snprintf(tsProcIOFile, sizeof(tsProcIOFile), "/proc/%d/io", tsProcId); } -int32_t taosGetTotalMemory(int64_t *totalKB) { - *totalKB = (int64_t)(sysconf(_SC_PHYS_PAGES) * tsPageSizeKB); - return 0; -} - -int32_t taosGetSysMemory(int64_t *usedKB) { - *usedKB = sysconf(_SC_AVPHYS_PAGES) * tsPageSizeKB; - return 0; -} - -int32_t taosGetProcMemory(int64_t *usedKB) { - TdFilePtr pFile = taosOpenFile(tsProcMemFile, TD_FILE_READ | TD_FILE_STREAM); - if (pFile == NULL) { - // printf("open file:%s failed", tsProcMemFile); - return -1; - } - - ssize_t _bytes = 0; - char *line = NULL; - while (!taosEOFFile(pFile)) { - _bytes = taosGetLineFile(pFile, &line); - if ((_bytes < 0) || (line == NULL)) { - break; - } - if (strstr(line, "VmRSS:") != NULL) { - break; - } - } - - if (line == NULL) { - // printf("read file:%s failed", tsProcMemFile); - taosCloseFile(&pFile); - return -1; - } - - char tmp[10]; - sscanf(line, "%s %" PRId64, tmp, usedKB); - - if (line != NULL) tfree(line); - taosCloseFile(&pFile); - return 0; -} - static int32_t taosGetSysCpuInfo(SysCpuInfo *cpuInfo) { TdFilePtr pFile = taosOpenFile(tsSysCpuFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { @@ -450,12 +178,210 @@ static int32_t taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) { return 0; } +#endif + +bool taosCheckSystemIsSmallEnd() { + union check{ + int16_t i; + char ch[2]; + }c; + c.i=1; + return c.ch[0]==1; +} + +void taosGetSystemInfo() { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + taosGetCpuCores(&tsNumOfCores); + taosGetTotalMemory(&tsTotalMemoryKB); + + double tmp1, tmp2, tmp3, tmp4; + taosGetCpuUsage(&tmp1, &tmp2); +#elif defined(_TD_DARWIN_64) + long physical_pages = sysconf(_SC_PHYS_PAGES); + long page_size = sysconf(_SC_PAGESIZE); + tsTotalMemoryKB = physical_pages * page_size / 1024; + tsPageSizeKB = page_size / 1024; + tsNumOfCores = sysconf(_SC_NPROCESSORS_ONLN); +#else + taosGetProcIOnfos(); + taosGetCpuCores(&tsNumOfCores); + taosGetTotalMemory(&tsTotalMemoryKB); + + double tmp1, tmp2, tmp3, tmp4; + taosGetCpuUsage(&tmp1, &tmp2); +#endif +} + +int32_t taosGetEmail(char *email, int32_t maxLen) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +#elif defined(_TD_DARWIN_64) + const char *filepath = "/usr/local/taos/email"; + + TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ); + if (pFile == NULL) return false; + + if (taosReadFile(pFile, (void *)email, maxLen) < 0) { + taosCloseFile(&pFile); + return -1; + } + + taosCloseFile(&pFile); + return 0; +#else + const char *filepath = "/usr/local/taos/email"; + + TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ); + if (pFile == NULL) return false; + + if (taosReadFile(pFile, (void *)email, maxLen) < 0) { + taosCloseFile(&pFile); + return -1; + } + + taosCloseFile(&pFile); + return 0; +#endif +} + +int32_t taosGetOsReleaseName(char *releaseName, int32_t maxLen) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +#elif defined(_TD_DARWIN_64) + char *line = NULL; + size_t size = 0; + int32_t code = -1; + + TdFilePtr pFile = taosOpenFile("/etc/os-release", TD_FILE_READ | TD_FILE_STREAM); + if (pFile == NULL) return false; + + while ((size = taosGetLineFile(pFile, &line)) != -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; + } + tstrncpy(releaseName, p, maxLen); + code = 0; + break; + } + } + + if (line != NULL) free(line); + taosCloseFile(&pFile); + return code; +#else + char *line = NULL; + size_t size = 0; + int32_t code = -1; + + TdFilePtr pFile = taosOpenFile("/etc/os-release", TD_FILE_READ | TD_FILE_STREAM); + if (pFile == NULL) return false; + + while ((size = taosGetLineFile(pFile, &line)) != -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; + } + tstrncpy(releaseName, p, maxLen); + code = 0; + break; + } + } + + if (line != NULL) free(line); + taosCloseFile(&pFile); + return code; +#endif +} + +int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +#elif defined(_TD_DARWIN_64) + char *line = NULL; + size_t size = 0; + int32_t done = 0; + int32_t code = -1; + + TdFilePtr pFile = taosOpenFile("/proc/cpuinfo", TD_FILE_READ | TD_FILE_STREAM); + if (pFile == NULL) return false; + + while (done != 3 && (size = taosGetLineFile(pFile, &line)) != -1) { + line[size - 1] = '\0'; + if (((done & 1) == 0) && strncmp(line, "model name", 10) == 0) { + const char *v = strchr(line, ':') + 2; + tstrncpy(cpuModel, v, maxLen); + code = 0; + done |= 1; + } else if (((done & 2) == 0) && strncmp(line, "cpu cores", 9) == 0) { + const char *v = strchr(line, ':') + 2; + *numOfCores = atof(v); + done |= 2; + } + } + + if (line != NULL) free(line); + taosCloseFile(&pFile); + + return code; +#else + char *line = NULL; + size_t size = 0; + int32_t done = 0; + int32_t code = -1; + + TdFilePtr pFile = taosOpenFile("/proc/cpuinfo", TD_FILE_READ | TD_FILE_STREAM); + if (pFile == NULL) return false; + + while (done != 3 && (size = taosGetLineFile(pFile, &line)) != -1) { + line[size - 1] = '\0'; + if (((done & 1) == 0) && strncmp(line, "model name", 10) == 0) { + const char *v = strchr(line, ':') + 2; + tstrncpy(cpuModel, v, maxLen); + code = 0; + done |= 1; + } else if (((done & 2) == 0) && strncmp(line, "cpu cores", 9) == 0) { + const char *v = strchr(line, ':') + 2; + *numOfCores = atof(v); + done |= 2; + } + } + + if (line != NULL) free(line); + taosCloseFile(&pFile); + + return code; +#endif +} + int32_t taosGetCpuCores(float *numOfCores) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + SYSTEM_INFO info; + GetSystemInfo(&info); + *numOfCores = info.dwNumberOfProcessors; + return 0; +#elif defined(_TD_DARWIN_64) *numOfCores = sysconf(_SC_NPROCESSORS_ONLN); return 0; +#else + *numOfCores = sysconf(_SC_NPROCESSORS_ONLN); + return 0; +#endif } int32_t taosGetCpuUsage(double *cpu_system, double *cpu_engine) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + *sysCpuUsage = 0; + *procCpuUsage = 0; + return 0; +#elif defined(_TD_DARWIN_64) + *sysCpuUsage = 0; + *procCpuUsage = 0; + return 0; +#else static uint64_t lastSysUsed = 0; static uint64_t lastSysTotal = 0; static uint64_t lastProcTotal = 0; @@ -492,9 +418,132 @@ int32_t taosGetCpuUsage(double *cpu_system, double *cpu_engine) { lastProcTotal = curProcTotal; return 0; +#endif +} + +int32_t taosGetTotalMemory(int64_t *totalKB) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + MEMORYSTATUSEX memsStat; + memsStat.dwLength = sizeof(memsStat); + if (!GlobalMemoryStatusEx(&memsStat)) { + return -1; + } + + *totalKB = memsStat.ullTotalPhys / 1024; + return 0; +#elif defined(_TD_DARWIN_64) + return 0; +#else + *totalKB = (int64_t)(sysconf(_SC_PHYS_PAGES) * tsPageSizeKB); + return 0; +#endif +} + +int32_t taosGetProcMemory(int64_t *usedKB) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + 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 + + *usedKB = bytes_used / 1024; + return 0; +#elif defined(_TD_DARWIN_64) + *usedKB = 0; + return 0; +#else + TdFilePtr pFile = taosOpenFile(tsProcMemFile, TD_FILE_READ | TD_FILE_STREAM); + if (pFile == NULL) { + // printf("open file:%s failed", tsProcMemFile); + return -1; + } + + ssize_t _bytes = 0; + char *line = NULL; + while (!taosEOFFile(pFile)) { + _bytes = taosGetLineFile(pFile, &line); + if ((_bytes < 0) || (line == NULL)) { + break; + } + if (strstr(line, "VmRSS:") != NULL) { + break; + } + } + + if (line == NULL) { + // printf("read file:%s failed", tsProcMemFile); + taosCloseFile(&pFile); + return -1; + } + + char tmp[10]; + sscanf(line, "%s %" PRId64, tmp, usedKB); + + if (line != NULL) tfree(line); + taosCloseFile(&pFile); + return 0; +#endif +} + +int32_t taosGetSysMemory(int64_t *usedKB) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + MEMORYSTATUSEX memsStat; + memsStat.dwLength = sizeof(memsStat); + if (!GlobalMemoryStatusEx(&memsStat)) { + return -1; + } + + int64_t nMemFree = memsStat.ullAvailPhys / 1024; + int64_t nMemTotal = memsStat.ullTotalPhys / 1024.0; + + *usedKB = nMemTotal - nMemFree; + return 0; +#elif defined(_TD_DARWIN_64) + *usedKB = 0; + return 0; +#else + *usedKB = sysconf(_SC_AVPHYS_PAGES) * tsPageSizeKB; + return 0; +#endif } int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + 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; + } +#elif defined(_TD_DARWIN_64) + 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; + } +#else struct statvfs info; if (statvfs(dataDir, &info)) { return -1; @@ -504,9 +553,79 @@ int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) { diskSize->used = diskSize->total - diskSize->avail; return 0; } +#endif +} + +int32_t taosGetProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + IO_COUNTERS io_counter; + if (GetProcessIoCounters(GetCurrentProcess(), &io_counter)) { + if (rchars) *rchars = io_counter.ReadTransferCount; + if (wchars) *wchars = io_counter.WriteTransferCount; + if (read_bytes) *read_bytes = 0; + if (write_bytes) *write_bytes = 0; + return 0; + } + return -1; +#elif defined(_TD_DARWIN_64) + if (rchars) *rchars = 0; + if (wchars) *wchars = 0; + if (read_bytes) *read_bytes = 0; + if (write_bytes) *write_bytes = 0; + return 0; +#else + TdFilePtr pFile = taosOpenFile(tsProcIOFile, TD_FILE_READ | TD_FILE_STREAM); + if (pFile == NULL) return -1; + + ssize_t _bytes = 0; + char *line = NULL; + char tmp[24]; + int readIndex = 0; + + while (!taosEOFFile(pFile)) { + _bytes = taosGetLineFile(pFile, &line); + if (_bytes < 10 || 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 (strstr(line, "read_bytes:") != NULL) { // read_bytes + sscanf(line, "%s %" PRId64, tmp, read_bytes); + readIndex++; + } else if (strstr(line, "write_bytes:") != NULL) { // write_bytes + sscanf(line, "%s %" PRId64, tmp, write_bytes); + readIndex++; + } else { + } + + if (readIndex >= 4) break; + } + + if (line != NULL) tfree(line); + taosCloseFile(&pFile); + + if (readIndex < 4) { + return -1; + } + + return 0; +#endif } int32_t taosGetCardInfo(int64_t *receive_bytes, int64_t *transmit_bytes) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + *receive_bytes = 0; + *transmit_bytes = 0; + return 0; +#elif defined(_TD_DARWIN_64) + *receive_bytes = 0; + *transmit_bytes = 0; + return 0; +#else TdFilePtr pFile = taosOpenFile(tsSysNetFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) return -1; @@ -549,66 +668,17 @@ int32_t taosGetCardInfo(int64_t *receive_bytes, int64_t *transmit_bytes) { taosCloseFile(&pFile); return 0; -} - -int32_t taosGetProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes) { - TdFilePtr pFile = taosOpenFile(tsProcIOFile, TD_FILE_READ | TD_FILE_STREAM); - if (pFile == NULL) return -1; - - ssize_t _bytes = 0; - char *line = NULL; - char tmp[24]; - int readIndex = 0; - - while (!taosEOFFile(pFile)) { - _bytes = taosGetLineFile(pFile, &line); - if (_bytes < 10 || 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 (strstr(line, "read_bytes:") != NULL) { // read_bytes - sscanf(line, "%s %" PRId64, tmp, read_bytes); - readIndex++; - } else if (strstr(line, "write_bytes:") != NULL) { // write_bytes - sscanf(line, "%s %" PRId64, tmp, write_bytes); - readIndex++; - } else { - } - - if (readIndex >= 4) break; - } - - if (line != NULL) tfree(line); - taosCloseFile(&pFile); - - if (readIndex < 4) { - return -1; - } - - return 0; -} - -void taosGetSystemInfo() { - taosGetProcIOnfos(); - taosGetCpuCores(&tsNumOfCores); - taosGetTotalMemory(&tsTotalMemoryKB); - - double tmp1, tmp2, tmp3, tmp4; - taosGetCpuUsage(&tmp1, &tmp2); -} - -void taosKillSystem() { - // SIGINT - // printf("taosd will shut down soon"); - kill(tsProcId, 2); +#endif } int taosSystem(const char *cmd) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + printf("taosSystem not support"); + return -1; +#elif defined(_TD_DARWIN_64) + printf("no support funtion"); + return -1; +#else FILE *fp; int res; char buf[1024]; @@ -633,9 +703,100 @@ int taosSystem(const char *cmd) { return res; } +#endif +} + +void taosKillSystem() { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + printf("function taosKillSystem, exit!"); + exit(0); +#elif defined(_TD_DARWIN_64) + printf("function taosKillSystem, exit!"); + exit(0); +#else + // SIGINT + printf("taosd will shut down soon"); + kill(tsProcId, 2); +#endif +} + +int32_t taosGetSystemUUID(char *uid, int32_t uidlen) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + 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 0; +#elif defined(_TD_DARWIN_64) + 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 0; +#else + int len = 0; + + // fd = open("/proc/sys/kernel/random/uuid", 0); + TdFilePtr pFile = taosOpenFile("/proc/sys/kernel/random/uuid", TD_FILE_READ); + if (pFile == NULL) { + return -1; + } else { + len = taosReadFile(pFile, uid, uidlen); + taosCloseFile(&pFile); + } + + if (len >= 36) { + uid[36] = 0; + return 0; + } + + return 0; +#endif +} + +char *taosGetCmdlineByPID(int pid) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + return ""; +#elif defined(_TD_DARWIN_64) + 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 + static char cmdline[1024]; + sprintf(cmdline, "/proc/%d/cmdline", pid); + + // int fd = open(cmdline, O_RDONLY); + TdFilePtr pFile = taosOpenFile(cmdline, TD_FILE_READ); + if (pFile != NULL) { + int n = taosReadFile(pFile, cmdline, sizeof(cmdline) - 1); + if (n < 0) n = 0; + + if (n > 0 && cmdline[n - 1] == '\n') --n; + + cmdline[n] = 0; + + taosCloseFile(&pFile); + } else { + cmdline[0] = 0; + } + + return cmdline; +#endif } void taosSetCoreDump(bool enable) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + SetUnhandledExceptionFilter(&FlCrashDump); +#elif defined(_TD_DARWIN_64) +#else if (!enable) return; // 1. set ulimit -c unlimited @@ -708,55 +869,12 @@ void taosSetCoreDump(bool enable) { // printf("The new core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid); #endif -} - -int32_t taosGetSystemUUID(char *uid, int32_t uidlen) { - int len = 0; - - // fd = open("/proc/sys/kernel/random/uuid", 0); - TdFilePtr pFile = taosOpenFile("/proc/sys/kernel/random/uuid", TD_FILE_READ); - if (pFile == NULL) { - return -1; - } else { - len = taosReadFile(pFile, uid, uidlen); - taosCloseFile(&pFile); - } - - if (len >= 36) { - uid[36] = 0; - return 0; - } - - return 0; -} - -char *taosGetCmdlineByPID(int pid) { - static char cmdline[1024]; - sprintf(cmdline, "/proc/%d/cmdline", pid); - - // int fd = open(cmdline, O_RDONLY); - TdFilePtr pFile = taosOpenFile(cmdline, TD_FILE_READ); - if (pFile != NULL) { - int n = taosReadFile(pFile, cmdline, sizeof(cmdline) - 1); - if (n < 0) n = 0; - - if (n > 0 && cmdline[n - 1] == '\n') --n; - - cmdline[n] = 0; - - taosCloseFile(&pFile); - } else { - cmdline[0] = 0; - } - - return cmdline; -} - #endif - -#if !(defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)) +} SysNameInfo taosGetSysNameInfo() { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +#elif defined(_TD_DARWIN_64) SysNameInfo info = {0}; struct utsname uts; @@ -769,77 +887,18 @@ SysNameInfo taosGetSysNameInfo() { } return info; -} +#else + SysNameInfo info = {0}; -int32_t taosGetEmail(char *email, int32_t maxLen) { - const char *filepath = "/usr/local/taos/email"; - - TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ); - if (pFile == NULL) return false; - - if (taosReadFile(pFile, (void *)email, maxLen) < 0) { - taosCloseFile(&pFile); - return -1; + struct utsname uts; + if (!uname(&uts)) { + tstrncpy(info.sysname, uts.sysname, sizeof(info.sysname)); + tstrncpy(info.nodename, uts.nodename, sizeof(info.nodename)); + tstrncpy(info.release, uts.release, sizeof(info.release)); + tstrncpy(info.version, uts.version, sizeof(info.version)); + tstrncpy(info.machine, uts.machine, sizeof(info.machine)); } - taosCloseFile(&pFile); - return 0; -} - -int32_t taosGetOsReleaseName(char *releaseName, int32_t maxLen) { - char *line = NULL; - size_t size = 0; - int32_t code = -1; - - TdFilePtr pFile = taosOpenFile("/etc/os-release", TD_FILE_READ | TD_FILE_STREAM); - if (pFile == NULL) return false; - - while ((size = taosGetLineFile(pFile, &line)) != -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; - } - tstrncpy(releaseName, p, maxLen); - code = 0; - break; - } - } - - if (line != NULL) free(line); - taosCloseFile(&pFile); - return code; -} - -int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores) { - char *line = NULL; - size_t size = 0; - int32_t done = 0; - int32_t code = -1; - - TdFilePtr pFile = taosOpenFile("/proc/cpuinfo", TD_FILE_READ | TD_FILE_STREAM); - if (pFile == NULL) return false; - - while (done != 3 && (size = taosGetLineFile(pFile, &line)) != -1) { - line[size - 1] = '\0'; - if (((done & 1) == 0) && strncmp(line, "model name", 10) == 0) { - const char *v = strchr(line, ':') + 2; - tstrncpy(cpuModel, v, maxLen); - code = 0; - done |= 1; - } else if (((done & 2) == 0) && strncmp(line, "cpu cores", 9) == 0) { - const char *v = strchr(line, ':') + 2; - *numOfCores = atof(v); - done |= 2; - } - } - - if (line != NULL) free(line); - taosCloseFile(&pFile); - - return code; -} - + return info; #endif +} diff --git a/source/util/src/tdes.c b/source/util/src/tdes.c index 105dd7f95f..d12b47efe8 100644 --- a/source/util/src/tdes.c +++ b/source/util/src/tdes.c @@ -32,7 +32,7 @@ void process_message(uint8_t* message_piece, uint8_t* processed_piece, key_set* #if 0 int64_t taosDesGenKey() { uint32_t iseed = (uint32_t)time(NULL); - srand(iseed); + taosSeedRand(iseed); uint8_t key[8] = {0}; generate_key(key); diff --git a/source/util/src/tskiplist.c b/source/util/src/tskiplist.c index 6b89ed2c43..d9d6e4e3da 100644 --- a/source/util/src/tskiplist.c +++ b/source/util/src/tskiplist.c @@ -51,7 +51,7 @@ SSkipList *tSkipListCreate(uint8_t maxLevel, uint8_t keyType, uint16_t keyLen, _ pSkipList->len = keyLen; pSkipList->flags = flags; pSkipList->keyFn = fn; - pSkipList->seed = rand(); + pSkipList->seed = taosRand(); #if 0 // the function getkeycomparfunc is defined in common @@ -82,7 +82,7 @@ SSkipList *tSkipListCreate(uint8_t maxLevel, uint8_t keyType, uint16_t keyLen, _ } } - srand((uint32_t)time(NULL)); + taosSeedRand((uint32_t)time(NULL)); #if SKIP_LIST_RECORD_PERFORMANCE pSkipList->state.nTotalMemSize += sizeof(SSkipList); @@ -560,9 +560,9 @@ static FORCE_INLINE int32_t getSkipListNodeRandomHeight(SSkipList *pSkipList) { int32_t n = 1; #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) - while ((rand() % factor) == 0 && n <= pSkipList->maxLevel) { + while ((taosRand() % factor) == 0 && n <= pSkipList->maxLevel) { #else - while ((rand_r(&(pSkipList->seed)) % factor) == 0 && n <= pSkipList->maxLevel) { + while ((taosRandR(&(pSkipList->seed)) % factor) == 0 && n <= pSkipList->maxLevel) { #endif n++; } diff --git a/source/util/test/cacheTest.cpp b/source/util/test/cacheTest.cpp index e1c8c8c14e..748cf31b67 100644 --- a/source/util/test/cacheTest.cpp +++ b/source/util/test/cacheTest.cpp @@ -14,7 +14,7 @@ TEST(cacheTest, client_cache_test) { char data1[] = "test11"; char* cachedObj = (char*) taosCachePut(tscMetaCache, key1, strlen(key1), data1, strlen(data1)+1, 1); - sleep(REFRESH_TIME_IN_SEC+1); + taosSsleep(REFRESH_TIME_IN_SEC+1); printf("obj is still valid: %s\n", cachedObj); @@ -37,7 +37,7 @@ TEST(cacheTest, client_cache_test) { taosCacheRelease(tscMetaCache, (void**) &cachedObj2, false); - sleep(3); + taosSsleep(3); char* d = (char*) taosCacheAcquireByKey(tscMetaCache, key3, strlen(key3)); assert(d == NULL); diff --git a/source/util/test/codingTests.cpp b/source/util/test/codingTests.cpp index 0cd9524646..b991411047 100644 --- a/source/util/test/codingTests.cpp +++ b/source/util/test/codingTests.cpp @@ -150,7 +150,7 @@ static bool test_variant_int64(int64_t value) { } TEST(codingTest, fixed_encode_decode) { - srand(time(0)); + taosSeedRand(time(0)); // uint16_t for (uint16_t value = 0; value <= UINT16_MAX; value++) { @@ -204,7 +204,7 @@ TEST(codingTest, fixed_encode_decode) { } TEST(codingTest, variant_encode_decode) { - srand(time(0)); + taosSeedRand(time(0)); // uint16_t for (uint16_t value = 0; value <= UINT16_MAX; value++) { diff --git a/source/util/test/pageBufferTest.cpp b/source/util/test/pageBufferTest.cpp index f392aac7d1..e63e6f04a1 100644 --- a/source/util/test/pageBufferTest.cpp +++ b/source/util/test/pageBufferTest.cpp @@ -161,7 +161,7 @@ void recyclePageTest() { TEST(testCase, resultBufferTest) { - srand(time(NULL)); + taosSeedRand(time(NULL)); simpleTest(); writeDownTest(); recyclePageTest(); diff --git a/source/util/test/skiplistTest.cpp b/source/util/test/skiplistTest.cpp index f2e696b0e5..f61ebfd890 100644 --- a/source/util/test/skiplistTest.cpp +++ b/source/util/test/skiplistTest.cpp @@ -47,7 +47,7 @@ void doubleSkipListTest() { SSkipListKey sk; for (int32_t i = 0; i < 100; ++i) { sk.nType = TSDB_DATA_TYPE_DOUBLE; - int32_t idx = abs((i * rand()) % 1000); + int32_t idx = abs((i * taosRand()) % 1000); sk.dKey = doubleVal[idx]; @@ -74,7 +74,7 @@ void randKeyTest() { false, getkey); int32_t size = 200000; - srand(time(NULL)); + taosSeedRand(time(NULL)); printf("generated %d keys is: \n", size); @@ -87,7 +87,7 @@ void randKeyTest() { d->level = level; int32_t* key = (int32_t*)SL_GET_NODE_KEY(pSkipList, d); - key[0] = rand() % 1000000000; + key[0] = taosRand() % 1000000000; key[1] = key[0]; @@ -337,7 +337,7 @@ void duplicatedKeyTest() { TEST(testCase, skiplist_test) { assert(sizeof(SSkipListKey) == 8); - srand(time(NULL)); + taosSeedRand(time(NULL)); stringKeySkiplistTest(); doubleSkipListTest(); diff --git a/source/util/test/trefTest.c b/source/util/test/trefTest.c index 586151d782..58d9d2202e 100644 --- a/source/util/test/trefTest.c +++ b/source/util/test/trefTest.c @@ -42,7 +42,7 @@ void *addRef(void *param) { pSpace->p[id] = malloc(128); pSpace->rid[id] = taosAddRef(pSpace->rsetId, pSpace->p[id]); } - usleep(100); + taosUsleep(100); } return NULL; @@ -60,7 +60,7 @@ void *removeRef(void *param) { if (code == 0) pSpace->rid[id] = 0; } - usleep(100); + taosUsleep(100); } return NULL; @@ -76,7 +76,7 @@ void *acquireRelease(void *param) { id = random() % pSpace->refNum; void *p = taosAcquireRef(pSpace->rsetId, (int64_t) pSpace->p[id]); if (p) { - usleep(id % 5 + 1); + taosUsleep(id % 5 + 1); taosReleaseRef(pSpace->rsetId, (int64_t) pSpace->p[id]); } } diff --git a/tests/script/api/batchprepare.c b/tests/script/api/batchprepare.c index 386df15d86..5336a557e3 100644 --- a/tests/script/api/batchprepare.c +++ b/tests/script/api/batchprepare.c @@ -5089,7 +5089,7 @@ int main(int argc, char *argv[]) //pthread_create(&(pThreadList[3]), &thattr, runcase, (void *)&par[3]); while(1) { - sleep(1); + taosSsleep(1); } return 0; } diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 629677c9a5..dae0a1c840 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -1 +1,10 @@ -add_subdirectory(shell) \ No newline at end of file +IF (TD_TAOS_TOOLS) + INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/tools/taos_tools/deps/avro/lang/c/src) + INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include/client) + INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include/common) + INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include/util) + INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include/os) + ADD_SUBDIRECTORY(taos-tools) +ENDIF () + +add_subdirectory(shell) diff --git a/tools/taos-tools b/tools/taos-tools new file mode 160000 index 0000000000..f36b07f710 --- /dev/null +++ b/tools/taos-tools @@ -0,0 +1 @@ +Subproject commit f36b07f710d661dca88fdd70e73b5e3e16a960e0