vnodeApiTests.cpp 3.8 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
more  
Hongze Cheng 已提交
63
TEST(vnodeApiTest, vnodeOpen_vnodeClose_test) {
H
more  
Hongze Cheng 已提交
64 65
  GTEST_ASSERT_GE(vnodeInit(), 0);

H
more  
Hongze Cheng 已提交
66 67 68 69
  // Create and open a vnode
  SVnode *pVnode = vnodeOpen("vnode1", NULL);
  ASSERT_NE(pVnode, nullptr);

H
more  
Hongze Cheng 已提交
70 71 72
  tb_uid_t suid = 1638166374163;
  {
    // Create a super table
H
more  
Hongze Cheng 已提交
73 74
    STSchema *pSchema = createBasicSchema();
    STSchema *pTagSchema = createBasicTagSchema();
H
more  
Hongze Cheng 已提交
75 76
    char      tbname[128] = "st";

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

H
more  
Hongze Cheng 已提交
80
    int      zs = vnodeBuildReq(NULL, &vCreateSTbReq, TSDB_MSG_TYPE_CREATE_TABLE);
H
more  
Hongze Cheng 已提交
81 82 83 84 85 86
    SRpcMsg *pMsg = (SRpcMsg *)malloc(sizeof(SRpcMsg) + zs);
    pMsg->contLen = zs;
    pMsg->pCont = POINTER_SHIFT(pMsg, sizeof(SRpcMsg));

    void **pBuf = &(pMsg->pCont);

H
more  
Hongze Cheng 已提交
87 88
    vnodeBuildReq(pBuf, &vCreateSTbReq, TSDB_MSG_TYPE_CREATE_TABLE);
    META_CLEAR_TB_CFG(&vCreateSTbReq);
H
more  
Hongze Cheng 已提交
89 90 91 92 93 94 95

    taosArrayPush(pMsgs, &(pMsg));

    vnodeProcessWMsgs(pVnode, pMsgs);

    free(pMsg);
    taosArrayClear(pMsgs);
H
more  
Hongze Cheng 已提交
96 97
    tdFreeSchema(pSchema);
    tdFreeSchema(pTagSchema);
H
more  
Hongze Cheng 已提交
98 99 100 101 102 103 104 105 106
  }

  {
    // Create some child tables
    int ntables = 1000000;
    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 已提交
107 108
        SKVRow pTag = createBasicTag();
        char   tbname[128];
H
more  
Hongze Cheng 已提交
109
        sprintf(tbname, "tb%d", i * batch + j);
H
more  
Hongze Cheng 已提交
110
        SVnodeReq vCreateCTbReq = VNODE_INIT_CREATE_CTB_REQ(tbname, UINT32_MAX, UINT32_MAX, suid, pTag);
H
more  
Hongze Cheng 已提交
111

H
more  
Hongze Cheng 已提交
112
        int      tz = vnodeBuildReq(NULL, &vCreateCTbReq, TSDB_MSG_TYPE_CREATE_TABLE);
H
more  
Hongze Cheng 已提交
113 114 115 116 117
        SRpcMsg *pMsg = (SRpcMsg *)malloc(sizeof(SRpcMsg) + tz);
        pMsg->contLen = tz;
        pMsg->pCont = POINTER_SHIFT(pMsg, sizeof(*pMsg));
        void **pBuf = &(pMsg->pCont);

H
more  
Hongze Cheng 已提交
118 119
        vnodeBuildReq(pBuf, &vCreateCTbReq, TSDB_MSG_TYPE_CREATE_TABLE);
        META_CLEAR_TB_CFG(&vCreateCTbReq);
H
Hongze Cheng 已提交
120 121

        taosArrayPush(pMsgs, pMsg);
H
more  
Hongze Cheng 已提交
122 123 124 125 126 127 128 129 130 131 132 133
      }

      vnodeProcessWMsgs(pVnode, pMsgs);

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

      taosArrayClear(pMsgs);
    }
  }
H
more  
Hongze Cheng 已提交
134

H
more  
Hongze Cheng 已提交
135 136
  // Close the vnode
  vnodeClose(pVnode);
H
more  
Hongze Cheng 已提交
137 138

  vnodeClear();
H
more  
Hongze Cheng 已提交
139
}
H
more  
Hongze Cheng 已提交
140 141 142 143 144 145 146 147 148 149 150

TEST(vnodeApiTest, vnode_process_create_table) {
  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);
}