mbnode.cpp 8.4 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) {
42
  test.SendShowReq(TSDB_MGMT_TABLE_BNODE, "bnodes", "");
S
Shengliang Guan 已提交
43 44
  EXPECT_EQ(test.GetShowRows(), 0);
}
S
shm  
Shengliang Guan 已提交
45

46 47
TEST_F(MndTestBnode, 02_Create_Bnode) {
  {
S
Shengliang Guan 已提交
48 49
    SMCreateBnodeReq createReq = {0};
    createReq.dnodeId = 2;
50

51
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
52
    void*   pReq = rpcMallocCont(contLen);
53
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
54

S
Shengliang Guan 已提交
55 56 57
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_DNODE_NOT_EXIST);
58 59
  }

S
Shengliang Guan 已提交
60
  {
S
Shengliang Guan 已提交
61 62
    SMCreateBnodeReq createReq = {0};
    createReq.dnodeId = 1;
S
Shengliang Guan 已提交
63

64
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
65
    void*   pReq = rpcMallocCont(contLen);
66
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
67

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

72
    test.SendShowReq(TSDB_MGMT_TABLE_BNODE, "bnodes", "");
S
Shengliang Guan 已提交
73 74 75 76
    EXPECT_EQ(test.GetShowRows(), 1);
  }

  {
S
Shengliang Guan 已提交
77 78
    SMCreateBnodeReq createReq = {0};
    createReq.dnodeId = 1;
S
Shengliang Guan 已提交
79

80
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
81
    void*   pReq = rpcMallocCont(contLen);
82
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
83

S
Shengliang Guan 已提交
84 85 86
    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 已提交
87 88 89
  }
}

90
TEST_F(MndTestBnode, 03_Drop_Bnode) {
S
Shengliang Guan 已提交
91
  {
S
Shengliang Guan 已提交
92 93 94
    SCreateDnodeReq createReq = {0};
    strcpy(createReq.fqdn, "localhost");
    createReq.port = 9019;
S
Shengliang Guan 已提交
95

S
Shengliang Guan 已提交
96 97 98
    int32_t contLen = tSerializeSCreateDnodeReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateDnodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
99

S
Shengliang Guan 已提交
100 101 102
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
103 104

    taosMsleep(1300);
105
    test.SendShowReq(TSDB_MGMT_TABLE_DNODE, "dnodes", "");
S
Shengliang Guan 已提交
106 107 108 109
    EXPECT_EQ(test.GetShowRows(), 2);
  }

  {
S
Shengliang Guan 已提交
110 111
    SMCreateBnodeReq createReq = {0};
    createReq.dnodeId = 2;
S
Shengliang Guan 已提交
112

113
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
114
    void*   pReq = rpcMallocCont(contLen);
115
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
116

S
Shengliang Guan 已提交
117 118 119
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
120

121
    test.SendShowReq(TSDB_MGMT_TABLE_BNODE, "bnodes", "");
S
Shengliang Guan 已提交
122 123 124 125
    EXPECT_EQ(test.GetShowRows(), 2);
  }

  {
S
Shengliang Guan 已提交
126 127
    SMDropBnodeReq dropReq = {0};
    dropReq.dnodeId = 2;
S
Shengliang Guan 已提交
128

129
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &dropReq);
S
Shengliang Guan 已提交
130
    void*   pReq = rpcMallocCont(contLen);
131
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
132

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

137
    test.SendShowReq(TSDB_MGMT_TABLE_BNODE, "bnodes", "");
S
Shengliang Guan 已提交
138 139
    EXPECT_EQ(test.GetShowRows(), 1);
  }
140 141

  {
S
Shengliang Guan 已提交
142 143
    SMDropBnodeReq dropReq = {0};
    dropReq.dnodeId = 2;
144

145
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &dropReq);
S
Shengliang Guan 已提交
146
    void*   pReq = rpcMallocCont(contLen);
147
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &dropReq);
148

S
Shengliang Guan 已提交
149 150 151
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_BNODE_NOT_EXIST);
152 153 154 155 156 157
  }
}

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

161
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
162
    void*   pReq = rpcMallocCont(contLen);
163
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
164 165

    server2.Stop();
S
Shengliang Guan 已提交
166 167 168
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_RPC_NETWORK_UNAVAIL);
169 170 171 172
  }

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

176
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
177
    void*   pReq = rpcMallocCont(contLen);
178
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
179

S
Shengliang Guan 已提交
180 181 182
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_CREATING);
183 184 185 186
  }

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

190
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &dropReq);
S
Shengliang Guan 已提交
191
    void*   pReq = rpcMallocCont(contLen);
192
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &dropReq);
193

S
Shengliang Guan 已提交
194 195 196
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_CREATING);
197 198 199 200 201 202 203 204
  }

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

    int32_t retry = 0;
205
    int32_t retryMax = 20;
206 207

    for (retry = 0; retry < retryMax; retry++) {
S
Shengliang Guan 已提交
208 209
      SMCreateBnodeReq createReq = {0};
      createReq.dnodeId = 2;
210

211
      int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
212
      void*   pReq = rpcMallocCont(contLen);
213
      tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
214

S
Shengliang Guan 已提交
215 216 217
      SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_BNODE, pReq, contLen);
      ASSERT_NE(pRsp, nullptr);
      if (pRsp->code == 0) break;
218 219 220 221 222 223 224 225 226 227
      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 已提交
228 229
    SMDropBnodeReq dropReq = {0};
    dropReq.dnodeId = 2;
230

231
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &dropReq);
S
Shengliang Guan 已提交
232
    void*   pReq = rpcMallocCont(contLen);
233
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &dropReq);
234 235

    server2.Stop();
S
Shengliang Guan 已提交
236 237 238
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_RPC_NETWORK_UNAVAIL);
239 240 241 242
  }

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

246
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
247
    void*   pReq = rpcMallocCont(contLen);
248
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
249

S
Shengliang Guan 已提交
250 251 252
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_DROPPING);
253 254 255 256
  }

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

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

S
Shengliang Guan 已提交
264 265 266
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_BNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_DROPPING);
267 268 269 270 271 272 273 274
  }

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

    int32_t retry = 0;
275
    int32_t retryMax = 20;
276 277

    for (retry = 0; retry < retryMax; retry++) {
S
Shengliang Guan 已提交
278 279
      SMCreateBnodeReq createReq = {0};
      createReq.dnodeId = 2;
280

281
      int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
282
      void*   pReq = rpcMallocCont(contLen);
283
      tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
284

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

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