db.cpp 6.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:
16
  static void SetUpTestSuite() { test.Init(TD_TMP_DIR_PATH "mnode_test_db", 9030); }
S
Shengliang Guan 已提交
17
  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
    createReq.minRows = 100;
    createReq.maxRows = 4096;
47
    createReq.walFsyncPeriod = 3000;
S
Shengliang Guan 已提交
48 49 50 51
    createReq.walLevel = 1;
    createReq.precision = 0;
    createReq.compression = 2;
    createReq.replications = 1;
S
Shengliang Guan 已提交
52
    createReq.strict = 1;
53
    createReq.cacheLast = 0;
S
Shengliang Guan 已提交
54
    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

S
Shengliang Guan 已提交
77
    alterdbReq.buffer = 12;
S
Shengliang Guan 已提交
78 79 80 81 82 83
    alterdbReq.pageSize = -1;
    alterdbReq.pages = -1;
    alterdbReq.daysPerFile = -1;
    alterdbReq.daysToKeep0 = -1;
    alterdbReq.daysToKeep1 = -1;
    alterdbReq.daysToKeep2 = -1;
84
    alterdbReq.walFsyncPeriod = 4000;
S
Shengliang Guan 已提交
85
    alterdbReq.walLevel = 2;
S
Shengliang Guan 已提交
86
    alterdbReq.strict = 1;
87
    alterdbReq.cacheLast = 1;
S
Shengliang Guan 已提交
88
    alterdbReq.replications = 1;
S
Shengliang Guan 已提交
89 90 91 92

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

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

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

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
129
TEST_F(MndTestDb, 03_Create_Use_Restart_Use_Db) {
S
Shengliang Guan 已提交
130
  {
S
Shengliang Guan 已提交
131 132 133
    SCreateDbReq createReq = {0};
    strcpy(createReq.db, "1.d2");
    createReq.numOfVgroups = 2;
S
Shengliang Guan 已提交
134 135 136
    createReq.buffer = -1;
    createReq.pageSize = -1;
    createReq.pages = -1;
S
Shengliang Guan 已提交
137 138 139 140
    createReq.daysPerFile = 1000;
    createReq.daysToKeep0 = 3650;
    createReq.daysToKeep1 = 3650;
    createReq.daysToKeep2 = 3650;
S
Shengliang Guan 已提交
141 142
    createReq.minRows = 100;
    createReq.maxRows = 4096;
143
    createReq.walFsyncPeriod = 3000;
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;
149
    createReq.cacheLast = 0;
S
Shengliang Guan 已提交
150
    createReq.ignoreExist = 1;
S
Shengliang Guan 已提交
151
    createReq.numOfStables = 0;
S
Shengliang Guan 已提交
152
    createReq.numOfRetensions = 0;
S
Shengliang Guan 已提交
153 154 155 156

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

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

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

S
Shengliang Guan 已提交
166 167
  uint64_t d2_uid = 0;

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

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

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

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

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

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

    tFreeSUsedbRsp(&usedbRsp);
S
Shengliang Guan 已提交
220
  }
S
Shengliang Guan 已提交
221 222

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

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

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

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