vnode.cpp 7.6 KB
Newer Older
1 2 3
/**
 * @file db.cpp
 * @author slguan (slguan@taosdata.com)
4
 * @brief DNODE module vnode tests
5 6 7 8 9 10 11
 * @version 0.1
 * @date 2021-12-20
 *
 * @copyright Copyright (c) 2021
 *
 */

S
Shengliang Guan 已提交
12
#include "sut.h"
13

S
Shengliang Guan 已提交
14
class DndTestVnode : public ::testing::Test {
15
 protected:
16
  static void SetUpTestSuite() { test.Init(TD_TMP_DIR_PATH "dvnodeTest", 9115); }
S
Shengliang Guan 已提交
17
  static void TearDownTestSuite() { test.Cleanup(); }
18

S
Shengliang Guan 已提交
19
  static Testbase test;
20 21 22 23 24 25

 public:
  void SetUp() override {}
  void TearDown() override {}
};

S
Shengliang Guan 已提交
26
Testbase DndTestVnode::test;
27

S
Shengliang Guan 已提交
28 29
TEST_F(DndTestVnode, 01_Create_Vnode) {
  for (int i = 0; i < 3; ++i) {
S
Shengliang Guan 已提交
30 31 32 33 34 35
    SCreateVnodeReq createReq = {0};
    createReq.vgId = 2;
    createReq.dnodeId = 1;
    strcpy(createReq.db, "1.d1");
    createReq.dbUid = 9527;
    createReq.vgVersion = 1;
S
Shengliang Guan 已提交
36 37 38 39
    createReq.daysPerFile = 10;
    createReq.daysToKeep0 = 3650;
    createReq.daysToKeep1 = 3650;
    createReq.daysToKeep2 = 3650;
S
Shengliang Guan 已提交
40 41 42 43 44 45 46
    createReq.minRows = 100;
    createReq.minRows = 4096;
    createReq.fsyncPeriod = 3000;
    createReq.walLevel = 1;
    createReq.precision = 0;
    createReq.compression = 2;
    createReq.replica = 1;
S
Shengliang Guan 已提交
47
    createReq.strict = 1;
S
Shengliang Guan 已提交
48 49 50 51 52 53
    createReq.cacheLastRow = 0;
    createReq.selfIndex = 0;
    for (int r = 0; r < createReq.replica; ++r) {
      SReplica* pReplica = &createReq.replicas[r];
      pReplica->id = 1;
      pReplica->port = 9527;
S
Shengliang Guan 已提交
54 55
    }

S
Shengliang Guan 已提交
56 57 58 59
    int32_t contLen = tSerializeSCreateVnodeReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateVnodeReq(pReq, contLen, &createReq);

S
Shengliang Guan 已提交
60 61 62 63 64 65
    SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_VNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    if (i == 0) {
      ASSERT_EQ(pRsp->code, 0);
      test.Restart();
    } else {
66
      ASSERT_EQ(pRsp->code, TSDB_CODE_NODE_ALREADY_DEPLOYED);
67 68
    }
  }
S
Shengliang Guan 已提交
69 70
}

S
Shengliang Guan 已提交
71
TEST_F(DndTestVnode, 02_Alter_Vnode) {
S
Shengliang Guan 已提交
72
  for (int i = 0; i < 3; ++i) {
S
Shengliang Guan 已提交
73 74
    SAlterVnodeReq alterReq = {0};
    alterReq.vgVersion = 2;
S
Shengliang Guan 已提交
75 76 77 78
    alterReq.daysPerFile = 10;
    alterReq.daysToKeep0 = 3650;
    alterReq.daysToKeep1 = 3650;
    alterReq.daysToKeep2 = 3650;
S
Shengliang Guan 已提交
79 80 81
    alterReq.fsyncPeriod = 3000;
    alterReq.walLevel = 1;
    alterReq.replica = 1;
S
Shengliang Guan 已提交
82
    alterReq.strict = 1;
S
Shengliang Guan 已提交
83 84 85 86 87 88
    alterReq.cacheLastRow = 0;
    alterReq.selfIndex = 0;
    for (int r = 0; r < alterReq.replica; ++r) {
      SReplica* pReplica = &alterReq.replicas[r];
      pReplica->id = 1;
      pReplica->port = 9527;
S
Shengliang Guan 已提交
89 90
    }

S
Shengliang Guan 已提交
91 92 93 94
    int32_t contLen = tSerializeSCreateVnodeReq(NULL, 0, &alterReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateVnodeReq(pReq, contLen, &alterReq);

S
Shengliang Guan 已提交
95
    SRpcMsg* pRsp = test.SendReq(TDMT_VND_ALTER_VNODE, pReq, contLen);
S
Shengliang Guan 已提交
96 97 98 99 100 101 102 103 104
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
  }
}

TEST_F(DndTestVnode, 03_Create_Stb) {
  for (int i = 0; i < 1; ++i) {
    SVCreateTbReq req = {0};
    req.ver = 0;
105
    req.dbFName = (char*)"1.db1";
S
Shengliang Guan 已提交
106 107 108 109 110
    req.name = (char*)"stb1";
    req.ttl = 0;
    req.keep = 0;
    req.type = TD_SUPER_TABLE;

C
Cary Xu 已提交
111
    SSchemaEx schemas[2] = {0};
S
Shengliang Guan 已提交
112
    {
C
Cary Xu 已提交
113
      SSchemaEx* pSchema = &schemas[0];
S
Shengliang Guan 已提交
114 115 116 117 118 119
      pSchema->bytes = htonl(8);
      pSchema->type = TSDB_DATA_TYPE_TIMESTAMP;
      strcpy(pSchema->name, "ts");
    }

    {
C
Cary Xu 已提交
120
      SSchemaEx* pSchema = &schemas[1];
S
Shengliang Guan 已提交
121 122 123 124
      pSchema->bytes = htonl(4);
      pSchema->type = TSDB_DATA_TYPE_INT;
      strcpy(pSchema->name, "col1");
    }
C
Cary Xu 已提交
125
    SSchema tagSchemas[3] = {0};
S
Shengliang Guan 已提交
126
    {
C
Cary Xu 已提交
127
      SSchema* pSchema = &tagSchemas[0];
S
Shengliang Guan 已提交
128 129 130 131 132 133
      pSchema->bytes = htonl(2);
      pSchema->type = TSDB_DATA_TYPE_TINYINT;
      strcpy(pSchema->name, "tag1");
    }

    {
C
Cary Xu 已提交
134
      SSchema* pSchema = &tagSchemas[1];
S
Shengliang Guan 已提交
135 136 137 138 139 140
      pSchema->bytes = htonl(8);
      pSchema->type = TSDB_DATA_TYPE_BIGINT;
      strcpy(pSchema->name, "tag2");
    }

    {
C
Cary Xu 已提交
141
      SSchema* pSchema = &tagSchemas[2];
S
Shengliang Guan 已提交
142 143 144 145 146 147 148 149 150
      pSchema->bytes = htonl(16);
      pSchema->type = TSDB_DATA_TYPE_BINARY;
      strcpy(pSchema->name, "tag3");
    }

    req.stbCfg.suid = 9527;
    req.stbCfg.nCols = 2;
    req.stbCfg.pSchema = &schemas[0];
    req.stbCfg.nTagCols = 3;
C
Cary Xu 已提交
151
    req.stbCfg.pTagSchema = &tagSchemas[0];
S
Shengliang Guan 已提交
152

S
Shengliang Guan 已提交
153 154
    int32_t   contLen = tSerializeSVCreateTbReq(NULL, &req) + sizeof(SMsgHead);
    SMsgHead* pHead = (SMsgHead*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
155

S
Shengliang Guan 已提交
156 157
    pHead->contLen = htonl(contLen);
    pHead->vgId = htonl(2);
S
Shengliang Guan 已提交
158

S
Shengliang Guan 已提交
159
    void* pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
S
Shengliang Guan 已提交
160 161
    tSerializeSVCreateTbReq(&pBuf, &req);

S
Shengliang Guan 已提交
162
    SRpcMsg* pRsp = test.SendReq(TDMT_VND_CREATE_STB, (void*)pHead, contLen);
S
Shengliang Guan 已提交
163 164 165 166 167 168 169 170 171 172
    ASSERT_NE(pRsp, nullptr);
    if (i == 0) {
      ASSERT_EQ(pRsp->code, 0);
      test.Restart();
    } else {
      ASSERT_EQ(pRsp->code, TSDB_CODE_TDB_TABLE_ALREADY_EXIST);
    }
  }
}

S
Shengliang Guan 已提交
173
TEST_F(DndTestVnode, 04_Alter_Stb) {
S
Shengliang Guan 已提交
174 175 176
  for (int i = 0; i < 1; ++i) {
    SVCreateTbReq req = {0};
    req.ver = 0;
177
    req.dbFName = (char*)"1.db1";
S
Shengliang Guan 已提交
178 179 180 181 182
    req.name = (char*)"stb1";
    req.ttl = 0;
    req.keep = 0;
    req.type = TD_SUPER_TABLE;

C
Cary Xu 已提交
183
    SSchemaEx schemas[2] = {0};
S
Shengliang Guan 已提交
184
    {
C
Cary Xu 已提交
185
      SSchemaEx* pSchema = &schemas[0];
S
Shengliang Guan 已提交
186 187 188 189 190 191
      pSchema->bytes = htonl(8);
      pSchema->type = TSDB_DATA_TYPE_TIMESTAMP;
      strcpy(pSchema->name, "ts");
    }

    {
C
Cary Xu 已提交
192
      SSchemaEx* pSchema = &schemas[1];
S
Shengliang Guan 已提交
193 194 195 196
      pSchema->bytes = htonl(4);
      pSchema->type = TSDB_DATA_TYPE_INT;
      strcpy(pSchema->name, "col1");
    }
C
Cary Xu 已提交
197
    SSchema tagSchemas[3] = {0};
S
Shengliang Guan 已提交
198
    {
C
Cary Xu 已提交
199
      SSchema* pSchema = &tagSchemas[0];
S
Shengliang Guan 已提交
200 201 202
      pSchema->bytes = htonl(2);
      pSchema->type = TSDB_DATA_TYPE_TINYINT;
      strcpy(pSchema->name, "_tag1");
203
    }
S
Shengliang Guan 已提交
204 205

    {
C
Cary Xu 已提交
206
      SSchema* pSchema = &tagSchemas[1];
S
Shengliang Guan 已提交
207 208 209 210 211 212
      pSchema->bytes = htonl(8);
      pSchema->type = TSDB_DATA_TYPE_BIGINT;
      strcpy(pSchema->name, "_tag2");
    }

    {
C
Cary Xu 已提交
213
      SSchema* pSchema = &tagSchemas[2];
S
Shengliang Guan 已提交
214 215 216 217 218 219 220 221 222
      pSchema->bytes = htonl(16);
      pSchema->type = TSDB_DATA_TYPE_BINARY;
      strcpy(pSchema->name, "_tag3");
    }

    req.stbCfg.suid = 9527;
    req.stbCfg.nCols = 2;
    req.stbCfg.pSchema = &schemas[0];
    req.stbCfg.nTagCols = 3;
C
Cary Xu 已提交
223
    req.stbCfg.pTagSchema = &tagSchemas[0];
S
Shengliang Guan 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236

    int32_t   contLen = tSerializeSVCreateTbReq(NULL, &req) + sizeof(SMsgHead);
    SMsgHead* pHead = (SMsgHead*)rpcMallocCont(contLen);

    pHead->contLen = htonl(contLen);
    pHead->vgId = htonl(2);

    void* pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
    tSerializeSVCreateTbReq(&pBuf, &req);

    SRpcMsg* pRsp = test.SendReq(TDMT_VND_ALTER_STB, (void*)pHead, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
237
  }
S
Shengliang Guan 已提交
238
}
239

S
Shengliang Guan 已提交
240
TEST_F(DndTestVnode, 05_DROP_Stb) {
241
  {
S
Shengliang Guan 已提交
242 243 244 245 246 247
    for (int i = 0; i < 3; ++i) {
      SVDropTbReq req = {0};
      req.ver = 0;
      req.name = (char*)"stb1";
      req.suid = 9599;
      req.type = TD_SUPER_TABLE;
S
Shengliang Guan 已提交
248

S
Shengliang Guan 已提交
249 250
      int32_t   contLen = tSerializeSVDropTbReq(NULL, &req) + sizeof(SMsgHead);
      SMsgHead* pHead = (SMsgHead*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
251

S
Shengliang Guan 已提交
252 253 254 255 256 257 258
      pHead->contLen = htonl(contLen);
      pHead->vgId = htonl(2);

      void* pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
      tSerializeSVDropTbReq(&pBuf, &req);

      SRpcMsg* pRsp = test.SendReq(TDMT_VND_DROP_STB, (void*)pHead, contLen);
S
Shengliang Guan 已提交
259
      ASSERT_NE(pRsp, nullptr);
S
Shengliang Guan 已提交
260
      ASSERT_EQ(pRsp->code, 0);
261
    }
262 263
  }
}
S
Shengliang Guan 已提交
264

S
Shengliang Guan 已提交
265
TEST_F(DndTestVnode, 06_Drop_Vnode) {
S
Shengliang Guan 已提交
266
  for (int i = 0; i < 3; ++i) {
S
Shengliang Guan 已提交
267 268 269 270 271
    SDropVnodeReq dropReq = {0};
    dropReq.vgId = 2;
    dropReq.dnodeId = 1;
    strcpy(dropReq.db, "1.d1");
    dropReq.dbUid = 9527;
S
Shengliang Guan 已提交
272

S
Shengliang Guan 已提交
273 274 275
    int32_t contLen = tSerializeSDropVnodeReq(NULL, 0, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSDropVnodeReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
276 277 278

    SRpcMsg rpcMsg = {0};
    rpcMsg.pCont = pReq;
S
Shengliang Guan 已提交
279
    rpcMsg.contLen = contLen;
S
Shengliang Guan 已提交
280 281 282 283 284 285 286 287
    rpcMsg.msgType = TDMT_DND_DROP_VNODE;

    SRpcMsg* pRsp = test.SendReq(TDMT_DND_DROP_VNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    if (i == 0) {
      ASSERT_EQ(pRsp->code, 0);
      test.Restart();
    } else {
288
      ASSERT_EQ(pRsp->code, TSDB_CODE_NODE_NOT_DEPLOYED);
S
Shengliang Guan 已提交
289 290 291
    }
  }
}