db.cpp 10.8 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
  static void SetUpTestSuite() { test.Init("/tmp/mnode_test_db", 9030); }
  static void TearDownTestSuite() { test.Cleanup(); }
S
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19
  static Testbase test;
S
Shengliang Guan 已提交
20 21 22 23 24 25

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

S
Shengliang Guan 已提交
26
Testbase MndTestDb::test;
S
Shengliang Guan 已提交
27

S
Shengliang Guan 已提交
28
TEST_F(MndTestDb, 01_ShowDb) {
S
Shengliang Guan 已提交
29
  test.SendShowMetaReq(TSDB_MGMT_TABLE_DB, "");
30
  CHECK_META("show databases", 17);
S
Shengliang Guan 已提交
31 32 33
  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 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47
  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");
48
//  CHECK_SCHEMA(17, TSDB_DATA_TYPE_TINYINT, 1, "update");
S
Shengliang Guan 已提交
49

S
Shengliang Guan 已提交
50
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
51
  EXPECT_EQ(test.GetShowRows(), 0);
S
Shengliang Guan 已提交
52 53
}

S
Shengliang Guan 已提交
54
TEST_F(MndTestDb, 02_Create_Alter_Drop_Db) {
S
Shengliang Guan 已提交
55
  {
S
Shengliang Guan 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    SCreateDbReq createReq = {0};
    strcpy(createReq.db, "1.d1");
    createReq.numOfVgroups = 2;
    createReq.cacheBlockSize = 16;
    createReq.totalBlocks = 10;
    createReq.daysPerFile = 10;
    createReq.daysToKeep0 = 3650;
    createReq.daysToKeep1 = 3650;
    createReq.daysToKeep2 = 3650;
    createReq.minRows = 100;
    createReq.maxRows = 4096;
    createReq.commitTime = 3600;
    createReq.fsyncPeriod = 3000;
    createReq.walLevel = 1;
    createReq.precision = 0;
    createReq.compression = 2;
    createReq.replications = 1;
    createReq.quorum = 1;
    createReq.update = 0;
    createReq.cacheLastRow = 0;
    createReq.ignoreExist = 1;

    int32_t contLen = tSerializeSCreateDbReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateDbReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
81

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

S
Shengliang Guan 已提交
87
  test.SendShowMetaReq(TSDB_MGMT_TABLE_DB, "");
88
  CHECK_META("show databases", 17);
S
Shengliang Guan 已提交
89

S
Shengliang Guan 已提交
90
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
91
  EXPECT_EQ(test.GetShowRows(), 1);
S
Shengliang Guan 已提交
92
  CheckBinary("d1", TSDB_DB_NAME_LEN - 1);
S
Shengliang Guan 已提交
93
  CheckTimestamp();
S
Shengliang Guan 已提交
94
  CheckInt16(2);                      // vgroups
S
Shengliang Guan 已提交
95
  CheckInt32(0);                      // ntables
S
Shengliang Guan 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109
  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 已提交
110

S
Shengliang Guan 已提交
111
  test.SendShowMetaReq(TSDB_MGMT_TABLE_VGROUP, "1.d1");
S
Shengliang Guan 已提交
112 113 114 115 116 117
  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 已提交
118
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
119
  EXPECT_EQ(test.GetShowRows(), 2);
S
Shengliang Guan 已提交
120
  CheckInt32(2);
121
  CheckInt32(3);
S
Shengliang Guan 已提交
122 123
  IgnoreInt32();
  IgnoreInt32();
S
Shengliang Guan 已提交
124 125 126 127 128
  CheckInt16(1);
  CheckInt16(1);
  CheckBinary("master", 9);
  CheckBinary("master", 9);

S
Shengliang Guan 已提交
129
  {
S
Shengliang Guan 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143
    SAlterDbReq alterdbReq = {0};
    strcpy(alterdbReq.db, "1.d1");
    alterdbReq.totalBlocks = 12;
    alterdbReq.daysToKeep0 = 300;
    alterdbReq.daysToKeep1 = 400;
    alterdbReq.daysToKeep2 = 500;
    alterdbReq.fsyncPeriod = 4000;
    alterdbReq.walLevel = 2;
    alterdbReq.quorum = 2;
    alterdbReq.cacheLastRow = 1;

    int32_t contLen = tSerializeSAlterDbReq(NULL, 0, &alterdbReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSAlterDbReq(pReq, contLen, &alterdbReq);
S
Shengliang Guan 已提交
144

S
Shengliang Guan 已提交
145 146 147
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_ALTER_DB, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
148 149
  }

S
Shengliang Guan 已提交
150 151
  test.SendShowMetaReq(TSDB_MGMT_TABLE_DB, "");
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
152
  EXPECT_EQ(test.GetShowRows(), 1);
S
Shengliang Guan 已提交
153
  CheckBinary("d1", TSDB_DB_NAME_LEN - 1);
S
Shengliang Guan 已提交
154
  CheckTimestamp();
S
Shengliang Guan 已提交
155
  CheckInt16(2);                   // vgroups
S
Shengliang Guan 已提交
156
  CheckInt32(0);                   // tables
S
Shengliang Guan 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170
  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 已提交
171

S
Shengliang Guan 已提交
172 173 174
  // restart
  test.Restart();

S
Shengliang Guan 已提交
175
  test.SendShowMetaReq(TSDB_MGMT_TABLE_DB, "");
176
  CHECK_META("show databases", 17);
S
Shengliang Guan 已提交
177

S
Shengliang Guan 已提交
178
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
179 180 181 182 183
  EXPECT_EQ(test.GetShowRows(), 1);

  CheckBinary("d1", TSDB_DB_NAME_LEN - 1);
  CheckTimestamp();
  CheckInt16(2);                   // vgroups
S
Shengliang Guan 已提交
184
  CheckInt32(0);                   // tables
S
Shengliang Guan 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  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 已提交
201 202
    SDropDbReq dropdbReq = {0};
    strcpy(dropdbReq.db, "1.d1");
S
Shengliang Guan 已提交
203

S
Shengliang Guan 已提交
204 205 206
    int32_t contLen = tSerializeSDropDbReq(NULL, 0, &dropdbReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSDropDbReq(pReq, contLen, &dropdbReq);
S
Shengliang Guan 已提交
207

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

S
Shengliang Guan 已提交
212 213 214
    SDropDbRsp dropdbRsp = {0};
    tDeserializeSDropDbRsp(pRsp->pCont, pRsp->contLen, &dropdbRsp);
    EXPECT_STREQ(dropdbRsp.db, "1.d1");
S
Shengliang Guan 已提交
215 216
  }

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

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

S
Shengliang Guan 已提交
224
TEST_F(MndTestDb, 03_Create_Use_Restart_Use_Db) {
S
Shengliang Guan 已提交
225
  {
S
Shengliang Guan 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    SCreateDbReq createReq = {0};
    strcpy(createReq.db, "1.d2");
    createReq.numOfVgroups = 2;
    createReq.cacheBlockSize = 16;
    createReq.totalBlocks = 10;
    createReq.daysPerFile = 10;
    createReq.daysToKeep0 = 3650;
    createReq.daysToKeep1 = 3650;
    createReq.daysToKeep2 = 3650;
    createReq.minRows = 100;
    createReq.maxRows = 4096;
    createReq.commitTime = 3600;
    createReq.fsyncPeriod = 3000;
    createReq.walLevel = 1;
    createReq.precision = 0;
    createReq.compression = 2;
    createReq.replications = 1;
    createReq.quorum = 1;
    createReq.update = 0;
    createReq.cacheLastRow = 0;
    createReq.ignoreExist = 1;

    int32_t contLen = tSerializeSCreateDbReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateDbReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
251

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

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

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

S
Shengliang Guan 已提交
264 265
  uint64_t d2_uid = 0;

S
Shengliang Guan 已提交
266
  {
S
Shengliang Guan 已提交
267 268 269
    SUseDbReq usedbReq = {0};
    strcpy(usedbReq.db, "1.d2");
    usedbReq.vgVersion = -1;
S
Shengliang Guan 已提交
270

S
Shengliang Guan 已提交
271 272 273
    int32_t contLen = tSerializeSUseDbReq(NULL, 0, &usedbReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSUseDbReq(pReq, contLen, &usedbReq);
S
Shengliang Guan 已提交
274

S
Shengliang Guan 已提交
275
    SRpcMsg* pMsg = test.SendReq(TDMT_MND_USE_DB, pReq, contLen);
S
Shengliang Guan 已提交
276 277 278
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);

S
Shengliang Guan 已提交
279 280 281 282 283 284 285
    SUseDbRsp usedbRsp = {0};
    tDeserializeSUseDbRsp(pMsg->pCont, pMsg->contLen, &usedbRsp);
    EXPECT_STREQ(usedbRsp.db, "1.d2");
    EXPECT_EQ(usedbRsp.vgVersion, 1);
    EXPECT_EQ(usedbRsp.vgNum, 2);
    EXPECT_EQ(usedbRsp.hashMethod, 1);
    d2_uid = usedbRsp.uid;
S
Shengliang Guan 已提交
286 287

    {
S
Shengliang Guan 已提交
288 289 290 291
      SVgroupInfo* pInfo = (SVgroupInfo*)taosArrayGet(usedbRsp.pVgroupInfos, 0);
      pInfo->vgId = pInfo->vgId;
      pInfo->hashBegin = pInfo->hashBegin;
      pInfo->hashEnd = pInfo->hashEnd;
S
Shengliang Guan 已提交
292 293
      EXPECT_GT(pInfo->vgId, 0);
      EXPECT_EQ(pInfo->hashBegin, 0);
S
Shengliang Guan 已提交
294
      EXPECT_EQ(pInfo->hashEnd, UINT32_MAX / 2 - 1);
L
Liu Jicong 已提交
295 296 297
      EXPECT_EQ(pInfo->epSet.inUse, 0);
      EXPECT_EQ(pInfo->epSet.numOfEps, 1);
      SEp* pAddr = &pInfo->epSet.eps[0];
S
Shengliang Guan 已提交
298
      EXPECT_EQ(pAddr->port, 9030);
S
Shengliang Guan 已提交
299 300 301 302
      EXPECT_STREQ(pAddr->fqdn, "localhost");
    }

    {
S
Shengliang Guan 已提交
303 304 305 306
      SVgroupInfo* pInfo = (SVgroupInfo*)taosArrayGet(usedbRsp.pVgroupInfos, 1);
      pInfo->vgId = pInfo->vgId;
      pInfo->hashBegin = pInfo->hashBegin;
      pInfo->hashEnd = pInfo->hashEnd;
S
Shengliang Guan 已提交
307
      EXPECT_GT(pInfo->vgId, 0);
S
Shengliang Guan 已提交
308 309
      EXPECT_EQ(pInfo->hashBegin, UINT32_MAX / 2);
      EXPECT_EQ(pInfo->hashEnd, UINT32_MAX);
L
Liu Jicong 已提交
310 311 312
      EXPECT_EQ(pInfo->epSet.inUse, 0);
      EXPECT_EQ(pInfo->epSet.numOfEps, 1);
      SEp* pAddr = &pInfo->epSet.eps[0];
S
Shengliang Guan 已提交
313
      EXPECT_EQ(pAddr->port, 9030);
S
Shengliang Guan 已提交
314 315
      EXPECT_STREQ(pAddr->fqdn, "localhost");
    }
S
Shengliang Guan 已提交
316 317

    tFreeSUsedbRsp(&usedbRsp);
S
Shengliang Guan 已提交
318
  }
S
Shengliang Guan 已提交
319 320

  {
S
Shengliang Guan 已提交
321 322
    SDropDbReq dropdbReq = {0};
    strcpy(dropdbReq.db, "1.d2");
S
Shengliang Guan 已提交
323

S
Shengliang Guan 已提交
324 325 326
    int32_t contLen = tSerializeSDropDbReq(NULL, 0, &dropdbReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSDropDbReq(pReq, contLen, &dropdbReq);
S
Shengliang Guan 已提交
327 328 329 330

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

S
Shengliang Guan 已提交
332 333 334 335
    SDropDbRsp dropdbRsp = {0};
    tDeserializeSDropDbRsp(pRsp->pCont, pRsp->contLen, &dropdbRsp);
    EXPECT_STREQ(dropdbRsp.db, "1.d2");
    EXPECT_EQ(dropdbRsp.uid, d2_uid);
S
Shengliang Guan 已提交
336
  }
S
Shengliang Guan 已提交
337
}