• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4945

30 Jan 2026 06:19AM UTC coverage: 66.87% (+0.02%) from 66.849%
#4945

push

travis-ci

web-flow
merge: from main to 3.0 #34453

1126 of 2018 new or added lines in 72 files covered. (55.8%)

13708 existing lines in 159 files now uncovered.

205277 of 306978 relevant lines covered (66.87%)

126353544.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.38
/source/dnode/mnode/impl/src/mndConsumer.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define _DEFAULT_SOURCE
17
#include "mndConsumer.h"
18
#include "mndDb.h"
19
#include "mndPrivilege.h"
20
#include "mndShow.h"
21
#include "mndSubscribe.h"
22
#include "mndTopic.h"
23
#include "mndTrans.h"
24
#include "mndUser.h"
25
#include "mndVgroup.h"
26
#include "tcompare.h"
27
#include "tname.h"
28

29
#define MND_CONSUMER_VER_NUMBER   3
30
#define MND_CONSUMER_RESERVE_SIZE 64
31

32
#define MND_MAX_GROUP_PER_TOPIC 100
33

34
static int32_t mndConsumerActionInsert(SSdb *pSdb, SMqConsumerObj *pConsumer);
35
static int32_t mndConsumerActionDelete(SSdb *pSdb, SMqConsumerObj *pConsumer);
36
static int32_t mndConsumerActionUpdate(SSdb *pSdb, SMqConsumerObj *pOldConsumer, SMqConsumerObj *pNewConsumer);
37
static int32_t mndRetrieveConsumer(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
38
static void    mndCancelGetNextConsumer(SMnode *pMnode, void *pIter);
39

40
static int32_t mndProcessSubscribeReq(SRpcMsg *pMsg);
41
static int32_t mndProcessAskEpReq(SRpcMsg *pMsg);
42
static int32_t mndProcessMqHbReq(SRpcMsg *pMsg);
43
static int32_t mndProcessConsumerClearMsg(SRpcMsg *pMsg);
44

45
int32_t mndInitConsumer(SMnode *pMnode) {
398,833✔
46
  SSdbTable table = {
398,833✔
47
      .sdbType = SDB_CONSUMER,
48
      .keyType = SDB_KEY_INT64,
49
      .encodeFp = (SdbEncodeFp)mndConsumerActionEncode,
50
      .decodeFp = (SdbDecodeFp)mndConsumerActionDecode,
51
      .insertFp = (SdbInsertFp)mndConsumerActionInsert,
52
      .updateFp = (SdbUpdateFp)mndConsumerActionUpdate,
53
      .deleteFp = (SdbDeleteFp)mndConsumerActionDelete,
54
  };
55

56
  if (pMnode == NULL){
398,833✔
UNCOV
57
    return TSDB_CODE_INVALID_PARA;
×
58
  }
59
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_SUBSCRIBE, mndProcessSubscribeReq);
398,833✔
60
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_HB, mndProcessMqHbReq);
398,833✔
61
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_ASK_EP, mndProcessAskEpReq);
398,833✔
62
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_LOST_CONSUMER_CLEAR, mndProcessConsumerClearMsg);
398,833✔
63

64
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndRetrieveConsumer);
398,833✔
65
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndCancelGetNextConsumer);
398,833✔
66

67
  return sdbSetTable(pMnode->pSdb, table);
398,833✔
68
}
69

70
void mndCleanupConsumer(SMnode *pMnode) {}
398,775✔
71

72
int32_t mndSendConsumerMsg(SMnode *pMnode, int64_t consumerId, uint16_t msgType, SRpcHandleInfo *info) {
100,688✔
73
  if (pMnode == NULL || info == NULL) {
100,688✔
UNCOV
74
    return TSDB_CODE_INVALID_PARA;
×
75
  }
76
  int32_t code = 0;
100,688✔
77
  int32_t lino = 0;
100,688✔
78
  PRINT_LOG_START
100,688✔
79
  void   *msg  = rpcMallocCont(sizeof(int64_t));
100,688✔
80
  MND_TMQ_NULL_CHECK(msg);
100,688✔
81

82
  *(int64_t*)msg = consumerId;
100,688✔
83
  SRpcMsg rpcMsg = {
100,688✔
84
      .msgType = msgType,
85
      .pCont = msg,
86
      .contLen = sizeof(int64_t),
87
      .info = *info,
88
  };
89

90
  mInfo("mndSendConsumerMsg type:%d consumer:0x%" PRIx64, msgType, consumerId);
100,688✔
91
  MND_TMQ_RETURN_CHECK(tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg));
100,688✔
92

93
END:
100,688✔
94
  PRINT_LOG_END
100,688✔
95
  return code;
100,688✔
96
}
97

98
static int32_t validateOneTopic(STrans* pTrans,char *pOneTopic, SCMSubscribeReq *subscribe, SMnode *pMnode, const char *user, const char* token) {
153,498✔
99
  int32_t      code = 0;
153,498✔
100
  int32_t lino = 0;
153,498✔
101
  SMqTopicObj *pTopic = NULL;
153,498✔
102

103
  PRINT_LOG_START
153,498✔
104
  MND_TMQ_RETURN_CHECK(mndAcquireTopic(pMnode, pOneTopic, &pTopic));
153,498✔
105
  taosRLockLatch(&pTopic->lock);
152,101✔
106

107
  MND_TMQ_RETURN_CHECK(mndCheckTopicPrivilege(pMnode, user, token, MND_OPER_SUBSCRIBE, pTopic));
152,101✔
108
  MND_TMQ_RETURN_CHECK(grantCheckExpire(TSDB_GRANT_SUBSCRIPTION));
151,960✔
109

110
  if (subscribe->enableReplay) {
151,960✔
111
    if (pTopic->subType != TOPIC_SUB_TYPE__COLUMN) {
1,883✔
112
      code = TSDB_CODE_TMQ_REPLAY_NOT_SUPPORT;
538✔
113
      goto END;
538✔
114
    } 
115
    if (pTopic->stbName[0] != 0) {
1,345✔
116
      SDbObj *pDb = mndAcquireDb(pMnode, pTopic->db);
1,076✔
117
      if (pDb == NULL) {
1,076✔
UNCOV
118
        code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
119
        goto END;
×
120
      }
121
      if (pDb->cfg.numOfVgroups != 1) {
1,076✔
122
        mndReleaseDb(pMnode, pDb);
269✔
123
        code = TSDB_CODE_TMQ_REPLAY_NEED_ONE_VGROUP;
269✔
124
        goto END;
269✔
125
      }
126
      mndReleaseDb(pMnode, pDb);
807✔
127
    }
128
  }
129
  char  key[TSDB_CONSUMER_ID_LEN] = {0};
151,153✔
130
  (void)snprintf(key, TSDB_CONSUMER_ID_LEN, "%"PRIx64, subscribe->consumerId);
151,153✔
131
  mndTransSetDbName(pTrans, pTopic->db, key);
151,153✔
132
  MND_TMQ_RETURN_CHECK(mndTransCheckConflict(pMnode, pTrans));
151,153✔
133

134
END:
153,498✔
135
  PRINT_LOG_END
153,498✔
136
  if (pTopic != NULL) {
153,498✔
137
    taosRUnLockLatch(&pTopic->lock);
152,101✔
138
  }
139
  mndReleaseTopic(pMnode, pTopic);
153,498✔
140
  return code;
153,498✔
141
}
142

143
static int32_t validateTopics(STrans* pTrans, SCMSubscribeReq *subscribe, SMnode *pMnode, const char *user, const char* token) {
256,813✔
144
  if (pTrans == NULL || subscribe == NULL || pMnode == NULL || user == NULL) {
256,813✔
UNCOV
145
    return TSDB_CODE_INVALID_PARA;
×
146
  }
147
  int32_t      code = 0;
256,813✔
148
  int32_t lino = 0;
256,813✔
149

150
  PRINT_LOG_START
256,813✔
151
  int32_t numOfTopics = taosArrayGetSize(subscribe->topicNames);
256,813✔
152
  for (int32_t i = 0; i < numOfTopics; i++) {
407,966✔
153
    char *pOneTopic = taosArrayGetP(subscribe->topicNames, i);
153,498✔
154
    MND_TMQ_RETURN_CHECK(validateOneTopic(pTrans, pOneTopic, subscribe, pMnode, user, token));
153,498✔
155
  }
156

157
END:
254,468✔
158
  PRINT_LOG_END
256,813✔
159
  return code;
256,813✔
160
}
161

162
static int32_t mndProcessConsumerClearMsg(SRpcMsg *pMsg) {
100,688✔
163
  if (pMsg == NULL || pMsg->pCont == NULL) {
100,688✔
UNCOV
164
    return TSDB_CODE_INVALID_PARA;
×
165
  }
166
  int32_t              code = 0;
100,688✔
167
  int32_t              lino = 0;
100,688✔
168
  SMnode              *pMnode = pMsg->info.node;
100,688✔
169
  SMqConsumerClearMsg *pClearMsg = pMsg->pCont;
100,688✔
170
  SMqConsumerObj      *pConsumerNew = NULL;
100,688✔
171
  STrans              *pTrans = NULL;
100,688✔
172
  SMqConsumerObj      *pConsumer = NULL;
100,688✔
173

174
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, pClearMsg->consumerId, &pConsumer));
100,688✔
175
  taosRLockLatch(&pConsumer->lock);
100,688✔
176
  mInfo("consumer:0x%" PRIx64 " needs to be cleared, status %s", pClearMsg->consumerId,
100,688✔
177
        mndConsumerStatusName(pConsumer->status));
178

179
  MND_TMQ_RETURN_CHECK(tNewSMqConsumerObj(pConsumer->consumerId, pConsumer->cgroup, CONSUMER_CLEAR, NULL, NULL, &pConsumerNew));
100,688✔
180

181
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, pMsg, "clear-csm");
100,688✔
182
  MND_TMQ_NULL_CHECK(pTrans);
100,688✔
183
  MND_TMQ_RETURN_CHECK(mndSetConsumerDropLogs(pTrans, pConsumerNew));
100,688✔
184
  code = mndTransPrepare(pMnode, pTrans);
100,688✔
185

186
END:
100,688✔
187
  PRINT_LOG_END
100,688✔
188
  if (pConsumer != NULL) {
100,688✔
189
    taosRUnLockLatch(&pConsumer->lock);
100,688✔
190
  }
191
  mndReleaseConsumer(pMnode, pConsumer);
100,688✔
192
  tDeleteSMqConsumerObj(pConsumerNew);
100,688✔
193
  mndTransDrop(pTrans);
100,688✔
194
  return code;
100,688✔
195
}
196

197
static void checkOnePrivilege(const char* topic, SMnode *pMnode, SMqHbRsp *rsp, const char *user, const char* token) {
844,996✔
198
  int32_t code = 0;
844,996✔
199
  int32_t lino = 0;
844,996✔
200
  SMqTopicObj *pTopic = NULL;
844,996✔
201
  PRINT_LOG_START
844,996✔
202
  MND_TMQ_RETURN_CHECK(mndAcquireTopic(pMnode, topic, &pTopic));
844,996✔
203
  taosRLockLatch(&pTopic->lock);
844,996✔
204
  STopicPrivilege *data = taosArrayReserve(rsp->topicPrivileges, 1);
844,996✔
205
  MND_TMQ_NULL_CHECK(data);
844,996✔
206
  tstrncpy(data->topic, topic, TSDB_TOPIC_FNAME_LEN);
844,996✔
207
  if (mndCheckTopicPrivilege(pMnode, user, token, MND_OPER_SUBSCRIBE, pTopic) != 0 ||
1,689,428✔
208
      grantCheckExpire(TSDB_GRANT_SUBSCRIPTION) < 0) {
844,432✔
209
    data->noPrivilege = 1;
564✔
210
  } else {
211
    data->noPrivilege = 0;
844,432✔
212
  }
213

214
END:
844,996✔
215
  PRINT_LOG_END
844,996✔
216
  if (pTopic != NULL) {
844,996✔
217
    taosRUnLockLatch(&pTopic->lock);
844,996✔
218
  }
219
  mndReleaseTopic(pMnode, pTopic);
844,996✔
220
}
844,996✔
221

222
static int32_t checkPrivilege(SMnode *pMnode, SMqConsumerObj *pConsumer, SMqHbRsp *rsp, const char *user, const char* token) {
915,499✔
223
  if (pMnode == NULL || pConsumer == NULL || rsp == NULL || user == NULL) {
915,499✔
UNCOV
224
    return TSDB_CODE_INVALID_PARA;
×
225
  }
226
  int32_t code = 0;
915,499✔
227
  int32_t lino = 0;
915,499✔
228
  PRINT_LOG_START
915,499✔
229

230
  rsp->topicPrivileges = taosArrayInit(taosArrayGetSize(pConsumer->currentTopics), sizeof(STopicPrivilege));
915,499✔
231
  MND_TMQ_NULL_CHECK(rsp->topicPrivileges);
915,499✔
232
  for (int32_t i = 0; i < taosArrayGetSize(pConsumer->currentTopics); i++) {
1,760,495✔
233
    char        *topic = taosArrayGetP(pConsumer->currentTopics, i);
844,996✔
234
    checkOnePrivilege(topic, pMnode, rsp, user, token);
844,996✔
235
  }
236

237
END:
915,499✔
238
  PRINT_LOG_END
915,499✔
239
  return code;
915,499✔
240
}
241

242
static void storeOffsetRows(SMnode *pMnode, SMqHbReq *req, SMqConsumerObj *pConsumer){
915,499✔
243
  if (pMnode == NULL || req == NULL || pConsumer == NULL){
915,499✔
UNCOV
244
    return;
×
245
  }
246
  for (int i = 0; i < taosArrayGetSize(req->topics); i++) {
1,765,944✔
247
    TopicOffsetRows *data = taosArrayGet(req->topics, i);
850,445✔
248
    if (data == NULL){
850,445✔
UNCOV
249
      continue;
×
250
    }
251
    mInfo("heartbeat report offset rows.%s:%s", pConsumer->cgroup, data->topicName);
850,445✔
252

253
    SMqSubscribeObj *pSub = NULL;
850,445✔
254
    char  key[TSDB_SUBSCRIBE_KEY_LEN] = {0};
850,445✔
255
    (void)snprintf(key, TSDB_SUBSCRIBE_KEY_LEN, "%s%s%s", pConsumer->cgroup, TMQ_SEPARATOR, data->topicName);
850,445✔
256
    int32_t code = mndAcquireSubscribeByKey(pMnode, key, &pSub);
850,445✔
257
    if (code != 0) {
850,445✔
UNCOV
258
      mError("failed to acquire subscribe by key:%s, code:%d", key, code);
×
259
      continue;
×
260
    }
261
    taosWLockLatch(&pSub->lock);
850,445✔
262
    SMqConsumerEp *pConsumerEp = taosHashGet(pSub->consumerHash, &pConsumer->consumerId, sizeof(int64_t));
850,445✔
263
    if (pConsumerEp) {
850,445✔
264
      taosArrayDestroy(pConsumerEp->offsetRows);
836,870✔
265
      pConsumerEp->offsetRows = data->offsetRows;
836,870✔
266
      data->offsetRows = NULL;
836,870✔
267
    }
268
    taosWUnLockLatch(&pSub->lock);
850,445✔
269

270
    mndReleaseSubscribe(pMnode, pSub);
850,445✔
271
  }
272
}
273

274
static int32_t buildMqHbRsp(SRpcMsg *pMsg, SMqHbRsp *rsp){
915,499✔
275
  if (pMsg == NULL || rsp == NULL){
915,499✔
UNCOV
276
    return TSDB_CODE_INVALID_PARA;
×
277
  }
278
  int32_t tlen = tSerializeSMqHbRsp(NULL, 0, rsp);
915,499✔
279
  if (tlen <= 0){
915,499✔
UNCOV
280
    return TSDB_CODE_TMQ_INVALID_MSG;
×
281
  }
282
  void   *buf = rpcMallocCont(tlen);
915,499✔
283
  if (buf == NULL) {
915,499✔
UNCOV
284
    return terrno;
×
285
  }
286

287
  if(tSerializeSMqHbRsp(buf, tlen, rsp) <= 0){
915,499✔
UNCOV
288
    rpcFreeCont(buf);
×
289
    return TSDB_CODE_TMQ_INVALID_MSG;
×
290
  }
291
  pMsg->info.rsp = buf;
915,499✔
292
  pMsg->info.rspLen = tlen;
915,499✔
293
  return 0;
915,499✔
294
}
295

296
static int32_t mndProcessMqHbReq(SRpcMsg *pMsg) {
921,574✔
297
  if (pMsg == NULL) {
921,574✔
UNCOV
298
    return TSDB_CODE_INVALID_PARA;
×
299
  }
300
  int32_t         code = 0;
921,574✔
301
  int32_t         lino = 0;
921,574✔
302
  SMnode         *pMnode = pMsg->info.node;
921,574✔
303
  SMqHbReq        req = {0};
921,574✔
304
  SMqHbRsp        rsp = {0};
921,574✔
305
  SMqConsumerObj *pConsumer = NULL;
921,574✔
306
  PRINT_LOG_START
921,574✔
307

308
  MND_TMQ_RETURN_CHECK(tDeserializeSMqHbReq(pMsg->pCont, pMsg->contLen, &req));
921,574✔
309
  int64_t consumerId = req.consumerId;
921,574✔
310
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
921,574✔
311
  taosWLockLatch(&pConsumer->lock);
915,499✔
312
  MND_TMQ_RETURN_CHECK(checkPrivilege(pMnode, pConsumer, &rsp, RPC_MSG_USER(pMsg), RPC_MSG_TOKEN(pMsg)));
915,499✔
313
  atomic_store_32(&pConsumer->hbStatus, 0);
915,499✔
314
  mDebug("consumer:0x%" PRIx64 " receive hb pollFlag:%d pollStatus:%d", consumerId, req.pollFlag, pConsumer->pollStatus);
915,499✔
315
  if (req.pollFlag == 1){
915,499✔
316
    atomic_store_32(&pConsumer->pollStatus, 0);
393,593✔
317
    pConsumer->pollTime = taosGetTimestampMs();
787,186✔
318
  }
319

320
  storeOffsetRows(pMnode, &req, pConsumer);
915,499✔
321
  rsp.debugFlag = tqClientDebugFlag;
915,499✔
322
  code = buildMqHbRsp(pMsg, &rsp);
915,499✔
323

324
END:
921,574✔
325
  if (pConsumer != NULL) {
921,574✔
326
    taosWUnLockLatch(&pConsumer->lock);
915,499✔
327
  }
328
  tDestroySMqHbRsp(&rsp);
921,574✔
329
  mndReleaseConsumer(pMnode, pConsumer);
921,574✔
330
  tDestroySMqHbReq(&req);
921,574✔
331
  PRINT_LOG_END
921,574✔
332
  return code;
921,574✔
333
}
334

335
static int32_t processEachTopicEp(SMnode *pMnode, SMqConsumerObj *pConsumer, char *topic, SMqAskEpRsp *rsp, int32_t epoch) {
953,186✔
336
  int32_t         code = 0;
953,186✔
337
  int32_t         lino = 0;
953,186✔
338
  SMqSubscribeObj *pSub = NULL;
953,186✔
339
  SMqSubTopicEp topicEp = {0};
953,186✔
340
  char  key[TSDB_SUBSCRIBE_KEY_LEN] = {0};
953,186✔
341
  PRINT_LOG_START
953,186✔
342
  (void)snprintf(key, TSDB_SUBSCRIBE_KEY_LEN, "%s%s%s", pConsumer->cgroup, TMQ_SEPARATOR, topic);
953,186✔
343
  if(mndAcquireSubscribeByKey(pMnode, key, &pSub) != 0) {
953,186✔
UNCOV
344
    mWarn("%s failed to acquire subscribe by key:%s", __func__, key);
×
345
    goto END;
×
346
  }
347
  tstrncpy(topicEp.topic, topic, TSDB_TOPIC_FNAME_LEN);
953,186✔
348

349
  taosWLockLatch(&pSub->lock);
953,186✔
350
  // 2.2 iterate all vg assigned to the consumer of that topic
351
  SMqConsumerEp *pConsumerEp = taosHashGet(pSub->consumerHash, &pConsumer->consumerId, sizeof(int64_t));
953,186✔
352
  MND_TMQ_NULL_CHECK(pConsumerEp);
953,186✔
353
  int32_t vgNum = taosArrayGetSize(pConsumerEp->vgs);
953,186✔
354
  topicEp.vgs = taosArrayInit(vgNum, sizeof(SMqSubVgEp));
953,186✔
355
  MND_TMQ_NULL_CHECK(topicEp.vgs);
953,186✔
356

357
  tstrncpy(topicEp.db, pSub->dbName, TSDB_DB_FNAME_LEN);
953,186✔
358
  for (int32_t j = 0; j < vgNum; j++) {
2,182,531✔
359
    SMqVgEp *pVgEp = taosArrayGet(pConsumerEp->vgs, j);
1,229,345✔
360
    if (pVgEp == NULL) {
1,229,345✔
UNCOV
361
      continue;
×
362
    }
363
    if (epoch == -1) {
1,229,345✔
364
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, pVgEp->vgId);
800,706✔
365
      if (pVgroup) {
800,706✔
366
        pVgEp->epSet = mndGetVgroupEpset(pMnode, pVgroup);
50,547✔
367
        mndReleaseVgroup(pMnode, pVgroup);
50,547✔
368
      }
369
    }
370
    SMqSubVgEp vgEp = {.epSet = pVgEp->epSet, .vgId = pVgEp->vgId, .offset = -1};
1,229,345✔
371
    MND_TMQ_NULL_CHECK(taosArrayPush(topicEp.vgs, &vgEp));
2,458,690✔
372
  }
373
  MND_TMQ_NULL_CHECK(taosArrayPush(rsp->topics, &topicEp));
1,906,372✔
374
  topicEp.vgs = NULL;
953,186✔
375

376
END:
953,186✔
377
  if (pSub != NULL) {
953,186✔
378
    taosWUnLockLatch(&pSub->lock);
953,186✔
379
  }
380
  taosArrayDestroy(topicEp.vgs);
953,186✔
381
  mndReleaseSubscribe(pMnode, pSub);
953,186✔
382
  PRINT_LOG_END
953,186✔
383
  return code;
953,186✔
384
}
385

386
static int32_t addEpSetInfo(SMnode *pMnode, SMqConsumerObj *pConsumer, int32_t epoch, SMqAskEpRsp *rsp){
1,056,189✔
387
  if (pMnode == NULL || pConsumer == NULL || rsp == NULL){
1,056,189✔
UNCOV
388
    return TSDB_CODE_INVALID_PARA;
×
389
  }
390
  int32_t code = 0;
1,056,189✔
391
  int32_t lino = 0;
1,056,189✔
392
  PRINT_LOG_START
1,056,189✔
393

394
  int32_t numOfTopics = taosArrayGetSize(pConsumer->currentTopics);
1,056,189✔
395
  rsp->topics = taosArrayInit(numOfTopics, sizeof(SMqSubTopicEp));
1,056,189✔
396
  MND_TMQ_NULL_CHECK(rsp->topics);
1,056,189✔
397

398
  // handle all topics subscribed by this consumer
399
  for (int32_t i = 0; i < numOfTopics; i++) {
2,009,375✔
400
    char            *topic = taosArrayGetP(pConsumer->currentTopics, i);
953,186✔
401
    MND_TMQ_RETURN_CHECK(processEachTopicEp(pMnode, pConsumer, topic, rsp, epoch));
953,186✔
402
  }
403

404
END:
1,056,189✔
405
  PRINT_LOG_END
1,056,189✔
406
  return code;
1,056,189✔
407
}
408

409
static int32_t buildAskEpRsp(SRpcMsg *pMsg, SMqAskEpRsp *rsp, int32_t serverEpoch, int64_t consumerId){
3,765,452✔
410
  if (pMsg == NULL || rsp == NULL) {
3,765,452✔
UNCOV
411
    return TSDB_CODE_INVALID_PARA;
×
412
  }
413
  int32_t code = 0;
3,766,096✔
414
  // encode rsp
415
  int32_t tlen = sizeof(SMqRspHead) + tEncodeSMqAskEpRsp(NULL, rsp);
3,765,120✔
416
  void   *buf = rpcMallocCont(tlen);
3,765,120✔
417
  if (buf == NULL) {
3,764,801✔
UNCOV
418
    return terrno;
×
419
  }
420

421
  SMqRspHead *pHead = buf;
3,764,801✔
422

423
  pHead->mqMsgType = TMQ_MSG_TYPE__EP_RSP;
3,764,801✔
424
  pHead->epoch = serverEpoch;
3,765,125✔
425
  pHead->consumerId = consumerId;
3,765,457✔
426
  pHead->walsver = 0;
3,765,457✔
427
  pHead->walever = 0;
3,765,452✔
428

429
  void *abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
3,765,125✔
430
  if (tEncodeSMqAskEpRsp(&abuf, rsp) < 0) {
3,766,096✔
UNCOV
431
    rpcFreeCont(buf);
×
432
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
433
  }
434

435
  // send rsp
436
  pMsg->info.rsp = buf;
3,766,096✔
437
  pMsg->info.rspLen = tlen;
3,765,113✔
438
  return code;
3,765,784✔
439
}
440

441
static int32_t mndProcessAskEpReq(SRpcMsg *pMsg) {
3,768,059✔
442
  if (pMsg == NULL) {
3,768,059✔
UNCOV
443
    return TSDB_CODE_INVALID_PARA;
×
444
  }
445
  SMnode     *pMnode = pMsg->info.node;
3,768,059✔
446
  SMqAskEpReq req = {0};
3,768,371✔
447
  SMqAskEpRsp rsp = {0};
3,768,371✔
448
  int32_t     code = 0;
3,767,727✔
449
  int32_t     lino = 0;
3,767,727✔
450
  SMqConsumerObj *pConsumer = NULL;
3,767,727✔
451
  PRINT_LOG_START
3,767,727✔
452

453
  MND_TMQ_RETURN_CHECK(tDeserializeSMqAskEpReq(pMsg->pCont, pMsg->contLen, &req));
3,768,695✔
454
  int64_t consumerId = req.consumerId;
3,768,703✔
455
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
3,768,703✔
456
  taosRLockLatch(&pConsumer->lock);
3,766,096✔
457
  if (strncmp(req.cgroup, pConsumer->cgroup, tListLen(pConsumer->cgroup)) != 0) {
3,766,096✔
UNCOV
458
    mError("consumer:0x%" PRIx64 " group:%s not consistent with data in sdb, saved cgroup:%s", consumerId, req.cgroup,
×
459
           pConsumer->cgroup);
UNCOV
460
    code = TSDB_CODE_MND_CONSUMER_NOT_EXIST;
×
461
    goto END;
×
462
  }
463

464
  // 1. check consumer status
465
  int32_t serverEpoch = atomic_load_32(&pConsumer->epoch);
3,765,457✔
466
  int32_t status = atomic_load_32(&pConsumer->status);
3,765,457✔
467
  if (status != MQ_CONSUMER_STATUS_READY) {
3,765,457✔
468
    mInfo("consumer:0x%" PRIx64 " not ready, status: %s", consumerId, mndConsumerStatusName(status));
760,673✔
469
    rsp.code = TSDB_CODE_MND_CONSUMER_NOT_READY;
760,673✔
470
  } else {
471
    int32_t epoch = req.epoch;
3,004,784✔
472

473
    // 2. check epoch, only send ep info when epochs do not match
474
    if (epoch != serverEpoch) {
3,004,784✔
475
      mInfo("process ask ep, consumer:0x%" PRIx64 "(epoch %d) update with server epoch %d",
1,056,189✔
476
            consumerId, epoch, serverEpoch);
477
      MND_TMQ_RETURN_CHECK(addEpSetInfo(pMnode, pConsumer, epoch, &rsp));
1,056,189✔
478
    }
479
  }
480
  code = buildAskEpRsp(pMsg, &rsp, serverEpoch, consumerId);
3,765,457✔
481

482
END:
3,768,379✔
483
  if (pConsumer != NULL) {
3,768,379✔
484
    taosRUnLockLatch(&pConsumer->lock);
3,765,772✔
485
  }
486
  tDeleteSMqAskEpRsp(&rsp);
487
  mndReleaseConsumer(pMnode, pConsumer);
3,767,400✔
488
  PRINT_LOG_END
3,768,703✔
489
  return code;
3,768,703✔
490
}
491

492
int32_t mndSetConsumerDropLogs(STrans *pTrans, SMqConsumerObj *pConsumer) {
101,662✔
493
  if (pConsumer == NULL || pTrans == NULL) {
101,662✔
UNCOV
494
    return TSDB_CODE_INVALID_PARA;
×
495
  }
496
  int32_t  code = 0;
101,662✔
497
  int32_t lino = 0;
101,662✔
498
  PRINT_LOG_START
101,662✔
499
  SSdbRaw *pCommitRaw = mndConsumerActionEncode(pConsumer);
101,662✔
500
  MND_TMQ_NULL_CHECK(pCommitRaw);
101,662✔
501
  code = mndTransAppendCommitlog(pTrans, pCommitRaw);
101,662✔
502
  if (code != 0) {
101,662✔
UNCOV
503
    sdbFreeRaw(pCommitRaw);
×
504
    goto END;
×
505
  }
506
  MND_TMQ_RETURN_CHECK(sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED));
101,662✔
507

508
END:
101,662✔
509
  PRINT_LOG_END
101,662✔
510
  return code;
101,662✔
511
}
512

513
int32_t mndSetConsumerCommitLogs(STrans *pTrans, SMqConsumerObj *pConsumer) {
517,399✔
514
  if (pConsumer == NULL || pTrans == NULL) {
517,399✔
UNCOV
515
    return TSDB_CODE_INVALID_PARA;
×
516
  }
517
  int32_t  code = 0;
517,399✔
518
  int32_t lino = 0;
517,399✔
519
  PRINT_LOG_START
517,399✔
520
  SSdbRaw *pCommitRaw = mndConsumerActionEncode(pConsumer);
517,399✔
521
  MND_TMQ_NULL_CHECK(pCommitRaw);
517,399✔
522
  code = mndTransAppendCommitlog(pTrans, pCommitRaw);
517,399✔
523
  if (code != 0) {
517,399✔
UNCOV
524
    sdbFreeRaw(pCommitRaw);
×
525
    goto END;
×
526
  }
527
  MND_TMQ_RETURN_CHECK(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
517,399✔
528
END:
517,399✔
529

530
  return code;
517,399✔
531
}
532

533
static void freeItem(void *param) {
304✔
534
  if (param == NULL) {
304✔
UNCOV
535
    return;
×
536
  }
537
  void *pItem = *(void **)param;
304✔
538
  if (pItem != NULL) {
304✔
539
    taosMemoryFree(pItem);
304✔
540
  }
541
}
542

543
#define ADD_TOPIC_TO_ARRAY(element, array) \
544
char *newTopicCopy = taosStrdup(element); \
545
MND_TMQ_NULL_CHECK(newTopicCopy);\
546
if (taosArrayPush(pConsumerNew->array, &newTopicCopy) == NULL){\
547
  taosMemoryFree(newTopicCopy);\
548
  code = terrno;\
549
  goto END;\
550
}
551

552
static int32_t getTopicAddDelete(SMqConsumerObj *pExistedConsumer, SMqConsumerObj *pConsumerNew){
111,125✔
553
  if (pExistedConsumer == NULL || pConsumerNew == NULL) {
111,125✔
UNCOV
554
    return TSDB_CODE_INVALID_PARA;
×
555
  }
556
  int32_t code = 0;
111,125✔
557
  int32_t lino = 0;
111,125✔
558
  PRINT_LOG_START
111,125✔
559
  taosRLockLatch(&pExistedConsumer->lock);
111,125✔
560

561
  pConsumerNew->rebNewTopics = taosArrayInit(0, sizeof(void *));
111,125✔
562
  MND_TMQ_NULL_CHECK(pConsumerNew->rebNewTopics);
111,125✔
563
  pConsumerNew->rebRemovedTopics = taosArrayInit(0, sizeof(void *));
111,125✔
564
  MND_TMQ_NULL_CHECK(pConsumerNew->rebRemovedTopics);
111,125✔
565

566
  int32_t newTopicNum = taosArrayGetSize(pConsumerNew->assignedTopics);
111,125✔
567
  int32_t oldTopicNum = taosArrayGetSize(pExistedConsumer->currentTopics);
111,125✔
568
  int32_t i = 0, j = 0;
111,125✔
569
  while (i < oldTopicNum || j < newTopicNum) {
224,745✔
570
    if (i >= oldTopicNum) {
113,620✔
571
      void* tmp = taosArrayGetP(pConsumerNew->assignedTopics, j);
620✔
572
      MND_TMQ_NULL_CHECK(tmp);
620✔
573
      ADD_TOPIC_TO_ARRAY(tmp, rebNewTopics);
1,240✔
574
      j++;
620✔
575
      continue;
620✔
576
    } else if (j >= newTopicNum) {
113,000✔
577
      void* tmp = taosArrayGetP(pExistedConsumer->currentTopics, i);
108,305✔
578
      MND_TMQ_NULL_CHECK(tmp);
108,305✔
579
      ADD_TOPIC_TO_ARRAY(tmp, rebRemovedTopics);
216,610✔
580
      i++;
108,305✔
581
      continue;
108,305✔
582
    } else {
583
      char *oldTopic = taosArrayGetP(pExistedConsumer->currentTopics, i);
4,695✔
584
      MND_TMQ_NULL_CHECK(oldTopic);
4,695✔
585
      char *newTopic = taosArrayGetP(pConsumerNew->assignedTopics, j);
4,695✔
586
      MND_TMQ_NULL_CHECK(newTopic);
4,695✔
587
      int   comp = strcmp(oldTopic, newTopic);
4,695✔
588
      if (comp == 0) {
4,695✔
589
        i++;
4,695✔
590
        j++;
4,695✔
591
        continue;
4,695✔
UNCOV
592
      } else if (comp < 0) {
×
593
        ADD_TOPIC_TO_ARRAY(oldTopic, rebRemovedTopics);
×
594
        i++;
×
595
        continue;
×
596
      } else {
×
597
        ADD_TOPIC_TO_ARRAY(newTopic, rebNewTopics);
×
598
        j++;
×
599
        continue;
×
600
      }
601
    }
602
  }
603
  // no topics need to be rebalanced
604
  if (taosArrayGetSize(pConsumerNew->rebNewTopics) == 0 && taosArrayGetSize(pConsumerNew->rebRemovedTopics) == 0) {
111,125✔
605
    code = TSDB_CODE_TMQ_NO_NEED_REBALANCE;
4,695✔
606
  }
607

608
END:
106,430✔
609
  taosRUnLockLatch(&pExistedConsumer->lock);
111,125✔
610
  PRINT_LOG_END
111,125✔
611
  return code;
111,125✔
612
}
613

614
static int32_t checkAndSortTopic(SMnode *pMnode, SArray *pTopicList){
257,145✔
615
  if (pTopicList == NULL || pMnode == NULL) {
257,145✔
UNCOV
616
    return TSDB_CODE_INVALID_PARA;
×
617
  }
618
  taosArraySort(pTopicList, taosArrayCompareString);
257,145✔
619
  taosArrayRemoveDuplicate(pTopicList, taosArrayCompareString, freeItem);
257,145✔
620

621
  int32_t newTopicNum = taosArrayGetSize(pTopicList);
257,145✔
622
  for (int i = 0; i < newTopicNum; i++) {
410,643✔
623
    int32_t gNum = mndGetGroupNumByTopic(pMnode, (const char *)taosArrayGetP(pTopicList, i));
153,830✔
624
    if (gNum >= MND_MAX_GROUP_PER_TOPIC) {
153,830✔
625
      return TSDB_CODE_TMQ_GROUP_OUT_OF_RANGE;
332✔
626
    }
627
  }
628
  return 0;
256,813✔
629
}
630

631
static int32_t buildSubConsumer(SMnode *pMnode, SCMSubscribeReq *subscribe, SMqConsumerObj** ppConsumer){
254,468✔
632
  if (pMnode == NULL || subscribe == NULL) {
254,468✔
UNCOV
633
    return TSDB_CODE_INVALID_PARA;
×
634
  }
635
  int64_t         consumerId = subscribe->consumerId;
254,468✔
636
  char           *cgroup     = subscribe->cgroup;
254,468✔
637
  SMqConsumerObj *pConsumerNew     = NULL;
254,468✔
638
  SMqConsumerObj *pExistedConsumer = NULL;
254,468✔
639
  int32_t lino = 0;
254,468✔
640
  PRINT_LOG_START
254,468✔
641
  int32_t code = mndAcquireConsumer(pMnode, consumerId, &pExistedConsumer);
254,468✔
642
  if (code != 0) {
254,468✔
643
    mInfo("receive tmq subscribe request from new consumer:0x%" PRIx64
143,343✔
644
              ",cgroup:%s, numOfTopics:%d", consumerId,
645
          subscribe->cgroup, (int32_t)taosArrayGetSize(subscribe->topicNames));
646

647
    MND_TMQ_RETURN_CHECK(tNewSMqConsumerObj(consumerId, cgroup, CONSUMER_INSERT_SUB, NULL, subscribe, &pConsumerNew));
143,343✔
648
  } else {
649
    int32_t status = atomic_load_32(&pExistedConsumer->status);
111,125✔
650

651
    mInfo("receive tmq subscribe request from existed consumer:0x%" PRIx64
111,125✔
652
              ",cgroup:%s, current status:%d(%s), subscribe topic num: %d",
653
          consumerId, subscribe->cgroup, status, mndConsumerStatusName(status),
654
          (int32_t)taosArrayGetSize(subscribe->topicNames));
655

656
    if (status != MQ_CONSUMER_STATUS_READY) {
111,125✔
UNCOV
657
      code = TSDB_CODE_MND_CONSUMER_NOT_READY;
×
658
      goto END;
×
659
    }
660
    MND_TMQ_RETURN_CHECK(tNewSMqConsumerObj(consumerId, cgroup, CONSUMER_UPDATE_SUB, NULL, subscribe, &pConsumerNew));
111,125✔
661
    MND_TMQ_RETURN_CHECK(getTopicAddDelete(pExistedConsumer, pConsumerNew));
111,125✔
662
  }
663
  if (ppConsumer){
249,773✔
664
    *ppConsumer = pConsumerNew;
249,773✔
665
    pConsumerNew = NULL;
249,773✔
666
  }
667

668
END:
254,156✔
669
  PRINT_LOG_END
254,468✔
670
  mndReleaseConsumer(pMnode, pExistedConsumer);
254,468✔
671
  tDeleteSMqConsumerObj(pConsumerNew);
254,468✔
672
  return code;
254,468✔
673
}
674

675
int32_t mndProcessSubscribeReq(SRpcMsg *pMsg) {
343,138✔
676
  if (pMsg == NULL) {
343,138✔
UNCOV
677
    return TSDB_CODE_INVALID_PARA;
×
678
  }
679
  SMnode         *pMnode = pMsg->info.node;
343,138✔
680
  char           *msgStr = pMsg->pCont;
343,138✔
681
  int32_t         code = 0;
343,138✔
682
  int32_t         lino = 0;
343,138✔
683
  SMqConsumerObj *pConsumerNew = NULL;
343,138✔
684
  STrans         *pTrans = NULL;
343,138✔
685

686
  PRINT_LOG_START
343,138✔
687
  SCMSubscribeReq subscribe = {0};
343,138✔
688
  MND_TMQ_RETURN_CHECK(tDeserializeSCMSubscribeReq(msgStr, &subscribe, pMsg->contLen));
686,276✔
689
  bool unSubscribe = (taosArrayGetSize(subscribe.topicNames) == 0);
343,138✔
690
  if(unSubscribe){
343,138✔
691
    SMqConsumerObj *pConsumerTmp = NULL;
191,803✔
692
    MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, subscribe.consumerId, &pConsumerTmp));
191,803✔
693
    taosRLockLatch(&pConsumerTmp->lock);
190,761✔
694
    size_t topicNum = taosArrayGetSize(pConsumerTmp->assignedTopics);
190,761✔
695
    taosRUnLockLatch(&pConsumerTmp->lock);
190,761✔
696
    mndReleaseConsumer(pMnode, pConsumerTmp);
190,761✔
697
    if (topicNum == 0){
190,761✔
698
      goto END;
84,951✔
699
    }
700
  }
701
  MND_TMQ_RETURN_CHECK(checkAndSortTopic(pMnode, subscribe.topicNames));
257,145✔
702
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY,
256,813✔
703
                          (unSubscribe ? TRN_CONFLICT_NOTHING :TRN_CONFLICT_DB_INSIDE),
704
                          pMsg, "subscribe");
705
  MND_TMQ_NULL_CHECK(pTrans);
256,813✔
706

707
  MND_TMQ_RETURN_CHECK(validateTopics(pTrans, &subscribe, pMnode, RPC_MSG_USER(pMsg), RPC_MSG_TOKEN(pMsg)));
256,813✔
708
  MND_TMQ_RETURN_CHECK(buildSubConsumer(pMnode, &subscribe, &pConsumerNew));
254,468✔
709
  MND_TMQ_RETURN_CHECK(mndSetConsumerCommitLogs(pTrans, pConsumerNew));
249,773✔
710
  MND_TMQ_RETURN_CHECK(mndTransPrepare(pMnode, pTrans));
249,773✔
711
  code = TSDB_CODE_ACTION_IN_PROGRESS;
249,773✔
712

713
END:
343,138✔
714
  mndTransDrop(pTrans);
343,138✔
715
  tDeleteSMqConsumerObj(pConsumerNew);
343,138✔
716
  taosArrayDestroyP(subscribe.topicNames, NULL);
343,138✔
717
  code = (code == TSDB_CODE_TMQ_NO_NEED_REBALANCE || code == TSDB_CODE_MND_CONSUMER_NOT_EXIST) ? 0 : code;
343,138✔
718
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS){
343,138✔
719
    mError("tmq subscribe request from consumer:0x%" PRIx64 " failed, code:%d", subscribe.consumerId, code);
2,677✔
720
  } else {
721
    mInfo("tmq subscribe request from consumer:0x%" PRIx64 " processed, code:%d", subscribe.consumerId, code);
340,461✔
722
  }
723
  return code;
343,138✔
724
}
725

726
SSdbRaw *mndConsumerActionEncode(SMqConsumerObj *pConsumer) {
731,603✔
727
  if (pConsumer == NULL) {
731,603✔
UNCOV
728
    return NULL;
×
729
  }
730
  int32_t code = 0;
731,603✔
731
  int32_t lino = 0;
731,603✔
732
  terrno = TSDB_CODE_OUT_OF_MEMORY;
731,603✔
733

734
  void   *buf = NULL;
731,603✔
735
  int32_t tlen = tEncodeSMqConsumerObj(NULL, pConsumer);
731,603✔
736
  int32_t size = sizeof(int32_t) + tlen + MND_CONSUMER_RESERVE_SIZE;
731,603✔
737

738
  SSdbRaw *pRaw = sdbAllocRaw(SDB_CONSUMER, MND_CONSUMER_VER_NUMBER, size);
731,603✔
739
  if (pRaw == NULL) goto CM_ENCODE_OVER;
731,603✔
740

741
  buf = taosMemoryMalloc(tlen);
731,603✔
742
  if (buf == NULL) goto CM_ENCODE_OVER;
731,603✔
743

744
  void *abuf = buf;
731,603✔
745
  if(tEncodeSMqConsumerObj(&abuf, pConsumer) < 0){
731,603✔
UNCOV
746
    goto CM_ENCODE_OVER;
×
747
  }
748

749
  int32_t dataPos = 0;
731,603✔
750
  SDB_SET_INT32(pRaw, dataPos, tlen, CM_ENCODE_OVER);
731,603✔
751
  SDB_SET_BINARY(pRaw, dataPos, buf, tlen, CM_ENCODE_OVER);
731,603✔
752
  SDB_SET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_ENCODE_OVER);
731,603✔
753
  SDB_SET_DATALEN(pRaw, dataPos, CM_ENCODE_OVER);
731,603✔
754

755
  terrno = TSDB_CODE_SUCCESS;
731,603✔
756

757
CM_ENCODE_OVER:
731,603✔
758
  taosMemoryFreeClear(buf);
731,603✔
759
  if (terrno != 0) {
731,603✔
UNCOV
760
    mError("consumer:0x%" PRIx64 " failed to encode to raw:%p since %s", pConsumer->consumerId, pRaw, terrstr());
×
761
    sdbFreeRaw(pRaw);
×
762
    return NULL;
×
763
  }
764

765
  mTrace("consumer:0x%" PRIx64 ", encode to raw:%p, row:%p", pConsumer->consumerId, pRaw, pConsumer);
731,603✔
766
  return pRaw;
731,603✔
767
}
768

769
SSdbRow *mndConsumerActionDecode(SSdbRaw *pRaw) {
631,322✔
770
  if (pRaw == NULL) {
631,322✔
UNCOV
771
    return NULL;
×
772
  }
773
  int32_t         code = 0;
631,322✔
774
  int32_t         lino = 0;
631,322✔
775
  SSdbRow        *pRow = NULL;
631,322✔
776
  SMqConsumerObj *pConsumer = NULL;
631,322✔
777
  void           *buf = NULL;
631,322✔
778

779
  terrno = 0;
631,322✔
780
  int8_t sver = 0;
631,322✔
781
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
631,322✔
UNCOV
782
    goto CM_DECODE_OVER;
×
783
  }
784

785
  if (sver < 1 || sver > MND_CONSUMER_VER_NUMBER) {
631,322✔
UNCOV
786
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
787
    goto CM_DECODE_OVER;
×
788
  }
789

790
  pRow = sdbAllocRow(sizeof(SMqConsumerObj));
631,322✔
791
  if (pRow == NULL) {
631,322✔
UNCOV
792
    goto CM_DECODE_OVER;
×
793
  }
794

795
  pConsumer = sdbGetRowObj(pRow);
631,322✔
796
  if (pConsumer == NULL) {
631,322✔
UNCOV
797
    goto CM_DECODE_OVER;
×
798
  }
799

800
  int32_t dataPos = 0;
631,322✔
801
  int32_t len;
630,594✔
802
  SDB_GET_INT32(pRaw, dataPos, &len, CM_DECODE_OVER);
631,322✔
803
  buf = taosMemoryMalloc(len);
631,322✔
804
  if (buf == NULL) {
631,322✔
UNCOV
805
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
806
    goto CM_DECODE_OVER;
×
807
  }
808

809
  SDB_GET_BINARY(pRaw, dataPos, buf, len, CM_DECODE_OVER);
631,322✔
810
  SDB_GET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_DECODE_OVER);
631,322✔
811

812
  if (tDecodeSMqConsumerObj(buf, pConsumer, sver) == NULL) {
631,322✔
UNCOV
813
    terrno = TSDB_CODE_OUT_OF_MEMORY;  // TODO set correct error code
×
814
    goto CM_DECODE_OVER;
×
815
  }
816

817
  tmsgUpdateDnodeEpSet(&pConsumer->ep);
631,322✔
818

819
CM_DECODE_OVER:
631,322✔
820
  taosMemoryFreeClear(buf);
631,322✔
821
  if (terrno != TSDB_CODE_SUCCESS) {
631,322✔
UNCOV
822
    mError("consumer:0x%" PRIx64 " failed to decode from raw:%p since %s",
×
823
           pConsumer == NULL ? 0 : pConsumer->consumerId, pRaw, terrstr());
UNCOV
824
    taosMemoryFreeClear(pRow);
×
825
  }
826

827
  return pRow;
631,322✔
828
}
829

830
static int32_t mndConsumerActionInsert(SSdb *pSdb, SMqConsumerObj *pConsumer) {
151,083✔
831
  if (pConsumer == NULL) {
151,083✔
UNCOV
832
    return TSDB_CODE_INVALID_PARA;
×
833
  }
834
  mInfo("consumer:0x%" PRIx64 " sub insert, cgroup:%s status:%d(%s) epoch:%d", pConsumer->consumerId, pConsumer->cgroup,
151,083✔
835
        pConsumer->status, mndConsumerStatusName(pConsumer->status), pConsumer->epoch);
836
  pConsumer->subscribeTime = pConsumer->createTime;
151,083✔
837
  return 0;
151,083✔
838
}
839

840
static int32_t mndConsumerActionDelete(SSdb *pSdb, SMqConsumerObj *pConsumer) {
631,322✔
841
  if (pConsumer == NULL) {
631,322✔
UNCOV
842
    return TSDB_CODE_INVALID_PARA;
×
843
  }
844
  mInfo("consumer:0x%" PRIx64 " perform delete action, status:(%d)%s", pConsumer->consumerId, pConsumer->status,
631,322✔
845
        mndConsumerStatusName(pConsumer->status));
846
  tClearSMqConsumerObj(pConsumer);
631,322✔
847
  return 0;
631,322✔
848
}
849

850
// remove from topic list
851
static void removeFromTopicList(SArray *topicList, const char *pTopic, int64_t consumerId, char *type) {
373,417✔
852
  if (topicList == NULL || pTopic == NULL) {
373,417✔
UNCOV
853
    return;
×
854
  }
855
  int32_t size = taosArrayGetSize(topicList);
373,417✔
856
  for (int32_t i = 0; i < size; i++) {
377,272✔
857
    char *p = taosArrayGetP(topicList, i);
373,790✔
858
    if (strcmp(pTopic, p) == 0) {
373,790✔
859
      taosArrayRemove(topicList, i);
369,935✔
860
      taosMemoryFree(p);
369,935✔
861

862
      mInfo("tmq rebalance consumer:0x%" PRIx64 " remove topic:%s in the %s topic list, remain newTopics:%d",
369,935✔
863
            consumerId, pTopic, type, (int)taosArrayGetSize(topicList));
864
      break;
369,935✔
865
    }
866
  }
867
}
868

869
static bool existInCurrentTopicList(const SMqConsumerObj *pConsumer, const char *pTopic) {
147,747✔
870
  if (pConsumer == NULL || pTopic == NULL) {
147,747✔
UNCOV
871
    return false;
×
872
  }
873
  bool    existing = false;
147,747✔
874
  int32_t size = taosArrayGetSize(pConsumer->currentTopics);
147,747✔
875
  for (int32_t i = 0; i < size; i++) {
150,511✔
876
    char *topic = taosArrayGetP(pConsumer->currentTopics, i);
2,764✔
877
    if (topic && strcmp(topic, pTopic) == 0) {
2,764✔
UNCOV
878
      existing = true;
×
879
      break;
×
880
    }
881
  }
882

883
  return existing;
147,747✔
884
}
885

886
static int32_t mndConsumerActionUpdate(SSdb *pSdb, SMqConsumerObj *pOldConsumer, SMqConsumerObj *pNewConsumer) {
377,770✔
887
  if (pOldConsumer == NULL || pNewConsumer == NULL) {
377,770✔
UNCOV
888
    return TSDB_CODE_INVALID_PARA;
×
889
  }
890
  int32_t lino = 0;
377,770✔
891
  int32_t code = 0;
377,770✔
892
  char *pNewTopic = NULL;
377,770✔
893
  PRINT_LOG_START
377,770✔
894
  taosWLockLatch(&pOldConsumer->lock);
377,770✔
895
  mInfo("consumer:0x%" PRIx64 " perform update action, update type:%d, subscribe-time:%" PRId64 ", createTime:%" PRId64,
377,770✔
896
        pOldConsumer->consumerId, pNewConsumer->updateType, pOldConsumer->subscribeTime, pOldConsumer->createTime);
897

898
  if (pNewConsumer->updateType == CONSUMER_UPDATE_SUB) {
377,770✔
899
    TSWAP(pOldConsumer->rebNewTopics, pNewConsumer->rebNewTopics);
107,486✔
900
    TSWAP(pOldConsumer->rebRemovedTopics, pNewConsumer->rebRemovedTopics);
107,486✔
901
    TSWAP(pOldConsumer->assignedTopics, pNewConsumer->assignedTopics);
107,486✔
902

903
    pOldConsumer->subscribeTime = taosGetTimestampMs();
107,486✔
904
    pOldConsumer->status = MQ_CONSUMER_STATUS_REBALANCE;
107,486✔
905
    mInfo("consumer:0x%" PRIx64 " subscribe update, modify existed consumer", pOldConsumer->consumerId);
107,486✔
906
  } else if (pNewConsumer->updateType == CONSUMER_UPDATE_REB) {
270,284✔
907
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
9,702✔
908
    pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
9,702✔
909
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
9,702✔
910
    mInfo("tmq rebalance consumer:0x%" PRIx64 " rebalance update, only rebalance time", pOldConsumer->consumerId);
9,702✔
911
  } else if (pNewConsumer->updateType == CONSUMER_ADD_REB) {
260,582✔
912
    void *tmp = taosArrayGetP(pNewConsumer->rebNewTopics, 0);
147,747✔
913
    MND_TMQ_NULL_CHECK(tmp);
147,747✔
914
    char *pNewTopic = taosStrdup(tmp);
147,747✔
915
    MND_TMQ_NULL_CHECK(pNewTopic);
147,747✔
916
    removeFromTopicList(pOldConsumer->rebNewTopics, pNewTopic, pOldConsumer->consumerId, "new");
147,747✔
917
    bool existing = existInCurrentTopicList(pOldConsumer, pNewTopic);
147,747✔
918
    if (existing) {
147,747✔
UNCOV
919
      mError("tmq rebalance consumer:0x%" PRIx64 " add new topic:%s should not in currentTopics", pOldConsumer->consumerId, pNewTopic);
×
920
    } else {
921
      MND_TMQ_NULL_CHECK(taosArrayPush(pOldConsumer->currentTopics, &pNewTopic));
295,494✔
922
      pNewTopic = NULL;
147,747✔
923
      taosArraySort(pOldConsumer->currentTopics, taosArrayCompareString);
147,747✔
924
    }
925

926
    int32_t status = pOldConsumer->status;
147,747✔
927
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
147,747✔
928
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
145,252✔
929
    }
930

931
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
147,747✔
932
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
147,747✔
933

934
    mInfo("tmq rebalance consumer:0x%" PRIx64 " rebalance update add, state (%d)%s -> (%d)%s, new epoch:%d, reb-time:%" PRId64
147,747✔
935
          ", current topics:%d, newTopics:%d, removeTopics:%d",
936
          pOldConsumer->consumerId, status, mndConsumerStatusName(status), pOldConsumer->status,
937
          mndConsumerStatusName(pOldConsumer->status), pOldConsumer->epoch, pOldConsumer->rebalanceTime,
938
          (int)taosArrayGetSize(pOldConsumer->currentTopics), (int)taosArrayGetSize(pOldConsumer->rebNewTopics),
939
          (int)taosArrayGetSize(pOldConsumer->rebRemovedTopics));
940

941
  } else if (pNewConsumer->updateType == CONSUMER_REMOVE_REB) {
112,835✔
942
    char *topic = taosArrayGetP(pNewConsumer->rebRemovedTopics, 0);
112,835✔
943
    MND_TMQ_NULL_CHECK(topic);
112,835✔
944
    removeFromTopicList(pOldConsumer->rebRemovedTopics, topic, pOldConsumer->consumerId, "remove");
112,835✔
945
    removeFromTopicList(pOldConsumer->currentTopics, topic, pOldConsumer->consumerId, "current");
112,835✔
946

947
    int32_t status = pOldConsumer->status;
112,835✔
948
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
112,835✔
949
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
110,340✔
950
    }
951
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
112,835✔
952
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
112,835✔
953

954
    mInfo("tmq rebalanceconsumer:0x%" PRIx64 " rebalance update remove, state (%d)%s -> (%d)%s, new epoch:%d, reb-time:%" PRId64
112,835✔
955
          ", current topics:%d, newTopics:%d, removeTopics:%d",
956
          pOldConsumer->consumerId, status, mndConsumerStatusName(status), pOldConsumer->status,
957
          mndConsumerStatusName(pOldConsumer->status), pOldConsumer->epoch, pOldConsumer->rebalanceTime,
958
          (int)taosArrayGetSize(pOldConsumer->currentTopics), (int)taosArrayGetSize(pOldConsumer->rebNewTopics),
959
          (int)taosArrayGetSize(pOldConsumer->rebRemovedTopics));
960
  }
961

UNCOV
962
END:
×
963
  taosMemoryFree(pNewTopic);
377,770✔
964
  taosWUnLockLatch(&pOldConsumer->lock);
377,770✔
965

966
  PRINT_LOG_END
377,770✔
967

968
  return code;
377,770✔
969
}
970

971
int32_t mndAcquireConsumer(SMnode *pMnode, int64_t consumerId, SMqConsumerObj** pConsumer) {
5,237,236✔
972
  if (pMnode == NULL || pConsumer == NULL) {
5,237,236✔
UNCOV
973
    return TSDB_CODE_INVALID_PARA;
×
974
  }
975
  SSdb           *pSdb = pMnode->pSdb;
5,237,236✔
976
  *pConsumer = sdbAcquire(pSdb, SDB_CONSUMER, &consumerId);
5,237,236✔
977
  if (*pConsumer == NULL) {
5,236,924✔
978
    return TSDB_CODE_MND_CONSUMER_NOT_EXIST;
153,067✔
979
  }
980
  return 0;
5,083,857✔
981
}
982

983
void mndReleaseConsumer(SMnode *pMnode, SMqConsumerObj *pConsumer) {
19,399,396✔
984
  if (pMnode == NULL || pConsumer == NULL) {
19,399,396✔
985
    return;
12,965,238✔
986
  }
987
  SSdb *pSdb = pMnode->pSdb;
6,434,158✔
988
  sdbRelease(pSdb, pConsumer);
6,433,494✔
989
}
990

991
static int32_t buildResult(SMqConsumerObj *pConsumer, SShowObj *pShow, SSDataBlock *pBlock, int32_t numOfRows, const char* showTopic, bool hasTopic) {
69,774✔
992
  int32_t         code = 0;
69,774✔
993
  int32_t         lino = 0;
69,774✔
994
  SColumnInfoData *pColInfo = NULL;
69,774✔
995
  int32_t          cols = 0;
69,774✔
996
  char * parasStr = NULL;
69,774✔
997
  char           *status = NULL;
69,774✔
998

999
  PRINT_LOG_START
69,774✔
1000
  // consumer id
1001
  char consumerIdHex[TSDB_CONSUMER_ID_LEN + VARSTR_HEADER_SIZE] = {0};
69,774✔
1002
  (void)snprintf(varDataVal(consumerIdHex), TSDB_CONSUMER_ID_LEN, "0x%" PRIx64, pConsumer->consumerId);
69,774✔
1003
  varDataSetLen(consumerIdHex, strlen(varDataVal(consumerIdHex)));
69,774✔
1004

1005
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
69,774✔
1006
  MND_TMQ_NULL_CHECK(pColInfo);
69,774✔
1007
  MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)consumerIdHex, false));
69,774✔
1008

1009
  // consumer group
1010
  char cgroup[TSDB_CGROUP_LEN + VARSTR_HEADER_SIZE] = {0};
69,774✔
1011
  STR_TO_VARSTR(cgroup, pConsumer->cgroup);
69,774✔
1012

1013
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
69,774✔
1014
  MND_TMQ_NULL_CHECK(pColInfo);
69,774✔
1015
  MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)cgroup, false));
69,774✔
1016

1017
  // client id
1018
  char clientId[TSDB_CLIENT_ID_LEN + VARSTR_HEADER_SIZE] = {0};
69,774✔
1019
  STR_TO_VARSTR(clientId, pConsumer->clientId);
69,774✔
1020

1021
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
69,774✔
1022
  MND_TMQ_NULL_CHECK(pColInfo);
69,774✔
1023
  MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)clientId, false));
69,774✔
1024

1025
  // user
1026
  char user[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
69,774✔
1027
  STR_TO_VARSTR(user, pConsumer->user);
69,774✔
1028

1029
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
69,774✔
1030
  MND_TMQ_NULL_CHECK(pColInfo);
69,774✔
1031
  MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)user, false));
69,774✔
1032

1033
  // fqdn
1034
  char fqdn[TSDB_FQDN_LEN + VARSTR_HEADER_SIZE] = {0};
69,774✔
1035
  STR_TO_VARSTR(fqdn, pConsumer->fqdn);
69,774✔
1036

1037
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
69,774✔
1038
  MND_TMQ_NULL_CHECK(pColInfo);
69,774✔
1039
  MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)fqdn, false));
69,774✔
1040

1041
  // status
1042
  const char *pStatusName = mndConsumerStatusName(pConsumer->status);
69,774✔
1043
  status = taosMemoryCalloc(1, pShow->pMeta->pSchemas[cols].bytes);
69,774✔
1044
  MND_TMQ_NULL_CHECK(status);
69,774✔
1045
  STR_TO_VARSTR(status, pStatusName);
69,774✔
1046

1047
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
69,774✔
1048
  MND_TMQ_NULL_CHECK(pColInfo);
69,774✔
1049
  MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)status, false));
69,774✔
1050

1051
  // one subscribed topic
1052
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
69,774✔
1053
  MND_TMQ_NULL_CHECK(pColInfo);
69,774✔
1054
  if (hasTopic) {
69,774✔
1055
    char        topic[TSDB_TOPIC_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
69,774✔
1056
    mndTopicGetShowName(showTopic, topic + VARSTR_HEADER_SIZE);
69,774✔
1057
    *(VarDataLenT *)(topic) = strlen(topic + VARSTR_HEADER_SIZE);
69,774✔
1058
    MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)topic, false));
69,774✔
1059
  } else {
UNCOV
1060
    MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, NULL, true));
×
1061
  }
1062

1063
  // up time
1064
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
69,774✔
1065
  MND_TMQ_NULL_CHECK(pColInfo);
69,774✔
1066
  MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->createTime, false));
69,774✔
1067

1068
  // subscribe time
1069
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
69,774✔
1070
  MND_TMQ_NULL_CHECK(pColInfo);
69,774✔
1071
  MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->subscribeTime, false));
69,774✔
1072

1073
  // rebalance time
1074
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
69,774✔
1075
  MND_TMQ_NULL_CHECK(pColInfo);
69,774✔
1076
  MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->rebalanceTime, pConsumer->rebalanceTime == 0));
69,774✔
1077

1078
  char         buf[TSDB_OFFSET_LEN] = {0};
69,774✔
1079
  STqOffsetVal pVal = {.type = pConsumer->resetOffsetCfg};
69,774✔
1080
  tFormatOffset(buf, TSDB_OFFSET_LEN, &pVal);
69,774✔
1081

1082
  parasStr = taosMemoryCalloc(1, pShow->pMeta->pSchemas[cols].bytes);
69,774✔
1083
  MND_TMQ_NULL_CHECK(parasStr);
69,774✔
1084
  (void)snprintf(varDataVal(parasStr), pShow->pMeta->pSchemas[cols].bytes - VARSTR_HEADER_SIZE, "tbname:%d,commit:%d,interval:%dms,reset:%s", pConsumer->withTbName,
139,548✔
1085
          pConsumer->autoCommit, pConsumer->autoCommitInterval, buf);
69,774✔
1086
  varDataSetLen(parasStr, strlen(varDataVal(parasStr)));
69,774✔
1087

1088
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
69,774✔
1089
  MND_TMQ_NULL_CHECK(pColInfo);
69,774✔
1090
  MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)parasStr, false));
69,774✔
1091

1092
  // rebalance time
1093
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
69,774✔
1094
  MND_TMQ_NULL_CHECK(pColInfo);
69,774✔
1095
  MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->pollTime, pConsumer->pollTime == 0));
69,774✔
1096

1097
END:
69,774✔
1098
  PRINT_LOG_END
69,774✔
1099
  taosMemoryFreeClear(status);
69,774✔
1100
  taosMemoryFreeClear(parasStr);
69,774✔
1101
  return code;
69,774✔
1102
}
1103

1104
static int32_t retrieveOneConsumer(SRpcMsg *pReq, SMqConsumerObj *pConsumer, SUserObj *pOperUser, bool showAll,
70,344✔
1105
                                   int32_t *numOfRows, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
1106
  int32_t code = 0;
70,344✔
1107
  int32_t lino = 0;
70,344✔
1108
  SMnode *pMnode = pReq->info.node;
70,344✔
1109
  PRINT_LOG_START
70,344✔
1110
  taosRLockLatch(&pConsumer->lock);
70,344✔
1111
  mDebug("showing consumer:0x%" PRIx64, pConsumer->consumerId);
70,344✔
1112
  if (taosArrayGetSize(pConsumer->assignedTopics) == 0) {
70,344✔
1113
    mInfo("showing consumer:0x%" PRIx64 " no assigned topic, skip", pConsumer->consumerId);
570✔
1114
    goto END;
570✔
1115
  }
1116

1117
  int32_t topicSz = taosArrayGetSize(pConsumer->assignedTopics);
69,774✔
1118
  bool    hasTopic = true;
69,774✔
1119
  if (topicSz == 0) {
69,774✔
UNCOV
1120
    hasTopic = false;
×
UNCOV
1121
    topicSz = 1;
×
1122
  }
1123

1124
  if (*numOfRows + topicSz > rowsCapacity) {
69,774✔
UNCOV
1125
    MND_TMQ_RETURN_CHECK(blockDataEnsureCapacity(pBlock, *numOfRows + topicSz));
×
1126
  }
1127

1128
  for (int32_t i = 0; i < topicSz; i++) {
139,548✔
1129
    char *pTopicFName = taosArrayGetP(pConsumer->assignedTopics, i);
69,774✔
1130
    if (!showAll && (strncmp(pOperUser->name, pConsumer->user, TSDB_USER_LEN) != 0)) {
69,774✔
NEW
1131
      bool         showConsumer = false;
×
NEW
1132
      SMqTopicObj *pTopic = NULL;
×
NEW
1133
      (void)mndAcquireTopic(pMnode, pTopicFName, &pTopic);
×
NEW
1134
      if (pTopic) {
×
NEW
1135
        SName name = {0};  // 1.topic1
×
NEW
1136
        if (0 == tNameFromString(&name, pTopic->name, T_NAME_ACCT | T_NAME_DB)) {
×
NEW
1137
          if (0 == mndCheckObjPrivilegeRecF(pMnode, pOperUser, PRIV_CONSUMER_SHOW, PRIV_OBJ_TOPIC, pTopic->ownerId,
×
NEW
1138
                                            pTopic->db, name.dbname)) {
×
NEW
1139
            showConsumer = true;
×
1140
          }
1141
        }
NEW
1142
        mndReleaseTopic(pMnode, pTopic);
×
1143
      }
NEW
1144
      if (!showConsumer) {
×
NEW
1145
        continue;
×
1146
      }
1147
    }
1148
    // char  topic[TSDB_TOPIC_FNAME_LEN] = {0};
1149
    // mndTopicGetShowName(showTopic, topic);
1150
    MND_TMQ_RETURN_CHECK(buildResult(pConsumer, pShow, pBlock, *numOfRows, pTopicFName, hasTopic));
69,774✔
1151
    (*numOfRows)++;
69,774✔
1152
  }
1153

1154
END:
69,774✔
1155
  taosRUnLockLatch(&pConsumer->lock);
70,344✔
1156
  PRINT_LOG_END
70,344✔
1157
  return code;
70,344✔
1158
}
1159

1160
static int32_t mndRetrieveConsumer(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
14,890✔
1161
  if (pReq == NULL || pShow == NULL || pBlock == NULL) {
14,890✔
1162
    return TSDB_CODE_INVALID_PARA;
×
1163
  }
1164
  SMnode         *pMnode = pReq->info.node;
14,890✔
1165
  SSdb           *pSdb = pMnode->pSdb;
14,890✔
1166
  int32_t         numOfRows = 0;
14,890✔
1167
  SMqConsumerObj *pConsumer = NULL;
14,890✔
1168
  SUserObj       *pOperUser = NULL;
14,890✔
1169
  int32_t         code = 0;
14,890✔
1170
  int32_t         lino = 0;
14,890✔
1171
  bool            showAll = false;
14,890✔
1172
  char            objFName[TSDB_OBJ_FNAME_LEN + 1] = {0};
14,890✔
1173
  PRINT_LOG_START
14,890✔
1174

1175
  MND_TMQ_RETURN_CHECK(mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser));
14,890✔
1176
  (void)snprintf(objFName, sizeof(objFName), "%d.*", pOperUser->acctId);
14,890✔
1177
  showAll = (0 == mndCheckSysObjPrivilege(pMnode, pOperUser, RPC_MSG_TOKEN(pReq), PRIV_CONSUMER_SHOW,
14,890✔
1178
                                          PRIV_OBJ_TOPIC, 0, objFName, "*"));
1179
  while (numOfRows < rowsCapacity) {
85,234✔
1180
    pShow->pIter = sdbFetch(pSdb, SDB_CONSUMER, pShow->pIter, (void **)&pConsumer);
85,234✔
1181
    if (pShow->pIter == NULL) {
85,234✔
1182
      break;
14,890✔
1183
    }
1184
    MND_TMQ_RETURN_CHECK(retrieveOneConsumer(pReq, pConsumer, pOperUser, showAll,  &numOfRows, pShow, pBlock, rowsCapacity));
70,344✔
1185
    
1186
    pBlock->info.rows = numOfRows;
70,344✔
1187
    sdbRelease(pSdb, pConsumer);
70,344✔
1188
    pConsumer = NULL;
70,344✔
1189
  }
1190

1191
  pShow->numOfRows += numOfRows;
14,890✔
1192

1193
END:
14,890✔
1194
  sdbRelease(pSdb, pConsumer);
14,890✔
1195
  sdbCancelFetch(pSdb, pShow->pIter);
14,890✔
1196
  if (code != 0) {
14,890✔
UNCOV
1197
    mError("show consumer failed, code:%d", code);
×
UNCOV
1198
    return code;
×
1199
  } else {
1200
    mDebug("show consumer processed, numOfRows:%d", numOfRows);
14,890✔
1201
    return numOfRows;
14,890✔
1202
  }
1203
}
1204

UNCOV
1205
static void mndCancelGetNextConsumer(SMnode *pMnode, void *pIter) {
×
UNCOV
1206
  if (pMnode == NULL || pIter == NULL) return;
×
UNCOV
1207
  SSdb *pSdb = pMnode->pSdb;
×
UNCOV
1208
  sdbCancelFetchByType(pSdb, pIter, SDB_CONSUMER);
×
1209
}
1210

1211
const char *mndConsumerStatusName(int status) {
3,699,604✔
1212
  switch (status) {
3,699,604✔
1213
    case MQ_CONSUMER_STATUS_READY:
1,737,949✔
1214
      return "ready";
1,737,949✔
1215
    case MQ_CONSUMER_STATUS_REBALANCE:
1,961,655✔
1216
      return "rebalancing";
1,961,655✔
UNCOV
1217
    default:
×
UNCOV
1218
      return "unknown";
×
1219
  }
1220
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc