sma.cpp 9.1 KB
Newer Older
S
sma  
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/**
 * @file sma.cpp
 * @author slguan (slguan@taosdata.com)
 * @brief MNODE module sma tests
 * @version 1.0
 * @date 2022-03-23
 *
 * @copyright Copyright (c) 2022
 *
 */

#include "sut.h"

class MndTestSma : public ::testing::Test {
 protected:
  static void SetUpTestSuite() { test.Init("/tmp/mnode_test_sma", 9035); }
  static void TearDownTestSuite() { test.Cleanup(); }

  static Testbase test;

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

  void* BuildCreateDbReq(const char* dbname, int32_t* pContLen);
  void* BuildDropDbReq(const char* dbname, int32_t* pContLen);
  void* BuildCreateStbReq(const char* stbname, int32_t* pContLen);
  void* BuildDropStbReq(const char* stbname, int32_t* pContLen);
S
sma  
Shengliang Guan 已提交
29 30 31 32 33 34
  void* BuildCreateBSmaStbReq(const char* stbname, int32_t* pContLen);
  void* BuildCreateTSmaReq(const char* smaname, const char* stbname, int8_t igExists, const char* expr,
                           const char* tagsFilter, const char* sql, const char* ast, int32_t* pContLen);
  void* BuildDropTSmaReq(const char* smaname, int8_t igNotExists, int32_t* pContLen);

  void PushField(SArray* pArray, int32_t bytes, int8_t type, const char* name);
S
sma  
Shengliang Guan 已提交
35 36 37 38 39 40 41 42 43 44
};

Testbase MndTestSma::test;

void* MndTestSma::BuildCreateDbReq(const char* dbname, int32_t* pContLen) {
  SCreateDbReq createReq = {0};
  strcpy(createReq.db, dbname);
  createReq.numOfVgroups = 2;
  createReq.cacheBlockSize = 16;
  createReq.totalBlocks = 10;
45 46 47 48
  createReq.daysPerFile = 10 * 1440;
  createReq.daysToKeep0 = 3650 * 1440;
  createReq.daysToKeep1 = 3650 * 1440;
  createReq.daysToKeep2 = 3650 * 1440;
S
sma  
Shengliang Guan 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  createReq.minRows = 100;
  createReq.maxRows = 4096;
  createReq.commitTime = 3600;
  createReq.fsyncPeriod = 3000;
  createReq.walLevel = 1;
  createReq.precision = 0;
  createReq.compression = 2;
  createReq.replications = 1;
  createReq.quorum = 1;
  createReq.update = 0;
  createReq.cacheLastRow = 0;
  createReq.ignoreExist = 1;

  int32_t contLen = tSerializeSCreateDbReq(NULL, 0, &createReq);
  void*   pReq = rpcMallocCont(contLen);
  tSerializeSCreateDbReq(pReq, contLen, &createReq);

  *pContLen = contLen;
  return pReq;
}

void* MndTestSma::BuildDropDbReq(const char* dbname, int32_t* pContLen) {
  SDropDbReq dropdbReq = {0};
  strcpy(dropdbReq.db, dbname);

  int32_t contLen = tSerializeSDropDbReq(NULL, 0, &dropdbReq);
  void*   pReq = rpcMallocCont(contLen);
  tSerializeSDropDbReq(pReq, contLen, &dropdbReq);

  *pContLen = contLen;
  return pReq;
}

S
sma  
Shengliang Guan 已提交
82 83 84 85 86 87 88 89
void MndTestSma::PushField(SArray* pArray, int32_t bytes, int8_t type, const char* name) {
  SField field = {0};
  field.bytes = bytes;
  field.type = type;
  strcpy(field.name, name);
  taosArrayPush(pArray, &field);
}

S
sma  
Shengliang Guan 已提交
90 91 92 93 94 95 96 97 98
void* MndTestSma::BuildCreateStbReq(const char* stbname, int32_t* pContLen) {
  SMCreateStbReq createReq = {0};
  createReq.numOfColumns = 3;
  createReq.numOfTags = 1;
  createReq.igExists = 0;
  createReq.pColumns = taosArrayInit(createReq.numOfColumns, sizeof(SField));
  createReq.pTags = taosArrayInit(createReq.numOfTags, sizeof(SField));
  strcpy(createReq.name, stbname);

S
sma  
Shengliang Guan 已提交
99 100 101 102
  PushField(createReq.pColumns, 8, TSDB_DATA_TYPE_TIMESTAMP, "ts");
  PushField(createReq.pColumns, 2, TSDB_DATA_TYPE_TINYINT, "col1");
  PushField(createReq.pColumns, 8, TSDB_DATA_TYPE_BIGINT, "col2");
  PushField(createReq.pTags, 2, TSDB_DATA_TYPE_TINYINT, "tag1");
S
sma  
Shengliang Guan 已提交
103

S
sma  
Shengliang Guan 已提交
104 105 106 107 108 109 110
  int32_t tlen = tSerializeSMCreateStbReq(NULL, 0, &createReq);
  void*   pHead = rpcMallocCont(tlen);
  tSerializeSMCreateStbReq(pHead, tlen, &createReq);
  tFreeSMCreateStbReq(&createReq);
  *pContLen = tlen;
  return pHead;
}
S
sma  
Shengliang Guan 已提交
111

S
sma  
Shengliang Guan 已提交
112 113 114 115 116 117 118 119 120 121
void* MndTestSma::BuildCreateBSmaStbReq(const char* stbname, int32_t* pContLen) {
  SMCreateStbReq createReq = {0};
  createReq.numOfColumns = 3;
  createReq.numOfTags = 1;
  createReq.numOfSmas = 1;
  createReq.igExists = 0;
  createReq.pColumns = taosArrayInit(createReq.numOfColumns, sizeof(SField));
  createReq.pTags = taosArrayInit(createReq.numOfTags, sizeof(SField));
  createReq.pSmas = taosArrayInit(createReq.numOfSmas, sizeof(SField));
  strcpy(createReq.name, stbname);
S
sma  
Shengliang Guan 已提交
122

S
sma  
Shengliang Guan 已提交
123 124 125 126 127
  PushField(createReq.pColumns, 8, TSDB_DATA_TYPE_TIMESTAMP, "ts");
  PushField(createReq.pColumns, 2, TSDB_DATA_TYPE_TINYINT, "col1");
  PushField(createReq.pColumns, 8, TSDB_DATA_TYPE_BIGINT, "col2");
  PushField(createReq.pTags, 2, TSDB_DATA_TYPE_TINYINT, "tag1");
  PushField(createReq.pSmas, 2, TSDB_DATA_TYPE_TINYINT, "col1");
S
sma  
Shengliang Guan 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

  int32_t tlen = tSerializeSMCreateStbReq(NULL, 0, &createReq);
  void*   pHead = rpcMallocCont(tlen);
  tSerializeSMCreateStbReq(pHead, tlen, &createReq);
  tFreeSMCreateStbReq(&createReq);
  *pContLen = tlen;
  return pHead;
}

void* MndTestSma::BuildDropStbReq(const char* stbname, int32_t* pContLen) {
  SMDropStbReq dropstbReq = {0};
  strcpy(dropstbReq.name, stbname);

  int32_t contLen = tSerializeSMDropStbReq(NULL, 0, &dropstbReq);
  void*   pReq = rpcMallocCont(contLen);
  tSerializeSMDropStbReq(pReq, contLen, &dropstbReq);

  *pContLen = contLen;
  return pReq;
}

S
sma  
Shengliang Guan 已提交
149 150
void* MndTestSma::BuildCreateTSmaReq(const char* smaname, const char* stbname, int8_t igExists, const char* expr,
                                     const char* tagsFilter, const char* sql, const char* ast, int32_t* pContLen) {
S
sma  
Shengliang Guan 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
  SMCreateSmaReq createReq = {0};
  strcpy(createReq.name, smaname);
  strcpy(createReq.stb, stbname);
  createReq.igExists = igExists;
  createReq.intervalUnit = 1;
  createReq.slidingUnit = 2;
  createReq.timezone = 3;
  createReq.dstVgId = 4;
  createReq.interval = 10;
  createReq.offset = 5;
  createReq.sliding = 6;
  createReq.expr = (char*)expr;
  createReq.exprLen = strlen(createReq.expr) + 1;
  createReq.tagsFilter = (char*)tagsFilter;
  createReq.tagsFilterLen = strlen(createReq.tagsFilter) + 1;
  createReq.sql = (char*)sql;
  createReq.sqlLen = strlen(createReq.sql) + 1;
S
sma  
Shengliang Guan 已提交
168
  createReq.ast = (char*)ast;
S
sma  
Shengliang Guan 已提交
169 170 171 172 173 174 175 176 177
  createReq.astLen = strlen(createReq.ast) + 1;

  int32_t tlen = tSerializeSMCreateSmaReq(NULL, 0, &createReq);
  void*   pHead = rpcMallocCont(tlen);
  tSerializeSMCreateSmaReq(pHead, tlen, &createReq);
  *pContLen = tlen;
  return pHead;
}

S
sma  
Shengliang Guan 已提交
178
void* MndTestSma::BuildDropTSmaReq(const char* smaname, int8_t igNotExists, int32_t* pContLen) {
S
sma  
Shengliang Guan 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191
  SMDropSmaReq dropsmaReq = {0};
  dropsmaReq.igNotExists = igNotExists;
  strcpy(dropsmaReq.name, smaname);

  int32_t contLen = tSerializeSMDropSmaReq(NULL, 0, &dropsmaReq);
  void*   pReq = rpcMallocCont(contLen);
  tSerializeSMDropSmaReq(pReq, contLen, &dropsmaReq);

  *pContLen = contLen;
  return pReq;
}

TEST_F(MndTestSma, 01_Create_Show_Meta_Drop_Restart_Stb) {
S
Shengliang Guan 已提交
192
  #if 0
S
sma  
Shengliang Guan 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
  const char* dbname = "1.d1";
  const char* stbname = "1.d1.stb";
  const char* smaname = "1.d1.sma";
  int32_t     contLen = 0;
  void*       pReq;
  SRpcMsg*    pRsp;

  {
    pReq = BuildCreateDbReq(dbname, &contLen);
    pRsp = test.SendReq(TDMT_MND_CREATE_DB, pReq, contLen);
    ASSERT_EQ(pRsp->code, 0);
  }

  {
    pReq = BuildCreateStbReq(stbname, &contLen);
    pRsp = test.SendReq(TDMT_MND_CREATE_STB, pReq, contLen);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
210
    test.SendShowReq(TSDB_MGMT_TABLE_STB, "user_stables",dbname);
S
sma  
Shengliang Guan 已提交
211 212 213
    test.SendShowRetrieveReq();
    EXPECT_EQ(test.GetShowRows(), 1);
  }
S
Shengliang Guan 已提交
214

S
sma  
Shengliang Guan 已提交
215
  {
S
sma  
Shengliang Guan 已提交
216
    pReq = BuildCreateTSmaReq(smaname, stbname, 0, "expr", "tagsFilter", "sql", "ast", &contLen);
S
sma  
Shengliang Guan 已提交
217 218
    pRsp = test.SendReq(TDMT_MND_CREATE_SMA, pReq, contLen);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
219
    test.SendShowReq(TSDB_MGMT_TABLE_INDEX, dbname);
S
sma  
Shengliang Guan 已提交
220 221 222 223 224 225 226 227
    test.SendShowRetrieveReq();
    EXPECT_EQ(test.GetShowRows(), 1);
  }

  // restart
  test.Restart();

  {
S
Shengliang Guan 已提交
228
    test.SendShowReq(TSDB_MGMT_TABLE_INDEX, dbname);
S
sma  
Shengliang Guan 已提交
229 230 231 232 233 234 235 236 237 238
    CHECK_META("show indexes", 3);
    test.SendShowRetrieveReq();
    EXPECT_EQ(test.GetShowRows(), 1);

    CheckBinary("sma", TSDB_INDEX_NAME_LEN);
    CheckTimestamp();
    CheckBinary("stb", TSDB_TABLE_NAME_LEN);
  }

  {
S
sma  
Shengliang Guan 已提交
239
     pReq = BuildDropTSmaReq(smaname, 0, &contLen);
S
sma  
Shengliang Guan 已提交
240 241
    pRsp = test.SendReq(TDMT_MND_DROP_SMA, pReq, contLen);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
242
    test.SendShowReq(TSDB_MGMT_TABLE_INDEX, dbname);
S
sma  
Shengliang Guan 已提交
243 244 245
    test.SendShowRetrieveReq();
    EXPECT_EQ(test.GetShowRows(), 0);
  }
S
sma  
Shengliang Guan 已提交
246
#endif  
S
sma  
Shengliang Guan 已提交
247
}
S
sma  
Shengliang Guan 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

TEST_F(MndTestSma, 02_Create_Show_Meta_Drop_Restart_BSma) {
  const char* dbname = "1.d1";
  const char* stbname = "1.d1.bsmastb";
  int32_t     contLen = 0;
  void*       pReq;
  SRpcMsg*    pRsp;

  {
    pReq = BuildCreateDbReq(dbname, &contLen);
    pRsp = test.SendReq(TDMT_MND_CREATE_DB, pReq, contLen);
    ASSERT_EQ(pRsp->code, 0);
  }

  {
    pReq = BuildCreateBSmaStbReq(stbname, &contLen);
    pRsp = test.SendReq(TDMT_MND_CREATE_STB, pReq, contLen);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
266
    test.SendShowReq(TSDB_MGMT_TABLE_STB, "user_stables",dbname);
S
sma  
Shengliang Guan 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    EXPECT_EQ(test.GetShowRows(), 1);
  }

  test.Restart();

  {
    pReq = BuildCreateBSmaStbReq(stbname, &contLen);
    pRsp = test.SendReq(TDMT_MND_CREATE_STB, pReq, contLen);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_STB_ALREADY_EXIST);
  }

  {
    pReq = BuildDropStbReq(stbname, &contLen);
    pRsp = test.SendReq(TDMT_MND_DROP_STB, pReq, contLen);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
282
    test.SendShowReq(TSDB_MGMT_TABLE_STB, "user_stables",dbname);
S
sma  
Shengliang Guan 已提交
283 284 285 286 287 288 289 290 291
    EXPECT_EQ(test.GetShowRows(), 0);
  }

  {
    pReq = BuildDropStbReq(stbname, &contLen);
    pRsp = test.SendReq(TDMT_MND_DROP_STB, pReq, contLen);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_STB_NOT_EXIST);
  }
}