profile.cpp 9.1 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", 9022); }
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);
S
Shengliang Guan 已提交
47
  pRsp->epSet.port[0] = htons(pRsp->epSet.port[0]);
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);
S
Shengliang Guan 已提交
56
  EXPECT_EQ(pRsp->epSet.port[0], 9022);
S
Shengliang Guan 已提交
57
  EXPECT_STREQ(pRsp->epSet.fqdn[0], "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 99
TEST_F(MndTestProfile, 04_HeartBeatMsg) {
  int32_t contLen = sizeof(SHeartBeatReq);
S
Shengliang Guan 已提交
100

S
Shengliang Guan 已提交
101
  SHeartBeatReq* pReq = (SHeartBeatReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
102
  pReq->connId = htonl(connId);
S
Shengliang Guan 已提交
103 104 105
  pReq->pid = htonl(1234);
  pReq->numOfQueries = htonl(0);
  pReq->numOfStreams = htonl(0);
S
Shengliang Guan 已提交
106
  strcpy(pReq->app, "mnode_test_profile");
S
Shengliang Guan 已提交
107

S
Shengliang Guan 已提交
108
  SRpcMsg* pMsg = test.SendReq(TDMT_MND_HEARTBEAT, pReq, contLen);
S
Shengliang Guan 已提交
109
  ASSERT_NE(pMsg, nullptr);
S
Shengliang Guan 已提交
110
  ASSERT_EQ(pMsg->code, 0);
S
Shengliang Guan 已提交
111

S
Shengliang Guan 已提交
112
  SHeartBeatRsp* pRsp = (SHeartBeatRsp*)pMsg->pCont;
S
Shengliang Guan 已提交
113 114 115 116 117 118 119 120
  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 已提交
121
  EXPECT_EQ(pRsp->connId, connId);
S
Shengliang Guan 已提交
122 123 124 125 126 127 128 129
  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 已提交
130
  EXPECT_EQ(pRsp->epSet.port[0], 9022);
S
Shengliang Guan 已提交
131 132
  EXPECT_STREQ(pRsp->epSet.fqdn[0], "localhost");
}
S
Shengliang Guan 已提交
133

S
Shengliang Guan 已提交
134
TEST_F(MndTestProfile, 05_KillConnMsg) {
S
Shengliang Guan 已提交
135
  {
S
Shengliang Guan 已提交
136
    int32_t contLen = sizeof(SKillConnReq);
S
Shengliang Guan 已提交
137

S
Shengliang Guan 已提交
138
    SKillConnReq* pReq = (SKillConnReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
139
    pReq->connId = htonl(connId);
S
Shengliang Guan 已提交
140

S
Shengliang Guan 已提交
141 142 143
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_KILL_CONN, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
144 145 146
  }

  {
S
Shengliang Guan 已提交
147
    int32_t contLen = sizeof(SHeartBeatReq);
S
Shengliang Guan 已提交
148

S
Shengliang Guan 已提交
149
    SHeartBeatReq* pReq = (SHeartBeatReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
150 151 152 153
    pReq->connId = htonl(connId);
    pReq->pid = htonl(1234);
    pReq->numOfQueries = htonl(0);
    pReq->numOfStreams = htonl(0);
S
Shengliang Guan 已提交
154
    strcpy(pReq->app, "mnode_test_profile");
S
Shengliang Guan 已提交
155

S
Shengliang Guan 已提交
156 157 158 159
    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 已提交
160 161 162
  }

  {
S
Shengliang Guan 已提交
163
    int32_t contLen = sizeof(SConnectReq);
S
Shengliang Guan 已提交
164

S
Shengliang Guan 已提交
165
    SConnectReq* pReq = (SConnectReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
166
    pReq->pid = htonl(1234);
S
Shengliang Guan 已提交
167
    strcpy(pReq->app, "mnode_test_profile");
S
Shengliang Guan 已提交
168 169
    strcpy(pReq->db, "");

S
Shengliang Guan 已提交
170
    SRpcMsg* pMsg = test.SendReq(TDMT_MND_CONNECT, pReq, contLen);
S
Shengliang Guan 已提交
171
    ASSERT_NE(pMsg, nullptr);
S
Shengliang Guan 已提交
172
    ASSERT_EQ(pMsg->code, 0);
S
Shengliang Guan 已提交
173 174 175 176

    SConnectRsp* pRsp = (SConnectRsp*)pMsg->pCont;
    ASSERT_NE(pRsp, nullptr);
    pRsp->acctId = htonl(pRsp->acctId);
S
Shengliang Guan 已提交
177
    pRsp->clusterId = htobe64(pRsp->clusterId);
S
Shengliang Guan 已提交
178 179 180 181 182
    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 已提交
183
    EXPECT_GT(pRsp->connId, connId);
S
Shengliang Guan 已提交
184
    EXPECT_EQ(pRsp->superUser, 1);
S
Shengliang Guan 已提交
185 186 187

    EXPECT_EQ(pRsp->epSet.inUse, 0);
    EXPECT_EQ(pRsp->epSet.numOfEps, 1);
S
Shengliang Guan 已提交
188
    EXPECT_EQ(pRsp->epSet.port[0], 9022);
S
Shengliang Guan 已提交
189 190 191 192 193 194
    EXPECT_STREQ(pRsp->epSet.fqdn[0], "localhost");

    connId = pRsp->connId;
  }
}

S
Shengliang Guan 已提交
195 196
TEST_F(MndTestProfile, 06_KillConnMsg_InvalidConn) {
  int32_t contLen = sizeof(SKillConnReq);
S
Shengliang Guan 已提交
197

S
Shengliang Guan 已提交
198
  SKillConnReq* pReq = (SKillConnReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
199 200
  pReq->connId = htonl(2345);

S
Shengliang Guan 已提交
201 202 203
  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 已提交
204 205
}

S
Shengliang Guan 已提交
206
TEST_F(MndTestProfile, 07_KillQueryMsg) {
S
Shengliang Guan 已提交
207
  {
S
Shengliang Guan 已提交
208
    int32_t contLen = sizeof(SKillQueryReq);
S
Shengliang Guan 已提交
209

S
Shengliang Guan 已提交
210
    SKillQueryReq* pReq = (SKillQueryReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
211 212 213
    pReq->connId = htonl(connId);
    pReq->queryId = htonl(1234);

S
Shengliang Guan 已提交
214 215 216 217
    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 已提交
218 219 220
  }

  {
S
Shengliang Guan 已提交
221
    int32_t contLen = sizeof(SHeartBeatReq);
S
Shengliang Guan 已提交
222

S
Shengliang Guan 已提交
223
    SHeartBeatReq* pReq = (SHeartBeatReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
224 225 226 227
    pReq->connId = htonl(connId);
    pReq->pid = htonl(1234);
    pReq->numOfQueries = htonl(0);
    pReq->numOfStreams = htonl(0);
S
Shengliang Guan 已提交
228
    strcpy(pReq->app, "mnode_test_profile");
S
Shengliang Guan 已提交
229

S
Shengliang Guan 已提交
230
    SRpcMsg* pMsg = test.SendReq(TDMT_MND_HEARTBEAT, pReq, contLen);
S
Shengliang Guan 已提交
231
    ASSERT_NE(pMsg, nullptr);
S
Shengliang Guan 已提交
232
    ASSERT_EQ(pMsg->code, 0);
S
Shengliang Guan 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

    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 已提交
252
    EXPECT_EQ(pRsp->epSet.port[0], 9022);
S
Shengliang Guan 已提交
253 254 255 256
    EXPECT_STREQ(pRsp->epSet.fqdn[0], "localhost");
  }
}

S
Shengliang Guan 已提交
257 258
TEST_F(MndTestProfile, 08_KillQueryMsg_InvalidConn) {
  int32_t contLen = sizeof(SKillQueryReq);
S
Shengliang Guan 已提交
259

S
Shengliang Guan 已提交
260
  SKillQueryReq* pReq = (SKillQueryReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
261 262 263
  pReq->connId = htonl(2345);
  pReq->queryId = htonl(1234);

S
Shengliang Guan 已提交
264 265 266
  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 已提交
267 268
}

S
Shengliang Guan 已提交
269
TEST_F(MndTestProfile, 09_KillQueryMsg) {
S
Shengliang Guan 已提交
270
  test.SendShowMetaReq(TSDB_MGMT_TABLE_QUERIES, "");
S
Shengliang Guan 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
  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 已提交
288
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
289
  EXPECT_EQ(test.GetShowRows(), 0);
S
Shengliang Guan 已提交
290
}