vnodeApiTests.cpp 4.8 KB
Newer Older
H
more  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11
/**
 * @file vnodeApiTests.cpp
 * @author hzcheng (hzcheng@taosdata.com)
 * @brief VNODE module API tests
 * @version 0.1
 * @date 2021-12-13
 *
 * @copyright Copyright (c) 2021
 *
 */

H
more  
Hongze Cheng 已提交
12 13 14
#include <gtest/gtest.h>
#include <iostream>

H
more  
Hongze Cheng 已提交
15 16
#include "vnode.h"

H
more  
Hongze Cheng 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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
static STSchema *createBasicSchema() {
  STSchemaBuilder sb;
  STSchema *      pSchema = NULL;

  tdInitTSchemaBuilder(&sb, 0);

  tdAddColToSchema(&sb, TSDB_DATA_TYPE_TIMESTAMP, 0, 0);
  for (int i = 1; i < 10; i++) {
    tdAddColToSchema(&sb, TSDB_DATA_TYPE_INT, i, 0);
  }

  pSchema = tdGetSchemaFromBuilder(&sb);

  tdDestroyTSchemaBuilder(&sb);

  return pSchema;
}

static STSchema *createBasicTagSchema() {
  STSchemaBuilder sb;
  STSchema *      pSchema = NULL;

  tdInitTSchemaBuilder(&sb, 0);

  tdAddColToSchema(&sb, TSDB_DATA_TYPE_TIMESTAMP, 0, 0);
  for (int i = 10; i < 12; i++) {
    tdAddColToSchema(&sb, TSDB_DATA_TYPE_BINARY, i, 20);
  }

  pSchema = tdGetSchemaFromBuilder(&sb);

  tdDestroyTSchemaBuilder(&sb);

  return pSchema;
}

static SKVRow createBasicTag() {
  SKVRowBuilder rb;
  SKVRow        pTag;

  tdInitKVRowBuilder(&rb);

H
Hongze Cheng 已提交
59
  for (int i = 0; i < 2; i++) {
H
more  
Hongze Cheng 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73
    void *pVal = malloc(sizeof(VarDataLenT) + strlen("foo"));
    varDataLen(pVal) = strlen("foo");
    memcpy(varDataVal(pVal), "foo", strlen("foo"));

    tdAddColToKVRow(&rb, i, TSDB_DATA_TYPE_BINARY, pVal);
    free(pVal);
  }

  pTag = tdGetKVRowFromBuilder(&rb);
  tdDestroyKVRowBuilder(&rb);

  return pTag;
}

H
Hongze Cheng 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#if 0
TEST(vnodeApiTest, test_create_table_encode_and_decode_function) {
  tb_uid_t  suid = 1638166374163;
  STSchema *pSchema = createBasicSchema();
  STSchema *pTagSchema = createBasicTagSchema();
  char      tbname[128] = "st";
  char *    buffer = new char[1024];
  void *    pBuf = (void *)buffer;
  SVnodeReq vCreateSTbReq = VNODE_INIT_CREATE_STB_REQ(tbname, UINT32_MAX, UINT32_MAX, suid, pSchema, pTagSchema);

  vnodeBuildReq(&pBuf, &vCreateSTbReq, TSDB_MSG_TYPE_CREATE_TABLE);

  SVnodeReq decoded_req;

  vnodeParseReq(buffer, &decoded_req, TSDB_MSG_TYPE_CREATE_TABLE);

  int k = 10;
}
#endif

H
more  
Hongze Cheng 已提交
94
TEST(vnodeApiTest, vnodeOpen_vnodeClose_test) {
H
more  
Hongze Cheng 已提交
95 96
  GTEST_ASSERT_GE(vnodeInit(), 0);

H
more  
Hongze Cheng 已提交
97 98 99 100
  // Create and open a vnode
  SVnode *pVnode = vnodeOpen("vnode1", NULL);
  ASSERT_NE(pVnode, nullptr);

H
more  
Hongze Cheng 已提交
101 102 103
  tb_uid_t suid = 1638166374163;
  {
    // Create a super table
H
more  
Hongze Cheng 已提交
104 105
    STSchema *pSchema = createBasicSchema();
    STSchema *pTagSchema = createBasicTagSchema();
H
more  
Hongze Cheng 已提交
106 107
    char      tbname[128] = "st";

H
more  
Hongze Cheng 已提交
108
    SArray *  pMsgs = (SArray *)taosArrayInit(1, sizeof(SRpcMsg *));
H
more  
Hongze Cheng 已提交
109
    SVnodeReq vCreateSTbReq = VNODE_INIT_CREATE_STB_REQ(tbname, UINT32_MAX, UINT32_MAX, suid, pSchema, pTagSchema);
H
more  
Hongze Cheng 已提交
110

H
more  
Hongze Cheng 已提交
111
    int      zs = vnodeBuildReq(NULL, &vCreateSTbReq, TSDB_MSG_TYPE_CREATE_TABLE);
H
more  
Hongze Cheng 已提交
112
    SRpcMsg *pMsg = (SRpcMsg *)malloc(sizeof(SRpcMsg) + zs);
H
Hongze Cheng 已提交
113
    pMsg->msgType = TSDB_MSG_TYPE_CREATE_TABLE;
H
more  
Hongze Cheng 已提交
114 115 116
    pMsg->contLen = zs;
    pMsg->pCont = POINTER_SHIFT(pMsg, sizeof(SRpcMsg));

H
Hongze Cheng 已提交
117
    void *pBuf = pMsg->pCont;
H
more  
Hongze Cheng 已提交
118

H
Hongze Cheng 已提交
119
    vnodeBuildReq(&pBuf, &vCreateSTbReq, TSDB_MSG_TYPE_CREATE_TABLE);
H
more  
Hongze Cheng 已提交
120
    META_CLEAR_TB_CFG(&vCreateSTbReq);
H
more  
Hongze Cheng 已提交
121 122 123 124 125 126

    taosArrayPush(pMsgs, &(pMsg));

    vnodeProcessWMsgs(pVnode, pMsgs);

    free(pMsg);
H
Hongze Cheng 已提交
127
    taosArrayDestroy(pMsgs);
H
more  
Hongze Cheng 已提交
128 129
    tdFreeSchema(pSchema);
    tdFreeSchema(pTagSchema);
H
more  
Hongze Cheng 已提交
130 131 132 133
  }

  {
    // Create some child tables
H
more  
Hongze Cheng 已提交
134
    int ntables = 1000000;
H
more  
Hongze Cheng 已提交
135 136 137 138
    int batch = 10;
    for (int i = 0; i < ntables / batch; i++) {
      SArray *pMsgs = (SArray *)taosArrayInit(batch, sizeof(SRpcMsg *));
      for (int j = 0; j < batch; j++) {
H
more  
Hongze Cheng 已提交
139 140
        SKVRow pTag = createBasicTag();
        char   tbname[128];
H
more  
Hongze Cheng 已提交
141
        sprintf(tbname, "tb%d", i * batch + j);
H
more  
Hongze Cheng 已提交
142
        SVnodeReq vCreateCTbReq = VNODE_INIT_CREATE_CTB_REQ(tbname, UINT32_MAX, UINT32_MAX, suid, pTag);
H
more  
Hongze Cheng 已提交
143

H
more  
Hongze Cheng 已提交
144
        int      tz = vnodeBuildReq(NULL, &vCreateCTbReq, TSDB_MSG_TYPE_CREATE_TABLE);
H
more  
Hongze Cheng 已提交
145
        SRpcMsg *pMsg = (SRpcMsg *)malloc(sizeof(SRpcMsg) + tz);
H
Hongze Cheng 已提交
146
        pMsg->msgType = TSDB_MSG_TYPE_CREATE_TABLE;
H
more  
Hongze Cheng 已提交
147 148
        pMsg->contLen = tz;
        pMsg->pCont = POINTER_SHIFT(pMsg, sizeof(*pMsg));
H
Hongze Cheng 已提交
149
        void *pBuf = pMsg->pCont;
H
more  
Hongze Cheng 已提交
150

H
Hongze Cheng 已提交
151
        vnodeBuildReq(&pBuf, &vCreateCTbReq, TSDB_MSG_TYPE_CREATE_TABLE);
H
more  
Hongze Cheng 已提交
152
        META_CLEAR_TB_CFG(&vCreateCTbReq);
H
Hongze Cheng 已提交
153
        free(pTag);
H
Hongze Cheng 已提交
154

H
Hongze Cheng 已提交
155
        taosArrayPush(pMsgs, &(pMsg));
H
more  
Hongze Cheng 已提交
156 157 158 159 160 161 162 163 164
      }

      vnodeProcessWMsgs(pVnode, pMsgs);

      for (int j = 0; j < batch; j++) {
        SRpcMsg *pMsg = *(SRpcMsg **)taosArrayPop(pMsgs);
        free(pMsg);
      }

H
Hongze Cheng 已提交
165
      taosArrayDestroy(pMsgs);
H
more  
Hongze Cheng 已提交
166

H
Hongze Cheng 已提交
167
      // std::cout << "the " << i << "th batch is created" << std::endl;
H
more  
Hongze Cheng 已提交
168 169
    }
  }
H
more  
Hongze Cheng 已提交
170

H
more  
Hongze Cheng 已提交
171 172
  // Close the vnode
  vnodeClose(pVnode);
H
more  
Hongze Cheng 已提交
173 174

  vnodeClear();
H
more  
Hongze Cheng 已提交
175
}
H
more  
Hongze Cheng 已提交
176

H
Hongze Cheng 已提交
177
TEST(vnodeApiTest, DISABLED_vnode_process_create_table) {
H
more  
Hongze Cheng 已提交
178 179 180 181 182 183 184 185 186
  STSchema *       pSchema = NULL;
  STSchema *       pTagSchema = NULL;
  char             stname[15];
  SVCreateTableReq pReq = META_INIT_STB_CFG(stname, UINT32_MAX, UINT32_MAX, 0, pSchema, pTagSchema);

  int k = 10;

  META_CLEAR_TB_CFG(pReq);
}