vnodeApiTests.cpp 4.6 KB
Newer Older
H
more  
Hongze Cheng 已提交
1 2 3
#include <gtest/gtest.h>
#include <iostream>

H
more  
Hongze Cheng 已提交
4 5
#include "vnode.h"

H
more  
Hongze Cheng 已提交
6 7 8 9 10 11 12 13 14 15 16 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 59 60 61 62
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);

  for (int i = 10; i < 12; i++) {
    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 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#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 已提交
83
TEST(vnodeApiTest, vnodeOpen_vnodeClose_test) {
H
more  
Hongze Cheng 已提交
84 85
  GTEST_ASSERT_GE(vnodeInit(), 0);

H
more  
Hongze Cheng 已提交
86 87 88 89
  // Create and open a vnode
  SVnode *pVnode = vnodeOpen("vnode1", NULL);
  ASSERT_NE(pVnode, nullptr);

H
more  
Hongze Cheng 已提交
90 91 92
  tb_uid_t suid = 1638166374163;
  {
    // Create a super table
H
more  
Hongze Cheng 已提交
93 94
    STSchema *pSchema = createBasicSchema();
    STSchema *pTagSchema = createBasicTagSchema();
H
more  
Hongze Cheng 已提交
95 96
    char      tbname[128] = "st";

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

H
more  
Hongze Cheng 已提交
100
    int      zs = vnodeBuildReq(NULL, &vCreateSTbReq, TSDB_MSG_TYPE_CREATE_TABLE);
H
more  
Hongze Cheng 已提交
101
    SRpcMsg *pMsg = (SRpcMsg *)malloc(sizeof(SRpcMsg) + zs);
H
Hongze Cheng 已提交
102
    pMsg->msgType = TSDB_MSG_TYPE_CREATE_TABLE;
H
more  
Hongze Cheng 已提交
103 104 105
    pMsg->contLen = zs;
    pMsg->pCont = POINTER_SHIFT(pMsg, sizeof(SRpcMsg));

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

H
Hongze Cheng 已提交
108
    vnodeBuildReq(&pBuf, &vCreateSTbReq, TSDB_MSG_TYPE_CREATE_TABLE);
H
more  
Hongze Cheng 已提交
109
    META_CLEAR_TB_CFG(&vCreateSTbReq);
H
more  
Hongze Cheng 已提交
110 111 112 113 114 115

    taosArrayPush(pMsgs, &(pMsg));

    vnodeProcessWMsgs(pVnode, pMsgs);

    free(pMsg);
H
Hongze Cheng 已提交
116
    taosArrayDestroy(pMsgs);
H
more  
Hongze Cheng 已提交
117 118
    tdFreeSchema(pSchema);
    tdFreeSchema(pTagSchema);
H
more  
Hongze Cheng 已提交
119 120 121 122
  }

  {
    // Create some child tables
H
Hongze Cheng 已提交
123
    int ntables = 100000;
H
more  
Hongze Cheng 已提交
124 125 126 127
    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 已提交
128 129
        SKVRow pTag = createBasicTag();
        char   tbname[128];
H
more  
Hongze Cheng 已提交
130
        sprintf(tbname, "tb%d", i * batch + j);
H
more  
Hongze Cheng 已提交
131
        SVnodeReq vCreateCTbReq = VNODE_INIT_CREATE_CTB_REQ(tbname, UINT32_MAX, UINT32_MAX, suid, pTag);
H
more  
Hongze Cheng 已提交
132

H
more  
Hongze Cheng 已提交
133
        int      tz = vnodeBuildReq(NULL, &vCreateCTbReq, TSDB_MSG_TYPE_CREATE_TABLE);
H
more  
Hongze Cheng 已提交
134
        SRpcMsg *pMsg = (SRpcMsg *)malloc(sizeof(SRpcMsg) + tz);
H
Hongze Cheng 已提交
135
        pMsg->msgType = TSDB_MSG_TYPE_CREATE_TABLE;
H
more  
Hongze Cheng 已提交
136 137
        pMsg->contLen = tz;
        pMsg->pCont = POINTER_SHIFT(pMsg, sizeof(*pMsg));
H
Hongze Cheng 已提交
138
        void *pBuf = pMsg->pCont;
H
more  
Hongze Cheng 已提交
139

H
Hongze Cheng 已提交
140
        vnodeBuildReq(&pBuf, &vCreateCTbReq, TSDB_MSG_TYPE_CREATE_TABLE);
H
more  
Hongze Cheng 已提交
141
        META_CLEAR_TB_CFG(&vCreateCTbReq);
H
Hongze Cheng 已提交
142
        free(pTag);
H
Hongze Cheng 已提交
143

H
Hongze Cheng 已提交
144
        taosArrayPush(pMsgs, &(pMsg));
H
more  
Hongze Cheng 已提交
145 146 147 148 149 150 151 152 153
      }

      vnodeProcessWMsgs(pVnode, pMsgs);

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

H
Hongze Cheng 已提交
154
      taosArrayDestroy(pMsgs);
H
more  
Hongze Cheng 已提交
155

H
Hongze Cheng 已提交
156
      // std::cout << "the " << i << "th batch is created" << std::endl;
H
more  
Hongze Cheng 已提交
157 158
    }
  }
H
more  
Hongze Cheng 已提交
159

H
more  
Hongze Cheng 已提交
160 161
  // Close the vnode
  vnodeClose(pVnode);
H
more  
Hongze Cheng 已提交
162 163

  vnodeClear();
H
more  
Hongze Cheng 已提交
164
}
H
more  
Hongze Cheng 已提交
165

H
Hongze Cheng 已提交
166
TEST(vnodeApiTest, DISABLED_vnode_process_create_table) {
H
more  
Hongze Cheng 已提交
167 168 169 170 171 172 173 174 175
  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);
}