profile.cpp 10.4 KB
Newer Older
S
Shengliang Guan 已提交
1
/**
S
Shengliang Guan 已提交
2
 * @file profile.cpp
S
Shengliang Guan 已提交
3
 * @author slguan (slguan@taosdata.com)
S
Shengliang Guan 已提交
4 5 6
 * @brief MNODE module profile tests
 * @version 1.0
 * @date 2022-01-06
S
Shengliang Guan 已提交
7
 *
S
Shengliang Guan 已提交
8
 * @copyright Copyright (c) 2022
S
Shengliang Guan 已提交
9 10 11
 *
 */

S
Shengliang Guan 已提交
12
#include "sut.h"
S
Shengliang Guan 已提交
13

S
Shengliang Guan 已提交
14
class MndTestProfile : public ::testing::Test {
S
Shengliang Guan 已提交
15
 protected:
S
Shengliang Guan 已提交
16
  static void SetUpTestSuite() { test.Init("/tmp/mnode_test_profile", 9031); }
S
Shengliang Guan 已提交
17
  static void TearDownTestSuite() { test.Cleanup(); }
S
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19
  static Testbase test;
S
Shengliang Guan 已提交
20
  static int32_t  connId;
S
Shengliang Guan 已提交
21 22 23 24

 public:
  void SetUp() override {}
  void TearDown() override {}
S
Shengliang Guan 已提交
25 26
};

S
Shengliang Guan 已提交
27 28
Testbase MndTestProfile::test;
int32_t  MndTestProfile::connId;
S
Shengliang Guan 已提交
29

S
Shengliang Guan 已提交
30 31
TEST_F(MndTestProfile, 01_ConnectMsg) {
  int32_t contLen = sizeof(SConnectReq);
S
Shengliang Guan 已提交
32

S
Shengliang Guan 已提交
33
  SConnectReq* pReq = (SConnectReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
34
  pReq->pid = htonl(1234);
S
Shengliang Guan 已提交
35
  strcpy(pReq->app, "mnode_test_profile");
S
Shengliang Guan 已提交
36 37
  strcpy(pReq->db, "");

S
Shengliang Guan 已提交
38
  SRpcMsg* pMsg = test.SendReq(TDMT_MND_CONNECT, pReq, contLen);
S
Shengliang Guan 已提交
39
  ASSERT_NE(pMsg, nullptr);
S
Shengliang Guan 已提交
40
  ASSERT_EQ(pMsg->code, 0);
S
Shengliang Guan 已提交
41

S
Shengliang Guan 已提交
42
  SConnectRsp* pRsp = (SConnectRsp*)pMsg->pCont;
S
Shengliang Guan 已提交
43
  ASSERT_NE(pRsp, nullptr);
S
Shengliang Guan 已提交
44
  pRsp->acctId = htonl(pRsp->acctId);
S
Shengliang Guan 已提交
45
  pRsp->clusterId = htobe64(pRsp->clusterId);
S
Shengliang Guan 已提交
46
  pRsp->connId = htonl(pRsp->connId);
H
Haojun Liao 已提交
47
  pRsp->epSet.eps[0].port = htons(pRsp->epSet.eps[0].port);
S
Shengliang Guan 已提交
48 49 50

  EXPECT_EQ(pRsp->acctId, 1);
  EXPECT_GT(pRsp->clusterId, 0);
S
Shengliang Guan 已提交
51
  EXPECT_EQ(pRsp->connId, 1);
S
Shengliang Guan 已提交
52
  EXPECT_EQ(pRsp->superUser, 1);
S
Shengliang Guan 已提交
53 54 55

  EXPECT_EQ(pRsp->epSet.inUse, 0);
  EXPECT_EQ(pRsp->epSet.numOfEps, 1);
H
Haojun Liao 已提交
56 57
  EXPECT_EQ(pRsp->epSet.eps[0].port, 9031);
  EXPECT_STREQ(pRsp->epSet.eps[0].fqdn, "localhost");
S
Shengliang Guan 已提交
58 59

  connId = pRsp->connId;
S
Shengliang Guan 已提交
60 61
}

S
Shengliang Guan 已提交
62 63
TEST_F(MndTestProfile, 02_ConnectMsg_InvalidDB) {
  int32_t contLen = sizeof(SConnectReq);
S
Shengliang Guan 已提交
64

S
Shengliang Guan 已提交
65
  SConnectReq* pReq = (SConnectReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
66
  pReq->pid = htonl(1234);
S
Shengliang Guan 已提交
67
  strcpy(pReq->app, "mnode_test_profile");
S
Shengliang Guan 已提交
68 69
  strcpy(pReq->db, "invalid_db");

S
Shengliang Guan 已提交
70 71 72 73
  SRpcMsg* pRsp = test.SendReq(TDMT_MND_CONNECT, pReq, contLen);
  ASSERT_NE(pRsp, nullptr);
  ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_DB);
  ASSERT_EQ(pRsp->contLen, 0);
S
Shengliang Guan 已提交
74 75
}

S
Shengliang Guan 已提交
76
TEST_F(MndTestProfile, 03_ConnectMsg_Show) {
S
Shengliang Guan 已提交
77
  test.SendShowMetaReq(TSDB_MGMT_TABLE_CONNS, "");
S
Shengliang Guan 已提交
78 79 80 81 82 83 84 85 86
  CHECK_META("show connections", 7);
  CHECK_SCHEMA(0, TSDB_DATA_TYPE_INT, 4, "connId");
  CHECK_SCHEMA(1, TSDB_DATA_TYPE_BINARY, TSDB_USER_LEN + VARSTR_HEADER_SIZE, "user");
  CHECK_SCHEMA(2, TSDB_DATA_TYPE_BINARY, TSDB_APP_NAME_LEN + VARSTR_HEADER_SIZE, "program");
  CHECK_SCHEMA(3, TSDB_DATA_TYPE_INT, 4, "pid");
  CHECK_SCHEMA(4, TSDB_DATA_TYPE_BINARY, TSDB_IPv4ADDR_LEN + 6 + VARSTR_HEADER_SIZE, "ip:port");
  CHECK_SCHEMA(5, TSDB_DATA_TYPE_TIMESTAMP, 8, "login_time");
  CHECK_SCHEMA(6, TSDB_DATA_TYPE_TIMESTAMP, 8, "last_access");

S
Shengliang Guan 已提交
87
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
88
  EXPECT_EQ(test.GetShowRows(), 1);
S
Shengliang Guan 已提交
89 90
  CheckInt32(1);
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
91
  CheckBinary("mnode_test_profile", TSDB_APP_NAME_LEN);
S
Shengliang Guan 已提交
92 93 94 95
  CheckInt32(1234);
  IgnoreBinary(TSDB_IPv4ADDR_LEN + 6);
  CheckTimestamp();
  CheckTimestamp();
S
Shengliang Guan 已提交
96 97
}

S
Shengliang Guan 已提交
98
TEST_F(MndTestProfile, 04_HeartBeatMsg) {
L
Liu Jicong 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  SClientHbBatchReq batchReq;
  batchReq.reqs = taosArrayInit(0, sizeof(SClientHbReq));
  SClientHbReq req = {0};
  req.connKey = {.connId = 123, .hbType = HEARTBEAT_TYPE_MQ};
  req.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
  SKv kv;
  kv.key = (void*)"abc";
  kv.keyLen = 4;
  kv.value = (void*)"bcd";
  kv.valueLen = 4;
  taosHashPut(req.info, kv.key, kv.keyLen, kv.value, kv.valueLen);
  taosArrayPush(batchReq.reqs, &req);

  int32_t tlen = tSerializeSClientHbBatchReq(NULL, &batchReq);
  
  void* buf = (SClientHbBatchReq*)rpcMallocCont(tlen);
  void* bufCopy = buf;
  tSerializeSClientHbBatchReq(&bufCopy, &batchReq);
  SRpcMsg* pMsg = test.SendReq(TDMT_MND_HEARTBEAT, buf, tlen);
  ASSERT_NE(pMsg, nullptr);
  ASSERT_EQ(pMsg->code, 0);
  char* pRspChar = (char*)pMsg->pCont;
  SClientHbBatchRsp rsp = {0};
  tDeserializeSClientHbBatchRsp(pRspChar, &rsp);
  int sz = taosArrayGetSize(rsp.rsps);
L
Liu Jicong 已提交
124 125 126 127 128
  ASSERT_EQ(sz, 0);
  //SClientHbRsp* pRsp = (SClientHbRsp*) taosArrayGet(rsp.rsps, 0);
  //EXPECT_EQ(pRsp->connKey.connId, 123);
  //EXPECT_EQ(pRsp->connKey.hbType, HEARTBEAT_TYPE_MQ);
  //EXPECT_EQ(pRsp->status, 0);
L
Liu Jicong 已提交
129

L
Liu Jicong 已提交
130
#if 0
S
Shengliang Guan 已提交
131
  int32_t contLen = sizeof(SHeartBeatReq);
S
Shengliang Guan 已提交
132

S
Shengliang Guan 已提交
133
  SHeartBeatReq* pReq = (SHeartBeatReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
134
  pReq->connId = htonl(connId);
S
Shengliang Guan 已提交
135 136 137
  pReq->pid = htonl(1234);
  pReq->numOfQueries = htonl(0);
  pReq->numOfStreams = htonl(0);
S
Shengliang Guan 已提交
138
  strcpy(pReq->app, "mnode_test_profile");
S
Shengliang Guan 已提交
139

S
Shengliang Guan 已提交
140
  SRpcMsg* pMsg = test.SendReq(TDMT_MND_HEARTBEAT, pReq, contLen);
S
Shengliang Guan 已提交
141
  ASSERT_NE(pMsg, nullptr);
S
Shengliang Guan 已提交
142
  ASSERT_EQ(pMsg->code, 0);
S
Shengliang Guan 已提交
143

S
Shengliang Guan 已提交
144
  SHeartBeatRsp* pRsp = (SHeartBeatRsp*)pMsg->pCont;
S
Shengliang Guan 已提交
145 146 147 148 149 150 151 152
  ASSERT_NE(pRsp, nullptr);
  pRsp->connId = htonl(pRsp->connId);
  pRsp->queryId = htonl(pRsp->queryId);
  pRsp->streamId = htonl(pRsp->streamId);
  pRsp->totalDnodes = htonl(pRsp->totalDnodes);
  pRsp->onlineDnodes = htonl(pRsp->onlineDnodes);
  pRsp->epSet.port[0] = htons(pRsp->epSet.port[0]);

S
Shengliang Guan 已提交
153
  EXPECT_EQ(pRsp->connId, connId);
S
Shengliang Guan 已提交
154 155 156 157 158 159 160 161
  EXPECT_EQ(pRsp->queryId, 0);
  EXPECT_EQ(pRsp->streamId, 0);
  EXPECT_EQ(pRsp->totalDnodes, 1);
  EXPECT_EQ(pRsp->onlineDnodes, 1);
  EXPECT_EQ(pRsp->killConnection, 0);

  EXPECT_EQ(pRsp->epSet.inUse, 0);
  EXPECT_EQ(pRsp->epSet.numOfEps, 1);
S
Shengliang Guan 已提交
162
  EXPECT_EQ(pRsp->epSet.port[0], 9031);
S
Shengliang Guan 已提交
163
  EXPECT_STREQ(pRsp->epSet.fqdn[0], "localhost");
L
Liu Jicong 已提交
164
#endif
S
Shengliang Guan 已提交
165
}
S
Shengliang Guan 已提交
166

S
Shengliang Guan 已提交
167
TEST_F(MndTestProfile, 05_KillConnMsg) {
L
Liu Jicong 已提交
168 169
  // temporary remove since kill will use new heartbeat msg
#if 0
S
Shengliang Guan 已提交
170
  {
S
Shengliang Guan 已提交
171
    int32_t contLen = sizeof(SKillConnReq);
S
Shengliang Guan 已提交
172

S
Shengliang Guan 已提交
173
    SKillConnReq* pReq = (SKillConnReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
174
    pReq->connId = htonl(connId);
S
Shengliang Guan 已提交
175

S
Shengliang Guan 已提交
176 177 178
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_KILL_CONN, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
179 180 181
  }

  {
S
Shengliang Guan 已提交
182
    int32_t contLen = sizeof(SHeartBeatReq);
S
Shengliang Guan 已提交
183

S
Shengliang Guan 已提交
184
    SHeartBeatReq* pReq = (SHeartBeatReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
185 186 187 188
    pReq->connId = htonl(connId);
    pReq->pid = htonl(1234);
    pReq->numOfQueries = htonl(0);
    pReq->numOfStreams = htonl(0);
S
Shengliang Guan 已提交
189
    strcpy(pReq->app, "mnode_test_profile");
S
Shengliang Guan 已提交
190

S
Shengliang Guan 已提交
191 192 193 194
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_HEARTBEAT, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_CONNECTION);
    ASSERT_EQ(pRsp->contLen, 0);
S
Shengliang Guan 已提交
195 196 197
  }

  {
S
Shengliang Guan 已提交
198
    int32_t contLen = sizeof(SConnectReq);
S
Shengliang Guan 已提交
199

S
Shengliang Guan 已提交
200
    SConnectReq* pReq = (SConnectReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
201
    pReq->pid = htonl(1234);
S
Shengliang Guan 已提交
202
    strcpy(pReq->app, "mnode_test_profile");
S
Shengliang Guan 已提交
203 204
    strcpy(pReq->db, "");

S
Shengliang Guan 已提交
205
    SRpcMsg* pMsg = test.SendReq(TDMT_MND_CONNECT, pReq, contLen);
S
Shengliang Guan 已提交
206
    ASSERT_NE(pMsg, nullptr);
S
Shengliang Guan 已提交
207
    ASSERT_EQ(pMsg->code, 0);
S
Shengliang Guan 已提交
208 209 210 211

    SConnectRsp* pRsp = (SConnectRsp*)pMsg->pCont;
    ASSERT_NE(pRsp, nullptr);
    pRsp->acctId = htonl(pRsp->acctId);
S
Shengliang Guan 已提交
212
    pRsp->clusterId = htobe64(pRsp->clusterId);
S
Shengliang Guan 已提交
213 214 215 216 217
    pRsp->connId = htonl(pRsp->connId);
    pRsp->epSet.port[0] = htons(pRsp->epSet.port[0]);

    EXPECT_EQ(pRsp->acctId, 1);
    EXPECT_GT(pRsp->clusterId, 0);
S
Shengliang Guan 已提交
218
    EXPECT_GT(pRsp->connId, connId);
S
Shengliang Guan 已提交
219
    EXPECT_EQ(pRsp->superUser, 1);
S
Shengliang Guan 已提交
220 221 222

    EXPECT_EQ(pRsp->epSet.inUse, 0);
    EXPECT_EQ(pRsp->epSet.numOfEps, 1);
S
Shengliang Guan 已提交
223
    EXPECT_EQ(pRsp->epSet.port[0], 9031);
S
Shengliang Guan 已提交
224 225 226 227
    EXPECT_STREQ(pRsp->epSet.fqdn[0], "localhost");

    connId = pRsp->connId;
  }
L
Liu Jicong 已提交
228
#endif
S
Shengliang Guan 已提交
229 230
}

S
Shengliang Guan 已提交
231 232
TEST_F(MndTestProfile, 06_KillConnMsg_InvalidConn) {
  int32_t contLen = sizeof(SKillConnReq);
S
Shengliang Guan 已提交
233

S
Shengliang Guan 已提交
234
  SKillConnReq* pReq = (SKillConnReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
235 236
  pReq->connId = htonl(2345);

S
Shengliang Guan 已提交
237 238 239
  SRpcMsg* pRsp = test.SendReq(TDMT_MND_KILL_CONN, pReq, contLen);
  ASSERT_NE(pRsp, nullptr);
  ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_CONN_ID);
S
Shengliang Guan 已提交
240 241
}

S
Shengliang Guan 已提交
242
TEST_F(MndTestProfile, 07_KillQueryMsg) {
L
Liu Jicong 已提交
243 244
  // temporary remove since kill will use new heartbeat msg
#if 0
S
Shengliang Guan 已提交
245
  {
S
Shengliang Guan 已提交
246
    int32_t contLen = sizeof(SKillQueryReq);
S
Shengliang Guan 已提交
247

S
Shengliang Guan 已提交
248
    SKillQueryReq* pReq = (SKillQueryReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
249 250 251
    pReq->connId = htonl(connId);
    pReq->queryId = htonl(1234);

S
Shengliang Guan 已提交
252 253 254 255
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_KILL_QUERY, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
    ASSERT_EQ(pRsp->contLen, 0);
S
Shengliang Guan 已提交
256 257 258
  }

  {
S
Shengliang Guan 已提交
259
    int32_t contLen = sizeof(SHeartBeatReq);
S
Shengliang Guan 已提交
260

S
Shengliang Guan 已提交
261
    SHeartBeatReq* pReq = (SHeartBeatReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
262 263 264 265
    pReq->connId = htonl(connId);
    pReq->pid = htonl(1234);
    pReq->numOfQueries = htonl(0);
    pReq->numOfStreams = htonl(0);
S
Shengliang Guan 已提交
266
    strcpy(pReq->app, "mnode_test_profile");
S
Shengliang Guan 已提交
267

S
Shengliang Guan 已提交
268
    SRpcMsg* pMsg = test.SendReq(TDMT_MND_HEARTBEAT, pReq, contLen);
S
Shengliang Guan 已提交
269
    ASSERT_NE(pMsg, nullptr);
S
Shengliang Guan 已提交
270
    ASSERT_EQ(pMsg->code, 0);
S
Shengliang Guan 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

    SHeartBeatRsp* pRsp = (SHeartBeatRsp*)pMsg->pCont;
    ASSERT_NE(pRsp, nullptr);
    pRsp->connId = htonl(pRsp->connId);
    pRsp->queryId = htonl(pRsp->queryId);
    pRsp->streamId = htonl(pRsp->streamId);
    pRsp->totalDnodes = htonl(pRsp->totalDnodes);
    pRsp->onlineDnodes = htonl(pRsp->onlineDnodes);
    pRsp->epSet.port[0] = htons(pRsp->epSet.port[0]);

    EXPECT_EQ(pRsp->connId, connId);
    EXPECT_EQ(pRsp->queryId, 1234);
    EXPECT_EQ(pRsp->streamId, 0);
    EXPECT_EQ(pRsp->totalDnodes, 1);
    EXPECT_EQ(pRsp->onlineDnodes, 1);
    EXPECT_EQ(pRsp->killConnection, 0);

    EXPECT_EQ(pRsp->epSet.inUse, 0);
    EXPECT_EQ(pRsp->epSet.numOfEps, 1);
S
Shengliang Guan 已提交
290
    EXPECT_EQ(pRsp->epSet.port[0], 9031);
S
Shengliang Guan 已提交
291 292
    EXPECT_STREQ(pRsp->epSet.fqdn[0], "localhost");
  }
L
Liu Jicong 已提交
293
#endif
S
Shengliang Guan 已提交
294 295
}

S
Shengliang Guan 已提交
296 297
TEST_F(MndTestProfile, 08_KillQueryMsg_InvalidConn) {
  int32_t contLen = sizeof(SKillQueryReq);
S
Shengliang Guan 已提交
298

S
Shengliang Guan 已提交
299
  SKillQueryReq* pReq = (SKillQueryReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
300 301 302
  pReq->connId = htonl(2345);
  pReq->queryId = htonl(1234);

S
Shengliang Guan 已提交
303 304 305
  SRpcMsg* pRsp = test.SendReq(TDMT_MND_KILL_QUERY, pReq, contLen);
  ASSERT_NE(pRsp, nullptr);
  ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_CONN_ID);
S
Shengliang Guan 已提交
306 307
}

S
Shengliang Guan 已提交
308
TEST_F(MndTestProfile, 09_KillQueryMsg) {
S
Shengliang Guan 已提交
309
  test.SendShowMetaReq(TSDB_MGMT_TABLE_QUERIES, "");
S
Shengliang Guan 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
  CHECK_META("show queries", 14);

  CHECK_SCHEMA(0, TSDB_DATA_TYPE_INT, 4, "queryId");
  CHECK_SCHEMA(1, TSDB_DATA_TYPE_INT, 4, "connId");
  CHECK_SCHEMA(2, TSDB_DATA_TYPE_BINARY, TSDB_USER_LEN + VARSTR_HEADER_SIZE, "user");
  CHECK_SCHEMA(3, TSDB_DATA_TYPE_BINARY, TSDB_IPv4ADDR_LEN + 6 + VARSTR_HEADER_SIZE, "ip:port");
  CHECK_SCHEMA(4, TSDB_DATA_TYPE_BINARY, 22 + VARSTR_HEADER_SIZE, "qid");
  CHECK_SCHEMA(5, TSDB_DATA_TYPE_TIMESTAMP, 8, "created_time");
  CHECK_SCHEMA(6, TSDB_DATA_TYPE_BIGINT, 8, "time");
  CHECK_SCHEMA(7, TSDB_DATA_TYPE_BINARY, 18 + VARSTR_HEADER_SIZE, "sql_obj_id");
  CHECK_SCHEMA(8, TSDB_DATA_TYPE_INT, 4, "pid");
  CHECK_SCHEMA(9, TSDB_DATA_TYPE_BINARY, TSDB_EP_LEN + VARSTR_HEADER_SIZE, "ep");
  CHECK_SCHEMA(10, TSDB_DATA_TYPE_BOOL, 1, "stable_query");
  CHECK_SCHEMA(11, TSDB_DATA_TYPE_INT, 4, "sub_queries");
  CHECK_SCHEMA(12, TSDB_DATA_TYPE_BINARY, TSDB_SHOW_SUBQUERY_LEN + VARSTR_HEADER_SIZE, "sub_query_info");
  CHECK_SCHEMA(13, TSDB_DATA_TYPE_BINARY, TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE, "sql");

S
Shengliang Guan 已提交
327
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
328
  EXPECT_EQ(test.GetShowRows(), 0);
S
Shengliang Guan 已提交
329
}