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 38 39
    SCreateDbReq createReq = {0};
    strcpy(createReq.db, "1.d1");
    createReq.numOfVgroups = 2;
    createReq.cacheBlockSize = 16;
    createReq.totalBlocks = 10;
S
Shengliang Guan 已提交
40
    createReq.daysPerFile = 1000;
S
Shengliang Guan 已提交
41 42 43 44 45 46 47
    createReq.daysToKeep0 = 3650;
    createReq.daysToKeep1 = 3650;
    createReq.daysToKeep2 = 3650;
    createReq.minRows = 100;
    createReq.maxRows = 4096;
    createReq.commitTime = 3600;
    createReq.fsyncPeriod = 3000;
S
Shengliang Guan 已提交
48
    createReq.ttl = 1;
S
Shengliang Guan 已提交
49 50 51 52
    createReq.walLevel = 1;
    createReq.precision = 0;
    createReq.compression = 2;
    createReq.replications = 1;
S
Shengliang Guan 已提交
53
    createReq.strict = 1;
S
Shengliang Guan 已提交
54 55 56
    createReq.update = 0;
    createReq.cacheLastRow = 0;
    createReq.ignoreExist = 1;
S
Shengliang Guan 已提交
57 58 59
    createReq.streamMode = 0;
    createReq.singleSTable = 0;
    createReq.numOfRetensions = 0;
S
Shengliang Guan 已提交
60 61 62 63

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

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

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

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

S
Shengliang Guan 已提交
76
  {
S
Shengliang Guan 已提交
77 78 79 80 81 82 83 84
    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;
S
Shengliang Guan 已提交
85
    alterdbReq.strict = 2;
S
Shengliang Guan 已提交
86 87 88 89 90
    alterdbReq.cacheLastRow = 1;

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

92
    taosMsleep(1000); // Wait for the vnode to become the leader
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 133 134
    SCreateDbReq createReq = {0};
    strcpy(createReq.db, "1.d2");
    createReq.numOfVgroups = 2;
    createReq.cacheBlockSize = 16;
    createReq.totalBlocks = 10;
S
Shengliang Guan 已提交
135
    createReq.daysPerFile = 1000;
S
Shengliang Guan 已提交
136 137 138 139 140 141 142
    createReq.daysToKeep0 = 3650;
    createReq.daysToKeep1 = 3650;
    createReq.daysToKeep2 = 3650;
    createReq.minRows = 100;
    createReq.maxRows = 4096;
    createReq.commitTime = 3600;
    createReq.fsyncPeriod = 3000;
S
Shengliang Guan 已提交
143
    createReq.ttl = 1;
S
Shengliang Guan 已提交
144 145 146 147
    createReq.walLevel = 1;
    createReq.precision = 0;
    createReq.compression = 2;
    createReq.replications = 1;
S
Shengliang Guan 已提交
148
    createReq.strict = 1;
S
Shengliang Guan 已提交
149 150 151
    createReq.update = 0;
    createReq.cacheLastRow = 0;
    createReq.ignoreExist = 1;
S
Shengliang Guan 已提交
152 153 154
    createReq.streamMode = 0;
    createReq.singleSTable = 0;
    createReq.numOfRetensions = 0;
S
Shengliang Guan 已提交
155 156 157 158

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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