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

taosdata / TDengine / #4878

11 Dec 2025 02:43AM UTC coverage: 64.569% (-0.02%) from 64.586%
#4878

push

travis-ci

guanshengliang
feat(TS-7270): internal dependence

307 of 617 new or added lines in 24 files covered. (49.76%)

3821 existing lines in 123 files now uncovered.

163630 of 253417 relevant lines covered (64.57%)

107598827.89 hits per line

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

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

63
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndRetrieveConsumer);
490,559✔
64
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndCancelGetNextConsumer);
490,559✔
65

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

69
void mndCleanupConsumer(SMnode *pMnode) {}
489,793✔
70

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

79
  *(int64_t*)msg = consumerId;
32,506✔
80
  SRpcMsg rpcMsg = {
32,506✔
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);
32,506✔
88
  MND_TMQ_RETURN_CHECK(tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg));
32,506✔
89

90
END:
32,506✔
91
  return code;
32,506✔
92
}
93

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

101
  int32_t numOfTopics = taosArrayGetSize(subscribe->topicNames);
82,614✔
102
  for (int32_t i = 0; i < numOfTopics; i++) {
130,756✔
103
    char *pOneTopic = taosArrayGetP(subscribe->topicNames, i);
48,399✔
104
    MND_TMQ_RETURN_CHECK(mndAcquireTopic(pMnode, pOneTopic, &pTopic));
48,399✔
105
    MND_TMQ_RETURN_CHECK(mndCheckTopicPrivilege(pMnode, pUser, MND_OPER_SUBSCRIBE, pTopic));
48,336✔
106
    MND_TMQ_RETURN_CHECK(grantCheckExpire(TSDB_GRANT_SUBSCRIPTION));
48,217✔
107

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

134
END:
257✔
135
  mndReleaseTopic(pMnode, pTopic);
257✔
136
  return code;
257✔
137
}
138

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

150
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, pClearMsg->consumerId, &pConsumer));
32,403✔
151
  mInfo("consumer:0x%" PRIx64 " needs to be cleared, status %s", pClearMsg->consumerId,
32,238✔
152
        mndConsumerStatusName(pConsumer->status));
153

154
  MND_TMQ_RETURN_CHECK(tNewSMqConsumerObj(pConsumer->consumerId, pConsumer->cgroup, -1, NULL, NULL, &pConsumerNew));
32,238✔
155
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, pMsg, "clear-csm");
32,238✔
156
  MND_TMQ_NULL_CHECK(pTrans);
32,238✔
157
  MND_TMQ_RETURN_CHECK(mndSetConsumerDropLogs(pTrans, pConsumerNew));
32,238✔
158
  code = mndTransPrepare(pMnode, pTrans);
32,238✔
159

160
END:
32,403✔
161
  mndReleaseConsumer(pMnode, pConsumer);
32,403✔
162
  tDeleteSMqConsumerObj(pConsumerNew);
32,403✔
163
  mndTransDrop(pTrans);
32,403✔
164
  return code;
32,403✔
165
}
166

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

196
static void storeOffsetRows(SMnode *pMnode, SMqHbReq *req, SMqConsumerObj *pConsumer){
258,679✔
197
  if (pMnode == NULL || req == NULL || pConsumer == NULL){
258,679✔
198
    return;
×
199
  }
200
  for (int i = 0; i < taosArrayGetSize(req->topics); i++) {
476,861✔
201
    TopicOffsetRows *data = taosArrayGet(req->topics, i);
218,182✔
202
    if (data == NULL){
218,182✔
203
      continue;
×
204
    }
205
    mInfo("heartbeat report offset rows.%s:%s", pConsumer->cgroup, data->topicName);
218,182✔
206

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

224
    mndReleaseSubscribe(pMnode, pSub);
218,182✔
225
  }
226
}
227

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

241
  if(tSerializeSMqHbRsp(buf, tlen, rsp) <= 0){
258,679✔
242
    rpcFreeCont(buf);
×
243
    return TSDB_CODE_TMQ_INVALID_MSG;
×
244
  }
245
  pMsg->info.rsp = buf;
258,679✔
246
  pMsg->info.rspLen = tlen;
258,679✔
247
  return 0;
258,679✔
248
}
249

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

271
  storeOffsetRows(pMnode, &req, pConsumer);
258,679✔
272
  rsp.debugFlag = tqClientDebugFlag;
258,679✔
273
  code = buildMqHbRsp(pMsg, &rsp);
258,679✔
274

275
END:
263,301✔
276
  tDestroySMqHbRsp(&rsp);
263,301✔
277
  mndReleaseConsumer(pMnode, pConsumer);
263,301✔
278
  tDestroySMqHbReq(&req);
263,301✔
279
  PRINT_LOG_END(code)
263,301✔
280
  return code;
263,301✔
281
}
282

283
static int32_t addEpSetInfo(SMnode *pMnode, SMqConsumerObj *pConsumer, int32_t epoch, SMqAskEpRsp *rsp){
109,624✔
284
  if (pMnode == NULL || pConsumer == NULL || rsp == NULL){
109,624✔
285
    return TSDB_CODE_INVALID_PARA;
×
286
  }
287
  taosRLockLatch(&pConsumer->lock);
109,624✔
288

289
  int32_t numOfTopics = taosArrayGetSize(pConsumer->currentTopics);
109,624✔
290

291
  rsp->topics = taosArrayInit(numOfTopics, sizeof(SMqSubTopicEp));
109,624✔
292
  if (rsp->topics == NULL) {
109,624✔
293
    taosRUnLockLatch(&pConsumer->lock);
×
294
    return terrno;
×
295
  }
296

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

309
    SMqSubTopicEp topicEp = {0};
75,255✔
310
    tstrncpy(topicEp.topic, topic, TSDB_TOPIC_FNAME_LEN);
75,255✔
311

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

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

356
    for (int32_t j = 0; j < vgNum; j++) {
268,850✔
357
      SMqVgEp *pVgEp = taosArrayGet(pConsumerEp->vgs, j);
193,595✔
358
      if (pVgEp == NULL) {
193,346✔
359
        continue;
×
360
      }
361
      if (epoch == -1) {
193,346✔
362
        SVgObj *pVgroup = mndAcquireVgroup(pMnode, pVgEp->vgId);
26,611✔
363
        if (pVgroup) {
26,611✔
364
          pVgEp->epSet = mndGetVgroupEpset(pMnode, pVgroup);
1,978✔
365
          mndReleaseVgroup(pMnode, pVgroup);
1,978✔
366
        }
367
      }
368
      SMqSubVgEp vgEp = {.epSet = pVgEp->epSet, .vgId = pVgEp->vgId, .offset = -1};
193,346✔
369
      if (taosArrayPush(topicEp.vgs, &vgEp) == NULL) {
386,941✔
370
        taosMemoryFreeClear(topicEp.schema.pSchema);
×
371
        taosArrayDestroy(topicEp.vgs);
×
372
        taosRUnLockLatch(&pConsumer->lock);
×
373
        taosRUnLockLatch(&pSub->lock);
×
374
        mndReleaseSubscribe(pMnode, pSub);
×
375
        return terrno;
×
376
      }
377
    }
378
    if (taosArrayPush(rsp->topics, &topicEp) == NULL) {
150,510✔
379
      taosMemoryFreeClear(topicEp.schema.pSchema);
×
380
      taosArrayDestroy(topicEp.vgs);
×
381
      taosRUnLockLatch(&pConsumer->lock);
×
382
      taosRUnLockLatch(&pSub->lock);
×
383
      mndReleaseSubscribe(pMnode, pSub);
×
384
      return terrno;
×
385
    }
386
    taosRUnLockLatch(&pSub->lock);
75,255✔
387
    mndReleaseSubscribe(pMnode, pSub);
75,255✔
388
  }
389
  taosRUnLockLatch(&pConsumer->lock);
109,624✔
390
  return 0;
109,624✔
391
}
392

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

405
  SMqRspHead *pHead = buf;
554,166✔
406

407
  pHead->mqMsgType = TMQ_MSG_TYPE__EP_RSP;
554,166✔
408
  pHead->epoch = serverEpoch;
554,166✔
409
  pHead->consumerId = consumerId;
554,166✔
410
  pHead->walsver = 0;
553,917✔
411
  pHead->walever = 0;
554,167✔
412

413
  void *abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
554,167✔
414
  if (tEncodeSMqAskEpRsp(&abuf, rsp) < 0) {
554,167✔
415
    rpcFreeCont(buf);
×
416
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
417
  }
418

419
  // send rsp
420
  pMsg->info.rsp = buf;
554,167✔
421
  pMsg->info.rspLen = tlen;
554,167✔
422
  return code;
554,253✔
423
}
424

425
static int32_t mndProcessAskEpReq(SRpcMsg *pMsg) {
902,253✔
426
  if (pMsg == NULL) {
902,253✔
427
    return TSDB_CODE_INVALID_PARA;
×
428
  }
429
  SMnode     *pMnode = pMsg->info.node;
902,253✔
430
  SMqAskEpReq req = {0};
902,356✔
431
  SMqAskEpRsp rsp = {0};
902,442✔
432
  int32_t     code = 0;
901,901✔
433
  SMqConsumerObj *pConsumer = NULL;
901,901✔
434
  PRINT_LOG_START
901,987✔
435

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

446
  // 1. check consumer status
447
  int32_t status = atomic_load_32(&pConsumer->status);
898,789✔
448
  if (status != MQ_CONSUMER_STATUS_READY) {
898,892✔
449
    mInfo("consumer:0x%" PRIx64 " not ready, status: %s", consumerId, mndConsumerStatusName(status));
344,725✔
450
    code = TSDB_CODE_MND_CONSUMER_NOT_READY;
344,725✔
451
    goto END;
344,725✔
452
  }
453

454
  int32_t epoch = req.epoch;
554,167✔
455
  int32_t serverEpoch = atomic_load_32(&pConsumer->epoch);
554,167✔
456

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

464
  code = buildAskEpRsp(pMsg, &rsp, serverEpoch, consumerId);
554,167✔
465

466
END:
902,356✔
467
  tDeleteSMqAskEpRsp(&rsp);
468
  mndReleaseConsumer(pMnode, pConsumer);
902,441✔
469
  PRINT_LOG_END(code);
902,442✔
470
  return code;
902,442✔
471
}
472

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

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

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

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

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

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

578
END:
34,935✔
579
  return code;
35,409✔
580
}
581

582
static int32_t checkAndSortTopic(SMnode *pMnode, SArray *pTopicList){
82,717✔
583
  if (pTopicList == NULL || pMnode == NULL) {
82,717✔
584
    return TSDB_CODE_INVALID_PARA;
×
585
  }
586
  taosArraySort(pTopicList, taosArrayCompareString);
82,717✔
587
  taosArrayRemoveDuplicate(pTopicList, taosArrayCompareString, freeItem);
82,717✔
588

589
  int32_t newTopicNum = taosArrayGetSize(pTopicList);
82,717✔
590
  for (int i = 0; i < newTopicNum; i++) {
131,116✔
591
    int32_t gNum = mndGetGroupNumByTopic(pMnode, (const char *)taosArrayGetP(pTopicList, i));
48,502✔
592
    if (gNum >= MND_MAX_GROUP_PER_TOPIC) {
48,502✔
593
      return TSDB_CODE_TMQ_GROUP_OUT_OF_RANGE;
103✔
594
    }
595
  }
596
  return 0;
82,614✔
597
}
598

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

613
    MND_TMQ_RETURN_CHECK(tNewSMqConsumerObj(consumerId, cgroup, CONSUMER_INSERT_SUB, NULL, subscribe, &pConsumerNew));
46,948✔
614
  } else {
615
    int32_t status = atomic_load_32(&pExistedConsumer->status);
35,409✔
616

617
    mInfo("receive tmq subscribe request from existed consumer:0x%" PRIx64
35,409✔
618
              ",cgroup:%s, current status:%d(%s), subscribe topic num: %d",
619
          consumerId, subscribe->cgroup, status, mndConsumerStatusName(status),
620
          (int32_t)taosArrayGetSize(subscribe->topicNames));
621

622
    if (status != MQ_CONSUMER_STATUS_READY) {
35,409✔
623
      code = TSDB_CODE_MND_CONSUMER_NOT_READY;
×
624
      goto END;
×
625
    }
626
    MND_TMQ_RETURN_CHECK(tNewSMqConsumerObj(consumerId, cgroup, CONSUMER_UPDATE_SUB, NULL, subscribe, &pConsumerNew));
35,409✔
627
    MND_TMQ_RETURN_CHECK(getTopicAddDelete(pExistedConsumer, pConsumerNew));
35,409✔
628
  }
629
  mndReleaseConsumer(pMnode, pExistedConsumer);
81,883✔
630
  if (ppConsumer){
81,883✔
631
    *ppConsumer = pConsumerNew;
81,883✔
632
  }
633
  return code;
81,883✔
634

635
END:
474✔
636
  mndReleaseConsumer(pMnode, pExistedConsumer);
474✔
637
  tDeleteSMqConsumerObj(pConsumerNew);
474✔
638
  return code;
474✔
639
}
640

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

651
  PRINT_LOG_START
113,950✔
652
  SCMSubscribeReq subscribe = {0};
113,950✔
653
  MND_TMQ_RETURN_CHECK(tDeserializeSCMSubscribeReq(msgStr, &subscribe, pMsg->contLen));
227,900✔
654
  bool unSubscribe = (taosArrayGetSize(subscribe.topicNames) == 0);
113,950✔
655
  if(unSubscribe){
113,950✔
656
    SMqConsumerObj *pConsumerTmp = NULL;
65,946✔
657
    MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, subscribe.consumerId, &pConsumerTmp));
65,946✔
658
    if (taosArrayGetSize(pConsumerTmp->assignedTopics) == 0){
63,580✔
659
      mndReleaseConsumer(pMnode, pConsumerTmp);
28,867✔
660
      goto END;
28,867✔
661
    }
662
    mndReleaseConsumer(pMnode, pConsumerTmp);
34,713✔
663
  }
664
  MND_TMQ_RETURN_CHECK(checkAndSortTopic(pMnode, subscribe.topicNames));
82,717✔
665
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY,
82,614✔
666
                          (unSubscribe ? TRN_CONFLICT_NOTHING :TRN_CONFLICT_DB_INSIDE),
667
                          pMsg, "subscribe");
668
  MND_TMQ_NULL_CHECK(pTrans);
82,614✔
669

670
  MND_TMQ_RETURN_CHECK(validateTopics(pTrans, &subscribe, pMnode, pMsg->info.conn.user));
82,614✔
671
  MND_TMQ_RETURN_CHECK(buildSubConsumer(pMnode, &subscribe, &pConsumerNew));
82,357✔
672
  MND_TMQ_RETURN_CHECK(mndSetConsumerCommitLogs(pTrans, pConsumerNew));
81,883✔
673
  MND_TMQ_RETURN_CHECK(mndTransPrepare(pMnode, pTrans));
81,883✔
674
  code = TSDB_CODE_ACTION_IN_PROGRESS;
81,883✔
675

676
END:
113,950✔
677
  mndTransDrop(pTrans);
113,950✔
678
  tDeleteSMqConsumerObj(pConsumerNew);
113,950✔
679
  taosArrayDestroyP(subscribe.topicNames, NULL);
113,950✔
680
  code = (code == TSDB_CODE_TMQ_NO_NEED_REBALANCE || code == TSDB_CODE_MND_CONSUMER_NOT_EXIST) ? 0 : code;
113,950✔
681
  if (code != TSDB_CODE_ACTION_IN_PROGRESS){
113,950✔
682
    PRINT_LOG_END(code);
32,067✔
683
  }
684
  return code;
113,950✔
685
}
686

687
SSdbRaw *mndConsumerActionEncode(SMqConsumerObj *pConsumer) {
235,332✔
688
  if (pConsumer == NULL) {
235,332✔
689
    return NULL;
×
690
  }
691
  int32_t code = 0;
235,332✔
692
  int32_t lino = 0;
235,332✔
693
  terrno = TSDB_CODE_OUT_OF_MEMORY;
235,332✔
694

695
  void   *buf = NULL;
235,332✔
696
  int32_t tlen = tEncodeSMqConsumerObj(NULL, pConsumer);
235,332✔
697
  int32_t size = sizeof(int32_t) + tlen + MND_CONSUMER_RESERVE_SIZE;
235,332✔
698

699
  SSdbRaw *pRaw = sdbAllocRaw(SDB_CONSUMER, MND_CONSUMER_VER_NUMBER, size);
235,332✔
700
  if (pRaw == NULL) goto CM_ENCODE_OVER;
235,332✔
701

702
  buf = taosMemoryMalloc(tlen);
235,332✔
703
  if (buf == NULL) goto CM_ENCODE_OVER;
235,332✔
704

705
  void *abuf = buf;
235,332✔
706
  if(tEncodeSMqConsumerObj(&abuf, pConsumer) < 0){
235,332✔
707
    goto CM_ENCODE_OVER;
×
708
  }
709

710
  int32_t dataPos = 0;
235,332✔
711
  SDB_SET_INT32(pRaw, dataPos, tlen, CM_ENCODE_OVER);
235,332✔
712
  SDB_SET_BINARY(pRaw, dataPos, buf, tlen, CM_ENCODE_OVER);
235,332✔
713
  SDB_SET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_ENCODE_OVER);
235,332✔
714
  SDB_SET_DATALEN(pRaw, dataPos, CM_ENCODE_OVER);
235,332✔
715

716
  terrno = TSDB_CODE_SUCCESS;
235,332✔
717

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

726
  mTrace("consumer:0x%" PRIx64 ", encode to raw:%p, row:%p", pConsumer->consumerId, pRaw, pConsumer);
235,332✔
727
  return pRaw;
235,332✔
728
}
729

730
SSdbRow *mndConsumerActionDecode(SSdbRaw *pRaw) {
201,109✔
731
  if (pRaw == NULL) {
201,109✔
732
    return NULL;
×
733
  }
734
  int32_t         code = 0;
201,109✔
735
  int32_t         lino = 0;
201,109✔
736
  SSdbRow        *pRow = NULL;
201,109✔
737
  SMqConsumerObj *pConsumer = NULL;
201,109✔
738
  void           *buf = NULL;
201,109✔
739

740
  terrno = 0;
201,109✔
741
  int8_t sver = 0;
201,109✔
742
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
201,109✔
743
    goto CM_DECODE_OVER;
×
744
  }
745

746
  if (sver < 1 || sver > MND_CONSUMER_VER_NUMBER) {
201,109✔
747
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
748
    goto CM_DECODE_OVER;
×
749
  }
750

751
  pRow = sdbAllocRow(sizeof(SMqConsumerObj));
201,109✔
752
  if (pRow == NULL) {
201,109✔
753
    goto CM_DECODE_OVER;
×
754
  }
755

756
  pConsumer = sdbGetRowObj(pRow);
201,109✔
757
  if (pConsumer == NULL) {
201,109✔
758
    goto CM_DECODE_OVER;
×
759
  }
760

761
  int32_t dataPos = 0;
201,109✔
762
  int32_t len;
198,547✔
763
  SDB_GET_INT32(pRaw, dataPos, &len, CM_DECODE_OVER);
201,109✔
764
  buf = taosMemoryMalloc(len);
201,109✔
765
  if (buf == NULL) {
201,109✔
766
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
767
    goto CM_DECODE_OVER;
×
768
  }
769

770
  SDB_GET_BINARY(pRaw, dataPos, buf, len, CM_DECODE_OVER);
201,109✔
771
  SDB_GET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_DECODE_OVER);
201,109✔
772

773
  if (tDecodeSMqConsumerObj(buf, pConsumer, sver) == NULL) {
201,109✔
774
    terrno = TSDB_CODE_OUT_OF_MEMORY;  // TODO set correct error code
×
775
    goto CM_DECODE_OVER;
×
776
  }
777

778
  tmsgUpdateDnodeEpSet(&pConsumer->ep);
201,109✔
779

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

788
  return pRow;
201,109✔
789
}
790

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

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

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

823
// remove from topic list
824
static void removeFromTopicList(SArray *topicList, const char *pTopic, int64_t consumerId, char *type) {
121,293✔
825
  if (topicList == NULL || pTopic == NULL) {
121,293✔
826
    return;
×
827
  }
828
  int32_t size = taosArrayGetSize(topicList);
121,293✔
829
  for (int32_t i = 0; i < size; i++) {
122,136✔
830
    char *p = taosArrayGetP(topicList, i);
120,652✔
831
    if (strcmp(pTopic, p) == 0) {
120,652✔
832
      taosArrayRemove(topicList, i);
119,809✔
833
      taosMemoryFree(p);
119,809✔
834

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

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

856
  return existing;
47,779✔
857
}
858

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

866
  taosWLockLatch(&pOldConsumer->lock);
121,039✔
867

868
  if (pNewConsumer->updateType == CONSUMER_UPDATE_SUB) {
121,039✔
869
    TSWAP(pOldConsumer->rebNewTopics, pNewConsumer->rebNewTopics);
34,998✔
870
    TSWAP(pOldConsumer->rebRemovedTopics, pNewConsumer->rebRemovedTopics);
34,998✔
871
    TSWAP(pOldConsumer->assignedTopics, pNewConsumer->assignedTopics);
34,998✔
872

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

902
    int32_t status = pOldConsumer->status;
47,779✔
903
//    updateConsumerStatus(pOldConsumer);
904
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
47,779✔
905
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
47,281✔
906
    }
907

908
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
47,779✔
909
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
47,779✔
910

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

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

926
    int32_t status = pOldConsumer->status;
36,757✔
927
//    updateConsumerStatus(pOldConsumer);
928
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
36,757✔
929
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
36,259✔
930
    }
931
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
36,757✔
932
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
36,757✔
933

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

942
  taosWUnLockLatch(&pOldConsumer->lock);
121,039✔
943
  return 0;
121,039✔
944
}
945

946
int32_t mndAcquireConsumer(SMnode *pMnode, int64_t consumerId, SMqConsumerObj** pConsumer) {
1,346,449✔
947
  if (pMnode == NULL || pConsumer == NULL) {
1,346,449✔
948
    return TSDB_CODE_INVALID_PARA;
×
949
  }
950
  SSdb           *pSdb = pMnode->pSdb;
1,346,449✔
951
  *pConsumer = sdbAcquire(pSdb, SDB_CONSUMER, &consumerId);
1,346,449✔
952
  if (*pConsumer == NULL) {
1,346,346✔
953
    return TSDB_CODE_MND_CONSUMER_NOT_EXIST;
57,565✔
954
  }
955
  return 0;
1,288,781✔
956
}
957

958
void mndReleaseConsumer(SMnode *pMnode, SMqConsumerObj *pConsumer) {
12,326,503✔
959
  if (pMnode == NULL || pConsumer == NULL) {
12,326,503✔
960
    return;
10,678,593✔
961
  }
962
  SSdb *pSdb = pMnode->pSdb;
1,647,910✔
963
  sdbRelease(pSdb, pConsumer);
1,647,824✔
964
}
965

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

978
  while (numOfRows < rowsCapacity) {
28,690✔
979
    pShow->pIter = sdbFetch(pSdb, SDB_CONSUMER, pShow->pIter, (void **)&pConsumer);
28,690✔
980
    if (pShow->pIter == NULL) {
28,690✔
981
      break;
9,421✔
982
    }
983

984
    if (taosArrayGetSize(pConsumer->assignedTopics) == 0) {
19,269✔
985
      mInfo("showing consumer:0x%" PRIx64 " no assigned topic, skip", pConsumer->consumerId);
467✔
986
      sdbRelease(pSdb, pConsumer);
467✔
987
      continue;
467✔
988
    }
989

990
    taosRLockLatch(&pConsumer->lock);
18,802✔
991
    mInfo("showing consumer:0x%" PRIx64, pConsumer->consumerId);
18,802✔
992

993
    int32_t topicSz = taosArrayGetSize(pConsumer->assignedTopics);
18,802✔
994
    bool    hasTopic = true;
18,802✔
995
    if (topicSz == 0) {
18,802✔
996
      hasTopic = false;
×
997
      topicSz = 1;
×
998
    }
999

1000
    if (numOfRows + topicSz > rowsCapacity) {
18,802✔
1001
      MND_TMQ_RETURN_CHECK(blockDataEnsureCapacity(pBlock, numOfRows + topicSz));
×
1002
    }
1003

1004
    for (int32_t i = 0; i < topicSz; i++) {
37,604✔
1005
      SColumnInfoData *pColInfo = NULL;
18,802✔
1006
      int32_t          cols = 0;
18,802✔
1007

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

1013
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,802✔
1014
      MND_TMQ_NULL_CHECK(pColInfo);
18,802✔
1015
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)consumerIdHex, false));
18,802✔
1016

1017
      // consumer group
1018
      char cgroup[TSDB_CGROUP_LEN + VARSTR_HEADER_SIZE] = {0};
18,802✔
1019
      STR_TO_VARSTR(cgroup, pConsumer->cgroup);
18,802✔
1020

1021
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,802✔
1022
      MND_TMQ_NULL_CHECK(pColInfo);
18,802✔
1023
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)cgroup, false));
18,802✔
1024

1025
      // client id
1026
      char clientId[TSDB_CLIENT_ID_LEN + VARSTR_HEADER_SIZE] = {0};
18,802✔
1027
      STR_TO_VARSTR(clientId, pConsumer->clientId);
18,802✔
1028

1029
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,802✔
1030
      MND_TMQ_NULL_CHECK(pColInfo);
18,802✔
1031
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)clientId, false));
18,802✔
1032

1033
      // user
1034
      char user[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
18,802✔
1035
      STR_TO_VARSTR(user, pConsumer->user);
18,802✔
1036

1037
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,802✔
1038
      MND_TMQ_NULL_CHECK(pColInfo);
18,802✔
1039
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)user, false));
18,802✔
1040

1041
      // fqdn
1042
      char fqdn[TSDB_FQDN_LEN + VARSTR_HEADER_SIZE] = {0};
18,802✔
1043
      STR_TO_VARSTR(fqdn, pConsumer->fqdn);
18,802✔
1044

1045
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,802✔
1046
      MND_TMQ_NULL_CHECK(pColInfo);
18,802✔
1047
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)fqdn, false));
18,802✔
1048

1049
      // status
1050
      const char *pStatusName = mndConsumerStatusName(pConsumer->status);
18,802✔
1051
      status = taosMemoryCalloc(1, pShow->pMeta->pSchemas[cols].bytes);
18,802✔
1052
      MND_TMQ_NULL_CHECK(status);
18,802✔
1053
      STR_TO_VARSTR(status, pStatusName);
18,802✔
1054

1055
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,802✔
1056
      MND_TMQ_NULL_CHECK(pColInfo);
18,802✔
1057
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)status, false));
18,802✔
1058
      taosMemoryFreeClear(status);
18,802✔
1059

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

1072
      // up time
1073
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,802✔
1074
      MND_TMQ_NULL_CHECK(pColInfo);
18,802✔
1075
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->createTime, false));
18,802✔
1076

1077
      // subscribe time
1078
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,802✔
1079
      MND_TMQ_NULL_CHECK(pColInfo);
18,802✔
1080
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->subscribeTime, false));
18,802✔
1081

1082
      // rebalance time
1083
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,802✔
1084
      MND_TMQ_NULL_CHECK(pColInfo);
18,802✔
1085
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->rebalanceTime, pConsumer->rebalanceTime == 0));
18,802✔
1086

1087
      char         buf[TSDB_OFFSET_LEN] = {0};
18,802✔
1088
      STqOffsetVal pVal = {.type = pConsumer->resetOffsetCfg};
18,802✔
1089
      tFormatOffset(buf, TSDB_OFFSET_LEN, &pVal);
18,802✔
1090

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

1097
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,802✔
1098
      MND_TMQ_NULL_CHECK(pColInfo);
18,802✔
1099
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)parasStr, false));
18,802✔
1100
      taosMemoryFreeClear(parasStr);
18,802✔
1101

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

1109
    taosRUnLockLatch(&pConsumer->lock);
18,802✔
1110
    sdbRelease(pSdb, pConsumer);
18,802✔
1111

1112
    pBlock->info.rows = numOfRows;
18,802✔
1113
  }
1114

1115
  pShow->numOfRows += numOfRows;
9,421✔
1116
  return numOfRows;
9,421✔
1117

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

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

1130
const char *mndConsumerStatusName(int status) {
1,209,454✔
1131
  switch (status) {
1,209,454✔
1132
    case MQ_CONSUMER_STATUS_READY:
482,415✔
1133
      return "ready";
482,415✔
1134
//    case MQ_CONSUMER_STATUS_LOST:
1135
//      return "lost";
1136
    case MQ_CONSUMER_STATUS_REBALANCE:
727,039✔
1137
      return "rebalancing";
727,039✔
1138
    default:
×
1139
      return "unknown";
×
1140
  }
1141
}
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