db.cpp 10.2 KB
Newer Older
S
Shengliang Guan 已提交
1
/**
S
Shengliang Guan 已提交
2
 * @file db.cpp
S
Shengliang Guan 已提交
3
 * @author slguan (slguan@taosdata.com)
S
Shengliang Guan 已提交
4 5 6
 * @brief MNODE module db tests
 * @version 1.0
 * @date 2022-01-11
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 MndTestDb : public ::testing::Test {
S
Shengliang Guan 已提交
15
 protected:
S
Shengliang Guan 已提交
16 17 18 19
  static void SetUpTestSuite() {
    test.Init("/tmp/mnode_test_db", 9030);
    const char* fqdn = "localhost";
    const char* firstEp = "localhost:9030";
S
Shengliang Guan 已提交
20

S
Shengliang Guan 已提交
21 22 23 24 25 26 27 28 29
    server2.Start("/tmp/mnode_test_db2", fqdn, 9031, firstEp);
  }
  static void TearDownTestSuite() {
    server2.Stop();
    test.Cleanup();
  }

  static Testbase   test;
  static TestServer server2;
S
Shengliang Guan 已提交
30 31 32 33 34 35

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

S
Shengliang Guan 已提交
36 37
Testbase   MndTestDb::test;
TestServer MndTestDb::server2;
S
Shengliang Guan 已提交
38

S
Shengliang Guan 已提交
39
TEST_F(MndTestDb, 01_ShowDb) {
S
Shengliang Guan 已提交
40
  test.SendShowMetaReq(TSDB_MGMT_TABLE_DB, "");
S
Shengliang Guan 已提交
41
  CHECK_META("show databases", 18);
S
Shengliang Guan 已提交
42 43 44
  CHECK_SCHEMA(0, TSDB_DATA_TYPE_BINARY, TSDB_DB_NAME_LEN - 1 + VARSTR_HEADER_SIZE, "name");
  CHECK_SCHEMA(1, TSDB_DATA_TYPE_TIMESTAMP, 8, "create_time");
  CHECK_SCHEMA(2, TSDB_DATA_TYPE_SMALLINT, 2, "vgroups");
S
Shengliang Guan 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  CHECK_SCHEMA(3, TSDB_DATA_TYPE_INT, 4, "ntables");
  CHECK_SCHEMA(4, TSDB_DATA_TYPE_SMALLINT, 2, "replica");
  CHECK_SCHEMA(5, TSDB_DATA_TYPE_SMALLINT, 2, "quorum");
  CHECK_SCHEMA(6, TSDB_DATA_TYPE_SMALLINT, 2, "days");
  CHECK_SCHEMA(7, TSDB_DATA_TYPE_BINARY, 24 + VARSTR_HEADER_SIZE, "keep0,keep1,keep2");
  CHECK_SCHEMA(8, TSDB_DATA_TYPE_INT, 4, "cache");
  CHECK_SCHEMA(9, TSDB_DATA_TYPE_INT, 4, "blocks");
  CHECK_SCHEMA(10, TSDB_DATA_TYPE_INT, 4, "minrows");
  CHECK_SCHEMA(11, TSDB_DATA_TYPE_INT, 4, "maxrows");
  CHECK_SCHEMA(12, TSDB_DATA_TYPE_TINYINT, 1, "wallevel");
  CHECK_SCHEMA(13, TSDB_DATA_TYPE_INT, 4, "fsync");
  CHECK_SCHEMA(14, TSDB_DATA_TYPE_TINYINT, 1, "comp");
  CHECK_SCHEMA(15, TSDB_DATA_TYPE_TINYINT, 1, "cachelast");
  CHECK_SCHEMA(16, TSDB_DATA_TYPE_BINARY, 3 + VARSTR_HEADER_SIZE, "precision");
  CHECK_SCHEMA(17, TSDB_DATA_TYPE_TINYINT, 1, "update");
S
Shengliang Guan 已提交
60

S
Shengliang Guan 已提交
61
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
62
  EXPECT_EQ(test.GetShowRows(), 0);
S
Shengliang Guan 已提交
63 64
}

S
Shengliang Guan 已提交
65
TEST_F(MndTestDb, 02_Create_Alter_Drop_Db) {
S
Shengliang Guan 已提交
66
  {
S
Shengliang Guan 已提交
67
    int32_t contLen = sizeof(SCreateDbReq);
S
Shengliang Guan 已提交
68

S
Shengliang Guan 已提交
69
    SCreateDbReq* pReq = (SCreateDbReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
70
    strcpy(pReq->db, "1.d1");
71
    pReq->numOfVgroups = htonl(2);
S
Shengliang Guan 已提交
72 73 74 75 76 77
    pReq->cacheBlockSize = htonl(16);
    pReq->totalBlocks = htonl(10);
    pReq->daysPerFile = htonl(10);
    pReq->daysToKeep0 = htonl(3650);
    pReq->daysToKeep1 = htonl(3650);
    pReq->daysToKeep2 = htonl(3650);
S
Shengliang Guan 已提交
78 79
    pReq->minRows = htonl(100);
    pReq->maxRows = htonl(4096);
S
Shengliang Guan 已提交
80 81
    pReq->commitTime = htonl(3600);
    pReq->fsyncPeriod = htonl(3000);
S
Shengliang Guan 已提交
82 83 84 85 86 87 88 89
    pReq->walLevel = 1;
    pReq->precision = 0;
    pReq->compression = 2;
    pReq->replications = 1;
    pReq->quorum = 1;
    pReq->update = 0;
    pReq->cacheLastRow = 0;
    pReq->ignoreExist = 1;
S
Shengliang Guan 已提交
90

S
Shengliang Guan 已提交
91 92 93
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DB, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
94 95
  }

S
Shengliang Guan 已提交
96
  test.SendShowMetaReq(TSDB_MGMT_TABLE_DB, "");
S
Shengliang Guan 已提交
97
  CHECK_META("show databases", 18);
S
Shengliang Guan 已提交
98

S
Shengliang Guan 已提交
99
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
100
  EXPECT_EQ(test.GetShowRows(), 1);
S
Shengliang Guan 已提交
101
  CheckBinary("d1", TSDB_DB_NAME_LEN - 1);
S
Shengliang Guan 已提交
102
  CheckTimestamp();
S
Shengliang Guan 已提交
103
  CheckInt16(2);                      // vgroups
S
Shengliang Guan 已提交
104
  CheckInt32(0);                      // ntables
S
Shengliang Guan 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118
  CheckInt16(1);                      // replica
  CheckInt16(1);                      // quorum
  CheckInt16(10);                     // days
  CheckBinary("3650,3650,3650", 24);  // days
  CheckInt32(16);                     // cache
  CheckInt32(10);                     // blocks
  CheckInt32(100);                    // minrows
  CheckInt32(4096);                   // maxrows
  CheckInt8(1);                       // wallevel
  CheckInt32(3000);                   // fsync
  CheckInt8(2);                       // comp
  CheckInt8(0);                       // cachelast
  CheckBinary("ms", 3);               // precision
  CheckInt8(0);                       // update
S
Shengliang Guan 已提交
119

S
Shengliang Guan 已提交
120
  test.SendShowMetaReq(TSDB_MGMT_TABLE_VGROUP, "1.d1");
S
Shengliang Guan 已提交
121 122 123 124 125 126
  CHECK_META("show vgroups", 4);
  CHECK_SCHEMA(0, TSDB_DATA_TYPE_INT, 4, "vgId");
  CHECK_SCHEMA(1, TSDB_DATA_TYPE_INT, 4, "tables");
  CHECK_SCHEMA(2, TSDB_DATA_TYPE_SMALLINT, 2, "v1_dnode");
  CHECK_SCHEMA(3, TSDB_DATA_TYPE_BINARY, 9 + VARSTR_HEADER_SIZE, "v1_status");

S
Shengliang Guan 已提交
127
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
128
  EXPECT_EQ(test.GetShowRows(), 2);
S
Shengliang Guan 已提交
129
  CheckInt32(2);
130
  CheckInt32(3);
S
Shengliang Guan 已提交
131 132 133 134 135 136 137
  CheckInt32(0);
  CheckInt32(0);
  CheckInt16(1);
  CheckInt16(1);
  CheckBinary("master", 9);
  CheckBinary("master", 9);

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

S
Shengliang Guan 已提交
141
    SAlterDbReq* pReq = (SAlterDbReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
142 143 144 145 146 147 148 149 150
    strcpy(pReq->db, "1.d1");
    pReq->totalBlocks = htonl(12);
    pReq->daysToKeep0 = htonl(300);
    pReq->daysToKeep1 = htonl(400);
    pReq->daysToKeep2 = htonl(500);
    pReq->fsyncPeriod = htonl(4000);
    pReq->walLevel = 2;
    pReq->quorum = 2;
    pReq->cacheLastRow = 1;
S
Shengliang Guan 已提交
151

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

S
Shengliang Guan 已提交
157 158
  test.SendShowMetaReq(TSDB_MGMT_TABLE_DB, "");
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
159
  EXPECT_EQ(test.GetShowRows(), 1);
S
Shengliang Guan 已提交
160
  CheckBinary("d1", TSDB_DB_NAME_LEN - 1);
S
Shengliang Guan 已提交
161
  CheckTimestamp();
S
Shengliang Guan 已提交
162
  CheckInt16(2);                   // vgroups
S
Shengliang Guan 已提交
163
  CheckInt32(0);                   // tables
S
Shengliang Guan 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177
  CheckInt16(1);                   // replica
  CheckInt16(2);                   // quorum
  CheckInt16(10);                  // days
  CheckBinary("300,400,500", 24);  // days
  CheckInt32(16);                  // cache
  CheckInt32(12);                  // blocks
  CheckInt32(100);                 // minrows
  CheckInt32(4096);                // maxrows
  CheckInt8(2);                    // wallevel
  CheckInt32(4000);                // fsync
  CheckInt8(2);                    // comp
  CheckInt8(1);                    // cachelast
  CheckBinary("ms", 3);            // precision
  CheckInt8(0);                    // update
S
Shengliang Guan 已提交
178

S
Shengliang Guan 已提交
179 180 181
  // restart
  test.Restart();

S
Shengliang Guan 已提交
182
  test.SendShowMetaReq(TSDB_MGMT_TABLE_DB, "");
S
Shengliang Guan 已提交
183
  CHECK_META("show databases", 18);
S
Shengliang Guan 已提交
184

S
Shengliang Guan 已提交
185
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
186 187 188 189 190
  EXPECT_EQ(test.GetShowRows(), 1);

  CheckBinary("d1", TSDB_DB_NAME_LEN - 1);
  CheckTimestamp();
  CheckInt16(2);                   // vgroups
S
Shengliang Guan 已提交
191
  CheckInt32(0);                   // tables
S
Shengliang Guan 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  CheckInt16(1);                   // replica
  CheckInt16(2);                   // quorum
  CheckInt16(10);                  // days
  CheckBinary("300,400,500", 24);  // days
  CheckInt32(16);                  // cache
  CheckInt32(12);                  // blocks
  CheckInt32(100);                 // minrows
  CheckInt32(4096);                // maxrows
  CheckInt8(2);                    // wallevel
  CheckInt32(4000);                // fsync
  CheckInt8(2);                    // comp
  CheckInt8(1);                    // cachelast
  CheckBinary("ms", 3);            // precision
  CheckInt8(0);                    // update

  {
S
Shengliang Guan 已提交
208
    int32_t contLen = sizeof(SDropDbReq);
S
Shengliang Guan 已提交
209

S
Shengliang Guan 已提交
210
    SDropDbReq* pReq = (SDropDbReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
211 212
    strcpy(pReq->db, "1.d1");

S
Shengliang Guan 已提交
213 214 215
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_DB, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
216 217
  }

S
Shengliang Guan 已提交
218
  test.SendShowMetaReq(TSDB_MGMT_TABLE_DB, "");
S
Shengliang Guan 已提交
219
  CHECK_META("show databases", 18);
S
Shengliang Guan 已提交
220

S
Shengliang Guan 已提交
221
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
222
  EXPECT_EQ(test.GetShowRows(), 0);
S
Shengliang Guan 已提交
223 224
}

S
Shengliang Guan 已提交
225
TEST_F(MndTestDb, 03_Create_Use_Restart_Use_Db) {
S
Shengliang Guan 已提交
226
  {
S
Shengliang Guan 已提交
227
    int32_t contLen = sizeof(SCreateDbReq);
S
Shengliang Guan 已提交
228

S
Shengliang Guan 已提交
229
    SCreateDbReq* pReq = (SCreateDbReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
230 231 232 233 234 235 236 237
    strcpy(pReq->db, "1.d2");
    pReq->numOfVgroups = htonl(2);
    pReq->cacheBlockSize = htonl(16);
    pReq->totalBlocks = htonl(10);
    pReq->daysPerFile = htonl(10);
    pReq->daysToKeep0 = htonl(3650);
    pReq->daysToKeep1 = htonl(3650);
    pReq->daysToKeep2 = htonl(3650);
S
Shengliang Guan 已提交
238 239
    pReq->minRows = htonl(100);
    pReq->maxRows = htonl(4096);
S
Shengliang Guan 已提交
240 241 242 243 244 245 246 247 248 249 250
    pReq->commitTime = htonl(3600);
    pReq->fsyncPeriod = htonl(3000);
    pReq->walLevel = 1;
    pReq->precision = 0;
    pReq->compression = 2;
    pReq->replications = 1;
    pReq->quorum = 1;
    pReq->update = 0;
    pReq->cacheLastRow = 0;
    pReq->ignoreExist = 1;

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

S
Shengliang Guan 已提交
256
  test.SendShowMetaReq(TSDB_MGMT_TABLE_DB, "");
S
Shengliang Guan 已提交
257
  CHECK_META("show databases", 18);
S
Shengliang Guan 已提交
258

S
Shengliang Guan 已提交
259
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
260
  EXPECT_EQ(test.GetShowRows(), 1);
S
Shengliang Guan 已提交
261 262 263
  CheckBinary("d2", TSDB_DB_NAME_LEN - 1);

  {
S
Shengliang Guan 已提交
264
    int32_t contLen = sizeof(SUseDbReq);
S
Shengliang Guan 已提交
265

S
Shengliang Guan 已提交
266
    SUseDbReq* pReq = (SUseDbReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
267 268 269
    strcpy(pReq->db, "1.d2");
    pReq->vgVersion = htonl(-1);

S
Shengliang Guan 已提交
270
    SRpcMsg* pMsg = test.SendReq(TDMT_MND_USE_DB, pReq, contLen);
S
Shengliang Guan 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);

    SUseDbRsp* pRsp = (SUseDbRsp*)pMsg->pCont;
    EXPECT_STREQ(pRsp->db, "1.d2");
    pRsp->vgVersion = htonl(pRsp->vgVersion);
    pRsp->vgNum = htonl(pRsp->vgNum);
    pRsp->hashMethod = pRsp->hashMethod;
    EXPECT_EQ(pRsp->vgVersion, 1);
    EXPECT_EQ(pRsp->vgNum, 2);
    EXPECT_EQ(pRsp->hashMethod, 1);

    {
      SVgroupInfo* pInfo = &pRsp->vgroupInfo[0];
      pInfo->vgId = htonl(pInfo->vgId);
      pInfo->hashBegin = htonl(pInfo->hashBegin);
      pInfo->hashEnd = htonl(pInfo->hashEnd);
      EXPECT_GT(pInfo->vgId, 0);
      EXPECT_EQ(pInfo->hashBegin, 0);
S
Shengliang Guan 已提交
290
      EXPECT_EQ(pInfo->hashEnd, UINT32_MAX / 2 - 1);
S
Shengliang Guan 已提交
291 292
      EXPECT_EQ(pInfo->inUse, 0);
      EXPECT_EQ(pInfo->numOfEps, 1);
S
Shengliang Guan 已提交
293
      SEpAddr* pAddr = &pInfo->epAddr[0];
S
Shengliang Guan 已提交
294
      pAddr->port = htons(pAddr->port);
S
Shengliang Guan 已提交
295
      EXPECT_EQ(pAddr->port, 9030);
S
Shengliang Guan 已提交
296 297 298 299 300 301 302 303 304
      EXPECT_STREQ(pAddr->fqdn, "localhost");
    }

    {
      SVgroupInfo* pInfo = &pRsp->vgroupInfo[1];
      pInfo->vgId = htonl(pInfo->vgId);
      pInfo->hashBegin = htonl(pInfo->hashBegin);
      pInfo->hashEnd = htonl(pInfo->hashEnd);
      EXPECT_GT(pInfo->vgId, 0);
S
Shengliang Guan 已提交
305 306
      EXPECT_EQ(pInfo->hashBegin, UINT32_MAX / 2);
      EXPECT_EQ(pInfo->hashEnd, UINT32_MAX);
S
Shengliang Guan 已提交
307 308
      EXPECT_EQ(pInfo->inUse, 0);
      EXPECT_EQ(pInfo->numOfEps, 1);
S
Shengliang Guan 已提交
309
      SEpAddr* pAddr = &pInfo->epAddr[0];
S
Shengliang Guan 已提交
310
      pAddr->port = htons(pAddr->port);
S
Shengliang Guan 已提交
311
      EXPECT_EQ(pAddr->port, 9030);
S
Shengliang Guan 已提交
312 313 314
      EXPECT_STREQ(pAddr->fqdn, "localhost");
    }
  }
S
Shengliang Guan 已提交
315 316

  {
S
Shengliang Guan 已提交
317
    int32_t contLen = sizeof(SDropDbReq);
S
Shengliang Guan 已提交
318

S
Shengliang Guan 已提交
319
    SDropDbReq* pReq = (SDropDbReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
320 321 322 323 324 325
    strcpy(pReq->db, "1.d2");

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