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

taosdata / TDengine / #4980

10 Mar 2026 08:57AM UTC coverage: 68.492% (-0.02%) from 68.512%
#4980

push

travis-ci

web-flow
fix: add retry while exec ci case test_stable_keep_compact.py. (#34729)

211901 of 309380 relevant lines covered (68.49%)

135171938.8 hits per line

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

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

64
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndRetrieveConsumer);
438,157✔
65
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndCancelGetNextConsumer);
438,157✔
66

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

70
void mndCleanupConsumer(SMnode *pMnode) {}
438,092✔
71

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

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

93
END:
115,373✔
94
  PRINT_LOG_END
115,373✔
95
  return code;
115,373✔
96
}
97

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

103
  PRINT_LOG_START
169,684✔
104
  MND_TMQ_RETURN_CHECK(mndAcquireTopic(pMnode, pOneTopic, &pTopic));
169,684✔
105
  taosRLockLatch(&pTopic->lock);
168,204✔
106

107
  MND_TMQ_RETURN_CHECK(mndCheckTopicPrivilege(pMnode, user, token, MND_OPER_SUBSCRIBE, pTopic));
168,204✔
108
  MND_TMQ_RETURN_CHECK(grantCheckExpire(TSDB_GRANT_SUBSCRIPTION));
168,033✔
109

110
  if (subscribe->enableReplay) {
168,033✔
111
    if (pTopic->subType != TOPIC_SUB_TYPE__COLUMN) {
2,009✔
112
      code = TSDB_CODE_TMQ_REPLAY_NOT_SUPPORT;
574✔
113
      goto END;
574✔
114
    } 
115
    if (pTopic->stbName[0] != 0) {
1,435✔
116
      SDbObj *pDb = mndAcquireDb(pMnode, pTopic->db);
1,148✔
117
      if (pDb == NULL) {
1,148✔
118
        code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
119
        goto END;
×
120
      }
121
      if (pDb->cfg.numOfVgroups != 1) {
1,148✔
122
        mndReleaseDb(pMnode, pDb);
287✔
123
        code = TSDB_CODE_TMQ_REPLAY_NEED_ONE_VGROUP;
287✔
124
        goto END;
287✔
125
      }
126
      mndReleaseDb(pMnode, pDb);
861✔
127
    }
128
  }
129
  char  key[TSDB_CONSUMER_ID_LEN] = {0};
167,172✔
130
  (void)snprintf(key, TSDB_CONSUMER_ID_LEN, "%"PRIx64, subscribe->consumerId);
167,172✔
131
  mndTransSetDbName(pTrans, pTopic->db, key);
167,172✔
132
  MND_TMQ_RETURN_CHECK(mndTransCheckConflict(pMnode, pTrans));
167,172✔
133

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

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

150
  PRINT_LOG_START
285,806✔
151
  int32_t numOfTopics = taosArrayGetSize(subscribe->topicNames);
285,806✔
152
  for (int32_t i = 0; i < numOfTopics; i++) {
452,978✔
153
    char *pOneTopic = taosArrayGetP(subscribe->topicNames, i);
169,684✔
154
    MND_TMQ_RETURN_CHECK(validateOneTopic(pTrans, pOneTopic, subscribe, pMnode, user, token));
169,684✔
155
  }
156

157
END:
283,294✔
158
  PRINT_LOG_END
285,806✔
159
  return code;
285,806✔
160
}
161

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

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

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

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

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

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

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

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

230
  rsp->topicPrivileges = taosArrayInit(taosArrayGetSize(pConsumer->currentTopics), sizeof(STopicPrivilege));
1,006,509✔
231
  MND_TMQ_NULL_CHECK(rsp->topicPrivileges);
1,006,509✔
232
  for (int32_t i = 0; i < taosArrayGetSize(pConsumer->currentTopics); i++) {
1,934,927✔
233
    char        *topic = taosArrayGetP(pConsumer->currentTopics, i);
928,418✔
234
    checkOnePrivilege(topic, pMnode, rsp, user, token);
928,418✔
235
  }
236

237
END:
1,006,509✔
238
  PRINT_LOG_END
1,006,509✔
239
  return code;
1,006,509✔
240
}
241

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

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

270
    mndReleaseSubscribe(pMnode, pSub);
917,007✔
271
  }
272
}
273

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

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

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

308
  MND_TMQ_RETURN_CHECK(tDeserializeSMqHbReq(pMsg->pCont, pMsg->contLen, &req));
1,014,447✔
309
  int64_t consumerId = req.consumerId;
1,014,447✔
310
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
1,014,447✔
311
  taosWLockLatch(&pConsumer->lock);
1,006,509✔
312
  MND_TMQ_RETURN_CHECK(checkPrivilege(pMnode, pConsumer, &rsp, RPC_MSG_USER(pMsg), RPC_MSG_TOKEN(pMsg)));
1,006,509✔
313
  atomic_store_32(&pConsumer->hbStatus, 0);
1,006,509✔
314
  mDebug("consumer:0x%" PRIx64 " receive hb pollFlag:%d pollStatus:%d", consumerId, req.pollFlag, pConsumer->pollStatus);
1,006,509✔
315
  if (req.pollFlag == 1){
1,006,509✔
316
    atomic_store_32(&pConsumer->pollStatus, 0);
498,023✔
317
    pConsumer->pollTime = taosGetTimestampMs();
996,046✔
318
  }
319

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

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

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

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

357
  tstrncpy(topicEp.db, pSub->dbName, TSDB_DB_FNAME_LEN);
980,541✔
358
  for (int32_t j = 0; j < vgNum; j++) {
2,263,843✔
359
    SMqVgEp *pVgEp = taosArrayGet(pConsumerEp->vgs, j);
1,283,302✔
360
    if (pVgEp == NULL) {
1,283,302✔
361
      continue;
×
362
    }
363
    if (epoch == -1) {
1,283,302✔
364
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, pVgEp->vgId);
813,683✔
365
      if (pVgroup) {
813,683✔
366
        pVgEp->epSet = mndGetVgroupEpset(pMnode, pVgroup);
54,797✔
367
        mndReleaseVgroup(pMnode, pVgroup);
54,797✔
368
      }
369
    }
370
    SMqSubVgEp vgEp = {.epSet = pVgEp->epSet, .vgId = pVgEp->vgId, .offset = -1};
1,283,302✔
371
    MND_TMQ_NULL_CHECK(taosArrayPush(topicEp.vgs, &vgEp));
2,566,604✔
372
  }
373
  MND_TMQ_NULL_CHECK(taosArrayPush(rsp->topics, &topicEp));
1,961,082✔
374
  topicEp.vgs = NULL;
980,541✔
375

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

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

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

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

404
END:
1,096,631✔
405
  PRINT_LOG_END
1,096,631✔
406
  return code;
1,096,631✔
407
}
408

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

421
  SMqRspHead *pHead = buf;
4,119,173✔
422

423
  pHead->mqMsgType = TMQ_MSG_TYPE__EP_RSP;
4,119,173✔
424
  pHead->epoch = serverEpoch;
4,119,173✔
425
  pHead->consumerId = consumerId;
4,119,509✔
426
  pHead->walsver = 0;
4,118,837✔
427
  pHead->walever = 0;
4,118,837✔
428

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

435
  // send rsp
436
  pMsg->info.rsp = buf;
4,118,859✔
437
  pMsg->info.rspLen = tlen;
4,118,859✔
438
  return code;
4,119,195✔
439
}
440

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

453
  MND_TMQ_RETURN_CHECK(tDeserializeSMqAskEpReq(pMsg->pCont, pMsg->contLen, &req));
4,123,815✔
454
  int64_t consumerId = req.consumerId;
4,124,883✔
455
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
4,124,883✔
456
  taosRLockLatch(&pConsumer->lock);
4,120,223✔
457
  if (strncmp(req.cgroup, pConsumer->cgroup, tListLen(pConsumer->cgroup)) != 0) {
4,120,223✔
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,118,870✔
466
  int32_t status = atomic_load_32(&pConsumer->status);
4,120,223✔
467
  if (status != MQ_CONSUMER_STATUS_READY) {
4,118,512✔
468
    mInfo("consumer:0x%" PRIx64 " not ready, status: %s", consumerId, mndConsumerStatusName(status));
928,158✔
469
    rsp.code = TSDB_CODE_MND_CONSUMER_NOT_READY;
928,483✔
470
  } else {
471
    int32_t epoch = req.epoch;
3,190,354✔
472

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

482
END:
4,123,835✔
483
  if (pConsumer != NULL) {
4,124,191✔
484
    taosRUnLockLatch(&pConsumer->lock);
4,119,511✔
485
  }
486
  tDeleteSMqAskEpRsp(&rsp);
487
  mndReleaseConsumer(pMnode, pConsumer);
4,122,827✔
488
  PRINT_LOG_END
4,124,883✔
489
  return code;
4,124,883✔
490
}
491

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

508
END:
115,093✔
509
  PRINT_LOG_END
115,093✔
510
  return code;
115,093✔
511
}
512

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

530
  return code;
576,671✔
531
}
532

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

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

566
  int32_t newTopicNum = taosArrayGetSize(pConsumerNew->assignedTopics);
123,791✔
567
  int32_t oldTopicNum = taosArrayGetSize(pExistedConsumer->currentTopics);
123,791✔
568
  int32_t i = 0, j = 0;
123,791✔
569
  while (i < oldTopicNum || j < newTopicNum) {
250,278✔
570
    if (i >= oldTopicNum) {
126,487✔
571
      void* tmp = taosArrayGetP(pConsumerNew->assignedTopics, j);
989✔
572
      MND_TMQ_NULL_CHECK(tmp);
989✔
573
      ADD_TOPIC_TO_ARRAY(tmp, rebNewTopics);
1,978✔
574
      j++;
989✔
575
      continue;
989✔
576
    } else if (j >= newTopicNum) {
125,498✔
577
      void* tmp = taosArrayGetP(pExistedConsumer->currentTopics, i);
121,514✔
578
      MND_TMQ_NULL_CHECK(tmp);
121,514✔
579
      ADD_TOPIC_TO_ARRAY(tmp, rebRemovedTopics);
243,028✔
580
      i++;
121,514✔
581
      continue;
121,514✔
582
    } else {
583
      char *oldTopic = taosArrayGetP(pExistedConsumer->currentTopics, i);
3,984✔
584
      MND_TMQ_NULL_CHECK(oldTopic);
3,984✔
585
      char *newTopic = taosArrayGetP(pConsumerNew->assignedTopics, j);
3,984✔
586
      MND_TMQ_NULL_CHECK(newTopic);
3,984✔
587
      int   comp = strcmp(oldTopic, newTopic);
3,984✔
588
      if (comp == 0) {
3,984✔
589
        i++;
3,984✔
590
        j++;
3,984✔
591
        continue;
3,984✔
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) {
123,791✔
605
    code = TSDB_CODE_TMQ_NO_NEED_REBALANCE;
3,984✔
606
  }
607

608
END:
119,807✔
609
  taosRUnLockLatch(&pExistedConsumer->lock);
123,791✔
610
  PRINT_LOG_END
123,791✔
611
  return code;
123,791✔
612
}
613

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

621
  int32_t newTopicNum = taosArrayGetSize(pTopicList);
286,164✔
622
  for (int i = 0; i < newTopicNum; i++) {
455,848✔
623
    int32_t gNum = mndGetGroupNumByTopic(pMnode, (const char *)taosArrayGetP(pTopicList, i));
170,042✔
624
    if (gNum >= MND_MAX_GROUP_PER_TOPIC) {
170,042✔
625
      return TSDB_CODE_TMQ_GROUP_OUT_OF_RANGE;
358✔
626
    }
627
  }
628
  return 0;
285,806✔
629
}
630

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

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

668
END:
282,970✔
669
  PRINT_LOG_END
283,294✔
670
  mndReleaseConsumer(pMnode, pExistedConsumer);
283,294✔
671
  tDeleteSMqConsumerObj(pConsumerNew);
283,294✔
672
  return code;
283,294✔
673
}
674

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

686
  PRINT_LOG_START
382,634✔
687
  SCMSubscribeReq subscribe = {0};
382,634✔
688
  MND_TMQ_RETURN_CHECK(tDeserializeSCMSubscribeReq(msgStr, &subscribe, pMsg->contLen));
765,268✔
689
  bool unSubscribe = (taosArrayGetSize(subscribe.topicNames) == 0);
382,634✔
690
  if(unSubscribe){
382,634✔
691
    SMqConsumerObj *pConsumerTmp = NULL;
215,288✔
692
    MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, subscribe.consumerId, &pConsumerTmp));
215,288✔
693
    taosRLockLatch(&pConsumerTmp->lock);
212,565✔
694
    size_t topicNum = taosArrayGetSize(pConsumerTmp->assignedTopics);
212,565✔
695
    taosRUnLockLatch(&pConsumerTmp->lock);
212,565✔
696
    mndReleaseConsumer(pMnode, pConsumerTmp);
212,565✔
697
    if (topicNum == 0){
212,565✔
698
      goto END;
93,747✔
699
    }
700
  }
701
  MND_TMQ_RETURN_CHECK(checkAndSortTopic(pMnode, subscribe.topicNames));
286,164✔
702
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY,
285,806✔
703
                          (unSubscribe ? TRN_CONFLICT_NOTHING :TRN_CONFLICT_DB_INSIDE),
704
                          pMsg, "subscribe");
705
  MND_TMQ_NULL_CHECK(pTrans);
285,806✔
706

707
  MND_TMQ_RETURN_CHECK(validateTopics(pTrans, &subscribe, pMnode, RPC_MSG_USER(pMsg), RPC_MSG_TOKEN(pMsg)));
285,806✔
708
  MND_TMQ_RETURN_CHECK(buildSubConsumer(pMnode, &subscribe, &pConsumerNew));
283,294✔
709
  MND_TMQ_RETURN_CHECK(mndSetConsumerCommitLogs(pTrans, pConsumerNew));
279,310✔
710
  MND_TMQ_RETURN_CHECK(mndTransPrepare(pMnode, pTrans));
279,310✔
711
  code = TSDB_CODE_ACTION_IN_PROGRESS;
279,310✔
712

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

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

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

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

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

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

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

755
  terrno = TSDB_CODE_SUCCESS;
813,509✔
756

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

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

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

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

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

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

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

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

817
  tmsgUpdateDnodeEpSet(&pConsumer->ep);
705,012✔
818

819
CM_DECODE_OVER:
705,012✔
820
  taosMemoryFreeClear(buf);
705,012✔
821
  if (terrno != TSDB_CODE_SUCCESS) {
705,012✔
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;
705,012✔
828
}
829

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

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

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

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

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

883
  return existing;
164,576✔
884
}
885

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

898
  if (pNewConsumer->updateType == CONSUMER_UPDATE_SUB) {
421,171✔
899
    TSWAP(pOldConsumer->rebNewTopics, pNewConsumer->rebNewTopics);
120,949✔
900
    TSWAP(pOldConsumer->rebRemovedTopics, pNewConsumer->rebRemovedTopics);
120,949✔
901
    TSWAP(pOldConsumer->assignedTopics, pNewConsumer->assignedTopics);
120,949✔
902

903
    pOldConsumer->subscribeTime = taosGetTimestampMs();
120,949✔
904
    pOldConsumer->status = MQ_CONSUMER_STATUS_REBALANCE;
120,949✔
905
    mInfo("consumer:0x%" PRIx64 " subscribe update, modify existed consumer", pOldConsumer->consumerId);
120,949✔
906
  } else if (pNewConsumer->updateType == CONSUMER_UPDATE_REB) {
300,222✔
907
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
8,999✔
908
    pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
8,999✔
909
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
8,999✔
910
    mInfo("tmq rebalance consumer:0x%" PRIx64 " rebalance update, only rebalance time", pOldConsumer->consumerId);
8,999✔
911
  } else if (pNewConsumer->updateType == CONSUMER_ADD_REB) {
291,223✔
912
    void *tmp = taosArrayGetP(pNewConsumer->rebNewTopics, 0);
164,576✔
913
    MND_TMQ_NULL_CHECK(tmp);
164,576✔
914
    char *pNewTopic = taosStrdup(tmp);
164,576✔
915
    MND_TMQ_NULL_CHECK(pNewTopic);
164,576✔
916
    removeFromTopicList(pOldConsumer->rebNewTopics, pNewTopic, pOldConsumer->consumerId, "new");
164,576✔
917
    bool existing = existInCurrentTopicList(pOldConsumer, pNewTopic);
164,576✔
918
    if (existing) {
164,576✔
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));
329,152✔
922
      pNewTopic = NULL;
164,576✔
923
      taosArraySort(pOldConsumer->currentTopics, taosArrayCompareString);
164,576✔
924
    }
925

926
    int32_t status = pOldConsumer->status;
164,576✔
927
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
164,576✔
928
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
161,880✔
929
    }
930

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

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

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

954
    mInfo("tmq rebalanceconsumer:0x%" PRIx64 " rebalance update remove, state (%d)%s -> (%d)%s, new epoch:%d, reb-time:%" PRId64
126,647✔
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);
421,171✔
964
  taosWUnLockLatch(&pOldConsumer->lock);
421,171✔
965

966
  PRINT_LOG_END
421,171✔
967

968
  return code;
421,171✔
969
}
970

971
int32_t mndAcquireConsumer(SMnode *pMnode, int64_t consumerId, SMqConsumerObj** pConsumer) {
5,752,571✔
972
  if (pMnode == NULL || pConsumer == NULL) {
5,752,571✔
973
    return TSDB_CODE_INVALID_PARA;
×
974
  }
975
  SSdb           *pSdb = pMnode->pSdb;
5,752,571✔
976
  *pConsumer = sdbAcquire(pSdb, SDB_CONSUMER, &consumerId);
5,752,927✔
977
  if (*pConsumer == NULL) {
5,752,591✔
978
    return TSDB_CODE_MND_CONSUMER_NOT_EXIST;
175,781✔
979
  }
980
  return 0;
5,577,146✔
981
}
982

983
void mndReleaseConsumer(SMnode *pMnode, SMqConsumerObj *pConsumer) {
22,081,093✔
984
  if (pMnode == NULL || pConsumer == NULL) {
22,081,093✔
985
    return;
15,072,522✔
986
  }
987
  SSdb *pSdb = pMnode->pSdb;
7,008,571✔
988
  sdbRelease(pSdb, pConsumer);
7,008,571✔
989
}
990

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1128
  for (int32_t i = 0; i < topicSz; i++) {
94,924✔
1129
    char *pTopicFName = taosArrayGetP(pConsumer->assignedTopics, i);
47,462✔
1130
    if (!showAll && (strncmp(pOperUser->name, pConsumer->user, TSDB_USER_LEN) != 0)) {
47,462✔
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));
47,462✔
1151
    (*numOfRows)++;
47,462✔
1152
  }
1153

1154
END:
47,462✔
1155
  taosRUnLockLatch(&pConsumer->lock);
48,066✔
1156
  PRINT_LOG_END
48,066✔
1157
  return code;
48,066✔
1158
}
1159

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

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

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

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

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

1212
const char *mndConsumerStatusName(int status) {
4,104,218✔
1213
  switch (status) {
4,104,218✔
1214
    case MQ_CONSUMER_STATUS_READY:
1,864,979✔
1215
      return "ready";
1,864,979✔
1216
    case MQ_CONSUMER_STATUS_REBALANCE:
2,239,564✔
1217
      return "rebalancing";
2,239,564✔
1218
    default:
×
1219
      return "unknown";
×
1220
  }
1221
}
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