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

taosdata / TDengine / #4859

22 Nov 2025 07:23AM UTC coverage: 64.272% (+0.2%) from 64.048%
#4859

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

0 of 79 new or added lines in 2 files covered. (0.0%)

367 existing lines in 91 files now uncovered.

154463 of 240326 relevant lines covered (64.27%)

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

63
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndRetrieveConsumer);
479,197✔
64
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndCancelGetNextConsumer);
479,197✔
65

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

69
void mndCleanupConsumer(SMnode *pMnode) {}
478,413✔
70

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

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

90
END:
33,678✔
91
  return code;
33,678✔
92
}
93

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

101
  int32_t numOfTopics = taosArrayGetSize(subscribe->topicNames);
84,880✔
102
  for (int32_t i = 0; i < numOfTopics; i++) {
134,310✔
103
    char *pOneTopic = taosArrayGetP(subscribe->topicNames, i);
49,708✔
104
    MND_TMQ_RETURN_CHECK(mndAcquireTopic(pMnode, pOneTopic, &pTopic));
49,708✔
105
    MND_TMQ_RETURN_CHECK(mndCheckTopicPrivilege(pMnode, pUser, MND_OPER_SUBSCRIBE, pTopic));
49,645✔
106
    MND_TMQ_RETURN_CHECK(grantCheckExpire(TSDB_GRANT_SUBSCRIPTION));
49,520✔
107

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

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

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

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

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

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

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

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

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

224
    mndReleaseSubscribe(pMnode, pSub);
231,271✔
225
  }
226
}
227

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

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

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

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

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

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

289
  int32_t numOfTopics = taosArrayGetSize(pConsumer->currentTopics);
124,749✔
290

291
  rsp->topics = taosArrayInit(numOfTopics, sizeof(SMqSubTopicEp));
124,749✔
292
  if (rsp->topics == NULL) {
124,749✔
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++) {
215,096✔
299
    char            *topic = taosArrayGetP(pConsumer->currentTopics, i);
90,347✔
300
    SMqSubscribeObj *pSub = NULL;
90,347✔
301
    char  key[TSDB_SUBSCRIBE_KEY_LEN] = {0};
90,347✔
302
    (void)snprintf(key, TSDB_SUBSCRIBE_KEY_LEN, "%s%s%s", pConsumer->cgroup, TMQ_SEPARATOR, topic);
90,347✔
303
    int32_t code = mndAcquireSubscribeByKey(pMnode, key, &pSub);
90,347✔
304
    if (code != 0) {
90,347✔
305
      continue;
×
306
    }
307
    taosRLockLatch(&pSub->lock);
90,347✔
308

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

312
    // 2.1 fetch topic schema
313
    SMqTopicObj *pTopic = NULL;
90,347✔
314
    code = mndAcquireTopic(pMnode, topic, &pTopic);
90,347✔
315
    if (code != TSDB_CODE_SUCCESS) {
90,347✔
316
      taosRUnLockLatch(&pSub->lock);
×
317
      mndReleaseSubscribe(pMnode, pSub);
×
318
      continue;
×
319
    }
320
    taosRLockLatch(&pTopic->lock);
90,347✔
321
    tstrncpy(topicEp.db, pTopic->db, TSDB_DB_FNAME_LEN);
90,347✔
322
    topicEp.schema.nCols = pTopic->schema.nCols;
90,347✔
323
    if (topicEp.schema.nCols) {
90,347✔
324
      topicEp.schema.pSchema = taosMemoryCalloc(topicEp.schema.nCols, sizeof(SSchema));
64,640✔
325
      if (topicEp.schema.pSchema == NULL) {
64,640✔
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));
64,640✔
333
    }
334
    taosRUnLockLatch(&pTopic->lock);
90,347✔
335
    mndReleaseTopic(pMnode, pTopic);
90,347✔
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));
90,347✔
339
    if (pConsumerEp == NULL) {
90,347✔
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);
90,347✔
347
    topicEp.vgs = taosArrayInit(vgNum, sizeof(SMqSubVgEp));
90,347✔
348
    if (topicEp.vgs == NULL) {
90,347✔
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++) {
294,599✔
357
      SMqVgEp *pVgEp = taosArrayGet(pConsumerEp->vgs, j);
204,252✔
358
      if (pVgEp == NULL) {
204,252✔
359
        continue;
×
360
      }
361
      if (epoch == -1) {
204,252✔
362
        SVgObj *pVgroup = mndAcquireVgroup(pMnode, pVgEp->vgId);
39,384✔
363
        if (pVgroup) {
39,384✔
364
          pVgEp->epSet = mndGetVgroupEpset(pMnode, pVgroup);
2,546✔
365
          mndReleaseVgroup(pMnode, pVgroup);
2,546✔
366
        }
367
      }
368
      SMqSubVgEp vgEp = {.epSet = pVgEp->epSet, .vgId = pVgEp->vgId, .offset = -1};
204,252✔
369
      if (taosArrayPush(topicEp.vgs, &vgEp) == NULL) {
408,504✔
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) {
180,694✔
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);
90,347✔
387
    mndReleaseSubscribe(pMnode, pSub);
90,347✔
388
  }
389
  taosRUnLockLatch(&pConsumer->lock);
124,749✔
390
  return 0;
124,749✔
391
}
392

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

405
  SMqRspHead *pHead = buf;
608,804✔
406

407
  pHead->mqMsgType = TMQ_MSG_TYPE__EP_RSP;
608,804✔
408
  pHead->epoch = serverEpoch;
608,804✔
409
  pHead->consumerId = consumerId;
608,804✔
410
  pHead->walsver = 0;
608,804✔
411
  pHead->walever = 0;
608,804✔
412

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

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

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

436
  MND_TMQ_RETURN_CHECK(tDeserializeSMqAskEpReq(pMsg->pCont, pMsg->contLen, &req));
969,096✔
437
  int64_t consumerId = req.consumerId;
969,096✔
438
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
969,096✔
439
  if (strncmp(req.cgroup, pConsumer->cgroup, tListLen(pConsumer->cgroup)) != 0) {
964,100✔
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);
964,100✔
448
  if (status != MQ_CONSUMER_STATUS_READY) {
964,100✔
449
    mInfo("consumer:0x%" PRIx64 " not ready, status: %s", consumerId, mndConsumerStatusName(status));
355,296✔
450
    code = TSDB_CODE_MND_CONSUMER_NOT_READY;
355,296✔
451
    goto END;
355,296✔
452
  }
453

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

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

464
  code = buildAskEpRsp(pMsg, &rsp, serverEpoch, consumerId);
608,804✔
465

466
END:
969,096✔
467
  tDeleteSMqAskEpRsp(&rsp);
468
  mndReleaseConsumer(pMnode, pConsumer);
969,096✔
469
  PRINT_LOG_END(code);
969,096✔
470
  return code;
968,990✔
471
}
472

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

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

507
static void freeItem(void *param) {
68✔
508
  if (param == NULL) {
68✔
509
    return;
×
510
  }
511
  void *pItem = *(void **)param;
68✔
512
  if (pItem != NULL) {
68✔
513
    taosMemoryFree(pItem);
68✔
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){
36,381✔
527
  if (pExistedConsumer == NULL || pConsumerNew == NULL) {
36,381✔
528
    return TSDB_CODE_INVALID_PARA;
×
529
  }
530
  int32_t code = 0;
36,381✔
531
  pConsumerNew->rebNewTopics = taosArrayInit(0, sizeof(void *));
36,381✔
532
  MND_TMQ_NULL_CHECK(pConsumerNew->rebNewTopics);
36,381✔
533
  pConsumerNew->rebRemovedTopics = taosArrayInit(0, sizeof(void *));
36,381✔
534
  MND_TMQ_NULL_CHECK(pConsumerNew->rebRemovedTopics);
36,381✔
535

536
  int32_t newTopicNum = taosArrayGetSize(pConsumerNew->assignedTopics);
36,381✔
537
  int32_t oldTopicNum = taosArrayGetSize(pExistedConsumer->currentTopics);
36,381✔
538
  int32_t i = 0, j = 0;
36,381✔
539
  while (i < oldTopicNum || j < newTopicNum) {
73,285✔
540
    if (i >= oldTopicNum) {
36,904✔
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) {
36,682✔
547
      void* tmp = taosArrayGetP(pExistedConsumer->currentTopics, i);
36,218✔
548
      MND_TMQ_NULL_CHECK(tmp);
36,218✔
549
      ADD_TOPIC_TO_ARRAY(tmp, rebRemovedTopics);
72,436✔
550
      i++;
36,218✔
551
      continue;
36,218✔
552
    } else {
553
      char *oldTopic = taosArrayGetP(pExistedConsumer->currentTopics, i);
464✔
554
      MND_TMQ_NULL_CHECK(oldTopic);
464✔
555
      char *newTopic = taosArrayGetP(pConsumerNew->assignedTopics, j);
464✔
556
      MND_TMQ_NULL_CHECK(newTopic);
464✔
557
      int   comp = strcmp(oldTopic, newTopic);
464✔
558
      if (comp == 0) {
464✔
559
        i++;
464✔
560
        j++;
464✔
561
        continue;
464✔
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) {
36,381✔
575
    code = TSDB_CODE_TMQ_NO_NEED_REBALANCE;
464✔
576
  }
577

578
END:
35,917✔
579
  return code;
36,381✔
580
}
581

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

589
  int32_t newTopicNum = taosArrayGetSize(pTopicList);
84,986✔
590
  for (int i = 0; i < newTopicNum; i++) {
134,694✔
591
    int32_t gNum = mndGetGroupNumByTopic(pMnode, (const char *)taosArrayGetP(pTopicList, i));
49,814✔
592
    if (gNum >= MND_MAX_GROUP_PER_TOPIC) {
49,814✔
593
      return TSDB_CODE_TMQ_GROUP_OUT_OF_RANGE;
106✔
594
    }
595
  }
596
  return 0;
84,880✔
597
}
598

599
static int32_t buildSubConsumer(SMnode *pMnode, SCMSubscribeReq *subscribe, SMqConsumerObj** ppConsumer){
84,602✔
600
  if (pMnode == NULL || subscribe == NULL) {
84,602✔
601
    return TSDB_CODE_INVALID_PARA;
×
602
  }
603
  int64_t         consumerId = subscribe->consumerId;
84,602✔
604
  char           *cgroup     = subscribe->cgroup;
84,602✔
605
  SMqConsumerObj *pConsumerNew     = NULL;
84,602✔
606
  SMqConsumerObj *pExistedConsumer = NULL;
84,602✔
607
  int32_t code = mndAcquireConsumer(pMnode, consumerId, &pExistedConsumer);
84,602✔
608
  if (code != 0) {
84,602✔
609
    mInfo("receive tmq subscribe request from new consumer:0x%" PRIx64
48,221✔
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));
48,221✔
614
  } else {
615
    int32_t status = atomic_load_32(&pExistedConsumer->status);
36,381✔
616

617
    mInfo("receive tmq subscribe request from existed consumer:0x%" PRIx64
36,381✔
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) {
36,381✔
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));
36,381✔
627
    MND_TMQ_RETURN_CHECK(getTopicAddDelete(pExistedConsumer, pConsumerNew));
36,381✔
628
  }
629
  mndReleaseConsumer(pMnode, pExistedConsumer);
84,138✔
630
  if (ppConsumer){
84,138✔
631
    *ppConsumer = pConsumerNew;
84,138✔
632
  }
633
  return code;
84,138✔
634

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

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

651
  PRINT_LOG_START
115,175✔
652
  SCMSubscribeReq subscribe = {0};
115,175✔
653
  MND_TMQ_RETURN_CHECK(tDeserializeSCMSubscribeReq(msgStr, &subscribe, pMsg->contLen));
230,350✔
654
  bool unSubscribe = (taosArrayGetSize(subscribe.topicNames) == 0);
115,175✔
655
  if(unSubscribe){
115,175✔
656
    SMqConsumerObj *pConsumerTmp = NULL;
65,884✔
657
    MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, subscribe.consumerId, &pConsumerTmp));
65,884✔
658
    if (taosArrayGetSize(pConsumerTmp->assignedTopics) == 0){
63,004✔
659
      mndReleaseConsumer(pMnode, pConsumerTmp);
27,309✔
660
      goto END;
27,309✔
661
    }
662
    mndReleaseConsumer(pMnode, pConsumerTmp);
35,695✔
663
  }
664
  MND_TMQ_RETURN_CHECK(checkAndSortTopic(pMnode, subscribe.topicNames));
84,986✔
665
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY,
84,880✔
666
                          (unSubscribe ? TRN_CONFLICT_NOTHING :TRN_CONFLICT_DB_INSIDE),
667
                          pMsg, "subscribe");
668
  MND_TMQ_NULL_CHECK(pTrans);
84,880✔
669

670
  MND_TMQ_RETURN_CHECK(validateTopics(pTrans, &subscribe, pMnode, pMsg->info.conn.user));
84,880✔
671
  MND_TMQ_RETURN_CHECK(buildSubConsumer(pMnode, &subscribe, &pConsumerNew));
84,602✔
672
  MND_TMQ_RETURN_CHECK(mndSetConsumerCommitLogs(pTrans, pConsumerNew));
84,138✔
673
  MND_TMQ_RETURN_CHECK(mndTransPrepare(pMnode, pTrans));
84,138✔
674
  code = TSDB_CODE_ACTION_IN_PROGRESS;
84,138✔
675

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

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

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

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

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

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

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

716
  terrno = TSDB_CODE_SUCCESS;
245,571✔
717

718
CM_ENCODE_OVER:
245,571✔
719
  taosMemoryFreeClear(buf);
245,571✔
720
  if (terrno != 0) {
245,571✔
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);
245,571✔
727
  return pRaw;
245,571✔
728
}
729

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

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

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

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

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

761
  int32_t dataPos = 0;
211,107✔
762
  int32_t len;
208,405✔
763
  SDB_GET_INT32(pRaw, dataPos, &len, CM_DECODE_OVER);
211,107✔
764
  buf = taosMemoryMalloc(len);
211,107✔
765
  if (buf == NULL) {
211,107✔
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);
211,107✔
771
  SDB_GET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_DECODE_OVER);
211,107✔
772

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

778
  tmsgUpdateDnodeEpSet(&pConsumer->ep);
211,107✔
779

780
CM_DECODE_OVER:
211,107✔
781
  taosMemoryFreeClear(buf);
211,107✔
782
  if (terrno != TSDB_CODE_SUCCESS) {
211,107✔
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;
211,107✔
789
}
790

791
static int32_t mndConsumerActionInsert(SSdb *pSdb, SMqConsumerObj *pConsumer) {
49,086✔
792
  if (pConsumer == NULL) {
49,086✔
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,
49,086✔
796
        pConsumer->status, mndConsumerStatusName(pConsumer->status), pConsumer->epoch);
797
  pConsumer->subscribeTime = pConsumer->createTime;
49,086✔
798
  return 0;
49,086✔
799
}
800

801
static int32_t mndConsumerActionDelete(SSdb *pSdb, SMqConsumerObj *pConsumer) {
211,107✔
802
  if (pConsumer == NULL) {
211,107✔
803
    return TSDB_CODE_INVALID_PARA;
×
804
  }
805
  mInfo("consumer:0x%" PRIx64 " perform delete action, status:(%d)%s", pConsumer->consumerId, pConsumer->status,
211,107✔
806
        mndConsumerStatusName(pConsumer->status));
807
  tClearSMqConsumerObj(pConsumer);
211,107✔
808
  return 0;
211,107✔
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) {
124,662✔
825
  if (topicList == NULL || pTopic == NULL) {
124,662✔
826
    return;
×
827
  }
828
  int32_t size = taosArrayGetSize(topicList);
124,662✔
829
  for (int32_t i = 0; i < size; i++) {
125,577✔
830
    char *p = taosArrayGetP(topicList, i);
124,076✔
831
    if (strcmp(pTopic, p) == 0) {
124,076✔
832
      taosArrayRemove(topicList, i);
123,161✔
833
      taosMemoryFree(p);
123,161✔
834

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

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

856
  return existing;
49,090✔
857
}
858

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

866
  taosWLockLatch(&pOldConsumer->lock);
128,230✔
867

868
  if (pNewConsumer->updateType == CONSUMER_UPDATE_SUB) {
128,230✔
869
    TSWAP(pOldConsumer->rebNewTopics, pNewConsumer->rebNewTopics);
35,989✔
870
    TSWAP(pOldConsumer->rebRemovedTopics, pNewConsumer->rebRemovedTopics);
35,989✔
871
    TSWAP(pOldConsumer->assignedTopics, pNewConsumer->assignedTopics);
35,989✔
872

873
    pOldConsumer->subscribeTime = taosGetTimestampMs();
35,989✔
874
    pOldConsumer->status = MQ_CONSUMER_STATUS_REBALANCE;
35,989✔
875
    mInfo("consumer:0x%" PRIx64 " subscribe update, modify existed consumer", pOldConsumer->consumerId);
35,989✔
876
  } else if (pNewConsumer->updateType == CONSUMER_UPDATE_REB) {
92,241✔
877
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
5,365✔
878
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
5,365✔
879
    mInfo("tmq rebalance consumer:0x%" PRIx64 " rebalance update, only rebalance time", pOldConsumer->consumerId);
5,365✔
880
  } else if (pNewConsumer->updateType == CONSUMER_ADD_REB) {
86,876✔
881
    void *tmp = taosArrayGetP(pNewConsumer->rebNewTopics, 0);
49,090✔
882
    if (tmp == NULL){
49,090✔
883
      return TSDB_CODE_TMQ_INVALID_MSG;
×
884
    }
885
    char *pNewTopic = taosStrdup(tmp);
49,090✔
886
    if (pNewTopic == NULL) {
49,090✔
887
      return terrno;
×
888
    }
889
    removeFromTopicList(pOldConsumer->rebNewTopics, pNewTopic, pOldConsumer->consumerId, "new");
49,090✔
890
    bool existing = existInCurrentTopicList(pOldConsumer, pNewTopic);
49,090✔
891
    if (existing) {
49,090✔
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) {
98,180✔
896
        taosMemoryFree(pNewTopic);
×
897
        return TSDB_CODE_TMQ_INVALID_MSG;
×
898
      }
899
      taosArraySort(pOldConsumer->currentTopics, taosArrayCompareString);
49,090✔
900
    }
901

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

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

911
    mInfo("tmq rebalance consumer:0x%" PRIx64 " rebalance update add, state (%d)%s -> (%d)%s, new epoch:%d, reb-time:%" PRId64
49,090✔
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) {
37,786✔
919
    char *topic = taosArrayGetP(pNewConsumer->rebRemovedTopics, 0);
37,786✔
920
    if (topic == NULL){
37,786✔
921
      return TSDB_CODE_TMQ_INVALID_MSG;
×
922
    }
923
    removeFromTopicList(pOldConsumer->rebRemovedTopics, topic, pOldConsumer->consumerId, "remove");
37,786✔
924
    removeFromTopicList(pOldConsumer->currentTopics, topic, pOldConsumer->consumerId, "current");
37,786✔
925

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

934
    mInfo("tmq rebalanceconsumer:0x%" PRIx64 " rebalance update remove, state (%d)%s -> (%d)%s, new epoch:%d, reb-time:%" PRId64
37,786✔
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);
128,230✔
943
  return 0;
128,230✔
944
}
945

946
int32_t mndAcquireConsumer(SMnode *pMnode, int64_t consumerId, SMqConsumerObj** pConsumer) {
1,428,426✔
947
  if (pMnode == NULL || pConsumer == NULL) {
1,428,426✔
948
    return TSDB_CODE_INVALID_PARA;
×
949
  }
950
  SSdb           *pSdb = pMnode->pSdb;
1,428,426✔
951
  *pConsumer = sdbAcquire(pSdb, SDB_CONSUMER, &consumerId);
1,428,426✔
952
  if (*pConsumer == NULL) {
1,428,426✔
953
    return TSDB_CODE_MND_CONSUMER_NOT_EXIST;
60,767✔
954
  }
955
  return 0;
1,367,659✔
956
}
957

958
void mndReleaseConsumer(SMnode *pMnode, SMqConsumerObj *pConsumer) {
14,242,704✔
959
  if (pMnode == NULL || pConsumer == NULL) {
14,242,704✔
960
    return;
12,494,001✔
961
  }
962
  SSdb *pSdb = pMnode->pSdb;
1,748,703✔
963
  sdbRelease(pSdb, pConsumer);
1,748,703✔
964
}
965

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

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

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

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

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

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

1004
    for (int32_t i = 0; i < topicSz; i++) {
41,936✔
1005
      SColumnInfoData *pColInfo = NULL;
20,968✔
1006
      int32_t          cols = 0;
20,968✔
1007

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1112
    pBlock->info.rows = numOfRows;
20,968✔
1113
  }
1114

1115
  pShow->numOfRows += numOfRows;
9,459✔
1116
  return numOfRows;
9,459✔
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,262,518✔
1131
  switch (status) {
1,262,518✔
1132
    case MQ_CONSUMER_STATUS_READY:
507,872✔
1133
      return "ready";
507,872✔
1134
//    case MQ_CONSUMER_STATUS_LOST:
1135
//      return "lost";
1136
    case MQ_CONSUMER_STATUS_REBALANCE:
754,646✔
1137
      return "rebalancing";
754,646✔
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