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

taosdata / TDengine / #4962

09 Feb 2026 01:16AM UTC coverage: 66.872% (+0.07%) from 66.798%
#4962

push

travis-ci

web-flow
docs: add support for recording STMT to CSV files (#34276)

* docs: add support for recording STMT to CSV files

* docs: update version for STMT recording feature in CSV files

205763 of 307696 relevant lines covered (66.87%)

127640448.9 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) {
410,722✔
46
  SSdbTable table = {
410,722✔
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){
410,722✔
57
    return TSDB_CODE_INVALID_PARA;
×
58
  }
59
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_SUBSCRIBE, mndProcessSubscribeReq);
410,722✔
60
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_HB, mndProcessMqHbReq);
410,722✔
61
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_ASK_EP, mndProcessAskEpReq);
410,722✔
62
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_LOST_CONSUMER_CLEAR, mndProcessConsumerClearMsg);
410,722✔
63

64
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndRetrieveConsumer);
410,722✔
65
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndCancelGetNextConsumer);
410,722✔
66

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

70
void mndCleanupConsumer(SMnode *pMnode) {}
410,665✔
71

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

82
  *(int64_t*)msg = consumerId;
104,831✔
83
  SRpcMsg rpcMsg = {
104,831✔
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);
104,831✔
91
  MND_TMQ_RETURN_CHECK(tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg));
104,831✔
92

93
END:
104,831✔
94
  PRINT_LOG_END
104,831✔
95
  return code;
104,831✔
96
}
97

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

103
  PRINT_LOG_START
157,381✔
104
  MND_TMQ_RETURN_CHECK(mndAcquireTopic(pMnode, pOneTopic, &pTopic));
157,381✔
105
  taosRLockLatch(&pTopic->lock);
155,932✔
106

107
  MND_TMQ_RETURN_CHECK(mndCheckTopicPrivilege(pMnode, user, token, MND_OPER_SUBSCRIBE, pTopic));
155,932✔
108
  MND_TMQ_RETURN_CHECK(grantCheckExpire(TSDB_GRANT_SUBSCRIPTION));
155,780✔
109

110
  if (subscribe->enableReplay) {
155,780✔
111
    if (pTopic->subType != TOPIC_SUB_TYPE__COLUMN) {
1,890✔
112
      code = TSDB_CODE_TMQ_REPLAY_NOT_SUPPORT;
540✔
113
      goto END;
540✔
114
    } 
115
    if (pTopic->stbName[0] != 0) {
1,350✔
116
      SDbObj *pDb = mndAcquireDb(pMnode, pTopic->db);
1,080✔
117
      if (pDb == NULL) {
1,080✔
118
        code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
119
        goto END;
×
120
      }
121
      if (pDb->cfg.numOfVgroups != 1) {
1,080✔
122
        mndReleaseDb(pMnode, pDb);
270✔
123
        code = TSDB_CODE_TMQ_REPLAY_NEED_ONE_VGROUP;
270✔
124
        goto END;
270✔
125
      }
126
      mndReleaseDb(pMnode, pDb);
810✔
127
    }
128
  }
129
  char  key[TSDB_CONSUMER_ID_LEN] = {0};
154,970✔
130
  (void)snprintf(key, TSDB_CONSUMER_ID_LEN, "%"PRIx64, subscribe->consumerId);
154,970✔
131
  mndTransSetDbName(pTrans, pTopic->db, key);
154,970✔
132
  MND_TMQ_RETURN_CHECK(mndTransCheckConflict(pMnode, pTrans));
154,970✔
133

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

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

150
  PRINT_LOG_START
263,412✔
151
  int32_t numOfTopics = taosArrayGetSize(subscribe->topicNames);
263,412✔
152
  for (int32_t i = 0; i < numOfTopics; i++) {
418,382✔
153
    char *pOneTopic = taosArrayGetP(subscribe->topicNames, i);
157,381✔
154
    MND_TMQ_RETURN_CHECK(validateOneTopic(pTrans, pOneTopic, subscribe, pMnode, user, token));
157,381✔
155
  }
156

157
END:
261,001✔
158
  PRINT_LOG_END
263,412✔
159
  return code;
263,412✔
160
}
161

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

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

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

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

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

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

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

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

230
  rsp->topicPrivileges = taosArrayInit(taosArrayGetSize(pConsumer->currentTopics), sizeof(STopicPrivilege));
966,868✔
231
  MND_TMQ_NULL_CHECK(rsp->topicPrivileges);
966,868✔
232
  for (int32_t i = 0; i < taosArrayGetSize(pConsumer->currentTopics); i++) {
1,857,512✔
233
    char        *topic = taosArrayGetP(pConsumer->currentTopics, i);
890,644✔
234
    checkOnePrivilege(topic, pMnode, rsp, user, token);
890,644✔
235
  }
236

237
END:
966,868✔
238
  PRINT_LOG_END
966,868✔
239
  return code;
966,868✔
240
}
241

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

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

270
    mndReleaseSubscribe(pMnode, pSub);
876,340✔
271
  }
272
}
273

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

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

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

308
  MND_TMQ_RETURN_CHECK(tDeserializeSMqHbReq(pMsg->pCont, pMsg->contLen, &req));
973,128✔
309
  int64_t consumerId = req.consumerId;
973,128✔
310
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
973,128✔
311
  taosWLockLatch(&pConsumer->lock);
966,868✔
312
  MND_TMQ_RETURN_CHECK(checkPrivilege(pMnode, pConsumer, &rsp, RPC_MSG_USER(pMsg), RPC_MSG_TOKEN(pMsg)));
966,868✔
313
  atomic_store_32(&pConsumer->hbStatus, 0);
966,868✔
314
  mDebug("consumer:0x%" PRIx64 " receive hb pollFlag:%d pollStatus:%d", consumerId, req.pollFlag, pConsumer->pollStatus);
966,868✔
315
  if (req.pollFlag == 1){
966,868✔
316
    atomic_store_32(&pConsumer->pollStatus, 0);
461,747✔
317
    pConsumer->pollTime = taosGetTimestampMs();
923,494✔
318
  }
319

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

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

335
static int32_t processEachTopicEp(SMnode *pMnode, SMqConsumerObj *pConsumer, char *topic, SMqAskEpRsp *rsp, int32_t epoch) {
964,976✔
336
  int32_t         code = 0;
964,976✔
337
  int32_t         lino = 0;
964,976✔
338
  SMqSubscribeObj *pSub = NULL;
964,976✔
339
  SMqSubTopicEp topicEp = {0};
964,976✔
340
  char  key[TSDB_SUBSCRIBE_KEY_LEN] = {0};
964,976✔
341
  PRINT_LOG_START
964,976✔
342
  (void)snprintf(key, TSDB_SUBSCRIBE_KEY_LEN, "%s%s%s", pConsumer->cgroup, TMQ_SEPARATOR, topic);
964,976✔
343
  if(mndAcquireSubscribeByKey(pMnode, key, &pSub) != 0) {
964,976✔
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);
964,976✔
348

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

357
  tstrncpy(topicEp.db, pSub->dbName, TSDB_DB_FNAME_LEN);
964,976✔
358
  for (int32_t j = 0; j < vgNum; j++) {
2,202,312✔
359
    SMqVgEp *pVgEp = taosArrayGet(pConsumerEp->vgs, j);
1,237,336✔
360
    if (pVgEp == NULL) {
1,237,336✔
361
      continue;
×
362
    }
363
    if (epoch == -1) {
1,237,336✔
364
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, pVgEp->vgId);
810,661✔
365
      if (pVgroup) {
810,661✔
366
        pVgEp->epSet = mndGetVgroupEpset(pMnode, pVgroup);
42,744✔
367
        mndReleaseVgroup(pMnode, pVgroup);
42,744✔
368
      }
369
    }
370
    SMqSubVgEp vgEp = {.epSet = pVgEp->epSet, .vgId = pVgEp->vgId, .offset = -1};
1,237,336✔
371
    MND_TMQ_NULL_CHECK(taosArrayPush(topicEp.vgs, &vgEp));
2,474,672✔
372
  }
373
  MND_TMQ_NULL_CHECK(taosArrayPush(rsp->topics, &topicEp));
1,929,952✔
374
  topicEp.vgs = NULL;
964,976✔
375

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

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

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

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

404
END:
1,071,007✔
405
  PRINT_LOG_END
1,071,007✔
406
  return code;
1,071,007✔
407
}
408

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

421
  SMqRspHead *pHead = buf;
4,006,669✔
422

423
  pHead->mqMsgType = TMQ_MSG_TYPE__EP_RSP;
4,006,669✔
424
  pHead->epoch = serverEpoch;
4,006,669✔
425
  pHead->consumerId = consumerId;
4,006,669✔
426
  pHead->walsver = 0;
4,006,669✔
427
  pHead->walever = 0;
4,006,669✔
428

429
  void *abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
4,006,669✔
430
  if (tEncodeSMqAskEpRsp(&abuf, rsp) < 0) {
4,006,669✔
431
    rpcFreeCont(buf);
×
432
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
433
  }
434

435
  // send rsp
436
  pMsg->info.rsp = buf;
4,006,669✔
437
  pMsg->info.rspLen = tlen;
4,006,328✔
438
  return code;
4,006,328✔
439
}
440

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

453
  MND_TMQ_RETURN_CHECK(tDeserializeSMqAskEpReq(pMsg->pCont, pMsg->contLen, &req));
4,010,013✔
454
  int64_t consumerId = req.consumerId;
4,010,013✔
455
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
4,010,013✔
456
  taosRLockLatch(&pConsumer->lock);
4,006,669✔
457
  if (strncmp(req.cgroup, pConsumer->cgroup, tListLen(pConsumer->cgroup)) != 0) {
4,006,669✔
458
    mError("consumer:0x%" PRIx64 " group:%s not consistent with data in sdb, saved cgroup:%s", consumerId, req.cgroup,
×
459
           pConsumer->cgroup);
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);
4,006,669✔
466
  int32_t status = atomic_load_32(&pConsumer->status);
4,006,669✔
467
  if (status != MQ_CONSUMER_STATUS_READY) {
4,006,669✔
468
    mInfo("consumer:0x%" PRIx64 " not ready, status: %s", consumerId, mndConsumerStatusName(status));
891,892✔
469
    rsp.code = TSDB_CODE_MND_CONSUMER_NOT_READY;
891,892✔
470
  } else {
471
    int32_t epoch = req.epoch;
3,114,777✔
472

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

482
END:
4,009,672✔
483
  if (pConsumer != NULL) {
4,009,672✔
484
    taosRUnLockLatch(&pConsumer->lock);
4,006,328✔
485
  }
486
  tDeleteSMqAskEpRsp(&rsp);
487
  mndReleaseConsumer(pMnode, pConsumer);
4,010,013✔
488
  PRINT_LOG_END
4,010,013✔
489
  return code;
4,010,013✔
490
}
491

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

508
END:
104,241✔
509
  PRINT_LOG_END
104,241✔
510
  return code;
104,241✔
511
}
512

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

530
  return code;
531,383✔
531
}
532

533
static void freeItem(void *param) {
316✔
534
  if (param == NULL) {
316✔
535
    return;
×
536
  }
537
  void *pItem = *(void **)param;
316✔
538
  if (pItem != NULL) {
316✔
539
    taosMemoryFree(pItem);
316✔
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){
113,979✔
553
  if (pExistedConsumer == NULL || pConsumerNew == NULL) {
113,979✔
554
    return TSDB_CODE_INVALID_PARA;
×
555
  }
556
  int32_t code = 0;
113,979✔
557
  int32_t lino = 0;
113,979✔
558
  PRINT_LOG_START
113,979✔
559
  taosRLockLatch(&pExistedConsumer->lock);
113,979✔
560

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

566
  int32_t newTopicNum = taosArrayGetSize(pConsumerNew->assignedTopics);
113,979✔
567
  int32_t oldTopicNum = taosArrayGetSize(pExistedConsumer->currentTopics);
113,979✔
568
  int32_t i = 0, j = 0;
113,979✔
569
  while (i < oldTopicNum || j < newTopicNum) {
230,504✔
570
    if (i >= oldTopicNum) {
116,525✔
571
      void* tmp = taosArrayGetP(pConsumerNew->assignedTopics, j);
632✔
572
      MND_TMQ_NULL_CHECK(tmp);
632✔
573
      ADD_TOPIC_TO_ARRAY(tmp, rebNewTopics);
1,264✔
574
      j++;
632✔
575
      continue;
632✔
576
    } else if (j >= newTopicNum) {
115,893✔
577
      void* tmp = taosArrayGetP(pExistedConsumer->currentTopics, i);
111,123✔
578
      MND_TMQ_NULL_CHECK(tmp);
111,123✔
579
      ADD_TOPIC_TO_ARRAY(tmp, rebRemovedTopics);
222,246✔
580
      i++;
111,123✔
581
      continue;
111,123✔
582
    } else {
583
      char *oldTopic = taosArrayGetP(pExistedConsumer->currentTopics, i);
4,770✔
584
      MND_TMQ_NULL_CHECK(oldTopic);
4,770✔
585
      char *newTopic = taosArrayGetP(pConsumerNew->assignedTopics, j);
4,770✔
586
      MND_TMQ_NULL_CHECK(newTopic);
4,770✔
587
      int   comp = strcmp(oldTopic, newTopic);
4,770✔
588
      if (comp == 0) {
4,770✔
589
        i++;
4,770✔
590
        j++;
4,770✔
591
        continue;
4,770✔
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) {
113,979✔
605
    code = TSDB_CODE_TMQ_NO_NEED_REBALANCE;
4,770✔
606
  }
607

608
END:
109,209✔
609
  taosRUnLockLatch(&pExistedConsumer->lock);
113,979✔
610
  PRINT_LOG_END
113,979✔
611
  return code;
113,979✔
612
}
613

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

621
  int32_t newTopicNum = taosArrayGetSize(pTopicList);
263,753✔
622
  for (int i = 0; i < newTopicNum; i++) {
421,134✔
623
    int32_t gNum = mndGetGroupNumByTopic(pMnode, (const char *)taosArrayGetP(pTopicList, i));
157,722✔
624
    if (gNum >= MND_MAX_GROUP_PER_TOPIC) {
157,722✔
625
      return TSDB_CODE_TMQ_GROUP_OUT_OF_RANGE;
341✔
626
    }
627
  }
628
  return 0;
263,412✔
629
}
630

631
static int32_t buildSubConsumer(SMnode *pMnode, SCMSubscribeReq *subscribe, SMqConsumerObj** ppConsumer){
261,001✔
632
  if (pMnode == NULL || subscribe == NULL) {
261,001✔
633
    return TSDB_CODE_INVALID_PARA;
×
634
  }
635
  int64_t         consumerId = subscribe->consumerId;
261,001✔
636
  char           *cgroup     = subscribe->cgroup;
261,001✔
637
  SMqConsumerObj *pConsumerNew     = NULL;
261,001✔
638
  SMqConsumerObj *pExistedConsumer = NULL;
261,001✔
639
  int32_t lino = 0;
261,001✔
640
  PRINT_LOG_START
261,001✔
641
  int32_t code = mndAcquireConsumer(pMnode, consumerId, &pExistedConsumer);
261,001✔
642
  if (code != 0) {
261,001✔
643
    mInfo("receive tmq subscribe request from new consumer:0x%" PRIx64
147,022✔
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));
147,022✔
648
  } else {
649
    int32_t status = atomic_load_32(&pExistedConsumer->status);
113,979✔
650

651
    mInfo("receive tmq subscribe request from existed consumer:0x%" PRIx64
113,979✔
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) {
113,979✔
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));
113,979✔
661
    MND_TMQ_RETURN_CHECK(getTopicAddDelete(pExistedConsumer, pConsumerNew));
113,979✔
662
  }
663
  if (ppConsumer){
256,231✔
664
    *ppConsumer = pConsumerNew;
256,231✔
665
    pConsumerNew = NULL;
256,231✔
666
  }
667

668
END:
260,695✔
669
  PRINT_LOG_END
261,001✔
670
  mndReleaseConsumer(pMnode, pExistedConsumer);
261,001✔
671
  tDeleteSMqConsumerObj(pConsumerNew);
261,001✔
672
  return code;
261,001✔
673
}
674

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

686
  PRINT_LOG_START
350,656✔
687
  SCMSubscribeReq subscribe = {0};
350,656✔
688
  MND_TMQ_RETURN_CHECK(tDeserializeSCMSubscribeReq(msgStr, &subscribe, pMsg->contLen));
701,312✔
689
  bool unSubscribe = (taosArrayGetSize(subscribe.topicNames) == 0);
350,656✔
690
  if(unSubscribe){
350,656✔
691
    SMqConsumerObj *pConsumerTmp = NULL;
195,480✔
692
    MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, subscribe.consumerId, &pConsumerTmp));
195,480✔
693
    taosRLockLatch(&pConsumerTmp->lock);
193,726✔
694
    size_t topicNum = taosArrayGetSize(pConsumerTmp->assignedTopics);
193,726✔
695
    taosRUnLockLatch(&pConsumerTmp->lock);
193,726✔
696
    mndReleaseConsumer(pMnode, pConsumerTmp);
193,726✔
697
    if (topicNum == 0){
193,726✔
698
      goto END;
85,149✔
699
    }
700
  }
701
  MND_TMQ_RETURN_CHECK(checkAndSortTopic(pMnode, subscribe.topicNames));
263,753✔
702
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY,
263,412✔
703
                          (unSubscribe ? TRN_CONFLICT_NOTHING :TRN_CONFLICT_DB_INSIDE),
704
                          pMsg, "subscribe");
705
  MND_TMQ_NULL_CHECK(pTrans);
263,412✔
706

707
  MND_TMQ_RETURN_CHECK(validateTopics(pTrans, &subscribe, pMnode, RPC_MSG_USER(pMsg), RPC_MSG_TOKEN(pMsg)));
263,412✔
708
  MND_TMQ_RETURN_CHECK(buildSubConsumer(pMnode, &subscribe, &pConsumerNew));
261,001✔
709
  MND_TMQ_RETURN_CHECK(mndSetConsumerCommitLogs(pTrans, pConsumerNew));
256,231✔
710
  MND_TMQ_RETURN_CHECK(mndTransPrepare(pMnode, pTrans));
256,231✔
711
  code = TSDB_CODE_ACTION_IN_PROGRESS;
256,231✔
712

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

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

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

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

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

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

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

755
  terrno = TSDB_CODE_SUCCESS;
751,192✔
756

757
CM_ENCODE_OVER:
751,192✔
758
  taosMemoryFreeClear(buf);
751,192✔
759
  if (terrno != 0) {
751,192✔
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);
751,192✔
766
  return pRaw;
751,192✔
767
}
768

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

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

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

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

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

800
  int32_t dataPos = 0;
648,449✔
801
  int32_t len;
647,735✔
802
  SDB_GET_INT32(pRaw, dataPos, &len, CM_DECODE_OVER);
648,449✔
803
  buf = taosMemoryMalloc(len);
648,449✔
804
  if (buf == NULL) {
648,449✔
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);
648,449✔
810
  SDB_GET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_DECODE_OVER);
648,449✔
811

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

817
  tmsgUpdateDnodeEpSet(&pConsumer->ep);
648,449✔
818

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

827
  return pRow;
648,449✔
828
}
829

830
static int32_t mndConsumerActionInsert(SSdb *pSdb, SMqConsumerObj *pConsumer) {
154,988✔
831
  if (pConsumer == NULL) {
154,988✔
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,
154,988✔
835
        pConsumer->status, mndConsumerStatusName(pConsumer->status), pConsumer->epoch);
836
  pConsumer->subscribeTime = pConsumer->createTime;
154,988✔
837
  return 0;
154,988✔
838
}
839

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

850
// remove from topic list
851
static void removeFromTopicList(SArray *topicList, const char *pTopic, int64_t consumerId, char *type) {
383,040✔
852
  if (topicList == NULL || pTopic == NULL) {
383,040✔
853
    return;
×
854
  }
855
  int32_t size = taosArrayGetSize(topicList);
383,040✔
856
  for (int32_t i = 0; i < size; i++) {
387,000✔
857
    char *p = taosArrayGetP(topicList, i);
383,435✔
858
    if (strcmp(pTopic, p) == 0) {
383,435✔
859
      taosArrayRemove(topicList, i);
379,475✔
860
      taosMemoryFree(p);
379,475✔
861

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

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

883
  return existing;
151,510✔
884
}
885

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

898
  if (pNewConsumer->updateType == CONSUMER_UPDATE_SUB) {
388,143✔
899
    TSWAP(pOldConsumer->rebNewTopics, pNewConsumer->rebNewTopics);
110,286✔
900
    TSWAP(pOldConsumer->rebRemovedTopics, pNewConsumer->rebRemovedTopics);
110,286✔
901
    TSWAP(pOldConsumer->assignedTopics, pNewConsumer->assignedTopics);
110,286✔
902

903
    pOldConsumer->subscribeTime = taosGetTimestampMs();
110,286✔
904
    pOldConsumer->status = MQ_CONSUMER_STATUS_REBALANCE;
110,286✔
905
    mInfo("consumer:0x%" PRIx64 " subscribe update, modify existed consumer", pOldConsumer->consumerId);
110,286✔
906
  } else if (pNewConsumer->updateType == CONSUMER_UPDATE_REB) {
277,857✔
907
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
10,582✔
908
    pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
10,582✔
909
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
10,582✔
910
    mInfo("tmq rebalance consumer:0x%" PRIx64 " rebalance update, only rebalance time", pOldConsumer->consumerId);
10,582✔
911
  } else if (pNewConsumer->updateType == CONSUMER_ADD_REB) {
267,275✔
912
    void *tmp = taosArrayGetP(pNewConsumer->rebNewTopics, 0);
151,510✔
913
    MND_TMQ_NULL_CHECK(tmp);
151,510✔
914
    char *pNewTopic = taosStrdup(tmp);
151,510✔
915
    MND_TMQ_NULL_CHECK(pNewTopic);
151,510✔
916
    removeFromTopicList(pOldConsumer->rebNewTopics, pNewTopic, pOldConsumer->consumerId, "new");
151,510✔
917
    bool existing = existInCurrentTopicList(pOldConsumer, pNewTopic);
151,510✔
918
    if (existing) {
151,510✔
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));
303,020✔
922
      pNewTopic = NULL;
151,510✔
923
      taosArraySort(pOldConsumer->currentTopics, taosArrayCompareString);
151,510✔
924
    }
925

926
    int32_t status = pOldConsumer->status;
151,510✔
927
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
151,510✔
928
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
148,964✔
929
    }
930

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

934
    mInfo("tmq rebalance consumer:0x%" PRIx64 " rebalance update add, state (%d)%s -> (%d)%s, new epoch:%d, reb-time:%" PRId64
151,510✔
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) {
115,765✔
942
    char *topic = taosArrayGetP(pNewConsumer->rebRemovedTopics, 0);
115,765✔
943
    MND_TMQ_NULL_CHECK(topic);
115,765✔
944
    removeFromTopicList(pOldConsumer->rebRemovedTopics, topic, pOldConsumer->consumerId, "remove");
115,765✔
945
    removeFromTopicList(pOldConsumer->currentTopics, topic, pOldConsumer->consumerId, "current");
115,765✔
946

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

954
    mInfo("tmq rebalanceconsumer:0x%" PRIx64 " rebalance update remove, state (%d)%s -> (%d)%s, new epoch:%d, reb-time:%" PRId64
115,765✔
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

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

966
  PRINT_LOG_END
388,143✔
967

968
  return code;
388,143✔
969
}
970

971
int32_t mndAcquireConsumer(SMnode *pMnode, int64_t consumerId, SMqConsumerObj** pConsumer) {
5,544,453✔
972
  if (pMnode == NULL || pConsumer == NULL) {
5,544,453✔
973
    return TSDB_CODE_INVALID_PARA;
×
974
  }
975
  SSdb           *pSdb = pMnode->pSdb;
5,544,453✔
976
  *pConsumer = sdbAcquire(pSdb, SDB_CONSUMER, &consumerId);
5,544,453✔
977
  if (*pConsumer == NULL) {
5,544,453✔
978
    return TSDB_CODE_MND_CONSUMER_NOT_EXIST;
159,959✔
979
  }
980
  return 0;
5,384,494✔
981
}
982

983
void mndReleaseConsumer(SMnode *pMnode, SMqConsumerObj *pConsumer) {
20,384,526✔
984
  if (pMnode == NULL || pConsumer == NULL) {
20,384,526✔
985
    return;
13,631,858✔
986
  }
987
  SSdb *pSdb = pMnode->pSdb;
6,752,668✔
988
  sdbRelease(pSdb, pConsumer);
6,752,327✔
989
}
990

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1128
  for (int32_t i = 0; i < topicSz; i++) {
103,298✔
1129
    char *pTopicFName = taosArrayGetP(pConsumer->assignedTopics, i);
51,649✔
1130
    if (!showAll && (strncmp(pOperUser->name, pConsumer->user, TSDB_USER_LEN) != 0)) {
51,649✔
1131
      bool         showConsumer = false;
×
1132
      SMqTopicObj *pTopic = NULL;
×
1133
      (void)mndAcquireTopic(pMnode, pTopicFName, &pTopic);
×
1134
      if (pTopic) {
×
1135
        SName name = {0};  // 1.topic1
×
1136
        if (0 == tNameFromString(&name, pTopic->name, T_NAME_ACCT | T_NAME_DB)) {
×
1137
          if (0 == mndCheckObjPrivilegeRecF(pMnode, pOperUser, PRIV_CONSUMER_SHOW, PRIV_OBJ_TOPIC, pTopic->ownerId,
×
1138
                                            pTopic->db, name.dbname)) {
×
1139
            showConsumer = true;
×
1140
          }
1141
        }
1142
        mndReleaseTopic(pMnode, pTopic);
×
1143
      }
1144
      if (!showConsumer) {
×
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));
51,649✔
1151
    (*numOfRows)++;
51,649✔
1152
  }
1153

1154
END:
51,649✔
1155
  taosRUnLockLatch(&pConsumer->lock);
52,238✔
1156
  PRINT_LOG_END
52,238✔
1157
  return code;
52,238✔
1158
}
1159

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

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

1191
  pShow->numOfRows += numOfRows;
15,264✔
1192

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

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

1211
const char *mndConsumerStatusName(int status) {
3,870,498✔
1212
  switch (status) {
3,870,498✔
1213
    case MQ_CONSUMER_STATUS_READY:
1,763,187✔
1214
      return "ready";
1,763,187✔
1215
    case MQ_CONSUMER_STATUS_REBALANCE:
2,107,311✔
1216
      return "rebalancing";
2,107,311✔
1217
    default:
×
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