snode.cpp 8.4 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

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.SendShowReq(TSDB_MGMT_TABLE_SNODE, "snodes", "");
S
Shengliang Guan 已提交
43 44 45
  EXPECT_EQ(test.GetShowRows(), 0);
}

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

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

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

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

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

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

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

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

TEST_F(MndTestSnode, 03_Create_Snode_Rollback) {
  {
    // send message first, then dnode2 crash, result is returned, and rollback is started
S
Shengliang Guan 已提交
158 159
    SMCreateSnodeReq 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();
S
Shengliang Guan 已提交
166 167 168
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_SNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_RPC_NETWORK_UNAVAIL);
169 170 171 172
  }

  {
    // continue send message, snode is creating
S
Shengliang Guan 已提交
173 174
    SMCreateSnodeReq createReq = {0};
    createReq.dnodeId = 2;
175

176
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
177
    void*   pReq = rpcMallocCont(contLen);
178
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
179

S
Shengliang Guan 已提交
180 181 182
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_SNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_CREATING);
183 184 185 186
  }

  {
    // continue send message, snode is creating
S
Shengliang Guan 已提交
187 188
    SMDropSnodeReq dropReq = {0};
    dropReq.dnodeId = 2;
189

190
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &dropReq);
S
Shengliang Guan 已提交
191
    void*   pReq = rpcMallocCont(contLen);
192
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &dropReq);
193

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

  {
    // server start, wait until the rollback finished
201
    server2.Start();
202 203 204
    taosMsleep(1000);

    int32_t retry = 0;
205
    int32_t retryMax = 20;
206 207

    for (retry = 0; retry < retryMax; retry++) {
S
Shengliang Guan 已提交
208 209
      SMCreateSnodeReq createReq = {0};
      createReq.dnodeId = 2;
210

211
      int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
212
      void*   pReq = rpcMallocCont(contLen);
213
      tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
214

S
Shengliang Guan 已提交
215 216 217
      SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_SNODE, pReq, contLen);
      ASSERT_NE(pRsp, nullptr);
      if (pRsp->code == 0) break;
218 219 220 221 222 223 224 225 226 227
      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 已提交
228 229
    SMDropSnodeReq dropReq = {0};
    dropReq.dnodeId = 2;
230

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

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

  {
    // continue send message, snode is dropping
S
Shengliang Guan 已提交
243 244
    SMCreateSnodeReq createReq = {0};
    createReq.dnodeId = 2;
245

246
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
247
    void*   pReq = rpcMallocCont(contLen);
248
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
249

S
Shengliang Guan 已提交
250 251 252
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_SNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_SDB_OBJ_DROPPING);
253 254 255 256
  }

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

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

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

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

    int32_t retry = 0;
275
    int32_t retryMax = 20;
276 277

    for (retry = 0; retry < retryMax; retry++) {
S
Shengliang Guan 已提交
278 279
      SMCreateSnodeReq createReq = {0};
      createReq.dnodeId = 2;
280

281
      int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
282
      void*   pReq = rpcMallocCont(contLen);
283
      tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
284

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

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