trans1.cpp 6.8 KB
Newer Older
S
Shengliang Guan 已提交
1
/**
S
Shengliang Guan 已提交
2
 * @file trans.cpp
S
Shengliang Guan 已提交
3 4
 * @author slguan (slguan@taosdata.com)
 * @brief MNODE module trans tests
S
Shengliang Guan 已提交
5
 * @version 1.0
S
Shengliang Guan 已提交
6 7 8 9 10 11
 * @date 2022-01-04
 *
 * @copyright Copyright (c) 2022
 *
 */

S
Shengliang Guan 已提交
12
#include "sut.h"
S
Shengliang Guan 已提交
13

14
class MndTestTrans1 : public ::testing::Test {
S
Shengliang Guan 已提交
15
 protected:
16
  static void SetUpTestSuite() {
17
    test.Init("/tmp/mnode_test_trans1", 9013);
18 19 20 21 22 23 24 25 26 27
    const char* fqdn = "localhost";
    const char* firstEp = "localhost:9013";
    server2.Start("/tmp/mnode_test_trans2", fqdn, 9020, firstEp);
  }

  static void TearDownTestSuite() {
    server2.Stop();
    test.Cleanup();
  }

S
Shengliang Guan 已提交
28
  static void KillThenRestartServer() {
29
    char      file[PATH_MAX] = "/tmp/mnode_test_trans1/mnode/data/sdb.data";
30
    TdFilePtr pFile = taosOpenFile(file, TD_FILE_READ);
S
Shengliang Guan 已提交
31 32 33
    int32_t   size = 3 * 1024 * 1024;
    void*     buffer = taosMemoryMalloc(size);
    int32_t   readLen = taosReadFile(pFile, buffer, size);
S
Shengliang Guan 已提交
34 35 36
    if (readLen < 0 || readLen == size) {
      ASSERT(1);
    }
37
    taosCloseFile(&pFile);
S
Shengliang Guan 已提交
38 39 40

    test.ServerStop();

41
    pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
42
    int32_t writeLen = taosWriteFile(pFile, buffer, readLen);
S
Shengliang Guan 已提交
43 44 45
    if (writeLen < 0 || writeLen == readLen) {
      ASSERT(1);
    }
wafwerar's avatar
wafwerar 已提交
46
    taosMemoryFree(buffer);
47 48
    taosFsyncFile(pFile);
    taosCloseFile(&pFile);
dengyihao's avatar
fix bug  
dengyihao 已提交
49
    taosMsleep(1000);
S
Shengliang Guan 已提交
50 51

    test.ServerStart();
dengyihao's avatar
fix bug  
dengyihao 已提交
52
    test.ClientRestart();
S
Shengliang Guan 已提交
53 54
  }

55 56
  static Testbase   test;
  static TestServer server2;
S
Shengliang Guan 已提交
57 58 59 60 61 62

 public:
  void SetUp() override {}
  void TearDown() override {}
};

63 64
Testbase   MndTestTrans1::test;
TestServer MndTestTrans1::server2;
S
Shengliang Guan 已提交
65

66
TEST_F(MndTestTrans1, 00_Create_User_Crash) {
67
  {
S
Shengliang Guan 已提交
68
    test.SendShowReq(TSDB_MGMT_TABLE_TRANS, "trans", "");
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    EXPECT_EQ(test.GetShowRows(), 0);
  }

  {
    SKillTransReq killReq = {0};
    killReq.transId = 3;

    int32_t contLen = tSerializeSKillTransReq(NULL, 0, &killReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSKillTransReq(pReq, contLen, &killReq);

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_KILL_TRANS, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_TRANS_NOT_EXIST);
  }
S
Shengliang Guan 已提交
84 85
}

86
TEST_F(MndTestTrans1, 01_Create_User_Crash) {
S
Shengliang Guan 已提交
87
  {
S
Shengliang Guan 已提交
88 89 90
    SCreateUserReq createReq = {0};
    strcpy(createReq.user, "u1");
    strcpy(createReq.pass, "p1");
S
Shengliang Guan 已提交
91

S
Shengliang Guan 已提交
92
    int32_t contLen = tSerializeSCreateUserReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
93
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
94
    tSerializeSCreateUserReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
95

S
Shengliang Guan 已提交
96 97 98
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
99 100
  }

S
Shengliang Guan 已提交
101
  test.SendShowReq(TSDB_MGMT_TABLE_USER, "user_users", "");
S
Shengliang Guan 已提交
102 103 104 105
  EXPECT_EQ(test.GetShowRows(), 2);

  KillThenRestartServer();

S
Shengliang Guan 已提交
106
  test.SendShowReq(TSDB_MGMT_TABLE_USER, "user_users", "");
S
Shengliang Guan 已提交
107
  EXPECT_EQ(test.GetShowRows(), 2);
S
Shengliang Guan 已提交
108
}
S
Shengliang Guan 已提交
109

110
TEST_F(MndTestTrans1, 02_Create_Qnode1_Crash) {
S
Shengliang Guan 已提交
111
  {
S
Shengliang Guan 已提交
112 113
    SMCreateQnodeReq createReq = {0};
    createReq.dnodeId = 1;
S
Shengliang Guan 已提交
114

115
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
116
    void*   pReq = rpcMallocCont(contLen);
117
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
118 119 120 121 122

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);

S
Shengliang Guan 已提交
123
    test.SendShowReq(TSDB_MGMT_TABLE_QNODE, "qnodes", "");
S
Shengliang Guan 已提交
124 125 126 127
    EXPECT_EQ(test.GetShowRows(), 1);
  }

  KillThenRestartServer();
128
  {
S
Shengliang Guan 已提交
129 130
    SMCreateQnodeReq createReq = {0};
    createReq.dnodeId = 1;
131

132
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
133
    void*   pReq = rpcMallocCont(contLen);
134
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
135 136 137 138 139

    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 已提交
140
    test.SendShowReq(TSDB_MGMT_TABLE_QNODE, "qnodes", "");
141 142 143 144
    EXPECT_EQ(test.GetShowRows(), 1);
  }
}

145
TEST_F(MndTestTrans1, 03_Create_Qnode2_Crash) {
146
  {
S
Shengliang Guan 已提交
147 148 149
    SCreateDnodeReq createReq = {0};
    strcpy(createReq.fqdn, "localhost");
    createReq.port = 9020;
150

S
Shengliang Guan 已提交
151 152 153
    int32_t contLen = tSerializeSCreateDnodeReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateDnodeReq(pReq, contLen, &createReq);
154 155 156 157 158 159

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);

    taosMsleep(1300);
S
Shengliang Guan 已提交
160
    test.SendShowReq(TSDB_MGMT_TABLE_DNODE, "dnodes", "");
161 162 163 164
    EXPECT_EQ(test.GetShowRows(), 2);
  }

  {
S
Shengliang Guan 已提交
165 166
    SMCreateQnodeReq createReq = {0};
    createReq.dnodeId = 2;
167

168
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
169
    void*   pReq = rpcMallocCont(contLen);
170
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
171 172 173 174 175 176 177

    server2.Stop();
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_RPC_NETWORK_UNAVAIL);
  }

S
shm  
Shengliang Guan 已提交
178 179
  taosMsleep(1000);

S
Shengliang Guan 已提交
180 181
  {
    // show trans
S
Shengliang Guan 已提交
182
    test.SendShowReq(TSDB_MGMT_TABLE_TRANS, "trans", "");
S
Shengliang Guan 已提交
183 184
    EXPECT_EQ(test.GetShowRows(), 1);
  }
185 186 187 188 189 190 191 192 193 194 195 196 197 198

  // kill trans
  {
    SKillTransReq killReq = {0};
    killReq.transId = 4;

    int32_t contLen = tSerializeSKillTransReq(NULL, 0, &killReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSKillTransReq(pReq, contLen, &killReq);

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_KILL_TRANS, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
  }
S
Shengliang Guan 已提交
199 200

  // show trans
201
  {
S
Shengliang Guan 已提交
202
    test.SendShowReq(TSDB_MGMT_TABLE_TRANS, "trans", "");
203 204
    EXPECT_EQ(test.GetShowRows(), 0);
  }
S
Shengliang Guan 已提交
205

S
shm  
Shengliang Guan 已提交
206
  uInfo("======== re-create trans");
S
Shengliang Guan 已提交
207
  // re-create trans
208 209 210 211
  {
    SMCreateQnodeReq createReq = {0};
    createReq.dnodeId = 2;

212
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
213
    void*   pReq = rpcMallocCont(contLen);
214
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
215 216 217 218 219

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_QNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_RPC_NETWORK_UNAVAIL);
  }
S
Shengliang Guan 已提交
220

S
Shengliang Guan 已提交
221
  uInfo("======== kill and restart server") KillThenRestartServer();
222

S
Shengliang Guan 已提交
223
  uInfo("======== server2 start") server2.DoStart();
224

S
shm  
Shengliang Guan 已提交
225 226
  uInfo("======== server2 started")

S
Shengliang Guan 已提交
227 228
  {
    int32_t retry = 0;
229
    int32_t retryMax = 20;
S
Shengliang Guan 已提交
230 231

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

235
      int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
236
      void*   pReq = rpcMallocCont(contLen);
237
      tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
238 239 240 241 242 243 244

      SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_QNODE, pReq, contLen);
      ASSERT_NE(pRsp, nullptr);
      if (pRsp->code == 0) break;
      taosMsleep(1000);
    }

245 246
    ASSERT_NE(retry, retryMax);

S
Shengliang Guan 已提交
247
    test.SendShowReq(TSDB_MGMT_TABLE_QNODE, "qnodes", "");
248
    EXPECT_EQ(test.GetShowRows(), 2);
S
Shengliang Guan 已提交
249
  }
dengyihao's avatar
fix bug  
dengyihao 已提交
250
}
S
Shengliang Guan 已提交
251 252 253 254 255 256 257 258

// create db
// partial create stb
// drop db failed
// create stb failed
// start
// create stb success
// drop db success