diff --git a/CMakeLists.txt b/CMakeLists.txt index dcad242a30..c89195a89a 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,21 +38,56 @@ IF (NOT DEFINED TD_CLUSTER) # Set macro definitions according to os platform SET(TD_LINUX_64 FALSE) SET(TD_LINUX_32 FALSE) + SET(TD_ARM FALSE) SET(TD_ARM_64 FALSE) SET(TD_ARM_32 FALSE) SET(TD_MIPS_64 FALSE) SET(TD_DARWIN_64 FALSE) SET(TD_WINDOWS_64 FALSE) + # if generate ARM version: + # cmake -DARMVER=arm32 .. or cmake -DARMVER=arm64 + IF (${ARMVER} MATCHES "arm32") + SET(TD_ARM TRUE) + SET(TD_ARM_32 TRUE) + ADD_DEFINITIONS(-D_TD_ARM_) + ADD_DEFINITIONS(-D_TD_ARM_32_) + ELSEIF (${ARMVER} MATCHES "arm64") + SET(TD_ARM TRUE) + SET(TD_ARM_64 TRUE) + ADD_DEFINITIONS(-D_TD_ARM_) + ADD_DEFINITIONS(-D_TD_ARM_64_) + ENDIF () + + IF (TD_ARM) + ADD_DEFINITIONS(-D_TD_ARM_) + IF (TD_ARM_32) + ADD_DEFINITIONS(-D_TD_ARM_32_) + ELSEIF (TD_ARM_64) + ADD_DEFINITIONS(-D_TD_ARM_64_) + ELSE () + EXIT () + ENDIF () + ENDIF () + IF (${CMAKE_SYSTEM_NAME} MATCHES "Linux") IF (${CMAKE_SIZEOF_VOID_P} MATCHES 8) SET(TD_LINUX_64 TRUE) SET(TD_OS_DIR ${TD_COMMUNITY_DIR}/src/os/linux) ADD_DEFINITIONS(-D_M_X64) MESSAGE(STATUS "The current platform is Linux 64-bit") - ELSE () + ELSEIF (${CMAKE_SIZEOF_VOID_P} MATCHES 4) + IF (TD_ARM) SET(TD_LINUX_32 TRUE) - MESSAGE(FATAL_ERROR "The current platform is Linux 32-bit, not supported yet") + SET(TD_OS_DIR ${TD_COMMUNITY_DIR}/src/os/linux) + #ADD_DEFINITIONS(-D_M_IX86) + MESSAGE(STATUS "The current platform is Linux 32-bit") + ELSE () + MESSAGE(FATAL_ERROR "The current platform is Linux 32-bit, but no ARM not supported yet") + EXIT () + ENDIF () + ELSE () + MESSAGE(FATAL_ERROR "The current platform is Linux neither 32-bit nor 64-bit, not supported yet") EXIT () ENDIF () ELSEIF (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") @@ -102,13 +137,27 @@ IF (NOT DEFINED TD_CLUSTER) IF (TD_LINUX_64) SET(DEBUG_FLAGS "-O0 -DDEBUG") SET(RELEASE_FLAGS "-O0") - IF (${CMAKE_CXX_COMPILER_ID} MATCHES "Clang") - SET(COMMON_FLAGS "-std=gnu99 -Wall -fPIC -malign-double -g -Wno-char-subscripts -msse4.2 -D_FILE_OFFSET_BITS=64 -D_LARGE_FILE") + IF (NOT TD_ARM) + IF (${CMAKE_CXX_COMPILER_ID} MATCHES "Clang") + SET(COMMON_FLAGS "-std=gnu99 -Wall -fPIC -malign-double -g -Wno-char-subscripts -msse4.2 -D_FILE_OFFSET_BITS=64 -D_LARGE_FILE") + ELSE () + SET(COMMON_FLAGS "-std=gnu99 -Wall -fPIC -malign-double -g -Wno-char-subscripts -malign-stringops -msse4.2 -D_FILE_OFFSET_BITS=64 -D_LARGE_FILE") + ENDIF () ELSE () - SET(COMMON_FLAGS "-std=gnu99 -Wall -fPIC -malign-double -g -Wno-char-subscripts -malign-stringops -msse4.2 -D_FILE_OFFSET_BITS=64 -D_LARGE_FILE") + SET(COMMON_FLAGS "-std=gnu99 -Wall -fPIC -g -Wno-char-subscripts -fsigned-char -munaligned-access -fpack-struct=8 -D_FILE_OFFSET_BITS=64 -D_LARGE_FILE") ENDIF () ADD_DEFINITIONS(-DLINUX) ADD_DEFINITIONS(-D_REENTRANT -D__USE_POSIX -D_LIBC_REENTRANT) + ELSEIF (TD_LINUX_32) + IF (NOT TD_ARM) + EXIT () + ENDIF () + SET(DEBUG_FLAGS "-O0 -DDEBUG") + SET(RELEASE_FLAGS "-O0") + SET(COMMON_FLAGS "-std=gnu99 -Wall -fPIC -g -Wno-char-subscripts -fsigned-char -munaligned-access -fpack-struct=8 -D_FILE_OFFSET_BITS=64 -D_LARGE_FILE") + ADD_DEFINITIONS(-DLINUX) + ADD_DEFINITIONS(-D_REENTRANT -D__USE_POSIX -D_LIBC_REENTRANT) + ADD_DEFINITIONS(-DUSE_LIBICONV) ELSEIF (TD_WINDOWS_64) SET(CMAKE_GENERATOR "NMake Makefiles" CACHE INTERNAL "" FORCE) SET(COMMON_FLAGS "/nologo /WX- /Oi /Oy- /Gm- /EHsc /MT /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Gd /errorReport:prompt /analyze-") @@ -169,6 +218,14 @@ IF (NOT DEFINED TD_CLUSTER) INSTALL(CODE "MESSAGE(\"make install script: ${TD_MAKE_INSTALL_SH}\")") INSTALL(CODE "execute_process(COMMAND chmod 777 ${TD_MAKE_INSTALL_SH})") INSTALL(CODE "execute_process(COMMAND ${TD_MAKE_INSTALL_SH} ${TD_COMMUNITY_DIR} ${PROJECT_BINARY_DIR})") + ELSEIF (TD_LINUX_32) + IF (NOT TD_ARM) + EXIT () + ENDIF () + SET(TD_MAKE_INSTALL_SH "${TD_COMMUNITY_DIR}/packaging/tools/make_install.sh") + INSTALL(CODE "MESSAGE(\"make install script: ${TD_MAKE_INSTALL_SH}\")") + INSTALL(CODE "execute_process(COMMAND chmod 777 ${TD_MAKE_INSTALL_SH})") + INSTALL(CODE "execute_process(COMMAND ${TD_MAKE_INSTALL_SH} ${TD_COMMUNITY_DIR} ${PROJECT_BINARY_DIR})") ELSEIF (TD_WINDOWS_64) SET(CMAKE_INSTALL_PREFIX C:/TDengine) INSTALL(DIRECTORY ${TD_COMMUNITY_DIR}/src/connector/go DESTINATION connector) diff --git a/documentation/tdenginedocs-cn/getting-started/index.html b/documentation/tdenginedocs-cn/getting-started/index.html index d7e8fe311c..65ef667d00 100644 --- a/documentation/tdenginedocs-cn/getting-started/index.html +++ b/documentation/tdenginedocs-cn/getting-started/index.html @@ -63,7 +63,7 @@ Query OK, 2 row(s) in set (0.001700s)

主要功能

TDengine的核心功能是时序数据库。除此之外,为减少研发的复杂度、系统维护的难度,TDengine还提供缓存、消息队列、订阅、流式计算等功能。更详细的功能如下:

写数据时自动建子表

-

在某些特殊场景中,用户在写数据时并不确定某个设备的表是否存在,此时可使用自动建表语法来实现写入数据时里用超级表定义的表结构自动创建不存在的子表,若该表已存在则不会建立新表。注意:自动建表语句只能自动建立子表而不能建立超级表,这就要求超级表已经被事先定义好。自动建表语法跟insert/import语法非常相似,唯一区别是语句中增加了超级表和标签信息。具体语法如下:

+

在某些特殊场景中,用户在写数据时并不确定某个设备的表是否存在,此时可使用自动建表语法来实现写入数据时用超级表定义的表结构自动创建不存在的子表,若该表已存在则不会建立新表。注意:自动建表语句只能自动建立子表而不能建立超级表,这就要求超级表已经被事先定义好。自动建表语法跟insert/import语法非常相似,唯一区别是语句中增加了超级表和标签信息。具体语法如下:

INSERT INTO <tb_name> USING <stb_name> TAGS (<tag1_value>, ...) VALUES (field_value, ...) (field_value, ...) ...;

向表tb_name中插入一条或多条记录,如果tb_name这张表不存在,则会用超级表stb_name定义的表结构以及用户指定的标签值(即tag1_value…)来创建名为tb_name新表,并将用户指定的值写入表中。如果tb_name已经存在,则建表过程会被忽略,系统也不会检查tb_name的标签是否与用户指定的标签值一致,也即不会更新已存在表的标签。

INSERT INTO <tb1_name> USING <stb1_name> TAGS (<tag1_value1>, ...) VALUES (<field1_value1>, ...) (<field1_value2>, ...) ... <tb_name2> USING <stb_name2> TAGS(<tag1_value2>, ...) VALUES (<field1_value1>, ...) ...;
@@ -105,6 +105,6 @@ GROUP BY location, type

查询仅位于北京以外地区的温度传感器最近24小时(24h)采样值的数量count(*)、平均温度avg(degree)、最高温度max(degree)和最低温度min(degree),将采集结果按照10分钟为周期进行聚合,并将结果按所处地域(location)和传感器类型(type)再次进行聚合。

SELECT COUNT(*), AVG(degree), MAX(degree), MIN(degree)
 FROM thermometer
-WHERE name<>'beijing' and ts>=now-1d
+WHERE location<>'beijing' and ts>=now-1d
 INTERVAL(10M)
 GROUP BY location, type
回去 diff --git a/documentation/webdocs/markdowndocs/Super Table-ch.md b/documentation/webdocs/markdowndocs/Super Table-ch.md index 9267e00a70..c5b207b564 100644 --- a/documentation/webdocs/markdowndocs/Super Table-ch.md +++ b/documentation/webdocs/markdowndocs/Super Table-ch.md @@ -169,7 +169,7 @@ SELECT function,… 以温度传感器采集时序数据作为例,示范STable的使用。 在这个例子中,对每个温度计都会建立一张表,表名为温度计的ID,温度计读数的时刻记为ts,采集的值记为degree。通过tags给每个采集器打上不同的标签,其中记录温度计的地区和类型,以方便我们后面的查询。所有温度计的采集量都一样,因此我们用STable来定义表结构。 -###定义STable表结构并使用它创建子表 +###1:定义STable表结构并使用它创建子表 创建STable语句如下: @@ -189,7 +189,7 @@ CREATE TABLE therm4 USING thermometer TAGS ('shanghai', 3); 其中therm1,therm2,therm3,therm4是超级表thermometer四个具体的子表,也即普通的Table。以therm1为例,它表示采集器therm1的数据,表结构完全由thermometer定义,标签location=”beijing”, type=1表示therm1的地区是北京,类型是第1类的温度计。 -###写入数据 +###2:写入数据 注意,写入数据时不能直接对STable操作,而是要对每张子表进行操作。我们分别向四张表therm1,therm2, therm3, therm4写入一条数据,写入语句如下: @@ -200,7 +200,7 @@ INSERT INTO therm3 VALUES ('2018-01-01 00:00:00.000', 24); INSERT INTO therm4 VALUES ('2018-01-01 00:00:00.000', 23); ``` -### 按标签聚合查询 +###3:按标签聚合查询 查询位于北京(beijing)和天津(tianjing)两个地区的温度传感器采样值的数量count(*)、平均温度avg(degree)、最高温度max(degree)、最低温度min(degree),并将结果按所处地域(location)和传感器类型(type)进行聚合。 @@ -211,7 +211,7 @@ WHERE location='beijing' or location='tianjin' GROUP BY location, type ``` -### 按时间周期聚合查询 +###4:按时间周期聚合查询 查询仅位于北京以外地区的温度传感器最近24小时(24h)采样值的数量count(*)、平均温度avg(degree)、最高温度max(degree)和最低温度min(degree),将采集结果按照10分钟为周期进行聚合,并将结果按所处地域(location)和传感器类型(type)再次进行聚合。 diff --git a/documentation/webdocs/markdowndocs/connector-ch.md b/documentation/webdocs/markdowndocs/connector-ch.md index a8ceaa848f..4d1701d848 100644 --- a/documentation/webdocs/markdowndocs/connector-ch.md +++ b/documentation/webdocs/markdowndocs/connector-ch.md @@ -269,13 +269,14 @@ public Connection getConn() throws Exception{ 用户可以在源代码的src/connector/python文件夹下找到python2和python3的安装包。用户可以通过pip命令安装: -​ `pip install src/connector/python/python2/` +​ `pip install src/connector/python/[linux|windows]/python2/` 或 -​ `pip install src/connector/python/python3/` +​ `pip install src/connector/python/[linux|windows]/python3/` 如果机器上没有pip命令,用户可将src/connector/python/python3或src/connector/python/python2下的taos文件夹拷贝到应用程序的目录使用。 +对于windows 客户端,安装TDengine windows 客户端后,将C:\TDengine\driver\taos.dll拷贝到C:\windows\system32目录下即可。所有TDengine的连接器,均需依赖taos.dll。 ### Python客户端接口 diff --git a/packaging/deb/makedeb.sh b/packaging/deb/makedeb.sh index d54953113a..e4c795f1b8 100755 --- a/packaging/deb/makedeb.sh +++ b/packaging/deb/makedeb.sh @@ -49,6 +49,7 @@ cp ${compile_dir}/build/bin/taosd ${pkg_dir}${install_home_pat cp ${compile_dir}/build/bin/taos ${pkg_dir}${install_home_path}/bin cp ${compile_dir}/build/lib/${libfile} ${pkg_dir}${install_home_path}/driver cp ${compile_dir}/../src/inc/taos.h ${pkg_dir}${install_home_path}/include +cp ${compile_dir}/../src/inc/taoserror.h ${pkg_dir}${install_home_path}/include cp -r ${top_dir}/tests/examples/* ${pkg_dir}${install_home_path}/examples cp -r ${top_dir}/src/connector/grafana ${pkg_dir}${install_home_path}/connector cp -r ${top_dir}/src/connector/python ${pkg_dir}${install_home_path}/connector diff --git a/packaging/rpm/tdengine.spec b/packaging/rpm/tdengine.spec index 6e9e5e6f87..9ede4102e7 100644 --- a/packaging/rpm/tdengine.spec +++ b/packaging/rpm/tdengine.spec @@ -62,6 +62,7 @@ cp %{_compiledir}/build/bin/taosdemo %{buildroot}%{homepath}/bin cp %{_compiledir}/build/bin/taosdump %{buildroot}%{homepath}/bin cp %{_compiledir}/build/lib/${libfile} %{buildroot}%{homepath}/driver cp %{_compiledir}/../src/inc/taos.h %{buildroot}%{homepath}/include +cp %{_compiledir}/../src/inc/taoserror.h %{buildroot}%{homepath}/include cp -r %{_compiledir}/../src/connector/grafana %{buildroot}%{homepath}/connector cp -r %{_compiledir}/../src/connector/python %{buildroot}%{homepath}/connector cp -r %{_compiledir}/../src/connector/go %{buildroot}%{homepath}/connector @@ -138,6 +139,7 @@ if [ $1 -eq 0 ];then ${csudo} rm -f ${bin_link_dir}/taosdump || : ${csudo} rm -f ${cfg_link_dir}/* || : ${csudo} rm -f ${inc_link_dir}/taos.h || : + ${csudo} rm -f ${inc_link_dir}/taoserror.h || : ${csudo} rm -f ${lib_link_dir}/libtaos.* || : ${csudo} rm -f ${log_link_dir} || : diff --git a/packaging/tools/install.sh b/packaging/tools/install.sh index 6b5af3f9ef..4b653dec9c 100755 --- a/packaging/tools/install.sh +++ b/packaging/tools/install.sh @@ -98,9 +98,10 @@ function install_lib() { } function install_header() { - ${csudo} rm -f ${inc_link_dir}/taos.h || : + ${csudo} rm -f ${inc_link_dir}/taos.h ${inc_link_dir}/taoserror.h || : ${csudo} cp -f ${script_dir}/inc/* ${install_main_dir}/include && ${csudo} chmod 644 ${install_main_dir}/include/* ${csudo} ln -s ${install_main_dir}/include/taos.h ${inc_link_dir}/taos.h + ${csudo} ln -s ${install_main_dir}/include/taoserror.h ${inc_link_dir}/taoserror.h } function install_config() { @@ -247,9 +248,9 @@ vercomp () { function is_version_compatible() { - curr_version=$(${bin_dir}/taosd -V | cut -d ' ' -f 1) + curr_version=$(${bin_dir}/taosd -V | cut -d ' ' -f 2) - min_compatible_version=$(${script_dir}/bin/taosd -V | cut -d ' ' -f 2) + min_compatible_version=$(${script_dir}/bin/taosd -V | cut -d ' ' -f 4) vercomp $curr_version $min_compatible_version case $? in diff --git a/packaging/tools/make_install.sh b/packaging/tools/make_install.sh index 4660ba5b2a..c43f918e8d 100755 --- a/packaging/tools/make_install.sh +++ b/packaging/tools/make_install.sh @@ -109,9 +109,10 @@ function install_lib() { function install_header() { - ${csudo} rm -f ${inc_link_dir}/taos.h || : - ${csudo} cp -f ${source_dir}/src/inc/taos.h ${install_main_dir}/include && ${csudo} chmod 644 ${install_main_dir}/include/* + ${csudo} rm -f ${inc_link_dir}/taos.h ${inc_link_dir}/taoserror.h || : + ${csudo} cp -f ${source_dir}/src/inc/taos.h ${source_dir}/src/inc/taoserror.h ${install_main_dir}/include && ${csudo} chmod 644 ${install_main_dir}/include/* ${csudo} ln -s ${install_main_dir}/include/taos.h ${inc_link_dir}/taos.h + ${csudo} ln -s ${install_main_dir}/include/taoserror.h ${inc_link_dir}/taoserror.h } function install_config() { diff --git a/packaging/tools/makepkg.sh b/packaging/tools/makepkg.sh index 1a24ae8a1f..967b14cee9 100755 --- a/packaging/tools/makepkg.sh +++ b/packaging/tools/makepkg.sh @@ -22,7 +22,7 @@ install_dir="${release_dir}/taos-${version}-${package_name}-$(echo ${build_time} bin_files="${build_dir}/bin/taosd ${build_dir}/bin/taos ${build_dir}/bin/taosdemo ${build_dir}/bin/taosdump ${script_dir}/remove.sh" versioninfo=$(${script_dir}/get_version.sh ${code_dir}/util/src/version.c) lib_files="${build_dir}/lib/libtaos.so.${versioninfo}" -header_files="${code_dir}/inc/taos.h" +header_files="${code_dir}/inc/taos.h ${code_dir}/inc/taoserror.h" cfg_files="${top_dir}/packaging/cfg/*.cfg" install_files="${script_dir}/install.sh ${script_dir}/install_client.sh" diff --git a/packaging/tools/post.sh b/packaging/tools/post.sh index 9e18783657..11f3ab9a9b 100755 --- a/packaging/tools/post.sh +++ b/packaging/tools/post.sh @@ -61,8 +61,9 @@ function kill_taosd() { } function install_include() { - ${csudo} rm -f ${inc_link_dir}/taos.h || : + ${csudo} rm -f ${inc_link_dir}/taos.h ${inc_link_dir}/taoserror.h|| : ${csudo} ln -s ${inc_dir}/taos.h ${inc_link_dir}/taos.h + ${csudo} ln -s ${inc_dir}/taoserror.h ${inc_link_dir}/taoserror.h } function install_lib() { diff --git a/packaging/tools/preun.sh b/packaging/tools/preun.sh index 4566db1b5a..a08f4e304e 100755 --- a/packaging/tools/preun.sh +++ b/packaging/tools/preun.sh @@ -94,6 +94,7 @@ ${csudo} rm -f ${bin_link_dir}/taosdemo || : ${csudo} rm -f ${bin_link_dir}/taosdump || : ${csudo} rm -f ${cfg_link_dir}/* || : ${csudo} rm -f ${inc_link_dir}/taos.h || : +${csudo} rm -f ${inc_link_dir}/taoserror.h || : ${csudo} rm -f ${lib_link_dir}/libtaos.* || : ${csudo} rm -f ${log_link_dir} || : diff --git a/packaging/tools/remove.sh b/packaging/tools/remove.sh index 4ada19762c..6c2c6d5ea8 100755 --- a/packaging/tools/remove.sh +++ b/packaging/tools/remove.sh @@ -61,6 +61,7 @@ function clean_lib() { function clean_header() { # Remove link ${csudo} rm -f ${inc_link_dir}/taos.h || : + ${csudo} rm -f ${inc_link_dir}/taoserror.h || : } function clean_config() { diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt index 3bb2f74868..d3baf84d67 100644 --- a/src/client/CMakeLists.txt +++ b/src/client/CMakeLists.txt @@ -7,7 +7,7 @@ INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/inc) INCLUDE_DIRECTORIES(${TD_OS_DIR}/inc) AUX_SOURCE_DIRECTORY(./src SRC) -IF (TD_LINUX_64) +IF ((TD_LINUX_64) OR (TD_LINUX_32 AND TD_ARM)) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/jni/linux) # set the static lib name diff --git a/src/client/inc/tscUtil.h b/src/client/inc/tscUtil.h index 0474697c3f..a0c16f062d 100644 --- a/src/client/inc/tscUtil.h +++ b/src/client/inc/tscUtil.h @@ -30,10 +30,10 @@ extern "C" { #include "tsdb.h" #include "tscSecondaryMerge.h" -#define UTIL_METER_IS_METRIC(cmd) (((cmd)->pMeterMeta != NULL) && ((cmd)->pMeterMeta->meterType == TSDB_METER_METRIC)) -#define UTIL_METER_IS_NOMRAL_METER(cmd) (!(UTIL_METER_IS_METRIC(cmd))) -#define UTIL_METER_IS_CREATE_FROM_METRIC(cmd) \ - (((cmd)->pMeterMeta != NULL) && ((cmd)->pMeterMeta->meterType == TSDB_METER_MTABLE)) +#define UTIL_METER_IS_METRIC(metaInfo) (((metaInfo)->pMeterMeta != NULL) && ((metaInfo)->pMeterMeta->meterType == TSDB_METER_METRIC)) +#define UTIL_METER_IS_NOMRAL_METER(metaInfo) (!(UTIL_METER_IS_METRIC(metaInfo))) +#define UTIL_METER_IS_CREATE_FROM_METRIC(metaInfo) \ + (((metaInfo)->pMeterMeta != NULL) && ((metaInfo)->pMeterMeta->meterType == TSDB_METER_MTABLE)) #define TSDB_COL_IS_TAG(f) (((f)&TSDB_COL_TAG) != 0) diff --git a/src/client/inc/tsclient.h b/src/client/inc/tsclient.h index 0d2c7dace3..44bfc978a3 100644 --- a/src/client/inc/tsclient.h +++ b/src/client/inc/tsclient.h @@ -479,6 +479,7 @@ void tscProcessMultiVnodesInsertForFile(SSqlObj *pSql); void tscKillMetricQuery(SSqlObj *pSql); void tscInitResObjForLocalQuery(SSqlObj *pObj, int32_t numOfRes, int32_t rowLen); int32_t tscBuildResultsForEmptyRetrieval(SSqlObj *pSql); +bool tscIsUpdateQuery(STscObj *pObj); // transfer SSqlInfo to SqlCmd struct int32_t tscToSQLCmd(SSqlObj *pSql, struct SSqlInfo *pInfo); diff --git a/src/client/src/TSDBJNIConnector.c b/src/client/src/TSDBJNIConnector.c index fb175618c0..d958679544 100644 --- a/src/client/src/TSDBJNIConnector.c +++ b/src/client/src/TSDBJNIConnector.c @@ -13,8 +13,6 @@ * along with this program. If not, see . */ -#include - #include "os.h" #include "com_taosdata_jdbc_TSDBJNIConnector.h" #include "taos.h" @@ -63,13 +61,13 @@ jmethodID g_rowdataSetByteArrayFp; void jniGetGlobalMethod(JNIEnv *env) { // make sure init function executed once - switch (__sync_val_compare_and_swap_32(&__init, 0, 1)) { + switch (atomic_val_compare_exchange_32(&__init, 0, 1)) { case 0: break; case 1: do { taosMsleep(0); - } while (__sync_val_load_32(&__init) == 1); + } while (atomic_load_32(&__init) == 1); case 2: return; } @@ -109,7 +107,7 @@ void jniGetGlobalMethod(JNIEnv *env) { g_rowdataSetByteArrayFp = (*env)->GetMethodID(env, g_rowdataClass, "setByteArray", "(I[B)V"); (*env)->DeleteLocalRef(env, rowdataClass); - __sync_val_restore_32(&__init, 2); + atomic_store_32(&__init, 2); jniTrace("native method register finished"); } @@ -208,10 +206,10 @@ JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_connectImp(JNIEn ret = (jlong)taos_connect((char *)host, (char *)user, (char *)pass, (char *)dbname, jport); if (ret == 0) { - jniError("jobj:%p, taos:%p, connect to tdengine failed, host=%s, user=%s, dbname=%s, port=%d", jobj, (void *)ret, + jniError("jobj:%p, conn:%p, connect to database failed, host=%s, user=%s, dbname=%s, port=%d", jobj, (void *)ret, (char *)host, (char *)user, (char *)dbname, jport); } else { - jniTrace("jobj:%p, taos:%p, connect to tdengine succeed, host=%s, user=%s, dbname=%s, port=%d", jobj, (void *)ret, + jniTrace("jobj:%p, conn:%p, connect to database succeed, host=%s, user=%s, dbname=%s, port=%d", jobj, (void *)ret, (char *)host, (char *)user, (char *)dbname, jport); } @@ -232,7 +230,7 @@ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_executeQueryImp(J } if (jsql == NULL) { - jniError("jobj:%p, taos:%p, sql is null", jobj, tscon); + jniError("jobj:%p, conn:%p, sql is null", jobj, tscon); return JNI_SQL_NULL; } @@ -250,7 +248,7 @@ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_executeQueryImp(J int code = taos_query(tscon, dst); if (code != 0) { - jniError("jobj:%p, taos:%p, code:%d, msg:%s, sql:%s", jobj, tscon, code, taos_errstr(tscon), dst); + jniError("jobj:%p, conn:%p, code:%d, msg:%s, sql:%s", jobj, tscon, code, taos_errstr(tscon), dst); free(dst); return JNI_TDENGINE_ERROR; } else { @@ -259,9 +257,9 @@ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_executeQueryImp(J if (pSql->cmd.command == TSDB_SQL_INSERT) { affectRows = taos_affected_rows(tscon); - jniTrace("jobj:%p, taos:%p, code:%d, affect rows:%d, sql:%s", jobj, tscon, code, affectRows, dst); + jniTrace("jobj:%p, conn:%p, code:%d, affect rows:%d, sql:%s", jobj, tscon, code, affectRows, dst); } else { - jniTrace("jobj:%p, taos:%p, code:%d, sql:%s", jobj, tscon, code, dst); + jniTrace("jobj:%p, conn:%p, code:%d, sql:%s", jobj, tscon, code, dst); } free(dst); @@ -291,15 +289,17 @@ JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_getResultSetImp( return JNI_CONNECTION_NULL; } - int num_fields = taos_field_count(tscon); - if (num_fields != 0) { - jlong ret = (jlong)taos_use_result(tscon); - jniTrace("jobj:%p, taos:%p, get resultset:%p", jobj, tscon, (void *)ret); - return ret; + jlong ret = 0; + + if (tscIsUpdateQuery(tscon)) { + ret = 0; // for update query, no result pointer + jniTrace("jobj:%p, conn:%p, no result", jobj, tscon); + } else { + ret = (jlong) taos_use_result(tscon); + jniTrace("jobj:%p, conn:%p, get resultset:%p", jobj, tscon, (void *) ret); } - jniTrace("jobj:%p, taos:%p, no resultset", jobj, tscon); - return 0; + return ret; } JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_freeResultSetImp(JNIEnv *env, jobject jobj, jlong con, @@ -311,12 +311,12 @@ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_freeResultSetImp( } if ((void *)res == NULL) { - jniError("jobj:%p, taos:%p, resultset is null", jobj, tscon); + jniError("jobj:%p, conn:%p, resultset is null", jobj, tscon); return JNI_RESULT_SET_NULL; } taos_free_result((void *)res); - jniTrace("jobj:%p, taos:%p, free resultset:%p", jobj, tscon, (void *)res); + jniTrace("jobj:%p, conn:%p, free resultset:%p", jobj, tscon, (void *)res); return JNI_SUCCESS; } @@ -330,7 +330,7 @@ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_getAffectedRowsIm jint ret = taos_affected_rows(tscon); - jniTrace("jobj:%p, taos:%p, affect rows:%d", jobj, tscon, (void *)con, ret); + jniTrace("jobj:%p, conn:%p, affect rows:%d", jobj, tscon, (void *)con, ret); return ret; } @@ -346,7 +346,7 @@ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_getSchemaMetaData TAOS_RES *result = (TAOS_RES *)res; if (result == NULL) { - jniError("jobj:%p, taos:%p, resultset is null", jobj, tscon); + jniError("jobj:%p, conn:%p, resultset is null", jobj, tscon); return JNI_RESULT_SET_NULL; } @@ -356,10 +356,10 @@ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_getSchemaMetaData // jobject arrayListObj = (*env)->NewObject(env, g_arrayListClass, g_arrayListConstructFp, ""); if (num_fields == 0) { - jniError("jobj:%p, taos:%p, resultset:%p, fields size is %d", jobj, tscon, res, num_fields); + jniError("jobj:%p, conn:%p, resultset:%p, fields size is %d", jobj, tscon, res, num_fields); return JNI_NUM_OF_FIELDS_0; } else { - jniTrace("jobj:%p, taos:%p, resultset:%p, fields size is %d", jobj, tscon, res, num_fields); + jniTrace("jobj:%p, conn:%p, resultset:%p, fields size is %d", jobj, tscon, res, num_fields); for (int i = 0; i < num_fields; ++i) { jobject metadataObj = (*env)->NewObject(env, g_metadataClass, g_metadataConstructFp); (*env)->SetIntField(env, metadataObj, g_metadataColtypeField, fields[i].type); @@ -402,7 +402,7 @@ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_fetchRowImp(JNIEn TAOS_RES *result = (TAOS_RES *)res; if (result == NULL) { - jniError("jobj:%p, taos:%p, resultset is null", jobj, tscon); + jniError("jobj:%p, conn:%p, resultset is null", jobj, tscon); return JNI_RESULT_SET_NULL; } @@ -410,7 +410,7 @@ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_fetchRowImp(JNIEn int num_fields = taos_num_fields(result); if (num_fields == 0) { - jniError("jobj:%p, taos:%p, resultset:%p, fields size is %d", jobj, tscon, res, num_fields); + jniError("jobj:%p, conn:%p, resultset:%p, fields size is %d", jobj, tscon, res, num_fields); return JNI_NUM_OF_FIELDS_0; } @@ -418,10 +418,10 @@ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_fetchRowImp(JNIEn if (row == NULL) { int tserrno = taos_errno(tscon); if (tserrno == 0) { - jniTrace("jobj:%p, taos:%p, resultset:%p, fields size is %d, fetch row to the end", jobj, tscon, res, num_fields); + jniTrace("jobj:%p, conn:%p, resultset:%p, fields size is %d, fetch row to the end", jobj, tscon, res, num_fields); return JNI_FETCH_END; } else { - jniTrace("jobj:%p, taos:%p, interruptted query", jobj, tscon); + jniTrace("jobj:%p, conn:%p, interruptted query", jobj, tscon); return JNI_RESULT_SET_NULL; } } @@ -485,7 +485,7 @@ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_closeConnectionIm jniError("jobj:%p, connection is closed", jobj); return JNI_CONNECTION_NULL; } else { - jniTrace("jobj:%p, taos:%p, close connection success", jobj, tscon); + jniTrace("jobj:%p, conn:%p, close connection success", jobj, tscon); taos_close(tscon); return JNI_SUCCESS; } @@ -640,7 +640,7 @@ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_validateCreateTab } if (jsql == NULL) { - jniError("jobj:%p, taos:%p, sql is null", jobj, tscon); + jniError("jobj:%p, conn:%p, sql is null", jobj, tscon); return JNI_SQL_NULL; } diff --git a/src/client/src/tscAst.c b/src/client/src/tscAst.c index 845a9fd36d..527b1345e2 100644 --- a/src/client/src/tscAst.c +++ b/src/client/src/tscAst.c @@ -13,12 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include - #include "os.h" #include "taosmsg.h" #include "tast.h" @@ -261,8 +255,7 @@ static tSQLSyntaxNode *createSyntaxTree(SSchema *pSchema, int32_t numOfCols, cha t0 = tStrGetToken(str, i, false, 0, NULL); if (t0.n == 0 || t0.type == TK_RP) { - if (pLeft->nodeType != TSQL_NODE_EXPR) { - // if left is not the expr, it is not a legal expr + if (pLeft->nodeType != TSQL_NODE_EXPR) { // if left is not the expr, it is not a legal expr tSQLSyntaxNodeDestroy(pLeft, NULL); return NULL; } @@ -326,13 +319,13 @@ static tSQLSyntaxNode *createSyntaxTree(SSchema *pSchema, int32_t numOfCols, cha pn->colId = -1; return pn; } else { - int32_t optr = getBinaryExprOptr(&t0); - if (optr <= 0) { + uint8_t localOptr = getBinaryExprOptr(&t0); + if (localOptr <= 0) { pError("not support binary operator:%d", t0.type); return NULL; } - return parseRemainStr(str, pBinExpr, pSchema, optr, numOfCols, i); + return parseRemainStr(str, pBinExpr, pSchema, localOptr, numOfCols, i); } } diff --git a/src/client/src/tscAsync.c b/src/client/src/tscAsync.c index 6e0e1ae7c5..aad0295959 100644 --- a/src/client/src/tscAsync.c +++ b/src/client/src/tscAsync.c @@ -13,8 +13,7 @@ * along with this program. If not, see . */ -#include -#include +#include "os.h" #include "tlog.h" #include "trpc.h" diff --git a/src/client/src/tscCache.c b/src/client/src/tscCache.c index 866b6e7dbc..f508857ce1 100644 --- a/src/client/src/tscCache.c +++ b/src/client/src/tscCache.c @@ -13,14 +13,7 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include -#include -#include +#include "os.h" #include "tglobalcfg.h" #include "tlog.h" diff --git a/src/client/src/tscFunctionImpl.c b/src/client/src/tscFunctionImpl.c index 0f9be90412..227095fc03 100644 --- a/src/client/src/tscFunctionImpl.c +++ b/src/client/src/tscFunctionImpl.c @@ -15,16 +15,6 @@ #pragma GCC diagnostic ignored "-Wincompatible-pointer-types" -#include -#include -#include -#include -#include -#include -#include -#include -#include - #include "os.h" #include "taosmsg.h" #include "tast.h" @@ -73,6 +63,14 @@ } \ } while (0); +#define DO_UPDATE_TAG_COLUMNS_WITHOUT_TS(ctx) \ +do {\ +for (int32_t i = 0; i < (ctx)->tagInfo.numOfTagCols; ++i) { \ + SQLFunctionCtx *__ctx = (ctx)->tagInfo.pTagCtxList[i]; \ + aAggs[TSDB_FUNC_TAG].xFunction(__ctx); \ + } \ +} while(0); + void noop(SQLFunctionCtx *UNUSED_PARAM(pCtx)) {} typedef struct tValuePair { @@ -114,7 +112,9 @@ typedef struct SFirstLastInfo { } SFirstLastInfo; typedef struct SFirstLastInfo SLastrowInfo; -typedef struct SPercentileInfo { tMemBucket *pMemBucket; } SPercentileInfo; +typedef struct SPercentileInfo { + tMemBucket *pMemBucket; +} SPercentileInfo; typedef struct STopBotInfo { int32_t num; @@ -128,9 +128,13 @@ typedef struct SLeastsquareInfo { int64_t num; } SLeastsquareInfo; -typedef struct SAPercentileInfo { SHistogramInfo *pHisto; } SAPercentileInfo; +typedef struct SAPercentileInfo { + SHistogramInfo *pHisto; +} SAPercentileInfo; -typedef struct STSCompInfo { STSBuf *pTSBuf; } STSCompInfo; +typedef struct STSCompInfo { + STSBuf *pTSBuf; +} STSCompInfo; int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionId, int32_t param, int16_t *type, int16_t *bytes, int16_t *intermediateResBytes, int16_t extLength, bool isSuperTable) { @@ -461,21 +465,32 @@ int32_t no_data_info(SQLFunctionCtx *pCtx, TSKEY start, TSKEY end, int32_t colId } \ }; -#define UPDATE_DATA(ctx, left, right, num, sign) \ - do { \ - if (((left) < (right)) ^ (sign)) { \ - (left) = right; \ - DO_UPDATE_TAG_COLUMNS(ctx, 0); \ - (num) += 1; \ - } \ - } while (0) +#define UPDATE_DATA(ctx, left, right, num, sign, k) \ + do { \ + if (((left) < (right)) ^ (sign)) { \ + (left) = (right); \ + DO_UPDATE_TAG_COLUMNS(ctx, k); \ + (num) += 1; \ + } \ + } while (0); + +#define DUPATE_DATA_WITHOUT_TS(ctx, left, right, num, sign) \ +do { \ + if (((left) < (right)) ^ (sign)) { \ + (left) = (right); \ + DO_UPDATE_TAG_COLUMNS_WITHOUT_TS(ctx); \ + (num) += 1; \ + } \ + } while (0); + #define LOOPCHECK_N(val, list, ctx, tsdbType, sign, num) \ for (int32_t i = 0; i < ((ctx)->size); ++i) { \ if ((ctx)->hasNull && isNull((char *)&(list)[i], tsdbType)) { \ continue; \ } \ - UPDATE_DATA(ctx, val, (list)[i], num, sign); \ + TSKEY key = (ctx)->ptsList[i]; \ + UPDATE_DATA(ctx, val, (list)[i], num, sign, key); \ } #define TYPED_LOOPCHECK_N(type, data, list, ctx, tsdbType, sign, notNullElems) \ @@ -896,16 +911,18 @@ static void minMax_function(SQLFunctionCtx *pCtx, char *pOutput, int32_t isMin, index = pCtx->preAggVals.maxIndex; } + TSKEY key = pCtx->ptsList[index]; + if (pCtx->inputType >= TSDB_DATA_TYPE_TINYINT && pCtx->inputType <= TSDB_DATA_TYPE_BIGINT) { int64_t val = GET_INT64_VAL(tval); if (pCtx->inputType == TSDB_DATA_TYPE_TINYINT) { int8_t *data = (int8_t *)pOutput; - UPDATE_DATA(pCtx, *data, val, notNullElems, isMin); + UPDATE_DATA(pCtx, *data, val, notNullElems, isMin, key); } else if (pCtx->inputType == TSDB_DATA_TYPE_SMALLINT) { int16_t *data = (int16_t *)pOutput; - UPDATE_DATA(pCtx, *data, val, notNullElems, isMin); + UPDATE_DATA(pCtx, *data, val, notNullElems, isMin, key); } else if (pCtx->inputType == TSDB_DATA_TYPE_INT) { int32_t *data = (int32_t *)pOutput; #if defined(_DEBUG_VIEW) @@ -916,27 +933,27 @@ static void minMax_function(SQLFunctionCtx *pCtx, char *pOutput, int32_t isMin, *data = val; for (int32_t i = 0; i < (pCtx)->tagInfo.numOfTagCols; ++i) { SQLFunctionCtx *__ctx = pCtx->tagInfo.pTagCtxList[i]; - if (__ctx->functionId == TSDB_FUNC_TAG_DUMMY) { - aAggs[TSDB_FUNC_TAG].xFunction(__ctx); - } else if (__ctx->functionId == TSDB_FUNC_TS_DUMMY) { - *((int64_t *)__ctx->aOutputBuf) = pCtx->ptsList[index]; + if (__ctx->functionId == TSDB_FUNC_TS_DUMMY) { + __ctx->tag = (tVariant){.i64Key = key, .nType = TSDB_DATA_TYPE_BIGINT}; } + + aAggs[TSDB_FUNC_TAG].xFunction(__ctx); } } } else if (pCtx->inputType == TSDB_DATA_TYPE_BIGINT) { int64_t *data = (int64_t *)pOutput; - UPDATE_DATA(pCtx, *data, val, notNullElems, isMin); + UPDATE_DATA(pCtx, *data, val, notNullElems, isMin, key); } } else if (pCtx->inputType == TSDB_DATA_TYPE_DOUBLE) { double *data = (double *)pOutput; double val = GET_DOUBLE_VAL(tval); - UPDATE_DATA(pCtx, *data, val, notNullElems, isMin); + UPDATE_DATA(pCtx, *data, val, notNullElems, isMin, key); } else if (pCtx->inputType == TSDB_DATA_TYPE_FLOAT) { float *data = (float *)pOutput; double val = GET_DOUBLE_VAL(tval); - UPDATE_DATA(pCtx, *data, val, notNullElems, isMin); + UPDATE_DATA(pCtx, *data, val, notNullElems, isMin, key); } return; @@ -961,7 +978,9 @@ static void minMax_function(SQLFunctionCtx *pCtx, char *pOutput, int32_t isMin, if ((*retVal < pData[i]) ^ isMin) { *retVal = pData[i]; - DO_UPDATE_TAG_COLUMNS(pCtx, pCtx->ptsList[i]); + TSKEY k = pCtx->ptsList[i]; + + DO_UPDATE_TAG_COLUMNS(pCtx, k); } *notNullElems += 1; @@ -1099,12 +1118,12 @@ static int32_t minmax_merge_impl(SQLFunctionCtx *pCtx, int32_t bytes, char *outp switch (type) { case TSDB_DATA_TYPE_TINYINT: { int8_t v = GET_INT8_VAL(input); - UPDATE_DATA(pCtx, *(int8_t *)output, v, notNullElems, isMin); + DUPATE_DATA_WITHOUT_TS(pCtx, *(int8_t *)output, v, notNullElems, isMin); break; }; case TSDB_DATA_TYPE_SMALLINT: { int16_t v = GET_INT16_VAL(input); - UPDATE_DATA(pCtx, *(int16_t *)output, v, notNullElems, isMin); + DUPATE_DATA_WITHOUT_TS(pCtx, *(int16_t *)output, v, notNullElems, isMin); break; } case TSDB_DATA_TYPE_INT: { @@ -1114,7 +1133,7 @@ static int32_t minmax_merge_impl(SQLFunctionCtx *pCtx, int32_t bytes, char *outp for (int32_t i = 0; i < pCtx->tagInfo.numOfTagCols; ++i) { SQLFunctionCtx *__ctx = pCtx->tagInfo.pTagCtxList[i]; - aAggs[TSDB_FUNC_TAG].xFunction(__ctx); + aAggs[TSDB_FUNC_TAG].xFunction(__ctx); } notNullElems++; @@ -1123,17 +1142,17 @@ static int32_t minmax_merge_impl(SQLFunctionCtx *pCtx, int32_t bytes, char *outp } case TSDB_DATA_TYPE_FLOAT: { float v = GET_FLOAT_VAL(input); - UPDATE_DATA(pCtx, *(float *)output, v, notNullElems, isMin); + DUPATE_DATA_WITHOUT_TS(pCtx, *(float *)output, v, notNullElems, isMin); break; } case TSDB_DATA_TYPE_DOUBLE: { double v = GET_DOUBLE_VAL(input); - UPDATE_DATA(pCtx, *(double *)output, v, notNullElems, isMin); + DUPATE_DATA_WITHOUT_TS(pCtx, *(double *)output, v, notNullElems, isMin); break; } case TSDB_DATA_TYPE_BIGINT: { int64_t v = GET_INT64_VAL(input); - UPDATE_DATA(pCtx, *(int64_t *)output, v, notNullElems, isMin); + DUPATE_DATA_WITHOUT_TS(pCtx, *(int64_t *)output, v, notNullElems, isMin); break; }; default: @@ -1189,38 +1208,39 @@ static void max_func_second_merge(SQLFunctionCtx *pCtx) { static void minMax_function_f(SQLFunctionCtx *pCtx, int32_t index, int32_t isMin) { char *pData = GET_INPUT_CHAR_INDEX(pCtx, index); + TSKEY key = pCtx->ptsList[index]; int32_t num = 0; if (pCtx->inputType == TSDB_DATA_TYPE_TINYINT) { int8_t *output = (int8_t *)pCtx->aOutputBuf; int8_t i = GET_INT8_VAL(pData); - UPDATE_DATA(pCtx, *output, i, num, isMin); + UPDATE_DATA(pCtx, *output, i, num, isMin, key); } else if (pCtx->inputType == TSDB_DATA_TYPE_SMALLINT) { int16_t *output = pCtx->aOutputBuf; int16_t i = GET_INT16_VAL(pData); - UPDATE_DATA(pCtx, *output, i, num, isMin); + UPDATE_DATA(pCtx, *output, i, num, isMin, key); } else if (pCtx->inputType == TSDB_DATA_TYPE_INT) { int32_t *output = pCtx->aOutputBuf; int32_t i = GET_INT32_VAL(pData); - UPDATE_DATA(pCtx, *output, i, num, isMin); + UPDATE_DATA(pCtx, *output, i, num, isMin, key); } else if (pCtx->inputType == TSDB_DATA_TYPE_BIGINT) { int64_t *output = pCtx->aOutputBuf; int64_t i = GET_INT64_VAL(pData); - UPDATE_DATA(pCtx, *output, i, num, isMin); + UPDATE_DATA(pCtx, *output, i, num, isMin, key); } else if (pCtx->inputType == TSDB_DATA_TYPE_FLOAT) { float *output = pCtx->aOutputBuf; float i = GET_FLOAT_VAL(pData); - UPDATE_DATA(pCtx, *output, i, num, isMin); + UPDATE_DATA(pCtx, *output, i, num, isMin, key); } else if (pCtx->inputType == TSDB_DATA_TYPE_DOUBLE) { double *output = pCtx->aOutputBuf; double i = GET_DOUBLE_VAL(pData); - UPDATE_DATA(pCtx, *output, i, num, isMin); + UPDATE_DATA(pCtx, *output, i, num, isMin, key); } GET_RES_INFO(pCtx)->hasResult = DATA_SET_FLAG; @@ -1462,7 +1482,9 @@ static void first_function_f(SQLFunctionCtx *pCtx, int32_t index) { SET_VAL(pCtx, 1, 1); memcpy(pCtx->aOutputBuf, pData, pCtx->inputBytes); - DO_UPDATE_TAG_COLUMNS(pCtx, 0); + + TSKEY ts = pCtx->ptsList[index]; + DO_UPDATE_TAG_COLUMNS(pCtx, ts); SResultInfo *pInfo = GET_RES_INFO(pCtx); pInfo->hasResult = DATA_SET_FLAG; @@ -1555,7 +1577,7 @@ static void first_dist_func_merge(SQLFunctionCtx *pCtx) { SFirstLastInfo *pOutput = (SFirstLastInfo *)(pCtx->aOutputBuf + pCtx->inputBytes); if (pOutput->hasResult != DATA_SET_FLAG || pInput->ts < pOutput->ts) { memcpy(pCtx->aOutputBuf, pData, pCtx->inputBytes + sizeof(SFirstLastInfo)); - DO_UPDATE_TAG_COLUMNS(pCtx, 0); + DO_UPDATE_TAG_COLUMNS(pCtx, pInput->ts); } } @@ -1603,7 +1625,9 @@ static void last_function(SQLFunctionCtx *pCtx) { } memcpy(pCtx->aOutputBuf, data, pCtx->inputBytes); - DO_UPDATE_TAG_COLUMNS(pCtx, 0); + + TSKEY ts = pCtx->ptsList[i]; + DO_UPDATE_TAG_COLUMNS(pCtx, ts); SResultInfo *pInfo = GET_RES_INFO(pCtx); pInfo->hasResult = DATA_SET_FLAG; @@ -1628,7 +1652,9 @@ static void last_function_f(SQLFunctionCtx *pCtx, int32_t index) { SET_VAL(pCtx, 1, 1); memcpy(pCtx->aOutputBuf, pData, pCtx->inputBytes); - DO_UPDATE_TAG_COLUMNS(pCtx, 0); + + TSKEY ts = pCtx->ptsList[index]; + DO_UPDATE_TAG_COLUMNS(pCtx, ts); SResultInfo *pResInfo = GET_RES_INFO(pCtx); pResInfo->hasResult = DATA_SET_FLAG; @@ -1725,7 +1751,7 @@ static void last_dist_func_merge(SQLFunctionCtx *pCtx) { if (pOutput->hasResult != DATA_SET_FLAG || pOutput->ts < pInput->ts) { memcpy(pCtx->aOutputBuf, pData, pCtx->inputBytes + sizeof(SFirstLastInfo)); - DO_UPDATE_TAG_COLUMNS(pCtx, 0); + DO_UPDATE_TAG_COLUMNS(pCtx, pInput->ts); } } @@ -1780,7 +1806,7 @@ static void last_row_function(SQLFunctionCtx *pCtx) { pInfo1->ts = pCtx->param[0].i64Key; pInfo1->hasResult = DATA_SET_FLAG; - DO_UPDATE_TAG_COLUMNS(pCtx, 0); + DO_UPDATE_TAG_COLUMNS(pCtx, pInfo1->ts); } SET_VAL(pCtx, pCtx->size, 1); @@ -1814,6 +1840,11 @@ static void valuePairAssign(tValuePair *dst, int16_t type, const char *val, int6 memcpy(dst->pTags, pTags, (size_t)pTagInfo->tagsLen); } else { // the tags are dumped from the ctx tag fields for (int32_t i = 0; i < pTagInfo->numOfTagCols; ++i) { + SQLFunctionCtx* __ctx = pTagInfo->pTagCtxList[i]; + if (__ctx->functionId == TSDB_FUNC_TS_DUMMY) { + __ctx->tag = (tVariant) {.nType = TSDB_DATA_TYPE_BIGINT, .i64Key = tsKey}; + } + tVariantDump(&pTagInfo->pTagCtxList[i]->tag, dst->pTags + size, pTagInfo->pTagCtxList[i]->tag.nType); size += pTagInfo->pTagCtxList[i]->outputBytes; } @@ -1835,8 +1866,9 @@ static void do_top_function_add(STopBotInfo *pInfo, int32_t maxLen, void *pData, tValuePair **pList = pInfo->res; if (pInfo->num < maxLen) { - if (pInfo->num == 0 || ((type >= TSDB_DATA_TYPE_TINYINT && type <= TSDB_DATA_TYPE_BIGINT) && - val.i64Key >= pList[pInfo->num - 1]->v.i64Key) || + if (pInfo->num == 0 || + ((type >= TSDB_DATA_TYPE_TINYINT && type <= TSDB_DATA_TYPE_BIGINT) && + val.i64Key >= pList[pInfo->num - 1]->v.i64Key) || ((type >= TSDB_DATA_TYPE_FLOAT && type <= TSDB_DATA_TYPE_DOUBLE) && val.dKey >= pList[pInfo->num - 1]->v.dKey)) { valuePairAssign(pList[pInfo->num], type, &val.i64Key, ts, pTags, pTagInfo, stage); @@ -3753,9 +3785,6 @@ static void getStatics_i64(int64_t *primaryKey, int64_t *data, int32_t numOfRow, assert(numOfRow <= INT16_MAX); - int64_t lastKey = 0; - int64_t lastVal = TSDB_DATA_BIGINT_NULL; - for (int32_t i = 0; i < numOfRow; ++i) { if (isNull(&data[i], TSDB_DATA_TYPE_BIGINT)) { (*numOfNull) += 1; @@ -3786,10 +3815,11 @@ static void getStatics_i64(int64_t *primaryKey, int64_t *data, int32_t numOfRow, static void getStatics_f(int64_t *primaryKey, float *data, int32_t numOfRow, double *min, double *max, double *sum, int16_t *minIndex, int16_t *maxIndex, int32_t *numOfNull) { - *min = DBL_MAX; - *max = -DBL_MAX; - *minIndex = 0; - *maxIndex = 0; + float fmin = DBL_MAX; + float fmax = -DBL_MAX; + float fminIndex = 0; + float fmaxIndex = 0; + double dsum = 0; assert(numOfRow <= INT16_MAX); @@ -3799,15 +3829,19 @@ static void getStatics_f(int64_t *primaryKey, float *data, int32_t numOfRow, dou continue; } - *sum += data[i]; - if (*min > data[i]) { - *min = data[i]; - *minIndex = i; + float fv = 0; + *(int32_t*)(&fv) = *(int32_t*)(&(data[i])); + + //*sum += data[i]; + dsum += fv; + if (fmin > fv) { + fmin = fv; + fminIndex = i; } - if (*max < data[i]) { - *max = data[i]; - *maxIndex = i; + if (fmax < fv) { + fmax = fv; + fmaxIndex = i; } // if (isNull(&lastVal, TSDB_DATA_TYPE_FLOAT)) { @@ -3819,35 +3853,48 @@ static void getStatics_f(int64_t *primaryKey, float *data, int32_t numOfRow, dou // lastVal = data[i]; // } } + + double csum = 0; + *(int64_t*)(&csum) = *(int64_t*)sum; + csum += dsum; + *(int64_t*)(sum) = *(int64_t*)(&csum); + + *(int32_t*)max = *(int32_t*)(&fmax); + *(int32_t*)min = *(int32_t*)(&fmin); + *(int32_t*)minIndex = *(int32_t*)(&fminIndex); + *(int32_t*)maxIndex = *(int32_t*)(&fmaxIndex); + } static void getStatics_d(int64_t *primaryKey, double *data, int32_t numOfRow, double *min, double *max, double *sum, int16_t *minIndex, int16_t *maxIndex, int32_t *numOfNull) { - *min = DBL_MAX; - *max = -DBL_MAX; - *minIndex = 0; - *maxIndex = 0; + double dmin = DBL_MAX; + double dmax = -DBL_MAX; + double dminIndex = 0; + double dmaxIndex = 0; + double dsum = 0; assert(numOfRow <= INT16_MAX); - int64_t lastKey = 0; - double lastVal = TSDB_DATA_DOUBLE_NULL; - for (int32_t i = 0; i < numOfRow; ++i) { if (isNull(&data[i], TSDB_DATA_TYPE_DOUBLE)) { (*numOfNull) += 1; continue; } - *sum += data[i]; - if (*min > data[i]) { - *min = data[i]; - *minIndex = i; + double dv = 0; + *(int64_t*)(&dv) = *(int64_t*)(&(data[i])); + + //*sum += data[i]; + dsum += dv; + if (dmin > dv) { + dmin = dv; + dminIndex = i; } - if (*max < data[i]) { - *max = data[i]; - *maxIndex = i; + if (dmax < dv) { + dmax = dv; + dmaxIndex = i; } // if (isNull(&lastVal, TSDB_DATA_TYPE_DOUBLE)) { @@ -3859,6 +3906,16 @@ static void getStatics_d(int64_t *primaryKey, double *data, int32_t numOfRow, do // lastVal = data[i]; // } } + + double csum = 0; + *(int64_t*)(&csum) = *(int64_t*)sum; + csum += dsum; + *(int64_t*)(sum) = *(int64_t*)(&csum); + + *(int64_t*)max = *(int64_t*)(&dmax); + *(int64_t*)min = *(int64_t*)(&dmin); + *(int64_t*)minIndex = *(int64_t*)(&dminIndex); + *(int64_t*)maxIndex = *(int64_t*)(&dmaxIndex); } void getStatistics(char *priData, char *data, int32_t size, int32_t numOfRow, int32_t type, int64_t *min, int64_t *max, @@ -4303,173 +4360,427 @@ int32_t funcCompatDefList[28] = { */ 1, 1, 1, -1, 1, 1, 5}; -SQLAggFuncElem aAggs[28] = { - { - // 0, count function does not invoke the finalize function - "count", TSDB_FUNC_COUNT, TSDB_FUNC_COUNT, TSDB_BASE_FUNC_SO, function_setup, count_function, count_function_f, - no_next_step, noop, count_func_merge, count_func_merge, count_load_data_info, - }, - { - // 1 - "sum", TSDB_FUNC_SUM, TSDB_FUNC_SUM, TSDB_BASE_FUNC_SO, function_setup, sum_function, sum_function_f, - no_next_step, function_finalizer, sum_func_merge, sum_func_second_merge, precal_req_load_info, - }, - { - // 2 - "avg", TSDB_FUNC_AVG, TSDB_FUNC_AVG, TSDB_BASE_FUNC_SO, function_setup, avg_function, avg_function_f, - no_next_step, avg_finalizer, avg_func_merge, avg_func_second_merge, precal_req_load_info, - }, - { - // 3 - "min", TSDB_FUNC_MIN, TSDB_FUNC_MIN, TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, min_func_setup, - min_function, min_function_f, no_next_step, function_finalizer, min_func_merge, min_func_second_merge, - precal_req_load_info, - }, - { - // 4 - "max", TSDB_FUNC_MAX, TSDB_FUNC_MAX, TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, max_func_setup, - max_function, max_function_f, no_next_step, function_finalizer, max_func_merge, max_func_second_merge, - precal_req_load_info, - }, - { - // 5 - "stddev", TSDB_FUNC_STDDEV, TSDB_FUNC_INVALID_ID, TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STREAM | TSDB_FUNCSTATE_OF, - function_setup, stddev_function, stddev_function_f, stddev_next_step, stddev_finalizer, noop, noop, - data_req_load_info, - }, - { - // 6 - "percentile", TSDB_FUNC_PERCT, TSDB_FUNC_INVALID_ID, - TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STREAM | TSDB_FUNCSTATE_OF, percentile_function_setup, percentile_function, - percentile_function_f, no_next_step, percentile_finalizer, noop, noop, data_req_load_info, - }, - { - // 7 - "apercentile", TSDB_FUNC_APERCT, TSDB_FUNC_APERCT, - TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STREAM | TSDB_FUNCSTATE_OF | TSDB_FUNCSTATE_METRIC, - apercentile_function_setup, apercentile_function, apercentile_function_f, no_next_step, apercentile_finalizer, - apercentile_func_merge, apercentile_func_second_merge, data_req_load_info, - }, - { - // 8 - "first", TSDB_FUNC_FIRST, TSDB_FUNC_FIRST_DST, TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, function_setup, - first_function, first_function_f, no_next_step, function_finalizer, noop, noop, first_data_req_info, - }, - { - // 9 - "last", TSDB_FUNC_LAST, TSDB_FUNC_LAST_DST, TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, function_setup, - last_function, last_function_f, no_next_step, function_finalizer, noop, noop, last_data_req_info, - }, - { - // 10 - "last_row", TSDB_FUNC_LAST_ROW, TSDB_FUNC_LAST_ROW, - TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_OF | TSDB_FUNCSTATE_METRIC | TSDB_FUNCSTATE_NEED_TS | - TSDB_FUNCSTATE_SELECTIVITY, - first_last_function_setup, last_row_function, noop, no_next_step, last_row_finalizer, noop, - last_dist_func_second_merge, data_req_load_info, - }, - { - // 11 - "top", TSDB_FUNC_TOP, TSDB_FUNC_TOP, TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_METRIC | TSDB_FUNCSTATE_OF | - TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SELECTIVITY, - top_bottom_function_setup, top_function, top_function_f, no_next_step, top_bottom_func_finalizer, - top_func_merge, top_func_second_merge, data_req_load_info, - }, - { - // 12 - "bottom", TSDB_FUNC_BOTTOM, TSDB_FUNC_BOTTOM, TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_METRIC | TSDB_FUNCSTATE_OF | - TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SELECTIVITY, - top_bottom_function_setup, bottom_function, bottom_function_f, no_next_step, top_bottom_func_finalizer, - bottom_func_merge, bottom_func_second_merge, data_req_load_info, - }, - { - // 13 - "spread", TSDB_FUNC_SPREAD, TSDB_FUNC_SPREAD, TSDB_BASE_FUNC_SO, spread_function_setup, spread_function, - spread_function_f, no_next_step, spread_function_finalizer, spread_func_merge, spread_func_sec_merge, - count_load_data_info, - }, - { - // 14 - "twa", TSDB_FUNC_TWA, TSDB_FUNC_TWA, TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS, twa_function_setup, - twa_function, twa_function_f, no_next_step, twa_function_finalizer, twa_func_merge, twa_function_copy, - data_req_load_info, - }, - { - // 15 - "leastsquares", TSDB_FUNC_LEASTSQR, TSDB_FUNC_INVALID_ID, - TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STREAM | TSDB_FUNCSTATE_OF, leastsquares_function_setup, - leastsquares_function, leastsquares_function_f, no_next_step, leastsquares_finalizer, noop, noop, - data_req_load_info, - }, - { - // 16 - "ts", TSDB_FUNC_TS, TSDB_FUNC_TS, TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS, function_setup, - date_col_output_function, date_col_output_function, no_next_step, noop, copy_function, copy_function, - no_data_info, - }, - { - // 17 - "ts", TSDB_FUNC_TS_DUMMY, TSDB_FUNC_TS_DUMMY, TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS, function_setup, noop, - noop, no_next_step, noop, copy_function, copy_function, data_req_load_info, - }, - { - // 18 - "tag", TSDB_FUNC_TAG_DUMMY, TSDB_FUNC_TAG_DUMMY, TSDB_BASE_FUNC_SO, function_setup, tag_function, noop, - no_next_step, noop, copy_function, copy_function, no_data_info, - }, - { - // 19 - "ts", TSDB_FUNC_TS_COMP, TSDB_FUNC_TS_COMP, TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_NEED_TS, ts_comp_function_setup, - ts_comp_function, ts_comp_function_f, no_next_step, ts_comp_finalize, copy_function, copy_function, - data_req_load_info, - }, - { - // 20 - "tag", TSDB_FUNC_TAG, TSDB_FUNC_TAG, TSDB_BASE_FUNC_SO, function_setup, tag_function, tag_function_f, - no_next_step, noop, copy_function, copy_function, no_data_info, - }, - { - // 21, column project sql function - "colprj", TSDB_FUNC_PRJ, TSDB_FUNC_PRJ, TSDB_BASE_FUNC_MO | TSDB_FUNCSTATE_NEED_TS, function_setup, - col_project_function, col_project_function_f, no_next_step, noop, copy_function, copy_function, - data_req_load_info, - }, - { - // 22, multi-output, tag function has only one result - "tagprj", TSDB_FUNC_TAGPRJ, TSDB_FUNC_TAGPRJ, TSDB_BASE_FUNC_MO, function_setup, tag_project_function, - tag_project_function_f, no_next_step, noop, copy_function, copy_function, no_data_info, - }, - { - // 23 - "arithmetic", TSDB_FUNC_ARITHM, TSDB_FUNC_ARITHM, - TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_METRIC | TSDB_FUNCSTATE_NEED_TS, function_setup, arithmetic_function, - arithmetic_function_f, no_next_step, noop, copy_function, copy_function, data_req_load_info, - }, - { - // 24 - "diff", TSDB_FUNC_DIFF, TSDB_FUNC_INVALID_ID, TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_NEED_TS, diff_function_setup, - diff_function, diff_function_f, no_next_step, noop, noop, noop, data_req_load_info, - }, - // distributed version used in two-stage aggregation processes - { - // 25 - "first_dist", TSDB_FUNC_FIRST_DST, TSDB_FUNC_FIRST_DST, - TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SELECTIVITY, first_last_function_setup, - first_dist_function, first_dist_function_f, no_next_step, function_finalizer, first_dist_func_merge, - first_dist_func_second_merge, first_dist_data_req_info, - }, - { - // 26 - "last_dist", TSDB_FUNC_LAST_DST, TSDB_FUNC_LAST_DST, - TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SELECTIVITY, first_last_function_setup, - last_dist_function, last_dist_function_f, no_next_step, function_finalizer, last_dist_func_merge, - last_dist_func_second_merge, last_dist_data_req_info, - }, - { - // 27 - "interp", TSDB_FUNC_INTERP, TSDB_FUNC_INTERP, - TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_OF | TSDB_FUNCSTATE_METRIC | TSDB_FUNCSTATE_NEED_TS, function_setup, - interp_function, - do_sum_f, // todo filter handle - no_next_step, noop, noop, copy_function, no_data_info, - }}; +SQLAggFuncElem aAggs[28] = {{ + // 0, count function does not invoke the finalize function + "count", + TSDB_FUNC_COUNT, + TSDB_FUNC_COUNT, + TSDB_BASE_FUNC_SO, + function_setup, + count_function, + count_function_f, + no_next_step, + noop, + count_func_merge, + count_func_merge, + count_load_data_info, + }, + { + // 1 + "sum", + TSDB_FUNC_SUM, + TSDB_FUNC_SUM, + TSDB_BASE_FUNC_SO, + function_setup, + sum_function, + sum_function_f, + no_next_step, + function_finalizer, + sum_func_merge, + sum_func_second_merge, + precal_req_load_info, + }, + { + // 2 + "avg", + TSDB_FUNC_AVG, + TSDB_FUNC_AVG, + TSDB_BASE_FUNC_SO, + function_setup, + avg_function, + avg_function_f, + no_next_step, + avg_finalizer, + avg_func_merge, + avg_func_second_merge, + precal_req_load_info, + }, + { + // 3 + "min", + TSDB_FUNC_MIN, + TSDB_FUNC_MIN, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, + min_func_setup, + min_function, + min_function_f, + no_next_step, + function_finalizer, + min_func_merge, + min_func_second_merge, + precal_req_load_info, + }, + { + // 4 + "max", + TSDB_FUNC_MAX, + TSDB_FUNC_MAX, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, + max_func_setup, + max_function, + max_function_f, + no_next_step, + function_finalizer, + max_func_merge, + max_func_second_merge, + precal_req_load_info, + }, + { + // 5 + "stddev", + TSDB_FUNC_STDDEV, + TSDB_FUNC_INVALID_ID, + TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STREAM | TSDB_FUNCSTATE_OF, + function_setup, + stddev_function, + stddev_function_f, + stddev_next_step, + stddev_finalizer, + noop, + noop, + data_req_load_info, + }, + { + // 6 + "percentile", + TSDB_FUNC_PERCT, + TSDB_FUNC_INVALID_ID, + TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STREAM | TSDB_FUNCSTATE_OF, + percentile_function_setup, + percentile_function, + percentile_function_f, + no_next_step, + percentile_finalizer, + noop, + noop, + data_req_load_info, + }, + { + // 7 + "apercentile", + TSDB_FUNC_APERCT, + TSDB_FUNC_APERCT, + TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STREAM | TSDB_FUNCSTATE_OF | TSDB_FUNCSTATE_METRIC, + apercentile_function_setup, + apercentile_function, + apercentile_function_f, + no_next_step, + apercentile_finalizer, + apercentile_func_merge, + apercentile_func_second_merge, + data_req_load_info, + }, + { + // 8 + "first", + TSDB_FUNC_FIRST, + TSDB_FUNC_FIRST_DST, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, + function_setup, + first_function, + first_function_f, + no_next_step, + function_finalizer, + noop, + noop, + first_data_req_info, + }, + { + // 9 + "last", + TSDB_FUNC_LAST, + TSDB_FUNC_LAST_DST, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_SELECTIVITY, + function_setup, + last_function, + last_function_f, + no_next_step, + function_finalizer, + noop, + noop, + last_data_req_info, + }, + { + // 10 + "last_row", + TSDB_FUNC_LAST_ROW, + TSDB_FUNC_LAST_ROW, + TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_OF | TSDB_FUNCSTATE_METRIC | TSDB_FUNCSTATE_NEED_TS | + TSDB_FUNCSTATE_SELECTIVITY, + first_last_function_setup, + last_row_function, + noop, + no_next_step, + last_row_finalizer, + noop, + last_dist_func_second_merge, + data_req_load_info, + }, + { + // 11 + "top", + TSDB_FUNC_TOP, + TSDB_FUNC_TOP, + TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_METRIC | TSDB_FUNCSTATE_OF | TSDB_FUNCSTATE_NEED_TS | + TSDB_FUNCSTATE_SELECTIVITY, + top_bottom_function_setup, + top_function, + top_function_f, + no_next_step, + top_bottom_func_finalizer, + top_func_merge, + top_func_second_merge, + data_req_load_info, + }, + { + // 12 + "bottom", + TSDB_FUNC_BOTTOM, + TSDB_FUNC_BOTTOM, + TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_METRIC | TSDB_FUNCSTATE_OF | TSDB_FUNCSTATE_NEED_TS | + TSDB_FUNCSTATE_SELECTIVITY, + top_bottom_function_setup, + bottom_function, + bottom_function_f, + no_next_step, + top_bottom_func_finalizer, + bottom_func_merge, + bottom_func_second_merge, + data_req_load_info, + }, + { + // 13 + "spread", + TSDB_FUNC_SPREAD, + TSDB_FUNC_SPREAD, + TSDB_BASE_FUNC_SO, + spread_function_setup, + spread_function, + spread_function_f, + no_next_step, + spread_function_finalizer, + spread_func_merge, + spread_func_sec_merge, + count_load_data_info, + }, + { + // 14 + "twa", + TSDB_FUNC_TWA, + TSDB_FUNC_TWA, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS, + twa_function_setup, + twa_function, + twa_function_f, + no_next_step, + twa_function_finalizer, + twa_func_merge, + twa_function_copy, + data_req_load_info, + }, + { + // 15 + "leastsquares", + TSDB_FUNC_LEASTSQR, + TSDB_FUNC_INVALID_ID, + TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STREAM | TSDB_FUNCSTATE_OF, + leastsquares_function_setup, + leastsquares_function, + leastsquares_function_f, + no_next_step, + leastsquares_finalizer, + noop, + noop, + data_req_load_info, + }, + { + // 16 + "ts", + TSDB_FUNC_TS, + TSDB_FUNC_TS, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS, + function_setup, + date_col_output_function, + date_col_output_function, + no_next_step, + noop, + copy_function, + copy_function, + no_data_info, + }, + { + // 17 + "ts", + TSDB_FUNC_TS_DUMMY, + TSDB_FUNC_TS_DUMMY, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS, + function_setup, + noop, + noop, + no_next_step, + noop, + copy_function, + copy_function, + data_req_load_info, + }, + { + // 18 + "tag", + TSDB_FUNC_TAG_DUMMY, + TSDB_FUNC_TAG_DUMMY, + TSDB_BASE_FUNC_SO, + function_setup, + tag_function, + noop, + no_next_step, + noop, + copy_function, + copy_function, + no_data_info, + }, + { + // 19 + "ts", + TSDB_FUNC_TS_COMP, + TSDB_FUNC_TS_COMP, + TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_NEED_TS, + ts_comp_function_setup, + ts_comp_function, + ts_comp_function_f, + no_next_step, + ts_comp_finalize, + copy_function, + copy_function, + data_req_load_info, + }, + { + // 20 + "tag", + TSDB_FUNC_TAG, + TSDB_FUNC_TAG, + TSDB_BASE_FUNC_SO, + function_setup, + tag_function, + tag_function_f, + no_next_step, + noop, + copy_function, + copy_function, + no_data_info, + }, + { + // 21, column project sql function + "colprj", + TSDB_FUNC_PRJ, + TSDB_FUNC_PRJ, + TSDB_BASE_FUNC_MO | TSDB_FUNCSTATE_NEED_TS, + function_setup, + col_project_function, + col_project_function_f, + no_next_step, + noop, + copy_function, + copy_function, + data_req_load_info, + }, + { + // 22, multi-output, tag function has only one result + "tagprj", + TSDB_FUNC_TAGPRJ, + TSDB_FUNC_TAGPRJ, + TSDB_BASE_FUNC_MO, + function_setup, + tag_project_function, + tag_project_function_f, + no_next_step, + noop, + copy_function, + copy_function, + no_data_info, + }, + { + // 23 + "arithmetic", + TSDB_FUNC_ARITHM, + TSDB_FUNC_ARITHM, + TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_METRIC | TSDB_FUNCSTATE_NEED_TS, + function_setup, + arithmetic_function, + arithmetic_function_f, + no_next_step, + noop, + copy_function, + copy_function, + data_req_load_info, + }, + { + // 24 + "diff", + TSDB_FUNC_DIFF, + TSDB_FUNC_INVALID_ID, + TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_NEED_TS, + diff_function_setup, + diff_function, + diff_function_f, + no_next_step, + noop, + noop, + noop, + data_req_load_info, + }, + // distributed version used in two-stage aggregation processes + { + // 25 + "first_dist", + TSDB_FUNC_FIRST_DST, + TSDB_FUNC_FIRST_DST, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SELECTIVITY, + first_last_function_setup, + first_dist_function, + first_dist_function_f, + no_next_step, + function_finalizer, + first_dist_func_merge, + first_dist_func_second_merge, + first_dist_data_req_info, + }, + { + // 26 + "last_dist", + TSDB_FUNC_LAST_DST, + TSDB_FUNC_LAST_DST, + TSDB_BASE_FUNC_SO | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SELECTIVITY, + first_last_function_setup, + last_dist_function, + last_dist_function_f, + no_next_step, + function_finalizer, + last_dist_func_merge, + last_dist_func_second_merge, + last_dist_data_req_info, + }, + { + // 27 + "interp", + TSDB_FUNC_INTERP, + TSDB_FUNC_INTERP, + TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_OF | TSDB_FUNCSTATE_METRIC | TSDB_FUNCSTATE_NEED_TS, + function_setup, + interp_function, + do_sum_f, // todo filter handle + no_next_step, + noop, + noop, + copy_function, + no_data_info, + }}; diff --git a/src/client/src/tscJoinProcess.c b/src/client/src/tscJoinProcess.c index 4965498eff..24d8ad902a 100644 --- a/src/client/src/tscJoinProcess.c +++ b/src/client/src/tscJoinProcess.c @@ -13,13 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include - #include "os.h" #include "tcache.h" #include "tscJoinProcess.h" @@ -360,7 +353,7 @@ static void doQuitSubquery(SSqlObj* pParentSql) { } static void quitAllSubquery(SSqlObj* pSqlObj, SJoinSubquerySupporter* pSupporter) { - if (__sync_add_and_fetch_32(&pSupporter->pState->numOfCompleted, 1) >= pSupporter->pState->numOfTotal) { + if (atomic_add_fetch_32(&pSupporter->pState->numOfCompleted, 1) >= pSupporter->pState->numOfTotal) { pSqlObj->res.code = abs(pSupporter->pState->code); tscError("%p all subquery return and query failed, global code:%d", pSqlObj, pSqlObj->res.code); @@ -419,7 +412,7 @@ static void joinRetrieveCallback(void* param, TAOS_RES* tres, int numOfRows) { taos_fetch_rows_a(tres, joinRetrieveCallback, param); } else if (numOfRows == 0) { // no data from this vnode anymore - if (__sync_add_and_fetch_32(&pSupporter->pState->numOfCompleted, 1) >= pSupporter->pState->numOfTotal) { + if (atomic_add_fetch_32(&pSupporter->pState->numOfCompleted, 1) >= pSupporter->pState->numOfTotal) { if (pSupporter->pState->code != TSDB_CODE_SUCCESS) { tscTrace("%p sub:%p, numOfSub:%d, quit from further procedure due to other queries failure", pParentSql, tres, @@ -458,7 +451,7 @@ static void joinRetrieveCallback(void* param, TAOS_RES* tres, int numOfRows) { tscError("%p retrieve failed, code:%d, index:%d", pSql, numOfRows, pSupporter->subqueryIndex); } - if (__sync_add_and_fetch_32(&pSupporter->pState->numOfCompleted, 1) >= pSupporter->pState->numOfTotal) { + if (atomic_add_fetch_32(&pSupporter->pState->numOfCompleted, 1) >= pSupporter->pState->numOfTotal) { tscTrace("%p secondary retrieve completed, global code:%d", tres, pParentSql->res.code); if (pSupporter->pState->code != TSDB_CODE_SUCCESS) { pParentSql->res.code = abs(pSupporter->pState->code); @@ -567,7 +560,7 @@ void tscJoinQueryCallback(void* param, TAOS_RES* tres, int code) { SJoinSubquerySupporter* pSupporter = (SJoinSubquerySupporter*)param; - // if (__sync_add_and_fetch_32(pSupporter->numOfComplete, 1) >= + // if (atomic_add_fetch_32(pSupporter->numOfComplete, 1) >= // pSupporter->numOfTotal) { // SSqlObj *pParentObj = pSupporter->pObj; // @@ -612,7 +605,7 @@ void tscJoinQueryCallback(void* param, TAOS_RES* tres, int code) { quitAllSubquery(pParentSql, pSupporter); } else { - if (__sync_add_and_fetch_32(&pSupporter->pState->numOfCompleted, 1) >= pSupporter->pState->numOfTotal) { + if (atomic_add_fetch_32(&pSupporter->pState->numOfCompleted, 1) >= pSupporter->pState->numOfTotal) { tscSetupOutputColumnIndex(pParentSql); if (pParentSql->fp == NULL) { diff --git a/src/client/src/tscLocal.c b/src/client/src/tscLocal.c index 9afb74fec2..d217d09588 100644 --- a/src/client/src/tscLocal.c +++ b/src/client/src/tscLocal.c @@ -13,9 +13,7 @@ * along with this program. If not, see . */ -#include -#include -#include +#include "os.h" #include "taosmsg.h" #include "tcache.h" diff --git a/src/client/src/tscParseInsert.c b/src/client/src/tscParseInsert.c index 37a32e9ebb..abebea64df 100644 --- a/src/client/src/tscParseInsert.c +++ b/src/client/src/tscParseInsert.c @@ -21,22 +21,8 @@ #pragma GCC diagnostic ignored "-Woverflow" #pragma GCC diagnostic ignored "-Wunused-variable" -#include -#include -#include - -#include -#include - -#include -#include -#include -#include -#include -#include - -#include "ihash.h" #include "os.h" +#include "ihash.h" #include "tscSecondaryMerge.h" #include "tscUtil.h" #include "tschemautil.h" @@ -205,10 +191,10 @@ int32_t tsParseOneColumnData(SSchema *pSchema, SSQLToken *pToken, char *payload, *((int8_t *)payload) = TSDB_DATA_TINYINT_NULL; } else { numType = tscToInteger(pToken, &iv, &endptr); - if (errno == ERANGE || iv > INT8_MAX || iv <= INT8_MIN) { - INVALID_SQL_RET_MSG(msg, "data is overflow"); - } else if (TK_ILLEGAL == numType) { + if (TK_ILLEGAL == numType) { INVALID_SQL_RET_MSG(msg, "data is illegal"); + } else if (errno == ERANGE || iv > INT8_MAX || iv <= INT8_MIN) { + INVALID_SQL_RET_MSG(msg, "data is overflow"); } *((int8_t *)payload) = (int8_t)iv; @@ -224,10 +210,10 @@ int32_t tsParseOneColumnData(SSchema *pSchema, SSQLToken *pToken, char *payload, *((int16_t *)payload) = TSDB_DATA_SMALLINT_NULL; } else { numType = tscToInteger(pToken, &iv, &endptr); - if (errno == ERANGE || iv > INT16_MAX || iv <= INT16_MIN) { - INVALID_SQL_RET_MSG(msg, "data is overflow"); - } else if (TK_ILLEGAL == numType) { + if (TK_ILLEGAL == numType) { INVALID_SQL_RET_MSG(msg, "data is illegal"); + } else if (errno == ERANGE || iv > INT16_MAX || iv <= INT16_MIN) { + INVALID_SQL_RET_MSG(msg, "data is overflow"); } *((int16_t *)payload) = (int16_t)iv; @@ -242,10 +228,10 @@ int32_t tsParseOneColumnData(SSchema *pSchema, SSQLToken *pToken, char *payload, *((int32_t *)payload) = TSDB_DATA_INT_NULL; } else { numType = tscToInteger(pToken, &iv, &endptr); - if (errno == ERANGE || iv > INT32_MAX || iv <= INT32_MIN) { - INVALID_SQL_RET_MSG(msg, "data is overflow"); - } else if (TK_ILLEGAL == numType) { + if (TK_ILLEGAL == numType) { INVALID_SQL_RET_MSG(msg, "data is illegal"); + } else if (errno == ERANGE || iv > INT32_MAX || iv <= INT32_MIN) { + INVALID_SQL_RET_MSG(msg, "data is overflow"); } *((int32_t *)payload) = (int32_t)iv; @@ -261,10 +247,10 @@ int32_t tsParseOneColumnData(SSchema *pSchema, SSQLToken *pToken, char *payload, *((int64_t *)payload) = TSDB_DATA_BIGINT_NULL; } else { numType = tscToInteger(pToken, &iv, &endptr); - if (errno == ERANGE || iv > INT64_MAX || iv <= INT64_MIN) { - INVALID_SQL_RET_MSG(msg, "data is overflow"); - } else if (TK_ILLEGAL == numType) { + if (TK_ILLEGAL == numType) { INVALID_SQL_RET_MSG(msg, "data is illegal"); + } else if (errno == ERANGE || iv > INT64_MAX || iv <= INT64_MIN) { + INVALID_SQL_RET_MSG(msg, "data is overflow"); } *((int64_t *)payload) = iv; diff --git a/src/client/src/tscPrepare.c b/src/client/src/tscPrepare.c index e956d6159e..03f644ac36 100644 --- a/src/client/src/tscPrepare.c +++ b/src/client/src/tscPrepare.c @@ -13,9 +13,6 @@ * along with this program. If not, see . */ -#include -#include - #include "taos.h" #include "tsclient.h" #include "tsql.h" diff --git a/src/client/src/tscProfile.c b/src/client/src/tscProfile.c index 770e61e278..350167a3b3 100644 --- a/src/client/src/tscProfile.c +++ b/src/client/src/tscProfile.c @@ -13,9 +13,6 @@ * along with this program. If not, see . */ -#include -#include - #include "os.h" #include "tlog.h" #include "tsclient.h" @@ -23,6 +20,27 @@ #include "ttimer.h" #include "tutil.h" +void tscSaveSlowQueryFp(void *handle, void *tmrId); +void *tscSlowQueryConn = NULL; +bool tscSlowQueryConnInitialized = false; +TAOS *taos_connect_a(char *ip, char *user, char *pass, char *db, int port, void (*fp)(void *, TAOS_RES *, int), + void *param, void **taos); + +void tscInitConnCb(void *param, TAOS_RES *result, int code) { + char *sql = param; + if (code < 0) { + tscError("taos:%p, slow query connect failed, code:%d", tscSlowQueryConn, code); + taos_close(tscSlowQueryConn); + tscSlowQueryConn = NULL; + tscSlowQueryConnInitialized = false; + free(sql); + } else { + tscTrace("taos:%p, slow query connect success, code:%d", tscSlowQueryConn, code); + tscSlowQueryConnInitialized = true; + tscSaveSlowQueryFp(sql, NULL); + } +} + void tscAddIntoSqlList(SSqlObj *pSql) { static uint32_t queryId = 1; @@ -47,33 +65,35 @@ void tscAddIntoSqlList(SSqlObj *pSql) { void tscSaveSlowQueryFpCb(void *param, TAOS_RES *result, int code) { if (code < 0) { - tscError("failed to save slowquery, code:%d", code); + tscError("failed to save slow query, code:%d", code); + } else { + tscTrace("success to save slow query, code:%d", code); } } void tscSaveSlowQueryFp(void *handle, void *tmrId) { char *sql = handle; - static void *taos = NULL; - if (taos == NULL) { - taos = taos_connect(NULL, "monitor", tsInternalPass, NULL, 0); - if (taos == NULL) { - tscError("failed to save slow query, can't connect to server"); + if (!tscSlowQueryConnInitialized) { + if (tscSlowQueryConn == NULL) { + tscTrace("start to init slow query connect"); + taos_connect_a(NULL, "monitor", tsInternalPass, "", 0, tscInitConnCb, sql, &tscSlowQueryConn); + } else { + tscError("taos:%p, slow query connect is already initialized", tscSlowQueryConn); free(sql); - return; } + } else { + tscTrace("taos:%p, save slow query:%s", tscSlowQueryConn, sql); + taos_query_a(tscSlowQueryConn, sql, tscSaveSlowQueryFpCb, NULL); + free(sql); } - - tscTrace("save slow query:sql", sql); - taos_query_a(taos, sql, tscSaveSlowQueryFpCb, NULL); - free(sql); } void tscSaveSlowQuery(SSqlObj *pSql) { const static int64_t SLOW_QUERY_INTERVAL = 3000000L; if (pSql->res.useconds < SLOW_QUERY_INTERVAL) return; - tscTrace("%p query time:%ld sql:%s", pSql, pSql->res.useconds, pSql->sqlstr); + tscTrace("%p query time:%lld sql:%s", pSql, pSql->res.useconds, pSql->sqlstr); char *sql = malloc(200); int len = snprintf(sql, 200, "insert into %s.slowquery values(now, '%s', %lld, %lld, '", tsMonitorDbName, diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index ae9704effa..c66d524fee 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -81,7 +81,7 @@ static bool validateIpAddress(char* ip); static bool hasUnsupportFunctionsForMetricQuery(SSqlCmd* pCmd); static bool functionCompatibleCheck(SSqlCmd* pCmd); -static void setColumnOffsetValueInResultset(SSqlCmd* pCmd); +static void setColumnOffsetValueInResultset(SSqlCmd* pCmd); static int32_t parseGroupbyClause(SSqlCmd* pCmd, tVariantList* pList); static int32_t parseIntervalClause(SSqlCmd* pCmd, SQuerySQL* pQuerySql); @@ -94,7 +94,7 @@ static int32_t parseFillClause(SSqlCmd* pCmd, SQuerySQL* pQuerySQL); static int32_t parseOrderbyClause(SSqlCmd* pCmd, SQuerySQL* pQuerySql, SSchema* pSchema, int32_t numOfCols); static int32_t tsRewriteFieldNameIfNecessary(SSqlCmd* pCmd); -static bool validateOneTags(SSqlCmd* pCmd, TAOS_FIELD* pTagField); +static bool validateOneTags(SSqlCmd* pCmd, TAOS_FIELD* pTagField); static int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo); static int32_t validateSqlFunctionInStreamSql(SSqlCmd* pCmd); static int32_t buildArithmeticExprString(tSQLExpr* pExpr, char** exprString); @@ -105,8 +105,8 @@ static int32_t validateLocalConfig(tDCLSQL* pOptions); static int32_t validateColumnName(char* name); static int32_t setKillInfo(SSqlObj* pSql, struct SSqlInfo* pInfo); -static bool hasTimestampForPointInterpQuery(SSqlCmd* pCmd); -static void updateTagColumnIndex(SSqlCmd* pCmd, int32_t tableIndex); +static bool hasTimestampForPointInterpQuery(SSqlCmd* pCmd); +static void updateTagColumnIndex(SSqlCmd* pCmd, int32_t tableIndex); static int32_t parseLimitClause(SSqlObj* pSql, SQuerySQL* pQuerySql); static int32_t parseCreateDBOptions(SCreateDBInfo* pCreateDbSql, SSqlCmd* pCmd); @@ -115,13 +115,12 @@ static int32_t getTableIndexByName(SSQLToken* pToken, SSqlCmd* pCmd, SColumnInde static int32_t optrToString(tSQLExpr* pExpr, char** exprString); static SColumnList getColumnList(int32_t num, int16_t tableIndex, int32_t columnIndex); -static int32_t getMeterIndex(SSQLToken* pTableToken, SSqlCmd* pCmd, SColumnIndex* pIndex); -static int32_t doFunctionsCompatibleCheck(SSqlObj* pSql); +static int32_t getMeterIndex(SSQLToken* pTableToken, SSqlCmd* pCmd, SColumnIndex* pIndex); +static int32_t doFunctionsCompatibleCheck(SSqlObj* pSql); static int32_t tscQueryOnlyMetricTags(SSqlCmd* pCmd, bool* queryOnMetricTags) { assert(QUERY_IS_STABLE_QUERY(pCmd->type)); - // here colIdx == -1 means the special column tbname that is the name of each table *queryOnMetricTags = true; for (int32_t i = 0; i < pCmd->fieldsInfo.numOfOutputCols; ++i) { SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); @@ -1151,7 +1150,7 @@ int32_t parseIntervalClause(SSqlCmd* pCmd, SQuerySQL* pQuerySql) { } // check the invalid sql expresssion: select count(tbname)/count(tag1)/count(tag2) from super_table interval(1d); - for(int32_t i = 0; i < pCmd->fieldsInfo.numOfOutputCols; ++i) { + for (int32_t i = 0; i < pCmd->fieldsInfo.numOfOutputCols; ++i) { SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); if (pExpr->functionId == TSDB_FUNC_COUNT && TSDB_COL_IS_TAG(pExpr->colInfo.flag)) { setErrMsg(pCmd, msg1); @@ -2791,10 +2790,14 @@ static bool functionCompatibleCheck(SSqlCmd* pCmd) { // diff function cannot be executed with other function // arithmetic function can be executed with other arithmetic functions for (int32_t i = startIdx + 1; i < pCmd->fieldsInfo.numOfOutputCols; ++i) { - int16_t functionId = tscSqlExprGet(pCmd, i)->functionId; - if (functionId == TSDB_FUNC_TAGPRJ || - functionId == TSDB_FUNC_TAG || - functionId == TSDB_FUNC_TS) { + SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); + + int16_t functionId = pExpr->functionId; + if (functionId == TSDB_FUNC_TAGPRJ || functionId == TSDB_FUNC_TAG || functionId == TSDB_FUNC_TS) { + continue; + } + + if (functionId == TSDB_FUNC_PRJ && pExpr->colInfo.colId == PRIMARYKEY_TIMESTAMP_COL_INDEX) { continue; } @@ -2803,61 +2806,6 @@ static bool functionCompatibleCheck(SSqlCmd* pCmd) { } } - // additional check for select aggfuntion(column), column1 from table_name group by(column1); - if ((pCmd->type & TSDB_QUERY_TYPE_PROJECTION_QUERY) == TSDB_QUERY_TYPE_PROJECTION_QUERY) { - bool isAggFunc = false; - for (int32_t i = 0; i < pCmd->fieldsInfo.numOfOutputCols; ++i) { - int16_t functionId = tscSqlExprGet(pCmd, i)->functionId; - - if (functionId == TSDB_FUNC_PRJ || - functionId == TSDB_FUNC_TAGPRJ || - functionId == TSDB_FUNC_TS || - functionId == TSDB_FUNC_ARITHM) { - continue; - } - - if ((aAggs[functionId].nStatus & TSDB_FUNCSTATE_SELECTIVITY) == 0) { - isAggFunc = true; - break; - } - } - - // TODO change the type, the type is not correct - if (isAggFunc) { - pCmd->type &= (~TSDB_QUERY_TYPE_PROJECTION_QUERY); - - // agg function mixed up with project query without group by exists - if (pCmd->groupbyExpr.numOfGroupCols == 0) { - return false; - } - - // get the project column - int32_t numOfPrjColumn = 0; - for (int32_t i = 0; i < pCmd->fieldsInfo.numOfOutputCols; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); - if (pExpr->functionId == TSDB_FUNC_PRJ) { - numOfPrjColumn += 1; - - bool qualifiedCol = false; - for (int32_t j = 0; j < pCmd->groupbyExpr.numOfGroupCols; ++j) { - if (pExpr->colInfo.colId == pCmd->groupbyExpr.columnInfo[j].colId) { - qualifiedCol = true; - - pExpr->param[0].i64Key = 1; // limit the output to be 1 for each state value - pExpr->numOfParams = 1; - break; - } - } - - if (!qualifiedCol) { - setErrMsg(pCmd, msg1); - return false; - } - } - } - } - } - return true; } @@ -4986,19 +4934,37 @@ int32_t validateSqlFunctionInStreamSql(SSqlCmd* pCmd) { int32_t validateFunctionsInIntervalOrGroupbyQuery(SSqlCmd* pCmd) { bool isProjectionFunction = false; - const char* msg = "column projection is not compatible with interval"; - + const char* msg1 = "column projection is not compatible with interval"; + const char* msg2 = "interval not allowed for tag queries"; + // multi-output set/ todo refactor for (int32_t k = 0; k < pCmd->fieldsInfo.numOfOutputCols; ++k) { SSqlExpr* pExpr = tscSqlExprGet(pCmd, k); + + // projection query on primary timestamp, the selectivity function needs to be present. + if (pExpr->functionId == TSDB_FUNC_PRJ && pExpr->colInfo.colId == PRIMARYKEY_TIMESTAMP_COL_INDEX) { + bool hasSelectivity = false; + for(int32_t j = 0; j < pCmd->fieldsInfo.numOfOutputCols; ++j) { + SSqlExpr* pEx = tscSqlExprGet(pCmd, j); + if ((aAggs[pEx->functionId].nStatus & TSDB_FUNCSTATE_SELECTIVITY) == TSDB_FUNCSTATE_SELECTIVITY) { + hasSelectivity = true; + break; + } + } + + if (hasSelectivity) { + continue; + } + } + if (pExpr->functionId == TSDB_FUNC_PRJ || pExpr->functionId == TSDB_FUNC_DIFF || - pExpr->functionId == TSDB_FUNC_ARITHM) { + pExpr->functionId == TSDB_FUNC_ARITHM) { isProjectionFunction = true; } } if (isProjectionFunction) { - setErrMsg(pCmd, msg); + setErrMsg(pCmd, msg1); } return isProjectionFunction == true ? TSDB_CODE_INVALID_SQL : TSDB_CODE_SUCCESS; @@ -5164,8 +5130,7 @@ int32_t parseLimitClause(SSqlObj* pSql, SQuerySQL* pQuerySql) { if (UTIL_METER_IS_METRIC(pMeterMetaInfo)) { bool queryOnTags = false; - int32_t ret = tscQueryOnlyMetricTags(pCmd, &queryOnTags); - if (ret != TSDB_CODE_SUCCESS) { + if (tscQueryOnlyMetricTags(pCmd, &queryOnTags) != TSDB_CODE_SUCCESS) { return TSDB_CODE_INVALID_SQL; } @@ -5382,8 +5347,8 @@ static void doUpdateSqlFunctionForTagPrj(SSqlCmd* pCmd) { } } - int16_t resType = 0; - int16_t resBytes = 0; + int16_t resType = 0; + int16_t resBytes = 0; SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); SSchema* pSchema = tsGetSchema(pMeterMetaInfo->pMeterMeta); @@ -5398,6 +5363,27 @@ static void doUpdateSqlFunctionForTagPrj(SSqlCmd* pCmd) { } } +static void doUpdateSqlFunctionForColPrj(SSqlCmd* pCmd) { + for (int32_t i = 0; i < pCmd->fieldsInfo.numOfOutputCols; ++i) { + SSqlExpr *pExpr = tscSqlExprGet(pCmd, i); + if (pExpr->functionId == TSDB_FUNC_PRJ) { + + bool qualifiedCol = false; + for (int32_t j = 0; j < pCmd->groupbyExpr.numOfGroupCols; ++j) { + if (pExpr->colInfo.colId == pCmd->groupbyExpr.columnInfo[j].colId) { + qualifiedCol = true; + + pExpr->param[0].i64Key = 1; // limit the output to be 1 for each state value + pExpr->numOfParams = 1; + break; + } + } + + assert(qualifiedCol); + } + } +} + static bool tagColumnInGroupby(SSqlGroupbyExpr* pGroupbyExpr, int16_t columnId) { for (int32_t j = 0; j < pGroupbyExpr->numOfGroupCols; ++j) { if (columnId == pGroupbyExpr->columnInfo[j].colId && pGroupbyExpr->columnInfo[j].flag == TSDB_COL_TAG) { @@ -5462,9 +5448,10 @@ static void updateTagPrjFunction(SSqlCmd* pCmd) { static int32_t checkUpdateTagPrjFunctions(SSqlCmd* pCmd) { const char* msg1 = "only one selectivity function allowed in presence of tags function"; const char* msg2 = "functions not allowed"; - + const char* msg3 = "aggregation function should not be mixed up with projection"; + bool tagColExists = false; - int16_t numOfTimestamp = 0; // primary timestamp column + int16_t numOfTimestamp = 0; // primary timestamp column int16_t numOfSelectivity = 0; int16_t numOfAggregation = 0; @@ -5476,24 +5463,24 @@ static int32_t checkUpdateTagPrjFunctions(SSqlCmd* pCmd) { break; } } - - if (tagColExists) { // check if the selectivity function exists - for (int32_t i = 0; i < pCmd->fieldsInfo.numOfOutputCols; ++i) { - int16_t functionId = tscSqlExprGet(pCmd, i)->functionId; - if (functionId == TSDB_FUNC_TAGPRJ || functionId == TSDB_FUNC_PRJ || functionId == TSDB_FUNC_TS) { - continue; - } - - if ((aAggs[functionId].nStatus & TSDB_FUNCSTATE_SELECTIVITY) != 0) { - numOfSelectivity++; - } else { - numOfAggregation++; - } + + for (int32_t i = 0; i < pCmd->fieldsInfo.numOfOutputCols; ++i) { + int16_t functionId = tscSqlExprGet(pCmd, i)->functionId; + if (functionId == TSDB_FUNC_TAGPRJ || functionId == TSDB_FUNC_PRJ || functionId == TSDB_FUNC_TS) { + continue; } - + + if ((aAggs[functionId].nStatus & TSDB_FUNCSTATE_SELECTIVITY) != 0) { + numOfSelectivity++; + } else { + numOfAggregation++; + } + } + + if (tagColExists) { // check if the selectivity function exists // When the tag projection function on tag column that is not in the group by clause, aggregation function and // selectivity function exist in select clause is not allowed. - if(numOfAggregation > 0) { + if (numOfAggregation > 0) { setErrMsg(pCmd, msg1); return TSDB_CODE_INVALID_SQL; } @@ -5503,6 +5490,7 @@ static int32_t checkUpdateTagPrjFunctions(SSqlCmd* pCmd) { */ if (numOfSelectivity == 1) { doUpdateSqlFunctionForTagPrj(pCmd); + doUpdateSqlFunctionForColPrj(pCmd); } else if (numOfSelectivity > 1) { /* * If more than one selectivity functions exist, all the selectivity functions must be last_row. @@ -5521,6 +5509,20 @@ static int32_t checkUpdateTagPrjFunctions(SSqlCmd* pCmd) { } doUpdateSqlFunctionForTagPrj(pCmd); + doUpdateSqlFunctionForColPrj(pCmd); + } + } else { + if ((pCmd->type & TSDB_QUERY_TYPE_PROJECTION_QUERY) == TSDB_QUERY_TYPE_PROJECTION_QUERY) { + if (numOfAggregation > 0 && pCmd->groupbyExpr.numOfGroupCols == 0) { + setErrMsg(pCmd, msg3); + return TSDB_CODE_INVALID_SQL; + } + + if (numOfAggregation > 0 || numOfSelectivity > 0) { + // clear the projection type flag + pCmd->type &= (~TSDB_QUERY_TYPE_PROJECTION_QUERY); + doUpdateSqlFunctionForColPrj(pCmd); + } } } @@ -5598,14 +5600,14 @@ int32_t doFunctionsCompatibleCheck(SSqlObj* pSql) { const char* msg2 = "interval not allowed in group by normal column"; const char* msg3 = "group by not allowed on projection query"; const char* msg4 = "tags retrieve not compatible with group by"; - const char* msg5 = "retrieve tags not compatible with group by "; + const char* msg5 = "retrieve tags not compatible with group by or interval query"; SSqlCmd* pCmd = &pSql->cmd; SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0); // only retrieve tags, group by is not supportted if (pCmd->command == TSDB_SQL_RETRIEVE_TAGS) { - if (pCmd->groupbyExpr.numOfGroupCols > 0) { + if (pCmd->groupbyExpr.numOfGroupCols > 0 || pCmd->nAggTimeInterval > 0) { setErrMsg(pCmd, msg5); return TSDB_CODE_INVALID_SQL; } else { @@ -5634,7 +5636,7 @@ int32_t doFunctionsCompatibleCheck(SSqlObj* pSql) { * group by normal columns. * Check if the column projection is identical to the group by column or not */ - if (functId == TSDB_FUNC_PRJ) { + if (functId == TSDB_FUNC_PRJ && pExpr->colInfo.colId != PRIMARYKEY_TIMESTAMP_COL_INDEX) { bool qualified = false; for (int32_t j = 0; j < pCmd->groupbyExpr.numOfGroupCols; ++j) { SColIndexEx* pColIndex = &pCmd->groupbyExpr.columnInfo[j]; @@ -5650,7 +5652,7 @@ int32_t doFunctionsCompatibleCheck(SSqlObj* pSql) { } if (IS_MULTIOUTPUT(aAggs[functId].nStatus) && functId != TSDB_FUNC_TOP && functId != TSDB_FUNC_BOTTOM && - functId != TSDB_FUNC_TAGPRJ) { + functId != TSDB_FUNC_TAGPRJ && functId != TSDB_FUNC_PRJ) { setErrMsg(pCmd, msg1); return TSDB_CODE_INVALID_SQL; } @@ -5678,6 +5680,8 @@ int32_t doFunctionsCompatibleCheck(SSqlObj* pSql) { setErrMsg(pCmd, msg3); return TSDB_CODE_INVALID_SQL; } + + return TSDB_CODE_SUCCESS; } else { return checkUpdateTagPrjFunctions(pCmd); } diff --git a/src/client/src/tscSQLParserImpl.c b/src/client/src/tscSQLParserImpl.c index 7e1b4a7cf1..4fefe1665c 100644 --- a/src/client/src/tscSQLParserImpl.c +++ b/src/client/src/tscSQLParserImpl.c @@ -13,13 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include - #include "os.h" #include "tglobalcfg.h" #include "tsql.h" diff --git a/src/client/src/tscSchemaUtil.c b/src/client/src/tscSchemaUtil.c index 9728811ae7..5e50c27ab1 100644 --- a/src/client/src/tscSchemaUtil.c +++ b/src/client/src/tscSchemaUtil.c @@ -13,10 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include - #include "os.h" #include "taosmsg.h" #include "tschemautil.h" diff --git a/src/client/src/tscSecondaryMerge.c b/src/client/src/tscSecondaryMerge.c index ac2638f635..c1fe0c8674 100644 --- a/src/client/src/tscSecondaryMerge.c +++ b/src/client/src/tscSecondaryMerge.c @@ -13,13 +13,8 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include "tlosertree.h" +#include "os.h" #include "tlosertree.h" #include "tscSecondaryMerge.h" #include "tscUtil.h" @@ -437,11 +432,10 @@ void tscDestroyLocalReducer(SSqlObj *pSql) { SSqlCmd *pCmd = &pSql->cmd; // there is no more result, so we release all allocated resource - SLocalReducer *pLocalReducer = - (SLocalReducer *)__sync_val_compare_and_swap_64(&pRes->pLocalReducer, pRes->pLocalReducer, 0); + SLocalReducer *pLocalReducer = (SLocalReducer*)atomic_exchange_ptr(&pRes->pLocalReducer, NULL); if (pLocalReducer != NULL) { int32_t status = 0; - while ((status = __sync_val_compare_and_swap_32(&pLocalReducer->status, TSC_LOCALREDUCE_READY, + while ((status = atomic_val_compare_exchange_32(&pLocalReducer->status, TSC_LOCALREDUCE_READY, TSC_LOCALREDUCE_TOBE_FREED)) == TSC_LOCALREDUCE_IN_PROGRESS) { taosMsleep(100); tscTrace("%p waiting for delete procedure, status: %d", pSql, status); @@ -1333,7 +1327,7 @@ int32_t tscLocalDoReduce(SSqlObj *pSql) { // set the data merge in progress int32_t prevStatus = - __sync_val_compare_and_swap_32(&pLocalReducer->status, TSC_LOCALREDUCE_READY, TSC_LOCALREDUCE_IN_PROGRESS); + atomic_val_compare_exchange_32(&pLocalReducer->status, TSC_LOCALREDUCE_READY, TSC_LOCALREDUCE_IN_PROGRESS); if (prevStatus != TSC_LOCALREDUCE_READY || pLocalReducer == NULL) { assert(prevStatus == TSC_LOCALREDUCE_TOBE_FREED); // it is in tscDestroyLocalReducer function already return TSDB_CODE_SUCCESS; diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index 6edec3d477..436bfa327a 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -13,12 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include - #include "os.h" #include "tcache.h" #include "trpc.h" @@ -140,6 +134,7 @@ void tscProcessActivityTimer(void *handle, void *tmrId) { tscProcessSql(pObj->pHb); } +//TODO HANDLE error from mgmt void tscGetConnToMgmt(SSqlObj *pSql, uint8_t *pCode) { STscObj *pTscObj = pSql->pTscObj; #ifdef CLUSTER @@ -169,10 +164,11 @@ void tscGetConnToMgmt(SSqlObj *pSql, uint8_t *pCode) { connInit.spi = 1; connInit.encrypt = 0; connInit.secret = pSql->pTscObj->pass; + #ifdef CLUSTER connInit.peerIp = tscMgmtIpList.ipstr[pSql->index]; #else - connInit.peerIp = tsServerIpStr; + connInit.peerIp = tsServerIpStr; #endif thandle = taosOpenRpcConn(&connInit, pCode); } @@ -228,7 +224,7 @@ void tscGetConnToVnode(SSqlObj *pSql, uint8_t *pCode) { (pSql->index) = (pSql->index + 1) % TSDB_VNODES_SUPPORT; continue; } - *pCode = 0; + *pCode = TSDB_CODE_SUCCESS; void *thandle = taosGetConnFromCache(tscConnCache, pVPeersDesc[pSql->index].ip, pVPeersDesc[pSql->index].vnode, pTscObj->user); @@ -254,7 +250,7 @@ void tscGetConnToVnode(SSqlObj *pSql, uint8_t *pCode) { pSql->thandle = thandle; pSql->ip = pVPeersDesc[pSql->index].ip; pSql->vnode = pVPeersDesc[pSql->index].vnode; - tscTrace("%p vnode:%d ip:0x%x index:%d is picked up, pConn:%p", pSql, pVPeersDesc[pSql->index].vnode, + tscTrace("%p vnode:%d ip:%p index:%d is picked up, pConn:%p", pSql, pVPeersDesc[pSql->index].vnode, pVPeersDesc[pSql->index].ip, pSql->index, pSql->thandle); #else *pCode = 0; @@ -284,6 +280,15 @@ void tscGetConnToVnode(SSqlObj *pSql, uint8_t *pCode) { break; } + + // the pSql->res.code is the previous error(status) code. + if (pSql->thandle == NULL && pSql->retry >= pSql->maxRetry) { + if (pSql->res.code != TSDB_CODE_SUCCESS && pSql->res.code != TSDB_CODE_ACTION_IN_PROGRESS) { + *pCode = pSql->res.code; + } + + tscError("%p reach the max retry:%d, code:%d", pSql, pSql->retry, *pCode); + } } int tscSendMsgToServer(SSqlObj *pSql) { @@ -319,11 +324,19 @@ int tscSendMsgToServer(SSqlObj *pSql) { char *pStart = taosBuildReqHeader(pSql->thandle, pSql->cmd.msgType, buf); if (pStart) { + /* + * this SQL object may be released by other thread due to the completion of this query even before the log + * is dumped to log file. So the signature needs to be kept in a local variable. + */ + uint64_t signature = (uint64_t) pSql->signature; if (tscUpdateVnodeMsg[pSql->cmd.command]) (*tscUpdateVnodeMsg[pSql->cmd.command])(pSql, buf); + int ret = taosSendMsgToPeerH(pSql->thandle, pStart, pSql->cmd.payloadLen, pSql); - - if (ret >= 0) code = 0; - tscTrace("%p send msg ret:%d code:%d sig:%p", pSql, ret, code, pSql->signature); + if (ret >= 0) { + code = 0; + } + + tscTrace("%p send msg ret:%d code:%d sig:%p", pSql, ret, code, signature); } } @@ -391,14 +404,11 @@ void *tscProcessMsgFromServer(char *msg, void *ahandle, void *thandle) { // for single node situation, do NOT try next index #endif pSql->thandle = NULL; - // todo taos_stop_query() in async model /* * in case of - * 1. query cancelled(pRes->code != TSDB_CODE_QUERY_CANCELLED), do NOT re-issue the - * request to server. - * 2. retrieve, do NOT re-issue the retrieve request since the qhandle may - * have been released by server + * 1. query cancelled(pRes->code != TSDB_CODE_QUERY_CANCELLED), do NOT re-issue the request to server. + * 2. retrieve, do NOT re-issue the retrieve request since the qhandle may have been released by server */ if (pCmd->command != TSDB_SQL_FETCH && pCmd->command != TSDB_SQL_RETRIEVE && pCmd->command != TSDB_SQL_KILL_QUERY && pRes->code != TSDB_CODE_QUERY_CANCELLED) { @@ -424,8 +434,11 @@ void *tscProcessMsgFromServer(char *msg, void *ahandle, void *thandle) { } } } else { + uint16_t rspCode = pMsg->content[0]; #ifdef CLUSTER - if (pMsg->content[0] == TSDB_CODE_REDIRECT) { + + + if (rspCode == TSDB_CODE_REDIRECT) { tscTrace("%p it shall be redirected!", pSql); taosAddConnIntoCache(tscConnCache, thandle, pSql->ip, pSql->vnode, pObj->user); pSql->thandle = NULL; @@ -439,28 +452,23 @@ void *tscProcessMsgFromServer(char *msg, void *ahandle, void *thandle) { code = tscSendMsgToServer(pSql); if (code == 0) return pSql; msg = NULL; - } else if (pMsg->content[0] == TSDB_CODE_NOT_ACTIVE_SESSION || pMsg->content[0] == TSDB_CODE_NETWORK_UNAVAIL || - pMsg->content[0] == TSDB_CODE_INVALID_SESSION_ID) { + } else if (rspCode == TSDB_CODE_NOT_ACTIVE_TABLE || rspCode == TSDB_CODE_INVALID_TABLE_ID || + rspCode == TSDB_CODE_INVALID_VNODE_ID || rspCode == TSDB_CODE_NOT_ACTIVE_VNODE || + rspCode == TSDB_CODE_NETWORK_UNAVAIL) { #else - if (pMsg->content[0] == TSDB_CODE_NOT_ACTIVE_SESSION || pMsg->content[0] == TSDB_CODE_NETWORK_UNAVAIL || - pMsg->content[0] == TSDB_CODE_INVALID_SESSION_ID) { + if (rspCode == TSDB_CODE_NOT_ACTIVE_TABLE || rspCode == TSDB_CODE_INVALID_TABLE_ID || + rspCode == TSDB_CODE_INVALID_VNODE_ID || rspCode == TSDB_CODE_NOT_ACTIVE_VNODE || + rspCode == TSDB_CODE_NETWORK_UNAVAIL) { #endif pSql->thandle = NULL; taosAddConnIntoCache(tscConnCache, thandle, pSql->ip, pSql->vnode, pObj->user); - - if (pMeterMetaInfo != NULL && UTIL_METER_IS_METRIC(pMeterMetaInfo) && - pMsg->content[0] == TSDB_CODE_NOT_ACTIVE_SESSION) { + + if ((pCmd->command == TSDB_SQL_INSERT || pCmd->command == TSDB_SQL_SELECT) && + (rspCode == TSDB_CODE_INVALID_TABLE_ID || rspCode == TSDB_CODE_INVALID_VNODE_ID)) { /* - * for metric query, in case of any meter missing during query, sub-query of metric query will failed, - * causing metric query failed, and return TSDB_CODE_METRICMETA_EXPIRED code to app - */ - tscTrace("%p invalid meters id cause metric query failed, code:%d", pSql, pMsg->content[0]); - code = TSDB_CODE_METRICMETA_EXPIRED; - } else if ((pCmd->command == TSDB_SQL_INSERT || pCmd->command == TSDB_SQL_SELECT) && - pMsg->content[0] == TSDB_CODE_INVALID_SESSION_ID) { - /* - * session id is invalid(e.g., less than 0 or larger than maximum session per - * vnode) in submit/query msg, no retry + * In case of the insert/select operations, the invalid table(vnode) id means + * the submit/query msg is invalid, renew meter meta will not help to fix this problem, + * so return the invalid_query_msg to client directly. */ code = TSDB_CODE_INVALID_QUERY_MSG; } else if (pCmd->command == TSDB_SQL_CONNECT) { @@ -468,9 +476,11 @@ void *tscProcessMsgFromServer(char *msg, void *ahandle, void *thandle) { } else if (pCmd->command == TSDB_SQL_HB) { code = TSDB_CODE_NOT_READY; } else { - tscTrace("%p it shall renew meter meta, code:%d", pSql, pMsg->content[0]); + tscTrace("%p it shall renew meter meta, code:%d", pSql, rspCode); + pSql->maxRetry = TSDB_VNODES_SUPPORT * 2; - + pSql->res.code = (uint8_t) rspCode; // keep the previous error code + code = tscRenewMeterMeta(pSql, pMeterMetaInfo->name); if (code == TSDB_CODE_ACTION_IN_PROGRESS) return pSql; @@ -482,7 +492,7 @@ void *tscProcessMsgFromServer(char *msg, void *ahandle, void *thandle) { msg = NULL; } else { // for other error set and return to invoker - code = pMsg->content[0]; + code = rspCode; } } @@ -568,7 +578,7 @@ void *tscProcessMsgFromServer(char *msg, void *ahandle, void *thandle) { void *taosres = tscKeepConn[command] ? pSql : NULL; code = pRes->code ? -pRes->code : pRes->numOfRows; - tscTrace("%p Async SQL result:%d taosres:%p", pSql, code, taosres); + tscTrace("%p Async SQL result:%d res:%p", pSql, code, taosres); /* * Whether to free sqlObj or not should be decided before call the user defined function, since this SqlObj @@ -729,9 +739,16 @@ int tscProcessSql(SSqlObj *pSql) { #else pSql->maxRetry = 2; #endif + + // the pMeterMetaInfo cannot be NULL + if (pMeterMetaInfo == NULL) { + pSql->res.code = TSDB_CODE_OTHERS; + return pSql->res.code; + } + if (UTIL_METER_IS_NOMRAL_METER(pMeterMetaInfo)) { pSql->index = pMeterMetaInfo->pMeterMeta->index; - } else { // it must be the parent SSqlObj for metric query + } else { // it must be the parent SSqlObj for super table query if ((pSql->cmd.type & TSDB_QUERY_TYPE_SUBQUERY) != 0) { int32_t idx = pSql->cmd.vnodeIdx; SVnodeSidList *pSidList = tscGetVnodeSidList(pMeterMetaInfo->pMetricMeta, idx); @@ -1029,13 +1046,13 @@ static void tscHandleSubRetrievalError(SRetrieveSupport *trsupport, SSqlObj *pSq tscProcessSql(pNew); return; } else { // reach the maximum retry count, abort - __sync_val_compare_and_swap_32(&trsupport->pState->code, TSDB_CODE_SUCCESS, numOfRows); + atomic_val_compare_exchange_32(&trsupport->pState->code, TSDB_CODE_SUCCESS, numOfRows); tscError("%p sub:%p retrieve failed,code:%d,orderOfSub:%d failed.no more retry,set global code:%d", pPObj, pSql, numOfRows, idx, trsupport->pState->code); } } - if (__sync_add_and_fetch_32(&trsupport->pState->numOfCompleted, 1) < trsupport->pState->numOfTotal) { + if (atomic_add_fetch_32(&trsupport->pState->numOfCompleted, 1) < trsupport->pState->numOfTotal) { return tscFreeSubSqlObj(trsupport, pSql); } @@ -1101,7 +1118,7 @@ void tscRetrieveFromVnodeCallBack(void *param, TAOS_RES *tres, int numOfRows) { if (numOfRows > 0) { assert(pRes->numOfRows == numOfRows); - __sync_add_and_fetch_64(&trsupport->pState->numOfRetrievedRows, numOfRows); + atomic_add_fetch_64(&trsupport->pState->numOfRetrievedRows, numOfRows); tscTrace("%p sub:%p retrieve numOfRows:%d totalNumOfRows:%d from ip:%u,vid:%d,orderOfSub:%d", pPObj, pSql, pRes->numOfRows, trsupport->pState->numOfRetrievedRows, pSvd->ip, pSvd->vnode, idx); @@ -1160,7 +1177,7 @@ void tscRetrieveFromVnodeCallBack(void *param, TAOS_RES *tres, int numOfRows) { return tscAbortFurtherRetryRetrieval(trsupport, tres, TSDB_CODE_CLI_NO_DISKSPACE); } - if (__sync_add_and_fetch_32(&trsupport->pState->numOfCompleted, 1) < trsupport->pState->numOfTotal) { + if (atomic_add_fetch_32(&trsupport->pState->numOfCompleted, 1) < trsupport->pState->numOfTotal) { return tscFreeSubSqlObj(trsupport, pSql); } @@ -1289,7 +1306,7 @@ void tscRetrieveDataRes(void *param, TAOS_RES *tres, int code) { if (code != TSDB_CODE_SUCCESS) { if (trsupport->numOfRetry++ >= MAX_NUM_OF_SUBQUERY_RETRY) { tscTrace("%p sub:%p reach the max retry count,set global code:%d", trsupport->pParentSqlObj, pSql, code); - __sync_val_compare_and_swap_32(&trsupport->pState->code, 0, code); + atomic_val_compare_exchange_32(&trsupport->pState->code, 0, code); } else { // does not reach the maximum retry count, go on tscTrace("%p sub:%p failed code:%d, retry:%d", trsupport->pParentSqlObj, pSql, code, trsupport->numOfRetry); @@ -1338,7 +1355,7 @@ int tscBuildRetrieveMsg(SSqlObj *pSql) { *((uint64_t *)pMsg) = pSql->res.qhandle; pMsg += sizeof(pSql->res.qhandle); - *pMsg = htons(pSql->cmd.type); + *((uint16_t*)pMsg) = htons(pSql->cmd.type); pMsg += sizeof(pSql->cmd.type); msgLen = pMsg - pStart; @@ -2466,10 +2483,10 @@ int tscBuildRetrieveFromMgmtMsg(SSqlObj *pSql) { pMsg += sizeof(SMgmtHead); - *((uint64_t *)pMsg) = pSql->res.qhandle; + *((uint64_t *) pMsg) = pSql->res.qhandle; pMsg += sizeof(pSql->res.qhandle); - *pMsg = htons(pCmd->type); + *((uint16_t*) pMsg) = htons(pCmd->type); pMsg += sizeof(pCmd->type); msgLen = pMsg - pStart; @@ -2848,7 +2865,7 @@ int tscBuildMetricMetaMsg(SSqlObj *pSql) { return msgLen; } -int tscEstimateBuildHeartBeatMsgLength(SSqlObj *pSql) { +int tscEstimateHeartBeatMsgLength(SSqlObj *pSql) { int size = 0; STscObj *pObj = pSql->pTscObj; @@ -2881,7 +2898,7 @@ int tscBuildHeartBeatMsg(SSqlObj *pSql) { pthread_mutex_lock(&pObj->mutex); - size = tscEstimateBuildHeartBeatMsgLength(pSql); + size = tscEstimateHeartBeatMsgLength(pSql); if (TSDB_CODE_SUCCESS != tscAllocPayload(pCmd, size)) { tscError("%p failed to malloc for heartbeat msg", pSql); return -1; @@ -3456,7 +3473,13 @@ int tscProcessRetrieveRspFromVnode(SSqlObj *pSql) { tscSetResultPointer(pCmd, pRes); pRes->row = 0; - if (pRes->numOfRows == 0 && !(tscProjectionQueryOnMetric(pCmd) && pRes->offset > 0)) { + /** + * If the query result is exhausted, or current query is to free resource at server side, + * the connection will be recycled. + */ + if ((pRes->numOfRows == 0 && !(tscProjectionQueryOnMetric(pCmd) && pRes->offset > 0)) || + ((pCmd->type & TSDB_QUERY_TYPE_FREE_RESOURCE) == TSDB_QUERY_TYPE_FREE_RESOURCE)) { + tscTrace("%p no result or free resource, recycle connection", pSql); taosAddConnIntoCache(tscConnCache, pSql->thandle, pSql->ip, pSql->vnode, pObj->user); pSql->thandle = NULL; } else { @@ -3613,7 +3636,7 @@ int tscRenewMeterMeta(SSqlObj *pSql, char *meterId) { code = tscDoGetMeterMeta(pSql, meterId, 0); // todo ?? } else { - tscTrace("%p metric query not update metric meta, numOfTags:%d, numOfCols:%d, uid:%d, addr:%p", pSql, + tscTrace("%p metric query not update metric meta, numOfTags:%d, numOfCols:%d, uid:%lld, addr:%p", pSql, pMeterMetaInfo->pMeterMeta->numOfTags, pCmd->numOfCols, pMeterMetaInfo->pMeterMeta->uid, pMeterMetaInfo->pMeterMeta); } @@ -3770,7 +3793,7 @@ void tscInitMsgs() { tscProcessMsgRsp[TSDB_SQL_MULTI_META] = tscProcessMultiMeterMetaRsp; tscProcessMsgRsp[TSDB_SQL_SHOW] = tscProcessShowRsp; - tscProcessMsgRsp[TSDB_SQL_RETRIEVE] = tscProcessRetrieveRspFromMgmt; + tscProcessMsgRsp[TSDB_SQL_RETRIEVE] = tscProcessRetrieveRspFromVnode; // rsp handled by same function. tscProcessMsgRsp[TSDB_SQL_DESCRIBE_TABLE] = tscProcessDescribeTableRsp; tscProcessMsgRsp[TSDB_SQL_RETRIEVE_TAGS] = tscProcessTagRetrieveRsp; tscProcessMsgRsp[TSDB_SQL_RETRIEVE_EMPTY_RESULT] = tscProcessEmptyResultRsp; diff --git a/src/client/src/tscSql.c b/src/client/src/tscSql.c index fa72b18d16..6ec8f425ca 100644 --- a/src/client/src/tscSql.c +++ b/src/client/src/tscSql.c @@ -13,9 +13,6 @@ * along with this program. If not, see . */ -#include -#include - #include "os.h" #include "tcache.h" #include "tlog.h" @@ -650,11 +647,8 @@ int taos_fetch_block(TAOS_RES *res, TAOS_ROW *rows) { pCmd->limit.limit = pSql->cmd.globalLimit - pRes->numOfTotal; pCmd->limit.offset = pRes->offset; -#ifdef CLUSTER - if ((++pSql->cmd.vnodeIdx) <= pMeterMetaInfo->pMetricMeta->numOfVnodes) { -#else + if ((++pSql->cmd.vnodeIdx) < pMeterMetaInfo->pMetricMeta->numOfVnodes) { -#endif pSql->cmd.command = TSDB_SQL_SELECT; assert(pSql->fp == NULL); tscProcessSql(pSql); diff --git a/src/client/src/tscStream.c b/src/client/src/tscStream.c index ec8233c6f9..c9809be1e3 100644 --- a/src/client/src/tscStream.c +++ b/src/client/src/tscStream.c @@ -546,7 +546,7 @@ TAOS_STREAM *taos_open_stream(TAOS *taos, const char *sqlstr, void (*fp)(void *p void taos_close_stream(TAOS_STREAM *handle) { SSqlStream *pStream = (SSqlStream *)handle; - SSqlObj *pSql = (SSqlObj *)__sync_val_compare_and_swap_64(&pStream->pSql, pStream->pSql, 0); + SSqlObj *pSql = (SSqlObj *)atomic_exchange_ptr(&pStream->pSql, 0); if (pSql == NULL) { return; } diff --git a/src/client/src/tscSub.c b/src/client/src/tscSub.c index dee8f02118..bcbcaba4c4 100644 --- a/src/client/src/tscSub.c +++ b/src/client/src/tscSub.c @@ -13,7 +13,7 @@ * along with this program. If not, see . */ -#include +#include "os.h" #include "shash.h" #include "taos.h" diff --git a/src/client/src/tscSyntaxtreefunction.c b/src/client/src/tscSyntaxtreefunction.c index 00781919e4..bbd30c1fa9 100644 --- a/src/client/src/tscSyntaxtreefunction.c +++ b/src/client/src/tscSyntaxtreefunction.c @@ -13,10 +13,7 @@ * along with this program. If not, see . */ -#include -#include -#include -#include +#include "os.h" #include "tscSyntaxtreefunction.h" #include "tsql.h" diff --git a/src/client/src/tscSystem.c b/src/client/src/tscSystem.c index 7ebd43cd19..2c5ad2939a 100644 --- a/src/client/src/tscSystem.c +++ b/src/client/src/tscSystem.c @@ -13,15 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include -#include -#include - #include "os.h" #include "taosmsg.h" #include "tcache.h" @@ -92,6 +83,8 @@ void taos_init_imp() { tscTrace("Local IP address is:%s", tsLocalIp); } + taosSetCoreDump(); + #ifdef CLUSTER tscMgmtIpList.numOfIps = 2; strcpy(tscMgmtIpList.ipstr[0], tsMasterIp); @@ -185,57 +178,51 @@ void taos_init_imp() { tscConnCache = taosOpenConnCache(tsMaxMeterConnections * 2, taosCloseRpcConn, tscTmr, tsShellActivityTimer * 1000); initialized = 1; - tscTrace("taos client is initialized successfully"); + tscTrace("client is initialized successfully"); tsInsertHeadSize = tsRpcHeadSize + sizeof(SShellSubmitMsg); } void taos_init() { pthread_once(&tscinit, taos_init_imp); } -int taos_options(TSDB_OPTION option, const void *arg, ...) { - char * pStr = NULL; - SGlobalConfig *cfg_configDir = tsGetConfigOption("configDir"); - SGlobalConfig *cfg_activetimer = tsGetConfigOption("shellActivityTimer"); - SGlobalConfig *cfg_locale = tsGetConfigOption("locale"); - SGlobalConfig *cfg_charset = tsGetConfigOption("charset"); - SGlobalConfig *cfg_timezone = tsGetConfigOption("timezone"); - SGlobalConfig *cfg_socket = tsGetConfigOption("sockettype"); +static int taos_options_imp(TSDB_OPTION option, const char *pStr) { + SGlobalConfig *cfg = NULL; switch (option) { case TSDB_OPTION_CONFIGDIR: - pStr = (char *)arg; - if (cfg_configDir && cfg_configDir->cfgStatus <= TSDB_CFG_CSTATUS_OPTION) { + cfg = tsGetConfigOption("configDir"); + if (cfg && cfg->cfgStatus <= TSDB_CFG_CSTATUS_OPTION) { strncpy(configDir, pStr, TSDB_FILENAME_LEN); - cfg_configDir->cfgStatus = TSDB_CFG_CSTATUS_OPTION; + cfg->cfgStatus = TSDB_CFG_CSTATUS_OPTION; tscPrint("set config file directory:%s", pStr); } else { - tscWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg_configDir->option, pStr, - tsCfgStatusStr[cfg_configDir->cfgStatus], (char *)cfg_configDir->ptr); + tscWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg->option, pStr, + tsCfgStatusStr[cfg->cfgStatus], (char *)cfg->ptr); } break; case TSDB_OPTION_SHELL_ACTIVITY_TIMER: - if (cfg_activetimer && cfg_activetimer->cfgStatus <= TSDB_CFG_CSTATUS_OPTION) { - tsShellActivityTimer = atoi((char *)arg); + cfg = tsGetConfigOption("shellActivityTimer"); + if (cfg && cfg->cfgStatus <= TSDB_CFG_CSTATUS_OPTION) { + tsShellActivityTimer = atoi(pStr); if (tsShellActivityTimer < 1) tsShellActivityTimer = 1; if (tsShellActivityTimer > 3600) tsShellActivityTimer = 3600; - cfg_activetimer->cfgStatus = TSDB_CFG_CSTATUS_OPTION; + cfg->cfgStatus = TSDB_CFG_CSTATUS_OPTION; tscPrint("set shellActivityTimer:%d", tsShellActivityTimer); } else { - tscWarn("config option:%s, input value:%s, is configured by %s, use %d", cfg_activetimer->option, pStr, - tsCfgStatusStr[cfg_activetimer->cfgStatus], (int32_t *)cfg_activetimer->ptr); + tscWarn("config option:%s, input value:%s, is configured by %s, use %d", cfg->option, pStr, + tsCfgStatusStr[cfg->cfgStatus], (int32_t *)cfg->ptr); } break; case TSDB_OPTION_LOCALE: { // set locale - pStr = (char *)arg; - + cfg = tsGetConfigOption("locale"); size_t len = strlen(pStr); if (len == 0 || len > TSDB_LOCALE_LEN) { tscPrint("Invalid locale:%s, use default", pStr); return -1; } - if (cfg_locale && cfg_charset && cfg_locale->cfgStatus <= TSDB_CFG_CSTATUS_OPTION) { + if (cfg && cfg && cfg->cfgStatus <= TSDB_CFG_CSTATUS_OPTION) { char sep = '.'; if (strlen(tsLocale) == 0) { // locale does not set yet @@ -248,7 +235,7 @@ int taos_options(TSDB_OPTION option, const void *arg, ...) { if (locale != NULL) { tscPrint("locale set, prev locale:%s, new locale:%s", tsLocale, locale); - cfg_locale->cfgStatus = TSDB_CFG_CSTATUS_OPTION; + cfg->cfgStatus = TSDB_CFG_CSTATUS_OPTION; } else { // set the user-specified localed failed, use default LC_CTYPE as current locale locale = setlocale(LC_CTYPE, tsLocale); tscPrint("failed to set locale:%s, current locale:%s", pStr, tsLocale); @@ -270,7 +257,7 @@ int taos_options(TSDB_OPTION option, const void *arg, ...) { } strncpy(tsCharset, charset, tListLen(tsCharset)); - cfg_charset->cfgStatus = TSDB_CFG_CSTATUS_OPTION; + cfg->cfgStatus = TSDB_CFG_CSTATUS_OPTION; } else { tscPrint("charset:%s is not valid in locale, charset remains:%s", charset, tsCharset); @@ -281,23 +268,22 @@ int taos_options(TSDB_OPTION option, const void *arg, ...) { tscPrint("charset remains:%s", tsCharset); } } else { - tscWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg_locale->option, pStr, - tsCfgStatusStr[cfg_locale->cfgStatus], (char *)cfg_locale->ptr); + tscWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg->option, pStr, + tsCfgStatusStr[cfg->cfgStatus], (char *)cfg->ptr); } break; } case TSDB_OPTION_CHARSET: { /* set charset will override the value of charset, assigned during system locale changed */ - pStr = (char *)arg; - + cfg = tsGetConfigOption("charset"); size_t len = strlen(pStr); if (len == 0 || len > TSDB_LOCALE_LEN) { tscPrint("failed to set charset:%s", pStr); return -1; } - if (cfg_charset && cfg_charset->cfgStatus <= TSDB_CFG_CSTATUS_OPTION) { + if (cfg && cfg->cfgStatus <= TSDB_CFG_CSTATUS_OPTION) { if (taosValidateEncodec(pStr)) { if (strlen(tsCharset) == 0) { tscPrint("charset is set:%s", pStr); @@ -306,40 +292,41 @@ int taos_options(TSDB_OPTION option, const void *arg, ...) { } strncpy(tsCharset, pStr, tListLen(tsCharset)); - cfg_charset->cfgStatus = TSDB_CFG_CSTATUS_OPTION; + cfg->cfgStatus = TSDB_CFG_CSTATUS_OPTION; } else { tscPrint("charset:%s not valid", pStr); } } else { - tscWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg_charset->option, pStr, - tsCfgStatusStr[cfg_charset->cfgStatus], (char *)cfg_charset->ptr); + tscWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg->option, pStr, + tsCfgStatusStr[cfg->cfgStatus], (char *)cfg->ptr); } break; } case TSDB_OPTION_TIMEZONE: - pStr = (char *)arg; - if (cfg_timezone && cfg_timezone->cfgStatus <= TSDB_CFG_CSTATUS_OPTION) { + cfg = tsGetConfigOption("timezone"); + if (cfg && cfg->cfgStatus <= TSDB_CFG_CSTATUS_OPTION) { strcpy(tsTimezone, pStr); tsSetTimeZone(); - cfg_timezone->cfgStatus = TSDB_CFG_CSTATUS_OPTION; + cfg->cfgStatus = TSDB_CFG_CSTATUS_OPTION; tscTrace("timezone set:%s, input:%s by taos_options", tsTimezone, pStr); } else { - tscWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg_timezone->option, pStr, - tsCfgStatusStr[cfg_timezone->cfgStatus], (char *)cfg_timezone->ptr); + tscWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg->option, pStr, + tsCfgStatusStr[cfg->cfgStatus], (char *)cfg->ptr); } break; case TSDB_OPTION_SOCKET_TYPE: - if (cfg_socket && cfg_socket->cfgStatus <= TSDB_CFG_CSTATUS_OPTION) { - if (strcasecmp(arg, TAOS_SOCKET_TYPE_NAME_UDP) != 0 && strcasecmp(arg, TAOS_SOCKET_TYPE_NAME_TCP) != 0) { + cfg = tsGetConfigOption("sockettype"); + if (cfg && cfg->cfgStatus <= TSDB_CFG_CSTATUS_OPTION) { + if (strcasecmp(pStr, TAOS_SOCKET_TYPE_NAME_UDP) != 0 && strcasecmp(pStr, TAOS_SOCKET_TYPE_NAME_TCP) != 0) { tscError("only 'tcp' or 'udp' allowed for configuring the socket type"); return -1; } - strncpy(tsSocketType, arg, tListLen(tsSocketType)); - cfg_socket->cfgStatus = TSDB_CFG_CSTATUS_OPTION; + strncpy(tsSocketType, pStr, tListLen(tsSocketType)); + cfg->cfgStatus = TSDB_CFG_CSTATUS_OPTION; tscPrint("socket type is set:%s", tsSocketType); } break; @@ -351,3 +338,20 @@ int taos_options(TSDB_OPTION option, const void *arg, ...) { return 0; } + + +int taos_options(TSDB_OPTION option, const void *arg, ...) { + static int32_t lock = 0; + + for (int i = 1; atomic_val_compare_exchange_32(&lock, 0, 1) != 0; ++i) { + if (i % 1000 == 0) { + tscPrint("haven't acquire lock after spin %d times.", i); + sched_yield(); + } + } + + int ret = taos_options_imp(option, (const char*)arg); + + atomic_store_32(&lock, 0); + return ret; +} \ No newline at end of file diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index ec5c980612..fd23b183b1 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -13,10 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include - #include "os.h" #include "ihash.h" #include "taosmsg.h" @@ -146,7 +142,6 @@ bool tscIsSelectivityWithTagQuery(SSqlCmd* pCmd) { return false; } - void tscGetDBInfoFromMeterId(char* meterId, char* db) { char* st = strstr(meterId, TS_PATH_DELIMITER); if (st != NULL) { @@ -269,7 +264,7 @@ bool tscIsPointInterpQuery(SSqlCmd* pCmd) { } bool tscIsTWAQuery(SSqlCmd* pCmd) { - for(int32_t i = 0; i < pCmd->exprsInfo.numOfExprs; ++i) { + for (int32_t i = 0; i < pCmd->exprsInfo.numOfExprs; ++i) { SSqlExpr* pExpr = tscSqlExprGet(pCmd, i); if (pExpr == NULL) { continue; @@ -454,7 +449,8 @@ void tscDestroyDataBlock(STableDataBlocks* pDataBlock) { tfree(pDataBlock); } -SParamInfo* tscAddParamToDataBlock(STableDataBlocks* pDataBlock, char type, uint8_t timePrec, short bytes, uint32_t offset) { +SParamInfo* tscAddParamToDataBlock(STableDataBlocks* pDataBlock, char type, uint8_t timePrec, short bytes, + uint32_t offset) { uint32_t needed = pDataBlock->numOfParams + 1; if (needed > pDataBlock->numOfAllocedParams) { needed *= 2; @@ -494,13 +490,13 @@ SDataBlockList* tscCreateBlockArrayList() { return pDataBlockArrayList; } -void tscAppendDataBlock(SDataBlockList *pList, STableDataBlocks *pBlocks) { +void tscAppendDataBlock(SDataBlockList* pList, STableDataBlocks* pBlocks) { if (pList->nSize >= pList->nAlloc) { pList->nAlloc = pList->nAlloc << 1; - pList->pData = realloc(pList->pData, sizeof(void *) * (size_t)pList->nAlloc); + pList->pData = realloc(pList->pData, sizeof(void*) * (size_t)pList->nAlloc); // reset allocated memory - memset(pList->pData + pList->nSize, 0, sizeof(void *) * (pList->nAlloc - pList->nSize)); + memset(pList->pData + pList->nSize, 0, sizeof(void*) * (pList->nAlloc - pList->nSize)); } pList->pData[pList->nSize++] = pBlocks; @@ -557,7 +553,7 @@ void tscFreeUnusedDataBlocks(SDataBlockList* pList) { } STableDataBlocks* tscCreateDataBlockEx(size_t size, int32_t rowSize, int32_t startOffset, char* name) { - STableDataBlocks *dataBuf = tscCreateDataBlock(size); + STableDataBlocks* dataBuf = tscCreateDataBlock(size); dataBuf->rowSize = rowSize; dataBuf->size = startOffset; @@ -577,7 +573,7 @@ STableDataBlocks* tscGetDataBlockFromList(void* pHashList, SDataBlockList* pData } if (dataBuf == NULL) { - dataBuf = tscCreateDataBlockEx((size_t) size, rowSize, startOffset, tableId); + dataBuf = tscCreateDataBlockEx((size_t)size, rowSize, startOffset, tableId); dataBuf = *(STableDataBlocks**)taosAddIntHash(pHashList, id, (char*)&dataBuf); tscAppendDataBlock(pDataBlockList, dataBuf); } @@ -608,7 +604,7 @@ int32_t tscMergeTableDataBlocks(SSqlObj* pSql, SDataBlockList* pTableDataBlockLi if (tmp != NULL) { dataBuf->pData = tmp; memset(dataBuf->pData + dataBuf->size, 0, dataBuf->nAllocSize - dataBuf->size); - } else { // failed to allocate memory, free already allocated memory and return error code + } else { // failed to allocate memory, free already allocated memory and return error code tscError("%p failed to allocate memory for merging submit block, size:%d", pSql, dataBuf->nAllocSize); taosCleanUpIntHash(pVnodeDataBlockHashList); @@ -677,7 +673,7 @@ int tscAllocPayload(SSqlCmd* pCmd, int size) { pCmd->allocSize = size; } else { if (pCmd->allocSize < size) { - char* b = realloc(pCmd->payload, size); + char* b = realloc(pCmd->payload, size); if (b == NULL) return TSDB_CODE_CLI_OUT_OF_MEMORY; pCmd->payload = b; pCmd->allocSize = size; @@ -873,11 +869,11 @@ void tscClearFieldInfo(SFieldInfo* pFieldInfo) { static void _exprCheckSpace(SSqlExprInfo* pExprInfo, int32_t size) { if (size > pExprInfo->numOfAlloc) { - int32_t oldSize = pExprInfo->numOfAlloc; + uint32_t oldSize = pExprInfo->numOfAlloc; - int32_t newSize = (oldSize <= 0) ? 8 : (oldSize << 1); + uint32_t newSize = (oldSize <= 0) ? 8 : (oldSize << 1U); while (newSize < size) { - newSize = (newSize << 1); + newSize = (newSize << 1U); } if (newSize > TSDB_MAX_COLUMNS) { @@ -1165,7 +1161,7 @@ void tscColumnBaseInfoDestroy(SColumnBaseInfo* pColumnBaseInfo) { assert(pColumnBaseInfo->numOfCols <= TSDB_MAX_COLUMNS); for (int32_t i = 0; i < pColumnBaseInfo->numOfCols; ++i) { - SColumnBase *pColBase = &(pColumnBaseInfo->pColList[i]); + SColumnBase* pColBase = &(pColumnBaseInfo->pColList[i]); if (pColBase->numOfFilters > 0) { for (int32_t j = 0; j < pColBase->numOfFilters; ++j) { @@ -1183,8 +1179,9 @@ void tscColumnBaseInfoDestroy(SColumnBaseInfo* pColumnBaseInfo) { tfree(pColumnBaseInfo->pColList); } - -void tscColumnBaseInfoReserve(SColumnBaseInfo* pColumnBaseInfo, int32_t size) { _cf_ensureSpace(pColumnBaseInfo, size); } +void tscColumnBaseInfoReserve(SColumnBaseInfo* pColumnBaseInfo, int32_t size) { + _cf_ensureSpace(pColumnBaseInfo, size); +} /* * 1. normal name, not a keyword or number @@ -1232,16 +1229,16 @@ int32_t tscValidateName(SSQLToken* pToken) { int len = tSQLGetToken(pToken->z, &pToken->type); // single token, validate it - if (len == pToken->n){ + if (len == pToken->n) { return validateQuoteToken(pToken); } else { - sep = strnchr(pToken->z, TS_PATH_DELIMITER[0], pToken->n, true); - if (sep == NULL) { - return TSDB_CODE_INVALID_SQL; - } + sep = strnchr(pToken->z, TS_PATH_DELIMITER[0], pToken->n, true); + if (sep == NULL) { + return TSDB_CODE_INVALID_SQL; + } return tscValidateName(pToken); - } + } } else { if (isNumber(pToken)) { return TSDB_CODE_INVALID_SQL; @@ -1620,8 +1617,8 @@ int32_t SStringAlloc(SString* pStr, int32_t size) { #ifdef WINDOWS LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, - GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language - (LPTSTR)&lpMsgBuf, 0, NULL); + GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + (LPTSTR)&lpMsgBuf, 0, NULL); tscTrace("failed to allocate memory, reason:%s", lpMsgBuf); LocalFree(lpMsgBuf); #else @@ -1656,12 +1653,11 @@ int32_t SStringEnsureRemain(SString* pStr, int32_t size) { char* tmp = realloc(pStr->z, newsize); if (tmp == NULL) { - #ifdef WINDOWS LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, - GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language - (LPTSTR)&lpMsgBuf, 0, NULL); + GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + (LPTSTR)&lpMsgBuf, 0, NULL); tscTrace("failed to allocate memory, reason:%s", lpMsgBuf); LocalFree(lpMsgBuf); #else @@ -1732,7 +1728,7 @@ SSqlObj* createSubqueryObj(SSqlObj* pSql, int32_t vnodeIndex, int16_t tableIndex if (pPrevSql != NULL) { pNew->cmd.type = pPrevSql->cmd.type; } else { - pNew->cmd.type |= TSDB_QUERY_TYPE_SUBQUERY; // it must be the subquery + pNew->cmd.type |= TSDB_QUERY_TYPE_SUBQUERY; // it must be the subquery } uint64_t uid = pMeterMetaInfo->pMeterMeta->uid; @@ -1764,7 +1760,7 @@ SSqlObj* createSubqueryObj(SSqlObj* pSql, int32_t vnodeIndex, int16_t tableIndex char key[TSDB_MAX_TAGS_LEN + 1] = {0}; tscGetMetricMetaCacheKey(pCmd, key, pMetermetaInfo->pMeterMeta->uid); - char* name = pMeterMetaInfo->name; + char* name = pMeterMetaInfo->name; SMeterMetaInfo* pFinalInfo = NULL; if (pPrevSql == NULL) { @@ -1772,11 +1768,11 @@ SSqlObj* createSubqueryObj(SSqlObj* pSql, int32_t vnodeIndex, int16_t tableIndex SMetricMeta* pMetricMeta = taosGetDataFromCache(tscCacheHandle, key); pFinalInfo = tscAddMeterMetaInfo(&pNew->cmd, name, pMeterMeta, pMetricMeta, pMeterMetaInfo->numOfTags, - pMeterMetaInfo->tagColumnIndex); + pMeterMetaInfo->tagColumnIndex); } else { SMeterMetaInfo* pPrevInfo = tscGetMeterMetaInfo(&pPrevSql->cmd, 0); - pFinalInfo = tscAddMeterMetaInfo(&pNew->cmd, name, pPrevInfo->pMeterMeta, pPrevInfo->pMetricMeta, pMeterMetaInfo->numOfTags, - pMeterMetaInfo->tagColumnIndex); + pFinalInfo = tscAddMeterMetaInfo(&pNew->cmd, name, pPrevInfo->pMeterMeta, pPrevInfo->pMetricMeta, + pMeterMetaInfo->numOfTags, pMeterMetaInfo->tagColumnIndex); pPrevInfo->pMeterMeta = NULL; pPrevInfo->pMetricMeta = NULL; @@ -1787,13 +1783,14 @@ SSqlObj* createSubqueryObj(SSqlObj* pSql, int32_t vnodeIndex, int16_t tableIndex assert(pFinalInfo->pMetricMeta != NULL); } - tscTrace("%p new subquery %p, vnodeIdx:%d, tableIndex:%d, type:%d", pSql, pNew, vnodeIndex, tableIndex, pNew->cmd.type); + tscTrace("%p new subquery %p, vnodeIdx:%d, tableIndex:%d, type:%d", pSql, pNew, vnodeIndex, tableIndex, + pNew->cmd.type); return pNew; } void tscDoQuery(SSqlObj* pSql) { SSqlCmd* pCmd = &pSql->cmd; - void* fp = pSql->fp; + void* fp = pSql->fp; if (pCmd->command > TSDB_SQL_LOCAL) { tscProcessLocalCmd(pSql); @@ -1821,3 +1818,15 @@ int16_t tscGetJoinTagColIndexByUid(SSqlCmd* pCmd, uint64_t uid) { return pTagCond->joinInfo.right.tagCol; } } + +bool tscIsUpdateQuery(STscObj* pObj) { + if (pObj == NULL || pObj->signature != pObj) { + globalCode = TSDB_CODE_DISCONNECTED; + return TSDB_CODE_DISCONNECTED; + } + + SSqlCmd* pCmd = &pObj->pSql->cmd; + return ((pCmd->command >= TSDB_SQL_INSERT && pCmd->command <= TSDB_SQL_DROP_DNODE) || + TSDB_SQL_USE_DB == pCmd->command) ? 1 : 0; + +} diff --git a/src/connector/go/src/taosSql/taosSqlCgo.go b/src/connector/go/src/taosSql/taosSqlCgo.go index e240ac44ec..fcef14045f 100755 --- a/src/connector/go/src/taosSql/taosSqlCgo.go +++ b/src/connector/go/src/taosSql/taosSqlCgo.go @@ -29,46 +29,47 @@ import ( "unsafe" ) -func (mc *taosConn) taosConnect(ip, user, pass, db string, port int) (taos unsafe.Pointer, err error){ +func (mc *taosConn) taosConnect(ip, user, pass, db string, port int) (taos unsafe.Pointer, err error) { cuser := C.CString(user) cpass := C.CString(pass) - cip := C.CString(ip) - cdb := C.CString(db) + cip := C.CString(ip) + cdb := C.CString(db) defer C.free(unsafe.Pointer(cip)) defer C.free(unsafe.Pointer(cuser)) defer C.free(unsafe.Pointer(cpass)) defer C.free(unsafe.Pointer(cdb)) taosObj := C.taos_connect(cip, cuser, cpass, cdb, (C.int)(port)) - if taosObj == nil { - return nil, errors.New("taos_connect() fail!") - } + if taosObj == nil { + return nil, errors.New("taos_connect() fail!") + } - return (unsafe.Pointer)(taosObj), nil -} + return (unsafe.Pointer)(taosObj), nil +} func (mc *taosConn) taosQuery(sqlstr string) (int, error) { - taosLog.Printf("taosQuery() input sql:%s\n", sqlstr) + //taosLog.Printf("taosQuery() input sql:%s\n", sqlstr) - csqlstr := C.CString(sqlstr) + csqlstr := C.CString(sqlstr) defer C.free(unsafe.Pointer(csqlstr)) - code := int(C.taos_query(mc.taos, csqlstr)) + code := int(C.taos_query(mc.taos, csqlstr)) - if 0 != code { - mc.taos_error() - errStr := C.GoString(C.taos_errstr(mc.taos)) - taosLog.Println("taos_query() failed:", errStr) - return 0, errors.New(errStr) - } + if 0 != code { + mc.taos_error() + errStr := C.GoString(C.taos_errstr(mc.taos)) + taosLog.Println("taos_query() failed:", errStr) + taosLog.Printf("taosQuery() input sql:%s\n", sqlstr) + return 0, errors.New(errStr) + } - // read result and save into mc struct - num_fields := int(C.taos_field_count(mc.taos)) - if 0 == num_fields { // there are no select and show kinds of commands - mc.affectedRows = int(C.taos_affected_rows(mc.taos)) - mc.insertId = 0 - } + // read result and save into mc struct + num_fields := int(C.taos_field_count(mc.taos)) + if 0 == num_fields { // there are no select and show kinds of commands + mc.affectedRows = int(C.taos_affected_rows(mc.taos)) + mc.insertId = 0 + } - return num_fields, nil + return num_fields, nil } func (mc *taosConn) taos_close() { @@ -76,8 +77,8 @@ func (mc *taosConn) taos_close() { } func (mc *taosConn) taos_error() { - // free local resouce: allocated memory/metric-meta refcnt - //var pRes unsafe.Pointer - pRes := C.taos_use_result(mc.taos) - C.taos_free_result(pRes) + // free local resouce: allocated memory/metric-meta refcnt + //var pRes unsafe.Pointer + pRes := C.taos_use_result(mc.taos) + C.taos_free_result(pRes) } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBJNIConnector.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBJNIConnector.java index c66a8322c1..3adb601822 100755 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBJNIConnector.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBJNIConnector.java @@ -134,7 +134,7 @@ public class TSDBJNIConnector { } } - // Try retrieving result set for the executed SQLusing the current connection pointer. If the executed + // Try retrieving result set for the executed SQL using the current connection pointer. If the executed // SQL is a DML/DDL which doesn't return a result set, then taosResultSetPointer should be 0L. Otherwise, // taosResultSetPointer should be a non-zero value. taosResultSetPointer = this.getResultSetImp(this.taos); diff --git a/src/inc/sdb.h b/src/inc/sdb.h index 4a1969b700..f645039441 100644 --- a/src/inc/sdb.h +++ b/src/inc/sdb.h @@ -23,10 +23,10 @@ extern "C" { #include "taosmsg.h" #include "tsdb.h" -extern short sdbPeerPort; -extern short sdbSyncPort; +extern short tsMgmtMgmtPort; +extern short tsMgmtSyncPort; extern int sdbMaxNodes; -extern int sdbHbTimer; // seconds +extern int tsMgmtPeerHBTimer; // seconds extern char sdbZone[]; extern char sdbMasterIp[]; extern char sdbPrivateIp[]; diff --git a/src/inc/taos.h b/src/inc/taos.h index 94e99d582c..05acc91551 100644 --- a/src/inc/taos.h +++ b/src/inc/taos.h @@ -122,9 +122,6 @@ void taos_close_stream(TAOS_STREAM *tstr); int taos_load_table_info(TAOS *taos, const char* tableNameList); -// TODO: `configDir` should not be declared here -extern char configDir[]; // the path to global configuration - #ifdef __cplusplus } #endif diff --git a/src/inc/taoserror.h b/src/inc/taoserror.h new file mode 100644 index 0000000000..5510212dbc --- /dev/null +++ b/src/inc/taoserror.h @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef TDENGINE_TAOSERROR_H +#define TDENGINE_TAOSERROR_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define TSDB_CODE_SUCCESS 0 +#define TSDB_CODE_ACTION_IN_PROGRESS 1 + +#define TSDB_CODE_LAST_SESSION_NOT_FINISHED 5 +#define TSDB_CODE_INVALID_SESSION_ID 6 +#define TSDB_CODE_INVALID_TRAN_ID 7 +#define TSDB_CODE_INVALID_MSG_TYPE 8 +#define TSDB_CODE_ALREADY_PROCESSED 9 +#define TSDB_CODE_AUTH_FAILURE 10 +#define TSDB_CODE_WRONG_MSG_SIZE 11 +#define TSDB_CODE_UNEXPECTED_RESPONSE 12 +#define TSDB_CODE_INVALID_RESPONSE_TYPE 13 +#define TSDB_CODE_NO_RESOURCE 14 +#define TSDB_CODE_INVALID_TIME_STAMP 15 +#define TSDB_CODE_MISMATCHED_METER_ID 16 +#define TSDB_CODE_ACTION_TRANS_NOT_FINISHED 17 +#define TSDB_CODE_ACTION_NOT_ONLINE 18 +#define TSDB_CODE_ACTION_SEND_FAILD 19 +#define TSDB_CODE_NOT_ACTIVE_SESSION 20 +#define TSDB_CODE_INVALID_VNODE_ID 21 +#define TSDB_CODE_APP_ERROR 22 +#define TSDB_CODE_INVALID_IE 23 +#define TSDB_CODE_INVALID_VALUE 24 +#define TSDB_CODE_REDIRECT 25 +#define TSDB_CODE_ALREADY_THERE 26 +#define TSDB_CODE_INVALID_METER_ID 27 +#define TSDB_CODE_INVALID_SQL 28 +#define TSDB_CODE_NETWORK_UNAVAIL 29 +#define TSDB_CODE_INVALID_MSG_LEN 30 +#define TSDB_CODE_INVALID_DB 31 +#define TSDB_CODE_INVALID_TABLE 32 +#define TSDB_CODE_DB_ALREADY_EXIST 33 +#define TSDB_CODE_TABLE_ALREADY_EXIST 34 +#define TSDB_CODE_INVALID_USER 35 +#define TSDB_CODE_INVALID_ACCT 36 +#define TSDB_CODE_INVALID_PASS 37 +#define TSDB_CODE_DB_NOT_SELECTED 38 +#define TSDB_CODE_MEMORY_CORRUPTED 39 +#define TSDB_CODE_USER_ALREADY_EXIST 40 +#define TSDB_CODE_NO_RIGHTS 41 +#define TSDB_CODE_DISCONNECTED 42 +#define TSDB_CODE_NO_MASTER 43 +#define TSDB_CODE_NOT_CONFIGURED 44 +#define TSDB_CODE_INVALID_OPTION 45 +#define TSDB_CODE_NODE_OFFLINE 46 +#define TSDB_CODE_SYNC_REQUIRED 47 +#define TSDB_CODE_NO_ENOUGH_DNODES 48 +#define TSDB_CODE_UNSYNCED 49 +#define TSDB_CODE_TOO_SLOW 50 +#define TSDB_CODE_OTHERS 51 +#define TSDB_CODE_NO_REMOVE_MASTER 52 +#define TSDB_CODE_WRONG_SCHEMA 53 +#define TSDB_CODE_NOT_ACTIVE_VNODE 54 +#define TSDB_CODE_TOO_MANY_USERS 55 +#define TSDB_CODE_TOO_MANY_DATABSES 56 +#define TSDB_CODE_TOO_MANY_TABLES 57 +#define TSDB_CODE_TOO_MANY_DNODES 58 +#define TSDB_CODE_TOO_MANY_ACCTS 59 +#define TSDB_CODE_ACCT_ALREADY_EXIST 60 +#define TSDB_CODE_DNODE_ALREADY_EXIST 61 +#define TSDB_CODE_SDB_ERROR 62 +#define TSDB_CODE_METRICMETA_EXPIRED 63 // local cached metric-meta expired causes error in metric query +#define TSDB_CODE_NOT_READY 64 // peer is not ready to process data +#define TSDB_CODE_MAX_SESSIONS 65 // too many sessions +#define TSDB_CODE_MAX_CONNECTIONS 66 // too many connections +#define TSDB_CODE_SESSION_ALREADY_EXIST 67 +#define TSDB_CODE_NO_QSUMMARY 68 +#define TSDB_CODE_SERV_OUT_OF_MEMORY 69 +#define TSDB_CODE_INVALID_QHANDLE 70 +#define TSDB_CODE_RELATED_TABLES_EXIST 71 +#define TSDB_CODE_MONITOR_DB_FORBEIDDEN 72 +#define TSDB_CODE_VG_COMMITLOG_INIT_FAILED 73 +#define TSDB_CODE_VG_INIT_FAILED 74 +#define TSDB_CODE_DATA_ALREADY_IMPORTED 75 +#define TSDB_CODE_OPS_NOT_SUPPORT 76 +#define TSDB_CODE_INVALID_QUERY_ID 77 +#define TSDB_CODE_INVALID_STREAM_ID 78 +#define TSDB_CODE_INVALID_CONNECTION 79 +#define TSDB_CODE_ACTION_NOT_BALANCED 80 +#define TSDB_CODE_CLI_OUT_OF_MEMORY 81 +#define TSDB_CODE_DATA_OVERFLOW 82 +#define TSDB_CODE_QUERY_CANCELLED 83 +#define TSDB_CODE_GRANT_TIMESERIES_LIMITED 84 +#define TSDB_CODE_GRANT_EXPIRED 85 +#define TSDB_CODE_CLI_NO_DISKSPACE 86 +#define TSDB_CODE_FILE_CORRUPTED 87 +#define TSDB_CODE_INVALID_CLIENT_VERSION 88 +#define TSDB_CODE_INVALID_ACCT_PARAMETER 89 +#define TSDB_CODE_NOT_ENOUGH_TIME_SERIES 90 +#define TSDB_CODE_NO_WRITE_ACCESS 91 +#define TSDB_CODE_NO_READ_ACCESS 92 +#define TSDB_CODE_GRANT_DB_LIMITED 93 +#define TSDB_CODE_GRANT_USER_LIMITED 94 +#define TSDB_CODE_GRANT_CONN_LIMITED 95 +#define TSDB_CODE_GRANT_STREAM_LIMITED 96 +#define TSDB_CODE_GRANT_SPEED_LIMITED 97 +#define TSDB_CODE_GRANT_STORAGE_LIMITED 98 +#define TSDB_CODE_GRANT_QUERYTIME_LIMITED 99 +#define TSDB_CODE_GRANT_ACCT_LIMITED 100 +#define TSDB_CODE_GRANT_DNODE_LIMITED 101 +#define TSDB_CODE_GRANT_CPU_LIMITED 102 +#define TSDB_CODE_SESSION_NOT_READY 103 // table NOT in ready state +#define TSDB_CODE_BATCH_SIZE_TOO_BIG 104 +#define TSDB_CODE_TIMESTAMP_OUT_OF_RANGE 105 +#define TSDB_CODE_INVALID_QUERY_MSG 106 // failed to validate the sql expression msg by vnode +#define TSDB_CODE_CACHE_BLOCK_TS_DISORDERED 107 // time stamp in cache block is disordered +#define TSDB_CODE_FILE_BLOCK_TS_DISORDERED 108 // time stamp in file block is disordered +#define TSDB_CODE_INVALID_COMMIT_LOG 109 // commit log init failed +#define TSDB_CODE_SERVER_NO_SPACE 110 +#define TSDB_CODE_NOT_SUPER_TABLE 111 // operation only available for super table +#define TSDB_CODE_DUPLICATE_TAGS 112 // tags value for join not unique +#define TSDB_CODE_INVALID_SUBMIT_MSG 113 +#define TSDB_CODE_NOT_ACTIVE_TABLE 114 +#define TSDB_CODE_INVALID_TABLE_ID 115 +#define TSDB_CODE_INVALID_VNODE_STATUS 116 +#define TSDB_CODE_FAILED_TO_LOCK_RESOURCES 117 + +#ifdef __cplusplus +} +#endif + +#endif //TDENGINE_TAOSERROR_H diff --git a/src/inc/taosmsg.h b/src/inc/taosmsg.h index 1d5e1bb5a5..e75404ad98 100644 --- a/src/inc/taosmsg.h +++ b/src/inc/taosmsg.h @@ -21,119 +21,7 @@ extern "C" { #endif #include "tsdb.h" - -#define TSDB_CODE_SUCCESS 0 -#define TSDB_CODE_ACTION_IN_PROGRESS 1 - -#define TSDB_CODE_LAST_SESSION_NOT_FINISHED 5 -#define TSDB_CODE_INVALID_SESSION_ID 6 -#define TSDB_CODE_INVALID_TRAN_ID 7 -#define TSDB_CODE_INVALID_MSG_TYPE 8 -#define TSDB_CODE_ALREADY_PROCESSED 9 -#define TSDB_CODE_AUTH_FAILURE 10 -#define TSDB_CODE_WRONG_MSG_SIZE 11 -#define TSDB_CODE_UNEXPECTED_RESPONSE 12 -#define TSDB_CODE_INVALID_RESPONSE_TYPE 13 -#define TSDB_CODE_NO_RESOURCE 14 -#define TSDB_CODE_INVALID_TIME_STAMP 15 -#define TSDB_CODE_MISMATCHED_METER_ID 16 -#define TSDB_CODE_ACTION_TRANS_NOT_FINISHED 17 -#define TSDB_CODE_ACTION_NOT_ONLINE 18 -#define TSDB_CODE_ACTION_SEND_FAILD 19 -#define TSDB_CODE_NOT_ACTIVE_SESSION 20 -#define TSDB_CODE_INSERT_FAILED 21 -#define TSDB_CODE_APP_ERROR 22 -#define TSDB_CODE_INVALID_IE 23 -#define TSDB_CODE_INVALID_VALUE 24 -#define TSDB_CODE_REDIRECT 25 -#define TSDB_CODE_ALREADY_THERE 26 -#define TSDB_CODE_INVALID_METER_ID 27 -#define TSDB_CODE_INVALID_SQL 28 -#define TSDB_CODE_NETWORK_UNAVAIL 29 -#define TSDB_CODE_INVALID_MSG_LEN 30 -#define TSDB_CODE_INVALID_DB 31 -#define TSDB_CODE_INVALID_TABLE 32 -#define TSDB_CODE_DB_ALREADY_EXIST 33 -#define TSDB_CODE_TABLE_ALREADY_EXIST 34 -#define TSDB_CODE_INVALID_USER 35 -#define TSDB_CODE_INVALID_ACCT 36 -#define TSDB_CODE_INVALID_PASS 37 -#define TSDB_CODE_DB_NOT_SELECTED 38 -#define TSDB_CODE_MEMORY_CORRUPTED 39 -#define TSDB_CODE_USER_ALREADY_EXIST 40 -#define TSDB_CODE_NO_RIGHTS 41 -#define TSDB_CODE_DISCONNECTED 42 -#define TSDB_CODE_NO_MASTER 43 -#define TSDB_CODE_NOT_CONFIGURED 44 -#define TSDB_CODE_INVALID_OPTION 45 -#define TSDB_CODE_NODE_OFFLINE 46 -#define TSDB_CODE_SYNC_REQUIRED 47 -#define TSDB_CODE_NO_ENOUGH_DNODES 48 -#define TSDB_CODE_UNSYNCED 49 -#define TSDB_CODE_TOO_SLOW 50 -#define TSDB_CODE_OTHERS 51 -#define TSDB_CODE_NO_REMOVE_MASTER 52 -#define TSDB_CODE_WRONG_SCHEMA 53 -#define TSDB_CODE_NO_RESULT 54 -#define TSDB_CODE_TOO_MANY_USERS 55 -#define TSDB_CODE_TOO_MANY_DATABSES 56 -#define TSDB_CODE_TOO_MANY_TABLES 57 -#define TSDB_CODE_TOO_MANY_DNODES 58 -#define TSDB_CODE_TOO_MANY_ACCTS 59 -#define TSDB_CODE_ACCT_ALREADY_EXIST 60 -#define TSDB_CODE_DNODE_ALREADY_EXIST 61 -#define TSDB_CODE_SDB_ERROR 62 -#define TSDB_CODE_METRICMETA_EXPIRED 63 // local cached metric-meta expired causes error in metric query -#define TSDB_CODE_NOT_READY 64 // peer is not ready to process data -#define TSDB_CODE_MAX_SESSIONS 65 // too many sessions -#define TSDB_CODE_MAX_CONNECTIONS 66 // too many connections -#define TSDB_CODE_SESSION_ALREADY_EXIST 67 -#define TSDB_CODE_NO_QSUMMARY 68 -#define TSDB_CODE_SERV_OUT_OF_MEMORY 69 -#define TSDB_CODE_INVALID_QHANDLE 70 -#define TSDB_CODE_RELATED_TABLES_EXIST 71 -#define TSDB_CODE_MONITOR_DB_FORBEIDDEN 72 -#define TSDB_CODE_VG_COMMITLOG_INIT_FAILED 73 -#define TSDB_CODE_VG_INIT_FAILED 74 -#define TSDB_CODE_DATA_ALREADY_IMPORTED 75 -#define TSDB_CODE_OPS_NOT_SUPPORT 76 -#define TSDB_CODE_INVALID_QUERY_ID 77 -#define TSDB_CODE_INVALID_STREAM_ID 78 -#define TSDB_CODE_INVALID_CONNECTION 79 -#define TSDB_CODE_ACTION_NOT_BALANCED 80 -#define TSDB_CODE_CLI_OUT_OF_MEMORY 81 -#define TSDB_CODE_DATA_OVERFLOW 82 -#define TSDB_CODE_QUERY_CANCELLED 83 -#define TSDB_CODE_GRANT_TIMESERIES_LIMITED 84 -#define TSDB_CODE_GRANT_EXPIRED 85 -#define TSDB_CODE_CLI_NO_DISKSPACE 86 -#define TSDB_CODE_FILE_CORRUPTED 87 -#define TSDB_CODE_INVALID_CLIENT_VERSION 88 -#define TSDB_CODE_INVALID_ACCT_PARAMETER 89 -#define TSDB_CODE_NOT_ENOUGH_TIME_SERIES 90 -#define TSDB_CODE_NO_WRITE_ACCESS 91 -#define TSDB_CODE_NO_READ_ACCESS 92 -#define TSDB_CODE_GRANT_DB_LIMITED 93 -#define TSDB_CODE_GRANT_USER_LIMITED 94 -#define TSDB_CODE_GRANT_CONN_LIMITED 95 -#define TSDB_CODE_GRANT_STREAM_LIMITED 96 -#define TSDB_CODE_GRANT_SPEED_LIMITED 97 -#define TSDB_CODE_GRANT_STORAGE_LIMITED 98 -#define TSDB_CODE_GRANT_QUERYTIME_LIMITED 99 -#define TSDB_CODE_GRANT_ACCT_LIMITED 100 -#define TSDB_CODE_GRANT_DNODE_LIMITED 101 -#define TSDB_CODE_GRANT_CPU_LIMITED 102 -#define TSDB_CODE_SESSION_NOT_READY 103 // table NOT in ready state -#define TSDB_CODE_BATCH_SIZE_TOO_BIG 104 -#define TSDB_CODE_TIMESTAMP_OUT_OF_RANGE 105 -#define TSDB_CODE_INVALID_QUERY_MSG 106 // failed to validate the sql expression msg by vnode -#define TSDB_CODE_CACHE_BLOCK_TS_DISORDERED 107 // time stamp in cache block is disordered -#define TSDB_CODE_FILE_BLOCK_TS_DISORDERED 108 // time stamp in file block is disordered -#define TSDB_CODE_INVALID_COMMIT_LOG 109 // commit log init failed -#define TSDB_CODE_SERVER_NO_SPACE 110 -#define TSDB_CODE_NOT_SUPER_TABLE 111 // -#define TSDB_CODE_DUPLICATE_TAGS 112 // tags value for join not unique -#define TSDB_CODE_INVALID_SUBMIT_MSG 113 +#include "taoserror.h" // message type #define TSDB_MSG_TYPE_REG 1 @@ -673,7 +561,7 @@ typedef struct { typedef struct { uint64_t qhandle; - int16_t free; + uint16_t free; } SRetrieveMeterMsg; typedef struct { diff --git a/src/inc/tglobalcfg.h b/src/inc/tglobalcfg.h index e83c89593f..09ced23c91 100644 --- a/src/inc/tglobalcfg.h +++ b/src/inc/tglobalcfg.h @@ -169,6 +169,8 @@ extern uint32_t debugFlag; extern uint32_t odbcdebugFlag; extern uint32_t qdebugFlag; +extern uint32_t taosMaxTmrCtrl; + extern int tsRpcTimer; extern int tsRpcMaxTime; extern int tsUdpDelay; @@ -245,7 +247,7 @@ typedef struct { extern SGlobalConfig *tsGlobalConfig; extern int tsGlobalConfigNum; extern char * tsCfgStatusStr[]; -SGlobalConfig *tsGetConfigOption(char *option); +SGlobalConfig *tsGetConfigOption(const char *option); #define TSDB_CFG_MAX_NUM 110 #define TSDB_CFG_PRINT_LEN 23 diff --git a/src/inc/trpc.h b/src/inc/trpc.h index 3ae6e9ea97..ef86e672d1 100644 --- a/src/inc/trpc.h +++ b/src/inc/trpc.h @@ -25,7 +25,6 @@ extern "C" { #define TAOS_CONN_UDPS 0 #define TAOS_CONN_UDPC 1 -#define TAOS_CONN_UDP 1 #define TAOS_CONN_TCPS 2 #define TAOS_CONN_TCPC 3 #define TAOS_CONN_HTTPS 4 @@ -39,7 +38,7 @@ extern "C" { #define TAOS_ID_REALLOCATE 2 #define TAOS_CONN_SOCKET_TYPE_S() ((strcasecmp(tsSocketType, TAOS_SOCKET_TYPE_NAME_UDP) == 0)? TAOS_CONN_UDPS:TAOS_CONN_TCPS) -#define TAOS_CONN_SOCKET_TYPE_C() ((strcasecmp(tsSocketType, TAOS_SOCKET_TYPE_NAME_UDP) == 0)? TAOS_CONN_UDP:TAOS_CONN_TCPC) +#define TAOS_CONN_SOCKET_TYPE_C() ((strcasecmp(tsSocketType, TAOS_SOCKET_TYPE_NAME_UDP) == 0)? TAOS_CONN_UDPC:TAOS_CONN_TCPC) #define taosSendMsgToPeer(x, y, z) taosSendMsgToPeerH(x, y, z, NULL) #define taosOpenRpcChann(x, y, z) taosOpenRpcChannWithQ(x,y,z,NULL) diff --git a/src/inc/tsdb.h b/src/inc/tsdb.h index de4e430a46..771046d4d1 100644 --- a/src/inc/tsdb.h +++ b/src/inc/tsdb.h @@ -44,22 +44,40 @@ extern "C" { #define TSDB_TIME_PRECISION_MILLI_STR "ms" #define TSDB_TIME_PRECISION_MICRO_STR "us" -enum _status { - TSDB_STATUS_OFFLINE, - TSDB_STATUS_CREATING, - TSDB_STATUS_UNSYNCED, - TSDB_STATUS_SLAVE, - TSDB_STATUS_MASTER, - TSDB_STATUS_READY, + enum _vnode_status { + TSDB_VNODE_STATUS_OFFLINE, + TSDB_VNODE_STATUS_CREATING, + TSDB_VNODE_STATUS_UNSYNCED, + TSDB_VNODE_STATUS_SLAVE, + TSDB_VNODE_STATUS_MASTER, + TSDB_VNODE_STATUS_CLOSING, + TSDB_VNODE_STATUS_DELETING, }; -enum _syncstatus { +enum _vnode_sync_status { STDB_SSTATUS_INIT, TSDB_SSTATUS_SYNCING, TSDB_SSTATUS_SYNC_CACHE, TSDB_SSTATUS_SYNC_FILE, }; +enum _dnode_status { + TSDB_DNODE_STATUS_OFFLINE, + TSDB_DNODE_STATUS_READY +}; + +enum _dnode_balance_status { + LB_DNODE_STATE_BALANCED, + LB_DNODE_STATE_BALANCING, + LB_DNODE_STATE_OFFLINE_REMOVING, + LB_DNODE_STATE_SHELL_REMOVING +}; + +enum _vgroup_status { + LB_VGROUP_STATE_READY, + LB_VGROUP_STATE_UPDATE +}; + #define TSDB_DATA_TYPE_BOOL 1 // 1 bytes #define TSDB_DATA_TYPE_TINYINT 2 // 1 byte #define TSDB_DATA_TYPE_SMALLINT 3 // 2 bytes @@ -221,20 +239,20 @@ enum _syncstatus { #define TSDB_MAX_RPC_THREADS 5 #define TSDB_QUERY_TYPE_QUERY 0 // normal query -#define TSDB_QUERY_TYPE_FREE_RESOURCE 0x1 // free qhandle at vnode +#define TSDB_QUERY_TYPE_FREE_RESOURCE 0x01U // free qhandle at vnode /* * 1. ordinary sub query for select * from super_table * 2. all sqlobj generated by createSubqueryObj with this flag */ -#define TSDB_QUERY_TYPE_SUBQUERY 0x2 -#define TSDB_QUERY_TYPE_STABLE_SUBQUERY 0x4 // two-stage subquery for super table +#define TSDB_QUERY_TYPE_SUBQUERY 0x02U +#define TSDB_QUERY_TYPE_STABLE_SUBQUERY 0x04U // two-stage subquery for super table -#define TSDB_QUERY_TYPE_TABLE_QUERY 0x8 // query ordinary table; below only apply to client side -#define TSDB_QUERY_TYPE_STABLE_QUERY 0x10 // query on super table -#define TSDB_QUERY_TYPE_JOIN_QUERY 0x20 // join query -#define TSDB_QUERY_TYPE_PROJECTION_QUERY 0x40 // select *,columns... query -#define TSDB_QUERY_TYPE_JOIN_SEC_STAGE 0x80 // join sub query at the second stage +#define TSDB_QUERY_TYPE_TABLE_QUERY 0x08U // query ordinary table; below only apply to client side +#define TSDB_QUERY_TYPE_STABLE_QUERY 0x10U // query on super table +#define TSDB_QUERY_TYPE_JOIN_QUERY 0x20U // join query +#define TSDB_QUERY_TYPE_PROJECTION_QUERY 0x40U // select *,columns... query +#define TSDB_QUERY_TYPE_JOIN_SEC_STAGE 0x80U // join sub query at the second stage #ifdef __cplusplus } diff --git a/src/inc/tstatus.h b/src/inc/tstatus.h index 34bc7c6f27..34319e40b3 100644 --- a/src/inc/tstatus.h +++ b/src/inc/tstatus.h @@ -20,10 +20,11 @@ extern "C" { #endif -extern char *sdbDnodeStatusStr[]; -extern char *sdbDnodeBalanceStateStr[]; -extern char *sdbVnodeDropStateStr[]; -extern char *sdbVnodeSyncStatusStr[]; +const char* taosGetVnodeStatusStr(int vnodeStatus); +const char* taosGetDnodeStatusStr(int dnodeStatus); +const char* taosGetDnodeBalanceStateStr(int dnodeBalanceStatus); +const char* taosGetVnodeSyncStatusStr(int vnodeSyncStatus); +const char* taosGetVnodeDropStatusStr(int dropping); #ifdef __cplusplus } diff --git a/src/inc/ttimer.h b/src/inc/ttimer.h index ddfd3f1fdf..b9bbbb04dd 100644 --- a/src/inc/ttimer.h +++ b/src/inc/ttimer.h @@ -25,6 +25,7 @@ typedef void (*TAOS_TMR_CALLBACK)(void *, void *); extern uint32_t tmrDebugFlag; extern int taosTmrThreads; +extern uint32_t taosMaxTmrCtrl; #define tmrError(...) \ do { if (tmrDebugFlag & DEBUG_ERROR) { \ @@ -41,7 +42,6 @@ extern int taosTmrThreads; tprintf("TMR ", tmrDebugFlag, __VA_ARGS__); \ } } while(0) -#define MAX_NUM_OF_TMRCTL 32 #define MSECONDS_PER_TICK 5 void *taosTmrInit(int maxTmr, int resoultion, int longest, const char *label); diff --git a/src/inc/tutil.h b/src/inc/tutil.h index f63f8b6cec..7725bd2d1b 100644 --- a/src/inc/tutil.h +++ b/src/inc/tutil.h @@ -89,7 +89,7 @@ extern "C" { } else { \ return (x) < (y) ? -1 : 1; \ } \ - } while (0); + } while (0) #define GET_INT8_VAL(x) (*(int8_t *)(x)) #define GET_INT16_VAL(x) (*(int16_t *)(x)) @@ -169,8 +169,6 @@ int32_t taosInitTimer(void (*callback)(int), int32_t ms); */ uint32_t MurmurHash3_32(const void *key, int32_t len); -bool taosCheckDbName(char *db, char *monitordb); - bool taosMbsToUcs4(char *mbs, int32_t mbs_len, char *ucs4, int32_t ucs4_max_len); bool taosUcs4ToMbs(void *ucs4, int32_t ucs4_max_len, char *mbs); diff --git a/src/kit/shell/CMakeLists.txt b/src/kit/shell/CMakeLists.txt index 20aa0c9d31..7442367e91 100644 --- a/src/kit/shell/CMakeLists.txt +++ b/src/kit/shell/CMakeLists.txt @@ -6,7 +6,7 @@ INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/client/inc) INCLUDE_DIRECTORIES(${TD_OS_DIR}/inc) INCLUDE_DIRECTORIES(inc) -IF (TD_LINUX_64) +IF ((TD_LINUX_64) OR (TD_LINUX_32 AND TD_ARM)) AUX_SOURCE_DIRECTORY(./src SRC) LIST(REMOVE_ITEM SRC ./src/shellWindows.c) ADD_EXECUTABLE(shell ${SRC}) diff --git a/src/kit/shell/src/shellCommand.c b/src/kit/shell/src/shellCommand.c index 9bf182ab0e..46aa04c1d7 100644 --- a/src/kit/shell/src/shellCommand.c +++ b/src/kit/shell/src/shellCommand.c @@ -13,16 +13,8 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include - #define __USE_XOPEN -#include - #include "os.h" #include "shell.h" #include "shellCommand.h" diff --git a/src/kit/shell/src/shellEngine.c b/src/kit/shell/src/shellEngine.c index 324dde55d1..e2897da698 100644 --- a/src/kit/shell/src/shellEngine.c +++ b/src/kit/shell/src/shellEngine.c @@ -16,16 +16,6 @@ #define _XOPEN_SOURCE #define _DEFAULT_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include - #include "os.h" #include "shell.h" #include "shellCommand.h" @@ -445,7 +435,7 @@ int shellDumpResult(TAOS *con, char *fname, int *error_no, bool printMode) { case TSDB_DATA_TYPE_BIGINT: printf("%*lld|", l[i], *((int64_t *)row[i])); break; - case TSDB_DATA_TYPE_FLOAT: + case TSDB_DATA_TYPE_FLOAT: { #ifdef _TD_ARM_32_ float fv = 0; //memcpy(&fv, row[i], sizeof(float)); @@ -454,8 +444,9 @@ int shellDumpResult(TAOS *con, char *fname, int *error_no, bool printMode) { #else printf("%*.5f|", l[i], *((float *)row[i])); #endif + } break; - case TSDB_DATA_TYPE_DOUBLE: + case TSDB_DATA_TYPE_DOUBLE: { #ifdef _TD_ARM_32_ double dv = 0; //memcpy(&dv, row[i], sizeof(double)); @@ -464,6 +455,7 @@ int shellDumpResult(TAOS *con, char *fname, int *error_no, bool printMode) { #else printf("%*.9f|", l[i], *((double *)row[i])); #endif + } break; case TSDB_DATA_TYPE_BINARY: case TSDB_DATA_TYPE_NCHAR: @@ -528,7 +520,7 @@ int shellDumpResult(TAOS *con, char *fname, int *error_no, bool printMode) { case TSDB_DATA_TYPE_BIGINT: printf("%lld\n", *((int64_t *)row[i])); break; - case TSDB_DATA_TYPE_FLOAT: + case TSDB_DATA_TYPE_FLOAT: { #ifdef _TD_ARM_32_ float fv = 0; //memcpy(&fv, row[i], sizeof(float)); @@ -537,8 +529,9 @@ int shellDumpResult(TAOS *con, char *fname, int *error_no, bool printMode) { #else printf("%.5f\n", *((float *)row[i])); #endif + } break; - case TSDB_DATA_TYPE_DOUBLE: + case TSDB_DATA_TYPE_DOUBLE: { #ifdef _TD_ARM_32_ double dv = 0; //memcpy(&dv, row[i], sizeof(double)); @@ -547,7 +540,8 @@ int shellDumpResult(TAOS *con, char *fname, int *error_no, bool printMode) { #else printf("%.9f\n", *((double *)row[i])); #endif - break; + } + break; case TSDB_DATA_TYPE_BINARY: case TSDB_DATA_TYPE_NCHAR: memset(t_str, 0, TSDB_MAX_BYTES_PER_ROW); @@ -614,7 +608,7 @@ int shellDumpResult(TAOS *con, char *fname, int *error_no, bool printMode) { case TSDB_DATA_TYPE_BIGINT: fprintf(fp, "%lld", *((int64_t *)row[i])); break; - case TSDB_DATA_TYPE_FLOAT: + case TSDB_DATA_TYPE_FLOAT: { #ifdef _TD_ARM_32_ float fv = 0; //memcpy(&fv, row[i], sizeof(float)); @@ -623,8 +617,9 @@ int shellDumpResult(TAOS *con, char *fname, int *error_no, bool printMode) { #else fprintf(fp, "%.5f", *((float *)row[i])); #endif + } break; - case TSDB_DATA_TYPE_DOUBLE: + case TSDB_DATA_TYPE_DOUBLE: { #ifdef _TD_ARM_32_ double dv = 0; //memcpy(&dv, row[i], sizeof(double)); @@ -633,6 +628,7 @@ int shellDumpResult(TAOS *con, char *fname, int *error_no, bool printMode) { #else fprintf(fp, "%.9f", *((double *)row[i])); #endif + } break; case TSDB_DATA_TYPE_BINARY: case TSDB_DATA_TYPE_NCHAR: @@ -840,7 +836,7 @@ void shellGetGrantInfo(void *con) { TAOS_FIELD *fields = taos_fetch_fields(result); TAOS_ROW row = taos_fetch_row(result); if (row == NULL) { - fprintf(stderr, "\nGrant information is empty.\n"); + fprintf(stderr, "\nFailed to get grant information from server. Abort.\n"); exit(0); } diff --git a/src/kit/shell/src/shellLinux.c b/src/kit/shell/src/shellLinux.c index 145025cbe1..0ece4efbb2 100644 --- a/src/kit/shell/src/shellLinux.c +++ b/src/kit/shell/src/shellLinux.c @@ -13,27 +13,9 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - #define __USE_XOPEN -#include +#include "os.h" #include "shell.h" #include "shellCommand.h" diff --git a/src/kit/shell/src/shellMain.c b/src/kit/shell/src/shellMain.c index 63c9eac0db..82333020f1 100644 --- a/src/kit/shell/src/shellMain.c +++ b/src/kit/shell/src/shellMain.c @@ -13,13 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include - #include "os.h" #include "shell.h" #include "tsclient.h" diff --git a/src/kit/taosdemo/CMakeLists.txt b/src/kit/taosdemo/CMakeLists.txt index 38a28e3079..599875fa60 100644 --- a/src/kit/taosdemo/CMakeLists.txt +++ b/src/kit/taosdemo/CMakeLists.txt @@ -6,7 +6,7 @@ INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/client/inc) INCLUDE_DIRECTORIES(${TD_OS_DIR}/inc) INCLUDE_DIRECTORIES(inc) -IF (TD_LINUX_64) +IF ((TD_LINUX_64) OR (TD_LINUX_32 AND TD_ARM)) AUX_SOURCE_DIRECTORY(. SRC) ADD_EXECUTABLE(taosdemo ${SRC}) TARGET_LINK_LIBRARIES(taosdemo taos_static) diff --git a/src/kit/taosdemo/taosdemo.c b/src/kit/taosdemo/taosdemo.c index df29a72e86..47ed92e2f7 100644 --- a/src/kit/taosdemo/taosdemo.c +++ b/src/kit/taosdemo/taosdemo.c @@ -30,6 +30,9 @@ #include #include "taos.h" + +extern char configDir[]; + #pragma GCC diagnostic ignored "-Wmissing-braces" #define BUFFER_SIZE 65536 @@ -176,7 +179,7 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) { fprintf(stderr, "Invalid path %s\n", arg); return -1; } - strcpy(configDir, full_path.we_wordv[0]); + taos_options(TSDB_OPTION_CONFIGDIR, full_path.we_wordv[0]); wordfree(&full_path); break; case OPT_ABORT: diff --git a/src/kit/taosdump/CMakeLists.txt b/src/kit/taosdump/CMakeLists.txt index 2bcc020654..76b40d1c2d 100644 --- a/src/kit/taosdump/CMakeLists.txt +++ b/src/kit/taosdump/CMakeLists.txt @@ -6,7 +6,7 @@ INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/client/inc) INCLUDE_DIRECTORIES(${TD_OS_DIR}/inc) INCLUDE_DIRECTORIES(inc) -IF (TD_LINUX_64) +IF ((TD_LINUX_64) OR (TD_LINUX_32 AND TD_ARM)) AUX_SOURCE_DIRECTORY(. SRC) ADD_EXECUTABLE(taosdump ${SRC}) TARGET_LINK_LIBRARIES(taosdump taos_static) diff --git a/src/modules/http/CMakeLists.txt b/src/modules/http/CMakeLists.txt index 911c5a3515..d8dccb526a 100644 --- a/src/modules/http/CMakeLists.txt +++ b/src/modules/http/CMakeLists.txt @@ -1,7 +1,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8) PROJECT(TDengine) -IF (TD_LINUX_64) +IF ((TD_LINUX_64) OR (TD_LINUX_32 AND TD_ARM)) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/inc) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/client/inc) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/zlib-1.2.11/inc) diff --git a/src/modules/http/src/httpServer.c b/src/modules/http/src/httpServer.c index 8b981e0f84..ae8f9eeaad 100644 --- a/src/modules/http/src/httpServer.c +++ b/src/modules/http/src/httpServer.c @@ -13,23 +13,7 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "os.h" #include "taosmsg.h" #include "tlog.h" @@ -72,7 +56,7 @@ void httpRemoveContextFromEpoll(HttpThread *pThread, HttpContext *pContext) { } bool httpAlterContextState(HttpContext *pContext, HttpContextState srcState, HttpContextState destState) { - return (__sync_val_compare_and_swap_32(&pContext->state, srcState, destState) == srcState); + return (atomic_val_compare_exchange_32(&pContext->state, srcState, destState) == srcState); } void httpFreeContext(HttpServer *pServer, HttpContext *pContext); @@ -124,7 +108,7 @@ void httpCleanUpContextTimer(HttpContext *pContext) { void httpCleanUpContext(HttpContext *pContext) { httpTrace("context:%p, start the clean up operation", pContext); - __sync_val_compare_and_swap_64(&pContext->signature, pContext, 0); + atomic_val_compare_exchange_ptr(&pContext->signature, pContext, 0); if (pContext->signature != NULL) { httpTrace("context:%p is freed by another thread.", pContext); return; @@ -494,7 +478,7 @@ void httpProcessHttpData(void *param) { } else { if (httpReadData(pThread, pContext)) { (*(pThread->processData))(pContext); - __sync_fetch_and_add(&pThread->pServer->requestNum, 1); + atomic_fetch_add_32(&pThread->pServer->requestNum, 1); } } } diff --git a/src/modules/http/src/httpSystem.c b/src/modules/http/src/httpSystem.c index 5c0d9a69d6..df49251f13 100644 --- a/src/modules/http/src/httpSystem.c +++ b/src/modules/http/src/httpSystem.c @@ -50,7 +50,7 @@ int httpInitSystem() { httpServer = (HttpServer *)malloc(sizeof(HttpServer)); memset(httpServer, 0, sizeof(HttpServer)); - strcpy(httpServer->label, "taosh"); + strcpy(httpServer->label, "rest"); strcpy(httpServer->serverIp, tsHttpIp); httpServer->serverPort = tsHttpPort; httpServer->cacheContext = tsHttpCacheSessions; @@ -77,7 +77,7 @@ int httpStartSystem() { if (httpServer == NULL) { httpError("http server is null"); - return -1; + httpInitSystem(); } if (httpServer->pContextPool == NULL) { @@ -148,7 +148,7 @@ void httpCleanUpSystem() { void httpGetReqCount(int32_t *httpReqestNum) { if (httpServer != NULL) { - *httpReqestNum = __sync_fetch_and_and(&httpServer->requestNum, 0); + *httpReqestNum = atomic_exchange_32(&httpServer->requestNum, 0); } else { *httpReqestNum = 0; } diff --git a/src/modules/monitor/CMakeLists.txt b/src/modules/monitor/CMakeLists.txt index ff8ddb1c2d..44ac8aae0b 100644 --- a/src/modules/monitor/CMakeLists.txt +++ b/src/modules/monitor/CMakeLists.txt @@ -1,7 +1,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8) PROJECT(TDengine) -IF (TD_LINUX_64) +IF ((TD_LINUX_64) OR (TD_LINUX_32 AND TD_ARM)) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/inc) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/client/inc) INCLUDE_DIRECTORIES(${TD_OS_DIR}/inc) diff --git a/src/modules/monitor/src/monitorSystem.c b/src/modules/monitor/src/monitorSystem.c index 013d86050e..4d6577c8f3 100644 --- a/src/modules/monitor/src/monitorSystem.c +++ b/src/modules/monitor/src/monitorSystem.c @@ -95,6 +95,9 @@ int monitorInitSystem() { } int monitorStartSystem() { + if (monitor == NULL) { + monitorInitSystem(); + } taosTmrReset(monitorInitConn, 10, NULL, tscTmr, &monitor->initTimer); return 0; } @@ -131,7 +134,7 @@ void monitorInitConn(void *para, void *unused) { void monitorInitConnCb(void *param, TAOS_RES *result, int code) { if (code < 0) { - monitorError("monitor:%p, connect to taosd failed, code:%d", monitor->conn, code); + monitorError("monitor:%p, connect to database failed, code:%d", monitor->conn, code); taos_close(monitor->conn); monitor->conn = NULL; monitor->state = MONITOR_STATE_UN_INIT; @@ -139,7 +142,7 @@ void monitorInitConnCb(void *param, TAOS_RES *result, int code) { return; } - monitorTrace("monitor:%p, connect to taosd success, code:%d", monitor->conn, code); + monitorTrace("monitor:%p, connect to database success, code:%d", monitor->conn, code); monitorInitDatabase(); } diff --git a/src/os/darwin/inc/os.h b/src/os/darwin/inc/os.h index 83a56483b8..eabd5cd221 100644 --- a/src/os/darwin/inc/os.h +++ b/src/os/darwin/inc/os.h @@ -73,28 +73,71 @@ #define atomic_exchange_64(ptr, val) __atomic_exchange_n((ptr), (val), __ATOMIC_SEQ_CST) #define atomic_exchange_ptr(ptr, val) __atomic_exchange_n((ptr), (val), __ATOMIC_SEQ_CST) -// TODO: update prefix of below macros to 'atomic' as '__' is reserved by compiler -// and GCC suggest new code to use '__atomic' builtins to replace '__sync' builtins. -#define __sync_val_compare_and_swap_64 __sync_val_compare_and_swap -#define __sync_val_compare_and_swap_32 __sync_val_compare_and_swap -#define __sync_val_compare_and_swap_16 __sync_val_compare_and_swap -#define __sync_val_compare_and_swap_8 __sync_val_compare_and_swap -#define __sync_val_compare_and_swap_ptr __sync_val_compare_and_swap +#define atomic_val_compare_exchange_8 __sync_val_compare_and_swap +#define atomic_val_compare_exchange_16 __sync_val_compare_and_swap +#define atomic_val_compare_exchange_32 __sync_val_compare_and_swap +#define atomic_val_compare_exchange_64 __sync_val_compare_and_swap +#define atomic_val_compare_exchange_ptr __sync_val_compare_and_swap -#define __sync_add_and_fetch_64 __sync_add_and_fetch -#define __sync_add_and_fetch_32 __sync_add_and_fetch -#define __sync_add_and_fetch_16 __sync_add_and_fetch -#define __sync_add_and_fetch_8 __sync_add_and_fetch -#define __sync_add_and_fetch_ptr __sync_add_and_fetch +#define atomic_add_fetch_8(ptr, val) __atomic_add_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_add_fetch_16(ptr, val) __atomic_add_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_add_fetch_32(ptr, val) __atomic_add_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_add_fetch_64(ptr, val) __atomic_add_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_add_fetch_ptr(ptr, val) __atomic_add_fetch((ptr), (val), __ATOMIC_SEQ_CST) -#define __sync_sub_and_fetch_64 __sync_sub_and_fetch -#define __sync_sub_and_fetch_32 __sync_sub_and_fetch -#define __sync_sub_and_fetch_16 __sync_sub_and_fetch -#define __sync_sub_and_fetch_8 __sync_sub_and_fetch -#define __sync_sub_and_fetch_ptr __sync_sub_and_fetch +#define atomic_fetch_add_8(ptr, val) __atomic_fetch_add((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_add_16(ptr, val) __atomic_fetch_add((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_add_32(ptr, val) __atomic_fetch_add((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_add_64(ptr, val) __atomic_fetch_add((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_add_ptr(ptr, val) __atomic_fetch_add((ptr), (val), __ATOMIC_SEQ_CST) -int32_t __sync_val_load_32(int32_t *ptr); -void __sync_val_restore_32(int32_t *ptr, int32_t newval); +#define atomic_sub_fetch_8(ptr, val) __atomic_sub_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_sub_fetch_16(ptr, val) __atomic_sub_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_sub_fetch_32(ptr, val) __atomic_sub_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_sub_fetch_64(ptr, val) __atomic_sub_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_sub_fetch_ptr(ptr, val) __atomic_sub_fetch((ptr), (val), __ATOMIC_SEQ_CST) + +#define atomic_fetch_sub_8(ptr, val) __atomic_fetch_sub((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_sub_16(ptr, val) __atomic_fetch_sub((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_sub_32(ptr, val) __atomic_fetch_sub((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_sub_64(ptr, val) __atomic_fetch_sub((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_sub_ptr(ptr, val) __atomic_fetch_sub((ptr), (val), __ATOMIC_SEQ_CST) + +#define atomic_and_fetch_8(ptr, val) __atomic_and_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_and_fetch_16(ptr, val) __atomic_and_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_and_fetch_32(ptr, val) __atomic_and_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_and_fetch_64(ptr, val) __atomic_and_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_and_fetch_ptr(ptr, val) __atomic_and_fetch((ptr), (val), __ATOMIC_SEQ_CST) + +#define atomic_fetch_and_8(ptr, val) __atomic_fetch_and((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_and_16(ptr, val) __atomic_fetch_and((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_and_32(ptr, val) __atomic_fetch_and((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_and_64(ptr, val) __atomic_fetch_and((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_and_ptr(ptr, val) __atomic_fetch_and((ptr), (val), __ATOMIC_SEQ_CST) + +#define atomic_or_fetch_8(ptr, val) __atomic_or_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_or_fetch_16(ptr, val) __atomic_or_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_or_fetch_32(ptr, val) __atomic_or_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_or_fetch_64(ptr, val) __atomic_or_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_or_fetch_ptr(ptr, val) __atomic_or_fetch((ptr), (val), __ATOMIC_SEQ_CST) + +#define atomic_fetch_or_8(ptr, val) __atomic_fetch_or((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_or_16(ptr, val) __atomic_fetch_or((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_or_32(ptr, val) __atomic_fetch_or((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_or_64(ptr, val) __atomic_fetch_or((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_or_ptr(ptr, val) __atomic_fetch_or((ptr), (val), __ATOMIC_SEQ_CST) + +#define atomic_xor_fetch_8(ptr, val) __atomic_xor_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_xor_fetch_16(ptr, val) __atomic_xor_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_xor_fetch_32(ptr, val) __atomic_xor_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_xor_fetch_64(ptr, val) __atomic_xor_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_xor_fetch_ptr(ptr, val) __atomic_xor_fetch((ptr), (val), __ATOMIC_SEQ_CST) + +#define atomic_fetch_xor_8(ptr, val) __atomic_fetch_xor((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_xor_16(ptr, val) __atomic_fetch_xor((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_xor_32(ptr, val) __atomic_fetch_xor((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_xor_64(ptr, val) __atomic_fetch_xor((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_xor_ptr(ptr, val) __atomic_fetch_xor((ptr), (val), __ATOMIC_SEQ_CST) #define SWAP(a, b, c) \ do { \ diff --git a/src/os/darwin/src/tdarwin.c b/src/os/darwin/src/tdarwin.c index 2f97c7c376..de37c76edd 100644 --- a/src/os/darwin/src/tdarwin.c +++ b/src/os/darwin/src/tdarwin.c @@ -204,7 +204,6 @@ int taosOpenUDServerSocket(char *ip, short port) { char name[128]; pTrace("open ud socket:%s", name); - // if (tsAllowLocalhost) ip = "0.0.0.0"; sprintf(name, "%s.%d", ip, port); bzero((char *)&serverAdd, sizeof(serverAdd)); @@ -243,10 +242,6 @@ int taosInitTimer(void (*callback)(int), int ms) { return setitimer(ITIMER_REAL, &tv, NULL); } -char *taosCharsetReplace(char *charsetstr) { - return charsetstr; -} - void taosGetSystemTimezone() { // get and set default timezone SGlobalConfig *cfg_timezone = tsGetConfigOption("timezone"); diff --git a/src/os/linux/CMakeLists.txt b/src/os/linux/CMakeLists.txt index 8a4cc56f72..a702cac759 100644 --- a/src/os/linux/CMakeLists.txt +++ b/src/os/linux/CMakeLists.txt @@ -1,7 +1,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8) PROJECT(TDengine) -IF (TD_LINUX_64) +IF ((TD_LINUX_64) OR (TD_LINUX_32 AND TD_ARM)) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/inc) INCLUDE_DIRECTORIES(inc) AUX_SOURCE_DIRECTORY(src SRC) diff --git a/src/os/linux/inc/os.h b/src/os/linux/inc/os.h index 78065e6df0..82cb439a94 100644 --- a/src/os/linux/inc/os.h +++ b/src/os/linux/inc/os.h @@ -1,17 +1,17 @@ /* -* 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 . -*/ + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ #ifndef TDENGINE_PLATFORM_LINUX_H #define TDENGINE_PLATFORM_LINUX_H @@ -23,12 +23,18 @@ extern "C" { #include #include +#include #include #include +#include +#include #include +#include #include #include +#include #include +#include #include #include #include @@ -37,29 +43,35 @@ extern "C" { #include #include #include +#include +#include +#include +#include #include -#include +#include #include #include #include #include #include #include +#include #include #include #include #include +#include #include +#include #include -#include #include #include #include #include #include #include -#include -#include +#include + #define taosCloseSocket(x) \ { \ @@ -89,28 +101,71 @@ extern "C" { #define atomic_exchange_64(ptr, val) __atomic_exchange_n((ptr), (val), __ATOMIC_SEQ_CST) #define atomic_exchange_ptr(ptr, val) __atomic_exchange_n((ptr), (val), __ATOMIC_SEQ_CST) -// TODO: update prefix of below macros to 'atomic' as '__' is reserved by compiler -// and GCC suggest new code to use '__atomic' builtins to replace '__sync' builtins. -#define __sync_val_compare_and_swap_64 __sync_val_compare_and_swap -#define __sync_val_compare_and_swap_32 __sync_val_compare_and_swap -#define __sync_val_compare_and_swap_16 __sync_val_compare_and_swap -#define __sync_val_compare_and_swap_8 __sync_val_compare_and_swap -#define __sync_val_compare_and_swap_ptr __sync_val_compare_and_swap +#define atomic_val_compare_exchange_8 __sync_val_compare_and_swap +#define atomic_val_compare_exchange_16 __sync_val_compare_and_swap +#define atomic_val_compare_exchange_32 __sync_val_compare_and_swap +#define atomic_val_compare_exchange_64 __sync_val_compare_and_swap +#define atomic_val_compare_exchange_ptr __sync_val_compare_and_swap -#define __sync_add_and_fetch_64 __sync_add_and_fetch -#define __sync_add_and_fetch_32 __sync_add_and_fetch -#define __sync_add_and_fetch_16 __sync_add_and_fetch -#define __sync_add_and_fetch_8 __sync_add_and_fetch -#define __sync_add_and_fetch_ptr __sync_add_and_fetch +#define atomic_add_fetch_8(ptr, val) __atomic_add_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_add_fetch_16(ptr, val) __atomic_add_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_add_fetch_32(ptr, val) __atomic_add_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_add_fetch_64(ptr, val) __atomic_add_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_add_fetch_ptr(ptr, val) __atomic_add_fetch((ptr), (val), __ATOMIC_SEQ_CST) -#define __sync_sub_and_fetch_64 __sync_sub_and_fetch -#define __sync_sub_and_fetch_32 __sync_sub_and_fetch -#define __sync_sub_and_fetch_16 __sync_sub_and_fetch -#define __sync_sub_and_fetch_8 __sync_sub_and_fetch -#define __sync_sub_and_fetch_ptr __sync_sub_and_fetch +#define atomic_fetch_add_8(ptr, val) __atomic_fetch_add((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_add_16(ptr, val) __atomic_fetch_add((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_add_32(ptr, val) __atomic_fetch_add((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_add_64(ptr, val) __atomic_fetch_add((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_add_ptr(ptr, val) __atomic_fetch_add((ptr), (val), __ATOMIC_SEQ_CST) -int32_t __sync_val_load_32(int32_t *ptr); -void __sync_val_restore_32(int32_t *ptr, int32_t newval); +#define atomic_sub_fetch_8(ptr, val) __atomic_sub_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_sub_fetch_16(ptr, val) __atomic_sub_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_sub_fetch_32(ptr, val) __atomic_sub_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_sub_fetch_64(ptr, val) __atomic_sub_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_sub_fetch_ptr(ptr, val) __atomic_sub_fetch((ptr), (val), __ATOMIC_SEQ_CST) + +#define atomic_fetch_sub_8(ptr, val) __atomic_fetch_sub((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_sub_16(ptr, val) __atomic_fetch_sub((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_sub_32(ptr, val) __atomic_fetch_sub((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_sub_64(ptr, val) __atomic_fetch_sub((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_sub_ptr(ptr, val) __atomic_fetch_sub((ptr), (val), __ATOMIC_SEQ_CST) + +#define atomic_and_fetch_8(ptr, val) __atomic_and_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_and_fetch_16(ptr, val) __atomic_and_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_and_fetch_32(ptr, val) __atomic_and_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_and_fetch_64(ptr, val) __atomic_and_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_and_fetch_ptr(ptr, val) __atomic_and_fetch((ptr), (val), __ATOMIC_SEQ_CST) + +#define atomic_fetch_and_8(ptr, val) __atomic_fetch_and((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_and_16(ptr, val) __atomic_fetch_and((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_and_32(ptr, val) __atomic_fetch_and((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_and_64(ptr, val) __atomic_fetch_and((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_and_ptr(ptr, val) __atomic_fetch_and((ptr), (val), __ATOMIC_SEQ_CST) + +#define atomic_or_fetch_8(ptr, val) __atomic_or_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_or_fetch_16(ptr, val) __atomic_or_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_or_fetch_32(ptr, val) __atomic_or_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_or_fetch_64(ptr, val) __atomic_or_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_or_fetch_ptr(ptr, val) __atomic_or_fetch((ptr), (val), __ATOMIC_SEQ_CST) + +#define atomic_fetch_or_8(ptr, val) __atomic_fetch_or((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_or_16(ptr, val) __atomic_fetch_or((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_or_32(ptr, val) __atomic_fetch_or((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_or_64(ptr, val) __atomic_fetch_or((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_or_ptr(ptr, val) __atomic_fetch_or((ptr), (val), __ATOMIC_SEQ_CST) + +#define atomic_xor_fetch_8(ptr, val) __atomic_xor_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_xor_fetch_16(ptr, val) __atomic_xor_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_xor_fetch_32(ptr, val) __atomic_xor_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_xor_fetch_64(ptr, val) __atomic_xor_fetch((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_xor_fetch_ptr(ptr, val) __atomic_xor_fetch((ptr), (val), __ATOMIC_SEQ_CST) + +#define atomic_fetch_xor_8(ptr, val) __atomic_fetch_xor((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_xor_16(ptr, val) __atomic_fetch_xor((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_xor_32(ptr, val) __atomic_fetch_xor((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_xor_64(ptr, val) __atomic_fetch_xor((ptr), (val), __ATOMIC_SEQ_CST) +#define atomic_fetch_xor_ptr(ptr, val) __atomic_fetch_xor((ptr), (val), __ATOMIC_SEQ_CST) #define SWAP(a, b, c) \ do { \ @@ -167,10 +222,13 @@ bool taosSkipSocketCheck(); int64_t str2int64(char *str); +void taosSetCoreDump(); + + #define BUILDIN_CLZL(val) __builtin_clzl(val) -#define BUILDIN_CLZ(val) __builtin_clz(val) +#define BUILDIN_CLZ(val) __builtin_clz(val) #define BUILDIN_CTZL(val) __builtin_ctzl(val) -#define BUILDIN_CTZ(val) __builtin_ctz(val) +#define BUILDIN_CTZ(val) __builtin_ctz(val) #ifdef __cplusplus } diff --git a/src/os/linux/src/tlinux.c b/src/os/linux/src/tlinux.c index b5271006e2..6a7225b476 100644 --- a/src/os/linux/src/tlinux.c +++ b/src/os/linux/src/tlinux.c @@ -197,7 +197,6 @@ int taosOpenUDServerSocket(char *ip, short port) { char name[128]; pTrace("open ud socket:%s", name); - // if (tsAllowLocalhost) ip = "0.0.0.0"; sprintf(name, "%s.%d", ip, port); bzero((char *)&serverAdd, sizeof(serverAdd)); @@ -340,11 +339,3 @@ bool taosSkipSocketCheck() { return false; } - -int32_t __sync_val_load_32(int32_t *ptr) { - return __atomic_load_n(ptr, __ATOMIC_ACQUIRE); -} - -void __sync_val_restore_32(int32_t *ptr, int32_t newval) { - __atomic_store_n(ptr, newval, __ATOMIC_RELEASE); -} diff --git a/src/os/linux/src/tsystem.c b/src/os/linux/src/tsystem.c index fc2d9860d7..fa41e8e6d0 100644 --- a/src/os/linux/src/tsystem.c +++ b/src/os/linux/src/tsystem.c @@ -25,6 +25,14 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include +#include #include "tglobalcfg.h" #include "tlog.h" @@ -210,25 +218,6 @@ void taosGetSystemTimezone() { pPrint("timezone not configured, set to system default:%s", tsTimezone); } -typedef struct CharsetPair { - char *oldCharset; - char *newCharset; -} CharsetPair; - -char *taosCharsetReplace(char *charsetstr) { - CharsetPair charsetRep[] = { - {"utf8", "UTF-8"}, {"936", "CP936"}, - }; - - for (int32_t i = 0; i < tListLen(charsetRep); ++i) { - if (strcasecmp(charsetRep[i].oldCharset, charsetstr) == 0) { - return strdup(charsetRep[i].newCharset); - } - } - - return strdup(charsetstr); -} - /* * POSIX format locale string: * (Language Strings)_(Country/Region Strings).(code_page) @@ -595,4 +584,123 @@ void taosKillSystem() { // SIGINT pPrint("taosd will shut down soon"); kill(tsProcId, 2); -} \ No newline at end of file +} + + +int _sysctl(struct __sysctl_args *args ); +void taosSetCoreDump() { + // 1. set ulimit -c unlimited + struct rlimit rlim; + struct rlimit rlim_new; + if (getrlimit(RLIMIT_CORE, &rlim) == 0) { + pPrint("the old unlimited para: rlim_cur=%d, rlim_max=%d", rlim.rlim_cur, rlim.rlim_max); + rlim_new.rlim_cur = RLIM_INFINITY; + rlim_new.rlim_max = RLIM_INFINITY; + if (setrlimit(RLIMIT_CORE, &rlim_new) != 0) { + pPrint("set unlimited fail, error: %s", strerror(errno)); + rlim_new.rlim_cur = rlim.rlim_max; + rlim_new.rlim_max = rlim.rlim_max; + (void)setrlimit(RLIMIT_CORE, &rlim_new); + } + } + + if (getrlimit(RLIMIT_CORE, &rlim) == 0) { + pPrint("the new unlimited para: rlim_cur=%d, rlim_max=%d", rlim.rlim_cur, rlim.rlim_max); + } + + // 2. set the path for saving core file + struct __sysctl_args args; + int old_usespid = 0; + size_t old_len = 0; + int new_usespid = 1; + size_t new_len = sizeof(new_usespid); + + int name[] = {CTL_KERN, KERN_CORE_USES_PID}; + + memset(&args, 0, sizeof(struct __sysctl_args)); + args.name = name; + args.nlen = sizeof(name)/sizeof(name[0]); + args.oldval = &old_usespid; + args.oldlenp = &old_len; + args.newval = &new_usespid; + args.newlen = new_len; + + old_len = sizeof(old_usespid); + + if (syscall(SYS__sysctl, &args) == -1) { + pPrint("_sysctl(kern_core_uses_pid) set fail: %s", strerror(errno)); + } + + pPrint("The old core_uses_pid[%d]: %d", old_len, old_usespid); + + + old_usespid = 0; + old_len = 0; + memset(&args, 0, sizeof(struct __sysctl_args)); + args.name = name; + args.nlen = sizeof(name)/sizeof(name[0]); + args.oldval = &old_usespid; + args.oldlenp = &old_len; + + old_len = sizeof(old_usespid); + + if (syscall(SYS__sysctl, &args) == -1) { + pPrint("_sysctl(kern_core_uses_pid) get fail: %s", strerror(errno)); + } + + pPrint("The new core_uses_pid[%d]: %d", old_len, old_usespid); + +#if 0 + // 3. set the path for saving core file + int status; + char coredump_dir[32] = "/var/log/taosdump"; + if (opendir(coredump_dir) == NULL) { + status = mkdir(coredump_dir, S_IRWXU | S_IRWXG | S_IRWXO); + if (status) { + pPrint("mkdir fail, error: %s\n", strerror(errno)); + } + } + + // 4. set kernel.core_pattern + struct __sysctl_args args; + char old_corefile[128]; + size_t old_len; + char new_corefile[128] = "/var/log/taosdump/core-%e-%p"; + size_t new_len = sizeof(new_corefile); + + int name[] = {CTL_KERN, KERN_CORE_PATTERN}; + + memset(&args, 0, sizeof(struct __sysctl_args)); + args.name = name; + args.nlen = sizeof(name)/sizeof(name[0]); + args.oldval = old_corefile; + args.oldlenp = &old_len; + args.newval = new_corefile; + args.newlen = new_len; + + old_len = sizeof(old_corefile); + + if (syscall(SYS__sysctl, &args) == -1) { + pPrint("_sysctl(kern_core_pattern) set fail: %s", strerror(errno)); + } + + pPrint("The old kern_core_pattern: %*s\n", old_len, old_corefile); + + + memset(&args, 0, sizeof(struct __sysctl_args)); + args.name = name; + args.nlen = sizeof(name)/sizeof(name[0]); + args.oldval = old_corefile; + args.oldlenp = &old_len; + + old_len = sizeof(old_corefile); + + if (syscall(SYS__sysctl, &args) == -1) { + pPrint("_sysctl(kern_core_pattern) get fail: %s", strerror(errno)); + } + + pPrint("The new kern_core_pattern: %*s\n", old_len, old_corefile); +#endif + +} + diff --git a/src/os/windows/inc/os.h b/src/os/windows/inc/os.h index 3f61ff12b5..a1a4bdfa5c 100644 --- a/src/os/windows/inc/os.h +++ b/src/os/windows/inc/os.h @@ -81,6 +81,10 @@ extern "C" { #if defined(_M_ARM) || defined(_M_ARM64) +/* the '__iso_volatile' functions does not use a memory fence, so these + * definitions are incorrect, comment out as we don't support Windows on + * ARM at present. + #define atomic_load_8(ptr) __iso_volatile_load8((const volatile __int8*)(ptr)) #define atomic_load_16(ptr) __iso_volatile_load16((const volatile __int16*)(ptr)) #define atomic_load_32(ptr) __iso_volatile_load32((const volatile __int32*)(ptr)) @@ -98,7 +102,7 @@ extern "C" { #define atomic_load_ptr atomic_load_32 #define atomic_store_ptr atomic_store_32 #endif - +*/ #else #define atomic_load_8(ptr) (*(char volatile*)(ptr)) @@ -121,35 +125,152 @@ extern "C" { #define atomic_exchange_64(ptr, val) _InterlockedExchange64((__int64 volatile*)(ptr), (__int64)(val)) #define atomic_exchange_ptr(ptr, val) _InterlockedExchangePointer((void* volatile*)(ptr), (void*)(val)) -#define __sync_val_compare_and_swap_8(ptr, oldval, newval) _InterlockedCompareExchange8((char volatile*)(ptr), (char)(newval), (char)(oldval)) -#define __sync_val_compare_and_swap_16(ptr, oldval, newval) _InterlockedCompareExchange16((short volatile*)(ptr), (short)(newval), (short)(oldval)) -#define __sync_val_compare_and_swap_32(ptr, oldval, newval) _InterlockedCompareExchange((long volatile*)(ptr), (long)(newval), (long)(oldval)) -#define __sync_val_compare_and_swap_64(ptr, oldval, newval) _InterlockedCompareExchange64((__int64 volatile*)(ptr), (__int64)(newval), (__int64)(oldval)) -#define __sync_val_compare_and_swap_ptr(ptr, oldval, newval) _InterlockedCompareExchangePointer((void* volatile*)(ptr), (void*)(newval), (void*)(oldval)) +#define atomic_val_compare_exchange_8(ptr, oldval, newval) _InterlockedCompareExchange8((char volatile*)(ptr), (char)(newval), (char)(oldval)) +#define atomic_val_compare_exchange_16(ptr, oldval, newval) _InterlockedCompareExchange16((short volatile*)(ptr), (short)(newval), (short)(oldval)) +#define atomic_val_compare_exchange_32(ptr, oldval, newval) _InterlockedCompareExchange((long volatile*)(ptr), (long)(newval), (long)(oldval)) +#define atomic_val_compare_exchange_64(ptr, oldval, newval) _InterlockedCompareExchange64((__int64 volatile*)(ptr), (__int64)(newval), (__int64)(oldval)) +#define atomic_val_compare_exchange_ptr(ptr, oldval, newval) _InterlockedCompareExchangePointer((void* volatile*)(ptr), (void*)(newval), (void*)(oldval)) -char interlocked_add_8(char volatile *ptr, char val); -short interlocked_add_16(short volatile *ptr, short val); -long interlocked_add_32(long volatile *ptr, long val); -__int64 interlocked_add_64(__int64 volatile *ptr, __int64 val); +char interlocked_add_fetch_8(char volatile *ptr, char val); +short interlocked_add_fetch_16(short volatile *ptr, short val); +long interlocked_add_fetch_32(long volatile *ptr, long val); +__int64 interlocked_add_fetch_64(__int64 volatile *ptr, __int64 val); -#define __sync_add_and_fetch_8(ptr, val) interlocked_add_8((char volatile*)(ptr), (char)(val)) -#define __sync_add_and_fetch_16(ptr, val) interlocked_add_16((short volatile*)(ptr), (short)(val)) -#define __sync_add_and_fetch_32(ptr, val) interlocked_add_32((long volatile*)(ptr), (long)(val)) -#define __sync_add_and_fetch_64(ptr, val) interlocked_add_64((__int64 volatile*)(ptr), (__int64)(val)) +#define atomic_add_fetch_8(ptr, val) interlocked_add_fetch_8((char volatile*)(ptr), (char)(val)) +#define atomic_add_fetch_16(ptr, val) interlocked_add_fetch_16((short volatile*)(ptr), (short)(val)) +#define atomic_add_fetch_32(ptr, val) interlocked_add_fetch_32((long volatile*)(ptr), (long)(val)) +#define atomic_add_fetch_64(ptr, val) interlocked_add_fetch_64((__int64 volatile*)(ptr), (__int64)(val)) #ifdef _WIN64 - #define __sync_add_and_fetch_ptr __sync_add_and_fetch_64 + #define atomic_add_fetch_ptr atomic_add_fetch_64 #else - #define __sync_add_and_fetch_ptr __sync_add_and_fetch_32 + #define atomic_add_fetch_ptr atomic_add_fetch_32 #endif -#define __sync_sub_and_fetch_8(ptr, val) __sync_add_and_fetch_8((ptr), -(val)) -#define __sync_sub_and_fetch_16(ptr, val) __sync_add_and_fetch_16((ptr), -(val)) -#define __sync_sub_and_fetch_32(ptr, val) __sync_add_and_fetch_32((ptr), -(val)) -#define __sync_sub_and_fetch_64(ptr, val) __sync_add_and_fetch_64((ptr), -(val)) -#define __sync_sub_and_fetch_ptr(ptr, val) __sync_add_and_fetch_ptr((ptr), -(val)) +#define atomic_fetch_add_8(ptr, val) _InterlockedExchangeAdd8((char volatile*)(ptr), (char)(val)) +#define atomic_fetch_add_16(ptr, val) _InterlockedExchangeAdd16((short volatile*)(ptr), (short)(val)) +#define atomic_fetch_add_32(ptr, val) _InterlockedExchangeAdd((long volatile*)(ptr), (long)(val)) +#define atomic_fetch_add_64(ptr, val) _InterlockedExchangeAdd64((__int64 volatile*)(ptr), (__int64)(val)) +#ifdef _WIN64 + #define atomic_fetch_add_ptr atomic_fetch_add_64 +#else + #define atomic_fetch_add_ptr atomic_fetch_add_32 +#endif -int32_t __sync_val_load_32(int32_t *ptr); -void __sync_val_restore_32(int32_t *ptr, int32_t newval); +#define atomic_sub_fetch_8(ptr, val) interlocked_add_fetch_8((char volatile*)(ptr), -(char)(val)) +#define atomic_sub_fetch_16(ptr, val) interlocked_add_fetch_16((short volatile*)(ptr), -(short)(val)) +#define atomic_sub_fetch_32(ptr, val) interlocked_add_fetch_32((long volatile*)(ptr), -(long)(val)) +#define atomic_sub_fetch_64(ptr, val) interlocked_add_fetch_64((__int64 volatile*)(ptr), -(__int64)(val)) +#ifdef _WIN64 + #define atomic_sub_fetch_ptr atomic_sub_fetch_64 +#else + #define atomic_sub_fetch_ptr atomic_sub_fetch_32 +#endif + +#define atomic_fetch_sub_8(ptr, val) _InterlockedExchangeAdd8((char volatile*)(ptr), -(char)(val)) +#define atomic_fetch_sub_16(ptr, val) _InterlockedExchangeAdd16((short volatile*)(ptr), -(short)(val)) +#define atomic_fetch_sub_32(ptr, val) _InterlockedExchangeAdd((long volatile*)(ptr), -(long)(val)) +#define atomic_fetch_sub_64(ptr, val) _InterlockedExchangeAdd64((__int64 volatile*)(ptr), -(__int64)(val)) +#ifdef _WIN64 + #define atomic_fetch_sub_ptr atomic_fetch_sub_64 +#else + #define atomic_fetch_sub_ptr atomic_fetch_sub_32 +#endif + +char interlocked_and_fetch_8(char volatile* ptr, char val); +short interlocked_and_fetch_16(short volatile* ptr, short val); +long interlocked_and_fetch_32(long volatile* ptr, long val); +__int64 interlocked_and_fetch_64(__int64 volatile* ptr, __int64 val); + +#define atomic_and_fetch_8(ptr, val) interlocked_and_fetch_8((char volatile*)(ptr), (char)(val)) +#define atomic_and_fetch_16(ptr, val) interlocked_and_fetch_16((short volatile*)(ptr), (short)(val)) +#define atomic_and_fetch_32(ptr, val) interlocked_and_fetch_32((long volatile*)(ptr), (long)(val)) +#define atomic_and_fetch_64(ptr, val) interlocked_and_fetch_64((__int64 volatile*)(ptr), (__int64)(val)) +#ifdef _WIN64 + #define atomic_and_fetch_ptr atomic_and_fetch_64 +#else + #define atomic_and_fetch_ptr atomic_and_fetch_32 +#endif + +#define atomic_fetch_and_8(ptr, val) _InterlockedAnd8((char volatile*)(ptr), (char)(val)) +#define atomic_fetch_and_16(ptr, val) _InterlockedAnd16((short volatile*)(ptr), (short)(val)) +#define atomic_fetch_and_32(ptr, val) _InterlockedAnd((long volatile*)(ptr), (long)(val)) + +#ifdef _M_IX86 + __int64 interlocked_fetch_and_64(__int64 volatile* ptr, __int64 val); + #define atomic_fetch_and_64(ptr, val) interlocked_fetch_and_64((__int64 volatile*)(ptr), (__int64)(val)) +#else + #define atomic_fetch_and_64(ptr, val) _InterlockedAnd64((__int64 volatile*)(ptr), (__int64)(val)) +#endif + +#ifdef _WIN64 + #define atomic_fetch_and_ptr atomic_fetch_and_64 +#else + #define atomic_fetch_and_ptr atomic_fetch_and_32 +#endif + +char interlocked_or_fetch_8(char volatile* ptr, char val); +short interlocked_or_fetch_16(short volatile* ptr, short val); +long interlocked_or_fetch_32(long volatile* ptr, long val); +__int64 interlocked_or_fetch_64(__int64 volatile* ptr, __int64 val); + +#define atomic_or_fetch_8(ptr, val) interlocked_or_fetch_8((char volatile*)(ptr), (char)(val)) +#define atomic_or_fetch_16(ptr, val) interlocked_or_fetch_16((short volatile*)(ptr), (short)(val)) +#define atomic_or_fetch_32(ptr, val) interlocked_or_fetch_32((long volatile*)(ptr), (long)(val)) +#define atomic_or_fetch_64(ptr, val) interlocked_or_fetch_64((__int64 volatile*)(ptr), (__int64)(val)) +#ifdef _WIN64 + #define atomic_or_fetch_ptr atomic_or_fetch_64 +#else + #define atomic_or_fetch_ptr atomic_or_fetch_32 +#endif + +#define atomic_fetch_or_8(ptr, val) _InterlockedOr8((char volatile*)(ptr), (char)(val)) +#define atomic_fetch_or_16(ptr, val) _InterlockedOr16((short volatile*)(ptr), (short)(val)) +#define atomic_fetch_or_32(ptr, val) _InterlockedOr((long volatile*)(ptr), (long)(val)) + +#ifdef _M_IX86 + __int64 interlocked_fetch_or_64(__int64 volatile* ptr, __int64 val); + #define atomic_fetch_or_64(ptr, val) interlocked_fetch_or_64((__int64 volatile*)(ptr), (__int64)(val)) +#else + #define atomic_fetch_or_64(ptr, val) _InterlockedOr64((__int64 volatile*)(ptr), (__int64)(val)) +#endif + +#ifdef _WIN64 + #define atomic_fetch_or_ptr atomic_fetch_or_64 +#else + #define atomic_fetch_or_ptr atomic_fetch_or_32 +#endif + +char interlocked_xor_fetch_8(char volatile* ptr, char val); +short interlocked_xor_fetch_16(short volatile* ptr, short val); +long interlocked_xor_fetch_32(long volatile* ptr, long val); +__int64 interlocked_xor_fetch_64(__int64 volatile* ptr, __int64 val); + +#define atomic_xor_fetch_8(ptr, val) interlocked_xor_fetch_8((char volatile*)(ptr), (char)(val)) +#define atomic_xor_fetch_16(ptr, val) interlocked_xor_fetch_16((short volatile*)(ptr), (short)(val)) +#define atomic_xor_fetch_32(ptr, val) interlocked_xor_fetch_32((long volatile*)(ptr), (long)(val)) +#define atomic_xor_fetch_64(ptr, val) interlocked_xor_fetch_64((__int64 volatile*)(ptr), (__int64)(val)) +#ifdef _WIN64 + #define atomic_xor_fetch_ptr atomic_xor_fetch_64 +#else + #define atomic_xor_fetch_ptr atomic_xor_fetch_32 +#endif + +#define atomic_fetch_xor_8(ptr, val) _InterlockedXor8((char volatile*)(ptr), (char)(val)) +#define atomic_fetch_xor_16(ptr, val) _InterlockedXor16((short volatile*)(ptr), (short)(val)) +#define atomic_fetch_xor_32(ptr, val) _InterlockedXor((long volatile*)(ptr), (long)(val)) + +#ifdef _M_IX86 + __int64 interlocked_fetch_xor_64(__int64 volatile* ptr, __int64 val); + #define atomic_fetch_xor_64(ptr, val) interlocked_fetch_xor_64((__int64 volatile*)(ptr), (__int64)(val)) +#else + #define atomic_fetch_xor_64(ptr, val) _InterlockedXor64((__int64 volatile*)(ptr), (__int64)(val)) +#endif + +#ifdef _WIN64 + #define atomic_fetch_xor_ptr atomic_fetch_xor_64 +#else + #define atomic_fetch_xor_ptr atomic_fetch_xor_32 +#endif #define SWAP(a, b, c) \ do { \ diff --git a/src/os/windows/src/twindows.c b/src/os/windows/src/twindows.c index 106cb903b1..9089f90d6a 100644 --- a/src/os/windows/src/twindows.c +++ b/src/os/windows/src/twindows.c @@ -66,37 +66,145 @@ int taosSetSockOpt(int socketfd, int level, int optname, void *optval, int optle return setsockopt(socketfd, level, optname, optval, optlen); } - -char interlocked_add_8(char volatile* ptr, char val) { +// add +char interlocked_add_fetch_8(char volatile* ptr, char val) { return _InterlockedExchangeAdd8(ptr, val) + val; } -short interlocked_add_16(short volatile* ptr, short val) { +short interlocked_add_fetch_16(short volatile* ptr, short val) { return _InterlockedExchangeAdd16(ptr, val) + val; } -long interlocked_add_32(long volatile* ptr, long val) { +long interlocked_add_fetch_32(long volatile* ptr, long val) { return _InterlockedExchangeAdd(ptr, val) + val; } -__int64 interlocked_add_64(__int64 volatile* ptr, __int64 val) { +__int64 interlocked_add_fetch_64(__int64 volatile* ptr, __int64 val) { return _InterlockedExchangeAdd64(ptr, val) + val; } -int32_t __sync_val_load_32(int32_t *ptr) { - return InterlockedOr(ptr, 0); +// and +char interlocked_and_fetch_8(char volatile* ptr, char val) { + return _InterlockedAnd8(ptr, val) & val; } -void __sync_val_restore_32(int32_t *ptr, int32_t newval) { - InterlockedCompareExchange(ptr, *ptr, newval); +short interlocked_and_fetch_16(short volatile* ptr, short val) { + return _InterlockedAnd16(ptr, val) & val; } +long interlocked_and_fetch_32(long volatile* ptr, long val) { + return _InterlockedAnd(ptr, val) & val; +} + +#ifndef _M_IX86 + +__int64 interlocked_and_fetch_64(__int64 volatile* ptr, __int64 val) { + return _InterlockedAnd64(ptr, val) & val; +} + +#else + +__int64 interlocked_and_fetch_64(__int64 volatile* ptr, __int64 val) { + __int64 old, res; + do { + old = *ptr; + res = old & val; + } while(_InterlockedCompareExchange64(ptr, res, old) != old); + return res; +} + +__int64 interlocked_fetch_and_64(__int64 volatile* ptr, __int64 val) { + __int64 old; + do { + old = *ptr; + } while(_InterlockedCompareExchange64(ptr, old & val, old) != old); + return old; +} + +#endif + +// or +char interlocked_or_fetch_8(char volatile* ptr, char val) { + return _InterlockedOr8(ptr, val) | val; +} + +short interlocked_or_fetch_16(short volatile* ptr, short val) { + return _InterlockedOr16(ptr, val) | val; +} + +long interlocked_or_fetch_32(long volatile* ptr, long val) { + return _InterlockedOr(ptr, val) | val; +} + +#ifndef _M_IX86 + +__int64 interlocked_or_fetch_64(__int64 volatile* ptr, __int64 val) { + return _InterlockedOr64(ptr, val) & val; +} + +#else + +__int64 interlocked_or_fetch_64(__int64 volatile* ptr, __int64 val) { + __int64 old, res; + do { + old = *ptr; + res = old | val; + } while(_InterlockedCompareExchange64(ptr, res, old) != old); + return res; +} + +__int64 interlocked_fetch_or_64(__int64 volatile* ptr, __int64 val) { + __int64 old; + do { + old = *ptr; + } while(_InterlockedCompareExchange64(ptr, old | val, old) != old); + return old; +} + +#endif + +// xor +char interlocked_xor_fetch_8(char volatile* ptr, char val) { + return _InterlockedXor8(ptr, val) ^ val; +} + +short interlocked_xor_fetch_16(short volatile* ptr, short val) { + return _InterlockedXor16(ptr, val) ^ val; +} + +long interlocked_xor_fetch_32(long volatile* ptr, long val) { + return _InterlockedXor(ptr, val) ^ val; +} + +#ifndef _M_IX86 + +__int64 interlocked_xor_fetch_64(__int64 volatile* ptr, __int64 val) { + return _InterlockedXor64(ptr, val) ^ val; +} + +#else + +__int64 interlocked_xor_fetch_64(__int64 volatile* ptr, __int64 val) { + __int64 old, res; + do { + old = *ptr; + res = old ^ val; + } while(_InterlockedCompareExchange64(ptr, res, old) != old); + return res; +} + +__int64 interlocked_fetch_xor_64(__int64 volatile* ptr, __int64 val) { + __int64 old; + do { + old = *ptr; + } while(_InterlockedCompareExchange64(ptr, old ^ val, old) != old); + return old; +} + +#endif + void tsPrintOsInfo() {} -char *taosCharsetReplace(char *charsetstr) { - return charsetstr; -} - void taosGetSystemTimezone() { // get and set default timezone SGlobalConfig *cfg_timezone = tsGetConfigOption("timezone"); diff --git a/src/rpc/CMakeLists.txt b/src/rpc/CMakeLists.txt index b3a761e3df..8d54d5fda5 100644 --- a/src/rpc/CMakeLists.txt +++ b/src/rpc/CMakeLists.txt @@ -5,7 +5,7 @@ INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/inc) INCLUDE_DIRECTORIES(${TD_OS_DIR}/inc) INCLUDE_DIRECTORIES(inc) -IF (TD_LINUX_64) +IF ((TD_LINUX_64) OR (TD_LINUX_32 AND TD_ARM)) AUX_SOURCE_DIRECTORY(./src SRC) ELSEIF (TD_DARWIN_64) LIST(APPEND SRC ./src/thaship.c) diff --git a/src/rpc/src/thaship.c b/src/rpc/src/thaship.c index 4cc6feeea3..2c46e53258 100644 --- a/src/rpc/src/thaship.c +++ b/src/rpc/src/thaship.c @@ -13,14 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include -#include - #include "os.h" #include "tlog.h" #include "tmempool.h" diff --git a/src/rpc/src/tmsghdr.c b/src/rpc/src/tmsghdr.c index 960d1fc5f6..a46f182b1d 100644 --- a/src/rpc/src/tmsghdr.c +++ b/src/rpc/src/tmsghdr.c @@ -13,10 +13,7 @@ * along with this program. If not, see . */ -#include -#include -#include -#include +#include "os.h" void taosFreeMsgHdr(void *hdr) { struct msghdr *msgHdr = (struct msghdr *)hdr; diff --git a/src/rpc/src/trpc.c b/src/rpc/src/trpc.c index 6f92154e9e..5403caac9d 100644 --- a/src/rpc/src/trpc.c +++ b/src/rpc/src/trpc.c @@ -13,16 +13,8 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include -#include -#include - #include "os.h" + #include "shash.h" #include "taosmsg.h" #include "tidpool.h" @@ -164,8 +156,8 @@ char *taosBuildReqHeader(void *param, char type, char *msg) { pHeader->spi = 0; pHeader->tcp = 0; pHeader->encrypt = 0; - pHeader->tranId = __sync_add_and_fetch_32(&pConn->tranId, 1); - if (pHeader->tranId == 0) pHeader->tranId = __sync_add_and_fetch_32(&pConn->tranId, 1); + pHeader->tranId = atomic_add_fetch_32(&pConn->tranId, 1); + if (pHeader->tranId == 0) pHeader->tranId = atomic_add_fetch_32(&pConn->tranId, 1); pHeader->sourceId = pConn->ownId; pHeader->destId = pConn->peerId; @@ -196,8 +188,8 @@ char *taosBuildReqMsgWithSize(void *param, char type, int size) { pHeader->spi = 0; pHeader->tcp = 0; pHeader->encrypt = 0; - pHeader->tranId = __sync_add_and_fetch_32(&pConn->tranId, 1); - if (pHeader->tranId == 0) pHeader->tranId = __sync_add_and_fetch_32(&pConn->tranId, 1); + pHeader->tranId = atomic_add_fetch_32(&pConn->tranId, 1); + if (pHeader->tranId == 0) pHeader->tranId = atomic_add_fetch_32(&pConn->tranId, 1); pHeader->sourceId = pConn->ownId; pHeader->destId = pConn->peerId; @@ -362,6 +354,8 @@ int taosOpenRpcChannWithQ(void *handle, int cid, int sessions, void *qhandle) { STaosRpc * pServer = (STaosRpc *)handle; SRpcChann *pChann; + tTrace("cid:%d, handle:%p open rpc chann", cid, handle); + if (pServer == NULL) return -1; if (cid >= pServer->numOfChanns || cid < 0) { tError("%s: cid:%d, chann is out of range, max:%d", pServer->label, cid, pServer->numOfChanns); @@ -410,6 +404,8 @@ void taosCloseRpcChann(void *handle, int cid) { STaosRpc * pServer = (STaosRpc *)handle; SRpcChann *pChann; + tTrace("cid:%d, handle:%p close rpc chann", cid, handle); + if (pServer == NULL) return; if (cid >= pServer->numOfChanns || cid < 0) { tError("%s cid:%d, chann is out of range, max:%d", pServer->label, cid, pServer->numOfChanns); diff --git a/src/rpc/src/tstring.c b/src/rpc/src/tstring.c index c7afe54d9a..e3daca130a 100644 --- a/src/rpc/src/tstring.c +++ b/src/rpc/src/tstring.c @@ -145,7 +145,7 @@ char *tsError[] = {"success", "not online", "send failed", "not active session", // 20 - "insert failed", + "invalid vnode id", "App error", "invalid IE", "invalid value", @@ -178,7 +178,7 @@ char *tsError[] = {"success", "others", "can't remove dnode which is master", "wrong schema", - "no results", + "vnode not active(not created yet or dropped already)", "num of users execeed maxUsers", //55 "num of databases execeed maxDbs", "num of tables execeed maxTables", @@ -233,9 +233,13 @@ char *tsError[] = {"success", "invalid query message", "timestamp disordered in cache block", "timestamp disordered in file block", - "invalid commit log", //110 - "server no disk space", + "invalid commit log", + "server no disk space", //110 "only super table has metric meta info", "tags value not unique for join", "invalid submit message", + "not active table(not created yet or deleted already)", //114 + "invalid table id", + "invalid vnode status", //116 + "failed to lock resources", }; diff --git a/src/rpc/src/ttcpclient.c b/src/rpc/src/ttcpclient.c index e12f1e1728..8e6f91a661 100644 --- a/src/rpc/src/ttcpclient.c +++ b/src/rpc/src/ttcpclient.c @@ -13,15 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include -#include -#include - #include "os.h" #include "taosmsg.h" #include "tlog.h" diff --git a/src/rpc/src/ttcpserver.c b/src/rpc/src/ttcpserver.c index b6b0e07230..fee506256b 100644 --- a/src/rpc/src/ttcpserver.c +++ b/src/rpc/src/ttcpserver.c @@ -13,16 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include -#include -#include -#include - #include "os.h" #include "taosmsg.h" #include "tlog.h" @@ -195,8 +185,9 @@ static void taosProcessTcpData(void *param) { void *buffer = malloc(1024); int headLen = taosReadMsg(pFdObj->fd, buffer, sizeof(STaosHeader)); + if (headLen != sizeof(STaosHeader)) { - tError("%s read error, headLen:%d", pThreadObj->label, headLen); + tError("%s read error, headLen:%d, errno:%d", pThreadObj->label, headLen, errno); taosCleanUpFdObj(pFdObj); tfree(buffer); continue; diff --git a/src/rpc/src/tudp.c b/src/rpc/src/tudp.c index 1940c89e85..7a4961f90c 100644 --- a/src/rpc/src/tudp.c +++ b/src/rpc/src/tudp.c @@ -13,15 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include -#include -#include - #include "os.h" #include "taosmsg.h" #include "thash.h" diff --git a/src/sdb/CMakeLists.txt b/src/sdb/CMakeLists.txt index 76b407fded..6b29eefde6 100644 --- a/src/sdb/CMakeLists.txt +++ b/src/sdb/CMakeLists.txt @@ -5,11 +5,11 @@ INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/inc) INCLUDE_DIRECTORIES(${TD_OS_DIR}/inc) INCLUDE_DIRECTORIES(inc) -IF (TD_LINUX_64) +IF ((TD_LINUX_64) OR (TD_LINUX_32 AND TD_ARM)) AUX_SOURCE_DIRECTORY(src SRC) ADD_LIBRARY(sdb ${SRC}) TARGET_LINK_LIBRARIES(sdb trpc) IF (TD_CLUSTER) TARGET_LINK_LIBRARIES(sdb sdb_cluster) ENDIF () -ENDIF () \ No newline at end of file +ENDIF () diff --git a/src/sdb/src/hashstr.c b/src/sdb/src/hashstr.c index 1a9a7fefb4..4b9aa33928 100644 --- a/src/sdb/src/hashstr.c +++ b/src/sdb/src/hashstr.c @@ -13,13 +13,7 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include -#include +#include "os.h" #include "tsdb.h" #define MAX_STR_LEN 40 diff --git a/src/sdb/src/sdbEngine.c b/src/sdb/src/sdbEngine.c index 59e3d7e039..326e9a4df2 100644 --- a/src/sdb/src/sdbEngine.c +++ b/src/sdb/src/sdbEngine.c @@ -13,19 +13,7 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "os.h" #include "sdb.h" #include "sdbint.h" diff --git a/src/system/detail/CMakeLists.txt b/src/system/detail/CMakeLists.txt index 9233d6c71b..95cce3dfe6 100644 --- a/src/system/detail/CMakeLists.txt +++ b/src/system/detail/CMakeLists.txt @@ -1,7 +1,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8) PROJECT(TDengine) -IF (TD_LINUX_64) +IF ((TD_LINUX_64) OR (TD_LINUX_32 AND TD_ARM)) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/inc) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/client/inc) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/modules/http/inc) diff --git a/src/system/detail/inc/mgmt.h b/src/system/detail/inc/mgmt.h index 1d1f325ec2..702eb00875 100644 --- a/src/system/detail/inc/mgmt.h +++ b/src/system/detail/inc/mgmt.h @@ -20,11 +20,7 @@ extern "C" { #endif -#include -#include -#include -#include -#include +#include "os.h" #include "sdb.h" #include "tglobalcfg.h" @@ -34,7 +30,6 @@ extern "C" { #include "tmempool.h" #include "trpc.h" #include "tsdb.h" -#include "tsdb.h" #include "tskiplist.h" #include "tsocket.h" #include "ttime.h" diff --git a/src/system/detail/inc/mgmtBalance.h b/src/system/detail/inc/mgmtBalance.h index 4157458a7f..67bfd55db2 100644 --- a/src/system/detail/inc/mgmtBalance.h +++ b/src/system/detail/inc/mgmtBalance.h @@ -20,9 +20,7 @@ extern "C" { #endif -#include -#include -#include +#include "os.h" #include "dnodeSystem.h" #include "mgmt.h" @@ -30,15 +28,6 @@ extern "C" { #include "tstatus.h" #include "ttime.h" -enum { - LB_DNODE_STATE_BALANCED, - LB_DNODE_STATE_BALANCING, - LB_DNODE_STATE_OFFLINE_REMOVING, - LB_DNODE_STATE_SHELL_REMOVING -}; - -enum { LB_VGROUP_STATE_READY, LB_VGROUP_STATE_UPDATE }; - void mgmtCreateDnodeOrderList(); void mgmtReleaseDnodeOrderList(); diff --git a/src/system/detail/inc/mgmtSystem.h b/src/system/detail/inc/mgmtSystem.h index 1262d7e834..44bb0331e1 100644 --- a/src/system/detail/inc/mgmtSystem.h +++ b/src/system/detail/inc/mgmtSystem.h @@ -20,8 +20,6 @@ extern "C" { #endif -#include - int mgmtInitRedirect(); void mgmtCleanUpRedirect(); diff --git a/src/system/detail/inc/mgmtUtil.h b/src/system/detail/inc/mgmtUtil.h index aecb229dba..6a6b819bf2 100644 --- a/src/system/detail/inc/mgmtUtil.h +++ b/src/system/detail/inc/mgmtUtil.h @@ -30,6 +30,7 @@ char* mgmtMeterGetTag(STabObj* pMeter, int32_t col, SSchema* pTagColSchema); int32_t mgmtFindTagCol(STabObj * pMetric, const char * tagName); int32_t mgmtGetTagsLength(STabObj* pMetric, int32_t col); +bool mgmtCheckIsMonitorDB(char *db, char *monitordb); int32_t mgmtRetrieveMetersFromMetric(SMetricMetaMsg* pInfo, int32_t tableIndex, tQueryResultset* pRes); int32_t mgmtDoJoin(SMetricMetaMsg* pMetricMetaMsg, tQueryResultset* pRes); diff --git a/src/system/detail/inc/vnode.h b/src/system/detail/inc/vnode.h index 4d2ebdfa35..c944c06d31 100644 --- a/src/system/detail/inc/vnode.h +++ b/src/system/detail/inc/vnode.h @@ -20,11 +20,7 @@ extern "C" { #endif -#include -#include -#include -#include -#include +#include "os.h" #include "tglobalcfg.h" #include "tidpool.h" @@ -33,7 +29,6 @@ extern "C" { #include "trpc.h" #include "tsclient.h" #include "tsdb.h" -#include "tsdb.h" #include "tsocket.h" #include "ttime.h" #include "ttimer.h" @@ -97,7 +92,7 @@ typedef struct { SVPeerDesc vpeers[TSDB_VNODES_SUPPORT]; SVnodePeer * peerInfo[TSDB_VNODES_SUPPORT]; char selfIndex; - char status; + char vnodeStatus; char accessState; // Vnode access state, Readable/Writable char syncStatus; char commitInProcess; @@ -218,15 +213,15 @@ typedef struct { * Only the QInfo.signature == QInfo, this structure can be released safely. */ #define TSDB_QINFO_QUERY_FLAG 0x1 -#define TSDB_QINFO_RESET_SIG(x) ((x)->signature = (uint64_t)(x)) +#define TSDB_QINFO_RESET_SIG(x) atomic_store_64(&((x)->signature), (uint64_t)(x)) #define TSDB_QINFO_SET_QUERY_FLAG(x) \ - __sync_val_compare_and_swap(&((x)->signature), (uint64_t)(x), TSDB_QINFO_QUERY_FLAG); + atomic_val_compare_exchange_64(&((x)->signature), (uint64_t)(x), TSDB_QINFO_QUERY_FLAG); // live lock: wait for query reaching a safe-point, release all resources // belongs to this query #define TSDB_WAIT_TO_SAFE_DROP_QINFO(x) \ { \ - while (__sync_val_compare_and_swap(&((x)->signature), (x), 0) == TSDB_QINFO_QUERY_FLAG) { \ + while (atomic_val_compare_exchange_64(&((x)->signature), (x), 0) == TSDB_QINFO_QUERY_FLAG) { \ taosMsleep(1); \ } \ } diff --git a/src/system/detail/inc/vnodePeer.h b/src/system/detail/inc/vnodePeer.h index d44143e619..4f17e66a70 100644 --- a/src/system/detail/inc/vnodePeer.h +++ b/src/system/detail/inc/vnodePeer.h @@ -16,7 +16,7 @@ #ifndef TDENGINE_VNODEPEER_H #define TDENGINE_VNODEPEER_H -#include +#include "os.h" #ifdef __cplusplus extern "C" { diff --git a/src/system/detail/inc/vnodeQueryImpl.h b/src/system/detail/inc/vnodeQueryImpl.h index c00af3b8e9..810105d638 100644 --- a/src/system/detail/inc/vnodeQueryImpl.h +++ b/src/system/detail/inc/vnodeQueryImpl.h @@ -20,8 +20,7 @@ extern "C" { #endif -#include -#include +#include "os.h" #include "ihash.h" diff --git a/src/system/detail/inc/vnodeRead.h b/src/system/detail/inc/vnodeRead.h index 20f49354f2..1de9f97e82 100644 --- a/src/system/detail/inc/vnodeRead.h +++ b/src/system/detail/inc/vnodeRead.h @@ -20,7 +20,7 @@ extern "C" { #endif -#include +#include "os.h" #include "tinterpolation.h" #include "vnodeTagMgmt.h" diff --git a/src/system/detail/inc/vnodeShell.h b/src/system/detail/inc/vnodeShell.h index d0194a1765..e450983dd7 100644 --- a/src/system/detail/inc/vnodeShell.h +++ b/src/system/detail/inc/vnodeShell.h @@ -20,7 +20,7 @@ extern "C" { #endif -#include +#include "os.h" typedef struct { int sid; diff --git a/src/system/detail/inc/vnodeStore.h b/src/system/detail/inc/vnodeStore.h index d4eedd4ce0..638bcb54bb 100644 --- a/src/system/detail/inc/vnodeStore.h +++ b/src/system/detail/inc/vnodeStore.h @@ -16,8 +16,6 @@ #ifndef TDENGINE_VNODESTORE_H #define TDENGINE_VNODESTORE_H -#include - #ifdef __cplusplus extern "C" { #endif diff --git a/src/system/detail/inc/vnodeSystem.h b/src/system/detail/inc/vnodeSystem.h index e69b0b9f1e..e436288fc1 100644 --- a/src/system/detail/inc/vnodeSystem.h +++ b/src/system/detail/inc/vnodeSystem.h @@ -16,8 +16,6 @@ #ifndef TDENGINE_VNODESYSTEM_H #define TDENGINE_VNODESYSTEM_H -#include - #ifdef __cplusplus extern "C" { #endif diff --git a/src/system/detail/src/dnodeMgmt.c b/src/system/detail/src/dnodeMgmt.c index 3a6c9e50a8..630e50b44d 100644 --- a/src/system/detail/src/dnodeMgmt.c +++ b/src/system/detail/src/dnodeMgmt.c @@ -14,9 +14,8 @@ */ #define _DEFAULT_SOURCE -#include -#include -#include + +#include "os.h" #include "dnodeSystem.h" #include "taosmsg.h" @@ -27,6 +26,7 @@ #include "vnodeMgmt.h" #include "vnodeSystem.h" #include "vnodeUtil.h" +#include "tstatus.h" SMgmtObj mgmtObj; extern uint64_t tsCreatedTime; @@ -105,14 +105,14 @@ int vnodeProcessCreateMeterRequest(char *pMsg, int msgLen, SMgmtObj *pObj) { if (vid >= TSDB_MAX_VNODES || vid < 0) { dError("vid:%d, vnode is out of range", vid); - code = TSDB_CODE_INVALID_SESSION_ID; + code = TSDB_CODE_INVALID_VNODE_ID; goto _over; } pVnode = vnodeList + vid; if (pVnode->cfg.maxSessions <= 0) { dError("vid:%d, not activated", vid); - code = TSDB_CODE_NOT_ACTIVE_SESSION; + code = TSDB_CODE_NOT_ACTIVE_VNODE; goto _over; } @@ -141,27 +141,27 @@ int vnodeProcessAlterStreamRequest(char *pMsg, int msgLen, SMgmtObj *pObj) { if (vid >= TSDB_MAX_VNODES || vid < 0) { dError("vid:%d, vnode is out of range", vid); - code = TSDB_CODE_INVALID_SESSION_ID; + code = TSDB_CODE_INVALID_VNODE_ID; goto _over; } pVnode = vnodeList + vid; if (pVnode->cfg.maxSessions <= 0 || pVnode->pCachePool == NULL) { dError("vid:%d is not activated yet", pAlter->vnode); - code = TSDB_CODE_INVALID_SESSION_ID; + code = TSDB_CODE_NOT_ACTIVE_VNODE; goto _over; } if (pAlter->sid >= pVnode->cfg.maxSessions || pAlter->sid < 0) { dError("vid:%d sid:%d uid:%ld, sid is out of range", pAlter->vnode, pAlter->sid, pAlter->uid); - code = TSDB_CODE_INVALID_SESSION_ID; + code = TSDB_CODE_INVALID_TABLE_ID; goto _over; } SMeterObj *pMeterObj = vnodeList[vid].meterList[sid]; if (pMeterObj == NULL || sid != pMeterObj->sid || vid != pMeterObj->vnode) { - dError("vid:%d sid:%d, no active session", vid, sid); - code = TSDB_CODE_NOT_ACTIVE_SESSION; + dError("vid:%d sid:%d, no active table", vid, sid); + code = TSDB_CODE_NOT_ACTIVE_TABLE; goto _over; } @@ -195,7 +195,7 @@ int vnodeProcessCreateMeterMsg(char *pMsg, int msgLen) { if (pCreate->vnode >= TSDB_MAX_VNODES || pCreate->vnode < 0) { dError("vid:%d is out of range", pCreate->vnode); - code = TSDB_CODE_INVALID_SESSION_ID; + code = TSDB_CODE_INVALID_VNODE_ID; goto _create_over; } @@ -203,13 +203,13 @@ int vnodeProcessCreateMeterMsg(char *pMsg, int msgLen) { if (pVnode->pCachePool == NULL) { dError("vid:%d is not activated yet", pCreate->vnode); vnodeSendVpeerCfgMsg(pCreate->vnode); - code = TSDB_CODE_NOT_ACTIVE_SESSION; + code = TSDB_CODE_NOT_ACTIVE_VNODE; goto _create_over; } if (pCreate->sid >= pVnode->cfg.maxSessions || pCreate->sid < 0) { dError("vid:%d sid:%d id:%s, sid is out of range", pCreate->vnode, pCreate->sid, pCreate->meterId); - code = TSDB_CODE_INVALID_SESSION_ID; + code = TSDB_CODE_INVALID_TABLE_ID; goto _create_over; } @@ -331,7 +331,7 @@ int vnodeProcessVPeerCfg(char *msg, int msgLen, SMgmtObj *pMgmtObj) { return -1; } - if (vnodeList[vnode].status == TSDB_STATUS_CREATING) { + if (vnodeList[vnode].vnodeStatus == TSDB_VNODE_STATUS_CREATING) { dTrace("vid:%d, vnode is still under creating", vnode); return 0; } @@ -360,13 +360,27 @@ int vnodeProcessVPeerCfg(char *msg, int msgLen, SMgmtObj *pMgmtObj) { } if (vnodeList[vnode].cfg.maxSessions == 0) { + dTrace("vid:%d, vnode is empty", vnode); if (pCfg->maxSessions > 0) { - return vnodeCreateVnode(vnode, pCfg, pMsg->vpeerDesc); + if (vnodeList[vnode].vnodeStatus == TSDB_VNODE_STATUS_OFFLINE) { + dTrace("vid:%d, status:%s, start to create vnode", vnode, taosGetVnodeStatusStr(vnodeList[vnode].vnodeStatus)); + return vnodeCreateVnode(vnode, pCfg, pMsg->vpeerDesc); + } else { + dTrace("vid:%d, status:%s, cannot preform create vnode operation", vnode, taosGetVnodeStatusStr(vnodeList[vnode].vnodeStatus)); + return TSDB_CODE_INVALID_VNODE_STATUS; + } } } else { + dTrace("vid:%d, vnode is not empty", vnode); if (pCfg->maxSessions > 0) { + if (vnodeList[vnode].vnodeStatus == TSDB_VNODE_STATUS_DELETING) { + dTrace("vid:%d, status:%s, wait vnode delete finished", vnode, taosGetVnodeStatusStr(vnodeList[vnode].vnodeStatus)); + } else { + dTrace("vid:%d, status:%s, start to update vnode", vnode, taosGetVnodeStatusStr(vnodeList[vnode].vnodeStatus)); + } + /* if (pCfg->maxSessions != vnodeList[vnode].cfg.maxSessions) { - vnodeCleanUpOneVnode(vnode); + vnodeCleanUpOneVnode(vnode); } vnodeConfigVPeers(vnode, pCfg->replications, pMsg->vpeerDesc); @@ -377,7 +391,10 @@ int vnodeProcessVPeerCfg(char *msg, int msgLen, SMgmtObj *pMgmtObj) { vnodeList[vnode].cfg.maxSessions = pCfg->maxSessions; vnodeOpenVnode(vnode); } + */ + return 0; } else { + dTrace("vid:%d, status:%s, start to delete vnode", vnode, taosGetVnodeStatusStr(vnodeList[vnode].vnodeStatus)); vnodeRemoveVnode(vnode); } } @@ -435,11 +452,11 @@ int vnodeProcessFreeVnodeRequest(char *pMsg, int msgLen, SMgmtObj *pMgmtObj) { pFree->vnode = htons(pFree->vnode); if (pFree->vnode < 0 || pFree->vnode >= TSDB_MAX_VNODES) { - dWarn("vid:%d out of range", pFree->vnode); + dWarn("vid:%d, out of range", pFree->vnode); return -1; } - dTrace("vid:%d receive free vnode message", pFree->vnode); + dTrace("vid:%d, receive free vnode message", pFree->vnode); int32_t code = vnodeRemoveVnode(pFree->vnode); assert(code == TSDB_CODE_SUCCESS || code == TSDB_CODE_ACTION_IN_PROGRESS); diff --git a/src/system/detail/src/dnodeService.c b/src/system/detail/src/dnodeService.c index 86d3b4a795..a14ec1fda6 100644 --- a/src/system/detail/src/dnodeService.c +++ b/src/system/detail/src/dnodeService.c @@ -14,20 +14,8 @@ */ #define _DEFAULT_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include + +#include "os.h" #include "dnodeSystem.h" #include "tglobalcfg.h" diff --git a/src/system/detail/src/dnodeSystem.c b/src/system/detail/src/dnodeSystem.c index a0fdf95fc8..8815a8f42d 100644 --- a/src/system/detail/src/dnodeSystem.c +++ b/src/system/detail/src/dnodeSystem.c @@ -14,11 +14,7 @@ */ #define _DEFAULT_SOURCE -#include -#include -#include -#include -#include +#include "os.h" #include "mgmt.h" #include "vnode.h" @@ -33,8 +29,8 @@ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Woverflow" -SModule tsModule[TSDB_MOD_MAX]; -uint32_t tsModuleStatus; +SModule tsModule[TSDB_MOD_MAX] = {0}; +uint32_t tsModuleStatus = 0; pthread_mutex_t dmutex; extern int vnodeSelectReqNum; extern int vnodeInsertReqNum; @@ -143,6 +139,8 @@ int dnodeInitSystem() { tsPrintGlobalConfig(); dPrint("Server IP address is:%s", tsInternalIp); + taosSetCoreDump(); + signal(SIGPIPE, SIG_IGN); dnodeInitModules(); @@ -216,8 +214,8 @@ void dnodeResetSystem() { void dnodeCountRequest(SCountInfo *info) { httpGetReqCount(&info->httpReqNum); - info->selectReqNum = __sync_fetch_and_and(&vnodeSelectReqNum, 0); - info->insertReqNum = __sync_fetch_and_and(&vnodeInsertReqNum, 0); + info->selectReqNum = atomic_exchange_32(&vnodeSelectReqNum, 0); + info->insertReqNum = atomic_exchange_32(&vnodeInsertReqNum, 0); } #pragma GCC diagnostic pop \ No newline at end of file diff --git a/src/system/detail/src/mgmtAcct.c b/src/system/detail/src/mgmtAcct.c index dac67518e9..e5f52b17f9 100644 --- a/src/system/detail/src/mgmtAcct.c +++ b/src/system/detail/src/mgmtAcct.c @@ -14,7 +14,7 @@ */ #define _DEFAULT_SOURCE -#include +#include "os.h" #include "mgmt.h" #include "tschemautil.h" diff --git a/src/system/detail/src/mgmtConn.c b/src/system/detail/src/mgmtConn.c index b3fb24de6b..40385d2fc4 100644 --- a/src/system/detail/src/mgmtConn.c +++ b/src/system/detail/src/mgmtConn.c @@ -14,8 +14,10 @@ */ #define _DEFAULT_SOURCE + +#include "os.h" + #include "mgmt.h" -#include #include "taosmsg.h" #include "tschemautil.h" diff --git a/src/system/detail/src/mgmtDb.c b/src/system/detail/src/mgmtDb.c index ae4c5bed7c..16a028d2de 100644 --- a/src/system/detail/src/mgmtDb.c +++ b/src/system/detail/src/mgmtDb.c @@ -14,9 +14,11 @@ */ #define _DEFAULT_SOURCE +#include "os.h" + #include "mgmt.h" -#include #include "mgmtBalance.h" +#include "mgmtUtil.h" #include "tschemautil.h" void *dbSdb = NULL; @@ -139,11 +141,10 @@ int mgmtCheckDbParams(SCreateDbMsg *pCreate) { if (pCreate->cacheNumOfBlocks.fraction < 0) pCreate->cacheNumOfBlocks.fraction = tsAverageCacheBlocks; // //-1 for balance -#ifdef CLUSTER - if (pCreate->replications > TSDB_VNODES_SUPPORT - 1) pCreate->replications = TSDB_VNODES_SUPPORT - 1; -#else - pCreate->replications = 1; -#endif + if (pCreate->replications <= 0 || pCreate->replications > TSDB_REPLICA_MAX_NUM) { + mTrace("invalid db option replications: %d", pCreate->replications); + return TSDB_CODE_INVALID_OPTION; + } if (pCreate->commitLog < 0 || pCreate->commitLog > 1) { mTrace("invalid db option commitLog: %d", pCreate->commitLog); @@ -314,7 +315,7 @@ bool mgmtCheckDropDbFinished(SDbObj *pDb) { SDnodeObj *pDnode = mgmtGetDnode(pVnodeGid->ip); if (pDnode == NULL) continue; - if (pDnode->status == TSDB_STATUS_OFFLINE) continue; + if (pDnode->status == TSDB_DNODE_STATUS_OFFLINE) continue; SVnodeLoad *pVload = &pDnode->vload[pVnodeGid->vnode]; if (pVload->dropStatus == TSDB_VN_STATUS_DROPPING) { @@ -373,10 +374,12 @@ int mgmtDropDbByName(SAcctObj *pAcct, char *name) { if (pDb == NULL) { mWarn("db:%s is not there", name); // return TSDB_CODE_INVALID_DB; - return 0; + return TSDB_CODE_SUCCESS; } - if (taosCheckDbName(pDb->name, tsMonitorDbName)) return TSDB_CODE_MONITOR_DB_FORBEIDDEN; + if (mgmtCheckIsMonitorDB(pDb->name, tsMonitorDbName)) { + return TSDB_CODE_MONITOR_DB_FORBEIDDEN; + } return mgmtDropDb(pDb); } diff --git a/src/system/detail/src/mgmtDnode.c b/src/system/detail/src/mgmtDnode.c index 36e3a41595..d8cfbbe917 100644 --- a/src/system/detail/src/mgmtDnode.c +++ b/src/system/detail/src/mgmtDnode.c @@ -15,14 +15,13 @@ #define _DEFAULT_SOURCE -#include -#include -#include +#include "os.h" #include "dnodeSystem.h" #include "mgmt.h" #include "tschemautil.h" #include "tstatus.h" +#include "tstatus.h" bool mgmtCheckModuleInDnode(SDnodeObj *pDnode, int moduleType); int mgmtGetDnodesNum(); @@ -45,9 +44,9 @@ void mgmtSetDnodeMaxVnodes(SDnodeObj *pDnode) { pDnode->openVnodes = 0; #ifdef CLUSTER - pDnode->status = TSDB_STATUS_OFFLINE; + pDnode->status = TSDB_DNODE_STATUS_OFFLINE; #else - pDnode->status = TSDB_STATUS_READY; + pDnode->status = TSDB_DNODE_STATUS_READY; #endif } @@ -59,9 +58,9 @@ void mgmtCalcNumOfFreeVnodes(SDnodeObj *pDnode) { if (pVload->vgId != 0) { mTrace("dnode:%s, calc free vnodes, exist vnode:%d, vgroup:%d, state:%d %s, dropstate:%d %s, syncstatus:%d %s", taosIpStr(pDnode->privateIp), i, pVload->vgId, - pVload->status, sdbDnodeStatusStr[pVload->status], - pVload->dropStatus, sdbVnodeDropStateStr[pVload->dropStatus], - pVload->syncStatus, sdbVnodeSyncStatusStr[pVload->syncStatus]); + pVload->status, taosGetDnodeStatusStr(pVload->status), + pVload->dropStatus, taosGetVnodeDropStatusStr(pVload->dropStatus), + pVload->syncStatus, taosGetVnodeSyncStatusStr(pVload->syncStatus)); totalVnodes++; } } @@ -198,11 +197,11 @@ int mgmtRetrieveDnodes(SShowObj *pShow, char *data, int rows, SConnObj *pConn) { cols++; pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - strcpy(pWrite, sdbDnodeStatusStr[pDnode->status]); + strcpy(pWrite, taosGetDnodeStatusStr(pDnode->status) ); cols++; pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - strcpy(pWrite, sdbDnodeBalanceStateStr[pDnode->lbState]); + strcpy(pWrite, taosGetDnodeBalanceStateStr(pDnode->lbState)); cols++; tinet_ntoa(ipstr, pDnode->publicIp); @@ -294,7 +293,7 @@ int mgmtRetrieveModules(SShowObj *pShow, char *data, int rows, SConnObj *pConn) cols++; pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - strcpy(pWrite, sdbDnodeStatusStr[pDnode->status]); + strcpy(pWrite, taosGetDnodeStatusStr(pDnode->status) ); cols++; numOfRows++; diff --git a/src/system/detail/src/mgmtDnodeInt.c b/src/system/detail/src/mgmtDnodeInt.c index 2b7fe3cf44..9310182a14 100644 --- a/src/system/detail/src/mgmtDnodeInt.c +++ b/src/system/detail/src/mgmtDnodeInt.c @@ -14,8 +14,7 @@ */ #define _DEFAULT_SOURCE -#include -#include +#include "os.h" #include "dnodeSystem.h" #include "mgmt.h" @@ -103,7 +102,7 @@ int mgmtProcessVpeerCfgMsg(char *cont, int contLen, SDnodeObj *pObj) { mTrace("dnode:%s, vnode:%d, vgroup:%d, send create meter msg, code:%d", taosIpStr(pObj->privateIp), vnode, pVgroup->vgId, *pMsg); } else { mTrace("dnode:%s, vnode:%d, no vgroup info, vgroup:%d", taosIpStr(pObj->privateIp), vnode, pObj->vload[vnode].vgId); - *pMsg = TSDB_CODE_INVALID_VALUE; + *pMsg = TSDB_CODE_NOT_ACTIVE_VNODE; pMsg++; *(int32_t *)pMsg = htonl(vnode); pMsg += sizeof(int32_t); @@ -129,7 +128,7 @@ int mgmtProcessVPeersRsp(char *msg, int msgLen, SDnodeObj *pObj) { } if (pDb->vgStatus != TSDB_VG_STATUS_IN_PROGRESS) { - mTrace("dnode:%s, db:%s vpeer rsp already disposed, code:%d", taosIpStr(pObj->privateIp), pRsp->more, pRsp->code); + mTrace("dnode:%s, db:%s vpeer rsp already disposed, vgroup status:%d code:%d", taosIpStr(pObj->privateIp), pRsp->more, pDb->vgStatus, pRsp->code); return 0; } @@ -141,10 +140,11 @@ int mgmtProcessVPeersRsp(char *msg, int msgLen, SDnodeObj *pObj) { if (pRsp->code == TSDB_CODE_VG_COMMITLOG_INIT_FAILED) { pDb->vgStatus = TSDB_VG_STATUS_COMMITLOG_INIT_FAILED; + mError("dnode:%s, db:%s vgroup commit log init failed, code:%d", taosIpStr(pObj->privateIp), pRsp->more, pRsp->code); } else { pDb->vgStatus = TSDB_VG_STATUS_INIT_FAILED; + mError("dnode:%s, db:%s vgroup init failed, code:%d", taosIpStr(pObj->privateIp), pRsp->more, pRsp->code); } - mError("dnode:%s, db:%s vgroup create failed, code:%d", taosIpStr(pObj->privateIp), pRsp->more, pRsp->code); return 0; } @@ -332,7 +332,6 @@ char *mgmtBuildVpeersIe(char *pMsg, SVgObj *pVgroup, int vnode) { pCfg->replications = (char)pVgroup->numOfVnodes; pCfg->rowsInFileBlock = htonl(pCfg->rowsInFileBlock); -#ifdef CLUSTER SVPeerDesc *vpeerDesc = pVPeers->vpeerDesc; pMsg = (char *)(pVPeers->vpeerDesc); @@ -342,7 +341,6 @@ char *mgmtBuildVpeersIe(char *pMsg, SVgObj *pVgroup, int vnode) { vpeerDesc[j].vnode = htonl(pVgroup->vnodeGid[j].vnode); pMsg += sizeof(SVPeerDesc); } -#endif return pMsg; } diff --git a/src/system/detail/src/mgmtMeter.c b/src/system/detail/src/mgmtMeter.c index bccf9a06c8..80ea5142e9 100644 --- a/src/system/detail/src/mgmtMeter.c +++ b/src/system/detail/src/mgmtMeter.c @@ -14,10 +14,7 @@ */ #define _DEFAULT_SOURCE -#include -#include -#include -#include +#include "os.h" #include "mgmt.h" #include "mgmtUtil.h" @@ -505,7 +502,7 @@ int mgmtCreateMeter(SDbObj *pDb, SCreateTableMsg *pCreate) { int numOfTables = sdbGetNumOfRows(meterSdb); if (numOfTables >= tsMaxTables) { - mWarn("numOfTables:%d, exceed tsMaxTables:%d", numOfTables, tsMaxTables); + mError("table:%s, numOfTables:%d exceed maxTables:%d", pCreate->meterId, numOfTables, tsMaxTables); return TSDB_CODE_TOO_MANY_TABLES; } @@ -513,6 +510,7 @@ int mgmtCreateMeter(SDbObj *pDb, SCreateTableMsg *pCreate) { assert(pAcct != NULL); int code = mgmtCheckMeterLimit(pAcct, pCreate); if (code != 0) { + mError("table:%s, exceed the limit", pCreate->meterId); return code; } @@ -520,8 +518,10 @@ int mgmtCreateMeter(SDbObj *pDb, SCreateTableMsg *pCreate) { pMeter = mgmtGetMeter(pCreate->meterId); if (pMeter) { if (pCreate->igExists) { + mError("table:%s, igExists is true", pCreate->meterId); return TSDB_CODE_SUCCESS; } else { + mError("table:%s, table is already exist", pCreate->meterId); return TSDB_CODE_TABLE_ALREADY_EXIST; } } @@ -536,6 +536,7 @@ int mgmtCreateMeter(SDbObj *pDb, SCreateTableMsg *pCreate) { char *pTagData = (char *)pCreate->schema; // it is a tag key pMetric = mgmtGetMeter(pTagData); if (pMetric == NULL) { + mError("table:%s, corresponding super table does not exist", pCreate->meterId); return TSDB_CODE_INVALID_TABLE; } @@ -548,6 +549,7 @@ int mgmtCreateMeter(SDbObj *pDb, SCreateTableMsg *pCreate) { pMeter->schema = (char *)malloc(size); if (pMeter->schema == NULL) { mgmtDestroyMeter(pMeter); + mError("table:%s, corresponding super table schema is null", pCreate->meterId); return TSDB_CODE_INVALID_TABLE; } memset(pMeter->schema, 0, size); @@ -559,13 +561,13 @@ int mgmtCreateMeter(SDbObj *pDb, SCreateTableMsg *pCreate) { pMeter->pTagData = pMeter->schema; pMeter->nextColId = pMetric->nextColId; memcpy(pMeter->pTagData, pTagData, size); - } else { int numOfCols = pCreate->numOfColumns + pCreate->numOfTags; size = numOfCols * sizeof(SSchema) + pCreate->sqlLen; pMeter->schema = (char *)malloc(size); if (pMeter->schema == NULL) { mgmtDestroyMeter(pMeter); + mError("table:%s, no schema input", pCreate->meterId); return TSDB_CODE_SERV_OUT_OF_MEMORY; } memset(pMeter->schema, 0, size); @@ -586,7 +588,7 @@ int mgmtCreateMeter(SDbObj *pDb, SCreateTableMsg *pCreate) { pMeter->pSql = pMeter->schema + numOfCols * sizeof(SSchema); memcpy(pMeter->pSql, (char *)(pCreate->schema) + numOfCols * sizeof(SSchema), pCreate->sqlLen); pMeter->pSql[pCreate->sqlLen - 1] = 0; - mTrace("stream sql len:%d, sql:%s", pCreate->sqlLen, pMeter->pSql); + mTrace("table:%s, stream sql len:%d sql:%s", pCreate->meterId, pCreate->sqlLen, pMeter->pSql); } else { if (pCreate->numOfTags > 0) { pMeter->meterType = TSDB_METER_METRIC; @@ -599,13 +601,14 @@ int mgmtCreateMeter(SDbObj *pDb, SCreateTableMsg *pCreate) { pMeter->createdTime = taosGetTimestampMs(); strcpy(pMeter->meterId, pCreate->meterId); if (pthread_rwlock_init(&pMeter->rwLock, NULL)) { - mError("Failed to init meter lock"); + mError("table:%s, failed to init meter lock", pCreate->meterId); mgmtDestroyMeter(pMeter); - return TSDB_CODE_OTHERS; + return TSDB_CODE_FAILED_TO_LOCK_RESOURCES; } code = mgmtCheckMeterGrant(pCreate, pMeter); if (code != 0) { + mError("table:%s, grant expired", pCreate->meterId); return code; } @@ -614,21 +617,25 @@ int mgmtCreateMeter(SDbObj *pDb, SCreateTableMsg *pCreate) { if (pDb->vgStatus == TSDB_VG_STATUS_IN_PROGRESS) { mgmtDestroyMeter(pMeter); + mTrace("table:%s, vgroup in creating progress", pCreate->meterId); return TSDB_CODE_ACTION_IN_PROGRESS; } if (pDb->vgStatus == TSDB_VG_STATUS_FULL) { mgmtDestroyMeter(pMeter); + mError("table:%s, vgroup is full", pCreate->meterId); return TSDB_CODE_NO_ENOUGH_DNODES; } if (pDb->vgStatus == TSDB_VG_STATUS_COMMITLOG_INIT_FAILED) { mgmtDestroyMeter(pMeter); + mError("table:%s, commit log init failed", pCreate->meterId); return TSDB_CODE_VG_COMMITLOG_INIT_FAILED; } if (pDb->vgStatus == TSDB_VG_STATUS_INIT_FAILED) { mgmtDestroyMeter(pMeter); + mError("table:%s, vgroup init failed", pCreate->meterId); return TSDB_CODE_VG_INIT_FAILED; } @@ -636,12 +643,13 @@ int mgmtCreateMeter(SDbObj *pDb, SCreateTableMsg *pCreate) { pDb->vgStatus = TSDB_VG_STATUS_IN_PROGRESS; mgmtCreateVgroup(pDb); mgmtDestroyMeter(pMeter); + mTrace("table:%s, vgroup malloced, wait for create progress finished", pCreate->meterId); return TSDB_CODE_ACTION_IN_PROGRESS; } int sid = taosAllocateId(pVgroup->idPool); if (sid < 0) { - mWarn("db:%s, vgroup:%d, run out of ID, num:%d", pDb->name, pVgroup->vgId, taosIdPoolNumOfUsed(pVgroup->idPool)); + mWarn("table:%s, vgroup:%d run out of ID, num:%d", pCreate->meterId, pVgroup->vgId, taosIdPoolNumOfUsed(pVgroup->idPool)); pDb->vgStatus = TSDB_VG_STATUS_IN_PROGRESS; mgmtCreateVgroup(pDb); mgmtDestroyMeter(pMeter); @@ -653,18 +661,21 @@ int mgmtCreateMeter(SDbObj *pDb, SCreateTableMsg *pCreate) { pMeter->uid = (((uint64_t)pMeter->gid.vgId) << 40) + ((((uint64_t)pMeter->gid.sid) & ((1ul << 24) - 1ul)) << 16) + ((uint64_t)sdbVersion & ((1ul << 16) - 1ul)); - mTrace("meter:%s, create meter in vgroup, vgId:%d, sid:%d, vnode:%d, uid:%d", - pMeter->meterId, pVgroup->vgId, sid, pVgroup->vnodeGid[0].vnode, pMeter->uid); + mTrace("table:%s, create table in vgroup, vgId:%d sid:%d vnode:%d uid:%d db:%s", + pMeter->meterId, pVgroup->vgId, sid, pVgroup->vnodeGid[0].vnode, pMeter->uid, pDb->name); } else { pMeter->uid = (((uint64_t)pMeter->createdTime) << 16) + ((uint64_t)sdbVersion & ((1ul << 16) - 1ul)); } - if (sdbInsertRow(meterSdb, pMeter, 0) < 0) return TSDB_CODE_SDB_ERROR; + if (sdbInsertRow(meterSdb, pMeter, 0) < 0) { + mError("table:%s, update sdb error", pCreate->meterId); + return TSDB_CODE_SDB_ERROR; + } // send create message to the selected vnode servers if (pCreate->numOfTags == 0) { - mTrace("meter:%s, send msg to dnode, vgId:%d, sid:%d, vnode:%d, dbname:%s", - pMeter->meterId, pMeter->gid.vgId, pMeter->gid.sid, pVgroup->vnodeGid[0].vnode, pDb->name); + mTrace("table:%s, send create msg to dnode, vgId:%d, sid:%d, vnode:%d", + pMeter->meterId, pMeter->gid.vgId, pMeter->gid.sid, pVgroup->vnodeGid[0].vnode); grantAddTimeSeries(pMeter->numOfColumns - 1); mgmtSendCreateMsgToVgroup(pMeter, pVgroup); @@ -688,8 +699,10 @@ int mgmtDropMeter(SDbObj *pDb, char *meterId, int ignore) { pAcct = mgmtGetAcct(pDb->cfg.acct); - // 0.sys - if (taosCheckDbName(pDb->name, tsMonitorDbName)) return TSDB_CODE_MONITOR_DB_FORBEIDDEN; + // 0.log + if (mgmtCheckIsMonitorDB(pDb->name, tsMonitorDbName)) { + return TSDB_CODE_MONITOR_DB_FORBEIDDEN; + } if (mgmtIsNormalMeter(pMeter)) { return dropMeterImp(pDb, pMeter, pAcct); @@ -719,8 +732,8 @@ int mgmtAlterMeter(SDbObj *pDb, SAlterTableMsg *pAlter) { return TSDB_CODE_INVALID_TABLE; } - // 0.sys - if (taosCheckDbName(pDb->name, tsMonitorDbName)) return TSDB_CODE_MONITOR_DB_FORBEIDDEN; + // 0.log + if (mgmtCheckIsMonitorDB(pDb->name, tsMonitorDbName)) return TSDB_CODE_MONITOR_DB_FORBEIDDEN; if (pAlter->type == TSDB_ALTER_TABLE_UPDATE_TAG_VAL) { if (!mgmtIsNormalMeter(pMeter) || !mgmtMeterCreateFromMetric(pMeter)) { @@ -833,6 +846,7 @@ static void removeMeterFromMetricIndex(STabObj *pMetric, STabObj *pMeter) { } } + tSkipListDestroyKey(&key); if (num != 0) { free(pRes); } @@ -881,7 +895,10 @@ void mgmtCleanUpMeters() { sdbCloseTable(meterSdb); } int mgmtGetMeterMeta(SMeterMeta *pMeta, SShowObj *pShow, SConnObj *pConn) { int cols = 0; - if (pConn->pDb == NULL) return TSDB_CODE_DB_NOT_SELECTED; + SDbObj *pDb = NULL; + if (pConn->pDb != NULL) pDb = mgmtGetDb(pConn->pDb->name); + + if (pDb == NULL) return TSDB_CODE_DB_NOT_SELECTED; SSchema *pSchema = tsGetSchema(pMeta); @@ -916,7 +933,7 @@ int mgmtGetMeterMeta(SMeterMeta *pMeta, SShowObj *pShow, SConnObj *pConn) { for (int i = 1; i < cols; ++i) pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1]; // pShow->numOfRows = sdbGetNumOfRows (meterSdb); - pShow->numOfRows = pConn->pDb->numOfTables; + pShow->numOfRows = pDb->numOfTables; pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1]; return 0; @@ -1208,8 +1225,12 @@ int mgmtRetrieveMeters(SShowObj *pShow, char *data, int rows, SConnObj *pConn) { int numOfRead = 0; char prefix[20] = {0}; - if (pConn->pDb == NULL) return 0; - strcpy(prefix, pConn->pDb->name); + SDbObj *pDb = NULL; + if (pConn->pDb != NULL) pDb = mgmtGetDb(pConn->pDb->name); + + if (pDb == NULL) return 0; + + strcpy(prefix, pDb->name); strcat(prefix, TS_PATH_DELIMITER); prefixLen = strlen(prefix); @@ -1269,7 +1290,10 @@ int mgmtRetrieveMeters(SShowObj *pShow, char *data, int rows, SConnObj *pConn) { int mgmtGetMetricMeta(SMeterMeta *pMeta, SShowObj *pShow, SConnObj *pConn) { int cols = 0; - if (pConn->pDb == NULL) return TSDB_CODE_DB_NOT_SELECTED; + SDbObj *pDb = NULL; + if (pConn->pDb != NULL) pDb = mgmtGetDb(pConn->pDb->name); + + if (pDb == NULL) return TSDB_CODE_DB_NOT_SELECTED; SSchema *pSchema = tsGetSchema(pMeta); @@ -1309,8 +1333,8 @@ int mgmtGetMetricMeta(SMeterMeta *pMeta, SShowObj *pShow, SConnObj *pConn) { pShow->offset[0] = 0; for (int i = 1; i < cols; ++i) pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1]; - pShow->numOfRows = pConn->pDb->numOfMetrics; - pShow->pNode = pConn->pDb->pMetric; + pShow->numOfRows = pDb->numOfMetrics; + pShow->pNode = pDb->pMetric; pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1]; return 0; diff --git a/src/system/detail/src/mgmtProfile.c b/src/system/detail/src/mgmtProfile.c index e641739e31..bd5540e5ee 100644 --- a/src/system/detail/src/mgmtProfile.c +++ b/src/system/detail/src/mgmtProfile.c @@ -14,8 +14,9 @@ */ #define _DEFAULT_SOURCE +#include "os.h" + #include "mgmt.h" -#include #include "mgmtProfile.h" #include "taosmsg.h" #include "tschemautil.h" diff --git a/src/system/detail/src/mgmtShell.c b/src/system/detail/src/mgmtShell.c index 44b9ff286c..e58938bdad 100644 --- a/src/system/detail/src/mgmtShell.c +++ b/src/system/detail/src/mgmtShell.c @@ -14,7 +14,7 @@ */ #define _DEFAULT_SOURCE -#include +#include "os.h" #include "dnodeSystem.h" #include "mgmt.h" @@ -189,8 +189,11 @@ int mgmtProcessMeterMetaMsg(char *pMsg, int msgLen, SConnObj *pConn) { int size = sizeof(STaosHeader) + sizeof(STaosRsp) + sizeof(SMeterMeta) + sizeof(SSchema) * TSDB_MAX_COLUMNS + sizeof(SSchema) * TSDB_MAX_TAGS + TSDB_MAX_TAGS_LEN + TSDB_EXTRA_PAYLOAD_SIZE; + SDbObj *pDb = NULL; + if (pConn->pDb != NULL) pDb = mgmtGetDb(pConn->pDb->name); + // todo db check should be extracted - if (pConn->pDb == NULL || (pConn->pDb != NULL && pConn->pDb->dropStatus != TSDB_DB_STATUS_READY)) { + if (pDb == NULL || (pDb != NULL && pDb->dropStatus != TSDB_DB_STATUS_READY)) { if ((pStart = mgmtAllocMsg(pConn, size, &pMsg, &pRsp)) == NULL) { taosSendSimpleRsp(pConn->thandle, TSDB_MSG_TYPE_METERINFO_RSP, TSDB_CODE_SERV_OUT_OF_MEMORY); @@ -223,10 +226,10 @@ int mgmtProcessMeterMetaMsg(char *pMsg, int msgLen, SConnObj *pConn) { SDbObj* pMeterDb = mgmtGetDbByMeterId(pCreateMsg->meterId); mTrace("meter:%s, pConnDb:%p, pConnDbName:%s, pMeterDb:%p, pMeterDbName:%s", - pCreateMsg->meterId, pConn->pDb, pConn->pDb->name, pMeterDb, pMeterDb->name); - assert(pConn->pDb == pMeterDb); + pCreateMsg->meterId, pDb, pDb->name, pMeterDb, pMeterDb->name); + assert(pDb == pMeterDb); - int32_t code = mgmtCreateMeter(pConn->pDb, pCreateMsg); + int32_t code = mgmtCreateMeter(pDb, pCreateMsg); char stableName[TSDB_METER_ID_LEN] = {0}; strncpy(stableName, pInfo->tags, TSDB_METER_ID_LEN); @@ -256,7 +259,7 @@ int mgmtProcessMeterMetaMsg(char *pMsg, int msgLen, SConnObj *pConn) { } if (pMeterObj == NULL) { - if (pConn->pDb) + if (pDb) pRsp->code = TSDB_CODE_INVALID_TABLE; else pRsp->code = TSDB_CODE_DB_NOT_SELECTED; @@ -274,7 +277,7 @@ int mgmtProcessMeterMetaMsg(char *pMsg, int msgLen, SConnObj *pConn) { pMeta->vgid = htonl(pMeterObj->gid.vgId); pMeta->sversion = htons(pMeterObj->sversion); - pMeta->precision = pConn->pDb->cfg.precision; + pMeta->precision = pDb->cfg.precision; pMeta->numOfTags = pMeterObj->numOfTags; pMeta->numOfColumns = htons(pMeterObj->numOfColumns); @@ -505,7 +508,10 @@ int mgmtProcessMetricMetaMsg(char *pMsg, int msgLen, SConnObj *pConn) { SMetricMetaElemMsg *pElem = (SMetricMetaElemMsg *)(((char *)pMetricMetaMsg) + pMetricMetaMsg->metaElem[0]); pMetric = mgmtGetMeter(pElem->meterId); - if (pMetric == NULL || (pConn->pDb != NULL && pConn->pDb->dropStatus != TSDB_DB_STATUS_READY)) { + SDbObj *pDb = NULL; + if (pConn->pDb != NULL) pDb = mgmtGetDb(pConn->pDb->name); + + if (pMetric == NULL || (pDb != NULL && pDb->dropStatus != TSDB_DB_STATUS_READY)) { pStart = taosBuildRspMsg(pConn->thandle, TSDB_MSG_TYPE_METRIC_META_RSP); if (pStart == NULL) { taosSendSimpleRsp(pConn->thandle, TSDB_MSG_TYPE_METRIC_META_RSP, TSDB_CODE_SERV_OUT_OF_MEMORY); @@ -514,7 +520,7 @@ int mgmtProcessMetricMetaMsg(char *pMsg, int msgLen, SConnObj *pConn) { pMsg = pStart; pRsp = (STaosRsp *)pMsg; - if (pConn->pDb) + if (pDb) pRsp->code = TSDB_CODE_INVALID_TABLE; else pRsp->code = TSDB_CODE_DB_NOT_SELECTED; @@ -873,7 +879,7 @@ int mgmtProcessRetrieveMsg(char *pMsg, int msgLen, SConnObj *pConn) { taosSendSimpleRsp(pConn->thandle, TSDB_MSG_TYPE_RETRIEVE_RSP, TSDB_CODE_MEMORY_CORRUPTED); return -1; } else { - if ((pRetrieve->free & TSDB_QUERY_TYPE_FREE_RESOURCE) == 0) { + if ((pRetrieve->free & TSDB_QUERY_TYPE_FREE_RESOURCE) != TSDB_QUERY_TYPE_FREE_RESOURCE) { rowsToRead = pShow->numOfRows - pShow->numOfReads; } @@ -905,7 +911,7 @@ int mgmtProcessRetrieveMsg(char *pMsg, int msgLen, SConnObj *pConn) { pMsg = pRsp->data; // if free flag is set, client wants to clean the resources - if ((pRetrieve->free & TSDB_QUERY_TYPE_FREE_RESOURCE) == 0) + if ((pRetrieve->free & TSDB_QUERY_TYPE_FREE_RESOURCE) != TSDB_QUERY_TYPE_FREE_RESOURCE) rowsRead = (*mgmtRetrieveFp[pShow->type])(pShow, pRsp->data, rowsToRead, pConn); if (rowsRead < 0) { @@ -922,8 +928,8 @@ int mgmtProcessRetrieveMsg(char *pMsg, int msgLen, SConnObj *pConn) { taosSendMsgToPeer(pConn->thandle, pStart, msgLen); if (rowsToRead == 0) { - int64_t oldSign = __sync_val_compare_and_swap(&pShow->signature, (uint64_t)pShow, 0); - if (oldSign != (uint64_t)pShow) { + uintptr_t oldSign = atomic_val_compare_exchange_ptr(&pShow->signature, pShow, 0); + if (oldSign != (uintptr_t)pShow) { return msgLen; } // pShow->signature = 0; @@ -957,17 +963,23 @@ int mgmtProcessCreateTableMsg(char *pMsg, int msgLen, SConnObj *pConn) { pSchema++; } - if (pConn->pDb) { - code = mgmtCreateMeter(pConn->pDb, pCreate); - if (code == 0) { - mTrace("meter:%s is created by %s", pCreate->meterId, pConn->pUser->user); - // mLPrint("meter:%s is created by %s", pCreate->meterId, pConn->pUser->user); - } + SDbObj *pDb = NULL; + if (pConn->pDb != NULL) pDb = mgmtGetDb(pConn->pDb->name); + + if (pDb) { + code = mgmtCreateMeter(pDb, pCreate); } else { code = TSDB_CODE_DB_NOT_SELECTED; } } + if (code != 0) { + mError("table:%s, failed to create table, code:%d", pCreate->meterId, code); + } else { + mTrace("table:%s, table is created by %s", pCreate->meterId, pConn->pUser->user); + //mLPrint("meter:%s is created by %s", pCreate->meterId, pConn->pUser->user); + } + taosSendSimpleRsp(pConn->thandle, TSDB_MSG_TYPE_CREATE_TABLE_RSP, code); return 0; @@ -984,7 +996,10 @@ int mgmtProcessDropTableMsg(char *pMsg, int msgLen, SConnObj *pConn) { if (!pConn->writeAuth) { code = TSDB_CODE_NO_RIGHTS; } else { - code = mgmtDropMeter(pConn->pDb, pDrop->meterId, pDrop->igNotExists); + SDbObj *pDb = NULL; + if (pConn->pDb != NULL) pDb = mgmtGetDb(pConn->pDb->name); + + code = mgmtDropMeter(pDb, pDrop->meterId, pDrop->igNotExists); if (code == 0) { mTrace("meter:%s is dropped by user:%s", pDrop->meterId, pConn->pUser->user); // mLPrint("meter:%s is dropped by user:%s", pDrop->meterId, pConn->pUser->user); @@ -1014,12 +1029,15 @@ int mgmtProcessAlterTableMsg(char *pMsg, int msgLen, SConnObj *pConn) { mError("meter:%s error numOfCols:%d in alter table", pAlter->meterId, pAlter->numOfCols); code = TSDB_CODE_APP_ERROR; } else { - if (pConn->pDb) { + SDbObj *pDb = NULL; + if (pConn->pDb != NULL) pDb = mgmtGetDb(pConn->pDb->name); + + if (pDb) { for (int32_t i = 0; i < pAlter->numOfCols; ++i) { pAlter->schema[i].bytes = htons(pAlter->schema[i].bytes); } - code = mgmtAlterMeter(pConn->pDb, pAlter); + code = mgmtAlterMeter(pDb, pAlter); if (code == 0) { mLPrint("meter:%s is altered by %s", pAlter->meterId, pConn->pUser->user); } @@ -1093,8 +1111,8 @@ int mgmtProcessHeartBeatMsg(char *cont, int contLen, SConnObj *pConn) { } void mgmtEstablishConn(SConnObj *pConn) { - __sync_fetch_and_add(&mgmtShellConns, 1); - __sync_fetch_and_add(&sdbExtConns, 1); + atomic_fetch_add_32(&mgmtShellConns, 1); + atomic_fetch_add_32(&sdbExtConns, 1); pConn->stime = taosGetTimestampMs(); if (strcmp(pConn->pUser->user, "root") == 0 || strcmp(pConn->pUser->user, pConn->pAcct->user) == 0) { @@ -1140,7 +1158,7 @@ int mgmtProcessConnectMsg(char *pMsg, int msgLen, SConnObj *pConn) { SAcctObj * pAcct = NULL; SUserObj * pUser = NULL; SDbObj * pDb = NULL; - char dbName[TSDB_METER_ID_LEN]; + char dbName[256] = {0}; pConnectMsg = (SConnectMsg *)pMsg; @@ -1158,7 +1176,6 @@ int mgmtProcessConnectMsg(char *pMsg, int msgLen, SConnObj *pConn) { pAcct = mgmtGetAcct(pUser->acct); if (pConnectMsg->db[0]) { - memset(dbName, 0, sizeof(dbName)); sprintf(dbName, "%x%s%s", pAcct->acctId, TS_PATH_DELIMITER, pConnectMsg->db); pDb = mgmtGetDb(dbName); if (pDb == NULL) { @@ -1169,8 +1186,8 @@ int mgmtProcessConnectMsg(char *pMsg, int msgLen, SConnObj *pConn) { if (pConn->pAcct) { mgmtRemoveConnFromAcct(pConn); - __sync_fetch_and_sub(&mgmtShellConns, 1); - __sync_fetch_and_sub(&sdbExtConns, 1); + atomic_fetch_sub_32(&mgmtShellConns, 1); + atomic_fetch_sub_32(&sdbExtConns, 1); } code = 0; @@ -1228,8 +1245,8 @@ void *mgmtProcessMsgFromShell(char *msg, void *ahandle, void *thandle) { if (msg == NULL) { if (pConn) { mgmtRemoveConnFromAcct(pConn); - __sync_fetch_and_sub(&mgmtShellConns, 1); - __sync_fetch_and_sub(&sdbExtConns, 1); + atomic_fetch_sub_32(&mgmtShellConns, 1); + atomic_fetch_sub_32(&sdbExtConns, 1); mTrace("connection from %s is closed", pConn->pUser->user); memset(pConn, 0, sizeof(SConnObj)); } @@ -1264,8 +1281,7 @@ void *mgmtProcessMsgFromShell(char *msg, void *ahandle, void *thandle) { } if (pConn->pAcct) { - if (pConn->pDb == NULL || - strncmp(pConn->pDb->name, pHead->db, tListLen(pConn->pDb->name)) != 0) { + if (pConn->pDb == NULL || strncmp(pConn->pDb->name, pHead->db, tListLen(pConn->pDb->name)) != 0) { pConn->pDb = mgmtGetDb(pHead->db); } diff --git a/src/system/detail/src/mgmtSupertableQuery.c b/src/system/detail/src/mgmtSupertableQuery.c index 31f3e29425..f5e0509c24 100644 --- a/src/system/detail/src/mgmtSupertableQuery.c +++ b/src/system/detail/src/mgmtSupertableQuery.c @@ -14,10 +14,7 @@ */ #define _DEFAULT_SOURCE -#include -#include -#include -#include +#include "os.h" #include "mgmt.h" #include "mgmtUtil.h" diff --git a/src/system/detail/src/mgmtSystem.c b/src/system/detail/src/mgmtSystem.c index bb05c35e9b..375e100a83 100644 --- a/src/system/detail/src/mgmtSystem.c +++ b/src/system/detail/src/mgmtSystem.c @@ -14,16 +14,7 @@ */ #define _DEFAULT_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "os.h" #include "dnodeSystem.h" #include "mgmt.h" diff --git a/src/system/detail/src/mgmtUser.c b/src/system/detail/src/mgmtUser.c index 83e619841b..1acceb1ade 100644 --- a/src/system/detail/src/mgmtUser.c +++ b/src/system/detail/src/mgmtUser.c @@ -14,7 +14,7 @@ */ #define _DEFAULT_SOURCE -#include +#include "os.h" #include "mgmt.h" #include "tschemautil.h" diff --git a/src/system/detail/src/mgmtUtil.c b/src/system/detail/src/mgmtUtil.c index a427771e0f..d31198be64 100644 --- a/src/system/detail/src/mgmtUtil.c +++ b/src/system/detail/src/mgmtUtil.c @@ -14,10 +14,7 @@ */ #define _DEFAULT_SOURCE -#include -#include -#include -#include +#include "os.h" #include "mgmt.h" #include "mgmtUtil.h" @@ -87,3 +84,11 @@ int32_t mgmtGetTagsLength(STabObj* pMetric, int32_t col) { // length before col return len; } + +bool mgmtCheckIsMonitorDB(char *db, char *monitordb) { + char dbName[TSDB_DB_NAME_LEN + 1] = {0}; + extractDBName(db, dbName); + + size_t len = strlen(dbName); + return (strncasecmp(dbName, monitordb, len) == 0 && len == strlen(monitordb)); +} diff --git a/src/system/detail/src/mgmtVgroup.c b/src/system/detail/src/mgmtVgroup.c index 5858be54ca..97a7343e1e 100644 --- a/src/system/detail/src/mgmtVgroup.c +++ b/src/system/detail/src/mgmtVgroup.c @@ -14,7 +14,7 @@ */ #define _DEFAULT_SOURCE -#include +#include "os.h" #include "mgmt.h" #include "tschemautil.h" @@ -123,7 +123,7 @@ void mgmtProcessVgTimer(void *handle, void *tmrId) { if (pDb == NULL) return; if (pDb->vgStatus > TSDB_VG_STATUS_IN_PROGRESS) { - mTrace("db:%s, set vgstatus from %d to %d", pDb->name, pDb->vgStatus, TSDB_VG_STATUS_READY); + mTrace("db:%s, set vgroup status from %d to ready", pDb->name, pDb->vgStatus); pDb->vgStatus = TSDB_VG_STATUS_READY; } @@ -143,7 +143,7 @@ SVgObj *mgmtCreateVgroup(SDbObj *pDb) { // based on load balance, create a new one if (mgmtAllocVnodes(pVgroup) != 0) { - mError("no enough free dnode"); + mError("db:%s, no enough free dnode to alloc %d vnodes", pDb->name, pVgroup->numOfVnodes); free(pVgroup); pDb->vgStatus = TSDB_VG_STATUS_FULL; taosTmrReset(mgmtProcessVgTimer, 5000, pDb, mgmtTmr, &pDb->vgTimer); @@ -152,9 +152,9 @@ SVgObj *mgmtCreateVgroup(SDbObj *pDb) { sdbInsertRow(vgSdb, pVgroup, 0); - mTrace("vgroup:%d, db:%s replica:%d is created", pVgroup->vgId, pDb->name, pVgroup->numOfVnodes); + mTrace("vgroup:%d, vgroup is created, db:%s replica:%d", pVgroup->vgId, pDb->name, pVgroup->numOfVnodes); for (int i = 0; i < pVgroup->numOfVnodes; ++i) - mTrace("dnode:%s, vgroup:%d, vnode:%d is created", taosIpStr(pVgroup->vnodeGid[i].ip), pVgroup->vgId, pVgroup->vnodeGid[i].vnode); + mTrace("vgroup:%d, dnode:%s vnode:%d is created", pVgroup->vgId, taosIpStr(pVgroup->vnodeGid[i].ip), pVgroup->vnodeGid[i].vnode); mgmtSendVPeersMsg(pVgroup); @@ -206,7 +206,10 @@ void mgmtCleanUpVgroups() { sdbCloseTable(vgSdb); } int mgmtGetVgroupMeta(SMeterMeta *pMeta, SShowObj *pShow, SConnObj *pConn) { int cols = 0; - if (pConn->pDb == NULL) return TSDB_CODE_DB_NOT_SELECTED; + SDbObj *pDb = NULL; + if (pConn->pDb != NULL) pDb = mgmtGetDb(pConn->pDb->name); + + if (pDb == NULL) return TSDB_CODE_DB_NOT_SELECTED; SSchema *pSchema = tsGetSchema(pMeta); @@ -229,7 +232,7 @@ int mgmtGetVgroupMeta(SMeterMeta *pMeta, SShowObj *pShow, SConnObj *pConn) { cols++; int maxReplica = 0; - SVgObj *pVgroup = pConn->pDb->pHead; + SVgObj *pVgroup = pDb->pHead; while (pVgroup != NULL) { maxReplica = pVgroup->numOfVnodes > maxReplica ? pVgroup->numOfVnodes : maxReplica; pVgroup = pVgroup->next; @@ -267,8 +270,8 @@ int mgmtGetVgroupMeta(SMeterMeta *pMeta, SShowObj *pShow, SConnObj *pConn) { pShow->offset[0] = 0; for (int i = 1; i < cols; ++i) pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1]; - pShow->numOfRows = pConn->pDb->numOfVgroups; - pShow->pNode = pConn->pDb->pHead; + pShow->numOfRows = pDb->numOfVgroups; + pShow->pNode = pDb->pHead; pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1]; return 0; @@ -282,7 +285,11 @@ int mgmtRetrieveVgroups(SShowObj *pShow, char *data, int rows, SConnObj *pConn) char ipstr[20]; int maxReplica = 0; - pVgroup = pConn->pDb->pHead; + + SDbObj *pDb = NULL; + if (pConn->pDb != NULL) pDb = mgmtGetDb(pConn->pDb->name); + + pVgroup = pDb->pHead; while (pVgroup != NULL) { maxReplica = pVgroup->numOfVnodes > maxReplica ? pVgroup->numOfVnodes : maxReplica; pVgroup = pVgroup->next; diff --git a/src/system/detail/src/vnodeCache.c b/src/system/detail/src/vnodeCache.c index ac7e19524a..6c969c1021 100644 --- a/src/system/detail/src/vnodeCache.c +++ b/src/system/detail/src/vnodeCache.c @@ -14,9 +14,7 @@ */ #define _DEFAULT_SOURCE -#include -#include -#include +#include "os.h" #include "taosmsg.h" #include "vnode.h" @@ -174,6 +172,7 @@ int vnodeFreeCacheBlock(SCacheBlock *pCacheBlock) { SCachePool *pPool = (SCachePool *)vnodeList[pObj->vnode].pCachePool; if (pCacheBlock->notFree) { pPool->notFreeSlots--; + pInfo->unCommittedBlocks--; dTrace("vid:%d sid:%d id:%s, cache block is not free, slot:%d, index:%d notFreeSlots:%d", pObj->vnode, pObj->sid, pObj->meterId, pCacheBlock->slot, pCacheBlock->index, pPool->notFreeSlots); } @@ -256,7 +255,7 @@ void vnodeUpdateCommitInfo(SMeterObj *pObj, int slot, int pos, uint64_t count) { tslot = (tslot + 1) % pInfo->maxBlocks; } - __sync_fetch_and_add(&pObj->freePoints, pObj->pointsPerBlock * slots); + atomic_fetch_add_32(&pObj->freePoints, pObj->pointsPerBlock * slots); pInfo->commitSlot = slot; pInfo->commitPoint = pos; pObj->commitCount = count; @@ -298,7 +297,7 @@ pthread_t vnodeCreateCommitThread(SVnodeObj *pVnode) { taosTmrStopA(&pVnode->commitTimer); - if (pVnode->status == TSDB_STATUS_UNSYNCED) { + if (pVnode->vnodeStatus == TSDB_VNODE_STATUS_UNSYNCED) { taosTmrReset(vnodeProcessCommitTimer, pVnode->cfg.commitTime * 1000, pVnode, vnodeTmrCtrl, &pVnode->commitTimer); dTrace("vid:%d, it is in unsyc state, commit later", pVnode->vnode); return pVnode->commitThread; @@ -517,7 +516,7 @@ int vnodeInsertPointToCache(SMeterObj *pObj, char *pData) { pData += pObj->schema[col].bytes; } - __sync_fetch_and_sub(&pObj->freePoints, 1); + atomic_fetch_sub_32(&pObj->freePoints, 1); pCacheBlock->numOfPoints++; pPool->count++; @@ -1126,7 +1125,7 @@ int vnodeSyncRestoreCache(int vnode, int fd) { for (int col = 0; col < pObj->numOfColumns; ++col) if (taosReadMsg(fd, pBlock->offset[col], pObj->schema[col].bytes * points) <= 0) return -1; - __sync_fetch_and_sub(&pObj->freePoints, points); + atomic_fetch_sub_32(&pObj->freePoints, points); blocksReceived++; pointsReceived += points; pObj->lastKey = *((TSKEY *)(pBlock->offset[0] + pObj->schema[0].bytes * (points - 1))); diff --git a/src/system/detail/src/vnodeCommit.c b/src/system/detail/src/vnodeCommit.c index af14f2be4a..73c891f7bb 100644 --- a/src/system/detail/src/vnodeCommit.c +++ b/src/system/detail/src/vnodeCommit.c @@ -14,15 +14,7 @@ */ #define _GNU_SOURCE /* See feature_test_macros(7) */ -#include - -#include -#include -#include -#include -#include -#include -#include +#include "os.h" #include "tsdb.h" #include "vnode.h" diff --git a/src/system/detail/src/vnodeFile.c b/src/system/detail/src/vnodeFile.c index f95ef01765..96b97f4059 100644 --- a/src/system/detail/src/vnodeFile.c +++ b/src/system/detail/src/vnodeFile.c @@ -14,15 +14,7 @@ */ #define _DEFAULT_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "os.h" #include "tscompression.h" #include "tutil.h" @@ -216,6 +208,7 @@ int vnodeOpenCommitFiles(SVnodeObj *pVnode, int noTempLast) { if (numOfFiles >= pVnode->numOfFiles) { // create empty header files backward filesAdded = numOfFiles - pVnode->numOfFiles + 1; + assert(filesAdded <= pVnode->maxFiles + 2); for (int i = 0; i < filesAdded; ++i) { fileId = pVnode->fileId - pVnode->numOfFiles - i; if (vnodeCreateEmptyCompFile(vnode, fileId) < 0) @@ -410,7 +403,7 @@ void vnodeRemoveFile(int vnode, int fileId) { int fd = open(headName, O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); if (fd > 0) { vnodeGetHeadFileHeaderInfo(fd, &headInfo); - __sync_fetch_and_add(&(pVnode->vnodeStatistic.totalStorage), -headInfo.totalStorage); + atomic_fetch_add_64(&(pVnode->vnodeStatistic.totalStorage), -headInfo.totalStorage); close(fd); } @@ -1297,7 +1290,7 @@ int vnodeWriteBlockToFile(SMeterObj *pObj, SCompBlock *pCompBlock, SData *data[] pCompBlock->len += wlen; } - dTrace("vid: %d vnode compStorage size is: %ld", pObj->vnode, pVnode->vnodeStatistic.compStorage); + dTrace("vid:%d, vnode compStorage size is: %ld", pObj->vnode, pVnode->vnodeStatistic.compStorage); pCompBlock->algorithm = pCfg->compression; pCompBlock->numOfPoints = points; diff --git a/src/system/detail/src/vnodeFileUtil.c b/src/system/detail/src/vnodeFileUtil.c index f8de6c4a42..b40e7cfd41 100644 --- a/src/system/detail/src/vnodeFileUtil.c +++ b/src/system/detail/src/vnodeFileUtil.c @@ -14,11 +14,7 @@ */ #define _DEFAULT_SOURCE -#include -#include -#include -#include -#include +#include "os.h" #include "vnode.h" diff --git a/src/system/detail/src/vnodeFilterFunc.c b/src/system/detail/src/vnodeFilterFunc.c index bd829460d9..08fe78c188 100644 --- a/src/system/detail/src/vnodeFilterFunc.c +++ b/src/system/detail/src/vnodeFilterFunc.c @@ -14,11 +14,7 @@ */ #define _DEFAULT_SOURCE -#include -#include -#include -#include -#include +#include "os.h" #include "taosmsg.h" #include "tsqlfunction.h" diff --git a/src/system/detail/src/vnodeMeter.c b/src/system/detail/src/vnodeMeter.c index a595e8f689..14cdf9eb78 100644 --- a/src/system/detail/src/vnodeMeter.c +++ b/src/system/detail/src/vnodeMeter.c @@ -14,10 +14,7 @@ */ #define _DEFAULT_SOURCE -#include -#include -#include -#include +#include "os.h" #include "trpc.h" #include "tschemautil.h" @@ -618,7 +615,7 @@ int vnodeInsertPoints(SMeterObj *pObj, char *cont, int contLen, char source, voi dWarn("vid:%d sid:%d id:%s, meter is dropped, abort insert, state:%d", pObj->vnode, pObj->sid, pObj->meterId, pObj->state); - code = TSDB_CODE_NOT_ACTIVE_SESSION; + code = TSDB_CODE_NOT_ACTIVE_TABLE; break; } @@ -643,8 +640,8 @@ int vnodeInsertPoints(SMeterObj *pObj, char *cont, int contLen, char source, voi pData += pObj->bytesPerPoint; points++; } - __sync_fetch_and_add(&(pVnode->vnodeStatistic.pointsWritten), points * (pObj->numOfColumns - 1)); - __sync_fetch_and_add(&(pVnode->vnodeStatistic.totalStorage), points * pObj->bytesPerPoint); + atomic_fetch_add_64(&(pVnode->vnodeStatistic.pointsWritten), points * (pObj->numOfColumns - 1)); + atomic_fetch_add_64(&(pVnode->vnodeStatistic.totalStorage), points * pObj->bytesPerPoint); pthread_mutex_lock(&(pVnode->vmutex)); diff --git a/src/system/detail/src/vnodeQueryImpl.c b/src/system/detail/src/vnodeQueryImpl.c index 187269d30b..f7f456ec53 100644 --- a/src/system/detail/src/vnodeQueryImpl.c +++ b/src/system/detail/src/vnodeQueryImpl.c @@ -1840,7 +1840,7 @@ static void setCtxTagColumnInfo(SQuery* pQuery, SQueryRuntimeEnv* pRuntimeEnv) { // ts may be the required primary timestamp column continue; } else { - assert(0); + // the column may be the normal column, group by normal_column, the functionId is TSDB_FUNC_PRJ } } @@ -3122,9 +3122,11 @@ static bool onlyOneQueryType(SQuery *pQuery, int32_t functId, int32_t functIdDst for (int32_t i = 0; i < pQuery->numOfOutputCols; ++i) { int32_t functionId = pQuery->pSelectExpr[i].pBase.functionId; - if (functionId == TSDB_FUNC_TS || functionId == TSDB_FUNC_TS_DUMMY || functionId == TSDB_FUNC_TAG) { + if (functionId == TSDB_FUNC_TS || functionId == TSDB_FUNC_TS_DUMMY || + functionId == TSDB_FUNC_TAG || functionId == TSDB_FUNC_TAG_DUMMY) { continue; } + if (functionId != functId && functionId != functIdDst) { return false; } @@ -3137,10 +3139,9 @@ static bool onlyFirstQuery(SQuery *pQuery) { return onlyOneQueryType(pQuery, TSD static bool onlyLastQuery(SQuery *pQuery) { return onlyOneQueryType(pQuery, TSDB_FUNC_LAST, TSDB_FUNC_LAST_DST); } -static void rewriteExecOrder(SQuery *pQuery, bool metricQuery) { +static void changeExecuteScanOrder(SQuery *pQuery, bool metricQuery) { // in case of point-interpolation query, use asc order scan - char msg[] = - "QInfo:%p scan order changed for %s query, old:%d, new:%d, qrange exchanged, old qrange:%lld-%lld, " + char msg[] = "QInfo:%p scan order changed for %s query, old:%d, new:%d, qrange exchanged, old qrange:%lld-%lld, " "new qrange:%lld-%lld"; // descending order query @@ -3436,9 +3437,18 @@ void pointInterpSupporterSetData(SQInfo *pQInfo, SPointInterpoSupporter *pPointI if (pQuery->interpoType == TSDB_INTERPO_SET_VALUE) { for (int32_t i = 0; i < pQuery->numOfOutputCols; ++i) { SQLFunctionCtx *pCtx = &pRuntimeEnv->pCtx[i]; + + // only the function of interp needs the corresponding information + if (pCtx->functionId != TSDB_FUNC_INTERP) { + continue; + } + + pCtx->numOfParams = 4; + SInterpInfo * pInterpInfo = (SInterpInfo *)pRuntimeEnv->pCtx[i].aOutputBuf; pInterpInfo->pInterpDetail = calloc(1, sizeof(SInterpInfoDetail)); + SInterpInfoDetail *pInterpDetail = pInterpInfo->pInterpDetail; // for primary timestamp column, set the flag @@ -3605,7 +3615,7 @@ int32_t vnodeQuerySingleMeterPrepare(SQInfo *pQInfo, SMeterObj *pMeterObj, SMete } setScanLimitationByResultBuffer(pQuery); - rewriteExecOrder(pQuery, false); + changeExecuteScanOrder(pQuery, false); pQInfo->over = 0; pQInfo->pointsRead = 0; @@ -3781,7 +3791,7 @@ int32_t vnodeMultiMeterQueryPrepare(SQInfo *pQInfo, SQuery *pQuery, void *param) pQInfo->pointsRead = 0; pQuery->pointsRead = 0; - rewriteExecOrder(pQuery, true); + changeExecuteScanOrder(pQuery, true); vnodeInitDataBlockInfo(&pSupporter->runtimeEnv.loadBlockInfo); vnodeInitLoadCompBlockInfo(&pSupporter->runtimeEnv.loadCompBlockInfo); @@ -3880,14 +3890,14 @@ void vnodeDecMeterRefcnt(SQInfo *pQInfo) { SMeterQuerySupportObj *pSupporter = pQInfo->pMeterQuerySupporter; if (pSupporter == NULL || pSupporter->numOfMeters == 1) { - __sync_fetch_and_sub(&pQInfo->pObj->numOfQueries, 1); + atomic_fetch_sub_32(&pQInfo->pObj->numOfQueries, 1); dTrace("QInfo:%p vid:%d sid:%d meterId:%s, query is over, numOfQueries:%d", pQInfo, pQInfo->pObj->vnode, pQInfo->pObj->sid, pQInfo->pObj->meterId, pQInfo->pObj->numOfQueries); } else { int32_t num = 0; for (int32_t i = 0; i < pSupporter->numOfMeters; ++i) { SMeterObj *pMeter = getMeterObj(pSupporter->pMeterObj, pSupporter->pSidSet->pSids[i]->sid); - __sync_fetch_and_sub(&(pMeter->numOfQueries), 1); + atomic_fetch_sub_32(&(pMeter->numOfQueries), 1); if (pMeter->numOfQueries > 0) { dTrace("QInfo:%p vid:%d sid:%d meterId:%s, query is over, numOfQueries:%d", pQInfo, pMeter->vnode, pMeter->sid, diff --git a/src/system/detail/src/vnodeQueryProcess.c b/src/system/detail/src/vnodeQueryProcess.c index 73d97f5e6e..7378963301 100644 --- a/src/system/detail/src/vnodeQueryProcess.c +++ b/src/system/detail/src/vnodeQueryProcess.c @@ -14,10 +14,7 @@ */ #define _DEFAULT_SOURCE -#include -#include -#include -#include +#include "os.h" #include "taosmsg.h" #include "textbuffer.h" diff --git a/src/system/detail/src/vnodeRead.c b/src/system/detail/src/vnodeRead.c index 3e580b747f..0f9565a3b5 100644 --- a/src/system/detail/src/vnodeRead.c +++ b/src/system/detail/src/vnodeRead.c @@ -14,12 +14,7 @@ */ #define _DEFAULT_SOURCE -#include -#include -#include -#include -#include -#include +#include "os.h" #include "ihash.h" #include "taosmsg.h" @@ -866,7 +861,8 @@ int vnodeSaveQueryResult(void *handle, char *data, int32_t *size) { pQInfo->pointsRead); if (pQInfo->over == 0) { - dTrace("QInfo:%p set query flag, oldSig:%p, func:%s", pQInfo, pQInfo->signature, __FUNCTION__); + //dTrace("QInfo:%p set query flag, oldSig:%p, func:%s", pQInfo, pQInfo->signature, __FUNCTION__); + dTrace("QInfo:%p set query flag, oldSig:%p", pQInfo, pQInfo->signature); uint64_t oldSignature = TSDB_QINFO_SET_QUERY_FLAG(pQInfo); /* diff --git a/src/system/detail/src/vnodeShell.c b/src/system/detail/src/vnodeShell.c index 190c1d6ee6..5982b7b1b5 100644 --- a/src/system/detail/src/vnodeShell.c +++ b/src/system/detail/src/vnodeShell.c @@ -14,10 +14,8 @@ */ #define _DEFAULT_SOURCE -#include -#include -#include -#include +#include "os.h" + #include "taosmsg.h" #include "vnode.h" #include "vnodeShell.h" @@ -29,6 +27,7 @@ #include "vnode.h" #include "vnodeRead.h" #include "vnodeUtil.h" +#include "vnodeStore.h" #pragma GCC diagnostic ignored "-Wint-conversion" extern int tsMaxQueues; @@ -91,9 +90,9 @@ void *vnodeProcessMsgFromShell(char *msg, void *ahandle, void *thandle) { // if ( vnodeList[vnode].status != TSDB_STATUS_MASTER && pMsg->msgType != TSDB_MSG_TYPE_RETRIEVE ) { #ifdef CLUSTER - if (vnodeList[vnode].status != TSDB_STATUS_MASTER) { + if (vnodeList[vnode].vnodeStatus != TSDB_VNODE_STATUS_MASTER) { taosSendSimpleRsp(thandle, pMsg->msgType + 1, TSDB_CODE_NOT_READY); - dTrace("vid:%d sid:%d, shell msg is ignored since in state:%d", vnode, sid, vnodeList[vnode].status); + dTrace("vid:%d sid:%d, shell msg is ignored since in state:%d", vnode, sid, vnodeList[vnode].vnodeStatus); } else { #endif dTrace("vid:%d sid:%d, msg:%s is received pConn:%p", vnode, sid, taosMsg[pMsg->msgType], thandle); @@ -156,6 +155,11 @@ int vnodeInitShell() { } int vnodeOpenShellVnode(int vnode) { + if (shellList[vnode] != NULL) { + dError("vid:%d, shell is already opened", vnode); + return -1; + } + const int32_t MIN_NUM_OF_SESSIONS = 300; SVnodeCfg *pCfg = &vnodeList[vnode].cfg; @@ -164,23 +168,29 @@ int vnodeOpenShellVnode(int vnode) { size_t size = sessions * sizeof(SShellObj); shellList[vnode] = (SShellObj *)calloc(1, size); if (shellList[vnode] == NULL) { - dError("vid:%d failed to allocate shellObj, size:%d", vnode, size); + dError("vid:%d, sessions:%d, failed to allocate shellObj, size:%d", vnode, pCfg->maxSessions, size); return -1; } if(taosOpenRpcChannWithQ(pShellServer, vnode, sessions, rpcQhandle[(vnode+1)%tsMaxQueues]) != TSDB_CODE_SUCCESS) { + dError("vid:%d, sessions:%d, failed to open shell", vnode, pCfg->maxSessions); return -1; } + dTrace("vid:%d, sessions:%d, shell is opened", vnode, pCfg->maxSessions); return TSDB_CODE_SUCCESS; } static void vnodeDelayedFreeResource(void *param, void *tmrId) { int32_t vnode = *(int32_t*) param; - taosCloseRpcChann(pShellServer, vnode); // close connection - tfree (shellList[vnode]); //free SShellObj + dTrace("vid:%d, start to free resources", vnode); + taosCloseRpcChann(pShellServer, vnode); // close connection + tfree(shellList[vnode]); //free SShellObj tfree(param); + + memset(vnodeList + vnode, 0, sizeof(SVnodeObj)); + vnodeCalcOpenVnodes(); } void vnodeCloseShellVnode(int vnode) { @@ -269,7 +279,7 @@ int vnodeProcessQueryRequest(char *pMsg, int msgLen, SShellObj *pObj) { if (pQueryMsg->vnode >= TSDB_MAX_VNODES || pQueryMsg->vnode < 0) { dTrace("qmsg:%p,vid:%d is out of range", pQueryMsg, pQueryMsg->vnode); - code = TSDB_CODE_INVALID_SESSION_ID; + code = TSDB_CODE_INVALID_TABLE_ID; goto _query_over; } @@ -278,7 +288,7 @@ int vnodeProcessQueryRequest(char *pMsg, int msgLen, SShellObj *pObj) { if (pVnode->cfg.maxSessions == 0) { dError("qmsg:%p,vid:%d is not activated yet", pQueryMsg, pQueryMsg->vnode); vnodeSendVpeerCfgMsg(pQueryMsg->vnode); - code = TSDB_CODE_NOT_ACTIVE_SESSION; + code = TSDB_CODE_NOT_ACTIVE_TABLE; goto _query_over; } @@ -295,7 +305,7 @@ int vnodeProcessQueryRequest(char *pMsg, int msgLen, SShellObj *pObj) { if (pVnode->meterList == NULL) { dError("qmsg:%p,vid:%d has been closed", pQueryMsg, pQueryMsg->vnode); - code = TSDB_CODE_NOT_ACTIVE_SESSION; + code = TSDB_CODE_NOT_ACTIVE_VNODE; goto _query_over; } @@ -305,7 +315,7 @@ int vnodeProcessQueryRequest(char *pMsg, int msgLen, SShellObj *pObj) { dTrace("qmsg:%p sid:%d is out of range, valid range:[%d,%d]", pQueryMsg, pSids[i]->sid, 0, pVnode->cfg.maxSessions); - code = TSDB_CODE_INVALID_SESSION_ID; + code = TSDB_CODE_INVALID_TABLE_ID; goto _query_over; } } @@ -365,7 +375,7 @@ _query_over: vnodeFreeColumnInfo(&pQueryMsg->colList[i]); } - __sync_fetch_and_add(&vnodeSelectReqNum, 1); + atomic_fetch_add_32(&vnodeSelectReqNum, 1); return ret; } @@ -385,14 +395,21 @@ void vnodeExecuteRetrieveReq(SSchedMsg *pSched) { pRetrieve = (SRetrieveMeterMsg *)pMsg; pRetrieve->free = htons(pRetrieve->free); + if ((pRetrieve->free & TSDB_QUERY_TYPE_FREE_RESOURCE) != TSDB_QUERY_TYPE_FREE_RESOURCE) { + dTrace("retrieve msg, handle:%p, free:%d", pRetrieve->qhandle, pRetrieve->free); + } else { + dTrace("retrieve msg to free resource from client, handle:%p, free:%d", pRetrieve->qhandle, pRetrieve->free); + } + /* * in case of server restart, apps may hold qhandle created by server before restart, * which is actually invalid, therefore, signature check is required. */ if (pRetrieve->qhandle == (uint64_t)pObj->qhandle) { // if free flag is set, client wants to clean the resources - if ((pRetrieve->free & TSDB_QUERY_TYPE_FREE_RESOURCE) != TSDB_QUERY_TYPE_FREE_RESOURCE) + if ((pRetrieve->free & TSDB_QUERY_TYPE_FREE_RESOURCE) != TSDB_QUERY_TYPE_FREE_RESOURCE) { code = vnodeRetrieveQueryInfo((void *)(pRetrieve->qhandle), &numOfRows, &rowSize, &timePrec); + } } else { dError("QInfo:%p, qhandle:%p is not matched with saved:%p", pObj->qhandle, pRetrieve->qhandle, pObj->qhandle); code = TSDB_CODE_INVALID_QHANDLE; @@ -419,7 +436,7 @@ void vnodeExecuteRetrieveReq(SSchedMsg *pSched) { if (code == TSDB_CODE_SUCCESS) { pRsp->offset = htobe64(vnodeGetOffsetVal(pRetrieve->qhandle)); - pRsp->useconds = ((SQInfo *)(pRetrieve->qhandle))->useconds; + pRsp->useconds = htobe64(((SQInfo *)(pRetrieve->qhandle))->useconds); } else { pRsp->offset = 0; pRsp->useconds = 0; @@ -481,7 +498,7 @@ int vnodeProcessShellSubmitRequest(char *pMsg, int msgLen, SShellObj *pObj) { if (pSubmit->vnode >= TSDB_MAX_VNODES || pSubmit->vnode < 0) { dTrace("vnode:%d is out of range", pSubmit->vnode); - code = TSDB_CODE_INVALID_SESSION_ID; + code = TSDB_CODE_INVALID_VNODE_ID; goto _submit_over; } @@ -489,7 +506,7 @@ int vnodeProcessShellSubmitRequest(char *pMsg, int msgLen, SShellObj *pObj) { if (pVnode->cfg.maxSessions == 0 || pVnode->meterList == NULL) { dError("vid:%d is not activated for submit", pSubmit->vnode); vnodeSendVpeerCfgMsg(pSubmit->vnode); - code = TSDB_CODE_NOT_ACTIVE_SESSION; + code = TSDB_CODE_NOT_ACTIVE_VNODE; goto _submit_over; } @@ -522,7 +539,7 @@ int vnodeProcessShellSubmitRequest(char *pMsg, int msgLen, SShellObj *pObj) { if (pBlocks->sid >= pVnode->cfg.maxSessions || pBlocks->sid <= 0) { dTrace("sid:%d is out of range", pBlocks->sid); - code = TSDB_CODE_INVALID_SESSION_ID; + code = TSDB_CODE_INVALID_TABLE_ID; goto _submit_over; } @@ -531,9 +548,9 @@ int vnodeProcessShellSubmitRequest(char *pMsg, int msgLen, SShellObj *pObj) { SMeterObj *pMeterObj = vnodeList[vnode].meterList[sid]; if (pMeterObj == NULL) { - dError("vid:%d sid:%d, no active session", vnode, sid); + dError("vid:%d sid:%d, no active table", vnode, sid); vnodeSendMeterCfgMsg(vnode, sid); - code = TSDB_CODE_NOT_ACTIVE_SESSION; + code = TSDB_CODE_NOT_ACTIVE_TABLE; goto _submit_over; } @@ -572,7 +589,7 @@ int vnodeProcessShellSubmitRequest(char *pMsg, int msgLen, SShellObj *pObj) { if (vnodeIsMeterState(pMeterObj, TSDB_METER_STATE_DELETING)) { dTrace("vid:%d sid:%d id:%s, it is removed, state:%d", pMeterObj->vnode, pMeterObj->sid, pMeterObj->meterId, pMeterObj->state); - code = TSDB_CODE_NOT_ACTIVE_SESSION; + code = TSDB_CODE_NOT_ACTIVE_TABLE; break; } else {// waiting for 300ms by default and try again dTrace("vid:%d sid:%d id:%s, try submit again since in state:%d", pMeterObj->vnode, pMeterObj->sid, @@ -592,6 +609,6 @@ _submit_over: // for import, send the submit response only when return code is not zero if (pSubmit->import == 0 || code != 0) ret = vnodeSendShellSubmitRspMsg(pObj, code, numOfTotalPoints); - __sync_fetch_and_add(&vnodeInsertReqNum, 1); + atomic_fetch_add_32(&vnodeInsertReqNum, 1); return ret; } diff --git a/src/system/detail/src/vnodeStore.c b/src/system/detail/src/vnodeStore.c index e00b4de7bc..572bd88cf7 100644 --- a/src/system/detail/src/vnodeStore.c +++ b/src/system/detail/src/vnodeStore.c @@ -14,11 +14,7 @@ */ #define _DEFAULT_SOURCE -#include -#include -#include -#include -#include +#include "os.h" #include "dnodeSystem.h" #include "trpc.h" @@ -26,6 +22,7 @@ #include "vnode.h" #include "vnodeStore.h" #include "vnodeUtil.h" +#include "tstatus.h" #pragma GCC diagnostic push #pragma GCC diagnostic warning "-Woverflow" @@ -34,12 +31,14 @@ int tsMaxVnode = -1; int tsOpenVnodes = 0; SVnodeObj *vnodeList = NULL; -int vnodeInitStoreVnode(int vnode) { +static int vnodeInitStoreVnode(int vnode) { SVnodeObj *pVnode = vnodeList + vnode; pVnode->vnode = vnode; vnodeOpenMetersVnode(vnode); - if (pVnode->cfg.maxSessions == 0) return 0; + if (pVnode->cfg.maxSessions <= 0) { + return TSDB_CODE_SUCCESS; + } pVnode->firstKey = taosGetTimestamp(pVnode->cfg.precision); @@ -49,9 +48,10 @@ int vnodeInitStoreVnode(int vnode) { return -1; } - if (vnodeInitFile(vnode) < 0) return -1; - - // vnodeOpenMeterMgmtStoreVnode(vnode); + if (vnodeInitFile(vnode) < 0) { + dError("vid:%d, files init failed.", pVnode->vnode); + return -1; + } if (vnodeInitCommit(vnode) < 0) { dError("vid:%d, commit init failed.", pVnode->vnode); @@ -74,10 +74,17 @@ int vnodeOpenVnode(int vnode) { pVnode->accessState = TSDB_VN_ALL_ACCCESS; // vnode is empty - if (pVnode->cfg.maxSessions == 0) return 0; + if (pVnode->cfg.maxSessions <= 0) { + return TSDB_CODE_SUCCESS; + } + if (!(pVnode->vnodeStatus == TSDB_VNODE_STATUS_OFFLINE || pVnode->vnodeStatus == TSDB_VNODE_STATUS_CREATING)) { + dError("vid:%d, status:%s, cannot enter open operation", vnode, taosGetVnodeStatusStr(pVnode->vnodeStatus)); + return TSDB_CODE_INVALID_VNODE_STATUS; + } + + dTrace("vid:%d, status:%s, start to open", vnode, taosGetVnodeStatusStr(pVnode->vnodeStatus)); pthread_mutex_lock(&dmutex); - // vnodeOpenMeterMgmtVnode(vnode); // not enough memory, abort if ((code = vnodeOpenShellVnode(vnode)) != TSDB_CODE_SUCCESS) { @@ -97,14 +104,13 @@ int vnodeOpenVnode(int vnode) { vnodeOpenStreams(pVnode, NULL); #endif - dTrace("vid:%d, vnode is opened, openVnodes:%d", vnode, tsOpenVnodes); + dTrace("vid:%d, vnode is opened, openVnodes:%d, status:%s", vnode, tsOpenVnodes, taosGetVnodeStatusStr(pVnode->vnodeStatus)); - return 0; + return TSDB_CODE_SUCCESS; } static int32_t vnodeMarkAllMetersDropped(SVnodeObj* pVnode) { if (pVnode->meterList == NULL) { - assert(pVnode->cfg.maxSessions == 0); return TSDB_CODE_SUCCESS; } @@ -123,7 +129,7 @@ static int32_t vnodeMarkAllMetersDropped(SVnodeObj* pVnode) { return ready? TSDB_CODE_SUCCESS:TSDB_CODE_ACTION_IN_PROGRESS; } -int vnodeCloseVnode(int vnode) { +static int vnodeCloseVnode(int vnode) { if (vnodeList == NULL) return TSDB_CODE_SUCCESS; SVnodeObj* pVnode = &vnodeList[vnode]; @@ -134,12 +140,23 @@ int vnodeCloseVnode(int vnode) { return TSDB_CODE_SUCCESS; } + if (pVnode->vnodeStatus == TSDB_VNODE_STATUS_DELETING) { + dTrace("vid:%d, status:%s, another thread performed delete operation", vnode, taosGetVnodeStatusStr(pVnode->vnodeStatus)); + return TSDB_CODE_SUCCESS; + } else { + dTrace("vid:%d, status:%s, enter close operation", vnode, taosGetVnodeStatusStr(pVnode->vnodeStatus)); + pVnode->vnodeStatus = TSDB_VNODE_STATUS_CLOSING; + } + // set the meter is dropped flag if (vnodeMarkAllMetersDropped(pVnode) != TSDB_CODE_SUCCESS) { pthread_mutex_unlock(&dmutex); return TSDB_CODE_ACTION_IN_PROGRESS; } + dTrace("vid:%d, status:%s, enter delete operation", vnode, taosGetVnodeStatusStr(pVnode->vnodeStatus)); + pVnode->vnodeStatus = TSDB_VNODE_STATUS_DELETING; + vnodeCloseStream(vnodeList + vnode); vnodeCancelCommit(vnodeList + vnode); vnodeClosePeerVnode(vnode); @@ -153,9 +170,6 @@ int vnodeCloseVnode(int vnode) { if (tsMaxVnode == vnode) tsMaxVnode = vnode - 1; tfree(vnodeList[vnode].meterIndex); - memset(vnodeList + vnode, 0, sizeof(SVnodeObj)); - - vnodeCalcOpenVnodes(); pthread_mutex_unlock(&dmutex); return TSDB_CODE_SUCCESS; @@ -164,7 +178,12 @@ int vnodeCloseVnode(int vnode) { int vnodeCreateVnode(int vnode, SVnodeCfg *pCfg, SVPeerDesc *pDesc) { char fileName[128]; - vnodeList[vnode].status = TSDB_STATUS_CREATING; + if (vnodeList[vnode].vnodeStatus != TSDB_VNODE_STATUS_OFFLINE) { + dError("vid:%d, status:%s, cannot enter create operation", vnode, taosGetVnodeStatusStr(vnodeList[vnode].vnodeStatus)); + return TSDB_CODE_INVALID_VNODE_STATUS; + } + + vnodeList[vnode].vnodeStatus = TSDB_VNODE_STATUS_CREATING; sprintf(fileName, "%s/vnode%d", tsDirectory, vnode); mkdir(fileName, 0755); @@ -181,14 +200,14 @@ int vnodeCreateVnode(int vnode, SVnodeCfg *pCfg, SVPeerDesc *pDesc) { return TSDB_CODE_VG_INIT_FAILED; } - if (vnodeInitStoreVnode(vnode) != 0) { + if (vnodeInitStoreVnode(vnode) < 0) { return TSDB_CODE_VG_COMMITLOG_INIT_FAILED; } return vnodeOpenVnode(vnode); } -void vnodeRemoveDataFiles(int vnode) { +static void vnodeRemoveDataFiles(int vnode) { char vnodeDir[TSDB_FILENAME_LEN]; char dfilePath[TSDB_FILENAME_LEN]; char linkFile[TSDB_FILENAME_LEN]; @@ -231,19 +250,28 @@ void vnodeRemoveDataFiles(int vnode) { sprintf(vnodeDir, "%s/vnode%d", tsDirectory, vnode); rmdir(vnodeDir); - dTrace("vnode %d is removed!", vnode); + dTrace("vid:%d, vnode is removed!", vnode); } int vnodeRemoveVnode(int vnode) { if (vnodeList == NULL) return TSDB_CODE_SUCCESS; if (vnodeList[vnode].cfg.maxSessions > 0) { - int32_t ret = vnodeCloseVnode(vnode); - if (ret != TSDB_CODE_SUCCESS) { - return ret; + SVnodeObj* pVnode = &vnodeList[vnode]; + if (pVnode->vnodeStatus == TSDB_VNODE_STATUS_CREATING + || pVnode->vnodeStatus == TSDB_VNODE_STATUS_OFFLINE + || pVnode->vnodeStatus == TSDB_VNODE_STATUS_DELETING) { + dError("vid:%d, status:%s, cannot enter close/delete operation", vnode, taosGetVnodeStatusStr(pVnode->vnodeStatus)); + return TSDB_CODE_ACTION_IN_PROGRESS; + } else { + int32_t ret = vnodeCloseVnode(vnode); + if (ret != TSDB_CODE_SUCCESS) { + return ret; + } + + vnodeRemoveDataFiles(vnode); } - vnodeRemoveDataFiles(vnode); } else { dTrace("vid:%d, max sessions:%d, this vnode already dropped!!!", vnode, vnodeList[vnode].cfg.maxSessions); vnodeList[vnode].cfg.maxSessions = 0; //reset value @@ -297,7 +325,7 @@ void vnodeCleanUpOneVnode(int vnode) { again = 1; if (vnodeList[vnode].pCachePool) { - vnodeList[vnode].status = TSDB_STATUS_OFFLINE; + vnodeList[vnode].vnodeStatus = TSDB_VNODE_STATUS_OFFLINE; vnodeClosePeerVnode(vnode); } @@ -326,7 +354,7 @@ void vnodeCleanUpVnodes() { for (int vnode = 0; vnode < TSDB_MAX_VNODES; ++vnode) { if (vnodeList[vnode].pCachePool) { - vnodeList[vnode].status = TSDB_STATUS_OFFLINE; + vnodeList[vnode].vnodeStatus = TSDB_VNODE_STATUS_OFFLINE; vnodeClosePeerVnode(vnode); } } @@ -351,7 +379,7 @@ void vnodeCalcOpenVnodes() { openVnodes++; } - __sync_val_compare_and_swap(&tsOpenVnodes, tsOpenVnodes, openVnodes); + atomic_store_32(&tsOpenVnodes, openVnodes); } void vnodeUpdateHeadFile(int vnode, int oldTables, int newTables) { diff --git a/src/system/detail/src/vnodeStream.c b/src/system/detail/src/vnodeStream.c index 0667ee77bd..562b7eb73c 100644 --- a/src/system/detail/src/vnodeStream.c +++ b/src/system/detail/src/vnodeStream.c @@ -171,7 +171,7 @@ void vnodeCloseStream(SVnodeObj *pVnode) { void vnodeUpdateStreamRole(SVnodeObj *pVnode) { /* SMeterObj *pObj; */ - int newRole = (pVnode->status == TSDB_STATUS_MASTER) ? 1 : 0; + int newRole = (pVnode->vnodeStatus == TSDB_VNODE_STATUS_MASTER) ? 1 : 0; if (newRole != pVnode->streamRole) { dTrace("vid:%d, stream role is changed to:%d", pVnode->vnode, newRole); pVnode->streamRole = newRole; diff --git a/src/system/detail/src/vnodeSystem.c b/src/system/detail/src/vnodeSystem.c index 2f350db3fa..631e258b25 100644 --- a/src/system/detail/src/vnodeSystem.c +++ b/src/system/detail/src/vnodeSystem.c @@ -14,15 +14,7 @@ */ #define _DEFAULT_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "os.h" #include "tsdb.h" #include "tsocket.h" diff --git a/src/system/detail/src/vnodeTagMgmt.c b/src/system/detail/src/vnodeTagMgmt.c index 9b76e14854..adf4e544bb 100644 --- a/src/system/detail/src/vnodeTagMgmt.c +++ b/src/system/detail/src/vnodeTagMgmt.c @@ -14,9 +14,7 @@ */ #define _DEFAULT_SOURCE -#include -#include -#include +#include "os.h" #include "tsdb.h" #include "tlog.h" diff --git a/src/system/detail/src/vnodeUtil.c b/src/system/detail/src/vnodeUtil.c index 790ad1c4a1..b8c463f50b 100644 --- a/src/system/detail/src/vnodeUtil.c +++ b/src/system/detail/src/vnodeUtil.c @@ -14,10 +14,7 @@ */ #define _DEFAULT_SOURCE -#include -#include -#include -#include +#include "os.h" #include "tast.h" #include "tscUtil.h" @@ -289,7 +286,7 @@ SSqlFunctionExpr* vnodeCreateSqlFunctionExpr(SQueryMeterMsg* pQueryMsg, int32_t* return NULL; } - if (pExprs[i].pBase.functionId == TSDB_FUNC_TAG_DUMMY) { + if (pExprs[i].pBase.functionId == TSDB_FUNC_TAG_DUMMY || pExprs[i].pBase.functionId == TSDB_FUNC_TS_DUMMY) { tagLen += pExprs[i].resBytes; } assert(isValidDataType(pExprs[i].resType, pExprs[i].resBytes)); @@ -553,7 +550,7 @@ int32_t vnodeIncQueryRefCount(SQueryMeterMsg* pQueryMsg, SMeterSidExtInfo** pSid if (pMeter == NULL || (pMeter->state > TSDB_METER_STATE_INSERT)) { if (pMeter == NULL || vnodeIsMeterState(pMeter, TSDB_METER_STATE_DELETING)) { - code = TSDB_CODE_NOT_ACTIVE_SESSION; + code = TSDB_CODE_NOT_ACTIVE_TABLE; dError("qmsg:%p, vid:%d sid:%d, not there or will be dropped", pQueryMsg, pQueryMsg->vnode, pSids[i]->sid); vnodeSendMeterCfgMsg(pQueryMsg->vnode, pSids[i]->sid); } else {//update or import @@ -567,7 +564,7 @@ int32_t vnodeIncQueryRefCount(SQueryMeterMsg* pQueryMsg, SMeterSidExtInfo** pSid * check if the numOfQueries is 0 or not. */ pMeterObjList[(*numOfInc)++] = pMeter; - __sync_fetch_and_add(&pMeter->numOfQueries, 1); + atomic_fetch_add_32(&pMeter->numOfQueries, 1); // output for meter more than one query executed if (pMeter->numOfQueries > 1) { @@ -591,7 +588,7 @@ void vnodeDecQueryRefCount(SQueryMeterMsg* pQueryMsg, SMeterObj** pMeterObjList, SMeterObj* pMeter = pMeterObjList[i]; if (pMeter != NULL) { // here, do not need to lock to perform operations - __sync_fetch_and_sub(&pMeter->numOfQueries, 1); + atomic_fetch_sub_32(&pMeter->numOfQueries, 1); if (pMeter->numOfQueries > 0) { dTrace("qmsg:%p, vid:%d sid:%d id:%s dec query ref, numOfQueries:%d", pQueryMsg, pMeter->vnode, pMeter->sid, @@ -646,7 +643,7 @@ void vnodeUpdateQueryColumnIndex(SQuery* pQuery, SMeterObj* pMeterObj) { } int32_t vnodeSetMeterState(SMeterObj* pMeterObj, int32_t state) { - return __sync_val_compare_and_swap(&pMeterObj->state, TSDB_METER_STATE_READY, state); + return atomic_val_compare_exchange_32(&pMeterObj->state, TSDB_METER_STATE_READY, state); } void vnodeClearMeterState(SMeterObj* pMeterObj, int32_t state) { diff --git a/src/system/lite/CMakeLists.txt b/src/system/lite/CMakeLists.txt index 965f7666b0..a22ed60563 100644 --- a/src/system/lite/CMakeLists.txt +++ b/src/system/lite/CMakeLists.txt @@ -1,7 +1,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8) PROJECT(TDengine) -IF (TD_LINUX_64) +IF ((TD_LINUX_64) OR (TD_LINUX_32 AND TD_ARM)) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/inc) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/client/inc) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/modules/http/inc) diff --git a/src/system/lite/src/dnodeMgmt.spec.c b/src/system/lite/src/dnodeMgmt.spec.c index 00e7e469c0..6c424a85af 100644 --- a/src/system/lite/src/dnodeMgmt.spec.c +++ b/src/system/lite/src/dnodeMgmt.spec.c @@ -50,7 +50,7 @@ char *taosBuildReqMsgToMnode(SMgmtObj *pObj, char type) { } int taosSendMsgToMnode(SMgmtObj *pObj, char *msg, int msgLen) { - mTrace("msg:%s is sent to mnode", taosMsg[*(msg-1)]); + dTrace("msg:%s is sent to mnode", taosMsg[*(msg-1)]); /* * Lite version has no message header, so minus one diff --git a/src/system/lite/src/mgmtDnode.spec.c b/src/system/lite/src/mgmtDnode.spec.c index c34ac58c00..3c7c7f06ba 100644 --- a/src/system/lite/src/mgmtDnode.spec.c +++ b/src/system/lite/src/mgmtDnode.spec.c @@ -30,7 +30,7 @@ int mgmtInitDnodes() { dnodeObj.createdTime = (int64_t)tsRebootTime * 1000; dnodeObj.lastReboot = tsRebootTime; dnodeObj.numOfCores = (uint16_t)tsNumOfCores; - dnodeObj.status = TSDB_STATUS_READY; + dnodeObj.status = TSDB_DNODE_STATUS_READY; dnodeObj.alternativeRole = TSDB_DNODE_ROLE_ANY; dnodeObj.numOfTotalVnodes = tsNumOfTotalVnodes; dnodeObj.thandle = (void*)(1); //hack way diff --git a/src/system/lite/src/mgmtDnodeInt.spec.c b/src/system/lite/src/mgmtDnodeInt.spec.c index acde36e7b8..10628d0fb0 100644 --- a/src/system/lite/src/mgmtDnodeInt.spec.c +++ b/src/system/lite/src/mgmtDnodeInt.spec.c @@ -82,7 +82,7 @@ void mgmtCleanUpDnodeInt() {} void mgmtProcessDnodeStatus(void *handle, void *tmrId) { SDnodeObj *pObj = &dnodeObj; pObj->openVnodes = tsOpenVnodes; - pObj->status = TSDB_STATUS_READY; + pObj->status = TSDB_DNODE_STATUS_READY; float memoryUsedMB = 0; taosGetSysMemory(&memoryUsedMB); @@ -97,7 +97,7 @@ void mgmtProcessDnodeStatus(void *handle, void *tmrId) { if (vnodeList[vnode].cfg.maxSessions <= 0) { pVload->dropStatus = TSDB_VN_STATUS_READY; pVload->status = TSDB_VN_STATUS_READY; - mPrint("vid:%d, drop finished", pObj->privateIp, vnode); + mPrint("dnode:%s, vid:%d, drop finished", taosIpStr(pObj->privateIp), vnode); taosTmrStart(mgmtMonitorDbDrop, 10000, NULL, mgmtTmr); } } diff --git a/src/system/lite/src/vnodePeer.spec.c b/src/system/lite/src/vnodePeer.spec.c index 1ceb8465c3..d33e4ead6e 100644 --- a/src/system/lite/src/vnodePeer.spec.c +++ b/src/system/lite/src/vnodePeer.spec.c @@ -15,6 +15,7 @@ #define _DEFAULT_SOURCE #include "vnode.h" +#include "tstatus.h" int vnodeInitPeer(int numOfThreads) { return 0; } @@ -30,8 +31,8 @@ void vnodeBroadcastStatusToUnsyncedPeer(SVnodeObj *pVnode) {} int vnodeOpenPeerVnode(int vnode) { SVnodeObj *pVnode = vnodeList + vnode; - pVnode->status = (pVnode->cfg.replications > 1) ? TSDB_STATUS_UNSYNCED : TSDB_STATUS_MASTER; - dTrace("vid:%d, vnode status:%d numOfPeers:%d", vnode, pVnode->status, pVnode->cfg.replications-1); + pVnode->vnodeStatus = (pVnode->cfg.replications > 1) ? TSDB_VNODE_STATUS_UNSYNCED : TSDB_VNODE_STATUS_MASTER; + dTrace("vid:%d, status:%s numOfPeers:%d", vnode, taosGetVnodeStatusStr(pVnode->vnodeStatus), pVnode->cfg.replications - 1); vnodeUpdateStreamRole(pVnode); return 0; } diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index 7e54759f7e..e73428353e 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -4,7 +4,7 @@ PROJECT(TDengine) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/inc) INCLUDE_DIRECTORIES(${TD_OS_DIR}/inc) -IF (TD_LINUX_64) +IF ((TD_LINUX_64) OR (TD_LINUX_32 AND TD_ARM)) AUX_SOURCE_DIRECTORY(src SRC) ADD_LIBRARY(tutil ${SRC}) TARGET_LINK_LIBRARIES(tutil pthread os m rt) diff --git a/src/util/src/ihash.c b/src/util/src/ihash.c index 606bddd79b..8c492b03f8 100644 --- a/src/util/src/ihash.c +++ b/src/util/src/ihash.c @@ -13,13 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include - #include "os.h" typedef struct _str_node_t { diff --git a/src/util/src/tcache.c b/src/util/src/tcache.c index 7804cb0d0e..8a2f1347df 100644 --- a/src/util/src/tcache.c +++ b/src/util/src/tcache.c @@ -13,15 +13,7 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include - -#include -#include -#include +#include "os.h" #include "tcache.h" #include "tlog.h" @@ -516,7 +508,7 @@ static SDataNode *taosUpdateCacheImpl(SCacheObj *pObj, SDataNode *pNode, char *k pNewNode->addTime = taosGetTimestampMs(); pNewNode->time = pNewNode->addTime + keepTime; - __sync_add_and_fetch_32(&pNewNode->refCount, 1); + atomic_add_fetch_32(&pNewNode->refCount, 1); // the address of this node may be changed, so the prev and next element should update the corresponding pointer taosUpdateInHashTable(pObj, pNewNode); @@ -529,7 +521,7 @@ static SDataNode *taosUpdateCacheImpl(SCacheObj *pObj, SDataNode *pNode, char *k return NULL; } - __sync_add_and_fetch_32(&pNewNode->refCount, 1); + atomic_add_fetch_32(&pNewNode->refCount, 1); assert(hashVal == (*pObj->hashFp)(key, keyLen - 1)); pNewNode->hashVal = hashVal; @@ -558,7 +550,7 @@ static FORCE_INLINE SDataNode *taosAddToCacheImpl(SCacheObj *pObj, char *key, ui return NULL; } - __sync_add_and_fetch_32(&pNode->refCount, 1); + atomic_add_fetch_32(&pNode->refCount, 1); pNode->hashVal = (*pObj->hashFp)(key, keyLen - 1); taosAddNodeToHashTable(pObj, pNode); @@ -616,7 +608,7 @@ static FORCE_INLINE void taosDecRef(SDataNode *pNode) { } if (pNode->refCount > 0) { - __sync_sub_and_fetch_32(&pNode->refCount, 1); + atomic_sub_fetch_32(&pNode->refCount, 1); pTrace("key:%s is released by app.refcnt:%d", pNode->key, pNode->refCount); } else { /* @@ -676,20 +668,20 @@ void *taosGetDataFromCache(void *handle, char *key) { SDataNode *ptNode = taosGetNodeFromHashTable(handle, key, keyLen); if (ptNode != NULL) { - __sync_add_and_fetch_32(&ptNode->refCount, 1); + atomic_add_fetch_32(&ptNode->refCount, 1); } __cache_unlock(pObj); if (ptNode != NULL) { - __sync_add_and_fetch_32(&pObj->statistics.hitCount, 1); + atomic_add_fetch_32(&pObj->statistics.hitCount, 1); pTrace("key:%s is retrieved from cache,refcnt:%d", key, ptNode->refCount); } else { - __sync_add_and_fetch_32(&pObj->statistics.missCount, 1); + atomic_add_fetch_32(&pObj->statistics.missCount, 1); pTrace("key:%s not in cache,retrieved failed", key); } - __sync_add_and_fetch_32(&pObj->statistics.totalAccess, 1); + atomic_add_fetch_32(&pObj->statistics.totalAccess, 1); return (ptNode != NULL) ? ptNode->data : NULL; } diff --git a/src/util/src/tcompression.c b/src/util/src/tcompression.c index cd69f30c3f..1ac42377fb 100644 --- a/src/util/src/tcompression.c +++ b/src/util/src/tcompression.c @@ -46,12 +46,6 @@ * of the XORed value with informations. If not, record the first corresponding bytes. * */ -#include -#include -#include -#include -#include -#include #include "os.h" #include "lz4.h" diff --git a/src/util/src/tcrc32c.c b/src/util/src/tcrc32c.c index 88841bcbc9..705ca5872a 100644 --- a/src/util/src/tcrc32c.c +++ b/src/util/src/tcrc32c.c @@ -1353,7 +1353,7 @@ uint32_t crc32c_hw(uint32_t crc, crc_stream buf, size_t len) { #endif // #ifndef _TD_ARM_ void taosResolveCRC() { -#ifndef _TD_ARM_32 +#ifndef _TD_ARM_ int sse42; SSE42(sse42); crc32c = sse42 ? crc32c_hw : crc32c_sf; diff --git a/src/util/src/textbuffer.c b/src/util/src/textbuffer.c index c6b56919e9..8944dd4391 100644 --- a/src/util/src/textbuffer.c +++ b/src/util/src/textbuffer.c @@ -13,17 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include -#include - -#include -#include -#include - -#include - #include "os.h" #include "taos.h" #include "taosmsg.h" @@ -60,7 +49,7 @@ void getTmpfilePath(const char *fileNamePrefix, char *dstPath) { strcat(tmpPath, fileNamePrefix); strcat(tmpPath, "-%u-%u"); - snprintf(dstPath, MAX_TMPFILE_PATH_LENGTH, tmpPath, taosGetPthreadId(), __sync_add_and_fetch_32(&tmpFileSerialNum, 1)); + snprintf(dstPath, MAX_TMPFILE_PATH_LENGTH, tmpPath, taosGetPthreadId(), atomic_add_fetch_32(&tmpFileSerialNum, 1)); } /* diff --git a/src/util/src/tglobalcfg.c b/src/util/src/tglobalcfg.c index 9abe60220f..ab26eda348 100644 --- a/src/util/src/tglobalcfg.c +++ b/src/util/src/tglobalcfg.c @@ -62,8 +62,8 @@ short tsMgmtShellPort = 6030; // udp[6030-6034] tcp[6030] short tsVnodeShellPort = 6035; // udp[6035-6039] tcp[6035] short tsMgmtVnodePort = 6040; // udp[6040-6044] tcp[6040] short tsVnodeVnodePort = 6045; // tcp[6045] -short tsMgmtMgmtPort = 6050; // sdbPeerPort only udp, numOfVnodes fixed to 1, range udp[6050] -short tsMgmtSyncPort = 6050; // sdbSyncPort only tcp, range tcp[6050] +short tsMgmtMgmtPort = 6050; // udp, numOfVnodes fixed to 1, range udp[6050] +short tsMgmtSyncPort = 6050; // tcp, range tcp[6050] int tsStatusInterval = 1; // second int tsShellActivityTimer = 3; // second @@ -77,7 +77,7 @@ float tsRatioOfQueryThreads = 0.5; char tsPublicIp[TSDB_IPv4ADDR_LEN] = {0}; char tsInternalIp[TSDB_IPv4ADDR_LEN] = {0}; char tsPrivateIp[TSDB_IPv4ADDR_LEN] = {0}; -char tsServerIpStr[TSDB_IPv4ADDR_LEN] = "0.0.0.0"; +char tsServerIpStr[TSDB_IPv4ADDR_LEN] = "127.0.0.1"; short tsNumOfVnodesPerCore = 8; short tsNumOfTotalVnodes = 0; short tsCheckHeaderFile = 0; @@ -364,7 +364,7 @@ void tsReadLogOption(char *option, char *value) { } } -SGlobalConfig *tsGetConfigOption(char *option) { +SGlobalConfig *tsGetConfigOption(const char *option) { tsInitGlobalConfig(); for (int i = 0; i < tsGlobalConfigNum; ++i) { SGlobalConfig *cfg = tsGlobalConfig + i; @@ -374,7 +374,7 @@ SGlobalConfig *tsGetConfigOption(char *option) { return NULL; } -void tsReadConfigOption(char *option, char *value) { +void tsReadConfigOption(const char *option, char *value) { for (int i = 0; i < tsGlobalConfigNum; ++i) { SGlobalConfig *cfg = tsGlobalConfig + i; if (!(cfg->cfgType & TSDB_CFG_CTYPE_B_CONFIG)) continue; @@ -423,9 +423,7 @@ void tsInitConfigOption(SGlobalConfig *cfg, char *name, void *ptr, int8_t valTyp cfg->cfgStatus = TSDB_CFG_CSTATUS_NONE; } -void tsInitGlobalConfig() { - if (tsGlobalConfig != NULL) return; - +static void doInitGlobalConfig() { tsGlobalConfig = (SGlobalConfig *) malloc(sizeof(SGlobalConfig) * TSDB_CFG_MAX_NUM); memset(tsGlobalConfig, 0, sizeof(SGlobalConfig) * TSDB_CFG_MAX_NUM); @@ -536,6 +534,11 @@ void tsInitGlobalConfig() { 0, 2, 0, TSDB_CFG_UTYPE_NONE); // 0-any, 1-mgmt, 2-dnode + // timer + tsInitConfigOption(cfg++, "maxTmrCtrl", &taosMaxTmrCtrl, TSDB_CFG_VTYPE_INT, + TSDB_CFG_CTYPE_B_CONFIG | TSDB_CFG_CTYPE_B_SHOW | TSDB_CFG_CTYPE_B_CLUSTER, + 8, 2048, 0, TSDB_CFG_UTYPE_NONE); + // time tsInitConfigOption(cfg++, "monitorInterval", &tsMonitorInterval, TSDB_CFG_VTYPE_INT, TSDB_CFG_CTYPE_B_CONFIG, @@ -778,6 +781,11 @@ void tsInitGlobalConfig() { tsGlobalConfigNum = (int)(cfg - tsGlobalConfig); } +static pthread_once_t initGlobalConfig = PTHREAD_ONCE_INIT; +void tsInitGlobalConfig() { + pthread_once(&initGlobalConfig, doInitGlobalConfig); +} + void tsReadGlobalLogConfig() { tsInitGlobalConfig(); diff --git a/src/util/src/thash.c b/src/util/src/thash.c index 3b9cec0df5..e3c6fe26b4 100644 --- a/src/util/src/thash.c +++ b/src/util/src/thash.c @@ -13,13 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include -#include #include "os.h" #include "tmempool.h" diff --git a/src/util/src/thistogram.c b/src/util/src/thistogram.c index 6cd02cc72e..5fef9077ea 100644 --- a/src/util/src/thistogram.c +++ b/src/util/src/thistogram.c @@ -13,13 +13,7 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include -#include +#include "os.h" #include "taosmsg.h" #include "thistogram.h" diff --git a/src/util/src/tidpool.c b/src/util/src/tidpool.c index a9cc78a026..c50c38aa3c 100644 --- a/src/util/src/tidpool.c +++ b/src/util/src/tidpool.c @@ -13,10 +13,7 @@ * along with this program. If not, see . */ -#include -#include -#include -#include +#include "os.h" #include "tlog.h" typedef struct { diff --git a/src/util/src/tlog.c b/src/util/src/tlog.c index 02e30be33a..1a7f672e00 100644 --- a/src/util/src/tlog.c +++ b/src/util/src/tlog.c @@ -13,22 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - #include "os.h" #include "tlog.h" #include "tutil.h" @@ -381,7 +365,7 @@ void tprintf(const char *const flags, int dflag, const char *const format, ...) } if (taosLogMaxLines > 0) { - __sync_add_and_fetch_32(&taosLogLines, 1); + atomic_add_fetch_32(&taosLogLines, 1); if ((taosLogLines > taosLogMaxLines) && (openInProgress == 0)) taosOpenNewLogFile(); } @@ -458,7 +442,7 @@ void taosPrintLongString(const char *const flags, int dflag, const char *const f taosPushLogBuffer(logHandle, buffer, len); if (taosLogMaxLines > 0) { - __sync_add_and_fetch_32(&taosLogLines, 1); + atomic_add_fetch_32(&taosLogLines, 1); if ((taosLogLines > taosLogMaxLines) && (openInProgress == 0)) taosOpenNewLogFile(); } diff --git a/src/util/src/tmem.c b/src/util/src/tmem.c index c2bb836635..462da884b9 100644 --- a/src/util/src/tmem.c +++ b/src/util/src/tmem.c @@ -13,13 +13,7 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include - +#include "os.h" #include "tlog.h" extern int32_t taosGetTimestampSec(); diff --git a/src/util/src/tmodule.c b/src/util/src/tmodule.c index fabdc1ed4f..54669a20be 100644 --- a/src/util/src/tmodule.c +++ b/src/util/src/tmodule.c @@ -13,12 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include - #include "os.h" #include "tmodule.h" #include "tutil.h" diff --git a/src/util/src/tsched.c b/src/util/src/tsched.c index ee16b92b65..bd49c670f6 100644 --- a/src/util/src/tsched.c +++ b/src/util/src/tsched.c @@ -13,14 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include -#include - #include "os.h" #include "tlog.h" #include "tsched.h" diff --git a/src/util/src/tsocket.c b/src/util/src/tsocket.c index be352eb7ad..9a2dafe377 100644 --- a/src/util/src/tsocket.c +++ b/src/util/src/tsocket.c @@ -13,17 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include -#include - -#include -#include -#include -#include -#include - #include "os.h" #include "tglobalcfg.h" #include "tlog.h" @@ -280,7 +269,6 @@ int taosOpenUdpSocket(char *ip, short port) { int bufSize = 8192000; pTrace("open udp socket:%s:%d", ip, port); - // if (tsAllowLocalhost) ip = "0.0.0.0"; memset((char *)&localAddr, 0, sizeof(localAddr)); localAddr.sin_family = AF_INET; @@ -345,7 +333,6 @@ int taosOpenTcpClientSocket(char *destIp, short destPort, char *clientIp) { int ret; pTrace("open tcp client socket:%s:%d", destIp, destPort); - // if (tsAllowLocalhost) destIp = "0.0.0.0"; sockFd = (int)socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); @@ -441,7 +428,6 @@ int taosOpenTcpServerSocket(char *ip, short port) { int reuse; pTrace("open tcp server socket:%s:%d", ip, port); - // if (tsAllowLocalhost) ip = "0.0.0.0"; bzero((char *)&serverAdd, sizeof(serverAdd)); serverAdd.sin_family = AF_INET; @@ -483,7 +469,6 @@ int taosOpenRawSocket(char *ip) { struct sockaddr_in rawAdd; pTrace("open udp raw socket:%s", ip); - // if (tsAllowLocalhost) ip = "0.0.0.0"; fd = (int)socket(AF_INET, SOCK_RAW, IPPROTO_UDP); if (fd < 0) { diff --git a/src/util/src/tstatus.c b/src/util/src/tstatus.c index 570ba547eb..1e21285960 100644 --- a/src/util/src/tstatus.c +++ b/src/util/src/tstatus.c @@ -13,10 +13,54 @@ * along with this program. If not, see . */ -char* sdbDnodeStatusStr[] = {"offline", "creating", "unsynced", "slave", "master", "ready"}; +#include "taosmsg.h" +#include "tsdb.h" -char* sdbDnodeBalanceStateStr[] = {"balanced", "balancing", "offline removing", "shell removing"}; +const char* taosGetVnodeStatusStr(int vnodeStatus) { + switch (vnodeStatus) { + case TSDB_VNODE_STATUS_OFFLINE:return "offline"; + case TSDB_VNODE_STATUS_CREATING: return "creating"; + case TSDB_VNODE_STATUS_UNSYNCED: return "unsynced"; + case TSDB_VNODE_STATUS_SLAVE: return "slave"; + case TSDB_VNODE_STATUS_MASTER: return "master"; + case TSDB_VNODE_STATUS_CLOSING: return "closing"; + case TSDB_VNODE_STATUS_DELETING: return "deleting"; + default: return "undefined"; + } +} -char* sdbVnodeSyncStatusStr[] = {"init", "syncing", "sync_cache", "sync_file"}; +const char* taosGetDnodeStatusStr(int dnodeStatus) { + switch (dnodeStatus) { + case TSDB_DNODE_STATUS_OFFLINE: return "offline"; + case TSDB_DNODE_STATUS_READY: return "ready"; + default: return "undefined"; + } +} -char* sdbVnodeDropStateStr[] = {"ready", "dropping"}; +const char* taosGetDnodeBalanceStateStr(int dnodeBalanceStatus) { + switch (dnodeBalanceStatus) { + case LB_DNODE_STATE_BALANCED: return "balanced"; + case LB_DNODE_STATE_BALANCING: return "balancing"; + case LB_DNODE_STATE_OFFLINE_REMOVING: return "offline removing"; + case LB_DNODE_STATE_SHELL_REMOVING: return "removing"; + default: return "undefined"; + } +} + +const char* taosGetVnodeSyncStatusStr(int vnodeSyncStatus) { + switch (vnodeSyncStatus) { + case STDB_SSTATUS_INIT: return "init"; + case TSDB_SSTATUS_SYNCING: return "syncing"; + case TSDB_SSTATUS_SYNC_CACHE: return "sync_cache"; + case TSDB_SSTATUS_SYNC_FILE: return "sync_file"; + default: return "undefined"; + } +} + +const char* taosGetVnodeDropStatusStr(int dropping) { + switch (dropping) { + case 0: return "ready"; + case 1: return "dropping"; + default: return "undefined"; + } +} \ No newline at end of file diff --git a/src/util/src/ttimer.c b/src/util/src/ttimer.c index 798e63a64a..a1a356b13b 100644 --- a/src/util/src/ttimer.c +++ b/src/util/src/ttimer.c @@ -13,12 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include #include "os.h" #include "tlog.h" #include "tsched.h" @@ -82,13 +76,16 @@ typedef struct time_wheel_t { } time_wheel_t; uint32_t tmrDebugFlag = DEBUG_ERROR | DEBUG_WARN | DEBUG_FILE; +uint32_t taosMaxTmrCtrl = 512; static pthread_once_t tmrModuleInit = PTHREAD_ONCE_INIT; static pthread_mutex_t tmrCtrlMutex; -static tmr_ctrl_t tmrCtrls[MAX_NUM_OF_TMRCTL]; +static tmr_ctrl_t* tmrCtrls; static tmr_ctrl_t* unusedTmrCtrl = NULL; -void* tmrQhandle; -int taosTmrThreads = 1; +static void* tmrQhandle; +static int numOfTmrCtrl = 0; + +int taosTmrThreads = 1; static uintptr_t nextTimerId = 0; @@ -102,15 +99,15 @@ static timer_map_t timerMap; static uintptr_t getNextTimerId() { uintptr_t id; do { - id = __sync_add_and_fetch_ptr(&nextTimerId, 1); + id = atomic_add_fetch_ptr(&nextTimerId, 1); } while (id == 0); return id; } -static void timerAddRef(tmr_obj_t* timer) { __sync_add_and_fetch_8(&timer->refCount, 1); } +static void timerAddRef(tmr_obj_t* timer) { atomic_add_fetch_8(&timer->refCount, 1); } static void timerDecRef(tmr_obj_t* timer) { - if (__sync_sub_and_fetch_8(&timer->refCount, 1) == 0) { + if (atomic_sub_fetch_8(&timer->refCount, 1) == 0) { free(timer); } } @@ -118,7 +115,7 @@ static void timerDecRef(tmr_obj_t* timer) { static void lockTimerList(timer_list_t* list) { int64_t tid = taosGetPthreadId(); int i = 0; - while (__sync_val_compare_and_swap_64(&(list->lockedBy), 0, tid) != 0) { + while (atomic_val_compare_exchange_64(&(list->lockedBy), 0, tid) != 0) { if (++i % 1000 == 0) { sched_yield(); } @@ -127,9 +124,9 @@ static void lockTimerList(timer_list_t* list) { static void unlockTimerList(timer_list_t* list) { int64_t tid = taosGetPthreadId(); - if (__sync_val_compare_and_swap_64(&(list->lockedBy), tid, 0) != tid) { + if (atomic_val_compare_exchange_64(&(list->lockedBy), tid, 0) != tid) { assert(false); - tmrError("trying to unlock a timer list not locked by current thread."); + tmrError("%d trying to unlock a timer list not locked by current thread.", tid); } } @@ -254,15 +251,15 @@ static bool removeFromWheel(tmr_obj_t* timer) { static void processExpiredTimer(void* handle, void* arg) { tmr_obj_t* timer = (tmr_obj_t*)handle; timer->executedBy = taosGetPthreadId(); - uint8_t state = __sync_val_compare_and_swap_8(&timer->state, TIMER_STATE_WAITING, TIMER_STATE_EXPIRED); + uint8_t state = atomic_val_compare_exchange_8(&timer->state, TIMER_STATE_WAITING, TIMER_STATE_EXPIRED); if (state == TIMER_STATE_WAITING) { - const char* fmt = "timer[label=%s, id=%lld, fp=%p, param=%p] execution start."; + const char* fmt = "%s timer[id=%lld, fp=%p, param=%p] execution start."; tmrTrace(fmt, timer->ctrl->label, timer->id, timer->fp, timer->param); (*timer->fp)(timer->param, (tmr_h)timer->id); atomic_store_8(&timer->state, TIMER_STATE_STOPPED); - fmt = "timer[label=%s, id=%lld, fp=%p, param=%p] execution end."; + fmt = "%s timer[id=%lld, fp=%p, param=%p] execution end."; tmrTrace(fmt, timer->ctrl->label, timer->id, timer->fp, timer->param); } removeTimer(timer->id); @@ -270,18 +267,21 @@ static void processExpiredTimer(void* handle, void* arg) { } static void addToExpired(tmr_obj_t* head) { - const char* fmt = "timer[label=%s, id=%lld, fp=%p, param=%p] expired"; + const char* fmt = "%s adding expired timer[id=%lld, fp=%p, param=%p] to queue."; while (head != NULL) { - tmrTrace(fmt, head->ctrl->label, head->id, head->fp, head->param); - + uintptr_t id = head->id; tmr_obj_t* next = head->next; + tmrTrace(fmt, head->ctrl->label, id, head->fp, head->param); + SSchedMsg schedMsg; schedMsg.fp = NULL; schedMsg.tfp = processExpiredTimer; schedMsg.ahandle = head; schedMsg.thandle = NULL; taosScheduleTask(tmrQhandle, &schedMsg); + + tmrTrace("timer[id=%lld] has been added to queue.", id); head = next; } } @@ -295,7 +295,7 @@ static uintptr_t doStartTimer(tmr_obj_t* timer, TAOS_TMR_CALLBACK fp, int msecon timer->ctrl = ctrl; addTimer(timer); - const char* fmt = "timer[label=%s, id=%lld, fp=%p, param=%p] started"; + const char* fmt = "%s timer[id=%lld, fp=%p, param=%p] started"; tmrTrace(fmt, ctrl->label, timer->id, timer->fp, timer->param); if (mseconds == 0) { @@ -318,7 +318,7 @@ tmr_h taosTmrStart(TAOS_TMR_CALLBACK fp, int mseconds, void* param, void* handle tmr_obj_t* timer = (tmr_obj_t*)calloc(1, sizeof(tmr_obj_t)); if (timer == NULL) { - tmrError("failed to allocated memory for new timer object."); + tmrError("%s failed to allocated memory for new timer object.", ctrl->label); return NULL; } @@ -389,7 +389,7 @@ static bool doStopTimer(tmr_obj_t* timer, uint8_t state) { // we cannot guarantee the thread safety of the timr in all other cases. reusable = true; } - const char* fmt = "timer[label=%s, id=%lld, fp=%p, param=%p] is cancelled."; + const char* fmt = "%s timer[id=%lld, fp=%p, param=%p] is cancelled."; tmrTrace(fmt, timer->ctrl->label, timer->id, timer->fp, timer->param); } else if (state != TIMER_STATE_EXPIRED) { // timer already stopped or cancelled, has nothing to do in this case @@ -400,7 +400,7 @@ static bool doStopTimer(tmr_obj_t* timer, uint8_t state) { } else { assert(timer->executedBy != taosGetPthreadId()); - const char* fmt = "timer[label=%s, id=%lld, fp=%p, param=%p] fired, waiting..."; + const char* fmt = "%s timer[id=%lld, fp=%p, param=%p] fired, waiting..."; tmrTrace(fmt, timer->ctrl->label, timer->id, timer->fp, timer->param); for (int i = 1; atomic_load_8(&timer->state) != TIMER_STATE_STOPPED; i++) { @@ -409,7 +409,7 @@ static bool doStopTimer(tmr_obj_t* timer, uint8_t state) { } } - fmt = "timer[label=%s, id=%lld, fp=%p, param=%p] stopped."; + fmt = "%s timer[id=%lld, fp=%p, param=%p] stopped."; tmrTrace(fmt, timer->ctrl->label, timer->id, timer->fp, timer->param); } @@ -425,7 +425,7 @@ bool taosTmrStop(tmr_h timerId) { return false; } - uint8_t state = __sync_val_compare_and_swap_8(&timer->state, TIMER_STATE_WAITING, TIMER_STATE_CANCELED); + uint8_t state = atomic_val_compare_exchange_8(&timer->state, TIMER_STATE_WAITING, TIMER_STATE_CANCELED); doStopTimer(timer, state); timerDecRef(timer); @@ -448,9 +448,9 @@ bool taosTmrReset(TAOS_TMR_CALLBACK fp, int mseconds, void* param, void* handle, bool stopped = false; tmr_obj_t* timer = findTimer(id); if (timer == NULL) { - tmrTrace("timer[id=%lld] does not exist", id); + tmrTrace("%s timer[id=%lld] does not exist", ctrl->label, id); } else { - uint8_t state = __sync_val_compare_and_swap_8(&timer->state, TIMER_STATE_WAITING, TIMER_STATE_CANCELED); + uint8_t state = atomic_val_compare_exchange_8(&timer->state, TIMER_STATE_WAITING, TIMER_STATE_CANCELED); if (!doStopTimer(timer, state)) { timerDecRef(timer); timer = NULL; @@ -463,7 +463,7 @@ bool taosTmrReset(TAOS_TMR_CALLBACK fp, int mseconds, void* param, void* handle, return stopped; } - tmrTrace("timer[id=%lld] is reused", timer->id); + tmrTrace("%s timer[id=%lld] is reused", ctrl->label, timer->id); // wait until there's no other reference to this timer, // so that we can reuse this timer safely. @@ -481,7 +481,13 @@ bool taosTmrReset(TAOS_TMR_CALLBACK fp, int mseconds, void* param, void* handle, } static void taosTmrModuleInit(void) { - for (int i = 0; i < tListLen(tmrCtrls) - 1; ++i) { + tmrCtrls = malloc(sizeof(tmr_ctrl_t) * taosMaxTmrCtrl); + if (tmrCtrls == NULL) { + tmrError("failed to allocate memory for timer controllers."); + return; + } + + for (int i = 0; i < taosMaxTmrCtrl - 1; ++i) { tmr_ctrl_t* ctrl = tmrCtrls + i; ctrl->next = ctrl + 1; } @@ -526,29 +532,33 @@ void* taosTmrInit(int maxNumOfTmrs, int resolution, int longest, const char* lab tmr_ctrl_t* ctrl = unusedTmrCtrl; if (ctrl != NULL) { unusedTmrCtrl = ctrl->next; + numOfTmrCtrl++; } pthread_mutex_unlock(&tmrCtrlMutex); if (ctrl == NULL) { - tmrError("too many timer controllers, failed to create timer controller[label=%s].", label); + tmrError("%s too many timer controllers, failed to create timer controller.", label); return NULL; } strncpy(ctrl->label, label, sizeof(ctrl->label)); ctrl->label[sizeof(ctrl->label) - 1] = 0; - tmrTrace("timer controller[label=%s] is initialized.", label); + tmrTrace("%s timer controller is initialized, number of timer controllers: %d.", label, numOfTmrCtrl); return ctrl; } void taosTmrCleanUp(void* handle) { tmr_ctrl_t* ctrl = (tmr_ctrl_t*)handle; - assert(ctrl != NULL && ctrl->label[0] != 0); + if (ctrl == NULL || ctrl->label[0] == 0) { + return; + } - tmrTrace("timer controller[label=%s] is cleaned up.", ctrl->label); + tmrTrace("%s timer controller is cleaned up.", ctrl->label); ctrl->label[0] = 0; pthread_mutex_lock(&tmrCtrlMutex); ctrl->next = unusedTmrCtrl; + numOfTmrCtrl--; unusedTmrCtrl = ctrl; pthread_mutex_unlock(&tmrCtrlMutex); } diff --git a/src/util/src/ttypes.c b/src/util/src/ttypes.c index 3b36b1b31c..185a63c0eb 100644 --- a/src/util/src/ttypes.c +++ b/src/util/src/ttypes.c @@ -13,15 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include -#include -#include - #include "os.h" #include "taos.h" #include "tsdb.h" diff --git a/src/util/src/tutil.c b/src/util/src/tutil.c index 805f27a381..8413294bcc 100644 --- a/src/util/src/tutil.c +++ b/src/util/src/tutil.c @@ -13,14 +13,6 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include -#include - #include "os.h" #ifdef USE_LIBICONV @@ -406,13 +398,6 @@ int32_t taosFileRename(char *fullPath, char *suffix, char delimiter, char **dstP return rename(fullPath, *dstPath); } -bool taosCheckDbName(char *db, char *monitordb) { - char *pos = strchr(db, '.'); - if (pos == NULL) return false; - - return strncasecmp(pos + 1, monitordb, strlen(monitordb)) == 0; -} - bool taosUcs4ToMbs(void *ucs4, int32_t ucs4_max_len, char *mbs) { #ifdef USE_LIBICONV iconv_t cd = iconv_open(tsCharset, DEFAULT_UNICODE_ENCODEC); @@ -524,3 +509,22 @@ FORCE_INLINE double taos_align_get_double(char* pBuf) { *(int64_t*)(&dv) = *(int64_t*)pBuf; return dv; } + +typedef struct CharsetPair { + char *oldCharset; + char *newCharset; +} CharsetPair; + +char *taosCharsetReplace(char *charsetstr) { + CharsetPair charsetRep[] = { + { "utf8", "UTF-8" }, { "936", "CP936" }, + }; + + for (int32_t i = 0; i < tListLen(charsetRep); ++i) { + if (strcasecmp(charsetRep[i].oldCharset, charsetstr) == 0) { + return strdup(charsetRep[i].newCharset); + } + } + + return strdup(charsetstr); +} diff --git a/src/util/src/version.c b/src/util/src/version.c index dc2c3c5480..96e7ad4ead 100644 --- a/src/util/src/version.c +++ b/src/util/src/version.c @@ -1,4 +1,4 @@ char version[64] = "1.6.4.0"; char compatible_version[64] = "1.6.1.0"; -char gitinfo[128] = "869171d2331eb25ba0901e88d33ae627bf5a9d91"; -char buildinfo[512] = "Built by ubuntu at 2019-11-07 22:31"; +char gitinfo[128] = "d04354a8ac2f7dd9ba521d755e5d484a203783d9"; +char buildinfo[512] = "Built by root at 2019-11-11 10:23"; diff --git a/tests/comparisonTest/opentsdb/opentsdbtest/pom.xml b/tests/comparisonTest/opentsdb/opentsdbtest/pom.xml index 9271d478a6..6f5fa6e132 100644 --- a/tests/comparisonTest/opentsdb/opentsdbtest/pom.xml +++ b/tests/comparisonTest/opentsdb/opentsdbtest/pom.xml @@ -118,7 +118,7 @@ com.fasterxml.jackson.core jackson-databind - 2.9.10 + 2.9.10.1 diff --git a/tests/examples/JDBC/readme.md b/tests/examples/JDBC/readme.md index 54a2e5c6ac..a91624a9e4 100644 --- a/tests/examples/JDBC/readme.md +++ b/tests/examples/JDBC/readme.md @@ -9,5 +9,5 @@ TDengine's JDBC driver jar is not yet published to maven center repo, so we need ## Compile the Demo Code and Run It To compile the demo project, go to the source directory ``TDengine/tests/examples/JDBC/JDBCDemo`` and execute
mvn clean assembly:single package
-The ``pom.xml`` is configured to package all the dependencies into one executable jar file. To run it, go to ``TDengine/tests/examples/JDBC/JDBCDemo/target`` and execute +The ``pom.xml`` is configured to package all the dependencies into one executable jar file. To run it, go to ``examples/JDBC/JDBCDemo/target`` and execute
java -jar jdbcdemo-1.0-SNAPSHOT-jar-with-dependencies.jar
diff --git a/tests/examples/lua/build.sh b/tests/examples/lua/build.sh old mode 100644 new mode 100755 diff --git a/tests/examples/lua/lua_connector.c b/tests/examples/lua/lua_connector.c index f37657e822..f4065bb274 100644 --- a/tests/examples/lua/lua_connector.c +++ b/tests/examples/lua/lua_connector.c @@ -7,8 +7,15 @@ #include #include -static int l_connect(lua_State *L) -{ +struct cb_param{ + lua_State* state; + int callback; + void * stream; +}; + + + +static int l_connect(lua_State *L){ TAOS * taos; char *host = lua_tostring(L, 1); char *user = lua_tostring(L, 2); @@ -29,6 +36,7 @@ static int l_connect(lua_State *L) lua_pushstring(L, taos_errstr(taos)); lua_setfield(L, table_index, "error"); lua_pushlightuserdata(L,NULL); + lua_setfield(L, table_index, "conn"); }else{ printf("success to connect server\n"); lua_pushnumber(L, 0); @@ -49,7 +57,7 @@ static int l_query(lua_State *L){ lua_newtable(L); int table_index = lua_gettop(L); - printf("receive command:%s\r\n",s); + // printf("receive command:%s\r\n",s); if(taos_query(taos, s)!=0){ printf("failed, reason:%s\n", taos_errstr(taos)); lua_pushnumber(L, -1); @@ -78,8 +86,12 @@ static int l_query(lua_State *L){ TAOS_FIELD *fields = taos_fetch_fields(result); char temp[256]; + int affectRows = taos_affected_rows(taos); + // printf(" affect rows:%d\r\n", affectRows); lua_pushnumber(L, 0); lua_setfield(L, table_index, "code"); + lua_pushinteger(L, affectRows); + lua_setfield(L, table_index, "affected"); lua_newtable(L); while ((row = taos_fetch_row(result))) { @@ -95,7 +107,7 @@ static int l_query(lua_State *L){ } lua_pushstring(L,fields[i].name); - //printf("field name:%s,type:%d\n",fields[i].name,fields[i].type); + switch (fields[i].type) { case TSDB_DATA_TYPE_TINYINT: lua_pushinteger(L,*((char *)row[i])); @@ -142,6 +154,115 @@ static int l_query(lua_State *L){ return 1; } +void stream_cb(void *param, TAOS_RES *result, TAOS_ROW row){ + + struct cb_param* p = (struct cb_param*) param; + TAOS_FIELD *fields = taos_fetch_fields(result); + int numFields = taos_num_fields(result); + + printf("\n\r-----------------------------------------------------------------------------------\n"); + + // printf("r:%d, L:%d\n",p->callback, p->state); + + lua_State *L = p->state; + lua_rawgeti(L, LUA_REGISTRYINDEX, p->callback); + + lua_newtable(L); + + for (int i = 0; i < numFields; ++i) { + if (row[i] == NULL) { + continue; + } + + lua_pushstring(L,fields[i].name); + + switch (fields[i].type) { + case TSDB_DATA_TYPE_TINYINT: + lua_pushinteger(L,*((char *)row[i])); + break; + case TSDB_DATA_TYPE_SMALLINT: + lua_pushinteger(L,*((short *)row[i])); + break; + case TSDB_DATA_TYPE_INT: + lua_pushinteger(L,*((int *)row[i])); + break; + case TSDB_DATA_TYPE_BIGINT: + lua_pushinteger(L,*((int64_t *)row[i])); + break; + case TSDB_DATA_TYPE_FLOAT: + lua_pushnumber(L,*((float *)row[i])); + break; + case TSDB_DATA_TYPE_DOUBLE: + lua_pushnumber(L,*((double *)row[i])); + break; + case TSDB_DATA_TYPE_BINARY: + case TSDB_DATA_TYPE_NCHAR: + lua_pushstring(L,(char *)row[i]); + break; + case TSDB_DATA_TYPE_TIMESTAMP: + lua_pushinteger(L,*((int64_t *)row[i])); + break; + case TSDB_DATA_TYPE_BOOL: + lua_pushinteger(L,*((char *)row[i])); + break; + default: + lua_pushnil(L); + break; + } + + lua_settable(L, -3); + } + + lua_call(L, 1, 0); + + printf("-----------------------------------------------------------------------------------\n\r"); +} + +static int l_open_stream(lua_State *L){ + int r = luaL_ref(L, LUA_REGISTRYINDEX); + TAOS * taos = lua_topointer(L,1); + char * sqlstr = lua_tostring(L,2); + int stime = luaL_checknumber(L,3); + + lua_newtable(L); + int table_index = lua_gettop(L); + + struct cb_param *p = malloc(sizeof(struct cb_param)); + p->state = L; + p->callback=r; + // printf("r:%d, L:%d\n",r,L); + void * s = taos_open_stream(taos,sqlstr,stream_cb,stime,p,NULL); + if (s == NULL) { + printf("failed to open stream, reason:%s\n", taos_errstr(taos)); + free(p); + lua_pushnumber(L, -1); + lua_setfield(L, table_index, "code"); + lua_pushstring(L, taos_errstr(taos)); + lua_setfield(L, table_index, "error"); + lua_pushlightuserdata(L,NULL); + lua_setfield(L, table_index, "stream"); + }else{ + // printf("success to open stream\n"); + lua_pushnumber(L, 0); + lua_setfield(L, table_index, "code"); + lua_pushstring(L, taos_errstr(taos)); + lua_setfield(L, table_index, "error"); + p->stream = s; + lua_pushlightuserdata(L,p); + lua_setfield(L, table_index, "stream");//stream has different content in lua and c. + } + + return 1; +} + +static int l_close_stream(lua_State *L){ + //TODO:get stream and free cb_param + struct cb_param *p = lua_touserdata(L,1); + taos_close_stream(p->stream); + free(p); + return 0; +} + static int l_close(lua_State *L){ TAOS * taos= lua_topointer(L,1); lua_newtable(L); @@ -166,6 +287,8 @@ static const struct luaL_Reg lib[] = { {"connect", l_connect}, {"query", l_query}, {"close", l_close}, + {"open_stream", l_open_stream}, + {"close_stream", l_close_stream}, {NULL, NULL} }; diff --git a/tests/examples/lua/test.lua b/tests/examples/lua/test.lua index f644b82dd4..38ae1c82f2 100644 --- a/tests/examples/lua/test.lua +++ b/tests/examples/lua/test.lua @@ -35,10 +35,12 @@ if res.code ~=0 then return end -res = driver.query(conn,"insert into m1 values (1592222222222,0,'robotspace'), (1592222222223,1,'Hilink'),(1592222222224,2,'Harmony')") +res = driver.query(conn,"insert into m1 values ('2019-09-01 00:00:00.001',0,'robotspace'), ('2019-09-01 00:00:00.002',1,'Hilink'),('2019-09-01 00:00:00.003',2,'Harmony')") if res.code ~=0 then print(res.error) return +else + print("insert successfully, affected:"..res.affected) end res = driver.query(conn,"select * from m1") @@ -55,4 +57,69 @@ else end end +res = driver.query(conn,"CREATE TABLE thermometer (ts timestamp, degree double) TAGS(location binary(20), type int)") +if res.code ~=0 then + print(res.error) + return +end +res = driver.query(conn,"CREATE TABLE therm1 USING thermometer TAGS ('beijing', 1)") +if res.code ~=0 then + print(res.error) + return +end +res = driver.query(conn,"INSERT INTO therm1 VALUES ('2019-09-01 00:00:00.001', 20),('2019-09-01 00:00:00.002', 21)") + +if res.code ~=0 then + print(res.error) + return +else + print("insert successfully, affected:"..res.affected) +end + +res = driver.query(conn,"SELECT COUNT(*) count, AVG(degree) AS av, MAX(degree), MIN(degree) FROM thermometer WHERE location='beijing' or location='tianjin' GROUP BY location, type") +if res.code ~=0 then + print("select error:"..res.error) + return +else + print("in lua, result:") + for i = 1, #(res.item) do + print("res:"..res.item[i].count) + end +end + +function callback(t) + print("continuous query result:") + for key, value in pairs(t) do + print("key:"..key..", value:"..value) + end +end + +local stream +res = driver.open_stream(conn,"SELECT COUNT(*) as count, AVG(degree) as avg, MAX(degree) as max, MIN(degree) as min FROM thermometer interval(2s) sliding(2s);)",0,callback) +if res.code ~=0 then + print("open stream error:"..res.error) + return +else + print("openstream ok") + stream = res.stream +end + +--From now on we begin continous query in an definite (infinite if you want) loop. +local loop_index = 0 +while loop_index < 20 do + local t = os.time()*1000 + local v = loop_index + res = driver.query(conn,string.format("INSERT INTO therm1 VALUES (%d, %d)",t,v)) + + if res.code ~=0 then + print(res.error) + return + else + print("insert successfully, affected:"..res.affected) + end + os.execute("sleep " .. 1) + loop_index = loop_index + 1 +end + +driver.close_stream(stream) driver.close(conn)