From 4d19f48d8a00c14ebe270f50bcfff550a9cd06b5 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Fri, 2 Aug 2024 16:29:48 +0800 Subject: [PATCH 01/39] feat:[TS-3718]save slow log file one day one file --- include/util/tlog.h | 2 +- source/common/src/tglobal.c | 2 +- source/dnode/mgmt/test/sut/src/sut.cpp | 2 +- source/dnode/mnode/impl/test/sdb/sdbTest.cpp | 2 +- source/dnode/mnode/impl/test/trans/trans2.cpp | 2 +- source/libs/executor/test/joinTests.cpp | 2 +- source/libs/index/test/fstUT.cc | 2 +- source/libs/index/test/indexBench.cc | 2 +- source/libs/index/test/indexTests.cc | 2 +- .../libs/index/test/index_executor_tests.cpp | 2 +- source/libs/index/test/jsonUT.cc | 2 +- source/libs/parser/test/parTestMain.cpp | 2 +- source/libs/planner/test/planTestMain.cpp | 2 +- source/libs/qworker/test/qworkerTests.cpp | 2 +- .../libs/scalar/test/filter/filterTests.cpp | 2 +- .../libs/scalar/test/scalar/scalarTests.cpp | 2 +- source/libs/scheduler/test/schedulerTests.cpp | 2 +- source/libs/sync/test/syncTest.cpp | 2 +- source/libs/sync/test/syncTestTool.cpp | 2 +- source/libs/transport/test/cliBench.c | 2 +- source/libs/transport/test/http_test.c | 2 +- source/libs/transport/test/svrBench.c | 2 +- source/libs/transport/test/transUT.cpp | 2 +- source/util/src/tlog.c | 239 +++++++++++------- source/util/test/trefTest.c | 2 +- 25 files changed, 167 insertions(+), 120 deletions(-) diff --git a/include/util/tlog.h b/include/util/tlog.h index 67aafbfe44..c24a456c99 100644 --- a/include/util/tlog.h +++ b/include/util/tlog.h @@ -69,7 +69,7 @@ extern int32_t tdbDebugFlag; extern int32_t sndDebugFlag; extern int32_t simDebugFlag; -int32_t taosInitLog(const char *logName, int32_t maxFiles); +int32_t taosInitLog(const char *logName, int32_t maxFiles, bool tsc); void taosCloseLog(); void taosResetLog(); void taosDumpData(uint8_t *msg, int32_t len); diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index 46219fe34c..b15ceaee7e 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -1596,7 +1596,7 @@ int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDi goto _exit; } - if ((code = taosInitLog(logname, logFileNum)) != 0) { + if ((code = taosInitLog(logname, logFileNum, tsc)) != 0) { printf("failed to init log file since %s\n", tstrerror(code)); goto _exit; } diff --git a/source/dnode/mgmt/test/sut/src/sut.cpp b/source/dnode/mgmt/test/sut/src/sut.cpp index b54590ce82..f074a015d2 100644 --- a/source/dnode/mgmt/test/sut/src/sut.cpp +++ b/source/dnode/mgmt/test/sut/src/sut.cpp @@ -37,7 +37,7 @@ void Testbase::InitLog(const char* path) { taosGetSystemInfo(); tsRpcQueueMemoryAllowed = tsTotalMemoryKB * 0.1; - if (taosInitLog("taosdlog", 1) != 0) { + if (taosInitLog("taosdlog", 1, false) != 0) { printf("failed to init log file\n"); } } diff --git a/source/dnode/mnode/impl/test/sdb/sdbTest.cpp b/source/dnode/mnode/impl/test/sdb/sdbTest.cpp index b12ea9a28f..97b7ccd5f7 100644 --- a/source/dnode/mnode/impl/test/sdb/sdbTest.cpp +++ b/source/dnode/mnode/impl/test/sdb/sdbTest.cpp @@ -35,7 +35,7 @@ class MndTestSdb : public ::testing::Test { taosRemoveDir(path); taosMkDir(path); tstrncpy(tsLogDir, path, PATH_MAX); - if (taosInitLog("taosdlog", 1) != 0) { + if (taosInitLog("taosdlog", 1, false) != 0) { printf("failed to init log file\n"); } } diff --git a/source/dnode/mnode/impl/test/trans/trans2.cpp b/source/dnode/mnode/impl/test/trans/trans2.cpp index 4d0d53ced0..b73f07c778 100644 --- a/source/dnode/mnode/impl/test/trans/trans2.cpp +++ b/source/dnode/mnode/impl/test/trans/trans2.cpp @@ -52,7 +52,7 @@ class MndTestTrans2 : public ::testing::Test { taosRemoveDir(logpath); taosMkDir(logpath); tstrncpy(tsLogDir, logpath, PATH_MAX); - if (taosInitLog("taosdlog", 1) != 0) { + if (taosInitLog("taosdlog", 1, false) != 0) { printf("failed to init log file\n"); } } diff --git a/source/libs/executor/test/joinTests.cpp b/source/libs/executor/test/joinTests.cpp index 6100b0722d..7d3cf0580c 100755 --- a/source/libs/executor/test/joinTests.cpp +++ b/source/libs/executor/test/joinTests.cpp @@ -2884,7 +2884,7 @@ void jtInitLogFile() { qDebugFlag = 159; TAOS_STRCPY(tsLogDir, TD_LOG_DIR_PATH); - if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) { + if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum, false) < 0) { JT_PRINTF("failed to open log file in directory:%s\n", tsLogDir); } } diff --git a/source/libs/index/test/fstUT.cc b/source/libs/index/test/fstUT.cc index 37c9d1b97b..04bc6c9a50 100644 --- a/source/libs/index/test/fstUT.cc +++ b/source/libs/index/test/fstUT.cc @@ -28,7 +28,7 @@ static void EnvInit() { taosMkDir(path.c_str()); // init log file tstrncpy(tsLogDir, path.c_str(), PATH_MAX); - if (taosInitLog("tindex.idx", 1) != 0) { + if (taosInitLog("tindex.idx", 1, false) != 0) { printf("failed to init log"); } // init index file diff --git a/source/libs/index/test/indexBench.cc b/source/libs/index/test/indexBench.cc index 78c687029c..369ed54d0b 100644 --- a/source/libs/index/test/indexBench.cc +++ b/source/libs/index/test/indexBench.cc @@ -40,7 +40,7 @@ static void initLog() { taosRemoveDir(tsLogDir); taosMkDir(tsLogDir); - if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) { + if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum, false) < 0) { printf("failed to open log file in directory:%s\n", tsLogDir); } } diff --git a/source/libs/index/test/indexTests.cc b/source/libs/index/test/indexTests.cc index 1c5a04d22d..090ff02ffb 100644 --- a/source/libs/index/test/indexTests.cc +++ b/source/libs/index/test/indexTests.cc @@ -283,7 +283,7 @@ static void initLog() { taosRemoveDir(tsLogDir); taosMkDir(tsLogDir); - if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) { + if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum, false) < 0) { printf("failed to open log file in directory:%s\n", tsLogDir); } } diff --git a/source/libs/index/test/index_executor_tests.cpp b/source/libs/index/test/index_executor_tests.cpp index 232675d32f..f65028e564 100644 --- a/source/libs/index/test/index_executor_tests.cpp +++ b/source/libs/index/test/index_executor_tests.cpp @@ -57,7 +57,7 @@ void sifInitLogFile() { taosRemoveDir(tsLogDir); taosMkDir(tsLogDir); - if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) { + if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum, false) < 0) { printf("failed to open log file in directory:%s\n", tsLogDir); } } diff --git a/source/libs/index/test/jsonUT.cc b/source/libs/index/test/jsonUT.cc index bee8e0dd85..493101eb27 100644 --- a/source/libs/index/test/jsonUT.cc +++ b/source/libs/index/test/jsonUT.cc @@ -40,7 +40,7 @@ static void initLog() { taosRemoveDir(tsLogDir); taosMkDir(tsLogDir); - if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) { + if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum, false) < 0) { printf("failed to open log file in directory:%s\n", tsLogDir); } } diff --git a/source/libs/parser/test/parTestMain.cpp b/source/libs/parser/test/parTestMain.cpp index 87ecf34ebc..3986c96f07 100644 --- a/source/libs/parser/test/parTestMain.cpp +++ b/source/libs/parser/test/parTestMain.cpp @@ -72,7 +72,7 @@ class ParserEnv : public testing::Environment { taosRemoveDir(path); ASSERT_EQ(TSDB_CODE_SUCCESS, taosMkDir(path)); tstrncpy(tsLogDir, path, PATH_MAX); - if (taosInitLog("taoslog", 1) != 0) { + if (taosInitLog("taoslog", 1, false) != 0) { std::cout << "failed to init log file" << std::endl; } } diff --git a/source/libs/planner/test/planTestMain.cpp b/source/libs/planner/test/planTestMain.cpp index 99f71e1bc6..2a1e169d5f 100644 --- a/source/libs/planner/test/planTestMain.cpp +++ b/source/libs/planner/test/planTestMain.cpp @@ -67,7 +67,7 @@ class PlannerEnv : public testing::Environment { taosRemoveDir(path); ASSERT_EQ(TSDB_CODE_SUCCESS,taosMkDir(path)); tstrncpy(tsLogDir, path, PATH_MAX); - if (taosInitLog("taoslog", 1) != 0) { + if (taosInitLog("taoslog", 1, false) != 0) { std::cout << "failed to init log file" << std::endl; } } diff --git a/source/libs/qworker/test/qworkerTests.cpp b/source/libs/qworker/test/qworkerTests.cpp index 91e1dda2ac..42b538b14f 100644 --- a/source/libs/qworker/test/qworkerTests.cpp +++ b/source/libs/qworker/test/qworkerTests.cpp @@ -105,7 +105,7 @@ void qwtInitLogFile() { qDebugFlag = 159; strcpy(tsLogDir, TD_LOG_DIR_PATH); - if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) { + if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum, false) < 0) { printf("failed to open log file in directory:%s\n", tsLogDir); } } diff --git a/source/libs/scalar/test/filter/filterTests.cpp b/source/libs/scalar/test/filter/filterTests.cpp index 80e2c005a7..33d2c1e2ef 100644 --- a/source/libs/scalar/test/filter/filterTests.cpp +++ b/source/libs/scalar/test/filter/filterTests.cpp @@ -57,7 +57,7 @@ void flttInitLogFile() { qDebugFlag = 159; (void)strcpy(tsLogDir, TD_LOG_DIR_PATH); - if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) { + if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum, false) < 0) { printf("failed to open log file in directory:%s\n", tsLogDir); } } diff --git a/source/libs/scalar/test/scalar/scalarTests.cpp b/source/libs/scalar/test/scalar/scalarTests.cpp index 75180c0b66..032b197046 100644 --- a/source/libs/scalar/test/scalar/scalarTests.cpp +++ b/source/libs/scalar/test/scalar/scalarTests.cpp @@ -83,7 +83,7 @@ void scltInitLogFile() { qDebugFlag = 159; (void)strcpy(tsLogDir, TD_LOG_DIR_PATH); - if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) { + if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum, false) < 0) { (void)printf("failed to open log file in directory:%s\n", tsLogDir); } } diff --git a/source/libs/scheduler/test/schedulerTests.cpp b/source/libs/scheduler/test/schedulerTests.cpp index f7c3fb8847..44d32b9480 100644 --- a/source/libs/scheduler/test/schedulerTests.cpp +++ b/source/libs/scheduler/test/schedulerTests.cpp @@ -79,7 +79,7 @@ void schtInitLogFile() { qDebugFlag = 159; TAOS_STRCPY(tsLogDir, TD_LOG_DIR_PATH); - if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) { + if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum, false) < 0) { (void)printf("failed to open log file in directory:%s\n", tsLogDir); } } diff --git a/source/libs/sync/test/syncTest.cpp b/source/libs/sync/test/syncTest.cpp index 7b636085f2..3a9a29f023 100644 --- a/source/libs/sync/test/syncTest.cpp +++ b/source/libs/sync/test/syncTest.cpp @@ -48,7 +48,7 @@ void test4() { } int main(int argc, char** argv) { - taosInitLog("/tmp/syncTest.log", 100); + taosInitLog("/tmp/syncTest.log", 100, false); tsAsyncLog = 0; sDebugFlag = DEBUG_SCREEN + DEBUG_FILE + DEBUG_TRACE + DEBUG_INFO + DEBUG_ERROR; diff --git a/source/libs/sync/test/syncTestTool.cpp b/source/libs/sync/test/syncTestTool.cpp index 4bc2e92d0c..6671cebcaf 100644 --- a/source/libs/sync/test/syncTestTool.cpp +++ b/source/libs/sync/test/syncTestTool.cpp @@ -351,7 +351,7 @@ int main(int argc, char** argv) { char logFile[256]; snprintf(logFile, sizeof(logFile), "/tmp/%s-replicaNum%d-myIndex%d.log", gDir, replicaNum, myIndex); - taosInitLog(logFile, 100); + taosInitLog(logFile, 100, false); sTrace("logFile : %s", logFile); gSnapshotLastApplyIndex = lastApplyIndex; diff --git a/source/libs/transport/test/cliBench.c b/source/libs/transport/test/cliBench.c index 8a5276b814..2fb71062a2 100644 --- a/source/libs/transport/test/cliBench.c +++ b/source/libs/transport/test/cliBench.c @@ -43,7 +43,7 @@ void initLogEnv() { taosRemoveDir(tsLogDir); taosMkDir(tsLogDir); - if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) { + if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum, false) < 0) { printf("failed to open log file in directory:%s\n", tsLogDir); } } diff --git a/source/libs/transport/test/http_test.c b/source/libs/transport/test/http_test.c index d04fb84843..1d0f0fa47a 100644 --- a/source/libs/transport/test/http_test.c +++ b/source/libs/transport/test/http_test.c @@ -31,7 +31,7 @@ void initLogEnv() { taosRemoveDir(tsLogDir); taosMkDir(tsLogDir); - if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) { + if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum, false) < 0) { printf("failed to open log file in directory:%s\n", tsLogDir); } } diff --git a/source/libs/transport/test/svrBench.c b/source/libs/transport/test/svrBench.c index dff3efef12..6408e4dcb2 100644 --- a/source/libs/transport/test/svrBench.c +++ b/source/libs/transport/test/svrBench.c @@ -53,7 +53,7 @@ void initLogEnv() { taosRemoveDir(tsLogDir); taosMkDir(tsLogDir); - if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) { + if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum, false) < 0) { printf("failed to open log file in directory:%s\n", tsLogDir); } } diff --git a/source/libs/transport/test/transUT.cpp b/source/libs/transport/test/transUT.cpp index 2fa94c358f..e57d01bcbc 100644 --- a/source/libs/transport/test/transUT.cpp +++ b/source/libs/transport/test/transUT.cpp @@ -230,7 +230,7 @@ static void initEnv() { taosMkDir(path.c_str()); tstrncpy(tsLogDir, path.c_str(), PATH_MAX); - if (taosInitLog("taosdlog", 1) != 0) { + if (taosInitLog("taosdlog", 1, false) != 0) { printf("failed to init log file\n"); } } diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index 1946a0a274..0f217c439f 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -67,9 +67,12 @@ typedef struct { int32_t lines; int32_t flag; int32_t openInProgress; + int32_t openInProgressSlowLog; int64_t lastKeepFileSec; + char slowLogDay[64]; pid_t pid; char logName[LOG_FILE_NAME_LEN]; + char slowLogName[LOG_FILE_NAME_LEN]; SLogBuff *logHandle; SLogBuff *slowHandle; TdThreadMutex logMutex; @@ -77,7 +80,7 @@ typedef struct { extern SConfig *tsCfg; static int8_t tsLogInited = 0; -static SLogObj tsLogObj = {.fileNum = 1}; +static SLogObj tsLogObj = {.fileNum = 1, .slowHandle = NULL}; static int64_t tsAsyncLogLostLines = 0; static int32_t tsDaylightActive; /* Currently in daylight saving time. */ @@ -131,18 +134,8 @@ static void *taosAsyncOutputLog(void *param); static int32_t taosPushLogBuffer(SLogBuff *pLogBuf, const char *msg, int32_t msgLen); static SLogBuff *taosLogBuffNew(int32_t bufSize); static void taosCloseLogByFd(TdFilePtr pFile); -static int32_t taosOpenLogFile(char *fn, int32_t maxFileNum); - -static FORCE_INLINE void taosUpdateDaylight() { - struct tm Tm, *ptm; - struct timeval timeSecs; - (void)taosGetTimeOfDay(&timeSecs); - time_t curTime = timeSecs.tv_sec; - ptm = taosLocalTime(&curTime, &Tm, NULL); - tsDaylightActive = ptm->tm_isdst; -} -static FORCE_INLINE int32_t taosGetDaylight() { return tsDaylightActive; } - +static int32_t taosInitNormalLog(const char *fn, int32_t maxFileNum); +static void backUpSlowLog(); static int32_t taosStartLog() { TdThreadAttr threadAttr; (void)taosThreadAttrInit(&threadAttr); @@ -153,47 +146,7 @@ static int32_t taosStartLog() { return 0; } -int32_t taosInitSlowLog() { - char fullName[PATH_MAX] = {0}; - char logFileName[64] = {0}; -#ifdef CUS_PROMPT - snprintf(logFileName, 64, "%sSlowLog", CUS_PROMPT); -#else - snprintf(logFileName, 64, "taosSlowLog"); -#endif - - if (strlen(tsLogDir) != 0) { - char lastC = tsLogDir[strlen(tsLogDir) - 1]; - if (lastC == '\\' || lastC == '/') { - snprintf(fullName, PATH_MAX, - "%s" - "%s", - tsLogDir, logFileName); - } else { - snprintf(fullName, PATH_MAX, "%s" TD_DIRSEP "%s", tsLogDir, logFileName); - } - } else { - snprintf(fullName, PATH_MAX, "%s", logFileName); - } - - tsLogObj.slowHandle = taosLogBuffNew(LOG_SLOW_BUF_SIZE); - if (tsLogObj.slowHandle == NULL) return terrno; - - (void)taosUmaskFile(0); - tsLogObj.slowHandle->pFile = taosOpenFile(fullName, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND); - if (tsLogObj.slowHandle->pFile == NULL) { - printf("\nfailed to open slow log file:%s, reason:%s\n", fullName, strerror(errno)); - return TAOS_SYSTEM_ERROR(errno); - } - - return 0; -} - -int32_t taosInitLog(const char *logName, int32_t maxFiles) { - if (atomic_val_compare_exchange_8(&tsLogInited, 0, 1) != 0) return 0; - osUpdate(); - - char fullName[PATH_MAX] = {0}; +static void getFullPathName(char* fullName, const char* logName){ if (strlen(tsLogDir) != 0) { char lastC = tsLogDir[strlen(tsLogDir) - 1]; if (lastC == '\\' || lastC == '/') { @@ -207,13 +160,40 @@ int32_t taosInitLog(const char *logName, int32_t maxFiles) { } else { snprintf(fullName, PATH_MAX, "%s", logName); } - taosUpdateDaylight(); +} - tsLogObj.logHandle = taosLogBuffNew(LOG_DEFAULT_BUF_SIZE); - if (tsLogObj.logHandle == NULL) return terrno; - TAOS_CHECK_RETURN(taosOpenLogFile(fullName, maxFiles)); +int32_t taosInitSlowLog() { + char logFileName[64] = {0}; +#ifdef CUS_PROMPT + snprintf(logFileName, 64, "%sSlowLog", CUS_PROMPT); +#else + snprintf(logFileName, 64, "taosSlowLog"); +#endif - TAOS_CHECK_RETURN(taosInitSlowLog()); + getFullPathName(tsLogObj.slowLogName, logFileName); + + tsLogObj.slowHandle = taosLogBuffNew(LOG_SLOW_BUF_SIZE); + if (tsLogObj.slowHandle == NULL) return terrno; + + (void)taosUmaskFile(0); + tsLogObj.slowHandle->pFile = taosOpenFile(tsLogObj.slowLogName, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND); + if (tsLogObj.slowHandle->pFile == NULL) { + printf("\nfailed to open slow log file:%s, reason:%s\n", tsLogObj.slowLogName, strerror(errno)); + return TAOS_SYSTEM_ERROR(errno); + } + + backUpSlowLog(); + return 0; +} + +int32_t taosInitLog(const char *logName, int32_t maxFiles, bool tsc) { + if (atomic_val_compare_exchange_8(&tsLogInited, 0, 1) != 0) return 0; + osUpdate(); + + TAOS_CHECK_RETURN(taosInitNormalLog(logName, maxFiles)); + if (tsc){ + TAOS_CHECK_RETURN(taosInitSlowLog()); + } TAOS_CHECK_RETURN(taosStartLog()); return 0; } @@ -383,6 +363,49 @@ static int32_t taosOpenNewLogFile() { return 0; } +static void taosOpenNewSlowLogFile(char* day) { + int32_t code = 0; + TdFilePtr pOldFile = tsLogObj.slowHandle->pFile; + if (taosLockFile(pOldFile) != 0){ + return; + } + + TdFilePtr pFile = NULL; + char name[LOG_FILE_NAME_LEN + 64] = {0}; + (void)sprintf(name, "%s.%s", tsLogObj.slowLogName, day); + if (taosCheckExistFile(name)){ + pFile = taosOpenFile(tsLogObj.slowLogName, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND); + if (pFile == NULL) { + uError("open new log file fail! reason:%s, reuse lastlog", strerror(errno)); + goto END; + } + }else{ + if ((code = taosRenameFile(tsLogObj.slowLogName, name))) { + uError("failed to rename file:%s to %s since %s", tsLogObj.slowLogName, name, tstrerror(code)); + goto END; + } + + (void)taosUmaskFile(0); + + pFile = taosOpenFile(tsLogObj.slowLogName, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC); + if (pFile == NULL) { + uError("open new log file fail! reason:%s, reuse lastlog", strerror(errno)); + goto END; + } + (void)taosLSeekFile(pFile, 0, SEEK_SET); + } + + tsLogObj.slowHandle->pFile = pFile; + taosUnLockLogFile(pOldFile); + (void)taosCloseFile(&pOldFile); + tstrncpy(tsLogObj.slowLogDay, day, 64); + return; + +END: + taosUnLockLogFile(pOldFile); + +} + void taosResetLog() { // force create a new log file tsLogObj.lines = tsNumOfLogLines + 10; @@ -415,7 +438,8 @@ static bool taosCheckFileIsOpen(char *logFileName) { } } -static void taosGetLogFileName(char *fn) { +static void decideLogFileName(const char *fn, int32_t maxFileNum) { + tsLogObj.fileNum = maxFileNum; if (tsLogObj.fileNum > 1) { for (int32_t i = 0; i < tsLogObj.fileNum; i++) { char fileName[LOG_FILE_NAME_LEN]; @@ -438,32 +462,20 @@ static void taosGetLogFileName(char *fn) { } } -static int32_t taosOpenLogFile(char *fn, int32_t maxFileNum) { -#ifdef WINDOWS_STASH - /* - * always set maxFileNum to 1 - * means client log filename is unique in windows - */ - maxFileNum = 1; -#endif - +static void decideLogFileNameFlag(){ char name[LOG_FILE_NAME_LEN + 50] = "\0"; - int32_t logstat0_mtime, logstat1_mtime; + int32_t logstat0_mtime = 0; + int32_t logstat1_mtime = 0; + bool log0Exist = false; + bool log1Exist = false; - tsLogObj.fileNum = maxFileNum; - taosGetLogFileName(fn); - - if (strlen(fn) < LOG_FILE_NAME_LEN + 50 - 2) { - strcpy(name, fn); + if (strlen(tsLogObj.logName) < LOG_FILE_NAME_LEN + 50 - 2) { + strcpy(name, tsLogObj.logName); strcat(name, ".0"); + log0Exist = taosStatFile(name, NULL, &logstat0_mtime, NULL) >= 0; + name[strlen(name) - 1] = '1'; + log1Exist = taosStatFile(name, NULL, &logstat1_mtime, NULL) >= 0; } - bool log0Exist = taosStatFile(name, NULL, &logstat0_mtime, NULL) >= 0; - - if (strlen(fn) < LOG_FILE_NAME_LEN + 50 - 2) { - strcpy(name, fn); - strcat(name, ".1"); - } - bool log1Exist = taosStatFile(name, NULL, &logstat1_mtime, NULL) >= 0; // if none of the log files exist, open 0, if both exists, open the old one if (!log0Exist && !log1Exist) { @@ -475,16 +487,37 @@ static int32_t taosOpenLogFile(char *fn, int32_t maxFileNum) { } else { tsLogObj.flag = (logstat0_mtime > logstat1_mtime) ? 0 : 1; } +} - char fileName[LOG_FILE_NAME_LEN + 50] = "\0"; - sprintf(fileName, "%s.%d", tsLogObj.logName, tsLogObj.flag); +static void processLogFileName(const char* logName , int32_t maxFileNum){ + char fullName[PATH_MAX] = {0}; + getFullPathName(fullName, logName); + decideLogFileName(fullName, maxFileNum); + decideLogFileNameFlag(); +} + +static int32_t taosInitNormalLog(const char *logName, int32_t maxFileNum) { +#ifdef WINDOWS_STASH + /* + * always set maxFileNum to 1 + * means client log filename is unique in windows + */ + maxFileNum = 1; +#endif + + processLogFileName(logName, maxFileNum); + + char name[LOG_FILE_NAME_LEN + 50] = "\0"; + sprintf(name, "%s.%d", tsLogObj.logName, tsLogObj.flag); (void)taosThreadMutexInit(&tsLogObj.logMutex, NULL); (void)taosUmaskFile(0); - tsLogObj.logHandle->pFile = taosOpenFile(fileName, TD_FILE_CREATE | TD_FILE_WRITE); + tsLogObj.logHandle = taosLogBuffNew(LOG_DEFAULT_BUF_SIZE); + if (tsLogObj.logHandle == NULL) return terrno; + tsLogObj.logHandle->pFile = taosOpenFile(name, TD_FILE_CREATE | TD_FILE_WRITE); if (tsLogObj.logHandle->pFile == NULL) { - printf("\nfailed to open log file:%s, reason:%s\n", fileName, strerror(errno)); + printf("\nfailed to open log file:%s, reason:%s\n", name, strerror(errno)); return TAOS_SYSTEM_ERROR(errno); } (void)taosLockLogFile(tsLogObj.logHandle->pFile); @@ -492,7 +525,7 @@ static int32_t taosOpenLogFile(char *fn, int32_t maxFileNum) { // only an estimate for number of lines int64_t filesize = 0; if (taosFStatFile(tsLogObj.logHandle->pFile, &filesize, NULL) < 0) { - printf("\nfailed to fstat log file:%s, reason:%s\n", fileName, strerror(errno)); + printf("\nfailed to fstat log file:%s, reason:%s\n", name, strerror(errno)); return TAOS_SYSTEM_ERROR(errno); } tsLogObj.lines = (int32_t)(filesize / 60); @@ -610,6 +643,21 @@ void taosPrintLongString(const char *flags, ELogLevel level, int32_t dflag, cons taosMemoryFree(buffer); } +static void backUpSlowLog(){ + char buf[64] = {0}; + time_t t = taosTime(NULL); + struct tm tmInfo; + if (taosLocalTime(&t, &tmInfo, buf) != NULL) { + (void)strftime(buf, sizeof(buf), "%Y-%m-%d", &tmInfo); + } +// (void)taosThreadMutexLock(&tsLogObj.logMutex); +// if (strlen(tsLogObj.slowLogDay) == 0) { +// tstrncpy(tsLogObj.slowLogDay, buf, 64); +// }else if (strcmp(tsLogObj.slowLogDay, buf) != 0) { + (void)taosOpenNewSlowLogFile(buf); +// } + (void)taosThreadMutexUnlock(&tsLogObj.logMutex); +} void taosPrintSlowLog(const char *format, ...) { if (!osLogSpaceAvailable()) return; @@ -834,7 +882,11 @@ static void *taosAsyncOutputLog(void *param) { int32_t writeInterval = 0; while (1) { - writeInterval = TMIN(pLogBuf->writeInterval, pSlowBuf->writeInterval); + if (pSlowBuf) { + writeInterval = TMIN(pLogBuf->writeInterval, pSlowBuf->writeInterval); + } else { + writeInterval = pLogBuf->writeInterval; + } count += writeInterval; updateCron++; taosMsleep(writeInterval); @@ -845,17 +897,12 @@ static void *taosAsyncOutputLog(void *param) { // Polling the buffer taosWriteLog(pLogBuf); - taosWriteLog(pSlowBuf); + if (pSlowBuf) taosWriteLog(pSlowBuf); - if (updateCron >= 3600 * 24 * 40 / 2) { - taosUpdateDaylight(); - updateCron = 0; - } - - if (pLogBuf->stop || pSlowBuf->stop) { + if (pLogBuf->stop || (pSlowBuf && pSlowBuf->stop)) { pLogBuf->lastDuration = LOG_MAX_WAIT_MSEC; taosWriteLog(pLogBuf); - taosWriteLog(pSlowBuf); + if (pSlowBuf) taosWriteLog(pSlowBuf); break; } } diff --git a/source/util/test/trefTest.c b/source/util/test/trefTest.c index 46656d7776..7abdef7c78 100644 --- a/source/util/test/trefTest.c +++ b/source/util/test/trefTest.c @@ -154,7 +154,7 @@ int main(int argc, char *argv[]) { } } - taosInitLog("tref.log", 10); + taosInitLog("tref.log", 10, false); SRefSpace *pSpaceList = (SRefSpace *)taosMemoryCalloc(sizeof(SRefSpace), threads); TdThread *pThreadList = (TdThread *)taosMemoryCalloc(sizeof(TdThread), threads); From f5e35549d714d1810a15a86985a148b9ac95b644 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Fri, 2 Aug 2024 17:10:21 +0800 Subject: [PATCH 02/39] feat:[TS-3718]save slow log file one day one file --- source/util/src/tlog.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index 0f217c439f..199e030d82 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -182,7 +182,6 @@ int32_t taosInitSlowLog() { return TAOS_SYSTEM_ERROR(errno); } - backUpSlowLog(); return 0; } @@ -403,7 +402,6 @@ static void taosOpenNewSlowLogFile(char* day) { END: taosUnLockLogFile(pOldFile); - } void taosResetLog() { @@ -643,19 +641,19 @@ void taosPrintLongString(const char *flags, ELogLevel level, int32_t dflag, cons taosMemoryFree(buffer); } -static void backUpSlowLog(){ +static void checkSwitchSlowLogFile(){ char buf[64] = {0}; time_t t = taosTime(NULL); struct tm tmInfo; if (taosLocalTime(&t, &tmInfo, buf) != NULL) { (void)strftime(buf, sizeof(buf), "%Y-%m-%d", &tmInfo); } -// (void)taosThreadMutexLock(&tsLogObj.logMutex); -// if (strlen(tsLogObj.slowLogDay) == 0) { -// tstrncpy(tsLogObj.slowLogDay, buf, 64); -// }else if (strcmp(tsLogObj.slowLogDay, buf) != 0) { - (void)taosOpenNewSlowLogFile(buf); -// } + (void)taosThreadMutexLock(&tsLogObj.logMutex); + if (strlen(tsLogObj.slowLogDay) == 0) { + tstrncpy(tsLogObj.slowLogDay, buf, 64); + }else if (strcmp(tsLogObj.slowLogDay, buf) != 0) { + taosOpenNewSlowLogFile(buf); + } (void)taosThreadMutexUnlock(&tsLogObj.logMutex); } void taosPrintSlowLog(const char *format, ...) { @@ -683,6 +681,8 @@ void taosPrintSlowLog(const char *format, ...) { (void)taosWriteFile(tsLogObj.slowHandle->pFile, buffer, len); } + checkSwitchSlowLogFile(); + taosMemoryFree(buffer); } From 0b663dbcccc09722d8f626367a34528b11d4c941 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Fri, 2 Aug 2024 17:11:32 +0800 Subject: [PATCH 03/39] feat:[TS-3718]save slow log file one day one file --- source/util/src/tlog.c | 1 - 1 file changed, 1 deletion(-) diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index 199e030d82..34893817b5 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -135,7 +135,6 @@ static int32_t taosPushLogBuffer(SLogBuff *pLogBuf, const char *msg, int32_t m static SLogBuff *taosLogBuffNew(int32_t bufSize); static void taosCloseLogByFd(TdFilePtr pFile); static int32_t taosInitNormalLog(const char *fn, int32_t maxFileNum); -static void backUpSlowLog(); static int32_t taosStartLog() { TdThreadAttr threadAttr; (void)taosThreadAttrInit(&threadAttr); From 51ad50c60362f5adf28bd3c8f428657019133bf2 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Fri, 2 Aug 2024 17:16:13 +0800 Subject: [PATCH 04/39] feat:[TS-3718]save slow log file one day one file --- source/util/src/tlog.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index 34893817b5..bb924a5c90 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -573,7 +573,7 @@ static inline int32_t taosBuildLogHead(char *buffer, const char *flags) { } static inline void taosPrintLogImp(ELogLevel level, int32_t dflag, const char *buffer, int32_t len) { - if ((dflag & DEBUG_FILE) && tsLogObj.logHandle && tsLogObj.logHandle->pFile != NULL && osLogSpaceAvailable()) { + if ((dflag & DEBUG_FILE) && tsLogObj.logHandle && tsLogObj.logHandle->pFile != NULL && osLogSpaceSufficient()) { taosUpdateLogNums(level); if (tsAsyncLog) { (void)taosPushLogBuffer(tsLogObj.logHandle, buffer, len); @@ -621,7 +621,7 @@ void taosPrintLog(const char *flags, ELogLevel level, int32_t dflag, const char } void taosPrintLongString(const char *flags, ELogLevel level, int32_t dflag, const char *format, ...) { - if (!osLogSpaceAvailable()) return; + if (!osLogSpaceSufficient()) return; if (!(dflag & DEBUG_FILE) && !(dflag & DEBUG_SCREEN)) return; char *buffer = taosMemoryMalloc(LOG_MAX_LINE_DUMP_BUFFER_SIZE); @@ -656,7 +656,7 @@ static void checkSwitchSlowLogFile(){ (void)taosThreadMutexUnlock(&tsLogObj.logMutex); } void taosPrintSlowLog(const char *format, ...) { - if (!osLogSpaceAvailable()) return; + if (!osLogSpaceSufficient()) return; char *buffer = taosMemoryMalloc(LOG_MAX_LINE_DUMP_BUFFER_SIZE); int32_t len = taosBuildLogHead(buffer, ""); From 4d7c54d1a5233960df45304488ed10dd99bf5f3b Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Fri, 2 Aug 2024 19:10:50 +0800 Subject: [PATCH 05/39] feat:[TS-3718]save slow log file one day one file --- source/util/src/tlog.c | 117 ++++++++++++++++++----------------------- 1 file changed, 50 insertions(+), 67 deletions(-) diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index bb924a5c90..5eacf561aa 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -26,7 +26,8 @@ #define LOG_MAX_LINE_DUMP_SIZE (1024 * 1024) #define LOG_MAX_LINE_DUMP_BUFFER_SIZE (LOG_MAX_LINE_DUMP_SIZE + 128) -#define LOG_FILE_NAME_LEN 300 +#define LOG_FILE_DAY_LEN 64 + #define LOG_DEFAULT_BUF_SIZE (20 * 1024 * 1024) // 20MB #define LOG_SLOW_BUF_SIZE (10 * 1024 * 1024) // 10MB @@ -69,10 +70,10 @@ typedef struct { int32_t openInProgress; int32_t openInProgressSlowLog; int64_t lastKeepFileSec; - char slowLogDay[64]; + char slowLogDay[LOG_FILE_DAY_LEN]; pid_t pid; - char logName[LOG_FILE_NAME_LEN]; - char slowLogName[LOG_FILE_NAME_LEN]; + char logName[PATH_MAX]; + char slowLogName[PATH_MAX]; SLogBuff *logHandle; SLogBuff *slowHandle; TdThreadMutex logMutex; @@ -145,6 +146,13 @@ static int32_t taosStartLog() { return 0; } +static void getDay(char* buf){ + time_t t = taosTime(NULL); + struct tm tmInfo; + if (taosLocalTime(&t, &tmInfo, buf) != NULL) { + (void)strftime(buf, sizeof(buf), "%Y-%m-%d", &tmInfo); + } +} static void getFullPathName(char* fullName, const char* logName){ if (strlen(tsLogDir) != 0) { char lastC = tsLogDir[strlen(tsLogDir) - 1]; @@ -164,20 +172,25 @@ static void getFullPathName(char* fullName, const char* logName){ int32_t taosInitSlowLog() { char logFileName[64] = {0}; #ifdef CUS_PROMPT - snprintf(logFileName, 64, "%sSlowLog", CUS_PROMPT); + (void)snprintf(logFileName, 64, "%sSlowLog", CUS_PROMPT); #else - snprintf(logFileName, 64, "taosSlowLog"); + (void)snprintf(logFileName, 64, "taosSlowLog"); #endif getFullPathName(tsLogObj.slowLogName, logFileName); + char name[PATH_MAX + LOG_FILE_DAY_LEN] = {0}; + char day[LOG_FILE_DAY_LEN] = {0}; + getDay(day); + (void)snprintf(name, PATH_MAX + LOG_FILE_DAY_LEN, "%s.%s", tsLogObj.slowLogName, day); + tsLogObj.slowHandle = taosLogBuffNew(LOG_SLOW_BUF_SIZE); if (tsLogObj.slowHandle == NULL) return terrno; (void)taosUmaskFile(0); - tsLogObj.slowHandle->pFile = taosOpenFile(tsLogObj.slowLogName, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND); + tsLogObj.slowHandle->pFile = taosOpenFile(name, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND); if (tsLogObj.slowHandle->pFile == NULL) { - printf("\nfailed to open slow log file:%s, reason:%s\n", tsLogObj.slowLogName, strerror(errno)); + printf("\nfailed to open slow log file:%s, reason:%s\n", name, strerror(errno)); return TAOS_SYSTEM_ERROR(errno); } @@ -266,7 +279,7 @@ static void taosReserveOldLog(char *oldName, char *keepName) { } else { fileSec = ++tsLogObj.lastKeepFileSec; } - snprintf(keepName, LOG_FILE_NAME_LEN + 20, "%s.%" PRId64, tsLogObj.logName, fileSec); + snprintf(keepName, PATH_MAX + 20, "%s.%" PRId64, tsLogObj.logName, fileSec); if ((code = taosRenameFile(oldName, keepName))) { keepName[0] = 0; uError("failed to rename file:%s to %s since %s", oldName, keepName, tstrerror(code)); @@ -275,8 +288,8 @@ static void taosReserveOldLog(char *oldName, char *keepName) { static void taosKeepOldLog(char *oldName) { if (oldName[0] != 0) { - char compressFileName[LOG_FILE_NAME_LEN + 20]; - snprintf(compressFileName, LOG_FILE_NAME_LEN + 20, "%s.gz", oldName); + char compressFileName[PATH_MAX + 20]; + snprintf(compressFileName, PATH_MAX + 20, "%s.gz", oldName); if (taosCompressFile(oldName, compressFileName) == 0) { (void)taosRemoveFile(oldName); } @@ -288,15 +301,15 @@ static void taosKeepOldLog(char *oldName) { } typedef struct { TdFilePtr pOldFile; - char keepName[LOG_FILE_NAME_LEN + 20]; + char keepName[PATH_MAX + 20]; } OldFileKeeper; static OldFileKeeper *taosOpenNewFile() { - char keepName[LOG_FILE_NAME_LEN + 20]; + char keepName[PATH_MAX + 20]; sprintf(keepName, "%s.%d", tsLogObj.logName, tsLogObj.flag); tsLogObj.flag ^= 1; tsLogObj.lines = 0; - char name[LOG_FILE_NAME_LEN + 20]; + char name[PATH_MAX + 20]; sprintf(name, "%s.%d", tsLogObj.logName, tsLogObj.flag); (void)taosUmaskFile(0); @@ -362,45 +375,19 @@ static int32_t taosOpenNewLogFile() { } static void taosOpenNewSlowLogFile(char* day) { - int32_t code = 0; - TdFilePtr pOldFile = tsLogObj.slowHandle->pFile; - if (taosLockFile(pOldFile) != 0){ + TdFilePtr pFile = NULL; + char name[PATH_MAX + LOG_FILE_DAY_LEN] = {0}; + (void)snprintf(name, PATH_MAX + LOG_FILE_DAY_LEN, "%s.%s", tsLogObj.slowLogName, day); + pFile = taosOpenFile(name, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND); + if (pFile == NULL) { + uError("open new log file fail! reason:%s, reuse lastlog", strerror(errno)); return; } - TdFilePtr pFile = NULL; - char name[LOG_FILE_NAME_LEN + 64] = {0}; - (void)sprintf(name, "%s.%s", tsLogObj.slowLogName, day); - if (taosCheckExistFile(name)){ - pFile = taosOpenFile(tsLogObj.slowLogName, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND); - if (pFile == NULL) { - uError("open new log file fail! reason:%s, reuse lastlog", strerror(errno)); - goto END; - } - }else{ - if ((code = taosRenameFile(tsLogObj.slowLogName, name))) { - uError("failed to rename file:%s to %s since %s", tsLogObj.slowLogName, name, tstrerror(code)); - goto END; - } - - (void)taosUmaskFile(0); - - pFile = taosOpenFile(tsLogObj.slowLogName, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC); - if (pFile == NULL) { - uError("open new log file fail! reason:%s, reuse lastlog", strerror(errno)); - goto END; - } - (void)taosLSeekFile(pFile, 0, SEEK_SET); - } - + TdFilePtr pOldFile = tsLogObj.slowHandle->pFile; tsLogObj.slowHandle->pFile = pFile; - taosUnLockLogFile(pOldFile); (void)taosCloseFile(&pOldFile); - tstrncpy(tsLogObj.slowLogDay, day, 64); - return; - -END: - taosUnLockLogFile(pOldFile); + tstrncpy(tsLogObj.slowLogDay, day, LOG_FILE_DAY_LEN); } void taosResetLog() { @@ -439,34 +426,34 @@ static void decideLogFileName(const char *fn, int32_t maxFileNum) { tsLogObj.fileNum = maxFileNum; if (tsLogObj.fileNum > 1) { for (int32_t i = 0; i < tsLogObj.fileNum; i++) { - char fileName[LOG_FILE_NAME_LEN]; + char fileName[PATH_MAX + 10]; - snprintf(fileName, LOG_FILE_NAME_LEN, "%s%d.0", fn, i); + (void)snprintf(fileName, PATH_MAX + 10, "%s%d.0", fn, i); bool file1open = taosCheckFileIsOpen(fileName); - snprintf(fileName, LOG_FILE_NAME_LEN, "%s%d.1", fn, i); + (void)snprintf(fileName, PATH_MAX + 10, "%s%d.1", fn, i); bool file2open = taosCheckFileIsOpen(fileName); if (!file1open && !file2open) { - snprintf(tsLogObj.logName, LOG_FILE_NAME_LEN, "%s%d", fn, i); + (void)snprintf(tsLogObj.logName, PATH_MAX, "%s%d", fn, i); return; } } } - if (strlen(fn) < LOG_FILE_NAME_LEN) { + if (strlen(fn) < PATH_MAX) { strcpy(tsLogObj.logName, fn); } } static void decideLogFileNameFlag(){ - char name[LOG_FILE_NAME_LEN + 50] = "\0"; + char name[PATH_MAX + 50] = "\0"; int32_t logstat0_mtime = 0; int32_t logstat1_mtime = 0; bool log0Exist = false; bool log1Exist = false; - if (strlen(tsLogObj.logName) < LOG_FILE_NAME_LEN + 50 - 2) { + if (strlen(tsLogObj.logName) < PATH_MAX + 50 - 2) { strcpy(name, tsLogObj.logName); strcat(name, ".0"); log0Exist = taosStatFile(name, NULL, &logstat0_mtime, NULL) >= 0; @@ -504,7 +491,7 @@ static int32_t taosInitNormalLog(const char *logName, int32_t maxFileNum) { processLogFileName(logName, maxFileNum); - char name[LOG_FILE_NAME_LEN + 50] = "\0"; + char name[PATH_MAX + 50] = "\0"; sprintf(name, "%s.%d", tsLogObj.logName, tsLogObj.flag); (void)taosThreadMutexInit(&tsLogObj.logMutex, NULL); @@ -641,17 +628,13 @@ void taosPrintLongString(const char *flags, ELogLevel level, int32_t dflag, cons } static void checkSwitchSlowLogFile(){ - char buf[64] = {0}; - time_t t = taosTime(NULL); - struct tm tmInfo; - if (taosLocalTime(&t, &tmInfo, buf) != NULL) { - (void)strftime(buf, sizeof(buf), "%Y-%m-%d", &tmInfo); - } + char day[LOG_FILE_DAY_LEN] = {0}; + getDay(day); (void)taosThreadMutexLock(&tsLogObj.logMutex); if (strlen(tsLogObj.slowLogDay) == 0) { - tstrncpy(tsLogObj.slowLogDay, buf, 64); - }else if (strcmp(tsLogObj.slowLogDay, buf) != 0) { - taosOpenNewSlowLogFile(buf); + tstrncpy(tsLogObj.slowLogDay, day, LOG_FILE_DAY_LEN); + }else if (strcmp(tsLogObj.slowLogDay, day) != 0) { + taosOpenNewSlowLogFile(day); } (void)taosThreadMutexUnlock(&tsLogObj.logMutex); } @@ -674,14 +657,14 @@ void taosPrintSlowLog(const char *format, ...) { (void)atomic_add_fetch_64(&tsNumOfSlowLogs, 1); + checkSwitchSlowLogFile(); + if (tsAsyncLog) { (void)taosPushLogBuffer(tsLogObj.slowHandle, buffer, len); } else { (void)taosWriteFile(tsLogObj.slowHandle->pFile, buffer, len); } - checkSwitchSlowLogFile(); - taosMemoryFree(buffer); } From 9c2adc59bb1ab2638b46915e127717233dd4a56e Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Mon, 5 Aug 2024 15:52:22 +0800 Subject: [PATCH 06/39] feat:[TS-3718]save slow log file one day one file --- source/util/src/tlog.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index 5eacf561aa..c6c869fb23 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -150,7 +150,7 @@ static void getDay(char* buf){ time_t t = taosTime(NULL); struct tm tmInfo; if (taosLocalTime(&t, &tmInfo, buf) != NULL) { - (void)strftime(buf, sizeof(buf), "%Y-%m-%d", &tmInfo); + (void)strftime(buf, LOG_FILE_DAY_LEN, "%Y-%m-%d", &tmInfo); } } static void getFullPathName(char* fullName, const char* logName){ From 7f8df84106cff9a175e95a86e01211acb7aafbfa Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Tue, 6 Aug 2024 18:02:01 +0800 Subject: [PATCH 07/39] feat:[TS-3718]save slow log file one day one file --- source/util/src/tlog.c | 54 +++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index c6c869fb23..26e5a0039d 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -68,9 +68,8 @@ typedef struct { int32_t lines; int32_t flag; int32_t openInProgress; - int32_t openInProgressSlowLog; int64_t lastKeepFileSec; - char slowLogDay[LOG_FILE_DAY_LEN]; + int64_t timestampToday; pid_t pid; char logName[PATH_MAX]; char slowLogName[PATH_MAX]; @@ -136,6 +135,7 @@ static int32_t taosPushLogBuffer(SLogBuff *pLogBuf, const char *msg, int32_t m static SLogBuff *taosLogBuffNew(int32_t bufSize); static void taosCloseLogByFd(TdFilePtr pFile); static int32_t taosInitNormalLog(const char *fn, int32_t maxFileNum); +static void taosWriteLog(SLogBuff *pLogBuf); static int32_t taosStartLog() { TdThreadAttr threadAttr; (void)taosThreadAttrInit(&threadAttr); @@ -153,6 +153,18 @@ static void getDay(char* buf){ (void)strftime(buf, LOG_FILE_DAY_LEN, "%Y-%m-%d", &tmInfo); } } + +static int64_t getTimestampToday() { + time_t t = taosTime(NULL); + struct tm tm; + (void) taosLocalTime(&t, &tm, NULL); + tm.tm_hour = 0; + tm.tm_min = 0; + tm.tm_sec = 0; + + return (int64_t)taosMktime(&tm); +} + static void getFullPathName(char* fullName, const char* logName){ if (strlen(tsLogDir) != 0) { char lastC = tsLogDir[strlen(tsLogDir) - 1]; @@ -184,6 +196,7 @@ int32_t taosInitSlowLog() { getDay(day); (void)snprintf(name, PATH_MAX + LOG_FILE_DAY_LEN, "%s.%s", tsLogObj.slowLogName, day); + tsLogObj.timestampToday = getTimestampToday(); tsLogObj.slowHandle = taosLogBuffNew(LOG_SLOW_BUF_SIZE); if (tsLogObj.slowHandle == NULL) return terrno; @@ -374,20 +387,31 @@ static int32_t taosOpenNewLogFile() { return 0; } -static void taosOpenNewSlowLogFile(char* day) { +static void taosOpenNewSlowLogFile(int64_t today) { + (void)taosThreadMutexLock(&tsLogObj.logMutex); + if (tsLogObj.timestampToday == today) { + uInfo("timestampToday is already equal to today, no need to open new slow log file"); + (void)taosThreadMutexUnlock(&tsLogObj.logMutex); + return; + } + taosWriteLog(tsLogObj.slowHandle); + char day[LOG_FILE_DAY_LEN] = {0}; + getDay(day); TdFilePtr pFile = NULL; char name[PATH_MAX + LOG_FILE_DAY_LEN] = {0}; (void)snprintf(name, PATH_MAX + LOG_FILE_DAY_LEN, "%s.%s", tsLogObj.slowLogName, day); pFile = taosOpenFile(name, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND); if (pFile == NULL) { uError("open new log file fail! reason:%s, reuse lastlog", strerror(errno)); + (void)taosThreadMutexUnlock(&tsLogObj.logMutex); return; } TdFilePtr pOldFile = tsLogObj.slowHandle->pFile; tsLogObj.slowHandle->pFile = pFile; (void)taosCloseFile(&pOldFile); - tstrncpy(tsLogObj.slowLogDay, day, LOG_FILE_DAY_LEN); + tsLogObj.timestampToday = today; + (void)taosThreadMutexUnlock(&tsLogObj.logMutex); } void taosResetLog() { @@ -456,9 +480,9 @@ static void decideLogFileNameFlag(){ if (strlen(tsLogObj.logName) < PATH_MAX + 50 - 2) { strcpy(name, tsLogObj.logName); strcat(name, ".0"); - log0Exist = taosStatFile(name, NULL, &logstat0_mtime, NULL) >= 0; + log0Exist = taosStatFile(name, NULL, &logstat0_mtime, NULL) == 0; name[strlen(name) - 1] = '1'; - log1Exist = taosStatFile(name, NULL, &logstat1_mtime, NULL) >= 0; + log1Exist = taosStatFile(name, NULL, &logstat1_mtime, NULL) == 0; } // if none of the log files exist, open 0, if both exists, open the old one @@ -627,20 +651,14 @@ void taosPrintLongString(const char *flags, ELogLevel level, int32_t dflag, cons taosMemoryFree(buffer); } -static void checkSwitchSlowLogFile(){ - char day[LOG_FILE_DAY_LEN] = {0}; - getDay(day); - (void)taosThreadMutexLock(&tsLogObj.logMutex); - if (strlen(tsLogObj.slowLogDay) == 0) { - tstrncpy(tsLogObj.slowLogDay, day, LOG_FILE_DAY_LEN); - }else if (strcmp(tsLogObj.slowLogDay, day) != 0) { - taosOpenNewSlowLogFile(day); - } - (void)taosThreadMutexUnlock(&tsLogObj.logMutex); -} void taosPrintSlowLog(const char *format, ...) { if (!osLogSpaceSufficient()) return; + int64_t today = getTimestampToday(); + if (today != tsLogObj.timestampToday){ + taosOpenNewSlowLogFile(today); + } + char *buffer = taosMemoryMalloc(LOG_MAX_LINE_DUMP_BUFFER_SIZE); int32_t len = taosBuildLogHead(buffer, ""); @@ -657,8 +675,6 @@ void taosPrintSlowLog(const char *format, ...) { (void)atomic_add_fetch_64(&tsNumOfSlowLogs, 1); - checkSwitchSlowLogFile(); - if (tsAsyncLog) { (void)taosPushLogBuffer(tsLogObj.slowHandle, buffer, len); } else { From bce6f40abbc7390d34048556411e114d66a65af2 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Thu, 8 Aug 2024 17:29:18 +0800 Subject: [PATCH 08/39] feat:[TS-3718]add lock to avoid write slow at the same time --- source/util/src/tlog.c | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index 26e5a0039d..411e251706 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -61,6 +61,7 @@ typedef struct { TdThreadMutex buffMutex; int32_t writeInterval; int32_t lastDuration; + int32_t lock; } SLogBuff; typedef struct { @@ -136,6 +137,8 @@ static SLogBuff *taosLogBuffNew(int32_t bufSize); static void taosCloseLogByFd(TdFilePtr pFile); static int32_t taosInitNormalLog(const char *fn, int32_t maxFileNum); static void taosWriteLog(SLogBuff *pLogBuf); +static void taosWriteSlowLog(SLogBuff *pLogBuf); + static int32_t taosStartLog() { TdThreadAttr threadAttr; (void)taosThreadAttrInit(&threadAttr); @@ -387,14 +390,24 @@ static int32_t taosOpenNewLogFile() { return 0; } -static void taosOpenNewSlowLogFile(int64_t today) { +static void taosOpenNewSlowLogFile() { (void)taosThreadMutexLock(&tsLogObj.logMutex); - if (tsLogObj.timestampToday == today) { + int64_t delta = taosGetTimestampSec() - tsLogObj.timestampToday; + if (delta >= 0 && delta < 86400) { uInfo("timestampToday is already equal to today, no need to open new slow log file"); (void)taosThreadMutexUnlock(&tsLogObj.logMutex); return; } + + for (int32_t i = 1; atomic_val_compare_exchange_32(&tsLogObj.slowHandle->lock, 0, 1) == 1; ++i) { + if (i % 1000 == 0) { + (void)sched_yield(); + } + } + tsLogObj.slowHandle->lastDuration = LOG_MAX_WAIT_MSEC; // force write taosWriteLog(tsLogObj.slowHandle); + atomic_store_32(&tsLogObj.slowHandle->lock, 0); + char day[LOG_FILE_DAY_LEN] = {0}; getDay(day); TdFilePtr pFile = NULL; @@ -410,7 +423,7 @@ static void taosOpenNewSlowLogFile(int64_t today) { TdFilePtr pOldFile = tsLogObj.slowHandle->pFile; tsLogObj.slowHandle->pFile = pFile; (void)taosCloseFile(&pOldFile); - tsLogObj.timestampToday = today; + tsLogObj.timestampToday = getTimestampToday(); (void)taosThreadMutexUnlock(&tsLogObj.logMutex); } @@ -654,9 +667,9 @@ void taosPrintLongString(const char *flags, ELogLevel level, int32_t dflag, cons void taosPrintSlowLog(const char *format, ...) { if (!osLogSpaceSufficient()) return; - int64_t today = getTimestampToday(); - if (today != tsLogObj.timestampToday){ - taosOpenNewSlowLogFile(today); + int64_t delta = taosGetTimestampSec() - tsLogObj.timestampToday; + if (delta >= 86400 || delta < 0) { + taosOpenNewSlowLogFile(); } char *buffer = taosMemoryMalloc(LOG_MAX_LINE_DUMP_BUFFER_SIZE); @@ -729,7 +742,7 @@ static SLogBuff *taosLogBuffNew(int32_t bufSize) { pLogBuf->minBuffSize = bufSize / 10; pLogBuf->stop = 0; pLogBuf->writeInterval = LOG_DEFAULT_INTERVAL; - + pLogBuf->lock = 0; if (taosThreadMutexInit(&LOG_BUF_MUTEX(pLogBuf), NULL) < 0) goto _err; // tsem_init(&(pLogBuf->buffNotEmpty), 0, 0); @@ -811,6 +824,12 @@ static int32_t taosGetLogRemainSize(SLogBuff *pLogBuf, int32_t start, int32_t en return rSize >= 0 ? rSize : LOG_BUF_SIZE(pLogBuf) + rSize; } +static void taosWriteSlowLog(SLogBuff *pLogBuf){ + int32_t lock = atomic_val_compare_exchange_32(&pLogBuf->lock, 0, 1); + if (lock == 1) return; + taosWriteLog(pLogBuf); + atomic_store_32(&pLogBuf->lock, 0); +} static void taosWriteLog(SLogBuff *pLogBuf) { int32_t start = LOG_BUF_START(pLogBuf); int32_t end = LOG_BUF_END(pLogBuf); @@ -895,12 +914,12 @@ static void *taosAsyncOutputLog(void *param) { // Polling the buffer taosWriteLog(pLogBuf); - if (pSlowBuf) taosWriteLog(pSlowBuf); + if (pSlowBuf) taosWriteSlowLog(pSlowBuf); if (pLogBuf->stop || (pSlowBuf && pSlowBuf->stop)) { pLogBuf->lastDuration = LOG_MAX_WAIT_MSEC; taosWriteLog(pLogBuf); - if (pSlowBuf) taosWriteLog(pSlowBuf); + if (pSlowBuf) taosWriteSlowLog(pSlowBuf); break; } } From 882cfb7deb545cf937a1f9ed7f843caab0832461 Mon Sep 17 00:00:00 2001 From: wangjiaming0909 <604227650@qq.com> Date: Mon, 12 Aug 2024 08:53:27 +0800 Subject: [PATCH 09/39] 1. lock not released when return 2. fix tsma err msg 3. fix fill prev wrong results --- include/util/taoserror.h | 1 + source/dnode/vnode/src/tsdb/tsdbReadUtil.c | 3 ++- source/libs/executor/src/tfill.c | 5 +++-- source/libs/parser/src/parTranslater.c | 2 +- source/util/src/terror.c | 1 + tests/system-test/2-query/tsma.py | 8 ++++---- tests/system-test/2-query/tsma2.py | 2 +- 7 files changed, 13 insertions(+), 9 deletions(-) diff --git a/include/util/taoserror.h b/include/util/taoserror.h index b091d870ec..b8098cc96e 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -914,6 +914,7 @@ int32_t taosGetErrSize(); #define TSDB_CODE_TSMA_UNSUPPORTED_FUNC TAOS_DEF_ERROR_CODE(0, 0x3109) #define TSDB_CODE_TSMA_MUST_BE_DROPPED TAOS_DEF_ERROR_CODE(0, 0x3110) #define TSDB_CODE_TSMA_NAME_TOO_LONG TAOS_DEF_ERROR_CODE(0, 0x3111) +#define TSDB_CODE_TSMA_INVALID_RECURSIVE_INTERVAL TAOS_DEF_ERROR_CODE(0, 0x3112) //rsma #define TSDB_CODE_RSMA_INVALID_ENV TAOS_DEF_ERROR_CODE(0, 0x3150) diff --git a/source/dnode/vnode/src/tsdb/tsdbReadUtil.c b/source/dnode/vnode/src/tsdb/tsdbReadUtil.c index 4dabffc10a..1fba59c9b2 100644 --- a/source/dnode/vnode/src/tsdb/tsdbReadUtil.c +++ b/source/dnode/vnode/src/tsdb/tsdbReadUtil.c @@ -957,6 +957,7 @@ int32_t loadMemTombData(SArray** ppMemDelData, STbData* pMemTbData, STbData* piM if (p->version <= ver) { void* px = taosArrayPush(pMemDelData, p); if (px == NULL) { + taosRUnLockLatch(&pMemTbData->lock); return terrno; } } @@ -1430,4 +1431,4 @@ bool overlapWithDelSkylineWithoutVer(STableBlockScanInfo* pBlockScanInfo, const return doCheckDatablockOverlapWithoutVersion(pBlockScanInfo, pRecord, index); } -} \ No newline at end of file +} diff --git a/source/libs/executor/src/tfill.c b/source/libs/executor/src/tfill.c index 3158c85987..e6c2ea1b45 100644 --- a/source/libs/executor/src/tfill.c +++ b/source/libs/executor/src/tfill.c @@ -331,8 +331,9 @@ static void copyCurrentRowIntoBuf(SFillInfo* pFillInfo, int32_t rowIndex, SRowVa for (int32_t i = 0; i < pFillInfo->numOfCols; ++i) { int32_t type = pFillInfo->pFillCol[i].pExpr->pExpr->nodeType; if (type == QUERY_NODE_COLUMN || type == QUERY_NODE_OPERATOR || type == QUERY_NODE_FUNCTION) { - if (!pFillInfo->pFillCol[i].notFillCol && pFillInfo->type != TSDB_FILL_NEXT) { - continue; + if (!pFillInfo->pFillCol[i].notFillCol) { + if (FILL_IS_ASC_FILL(pFillInfo) && pFillInfo->type != TSDB_FILL_NEXT) continue; + if (!FILL_IS_ASC_FILL(pFillInfo) && pFillInfo->type != TSDB_FILL_PREV) continue; } int32_t srcSlotId = GET_DEST_SLOT_ID(&pFillInfo->pFillCol[i]); diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 03c3355920..c86e2d4874 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -11864,7 +11864,7 @@ static int32_t buildCreateTSMAReq(STranslateContext* pCxt, SCreateTSMAStmt* pStm if (checkRecursiveTsmaInterval(pRecursiveTsma->interval, pRecursiveTsma->unit, pInterval->datum.i, pInterval->unit, pDbInfo.precision, true)) { } else { - code = TSDB_CODE_TSMA_INVALID_INTERVAL; + code = TSDB_CODE_TSMA_INVALID_RECURSIVE_INTERVAL; } } } diff --git a/source/util/src/terror.c b/source/util/src/terror.c index 7b9d48eb58..2f4414cd03 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -759,6 +759,7 @@ TAOS_DEFINE_ERROR(TSDB_CODE_TSMA_INVALID_FUNC_PARAM, "Invalid tsma func p TAOS_DEFINE_ERROR(TSDB_CODE_TSMA_UNSUPPORTED_FUNC, "Tsma func not supported") TAOS_DEFINE_ERROR(TSDB_CODE_TSMA_MUST_BE_DROPPED, "Tsma must be dropped first") TAOS_DEFINE_ERROR(TSDB_CODE_TSMA_NAME_TOO_LONG, "Tsma name too long") +TAOS_DEFINE_ERROR(TSDB_CODE_TSMA_INVALID_RECURSIVE_INTERVAL,"Invalid recursive tsma interval") //rsma TAOS_DEFINE_ERROR(TSDB_CODE_RSMA_INVALID_ENV, "Invalid rsma env") diff --git a/tests/system-test/2-query/tsma.py b/tests/system-test/2-query/tsma.py index 2bf908e250..1b52302503 100644 --- a/tests/system-test/2-query/tsma.py +++ b/tests/system-test/2-query/tsma.py @@ -1476,18 +1476,18 @@ class TDTestCase: tdSql.error(sql, -2147473920) # syntax error sql = 'create recursive tsma tsma2 on test.tsma1 interval(1m)' - tdSql.error(sql, -2147471097) # invalid tsma interval + tdSql.error(sql, -2147471086) # invalid tsma interval sql = 'create recursive tsma tsma2 on test.tsma1 interval(7m)' - tdSql.error(sql, -2147471097) # invalid tsma interval + tdSql.error(sql, -2147471086) # invalid tsma interval sql = 'create recursive tsma tsma2 on test.tsma1 interval(11m)' - tdSql.error(sql, -2147471097) # invalid tsma interval + tdSql.error(sql, -2147471086) # invalid tsma interval self.create_recursive_tsma('tsma1', 'tsma2', 'test', '20m', 'meters') sql = 'create recursive tsma tsma3 on test.tsma2 interval(30m)' - tdSql.error(sql, -2147471097) # invalid tsma interval + tdSql.error(sql, -2147471086) # invalid tsma interval self.create_recursive_tsma('tsma2', 'tsma3', 'test', '40m', 'meters') diff --git a/tests/system-test/2-query/tsma2.py b/tests/system-test/2-query/tsma2.py index 404fb3f00c..f53b578012 100644 --- a/tests/system-test/2-query/tsma2.py +++ b/tests/system-test/2-query/tsma2.py @@ -852,7 +852,7 @@ class TDTestCase: ] tdSql.execute('use db') for (i, ri, ret) in examples: - self.test_create_recursive_tsma_interval(db, tb, func, i, ri, ret, -2147471097) + self.test_create_recursive_tsma_interval(db, tb, func, i, ri, ret, -2147471086) self.create_tsma('tsma1', db, tb, func, '1h') self.create_recursive_tsma('tsma1', 'tsma2', db, '1n', tb, func) From 89244490ee4e6226ef599418eb92c4ad1a5f33e3 Mon Sep 17 00:00:00 2001 From: sheyanjie-qq <249478495@qq.com> Date: Mon, 12 Aug 2024 11:18:25 +0800 Subject: [PATCH 10/39] improve java log --- .../taosdata/example/ConsumerLoopFull.java | 8 +++++-- .../com/taosdata/example/JdbcCreatDBDemo.java | 12 +++------- .../taosdata/example/JdbcInsertDataDemo.java | 2 +- .../com/taosdata/example/JdbcReqIdDemo.java | 22 ++++++++++++++----- .../example/ParameterBindingBasicDemo.java | 4 +++- .../example/WSParameterBindingBasicDemo.java | 6 ++--- .../taosdata/example/WsConsumerLoopFull.java | 9 ++++++-- 7 files changed, 39 insertions(+), 24 deletions(-) diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java index 25b3958069..f2fa345f47 100644 --- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java +++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java @@ -37,7 +37,9 @@ public class ConsumerLoopFull { config.setProperty("value.deserializer.encoding", "UTF-8"); try { - return new TaosConsumer<>(config); + TaosConsumer consumer= new TaosConsumer<>(config); + System.out.println("Create consumer successfully, host: " + config.getProperty("bootstrap.servers") + ", groupId: " + config.getProperty("group.id") + ", clientId: " + config.getProperty("client.id")); + return consumer; } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info System.out.println("Failed to create websocket consumer, host : " + config.getProperty("bootstrap.servers") + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); @@ -98,7 +100,6 @@ public class ConsumerLoopFull { consumer.seekToBeginning(assignment); System.out.println("assignment seek to beginning successfully"); - System.out.println("beginning assignment: " + JSON.toJSONString(assignment)); } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info System.out.println("seek example failed; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); @@ -127,6 +128,7 @@ public class ConsumerLoopFull { if (!records.isEmpty()) { // after processing the data, commit the offset manually consumer.commitSync(); + System.out.println("commit offset manually successfully."); } } } catch (SQLException ex) { @@ -147,6 +149,7 @@ public class ConsumerLoopFull { try { // unsubscribe the consumer consumer.unsubscribe(); + System.out.println("unsubscribe consumer successfully."); } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info System.out.println("Failed to unsubscribe consumer. ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); @@ -158,6 +161,7 @@ public class ConsumerLoopFull { finally { // close the consumer consumer.close(); + System.out.println("consumer closed successfully."); } // ANCHOR_END: unsubscribe_data_code_piece } diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcCreatDBDemo.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcCreatDBDemo.java index 93531bd80f..ca50c4ca77 100644 --- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcCreatDBDemo.java +++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcCreatDBDemo.java @@ -30,17 +30,11 @@ public class JdbcCreatDBDemo { // create database int rowsAffected = stmt.executeUpdate("CREATE DATABASE IF NOT EXISTS power"); // you can check rowsAffected here - assert rowsAffected == 0; - - // use database - rowsAffected = stmt.executeUpdate("USE power"); - // you can check rowsAffected here - assert rowsAffected == 0; - + System.out.println("Create database power successfully, rowsAffected: " + rowsAffected); // create table - rowsAffected = stmt.executeUpdate("CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))"); + rowsAffected = stmt.executeUpdate("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))"); // you can check rowsAffected here - assert rowsAffected == 0; + System.out.println("Create stable power.meters successfully, rowsAffected: " + rowsAffected); } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcInsertDataDemo.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcInsertDataDemo.java index cb8755788d..7e60c10bf7 100644 --- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcInsertDataDemo.java +++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcInsertDataDemo.java @@ -39,7 +39,7 @@ public class JdbcInsertDataDemo { "(NOW + 1a, 10.30000, 218, 0.25000) "; int affectedRows = stmt.executeUpdate(insertQuery); // you can check affectedRows here - System.out.println("inserted into " + affectedRows + " rows to power.meters successfully."); + System.out.println("Successfully inserted " + affectedRows + " rows to power.meters."); } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info System.out.println("Failed to insert data to power.meters, url:" + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcReqIdDemo.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcReqIdDemo.java index 0f2e522ce0..7dd99c58dc 100644 --- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcReqIdDemo.java +++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcReqIdDemo.java @@ -25,22 +25,32 @@ public class JdbcReqIdDemo { System.out.println("get connection starting..."); // ANCHOR: with_reqid + long reqId = 3L; try (Connection connection = DriverManager.getConnection(jdbcUrl, properties); // Create a statement that allows specifying a request ID AbstractStatement aStmt = (AbstractStatement) connection.createStatement()) { - try (ResultSet rs = aStmt.executeQuery("SELECT ts, current, location FROM power.meters limit 1", 3L)) { - while (rs.next()) { - Timestamp timestamp = rs.getTimestamp(1); - System.out.println("timestamp = " + timestamp); + try (ResultSet resultSet = aStmt.executeQuery("SELECT ts, current, location FROM power.meters limit 1", reqId)) { + Timestamp ts; + float current; + String location; + while (resultSet.next()) { + ts = resultSet.getTimestamp(1); + current = resultSet.getFloat(2); + // we recommend using the column name to get the value + location = resultSet.getString("location"); + + // you can check data here + System.out.printf("ts: %s, current: %f, location: %s %n", ts, current, location); + } } } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info - System.out.println("Failed to execute sql with reqId, url:" + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); + System.out.println("Failed to execute sql with reqId: " + reqId + ", url:" + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); throw ex; } catch (Exception ex){ - System.out.println("Failed to execute sql with reqId, url:" + jdbcUrl + "; ErrMessage: " + ex.getMessage()); + System.out.println("Failed to execute sql with reqId: " + reqId + ", url:" + jdbcUrl + "; ErrMessage: " + ex.getMessage()); throw ex; } // ANCHOR_END: with_reqid diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ParameterBindingBasicDemo.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ParameterBindingBasicDemo.java index 6a9fc5e44f..ab93e324ea 100644 --- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ParameterBindingBasicDemo.java +++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ParameterBindingBasicDemo.java @@ -24,7 +24,7 @@ public class ParameterBindingBasicDemo { init(conn); - String sql = "INSERT INTO ? USING meters TAGS(?,?) VALUES (?,?,?,?)"; + String sql = "INSERT INTO ? USING power.meters TAGS(?,?) VALUES (?,?,?,?)"; try (TSDBPreparedStatement pstmt = conn.prepareStatement(sql).unwrap(TSDBPreparedStatement.class)) { @@ -65,6 +65,8 @@ public class ParameterBindingBasicDemo { } // execute column pstmt.columnDataExecuteBatch(); + // you can check exeResult here + System.out.println("Successfully inserted " + (numOfSubTable * numOfRow) + " rows to power.meters."); } } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WSParameterBindingBasicDemo.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WSParameterBindingBasicDemo.java index 2f58b3798a..29013014b9 100644 --- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WSParameterBindingBasicDemo.java +++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WSParameterBindingBasicDemo.java @@ -21,7 +21,7 @@ public class WSParameterBindingBasicDemo { try (Connection conn = DriverManager.getConnection(jdbcUrl, "root", "taosdata")) { init(conn); - String sql = "INSERT INTO ? USING meters TAGS(?,?) VALUES (?,?,?,?)"; + String sql = "INSERT INTO ? USING power.meters TAGS(?,?) VALUES (?,?,?,?)"; try (TSWSPreparedStatement pstmt = conn.prepareStatement(sql).unwrap(TSWSPreparedStatement.class)) { @@ -44,7 +44,7 @@ public class WSParameterBindingBasicDemo { } int [] exeResult = pstmt.executeBatch(); // you can check exeResult here - System.out.println("insert " + exeResult.length + " rows."); + System.out.println("Successfully inserted " + exeResult.length + " rows to power.meters."); } } } catch (SQLException ex) { @@ -61,7 +61,7 @@ public class WSParameterBindingBasicDemo { try (Statement stmt = conn.createStatement()) { stmt.execute("CREATE DATABASE IF NOT EXISTS power"); stmt.execute("USE power"); - stmt.execute("CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))"); + stmt.execute("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))"); } } } diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java index 55368b3e56..3ac51f61fc 100644 --- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java +++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java @@ -10,6 +10,7 @@ import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; // ANCHOR: consumer_demo public class WsConsumerLoopFull { @@ -34,7 +35,9 @@ public class WsConsumerLoopFull { config.setProperty("value.deserializer.encoding", "UTF-8"); try { - return new TaosConsumer<>(config); + TaosConsumer consumer= new TaosConsumer<>(config); + System.out.println("Create consumer successfully, host: " + config.getProperty("bootstrap.servers") + ", groupId: " + config.getProperty("group.id") + ", clientId: " + config.getProperty("client.id")); + return consumer; } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info System.out.println("Failed to create websocket consumer, host : " + config.getProperty("bootstrap.servers") + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); @@ -95,7 +98,6 @@ public class WsConsumerLoopFull { consumer.seekToBeginning(assignment); System.out.println("assignment seek to beginning successfully"); - System.out.println("beginning assignment: " + JSON.toJSONString(assignment)); } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info System.out.println("seek example failed; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); @@ -124,6 +126,7 @@ public class WsConsumerLoopFull { if (!records.isEmpty()) { // after processing the data, commit the offset manually consumer.commitSync(); + System.out.println("commit offset manually successfully."); } } } catch (SQLException ex) { @@ -144,6 +147,7 @@ public class WsConsumerLoopFull { try { // unsubscribe the consumer consumer.unsubscribe(); + System.out.println("unsubscribe consumer successfully."); } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info System.out.println("Failed to unsubscribe consumer. ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); @@ -155,6 +159,7 @@ public class WsConsumerLoopFull { finally { // close the consumer consumer.close(); + System.out.println("consumer closed successfully."); } // ANCHOR_END: unsubscribe_data_code_piece } From 7a0f2b135e9d323a18a31d78d793f2635f6ab70a Mon Sep 17 00:00:00 2001 From: menshibin Date: Mon, 12 Aug 2024 14:13:31 +0800 Subject: [PATCH 11/39] perfect python and nodejs example --- .../node/websocketexample/line_example.js | 4 +- .../node/websocketexample/sql_example.js | 65 ++++++++++--------- .../node/websocketexample/stmt_example.js | 9 +-- .../node/websocketexample/tmq_example.js | 25 ++++--- .../node/websocketexample/tmq_seek_example.js | 1 + docs/examples/python/connect_example.py | 2 +- docs/examples/python/connect_rest_example.py | 2 +- docs/examples/python/create_db_native.py | 18 ++--- docs/examples/python/create_db_rest.py | 14 ++-- docs/examples/python/create_db_ws.py | 19 ++---- docs/examples/python/insert_native.py | 15 +++-- docs/examples/python/insert_rest.py | 8 +-- docs/examples/python/insert_ws.py | 11 ++-- docs/examples/python/query_native.py | 17 ++--- docs/examples/python/query_rest.py | 12 ++-- docs/examples/python/query_ws.py | 14 ++-- docs/examples/python/reqid_native.py | 19 +++--- docs/examples/python/reqid_rest.py | 13 ++-- docs/examples/python/reqid_ws.py | 14 ++-- docs/examples/python/schemaless_native.py | 10 +-- docs/examples/python/schemaless_ws.py | 29 ++++++--- docs/examples/python/stmt_native.py | 11 ++-- docs/examples/python/stmt_ws.py | 6 +- docs/examples/python/tmq_native.py | 20 ++++-- docs/examples/python/tmq_websocket_example.py | 21 ++++-- 25 files changed, 200 insertions(+), 179 deletions(-) diff --git a/docs/examples/node/websocketexample/line_example.js b/docs/examples/node/websocketexample/line_example.js index 96550c4d39..7e76db2677 100644 --- a/docs/examples/node/websocketexample/line_example.js +++ b/docs/examples/node/websocketexample/line_example.js @@ -16,6 +16,7 @@ async function createConnect() { } async function test() { + let dsn = 'ws://localhost:6041' let wsSql = null; let wsRows = null; let ttl = 0; @@ -24,9 +25,10 @@ async function test() { await wsSql.schemalessInsert(influxdbData, taos.SchemalessProto.InfluxDBLineProtocol, taos.Precision.MILLI_SECONDS, ttl); await wsSql.schemalessInsert(telnetData, taos.SchemalessProto.OpenTSDBTelnetLineProtocol, taos.Precision.MILLI_SECONDS, ttl); await wsSql.schemalessInsert(jsonData, taos.SchemalessProto.OpenTSDBJsonFormatProtocol, taos.Precision.SECONDS, ttl); + console.log("Inserted data with schemaless successfully.") } catch (err) { - console.error("Failed to insert data with schemaless, ErrCode:" + err.code + "; ErrMessage: " + err.message); + console.error("Failed to insert data with schemaless, url:"+ dsn +", ErrCode:" + err.code + "; ErrMessage: " + err.message); } finally { if (wsRows) { diff --git a/docs/examples/node/websocketexample/sql_example.js b/docs/examples/node/websocketexample/sql_example.js index 1e4d547770..b594b9967b 100644 --- a/docs/examples/node/websocketexample/sql_example.js +++ b/docs/examples/node/websocketexample/sql_example.js @@ -1,34 +1,39 @@ // ANCHOR: createConnect const taos = require("@tdengine/websocket"); +let dsn = 'ws://localhost:6041'; async function createConnect() { - let dsn = 'ws://localhost:6041'; - let conf = new taos.WSConfig(dsn); - conf.setUser('root'); - conf.setPwd('taosdata'); - conf.setDb('power'); - return await taos.sqlConnect(conf); + + try { + let conf = new taos.WSConfig(dsn); + conf.setUser('root'); + conf.setPwd('taosdata'); + conf.setDb('power'); + conn = await taos.sqlConnect(conf); + console.log("Connected to " + dsn + " successfully."); + return conn; + } catch (err) { + console.log("Failed to connect to " + dns + "; ErrCode:" + err.code + "; ErrMessage: " + err.message); + throw err; + } + } // ANCHOR_END: createConnect // ANCHOR: create_db_and_table -async function createDbAndTable(wsSql) { +async function createDbAndTable() { let wsSql = null; try { wsSql = await createConnect(); - await wsSql.exec('CREATE DATABASE IF NOT EXISTS POWER ' + - 'KEEP 3650 DURATION 10 BUFFER 16 WAL_LEVEL 1;'); + await wsSql.exec('CREATE DATABASE IF NOT EXISTS power'); - await wsSql.exec('USE power'); - - await wsSql.exec('CREATE STABLE IF NOT EXISTS meters ' + + await wsSql.exec('CREATE STABLE IF NOT EXISTS power.meters ' + '(_ts timestamp, current float, voltage int, phase float) ' + 'TAGS (location binary(64), groupId int);'); - taosResult = await wsSql.exec('describe meters'); - console.log(taosResult); + console.log("Create stable power.meters successfully"); } catch (err) { - console.error("Failed to create db and table, ErrCode:" + err.code + "; ErrMessage: " + err.message); + console.error("Failed to create db and table, url:" + dns + "; ErrCode:" + err.code + "; ErrMessage: " + err.message); } finally { if (wsSql) { await wsSql.close(); @@ -39,8 +44,8 @@ async function createDbAndTable(wsSql) { // ANCHOR_END: create_db_and_table // ANCHOR: insertData -async function insertData(wsSql) { - let wsSql = null; +async function insertData() { + let wsSql = null try { wsSql = await createConnect(); let insertQuery = "INSERT INTO " + @@ -53,9 +58,9 @@ async function insertData(wsSql) { "VALUES " + "(NOW + 1a, 10.30000, 218, 0.25000) "; taosResult = await wsSql.exec(insertQuery); - console.log(taosResult); + console.log("Successfully inserted " + taosResult.getAffectRows() + " rows to power.meters."); } catch (err) { - console.error("Failed to insert data to power.meters, ErrCode:" + err.code + "; ErrMessage: " + err.message); + console.error("Failed to insert data to power.meters, url:" + dsn + "; ErrCode:" + err.code + "; ErrMessage: " + err.message); } finally { if (wsSql) { await wsSql.close(); @@ -71,15 +76,13 @@ async function queryData() { try { wsSql = await createConnect(); wsRows = await wsSql.query('SELECT ts, current, location FROM power.meters limit 100'); - let meta = wsRows.getMeta(); - console.log("wsRow:meta:=>", meta); while (await wsRows.next()) { - let result = wsRows.getData(); - console.log('queryRes.Scan().then=>', result); + let row = wsRows.getData(); + console.log('ts: ' + row[0] + ', current: ' + row[1] + ', location: ' + row[2]); } } catch (err) { - console.error("Failed to query data from power.meters," + err.code + "; ErrMessage: " + err.message); + console.error("Failed to query data from power.meters, url:" + dsn + " ; ErrCode:" + err.code + "; ErrMessage: " + err.message); } finally { if (wsRows) { @@ -93,22 +96,20 @@ async function queryData() { // ANCHOR_END: queryData // ANCHOR: sqlWithReqid -async function sqlWithReqid(wsSql) { - +async function sqlWithReqid() { let wsRows = null; let wsSql = null; + let reqId = 1; try { wsSql = await createConnect(); - wsRows = await wsSql.query('SELECT ts, current, location FROM power.meters limit 100', 1); - let meta = wsRows.getMeta(); - console.log("wsRow:meta:=>", meta); + wsRows = await wsSql.query('SELECT ts, current, location FROM power.meters limit 100', reqId); while (await wsRows.next()) { - let result = wsRows.getData(); - console.log('queryRes.Scan().then=>', result); + let row = wsRows.getData(); + console.log('ts: ' + row[0] + ', current: ' + row[1] + ', location: ' + row[2]); } } catch (err) { - console.error("Failed to execute sql with reqId," + err.code + "; ErrMessage: " + err.message); + console.error("Failed to execute sql with reqId: " + reqId + "," + err.code + "; ErrMessage: " + err.message); } finally { if (wsRows) { diff --git a/docs/examples/node/websocketexample/stmt_example.js b/docs/examples/node/websocketexample/stmt_example.js index 369fcdb319..d159de6ef8 100644 --- a/docs/examples/node/websocketexample/stmt_example.js +++ b/docs/examples/node/websocketexample/stmt_example.js @@ -3,7 +3,8 @@ const taos = require("@tdengine/websocket"); let db = 'power'; let stable = 'meters'; let numOfSubTable = 10; -let numOfRow = 10; +let numOfRow = 10; +let dsn = 'ws://localhost:6041' function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); @@ -11,7 +12,7 @@ function getRandomInt(min, max) { } async function prepare() { - let dsn = 'ws://localhost:6041' + let conf = new taos.WSConfig(dsn); conf.setUser('root') conf.setPwd('taosdata') @@ -54,11 +55,11 @@ async function prepare() { await stmt.bind(bindParams); await stmt.batch(); await stmt.exec(); - console.log(`d_bind_${i} insert ` + stmt.getLastAffected() + " rows."); + console.log("Successfully inserted " + stmt.getLastAffected() + " to power.meters."); } } catch (err) { - console.error("Failed to insert to table meters using stmt, ErrCode:" + err.code + "; ErrMessage: " + err.message); + console.error("Failed to insert to table meters using stmt, url:" + dsn + "ErrCode:" + err.code + "; ErrMessage: " + err.message); } finally { if (stmt) { diff --git a/docs/examples/node/websocketexample/tmq_example.js b/docs/examples/node/websocketexample/tmq_example.js index 1842035de5..0c9e00446b 100644 --- a/docs/examples/node/websocketexample/tmq_example.js +++ b/docs/examples/node/websocketexample/tmq_example.js @@ -1,23 +1,28 @@ const taos = require("@tdengine/websocket"); +// ANCHOR: create_consumer const db = 'power'; const stable = 'meters'; const topics = ['power_meters_topic']; - -// ANCHOR: create_consumer +const url = 'ws://localhost:6041'; async function createConsumer() { + + let groupId = "group1"; + let clientId = "1"; let configMap = new Map([ - [taos.TMQConstants.GROUP_ID, "group1"], - [taos.TMQConstants.CLIENT_ID, 'client1'], + [taos.TMQConstants.GROUP_ID, groupId], + [taos.TMQConstants.CLIENT_ID, clientId], [taos.TMQConstants.CONNECT_USER, "root"], [taos.TMQConstants.CONNECT_PASS, "taosdata"], [taos.TMQConstants.AUTO_OFFSET_RESET, "latest"], - [taos.TMQConstants.WS_URL, 'ws://localhost:6041'], + [taos.TMQConstants.WS_URL, url], [taos.TMQConstants.ENABLE_AUTO_COMMIT, 'true'], [taos.TMQConstants.AUTO_COMMIT_INTERVAL_MS, '1000'] ]); try { - return await taos.tmqConnect(configMap); + conn = await taos.tmqConnect(configMap); + console.log(`Create consumer successfully, host: ${url}, groupId: ${groupId}, clientId: ${clientId}`) + return conn; }catch (err) { console.log("Failed to create websocket consumer, ErrCode:" + err.code + "; ErrMessage: " + err.message); throw err; @@ -31,7 +36,7 @@ async function prepare() { conf.setUser('root'); conf.setPwd('taosdata'); conf.setDb('power'); - const createDB = `CREATE DATABASE IF NOT EXISTS POWER ${db} KEEP 3650 DURATION 10 BUFFER 16 WAL_LEVEL 1;`; + const createDB = `CREATE DATABASE IF NOT EXISTS ${db}`; const createStable = `CREATE STABLE IF NOT EXISTS ${db}.${stable} (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int);`; let wsSql = await taos.sqlConnect(conf); @@ -45,7 +50,7 @@ async function prepare() { for (let i = 0; i < 10; i++) { await wsSql.exec(`INSERT INTO d1001 USING ${stable} (location, groupId) TAGS ("California.SanFrancisco", 3) VALUES (NOW, ${10 + i}, ${200 + i}, ${0.32 + i})`); } - wsSql.Close(); + wsSql.close(); } async function subscribe(consumer) { @@ -55,9 +60,10 @@ async function subscribe(consumer) { for (let i = 0; i < 50; i++) { let res = await consumer.poll(100); for (let [key, value] of res) { - console.log(key, value); + console.log(`data: ${key} ${value}`); } consumer.commit(); + console.log("commit offset manually successfully."); } } catch (err) { console.error("Failed to poll data; err.code, ErrCode:" + err.code + "; ErrMessage: " + err.message); @@ -74,6 +80,7 @@ async function test() { let consumer = await createConsumer() await subscribe(consumer) await consumer.unsubscribe(); + console.log("unsubscribe consumer successfully."); } catch (err) { console.error("Failed to unsubscribe consume, ErrCode:" + err.code + "; ErrMessage: " + err.message); diff --git a/docs/examples/node/websocketexample/tmq_seek_example.js b/docs/examples/node/websocketexample/tmq_seek_example.js index 593867f999..b5477cfeaa 100644 --- a/docs/examples/node/websocketexample/tmq_seek_example.js +++ b/docs/examples/node/websocketexample/tmq_seek_example.js @@ -84,6 +84,7 @@ async function test() { } await consumer.seekToBeginning(assignment); + console.log("assignment seek to beginning successfully"); assignment = await consumer.assignment(); for (let i in assignment) { console.log("seek after:", assignment[i]); diff --git a/docs/examples/python/connect_example.py b/docs/examples/python/connect_example.py index 095696f9a5..82698d8e7b 100644 --- a/docs/examples/python/connect_example.py +++ b/docs/examples/python/connect_example.py @@ -14,7 +14,7 @@ def create_connection(): ) print(f"Connected to {host}:{port} successfully."); except Exception as err: - print(f"Failed to connect to {host}:{port} ; Err:{err}") + print(f"Failed to connect to {host}:{port} ; err:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/connect_rest_example.py b/docs/examples/python/connect_rest_example.py index a9e5a00970..b10f9e7cf0 100644 --- a/docs/examples/python/connect_rest_example.py +++ b/docs/examples/python/connect_rest_example.py @@ -12,7 +12,7 @@ def create_connection(): print(f"Connected to {url} successfully."); except Exception as err: - print(f"Failed to connect to {url} ; Err:{err}") + print(f"Failed to connect to {url} ; err:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/create_db_native.py b/docs/examples/python/create_db_native.py index df29c730b5..94f0726269 100644 --- a/docs/examples/python/create_db_native.py +++ b/docs/examples/python/create_db_native.py @@ -9,26 +9,18 @@ try: user="root", password="taosdata") - db = "power" # create database - rowsAffected = conn.execute(f"CREATE DATABASE IF NOT EXISTS {db}") - assert rowsAffected == 0 - - # change database. same as execute "USE db" - conn.select_db(db) + rowsAffected = conn.execute(f"CREATE DATABASE IF NOT EXISTS power") + print(f"Create database power successfully, rowsAffected: {rowsAffected}"); # create super table rowsAffected = conn.execute( - "CREATE TABLE IF NOT EXISTS `meters` (`ts` TIMESTAMP, `current` FLOAT, `voltage` INT, `phase` FLOAT) TAGS (`groupid` INT, `location` BINARY(16))" + "CREATE TABLE IF NOT EXISTS power.meters (`ts` TIMESTAMP, `current` FLOAT, `voltage` INT, `phase` FLOAT) TAGS (`groupid` INT, `location` BINARY(16))" ) - assert rowsAffected == 0 - - # create table - rowsAffected = conn.execute("CREATE TABLE IF NOT EXISTS `d0` USING `meters` (groupid, location) TAGS(0, 'Los Angles')") - assert rowsAffected == 0 + print(f"Create stable power.meters successfully, rowsAffected: {rowsAffected}"); except Exception as err: - print(f"Failed to create db and table, db addrr:{host}:{port} err:{err}") + print(f"Failed to create db and table, db addr:{host}:{port} ; err:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/create_db_rest.py b/docs/examples/python/create_db_rest.py index a976d86aa0..d866f7aa1c 100644 --- a/docs/examples/python/create_db_rest.py +++ b/docs/examples/python/create_db_rest.py @@ -8,22 +8,18 @@ try: password="taosdata", timeout=30) - db = "power" # create database - rowsAffected = conn.execute(f"CREATE DATABASE IF NOT EXISTS {db}") - assert rowsAffected == 0 + rowsAffected = conn.execute(f"CREATE DATABASE IF NOT EXISTS power") + print(f"Create database power successfully, rowsAffected: {rowsAffected}"); # create super table rowsAffected = conn.execute( - f"CREATE TABLE IF NOT EXISTS `{db}`.`meters` (`ts` TIMESTAMP, `current` FLOAT, `voltage` INT, `phase` FLOAT) TAGS (`groupid` INT, `location` BINARY(16))" + f"CREATE TABLE IF NOT EXISTS power.meters (`ts` TIMESTAMP, `current` FLOAT, `voltage` INT, `phase` FLOAT) TAGS (`groupid` INT, `location` BINARY(16))" ) - assert rowsAffected == 0 - # create table - rowsAffected = conn.execute(f"CREATE TABLE IF NOT EXISTS `{db}`.`d0` USING `{db}`.`meters` (groupid, location) TAGS(0, 'Los Angles')") - assert rowsAffected == 0 + print(f"Create stable power.meters successfully, rowsAffected: {rowsAffected}"); except Exception as err: - print(f"Failed to create db and table, url:{url} err:{err}") + print(f"Failed to create db and table, url:{url} ; err:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/create_db_ws.py b/docs/examples/python/create_db_ws.py index edebd8a646..1cc916e96d 100644 --- a/docs/examples/python/create_db_ws.py +++ b/docs/examples/python/create_db_ws.py @@ -9,27 +9,18 @@ try: host=host, port=port) - db = "power" # create database - rowsAffected = conn.execute(f"CREATE DATABASE IF NOT EXISTS {db}") - assert rowsAffected == 0 - - # change database. - rowsAffected = conn.execute(f"USE {db}") - assert rowsAffected == 0 + rowsAffected = conn.execute(f"CREATE DATABASE IF NOT EXISTS power") + print(f"Create database power successfully, rowsAffected: {rowsAffected}"); # create super table rowsAffected = conn.execute( - "CREATE TABLE IF NOT EXISTS `meters` (`ts` TIMESTAMP, `current` FLOAT, `voltage` INT, `phase` FLOAT) TAGS (`groupid` INT, `location` BINARY(16))" + "CREATE TABLE IF NOT EXISTS power.meters (`ts` TIMESTAMP, `current` FLOAT, `voltage` INT, `phase` FLOAT) TAGS (`groupid` INT, `location` BINARY(16))" ) - assert rowsAffected == 0 - - # create table - rowsAffected = conn.execute("CREATE TABLE IF NOT EXISTS `d0` USING `meters` (groupid, location) TAGS(0, 'Los Angles')") - assert rowsAffected == 0 + print(f"Create stable power.meters successfully, rowsAffected: {rowsAffected}"); except Exception as err: - print(f"Failed to create db and table, db addrr:{host}:{port} err:{err}") + print(f"Failed to create db and table, db addrr:{host}:{port} ; err:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/insert_native.py b/docs/examples/python/insert_native.py index ea8901f5b7..67a3807c0a 100644 --- a/docs/examples/python/insert_native.py +++ b/docs/examples/python/insert_native.py @@ -1,12 +1,13 @@ import taos conn = None - +host = "localhost" +port = 6030 try: - conn = taos.connect(user="root", - password="taosdata", - host="localhost", - port=6030) + conn = taos.connect(host=host, + port=port, + user="root", + password="taosdata") sql = """ INSERT INTO @@ -17,10 +18,10 @@ try: VALUES (NOW + 1a, 10.30000, 218, 0.25000) """ affectedRows = conn.execute(sql) - print(f"inserted into {affectedRows} rows to power.meters successfully.") + print(f"Successfully inserted {affectedRows} rows to power.meters.") except Exception as err: - print(err) + print(f"Failed to insert data to power.meters, db addr:{host}:{port} ; err:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/insert_rest.py b/docs/examples/python/insert_rest.py index c43a6a6d17..7f0881bd77 100644 --- a/docs/examples/python/insert_rest.py +++ b/docs/examples/python/insert_rest.py @@ -1,9 +1,9 @@ import taosrest conn = None - +url="http://localhost:6041" try: - conn = taosrest.connect(url="http://localhost:6041", + conn = taosrest.connect(url=url, user="root", password="taosdata", timeout=30) @@ -17,10 +17,10 @@ try: VALUES (NOW + 1a, 10.30000, 218, 0.25000) """ affectedRows = conn.execute(sql) - print(f"inserted into {affectedRows} rows to power.meters successfully.") + print(f"Successfully inserted {affectedRows} rows to power.meters.") except Exception as err: - print(err) + print(f"Failed to insert data to power.meters, url:{url} ; err:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/insert_ws.py b/docs/examples/python/insert_ws.py index d838e79670..254027b1a6 100644 --- a/docs/examples/python/insert_ws.py +++ b/docs/examples/python/insert_ws.py @@ -1,12 +1,13 @@ import taosws conn = None - +host="localhost" +port=6041 try: conn = taosws.connect(user="root", password="taosdata", - host="localhost", - port=6041) + host=host, + port=port) sql = """ INSERT INTO @@ -17,10 +18,10 @@ try: VALUES (NOW + 1a, 10.30000, 218, 0.25000) """ affectedRows = conn.execute(sql) - print(f"inserted into {affectedRows} rows to power.meters successfully.") + print(f"Successfully inserted {affectedRows} rows to power.meters.") except Exception as err: - print(err) + print(f"Failed to insert data to power.meters, db addr:{host}:{port} ; err:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/query_native.py b/docs/examples/python/query_native.py index da271010eb..08cc1462cb 100644 --- a/docs/examples/python/query_native.py +++ b/docs/examples/python/query_native.py @@ -1,26 +1,21 @@ import taos - +host="localhost" +port=6030 conn = None try: - conn = taos.connect(host="localhost", - port=6030, + conn = taos.connect(host=host, + port=port, user="root", password="taosdata") result = conn.query("SELECT ts, current, location FROM power.meters limit 100") - print(result) - # Get fields from result - fields = result.fields - for field in fields: - print(field) - # Get data from result as list of tuple data = result.fetch_all() for row in data: - print(row) + print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}") except Exception as err: - print(f"Failed to query data from power.meters, err:{err}") + print(f"Failed to query data from power.meters, db addr:{host}:{port} ; err:{err}") finally: if conn: conn.close() \ No newline at end of file diff --git a/docs/examples/python/query_rest.py b/docs/examples/python/query_rest.py index 79f756cbf9..02d3536f82 100644 --- a/docs/examples/python/query_rest.py +++ b/docs/examples/python/query_rest.py @@ -1,15 +1,17 @@ import taosrest client = None - +url="http://localhost:6041" try: - client = taosrest.RestClient(url="http://localhost:6041", + client = taosrest.RestClient(url=url, user="root", password="taosdata", timeout=30) - result = client.sql(f"SELECT ts, current, location FROM power.meters limit 100", 1) - print(result) + result = client.sql(f"SELECT ts, current, location FROM power.meters limit 100") + if result["data"]: + for row in result["data"]: + print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}") except Exception as err: - print(f"Failed to query data from power.meters, err:{err}") + print(f"Failed to query data from power.meters, url:{url} ; err:{err}") diff --git a/docs/examples/python/query_ws.py b/docs/examples/python/query_ws.py index d7480b5caa..e59977e0a1 100644 --- a/docs/examples/python/query_ws.py +++ b/docs/examples/python/query_ws.py @@ -1,22 +1,20 @@ import taosws conn = None - +host="localhost" +port=6041 try: conn = taosws.connect(user="root", password="taosdata", - host="localhost", - port=6041) + host=host, + port=port) result = conn.query("SELECT ts, current, location FROM power.meters limit 100") - num_of_fields = result.field_count - print(num_of_fields) - for row in result: - print(row) + print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}") except Exception as err: - print(f"Failed to query data from power.meters, err:{err}") + print(f"Failed to query data from power.meters, db addr:{host}:{port} ; err:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/reqid_native.py b/docs/examples/python/reqid_native.py index dbf4990461..1ece38e67e 100644 --- a/docs/examples/python/reqid_native.py +++ b/docs/examples/python/reqid_native.py @@ -1,25 +1,24 @@ import taos conn = None +reqId = 1 +host="localhost" +port=6030 try: - conn = taos.connect(host="localhost", - port=6030, + conn = taos.connect(host=host, + port=port, user="root", password="taosdata") - result = conn.query("SELECT ts, current, location FROM power.meters limit 100", 1) - # Get fields from result - fields = result.fields - for field in fields: - print(field) - + result = conn.query("SELECT ts, current, location FROM power.meters limit 100", reqId) # Get data from result as list of tuple data = result.fetch_all() for row in data: - print(row) + print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}") except Exception as err: - print(f"Failed to execute sql with reqId, err:{err}") + print(f"Failed to execute sql with reqId:{reqId}, db addr:{host}:{port} ; err:{err}") + finally: if conn: conn.close() \ No newline at end of file diff --git a/docs/examples/python/reqid_rest.py b/docs/examples/python/reqid_rest.py index 847b5a0f54..2a5379360f 100644 --- a/docs/examples/python/reqid_rest.py +++ b/docs/examples/python/reqid_rest.py @@ -1,15 +1,18 @@ import taosrest client = None - +url="http://localhost:6041" +reqId = 1 try: - client = taosrest.RestClient(url="http://localhost:6041", + client = taosrest.RestClient(url=url, user="root", password="taosdata", timeout=30) - result = client.sql(f"SELECT ts, current, location FROM power.meters limit 100", 1) - print(result) + result = client.sql(f"SELECT ts, current, location FROM power.meters limit 100", reqId) + if result["data"]: + for row in result["data"]: + print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}") except Exception as err: - print(f"Failed to execute sql with reqId, err:{err}") + print(f"Failed to execute sql with reqId:{reqId}, url:{url} ; err:{err}") diff --git a/docs/examples/python/reqid_ws.py b/docs/examples/python/reqid_ws.py index b541d5d9fd..f93b5ca1e8 100644 --- a/docs/examples/python/reqid_ws.py +++ b/docs/examples/python/reqid_ws.py @@ -1,22 +1,24 @@ import taosws conn = None - +reqId = 1 +host="localhost" +port=6041 try: conn = taosws.connect( user="root", password="taosdata", - host="localhost", - port=6041, + host=host, + port=port, ) result = conn.query_with_req_id("SELECT ts, current, location FROM power.meters limit 100", req_id=1) - + # Get data from result as list of tuple for row in result: - print(row) + print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}") except Exception as err: - print(f"Failed to execute sql with reqId, err:{err}") + print(f"Failed to execute sql with reqId:{reqId}, db addr:{host}:{port} ; err:{err}") finally: if conn: conn.close() \ No newline at end of file diff --git a/docs/examples/python/schemaless_native.py b/docs/examples/python/schemaless_native.py index 52a9d0e472..44d8edc475 100644 --- a/docs/examples/python/schemaless_native.py +++ b/docs/examples/python/schemaless_native.py @@ -9,13 +9,14 @@ telnetDemo = ["metric_telnet 1707095283260 4 host=host0 interface=eth0"] jsonDemo = [ '{"metric": "metric_json","timestamp": 1626846400,"value": 10.3, "tags": {"groupid": 2, "location": "California.SanFrancisco", "id": "d1001"}}' ] - +host = "localhost" +port = 6030 try: conn = taos.connect( - host="localhost", user="root", password="taosdata", - port=6030 + host=host, + port=port ) conn.execute("CREATE DATABASE IF NOT EXISTS power") @@ -31,8 +32,9 @@ try: conn.schemaless_insert( jsonDemo, taos.SmlProtocol.JSON_PROTOCOL, taos.SmlPrecision.MILLI_SECONDS ) + print("Inserted data with schemaless successfully."); except Exception as err: - print(f"Failed to insert data with schemaless, err:{err}") + print(f"Failed to insert data with schemaless, addr: {host}:{port} err:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/schemaless_ws.py b/docs/examples/python/schemaless_ws.py index c08b9b574d..08c0926c47 100644 --- a/docs/examples/python/schemaless_ws.py +++ b/docs/examples/python/schemaless_ws.py @@ -1,6 +1,5 @@ import taosws -db = "power" def prepare(): conn = None try: @@ -10,11 +9,12 @@ def prepare(): port=6041) # create database - rowsAffected = conn.execute(f"CREATE DATABASE IF NOT EXISTS {db}") + rowsAffected = conn.execute(f"CREATE DATABASE IF NOT EXISTS power") assert rowsAffected == 0 except Exception as err: print(f"Failed to create db and table, err:{err}") + raise err finally: if conn: conn.close() @@ -32,13 +32,14 @@ def schemaless_insert(): jsonDemo = [ '{"metric": "metric_json","timestamp": 1626846400,"value": 10.3, "tags": {"groupid": 2, "location": "California.SanFrancisco", "id": "d1001"}}' ] - + host = "localhost" + port = 6041 try: conn = taosws.connect(user="root", password="taosdata", - host="localhost", - port=6041, - database=db) + host=host, + port=port, + database='power') conn.schemaless_insert( lines = lineDemo, @@ -63,10 +64,18 @@ def schemaless_insert(): ttl=1, req_id=3, ) - + print("Inserted data with schemaless successfully."); except Exception as err: - print(f"Failed to insert data with schemaless, err:{err}") - + print(f"Failed to insert data with schemaless, addr: {host}:{port} err:{err}") + raise err + finally: if conn: - conn.close() \ No newline at end of file + conn.close() + +if __name__ == "__main__": + try: + prepare() + schemaless_insert + except Exception as err: + print(f"Failed to insert data with schemaless, err:{err}") \ No newline at end of file diff --git a/docs/examples/python/stmt_native.py b/docs/examples/python/stmt_native.py index 5b7bc3f487..138272204f 100644 --- a/docs/examples/python/stmt_native.py +++ b/docs/examples/python/stmt_native.py @@ -7,13 +7,14 @@ numOfRow = 10 conn = None stmt = None - +host="localhost", +port=6030, try: conn = taos.connect( - host="localhost", user="root", password="taosdata", - port=6030, + host=host, + port=port, ) conn.execute("CREATE DATABASE IF NOT EXISTS power") @@ -52,10 +53,10 @@ try: params[3].float(phases) stmt.bind_param_batch(params) stmt.execute() - print(f"stmt insert successfully.") + print(f"Successfully inserted to power.meters.") except Exception as err: - print(f"Failed to insert to table meters using stmt, error: {err}") + print(f"Failed to insert to table meters using stmt, addr:{host}:{port} ; err:{err}") finally: if stmt: stmt.close() diff --git a/docs/examples/python/stmt_ws.py b/docs/examples/python/stmt_ws.py index 9e5f34fc22..c391432300 100644 --- a/docs/examples/python/stmt_ws.py +++ b/docs/examples/python/stmt_ws.py @@ -8,6 +8,8 @@ numOfRow = 10 conn = None stmt = None +host="localhost" +port=6041 try: conn = taosws.connect(user="root", password="taosdata", @@ -56,10 +58,10 @@ try: stmt.add_batch() stmt.execute() - print(f"stmt insert successfully.") + print(f"Successfully inserted to power.meters.") except Exception as err: - print(f"Failed to insert to table meters using stmt, error: {err}") + print(f"Failed to insert to table meters using stmt, addr:{host}:{port} ; err:{err}") finally: if stmt: stmt.close() diff --git a/docs/examples/python/tmq_native.py b/docs/examples/python/tmq_native.py index 039a8107e8..fa813c9084 100644 --- a/docs/examples/python/tmq_native.py +++ b/docs/examples/python/tmq_native.py @@ -49,23 +49,28 @@ def prepareMeta(): from taos.tmq import Consumer def create_consumer(): + host = "localhost" + port = 6030 + groupId = "group1" + clientId = "1" try: consumer = Consumer( { - "group.id": "group1", - "client.id": "1", + "group.id": groupId, + "client.id": clientId, "td.connect.user": "root", "td.connect.pass": "taosdata", "enable.auto.commit": "true", "auto.commit.interval.ms": "1000", "auto.offset.reset": "latest", - "td.connect.ip": "localhost", - "td.connect.port": "6030", + "td.connect.ip": host, + "td.connect.port": port, } ) + print(f"Create consumer successfully, host: {host}:{port}, groupId: {groupId}, clientId: {clientId}"); return consumer except Exception as err: - print(f"Failed to poll data, err:{err}") + print(f"Failed to create websocket consumer, host: {host}:{port} ; err:{err}"); raise err # ANCHOR_END: create_consumer @@ -87,7 +92,8 @@ def subscribe(consumer): val = records.value() if val: for block in val: - print(block.fetchall()) + data = block.fetchall() + print(f"data: {data}") except Exception as err: print(f"Failed to poll data, err:{err}") @@ -114,6 +120,7 @@ def commit_offset(consumer): # after processing the data, commit the offset manually consumer.commit(records) + print("commit offset manually successfully."); except Exception as err: print(f"Failed to poll data, err:{err}") @@ -141,6 +148,7 @@ def seek_offset(consumer): def unsubscribe(consumer): try: consumer.unsubscribe() + print("unsubscribe consumer successfully."); except Exception as err: print(f"Failed to unsubscribe consumer. err:{err}") diff --git a/docs/examples/python/tmq_websocket_example.py b/docs/examples/python/tmq_websocket_example.py index a0aff17c42..94a3ddb6ee 100644 --- a/docs/examples/python/tmq_websocket_example.py +++ b/docs/examples/python/tmq_websocket_example.py @@ -60,20 +60,25 @@ def prepareMeta(): # ANCHOR: create_consumer def create_consumer(): + host = "localhost" + port = 6041 + groupId = "group1" + clientId = "1" try: consumer = taosws.Consumer(conf={ "td.connect.websocket.scheme": "ws", - "group.id": "group1", - "client.id": "1", + "group.id": groupId, + "client.id": clientId, "auto.offset.reset": "latest", - "td.connect.ip": "localhost", - "td.connect.port": "6041", + "td.connect.ip": host, + "td.connect.port": port, "enable.auto.commit": "true", "auto.commit.interval.ms": "1000", }) + print(f"Create consumer successfully, host: {host}:{port}, groupId: {groupId}, clientId: {clientId}"); return consumer; except Exception as err: - print(f"Failed to create websocket consumer, err:{err}"); + print(f"Failed to create websocket consumer, host: {host}:{port} ; err:{err}"); raise err @@ -108,7 +113,7 @@ def subscribe(consumer): if records: for block in records: for row in block: - print(row) + print(f"data: {row}") except Exception as err: print(f"Failed to poll data, err:{err}") @@ -125,10 +130,11 @@ def commit_offset(consumer): if records: for block in records: for row in block: - print(row) + print(f"data: {row}") # after processing the data, commit the offset manually consumer.commit(records) + print("commit offset manually successfully."); except Exception as err: print(f"Failed to poll data, err:{err}") @@ -141,6 +147,7 @@ def commit_offset(consumer): def unsubscribe(consumer): try: consumer.unsubscribe() + print("unsubscribe consumer successfully."); except Exception as err: print("Failed to unsubscribe consumer. err:{err}") From d4e2998e7fcc3cb5dc6a5e09ec5d3974da00e0b8 Mon Sep 17 00:00:00 2001 From: Yaming Pei Date: Mon, 12 Aug 2024 15:01:34 +0800 Subject: [PATCH 12/39] c language sample optimization --- docs/examples/c/connect_example.c | 12 ++-- docs/examples/c/create_db_demo.c | 25 ++++---- docs/examples/c/insert_data_demo.c | 21 +++--- docs/examples/c/query_data_demo.c | 20 +++--- docs/examples/c/sml_insert_demo.c | 45 ++++++++----- docs/examples/c/stmt_insert_demo.c | 27 +++++--- docs/examples/c/tmq_demo.c | 46 +++++++++---- docs/examples/c/with_reqid_demo.c | 100 +++++++++++++++-------------- 8 files changed, 166 insertions(+), 130 deletions(-) diff --git a/docs/examples/c/connect_example.c b/docs/examples/c/connect_example.c index f31db557cc..614e828018 100644 --- a/docs/examples/c/connect_example.c +++ b/docs/examples/c/connect_example.c @@ -8,19 +8,15 @@ int main() { const char *host = "localhost"; const char *user = "root"; const char *passwd = "taosdata"; - // if don't want to connect to a default db, set it to NULL or "" - const char *db = NULL; - uint16_t port = 0; // 0 means use the default port + const char *db = NULL; // if don't want to connect to a default db, set it to NULL or "" + uint16_t port = 6030; // 0 means use the default port TAOS *taos = taos_connect(host, user, passwd, db, port); if (taos == NULL) { - int errno = taos_errno(NULL); - const char *msg = taos_errstr(NULL); - printf("%d, %s\n", errno, msg); - printf("failed to connect to server %s, errno: %d, msg: %s\n", host, errno, msg); + printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } - printf("success to connect server %s\n", host); + printf("Connected to %s:%hu successfully.\n", host, port); /* put your code here for read and write */ diff --git a/docs/examples/c/create_db_demo.c b/docs/examples/c/create_db_demo.c index 32168edcaa..14848f8237 100644 --- a/docs/examples/c/create_db_demo.c +++ b/docs/examples/c/create_db_demo.c @@ -25,47 +25,44 @@ static int DemoCreateDB() { // ANCHOR: create_db_and_table -const char *ip = "localhost"; +const char *host = "localhost"; const char *user = "root"; const char *password = "taosdata"; +uint16_t port = 6030; +int code = 0; // connect -TAOS *taos = taos_connect(ip, user, password, NULL, 0); +TAOS *taos = taos_connect(host, user, password, NULL, port); if (taos == NULL) { - printf("failed to connect to server %s, reason: %s\n", ip, taos_errstr(NULL)); + printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } -printf("success to connect server %s\n", ip); // create database TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power"); -int code = taos_errno(result); +code = taos_errno(result); if (code != 0) { - printf("failed to create database power, reason: %s\n", taos_errstr(result)); + printf("Failed to create database power, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; } taos_free_result(result); -printf("success to create database power\n"); - -// use database -result = taos_query(taos, "USE power"); -taos_free_result(result); +printf("Create database power successfully.\n"); // create table -const char* sql = "CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))"; +const char* sql = "CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))"; result = taos_query(taos, sql); code = taos_errno(result); if (code != 0) { - printf("failed to create stable meters, reason: %s\n", taos_errstr(result)); + printf("Failed to create stable power.meters, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; } taos_free_result(result); -printf("success to create table meters\n"); +printf("Create stable meters successfully.\n"); // close & clean taos_close(taos); diff --git a/docs/examples/c/insert_data_demo.c b/docs/examples/c/insert_data_demo.c index 03e86d7f2c..8796556d03 100644 --- a/docs/examples/c/insert_data_demo.c +++ b/docs/examples/c/insert_data_demo.c @@ -24,22 +24,19 @@ static int DemoInsertData() { // ANCHOR: insert_data -const char *ip = "localhost"; +const char *host = "localhost"; const char *user = "root"; const char *password = "taosdata"; +uint16_t port = 6030; +int code = 0; // connect -TAOS *taos = taos_connect(ip, user, password, NULL, 0); +TAOS *taos = taos_connect(host, user, password, NULL, port); if (taos == NULL) { - printf("failed to connect to server %s, reason: %s\n", ip, taos_errstr(NULL)); + printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } -printf("success to connect server %s\n", ip); - -// use database -TAOS_RES *result = taos_query(taos, "USE power"); -taos_free_result(result); // insert data, please make sure the database and table are already created const char* sql = "INSERT INTO " \ @@ -51,10 +48,10 @@ const char* sql = "INSERT INTO " "power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco') " \ "VALUES " \ "(NOW + 1a, 10.30000, 218, 0.25000) "; -result = taos_query(taos, sql); -int code = taos_errno(result); +TAOS_RES *result = taos_query(taos, sql); +code = taos_errno(result); if (code != 0) { - printf("failed to insert data to power.meters, ip: %s, reason: %s\n", ip, taos_errstr(result)); + printf("Failed to insert data to power.meters, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; @@ -63,7 +60,7 @@ taos_free_result(result); // you can check affectedRows here int rows = taos_affected_rows(result); -printf("success to insert %d rows data to power.meters\n", rows); +printf("Successfully inserted %d rows into power.meters.\n", rows); // close & clean taos_close(taos); diff --git a/docs/examples/c/query_data_demo.c b/docs/examples/c/query_data_demo.c index 02f54b537c..77af4c9a8d 100644 --- a/docs/examples/c/query_data_demo.c +++ b/docs/examples/c/query_data_demo.c @@ -25,29 +25,28 @@ static int DemoQueryData() { // ANCHOR: query_data -const char *ip = "localhost"; +const char *host = "localhost"; const char *user = "root"; const char *password = "taosdata"; +uint16_t port = 6030; +int code = 0; // connect -TAOS *taos = taos_connect(ip, user, password, NULL, 0); +TAOS *taos = taos_connect(host, user, password, NULL, port); if (taos == NULL) { - printf("failed to connect to server %s, reason: %s\n", ip, taos_errstr(NULL)); + printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } -printf("success to connect server %s\n", ip); -// use database -TAOS_RES *result = taos_query(taos, "USE power"); -taos_free_result(result); + // query data, please make sure the database and table are already created const char* sql = "SELECT ts, current, location FROM power.meters limit 100"; -result = taos_query(taos, sql); -int code = taos_errno(result); +TAOS_RES *result = taos_query(taos, sql); +code = taos_errno(result); if (code != 0) { - printf("failed to query data from power.meters, ip: %s, reason: %s\n", ip, taos_errstr(result)); + printf("Failed to query data from power.meters, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; @@ -70,7 +69,6 @@ while ((row = taos_fetch_row(result))) { } printf("total rows: %d\n", rows); taos_free_result(result); -printf("success to query data from power.meters\n"); // close & clean taos_close(taos); diff --git a/docs/examples/c/sml_insert_demo.c b/docs/examples/c/sml_insert_demo.c index 0c4be1d6f8..65636d89b8 100644 --- a/docs/examples/c/sml_insert_demo.c +++ b/docs/examples/c/sml_insert_demo.c @@ -24,33 +24,41 @@ static int DemoSmlInsert() { // ANCHOR: schemaless -const char *ip = "localhost"; +const char *host = "localhost"; const char *user = "root"; const char *password = "taosdata"; +uint16_t port = 6030; +int code = 0; // connect -TAOS *taos = taos_connect(ip, user, password, NULL, 0); +TAOS *taos = taos_connect(host, user, password, NULL, port); if (taos == NULL) { - printf("failed to connect to server %s, reason: %s\n", ip, taos_errstr(NULL)); + printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } -printf("success to connect server %s\n", ip); // create database TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power"); -int code = taos_errno(result); +code = taos_errno(result); if (code != 0) { - printf("failed to create database power, reason: %s\n", taos_errstr(result)); + printf("Failed to create database power, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; } taos_free_result(result); -printf("success to create database power\n"); +printf("Create database power successfully.\n"); // use database result = taos_query(taos, "USE power"); +code = taos_errno(result); +if (code != 0) { + printf("Failed to execute use power, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", host, port, code, taos_errstr(result)); + taos_close(taos); + taos_cleanup(); + return -1; +} taos_free_result(result); // schemaless demo data @@ -61,29 +69,31 @@ char * json_demo = "{\"metric\": \"metric_json\",\"timestamp\": 1626846400,\"val // influxdb line protocol char *lines[] = {line_demo}; result = taos_schemaless_insert(taos, lines, 1, TSDB_SML_LINE_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS); -if (taos_errno(result) != 0) { - printf("failed to insert schemaless line data, reason: %s\n", taos_errstr(result)); +code = taos_errno(result); +if (code != 0) { + printf("Failed to insert schemaless line data, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; } int rows = taos_affected_rows(result); -printf("success to insert %d rows of schemaless line data\n", rows); +printf("Insert %d rows of schemaless line data successfully.\n", rows); taos_free_result(result); // opentsdb telnet protocol char *telnets[] = {telnet_demo}; result = taos_schemaless_insert(taos, telnets, 1, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS); -if (taos_errno(result) != 0) { - printf("failed to insert schemaless telnet data, reason: %s\n", taos_errstr(result)); +code = taos_errno(result); +if (code != 0) { + printf("Failed to insert schemaless telnet data, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; } rows = taos_affected_rows(result); -printf("success to insert %d rows of schemaless telnet data\n", rows); +printf("Insert %d rows of schemaless telnet data successfully.\n", rows); taos_free_result(result); // opentsdb json protocol @@ -91,16 +101,17 @@ char *jsons[1] = {0}; // allocate memory for json data. can not use static memory. jsons[0] = malloc(1024); if (jsons[0] == NULL) { - printf("failed to allocate memory\n"); + printf("Failed to allocate memory\n"); taos_close(taos); taos_cleanup(); return -1; } (void)strncpy(jsons[0], json_demo, 1023); result = taos_schemaless_insert(taos, jsons, 1, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NOT_CONFIGURED); -if (taos_errno(result) != 0) { +code = taos_errno(result); +if (code != 0) { free(jsons[0]); - printf("failed to insert schemaless json data, reason: %s\n", taos_errstr(result)); + printf("Failed to insert schemaless json data, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; @@ -108,7 +119,7 @@ if (taos_errno(result) != 0) { free(jsons[0]); rows = taos_affected_rows(result); -printf("success to insert %d rows of schemaless json data\n", rows); +printf("Insert %d rows of schemaless json data successfully.\n", rows); taos_free_result(result); // close & clean diff --git a/docs/examples/c/stmt_insert_demo.c b/docs/examples/c/stmt_insert_demo.c index ba953a856f..4bf794bcda 100644 --- a/docs/examples/c/stmt_insert_demo.c +++ b/docs/examples/c/stmt_insert_demo.c @@ -64,6 +64,7 @@ typedef struct { int num_of_sub_table = 10; int num_of_row = 10; +int total_affected = 0; /** * @brief insert data using stmt API * @@ -72,10 +73,14 @@ int num_of_row = 10; void insertData(TAOS *taos) { // init TAOS_STMT *stmt = taos_stmt_init(taos); + if (stmt == NULL) { + printf("Failed to init taos_stmt, error: %s\n", taos_stmt_errstr(NULL)); + exit(EXIT_FAILURE); + } // prepare const char *sql = "INSERT INTO ? USING meters TAGS(?,?) VALUES (?,?,?,?)"; int code = taos_stmt_prepare(stmt, sql, 0); - checkErrorCode(stmt, code, "failed to execute taos_stmt_prepare"); + checkErrorCode(stmt, code, "Failed to execute taos_stmt_prepare"); for (int i = 1; i <= num_of_sub_table; i++) { char table_name[20]; sprintf(table_name, "d_bind_%d", i); @@ -99,7 +104,7 @@ void insertData(TAOS *taos) { tags[1].is_null = NULL; tags[1].num = 1; code = taos_stmt_set_tbname_tags(stmt, table_name, tags); - checkErrorCode(stmt, code, "failed to set table name and tags\n"); + checkErrorCode(stmt, code, "Failed to set table name and tags\n"); // insert rows TAOS_MULTI_BIND params[4]; @@ -142,25 +147,31 @@ void insertData(TAOS *taos) { params[3].buffer = &phase; // bind param code = taos_stmt_bind_param(stmt, params); - checkErrorCode(stmt, code, "failed to bind param"); + checkErrorCode(stmt, code, "Failed to bind param"); } // add batch code = taos_stmt_add_batch(stmt); - checkErrorCode(stmt, code, "failed to add batch"); + checkErrorCode(stmt, code, "Failed to add batch"); // execute batch code = taos_stmt_execute(stmt); - checkErrorCode(stmt, code, "failed to exec stmt"); + checkErrorCode(stmt, code, "Failed to exec stmt"); // get affected rows int affected = taos_stmt_affected_rows_once(stmt); - printf("table %s insert %d rows.\n", table_name, affected); + total_affected += affected; } + printf("Successfully inserted %d rows to power.meters.\n", total_affected); taos_stmt_close(stmt); } int main() { - TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 6030); + const char *host = "localhost"; + const char *user = "root"; + const char *password = "taosdata"; + uint16_t port = 6030; + TAOS *taos = taos_connect(host, user, password, NULL, port); if (taos == NULL) { - printf("failed to connect to server\n"); + printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + taos_cleanup(); exit(EXIT_FAILURE); } // create database and table diff --git a/docs/examples/c/tmq_demo.c b/docs/examples/c/tmq_demo.c index adce4bbd5f..30cbbdc1c0 100644 --- a/docs/examples/c/tmq_demo.c +++ b/docs/examples/c/tmq_demo.c @@ -27,9 +27,15 @@ static int running = 1; const char* topic_name = "topic_meters"; void* prepare_data(void* arg) { - TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0); + const char *host = "localhost"; + const char *user = "root"; + const char *password = "taosdata"; + uint16_t port = 6030; + TAOS *pConn = taos_connect(host, user, password, NULL, port); if (pConn == NULL) { - return NULL; + printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + taos_cleanup(); + return -1; } TAOS_RES* pRes; @@ -46,12 +52,12 @@ void* prepare_data(void* arg) { pRes = taos_query(pConn, buf); if (taos_errno(pRes) != 0) { - printf("error in insert data to power.meters, reason:%s\n", taos_errstr(pRes)); + printf("Failed to insert data to power.meters, reason: %s\n", taos_errstr(pRes)); } taos_free_result(pRes); sleep(1); } - printf("prepare data thread exit\n"); + printf("Prepare data thread exit\n"); return NULL; } @@ -81,7 +87,7 @@ static int32_t msg_process(TAOS_RES* msg) { rows++; // print the row content if (taos_print_row(buf, row, fields, numOfFields) < 0) { - printf("failed to print row\n"); + printf("Failed to print row\n"); break; } // print the precision and row content to the console @@ -93,8 +99,14 @@ static int32_t msg_process(TAOS_RES* msg) { // ANCHOR_END: msg_process static int32_t init_env() { - TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0); + const char *host = "localhost"; + const char *user = "root"; + const char *password = "taosdata"; + uint16_t port = 6030; + TAOS *pConn = taos_connect(host, user, password, NULL, port); if (pConn == NULL) { + printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + taos_cleanup(); return -1; } @@ -145,8 +157,14 @@ END: int32_t create_topic() { printf("create topic\n"); TAOS_RES* pRes; - TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0); + const char *host = "localhost"; + const char *user = "root"; + const char *password = "taosdata"; + uint16_t port = 6030; + TAOS *pConn = taos_connect(host, user, password, NULL, port); if (pConn == NULL) { + printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + taos_cleanup(); return -1; } @@ -285,7 +303,7 @@ void consume_repeatly(tmq_t* tmq) { // get the topic assignment int32_t code = tmq_get_topic_assignment(tmq, topic_name, &pAssign, &numOfAssignment); if (code != 0 || pAssign == NULL || numOfAssignment == 0) { - fprintf(stderr, "failed to get assignment, reason:%s", tmq_err2str(code)); + fprintf(stderr, "Failed to get assignment, reason:%s", tmq_err2str(code)); return; } @@ -295,7 +313,7 @@ void consume_repeatly(tmq_t* tmq) { code = tmq_offset_seek(tmq, topic_name, p->vgId, p->begin); if (code != 0) { - fprintf(stderr, "failed to seek to %d, reason:%s", (int)p->begin, tmq_err2str(code)); + fprintf(stderr, "Failed to seek to %d, reason:%s", (int)p->begin, tmq_err2str(code)); } } @@ -328,6 +346,8 @@ void manual_commit(tmq_t* tmq) { // free the message taos_free_result(tmqmsg); break; + } else { + printf("Commit offset manually successfully."); } // free the message taos_free_result(tmqmsg); @@ -356,17 +376,17 @@ int main(int argc, char* argv[]) { } if (pthread_create(&thread_id, NULL, &prepare_data, NULL)) { - fprintf(stderr, "create thread failed\n"); + fprintf(stderr, "Create thread failed\n"); return 1; } // ANCHOR: create_consumer_2 tmq_t* tmq = build_consumer(); if (NULL == tmq) { - fprintf(stderr, "build consumer to localhost fail!\n"); + fprintf(stderr, "Failed to create consumer.\n"); return -1; } - printf("build consumer to localhost successfully \n"); + printf("Create consumer successfully.\n"); // ANCHOR_END: create_consumer_2 @@ -378,6 +398,8 @@ int main(int argc, char* argv[]) { if ((code = tmq_subscribe(tmq, topic_list))) { fprintf(stderr, "Failed to tmq_subscribe(): %s\n", tmq_err2str(code)); + } else { + printf("Subscribe topics successfully.\n"); } tmq_list_destroy(topic_list); diff --git a/docs/examples/c/with_reqid_demo.c b/docs/examples/c/with_reqid_demo.c index 50d29805f9..99f03a579e 100644 --- a/docs/examples/c/with_reqid_demo.c +++ b/docs/examples/c/with_reqid_demo.c @@ -23,55 +23,59 @@ #include "taos.h" static int DemoWithReqId() { - // ANCHOR: with_reqid - const char *ip = "localhost"; - const char *user = "root"; - const char *password = "taosdata"; +// ANCHOR: with_reqid +const char *host = "localhost"; +const char *user = "root"; +const char *password = "taosdata"; +uint16_t port = 6030; +int code = 0; - // connect - TAOS *taos = taos_connect(ip, user, password, NULL, 0); - if (taos == NULL) { - printf("failed to connect to server %s, reason: %s\n", ip, taos_errstr(NULL)); - taos_cleanup(); - return -1; - } - printf("success to connect server %s\n", ip); - - const char *sql = "SELECT ts, current, location FROM power.meters limit 1"; - // query data with reqid - TAOS_RES *result = taos_query_with_reqid(taos, sql, 3L); - int code = taos_errno(result); - if (code != 0) { - printf("failed to query data from power.meters, ip: %s, reason: %s\n", ip, taos_errstr(result)); - taos_close(taos); - taos_cleanup(); - return -1; - } - - TAOS_ROW row = NULL; - int rows = 0; - int num_fields = taos_field_count(result); - TAOS_FIELD *fields = taos_fetch_fields(result); - - printf("fields: %d\n", num_fields); - printf("sql: %s, result:\n", sql); - - // fetch the records row by row - while ((row = taos_fetch_row(result))) { - char temp[1024] = {0}; - rows++; - taos_print_row(temp, row, fields, num_fields); - printf("%s\n", temp); - } - printf("total rows: %d\n", rows); - taos_free_result(result); - printf("success to query data from power.meters\n"); - - // close & clean - taos_close(taos); +// connect +TAOS *taos = taos_connect(host, user, password, NULL, port); +if (taos == NULL) { + printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); - return 0; - // ANCHOR_END: with_reqid + return -1; +} + +const char *sql = "SELECT ts, current, location FROM power.meters limit 1"; +// query data with reqid +long reqid = 3L; +TAOS_RES *result = taos_query_with_reqid(taos, sql, reqid); +code = taos_errno(result); +if (code != 0) { + printf("Failed to execute sql with reqId: %ld, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", reqid, host, port, code, taos_errstr(result)); + taos_close(taos); + taos_cleanup(); + return -1; +} + +TAOS_ROW row = NULL; +int rows = 0; +int num_fields = taos_field_count(result); +TAOS_FIELD *fields = taos_fetch_fields(result); + +printf("fields: %d\n", num_fields); +printf("sql: %s, result:\n", sql); + +// fetch the records row by row +while ((row = taos_fetch_row(result))) { + char temp[1024] = {0}; + rows++; + taos_print_row(temp, row, fields, num_fields); + printf("%s\n", temp); +} +printf("total rows: %d\n", rows); +taos_free_result(result); + +// close & clean +taos_close(taos); +taos_cleanup(); +return 0; +// ANCHOR_END: with_reqid +} + +int main(int argc, char *argv[]) { + return DemoWithReqId(); } -int main(int argc, char *argv[]) { return DemoWithReqId(); } From 19952995af675694dec8ef5a2175ba0c20e2f566 Mon Sep 17 00:00:00 2001 From: wangjiaming0909 <604227650@qq.com> Date: Mon, 12 Aug 2024 15:27:15 +0800 Subject: [PATCH 13/39] fix fill order by returned wrong values --- source/libs/executor/src/filloperator.c | 2 + source/libs/executor/src/tfill.c | 13 +++++-- tests/system-test/2-query/fill.py | 11 ++++++ tests/system-test/2-query/fill_with_group.py | 41 +++++++++++++++++++- 4 files changed, 62 insertions(+), 5 deletions(-) diff --git a/source/libs/executor/src/filloperator.c b/source/libs/executor/src/filloperator.c index f61b514626..e39667531c 100644 --- a/source/libs/executor/src/filloperator.c +++ b/source/libs/executor/src/filloperator.c @@ -254,6 +254,8 @@ static SSDataBlock* doFillImpl(SOperatorInfo* pOperator) { (pInfo->pFillInfo->type != TSDB_FILL_NULL_F && pInfo->pFillInfo->type != TSDB_FILL_SET_VALUE_F)) { setOperatorCompleted(pOperator); return NULL; + } else if (pInfo->totalInputRows == 0 && taosFillNotStarted(pInfo->pFillInfo)) { + reviseFillStartAndEndKey(pInfo, order); } taosFillSetStartInfo(pInfo->pFillInfo, 0, pInfo->win.ekey); diff --git a/source/libs/executor/src/tfill.c b/source/libs/executor/src/tfill.c index e6c2ea1b45..b8ee5bf047 100644 --- a/source/libs/executor/src/tfill.c +++ b/source/libs/executor/src/tfill.c @@ -207,7 +207,7 @@ static void doFillOneRow(SFillInfo* pFillInfo, SSDataBlock* pBlock, SSDataBlock* setNotFillColumn(pFillInfo, pDstCol, index, i); } } else { - SRowVal* pRVal = FILL_IS_ASC_FILL(pFillInfo) ? &pFillInfo->prev : &pFillInfo->next; + SRowVal* pRVal = &pFillInfo->prev; SGroupKeys* pKey = taosArrayGet(pRVal->pRowVal, i); if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_BOOL || pKey->isNull) { colDataSetNULL(pDstCol, index); @@ -395,8 +395,8 @@ static int32_t fillResultImpl(SFillInfo* pFillInfo, SSDataBlock* pBlock, int32_t ASSERT(pFillInfo->currentKey == ts); int32_t index = pBlock->info.rows; + int32_t nextRowIndex = pFillInfo->index + 1; if (pFillInfo->type == TSDB_FILL_NEXT) { - int32_t nextRowIndex = pFillInfo->index + 1; if ((pFillInfo->index + 1) < pFillInfo->numOfRows) { copyCurrentRowIntoBuf(pFillInfo, nextRowIndex, &pFillInfo->next, false); } else { @@ -404,6 +404,11 @@ static int32_t fillResultImpl(SFillInfo* pFillInfo, SSDataBlock* pBlock, int32_t copyCurrentRowIntoBuf(pFillInfo, nextRowIndex, &pFillInfo->next, true); } } + if (pFillInfo->type == TSDB_FILL_PREV) { + if (nextRowIndex + 1 >= pFillInfo->numOfRows && !FILL_IS_ASC_FILL(pFillInfo)) { + copyCurrentRowIntoBuf(pFillInfo, nextRowIndex, &pFillInfo->next, true); + } + } // copy rows to dst buffer for (int32_t i = 0; i < pFillInfo->numOfCols; ++i) { @@ -418,7 +423,7 @@ static int32_t fillResultImpl(SFillInfo* pFillInfo, SSDataBlock* pBlock, int32_t if (!colDataIsNull_s(pSrc, pFillInfo->index)) { code = colDataSetVal(pDst, index, src, false); QUERY_CHECK_CODE(code, lino, _end); - SRowVal* pRVal = FILL_IS_ASC_FILL(pFillInfo) ? &pFillInfo->prev : &pFillInfo->next; + SRowVal* pRVal = &pFillInfo->prev; saveColData(pRVal->pRowVal, i, src, false); if (pFillInfo->srcTsSlotId == dstSlotId) { pRVal->key = *(int64_t*)src; @@ -439,7 +444,7 @@ static int32_t fillResultImpl(SFillInfo* pFillInfo, SSDataBlock* pBlock, int32_t code = colDataSetVal(pDst, index, src, isNull); QUERY_CHECK_CODE(code, lino, _end); - SArray* p = FILL_IS_ASC_FILL(pFillInfo) ? pFillInfo->prev.pRowVal : pFillInfo->next.pRowVal; + SArray* p = pFillInfo->prev.pRowVal; saveColData(p, i, src, isNull); // todo: } else if (pFillInfo->type == TSDB_FILL_NULL || pFillInfo->type == TSDB_FILL_NULL_F) { colDataSetNULL(pDst, index); diff --git a/tests/system-test/2-query/fill.py b/tests/system-test/2-query/fill.py index 64a43bd80a..31d2d92563 100644 --- a/tests/system-test/2-query/fill.py +++ b/tests/system-test/2-query/fill.py @@ -126,6 +126,17 @@ class TDTestCase: def test_fill_range(self): os.system('taosBenchmark -t 10 -n 10000 -v 8 -S 32000 -y') self.schedule_fill_test_tasks() + sql = "select first(_wstart), last(_wstart) from (select _wstart, count(*) from test.meters where ts >= '2019-09-19 23:54:00.000' and ts < '2019-09-20 01:00:00.000' interval(10s) sliding(10s) fill(VALUE_F, 122) order by _wstart) t" + tdSql.query(sql, queryTimes=1) + rows = tdSql.getRows() + first_ts = tdSql.queryResult[0][0] + last_ts = tdSql.queryResult[0][1] + + sql = "select first(_wstart), last(_wstart) from (select _wstart, count(*) from test.meters where ts >= '2019-09-19 23:54:00.000' and ts < '2019-09-20 01:00:00.000' interval(10s) sliding(10s) fill(VALUE_F, 122) order by _wstart desc) t" + tdSql.query(sql, queryTimes=1) + tdSql.checkRows(rows) + tdSql.checkData(0, 0, first_ts) + tdSql.checkData(0, 1, last_ts) tdSql.execute('drop database test') def run(self): diff --git a/tests/system-test/2-query/fill_with_group.py b/tests/system-test/2-query/fill_with_group.py index 47e70d3c0f..2139bbbfb3 100644 --- a/tests/system-test/2-query/fill_with_group.py +++ b/tests/system-test/2-query/fill_with_group.py @@ -165,7 +165,7 @@ class TDTestCase: sql = "select _wstart, count(*) from meters where ts >= '2018-09-20 00:00:00.000' and ts < '2018-09-20 01:00:00.000' interval(5m) fill(prev) order by _wstart desc;" tdSql.query(sql, queryTimes=1) tdSql.checkRows(12) - tdSql.checkData(0, 1, None) + tdSql.checkData(0, 1, 10) tdSql.checkData(1, 1, 10) tdSql.checkData(2, 1, 10) tdSql.checkData(3, 1, 10) @@ -198,6 +198,45 @@ class TDTestCase: tdSql.query(sql, queryTimes=1) tdSql.checkRows(60) + sql = "select _wstart, count(*) from meters where ts >= '2018-09-19 23:54:00.000' and ts < '2018-09-20 01:00:00.000' interval(5m) fill(next) order by _wstart asc;" + tdSql.query(sql, queryTimes=1) + for i in range(0, 13): + tdSql.checkData(i, 1, 10) + tdSql.checkData(13, 1, None) + sql = "select _wstart, count(*) from meters where ts >= '2018-09-19 23:54:00.000' and ts < '2018-09-20 01:00:00.000' interval(5m) fill(next) order by _wstart desc;" + tdSql.query(sql, queryTimes=1) + tdSql.checkData(0, 1, None) + for i in range(1, 14): + tdSql.checkData(i, 1, 10) + + sql = "select _wstart, count(*) from meters where ts >= '2018-09-19 23:54:00.000' and ts < '2018-09-20 01:00:00.000' interval(5m) fill(prev) order by _wstart asc;" + tdSql.query(sql, queryTimes=1) + tdSql.checkData(0, 1, None) + tdSql.checkData(1, 1, None) + for i in range(2, 14): + tdSql.checkData(i, 1, 10) + sql = "select _wstart, count(*) from meters where ts >= '2018-09-19 23:54:00.000' and ts < '2018-09-20 01:00:00.000' interval(5m) fill(prev) order by _wstart desc;" + tdSql.query(sql, queryTimes=1) + for i in range(0, 12): + tdSql.checkData(i, 1, 10) + tdSql.checkData(12, 1, None) + tdSql.checkData(13, 1, None) + + sql = "select _wstart, count(*) from meters where ts >= '2018-09-19 23:54:00.000' and ts < '2018-09-20 01:00:00.000' interval(5m) fill(linear) order by _wstart asc;" + tdSql.query(sql, queryTimes=1) + tdSql.checkData(0, 1, None) + tdSql.checkData(1, 1, None) + for i in range(2, 13): + tdSql.checkData(i, 1, 10) + tdSql.checkData(13, 1, None) + sql = "select _wstart, count(*) from meters where ts >= '2018-09-19 23:54:00.000' and ts < '2018-09-20 01:00:00.000' interval(5m) fill(linear) order by _wstart desc;" + tdSql.query(sql, queryTimes=1) + tdSql.checkData(0, 1, None) + for i in range(1, 12): + tdSql.checkData(i, 1, 10) + tdSql.checkData(12, 1, None) + tdSql.checkData(13, 1, None) + def run(self): self.prepareTestEnv() self.test_partition_by_with_interval_fill_prev_new_group_fill_error() From 7e57747beea402f8a7f00b961e296cd584e46a93 Mon Sep 17 00:00:00 2001 From: kailixu Date: Mon, 12 Aug 2024 15:51:00 +0800 Subject: [PATCH 14/39] fix: null pointer check when clearing tsdb reader --- source/dnode/vnode/src/tsdb/tsdbRead2.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/source/dnode/vnode/src/tsdb/tsdbRead2.c b/source/dnode/vnode/src/tsdb/tsdbRead2.c index 0af88e8551..97be3cde85 100644 --- a/source/dnode/vnode/src/tsdb/tsdbRead2.c +++ b/source/dnode/vnode/src/tsdb/tsdbRead2.c @@ -4608,11 +4608,13 @@ static void freeSchemaFunc(void* param) { } static void clearSharedPtr(STsdbReader* p) { - p->status.pTableMap = NULL; - p->status.uidList.tableUidList = NULL; - p->info.pSchema = NULL; - p->pReadSnap = NULL; - p->pSchemaMap = NULL; + if (p) { + p->status.pTableMap = NULL; + p->status.uidList.tableUidList = NULL; + p->info.pSchema = NULL; + p->pReadSnap = NULL; + p->pSchemaMap = NULL; + } } static int32_t setSharedPtr(STsdbReader* pDst, const STsdbReader* pSrc) { From 3f10665ba31da321154bd3b8efb607970e1a8a03 Mon Sep 17 00:00:00 2001 From: t_max <1172915550@qq.com> Date: Mon, 12 Aug 2024 15:54:28 +0800 Subject: [PATCH 15/39] docs: update go and c# examples --- docs/examples/csharp/optsJSON/Program.cs | 10 ++-- docs/examples/csharp/stmtInsert/Program.cs | 9 ++-- docs/examples/csharp/subscribe/Program.cs | 36 +++++++++----- docs/examples/csharp/wsInsert/Program.cs | 49 ++++++++++--------- docs/examples/csharp/wsStmt/Program.cs | 9 ++-- docs/examples/csharp/wssml/Program.cs | 13 +++-- docs/examples/csharp/wssubscribe/Program.cs | 47 +++++++++++------- docs/examples/go/connect/cgoexample/main.go | 2 +- docs/examples/go/connect/restexample/main.go | 2 +- docs/examples/go/connect/wsexample/main.go | 2 +- docs/examples/go/queryreqid/main.go | 10 ++-- docs/examples/go/schemaless/native/main.go | 14 +++--- docs/examples/go/schemaless/ws/main.go | 16 ++++--- docs/examples/go/sqlquery/main.go | 42 ++++++++-------- docs/examples/go/stmt/native/main.go | 20 ++++---- docs/examples/go/stmt/ws/main.go | 30 ++++++------ docs/examples/go/tmq/native/main.go | 42 ++++++++-------- docs/examples/go/tmq/ws/main.go | 44 ++++++++--------- tests/docs-examples-test/go.sh | 50 +++++++++++++------- 19 files changed, 251 insertions(+), 196 deletions(-) diff --git a/docs/examples/csharp/optsJSON/Program.cs b/docs/examples/csharp/optsJSON/Program.cs index d9b936745e..d348e0efbc 100644 --- a/docs/examples/csharp/optsJSON/Program.cs +++ b/docs/examples/csharp/optsJSON/Program.cs @@ -5,9 +5,9 @@ namespace TDengineExample { internal class OptsJsonExample { + // ANCHOR: main public static void Main(string[] args) { - // ANCHOR: main var host = "127.0.0.1"; var lineDemo = @@ -38,20 +38,22 @@ namespace TDengineExample client.SchemalessInsert(new []{jsonDemo}, TDengineSchemalessProtocol.TSDB_SML_JSON_PROTOCOL, TDengineSchemalessPrecision.TSDB_SML_TIMESTAMP_NOT_CONFIGURED, 0, ReqId.GetReqId()); } + + Console.WriteLine("Inserted data with schemaless successfully."); } catch (TDengineError e) { // handle TDengine error - Console.WriteLine("Failed to insert data with schemaless; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); + Console.WriteLine("Failed to insert data with schemaless, host:" + host + "; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); throw; } catch (Exception e) { // handle other exceptions - Console.WriteLine("Failed to insert data with schemaless; Err:" + e.Message); + Console.WriteLine("Failed to insert data with schemaless, host:" + host + "; ErrMessage: " + e.Message); throw; } - // ANCHOR_END: main } + // ANCHOR_END: main } } \ No newline at end of file diff --git a/docs/examples/csharp/stmtInsert/Program.cs b/docs/examples/csharp/stmtInsert/Program.cs index 161682b19e..60d1a58e55 100644 --- a/docs/examples/csharp/stmtInsert/Program.cs +++ b/docs/examples/csharp/stmtInsert/Program.cs @@ -12,9 +12,10 @@ namespace TDengineExample var numOfSubTable = 10; var numOfRow = 10; var random = new Random(); + var connectionString = $"host={host};port=6030;username=root;password=taosdata"; try { - var builder = new ConnectionStringBuilder($"host={host};port=6030;username=root;password=taosdata"); + var builder = new ConnectionStringBuilder(connectionString); using (var client = DbDriver.Open(builder)) { // create database @@ -53,7 +54,7 @@ namespace TDengineExample stmt.Exec(); // get affected rows var affectedRows = stmt.Affected(); - Console.WriteLine($"table {tableName} insert {affectedRows} rows."); + Console.WriteLine($"Successfully inserted {affectedRows} rows to {tableName}."); } } } @@ -61,13 +62,13 @@ namespace TDengineExample catch (TDengineError e) { // handle TDengine error - Console.WriteLine("Failed to insert to table meters using stmt; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); + Console.WriteLine("Failed to insert to table meters using stmt, url: " + connectionString + "; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); throw; } catch (Exception e) { // handle other exceptions - Console.WriteLine("Failed to insert to table meters using stmt; Err:" + e.Message); + Console.WriteLine("Failed to insert to table meters using stmt, url: " + connectionString + "; ErrMessage: " + e.Message); throw; } } diff --git a/docs/examples/csharp/subscribe/Program.cs b/docs/examples/csharp/subscribe/Program.cs index 4798877440..a83a9f07a8 100644 --- a/docs/examples/csharp/subscribe/Program.cs +++ b/docs/examples/csharp/subscribe/Program.cs @@ -64,6 +64,9 @@ namespace TMQExample { // ANCHOR: create_consumer // consumer config + var host = "127.0.0.1"; + var groupId = "group1"; + var clientId = "client1"; var cfg = new Dictionary() { { "td.connect.port", "6030" }, @@ -71,9 +74,9 @@ namespace TMQExample { "msg.with.table.name", "true" }, { "enable.auto.commit", "true" }, { "auto.commit.interval.ms", "1000" }, - { "group.id", "group1" }, - { "client.id", "client1" }, - { "td.connect.ip", "127.0.0.1" }, + { "group.id", groupId }, + { "client.id", clientId }, + { "td.connect.ip", host }, { "td.connect.user", "root" }, { "td.connect.pass", "taosdata" }, }; @@ -82,17 +85,20 @@ namespace TMQExample { // create consumer consumer = new ConsumerBuilder>(cfg).Build(); + Console.WriteLine("Create consumer successfully, host: " + host + ", groupId: " + groupId + + ", clientId: " + clientId); } catch (TDengineError e) { // handle TDengine error - Console.WriteLine("Failed to create consumer; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); + Console.WriteLine("Failed to create native consumer, host : " + host + "; ErrCode:" + e.Code + + "; ErrMessage: " + e.Error); throw; } catch (Exception e) { // handle other exceptions - Console.WriteLine("Failed to create consumer; Err:" + e.Message); + Console.WriteLine("Failed to create native consumer, host : " + host + "; ErrMessage: " + e.Message); throw; } @@ -107,6 +113,7 @@ namespace TMQExample { // subscribe consumer.Subscribe(new List() { "topic_meters" }); + Console.WriteLine("subscribe topics successfully"); for (int i = 0; i < 50; i++) { // consume message with using block to ensure the result is disposed @@ -117,7 +124,7 @@ namespace TMQExample { // handle message Console.WriteLine( - $"data {{{((DateTime)message.Value["ts"]).ToString("yyyy-MM-dd HH:mm:ss.fff")}, " + + $"data: {{{((DateTime)message.Value["ts"]).ToString("yyyy-MM-dd HH:mm:ss.fff")}, " + $"{message.Value["current"]}, {message.Value["voltage"]}, {message.Value["phase"]}}}"); } } @@ -132,7 +139,7 @@ namespace TMQExample catch (Exception e) { // handle other exceptions - Console.WriteLine("Failed to poll data; Err:" + e.Message); + Console.WriteLine("Failed to poll data; ErrMessage:" + e.Message); throw; } // ANCHOR_END: subscribe @@ -145,6 +152,7 @@ namespace TMQExample { // get assignment var assignment = consumer.Assignment; + Console.WriteLine($"now assignment: ${assignment}"); // seek to the beginning foreach (var topicPartition in assignment) { @@ -172,13 +180,13 @@ namespace TMQExample catch (TDengineError e) { // handle TDengine error - Console.WriteLine("Failed to seek; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); + Console.WriteLine("seek example failed; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); throw; } catch (Exception e) { // handle other exceptions - Console.WriteLine("Failed to seek; Err:" + e.Message); + Console.WriteLine("seek example failed; ErrMessage: " + e.Message); throw; } // ANCHOR_END: seek @@ -200,18 +208,19 @@ namespace TMQExample { cr.TopicPartitionOffset, }); + Console.WriteLine("commit offset manually successfully."); } } catch (TDengineError e) { // handle TDengine error - Console.WriteLine("Failed to commit offset; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); + Console.WriteLine("Failed to execute consumer functions. ErrCode:" + e.Code + "; ErrMessage: " + e.Error); throw; } catch (Exception e) { // handle other exceptions - Console.WriteLine("Failed to commit offset; Err:" + e.Message); + Console.WriteLine("Failed to execute consumer functions. ErrMessage:" + e.Message); throw; } } @@ -229,19 +238,20 @@ namespace TMQExample catch (TDengineError e) { // handle TDengine error - Console.WriteLine("Failed to unsubscribe consumer; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); + Console.WriteLine("Failed to unsubscribe consumer. ErrCode:" + e.Code + "; ErrMessage: " + e.Error); throw; } catch (Exception e) { // handle other exceptions - Console.WriteLine("Failed to unsubscribe consumer; Err:" + e.Message); + Console.WriteLine("Failed to unsubscribe consumer. Err: " + e.Message); throw; } finally { // close consumer consumer.Close(); + Console.WriteLine("consumer closed successfully."); } // ANCHOR_END: close } diff --git a/docs/examples/csharp/wsInsert/Program.cs b/docs/examples/csharp/wsInsert/Program.cs index 06e3dbe5e8..cdddf27f42 100644 --- a/docs/examples/csharp/wsInsert/Program.cs +++ b/docs/examples/csharp/wsInsert/Program.cs @@ -11,13 +11,15 @@ namespace Examples { try { - var builder = new ConnectionStringBuilder("protocol=WebSocket;host=127.0.0.1;port=6041;useSSL=false;username=root;password=taosdata"); + var connectionString = + "protocol=WebSocket;host=127.0.0.1;port=6041;useSSL=false;username=root;password=taosdata"; + var builder = new ConnectionStringBuilder(connectionString); using (var client = DbDriver.Open(builder)) { - CreateDatabaseAndTable(client); - InsertData(client); - QueryData(client); - QueryWithReqId(client); + CreateDatabaseAndTable(client,connectionString); + InsertData(client,connectionString); + QueryData(client,connectionString); + QueryWithReqId(client,connectionString); } } catch (TDengineError e) @@ -34,40 +36,40 @@ namespace Examples } } - private static void CreateDatabaseAndTable(ITDengineClient client) + private static void CreateDatabaseAndTable(ITDengineClient client, string connectionString) { // ANCHOR: create_db_and_table try { // create database var affected = client.Exec("CREATE DATABASE IF NOT EXISTS power"); - Console.WriteLine($"Create database power, affected rows: {affected}"); + Console.WriteLine($"Create database power successfully, rowsAffected: {affected}"); // create table affected = client.Exec( "CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))"); - Console.WriteLine($"Create table meters, affected rows: {affected}"); + Console.WriteLine($"Create stable power.meters successfully, rowsAffected: {affected}"); } catch (TDengineError e) { // handle TDengine error - Console.WriteLine("Failed to create db and table; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); + Console.WriteLine("Failed to create db and table,url:" + connectionString +"; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); throw; } catch (Exception e) { // handle other exceptions - Console.WriteLine("Failed to create db and table; Err:" + e.Message); + Console.WriteLine("Failed to create db and table, url:" + connectionString + "; ErrMessage: " + e.Message); throw; } // ANCHOR_END: create_db_and_table } - private static void InsertData(ITDengineClient client) + private static void InsertData(ITDengineClient client,string connectionString) { // ANCHOR: insert_data try { - // insert data + // insert data, please make sure the database and table are created before var insertQuery = "INSERT INTO " + "power.d1001 USING power.meters TAGS(2,'California.SanFrancisco') " + "VALUES " + @@ -78,29 +80,29 @@ namespace Examples "VALUES " + "(NOW + 1a, 10.30000, 218, 0.25000) "; var affectedRows = client.Exec(insertQuery); - Console.WriteLine("insert " + affectedRows + " rows to power.meters successfully."); + Console.WriteLine("Successfully inserted " + affectedRows + " rows to power.meters."); } catch (TDengineError e) { // handle TDengine error - Console.WriteLine("Failed to insert data to power.meters; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); + Console.WriteLine("Failed to insert data to power.meters, url:" + connectionString + "; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); throw; } catch (Exception e) { // handle other exceptions - Console.WriteLine("Failed to insert data to power.meters; Err:" + e.Message); + Console.WriteLine("Failed to insert data to power.meters, url:" + connectionString + "; ErrMessage: " + e.Message); throw; } // ANCHOR_END: insert_data } - private static void QueryData(ITDengineClient client) + private static void QueryData(ITDengineClient client,string connectionString) { // ANCHOR: select_data try { - // query data + // query data, make sure the database and table are created before var query = "SELECT ts, current, location FROM power.meters limit 100"; using (var rows = client.Query(query)) { @@ -117,27 +119,28 @@ namespace Examples catch (TDengineError e) { // handle TDengine error - Console.WriteLine("Failed to query data from power.meters; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); + Console.WriteLine("Failed to query data from power.meters, url:" + connectionString + "; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); throw; } catch (Exception e) { // handle other exceptions - Console.WriteLine("Failed to query data from power.meters; Err:" + e.Message); + Console.WriteLine("Failed to query data from power.meters, url:" + connectionString + "; ErrMessage: " + e.Message); throw; } // ANCHOR_END: select_data } - private static void QueryWithReqId(ITDengineClient client) + private static void QueryWithReqId(ITDengineClient client,string connectionString) { // ANCHOR: query_id + var reqId = (long)3; try { // query data var query = "SELECT ts, current, location FROM power.meters limit 1"; // query with request id 3 - using (var rows = client.Query(query,3)) + using (var rows = client.Query(query,reqId)) { while (rows.Read()) { @@ -152,13 +155,13 @@ namespace Examples catch (TDengineError e) { // handle TDengine error - Console.WriteLine("Failed to execute sql with reqId; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); + Console.WriteLine("Failed to execute sql with reqId: " + reqId + ", url:" + connectionString + "; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); throw; } catch (Exception e) { // handle other exceptions - Console.WriteLine("Failed to execute sql with reqId; Err:" + e.Message); + Console.WriteLine("Failed to execute sql with reqId: " + reqId + ", url:" + connectionString + "; ErrMessage: " + e.Message); throw; } // ANCHOR_END: query_id diff --git a/docs/examples/csharp/wsStmt/Program.cs b/docs/examples/csharp/wsStmt/Program.cs index 139a423388..394165f109 100644 --- a/docs/examples/csharp/wsStmt/Program.cs +++ b/docs/examples/csharp/wsStmt/Program.cs @@ -13,9 +13,10 @@ namespace Examples var numOfSubTable = 10; var numOfRow = 10; var random = new Random(); + var connectionString = $"protocol=WebSocket;host={host};port=6041;useSSL=false;username=root;password=taosdata"; try { - var builder = new ConnectionStringBuilder($"protocol=WebSocket;host={host};port=6041;useSSL=false;username=root;password=taosdata"); + var builder = new ConnectionStringBuilder(connectionString); using (var client = DbDriver.Open(builder)) { // create database @@ -54,7 +55,7 @@ namespace Examples stmt.Exec(); // get affected rows var affectedRows = stmt.Affected(); - Console.WriteLine($"table {tableName} insert {affectedRows} rows."); + Console.WriteLine($"Successfully inserted {affectedRows} rows to {tableName}."); } } } @@ -62,13 +63,13 @@ namespace Examples catch (TDengineError e) { // handle TDengine error - Console.WriteLine("Failed to insert to table meters using stmt; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); + Console.WriteLine("Failed to insert to table meters using stmt, url: " + connectionString + "; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); throw; } catch (Exception e) { // handle other exceptions - Console.WriteLine("Failed to insert to table meters using stmt; Err:" + e.Message); + Console.WriteLine("Failed to insert to table meters using stmt, url: " + connectionString + "; ErrMessage: " + e.Message); throw; } } diff --git a/docs/examples/csharp/wssml/Program.cs b/docs/examples/csharp/wssml/Program.cs index 1eac06ed08..94ee86919b 100644 --- a/docs/examples/csharp/wssml/Program.cs +++ b/docs/examples/csharp/wssml/Program.cs @@ -29,26 +29,29 @@ namespace TDengineExample // use database client.Exec("USE power"); // insert influx line protocol data - client.SchemalessInsert(new[]{lineDemo}, TDengineSchemalessProtocol.TSDB_SML_LINE_PROTOCOL, + client.SchemalessInsert(new[] { lineDemo }, TDengineSchemalessProtocol.TSDB_SML_LINE_PROTOCOL, TDengineSchemalessPrecision.TSDB_SML_TIMESTAMP_MILLI_SECONDS, 0, ReqId.GetReqId()); // insert opentsdb telnet protocol data - client.SchemalessInsert(new[]{telnetDemo}, TDengineSchemalessProtocol.TSDB_SML_TELNET_PROTOCOL, + client.SchemalessInsert(new[] { telnetDemo }, TDengineSchemalessProtocol.TSDB_SML_TELNET_PROTOCOL, TDengineSchemalessPrecision.TSDB_SML_TIMESTAMP_MILLI_SECONDS, 0, ReqId.GetReqId()); // insert json data - client.SchemalessInsert(new []{jsonDemo}, TDengineSchemalessProtocol.TSDB_SML_JSON_PROTOCOL, + client.SchemalessInsert(new[] { jsonDemo }, TDengineSchemalessProtocol.TSDB_SML_JSON_PROTOCOL, TDengineSchemalessPrecision.TSDB_SML_TIMESTAMP_NOT_CONFIGURED, 0, ReqId.GetReqId()); } + + Console.WriteLine("Inserted data with schemaless successfully."); } catch (TDengineError e) { // handle TDengine error - Console.WriteLine("Failed to insert data with schemaless; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); + Console.WriteLine("Failed to insert data with schemaless, host:" + host + "; ErrCode:" + e.Code + + "; ErrMessage: " + e.Error); throw; } catch (Exception e) { // handle other exceptions - Console.WriteLine("Failed to insert data with schemaless; Err:" + e.Message); + Console.WriteLine("Failed to insert data with schemaless, host:" + host + "; ErrMessage: " + e.Message); throw; } } diff --git a/docs/examples/csharp/wssubscribe/Program.cs b/docs/examples/csharp/wssubscribe/Program.cs index 4f242e12e2..cfde01920c 100644 --- a/docs/examples/csharp/wssubscribe/Program.cs +++ b/docs/examples/csharp/wssubscribe/Program.cs @@ -10,7 +10,9 @@ namespace TMQExample { try { - var builder = new ConnectionStringBuilder("protocol=WebSocket;host=127.0.0.1;port=6041;username=root;password=taosdata"); + var builder = + new ConnectionStringBuilder( + "protocol=WebSocket;host=127.0.0.1;port=6041;username=root;password=taosdata"); using (var client = DbDriver.Open(builder)) { client.Exec("CREATE DATABASE IF NOT EXISTS power"); @@ -48,7 +50,9 @@ namespace TMQExample static void InsertData() { - var builder = new ConnectionStringBuilder("protocol=WebSocket;host=127.0.0.1;port=6041;username=root;password=taosdata"); + var builder = + new ConnectionStringBuilder( + "protocol=WebSocket;host=127.0.0.1;port=6041;username=root;password=taosdata"); using (var client = DbDriver.Open(builder)) { while (true) @@ -64,17 +68,20 @@ namespace TMQExample { // ANCHOR: create_consumer // consumer config + var host = "127.0.0.1"; + var groupId = "group1"; + var clientId = "client1"; var cfg = new Dictionary() { - {"td.connect.type", "WebSocket"}, + { "td.connect.type", "WebSocket" }, { "td.connect.port", "6041" }, { "auto.offset.reset", "latest" }, { "msg.with.table.name", "true" }, { "enable.auto.commit", "true" }, { "auto.commit.interval.ms", "1000" }, - { "group.id", "group1" }, - { "client.id", "client1" }, - { "td.connect.ip", "127.0.0.1" }, + { "group.id", groupId }, + { "client.id", clientId }, + { "td.connect.ip", host }, { "td.connect.user", "root" }, { "td.connect.pass", "taosdata" }, }; @@ -83,17 +90,20 @@ namespace TMQExample { // create consumer consumer = new ConsumerBuilder>(cfg).Build(); + Console.WriteLine("Create consumer successfully, host: " + host + ", groupId: " + groupId + + ", clientId: " + clientId); } catch (TDengineError e) { // handle TDengine error - Console.WriteLine("Failed to create consumer; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); + Console.WriteLine("Failed to create websocket consumer, host : " + host + "; ErrCode:" + e.Code + + "; ErrMessage: " + e.Error); throw; } catch (Exception e) { // handle other exceptions - Console.WriteLine("Failed to create consumer; Err:" + e.Message); + Console.WriteLine("Failed to create websocket consumer, host : " + host + "; ErrMessage: " + e.Message); throw; } @@ -108,6 +118,7 @@ namespace TMQExample { // subscribe consumer.Subscribe(new List() { "topic_meters" }); + Console.WriteLine("subscribe topics successfully"); for (int i = 0; i < 50; i++) { // consume message with using block to ensure the result is disposed @@ -118,7 +129,7 @@ namespace TMQExample { // handle message Console.WriteLine( - $"data {{{((DateTime)message.Value["ts"]).ToString("yyyy-MM-dd HH:mm:ss.fff")}, " + + $"data: {{{((DateTime)message.Value["ts"]).ToString("yyyy-MM-dd HH:mm:ss.fff")}, " + $"{message.Value["current"]}, {message.Value["voltage"]}, {message.Value["phase"]}}}"); } } @@ -133,7 +144,7 @@ namespace TMQExample catch (Exception e) { // handle other exceptions - Console.WriteLine("Failed to poll data; Err:" + e.Message); + Console.WriteLine("Failed to poll data; ErrMessage:" + e.Message); throw; } // ANCHOR_END: subscribe @@ -146,6 +157,7 @@ namespace TMQExample { // get assignment var assignment = consumer.Assignment; + Console.WriteLine($"now assignment: ${assignment}"); // seek to the beginning foreach (var topicPartition in assignment) { @@ -166,6 +178,7 @@ namespace TMQExample $"second data polled: {{{((DateTime)message.Value["ts"]).ToString("yyyy-MM-dd HH:mm:ss.fff")}, " + $"{message.Value["current"]}, {message.Value["voltage"]}, {message.Value["phase"]}}}"); } + break; } } @@ -173,13 +186,13 @@ namespace TMQExample catch (TDengineError e) { // handle TDengine error - Console.WriteLine("Failed to seek; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); + Console.WriteLine("seek example failed; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); throw; } catch (Exception e) { // handle other exceptions - Console.WriteLine("Failed to seek; Err:" + e.Message); + Console.WriteLine("seek example failed; ErrMessage: " + e.Message); throw; } // ANCHOR_END: seek @@ -201,18 +214,19 @@ namespace TMQExample { cr.TopicPartitionOffset, }); + Console.WriteLine("commit offset manually successfully."); } } catch (TDengineError e) { // handle TDengine error - Console.WriteLine("Failed to commit offset; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); + Console.WriteLine("Failed to execute consumer functions. ErrCode:" + e.Code + "; ErrMessage: " + e.Error); throw; } catch (Exception e) { // handle other exceptions - Console.WriteLine("Failed to commit offset; Err:" + e.Message); + Console.WriteLine("Failed to execute consumer functions. ErrMessage:" + e.Message); throw; } } @@ -230,19 +244,20 @@ namespace TMQExample catch (TDengineError e) { // handle TDengine error - Console.WriteLine("Failed to unsubscribe consumer; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); + Console.WriteLine("Failed to unsubscribe consumer. ErrCode:" + e.Code + "; ErrMessage: " + e.Error); throw; } catch (Exception e) { // handle other exceptions - Console.WriteLine("Failed to unsubscribe consumer; Err:" + e.Message); + Console.WriteLine("Failed to unsubscribe consumer. Err: " + e.Message); throw; } finally { // close consumer consumer.Close(); + Console.WriteLine("consumer closed successfully."); } // ANCHOR_END: close } diff --git a/docs/examples/go/connect/cgoexample/main.go b/docs/examples/go/connect/cgoexample/main.go index 02dbb6ab85..eb5d270e93 100644 --- a/docs/examples/go/connect/cgoexample/main.go +++ b/docs/examples/go/connect/cgoexample/main.go @@ -15,7 +15,7 @@ func main() { var taosDSN = "root:taosdata@tcp(localhost:6030)/" taos, err := sql.Open("taosSql", taosDSN) if err != nil { - log.Fatalln("failed to connect TDengine, err:", err) + log.Fatalln("Failed to connect to " + taosDSN + "; ErrMessage: " + err.Error()) } fmt.Println("Connected to " + taosDSN + " successfully.") defer taos.Close() diff --git a/docs/examples/go/connect/restexample/main.go b/docs/examples/go/connect/restexample/main.go index a76091a4a3..aeee04c6a9 100644 --- a/docs/examples/go/connect/restexample/main.go +++ b/docs/examples/go/connect/restexample/main.go @@ -15,7 +15,7 @@ func main() { var taosDSN = "root:taosdata@http(localhost:6041)/" taos, err := sql.Open("taosRestful", taosDSN) if err != nil { - log.Fatalln("failed to connect TDengine, err:", err) + log.Fatalln("Failed to connect to " + taosDSN + "; ErrMessage: " + err.Error()) } fmt.Println("Connected to " + taosDSN + " successfully.") defer taos.Close() diff --git a/docs/examples/go/connect/wsexample/main.go b/docs/examples/go/connect/wsexample/main.go index 5c5abf0796..57b6c8dd1f 100644 --- a/docs/examples/go/connect/wsexample/main.go +++ b/docs/examples/go/connect/wsexample/main.go @@ -15,7 +15,7 @@ func main() { var taosDSN = "root:taosdata@ws(localhost:6041)/" taos, err := sql.Open("taosWS", taosDSN) if err != nil { - log.Fatalln("failed to connect TDengine, err:", err) + log.Fatalln("Failed to connect to " + taosDSN + "; ErrMessage: " + err.Error()) } fmt.Println("Connected to " + taosDSN + " successfully.") defer taos.Close() diff --git a/docs/examples/go/queryreqid/main.go b/docs/examples/go/queryreqid/main.go index f43a5b5c44..045f0f8901 100644 --- a/docs/examples/go/queryreqid/main.go +++ b/docs/examples/go/queryreqid/main.go @@ -11,19 +11,21 @@ import ( ) func main() { - db, err := sql.Open("taosSql", "root:taosdata@tcp(localhost:6030)/") + taosDSN := "root:taosdata@tcp(localhost:6030)/" + db, err := sql.Open("taosSql", taosDSN) if err != nil { - log.Fatal("Open database error: ", err) + log.Fatalln("Failed to connect to " + taosDSN + "; ErrMessage: " + err.Error()) } defer db.Close() initEnv(db) // ANCHOR: query_id // use context to set request id - ctx := context.WithValue(context.Background(), "taos_req_id", int64(3)) + reqId := int64(3) + ctx := context.WithValue(context.Background(), "taos_req_id", reqId) // execute query with context rows, err := db.QueryContext(ctx, "SELECT ts, current, location FROM power.meters limit 1") if err != nil { - log.Fatal("Query error: ", err) + log.Fatalf("Failed to execute sql with reqId: %d, url: %s; ErrMessage: %s\n", reqId, taosDSN, err.Error()) } for rows.Next() { var ( diff --git a/docs/examples/go/schemaless/native/main.go b/docs/examples/go/schemaless/native/main.go index acb5b95c89..6caa8bb4cb 100644 --- a/docs/examples/go/schemaless/native/main.go +++ b/docs/examples/go/schemaless/native/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log" "github.com/taosdata/driver-go/v3/af" @@ -14,30 +15,31 @@ func main() { conn, err := af.Open(host, "root", "taosdata", "", 0) if err != nil { - log.Fatal("failed to connect TDengine, err:", err) + log.Fatalln("Failed to connect to host: " + host + "; ErrMessage: " + err.Error()) } defer conn.Close() _, err = conn.Exec("CREATE DATABASE IF NOT EXISTS power") if err != nil { - log.Fatal("failed to create database, err:", err) + log.Fatalln("Failed to create db host: " + host + "; ErrMessage: " + err.Error()) } _, err = conn.Exec("USE power") if err != nil { - log.Fatal("failed to use database, err:", err) + log.Fatalln("Failed to use db host: " + host + "; ErrMessage: " + err.Error()) } // insert influxdb line protocol err = conn.InfluxDBInsertLines([]string{lineDemo}, "ms") if err != nil { - log.Fatal("failed to insert influxdb line protocol, err:", err) + log.Fatalln("Failed to insert data with schemaless, host: " + host + "; ErrMessage: " + err.Error()) } // insert opentsdb telnet protocol err = conn.OpenTSDBInsertTelnetLines([]string{telnetDemo}) if err != nil { - log.Fatal("failed to insert opentsdb telnet line protocol, err:", err) + log.Fatalln("Failed to insert data with schemaless, host: " + host + "; ErrMessage: " + err.Error()) } // insert opentsdb json protocol err = conn.OpenTSDBInsertJsonPayload(jsonDemo) if err != nil { - log.Fatal("failed to insert opentsdb json format protocol, err:", err) + log.Fatalln("Failed to insert data with schemaless, host: " + host + "; ErrMessage: " + err.Error()) } + fmt.Println("Inserted data with schemaless successfully.") } diff --git a/docs/examples/go/schemaless/ws/main.go b/docs/examples/go/schemaless/ws/main.go index 84280fc0fa..8507a70811 100644 --- a/docs/examples/go/schemaless/ws/main.go +++ b/docs/examples/go/schemaless/ws/main.go @@ -17,14 +17,15 @@ func main() { telnetDemo := "metric_telnet 1707095283260 4 host=host0 interface=eth0" jsonDemo := "{\"metric\": \"metric_json\",\"timestamp\": 1626846400,\"value\": 10.3, \"tags\": {\"groupid\": 2, \"location\": \"California.SanFrancisco\", \"id\": \"d1001\"}}" - db, err := sql.Open("taosWS", fmt.Sprintf("root:taosdata@ws(%s:6041)/", host)) + taosDSN := fmt.Sprintf("root:taosdata@ws(%s:6041)/", host) + db, err := sql.Open("taosWS", taosDSN) if err != nil { - log.Fatal("failed to connect TDengine, err:", err) + log.Fatalln("Failed to connect to host: " + host + "; ErrMessage: " + err.Error()) } defer db.Close() _, err = db.Exec("CREATE DATABASE IF NOT EXISTS power") if err != nil { - log.Fatal("failed to create database, err:", err) + log.Fatalln("Failed to create db host: " + host + "; ErrMessage: " + err.Error()) } s, err := schemaless.NewSchemaless(schemaless.NewConfig("ws://localhost:6041", 1, schemaless.SetDb("power"), @@ -34,21 +35,22 @@ func main() { schemaless.SetPassword("taosdata"), )) if err != nil { - log.Fatal("failed to create schemaless connection, err:", err) + log.Fatalln("Failed to connect to host: " + host + "; ErrMessage: " + err.Error()) } // insert influxdb line protocol err = s.Insert(lineDemo, schemaless.InfluxDBLineProtocol, "ms", 0, common.GetReqID()) if err != nil { - log.Fatal("failed to insert influxdb line protocol, err:", err) + log.Fatalln("Failed to insert data with schemaless, host:" + host + "; ErrMessage: " + err.Error()) } // insert opentsdb telnet line protocol err = s.Insert(telnetDemo, schemaless.OpenTSDBTelnetLineProtocol, "ms", 0, common.GetReqID()) if err != nil { - log.Fatal("failed to insert opentsdb telnet line protocol, err:", err) + log.Fatalln("Failed to insert data with schemaless, host: " + host + "; ErrMessage: " + err.Error()) } // insert opentsdb json format protocol err = s.Insert(jsonDemo, schemaless.OpenTSDBJsonFormatProtocol, "s", 0, common.GetReqID()) if err != nil { - log.Fatal("failed to insert opentsdb json format protocol, err:", err) + log.Fatalln("Failed to insert data with schemaless, host: " + host + "; ErrMessage: " + err.Error()) } + fmt.Println("Inserted data with schemaless successfully.") } diff --git a/docs/examples/go/sqlquery/main.go b/docs/examples/go/sqlquery/main.go index 19d0029bfa..e03c2a14c4 100644 --- a/docs/examples/go/sqlquery/main.go +++ b/docs/examples/go/sqlquery/main.go @@ -10,39 +10,35 @@ import ( ) func main() { - db, err := sql.Open("taosSql", "root:taosdata@tcp(localhost:6030)/") + var taosDSN = "root:taosdata@tcp(localhost:6030)/" + db, err := sql.Open("taosSql", taosDSN) if err != nil { - log.Fatal("open database failed:", err) + log.Fatalln("Failed to connect to " + taosDSN + "; ErrMessage: " + err.Error()) } defer db.Close() // ANCHOR: create_db_and_table // create database res, err := db.Exec("CREATE DATABASE IF NOT EXISTS power") if err != nil { - log.Fatal("create database failed:", err) + log.Fatalln("Failed to create db, url:" + taosDSN + "; ErrMessage: " + err.Error()) } - affected, err := res.RowsAffected() + rowsAffected, err := res.RowsAffected() if err != nil { - log.Fatal("get affected rows failed:", err) + log.Fatalln("Failed to get create db rowsAffected, url:" + taosDSN + "; ErrMessage: " + err.Error()) } - fmt.Println("create database affected:", affected) - // use database - res, err = db.Exec("USE power") - if err != nil { - log.Fatal("use database failed:", err) - } - affected, err = res.RowsAffected() - if err != nil { - log.Fatal("get affected rows failed:", err) - } - fmt.Println("use database affected:", affected) + // you can check rowsAffected here + fmt.Println("Create database power successfully, rowsAffected: ", rowsAffected) // create table res, err = db.Exec("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))") - affected, err = res.RowsAffected() if err != nil { - log.Fatal("create table failed:", err) + log.Fatalln("Failed to create db and table, url:" + taosDSN + "; ErrMessage: " + err.Error()) } - fmt.Println("create table affected:", affected) + rowsAffected, err = res.RowsAffected() + if err != nil { + log.Fatalln("Failed to get create create rowsAffected, url:" + taosDSN + "; ErrMessage: " + err.Error()) + } + // you can check rowsAffected here + fmt.Println("Create stable power.meters successfully, rowsAffected:", rowsAffected) // ANCHOR_END: create_db_and_table // ANCHOR: insert_data // insert data, please make sure the database and table are created before @@ -57,14 +53,14 @@ func main() { "(NOW + 1a, 10.30000, 218, 0.25000) " res, err = db.Exec(insertQuery) if err != nil { - log.Fatal("insert data failed:", err) + log.Fatal("Failed to insert data to power.meters, url:" + taosDSN + "; ErrMessage: " + err.Error()) } - affected, err = res.RowsAffected() + rowsAffected, err = res.RowsAffected() if err != nil { - log.Fatal("get affected rows failed:", err) + log.Fatal("Failed to get insert rowsAffected, url:" + taosDSN + "; ErrMessage: " + err.Error()) } // you can check affectedRows here - fmt.Println("insert data affected:", affected) + fmt.Printf("Successfully inserted %d rows to power.meters.\n", rowsAffected) // ANCHOR_END: insert_data // ANCHOR: select_data // query data, make sure the database and table are created before diff --git a/docs/examples/go/stmt/native/main.go b/docs/examples/go/stmt/native/main.go index 6fec28969b..46db6eee46 100644 --- a/docs/examples/go/stmt/native/main.go +++ b/docs/examples/go/stmt/native/main.go @@ -17,28 +17,28 @@ func main() { numOfRow := 10 db, err := af.Open(host, "root", "taosdata", "", 0) if err != nil { - log.Fatal("failed to connect TDengine, err:", err) + log.Fatalln("Failed to connect to " + host + "; ErrMessage: " + err.Error()) } defer db.Close() // prepare database and table _, err = db.Exec("CREATE DATABASE IF NOT EXISTS power") if err != nil { - log.Fatal("failed to create database, err:", err) + log.Fatalln("Failed to create db, host: " + host + "; ErrMessage: " + err.Error()) } _, err = db.Exec("USE power") if err != nil { - log.Fatal("failed to use database, err:", err) + log.Fatalln("Failed to use db, host: " + host + "; ErrMessage: " + err.Error()) } _, err = db.Exec("CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))") if err != nil { - log.Fatal("failed to create table, err:", err) + log.Fatalln("Failed to create table, host: " + host + "; ErrMessage: " + err.Error()) } // prepare statement sql := "INSERT INTO ? USING meters TAGS(?,?) VALUES (?,?,?,?)" stmt := db.Stmt() err = stmt.Prepare(sql) if err != nil { - log.Fatal("failed to prepare statement, err:", err) + log.Fatalln("Failed to prepare sql, host: " + host + "; ErrMessage: " + err.Error()) } for i := 1; i <= numOfSubTable; i++ { tableName := fmt.Sprintf("d_bind_%d", i) @@ -46,7 +46,7 @@ func main() { // set tableName and tags err = stmt.SetTableNameWithTags(tableName, tags) if err != nil { - log.Fatal("failed to set table name and tags, err:", err) + log.Fatalln("Failed to set table name and tags, host: " + host + "; ErrMessage: " + err.Error()) } // bind column data current := time.Now() @@ -58,23 +58,23 @@ func main() { AddFloat(rand.Float32()) err = stmt.BindRow(row) if err != nil { - log.Fatal("failed to bind row, err:", err) + log.Fatalln("Failed to bind params, host: " + host + "; ErrMessage: " + err.Error()) } } // add batch err = stmt.AddBatch() if err != nil { - log.Fatal("failed to add batch, err:", err) + log.Fatalln("Failed to add batch, host: " + host + "; ErrMessage: " + err.Error()) } // execute batch err = stmt.Execute() if err != nil { - log.Fatal("failed to execute batch, err:", err) + log.Fatalln("Failed to exec, host: " + host + "; ErrMessage: " + err.Error()) } // get affected rows affected := stmt.GetAffectedRows() // you can check exeResult here - fmt.Printf("table %s insert %d rows.\n", tableName, affected) + fmt.Printf("Successfully inserted %d rows to %s.\n", affected, tableName) } err = stmt.Close() if err != nil { diff --git a/docs/examples/go/stmt/ws/main.go b/docs/examples/go/stmt/ws/main.go index 4873e7df91..ae224c704f 100644 --- a/docs/examples/go/stmt/ws/main.go +++ b/docs/examples/go/stmt/ws/main.go @@ -17,19 +17,21 @@ func main() { host := "127.0.0.1" numOfSubTable := 10 numOfRow := 10 - db, err := sql.Open("taosRestful", fmt.Sprintf("root:taosdata@http(%s:6041)/", host)) + + taosDSN := fmt.Sprintf("root:taosdata@http(%s:6041)/", host) + db, err := sql.Open("taosRestful", taosDSN) if err != nil { - log.Fatal("failed to connect TDengine, err:", err) + log.Fatalln("Failed to connect to " + taosDSN + "; ErrMessage: " + err.Error()) } defer db.Close() // prepare database and table _, err = db.Exec("CREATE DATABASE IF NOT EXISTS power") if err != nil { - log.Fatal("failed to create database, err:", err) + log.Fatalln("Failed to create db, url: " + taosDSN + "; ErrMessage: " + err.Error()) } _, err = db.Exec("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))") if err != nil { - log.Fatal("failed to create table, err:", err) + log.Fatalln("Failed to create table, url: " + taosDSN + "; ErrMessage: " + err.Error()) } config := stmt.NewConfig(fmt.Sprintf("ws://%s:6041", host), 0) @@ -41,17 +43,17 @@ func main() { connector, err := stmt.NewConnector(config) if err != nil { - log.Fatal("failed to create stmt connector, err:", err) + log.Fatalln("Failed to create stmt connector,url: " + taosDSN + "; ErrMessage: " + err.Error()) } // prepare statement sql := "INSERT INTO ? USING meters TAGS(?,?) VALUES (?,?,?,?)" stmt, err := connector.Init() if err != nil { - log.Fatal("failed to init stmt, err:", err) + log.Fatalln("Failed to init stmt, url: " + taosDSN + "; ErrMessage: " + err.Error()) } err = stmt.Prepare(sql) if err != nil { - log.Fatal("failed to prepare stmt, err:", err) + log.Fatal("Failed to prepare sql, url: " + taosDSN + "; ErrMessage: " + err.Error()) } for i := 1; i <= numOfSubTable; i++ { tableName := fmt.Sprintf("d_bind_%d", i) @@ -61,12 +63,12 @@ func main() { // set tableName err = stmt.SetTableName(tableName) if err != nil { - log.Fatal("failed to set table name, err:", err) + log.Fatal("Failed to set table name, url: " + taosDSN + "; ErrMessage: " + err.Error()) } // set tags err = stmt.SetTags(tags, tagsType) if err != nil { - log.Fatal("failed to set tags, err:", err) + log.Fatal("Failed to set tags, url: " + taosDSN + "; ErrMessage: " + err.Error()) } // bind column data current := time.Now() @@ -78,26 +80,26 @@ func main() { columnData[3] = param.NewParam(1).AddFloat(rand.Float32()) err = stmt.BindParam(columnData, columnType) if err != nil { - log.Fatal("failed to bind param, err:", err) + log.Fatal("Failed to bind params, url: " + taosDSN + "; ErrMessage: " + err.Error()) } } // add batch err = stmt.AddBatch() if err != nil { - log.Fatal("failed to add batch, err:", err) + log.Fatal("Failed to add batch, url: " + taosDSN + "; ErrMessage: " + err.Error()) } // execute batch err = stmt.Exec() if err != nil { - log.Fatal("failed to exec stmt, err:", err) + log.Fatal("Failed to exec, url: " + taosDSN + "; ErrMessage: " + err.Error()) } // get affected rows affected := stmt.GetAffectedRows() // you can check exeResult here - fmt.Printf("table %s insert %d rows.\n", tableName, affected) + fmt.Printf("Successfully inserted %d rows to %s.\n", affected, tableName) } err = stmt.Close() if err != nil { - log.Fatal("failed to close stmt, err:", err) + log.Fatal("Failed to close stmt, url: " + taosDSN + "; ErrMessage: " + err.Error()) } } diff --git a/docs/examples/go/tmq/native/main.go b/docs/examples/go/tmq/native/main.go index 299d5ada07..b5f7c8c103 100644 --- a/docs/examples/go/tmq/native/main.go +++ b/docs/examples/go/tmq/native/main.go @@ -15,9 +15,10 @@ var done = make(chan struct{}) func main() { // init env - conn, err := sql.Open("taosSql", "root:taosdata@tcp(127.0.0.1:6030)/") + taosDSN := "root:taosdata@tcp(127.0.0.1:6030)/" + conn, err := sql.Open("taosSql", taosDSN) if err != nil { - log.Fatal("failed to connect TDengine, err:", err) + log.Fatalln("Failed to connect to " + taosDSN + "; ErrMessage: " + err.Error()) } defer func() { conn.Close() @@ -25,6 +26,9 @@ func main() { initEnv(conn) // ANCHOR: create_consumer // create consumer + groupID := "group1" + clientID := "client1" + host := "127.0.0.1" consumer, err := tmq.NewConsumer(&tmqcommon.ConfigMap{ "td.connect.user": "root", "td.connect.pass": "taosdata", @@ -32,18 +36,21 @@ func main() { "msg.with.table.name": "true", "enable.auto.commit": "true", "auto.commit.interval.ms": "1000", - "group.id": "group1", - "client.id": "client1", + "group.id": groupID, + "client.id": clientID, }) if err != nil { - log.Fatal("failed to create consumer, err:", err) + log.Fatalln("Failed to create native consumer, host : " + host + "; ErrMessage: " + err.Error()) } + log.Println("Create consumer successfully, host: " + host + ", groupId: " + groupID + ", clientId: " + clientID) + // ANCHOR_END: create_consumer // ANCHOR: subscribe err = consumer.Subscribe("topic_meters", nil) if err != nil { - log.Fatal("failed to subscribe, err:", err) + log.Fatalln("Failed to subscribe, host : " + host + "; ErrMessage: " + err.Error()) } + log.Println("subscribe topics successfully") for i := 0; i < 50; i++ { ev := consumer.Poll(100) if ev != nil { @@ -53,23 +60,16 @@ func main() { fmt.Printf("data:%v\n", e) // ANCHOR: commit_offset // commit offset - topicPartition, err := consumer.CommitOffsets([]tmqcommon.TopicPartition{e.TopicPartition}) + _, err = consumer.CommitOffsets([]tmqcommon.TopicPartition{e.TopicPartition}) if err != nil { - log.Fatal("failed to commit offset, err:", err) + log.Fatalln("Failed to commit offset, host : " + host + "; ErrMessage: " + err.Error()) } - fmt.Println(topicPartition) + log.Println("commit offset manually successfully.") // ANCHOR_END: commit_offset case tmqcommon.Error: fmt.Printf("%% Error: %v: %v\n", e.Code(), e) - log.Fatal("failed to get message, err:", e) + log.Fatalln("Failed to poll data, host : " + host + "; ErrMessage: " + err.Error()) } - // commit all offsets - topicPartition, err := consumer.Commit() - if err != nil { - log.Fatal("failed to commit, err:", err) - } - fmt.Println(topicPartition) - } } // ANCHOR_END: subscribe @@ -79,8 +79,8 @@ func main() { if err != nil { log.Fatal("failed to get assignment, err:", err) } + fmt.Println("now assignment:", partitions) for i := 0; i < len(partitions); i++ { - fmt.Println(partitions[i]) // seek to the beginning err = consumer.Seek(tmqcommon.TopicPartition{ Topic: partitions[i].Topic, @@ -88,7 +88,7 @@ func main() { Offset: 0, }, 0) if err != nil { - log.Fatal("failed to seek, err:", err) + log.Fatalln("seek example failed; ErrMessage: " + err.Error()) } } fmt.Println("assignment seek to beginning successfully") @@ -116,12 +116,12 @@ func main() { // unsubscribe err = consumer.Unsubscribe() if err != nil { - log.Fatal("failed to unsubscribe, err:", err) + log.Fatal("Failed to unsubscribe consumer. ErrMessage: " + err.Error()) } // close consumer err = consumer.Close() if err != nil { - log.Fatal("failed to close consumer, err:", err) + log.Fatal("Failed to close consumer. ErrMessage: " + err.Error()) } // ANCHOR_END: close <-done diff --git a/docs/examples/go/tmq/ws/main.go b/docs/examples/go/tmq/ws/main.go index e05f863fb5..073efd1bba 100644 --- a/docs/examples/go/tmq/ws/main.go +++ b/docs/examples/go/tmq/ws/main.go @@ -16,9 +16,10 @@ var done = make(chan struct{}) func main() { // init env - conn, err := sql.Open("taosWS", "root:taosdata@ws(127.0.0.1:6041)/") + taosDSN := "root:taosdata@ws(127.0.0.1:6041)/" + conn, err := sql.Open("taosWS", taosDSN) if err != nil { - log.Fatal("failed to connect TDengine, err:", err) + log.Fatalln("Failed to connect to " + taosDSN + "; ErrMessage: " + err.Error()) } defer func() { conn.Close() @@ -26,8 +27,11 @@ func main() { initEnv(conn) // ANCHOR: create_consumer // create consumer + wsUrl := "ws://127.0.0.1:6041" + groupID := "group1" + clientID := "client1" consumer, err := tmq.NewConsumer(&tmqcommon.ConfigMap{ - "ws.url": "ws://127.0.0.1:6041", + "ws.url": wsUrl, "ws.message.channelLen": uint(0), "ws.message.timeout": common.DefaultMessageTimeout, "ws.message.writeWait": common.DefaultWriteWait, @@ -37,18 +41,21 @@ func main() { "msg.with.table.name": "true", "enable.auto.commit": "true", "auto.commit.interval.ms": "1000", - "group.id": "group1", - "client.id": "client1", + "group.id": groupID, + "client.id": clientID, }) if err != nil { - log.Fatal("failed to create consumer, err:", err) + log.Fatalln("Failed to create websocket consumer, host : " + wsUrl + "; ErrMessage: " + err.Error()) } + log.Println("Create consumer successfully, host: " + wsUrl + ", groupId: " + groupID + ", clientId: " + clientID) + // ANCHOR_END: create_consumer // ANCHOR: subscribe err = consumer.Subscribe("topic_meters", nil) if err != nil { - log.Fatal("failed to subscribe, err:", err) + log.Fatalln("Failed to subscribe, host : " + wsUrl + "; ErrMessage: " + err.Error()) } + log.Println("subscribe topics successfully") for i := 0; i < 50; i++ { ev := consumer.Poll(100) if ev != nil { @@ -58,23 +65,16 @@ func main() { fmt.Printf("data:%v\n", e) // ANCHOR: commit_offset // commit offset - topicPartition, err := consumer.CommitOffsets([]tmqcommon.TopicPartition{e.TopicPartition}) + _, err = consumer.CommitOffsets([]tmqcommon.TopicPartition{e.TopicPartition}) if err != nil { - log.Fatal("failed to commit offset, err:", err) + log.Fatalln("Failed to commit offset, host : " + wsUrl + "; ErrMessage: " + err.Error()) } - fmt.Println(topicPartition) + log.Println("commit offset manually successfully.") // ANCHOR_END: commit_offset case tmqcommon.Error: fmt.Printf("%% Error: %v: %v\n", e.Code(), e) - log.Fatal("failed to get message, err:", e) + log.Fatalln("Failed to poll data, host : " + wsUrl + "; ErrMessage: " + err.Error()) } - // commit all offsets - topicPartition, err := consumer.Commit() - if err != nil { - log.Fatal("failed to commit, err:", err) - } - fmt.Println(topicPartition) - } } // ANCHOR_END: subscribe @@ -84,8 +84,8 @@ func main() { if err != nil { log.Fatal("failed to get assignment, err:", err) } + fmt.Println("now assignment:", partitions) for i := 0; i < len(partitions); i++ { - fmt.Println(partitions[i]) // seek to the beginning err = consumer.Seek(tmqcommon.TopicPartition{ Topic: partitions[i].Topic, @@ -93,7 +93,7 @@ func main() { Offset: 0, }, 0) if err != nil { - log.Fatal("failed to seek, err:", err) + log.Fatalln("seek example failed; ErrMessage: " + err.Error()) } } fmt.Println("assignment seek to beginning successfully") @@ -121,12 +121,12 @@ func main() { // unsubscribe err = consumer.Unsubscribe() if err != nil { - log.Fatal("failed to unsubscribe, err:", err) + log.Fatal("Failed to unsubscribe consumer. ErrMessage: " + err.Error()) } // close consumer err = consumer.Close() if err != nil { - log.Fatal("failed to close consumer, err:", err) + log.Fatal("Failed to close consumer. ErrMessage: " + err.Error()) } // ANCHOR_END: close <-done diff --git a/tests/docs-examples-test/go.sh b/tests/docs-examples-test/go.sh index f8716b6bd7..ea19d3212a 100644 --- a/tests/docs-examples-test/go.sh +++ b/tests/docs-examples-test/go.sh @@ -2,6 +2,21 @@ set -e +check_transactions() { + for i in {1..30} + do + output=$(taos -s "show transactions;") + if [[ $output == *"Query OK, 0 row(s)"* ]]; then + echo "Success: No transactions are in progress." + return 0 + fi + sleep 1 + done + + echo "Error: Transactions are still in progress after 30 attempts." + return 1 +} + taosd >>/dev/null 2>&1 & taosadapter >>/dev/null 2>&1 & sleep 1 @@ -19,60 +34,61 @@ taos -s "drop database if exists power" go run ./sqlquery/main.go taos -s "drop database if exists power" -sleep 1 +check_transactions || exit 1 go run ./queryreqid/main.go taos -s "drop database if exists power" -sleep 1 +check_transactions || exit 1 go run ./stmt/native/main.go taos -s "drop database if exists power" -sleep 1 +check_transactions || exit 1 go run ./stmt/ws/main.go taos -s "drop database if exists power" -sleep 1 +check_transactions || exit 1 +sleep 3 go run ./schemaless/native/main.go taos -s "drop database if exists power" -sleep 1 +check_transactions || exit 1 go run ./schemaless/ws/main.go taos -s "drop topic if exists topic_meters" -sleep 1 +check_transactions || exit 1 taos -s "drop database if exists power" -sleep 1 +check_transactions || exit 1 go run ./tmq/native/main.go taos -s "drop topic if exists topic_meters" -sleep 1 +check_transactions || exit 1 taos -s "drop database if exists power" -sleep 1 +check_transactions || exit 1 go run ./tmq/ws/main.go taos -s "drop database if exists test" -sleep 1 +check_transactions || exit 1 go run ./insert/json/main.go taos -s "drop database if exists test" -sleep 1 +check_transactions || exit 1 go run ./insert/line/main.go taos -s "drop topic if exists topic_meters" -sleep 1 +check_transactions || exit 1 taos -s "drop database if exists power" -sleep 1 +check_transactions || exit 1 go run ./insert/sql/main.go taos -s "drop database if exists power" -sleep 1 +check_transactions || exit 1 go run ./insert/stmt/main.go taos -s "drop database if exists test" -sleep 1 +check_transactions || exit 1 go run ./insert/telnet/main.go go run ./query/sync/main.go taos -s "drop topic if exists example_tmq_topic" -sleep 1 +check_transactions || exit 1 taos -s "drop database if exists example_tmq" -sleep 1 +check_transactions || exit 1 go run ./sub/main.go From 4d595d28c1ec8a79a0726aa7e9296074c190e694 Mon Sep 17 00:00:00 2001 From: Yaming Pei Date: Mon, 12 Aug 2024 15:54:49 +0800 Subject: [PATCH 16/39] c language sample optimization --- docs/examples/c/tmq_demo.c | 58 +++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/docs/examples/c/tmq_demo.c b/docs/examples/c/tmq_demo.c index 30cbbdc1c0..6277d61b8a 100644 --- a/docs/examples/c/tmq_demo.c +++ b/docs/examples/c/tmq_demo.c @@ -13,6 +13,8 @@ * along with this program. If not, see . */ +// to compile: gcc -o tmq_demo tmq_demo.c -ltaos -lpthread + #include #include #include @@ -33,9 +35,9 @@ void* prepare_data(void* arg) { uint16_t port = 6030; TAOS *pConn = taos_connect(host, user, password, NULL, port); if (pConn == NULL) { - printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + fprintf(stderr, "Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); - return -1; + return NULL; } TAOS_RES* pRes; @@ -52,12 +54,12 @@ void* prepare_data(void* arg) { pRes = taos_query(pConn, buf); if (taos_errno(pRes) != 0) { - printf("Failed to insert data to power.meters, reason: %s\n", taos_errstr(pRes)); + fprintf(stderr, "Failed to insert data to power.meters, reason: %s\n", taos_errstr(pRes)); } taos_free_result(pRes); sleep(1); } - printf("Prepare data thread exit\n"); + fprintf(stdout, "Prepare data thread exit\n"); return NULL; } @@ -69,9 +71,9 @@ static int32_t msg_process(TAOS_RES* msg) { const char* dbName = tmq_get_db_name(msg); int32_t vgroupId = tmq_get_vgroup_id(msg); - printf("topic: %s\n", topicName); - printf("db: %s\n", dbName); - printf("vgroup id: %d\n", vgroupId); + fprintf(stdout, "topic: %s\n", topicName); + fprintf(stdout, "db: %s\n", dbName); + fprintf(stdout, "vgroup id: %d\n", vgroupId); while (1) { // get one row data from message @@ -87,11 +89,11 @@ static int32_t msg_process(TAOS_RES* msg) { rows++; // print the row content if (taos_print_row(buf, row, fields, numOfFields) < 0) { - printf("Failed to print row\n"); + fprintf(stderr, "Failed to print row\n"); break; } // print the precision and row content to the console - printf("precision: %d, row content: %s\n", precision, buf); + fprintf(stdout, "precision: %d, data: %s\n", precision, buf); } return rows; @@ -105,42 +107,42 @@ static int32_t init_env() { uint16_t port = 6030; TAOS *pConn = taos_connect(host, user, password, NULL, port); if (pConn == NULL) { - printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + fprintf(stderr, "Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } TAOS_RES* pRes; // drop database if exists - printf("create database\n"); + fprintf(stdout, "Create database.\n"); pRes = taos_query(pConn, "drop topic if exists topic_meters"); if (taos_errno(pRes) != 0) { - printf("error in drop topic_meters, reason:%s\n", taos_errstr(pRes)); + fprintf(stderr, "Failed to drop topic_meters, reason:%s\n", taos_errstr(pRes)); } taos_free_result(pRes); pRes = taos_query(pConn, "drop database if exists power"); if (taos_errno(pRes) != 0) { - printf("error in drop power, reason:%s\n", taos_errstr(pRes)); + fprintf(stderr, "Failed to drop database power, reason:%s\n", taos_errstr(pRes)); } taos_free_result(pRes); // create database pRes = taos_query(pConn, "create database power precision 'ms' WAL_RETENTION_PERIOD 3600"); if (taos_errno(pRes) != 0) { - printf("error in create tmqdb, reason:%s\n", taos_errstr(pRes)); + fprintf(stderr, "Failed to create tmqdb, reason:%s\n", taos_errstr(pRes)); goto END; } taos_free_result(pRes); // create super table - printf("create super table\n"); + fprintf(stdout, "Create super table.\n"); pRes = taos_query( pConn, "CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS " "(groupId INT, location BINARY(24))"); if (taos_errno(pRes) != 0) { - printf("failed to create super table meters, reason:%s\n", taos_errstr(pRes)); + fprintf(stderr, "Failed to create super table meters, reason:%s\n", taos_errstr(pRes)); goto END; } @@ -155,7 +157,7 @@ END: } int32_t create_topic() { - printf("create topic\n"); + fprintf(stdout, "Create topic.\n"); TAOS_RES* pRes; const char *host = "localhost"; const char *user = "root"; @@ -163,14 +165,14 @@ int32_t create_topic() { uint16_t port = 6030; TAOS *pConn = taos_connect(host, user, password, NULL, port); if (pConn == NULL) { - printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + fprintf(stderr, "Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } pRes = taos_query(pConn, "use power"); if (taos_errno(pRes) != 0) { - printf("error in use tmqdb, reason:%s\n", taos_errstr(pRes)); + fprintf(stderr, "Failed to use tmqdb, reason:%s\n", taos_errstr(pRes)); return -1; } taos_free_result(pRes); @@ -179,7 +181,7 @@ int32_t create_topic() { pConn, "CREATE TOPIC IF NOT EXISTS topic_meters AS SELECT ts, current, voltage, phase, groupid, location FROM meters"); if (taos_errno(pRes) != 0) { - printf("failed to create topic topic_meters, reason:%s\n", taos_errstr(pRes)); + fprintf(stderr, "Failed to create topic topic_meters, reason:%s\n", taos_errstr(pRes)); return -1; } taos_free_result(pRes); @@ -189,7 +191,7 @@ int32_t create_topic() { } void tmq_commit_cb_print(tmq_t* tmq, int32_t code, void* param) { - printf("tmq_commit_cb_print() code: %d, tmq: %p, param: %p\n", code, tmq, param); + fprintf(stdout, "tmq_commit_cb_print() code: %d, tmq: %p, param: %p\n", code, tmq, param); } // ANCHOR: create_consumer_1 @@ -261,6 +263,7 @@ tmq_list_t* build_topic_list() { if (code) { // if failed, destroy the list and return NULL tmq_list_destroy(topicList); + fprintf(stderr, "Failed to create topic_list, ErrCode: 0x%x; ErrMessage: %s.\n", code, tmq_err2str(code)); return NULL; } // if success, return the list @@ -313,7 +316,9 @@ void consume_repeatly(tmq_t* tmq) { code = tmq_offset_seek(tmq, topic_name, p->vgId, p->begin); if (code != 0) { - fprintf(stderr, "Failed to seek to %d, reason:%s", (int)p->begin, tmq_err2str(code)); + fprintf(stderr, "Failed to seek assignment %d to beginning %ld, reason: %s.\n", i, p->begin, tmq_err2str(code)); + } else { + fprintf(stdout, "Seek assignment %d to beginning %ld successfully.\n", i, p->begin); } } @@ -347,7 +352,7 @@ void manual_commit(tmq_t* tmq) { taos_free_result(tmqmsg); break; } else { - printf("Commit offset manually successfully."); + fprintf(stdout, "Commit offset manually successfully."); } // free the message taos_free_result(tmqmsg); @@ -386,20 +391,21 @@ int main(int argc, char* argv[]) { fprintf(stderr, "Failed to create consumer.\n"); return -1; } - printf("Create consumer successfully.\n"); + fprintf(stdout, "Create consumer successfully.\n"); // ANCHOR_END: create_consumer_2 // ANCHOR: subscribe_3 tmq_list_t* topic_list = build_topic_list(); if (NULL == topic_list) { + fprintf(stderr, "Failed to create topic_list.\n"); return -1; } if ((code = tmq_subscribe(tmq, topic_list))) { - fprintf(stderr, "Failed to tmq_subscribe(): %s\n", tmq_err2str(code)); + fprintf(stderr, "Failed to subscribe tmq_subscribe(): %s\n", tmq_err2str(code)); } else { - printf("Subscribe topics successfully.\n"); + fprintf(stdout, "Subscribe topics successfully.\n"); } tmq_list_destroy(topic_list); From e4f95d6a17904259cdb9986dda2d8c20c30884cf Mon Sep 17 00:00:00 2001 From: gccgdb1234 Date: Mon, 12 Aug 2024 15:56:19 +0800 Subject: [PATCH 17/39] doc: remove obsolete _category_.yml --- docs/zh/06-advanced/05-data-in/_category_.yml | 1 - 1 file changed, 1 deletion(-) delete mode 100644 docs/zh/06-advanced/05-data-in/_category_.yml diff --git a/docs/zh/06-advanced/05-data-in/_category_.yml b/docs/zh/06-advanced/05-data-in/_category_.yml deleted file mode 100644 index 8a7f326b68..0000000000 --- a/docs/zh/06-advanced/05-data-in/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: 数据写入 \ No newline at end of file From 012112bb88d86bdb4177caa59d12690b940158b8 Mon Sep 17 00:00:00 2001 From: gccgdb1234 Date: Mon, 12 Aug 2024 16:19:39 +0800 Subject: [PATCH 18/39] doc: remove _category_.yml --- docs/en/04-get-started/_category_.yml | 1 - docs/en/07-operation/_category_.yml | 1 - docs/en/08-develop/01-connect/_category_.yml | 1 - docs/en/08-develop/02-model/_category_.yml | 2 -- docs/en/08-develop/03-insert-data/_category_.yml | 1 - docs/en/08-develop/04-query-data/_category_.yml | 1 - docs/en/08-develop/_category_.yml | 1 - .../en/14-reference/01-components/03-taosadapter.md | 2 +- docs/en/14-reference/01-components/06-taoskeeper.md | 2 +- docs/en/14-reference/01-components/08-taos-cli.md | 2 +- docs/en/14-reference/01-components/index.md | 13 +++++++++++++ docs/en/14-reference/03-taos-sql/_category_.yml | 1 - docs/en/14-reference/03-taos-sql/index.md | 3 ++- docs/en/14-reference/05-connectors/_category_.yml | 1 - docs/en/14-reference/05-connectors/index.mdx | 3 ++- .../14-reference/09-support-platform/_category_.yml | 1 - docs/en/14-reference/09-support-platform/index.md | 3 ++- docs/en/14-reference/12-config/_category_.yml | 1 - docs/en/14-reference/12-config/index.md | 3 ++- docs/en/14-reference/13-schemaless/13-schemaless.md | 1 + docs/en/14-reference/13-schemaless/_category_.yml | 1 - docs/en/14-reference/_category_.yml | 1 - docs/en/14-reference/index.md | 2 +- docs/en/20-third-party/_category_.yml | 2 -- docs/en/20-third-party/index.md | 1 + docs/en/25-application/_category_.yml | 1 - docs/en/25-application/index.md | 3 ++- docs/en/26-tdinternal/_category_.yml | 1 - docs/en/26-tdinternal/index.md | 1 + docs/en/27-train-faq/_category_.yml | 1 - docs/en/27-train-faq/index.md | 1 + docs/en/28-releases/_category_.yml | 1 - 32 files changed, 31 insertions(+), 29 deletions(-) delete mode 100644 docs/en/04-get-started/_category_.yml delete mode 100644 docs/en/07-operation/_category_.yml delete mode 100644 docs/en/08-develop/01-connect/_category_.yml delete mode 100644 docs/en/08-develop/02-model/_category_.yml delete mode 100644 docs/en/08-develop/03-insert-data/_category_.yml delete mode 100644 docs/en/08-develop/04-query-data/_category_.yml delete mode 100644 docs/en/08-develop/_category_.yml create mode 100644 docs/en/14-reference/01-components/index.md delete mode 100644 docs/en/14-reference/03-taos-sql/_category_.yml delete mode 100644 docs/en/14-reference/05-connectors/_category_.yml delete mode 100644 docs/en/14-reference/09-support-platform/_category_.yml delete mode 100644 docs/en/14-reference/12-config/_category_.yml delete mode 100644 docs/en/14-reference/13-schemaless/_category_.yml delete mode 100644 docs/en/14-reference/_category_.yml delete mode 100644 docs/en/20-third-party/_category_.yml delete mode 100644 docs/en/25-application/_category_.yml delete mode 100644 docs/en/26-tdinternal/_category_.yml delete mode 100644 docs/en/27-train-faq/_category_.yml delete mode 100644 docs/en/28-releases/_category_.yml diff --git a/docs/en/04-get-started/_category_.yml b/docs/en/04-get-started/_category_.yml deleted file mode 100644 index 043ae21554..0000000000 --- a/docs/en/04-get-started/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: Get Started diff --git a/docs/en/07-operation/_category_.yml b/docs/en/07-operation/_category_.yml deleted file mode 100644 index 3231ce910d..0000000000 --- a/docs/en/07-operation/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: Administration diff --git a/docs/en/08-develop/01-connect/_category_.yml b/docs/en/08-develop/01-connect/_category_.yml deleted file mode 100644 index 83f9754f58..0000000000 --- a/docs/en/08-develop/01-connect/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: Connect diff --git a/docs/en/08-develop/02-model/_category_.yml b/docs/en/08-develop/02-model/_category_.yml deleted file mode 100644 index a2b49eb879..0000000000 --- a/docs/en/08-develop/02-model/_category_.yml +++ /dev/null @@ -1,2 +0,0 @@ -label: Data Model - diff --git a/docs/en/08-develop/03-insert-data/_category_.yml b/docs/en/08-develop/03-insert-data/_category_.yml deleted file mode 100644 index e515d60e09..0000000000 --- a/docs/en/08-develop/03-insert-data/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: Insert Data diff --git a/docs/en/08-develop/04-query-data/_category_.yml b/docs/en/08-develop/04-query-data/_category_.yml deleted file mode 100644 index 809db34621..0000000000 --- a/docs/en/08-develop/04-query-data/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: Query Data diff --git a/docs/en/08-develop/_category_.yml b/docs/en/08-develop/_category_.yml deleted file mode 100644 index 6f0d66351a..0000000000 --- a/docs/en/08-develop/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: Developer Guide \ No newline at end of file diff --git a/docs/en/14-reference/01-components/03-taosadapter.md b/docs/en/14-reference/01-components/03-taosadapter.md index 9a65a83e83..b257633d3b 100644 --- a/docs/en/14-reference/01-components/03-taosadapter.md +++ b/docs/en/14-reference/01-components/03-taosadapter.md @@ -1,5 +1,5 @@ --- -title: taosAdapter +title: taosAdapter Reference Guide sidebar_label: taosAdapter description: This document describes how to use taosAdapter, a TDengine companion tool that acts as a bridge and adapter between TDengine clusters and applications. --- diff --git a/docs/en/14-reference/01-components/06-taoskeeper.md b/docs/en/14-reference/01-components/06-taoskeeper.md index 2c12ce5e08..4f8315e48f 100644 --- a/docs/en/14-reference/01-components/06-taoskeeper.md +++ b/docs/en/14-reference/01-components/06-taoskeeper.md @@ -1,6 +1,6 @@ --- sidebar_label: taosKeeper -title: taosKeeper +title: taosKeeper Reference Guide description: This document describes how to use taosKeeper, a tool for exporting TDengine monitoring metrics. --- diff --git a/docs/en/14-reference/01-components/08-taos-cli.md b/docs/en/14-reference/01-components/08-taos-cli.md index 1c4c1103e2..55a7a940ea 100644 --- a/docs/en/14-reference/01-components/08-taos-cli.md +++ b/docs/en/14-reference/01-components/08-taos-cli.md @@ -1,6 +1,6 @@ --- title: TDengine Command Line Interface (CLI) -sidebar_label: Command Line Interface +sidebar_label: TDengine CLI description: This document describes how to use the TDengine CLI. --- diff --git a/docs/en/14-reference/01-components/index.md b/docs/en/14-reference/01-components/index.md new file mode 100644 index 0000000000..c0c9e1894f --- /dev/null +++ b/docs/en/14-reference/01-components/index.md @@ -0,0 +1,13 @@ +--- +title: Components Reference Guide +sidebar_label: Components +--- + +In this section we will explain the major TDengine components, including their functionalities, command line parameters, configurations, etc. + +```mdx-code-block +import DocCardList from '@theme/DocCardList'; +import {useCurrentSidebarCategory} from '@docusaurus/theme-common'; + + +``` \ No newline at end of file diff --git a/docs/en/14-reference/03-taos-sql/_category_.yml b/docs/en/14-reference/03-taos-sql/_category_.yml deleted file mode 100644 index 74a3b6309e..0000000000 --- a/docs/en/14-reference/03-taos-sql/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: TDengine SQL diff --git a/docs/en/14-reference/03-taos-sql/index.md b/docs/en/14-reference/03-taos-sql/index.md index f1871cc6fb..dfe6bc8d9f 100644 --- a/docs/en/14-reference/03-taos-sql/index.md +++ b/docs/en/14-reference/03-taos-sql/index.md @@ -1,5 +1,6 @@ --- -title: TDengine SQL +title: SQL Reference Guide +sidebar_label: SQL description: This document describes the syntax and functions supported by TDengine SQL. --- diff --git a/docs/en/14-reference/05-connectors/_category_.yml b/docs/en/14-reference/05-connectors/_category_.yml deleted file mode 100644 index a70a33caa6..0000000000 --- a/docs/en/14-reference/05-connectors/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: "Client Libraries" diff --git a/docs/en/14-reference/05-connectors/index.mdx b/docs/en/14-reference/05-connectors/index.mdx index c06103ce90..8e224fb1ab 100644 --- a/docs/en/14-reference/05-connectors/index.mdx +++ b/docs/en/14-reference/05-connectors/index.mdx @@ -1,5 +1,6 @@ --- -title: Client Libraries +title: Connector Reference Guide +sidebar_label: Connectors description: This document describes the client libraries that TDengine provides to interface with various programming languages. --- diff --git a/docs/en/14-reference/09-support-platform/_category_.yml b/docs/en/14-reference/09-support-platform/_category_.yml deleted file mode 100644 index fabf2fef69..0000000000 --- a/docs/en/14-reference/09-support-platform/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: supported platforms diff --git a/docs/en/14-reference/09-support-platform/index.md b/docs/en/14-reference/09-support-platform/index.md index bc1133b9a7..19fa490154 100644 --- a/docs/en/14-reference/09-support-platform/index.md +++ b/docs/en/14-reference/09-support-platform/index.md @@ -1,5 +1,6 @@ --- -title: List of supported platforms +title: Supported Platforms +sidebar_label: Supported Platforms description: This document describes the supported platforms for the TDengine server, client, and client libraries. --- diff --git a/docs/en/14-reference/12-config/_category_.yml b/docs/en/14-reference/12-config/_category_.yml deleted file mode 100644 index 94aa943872..0000000000 --- a/docs/en/14-reference/12-config/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: Configuration \ No newline at end of file diff --git a/docs/en/14-reference/12-config/index.md b/docs/en/14-reference/12-config/index.md index 4ec01183da..14a1a0fb9d 100755 --- a/docs/en/14-reference/12-config/index.md +++ b/docs/en/14-reference/12-config/index.md @@ -1,5 +1,6 @@ --- -title: Configuration Parameters +title: Configuration Parmeters +sidebar_label: Configurations description: This document describes the configuration parameters for the TDengine server and client. --- diff --git a/docs/en/14-reference/13-schemaless/13-schemaless.md b/docs/en/14-reference/13-schemaless/13-schemaless.md index b45860976d..a451432217 100644 --- a/docs/en/14-reference/13-schemaless/13-schemaless.md +++ b/docs/en/14-reference/13-schemaless/13-schemaless.md @@ -1,5 +1,6 @@ --- title: Schemaless Writing +sidebar_label: Schemaless description: This document describes how to use the schemaless write component of TDengine. --- diff --git a/docs/en/14-reference/13-schemaless/_category_.yml b/docs/en/14-reference/13-schemaless/_category_.yml deleted file mode 100644 index 416b41f73c..0000000000 --- a/docs/en/14-reference/13-schemaless/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: Schemaless writing diff --git a/docs/en/14-reference/_category_.yml b/docs/en/14-reference/_category_.yml deleted file mode 100644 index 1fb42e60a7..0000000000 --- a/docs/en/14-reference/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: Reference \ No newline at end of file diff --git a/docs/en/14-reference/index.md b/docs/en/14-reference/index.md index d57eee512a..ada0332272 100644 --- a/docs/en/14-reference/index.md +++ b/docs/en/14-reference/index.md @@ -1,5 +1,5 @@ --- -title: Reference +title: Reference Manual description: This document describes TDengine utilities. --- diff --git a/docs/en/20-third-party/_category_.yml b/docs/en/20-third-party/_category_.yml deleted file mode 100644 index e71e65a6a3..0000000000 --- a/docs/en/20-third-party/_category_.yml +++ /dev/null @@ -1,2 +0,0 @@ -label: Third Party Tools - diff --git a/docs/en/20-third-party/index.md b/docs/en/20-third-party/index.md index 6fc8043eef..a3b52eea49 100644 --- a/docs/en/20-third-party/index.md +++ b/docs/en/20-third-party/index.md @@ -1,5 +1,6 @@ --- title: Third Party Tools +sidebar_label: 3rd Party description: This document describes how to integrate TDengine with various third-party tools. --- diff --git a/docs/en/25-application/_category_.yml b/docs/en/25-application/_category_.yml deleted file mode 100644 index 772d45e6cf..0000000000 --- a/docs/en/25-application/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: Application Practice diff --git a/docs/en/25-application/index.md b/docs/en/25-application/index.md index 178fef47b6..c9d652f0a5 100644 --- a/docs/en/25-application/index.md +++ b/docs/en/25-application/index.md @@ -1,5 +1,6 @@ --- -title: Application Practice +title: Application Practices +sidebar_label: Application Practices description: This document describes some examples of building systems around TDengine. --- diff --git a/docs/en/26-tdinternal/_category_.yml b/docs/en/26-tdinternal/_category_.yml deleted file mode 100644 index 38a7f9a764..0000000000 --- a/docs/en/26-tdinternal/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: Inside TDengine diff --git a/docs/en/26-tdinternal/index.md b/docs/en/26-tdinternal/index.md index 4f08467543..37899eea86 100644 --- a/docs/en/26-tdinternal/index.md +++ b/docs/en/26-tdinternal/index.md @@ -1,5 +1,6 @@ --- title: TDengine Inside +sidebar_label: TDengine Inside description: This document describes the internals of TDengine from an architectural perspective. --- diff --git a/docs/en/27-train-faq/_category_.yml b/docs/en/27-train-faq/_category_.yml deleted file mode 100644 index 7a8530c129..0000000000 --- a/docs/en/27-train-faq/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: FAQ & Others diff --git a/docs/en/27-train-faq/index.md b/docs/en/27-train-faq/index.md index cc55a41559..d509cdc7f6 100644 --- a/docs/en/27-train-faq/index.md +++ b/docs/en/27-train-faq/index.md @@ -1,5 +1,6 @@ --- title: FAQ & Others +sidebar_label: FAQ description: This document describes common issues related with TDengine. --- diff --git a/docs/en/28-releases/_category_.yml b/docs/en/28-releases/_category_.yml deleted file mode 100644 index c163835206..0000000000 --- a/docs/en/28-releases/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: Releases \ No newline at end of file From 1d5d413010ae63542e20924a06b7c84aa6152004 Mon Sep 17 00:00:00 2001 From: gccgdb1234 Date: Mon, 12 Aug 2024 17:03:09 +0800 Subject: [PATCH 19/39] doc: remove _category_.yml --- docs/zh/04-get-started/_category_.yml | 1 - docs/zh/08-develop/01-connect/_category_.yml | 1 - docs/zh/08-develop/_category_.yml | 1 - docs/zh/14-reference/03-taos-sql/_category_.yml | 1 - docs/zh/14-reference/03-taos-sql/index.md | 3 ++- docs/zh/14-reference/05-connector/_category_.yml | 1 - docs/zh/14-reference/05-connector/index.md | 2 +- docs/zh/14-reference/_13-schemaless/_category_.yml | 1 - docs/zh/14-reference/_category_.yml | 1 - docs/zh/20-third-party/_category_.yml | 1 - docs/zh/25-application/_category_.yml | 1 - docs/zh/26-tdinternal/_category_.yml | 1 - docs/zh/27-train-faq/_category_.yml | 1 - docs/zh/27-train-faq/index.md | 2 +- docs/zh/28-releases/_category_.yml | 1 - docs/zh/28-releases/index.md | 10 ++++++++++ 16 files changed, 14 insertions(+), 15 deletions(-) delete mode 100644 docs/zh/04-get-started/_category_.yml delete mode 100644 docs/zh/08-develop/01-connect/_category_.yml delete mode 100644 docs/zh/08-develop/_category_.yml delete mode 100644 docs/zh/14-reference/03-taos-sql/_category_.yml delete mode 100644 docs/zh/14-reference/05-connector/_category_.yml delete mode 100644 docs/zh/14-reference/_13-schemaless/_category_.yml delete mode 100644 docs/zh/14-reference/_category_.yml delete mode 100644 docs/zh/20-third-party/_category_.yml delete mode 100644 docs/zh/25-application/_category_.yml delete mode 100644 docs/zh/26-tdinternal/_category_.yml delete mode 100644 docs/zh/27-train-faq/_category_.yml delete mode 100644 docs/zh/28-releases/_category_.yml create mode 100644 docs/zh/28-releases/index.md diff --git a/docs/zh/04-get-started/_category_.yml b/docs/zh/04-get-started/_category_.yml deleted file mode 100644 index b2348fade6..0000000000 --- a/docs/zh/04-get-started/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: 立即开始 diff --git a/docs/zh/08-develop/01-connect/_category_.yml b/docs/zh/08-develop/01-connect/_category_.yml deleted file mode 100644 index f75d563ac9..0000000000 --- a/docs/zh/08-develop/01-connect/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: 建立连接 diff --git a/docs/zh/08-develop/_category_.yml b/docs/zh/08-develop/_category_.yml deleted file mode 100644 index 509a9405c4..0000000000 --- a/docs/zh/08-develop/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: 开发指南 \ No newline at end of file diff --git a/docs/zh/14-reference/03-taos-sql/_category_.yml b/docs/zh/14-reference/03-taos-sql/_category_.yml deleted file mode 100644 index 62290997ec..0000000000 --- a/docs/zh/14-reference/03-taos-sql/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: SQL 手册 diff --git a/docs/zh/14-reference/03-taos-sql/index.md b/docs/zh/14-reference/03-taos-sql/index.md index 6095134554..ac879e4352 100644 --- a/docs/zh/14-reference/03-taos-sql/index.md +++ b/docs/zh/14-reference/03-taos-sql/index.md @@ -1,5 +1,6 @@ --- -title: TDengine SQL +title: SQL 手册 +sidebar_label: SQL 手册 description: 'TDengine SQL 支持的语法规则、主要查询功能、支持的 SQL 查询函数,以及常用技巧等内容' --- diff --git a/docs/zh/14-reference/05-connector/_category_.yml b/docs/zh/14-reference/05-connector/_category_.yml deleted file mode 100644 index abd3f666f3..0000000000 --- a/docs/zh/14-reference/05-connector/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: "连接器" \ No newline at end of file diff --git a/docs/zh/14-reference/05-connector/index.md b/docs/zh/14-reference/05-connector/index.md index 6cca1960c3..c4e5628686 100644 --- a/docs/zh/14-reference/05-connector/index.md +++ b/docs/zh/14-reference/05-connector/index.md @@ -1,6 +1,6 @@ --- sidebar_label: 连接器 -title: 连接器 +title: 连接器参考手册 description: 详细介绍各种语言的连接器及 REST API --- diff --git a/docs/zh/14-reference/_13-schemaless/_category_.yml b/docs/zh/14-reference/_13-schemaless/_category_.yml deleted file mode 100644 index 2e93df8b36..0000000000 --- a/docs/zh/14-reference/_13-schemaless/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: Schemaless 写入 diff --git a/docs/zh/14-reference/_category_.yml b/docs/zh/14-reference/_category_.yml deleted file mode 100644 index faca32476c..0000000000 --- a/docs/zh/14-reference/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: 参考手册 \ No newline at end of file diff --git a/docs/zh/20-third-party/_category_.yml b/docs/zh/20-third-party/_category_.yml deleted file mode 100644 index 514d8c19a8..0000000000 --- a/docs/zh/20-third-party/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: 第三方工具 \ No newline at end of file diff --git a/docs/zh/25-application/_category_.yml b/docs/zh/25-application/_category_.yml deleted file mode 100644 index f43a4601b6..0000000000 --- a/docs/zh/25-application/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: 应用实践 diff --git a/docs/zh/26-tdinternal/_category_.yml b/docs/zh/26-tdinternal/_category_.yml deleted file mode 100644 index c7509bf662..0000000000 --- a/docs/zh/26-tdinternal/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: 技术内幕 \ No newline at end of file diff --git a/docs/zh/27-train-faq/_category_.yml b/docs/zh/27-train-faq/_category_.yml deleted file mode 100644 index 16b32bc38f..0000000000 --- a/docs/zh/27-train-faq/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: FAQ 及其他 diff --git a/docs/zh/27-train-faq/index.md b/docs/zh/27-train-faq/index.md index e7159d98c8..8c326d74cb 100644 --- a/docs/zh/27-train-faq/index.md +++ b/docs/zh/27-train-faq/index.md @@ -1,5 +1,5 @@ --- -title: FAQ 及其他 +title: 常见问题 description: 用户经常遇到的问题 --- diff --git a/docs/zh/28-releases/_category_.yml b/docs/zh/28-releases/_category_.yml deleted file mode 100644 index dcd57247d7..0000000000 --- a/docs/zh/28-releases/_category_.yml +++ /dev/null @@ -1 +0,0 @@ -label: 发布历史 \ No newline at end of file diff --git a/docs/zh/28-releases/index.md b/docs/zh/28-releases/index.md new file mode 100644 index 0000000000..65fcf70acd --- /dev/null +++ b/docs/zh/28-releases/index.md @@ -0,0 +1,10 @@ +--- +title: 发布历史 +--- + +```mdx-code-block +import DocCardList from '@theme/DocCardList'; +import {useCurrentSidebarCategory} from '@docusaurus/theme-common'; + + +``` \ No newline at end of file From e3bfb9870ab53fedcbda5eace0bcf3f61caf06d8 Mon Sep 17 00:00:00 2001 From: Minglei Jin Date: Mon, 12 Aug 2024 17:12:19 +0800 Subject: [PATCH 20/39] fix(meta/commit): transfer txn's ownership to asyn commit thread --- source/dnode/vnode/src/meta/metaCommit.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/dnode/vnode/src/meta/metaCommit.c b/source/dnode/vnode/src/meta/metaCommit.c index bbf2217b31..ec9e2d90ec 100644 --- a/source/dnode/vnode/src/meta/metaCommit.c +++ b/source/dnode/vnode/src/meta/metaCommit.c @@ -73,6 +73,8 @@ int metaPrepareAsyncCommit(SMeta *pMeta) { TSDB_CHECK_CODE(code, lino, _exit); pMeta->changed = false; + pMeta->txn = NULL; + _exit: if (code) { metaError("vgId:%d %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, From ef845842f8a02af544bb56d76635b9f6ce923e3b Mon Sep 17 00:00:00 2001 From: sheyanjie-qq <249478495@qq.com> Date: Mon, 12 Aug 2024 17:29:17 +0800 Subject: [PATCH 21/39] mod rust log --- .../rust/nativeexample/examples/connect.rs | 18 ++- .../rust/nativeexample/examples/createdb.rs | 35 +++++ .../rust/nativeexample/examples/insert.rs | 31 +++++ .../rust/nativeexample/examples/query.rs | 129 +++++++++--------- .../rust/nativeexample/examples/schemaless.rs | 27 +++- .../rust/nativeexample/examples/stmt.rs | 19 ++- .../rust/nativeexample/examples/tmq.rs | 79 +++++++---- .../rust/restexample/examples/connect.rs | 18 ++- .../rust/restexample/examples/createdb.rs | 35 +++++ .../rust/restexample/examples/insert.rs | 31 +++++ .../rust/restexample/examples/query.rs | 102 ++++++++++++++ .../rust/restexample/examples/schemaless.rs | 27 +++- .../rust/restexample/examples/stmt.rs | 21 +-- .../examples/rust/restexample/examples/tmq.rs | 79 +++++++---- docs/zh/08-develop/02-sql.md | 4 +- .../taosdata/example/ConsumerLoopFull.java | 18 +-- .../taosdata/example/WsConsumerLoopFull.java | 16 +-- 17 files changed, 521 insertions(+), 168 deletions(-) create mode 100644 docs/examples/rust/nativeexample/examples/createdb.rs create mode 100644 docs/examples/rust/nativeexample/examples/insert.rs create mode 100644 docs/examples/rust/restexample/examples/createdb.rs create mode 100644 docs/examples/rust/restexample/examples/insert.rs create mode 100644 docs/examples/rust/restexample/examples/query.rs diff --git a/docs/examples/rust/nativeexample/examples/connect.rs b/docs/examples/rust/nativeexample/examples/connect.rs index dee6db2a41..7da09ae7ec 100644 --- a/docs/examples/rust/nativeexample/examples/connect.rs +++ b/docs/examples/rust/nativeexample/examples/connect.rs @@ -1,9 +1,17 @@ use taos::*; #[tokio::main] -async fn main() -> Result<(), Error> { - #[allow(unused_variables)] - let taos = TaosBuilder::from_dsn("taos://localhost:6030")?.build()?; - println!("Connected to localhost with native connection successfully."); - Ok(()) +async fn main() -> anyhow::Result<()> { + let dsn = "taos://localhost:6030".to_string(); + + match TaosBuilder::from_dsn(&dsn)?.build().await { + Ok(_taos) => { + println!("Connected to {} successfully.", dsn); + Ok(()) + } + Err(err) => { + eprintln!("Failed to connect to {}; ErrMessage: {}", dsn, err); + return Err(err.into()); + } + } } diff --git a/docs/examples/rust/nativeexample/examples/createdb.rs b/docs/examples/rust/nativeexample/examples/createdb.rs new file mode 100644 index 0000000000..d3b2f5178b --- /dev/null +++ b/docs/examples/rust/nativeexample/examples/createdb.rs @@ -0,0 +1,35 @@ +use taos::*; + +#[tokio::main] +async fn main() -> anyhow::Result<()> { + let url = "taos://localhost:6030"; + + // ANCHOR: create_db_and_table + let taos = TaosBuilder::from_dsn(url)?.build().await?; + + // create database and use it + match taos.exec_many([ + "CREATE DATABASE IF NOT EXISTS power", + ]).await { + Ok(afffected_rows) => println!("Create database power successfully, rowsAffected: {}", afffected_rows), + Err(err) => { + eprintln!("Failed to create database power; ErrMessage: {}", err); + return Err(err.into()); + } + } + + // create super table + match taos.exec_many([ + "CREATE STABLE IF NOT EXISTS power.meters (`ts` TIMESTAMP, `current` FLOAT, `voltage` INT, `phase` FLOAT) \ + TAGS (`groupid` INT, `location` BINARY(24))", + ]).await { + Ok(afffected_rows) => println!("Create stable power.meters successfully, rowsAffected: {}", afffected_rows), + Err(err) => { + eprintln!("Failed to create stable power.meters; ErrMessage: {}", err); + return Err(err.into()); + } + } + + Ok(()) + // ANCHOR_END: create_db_and_table +} diff --git a/docs/examples/rust/nativeexample/examples/insert.rs b/docs/examples/rust/nativeexample/examples/insert.rs new file mode 100644 index 0000000000..d551da436e --- /dev/null +++ b/docs/examples/rust/nativeexample/examples/insert.rs @@ -0,0 +1,31 @@ +use taos::*; + +#[tokio::main] +async fn main() -> anyhow::Result<()> { + let dsn = "taos://localhost:6030"; + let builder = TaosBuilder::from_dsn(dsn)?; + + let taos = builder.build().await?; + + + // ANCHOR: insert_data + match taos.exec(r#"INSERT INTO + power.d1001 USING power.meters TAGS(2,'California.SanFrancisco') + VALUES + (NOW + 1a, 10.30000, 219, 0.31000) + (NOW + 2a, 12.60000, 218, 0.33000) + (NOW + 3a, 12.30000, 221, 0.31000) + power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco') + VALUES + (NOW + 1a, 10.30000, 218, 0.25000) "#).await{ + Ok(affected_rows) => println!("Successfully inserted {} rows to power.meters.", affected_rows), + Err(err) => { + eprintln!("Failed to insert data to power.meters, dsn: {}; ErrMessage: {}", dsn, err); + return Err(err.into()); + } + } + + // ANCHOR_END: insert_data + + Ok(()) +} diff --git a/docs/examples/rust/nativeexample/examples/query.rs b/docs/examples/rust/nativeexample/examples/query.rs index 08ec223761..6b6fde6a31 100644 --- a/docs/examples/rust/nativeexample/examples/query.rs +++ b/docs/examples/rust/nativeexample/examples/query.rs @@ -9,58 +9,33 @@ async fn main() -> anyhow::Result<()> { let taos = builder.build().await?; - // ANCHOR: create_db_and_table - let db = "power"; - // create database - taos.exec_many([ - format!("CREATE DATABASE IF NOT EXISTS `{db}`"), - format!("USE `{db}`"), - ]) - .await?; - println!("Create database power successfully."); - - // create super table - taos.exec_many([ - "CREATE STABLE IF NOT EXISTS `meters` (`ts` TIMESTAMP, `current` FLOAT, `voltage` INT, `phase` FLOAT) \ - TAGS (`groupid` INT, `location` BINARY(24))", - ]).await?; - println!("Create stable meters successfully."); - - // ANCHOR_END: create_db_and_table - - // ANCHOR: insert_data - let inserted = taos.exec(r#"INSERT INTO - power.d1001 USING power.meters TAGS(2,'California.SanFrancisco') - VALUES - (NOW + 1a, 10.30000, 219, 0.31000) - (NOW + 2a, 12.60000, 218, 0.33000) - (NOW + 3a, 12.30000, 221, 0.31000) - power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco') - VALUES - (NOW + 1a, 10.30000, 218, 0.25000) "#).await?; - - println!("inserted: {} rows to power.meters successfully.", inserted); - // ANCHOR_END: insert_data - // ANCHOR: query_data // query data, make sure the database and table are created before - let mut result = taos.query("SELECT ts, current, location FROM power.meters limit 100").await?; - - for field in result.fields() { - println!("got field: {}", field.name()); - } - - let mut rows = result.rows(); - let mut nrows = 0; - while let Some(row) = rows.try_next().await? { - for (col, (name, value)) in row.enumerate() { - println!( - "[{}] got value in col {} (named `{:>8}`): {}", - nrows, col, name, value - ); + match taos.query("SELECT ts, current, location FROM power.meters limit 100").await{ + Ok(mut result) => { + for field in result.fields() { + println!("got field: {}", field.name()); + } + + let mut rows = result.rows(); + let mut nrows = 0; + while let Some(row) = rows.try_next().await? { + for (col, (name, value)) in row.enumerate() { + println!( + "[{}] got value in col {} (named `{:>8}`): {}", + nrows, col, name, value + ); + } + nrows += 1; + } + } + Err(err) => { + eprintln!("Failed to query data from power.meters, dsn: {}; ErrMessage: {}", dsn, err); + return Err(err.into()); } - nrows += 1; } + + // ANCHOR_END: query_data // ANCHOR: query_data_2 @@ -72,30 +47,56 @@ async fn main() -> anyhow::Result<()> { ts: DateTime, // float to f32 current: Option, - // int to i32 - voltage: Option, - phase: Option, - groupid: i32, // binary/varchar to String location: String, } - let records: Vec = taos - .query("select ts, current, voltage, phase, groupid, location from power.meters limit 100") - .await? - .deserialize() - .try_collect() - .await?; - - dbg!(records); + match taos.query("SELECT ts, current, location FROM power.meters limit 100").await { + Ok(mut query) => { + match query.deserialize::().try_collect::>().await { + Ok(records) => { + dbg!(records); + } + Err(err) => { + eprintln!("Failed to deserialize query results; ErrMessage: {}", err); + return Err(err.into()); + } + } + } + Err(err) => { + eprintln!("Failed to query data from power.meters, url: {}; ErrMessage: {}", dsn, err); + return Err(err.into()); + } + } // ANCHOR_END: query_data_2 // ANCHOR: query_with_req_id - let result = taos.query_with_req_id("SELECT ts, current, location FROM power.meters limit 1", 1).await?; - for field in result.fields() { - println!("got field: {}", field.name()); + + let req_id :u64 = 3; + match taos.query_with_req_id("SELECT ts, current, location FROM power.meters limit 1", req_id).await{ + Ok(mut result) => { + for field in result.fields() { + println!("got field: {}", field.name()); + } + + let mut rows = result.rows(); + let mut nrows = 0; + while let Some(row) = rows.try_next().await? { + for (col, (name, value)) in row.enumerate() { + println!( + "[{}] got value in col {} (named `{:>8}`): {}", + nrows, col, name, value + ); + } + nrows += 1; + } + } + Err(err) => { + eprintln!("Failed to execute sql with reqId: {}, dsn: {}; ErrMessage: {}", req_id, dsn, err); + return Err(err.into()); + } } - println!("query with reqId successfully"); + // ANCHOR_END: query_with_req_id Ok(()) } diff --git a/docs/examples/rust/nativeexample/examples/schemaless.rs b/docs/examples/rust/nativeexample/examples/schemaless.rs index 9e8eab4597..88e9ef4584 100644 --- a/docs/examples/rust/nativeexample/examples/schemaless.rs +++ b/docs/examples/rust/nativeexample/examples/schemaless.rs @@ -11,7 +11,8 @@ use taos::taos_query; async fn main() -> anyhow::Result<()> { std::env::set_var("RUST_LOG", "taos=debug"); pretty_env_logger::init(); - let dsn = "taos://localhost:6030".to_string(); + let host = "localhost"; + let dsn = format!("taos://{}:6030", host); log::debug!("dsn: {:?}", &dsn); let client = TaosBuilder::from_dsn(dsn)?.build().await?; @@ -39,7 +40,13 @@ async fn main() -> anyhow::Result<()> { .ttl(1000) .req_id(100u64) .build()?; - assert_eq!(client.put(&sml_data).await?, ()); + match client.put(&sml_data).await{ + Ok(_) => {}, + Err(err) => { + eprintln!("Failed to insert data with schemaless, host: {}; ErrMessage: {}", host, err); + return Err(err.into()); + } + } // SchemalessProtocol::Telnet let data = [ @@ -55,7 +62,13 @@ async fn main() -> anyhow::Result<()> { .ttl(1000) .req_id(200u64) .build()?; - assert_eq!(client.put(&sml_data).await?, ()); + match client.put(&sml_data).await{ + Ok(_) => {}, + Err(err) => { + eprintln!("Failed to insert data with schemaless, host: {}; ErrMessage: {}", host, err); + return Err(err.into()); + } + } // SchemalessProtocol::Json let data = [ @@ -80,7 +93,13 @@ async fn main() -> anyhow::Result<()> { .ttl(1000) .req_id(300u64) .build()?; - assert_eq!(client.put(&sml_data).await?, ()); + match client.put(&sml_data).await{ + Ok(_) => {}, + Err(err) => { + eprintln!("Failed to insert data with schemaless, host: {}; ErrMessage: {}", host, err); + return Err(err.into()); + } + } println!("Inserted data with schemaless successfully."); Ok(()) diff --git a/docs/examples/rust/nativeexample/examples/stmt.rs b/docs/examples/rust/nativeexample/examples/stmt.rs index 04faa888bf..de35ae2972 100644 --- a/docs/examples/rust/nativeexample/examples/stmt.rs +++ b/docs/examples/rust/nativeexample/examples/stmt.rs @@ -2,12 +2,13 @@ use taos::*; #[tokio::main] async fn main() -> anyhow::Result<()> { - let taos = TaosBuilder::from_dsn("taos://")?.build().await?; + let dsn = "taos://localhost:6030"; + let taos = TaosBuilder::from_dsn(dsn)?.build().await?; taos.exec("DROP DATABASE IF EXISTS power").await?; taos.create_database("power").await?; taos.use_database("power").await?; - taos.exec("CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)").await?; + taos.exec("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))").await?; let mut stmt = Stmt::init(&taos).await?; stmt.prepare("INSERT INTO ? USING meters TAGS(?, ?) VALUES(?, ?, ?, ?)").await?; @@ -15,8 +16,8 @@ async fn main() -> anyhow::Result<()> { const NUM_TABLES: usize = 10; const NUM_ROWS: usize = 10; for i in 0..NUM_TABLES { - let table_name = format!("d{}", i); - let tags = vec![Value::VarChar("California.SanFransico".into()), Value::Int(2)]; + let table_name = format!("d_bind_{}", i); + let tags = vec![Value::Int(i as i32), Value::VarChar(format!("location_{}", i).into())]; // set table name and tags for the prepared statement. stmt.set_tbname_tags(&table_name, &tags).await?; @@ -35,9 +36,13 @@ async fn main() -> anyhow::Result<()> { } // execute. - let rows = stmt.execute().await?; - assert_eq!(rows, NUM_TABLES * NUM_ROWS); + match stmt.execute().await{ + Ok(affected_rows) => println!("Successfully inserted {} rows to power.meters.", affected_rows), + Err(err) => { + eprintln!("Failed to insert to table meters using stmt, dsn: {}; ErrMessage: {}", dsn, err); + return Err(err.into()); + } + } - println!("execute stmt insert successfully"); Ok(()) } diff --git a/docs/examples/rust/nativeexample/examples/tmq.rs b/docs/examples/rust/nativeexample/examples/tmq.rs index 4adfd28989..4ae71bc445 100644 --- a/docs/examples/rust/nativeexample/examples/tmq.rs +++ b/docs/examples/rust/nativeexample/examples/tmq.rs @@ -12,7 +12,7 @@ async fn main() -> anyhow::Result<()> { use taos_query::prelude::*; // ANCHOR: create_consumer_dsn let dsn = "taos://localhost:6030".to_string(); - log::info!("dsn: {}", dsn); + println!("dsn: {}", dsn); let mut dsn = Dsn::from_str(&dsn)?; // ANCHOR_END: create_consumer_dsn @@ -37,20 +37,36 @@ async fn main() -> anyhow::Result<()> { // ANCHOR_END: create_topic // ANCHOR: create_consumer_ac + let group_id = "group1".to_string(); + let client_id = "client1".to_string(); dsn.params.insert("auto.offset.reset".to_string(), "latest".to_string()); dsn.params.insert("msg.with.table.name".to_string(), "true".to_string()); dsn.params.insert("enable.auto.commit".to_string(), "true".to_string()); dsn.params.insert("auto.commit.interval.ms".to_string(), "1000".to_string()); - dsn.params.insert("group.id".to_string(), "group1".to_string()); - dsn.params.insert("client.id".to_string(), "client1".to_string()); + dsn.params.insert("group.id".to_string(), group_id.clone()); + dsn.params.insert("client.id".to_string(), client_id.clone()); let builder = TmqBuilder::from_dsn(&dsn)?; - let mut consumer = builder.build().await?; + let mut consumer = match builder.build().await{ + Ok(consumer) => { + println!("Create consumer successfully, dsn: {}, groupId: {}, clientId: {}.", dsn, group_id, client_id); + consumer + } + Err(err) => { + eprintln!("Failed to create consumer, dsn: {}; ErrMessage: {}", dsn, err); + return Err(err.into()); + } + }; // ANCHOR_END: create_consumer_ac - // ANCHOR: subscribe - consumer.subscribe(["topic_meters"]).await?; - // ANCHOR_END: subscribe + // ANCHOR: consume + match consumer.subscribe(["topic_meters"]).await{ + Ok(_) => println!("subscribe topics successfully."), + Err(err) => { + eprintln!("Failed to subscribe topic_meters, dsn: {}; ErrMessage: {}", dsn, err); + return Err(err.into()); + } + } #[derive(Debug, serde::Deserialize)] #[allow(dead_code)] @@ -67,8 +83,6 @@ async fn main() -> anyhow::Result<()> { location: String, } - // ANCHOR: consume - consumer .stream() .try_for_each(|(offset, message)| async move { @@ -85,7 +99,10 @@ async fn main() -> anyhow::Result<()> { } Ok(()) }) - .await?; + .await.map_err(|e| { + eprintln!("Failed to execute consumer functions. ErrMessage: {:?}", e); + e + })?; // ANCHOR_END: consume @@ -105,16 +122,25 @@ async fn main() -> anyhow::Result<()> { } } // commit offset manually when you have processed the message. - consumer.commit(offset).await?; + match consumer.commit(offset).await{ + Ok(_) => println!("commit offset manually successfully."), + Err(err) => { + eprintln!("Failed to commit offset manually, dsn: {}; ErrMessage: {}", dsn, err); + return Err(err.into()); + } + } Ok(()) }) - .await?; + .await.map_err(|e| { + eprintln!("Failed to execute consumer functions. ErrMessage: {:?}", e); + e + })?; // ANCHOR_END: consumer_commit_manually - // ANCHOR: assignments + + // ANCHOR: seek_offset let assignments = consumer.assignments().await.unwrap(); - log::info!("assignments: {:?}", assignments); - // ANCHOR_END: assignments + println!("assignments: {:?}", assignments); // seek offset for topic_vec_assignment in assignments { @@ -125,7 +151,7 @@ async fn main() -> anyhow::Result<()> { let current = assignment.current_offset(); let begin = assignment.begin(); let end = assignment.end(); - log::debug!( + println!( "topic: {}, vgroup_id: {}, current offset: {} begin {}, end: {}", topic, vgroup_id, @@ -133,23 +159,24 @@ async fn main() -> anyhow::Result<()> { begin, end ); - // ANCHOR: seek_offset - let res = consumer.offset_seek(topic, vgroup_id, end).await; - if res.is_err() { - log::error!("seek offset error: {:?}", res); - let a = consumer.assignments().await.unwrap(); - log::error!("assignments: {:?}", a); + + match consumer.offset_seek(topic, vgroup_id, begin).await{ + Ok(_) => (), + Err(err) => { + eprintln!("seek example failed; ErrMessage: {}", err); + return Err(err.into()); + } } - // ANCHOR_END: seek_offset } let topic_assignment = consumer.topic_assignment(topic).await; - log::debug!("topic assignment: {:?}", topic_assignment); + println!("topic assignment: {:?}", topic_assignment); } - + println!("assignment seek to beginning successfully."); // after seek offset let assignments = consumer.assignments().await.unwrap(); - log::info!("after seek offset assignments: {:?}", assignments); + println!("after seek offset assignments: {:?}", assignments); + // ANCHOR_END: seek_offset // ANCHOR: unsubscribe consumer.unsubscribe().await; diff --git a/docs/examples/rust/restexample/examples/connect.rs b/docs/examples/rust/restexample/examples/connect.rs index 0c89517f22..535e265a97 100644 --- a/docs/examples/rust/restexample/examples/connect.rs +++ b/docs/examples/rust/restexample/examples/connect.rs @@ -1,9 +1,17 @@ use taos::*; #[tokio::main] -async fn main() -> Result<(), Error> { - #[allow(unused_variables)] - let taos = TaosBuilder::from_dsn("taos+ws://localhost:6041")?.build()?; - println!("Connected to localhost with websocket connection successfully."); - Ok(()) +async fn main() -> anyhow::Result<()> { + let dsn = "ws://localhost:6041".to_string(); + + match TaosBuilder::from_dsn(&dsn)?.build().await { + Ok(_taos) => { + println!("Connected to {} successfully.", dsn); + Ok(()) + } + Err(err) => { + eprintln!("Failed to connect to {}; ErrMessage: {}", dsn, err); + return Err(err.into()); + } + } } diff --git a/docs/examples/rust/restexample/examples/createdb.rs b/docs/examples/rust/restexample/examples/createdb.rs new file mode 100644 index 0000000000..e061f1d835 --- /dev/null +++ b/docs/examples/rust/restexample/examples/createdb.rs @@ -0,0 +1,35 @@ +use taos::*; + +#[tokio::main] +async fn main() -> anyhow::Result<()> { + let url = "ws://localhost:6041"; + + // ANCHOR: create_db_and_table + let taos = TaosBuilder::from_dsn(url)?.build().await?; + + // create database and use it + match taos.exec_many([ + "CREATE DATABASE IF NOT EXISTS power", + ]).await { + Ok(afffected_rows) => println!("Create database power successfully, rowsAffected: {}", afffected_rows), + Err(err) => { + eprintln!("Failed to create database power; ErrMessage: {}", err); + return Err(err.into()); + } + } + + // create super table + match taos.exec_many([ + "CREATE STABLE IF NOT EXISTS power.meters (`ts` TIMESTAMP, `current` FLOAT, `voltage` INT, `phase` FLOAT) \ + TAGS (`groupid` INT, `location` BINARY(24))", + ]).await { + Ok(afffected_rows) => println!("Create stable power.meters successfully, rowsAffected: {}", afffected_rows), + Err(err) => { + eprintln!("Failed to create stable power.meters; ErrMessage: {}", err); + return Err(err.into()); + } + } + + Ok(()) + // ANCHOR_END: create_db_and_table +} diff --git a/docs/examples/rust/restexample/examples/insert.rs b/docs/examples/rust/restexample/examples/insert.rs new file mode 100644 index 0000000000..62522575ee --- /dev/null +++ b/docs/examples/rust/restexample/examples/insert.rs @@ -0,0 +1,31 @@ +use taos::*; + +#[tokio::main] +async fn main() -> anyhow::Result<()> { + let dsn = "ws://localhost:6041"; + let builder = TaosBuilder::from_dsn(dsn)?; + + let taos = builder.build().await?; + + + // ANCHOR: insert_data + match taos.exec(r#"INSERT INTO + power.d1001 USING power.meters TAGS(2,'California.SanFrancisco') + VALUES + (NOW + 1a, 10.30000, 219, 0.31000) + (NOW + 2a, 12.60000, 218, 0.33000) + (NOW + 3a, 12.30000, 221, 0.31000) + power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco') + VALUES + (NOW + 1a, 10.30000, 218, 0.25000) "#).await{ + Ok(affected_rows) => println!("Successfully inserted {} rows to power.meters.", affected_rows), + Err(err) => { + eprintln!("Failed to insert data to power.meters, dsn: {}; ErrMessage: {}", dsn, err); + return Err(err.into()); + } + } + + // ANCHOR_END: insert_data + + Ok(()) +} diff --git a/docs/examples/rust/restexample/examples/query.rs b/docs/examples/rust/restexample/examples/query.rs new file mode 100644 index 0000000000..4e69f9fc97 --- /dev/null +++ b/docs/examples/rust/restexample/examples/query.rs @@ -0,0 +1,102 @@ +use taos::*; +use chrono::Local; +use chrono::DateTime; + +#[tokio::main] +async fn main() -> anyhow::Result<()> { + let dsn = "ws://localhost:6041"; + let builder = TaosBuilder::from_dsn(dsn)?; + + let taos = builder.build().await?; + + // ANCHOR: query_data + // query data, make sure the database and table are created before + match taos.query("SELECT ts, current, location FROM power.meters limit 100").await{ + Ok(mut result) => { + for field in result.fields() { + println!("got field: {}", field.name()); + } + + let mut rows = result.rows(); + let mut nrows = 0; + while let Some(row) = rows.try_next().await? { + for (col, (name, value)) in row.enumerate() { + println!( + "[{}] got value in col {} (named `{:>8}`): {}", + nrows, col, name, value + ); + } + nrows += 1; + } + } + Err(err) => { + eprintln!("Failed to query data from power.meters, dsn: {}; ErrMessage: {}", dsn, err); + return Err(err.into()); + } + } + + + // ANCHOR_END: query_data + + // ANCHOR: query_data_2 + // query data, make sure the database and table are created before + #[derive(Debug, serde::Deserialize)] + #[allow(dead_code)] + struct Record { + // deserialize timestamp to chrono::DateTime + ts: DateTime, + // float to f32 + current: Option, + // binary/varchar to String + location: String, + } + + match taos.query("SELECT ts, current, location FROM power.meters limit 100").await { + Ok(mut query) => { + match query.deserialize::().try_collect::>().await { + Ok(records) => { + dbg!(records); + } + Err(err) => { + eprintln!("Failed to deserialize query results; ErrMessage: {}", err); + return Err(err.into()); + } + } + } + Err(err) => { + eprintln!("Failed to query data from power.meters, url: {}; ErrMessage: {}", dsn, err); + return Err(err.into()); + } + } + // ANCHOR_END: query_data_2 + + // ANCHOR: query_with_req_id + + let req_id :u64 = 3; + match taos.query_with_req_id("SELECT ts, current, location FROM power.meters limit 1", req_id).await{ + Ok(mut result) => { + for field in result.fields() { + println!("got field: {}", field.name()); + } + + let mut rows = result.rows(); + let mut nrows = 0; + while let Some(row) = rows.try_next().await? { + for (col, (name, value)) in row.enumerate() { + println!( + "[{}] got value in col {} (named `{:>8}`): {}", + nrows, col, name, value + ); + } + nrows += 1; + } + } + Err(err) => { + eprintln!("Failed to execute sql with reqId: {}, dsn: {}; ErrMessage: {}", req_id, dsn, err); + return Err(err.into()); + } + } + + // ANCHOR_END: query_with_req_id + Ok(()) +} diff --git a/docs/examples/rust/restexample/examples/schemaless.rs b/docs/examples/rust/restexample/examples/schemaless.rs index a0ee1809e5..f629d66663 100644 --- a/docs/examples/rust/restexample/examples/schemaless.rs +++ b/docs/examples/rust/restexample/examples/schemaless.rs @@ -11,7 +11,8 @@ use taos::taos_query; async fn main() -> anyhow::Result<()> { std::env::set_var("RUST_LOG", "taos=debug"); pretty_env_logger::init(); - let dsn = "http://localhost:6041/power".to_string(); + let host = "localhost"; + let dsn = format!("ws://{}:6041/power", host); log::debug!("dsn: {:?}", &dsn); let client = TaosBuilder::from_dsn(dsn)?.build().await?; @@ -30,7 +31,13 @@ async fn main() -> anyhow::Result<()> { .ttl(1000) .req_id(100u64) .build()?; - assert_eq!(client.put(&sml_data).await?, ()); + match client.put(&sml_data).await{ + Ok(_) => {}, + Err(err) => { + eprintln!("Failed to insert data with schemaless, host: {}; ErrMessage: {}", host, err); + return Err(err.into()); + } + } // SchemalessProtocol::Telnet let data = [ @@ -46,7 +53,13 @@ async fn main() -> anyhow::Result<()> { .ttl(1000) .req_id(200u64) .build()?; - assert_eq!(client.put(&sml_data).await?, ()); + match client.put(&sml_data).await{ + Ok(_) => {}, + Err(err) => { + eprintln!("Failed to insert data with schemaless, host: {}; ErrMessage: {}", host, err); + return Err(err.into()); + } + } // SchemalessProtocol::Json let data = [ @@ -71,7 +84,13 @@ async fn main() -> anyhow::Result<()> { .ttl(1000) .req_id(300u64) .build()?; - assert_eq!(client.put(&sml_data).await?, ()); + match client.put(&sml_data).await{ + Ok(_) => {}, + Err(err) => { + eprintln!("Failed to insert data with schemaless, host: {}; ErrMessage: {}", host, err); + return Err(err.into()); + } + } println!("Inserted data with schemaless successfully."); Ok(()) diff --git a/docs/examples/rust/restexample/examples/stmt.rs b/docs/examples/rust/restexample/examples/stmt.rs index 8148050e15..636b6780cd 100644 --- a/docs/examples/rust/restexample/examples/stmt.rs +++ b/docs/examples/rust/restexample/examples/stmt.rs @@ -2,12 +2,13 @@ use taos::*; #[tokio::main] async fn main() -> anyhow::Result<()> { - let taos = TaosBuilder::from_dsn("ws://")?.build().await?; + let dsn = "ws://"; + let taos = TaosBuilder::from_dsn(dsn)?.build().await?; taos.exec("DROP DATABASE IF EXISTS power").await?; taos.create_database("power").await?; taos.use_database("power").await?; - taos.exec("CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)").await?; + taos.exec("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))").await?; let mut stmt = Stmt::init(&taos).await?; stmt.prepare("INSERT INTO ? USING meters TAGS(?, ?) VALUES(?, ?, ?, ?)").await?; @@ -15,8 +16,8 @@ async fn main() -> anyhow::Result<()> { const NUM_TABLES: usize = 10; const NUM_ROWS: usize = 10; for i in 0..NUM_TABLES { - let table_name = format!("d{}", i); - let tags = vec![Value::VarChar("California.SanFransico".into()), Value::Int(2)]; + let table_name = format!("d_bind_{}", i); + let tags = vec![Value::Int(i as i32), Value::VarChar(format!("location_{}", i).into())]; // set table name and tags for the prepared statement. stmt.set_tbname_tags(&table_name, &tags).await?; @@ -35,9 +36,13 @@ async fn main() -> anyhow::Result<()> { } // execute. - let rows = stmt.execute().await?; - assert_eq!(rows, NUM_TABLES * NUM_ROWS); - - println!("execute stmt insert successfully"); + match stmt.execute().await{ + Ok(affected_rows) => println!("Successfully inserted {} rows to power.meters.", affected_rows), + Err(err) => { + eprintln!("Failed to insert to table meters using stmt, dsn: {}; ErrMessage: {}", dsn, err); + return Err(err.into()); + } + } + Ok(()) } diff --git a/docs/examples/rust/restexample/examples/tmq.rs b/docs/examples/rust/restexample/examples/tmq.rs index 48df89d5bf..046e1ec59c 100644 --- a/docs/examples/rust/restexample/examples/tmq.rs +++ b/docs/examples/rust/restexample/examples/tmq.rs @@ -12,7 +12,7 @@ async fn main() -> anyhow::Result<()> { use taos_query::prelude::*; // ANCHOR: create_consumer_dsn let dsn = "ws://localhost:6041".to_string(); - log::info!("dsn: {}", dsn); + println!("dsn: {}", dsn); let mut dsn = Dsn::from_str(&dsn)?; // ANCHOR_END: create_consumer_dsn @@ -37,20 +37,36 @@ async fn main() -> anyhow::Result<()> { // ANCHOR_END: create_topic // ANCHOR: create_consumer_ac + let group_id = "group1".to_string(); + let client_id = "client1".to_string(); dsn.params.insert("auto.offset.reset".to_string(), "latest".to_string()); dsn.params.insert("msg.with.table.name".to_string(), "true".to_string()); dsn.params.insert("enable.auto.commit".to_string(), "true".to_string()); dsn.params.insert("auto.commit.interval.ms".to_string(), "1000".to_string()); - dsn.params.insert("group.id".to_string(), "group1".to_string()); - dsn.params.insert("client.id".to_string(), "client1".to_string()); + dsn.params.insert("group.id".to_string(), group_id.clone()); + dsn.params.insert("client.id".to_string(), client_id.clone()); let builder = TmqBuilder::from_dsn(&dsn)?; - let mut consumer = builder.build().await?; + let mut consumer = match builder.build().await{ + Ok(consumer) => { + println!("Create consumer successfully, dsn: {}, groupId: {}, clientId: {}.", dsn, group_id, client_id); + consumer + } + Err(err) => { + eprintln!("Failed to create consumer, dsn: {}; ErrMessage: {}", dsn, err); + return Err(err.into()); + } + }; // ANCHOR_END: create_consumer_ac - // ANCHOR: subscribe - consumer.subscribe(["topic_meters"]).await?; - // ANCHOR_END: subscribe + // ANCHOR: consume + match consumer.subscribe(["topic_meters"]).await{ + Ok(_) => println!("subscribe topics successfully."), + Err(err) => { + eprintln!("Failed to subscribe topic_meters, dsn: {}; ErrMessage: {}", dsn, err); + return Err(err.into()); + } + } #[derive(Debug, serde::Deserialize)] #[allow(dead_code)] @@ -67,8 +83,6 @@ async fn main() -> anyhow::Result<()> { location: String, } - // ANCHOR: consume - consumer .stream() .try_for_each(|(offset, message)| async move { @@ -85,7 +99,10 @@ async fn main() -> anyhow::Result<()> { } Ok(()) }) - .await?; + .await.map_err(|e| { + eprintln!("Failed to poll data; ErrMessage: {:?}", e); + e + })?; // ANCHOR_END: consume @@ -105,16 +122,25 @@ async fn main() -> anyhow::Result<()> { } } // commit offset manually when you have processed the message. - consumer.commit(offset).await?; + match consumer.commit(offset).await{ + Ok(_) => println!("commit offset manually successfully."), + Err(err) => { + eprintln!("Failed to commit offset manually, dsn: {}; ErrMessage: {}", dsn, err); + return Err(err.into()); + } + } Ok(()) }) - .await?; + .await.map_err(|e| { + eprintln!("Failed to execute consumer functions. ErrMessage: {:?}", e); + e + })?; // ANCHOR_END: consumer_commit_manually - // ANCHOR: assignments + + // ANCHOR: seek_offset let assignments = consumer.assignments().await.unwrap(); - log::info!("assignments: {:?}", assignments); - // ANCHOR_END: assignments + println!("assignments: {:?}", assignments); // seek offset for topic_vec_assignment in assignments { @@ -125,7 +151,7 @@ async fn main() -> anyhow::Result<()> { let current = assignment.current_offset(); let begin = assignment.begin(); let end = assignment.end(); - log::debug!( + println!( "topic: {}, vgroup_id: {}, current offset: {} begin {}, end: {}", topic, vgroup_id, @@ -133,23 +159,24 @@ async fn main() -> anyhow::Result<()> { begin, end ); - // ANCHOR: seek_offset - let res = consumer.offset_seek(topic, vgroup_id, end).await; - if res.is_err() { - log::error!("seek offset error: {:?}", res); - let a = consumer.assignments().await.unwrap(); - log::error!("assignments: {:?}", a); + + match consumer.offset_seek(topic, vgroup_id, begin).await{ + Ok(_) => (), + Err(err) => { + eprintln!("seek example failed; ErrMessage: {}", err); + return Err(err.into()); + } } - // ANCHOR_END: seek_offset } let topic_assignment = consumer.topic_assignment(topic).await; - log::debug!("topic assignment: {:?}", topic_assignment); + println!("topic assignment: {:?}", topic_assignment); } - + println!("assignment seek to beginning successfully."); // after seek offset let assignments = consumer.assignments().await.unwrap(); - log::info!("after seek offset assignments: {:?}", assignments); + println!("after seek offset assignments: {:?}", assignments); + // ANCHOR_END: seek_offset // ANCHOR: unsubscribe consumer.unsubscribe().await; diff --git a/docs/zh/08-develop/02-sql.md b/docs/zh/08-develop/02-sql.md index 1651bf21cd..7e42e38949 100644 --- a/docs/zh/08-develop/02-sql.md +++ b/docs/zh/08-develop/02-sql.md @@ -53,7 +53,7 @@ REST API:直接调用 `taosadapter` 提供的 REST API 接口,进行数据 ```rust -{{#include docs/examples/rust/nativeexample/examples/query.rs:create_db_and_table}} +{{#include docs/examples/rust/nativeexample/examples/createdb.rs:create_db_and_table}} ``` @@ -129,7 +129,7 @@ NOW 为系统内部函数,默认为客户端所在计算机当前时间。 NOW ```rust -{{#include docs/examples/rust/nativeexample/examples/query.rs:insert_data}} +{{#include docs/examples/rust/nativeexample/examples/insert.rs:insert_data}} ``` diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java index f2fa345f47..64042ed309 100644 --- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java +++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java @@ -42,10 +42,10 @@ public class ConsumerLoopFull { return consumer; } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info - System.out.println("Failed to create websocket consumer, host : " + config.getProperty("bootstrap.servers") + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); + System.out.println("Failed to create native consumer, host : " + config.getProperty("bootstrap.servers") + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); throw new SQLException("Failed to create consumer", ex); } catch (Exception ex) { - System.out.println("Failed to create websocket consumer, host : " + config.getProperty("bootstrap.servers") + System.out.println("Failed to create native consumer, host : " + config.getProperty("bootstrap.servers") + "; ErrMessage: " + ex.getMessage()); throw new SQLException("Failed to create consumer", ex); } @@ -59,7 +59,7 @@ public class ConsumerLoopFull { // subscribe to the topics consumer.subscribe(topics); - System.out.println("subscribe topics successfully"); + System.out.println("subscribe topics successfully."); for (int i = 0; i < 50; i++) { // poll data ConsumerRecords records = consumer.poll(Duration.ofMillis(100)); @@ -88,7 +88,7 @@ public class ConsumerLoopFull { // subscribe to the topics consumer.subscribe(topics); - System.out.println("subscribe topics successfully"); + System.out.println("subscribe topics successfully."); Set assignment = consumer.assignment(); System.out.println("now assignment: " + JSON.toJSONString(assignment)); @@ -99,7 +99,7 @@ public class ConsumerLoopFull { } consumer.seekToBeginning(assignment); - System.out.println("assignment seek to beginning successfully"); + System.out.println("assignment seek to beginning successfully."); } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info System.out.println("seek example failed; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); @@ -317,22 +317,22 @@ public class ConsumerLoopFull { System.out.println("Failed to prepare data, ErrMessage: " + ex.getMessage()); return; } - System.out.println("pollDataExample executed successfully"); + System.out.println("pollDataExample executed successfully."); }); try { TaosConsumer consumer = getConsumer(); pollExample(consumer); - System.out.println("pollExample executed successfully"); + System.out.println("pollExample executed successfully."); consumer.unsubscribe(); seekExample(consumer); - System.out.println("seekExample executed successfully"); + System.out.println("seekExample executed successfully."); consumer.unsubscribe(); commitExample(consumer); - System.out.println("commitExample executed successfully"); + System.out.println("commitExample executed successfully."); consumer.unsubscribe(); unsubscribeExample(consumer); diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java index 3ac51f61fc..78f743b55d 100644 --- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java +++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java @@ -57,7 +57,7 @@ public class WsConsumerLoopFull { // subscribe to the topics consumer.subscribe(topics); - System.out.println("subscribe topics successfully"); + System.out.println("subscribe topics successfully."); for (int i = 0; i < 50; i++) { // poll data ConsumerRecords records = consumer.poll(Duration.ofMillis(100)); @@ -86,7 +86,7 @@ public class WsConsumerLoopFull { // subscribe to the topics consumer.subscribe(topics); - System.out.println("subscribe topics successfully"); + System.out.println("subscribe topics successfully."); Set assignment = consumer.assignment(); System.out.println("now assignment: " + JSON.toJSONString(assignment)); @@ -97,7 +97,7 @@ public class WsConsumerLoopFull { } consumer.seekToBeginning(assignment); - System.out.println("assignment seek to beginning successfully"); + System.out.println("assignment seek to beginning successfully."); } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info System.out.println("seek example failed; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); @@ -315,26 +315,26 @@ public class WsConsumerLoopFull { System.out.println("Failed to prepare data, ErrMessage: " + ex.getMessage()); return; } - System.out.println("pollDataExample executed successfully"); + System.out.println("pollDataExample executed successfully."); }); try { TaosConsumer consumer = getConsumer(); pollExample(consumer); - System.out.println("pollExample executed successfully"); + System.out.println("pollExample executed successfully."); consumer.unsubscribe(); seekExample(consumer); - System.out.println("seekExample executed successfully"); + System.out.println("seekExample executed successfully."); consumer.unsubscribe(); commitExample(consumer); - System.out.println("commitExample executed successfully"); + System.out.println("commitExample executed successfully."); consumer.unsubscribe(); unsubscribeExample(consumer); - System.out.println("unsubscribeExample executed successfully"); + System.out.println("unsubscribeExample executed successfully."); } catch (SQLException ex) { System.out.println("Failed to poll data from topic_meters, ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); From d72e0307f0e669a09216ab93fa98e0bbe188458c Mon Sep 17 00:00:00 2001 From: sheyanjie-qq <249478495@qq.com> Date: Mon, 12 Aug 2024 17:44:23 +0800 Subject: [PATCH 22/39] mod rust log --- docs/examples/rust/restexample/examples/tmq.rs | 1 + .../main/java/com/taosdata/example/ConsumerLoopFull.java | 6 +++--- .../main/java/com/taosdata/example/WsConsumerLoopFull.java | 6 +++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/docs/examples/rust/restexample/examples/tmq.rs b/docs/examples/rust/restexample/examples/tmq.rs index 046e1ec59c..c828bd91ac 100644 --- a/docs/examples/rust/restexample/examples/tmq.rs +++ b/docs/examples/rust/restexample/examples/tmq.rs @@ -180,6 +180,7 @@ async fn main() -> anyhow::Result<()> { // ANCHOR: unsubscribe consumer.unsubscribe().await; + println!("consumer unsubscribed successfully."); // ANCHOR_END: unsubscribe tokio::time::sleep(Duration::from_secs(1)).await; diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java index 64042ed309..39782e787c 100644 --- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java +++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java @@ -72,10 +72,10 @@ public class ConsumerLoopFull { } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info - System.out.println("Failed to poll data; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); + System.out.println("Failed to poll data, ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); throw new SQLException("Failed to poll data", ex); } catch (Exception ex) { - System.out.println("Failed to poll data; ErrMessage: " + ex.getMessage()); + System.out.println("Failed to poll data, ErrMessage: " + ex.getMessage()); throw new SQLException("Failed to poll data", ex); } // ANCHOR_END: poll_data_code_piece @@ -149,7 +149,7 @@ public class ConsumerLoopFull { try { // unsubscribe the consumer consumer.unsubscribe(); - System.out.println("unsubscribe consumer successfully."); + System.out.println("consumer unsubscribed successfully."); } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info System.out.println("Failed to unsubscribe consumer. ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java index 78f743b55d..45767f8461 100644 --- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java +++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java @@ -70,10 +70,10 @@ public class WsConsumerLoopFull { } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info - System.out.println("Failed to poll data; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); + System.out.println("Failed to poll data, ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); throw new SQLException("Failed to poll data", ex); } catch (Exception ex) { - System.out.println("Failed to poll data; ErrMessage: " + ex.getMessage()); + System.out.println("Failed to poll data, ErrMessage: " + ex.getMessage()); throw new SQLException("Failed to poll data", ex); } // ANCHOR_END: poll_data_code_piece @@ -147,7 +147,7 @@ public class WsConsumerLoopFull { try { // unsubscribe the consumer consumer.unsubscribe(); - System.out.println("unsubscribe consumer successfully."); + System.out.println("consumer unsubscribed successfully."); } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info System.out.println("Failed to unsubscribe consumer. ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); From 96379b8c04b83af4174da34a122631476ca73c6a Mon Sep 17 00:00:00 2001 From: t_max <1172915550@qq.com> Date: Mon, 12 Aug 2024 18:21:19 +0800 Subject: [PATCH 23/39] docs: update go example --- docs/examples/go/sqlquery/main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/examples/go/sqlquery/main.go b/docs/examples/go/sqlquery/main.go index e03c2a14c4..1301c79325 100644 --- a/docs/examples/go/sqlquery/main.go +++ b/docs/examples/go/sqlquery/main.go @@ -35,7 +35,7 @@ func main() { } rowsAffected, err = res.RowsAffected() if err != nil { - log.Fatalln("Failed to get create create rowsAffected, url:" + taosDSN + "; ErrMessage: " + err.Error()) + log.Fatalln("Failed to get create db rowsAffected, url:" + taosDSN + "; ErrMessage: " + err.Error()) } // you can check rowsAffected here fmt.Println("Create stable power.meters successfully, rowsAffected:", rowsAffected) @@ -66,7 +66,7 @@ func main() { // query data, make sure the database and table are created before rows, err := db.Query("SELECT ts, current, location FROM power.meters limit 100") if err != nil { - log.Fatal("query data failed:", err) + log.Fatal("Failed to query data from power.meters, url:" + taosDSN + "; ErrMessage: " + err.Error()) } for rows.Next() { var ( @@ -76,7 +76,7 @@ func main() { ) err = rows.Scan(&ts, ¤t, &location) if err != nil { - log.Fatal("scan data failed:", err) + log.Fatal("Failed to scan data, url:" + taosDSN + "; ErrMessage: " + err.Error()) } // you can check data here fmt.Printf("ts: %s, current: %f, location: %s\n", ts, current, location) From d9b514dfe6ce40fac0dc07906f3d2a90628365b6 Mon Sep 17 00:00:00 2001 From: Yaming Pei Date: Mon, 12 Aug 2024 18:28:39 +0800 Subject: [PATCH 24/39] c language sample optimization --- docs/examples/c/connect_example.c | 2 +- docs/examples/c/create_db_demo.c | 6 +-- docs/examples/c/stmt_insert_demo.c | 2 +- docs/examples/c/tmq_demo.c | 70 +++++++++++++++++------------- docs/examples/c/with_reqid_demo.c | 4 +- 5 files changed, 48 insertions(+), 36 deletions(-) diff --git a/docs/examples/c/connect_example.c b/docs/examples/c/connect_example.c index 614e828018..53d2d0d59b 100644 --- a/docs/examples/c/connect_example.c +++ b/docs/examples/c/connect_example.c @@ -12,7 +12,7 @@ int main() { uint16_t port = 6030; // 0 means use the default port TAOS *taos = taos_connect(host, user, passwd, db, port); if (taos == NULL) { - printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } diff --git a/docs/examples/c/create_db_demo.c b/docs/examples/c/create_db_demo.c index 14848f8237..ff1f4e62fd 100644 --- a/docs/examples/c/create_db_demo.c +++ b/docs/examples/c/create_db_demo.c @@ -34,7 +34,7 @@ int code = 0; // connect TAOS *taos = taos_connect(host, user, password, NULL, port); if (taos == NULL) { - printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } @@ -43,7 +43,7 @@ if (taos == NULL) { TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power"); code = taos_errno(result); if (code != 0) { - printf("Failed to create database power, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, code, taos_errstr(result)); + printf("Failed to create database power, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; @@ -56,7 +56,7 @@ const char* sql = "CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, curre result = taos_query(taos, sql); code = taos_errno(result); if (code != 0) { - printf("Failed to create stable power.meters, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", host, port, code, taos_errstr(result)); + printf("Failed to create stable power.meters, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; diff --git a/docs/examples/c/stmt_insert_demo.c b/docs/examples/c/stmt_insert_demo.c index 4bf794bcda..854c9f86e2 100644 --- a/docs/examples/c/stmt_insert_demo.c +++ b/docs/examples/c/stmt_insert_demo.c @@ -170,7 +170,7 @@ int main() { uint16_t port = 6030; TAOS *taos = taos_connect(host, user, password, NULL, port); if (taos == NULL) { - printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); exit(EXIT_FAILURE); } diff --git a/docs/examples/c/tmq_demo.c b/docs/examples/c/tmq_demo.c index 6277d61b8a..6a9006fc99 100644 --- a/docs/examples/c/tmq_demo.c +++ b/docs/examples/c/tmq_demo.c @@ -33,9 +33,10 @@ void* prepare_data(void* arg) { const char *user = "root"; const char *password = "taosdata"; uint16_t port = 6030; + int code = 0; TAOS *pConn = taos_connect(host, user, password, NULL, port); if (pConn == NULL) { - fprintf(stderr, "Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return NULL; } @@ -53,8 +54,9 @@ void* prepare_data(void* arg) { i); pRes = taos_query(pConn, buf); - if (taos_errno(pRes) != 0) { - fprintf(stderr, "Failed to insert data to power.meters, reason: %s\n", taos_errstr(pRes)); + code = taos_errno(pRes); + if (code != 0) { + fprintf(stderr, "Failed to insert data to power.meters, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); } taos_free_result(pRes); sleep(1); @@ -105,9 +107,10 @@ static int32_t init_env() { const char *user = "root"; const char *password = "taosdata"; uint16_t port = 6030; + int code = 0; TAOS *pConn = taos_connect(host, user, password, NULL, port); if (pConn == NULL) { - fprintf(stderr, "Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } @@ -116,21 +119,24 @@ static int32_t init_env() { // drop database if exists fprintf(stdout, "Create database.\n"); pRes = taos_query(pConn, "drop topic if exists topic_meters"); - if (taos_errno(pRes) != 0) { - fprintf(stderr, "Failed to drop topic_meters, reason:%s\n", taos_errstr(pRes)); + code = taos_errno(pRes); + if (code != 0) { + fprintf(stderr, "Failed to drop topic_meters, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); } taos_free_result(pRes); pRes = taos_query(pConn, "drop database if exists power"); - if (taos_errno(pRes) != 0) { - fprintf(stderr, "Failed to drop database power, reason:%s\n", taos_errstr(pRes)); + code = taos_errno(pRes); + if (code != 0) { + fprintf(stderr, "Failed to drop database power, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); } taos_free_result(pRes); // create database pRes = taos_query(pConn, "create database power precision 'ms' WAL_RETENTION_PERIOD 3600"); - if (taos_errno(pRes) != 0) { - fprintf(stderr, "Failed to create tmqdb, reason:%s\n", taos_errstr(pRes)); + code = taos_errno(pRes); + if (code != 0) { + fprintf(stderr, "Failed to create tmqdb, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); goto END; } taos_free_result(pRes); @@ -141,8 +147,9 @@ static int32_t init_env() { pConn, "CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS " "(groupId INT, location BINARY(24))"); - if (taos_errno(pRes) != 0) { - fprintf(stderr, "Failed to create super table meters, reason:%s\n", taos_errstr(pRes)); + code = taos_errno(pRes); + if (code != 0) { + fprintf(stderr, "Failed to create super table meters, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); goto END; } @@ -163,16 +170,18 @@ int32_t create_topic() { const char *user = "root"; const char *password = "taosdata"; uint16_t port = 6030; + int code = 0; TAOS *pConn = taos_connect(host, user, password, NULL, port); if (pConn == NULL) { - fprintf(stderr, "Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } pRes = taos_query(pConn, "use power"); + code = taos_errno(pRes); if (taos_errno(pRes) != 0) { - fprintf(stderr, "Failed to use tmqdb, reason:%s\n", taos_errstr(pRes)); + fprintf(stderr, "Failed to use tmqdb, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); return -1; } taos_free_result(pRes); @@ -180,8 +189,9 @@ int32_t create_topic() { pRes = taos_query( pConn, "CREATE TOPIC IF NOT EXISTS topic_meters AS SELECT ts, current, voltage, phase, groupid, location FROM meters"); - if (taos_errno(pRes) != 0) { - fprintf(stderr, "Failed to create topic topic_meters, reason:%s\n", taos_errstr(pRes)); + code = taos_errno(pRes); + if (code != 0) { + fprintf(stderr, "Failed to create topic topic_meters, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); return -1; } taos_free_result(pRes); @@ -263,7 +273,7 @@ tmq_list_t* build_topic_list() { if (code) { // if failed, destroy the list and return NULL tmq_list_destroy(topicList); - fprintf(stderr, "Failed to create topic_list, ErrCode: 0x%x; ErrMessage: %s.\n", code, tmq_err2str(code)); + fprintf(stderr, "Failed to create topic_list, ErrCode: 0x%x, ErrMessage: %s.\n", code, tmq_err2str(code)); return NULL; } // if success, return the list @@ -306,7 +316,7 @@ void consume_repeatly(tmq_t* tmq) { // get the topic assignment int32_t code = tmq_get_topic_assignment(tmq, topic_name, &pAssign, &numOfAssignment); if (code != 0 || pAssign == NULL || numOfAssignment == 0) { - fprintf(stderr, "Failed to get assignment, reason:%s", tmq_err2str(code)); + fprintf(stderr, "Failed to get assignment, ErrCode: 0x%x, ErrMessage: %s.\n", code, tmq_err2str(code)); return; } @@ -316,7 +326,7 @@ void consume_repeatly(tmq_t* tmq) { code = tmq_offset_seek(tmq, topic_name, p->vgId, p->begin); if (code != 0) { - fprintf(stderr, "Failed to seek assignment %d to beginning %ld, reason: %s.\n", i, p->begin, tmq_err2str(code)); + fprintf(stderr, "Failed to seek assignment %d to beginning %ld, ErrCode: 0x%x, ErrMessage: %s.\n", i, p->begin, code, tmq_err2str(code)); } else { fprintf(stdout, "Seek assignment %d to beginning %ld successfully.\n", i, p->begin); } @@ -347,12 +357,12 @@ void manual_commit(tmq_t* tmq) { int32_t code = tmq_commit_sync(tmq, tmqmsg); if (code) { - fprintf(stderr, "Failed to commit message: %s\n", tmq_err2str(code)); + fprintf(stderr, "Failed to commit message, ErrCode: 0x%x, ErrMessage: %s.\n", code, tmq_err2str(code)); // free the message taos_free_result(tmqmsg); break; } else { - fprintf(stdout, "Commit offset manually successfully."); + fprintf(stdout, "commit offset manually successfully."); } // free the message taos_free_result(tmqmsg); @@ -364,7 +374,7 @@ void manual_commit(tmq_t* tmq) { } // print the result: total messages and total rows consumed - fprintf(stderr, "%d msg consumed, include %d rows\n", msgCnt, totalRows); + fprintf(stderr, "%d msg consumed, include %d rows.\n", msgCnt, totalRows); } // ANCHOR_END: manual_commit @@ -381,7 +391,7 @@ int main(int argc, char* argv[]) { } if (pthread_create(&thread_id, NULL, &prepare_data, NULL)) { - fprintf(stderr, "Create thread failed\n"); + fprintf(stderr, "Failed to create thread.\n"); return 1; } @@ -403,9 +413,9 @@ int main(int argc, char* argv[]) { } if ((code = tmq_subscribe(tmq, topic_list))) { - fprintf(stderr, "Failed to subscribe tmq_subscribe(): %s\n", tmq_err2str(code)); + fprintf(stderr, "Failed to subscribe topic_list, ErrCode: 0x%x, ErrMessage: %s.\n", code, tmq_err2str(code)); } else { - fprintf(stdout, "Subscribe topics successfully.\n"); + fprintf(stdout, "subscribe topics successfully.\n"); } tmq_list_destroy(topic_list); @@ -421,15 +431,17 @@ int main(int argc, char* argv[]) { // unsubscribe the topic code = tmq_unsubscribe(tmq); if (code) { - fprintf(stderr, "Failed to tmq_unsubscribe(): %s\n", tmq_err2str(code)); + fprintf(stderr, "Failed to unsubscribe consumer, ErrCode: 0x%x, ErrMessage: %s.\n", code, tmq_err2str(code)); + } else { + fprintf(stderr, "consumer unsubscribed successfully.\n"); } - fprintf(stderr, "Unsubscribed consumer successfully.\n"); + // close the consumer code = tmq_consumer_close(tmq); if (code) { - fprintf(stderr, "Failed to close consumer: %s\n", tmq_err2str(code)); + fprintf(stderr, "Failed to close consumer: %s.\n", tmq_err2str(code)); } else { - fprintf(stderr, "Consumer closed successfully.\n"); + fprintf(stderr, "consumer closed successfully.\n"); } // ANCHOR_END: unsubscribe_and_close diff --git a/docs/examples/c/with_reqid_demo.c b/docs/examples/c/with_reqid_demo.c index 99f03a579e..724b682850 100644 --- a/docs/examples/c/with_reqid_demo.c +++ b/docs/examples/c/with_reqid_demo.c @@ -33,7 +33,7 @@ int code = 0; // connect TAOS *taos = taos_connect(host, user, password, NULL, port); if (taos == NULL) { - printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } @@ -44,7 +44,7 @@ long reqid = 3L; TAOS_RES *result = taos_query_with_reqid(taos, sql, reqid); code = taos_errno(result); if (code != 0) { - printf("Failed to execute sql with reqId: %ld, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", reqid, host, port, code, taos_errstr(result)); + printf("Failed to execute sql with reqId: %ld, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", reqid, host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; From a3637af498fcd5f650b1ae5f4f3c367f148194bf Mon Sep 17 00:00:00 2001 From: sima Date: Mon, 12 Aug 2024 15:39:29 +0800 Subject: [PATCH 25/39] fix:[TD-31374] vectorConvertSingleCol set precision only when type is timestamp. --- source/libs/scalar/src/sclvector.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/libs/scalar/src/sclvector.c b/source/libs/scalar/src/sclvector.c index 376a8a11d8..71773ced57 100644 --- a/source/libs/scalar/src/sclvector.c +++ b/source/libs/scalar/src/sclvector.c @@ -1037,8 +1037,8 @@ int32_t vectorConvertSingleCol(SScalarParam *input, SScalarParam *output, int32_ output->numOfRows = input->numOfRows; SDataType t = {.type = type}; - t.bytes = IS_VAR_DATA_TYPE(t.type)? input->columnData->info.bytes:tDataTypes[type].bytes; - t.precision = input->columnData->info.precision; + t.bytes = (IS_VAR_DATA_TYPE(t.type) && input->columnData) ? input->columnData->info.bytes:tDataTypes[type].bytes; + t.precision = (IS_TIMESTAMP_TYPE(t.type) && input->columnData) ? input->columnData->info.precision : TSDB_TIME_PRECISION_MILLI; int32_t code = sclCreateColumnInfoData(&t, input->numOfRows, output); if (code != TSDB_CODE_SUCCESS) { From d30cda78d436f264da6a976b936df06116b38414 Mon Sep 17 00:00:00 2001 From: Yaming Pei Date: Mon, 12 Aug 2024 19:00:37 +0800 Subject: [PATCH 26/39] mod c sample code --- docs/examples/c/tmq_demo.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/examples/c/tmq_demo.c b/docs/examples/c/tmq_demo.c index 6a9006fc99..d3285cb2d0 100644 --- a/docs/examples/c/tmq_demo.c +++ b/docs/examples/c/tmq_demo.c @@ -118,14 +118,14 @@ static int32_t init_env() { TAOS_RES* pRes; // drop database if exists fprintf(stdout, "Create database.\n"); - pRes = taos_query(pConn, "drop topic if exists topic_meters"); + pRes = taos_query(pConn, "DROP TOPIC IF EXISTS topic_meters"); code = taos_errno(pRes); if (code != 0) { fprintf(stderr, "Failed to drop topic_meters, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); } taos_free_result(pRes); - pRes = taos_query(pConn, "drop database if exists power"); + pRes = taos_query(pConn, "DROP DATABASE IF EXISTS power"); code = taos_errno(pRes); if (code != 0) { fprintf(stderr, "Failed to drop database power, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); @@ -133,7 +133,7 @@ static int32_t init_env() { taos_free_result(pRes); // create database - pRes = taos_query(pConn, "create database power precision 'ms' WAL_RETENTION_PERIOD 3600"); + pRes = taos_query(pConn, "CREATE DATABASE power PRECISION 'ms' WAL_RETENTION_PERIOD 3600"); code = taos_errno(pRes); if (code != 0) { fprintf(stderr, "Failed to create tmqdb, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); @@ -178,7 +178,7 @@ int32_t create_topic() { return -1; } - pRes = taos_query(pConn, "use power"); + pRes = taos_query(pConn, "USE POWER"); code = taos_errno(pRes); if (taos_errno(pRes) != 0) { fprintf(stderr, "Failed to use tmqdb, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); @@ -362,7 +362,7 @@ void manual_commit(tmq_t* tmq) { taos_free_result(tmqmsg); break; } else { - fprintf(stdout, "commit offset manually successfully."); + fprintf(stdout, "Commit offset manually successfully.\n"); } // free the message taos_free_result(tmqmsg); @@ -415,7 +415,7 @@ int main(int argc, char* argv[]) { if ((code = tmq_subscribe(tmq, topic_list))) { fprintf(stderr, "Failed to subscribe topic_list, ErrCode: 0x%x, ErrMessage: %s.\n", code, tmq_err2str(code)); } else { - fprintf(stdout, "subscribe topics successfully.\n"); + fprintf(stdout, "Subscribe topics successfully.\n"); } tmq_list_destroy(topic_list); @@ -433,7 +433,7 @@ int main(int argc, char* argv[]) { if (code) { fprintf(stderr, "Failed to unsubscribe consumer, ErrCode: 0x%x, ErrMessage: %s.\n", code, tmq_err2str(code)); } else { - fprintf(stderr, "consumer unsubscribed successfully.\n"); + fprintf(stderr, "Consumer unsubscribed successfully.\n"); } // close the consumer @@ -441,7 +441,7 @@ int main(int argc, char* argv[]) { if (code) { fprintf(stderr, "Failed to close consumer: %s.\n", tmq_err2str(code)); } else { - fprintf(stderr, "consumer closed successfully.\n"); + fprintf(stderr, "Consumer closed successfully.\n"); } // ANCHOR_END: unsubscribe_and_close From edcc5f5b67e52453b3a42a22b6d97395153084fc Mon Sep 17 00:00:00 2001 From: sheyanjie-qq <249478495@qq.com> Date: Mon, 12 Aug 2024 19:05:01 +0800 Subject: [PATCH 27/39] improve log --- .../rust/nativeexample/examples/tmq.rs | 15 ++++++++------- docs/examples/rust/restexample/examples/tmq.rs | 6 +++--- .../com/taosdata/example/ConsumerLoopFull.java | 18 +++++++++--------- .../taosdata/example/WsConsumerLoopFull.java | 18 +++++++++--------- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/docs/examples/rust/nativeexample/examples/tmq.rs b/docs/examples/rust/nativeexample/examples/tmq.rs index 4ae71bc445..7f3bd416d1 100644 --- a/docs/examples/rust/nativeexample/examples/tmq.rs +++ b/docs/examples/rust/nativeexample/examples/tmq.rs @@ -61,7 +61,7 @@ async fn main() -> anyhow::Result<()> { // ANCHOR: consume match consumer.subscribe(["topic_meters"]).await{ - Ok(_) => println!("subscribe topics successfully."), + Ok(_) => println!("Subscribe topics successfully."), Err(err) => { eprintln!("Failed to subscribe topic_meters, dsn: {}; ErrMessage: {}", dsn, err); return Err(err.into()); @@ -123,7 +123,7 @@ async fn main() -> anyhow::Result<()> { } // commit offset manually when you have processed the message. match consumer.commit(offset).await{ - Ok(_) => println!("commit offset manually successfully."), + Ok(_) => println!("Commit offset manually successfully."), Err(err) => { eprintln!("Failed to commit offset manually, dsn: {}; ErrMessage: {}", dsn, err); return Err(err.into()); @@ -140,7 +140,7 @@ async fn main() -> anyhow::Result<()> { // ANCHOR: seek_offset let assignments = consumer.assignments().await.unwrap(); - println!("assignments: {:?}", assignments); + println!("Now assignments: {:?}", assignments); // seek offset for topic_vec_assignment in assignments { @@ -163,23 +163,24 @@ async fn main() -> anyhow::Result<()> { match consumer.offset_seek(topic, vgroup_id, begin).await{ Ok(_) => (), Err(err) => { - eprintln!("seek example failed; ErrMessage: {}", err); + eprintln!("Seek example failed; ErrMessage: {}", err); return Err(err.into()); } } } let topic_assignment = consumer.topic_assignment(topic).await; - println!("topic assignment: {:?}", topic_assignment); + println!("Topic assignment: {:?}", topic_assignment); } - println!("assignment seek to beginning successfully."); + println!("Assignment seek to beginning successfully."); // after seek offset let assignments = consumer.assignments().await.unwrap(); - println!("after seek offset assignments: {:?}", assignments); + println!("After seek offset assignments: {:?}", assignments); // ANCHOR_END: seek_offset // ANCHOR: unsubscribe consumer.unsubscribe().await; + println!("Consumer unsubscribed successfully."); // ANCHOR_END: unsubscribe tokio::time::sleep(Duration::from_secs(1)).await; diff --git a/docs/examples/rust/restexample/examples/tmq.rs b/docs/examples/rust/restexample/examples/tmq.rs index c828bd91ac..37fb9edc21 100644 --- a/docs/examples/rust/restexample/examples/tmq.rs +++ b/docs/examples/rust/restexample/examples/tmq.rs @@ -172,15 +172,15 @@ async fn main() -> anyhow::Result<()> { let topic_assignment = consumer.topic_assignment(topic).await; println!("topic assignment: {:?}", topic_assignment); } - println!("assignment seek to beginning successfully."); + println!("Assignment seek to beginning successfully."); // after seek offset let assignments = consumer.assignments().await.unwrap(); - println!("after seek offset assignments: {:?}", assignments); + println!("After seek offset assignments: {:?}", assignments); // ANCHOR_END: seek_offset // ANCHOR: unsubscribe consumer.unsubscribe().await; - println!("consumer unsubscribed successfully."); + println!("Consumer unsubscribed successfully."); // ANCHOR_END: unsubscribe tokio::time::sleep(Duration::from_secs(1)).await; diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java index 39782e787c..3c0798d198 100644 --- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java +++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java @@ -59,7 +59,7 @@ public class ConsumerLoopFull { // subscribe to the topics consumer.subscribe(topics); - System.out.println("subscribe topics successfully."); + System.out.println("Subscribe topics successfully."); for (int i = 0; i < 50; i++) { // poll data ConsumerRecords records = consumer.poll(Duration.ofMillis(100)); @@ -88,9 +88,9 @@ public class ConsumerLoopFull { // subscribe to the topics consumer.subscribe(topics); - System.out.println("subscribe topics successfully."); + System.out.println("Subscribe topics successfully."); Set assignment = consumer.assignment(); - System.out.println("now assignment: " + JSON.toJSONString(assignment)); + System.out.println("Now assignment: " + JSON.toJSONString(assignment)); ConsumerRecords records = ConsumerRecords.emptyRecord(); // make sure we have got some data @@ -99,13 +99,13 @@ public class ConsumerLoopFull { } consumer.seekToBeginning(assignment); - System.out.println("assignment seek to beginning successfully."); + System.out.println("Assignment seek to beginning successfully."); } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info - System.out.println("seek example failed; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); + System.out.println("Seek example failed; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); throw new SQLException("seek example failed", ex); } catch (Exception ex) { - System.out.println("seek example failed; ErrMessage: " + ex.getMessage()); + System.out.println("Seek example failed; ErrMessage: " + ex.getMessage()); throw new SQLException("seek example failed", ex); } // ANCHOR_END: consumer_seek @@ -128,7 +128,7 @@ public class ConsumerLoopFull { if (!records.isEmpty()) { // after processing the data, commit the offset manually consumer.commitSync(); - System.out.println("commit offset manually successfully."); + System.out.println("Commit offset manually successfully."); } } } catch (SQLException ex) { @@ -149,7 +149,7 @@ public class ConsumerLoopFull { try { // unsubscribe the consumer consumer.unsubscribe(); - System.out.println("consumer unsubscribed successfully."); + System.out.println("Consumer unsubscribed successfully."); } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info System.out.println("Failed to unsubscribe consumer. ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); @@ -161,7 +161,7 @@ public class ConsumerLoopFull { finally { // close the consumer consumer.close(); - System.out.println("consumer closed successfully."); + System.out.println("Consumer closed successfully."); } // ANCHOR_END: unsubscribe_data_code_piece } diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java index 45767f8461..d7207ffe71 100644 --- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java +++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java @@ -57,7 +57,7 @@ public class WsConsumerLoopFull { // subscribe to the topics consumer.subscribe(topics); - System.out.println("subscribe topics successfully."); + System.out.println("Subscribe topics successfully."); for (int i = 0; i < 50; i++) { // poll data ConsumerRecords records = consumer.poll(Duration.ofMillis(100)); @@ -86,9 +86,9 @@ public class WsConsumerLoopFull { // subscribe to the topics consumer.subscribe(topics); - System.out.println("subscribe topics successfully."); + System.out.println("Subscribe topics successfully."); Set assignment = consumer.assignment(); - System.out.println("now assignment: " + JSON.toJSONString(assignment)); + System.out.println("Now assignment: " + JSON.toJSONString(assignment)); ConsumerRecords records = ConsumerRecords.emptyRecord(); // make sure we have got some data @@ -97,13 +97,13 @@ public class WsConsumerLoopFull { } consumer.seekToBeginning(assignment); - System.out.println("assignment seek to beginning successfully."); + System.out.println("Assignment seek to beginning successfully."); } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info - System.out.println("seek example failed; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); + System.out.println("Seek example failed; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); throw new SQLException("seek example failed", ex); } catch (Exception ex) { - System.out.println("seek example failed; ErrMessage: " + ex.getMessage()); + System.out.println("Seek example failed; ErrMessage: " + ex.getMessage()); throw new SQLException("seek example failed", ex); } // ANCHOR_END: consumer_seek @@ -126,7 +126,7 @@ public class WsConsumerLoopFull { if (!records.isEmpty()) { // after processing the data, commit the offset manually consumer.commitSync(); - System.out.println("commit offset manually successfully."); + System.out.println("Commit offset manually successfully."); } } } catch (SQLException ex) { @@ -147,7 +147,7 @@ public class WsConsumerLoopFull { try { // unsubscribe the consumer consumer.unsubscribe(); - System.out.println("consumer unsubscribed successfully."); + System.out.println("Consumer unsubscribed successfully."); } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info System.out.println("Failed to unsubscribe consumer. ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); @@ -159,7 +159,7 @@ public class WsConsumerLoopFull { finally { // close the consumer consumer.close(); - System.out.println("consumer closed successfully."); + System.out.println("Consumer closed successfully."); } // ANCHOR_END: unsubscribe_data_code_piece } From 1690c81510d7de697e1d186b12210822546236f8 Mon Sep 17 00:00:00 2001 From: sheyanjie-qq <249478495@qq.com> Date: Mon, 12 Aug 2024 19:09:59 +0800 Subject: [PATCH 28/39] improve log --- docs/examples/rust/restexample/examples/tmq.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/examples/rust/restexample/examples/tmq.rs b/docs/examples/rust/restexample/examples/tmq.rs index 37fb9edc21..3f26414105 100644 --- a/docs/examples/rust/restexample/examples/tmq.rs +++ b/docs/examples/rust/restexample/examples/tmq.rs @@ -61,7 +61,7 @@ async fn main() -> anyhow::Result<()> { // ANCHOR: consume match consumer.subscribe(["topic_meters"]).await{ - Ok(_) => println!("subscribe topics successfully."), + Ok(_) => println!("Subscribe topics successfully."), Err(err) => { eprintln!("Failed to subscribe topic_meters, dsn: {}; ErrMessage: {}", dsn, err); return Err(err.into()); From 4d12ad127ee1f7ae98882890bad0b2dc7266cdd4 Mon Sep 17 00:00:00 2001 From: sheyanjie-qq <249478495@qq.com> Date: Mon, 12 Aug 2024 19:12:37 +0800 Subject: [PATCH 29/39] improve log --- docs/examples/rust/restexample/examples/tmq.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/examples/rust/restexample/examples/tmq.rs b/docs/examples/rust/restexample/examples/tmq.rs index 3f26414105..2abc0a36da 100644 --- a/docs/examples/rust/restexample/examples/tmq.rs +++ b/docs/examples/rust/restexample/examples/tmq.rs @@ -123,7 +123,7 @@ async fn main() -> anyhow::Result<()> { } // commit offset manually when you have processed the message. match consumer.commit(offset).await{ - Ok(_) => println!("commit offset manually successfully."), + Ok(_) => println!("Commit offset manually successfully."), Err(err) => { eprintln!("Failed to commit offset manually, dsn: {}; ErrMessage: {}", dsn, err); return Err(err.into()); From ffebe1d4c9ae601dc046fb231f028b6a9358487b Mon Sep 17 00:00:00 2001 From: menshibin Date: Mon, 12 Aug 2024 19:33:50 +0800 Subject: [PATCH 30/39] modify alert sql --- .../node/websocketexample/sql_example.js | 6 ++++-- docs/examples/python/connect_example.py | 2 +- docs/examples/python/connect_rest_example.py | 2 +- .../python/connect_websocket_examples.py | 2 +- docs/examples/python/create_db_native.py | 2 +- docs/examples/python/create_db_rest.py | 2 +- docs/examples/python/create_db_ws.py | 2 +- docs/examples/python/insert_native.py | 2 +- docs/examples/python/insert_rest.py | 2 +- docs/examples/python/insert_ws.py | 2 +- docs/examples/python/query_native.py | 2 +- docs/examples/python/query_rest.py | 2 +- docs/examples/python/query_ws.py | 2 +- docs/examples/python/reqid_native.py | 2 +- docs/examples/python/reqid_rest.py | 2 +- docs/examples/python/reqid_ws.py | 2 +- docs/examples/python/schemaless_native.py | 2 +- docs/examples/python/schemaless_ws.py | 2 +- docs/examples/python/stmt_native.py | 2 +- docs/examples/python/stmt_ws.py | 2 +- docs/examples/python/tmq_native.py | 14 +++++++------- docs/examples/python/tmq_websocket_example.py | 16 ++++++++-------- 22 files changed, 38 insertions(+), 36 deletions(-) diff --git a/docs/examples/node/websocketexample/sql_example.js b/docs/examples/node/websocketexample/sql_example.js index b594b9967b..e36bd037ed 100644 --- a/docs/examples/node/websocketexample/sql_example.js +++ b/docs/examples/node/websocketexample/sql_example.js @@ -25,8 +25,10 @@ async function createDbAndTable() { let wsSql = null; try { wsSql = await createConnect(); + // create database await wsSql.exec('CREATE DATABASE IF NOT EXISTS power'); - + console.log("Create database power successfully."); + // create table await wsSql.exec('CREATE STABLE IF NOT EXISTS power.meters ' + '(_ts timestamp, current float, voltage int, phase float) ' + 'TAGS (location binary(64), groupId int);'); @@ -109,7 +111,7 @@ async function sqlWithReqid() { } } catch (err) { - console.error("Failed to execute sql with reqId: " + reqId + "," + err.code + "; ErrMessage: " + err.message); + console.error("Failed to execute sql with reqId: " + reqId + ", ErrCode:" + err.code + "; ErrMessage: " + err.message); } finally { if (wsRows) { diff --git a/docs/examples/python/connect_example.py b/docs/examples/python/connect_example.py index 82698d8e7b..1f01a04e4c 100644 --- a/docs/examples/python/connect_example.py +++ b/docs/examples/python/connect_example.py @@ -14,7 +14,7 @@ def create_connection(): ) print(f"Connected to {host}:{port} successfully."); except Exception as err: - print(f"Failed to connect to {host}:{port} ; err:{err}") + print(f"Failed to connect to {host}:{port} ; ErrMessage:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/connect_rest_example.py b/docs/examples/python/connect_rest_example.py index b10f9e7cf0..bd65ce7970 100644 --- a/docs/examples/python/connect_rest_example.py +++ b/docs/examples/python/connect_rest_example.py @@ -12,7 +12,7 @@ def create_connection(): print(f"Connected to {url} successfully."); except Exception as err: - print(f"Failed to connect to {url} ; err:{err}") + print(f"Failed to connect to {url} ; ErrMessage:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/connect_websocket_examples.py b/docs/examples/python/connect_websocket_examples.py index 0cd418eda7..79e96dd09c 100644 --- a/docs/examples/python/connect_websocket_examples.py +++ b/docs/examples/python/connect_websocket_examples.py @@ -14,7 +14,7 @@ def create_connection(): ) print(f"Connected to {host}:{port} successfully."); except Exception as err: - print(f"Failed to connect to {host}:{port} ; Err:{err}") + print(f"Failed to connect to {host}:{port} ; ErrMessage:{err}") return conn # ANCHOR_END: connect diff --git a/docs/examples/python/create_db_native.py b/docs/examples/python/create_db_native.py index 94f0726269..1a98a242f2 100644 --- a/docs/examples/python/create_db_native.py +++ b/docs/examples/python/create_db_native.py @@ -20,7 +20,7 @@ try: print(f"Create stable power.meters successfully, rowsAffected: {rowsAffected}"); except Exception as err: - print(f"Failed to create db and table, db addr:{host}:{port} ; err:{err}") + print(f"Failed to create db and table, db addr:{host}:{port} ; ErrMessage:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/create_db_rest.py b/docs/examples/python/create_db_rest.py index d866f7aa1c..2387b80195 100644 --- a/docs/examples/python/create_db_rest.py +++ b/docs/examples/python/create_db_rest.py @@ -19,7 +19,7 @@ try: print(f"Create stable power.meters successfully, rowsAffected: {rowsAffected}"); except Exception as err: - print(f"Failed to create db and table, url:{url} ; err:{err}") + print(f"Failed to create db and table, url:{url} ; ErrMessage:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/create_db_ws.py b/docs/examples/python/create_db_ws.py index 1cc916e96d..7749ba617f 100644 --- a/docs/examples/python/create_db_ws.py +++ b/docs/examples/python/create_db_ws.py @@ -20,7 +20,7 @@ try: print(f"Create stable power.meters successfully, rowsAffected: {rowsAffected}"); except Exception as err: - print(f"Failed to create db and table, db addrr:{host}:{port} ; err:{err}") + print(f"Failed to create db and table, db addrr:{host}:{port} ; ErrMessage:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/insert_native.py b/docs/examples/python/insert_native.py index 67a3807c0a..5f1b6fbc21 100644 --- a/docs/examples/python/insert_native.py +++ b/docs/examples/python/insert_native.py @@ -21,7 +21,7 @@ try: print(f"Successfully inserted {affectedRows} rows to power.meters.") except Exception as err: - print(f"Failed to insert data to power.meters, db addr:{host}:{port} ; err:{err}") + print(f"Failed to insert data to power.meters, db addr:{host}:{port} ; ErrMessage:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/insert_rest.py b/docs/examples/python/insert_rest.py index 7f0881bd77..d8e07323a9 100644 --- a/docs/examples/python/insert_rest.py +++ b/docs/examples/python/insert_rest.py @@ -20,7 +20,7 @@ try: print(f"Successfully inserted {affectedRows} rows to power.meters.") except Exception as err: - print(f"Failed to insert data to power.meters, url:{url} ; err:{err}") + print(f"Failed to insert data to power.meters, url:{url} ; ErrMessage:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/insert_ws.py b/docs/examples/python/insert_ws.py index 254027b1a6..0e1f74e2cf 100644 --- a/docs/examples/python/insert_ws.py +++ b/docs/examples/python/insert_ws.py @@ -21,7 +21,7 @@ try: print(f"Successfully inserted {affectedRows} rows to power.meters.") except Exception as err: - print(f"Failed to insert data to power.meters, db addr:{host}:{port} ; err:{err}") + print(f"Failed to insert data to power.meters, db addr:{host}:{port} ; ErrMessage:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/query_native.py b/docs/examples/python/query_native.py index 08cc1462cb..df19819de7 100644 --- a/docs/examples/python/query_native.py +++ b/docs/examples/python/query_native.py @@ -15,7 +15,7 @@ try: print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}") except Exception as err: - print(f"Failed to query data from power.meters, db addr:{host}:{port} ; err:{err}") + print(f"Failed to query data from power.meters, db addr:{host}:{port} ; ErrMessage:{err}") finally: if conn: conn.close() \ No newline at end of file diff --git a/docs/examples/python/query_rest.py b/docs/examples/python/query_rest.py index 02d3536f82..de16ace8be 100644 --- a/docs/examples/python/query_rest.py +++ b/docs/examples/python/query_rest.py @@ -14,4 +14,4 @@ try: print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}") except Exception as err: - print(f"Failed to query data from power.meters, url:{url} ; err:{err}") + print(f"Failed to query data from power.meters, url:{url} ; ErrMessage:{err}") diff --git a/docs/examples/python/query_ws.py b/docs/examples/python/query_ws.py index e59977e0a1..8d177b167a 100644 --- a/docs/examples/python/query_ws.py +++ b/docs/examples/python/query_ws.py @@ -14,7 +14,7 @@ try: print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}") except Exception as err: - print(f"Failed to query data from power.meters, db addr:{host}:{port} ; err:{err}") + print(f"Failed to query data from power.meters, db addr:{host}:{port} ; ErrMessage:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/reqid_native.py b/docs/examples/python/reqid_native.py index 1ece38e67e..73fc05af53 100644 --- a/docs/examples/python/reqid_native.py +++ b/docs/examples/python/reqid_native.py @@ -17,7 +17,7 @@ try: print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}") except Exception as err: - print(f"Failed to execute sql with reqId:{reqId}, db addr:{host}:{port} ; err:{err}") + print(f"Failed to execute sql with reqId:{reqId}, db addr:{host}:{port} ; ErrMessage:{err}") finally: if conn: diff --git a/docs/examples/python/reqid_rest.py b/docs/examples/python/reqid_rest.py index 2a5379360f..a4a8999f3a 100644 --- a/docs/examples/python/reqid_rest.py +++ b/docs/examples/python/reqid_rest.py @@ -15,4 +15,4 @@ try: print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}") except Exception as err: - print(f"Failed to execute sql with reqId:{reqId}, url:{url} ; err:{err}") + print(f"Failed to execute sql with reqId:{reqId}, url:{url} ; ErrMessage:{err}") diff --git a/docs/examples/python/reqid_ws.py b/docs/examples/python/reqid_ws.py index f93b5ca1e8..a97410c2e4 100644 --- a/docs/examples/python/reqid_ws.py +++ b/docs/examples/python/reqid_ws.py @@ -18,7 +18,7 @@ try: print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}") except Exception as err: - print(f"Failed to execute sql with reqId:{reqId}, db addr:{host}:{port} ; err:{err}") + print(f"Failed to execute sql with reqId:{reqId}, db addr:{host}:{port} ; ErrMessage:{err}") finally: if conn: conn.close() \ No newline at end of file diff --git a/docs/examples/python/schemaless_native.py b/docs/examples/python/schemaless_native.py index 44d8edc475..0cb4d1c94d 100644 --- a/docs/examples/python/schemaless_native.py +++ b/docs/examples/python/schemaless_native.py @@ -34,7 +34,7 @@ try: ) print("Inserted data with schemaless successfully."); except Exception as err: - print(f"Failed to insert data with schemaless, addr: {host}:{port} err:{err}") + print(f"Failed to insert data with schemaless, addr: {host}:{port} ErrMessage:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/schemaless_ws.py b/docs/examples/python/schemaless_ws.py index 08c0926c47..f03f78b10f 100644 --- a/docs/examples/python/schemaless_ws.py +++ b/docs/examples/python/schemaless_ws.py @@ -66,7 +66,7 @@ def schemaless_insert(): ) print("Inserted data with schemaless successfully."); except Exception as err: - print(f"Failed to insert data with schemaless, addr: {host}:{port} err:{err}") + print(f"Failed to insert data with schemaless, addr: {host}:{port} ErrMessage:{err}") raise err finally: diff --git a/docs/examples/python/stmt_native.py b/docs/examples/python/stmt_native.py index 138272204f..ea0cf9c626 100644 --- a/docs/examples/python/stmt_native.py +++ b/docs/examples/python/stmt_native.py @@ -56,7 +56,7 @@ try: print(f"Successfully inserted to power.meters.") except Exception as err: - print(f"Failed to insert to table meters using stmt, addr:{host}:{port} ; err:{err}") + print(f"Failed to insert to table meters using stmt, addr:{host}:{port} ; ErrMessage:{err}") finally: if stmt: stmt.close() diff --git a/docs/examples/python/stmt_ws.py b/docs/examples/python/stmt_ws.py index c391432300..8d992b32e6 100644 --- a/docs/examples/python/stmt_ws.py +++ b/docs/examples/python/stmt_ws.py @@ -61,7 +61,7 @@ try: print(f"Successfully inserted to power.meters.") except Exception as err: - print(f"Failed to insert to table meters using stmt, addr:{host}:{port} ; err:{err}") + print(f"Failed to insert to table meters using stmt, addr:{host}:{port} ; ErrMessage:{err}") finally: if stmt: stmt.close() diff --git a/docs/examples/python/tmq_native.py b/docs/examples/python/tmq_native.py index fa813c9084..3e7fd1c53a 100644 --- a/docs/examples/python/tmq_native.py +++ b/docs/examples/python/tmq_native.py @@ -39,7 +39,7 @@ def prepareMeta(): affectedRows = conn.execute(sql) print(f"inserted into {affectedRows} rows to power.meters successfully.") except Exception as err: - print(f"prepare meta err:{err}") + print(f"prepare meta ErrMessage:{err}") raise err finally: if conn: @@ -70,7 +70,7 @@ def create_consumer(): print(f"Create consumer successfully, host: {host}:{port}, groupId: {groupId}, clientId: {clientId}"); return consumer except Exception as err: - print(f"Failed to create websocket consumer, host: {host}:{port} ; err:{err}"); + print(f"Failed to create websocket consumer, host: {host}:{port} ; ErrMessage:{err}"); raise err # ANCHOR_END: create_consumer @@ -96,7 +96,7 @@ def subscribe(consumer): print(f"data: {data}") except Exception as err: - print(f"Failed to poll data, err:{err}") + print(f"Failed to poll data, ErrMessage:{err}") raise err @@ -123,7 +123,7 @@ def commit_offset(consumer): print("commit offset manually successfully."); except Exception as err: - print(f"Failed to poll data, err:{err}") + print(f"Failed to poll data, ErrMessage:{err}") raise err # ANCHOR_END: commit_offset @@ -139,7 +139,7 @@ def seek_offset(consumer): consumer.seek(partition) print(f"assignment seek to beginning successfully"); except Exception as err: - print(f"seek example failed; err:{err}") + print(f"seek example failed; ErrMessage:{err}") raise err # ANCHOR_END: assignment @@ -150,7 +150,7 @@ def unsubscribe(consumer): consumer.unsubscribe() print("unsubscribe consumer successfully."); except Exception as err: - print(f"Failed to unsubscribe consumer. err:{err}") + print(f"Failed to unsubscribe consumer. ErrMessage:{err}") # ANCHOR_END: unsubscribe @@ -165,7 +165,7 @@ if __name__ == "__main__": commit_offset(consumer) unsubscribe(consumer) except Exception as err: - print(f"Failed to stmt consumer. err:{err}") + print(f"Failed to stmt consumer. ErrMessage:{err}") finally: if consumer: consumer.close() \ No newline at end of file diff --git a/docs/examples/python/tmq_websocket_example.py b/docs/examples/python/tmq_websocket_example.py index 94a3ddb6ee..2fe2ae57d5 100644 --- a/docs/examples/python/tmq_websocket_example.py +++ b/docs/examples/python/tmq_websocket_example.py @@ -51,7 +51,7 @@ def prepareMeta(): print(f"inserted into {affectedRows} rows to power.meters successfully.") except Exception as err: - print(f"Failed to prepareMeta {err}") + print(f"Failed to prepareMeta ErrMessage:{err}") raise err finally: if conn: @@ -78,7 +78,7 @@ def create_consumer(): print(f"Create consumer successfully, host: {host}:{port}, groupId: {groupId}, clientId: {clientId}"); return consumer; except Exception as err: - print(f"Failed to create websocket consumer, host: {host}:{port} ; err:{err}"); + print(f"Failed to create websocket consumer, host: {host}:{port} ; ErrMessage:{err}"); raise err @@ -98,7 +98,7 @@ def seek_offset(consumer): print("assignment seek to beginning successfully"); except Exception as err: - print(f"seek example failed; err:{err}") + print(f"seek example failed; ErrMessage:{err}") raise err # ANCHOR_END: assignment @@ -116,7 +116,7 @@ def subscribe(consumer): print(f"data: {row}") except Exception as err: - print(f"Failed to poll data, err:{err}") + print(f"Failed to poll data, ErrMessage:{err}") raise err @@ -137,7 +137,7 @@ def commit_offset(consumer): print("commit offset manually successfully."); except Exception as err: - print(f"Failed to poll data, err:{err}") + print(f"Failed to poll data, ErrMessage:{err}") raise err @@ -147,9 +147,9 @@ def commit_offset(consumer): def unsubscribe(consumer): try: consumer.unsubscribe() - print("unsubscribe consumer successfully."); + print("consumer unsubscribed successfully."); except Exception as err: - print("Failed to unsubscribe consumer. err:{err}") + print(f"Failed to unsubscribe consumer. ErrMessage:{err}") # ANCHOR_END: unsubscribe @@ -164,7 +164,7 @@ if __name__ == "__main__": commit_offset(consumer) unsubscribe(consumer) except Exception as err: - print(f"Failed to stmt consumer. err:{err}") + print(f"Failed to stmt consumer. ErrorMessage:{err}") finally: if consumer: consumer.close() \ No newline at end of file From 0cd09c02de87a0a088a7762571dd82c352860337 Mon Sep 17 00:00:00 2001 From: t_max <1172915550@qq.com> Date: Mon, 12 Aug 2024 19:48:41 +0800 Subject: [PATCH 31/39] docs: update go and c# example --- docs/examples/csharp/subscribe/Program.cs | 29 ++++----------- docs/examples/csharp/wssubscribe/Program.cs | 32 ++++------------- docs/examples/go/tmq/native/main.go | 39 ++++++--------------- docs/examples/go/tmq/ws/main.go | 39 ++++++--------------- 4 files changed, 33 insertions(+), 106 deletions(-) diff --git a/docs/examples/csharp/subscribe/Program.cs b/docs/examples/csharp/subscribe/Program.cs index a83a9f07a8..fa61435b03 100644 --- a/docs/examples/csharp/subscribe/Program.cs +++ b/docs/examples/csharp/subscribe/Program.cs @@ -152,41 +152,24 @@ namespace TMQExample { // get assignment var assignment = consumer.Assignment; - Console.WriteLine($"now assignment: ${assignment}"); + Console.WriteLine($"now assignment: {assignment}"); // seek to the beginning foreach (var topicPartition in assignment) { consumer.Seek(new TopicPartitionOffset(topicPartition.Topic, topicPartition.Partition, 0)); } - Console.WriteLine("assignment seek to beginning successfully"); - // poll data again - for (int i = 0; i < 50; i++) - { - // consume message with using block to ensure the result is disposed - using (var cr = consumer.Consume(100)) - { - if (cr == null) continue; - foreach (var message in cr.Message) - { - // handle message - Console.WriteLine( - $"second data polled: {{{((DateTime)message.Value["ts"]).ToString("yyyy-MM-dd HH:mm:ss.fff")}, " + - $"{message.Value["current"]}, {message.Value["voltage"]}, {message.Value["phase"]}}}"); - } - break; - } - } + Console.WriteLine("Assignment seek to beginning successfully"); } catch (TDengineError e) { // handle TDengine error - Console.WriteLine("seek example failed; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); + Console.WriteLine("Seek example failed; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); throw; } catch (Exception e) { // handle other exceptions - Console.WriteLine("seek example failed; ErrMessage: " + e.Message); + Console.WriteLine("Seek example failed; ErrMessage: " + e.Message); throw; } // ANCHOR_END: seek @@ -208,7 +191,7 @@ namespace TMQExample { cr.TopicPartitionOffset, }); - Console.WriteLine("commit offset manually successfully."); + Console.WriteLine("Commit offset manually successfully."); } } catch (TDengineError e) @@ -251,7 +234,7 @@ namespace TMQExample { // close consumer consumer.Close(); - Console.WriteLine("consumer closed successfully."); + Console.WriteLine("Consumer closed successfully."); } // ANCHOR_END: close } diff --git a/docs/examples/csharp/wssubscribe/Program.cs b/docs/examples/csharp/wssubscribe/Program.cs index cfde01920c..9974c87906 100644 --- a/docs/examples/csharp/wssubscribe/Program.cs +++ b/docs/examples/csharp/wssubscribe/Program.cs @@ -118,7 +118,7 @@ namespace TMQExample { // subscribe consumer.Subscribe(new List() { "topic_meters" }); - Console.WriteLine("subscribe topics successfully"); + Console.WriteLine("Subscribe topics successfully"); for (int i = 0; i < 50; i++) { // consume message with using block to ensure the result is disposed @@ -157,42 +157,24 @@ namespace TMQExample { // get assignment var assignment = consumer.Assignment; - Console.WriteLine($"now assignment: ${assignment}"); + Console.WriteLine($"Now assignment: {assignment}"); // seek to the beginning foreach (var topicPartition in assignment) { consumer.Seek(new TopicPartitionOffset(topicPartition.Topic, topicPartition.Partition, 0)); } - Console.WriteLine("assignment seek to beginning successfully"); - // poll data again - for (int i = 0; i < 50; i++) - { - // consume message with using block to ensure the result is disposed - using (var cr = consumer.Consume(100)) - { - if (cr == null) continue; - foreach (var message in cr.Message) - { - // handle message - Console.WriteLine( - $"second data polled: {{{((DateTime)message.Value["ts"]).ToString("yyyy-MM-dd HH:mm:ss.fff")}, " + - $"{message.Value["current"]}, {message.Value["voltage"]}, {message.Value["phase"]}}}"); - } - - break; - } - } + Console.WriteLine("Assignment seek to beginning successfully"); } catch (TDengineError e) { // handle TDengine error - Console.WriteLine("seek example failed; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); + Console.WriteLine("Seek example failed; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); throw; } catch (Exception e) { // handle other exceptions - Console.WriteLine("seek example failed; ErrMessage: " + e.Message); + Console.WriteLine("Seek example failed; ErrMessage: " + e.Message); throw; } // ANCHOR_END: seek @@ -214,7 +196,7 @@ namespace TMQExample { cr.TopicPartitionOffset, }); - Console.WriteLine("commit offset manually successfully."); + Console.WriteLine("Commit offset manually successfully."); } } catch (TDengineError e) @@ -257,7 +239,7 @@ namespace TMQExample { // close consumer consumer.Close(); - Console.WriteLine("consumer closed successfully."); + Console.WriteLine("Consumer closed successfully."); } // ANCHOR_END: close } diff --git a/docs/examples/go/tmq/native/main.go b/docs/examples/go/tmq/native/main.go index b5f7c8c103..4bf2fb4a60 100644 --- a/docs/examples/go/tmq/native/main.go +++ b/docs/examples/go/tmq/native/main.go @@ -50,7 +50,7 @@ func main() { if err != nil { log.Fatalln("Failed to subscribe, host : " + host + "; ErrMessage: " + err.Error()) } - log.Println("subscribe topics successfully") + log.Println("Subscribe topics successfully") for i := 0; i < 50; i++ { ev := consumer.Poll(100) if ev != nil { @@ -64,7 +64,7 @@ func main() { if err != nil { log.Fatalln("Failed to commit offset, host : " + host + "; ErrMessage: " + err.Error()) } - log.Println("commit offset manually successfully.") + log.Println("Commit offset manually successfully.") // ANCHOR_END: commit_offset case tmqcommon.Error: fmt.Printf("%% Error: %v: %v\n", e.Code(), e) @@ -77,9 +77,9 @@ func main() { // get assignment partitions, err := consumer.Assignment() if err != nil { - log.Fatal("failed to get assignment, err:", err) + log.Fatal("Failed to get assignment; ErrMessage: " + err.Error()) } - fmt.Println("now assignment:", partitions) + fmt.Println("Now assignment:", partitions) for i := 0; i < len(partitions); i++ { // seek to the beginning err = consumer.Seek(tmqcommon.TopicPartition{ @@ -88,29 +88,10 @@ func main() { Offset: 0, }, 0) if err != nil { - log.Fatalln("seek example failed; ErrMessage: " + err.Error()) - } - } - fmt.Println("assignment seek to beginning successfully") - // poll data again - gotData := false - for i := 0; i < 50; i++ { - if gotData { - break - } - ev := consumer.Poll(100) - if ev != nil { - switch e := ev.(type) { - case *tmqcommon.DataMessage: - // process your data here - fmt.Printf("second data polled:%v\n", e) - gotData = true - case tmqcommon.Error: - fmt.Printf("%% Error: %v: %v\n", e.Code(), e) - log.Fatal("failed to get message, err:", e) - } + log.Fatalln("Seek example failed; ErrMessage: " + err.Error()) } } + fmt.Println("Assignment seek to beginning successfully") // ANCHOR_END: seek // ANCHOR: close // unsubscribe @@ -130,22 +111,22 @@ func main() { func initEnv(conn *sql.DB) { _, err := conn.Exec("CREATE DATABASE IF NOT EXISTS power") if err != nil { - log.Fatal("failed to create database, err:", err) + log.Fatal("Failed to create database. ErrMessage: " + err.Error()) } _, err = conn.Exec("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))") if err != nil { - log.Fatal("failed to create stable, err:", err) + log.Fatal("Failed to create stable. ErrMessage: " + err.Error()) } _, err = conn.Exec("CREATE TOPIC IF NOT EXISTS topic_meters AS SELECT ts, current, voltage, phase, groupid, location FROM power.meters") if err != nil { - log.Fatal("failed to create topic, err:", err) + log.Fatal("Failed to create topic. ErrMessage: " + err.Error()) } go func() { for i := 0; i < 10; i++ { time.Sleep(time.Second) _, err = conn.Exec("INSERT INTO power.d1001 USING power.meters TAGS (2, 'California.SanFrancisco') VALUES (NOW , 10.2, 219, 0.32)") if err != nil { - log.Fatal("failed to insert data, err:", err) + log.Fatal("Failed to insert data. ErrMessage: " + err.Error()) } } done <- struct{}{} diff --git a/docs/examples/go/tmq/ws/main.go b/docs/examples/go/tmq/ws/main.go index 073efd1bba..c6be0326b5 100644 --- a/docs/examples/go/tmq/ws/main.go +++ b/docs/examples/go/tmq/ws/main.go @@ -55,7 +55,7 @@ func main() { if err != nil { log.Fatalln("Failed to subscribe, host : " + wsUrl + "; ErrMessage: " + err.Error()) } - log.Println("subscribe topics successfully") + log.Println("Subscribe topics successfully") for i := 0; i < 50; i++ { ev := consumer.Poll(100) if ev != nil { @@ -69,7 +69,7 @@ func main() { if err != nil { log.Fatalln("Failed to commit offset, host : " + wsUrl + "; ErrMessage: " + err.Error()) } - log.Println("commit offset manually successfully.") + log.Println("Commit offset manually successfully.") // ANCHOR_END: commit_offset case tmqcommon.Error: fmt.Printf("%% Error: %v: %v\n", e.Code(), e) @@ -82,9 +82,9 @@ func main() { // get assignment partitions, err := consumer.Assignment() if err != nil { - log.Fatal("failed to get assignment, err:", err) + log.Fatal("Failed to get assignment; ErrMessage: " + err.Error()) } - fmt.Println("now assignment:", partitions) + fmt.Println("Now assignment:", partitions) for i := 0; i < len(partitions); i++ { // seek to the beginning err = consumer.Seek(tmqcommon.TopicPartition{ @@ -93,29 +93,10 @@ func main() { Offset: 0, }, 0) if err != nil { - log.Fatalln("seek example failed; ErrMessage: " + err.Error()) - } - } - fmt.Println("assignment seek to beginning successfully") - // poll data again - gotData := false - for i := 0; i < 50; i++ { - if gotData { - break - } - ev := consumer.Poll(100) - if ev != nil { - switch e := ev.(type) { - case *tmqcommon.DataMessage: - // process your data here - fmt.Printf("second data polled:%v\n", e) - gotData = true - case tmqcommon.Error: - fmt.Printf("%% Error: %v: %v\n", e.Code(), e) - log.Fatal("failed to get message, err:", e) - } + log.Fatalln("Seek example failed; ErrMessage: " + err.Error()) } } + fmt.Println("Assignment seek to beginning successfully") // ANCHOR_END: seek // ANCHOR: close // unsubscribe @@ -135,22 +116,22 @@ func main() { func initEnv(conn *sql.DB) { _, err := conn.Exec("CREATE DATABASE IF NOT EXISTS power") if err != nil { - log.Fatal("failed to create database, err:", err) + log.Fatal("Failed to create database. ErrMessage: " + err.Error()) } _, err = conn.Exec("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))") if err != nil { - log.Fatal("failed to create stable, err:", err) + log.Fatal("Failed to create stable. ErrMessage: " + err.Error()) } _, err = conn.Exec("CREATE TOPIC IF NOT EXISTS topic_meters AS SELECT ts, current, voltage, phase, groupid, location FROM power.meters") if err != nil { - log.Fatal("failed to create topic, err:", err) + log.Fatal("Failed to create topic. ErrMessage: " + err.Error()) } go func() { for i := 0; i < 10; i++ { time.Sleep(time.Second) _, err = conn.Exec("INSERT INTO power.d1001 USING power.meters TAGS (2, 'California.SanFrancisco') VALUES (NOW , 10.2, 219, 0.32)") if err != nil { - log.Fatal("failed to insert data, err:", err) + log.Fatal("Failed to insert data. ErrMessage: " + err.Error()) } } done <- struct{}{} From eb4a7213fd88074c1162954178b99a1fd03143f7 Mon Sep 17 00:00:00 2001 From: menshibin Date: Mon, 12 Aug 2024 20:21:16 +0800 Subject: [PATCH 32/39] modify alert sql --- docs/examples/python/tmq_native.py | 2 +- docs/examples/python/tmq_websocket_example.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/examples/python/tmq_native.py b/docs/examples/python/tmq_native.py index 3e7fd1c53a..4d507d0190 100644 --- a/docs/examples/python/tmq_native.py +++ b/docs/examples/python/tmq_native.py @@ -70,7 +70,7 @@ def create_consumer(): print(f"Create consumer successfully, host: {host}:{port}, groupId: {groupId}, clientId: {clientId}"); return consumer except Exception as err: - print(f"Failed to create websocket consumer, host: {host}:{port} ; ErrMessage:{err}"); + print(f"Failed to create native consumer, host: {host}:{port} ; ErrMessage:{err}"); raise err # ANCHOR_END: create_consumer diff --git a/docs/examples/python/tmq_websocket_example.py b/docs/examples/python/tmq_websocket_example.py index 2fe2ae57d5..e22f2d595b 100644 --- a/docs/examples/python/tmq_websocket_example.py +++ b/docs/examples/python/tmq_websocket_example.py @@ -48,7 +48,7 @@ def prepareMeta(): VALUES (NOW + 1a, 10.30000, 218, 0.25000) """ affectedRows = conn.execute(sql) - print(f"inserted into {affectedRows} rows to power.meters successfully.") + print(f"Inserted into {affectedRows} rows to power.meters successfully.") except Exception as err: print(f"Failed to prepareMeta ErrMessage:{err}") @@ -95,10 +95,10 @@ def seek_offset(consumer): print( f"vg_id: {assign.vg_id()}, offset: {assign.offset()}, begin: {assign.begin()}, end: {assign.end()}") consumer.seek(topic, assign.vg_id(), assign.begin()) - print("assignment seek to beginning successfully"); + print("Assignment seek to beginning successfully"); except Exception as err: - print(f"seek example failed; ErrMessage:{err}") + print(f"Seek example failed; ErrMessage:{err}") raise err # ANCHOR_END: assignment @@ -107,7 +107,7 @@ def seek_offset(consumer): def subscribe(consumer): try: consumer.subscribe([topic]) - print("subscribe topics successfully") + print("Subscribe topics successfully") for i in range(50): records = consumer.poll(timeout=1.0) if records: @@ -134,7 +134,7 @@ def commit_offset(consumer): # after processing the data, commit the offset manually consumer.commit(records) - print("commit offset manually successfully."); + print("Commit offset manually successfully."); except Exception as err: print(f"Failed to poll data, ErrMessage:{err}") @@ -147,7 +147,7 @@ def commit_offset(consumer): def unsubscribe(consumer): try: consumer.unsubscribe() - print("consumer unsubscribed successfully."); + print("Consumer unsubscribed successfully."); except Exception as err: print(f"Failed to unsubscribe consumer. ErrMessage:{err}") From 29df00a89d584553cd0e2e74a8ecb9493e605719 Mon Sep 17 00:00:00 2001 From: menshibin Date: Mon, 12 Aug 2024 20:28:31 +0800 Subject: [PATCH 33/39] modify alert sql --- .../node/websocketexample/tmq_example.js | 4 ++-- .../node/websocketexample/tmq_seek_example.js | 13 ++----------- docs/examples/python/tmq_native.py | 19 +++++++++---------- 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/docs/examples/node/websocketexample/tmq_example.js b/docs/examples/node/websocketexample/tmq_example.js index 0c9e00446b..da0bffc0aa 100644 --- a/docs/examples/node/websocketexample/tmq_example.js +++ b/docs/examples/node/websocketexample/tmq_example.js @@ -63,7 +63,7 @@ async function subscribe(consumer) { console.log(`data: ${key} ${value}`); } consumer.commit(); - console.log("commit offset manually successfully."); + console.log("Commit offset manually successfully."); } } catch (err) { console.error("Failed to poll data; err.code, ErrCode:" + err.code + "; ErrMessage: " + err.message); @@ -80,7 +80,7 @@ async function test() { let consumer = await createConsumer() await subscribe(consumer) await consumer.unsubscribe(); - console.log("unsubscribe consumer successfully."); + console.log("Consumer unsubscribed successfully."); } catch (err) { console.error("Failed to unsubscribe consume, ErrCode:" + err.code + "; ErrMessage: " + err.message); diff --git a/docs/examples/node/websocketexample/tmq_seek_example.js b/docs/examples/node/websocketexample/tmq_seek_example.js index b5477cfeaa..ef34cb2d16 100644 --- a/docs/examples/node/websocketexample/tmq_seek_example.js +++ b/docs/examples/node/websocketexample/tmq_seek_example.js @@ -79,20 +79,11 @@ async function test() { } let assignment = await consumer.assignment(); - for (let i in assignment) { - console.log("seek before:", assignment[i]); - } - await consumer.seekToBeginning(assignment); - console.log("assignment seek to beginning successfully"); - assignment = await consumer.assignment(); - for (let i in assignment) { - console.log("seek after:", assignment[i]); - } - await consumer.unsubscribe(); + console.log("Assignment seek to beginning successfully"); } catch (err) { - console.error("seek example failed, ErrCode:" + err.code + "; ErrMessage: " + err.message); + console.error("Seek example failed, ErrCode:" + err.code + "; ErrMessage: " + err.message); } finally { if (consumer) { diff --git a/docs/examples/python/tmq_native.py b/docs/examples/python/tmq_native.py index 4d507d0190..32a21d2780 100644 --- a/docs/examples/python/tmq_native.py +++ b/docs/examples/python/tmq_native.py @@ -37,9 +37,9 @@ def prepareMeta(): VALUES (NOW + 1a, 10.30000, 218, 0.25000) """ affectedRows = conn.execute(sql) - print(f"inserted into {affectedRows} rows to power.meters successfully.") + print(f"Inserted into {affectedRows} rows to power.meters successfully.") except Exception as err: - print(f"prepare meta ErrMessage:{err}") + print(f"Prepare insert data error, ErrMessage:{err}") raise err finally: if conn: @@ -80,13 +80,13 @@ def subscribe(consumer): try: # subscribe to the topics consumer.subscribe(["topic_meters"]) - print("subscribe topics successfully") + print("Subscribe topics successfully") for i in range(50): records = consumer.poll(1) if records: err = records.error() if err is not None: - print(f"poll data error, {err}") + print(f"Poll data error, {err}") raise err val = records.value() @@ -110,7 +110,7 @@ def commit_offset(consumer): if records: err = records.error() if err is not None: - print(f"poll data error, {err}") + print(f"Poll data error, {err}") raise err val = records.value() @@ -120,7 +120,7 @@ def commit_offset(consumer): # after processing the data, commit the offset manually consumer.commit(records) - print("commit offset manually successfully."); + print("Commit offset manually successfully."); except Exception as err: print(f"Failed to poll data, ErrMessage:{err}") @@ -134,12 +134,11 @@ def seek_offset(consumer): assignments = consumer.assignment() if assignments: for partition in assignments: - print(f"first data polled: {partition.offset}") partition.offset = 0 consumer.seek(partition) - print(f"assignment seek to beginning successfully"); + print(f"Assignment seek to beginning successfully"); except Exception as err: - print(f"seek example failed; ErrMessage:{err}") + print(f"Seek example failed; ErrMessage:{err}") raise err # ANCHOR_END: assignment @@ -148,7 +147,7 @@ def seek_offset(consumer): def unsubscribe(consumer): try: consumer.unsubscribe() - print("unsubscribe consumer successfully."); + print("Consumer unsubscribed successfully."); except Exception as err: print(f"Failed to unsubscribe consumer. ErrMessage:{err}") From 34787a8211a220e50016a67101a59781635d32cf Mon Sep 17 00:00:00 2001 From: Yaming Pei Date: Mon, 12 Aug 2024 20:54:11 +0800 Subject: [PATCH 34/39] mod c tmq sample code --- docs/examples/c/tmq_demo.c | 67 +++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 16 deletions(-) diff --git a/docs/examples/c/tmq_demo.c b/docs/examples/c/tmq_demo.c index d3285cb2d0..e7a2425c7d 100644 --- a/docs/examples/c/tmq_demo.c +++ b/docs/examples/c/tmq_demo.c @@ -28,6 +28,18 @@ volatile int thread_stop = 0; static int running = 1; const char* topic_name = "topic_meters"; +typedef struct { + const char* enable_auto_commit; + const char* auto_commit_interval_ms; + const char* group_id; + const char* client_id; + const char* td_connect_host; + const char* td_connect_port; + const char* td_connect_user; + const char* td_connect_pass; + const char* auto_offset_reset; +} ConsumerConfig; + void* prepare_data(void* arg) { const char *host = "localhost"; const char *user = "root"; @@ -205,7 +217,7 @@ void tmq_commit_cb_print(tmq_t* tmq, int32_t code, void* param) { } // ANCHOR: create_consumer_1 -tmq_t* build_consumer() { +tmq_t* build_consumer(const ConsumerConfig* config) { tmq_conf_res_t code; tmq_t* tmq = NULL; @@ -213,37 +225,47 @@ tmq_t* build_consumer() { tmq_conf_t* conf = tmq_conf_new(); // set the configuration parameters - code = tmq_conf_set(conf, "enable.auto.commit", "true"); + code = tmq_conf_set(conf, "enable.auto.commit", config->enable_auto_commit); if (TMQ_CONF_OK != code) { tmq_conf_destroy(conf); return NULL; } - code = tmq_conf_set(conf, "auto.commit.interval.ms", "1000"); + code = tmq_conf_set(conf, "auto.commit.interval.ms", config->auto_commit_interval_ms); if (TMQ_CONF_OK != code) { tmq_conf_destroy(conf); return NULL; } - code = tmq_conf_set(conf, "group.id", "group1"); + code = tmq_conf_set(conf, "group.id", config->group_id); if (TMQ_CONF_OK != code) { tmq_conf_destroy(conf); return NULL; } - code = tmq_conf_set(conf, "client.id", "client1"); + code = tmq_conf_set(conf, "client.id", config->client_id); if (TMQ_CONF_OK != code) { tmq_conf_destroy(conf); return NULL; } - code = tmq_conf_set(conf, "td.connect.user", "root"); + code = tmq_conf_set(conf, "td.connect.ip", config->td_connect_host); if (TMQ_CONF_OK != code) { tmq_conf_destroy(conf); return NULL; } - code = tmq_conf_set(conf, "td.connect.pass", "taosdata"); + code = tmq_conf_set(conf, "td.connect.port", config->td_connect_port); if (TMQ_CONF_OK != code) { tmq_conf_destroy(conf); return NULL; } - code = tmq_conf_set(conf, "auto.offset.reset", "latest"); + code = tmq_conf_set(conf, "td.connect.user", config->td_connect_user); + if (TMQ_CONF_OK != code) { + tmq_conf_destroy(conf); + return NULL; + } + code = tmq_conf_set(conf, "td.connect.pass", config->td_connect_pass); + if (TMQ_CONF_OK != code) { + tmq_conf_destroy(conf); + return NULL; + } + code = tmq_conf_set(conf, "auto.offset.reset", config->auto_offset_reset); if (TMQ_CONF_OK != code) { tmq_conf_destroy(conf); return NULL; @@ -392,16 +414,29 @@ int main(int argc, char* argv[]) { if (pthread_create(&thread_id, NULL, &prepare_data, NULL)) { fprintf(stderr, "Failed to create thread.\n"); - return 1; - } - - // ANCHOR: create_consumer_2 - tmq_t* tmq = build_consumer(); - if (NULL == tmq) { - fprintf(stderr, "Failed to create consumer.\n"); return -1; } - fprintf(stdout, "Create consumer successfully.\n"); + + ConsumerConfig config = { + .enable_auto_commit = "true", + .auto_commit_interval_ms = "1000", + .group_id = "group1", + .client_id = "client1", + .td_connect_host = "localhost", + .td_connect_port = "6030", + .td_connect_user = "root", + .td_connect_pass = "taosdata", + .auto_offset_reset = "latest" + }; + + // ANCHOR: create_consumer_2 + tmq_t* tmq = build_consumer(&config); + if (NULL == tmq) { + fprintf(stderr, "Failed to create native consumer, host: %s, groupId: %s, , clientId: %s.\n", config.td_connect_host, config.group_id, config.client_id); + return -1; + } else { + fprintf(stdout, "Create consumer successfully, host: %s, groupId: %s, , clientId: %s.\n", config.td_connect_host, config.group_id, config.client_id); + } // ANCHOR_END: create_consumer_2 From c6e7d69bb4ee540063fa9bb0b1a35df5fb6dcd7b Mon Sep 17 00:00:00 2001 From: sheyanjie-qq <249478495@qq.com> Date: Mon, 12 Aug 2024 20:56:20 +0800 Subject: [PATCH 35/39] update log --- docs/examples/c/create_db_demo.c | 92 ++++++++++--------- .../node/websocketexample/tmq_example.js | 2 +- .../node/websocketexample/tmq_seek_example.js | 14 +-- docs/examples/python/reqid_native.py | 4 +- docs/examples/python/reqid_rest.py | 2 +- docs/examples/python/reqid_ws.py | 6 +- .../taosdata/example/ConsumerLoopFull.java | 4 +- .../taosdata/example/WsConsumerLoopFull.java | 4 +- 8 files changed, 65 insertions(+), 63 deletions(-) diff --git a/docs/examples/c/create_db_demo.c b/docs/examples/c/create_db_demo.c index ff1f4e62fd..45d4a17c5c 100644 --- a/docs/examples/c/create_db_demo.c +++ b/docs/examples/c/create_db_demo.c @@ -22,55 +22,57 @@ #include #include "taos.h" - static int DemoCreateDB() { -// ANCHOR: create_db_and_table -const char *host = "localhost"; -const char *user = "root"; -const char *password = "taosdata"; -uint16_t port = 6030; -int code = 0; + // ANCHOR: create_db_and_table + const char *host = "localhost"; + const char *user = "root"; + const char *password = "taosdata"; + uint16_t port = 6030; + int code = 0; -// connect -TAOS *taos = taos_connect(host, user, password, NULL, port); -if (taos == NULL) { - printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); - taos_cleanup(); - return -1; -} + // connect + TAOS *taos = taos_connect(host, user, password, NULL, port); + if (taos == NULL) { + printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), + taos_errstr(NULL)); + taos_cleanup(); + return -1; + } -// create database -TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power"); -code = taos_errno(result); -if (code != 0) { - printf("Failed to create database power, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, code, taos_errstr(result)); + // create database + TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power"); + code = taos_errno(result); + if (code != 0) { + printf("Failed to create database power, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, code, + taos_errstr(result)); + taos_close(taos); + taos_cleanup(); + return -1; + } + taos_free_result(result); + printf("Create database power successfully.\n"); + + // create table + const char *sql = + "CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId " + "INT, location BINARY(24))"; + result = taos_query(taos, sql); + code = taos_errno(result); + if (code != 0) { + printf("Failed to create stable power.meters, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, + taos_errstr(result)); + taos_close(taos); + taos_cleanup(); + return -1; + } + taos_free_result(result); + printf("Create stable power.meters successfully.\n"); + + // close & clean taos_close(taos); taos_cleanup(); - return -1; -} -taos_free_result(result); -printf("Create database power successfully.\n"); - -// create table -const char* sql = "CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))"; -result = taos_query(taos, sql); -code = taos_errno(result); -if (code != 0) { - printf("Failed to create stable power.meters, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result)); - taos_close(taos); - taos_cleanup(); - return -1; -} -taos_free_result(result); -printf("Create stable meters successfully.\n"); - -// close & clean -taos_close(taos); -taos_cleanup(); -return 0; -// ANCHOR_END: create_db_and_table + return 0; + // ANCHOR_END: create_db_and_table } -int main(int argc, char *argv[]) { - return DemoCreateDB(); -} +int main(int argc, char *argv[]) { return DemoCreateDB(); } diff --git a/docs/examples/node/websocketexample/tmq_example.js b/docs/examples/node/websocketexample/tmq_example.js index da0bffc0aa..ff676fa972 100644 --- a/docs/examples/node/websocketexample/tmq_example.js +++ b/docs/examples/node/websocketexample/tmq_example.js @@ -66,7 +66,7 @@ async function subscribe(consumer) { console.log("Commit offset manually successfully."); } } catch (err) { - console.error("Failed to poll data; err.code, ErrCode:" + err.code + "; ErrMessage: " + err.message); + console.error("Failed to poll data; ErrCode:" + err.code + "; ErrMessage: " + err.message); throw err; } // ANCHOR_END: commit diff --git a/docs/examples/node/websocketexample/tmq_seek_example.js b/docs/examples/node/websocketexample/tmq_seek_example.js index ef34cb2d16..1ae729534c 100644 --- a/docs/examples/node/websocketexample/tmq_seek_example.js +++ b/docs/examples/node/websocketexample/tmq_seek_example.js @@ -18,11 +18,11 @@ async function createConsumer() { ]); try { return await taos.tmqConnect(configMap); - }catch (err) { + } catch (err) { console.log(err); throw err; } - + } // ANCHOR_END: create_consumer @@ -33,7 +33,7 @@ async function prepare() { conf.setDb('power'); const createDB = `CREATE DATABASE IF NOT EXISTS POWER ${db} KEEP 3650 DURATION 10 BUFFER 16 WAL_LEVEL 1;`; const createStable = `CREATE STABLE IF NOT EXISTS ${db}.${stable} (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int);`; - + let wsSql = await taos.sqlConnect(conf); await wsSql.exec(createDB); await wsSql.exec(createStable); @@ -57,9 +57,9 @@ async function subscribe(consumer) { for (let [key, value] of res) { console.log(key, value); } - } - }catch (err) { - console.error("Failed to poll data; err.code, ErrCode:" + err.code + "; ErrMessage: " + err.message); + } + } catch (err) { + console.error("Failed to poll data; ErrCode:" + err.code + "; ErrMessage: " + err.message); throw err; } @@ -76,7 +76,7 @@ async function test() { let res = new Map(); while (res.size == 0) { res = await consumer.poll(100); - } + } let assignment = await consumer.assignment(); await consumer.seekToBeginning(assignment); diff --git a/docs/examples/python/reqid_native.py b/docs/examples/python/reqid_native.py index 73fc05af53..8f5bb3538c 100644 --- a/docs/examples/python/reqid_native.py +++ b/docs/examples/python/reqid_native.py @@ -1,7 +1,7 @@ import taos conn = None -reqId = 1 +reqId = 3 host="localhost" port=6030 try: @@ -21,4 +21,4 @@ except Exception as err: finally: if conn: - conn.close() \ No newline at end of file + conn.close() diff --git a/docs/examples/python/reqid_rest.py b/docs/examples/python/reqid_rest.py index a4a8999f3a..76ba735234 100644 --- a/docs/examples/python/reqid_rest.py +++ b/docs/examples/python/reqid_rest.py @@ -2,7 +2,7 @@ import taosrest client = None url="http://localhost:6041" -reqId = 1 +reqId = 3 try: client = taosrest.RestClient(url=url, user="root", diff --git a/docs/examples/python/reqid_ws.py b/docs/examples/python/reqid_ws.py index a97410c2e4..691dde710f 100644 --- a/docs/examples/python/reqid_ws.py +++ b/docs/examples/python/reqid_ws.py @@ -1,7 +1,7 @@ import taosws conn = None -reqId = 1 +reqId = 3 host="localhost" port=6041 try: @@ -12,7 +12,7 @@ try: port=port, ) - result = conn.query_with_req_id("SELECT ts, current, location FROM power.meters limit 100", req_id=1) + result = conn.query_with_req_id("SELECT ts, current, location FROM power.meters limit 100", req_id=3) # Get data from result as list of tuple for row in result: print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}") @@ -21,4 +21,4 @@ except Exception as err: print(f"Failed to execute sql with reqId:{reqId}, db addr:{host}:{port} ; ErrMessage:{err}") finally: if conn: - conn.close() \ No newline at end of file + conn.close() diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java index 3c0798d198..34629fb8d2 100644 --- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java +++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java @@ -42,10 +42,10 @@ public class ConsumerLoopFull { return consumer; } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info - System.out.println("Failed to create native consumer, host : " + config.getProperty("bootstrap.servers") + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); + System.out.println("Failed to create native consumer, host: " + config.getProperty("bootstrap.servers") + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); throw new SQLException("Failed to create consumer", ex); } catch (Exception ex) { - System.out.println("Failed to create native consumer, host : " + config.getProperty("bootstrap.servers") + System.out.println("Failed to create native consumer, host: " + config.getProperty("bootstrap.servers") + "; ErrMessage: " + ex.getMessage()); throw new SQLException("Failed to create consumer", ex); } diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java index d7207ffe71..241ab2df76 100644 --- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java +++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java @@ -40,10 +40,10 @@ public class WsConsumerLoopFull { return consumer; } catch (SQLException ex) { // handle any errors, please refer to the JDBC specifications for detailed exceptions info - System.out.println("Failed to create websocket consumer, host : " + config.getProperty("bootstrap.servers") + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); + System.out.println("Failed to create websocket consumer, host: " + config.getProperty("bootstrap.servers") + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); throw new SQLException("Failed to create consumer", ex); } catch (Exception ex) { - System.out.println("Failed to create websocket consumer, host : " + config.getProperty("bootstrap.servers") + System.out.println("Failed to create websocket consumer, host: " + config.getProperty("bootstrap.servers") + "; ErrMessage: " + ex.getMessage()); throw new SQLException("Failed to create consumer", ex); } From 69bbc4185d361a13ae79d2c9f829d6d4f2b51204 Mon Sep 17 00:00:00 2001 From: menshibin Date: Mon, 12 Aug 2024 21:09:39 +0800 Subject: [PATCH 36/39] modify alert sql --- docs/examples/node/websocketexample/tmq_example.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/examples/node/websocketexample/tmq_example.js b/docs/examples/node/websocketexample/tmq_example.js index da0bffc0aa..ff676fa972 100644 --- a/docs/examples/node/websocketexample/tmq_example.js +++ b/docs/examples/node/websocketexample/tmq_example.js @@ -66,7 +66,7 @@ async function subscribe(consumer) { console.log("Commit offset manually successfully."); } } catch (err) { - console.error("Failed to poll data; err.code, ErrCode:" + err.code + "; ErrMessage: " + err.message); + console.error("Failed to poll data; ErrCode:" + err.code + "; ErrMessage: " + err.message); throw err; } // ANCHOR_END: commit From 88a049c02e67c5f03b756455bac22f5376b222df Mon Sep 17 00:00:00 2001 From: Yaming Pei Date: Mon, 12 Aug 2024 21:12:03 +0800 Subject: [PATCH 37/39] mod c tmq sample code --- docs/examples/c/insert_data_demo.c | 4 ++-- docs/examples/c/query_data_demo.c | 4 ++-- docs/examples/c/sml_insert_demo.c | 12 ++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/examples/c/insert_data_demo.c b/docs/examples/c/insert_data_demo.c index 8796556d03..3d655edca3 100644 --- a/docs/examples/c/insert_data_demo.c +++ b/docs/examples/c/insert_data_demo.c @@ -33,7 +33,7 @@ int code = 0; // connect TAOS *taos = taos_connect(host, user, password, NULL, port); if (taos == NULL) { - printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } @@ -51,7 +51,7 @@ const char* sql = "INSERT INTO " TAOS_RES *result = taos_query(taos, sql); code = taos_errno(result); if (code != 0) { - printf("Failed to insert data to power.meters, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", host, port, code, taos_errstr(result)); + printf("Failed to insert data to power.meters, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; diff --git a/docs/examples/c/query_data_demo.c b/docs/examples/c/query_data_demo.c index 77af4c9a8d..e58c467ccf 100644 --- a/docs/examples/c/query_data_demo.c +++ b/docs/examples/c/query_data_demo.c @@ -34,7 +34,7 @@ int code = 0; // connect TAOS *taos = taos_connect(host, user, password, NULL, port); if (taos == NULL) { - printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } @@ -46,7 +46,7 @@ const char* sql = "SELECT ts, current, location FROM power.meters limit 100"; TAOS_RES *result = taos_query(taos, sql); code = taos_errno(result); if (code != 0) { - printf("Failed to query data from power.meters, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", host, port, code, taos_errstr(result)); + printf("Failed to query data from power.meters, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; diff --git a/docs/examples/c/sml_insert_demo.c b/docs/examples/c/sml_insert_demo.c index 65636d89b8..807fd10501 100644 --- a/docs/examples/c/sml_insert_demo.c +++ b/docs/examples/c/sml_insert_demo.c @@ -33,7 +33,7 @@ int code = 0; // connect TAOS *taos = taos_connect(host, user, password, NULL, port); if (taos == NULL) { - printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } @@ -42,7 +42,7 @@ if (taos == NULL) { TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power"); code = taos_errno(result); if (code != 0) { - printf("Failed to create database power, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, code, taos_errstr(result)); + printf("Failed to create database power, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; @@ -54,7 +54,7 @@ printf("Create database power successfully.\n"); result = taos_query(taos, "USE power"); code = taos_errno(result); if (code != 0) { - printf("Failed to execute use power, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", host, port, code, taos_errstr(result)); + printf("Failed to execute use power, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; @@ -71,7 +71,7 @@ char *lines[] = {line_demo}; result = taos_schemaless_insert(taos, lines, 1, TSDB_SML_LINE_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS); code = taos_errno(result); if (code != 0) { - printf("Failed to insert schemaless line data, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", host, port, code, taos_errstr(result)); + printf("Failed to insert schemaless line data, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; @@ -86,7 +86,7 @@ char *telnets[] = {telnet_demo}; result = taos_schemaless_insert(taos, telnets, 1, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS); code = taos_errno(result); if (code != 0) { - printf("Failed to insert schemaless telnet data, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", host, port, code, taos_errstr(result)); + printf("Failed to insert schemaless telnet data, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; @@ -111,7 +111,7 @@ result = taos_schemaless_insert(taos, jsons, 1, TSDB_SML_JSON_PROTOCOL, TSDB_SML code = taos_errno(result); if (code != 0) { free(jsons[0]); - printf("Failed to insert schemaless json data, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", host, port, code, taos_errstr(result)); + printf("Failed to insert schemaless json data, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; From 9ae084ee6eb4d294db3673ecfa05be40f24d21db Mon Sep 17 00:00:00 2001 From: menshibin Date: Mon, 12 Aug 2024 22:13:57 +0800 Subject: [PATCH 38/39] modify alert sql --- docs/examples/node/websocketexample/tmq_seek_example.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/examples/node/websocketexample/tmq_seek_example.js b/docs/examples/node/websocketexample/tmq_seek_example.js index 1ae729534c..da22217e14 100644 --- a/docs/examples/node/websocketexample/tmq_seek_example.js +++ b/docs/examples/node/websocketexample/tmq_seek_example.js @@ -55,7 +55,7 @@ async function subscribe(consumer) { for (let i = 0; i < 50; i++) { let res = await consumer.poll(100); for (let [key, value] of res) { - console.log(key, value); + console.log(`data: ${key} ${value}`); } } } catch (err) { From b9165de6b2b930c52e9c0402da4a25b249ba0dde Mon Sep 17 00:00:00 2001 From: menshibin Date: Mon, 12 Aug 2024 22:55:35 +0800 Subject: [PATCH 39/39] modify alert sql --- docs/examples/python/tmq_native.py | 17 +++++++++-------- docs/examples/python/tmq_websocket_example.py | 13 +++++++------ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/docs/examples/python/tmq_native.py b/docs/examples/python/tmq_native.py index 32a21d2780..64e447384c 100644 --- a/docs/examples/python/tmq_native.py +++ b/docs/examples/python/tmq_native.py @@ -142,17 +142,17 @@ def seek_offset(consumer): raise err # ANCHOR_END: assignment - -# ANCHOR: unsubscribe def unsubscribe(consumer): + # ANCHOR: unsubscribe try: consumer.unsubscribe() print("Consumer unsubscribed successfully."); except Exception as err: print(f"Failed to unsubscribe consumer. ErrMessage:{err}") - - -# ANCHOR_END: unsubscribe + finally: + if consumer: + consumer.close() + # ANCHOR_END: unsubscribe if __name__ == "__main__": consumer = None @@ -162,9 +162,10 @@ if __name__ == "__main__": subscribe(consumer) seek_offset(consumer) commit_offset(consumer) - unsubscribe(consumer) + consumer.unsubscribe() + print("Consumer unsubscribed successfully."); except Exception as err: print(f"Failed to stmt consumer. ErrMessage:{err}") finally: - if consumer: - consumer.close() \ No newline at end of file + consumer.unsubscribe() + diff --git a/docs/examples/python/tmq_websocket_example.py b/docs/examples/python/tmq_websocket_example.py index e22f2d595b..aa9cbf6545 100644 --- a/docs/examples/python/tmq_websocket_example.py +++ b/docs/examples/python/tmq_websocket_example.py @@ -143,14 +143,17 @@ def commit_offset(consumer): # ANCHOR_END: commit_offset # -# ANCHOR: unsubscribe + def unsubscribe(consumer): +# ANCHOR: unsubscribe try: consumer.unsubscribe() print("Consumer unsubscribed successfully."); except Exception as err: print(f"Failed to unsubscribe consumer. ErrMessage:{err}") - + finally: + if consumer: + consumer.close() # ANCHOR_END: unsubscribe @@ -161,10 +164,8 @@ if __name__ == "__main__": consumer = create_consumer() subscribe(consumer) seek_offset(consumer) - commit_offset(consumer) - unsubscribe(consumer) + commit_offset(consumer) except Exception as err: print(f"Failed to stmt consumer. ErrorMessage:{err}") finally: - if consumer: - consumer.close() \ No newline at end of file + unsubscribe(consumer); \ No newline at end of file