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

taosdata / TDengine / #5071

17 May 2026 01:15AM UTC coverage: 63.054% (-10.3%) from 73.326%
#5071

push

travis-ci

web-flow
feat (TDgpt): Dynamic Model Synchronization Enhancements (#35344)

* refactor: do some internal refactor.

* fix: fix multiprocess sync issue.

* feat: add dynamic anomaly detection and forecasting services

* fix: log error message for undeploying model in exception handling

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix: handle undeploy when model exists only on disk

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/286aafa0-c3ce-4c27-b803-2707571e9dc1

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: guard dynamic registry concurrent access

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: tighten service list locking scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: restore prophet support and update tests per review feedback

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: improve test name and move copy inside lock scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* Potential fix for pull request finding

Co-au... (continued)

238317 of 377957 relevant lines covered (63.05%)

130539817.12 hits per line

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

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

64
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndRetrieveConsumer);
404,507✔
65
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndCancelGetNextConsumer);
404,507✔
66

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

70
void mndCleanupConsumer(SMnode *pMnode) {}
404,507✔
71

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

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

93
END:
80,573✔
94
  PRINT_LOG_END
80,573✔
95
  return code;
80,573✔
96
}
97

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

103
  PRINT_LOG_START
137,644✔
104
  MND_TMQ_RETURN_CHECK(mndAcquireTopic(pMnode, pOneTopic, &pTopic));
137,644✔
105
  taosRLockLatch(&pTopic->lock);
136,852✔
106

107
  MND_TMQ_RETURN_CHECK(mndCheckTopicPrivilege(pMnode, user, token, MND_OPER_SUBSCRIBE, pTopic));
136,852✔
108
  MND_TMQ_RETURN_CHECK(grantCheckExpire(TSDB_GRANT_SUBSCRIPTION));
136,852✔
109

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

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

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

150
  PRINT_LOG_START
219,727✔
151
  int32_t numOfTopics = taosArrayGetSize(subscribe->topicNames);
219,727✔
152
  for (int32_t i = 0; i < numOfTopics; i++) {
356,579✔
153
    char *pOneTopic = taosArrayGetP(subscribe->topicNames, i);
137,644✔
154
    MND_TMQ_RETURN_CHECK(validateOneTopic(pTrans, pOneTopic, subscribe, pMnode, user, token));
137,644✔
155
  }
156

157
END:
218,935✔
158
  PRINT_LOG_END
219,727✔
159
  return code;
219,727✔
160
}
161

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

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

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

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

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

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

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

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

230
  rsp->topicPrivileges = taosArrayInit(taosArrayGetSize(pConsumer->currentTopics), sizeof(STopicPrivilege));
587,551✔
231
  MND_TMQ_NULL_CHECK(rsp->topicPrivileges);
587,551✔
232
  for (int32_t i = 0; i < taosArrayGetSize(pConsumer->currentTopics); i++) {
1,127,843✔
233
    char        *topic = taosArrayGetP(pConsumer->currentTopics, i);
540,292✔
234
    checkOnePrivilege(topic, pMnode, rsp, user, token);
540,292✔
235
  }
236

237
END:
587,551✔
238
  PRINT_LOG_END
587,551✔
239
  return code;
587,551✔
240
}
241

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

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

270
    mndReleaseSubscribe(pMnode, pSub);
546,687✔
271
  }
272
}
273

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

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

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

308
  MND_TMQ_RETURN_CHECK(tDeserializeSMqHbReq(pMsg->pCont, pMsg->contLen, &req));
592,866✔
309
  int64_t consumerId = req.consumerId;
592,866✔
310
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
592,866✔
311
  taosWLockLatch(&pConsumer->lock);
587,551✔
312
  MND_TMQ_RETURN_CHECK(checkPrivilege(pMnode, pConsumer, &rsp, RPC_MSG_USER(pMsg), RPC_MSG_TOKEN(pMsg)));
587,551✔
313
  atomic_store_32(&pConsumer->hbStatus, 0);
587,551✔
314
  mDebug("consumer:0x%" PRIx64 " receive hb pollFlag:%d pollStatus:%d", consumerId, req.pollFlag, pConsumer->pollStatus);
587,551✔
315
  if (req.pollFlag == 1){
587,551✔
316
    atomic_store_32(&pConsumer->pollStatus, 0);
239,599✔
317
    pConsumer->pollTime = taosGetTimestampMs();
479,198✔
318
  }
319

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

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

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

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

357
  tstrncpy(topicEp.db, pSub->dbName, TSDB_DB_FNAME_LEN);
135,260✔
358
  for (int32_t j = 0; j < vgNum; j++) {
508,653✔
359
    int32_t *vgId = taosArrayGet(pConsumerEp->vgs, j);
373,393✔
360
    if (vgId == NULL) {
373,393✔
361
      continue;
×
362
    }
363
    SMqSubVgEp vgEp = {.epSet = {0}, .vgId = *vgId, .offset = -1};
373,393✔
364
    SVgObj *pVgroup = mndAcquireVgroup(pMnode, *vgId);
373,393✔
365
    if (pVgroup == NULL) {
373,393✔
366
      mWarn("failed to acquire vgroup:%d", *vgId);
×
367
      code = terrno;
×
368
      goto END;
×
369
    }
370
    vgEp.epSet = mndGetVgroupEpset(pMnode, pVgroup);
373,393✔
371
    mndReleaseVgroup(pMnode, pVgroup);
373,393✔
372
    MND_TMQ_NULL_CHECK(taosArrayPush(topicEp.vgs, &vgEp));
746,786✔
373
  }
374
  MND_TMQ_NULL_CHECK(taosArrayPush(rsp->topics, &topicEp));
270,520✔
375
  topicEp.vgs = NULL;
135,260✔
376

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

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

395
  int32_t numOfTopics = taosArrayGetSize(pConsumer->currentTopics);
286,104✔
396
  rsp->topics = taosArrayInit(numOfTopics, sizeof(SMqSubTopicEp));
286,104✔
397
  MND_TMQ_NULL_CHECK(rsp->topics);
286,104✔
398

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

405
END:
286,104✔
406
  PRINT_LOG_END
286,104✔
407
  return code;
286,104✔
408
}
409

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

422
  SMqRspHead *pHead = buf;
2,044,559✔
423

424
  pHead->mqMsgType = TMQ_MSG_TYPE__EP_RSP;
2,044,559✔
425
  pHead->epoch = serverEpoch;
2,044,559✔
426
  pHead->consumerId = consumerId;
2,045,374✔
427
  pHead->walsver = 0;
2,045,374✔
428
  pHead->walever = 0;
2,044,559✔
429

430
  void *abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
2,045,374✔
431
  if (tEncodeSMqAskEpRsp(&abuf, rsp) < 0) {
2,044,559✔
432
    rpcFreeCont(buf);
×
433
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
434
  }
435

436
  // send rsp
437
  pMsg->info.rsp = buf;
2,044,559✔
438
  pMsg->info.rspLen = tlen;
2,045,374✔
439
  return code;
2,044,971✔
440
}
441

442
static int32_t mndProcessAskEpReq(SRpcMsg *pMsg) {
2,048,218✔
443
  if (pMsg == NULL) {
2,048,218✔
444
    return TSDB_CODE_INVALID_PARA;
×
445
  }
446
  SMnode     *pMnode = pMsg->info.node;
2,048,218✔
447
  SMqAskEpReq req = {0};
2,048,218✔
448
  SMqAskEpRsp rsp = {0};
2,048,218✔
449
  int32_t     code = 0;
2,047,403✔
450
  int32_t     lino = 0;
2,047,403✔
451
  SMqConsumerObj *pConsumer = NULL;
2,047,403✔
452
  PRINT_LOG_START
2,047,806✔
453

454
  MND_TMQ_RETURN_CHECK(tDeserializeSMqAskEpReq(pMsg->pCont, pMsg->contLen, &req));
2,048,621✔
455
  int64_t consumerId = req.consumerId;
2,048,621✔
456
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
2,048,621✔
457
  taosRLockLatch(&pConsumer->lock);
2,045,374✔
458
  if (strncmp(req.cgroup, pConsumer->cgroup, tListLen(pConsumer->cgroup)) != 0) {
2,045,374✔
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;
×
462
    goto END;
×
463
  }
464

465
  // 1. check consumer status
466
  int32_t serverEpoch = atomic_load_32(&pConsumer->epoch);
2,045,374✔
467
  int32_t status = atomic_load_32(&pConsumer->status);
2,045,374✔
468
  if (status != MQ_CONSUMER_STATUS_READY) {
2,045,374✔
469
    mInfo("consumer:0x%" PRIx64 " not ready, status: %s", consumerId, mndConsumerStatusName(status));
856,199✔
470
    rsp.code = TSDB_CODE_MND_CONSUMER_NOT_READY;
856,199✔
471
  } else {
472
    int32_t epoch = req.epoch;
1,189,175✔
473

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

483
END:
2,048,218✔
484
  if (pConsumer != NULL) {
2,048,218✔
485
    taosRUnLockLatch(&pConsumer->lock);
2,044,971✔
486
  }
487
  tDeleteSMqAskEpRsp(&rsp);
488
  mndReleaseConsumer(pMnode, pConsumer);
2,048,209✔
489
  PRINT_LOG_END
2,048,621✔
490
  return code;
2,048,621✔
491
}
492

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

509
END:
79,466✔
510
  PRINT_LOG_END
79,466✔
511
  return code;
79,466✔
512
}
513

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

531
  return code;
438,430✔
532
}
533

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

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

567
  int32_t newTopicNum = taosArrayGetSize(pConsumerNew->assignedTopics);
92,426✔
568
  int32_t oldTopicNum = taosArrayGetSize(pExistedConsumer->currentTopics);
92,426✔
569
  int32_t i = 0, j = 0;
92,426✔
570
  while (i < oldTopicNum || j < newTopicNum) {
187,287✔
571
    if (i >= oldTopicNum) {
94,861✔
572
      void* tmp = taosArrayGetP(pConsumerNew->assignedTopics, j);
2,760✔
573
      MND_TMQ_NULL_CHECK(tmp);
2,760✔
574
      ADD_TOPIC_TO_ARRAY(tmp, rebNewTopics);
5,520✔
575
      j++;
2,760✔
576
      continue;
2,760✔
577
    } else if (j >= newTopicNum) {
92,101✔
578
      void* tmp = taosArrayGetP(pExistedConsumer->currentTopics, i);
86,953✔
579
      MND_TMQ_NULL_CHECK(tmp);
86,953✔
580
      ADD_TOPIC_TO_ARRAY(tmp, rebRemovedTopics);
173,906✔
581
      i++;
86,953✔
582
      continue;
86,953✔
583
    } else {
584
      char *oldTopic = taosArrayGetP(pExistedConsumer->currentTopics, i);
5,148✔
585
      MND_TMQ_NULL_CHECK(oldTopic);
5,148✔
586
      char *newTopic = taosArrayGetP(pConsumerNew->assignedTopics, j);
5,148✔
587
      MND_TMQ_NULL_CHECK(newTopic);
5,148✔
588
      int   comp = strcmp(oldTopic, newTopic);
5,148✔
589
      if (comp == 0) {
5,148✔
590
        i++;
5,148✔
591
        j++;
5,148✔
592
        continue;
5,148✔
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++;
×
600
        continue;
×
601
      }
602
    }
603
  }
604
  // no topics need to be rebalanced
605
  if (taosArrayGetSize(pConsumerNew->rebNewTopics) == 0 && taosArrayGetSize(pConsumerNew->rebRemovedTopics) == 0) {
92,426✔
606
    code = TSDB_CODE_TMQ_NO_NEED_REBALANCE;
5,148✔
607
  }
608

609
END:
87,278✔
610
  taosRUnLockLatch(&pExistedConsumer->lock);
92,426✔
611
  PRINT_LOG_END
92,426✔
612
  return code;
92,426✔
613
}
614

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

622
  int32_t newTopicNum = taosArrayGetSize(pTopicList);
220,130✔
623
  for (int i = 0; i < newTopicNum; i++) {
357,774✔
624
    int32_t gNum = mndGetGroupNumByTopic(pMnode, (const char *)taosArrayGetP(pTopicList, i));
138,047✔
625
    if (gNum >= MND_MAX_GROUP_PER_TOPIC) {
138,047✔
626
      return TSDB_CODE_TMQ_GROUP_OUT_OF_RANGE;
403✔
627
    }
628
  }
629
  return 0;
219,727✔
630
}
631

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

652
    mInfo("receive tmq subscribe request from existed consumer:0x%" PRIx64
92,426✔
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) {
92,426✔
658
      code = TSDB_CODE_MND_CONSUMER_NOT_READY;
×
659
      goto END;
×
660
    }
661
    MND_TMQ_RETURN_CHECK(tNewSMqConsumerObj(consumerId, cgroup, CONSUMER_UPDATE_SUB, NULL, subscribe, &pConsumerNew));
92,426✔
662
    MND_TMQ_RETURN_CHECK(getTopicAddDelete(pExistedConsumer, pConsumerNew));
92,426✔
663
  }
664
  if (ppConsumer){
213,787✔
665
    *ppConsumer = pConsumerNew;
213,787✔
666
    pConsumerNew = NULL;
213,787✔
667
  }
668

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

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

687
  PRINT_LOG_START
290,158✔
688
  SCMSubscribeReq subscribe = {0};
290,158✔
689
  MND_TMQ_RETURN_CHECK(tDeserializeSCMSubscribeReq(msgStr, &subscribe, pMsg->contLen));
580,316✔
690
  bool unSubscribe = (taosArrayGetSize(subscribe.topicNames) == 0);
290,158✔
691
  if(unSubscribe){
290,158✔
692
    SMqConsumerObj *pConsumerTmp = NULL;
154,546✔
693
    MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, subscribe.consumerId, &pConsumerTmp));
154,546✔
694
    taosRLockLatch(&pConsumerTmp->lock);
152,883✔
695
    size_t topicNum = taosArrayGetSize(pConsumerTmp->assignedTopics);
152,883✔
696
    taosRUnLockLatch(&pConsumerTmp->lock);
152,883✔
697
    mndReleaseConsumer(pMnode, pConsumerTmp);
152,883✔
698
    if (topicNum == 0){
152,883✔
699
      goto END;
68,365✔
700
    }
701
  }
702
  MND_TMQ_RETURN_CHECK(checkAndSortTopic(pMnode, subscribe.topicNames));
220,130✔
703
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY,
219,727✔
704
                          (unSubscribe ? TRN_CONFLICT_NOTHING :TRN_CONFLICT_DB_INSIDE),
705
                          pMsg, "subscribe");
706
  MND_TMQ_NULL_CHECK(pTrans);
219,727✔
707

708
  MND_TMQ_RETURN_CHECK(validateTopics(pTrans, &subscribe, pMnode, RPC_MSG_USER(pMsg), RPC_MSG_TOKEN(pMsg)));
219,727✔
709
  MND_TMQ_RETURN_CHECK(buildSubConsumer(pMnode, &subscribe, &pConsumerNew));
218,935✔
710
  MND_TMQ_RETURN_CHECK(mndSetConsumerCommitLogs(pTrans, pConsumerNew));
213,787✔
711
  MND_TMQ_RETURN_CHECK(mndTransPrepare(pMnode, pTrans));
213,787✔
712
  code = TSDB_CODE_ACTION_IN_PROGRESS;
213,787✔
713

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

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

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

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

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

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

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

756
  terrno = TSDB_CODE_SUCCESS;
644,930✔
757

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

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

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

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

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

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

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

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

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

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

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

826
  return pRow;
520,583✔
827
}
828

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

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

849
// remove from topic list
850
static void removeFromTopicList(SArray *topicList, const char *pTopic, int64_t consumerId, char *type) {
311,646✔
851
  if (topicList == NULL || pTopic == NULL) {
311,646✔
852
    return;
×
853
  }
854
  int32_t size = taosArrayGetSize(topicList);
311,646✔
855
  for (int32_t i = 0; i < size; i++) {
316,506✔
856
    char *p = taosArrayGetP(topicList, i);
313,290✔
857
    if (strcmp(pTopic, p) == 0) {
313,290✔
858
      taosArrayRemove(topicList, i);
308,430✔
859
      taosMemoryFree(p);
308,430✔
860

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

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

882
  return existing;
131,308✔
883
}
884

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

897
  if (pNewConsumer->updateType == CONSUMER_UPDATE_SUB) {
311,921✔
898
    TSWAP(pOldConsumer->rebNewTopics, pNewConsumer->rebNewTopics);
87,278✔
899
    TSWAP(pOldConsumer->rebRemovedTopics, pNewConsumer->rebRemovedTopics);
87,278✔
900
    TSWAP(pOldConsumer->assignedTopics, pNewConsumer->assignedTopics);
87,278✔
901

902
    pOldConsumer->subscribeTime = taosGetTimestampMs();
87,278✔
903
    pOldConsumer->status = MQ_CONSUMER_STATUS_REBALANCE;
87,278✔
904
    mInfo("consumer:0x%" PRIx64 " subscribe update, modify existed consumer", pOldConsumer->consumerId);
87,278✔
905
  } else if (pNewConsumer->updateType == CONSUMER_UPDATE_REB) {
224,643✔
906
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
3,166✔
907
    pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
3,166✔
908
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
3,166✔
909
    mInfo("tmq rebalance consumer:0x%" PRIx64 " rebalance update, only rebalance time", pOldConsumer->consumerId);
3,166✔
910
  } else if (pNewConsumer->updateType == CONSUMER_ADD_REB) {
221,477✔
911
    void *tmp = taosArrayGetP(pNewConsumer->rebNewTopics, 0);
131,308✔
912
    MND_TMQ_NULL_CHECK(tmp);
131,308✔
913
    char *pNewTopic = taosStrdup(tmp);
131,308✔
914
    MND_TMQ_NULL_CHECK(pNewTopic);
131,308✔
915
    removeFromTopicList(pOldConsumer->rebNewTopics, pNewTopic, pOldConsumer->consumerId, "new");
131,308✔
916
    bool existing = existInCurrentTopicList(pOldConsumer, pNewTopic);
131,308✔
917
    if (existing) {
131,308✔
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));
262,616✔
921
      pNewTopic = NULL;
131,308✔
922
      taosArraySort(pOldConsumer->currentTopics, taosArrayCompareString);
131,308✔
923
    }
924

925
    int32_t status = pOldConsumer->status;
131,308✔
926
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
131,308✔
927
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
128,873✔
928
    }
929

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

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

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

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

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

965
  PRINT_LOG_END
311,921✔
966

967
  return code;
311,921✔
968
}
969

970
int32_t mndAcquireConsumer(SMnode *pMnode, int64_t consumerId, SMqConsumerObj** pConsumer) {
3,095,541✔
971
  if (pMnode == NULL || pConsumer == NULL) {
3,095,541✔
972
    return TSDB_CODE_INVALID_PARA;
×
973
  }
974
  SSdb           *pSdb = pMnode->pSdb;
3,095,541✔
975
  *pConsumer = sdbAcquire(pSdb, SDB_CONSUMER, &consumerId);
3,095,541✔
976
  if (*pConsumer == NULL) {
3,095,541✔
977
    return TSDB_CODE_MND_CONSUMER_NOT_EXIST;
139,056✔
978
  }
979
  return 0;
2,956,485✔
980
}
981

982
void mndReleaseConsumer(SMnode *pMnode, SMqConsumerObj *pConsumer) {
14,979,944✔
983
  if (pMnode == NULL || pConsumer == NULL) {
14,979,944✔
984
    return;
11,231,878✔
985
  }
986
  SSdb *pSdb = pMnode->pSdb;
3,748,066✔
987
  sdbRelease(pSdb, pConsumer);
3,748,478✔
988
}
989

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1153
END:
51,049✔
1154
  taosRUnLockLatch(&pConsumer->lock);
51,445✔
1155
  PRINT_LOG_END
51,445✔
1156
  return code;
51,445✔
1157
}
1158

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

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

1190
  pShow->numOfRows += numOfRows;
12,332✔
1191

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

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

1211
const char *mndConsumerStatusName(int status) {
2,967,056✔
1212
  switch (status) {
2,967,056✔
1213
    case MQ_CONSUMER_STATUS_READY:
1,115,728✔
1214
      return "ready";
1,115,728✔
1215
    case MQ_CONSUMER_STATUS_REBALANCE:
1,851,328✔
1216
      return "rebalancing";
1,851,328✔
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