db.cpp 6.9 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 41 42 43 44
    createReq.buffer = -1;
    createReq.pageSize = -1;
    createReq.pages = -1;
    createReq.durationPerFile = 1000;
    createReq.durationToKeep0 = 3650;
    createReq.durationToKeep1 = 3650;
    createReq.durationToKeep2 = 3650;
S
Shengliang Guan 已提交
45 46 47 48
    createReq.minRows = 100;
    createReq.maxRows = 4096;
    createReq.commitTime = 3600;
    createReq.fsyncPeriod = 3000;
S
Shengliang Guan 已提交
49
    createReq.ttl = 1;
S
Shengliang Guan 已提交
50 51 52 53
    createReq.walLevel = 1;
    createReq.precision = 0;
    createReq.compression = 2;
    createReq.replications = 1;
S
Shengliang Guan 已提交
54
    createReq.strict = 1;
S
Shengliang Guan 已提交
55 56 57
    createReq.update = 0;
    createReq.cacheLastRow = 0;
    createReq.ignoreExist = 1;
S
Shengliang Guan 已提交
58 59 60
    createReq.streamMode = 0;
    createReq.singleSTable = 0;
    createReq.numOfRetensions = 0;
S
Shengliang Guan 已提交
61 62 63 64

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

S
Shengliang Guan 已提交
66 67 68
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DB, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
69 70
  }

S
Shengliang Guan 已提交
71 72
  test.SendShowReq(TSDB_MGMT_TABLE_DB, "user_databases", "");
  EXPECT_EQ(test.GetShowRows(), 3);
S
Shengliang Guan 已提交
73

S
Shengliang Guan 已提交
74
  test.SendShowReq(TSDB_MGMT_TABLE_VGROUP, "vgroups", "1.d1");
S
Shengliang Guan 已提交
75
  EXPECT_EQ(test.GetShowRows(), 2);
S
Shengliang Guan 已提交
76

S
Shengliang Guan 已提交
77
  {
S
Shengliang Guan 已提交
78 79
    SAlterDbReq alterdbReq = {0};
    strcpy(alterdbReq.db, "1.d1");
S
Shengliang Guan 已提交
80 81 82 83
    alterdbReq.buffer = 12;
    alterdbReq.durationToKeep0 = 300;
    alterdbReq.durationToKeep1 = 400;
    alterdbReq.durationToKeep2 = 500;
S
Shengliang Guan 已提交
84 85
    alterdbReq.fsyncPeriod = 4000;
    alterdbReq.walLevel = 2;
S
Shengliang Guan 已提交
86
    alterdbReq.strict = 2;
S
Shengliang Guan 已提交
87 88 89 90 91
    alterdbReq.cacheLastRow = 1;

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

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

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

S
Shengliang Guan 已提交
101 102 103
  // restart
  test.Restart();

S
Shengliang Guan 已提交
104 105
  test.SendShowReq(TSDB_MGMT_TABLE_DB, "user_databases", "");
  EXPECT_EQ(test.GetShowRows(), 3);
S
Shengliang Guan 已提交
106 107

  {
S
Shengliang Guan 已提交
108 109
    SDropDbReq dropdbReq = {0};
    strcpy(dropdbReq.db, "1.d1");
S
Shengliang Guan 已提交
110

S
Shengliang Guan 已提交
111 112 113
    int32_t contLen = tSerializeSDropDbReq(NULL, 0, &dropdbReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSDropDbReq(pReq, contLen, &dropdbReq);
S
Shengliang Guan 已提交
114

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

S
Shengliang Guan 已提交
119 120 121
    SDropDbRsp dropdbRsp = {0};
    tDeserializeSDropDbRsp(pRsp->pCont, pRsp->contLen, &dropdbRsp);
    EXPECT_STREQ(dropdbRsp.db, "1.d1");
S
Shengliang Guan 已提交
122 123
  }

S
Shengliang Guan 已提交
124 125
  test.SendShowReq(TSDB_MGMT_TABLE_DB, "user_databases", "");
  EXPECT_EQ(test.GetShowRows(), 2);
S
Shengliang Guan 已提交
126 127
}

S
Shengliang Guan 已提交
128
TEST_F(MndTestDb, 03_Create_Use_Restart_Use_Db) {
S
Shengliang Guan 已提交
129
  {
S
Shengliang Guan 已提交
130 131 132
    SCreateDbReq createReq = {0};
    strcpy(createReq.db, "1.d2");
    createReq.numOfVgroups = 2;
S
Shengliang Guan 已提交
133 134 135 136 137 138 139
    createReq.buffer = -1;
    createReq.pageSize = -1;
    createReq.pages = -1;
    createReq.durationPerFile = 1000;
    createReq.durationToKeep0 = 3650;
    createReq.durationToKeep1 = 3650;
    createReq.durationToKeep2 = 3650;
S
Shengliang Guan 已提交
140 141 142 143
    createReq.minRows = 100;
    createReq.maxRows = 4096;
    createReq.commitTime = 3600;
    createReq.fsyncPeriod = 3000;
S
Shengliang Guan 已提交
144
    createReq.ttl = 1;
S
Shengliang Guan 已提交
145 146 147 148
    createReq.walLevel = 1;
    createReq.precision = 0;
    createReq.compression = 2;
    createReq.replications = 1;
S
Shengliang Guan 已提交
149
    createReq.strict = 1;
S
Shengliang Guan 已提交
150 151 152
    createReq.update = 0;
    createReq.cacheLastRow = 0;
    createReq.ignoreExist = 1;
S
Shengliang Guan 已提交
153 154 155
    createReq.streamMode = 0;
    createReq.singleSTable = 0;
    createReq.numOfRetensions = 0;
S
Shengliang Guan 已提交
156 157 158 159

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

S
Shengliang Guan 已提交
161 162 163
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DB, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
164 165
  }

S
Shengliang Guan 已提交
166 167
  test.SendShowReq(TSDB_MGMT_TABLE_DB, "user_databases", "");
  EXPECT_EQ(test.GetShowRows(), 3);
S
Shengliang Guan 已提交
168

S
Shengliang Guan 已提交
169 170
  uint64_t d2_uid = 0;

S
Shengliang Guan 已提交
171
  {
S
Shengliang Guan 已提交
172 173 174
    SUseDbReq usedbReq = {0};
    strcpy(usedbReq.db, "1.d2");
    usedbReq.vgVersion = -1;
S
Shengliang Guan 已提交
175

S
Shengliang Guan 已提交
176 177 178
    int32_t contLen = tSerializeSUseDbReq(NULL, 0, &usedbReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSUseDbReq(pReq, contLen, &usedbReq);
S
Shengliang Guan 已提交
179

S
Shengliang Guan 已提交
180
    SRpcMsg* pMsg = test.SendReq(TDMT_MND_USE_DB, pReq, contLen);
S
Shengliang Guan 已提交
181 182 183
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);

S
Shengliang Guan 已提交
184 185 186 187 188 189 190
    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 已提交
191 192

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

    {
S
Shengliang Guan 已提交
208 209 210 211
      SVgroupInfo* pInfo = (SVgroupInfo*)taosArrayGet(usedbRsp.pVgroupInfos, 1);
      pInfo->vgId = pInfo->vgId;
      pInfo->hashBegin = pInfo->hashBegin;
      pInfo->hashEnd = pInfo->hashEnd;
S
Shengliang Guan 已提交
212
      EXPECT_GT(pInfo->vgId, 0);
S
Shengliang Guan 已提交
213 214
      EXPECT_EQ(pInfo->hashBegin, UINT32_MAX / 2);
      EXPECT_EQ(pInfo->hashEnd, UINT32_MAX);
L
Liu Jicong 已提交
215 216 217
      EXPECT_EQ(pInfo->epSet.inUse, 0);
      EXPECT_EQ(pInfo->epSet.numOfEps, 1);
      SEp* pAddr = &pInfo->epSet.eps[0];
S
Shengliang Guan 已提交
218
      EXPECT_EQ(pAddr->port, 9030);
S
Shengliang Guan 已提交
219 220
      EXPECT_STREQ(pAddr->fqdn, "localhost");
    }
S
Shengliang Guan 已提交
221 222

    tFreeSUsedbRsp(&usedbRsp);
S
Shengliang Guan 已提交
223
  }
S
Shengliang Guan 已提交
224 225

  {
S
Shengliang Guan 已提交
226 227
    SDropDbReq dropdbReq = {0};
    strcpy(dropdbReq.db, "1.d2");
S
Shengliang Guan 已提交
228

S
Shengliang Guan 已提交
229 230 231
    int32_t contLen = tSerializeSDropDbReq(NULL, 0, &dropdbReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSDropDbReq(pReq, contLen, &dropdbReq);
S
Shengliang Guan 已提交
232 233 234 235

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

S
Shengliang Guan 已提交
237 238 239 240
    SDropDbRsp dropdbRsp = {0};
    tDeserializeSDropDbRsp(pRsp->pCont, pRsp->contLen, &dropdbRsp);
    EXPECT_STREQ(dropdbRsp.db, "1.d2");
    EXPECT_EQ(dropdbRsp.uid, d2_uid);
S
Shengliang Guan 已提交
241
  }
S
Shengliang Guan 已提交
242
}