profile.cpp 9.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
TEST_F(MndTestProfile, 01_ConnectMsg) {
S
Shengliang Guan 已提交
31 32
  SConnectReq connectReq = {0};
  connectReq.pid = 1234;
dengyihao's avatar
dengyihao 已提交
33 34 35 36 37

  char passwd[] = "taosdata";
  char secretEncrypt[TSDB_PASSWORD_LEN] = {0};
  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);
S
Shengliang Guan 已提交
42

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

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

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

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

S
Shengliang Guan 已提交
59 60 61 62
  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 已提交
63

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

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

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

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

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

S
Shengliang Guan 已提交
89
TEST_F(MndTestProfile, 03_ConnectMsg_Show) {
S
Shengliang Guan 已提交
90 91
  test.SendShowReq(TSDB_MGMT_TABLE_CONNS, "connections", "");
  EXPECT_EQ(test.GetShowRows(), 0);
S
Shengliang Guan 已提交
92 93
}

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

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

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

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
170 171 172
    int32_t contLen = tSerializeSKillConnReq(NULL, 0, &killReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSKillConnReq(pReq, contLen, &killReq);
S
Shengliang Guan 已提交
173

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

  {
S
Shengliang Guan 已提交
180
    int32_t contLen = sizeof(SHeartBeatReq);
S
Shengliang Guan 已提交
181

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

S
Shengliang Guan 已提交
189 190 191 192
    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 已提交
193 194 195
  }

  {
S
Shengliang Guan 已提交
196 197 198 199
    SConnectReq connectReq = {0};
    connectReq.pid = 1234;
    strcpy(connectReq.app, "mnode_test_profile");
    strcpy(connectReq.db, "invalid_db");
S
Shengliang Guan 已提交
200

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

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

S
Shengliang Guan 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222
    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 已提交
223
  }
L
Liu Jicong 已提交
224
#endif
S
Shengliang Guan 已提交
225 226
}

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

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

S
Shengliang Guan 已提交
235 236 237
  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 已提交
238 239
}

S
Shengliang Guan 已提交
240
TEST_F(MndTestProfile, 07_KillQueryMsg) {
L
Liu Jicong 已提交
241 242
  // temporary remove since kill will use new heartbeat msg
#if 0
S
Shengliang Guan 已提交
243
  {
S
Shengliang Guan 已提交
244 245 246
    SKillQueryReq killReq = {0};
    killReq.connId = connId;
    killReq.queryId = 1234;
S
Shengliang Guan 已提交
247

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

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
TEST_F(MndTestProfile, 08_KillQueryMsg_InvalidConn) {
S
Shengliang Guan 已提交
297 298 299
  SKillQueryReq killReq = {0};
  killReq.connId = 2345;
  killReq.queryId = 2345;
S
Shengliang Guan 已提交
300

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

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

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