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

taosdata / TDengine / #4143

24 May 2025 03:30AM UTC coverage: 32.868% (-29.4%) from 62.238%
#4143

push

travis-ci

web-flow
test: migrate stream cases (#31164)

76401 of 312956 branches covered (24.41%)

Branch coverage included in aggregate %.

128686 of 311012 relevant lines covered (41.38%)

579734.08 hits per line

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

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

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

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

69
void mndCleanupConsumer(SMnode *pMnode) {}
15✔
70

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

79
  *(int64_t*)msg = consumerId;
6✔
80
  SRpcMsg rpcMsg = {
6✔
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);
6!
88
  MND_TMQ_RETURN_CHECK(tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg));
6!
89
  return code;
6✔
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) {
12✔
97
  if (pTrans == NULL || subscribe == NULL || pMnode == NULL || pUser == NULL) {
12!
98
    return TSDB_CODE_INVALID_PARA;
×
99
  }
100
  SMqTopicObj *pTopic = NULL;
12✔
101
  int32_t      code = 0;
12✔
102

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

291
  int32_t numOfTopics = taosArrayGetSize(pConsumer->currentTopics);
17✔
292

293
  rsp->topics = taosArrayInit(numOfTopics, sizeof(SMqSubTopicEp));
17✔
294
  if (rsp->topics == NULL) {
17!
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++) {
39✔
301
    char            *topic = taosArrayGetP(pConsumer->currentTopics, i);
22✔
302
    SMqSubscribeObj *pSub = NULL;
22✔
303
    char  key[TSDB_SUBSCRIBE_KEY_LEN] = {0};
22✔
304
    (void)snprintf(key, TSDB_SUBSCRIBE_KEY_LEN, "%s%s%s", pConsumer->cgroup, TMQ_SEPARATOR, topic);
22✔
305
    int32_t code = mndAcquireSubscribeByKey(pMnode, key, &pSub);
22✔
306
    if (code != 0) {
22!
307
      continue;
×
308
    }
309
    taosRLockLatch(&pSub->lock);
22✔
310

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

314
    // 2.1 fetch topic schema
315
    SMqTopicObj *pTopic = NULL;
22✔
316
    code = mndAcquireTopic(pMnode, topic, &pTopic);
22✔
317
    if (code != TDB_CODE_SUCCESS) {
22!
318
      taosRUnLockLatch(&pSub->lock);
×
319
      mndReleaseSubscribe(pMnode, pSub);
×
320
      continue;
×
321
    }
322
    taosRLockLatch(&pTopic->lock);
22✔
323
    tstrncpy(topicEp.db, pTopic->db, TSDB_DB_FNAME_LEN);
22✔
324
    topicEp.schema.nCols = pTopic->schema.nCols;
22✔
325
    if (topicEp.schema.nCols) {
22!
326
      topicEp.schema.pSchema = taosMemoryCalloc(topicEp.schema.nCols, sizeof(SSchema));
22!
327
      if (topicEp.schema.pSchema == NULL) {
22!
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));
22✔
335
    }
336
    taosRUnLockLatch(&pTopic->lock);
22✔
337
    mndReleaseTopic(pMnode, pTopic);
22✔
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));
22✔
341
    if (pConsumerEp == NULL) {
22!
342
      taosRUnLockLatch(&pConsumer->lock);
×
343
      taosRUnLockLatch(&pSub->lock);
×
344
      mndReleaseSubscribe(pMnode, pSub);
×
345
      return TSDB_CODE_OUT_OF_MEMORY;
×
346
    }
347
    int32_t vgNum = taosArrayGetSize(pConsumerEp->vgs);
22✔
348
    topicEp.vgs = taosArrayInit(vgNum, sizeof(SMqSubVgEp));
22✔
349
    if (topicEp.vgs == NULL) {
22!
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++) {
41✔
357
      SMqVgEp *pVgEp = taosArrayGetP(pConsumerEp->vgs, j);
19✔
358
      if (pVgEp == NULL) {
19!
359
        continue;
×
360
      }
361
      if (epoch == -1) {
19!
362
        SVgObj *pVgroup = mndAcquireVgroup(pMnode, pVgEp->vgId);
×
363
        if (pVgroup) {
×
364
          pVgEp->epSet = mndGetVgroupEpset(pMnode, pVgroup);
×
365
          mndReleaseVgroup(pMnode, pVgroup);
×
366
        }
367
      }
368
      SMqSubVgEp vgEp = {.epSet = pVgEp->epSet, .vgId = pVgEp->vgId, .offset = -1};
19✔
369
      if (taosArrayPush(topicEp.vgs, &vgEp) == NULL) {
38!
370
        taosArrayDestroy(topicEp.vgs);
×
371
        taosRUnLockLatch(&pConsumer->lock);
×
372
        taosRUnLockLatch(&pSub->lock);
×
373
        mndReleaseSubscribe(pMnode, pSub);
×
374
        return terrno;
×
375
      }
376
    }
377
    if (taosArrayPush(rsp->topics, &topicEp) == NULL) {
44!
378
      taosArrayDestroy(topicEp.vgs);
×
379
      taosRUnLockLatch(&pConsumer->lock);
×
380
      taosRUnLockLatch(&pSub->lock);
×
381
      mndReleaseSubscribe(pMnode, pSub);
×
382
      return terrno;
×
383
    }
384
    taosRUnLockLatch(&pSub->lock);
22✔
385
    mndReleaseSubscribe(pMnode, pSub);
22✔
386
  }
387
  taosRUnLockLatch(&pConsumer->lock);
17✔
388
  return 0;
17✔
389
}
390

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

403
  SMqRspHead *pHead = buf;
32✔
404

405
  pHead->mqMsgType = TMQ_MSG_TYPE__EP_RSP;
32✔
406
  pHead->epoch = serverEpoch;
32✔
407
  pHead->consumerId = consumerId;
32✔
408
  pHead->walsver = 0;
32✔
409
  pHead->walever = 0;
32✔
410

411
  void *abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
32✔
412
  if (tEncodeSMqAskEpRsp(&abuf, rsp) < 0) {
32!
413
    rpcFreeCont(buf);
×
414
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
415
  }
416

417
  // send rsp
418
  pMsg->info.rsp = buf;
32✔
419
  pMsg->info.rspLen = tlen;
32✔
420
  return code;
32✔
421
}
422

423
static int32_t mndProcessAskEpReq(SRpcMsg *pMsg) {
72✔
424
  if (pMsg == NULL) {
72!
425
    return TSDB_CODE_INVALID_PARA;
×
426
  }
427
  SMnode     *pMnode = pMsg->info.node;
72✔
428
  SMqAskEpReq req = {0};
72✔
429
  SMqAskEpRsp rsp = {0};
72✔
430
  int32_t     code = 0;
72✔
431
  SMqConsumerObj *pConsumer = NULL;
72✔
432
  PRINT_LOG_START
72!
433

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

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

452
  int32_t epoch = req.epoch;
32✔
453
  int32_t serverEpoch = atomic_load_32(&pConsumer->epoch);
32✔
454

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

462
  code = buildAskEpRsp(pMsg, &rsp, serverEpoch, consumerId);
32✔
463

464
END:
72✔
465
  tDeleteSMqAskEpRsp(&rsp);
466
  mndReleaseConsumer(pMnode, pConsumer);
72✔
467
  PRINT_LOG_END(code);
72!
468
  return code;
72✔
469
}
470

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

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

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

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

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

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

576
END:
6✔
577
  return code;
6✔
578
}
579

580
static int32_t checkAndSortTopic(SMnode *pMnode, SArray *pTopicList){
12✔
581
  if (pTopicList == NULL || pMnode == NULL) {
12!
582
    return TSDB_CODE_INVALID_PARA;
×
583
  }
584
  taosArraySort(pTopicList, taosArrayCompareString);
12✔
585
  taosArrayRemoveDuplicate(pTopicList, taosArrayCompareString, freeItem);
12✔
586

587
  int32_t newTopicNum = taosArrayGetSize(pTopicList);
12✔
588
  for (int i = 0; i < newTopicNum; i++) {
24✔
589
    int32_t gNum = mndGetGroupNumByTopic(pMnode, (const char *)taosArrayGetP(pTopicList, i));
12✔
590
    if (gNum >= MND_MAX_GROUP_PER_TOPIC) {
12!
591
      return TSDB_CODE_TMQ_GROUP_OUT_OF_RANGE;
×
592
    }
593
  }
594
  return 0;
12✔
595
}
596

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

611
    MND_TMQ_RETURN_CHECK(tNewSMqConsumerObj(consumerId, cgroup, CONSUMER_INSERT_SUB, NULL, subscribe, &pConsumerNew));
6!
612
  } else {
613
    int32_t status = atomic_load_32(&pExistedConsumer->status);
6✔
614

615
    mInfo("receive tmq subscribe request from existed consumer:0x%" PRIx64
6!
616
              ",cgroup:%s, current status:%d(%s), subscribe topic num: %d",
617
          consumerId, subscribe->cgroup, status, mndConsumerStatusName(status),
618
          (int32_t)taosArrayGetSize(subscribe->topicNames));
619

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

633
END:
×
634
  mndReleaseConsumer(pMnode, pExistedConsumer);
×
635
  tDeleteSMqConsumerObj(pConsumerNew);
×
636
  return code;
×
637
}
638

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

649
  PRINT_LOG_START
18!
650
  SCMSubscribeReq subscribe = {0};
18✔
651
  MND_TMQ_RETURN_CHECK(tDeserializeSCMSubscribeReq(msgStr, &subscribe, pMsg->contLen));
36!
652
  bool unSubscribe = (taosArrayGetSize(subscribe.topicNames) == 0);
18✔
653
  if(unSubscribe){
18✔
654
    SMqConsumerObj *pConsumerTmp = NULL;
12✔
655
    MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, subscribe.consumerId, &pConsumerTmp));
18!
656
    if (taosArrayGetSize(pConsumerTmp->assignedTopics) == 0){
12✔
657
      mndReleaseConsumer(pMnode, pConsumerTmp);
6✔
658
      goto END;
6✔
659
    }
660
    mndReleaseConsumer(pMnode, pConsumerTmp);
6✔
661
  }
662
  MND_TMQ_RETURN_CHECK(checkAndSortTopic(pMnode, subscribe.topicNames));
12!
663
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY,
12✔
664
                          (unSubscribe ? TRN_CONFLICT_NOTHING :TRN_CONFLICT_DB_INSIDE),
665
                          pMsg, "subscribe");
666
  MND_TMQ_NULL_CHECK(pTrans);
12!
667

668
  MND_TMQ_RETURN_CHECK(validateTopics(pTrans, &subscribe, pMnode, pMsg->info.conn.user));
12!
669
  MND_TMQ_RETURN_CHECK(buildSubConsumer(pMnode, &subscribe, &pConsumerNew));
12!
670
  MND_TMQ_RETURN_CHECK(mndSetConsumerCommitLogs(pTrans, pConsumerNew));
12!
671
  MND_TMQ_RETURN_CHECK(mndTransPrepare(pMnode, pTrans));
12!
672
  code = TSDB_CODE_ACTION_IN_PROGRESS;
12✔
673

674
END:
18✔
675
  mndTransDrop(pTrans);
18✔
676
  tDeleteSMqConsumerObj(pConsumerNew);
18✔
677
  taosArrayDestroyP(subscribe.topicNames, NULL);
18✔
678
  code = (code == TSDB_CODE_TMQ_NO_NEED_REBALANCE || code == TSDB_CODE_MND_CONSUMER_NOT_EXIST) ? 0 : code;
18!
679
  PRINT_LOG_END(code);
18!
680
  return code;
18✔
681
}
682

683
SSdbRaw *mndConsumerActionEncode(SMqConsumerObj *pConsumer) {
45✔
684
  if (pConsumer == NULL) {
45!
685
    return NULL;
×
686
  }
687
  int32_t code = 0;
45✔
688
  int32_t lino = 0;
45✔
689
  terrno = TSDB_CODE_OUT_OF_MEMORY;
45✔
690

691
  void   *buf = NULL;
45✔
692
  int32_t tlen = tEncodeSMqConsumerObj(NULL, pConsumer);
45✔
693
  int32_t size = sizeof(int32_t) + tlen + MND_CONSUMER_RESERVE_SIZE;
45✔
694

695
  SSdbRaw *pRaw = sdbAllocRaw(SDB_CONSUMER, MND_CONSUMER_VER_NUMBER, size);
45✔
696
  if (pRaw == NULL) goto CM_ENCODE_OVER;
45!
697

698
  buf = taosMemoryMalloc(tlen);
45!
699
  if (buf == NULL) goto CM_ENCODE_OVER;
45!
700

701
  void *abuf = buf;
45✔
702
  if(tEncodeSMqConsumerObj(&abuf, pConsumer) < 0){
45!
703
    goto CM_ENCODE_OVER;
×
704
  }
705

706
  int32_t dataPos = 0;
45✔
707
  SDB_SET_INT32(pRaw, dataPos, tlen, CM_ENCODE_OVER);
45!
708
  SDB_SET_BINARY(pRaw, dataPos, buf, tlen, CM_ENCODE_OVER);
45!
709
  SDB_SET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_ENCODE_OVER);
45!
710
  SDB_SET_DATALEN(pRaw, dataPos, CM_ENCODE_OVER);
45!
711

712
  terrno = TSDB_CODE_SUCCESS;
45✔
713

714
CM_ENCODE_OVER:
45✔
715
  taosMemoryFreeClear(buf);
45!
716
  if (terrno != 0) {
45!
717
    mError("consumer:0x%" PRIx64 " failed to encode to raw:%p since %s", pConsumer->consumerId, pRaw, terrstr());
×
718
    sdbFreeRaw(pRaw);
×
719
    return NULL;
×
720
  }
721

722
  mTrace("consumer:0x%" PRIx64 ", encode to raw:%p, row:%p", pConsumer->consumerId, pRaw, pConsumer);
45!
723
  return pRaw;
45✔
724
}
725

726
SSdbRow *mndConsumerActionDecode(SSdbRaw *pRaw) {
45✔
727
  if (pRaw == NULL) {
45!
728
    return NULL;
×
729
  }
730
  int32_t         code = 0;
45✔
731
  int32_t         lino = 0;
45✔
732
  SSdbRow        *pRow = NULL;
45✔
733
  SMqConsumerObj *pConsumer = NULL;
45✔
734
  void           *buf = NULL;
45✔
735

736
  terrno = 0;
45✔
737
  int8_t sver = 0;
45✔
738
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
45!
739
    goto CM_DECODE_OVER;
×
740
  }
741

742
  if (sver < 1 || sver > MND_CONSUMER_VER_NUMBER) {
45!
743
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
744
    goto CM_DECODE_OVER;
×
745
  }
746

747
  pRow = sdbAllocRow(sizeof(SMqConsumerObj));
45✔
748
  if (pRow == NULL) {
45!
749
    goto CM_DECODE_OVER;
×
750
  }
751

752
  pConsumer = sdbGetRowObj(pRow);
45✔
753
  if (pConsumer == NULL) {
45!
754
    goto CM_DECODE_OVER;
×
755
  }
756

757
  int32_t dataPos = 0;
45✔
758
  int32_t len;
759
  SDB_GET_INT32(pRaw, dataPos, &len, CM_DECODE_OVER);
45!
760
  buf = taosMemoryMalloc(len);
45!
761
  if (buf == NULL) {
45!
762
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
763
    goto CM_DECODE_OVER;
×
764
  }
765

766
  SDB_GET_BINARY(pRaw, dataPos, buf, len, CM_DECODE_OVER);
45!
767
  SDB_GET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_DECODE_OVER);
45!
768

769
  if (tDecodeSMqConsumerObj(buf, pConsumer, sver) == NULL) {
45!
770
    terrno = TSDB_CODE_OUT_OF_MEMORY;  // TODO set correct error code
×
771
    goto CM_DECODE_OVER;
×
772
  }
773

774
  tmsgUpdateDnodeEpSet(&pConsumer->ep);
45✔
775

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

784
  return pRow;
45✔
785
}
786

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

797
static int32_t mndConsumerActionDelete(SSdb *pSdb, SMqConsumerObj *pConsumer) {
45✔
798
  if (pConsumer == NULL) {
45!
799
    return TSDB_CODE_INVALID_PARA;
×
800
  }
801
  mInfo("consumer:0x%" PRIx64 " perform delete action, status:(%d)%s", pConsumer->consumerId, pConsumer->status,
45!
802
        mndConsumerStatusName(pConsumer->status));
803
  tClearSMqConsumerObj(pConsumer);
45✔
804
  return 0;
45✔
805
}
806

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

819
// remove from topic list
820
static void removeFromTopicList(SArray *topicList, const char *pTopic, int64_t consumerId, char *type) {
36✔
821
  if (topicList == NULL || pTopic == NULL) {
36!
822
    return;
×
823
  }
824
  int32_t size = taosArrayGetSize(topicList);
36✔
825
  for (int32_t i = 0; i < size; i++) {
48!
826
    char *p = taosArrayGetP(topicList, i);
48✔
827
    if (strcmp(pTopic, p) == 0) {
48✔
828
      taosArrayRemove(topicList, i);
36✔
829
      taosMemoryFree(p);
36!
830

831
      mInfo("tmq rebalance consumer:0x%" PRIx64 " remove topic:%s in the %s topic list, remain newTopics:%d",
36!
832
            consumerId, pTopic, type, (int)taosArrayGetSize(topicList));
833
      break;
36✔
834
    }
835
  }
836
}
837

838
static bool existInCurrentTopicList(const SMqConsumerObj *pConsumer, const char *pTopic) {
12✔
839
  if (pConsumer == NULL || pTopic == NULL) {
12!
840
    return false;
×
841
  }
842
  bool    existing = false;
12✔
843
  int32_t size = taosArrayGetSize(pConsumer->currentTopics);
12✔
844
  for (int32_t i = 0; i < size; i++) {
18✔
845
    char *topic = taosArrayGetP(pConsumer->currentTopics, i);
6✔
846
    if (topic && strcmp(topic, pTopic) == 0) {
6!
847
      existing = true;
×
848
      break;
×
849
    }
850
  }
851

852
  return existing;
12✔
853
}
854

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

862
  taosWLockLatch(&pOldConsumer->lock);
33✔
863

864
  if (pNewConsumer->updateType == CONSUMER_UPDATE_SUB) {
33✔
865
    TSWAP(pOldConsumer->rebNewTopics, pNewConsumer->rebNewTopics);
6✔
866
    TSWAP(pOldConsumer->rebRemovedTopics, pNewConsumer->rebRemovedTopics);
6✔
867
    TSWAP(pOldConsumer->assignedTopics, pNewConsumer->assignedTopics);
6✔
868

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

898
    int32_t status = pOldConsumer->status;
12✔
899
//    updateConsumerStatus(pOldConsumer);
900
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
12!
901
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
6✔
902
    }
903

904
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
12✔
905
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
12✔
906

907
    mInfo("tmq rebalance consumer:0x%" PRIx64 " rebalance update add, state (%d)%s -> (%d)%s, new epoch:%d, reb-time:%" PRId64
12!
908
          ", current topics:%d, newTopics:%d, removeTopics:%d",
909
          pOldConsumer->consumerId, status, mndConsumerStatusName(status), pOldConsumer->status,
910
          mndConsumerStatusName(pOldConsumer->status), pOldConsumer->epoch, pOldConsumer->rebalanceTime,
911
          (int)taosArrayGetSize(pOldConsumer->currentTopics), (int)taosArrayGetSize(pOldConsumer->rebNewTopics),
912
          (int)taosArrayGetSize(pOldConsumer->rebRemovedTopics));
913

914
  } else if (pNewConsumer->updateType == CONSUMER_REMOVE_REB) {
12!
915
    char *topic = taosArrayGetP(pNewConsumer->rebRemovedTopics, 0);
12✔
916
    if (topic == NULL){
12!
917
      return TSDB_CODE_TMQ_INVALID_MSG;
×
918
    }
919
    removeFromTopicList(pOldConsumer->rebRemovedTopics, topic, pOldConsumer->consumerId, "remove");
12✔
920
    removeFromTopicList(pOldConsumer->currentTopics, topic, pOldConsumer->consumerId, "current");
12✔
921

922
    int32_t status = pOldConsumer->status;
12✔
923
//    updateConsumerStatus(pOldConsumer);
924
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
12!
925
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
6✔
926
    }
927
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
12✔
928
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
12✔
929

930
    mInfo("tmq rebalanceconsumer:0x%" PRIx64 " rebalance update remove, state (%d)%s -> (%d)%s, new epoch:%d, reb-time:%" PRId64
12!
931
          ", current topics:%d, newTopics:%d, removeTopics:%d",
932
          pOldConsumer->consumerId, status, mndConsumerStatusName(status), pOldConsumer->status,
933
          mndConsumerStatusName(pOldConsumer->status), pOldConsumer->epoch, pOldConsumer->rebalanceTime,
934
          (int)taosArrayGetSize(pOldConsumer->currentTopics), (int)taosArrayGetSize(pOldConsumer->rebNewTopics),
935
          (int)taosArrayGetSize(pOldConsumer->rebRemovedTopics));
936
  }
937

938
  taosWUnLockLatch(&pOldConsumer->lock);
33✔
939
  return 0;
33✔
940
}
941

942
int32_t mndAcquireConsumer(SMnode *pMnode, int64_t consumerId, SMqConsumerObj** pConsumer) {
138✔
943
  if (pMnode == NULL || pConsumer == NULL) {
138!
944
    return TSDB_CODE_INVALID_PARA;
×
945
  }
946
  SSdb           *pSdb = pMnode->pSdb;
138✔
947
  *pConsumer = sdbAcquire(pSdb, SDB_CONSUMER, &consumerId);
138✔
948
  if (*pConsumer == NULL) {
138✔
949
    return TSDB_CODE_MND_CONSUMER_NOT_EXIST;
6✔
950
  }
951
  return 0;
132✔
952
}
953

954
void mndReleaseConsumer(SMnode *pMnode, SMqConsumerObj *pConsumer) {
162✔
955
  if (pMnode == NULL || pConsumer == NULL) {
162!
956
    return;
6✔
957
  }
958
  SSdb *pSdb = pMnode->pSdb;
156✔
959
  sdbRelease(pSdb, pConsumer);
156✔
960
}
961

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

974
  while (numOfRows < rowsCapacity) {
2!
975
    pShow->pIter = sdbFetch(pSdb, SDB_CONSUMER, pShow->pIter, (void **)&pConsumer);
2✔
976
    if (pShow->pIter == NULL) {
2✔
977
      break;
1✔
978
    }
979

980
    if (taosArrayGetSize(pConsumer->assignedTopics) == 0) {
1!
981
      mInfo("showing consumer:0x%" PRIx64 " no assigned topic, skip", pConsumer->consumerId);
1!
982
      sdbRelease(pSdb, pConsumer);
1✔
983
      continue;
1✔
984
    }
985

986
    taosRLockLatch(&pConsumer->lock);
×
987
    mInfo("showing consumer:0x%" PRIx64, pConsumer->consumerId);
×
988

989
    int32_t topicSz = taosArrayGetSize(pConsumer->assignedTopics);
×
990
    bool    hasTopic = true;
×
991
    if (topicSz == 0) {
×
992
      hasTopic = false;
×
993
      topicSz = 1;
×
994
    }
995

996
    if (numOfRows + topicSz > rowsCapacity) {
×
997
      MND_TMQ_RETURN_CHECK(blockDataEnsureCapacity(pBlock, numOfRows + topicSz));
×
998
    }
999

1000
    for (int32_t i = 0; i < topicSz; i++) {
×
1001
      SColumnInfoData *pColInfo = NULL;
×
1002
      int32_t          cols = 0;
×
1003

1004
      // consumer id
1005
      char consumerIdHex[TSDB_CONSUMER_ID_LEN + VARSTR_HEADER_SIZE] = {0};
×
1006
      (void)snprintf(varDataVal(consumerIdHex), TSDB_CONSUMER_ID_LEN, "0x%" PRIx64, pConsumer->consumerId);
×
1007
      varDataSetLen(consumerIdHex, strlen(varDataVal(consumerIdHex)));
×
1008

1009
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1010
      MND_TMQ_NULL_CHECK(pColInfo);
×
1011
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)consumerIdHex, false));
×
1012

1013
      // consumer group
1014
      char cgroup[TSDB_CGROUP_LEN + VARSTR_HEADER_SIZE] = {0};
×
1015
      STR_TO_VARSTR(cgroup, pConsumer->cgroup);
×
1016

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

1021
      // client id
1022
      char clientId[TSDB_CLIENT_ID_LEN + VARSTR_HEADER_SIZE] = {0};
×
1023
      STR_TO_VARSTR(clientId, pConsumer->clientId);
×
1024

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

1029
      // user
1030
      char user[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
×
1031
      STR_TO_VARSTR(user, pConsumer->user);
×
1032

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

1037
      // fqdn
1038
      char fqdn[TSDB_FQDN_LEN + VARSTR_HEADER_SIZE] = {0};
×
1039
      STR_TO_VARSTR(fqdn, pConsumer->fqdn);
×
1040

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

1045
      // status
1046
      const char *pStatusName = mndConsumerStatusName(pConsumer->status);
×
1047
      status = taosMemoryCalloc(1, pShow->pMeta->pSchemas[cols].bytes);
×
1048
      MND_TMQ_NULL_CHECK(status);
×
1049
      STR_TO_VARSTR(status, pStatusName);
×
1050

1051
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1052
      MND_TMQ_NULL_CHECK(pColInfo);
×
1053
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)status, false));
×
1054
      taosMemoryFreeClear(status);
×
1055

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

1068
      // up time
1069
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1070
      MND_TMQ_NULL_CHECK(pColInfo);
×
1071
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->createTime, false));
×
1072

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

1078
      // rebalance time
1079
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1080
      MND_TMQ_NULL_CHECK(pColInfo);
×
1081
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->rebalanceTime, pConsumer->rebalanceTime == 0));
×
1082

1083
      char         buf[TSDB_OFFSET_LEN] = {0};
×
1084
      STqOffsetVal pVal = {.type = pConsumer->resetOffsetCfg};
×
1085
      tFormatOffset(buf, TSDB_OFFSET_LEN, &pVal);
×
1086

1087
      parasStr = taosMemoryCalloc(1, pShow->pMeta->pSchemas[cols].bytes);
×
1088
      MND_TMQ_NULL_CHECK(parasStr);
×
1089
      (void)snprintf(varDataVal(parasStr), pShow->pMeta->pSchemas[cols].bytes - VARSTR_HEADER_SIZE, "tbname:%d,commit:%d,interval:%dms,reset:%s", pConsumer->withTbName,
×
1090
              pConsumer->autoCommit, pConsumer->autoCommitInterval, buf);
×
1091
      varDataSetLen(parasStr, strlen(varDataVal(parasStr)));
×
1092

1093
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1094
      MND_TMQ_NULL_CHECK(pColInfo);
×
1095
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)parasStr, false));
×
1096
      taosMemoryFreeClear(parasStr);
×
1097

1098
      // rebalance time
1099
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1100
      MND_TMQ_NULL_CHECK(pColInfo);
×
1101
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->pollTime, pConsumer->pollTime == 0));
×
1102
      numOfRows++;
×
1103
    }
1104

1105
    taosRUnLockLatch(&pConsumer->lock);
×
1106
    sdbRelease(pSdb, pConsumer);
×
1107

1108
    pBlock->info.rows = numOfRows;
×
1109
  }
1110

1111
  pShow->numOfRows += numOfRows;
1✔
1112
  return numOfRows;
1✔
1113

1114
END:
×
1115
  taosMemoryFreeClear(status);
×
1116
  taosMemoryFreeClear(parasStr);
×
1117
  return code;
×
1118
}
1119

1120
static void mndCancelGetNextConsumer(SMnode *pMnode, void *pIter) {
×
1121
  if (pMnode == NULL || pIter == NULL) return;
×
1122
  SSdb *pSdb = pMnode->pSdb;
×
1123
  sdbCancelFetchByType(pSdb, pIter, SDB_CONSUMER);
×
1124
}
1125

1126
const char *mndConsumerStatusName(int status) {
175✔
1127
  switch (status) {
175!
1128
    case MQ_CONSUMER_STATUS_READY:
42✔
1129
      return "ready";
42✔
1130
//    case MQ_CONSUMER_STATUS_LOST:
1131
//      return "lost";
1132
    case MQ_CONSUMER_STATUS_REBALANCE:
133✔
1133
      return "rebalancing";
133✔
1134
    default:
×
1135
      return "unknown";
×
1136
  }
1137
}
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