bnode.cpp 7.8 KB
Newer Older
S
Shengliang Guan 已提交
1
/**
S
Shengliang Guan 已提交
2
 * @file bnode.cpp
S
Shengliang Guan 已提交
3
 * @author slguan (slguan@taosdata.com)
S
Shengliang Guan 已提交
4 5 6
 * @brief MNODE module bnode tests
 * @version 1.0
 * @date 2022-01-05
S
Shengliang Guan 已提交
7
 *
S
Shengliang Guan 已提交
8
 * @copyright Copyright (c) 2022
S
Shengliang Guan 已提交
9 10 11
 *
 */

S
Shengliang Guan 已提交
12
#include "sut.h"
S
Shengliang Guan 已提交
13

S
Shengliang Guan 已提交
14
class MndTestBnode : public ::testing::Test {
S
Shengliang Guan 已提交
15 16 17 18 19 20
 public:
  void SetUp() override {}
  void TearDown() override {}

 public:
  static void SetUpTestSuite() {
S
Shengliang Guan 已提交
21
    test.Init("/tmp/mnode_test_bnode1", 9018);
S
Shengliang Guan 已提交
22
    const char* fqdn = "localhost";
S
Shengliang Guan 已提交
23
    const char* firstEp = "localhost:9018";
S
Shengliang Guan 已提交
24

S
Shengliang Guan 已提交
25
    server2.Start("/tmp/mnode_test_bnode2", fqdn, 9019, firstEp);
S
Shengliang Guan 已提交
26 27 28 29 30 31 32 33 34 35 36 37
    taosMsleep(300);
  }

  static void TearDownTestSuite() {
    server2.Stop();
    test.Cleanup();
  }

  static Testbase   test;
  static TestServer server2;
};

S
Shengliang Guan 已提交
38 39
Testbase   MndTestBnode::test;
TestServer MndTestBnode::server2;
S
Shengliang Guan 已提交
40

S
Shengliang Guan 已提交
41
TEST_F(MndTestBnode, 01_Show_Bnode) {
S
Shengliang Guan 已提交
42 43 44 45 46 47 48 49 50 51 52
  test.SendShowMetaMsg(TSDB_MGMT_TABLE_BNODE, "");
  CHECK_META("show bnodes", 3);

  CHECK_SCHEMA(0, TSDB_DATA_TYPE_SMALLINT, 2, "id");
  CHECK_SCHEMA(1, TSDB_DATA_TYPE_BINARY, TSDB_EP_LEN + VARSTR_HEADER_SIZE, "endpoint");
  CHECK_SCHEMA(2, TSDB_DATA_TYPE_TIMESTAMP, 8, "create_time");

  test.SendShowRetrieveMsg();
  EXPECT_EQ(test.GetShowRows(), 0);
}

53 54 55 56 57 58 59 60 61 62 63 64
TEST_F(MndTestBnode, 02_Create_Bnode) {
  {
    int32_t contLen = sizeof(SMCreateBnodeReq);

    SMCreateBnodeReq* pReq = (SMCreateBnodeReq*)rpcMallocCont(contLen);
    pReq->dnodeId = htonl(2);

    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, TSDB_CODE_MND_DNODE_NOT_EXIST);
  }

S
Shengliang Guan 已提交
65
  {
S
Shengliang Guan 已提交
66
    int32_t contLen = sizeof(SMCreateBnodeReq);
S
Shengliang Guan 已提交
67

S
Shengliang Guan 已提交
68
    SMCreateBnodeReq* pReq = (SMCreateBnodeReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
69 70 71 72 73 74 75 76 77 78 79 80
    pReq->dnodeId = htonl(1);

    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);

    test.SendShowMetaMsg(TSDB_MGMT_TABLE_BNODE, "");
    CHECK_META("show bnodes", 3);
    test.SendShowRetrieveMsg();
    EXPECT_EQ(test.GetShowRows(), 1);

    CheckInt16(1);
S
Shengliang Guan 已提交
81
    CheckBinary("localhost:9018", TSDB_EP_LEN);
S
Shengliang Guan 已提交
82 83 84 85
    CheckTimestamp();
  }

  {
S
Shengliang Guan 已提交
86
    int32_t contLen = sizeof(SMCreateBnodeReq);
S
Shengliang Guan 已提交
87

S
Shengliang Guan 已提交
88
    SMCreateBnodeReq* pReq = (SMCreateBnodeReq*)rpcMallocCont(contLen);
89
    pReq->dnodeId = htonl(1);
S
Shengliang Guan 已提交
90 91 92

    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
93
    ASSERT_EQ(pMsg->code, TSDB_CODE_MND_BNODE_ALREADY_EXIST);
S
Shengliang Guan 已提交
94 95 96
  }
}

97
TEST_F(MndTestBnode, 03_Drop_Bnode) {
S
Shengliang Guan 已提交
98 99 100 101 102
  {
    int32_t contLen = sizeof(SCreateDnodeMsg);

    SCreateDnodeMsg* pReq = (SCreateDnodeMsg*)rpcMallocCont(contLen);
    strcpy(pReq->fqdn, "localhost");
S
Shengliang Guan 已提交
103
    pReq->port = htonl(9019);
S
Shengliang Guan 已提交
104 105 106 107 108 109 110 111 112 113 114 115

    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_DNODE, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);

    taosMsleep(1300);
    test.SendShowMetaMsg(TSDB_MGMT_TABLE_DNODE, "");
    test.SendShowRetrieveMsg();
    EXPECT_EQ(test.GetShowRows(), 2);
  }

  {
S
Shengliang Guan 已提交
116
    int32_t contLen = sizeof(SMCreateBnodeReq);
S
Shengliang Guan 已提交
117

S
Shengliang Guan 已提交
118
    SMCreateBnodeReq* pReq = (SMCreateBnodeReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
119 120 121 122 123 124 125 126 127 128 129 130
    pReq->dnodeId = htonl(2);

    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);

    test.SendShowMetaMsg(TSDB_MGMT_TABLE_BNODE, "");
    test.SendShowRetrieveMsg();
    EXPECT_EQ(test.GetShowRows(), 2);

    CheckInt16(1);
    CheckInt16(2);
S
Shengliang Guan 已提交
131 132
    CheckBinary("localhost:9018", TSDB_EP_LEN);
    CheckBinary("localhost:9019", TSDB_EP_LEN);
S
Shengliang Guan 已提交
133 134 135 136 137
    CheckTimestamp();
    CheckTimestamp();
  }

  {
S
Shengliang Guan 已提交
138
    int32_t contLen = sizeof(SMDropBnodeReq);
S
Shengliang Guan 已提交
139

S
Shengliang Guan 已提交
140
    SMDropBnodeReq* pReq = (SMDropBnodeReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
141 142 143 144 145 146 147 148 149 150 151
    pReq->dnodeId = htonl(2);

    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_DROP_BNODE, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);

    test.SendShowMetaMsg(TSDB_MGMT_TABLE_BNODE, "");
    test.SendShowRetrieveMsg();
    EXPECT_EQ(test.GetShowRows(), 1);

    CheckInt16(1);
S
Shengliang Guan 已提交
152
    CheckBinary("localhost:9018", TSDB_EP_LEN);
S
Shengliang Guan 已提交
153 154
    CheckTimestamp();
  }
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

  {
    int32_t contLen = sizeof(SMDropBnodeReq);

    SMDropBnodeReq* pReq = (SMDropBnodeReq*)rpcMallocCont(contLen);
    pReq->dnodeId = htonl(2);

    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_DROP_BNODE, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, TSDB_CODE_MND_BNODE_NOT_EXIST);
  }
}

TEST_F(MndTestBnode, 03_Create_Bnode_Rollback) {
  {
    // send message first, then dnode2 crash, result is returned, and rollback is started
    int32_t contLen = sizeof(SMCreateBnodeReq);

    SMCreateBnodeReq* pReq = (SMCreateBnodeReq*)rpcMallocCont(contLen);
    pReq->dnodeId = htonl(2);

    server2.Stop();
    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, TSDB_CODE_RPC_NETWORK_UNAVAIL);
  }

  {
    // continue send message, bnode is creating
    int32_t contLen = sizeof(SMCreateBnodeReq);

    SMCreateBnodeReq* pReq = (SMCreateBnodeReq*)rpcMallocCont(contLen);
    pReq->dnodeId = htonl(2);

    server2.Stop();
    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, TSDB_CODE_SDB_OBJ_CREATING);
  }

  {
    // continue send message, bnode is creating
    int32_t contLen = sizeof(SMDropBnodeReq);

    SMDropBnodeReq* pReq = (SMDropBnodeReq*)rpcMallocCont(contLen);
    pReq->dnodeId = htonl(2);

    server2.Stop();
    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_DROP_BNODE, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, TSDB_CODE_SDB_OBJ_CREATING);
  }

  {
    // server start, wait until the rollback finished
    server2.DoStart();
    taosMsleep(1000);

    int32_t retry = 0;
    int32_t retryMax = 10;

    for (retry = 0; retry < retryMax; retry++) {
      int32_t contLen = sizeof(SMCreateBnodeReq);

      SMCreateBnodeReq* pReq = (SMCreateBnodeReq*)rpcMallocCont(contLen);
      pReq->dnodeId = htonl(2);

      SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_BNODE, pReq, contLen);
      ASSERT_NE(pMsg, nullptr);
      if (pMsg->code == 0) break;
      taosMsleep(1000);
    }

    ASSERT_NE(retry, retryMax);
  }
}

TEST_F(MndTestBnode, 04_Drop_Bnode_Rollback) {
  {
    // send message first, then dnode2 crash, result is returned, and rollback is started
    int32_t contLen = sizeof(SMDropBnodeReq);

    SMDropBnodeReq* pReq = (SMDropBnodeReq*)rpcMallocCont(contLen);
    pReq->dnodeId = htonl(2);

    server2.Stop();
    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_DROP_BNODE, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, TSDB_CODE_RPC_NETWORK_UNAVAIL);
  }

  {
    // continue send message, bnode is dropping
    int32_t contLen = sizeof(SMCreateBnodeReq);

    SMCreateBnodeReq* pReq = (SMCreateBnodeReq*)rpcMallocCont(contLen);
    pReq->dnodeId = htonl(2);

    server2.Stop();
    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, TSDB_CODE_SDB_OBJ_DROPPING);
  }

  {
    // continue send message, bnode is dropping
    int32_t contLen = sizeof(SMDropBnodeReq);

    SMDropBnodeReq* pReq = (SMDropBnodeReq*)rpcMallocCont(contLen);
    pReq->dnodeId = htonl(2);

    server2.Stop();
    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_DROP_BNODE, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, TSDB_CODE_SDB_OBJ_DROPPING);
  }

  {
    // server start, wait until the rollback finished
    server2.DoStart();
    taosMsleep(1000);

    int32_t retry = 0;
    int32_t retryMax = 10;

    for (retry = 0; retry < retryMax; retry++) {
      int32_t contLen = sizeof(SMCreateBnodeReq);

      SMCreateBnodeReq* pReq = (SMCreateBnodeReq*)rpcMallocCont(contLen);
      pReq->dnodeId = htonl(2);

      SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_BNODE, pReq, contLen);
      ASSERT_NE(pMsg, nullptr);
      if (pMsg->code == 0) break;
      taosMsleep(1000);
    }

    ASSERT_NE(retry, retryMax);
  }
S
Shengliang Guan 已提交
294
}