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

taosdata / TDengine / #4887

16 Dec 2025 08:27AM UTC coverage: 65.289% (-0.003%) from 65.292%
#4887

push

travis-ci

web-flow
feat[TS-7233]: audit (#33850)

377 of 536 new or added lines in 28 files covered. (70.34%)

1025 existing lines in 111 files now uncovered.

178977 of 274129 relevant lines covered (65.29%)

102580217.43 hits per line

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

88.45
/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 "mndVgroup.h"
25
#include "tcompare.h"
26
#include "tname.h"
27

28
#define MND_CONSUMER_VER_NUMBER   3
29
#define MND_CONSUMER_RESERVE_SIZE 64
30

31
#define MND_MAX_GROUP_PER_TOPIC 100
32

33
static int32_t mndConsumerActionInsert(SSdb *pSdb, SMqConsumerObj *pConsumer);
34
static int32_t mndConsumerActionDelete(SSdb *pSdb, SMqConsumerObj *pConsumer);
35
static int32_t mndConsumerActionUpdate(SSdb *pSdb, SMqConsumerObj *pOldConsumer, SMqConsumerObj *pNewConsumer);
36
static int32_t mndRetrieveConsumer(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
37
static void    mndCancelGetNextConsumer(SMnode *pMnode, void *pIter);
38

39
static int32_t mndProcessSubscribeReq(SRpcMsg *pMsg);
40
static int32_t mndProcessAskEpReq(SRpcMsg *pMsg);
41
static int32_t mndProcessMqHbReq(SRpcMsg *pMsg);
42
static int32_t mndProcessConsumerClearMsg(SRpcMsg *pMsg);
43

44
int32_t mndInitConsumer(SMnode *pMnode) {
516,656✔
45
  SSdbTable table = {
516,656✔
46
      .sdbType = SDB_CONSUMER,
47
      .keyType = SDB_KEY_INT64,
48
      .encodeFp = (SdbEncodeFp)mndConsumerActionEncode,
49
      .decodeFp = (SdbDecodeFp)mndConsumerActionDecode,
50
      .insertFp = (SdbInsertFp)mndConsumerActionInsert,
51
      .updateFp = (SdbUpdateFp)mndConsumerActionUpdate,
52
      .deleteFp = (SdbDeleteFp)mndConsumerActionDelete,
53
  };
54

55
  if (pMnode == NULL){
516,656✔
56
    return TSDB_CODE_INVALID_PARA;
×
57
  }
58
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_SUBSCRIBE, mndProcessSubscribeReq);
516,656✔
59
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_HB, mndProcessMqHbReq);
516,656✔
60
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_ASK_EP, mndProcessAskEpReq);
516,656✔
61
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_LOST_CONSUMER_CLEAR, mndProcessConsumerClearMsg);
516,656✔
62

63
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndRetrieveConsumer);
516,656✔
64
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndCancelGetNextConsumer);
516,656✔
65

66
  return sdbSetTable(pMnode->pSdb, table);
516,656✔
67
}
68

69
void mndCleanupConsumer(SMnode *pMnode) {}
516,539✔
70

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

81
  *(int64_t*)msg = consumerId;
38,317✔
82
  SRpcMsg rpcMsg = {
38,317✔
83
      .msgType = msgType,
84
      .pCont = msg,
85
      .contLen = sizeof(int64_t),
86
      .info = *info,
87
  };
88

89
  mInfo("mndSendConsumerMsg type:%d consumer:0x%" PRIx64, msgType, consumerId);
38,317✔
90
  MND_TMQ_RETURN_CHECK(tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg));
38,317✔
91

92
END:
38,317✔
93
  PRINT_LOG_END
38,317✔
94
  return code;
38,317✔
95
}
96

97
static int32_t validateOneTopic(STrans* pTrans,char *pOneTopic, SCMSubscribeReq *subscribe, SMnode *pMnode, const char *pUser) {
46,275✔
98
  int32_t      code = 0;
46,275✔
99
  int32_t lino = 0;
46,275✔
100
  SMqTopicObj *pTopic = NULL;
46,275✔
101

102
  PRINT_LOG_START
46,275✔
103
  MND_TMQ_RETURN_CHECK(mndAcquireTopic(pMnode, pOneTopic, &pTopic));
46,275✔
104
  taosRLockLatch(&pTopic->lock);
46,200✔
105

106
  MND_TMQ_RETURN_CHECK(mndCheckTopicPrivilege(pMnode, pUser, MND_OPER_SUBSCRIBE, pTopic));
46,200✔
107
  MND_TMQ_RETURN_CHECK(grantCheckExpire(TSDB_GRANT_SUBSCRIPTION));
46,064✔
108

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

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

142
static int32_t validateTopics(STrans* pTrans, SCMSubscribeReq *subscribe, SMnode *pMnode, const char *pUser) {
87,839✔
143
  if (pTrans == NULL || subscribe == NULL || pMnode == NULL || pUser == NULL) {
87,839✔
144
    return TSDB_CODE_INVALID_PARA;
×
145
  }
146
  int32_t      code = 0;
87,839✔
147
  int32_t lino = 0;
87,839✔
148

149
  PRINT_LOG_START
87,839✔
150
  int32_t numOfTopics = taosArrayGetSize(subscribe->topicNames);
87,839✔
151
  for (int32_t i = 0; i < numOfTopics; i++) {
133,771✔
152
    char *pOneTopic = taosArrayGetP(subscribe->topicNames, i);
46,275✔
153
    MND_TMQ_RETURN_CHECK(validateOneTopic(pTrans, pOneTopic, subscribe, pMnode, pUser));
46,275✔
154
  }
155

156
END:
87,496✔
157
  PRINT_LOG_END
87,839✔
158
  return code;
87,839✔
159
}
160

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

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

178
  MND_TMQ_RETURN_CHECK(tNewSMqConsumerObj(pConsumer->consumerId, pConsumer->cgroup, CONSUMER_CLEAR, NULL, NULL, &pConsumerNew));
38,104✔
179

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

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

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

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

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

229
  rsp->topicPrivileges = taosArrayInit(taosArrayGetSize(pConsumer->currentTopics), sizeof(STopicPrivilege));
252,519✔
230
  MND_TMQ_NULL_CHECK(rsp->topicPrivileges);
252,519✔
231
  for (int32_t i = 0; i < taosArrayGetSize(pConsumer->currentTopics); i++) {
471,623✔
232
    char        *topic = taosArrayGetP(pConsumer->currentTopics, i);
219,104✔
233
    checkOnePrivilege(topic, pMnode, rsp, user);
219,104✔
234
  }
235

236
END:
252,519✔
237
  PRINT_LOG_END
252,519✔
238
  return code;
252,519✔
239
}
240

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

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

269
    mndReleaseSubscribe(pMnode, pSub);
224,688✔
270
  }
271
}
272

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

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

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

307
  MND_TMQ_RETURN_CHECK(tDeserializeSMqHbReq(pMsg->pCont, pMsg->contLen, &req));
255,770✔
308
  int64_t consumerId = req.consumerId;
255,770✔
309
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
255,770✔
310
  taosWLockLatch(&pConsumer->lock);
252,519✔
311
  MND_TMQ_RETURN_CHECK(checkPrivilege(pMnode, pConsumer, &rsp, pMsg->info.conn.user));
252,519✔
312
  atomic_store_32(&pConsumer->hbStatus, 0);
252,519✔
313
  mDebug("consumer:0x%" PRIx64 " receive hb pollFlag:%d pollStatus:%d", consumerId, req.pollFlag, pConsumer->pollStatus);
252,519✔
314
  if (req.pollFlag == 1){
252,519✔
315
    atomic_store_32(&pConsumer->pollStatus, 0);
113,997✔
316
    pConsumer->pollTime = taosGetTimestampMs();
227,994✔
317
  }
318

319
  storeOffsetRows(pMnode, &req, pConsumer);
252,519✔
320
  rsp.debugFlag = tqClientDebugFlag;
252,519✔
321
  code = buildMqHbRsp(pMsg, &rsp);
252,519✔
322

323
END:
255,770✔
324
  if (pConsumer != NULL) {
255,770✔
325
    taosWUnLockLatch(&pConsumer->lock);
252,519✔
326
  }
327
  tDestroySMqHbRsp(&rsp);
255,770✔
328
  mndReleaseConsumer(pMnode, pConsumer);
255,770✔
329
  tDestroySMqHbReq(&req);
255,770✔
330
  PRINT_LOG_END
255,770✔
331
  return code;
255,770✔
332
}
333

334
static int32_t processEachTopicEp(SMnode *pMnode, SMqConsumerObj *pConsumer, char *topic, SMqAskEpRsp *rsp, int32_t epoch) {
97,865✔
335
  int32_t         code = 0;
97,865✔
336
  int32_t         lino = 0;
97,865✔
337
  SMqSubscribeObj *pSub = NULL;
97,865✔
338
  SMqSubTopicEp topicEp = {0};
97,865✔
339
  char  key[TSDB_SUBSCRIBE_KEY_LEN] = {0};
97,865✔
340
  PRINT_LOG_START
97,865✔
341
  (void)snprintf(key, TSDB_SUBSCRIBE_KEY_LEN, "%s%s%s", pConsumer->cgroup, TMQ_SEPARATOR, topic);
97,865✔
342
  if(mndAcquireSubscribeByKey(pMnode, key, &pSub) != 0) {
97,865✔
343
    mWarn("%s failed to acquire subscribe by key:%s", __func__, key);
×
344
    goto END;
×
345
  }
346
  tstrncpy(topicEp.topic, topic, TSDB_TOPIC_FNAME_LEN);
97,865✔
347

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

356
  tstrncpy(topicEp.db, pSub->dbName, TSDB_DB_FNAME_LEN);
97,865✔
357
  for (int32_t j = 0; j < vgNum; j++) {
339,378✔
358
    SMqVgEp *pVgEp = taosArrayGet(pConsumerEp->vgs, j);
241,513✔
359
    if (pVgEp == NULL) {
241,513✔
360
      continue;
×
361
    }
362
    if (epoch == -1) {
241,513✔
363
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, pVgEp->vgId);
51,474✔
364
      if (pVgroup) {
51,474✔
365
        pVgEp->epSet = mndGetVgroupEpset(pMnode, pVgroup);
3,674✔
366
        mndReleaseVgroup(pMnode, pVgroup);
3,674✔
367
      }
368
    }
369
    SMqSubVgEp vgEp = {.epSet = pVgEp->epSet, .vgId = pVgEp->vgId, .offset = -1};
241,513✔
370
    MND_TMQ_NULL_CHECK(taosArrayPush(topicEp.vgs, &vgEp));
483,026✔
371
  }
372
  MND_TMQ_NULL_CHECK(taosArrayPush(rsp->topics, &topicEp));
195,730✔
373
  topicEp.vgs = NULL;
97,865✔
374

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

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

393
  int32_t numOfTopics = taosArrayGetSize(pConsumer->currentTopics);
139,521✔
394
  rsp->topics = taosArrayInit(numOfTopics, sizeof(SMqSubTopicEp));
139,521✔
395
  MND_TMQ_NULL_CHECK(rsp->topics);
139,521✔
396

397
  // handle all topics subscribed by this consumer
398
  for (int32_t i = 0; i < numOfTopics; i++) {
237,386✔
399
    char            *topic = taosArrayGetP(pConsumer->currentTopics, i);
97,865✔
400
    MND_TMQ_RETURN_CHECK(processEachTopicEp(pMnode, pConsumer, topic, rsp, epoch));
97,865✔
401
  }
402

403
END:
139,521✔
404
  PRINT_LOG_END
139,521✔
405
  return code;
139,521✔
406
}
407

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

420
  SMqRspHead *pHead = buf;
591,226✔
421

422
  pHead->mqMsgType = TMQ_MSG_TYPE__EP_RSP;
591,226✔
423
  pHead->epoch = serverEpoch;
591,226✔
424
  pHead->consumerId = consumerId;
591,226✔
425
  pHead->walsver = 0;
591,226✔
426
  pHead->walever = 0;
591,226✔
427

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

434
  // send rsp
435
  pMsg->info.rsp = buf;
591,226✔
436
  pMsg->info.rspLen = tlen;
591,226✔
437
  return code;
591,226✔
438
}
439

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

452
  MND_TMQ_RETURN_CHECK(tDeserializeSMqAskEpReq(pMsg->pCont, pMsg->contLen, &req));
840,796✔
453
  int64_t consumerId = req.consumerId;
840,796✔
454
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
840,796✔
455
  taosRLockLatch(&pConsumer->lock);
837,493✔
456
  if (strncmp(req.cgroup, pConsumer->cgroup, tListLen(pConsumer->cgroup)) != 0) {
837,493✔
457
    mError("consumer:0x%" PRIx64 " group:%s not consistent with data in sdb, saved cgroup:%s", consumerId, req.cgroup,
×
458
           pConsumer->cgroup);
459
    code = TSDB_CODE_MND_CONSUMER_NOT_EXIST;
×
460
    goto END;
×
461
  }
462

463
  // 1. check consumer status
464
  int32_t status = atomic_load_32(&pConsumer->status);
837,421✔
465
  if (status != MQ_CONSUMER_STATUS_READY) {
837,493✔
466
    mInfo("consumer:0x%" PRIx64 " not ready, status: %s", consumerId, mndConsumerStatusName(status));
246,267✔
467
    code = TSDB_CODE_MND_CONSUMER_NOT_READY;
246,267✔
468
    goto END;
246,267✔
469
  }
470

471
  int32_t epoch = req.epoch;
591,226✔
472
  int32_t serverEpoch = atomic_load_32(&pConsumer->epoch);
591,226✔
473

474
  // 2. check epoch, only send ep info when epochs do not match
475
  if (epoch != serverEpoch) {
591,226✔
476
    mInfo("process ask ep, consumer:0x%" PRIx64 "(epoch %d) update with server epoch %d",
139,521✔
477
          consumerId, epoch, serverEpoch);
478
    MND_TMQ_RETURN_CHECK(addEpSetInfo(pMnode, pConsumer, epoch, &rsp));
139,521✔
479
  }
480

481
  code = buildAskEpRsp(pMsg, &rsp, serverEpoch, consumerId);
591,226✔
482

483
END:
840,796✔
484
  if (pConsumer != NULL) {
840,796✔
485
    taosRUnLockLatch(&pConsumer->lock);
837,493✔
486
  }
487
  tDeleteSMqAskEpRsp(&rsp);
488
  mndReleaseConsumer(pMnode, pConsumer);
840,796✔
489
  PRINT_LOG_END
840,796✔
490
  return code;
840,724✔
491
}
492

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

509
END:
38,653✔
510
  PRINT_LOG_END
38,653✔
511
  return code;
38,653✔
512
}
513

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

531
  return code;
178,358✔
532
}
533

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

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

567
  int32_t newTopicNum = taosArrayGetSize(pConsumerNew->assignedTopics);
43,028✔
568
  int32_t oldTopicNum = taosArrayGetSize(pExistedConsumer->currentTopics);
43,028✔
569
  int32_t i = 0, j = 0;
43,028✔
570
  while (i < oldTopicNum || j < newTopicNum) {
86,677✔
571
    if (i >= oldTopicNum) {
43,649✔
572
      void* tmp = taosArrayGetP(pConsumerNew->assignedTopics, j);
271✔
573
      MND_TMQ_NULL_CHECK(tmp);
271✔
574
      ADD_TOPIC_TO_ARRAY(tmp, rebNewTopics);
542✔
575
      j++;
271✔
576
      continue;
271✔
577
    } else if (j >= newTopicNum) {
43,378✔
578
      void* tmp = taosArrayGetP(pExistedConsumer->currentTopics, i);
42,806✔
579
      MND_TMQ_NULL_CHECK(tmp);
42,806✔
580
      ADD_TOPIC_TO_ARRAY(tmp, rebRemovedTopics);
85,612✔
581
      i++;
42,806✔
582
      continue;
42,806✔
583
    } else {
584
      char *oldTopic = taosArrayGetP(pExistedConsumer->currentTopics, i);
572✔
585
      MND_TMQ_NULL_CHECK(oldTopic);
572✔
586
      char *newTopic = taosArrayGetP(pConsumerNew->assignedTopics, j);
572✔
587
      MND_TMQ_NULL_CHECK(newTopic);
572✔
588
      int   comp = strcmp(oldTopic, newTopic);
572✔
589
      if (comp == 0) {
572✔
590
        i++;
572✔
591
        j++;
572✔
592
        continue;
572✔
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) {
43,028✔
606
    code = TSDB_CODE_TMQ_NO_NEED_REBALANCE;
572✔
607
  }
608

609
END:
42,456✔
610
  taosRUnLockLatch(&pExistedConsumer->lock);
43,028✔
611
  PRINT_LOG_END
43,028✔
612
  return code;
43,028✔
613
}
614

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

622
  int32_t newTopicNum = taosArrayGetSize(pTopicList);
87,839✔
623
  for (int i = 0; i < newTopicNum; i++) {
134,114✔
624
    int32_t gNum = mndGetGroupNumByTopic(pMnode, (const char *)taosArrayGetP(pTopicList, i));
46,275✔
625
    if (gNum >= MND_MAX_GROUP_PER_TOPIC) {
46,275✔
UNCOV
626
      return TSDB_CODE_TMQ_GROUP_OUT_OF_RANGE;
×
627
    }
628
  }
629
  return 0;
87,839✔
630
}
631

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

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

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

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

687
  PRINT_LOG_START
125,758✔
688
  SCMSubscribeReq subscribe = {0};
125,758✔
689
  MND_TMQ_RETURN_CHECK(tDeserializeSCMSubscribeReq(msgStr, &subscribe, pMsg->contLen));
251,516✔
690
  bool unSubscribe = (taosArrayGetSize(subscribe.topicNames) == 0);
125,758✔
691
  if(unSubscribe){
125,758✔
692
    SMqConsumerObj *pConsumerTmp = NULL;
80,104✔
693
    MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, subscribe.consumerId, &pConsumerTmp));
80,104✔
694
    taosRLockLatch(&pConsumerTmp->lock);
78,314✔
695
    size_t topicNum = taosArrayGetSize(pConsumerTmp->assignedTopics);
78,314✔
696
    taosRUnLockLatch(&pConsumerTmp->lock);
78,314✔
697
    mndReleaseConsumer(pMnode, pConsumerTmp);
78,314✔
698
    if (topicNum == 0){
78,314✔
699
      goto END;
36,129✔
700
    }
701
  }
702
  MND_TMQ_RETURN_CHECK(checkAndSortTopic(pMnode, subscribe.topicNames));
87,839✔
703
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY,
87,839✔
704
                          (unSubscribe ? TRN_CONFLICT_NOTHING :TRN_CONFLICT_DB_INSIDE),
705
                          pMsg, "subscribe");
706
  MND_TMQ_NULL_CHECK(pTrans);
87,839✔
707

708
  MND_TMQ_RETURN_CHECK(validateTopics(pTrans, &subscribe, pMnode, pMsg->info.conn.user));
87,839✔
709
  MND_TMQ_RETURN_CHECK(buildSubConsumer(pMnode, &subscribe, &pConsumerNew));
87,496✔
710
  MND_TMQ_RETURN_CHECK(mndSetConsumerCommitLogs(pTrans, pConsumerNew));
86,924✔
711
  MND_TMQ_RETURN_CHECK(mndTransPrepare(pMnode, pTrans));
86,924✔
712
  code = TSDB_CODE_ACTION_IN_PROGRESS;
86,924✔
713

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

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

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

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

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

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

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

756
  terrno = TSDB_CODE_SUCCESS;
223,884✔
757

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

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

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

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

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

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

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

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

818
  tmsgUpdateDnodeEpSet(&pConsumer->ep);
218,507✔
819

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

828
  return pRow;
218,507✔
829
}
830

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

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

851
// remove from topic list
852
static void removeFromTopicList(SArray *topicList, const char *pTopic, int64_t consumerId, char *type) {
134,503✔
853
  if (topicList == NULL || pTopic == NULL) {
134,503✔
854
    return;
×
855
  }
856
  int32_t size = taosArrayGetSize(topicList);
134,503✔
857
  for (int32_t i = 0; i < size; i++) {
135,571✔
858
    char *p = taosArrayGetP(topicList, i);
133,963✔
859
    if (strcmp(pTopic, p) == 0) {
133,963✔
860
      taosArrayRemove(topicList, i);
132,895✔
861
      taosMemoryFree(p);
132,895✔
862

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

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

884
  return existing;
45,479✔
885
}
886

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

899
  if (pNewConsumer->updateType == CONSUMER_UPDATE_SUB) {
134,260✔
900
    TSWAP(pOldConsumer->rebNewTopics, pNewConsumer->rebNewTopics);
42,554✔
901
    TSWAP(pOldConsumer->rebRemovedTopics, pNewConsumer->rebRemovedTopics);
42,554✔
902
    TSWAP(pOldConsumer->assignedTopics, pNewConsumer->assignedTopics);
42,554✔
903

904
    pOldConsumer->subscribeTime = taosGetTimestampMs();
42,554✔
905
    pOldConsumer->status = MQ_CONSUMER_STATUS_REBALANCE;
42,554✔
906
    mInfo("consumer:0x%" PRIx64 " subscribe update, modify existed consumer", pOldConsumer->consumerId);
42,554✔
907
  } else if (pNewConsumer->updateType == CONSUMER_UPDATE_REB) {
91,706✔
908
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
1,715✔
909
    pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
1,715✔
910
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
1,715✔
911
    mInfo("tmq rebalance consumer:0x%" PRIx64 " rebalance update, only rebalance time", pOldConsumer->consumerId);
1,715✔
912
  } else if (pNewConsumer->updateType == CONSUMER_ADD_REB) {
89,991✔
913
    void *tmp = taosArrayGetP(pNewConsumer->rebNewTopics, 0);
45,479✔
914
    MND_TMQ_NULL_CHECK(tmp);
45,479✔
915
    char *pNewTopic = taosStrdup(tmp);
45,479✔
916
    MND_TMQ_NULL_CHECK(pNewTopic);
45,479✔
917
    removeFromTopicList(pOldConsumer->rebNewTopics, pNewTopic, pOldConsumer->consumerId, "new");
45,479✔
918
    bool existing = existInCurrentTopicList(pOldConsumer, pNewTopic);
45,479✔
919
    if (existing) {
45,479✔
920
      mError("tmq rebalance consumer:0x%" PRIx64 " add new topic:%s should not in currentTopics", pOldConsumer->consumerId, pNewTopic);
×
921
    } else {
922
      MND_TMQ_NULL_CHECK(taosArrayPush(pOldConsumer->currentTopics, &pNewTopic));
90,958✔
923
      pNewTopic = NULL;
45,479✔
924
      taosArraySort(pOldConsumer->currentTopics, taosArrayCompareString);
45,479✔
925
    }
926

927
    int32_t status = pOldConsumer->status;
45,479✔
928
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
45,479✔
929
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
44,858✔
930
    }
931

932
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
45,479✔
933
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
45,479✔
934

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

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

948
    int32_t status = pOldConsumer->status;
44,512✔
949
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
44,512✔
950
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
43,891✔
951
    }
952
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
44,512✔
953
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
44,512✔
954

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

963
END:
×
964
  taosMemoryFree(pNewTopic);
134,260✔
965
  taosWUnLockLatch(&pOldConsumer->lock);
134,260✔
966

967
  PRINT_LOG_END
134,260✔
968

969
  return code;
134,260✔
970
}
971

972
int32_t mndAcquireConsumer(SMnode *pMnode, int64_t consumerId, SMqConsumerObj** pConsumer) {
1,302,483✔
973
  if (pMnode == NULL || pConsumer == NULL) {
1,302,483✔
974
    return TSDB_CODE_INVALID_PARA;
×
975
  }
976
  SSdb           *pSdb = pMnode->pSdb;
1,302,483✔
977
  *pConsumer = sdbAcquire(pSdb, SDB_CONSUMER, &consumerId);
1,302,483✔
978
  if (*pConsumer == NULL) {
1,302,483✔
979
    return TSDB_CODE_MND_CONSUMER_NOT_EXIST;
53,025✔
980
  }
981
  return 0;
1,249,458✔
982
}
983

984
void mndReleaseConsumer(SMnode *pMnode, SMqConsumerObj *pConsumer) {
12,361,510✔
985
  if (pMnode == NULL || pConsumer == NULL) {
12,361,510✔
986
    return;
10,762,324✔
987
  }
988
  SSdb *pSdb = pMnode->pSdb;
1,599,186✔
989
  sdbRelease(pSdb, pConsumer);
1,599,186✔
990
}
991

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1098
END:
3,743✔
1099
  PRINT_LOG_END
3,743✔
1100
  taosMemoryFreeClear(status);
3,743✔
1101
  taosMemoryFreeClear(parasStr);
3,743✔
1102
  return code;
3,743✔
1103
}
1104

1105
static int32_t retrieveOneConsumer(SMqConsumerObj *pConsumer, int32_t* numOfRows, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
4,239✔
1106
  int32_t         code = 0;
4,239✔
1107
  int32_t         lino = 0;
4,239✔
1108
  PRINT_LOG_START
4,239✔
1109
  taosRLockLatch(&pConsumer->lock);
4,239✔
1110
  mDebug("showing consumer:0x%" PRIx64, pConsumer->consumerId);
4,239✔
1111
  if (taosArrayGetSize(pConsumer->assignedTopics) == 0) {
4,239✔
1112
    mInfo("showing consumer:0x%" PRIx64 " no assigned topic, skip", pConsumer->consumerId);
496✔
1113
    goto END;
496✔
1114
  }
1115

1116

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

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

1128
  for (int32_t i = 0; i < topicSz; i++) {
7,486✔
1129
    MND_TMQ_RETURN_CHECK(buildResult(pConsumer, pShow, pBlock, *numOfRows, taosArrayGetP(pConsumer->assignedTopics, i), hasTopic));
3,743✔
1130
    (*numOfRows)++;
3,743✔
1131
  }
1132

1133
END:
3,743✔
1134
  taosRUnLockLatch(&pConsumer->lock);
4,239✔
1135
  PRINT_LOG_END
4,239✔
1136
  return code;
4,239✔
1137
}
1138

1139
static int32_t mndRetrieveConsumer(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
9,958✔
1140
  if (pReq == NULL || pShow == NULL || pBlock == NULL) {
9,958✔
1141
    return TSDB_CODE_INVALID_PARA;
×
1142
  }
1143
  SMnode         *pMnode = pReq->info.node;
9,958✔
1144
  SSdb           *pSdb = pMnode->pSdb;
9,958✔
1145
  int32_t         numOfRows = 0;
9,958✔
1146
  SMqConsumerObj *pConsumer = NULL;
9,958✔
1147
  int32_t         code = 0;
9,958✔
1148
  int32_t         lino = 0;
9,958✔
1149
  PRINT_LOG_START
9,958✔
1150

1151
  while (numOfRows < rowsCapacity) {
14,197✔
1152
    pShow->pIter = sdbFetch(pSdb, SDB_CONSUMER, pShow->pIter, (void **)&pConsumer);
14,197✔
1153
    if (pShow->pIter == NULL) {
14,197✔
1154
      break;
9,958✔
1155
    }
1156
    MND_TMQ_RETURN_CHECK(retrieveOneConsumer(pConsumer, &numOfRows, pShow, pBlock, rowsCapacity));
4,239✔
1157
    
1158
    pBlock->info.rows = numOfRows;
4,239✔
1159
    sdbRelease(pSdb, pConsumer);
4,239✔
1160
    pConsumer = NULL;
4,239✔
1161
  }
1162

1163
  pShow->numOfRows += numOfRows;
9,958✔
1164

1165
END:
9,958✔
1166
  sdbRelease(pSdb, pConsumer);
9,958✔
1167
  sdbCancelFetch(pSdb, pShow->pIter);
9,958✔
1168
  if (code != 0) {
9,958✔
1169
    mError("show consumer failed, code:%d", code);
×
1170
    return code;
×
1171
  } else {
1172
    mDebug("show consumer processed, numOfRows:%d", numOfRows);
9,958✔
1173
    return numOfRows;
9,958✔
1174
  }
1175
}
1176

1177
static void mndCancelGetNextConsumer(SMnode *pMnode, void *pIter) {
×
1178
  if (pMnode == NULL || pIter == NULL) return;
×
1179
  SSdb *pSdb = pMnode->pSdb;
×
1180
  sdbCancelFetchByType(pSdb, pIter, SDB_CONSUMER);
×
1181
}
1182

1183
const char *mndConsumerStatusName(int status) {
1,126,463✔
1184
  switch (status) {
1,126,463✔
1185
    case MQ_CONSUMER_STATUS_READY:
485,884✔
1186
      return "ready";
485,884✔
1187
    case MQ_CONSUMER_STATUS_REBALANCE:
640,579✔
1188
      return "rebalancing";
640,579✔
1189
    default:
×
1190
      return "unknown";
×
1191
  }
1192
}
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