profile.cpp 9.5 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:
16
  static void SetUpTestSuite() { test.Init(TD_TMP_DIR_PATH "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
TEST_F(MndTestProfile, 01_ConnectMsg) {
S
Shengliang Guan 已提交
31 32
  SConnectReq connectReq = {0};
  connectReq.pid = 1234;
dengyihao's avatar
dengyihao 已提交
33 34

  char passwd[] = "taosdata";
35
  char secretEncrypt[TSDB_PASSWORD_LEN + 1] = {0};
dengyihao's avatar
dengyihao 已提交
36 37
  taosEncryptPass_c((uint8_t*)passwd, strlen(passwd), secretEncrypt);

S
Shengliang Guan 已提交
38 39
  strcpy(connectReq.app, "mnode_test_profile");
  strcpy(connectReq.db, "");
dengyihao's avatar
dengyihao 已提交
40 41
  strcpy(connectReq.user, "root");
  strcpy(connectReq.passwd, secretEncrypt);
42
  strcpy(connectReq.sVer, version);
S
Shengliang Guan 已提交
43

S
Shengliang Guan 已提交
44 45 46
  int32_t contLen = tSerializeSConnectReq(NULL, 0, &connectReq);
  void*   pReq = rpcMallocCont(contLen);
  tSerializeSConnectReq(pReq, contLen, &connectReq);
S
Shengliang Guan 已提交
47

S
Shengliang Guan 已提交
48
  SRpcMsg* pMsg = test.SendReq(TDMT_MND_CONNECT, pReq, contLen);
S
Shengliang Guan 已提交
49
  ASSERT_NE(pMsg, nullptr);
S
Shengliang Guan 已提交
50
  ASSERT_EQ(pMsg->code, 0);
S
Shengliang Guan 已提交
51

S
Shengliang Guan 已提交
52 53
  SConnectRsp connectRsp = {0};
  tDeserializeSConnectRsp(pMsg->pCont, pMsg->contLen, &connectRsp);
S
Shengliang Guan 已提交
54

S
Shengliang Guan 已提交
55 56
  EXPECT_EQ(connectRsp.acctId, 1);
  EXPECT_GT(connectRsp.clusterId, 0);
S
Shengliang Guan 已提交
57
  EXPECT_NE(connectRsp.connId, 0);
S
Shengliang Guan 已提交
58
  EXPECT_EQ(connectRsp.superUser, 1);
S
Shengliang Guan 已提交
59

S
Shengliang Guan 已提交
60 61 62 63
  EXPECT_EQ(connectRsp.epSet.inUse, 0);
  EXPECT_EQ(connectRsp.epSet.numOfEps, 1);
  EXPECT_EQ(connectRsp.epSet.eps[0].port, 9031);
  EXPECT_STREQ(connectRsp.epSet.eps[0].fqdn, "localhost");
S
Shengliang Guan 已提交
64

S
Shengliang Guan 已提交
65
  connId = connectRsp.connId;
S
Shengliang Guan 已提交
66 67
}

S
Shengliang Guan 已提交
68
TEST_F(MndTestProfile, 02_ConnectMsg_InvalidDB) {
dengyihao's avatar
dengyihao 已提交
69
  char passwd[] = "taosdata";
70
  char secretEncrypt[TSDB_PASSWORD_LEN + 1] = {0};
dengyihao's avatar
dengyihao 已提交
71 72
  taosEncryptPass_c((uint8_t*)passwd, strlen(passwd), secretEncrypt);

S
Shengliang Guan 已提交
73 74 75 76
  SConnectReq connectReq = {0};
  connectReq.pid = 1234;
  strcpy(connectReq.app, "mnode_test_profile");
  strcpy(connectReq.db, "invalid_db");
dengyihao's avatar
dengyihao 已提交
77 78
  strcpy(connectReq.user, "root");
  strcpy(connectReq.passwd, secretEncrypt);
79
  strcpy(connectReq.sVer, version);
S
Shengliang Guan 已提交
80

S
Shengliang Guan 已提交
81 82 83
  int32_t contLen = tSerializeSConnectReq(NULL, 0, &connectReq);
  void*   pReq = rpcMallocCont(contLen);
  tSerializeSConnectReq(pReq, contLen, &connectReq);
S
Shengliang Guan 已提交
84

S
Shengliang Guan 已提交
85 86 87 88
  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 已提交
89 90
}

S
Shengliang Guan 已提交
91
TEST_F(MndTestProfile, 03_ConnectMsg_Show) {
D
dapan1121 已提交
92
  test.SendShowReq(TSDB_MGMT_TABLE_CONNS, "perf_connections", "");
D
dapan1121 已提交
93
  EXPECT_EQ(test.GetShowRows(), 1);
S
Shengliang Guan 已提交
94 95
}

S
Shengliang Guan 已提交
96
TEST_F(MndTestProfile, 04_HeartBeatMsg) {
S
Shengliang Guan 已提交
97
  SClientHbBatchReq batchReq = {0};
L
Liu Jicong 已提交
98 99
  batchReq.reqs = taosArrayInit(0, sizeof(SClientHbReq));
  SClientHbReq req = {0};
S
Shengliang Guan 已提交
100 101
  req.connKey.tscRid = 123;
  req.connKey.connType = CONN_TYPE__TMQ;
L
Liu Jicong 已提交
102
  req.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
S
Shengliang Guan 已提交
103
  SKv kv = {0};
D
dapan1121 已提交
104
  kv.key = 123;
L
Liu Jicong 已提交
105 106
  kv.value = (void*)"bcd";
  kv.valueLen = 4;
D
dapan1121 已提交
107
  taosHashPut(req.info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
L
Liu Jicong 已提交
108 109
  taosArrayPush(batchReq.reqs, &req);

S
Shengliang Guan 已提交
110 111 112 113
  int32_t tlen = tSerializeSClientHbBatchReq(NULL, 0, &batchReq);
  void*   buf = (SClientHbBatchReq*)rpcMallocCont(tlen);
  tSerializeSClientHbBatchReq(buf, tlen, &batchReq);

L
Liu Jicong 已提交
114 115 116
  SRpcMsg* pMsg = test.SendReq(TDMT_MND_HEARTBEAT, buf, tlen);
  ASSERT_NE(pMsg, nullptr);
  ASSERT_EQ(pMsg->code, 0);
S
Shengliang Guan 已提交
117

L
Liu Jicong 已提交
118
  SClientHbBatchRsp rsp = {0};
S
Shengliang Guan 已提交
119
  tDeserializeSClientHbBatchRsp(pMsg->pCont, pMsg->contLen, &rsp);
L
Liu Jicong 已提交
120
  int sz = taosArrayGetSize(rsp.rsps);
L
Liu Jicong 已提交
121
  ASSERT_EQ(sz, 0);
S
Shengliang Guan 已提交
122 123 124 125 126

  // 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 已提交
127

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

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

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

S
Shengliang Guan 已提交
142
  SHeartBeatRsp* pRsp = (SHeartBeatRsp*)pMsg->pCont;
S
Shengliang Guan 已提交
143 144 145 146 147 148 149 150
  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 已提交
151
  EXPECT_EQ(pRsp->connId, connId);
S
Shengliang Guan 已提交
152 153 154 155 156 157 158 159
  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 已提交
160
  EXPECT_EQ(pRsp->epSet.port[0], 9031);
S
Shengliang Guan 已提交
161
  EXPECT_STREQ(pRsp->epSet.fqdn[0], "localhost");
L
Liu Jicong 已提交
162
#endif
S
Shengliang Guan 已提交
163
}
S
Shengliang Guan 已提交
164

S
Shengliang Guan 已提交
165
TEST_F(MndTestProfile, 05_KillConnMsg) {
L
Liu Jicong 已提交
166 167
  // temporary remove since kill will use new heartbeat msg
#if 0
S
Shengliang Guan 已提交
168
  {
S
Shengliang Guan 已提交
169 170
    SKillConnReq killReq = {0};
    killReq.connId = connId;
S
Shengliang Guan 已提交
171

S
Shengliang Guan 已提交
172 173 174
    int32_t contLen = tSerializeSKillConnReq(NULL, 0, &killReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSKillConnReq(pReq, contLen, &killReq);
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 199 200 201
    SConnectReq connectReq = {0};
    connectReq.pid = 1234;
    strcpy(connectReq.app, "mnode_test_profile");
    strcpy(connectReq.db, "invalid_db");
S
Shengliang Guan 已提交
202

S
Shengliang Guan 已提交
203 204 205
    int32_t contLen = tSerializeSConnectReq(NULL, 0, &connectReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSConnectReq(pReq, contLen, &connectReq);
S
Shengliang Guan 已提交
206

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

S
Shengliang Guan 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224
    SConnectRsp connectRsp = {0};
    tDeserializeSConnectRsp(pMsg->pCont, pMsg->contLen, &connectRsp);

    EXPECT_EQ(connectRsp.acctId, 1);
    EXPECT_GT(connectRsp.clusterId, 0);
    EXPECT_GT(connectRsp.connId, connId);
    EXPECT_EQ(connectRsp.superUser, 1);

    EXPECT_EQ(connectRsp.epSet.inUse, 0);
    EXPECT_EQ(connectRsp.epSet.numOfEps, 1);
    EXPECT_EQ(connectRsp.epSet.port[0], 9031);
    EXPECT_STREQ(connectRsp.epSet.fqdn[0], "localhost");

    connId = connectRsp.connId;
S
Shengliang Guan 已提交
225
  }
L
Liu Jicong 已提交
226
#endif
S
Shengliang Guan 已提交
227 228
}

S
Shengliang Guan 已提交
229
TEST_F(MndTestProfile, 06_KillConnMsg_InvalidConn) {
S
Shengliang Guan 已提交
230 231
  SKillConnReq killReq = {0};
  killReq.connId = 2345;
S
Shengliang Guan 已提交
232

S
Shengliang Guan 已提交
233 234 235
  int32_t contLen = tSerializeSKillConnReq(NULL, 0, &killReq);
  void*   pReq = rpcMallocCont(contLen);
  tSerializeSKillConnReq(pReq, contLen, &killReq);
S
Shengliang Guan 已提交
236

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 247 248
    SKillQueryReq killReq = {0};
    killReq.connId = connId;
    killReq.queryId = 1234;
S
Shengliang Guan 已提交
249

S
Shengliang Guan 已提交
250 251 252
    int32_t contLen = tSerializeSKillQueryReq(NULL, 0, &killReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSKillQueryReq(pReq, contLen, &killReq);
S
Shengliang Guan 已提交
253

S
Shengliang Guan 已提交
254 255 256 257
    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 已提交
258 259 260
  }

  {
S
Shengliang Guan 已提交
261
    int32_t contLen = sizeof(SHeartBeatReq);
S
Shengliang Guan 已提交
262

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

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

    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 已提交
292
    EXPECT_EQ(pRsp->epSet.port[0], 9031);
S
Shengliang Guan 已提交
293 294
    EXPECT_STREQ(pRsp->epSet.fqdn[0], "localhost");
  }
L
Liu Jicong 已提交
295
#endif
S
Shengliang Guan 已提交
296 297
}

S
Shengliang Guan 已提交
298
TEST_F(MndTestProfile, 08_KillQueryMsg_InvalidConn) {
S
Shengliang Guan 已提交
299
  SKillQueryReq killReq = {0};
D
dapan1121 已提交
300
  strcpy(killReq.queryStrId, "2345:2345");
S
Shengliang Guan 已提交
301

S
Shengliang Guan 已提交
302 303 304
  int32_t contLen = tSerializeSKillQueryReq(NULL, 0, &killReq);
  void*   pReq = rpcMallocCont(contLen);
  tSerializeSKillQueryReq(pReq, contLen, &killReq);
S
Shengliang Guan 已提交
305

S
Shengliang Guan 已提交
306 307 308
  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 已提交
309 310
}

S
Shengliang Guan 已提交
311
TEST_F(MndTestProfile, 09_KillQueryMsg) {
D
dapan1121 已提交
312
  test.SendShowReq(TSDB_MGMT_TABLE_QUERIES, "perf_queries", "");
S
Shengliang Guan 已提交
313
  EXPECT_EQ(test.GetShowRows(), 0);
S
Shengliang Guan 已提交
314
}