TDengine/source/dnode/mnode/impl/test/mnode/mnode.cpp

393 lines
10 KiB
C++
Raw Normal View History

2021-12-23 02:30:51 +00:00
/**
2022-01-06 11:18:13 +00:00
* @file mnode.cpp
2021-12-23 02:30:51 +00:00
* @author slguan (slguan@taosdata.com)
2022-01-06 11:18:13 +00:00
* @brief MNODE module mnode tests
* @version 1.0
* @date 2022-01-07
2021-12-23 02:30:51 +00:00
*
2022-01-06 11:18:13 +00:00
* @copyright Copyright (c) 2022
2021-12-23 02:30:51 +00:00
*
*/
2022-01-04 12:36:54 +00:00
#include "sut.h"
2021-12-23 02:30:51 +00:00
2022-01-06 11:18:13 +00:00
class MndTestMnode : public ::testing::Test {
2021-12-23 02:30:51 +00:00
public:
void SetUp() override {}
void TearDown() override {}
public:
static void SetUpTestSuite() {
test.Init(TD_TMP_DIR_PATH "mnode_test_mnode1", 9028);
2021-12-23 02:30:51 +00:00
const char* fqdn = "localhost";
2022-01-07 11:52:59 +00:00
const char* firstEp = "localhost:9028";
2021-12-23 02:30:51 +00:00
// server2.Start(TD_TMP_DIR_PATH "mnode_test_mnode2", fqdn, 9029, firstEp);
2021-12-23 02:30:51 +00:00
taosMsleep(300);
}
static void TearDownTestSuite() {
server2.Stop();
test.Cleanup();
}
static Testbase test;
static TestServer server2;
};
2022-01-06 11:18:13 +00:00
Testbase MndTestMnode::test;
TestServer MndTestMnode::server2;
2021-12-23 02:30:51 +00:00
2022-01-06 11:18:13 +00:00
TEST_F(MndTestMnode, 01_ShowDnode) {
2022-04-15 02:47:00 +00:00
test.SendShowReq(TSDB_MGMT_TABLE_MNODE, "mnodes", "");
2021-12-23 02:30:51 +00:00
EXPECT_EQ(test.GetShowRows(), 1);
}
2022-01-06 11:18:13 +00:00
TEST_F(MndTestMnode, 02_Create_Mnode_Invalid_Id) {
2021-12-23 02:30:51 +00:00
{
2022-02-12 06:21:26 +00:00
SMCreateMnodeReq createReq = {0};
createReq.dnodeId = 1;
2021-12-23 02:30:51 +00:00
2022-10-18 09:34:58 +00:00
int32_t contLen = tSerializeSCreateDropMQSNodeReq(NULL, 0, &createReq);
2022-02-12 06:21:26 +00:00
void* pReq = rpcMallocCont(contLen);
2022-10-18 09:34:58 +00:00
tSerializeSCreateDropMQSNodeReq(pReq, contLen, &createReq);
2021-12-23 02:30:51 +00:00
2022-01-05 12:18:56 +00:00
SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_MNODE, pReq, contLen);
ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, TSDB_CODE_MND_MNODE_ALREADY_EXIST);
2021-12-23 02:30:51 +00:00
}
2021-12-23 09:47:21 +00:00
}
2021-12-23 02:30:51 +00:00
2022-01-06 11:18:13 +00:00
TEST_F(MndTestMnode, 03_Create_Mnode_Invalid_Id) {
2021-12-23 02:30:51 +00:00
{
2022-02-12 06:21:26 +00:00
SMCreateMnodeReq createReq = {0};
createReq.dnodeId = 2;
2021-12-23 02:30:51 +00:00
2022-10-18 09:34:58 +00:00
int32_t contLen = tSerializeSCreateDropMQSNodeReq(NULL, 0, &createReq);
2022-02-12 06:21:26 +00:00
void* pReq = rpcMallocCont(contLen);
2022-10-18 09:34:58 +00:00
tSerializeSCreateDropMQSNodeReq(pReq, contLen, &createReq);
2021-12-23 02:30:51 +00:00
2022-01-05 12:18:56 +00:00
SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_MNODE, pReq, contLen);
ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, TSDB_CODE_MND_DNODE_NOT_EXIST);
2021-12-23 02:30:51 +00:00
}
2021-12-23 09:47:21 +00:00
}
2021-12-23 02:30:51 +00:00
2022-01-06 11:18:13 +00:00
TEST_F(MndTestMnode, 04_Create_Mnode) {
2021-12-23 02:30:51 +00:00
{
2021-12-23 09:47:21 +00:00
// create dnode
2022-02-12 06:06:13 +00:00
SCreateDnodeReq createReq = {0};
strcpy(createReq.fqdn, "localhost");
createReq.port = 9029;
2021-12-23 02:30:51 +00:00
2022-02-12 06:06:13 +00:00
int32_t contLen = tSerializeSCreateDnodeReq(NULL, 0, &createReq);
void* pReq = rpcMallocCont(contLen);
tSerializeSCreateDnodeReq(pReq, contLen, &createReq);
2021-12-23 02:30:51 +00:00
2022-01-05 12:18:56 +00:00
SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DNODE, pReq, contLen);
ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, 0);
2021-12-23 02:30:51 +00:00
2021-12-23 09:47:21 +00:00
taosMsleep(1300);
2022-04-15 02:42:44 +00:00
test.SendShowReq(TSDB_MGMT_TABLE_DNODE, "dnodes", "");
2021-12-23 09:47:21 +00:00
EXPECT_EQ(test.GetShowRows(), 2);
2021-12-23 02:30:51 +00:00
}
{
2021-12-23 09:47:21 +00:00
// create mnode
2022-02-12 06:21:26 +00:00
SMCreateMnodeReq createReq = {0};
createReq.dnodeId = 2;
2021-12-23 02:30:51 +00:00
2022-10-18 09:34:58 +00:00
int32_t contLen = tSerializeSCreateDropMQSNodeReq(NULL, 0, &createReq);
2022-02-12 06:21:26 +00:00
void* pReq = rpcMallocCont(contLen);
2022-10-18 09:34:58 +00:00
tSerializeSCreateDropMQSNodeReq(pReq, contLen, &createReq);
2021-12-23 02:30:51 +00:00
2022-01-05 12:18:56 +00:00
SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_MNODE, pReq, contLen);
ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, 0);
2021-12-23 02:30:51 +00:00
2022-04-15 02:47:00 +00:00
test.SendShowReq(TSDB_MGMT_TABLE_MNODE, "mnodes", "");
2021-12-23 09:47:21 +00:00
EXPECT_EQ(test.GetShowRows(), 2);
}
2021-12-23 11:59:42 +00:00
{
// drop mnode
2022-02-12 06:21:26 +00:00
SMDropMnodeReq dropReq = {0};
dropReq.dnodeId = 2;
2021-12-23 11:59:42 +00:00
2022-10-18 09:34:58 +00:00
int32_t contLen = tSerializeSCreateDropMQSNodeReq(NULL, 0, &dropReq);
2022-02-12 06:21:26 +00:00
void* pReq = rpcMallocCont(contLen);
2022-10-18 09:34:58 +00:00
tSerializeSCreateDropMQSNodeReq(pReq, contLen, &dropReq);
2021-12-23 11:59:42 +00:00
2022-01-05 12:18:56 +00:00
SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_MNODE, pReq, contLen);
ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, 0);
2021-12-23 11:59:42 +00:00
2022-04-15 02:47:00 +00:00
test.SendShowReq(TSDB_MGMT_TABLE_MNODE, "mnodes", "");
2021-12-23 11:59:42 +00:00
EXPECT_EQ(test.GetShowRows(), 1);
}
2022-01-07 11:52:59 +00:00
{
// drop mnode
2022-02-12 06:21:26 +00:00
SMDropMnodeReq dropReq = {0};
dropReq.dnodeId = 2;
2022-01-07 11:52:59 +00:00
2022-10-18 09:34:58 +00:00
int32_t contLen = tSerializeSCreateDropMQSNodeReq(NULL, 0, &dropReq);
2022-02-12 06:21:26 +00:00
void* pReq = rpcMallocCont(contLen);
2022-10-18 09:34:58 +00:00
tSerializeSCreateDropMQSNodeReq(pReq, contLen, &dropReq);
2022-01-07 11:52:59 +00:00
SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_MNODE, pReq, contLen);
ASSERT_NE(pRsp, nullptr);
2022-01-07 12:08:02 +00:00
ASSERT_EQ(pRsp->code, TSDB_CODE_MND_MNODE_NOT_EXIST);
}
}
TEST_F(MndTestMnode, 03_Create_Mnode_Rollback) {
{
// send message first, then dnode2 crash, result is returned, and rollback is started
2022-02-12 06:21:26 +00:00
SMCreateMnodeReq createReq = {0};
createReq.dnodeId = 2;
2022-01-07 12:08:02 +00:00
2022-10-18 09:34:58 +00:00
int32_t contLen = tSerializeSCreateDropMQSNodeReq(NULL, 0, &createReq);
2022-02-12 06:21:26 +00:00
void* pReq = rpcMallocCont(contLen);
2022-10-18 09:34:58 +00:00
tSerializeSCreateDropMQSNodeReq(pReq, contLen, &createReq);
2022-01-07 12:08:02 +00:00
server2.Stop();
SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_MNODE, pReq, contLen);
ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, TSDB_CODE_RPC_NETWORK_UNAVAIL);
}
{
// continue send message, mnode is creating
2022-02-12 06:21:26 +00:00
SMCreateMnodeReq createReq = {0};
createReq.dnodeId = 2;
2022-01-07 12:08:02 +00:00
2022-10-18 09:34:58 +00:00
int32_t contLen = tSerializeSCreateDropMQSNodeReq(NULL, 0, &createReq);
2022-02-12 06:21:26 +00:00
void* pReq = rpcMallocCont(contLen);
2022-10-18 09:34:58 +00:00
tSerializeSCreateDropMQSNodeReq(pReq, contLen, &createReq);
2022-01-07 12:08:02 +00:00
SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_MNODE, pReq, contLen);
ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_CREATING);
}
{
// continue send message, mnode is creating
2022-02-12 06:21:26 +00:00
SMDropMnodeReq dropReq = {0};
dropReq.dnodeId = 2;
2022-01-07 12:08:02 +00:00
2022-10-18 09:34:58 +00:00
int32_t contLen = tSerializeSCreateDropMQSNodeReq(NULL, 0, &dropReq);
2022-02-12 06:21:26 +00:00
void* pReq = rpcMallocCont(contLen);
2022-10-18 09:34:58 +00:00
tSerializeSCreateDropMQSNodeReq(pReq, contLen, &dropReq);
2022-01-07 12:08:02 +00:00
SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_MNODE, pReq, contLen);
ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_CREATING);
}
{
// server start, wait until the rollback finished
2022-05-17 08:01:06 +00:00
// server2.Start();
2022-01-07 12:08:02 +00:00
taosMsleep(1000);
int32_t retry = 0;
int32_t retryMax = 20;
for (retry = 0; retry < retryMax; retry++) {
2022-02-12 06:21:26 +00:00
SMCreateMnodeReq createReq = {0};
createReq.dnodeId = 2;
2022-01-07 12:08:02 +00:00
2022-10-18 09:34:58 +00:00
int32_t contLen = tSerializeSCreateDropMQSNodeReq(NULL, 0, &createReq);
2022-02-12 06:21:26 +00:00
void* pReq = rpcMallocCont(contLen);
2022-10-18 09:34:58 +00:00
tSerializeSCreateDropMQSNodeReq(pReq, contLen, &createReq);
2022-01-07 12:08:02 +00:00
SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_MNODE, pReq, contLen);
ASSERT_NE(pRsp, nullptr);
if (pRsp->code == TSDB_CODE_MND_MNODE_ALREADY_EXIST) break;
taosMsleep(1000);
}
ASSERT_NE(retry, retryMax);
}
}
TEST_F(MndTestMnode, 04_Drop_Mnode_Rollback) {
{
// send message first, then dnode2 crash, result is returned, and rollback is started
2022-02-12 06:21:26 +00:00
SMDropMnodeReq dropReq = {0};
dropReq.dnodeId = 2;
2022-01-07 12:08:02 +00:00
2022-10-18 09:34:58 +00:00
int32_t contLen = tSerializeSCreateDropMQSNodeReq(NULL, 0, &dropReq);
2022-02-12 06:21:26 +00:00
void* pReq = rpcMallocCont(contLen);
2022-10-18 09:34:58 +00:00
tSerializeSCreateDropMQSNodeReq(pReq, contLen, &dropReq);
2022-01-07 12:08:02 +00:00
server2.Stop();
SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_MNODE, pReq, contLen);
ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, TSDB_CODE_RPC_NETWORK_UNAVAIL);
}
{
// continue send message, mnode is dropping
2022-02-12 06:21:26 +00:00
SMCreateMnodeReq createReq = {0};
createReq.dnodeId = 2;
2022-01-07 12:08:02 +00:00
2022-10-18 09:34:58 +00:00
int32_t contLen = tSerializeSCreateDropMQSNodeReq(NULL, 0, &createReq);
2022-02-12 06:21:26 +00:00
void* pReq = rpcMallocCont(contLen);
2022-10-18 09:34:58 +00:00
tSerializeSCreateDropMQSNodeReq(pReq, contLen, &createReq);
2022-01-07 12:08:02 +00:00
SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_MNODE, pReq, contLen);
ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_DROPPING);
}
{
// continue send message, mnode is dropping
2022-02-12 06:21:26 +00:00
SMDropMnodeReq dropReq = {0};
dropReq.dnodeId = 2;
2022-01-07 12:08:02 +00:00
2022-10-18 09:34:58 +00:00
int32_t contLen = tSerializeSCreateDropMQSNodeReq(NULL, 0, &dropReq);
2022-02-12 06:21:26 +00:00
void* pReq = rpcMallocCont(contLen);
2022-10-18 09:34:58 +00:00
tSerializeSCreateDropMQSNodeReq(pReq, contLen, &dropReq);
2022-01-07 12:08:02 +00:00
SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_MNODE, pReq, contLen);
ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_DROPPING);
}
{
// server start, wait until the rollback finished
2022-05-17 08:01:06 +00:00
// server2.Start();
2022-01-07 12:08:02 +00:00
taosMsleep(1000);
int32_t retry = 0;
int32_t retryMax = 20;
for (retry = 0; retry < retryMax; retry++) {
2022-02-12 06:21:26 +00:00
SMCreateMnodeReq createReq = {0};
createReq.dnodeId = 2;
2022-01-07 12:08:02 +00:00
2022-10-18 09:34:58 +00:00
int32_t contLen = tSerializeSCreateDropMQSNodeReq(NULL, 0, &createReq);
2022-02-12 06:21:26 +00:00
void* pReq = rpcMallocCont(contLen);
2022-10-18 09:34:58 +00:00
tSerializeSCreateDropMQSNodeReq(pReq, contLen, &createReq);
2022-01-07 12:08:02 +00:00
SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_MNODE, pReq, contLen);
ASSERT_NE(pRsp, nullptr);
if (pRsp->code == 0) break;
taosMsleep(1000);
}
ASSERT_NE(retry, retryMax);
2022-01-07 11:52:59 +00:00
}
}
#define SLOW_LOG_TYPE_NULL 0x0
#define SLOW_LOG_TYPE_QUERY 0x1
#define SLOW_LOG_TYPE_INSERT 0x2
#define SLOW_LOG_TYPE_OTHERS 0x4
#define SLOW_LOG_TYPE_ALL 0x7
void getSlowLogScopeString(int32_t scope, char* result){
if(scope == SLOW_LOG_TYPE_NULL) {
strcat(result, "NONE");
return;
}
while(scope > 0){
if(scope & SLOW_LOG_TYPE_QUERY) {
strcat(result, "QUERY");
scope &= ~SLOW_LOG_TYPE_QUERY;
} else if(scope & SLOW_LOG_TYPE_INSERT) {
strcat(result, "INSERT");
scope &= ~SLOW_LOG_TYPE_INSERT;
} else if(scope & SLOW_LOG_TYPE_OTHERS) {
strcat(result, "OTHERS");
scope &= ~SLOW_LOG_TYPE_OTHERS;
} else{
printf("invalid slow log scope:%d", scope);
return;
}
if(scope > 0) {
strcat(result, "|");
}
}
}
// Define test cases
TEST_F(MndTestMnode, ScopeIsNull) {
// Arrange
char result[256] = {0};
// Act
getSlowLogScopeString(SLOW_LOG_TYPE_NULL, result);
// Assert
EXPECT_STREQ(result, "NONE");
}
TEST_F(MndTestMnode, ScopeIsQuery) {
// Arrange
char result[256] = {0};
// Act
getSlowLogScopeString(SLOW_LOG_TYPE_QUERY, result);
// Assert
EXPECT_STREQ(result, "QUERY");
}
TEST_F(MndTestMnode, ScopeIsInsert) {
// Arrange
char result[256] = {0};
// Act
getSlowLogScopeString(SLOW_LOG_TYPE_INSERT, result);
// Assert
EXPECT_STREQ(result, "INSERT");
}
TEST_F(MndTestMnode, ScopeIsOthers) {
// Arrange
char result[256] = {0};
// Act
getSlowLogScopeString(SLOW_LOG_TYPE_OTHERS, result);
// Assert
EXPECT_STREQ(result, "OTHERS");
}
TEST_F(MndTestMnode, ScopeIsMixed) {
// Arrange
char result[256] = {0};
// Act
getSlowLogScopeString(SLOW_LOG_TYPE_OTHERS|SLOW_LOG_TYPE_INSERT, result);
// Assert
EXPECT_STREQ(result, "INSERT|OTHERS");
}
TEST_F(MndTestMnode, ScopeIsMixed1) {
// Arrange
char result[256] = {0};
// Act
getSlowLogScopeString(SLOW_LOG_TYPE_ALL, result);
// Assert
EXPECT_STREQ(result, "QUERY|INSERT|OTHERS");
}
TEST_F(MndTestMnode, ScopeIsInvalid) {
// Arrange
char result[256] = {0};
// Act
getSlowLogScopeString(0xF000, result);
// Assert
EXPECT_STREQ(result, ""); // Expect an empty string since the scope is invalid
// You may also want to check if the error message is correctly logged
2022-01-07 11:52:59 +00:00
}