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

taosdata / TDengine / #5064

17 May 2026 01:15AM UTC coverage: 73.37% (-0.02%) from 73.388%
#5064

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)

281592 of 383795 relevant lines covered (73.37%)

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

64
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndRetrieveConsumer);
523,908✔
65
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndCancelGetNextConsumer);
523,908✔
66

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

70
void mndCleanupConsumer(SMnode *pMnode) {}
523,844✔
71

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

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

93
END:
132,793✔
94
  PRINT_LOG_END
132,793✔
95
  return code;
132,793✔
96
}
97

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

103
  PRINT_LOG_START
200,545✔
104
  MND_TMQ_RETURN_CHECK(mndAcquireTopic(pMnode, pOneTopic, &pTopic));
200,545✔
105
  taosRLockLatch(&pTopic->lock);
199,781✔
106

107
  MND_TMQ_RETURN_CHECK(mndCheckTopicPrivilege(pMnode, user, token, MND_OPER_SUBSCRIBE, pTopic));
199,781✔
108
  MND_TMQ_RETURN_CHECK(grantCheckExpire(TSDB_GRANT_SUBSCRIPTION));
199,003✔
109

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

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

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

150
  PRINT_LOG_START
334,605✔
151
  int32_t numOfTopics = taosArrayGetSize(subscribe->topicNames);
334,605✔
152
  for (int32_t i = 0; i < numOfTopics; i++) {
532,594✔
153
    char *pOneTopic = taosArrayGetP(subscribe->topicNames, i);
200,545✔
154
    MND_TMQ_RETURN_CHECK(validateOneTopic(pTrans, pOneTopic, subscribe, pMnode, user, token));
200,545✔
155
  }
156

157
END:
332,049✔
158
  PRINT_LOG_END
334,605✔
159
  return code;
334,605✔
160
}
161

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

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

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

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

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

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

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

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

230
  rsp->topicPrivileges = taosArrayInit(taosArrayGetSize(pConsumer->currentTopics), sizeof(STopicPrivilege));
1,354,016✔
231
  MND_TMQ_NULL_CHECK(rsp->topicPrivileges);
1,354,016✔
232
  for (int32_t i = 0; i < taosArrayGetSize(pConsumer->currentTopics); i++) {
2,624,114✔
233
    char        *topic = taosArrayGetP(pConsumer->currentTopics, i);
1,270,098✔
234
    checkOnePrivilege(topic, pMnode, rsp, user, token);
1,270,098✔
235
  }
236

237
END:
1,354,016✔
238
  PRINT_LOG_END
1,354,016✔
239
  return code;
1,354,016✔
240
}
241

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

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

270
    mndReleaseSubscribe(pMnode, pSub);
1,262,155✔
271
  }
272
}
273

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

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

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

308
  MND_TMQ_RETURN_CHECK(tDeserializeSMqHbReq(pMsg->pCont, pMsg->contLen, &req));
1,359,598✔
309
  int64_t consumerId = req.consumerId;
1,359,598✔
310
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
1,359,598✔
311
  taosWLockLatch(&pConsumer->lock);
1,354,016✔
312
  MND_TMQ_RETURN_CHECK(checkPrivilege(pMnode, pConsumer, &rsp, RPC_MSG_USER(pMsg), RPC_MSG_TOKEN(pMsg)));
1,354,016✔
313
  atomic_store_32(&pConsumer->hbStatus, 0);
1,354,016✔
314
  mDebug("consumer:0x%" PRIx64 " receive hb pollFlag:%d pollStatus:%d", consumerId, req.pollFlag, pConsumer->pollStatus);
1,354,016✔
315
  if (req.pollFlag == 1){
1,354,016✔
316
    atomic_store_32(&pConsumer->pollStatus, 0);
772,338✔
317
    pConsumer->pollTime = taosGetTimestampMs();
1,544,676✔
318
  }
319

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

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

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

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

357
  tstrncpy(topicEp.db, pSub->dbName, TSDB_DB_FNAME_LEN);
15,195,842✔
358
  for (int32_t j = 0; j < vgNum; j++) {
16,346,631✔
359
    int32_t *vgId = taosArrayGet(pConsumerEp->vgs, j);
15,524,683✔
360
    if (vgId == NULL) {
15,524,683✔
361
      continue;
×
362
    }
363
    SMqSubVgEp vgEp = {.epSet = {0}, .vgId = *vgId, .offset = -1};
15,524,683✔
364
    SVgObj *pVgroup = mndAcquireVgroup(pMnode, *vgId);
15,524,683✔
365
    if (pVgroup == NULL) {
15,524,683✔
366
      mWarn("failed to acquire vgroup:%d", *vgId);
14,373,894✔
367
      code = terrno;
14,373,894✔
368
      goto END;
14,373,894✔
369
    }
370
    vgEp.epSet = mndGetVgroupEpset(pMnode, pVgroup);
1,150,789✔
371
    mndReleaseVgroup(pMnode, pVgroup);
1,150,789✔
372
    MND_TMQ_NULL_CHECK(taosArrayPush(topicEp.vgs, &vgEp));
2,301,578✔
373
  }
374
  MND_TMQ_NULL_CHECK(taosArrayPush(rsp->topics, &topicEp));
1,643,896✔
375
  topicEp.vgs = NULL;
821,948✔
376

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

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

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

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

405
END:
1,049,674✔
406
  PRINT_LOG_END
15,423,568✔
407
  return code;
15,423,568✔
408
}
409

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

422
  SMqRspHead *pHead = buf;
4,935,952✔
423

424
  pHead->mqMsgType = TMQ_MSG_TYPE__EP_RSP;
4,935,952✔
425
  pHead->epoch = serverEpoch;
4,935,952✔
426
  pHead->consumerId = consumerId;
4,935,952✔
427
  pHead->walsver = 0;
4,935,952✔
428
  pHead->walever = 0;
4,935,952✔
429

430
  void *abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
4,935,952✔
431
  if (tEncodeSMqAskEpRsp(&abuf, rsp) < 0) {
4,936,334✔
432
    rpcFreeCont(buf);
×
433
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
434
  }
435

436
  // send rsp
437
  pMsg->info.rsp = buf;
4,936,334✔
438
  pMsg->info.rspLen = tlen;
4,936,334✔
439
  return code;
4,935,952✔
440
}
441

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

454
  MND_TMQ_RETURN_CHECK(tDeserializeSMqAskEpReq(pMsg->pCont, pMsg->contLen, &req));
19,315,271✔
455
  int64_t consumerId = req.consumerId;
19,315,271✔
456
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
19,315,271✔
457
  taosRLockLatch(&pConsumer->lock);
19,310,228✔
458
  if (strncmp(req.cgroup, pConsumer->cgroup, tListLen(pConsumer->cgroup)) != 0) {
19,310,228✔
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);
19,309,846✔
467
  int32_t status = atomic_load_32(&pConsumer->status);
19,309,517✔
468
  if (status != MQ_CONSUMER_STATUS_READY) {
19,309,896✔
469
    mInfo("consumer:0x%" PRIx64 " not ready, status: %s", consumerId, mndConsumerStatusName(status));
1,090,457✔
470
    rsp.code = TSDB_CODE_MND_CONSUMER_NOT_READY;
1,090,457✔
471
  } else {
472
    int32_t epoch = req.epoch;
18,219,439✔
473

474
    // 2. check epoch, only send ep info when epochs do not match
475
    if (epoch != serverEpoch) {
18,219,439✔
476
      mInfo("process ask ep, consumer:0x%" PRIx64 "(epoch %d) update with server epoch %d",
15,423,568✔
477
            consumerId, epoch, serverEpoch);
478
      MND_TMQ_RETURN_CHECK(addEpSetInfo(pMnode, pConsumer, &rsp));
15,423,568✔
479
    }
480
  }
481
  code = buildAskEpRsp(pMsg, &rsp, serverEpoch, consumerId);
4,936,002✔
482

483
END:
19,315,271✔
484
  if (pConsumer != NULL) {
19,315,271✔
485
    taosRUnLockLatch(&pConsumer->lock);
19,310,228✔
486
  }
487
  tDeleteSMqAskEpRsp(&rsp);
488
  mndReleaseConsumer(pMnode, pConsumer);
19,314,926✔
489
  PRINT_LOG_END
19,315,271✔
490
  return code;
19,314,936✔
491
}
492

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

509
END:
133,974✔
510
  PRINT_LOG_END
133,974✔
511
  return code;
133,974✔
512
}
513

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

531
  return code;
667,565✔
532
}
533

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

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

567
  int32_t newTopicNum = taosArrayGetSize(pConsumerNew->assignedTopics);
147,765✔
568
  int32_t oldTopicNum = taosArrayGetSize(pExistedConsumer->currentTopics);
147,765✔
569
  int32_t i = 0, j = 0;
147,765✔
570
  while (i < oldTopicNum || j < newTopicNum) {
298,549✔
571
    if (i >= oldTopicNum) {
150,784✔
572
      void* tmp = taosArrayGetP(pConsumerNew->assignedTopics, j);
754✔
573
      MND_TMQ_NULL_CHECK(tmp);
754✔
574
      ADD_TOPIC_TO_ARRAY(tmp, rebNewTopics);
1,508✔
575
      j++;
754✔
576
      continue;
754✔
577
    } else if (j >= newTopicNum) {
150,030✔
578
      void* tmp = taosArrayGetP(pExistedConsumer->currentTopics, i);
140,098✔
579
      MND_TMQ_NULL_CHECK(tmp);
140,098✔
580
      ADD_TOPIC_TO_ARRAY(tmp, rebRemovedTopics);
280,196✔
581
      i++;
140,098✔
582
      continue;
140,098✔
583
    } else {
584
      char *oldTopic = taosArrayGetP(pExistedConsumer->currentTopics, i);
9,932✔
585
      MND_TMQ_NULL_CHECK(oldTopic);
9,932✔
586
      char *newTopic = taosArrayGetP(pConsumerNew->assignedTopics, j);
9,932✔
587
      MND_TMQ_NULL_CHECK(newTopic);
9,932✔
588
      int   comp = strcmp(oldTopic, newTopic);
9,932✔
589
      if (comp == 0) {
9,932✔
590
        i++;
9,932✔
591
        j++;
9,932✔
592
        continue;
9,932✔
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) {
147,765✔
606
    code = TSDB_CODE_TMQ_NO_NEED_REBALANCE;
9,932✔
607
  }
608

609
END:
137,833✔
610
  taosRUnLockLatch(&pExistedConsumer->lock);
147,765✔
611
  PRINT_LOG_END
147,765✔
612
  return code;
147,765✔
613
}
614

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

622
  int32_t newTopicNum = taosArrayGetSize(pTopicList);
335,019✔
623
  for (int i = 0; i < newTopicNum; i++) {
535,564✔
624
    int32_t gNum = mndGetGroupNumByTopic(pMnode, (const char *)taosArrayGetP(pTopicList, i));
200,959✔
625
    if (gNum >= MND_MAX_GROUP_PER_TOPIC) {
200,959✔
626
      return TSDB_CODE_TMQ_GROUP_OUT_OF_RANGE;
414✔
627
    }
628
  }
629
  return 0;
334,605✔
630
}
631

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

652
    mInfo("receive tmq subscribe request from existed consumer:0x%" PRIx64
147,765✔
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) {
147,765✔
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));
147,765✔
662
    MND_TMQ_RETURN_CHECK(getTopicAddDelete(pExistedConsumer, pConsumerNew));
147,765✔
663
  }
664
  if (ppConsumer){
322,117✔
665
    *ppConsumer = pConsumerNew;
322,117✔
666
    pConsumerNew = NULL;
322,117✔
667
  }
668

669
END:
331,707✔
670
  PRINT_LOG_END
332,049✔
671
  mndReleaseConsumer(pMnode, pExistedConsumer);
332,049✔
672
  tDeleteSMqConsumerObj(pConsumerNew);
332,049✔
673
  return code;
332,049✔
674
}
675

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

687
  PRINT_LOG_START
435,709✔
688
  SCMSubscribeReq subscribe = {0};
435,709✔
689
  MND_TMQ_RETURN_CHECK(tDeserializeSCMSubscribeReq(msgStr, &subscribe, pMsg->contLen));
871,418✔
690
  bool unSubscribe = (taosArrayGetSize(subscribe.topicNames) == 0);
435,709✔
691
  if(unSubscribe){
435,709✔
692
    SMqConsumerObj *pConsumerTmp = NULL;
237,769✔
693
    MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, subscribe.consumerId, &pConsumerTmp));
237,769✔
694
    taosRLockLatch(&pConsumerTmp->lock);
235,722✔
695
    size_t topicNum = taosArrayGetSize(pConsumerTmp->assignedTopics);
235,722✔
696
    taosRUnLockLatch(&pConsumerTmp->lock);
235,722✔
697
    mndReleaseConsumer(pMnode, pConsumerTmp);
235,722✔
698
    if (topicNum == 0){
235,722✔
699
      goto END;
98,643✔
700
    }
701
  }
702
  MND_TMQ_RETURN_CHECK(checkAndSortTopic(pMnode, subscribe.topicNames));
335,019✔
703
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY,
334,605✔
704
                          (unSubscribe ? TRN_CONFLICT_NOTHING :TRN_CONFLICT_DB_INSIDE),
705
                          pMsg, "subscribe");
706
  MND_TMQ_NULL_CHECK(pTrans);
334,605✔
707

708
  MND_TMQ_RETURN_CHECK(validateTopics(pTrans, &subscribe, pMnode, RPC_MSG_USER(pMsg), RPC_MSG_TOKEN(pMsg)));
334,605✔
709
  MND_TMQ_RETURN_CHECK(buildSubConsumer(pMnode, &subscribe, &pConsumerNew));
332,049✔
710
  MND_TMQ_RETURN_CHECK(mndSetConsumerCommitLogs(pTrans, pConsumerNew));
322,117✔
711
  MND_TMQ_RETURN_CHECK(mndTransPrepare(pMnode, pTrans));
322,117✔
712
  code = TSDB_CODE_ACTION_IN_PROGRESS;
322,117✔
713

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

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

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

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

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

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

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

756
  terrno = TSDB_CODE_SUCCESS;
941,481✔
757

758
CM_ENCODE_OVER:
941,481✔
759
  taosMemoryFreeClear(buf);
941,481✔
760
  if (terrno != 0) {
941,481✔
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);
941,481✔
767
  return pRaw;
941,481✔
768
}
769

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

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

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

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

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

801
  int32_t dataPos = 0;
816,204✔
802
  int32_t len;
815,406✔
803
  SDB_GET_INT32(pRaw, dataPos, &len, CM_DECODE_OVER);
816,204✔
804
  buf = taosMemoryMalloc(len);
816,204✔
805
  if (buf == NULL) {
816,204✔
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);
816,204✔
811
  SDB_GET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_DECODE_OVER);
816,204✔
812

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

818
CM_DECODE_OVER:
816,204✔
819
  taosMemoryFreeClear(buf);
816,204✔
820
  if (terrno != TSDB_CODE_SUCCESS) {
816,204✔
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;
816,204✔
827
}
828

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

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

849
// remove from topic list
850
static void removeFromTopicList(SArray *topicList, const char *pTopic, int64_t consumerId, char *type) {
483,115✔
851
  if (topicList == NULL || pTopic == NULL) {
483,115✔
852
    return;
×
853
  }
854
  int32_t size = taosArrayGetSize(topicList);
483,115✔
855
  for (int32_t i = 0; i < size; i++) {
488,094✔
856
    char *p = taosArrayGetP(topicList, i);
482,800✔
857
    if (strcmp(pTopic, p) == 0) {
482,800✔
858
      taosArrayRemove(topicList, i);
477,821✔
859
      taosMemoryFree(p);
477,821✔
860

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

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

882
  return existing;
189,687✔
883
}
884

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

897
  if (pNewConsumer->updateType == CONSUMER_UPDATE_SUB) {
487,939✔
898
    TSWAP(pOldConsumer->rebNewTopics, pNewConsumer->rebNewTopics);
139,157✔
899
    TSWAP(pOldConsumer->rebRemovedTopics, pNewConsumer->rebRemovedTopics);
139,157✔
900
    TSWAP(pOldConsumer->assignedTopics, pNewConsumer->assignedTopics);
139,157✔
901

902
    pOldConsumer->subscribeTime = taosGetTimestampMs();
139,157✔
903
    pOldConsumer->status = MQ_CONSUMER_STATUS_REBALANCE;
139,157✔
904
    mInfo("consumer:0x%" PRIx64 " subscribe update, modify existed consumer", pOldConsumer->consumerId);
139,157✔
905
  } else if (pNewConsumer->updateType == CONSUMER_UPDATE_REB) {
348,782✔
906
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
12,381✔
907
    pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
12,381✔
908
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
12,381✔
909
    mInfo("tmq rebalance consumer:0x%" PRIx64 " rebalance update, only rebalance time", pOldConsumer->consumerId);
12,381✔
910
  } else if (pNewConsumer->updateType == CONSUMER_ADD_REB) {
336,401✔
911
    void *tmp = taosArrayGetP(pNewConsumer->rebNewTopics, 0);
189,687✔
912
    MND_TMQ_NULL_CHECK(tmp);
189,687✔
913
    char *pNewTopic = taosStrdup(tmp);
189,687✔
914
    MND_TMQ_NULL_CHECK(pNewTopic);
189,687✔
915
    removeFromTopicList(pOldConsumer->rebNewTopics, pNewTopic, pOldConsumer->consumerId, "new");
189,687✔
916
    bool existing = existInCurrentTopicList(pOldConsumer, pNewTopic);
189,687✔
917
    if (existing) {
189,687✔
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));
379,374✔
921
      pNewTopic = NULL;
189,687✔
922
      taosArraySort(pOldConsumer->currentTopics, taosArrayCompareString);
189,687✔
923
    }
924

925
    int32_t status = pOldConsumer->status;
189,687✔
926
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
189,687✔
927
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
186,668✔
928
    }
929

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

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

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

953
    mInfo("tmq rebalanceconsumer:0x%" PRIx64 " rebalance update remove, state (%d)%s -> (%d)%s, new epoch:%d, reb-time:%" PRId64
146,714✔
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);
487,939✔
963
  taosWUnLockLatch(&pOldConsumer->lock);
487,939✔
964

965
  PRINT_LOG_END
487,939✔
966

967
  return code;
487,939✔
968
}
969

970
int32_t mndAcquireConsumer(SMnode *pMnode, int64_t consumerId, SMqConsumerObj** pConsumer) {
21,377,480✔
971
  if (pMnode == NULL || pConsumer == NULL) {
21,377,480✔
972
    return TSDB_CODE_INVALID_PARA;
×
973
  }
974
  SSdb           *pSdb = pMnode->pSdb;
21,377,480✔
975
  *pConsumer = sdbAcquire(pSdb, SDB_CONSUMER, &consumerId);
21,377,480✔
976
  if (*pConsumer == NULL) {
21,377,074✔
977
    return TSDB_CODE_MND_CONSUMER_NOT_EXIST;
196,956✔
978
  }
979
  return 0;
21,180,118✔
980
}
981

982
void mndReleaseConsumer(SMnode *pMnode, SMqConsumerObj *pConsumer) {
41,811,010✔
983
  if (pMnode == NULL || pConsumer == NULL) {
41,811,010✔
984
    return;
18,738,755✔
985
  }
986
  SSdb *pSdb = pMnode->pSdb;
23,072,255✔
987
  sdbRelease(pSdb, pConsumer);
23,072,255✔
988
}
989

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1153
END:
89,381✔
1154
  taosRUnLockLatch(&pConsumer->lock);
90,279✔
1155
  PRINT_LOG_END
90,279✔
1156
  return code;
90,279✔
1157
}
1158

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

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

1190
  pShow->numOfRows += numOfRows;
17,595✔
1191

1192
END:
17,595✔
1193
  sdbRelease(pSdb, pConsumer);
17,595✔
1194
  sdbCancelFetch(pSdb, pShow->pIter);
17,595✔
1195
  mndReleaseUser(pMnode, pOperUser);
17,595✔
1196
  if (code != 0) {
17,595✔
1197
    mError("show consumer failed, code:%d", code);
×
1198
    return code;
×
1199
  } else {
1200
    mDebug("show consumer processed, numOfRows:%d", numOfRows);
17,595✔
1201
    return numOfRows;
17,595✔
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) {
5,040,031✔
1212
  switch (status) {
5,040,031✔
1213
    case MQ_CONSUMER_STATUS_READY:
2,393,808✔
1214
      return "ready";
2,393,808✔
1215
    case MQ_CONSUMER_STATUS_REBALANCE:
2,646,223✔
1216
      return "rebalancing";
2,646,223✔
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