qnode.cpp 9.2 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() {
S
Shengliang Guan 已提交
21
    test.Init("/tmp/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

S
Shengliang Guan 已提交
25
    server2.Start("/tmp/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.SendShowMetaReq(TSDB_MGMT_TABLE_QNODE, "");
S
Shengliang Guan 已提交
43
  CHECK_META("show qnodes", 3);
S
Shengliang Guan 已提交
44

S
Shengliang Guan 已提交
45 46 47
  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 已提交
48

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

53 54
TEST_F(MndTestQnode, 02_Create_Qnode) {
  {
S
Shengliang Guan 已提交
55 56
    SMCreateQnodeReq 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_QNODE, 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
    SMCreateQnodeReq 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_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
78

S
Shengliang Guan 已提交
79
    test.SendShowMetaReq(TSDB_MGMT_TABLE_QNODE, "");
S
Shengliang Guan 已提交
80
    CHECK_META("show qnodes", 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:9014", TSDB_EP_LEN);
S
Shengliang Guan 已提交
86 87 88
    CheckTimestamp();
  }

S
Shengliang Guan 已提交
89
  {
S
Shengliang Guan 已提交
90 91
    SMCreateQnodeReq 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_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_QNODE_ALREADY_EXIST);
S
Shengliang Guan 已提交
100 101 102
  }
}

103
TEST_F(MndTestQnode, 03_Drop_Qnode) {
S
Shengliang Guan 已提交
104
  {
S
Shengliang Guan 已提交
105 106 107
    SCreateDnodeReq createReq = {0};
    strcpy(createReq.fqdn, "localhost");
    createReq.port = 9015;
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
    SMCreateQnodeReq 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_QNODE, 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_QNODE, "");
    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:9014", TSDB_EP_LEN);
    CheckBinary("localhost:9015", TSDB_EP_LEN);
S
Shengliang Guan 已提交
143 144 145 146 147
    CheckTimestamp();
    CheckTimestamp();
  }

  {
S
Shengliang Guan 已提交
148 149
    SMDropQnodeReq 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_QNODE, 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_QNODE, "");
    test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
161 162 163
    EXPECT_EQ(test.GetShowRows(), 1);

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

  {
S
Shengliang Guan 已提交
169 170
    SMDropQnodeReq 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_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_QNODE_NOT_EXIST);
179 180 181 182 183 184
  }
}

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

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

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

S
Shengliang Guan 已提交
206 207 208
    int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq);
209

S
Shengliang Guan 已提交
210 211 212
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_CREATING);
213 214 215 216
  }

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

S
Shengliang Guan 已提交
220 221 222
    int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq);
223

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

  {
    // server start, wait until the rollback finished
    server2.DoStart();
dengyihao's avatar
fix bug  
dengyihao 已提交
232
    test.ClientRestart();
233 234 235
    taosMsleep(1000);

    int32_t retry = 0;
236
    int32_t retryMax = 20;
237 238

    for (retry = 0; retry < retryMax; retry++) {
S
Shengliang Guan 已提交
239 240
      SMCreateQnodeReq createReq = {0};
      createReq.dnodeId = 2;
241

S
Shengliang Guan 已提交
242 243 244
      int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq);
      void*   pReq = rpcMallocCont(contLen);
      tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq);
245

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

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

    server2.Stop();
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_RPC_NETWORK_UNAVAIL);
269 270 271 272
  }

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

S
Shengliang Guan 已提交
276 277 278
    int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
279 280 281
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_DROPPING);
282 283 284 285
  }

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

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

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

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

    int32_t retry = 0;
304
    int32_t retryMax = 20;
305 306

    for (retry = 0; retry < retryMax; retry++) {
S
Shengliang Guan 已提交
307 308
      SMCreateQnodeReq createReq = {0};
      createReq.dnodeId = 2;
309

S
Shengliang Guan 已提交
310 311 312
      int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq);
      void*   pReq = rpcMallocCont(contLen);
      tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
313 314 315
      SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_QNODE, pReq, contLen);
      ASSERT_NE(pRsp, nullptr);
      if (pRsp->code == 0) break;
316 317 318 319 320
      taosMsleep(1000);
    }

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