db.cpp 6.7 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 30
  test.SendShowReq(TSDB_MGMT_TABLE_DB, "user_databases", "");
  EXPECT_EQ(test.GetShowRows(), 2);
S
Shengliang Guan 已提交
31 32
}

S
Shengliang Guan 已提交
33
TEST_F(MndTestDb, 02_Create_Alter_Drop_Db) {
S
Shengliang Guan 已提交
34
  {
S
Shengliang Guan 已提交
35 36 37
    SCreateDbReq createReq = {0};
    strcpy(createReq.db, "1.d1");
    createReq.numOfVgroups = 2;
S
Shengliang Guan 已提交
38 39 40
    createReq.buffer = -1;
    createReq.pageSize = -1;
    createReq.pages = -1;
S
Shengliang Guan 已提交
41 42 43 44
    createReq.daysPerFile = 1000;
    createReq.daysToKeep0 = 3650;
    createReq.daysToKeep1 = 3650;
    createReq.daysToKeep2 = 3650;
S
Shengliang Guan 已提交
45 46 47 48 49 50 51
    createReq.minRows = 100;
    createReq.maxRows = 4096;
    createReq.fsyncPeriod = 3000;
    createReq.walLevel = 1;
    createReq.precision = 0;
    createReq.compression = 2;
    createReq.replications = 1;
S
Shengliang Guan 已提交
52
    createReq.strict = 1;
S
Shengliang Guan 已提交
53 54
    createReq.cacheLastRow = 0;
    createReq.ignoreExist = 1;
S
Shengliang Guan 已提交
55
    createReq.numOfStables = 0;
S
Shengliang Guan 已提交
56
    createReq.numOfRetensions = 0;
S
Shengliang Guan 已提交
57 58 59 60

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

S
Shengliang Guan 已提交
62 63 64
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DB, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
65 66
  }

S
Shengliang Guan 已提交
67 68
  test.SendShowReq(TSDB_MGMT_TABLE_DB, "user_databases", "");
  EXPECT_EQ(test.GetShowRows(), 3);
S
Shengliang Guan 已提交
69

S
Shengliang Guan 已提交
70
  test.SendShowReq(TSDB_MGMT_TABLE_VGROUP, "vgroups", "1.d1");
S
Shengliang Guan 已提交
71
  EXPECT_EQ(test.GetShowRows(), 2);
S
Shengliang Guan 已提交
72

S
Shengliang Guan 已提交
73
  {
S
Shengliang Guan 已提交
74 75
    SAlterDbReq alterdbReq = {0};
    strcpy(alterdbReq.db, "1.d1");
S
Shengliang Guan 已提交
76
    alterdbReq.buffer = 12;
S
Shengliang Guan 已提交
77 78 79
    alterdbReq.daysToKeep0 = 300;
    alterdbReq.daysToKeep1 = 400;
    alterdbReq.daysToKeep2 = 500;
S
Shengliang Guan 已提交
80 81
    alterdbReq.fsyncPeriod = 4000;
    alterdbReq.walLevel = 2;
S
Shengliang Guan 已提交
82
    alterdbReq.strict = 2;
S
Shengliang Guan 已提交
83 84 85 86 87
    alterdbReq.cacheLastRow = 1;

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

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

S
Shengliang Guan 已提交
94 95
  test.SendShowReq(TSDB_MGMT_TABLE_DB, "user_databases", "");
  EXPECT_EQ(test.GetShowRows(), 3);
S
Shengliang Guan 已提交
96

S
Shengliang Guan 已提交
97 98 99
  // restart
  test.Restart();

S
Shengliang Guan 已提交
100 101
  test.SendShowReq(TSDB_MGMT_TABLE_DB, "user_databases", "");
  EXPECT_EQ(test.GetShowRows(), 3);
S
Shengliang Guan 已提交
102 103

  {
S
Shengliang Guan 已提交
104 105
    SDropDbReq dropdbReq = {0};
    strcpy(dropdbReq.db, "1.d1");
S
Shengliang Guan 已提交
106

S
Shengliang Guan 已提交
107 108 109
    int32_t contLen = tSerializeSDropDbReq(NULL, 0, &dropdbReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSDropDbReq(pReq, contLen, &dropdbReq);
S
Shengliang Guan 已提交
110

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

S
Shengliang Guan 已提交
115 116 117
    SDropDbRsp dropdbRsp = {0};
    tDeserializeSDropDbRsp(pRsp->pCont, pRsp->contLen, &dropdbRsp);
    EXPECT_STREQ(dropdbRsp.db, "1.d1");
S
Shengliang Guan 已提交
118 119
  }

S
Shengliang Guan 已提交
120 121
  test.SendShowReq(TSDB_MGMT_TABLE_DB, "user_databases", "");
  EXPECT_EQ(test.GetShowRows(), 2);
S
Shengliang Guan 已提交
122 123
}

S
Shengliang Guan 已提交
124
TEST_F(MndTestDb, 03_Create_Use_Restart_Use_Db) {
S
Shengliang Guan 已提交
125
  {
S
Shengliang Guan 已提交
126 127 128
    SCreateDbReq createReq = {0};
    strcpy(createReq.db, "1.d2");
    createReq.numOfVgroups = 2;
S
Shengliang Guan 已提交
129 130 131
    createReq.buffer = -1;
    createReq.pageSize = -1;
    createReq.pages = -1;
S
Shengliang Guan 已提交
132 133 134 135
    createReq.daysPerFile = 1000;
    createReq.daysToKeep0 = 3650;
    createReq.daysToKeep1 = 3650;
    createReq.daysToKeep2 = 3650;
S
Shengliang Guan 已提交
136 137 138 139 140 141 142
    createReq.minRows = 100;
    createReq.maxRows = 4096;
    createReq.fsyncPeriod = 3000;
    createReq.walLevel = 1;
    createReq.precision = 0;
    createReq.compression = 2;
    createReq.replications = 1;
S
Shengliang Guan 已提交
143
    createReq.strict = 1;
S
Shengliang Guan 已提交
144 145
    createReq.cacheLastRow = 0;
    createReq.ignoreExist = 1;
S
Shengliang Guan 已提交
146
    createReq.numOfStables = 0;
S
Shengliang Guan 已提交
147
    createReq.numOfRetensions = 0;
S
Shengliang Guan 已提交
148 149 150 151

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

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

S
Shengliang Guan 已提交
158 159
  test.SendShowReq(TSDB_MGMT_TABLE_DB, "user_databases", "");
  EXPECT_EQ(test.GetShowRows(), 3);
S
Shengliang Guan 已提交
160

S
Shengliang Guan 已提交
161 162
  uint64_t d2_uid = 0;

S
Shengliang Guan 已提交
163
  {
S
Shengliang Guan 已提交
164 165 166
    SUseDbReq usedbReq = {0};
    strcpy(usedbReq.db, "1.d2");
    usedbReq.vgVersion = -1;
S
Shengliang Guan 已提交
167

S
Shengliang Guan 已提交
168 169 170
    int32_t contLen = tSerializeSUseDbReq(NULL, 0, &usedbReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSUseDbReq(pReq, contLen, &usedbReq);
S
Shengliang Guan 已提交
171

S
Shengliang Guan 已提交
172
    SRpcMsg* pMsg = test.SendReq(TDMT_MND_USE_DB, pReq, contLen);
S
Shengliang Guan 已提交
173 174 175
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);

S
Shengliang Guan 已提交
176 177 178 179 180 181 182
    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 已提交
183 184

    {
S
Shengliang Guan 已提交
185 186 187 188
      SVgroupInfo* pInfo = (SVgroupInfo*)taosArrayGet(usedbRsp.pVgroupInfos, 0);
      pInfo->vgId = pInfo->vgId;
      pInfo->hashBegin = pInfo->hashBegin;
      pInfo->hashEnd = pInfo->hashEnd;
S
Shengliang Guan 已提交
189 190
      EXPECT_GT(pInfo->vgId, 0);
      EXPECT_EQ(pInfo->hashBegin, 0);
S
Shengliang Guan 已提交
191
      EXPECT_EQ(pInfo->hashEnd, UINT32_MAX / 2 - 1);
L
Liu Jicong 已提交
192 193 194
      EXPECT_EQ(pInfo->epSet.inUse, 0);
      EXPECT_EQ(pInfo->epSet.numOfEps, 1);
      SEp* pAddr = &pInfo->epSet.eps[0];
S
Shengliang Guan 已提交
195
      EXPECT_EQ(pAddr->port, 9030);
S
Shengliang Guan 已提交
196 197 198 199
      EXPECT_STREQ(pAddr->fqdn, "localhost");
    }

    {
S
Shengliang Guan 已提交
200 201 202 203
      SVgroupInfo* pInfo = (SVgroupInfo*)taosArrayGet(usedbRsp.pVgroupInfos, 1);
      pInfo->vgId = pInfo->vgId;
      pInfo->hashBegin = pInfo->hashBegin;
      pInfo->hashEnd = pInfo->hashEnd;
S
Shengliang Guan 已提交
204
      EXPECT_GT(pInfo->vgId, 0);
S
Shengliang Guan 已提交
205 206
      EXPECT_EQ(pInfo->hashBegin, UINT32_MAX / 2);
      EXPECT_EQ(pInfo->hashEnd, UINT32_MAX);
L
Liu Jicong 已提交
207 208 209
      EXPECT_EQ(pInfo->epSet.inUse, 0);
      EXPECT_EQ(pInfo->epSet.numOfEps, 1);
      SEp* pAddr = &pInfo->epSet.eps[0];
S
Shengliang Guan 已提交
210
      EXPECT_EQ(pAddr->port, 9030);
S
Shengliang Guan 已提交
211 212
      EXPECT_STREQ(pAddr->fqdn, "localhost");
    }
S
Shengliang Guan 已提交
213 214

    tFreeSUsedbRsp(&usedbRsp);
S
Shengliang Guan 已提交
215
  }
S
Shengliang Guan 已提交
216 217

  {
S
Shengliang Guan 已提交
218 219
    SDropDbReq dropdbReq = {0};
    strcpy(dropdbReq.db, "1.d2");
S
Shengliang Guan 已提交
220

S
Shengliang Guan 已提交
221 222 223
    int32_t contLen = tSerializeSDropDbReq(NULL, 0, &dropdbReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSDropDbReq(pReq, contLen, &dropdbReq);
S
Shengliang Guan 已提交
224 225 226 227

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

S
Shengliang Guan 已提交
229 230 231 232
    SDropDbRsp dropdbRsp = {0};
    tDeserializeSDropDbRsp(pRsp->pCont, pRsp->contLen, &dropdbRsp);
    EXPECT_STREQ(dropdbRsp.db, "1.d2");
    EXPECT_EQ(dropdbRsp.uid, d2_uid);
S
Shengliang Guan 已提交
233
  }
S
Shengliang Guan 已提交
234
}