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

taosdata / TDengine / #5024

16 Apr 2026 10:31AM UTC coverage: 72.954% (+0.7%) from 72.251%
#5024

push

travis-ci

web-flow
merge: from main to 3.0 branch #35157

120 of 200 new or added lines in 14 files covered. (60.0%)

655 existing lines in 130 files now uncovered.

273150 of 374416 relevant lines covered (72.95%)

129604715.8 hits per line

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

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

64
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndRetrieveConsumer);
467,454✔
65
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndCancelGetNextConsumer);
467,454✔
66

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

70
void mndCleanupConsumer(SMnode *pMnode) {}
467,392✔
71

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

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

93
END:
122,163✔
94
  PRINT_LOG_END
122,163✔
95
  return code;
122,163✔
96
}
97

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

103
  PRINT_LOG_START
176,638✔
104
  MND_TMQ_RETURN_CHECK(mndAcquireTopic(pMnode, pOneTopic, &pTopic));
176,638✔
105
  taosRLockLatch(&pTopic->lock);
175,086✔
106

107
  MND_TMQ_RETURN_CHECK(mndCheckTopicPrivilege(pMnode, user, token, MND_OPER_SUBSCRIBE, pTopic));
175,086✔
108
  MND_TMQ_RETURN_CHECK(grantCheckExpire(TSDB_GRANT_SUBSCRIPTION));
174,433✔
109

110
  if (subscribe->enableReplay) {
174,433✔
111
    if (pTopic->subType != TOPIC_SUB_TYPE__COLUMN) {
2,086✔
112
      code = TSDB_CODE_TMQ_REPLAY_NOT_SUPPORT;
596✔
113
      goto END;
596✔
114
    } 
115
    if (pTopic->stbName[0] != 0) {
1,490✔
116
      SDbObj *pDb = mndAcquireDb(pMnode, pTopic->db);
1,192✔
117
      if (pDb == NULL) {
1,192✔
118
        code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
119
        goto END;
×
120
      }
121
      if (pDb->cfg.numOfVgroups != 1) {
1,192✔
122
        mndReleaseDb(pMnode, pDb);
298✔
123
        code = TSDB_CODE_TMQ_REPLAY_NEED_ONE_VGROUP;
298✔
124
        goto END;
298✔
125
      }
126
      mndReleaseDb(pMnode, pDb);
894✔
127
    }
128
  }
129
  char  key[TSDB_CONSUMER_ID_LEN] = {0};
173,539✔
130
  (void)snprintf(key, TSDB_CONSUMER_ID_LEN, "%"PRIx64, subscribe->consumerId);
173,539✔
131
  mndTransSetDbName(pTrans, pTopic->db, key);
173,539✔
132
  MND_TMQ_RETURN_CHECK(mndTransCheckConflict(pMnode, pTrans));
173,539✔
133

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

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

150
  PRINT_LOG_START
297,005✔
151
  int32_t numOfTopics = taosArrayGetSize(subscribe->topicNames);
297,005✔
152
  for (int32_t i = 0; i < numOfTopics; i++) {
470,544✔
153
    char *pOneTopic = taosArrayGetP(subscribe->topicNames, i);
176,638✔
154
    MND_TMQ_RETURN_CHECK(validateOneTopic(pTrans, pOneTopic, subscribe, pMnode, user, token));
176,638✔
155
  }
156

157
END:
293,906✔
158
  PRINT_LOG_END
297,005✔
159
  return code;
297,005✔
160
}
161

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

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

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

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

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

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

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

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

230
  rsp->topicPrivileges = taosArrayInit(taosArrayGetSize(pConsumer->currentTopics), sizeof(STopicPrivilege));
3,106,527✔
231
  MND_TMQ_NULL_CHECK(rsp->topicPrivileges);
3,106,527✔
232
  for (int32_t i = 0; i < taosArrayGetSize(pConsumer->currentTopics); i++) {
4,972,873✔
233
    char        *topic = taosArrayGetP(pConsumer->currentTopics, i);
1,866,346✔
234
    checkOnePrivilege(topic, pMnode, rsp, user, token);
1,866,346✔
235
  }
236

237
END:
3,106,527✔
238
  PRINT_LOG_END
3,106,527✔
239
  return code;
3,106,527✔
240
}
241

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

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

270
    mndReleaseSubscribe(pMnode, pSub);
1,525,268✔
271
  }
272
}
273

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

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

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

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

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

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

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

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

357
  tstrncpy(topicEp.db, pSub->dbName, TSDB_DB_FNAME_LEN);
10,192,765✔
358
  for (int32_t j = 0; j < vgNum; j++) {
11,282,315✔
359
    int32_t *vgId = taosArrayGet(pConsumerEp->vgs, j);
10,482,187✔
360
    if (vgId == NULL) {
10,482,187✔
361
      continue;
×
362
    }
363
    SMqSubVgEp vgEp = {.epSet = {0}, .vgId = *vgId, .offset = -1};
10,482,187✔
364
    SVgObj *pVgroup = mndAcquireVgroup(pMnode, *vgId);
10,482,187✔
365
    if (pVgroup == NULL) {
10,482,187✔
366
      mWarn("failed to acquire vgroup:%d", *vgId);
9,392,637✔
367
      code = terrno;
9,392,637✔
368
      goto END;
9,392,637✔
369
    }
370
    vgEp.epSet = mndGetVgroupEpset(pMnode, pVgroup);
1,089,550✔
371
    mndReleaseVgroup(pMnode, pVgroup);
1,089,550✔
372
    MND_TMQ_NULL_CHECK(taosArrayPush(topicEp.vgs, &vgEp));
2,179,100✔
373
  }
374
  MND_TMQ_NULL_CHECK(taosArrayPush(rsp->topics, &topicEp));
1,600,256✔
375
  topicEp.vgs = NULL;
800,128✔
376

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

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

395
  int32_t numOfTopics = taosArrayGetSize(pConsumer->currentTopics);
10,399,302✔
396
  rsp->topics = taosArrayInit(numOfTopics, sizeof(SMqSubTopicEp));
10,399,302✔
397
  MND_TMQ_NULL_CHECK(rsp->topics);
10,398,990✔
398

399
  // handle all topics subscribed by this consumer
400
  for (int32_t i = 0; i < numOfTopics; i++) {
11,199,118✔
401
    char            *topic = taosArrayGetP(pConsumer->currentTopics, i);
10,192,453✔
402
    MND_TMQ_RETURN_CHECK(processEachTopicEp(pMnode, pConsumer, topic, rsp));
10,192,453✔
403
  }
404

405
END:
1,006,665✔
406
  PRINT_LOG_END
10,399,302✔
407
  return code;
10,399,302✔
408
}
409

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

422
  SMqRspHead *pHead = buf;
11,975,606✔
423

424
  pHead->mqMsgType = TMQ_MSG_TYPE__EP_RSP;
11,975,606✔
425
  pHead->epoch = serverEpoch;
11,975,606✔
426
  pHead->consumerId = consumerId;
11,975,606✔
427
  pHead->walsver = 0;
11,975,963✔
428
  pHead->walever = 0;
11,976,677✔
429

430
  void *abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
11,976,320✔
431
  if (tEncodeSMqAskEpRsp(&abuf, rsp) < 0) {
11,977,034✔
432
    rpcFreeCont(buf);
×
UNCOV
433
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
434
  }
435

436
  // send rsp
437
  pMsg->info.rsp = buf;
11,977,034✔
438
  pMsg->info.rspLen = tlen;
11,976,320✔
439
  return code;
11,975,963✔
440
}
441

442
static int32_t mndProcessAskEpReq(SRpcMsg *pMsg) {
21,374,495✔
443
  if (pMsg == NULL) {
21,374,495✔
UNCOV
444
    return TSDB_CODE_INVALID_PARA;
×
445
  }
446
  SMnode     *pMnode = pMsg->info.node;
21,374,495✔
447
  SMqAskEpReq req = {0};
21,375,566✔
448
  SMqAskEpRsp rsp = {0};
21,375,923✔
449
  int32_t     code = 0;
21,372,762✔
450
  int32_t     lino = 0;
21,372,762✔
451
  SMqConsumerObj *pConsumer = NULL;
21,372,762✔
452
  PRINT_LOG_START
21,372,762✔
453

454
  MND_TMQ_RETURN_CHECK(tDeserializeSMqAskEpReq(pMsg->pCont, pMsg->contLen, &req));
21,375,228✔
455
  int64_t consumerId = req.consumerId;
21,375,209✔
456
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
21,375,209✔
457
  taosRLockLatch(&pConsumer->lock);
21,369,671✔
458
  if (strncmp(req.cgroup, pConsumer->cgroup, tListLen(pConsumer->cgroup)) != 0) {
21,370,028✔
UNCOV
459
    mError("consumer:0x%" PRIx64 " group:%s not consistent with data in sdb, saved cgroup:%s", consumerId, req.cgroup,
×
460
           pConsumer->cgroup);
461
    code = TSDB_CODE_MND_CONSUMER_NOT_EXIST;
×
UNCOV
462
    goto END;
×
463
  }
464

465
  // 1. check consumer status
466
  int32_t serverEpoch = atomic_load_32(&pConsumer->epoch);
21,367,172✔
467
  int32_t status = atomic_load_32(&pConsumer->status);
21,366,458✔
468
  if (status != MQ_CONSUMER_STATUS_READY) {
21,366,815✔
469
    mInfo("consumer:0x%" PRIx64 " not ready, status: %s", consumerId, mndConsumerStatusName(status));
7,190,175✔
470
    rsp.code = TSDB_CODE_MND_CONSUMER_NOT_READY;
7,193,031✔
471
  } else {
472
    int32_t epoch = req.epoch;
14,176,640✔
473

474
    // 2. check epoch, only send ep info when epochs do not match
475
    if (epoch != serverEpoch) {
14,176,640✔
476
      mInfo("process ask ep, consumer:0x%" PRIx64 "(epoch %d) update with server epoch %d",
10,399,302✔
477
            consumerId, epoch, serverEpoch);
478
      MND_TMQ_RETURN_CHECK(addEpSetInfo(pMnode, pConsumer, &rsp));
10,399,302✔
479
    }
480
  }
481
  code = buildAskEpRsp(pMsg, &rsp, serverEpoch, consumerId);
11,977,034✔
482

483
END:
21,373,067✔
484
  if (pConsumer != NULL) {
21,373,424✔
485
    taosRUnLockLatch(&pConsumer->lock);
21,367,886✔
486
  }
487
  tDeleteSMqAskEpRsp(&rsp);
488
  mndReleaseConsumer(pMnode, pConsumer);
21,375,209✔
489
  PRINT_LOG_END
21,375,923✔
490
  return code;
21,375,923✔
491
}
492

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

509
END:
120,177✔
510
  PRINT_LOG_END
120,177✔
511
  return code;
120,177✔
512
}
513

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

531
  return code;
593,900✔
532
}
533

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

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

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

562
  pConsumerNew->rebNewTopics = taosArrayInit(0, sizeof(void *));
129,654✔
563
  MND_TMQ_NULL_CHECK(pConsumerNew->rebNewTopics);
129,654✔
564
  pConsumerNew->rebRemovedTopics = taosArrayInit(0, sizeof(void *));
129,654✔
565
  MND_TMQ_NULL_CHECK(pConsumerNew->rebRemovedTopics);
129,654✔
566

567
  int32_t newTopicNum = taosArrayGetSize(pConsumerNew->assignedTopics);
129,654✔
568
  int32_t oldTopicNum = taosArrayGetSize(pExistedConsumer->currentTopics);
129,654✔
569
  int32_t i = 0, j = 0;
129,654✔
570
  while (i < oldTopicNum || j < newTopicNum) {
262,076✔
571
    if (i >= oldTopicNum) {
132,422✔
572
      void* tmp = taosArrayGetP(pConsumerNew->assignedTopics, j);
671✔
573
      MND_TMQ_NULL_CHECK(tmp);
671✔
574
      ADD_TOPIC_TO_ARRAY(tmp, rebNewTopics);
1,342✔
575
      j++;
671✔
576
      continue;
671✔
577
    } else if (j >= newTopicNum) {
131,751✔
578
      void* tmp = taosArrayGetP(pExistedConsumer->currentTopics, i);
125,903✔
579
      MND_TMQ_NULL_CHECK(tmp);
125,903✔
580
      ADD_TOPIC_TO_ARRAY(tmp, rebRemovedTopics);
251,806✔
581
      i++;
125,903✔
582
      continue;
125,903✔
583
    } else {
584
      char *oldTopic = taosArrayGetP(pExistedConsumer->currentTopics, i);
5,848✔
585
      MND_TMQ_NULL_CHECK(oldTopic);
5,848✔
586
      char *newTopic = taosArrayGetP(pConsumerNew->assignedTopics, j);
5,848✔
587
      MND_TMQ_NULL_CHECK(newTopic);
5,848✔
588
      int   comp = strcmp(oldTopic, newTopic);
5,848✔
589
      if (comp == 0) {
5,848✔
590
        i++;
5,848✔
591
        j++;
5,848✔
592
        continue;
5,848✔
593
      } else if (comp < 0) {
×
594
        ADD_TOPIC_TO_ARRAY(oldTopic, rebRemovedTopics);
×
595
        i++;
×
596
        continue;
×
597
      } else {
×
598
        ADD_TOPIC_TO_ARRAY(newTopic, rebNewTopics);
×
599
        j++;
×
UNCOV
600
        continue;
×
601
      }
602
    }
603
  }
604
  // no topics need to be rebalanced
605
  if (taosArrayGetSize(pConsumerNew->rebNewTopics) == 0 && taosArrayGetSize(pConsumerNew->rebRemovedTopics) == 0) {
129,654✔
606
    code = TSDB_CODE_TMQ_NO_NEED_REBALANCE;
5,848✔
607
  }
608

609
END:
123,806✔
610
  taosRUnLockLatch(&pExistedConsumer->lock);
129,654✔
611
  PRINT_LOG_END
129,654✔
612
  return code;
129,654✔
613
}
614

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

622
  int32_t newTopicNum = taosArrayGetSize(pTopicList);
297,362✔
623
  for (int i = 0; i < newTopicNum; i++) {
474,000✔
624
    int32_t gNum = mndGetGroupNumByTopic(pMnode, (const char *)taosArrayGetP(pTopicList, i));
176,995✔
625
    if (gNum >= MND_MAX_GROUP_PER_TOPIC) {
176,995✔
626
      return TSDB_CODE_TMQ_GROUP_OUT_OF_RANGE;
357✔
627
    }
628
  }
629
  return 0;
297,005✔
630
}
631

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

648
    MND_TMQ_RETURN_CHECK(tNewSMqConsumerObj(consumerId, cgroup, CONSUMER_INSERT_SUB, NULL, subscribe, &pConsumerNew));
164,252✔
649
  } else {
650
    int32_t status = atomic_load_32(&pExistedConsumer->status);
129,654✔
651

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

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

669
END:
293,576✔
670
  PRINT_LOG_END
293,906✔
671
  mndReleaseConsumer(pMnode, pExistedConsumer);
293,906✔
672
  tDeleteSMqConsumerObj(pConsumerNew);
293,906✔
673
  return code;
293,906✔
674
}
675

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

687
  PRINT_LOG_START
390,076✔
688
  SCMSubscribeReq subscribe = {0};
390,076✔
689
  MND_TMQ_RETURN_CHECK(tDeserializeSCMSubscribeReq(msgStr, &subscribe, pMsg->contLen));
780,152✔
690
  bool unSubscribe = (taosArrayGetSize(subscribe.topicNames) == 0);
390,076✔
691
  if(unSubscribe){
390,076✔
692
    SMqConsumerObj *pConsumerTmp = NULL;
215,849✔
693
    MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, subscribe.consumerId, &pConsumerTmp));
215,849✔
694
    taosRLockLatch(&pConsumerTmp->lock);
212,277✔
695
    size_t topicNum = taosArrayGetSize(pConsumerTmp->assignedTopics);
212,277✔
696
    taosRUnLockLatch(&pConsumerTmp->lock);
212,277✔
697
    mndReleaseConsumer(pMnode, pConsumerTmp);
212,277✔
698
    if (topicNum == 0){
212,277✔
699
      goto END;
89,142✔
700
    }
701
  }
702
  MND_TMQ_RETURN_CHECK(checkAndSortTopic(pMnode, subscribe.topicNames));
297,362✔
703
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY,
297,005✔
704
                          (unSubscribe ? TRN_CONFLICT_NOTHING :TRN_CONFLICT_DB_INSIDE),
705
                          pMsg, "subscribe");
706
  MND_TMQ_NULL_CHECK(pTrans);
297,005✔
707

708
  MND_TMQ_RETURN_CHECK(validateTopics(pTrans, &subscribe, pMnode, RPC_MSG_USER(pMsg), RPC_MSG_TOKEN(pMsg)));
297,005✔
709
  MND_TMQ_RETURN_CHECK(buildSubConsumer(pMnode, &subscribe, &pConsumerNew));
293,906✔
710
  MND_TMQ_RETURN_CHECK(mndSetConsumerCommitLogs(pTrans, pConsumerNew));
288,058✔
711
  MND_TMQ_RETURN_CHECK(mndTransPrepare(pMnode, pTrans));
288,058✔
712
  code = TSDB_CODE_ACTION_IN_PROGRESS;
288,058✔
713

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

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

735
  void   *buf = NULL;
836,315✔
736
  int32_t tlen = tEncodeSMqConsumerObj(NULL, pConsumer);
836,315✔
737
  int32_t size = sizeof(int32_t) + tlen + MND_CONSUMER_RESERVE_SIZE;
836,315✔
738

739
  SSdbRaw *pRaw = sdbAllocRaw(SDB_CONSUMER, MND_CONSUMER_VER_NUMBER, size);
836,315✔
740
  if (pRaw == NULL) goto CM_ENCODE_OVER;
836,315✔
741

742
  buf = taosMemoryMalloc(tlen);
836,315✔
743
  if (buf == NULL) goto CM_ENCODE_OVER;
836,315✔
744

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

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

756
  terrno = TSDB_CODE_SUCCESS;
836,315✔
757

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

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

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

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

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

791
  pRow = sdbAllocRow(sizeof(SMqConsumerObj));
726,538✔
792
  if (pRow == NULL) {
726,538✔
UNCOV
793
    goto CM_DECODE_OVER;
×
794
  }
795

796
  pConsumer = sdbGetRowObj(pRow);
726,538✔
797
  if (pConsumer == NULL) {
726,538✔
UNCOV
798
    goto CM_DECODE_OVER;
×
799
  }
800

801
  int32_t dataPos = 0;
726,538✔
802
  int32_t len;
725,768✔
803
  SDB_GET_INT32(pRaw, dataPos, &len, CM_DECODE_OVER);
726,538✔
804
  buf = taosMemoryMalloc(len);
726,538✔
805
  if (buf == NULL) {
726,538✔
806
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
807
    goto CM_DECODE_OVER;
×
808
  }
809

810
  SDB_GET_BINARY(pRaw, dataPos, buf, len, CM_DECODE_OVER);
726,538✔
811
  SDB_GET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_DECODE_OVER);
726,538✔
812

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

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

826
  return pRow;
726,538✔
827
}
828

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

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

849
// remove from topic list
850
static void removeFromTopicList(SArray *topicList, const char *pTopic, int64_t consumerId, char *type) {
432,391✔
851
  if (topicList == NULL || pTopic == NULL) {
432,391✔
UNCOV
852
    return;
×
853
  }
854
  int32_t size = taosArrayGetSize(topicList);
432,391✔
855
  for (int32_t i = 0; i < size; i++) {
436,708✔
856
    char *p = taosArrayGetP(topicList, i);
432,170✔
857
    if (strcmp(pTopic, p) == 0) {
432,170✔
858
      taosArrayRemove(topicList, i);
427,853✔
859
      taosMemoryFree(p);
427,853✔
860

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

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

882
  return existing;
169,147✔
883
}
884

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

897
  if (pNewConsumer->updateType == CONSUMER_UPDATE_SUB) {
433,816✔
898
    TSWAP(pOldConsumer->rebNewTopics, pNewConsumer->rebNewTopics);
124,990✔
899
    TSWAP(pOldConsumer->rebRemovedTopics, pNewConsumer->rebRemovedTopics);
124,990✔
900
    TSWAP(pOldConsumer->assignedTopics, pNewConsumer->assignedTopics);
124,990✔
901

902
    pOldConsumer->subscribeTime = taosGetTimestampMs();
124,990✔
903
    pOldConsumer->status = MQ_CONSUMER_STATUS_REBALANCE;
124,990✔
904
    mInfo("consumer:0x%" PRIx64 " subscribe update, modify existed consumer", pOldConsumer->consumerId);
124,990✔
905
  } else if (pNewConsumer->updateType == CONSUMER_UPDATE_REB) {
308,826✔
906
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
8,057✔
907
    pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
8,057✔
908
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
8,057✔
909
    mInfo("tmq rebalance consumer:0x%" PRIx64 " rebalance update, only rebalance time", pOldConsumer->consumerId);
8,057✔
910
  } else if (pNewConsumer->updateType == CONSUMER_ADD_REB) {
300,769✔
911
    void *tmp = taosArrayGetP(pNewConsumer->rebNewTopics, 0);
169,147✔
912
    MND_TMQ_NULL_CHECK(tmp);
169,147✔
913
    char *pNewTopic = taosStrdup(tmp);
169,147✔
914
    MND_TMQ_NULL_CHECK(pNewTopic);
169,147✔
915
    removeFromTopicList(pOldConsumer->rebNewTopics, pNewTopic, pOldConsumer->consumerId, "new");
169,147✔
916
    bool existing = existInCurrentTopicList(pOldConsumer, pNewTopic);
169,147✔
917
    if (existing) {
169,147✔
UNCOV
918
      mError("tmq rebalance consumer:0x%" PRIx64 " add new topic:%s should not in currentTopics", pOldConsumer->consumerId, pNewTopic);
×
919
    } else {
920
      MND_TMQ_NULL_CHECK(taosArrayPush(pOldConsumer->currentTopics, &pNewTopic));
338,294✔
921
      pNewTopic = NULL;
169,147✔
922
      taosArraySort(pOldConsumer->currentTopics, taosArrayCompareString);
169,147✔
923
    }
924

925
    int32_t status = pOldConsumer->status;
169,147✔
926
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
169,147✔
927
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
166,379✔
928
    }
929

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

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

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

946
    int32_t status = pOldConsumer->status;
131,622✔
947
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
131,622✔
948
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
128,854✔
949
    }
950
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
131,622✔
951
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
131,622✔
952

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

UNCOV
961
END:
×
962
  taosMemoryFree(pNewTopic);
433,816✔
963
  taosWUnLockLatch(&pOldConsumer->lock);
433,816✔
964

965
  PRINT_LOG_END
433,816✔
966

967
  return code;
433,816✔
968
}
969

970
int32_t mndAcquireConsumer(SMnode *pMnode, int64_t consumerId, SMqConsumerObj** pConsumer) {
25,123,490✔
971
  if (pMnode == NULL || pConsumer == NULL) {
25,123,490✔
UNCOV
972
    return TSDB_CODE_INVALID_PARA;
×
973
  }
974
  SSdb           *pSdb = pMnode->pSdb;
25,123,847✔
975
  *pConsumer = sdbAcquire(pSdb, SDB_CONSUMER, &consumerId);
25,123,490✔
976
  if (*pConsumer == NULL) {
25,123,490✔
977
    return TSDB_CODE_MND_CONSUMER_NOT_EXIST;
185,901✔
978
  }
979
  return 0;
24,937,232✔
980
}
981

982
void mndReleaseConsumer(SMnode *pMnode, SMqConsumerObj *pConsumer) {
43,123,878✔
983
  if (pMnode == NULL || pConsumer == NULL) {
43,123,878✔
984
    return;
16,372,709✔
985
  }
986
  SSdb *pSdb = pMnode->pSdb;
26,751,169✔
987
  sdbRelease(pSdb, pConsumer);
26,751,169✔
988
}
989

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1096
END:
975,927✔
1097
  PRINT_LOG_END
975,927✔
1098
  taosMemoryFreeClear(status);
975,927✔
1099
  taosMemoryFreeClear(parasStr);
975,927✔
1100
  return code;
975,927✔
1101
}
1102

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

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

1123
  if (*numOfRows + topicSz > rowsCapacity) {
976,063✔
UNCOV
1124
    MND_TMQ_RETURN_CHECK(blockDataEnsureCapacity(pBlock, *numOfRows + topicSz));
×
1125
  }
1126

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

1153
END:
976,063✔
1154
  taosRUnLockLatch(&pConsumer->lock);
976,853✔
1155
  PRINT_LOG_END
976,853✔
1156
  return code;
976,853✔
1157
}
1158

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

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

1190
  pShow->numOfRows += numOfRows;
33,748✔
1191

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

UNCOV
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) {
11,732,745✔
1212
  switch (status) {
11,732,745✔
1213
    case MQ_CONSUMER_STATUS_READY:
2,263,167✔
1214
      return "ready";
2,263,167✔
1215
    case MQ_CONSUMER_STATUS_REBALANCE:
9,470,649✔
1216
      return "rebalancing";
9,470,649✔
UNCOV
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