qnode.cpp 8.5 KB
Newer Older
S
Shengliang Guan 已提交
1
/**
S
Shengliang Guan 已提交
2
 * @file qnode.cpp
S
Shengliang Guan 已提交
3
 * @author slguan (slguan@taosdata.com)
S
Shengliang Guan 已提交
4 5 6
 * @brief MNODE module qnode 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 MndTestQnode : public ::testing::Test {
S
Shengliang Guan 已提交
15 16 17 18 19 20
 public:
  void SetUp() override {}
  void TearDown() override {}

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

25
    // server2.Start(TD_TMP_DIR_PATH "mnode_test_qnode2", fqdn, 9015, 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   MndTestQnode::test;
TestServer MndTestQnode::server2;
S
Shengliang Guan 已提交
40

S
Shengliang Guan 已提交
41
TEST_F(MndTestQnode, 01_Show_Qnode) {
S
Shengliang Guan 已提交
42
  test.SendShowReq(TSDB_MGMT_TABLE_QNODE, "qnodes", "");
S
Shengliang Guan 已提交
43 44
  EXPECT_EQ(test.GetShowRows(), 0);
}
S
Shengliang Guan 已提交
45

46 47
TEST_F(MndTestQnode, 02_Create_Qnode) {
  {
S
Shengliang Guan 已提交
48 49
    SMCreateQnodeReq 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_QNODE, 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
    SMCreateQnodeReq 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_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
71

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

S
Shengliang Guan 已提交
76
  {
S
Shengliang Guan 已提交
77 78
    SMCreateQnodeReq 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_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_QNODE_ALREADY_EXIST);
S
Shengliang Guan 已提交
87 88 89
  }
}

90
TEST_F(MndTestQnode, 03_Drop_Qnode) {
S
Shengliang Guan 已提交
91
  {
S
Shengliang Guan 已提交
92 93 94
    SCreateDnodeReq createReq = {0};
    strcpy(createReq.fqdn, "localhost");
    createReq.port = 9015;
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);
S
Shengliang Guan 已提交
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
    SMCreateQnodeReq 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_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
120

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

  {
S
Shengliang Guan 已提交
126 127
    SMDropQnodeReq 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_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
136

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

  {
S
Shengliang Guan 已提交
142 143
    SMDropQnodeReq 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_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_QNODE_NOT_EXIST);
152 153 154 155 156 157
  }
}

TEST_F(MndTestQnode, 03_Create_Qnode_Rollback) {
  {
    // send message first, then dnode2 crash, result is returned, and rollback is started
S
Shengliang Guan 已提交
158 159
    SMCreateQnodeReq 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();
dengyihao's avatar
fix bug  
dengyihao 已提交
166 167 168
    taosMsleep(1000);
    // test.ClientRestart();

S
Shengliang Guan 已提交
169 170 171
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_RPC_NETWORK_UNAVAIL);
172 173 174 175
  }

  {
    // continue send message, qnode is creating
S
Shengliang Guan 已提交
176 177
    SMCreateQnodeReq createReq = {0};
    createReq.dnodeId = 2;
178

179
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
180
    void*   pReq = rpcMallocCont(contLen);
181
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
182

S
Shengliang Guan 已提交
183 184 185
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_CREATING);
186 187 188 189
  }

  {
    // continue send message, qnode is creating
S
Shengliang Guan 已提交
190 191
    SMDropQnodeReq dropReq = {0};
    dropReq.dnodeId = 2;
192

193
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &dropReq);
S
Shengliang Guan 已提交
194
    void*   pReq = rpcMallocCont(contLen);
195
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &dropReq);
196

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

  {
    // server start, wait until the rollback finished
204
    server2.Start();
dengyihao's avatar
fix bug  
dengyihao 已提交
205
    test.ClientRestart();
206 207 208
    taosMsleep(1000);

    int32_t retry = 0;
209
    int32_t retryMax = 20;
210 211

    for (retry = 0; retry < retryMax; retry++) {
S
Shengliang Guan 已提交
212 213
      SMCreateQnodeReq createReq = {0};
      createReq.dnodeId = 2;
214

215
      int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
216
      void*   pReq = rpcMallocCont(contLen);
217
      tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
218

S
Shengliang Guan 已提交
219 220 221
      SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_QNODE, pReq, contLen);
      ASSERT_NE(pRsp, nullptr);
      if (pRsp->code == 0) break;
222 223 224 225 226 227 228 229 230
      taosMsleep(1000);
    }

    ASSERT_NE(retry, retryMax);
  }
}
TEST_F(MndTestQnode, 04_Drop_Qnode_Rollback) {
  {
    // send message first, then dnode2 crash, result is returned, and rollback is started
S
Shengliang Guan 已提交
231 232
    SMDropQnodeReq dropReq = {0};
    dropReq.dnodeId = 2;
233

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

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

  {
    // continue send message, qnode is dropping
S
Shengliang Guan 已提交
246 247
    SMCreateQnodeReq createReq = {0};
    createReq.dnodeId = 2;
248

249
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
250
    void*   pReq = rpcMallocCont(contLen);
251
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
252 253 254
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_DROPPING);
255 256 257 258
  }

  {
    // continue send message, qnode is dropping
S
Shengliang Guan 已提交
259 260
    SMDropQnodeReq dropReq = {0};
    dropReq.dnodeId = 2;
261

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

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

  {
    // server start, wait until the rollback finished
273
    server2.Start();
274 275 276
    taosMsleep(1000);

    int32_t retry = 0;
277
    int32_t retryMax = 20;
278 279

    for (retry = 0; retry < retryMax; retry++) {
S
Shengliang Guan 已提交
280 281
      SMCreateQnodeReq createReq = {0};
      createReq.dnodeId = 2;
282

283
      int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
284
      void*   pReq = rpcMallocCont(contLen);
285
      tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
286 287 288
      SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_QNODE, pReq, contLen);
      ASSERT_NE(pRsp, nullptr);
      if (pRsp->code == 0) break;
289 290 291 292 293
      taosMsleep(1000);
    }

    ASSERT_NE(retry, retryMax);
  }
dengyihao's avatar
fix bug  
dengyihao 已提交
294
}