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
  test.SendShowMetaReq(TSDB_MGMT_TABLE_BNODE, "");
S
Shengliang Guan 已提交
43 44 45 46 47 48
  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");

S
Shengliang Guan 已提交
49
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
50 51 52
  EXPECT_EQ(test.GetShowRows(), 0);
}

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

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

S
Shengliang Guan 已提交
60 61 62
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_DNODE_NOT_EXIST);
63 64
  }

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
    pReq->dnodeId = htonl(1);

S
Shengliang Guan 已提交
71 72 73
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
74

S
Shengliang Guan 已提交
75
    test.SendShowMetaReq(TSDB_MGMT_TABLE_BNODE, "");
S
Shengliang Guan 已提交
76
    CHECK_META("show bnodes", 3);
S
Shengliang Guan 已提交
77
    test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
78 79 80
    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

S
Shengliang Guan 已提交
91 92 93
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->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

S
Shengliang Guan 已提交
105 106 107
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
108 109

    taosMsleep(1300);
S
Shengliang Guan 已提交
110 111
    test.SendShowMetaReq(TSDB_MGMT_TABLE_DNODE, "");
    test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
112 113 114 115
    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
    pReq->dnodeId = htonl(2);

S
Shengliang Guan 已提交
121 122 123
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
124

S
Shengliang Guan 已提交
125 126
    test.SendShowMetaReq(TSDB_MGMT_TABLE_BNODE, "");
    test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
127 128 129 130
    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
    pReq->dnodeId = htonl(2);

S
Shengliang Guan 已提交
143 144 145
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
146

S
Shengliang Guan 已提交
147 148
    test.SendShowMetaReq(TSDB_MGMT_TABLE_BNODE, "");
    test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
149 150 151
    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

  {
    int32_t contLen = sizeof(SMDropBnodeReq);

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

S
Shengliang Guan 已提交
162 163 164
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_BNODE_NOT_EXIST);
165 166 167 168 169 170 171 172 173 174 175 176
  }
}

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();
S
Shengliang Guan 已提交
177 178 179
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_RPC_NETWORK_UNAVAIL);
180 181 182 183 184 185 186 187 188 189
  }

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

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

    server2.Stop();
S
Shengliang Guan 已提交
190 191 192
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_CREATING);
193 194 195 196 197 198 199 200 201 202
  }

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

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

    server2.Stop();
S
Shengliang Guan 已提交
203 204 205
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_CREATING);
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  }

  {
    // 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);

S
Shengliang Guan 已提交
222 223 224
      SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_BNODE, pReq, contLen);
      ASSERT_NE(pRsp, nullptr);
      if (pRsp->code == 0) break;
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
      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();
S
Shengliang Guan 已提交
241 242 243
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_RPC_NETWORK_UNAVAIL);
244 245 246 247 248 249 250 251 252 253
  }

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

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

    server2.Stop();
S
Shengliang Guan 已提交
254 255 256
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_DROPPING);
257 258 259 260 261 262 263 264 265 266
  }

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

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

    server2.Stop();
S
Shengliang Guan 已提交
267 268 269
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_DROPPING);
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
  }

  {
    // 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);

S
Shengliang Guan 已提交
286 287 288
      SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_BNODE, pReq, contLen);
      ASSERT_NE(pRsp, nullptr);
      if (pRsp->code == 0) break;
289 290 291 292 293
      taosMsleep(1000);
    }

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