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

taosdata / TDengine / #4800

16 Oct 2025 09:19AM UTC coverage: 53.935% (-7.1%) from 61.083%
#4800

push

travis-ci

web-flow
Merge b32e3a393 into a190048d5

134724 of 323629 branches covered (41.63%)

Branch coverage included in aggregate %.

184803 of 268802 relevant lines covered (68.75%)

69058627.2 hits per line

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

46.73
/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) {
166,555✔
45
  SSdbTable table = {
166,555✔
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){
166,555!
56
    return TSDB_CODE_INVALID_PARA;
×
57
  }
58
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_SUBSCRIBE, mndProcessSubscribeReq);
166,555✔
59
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_HB, mndProcessMqHbReq);
166,555✔
60
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_ASK_EP, mndProcessAskEpReq);
166,555✔
61
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_LOST_CONSUMER_CLEAR, mndProcessConsumerClearMsg);
166,555✔
62

63
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndRetrieveConsumer);
166,555✔
64
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndCancelGetNextConsumer);
166,555✔
65

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

69
void mndCleanupConsumer(SMnode *pMnode) {}
166,509✔
70

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

79
  *(int64_t*)msg = consumerId;
2,483✔
80
  SRpcMsg rpcMsg = {
2,483✔
81
      .msgType = msgType,
82
      .pCont = msg,
83
      .contLen = sizeof(int64_t),
84
      .info = *info,
85
  };
86

87
  mInfo("mndSendConsumerMsg type:%d consumer:0x%" PRIx64, msgType, consumerId);
2,483!
88
  MND_TMQ_RETURN_CHECK(tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg));
2,483!
89
  return code;
2,483✔
90

91
END:
×
92
  taosMemoryFree(msg);
×
93
  return code;
×
94
}
95

96
static int32_t validateTopics(STrans* pTrans, SCMSubscribeReq *subscribe, SMnode *pMnode, const char *pUser) {
6,360✔
97
  if (pTrans == NULL || subscribe == NULL || pMnode == NULL || pUser == NULL) {
6,360!
98
    return TSDB_CODE_INVALID_PARA;
×
99
  }
100
  SMqTopicObj *pTopic = NULL;
6,360✔
101
  int32_t      code = 0;
6,360✔
102

103
  int32_t numOfTopics = taosArrayGetSize(subscribe->topicNames);
6,360✔
104
  for (int32_t i = 0; i < numOfTopics; i++) {
9,843✔
105
    char *pOneTopic = taosArrayGetP(subscribe->topicNames, i);
3,483✔
106
    MND_TMQ_RETURN_CHECK(mndAcquireTopic(pMnode, pOneTopic, &pTopic));
3,483!
107
    MND_TMQ_RETURN_CHECK(mndCheckTopicPrivilege(pMnode, pUser, MND_OPER_SUBSCRIBE, pTopic));
3,483!
108
    MND_TMQ_RETURN_CHECK(grantCheckExpire(TSDB_GRANT_SUBSCRIPTION));
3,483!
109

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

136
END:
×
137
  mndReleaseTopic(pMnode, pTopic);
×
138
  return code;
×
139
}
140

141
static int32_t mndProcessConsumerClearMsg(SRpcMsg *pMsg) {
2,483✔
142
  if (pMsg == NULL || pMsg->pCont == NULL) {
2,483!
143
    return TSDB_CODE_INVALID_PARA;
×
144
  }
145
  int32_t              code = 0;
2,483✔
146
  SMnode              *pMnode = pMsg->info.node;
2,483✔
147
  SMqConsumerClearMsg *pClearMsg = pMsg->pCont;
2,483✔
148
  SMqConsumerObj      *pConsumerNew = NULL;
2,483✔
149
  STrans              *pTrans = NULL;
2,483✔
150
  SMqConsumerObj      *pConsumer = NULL;
2,483✔
151

152
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, pClearMsg->consumerId, &pConsumer));
2,483!
153
  mInfo("consumer:0x%" PRIx64 " needs to be cleared, status %s", pClearMsg->consumerId,
2,483!
154
        mndConsumerStatusName(pConsumer->status));
155

156
  MND_TMQ_RETURN_CHECK(tNewSMqConsumerObj(pConsumer->consumerId, pConsumer->cgroup, -1, NULL, NULL, &pConsumerNew));
2,483!
157
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, pMsg, "clear-csm");
2,483✔
158
  MND_TMQ_NULL_CHECK(pTrans);
2,483!
159
  MND_TMQ_RETURN_CHECK(mndSetConsumerDropLogs(pTrans, pConsumerNew));
2,483!
160
  code = mndTransPrepare(pMnode, pTrans);
2,483✔
161

162
END:
2,483✔
163
  mndReleaseConsumer(pMnode, pConsumer);
2,483✔
164
  tDeleteSMqConsumerObj(pConsumerNew);
2,483✔
165
  mndTransDrop(pTrans);
2,483✔
166
  return code;
2,483✔
167
}
168

169
static int32_t checkPrivilege(SMnode *pMnode, SMqConsumerObj *pConsumer, SMqHbRsp *rsp, char *user) {
11,474✔
170
  if (pMnode == NULL || pConsumer == NULL || rsp == NULL || user == NULL) {
11,474!
171
    return TSDB_CODE_INVALID_PARA;
×
172
  }
173
  int32_t code = 0;
11,474✔
174
  rsp->topicPrivileges = taosArrayInit(taosArrayGetSize(pConsumer->currentTopics), sizeof(STopicPrivilege));
11,474✔
175
  MND_TMQ_NULL_CHECK(rsp->topicPrivileges);
11,474!
176
  for (int32_t i = 0; i < taosArrayGetSize(pConsumer->currentTopics); i++) {
21,801✔
177
    char        *topic = taosArrayGetP(pConsumer->currentTopics, i);
10,327✔
178
    SMqTopicObj *pTopic = NULL;
10,327✔
179
    code = mndAcquireTopic(pMnode, topic, &pTopic);
10,327✔
180
    if (code != TSDB_CODE_SUCCESS) {
10,327!
181
      continue;
×
182
    }
183
    STopicPrivilege *data = taosArrayReserve(rsp->topicPrivileges, 1);
10,327✔
184
    MND_TMQ_NULL_CHECK(data);
10,327!
185
    tstrncpy(data->topic, topic, TSDB_TOPIC_FNAME_LEN);
10,327!
186
    if (mndCheckTopicPrivilege(pMnode, user, MND_OPER_SUBSCRIBE, pTopic) != 0 ||
20,654!
187
        grantCheckExpire(TSDB_GRANT_SUBSCRIPTION) < 0) {
10,327✔
188
      data->noPrivilege = 1;
×
189
    } else {
190
      data->noPrivilege = 0;
10,327✔
191
    }
192
    mndReleaseTopic(pMnode, pTopic);
10,327✔
193
  }
194
END:
11,474✔
195
  return code;
11,474✔
196
}
197

198
static void storeOffsetRows(SMnode *pMnode, SMqHbReq *req, SMqConsumerObj *pConsumer){
11,474✔
199
  if (pMnode == NULL || req == NULL || pConsumer == NULL){
11,474!
200
    return;
×
201
  }
202
  for (int i = 0; i < taosArrayGetSize(req->topics); i++) {
22,381✔
203
    TopicOffsetRows *data = taosArrayGet(req->topics, i);
10,907✔
204
    if (data == NULL){
10,907!
205
      continue;
×
206
    }
207
    mInfo("heartbeat report offset rows.%s:%s", pConsumer->cgroup, data->topicName);
10,907!
208

209
    SMqSubscribeObj *pSub = NULL;
10,907✔
210
    char  key[TSDB_SUBSCRIBE_KEY_LEN] = {0};
10,907✔
211
    (void)snprintf(key, TSDB_SUBSCRIBE_KEY_LEN, "%s%s%s", pConsumer->cgroup, TMQ_SEPARATOR, data->topicName);
10,907!
212
    int32_t code = mndAcquireSubscribeByKey(pMnode, key, &pSub);
10,907✔
213
    if (code != 0) {
10,907!
214
      mError("failed to acquire subscribe by key:%s, code:%d", key, code);
×
215
      continue;
×
216
    }
217
    taosWLockLatch(&pSub->lock);
10,907✔
218
    SMqConsumerEp *pConsumerEp = taosHashGet(pSub->consumerHash, &pConsumer->consumerId, sizeof(int64_t));
10,907✔
219
    if (pConsumerEp) {
10,907✔
220
      taosArrayDestroy(pConsumerEp->offsetRows);
10,327✔
221
      pConsumerEp->offsetRows = data->offsetRows;
10,327✔
222
      data->offsetRows = NULL;
10,327✔
223
    }
224
    taosWUnLockLatch(&pSub->lock);
10,907✔
225

226
    mndReleaseSubscribe(pMnode, pSub);
10,907✔
227
  }
228
}
229

230
static int32_t buildMqHbRsp(SRpcMsg *pMsg, SMqHbRsp *rsp){
11,474✔
231
  if (pMsg == NULL || rsp == NULL){
11,474!
232
    return TSDB_CODE_INVALID_PARA;
×
233
  }
234
  int32_t tlen = tSerializeSMqHbRsp(NULL, 0, rsp);
11,474✔
235
  if (tlen <= 0){
11,474!
236
    return TSDB_CODE_TMQ_INVALID_MSG;
×
237
  }
238
  void   *buf = rpcMallocCont(tlen);
11,474✔
239
  if (buf == NULL) {
11,474!
240
    return terrno;
×
241
  }
242

243
  if(tSerializeSMqHbRsp(buf, tlen, rsp) <= 0){
11,474!
244
    rpcFreeCont(buf);
×
245
    return TSDB_CODE_TMQ_INVALID_MSG;
×
246
  }
247
  pMsg->info.rsp = buf;
11,474✔
248
  pMsg->info.rspLen = tlen;
11,474✔
249
  return 0;
11,474✔
250
}
251

252
static int32_t mndProcessMqHbReq(SRpcMsg *pMsg) {
12,888✔
253
  if (pMsg == NULL) {
12,888!
254
    return TSDB_CODE_INVALID_PARA;
×
255
  }
256
  PRINT_LOG_START
12,888!
257
  int32_t         code = 0;
12,888✔
258
  SMnode         *pMnode = pMsg->info.node;
12,888✔
259
  SMqHbReq        req = {0};
12,888✔
260
  SMqHbRsp        rsp = {0};
12,888✔
261
  SMqConsumerObj *pConsumer = NULL;
12,888✔
262
  MND_TMQ_RETURN_CHECK(tDeserializeSMqHbReq(pMsg->pCont, pMsg->contLen, &req));
12,888!
263
  int64_t consumerId = req.consumerId;
12,888✔
264
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
12,888✔
265
  MND_TMQ_RETURN_CHECK(checkPrivilege(pMnode, pConsumer, &rsp, pMsg->info.conn.user));
11,474!
266
  atomic_store_32(&pConsumer->hbStatus, 0);
11,474✔
267
  mDebug("consumer:0x%" PRIx64 " receive hb pollFlag:%d pollStatus:%d", consumerId, req.pollFlag, pConsumer->pollStatus);
11,474!
268
  if (req.pollFlag == 1){
11,474✔
269
    atomic_store_32(&pConsumer->pollStatus, 0);
7,660✔
270
    pConsumer->pollTime = taosGetTimestampMs();
15,320✔
271
  }
272

273
  storeOffsetRows(pMnode, &req, pConsumer);
11,474✔
274
  rsp.debugFlag = tqClientDebugFlag;
11,474✔
275
  code = buildMqHbRsp(pMsg, &rsp);
11,474✔
276

277
END:
12,888✔
278
  tDestroySMqHbRsp(&rsp);
12,888✔
279
  mndReleaseConsumer(pMnode, pConsumer);
12,888✔
280
  tDestroySMqHbReq(&req);
12,888✔
281
  PRINT_LOG_END(code)
12,888!
282
  return code;
12,888✔
283
}
284

285
static int32_t addEpSetInfo(SMnode *pMnode, SMqConsumerObj *pConsumer, int32_t epoch, SMqAskEpRsp *rsp){
4,279✔
286
  if (pMnode == NULL || pConsumer == NULL || rsp == NULL){
4,279!
287
    return TSDB_CODE_INVALID_PARA;
×
288
  }
289
  taosRLockLatch(&pConsumer->lock);
4,279✔
290

291
  int32_t numOfTopics = taosArrayGetSize(pConsumer->currentTopics);
4,279✔
292

293
  rsp->topics = taosArrayInit(numOfTopics, sizeof(SMqSubTopicEp));
4,279✔
294
  if (rsp->topics == NULL) {
4,279!
295
    taosRUnLockLatch(&pConsumer->lock);
×
296
    return terrno;
×
297
  }
298

299
  // handle all topics subscribed by this consumer
300
  for (int32_t i = 0; i < numOfTopics; i++) {
7,762✔
301
    char            *topic = taosArrayGetP(pConsumer->currentTopics, i);
3,483✔
302
    SMqSubscribeObj *pSub = NULL;
3,483✔
303
    char  key[TSDB_SUBSCRIBE_KEY_LEN] = {0};
3,483✔
304
    (void)snprintf(key, TSDB_SUBSCRIBE_KEY_LEN, "%s%s%s", pConsumer->cgroup, TMQ_SEPARATOR, topic);
3,483✔
305
    int32_t code = mndAcquireSubscribeByKey(pMnode, key, &pSub);
3,483✔
306
    if (code != 0) {
3,483!
307
      continue;
×
308
    }
309
    taosRLockLatch(&pSub->lock);
3,483✔
310

311
    SMqSubTopicEp topicEp = {0};
3,483✔
312
    tstrncpy(topicEp.topic, topic, TSDB_TOPIC_FNAME_LEN);
3,483!
313

314
    // 2.1 fetch topic schema
315
    SMqTopicObj *pTopic = NULL;
3,483✔
316
    code = mndAcquireTopic(pMnode, topic, &pTopic);
3,483✔
317
    if (code != TSDB_CODE_SUCCESS) {
3,483!
318
      taosRUnLockLatch(&pSub->lock);
×
319
      mndReleaseSubscribe(pMnode, pSub);
×
320
      continue;
×
321
    }
322
    taosRLockLatch(&pTopic->lock);
3,483✔
323
    tstrncpy(topicEp.db, pTopic->db, TSDB_DB_FNAME_LEN);
3,483!
324
    topicEp.schema.nCols = pTopic->schema.nCols;
3,483✔
325
    if (topicEp.schema.nCols) {
3,483✔
326
      topicEp.schema.pSchema = taosMemoryCalloc(topicEp.schema.nCols, sizeof(SSchema));
3,052!
327
      if (topicEp.schema.pSchema == NULL) {
3,052!
328
        taosRUnLockLatch(&pTopic->lock);
×
329
        taosRUnLockLatch(&pSub->lock);
×
330
        mndReleaseSubscribe(pMnode, pSub);
×
331
        mndReleaseTopic(pMnode, pTopic);
×
332
        return terrno;
×
333
      }
334
      (void)memcpy(topicEp.schema.pSchema, pTopic->schema.pSchema, topicEp.schema.nCols * sizeof(SSchema));
3,052!
335
    }
336
    taosRUnLockLatch(&pTopic->lock);
3,483✔
337
    mndReleaseTopic(pMnode, pTopic);
3,483✔
338

339
    // 2.2 iterate all vg assigned to the consumer of that topic
340
    SMqConsumerEp *pConsumerEp = taosHashGet(pSub->consumerHash, &pConsumer->consumerId, sizeof(int64_t));
3,483✔
341
    if (pConsumerEp == NULL) {
3,483!
342
      taosMemoryFreeClear(topicEp.schema.pSchema);
×
343
      taosRUnLockLatch(&pConsumer->lock);
×
344
      taosRUnLockLatch(&pSub->lock);
×
345
      mndReleaseSubscribe(pMnode, pSub);
×
346
      return TSDB_CODE_OUT_OF_MEMORY;
×
347
    }
348
    int32_t vgNum = taosArrayGetSize(pConsumerEp->vgs);
3,483✔
349
    topicEp.vgs = taosArrayInit(vgNum, sizeof(SMqSubVgEp));
3,483✔
350
    if (topicEp.vgs == NULL) {
3,483!
351
      taosMemoryFreeClear(topicEp.schema.pSchema);
×
352
      taosRUnLockLatch(&pConsumer->lock);
×
353
      taosRUnLockLatch(&pSub->lock);
×
354
      mndReleaseSubscribe(pMnode, pSub);
×
355
      return terrno;
×
356
    }
357

358
    for (int32_t j = 0; j < vgNum; j++) {
20,463✔
359
      SMqVgEp *pVgEp = taosArrayGet(pConsumerEp->vgs, j);
16,980✔
360
      if (pVgEp == NULL) {
16,980!
361
        continue;
×
362
      }
363
      if (epoch == -1) {
16,980!
364
        SVgObj *pVgroup = mndAcquireVgroup(pMnode, pVgEp->vgId);
×
365
        if (pVgroup) {
×
366
          pVgEp->epSet = mndGetVgroupEpset(pMnode, pVgroup);
×
367
          mndReleaseVgroup(pMnode, pVgroup);
×
368
        }
369
      }
370
      SMqSubVgEp vgEp = {.epSet = pVgEp->epSet, .vgId = pVgEp->vgId, .offset = -1};
16,980✔
371
      if (taosArrayPush(topicEp.vgs, &vgEp) == NULL) {
33,960!
372
        taosMemoryFreeClear(topicEp.schema.pSchema);
×
373
        taosArrayDestroy(topicEp.vgs);
×
374
        taosRUnLockLatch(&pConsumer->lock);
×
375
        taosRUnLockLatch(&pSub->lock);
×
376
        mndReleaseSubscribe(pMnode, pSub);
×
377
        return terrno;
×
378
      }
379
    }
380
    if (taosArrayPush(rsp->topics, &topicEp) == NULL) {
6,966!
381
      taosMemoryFreeClear(topicEp.schema.pSchema);
×
382
      taosArrayDestroy(topicEp.vgs);
×
383
      taosRUnLockLatch(&pConsumer->lock);
×
384
      taosRUnLockLatch(&pSub->lock);
×
385
      mndReleaseSubscribe(pMnode, pSub);
×
386
      return terrno;
×
387
    }
388
    taosRUnLockLatch(&pSub->lock);
3,483✔
389
    mndReleaseSubscribe(pMnode, pSub);
3,483✔
390
  }
391
  taosRUnLockLatch(&pConsumer->lock);
4,279✔
392
  return 0;
4,279✔
393
}
394

395
static int32_t buildAskEpRsp(SRpcMsg *pMsg, SMqAskEpRsp *rsp, int32_t serverEpoch, int64_t consumerId){
22,476✔
396
  if (pMsg == NULL || rsp == NULL) {
22,476!
397
    return TSDB_CODE_INVALID_PARA;
×
398
  }
399
  int32_t code = 0;
22,476✔
400
  // encode rsp
401
  int32_t tlen = sizeof(SMqRspHead) + tEncodeSMqAskEpRsp(NULL, rsp);
22,476✔
402
  void   *buf = rpcMallocCont(tlen);
22,476✔
403
  if (buf == NULL) {
22,476!
404
    return terrno;
×
405
  }
406

407
  SMqRspHead *pHead = buf;
22,476✔
408

409
  pHead->mqMsgType = TMQ_MSG_TYPE__EP_RSP;
22,476✔
410
  pHead->epoch = serverEpoch;
22,476✔
411
  pHead->consumerId = consumerId;
22,476✔
412
  pHead->walsver = 0;
22,476✔
413
  pHead->walever = 0;
22,476✔
414

415
  void *abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
22,476✔
416
  if (tEncodeSMqAskEpRsp(&abuf, rsp) < 0) {
22,476!
417
    rpcFreeCont(buf);
×
418
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
419
  }
420

421
  // send rsp
422
  pMsg->info.rsp = buf;
22,476✔
423
  pMsg->info.rspLen = tlen;
22,476✔
424
  return code;
22,476✔
425
}
426

427
static int32_t mndProcessAskEpReq(SRpcMsg *pMsg) {
40,719✔
428
  if (pMsg == NULL) {
40,719!
429
    return TSDB_CODE_INVALID_PARA;
×
430
  }
431
  SMnode     *pMnode = pMsg->info.node;
40,719✔
432
  SMqAskEpReq req = {0};
40,719✔
433
  SMqAskEpRsp rsp = {0};
40,719✔
434
  int32_t     code = 0;
40,719✔
435
  SMqConsumerObj *pConsumer = NULL;
40,719✔
436
  PRINT_LOG_START
40,719!
437

438
  MND_TMQ_RETURN_CHECK(tDeserializeSMqAskEpReq(pMsg->pCont, pMsg->contLen, &req));
40,719!
439
  int64_t consumerId = req.consumerId;
40,719✔
440
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
40,719✔
441
  if (strncmp(req.cgroup, pConsumer->cgroup, tListLen(pConsumer->cgroup)) != 0) {
40,113!
442
    mError("consumer:0x%" PRIx64 " group:%s not consistent with data in sdb, saved cgroup:%s", consumerId, req.cgroup,
×
443
           pConsumer->cgroup);
444
    code = TSDB_CODE_MND_CONSUMER_NOT_EXIST;
×
445
    goto END;
×
446
  }
447

448
  // 1. check consumer status
449
  int32_t status = atomic_load_32(&pConsumer->status);
40,113✔
450
  if (status != MQ_CONSUMER_STATUS_READY) {
40,113✔
451
    mInfo("consumer:0x%" PRIx64 " not ready, status: %s", consumerId, mndConsumerStatusName(status));
17,637!
452
    code = TSDB_CODE_MND_CONSUMER_NOT_READY;
17,637✔
453
    goto END;
17,637✔
454
  }
455

456
  int32_t epoch = req.epoch;
22,476✔
457
  int32_t serverEpoch = atomic_load_32(&pConsumer->epoch);
22,476✔
458

459
  // 2. check epoch, only send ep info when epochs do not match
460
  if (epoch != serverEpoch) {
22,476✔
461
    mInfo("process ask ep, consumer:0x%" PRIx64 "(epoch %d) update with server epoch %d",
4,279!
462
          consumerId, epoch, serverEpoch);
463
    MND_TMQ_RETURN_CHECK(addEpSetInfo(pMnode, pConsumer, epoch, &rsp));
4,279!
464
  }
465

466
  code = buildAskEpRsp(pMsg, &rsp, serverEpoch, consumerId);
22,476✔
467

468
END:
40,719✔
469
  tDeleteSMqAskEpRsp(&rsp);
470
  mndReleaseConsumer(pMnode, pConsumer);
40,719✔
471
  PRINT_LOG_END(code);
40,719!
472
  return code;
40,719✔
473
}
474

475
int32_t mndSetConsumerDropLogs(STrans *pTrans, SMqConsumerObj *pConsumer) {
2,685✔
476
  if (pConsumer == NULL || pTrans == NULL) {
2,685!
477
    return TSDB_CODE_INVALID_PARA;
×
478
  }
479
  int32_t  code = 0;
2,685✔
480
  SSdbRaw *pCommitRaw = mndConsumerActionEncode(pConsumer);
2,685✔
481
  MND_TMQ_NULL_CHECK(pCommitRaw);
2,685!
482
  code = mndTransAppendCommitlog(pTrans, pCommitRaw);
2,685✔
483
  if (code != 0) {
2,685!
484
    sdbFreeRaw(pCommitRaw);
×
485
    goto END;
×
486
  }
487
  MND_TMQ_RETURN_CHECK(sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED));
2,685!
488
END:
2,685✔
489
  return code;
2,685✔
490
}
491

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

509
static void freeItem(void *param) {
×
510
  if (param == NULL) {
×
511
    return;
×
512
  }
513
  void *pItem = *(void **)param;
×
514
  if (pItem != NULL) {
×
515
    taosMemoryFree(pItem);
×
516
  }
517
}
518

519
#define ADD_TOPIC_TO_ARRAY(element, array) \
520
char *newTopicCopy = taosStrdup(element); \
521
MND_TMQ_NULL_CHECK(newTopicCopy);\
522
if (taosArrayPush(pConsumerNew->array, &newTopicCopy) == NULL){\
523
  taosMemoryFree(newTopicCopy);\
524
  code = terrno;\
525
  goto END;\
526
}
527

528
static int32_t getTopicAddDelete(SMqConsumerObj *pExistedConsumer, SMqConsumerObj *pConsumerNew){
2,877✔
529
  if (pExistedConsumer == NULL || pConsumerNew == NULL) {
2,877!
530
    return TSDB_CODE_INVALID_PARA;
×
531
  }
532
  int32_t code = 0;
2,877✔
533
  pConsumerNew->rebNewTopics = taosArrayInit(0, sizeof(void *));
2,877✔
534
  MND_TMQ_NULL_CHECK(pConsumerNew->rebNewTopics);
2,877!
535
  pConsumerNew->rebRemovedTopics = taosArrayInit(0, sizeof(void *));
2,877✔
536
  MND_TMQ_NULL_CHECK(pConsumerNew->rebRemovedTopics);
2,877!
537

538
  int32_t newTopicNum = taosArrayGetSize(pConsumerNew->assignedTopics);
2,877✔
539
  int32_t oldTopicNum = taosArrayGetSize(pExistedConsumer->currentTopics);
2,877✔
540
  int32_t i = 0, j = 0;
2,877✔
541
  while (i < oldTopicNum || j < newTopicNum) {
5,754!
542
    if (i >= oldTopicNum) {
2,877!
543
      void* tmp = taosArrayGetP(pConsumerNew->assignedTopics, j);
×
544
      MND_TMQ_NULL_CHECK(tmp);
×
545
      ADD_TOPIC_TO_ARRAY(tmp, rebNewTopics);
×
546
      j++;
×
547
      continue;
×
548
    } else if (j >= newTopicNum) {
2,877!
549
      void* tmp = taosArrayGetP(pExistedConsumer->currentTopics, i);
2,877✔
550
      MND_TMQ_NULL_CHECK(tmp);
2,877!
551
      ADD_TOPIC_TO_ARRAY(tmp, rebRemovedTopics);
5,754!
552
      i++;
2,877✔
553
      continue;
2,877✔
554
    } else {
555
      char *oldTopic = taosArrayGetP(pExistedConsumer->currentTopics, i);
×
556
      MND_TMQ_NULL_CHECK(oldTopic);
×
557
      char *newTopic = taosArrayGetP(pConsumerNew->assignedTopics, j);
×
558
      MND_TMQ_NULL_CHECK(newTopic);
×
559
      int   comp = strcmp(oldTopic, newTopic);
×
560
      if (comp == 0) {
×
561
        i++;
×
562
        j++;
×
563
        continue;
×
564
      } else if (comp < 0) {
×
565
        ADD_TOPIC_TO_ARRAY(oldTopic, rebRemovedTopics);
×
566
        i++;
×
567
        continue;
×
568
      } else {
×
569
        ADD_TOPIC_TO_ARRAY(newTopic, rebNewTopics);
×
570
        j++;
×
571
        continue;
×
572
      }
573
    }
574
  }
575
  // no topics need to be rebalanced
576
  if (taosArrayGetSize(pConsumerNew->rebNewTopics) == 0 && taosArrayGetSize(pConsumerNew->rebRemovedTopics) == 0) {
2,877!
577
    code = TSDB_CODE_TMQ_NO_NEED_REBALANCE;
×
578
  }
579

580
END:
2,877✔
581
  return code;
2,877✔
582
}
583

584
static int32_t checkAndSortTopic(SMnode *pMnode, SArray *pTopicList){
6,360✔
585
  if (pTopicList == NULL || pMnode == NULL) {
6,360!
586
    return TSDB_CODE_INVALID_PARA;
×
587
  }
588
  taosArraySort(pTopicList, taosArrayCompareString);
6,360✔
589
  taosArrayRemoveDuplicate(pTopicList, taosArrayCompareString, freeItem);
6,360✔
590

591
  int32_t newTopicNum = taosArrayGetSize(pTopicList);
6,360✔
592
  for (int i = 0; i < newTopicNum; i++) {
9,843✔
593
    int32_t gNum = mndGetGroupNumByTopic(pMnode, (const char *)taosArrayGetP(pTopicList, i));
3,483✔
594
    if (gNum >= MND_MAX_GROUP_PER_TOPIC) {
3,483!
595
      return TSDB_CODE_TMQ_GROUP_OUT_OF_RANGE;
×
596
    }
597
  }
598
  return 0;
6,360✔
599
}
600

601
static int32_t buildSubConsumer(SMnode *pMnode, SCMSubscribeReq *subscribe, SMqConsumerObj** ppConsumer){
6,360✔
602
  if (pMnode == NULL || subscribe == NULL) {
6,360!
603
    return TSDB_CODE_INVALID_PARA;
×
604
  }
605
  int64_t         consumerId = subscribe->consumerId;
6,360✔
606
  char           *cgroup     = subscribe->cgroup;
6,360✔
607
  SMqConsumerObj *pConsumerNew     = NULL;
6,360✔
608
  SMqConsumerObj *pExistedConsumer = NULL;
6,360✔
609
  int32_t code = mndAcquireConsumer(pMnode, consumerId, &pExistedConsumer);
6,360✔
610
  if (code != 0) {
6,360✔
611
    mInfo("receive tmq subscribe request from new consumer:0x%" PRIx64
3,483!
612
              ",cgroup:%s, numOfTopics:%d", consumerId,
613
          subscribe->cgroup, (int32_t)taosArrayGetSize(subscribe->topicNames));
614

615
    MND_TMQ_RETURN_CHECK(tNewSMqConsumerObj(consumerId, cgroup, CONSUMER_INSERT_SUB, NULL, subscribe, &pConsumerNew));
3,483!
616
  } else {
617
    int32_t status = atomic_load_32(&pExistedConsumer->status);
2,877✔
618

619
    mInfo("receive tmq subscribe request from existed consumer:0x%" PRIx64
2,877!
620
              ",cgroup:%s, current status:%d(%s), subscribe topic num: %d",
621
          consumerId, subscribe->cgroup, status, mndConsumerStatusName(status),
622
          (int32_t)taosArrayGetSize(subscribe->topicNames));
623

624
    if (status != MQ_CONSUMER_STATUS_READY) {
2,877!
625
      code = TSDB_CODE_MND_CONSUMER_NOT_READY;
×
626
      goto END;
×
627
    }
628
    MND_TMQ_RETURN_CHECK(tNewSMqConsumerObj(consumerId, cgroup, CONSUMER_UPDATE_SUB, NULL, subscribe, &pConsumerNew));
2,877!
629
    MND_TMQ_RETURN_CHECK(getTopicAddDelete(pExistedConsumer, pConsumerNew));
2,877!
630
  }
631
  mndReleaseConsumer(pMnode, pExistedConsumer);
6,360✔
632
  if (ppConsumer){
6,360!
633
    *ppConsumer = pConsumerNew;
6,360✔
634
  }
635
  return code;
6,360✔
636

637
END:
×
638
  mndReleaseConsumer(pMnode, pExistedConsumer);
×
639
  tDeleteSMqConsumerObj(pConsumerNew);
×
640
  return code;
×
641
}
642

643
int32_t mndProcessSubscribeReq(SRpcMsg *pMsg) {
7,533✔
644
  if (pMsg == NULL) {
7,533!
645
    return TSDB_CODE_INVALID_PARA;
×
646
  }
647
  SMnode *pMnode = pMsg->info.node;
7,533✔
648
  char   *msgStr = pMsg->pCont;
7,533✔
649
  int32_t code = 0;
7,533✔
650
  SMqConsumerObj *pConsumerNew = NULL;
7,533✔
651
  STrans         *pTrans = NULL;
7,533✔
652

653
  PRINT_LOG_START
7,533!
654
  SCMSubscribeReq subscribe = {0};
7,533✔
655
  MND_TMQ_RETURN_CHECK(tDeserializeSCMSubscribeReq(msgStr, &subscribe, pMsg->contLen));
15,066!
656
  bool unSubscribe = (taosArrayGetSize(subscribe.topicNames) == 0);
7,533✔
657
  if(unSubscribe){
7,533✔
658
    SMqConsumerObj *pConsumerTmp = NULL;
4,050✔
659
    MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, subscribe.consumerId, &pConsumerTmp));
4,050✔
660
    if (taosArrayGetSize(pConsumerTmp->assignedTopics) == 0){
3,444✔
661
      mndReleaseConsumer(pMnode, pConsumerTmp);
567✔
662
      goto END;
567✔
663
    }
664
    mndReleaseConsumer(pMnode, pConsumerTmp);
2,877✔
665
  }
666
  MND_TMQ_RETURN_CHECK(checkAndSortTopic(pMnode, subscribe.topicNames));
6,360!
667
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY,
6,360✔
668
                          (unSubscribe ? TRN_CONFLICT_NOTHING :TRN_CONFLICT_DB_INSIDE),
669
                          pMsg, "subscribe");
670
  MND_TMQ_NULL_CHECK(pTrans);
6,360!
671

672
  MND_TMQ_RETURN_CHECK(validateTopics(pTrans, &subscribe, pMnode, pMsg->info.conn.user));
6,360!
673
  MND_TMQ_RETURN_CHECK(buildSubConsumer(pMnode, &subscribe, &pConsumerNew));
6,360!
674
  MND_TMQ_RETURN_CHECK(mndSetConsumerCommitLogs(pTrans, pConsumerNew));
6,360!
675
  MND_TMQ_RETURN_CHECK(mndTransPrepare(pMnode, pTrans));
6,360!
676
  code = TSDB_CODE_ACTION_IN_PROGRESS;
6,360✔
677

678
END:
7,533✔
679
  mndTransDrop(pTrans);
7,533✔
680
  tDeleteSMqConsumerObj(pConsumerNew);
7,533✔
681
  taosArrayDestroyP(subscribe.topicNames, NULL);
7,533✔
682
  code = (code == TSDB_CODE_TMQ_NO_NEED_REBALANCE || code == TSDB_CODE_MND_CONSUMER_NOT_EXIST) ? 0 : code;
7,533!
683
  if (code != TSDB_CODE_ACTION_IN_PROGRESS){
7,533✔
684
    PRINT_LOG_END(code);
1,173!
685
  }
686
  return code;
7,533✔
687
}
688

689
SSdbRaw *mndConsumerActionEncode(SMqConsumerObj *pConsumer) {
16,607✔
690
  if (pConsumer == NULL) {
16,607!
691
    return NULL;
×
692
  }
693
  int32_t code = 0;
16,607✔
694
  int32_t lino = 0;
16,607✔
695
  terrno = TSDB_CODE_OUT_OF_MEMORY;
16,607✔
696

697
  void   *buf = NULL;
16,607✔
698
  int32_t tlen = tEncodeSMqConsumerObj(NULL, pConsumer);
16,607✔
699
  int32_t size = sizeof(int32_t) + tlen + MND_CONSUMER_RESERVE_SIZE;
16,607✔
700

701
  SSdbRaw *pRaw = sdbAllocRaw(SDB_CONSUMER, MND_CONSUMER_VER_NUMBER, size);
16,607✔
702
  if (pRaw == NULL) goto CM_ENCODE_OVER;
16,607!
703

704
  buf = taosMemoryMalloc(tlen);
16,607!
705
  if (buf == NULL) goto CM_ENCODE_OVER;
16,607!
706

707
  void *abuf = buf;
16,607✔
708
  if(tEncodeSMqConsumerObj(&abuf, pConsumer) < 0){
16,607!
709
    goto CM_ENCODE_OVER;
×
710
  }
711

712
  int32_t dataPos = 0;
16,607✔
713
  SDB_SET_INT32(pRaw, dataPos, tlen, CM_ENCODE_OVER);
16,607!
714
  SDB_SET_BINARY(pRaw, dataPos, buf, tlen, CM_ENCODE_OVER);
16,607!
715
  SDB_SET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_ENCODE_OVER);
16,607!
716
  SDB_SET_DATALEN(pRaw, dataPos, CM_ENCODE_OVER);
16,607!
717

718
  terrno = TSDB_CODE_SUCCESS;
16,607✔
719

720
CM_ENCODE_OVER:
16,607✔
721
  taosMemoryFreeClear(buf);
16,607!
722
  if (terrno != 0) {
16,607!
723
    mError("consumer:0x%" PRIx64 " failed to encode to raw:%p since %s", pConsumer->consumerId, pRaw, terrstr());
×
724
    sdbFreeRaw(pRaw);
×
725
    return NULL;
×
726
  }
727

728
  mTrace("consumer:0x%" PRIx64 ", encode to raw:%p, row:%p", pConsumer->consumerId, pRaw, pConsumer);
16,607!
729
  return pRaw;
16,607✔
730
}
731

732
SSdbRow *mndConsumerActionDecode(SSdbRaw *pRaw) {
15,618✔
733
  if (pRaw == NULL) {
15,618!
734
    return NULL;
×
735
  }
736
  int32_t         code = 0;
15,618✔
737
  int32_t         lino = 0;
15,618✔
738
  SSdbRow        *pRow = NULL;
15,618✔
739
  SMqConsumerObj *pConsumer = NULL;
15,618✔
740
  void           *buf = NULL;
15,618✔
741

742
  terrno = 0;
15,618✔
743
  int8_t sver = 0;
15,618✔
744
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
15,618!
745
    goto CM_DECODE_OVER;
×
746
  }
747

748
  if (sver < 1 || sver > MND_CONSUMER_VER_NUMBER) {
15,618!
749
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
750
    goto CM_DECODE_OVER;
×
751
  }
752

753
  pRow = sdbAllocRow(sizeof(SMqConsumerObj));
15,618✔
754
  if (pRow == NULL) {
15,618!
755
    goto CM_DECODE_OVER;
×
756
  }
757

758
  pConsumer = sdbGetRowObj(pRow);
15,618✔
759
  if (pConsumer == NULL) {
15,618!
760
    goto CM_DECODE_OVER;
×
761
  }
762

763
  int32_t dataPos = 0;
15,618✔
764
  int32_t len;
15,618✔
765
  SDB_GET_INT32(pRaw, dataPos, &len, CM_DECODE_OVER);
15,618!
766
  buf = taosMemoryMalloc(len);
15,618!
767
  if (buf == NULL) {
15,618!
768
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
769
    goto CM_DECODE_OVER;
×
770
  }
771

772
  SDB_GET_BINARY(pRaw, dataPos, buf, len, CM_DECODE_OVER);
15,618!
773
  SDB_GET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_DECODE_OVER);
15,618!
774

775
  if (tDecodeSMqConsumerObj(buf, pConsumer, sver) == NULL) {
15,618!
776
    terrno = TSDB_CODE_OUT_OF_MEMORY;  // TODO set correct error code
×
777
    goto CM_DECODE_OVER;
×
778
  }
779

780
  tmsgUpdateDnodeEpSet(&pConsumer->ep);
15,618✔
781

782
CM_DECODE_OVER:
15,618✔
783
  taosMemoryFreeClear(buf);
15,618!
784
  if (terrno != TSDB_CODE_SUCCESS) {
15,618!
785
    mError("consumer:0x%" PRIx64 " failed to decode from raw:%p since %s",
×
786
           pConsumer == NULL ? 0 : pConsumer->consumerId, pRaw, terrstr());
787
    taosMemoryFreeClear(pRow);
×
788
  }
789

790
  return pRow;
15,618✔
791
}
792

793
static int32_t mndConsumerActionInsert(SSdb *pSdb, SMqConsumerObj *pConsumer) {
3,483✔
794
  if (pConsumer == NULL) {
3,483!
795
    return TSDB_CODE_INVALID_PARA;
×
796
  }
797
  mInfo("consumer:0x%" PRIx64 " sub insert, cgroup:%s status:%d(%s) epoch:%d", pConsumer->consumerId, pConsumer->cgroup,
3,483!
798
        pConsumer->status, mndConsumerStatusName(pConsumer->status), pConsumer->epoch);
799
  pConsumer->subscribeTime = pConsumer->createTime;
3,483✔
800
  return 0;
3,483✔
801
}
802

803
static int32_t mndConsumerActionDelete(SSdb *pSdb, SMqConsumerObj *pConsumer) {
15,618✔
804
  if (pConsumer == NULL) {
15,618!
805
    return TSDB_CODE_INVALID_PARA;
×
806
  }
807
  mInfo("consumer:0x%" PRIx64 " perform delete action, status:(%d)%s", pConsumer->consumerId, pConsumer->status,
15,618!
808
        mndConsumerStatusName(pConsumer->status));
809
  tClearSMqConsumerObj(pConsumer);
15,618✔
810
  return 0;
15,618✔
811
}
812

813
//static void updateConsumerStatus(SMqConsumerObj *pConsumer) {
814
//  int32_t status = pConsumer->status;
815
//
816
//  if (taosArrayGetSize(pConsumer->rebNewTopics) == 0 && taosArrayGetSize(pConsumer->rebRemovedTopics) == 0) {
817
//    if (status == MQ_CONSUMER_STATUS_REBALANCE) {
818
//      pConsumer->status = MQ_CONSUMER_STATUS_READY;
819
//    } else if (status == MQ_CONSUMER_STATUS_READY && taosArrayGetSize(pConsumer->currentTopics) == 0) {
820
//      pConsumer->status = MQ_CONSUMER_STATUS_LOST;
821
//    }
822
//  }
823
//}
824

825
// remove from topic list
826
static void removeFromTopicList(SArray *topicList, const char *pTopic, int64_t consumerId, char *type) {
9,663✔
827
  if (topicList == NULL || pTopic == NULL) {
9,663!
828
    return;
×
829
  }
830
  int32_t size = taosArrayGetSize(topicList);
9,663✔
831
  for (int32_t i = 0; i < size; i++) {
9,663✔
832
    char *p = taosArrayGetP(topicList, i);
9,259✔
833
    if (strcmp(pTopic, p) == 0) {
9,259!
834
      taosArrayRemove(topicList, i);
9,259✔
835
      taosMemoryFree(p);
9,259!
836

837
      mInfo("tmq rebalance consumer:0x%" PRIx64 " remove topic:%s in the %s topic list, remain newTopics:%d",
9,259!
838
            consumerId, pTopic, type, (int)taosArrayGetSize(topicList));
839
      break;
9,259✔
840
    }
841
  }
842
}
843

844
static bool existInCurrentTopicList(const SMqConsumerObj *pConsumer, const char *pTopic) {
3,483✔
845
  if (pConsumer == NULL || pTopic == NULL) {
3,483!
846
    return false;
×
847
  }
848
  bool    existing = false;
3,483✔
849
  int32_t size = taosArrayGetSize(pConsumer->currentTopics);
3,483✔
850
  for (int32_t i = 0; i < size; i++) {
3,483!
851
    char *topic = taosArrayGetP(pConsumer->currentTopics, i);
×
852
    if (topic && strcmp(topic, pTopic) == 0) {
×
853
      existing = true;
×
854
      break;
×
855
    }
856
  }
857

858
  return existing;
3,483✔
859
}
860

861
static int32_t mndConsumerActionUpdate(SSdb *pSdb, SMqConsumerObj *pOldConsumer, SMqConsumerObj *pNewConsumer) {
9,450✔
862
  if (pOldConsumer == NULL || pNewConsumer == NULL) {
9,450!
863
    return TSDB_CODE_INVALID_PARA;
×
864
  }
865
  mInfo("consumer:0x%" PRIx64 " perform update action, update type:%d, subscribe-time:%" PRId64 ", createTime:%" PRId64,
9,450!
866
        pOldConsumer->consumerId, pNewConsumer->updateType, pOldConsumer->subscribeTime, pOldConsumer->createTime);
867

868
  taosWLockLatch(&pOldConsumer->lock);
9,450✔
869

870
  if (pNewConsumer->updateType == CONSUMER_UPDATE_SUB) {
9,450✔
871
    TSWAP(pOldConsumer->rebNewTopics, pNewConsumer->rebNewTopics);
2,877✔
872
    TSWAP(pOldConsumer->rebRemovedTopics, pNewConsumer->rebRemovedTopics);
2,877✔
873
    TSWAP(pOldConsumer->assignedTopics, pNewConsumer->assignedTopics);
2,877✔
874

875
    pOldConsumer->subscribeTime = taosGetTimestampMs();
2,877✔
876
    pOldConsumer->status = MQ_CONSUMER_STATUS_REBALANCE;
2,877✔
877
    mInfo("consumer:0x%" PRIx64 " subscribe update, modify existed consumer", pOldConsumer->consumerId);
2,877!
878
  } else if (pNewConsumer->updateType == CONSUMER_UPDATE_REB) {
6,573!
879
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
×
880
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
×
881
    mInfo("tmq rebalance consumer:0x%" PRIx64 " rebalance update, only rebalance time", pOldConsumer->consumerId);
×
882
  } else if (pNewConsumer->updateType == CONSUMER_ADD_REB) {
6,573✔
883
    void *tmp = taosArrayGetP(pNewConsumer->rebNewTopics, 0);
3,483✔
884
    if (tmp == NULL){
3,483!
885
      return TSDB_CODE_TMQ_INVALID_MSG;
×
886
    }
887
    char *pNewTopic = taosStrdup(tmp);
3,483!
888
    if (pNewTopic == NULL) {
3,483!
889
      return terrno;
×
890
    }
891
    removeFromTopicList(pOldConsumer->rebNewTopics, pNewTopic, pOldConsumer->consumerId, "new");
3,483✔
892
    bool existing = existInCurrentTopicList(pOldConsumer, pNewTopic);
3,483✔
893
    if (existing) {
3,483!
894
      mError("tmq rebalance consumer:0x%" PRIx64 " add new topic:%s should not in currentTopics", pOldConsumer->consumerId, pNewTopic);
×
895
      taosMemoryFree(pNewTopic);
×
896
    } else {
897
      if (taosArrayPush(pOldConsumer->currentTopics, &pNewTopic) == NULL) {
6,966!
898
        taosMemoryFree(pNewTopic);
×
899
        return TSDB_CODE_TMQ_INVALID_MSG;
×
900
      }
901
      taosArraySort(pOldConsumer->currentTopics, taosArrayCompareString);
3,483✔
902
    }
903

904
    int32_t status = pOldConsumer->status;
3,483✔
905
//    updateConsumerStatus(pOldConsumer);
906
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
3,483!
907
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
3,483✔
908
    }
909

910
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
3,483✔
911
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
3,483✔
912

913
    mInfo("tmq rebalance consumer:0x%" PRIx64 " rebalance update add, state (%d)%s -> (%d)%s, new epoch:%d, reb-time:%" PRId64
3,483!
914
          ", current topics:%d, newTopics:%d, removeTopics:%d",
915
          pOldConsumer->consumerId, status, mndConsumerStatusName(status), pOldConsumer->status,
916
          mndConsumerStatusName(pOldConsumer->status), pOldConsumer->epoch, pOldConsumer->rebalanceTime,
917
          (int)taosArrayGetSize(pOldConsumer->currentTopics), (int)taosArrayGetSize(pOldConsumer->rebNewTopics),
918
          (int)taosArrayGetSize(pOldConsumer->rebRemovedTopics));
919

920
  } else if (pNewConsumer->updateType == CONSUMER_REMOVE_REB) {
3,090!
921
    char *topic = taosArrayGetP(pNewConsumer->rebRemovedTopics, 0);
3,090✔
922
    if (topic == NULL){
3,090!
923
      return TSDB_CODE_TMQ_INVALID_MSG;
×
924
    }
925
    removeFromTopicList(pOldConsumer->rebRemovedTopics, topic, pOldConsumer->consumerId, "remove");
3,090✔
926
    removeFromTopicList(pOldConsumer->currentTopics, topic, pOldConsumer->consumerId, "current");
3,090✔
927

928
    int32_t status = pOldConsumer->status;
3,090✔
929
//    updateConsumerStatus(pOldConsumer);
930
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
3,090!
931
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
3,090✔
932
    }
933
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
3,090✔
934
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
3,090✔
935

936
    mInfo("tmq rebalanceconsumer:0x%" PRIx64 " rebalance update remove, state (%d)%s -> (%d)%s, new epoch:%d, reb-time:%" PRId64
3,090!
937
          ", current topics:%d, newTopics:%d, removeTopics:%d",
938
          pOldConsumer->consumerId, status, mndConsumerStatusName(status), pOldConsumer->status,
939
          mndConsumerStatusName(pOldConsumer->status), pOldConsumer->epoch, pOldConsumer->rebalanceTime,
940
          (int)taosArrayGetSize(pOldConsumer->currentTopics), (int)taosArrayGetSize(pOldConsumer->rebNewTopics),
941
          (int)taosArrayGetSize(pOldConsumer->rebRemovedTopics));
942
  }
943

944
  taosWUnLockLatch(&pOldConsumer->lock);
9,450✔
945
  return 0;
9,450✔
946
}
947

948
int32_t mndAcquireConsumer(SMnode *pMnode, int64_t consumerId, SMqConsumerObj** pConsumer) {
66,500✔
949
  if (pMnode == NULL || pConsumer == NULL) {
66,500!
950
    return TSDB_CODE_INVALID_PARA;
×
951
  }
952
  SSdb           *pSdb = pMnode->pSdb;
66,500✔
953
  *pConsumer = sdbAcquire(pSdb, SDB_CONSUMER, &consumerId);
66,500✔
954
  if (*pConsumer == NULL) {
66,500✔
955
    return TSDB_CODE_MND_CONSUMER_NOT_EXIST;
6,109✔
956
  }
957
  return 0;
60,391✔
958
}
959

960
void mndReleaseConsumer(SMnode *pMnode, SMqConsumerObj *pConsumer) {
3,540,486✔
961
  if (pMnode == NULL || pConsumer == NULL) {
3,540,486!
962
    return;
3,458,102✔
963
  }
964
  SSdb *pSdb = pMnode->pSdb;
82,384✔
965
  sdbRelease(pSdb, pConsumer);
82,384✔
966
}
967

968
static int32_t mndRetrieveConsumer(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
1,867✔
969
  if (pReq == NULL || pShow == NULL || pBlock == NULL) {
1,867!
970
    return TSDB_CODE_INVALID_PARA;
×
971
  }
972
  SMnode         *pMnode = pReq->info.node;
1,867✔
973
  SSdb           *pSdb = pMnode->pSdb;
1,867✔
974
  int32_t         numOfRows = 0;
1,867✔
975
  SMqConsumerObj *pConsumer = NULL;
1,867✔
976
  int32_t         code = 0;
1,867✔
977
  char           *parasStr = NULL;
1,867✔
978
  char           *status = NULL;
1,867✔
979

980
  while (numOfRows < rowsCapacity) {
1,894!
981
    pShow->pIter = sdbFetch(pSdb, SDB_CONSUMER, pShow->pIter, (void **)&pConsumer);
1,894✔
982
    if (pShow->pIter == NULL) {
1,894✔
983
      break;
1,867✔
984
    }
985

986
    if (taosArrayGetSize(pConsumer->assignedTopics) == 0) {
27!
987
      mInfo("showing consumer:0x%" PRIx64 " no assigned topic, skip", pConsumer->consumerId);
27!
988
      sdbRelease(pSdb, pConsumer);
27✔
989
      continue;
27✔
990
    }
991

992
    taosRLockLatch(&pConsumer->lock);
×
993
    mInfo("showing consumer:0x%" PRIx64, pConsumer->consumerId);
×
994

995
    int32_t topicSz = taosArrayGetSize(pConsumer->assignedTopics);
×
996
    bool    hasTopic = true;
×
997
    if (topicSz == 0) {
×
998
      hasTopic = false;
×
999
      topicSz = 1;
×
1000
    }
1001

1002
    if (numOfRows + topicSz > rowsCapacity) {
×
1003
      MND_TMQ_RETURN_CHECK(blockDataEnsureCapacity(pBlock, numOfRows + topicSz));
×
1004
    }
1005

1006
    for (int32_t i = 0; i < topicSz; i++) {
×
1007
      SColumnInfoData *pColInfo = NULL;
×
1008
      int32_t          cols = 0;
×
1009

1010
      // consumer id
1011
      char consumerIdHex[TSDB_CONSUMER_ID_LEN + VARSTR_HEADER_SIZE] = {0};
×
1012
      (void)snprintf(varDataVal(consumerIdHex), TSDB_CONSUMER_ID_LEN, "0x%" PRIx64, pConsumer->consumerId);
×
1013
      varDataSetLen(consumerIdHex, strlen(varDataVal(consumerIdHex)));
×
1014

1015
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1016
      MND_TMQ_NULL_CHECK(pColInfo);
×
1017
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)consumerIdHex, false));
×
1018

1019
      // consumer group
1020
      char cgroup[TSDB_CGROUP_LEN + VARSTR_HEADER_SIZE] = {0};
×
1021
      STR_TO_VARSTR(cgroup, pConsumer->cgroup);
×
1022

1023
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1024
      MND_TMQ_NULL_CHECK(pColInfo);
×
1025
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)cgroup, false));
×
1026

1027
      // client id
1028
      char clientId[TSDB_CLIENT_ID_LEN + VARSTR_HEADER_SIZE] = {0};
×
1029
      STR_TO_VARSTR(clientId, pConsumer->clientId);
×
1030

1031
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1032
      MND_TMQ_NULL_CHECK(pColInfo);
×
1033
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)clientId, false));
×
1034

1035
      // user
1036
      char user[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
×
1037
      STR_TO_VARSTR(user, pConsumer->user);
×
1038

1039
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1040
      MND_TMQ_NULL_CHECK(pColInfo);
×
1041
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)user, false));
×
1042

1043
      // fqdn
1044
      char fqdn[TSDB_FQDN_LEN + VARSTR_HEADER_SIZE] = {0};
×
1045
      STR_TO_VARSTR(fqdn, pConsumer->fqdn);
×
1046

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

1051
      // status
1052
      const char *pStatusName = mndConsumerStatusName(pConsumer->status);
×
1053
      status = taosMemoryCalloc(1, pShow->pMeta->pSchemas[cols].bytes);
×
1054
      MND_TMQ_NULL_CHECK(status);
×
1055
      STR_TO_VARSTR(status, pStatusName);
×
1056

1057
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1058
      MND_TMQ_NULL_CHECK(pColInfo);
×
1059
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)status, false));
×
1060
      taosMemoryFreeClear(status);
×
1061

1062
      // one subscribed topic
1063
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1064
      MND_TMQ_NULL_CHECK(pColInfo);
×
1065
      if (hasTopic) {
×
1066
        char        topic[TSDB_TOPIC_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
1067
        mndTopicGetShowName(taosArrayGetP(pConsumer->assignedTopics, i), topic + VARSTR_HEADER_SIZE);
×
1068
        *(VarDataLenT *)(topic) = strlen(topic + VARSTR_HEADER_SIZE);
×
1069
        MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)topic, false));
×
1070
      } else {
1071
        MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, NULL, true));
×
1072
      }
1073

1074
      // up time
1075
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1076
      MND_TMQ_NULL_CHECK(pColInfo);
×
1077
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->createTime, false));
×
1078

1079
      // subscribe time
1080
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1081
      MND_TMQ_NULL_CHECK(pColInfo);
×
1082
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->subscribeTime, false));
×
1083

1084
      // rebalance time
1085
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1086
      MND_TMQ_NULL_CHECK(pColInfo);
×
1087
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->rebalanceTime, pConsumer->rebalanceTime == 0));
×
1088

1089
      char         buf[TSDB_OFFSET_LEN] = {0};
×
1090
      STqOffsetVal pVal = {.type = pConsumer->resetOffsetCfg};
×
1091
      tFormatOffset(buf, TSDB_OFFSET_LEN, &pVal);
×
1092

1093
      parasStr = taosMemoryCalloc(1, pShow->pMeta->pSchemas[cols].bytes);
×
1094
      MND_TMQ_NULL_CHECK(parasStr);
×
1095
      (void)snprintf(varDataVal(parasStr), pShow->pMeta->pSchemas[cols].bytes - VARSTR_HEADER_SIZE, "tbname:%d,commit:%d,interval:%dms,reset:%s", pConsumer->withTbName,
×
1096
              pConsumer->autoCommit, pConsumer->autoCommitInterval, buf);
×
1097
      varDataSetLen(parasStr, strlen(varDataVal(parasStr)));
×
1098

1099
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1100
      MND_TMQ_NULL_CHECK(pColInfo);
×
1101
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)parasStr, false));
×
1102
      taosMemoryFreeClear(parasStr);
×
1103

1104
      // rebalance time
1105
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1106
      MND_TMQ_NULL_CHECK(pColInfo);
×
1107
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->pollTime, pConsumer->pollTime == 0));
×
1108
      numOfRows++;
×
1109
    }
1110

1111
    taosRUnLockLatch(&pConsumer->lock);
×
1112
    sdbRelease(pSdb, pConsumer);
×
1113

1114
    pBlock->info.rows = numOfRows;
×
1115
  }
1116

1117
  pShow->numOfRows += numOfRows;
1,867✔
1118
  return numOfRows;
1,867✔
1119

1120
END:
×
1121
  taosMemoryFreeClear(status);
×
1122
  taosMemoryFreeClear(parasStr);
×
1123
  return code;
×
1124
}
1125

1126
static void mndCancelGetNextConsumer(SMnode *pMnode, void *pIter) {
×
1127
  if (pMnode == NULL || pIter == NULL) return;
×
1128
  SSdb *pSdb = pMnode->pSdb;
×
1129
  sdbCancelFetchByType(pSdb, pIter, SDB_CONSUMER);
×
1130
}
1131

1132
const char *mndConsumerStatusName(int status) {
77,641✔
1133
  switch (status) {
77,641!
1134
    case MQ_CONSUMER_STATUS_READY:
31,666✔
1135
      return "ready";
31,666✔
1136
//    case MQ_CONSUMER_STATUS_LOST:
1137
//      return "lost";
1138
    case MQ_CONSUMER_STATUS_REBALANCE:
45,975✔
1139
      return "rebalancing";
45,975✔
1140
    default:
×
1141
      return "unknown";
×
1142
  }
1143
}
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