diff --git a/include/os/osSysinfo.h b/include/os/osSysinfo.h index fb6acf2a0e..d292b326d5 100644 --- a/include/os/osSysinfo.h +++ b/include/os/osSysinfo.h @@ -52,7 +52,8 @@ int32_t taosGetCardInfoDelta(int64_t *receive_bytes, int64_t *transmit_bytes); void taosSetDefaultCardInfoDelta(int64_t *receive_bytes, int64_t *transmit_bytes); void taosKillSystem(); -int32_t taosGetSystemUUID(char *uid, int32_t uidlen); +int32_t taosGetSystemUUIDLimit36(char *uid, int32_t uidlen); +int32_t taosGetSystemUUIDLen(char *uid, int32_t uidlen); char *taosGetCmdlineByPID(int32_t pid); void taosSetCoreDump(bool enable); diff --git a/include/util/tuuid.h b/include/util/tuuid.h index 9bac855a6c..f7e71b910f 100644 --- a/include/util/tuuid.h +++ b/include/util/tuuid.h @@ -49,5 +49,5 @@ int64_t tGenIdPI64(void); */ int64_t tGenQid64(int8_t dnodeId); -int32_t taosGetSystemUUID32(uint32_t *uuid); -int32_t taosGetSystemUUID64(uint64_t *uuid); +int32_t taosGetSystemUUIDU32(uint32_t *uuid); +int32_t taosGetSystemUUIDU64(uint64_t *uuid); diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index bcfc5b7a5a..694e085007 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -1098,7 +1098,7 @@ uint64_t generateRequestId() { static int32_t requestSerialId = 0; if (hashId == 0) { - int32_t code = taosGetSystemUUID32(&hashId); + int32_t code = taosGetSystemUUIDU32(&hashId); if (code != TSDB_CODE_SUCCESS) { tscError("Failed to get the system uid to generated request id, reason:%s. use ip address instead", tstrerror(code)); diff --git a/source/dnode/mnode/impl/src/mndCluster.c b/source/dnode/mnode/impl/src/mndCluster.c index 5e38a91ab0..3779bea564 100644 --- a/source/dnode/mnode/impl/src/mndCluster.c +++ b/source/dnode/mnode/impl/src/mndCluster.c @@ -241,7 +241,7 @@ static int32_t mndCreateDefaultCluster(SMnode *pMnode) { clusterObj.createdTime = taosGetTimestampMs(); clusterObj.updateTime = clusterObj.createdTime; - int32_t code = taosGetSystemUUID(clusterObj.name, TSDB_CLUSTER_ID_LEN); + int32_t code = taosGetSystemUUIDLen(clusterObj.name, TSDB_CLUSTER_ID_LEN); if (code != 0) { (void)strcpy(clusterObj.name, "tdengine3.0"); mError("failed to get name from system, set to default val %s", clusterObj.name); diff --git a/source/libs/scheduler/src/schUtil.c b/source/libs/scheduler/src/schUtil.c index 6cfeab3467..e6945dac89 100644 --- a/source/libs/scheduler/src/schUtil.c +++ b/source/libs/scheduler/src/schUtil.c @@ -297,24 +297,21 @@ uint64_t schGenTaskId(void) { return atomic_add_fetch_64(&schMgmt.taskId, 1); } #ifdef BUILD_NO_CALL uint64_t schGenUUID(void) { - static uint64_t hashId = 0; + static uint32_t hashId = 0; static int32_t requestSerialId = 0; if (hashId == 0) { - char uid[64] = {0}; - int32_t code = taosGetSystemUUID(uid, tListLen(uid) - 1); + int32_t code = taosGetSystemUUID32(&hashId); if (code != TSDB_CODE_SUCCESS) { qError("Failed to get the system uid, reason:%s", tstrerror(TAOS_SYSTEM_ERROR(errno))); - } else { - hashId = MurmurHash3_32(uid, strlen(uid)); } } - +ßßß int64_t ts = taosGetTimestampMs(); uint64_t pid = taosGetPId(); int32_t val = atomic_add_fetch_32(&requestSerialId, 1); - uint64_t id = ((hashId & 0x0FFF) << 52) | ((pid & 0x0FFF) << 40) | ((ts & 0xFFFFFF) << 16) | (val & 0xFFFF); + uint64_t id = ((uint64_t)((hashId & 0x0FFF)) << 52) | ((pid & 0x0FFF) << 40) | ((ts & 0xFFFFFF) << 16) | (val & 0xFFFF); return id; } #endif diff --git a/source/libs/scheduler/src/scheduler.c b/source/libs/scheduler/src/scheduler.c index f3d9d88c5e..091de5c048 100644 --- a/source/libs/scheduler/src/scheduler.c +++ b/source/libs/scheduler/src/scheduler.c @@ -56,7 +56,7 @@ int32_t schedulerInit() { SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY); } - if (taosGetSystemUUID64(&schMgmt.sId)) { + if (taosGetSystemUUIDU64(&schMgmt.sId)) { qError("generate schedulerId failed, errno:%d", errno); SCH_ERR_RET(TSDB_CODE_QRY_SYS_ERROR); } diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index 905f3d008b..bd1a058291 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -1035,19 +1035,17 @@ void taosKillSystem() { #endif } -int32_t taosGetSystemUUID(char *uid, int32_t uidlen) { +#define UUIDLEN (36) +int32_t taosGetSystemUUIDLimit36(char *uid, int32_t uidlen) { #ifdef WINDOWS GUID guid; HRESULT h = CoCreateGuid(&guid); if (h != S_OK) { return TAOS_SYSTEM_WINAPI_ERROR(GetLastError()); } - int n = snprintf(uid, uidlen, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", guid.Data1, guid.Data2, guid.Data3, + (void)snprintf(uid, uidlen, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]); - if(n >= uidlen) { - return TSDB_CODE_OUT_OF_BUFFER;; - } return 0; #elif defined(_TD_DARWIN_64) @@ -1057,10 +1055,7 @@ int32_t taosGetSystemUUID(char *uid, int32_t uidlen) { uuid_generate(uuid); // it's caller's responsibility to make enough space for `uid`, that's 36-char + 1-null uuid_unparse_lower(uuid, buf); - int n = snprintf(uid, uidlen, "%.*s", (int)sizeof(buf), buf); // though less performance, much safer - if(n >= uidlen) { - return TSDB_CODE_OUT_OF_BUFFER;; - } + (void)snprintf(uid, uidlen, "%.*s", (int)sizeof(buf), buf); return 0; #else int64_t len = 0; @@ -1076,16 +1071,32 @@ int32_t taosGetSystemUUID(char *uid, int32_t uidlen) { return terrno; } } - - if (len >= 36) { - uid[36] = 0; - return 0; + if (len >= UUIDLEN + 1) { + uid[len - 1] = 0; + } else { + uid[uidlen - 1] = 0; } return 0; #endif } +int32_t taosGetSystemUUIDLen(char *uid, int32_t uidlen) { + if (uid == NULL || uidlen <= 0) { + return TSDB_CODE_APP_ERROR; + } + int num = (uidlen % UUIDLEN == 0) ? (uidlen / UUIDLEN) : (uidlen / UUIDLEN + 1); + int left = uidlen; + for (int i = 0; i < num; ++i) { + int32_t code = taosGetSystemUUIDLimit36(uid + i * UUIDLEN, left); + if (code != 0) { + return code; + } + left -= UUIDLEN; + } + return TSDB_CODE_SUCCESS; +} + char *taosGetCmdlineByPID(int pid) { #ifdef WINDOWS return ""; diff --git a/source/os/test/osSystemTests.cpp b/source/os/test/osSystemTests.cpp index 3712e45451..bc122aafa0 100644 --- a/source/os/test/osSystemTests.cpp +++ b/source/os/test/osSystemTests.cpp @@ -60,3 +60,88 @@ TEST(osSystemTest, osSystem1) { (void)printf("cpu info: %s\n", tmp); ASSERT_EQ(res, 0); } + + +TEST(osSystemTest, systemUUIDTest) { + char uuid1[38]; + memset(uuid1, 0, sizeof(uuid1)); + taosGetSystemUUIDLimit36(uuid1, sizeof(uuid1)); + ASSERT_EQ(strlen(uuid1), 36); + + char uuid2[34]; + memset(uuid2, 0, sizeof(uuid2)); + taosGetSystemUUIDLimit36(uuid2, sizeof(uuid2)); + ASSERT_EQ(strlen(uuid2), 33); + + char uuid3[36]; + memset(uuid3, 0, sizeof(uuid3)); + taosGetSystemUUIDLimit36(uuid3, sizeof(uuid3)); + ASSERT_EQ(strlen(uuid3), 35); + + char uuid4[2]; + memset(uuid4, 0, sizeof(uuid4)); + taosGetSystemUUIDLimit36(uuid4, sizeof(uuid4)); + ASSERT_EQ(strlen(uuid4), 1); + + char uuid5[36]; + memset( uuid5, 0, sizeof(uuid5)); + taosGetSystemUUIDLimit36(uuid5, sizeof(uuid5)); + ASSERT_EQ(strlen(uuid5), 35); + + char uuid6[37]; + memset( uuid6, 0, sizeof(uuid6)); + taosGetSystemUUIDLimit36(uuid6, sizeof(uuid6)); + ASSERT_EQ(strlen(uuid6), 36); + + char uuid7[1]; + memset(uuid7, 0, sizeof(uuid7)); + taosGetSystemUUIDLimit36(uuid7, sizeof(uuid7)); + ASSERT_EQ(strlen(uuid7), 0); +} + +TEST(osSystemTest, systemUUIDTest2) { + char uuid1[38]; + memset(uuid1, 0, sizeof(uuid1)); + taosGetSystemUUIDLen(uuid1, sizeof(uuid1)); + ASSERT_EQ(strlen(uuid1), sizeof(uuid1) - 1); + + char uuid2[34]; + memset(uuid2, 0, sizeof(uuid2)); + taosGetSystemUUIDLen(uuid2, sizeof(uuid2)); + ASSERT_EQ(strlen(uuid2), sizeof(uuid2) - 1); + + char uuid3[36]; + memset(uuid3, 0, sizeof(uuid3)); + taosGetSystemUUIDLen(uuid3, sizeof(uuid3)); + ASSERT_EQ(strlen(uuid3), sizeof(uuid3) - 1); + + char uuid4[2]; + memset(uuid4, 0, sizeof(uuid4)); + taosGetSystemUUIDLen(uuid4, sizeof(uuid4)); + ASSERT_EQ(strlen(uuid4), sizeof(uuid4) - 1); + + char uuid5[36]; + memset( uuid5, 0, sizeof(uuid5)); + taosGetSystemUUIDLen(uuid5, sizeof(uuid5)); + ASSERT_EQ(strlen(uuid5), sizeof(uuid5) - 1); + + char uuid6[37]; + memset( uuid6, 0, sizeof(uuid6)); + taosGetSystemUUIDLen(uuid6, sizeof(uuid6)); + ASSERT_EQ(strlen(uuid6), sizeof(uuid6) - 1); + + char uuid7[1]; + memset(uuid7, 0, sizeof(uuid7)); + taosGetSystemUUIDLen(uuid7, sizeof(uuid7)); + ASSERT_EQ(strlen(uuid7), sizeof(uuid7) - 1); + + char uuid8[40]; + memset(uuid8, 0, sizeof(uuid8)); + taosGetSystemUUIDLen(uuid8, sizeof(uuid8)); + ASSERT_EQ(strlen(uuid8), sizeof(uuid8) - 1); + + char uuid9[73]; + memset(uuid9, 0, sizeof(uuid9)); + taosGetSystemUUIDLen(uuid9, sizeof(uuid9)); + ASSERT_EQ(strlen(uuid9), sizeof(uuid9) - 1); +} diff --git a/source/util/src/tuuid.c b/source/util/src/tuuid.c index 53ef5c493d..4d50d93e74 100644 --- a/source/util/src/tuuid.c +++ b/source/util/src/tuuid.c @@ -18,11 +18,11 @@ static uint32_t tUUIDHashId = 0; static int32_t tUUIDSerialNo = 0; -int32_t taosGetSystemUUID32(uint32_t *uuid) { +int32_t taosGetSystemUUIDU32(uint32_t *uuid) { if (uuid == NULL) return TSDB_CODE_APP_ERROR; - char uid[65] = {0}; - int32_t code = taosGetSystemUUID(uid, sizeof(uid)); - uid[64] = 0; + char uid[37] = {0}; + int32_t code = taosGetSystemUUIDLimit36(uid, sizeof(uid)); + uid[36] = 0; if (code != TSDB_CODE_SUCCESS) { return code; @@ -32,11 +32,11 @@ int32_t taosGetSystemUUID32(uint32_t *uuid) { return TSDB_CODE_SUCCESS; } -int32_t taosGetSystemUUID64(uint64_t *uuid) { +int32_t taosGetSystemUUIDU64(uint64_t *uuid) { if (uuid == NULL) return TSDB_CODE_APP_ERROR; - char uid[65] = {0}; - int32_t code = taosGetSystemUUID(uid, sizeof(uid)); - uid[64] = 0; + char uid[37] = {0}; + int32_t code = taosGetSystemUUIDLimit36(uid, sizeof(uid)); + uid[36] = 0; if (code != TSDB_CODE_SUCCESS) { return code; @@ -48,7 +48,7 @@ int32_t taosGetSystemUUID64(uint64_t *uuid) { int32_t tGenIdPI32(void) { if (tUUIDHashId == 0) { - int32_t code = taosGetSystemUUID32(&tUUIDHashId); + int32_t code = taosGetSystemUUIDU32(&tUUIDHashId); if (code != TSDB_CODE_SUCCESS) { terrno = code; } @@ -64,7 +64,7 @@ int32_t tGenIdPI32(void) { int64_t tGenIdPI64(void) { if (tUUIDHashId == 0) { - int32_t code = taosGetSystemUUID32(&tUUIDHashId); + int32_t code = taosGetSystemUUIDU32(&tUUIDHashId); if (code != TSDB_CODE_SUCCESS) { terrno = code; }