From f5c13c13c6d1a6ddfd3a3ff18ae979cba342bd9a Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Tue, 1 Mar 2022 22:36:19 +0800 Subject: [PATCH 01/19] support json --- include/libs/index/index.h | 114 +++++++++++++++++++++++++--- source/libs/index/inc/indexInt.h | 57 +++++++++----- source/libs/index/src/index.c | 49 +++++++++--- source/libs/index/src/index_cache.c | 4 +- source/libs/index/src/index_json.c | 44 +++++++++++ source/libs/index/src/index_tfile.c | 13 ++-- source/libs/index/test/fstUT.cc | 10 +-- source/libs/index/test/jsonDemo.cc | 0 8 files changed, 241 insertions(+), 50 deletions(-) create mode 100644 source/libs/index/src/index_json.c create mode 100644 source/libs/index/test/jsonDemo.cc diff --git a/include/libs/index/index.h b/include/libs/index/index.h index 47eb97cc3a..9424eae1b7 100644 --- a/include/libs/index/index.h +++ b/include/libs/index/index.h @@ -29,6 +29,12 @@ typedef struct SIndexOpts SIndexOpts; typedef struct SIndexMultiTermQuery SIndexMultiTermQuery; typedef struct SArray SIndexMultiTerm; +typedef struct SIndex SIndexJson; +typedef struct SIndexTerm SIndexJsonTerm; +typedef struct SIndexOpts SIndexJsonOpts; +typedef struct SIndexMultiTermQuery SIndexJsonMultiTermQuery; +typedef struct SArray SIndexJsonMultiTerm; + typedef enum { ADD_VALUE, // add index colume value DEL_VALUE, // delete index column value @@ -39,24 +45,108 @@ typedef enum { } SIndexOperOnColumn; typedef enum { MUST = 0, SHOULD = 1, NOT = 2 } EIndexOperatorType; -typedef enum { QUERY_TERM = 0, QUERY_PREFIX = 1, QUERY_SUFFIX = 2, QUERY_REGEX = 3 } EIndexQueryType; +typedef enum { QUERY_TERM = 0, QUERY_PREFIX = 1, QUERY_SUFFIX = 2, QUERY_REGEX = 3, QUERY_RANGE = 4 } EIndexQueryType; + /* - * @param: oper - * + * create multi query + * @param oper (input, relation between querys) */ SIndexMultiTermQuery* indexMultiTermQueryCreate(EIndexOperatorType oper); -void indexMultiTermQueryDestroy(SIndexMultiTermQuery* pQuery); -int indexMultiTermQueryAdd(SIndexMultiTermQuery* pQuery, SIndexTerm* term, EIndexQueryType type); + /* - * @param: - * @param: + * destroy multi query + * @param pQuery (input, multi-query-object to be destory) + */ + +void indexMultiTermQueryDestroy(SIndexMultiTermQuery* pQuery); +/* + * add query to multi query + * @param pQuery (input, multi-query-object) + * @param term (input, single query term) + * @param type (input, single query type) + * @return error code + */ +int indexMultiTermQueryAdd(SIndexMultiTermQuery* pQuery, SIndexTerm* term, EIndexQueryType type); +/* + * open index + * @param opt (input, index opt) + * @param path (input, index path) + * @param index (output, index object) + * @return error code + */ +int indexOpen(SIndexOpts* opt, const char* path, SIndex** index); +/* + * close index + * @param index (input, index to be closed) + * @return error code */ -int indexOpen(SIndexOpts* opt, const char* path, SIndex** index); void indexClose(SIndex* index); -int indexPut(SIndex* index, SIndexMultiTerm* terms, uint64_t uid); -int indexDelete(SIndex* index, SIndexMultiTermQuery* query); -int indexSearch(SIndex* index, SIndexMultiTermQuery* query, SArray* result); -int indexRebuild(SIndex* index, SIndexOpts* opt); + +/* + * insert terms into index + * @param index (input, index object) + * @param term (input, terms inserted into index) + * @param uid (input, uid of terms) + * @return error code + */ +int indexPut(SIndex* index, SIndexMultiTerm* terms, uint64_t uid); +/* + * delete terms that meet query condition + * @param index (input, index object) + * @param query (input, condition query to deleted) + * @return error code + */ + +int indexDelete(SIndex* index, SIndexMultiTermQuery* query); +/* + * search index + * @param index (input, index object) + * @param query (input, multi query condition) + * @param result(output, query result) + * @return error code + */ +int indexSearch(SIndex* index, SIndexMultiTermQuery* query, SArray* result); +/* + * rebuild index + * @param index (input, index object) + * @parma opt (input, rebuild index opts) + * @return error code + */ +int indexRebuild(SIndex* index, SIndexOpts* opt); + +/* + * open index + * @param opt (input,index json opt) + * @param path (input, index json path) + * @param index (output, index json object) + * @return error code + */ +int tIndexJsonOpen(SIndexJsonOpts* opts, const char* path, SIndexJson** index); +/* + * close index + * @param index (input, index to be closed) + * @return error code + */ + +int tIndexJsonClose(SIndexJson* index); + +/* + * insert terms into index + * @param index (input, index object) + * @param term (input, terms inserted into index) + * @param uid (input, uid of terms) + * @return error code + */ +int tIndexJsonPut(SIndexJson* index, SIndexJsonMultiTerm* terms, uint64_t uid); +/* + * search index + * @param index (input, index object) + * @param query (input, multi query condition) + * @param result(output, query result) + * @return error code + */ + +int tIndexJsonSearch(SIndexJson* index, SIndexJsonMultiTermQuery* query, SArray* result); /* * @param * @param diff --git a/source/libs/index/inc/indexInt.h b/source/libs/index/inc/indexInt.h index 90ad1e15f4..57d587d297 100644 --- a/source/libs/index/inc/indexInt.h +++ b/source/libs/index/inc/indexInt.h @@ -120,29 +120,50 @@ int indexFlushCacheToTFile(SIndex* sIdx, void*); int32_t indexSerialCacheKey(ICacheKey* key, char* buf); -#define indexFatal(...) \ - do { \ - if (sDebugFlag & DEBUG_FATAL) { taosPrintLog("index FATAL ", 255, __VA_ARGS__); } \ +#define indexFatal(...) \ + do { \ + if (sDebugFlag & DEBUG_FATAL) { \ + taosPrintLog("index FATAL ", 255, __VA_ARGS__); \ + } \ } while (0) -#define indexError(...) \ - do { \ - if (sDebugFlag & DEBUG_ERROR) { taosPrintLog("index ERROR ", 255, __VA_ARGS__); } \ +#define indexError(...) \ + do { \ + if (sDebugFlag & DEBUG_ERROR) { \ + taosPrintLog("index ERROR ", 255, __VA_ARGS__); \ + } \ } while (0) -#define indexWarn(...) \ - do { \ - if (sDebugFlag & DEBUG_WARN) { taosPrintLog("index WARN ", 255, __VA_ARGS__); } \ +#define indexWarn(...) \ + do { \ + if (sDebugFlag & DEBUG_WARN) { \ + taosPrintLog("index WARN ", 255, __VA_ARGS__); \ + } \ } while (0) -#define indexInfo(...) \ - do { \ - if (sDebugFlag & DEBUG_INFO) { taosPrintLog("index ", 255, __VA_ARGS__); } \ +#define indexInfo(...) \ + do { \ + if (sDebugFlag & DEBUG_INFO) { \ + taosPrintLog("index ", 255, __VA_ARGS__); \ + } \ } while (0) -#define indexDebug(...) \ - do { \ - if (sDebugFlag & DEBUG_DEBUG) { taosPrintLog("index ", sDebugFlag, __VA_ARGS__); } \ +#define indexDebug(...) \ + do { \ + if (sDebugFlag & DEBUG_DEBUG) { \ + taosPrintLog("index ", sDebugFlag, __VA_ARGS__); \ + } \ } while (0) -#define indexTrace(...) \ - do { \ - if (sDebugFlag & DEBUG_TRACE) { taosPrintLog("index ", sDebugFlag, __VA_ARGS__); } \ +#define indexTrace(...) \ + do { \ + if (sDebugFlag & DEBUG_TRACE) { \ + taosPrintLog("index ", sDebugFlag, __VA_ARGS__); \ + } \ + } while (0) + +#define INDEX_TYPE_CONTAIN_EXTERN_TYPE(ty, exTy) (((ty >> 4) & (exTy)) != 0) +#define INDEX_TYPE_GET_TYPE(ty) (ty & 0x0F) +#define INDEX_TYPE_ADD_EXTERN_TYPE(ty, exTy) \ + do { \ + uint8_t oldTy = ty; \ + ty = (ty >> 4) | exTy; \ + ty = (ty << 4) | oldTy; \ } while (0) #ifdef __cplusplus diff --git a/source/libs/index/src/index.c b/source/libs/index/src/index.c index 5147734a85..b518df2ea2 100644 --- a/source/libs/index/src/index.c +++ b/source/libs/index/src/index.c @@ -2,8 +2,8 @@ * Copyright (c) 2019 TAOS Data, Inc. * * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. + * it under the terms of the GNU Affero General Public License, version 3 * or later ("AGPL"), as published by the Free + * Software Foundation. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or @@ -30,6 +30,8 @@ void* indexQhandle = NULL; +static char JSON_COLUMN[] = "JSON"; + void indexInit() { // refactor later indexQhandle = taosInitScheduler(INDEX_QUEUE_SIZE, INDEX_NUM_OF_THREADS, "index"); @@ -63,6 +65,9 @@ static int indexGenTFile(SIndex* index, IndexCache* cache, SArray* batch); static void indexMergeCacheAndTFile(SArray* result, IterateValue* icache, IterateValue* iTfv); static void indexMergeSameKey(SArray* result, TFileValue* tv); +static int32_t indexSerialTermKey(SIndexTerm* itm, char* buf); +int32_t indexSerialKey(ICacheKey* key, char* buf); + int indexOpen(SIndexOpts* opts, const char* path, SIndex** index) { pthread_once(&isInit, indexInit); SIndex* sIdx = calloc(1, sizeof(SIndex)); @@ -147,9 +152,8 @@ int indexPut(SIndex* index, SIndexMultiTerm* fVals, uint64_t uid) { for (int i = 0; i < taosArrayGetSize(fVals); i++) { SIndexTerm* p = taosArrayGetP(fVals, i); - char buf[128] = {0}; - ICacheKey key = {.suid = p->suid, .colName = p->colName, .nColName = strlen(p->colName)}; - int32_t sz = indexSerialCacheKey(&key, buf); + char buf[128] = {0}; + int32_t sz = indexSerialTermKey(p, buf); IndexCache** cache = taosHashGet(index->colObj, buf, sz); if (cache == NULL) { @@ -162,9 +166,9 @@ int indexPut(SIndex* index, SIndexMultiTerm* fVals, uint64_t uid) { for (int i = 0; i < taosArrayGetSize(fVals); i++) { SIndexTerm* p = taosArrayGetP(fVals, i); - char buf[128] = {0}; - ICacheKey key = {.suid = p->suid, .colName = p->colName, .nColName = strlen(p->colName)}; - int32_t sz = indexSerialCacheKey(&key, buf); + char buf[128] = {0}; + // ICacheKey key = {.suid = p->suid, .colName = p->colName, .nColName = strlen(p->colName)}; + int32_t sz = indexSerialTermKey(p, buf); IndexCache** cache = taosHashGet(index->colObj, buf, sz); assert(*cache != NULL); @@ -554,7 +558,24 @@ END: return -1; } -int32_t indexSerialCacheKey(ICacheKey* key, char* buf) { +int32_t indexSerialTermKeyOfTag(SIndexTerm* p, char* buf) { + ICacheKey key = {.suid = p->suid, .colName = p->colName, .nColName = strlen(p->colName)}; + return indexSerialKey(&key, buf); +} +int32_t indexSerilaTermKeyOfJson(SIndexTerm* p, char* buf) { + ICacheKey key = {.suid = p->suid, .colName = JSON_COLUMN, .nColName = strlen(JSON_COLUMN)}; + return indexSerialKey(&key, buf); +} + +int32_t indexSerialTermKey(SIndexTerm* itm, char* buf) { + bool hasJson = INDEX_TYPE_CONTAIN_EXTERN_TYPE(itm->colType, TSDB_DATA_TYPE_JSON); + if (hasJson) { + return indexSerilaTermKeyOfJson(itm, buf); + } else { + return indexSerialTermKeyOfTag(itm, buf); + } +} +int32_t indexSerialKey(ICacheKey* key, char* buf) { char* p = buf; SERIALIZE_MEM_TO_BUF(buf, key, suid); SERIALIZE_VAR_TO_BUF(buf, '_', char); @@ -563,3 +584,13 @@ int32_t indexSerialCacheKey(ICacheKey* key, char* buf) { SERIALIZE_STR_MEM_TO_BUF(buf, key, colName, key->nColName); return buf - p; } + +// int32_t indexSerialCacheKey(ICacheKey* key, char* buf) { +// char* p = buf; +// SERIALIZE_MEM_TO_BUF(buf, key, suid); +// SERIALIZE_VAR_TO_BUF(buf, '_', char); +// // SERIALIZE_MEM_TO_BUF(buf, key, colType); +// // SERIALIZE_VAR_TO_BUF(buf, '_', char); +// SERIALIZE_STR_MEM_TO_BUF(buf, key, colName, key->nColName); +// return buf - p; +//} diff --git a/source/libs/index/src/index_cache.c b/source/libs/index/src/index_cache.c index d6a7141825..bb29a78cb1 100644 --- a/source/libs/index/src/index_cache.c +++ b/source/libs/index/src/index_cache.c @@ -24,6 +24,8 @@ #define MEM_THRESHOLD 1024 * 1024 #define MEM_ESTIMATE_RADIO 1.5 +static char JSON_COLUMN[] = "JSON"; + static void indexMemRef(MemTable* tbl); static void indexMemUnRef(MemTable* tbl); @@ -45,7 +47,7 @@ IndexCache* indexCacheCreate(SIndex* idx, uint64_t suid, const char* colName, in return NULL; }; cache->mem = indexInternalCacheCreate(type); - cache->colName = tstrdup(colName); + cache->colName = INDEX_TYPE_CONTAIN_EXTERN_TYPE(type, TSDB_DATA_TYPE_JSON) ? tstrdup(JSON_COLUMN) : tstrdup(colName); cache->type = type; cache->index = idx; cache->version = 0; diff --git a/source/libs/index/src/index_json.c b/source/libs/index/src/index_json.c new file mode 100644 index 0000000000..f4d28b45b0 --- /dev/null +++ b/source/libs/index/src/index_json.c @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 * or later ("AGPL"), as published by the Free + * Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#include "indeInt.h" +#include "index.h" + +int tIndexJsonOpen(SIndexJsonOpts *opts, const char *path, SIndexJson **index) { + // handle + return tIndexOpen(opts, path, index); +} +// k +int tIndexJsonPut(SIndexJson *index, SIndexJsonMultiTerm *terms, uint64_t uid) { + for (int i = 0; i < taosArrayGetSize(terms); i++) { + SIndexJsonTerm *p = taosArrayGetP(terms, i); + INDEX_TYPE_ADD_EXTERN_TYPE(p->colType, TSDB_DATA_TYPE_JSON); + } + return indexPut(index, terms, uid); + // handle put +} + +int tIndexJsonSearch(SIndexJson *index, SIndexJsonMultiTermQuery *query, SArray *result) { + for (int i = 0; i < taosArrayGetSize(terms); i++) { + SIndexJsonTerm *p = taosArrayGetP(terms, i); + INDEX_TYPE_ADD_EXTERN_TYPE(p->colType, TSDB_DATA_TYPE_JSON); + } + return indexSearch(index, query, result); + // handle search +} + +int tIndexJsonClose(SIndexJson *index) { + return tIndexClose(index); + // handle close +} diff --git a/source/libs/index/src/index_tfile.c b/source/libs/index/src/index_tfile.c index e44f8fc1c3..a05884b790 100644 --- a/source/libs/index/src/index_tfile.c +++ b/source/libs/index/src/index_tfile.c @@ -205,7 +205,11 @@ int tfileReaderSearch(TFileReader* reader, SIndexTermQuery* query, SArray* resul } else if (qtype == QUERY_PREFIX) { // handle later // - } else { + } else if (qtype == QUERY_SUFFIX) { + // handle later + } else if (qtype == QUERY_REGEX) { + // handle later + } else if (qtype == QUERY_RANGE) { // handle later } tfileReaderUnRef(reader); @@ -586,11 +590,10 @@ static int tfileReaderLoadHeader(TFileReader* reader) { int64_t nread = reader->ctx->readFrom(reader->ctx, buf, sizeof(buf), 0); if (nread == -1) { - indexError("actual Read: %d, to read: %d, errno: %d, filename: %s", (int)(nread), (int)sizeof(buf), - errno, reader->ctx->file.buf); + indexError("actual Read: %d, to read: %d, errno: %d, filename: %s", (int)(nread), (int)sizeof(buf), errno, + reader->ctx->file.buf); } else { - indexInfo("actual Read: %d, to read: %d, filename: %s", (int)(nread), (int)sizeof(buf), - reader->ctx->file.buf); + indexInfo("actual Read: %d, to read: %d, filename: %s", (int)(nread), (int)sizeof(buf), reader->ctx->file.buf); } // assert(nread == sizeof(buf)); memcpy(&reader->header, buf, sizeof(buf)); diff --git a/source/libs/index/test/fstUT.cc b/source/libs/index/test/fstUT.cc index d59a3428da..b34fd71e9c 100644 --- a/source/libs/index/test/fstUT.cc +++ b/source/libs/index/test/fstUT.cc @@ -216,21 +216,21 @@ class FstEnv : public ::testing::Test { TEST_F(FstEnv, writeNormal) { fst->CreateWriter(); - std::string str("aa"); + std::string str("11"); for (int i = 0; i < 10; i++) { - str[0] = 'a' + i; + str[0] = '1' + i; str.resize(2); assert(fst->Put(str, i) == true); } // order failed - assert(fst->Put("aa", 1) == false); + assert(fst->Put("11", 1) == false); fst->DestroyWriter(); fst->CreateReader(); uint64_t val; - assert(fst->Get("a", &val) == false); - assert(fst->Get("aa", &val) == true); + assert(fst->Get("1", &val) == false); + assert(fst->Get("11", &val) == true); assert(val == 0); std::vector rlt; diff --git a/source/libs/index/test/jsonDemo.cc b/source/libs/index/test/jsonDemo.cc new file mode 100644 index 0000000000..e69de29bb2 From d7c3415ae0a5806f3a8710dc6fb13f4816197c25 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Wed, 2 Mar 2022 09:24:17 +0800 Subject: [PATCH 02/19] support json --- source/libs/index/src/index_json.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/libs/index/src/index_json.c b/source/libs/index/src/index_json.c index f4d28b45b0..9ffab3fad1 100644 --- a/source/libs/index/src/index_json.c +++ b/source/libs/index/src/index_json.c @@ -12,8 +12,8 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#include "indeInt.h" #include "index.h" +#include "indexInt.h" int tIndexJsonOpen(SIndexJsonOpts *opts, const char *path, SIndexJson **index) { // handle From 4be79789ca9d044df45cd465b85fad63146b647e Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Wed, 2 Mar 2022 15:05:16 +0800 Subject: [PATCH 03/19] support json --- include/libs/index/index.h | 4 +-- source/libs/index/inc/indexInt.h | 2 ++ source/libs/index/src/index.c | 55 ++++++++++------------------- source/libs/index/src/index_cache.c | 46 +++++++++++++++++++----- source/libs/index/src/index_json.c | 12 +++---- 5 files changed, 65 insertions(+), 54 deletions(-) diff --git a/include/libs/index/index.h b/include/libs/index/index.h index 9424eae1b7..453b49e4c6 100644 --- a/include/libs/index/index.h +++ b/include/libs/index/index.h @@ -125,10 +125,10 @@ int tIndexJsonOpen(SIndexJsonOpts* opts, const char* path, SIndexJson** index); /* * close index * @param index (input, index to be closed) - * @return error code + * @return void */ -int tIndexJsonClose(SIndexJson* index); +void tIndexJsonClose(SIndexJson* index); /* * insert terms into index diff --git a/source/libs/index/inc/indexInt.h b/source/libs/index/inc/indexInt.h index 57d587d297..928e8b6102 100644 --- a/source/libs/index/inc/indexInt.h +++ b/source/libs/index/inc/indexInt.h @@ -119,6 +119,8 @@ typedef struct TFileCacheKey { int indexFlushCacheToTFile(SIndex* sIdx, void*); int32_t indexSerialCacheKey(ICacheKey* key, char* buf); +// int32_t indexSerialKey(ICacheKey* key, char* buf); +// int32_t indexSerialTermKey(SIndexTerm* itm, char* buf); #define indexFatal(...) \ do { \ diff --git a/source/libs/index/src/index.c b/source/libs/index/src/index.c index b518df2ea2..168e819073 100644 --- a/source/libs/index/src/index.c +++ b/source/libs/index/src/index.c @@ -65,8 +65,8 @@ static int indexGenTFile(SIndex* index, IndexCache* cache, SArray* batch); static void indexMergeCacheAndTFile(SArray* result, IterateValue* icache, IterateValue* iTfv); static void indexMergeSameKey(SArray* result, TFileValue* tv); -static int32_t indexSerialTermKey(SIndexTerm* itm, char* buf); -int32_t indexSerialKey(ICacheKey* key, char* buf); +// static int32_t indexSerialTermKey(SIndexTerm* itm, char* buf); +// int32_t indexSerialKey(ICacheKey* key, char* buf); int indexOpen(SIndexOpts* opts, const char* path, SIndex** index) { pthread_once(&isInit, indexInit); @@ -152,8 +152,9 @@ int indexPut(SIndex* index, SIndexMultiTerm* fVals, uint64_t uid) { for (int i = 0; i < taosArrayGetSize(fVals); i++) { SIndexTerm* p = taosArrayGetP(fVals, i); - char buf[128] = {0}; - int32_t sz = indexSerialTermKey(p, buf); + char buf[128] = {0}; + ICacheKey key = {.suid = p->suid, .colName = p->colName, .nColName = strlen(p->colName), .colType = p->colType}; + int32_t sz = indexSerialCacheKey(&key, buf); IndexCache** cache = taosHashGet(index->colObj, buf, sz); if (cache == NULL) { @@ -166,9 +167,9 @@ int indexPut(SIndex* index, SIndexMultiTerm* fVals, uint64_t uid) { for (int i = 0; i < taosArrayGetSize(fVals); i++) { SIndexTerm* p = taosArrayGetP(fVals, i); - char buf[128] = {0}; - // ICacheKey key = {.suid = p->suid, .colName = p->colName, .nColName = strlen(p->colName)}; - int32_t sz = indexSerialTermKey(p, buf); + char buf[128] = {0}; + ICacheKey key = {.suid = p->suid, .colName = p->colName, .nColName = strlen(p->colName), .colType = p->colType}; + int32_t sz = indexSerialCacheKey(&key, buf); IndexCache** cache = taosHashGet(index->colObj, buf, sz); assert(*cache != NULL); @@ -334,8 +335,9 @@ static int indexTermSearch(SIndex* sIdx, SIndexTermQuery* query, SArray** result IndexCache* cache = NULL; char buf[128] = {0}; - ICacheKey key = {.suid = term->suid, .colName = term->colName, .nColName = strlen(term->colName)}; - int32_t sz = indexSerialCacheKey(&key, buf); + ICacheKey key = { + .suid = term->suid, .colName = term->colName, .nColName = strlen(term->colName), .colType = term->colType}; + int32_t sz = indexSerialCacheKey(&key, buf); pthread_mutex_lock(&sIdx->mtx); IndexCache** pCache = taosHashGet(sIdx->colObj, buf, sz); @@ -558,39 +560,18 @@ END: return -1; } -int32_t indexSerialTermKeyOfTag(SIndexTerm* p, char* buf) { - ICacheKey key = {.suid = p->suid, .colName = p->colName, .nColName = strlen(p->colName)}; - return indexSerialKey(&key, buf); -} -int32_t indexSerilaTermKeyOfJson(SIndexTerm* p, char* buf) { - ICacheKey key = {.suid = p->suid, .colName = JSON_COLUMN, .nColName = strlen(JSON_COLUMN)}; - return indexSerialKey(&key, buf); -} +int32_t indexSerialCacheKey(ICacheKey* key, char* buf) { + bool hasJson = INDEX_TYPE_CONTAIN_EXTERN_TYPE(key->colType, TSDB_DATA_TYPE_JSON); -int32_t indexSerialTermKey(SIndexTerm* itm, char* buf) { - bool hasJson = INDEX_TYPE_CONTAIN_EXTERN_TYPE(itm->colType, TSDB_DATA_TYPE_JSON); - if (hasJson) { - return indexSerilaTermKeyOfJson(itm, buf); - } else { - return indexSerialTermKeyOfTag(itm, buf); - } -} -int32_t indexSerialKey(ICacheKey* key, char* buf) { char* p = buf; SERIALIZE_MEM_TO_BUF(buf, key, suid); SERIALIZE_VAR_TO_BUF(buf, '_', char); // SERIALIZE_MEM_TO_BUF(buf, key, colType); // SERIALIZE_VAR_TO_BUF(buf, '_', char); - SERIALIZE_STR_MEM_TO_BUF(buf, key, colName, key->nColName); + if (hasJson) { + SERIALIZE_STR_VAR_TO_BUF(buf, JSON_COLUMN, strlen(JSON_COLUMN)); + } else { + SERIALIZE_STR_MEM_TO_BUF(buf, key, colName, key->nColName); + } return buf - p; } - -// int32_t indexSerialCacheKey(ICacheKey* key, char* buf) { -// char* p = buf; -// SERIALIZE_MEM_TO_BUF(buf, key, suid); -// SERIALIZE_VAR_TO_BUF(buf, '_', char); -// // SERIALIZE_MEM_TO_BUF(buf, key, colType); -// // SERIALIZE_VAR_TO_BUF(buf, '_', char); -// SERIALIZE_STR_MEM_TO_BUF(buf, key, colName, key->nColName); -// return buf - p; -//} diff --git a/source/libs/index/src/index_cache.c b/source/libs/index/src/index_cache.c index bb29a78cb1..a1f074feae 100644 --- a/source/libs/index/src/index_cache.c +++ b/source/libs/index/src/index_cache.c @@ -25,6 +25,7 @@ #define MEM_ESTIMATE_RADIO 1.5 static char JSON_COLUMN[] = "JSON"; +static char JSON_VALUE_DELIM = '&'; static void indexMemRef(MemTable* tbl); static void indexMemUnRef(MemTable* tbl); @@ -46,6 +47,7 @@ IndexCache* indexCacheCreate(SIndex* idx, uint64_t suid, const char* colName, in indexError("failed to create index cache"); return NULL; }; + cache->mem = indexInternalCacheCreate(type); cache->colName = INDEX_TYPE_CONTAIN_EXTERN_TYPE(type, TSDB_DATA_TYPE_JSON) ? tstrdup(JSON_COLUMN) : tstrdup(colName); cache->type = type; @@ -209,11 +211,38 @@ static void indexCacheMakeRoomForWrite(IndexCache* cache) { } } } +static char* indexCachePackJsonData(SIndexTerm* itm) { + /* + * |<-----colname---->|<-----dataType---->|<--------colVal---------->| + * |<-----string----->|<-----uint8_t----->|<----depend on dataType-->| + */ + uint8_t ty = INDEX_TYPE_GET_TYPE(itm->colType); + int32_t sz = itm->nColName + itm->nColVal + sizeof(uint8_t) + sizeof(JSON_VALUE_DELIM) * 2 + 1; + char* buf = (char*)calloc(1, sz); + char* p = buf; + + memcpy(p, itm->colVal, itm->nColName); + p += itm->nColName; + + memcpy(p, &JSON_VALUE_DELIM, sizeof(JSON_VALUE_DELIM)); + p += sizeof(JSON_VALUE_DELIM); + + memcpy(p, &ty, sizeof(ty)); + p += sizeof(ty); + + memcpy(p, &JSON_VALUE_DELIM, sizeof(JSON_VALUE_DELIM)); + p += sizeof(JSON_VALUE_DELIM); + + memcpy(p, itm->colVal, itm->nColVal); + + return buf; +} int indexCachePut(void* cache, SIndexTerm* term, uint64_t uid) { if (cache == NULL) { return -1; } + bool hasJson = INDEX_TYPE_CONTAIN_EXTERN_TYPE(term->colType, TSDB_DATA_TYPE_JSON); IndexCache* pCache = cache; indexCacheRef(pCache); @@ -224,8 +253,12 @@ int indexCachePut(void* cache, SIndexTerm* term, uint64_t uid) { } // set up key ct->colType = term->colType; - ct->colVal = (char*)calloc(1, sizeof(char) * (term->nColVal + 1)); - memcpy(ct->colVal, term->colVal, term->nColVal); + if (hasJson) { + ct->colVal = indexCachePackJsonData(term); + } else { + ct->colVal = (char*)calloc(1, sizeof(char) * (term->nColVal + 1)); + memcpy(ct->colVal, term->colVal, term->nColVal); + } ct->version = atomic_add_fetch_32(&pCache->version, 1); // set value ct->uid = uid; @@ -369,6 +402,8 @@ static int32_t indexCacheTermCompare(const void* l, const void* r) { } static MemTable* indexInternalCacheCreate(int8_t type) { + type = INDEX_TYPE_CONTAIN_EXTERN_TYPE(type, TSDB_DATA_TYPE_JSON) ? TSDB_DATA_TYPE_BINARY : type; + MemTable* tbl = calloc(1, sizeof(MemTable)); indexMemRef(tbl); if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) { @@ -391,9 +426,6 @@ static bool indexCacheIteratorNext(Iterate* itera) { IterateValue* iv = &itera->val; iterateValueDestroy(iv, false); - // IterateValue* iv = &itera->val; - // IterateValue tIterVal = {.colVal = NULL, .val = taosArrayInit(1, sizeof(uint64_t))}; - bool next = tSkipListIterNext(iter); if (next) { SSkipListNode* node = tSkipListIterGet(iter); @@ -413,10 +445,6 @@ static bool indexCacheIteratorNext(Iterate* itera) { taosArrayPush(iv->val, &ct->uid); } - // IterateValue* iv = &itera->val; - // iterateValueDestroy(iv, true); - //*iv = tIterVal; - return next; } diff --git a/source/libs/index/src/index_json.c b/source/libs/index/src/index_json.c index 9ffab3fad1..de88ff3c8a 100644 --- a/source/libs/index/src/index_json.c +++ b/source/libs/index/src/index_json.c @@ -17,9 +17,8 @@ int tIndexJsonOpen(SIndexJsonOpts *opts, const char *path, SIndexJson **index) { // handle - return tIndexOpen(opts, path, index); + return indexOpen(opts, path, index); } -// k int tIndexJsonPut(SIndexJson *index, SIndexJsonMultiTerm *terms, uint64_t uid) { for (int i = 0; i < taosArrayGetSize(terms); i++) { SIndexJsonTerm *p = taosArrayGetP(terms, i); @@ -29,16 +28,17 @@ int tIndexJsonPut(SIndexJson *index, SIndexJsonMultiTerm *terms, uint64_t uid) { // handle put } -int tIndexJsonSearch(SIndexJson *index, SIndexJsonMultiTermQuery *query, SArray *result) { +int tIndexJsonSearch(SIndexJson *index, SIndexJsonMultiTermQuery *tq, SArray *result) { + SArray *terms = tq->query; for (int i = 0; i < taosArrayGetSize(terms); i++) { SIndexJsonTerm *p = taosArrayGetP(terms, i); INDEX_TYPE_ADD_EXTERN_TYPE(p->colType, TSDB_DATA_TYPE_JSON); } - return indexSearch(index, query, result); + return indexSearch(index, tq, result); // handle search } -int tIndexJsonClose(SIndexJson *index) { - return tIndexClose(index); +void tIndexJsonClose(SIndexJson *index) { + return indexClose(index); // handle close } From 6a6f31c4a75dd30d916b7b31512f7620b6af1d02 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Wed, 2 Mar 2022 17:09:22 +0800 Subject: [PATCH 04/19] support json --- source/libs/index/src/index_cache.c | 13 +++- source/libs/index/test/CMakeLists.txt | 18 ++++++ source/libs/index/test/jsonUT.cc | 92 +++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 source/libs/index/test/jsonUT.cc diff --git a/source/libs/index/src/index_cache.c b/source/libs/index/src/index_cache.c index a1f074feae..2742830d3b 100644 --- a/source/libs/index/src/index_cache.c +++ b/source/libs/index/src/index_cache.c @@ -222,7 +222,7 @@ static char* indexCachePackJsonData(SIndexTerm* itm) { char* buf = (char*)calloc(1, sz); char* p = buf; - memcpy(p, itm->colVal, itm->nColName); + memcpy(p, itm->colName, itm->nColName); p += itm->nColName; memcpy(p, &JSON_VALUE_DELIM, sizeof(JSON_VALUE_DELIM)); @@ -329,13 +329,22 @@ int indexCacheSearch(void* cache, SIndexTermQuery* query, SArray* result, STermV SIndexTerm* term = query->term; EIndexQueryType qtype = query->qType; - CacheTerm ct = {.colVal = term->colVal, .version = atomic_load_32(&pCache->version)}; + + bool hasJson = INDEX_TYPE_CONTAIN_EXTERN_TYPE(term->colType, TSDB_DATA_TYPE_JSON); + char* p = term->colVal; + if (hasJson) { + p = indexCachePackJsonData(term); + } + CacheTerm ct = {.colVal = p, .version = atomic_load_32(&pCache->version)}; int ret = indexQueryMem(mem, &ct, qtype, result, s); if (ret == 0 && *s != kTypeDeletion) { // continue search in imm ret = indexQueryMem(imm, &ct, qtype, result, s); } + if (hasJson) { + tfree(p); + } indexMemUnRef(mem); indexMemUnRef(imm); diff --git a/source/libs/index/test/CMakeLists.txt b/source/libs/index/test/CMakeLists.txt index bed42be3e5..1ebf85368b 100644 --- a/source/libs/index/test/CMakeLists.txt +++ b/source/libs/index/test/CMakeLists.txt @@ -2,6 +2,7 @@ add_executable(indexTest "") add_executable(fstTest "") add_executable(fstUT "") add_executable(UtilUT "") +add_executable(jsonUT "") target_sources(indexTest PRIVATE @@ -21,6 +22,10 @@ target_sources(UtilUT "utilUT.cc" ) +target_sources(jsonUT + PRIVATE + "jsonUT.cc" +) target_include_directories ( indexTest PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/index" @@ -43,6 +48,12 @@ target_include_directories ( UtilUT "${CMAKE_SOURCE_DIR}/include/libs/index" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) + +target_include_directories (jsonUT + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/index" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) target_link_libraries (indexTest os util @@ -73,6 +84,13 @@ target_link_libraries (UtilUT index ) +target_link_libraries (jsonUT + os + util + common + gtest_main + index +) #add_test( # NAME index_test diff --git a/source/libs/index/test/jsonUT.cc b/source/libs/index/test/jsonUT.cc new file mode 100644 index 0000000000..20b59add9f --- /dev/null +++ b/source/libs/index/test/jsonUT.cc @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include +#include +#include "index.h" +#include "indexInt.h" +#include "index_cache.h" +#include "index_fst.h" +#include "index_fst_counting_writer.h" +#include "index_fst_util.h" +#include "index_tfile.h" +#include "index_util.h" +#include "tglobal.h" +#include "tskiplist.h" +#include "tutil.h" + +static std::string dir = "/tmp/json"; +class JsonEnv : public ::testing::Test { + protected: + virtual void SetUp() { + taosRemoveDir(dir.c_str()); + opts = indexOptsCreate(); + int ret = tIndexJsonOpen(opts, dir.c_str(), &index); + assert(ret == 0); + } + virtual void TearDown() { + tIndexJsonClose(index); + indexOptsDestroy(opts); + } + SIndexJsonOpts* opts; + SIndexJson* index; +}; + +TEST_F(JsonEnv, testWrite) { + { + std::string colName("voltage"); + std::string colVal("ab"); + SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), + colVal.c_str(), colVal.size()); + + SIndexMultiTerm* terms = indexMultiTermCreate(); + indexMultiTermAdd(terms, term); + for (size_t i = 0; i < 100; i++) { + tIndexJsonPut(index, terms, i); + } + indexMultiTermDestroy(terms); + } + { + std::string colName("voltage"); + std::string colVal("ab1"); + SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), + colVal.c_str(), colVal.size()); + + SIndexMultiTerm* terms = indexMultiTermCreate(); + indexMultiTermAdd(terms, term); + for (size_t i = 0; i < 100; i++) { + tIndexJsonPut(index, terms, i); + } + indexMultiTermDestroy(terms); + } + { + std::string colName("voltage"); + std::string colVal("123"); + SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), + colVal.c_str(), colVal.size()); + + SIndexMultiTerm* terms = indexMultiTermCreate(); + indexMultiTermAdd(terms, term); + for (size_t i = 0; i < 100; i++) { + tIndexJsonPut(index, terms, i); + } + indexMultiTermDestroy(terms); + } + { + std::string colName("voltage"); + std::string colVal("ab"); + + SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST); + SIndexTerm* q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), + colVal.c_str(), colVal.size()); + + SArray* result = taosArrayInit(1, sizeof(uint64_t)); + indexMultiTermQueryAdd(mq, q, QUERY_TERM); + tIndexJsonSearch(index, mq, result); + assert(100 == taosArrayGetSize(result)); + indexMultiTermQueryDestroy(mq); + } + + // SIndexTermQuery query = {.term = term, .qType = QUERY_TERM}; +} From 7ae87c10103c801d06092ea3ddea00b9e9b671bd Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Wed, 2 Mar 2022 22:06:02 +0800 Subject: [PATCH 05/19] add UT --- source/libs/index/inc/index_comm.h | 32 +++++++++++++++++++ source/libs/index/src/index_cache.c | 35 ++------------------ source/libs/index/src/index_comm.c | 48 ++++++++++++++++++++++++++++ source/libs/index/src/index_tfile.c | 13 +++++++- source/libs/index/test/fstUT.cc | 16 ++++++++++ source/libs/index/test/jsonDemo.cc | 0 source/libs/index/test/jsonUT.cc | 33 +++++++++++++++++-- source/libs/transport/src/transSrv.c | 20 ++++++------ 8 files changed, 153 insertions(+), 44 deletions(-) create mode 100644 source/libs/index/inc/index_comm.h create mode 100644 source/libs/index/src/index_comm.c delete mode 100644 source/libs/index/test/jsonDemo.cc diff --git a/source/libs/index/inc/index_comm.h b/source/libs/index/inc/index_comm.h new file mode 100644 index 0000000000..0d8418ba65 --- /dev/null +++ b/source/libs/index/inc/index_comm.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef _TD_INDEX_COMM_H_ +#define _TD_INDEX_COMM_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +extern char JSON_COLUMN[]; +extern char JSON_VALUE_DELIM; + +char* indexPackJsonData(SIndexTerm* itm); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/source/libs/index/src/index_cache.c b/source/libs/index/src/index_cache.c index 2742830d3b..599bac3fe6 100644 --- a/source/libs/index/src/index_cache.c +++ b/source/libs/index/src/index_cache.c @@ -14,6 +14,7 @@ */ #include "index_cache.h" +#include "index_comm.h" #include "index_util.h" #include "tcompare.h" #include "tsched.h" @@ -24,9 +25,6 @@ #define MEM_THRESHOLD 1024 * 1024 #define MEM_ESTIMATE_RADIO 1.5 -static char JSON_COLUMN[] = "JSON"; -static char JSON_VALUE_DELIM = '&'; - static void indexMemRef(MemTable* tbl); static void indexMemUnRef(MemTable* tbl); @@ -211,33 +209,6 @@ static void indexCacheMakeRoomForWrite(IndexCache* cache) { } } } -static char* indexCachePackJsonData(SIndexTerm* itm) { - /* - * |<-----colname---->|<-----dataType---->|<--------colVal---------->| - * |<-----string----->|<-----uint8_t----->|<----depend on dataType-->| - */ - uint8_t ty = INDEX_TYPE_GET_TYPE(itm->colType); - - int32_t sz = itm->nColName + itm->nColVal + sizeof(uint8_t) + sizeof(JSON_VALUE_DELIM) * 2 + 1; - char* buf = (char*)calloc(1, sz); - char* p = buf; - - memcpy(p, itm->colName, itm->nColName); - p += itm->nColName; - - memcpy(p, &JSON_VALUE_DELIM, sizeof(JSON_VALUE_DELIM)); - p += sizeof(JSON_VALUE_DELIM); - - memcpy(p, &ty, sizeof(ty)); - p += sizeof(ty); - - memcpy(p, &JSON_VALUE_DELIM, sizeof(JSON_VALUE_DELIM)); - p += sizeof(JSON_VALUE_DELIM); - - memcpy(p, itm->colVal, itm->nColVal); - - return buf; -} int indexCachePut(void* cache, SIndexTerm* term, uint64_t uid) { if (cache == NULL) { return -1; @@ -254,7 +225,7 @@ int indexCachePut(void* cache, SIndexTerm* term, uint64_t uid) { // set up key ct->colType = term->colType; if (hasJson) { - ct->colVal = indexCachePackJsonData(term); + ct->colVal = indexPackJsonData(term); } else { ct->colVal = (char*)calloc(1, sizeof(char) * (term->nColVal + 1)); memcpy(ct->colVal, term->colVal, term->nColVal); @@ -333,7 +304,7 @@ int indexCacheSearch(void* cache, SIndexTermQuery* query, SArray* result, STermV bool hasJson = INDEX_TYPE_CONTAIN_EXTERN_TYPE(term->colType, TSDB_DATA_TYPE_JSON); char* p = term->colVal; if (hasJson) { - p = indexCachePackJsonData(term); + p = indexPackJsonData(term); } CacheTerm ct = {.colVal = p, .version = atomic_load_32(&pCache->version)}; diff --git a/source/libs/index/src/index_comm.c b/source/libs/index/src/index_comm.c new file mode 100644 index 0000000000..4f3cbaa4da --- /dev/null +++ b/source/libs/index/src/index_comm.c @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 * or later ("AGPL"), as published by the Free + * Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "index.h" +#include "indexInt.h" + +char JSON_COLUMN[] = "JSON"; +char JSON_VALUE_DELIM = '&'; + +char* indexPackJsonData(SIndexTerm* itm) { + /* + * |<-----colname---->|<-----dataType---->|<--------colVal---------->| + * |<-----string----->|<-----uint8_t----->|<----depend on dataType-->| + */ + uint8_t ty = INDEX_TYPE_GET_TYPE(itm->colType); + + int32_t sz = itm->nColName + itm->nColVal + sizeof(uint8_t) + sizeof(JSON_VALUE_DELIM) * 2 + 1; + char* buf = (char*)calloc(1, sz); + char* p = buf; + + memcpy(p, itm->colName, itm->nColName); + p += itm->nColName; + + memcpy(p, &JSON_VALUE_DELIM, sizeof(JSON_VALUE_DELIM)); + p += sizeof(JSON_VALUE_DELIM); + + memcpy(p, &ty, sizeof(ty)); + p += sizeof(ty); + + memcpy(p, &JSON_VALUE_DELIM, sizeof(JSON_VALUE_DELIM)); + p += sizeof(JSON_VALUE_DELIM); + + memcpy(p, itm->colVal, itm->nColVal); + + return buf; +} diff --git a/source/libs/index/src/index_tfile.c b/source/libs/index/src/index_tfile.c index a05884b790..a2089e8eee 100644 --- a/source/libs/index/src/index_tfile.c +++ b/source/libs/index/src/index_tfile.c @@ -15,6 +15,7 @@ p * #include "index_tfile.h" #include "index.h" +#include "index_comm.h" #include "index_fst.h" #include "index_fst_counting_writer.h" #include "index_util.h" @@ -186,13 +187,20 @@ void tfileReaderDestroy(TFileReader* reader) { int tfileReaderSearch(TFileReader* reader, SIndexTermQuery* query, SArray* result) { SIndexTerm* term = query->term; + bool hasJson = INDEX_TYPE_CONTAIN_EXTERN_TYPE(term->colType, TSDB_DATA_TYPE_JSON); EIndexQueryType qtype = query->qType; int ret = -1; // refactor to callback later if (qtype == QUERY_TERM) { uint64_t offset; - FstSlice key = fstSliceCreate(term->colVal, term->nColVal); + char* p = term->colVal; + uint64_t sz = term->nColVal; + if (hasJson) { + p = indexPackJsonData(term); + sz = strlen(p); + } + FstSlice key = fstSliceCreate(p, sz); if (fstGet(reader->fst, &key, &offset)) { indexInfo("index: %" PRIu64 ", col: %s, colVal: %s, found table info in tindex", term->suid, term->colName, term->colVal); @@ -202,6 +210,9 @@ int tfileReaderSearch(TFileReader* reader, SIndexTermQuery* query, SArray* resul term->colVal); } fstSliceDestroy(&key); + if (hasJson) { + free(p); + } } else if (qtype == QUERY_PREFIX) { // handle later // diff --git a/source/libs/index/test/fstUT.cc b/source/libs/index/test/fstUT.cc index b34fd71e9c..3bc3b6da6a 100644 --- a/source/libs/index/test/fstUT.cc +++ b/source/libs/index/test/fstUT.cc @@ -238,3 +238,19 @@ TEST_F(FstEnv, writeNormal) { assert(fst->Search(ctx, rlt) == true); } TEST_F(FstEnv, WriteMillonrRecord) {} +TEST_F(FstEnv, writeAbNormal) { + fst->CreateWriter(); + std::string str1("voltage&\b&ab"); + std::string str2("voltbge&\b&ab"); + + fst->Put(str1, 1); + fst->Put(str2, 2); + + fst->DestroyWriter(); + + fst->CreateReader(); + uint64_t val; + assert(fst->Get("1", &val) == false); + assert(fst->Get("voltage&\b&ab", &val) == true); + assert(val == 1); +} diff --git a/source/libs/index/test/jsonDemo.cc b/source/libs/index/test/jsonDemo.cc deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/source/libs/index/test/jsonUT.cc b/source/libs/index/test/jsonUT.cc index 20b59add9f..e184bc49ae 100644 --- a/source/libs/index/test/jsonUT.cc +++ b/source/libs/index/test/jsonUT.cc @@ -21,6 +21,8 @@ class JsonEnv : public ::testing::Test { protected: virtual void SetUp() { taosRemoveDir(dir.c_str()); + taosMkDir(dir.c_str()); + opts = indexOptsCreate(); int ret = tIndexJsonOpen(opts, dir.c_str(), &index); assert(ret == 0); @@ -87,6 +89,33 @@ TEST_F(JsonEnv, testWrite) { assert(100 == taosArrayGetSize(result)); indexMultiTermQueryDestroy(mq); } - - // SIndexTermQuery query = {.term = term, .qType = QUERY_TERM}; +} +TEST_F(JsonEnv, testWriteMillonData) { + { + std::string colName("voltagefdadfa"); + std::string colVal("abxxxxxxxxxxxx"); + SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), + colVal.c_str(), colVal.size()); + + SIndexMultiTerm* terms = indexMultiTermCreate(); + indexMultiTermAdd(terms, term); + for (size_t i = 0; i < 1000000; i++) { + tIndexJsonPut(index, terms, i); + } + indexMultiTermDestroy(terms); + } + { + std::string colName("voltage"); + std::string colVal("ab"); + + SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST); + SIndexTerm* q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), + colVal.c_str(), colVal.size()); + + SArray* result = taosArrayInit(1, sizeof(uint64_t)); + indexMultiTermQueryAdd(mq, q, QUERY_TERM); + tIndexJsonSearch(index, mq, result); + assert(100 == taosArrayGetSize(result)); + indexMultiTermQueryDestroy(mq); + } } diff --git a/source/libs/transport/src/transSrv.c b/source/libs/transport/src/transSrv.c index f0db054797..c7b6ca2a2c 100644 --- a/source/libs/transport/src/transSrv.c +++ b/source/libs/transport/src/transSrv.c @@ -286,15 +286,17 @@ void uvOnWriteCb(uv_write_t* req, int status) { transClearBuffer(&conn->readBuf); if (status == 0) { tTrace("server conn %p data already was written on stream", conn); - assert(taosArrayGetSize(conn->srvMsgs) >= 1); - SSrvMsg* msg = taosArrayGetP(conn->srvMsgs, 0); - taosArrayRemove(conn->srvMsgs, 0); - destroySmsg(msg); + if (conn->srvMsgs != NULL) { + assert(taosArrayGetSize(conn->srvMsgs) >= 1); + SSrvMsg* msg = taosArrayGetP(conn->srvMsgs, 0); + taosArrayRemove(conn->srvMsgs, 0); + destroySmsg(msg); - // send second data, just use for push - if (taosArrayGetSize(conn->srvMsgs) > 0) { - msg = (SSrvMsg*)taosArrayGetP(conn->srvMsgs, 0); - uvStartSendRespInternal(msg); + // send second data, just use for push + if (taosArrayGetSize(conn->srvMsgs) > 0) { + msg = (SSrvMsg*)taosArrayGetP(conn->srvMsgs, 0); + uvStartSendRespInternal(msg); + } } } else { tError("server conn %p failed to write data, %s", conn, uv_err_name(status)); @@ -615,7 +617,7 @@ static void destroyConn(SSrvConn* conn, bool clear) { SSrvMsg* msg = taosArrayGetP(conn->srvMsgs, i); destroySmsg(msg); } - taosArrayDestroy(conn->srvMsgs); + conn->srvMsgs = taosArrayDestroy(conn->srvMsgs); QUEUE_REMOVE(&conn->queue); if (clear) { From 7986a372e9160594ecf16cdd059a219395d16355 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 3 Mar 2022 11:29:08 +0800 Subject: [PATCH 06/19] monitor --- include/libs/monitor/monitor.h | 9 +- source/dnode/mgmt/impl/inc/dndEnv.h | 3 + source/dnode/mgmt/impl/src/dndEnv.c | 2 + source/dnode/mgmt/impl/src/dndMgmt.c | 20 +++- source/libs/monitor/src/monitor.c | 157 ++++++++++++++++++++++++++- 5 files changed, 183 insertions(+), 8 deletions(-) diff --git a/include/libs/monitor/monitor.h b/include/libs/monitor/monitor.h index 4b7c6a9bc9..090fea9dab 100644 --- a/include/libs/monitor/monitor.h +++ b/include/libs/monitor/monitor.h @@ -57,7 +57,6 @@ typedef struct { typedef struct { int32_t dnode_id; - int8_t vnode_online; char vnode_role[8]; } SMonVnodeDesc; @@ -69,7 +68,7 @@ typedef struct { typedef struct { char database_name[TSDB_DB_NAME_LEN]; int32_t tables_num; - int8_t status; + char status[10]; SArray *vgroups; // array of SMonVgroupDesc } SMonVgroupInfo; @@ -107,7 +106,7 @@ typedef struct { int32_t errors; int32_t vnodes_num; int32_t masters; - int32_t has_mnode; + int8_t has_mnode; } SMonDnodeInfo; typedef struct { @@ -117,7 +116,9 @@ typedef struct { } SMonDiskDesc; typedef struct { - SArray *disks; // array of SMonDiskDesc + SArray *datadirs; // array of SMonDiskDesc + SMonDiskDesc logdir; + SMonDiskDesc tempdir; } SMonDiskInfo; typedef struct { diff --git a/source/dnode/mgmt/impl/inc/dndEnv.h b/source/dnode/mgmt/impl/inc/dndEnv.h index cbd5eb5827..724ba30155 100644 --- a/source/dnode/mgmt/impl/inc/dndEnv.h +++ b/source/dnode/mgmt/impl/inc/dndEnv.h @@ -137,6 +137,9 @@ typedef struct SDnode { SStartupReq startup; } SDnode; + +int32_t dndGetDiskInfo(SDnode *pDnode, SMonDiskInfo *pInfo); + #ifdef __cplusplus } #endif diff --git a/source/dnode/mgmt/impl/src/dndEnv.c b/source/dnode/mgmt/impl/src/dndEnv.c index 2539443b41..ae08d6387c 100644 --- a/source/dnode/mgmt/impl/src/dndEnv.c +++ b/source/dnode/mgmt/impl/src/dndEnv.c @@ -323,3 +323,5 @@ void dndCleanup() { taosStopCacheRefreshWorker(); dInfo("dnode env is cleaned up"); } + +int32_t dndGetDiskInfo(SDnode *pDnode, SMonDiskInfo *pInfo) { return 0; } \ No newline at end of file diff --git a/source/dnode/mgmt/impl/src/dndMgmt.c b/source/dnode/mgmt/impl/src/dndMgmt.c index 4bf8b82043..afad4b87bc 100644 --- a/source/dnode/mgmt/impl/src/dndMgmt.c +++ b/source/dnode/mgmt/impl/src/dndMgmt.c @@ -474,11 +474,14 @@ void dndProcessStartupReq(SDnode *pDnode, SRpcMsg *pReq) { rpcSendResponse(&rpcRsp); } -void dndGetBasicInfo(SDnode *pDnode, SMonBasicInfo *pInfo) { +static int32_t dndGetBasicInfo(SDnode *pDnode, SMonBasicInfo *pInfo) { pInfo->dnode_id = dndGetDnodeId(pDnode); tstrncpy(pInfo->dnode_ep, tsLocalEp, TSDB_EP_LEN); + return 0; } +static int32_t dndGetDnodeInfo(SDnode *pDnode, SMonDnodeInfo *pInfo) { return 0; } + static void dndSendMonitorReport(SDnode *pDnode) { if (!tsEnableMonitor || tsMonitorFqdn[0] == 0) return; SMonInfo *pMonitor = monCreateMonitorInfo(); @@ -487,8 +490,9 @@ static void dndSendMonitorReport(SDnode *pDnode) { dTrace("pDnode:%p, send monitor report to %s:%u", pDnode, tsMonitorFqdn, tsMonitorPort); SMonBasicInfo basicInfo = {0}; - dndGetBasicInfo(pDnode, &basicInfo); - monSetBasicInfo(pMonitor, &basicInfo); + if (dndGetBasicInfo(pDnode, &basicInfo) == 0) { + monSetBasicInfo(pMonitor, &basicInfo); + } SMonClusterInfo clusterInfo = {0}; SMonVgroupInfo vgroupInfo = {0}; @@ -499,6 +503,16 @@ static void dndSendMonitorReport(SDnode *pDnode) { monSetGrantInfo(pMonitor, &grantInfo); } + SMonDnodeInfo dnodeInfo = {0}; + if (dndGetDnodeInfo(pDnode, &dnodeInfo) == 0) { + monSetDnodeInfo(pMonitor, &dnodeInfo); + } + + SMonDiskInfo diskInfo = {0}; + if (dndGetDiskInfo(pDnode, &diskInfo) == 0) { + monSetDiskInfo(pMonitor, &diskInfo); + } + monSendReport(pMonitor); monCleanupMonitorInfo(pMonitor); } diff --git a/source/libs/monitor/src/monitor.c b/source/libs/monitor/src/monitor.c index 811ee40dc8..67605432ac 100644 --- a/source/libs/monitor/src/monitor.c +++ b/source/libs/monitor/src/monitor.c @@ -97,21 +97,176 @@ void monSetBasicInfo(SMonInfo *pMonitor, SMonBasicInfo *pInfo) { } void monSetClusterInfo(SMonInfo *pMonitor, SMonClusterInfo *pInfo) { + SJson *pParentJson = pMonitor->pJson; + SJson *pJson = tjsonCreateObject(); + if (pJson == NULL) return; + if (tjsonAddItemToObject(pParentJson, "cluster_info", pJson) != 0) return; + tjsonAddStringToObject(pJson, "first_ep", pInfo->first_ep); + tjsonAddDoubleToObject(pJson, "first_ep_dnode_id", pInfo->first_ep_dnode_id); + tjsonAddStringToObject(pJson, "version", pInfo->version); + tjsonAddDoubleToObject(pJson, "master_uptime", pInfo->master_uptime); + tjsonAddDoubleToObject(pJson, "monitor_interval", pInfo->monitor_interval); + tjsonAddDoubleToObject(pJson, "vgroups_total", pInfo->vgroups_total); + tjsonAddDoubleToObject(pJson, "vgroups_alive", pInfo->vgroups_alive); + tjsonAddDoubleToObject(pJson, "vnodes_total", pInfo->vnodes_total); + tjsonAddDoubleToObject(pJson, "vnodes_alive", pInfo->vnodes_alive); + tjsonAddDoubleToObject(pJson, "connections_total", pInfo->connections_total); + + SJson *pDnodesJson = tjsonAddArrayToObject(pJson, "dnodes"); + if (pDnodesJson == NULL) return; + + for (int32_t i = 0; i < taosArrayGetSize(pInfo->dnodes); ++i) { + SJson *pDnodeJson = tjsonCreateObject(); + if (pDnodeJson == NULL) continue; + + SMonDnodeDesc *pDnodeDesc = taosArrayGet(pInfo->dnodes, i); + if (tjsonAddDoubleToObject(pDnodesJson, "dnode_id", pDnodeDesc->dnode_id) != 0) tjsonDelete(pDnodeJson); + if (tjsonAddStringToObject(pDnodesJson, "dnode_ep", pDnodeDesc->dnode_ep) != 0) tjsonDelete(pDnodeJson); + if (tjsonAddStringToObject(pDnodesJson, "status", pDnodeDesc->status) != 0) tjsonDelete(pDnodeJson); + + if (tjsonAddItemToArray(pDnodesJson, pDnodeJson) != 0) tjsonDelete(pDnodeJson); + } + + SJson *pMnodesJson = tjsonAddArrayToObject(pJson, "mnodes"); + if (pMnodesJson == NULL) return; + + for (int32_t i = 0; i < taosArrayGetSize(pInfo->dnodes); ++i) { + SJson *pMnodeJson = tjsonCreateObject(); + if (pMnodeJson == NULL) continue; + + SMonMnodeDesc *pMnodeDesc = taosArrayGet(pInfo->dnodes, i); + if (tjsonAddDoubleToObject(pMnodesJson, "mnode_id", pMnodeDesc->mnode_id) != 0) tjsonDelete(pMnodeJson); + if (tjsonAddStringToObject(pMnodesJson, "mnode_ep", pMnodeDesc->mnode_ep) != 0) tjsonDelete(pMnodeJson); + if (tjsonAddStringToObject(pMnodesJson, "role", pMnodeDesc->role) != 0) tjsonDelete(pMnodeJson); + + if (tjsonAddItemToArray(pMnodesJson, pMnodeJson) != 0) tjsonDelete(pMnodeJson); + } } void monSetVgroupInfo(SMonInfo *pMonitor, SMonVgroupInfo *pInfo) { + SJson *pParentJson = pMonitor->pJson; + SJson *pJson = tjsonCreateObject(); + if (pJson == NULL) return; + if (tjsonAddItemToObject(pParentJson, "vgroups_info", pJson) != 0) return; + tjsonAddStringToObject(pJson, "database_name", pInfo->database_name); + tjsonAddDoubleToObject(pJson, "tables_num", pInfo->tables_num); + tjsonAddStringToObject(pJson, "status", pInfo->status); + + SJson *pVgroupsJson = tjsonAddArrayToObject(pJson, "vgroups"); + if (pVgroupsJson == NULL) return; + + for (int32_t i = 0; i < taosArrayGetSize(pInfo->vgroups); ++i) { + SJson *pVgroupJson = tjsonCreateObject(); + if (pVgroupJson == NULL) continue; + + SMonVgroupDesc *pVgroupDesc = taosArrayGet(pInfo->vgroups, i); + if (tjsonAddDoubleToObject(pVgroupJson, "vgroup_id", pVgroupDesc->vgroup_id) != 0) tjsonDelete(pVgroupJson); + + SJson *pVnodesJson = tjsonAddArrayToObject(pVgroupJson, "vnodes"); + if (pVnodesJson == NULL) tjsonDelete(pVgroupJson); + + for (int32_t j = 0; j < TSDB_MAX_REPLICA; ++j) { + SMonVnodeDesc *pVnodeDesc = &pVgroupDesc->vnodes[j]; + if (pVnodeDesc[j].dnode_id <= 0) continue; + + SJson *pVnodeJson = tjsonCreateObject(); + if (pVnodeJson == NULL) continue; + + if (tjsonAddDoubleToObject(pVnodeJson, "dnode_id", pVnodeDesc->dnode_id) != 0) tjsonDelete(pVnodeJson); + if (tjsonAddStringToObject(pVnodeJson, "vnode_role", pVnodeDesc->vnode_role) != 0) tjsonDelete(pVnodeJson); + + if (tjsonAddItemToArray(pVnodesJson, pVnodeJson) != 0) tjsonDelete(pVnodeJson); + } + } } void monSetGrantInfo(SMonInfo *pMonitor, SMonGrantInfo *pInfo) { + SJson *pParentJson = pMonitor->pJson; + SJson *pJson = tjsonCreateObject(); + if (pJson == NULL) return; + if (tjsonAddItemToObject(pParentJson, "grant_info", pJson) != 0) return; + tjsonAddDoubleToObject(pJson, "expire_time", pInfo->expire_time); + tjsonAddDoubleToObject(pJson, "timeseries_used", pInfo->timeseries_used); + tjsonAddDoubleToObject(pJson, "timeseries_total", pInfo->timeseries_total); } void monSetDnodeInfo(SMonInfo *pMonitor, SMonDnodeInfo *pInfo) { + SJson *pParentJson = pMonitor->pJson; + SJson *pJson = tjsonCreateObject(); + if (pJson == NULL) return; + if (tjsonAddItemToObject(pParentJson, "dnode_info", pJson) != 0) return; + tjsonAddDoubleToObject(pJson, "uptime", pInfo->uptime); + tjsonAddDoubleToObject(pJson, "cpu_engine", pInfo->cpu_engine); + tjsonAddDoubleToObject(pJson, "cpu_system", pInfo->cpu_system); + tjsonAddDoubleToObject(pJson, "cpu_cores", pInfo->cpu_cores); + tjsonAddDoubleToObject(pJson, "mem_engine", pInfo->mem_engine); + tjsonAddDoubleToObject(pJson, "mem_system", pInfo->mem_system); + tjsonAddDoubleToObject(pJson, "mem_total", pInfo->mem_total); + tjsonAddDoubleToObject(pJson, "disk_engine", pInfo->disk_engine); + tjsonAddDoubleToObject(pJson, "disk_used", pInfo->disk_used); + tjsonAddDoubleToObject(pJson, "disk_total", pInfo->disk_total); + tjsonAddDoubleToObject(pJson, "net_in", pInfo->net_in); + tjsonAddDoubleToObject(pJson, "net_out", pInfo->net_out); + tjsonAddDoubleToObject(pJson, "io_read", pInfo->io_read); + tjsonAddDoubleToObject(pJson, "io_write", pInfo->io_write); + tjsonAddDoubleToObject(pJson, "io_read_disk", pInfo->io_read_disk); + tjsonAddDoubleToObject(pJson, "io_write_disk", pInfo->io_write_disk); + tjsonAddDoubleToObject(pJson, "req_select", pInfo->req_select); + tjsonAddDoubleToObject(pJson, "req_select_rate", pInfo->req_select_rate); + tjsonAddDoubleToObject(pJson, "req_insert", pInfo->req_insert); + tjsonAddDoubleToObject(pJson, "req_insert_success", pInfo->req_insert_success); + tjsonAddDoubleToObject(pJson, "req_insert_rate", pInfo->req_insert_rate); + tjsonAddDoubleToObject(pJson, "req_insert_batch", pInfo->req_insert_batch); + tjsonAddDoubleToObject(pJson, "req_insert_batch_success", pInfo->req_insert_batch_success); + tjsonAddDoubleToObject(pJson, "req_insert_batch_rate", pInfo->req_insert_batch_rate); + tjsonAddDoubleToObject(pJson, "errors", pInfo->errors); + tjsonAddDoubleToObject(pJson, "vnodes_num", pInfo->vnodes_num); + tjsonAddDoubleToObject(pJson, "masters", pInfo->masters); + tjsonAddDoubleToObject(pJson, "has_mnode", pInfo->has_mnode); } void monSetDiskInfo(SMonInfo *pMonitor, SMonDiskInfo *pInfo) { - + SJson *pParentJson = pMonitor->pJson; + SJson *pJson = tjsonCreateObject(); + if (pJson == NULL) return; + if (tjsonAddItemToObject(pParentJson, "disks_info", pJson) != 0) return; + + SJson *pDatadirsJson = tjsonAddArrayToObject(pJson, "datadirs"); + if (pDatadirsJson == NULL) return; + + for (int32_t i = 0; i < taosArrayGetSize(pInfo->datadirs); ++i) { + SJson *pDatadirJson = tjsonCreateObject(); + if (pDatadirJson == NULL) continue; + + SMonDiskDesc *pDatadirDesc = taosArrayGet(pInfo->datadirs, i); + if (tjsonAddStringToObject(pDatadirJson, "name", pDatadirDesc->name) != 0) tjsonDelete(pDatadirJson); + if (tjsonAddDoubleToObject(pDatadirJson, "level", pDatadirDesc->level) != 0) tjsonDelete(pDatadirJson); + if (tjsonAddDoubleToObject(pDatadirJson, "avail", pDatadirDesc->size.avail) != 0) tjsonDelete(pDatadirJson); + if (tjsonAddDoubleToObject(pDatadirJson, "used", pDatadirDesc->size.used) != 0) tjsonDelete(pDatadirJson); + if (tjsonAddDoubleToObject(pDatadirJson, "total", pDatadirDesc->size.total) != 0) tjsonDelete(pDatadirJson); + + if (tjsonAddItemToArray(pDatadirsJson, pDatadirJson) != 0) tjsonDelete(pDatadirJson); + } + + SJson *pLogdirJson = tjsonCreateObject(); + if (pLogdirJson == NULL) return; + if (tjsonAddItemToObject(pJson, "logdir", pLogdirJson) != 0) return; + tjsonAddStringToObject(pLogdirJson, "name", pInfo->logdir.name); + tjsonAddDoubleToObject(pLogdirJson, "level", pInfo->logdir.level); + tjsonAddDoubleToObject(pLogdirJson, "avail", pInfo->logdir.size.avail); + tjsonAddDoubleToObject(pLogdirJson, "used", pInfo->logdir.size.used); + tjsonAddDoubleToObject(pLogdirJson, "total", pInfo->logdir.size.total); + + SJson *pTempdirJson = tjsonCreateObject(); + if (pTempdirJson == NULL) return; + if (tjsonAddItemToObject(pJson, "tempdir", pTempdirJson) != 0) return; + tjsonAddStringToObject(pTempdirJson, "name", pInfo->tempdir.name); + tjsonAddDoubleToObject(pTempdirJson, "level", pInfo->tempdir.level); + tjsonAddDoubleToObject(pTempdirJson, "avail", pInfo->tempdir.size.avail); + tjsonAddDoubleToObject(pTempdirJson, "used", pInfo->tempdir.size.used); + tjsonAddDoubleToObject(pTempdirJson, "total", pInfo->tempdir.size.total); } From 76c4fce8d03c0201ea71bfb5aa42c774b6ea86f9 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Thu, 3 Mar 2022 11:37:19 +0800 Subject: [PATCH 07/19] ping test --- source/libs/sync/src/syncIO.c | 14 ++++-------- source/libs/sync/src/syncMain.c | 10 --------- source/libs/sync/test/syncPingTest.cpp | 31 ++++++++++++++++---------- 3 files changed, 23 insertions(+), 32 deletions(-) diff --git a/source/libs/sync/src/syncIO.c b/source/libs/sync/src/syncIO.c index 8d04b5bb26..3ba145a96b 100644 --- a/source/libs/sync/src/syncIO.c +++ b/source/libs/sync/src/syncIO.c @@ -211,23 +211,17 @@ static void *syncIOConsumerFunc(void *param) { if (pRpcMsg->msgType == SYNC_PING) { if (io->FpOnSyncPing != NULL) { SyncPing *pSyncMsg; - - SRpcMsg tmpRpcMsg; - memcpy(&tmpRpcMsg, pRpcMsg, sizeof(SRpcMsg)); - pSyncMsg = syncPingBuild(tmpRpcMsg.contLen); - + pSyncMsg = syncPingBuild(pRpcMsg->contLen); syncPingFromRpcMsg(pRpcMsg, pSyncMsg); - // memcpy(pSyncMsg, tmpRpcMsg.pCont, tmpRpcMsg.contLen); - io->FpOnSyncPing(io->pSyncNode, pSyncMsg); } } else if (pRpcMsg->msgType == SYNC_PING_REPLY) { - SyncPingReply *pSyncMsg = syncPingReplyBuild(pRpcMsg->contLen); - syncPingReplyFromRpcMsg(pRpcMsg, pSyncMsg); - if (io->FpOnSyncPingReply != NULL) { + SyncPingReply *pSyncMsg; + pSyncMsg = syncPingReplyBuild(pRpcMsg->contLen); + syncPingReplyFromRpcMsg(pRpcMsg, pSyncMsg); io->FpOnSyncPingReply(io->pSyncNode, pSyncMsg); } } else { diff --git a/source/libs/sync/src/syncMain.c b/source/libs/sync/src/syncMain.c index 49fac038da..9cb3a61fff 100644 --- a/source/libs/sync/src/syncMain.c +++ b/source/libs/sync/src/syncMain.c @@ -171,16 +171,6 @@ static int32_t syncNodePing(SSyncNode* pSyncNode, const SRaftId* destRaftId, Syn SRpcMsg rpcMsg; syncPing2RpcMsg(pMsg, &rpcMsg); - - /* - SRpcMsg rpcMsg; - rpcMsg.contLen = 64; - rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen); - snprintf((char*)rpcMsg.pCont, rpcMsg.contLen, "%s", "xxxxxxxxxxxxxx"); - rpcMsg.handle = NULL; - rpcMsg.msgType = 1; - */ - syncNodeSendMsgById(destRaftId, pSyncNode, &rpcMsg); { diff --git a/source/libs/sync/test/syncPingTest.cpp b/source/libs/sync/test/syncPingTest.cpp index 24d9ead5e3..8268128347 100644 --- a/source/libs/sync/test/syncPingTest.cpp +++ b/source/libs/sync/test/syncPingTest.cpp @@ -13,7 +13,9 @@ void logTest() { sFatal("--- sync log test: fatal"); } -SSyncNode* doSync() { +uint16_t ports[3] = {7010, 7110, 7210}; + +SSyncNode* doSync(int myIndex) { SSyncFSM* pFsm; SSyncInfo syncInfo; @@ -24,18 +26,18 @@ SSyncNode* doSync() { snprintf(syncInfo.path, sizeof(syncInfo.path), "%s", "./test_sync_ping"); SSyncCfg* pCfg = &syncInfo.syncCfg; - pCfg->myIndex = 0; - pCfg->replicaNum = 2; + pCfg->myIndex = myIndex; + pCfg->replicaNum = 3; - pCfg->nodeInfo[0].nodePort = 7010; + pCfg->nodeInfo[0].nodePort = ports[0]; snprintf(pCfg->nodeInfo[0].nodeFqdn, sizeof(pCfg->nodeInfo[0].nodeFqdn), "%s", "127.0.0.1"); // taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn); - pCfg->nodeInfo[1].nodePort = 7110; + pCfg->nodeInfo[1].nodePort = ports[1]; snprintf(pCfg->nodeInfo[1].nodeFqdn, sizeof(pCfg->nodeInfo[1].nodeFqdn), "%s", "127.0.0.1"); // taosGetFqdn(pCfg->nodeInfo[1].nodeFqdn); - pCfg->nodeInfo[2].nodePort = 7210; + pCfg->nodeInfo[2].nodePort = ports[2]; snprintf(pCfg->nodeInfo[2].nodeFqdn, sizeof(pCfg->nodeInfo[2].nodeFqdn), "%s", "127.0.0.1"); // taosGetFqdn(pCfg->nodeInfo[2].nodeFqdn); @@ -53,20 +55,25 @@ void timerPingAll(void* param, void* tmrId) { syncNodePingAll(pSyncNode); } -int main() { +int main(int argc, char** argv) { // taosInitLog((char*)"syncPingTest.log", 100000, 10); tsAsyncLog = 0; sDebugFlag = 143 + 64; logTest(); - int32_t ret = syncIOStart((char*)"127.0.0.1", 7010); + int myIndex = 0; + if (argc >= 2) { + myIndex = atoi(argv[1]); + } + + int32_t ret = syncIOStart((char*)"127.0.0.1", ports[myIndex]); assert(ret == 0); ret = syncEnvStart(); assert(ret == 0); - SSyncNode* pSyncNode = doSync(); + SSyncNode* pSyncNode = doSync(myIndex); gSyncIO->FpOnSyncPing = pSyncNode->FpOnPing; gSyncIO->FpOnSyncPingReply = pSyncNode->FpOnPingReply; @@ -74,9 +81,9 @@ int main() { assert(ret == 0); /* - taosMsleep(10000); - ret = syncNodeStopPingTimer(pSyncNode); - assert(ret == 0); + taosMsleep(10000); + ret = syncNodeStopPingTimer(pSyncNode); + assert(ret == 0); */ while (1) { From aec7bef25b05afb69696533bafe00e7b53210c0c Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Thu, 3 Mar 2022 12:29:54 +0800 Subject: [PATCH 08/19] add UT --- source/libs/index/src/index_tfile.c | 3 +++ source/libs/index/test/fstTest.cc | 23 +++++++++++++---------- source/libs/index/test/jsonUT.cc | 22 ++++++++++++++++++---- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/source/libs/index/src/index_tfile.c b/source/libs/index/src/index_tfile.c index a2089e8eee..0947c796b2 100644 --- a/source/libs/index/src/index_tfile.c +++ b/source/libs/index/src/index_tfile.c @@ -275,6 +275,7 @@ int tfileWriterPut(TFileWriter* tw, void* data, bool order) { __compar_fn_t fn; int8_t colType = tw->header.colType; + colType = INDEX_TYPE_GET_TYPE(colType); if (colType == TSDB_DATA_TYPE_BINARY || colType == TSDB_DATA_TYPE_NCHAR) { fn = tfileStrCompare; } else { @@ -572,6 +573,8 @@ static int tfileWriteHeader(TFileWriter* writer) { static int tfileWriteData(TFileWriter* write, TFileValue* tval) { TFileHeader* header = &write->header; uint8_t colType = header->colType; + + colType = INDEX_TYPE_GET_TYPE(colType); if (colType == TSDB_DATA_TYPE_BINARY || colType == TSDB_DATA_TYPE_NCHAR) { FstSlice key = fstSliceCreate((uint8_t*)(tval->colVal), (size_t)strlen(tval->colVal)); if (fstBuilderInsert(write->fb, key, tval->offset)) { diff --git a/source/libs/index/test/fstTest.cc b/source/libs/index/test/fstTest.cc index 65118a2bce..74dcec4490 100644 --- a/source/libs/index/test/fstTest.cc +++ b/source/libs/index/test/fstTest.cc @@ -312,15 +312,18 @@ void validateTFile(char* arg) { tfCleanup(); } -void iterTFileReader(char* path, char* ver) { +void iterTFileReader(char* path, char* uid, char* colName, char* ver) { tfInit(); - int version = atoi(ver); - TFileReader* reader = tfileReaderOpen(path, 0, version, "tag1"); - Iterate* iter = tfileIteratorCreate(reader); - bool tn = iter ? iter->next(iter) : false; - int count = 0; - int termCount = 0; + uint64_t suid = atoi(uid); + int version = atoi(ver); + + TFileReader* reader = tfileReaderOpen(path, suid, version, colName); + + Iterate* iter = tfileIteratorCreate(reader); + bool tn = iter ? iter->next(iter) : false; + int count = 0; + int termCount = 0; while (tn == true) { count++; IterateValue* cv = iter->getValue(iter); @@ -337,9 +340,9 @@ void iterTFileReader(char* path, char* ver) { int main(int argc, char* argv[]) { // tool to check all kind of fst test // if (argc > 1) { validateTFile(argv[1]); } - if (argc > 2) { - // opt - iterTFileReader(argv[1], argv[2]); + if (argc > 4) { + // path suid colName ver + iterTFileReader(argv[1], argv[2], argv[3], argv[4]); } // checkFstCheckIterator(); // checkFstLongTerm(); diff --git a/source/libs/index/test/jsonUT.cc b/source/libs/index/test/jsonUT.cc index e184bc49ae..e5c79d137f 100644 --- a/source/libs/index/test/jsonUT.cc +++ b/source/libs/index/test/jsonUT.cc @@ -22,7 +22,7 @@ class JsonEnv : public ::testing::Test { virtual void SetUp() { taosRemoveDir(dir.c_str()); taosMkDir(dir.c_str()); - + printf("set up\n"); opts = indexOptsCreate(); int ret = tIndexJsonOpen(opts, dir.c_str(), &index); assert(ret == 0); @@ -30,6 +30,7 @@ class JsonEnv : public ::testing::Test { virtual void TearDown() { tIndexJsonClose(index); indexOptsDestroy(opts); + printf("destory\n"); } SIndexJsonOpts* opts; SIndexJson* index; @@ -37,7 +38,7 @@ class JsonEnv : public ::testing::Test { TEST_F(JsonEnv, testWrite) { { - std::string colName("voltage"); + std::string colName("test"); std::string colVal("ab"); SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size()); @@ -76,7 +77,7 @@ TEST_F(JsonEnv, testWrite) { indexMultiTermDestroy(terms); } { - std::string colName("voltage"); + std::string colName("test"); std::string colVal("ab"); SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST); @@ -91,6 +92,19 @@ TEST_F(JsonEnv, testWrite) { } } TEST_F(JsonEnv, testWriteMillonData) { + { + std::string colName("test"); + std::string colVal("ab"); + SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), + colVal.c_str(), colVal.size()); + + SIndexMultiTerm* terms = indexMultiTermCreate(); + indexMultiTermAdd(terms, term); + for (size_t i = 0; i < 100; i++) { + tIndexJsonPut(index, terms, i); + } + indexMultiTermDestroy(terms); + } { std::string colName("voltagefdadfa"); std::string colVal("abxxxxxxxxxxxx"); @@ -105,7 +119,7 @@ TEST_F(JsonEnv, testWriteMillonData) { indexMultiTermDestroy(terms); } { - std::string colName("voltage"); + std::string colName("test"); std::string colVal("ab"); SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST); From 1fe0c21fd22ab64be6c6afa75c0c49cf191c9ea8 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 3 Mar 2022 13:48:13 +0800 Subject: [PATCH 09/19] monitor --- source/dnode/mgmt/impl/src/dndMgmt.c | 6 +- source/libs/monitor/src/monitor.c | 118 +++++++++++++++++++++------ 2 files changed, 97 insertions(+), 27 deletions(-) diff --git a/source/dnode/mgmt/impl/src/dndMgmt.c b/source/dnode/mgmt/impl/src/dndMgmt.c index afad4b87bc..268ed7e362 100644 --- a/source/dnode/mgmt/impl/src/dndMgmt.c +++ b/source/dnode/mgmt/impl/src/dndMgmt.c @@ -483,12 +483,12 @@ static int32_t dndGetBasicInfo(SDnode *pDnode, SMonBasicInfo *pInfo) { static int32_t dndGetDnodeInfo(SDnode *pDnode, SMonDnodeInfo *pInfo) { return 0; } static void dndSendMonitorReport(SDnode *pDnode) { - if (!tsEnableMonitor || tsMonitorFqdn[0] == 0) return; + if (!tsEnableMonitor || tsMonitorFqdn[0] == 0 || tsMonitorPort == 0) return; + dTrace("pDnode:%p, send monitor report to %s:%u", pDnode, tsMonitorFqdn, tsMonitorPort); + SMonInfo *pMonitor = monCreateMonitorInfo(); if (pMonitor == NULL) return; - dTrace("pDnode:%p, send monitor report to %s:%u", pDnode, tsMonitorFqdn, tsMonitorPort); - SMonBasicInfo basicInfo = {0}; if (dndGetBasicInfo(pDnode, &basicInfo) == 0) { monSetBasicInfo(pMonitor, &basicInfo); diff --git a/source/libs/monitor/src/monitor.c b/source/libs/monitor/src/monitor.c index 67605432ac..7ba65b87b0 100644 --- a/source/libs/monitor/src/monitor.c +++ b/source/libs/monitor/src/monitor.c @@ -54,7 +54,10 @@ void monAddLogItem(SMonLogItem *pItem) { SMonInfo *monCreateMonitorInfo() { SMonInfo *pMonitor = calloc(1, sizeof(SMonInfo)); - if (pMonitor == NULL) return NULL; + if (pMonitor == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return NULL; + } taosWLockLatch(&tsMonitor.lock); pMonitor->logs = taosArrayDup(tsMonitor.logs); @@ -77,30 +80,24 @@ void monCleanupMonitorInfo(SMonInfo *pMonitor) { free(pMonitor); } -void monSendReport(SMonInfo *pMonitor) { - char *pCont = tjsonToString(pMonitor->pJson); - if (pCont != NULL) { - taosSendHttpReport(tsMonitor.server, tsMonitor.port, pCont, strlen(pCont)); - free(pCont); - } -} - void monSetBasicInfo(SMonInfo *pMonitor, SMonBasicInfo *pInfo) { - SJson *pJson = pMonitor->pJson; - tjsonAddDoubleToObject(pJson, "dnode_id", pInfo->dnode_id); - tjsonAddStringToObject(pJson, "dnode_ep", pInfo->dnode_ep); - + SJson *pJson = pMonitor->pJson; int64_t ms = taosGetTimestampMs(); char buf[40] = {0}; taosFormatUtcTime(buf, sizeof(buf), ms, TSDB_TIME_PRECISION_MILLI); + tjsonAddStringToObject(pJson, "ts", buf); + tjsonAddDoubleToObject(pJson, "dnode_id", pInfo->dnode_id); + tjsonAddStringToObject(pJson, "dnode_ep", pInfo->dnode_ep); } void monSetClusterInfo(SMonInfo *pMonitor, SMonClusterInfo *pInfo) { - SJson *pParentJson = pMonitor->pJson; SJson *pJson = tjsonCreateObject(); if (pJson == NULL) return; - if (tjsonAddItemToObject(pParentJson, "cluster_info", pJson) != 0) return; + if (tjsonAddItemToObject(pMonitor->pJson, "cluster_info", pJson) != 0) { + tjsonDelete(pJson); + return; + } tjsonAddStringToObject(pJson, "first_ep", pInfo->first_ep); tjsonAddDoubleToObject(pJson, "first_ep_dnode_id", pInfo->first_ep_dnode_id); @@ -145,10 +142,12 @@ void monSetClusterInfo(SMonInfo *pMonitor, SMonClusterInfo *pInfo) { } void monSetVgroupInfo(SMonInfo *pMonitor, SMonVgroupInfo *pInfo) { - SJson *pParentJson = pMonitor->pJson; SJson *pJson = tjsonCreateObject(); if (pJson == NULL) return; - if (tjsonAddItemToObject(pParentJson, "vgroups_info", pJson) != 0) return; + if (tjsonAddItemToObject(pMonitor->pJson, "vgroup_infos", pJson) != 0) { + tjsonDelete(pJson); + return; + } tjsonAddStringToObject(pJson, "database_name", pInfo->database_name); tjsonAddDoubleToObject(pJson, "tables_num", pInfo->tables_num); @@ -183,10 +182,12 @@ void monSetVgroupInfo(SMonInfo *pMonitor, SMonVgroupInfo *pInfo) { } void monSetGrantInfo(SMonInfo *pMonitor, SMonGrantInfo *pInfo) { - SJson *pParentJson = pMonitor->pJson; SJson *pJson = tjsonCreateObject(); if (pJson == NULL) return; - if (tjsonAddItemToObject(pParentJson, "grant_info", pJson) != 0) return; + if (tjsonAddItemToObject(pMonitor->pJson, "grant_info", pJson) != 0) { + tjsonDelete(pJson); + return; + } tjsonAddDoubleToObject(pJson, "expire_time", pInfo->expire_time); tjsonAddDoubleToObject(pJson, "timeseries_used", pInfo->timeseries_used); @@ -194,10 +195,12 @@ void monSetGrantInfo(SMonInfo *pMonitor, SMonGrantInfo *pInfo) { } void monSetDnodeInfo(SMonInfo *pMonitor, SMonDnodeInfo *pInfo) { - SJson *pParentJson = pMonitor->pJson; SJson *pJson = tjsonCreateObject(); if (pJson == NULL) return; - if (tjsonAddItemToObject(pParentJson, "dnode_info", pJson) != 0) return; + if (tjsonAddItemToObject(pMonitor->pJson, "dnode_info", pJson) != 0) { + tjsonDelete(pJson); + return; + } tjsonAddDoubleToObject(pJson, "uptime", pInfo->uptime); tjsonAddDoubleToObject(pJson, "cpu_engine", pInfo->cpu_engine); @@ -230,12 +233,14 @@ void monSetDnodeInfo(SMonInfo *pMonitor, SMonDnodeInfo *pInfo) { } void monSetDiskInfo(SMonInfo *pMonitor, SMonDiskInfo *pInfo) { - SJson *pParentJson = pMonitor->pJson; SJson *pJson = tjsonCreateObject(); if (pJson == NULL) return; - if (tjsonAddItemToObject(pParentJson, "disks_info", pJson) != 0) return; + if (tjsonAddItemToObject(pMonitor->pJson, "disks_infos", pJson) != 0) { + tjsonDelete(pJson); + return; + } - SJson *pDatadirsJson = tjsonAddArrayToObject(pJson, "datadirs"); + SJson *pDatadirsJson = tjsonAddArrayToObject(pJson, "datadir"); if (pDatadirsJson == NULL) return; for (int32_t i = 0; i < taosArrayGetSize(pInfo->datadirs); ++i) { @@ -270,3 +275,68 @@ void monSetDiskInfo(SMonInfo *pMonitor, SMonDiskInfo *pInfo) { tjsonAddDoubleToObject(pTempdirJson, "used", pInfo->tempdir.size.used); tjsonAddDoubleToObject(pTempdirJson, "total", pInfo->tempdir.size.total); } + +static void monSetLogInfo(SMonInfo *pMonitor) { + SJson *pJson = tjsonCreateObject(); + if (pJson == NULL) return; + if (tjsonAddItemToObject(pMonitor->pJson, "log_infos", pJson) != 0) { + tjsonDelete(pJson); + return; + } + + SJson *pLogsJson = tjsonAddArrayToObject(pJson, "logs"); + if (pLogsJson == NULL) return; + + for (int32_t i = 0; i < taosArrayGetSize(pMonitor->logs); ++i) { + SJson *pLogJson = tjsonCreateObject(); + if (pLogJson == NULL) continue; + + SMonLogItem *pLogItem = taosArrayGet(pMonitor->logs, i); + + char buf[40] = {0}; + taosFormatUtcTime(buf, sizeof(buf), pLogItem->ts, TSDB_TIME_PRECISION_MILLI); + + if (tjsonAddStringToObject(pLogItem, "ts", buf) != 0) tjsonDelete(pLogJson); + if (tjsonAddDoubleToObject(pLogItem, "level", pLogItem->level) != 0) tjsonDelete(pLogJson); + if (tjsonAddStringToObject(pLogItem, "content", pLogItem->content) != 0) tjsonDelete(pLogJson); + + if (tjsonAddItemToArray(pLogsJson, pLogJson) != 0) tjsonDelete(pLogJson); + } + + SJson *pSummaryJson = tjsonAddArrayToObject(pJson, "summary"); + if (pSummaryJson == NULL) return; + + SJson *pLogError = tjsonCreateObject(); + if (pLogError == NULL) return; + tjsonAddStringToObject(pLogError, "level", "error"); + tjsonAddDoubleToObject(pLogError, "total", 1); + if (tjsonAddItemToArray(pSummaryJson, pLogError) != 0) tjsonDelete(pLogError); + + SJson *pLogInfo = tjsonCreateObject(); + if (pLogInfo == NULL) return; + tjsonAddStringToObject(pLogInfo, "level", "info"); + tjsonAddDoubleToObject(pLogInfo, "total", 1); + if (tjsonAddItemToArray(pSummaryJson, pLogInfo) != 0) tjsonDelete(pLogInfo); + + SJson *pLogDebug = tjsonCreateObject(); + if (pLogDebug == NULL) return; + tjsonAddStringToObject(pLogDebug, "level", "debug"); + tjsonAddDoubleToObject(pLogDebug, "total", 1); + if (tjsonAddItemToArray(pSummaryJson, pLogDebug) != 0) tjsonDelete(pLogDebug); + + SJson *pLogTrace = tjsonCreateObject(); + if (pLogTrace == NULL) return; + tjsonAddStringToObject(pLogTrace, "level", "trace"); + tjsonAddDoubleToObject(pLogTrace, "total", 1); + if (tjsonAddItemToArray(pSummaryJson, pLogTrace) != 0) tjsonDelete(pLogTrace); +} + +void monSendReport(SMonInfo *pMonitor) { + monSetLogInfo(pMonitor); + + char *pCont = tjsonToString(pMonitor->pJson); + if (pCont != NULL) { + taosSendHttpReport(tsMonitor.server, tsMonitor.port, pCont, strlen(pCont)); + free(pCont); + } +} From f263a623dd7186525c28bb79668426158404a71e Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Thu, 3 Mar 2022 14:52:30 +0800 Subject: [PATCH 10/19] sync encode/decode --- source/libs/sync/inc/syncMessage.h | 162 ++++++++------- source/libs/sync/src/syncMessage.c | 245 ++++++++++++++++------- source/libs/sync/test/syncEncodeTest.cpp | 129 +++++++++--- 3 files changed, 368 insertions(+), 168 deletions(-) diff --git a/source/libs/sync/inc/syncMessage.h b/source/libs/sync/inc/syncMessage.h index be53559e8a..a51567d1dd 100644 --- a/source/libs/sync/inc/syncMessage.h +++ b/source/libs/sync/inc/syncMessage.h @@ -28,30 +28,25 @@ extern "C" { #include "syncRaftEntry.h" #include "taosdef.h" -// encode as uint64 +// encode as uint32 typedef enum ESyncMessageType { SYNC_PING = 101, SYNC_PING_REPLY = 103, - SYNC_CLIENT_REQUEST, - SYNC_CLIENT_REQUEST_REPLY, - SYNC_REQUEST_VOTE, - SYNC_REQUEST_VOTE_REPLY, - SYNC_APPEND_ENTRIES, - SYNC_APPEND_ENTRIES_REPLY, + SYNC_CLIENT_REQUEST = 105, + SYNC_CLIENT_REQUEST_REPLY = 107, + SYNC_REQUEST_VOTE = 109, + SYNC_REQUEST_VOTE_REPLY = 111, + SYNC_APPEND_ENTRIES = 113, + SYNC_APPEND_ENTRIES_REPLY = 115, } ESyncMessageType; -/* -typedef struct SRaftId { - SyncNodeId addr; // typedef uint64_t SyncNodeId; - SyncGroupId vgId; // typedef int32_t SyncGroupId; -} SRaftId; -*/ - +// --------------------------------------------- typedef struct SyncPing { uint32_t bytes; uint32_t msgType; SRaftId srcId; SRaftId destId; + // private data uint32_t dataLen; char data[]; } SyncPing; @@ -59,28 +54,22 @@ typedef struct SyncPing { #define SYNC_PING_FIX_LEN (sizeof(uint32_t) + sizeof(uint32_t) + sizeof(SRaftId) + sizeof(SRaftId) + sizeof(uint32_t)) SyncPing* syncPingBuild(uint32_t dataLen); - -void syncPingDestroy(SyncPing* pMsg); - -void syncPingSerialize(const SyncPing* pMsg, char* buf, uint32_t bufLen); - -void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pMsg); - -void syncPing2RpcMsg(const SyncPing* pMsg, SRpcMsg* pRpcMsg); - -void syncPingFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPing* pMsg); - -cJSON* syncPing2Json(const SyncPing* pMsg); - +void syncPingDestroy(SyncPing* pMsg); +void syncPingSerialize(const SyncPing* pMsg, char* buf, uint32_t bufLen); +void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pMsg); +void syncPing2RpcMsg(const SyncPing* pMsg, SRpcMsg* pRpcMsg); +void syncPingFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPing* pMsg); +cJSON* syncPing2Json(const SyncPing* pMsg); SyncPing* syncPingBuild2(const SRaftId* srcId, const SRaftId* destId, const char* str); - SyncPing* syncPingBuild3(const SRaftId* srcId, const SRaftId* destId); +// --------------------------------------------- typedef struct SyncPingReply { uint32_t bytes; uint32_t msgType; SRaftId srcId; SRaftId destId; + // private data uint32_t dataLen; char data[]; } SyncPingReply; @@ -89,72 +78,95 @@ typedef struct SyncPingReply { (sizeof(uint32_t) + sizeof(uint32_t) + sizeof(SRaftId) + sizeof(SRaftId) + sizeof(uint32_t)) SyncPingReply* syncPingReplyBuild(uint32_t dataLen); - -void syncPingReplyDestroy(SyncPingReply* pMsg); - -void syncPingReplySerialize(const SyncPingReply* pMsg, char* buf, uint32_t bufLen); - -void syncPingReplyDeserialize(const char* buf, uint32_t len, SyncPingReply* pMsg); - -void syncPingReply2RpcMsg(const SyncPingReply* pMsg, SRpcMsg* pRpcMsg); - -void syncPingReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPingReply* pMsg); - -cJSON* syncPingReply2Json(const SyncPingReply* pMsg); - +void syncPingReplyDestroy(SyncPingReply* pMsg); +void syncPingReplySerialize(const SyncPingReply* pMsg, char* buf, uint32_t bufLen); +void syncPingReplyDeserialize(const char* buf, uint32_t len, SyncPingReply* pMsg); +void syncPingReply2RpcMsg(const SyncPingReply* pMsg, SRpcMsg* pRpcMsg); +void syncPingReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPingReply* pMsg); +cJSON* syncPingReply2Json(const SyncPingReply* pMsg); SyncPingReply* syncPingReplyBuild2(const SRaftId* srcId, const SRaftId* destId, const char* str); - SyncPingReply* syncPingReplyBuild3(const SRaftId* srcId, const SRaftId* destId); +// --------------------------------------------- typedef struct SyncClientRequest { - ESyncMessageType msgType; - char* data; - uint32_t dataLen; - int64_t seqNum; - bool isWeak; + uint32_t bytes; + uint32_t msgType; + int64_t seqNum; + bool isWeak; + uint32_t dataLen; + char data[]; } SyncClientRequest; +// --------------------------------------------- typedef struct SyncClientRequestReply { - ESyncMessageType msgType; - int32_t errCode; - SSyncBuffer* pErrMsg; - SSyncBuffer* pLeaderHint; + uint32_t bytes; + uint32_t msgType; + int32_t errCode; + SRaftId leaderHint; } SyncClientRequestReply; +// --------------------------------------------- typedef struct SyncRequestVote { - ESyncMessageType msgType; - SyncTerm currentTerm; - SyncNodeId nodeId; - SyncGroupId vgId; - SyncIndex lastLogIndex; - SyncTerm lastLogTerm; + uint32_t bytes; + uint32_t msgType; + SRaftId srcId; + SRaftId destId; + // private data + SyncTerm currentTerm; + SyncIndex lastLogIndex; + SyncTerm lastLogTerm; } SyncRequestVote; +SyncRequestVote* syncRequestVoteBuild(); +void syncRequestVoteDestroy(SyncRequestVote* pMsg); +void syncRequestVoteSerialize(const SyncRequestVote* pMsg, char* buf, uint32_t bufLen); +void syncRequestVoteDeserialize(const char* buf, uint32_t len, SyncRequestVote* pMsg); +void syncRequestVote2RpcMsg(const SyncRequestVote* pMsg, SRpcMsg* pRpcMsg); +void syncRequestVoteFromRpcMsg(const SRpcMsg* pRpcMsg, SyncRequestVote* pMsg); +cJSON* syncRequestVote2Json(const SyncRequestVote* pMsg); + +// --------------------------------------------- typedef struct SyncRequestVoteReply { - ESyncMessageType msgType; - SyncTerm currentTerm; - SyncNodeId nodeId; - SyncGroupId vgId; - bool voteGranted; + uint32_t bytes; + uint32_t msgType; + SRaftId srcId; + SRaftId destId; + // private data + SyncTerm term; + bool voteGranted; } SyncRequestVoteReply; +SyncRequestVoteReply* SyncRequestVoteReplyBuild(); +void syncRequestVoteReplyDestroy(SyncRequestVoteReply* pMsg); +void syncRequestVoteReplySerialize(const SyncRequestVoteReply* pMsg, char* buf, uint32_t bufLen); +void syncRequestVoteReplyDeserialize(const char* buf, uint32_t len, SyncRequestVoteReply* pMsg); +void syncRequestVoteReply2RpcMsg(const SyncRequestVoteReply* pMsg, SRpcMsg* pRpcMsg); +void syncRequestVoteReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncRequestVoteReply* pMsg); +cJSON* syncRequestVoteReply2Json(const SyncRequestVoteReply* pMsg); + +// --------------------------------------------- typedef struct SyncAppendEntries { - ESyncMessageType msgType; - SyncTerm currentTerm; - SyncNodeId nodeId; - SyncIndex prevLogIndex; - SyncTerm prevLogTerm; - int32_t entryCount; - SSyncRaftEntry* logEntries; - SyncIndex commitIndex; + uint32_t bytes; + uint32_t msgType; + SRaftId srcId; + SRaftId destId; + // private data + SyncIndex prevLogIndex; + SyncTerm prevLogTerm; + SyncIndex commitIndex; + uint32_t dataLen; + char data[]; } SyncAppendEntries; +// --------------------------------------------- typedef struct SyncAppendEntriesReply { - ESyncMessageType msgType; - SyncTerm currentTerm; - SyncNodeId nodeId; - bool success; - SyncIndex matchIndex; + uint32_t bytes; + uint32_t msgType; + SRaftId srcId; + SRaftId destId; + // private data + bool success; + SyncIndex matchIndex; } SyncAppendEntriesReply; #ifdef __cplusplus diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index a26c8401b2..ac3f550e3e 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -60,12 +60,15 @@ void syncPingFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPing* pMsg) { } cJSON* syncPing2Json(const SyncPing* pMsg) { + char u64buf[128]; + cJSON* pRoot = cJSON_CreateObject(); cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes); cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType); cJSON* pSrcId = cJSON_CreateObject(); - cJSON_AddNumberToObject(pSrcId, "addr", pMsg->srcId.addr); + snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->srcId.addr); + cJSON_AddStringToObject(pSrcId, "addr", u64buf); { uint64_t u64 = pMsg->srcId.addr; cJSON* pTmp = pSrcId; @@ -79,7 +82,8 @@ cJSON* syncPing2Json(const SyncPing* pMsg) { cJSON_AddItemToObject(pRoot, "srcId", pSrcId); cJSON* pDestId = cJSON_CreateObject(); - cJSON_AddNumberToObject(pDestId, "addr", pMsg->destId.addr); + snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->destId.addr); + cJSON_AddStringToObject(pDestId, "addr", u64buf); { uint64_t u64 = pMsg->destId.addr; cJSON* pTmp = pDestId; @@ -154,12 +158,15 @@ void syncPingReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPingReply* pMsg) { } cJSON* syncPingReply2Json(const SyncPingReply* pMsg) { + char u64buf[128]; + cJSON* pRoot = cJSON_CreateObject(); cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes); cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType); cJSON* pSrcId = cJSON_CreateObject(); - cJSON_AddNumberToObject(pSrcId, "addr", pMsg->srcId.addr); + snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->srcId.addr); + cJSON_AddStringToObject(pSrcId, "addr", u64buf); { uint64_t u64 = pMsg->srcId.addr; cJSON* pTmp = pSrcId; @@ -173,7 +180,8 @@ cJSON* syncPingReply2Json(const SyncPingReply* pMsg) { cJSON_AddItemToObject(pRoot, "srcId", pSrcId); cJSON* pDestId = cJSON_CreateObject(); - cJSON_AddNumberToObject(pDestId, "addr", pMsg->destId.addr); + snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->destId.addr); + cJSON_AddStringToObject(pDestId, "addr", u64buf); { uint64_t u64 = pMsg->destId.addr; cJSON* pTmp = pDestId; @@ -208,72 +216,169 @@ SyncPingReply* syncPingReplyBuild3(const SRaftId* srcId, const SRaftId* destId) return pMsg; } -#if 0 -void syncPingSerialize(const SyncPing* pMsg, char** ppBuf, uint32_t* bufLen) { - *bufLen = sizeof(SyncPing) + pMsg->dataLen; - *ppBuf = (char*)malloc(*bufLen); - void* pStart = *ppBuf; - uint32_t allBytes = *bufLen; - - int len = 0; - len = taosEncodeFixedU32(&pStart, pMsg->msgType); - allBytes -= len; - assert(len > 0); - pStart += len; - - len = taosEncodeFixedU64(&pStart, pMsg->srcId.addr); - allBytes -= len; - assert(len > 0); - pStart += len; - - len = taosEncodeFixedI32(&pStart, pMsg->srcId.vgId); - allBytes -= len; - assert(len > 0); - pStart += len; - - len = taosEncodeFixedU64(&pStart, pMsg->destId.addr); - allBytes -= len; - assert(len > 0); - pStart += len; - - len = taosEncodeFixedI32(&pStart, pMsg->destId.vgId); - allBytes -= len; - assert(len > 0); - pStart += len; - - len = taosEncodeFixedU32(&pStart, pMsg->dataLen); - allBytes -= len; - assert(len > 0); - pStart += len; - - memcpy(pStart, pMsg->data, pMsg->dataLen); - allBytes -= pMsg->dataLen; - assert(allBytes == 0); +// ---- message process SyncRequestVote---- +SyncRequestVote* syncRequestVoteBuild() { + uint32_t bytes = sizeof(SyncRequestVote); + SyncRequestVote* pMsg = malloc(bytes); + memset(pMsg, 0, bytes); + pMsg->bytes = bytes; + pMsg->msgType = SYNC_REQUEST_VOTE; } - -void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pMsg) { - void* pStart = (void*)buf; - uint64_t u64; - int32_t i32; - uint32_t u32; - - pStart = taosDecodeFixedU64(pStart, &u64); - pMsg->msgType = u64; - - pStart = taosDecodeFixedU64(pStart, &u64); - pMsg->srcId.addr = u64; - - pStart = taosDecodeFixedI32(pStart, &i32); - pMsg->srcId.vgId = i32; - - pStart = taosDecodeFixedU64(pStart, &u64); - pMsg->destId.addr = u64; - - pStart = taosDecodeFixedI32(pStart, &i32); - pMsg->destId.vgId = i32; - - pStart = taosDecodeFixedU32(pStart, &u32); - pMsg->dataLen = u32; +void syncRequestVoteDestroy(SyncRequestVote* pMsg) { + if (pMsg != NULL) { + free(pMsg); + } } -#endif \ No newline at end of file + +void syncRequestVoteSerialize(const SyncRequestVote* pMsg, char* buf, uint32_t bufLen) { + assert(pMsg->bytes <= bufLen); + memcpy(buf, pMsg, pMsg->bytes); +} + +void syncRequestVoteDeserialize(const char* buf, uint32_t len, SyncRequestVote* pMsg) { + memcpy(pMsg, buf, len); + assert(len == pMsg->bytes); +} + +void syncRequestVote2RpcMsg(const SyncRequestVote* pMsg, SRpcMsg* pRpcMsg) { + memset(pRpcMsg, 0, sizeof(*pRpcMsg)); + pRpcMsg->msgType = pMsg->msgType; + pRpcMsg->contLen = pMsg->bytes; + pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen); + syncRequestVoteSerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen); +} + +void syncRequestVoteFromRpcMsg(const SRpcMsg* pRpcMsg, SyncRequestVote* pMsg) { + syncRequestVoteDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg); +} + +cJSON* syncRequestVote2Json(const SyncRequestVote* pMsg) { + char u64buf[128]; + + cJSON* pRoot = cJSON_CreateObject(); + cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes); + cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType); + + cJSON* pSrcId = cJSON_CreateObject(); + snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->srcId.addr); + cJSON_AddStringToObject(pSrcId, "addr", u64buf); + { + uint64_t u64 = pMsg->srcId.addr; + cJSON* pTmp = pSrcId; + char host[128]; + uint16_t port; + syncUtilU642Addr(u64, host, sizeof(host), &port); + cJSON_AddStringToObject(pTmp, "addr_host", host); + cJSON_AddNumberToObject(pTmp, "addr_port", port); + } + cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId); + cJSON_AddItemToObject(pRoot, "srcId", pSrcId); + + cJSON* pDestId = cJSON_CreateObject(); + cJSON_AddNumberToObject(pDestId, "addr", pMsg->destId.addr); + { + uint64_t u64 = pMsg->destId.addr; + cJSON* pTmp = pDestId; + char host[128]; + uint16_t port; + syncUtilU642Addr(u64, host, sizeof(host), &port); + cJSON_AddStringToObject(pTmp, "addr_host", host); + cJSON_AddNumberToObject(pTmp, "addr_port", port); + } + cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId); + cJSON_AddItemToObject(pRoot, "destId", pDestId); + + snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->currentTerm); + cJSON_AddStringToObject(pRoot, "currentTerm", u64buf); + snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->lastLogIndex); + cJSON_AddStringToObject(pRoot, "lastLogIndex", u64buf); + snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->lastLogTerm); + cJSON_AddStringToObject(pRoot, "lastLogTerm", u64buf); + + cJSON* pJson = cJSON_CreateObject(); + cJSON_AddItemToObject(pJson, "SyncRequestVote", pRoot); + return pJson; +} + +// ---- message process SyncRequestVoteReply---- +SyncRequestVoteReply* SyncRequestVoteReplyBuild() { + uint32_t bytes = sizeof(SyncRequestVoteReply); + SyncRequestVoteReply* pMsg = malloc(bytes); + memset(pMsg, 0, bytes); + pMsg->bytes = bytes; + pMsg->msgType = SYNC_REQUEST_VOTE_REPLY; +} + +void syncRequestVoteReplyDestroy(SyncRequestVoteReply* pMsg) { + if (pMsg != NULL) { + free(pMsg); + } +} + +void syncRequestVoteReplySerialize(const SyncRequestVoteReply* pMsg, char* buf, uint32_t bufLen) { + assert(pMsg->bytes <= bufLen); + memcpy(buf, pMsg, pMsg->bytes); +} + +void syncRequestVoteReplyDeserialize(const char* buf, uint32_t len, SyncRequestVoteReply* pMsg) { + memcpy(pMsg, buf, len); + assert(len == pMsg->bytes); +} + +void syncRequestVoteReply2RpcMsg(const SyncRequestVoteReply* pMsg, SRpcMsg* pRpcMsg) { + memset(pRpcMsg, 0, sizeof(*pRpcMsg)); + pRpcMsg->msgType = pMsg->msgType; + pRpcMsg->contLen = pMsg->bytes; + pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen); + syncRequestVoteReplySerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen); +} + +void syncRequestVoteReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncRequestVoteReply* pMsg) { + syncRequestVoteReplyDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg); +} + +cJSON* syncRequestVoteReply2Json(const SyncRequestVoteReply* pMsg) { + char u64buf[128]; + + cJSON* pRoot = cJSON_CreateObject(); + cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes); + cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType); + + cJSON* pSrcId = cJSON_CreateObject(); + snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->srcId.addr); + cJSON_AddStringToObject(pSrcId, "addr", u64buf); + { + uint64_t u64 = pMsg->srcId.addr; + cJSON* pTmp = pSrcId; + char host[128]; + uint16_t port; + syncUtilU642Addr(u64, host, sizeof(host), &port); + cJSON_AddStringToObject(pTmp, "addr_host", host); + cJSON_AddNumberToObject(pTmp, "addr_port", port); + } + cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId); + cJSON_AddItemToObject(pRoot, "srcId", pSrcId); + + cJSON* pDestId = cJSON_CreateObject(); + cJSON_AddNumberToObject(pDestId, "addr", pMsg->destId.addr); + { + uint64_t u64 = pMsg->destId.addr; + cJSON* pTmp = pDestId; + char host[128]; + uint16_t port; + syncUtilU642Addr(u64, host, sizeof(host), &port); + cJSON_AddStringToObject(pTmp, "addr_host", host); + cJSON_AddNumberToObject(pTmp, "addr_port", port); + } + cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId); + cJSON_AddItemToObject(pRoot, "destId", pDestId); + + snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->term); + cJSON_AddStringToObject(pRoot, "term", u64buf); + cJSON_AddNumberToObject(pRoot, "vote_granted", pMsg->voteGranted); + + cJSON* pJson = cJSON_CreateObject(); + cJSON_AddItemToObject(pJson, "SyncRequestVoteReply", pRoot); + return pJson; +} \ No newline at end of file diff --git a/source/libs/sync/test/syncEncodeTest.cpp b/source/libs/sync/test/syncEncodeTest.cpp index 4deceb603e..e0179b2c8b 100644 --- a/source/libs/sync/test/syncEncodeTest.cpp +++ b/source/libs/sync/test/syncEncodeTest.cpp @@ -3,6 +3,7 @@ #include "syncIO.h" #include "syncInt.h" #include "syncMessage.h" +#include "syncUtil.h" void logTest() { sTrace("--- sync log test: trace"); @@ -21,16 +22,16 @@ void test1() { char msg[PING_MSG_LEN]; snprintf(msg, sizeof(msg), "%s", "test ping"); SyncPing* pMsg = syncPingBuild(PING_MSG_LEN); - pMsg->srcId.addr = 1; - pMsg->srcId.vgId = 2; - pMsg->destId.addr = 3; - pMsg->destId.vgId = 4; + pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1111); + pMsg->srcId.vgId = 100; + pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 2222); + pMsg->destId.vgId = 100; memcpy(pMsg->data, msg, PING_MSG_LEN); { cJSON* pJson = syncPing2Json(pMsg); char* serialized = cJSON_Print(pJson); - printf("SyncPing: \n%s\n\n", serialized); + printf("\n%s\n\n", serialized); free(serialized); cJSON_Delete(pJson); } @@ -45,7 +46,7 @@ void test1() { { cJSON* pJson = syncPing2Json(pMsg2); char* serialized = cJSON_Print(pJson); - printf("SyncPing2: \n%s\n\n", serialized); + printf("\n%s\n\n", serialized); free(serialized); cJSON_Delete(pJson); } @@ -61,16 +62,16 @@ void test2() { char msg[PING_MSG_LEN]; snprintf(msg, sizeof(msg), "%s", "hello raft"); SyncPing* pMsg = syncPingBuild(PING_MSG_LEN); - pMsg->srcId.addr = 100; + pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 3333); pMsg->srcId.vgId = 200; - pMsg->destId.addr = 300; - pMsg->destId.vgId = 400; + pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 4444); + pMsg->destId.vgId = 200; memcpy(pMsg->data, msg, PING_MSG_LEN); { cJSON* pJson = syncPing2Json(pMsg); char* serialized = cJSON_Print(pJson); - printf("SyncPing: \n%s\n\n", serialized); + printf("\n%s\n\n", serialized); free(serialized); cJSON_Delete(pJson); } @@ -84,7 +85,7 @@ void test2() { { cJSON* pJson = syncPing2Json(pMsg2); char* serialized = cJSON_Print(pJson); - printf("SyncPing2: \n%s\n\n", serialized); + printf("\n%s\n\n", serialized); free(serialized); cJSON_Delete(pJson); } @@ -99,16 +100,16 @@ void test3() { char msg[PING_MSG_LEN]; snprintf(msg, sizeof(msg), "%s", "test ping"); SyncPingReply* pMsg = syncPingReplyBuild(PING_MSG_LEN); - pMsg->srcId.addr = 19; - pMsg->srcId.vgId = 29; - pMsg->destId.addr = 39; - pMsg->destId.vgId = 49; + pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 5555); + pMsg->srcId.vgId = 100; + pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 6666); + pMsg->destId.vgId = 100; memcpy(pMsg->data, msg, PING_MSG_LEN); { cJSON* pJson = syncPingReply2Json(pMsg); char* serialized = cJSON_Print(pJson); - printf("SyncPingReply: \n%s\n\n", serialized); + printf("\n%s\n\n", serialized); free(serialized); cJSON_Delete(pJson); } @@ -123,7 +124,7 @@ void test3() { { cJSON* pJson = syncPingReply2Json(pMsg2); char* serialized = cJSON_Print(pJson); - printf("SyncPingReply2: \n%s\n\n", serialized); + printf("\n%s\n\n", serialized); free(serialized); cJSON_Delete(pJson); } @@ -139,16 +140,16 @@ void test4() { char msg[PING_MSG_LEN]; snprintf(msg, sizeof(msg), "%s", "hello raft"); SyncPingReply* pMsg = syncPingReplyBuild(PING_MSG_LEN); - pMsg->srcId.addr = 66; - pMsg->srcId.vgId = 77; - pMsg->destId.addr = 88; - pMsg->destId.vgId = 99; + pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 7777); + pMsg->srcId.vgId = 100; + pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 8888); + pMsg->destId.vgId = 100; memcpy(pMsg->data, msg, PING_MSG_LEN); { cJSON* pJson = syncPingReply2Json(pMsg); char* serialized = cJSON_Print(pJson); - printf("SyncPingReply: \n%s\n\n", serialized); + printf("\n%s\n\n", serialized); free(serialized); cJSON_Delete(pJson); } @@ -162,7 +163,7 @@ void test4() { { cJSON* pJson = syncPingReply2Json(pMsg2); char* serialized = cJSON_Print(pJson); - printf("SyncPingReply2: \n%s\n\n", serialized); + printf("\n%s\n\n", serialized); free(serialized); cJSON_Delete(pJson); } @@ -170,6 +171,86 @@ void test4() { syncPingReplyDestroy(pMsg); syncPingReplyDestroy(pMsg2); } + +void test5() { + sTrace("test5: ---- syncRequestVoteSerialize, syncRequestVoteDeserialize"); + + SyncRequestVote* pMsg = syncRequestVoteBuild(); + pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); + pMsg->srcId.vgId = 100; + pMsg->destId.addr = syncUtilAddr2U64("8.8.8.8", 5678); + pMsg->destId.vgId = 100; + pMsg->currentTerm = 20; + pMsg->lastLogIndex = 21; + pMsg->lastLogTerm = 22; + + { + cJSON* pJson = syncRequestVote2Json(pMsg); + char* serialized = cJSON_Print(pJson); + printf("\n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + uint32_t bufLen = pMsg->bytes; + char* buf = (char*)malloc(bufLen); + syncRequestVoteSerialize(pMsg, buf, bufLen); + + SyncRequestVote* pMsg2 = (SyncRequestVote*)malloc(pMsg->bytes); + syncRequestVoteDeserialize(buf, bufLen, pMsg2); + + { + cJSON* pJson = syncRequestVote2Json(pMsg2); + char* serialized = cJSON_Print(pJson); + printf("\n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + syncRequestVoteDestroy(pMsg); + syncRequestVoteDestroy(pMsg2); + free(buf); +} + +void test6() { + sTrace("test6: ---- syncRequestVoteReplySerialize, syncRequestVoteReplyDeserialize"); + + SyncRequestVoteReply* pMsg = SyncRequestVoteReplyBuild(); + pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); + pMsg->srcId.vgId = 100; + pMsg->destId.addr = syncUtilAddr2U64("8.8.8.8", 5678); + pMsg->destId.vgId = 100; + pMsg->term = 20; + pMsg->voteGranted = 1; + + { + cJSON* pJson = syncRequestVoteReply2Json(pMsg); + char* serialized = cJSON_Print(pJson); + printf("\n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + uint32_t bufLen = pMsg->bytes; + char* buf = (char*)malloc(bufLen); + syncRequestVoteReplySerialize(pMsg, buf, bufLen); + + SyncRequestVoteReply* pMsg2 = (SyncRequestVoteReply*)malloc(pMsg->bytes); + syncRequestVoteReplyDeserialize(buf, bufLen, pMsg2); + + { + cJSON* pJson = syncRequestVoteReply2Json(pMsg2); + char* serialized = cJSON_Print(pJson); + printf("\n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + syncRequestVoteReplyDestroy(pMsg); + syncRequestVoteReplyDestroy(pMsg2); + free(buf); +} + int main() { // taosInitLog((char*)"syncPingTest.log", 100000, 10); tsAsyncLog = 0; @@ -179,6 +260,8 @@ int main() { test2(); test3(); test4(); + test5(); + test6(); return 0; } From aeb94af5e0b382e2ca29db907de5c6c78c649bf3 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Thu, 3 Mar 2022 16:15:18 +0800 Subject: [PATCH 11/19] sync encode/decode --- source/libs/sync/inc/syncMessage.h | 20 ++ source/libs/sync/src/syncMessage.c | 176 ++++++++++++++++ source/libs/sync/test/syncEncodeTest.cpp | 244 ++++++++++++++++++++++- 3 files changed, 439 insertions(+), 1 deletion(-) diff --git a/source/libs/sync/inc/syncMessage.h b/source/libs/sync/inc/syncMessage.h index a51567d1dd..3057e23bc2 100644 --- a/source/libs/sync/inc/syncMessage.h +++ b/source/libs/sync/inc/syncMessage.h @@ -158,6 +158,18 @@ typedef struct SyncAppendEntries { char data[]; } SyncAppendEntries; +#define SYNC_APPEND_ENTRIES_FIX_LEN \ + (sizeof(uint32_t) + sizeof(uint32_t) + sizeof(SRaftId) + sizeof(SRaftId) + sizeof(SyncIndex) + sizeof(SyncTerm) + \ + sizeof(SyncIndex) + sizeof(uint32_t)) + +SyncAppendEntries* syncAppendEntriesBuild(uint32_t dataLen); +void syncAppendEntriesDestroy(SyncAppendEntries* pMsg); +void syncAppendEntriesSerialize(const SyncAppendEntries* pMsg, char* buf, uint32_t bufLen); +void syncAppendEntriesDeserialize(const char* buf, uint32_t len, SyncAppendEntries* pMsg); +void syncAppendEntries2RpcMsg(const SyncAppendEntries* pMsg, SRpcMsg* pRpcMsg); +void syncAppendEntriesFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntries* pMsg); +cJSON* syncAppendEntries2Json(const SyncAppendEntries* pMsg); + // --------------------------------------------- typedef struct SyncAppendEntriesReply { uint32_t bytes; @@ -169,6 +181,14 @@ typedef struct SyncAppendEntriesReply { SyncIndex matchIndex; } SyncAppendEntriesReply; +SyncAppendEntriesReply* syncAppendEntriesReplyBuild(); +void syncAppendEntriesReplyDestroy(SyncAppendEntriesReply* pMsg); +void syncAppendEntriesReplySerialize(const SyncAppendEntriesReply* pMsg, char* buf, uint32_t bufLen); +void syncAppendEntriesReplyDeserialize(const char* buf, uint32_t len, SyncAppendEntriesReply* pMsg); +void syncAppendEntriesReply2RpcMsg(const SyncAppendEntriesReply* pMsg, SRpcMsg* pRpcMsg); +void syncAppendEntriesReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntriesReply* pMsg); +cJSON* syncAppendEntriesReply2Json(const SyncAppendEntriesReply* pMsg); + #ifdef __cplusplus } #endif diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index ac3f550e3e..4c44b4691c 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -381,4 +381,180 @@ cJSON* syncRequestVoteReply2Json(const SyncRequestVoteReply* pMsg) { cJSON* pJson = cJSON_CreateObject(); cJSON_AddItemToObject(pJson, "SyncRequestVoteReply", pRoot); return pJson; +} + +// ---- message process SyncAppendEntries---- +SyncAppendEntries* syncAppendEntriesBuild(uint32_t dataLen) { + uint32_t bytes = SYNC_APPEND_ENTRIES_FIX_LEN + dataLen; + SyncAppendEntries* pMsg = malloc(bytes); + memset(pMsg, 0, bytes); + pMsg->bytes = bytes; + pMsg->msgType = SYNC_APPEND_ENTRIES; + pMsg->dataLen = dataLen; +} + +void syncAppendEntriesDestroy(SyncAppendEntries* pMsg) { + if (pMsg != NULL) { + free(pMsg); + } +} + +void syncAppendEntriesSerialize(const SyncAppendEntries* pMsg, char* buf, uint32_t bufLen) { + assert(pMsg->bytes <= bufLen); + memcpy(buf, pMsg, pMsg->bytes); +} + +void syncAppendEntriesDeserialize(const char* buf, uint32_t len, SyncAppendEntries* pMsg) { + memcpy(pMsg, buf, len); + assert(len == pMsg->bytes); + assert(pMsg->bytes == SYNC_APPEND_ENTRIES_FIX_LEN + pMsg->dataLen); +} + +void syncAppendEntries2RpcMsg(const SyncAppendEntries* pMsg, SRpcMsg* pRpcMsg) { + memset(pRpcMsg, 0, sizeof(*pRpcMsg)); + pRpcMsg->msgType = pMsg->msgType; + pRpcMsg->contLen = pMsg->bytes; + pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen); + syncAppendEntriesSerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen); +} + +void syncAppendEntriesFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntries* pMsg) { + syncAppendEntriesDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg); +} + +cJSON* syncAppendEntries2Json(const SyncAppendEntries* pMsg) { + char u64buf[128]; + + cJSON* pRoot = cJSON_CreateObject(); + cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes); + cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType); + + cJSON* pSrcId = cJSON_CreateObject(); + snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->srcId.addr); + cJSON_AddStringToObject(pSrcId, "addr", u64buf); + { + uint64_t u64 = pMsg->srcId.addr; + cJSON* pTmp = pSrcId; + char host[128]; + uint16_t port; + syncUtilU642Addr(u64, host, sizeof(host), &port); + cJSON_AddStringToObject(pTmp, "addr_host", host); + cJSON_AddNumberToObject(pTmp, "addr_port", port); + } + cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId); + cJSON_AddItemToObject(pRoot, "srcId", pSrcId); + + cJSON* pDestId = cJSON_CreateObject(); + snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->destId.addr); + cJSON_AddStringToObject(pDestId, "addr", u64buf); + { + uint64_t u64 = pMsg->destId.addr; + cJSON* pTmp = pDestId; + char host[128]; + uint16_t port; + syncUtilU642Addr(u64, host, sizeof(host), &port); + cJSON_AddStringToObject(pTmp, "addr_host", host); + cJSON_AddNumberToObject(pTmp, "addr_port", port); + } + cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId); + cJSON_AddItemToObject(pRoot, "destId", pDestId); + + snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->prevLogIndex); + cJSON_AddStringToObject(pRoot, "pre_log_index", u64buf); + + snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->prevLogTerm); + cJSON_AddStringToObject(pRoot, "pre_log_term", u64buf); + + snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->commitIndex); + cJSON_AddStringToObject(pRoot, "commit_index", u64buf); + + cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen); + cJSON_AddStringToObject(pRoot, "data", pMsg->data); + + cJSON* pJson = cJSON_CreateObject(); + cJSON_AddItemToObject(pJson, "SyncAppendEntries", pRoot); + return pJson; +} + +// ---- message process SyncAppendEntriesReply---- +SyncAppendEntriesReply* syncAppendEntriesReplyBuild() { + uint32_t bytes = sizeof(SyncAppendEntriesReply); + SyncAppendEntriesReply* pMsg = malloc(bytes); + memset(pMsg, 0, bytes); + pMsg->bytes = bytes; + pMsg->msgType = SYNC_APPEND_ENTRIES_REPLY; +} + +void syncAppendEntriesReplyDestroy(SyncAppendEntriesReply* pMsg) { + if (pMsg != NULL) { + free(pMsg); + } +} + +void syncAppendEntriesReplySerialize(const SyncAppendEntriesReply* pMsg, char* buf, uint32_t bufLen) { + assert(pMsg->bytes <= bufLen); + memcpy(buf, pMsg, pMsg->bytes); +} + +void syncAppendEntriesReplyDeserialize(const char* buf, uint32_t len, SyncAppendEntriesReply* pMsg) { + memcpy(pMsg, buf, len); + assert(len == pMsg->bytes); +} + +void syncAppendEntriesReply2RpcMsg(const SyncAppendEntriesReply* pMsg, SRpcMsg* pRpcMsg) { + memset(pRpcMsg, 0, sizeof(*pRpcMsg)); + pRpcMsg->msgType = pMsg->msgType; + pRpcMsg->contLen = pMsg->bytes; + pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen); + syncAppendEntriesReplySerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen); +} + +void syncAppendEntriesReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntriesReply* pMsg) { + syncAppendEntriesReplyDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg); +} + +cJSON* syncAppendEntriesReply2Json(const SyncAppendEntriesReply* pMsg) { + char u64buf[128]; + + cJSON* pRoot = cJSON_CreateObject(); + cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes); + cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType); + + cJSON* pSrcId = cJSON_CreateObject(); + snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->srcId.addr); + cJSON_AddStringToObject(pSrcId, "addr", u64buf); + { + uint64_t u64 = pMsg->srcId.addr; + cJSON* pTmp = pSrcId; + char host[128]; + uint16_t port; + syncUtilU642Addr(u64, host, sizeof(host), &port); + cJSON_AddStringToObject(pTmp, "addr_host", host); + cJSON_AddNumberToObject(pTmp, "addr_port", port); + } + cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId); + cJSON_AddItemToObject(pRoot, "srcId", pSrcId); + + cJSON* pDestId = cJSON_CreateObject(); + snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->destId.addr); + cJSON_AddStringToObject(pDestId, "addr", u64buf); + { + uint64_t u64 = pMsg->destId.addr; + cJSON* pTmp = pDestId; + char host[128]; + uint16_t port; + syncUtilU642Addr(u64, host, sizeof(host), &port); + cJSON_AddStringToObject(pTmp, "addr_host", host); + cJSON_AddNumberToObject(pTmp, "addr_port", port); + } + cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId); + cJSON_AddItemToObject(pRoot, "destId", pDestId); + + cJSON_AddNumberToObject(pRoot, "success", pMsg->success); + snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->matchIndex); + cJSON_AddStringToObject(pRoot, "match_index", u64buf); + + cJSON* pJson = cJSON_CreateObject(); + cJSON_AddItemToObject(pJson, "SyncAppendEntriesReply", pRoot); + return pJson; } \ No newline at end of file diff --git a/source/libs/sync/test/syncEncodeTest.cpp b/source/libs/sync/test/syncEncodeTest.cpp index e0179b2c8b..6197621051 100644 --- a/source/libs/sync/test/syncEncodeTest.cpp +++ b/source/libs/sync/test/syncEncodeTest.cpp @@ -15,6 +15,7 @@ void logTest() { } #define PING_MSG_LEN 20 +#define APPEND_ENTRIES_VALUE_LEN 32 void test1() { sTrace("test1: ---- syncPingSerialize, syncPingDeserialize"); @@ -213,7 +214,45 @@ void test5() { } void test6() { - sTrace("test6: ---- syncRequestVoteReplySerialize, syncRequestVoteReplyDeserialize"); + sTrace("test6: ---- syncRequestVote2RpcMsg, syncRequestVoteFromRpcMsg"); + + SyncRequestVote* pMsg = syncRequestVoteBuild(); + pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); + pMsg->srcId.vgId = 100; + pMsg->destId.addr = syncUtilAddr2U64("8.8.8.8", 5678); + pMsg->destId.vgId = 100; + pMsg->currentTerm = 20; + pMsg->lastLogIndex = 21; + pMsg->lastLogTerm = 22; + + { + cJSON* pJson = syncRequestVote2Json(pMsg); + char* serialized = cJSON_Print(pJson); + printf("\n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + SRpcMsg rpcMsg; + syncRequestVote2RpcMsg(pMsg, &rpcMsg); + SyncRequestVote* pMsg2 = (SyncRequestVote*)malloc(pMsg->bytes); + syncRequestVoteFromRpcMsg(&rpcMsg, pMsg2); + rpcFreeCont(rpcMsg.pCont); + + { + cJSON* pJson = syncRequestVote2Json(pMsg2); + char* serialized = cJSON_Print(pJson); + printf("\n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + syncRequestVoteDestroy(pMsg); + syncRequestVoteDestroy(pMsg2); +} + +void test7() { + sTrace("test7: ---- syncRequestVoteReplySerialize, syncRequestVoteReplyDeserialize"); SyncRequestVoteReply* pMsg = SyncRequestVoteReplyBuild(); pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); @@ -251,6 +290,203 @@ void test6() { free(buf); } +void test8() { + sTrace("test8: ---- syncRequestVoteReply2RpcMsg, syncRequestVoteReplyFromRpcMsg"); + + SyncRequestVoteReply* pMsg = SyncRequestVoteReplyBuild(); + pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); + pMsg->srcId.vgId = 100; + pMsg->destId.addr = syncUtilAddr2U64("8.8.8.8", 5678); + pMsg->destId.vgId = 100; + pMsg->term = 20; + pMsg->voteGranted = 1; + + { + cJSON* pJson = syncRequestVoteReply2Json(pMsg); + char* serialized = cJSON_Print(pJson); + printf("\n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + SRpcMsg rpcMsg; + syncRequestVoteReply2RpcMsg(pMsg, &rpcMsg); + SyncRequestVoteReply* pMsg2 = (SyncRequestVoteReply*)malloc(pMsg->bytes); + syncRequestVoteReplyFromRpcMsg(&rpcMsg, pMsg2); + rpcFreeCont(rpcMsg.pCont); + + { + cJSON* pJson = syncRequestVoteReply2Json(pMsg2); + char* serialized = cJSON_Print(pJson); + printf("\n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + syncRequestVoteReplyDestroy(pMsg); + syncRequestVoteReplyDestroy(pMsg2); +} + +void test9() { + sTrace("test9: ---- syncAppendEntriesSerialize, syncAppendEntriesDeserialize"); + + char msg[APPEND_ENTRIES_VALUE_LEN]; + snprintf(msg, sizeof(msg), "%s", "test value"); + SyncAppendEntries* pMsg = syncAppendEntriesBuild(APPEND_ENTRIES_VALUE_LEN); + pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1111); + pMsg->srcId.vgId = 100; + pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 2222); + pMsg->destId.vgId = 100; + pMsg->prevLogIndex = 55; + pMsg->prevLogTerm = 66; + pMsg->commitIndex = 77; + memcpy(pMsg->data, msg, APPEND_ENTRIES_VALUE_LEN); + + { + cJSON* pJson = syncAppendEntries2Json(pMsg); + char* serialized = cJSON_Print(pJson); + printf("\n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + uint32_t bufLen = pMsg->bytes; + char* buf = (char*)malloc(bufLen); + syncAppendEntriesSerialize(pMsg, buf, bufLen); + + SyncAppendEntries* pMsg2 = (SyncAppendEntries*)malloc(pMsg->bytes); + syncAppendEntriesDeserialize(buf, bufLen, pMsg2); + + { + cJSON* pJson = syncAppendEntries2Json(pMsg2); + char* serialized = cJSON_Print(pJson); + printf("\n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + syncAppendEntriesDestroy(pMsg); + syncAppendEntriesDestroy(pMsg2); + free(buf); +} + +void test10() { + sTrace("test10: ---- syncAppendEntries2RpcMsg, syncAppendEntriesFromRpcMsg"); + + char msg[APPEND_ENTRIES_VALUE_LEN]; + snprintf(msg, sizeof(msg), "%s", "test value"); + SyncAppendEntries* pMsg = syncAppendEntriesBuild(APPEND_ENTRIES_VALUE_LEN); + pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1111); + pMsg->srcId.vgId = 100; + pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 2222); + pMsg->destId.vgId = 100; + pMsg->prevLogIndex = 55; + pMsg->prevLogTerm = 66; + pMsg->commitIndex = 77; + memcpy(pMsg->data, msg, APPEND_ENTRIES_VALUE_LEN); + + { + cJSON* pJson = syncAppendEntries2Json(pMsg); + char* serialized = cJSON_Print(pJson); + printf("\n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + SRpcMsg rpcMsg; + syncAppendEntries2RpcMsg(pMsg, &rpcMsg); + SyncAppendEntries* pMsg2 = (SyncAppendEntries*)malloc(pMsg->bytes); + syncAppendEntriesFromRpcMsg(&rpcMsg, pMsg2); + rpcFreeCont(rpcMsg.pCont); + + { + cJSON* pJson = syncAppendEntries2Json(pMsg2); + char* serialized = cJSON_Print(pJson); + printf("\n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + syncAppendEntriesDestroy(pMsg); + syncAppendEntriesDestroy(pMsg2); +} + +void test11() { + sTrace("test11: ---- syncAppendEntriesReplySerialize, syncAppendEntriesReplyDeserialize"); + + SyncAppendEntriesReply* pMsg = syncAppendEntriesReplyBuild(); + pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1111); + pMsg->srcId.vgId = 100; + pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 2222); + pMsg->destId.vgId = 100; + pMsg->success = 1; + pMsg->matchIndex = 23; + + { + cJSON* pJson = syncAppendEntriesReply2Json(pMsg); + char* serialized = cJSON_Print(pJson); + printf("\n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + uint32_t bufLen = pMsg->bytes; + char* buf = (char*)malloc(bufLen); + syncAppendEntriesReplySerialize(pMsg, buf, bufLen); + + SyncAppendEntriesReply* pMsg2 = (SyncAppendEntriesReply*)malloc(pMsg->bytes); + syncAppendEntriesReplyDeserialize(buf, bufLen, pMsg2); + + { + cJSON* pJson = syncAppendEntriesReply2Json(pMsg2); + char* serialized = cJSON_Print(pJson); + printf("\n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + syncAppendEntriesReplyDestroy(pMsg); + syncAppendEntriesReplyDestroy(pMsg2); + free(buf); +} + +void test12() { + sTrace("test12: ---- syncAppendEntriesReply2RpcMsg, syncAppendEntriesReplyFromRpcMsg"); + + SyncAppendEntriesReply* pMsg = syncAppendEntriesReplyBuild(); + pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1111); + pMsg->srcId.vgId = 100; + pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 2222); + pMsg->destId.vgId = 100; + pMsg->success = 1; + pMsg->matchIndex = 23; + + { + cJSON* pJson = syncAppendEntriesReply2Json(pMsg); + char* serialized = cJSON_Print(pJson); + printf("\n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + SRpcMsg rpcMsg; + syncAppendEntriesReply2RpcMsg(pMsg, &rpcMsg); + SyncAppendEntriesReply* pMsg2 = (SyncAppendEntriesReply*)malloc(pMsg->bytes); + syncAppendEntriesReplyFromRpcMsg(&rpcMsg, pMsg2); + rpcFreeCont(rpcMsg.pCont); + + { + cJSON* pJson = syncAppendEntriesReply2Json(pMsg2); + char* serialized = cJSON_Print(pJson); + printf("\n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + syncAppendEntriesReplyDestroy(pMsg); + syncAppendEntriesReplyDestroy(pMsg2); +} + int main() { // taosInitLog((char*)"syncPingTest.log", 100000, 10); tsAsyncLog = 0; @@ -262,6 +498,12 @@ int main() { test4(); test5(); test6(); + test7(); + test8(); + test9(); + test10(); + test11(); + test12(); return 0; } From cc82966e2e2281a89e3436c47b6e6bfc92f3f044 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 3 Mar 2022 16:37:37 +0800 Subject: [PATCH 12/19] monitor --- include/libs/monitor/monitor.h | 21 ++- source/libs/monitor/src/monitor.c | 56 +++--- source/libs/monitor/test/monTest.cpp | 255 ++++++++++++++++++++++++++- 3 files changed, 286 insertions(+), 46 deletions(-) diff --git a/include/libs/monitor/monitor.h b/include/libs/monitor/monitor.h index 090fea9dab..b3b6091b95 100644 --- a/include/libs/monitor/monitor.h +++ b/include/libs/monitor/monitor.h @@ -23,6 +23,11 @@ extern "C" { #endif +#define MON_STATUS_LEN 8 +#define MON_ROLE_LEN 9 +#define MON_VER_LEN 12 +#define MON_LOG_LEN 1024 + typedef struct { int32_t dnode_id; char dnode_ep[TSDB_EP_LEN]; @@ -31,19 +36,19 @@ typedef struct { typedef struct { int32_t dnode_id; char dnode_ep[TSDB_EP_LEN]; - char status[8]; + char status[MON_STATUS_LEN]; } SMonDnodeDesc; typedef struct { int32_t mnode_id; char mnode_ep[TSDB_EP_LEN]; - char role[8]; + char role[MON_ROLE_LEN]; } SMonMnodeDesc; typedef struct { char first_ep[TSDB_EP_LEN]; int32_t first_ep_dnode_id; - char version[12]; + char version[MON_VER_LEN]; float master_uptime; // day int32_t monitor_interval; // sec int32_t vgroups_total; @@ -57,18 +62,18 @@ typedef struct { typedef struct { int32_t dnode_id; - char vnode_role[8]; + char vnode_role[MON_ROLE_LEN]; } SMonVnodeDesc; typedef struct { int32_t vgroup_id; + char database_name[TSDB_DB_NAME_LEN]; + int32_t tables_num; + char status[MON_STATUS_LEN]; SMonVnodeDesc vnodes[TSDB_MAX_REPLICA]; } SMonVgroupDesc; typedef struct { - char database_name[TSDB_DB_NAME_LEN]; - int32_t tables_num; - char status[10]; SArray *vgroups; // array of SMonVgroupDesc } SMonVgroupInfo; @@ -124,7 +129,7 @@ typedef struct { typedef struct { int64_t ts; int8_t level; - char content[1024]; + char content[MON_LOG_LEN]; } SMonLogItem; typedef struct SMonInfo SMonInfo; diff --git a/source/libs/monitor/src/monitor.c b/source/libs/monitor/src/monitor.c index 7ba65b87b0..ecf9da218b 100644 --- a/source/libs/monitor/src/monitor.c +++ b/source/libs/monitor/src/monitor.c @@ -23,7 +23,7 @@ static SMonitor tsMonitor = {0}; int32_t monInit(const SMonCfg *pCfg) { - tsMonitor.logs = taosArrayInit(16, sizeof(SMonInfo)); + tsMonitor.logs = taosArrayInit(16, sizeof(SMonLogItem)); if (tsMonitor.logs == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; @@ -44,7 +44,7 @@ void monCleanup() { void monAddLogItem(SMonLogItem *pItem) { taosWLockLatch(&tsMonitor.lock); int32_t size = taosArrayGetSize(tsMonitor.logs); - if (size > tsMonitor.maxLogs) { + if (size >= tsMonitor.maxLogs) { uInfo("too many logs for monitor"); } else { taosArrayPush(tsMonitor.logs, pItem); @@ -118,9 +118,9 @@ void monSetClusterInfo(SMonInfo *pMonitor, SMonClusterInfo *pInfo) { if (pDnodeJson == NULL) continue; SMonDnodeDesc *pDnodeDesc = taosArrayGet(pInfo->dnodes, i); - if (tjsonAddDoubleToObject(pDnodesJson, "dnode_id", pDnodeDesc->dnode_id) != 0) tjsonDelete(pDnodeJson); - if (tjsonAddStringToObject(pDnodesJson, "dnode_ep", pDnodeDesc->dnode_ep) != 0) tjsonDelete(pDnodeJson); - if (tjsonAddStringToObject(pDnodesJson, "status", pDnodeDesc->status) != 0) tjsonDelete(pDnodeJson); + tjsonAddDoubleToObject(pDnodeJson, "dnode_id", pDnodeDesc->dnode_id); + tjsonAddStringToObject(pDnodeJson, "dnode_ep", pDnodeDesc->dnode_ep); + tjsonAddStringToObject(pDnodeJson, "status", pDnodeDesc->status); if (tjsonAddItemToArray(pDnodesJson, pDnodeJson) != 0) tjsonDelete(pDnodeJson); } @@ -133,48 +133,44 @@ void monSetClusterInfo(SMonInfo *pMonitor, SMonClusterInfo *pInfo) { if (pMnodeJson == NULL) continue; SMonMnodeDesc *pMnodeDesc = taosArrayGet(pInfo->dnodes, i); - if (tjsonAddDoubleToObject(pMnodesJson, "mnode_id", pMnodeDesc->mnode_id) != 0) tjsonDelete(pMnodeJson); - if (tjsonAddStringToObject(pMnodesJson, "mnode_ep", pMnodeDesc->mnode_ep) != 0) tjsonDelete(pMnodeJson); - if (tjsonAddStringToObject(pMnodesJson, "role", pMnodeDesc->role) != 0) tjsonDelete(pMnodeJson); + tjsonAddDoubleToObject(pMnodeJson, "mnode_id", pMnodeDesc->mnode_id); + tjsonAddStringToObject(pMnodeJson, "mnode_ep", pMnodeDesc->mnode_ep); + tjsonAddStringToObject(pMnodeJson, "role", pMnodeDesc->role); if (tjsonAddItemToArray(pMnodesJson, pMnodeJson) != 0) tjsonDelete(pMnodeJson); } } void monSetVgroupInfo(SMonInfo *pMonitor, SMonVgroupInfo *pInfo) { - SJson *pJson = tjsonCreateObject(); + SJson *pJson = tjsonAddArrayToObject(pMonitor->pJson, "vgroup_infos"); if (pJson == NULL) return; - if (tjsonAddItemToObject(pMonitor->pJson, "vgroup_infos", pJson) != 0) { - tjsonDelete(pJson); - return; - } - - tjsonAddStringToObject(pJson, "database_name", pInfo->database_name); - tjsonAddDoubleToObject(pJson, "tables_num", pInfo->tables_num); - tjsonAddStringToObject(pJson, "status", pInfo->status); - - SJson *pVgroupsJson = tjsonAddArrayToObject(pJson, "vgroups"); - if (pVgroupsJson == NULL) return; for (int32_t i = 0; i < taosArrayGetSize(pInfo->vgroups); ++i) { SJson *pVgroupJson = tjsonCreateObject(); if (pVgroupJson == NULL) continue; + if (tjsonAddItemToArray(pJson, pVgroupJson) != 0) { + tjsonDelete(pVgroupJson); + continue; + } SMonVgroupDesc *pVgroupDesc = taosArrayGet(pInfo->vgroups, i); - if (tjsonAddDoubleToObject(pVgroupJson, "vgroup_id", pVgroupDesc->vgroup_id) != 0) tjsonDelete(pVgroupJson); + tjsonAddDoubleToObject(pVgroupJson, "vgroup_id", pVgroupDesc->vgroup_id); + tjsonAddStringToObject(pVgroupJson, "database_name", pVgroupDesc->database_name); + tjsonAddDoubleToObject(pVgroupJson, "tables_num", pVgroupDesc->tables_num); + tjsonAddStringToObject(pVgroupJson, "status", pVgroupDesc->status); SJson *pVnodesJson = tjsonAddArrayToObject(pVgroupJson, "vnodes"); - if (pVnodesJson == NULL) tjsonDelete(pVgroupJson); + if (pVnodesJson == NULL) continue; for (int32_t j = 0; j < TSDB_MAX_REPLICA; ++j) { SMonVnodeDesc *pVnodeDesc = &pVgroupDesc->vnodes[j]; - if (pVnodeDesc[j].dnode_id <= 0) continue; + if (pVnodeDesc->dnode_id <= 0) continue; SJson *pVnodeJson = tjsonCreateObject(); if (pVnodeJson == NULL) continue; - if (tjsonAddDoubleToObject(pVnodeJson, "dnode_id", pVnodeDesc->dnode_id) != 0) tjsonDelete(pVnodeJson); - if (tjsonAddStringToObject(pVnodeJson, "vnode_role", pVnodeDesc->vnode_role) != 0) tjsonDelete(pVnodeJson); + tjsonAddDoubleToObject(pVnodeJson, "dnode_id", pVnodeDesc->dnode_id); + tjsonAddStringToObject(pVnodeJson, "vnode_role", pVnodeDesc->vnode_role); if (tjsonAddItemToArray(pVnodesJson, pVnodeJson) != 0) tjsonDelete(pVnodeJson); } @@ -235,7 +231,7 @@ void monSetDnodeInfo(SMonInfo *pMonitor, SMonDnodeInfo *pInfo) { void monSetDiskInfo(SMonInfo *pMonitor, SMonDiskInfo *pInfo) { SJson *pJson = tjsonCreateObject(); if (pJson == NULL) return; - if (tjsonAddItemToObject(pMonitor->pJson, "disks_infos", pJson) != 0) { + if (tjsonAddItemToObject(pMonitor->pJson, "disk_infos", pJson) != 0) { tjsonDelete(pJson); return; } @@ -261,7 +257,6 @@ void monSetDiskInfo(SMonInfo *pMonitor, SMonDiskInfo *pInfo) { if (pLogdirJson == NULL) return; if (tjsonAddItemToObject(pJson, "logdir", pLogdirJson) != 0) return; tjsonAddStringToObject(pLogdirJson, "name", pInfo->logdir.name); - tjsonAddDoubleToObject(pLogdirJson, "level", pInfo->logdir.level); tjsonAddDoubleToObject(pLogdirJson, "avail", pInfo->logdir.size.avail); tjsonAddDoubleToObject(pLogdirJson, "used", pInfo->logdir.size.used); tjsonAddDoubleToObject(pLogdirJson, "total", pInfo->logdir.size.total); @@ -270,7 +265,6 @@ void monSetDiskInfo(SMonInfo *pMonitor, SMonDiskInfo *pInfo) { if (pTempdirJson == NULL) return; if (tjsonAddItemToObject(pJson, "tempdir", pTempdirJson) != 0) return; tjsonAddStringToObject(pTempdirJson, "name", pInfo->tempdir.name); - tjsonAddDoubleToObject(pTempdirJson, "level", pInfo->tempdir.level); tjsonAddDoubleToObject(pTempdirJson, "avail", pInfo->tempdir.size.avail); tjsonAddDoubleToObject(pTempdirJson, "used", pInfo->tempdir.size.used); tjsonAddDoubleToObject(pTempdirJson, "total", pInfo->tempdir.size.total); @@ -296,9 +290,9 @@ static void monSetLogInfo(SMonInfo *pMonitor) { char buf[40] = {0}; taosFormatUtcTime(buf, sizeof(buf), pLogItem->ts, TSDB_TIME_PRECISION_MILLI); - if (tjsonAddStringToObject(pLogItem, "ts", buf) != 0) tjsonDelete(pLogJson); - if (tjsonAddDoubleToObject(pLogItem, "level", pLogItem->level) != 0) tjsonDelete(pLogJson); - if (tjsonAddStringToObject(pLogItem, "content", pLogItem->content) != 0) tjsonDelete(pLogJson); + tjsonAddStringToObject(pLogJson, "ts", buf); + tjsonAddDoubleToObject(pLogJson, "level", pLogItem->level); + tjsonAddStringToObject(pLogJson, "content", pLogItem->content); if (tjsonAddItemToArray(pLogsJson, pLogJson) != 0) tjsonDelete(pLogJson); } diff --git a/source/libs/monitor/test/monTest.cpp b/source/libs/monitor/test/monTest.cpp index a1805d0c9c..ad48ed5407 100644 --- a/source/libs/monitor/test/monTest.cpp +++ b/source/libs/monitor/test/monTest.cpp @@ -13,21 +13,262 @@ #include "os.h" #include "monitor.h" +#include "tglobal.h" class MonitorTest : public ::testing::Test { protected: - static void SetUpTestSuite() { root = "/tmp/monTest"; } - static void TearDownTestSuite() {} + static void SetUpTestSuite() { + SMonCfg cfg; + cfg.maxLogs = 2; + cfg.port = 80; + cfg.server = "localhost"; + monInit(&cfg); + } + + static void TearDownTestSuite() { monCleanup(); } public: void SetUp() override {} void TearDown() override {} - static const char *root; + void GetBasicInfo(SMonInfo *pMonitor, SMonBasicInfo *pInfo); + void GetClusterInfo(SMonInfo *pMonitor, SMonClusterInfo *pInfo); + void GetVgroupInfo(SMonInfo *pMonitor, SMonVgroupInfo *pInfo); + void GetGrantInfo(SMonInfo *pMonitor, SMonGrantInfo *pInfo); + void GetDnodeInfo(SMonInfo *pMonitor, SMonDnodeInfo *pInfo); + void GetDiskInfo(SMonInfo *pMonitor, SMonDiskInfo *pInfo); + void AddLogInfo1(); + void AddLogInfo2(); }; -const char *MonitorTest::root; - -TEST_F(MonitorTest, 01_Open_Close) { - +void MonitorTest::GetBasicInfo(SMonInfo *pMonitor, SMonBasicInfo *pInfo) { + pInfo->dnode_id = 1; + strcpy(pInfo->dnode_ep, "localhost"); +} + +void MonitorTest::GetClusterInfo(SMonInfo *pMonitor, SMonClusterInfo *pInfo) { + strcpy(pInfo->first_ep, "localhost:6030"); + pInfo->first_ep_dnode_id = 1; + strcpy(pInfo->version, "3.0.0.0"); + pInfo->master_uptime = 1; + pInfo->monitor_interval = 2; + pInfo->vgroups_total = 3; + pInfo->vgroups_alive = 43; + pInfo->vnodes_total = 5; + pInfo->vnodes_alive = 6; + pInfo->connections_total = 7; + + pInfo->dnodes = taosArrayInit(4, sizeof(SMonDnodeDesc)); + SMonDnodeDesc d1 = {0}; + d1.dnode_id = 1; + strcpy(d1.dnode_ep, "localhost:6030"); + strcpy(d1.status, "ready"); + taosArrayPush(pInfo->dnodes, &d1); + SMonDnodeDesc d2 = {0}; + d2.dnode_id = 2; + strcpy(d2.dnode_ep, "localhost:7030"); + strcpy(d2.status, "offline"); + taosArrayPush(pInfo->dnodes, &d2); + + pInfo->mnodes = taosArrayInit(4, sizeof(SMonMnodeDesc)); + SMonMnodeDesc m1 = {0}; + m1.mnode_id = 1; + strcpy(m1.mnode_ep, "localhost:6030"); + strcpy(m1.role, "master"); + taosArrayPush(pInfo->mnodes, &m1); + SMonMnodeDesc m2 = {0}; + m2.mnode_id = 2; + strcpy(m2.mnode_ep, "localhost:7030"); + strcpy(m2.role, "unsynced"); + taosArrayPush(pInfo->mnodes, &m2); +} + +void MonitorTest::GetVgroupInfo(SMonInfo *pMonitor, SMonVgroupInfo *pInfo) { + pInfo->vgroups = taosArrayInit(4, sizeof(SMonVgroupDesc)); + + SMonVgroupDesc vg1 = {0}; + vg1.vgroup_id = 1; + strcpy(vg1.database_name, "d1"); + vg1.tables_num = 4; + strcpy(vg1.status, "ready"); + vg1.vnodes[0].dnode_id = 1; + strcpy(vg1.vnodes[0].vnode_role, "master"); + vg1.vnodes[1].dnode_id = 2; + strcpy(vg1.vnodes[1].vnode_role, "slave"); + taosArrayPush(pInfo->vgroups, &vg1); + + SMonVgroupDesc vg2 = {0}; + vg2.vgroup_id = 2; + strcpy(vg2.database_name, "d2"); + vg2.tables_num = 5; + strcpy(vg2.status, "offline"); + vg2.vnodes[0].dnode_id = 1; + strcpy(vg2.vnodes[0].vnode_role, "master"); + vg2.vnodes[1].dnode_id = 2; + strcpy(vg2.vnodes[1].vnode_role, "unsynced"); + taosArrayPush(pInfo->vgroups, &vg2); + + SMonVgroupDesc vg3 = {0}; + vg3.vgroup_id = 3; + strcpy(vg3.database_name, "d3"); + vg3.tables_num = 6; + strcpy(vg3.status, "ready"); + vg3.vnodes[0].dnode_id = 1; + strcpy(vg3.vnodes[0].vnode_role, "master"); + taosArrayPush(pInfo->vgroups, &vg3); +} + +void MonitorTest::GetGrantInfo(SMonInfo *pMonitor, SMonGrantInfo *pInfo) { + pInfo->expire_time = 1234567; + pInfo->timeseries_total = 234567; + pInfo->timeseries_used = 34567; +} + +void MonitorTest::GetDnodeInfo(SMonInfo *pMonitor, SMonDnodeInfo *pInfo) { + pInfo->uptime = 1.2; + pInfo->cpu_engine = 2.1; + pInfo->cpu_system = 2.1; + pInfo->cpu_cores = 2; + pInfo->mem_engine = 3.1; + pInfo->mem_system = 3.2; + pInfo->mem_total = 3.3; + pInfo->disk_engine = 4.1; + pInfo->disk_used = 4.2; + pInfo->disk_total = 4.3; + pInfo->net_in = 5.1; + pInfo->net_out = 5.2; + pInfo->io_read = 6.1; + pInfo->io_write = 6.2; + pInfo->io_read_disk = 7.1; + pInfo->io_write_disk = 7.2; + pInfo->req_select = 8; + pInfo->req_select_rate = 8.1; + pInfo->req_insert = 9; + pInfo->req_insert_success = 10; + pInfo->req_insert_rate = 10.1; + pInfo->req_insert_batch = 11; + pInfo->req_insert_batch_success = 12; + pInfo->req_insert_batch_rate = 12.3; + pInfo->errors = 4; + pInfo->vnodes_num = 5; + pInfo->masters = 6; + pInfo->has_mnode = 1; +} + +void MonitorTest::GetDiskInfo(SMonInfo *pMonitor, SMonDiskInfo *pInfo) { + pInfo->datadirs = taosArrayInit(2, sizeof(SMonDiskDesc)); + SMonDiskDesc d1 = {0}; + strcpy(d1.name, "/t1/d1/d"); + d1.level = 0; + d1.size.avail = 11; + d1.size.total = 12; + d1.size.used = 13; + taosArrayPush(pInfo->datadirs, &d1); + + SMonDiskDesc d2 = {0}; + strcpy(d2.name, "/t2d2/d"); + d2.level = 2; + d2.size.avail = 21; + d2.size.total = 22; + d2.size.used = 23; + taosArrayPush(pInfo->datadirs, &d2); + + SMonDiskDesc d3 = {0}; + strcpy(d3.name, "/t3/d3/d"); + d3.level = 3; + d3.size.avail = 31; + d3.size.total = 32; + d3.size.used = 33; + taosArrayPush(pInfo->datadirs, &d3); + + strcpy(pInfo->logdir.name, "/log/dir/d"); + pInfo->logdir.size.avail = 41; + pInfo->logdir.size.total = 42; + pInfo->logdir.size.used = 43; + + strcpy(pInfo->tempdir.name, "/data/dir/d"); + pInfo->tempdir.size.avail = 51; + pInfo->tempdir.size.total = 52; + pInfo->tempdir.size.used = 53; +} + +void MonitorTest::AddLogInfo1() { + SMonLogItem log1 = {0}; + log1.ts = taosGetTimestampMs(); + log1.level = 1; + strcpy(log1.content, "1 -------------------------- a"); + monAddLogItem(&log1); + + SMonLogItem log2 = {0}; + log2.ts = taosGetTimestampMs(); + log2.level = 1; + strcpy(log2.content, "1 ------------------------ b"); + monAddLogItem(&log2); + + SMonLogItem log3 = {0}; + log3.ts = taosGetTimestampMs(); + log3.level = 1; + strcpy(log3.content, "1 ------- c"); + monAddLogItem(&log3); +} + +void MonitorTest::AddLogInfo2() { + SMonLogItem log1; + log1.ts = taosGetTimestampMs(); + log1.level = 01; + strcpy(log1.content, "2 ------- a"); + monAddLogItem(&log1); + + SMonLogItem log2; + log2.ts = taosGetTimestampMs(); + log2.level = 0; + strcpy(log2.content, "2 ------- b"); + monAddLogItem(&log2); +} + +TEST_F(MonitorTest, 01_Full) { + AddLogInfo1(); + + SMonInfo *pMonitor = monCreateMonitorInfo(); + if (pMonitor == NULL) return; + + SMonBasicInfo basicInfo = {0}; + GetBasicInfo(pMonitor, &basicInfo); + monSetBasicInfo(pMonitor, &basicInfo); + + SMonClusterInfo clusterInfo = {0}; + SMonVgroupInfo vgroupInfo = {0}; + SMonGrantInfo grantInfo = {0}; + GetClusterInfo(pMonitor, &clusterInfo); + GetVgroupInfo(pMonitor, &vgroupInfo); + GetGrantInfo(pMonitor, &grantInfo); + monSetClusterInfo(pMonitor, &clusterInfo); + monSetVgroupInfo(pMonitor, &vgroupInfo); + monSetGrantInfo(pMonitor, &grantInfo); + + SMonDnodeInfo dnodeInfo = {0}; + GetDnodeInfo(pMonitor, &dnodeInfo); + monSetDnodeInfo(pMonitor, &dnodeInfo); + + SMonDiskInfo diskInfo = {0}; + GetDiskInfo(pMonitor, &diskInfo); + monSetDiskInfo(pMonitor, &diskInfo); + + monSendReport(pMonitor); + monCleanupMonitorInfo(pMonitor); + + taosArrayDestroy(clusterInfo.dnodes); + taosArrayDestroy(clusterInfo.mnodes); + taosArrayDestroy(vgroupInfo.vgroups); + taosArrayDestroy(diskInfo.datadirs); +} + +TEST_F(MonitorTest, 02_Log) { + AddLogInfo2(); + + SMonInfo *pMonitor = monCreateMonitorInfo(); + if (pMonitor == NULL) return; + + monSendReport(pMonitor); + monCleanupMonitorInfo(pMonitor); } From 6cfff92b21d0d38e7e9e155402f1b40e62a7f522 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 3 Mar 2022 17:11:24 +0800 Subject: [PATCH 13/19] get tfs monitor info --- include/libs/tfs/tfs.h | 9 +++++++++ source/dnode/mgmt/impl/inc/dndEnv.h | 5 ++--- source/dnode/mgmt/impl/src/dndEnv.c | 9 ++++++++- source/dnode/mgmt/impl/src/dndMgmt.c | 15 ++++++++++----- source/libs/tfs/CMakeLists.txt | 2 +- source/libs/tfs/src/tfs.c | 21 +++++++++++++++++++++ 6 files changed, 51 insertions(+), 10 deletions(-) diff --git a/include/libs/tfs/tfs.h b/include/libs/tfs/tfs.h index b6d10c81bf..1b41da33bb 100644 --- a/include/libs/tfs/tfs.h +++ b/include/libs/tfs/tfs.h @@ -17,6 +17,7 @@ #define _TD_TFS_H_ #include "tdef.h" +#include "monitor.h" #ifdef __cplusplus extern "C" { @@ -237,6 +238,14 @@ const STfsFile *tfsReaddir(STfsDir *pDir); */ void tfsClosedir(STfsDir *pDir); +/** + * @brief Get disk info of tfs. + * + * @param pTfs The fs object. + * @param pInfo The info object. + */ +int32_t tfsGetMonitorInfo(STfs *pTfs, SMonDiskInfo *pInfo); + #ifdef __cplusplus } #endif diff --git a/source/dnode/mgmt/impl/inc/dndEnv.h b/source/dnode/mgmt/impl/inc/dndEnv.h index 724ba30155..efe54ee1e5 100644 --- a/source/dnode/mgmt/impl/inc/dndEnv.h +++ b/source/dnode/mgmt/impl/inc/dndEnv.h @@ -31,7 +31,7 @@ typedef struct { SDnode *pDnode; STaosQueue *queue; union { - SQWorkerPool pool; + SQWorkerPool pool; SWWorkerPool mpool; }; } SDnodeWorker; @@ -137,8 +137,7 @@ typedef struct SDnode { SStartupReq startup; } SDnode; - -int32_t dndGetDiskInfo(SDnode *pDnode, SMonDiskInfo *pInfo); +int32_t dndGetMonitorDiskInfo(SDnode *pDnode, SMonDiskInfo *pInfo); #ifdef __cplusplus } diff --git a/source/dnode/mgmt/impl/src/dndEnv.c b/source/dnode/mgmt/impl/src/dndEnv.c index ae08d6387c..1cbdf0c1dc 100644 --- a/source/dnode/mgmt/impl/src/dndEnv.c +++ b/source/dnode/mgmt/impl/src/dndEnv.c @@ -324,4 +324,11 @@ void dndCleanup() { dInfo("dnode env is cleaned up"); } -int32_t dndGetDiskInfo(SDnode *pDnode, SMonDiskInfo *pInfo) { return 0; } \ No newline at end of file +int32_t dndGetMonitorDiskInfo(SDnode *pDnode, SMonDiskInfo *pInfo) { + tstrncpy(pInfo->logdir.name, tsLogDir, sizeof(pInfo->logdir.name)); + pInfo->logdir.size = tsLogSpace.size; + tstrncpy(pInfo->tempdir.name, tsTempDir, sizeof(pInfo->tempdir.name)); + pInfo->tempdir.size = tsTempSpace.size; + + return tfsGetMonitorInfo(pDnode->pTfs, pInfo); +} \ No newline at end of file diff --git a/source/dnode/mgmt/impl/src/dndMgmt.c b/source/dnode/mgmt/impl/src/dndMgmt.c index 268ed7e362..70f5060ead 100644 --- a/source/dnode/mgmt/impl/src/dndMgmt.c +++ b/source/dnode/mgmt/impl/src/dndMgmt.c @@ -474,13 +474,13 @@ void dndProcessStartupReq(SDnode *pDnode, SRpcMsg *pReq) { rpcSendResponse(&rpcRsp); } -static int32_t dndGetBasicInfo(SDnode *pDnode, SMonBasicInfo *pInfo) { +static int32_t dndGetMonitorBasicInfo(SDnode *pDnode, SMonBasicInfo *pInfo) { pInfo->dnode_id = dndGetDnodeId(pDnode); tstrncpy(pInfo->dnode_ep, tsLocalEp, TSDB_EP_LEN); return 0; } -static int32_t dndGetDnodeInfo(SDnode *pDnode, SMonDnodeInfo *pInfo) { return 0; } +static int32_t dndGetMonitorDnodeInfo(SDnode *pDnode, SMonDnodeInfo *pInfo) { return 0; } static void dndSendMonitorReport(SDnode *pDnode) { if (!tsEnableMonitor || tsMonitorFqdn[0] == 0 || tsMonitorPort == 0) return; @@ -490,7 +490,7 @@ static void dndSendMonitorReport(SDnode *pDnode) { if (pMonitor == NULL) return; SMonBasicInfo basicInfo = {0}; - if (dndGetBasicInfo(pDnode, &basicInfo) == 0) { + if (dndGetMonitorBasicInfo(pDnode, &basicInfo) == 0) { monSetBasicInfo(pMonitor, &basicInfo); } @@ -504,15 +504,20 @@ static void dndSendMonitorReport(SDnode *pDnode) { } SMonDnodeInfo dnodeInfo = {0}; - if (dndGetDnodeInfo(pDnode, &dnodeInfo) == 0) { + if (dndGetMonitorDnodeInfo(pDnode, &dnodeInfo) == 0) { monSetDnodeInfo(pMonitor, &dnodeInfo); } SMonDiskInfo diskInfo = {0}; - if (dndGetDiskInfo(pDnode, &diskInfo) == 0) { + if (dndGetMonitorDiskInfo(pDnode, &diskInfo) == 0) { monSetDiskInfo(pMonitor, &diskInfo); } + taosArrayDestroy(clusterInfo.dnodes); + taosArrayDestroy(clusterInfo.mnodes); + taosArrayDestroy(vgroupInfo.vgroups); + taosArrayDestroy(diskInfo.datadirs); + monSendReport(pMonitor); monCleanupMonitorInfo(pMonitor); } diff --git a/source/libs/tfs/CMakeLists.txt b/source/libs/tfs/CMakeLists.txt index 607ccd4c48..97e02fc8a9 100644 --- a/source/libs/tfs/CMakeLists.txt +++ b/source/libs/tfs/CMakeLists.txt @@ -6,7 +6,7 @@ target_include_directories( PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) -target_link_libraries(tfs os util common) +target_link_libraries(tfs os util common monitor) if(${BUILD_TEST}) add_subdirectory(test) diff --git a/source/libs/tfs/src/tfs.c b/source/libs/tfs/src/tfs.c index f686703643..944e67c863 100644 --- a/source/libs/tfs/src/tfs.c +++ b/source/libs/tfs/src/tfs.c @@ -544,3 +544,24 @@ static STfsDisk *tfsNextDisk(STfs *pTfs, SDiskIter *pIter) { return pDisk; } + +int32_t tfsGetMonitorInfo(STfs *pTfs, SMonDiskInfo *pInfo) { + pInfo->datadirs = taosArrayInit(32, sizeof(SMonDiskDesc)); + if (pInfo->datadirs == NULL) return -1; + + tfsUpdateSize(pTfs); + + tfsLock(pTfs); + for (int32_t level = 0; level < pTfs->nlevel; level++) { + STfsTier *pTier = &pTfs->tiers[level]; + for (int32_t disk = 0; disk < pTier->ndisk; ++disk) { + STfsDisk *pDisk = pTier->disks[disk]; + SMonDiskDesc dinfo = {0}; + dinfo.size = pDisk->size; + dinfo.level = pDisk->level; + tstrncpy(dinfo.name, pDisk->path, sizeof(dinfo.name)); + taosArrayPush(pInfo->datadirs, &dinfo); + } + } + tfsUnLock(pTfs); +} \ No newline at end of file From 64d224a0d2b5450e221b0136b8a80b8d57184025 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Thu, 3 Mar 2022 17:28:00 +0800 Subject: [PATCH 14/19] syncInt --- include/libs/sync/sync.h | 4 +- source/libs/sync/inc/syncInt.h | 72 +++++++++++++++++++----------- source/libs/sync/inc/syncVoteMgr.h | 6 +++ source/libs/sync/src/syncMain.c | 2 +- 4 files changed, 54 insertions(+), 30 deletions(-) diff --git a/include/libs/sync/sync.h b/include/libs/sync/sync.h index cd04783dbc..53fad4607a 100644 --- a/include/libs/sync/sync.h +++ b/include/libs/sync/sync.h @@ -34,9 +34,7 @@ typedef enum { TAOS_SYNC_STATE_FOLLOWER = 0, TAOS_SYNC_STATE_CANDIDATE = 1, TAOS_SYNC_STATE_LEADER = 2, -} ESyncRole; - -typedef ESyncRole ESyncState; +} ESyncState; typedef struct SSyncBuffer { void* data; diff --git a/source/libs/sync/inc/syncInt.h b/source/libs/sync/inc/syncInt.h index 0901330488..aedb9662b1 100644 --- a/source/libs/sync/inc/syncInt.h +++ b/source/libs/sync/inc/syncInt.h @@ -25,6 +25,7 @@ extern "C" { #include #include "sync.h" #include "taosdef.h" +#include "tglobal.h" #include "tlog.h" #include "ttimer.h" @@ -91,31 +92,61 @@ typedef struct SyncAppendEntriesReply SyncAppendEntriesReply; struct SSyncEnv; typedef struct SSyncEnv SSyncEnv; +struct SRaftStore; +typedef struct SRaftStore SRaftStore; + +struct SVotesGranted; +typedef struct SVotesGranted SVotesGranted; + +struct SVotesResponded; +typedef struct SVotesResponded SVotesResponded; + typedef struct SRaftId { SyncNodeId addr; // typedef uint64_t SyncNodeId; SyncGroupId vgId; // typedef int32_t SyncGroupId; } SRaftId; typedef struct SSyncNode { + // init by SSyncInfo SyncGroupId vgId; SSyncCfg syncCfg; char path[TSDB_FILENAME_LEN]; - SSyncFSM* pFsm; - - // passed from outside - void* rpcClient; + void* rpcClient; int32_t (*FpSendMsg)(void* rpcClient, const SEpSet* pEpSet, SRpcMsg* pMsg); + // init internal + SNodeInfo me; + int32_t peersNum; + SNodeInfo peers[TSDB_MAX_REPLICA]; + + // raft algorithm + SSyncFSM* pFsm; + SRaftId raftId; + SRaftId peersId[TSDB_MAX_REPLICA]; + int32_t replicaNum; + int32_t quorum; + + // life cycle int32_t refCount; int64_t rid; - SNodeInfo me; - SNodeInfo peers[TSDB_MAX_REPLICA]; - int32_t peersNum; + // tla+ server vars + ESyncState state; + SRaftStore* pRaftStore; - ESyncRole role; - SRaftId raftId; + // tla+ candidate vars + SVotesGranted* pVotesGranted; + SVotesResponded* pVotesResponded; + // tla+ leader vars + SHashObj* pNextIndex; + SHashObj* pMatchIndex; + + // tla+ log vars + SSyncLogStore* pLogStore; + SyncIndex commitIndex; + + // timer tmr_h pPingTimer; int32_t pingTimerMS; uint8_t pingTimerStart; @@ -136,32 +167,21 @@ typedef struct SSyncNode { // callback int32_t (*FpOnPing)(SSyncNode* ths, SyncPing* pMsg); - int32_t (*FpOnPingReply)(SSyncNode* ths, SyncPingReply* pMsg); - int32_t (*FpOnRequestVote)(SSyncNode* ths, SyncRequestVote* pMsg); - int32_t (*FpOnRequestVoteReply)(SSyncNode* ths, SyncRequestVoteReply* pMsg); - int32_t (*FpOnAppendEntries)(SSyncNode* ths, SyncAppendEntries* pMsg); - int32_t (*FpOnAppendEntriesReply)(SSyncNode* ths, SyncAppendEntriesReply* pMsg); } SSyncNode; SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo); - -void syncNodeClose(SSyncNode* pSyncNode); - -void syncNodePingAll(SSyncNode* pSyncNode); - -void syncNodePingPeers(SSyncNode* pSyncNode); - -void syncNodePingSelf(SSyncNode* pSyncNode); - -int32_t syncNodeStartPingTimer(SSyncNode* pSyncNode); - -int32_t syncNodeStopPingTimer(SSyncNode* pSyncNode); +void syncNodeClose(SSyncNode* pSyncNode); +void syncNodePingAll(SSyncNode* pSyncNode); +void syncNodePingPeers(SSyncNode* pSyncNode); +void syncNodePingSelf(SSyncNode* pSyncNode); +int32_t syncNodeStartPingTimer(SSyncNode* pSyncNode); +int32_t syncNodeStopPingTimer(SSyncNode* pSyncNode); #ifdef __cplusplus } diff --git a/source/libs/sync/inc/syncVoteMgr.h b/source/libs/sync/inc/syncVoteMgr.h index cfcf58bee2..b841f2e316 100644 --- a/source/libs/sync/inc/syncVoteMgr.h +++ b/source/libs/sync/inc/syncVoteMgr.h @@ -26,6 +26,12 @@ extern "C" { #include "syncInt.h" #include "taosdef.h" +typedef struct SVotesGranted { +} SVotesGranted; + +typedef struct SVotesResponded { +} SVotesResponded; + #ifdef __cplusplus } #endif diff --git a/source/libs/sync/src/syncMain.c b/source/libs/sync/src/syncMain.c index 9cb3a61fff..7e01e7e81c 100644 --- a/source/libs/sync/src/syncMain.c +++ b/source/libs/sync/src/syncMain.c @@ -88,7 +88,7 @@ SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo) { } } - pSyncNode->role = TAOS_SYNC_STATE_FOLLOWER; + pSyncNode->state = TAOS_SYNC_STATE_FOLLOWER; syncUtilnodeInfo2raftId(&pSyncNode->me, pSyncNode->vgId, &pSyncNode->raftId); pSyncNode->pPingTimer = NULL; From c018e91859645c280a8f12d7784a0ba87abd8885 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 3 Mar 2022 17:30:33 +0800 Subject: [PATCH 15/19] minor changes --- include/os/osSysinfo.h | 6 +- source/dnode/mnode/impl/src/mndTelem.c | 4 +- source/os/src/osSysinfo.c | 110 ++++++++++++------------- 3 files changed, 58 insertions(+), 62 deletions(-) diff --git a/include/os/osSysinfo.h b/include/os/osSysinfo.h index bf9d7662e6..7ad9c57a3c 100644 --- a/include/os/osSysinfo.h +++ b/include/os/osSysinfo.h @@ -34,9 +34,9 @@ typedef struct { } SDiskSpace; void taosGetSystemInfo(); -bool taosGetEmail(char *email, int32_t maxLen); -bool taosGetOsReleaseName(char *releaseName, int32_t maxLen); -bool taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores); +int32_t taosGetEmail(char *email, int32_t maxLen); +int32_t taosGetOsReleaseName(char *releaseName, int32_t maxLen); +int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores); int32_t taosGetCpuCores(); bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage); bool taosGetTotalSysMemoryKB(uint64_t *kb); diff --git a/source/dnode/mnode/impl/src/mndTelem.c b/source/dnode/mnode/impl/src/mndTelem.c index 1cd7593846..da2c52ec8f 100644 --- a/source/dnode/mnode/impl/src/mndTelem.c +++ b/source/dnode/mnode/impl/src/mndTelem.c @@ -52,12 +52,12 @@ static char* mndBuildTelemetryReport(SMnode* pMnode) { tjsonAddStringToObject(pJson, "instanceId", clusterName); tjsonAddDoubleToObject(pJson, "reportVersion", 1); - if (taosGetOsReleaseName(tmp, sizeof(tmp))) { + if (taosGetOsReleaseName(tmp, sizeof(tmp)) == 0) { tjsonAddStringToObject(pJson, "os", tmp); } int32_t numOfCores = 0; - if (taosGetCpuInfo(tmp, sizeof(tmp), &numOfCores)) { + if (taosGetCpuInfo(tmp, sizeof(tmp), &numOfCores) == 0) { tjsonAddStringToObject(pJson, "cpuModel", tmp); tjsonAddDoubleToObject(pJson, "numOfCpu", numOfCores); } else { diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index 3e761c6d91..d3f56e5901 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -80,7 +80,6 @@ bool taosGetProcMemory(float *memoryUsedMB) { return true; } - int32_t taosGetCpuCores() { SYSTEM_INFO info; GetSystemInfo(&info); @@ -106,7 +105,7 @@ int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) { diskSize->used = (int64_t)(i64TotalBytes - i64FreeBytes); return 0; } else { - //printf("failed to get disk size, dataDir:%s errno:%s", tsDataDir, strerror(errno)); + // printf("failed to get disk size, dataDir:%s errno:%s", tsDataDir, strerror(errno)); terrno = TAOS_SYSTEM_ERROR(errno); return -1; } @@ -173,12 +172,12 @@ void taosGetSystemInfo() { } void taosKillSystem() { - //printf("function taosKillSystem, exit!"); + // printf("function taosKillSystem, exit!"); exit(0); } int taosSystem(const char *cmd) { - //printf("taosSystem not support"); + // printf("taosSystem not support"); return -1; } @@ -241,9 +240,8 @@ char *taosGetCmdlineByPID(int pid) { return ""; } #include #include - void taosKillSystem() { - //printf("function taosKillSystem, exit!"); + // printf("function taosKillSystem, exit!"); exit(0); } @@ -300,7 +298,7 @@ bool taosGetSysMemory(float *memoryUsedMB) { } int taosSystem(const char *cmd) { - //printf("un support funtion"); + // printf("un support funtion"); return -1; } @@ -309,7 +307,7 @@ 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)); + // printf("failed to get disk size, dataDir:%s errno:%s", tsDataDir, strerror(errno)); terrno = TAOS_SYSTEM_ERROR(errno); return -1; } else { @@ -404,12 +402,12 @@ bool taosGetProcMemory(float *memoryUsedMB) { // FILE *fp = fopen(tsProcMemFile, "r"); TdFilePtr pFile = taosOpenFile(tsProcMemFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { - //printf("open file:%s failed", tsProcMemFile); + // printf("open file:%s failed", tsProcMemFile); return false; } ssize_t _bytes = 0; - char * line = NULL; + char *line = NULL; while (!taosEOFFile(pFile)) { _bytes = taosGetLineFile(pFile, &line); if ((_bytes < 0) || (line == NULL)) { @@ -421,7 +419,7 @@ bool taosGetProcMemory(float *memoryUsedMB) { } if (line == NULL) { - //printf("read file:%s failed", tsProcMemFile); + // printf("read file:%s failed", tsProcMemFile); taosCloseFile(&pFile); return false; } @@ -431,7 +429,7 @@ bool taosGetProcMemory(float *memoryUsedMB) { sscanf(line, "%s %" PRId64, tmp, &memKB); *memoryUsedMB = (float)((double)memKB / 1024); - if(line != NULL) tfree(line); + if (line != NULL) tfree(line); taosCloseFile(&pFile); return true; } @@ -440,14 +438,14 @@ static bool taosGetSysCpuInfo(SysCpuInfo *cpuInfo) { // FILE *fp = fopen(tsSysCpuFile, "r"); TdFilePtr pFile = taosOpenFile(tsSysCpuFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { - //printf("open file:%s failed", tsSysCpuFile); + // printf("open file:%s failed", tsSysCpuFile); return false; } - char * line = NULL; + char *line = NULL; ssize_t _bytes = taosGetLineFile(pFile, &line); if ((_bytes < 0) || (line == NULL)) { - //printf("read file:%s failed", tsSysCpuFile); + // printf("read file:%s failed", tsSysCpuFile); taosCloseFile(&pFile); return false; } @@ -456,7 +454,7 @@ static bool taosGetSysCpuInfo(SysCpuInfo *cpuInfo) { sscanf(line, "%s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, cpu, &cpuInfo->user, &cpuInfo->nice, &cpuInfo->system, &cpuInfo->idle); - if(line != NULL) tfree(line); + if (line != NULL) tfree(line); taosCloseFile(&pFile); return true; } @@ -465,14 +463,14 @@ static bool taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) { // FILE *fp = fopen(tsProcCpuFile, "r"); TdFilePtr pFile = taosOpenFile(tsProcCpuFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { - //printf("open file:%s failed", tsProcCpuFile); + // printf("open file:%s failed", tsProcCpuFile); return false; } - char * line = NULL; + char *line = NULL; ssize_t _bytes = taosGetLineFile(pFile, &line); if ((_bytes < 0) || (line == NULL)) { - //printf("read file:%s failed", tsProcCpuFile); + // printf("read file:%s failed", tsProcCpuFile); taosCloseFile(&pFile); return false; } @@ -486,12 +484,11 @@ static bool taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) { } } - if(line != NULL) tfree(line); + if (line != NULL) tfree(line); taosCloseFile(&pFile); return true; } - int32_t taosGetCpuCores() { return (int32_t)sysconf(_SC_NPROCESSORS_ONLN); } bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { @@ -550,12 +547,12 @@ bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { // FILE *fp = fopen(tsSysNetFile, "r"); TdFilePtr pFile = taosOpenFile(tsSysNetFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { - //printf("open file:%s failed", tsSysNetFile); + // printf("open file:%s failed", tsSysNetFile); return false; } ssize_t _bytes = 0; - char * line = NULL; + char *line = NULL; while (!taosEOFFile(pFile)) { int64_t o_rbytes = 0; @@ -590,7 +587,7 @@ bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { *bytes += (o_rbytes + o_tbytes); } - if(line != NULL) tfree(line); + if (line != NULL) tfree(line); taosCloseFile(&pFile); return true; @@ -636,12 +633,12 @@ bool taosReadProcIO(int64_t *rchars, int64_t *wchars) { // FILE *fp = fopen(tsProcIOFile, "r"); TdFilePtr pFile = taosOpenFile(tsProcIOFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { - //printf("open file:%s failed", tsProcIOFile); + // printf("open file:%s failed", tsProcIOFile); return false; } ssize_t _bytes = 0; - char * line = NULL; + char *line = NULL; char tmp[10]; int readIndex = 0; @@ -662,11 +659,11 @@ bool taosReadProcIO(int64_t *rchars, int64_t *wchars) { if (readIndex >= 2) break; } - if(line != NULL) tfree(line); + if (line != NULL) tfree(line); taosCloseFile(&pFile); if (readIndex < 2) { - //printf("read file:%s failed", tsProcIOFile); + // printf("read file:%s failed", tsProcIOFile); return false; } @@ -713,12 +710,11 @@ void taosGetSystemInfo() { taosGetBandSpeed(&tmp1); taosGetCpuUsage(&tmp1, &tmp2); taosGetProcIO(&tmp1, &tmp2); - } void taosKillSystem() { // SIGINT - //printf("taosd will shut down soon"); + // printf("taosd will shut down soon"); kill(tsProcId, 2); } @@ -727,22 +723,22 @@ int taosSystem(const char *cmd) { int res; char buf[1024]; if (cmd == NULL) { - //printf("taosSystem cmd is NULL!"); + // printf("taosSystem cmd is NULL!"); return -1; } if ((fp = popen(cmd, "r")) == NULL) { - //printf("popen cmd:%s error: %s", cmd, strerror(errno)); + // printf("popen cmd:%s error: %s", cmd, strerror(errno)); return -1; } else { while (fgets(buf, sizeof(buf), fp)) { - //printf("popen result:%s", buf); + // printf("popen result:%s", buf); } if ((res = pclose(fp)) == -1) { - //printf("close popen file pointer fp error!"); + // printf("close popen file pointer fp error!"); } else { - //printf("popen res is :%d", res); + // printf("popen res is :%d", res); } return res; @@ -757,14 +753,14 @@ void taosSetCoreDump(bool enable) { struct rlimit rlim_new; if (getrlimit(RLIMIT_CORE, &rlim) == 0) { #ifndef _ALPINE - //printf("the old unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max); + // printf("the old unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max); #else - //printf("the old unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max); + // printf("the old unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max); #endif rlim_new.rlim_cur = RLIM_INFINITY; rlim_new.rlim_max = RLIM_INFINITY; if (setrlimit(RLIMIT_CORE, &rlim_new) != 0) { - //printf("set unlimited fail, error: %s", strerror(errno)); + // printf("set unlimited fail, error: %s", strerror(errno)); rlim_new.rlim_cur = rlim.rlim_max; rlim_new.rlim_max = rlim.rlim_max; (void)setrlimit(RLIMIT_CORE, &rlim_new); @@ -773,9 +769,9 @@ void taosSetCoreDump(bool enable) { if (getrlimit(RLIMIT_CORE, &rlim) == 0) { #ifndef _ALPINE - //printf("the new unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max); + // printf("the new unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max); #else - //printf("the new unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max); + // printf("the new unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max); #endif } @@ -801,10 +797,10 @@ void taosSetCoreDump(bool enable) { old_len = sizeof(old_usespid); if (syscall(SYS__sysctl, &args) == -1) { - //printf("_sysctl(kern_core_uses_pid) set fail: %s", strerror(errno)); + // printf("_sysctl(kern_core_uses_pid) set fail: %s", strerror(errno)); } - //printf("The old core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid); + // printf("The old core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid); old_usespid = 0; old_len = 0; @@ -817,10 +813,10 @@ void taosSetCoreDump(bool enable) { old_len = sizeof(old_usespid); if (syscall(SYS__sysctl, &args) == -1) { - //printf("_sysctl(kern_core_uses_pid) get fail: %s", strerror(errno)); + // printf("_sysctl(kern_core_uses_pid) get fail: %s", strerror(errno)); } - //printf("The new core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid); + // printf("The new core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid); #endif } @@ -885,7 +881,7 @@ SysNameInfo taosGetSysNameInfo() { return info; } -bool taosGetEmail(char *email, int32_t maxLen) { +int32_t taosGetEmail(char *email, int32_t maxLen) { const char *filepath = "/usr/local/taos/email"; TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ); @@ -893,17 +889,17 @@ bool taosGetEmail(char *email, int32_t maxLen) { if (taosReadFile(pFile, (void *)email, maxLen) < 0) { taosCloseFile(&pFile); - return false; + return -1; } taosCloseFile(&pFile); - return true; + return 0; } -bool taosGetOsReleaseName(char *releaseName, int32_t maxLen) { - char *line = NULL; - size_t size = 0; - bool ret = false; +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; @@ -917,21 +913,21 @@ bool taosGetOsReleaseName(char *releaseName, int32_t maxLen) { line[size - 2] = 0; } tstrncpy(releaseName, p, maxLen); - ret = true; + code = 0; break; } } if (line != NULL) free(line); taosCloseFile(&pFile); - return ret; + return code; } -bool taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores) { +int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores) { char *line = NULL; size_t size = 0; int32_t done = 0; - bool ret = false; + int32_t code = -1; TdFilePtr pFile = taosOpenFile("/proc/cpuinfo", TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) return false; @@ -941,7 +937,7 @@ bool taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores) { if (((done & 1) == 0) && strncmp(line, "model name", 10) == 0) { const char *v = strchr(line, ':') + 2; tstrncpy(cpuModel, v, maxLen); - ret = true; + code = 0; done |= 1; } else if (((done & 2) == 0) && strncmp(line, "cpu cores", 9) == 0) { const char *v = strchr(line, ':') + 2; @@ -953,7 +949,7 @@ bool taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores) { if (line != NULL) free(line); taosCloseFile(&pFile); - return ret; + return code; } bool taosGetTotalSysMemoryKB(uint64_t *kb) { From c890b0b5c1987ffbec0186c5c72ba4a0db27d100 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 3 Mar 2022 18:10:56 +0800 Subject: [PATCH 16/19] sysinfo in monitor --- include/libs/monitor/monitor.h | 6 +- include/os/osEnv.h | 6 +- include/os/osSysinfo.h | 12 +- source/common/src/tglobal.c | 10 +- source/dnode/mgmt/impl/src/dndMgmt.c | 43 ++++-- source/dnode/mnode/impl/src/mndTelem.c | 11 +- source/os/src/osEnv.c | 11 +- source/os/src/osSysinfo.c | 183 +++++++++++-------------- 8 files changed, 140 insertions(+), 142 deletions(-) diff --git a/include/libs/monitor/monitor.h b/include/libs/monitor/monitor.h index b3b6091b95..487b2bfe88 100644 --- a/include/libs/monitor/monitor.h +++ b/include/libs/monitor/monitor.h @@ -88,9 +88,9 @@ typedef struct { float cpu_engine; float cpu_system; float cpu_cores; - float mem_engine; // MB - float mem_system; // MB - float mem_total; // MB + int64_t mem_engine; // KB + int64_t mem_system; // KB + int64_t mem_total; // KB float disk_engine; // GB float disk_used; // GB float disk_total; // GB diff --git a/include/os/osEnv.h b/include/os/osEnv.h index 4ac073f6c2..1426ba87f6 100644 --- a/include/os/osEnv.h +++ b/include/os/osEnv.h @@ -28,11 +28,11 @@ extern char tsCharset[]; extern char tsLocale[]; extern int8_t tsDaylight; extern bool tsEnableCoreFile; -extern int64_t tsPageSize; +extern int64_t tsPageSizeKB; extern int64_t tsOpenMax; extern int64_t tsStreamMax; -extern int32_t tsNumOfCores; -extern int32_t tsTotalMemoryMB; +extern float tsNumOfCores; +extern int64_t tsTotalMemoryKB; extern char configDir[]; extern char tsDataDir[]; diff --git a/include/os/osSysinfo.h b/include/os/osSysinfo.h index 7ad9c57a3c..df5ed28a99 100644 --- a/include/os/osSysinfo.h +++ b/include/os/osSysinfo.h @@ -36,12 +36,12 @@ typedef struct { void taosGetSystemInfo(); int32_t taosGetEmail(char *email, int32_t maxLen); int32_t taosGetOsReleaseName(char *releaseName, int32_t maxLen); -int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores); -int32_t taosGetCpuCores(); -bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage); -bool taosGetTotalSysMemoryKB(uint64_t *kb); -bool taosGetProcMemory(float *memoryUsedMB); // -bool taosGetSysMemory(float *memoryUsedMB); // +int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores); +int32_t taosGetCpuCores(float *numOfCores); +int32_t taosGetCpuUsage(float *cpu_system, float *cpu_engine); +int32_t taosGetTotalMemory(int64_t *totalKB); +int32_t taosGetProcMemory(int64_t *usedKB); +int32_t taosGetSysMemory(int64_t *usedKB); int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize); bool taosReadProcIO(int64_t *rchars, int64_t *wchars); bool taosGetProcIO(float *readKB, float *writeKB); diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index 5998b7c9ce..93b66e3369 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -279,11 +279,11 @@ static int32_t taosAddSystemCfg(SConfig *pCfg) { if (cfgAddLocale(pCfg, "locale", tsLocale) != 0) return -1; if (cfgAddCharset(pCfg, "charset", tsCharset) != 0) return -1; if (cfgAddBool(pCfg, "enableCoreFile", 1, 1) != 0) return -1; - if (cfgAddInt32(pCfg, "numOfCores", tsNumOfCores, 1, 100000, 1) != 0) return -1; - if (cfgAddInt32(pCfg, "pageSize(KB)", tsPageSize, 0, INT64_MAX, 1) != 0) return -1; + if (cfgAddFloat(pCfg, "numOfCores", tsNumOfCores, 0, 100000, 1) != 0) return -1; if (cfgAddInt64(pCfg, "openMax", tsOpenMax, 0, INT64_MAX, 1) != 0) return -1; if (cfgAddInt64(pCfg, "streamMax", tsStreamMax, 0, INT64_MAX, 1) != 0) return -1; - if (cfgAddInt32(pCfg, "totalMemory(MB)", tsTotalMemoryMB, 0, INT32_MAX, 1) != 0) return -1; + if (cfgAddInt32(pCfg, "pageSize(KB)", tsPageSizeKB, 0, INT64_MAX, 1) != 0) return -1; + if (cfgAddInt64(pCfg, "totalMemory(KB)", tsTotalMemoryKB, 0, INT64_MAX, 1) != 0) return -1; if (cfgAddString(pCfg, "os sysname", info.sysname, 1) != 0) return -1; if (cfgAddString(pCfg, "os nodename", info.nodename, 1) != 0) return -1; if (cfgAddString(pCfg, "os release", info.release, 1) != 0) return -1; @@ -404,10 +404,6 @@ static void taosSetSystemCfg(SConfig *pCfg) { const char *charset = cfgGetItem(pCfg, "charset")->str; taosSetSystemLocale(locale, charset); - if (tsNumOfCores <= 1) { - tsNumOfCores = 2; - } - bool enableCore = cfgGetItem(pCfg, "enableCoreFile")->bval; taosSetConsoleEcho(enableCore); diff --git a/source/dnode/mgmt/impl/src/dndMgmt.c b/source/dnode/mgmt/impl/src/dndMgmt.c index 70f5060ead..704847894e 100644 --- a/source/dnode/mgmt/impl/src/dndMgmt.c +++ b/source/dnode/mgmt/impl/src/dndMgmt.c @@ -474,13 +474,40 @@ void dndProcessStartupReq(SDnode *pDnode, SRpcMsg *pReq) { rpcSendResponse(&rpcRsp); } -static int32_t dndGetMonitorBasicInfo(SDnode *pDnode, SMonBasicInfo *pInfo) { +static void dndGetMonitorBasicInfo(SDnode *pDnode, SMonBasicInfo *pInfo) { pInfo->dnode_id = dndGetDnodeId(pDnode); tstrncpy(pInfo->dnode_ep, tsLocalEp, TSDB_EP_LEN); - return 0; } -static int32_t dndGetMonitorDnodeInfo(SDnode *pDnode, SMonDnodeInfo *pInfo) { return 0; } +static void dndGetMonitorDnodeInfo(SDnode *pDnode, SMonDnodeInfo *pInfo) { + pInfo->uptime = (taosGetTimestampMs() - pDnode->dmgmt.rebootTime) / (86400000.0f); + taosGetCpuUsage(&pInfo->cpu_engine, &pInfo->cpu_system); + pInfo->cpu_cores = tsNumOfCores; + taosGetProcMemory(&pInfo->mem_engine); + taosGetSysMemory(&pInfo->mem_system); + pInfo->mem_total = tsTotalMemoryKB; + pInfo->disk_engine = 4.1; + pInfo->disk_used = 4.2; + pInfo->disk_total = 4.3; + pInfo->net_in = 5.1; + pInfo->net_out = 5.2; + pInfo->io_read = 6.1; + pInfo->io_write = 6.2; + pInfo->io_read_disk = 7.1; + pInfo->io_write_disk = 7.2; + pInfo->req_select = 8; + pInfo->req_select_rate = 8.1; + pInfo->req_insert = 9; + pInfo->req_insert_success = 10; + pInfo->req_insert_rate = 10.1; + pInfo->req_insert_batch = 11; + pInfo->req_insert_batch_success = 12; + pInfo->req_insert_batch_rate = 12.3; + pInfo->errors = 4; + pInfo->vnodes_num = 5; + pInfo->masters = 6; + pInfo->has_mnode = 1; +} static void dndSendMonitorReport(SDnode *pDnode) { if (!tsEnableMonitor || tsMonitorFqdn[0] == 0 || tsMonitorPort == 0) return; @@ -490,9 +517,8 @@ static void dndSendMonitorReport(SDnode *pDnode) { if (pMonitor == NULL) return; SMonBasicInfo basicInfo = {0}; - if (dndGetMonitorBasicInfo(pDnode, &basicInfo) == 0) { - monSetBasicInfo(pMonitor, &basicInfo); - } + dndGetMonitorBasicInfo(pDnode, &basicInfo); + monSetBasicInfo(pMonitor, &basicInfo); SMonClusterInfo clusterInfo = {0}; SMonVgroupInfo vgroupInfo = {0}; @@ -504,9 +530,8 @@ static void dndSendMonitorReport(SDnode *pDnode) { } SMonDnodeInfo dnodeInfo = {0}; - if (dndGetMonitorDnodeInfo(pDnode, &dnodeInfo) == 0) { - monSetDnodeInfo(pMonitor, &dnodeInfo); - } + dndGetMonitorDnodeInfo(pDnode, &dnodeInfo); + monSetDnodeInfo(pMonitor, &dnodeInfo); SMonDiskInfo diskInfo = {0}; if (dndGetMonitorDiskInfo(pDnode, &diskInfo) == 0) { diff --git a/source/dnode/mnode/impl/src/mndTelem.c b/source/dnode/mnode/impl/src/mndTelem.c index da2c52ec8f..968319e7b4 100644 --- a/source/dnode/mnode/impl/src/mndTelem.c +++ b/source/dnode/mnode/impl/src/mndTelem.c @@ -56,19 +56,16 @@ static char* mndBuildTelemetryReport(SMnode* pMnode) { tjsonAddStringToObject(pJson, "os", tmp); } - int32_t numOfCores = 0; + float numOfCores = 0; if (taosGetCpuInfo(tmp, sizeof(tmp), &numOfCores) == 0) { tjsonAddStringToObject(pJson, "cpuModel", tmp); tjsonAddDoubleToObject(pJson, "numOfCpu", numOfCores); } else { - tjsonAddDoubleToObject(pJson, "numOfCpu", taosGetCpuCores()); + tjsonAddDoubleToObject(pJson, "numOfCpu", tsNumOfCores); } - uint64_t memoryKB = 0; - if (taosGetTotalSysMemoryKB(&memoryKB)) { - snprintf(tmp, sizeof(tmp), "%" PRIu64 " kB", memoryKB); - tjsonAddStringToObject(pJson, "memory", tmp); - } + snprintf(tmp, sizeof(tmp), "%" PRId64 " kB", tsTotalMemoryKB); + tjsonAddStringToObject(pJson, "memory", tmp); tjsonAddStringToObject(pJson, "version", version); tjsonAddStringToObject(pJson, "buildInfo", buildinfo); diff --git a/source/os/src/osEnv.c b/source/os/src/osEnv.c index 4c368fe895..0b2fe904b3 100644 --- a/source/os/src/osEnv.c +++ b/source/os/src/osEnv.c @@ -31,11 +31,11 @@ char tsLocale[TD_LOCALE_LEN] = {0}; char tsCharset[TD_CHARSET_LEN] = {0}; int8_t tsDaylight = 0; bool tsEnableCoreFile = 0; -int64_t tsPageSize = 0; +int64_t tsPageSizeKB = 0; int64_t tsOpenMax = 0; int64_t tsStreamMax = 0; -int32_t tsNumOfCores = 0; -int32_t tsTotalMemoryMB = 0; +float tsNumOfCores = 0; +int64_t tsTotalMemoryKB = 0; void osInit() { srand(taosSafeRand()); @@ -44,6 +44,11 @@ void osInit() { taosSetSystemTimezone(tsTimezone, tsTimezone, &tsDaylight); taosGetSystemInfo(); + // deadlock in query + if (tsNumOfCores < 2) { + tsNumOfCores = 2; + } + #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) taosWinSocketInit(); diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index d3f56e5901..bf036354e9 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -39,32 +39,32 @@ #include #pragma warning(pop) -static int32_t taosGetTotalMemory() { +int32_t taosGetTotalMemory(int64_t *totalKB) { MEMORYSTATUSEX memsStat; memsStat.dwLength = sizeof(memsStat); if (!GlobalMemoryStatusEx(&memsStat)) { - return 0; + return -1; } - float nMemTotal = memsStat.ullTotalPhys / (1024.0f * 1024.0f); - return (int32_t)nMemTotal; + *totalKB = memsStat.ullTotalPhys / 1024; + return 0; } -bool taosGetSysMemory(float *memoryUsedMB) { +int32_t taosGetSysMemory(int64_t *usedKB) { MEMORYSTATUSEX memsStat; memsStat.dwLength = sizeof(memsStat); if (!GlobalMemoryStatusEx(&memsStat)) { - return false; + return -1; } - float nMemFree = memsStat.ullAvailPhys / (1024.0f * 1024.0f); - float nMemTotal = memsStat.ullTotalPhys / (1024.0f * 1024.0f); + int64_t nMemFree = memsStat.ullAvailPhys / 1024; + int64_t nMemTotal = memsStat.ullTotalPhys / 1024.0; - *memoryUsedMB = nMemTotal - nMemFree; - return true; + *usedKB = nMemTotal - nMemFree; + return 0; } -bool taosGetProcMemory(float *memoryUsedMB) { +int32_t taosGetProcMemory(int64_t *usedKB) { unsigned bytes_used = 0; #if defined(_WIN64) && defined(_MSC_VER) @@ -76,20 +76,21 @@ bool taosGetProcMemory(float *memoryUsedMB) { } #endif - *memoryUsedMB = (float)bytes_used / 1024 / 1024; - return true; + *usedKB = bytes_used / 1024; + return 0; } -int32_t taosGetCpuCores() { +int32_t taosGetCpuCores(float *numOfCores) { SYSTEM_INFO info; GetSystemInfo(&info); - return (int32_t)info.dwNumberOfProcessors; + *numOfCores = info.dwNumberOfProcessors; + return 0; } -bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { +int32_t taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { *sysCpuUsage = 0; *procCpuUsage = 0; - return true; + return 0; } int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) { @@ -162,8 +163,8 @@ bool taosGetProcIO(float *readKB, float *writeKB) { } void taosGetSystemInfo() { - tsNumOfCores = taosGetCpuCores(); - tsTotalMemoryMB = taosGetTotalMemory(); + taosGetCpuCores(&tsNumOfCores); + taosGetTotalMemory(&tsTotalMemoryKB); float tmp1, tmp2; taosGetBandSpeed(&tmp1); @@ -245,16 +246,17 @@ void taosKillSystem() { exit(0); } -int32_t taosGetCpuCores() { return sysconf(_SC_NPROCESSORS_ONLN); } +int32_t taosGetCpuCores(float *numOfCores) { + *numOfCores = sysconf(_SC_NPROCESSORS_ONLN); + return 0; +} void taosGetSystemInfo() { - // taosGetProcInfos(); - - tsNumOfCores = sysconf(_SC_NPROCESSORS_ONLN); long physical_pages = sysconf(_SC_PHYS_PAGES); long page_size = sysconf(_SC_PAGESIZE); - tsTotalMemoryMB = physical_pages * page_size / (1024 * 1024); - tsPageSize = page_size; + tsTotalMemoryKB = physical_pages * page_size / 1024; + tsPageSizeKB = page_size / 1024; + tsNumOfCores = sysconf(_SC_NPROCESSORS_ONLN); } bool taosReadProcIO(int64_t *rchars, int64_t *wchars) { @@ -281,20 +283,20 @@ bool taosGetBandSpeed(float *bandSpeedKb) { return true; } -bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { +int32_t taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { *sysCpuUsage = 0; *procCpuUsage = 0; - return true; + return 0; } -bool taosGetProcMemory(float *memoryUsedMB) { - *memoryUsedMB = 0; - return true; +int32_t taosGetProcMemory(int64_t *usedKB) { + *usedKB = 0; + return 0; } -bool taosGetSysMemory(float *memoryUsedMB) { - *memoryUsedMB = 0; - return true; +int32_t taosGetSysMemory(int64_t *usedKB) { + *usedKB = 0; + return 0; } int taosSystem(const char *cmd) { @@ -375,35 +377,34 @@ static char tsSysCpuFile[] = "/proc/stat"; static char tsProcCpuFile[25] = {0}; static char tsProcMemFile[25] = {0}; static char tsProcIOFile[25] = {0}; -static float tsPageSizeKB = 0; static void taosGetProcInfos() { - tsPageSize = sysconf(_SC_PAGESIZE); + tsPageSizeKB = sysconf(_SC_PAGESIZE) / 1024; tsOpenMax = sysconf(_SC_OPEN_MAX); tsStreamMax = sysconf(_SC_STREAM_MAX); - tsProcId = (pid_t)syscall(SYS_gettid); - tsPageSizeKB = (float)(sysconf(_SC_PAGESIZE)) / 1024; - snprintf(tsProcMemFile, 25, "/proc/%d/status", tsProcId); - snprintf(tsProcCpuFile, 25, "/proc/%d/stat", tsProcId); - snprintf(tsProcIOFile, 25, "/proc/%d/io", tsProcId); + snprintf(tsProcMemFile, sizeof(tsProcMemFile), "/proc/%d/status", tsProcId); + snprintf(tsProcCpuFile, sizeof(tsProcCpuFile), "/proc/%d/stat", tsProcId); + snprintf(tsProcIOFile, sizeof(tsProcIOFile), "/proc/%d/io", tsProcId); } -static int32_t taosGetTotalMemory() { return (int32_t)((float)sysconf(_SC_PHYS_PAGES) * tsPageSizeKB / 1024); } - -bool taosGetSysMemory(float *memoryUsedMB) { - float memoryAvailMB = (float)sysconf(_SC_AVPHYS_PAGES) * tsPageSizeKB / 1024; - *memoryUsedMB = (float)tsTotalMemoryMB - memoryAvailMB; - return true; +int32_t taosGetTotalMemory(int64_t *totalKB) { + *totalKB = (int64_t)(sysconf(_SC_PHYS_PAGES) * tsPageSizeKB); + return 0; } -bool taosGetProcMemory(float *memoryUsedMB) { +int32_t taosGetSysMemory(int64_t *usedKB) { + *usedKB = sysconf(_SC_AVPHYS_PAGES) * tsPageSizeKB; + return 0; +} + +int32_t taosGetProcMemory(int64_t *usedKB) { // FILE *fp = fopen(tsProcMemFile, "r"); TdFilePtr pFile = taosOpenFile(tsProcMemFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { // printf("open file:%s failed", tsProcMemFile); - return false; + return -1; } ssize_t _bytes = 0; @@ -421,25 +422,23 @@ bool taosGetProcMemory(float *memoryUsedMB) { if (line == NULL) { // printf("read file:%s failed", tsProcMemFile); taosCloseFile(&pFile); - return false; + return -1; } - int64_t memKB = 0; - char tmp[10]; - sscanf(line, "%s %" PRId64, tmp, &memKB); - *memoryUsedMB = (float)((double)memKB / 1024); + char tmp[10]; + sscanf(line, "%s %" PRId64, tmp, usedKB); if (line != NULL) tfree(line); taosCloseFile(&pFile); - return true; + return 0; } -static bool taosGetSysCpuInfo(SysCpuInfo *cpuInfo) { +static int32_t taosGetSysCpuInfo(SysCpuInfo *cpuInfo) { // FILE *fp = fopen(tsSysCpuFile, "r"); TdFilePtr pFile = taosOpenFile(tsSysCpuFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { // printf("open file:%s failed", tsSysCpuFile); - return false; + return -1; } char *line = NULL; @@ -447,7 +446,7 @@ static bool taosGetSysCpuInfo(SysCpuInfo *cpuInfo) { if ((_bytes < 0) || (line == NULL)) { // printf("read file:%s failed", tsSysCpuFile); taosCloseFile(&pFile); - return false; + return -1; } char cpu[10] = {0}; @@ -456,15 +455,15 @@ static bool taosGetSysCpuInfo(SysCpuInfo *cpuInfo) { if (line != NULL) tfree(line); taosCloseFile(&pFile); - return true; + return 0; } -static bool taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) { +static int32_t taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) { // FILE *fp = fopen(tsProcCpuFile, "r"); TdFilePtr pFile = taosOpenFile(tsProcCpuFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { // printf("open file:%s failed", tsProcCpuFile); - return false; + return -1; } char *line = NULL; @@ -472,7 +471,7 @@ static bool taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) { if ((_bytes < 0) || (line == NULL)) { // printf("read file:%s failed", tsProcCpuFile); taosCloseFile(&pFile); - return false; + return -1; } for (int i = 0, blank = 0; line[i] != 0; ++i) { @@ -486,23 +485,26 @@ static bool taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) { if (line != NULL) tfree(line); taosCloseFile(&pFile); - return true; + return 0; } -int32_t taosGetCpuCores() { return (int32_t)sysconf(_SC_NPROCESSORS_ONLN); } +int32_t taosGetCpuCores(float *numOfCores) { + *numOfCores = sysconf(_SC_NPROCESSORS_ONLN); + return 0; +} -bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { +int32_t taosGetCpuUsage(float *cpu_system, float *cpu_engine) { static uint64_t lastSysUsed = 0; static uint64_t lastSysTotal = 0; static uint64_t lastProcTotal = 0; SysCpuInfo sysCpu; ProcCpuInfo procCpu; - if (!taosGetSysCpuInfo(&sysCpu)) { - return false; + if (taosGetSysCpuInfo(&sysCpu) != 0) { + return -1; } - if (!taosGetProcCpuInfo(&procCpu)) { - return false; + if (taosGetProcCpuInfo(&procCpu) != 0) { + return -1; } uint64_t curSysUsed = sysCpu.user + sysCpu.nice + sysCpu.system; @@ -513,21 +515,21 @@ bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { lastSysUsed = curSysUsed > 1 ? curSysUsed : 1; lastSysTotal = curSysTotal > 1 ? curSysTotal : 1; lastProcTotal = curProcTotal > 1 ? curProcTotal : 1; - return false; + return -1; } if (curSysTotal == lastSysTotal) { - return false; + return -1; } - *sysCpuUsage = (float)((double)(curSysUsed - lastSysUsed) / (double)(curSysTotal - lastSysTotal) * 100); - *procCpuUsage = (float)((double)(curProcTotal - lastProcTotal) / (double)(curSysTotal - lastSysTotal) * 100); + *cpu_engine = (float)((double)(curSysUsed - lastSysUsed) / (double)(curSysTotal - lastSysTotal) * 100); + *cpu_system = (float)((double)(curProcTotal - lastProcTotal) / (double)(curSysTotal - lastSysTotal) * 100); lastSysUsed = curSysUsed; lastSysTotal = curSysTotal; lastProcTotal = curProcTotal; - return true; + return 0; } int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) { @@ -700,13 +702,10 @@ bool taosGetProcIO(float *readKB, float *writeKB) { void taosGetSystemInfo() { taosGetProcInfos(); - - tsNumOfCores = taosGetCpuCores(); - tsTotalMemoryMB = taosGetTotalMemory(); + taosGetCpuCores(&tsNumOfCores); + taosGetTotalMemory(&tsTotalMemoryKB); float tmp1, tmp2; - taosGetSysMemory(&tmp1); - taosGetProcMemory(&tmp2); taosGetBandSpeed(&tmp1); taosGetCpuUsage(&tmp1, &tmp2); taosGetProcIO(&tmp1, &tmp2); @@ -923,7 +922,7 @@ int32_t taosGetOsReleaseName(char *releaseName, int32_t maxLen) { return code; } -int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores) { +int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores) { char *line = NULL; size_t size = 0; int32_t done = 0; @@ -941,7 +940,7 @@ int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores) { done |= 1; } else if (((done & 2) == 0) && strncmp(line, "cpu cores", 9) == 0) { const char *v = strchr(line, ':') + 2; - *numOfCores = atoi(v); + *numOfCores = atof(v); done |= 2; } } @@ -952,28 +951,4 @@ int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores) { return code; } -bool taosGetTotalSysMemoryKB(uint64_t *kb) { - char *line = NULL; - size_t size = 0; - bool ret = false; - - TdFilePtr pFile = taosOpenFile("/proc/meminfo", TD_FILE_READ | TD_FILE_STREAM); - if (pFile == NULL) return false; - - while ((size = taosGetLineFile(pFile, &line)) != -1) { - line[size - 1] = '\0'; - if (strncmp(line, "MemTotal", 8) == 0) { - const char *p = strchr(line, ':') + 1; - while (*p == ' ') p++; - ret = true; - *kb = atoll(p); - break; - } - } - - if (line != NULL) free(line); - taosCloseFile(&pFile); - return ret; -} - #endif From 3fbb78a951fd383b6a4882f915393ba867544565 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 3 Mar 2022 18:55:16 +0800 Subject: [PATCH 17/19] io --- include/os/osSysinfo.h | 4 +-- source/dnode/mgmt/impl/src/dndMgmt.c | 7 ++--- source/os/src/osSysinfo.c | 42 ++++++++++++++-------------- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/include/os/osSysinfo.h b/include/os/osSysinfo.h index df5ed28a99..3c68615c64 100644 --- a/include/os/osSysinfo.h +++ b/include/os/osSysinfo.h @@ -43,8 +43,8 @@ int32_t taosGetTotalMemory(int64_t *totalKB); int32_t taosGetProcMemory(int64_t *usedKB); int32_t taosGetSysMemory(int64_t *usedKB); int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize); -bool taosReadProcIO(int64_t *rchars, int64_t *wchars); -bool taosGetProcIO(float *readKB, float *writeKB); +int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars); +int32_t taosGetProcIO(float *readKB, float *writeKB); bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes); bool taosGetBandSpeed(float *bandSpeedKb); diff --git a/source/dnode/mgmt/impl/src/dndMgmt.c b/source/dnode/mgmt/impl/src/dndMgmt.c index 704847894e..6a743fcf48 100644 --- a/source/dnode/mgmt/impl/src/dndMgmt.c +++ b/source/dnode/mgmt/impl/src/dndMgmt.c @@ -491,10 +491,9 @@ static void dndGetMonitorDnodeInfo(SDnode *pDnode, SMonDnodeInfo *pInfo) { pInfo->disk_total = 4.3; pInfo->net_in = 5.1; pInfo->net_out = 5.2; - pInfo->io_read = 6.1; - pInfo->io_write = 6.2; - pInfo->io_read_disk = 7.1; - pInfo->io_write_disk = 7.2; + taosGetProcIO(&pInfo->io_read, &pInfo->io_write); + pInfo->io_read_disk = 0; + pInfo->io_write_disk = 0; pInfo->req_select = 8; pInfo->req_select_rate = 8.1; pInfo->req_insert = 9; diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index bf036354e9..3683943c61 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -124,31 +124,31 @@ bool taosGetBandSpeed(float *bandSpeedKb) { return true; } -bool taosReadProcIO(int64_t *readbyte, int64_t *writebyte) { +int32_t taosReadProcIO(int64_t *readbyte, int64_t *writebyte) { IO_COUNTERS io_counter; if (GetProcessIoCounters(GetCurrentProcess(), &io_counter)) { if (readbyte) *readbyte = io_counter.ReadTransferCount; if (writebyte) *writebyte = io_counter.WriteTransferCount; - return true; + return 0; } - return false; + return -1; } -bool taosGetProcIO(float *readKB, float *writeKB) { +int32_t taosGetProcIO(float *readKB, float *writeKB) { static int64_t lastReadbyte = -1; static int64_t lastWritebyte = -1; int64_t curReadbyte = 0; int64_t curWritebyte = 0; - if (!taosReadProcIO(&curReadbyte, &curWritebyte)) { - return false; + if (taosReadProcIO(&curReadbyte, &curWritebyte) != 0) { + return -1; } if (lastReadbyte == -1 || lastWritebyte == -1) { lastReadbyte = curReadbyte; lastWritebyte = curWritebyte; - return false; + return -1; } *readKB = (float)((double)(curReadbyte - lastReadbyte) / 1024); @@ -159,7 +159,7 @@ bool taosGetProcIO(float *readKB, float *writeKB) { lastReadbyte = curReadbyte; lastWritebyte = curWritebyte; - return true; + return 0; } void taosGetSystemInfo() { @@ -259,16 +259,16 @@ void taosGetSystemInfo() { tsNumOfCores = sysconf(_SC_NPROCESSORS_ONLN); } -bool taosReadProcIO(int64_t *rchars, int64_t *wchars) { +int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars) { if (rchars) *rchars = 0; if (wchars) *wchars = 0; - return true; + return 0; } -bool taosGetProcIO(float *readKB, float *writeKB) { +int32_t taosGetProcIO(float *readKB, float *writeKB) { *readKB = 0; *writeKB = 0; - return true; + return 0; } bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { @@ -631,12 +631,12 @@ bool taosGetBandSpeed(float *bandSpeedKb) { return true; } -bool taosReadProcIO(int64_t *rchars, int64_t *wchars) { +int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars) { // FILE *fp = fopen(tsProcIOFile, "r"); TdFilePtr pFile = taosOpenFile(tsProcIOFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { // printf("open file:%s failed", tsProcIOFile); - return false; + return -1; } ssize_t _bytes = 0; @@ -666,27 +666,27 @@ bool taosReadProcIO(int64_t *rchars, int64_t *wchars) { if (readIndex < 2) { // printf("read file:%s failed", tsProcIOFile); - return false; + return -1; } - return true; + return 0; } -bool taosGetProcIO(float *readKB, float *writeKB) { +int32_t taosGetProcIO(float *readKB, float *writeKB) { static int64_t lastReadbyte = -1; static int64_t lastWritebyte = -1; int64_t curReadbyte = 0; int64_t curWritebyte = 0; - if (!taosReadProcIO(&curReadbyte, &curWritebyte)) { - return false; + if (taosReadProcIO(&curReadbyte, &curWritebyte) != 0) { + return -1; } if (lastReadbyte == -1 || lastWritebyte == -1) { lastReadbyte = curReadbyte; lastWritebyte = curWritebyte; - return false; + return -1; } *readKB = (float)((double)(curReadbyte - lastReadbyte) / 1024); @@ -697,7 +697,7 @@ bool taosGetProcIO(float *readKB, float *writeKB) { lastReadbyte = curReadbyte; lastWritebyte = curWritebyte; - return true; + return 0; } void taosGetSystemInfo() { From 2bf46ddc54a1282620b4ad725f2a94a68c95008c Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 3 Mar 2022 19:12:08 +0800 Subject: [PATCH 18/19] monitor --- include/libs/monitor/monitor.h | 12 ++++----- include/os/osSysinfo.h | 4 +-- source/dnode/mgmt/impl/inc/dndMnode.h | 1 + source/dnode/mgmt/impl/src/dndMgmt.c | 33 +++++++++++------------ source/dnode/mgmt/impl/src/dndMnode.c | 7 +++++ source/os/src/osSysinfo.c | 38 +++++++++++++-------------- 6 files changed, 51 insertions(+), 44 deletions(-) diff --git a/include/libs/monitor/monitor.h b/include/libs/monitor/monitor.h index 487b2bfe88..00cb7c3dbc 100644 --- a/include/libs/monitor/monitor.h +++ b/include/libs/monitor/monitor.h @@ -94,12 +94,12 @@ typedef struct { float disk_engine; // GB float disk_used; // GB float disk_total; // GB - float net_in; // Kb/s - float net_out; // Kb/s - float io_read; // Mb/s - float io_write; // Mb/s - float io_read_disk; // Mb/s - float io_write_disk; // Mb/s + int64_t net_in; + int64_t net_out; + float io_read; + float io_write; + float io_read_disk; + float io_write_disk; int32_t req_select; float req_select_rate; int32_t req_insert; diff --git a/include/os/osSysinfo.h b/include/os/osSysinfo.h index 3c68615c64..e14dba8269 100644 --- a/include/os/osSysinfo.h +++ b/include/os/osSysinfo.h @@ -45,8 +45,8 @@ int32_t taosGetSysMemory(int64_t *usedKB); int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize); int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars); int32_t taosGetProcIO(float *readKB, float *writeKB); -bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes); -bool taosGetBandSpeed(float *bandSpeedKb); +int32_t taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes); +int32_t taosGetBandSpeed(float *bandSpeedKb); int32_t taosSystem(const char *cmd); void taosKillSystem(); diff --git a/source/dnode/mgmt/impl/inc/dndMnode.h b/source/dnode/mgmt/impl/inc/dndMnode.h index 0f03bb3832..0aee3b4b43 100644 --- a/source/dnode/mgmt/impl/inc/dndMnode.h +++ b/source/dnode/mgmt/impl/inc/dndMnode.h @@ -34,6 +34,7 @@ int32_t dndProcessDropMnodeReq(SDnode *pDnode, SRpcMsg *pRpcMsg); int32_t dndGetMnodeMonitorInfo(SDnode *pDnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo, SMonGrantInfo *pGrantInfo); +int8_t dndIsMnode(SDnode *pDnode); #ifdef __cplusplus } diff --git a/source/dnode/mgmt/impl/src/dndMgmt.c b/source/dnode/mgmt/impl/src/dndMgmt.c index 6a743fcf48..60cfdc299c 100644 --- a/source/dnode/mgmt/impl/src/dndMgmt.c +++ b/source/dnode/mgmt/impl/src/dndMgmt.c @@ -486,26 +486,25 @@ static void dndGetMonitorDnodeInfo(SDnode *pDnode, SMonDnodeInfo *pInfo) { taosGetProcMemory(&pInfo->mem_engine); taosGetSysMemory(&pInfo->mem_system); pInfo->mem_total = tsTotalMemoryKB; - pInfo->disk_engine = 4.1; - pInfo->disk_used = 4.2; - pInfo->disk_total = 4.3; - pInfo->net_in = 5.1; - pInfo->net_out = 5.2; + pInfo->disk_engine = 0; + pInfo->disk_used = tsDataSpace.size.used / (1024 * 1024 * 1024.0); + pInfo->disk_total = tsDataSpace.size.avail / (1024 * 1024 * 1024.0); + taosGetCardInfo(NULL, &pInfo->net_in, &pInfo->net_out); taosGetProcIO(&pInfo->io_read, &pInfo->io_write); pInfo->io_read_disk = 0; pInfo->io_write_disk = 0; - pInfo->req_select = 8; - pInfo->req_select_rate = 8.1; - pInfo->req_insert = 9; - pInfo->req_insert_success = 10; - pInfo->req_insert_rate = 10.1; - pInfo->req_insert_batch = 11; - pInfo->req_insert_batch_success = 12; - pInfo->req_insert_batch_rate = 12.3; - pInfo->errors = 4; - pInfo->vnodes_num = 5; - pInfo->masters = 6; - pInfo->has_mnode = 1; + pInfo->req_select = 0; + pInfo->req_select_rate = 0; + pInfo->req_insert = 0; + pInfo->req_insert_success = 0; + pInfo->req_insert_rate = 0; + pInfo->req_insert_batch = 0; + pInfo->req_insert_batch_success = 0; + pInfo->req_insert_batch_rate = 0; + pInfo->errors = 0; + pInfo->vnodes_num = 0; + pInfo->masters = 0; + pInfo->has_mnode = dndIsMnode(pDnode); } static void dndSendMonitorReport(SDnode *pDnode) { diff --git a/source/dnode/mgmt/impl/src/dndMnode.c b/source/dnode/mgmt/impl/src/dndMnode.c index 6cb117867f..47e74b5c57 100644 --- a/source/dnode/mgmt/impl/src/dndMnode.c +++ b/source/dnode/mgmt/impl/src/dndMnode.c @@ -639,4 +639,11 @@ int32_t dndGetMnodeMonitorInfo(SDnode *pDnode, SMonClusterInfo *pClusterInfo, SM int32_t code = mndGetMonitorInfo(pMnode, pClusterInfo, pVgroupInfo, pGrantInfo); dndReleaseMnode(pDnode, pMnode); return code; +} + +int8_t dndIsMnode(SDnode *pDnode) { + SMnode *pMnode = dndAcquireMnode(pDnode); + if (pMnode == NULL) return 0; + dndReleaseMnode(pDnode, pMnode); + return 1; } \ No newline at end of file diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index 3683943c61..c674aeaca2 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -112,16 +112,16 @@ int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) { } } -bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { +int32_t taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { if (bytes) *bytes = 0; if (rbytes) *rbytes = 0; if (tbytes) *tbytes = 0; - return true; + return 0; } -bool taosGetBandSpeed(float *bandSpeedKb) { +int32_t taosGetBandSpeed(float *bandSpeedKb) { *bandSpeedKb = 0; - return true; + return 0; } int32_t taosReadProcIO(int64_t *readbyte, int64_t *writebyte) { @@ -271,16 +271,16 @@ int32_t taosGetProcIO(float *readKB, float *writeKB) { return 0; } -bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { +int32_t taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { if (bytes) *bytes = 0; if (rbytes) *rbytes = 0; if (tbytes) *tbytes = 0; - return true; + return 0; } -bool taosGetBandSpeed(float *bandSpeedKb) { +int32_t taosGetBandSpeed(float *bandSpeedKb) { *bandSpeedKb = 0; - return true; + return 0; } int32_t taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { @@ -544,13 +544,13 @@ int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) { } } -bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { - *bytes = 0; +int32_t taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { + if (bytes) *bytes = 0; // FILE *fp = fopen(tsSysNetFile, "r"); TdFilePtr pFile = taosOpenFile(tsSysNetFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { // printf("open file:%s failed", tsSysNetFile); - return false; + return -1; } ssize_t _bytes = 0; @@ -586,37 +586,37 @@ bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { nouse0, &o_rbytes, &rpackts, &nouse1, &nouse2, &nouse3, &nouse4, &nouse5, &nouse6, &o_tbytes, &tpackets); if (rbytes) *rbytes = o_rbytes; if (tbytes) *tbytes = o_tbytes; - *bytes += (o_rbytes + o_tbytes); + if (bytes) *bytes += (o_rbytes + o_tbytes); } if (line != NULL) tfree(line); taosCloseFile(&pFile); - return true; + return 0; } -bool taosGetBandSpeed(float *bandSpeedKb) { +int32_t taosGetBandSpeed(float *bandSpeedKb) { static int64_t lastBytes = 0; static time_t lastTime = 0; int64_t curBytes = 0; time_t curTime = time(NULL); - if (!taosGetCardInfo(&curBytes, NULL, NULL)) { - return false; + if (taosGetCardInfo(&curBytes, NULL, NULL) != 0) { + return -1; } if (lastTime == 0 || lastBytes == 0) { lastTime = curTime; lastBytes = curBytes; *bandSpeedKb = 0; - return true; + return 0; } if (lastTime >= curTime || lastBytes > curBytes) { lastTime = curTime; lastBytes = curBytes; *bandSpeedKb = 0; - return true; + return 0; } double totalBytes = (double)(curBytes - lastBytes) / 1024 * 8; // Kb @@ -628,7 +628,7 @@ bool taosGetBandSpeed(float *bandSpeedKb) { lastTime = curTime; lastBytes = curBytes; - return true; + return 0; } int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars) { From 35b3edaea562f5c01d7f63d17634bd5479d534d0 Mon Sep 17 00:00:00 2001 From: afwerar <1296468573@qq.com> Date: Fri, 4 Mar 2022 00:04:57 +0800 Subject: [PATCH 19/19] [TD-13756]: file system stat access func. --- include/os/osDir.h | 2 +- include/os/osFile.h | 24 +++-- source/common/test/commonTests.cpp | 4 +- source/common/test/tmsgTest.cpp | 2 +- source/dnode/vnode/src/tq/tqMetaStore.c | 2 +- source/dnode/vnode/src/tsdb/tsdbCommit.c | 4 +- source/dnode/vnode/src/tsdb/tsdbFS.c | 2 +- source/dnode/vnode/src/tsdb/tsdbFile.c | 15 ++- source/libs/CMakeLists.txt | 1 - source/libs/catalog/test/catalogTests.cpp | 6 +- source/libs/executor/src/executorimpl.c | 8 +- source/libs/executor/test/executorTests.cpp | 6 +- source/libs/executor/test/sortTests.cpp | 2 +- .../index/src/index_fst_counting_writer.c | 18 ++-- source/libs/parser/test/mockCatalog.cpp | 6 +- source/libs/parser/test/parserTests.cpp | 4 +- source/libs/parser/test/plannerTest.cpp | 2 +- source/libs/qworker/test/qworkerTests.cpp | 4 +- .../libs/scalar/test/filter/filterTests.cpp | 4 +- .../libs/scalar/test/scalar/scalarTests.cpp | 4 +- source/libs/scheduler/test/schedulerTests.cpp | 24 ++--- .../sync/test/syncIOSendMsgClientTest.cpp | 2 +- .../sync/test/syncIOSendMsgServerTest.cpp | 2 +- source/libs/sync/test/syncIOSendMsgTest.cpp | 2 +- source/libs/sync/test/syncIOTickPingTest.cpp | 2 +- source/libs/sync/test/syncIOTickQTest.cpp | 2 +- source/libs/sync/test/syncRaftStoreTest.cpp | 4 +- source/libs/sync/test/syncTest.cpp | 2 +- source/libs/tdb/src/db/tdb.c | 2 +- source/libs/tdb/src/db/tdbUtil.c | 43 ++++---- source/libs/tdb/src/inc/tdbUtil.h | 8 +- source/libs/tdb/test/tdbTest.cpp | 2 +- source/libs/tfs/src/tfs.c | 11 +-- source/libs/tfs/test/tfsTest.cpp | 98 +++++++++---------- source/libs/wal/src/walMeta.c | 22 ++--- source/os/src/osDir.c | 2 +- source/os/src/osFile.c | 39 ++++++++ source/util/test/cacheTest.cpp | 2 +- source/util/test/encodeTest.cpp | 2 +- source/util/test/freelistTest.cpp | 2 +- source/util/test/hashTest.cpp | 4 +- tools/shell/src/backup/shellImport.c | 8 +- 42 files changed, 221 insertions(+), 184 deletions(-) diff --git a/include/os/osDir.h b/include/os/osDir.h index eda83ea0ea..223c603352 100644 --- a/include/os/osDir.h +++ b/include/os/osDir.h @@ -21,7 +21,7 @@ extern "C" { #endif void taosRemoveDir(const char *dirname); -int32_t taosDirExist(char *dirname); +bool taosDirExist(char *dirname); int32_t taosMkDir(const char *dirname); void taosRemoveOldFiles(const char *dirname, int32_t keepDays); int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen); diff --git a/include/os/osFile.h b/include/os/osFile.h index cbd5d693c5..7e3a5277c8 100644 --- a/include/os/osFile.h +++ b/include/os/osFile.h @@ -25,8 +25,12 @@ extern "C" { #ifndef ALLOW_FORBID_FUNC #define open OPEN_FUNC_TAOS_FORBID #define fopen FOPEN_FUNC_TAOS_FORBID - // #define close CLOSE_FUNC_TAOS_FORBID - // #define fclose FCLOSE_FUNC_TAOS_FORBID + #define access ACCESS_FUNC_TAOS_FORBID + #define stat STAT_FUNC_TAOS_FORBID + #define lstat LSTAT_FUNC_TAOS_FORBID + #define fstat FSTAT_FUNC_TAOS_FORBID + #define close CLOSE_FUNC_TAOS_FORBID + #define fclose FCLOSE_FUNC_TAOS_FORBID #endif #ifndef PATH_MAX @@ -44,6 +48,12 @@ typedef struct TdFile *TdFilePtr; #define TD_FILE_AUTO_DEL 0x0040 #define TD_FILE_EXCL 0x0080 #define TD_FILE_STREAM 0x0100 // Only support taosFprintfFile, taosGetLineFile, taosGetLineFile, taosEOFFile +TdFilePtr taosOpenFile(const char *path,int32_t tdFileOptions); + +#define TD_FILE_ACCESS_EXIST_OK 0x1 +#define TD_FILE_ACCESS_READ_OK 0x2 +#define TD_FILE_ACCESS_WRITE_OK 0x4 +bool taosCheckAccessFile(const char *pathname, int mode); int32_t taosLockFile(TdFilePtr pFile); int32_t taosUnLockFile(TdFilePtr pFile); @@ -51,9 +61,9 @@ int32_t taosUnLockFile(TdFilePtr pFile); int32_t taosUmaskFile(int32_t maskVal); int32_t taosStatFile(const char *path, int64_t *size, int32_t *mtime); +int32_t taosDevInoFile(const char *path, int64_t *stDev, int64_t *stIno); int32_t taosFStatFile(TdFilePtr pFile, int64_t *size, int32_t *mtime); - -TdFilePtr taosOpenFile(const char *path,int32_t tdFileOptions); +bool taosCheckExistFile(const char *pathname); int64_t taosLSeekFile(TdFilePtr pFile, int64_t offset, int32_t whence); int32_t taosFtruncateFile(TdFilePtr pFile, int64_t length); @@ -62,7 +72,7 @@ int32_t taosFsyncFile(TdFilePtr pFile); int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count); int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset); int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count); -void taosFprintfFile(TdFilePtr pFile, const char *format, ...); +void taosFprintfFile(TdFilePtr pFile, const char *format, ...); int64_t taosGetLineFile(TdFilePtr pFile, char ** __restrict__ ptrBuf); int32_t taosEOFFile(TdFilePtr pFile); @@ -71,7 +81,7 @@ int64_t taosCloseFile(TdFilePtr *ppFile); int32_t taosRenameFile(const char *oldName, const char *newName); int64_t taosCopyFile(const char *from, const char *to); -void taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, char *dstPath); +void taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, char *dstPath); int64_t taosSendFile(SocketFd fdDst, TdFilePtr pFileSrc, int64_t *offset, int64_t size); int64_t taosFSendFile(TdFilePtr pFileOut, TdFilePtr pFileIn, int64_t *offset, int64_t size); @@ -79,7 +89,7 @@ int64_t taosFSendFile(TdFilePtr pFileOut, TdFilePtr pFileIn, int64_t *offset, in void *taosMmapReadOnlyFile(TdFilePtr pFile, int64_t length); bool taosValidFile(TdFilePtr pFile); -int taosGetErrorFile(TdFilePtr pFile); +int32_t taosGetErrorFile(TdFilePtr pFile); #ifdef __cplusplus } diff --git a/source/common/test/commonTests.cpp b/source/common/test/commonTests.cpp index 4821d60875..563efe4d54 100644 --- a/source/common/test/commonTests.cpp +++ b/source/common/test/commonTests.cpp @@ -1,6 +1,4 @@ -#include "tcommon.h" #include -#include #include #pragma GCC diagnostic push @@ -10,6 +8,8 @@ #pragma GCC diagnostic ignored "-Wsign-compare" #include "os.h" +#include "tep.h" +#include "tcommon.h" #include "taos.h" #include "tvariant.h" #include "tdef.h" diff --git a/source/common/test/tmsgTest.cpp b/source/common/test/tmsgTest.cpp index ca33d24a8c..e8c3284ea5 100644 --- a/source/common/test/tmsgTest.cpp +++ b/source/common/test/tmsgTest.cpp @@ -1,6 +1,6 @@ #include -#include "gtest/gtest.h" +#include #include "tmsg.h" diff --git a/source/dnode/vnode/src/tq/tqMetaStore.c b/source/dnode/vnode/src/tq/tqMetaStore.c index 6d5085ee74..ce00c98ff9 100644 --- a/source/dnode/vnode/src/tq/tqMetaStore.c +++ b/source/dnode/vnode/src/tq/tqMetaStore.c @@ -90,7 +90,7 @@ STqMetaStore* tqStoreOpen(STQ* pTq, const char* path, FTqSerialize serializer, F char name[pathLen + 10]; strcpy(name, path); - if (taosDirExist(name) != 0 && taosMkDir(name) != 0) { + if (!taosDirExist(name) && taosMkDir(name) != 0) { terrno = TSDB_CODE_TQ_FAILED_TO_CREATE_DIR; tqError("failed to create dir:%s since %s ", name, terrstr()); } diff --git a/source/dnode/vnode/src/tsdb/tsdbCommit.c b/source/dnode/vnode/src/tsdb/tsdbCommit.c index 2e7116969e..7a95820115 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCommit.c +++ b/source/dnode/vnode/src/tsdb/tsdbCommit.c @@ -580,7 +580,7 @@ static int tsdbSetAndOpenCommitFile(SCommitH *pCommith, SDFileSet *pSet, int fid SDFile *pRSmadF = TSDB_READ_SMAD_FILE(&(pCommith->readh)); SDFile *pWSmadF = TSDB_COMMIT_SMAD_FILE(pCommith); - if (access(TSDB_FILE_FULL_NAME(pRSmadF), F_OK) != 0) { + if (!taosCheckExistFile(TSDB_FILE_FULL_NAME(pRSmadF))) { tsdbDebug("vgId:%d create data file %s as not exist", REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pRSmadF)); tsdbInitDFile(pRepo, pWSmadF, did, fid, FS_TXN_VERSION(REPO_FS(pRepo)), TSDB_FILE_SMAD); @@ -614,7 +614,7 @@ static int tsdbSetAndOpenCommitFile(SCommitH *pCommith, SDFileSet *pSet, int fid SDFile *pRSmalF = TSDB_READ_SMAL_FILE(&(pCommith->readh)); SDFile *pWSmalF = TSDB_COMMIT_SMAL_FILE(pCommith); - if ((pCommith->isLFileSame) && access(TSDB_FILE_FULL_NAME(pRSmalF), F_OK) == 0) { + if ((pCommith->isLFileSame) && taosCheckExistFile(TSDB_FILE_FULL_NAME(pRSmalF))) { tsdbInitDFileEx(pWSmalF, pRSmalF); if (tsdbOpenDFile(pWSmalF, O_RDWR) < 0) { tsdbError("vgId:%d failed to open file %s to commit since %s", REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pWSmalF), diff --git a/source/dnode/vnode/src/tsdb/tsdbFS.c b/source/dnode/vnode/src/tsdb/tsdbFS.c index dcc9700d83..411a166caa 100644 --- a/source/dnode/vnode/src/tsdb/tsdbFS.c +++ b/source/dnode/vnode/src/tsdb/tsdbFS.c @@ -314,7 +314,7 @@ int tsdbOpenFS(STsdb *pRepo) { tsdbGetTxnFname(pRepo, TSDB_TXN_CURR_FILE, current); tsdbGetRtnSnap(pRepo, &pRepo->rtn); - if (access(current, F_OK) == 0) { + if (taosCheckExistFile(current)) { if (tsdbOpenFSFromCurrent(pRepo) < 0) { tsdbError("vgId:%d failed to open FS since %s", REPO_ID(pRepo), tstrerror(terrno)); return -1; diff --git a/source/dnode/vnode/src/tsdb/tsdbFile.c b/source/dnode/vnode/src/tsdb/tsdbFile.c index 4d70c429ff..74fb8c1c1f 100644 --- a/source/dnode/vnode/src/tsdb/tsdbFile.c +++ b/source/dnode/vnode/src/tsdb/tsdbFile.c @@ -443,25 +443,24 @@ int tsdbLoadDFileHeader(SDFile *pDFile, SDFInfo *pInfo) { } static int tsdbScanAndTryFixDFile(STsdb *pRepo, SDFile *pDFile) { - struct stat dfstat; SDFile df; tsdbInitDFileEx(&df, pDFile); - if (access(TSDB_FILE_FULL_NAME(pDFile), F_OK) != 0) { + if (!taosCheckExistFile(TSDB_FILE_FULL_NAME(pDFile))) { tsdbError("vgId:%d data file %s not exit, report to upper layer to fix it", REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pDFile)); // pRepo->state |= TSDB_STATE_BAD_DATA; TSDB_FILE_SET_STATE(pDFile, TSDB_FILE_STATE_BAD); return 0; } - - if (stat(TSDB_FILE_FULL_NAME(&df), &dfstat) < 0) { + int64_t file_size = 0; + if (taosStatFile(TSDB_FILE_FULL_NAME(&df), &file_size, NULL) < 0) { terrno = TAOS_SYSTEM_ERROR(errno); return -1; } - if (pDFile->info.size < dfstat.st_size) { + if (pDFile->info.size < file_size) { // if (tsdbOpenDFile(&df, O_WRONLY) < 0) { if (tsdbOpenDFile(&df, TD_FILE_WRITE) < 0) { return -1; @@ -480,10 +479,10 @@ static int tsdbScanAndTryFixDFile(STsdb *pRepo, SDFile *pDFile) { tsdbCloseDFile(&df); tsdbInfo("vgId:%d file %s is truncated from %" PRId64 " to %" PRId64, REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pDFile), - dfstat.st_size, pDFile->info.size); - } else if (pDFile->info.size > dfstat.st_size) { + file_size, pDFile->info.size); + } else if (pDFile->info.size > file_size) { tsdbError("vgId:%d data file %s has wrong size %" PRId64 " expected %" PRId64 ", report to upper layer to fix it", - REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pDFile), dfstat.st_size, pDFile->info.size); + REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pDFile), file_size, pDFile->info.size); // pRepo->state |= TSDB_STATE_BAD_DATA; TSDB_FILE_SET_STATE(pDFile, TSDB_FILE_STATE_BAD); terrno = TSDB_CODE_TDB_FILE_CORRUPTED; diff --git a/source/libs/CMakeLists.txt b/source/libs/CMakeLists.txt index 78625d1eed..d766f3cbf1 100644 --- a/source/libs/CMakeLists.txt +++ b/source/libs/CMakeLists.txt @@ -1,4 +1,3 @@ -add_definitions("-D ALLOW_FORBID_FUNC") add_subdirectory(transport) add_subdirectory(sync) add_subdirectory(tdb) diff --git a/source/libs/catalog/test/catalogTests.cpp b/source/libs/catalog/test/catalogTests.cpp index b417a645be..b05fc7812a 100644 --- a/source/libs/catalog/test/catalogTests.cpp +++ b/source/libs/catalog/test/catalogTests.cpp @@ -14,9 +14,7 @@ */ #include -#include #include -#include "os.h" #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wwrite-strings" @@ -24,8 +22,10 @@ #pragma GCC diagnostic ignored "-Wunused-variable" #pragma GCC diagnostic ignored "-Wsign-compare" #pragma GCC diagnostic ignored "-Wformat" +#include -#include "addr_any.h" +#include "os.h" +#include "tglobal.h" #include "catalog.h" #include "stub.h" #include "taos.h" diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index c400ded1c1..d449d7fac4 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -2215,10 +2215,10 @@ static void destroyTsComp(STaskRuntimeEnv *pRuntimeEnv, STaskAttr *pQueryAttr) { if (pQueryAttr->tsCompQuery && pRuntimeEnv->outputBuf && pRuntimeEnv->outputBuf->pDataBlock && taosArrayGetSize(pRuntimeEnv->outputBuf->pDataBlock) > 0) { SColumnInfoData* pColInfoData = taosArrayGet(pRuntimeEnv->outputBuf->pDataBlock, 0); if (pColInfoData) { - FILE *f = *(FILE **)pColInfoData->pData; // TODO refactor - if (f) { - fclose(f); - *(FILE **)pColInfoData->pData = NULL; + TdFilePtr pFile = *(TdFilePtr *)pColInfoData->pData; // TODO refactor + if (pFile != NULL) { + taosCloseFile(&pFile); + *(TdFilePtr *)pColInfoData->pData = NULL; } } } diff --git a/source/libs/executor/test/executorTests.cpp b/source/libs/executor/test/executorTests.cpp index 262232ceb1..7d3665a8cc 100644 --- a/source/libs/executor/test/executorTests.cpp +++ b/source/libs/executor/test/executorTests.cpp @@ -13,10 +13,7 @@ * along with this program. If not, see . */ -#include -#include #include -#include #include #pragma GCC diagnostic push @@ -26,6 +23,9 @@ #pragma GCC diagnostic ignored "-Wsign-compare" #include "os.h" +#include "tglobal.h" +#include "executorimpl.h" +#include "function.h" #include "taos.h" #include "tdef.h" #include "tvariant.h" diff --git a/source/libs/executor/test/sortTests.cpp b/source/libs/executor/test/sortTests.cpp index fc366e4cc8..612b0705ea 100644 --- a/source/libs/executor/test/sortTests.cpp +++ b/source/libs/executor/test/sortTests.cpp @@ -13,7 +13,6 @@ * along with this program. If not, see . */ -#include #include #include #include @@ -26,6 +25,7 @@ #pragma GCC diagnostic ignored "-Wsign-compare" #include "os.h" +#include "executorimpl.h" #include "executor.h" #include "stub.h" #include "taos.h" diff --git a/source/libs/index/src/index_fst_counting_writer.c b/source/libs/index/src/index_fst_counting_writer.c index 16d9893778..8cb2ff9246 100644 --- a/source/libs/index/src/index_fst_counting_writer.c +++ b/source/libs/index/src/index_fst_counting_writer.c @@ -63,9 +63,9 @@ static int writeCtxDoReadFrom(WriterCtx* ctx, uint8_t* buf, int len, int32_t off } static int writeCtxGetSize(WriterCtx* ctx) { if (ctx->type == TFile) { - struct stat fstat; - stat(ctx->file.buf, &fstat); - return fstat.st_size; + int64_t file_size = 0; + taosStatFile(ctx->file.buf, &file_size, NULL); + return (int)file_size; } return 0; } @@ -99,9 +99,9 @@ WriterCtx* writerCtxCreate(WriterType type, const char* path, bool readOnly, int // ctx->file.pFile = open(path, O_RDONLY, S_IRWXU | S_IRWXG | S_IRWXO); ctx->file.pFile = taosOpenFile(path, TD_FILE_READ); - struct stat fstat; - stat(path, &fstat); - ctx->file.size = fstat.st_size; + int64_t file_size = 0; + taosFStatFile(ctx->file.pFile, &file_size, NULL); + ctx->file.size = (int)file_size; #ifdef USE_MMAP ctx->file.ptr = (char*)tfMmapReadOnly(ctx->file.pFile, ctx->file.size); #endif @@ -142,8 +142,10 @@ void writerCtxDestroy(WriterCtx* ctx, bool remove) { #endif } if (ctx->file.readOnly == false) { - struct stat fstat; - stat(ctx->file.buf, &fstat); + int64_t file_size = 0; + taosStatFile(ctx->file.buf, &file_size, NULL); + // struct stat fstat; + // stat(ctx->file.buf, &fstat); // indexError("write file size: %d", (int)(fstat.st_size)); } if (remove) { unlink(ctx->file.buf); } diff --git a/source/libs/parser/test/mockCatalog.cpp b/source/libs/parser/test/mockCatalog.cpp index 457d586ea4..e626fc68ae 100644 --- a/source/libs/parser/test/mockCatalog.cpp +++ b/source/libs/parser/test/mockCatalog.cpp @@ -13,19 +13,17 @@ * along with this program. If not, see . */ -#include "mockCatalog.h" - #include - #include "stub.h" #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wformat" -#include "addr_any.h" +#include #pragma GCC diagnostic pop +#include "mockCatalog.h" namespace { void generateTestT1(MockCatalogService* mcs) { diff --git a/source/libs/parser/test/parserTests.cpp b/source/libs/parser/test/parserTests.cpp index b971760132..b80efe09ed 100644 --- a/source/libs/parser/test/parserTests.cpp +++ b/source/libs/parser/test/parserTests.cpp @@ -13,10 +13,8 @@ * along with this program. If not, see . */ -#include #include #include -#include "tglobal.h" #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wwrite-strings" @@ -25,6 +23,8 @@ #pragma GCC diagnostic ignored "-Wsign-compare" #include "os.h" +#include "function.h" +#include "tglobal.h" #include "astGenerator.h" #include "parserInt.h" #include "taos.h" diff --git a/source/libs/parser/test/plannerTest.cpp b/source/libs/parser/test/plannerTest.cpp index 43daff9fec..095e2f4f97 100644 --- a/source/libs/parser/test/plannerTest.cpp +++ b/source/libs/parser/test/plannerTest.cpp @@ -13,7 +13,6 @@ * along with this program. If not, see . */ -#include #include #include #include @@ -25,6 +24,7 @@ #pragma GCC diagnostic ignored "-Wsign-compare" #include "os.h" +#include "function.h" #include "astGenerator.h" #include "parserInt.h" #include "taos.h" diff --git a/source/libs/qworker/test/qworkerTests.cpp b/source/libs/qworker/test/qworkerTests.cpp index 8ad5a76388..94d4260696 100644 --- a/source/libs/qworker/test/qworkerTests.cpp +++ b/source/libs/qworker/test/qworkerTests.cpp @@ -14,7 +14,6 @@ */ #include -#include #include #pragma GCC diagnostic push @@ -26,9 +25,11 @@ #pragma GCC diagnostic ignored "-Wformat" #pragma GCC diagnostic ignored "-Wint-to-pointer-cast" #pragma GCC diagnostic ignored "-Wpointer-arith" +#include #include "os.h" +#include "tglobal.h" #include "taos.h" #include "tdef.h" #include "tvariant.h" @@ -37,7 +38,6 @@ #include "planner.h" #include "qworker.h" #include "stub.h" -#include "addr_any.h" #include "executor.h" #include "dataSinkMgt.h" diff --git a/source/libs/scalar/test/filter/filterTests.cpp b/source/libs/scalar/test/filter/filterTests.cpp index fafc1ea42e..13829618a2 100644 --- a/source/libs/scalar/test/filter/filterTests.cpp +++ b/source/libs/scalar/test/filter/filterTests.cpp @@ -14,7 +14,6 @@ */ #include -#include #include #pragma GCC diagnostic push @@ -26,15 +25,16 @@ #pragma GCC diagnostic ignored "-Wformat" #pragma GCC diagnostic ignored "-Wint-to-pointer-cast" #pragma GCC diagnostic ignored "-Wpointer-arith" +#include #include "os.h" +#include "tglobal.h" #include "taos.h" #include "tdef.h" #include "tvariant.h" #include "tep.h" #include "stub.h" -#include "addr_any.h" #include "scalar.h" #include "nodes.h" #include "tlog.h" diff --git a/source/libs/scalar/test/scalar/scalarTests.cpp b/source/libs/scalar/test/scalar/scalarTests.cpp index b9aef99088..8eef1836a5 100644 --- a/source/libs/scalar/test/scalar/scalarTests.cpp +++ b/source/libs/scalar/test/scalar/scalarTests.cpp @@ -14,7 +14,6 @@ */ #include -#include #include #pragma GCC diagnostic push @@ -26,15 +25,16 @@ #pragma GCC diagnostic ignored "-Wformat" #pragma GCC diagnostic ignored "-Wint-to-pointer-cast" #pragma GCC diagnostic ignored "-Wpointer-arith" +#include #include "os.h" +#include "tglobal.h" #include "taos.h" #include "tdef.h" #include "tvariant.h" #include "tep.h" #include "stub.h" -#include "addr_any.h" #include "scalar.h" #include "nodes.h" #include "tlog.h" diff --git a/source/libs/scheduler/test/schedulerTests.cpp b/source/libs/scheduler/test/schedulerTests.cpp index 11ed3335e6..1a34c20ba0 100644 --- a/source/libs/scheduler/test/schedulerTests.cpp +++ b/source/libs/scheduler/test/schedulerTests.cpp @@ -14,19 +14,8 @@ */ #include -#include #include -#include "os.h" - -#include "taos.h" -#include "tdef.h" -#include "tvariant.h" -#include "catalog.h" -#include "scheduler.h" -#include "tep.h" -#include "trpc.h" - #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wwrite-strings" #pragma GCC diagnostic ignored "-Wunused-function" @@ -34,10 +23,21 @@ #pragma GCC diagnostic ignored "-Wsign-compare" #pragma GCC diagnostic ignored "-Wreturn-type" #pragma GCC diagnostic ignored "-Wformat" +#include + +#include "os.h" + +#include "tglobal.h" +#include "taos.h" +#include "tdef.h" +#include "tvariant.h" +#include "catalog.h" +#include "scheduler.h" +#include "tep.h" +#include "trpc.h" #include "schedulerInt.h" #include "stub.h" -#include "addr_any.h" #include "tref.h" namespace { diff --git a/source/libs/sync/test/syncIOSendMsgClientTest.cpp b/source/libs/sync/test/syncIOSendMsgClientTest.cpp index 9e8c7c8b65..0d06c4f811 100644 --- a/source/libs/sync/test/syncIOSendMsgClientTest.cpp +++ b/source/libs/sync/test/syncIOSendMsgClientTest.cpp @@ -1,5 +1,5 @@ #include -#include "gtest/gtest.h" +#include #include "syncIO.h" #include "syncInt.h" #include "syncRaftStore.h" diff --git a/source/libs/sync/test/syncIOSendMsgServerTest.cpp b/source/libs/sync/test/syncIOSendMsgServerTest.cpp index 8af9344ed6..1582e097d3 100644 --- a/source/libs/sync/test/syncIOSendMsgServerTest.cpp +++ b/source/libs/sync/test/syncIOSendMsgServerTest.cpp @@ -1,5 +1,5 @@ #include -#include "gtest/gtest.h" +#include #include "syncIO.h" #include "syncInt.h" #include "syncRaftStore.h" diff --git a/source/libs/sync/test/syncIOSendMsgTest.cpp b/source/libs/sync/test/syncIOSendMsgTest.cpp index a297981ee5..9b2cfcf1d8 100644 --- a/source/libs/sync/test/syncIOSendMsgTest.cpp +++ b/source/libs/sync/test/syncIOSendMsgTest.cpp @@ -1,5 +1,5 @@ #include -#include "gtest/gtest.h" +#include #include "syncIO.h" #include "syncInt.h" #include "syncRaftStore.h" diff --git a/source/libs/sync/test/syncIOTickPingTest.cpp b/source/libs/sync/test/syncIOTickPingTest.cpp index 1559b57585..42b9a73432 100644 --- a/source/libs/sync/test/syncIOTickPingTest.cpp +++ b/source/libs/sync/test/syncIOTickPingTest.cpp @@ -1,5 +1,5 @@ #include -#include "gtest/gtest.h" +#include #include "syncIO.h" #include "syncInt.h" #include "syncRaftStore.h" diff --git a/source/libs/sync/test/syncIOTickQTest.cpp b/source/libs/sync/test/syncIOTickQTest.cpp index 90304079e3..3d31c596bf 100644 --- a/source/libs/sync/test/syncIOTickQTest.cpp +++ b/source/libs/sync/test/syncIOTickQTest.cpp @@ -1,5 +1,5 @@ #include -#include "gtest/gtest.h" +#include #include "syncIO.h" #include "syncInt.h" #include "syncRaftStore.h" diff --git a/source/libs/sync/test/syncRaftStoreTest.cpp b/source/libs/sync/test/syncRaftStoreTest.cpp index e533b89a92..9cfbe5a4ea 100644 --- a/source/libs/sync/test/syncRaftStoreTest.cpp +++ b/source/libs/sync/test/syncRaftStoreTest.cpp @@ -1,6 +1,6 @@ -#include "syncRaftStore.h" #include -#include "gtest/gtest.h" +#include +#include "syncRaftStore.h" #include "syncIO.h" #include "syncInt.h" diff --git a/source/libs/sync/test/syncTest.cpp b/source/libs/sync/test/syncTest.cpp index 0b397ef921..c1c5658aba 100644 --- a/source/libs/sync/test/syncTest.cpp +++ b/source/libs/sync/test/syncTest.cpp @@ -1,5 +1,5 @@ #include -#include "gtest/gtest.h" +#include #include "syncIO.h" #include "syncInt.h" #include "syncRaftStore.h" diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index cc3b7fa6b9..65d4cf80cc 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -90,7 +90,7 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) { // get page file from the env, if not opened yet, open it pPgFile = NULL; snprintf(dbfname, 128, "%s/%s", tdbEnvGetRootDir(pEnv), fname); - fileExist = (tdbCheckFileAccess(fname, TDB_F_OK) == 0); + fileExist = taosCheckExistFile(fname); if (fileExist) { tdbGnrtFileID(dbfname, fileid, false); pPgFile = tdbEnvGetPageFile(pEnv, fileid); diff --git a/source/libs/tdb/src/db/tdbUtil.c b/source/libs/tdb/src/db/tdbUtil.c index fa9a3297da..fe0f3befd6 100644 --- a/source/libs/tdb/src/db/tdbUtil.c +++ b/source/libs/tdb/src/db/tdbUtil.c @@ -16,16 +16,16 @@ #include "tdbInt.h" int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique) { - struct stat statbuf; + int64_t stDev = 0, stIno = 0; - if (stat(fname, &statbuf) < 0) { + if (taosDevInoFile(fname, &stDev, &stIno) < 0) { return -1; } memset(fileid, 0, TDB_FILE_ID_LEN); - ((uint64_t *)fileid)[0] = (uint64_t)statbuf.st_ino; - ((uint64_t *)fileid)[1] = (uint64_t)statbuf.st_dev; + ((uint64_t *)fileid)[0] = stDev; + ((uint64_t *)fileid)[1] = stIno; if (unique) { ((uint64_t *)fileid)[2] = rand(); } @@ -33,35 +33,34 @@ int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique) { return 0; } -int tdbCheckFileAccess(const char *pathname, int mode) { - int flags = 0; +// int tdbCheckFileAccess(const char *pathname, int mode) { +// int flags = 0; - if (mode & TDB_F_OK) { - flags |= F_OK; - } +// if (mode & TDB_F_OK) { +// flags |= F_OK; +// } - if (mode & TDB_R_OK) { - flags |= R_OK; - } +// if (mode & TDB_R_OK) { +// flags |= R_OK; +// } - if (mode & TDB_W_OK) { - flags |= W_OK; - } +// if (mode & TDB_W_OK) { +// flags |= W_OK; +// } - return access(pathname, flags); -} +// return access(pathname, flags); +// } int tdbGetFileSize(const char *fname, pgsz_t pgSize, pgno_t *pSize) { - struct stat st; int ret; - - ret = stat(fname, &st); + int64_t file_size = 0; + ret = taosStatFile(fname, &file_size, NULL); if (ret != 0) { return -1; } - ASSERT(st.st_size % pgSize == 0); + ASSERT(file_size % pgSize == 0); - *pSize = st.st_size / pgSize; + *pSize = file_size / pgSize; return 0; } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdbUtil.h b/source/libs/tdb/src/inc/tdbUtil.h index 8108e5aba6..ca05790f77 100644 --- a/source/libs/tdb/src/inc/tdbUtil.h +++ b/source/libs/tdb/src/inc/tdbUtil.h @@ -30,10 +30,10 @@ extern "C" { int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique); -#define TDB_F_OK 0x1 -#define TDB_R_OK 0x2 -#define TDB_W_OK 0x4 -int tdbCheckFileAccess(const char *pathname, int mode); +// #define TDB_F_OK 0x1 +// #define TDB_R_OK 0x2 +// #define TDB_W_OK 0x4 +// int tdbCheckFileAccess(const char *pathname, int mode); int tdbGetFileSize(const char *fname, pgsz_t pgSize, pgno_t *pSize); diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index 5ab0b4c0f1..ad550c7804 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -1,4 +1,4 @@ -#include "gtest/gtest.h" +#include #include "tdb.h" diff --git a/source/libs/tfs/src/tfs.c b/source/libs/tfs/src/tfs.c index 944e67c863..26fa90bdef 100644 --- a/source/libs/tfs/src/tfs.c +++ b/source/libs/tfs/src/tfs.c @@ -389,7 +389,6 @@ static int32_t tfsMount(STfs *pTfs, SDiskCfg *pCfg) { static int32_t tfsCheckAndFormatCfg(STfs *pTfs, SDiskCfg *pCfg) { char dirName[TSDB_FILENAME_LEN] = "\0"; - struct stat pstat; if (pCfg->level < 0 || pCfg->level >= TFS_MAX_TIERS) { fError("failed to mount %s to FS since invalid level %d", pCfg->dir, pCfg->level); @@ -422,19 +421,13 @@ static int32_t tfsCheckAndFormatCfg(STfs *pTfs, SDiskCfg *pCfg) { return -1; } - if (access(dirName, W_OK | R_OK | F_OK) != 0) { + if (!taosCheckAccessFile(dirName, TD_FILE_ACCESS_EXIST_OK | TD_FILE_ACCESS_READ_OK | TD_FILE_ACCESS_WRITE_OK)) { fError("failed to mount %s to FS since no R/W access rights", pCfg->dir); terrno = TSDB_CODE_FS_INVLD_CFG; return -1; } - if (stat(dirName, &pstat) < 0) { - fError("failed to mount %s to FS since %s", pCfg->dir, strerror(errno)); - terrno = TAOS_SYSTEM_ERROR(errno); - return -1; - } - - if (!S_ISDIR(pstat.st_mode)) { + if (!taosIsDir(dirName)) { fError("failed to mount %s to FS since not a directory", pCfg->dir); terrno = TSDB_CODE_FS_INVLD_CFG; return -1; diff --git a/source/libs/tfs/test/tfsTest.cpp b/source/libs/tfs/test/tfsTest.cpp index af66304f84..9cb914a670 100644 --- a/source/libs/tfs/test/tfsTest.cpp +++ b/source/libs/tfs/test/tfsTest.cpp @@ -119,29 +119,29 @@ TEST_F(TfsTest, 03_Dir) { char p1[] = "p1"; char ap1[128] = {0}; snprintf(ap1, 128, "%s%s%s", root, TD_DIRSEP, p1); - EXPECT_NE(taosDirExist(ap1), 0); + EXPECT_NE(taosDirExist(ap1), 1); EXPECT_EQ(tfsMkdir(pTfs, p1), 0); - EXPECT_EQ(taosDirExist(ap1), 0); + EXPECT_EQ(taosDirExist(ap1), 1); char p2[] = "p2"; char ap2[128] = {0}; snprintf(ap2, 128, "%s%s%s", root, TD_DIRSEP, p2); SDiskID did = {0}; - EXPECT_NE(taosDirExist(ap2), 0); + EXPECT_NE(taosDirExist(ap2), 1); EXPECT_EQ(tfsMkdirAt(pTfs, p2, did), 0); - EXPECT_EQ(taosDirExist(ap2), 0); + EXPECT_EQ(taosDirExist(ap2), 1); char p3[] = "p3/p2/p1/p0"; char ap3[128] = {0}; snprintf(ap3, 128, "%s%s%s", root, TD_DIRSEP, p3); - EXPECT_NE(taosDirExist(ap3), 0); + EXPECT_NE(taosDirExist(ap3), 1); EXPECT_NE(tfsMkdir(pTfs, p3), 0); EXPECT_NE(tfsMkdirAt(pTfs, p3, did), 0); EXPECT_EQ(tfsMkdirRecurAt(pTfs, p3, did), 0); - EXPECT_EQ(taosDirExist(ap3), 0); + EXPECT_EQ(taosDirExist(ap3), 1); EXPECT_EQ(tfsRmdir(pTfs, p3), 0); - EXPECT_NE(taosDirExist(ap3), 0); + EXPECT_NE(taosDirExist(ap3), 1); char p45[] = "p5"; char p44[] = "p4"; @@ -149,12 +149,12 @@ TEST_F(TfsTest, 03_Dir) { char ap4[128] = {0}; snprintf(ap4, 128, "%s%s%s", root, TD_DIRSEP, p4); - EXPECT_NE(taosDirExist(ap4), 0); + EXPECT_NE(taosDirExist(ap4), 1); EXPECT_EQ(tfsMkdirRecurAt(pTfs, p4, did), 0); - EXPECT_EQ(taosDirExist(ap4), 0); + EXPECT_EQ(taosDirExist(ap4), 1); EXPECT_EQ(tfsRename(pTfs, p44, p45), 0); EXPECT_EQ(tfsRmdir(pTfs, p4), 0); - EXPECT_NE(taosDirExist(ap4), 0); + EXPECT_NE(taosDirExist(ap4), 1); tfsClose(pTfs); } @@ -251,9 +251,9 @@ TEST_F(TfsTest, 04_File) { char af2[128] = {0}; snprintf(af2, 128, "%s%s%s", root, TD_DIRSEP, n2); - EXPECT_EQ(taosDirExist(af2), 0); + EXPECT_EQ(taosDirExist(af2), 1); tfsRemoveFile(&f2); - EXPECT_NE(taosDirExist(af2), 0); + EXPECT_NE(taosDirExist(af2), 1); { STfsDir *pDir = tfsOpendir(pTfs, "t3"); @@ -529,35 +529,35 @@ TEST_F(TfsTest, 05_MultiDisk) { snprintf(ap22, 128, "%s%s%s", root22, TD_DIRSEP, p1); char ap23[128] = {0}; snprintf(ap23, 128, "%s%s%s", root23, TD_DIRSEP, p1); - EXPECT_NE(taosDirExist(ap00), 0); - EXPECT_NE(taosDirExist(ap01), 0); - EXPECT_NE(taosDirExist(ap10), 0); - EXPECT_NE(taosDirExist(ap11), 0); - EXPECT_NE(taosDirExist(ap12), 0); - EXPECT_NE(taosDirExist(ap20), 0); - EXPECT_NE(taosDirExist(ap21), 0); - EXPECT_NE(taosDirExist(ap22), 0); - EXPECT_NE(taosDirExist(ap23), 0); + EXPECT_NE(taosDirExist(ap00), 1); + EXPECT_NE(taosDirExist(ap01), 1); + EXPECT_NE(taosDirExist(ap10), 1); + EXPECT_NE(taosDirExist(ap11), 1); + EXPECT_NE(taosDirExist(ap12), 1); + EXPECT_NE(taosDirExist(ap20), 1); + EXPECT_NE(taosDirExist(ap21), 1); + EXPECT_NE(taosDirExist(ap22), 1); + EXPECT_NE(taosDirExist(ap23), 1); EXPECT_EQ(tfsMkdir(pTfs, p1), 0); - EXPECT_EQ(taosDirExist(ap00), 0); - EXPECT_EQ(taosDirExist(ap01), 0); - EXPECT_EQ(taosDirExist(ap10), 0); - EXPECT_EQ(taosDirExist(ap11), 0); - EXPECT_EQ(taosDirExist(ap12), 0); - EXPECT_EQ(taosDirExist(ap20), 0); - EXPECT_EQ(taosDirExist(ap21), 0); - EXPECT_EQ(taosDirExist(ap22), 0); - EXPECT_EQ(taosDirExist(ap23), 0); + EXPECT_EQ(taosDirExist(ap00), 1); + EXPECT_EQ(taosDirExist(ap01), 1); + EXPECT_EQ(taosDirExist(ap10), 1); + EXPECT_EQ(taosDirExist(ap11), 1); + EXPECT_EQ(taosDirExist(ap12), 1); + EXPECT_EQ(taosDirExist(ap20), 1); + EXPECT_EQ(taosDirExist(ap21), 1); + EXPECT_EQ(taosDirExist(ap22), 1); + EXPECT_EQ(taosDirExist(ap23), 1); EXPECT_EQ(tfsRmdir(pTfs, p1), 0); - EXPECT_NE(taosDirExist(ap00), 0); - EXPECT_NE(taosDirExist(ap01), 0); - EXPECT_NE(taosDirExist(ap10), 0); - EXPECT_NE(taosDirExist(ap11), 0); - EXPECT_NE(taosDirExist(ap12), 0); - EXPECT_NE(taosDirExist(ap20), 0); - EXPECT_NE(taosDirExist(ap21), 0); - EXPECT_NE(taosDirExist(ap22), 0); - EXPECT_NE(taosDirExist(ap23), 0); + EXPECT_NE(taosDirExist(ap00), 1); + EXPECT_NE(taosDirExist(ap01), 1); + EXPECT_NE(taosDirExist(ap10), 1); + EXPECT_NE(taosDirExist(ap11), 1); + EXPECT_NE(taosDirExist(ap12), 1); + EXPECT_NE(taosDirExist(ap20), 1); + EXPECT_NE(taosDirExist(ap21), 1); + EXPECT_NE(taosDirExist(ap22), 1); + EXPECT_NE(taosDirExist(ap23), 1); char p2[] = "p2"; char _ap21[128] = {0}; @@ -565,22 +565,22 @@ TEST_F(TfsTest, 05_MultiDisk) { SDiskID did = {0}; did.level = 2; did.id = 1; - EXPECT_NE(taosDirExist(_ap21), 0); + EXPECT_NE(taosDirExist(_ap21), 1); EXPECT_EQ(tfsMkdirAt(pTfs, p2, did), 0); - EXPECT_EQ(taosDirExist(_ap21), 0); + EXPECT_EQ(taosDirExist(_ap21), 1); char p3[] = "p3/p2/p1/p0"; char _ap12[128] = {0}; snprintf(_ap12, 128, "%s%s%s", root12, TD_DIRSEP, p3); did.level = 1; did.id = 2; - EXPECT_NE(taosDirExist(_ap12), 0); + EXPECT_NE(taosDirExist(_ap12), 1); EXPECT_NE(tfsMkdir(pTfs, p3), 0); EXPECT_NE(tfsMkdirAt(pTfs, p3, did), 0); EXPECT_EQ(tfsMkdirRecurAt(pTfs, p3, did), 0); - EXPECT_EQ(taosDirExist(_ap12), 0); + EXPECT_EQ(taosDirExist(_ap12), 1); EXPECT_EQ(tfsRmdir(pTfs, p3), 0); - EXPECT_NE(taosDirExist(_ap12), 0); + EXPECT_NE(taosDirExist(_ap12), 1); char p45[] = "p5"; char p44[] = "p4"; @@ -590,12 +590,12 @@ TEST_F(TfsTest, 05_MultiDisk) { did.level = 2; did.id = 2; - EXPECT_NE(taosDirExist(_ap22), 0); + EXPECT_NE(taosDirExist(_ap22), 1); EXPECT_EQ(tfsMkdirRecurAt(pTfs, p4, did), 0); - EXPECT_EQ(taosDirExist(_ap22), 0); + EXPECT_EQ(taosDirExist(_ap22), 1); EXPECT_EQ(tfsRename(pTfs, p44, p45), 0); EXPECT_EQ(tfsRmdir(pTfs, p4), 0); - EXPECT_NE(taosDirExist(_ap22), 0); + EXPECT_NE(taosDirExist(_ap22), 1); } //------------- File -----------------// @@ -660,7 +660,7 @@ TEST_F(TfsTest, 05_MultiDisk) { char af2[128] = {0}; snprintf(af2, 128, "%s%s%s", root23, TD_DIRSEP, n2); - EXPECT_EQ(taosDirExist(af2), 0); + EXPECT_EQ(taosDirExist(af2), 1); tfsRemoveFile(&f2); { @@ -678,7 +678,7 @@ TEST_F(TfsTest, 05_MultiDisk) { tfsClosedir(pDir); } - EXPECT_NE(taosDirExist(af2), 0); + EXPECT_NE(taosDirExist(af2), 1); EXPECT_GT(tfsCopyFile(&f1, &f2), 0); { diff --git a/source/libs/wal/src/walMeta.c b/source/libs/wal/src/walMeta.c index ae0b0bd849..e64260c541 100644 --- a/source/libs/wal/src/walMeta.c +++ b/source/libs/wal/src/walMeta.c @@ -64,10 +64,10 @@ static inline int64_t walScanLogGetLastVer(SWal* pWal) { char fnameStr[WAL_FILE_LEN]; walBuildLogName(pWal, pLastFileInfo->firstVer, fnameStr); - struct stat statbuf; - stat(fnameStr, &statbuf); - int readSize = TMIN(WAL_MAX_SIZE + 2, statbuf.st_size); - pLastFileInfo->fileSize = statbuf.st_size; + int64_t file_size = 0; + taosStatFile(fnameStr, &file_size, NULL); + int readSize = TMIN(WAL_MAX_SIZE + 2, file_size); + pLastFileInfo->fileSize = file_size; TdFilePtr pFile = taosOpenFile(fnameStr, TD_FILE_READ); if (pFile == NULL) { @@ -177,11 +177,11 @@ int walCheckAndRepairMeta(SWal* pWal) { SWalFileInfo *pLastFileInfo = taosArrayGet(pWal->fileInfoSet, newSz-1); char fnameStr[WAL_FILE_LEN]; walBuildLogName(pWal, pLastFileInfo->firstVer, fnameStr); - struct stat statbuf; - stat(fnameStr, &statbuf); + int64_t file_size = 0; + taosStatFile(fnameStr, &file_size, NULL); - if (oldSz != newSz || pLastFileInfo->fileSize != statbuf.st_size) { - pLastFileInfo->fileSize = statbuf.st_size; + if (oldSz != newSz || pLastFileInfo->fileSize != file_size) { + pLastFileInfo->fileSize = file_size; pWal->vers.lastVer = walScanLogGetLastVer(pWal); ((SWalFileInfo*)taosArrayGetLast(pWal->fileInfoSet))->lastVer = pWal->vers.lastVer; ASSERT(pWal->vers.lastVer != -1); @@ -395,9 +395,9 @@ int walLoadMeta(SWal* pWal) { char fnameStr[WAL_FILE_LEN]; walBuildMetaName(pWal, metaVer, fnameStr); // read metafile - struct stat statbuf; - stat(fnameStr, &statbuf); - int size = statbuf.st_size; + int64_t file_size = 0; + taosStatFile(fnameStr, &file_size, NULL); + int size = (int)file_size; char* buf = malloc(size + 5); if (buf == NULL) { terrno = TSDB_CODE_WAL_OUT_OF_MEMORY; diff --git a/source/os/src/osDir.c b/source/os/src/osDir.c index 91ef97e66b..e0f5f80dab 100644 --- a/source/os/src/osDir.c +++ b/source/os/src/osDir.c @@ -60,7 +60,7 @@ void taosRemoveDir(const char *dirname) { //printf("dir:%s is removed\n", dirname); } -int32_t taosDirExist(char *dirname) { return access(dirname, F_OK); } +bool taosDirExist(char *dirname) { return taosCheckExistFile(dirname); } int32_t taosMkDir(const char *dirname) { int32_t code = mkdir(dirname, 0755); diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index c2e9b52f88..37004e9d70 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -186,6 +186,27 @@ int32_t taosStatFile(const char *path, int64_t *size, int32_t *mtime) { return 0; #endif } +int32_t taosDevInoFile(const char *path, int64_t *stDev, int64_t *stIno) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + return 0; +#else + struct stat fileStat; + int32_t code = stat(path, &fileStat); + if (code < 0) { + return code; + } + + if (stDev != NULL) { + *stDev = fileStat.st_dev; + } + + if (stIno != NULL) { + *stIno = fileStat.st_ino; + } + + return 0; +#endif +} void autoDelFileListAdd(const char *path) { return; } @@ -733,3 +754,21 @@ int32_t taosEOFFile(TdFilePtr pFile) { return feof(pFile->fp); } +bool taosCheckAccessFile(const char *pathname, int32_t tdFileAccessOptions) { + int flags = 0; + + if (tdFileAccessOptions & TD_FILE_ACCESS_EXIST_OK) { + flags |= F_OK; + } + + if (tdFileAccessOptions & TD_FILE_ACCESS_READ_OK) { + flags |= R_OK; + } + + if (tdFileAccessOptions & TD_FILE_ACCESS_WRITE_OK) { + flags |= W_OK; + } + + return access(pathname, flags) == 0; +} +bool taosCheckExistFile(const char *pathname) { return taosCheckAccessFile(pathname, TD_FILE_ACCESS_EXIST_OK); }; \ No newline at end of file diff --git a/source/util/test/cacheTest.cpp b/source/util/test/cacheTest.cpp index 970f1c23a9..4dde374bdd 100644 --- a/source/util/test/cacheTest.cpp +++ b/source/util/test/cacheTest.cpp @@ -1,7 +1,7 @@ -#include "os.h" #include #include +#include "os.h" #include "taos.h" #include "tcache.h" diff --git a/source/util/test/encodeTest.cpp b/source/util/test/encodeTest.cpp index 5505a6207f..7b993ebf35 100644 --- a/source/util/test/encodeTest.cpp +++ b/source/util/test/encodeTest.cpp @@ -1,6 +1,6 @@ #include -#include "gtest/gtest.h" +#include #include "tencode.h" diff --git a/source/util/test/freelistTest.cpp b/source/util/test/freelistTest.cpp index cf11d6b5bf..7fd3d693fb 100644 --- a/source/util/test/freelistTest.cpp +++ b/source/util/test/freelistTest.cpp @@ -1,4 +1,4 @@ -#include "gtest/gtest.h" +#include #include "tfreelist.h" diff --git a/source/util/test/hashTest.cpp b/source/util/test/hashTest.cpp index ac1bae2434..4c6ee8d10c 100644 --- a/source/util/test/hashTest.cpp +++ b/source/util/test/hashTest.cpp @@ -1,9 +1,9 @@ -#include "os.h" #include #include -#include #include +#include "os.h" +#include "taosdef.h" #include "thash.h" #include "taos.h" diff --git a/tools/shell/src/backup/shellImport.c b/tools/shell/src/backup/shellImport.c index c0ab7ef461..36489a448b 100644 --- a/tools/shell/src/backup/shellImport.c +++ b/tools/shell/src/backup/shellImport.c @@ -93,8 +93,7 @@ static void shellCheckTablesSQLFile(const char *directoryName) { sprintf(shellTablesSQLFile, "%s/tables.sql", directoryName); - struct stat fstat; - if (stat(shellTablesSQLFile, &fstat) < 0) { + if (taosFStatFile(shellTablesSQLFile, NULL, NULL) < 0) { shellTablesSQLFile[0] = 0; } } @@ -109,13 +108,12 @@ static void shellMallocSQLFiles() static void shellGetDirectoryFileList(char *inputDir) { - struct stat fileStat; - if (stat(inputDir, &fileStat) < 0) { + if (!taosDirExist(inputDir)) { fprintf(stderr, "ERROR: %s not exist\n", inputDir); exit(0); } - if (fileStat.st_mode & S_IFDIR) { + if (taosIsDir(inputDir)) { shellCheckTablesSQLFile(inputDir); shellSQLFileNum = shellGetFilesNum(inputDir, "sql"); int totalSQLFileNum = shellSQLFileNum;