db.cpp 9.5 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
 * @brief DNODE module db-msg tests
S
Shengliang Guan 已提交
5
 * @version 0.1
S
Shengliang Guan 已提交
6
 * @date 2021-12-15
S
Shengliang Guan 已提交
7
 *
S
Shengliang Guan 已提交
8
 * @copyright Copyright (c) 2021
S
Shengliang Guan 已提交
9 10 11
 *
 */

S
Shengliang Guan 已提交
12
#include "base.h"
S
Shengliang Guan 已提交
13 14 15

class DndTestDb : public ::testing::Test {
 protected:
S
Shengliang Guan 已提交
16 17
  static void SetUpTestSuite() { test.Init("/tmp/dnode_test_db", 9040); }
  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 DndTestDb::test;
S
Shengliang Guan 已提交
27

S
Shengliang Guan 已提交
28
TEST_F(DndTestDb, 01_ShowDb) {
S
Shengliang Guan 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  test.SendShowMetaMsg(TSDB_MGMT_TABLE_DB, "");
  CHECK_META("show databases", 17);
  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");
  CHECK_SCHEMA(3, TSDB_DATA_TYPE_SMALLINT, 2, "replica");
  CHECK_SCHEMA(4, TSDB_DATA_TYPE_SMALLINT, 2, "quorum");
  CHECK_SCHEMA(5, TSDB_DATA_TYPE_SMALLINT, 2, "days");
  CHECK_SCHEMA(6, TSDB_DATA_TYPE_BINARY, 24 + VARSTR_HEADER_SIZE, "keep0,keep1,keep2");
  CHECK_SCHEMA(7, TSDB_DATA_TYPE_INT, 4, "cache");
  CHECK_SCHEMA(8, TSDB_DATA_TYPE_INT, 4, "blocks");
  CHECK_SCHEMA(9, TSDB_DATA_TYPE_INT, 4, "minrows");
  CHECK_SCHEMA(10, TSDB_DATA_TYPE_INT, 4, "maxrows");
  CHECK_SCHEMA(11, TSDB_DATA_TYPE_TINYINT, 1, "wallevel");
  CHECK_SCHEMA(12, TSDB_DATA_TYPE_INT, 4, "fsync");
  CHECK_SCHEMA(13, TSDB_DATA_TYPE_TINYINT, 1, "comp");
  CHECK_SCHEMA(14, TSDB_DATA_TYPE_TINYINT, 1, "cachelast");
  CHECK_SCHEMA(15, TSDB_DATA_TYPE_BINARY, 3 + VARSTR_HEADER_SIZE, "precision");
  CHECK_SCHEMA(16, TSDB_DATA_TYPE_TINYINT, 1, "update");

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

S
Shengliang Guan 已提交
53
TEST_F(DndTestDb, 02_Create_Alter_Drop_Db) {
S
Shengliang Guan 已提交
54
  {
S
Shengliang Guan 已提交
55 56 57
    int32_t contLen = sizeof(SCreateDbMsg);

    SCreateDbMsg* pReq = (SCreateDbMsg*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
58
    strcpy(pReq->db, "1.d1");
59
    pReq->numOfVgroups = htonl(2);
S
Shengliang Guan 已提交
60 61 62 63 64 65
    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 已提交
66 67
    pReq->minRows = htonl(100);
    pReq->maxRows = htonl(4096);
S
Shengliang Guan 已提交
68 69
    pReq->commitTime = htonl(3600);
    pReq->fsyncPeriod = htonl(3000);
S
Shengliang Guan 已提交
70 71 72 73 74 75 76 77
    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 已提交
78

S
Shengliang Guan 已提交
79
    SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_CREATE_DB, pReq, contLen);
S
Shengliang Guan 已提交
80 81 82 83
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);
  }

S
Shengliang Guan 已提交
84 85 86 87 88
  test.SendShowMetaMsg(TSDB_MGMT_TABLE_DB, "");
  CHECK_META("show databases", 17);

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

S
Shengliang Guan 已提交
107 108 109 110 111 112 113 114 115
  test.SendShowMetaMsg(TSDB_MGMT_TABLE_VGROUP, "1.d1");
  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");

  test.SendShowRetrieveMsg();
  EXPECT_EQ(test.GetShowRows(), 2);
S
Shengliang Guan 已提交
116 117 118 119 120 121 122 123 124
  CheckInt32(1);
  CheckInt32(2);
  CheckInt32(0);
  CheckInt32(0);
  CheckInt16(1);
  CheckInt16(1);
  CheckBinary("master", 9);
  CheckBinary("master", 9);

S
Shengliang Guan 已提交
125
  {
S
Shengliang Guan 已提交
126 127 128
    int32_t contLen = sizeof(SAlterDbMsg);

    SAlterDbMsg* pReq = (SAlterDbMsg*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
129 130 131 132 133 134 135 136 137
    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 已提交
138

S
Shengliang Guan 已提交
139
    SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_ALTER_DB, pReq, contLen);
S
Shengliang Guan 已提交
140 141 142 143
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);
  }

S
Shengliang Guan 已提交
144 145 146
  test.SendShowMetaMsg(TSDB_MGMT_TABLE_DB, "");
  test.SendShowRetrieveMsg();
  EXPECT_EQ(test.GetShowRows(), 1);
S
Shengliang Guan 已提交
147
  CheckBinary("d1", TSDB_DB_NAME_LEN - 1);
S
Shengliang Guan 已提交
148
  CheckTimestamp();
S
Shengliang Guan 已提交
149
  CheckInt16(2);                   // vgroups
S
Shengliang Guan 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163
  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 已提交
164

S
Shengliang Guan 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  // restart
  test.Restart();

  test.SendShowMetaMsg(TSDB_MGMT_TABLE_DB, "");
  CHECK_META("show databases", 17);

  test.SendShowRetrieveMsg();
  EXPECT_EQ(test.GetShowRows(), 1);

  CheckBinary("d1", TSDB_DB_NAME_LEN - 1);
  CheckTimestamp();
  CheckInt16(2);                   // vgroups
  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

  {
    int32_t contLen = sizeof(SDropDbMsg);

    SDropDbMsg* pReq = (SDropDbMsg*)rpcMallocCont(contLen);
    strcpy(pReq->db, "1.d1");

    SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_DROP_DB, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);
  }

  test.SendShowMetaMsg(TSDB_MGMT_TABLE_DB, "");
  CHECK_META("show databases", 17);

  test.SendShowRetrieveMsg();
  EXPECT_EQ(test.GetShowRows(), 0);
S
Shengliang Guan 已提交
208 209 210 211
}

TEST_F(DndTestDb, 03_Create_Use_Restart_Use_Db) {
  {
S
Shengliang Guan 已提交
212 213 214
    int32_t contLen = sizeof(SCreateDbMsg);

    SCreateDbMsg* pReq = (SCreateDbMsg*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
215 216 217 218 219 220 221 222
    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 已提交
223 224
    pReq->minRows = htonl(100);
    pReq->maxRows = htonl(4096);
S
Shengliang Guan 已提交
225 226 227 228 229 230 231 232 233 234 235
    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 已提交
236
    SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_CREATE_DB, pReq, contLen);
S
Shengliang Guan 已提交
237 238 239 240
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);
  }

S
Shengliang Guan 已提交
241 242 243 244 245
  test.SendShowMetaMsg(TSDB_MGMT_TABLE_DB, "");
  CHECK_META("show databases", 17);

  test.SendShowRetrieveMsg();
  EXPECT_EQ(test.GetShowRows(), 1);
S
Shengliang Guan 已提交
246 247 248
  CheckBinary("d2", TSDB_DB_NAME_LEN - 1);

  {
S
Shengliang Guan 已提交
249 250 251
    int32_t contLen = sizeof(SUseDbMsg);

    SUseDbMsg* pReq = (SUseDbMsg*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
252 253 254
    strcpy(pReq->db, "1.d2");
    pReq->vgVersion = htonl(-1);

S
Shengliang Guan 已提交
255
    SRpcMsg* pMsg = test.SendMsg(TSDB_MSG_TYPE_USE_DB, pReq, contLen);
S
Shengliang Guan 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    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 已提交
275
      EXPECT_EQ(pInfo->hashEnd, UINT32_MAX / 2 - 1);
S
Shengliang Guan 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289
      EXPECT_EQ(pInfo->inUse, 0);
      EXPECT_EQ(pInfo->numOfEps, 1);
      SEpAddrMsg* pAddr = &pInfo->epAddr[0];
      pAddr->port = htons(pAddr->port);
      EXPECT_EQ(pAddr->port, 9040);
      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 已提交
290 291
      EXPECT_EQ(pInfo->hashBegin, UINT32_MAX / 2);
      EXPECT_EQ(pInfo->hashEnd, UINT32_MAX);
S
Shengliang Guan 已提交
292 293 294 295 296 297 298 299 300
      EXPECT_EQ(pInfo->inUse, 0);
      EXPECT_EQ(pInfo->numOfEps, 1);
      SEpAddrMsg* pAddr = &pInfo->epAddr[0];
      pAddr->port = htons(pAddr->port);
      EXPECT_EQ(pAddr->port, 9040);
      EXPECT_STREQ(pAddr->fqdn, "localhost");
    }
  }
}