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

taosdata / TDengine / #4986

15 Mar 2026 08:32AM UTC coverage: 37.305% (-31.3%) from 68.601%
#4986

push

travis-ci

tomchon
test: keep docs and unit test

125478 of 336361 relevant lines covered (37.3%)

1134847.06 hits per line

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

1.73
/source/dnode/mnode/impl/src/mndConsumer.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define _DEFAULT_SOURCE
17
#include "mndConsumer.h"
18
#include "mndDb.h"
19
#include "mndPrivilege.h"
20
#include "mndShow.h"
21
#include "mndSubscribe.h"
22
#include "mndTopic.h"
23
#include "mndTrans.h"
24
#include "mndUser.h"
25
#include "mndVgroup.h"
26
#include "tcompare.h"
27
#include "tname.h"
28

29
#define MND_CONSUMER_VER_NUMBER   3
30
#define MND_CONSUMER_RESERVE_SIZE 64
31

32
#define MND_MAX_GROUP_PER_TOPIC 100
33

34
static int32_t mndConsumerActionInsert(SSdb *pSdb, SMqConsumerObj *pConsumer);
35
static int32_t mndConsumerActionDelete(SSdb *pSdb, SMqConsumerObj *pConsumer);
36
static int32_t mndConsumerActionUpdate(SSdb *pSdb, SMqConsumerObj *pOldConsumer, SMqConsumerObj *pNewConsumer);
37
static int32_t mndRetrieveConsumer(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
38
static void    mndCancelGetNextConsumer(SMnode *pMnode, void *pIter);
39

40
static int32_t mndProcessSubscribeReq(SRpcMsg *pMsg);
41
static int32_t mndProcessAskEpReq(SRpcMsg *pMsg);
42
static int32_t mndProcessMqHbReq(SRpcMsg *pMsg);
43
static int32_t mndProcessConsumerClearMsg(SRpcMsg *pMsg);
44

45
int32_t mndInitConsumer(SMnode *pMnode) {
16✔
46
  SSdbTable table = {
16✔
47
      .sdbType = SDB_CONSUMER,
48
      .keyType = SDB_KEY_INT64,
49
      .encodeFp = (SdbEncodeFp)mndConsumerActionEncode,
50
      .decodeFp = (SdbDecodeFp)mndConsumerActionDecode,
51
      .insertFp = (SdbInsertFp)mndConsumerActionInsert,
52
      .updateFp = (SdbUpdateFp)mndConsumerActionUpdate,
53
      .deleteFp = (SdbDeleteFp)mndConsumerActionDelete,
54
  };
55

56
  if (pMnode == NULL){
16✔
57
    return TSDB_CODE_INVALID_PARA;
×
58
  }
59
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_SUBSCRIBE, mndProcessSubscribeReq);
16✔
60
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_HB, mndProcessMqHbReq);
16✔
61
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_ASK_EP, mndProcessAskEpReq);
16✔
62
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_LOST_CONSUMER_CLEAR, mndProcessConsumerClearMsg);
16✔
63

64
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndRetrieveConsumer);
16✔
65
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndCancelGetNextConsumer);
16✔
66

67
  return sdbSetTable(pMnode->pSdb, table);
16✔
68
}
69

70
void mndCleanupConsumer(SMnode *pMnode) {}
16✔
71

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

82
  *(int64_t*)msg = consumerId;
×
83
  SRpcMsg rpcMsg = {
×
84
      .msgType = msgType,
85
      .pCont = msg,
86
      .contLen = sizeof(int64_t),
87
      .info = *info,
88
  };
89

90
  mInfo("mndSendConsumerMsg type:%d consumer:0x%" PRIx64, msgType, consumerId);
×
91
  MND_TMQ_RETURN_CHECK(tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg));
×
92

93
END:
×
94
  PRINT_LOG_END
×
95
  return code;
×
96
}
97

98
static int32_t validateOneTopic(STrans* pTrans,char *pOneTopic, SCMSubscribeReq *subscribe, SMnode *pMnode, const char *user, const char* token) {
×
99
  int32_t      code = 0;
×
100
  int32_t lino = 0;
×
101
  SMqTopicObj *pTopic = NULL;
×
102

103
  PRINT_LOG_START
×
104
  MND_TMQ_RETURN_CHECK(mndAcquireTopic(pMnode, pOneTopic, &pTopic));
×
105
  taosRLockLatch(&pTopic->lock);
×
106

107
  MND_TMQ_RETURN_CHECK(mndCheckTopicPrivilege(pMnode, user, token, MND_OPER_SUBSCRIBE, pTopic));
×
108
  MND_TMQ_RETURN_CHECK(grantCheckExpire(TSDB_GRANT_SUBSCRIPTION));
×
109

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

134
END:
×
135
  PRINT_LOG_END
×
136
  if (pTopic != NULL) {
×
137
    taosRUnLockLatch(&pTopic->lock);
×
138
  }
139
  mndReleaseTopic(pMnode, pTopic);
×
140
  return code;
×
141
}
142

143
static int32_t validateTopics(STrans* pTrans, SCMSubscribeReq *subscribe, SMnode *pMnode, const char *user, const char* token) {
×
144
  if (pTrans == NULL || subscribe == NULL || pMnode == NULL || user == NULL) {
×
145
    return TSDB_CODE_INVALID_PARA;
×
146
  }
147
  int32_t      code = 0;
×
148
  int32_t lino = 0;
×
149

150
  PRINT_LOG_START
×
151
  int32_t numOfTopics = taosArrayGetSize(subscribe->topicNames);
×
152
  for (int32_t i = 0; i < numOfTopics; i++) {
×
153
    char *pOneTopic = taosArrayGetP(subscribe->topicNames, i);
×
154
    MND_TMQ_RETURN_CHECK(validateOneTopic(pTrans, pOneTopic, subscribe, pMnode, user, token));
×
155
  }
156

157
END:
×
158
  PRINT_LOG_END
×
159
  return code;
×
160
}
161

162
static int32_t mndProcessConsumerClearMsg(SRpcMsg *pMsg) {
×
163
  if (pMsg == NULL || pMsg->pCont == NULL) {
×
164
    return TSDB_CODE_INVALID_PARA;
×
165
  }
166
  int32_t              code = 0;
×
167
  int32_t              lino = 0;
×
168
  SMnode              *pMnode = pMsg->info.node;
×
169
  SMqConsumerClearMsg *pClearMsg = pMsg->pCont;
×
170
  SMqConsumerObj      *pConsumerNew = NULL;
×
171
  STrans              *pTrans = NULL;
×
172
  SMqConsumerObj      *pConsumer = NULL;
×
173

174
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, pClearMsg->consumerId, &pConsumer));
×
175
  taosRLockLatch(&pConsumer->lock);
×
176
  mInfo("consumer:0x%" PRIx64 " needs to be cleared, status %s", pClearMsg->consumerId,
×
177
        mndConsumerStatusName(pConsumer->status));
178

179
  MND_TMQ_RETURN_CHECK(tNewSMqConsumerObj(pConsumer->consumerId, pConsumer->cgroup, CONSUMER_CLEAR, NULL, NULL, &pConsumerNew));
×
180

181
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, pMsg, "clear-csm");
×
182
  MND_TMQ_NULL_CHECK(pTrans);
×
183
  MND_TMQ_RETURN_CHECK(mndSetConsumerDropLogs(pTrans, pConsumerNew));
×
184
  code = mndTransPrepare(pMnode, pTrans);
×
185

186
END:
×
187
  PRINT_LOG_END
×
188
  if (pConsumer != NULL) {
×
189
    taosRUnLockLatch(&pConsumer->lock);
×
190
  }
191
  mndReleaseConsumer(pMnode, pConsumer);
×
192
  tDeleteSMqConsumerObj(pConsumerNew);
×
193
  mndTransDrop(pTrans);
×
194
  return code;
×
195
}
196

197
static void checkOnePrivilege(const char* topic, SMnode *pMnode, SMqHbRsp *rsp, const char *user, const char* token) {
×
198
  int32_t code = 0;
×
199
  int32_t lino = 0;
×
200
  SMqTopicObj *pTopic = NULL;
×
201
  PRINT_LOG_START
×
202
  MND_TMQ_RETURN_CHECK(mndAcquireTopic(pMnode, topic, &pTopic));
×
203
  taosRLockLatch(&pTopic->lock);
×
204
  STopicPrivilege *data = taosArrayReserve(rsp->topicPrivileges, 1);
×
205
  MND_TMQ_NULL_CHECK(data);
×
206
  tstrncpy(data->topic, topic, TSDB_TOPIC_FNAME_LEN);
×
207
  if (mndCheckTopicPrivilege(pMnode, user, token, MND_OPER_SUBSCRIBE, pTopic) != 0 ||
×
208
      grantCheckExpire(TSDB_GRANT_SUBSCRIPTION) < 0) {
×
209
    data->noPrivilege = 1;
×
210
  } else {
211
    data->noPrivilege = 0;
×
212
  }
213

214
END:
×
215
  PRINT_LOG_END
×
216
  if (pTopic != NULL) {
×
217
    taosRUnLockLatch(&pTopic->lock);
×
218
  }
219
  mndReleaseTopic(pMnode, pTopic);
×
220
}
×
221

222
static int32_t checkPrivilege(SMnode *pMnode, SMqConsumerObj *pConsumer, SMqHbRsp *rsp, const char *user, const char* token) {
×
223
  if (pMnode == NULL || pConsumer == NULL || rsp == NULL || user == NULL) {
×
224
    return TSDB_CODE_INVALID_PARA;
×
225
  }
226
  int32_t code = 0;
×
227
  int32_t lino = 0;
×
228
  PRINT_LOG_START
×
229

230
  rsp->topicPrivileges = taosArrayInit(taosArrayGetSize(pConsumer->currentTopics), sizeof(STopicPrivilege));
×
231
  MND_TMQ_NULL_CHECK(rsp->topicPrivileges);
×
232
  for (int32_t i = 0; i < taosArrayGetSize(pConsumer->currentTopics); i++) {
×
233
    char        *topic = taosArrayGetP(pConsumer->currentTopics, i);
×
234
    checkOnePrivilege(topic, pMnode, rsp, user, token);
×
235
  }
236

237
END:
×
238
  PRINT_LOG_END
×
239
  return code;
×
240
}
241

242
static void storeOffsetRows(SMnode *pMnode, SMqHbReq *req, SMqConsumerObj *pConsumer){
×
243
  if (pMnode == NULL || req == NULL || pConsumer == NULL){
×
244
    return;
×
245
  }
246
  for (int i = 0; i < taosArrayGetSize(req->topics); i++) {
×
247
    TopicOffsetRows *data = taosArrayGet(req->topics, i);
×
248
    if (data == NULL){
×
249
      continue;
×
250
    }
251
    mInfo("heartbeat report offset rows.%s:%s", pConsumer->cgroup, data->topicName);
×
252

253
    SMqSubscribeObj *pSub = NULL;
×
254
    char  key[TSDB_SUBSCRIBE_KEY_LEN] = {0};
×
255
    (void)snprintf(key, TSDB_SUBSCRIBE_KEY_LEN, "%s%s%s", pConsumer->cgroup, TMQ_SEPARATOR, data->topicName);
×
256
    int32_t code = mndAcquireSubscribeByKey(pMnode, key, &pSub);
×
257
    if (code != 0) {
×
258
      mError("failed to acquire subscribe by key:%s, code:%d", key, code);
×
259
      continue;
×
260
    }
261
    taosWLockLatch(&pSub->lock);
×
262
    SMqConsumerEp *pConsumerEp = taosHashGet(pSub->consumerHash, &pConsumer->consumerId, sizeof(int64_t));
×
263
    if (pConsumerEp) {
×
264
      taosArrayDestroy(pConsumerEp->offsetRows);
×
265
      pConsumerEp->offsetRows = data->offsetRows;
×
266
      data->offsetRows = NULL;
×
267
    }
268
    taosWUnLockLatch(&pSub->lock);
×
269

270
    mndReleaseSubscribe(pMnode, pSub);
×
271
  }
272
}
273

274
static int32_t buildMqHbRsp(SRpcMsg *pMsg, SMqHbRsp *rsp){
×
275
  if (pMsg == NULL || rsp == NULL){
×
276
    return TSDB_CODE_INVALID_PARA;
×
277
  }
278
  int32_t tlen = tSerializeSMqHbRsp(NULL, 0, rsp);
×
279
  if (tlen <= 0){
×
280
    return TSDB_CODE_TMQ_INVALID_MSG;
×
281
  }
282
  void   *buf = rpcMallocCont(tlen);
×
283
  if (buf == NULL) {
×
284
    return terrno;
×
285
  }
286

287
  if(tSerializeSMqHbRsp(buf, tlen, rsp) <= 0){
×
288
    rpcFreeCont(buf);
×
289
    return TSDB_CODE_TMQ_INVALID_MSG;
×
290
  }
291
  pMsg->info.rsp = buf;
×
292
  pMsg->info.rspLen = tlen;
×
293
  return 0;
×
294
}
295

296
static int32_t mndProcessMqHbReq(SRpcMsg *pMsg) {
×
297
  if (pMsg == NULL) {
×
298
    return TSDB_CODE_INVALID_PARA;
×
299
  }
300
  int32_t         code = 0;
×
301
  int32_t         lino = 0;
×
302
  SMnode         *pMnode = pMsg->info.node;
×
303
  SMqHbReq        req = {0};
×
304
  SMqHbRsp        rsp = {0};
×
305
  SMqConsumerObj *pConsumer = NULL;
×
306
  PRINT_LOG_START
×
307

308
  MND_TMQ_RETURN_CHECK(tDeserializeSMqHbReq(pMsg->pCont, pMsg->contLen, &req));
×
309
  int64_t consumerId = req.consumerId;
×
310
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
×
311
  taosWLockLatch(&pConsumer->lock);
×
312
  MND_TMQ_RETURN_CHECK(checkPrivilege(pMnode, pConsumer, &rsp, RPC_MSG_USER(pMsg), RPC_MSG_TOKEN(pMsg)));
×
313
  atomic_store_32(&pConsumer->hbStatus, 0);
×
314
  mDebug("consumer:0x%" PRIx64 " receive hb pollFlag:%d pollStatus:%d", consumerId, req.pollFlag, pConsumer->pollStatus);
×
315
  if (req.pollFlag == 1){
×
316
    atomic_store_32(&pConsumer->pollStatus, 0);
×
317
    pConsumer->pollTime = taosGetTimestampMs();
×
318
  }
319

320
  storeOffsetRows(pMnode, &req, pConsumer);
×
321
  rsp.debugFlag = tqClientDebugFlag;
×
322
  code = buildMqHbRsp(pMsg, &rsp);
×
323

324
END:
×
325
  if (pConsumer != NULL) {
×
326
    taosWUnLockLatch(&pConsumer->lock);
×
327
  }
328
  tDestroySMqHbRsp(&rsp);
×
329
  mndReleaseConsumer(pMnode, pConsumer);
×
330
  tDestroySMqHbReq(&req);
×
331
  PRINT_LOG_END
×
332
  return code;
×
333
}
334

335
static int32_t processEachTopicEp(SMnode *pMnode, SMqConsumerObj *pConsumer, char *topic, SMqAskEpRsp *rsp, int32_t epoch) {
×
336
  int32_t         code = 0;
×
337
  int32_t         lino = 0;
×
338
  SMqSubscribeObj *pSub = NULL;
×
339
  SMqSubTopicEp topicEp = {0};
×
340
  char  key[TSDB_SUBSCRIBE_KEY_LEN] = {0};
×
341
  PRINT_LOG_START
×
342
  (void)snprintf(key, TSDB_SUBSCRIBE_KEY_LEN, "%s%s%s", pConsumer->cgroup, TMQ_SEPARATOR, topic);
×
343
  if(mndAcquireSubscribeByKey(pMnode, key, &pSub) != 0) {
×
344
    mWarn("%s failed to acquire subscribe by key:%s", __func__, key);
×
345
    goto END;
×
346
  }
347
  tstrncpy(topicEp.topic, topic, TSDB_TOPIC_FNAME_LEN);
×
348

349
  taosWLockLatch(&pSub->lock);
×
350
  // 2.2 iterate all vg assigned to the consumer of that topic
351
  SMqConsumerEp *pConsumerEp = taosHashGet(pSub->consumerHash, &pConsumer->consumerId, sizeof(int64_t));
×
352
  MND_TMQ_NULL_CHECK(pConsumerEp);
×
353
  int32_t vgNum = taosArrayGetSize(pConsumerEp->vgs);
×
354
  topicEp.vgs = taosArrayInit(vgNum, sizeof(SMqSubVgEp));
×
355
  MND_TMQ_NULL_CHECK(topicEp.vgs);
×
356

357
  tstrncpy(topicEp.db, pSub->dbName, TSDB_DB_FNAME_LEN);
×
358
  for (int32_t j = 0; j < vgNum; j++) {
×
359
    SMqVgEp *pVgEp = taosArrayGet(pConsumerEp->vgs, j);
×
360
    if (pVgEp == NULL) {
×
361
      continue;
×
362
    }
363
    if (epoch == -1) {
×
364
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, pVgEp->vgId);
×
365
      if (pVgroup) {
×
366
        pVgEp->epSet = mndGetVgroupEpset(pMnode, pVgroup);
×
367
        mndReleaseVgroup(pMnode, pVgroup);
×
368
      }
369
    }
370
    SMqSubVgEp vgEp = {.epSet = pVgEp->epSet, .vgId = pVgEp->vgId, .offset = -1};
×
371
    MND_TMQ_NULL_CHECK(taosArrayPush(topicEp.vgs, &vgEp));
×
372
  }
373
  MND_TMQ_NULL_CHECK(taosArrayPush(rsp->topics, &topicEp));
×
374
  topicEp.vgs = NULL;
×
375

376
END:
×
377
  if (pSub != NULL) {
×
378
    taosWUnLockLatch(&pSub->lock);
×
379
  }
380
  taosArrayDestroy(topicEp.vgs);
×
381
  mndReleaseSubscribe(pMnode, pSub);
×
382
  PRINT_LOG_END
×
383
  return code;
×
384
}
385

386
static int32_t addEpSetInfo(SMnode *pMnode, SMqConsumerObj *pConsumer, int32_t epoch, SMqAskEpRsp *rsp){
×
387
  if (pMnode == NULL || pConsumer == NULL || rsp == NULL){
×
388
    return TSDB_CODE_INVALID_PARA;
×
389
  }
390
  int32_t code = 0;
×
391
  int32_t lino = 0;
×
392
  PRINT_LOG_START
×
393

394
  int32_t numOfTopics = taosArrayGetSize(pConsumer->currentTopics);
×
395
  rsp->topics = taosArrayInit(numOfTopics, sizeof(SMqSubTopicEp));
×
396
  MND_TMQ_NULL_CHECK(rsp->topics);
×
397

398
  // handle all topics subscribed by this consumer
399
  for (int32_t i = 0; i < numOfTopics; i++) {
×
400
    char            *topic = taosArrayGetP(pConsumer->currentTopics, i);
×
401
    MND_TMQ_RETURN_CHECK(processEachTopicEp(pMnode, pConsumer, topic, rsp, epoch));
×
402
  }
403

404
END:
×
405
  PRINT_LOG_END
×
406
  return code;
×
407
}
408

409
static int32_t buildAskEpRsp(SRpcMsg *pMsg, SMqAskEpRsp *rsp, int32_t serverEpoch, int64_t consumerId){
×
410
  if (pMsg == NULL || rsp == NULL) {
×
411
    return TSDB_CODE_INVALID_PARA;
×
412
  }
413
  int32_t code = 0;
×
414
  // encode rsp
415
  int32_t tlen = sizeof(SMqRspHead) + tEncodeSMqAskEpRsp(NULL, rsp);
×
416
  void   *buf = rpcMallocCont(tlen);
×
417
  if (buf == NULL) {
×
418
    return terrno;
×
419
  }
420

421
  SMqRspHead *pHead = buf;
×
422

423
  pHead->mqMsgType = TMQ_MSG_TYPE__EP_RSP;
×
424
  pHead->epoch = serverEpoch;
×
425
  pHead->consumerId = consumerId;
×
426
  pHead->walsver = 0;
×
427
  pHead->walever = 0;
×
428

429
  void *abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
×
430
  if (tEncodeSMqAskEpRsp(&abuf, rsp) < 0) {
×
431
    rpcFreeCont(buf);
×
432
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
433
  }
434

435
  // send rsp
436
  pMsg->info.rsp = buf;
×
437
  pMsg->info.rspLen = tlen;
×
438
  return code;
×
439
}
440

441
static int32_t mndProcessAskEpReq(SRpcMsg *pMsg) {
×
442
  if (pMsg == NULL) {
×
443
    return TSDB_CODE_INVALID_PARA;
×
444
  }
445
  SMnode     *pMnode = pMsg->info.node;
×
446
  SMqAskEpReq req = {0};
×
447
  SMqAskEpRsp rsp = {0};
×
448
  int32_t     code = 0;
×
449
  int32_t     lino = 0;
×
450
  SMqConsumerObj *pConsumer = NULL;
×
451
  PRINT_LOG_START
×
452

453
  MND_TMQ_RETURN_CHECK(tDeserializeSMqAskEpReq(pMsg->pCont, pMsg->contLen, &req));
×
454
  int64_t consumerId = req.consumerId;
×
455
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
×
456
  taosRLockLatch(&pConsumer->lock);
×
457
  if (strncmp(req.cgroup, pConsumer->cgroup, tListLen(pConsumer->cgroup)) != 0) {
×
458
    mError("consumer:0x%" PRIx64 " group:%s not consistent with data in sdb, saved cgroup:%s", consumerId, req.cgroup,
×
459
           pConsumer->cgroup);
460
    code = TSDB_CODE_MND_CONSUMER_NOT_EXIST;
×
461
    goto END;
×
462
  }
463

464
  // 1. check consumer status
465
  int32_t serverEpoch = atomic_load_32(&pConsumer->epoch);
×
466
  int32_t status = atomic_load_32(&pConsumer->status);
×
467
  if (status != MQ_CONSUMER_STATUS_READY) {
×
468
    mInfo("consumer:0x%" PRIx64 " not ready, status: %s", consumerId, mndConsumerStatusName(status));
×
469
    rsp.code = TSDB_CODE_MND_CONSUMER_NOT_READY;
×
470
  } else {
471
    int32_t epoch = req.epoch;
×
472

473
    // 2. check epoch, only send ep info when epochs do not match
474
    if (epoch != serverEpoch) {
×
475
      mInfo("process ask ep, consumer:0x%" PRIx64 "(epoch %d) update with server epoch %d",
×
476
            consumerId, epoch, serverEpoch);
477
      MND_TMQ_RETURN_CHECK(addEpSetInfo(pMnode, pConsumer, epoch, &rsp));
×
478
    }
479
  }
480
  code = buildAskEpRsp(pMsg, &rsp, serverEpoch, consumerId);
×
481

482
END:
×
483
  if (pConsumer != NULL) {
×
484
    taosRUnLockLatch(&pConsumer->lock);
×
485
  }
486
  tDeleteSMqAskEpRsp(&rsp);
487
  mndReleaseConsumer(pMnode, pConsumer);
×
488
  PRINT_LOG_END
×
489
  return code;
×
490
}
491

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

508
END:
×
509
  PRINT_LOG_END
×
510
  return code;
×
511
}
512

513
int32_t mndSetConsumerCommitLogs(STrans *pTrans, SMqConsumerObj *pConsumer) {
×
514
  if (pConsumer == NULL || pTrans == NULL) {
×
515
    return TSDB_CODE_INVALID_PARA;
×
516
  }
517
  int32_t  code = 0;
×
518
  int32_t lino = 0;
×
519
  PRINT_LOG_START
×
520
  SSdbRaw *pCommitRaw = mndConsumerActionEncode(pConsumer);
×
521
  MND_TMQ_NULL_CHECK(pCommitRaw);
×
522
  code = mndTransAppendCommitlog(pTrans, pCommitRaw);
×
523
  if (code != 0) {
×
524
    sdbFreeRaw(pCommitRaw);
×
525
    goto END;
×
526
  }
527
  MND_TMQ_RETURN_CHECK(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
×
528
END:
×
529

530
  return code;
×
531
}
532

533
static void freeItem(void *param) {
×
534
  if (param == NULL) {
×
535
    return;
×
536
  }
537
  void *pItem = *(void **)param;
×
538
  if (pItem != NULL) {
×
539
    taosMemoryFree(pItem);
×
540
  }
541
}
542

543
#define ADD_TOPIC_TO_ARRAY(element, array) \
544
char *newTopicCopy = taosStrdup(element); \
545
MND_TMQ_NULL_CHECK(newTopicCopy);\
546
if (taosArrayPush(pConsumerNew->array, &newTopicCopy) == NULL){\
547
  taosMemoryFree(newTopicCopy);\
548
  code = terrno;\
549
  goto END;\
550
}
551

552
static int32_t getTopicAddDelete(SMqConsumerObj *pExistedConsumer, SMqConsumerObj *pConsumerNew){
×
553
  if (pExistedConsumer == NULL || pConsumerNew == NULL) {
×
554
    return TSDB_CODE_INVALID_PARA;
×
555
  }
556
  int32_t code = 0;
×
557
  int32_t lino = 0;
×
558
  PRINT_LOG_START
×
559
  taosRLockLatch(&pExistedConsumer->lock);
×
560

561
  pConsumerNew->rebNewTopics = taosArrayInit(0, sizeof(void *));
×
562
  MND_TMQ_NULL_CHECK(pConsumerNew->rebNewTopics);
×
563
  pConsumerNew->rebRemovedTopics = taosArrayInit(0, sizeof(void *));
×
564
  MND_TMQ_NULL_CHECK(pConsumerNew->rebRemovedTopics);
×
565

566
  int32_t newTopicNum = taosArrayGetSize(pConsumerNew->assignedTopics);
×
567
  int32_t oldTopicNum = taosArrayGetSize(pExistedConsumer->currentTopics);
×
568
  int32_t i = 0, j = 0;
×
569
  while (i < oldTopicNum || j < newTopicNum) {
×
570
    if (i >= oldTopicNum) {
×
571
      void* tmp = taosArrayGetP(pConsumerNew->assignedTopics, j);
×
572
      MND_TMQ_NULL_CHECK(tmp);
×
573
      ADD_TOPIC_TO_ARRAY(tmp, rebNewTopics);
×
574
      j++;
×
575
      continue;
×
576
    } else if (j >= newTopicNum) {
×
577
      void* tmp = taosArrayGetP(pExistedConsumer->currentTopics, i);
×
578
      MND_TMQ_NULL_CHECK(tmp);
×
579
      ADD_TOPIC_TO_ARRAY(tmp, rebRemovedTopics);
×
580
      i++;
×
581
      continue;
×
582
    } else {
583
      char *oldTopic = taosArrayGetP(pExistedConsumer->currentTopics, i);
×
584
      MND_TMQ_NULL_CHECK(oldTopic);
×
585
      char *newTopic = taosArrayGetP(pConsumerNew->assignedTopics, j);
×
586
      MND_TMQ_NULL_CHECK(newTopic);
×
587
      int   comp = strcmp(oldTopic, newTopic);
×
588
      if (comp == 0) {
×
589
        i++;
×
590
        j++;
×
591
        continue;
×
592
      } else if (comp < 0) {
×
593
        ADD_TOPIC_TO_ARRAY(oldTopic, rebRemovedTopics);
×
594
        i++;
×
595
        continue;
×
596
      } else {
×
597
        ADD_TOPIC_TO_ARRAY(newTopic, rebNewTopics);
×
598
        j++;
×
599
        continue;
×
600
      }
601
    }
602
  }
603
  // no topics need to be rebalanced
604
  if (taosArrayGetSize(pConsumerNew->rebNewTopics) == 0 && taosArrayGetSize(pConsumerNew->rebRemovedTopics) == 0) {
×
605
    code = TSDB_CODE_TMQ_NO_NEED_REBALANCE;
×
606
  }
607

608
END:
×
609
  taosRUnLockLatch(&pExistedConsumer->lock);
×
610
  PRINT_LOG_END
×
611
  return code;
×
612
}
613

614
static int32_t checkAndSortTopic(SMnode *pMnode, SArray *pTopicList){
×
615
  if (pTopicList == NULL || pMnode == NULL) {
×
616
    return TSDB_CODE_INVALID_PARA;
×
617
  }
618
  taosArraySort(pTopicList, taosArrayCompareString);
×
619
  taosArrayRemoveDuplicate(pTopicList, taosArrayCompareString, freeItem);
×
620

621
  int32_t newTopicNum = taosArrayGetSize(pTopicList);
×
622
  for (int i = 0; i < newTopicNum; i++) {
×
623
    int32_t gNum = mndGetGroupNumByTopic(pMnode, (const char *)taosArrayGetP(pTopicList, i));
×
624
    if (gNum >= MND_MAX_GROUP_PER_TOPIC) {
×
625
      return TSDB_CODE_TMQ_GROUP_OUT_OF_RANGE;
×
626
    }
627
  }
628
  return 0;
×
629
}
630

631
static int32_t buildSubConsumer(SMnode *pMnode, SCMSubscribeReq *subscribe, SMqConsumerObj** ppConsumer){
×
632
  if (pMnode == NULL || subscribe == NULL) {
×
633
    return TSDB_CODE_INVALID_PARA;
×
634
  }
635
  int64_t         consumerId = subscribe->consumerId;
×
636
  char           *cgroup     = subscribe->cgroup;
×
637
  SMqConsumerObj *pConsumerNew     = NULL;
×
638
  SMqConsumerObj *pExistedConsumer = NULL;
×
639
  int32_t lino = 0;
×
640
  PRINT_LOG_START
×
641
  int32_t code = mndAcquireConsumer(pMnode, consumerId, &pExistedConsumer);
×
642
  if (code != 0) {
×
643
    mInfo("receive tmq subscribe request from new consumer:0x%" PRIx64
×
644
              ",cgroup:%s, numOfTopics:%d", consumerId,
645
          subscribe->cgroup, (int32_t)taosArrayGetSize(subscribe->topicNames));
646

647
    MND_TMQ_RETURN_CHECK(tNewSMqConsumerObj(consumerId, cgroup, CONSUMER_INSERT_SUB, NULL, subscribe, &pConsumerNew));
×
648
  } else {
649
    int32_t status = atomic_load_32(&pExistedConsumer->status);
×
650

651
    mInfo("receive tmq subscribe request from existed consumer:0x%" PRIx64
×
652
              ",cgroup:%s, current status:%d(%s), subscribe topic num: %d",
653
          consumerId, subscribe->cgroup, status, mndConsumerStatusName(status),
654
          (int32_t)taosArrayGetSize(subscribe->topicNames));
655

656
    if (status != MQ_CONSUMER_STATUS_READY) {
×
657
      code = TSDB_CODE_MND_CONSUMER_NOT_READY;
×
658
      goto END;
×
659
    }
660
    MND_TMQ_RETURN_CHECK(tNewSMqConsumerObj(consumerId, cgroup, CONSUMER_UPDATE_SUB, NULL, subscribe, &pConsumerNew));
×
661
    MND_TMQ_RETURN_CHECK(getTopicAddDelete(pExistedConsumer, pConsumerNew));
×
662
  }
663
  if (ppConsumer){
×
664
    *ppConsumer = pConsumerNew;
×
665
    pConsumerNew = NULL;
×
666
  }
667

668
END:
×
669
  PRINT_LOG_END
×
670
  mndReleaseConsumer(pMnode, pExistedConsumer);
×
671
  tDeleteSMqConsumerObj(pConsumerNew);
×
672
  return code;
×
673
}
674

675
int32_t mndProcessSubscribeReq(SRpcMsg *pMsg) {
×
676
  if (pMsg == NULL) {
×
677
    return TSDB_CODE_INVALID_PARA;
×
678
  }
679
  SMnode         *pMnode = pMsg->info.node;
×
680
  char           *msgStr = pMsg->pCont;
×
681
  int32_t         code = 0;
×
682
  int32_t         lino = 0;
×
683
  SMqConsumerObj *pConsumerNew = NULL;
×
684
  STrans         *pTrans = NULL;
×
685

686
  PRINT_LOG_START
×
687
  SCMSubscribeReq subscribe = {0};
×
688
  MND_TMQ_RETURN_CHECK(tDeserializeSCMSubscribeReq(msgStr, &subscribe, pMsg->contLen));
×
689
  bool unSubscribe = (taosArrayGetSize(subscribe.topicNames) == 0);
×
690
  if(unSubscribe){
×
691
    SMqConsumerObj *pConsumerTmp = NULL;
×
692
    MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, subscribe.consumerId, &pConsumerTmp));
×
693
    taosRLockLatch(&pConsumerTmp->lock);
×
694
    size_t topicNum = taosArrayGetSize(pConsumerTmp->assignedTopics);
×
695
    taosRUnLockLatch(&pConsumerTmp->lock);
×
696
    mndReleaseConsumer(pMnode, pConsumerTmp);
×
697
    if (topicNum == 0){
×
698
      goto END;
×
699
    }
700
  }
701
  MND_TMQ_RETURN_CHECK(checkAndSortTopic(pMnode, subscribe.topicNames));
×
702
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY,
×
703
                          (unSubscribe ? TRN_CONFLICT_NOTHING :TRN_CONFLICT_DB_INSIDE),
704
                          pMsg, "subscribe");
705
  MND_TMQ_NULL_CHECK(pTrans);
×
706

707
  MND_TMQ_RETURN_CHECK(validateTopics(pTrans, &subscribe, pMnode, RPC_MSG_USER(pMsg), RPC_MSG_TOKEN(pMsg)));
×
708
  MND_TMQ_RETURN_CHECK(buildSubConsumer(pMnode, &subscribe, &pConsumerNew));
×
709
  MND_TMQ_RETURN_CHECK(mndSetConsumerCommitLogs(pTrans, pConsumerNew));
×
710
  MND_TMQ_RETURN_CHECK(mndTransPrepare(pMnode, pTrans));
×
711
  code = TSDB_CODE_ACTION_IN_PROGRESS;
×
712

713
END:
×
714
  mndTransDrop(pTrans);
×
715
  tDeleteSMqConsumerObj(pConsumerNew);
×
716
  taosArrayDestroyP(subscribe.topicNames, NULL);
×
717
  code = (code == TSDB_CODE_TMQ_NO_NEED_REBALANCE || code == TSDB_CODE_MND_CONSUMER_NOT_EXIST) ? 0 : code;
×
718
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS){
×
719
    mError("tmq subscribe request from consumer:0x%" PRIx64 " failed, code:%d", subscribe.consumerId, code);
×
720
  } else {
721
    mInfo("tmq subscribe request from consumer:0x%" PRIx64 " processed, code:%d", subscribe.consumerId, code);
×
722
  }
723
  return code;
×
724
}
725

726
SSdbRaw *mndConsumerActionEncode(SMqConsumerObj *pConsumer) {
×
727
  if (pConsumer == NULL) {
×
728
    return NULL;
×
729
  }
730
  int32_t code = 0;
×
731
  int32_t lino = 0;
×
732
  terrno = TSDB_CODE_OUT_OF_MEMORY;
×
733

734
  void   *buf = NULL;
×
735
  int32_t tlen = tEncodeSMqConsumerObj(NULL, pConsumer);
×
736
  int32_t size = sizeof(int32_t) + tlen + MND_CONSUMER_RESERVE_SIZE;
×
737

738
  SSdbRaw *pRaw = sdbAllocRaw(SDB_CONSUMER, MND_CONSUMER_VER_NUMBER, size);
×
739
  if (pRaw == NULL) goto CM_ENCODE_OVER;
×
740

741
  buf = taosMemoryMalloc(tlen);
×
742
  if (buf == NULL) goto CM_ENCODE_OVER;
×
743

744
  void *abuf = buf;
×
745
  if(tEncodeSMqConsumerObj(&abuf, pConsumer) < 0){
×
746
    goto CM_ENCODE_OVER;
×
747
  }
748

749
  int32_t dataPos = 0;
×
750
  SDB_SET_INT32(pRaw, dataPos, tlen, CM_ENCODE_OVER);
×
751
  SDB_SET_BINARY(pRaw, dataPos, buf, tlen, CM_ENCODE_OVER);
×
752
  SDB_SET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_ENCODE_OVER);
×
753
  SDB_SET_DATALEN(pRaw, dataPos, CM_ENCODE_OVER);
×
754

755
  terrno = TSDB_CODE_SUCCESS;
×
756

757
CM_ENCODE_OVER:
×
758
  taosMemoryFreeClear(buf);
×
759
  if (terrno != 0) {
×
760
    mError("consumer:0x%" PRIx64 " failed to encode to raw:%p since %s", pConsumer->consumerId, pRaw, terrstr());
×
761
    sdbFreeRaw(pRaw);
×
762
    return NULL;
×
763
  }
764

765
  mTrace("consumer:0x%" PRIx64 ", encode to raw:%p, row:%p", pConsumer->consumerId, pRaw, pConsumer);
×
766
  return pRaw;
×
767
}
768

769
SSdbRow *mndConsumerActionDecode(SSdbRaw *pRaw) {
×
770
  if (pRaw == NULL) {
×
771
    return NULL;
×
772
  }
773
  int32_t         code = 0;
×
774
  int32_t         lino = 0;
×
775
  SSdbRow        *pRow = NULL;
×
776
  SMqConsumerObj *pConsumer = NULL;
×
777
  void           *buf = NULL;
×
778

779
  terrno = 0;
×
780
  int8_t sver = 0;
×
781
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
×
782
    goto CM_DECODE_OVER;
×
783
  }
784

785
  if (sver < 1 || sver > MND_CONSUMER_VER_NUMBER) {
×
786
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
787
    goto CM_DECODE_OVER;
×
788
  }
789

790
  pRow = sdbAllocRow(sizeof(SMqConsumerObj));
×
791
  if (pRow == NULL) {
×
792
    goto CM_DECODE_OVER;
×
793
  }
794

795
  pConsumer = sdbGetRowObj(pRow);
×
796
  if (pConsumer == NULL) {
×
797
    goto CM_DECODE_OVER;
×
798
  }
799

800
  int32_t dataPos = 0;
×
801
  int32_t len;
802
  SDB_GET_INT32(pRaw, dataPos, &len, CM_DECODE_OVER);
×
803
  buf = taosMemoryMalloc(len);
×
804
  if (buf == NULL) {
×
805
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
806
    goto CM_DECODE_OVER;
×
807
  }
808

809
  SDB_GET_BINARY(pRaw, dataPos, buf, len, CM_DECODE_OVER);
×
810
  SDB_GET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_DECODE_OVER);
×
811

812
  if (tDecodeSMqConsumerObj(buf, pConsumer, sver) == NULL) {
×
813
    terrno = TSDB_CODE_OUT_OF_MEMORY;  // TODO set correct error code
×
814
    goto CM_DECODE_OVER;
×
815
  }
816

817
  tmsgUpdateDnodeEpSet(&pConsumer->ep);
×
818

819
CM_DECODE_OVER:
×
820
  taosMemoryFreeClear(buf);
×
821
  if (terrno != TSDB_CODE_SUCCESS) {
×
822
    mError("consumer:0x%" PRIx64 " failed to decode from raw:%p since %s",
×
823
           pConsumer == NULL ? 0 : pConsumer->consumerId, pRaw, terrstr());
824
    taosMemoryFreeClear(pRow);
×
825
  }
826

827
  return pRow;
×
828
}
829

830
static int32_t mndConsumerActionInsert(SSdb *pSdb, SMqConsumerObj *pConsumer) {
×
831
  if (pConsumer == NULL) {
×
832
    return TSDB_CODE_INVALID_PARA;
×
833
  }
834
  mInfo("consumer:0x%" PRIx64 " sub insert, cgroup:%s status:%d(%s) epoch:%d", pConsumer->consumerId, pConsumer->cgroup,
×
835
        pConsumer->status, mndConsumerStatusName(pConsumer->status), pConsumer->epoch);
836
  pConsumer->subscribeTime = pConsumer->createTime;
×
837
  return 0;
×
838
}
839

840
static int32_t mndConsumerActionDelete(SSdb *pSdb, SMqConsumerObj *pConsumer) {
×
841
  if (pConsumer == NULL) {
×
842
    return TSDB_CODE_INVALID_PARA;
×
843
  }
844
  mInfo("consumer:0x%" PRIx64 " perform delete action, status:(%d)%s", pConsumer->consumerId, pConsumer->status,
×
845
        mndConsumerStatusName(pConsumer->status));
846
  tClearSMqConsumerObj(pConsumer);
×
847
  return 0;
×
848
}
849

850
// remove from topic list
851
static void removeFromTopicList(SArray *topicList, const char *pTopic, int64_t consumerId, char *type) {
×
852
  if (topicList == NULL || pTopic == NULL) {
×
853
    return;
×
854
  }
855
  int32_t size = taosArrayGetSize(topicList);
×
856
  for (int32_t i = 0; i < size; i++) {
×
857
    char *p = taosArrayGetP(topicList, i);
×
858
    if (strcmp(pTopic, p) == 0) {
×
859
      taosArrayRemove(topicList, i);
×
860
      taosMemoryFree(p);
×
861

862
      mInfo("tmq rebalance consumer:0x%" PRIx64 " remove topic:%s in the %s topic list, remain newTopics:%d",
×
863
            consumerId, pTopic, type, (int)taosArrayGetSize(topicList));
864
      break;
×
865
    }
866
  }
867
}
868

869
static bool existInCurrentTopicList(const SMqConsumerObj *pConsumer, const char *pTopic) {
×
870
  if (pConsumer == NULL || pTopic == NULL) {
×
871
    return false;
×
872
  }
873
  bool    existing = false;
×
874
  int32_t size = taosArrayGetSize(pConsumer->currentTopics);
×
875
  for (int32_t i = 0; i < size; i++) {
×
876
    char *topic = taosArrayGetP(pConsumer->currentTopics, i);
×
877
    if (topic && strcmp(topic, pTopic) == 0) {
×
878
      existing = true;
×
879
      break;
×
880
    }
881
  }
882

883
  return existing;
×
884
}
885

886
static int32_t mndConsumerActionUpdate(SSdb *pSdb, SMqConsumerObj *pOldConsumer, SMqConsumerObj *pNewConsumer) {
×
887
  if (pOldConsumer == NULL || pNewConsumer == NULL) {
×
888
    return TSDB_CODE_INVALID_PARA;
×
889
  }
890
  int32_t lino = 0;
×
891
  int32_t code = 0;
×
892
  char *pNewTopic = NULL;
×
893
  PRINT_LOG_START
×
894
  taosWLockLatch(&pOldConsumer->lock);
×
895
  mInfo("consumer:0x%" PRIx64 " perform update action, update type:%d, subscribe-time:%" PRId64 ", createTime:%" PRId64,
×
896
        pOldConsumer->consumerId, pNewConsumer->updateType, pOldConsumer->subscribeTime, pOldConsumer->createTime);
897

898
  if (pNewConsumer->updateType == CONSUMER_UPDATE_SUB) {
×
899
    TSWAP(pOldConsumer->rebNewTopics, pNewConsumer->rebNewTopics);
×
900
    TSWAP(pOldConsumer->rebRemovedTopics, pNewConsumer->rebRemovedTopics);
×
901
    TSWAP(pOldConsumer->assignedTopics, pNewConsumer->assignedTopics);
×
902

903
    pOldConsumer->subscribeTime = taosGetTimestampMs();
×
904
    pOldConsumer->status = MQ_CONSUMER_STATUS_REBALANCE;
×
905
    mInfo("consumer:0x%" PRIx64 " subscribe update, modify existed consumer", pOldConsumer->consumerId);
×
906
  } else if (pNewConsumer->updateType == CONSUMER_UPDATE_REB) {
×
907
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
×
908
    pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
×
909
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
×
910
    mInfo("tmq rebalance consumer:0x%" PRIx64 " rebalance update, only rebalance time", pOldConsumer->consumerId);
×
911
  } else if (pNewConsumer->updateType == CONSUMER_ADD_REB) {
×
912
    void *tmp = taosArrayGetP(pNewConsumer->rebNewTopics, 0);
×
913
    MND_TMQ_NULL_CHECK(tmp);
×
914
    char *pNewTopic = taosStrdup(tmp);
×
915
    MND_TMQ_NULL_CHECK(pNewTopic);
×
916
    removeFromTopicList(pOldConsumer->rebNewTopics, pNewTopic, pOldConsumer->consumerId, "new");
×
917
    bool existing = existInCurrentTopicList(pOldConsumer, pNewTopic);
×
918
    if (existing) {
×
919
      mError("tmq rebalance consumer:0x%" PRIx64 " add new topic:%s should not in currentTopics", pOldConsumer->consumerId, pNewTopic);
×
920
    } else {
921
      MND_TMQ_NULL_CHECK(taosArrayPush(pOldConsumer->currentTopics, &pNewTopic));
×
922
      pNewTopic = NULL;
×
923
      taosArraySort(pOldConsumer->currentTopics, taosArrayCompareString);
×
924
    }
925

926
    int32_t status = pOldConsumer->status;
×
927
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
×
928
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
×
929
    }
930

931
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
×
932
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
×
933

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

941
  } else if (pNewConsumer->updateType == CONSUMER_REMOVE_REB) {
×
942
    char *topic = taosArrayGetP(pNewConsumer->rebRemovedTopics, 0);
×
943
    MND_TMQ_NULL_CHECK(topic);
×
944
    removeFromTopicList(pOldConsumer->rebRemovedTopics, topic, pOldConsumer->consumerId, "remove");
×
945
    removeFromTopicList(pOldConsumer->currentTopics, topic, pOldConsumer->consumerId, "current");
×
946

947
    int32_t status = pOldConsumer->status;
×
948
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
×
949
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
×
950
    }
951
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
×
952
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
×
953

954
    mInfo("tmq rebalanceconsumer:0x%" PRIx64 " rebalance update remove, state (%d)%s -> (%d)%s, new epoch:%d, reb-time:%" PRId64
×
955
          ", current topics:%d, newTopics:%d, removeTopics:%d",
956
          pOldConsumer->consumerId, status, mndConsumerStatusName(status), pOldConsumer->status,
957
          mndConsumerStatusName(pOldConsumer->status), pOldConsumer->epoch, pOldConsumer->rebalanceTime,
958
          (int)taosArrayGetSize(pOldConsumer->currentTopics), (int)taosArrayGetSize(pOldConsumer->rebNewTopics),
959
          (int)taosArrayGetSize(pOldConsumer->rebRemovedTopics));
960
  }
961

962
END:
×
963
  taosMemoryFree(pNewTopic);
×
964
  taosWUnLockLatch(&pOldConsumer->lock);
×
965

966
  PRINT_LOG_END
×
967

968
  return code;
×
969
}
970

971
int32_t mndAcquireConsumer(SMnode *pMnode, int64_t consumerId, SMqConsumerObj** pConsumer) {
×
972
  if (pMnode == NULL || pConsumer == NULL) {
×
973
    return TSDB_CODE_INVALID_PARA;
×
974
  }
975
  SSdb           *pSdb = pMnode->pSdb;
×
976
  *pConsumer = sdbAcquire(pSdb, SDB_CONSUMER, &consumerId);
×
977
  if (*pConsumer == NULL) {
×
978
    return TSDB_CODE_MND_CONSUMER_NOT_EXIST;
×
979
  }
980
  return 0;
×
981
}
982

983
void mndReleaseConsumer(SMnode *pMnode, SMqConsumerObj *pConsumer) {
37✔
984
  if (pMnode == NULL || pConsumer == NULL) {
37✔
985
    return;
37✔
986
  }
987
  SSdb *pSdb = pMnode->pSdb;
×
988
  sdbRelease(pSdb, pConsumer);
×
989
}
990

991
static int32_t buildResult(SMqConsumerObj *pConsumer, SShowObj *pShow, SSDataBlock *pBlock, int32_t numOfRows, const char* showTopic, bool hasTopic) {
×
992
  int32_t         code = 0;
×
993
  int32_t         lino = 0;
×
994
  SColumnInfoData *pColInfo = NULL;
×
995
  int32_t          cols = 0;
×
996
  char * parasStr = NULL;
×
997
  char           *status = NULL;
×
998

999
  PRINT_LOG_START
×
1000
  // consumer id
1001
  char consumerIdHex[TSDB_CONSUMER_ID_LEN + VARSTR_HEADER_SIZE] = {0};
×
1002
  (void)snprintf(varDataVal(consumerIdHex), TSDB_CONSUMER_ID_LEN, "0x%" PRIx64, pConsumer->consumerId);
×
1003
  varDataSetLen(consumerIdHex, strlen(varDataVal(consumerIdHex)));
×
1004

1005
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1006
  MND_TMQ_NULL_CHECK(pColInfo);
×
1007
  MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)consumerIdHex, false));
×
1008

1009
  // consumer group
1010
  char cgroup[TSDB_CGROUP_LEN + VARSTR_HEADER_SIZE] = {0};
×
1011
  STR_TO_VARSTR(cgroup, pConsumer->cgroup);
×
1012

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

1017
  // client id
1018
  char clientId[TSDB_CLIENT_ID_LEN + VARSTR_HEADER_SIZE] = {0};
×
1019
  STR_TO_VARSTR(clientId, pConsumer->clientId);
×
1020

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

1025
  // user
1026
  char user[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
×
1027
  STR_TO_VARSTR(user, pConsumer->user);
×
1028

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

1033
  // fqdn
1034
  char fqdn[TSDB_FQDN_LEN + VARSTR_HEADER_SIZE] = {0};
×
1035
  STR_TO_VARSTR(fqdn, pConsumer->fqdn);
×
1036

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

1041
  // status
1042
  const char *pStatusName = mndConsumerStatusName(pConsumer->status);
×
1043
  status = taosMemoryCalloc(1, pShow->pMeta->pSchemas[cols].bytes);
×
1044
  MND_TMQ_NULL_CHECK(status);
×
1045
  STR_TO_VARSTR(status, pStatusName);
×
1046

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

1051
  // one subscribed topic
1052
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1053
  MND_TMQ_NULL_CHECK(pColInfo);
×
1054
  if (hasTopic) {
×
1055
    char        topic[TSDB_TOPIC_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
1056
    mndTopicGetShowName(showTopic, topic + VARSTR_HEADER_SIZE);
×
1057
    *(VarDataLenT *)(topic) = strlen(topic + VARSTR_HEADER_SIZE);
×
1058
    MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)topic, false));
×
1059
  } else {
1060
    MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, NULL, true));
×
1061
  }
1062

1063
  // up time
1064
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1065
  MND_TMQ_NULL_CHECK(pColInfo);
×
1066
  MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->createTime, false));
×
1067

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

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

1078
  char         buf[TSDB_OFFSET_LEN] = {0};
×
1079
  STqOffsetVal pVal = {.type = pConsumer->resetOffsetCfg};
×
1080
  tFormatOffset(buf, TSDB_OFFSET_LEN, &pVal);
×
1081

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

1088
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1089
  MND_TMQ_NULL_CHECK(pColInfo);
×
1090
  MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)parasStr, false));
×
1091

1092
  // rebalance time
1093
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1094
  MND_TMQ_NULL_CHECK(pColInfo);
×
1095
  MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->pollTime, pConsumer->pollTime == 0));
×
1096

1097
END:
×
1098
  PRINT_LOG_END
×
1099
  taosMemoryFreeClear(status);
×
1100
  taosMemoryFreeClear(parasStr);
×
1101
  return code;
×
1102
}
1103

1104
static int32_t retrieveOneConsumer(SRpcMsg *pReq, SMqConsumerObj *pConsumer, SUserObj *pOperUser, bool showAll,
×
1105
                                   int32_t *numOfRows, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
1106
  int32_t code = 0;
×
1107
  int32_t lino = 0;
×
1108
  SMnode *pMnode = pReq->info.node;
×
1109
  PRINT_LOG_START
×
1110
  taosRLockLatch(&pConsumer->lock);
×
1111
  mDebug("showing consumer:0x%" PRIx64, pConsumer->consumerId);
×
1112
  if (taosArrayGetSize(pConsumer->assignedTopics) == 0) {
×
1113
    mInfo("showing consumer:0x%" PRIx64 " no assigned topic, skip", pConsumer->consumerId);
×
1114
    goto END;
×
1115
  }
1116

1117
  int32_t topicSz = taosArrayGetSize(pConsumer->assignedTopics);
×
1118
  bool    hasTopic = true;
×
1119
  if (topicSz == 0) {
×
1120
    hasTopic = false;
×
1121
    topicSz = 1;
×
1122
  }
1123

1124
  if (*numOfRows + topicSz > rowsCapacity) {
×
1125
    MND_TMQ_RETURN_CHECK(blockDataEnsureCapacity(pBlock, *numOfRows + topicSz));
×
1126
  }
1127

1128
  for (int32_t i = 0; i < topicSz; i++) {
×
1129
    char *pTopicFName = taosArrayGetP(pConsumer->assignedTopics, i);
×
1130
    if (!showAll && (strncmp(pOperUser->name, pConsumer->user, TSDB_USER_LEN) != 0)) {
×
1131
      bool         showConsumer = false;
×
1132
      SMqTopicObj *pTopic = NULL;
×
1133
      (void)mndAcquireTopic(pMnode, pTopicFName, &pTopic);
×
1134
      if (pTopic) {
×
1135
        SName name = {0};  // 1.topic1
×
1136
        if (0 == tNameFromString(&name, pTopic->name, T_NAME_ACCT | T_NAME_DB)) {
×
1137
          if (0 == mndCheckObjPrivilegeRecF(pMnode, pOperUser, PRIV_CONSUMER_SHOW, PRIV_OBJ_TOPIC, pTopic->ownerId,
×
1138
                                            pTopic->db, name.dbname)) {
×
1139
            showConsumer = true;
×
1140
          }
1141
        }
1142
        mndReleaseTopic(pMnode, pTopic);
×
1143
      }
1144
      if (!showConsumer) {
×
1145
        continue;
×
1146
      }
1147
    }
1148
    // char  topic[TSDB_TOPIC_FNAME_LEN] = {0};
1149
    // mndTopicGetShowName(showTopic, topic);
1150
    MND_TMQ_RETURN_CHECK(buildResult(pConsumer, pShow, pBlock, *numOfRows, pTopicFName, hasTopic));
×
1151
    (*numOfRows)++;
×
1152
  }
1153

1154
END:
×
1155
  taosRUnLockLatch(&pConsumer->lock);
×
1156
  PRINT_LOG_END
×
1157
  return code;
×
1158
}
1159

1160
static int32_t mndRetrieveConsumer(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
×
1161
  if (pReq == NULL || pShow == NULL || pBlock == NULL) {
×
1162
    return TSDB_CODE_INVALID_PARA;
×
1163
  }
1164
  SMnode         *pMnode = pReq->info.node;
×
1165
  SSdb           *pSdb = pMnode->pSdb;
×
1166
  int32_t         numOfRows = 0;
×
1167
  SMqConsumerObj *pConsumer = NULL;
×
1168
  SUserObj       *pOperUser = NULL;
×
1169
  int32_t         code = 0;
×
1170
  int32_t         lino = 0;
×
1171
  bool            showAll = false;
×
1172
  char            objFName[TSDB_OBJ_FNAME_LEN + 1] = {0};
×
1173
  PRINT_LOG_START
×
1174

1175
  MND_TMQ_RETURN_CHECK(mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser));
×
1176
  (void)snprintf(objFName, sizeof(objFName), "%d.*", pOperUser->acctId);
×
1177
  showAll = (0 == mndCheckSysObjPrivilege(pMnode, pOperUser, RPC_MSG_TOKEN(pReq), PRIV_CONSUMER_SHOW,
×
1178
                                          PRIV_OBJ_TOPIC, 0, objFName, "*"));
1179
  while (numOfRows < rowsCapacity) {
×
1180
    pShow->pIter = sdbFetch(pSdb, SDB_CONSUMER, pShow->pIter, (void **)&pConsumer);
×
1181
    if (pShow->pIter == NULL) {
×
1182
      break;
×
1183
    }
1184
    MND_TMQ_RETURN_CHECK(retrieveOneConsumer(pReq, pConsumer, pOperUser, showAll,  &numOfRows, pShow, pBlock, rowsCapacity));
×
1185
    
1186
    pBlock->info.rows = numOfRows;
×
1187
    sdbRelease(pSdb, pConsumer);
×
1188
    pConsumer = NULL;
×
1189
  }
1190

1191
  pShow->numOfRows += numOfRows;
×
1192

1193
END:
×
1194
  sdbRelease(pSdb, pConsumer);
×
1195
  sdbCancelFetch(pSdb, pShow->pIter);
×
1196
  mndReleaseUser(pMnode, pOperUser);
×
1197
  if (code != 0) {
×
1198
    mError("show consumer failed, code:%d", code);
×
1199
    return code;
×
1200
  } else {
1201
    mDebug("show consumer processed, numOfRows:%d", numOfRows);
×
1202
    return numOfRows;
×
1203
  }
1204
}
1205

1206
static void mndCancelGetNextConsumer(SMnode *pMnode, void *pIter) {
×
1207
  if (pMnode == NULL || pIter == NULL) return;
×
1208
  SSdb *pSdb = pMnode->pSdb;
×
1209
  sdbCancelFetchByType(pSdb, pIter, SDB_CONSUMER);
×
1210
}
1211

1212
const char *mndConsumerStatusName(int status) {
×
1213
  switch (status) {
×
1214
    case MQ_CONSUMER_STATUS_READY:
×
1215
      return "ready";
×
1216
    case MQ_CONSUMER_STATUS_REBALANCE:
×
1217
      return "rebalancing";
×
1218
    default:
×
1219
      return "unknown";
×
1220
  }
1221
}
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