Merge pull request #24798 from taosdata/feat/TD-27463-3.0
enh: get machine id and tbase58 codec
This commit is contained in:
commit
67f5684151
|
@ -1610,7 +1610,6 @@ typedef struct {
|
|||
SEp ep;
|
||||
char active[TSDB_ACTIVE_KEY_LEN];
|
||||
char connActive[TSDB_CONN_ACTIVE_KEY_LEN];
|
||||
char machineId[TSDB_MACHINE_ID_LEN + 1];
|
||||
} SDnodeInfo;
|
||||
|
||||
typedef struct {
|
||||
|
|
|
@ -22,6 +22,9 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define TBASE_MAX_ILEN 4096
|
||||
#define TBASE_MAX_OLEN 5653
|
||||
|
||||
uint8_t *base58_decode(const char *value, size_t inlen, int32_t *outlen);
|
||||
char *base58_encode(const uint8_t *value, int32_t vlen);
|
||||
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
#define _DEFAULT_SOURCE
|
||||
#include "dmInt.h"
|
||||
#include "systable.h"
|
||||
#include "tgrant.h"
|
||||
|
||||
extern SConfig *tsCfg;
|
||||
|
||||
|
@ -118,11 +117,7 @@ void dmSendStatusReq(SDnodeMgmt *pMgmt) {
|
|||
req.memTotal = tsTotalMemoryKB * 1024;
|
||||
req.memAvail = req.memTotal - tsRpcQueueMemoryAllowed - 16 * 1024 * 1024;
|
||||
tstrncpy(req.dnodeEp, tsLocalEp, TSDB_EP_LEN);
|
||||
char *machine = tGetMachineId();
|
||||
if (machine) {
|
||||
tstrncpy(req.machineId, machine, TSDB_MACHINE_ID_LEN + 1);
|
||||
taosMemoryFreeClear(machine);
|
||||
}
|
||||
tstrncpy(req.machineId, pMgmt->pData->machineId, TSDB_MACHINE_ID_LEN + 1);
|
||||
|
||||
req.clusterCfg.statusInterval = tsStatusInterval;
|
||||
req.clusterCfg.checkTime = 0;
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#ifdef TD_TSZ
|
||||
#include "tcompression.h"
|
||||
#include "tglobal.h"
|
||||
#include "tgrant.h"
|
||||
#endif
|
||||
|
||||
static bool dmRequireNode(SDnode *pDnode, SMgmtWrapper *pWrapper) {
|
||||
|
@ -137,6 +138,16 @@ int32_t dmInitVars(SDnode *pDnode) {
|
|||
pData->rebootTime = taosGetTimestampMs();
|
||||
pData->dropped = 0;
|
||||
pData->stopped = 0;
|
||||
char *machineId = tGetMachineId();
|
||||
if (machineId) {
|
||||
tstrncpy(pData->machineId, machineId, TSDB_MACHINE_ID_LEN + 1);
|
||||
taosMemoryFreeClear(machineId);
|
||||
} else {
|
||||
#if defined(TD_ENTERPRISE) && !defined(GRANTS_CFG)
|
||||
terrno = TSDB_CODE_DNODE_NO_MACHINE_CODE;
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
pData->dnodeHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
|
||||
if (pData->dnodeHash == NULL) {
|
||||
|
|
|
@ -109,6 +109,7 @@ typedef struct {
|
|||
SMsgCb msgCb;
|
||||
bool validMnodeEps;
|
||||
int64_t ipWhiteVer;
|
||||
char machineId[TSDB_MACHINE_ID_LEN + 1];
|
||||
} SDnodeData;
|
||||
|
||||
typedef struct {
|
||||
|
|
|
@ -141,7 +141,7 @@ static int32_t mndCreateDefaultDnode(SMnode *pMnode) {
|
|||
memcpy(dnodeObj.machineId, machineId, TSDB_MACHINE_ID_LEN);
|
||||
taosMemoryFreeClear(machineId);
|
||||
} else {
|
||||
#ifdef TD_ENTERPRISE
|
||||
#if defined(TD_ENTERPRISE) && !defined(GRANTS_CFG)
|
||||
terrno = TSDB_CODE_DNODE_NO_MACHINE_CODE;
|
||||
goto _OVER;
|
||||
#endif
|
||||
|
@ -410,9 +410,6 @@ void mndGetDnodeData(SMnode *pMnode, SArray *pDnodeInfo) {
|
|||
dInfo.ep.port = pDnode->port;
|
||||
dInfo.offlineReason = pDnode->offlineReason;
|
||||
tstrncpy(dInfo.ep.fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
|
||||
tstrncpy(dInfo.active, pDnode->active, TSDB_ACTIVE_KEY_LEN);
|
||||
tstrncpy(dInfo.connActive, pDnode->connActive, TSDB_CONN_ACTIVE_KEY_LEN);
|
||||
tstrncpy(dInfo.machineId, pDnode->machineId, TSDB_MACHINE_ID_LEN + 1);
|
||||
sdbRelease(pSdb, pDnode);
|
||||
if (mndIsMnode(pMnode, pDnode->id)) {
|
||||
dInfo.isMnode = 1;
|
||||
|
|
|
@ -18,24 +18,29 @@
|
|||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define BASE_BUF_SIZE 256
|
||||
#define TBASE_BUF_SIZE 256
|
||||
static const char *basis_58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
||||
|
||||
char *base58_encode(const uint8_t *value, int32_t vlen) {
|
||||
const uint8_t *pb = value;
|
||||
const uint8_t *pe = pb + vlen;
|
||||
uint8_t buf[BASE_BUF_SIZE] = {0};
|
||||
uint8_t buf[TBASE_BUF_SIZE] = {0};
|
||||
uint8_t *pbuf = &buf[0];
|
||||
bool bfree = false;
|
||||
int32_t nz = 0, size = 0, len = 0;
|
||||
|
||||
if (vlen > TBASE_MAX_ILEN) {
|
||||
terrno = TSDB_CODE_INVALID_PARA;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (pb != pe && *pb == 0) {
|
||||
++pb;
|
||||
++nz;
|
||||
}
|
||||
|
||||
size = (pe - pb) * 69 / 50 + 1;
|
||||
if (size > BASE_BUF_SIZE) {
|
||||
if (size > TBASE_BUF_SIZE) {
|
||||
if (!(pbuf = taosMemoryCalloc(1, size))) {
|
||||
terrno = TSDB_CODE_OUT_OF_MEMORY;
|
||||
return NULL;
|
||||
|
@ -47,7 +52,7 @@ char *base58_encode(const uint8_t *value, int32_t vlen) {
|
|||
int32_t num = *pb;
|
||||
int32_t i = 0;
|
||||
for (int32_t j = (int32_t)size - 1; (num != 0 || i < len) && j >= 0; --j, ++i) {
|
||||
num += ((int32_t)buf[j]) << 8;
|
||||
num += ((int32_t)pbuf[j]) << 8;
|
||||
pbuf[j] = num % 58;
|
||||
num /= 58;
|
||||
}
|
||||
|
@ -57,7 +62,7 @@ char *base58_encode(const uint8_t *value, int32_t vlen) {
|
|||
|
||||
const uint8_t *pi = pbuf + (size - len);
|
||||
while (pi != pbuf + size && *pi == 0) ++pi;
|
||||
uint8_t *result = taosMemoryCalloc(1, size + 1);
|
||||
uint8_t *result = taosMemoryCalloc(1, nz + (pbuf + size - pi) + 1);
|
||||
if (!result) {
|
||||
terrno = TSDB_CODE_OUT_OF_MEMORY;
|
||||
if (bfree) taosMemoryFree(pbuf);
|
||||
|
@ -82,20 +87,35 @@ static const signed char index_58[256] = {
|
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
|
||||
|
||||
uint8_t *base58_decode(const char *value, size_t inlen, int32_t *outlen) {
|
||||
const char *pb = value;
|
||||
const char *pe = value + inlen;
|
||||
uint8_t buf[BASE_BUF_SIZE] = {0};
|
||||
uint8_t buf[TBASE_BUF_SIZE] = {0};
|
||||
uint8_t *pbuf = &buf[0];
|
||||
bool bfree = false;
|
||||
int32_t nz = 0, size = 0, len = 0;
|
||||
|
||||
while (*value && isspace(*value)) ++value;
|
||||
while (*value == '1') {
|
||||
++nz;
|
||||
++value;
|
||||
if (inlen > TBASE_MAX_OLEN) {
|
||||
terrno = TSDB_CODE_INVALID_PARA;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size = (int32_t)(pe - value) * 733 / 1000 + 1;
|
||||
if (size > BASE_BUF_SIZE) {
|
||||
while (pb != pe) {
|
||||
if (*pb == 0) {
|
||||
terrno = TSDB_CODE_INVALID_PARA;
|
||||
return NULL;
|
||||
}
|
||||
++pb;
|
||||
}
|
||||
|
||||
pb = value;
|
||||
while (pb != pe && *pb && isspace(*pb)) ++pb;
|
||||
while (pb != pe && *pb == '1') {
|
||||
++nz;
|
||||
++pb;
|
||||
}
|
||||
|
||||
size = (int32_t)(pe - pb) * 733 / 1000 + 1;
|
||||
if (size > TBASE_BUF_SIZE) {
|
||||
if (!(pbuf = taosMemoryCalloc(1, size))) {
|
||||
terrno = TSDB_CODE_OUT_OF_MEMORY;
|
||||
return NULL;
|
||||
|
@ -103,9 +123,10 @@ uint8_t *base58_decode(const char *value, size_t inlen, int32_t *outlen) {
|
|||
bfree = true;
|
||||
}
|
||||
|
||||
while (*value && !isspace(*value)) {
|
||||
int32_t num = index_58[(uint8_t)*value];
|
||||
while (pb != pe && *pb && !isspace(*pb)) {
|
||||
int32_t num = index_58[(uint8_t)*pb];
|
||||
if (num == -1) {
|
||||
terrno = TSDB_CODE_INVALID_PARA;
|
||||
if (bfree) taosMemoryFree(pbuf);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -116,18 +137,18 @@ uint8_t *base58_decode(const char *value, size_t inlen, int32_t *outlen) {
|
|||
num >>= 8;
|
||||
}
|
||||
len = i;
|
||||
++value;
|
||||
++pb;
|
||||
}
|
||||
|
||||
while (isspace(*value)) ++value;
|
||||
if (*value != 0) {
|
||||
while (pb != pe && isspace(*pb)) ++pb;
|
||||
if (*pb != 0) {
|
||||
if (bfree) taosMemoryFree(pbuf);
|
||||
return NULL;
|
||||
}
|
||||
const uint8_t *it = pbuf + (size - len);
|
||||
while (it != pbuf + size && *it == 0) ++it;
|
||||
|
||||
uint8_t *result = taosMemoryCalloc(1, size + 1);
|
||||
uint8_t *result = taosMemoryCalloc(1, nz + (pbuf + size - it) + 1);
|
||||
if (!result) {
|
||||
if (bfree) taosMemoryFree(pbuf);
|
||||
terrno = TSDB_CODE_OUT_OF_MEMORY;
|
||||
|
|
|
@ -100,3 +100,11 @@ add_test(
|
|||
NAME talgoTest
|
||||
COMMAND talgoTest
|
||||
)
|
||||
|
||||
# tbaseCodecTest
|
||||
add_executable(tbaseCodecTest "tbaseCodecTest.cpp")
|
||||
target_link_libraries(tbaseCodecTest os util common gtest_main)
|
||||
add_test(
|
||||
NAME tbaseCodecTest
|
||||
COMMAND tbaseCodecTest
|
||||
)
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
#include <gtest/gtest.h>
|
||||
#include <cassert>
|
||||
|
||||
#include <iostream>
|
||||
#include "os.h"
|
||||
#include "osTime.h"
|
||||
#include "taos.h"
|
||||
#include "taoserror.h"
|
||||
#include "tbase58.h"
|
||||
#include "tglobal.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wwrite-strings"
|
||||
#pragma GCC diagnostic ignored "-Wunused-function"
|
||||
#pragma GCC diagnostic ignored "-Wunused-variable"
|
||||
#pragma GCC diagnostic ignored "-Wsign-compare"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
|
||||
static void checkBase58Codec(uint8_t *pRaw, int32_t rawLen, int32_t index) {
|
||||
int64_t start = taosGetTimestampUs();
|
||||
char *pEnc = base58_encode((const uint8_t *)pRaw, rawLen);
|
||||
ASSERT_NE(nullptr, pEnc);
|
||||
|
||||
int32_t encLen = strlen(pEnc);
|
||||
int64_t endOfEnc = taosGetTimestampUs();
|
||||
std::cout << "index:" << index << ", encLen is " << encLen << ", cost:" << endOfEnc - start << " us" << std::endl;
|
||||
int32_t decLen = 0;
|
||||
char *pDec = (char *)base58_decode((const char *)pEnc, encLen, &decLen);
|
||||
std::cout << "index:" << index << ", decLen is " << decLen << ", cost:" << taosGetTimestampUs() - endOfEnc << " us"
|
||||
<< std::endl;
|
||||
ASSERT_NE(nullptr, pDec);
|
||||
ASSERT_EQ(rawLen, decLen);
|
||||
ASSERT_LE(rawLen, encLen);
|
||||
ASSERT_EQ(0, strncmp((char *)pRaw, pDec, rawLen));
|
||||
taosMemoryFreeClear(pDec);
|
||||
taosMemoryFreeClear(pEnc);
|
||||
}
|
||||
|
||||
TEST(TD_BASE_CODEC_TEST, tbase58_test) {
|
||||
const int32_t TEST_LEN_MAX = TBASE_MAX_ILEN;
|
||||
const int32_t TEST_LEN_STEP = 10;
|
||||
int32_t rawLen = 0;
|
||||
uint8_t *pRaw = NULL;
|
||||
|
||||
pRaw = (uint8_t *)taosMemoryCalloc(1, TEST_LEN_MAX);
|
||||
ASSERT_NE(nullptr, pRaw);
|
||||
|
||||
// 1. normal case
|
||||
// string blend with char and '\0'
|
||||
rawLen = TEST_LEN_MAX;
|
||||
for (int32_t i = 0; i < TEST_LEN_MAX; i += 500) {
|
||||
checkBase58Codec(pRaw, rawLen, i);
|
||||
pRaw[i] = i & 127;
|
||||
}
|
||||
|
||||
// string without '\0'
|
||||
for (int32_t i = 0; i < TEST_LEN_MAX; ++i) {
|
||||
pRaw[i] = i & 127;
|
||||
}
|
||||
checkBase58Codec(pRaw, TEST_LEN_MAX, 0);
|
||||
for (int32_t i = 0; i < TEST_LEN_MAX; i += 500) {
|
||||
rawLen = i;
|
||||
checkBase58Codec(pRaw, rawLen, i);
|
||||
}
|
||||
taosMemoryFreeClear(pRaw);
|
||||
ASSERT_EQ(nullptr, pRaw);
|
||||
|
||||
// 2. overflow case
|
||||
char tmp[1];
|
||||
char *pEnc = base58_encode((const uint8_t *)tmp, TBASE_MAX_ILEN + 1);
|
||||
ASSERT_EQ(nullptr, pEnc);
|
||||
char *pDec = (char *)base58_decode((const char *)tmp, TBASE_MAX_OLEN + 1, NULL);
|
||||
ASSERT_EQ(nullptr, pDec);
|
||||
|
||||
taosMemoryFreeClear(pRaw);
|
||||
ASSERT_EQ(nullptr, pRaw);
|
||||
}
|
Loading…
Reference in New Issue