diff --git a/include/os/osDir.h b/include/os/osDir.h index e328c420638eb1d146abe97f8abf6bacb6286377..eda83ea0ea85a5cf4e03cdf620c357c34294ec0c 100644 --- a/include/os/osDir.h +++ b/include/os/osDir.h @@ -26,6 +26,7 @@ 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); int32_t taosRealPath(char *dirname, int32_t maxlen); +bool taosIsDir(const char *dirname); #ifdef __cplusplus } diff --git a/include/os/osSysinfo.h b/include/os/osSysinfo.h index a0771dc7340562787a0e8bd69fbb880998fb8dcc..35ac032a8a65d4d530893478d7051cee2bd5565b 100644 --- a/include/os/osSysinfo.h +++ b/include/os/osSysinfo.h @@ -33,21 +33,26 @@ typedef struct { SDiskSize size; } SDiskSpace; -int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize); -int32_t taosGetCpuCores(); 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 taosGetCpuCores(); +bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage); +bool taosGetTotalSysMemoryKB(uint64_t *kb); +bool taosGetProcMemory(float *memoryUsedMB); // +bool taosGetSysMemory(float *memoryUsedMB); // +void taosGetDisk(); +int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize); bool taosReadProcIO(int64_t *rchars, int64_t *wchars); bool taosGetProcIO(float *readKB, float *writeKB); bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes); bool taosGetBandSpeed(float *bandSpeedKb); -void taosGetDisk(); -bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage); -bool taosGetProcMemory(float *memoryUsedMB); -bool taosGetSysMemory(float *memoryUsedMB); -int taosSystem(const char *cmd); + +int32_t taosSystem(const char *cmd); void taosKillSystem(); int32_t taosGetSystemUUID(char *uid, int32_t uidlen); -char * taosGetCmdlineByPID(int pid); +char *taosGetCmdlineByPID(int32_t pid); void taosSetCoreDump(bool enable); typedef struct { diff --git a/include/util/tconfig.h b/include/util/tconfig.h index 8275054a64f188c6f30a70a481f9ff9f9ab95d2b..0e1d352f9a8b2cdc4ab9bffb728868738b8b9cac 100644 --- a/include/util/tconfig.h +++ b/include/util/tconfig.h @@ -82,7 +82,7 @@ typedef struct SConfig { SConfig *cfgInit(); int32_t cfgLoad(SConfig *pCfg, ECfgSrcType cfgType, const char *sourceStr); -int32_t cfgLoadArray(SConfig *pCfg, SArray *pArgs); // SConfigPair +int32_t cfgLoadFromArray(SConfig *pCfg, SArray *pArgs); // SConfigPair void cfgCleanup(SConfig *pCfg); int32_t cfgGetSize(SConfig *pCfg); diff --git a/include/util/thttp.h b/include/util/thttp.h new file mode 100644 index 0000000000000000000000000000000000000000..f211b2615d8d166ad82bf0125655a7efdb58ff94 --- /dev/null +++ b/include/util/thttp.h @@ -0,0 +1,31 @@ +/* + * 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_UTIL_HTTP_H_ +#define _TD_UTIL_HTTP_H_ + +#include "os.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int32_t taosSendHttpReport(const char* server, uint16_t port, const char* pCont, int32_t contLen); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_UTIL_UTIL_H_*/ diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index 00cf770e6b2756df1b5bdcdde812e01cdd5b2e2e..d66895888b60d4c7d265079513ab5415b3ed9507 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -177,24 +177,24 @@ static int32_t taosLoadCfg(SConfig *pCfg, const char *inputCfgDir, const char *e snprintf(cfgFile, sizeof(cfgFile), "%s" TD_DIRSEP "taos.cfg", cfgDir); if (cfgLoad(pCfg, CFG_STYPE_APOLLO_URL, apolloUrl) != 0) { - uError("failed to load from apollo url:%s since %s\n", apolloUrl, terrstr()); + uError("failed to load from apollo url:%s since %s", apolloUrl, terrstr()); return -1; } - if (cfgLoad(pCfg, CFG_STYPE_CFG_FILE, cfgFile) != 0) { - if (cfgLoad(pCfg, CFG_STYPE_CFG_FILE, cfgDir) != 0) { - uError("failed to load from config file:%s since %s\n", cfgFile, terrstr()); - return -1; + if (cfgLoad(pCfg, CFG_STYPE_CFG_FILE, cfgDir) != 0) { + if (cfgLoad(pCfg, CFG_STYPE_CFG_FILE, cfgFile) != 0) { + uError("failed to load from config file:%s since %s", cfgFile, terrstr()); + return 0; } } if (cfgLoad(pCfg, CFG_STYPE_ENV_FILE, envFile) != 0) { - uError("failed to load from env file:%s since %s\n", envFile, terrstr()); + uError("failed to load from env file:%s since %s", envFile, terrstr()); return -1; } if (cfgLoad(pCfg, CFG_STYPE_ENV_VAR, NULL) != 0) { - uError("failed to load from global env variables since %s\n", terrstr()); + uError("failed to load from global env variables since %s", terrstr()); return -1; } @@ -438,8 +438,10 @@ int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDi if (pCfg == NULL) return -1; if (tsc) { + tscEmbeddedInUtil = 0; if (taosAddClientLogCfg(pCfg) != 0) return -1; } else { + tscEmbeddedInUtil = 1; if (taosAddClientLogCfg(pCfg) != 0) return -1; if (taosAddServerLogCfg(pCfg) != 0) return -1; } @@ -450,7 +452,7 @@ int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDi return -1; } - if (cfgLoadArray(pCfg, pArgs) != 0) { + if (cfgLoadFromArray(pCfg, pArgs) != 0) { uError("failed to load cfg from array since %s", terrstr()); cfgCleanup(pCfg); return -1; @@ -465,8 +467,14 @@ int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDi taosSetAllDebugFlag(cfgGetItem(pCfg, "debugFlag")->i32); + if (taosMkDir(tsLogDir) != 0) { + uError("failed to create dir:%s since %s", tsLogDir, terrstr()); + cfgCleanup(pCfg); + return -1; + } + if (taosInitLog(logname, logFileNum) != 0) { - printf("failed to init log file since %s\n", terrstr()); + uError("failed to init log file since %s", terrstr()); cfgCleanup(pCfg); return -1; } @@ -497,7 +505,7 @@ int32_t taosInitCfg(const char *cfgDir, const char *envFile, const char *apolloU return -1; } - if (cfgLoadArray(tsCfg, pArgs) != 0) { + if (cfgLoadFromArray(tsCfg, pArgs) != 0) { uError("failed to load cfg from array since %s", terrstr()); cfgCleanup(tsCfg); return -1; @@ -512,6 +520,16 @@ int32_t taosInitCfg(const char *cfgDir, const char *envFile, const char *apolloU } taosSetSystemCfg(tsCfg); + if (taosMkDir(tsTempDir) != 0) { + uError("failed to create dir:%s since %s", tsTempDir, terrstr()); + return -1; + } + + if (!tsc && taosMkDir(tsDataDir) != 0) { + uError("failed to create dir:%s since %s", tsDataDir, terrstr()); + return -1; + } + cfgDumpCfg(tsCfg, tsc, false); return 0; } diff --git a/source/common/src/ttypes.c b/source/common/src/ttypes.c index f32698c98ad7179f2661c85fec2df3906aa76dc0..0c1bafbeb86f9dc04521d7245193c620b6f42dda 100644 --- a/source/common/src/ttypes.c +++ b/source/common/src/ttypes.c @@ -49,16 +49,16 @@ const int32_t TYPE_BYTES[15] = { } \ } while (0) -static void getStatics_bool(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, - int64_t *sum, int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { +static void getStatics_bool(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, int64_t *sum, + int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { int8_t *data = (int8_t *)pData; *min = INT64_MAX; *max = INT64_MIN; *minIndex = 0; *maxIndex = 0; - + assert(numOfRow <= INT16_MAX); - + for (int32_t i = 0; i < numOfRow; ++i) { if (data[i] == TSDB_DATA_BOOL_NULL) { (*numOfNull) += 1; @@ -76,9 +76,9 @@ static void getStatics_i8(const void *pData, int32_t numOfRow, int64_t *min, int *max = INT64_MIN; *minIndex = 0; *maxIndex = 0; - + assert(numOfRow <= INT16_MAX); - + for (int32_t i = 0; i < numOfRow; ++i) { if (((uint8_t)data[i]) == TSDB_DATA_TINYINT_NULL) { (*numOfNull) += 1; @@ -122,9 +122,9 @@ static void getStatics_i16(const void *pData, int32_t numOfRow, int64_t *min, in *max = INT64_MIN; *minIndex = 0; *maxIndex = 0; - + assert(numOfRow <= INT16_MAX); - + for (int32_t i = 0; i < numOfRow; ++i) { if (((uint16_t)data[i]) == TSDB_DATA_SMALLINT_NULL) { (*numOfNull) += 1; @@ -133,15 +133,14 @@ static void getStatics_i16(const void *pData, int32_t numOfRow, int64_t *min, in DO_STATICS(*sum, *min, *max, *minIndex, *maxIndex, data, i); } - } static void getStatics_u16(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, int64_t *sum, int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { uint16_t *data = (uint16_t *)pData; - uint64_t _min = UINT64_MAX; - uint64_t _max = 0; - uint64_t _sum = 0; + uint64_t _min = UINT64_MAX; + uint64_t _max = 0; + uint64_t _sum = 0; *minIndex = 0; *maxIndex = 0; @@ -169,9 +168,9 @@ static void getStatics_i32(const void *pData, int32_t numOfRow, int64_t *min, in *max = INT64_MIN; *minIndex = 0; *maxIndex = 0; - + assert(numOfRow <= INT16_MAX); - + for (int32_t i = 0; i < numOfRow; ++i) { if (((uint32_t)data[i]) == TSDB_DATA_INT_NULL) { (*numOfNull) += 1; @@ -182,12 +181,12 @@ static void getStatics_i32(const void *pData, int32_t numOfRow, int64_t *min, in } } -static void getStatics_u32(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, - int64_t *sum, int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { +static void getStatics_u32(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, int64_t *sum, + int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { uint32_t *data = (uint32_t *)pData; - uint64_t _min = UINT64_MAX; - uint64_t _max = 0; - uint64_t _sum = 0; + uint64_t _min = UINT64_MAX; + uint64_t _max = 0; + uint64_t _sum = 0; *minIndex = 0; *maxIndex = 0; @@ -208,16 +207,16 @@ static void getStatics_u32(const void *pData, int32_t numOfRow, int64_t *min, in *sum = _sum; } -static void getStatics_i64(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, - int64_t *sum, int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { +static void getStatics_i64(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, int64_t *sum, + int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { int64_t *data = (int64_t *)pData; *min = INT64_MAX; *max = INT64_MIN; *minIndex = 0; *maxIndex = 0; - + assert(numOfRow <= INT16_MAX); - + for (int32_t i = 0; i < numOfRow; ++i) { if (((uint64_t)data[i]) == TSDB_DATA_BIGINT_NULL) { (*numOfNull) += 1; @@ -228,12 +227,12 @@ static void getStatics_i64(const void *pData, int32_t numOfRow, int64_t *min, in } } -static void getStatics_u64(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, - int64_t *sum, int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { +static void getStatics_u64(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, int64_t *sum, + int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { uint64_t *data = (uint64_t *)pData; - uint64_t _min = UINT64_MAX; - uint64_t _max = 0; - uint64_t _sum = 0; + uint64_t _min = UINT64_MAX; + uint64_t _max = 0; + uint64_t _sum = 0; *minIndex = 0; *maxIndex = 0; @@ -254,91 +253,91 @@ static void getStatics_u64(const void *pData, int32_t numOfRow, int64_t *min, in *sum = _sum; } -static void getStatics_f(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, - int64_t *sum, int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { - float *data = (float *)pData; - float fmin = FLT_MAX; - float fmax = -FLT_MAX; - double dsum = 0; - *minIndex = 0; - *maxIndex = 0; - +static void getStatics_f(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, int64_t *sum, + int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { + float *data = (float *)pData; + float fmin = FLT_MAX; + float fmax = -FLT_MAX; + double dsum = 0; + *minIndex = 0; + *maxIndex = 0; + assert(numOfRow <= INT16_MAX); - + for (int32_t i = 0; i < numOfRow; ++i) { - if ((*(uint32_t*)&(data[i])) == TSDB_DATA_FLOAT_NULL) { + if ((*(uint32_t *)&(data[i])) == TSDB_DATA_FLOAT_NULL) { (*numOfNull) += 1; continue; } - float fv = GET_FLOAT_VAL((const char*)&(data[i])); + float fv = GET_FLOAT_VAL((const char *)&(data[i])); dsum += fv; if (fmin > fv) { fmin = fv; *minIndex = i; } - + if (fmax < fv) { fmax = fv; *maxIndex = i; } } - + SET_DOUBLE_VAL(sum, dsum); SET_DOUBLE_VAL(max, fmax); SET_DOUBLE_VAL(min, fmin); } -static void getStatics_d(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, - int64_t *sum, int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { +static void getStatics_d(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, int64_t *sum, + int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { double *data = (double *)pData; - double dmin = DBL_MAX; - double dmax = -DBL_MAX; - double dsum = 0; - *minIndex = 0; - *maxIndex = 0; - + double dmin = DBL_MAX; + double dmax = -DBL_MAX; + double dsum = 0; + *minIndex = 0; + *maxIndex = 0; + assert(numOfRow <= INT16_MAX); - + for (int32_t i = 0; i < numOfRow; ++i) { - if ((*(uint64_t*)&(data[i])) == TSDB_DATA_DOUBLE_NULL) { + if ((*(uint64_t *)&(data[i])) == TSDB_DATA_DOUBLE_NULL) { (*numOfNull) += 1; continue; } - + double dv = 0; - dv = GET_DOUBLE_VAL((const char*)&(data[i])); + dv = GET_DOUBLE_VAL((const char *)&(data[i])); dsum += dv; if (dmin > dv) { dmin = dv; *minIndex = i; } - + if (dmax < dv) { dmax = dv; *maxIndex = i; } } - + SET_DOUBLE_PTR(sum, &dsum); SET_DOUBLE_PTR(max, &dmax); SET_DOUBLE_PTR(min, &dmin); } -static void getStatics_bin(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, - int64_t *sum, int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { - const char* data = pData; +static void getStatics_bin(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, int64_t *sum, + int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { + const char *data = pData; assert(numOfRow <= INT16_MAX); - + for (int32_t i = 0; i < numOfRow; ++i) { if (isNull(data, TSDB_DATA_TYPE_BINARY)) { (*numOfNull) += 1; } - + data += varDataTLen(data); } - + *sum = 0; *max = 0; *min = 0; @@ -346,19 +345,19 @@ static void getStatics_bin(const void *pData, int32_t numOfRow, int64_t *min, in *maxIndex = 0; } -static void getStatics_nchr(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, - int64_t *sum, int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { - const char* data = pData; +static void getStatics_nchr(const void *pData, int32_t numOfRow, int64_t *min, int64_t *max, int64_t *sum, + int16_t *minIndex, int16_t *maxIndex, int16_t *numOfNull) { + const char *data = pData; assert(numOfRow <= INT16_MAX); - + for (int32_t i = 0; i < numOfRow; ++i) { if (isNull(data, TSDB_DATA_TYPE_NCHAR)) { (*numOfNull) += 1; } - + data += varDataTLen(data); } - + *sum = 0; *max = 0; *min = 0; @@ -367,21 +366,28 @@ static void getStatics_nchr(const void *pData, int32_t numOfRow, int64_t *min, i } tDataTypeDescriptor tDataTypes[15] = { - {TSDB_DATA_TYPE_NULL, 6, 1, "NOTYPE", 0, 0, NULL, NULL, NULL}, - {TSDB_DATA_TYPE_BOOL, 4, CHAR_BYTES, "BOOL", false, true, tsCompressBool, tsDecompressBool, getStatics_bool}, - {TSDB_DATA_TYPE_TINYINT, 7, CHAR_BYTES, "TINYINT", INT8_MIN, INT8_MAX, tsCompressTinyint, tsDecompressTinyint, getStatics_i8}, - {TSDB_DATA_TYPE_SMALLINT, 8, SHORT_BYTES, "SMALLINT", INT16_MIN, INT16_MAX, tsCompressSmallint, tsDecompressSmallint, getStatics_i16}, - {TSDB_DATA_TYPE_INT, 3, INT_BYTES, "INT", INT32_MIN, INT32_MAX, tsCompressInt, tsDecompressInt, getStatics_i32}, - {TSDB_DATA_TYPE_BIGINT, 6, LONG_BYTES, "BIGINT", INT64_MIN, INT64_MAX, tsCompressBigint, tsDecompressBigint, getStatics_i64}, - {TSDB_DATA_TYPE_FLOAT, 5, FLOAT_BYTES, "FLOAT", 0, 0, tsCompressFloat, tsDecompressFloat, getStatics_f}, - {TSDB_DATA_TYPE_DOUBLE, 6, DOUBLE_BYTES, "DOUBLE", 0, 0, tsCompressDouble, tsDecompressDouble, getStatics_d}, - {TSDB_DATA_TYPE_BINARY, 6, 0, "BINARY", 0, 0, tsCompressString, tsDecompressString, getStatics_bin}, - {TSDB_DATA_TYPE_TIMESTAMP, 9, LONG_BYTES, "TIMESTAMP", INT64_MIN, INT64_MAX, tsCompressTimestamp, tsDecompressTimestamp, getStatics_i64}, - {TSDB_DATA_TYPE_NCHAR, 5, 8, "NCHAR", 0, 0, tsCompressString, tsDecompressString, getStatics_nchr}, - {TSDB_DATA_TYPE_UTINYINT, 16, CHAR_BYTES, "TINYINT UNSIGNED", 0, UINT8_MAX, tsCompressTinyint, tsDecompressTinyint, getStatics_u8}, - {TSDB_DATA_TYPE_USMALLINT, 17, SHORT_BYTES, "SMALLINT UNSIGNED", 0, UINT16_MAX, tsCompressSmallint, tsDecompressSmallint, getStatics_u16}, - {TSDB_DATA_TYPE_UINT, 12, INT_BYTES, "INT UNSIGNED", 0, UINT32_MAX, tsCompressInt, tsDecompressInt, getStatics_u32}, - {TSDB_DATA_TYPE_UBIGINT, 15, LONG_BYTES, "BIGINT UNSIGNED", 0, UINT64_MAX, tsCompressBigint, tsDecompressBigint, getStatics_u64}, + {TSDB_DATA_TYPE_NULL, 6, 1, "NOTYPE", 0, 0, NULL, NULL, NULL}, + {TSDB_DATA_TYPE_BOOL, 4, CHAR_BYTES, "BOOL", false, true, tsCompressBool, tsDecompressBool, getStatics_bool}, + {TSDB_DATA_TYPE_TINYINT, 7, CHAR_BYTES, "TINYINT", INT8_MIN, INT8_MAX, tsCompressTinyint, tsDecompressTinyint, + getStatics_i8}, + {TSDB_DATA_TYPE_SMALLINT, 8, SHORT_BYTES, "SMALLINT", INT16_MIN, INT16_MAX, tsCompressSmallint, + tsDecompressSmallint, getStatics_i16}, + {TSDB_DATA_TYPE_INT, 3, INT_BYTES, "INT", INT32_MIN, INT32_MAX, tsCompressInt, tsDecompressInt, getStatics_i32}, + {TSDB_DATA_TYPE_BIGINT, 6, LONG_BYTES, "BIGINT", INT64_MIN, INT64_MAX, tsCompressBigint, tsDecompressBigint, + getStatics_i64}, + {TSDB_DATA_TYPE_FLOAT, 5, FLOAT_BYTES, "FLOAT", 0, 0, tsCompressFloat, tsDecompressFloat, getStatics_f}, + {TSDB_DATA_TYPE_DOUBLE, 6, DOUBLE_BYTES, "DOUBLE", 0, 0, tsCompressDouble, tsDecompressDouble, getStatics_d}, + {TSDB_DATA_TYPE_BINARY, 6, 0, "BINARY", 0, 0, tsCompressString, tsDecompressString, getStatics_bin}, + {TSDB_DATA_TYPE_TIMESTAMP, 9, LONG_BYTES, "TIMESTAMP", INT64_MIN, INT64_MAX, tsCompressTimestamp, + tsDecompressTimestamp, getStatics_i64}, + {TSDB_DATA_TYPE_NCHAR, 5, 8, "NCHAR", 0, 0, tsCompressString, tsDecompressString, getStatics_nchr}, + {TSDB_DATA_TYPE_UTINYINT, 16, CHAR_BYTES, "TINYINT UNSIGNED", 0, UINT8_MAX, tsCompressTinyint, tsDecompressTinyint, + getStatics_u8}, + {TSDB_DATA_TYPE_USMALLINT, 17, SHORT_BYTES, "SMALLINT UNSIGNED", 0, UINT16_MAX, tsCompressSmallint, + tsDecompressSmallint, getStatics_u16}, + {TSDB_DATA_TYPE_UINT, 12, INT_BYTES, "INT UNSIGNED", 0, UINT32_MAX, tsCompressInt, tsDecompressInt, getStatics_u32}, + {TSDB_DATA_TYPE_UBIGINT, 15, LONG_BYTES, "BIGINT UNSIGNED", 0, UINT64_MAX, tsCompressBigint, tsDecompressBigint, + getStatics_u64}, }; char tTokenTypeSwitcher[13] = { @@ -400,10 +406,10 @@ char tTokenTypeSwitcher[13] = { TSDB_DATA_TYPE_NCHAR, // TK_NCHAR }; -float floatMin = -FLT_MAX, floatMax = FLT_MAX; +float floatMin = -FLT_MAX, floatMax = FLT_MAX; double doubleMin = -DBL_MAX, doubleMax = DBL_MAX; -FORCE_INLINE void* getDataMin(int32_t type) { +FORCE_INLINE void *getDataMin(int32_t type) { switch (type) { case TSDB_DATA_TYPE_FLOAT: return &floatMin; @@ -414,7 +420,7 @@ FORCE_INLINE void* getDataMin(int32_t type) { } } -FORCE_INLINE void* getDataMax(int32_t type) { +FORCE_INLINE void *getDataMax(int32_t type) { switch (type) { case TSDB_DATA_TYPE_FLOAT: return &floatMax; @@ -425,17 +431,15 @@ FORCE_INLINE void* getDataMax(int32_t type) { } } -bool isValidDataType(int32_t type) { - return type >= TSDB_DATA_TYPE_NULL && type <= TSDB_DATA_TYPE_UBIGINT; -} +bool isValidDataType(int32_t type) { return type >= TSDB_DATA_TYPE_NULL && type <= TSDB_DATA_TYPE_UBIGINT; } -void setVardataNull(void* val, int32_t type) { +void setVardataNull(void *val, int32_t type) { if (type == TSDB_DATA_TYPE_BINARY) { varDataSetLen(val, sizeof(int8_t)); - *(uint8_t*) varDataVal(val) = TSDB_DATA_BINARY_NULL; + *(uint8_t *)varDataVal(val) = TSDB_DATA_BINARY_NULL; } else if (type == TSDB_DATA_TYPE_NCHAR) { varDataSetLen(val, sizeof(int32_t)); - *(uint32_t*) varDataVal(val) = TSDB_DATA_NCHAR_NULL; + *(uint32_t *)varDataVal(val) = TSDB_DATA_NCHAR_NULL; } else { assert(0); } @@ -533,9 +537,8 @@ static SBinaryNullT nullBinary = {1, TSDB_DATA_BINARY_NULL}; static SNCharNullT nullNchar = {4, TSDB_DATA_NCHAR_NULL}; static const void *nullValues[] = { - &nullBool, &nullTinyInt, &nullSmallInt, &nullInt, &nullBigInt, - &nullFloat, &nullDouble, &nullBinary, &nullBigInt, &nullNchar, - &nullTinyIntu, &nullSmallIntu, &nullIntu, &nullBigIntu, + &nullBool, &nullTinyInt, &nullSmallInt, &nullInt, &nullBigInt, &nullFloat, &nullDouble, + &nullBinary, &nullBigInt, &nullNchar, &nullTinyIntu, &nullSmallIntu, &nullIntu, &nullBigIntu, }; const void *getNullValue(int32_t type) { @@ -590,7 +593,7 @@ void operateVal(void *dst, void *s1, void *s2, int32_t optr, int32_t type) { *((int8_t *)dst) = GET_INT8_VAL(s1) + GET_INT8_VAL(s2); break; case TSDB_DATA_TYPE_UTINYINT: - *((uint8_t *)dst) = GET_UINT8_VAL(s1) + GET_UINT8_VAL(s2); + *((uint8_t *)dst) = GET_UINT8_VAL(s1) + GET_UINT8_VAL(s2); break; case TSDB_DATA_TYPE_SMALLINT: *((int16_t *)dst) = GET_INT16_VAL(s1) + GET_INT16_VAL(s2); @@ -629,14 +632,14 @@ void operateVal(void *dst, void *s1, void *s2, int32_t optr, int32_t type) { } } -void tsDataSwap(void *pLeft, void *pRight, int32_t type, int32_t size, void* buf) { +void tsDataSwap(void *pLeft, void *pRight, int32_t type, int32_t size, void *buf) { switch (type) { case TSDB_DATA_TYPE_INT: case TSDB_DATA_TYPE_UINT: { TSWAP(*(int32_t *)(pLeft), *(int32_t *)(pRight), int32_t); break; } - + case TSDB_DATA_TYPE_BIGINT: case TSDB_DATA_TYPE_UBIGINT: case TSDB_DATA_TYPE_TIMESTAMP: { @@ -652,19 +655,19 @@ void tsDataSwap(void *pLeft, void *pRight, int32_t type, int32_t size, void* buf TSWAP(*(int16_t *)(pLeft), *(int16_t *)(pRight), int16_t); break; } - + case TSDB_DATA_TYPE_FLOAT: { TSWAP(*(float *)(pLeft), *(float *)(pRight), float); break; } - + case TSDB_DATA_TYPE_BOOL: case TSDB_DATA_TYPE_TINYINT: case TSDB_DATA_TYPE_UTINYINT: { TSWAP(*(int8_t *)(pLeft), *(int8_t *)(pRight), int8_t); break; } - + default: { memcpy(buf, pLeft, size); memcpy(pLeft, pRight, size); diff --git a/source/common/src/tvariant.c b/source/common/src/tvariant.c index ceef12e986105ad42b1322db26c20a19a760f740..e885d842d77442edc72a83b86458770001b52a06 100644 --- a/source/common/src/tvariant.c +++ b/source/common/src/tvariant.c @@ -35,9 +35,9 @@ assert(0); \ } while (0) -int32_t toInteger(const char* z, int32_t n, int32_t base, int64_t* value, bool* isSigned) { +int32_t toInteger(const char *z, int32_t n, int32_t base, int64_t *value, bool *isSigned) { errno = 0; - char* endPtr = NULL; + char *endPtr = NULL; int32_t index = 0; @@ -63,15 +63,15 @@ int32_t toInteger(const char* z, int32_t n, int32_t base, int64_t* value, bool* *isSigned = specifiedSign || (val <= INT64_MAX); if (*isSigned) { - *value = (z[0] == '-')? -val:val; + *value = (z[0] == '-') ? -val : val; } else { - *(uint64_t*) value = val; + *(uint64_t *)value = val; } return 0; } -void taosVariantCreate(SVariant *pVar, const char* z, int32_t n, int32_t type) { +void taosVariantCreate(SVariant *pVar, const char *z, int32_t n, int32_t type) { int32_t ret = 0; memset(pVar, 0, sizeof(SVariant)); @@ -90,7 +90,7 @@ void taosVariantCreate(SVariant *pVar, const char* z, int32_t n, int32_t type) { case TSDB_DATA_TYPE_TINYINT: case TSDB_DATA_TYPE_SMALLINT: case TSDB_DATA_TYPE_BIGINT: - case TSDB_DATA_TYPE_INT:{ + case TSDB_DATA_TYPE_INT: { bool sign = true; int32_t base = 10; @@ -104,11 +104,11 @@ void taosVariantCreate(SVariant *pVar, const char* z, int32_t n, int32_t type) { ret = toInteger(z, n, base, &pVar->i, &sign); if (ret != 0) { - pVar->nType = -1; // -1 means error type + pVar->nType = -1; // -1 means error type return; } - pVar->nType = (sign)? TSDB_DATA_TYPE_BIGINT:TSDB_DATA_TYPE_UBIGINT; + pVar->nType = (sign) ? TSDB_DATA_TYPE_BIGINT : TSDB_DATA_TYPE_UBIGINT; break; } case TSDB_DATA_TYPE_DOUBLE: @@ -123,15 +123,15 @@ void taosVariantCreate(SVariant *pVar, const char* z, int32_t n, int32_t type) { } case TSDB_DATA_TYPE_TIMESTAMP: { assert(0); - pVar->i = taosGetTimestamp(TSDB_TIME_PRECISION_NANO); - break; - } - + pVar->i = taosGetTimestamp(TSDB_TIME_PRECISION_NANO); + break; + } + default: { // nType == 0 means the null value type = TSDB_DATA_TYPE_NULL; } } - + pVar->nType = type; } @@ -196,13 +196,13 @@ void taosVariantCreateFromBinary(SVariant *pVar, const char *pz, size_t len, uin pVar->d = GET_FLOAT_VAL(pz); break; } - case TSDB_DATA_TYPE_NCHAR: { // here we get the nchar length from raw binary bits length + case TSDB_DATA_TYPE_NCHAR: { // here we get the nchar length from raw binary bits length size_t lenInwchar = len / TSDB_NCHAR_SIZE; pVar->wpz = calloc(1, (lenInwchar + 1) * TSDB_NCHAR_SIZE); memcpy(pVar->wpz, pz, lenInwchar * TSDB_NCHAR_SIZE); pVar->nLen = (int32_t)len; - + break; } case TSDB_DATA_TYPE_BINARY: { // todo refactor, extract a method @@ -211,18 +211,18 @@ void taosVariantCreateFromBinary(SVariant *pVar, const char *pz, size_t len, uin pVar->nLen = (int32_t)len; break; } - + default: pVar->i = GET_INT32_VAL(pz); pVar->nLen = tDataTypes[TSDB_DATA_TYPE_INT].bytes; } - + pVar->nType = type; } void taosVariantDestroy(SVariant *pVar) { if (pVar == NULL) return; - + if (pVar->nType == TSDB_DATA_TYPE_BINARY || pVar->nType == TSDB_DATA_TYPE_NCHAR) { tfree(pVar->pz); pVar->nLen = 0; @@ -231,8 +231,8 @@ void taosVariantDestroy(SVariant *pVar) { // NOTE: this is only for string array if (pVar->nType == TSDB_DATA_TYPE_POINTER_ARRAY) { size_t num = taosArrayGetSize(pVar->arr); - for(size_t i = 0; i < num; i++) { - void* p = taosArrayGetP(pVar->arr, i); + for (size_t i = 0; i < num; i++) { + void *p = taosArrayGetP(pVar->arr, i); free(p); } taosArrayDestroy(pVar->arr); @@ -250,11 +250,11 @@ bool taosVariantIsValid(SVariant *pVar) { void taosVariantAssign(SVariant *pDst, const SVariant *pSrc) { if (pSrc == NULL || pDst == NULL) return; - + pDst->nType = pSrc->nType; if (pSrc->nType == TSDB_DATA_TYPE_BINARY || pSrc->nType == TSDB_DATA_TYPE_NCHAR) { int32_t len = pSrc->nLen + TSDB_NCHAR_SIZE; - char* p = realloc(pDst->pz, len); + char *p = realloc(pDst->pz, len); assert(p); memset(p, 0, len); @@ -263,28 +263,27 @@ void taosVariantAssign(SVariant *pDst, const SVariant *pSrc) { memcpy(pDst->pz, pSrc->pz, pSrc->nLen); pDst->nLen = pSrc->nLen; return; - } if (IS_NUMERIC_TYPE(pSrc->nType) || (pSrc->nType == TSDB_DATA_TYPE_BOOL)) { pDst->i = pSrc->i; } else if (pSrc->nType == TSDB_DATA_TYPE_POINTER_ARRAY) { // this is only for string array size_t num = taosArrayGetSize(pSrc->arr); - pDst->arr = taosArrayInit(num, sizeof(char*)); - for(size_t i = 0; i < num; i++) { - char* p = (char*)taosArrayGetP(pSrc->arr, i); - char* n = strdup(p); + pDst->arr = taosArrayInit(num, sizeof(char *)); + for (size_t i = 0; i < num; i++) { + char *p = (char *)taosArrayGetP(pSrc->arr, i); + char *n = strdup(p); taosArrayPush(pDst->arr, &n); } } else if (pSrc->nType == TSDB_DATA_TYPE_VALUE_ARRAY) { - size_t num = taosArrayGetSize(pSrc->arr); - pDst->arr = taosArrayInit(num, sizeof(int64_t)); - pDst->nLen = pSrc->nLen; - assert(pSrc->nLen == num); - for(size_t i = 0; i < num; i++) { - int64_t *p = taosArrayGet(pSrc->arr, i); - taosArrayPush(pDst->arr, p); - } + size_t num = taosArrayGetSize(pSrc->arr); + pDst->arr = taosArrayInit(num, sizeof(int64_t)); + pDst->nLen = pSrc->nLen; + assert(pSrc->nLen == num); + for (size_t i = 0; i < num; i++) { + int64_t *p = taosArrayGet(pSrc->arr, i); + taosArrayPush(pDst->arr, p); + } } if (pDst->nType != TSDB_DATA_TYPE_POINTER_ARRAY && pDst->nType != TSDB_DATA_TYPE_VALUE_ARRAY) { @@ -292,7 +291,7 @@ void taosVariantAssign(SVariant *pDst, const SVariant *pSrc) { } } -int32_t taosVariantCompare(const SVariant* p1, const SVariant* p2) { +int32_t taosVariantCompare(const SVariant *p1, const SVariant *p2) { if (p1->nType == TSDB_DATA_TYPE_NULL && p2->nType == TSDB_DATA_TYPE_NULL) { return 0; } @@ -309,39 +308,39 @@ int32_t taosVariantCompare(const SVariant* p1, const SVariant* p2) { if (p1->nLen == p2->nLen) { return memcmp(p1->pz, p2->pz, p1->nLen); } else { - return p1->nLen > p2->nLen? 1:-1; + return p1->nLen > p2->nLen ? 1 : -1; } } else if (p1->nType == TSDB_DATA_TYPE_FLOAT || p1->nType == TSDB_DATA_TYPE_DOUBLE) { if (p1->d == p2->d) { return 0; } else { - return p1->d > p2->d? 1:-1; + return p1->d > p2->d ? 1 : -1; } } else if (IS_UNSIGNED_NUMERIC_TYPE(p1->nType)) { if (p1->u == p2->u) { return 0; } else { - return p1->u > p2->u? 1:-1; + return p1->u > p2->u ? 1 : -1; } } else { if (p1->i == p2->i) { return 0; } else { - return p1->i > p2->i? 1:-1; + return p1->i > p2->i ? 1 : -1; } } } int32_t taosVariantToString(SVariant *pVar, char *dst) { if (pVar == NULL || dst == NULL) return 0; - + switch (pVar->nType) { case TSDB_DATA_TYPE_BINARY: { int32_t len = sprintf(dst, "\'%s\'", pVar->pz); assert(len <= pVar->nLen + sizeof("\'") * 2); // two more chars return len; } - + case TSDB_DATA_TYPE_NCHAR: { dst[0] = '\''; taosUcs4ToMbs(pVar->wpz, (twcslen(pVar->wpz) + 1) * TSDB_NCHAR_SIZE, dst + 1); @@ -350,7 +349,7 @@ int32_t taosVariantToString(SVariant *pVar, char *dst) { dst[len + 1] = 0; return len + 1; } - + case TSDB_DATA_TYPE_BOOL: case TSDB_DATA_TYPE_TINYINT: case TSDB_DATA_TYPE_SMALLINT: @@ -359,7 +358,7 @@ int32_t taosVariantToString(SVariant *pVar, char *dst) { case TSDB_DATA_TYPE_USMALLINT: case TSDB_DATA_TYPE_UINT: return sprintf(dst, "%d", (int32_t)pVar->i); - + case TSDB_DATA_TYPE_BIGINT: return sprintf(dst, "%" PRId64, pVar->i); case TSDB_DATA_TYPE_UBIGINT: @@ -367,7 +366,7 @@ int32_t taosVariantToString(SVariant *pVar, char *dst) { case TSDB_DATA_TYPE_FLOAT: case TSDB_DATA_TYPE_DOUBLE: return sprintf(dst, "%.9lf", pVar->d); - + default: return 0; } @@ -399,27 +398,27 @@ static FORCE_INLINE int32_t wcsconvertToBoolImpl(wchar_t *pstr, int32_t len) { static int32_t toBinary(SVariant *pVariant, char **pDest, int32_t *pDestSize) { const int32_t INITIAL_ALLOC_SIZE = 40; - char * pBuf = NULL; + char *pBuf = NULL; // it is a in-place convert type for SVariant, local buffer is needed if (*pDest == pVariant->pz) { pBuf = calloc(1, INITIAL_ALLOC_SIZE); } - + if (pVariant->nType == TSDB_DATA_TYPE_NCHAR) { size_t newSize = pVariant->nLen * TSDB_NCHAR_SIZE; if (pBuf != NULL) { if (newSize >= INITIAL_ALLOC_SIZE) { pBuf = realloc(pBuf, newSize + 1); } - + taosUcs4ToMbs(pVariant->wpz, (int32_t)newSize, pBuf); free(pVariant->wpz); pBuf[newSize] = 0; } else { taosUcs4ToMbs(pVariant->wpz, (int32_t)newSize, *pDest); } - + } else { if (IS_SIGNED_NUMERIC_TYPE(pVariant->nType)) { sprintf(pBuf == NULL ? *pDest : pBuf, "%" PRId64, pVariant->i); @@ -431,26 +430,26 @@ static int32_t toBinary(SVariant *pVariant, char **pDest, int32_t *pDestSize) { setNull(pBuf == NULL ? *pDest : pBuf, TSDB_DATA_TYPE_BINARY, 0); } } - + if (pBuf != NULL) { *pDest = pBuf; } - + *pDestSize = (int32_t)strlen(*pDest); return 0; } static int32_t toNchar(SVariant *pVariant, char **pDest, int32_t *pDestSize) { char tmpBuf[40] = {0}; - - char * pDst = tmpBuf; + + char *pDst = tmpBuf; int32_t nLen = 0; // convert the number to string, than convert it to wchar string. if (IS_SIGNED_NUMERIC_TYPE(pVariant->nType)) { nLen = sprintf(pDst, "%" PRId64, pVariant->i); } else if (IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) { - nLen = sprintf(pDst, "%"PRIu64, pVariant->u); + nLen = sprintf(pDst, "%" PRIu64, pVariant->u); } else if (pVariant->nType == TSDB_DATA_TYPE_DOUBLE || pVariant->nType == TSDB_DATA_TYPE_FLOAT) { nLen = sprintf(pDst, "%lf", pVariant->d); } else if (pVariant->nType == TSDB_DATA_TYPE_BINARY) { @@ -459,10 +458,10 @@ static int32_t toNchar(SVariant *pVariant, char **pDest, int32_t *pDestSize) { } else if (pVariant->nType == TSDB_DATA_TYPE_BOOL) { nLen = sprintf(pDst, "%s", (pVariant->i == TSDB_TRUE) ? "TRUE" : "FALSE"); } - + if (*pDest == pVariant->pz) { wchar_t *pWStr = calloc(1, (nLen + 1) * TSDB_NCHAR_SIZE); - bool ret = taosMbsToUcs4(pDst, nLen, (char *)pWStr, (nLen + 1) * TSDB_NCHAR_SIZE, NULL); + bool ret = taosMbsToUcs4(pDst, nLen, (char *)pWStr, (nLen + 1) * TSDB_NCHAR_SIZE, NULL); if (!ret) { tfree(pWStr); return -1; @@ -472,14 +471,14 @@ static int32_t toNchar(SVariant *pVariant, char **pDest, int32_t *pDestSize) { if (pVariant->nType == TSDB_DATA_TYPE_BINARY) { free(pVariant->wpz); } - + pVariant->wpz = pWStr; *pDestSize = twcslen(pVariant->wpz); - + // shrink the allocate memory, no need to check here. - char* tmp = realloc(pVariant->wpz, (*pDestSize + 1)*TSDB_NCHAR_SIZE); + char *tmp = realloc(pVariant->wpz, (*pDestSize + 1) * TSDB_NCHAR_SIZE); assert(tmp != NULL); - + pVariant->wpz = (wchar_t *)tmp; } else { int32_t output = 0; @@ -493,21 +492,22 @@ static int32_t toNchar(SVariant *pVariant, char **pDest, int32_t *pDestSize) { *pDestSize = output; } } - + return 0; } static FORCE_INLINE int32_t convertToDouble(char *pStr, int32_t len, double *value) { -// SToken stoken = {.z = pStr, .n = len}; -// if (TK_ILLEGAL == tGetNumericStringType(&stoken)) { -// return -1; -// } -// -// *value = strtod(pStr, NULL); + // SToken stoken = {.z = pStr, .n = len}; + // if (TK_ILLEGAL == tGetNumericStringType(&stoken)) { + // return -1; + // } + // + // *value = strtod(pStr, NULL); return 0; } -static FORCE_INLINE int32_t convertToInteger(SVariant *pVariant, int64_t *result, int32_t type, bool issigned, bool releaseVariantPtr, bool *converted) { +static FORCE_INLINE int32_t convertToInteger(SVariant *pVariant, int64_t *result, int32_t type, bool issigned, + bool releaseVariantPtr, bool *converted) { if (pVariant->nType == TSDB_DATA_TYPE_NULL) { setNull((char *)result, type, tDataTypes[type].bytes); return 0; @@ -645,7 +645,7 @@ static int32_t convertToBool(SVariant *pVariant, int64_t *pDest) { if ((ret = convertToBoolImpl(pVariant->pz, pVariant->nLen)) < 0) { return ret; } - + *pDest = ret; } else if (pVariant->nType == TSDB_DATA_TYPE_NCHAR) { int32_t ret = 0; @@ -656,7 +656,7 @@ static int32_t convertToBool(SVariant *pVariant, int64_t *pDest) { } else if (pVariant->nType == TSDB_DATA_TYPE_NULL) { *pDest = TSDB_DATA_BOOL_NULL; } - + assert(*pDest == TSDB_TRUE || *pDest == TSDB_FALSE || *pDest == TSDB_DATA_BOOL_NULL); return 0; } @@ -665,11 +665,12 @@ static int32_t convertToBool(SVariant *pVariant, int64_t *pDest) { * transfer data from variant serve as the implicit data conversion: from input sql string pVariant->nType * to column type defined in schema */ -int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool includeLengthPrefix, bool *converted, char *extInfo) { +int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool includeLengthPrefix, bool *converted, + char *extInfo) { if (converted) { *converted = false; } - + if (pVariant == NULL || (pVariant->nType != 0 && !isValidDataType(pVariant->nType))) { return -1; } @@ -686,25 +687,25 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc *(int8_t *)payload = (int8_t)result; break; } - + case TSDB_DATA_TYPE_TINYINT: { if (convertToInteger(pVariant, &result, type, true, false, converted) < 0) { SET_EXT_INFO(converted, result, INT8_MIN + 1, INT8_MAX, extInfo); return -1; } - *((int8_t *)payload) = (int8_t) result; + *((int8_t *)payload) = (int8_t)result; break; } case TSDB_DATA_TYPE_UTINYINT: { - if (convertToInteger(pVariant, &result, type, false, false, converted) < 0) { + if (convertToInteger(pVariant, &result, type, false, false, converted) < 0) { SET_EXT_INFO(converted, result, 0, UINT8_MAX - 1, extInfo); return -1; } - *((uint8_t *)payload) = (uint8_t) result; + *((uint8_t *)payload) = (uint8_t)result; break; } - + case TSDB_DATA_TYPE_SMALLINT: { if (convertToInteger(pVariant, &result, type, true, false, converted) < 0) { SET_EXT_INFO(converted, result, INT16_MIN + 1, INT16_MAX, extInfo); @@ -722,7 +723,7 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc *((uint16_t *)payload) = (uint16_t)result; break; } - + case TSDB_DATA_TYPE_INT: { if (convertToInteger(pVariant, &result, type, true, false, converted) < 0) { SET_EXT_INFO(converted, result, INT32_MIN + 1, INT32_MAX, extInfo); @@ -740,7 +741,7 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc *((uint32_t *)payload) = (uint32_t)result; break; } - + case TSDB_DATA_TYPE_BIGINT: { if (convertToInteger(pVariant, &result, type, true, false, converted) < 0) { SET_EXT_INFO(converted, (int64_t)result, INT64_MIN + 1, INT64_MAX, extInfo); @@ -775,19 +776,20 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc if (converted) { *converted = true; } - + if (value > FLT_MAX || value < -FLT_MAX) { SET_EXT_INFO(converted, value, -FLT_MAX, FLT_MAX, extInfo); return -1; } SET_FLOAT_VAL(payload, value); } - } else if (pVariant->nType == TSDB_DATA_TYPE_BOOL || IS_SIGNED_NUMERIC_TYPE(pVariant->nType) || IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) { + } else if (pVariant->nType == TSDB_DATA_TYPE_BOOL || IS_SIGNED_NUMERIC_TYPE(pVariant->nType) || + IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) { if (converted) { *converted = true; } - - if (pVariant->i > FLT_MAX || pVariant->i < -FLT_MAX) { + + if (pVariant->i > FLT_MAX || pVariant->i < -FLT_MAX) { SET_EXT_INFO(converted, pVariant->i, -FLT_MAX, FLT_MAX, extInfo); return -1; } @@ -797,12 +799,12 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc if (converted) { *converted = true; } - - if (pVariant->d > FLT_MAX || pVariant->d < -FLT_MAX) { + + if (pVariant->d > FLT_MAX || pVariant->d < -FLT_MAX) { SET_EXT_INFO(converted, pVariant->d, -FLT_MAX, FLT_MAX, extInfo); return -1; } - + SET_FLOAT_VAL(payload, pVariant->d); } else if (pVariant->nType == TSDB_DATA_TYPE_NULL) { *((uint32_t *)payload) = TSDB_DATA_FLOAT_NULL; @@ -831,7 +833,8 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc SET_DOUBLE_VAL(payload, value); } - } else if (pVariant->nType == TSDB_DATA_TYPE_BOOL || IS_SIGNED_NUMERIC_TYPE(pVariant->nType) || IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) { + } else if (pVariant->nType == TSDB_DATA_TYPE_BOOL || IS_SIGNED_NUMERIC_TYPE(pVariant->nType) || + IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) { SET_DOUBLE_VAL(payload, pVariant->i); } else if (IS_FLOAT_TYPE(pVariant->nType)) { SET_DOUBLE_VAL(payload, pVariant->d); @@ -847,11 +850,11 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc break; } - + case TSDB_DATA_TYPE_BINARY: { if (!includeLengthPrefix) { if (pVariant->nType == TSDB_DATA_TYPE_NULL) { - *(uint8_t*) payload = TSDB_DATA_BINARY_NULL; + *(uint8_t *)payload = TSDB_DATA_BINARY_NULL; } else { if (pVariant->nType != TSDB_DATA_TYPE_BINARY) { toBinary(pVariant, &payload, &pVariant->nLen); @@ -918,11 +921,11 @@ int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool inc assert(p == varDataVal(payload)); } } - + break; } } - + return 0; } @@ -944,13 +947,13 @@ int32_t taosVariantTypeSetType(SVariant *pVariant, char type) { if (pVariant == NULL || pVariant->nType == 0) { // value is not set return 0; } - + switch (type) { case TSDB_DATA_TYPE_BOOL: { // bool if (convertToBool(pVariant, &pVariant->i) < 0) { return -1; } - + pVariant->nType = type; break; } @@ -971,7 +974,7 @@ int32_t taosVariantTypeSetType(SVariant *pVariant, char type) { free(pVariant->pz); return -1; } - + free(pVariant->pz); pVariant->d = v; } else if (pVariant->nType == TSDB_DATA_TYPE_NCHAR) { @@ -981,14 +984,14 @@ int32_t taosVariantTypeSetType(SVariant *pVariant, char type) { free(pVariant->pz); return -1; } - + free(pVariant->pz); pVariant->d = v; } else if (pVariant->nType >= TSDB_DATA_TYPE_BOOL && pVariant->nType <= TSDB_DATA_TYPE_BIGINT) { - double tmp = (double) pVariant->i; + double tmp = (double)pVariant->i; pVariant->d = tmp; } - + pVariant->nType = TSDB_DATA_TYPE_DOUBLE; break; } @@ -1009,6 +1012,6 @@ int32_t taosVariantTypeSetType(SVariant *pVariant, char type) { break; } } - + return 0; } \ No newline at end of file diff --git a/source/dnode/mnode/impl/src/mndTelem.c b/source/dnode/mnode/impl/src/mndTelem.c index d6a4c76c6220b8b0cc796a48497ea45141c5a4d1..1cd7593846716f54b7cbb491dc7e207280821c80 100644 --- a/source/dnode/mnode/impl/src/mndTelem.c +++ b/source/dnode/mnode/impl/src/mndTelem.c @@ -18,227 +18,68 @@ #include "mndCluster.h" #include "mndSync.h" #include "tbuffer.h" -#include "tversion.h" +#include "tjson.h" +#include "thttp.h" #define TELEMETRY_SERVER "telemetry.taosdata.com" #define TELEMETRY_PORT 80 -#define REPORT_INTERVAL 86400 -static void mndBeginObject(SBufferWriter* bw) { tbufWriteChar(bw, '{'); } - -static void mndCloseObject(SBufferWriter* bw) { - size_t len = tbufTell(bw); - if (tbufGetData(bw, false)[len - 1] == ',') { - tbufWriteCharAt(bw, len - 1, '}'); - } else { - tbufWriteChar(bw, '}'); - } -} - -static void mndWriteString(SBufferWriter* bw, const char* str) { - tbufWriteChar(bw, '"'); - tbufWrite(bw, str, strlen(str)); - tbufWriteChar(bw, '"'); -} - -static void mndAddIntField(SBufferWriter* bw, const char* k, int64_t v) { - mndWriteString(bw, k); - tbufWriteChar(bw, ':'); - char buf[32] = {0}; - sprintf(buf, "%" PRId64, v); - tbufWrite(bw, buf, strlen(buf)); - tbufWriteChar(bw, ','); -} - -static void mndAddStringField(SBufferWriter* bw, const char* k, const char* v) { - mndWriteString(bw, k); - tbufWriteChar(bw, ':'); - mndWriteString(bw, v); - tbufWriteChar(bw, ','); -} - -static void mndAddCpuInfo(SMnode* pMnode, SBufferWriter* bw) { - char* line = NULL; - size_t size = 0; - int32_t done = 0; - - // FILE* fp = fopen("/proc/cpuinfo", "r"); - TdFilePtr pFile = taosOpenFile("/proc/cpuinfo", TD_FILE_READ | TD_FILE_STREAM); - if (pFile == NULL) { - return; - } - - while (done != 3 && (size = taosGetLineFile(pFile, &line)) != -1) { - line[size - 1] = '\0'; - if (((done & 1) == 0) && strncmp(line, "model name", 10) == 0) { - const char* v = strchr(line, ':') + 2; - mndAddStringField(bw, "cpuModel", v); - done |= 1; - } else if (((done & 2) == 0) && strncmp(line, "cpu cores", 9) == 0) { - const char* v = strchr(line, ':') + 2; - mndWriteString(bw, "numOfCpu"); - tbufWriteChar(bw, ':'); - tbufWrite(bw, v, strlen(v)); - tbufWriteChar(bw, ','); - done |= 2; - } - } - - if(line != NULL) free(line); - taosCloseFile(&pFile); -} - -static void mndAddOsInfo(SMnode* pMnode, SBufferWriter* bw) { - char* line = NULL; - size_t size = 0; - - // FILE* fp = fopen("/etc/os-release", "r"); - TdFilePtr pFile = taosOpenFile("/etc/os-release", TD_FILE_READ | TD_FILE_STREAM); - if (pFile == NULL) { - return; - } - - while ((size = taosGetLineFile(pFile, &line)) != -1) { - line[size - 1] = '\0'; - if (strncmp(line, "PRETTY_NAME", 11) == 0) { - const char* p = strchr(line, '=') + 1; - if (*p == '"') { - p++; - line[size - 2] = 0; - } - mndAddStringField(bw, "os", p); - break; - } - } - - if(line != NULL) free(line); - taosCloseFile(&pFile); -} - -static void mndAddMemoryInfo(SMnode* pMnode, SBufferWriter* bw) { - char* line = NULL; - size_t size = 0; - - // FILE* fp = fopen("/proc/meminfo", "r"); - TdFilePtr pFile = taosOpenFile("/proc/meminfo", TD_FILE_READ | TD_FILE_STREAM); - if (pFile == NULL) { - return; - } - - 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++; - mndAddStringField(bw, "memory", p); - break; - } - } - - if(line != NULL) free(line); - taosCloseFile(&pFile); +static void mndBuildRuntimeInfo(SMnode* pMnode, SJson* pJson) { + SMnodeLoad load = {0}; + if (mndGetLoad(pMnode, &load) != 0) return; + + tjsonAddDoubleToObject(pJson, "numOfDnode", load.numOfDnode); + tjsonAddDoubleToObject(pJson, "numOfMnode", load.numOfMnode); + tjsonAddDoubleToObject(pJson, "numOfVgroup", load.numOfVgroup); + tjsonAddDoubleToObject(pJson, "numOfDatabase", load.numOfDatabase); + tjsonAddDoubleToObject(pJson, "numOfSuperTable", load.numOfSuperTable); + tjsonAddDoubleToObject(pJson, "numOfChildTable", load.numOfChildTable); + tjsonAddDoubleToObject(pJson, "numOfColumn", load.numOfColumn); + tjsonAddDoubleToObject(pJson, "numOfPoint", load.totalPoints); + tjsonAddDoubleToObject(pJson, "totalStorage", load.totalStorage); + tjsonAddDoubleToObject(pJson, "compStorage", load.compStorage); } -static void mndAddVersionInfo(SMnode* pMnode, SBufferWriter* bw) { +static char* mndBuildTelemetryReport(SMnode* pMnode) { + char tmp[4096] = {0}; STelemMgmt* pMgmt = &pMnode->telemMgmt; - mndAddStringField(bw, "version", version); - mndAddStringField(bw, "buildInfo", buildinfo); - mndAddStringField(bw, "gitInfo", gitinfo); - mndAddStringField(bw, "email", pMgmt->email); -} - -static void mndAddRuntimeInfo(SMnode* pMnode, SBufferWriter* bw) { - SMnodeLoad load = {0}; - if (mndGetLoad(pMnode, &load) != 0) { - return; - } - mndAddIntField(bw, "numOfDnode", load.numOfDnode); - mndAddIntField(bw, "numOfMnode", load.numOfMnode); - mndAddIntField(bw, "numOfVgroup", load.numOfVgroup); - mndAddIntField(bw, "numOfDatabase", load.numOfDatabase); - mndAddIntField(bw, "numOfSuperTable", load.numOfSuperTable); - mndAddIntField(bw, "numOfChildTable", load.numOfChildTable); - mndAddIntField(bw, "numOfColumn", load.numOfColumn); - mndAddIntField(bw, "numOfPoint", load.totalPoints); - mndAddIntField(bw, "totalStorage", load.totalStorage); - mndAddIntField(bw, "compStorage", load.compStorage); -} - -static void mndSendTelemetryReport(SMnode* pMnode) { - STelemMgmt* pMgmt = &pMnode->telemMgmt; - SBufferWriter bw = tbufInitWriter(NULL, false); - int32_t code = -1; - char buf[128] = {0}; - SOCKET fd = 0; + SJson* pJson = tjsonCreateObject(); + if (pJson == NULL) return NULL; char clusterName[64] = {0}; - if (mndGetClusterName(pMnode, clusterName, sizeof(clusterName)) != 0) { - goto SEND_OVER; - } - - mndBeginObject(&bw); - mndAddStringField(&bw, "instanceId", clusterName); - mndAddIntField(&bw, "reportVersion", 1); - mndAddOsInfo(pMnode, &bw); - mndAddCpuInfo(pMnode, &bw); - mndAddMemoryInfo(pMnode, &bw); - mndAddVersionInfo(pMnode, &bw); - mndAddRuntimeInfo(pMnode, &bw); - mndCloseObject(&bw); + mndGetClusterName(pMnode, clusterName, sizeof(clusterName)); + tjsonAddStringToObject(pJson, "instanceId", clusterName); + tjsonAddDoubleToObject(pJson, "reportVersion", 1); - uint32_t ip = taosGetIpv4FromFqdn(TELEMETRY_SERVER); - if (ip == 0xffffffff) { - terrno = TAOS_SYSTEM_ERROR(errno); - mError("failed to get ip of %s since :%s", TELEMETRY_SERVER, terrstr()); - goto SEND_OVER; + if (taosGetOsReleaseName(tmp, sizeof(tmp))) { + tjsonAddStringToObject(pJson, "os", tmp); } - fd = taosOpenTcpClientSocket(ip, TELEMETRY_PORT, 0); - if (fd < 0) { - terrno = TAOS_SYSTEM_ERROR(errno); - mError("failed to create socket to %s:%d since:%s", TELEMETRY_SERVER, TELEMETRY_PORT, terrstr()); - goto SEND_OVER; - } - - const char* header = - "POST /report HTTP/1.1\n" - "Host: " TELEMETRY_SERVER - "\n" - "Content-Type: application/json\n" - "Content-Length: "; - if (taosWriteSocket(fd, (void*)header, (int32_t)strlen(header)) < 0) { - goto SEND_OVER; - } - - int32_t contLen = (int32_t)(tbufTell(&bw)); - sprintf(buf, "%d\n\n", contLen); - if (taosWriteSocket(fd, buf, (int32_t)strlen(buf)) < 0) { - goto SEND_OVER; - } - - const char* pCont = tbufGetData(&bw, false); - if (taosWriteSocket(fd, (void*)pCont, contLen) < 0) { - goto SEND_OVER; + int32_t numOfCores = 0; + if (taosGetCpuInfo(tmp, sizeof(tmp), &numOfCores)) { + tjsonAddStringToObject(pJson, "cpuModel", tmp); + tjsonAddDoubleToObject(pJson, "numOfCpu", numOfCores); + } else { + tjsonAddDoubleToObject(pJson, "numOfCpu", taosGetCpuCores()); } - // read something to avoid nginx error 499 - if (taosReadSocket(fd, buf, 10) < 0) { - terrno = TAOS_SYSTEM_ERROR(errno); - mError("failed to receive response since %s", terrstr()); - goto SEND_OVER; + uint64_t memoryKB = 0; + if (taosGetTotalSysMemoryKB(&memoryKB)) { + snprintf(tmp, sizeof(tmp), "%" PRIu64 " kB", memoryKB); + tjsonAddStringToObject(pJson, "memory", tmp); } - mInfo("send telemetry to %s:%d, len:%d content: %s", TELEMETRY_SERVER, TELEMETRY_PORT, contLen, pCont); - code = 0; + tjsonAddStringToObject(pJson, "version", version); + tjsonAddStringToObject(pJson, "buildInfo", buildinfo); + tjsonAddStringToObject(pJson, "gitInfo", gitinfo); + tjsonAddStringToObject(pJson, "email", pMgmt->email); -SEND_OVER: - tbufCloseWriter(&bw); - taosCloseSocket(fd); + mndBuildRuntimeInfo(pMnode, pJson); - if (code != 0) { - mError("failed to send telemetry to %s:%d since %s", TELEMETRY_SERVER, TELEMETRY_PORT, terrstr()); - } + char* pCont = tjsonToString(pJson); + tjsonDelete(pJson); + return pCont; } static int32_t mndProcessTelemTimer(SMnodeMsg* pReq) { @@ -247,33 +88,23 @@ static int32_t mndProcessTelemTimer(SMnodeMsg* pReq) { if (!pMgmt->enable) return 0; taosWLockLatch(&pMgmt->lock); - mndSendTelemetryReport(pMnode); + char* pCont = mndBuildTelemetryReport(pMnode); + if (pCont != NULL) { + taosSendHttpReport(TELEMETRY_SERVER, TELEMETRY_PORT, pCont, strlen(pCont)); + free(pCont); + } taosWUnLockLatch(&pMgmt->lock); return 0; } -static void mndGetEmail(SMnode* pMnode, char* filepath) { - STelemMgmt* pMgmt = &pMnode->telemMgmt; - - TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ); - if (pFile == NULL) { - return; - } - - if (taosReadFile(pFile, (void*)pMgmt->email, TSDB_FQDN_LEN) < 0) { - mError("failed to read %d bytes from file %s since %s", TSDB_FQDN_LEN, filepath, strerror(errno)); - } - - taosCloseFile(&pFile); -} - int32_t mndInitTelem(SMnode* pMnode) { STelemMgmt* pMgmt = &pMnode->telemMgmt; - pMgmt->enable = tsEnableTelemetryReporting; - taosInitRWLatch(&pMgmt->lock); - mndGetEmail(pMnode, "/usr/local/taos/email"); + taosInitRWLatch(&pMgmt->lock); + pMgmt->enable = tsEnableTelemetryReporting; + taosGetEmail(pMgmt->email, sizeof(pMgmt->email)); mndSetMsgHandle(pMnode, TDMT_MND_TELEM_TIMER, mndProcessTelemTimer); + mDebug("mnode telemetry is initialized"); return 0; } diff --git a/source/os/src/osDir.c b/source/os/src/osDir.c index c464073e5faeb6a9a08da886268f469189b2fbf1..91ef97e66be08df3f11e8ea44938005854ff8f96 100644 --- a/source/os/src/osDir.c +++ b/source/os/src/osDir.c @@ -140,4 +140,13 @@ int32_t taosRealPath(char *dirname, int32_t maxlen) { return 0; } +bool taosIsDir(const char *dirname) { + DIR *dir = opendir(dirname); + if (dir != NULL) { + closedir(dir); + return true; + } + return false; +} + #endif diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index 136afb9a1558f30a6192b2b02ac946652d4df2ad..c2e9b52f883818defbb72a06cc5fa8f559858eed 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -15,8 +15,6 @@ #define ALLOW_FORBID_FUNC #include "os.h" -#define MAX_FPRINTFLINE_BUFFER_SIZE (1000) - #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) #include @@ -46,10 +44,15 @@ extern int openU(const char *, int, ...); /* MsvcLibX UTF-8 version of open */ typedef int32_t FileFd; +#define FILE_WITH_LOCK 1 + typedef struct TdFile { - int refId; - FileFd fd; - FILE *fp; +#if FILE_WITH_LOCK + pthread_rwlock_t rwlock; +#endif + int refId; + FileFd fd; + FILE *fp; } * TdFilePtr, TdFile; void taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, char *dstPath) { @@ -238,6 +241,9 @@ TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) { if (fp != NULL) fclose(fp); return NULL; } +#if FILE_WITH_LOCK + pthread_rwlock_init(&(pFile->rwlock),NULL); +#endif pFile->fd = fd; pFile->fp = fp; pFile->refId = 0; @@ -249,6 +255,12 @@ int64_t taosCloseFile(TdFilePtr *ppFile) { #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) return 0; #else + if (ppFile == NULL || *ppFile == NULL) { + return 0; + } +#if FILE_WITH_LOCK + pthread_rwlock_wrlock(&((*ppFile)->rwlock)); +#endif if (ppFile == NULL || *ppFile == NULL || (*ppFile)->fd == -1) { return 0; } @@ -263,6 +275,10 @@ int64_t taosCloseFile(TdFilePtr *ppFile) { (*ppFile)->fd = -1; } (*ppFile)->refId = 0; +#if FILE_WITH_LOCK + pthread_rwlock_unlock(&((*ppFile)->rwlock)); + pthread_rwlock_destroy(&((*ppFile)->rwlock)); +#endif free(*ppFile); *ppFile = NULL; return 0; @@ -273,6 +289,9 @@ int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) { if (pFile == NULL) { return 0; } +#if FILE_WITH_LOCK + pthread_rwlock_rdlock(&(pFile->rwlock)); +#endif assert(pFile->fd >= 0); int64_t leftbytes = count; int64_t readbytes; @@ -284,9 +303,15 @@ int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) { if (errno == EINTR) { continue; } else { +#if FILE_WITH_LOCK + pthread_rwlock_unlock(&(pFile->rwlock)); +#endif return -1; } } else if (readbytes == 0) { +#if FILE_WITH_LOCK + pthread_rwlock_unlock(&(pFile->rwlock)); +#endif return (int64_t)(count - leftbytes); } @@ -294,6 +319,9 @@ int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) { tbuf += readbytes; } +#if FILE_WITH_LOCK + pthread_rwlock_unlock(&(pFile->rwlock)); +#endif return count; } @@ -301,14 +329,24 @@ int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset) if (pFile == NULL) { return 0; } +#if FILE_WITH_LOCK + pthread_rwlock_rdlock(&(pFile->rwlock)); +#endif assert(pFile->fd >= 0); - return pread(pFile->fd, buf, count, offset); + int64_t ret = pread(pFile->fd, buf, count, offset); +#if FILE_WITH_LOCK + pthread_rwlock_unlock(&(pFile->rwlock)); +#endif + return ret; } int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) { if (pFile == NULL) { return 0; } +#if FILE_WITH_LOCK + pthread_rwlock_wrlock(&(pFile->rwlock)); +#endif assert(pFile->fd >= 0); int64_t nleft = count; @@ -321,12 +359,18 @@ int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) { if (errno == EINTR) { continue; } +#if FILE_WITH_LOCK + pthread_rwlock_unlock(&(pFile->rwlock)); +#endif return -1; } nleft -= nwritten; tbuf += nwritten; } - fsync(pFile->fd); + +#if FILE_WITH_LOCK + pthread_rwlock_unlock(&(pFile->rwlock)); +#endif return count; } @@ -334,8 +378,15 @@ int64_t taosLSeekFile(TdFilePtr pFile, int64_t offset, int32_t whence) { if (pFile == NULL) { return 0; } +#if FILE_WITH_LOCK + pthread_rwlock_rdlock(&(pFile->rwlock)); +#endif assert(pFile->fd >= 0); - return (int64_t)lseek(pFile->fd, (long)offset, whence); + int64_t ret = lseek(pFile->fd, (long)offset, whence); +#if FILE_WITH_LOCK + pthread_rwlock_unlock(&(pFile->rwlock)); +#endif + return ret; } int32_t taosFStatFile(TdFilePtr pFile, int64_t *size, int32_t *mtime) { @@ -637,7 +688,6 @@ void taosFprintfFile(TdFilePtr pFile, const char *format, ...) { } assert(pFile->fp != NULL); - char buffer[MAX_FPRINTFLINE_BUFFER_SIZE] = {0}; va_list ap; va_start(ap, format); vfprintf(pFile->fp, format, ap); diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index cf9c557f5ef5b36a23de42adf9b1a54e75a27f4c..163fad803fc11487101c3d106fe69343b4154c47 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -888,4 +888,99 @@ SysNameInfo taosGetSysNameInfo() { return info; } -#endif \ No newline at end of file +bool taosGetEmail(char *email, int32_t maxLen) { + const char *filepath = "/usr/local/taos/email"; + + TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ); + if (pFile == NULL) return false; + + if (taosReadFile(pFile, (void *)email, maxLen) < 0) { + taosCloseFile(&pFile); + return false; + } + + taosCloseFile(&pFile); + return true; +} + +bool taosGetOsReleaseName(char *releaseName, int32_t maxLen) { + char *line = NULL; + size_t size = 0; + bool ret = false; + + TdFilePtr pFile = taosOpenFile("/etc/os-release", TD_FILE_READ | TD_FILE_STREAM); + if (pFile == NULL) return false; + + while ((size = taosGetLineFile(pFile, &line)) != -1) { + line[size - 1] = '\0'; + if (strncmp(line, "PRETTY_NAME", 11) == 0) { + const char *p = strchr(line, '=') + 1; + if (*p == '"') { + p++; + line[size - 2] = 0; + } + tstrncpy(releaseName, p, maxLen); + ret = true; + break; + } + } + + if (line != NULL) free(line); + taosCloseFile(&pFile); + return ret; +} + +bool taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores) { + char *line = NULL; + size_t size = 0; + int32_t done = 0; + bool ret = false; + + TdFilePtr pFile = taosOpenFile("/proc/cpuinfo", TD_FILE_READ | TD_FILE_STREAM); + if (pFile == NULL) return false; + + while (done != 3 && (size = taosGetLineFile(pFile, &line)) != -1) { + line[size - 1] = '\0'; + if (((done & 1) == 0) && strncmp(line, "model name", 10) == 0) { + const char *v = strchr(line, ':') + 2; + tstrncpy(cpuModel, v, maxLen); + ret = true; + done |= 1; + } else if (((done & 2) == 0) && strncmp(line, "cpu cores", 9) == 0) { + const char *v = strchr(line, ':') + 2; + *numOfCores = atoi(v); + done |= 2; + } + } + + if (line != NULL) free(line); + taosCloseFile(&pFile); + + return ret; +} + +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 diff --git a/source/util/src/tconfig.c b/source/util/src/tconfig.c index 8330c10ec703b7c2484167ff5cba7d46b9eabe85..0f22d15cd46c1269379545f60f745cc80af0d131 100644 --- a/source/util/src/tconfig.c +++ b/source/util/src/tconfig.c @@ -60,7 +60,7 @@ int32_t cfgLoad(SConfig *pCfg, ECfgSrcType cfgType, const char *sourceStr) { } } -int32_t cfgLoadArray(SConfig *pCfg, SArray *pArgs) { +int32_t cfgLoadFromArray(SConfig *pCfg, SArray *pArgs) { int32_t size = taosArrayGetSize(pArgs); for (int32_t i = 0; i < size; ++i) { SConfigPair *pPair = taosArrayGet(pArgs, i); @@ -145,10 +145,12 @@ static int32_t cfgCheckAndSetDir(SConfigItem *pItem, const char *inputDir) { return -1; } +#if 0 if (taosMkDir(fullDir) != 0) { uError("failed to create dir:%s realpath:%s since %s", inputDir, fullDir, terrstr()); return -1; } +#endif cfgFreeItem(pItem); pItem->str = strdup(fullDir); @@ -608,7 +610,10 @@ int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath) { int32_t olen, vlen, vlen2, vlen3; ssize_t _bytes = 0; - // FILE *fp = fopen(filepath, "r"); + if (taosIsDir(filepath)) { + return -1; + } + TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { terrno = TAOS_SYSTEM_ERROR(errno); diff --git a/source/util/src/thttp.c b/source/util/src/thttp.c new file mode 100644 index 0000000000000000000000000000000000000000..14c39d3f03f6a59e921de174abe4425bec6f7441 --- /dev/null +++ b/source/util/src/thttp.c @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#define _DEFAULT_SOURCE +#include "thttp.h" +#include "taoserror.h" +#include "tlog.h" + +int32_t taosSendHttpReport(const char* server, uint16_t port, const char* pCont, int32_t contLen) { + int32_t code = -1; + SOCKET fd = 0; + + uint32_t ip = taosGetIpv4FromFqdn(server); + if (ip == 0xffffffff) { + terrno = TAOS_SYSTEM_ERROR(errno); + uError("failed to get http server:%s ip since %s", server, terrstr()); + goto SEND_OVER; + } + + fd = taosOpenTcpClientSocket(ip, port, 0); + if (fd < 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + uError("failed to create http socket since %s", terrstr()); + goto SEND_OVER; + } + + char header[4096] = {0}; + int32_t headLen = snprintf(header, sizeof(header), + "POST /report HTTP/1.1\n" + "Host: %s\n" + "Content-Type: application/json\n" + "Content-Length: %d\n\n", + server, contLen); + + if (taosWriteSocket(fd, (void*)header, headLen) < 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + uError("failed to send http header since %s", terrstr()); + goto SEND_OVER; + } + + if (taosWriteSocket(fd, (void*)pCont, contLen) < 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + uError("failed to send http content since %s", terrstr()); + goto SEND_OVER; + } + + // read something to avoid nginx error 499 + if (taosReadSocket(fd, header, 10) < 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + uError("failed to receive response since %s", terrstr()); + goto SEND_OVER; + } + + uInfo("send http to %s:%d, len:%d content: %s", server, port, contLen, pCont); + code = 0; + +SEND_OVER: + if (fd != 0) { + taosCloseSocket(fd); + } + + return code; +} diff --git a/source/util/src/tjson.c b/source/util/src/tjson.c index 4b68467450a1e2f8b1313ca81953e295eb752483..2c117771b12b5236c5599600a20c1eca3004a2c5 100644 --- a/source/util/src/tjson.c +++ b/source/util/src/tjson.c @@ -18,36 +18,73 @@ #include "cJSON.h" #include "taoserror.h" -SJson* tjsonCreateObject() { return cJSON_CreateObject(); } +SJson* tjsonCreateObject() { + SJson* pJson = cJSON_CreateObject(); + if (pJson == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + } + return pJson; +} void tjsonDelete(SJson* pJson) { cJSON_Delete((cJSON*)pJson); } int32_t tjsonAddIntegerToObject(SJson* pJson, const char* pName, const uint64_t number) { char tmp[40] = {0}; - snprintf(tmp, tListLen(tmp), "%" PRId64, number); + snprintf(tmp, sizeof(tmp), "%" PRId64, number); return tjsonAddStringToObject(pJson, pName, tmp); } int32_t tjsonAddDoubleToObject(SJson* pJson, const char* pName, const double number) { - return (NULL == cJSON_AddNumberToObject((cJSON*)pJson, pName, number) ? TSDB_CODE_FAILED : TSDB_CODE_SUCCESS); + if (NULL == cJSON_AddNumberToObject((cJSON*)pJson, pName, number)) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return TSDB_CODE_FAILED; + } + + return TSDB_CODE_SUCCESS; } int32_t tjsonAddBoolToObject(SJson* pJson, const char* pName, const bool boolean) { - return (NULL == cJSON_AddBoolToObject((cJSON*)pJson, pName, boolean) ? TSDB_CODE_FAILED : TSDB_CODE_SUCCESS); + if (NULL == cJSON_AddBoolToObject((cJSON*)pJson, pName, boolean)) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return TSDB_CODE_FAILED; + } + + return TSDB_CODE_SUCCESS; } int32_t tjsonAddStringToObject(SJson* pJson, const char* pName, const char* pVal) { - return (NULL == cJSON_AddStringToObject((cJSON*)pJson, pName, pVal) ? TSDB_CODE_FAILED : TSDB_CODE_SUCCESS); + if (NULL == cJSON_AddStringToObject((cJSON*)pJson, pName, pVal)) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return TSDB_CODE_FAILED; + } + + return TSDB_CODE_SUCCESS; } -SJson* tjsonAddArrayToObject(SJson* pJson, const char* pName) { return cJSON_AddArrayToObject((cJSON*)pJson, pName); } +SJson* tjsonAddArrayToObject(SJson* pJson, const char* pName) { + SJson* ret = (SJson*)cJSON_AddArrayToObject((cJSON*)pJson, pName); + if (ret == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + } + return ret; +} int32_t tjsonAddItemToObject(SJson* pJson, const char* pName, SJson* pItem) { - return (cJSON_AddItemToObject((cJSON*)pJson, pName, pItem) ? TSDB_CODE_SUCCESS : TSDB_CODE_FAILED); + if (cJSON_AddItemToObject((cJSON*)pJson, pName, pItem)) { + return TSDB_CODE_SUCCESS; + } + + terrno = TSDB_CODE_OUT_OF_MEMORY; + return TSDB_CODE_FAILED; } int32_t tjsonAddItemToArray(SJson* pJson, SJson* pItem) { - return (cJSON_AddItemToArray((cJSON*)pJson, pItem) ? TSDB_CODE_SUCCESS : TSDB_CODE_FAILED); + if (cJSON_AddItemToArray((cJSON*)pJson, pItem)) { + return TSDB_CODE_SUCCESS; + } + + terrno = TSDB_CODE_OUT_OF_MEMORY; + return TSDB_CODE_FAILED; } int32_t tjsonAddObject(SJson* pJson, const char* pName, FToJson func, const void* pObj) { diff --git a/tests b/tests index 904e6f0e152e8fe61edfe0a0a9ae497cfde2a72c..08ed39f0a5fcbbfb5a630b945ab3d1998d4b4136 160000 --- a/tests +++ b/tests @@ -1 +1 @@ -Subproject commit 904e6f0e152e8fe61edfe0a0a9ae497cfde2a72c +Subproject commit 08ed39f0a5fcbbfb5a630b945ab3d1998d4b4136