vnode.cpp 8.1 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:
S
Shengliang Guan 已提交
16
  static void SetUpTestSuite() { test.Init("/tmp/dnode_test_vnode", 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    SCreateVnodeReq createReq = {0};
    createReq.vgId = 2;
    createReq.dnodeId = 1;
    strcpy(createReq.db, "1.d1");
    createReq.dbUid = 9527;
    createReq.vgVersion = 1;
    createReq.cacheBlockSize = 16;
    createReq.totalBlocks = 10;
    createReq.daysPerFile = 10;
    createReq.daysToKeep0 = 3650;
    createReq.daysToKeep1 = 3650;
    createReq.daysToKeep2 = 3650;
    createReq.minRows = 100;
    createReq.minRows = 4096;
    createReq.commitTime = 3600;
    createReq.fsyncPeriod = 3000;
    createReq.walLevel = 1;
    createReq.precision = 0;
    createReq.compression = 2;
    createReq.replica = 1;
S
Shengliang Guan 已提交
50
    createReq.strict = 1;
S
Shengliang Guan 已提交
51 52 53 54 55 56 57
    createReq.update = 0;
    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 已提交
58 59
    }

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

S
Shengliang Guan 已提交
64 65 66 67 68 69
    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 {
70
      ASSERT_EQ(pRsp->code, TSDB_CODE_NODE_ALREADY_DEPLOYED);
71 72
    }
  }
S
Shengliang Guan 已提交
73 74
}

S
Shengliang Guan 已提交
75
TEST_F(DndTestVnode, 02_Alter_Vnode) {
S
Shengliang Guan 已提交
76
  for (int i = 0; i < 3; ++i) {
S
Shengliang Guan 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    SAlterVnodeReq alterReq = {0};
    alterReq.vgId = 2;
    alterReq.dnodeId = 1;
    strcpy(alterReq.db, "1.d1");
    alterReq.dbUid = 9527;
    alterReq.vgVersion = 2;
    alterReq.cacheBlockSize = 16;
    alterReq.totalBlocks = 10;
    alterReq.daysPerFile = 10;
    alterReq.daysToKeep0 = 3650;
    alterReq.daysToKeep1 = 3650;
    alterReq.daysToKeep2 = 3650;
    alterReq.minRows = 100;
    alterReq.minRows = 4096;
    alterReq.commitTime = 3600;
    alterReq.fsyncPeriod = 3000;
    alterReq.walLevel = 1;
    alterReq.precision = 0;
    alterReq.compression = 2;
    alterReq.replica = 1;
S
Shengliang Guan 已提交
97
    alterReq.strict = 1;
S
Shengliang Guan 已提交
98 99 100 101 102 103 104
    alterReq.update = 0;
    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 已提交
105 106
    }

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

S
Shengliang Guan 已提交
111 112 113 114 115 116 117 118 119 120
    SRpcMsg* pRsp = test.SendReq(TDMT_DND_ALTER_VNODE, pReq, contLen);
    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;
121
    req.dbFName = (char*)"1.db1";
S
Shengliang Guan 已提交
122 123 124 125 126
    req.name = (char*)"stb1";
    req.ttl = 0;
    req.keep = 0;
    req.type = TD_SUPER_TABLE;

C
Cary Xu 已提交
127
    SSchemaEx schemas[2] = {0};
S
Shengliang Guan 已提交
128
    {
C
Cary Xu 已提交
129
      SSchemaEx* pSchema = &schemas[0];
S
Shengliang Guan 已提交
130 131 132 133 134 135
      pSchema->bytes = htonl(8);
      pSchema->type = TSDB_DATA_TYPE_TIMESTAMP;
      strcpy(pSchema->name, "ts");
    }

    {
C
Cary Xu 已提交
136
      SSchemaEx* pSchema = &schemas[1];
S
Shengliang Guan 已提交
137 138 139 140
      pSchema->bytes = htonl(4);
      pSchema->type = TSDB_DATA_TYPE_INT;
      strcpy(pSchema->name, "col1");
    }
C
Cary Xu 已提交
141
    SSchema tagSchemas[3] = {0};
S
Shengliang Guan 已提交
142
    {
C
Cary Xu 已提交
143
      SSchema* pSchema = &tagSchemas[0];
S
Shengliang Guan 已提交
144 145 146 147 148 149
      pSchema->bytes = htonl(2);
      pSchema->type = TSDB_DATA_TYPE_TINYINT;
      strcpy(pSchema->name, "tag1");
    }

    {
C
Cary Xu 已提交
150
      SSchema* pSchema = &tagSchemas[1];
S
Shengliang Guan 已提交
151 152 153 154 155 156
      pSchema->bytes = htonl(8);
      pSchema->type = TSDB_DATA_TYPE_BIGINT;
      strcpy(pSchema->name, "tag2");
    }

    {
C
Cary Xu 已提交
157
      SSchema* pSchema = &tagSchemas[2];
S
Shengliang Guan 已提交
158 159 160 161 162 163 164 165 166
      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 已提交
167
    req.stbCfg.pTagSchema = &tagSchemas[0];
S
Shengliang Guan 已提交
168

S
Shengliang Guan 已提交
169 170
    int32_t   contLen = tSerializeSVCreateTbReq(NULL, &req) + sizeof(SMsgHead);
    SMsgHead* pHead = (SMsgHead*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
171

S
Shengliang Guan 已提交
172 173
    pHead->contLen = htonl(contLen);
    pHead->vgId = htonl(2);
S
Shengliang Guan 已提交
174

S
Shengliang Guan 已提交
175
    void* pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
S
Shengliang Guan 已提交
176 177
    tSerializeSVCreateTbReq(&pBuf, &req);

S
Shengliang Guan 已提交
178
    SRpcMsg* pRsp = test.SendReq(TDMT_VND_CREATE_STB, (void*)pHead, contLen);
S
Shengliang Guan 已提交
179 180 181 182 183 184 185 186 187 188
    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 已提交
189
TEST_F(DndTestVnode, 04_Alter_Stb) {
S
Shengliang Guan 已提交
190 191 192
  for (int i = 0; i < 1; ++i) {
    SVCreateTbReq req = {0};
    req.ver = 0;
193
    req.dbFName = (char*)"1.db1";
S
Shengliang Guan 已提交
194 195 196 197 198
    req.name = (char*)"stb1";
    req.ttl = 0;
    req.keep = 0;
    req.type = TD_SUPER_TABLE;

C
Cary Xu 已提交
199
    SSchemaEx schemas[2] = {0};
S
Shengliang Guan 已提交
200
    {
C
Cary Xu 已提交
201
      SSchemaEx* pSchema = &schemas[0];
S
Shengliang Guan 已提交
202 203 204 205 206 207
      pSchema->bytes = htonl(8);
      pSchema->type = TSDB_DATA_TYPE_TIMESTAMP;
      strcpy(pSchema->name, "ts");
    }

    {
C
Cary Xu 已提交
208
      SSchemaEx* pSchema = &schemas[1];
S
Shengliang Guan 已提交
209 210 211 212
      pSchema->bytes = htonl(4);
      pSchema->type = TSDB_DATA_TYPE_INT;
      strcpy(pSchema->name, "col1");
    }
C
Cary Xu 已提交
213
    SSchema tagSchemas[3] = {0};
S
Shengliang Guan 已提交
214
    {
C
Cary Xu 已提交
215
      SSchema* pSchema = &tagSchemas[0];
S
Shengliang Guan 已提交
216 217 218
      pSchema->bytes = htonl(2);
      pSchema->type = TSDB_DATA_TYPE_TINYINT;
      strcpy(pSchema->name, "_tag1");
219
    }
S
Shengliang Guan 已提交
220 221

    {
C
Cary Xu 已提交
222
      SSchema* pSchema = &tagSchemas[1];
S
Shengliang Guan 已提交
223 224 225 226 227 228
      pSchema->bytes = htonl(8);
      pSchema->type = TSDB_DATA_TYPE_BIGINT;
      strcpy(pSchema->name, "_tag2");
    }

    {
C
Cary Xu 已提交
229
      SSchema* pSchema = &tagSchemas[2];
S
Shengliang Guan 已提交
230 231 232 233 234 235 236 237 238
      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 已提交
239
    req.stbCfg.pTagSchema = &tagSchemas[0];
S
Shengliang Guan 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252

    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);
253
  }
S
Shengliang Guan 已提交
254
}
255

S
Shengliang Guan 已提交
256
TEST_F(DndTestVnode, 05_DROP_Stb) {
257
  {
S
Shengliang Guan 已提交
258 259 260 261 262 263
    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 已提交
264

S
Shengliang Guan 已提交
265 266
      int32_t   contLen = tSerializeSVDropTbReq(NULL, &req) + sizeof(SMsgHead);
      SMsgHead* pHead = (SMsgHead*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
267

S
Shengliang Guan 已提交
268 269 270 271 272 273 274
      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 已提交
275
      ASSERT_NE(pRsp, nullptr);
S
Shengliang Guan 已提交
276
      ASSERT_EQ(pRsp->code, 0);
277
    }
278 279
  }
}
S
Shengliang Guan 已提交
280

S
Shengliang Guan 已提交
281
TEST_F(DndTestVnode, 06_Drop_Vnode) {
S
Shengliang Guan 已提交
282
  for (int i = 0; i < 3; ++i) {
S
Shengliang Guan 已提交
283 284 285 286 287
    SDropVnodeReq dropReq = {0};
    dropReq.vgId = 2;
    dropReq.dnodeId = 1;
    strcpy(dropReq.db, "1.d1");
    dropReq.dbUid = 9527;
S
Shengliang Guan 已提交
288

S
Shengliang Guan 已提交
289 290 291
    int32_t contLen = tSerializeSDropVnodeReq(NULL, 0, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSDropVnodeReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
292 293 294

    SRpcMsg rpcMsg = {0};
    rpcMsg.pCont = pReq;
S
Shengliang Guan 已提交
295
    rpcMsg.contLen = contLen;
S
Shengliang Guan 已提交
296 297 298 299 300 301 302 303
    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 {
304
      ASSERT_EQ(pRsp->code, TSDB_CODE_NODE_NOT_DEPLOYED);
S
Shengliang Guan 已提交
305 306 307
    }
  }
}