vnode.cpp 7.4 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 30
TEST_F(DndTestVnode, 01_Create_Vnode) {
  for (int i = 0; i < 3; ++i) {
    int32_t contLen = sizeof(SCreateVnodeReq);
31

S
Shengliang Guan 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    SCreateVnodeReq* pReq = (SCreateVnodeReq*)rpcMallocCont(contLen);
    pReq->vgId = htonl(2);
    pReq->dnodeId = htonl(1);
    strcpy(pReq->db, "1.d1");
    pReq->dbUid = htobe64(9527);
    pReq->vgVersion = htonl(1);
    pReq->cacheBlockSize = htonl(16);
    pReq->totalBlocks = htonl(10);
    pReq->daysPerFile = htonl(10);
    pReq->daysToKeep0 = htonl(3650);
    pReq->daysToKeep1 = htonl(3650);
    pReq->daysToKeep2 = htonl(3650);
    pReq->minRows = htonl(100);
    pReq->minRows = htonl(4096);
    pReq->commitTime = htonl(3600);
    pReq->fsyncPeriod = htonl(3000);
    pReq->walLevel = 1;
    pReq->precision = 0;
    pReq->compression = 2;
    pReq->replica = 1;
    pReq->quorum = 1;
    pReq->update = 0;
    pReq->cacheLastRow = 0;
    pReq->selfIndex = 0;
    for (int r = 0; r < pReq->replica; ++r) {
      SReplica* pReplica = &pReq->replicas[r];
      pReplica->id = htonl(1);
      pReplica->port = htons(9527);
    }

    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 {
      ASSERT_EQ(pRsp->code, TSDB_CODE_DND_VNODE_ALREADY_DEPLOYED);
69 70
    }
  }
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

  {
    int32_t contLen = sizeof(SCreateVnodeReq);

    SCreateVnodeReq* pReq = (SCreateVnodeReq*)rpcMallocCont(contLen);
    pReq->vgId = htonl(2);
    pReq->dnodeId = htonl(3);
    strcpy(pReq->db, "1.d1");
    pReq->dbUid = htobe64(9527);
    pReq->vgVersion = htonl(1);
    pReq->cacheBlockSize = htonl(16);
    pReq->totalBlocks = htonl(10);
    pReq->daysPerFile = htonl(10);
    pReq->daysToKeep0 = htonl(3650);
    pReq->daysToKeep1 = htonl(3650);
    pReq->daysToKeep2 = htonl(3650);
    pReq->minRows = htonl(100);
    pReq->minRows = htonl(4096);
    pReq->commitTime = htonl(3600);
    pReq->fsyncPeriod = htonl(3000);
    pReq->walLevel = 1;
    pReq->precision = 0;
    pReq->compression = 2;
    pReq->replica = 1;
    pReq->quorum = 1;
    pReq->update = 0;
    pReq->cacheLastRow = 0;
    pReq->selfIndex = 0;
    for (int r = 0; r < pReq->replica; ++r) {
      SReplica* pReplica = &pReq->replicas[r];
      pReplica->id = htonl(1);
      pReplica->port = htons(9527);
    }

    SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_VNODE, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_DND_VNODE_INVALID_OPTION);
  }
S
Shengliang Guan 已提交
109 110 111 112 113
}

TEST_F(DndTestVnode, 02_ALTER_Vnode) {
  for (int i = 0; i < 3; ++i) {
    int32_t contLen = sizeof(SAlterVnodeReq);
114

S
Shengliang Guan 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    SAlterVnodeReq* pReq = (SAlterVnodeReq*)rpcMallocCont(contLen);
    pReq->vgId = htonl(2);
    pReq->dnodeId = htonl(1);
    strcpy(pReq->db, "1.d1");
    pReq->dbUid = htobe64(9527);
    pReq->vgVersion = htonl(2);
    pReq->cacheBlockSize = htonl(16);
    pReq->totalBlocks = htonl(10);
    pReq->daysPerFile = htonl(10);
    pReq->daysToKeep0 = htonl(3650);
    pReq->daysToKeep1 = htonl(3650);
    pReq->daysToKeep2 = htonl(3650);
    pReq->minRows = htonl(100);
    pReq->minRows = htonl(4096);
    pReq->commitTime = htonl(3600);
    pReq->fsyncPeriod = htonl(3000);
    pReq->walLevel = 1;
    pReq->precision = 0;
    pReq->compression = 2;
    pReq->replica = 1;
    pReq->quorum = 1;
    pReq->update = 0;
    pReq->cacheLastRow = 0;
    pReq->selfIndex = 0;
    for (int r = 0; r < pReq->replica; ++r) {
      SReplica* pReplica = &pReq->replicas[r];
      pReplica->id = htonl(1);
      pReplica->port = htons(9527);
    }

    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;
    req.name = (char*)"stb1";
    req.ttl = 0;
    req.keep = 0;
    req.type = TD_SUPER_TABLE;

    SSchema schemas[5] = {0};
    {
      SSchema* pSchema = &schemas[0];
      pSchema->bytes = htonl(8);
      pSchema->type = TSDB_DATA_TYPE_TIMESTAMP;
      strcpy(pSchema->name, "ts");
    }

    {
      SSchema* pSchema = &schemas[1];
      pSchema->bytes = htonl(4);
      pSchema->type = TSDB_DATA_TYPE_INT;
      strcpy(pSchema->name, "col1");
    }

    {
      SSchema* pSchema = &schemas[2];
      pSchema->bytes = htonl(2);
      pSchema->type = TSDB_DATA_TYPE_TINYINT;
      strcpy(pSchema->name, "tag1");
    }

    {
      SSchema* pSchema = &schemas[3];
      pSchema->bytes = htonl(8);
      pSchema->type = TSDB_DATA_TYPE_BIGINT;
      strcpy(pSchema->name, "tag2");
    }

    {
      SSchema* pSchema = &schemas[4];
      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;
    req.stbCfg.pTagSchema = &schemas[2];

S
Shengliang Guan 已提交
202 203
    int32_t   contLen = tSerializeSVCreateTbReq(NULL, &req) + sizeof(SMsgHead);
    SMsgHead* pHead = (SMsgHead*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
204

S
Shengliang Guan 已提交
205 206
    pHead->contLen = htonl(contLen);
    pHead->vgId = htonl(2);
S
Shengliang Guan 已提交
207

S
Shengliang Guan 已提交
208
    void* pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
S
Shengliang Guan 已提交
209 210
    tSerializeSVCreateTbReq(&pBuf, &req);

S
Shengliang Guan 已提交
211
    SRpcMsg* pRsp = test.SendReq(TDMT_VND_CREATE_STB, (void*)pHead, contLen);
S
Shengliang Guan 已提交
212 213 214 215 216 217 218 219 220 221 222 223
    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);
    }
  }
}

TEST_F(DndTestVnode, 04_ALTER_Stb) {
#if 0
224 225
  {
    for (int i = 0; i < 3; ++i) {
S
Shengliang Guan 已提交
226
      SRpcMsg* pRsp = test.SendReq(TDMT_VND_ALTER_STB, pReq, contLen);
S
Shengliang Guan 已提交
227 228
      ASSERT_NE(pRsp, nullptr);
      ASSERT_EQ(pRsp->code, 0);
229
    }
230
  }
S
Shengliang Guan 已提交
231 232
#endif
}
233

S
Shengliang Guan 已提交
234
TEST_F(DndTestVnode, 05_DROP_Stb) {
235
  {
S
Shengliang Guan 已提交
236 237 238 239 240 241
    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 已提交
242

S
Shengliang Guan 已提交
243 244
      int32_t   contLen = tSerializeSVDropTbReq(NULL, &req) + sizeof(SMsgHead);
      SMsgHead* pHead = (SMsgHead*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
245

S
Shengliang Guan 已提交
246 247 248 249 250 251 252
      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 已提交
253
      ASSERT_NE(pRsp, nullptr);
S
Shengliang Guan 已提交
254
      ASSERT_EQ(pRsp->code, 0);
255
    }
256 257
  }
}
S
Shengliang Guan 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

TEST_F(DndTestVnode, 06_DROP_Vnode) {
  for (int i = 0; i < 3; ++i) {
    int32_t contLen = sizeof(SDropVnodeReq);

    SDropVnodeReq* pReq = (SDropVnodeReq*)rpcMallocCont(contLen);
    pReq->vgId = htonl(2);
    pReq->dnodeId = htonl(1);
    strcpy(pReq->db, "1.d1");
    pReq->dbUid = htobe64(9527);

    SRpcMsg rpcMsg = {0};
    rpcMsg.pCont = pReq;
    rpcMsg.contLen = sizeof(SDropVnodeReq);
    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 {
      ASSERT_EQ(pRsp->code, TSDB_CODE_DND_VNODE_NOT_DEPLOYED);
    }
  }
}