Merge pull request #26787 from taosdata/end/TD-30987-19

enh: refactor return code
This commit is contained in:
Hongze Cheng 2024-07-25 17:14:41 +08:00 committed by GitHub
commit a3b30192ae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 48 additions and 40 deletions

View File

@ -19,6 +19,7 @@
#include "tconfig.h" #include "tconfig.h"
#include "tglobal.h" #include "tglobal.h"
#include "tjson.h" #include "tjson.h"
#include "tutil.h"
#define LOG_MAX_LINE_SIZE (10024) #define LOG_MAX_LINE_SIZE (10024)
#define LOG_MAX_LINE_BUFFER_SIZE (LOG_MAX_LINE_SIZE + 3) #define LOG_MAX_LINE_BUFFER_SIZE (LOG_MAX_LINE_SIZE + 3)
@ -146,7 +147,7 @@ static int32_t taosStartLog() {
TdThreadAttr threadAttr; TdThreadAttr threadAttr;
taosThreadAttrInit(&threadAttr); taosThreadAttrInit(&threadAttr);
if (taosThreadCreate(&(tsLogObj.logHandle->asyncThread), &threadAttr, taosAsyncOutputLog, tsLogObj.logHandle) != 0) { if (taosThreadCreate(&(tsLogObj.logHandle->asyncThread), &threadAttr, taosAsyncOutputLog, tsLogObj.logHandle) != 0) {
return -1; return terrno;
} }
taosThreadAttrDestroy(&threadAttr); taosThreadAttrDestroy(&threadAttr);
return 0; return 0;
@ -176,13 +177,13 @@ int32_t taosInitSlowLog() {
} }
tsLogObj.slowHandle = taosLogBuffNew(LOG_SLOW_BUF_SIZE); tsLogObj.slowHandle = taosLogBuffNew(LOG_SLOW_BUF_SIZE);
if (tsLogObj.slowHandle == NULL) return -1; if (tsLogObj.slowHandle == NULL) return terrno;
taosUmaskFile(0); taosUmaskFile(0);
tsLogObj.slowHandle->pFile = taosOpenFile(fullName, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND); tsLogObj.slowHandle->pFile = taosOpenFile(fullName, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND);
if (tsLogObj.slowHandle->pFile == NULL) { if (tsLogObj.slowHandle->pFile == NULL) {
printf("\nfailed to open slow log file:%s, reason:%s\n", fullName, strerror(errno)); printf("\nfailed to open slow log file:%s, reason:%s\n", fullName, strerror(errno));
return -1; return TAOS_SYSTEM_ERROR(errno);
} }
return 0; return 0;
@ -209,11 +210,11 @@ int32_t taosInitLog(const char *logName, int32_t maxFiles) {
taosUpdateDaylight(); taosUpdateDaylight();
tsLogObj.logHandle = taosLogBuffNew(LOG_DEFAULT_BUF_SIZE); tsLogObj.logHandle = taosLogBuffNew(LOG_DEFAULT_BUF_SIZE);
if (tsLogObj.logHandle == NULL) return -1; if (tsLogObj.logHandle == NULL) return terrno;
if (taosOpenLogFile(fullName, maxFiles) < 0) return -1; TAOS_CHECK_RETURN(taosOpenLogFile(fullName, maxFiles));
if (taosInitSlowLog() < 0) return -1; TAOS_CHECK_RETURN(taosInitSlowLog());
if (taosStartLog() < 0) return -1; TAOS_CHECK_RETURN(taosStartLog());
return 0; return 0;
} }
@ -484,7 +485,7 @@ static int32_t taosOpenLogFile(char *fn, int32_t maxFileNum) {
if (tsLogObj.logHandle->pFile == NULL) { 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", fileName, strerror(errno));
return -1; return TAOS_SYSTEM_ERROR(errno);
} }
taosLockLogFile(tsLogObj.logHandle->pFile); taosLockLogFile(tsLogObj.logHandle->pFile);
@ -492,7 +493,7 @@ static int32_t taosOpenLogFile(char *fn, int32_t maxFileNum) {
int64_t filesize = 0; int64_t filesize = 0;
if (taosFStatFile(tsLogObj.logHandle->pFile, &filesize, NULL) < 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", fileName, strerror(errno));
return -1; return TAOS_SYSTEM_ERROR(errno);
} }
tsLogObj.lines = (int32_t)(filesize / 60); tsLogObj.lines = (int32_t)(filesize / 60);

View File

@ -516,7 +516,7 @@ static int32_t taosTmrModuleInit(void) {
tmrCtrls = taosMemoryMalloc(sizeof(tmr_ctrl_t) * tsMaxTmrCtrl); tmrCtrls = taosMemoryMalloc(sizeof(tmr_ctrl_t) * tsMaxTmrCtrl);
if (tmrCtrls == NULL) { if (tmrCtrls == NULL) {
tmrError("failed to allocate memory for timer controllers."); tmrError("failed to allocate memory for timer controllers.");
return -1; return terrno;
} }
memset(&timerMap, 0, sizeof(timerMap)); memset(&timerMap, 0, sizeof(timerMap));
@ -535,14 +535,14 @@ static int32_t taosTmrModuleInit(void) {
time_wheel_t* wheel = wheels + i; time_wheel_t* wheel = wheels + i;
if (taosThreadMutexInit(&wheel->mutex, NULL) != 0) { if (taosThreadMutexInit(&wheel->mutex, NULL) != 0) {
tmrError("failed to create the mutex for wheel, reason:%s", strerror(errno)); tmrError("failed to create the mutex for wheel, reason:%s", strerror(errno));
return -1; return terrno;
} }
wheel->nextScanAt = now + wheel->resolution; wheel->nextScanAt = now + wheel->resolution;
wheel->index = 0; wheel->index = 0;
wheel->slots = (tmr_obj_t**)taosMemoryCalloc(wheel->size, sizeof(tmr_obj_t*)); wheel->slots = (tmr_obj_t**)taosMemoryCalloc(wheel->size, sizeof(tmr_obj_t*));
if (wheel->slots == NULL) { if (wheel->slots == NULL) {
tmrError("failed to allocate wheel slots"); tmrError("failed to allocate wheel slots");
return -1; return terrno;
} }
timerMap.size += wheel->size; timerMap.size += wheel->size;
} }
@ -551,7 +551,7 @@ static int32_t taosTmrModuleInit(void) {
timerMap.slots = (timer_list_t*)taosMemoryCalloc(timerMap.size, sizeof(timer_list_t)); timerMap.slots = (timer_list_t*)taosMemoryCalloc(timerMap.size, sizeof(timer_list_t));
if (timerMap.slots == NULL) { if (timerMap.slots == NULL) {
tmrError("failed to allocate hash map"); tmrError("failed to allocate hash map");
return -1; return terrno;
} }
tmrQhandle = taosInitScheduler(10000, taosTmrThreads, "tmr", NULL); tmrQhandle = taosInitScheduler(10000, taosTmrThreads, "tmr", NULL);

View File

@ -107,6 +107,9 @@ char **strsplit(char *z, const char *delim, int32_t *num) {
int32_t size = 4; int32_t size = 4;
char **split = taosMemoryMalloc(POINTER_BYTES * size); char **split = taosMemoryMalloc(POINTER_BYTES * size);
if (split == NULL) {
return NULL;
}
for (char *p = strsep(&z, delim); p != NULL; p = strsep(&z, delim)) { for (char *p = strsep(&z, delim); p != NULL; p = strsep(&z, delim)) {
size_t len = strlen(p); size_t len = strlen(p);
@ -118,7 +121,10 @@ char **strsplit(char *z, const char *delim, int32_t *num) {
if ((*num) >= size) { if ((*num) >= size) {
size = (size << 1); size = (size << 1);
split = taosMemoryRealloc(split, POINTER_BYTES * size); split = taosMemoryRealloc(split, POINTER_BYTES * size);
ASSERTS(NULL != split, "realloc memory failed. size=%d", (int32_t) POINTER_BYTES * size); if (split == NULL) {
return NULL;
}
ASSERTS(NULL != split, "realloc memory failed. size=%d", (int32_t)POINTER_BYTES * size);
} }
} }
@ -145,10 +151,10 @@ char *strnchr(const char *haystack, char needle, int32_t len, bool skipquote) {
return NULL; return NULL;
} }
TdUcs4* wcsnchr(const TdUcs4* haystack, TdUcs4 needle, size_t len) { TdUcs4 *wcsnchr(const TdUcs4 *haystack, TdUcs4 needle, size_t len) {
for(int32_t i = 0; i < len; ++i) { for (int32_t i = 0; i < len; ++i) {
if (haystack[i] == needle) { if (haystack[i] == needle) {
return (TdUcs4*) &haystack[i]; return (TdUcs4 *)&haystack[i];
} }
} }
@ -314,6 +320,9 @@ char *strbetween(char *string, char *begin, char *end) {
int32_t size = (int32_t)(_end - _begin); int32_t size = (int32_t)(_end - _begin);
if (_end != NULL && size > 0) { if (_end != NULL && size > 0) {
result = (char *)taosMemoryCalloc(1, size); result = (char *)taosMemoryCalloc(1, size);
if (result) {
return NULL;
}
memcpy(result, _begin + strlen(begin), size - +strlen(begin)); memcpy(result, _begin + strlen(begin), size - +strlen(begin));
} }
} }
@ -324,13 +333,13 @@ int32_t tintToHex(uint64_t val, char hex[]) {
const char hexstr[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; const char hexstr[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
int32_t j = 0, k = 0; int32_t j = 0, k = 0;
if (val == 0) { if (val == 0) {
hex[j++] = hexstr[0]; hex[j++] = hexstr[0];
return j; return j;
} }
// ignore the initial 0 // ignore the initial 0
while((val & (((uint64_t)0xfL) << ((15 - k) * 4))) == 0) { while ((val & (((uint64_t)0xfL) << ((15 - k) * 4))) == 0) {
k += 1; k += 1;
} }
@ -346,10 +355,10 @@ int32_t titoa(uint64_t val, size_t radix, char str[]) {
return 0; return 0;
} }
const char* s = "0123456789abcdef"; const char *s = "0123456789abcdef";
char buf[65] = {0}; char buf[65] = {0};
int32_t i = 0; int32_t i = 0;
uint64_t v = val; uint64_t v = val;
do { do {
buf[i++] = s[v % radix]; buf[i++] = s[v % radix];
@ -357,7 +366,7 @@ int32_t titoa(uint64_t val, size_t radix, char str[]) {
} while (v > 0); } while (v > 0);
// reverse order // reverse order
for(int32_t j = 0; j < i; ++j) { for (int32_t j = 0; j < i; ++j) {
str[j] = buf[i - j - 1]; str[j] = buf[i - j - 1];
} }
@ -429,8 +438,8 @@ void taosIpPort2String(uint32_t ip, uint16_t port, char *str) {
size_t tstrncspn(const char *str, size_t size, const char *reject, size_t rsize) { size_t tstrncspn(const char *str, size_t size, const char *reject, size_t rsize) {
if (rsize == 0 || rsize == 1) { if (rsize == 0 || rsize == 1) {
char* p = strnchr(str, reject[0], size, false); char *p = strnchr(str, reject[0], size, false);
return (p == NULL)? size:(p-str); return (p == NULL) ? size : (p - str);
} }
/* Use multiple small memsets to enable inlining on most targets. */ /* Use multiple small memsets to enable inlining on most targets. */
@ -441,15 +450,15 @@ size_t tstrncspn(const char *str, size_t size, const char *reject, size_t rsize)
memset(p + 192, 0, 64); memset(p + 192, 0, 64);
unsigned char *s = (unsigned char *)reject; unsigned char *s = (unsigned char *)reject;
int32_t index = 0; int32_t index = 0;
do { do {
p[s[index++]] = 1; p[s[index++]] = 1;
} while (index < rsize); } while (index < rsize);
s = (unsigned char*) str; s = (unsigned char *)str;
int32_t times = size >> 2; int32_t times = size >> 2;
if (times == 0) { if (times == 0) {
for(int32_t i = 0; i < size; ++i) { for (int32_t i = 0; i < size; ++i) {
if (p[s[i]]) { if (p[s[i]]) {
return i; return i;
} }
@ -460,7 +469,7 @@ size_t tstrncspn(const char *str, size_t size, const char *reject, size_t rsize)
index = 0; index = 0;
uint32_t c0, c1, c2, c3; uint32_t c0, c1, c2, c3;
for(int32_t i = 0; i < times; ++i, index += 4) { for (int32_t i = 0; i < times; ++i, index += 4) {
int32_t j = index; int32_t j = index;
c0 = p[s[j]]; c0 = p[s[j]];
c1 = p[s[j + 1]]; c1 = p[s[j + 1]];
@ -474,7 +483,7 @@ size_t tstrncspn(const char *str, size_t size, const char *reject, size_t rsize)
} }
int32_t offset = times * 4; int32_t offset = times * 4;
for(int32_t i = offset; i < size; ++i) { for (int32_t i = offset; i < size; ++i) {
if (p[s[i]]) { if (p[s[i]]) {
return i; return i;
} }
@ -485,8 +494,8 @@ size_t tstrncspn(const char *str, size_t size, const char *reject, size_t rsize)
size_t twcsncspn(const TdUcs4 *wcs, size_t size, const TdUcs4 *reject, size_t rsize) { size_t twcsncspn(const TdUcs4 *wcs, size_t size, const TdUcs4 *reject, size_t rsize) {
if (rsize == 0 || rsize == 1) { if (rsize == 0 || rsize == 1) {
TdUcs4* p = wcsnchr(wcs, reject[0], size); TdUcs4 *p = wcsnchr(wcs, reject[0], size);
return (p == NULL)? size:(p-wcs); return (p == NULL) ? size : (p - wcs);
} }
size_t index = 0; size_t index = 0;
@ -497,19 +506,17 @@ size_t twcsncspn(const TdUcs4 *wcs, size_t size, const TdUcs4 *reject, size_t rs
return index; return index;
} }
int32_t parseCfgReal(const char* str, double* out) { int32_t parseCfgReal(const char *str, double *out) {
double val; double val;
char *endPtr; char *endPtr;
errno = 0; errno = 0;
val = taosStr2Double(str, &endPtr); val = taosStr2Double(str, &endPtr);
if (str == endPtr || errno == ERANGE || isnan(val)) { if (str == endPtr || errno == ERANGE || isnan(val)) {
terrno = TSDB_CODE_INVALID_CFG_VALUE; return terrno = TSDB_CODE_INVALID_CFG_VALUE;
return -1;
} }
while(isspace((unsigned char)*endPtr)) endPtr++; while (isspace((unsigned char)*endPtr)) endPtr++;
if (*endPtr != '\0') { if (*endPtr != '\0') {
terrno = TSDB_CODE_INVALID_CFG_VALUE; return terrno = TSDB_CODE_INVALID_CFG_VALUE;
return -1;
} }
*out = val; *out = val;
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;