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

taosdata / TDengine / #4712

06 Sep 2025 04:27PM UTC coverage: 58.144% (-1.0%) from 59.134%
#4712

push

travis-ci

GitHub
test: update case description (#32878)

133123 of 291691 branches covered (45.64%)

Branch coverage included in aggregate %.

201244 of 283375 relevant lines covered (71.02%)

5637899.03 hits per line

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

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

63
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndRetrieveConsumer);
1,920✔
64
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndCancelGetNextConsumer);
1,920✔
65

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

69
void mndCleanupConsumer(SMnode *pMnode) {}
1,920✔
70

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

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

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

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

103
  int32_t numOfTopics = taosArrayGetSize(subscribe->topicNames);
764✔
104
  for (int32_t i = 0; i < numOfTopics; i++) {
1,205✔
105
    char *pOneTopic = taosArrayGetP(subscribe->topicNames, i);
453✔
106
    MND_TMQ_RETURN_CHECK(mndAcquireTopic(pMnode, pOneTopic, &pTopic));
457✔
107
    MND_TMQ_RETURN_CHECK(mndCheckTopicPrivilege(pMnode, pUser, MND_OPER_SUBSCRIBE, pTopic));
445✔
108
    MND_TMQ_RETURN_CHECK(grantCheckExpire(TSDB_GRANT_SUBSCRIPTION));
444!
109

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

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

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

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

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

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

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

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

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

226
    mndReleaseSubscribe(pMnode, pSub);
2,112✔
227
  }
228
}
229

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

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

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

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

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

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

291
  int32_t numOfTopics = taosArrayGetSize(pConsumer->currentTopics);
3,110✔
292

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

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

311
    SMqSubTopicEp topicEp = {0};
2,810✔
312
    tstrncpy(topicEp.topic, topic, TSDB_TOPIC_FNAME_LEN);
2,810✔
313

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

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

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

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

407
  SMqRspHead *pHead = buf;
7,901✔
408

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

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

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

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

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

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

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

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

466
  code = buildAskEpRsp(pMsg, &rsp, serverEpoch, consumerId);
7,901✔
467

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

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

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

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

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

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

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

580
END:
321✔
581
  return code;
332✔
582
}
583

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

591
  int32_t newTopicNum = taosArrayGetSize(pTopicList);
765✔
592
  for (int i = 0; i < newTopicNum; i++) {
1,218✔
593
    int32_t gNum = mndGetGroupNumByTopic(pMnode, (const char *)taosArrayGetP(pTopicList, i));
454✔
594
    if (gNum >= MND_MAX_GROUP_PER_TOPIC) {
454✔
595
      return TSDB_CODE_TMQ_GROUP_OUT_OF_RANGE;
1✔
596
    }
597
  }
598
  return 0;
764✔
599
}
600

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

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

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

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

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

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

653
  PRINT_LOG_START
1,009✔
654
  SCMSubscribeReq subscribe = {0};
1,009✔
655
  MND_TMQ_RETURN_CHECK(tDeserializeSCMSubscribeReq(msgStr, &subscribe, pMsg->contLen));
2,018!
656
  bool unSubscribe = (taosArrayGetSize(subscribe.topicNames) == 0);
1,009✔
657
  if(unSubscribe){
1,009✔
658
    SMqConsumerObj *pConsumerTmp = NULL;
560✔
659
    MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, subscribe.consumerId, &pConsumerTmp));
797✔
660
    if (taosArrayGetSize(pConsumerTmp->assignedTopics) == 0){
553✔
661
      mndReleaseConsumer(pMnode, pConsumerTmp);
237✔
662
      goto END;
237✔
663
    }
664
    mndReleaseConsumer(pMnode, pConsumerTmp);
316✔
665
  }
666
  MND_TMQ_RETURN_CHECK(checkAndSortTopic(pMnode, subscribe.topicNames));
765✔
667
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY,
764✔
668
                          (unSubscribe ? TRN_CONFLICT_NOTHING :TRN_CONFLICT_DB_INSIDE),
669
                          pMsg, "subscribe");
670
  MND_TMQ_NULL_CHECK(pTrans);
764!
671

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

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

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

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

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

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

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

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

718
  terrno = TSDB_CODE_SUCCESS;
2,171✔
719

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

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

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

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

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

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

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

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

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

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

780
  tmsgUpdateDnodeEpSet(&pConsumer->ep);
1,858✔
781

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

790
  return pRow;
1,858✔
791
}
792

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

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

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

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

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

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

858
  return existing;
436✔
859
}
860

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

868
  taosWLockLatch(&pOldConsumer->lock);
1,132✔
869

870
  if (pNewConsumer->updateType == CONSUMER_UPDATE_SUB) {
1,132✔
871
    TSWAP(pOldConsumer->rebNewTopics, pNewConsumer->rebNewTopics);
327✔
872
    TSWAP(pOldConsumer->rebRemovedTopics, pNewConsumer->rebRemovedTopics);
327✔
873
    TSWAP(pOldConsumer->assignedTopics, pNewConsumer->assignedTopics);
327✔
874

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

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

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

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

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

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

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

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

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

960
void mndReleaseConsumer(SMnode *pMnode, SMqConsumerObj *pConsumer) {
46,109✔
961
  if (pMnode == NULL || pConsumer == NULL) {
46,109!
962
    return;
28,665✔
963
  }
964
  SSdb *pSdb = pMnode->pSdb;
17,444✔
965
  sdbRelease(pSdb, pConsumer);
17,444✔
966
}
967

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1117
  pShow->numOfRows += numOfRows;
79✔
1118
  return numOfRows;
79✔
1119

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

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

1132
const char *mndConsumerStatusName(int status) {
10,573✔
1133
  switch (status) {
10,573!
1134
    case MQ_CONSUMER_STATUS_READY:
4,560✔
1135
      return "ready";
4,560✔
1136
//    case MQ_CONSUMER_STATUS_LOST:
1137
//      return "lost";
1138
    case MQ_CONSUMER_STATUS_REBALANCE:
6,013✔
1139
      return "rebalancing";
6,013✔
1140
    default:
×
1141
      return "unknown";
×
1142
  }
1143
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc