qnode.cpp 7.7 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 55 56 57 58 59
TEST_F(MndTestQnode, 02_Create_Qnode) {
  {
    int32_t contLen = sizeof(SMCreateQnodeReq);

    SMCreateQnodeReq* pReq = (SMCreateQnodeReq*)rpcMallocCont(contLen);
    pReq->dnodeId = htonl(2);

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

S
Shengliang Guan 已提交
65
  {
S
Shengliang Guan 已提交
66
    int32_t contLen = sizeof(SMCreateQnodeReq);
S
Shengliang Guan 已提交
67

S
Shengliang Guan 已提交
68
    SMCreateQnodeReq* pReq = (SMCreateQnodeReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
69 70
    pReq->dnodeId = htonl(1);

S
Shengliang Guan 已提交
71 72 73
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
74

S
Shengliang Guan 已提交
75
    test.SendShowMetaReq(TSDB_MGMT_TABLE_QNODE, "");
S
Shengliang Guan 已提交
76
    CHECK_META("show qnodes", 3);
S
Shengliang Guan 已提交
77
    test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
78 79 80
    EXPECT_EQ(test.GetShowRows(), 1);

    CheckInt16(1);
S
Shengliang Guan 已提交
81
    CheckBinary("localhost:9014", TSDB_EP_LEN);
S
Shengliang Guan 已提交
82 83 84
    CheckTimestamp();
  }

S
Shengliang Guan 已提交
85
  {
S
Shengliang Guan 已提交
86
    int32_t contLen = sizeof(SMCreateQnodeReq);
S
Shengliang Guan 已提交
87

S
Shengliang Guan 已提交
88
    SMCreateQnodeReq* pReq = (SMCreateQnodeReq*)rpcMallocCont(contLen);
89
    pReq->dnodeId = htonl(1);
S
Shengliang Guan 已提交
90

S
Shengliang Guan 已提交
91 92 93
    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 已提交
94 95 96
  }
}

97
TEST_F(MndTestQnode, 03_Drop_Qnode) {
S
Shengliang Guan 已提交
98
  {
S
Shengliang Guan 已提交
99
    int32_t contLen = sizeof(SCreateDnodeReq);
S
Shengliang Guan 已提交
100

S
Shengliang Guan 已提交
101
    SCreateDnodeReq* pReq = (SCreateDnodeReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
102
    strcpy(pReq->fqdn, "localhost");
S
Shengliang Guan 已提交
103
    pReq->port = htonl(9015);
S
Shengliang Guan 已提交
104

S
Shengliang Guan 已提交
105 106 107
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
108 109

    taosMsleep(1300);
S
Shengliang Guan 已提交
110 111
    test.SendShowMetaReq(TSDB_MGMT_TABLE_DNODE, "");
    test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
112 113 114 115
    EXPECT_EQ(test.GetShowRows(), 2);
  }

  {
S
Shengliang Guan 已提交
116
    int32_t contLen = sizeof(SMCreateQnodeReq);
S
Shengliang Guan 已提交
117

S
Shengliang Guan 已提交
118
    SMCreateQnodeReq* pReq = (SMCreateQnodeReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
119 120
    pReq->dnodeId = htonl(2);

S
Shengliang Guan 已提交
121 122 123
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
124

S
Shengliang Guan 已提交
125 126
    test.SendShowMetaReq(TSDB_MGMT_TABLE_QNODE, "");
    test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
127 128 129 130
    EXPECT_EQ(test.GetShowRows(), 2);

    CheckInt16(1);
    CheckInt16(2);
S
Shengliang Guan 已提交
131 132
    CheckBinary("localhost:9014", TSDB_EP_LEN);
    CheckBinary("localhost:9015", TSDB_EP_LEN);
S
Shengliang Guan 已提交
133 134 135 136 137
    CheckTimestamp();
    CheckTimestamp();
  }

  {
S
Shengliang Guan 已提交
138
    int32_t contLen = sizeof(SMDropQnodeReq);
S
Shengliang Guan 已提交
139

S
Shengliang Guan 已提交
140
    SMDropQnodeReq* pReq = (SMDropQnodeReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
141 142
    pReq->dnodeId = htonl(2);

S
Shengliang Guan 已提交
143 144 145
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
146

S
Shengliang Guan 已提交
147 148
    test.SendShowMetaReq(TSDB_MGMT_TABLE_QNODE, "");
    test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
149 150 151
    EXPECT_EQ(test.GetShowRows(), 1);

    CheckInt16(1);
S
Shengliang Guan 已提交
152
    CheckBinary("localhost:9014", TSDB_EP_LEN);
S
Shengliang Guan 已提交
153 154
    CheckTimestamp();
  }
155 156 157 158 159 160 161

  {
    int32_t contLen = sizeof(SMDropQnodeReq);

    SMDropQnodeReq* pReq = (SMDropQnodeReq*)rpcMallocCont(contLen);
    pReq->dnodeId = htonl(2);

S
Shengliang Guan 已提交
162 163 164
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_QNODE_NOT_EXIST);
165 166 167 168 169 170 171 172 173 174 175 176
  }
}

TEST_F(MndTestQnode, 03_Create_Qnode_Rollback) {
  {
    // send message first, then dnode2 crash, result is returned, and rollback is started
    int32_t contLen = sizeof(SMCreateQnodeReq);

    SMCreateQnodeReq* pReq = (SMCreateQnodeReq*)rpcMallocCont(contLen);
    pReq->dnodeId = htonl(2);

    server2.Stop();
S
Shengliang Guan 已提交
177 178 179
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_RPC_NETWORK_UNAVAIL);
180 181 182 183 184 185 186 187 188
  }

  {
    // continue send message, qnode is creating
    int32_t contLen = sizeof(SMCreateQnodeReq);

    SMCreateQnodeReq* pReq = (SMCreateQnodeReq*)rpcMallocCont(contLen);
    pReq->dnodeId = htonl(2);

S
Shengliang Guan 已提交
189 190 191
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_CREATING);
192 193 194 195 196 197 198 199 200
  }

  {
    // continue send message, qnode is creating
    int32_t contLen = sizeof(SMDropQnodeReq);

    SMDropQnodeReq* pReq = (SMDropQnodeReq*)rpcMallocCont(contLen);
    pReq->dnodeId = htonl(2);

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

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

    int32_t retry = 0;
212
    int32_t retryMax = 20;
213 214 215 216 217 218 219

    for (retry = 0; retry < retryMax; retry++) {
      int32_t contLen = sizeof(SMCreateQnodeReq);

      SMCreateQnodeReq* pReq = (SMCreateQnodeReq*)rpcMallocCont(contLen);
      pReq->dnodeId = htonl(2);

S
Shengliang Guan 已提交
220 221 222
      SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_QNODE, pReq, contLen);
      ASSERT_NE(pRsp, nullptr);
      if (pRsp->code == 0) break;
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
      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
    int32_t contLen = sizeof(SMDropQnodeReq);

    SMDropQnodeReq* pReq = (SMDropQnodeReq*)rpcMallocCont(contLen);
    pReq->dnodeId = htonl(2);

    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 246 247 248 249 250
  }

  {
    // continue send message, qnode is dropping
    int32_t contLen = sizeof(SMCreateQnodeReq);

    SMCreateQnodeReq* pReq = (SMCreateQnodeReq*)rpcMallocCont(contLen);
    pReq->dnodeId = htonl(2);

S
Shengliang Guan 已提交
251 252 253
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_DROPPING);
254 255 256 257 258 259 260 261 262
  }

  {
    // continue send message, qnode is dropping
    int32_t contLen = sizeof(SMDropQnodeReq);

    SMDropQnodeReq* pReq = (SMDropQnodeReq*)rpcMallocCont(contLen);
    pReq->dnodeId = htonl(2);

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

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

    int32_t retry = 0;
274
    int32_t retryMax = 20;
275 276 277 278 279 280 281

    for (retry = 0; retry < retryMax; retry++) {
      int32_t contLen = sizeof(SMCreateQnodeReq);

      SMCreateQnodeReq* pReq = (SMCreateQnodeReq*)rpcMallocCont(contLen);
      pReq->dnodeId = htonl(2);

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

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