snode.cpp 9.0 KB
Newer Older
S
Shengliang Guan 已提交
1
/**
S
Shengliang Guan 已提交
2
 * @file snode.cpp
S
Shengliang Guan 已提交
3
 * @author slguan (slguan@taosdata.com)
S
Shengliang Guan 已提交
4 5 6
 * @brief MNODE module snode 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 MndTestSnode : 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_snode1", 9016);
S
Shengliang Guan 已提交
22
    const char* fqdn = "localhost";
S
Shengliang Guan 已提交
23
    const char* firstEp = "localhost:9016";
S
Shengliang Guan 已提交
24

S
Shengliang Guan 已提交
25
    server2.Start("/tmp/mnode_test_snode2", fqdn, 9017, 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   MndTestSnode::test;
TestServer MndTestSnode::server2;
S
Shengliang Guan 已提交
40

S
Shengliang Guan 已提交
41
TEST_F(MndTestSnode, 01_Show_Snode) {
S
Shengliang Guan 已提交
42
  test.SendShowMetaReq(TSDB_MGMT_TABLE_SNODE, "");
S
Shengliang Guan 已提交
43 44 45 46 47 48
  CHECK_META("show snodes", 3);

  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 已提交
49
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
50 51 52
  EXPECT_EQ(test.GetShowRows(), 0);
}

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

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

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

103
TEST_F(MndTestSnode, 03_Drop_Snode) {
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(9017);
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
    SMCreateSnodeReq 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_SNODE, 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_SNODE, "");
    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:9016", TSDB_EP_LEN);
    CheckBinary("localhost:9017", TSDB_EP_LEN);
S
Shengliang Guan 已提交
141 142 143 144 145
    CheckTimestamp();
    CheckTimestamp();
  }

  {
S
Shengliang Guan 已提交
146 147
    SMDropSnodeReq 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_SNODE, 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_SNODE, "");
    test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
159 160 161
    EXPECT_EQ(test.GetShowRows(), 1);

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

  {
S
Shengliang Guan 已提交
167 168
    SMDropSnodeReq 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_SNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_SNODE_NOT_EXIST);
177 178 179 180 181 182
  }
}

TEST_F(MndTestSnode, 03_Create_Snode_Rollback) {
  {
    // send message first, then dnode2 crash, result is returned, and rollback is started
S
Shengliang Guan 已提交
183 184
    SMCreateSnodeReq 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_SNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_RPC_NETWORK_UNAVAIL);
194 195 196 197
  }

  {
    // continue send message, snode is creating
S
Shengliang Guan 已提交
198 199
    SMCreateSnodeReq 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_SNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_CREATING);
208 209 210 211
  }

  {
    // continue send message, snode is creating
S
Shengliang Guan 已提交
212 213
    SMDropSnodeReq 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_SNODE, 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
      SMCreateSnodeReq 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_SNODE, 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(MndTestSnode, 04_Drop_Snode_Rollback) {
  {
    // send message first, then dnode2 crash, result is returned, and rollback is started
S
Shengliang Guan 已提交
253 254
    SMDropSnodeReq 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_SNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_RPC_NETWORK_UNAVAIL);
264 265 266 267
  }

  {
    // continue send message, snode is dropping
S
Shengliang Guan 已提交
268 269
    SMCreateSnodeReq 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);
274

S
Shengliang Guan 已提交
275 276 277
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_SNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_DROPPING);
278 279 280 281
  }

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

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

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

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

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

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

S
Shengliang Guan 已提交
306 307 308
      int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq);
      void*   pReq = rpcMallocCont(contLen);
      tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq);
309

S
Shengliang Guan 已提交
310 311 312
      SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_SNODE, pReq, contLen);
      ASSERT_NE(pRsp, nullptr);
      if (pRsp->code == 0) break;
313 314 315 316 317
      taosMsleep(1000);
    }

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