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
  vnodeDestroy("vnode1");

H
more  
Hongze Cheng 已提交
97
  GTEST_ASSERT_GE(vnodeInit(2), 0);
H
more  
Hongze Cheng 已提交
98

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

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

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

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

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

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

    taosArrayPush(pMsgs, &(pMsg));

    vnodeProcessWMsgs(pVnode, pMsgs);

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

  {
    // Create some child tables
H
more  
Hongze Cheng 已提交
136
    int ntables = 1000000;
H
more  
Hongze Cheng 已提交
137 138 139 140
    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 已提交
141 142
        SKVRow pTag = createBasicTag();
        char   tbname[128];
H
more  
Hongze Cheng 已提交
143
        sprintf(tbname, "tb%d", i * batch + j);
H
more  
Hongze Cheng 已提交
144
        SVnodeReq vCreateCTbReq = VNODE_INIT_CREATE_CTB_REQ(tbname, UINT32_MAX, UINT32_MAX, suid, pTag);
H
more  
Hongze Cheng 已提交
145

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

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

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

      vnodeProcessWMsgs(pVnode, pMsgs);

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

H
Hongze Cheng 已提交
167
      taosArrayDestroy(pMsgs);
H
more  
Hongze Cheng 已提交
168

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

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

  vnodeClear();
H
more  
Hongze Cheng 已提交
177
}
H
more  
Hongze Cheng 已提交
178

H
Hongze Cheng 已提交
179
TEST(vnodeApiTest, DISABLED_vnode_process_create_table) {
H
more  
Hongze Cheng 已提交
180 181 182 183 184 185 186 187 188
  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);
}