qnode.cpp 9.0 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
    int32_t contLen = sizeof(SCreateDnodeReq);
S
Shengliang Guan 已提交
106

S
Shengliang Guan 已提交
107
    SCreateDnodeReq* pReq = (SCreateDnodeReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
108
    strcpy(pReq->fqdn, "localhost");
S
Shengliang Guan 已提交
109
    pReq->port = htonl(9015);
S
Shengliang Guan 已提交
110

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

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

  {
S
Shengliang Guan 已提交
122 123
    SMCreateQnodeReq createReq = {0};
    createReq.dnodeId = 2;
S
Shengliang Guan 已提交
124

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

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

S
Shengliang Guan 已提交
133 134
    test.SendShowMetaReq(TSDB_MGMT_TABLE_QNODE, "");
    test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
135 136 137 138
    EXPECT_EQ(test.GetShowRows(), 2);

    CheckInt16(1);
    CheckInt16(2);
S
Shengliang Guan 已提交
139 140
    CheckBinary("localhost:9014", TSDB_EP_LEN);
    CheckBinary("localhost:9015", TSDB_EP_LEN);
S
Shengliang Guan 已提交
141 142 143 144 145
    CheckTimestamp();
    CheckTimestamp();
  }

  {
S
Shengliang Guan 已提交
146 147
    SMDropQnodeReq dropReq = {0};
    dropReq.dnodeId = 2;
S
Shengliang Guan 已提交
148

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

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

S
Shengliang Guan 已提交
157 158
    test.SendShowMetaReq(TSDB_MGMT_TABLE_QNODE, "");
    test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
159 160 161
    EXPECT_EQ(test.GetShowRows(), 1);

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

  {
S
Shengliang Guan 已提交
167 168
    SMDropQnodeReq dropReq = {0};
    dropReq.dnodeId = 2;
169

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

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

TEST_F(MndTestQnode, 03_Create_Qnode_Rollback) {
  {
    // send message first, then dnode2 crash, result is returned, and rollback is started
S
Shengliang Guan 已提交
183 184
    SMCreateQnodeReq createReq = {0};
    createReq.dnodeId = 2;
185

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

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

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

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

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

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

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

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

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

    int32_t retry = 0;
230
    int32_t retryMax = 20;
231 232

    for (retry = 0; retry < retryMax; retry++) {
S
Shengliang Guan 已提交
233 234
      SMCreateQnodeReq createReq = {0};
      createReq.dnodeId = 2;
235

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
284 285 286
    int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq);
287

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

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

    int32_t retry = 0;
299
    int32_t retryMax = 20;
300 301

    for (retry = 0; retry < retryMax; retry++) {
S
Shengliang Guan 已提交
302 303
      SMCreateQnodeReq createReq = {0};
      createReq.dnodeId = 2;
304

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

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