bnode.cpp 9.1 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
TEST_F(MndTestBnode, 02_Create_Bnode) {
  {
S
Shengliang Guan 已提交
55 56
    SMCreateBnodeReq createReq = {0};
    createReq.dnodeId = 2;
57

S
Shengliang Guan 已提交
58 59 60
    int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq);
61

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

S
Shengliang Guan 已提交
67
  {
S
Shengliang Guan 已提交
68 69
    SMCreateBnodeReq createReq = {0};
    createReq.dnodeId = 1;
S
Shengliang Guan 已提交
70

S
Shengliang Guan 已提交
71 72 73
    int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
74

S
Shengliang Guan 已提交
75 76 77
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
78

S
Shengliang Guan 已提交
79
    test.SendShowMetaReq(TSDB_MGMT_TABLE_BNODE, "");
S
Shengliang Guan 已提交
80
    CHECK_META("show bnodes", 3);
S
Shengliang Guan 已提交
81
    test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
82 83 84
    EXPECT_EQ(test.GetShowRows(), 1);

    CheckInt16(1);
S
Shengliang Guan 已提交
85
    CheckBinary("localhost:9018", TSDB_EP_LEN);
S
Shengliang Guan 已提交
86 87 88 89
    CheckTimestamp();
  }

  {
S
Shengliang Guan 已提交
90 91
    SMCreateBnodeReq createReq = {0};
    createReq.dnodeId = 1;
S
Shengliang Guan 已提交
92

S
Shengliang Guan 已提交
93 94 95
    int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
96

S
Shengliang Guan 已提交
97 98 99
    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 已提交
100 101 102
  }
}

103
TEST_F(MndTestBnode, 03_Drop_Bnode) {
S
Shengliang Guan 已提交
104
  {
S
Shengliang Guan 已提交
105 106 107
    SCreateDnodeReq createReq = {0};
    strcpy(createReq.fqdn, "localhost");
    createReq.port = 9019;
S
Shengliang Guan 已提交
108

S
Shengliang Guan 已提交
109 110 111
    int32_t contLen = tSerializeSCreateDnodeReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateDnodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
112

S
Shengliang Guan 已提交
113 114 115
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
116 117

    taosMsleep(1300);
S
Shengliang Guan 已提交
118 119
    test.SendShowMetaReq(TSDB_MGMT_TABLE_DNODE, "");
    test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
120 121 122 123
    EXPECT_EQ(test.GetShowRows(), 2);
  }

  {
S
Shengliang Guan 已提交
124 125
    SMCreateBnodeReq createReq = {0};
    createReq.dnodeId = 2;
S
Shengliang Guan 已提交
126

S
Shengliang Guan 已提交
127 128 129
    int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
130

S
Shengliang Guan 已提交
131 132 133
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
134

S
Shengliang Guan 已提交
135 136
    test.SendShowMetaReq(TSDB_MGMT_TABLE_BNODE, "");
    test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
137 138 139 140
    EXPECT_EQ(test.GetShowRows(), 2);

    CheckInt16(1);
    CheckInt16(2);
S
Shengliang Guan 已提交
141 142
    CheckBinary("localhost:9018", TSDB_EP_LEN);
    CheckBinary("localhost:9019", TSDB_EP_LEN);
S
Shengliang Guan 已提交
143 144 145 146 147
    CheckTimestamp();
    CheckTimestamp();
  }

  {
S
Shengliang Guan 已提交
148 149
    SMDropBnodeReq dropReq = {0};
    dropReq.dnodeId = 2;
S
Shengliang Guan 已提交
150

S
Shengliang Guan 已提交
151 152 153
    int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
154

S
Shengliang Guan 已提交
155 156 157
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
158

S
Shengliang Guan 已提交
159 160
    test.SendShowMetaReq(TSDB_MGMT_TABLE_BNODE, "");
    test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
161 162 163
    EXPECT_EQ(test.GetShowRows(), 1);

    CheckInt16(1);
S
Shengliang Guan 已提交
164
    CheckBinary("localhost:9018", TSDB_EP_LEN);
S
Shengliang Guan 已提交
165 166
    CheckTimestamp();
  }
167 168

  {
S
Shengliang Guan 已提交
169 170
    SMDropBnodeReq dropReq = {0};
    dropReq.dnodeId = 2;
171

S
Shengliang Guan 已提交
172 173 174
    int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq);
175

S
Shengliang Guan 已提交
176 177 178
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_BNODE_NOT_EXIST);
179 180 181 182 183 184
  }
}

TEST_F(MndTestBnode, 03_Create_Bnode_Rollback) {
  {
    // send message first, then dnode2 crash, result is returned, and rollback is started
S
Shengliang Guan 已提交
185 186
    SMCreateBnodeReq createReq = {0};
    createReq.dnodeId = 2;
187

S
Shengliang Guan 已提交
188 189 190
    int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq);
191 192

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

  {
    // continue send message, bnode is creating
S
Shengliang Guan 已提交
200 201
    SMCreateBnodeReq createReq = {0};
    createReq.dnodeId = 2;
202

S
Shengliang Guan 已提交
203 204 205
    int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq);
206

S
Shengliang Guan 已提交
207 208 209
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_CREATING);
210 211 212 213
  }

  {
    // continue send message, bnode is creating
S
Shengliang Guan 已提交
214 215
    SMDropBnodeReq dropReq = {0};
    dropReq.dnodeId = 2;
216

S
Shengliang Guan 已提交
217 218 219
    int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq);
220

S
Shengliang Guan 已提交
221 222 223
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_CREATING);
224 225 226 227 228 229 230 231
  }

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

    int32_t retry = 0;
232
    int32_t retryMax = 20;
233 234

    for (retry = 0; retry < retryMax; retry++) {
S
Shengliang Guan 已提交
235 236
      SMCreateBnodeReq createReq = {0};
      createReq.dnodeId = 2;
237

S
Shengliang Guan 已提交
238 239 240
      int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq);
      void*   pReq = rpcMallocCont(contLen);
      tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq);
241

S
Shengliang Guan 已提交
242 243 244
      SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_BNODE, pReq, contLen);
      ASSERT_NE(pRsp, nullptr);
      if (pRsp->code == 0) break;
245 246 247 248 249 250 251 252 253 254
      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
S
Shengliang Guan 已提交
255 256
    SMDropBnodeReq dropReq = {0};
    dropReq.dnodeId = 2;
257

S
Shengliang Guan 已提交
258 259 260
    int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq);
261 262

    server2.Stop();
S
Shengliang Guan 已提交
263 264 265
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_RPC_NETWORK_UNAVAIL);
266 267 268 269
  }

  {
    // continue send message, bnode is dropping
S
Shengliang Guan 已提交
270 271
    SMCreateBnodeReq createReq = {0};
    createReq.dnodeId = 2;
272

S
Shengliang Guan 已提交
273 274 275
    int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq);
276

S
Shengliang Guan 已提交
277 278 279
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_DROPPING);
280 281 282 283
  }

  {
    // continue send message, bnode is dropping
S
Shengliang Guan 已提交
284 285
    SMDropBnodeReq dropReq = {0};
    dropReq.dnodeId = 2;
286

S
Shengliang Guan 已提交
287 288 289
    int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq);
290

S
Shengliang Guan 已提交
291 292 293
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_DROPPING);
294 295 296 297 298 299 300 301
  }

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

    int32_t retry = 0;
302
    int32_t retryMax = 20;
303 304

    for (retry = 0; retry < retryMax; retry++) {
S
Shengliang Guan 已提交
305 306
      SMCreateBnodeReq createReq = {0};
      createReq.dnodeId = 2;
307

S
Shengliang Guan 已提交
308 309 310
      int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq);
      void*   pReq = rpcMallocCont(contLen);
      tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq);
311

S
Shengliang Guan 已提交
312 313 314
      SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_BNODE, pReq, contLen);
      ASSERT_NE(pRsp, nullptr);
      if (pRsp->code == 0) break;
315 316 317 318 319
      taosMsleep(1000);
    }

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