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

taosdata / TDengine / #3770

28 Mar 2025 10:15AM UTC coverage: 33.994% (-0.01%) from 34.005%
#3770

push

travis-ci

happyguoxy
test:alter lcov result

147922 of 598399 branches covered (24.72%)

Branch coverage included in aggregate %.

221525 of 488415 relevant lines covered (45.36%)

764609.06 hits per line

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

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

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

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

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

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

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

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

110
    if (subscribe->enableReplay) {
40!
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};
40✔
129
    (void)snprintf(key, TSDB_CONSUMER_ID_LEN, "%"PRIx64, subscribe->consumerId);
40✔
130
    mndTransSetDbName(pTrans, pTopic->db, key);
40✔
131
    MND_TMQ_RETURN_CHECK(mndTransCheckConflict(pMnode, pTrans));
40!
132
    mndReleaseTopic(pMnode, pTopic);
40✔
133
  }
134
  return 0;
72✔
135

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

288
  int32_t numOfTopics = taosArrayGetSize(pConsumer->currentTopics);
72✔
289

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

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

308
    SMqSubTopicEp topicEp = {0};
40✔
309
    tstrncpy(topicEp.topic, topic, TSDB_TOPIC_FNAME_LEN);
40✔
310

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

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

353
    for (int32_t j = 0; j < vgNum; j++) {
240✔
354
      SMqVgEp *pVgEp = taosArrayGetP(pConsumerEp->vgs, j);
200✔
355
      if (pVgEp == NULL) {
200!
356
        continue;
×
357
      }
358
      if (epoch == -1) {
200!
359
        SVgObj *pVgroup = mndAcquireVgroup(pMnode, pVgEp->vgId);
×
360
        if (pVgroup) {
×
361
          pVgEp->epSet = mndGetVgroupEpset(pMnode, pVgroup);
×
362
          mndReleaseVgroup(pMnode, pVgroup);
×
363
        }
364
      }
365
      SMqSubVgEp vgEp = {.epSet = pVgEp->epSet, .vgId = pVgEp->vgId, .offset = -1};
200✔
366
      if (taosArrayPush(topicEp.vgs, &vgEp) == NULL) {
400!
367
        taosArrayDestroy(topicEp.vgs);
×
368
        taosRUnLockLatch(&pConsumer->lock);
×
369
        taosRUnLockLatch(&pSub->lock);
×
370
        mndReleaseSubscribe(pMnode, pSub);
×
371
        return terrno;
×
372
      }
373
    }
374
    if (taosArrayPush(rsp->topics, &topicEp) == NULL) {
80!
375
      taosArrayDestroy(topicEp.vgs);
×
376
      taosRUnLockLatch(&pConsumer->lock);
×
377
      taosRUnLockLatch(&pSub->lock);
×
378
      mndReleaseSubscribe(pMnode, pSub);
×
379
      return terrno;
×
380
    }
381
    taosRUnLockLatch(&pSub->lock);
40✔
382
    mndReleaseSubscribe(pMnode, pSub);
40✔
383
  }
384
  taosRUnLockLatch(&pConsumer->lock);
72✔
385
  return 0;
72✔
386
}
387

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

400
  SMqRspHead *pHead = buf;
168✔
401

402
  pHead->mqMsgType = TMQ_MSG_TYPE__EP_RSP;
168✔
403
  pHead->epoch = serverEpoch;
168✔
404
  pHead->consumerId = consumerId;
168✔
405
  pHead->walsver = 0;
168✔
406
  pHead->walever = 0;
168✔
407

408
  void *abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
168✔
409
  if (tEncodeSMqAskEpRsp(&abuf, rsp) < 0) {
168!
410
    rpcFreeCont(buf);
×
411
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
412
  }
413

414
  // send rsp
415
  pMsg->info.rsp = buf;
168✔
416
  pMsg->info.rspLen = tlen;
168✔
417
  return code;
168✔
418
}
419

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

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

440
  // 1. check consumer status
441
  int32_t status = atomic_load_32(&pConsumer->status);
357✔
442
  if (status != MQ_CONSUMER_STATUS_READY) {
357✔
443
    mInfo("consumer:0x%" PRIx64 " not ready, status: %s", consumerId, mndConsumerStatusName(status));
189!
444
    code = TSDB_CODE_MND_CONSUMER_NOT_READY;
189✔
445
    goto END;
189✔
446
  }
447

448
  int32_t epoch = req.epoch;
168✔
449
  int32_t serverEpoch = atomic_load_32(&pConsumer->epoch);
168✔
450

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

458
  code = buildAskEpRsp(pMsg, &rsp, serverEpoch, consumerId);
168✔
459

460
END:
357✔
461
  tDeleteSMqAskEpRsp(&rsp);
462
  mndReleaseConsumer(pMnode, pConsumer);
357✔
463
  return code;
357✔
464
}
465

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

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

500
static void freeItem(void *param) {
×
501
  if (param == NULL) {
×
502
    return;
×
503
  }
504
  void *pItem = *(void **)param;
×
505
  if (pItem != NULL) {
×
506
    taosMemoryFree(pItem);
×
507
  }
508
}
509

510
#define ADD_TOPIC_TO_ARRAY(element, array) \
511
char *newTopicCopy = taosStrdup(element); \
512
MND_TMQ_NULL_CHECK(newTopicCopy);\
513
if (taosArrayPush(pConsumerNew->array, &newTopicCopy) == NULL){\
514
  taosMemoryFree(newTopicCopy);\
515
  code = terrno;\
516
  goto END;\
517
}
518

519
static int32_t getTopicAddDelete(SMqConsumerObj *pExistedConsumer, SMqConsumerObj *pConsumerNew){
34✔
520
  if (pExistedConsumer == NULL || pConsumerNew == NULL) {
34!
521
    return TSDB_CODE_INVALID_PARA;
×
522
  }
523
  int32_t code = 0;
34✔
524
  pConsumerNew->rebNewTopics = taosArrayInit(0, sizeof(void *));
34✔
525
  MND_TMQ_NULL_CHECK(pConsumerNew->rebNewTopics);
34!
526
  pConsumerNew->rebRemovedTopics = taosArrayInit(0, sizeof(void *));
34✔
527
  MND_TMQ_NULL_CHECK(pConsumerNew->rebRemovedTopics);
34!
528

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

571
END:
34✔
572
  return code;
34✔
573
}
574

575
static int32_t checkAndSortTopic(SMnode *pMnode, SArray *pTopicList){
72✔
576
  if (pTopicList == NULL || pMnode == NULL) {
72!
577
    return TSDB_CODE_INVALID_PARA;
×
578
  }
579
  taosArraySort(pTopicList, taosArrayCompareString);
72✔
580
  taosArrayRemoveDuplicate(pTopicList, taosArrayCompareString, freeItem);
72✔
581

582
  int32_t newTopicNum = taosArrayGetSize(pTopicList);
72✔
583
  for (int i = 0; i < newTopicNum; i++) {
112✔
584
    int32_t gNum = mndGetGroupNumByTopic(pMnode, (const char *)taosArrayGetP(pTopicList, i));
40✔
585
    if (gNum >= MND_MAX_GROUP_PER_TOPIC) {
40!
586
      return TSDB_CODE_TMQ_GROUP_OUT_OF_RANGE;
×
587
    }
588
  }
589
  return 0;
72✔
590
}
591

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

606
    MND_TMQ_RETURN_CHECK(tNewSMqConsumerObj(consumerId, cgroup, CONSUMER_INSERT_SUB, NULL, subscribe, &pConsumerNew));
38!
607
  } else {
608
    int32_t status = atomic_load_32(&pExistedConsumer->status);
34✔
609

610
    mInfo("receive subscribe request from existed consumer:0x%" PRIx64
34!
611
              ",cgroup:%s, current status:%d(%s), subscribe topic num: %d",
612
          consumerId, subscribe->cgroup, status, mndConsumerStatusName(status),
613
          (int32_t)taosArrayGetSize(subscribe->topicNames));
614

615
    if (status != MQ_CONSUMER_STATUS_READY) {
34!
616
      code = TSDB_CODE_MND_CONSUMER_NOT_READY;
×
617
      goto END;
×
618
    }
619
    MND_TMQ_RETURN_CHECK(tNewSMqConsumerObj(consumerId, cgroup, CONSUMER_UPDATE_SUB, NULL, subscribe, &pConsumerNew));
34!
620
    MND_TMQ_RETURN_CHECK(getTopicAddDelete(pExistedConsumer, pConsumerNew));
34!
621
  }
622
  mndReleaseConsumer(pMnode, pExistedConsumer);
72✔
623
  if (ppConsumer){
72!
624
    *ppConsumer = pConsumerNew;
72✔
625
  }
626
  return code;
72✔
627

628
END:
×
629
  mndReleaseConsumer(pMnode, pExistedConsumer);
×
630
  tDeleteSMqConsumerObj(pConsumerNew);
×
631
  return code;
×
632
}
633

634
int32_t mndProcessSubscribeReq(SRpcMsg *pMsg) {
104✔
635
  if (pMsg == NULL) {
104!
636
    return TSDB_CODE_INVALID_PARA;
×
637
  }
638
  SMnode *pMnode = pMsg->info.node;
104✔
639
  char   *msgStr = pMsg->pCont;
104✔
640
  int32_t code = 0;
104✔
641
  SMqConsumerObj *pConsumerNew = NULL;
104✔
642
  STrans         *pTrans = NULL;
104✔
643

644
  SCMSubscribeReq subscribe = {0};
104✔
645
  MND_TMQ_RETURN_CHECK(tDeserializeSCMSubscribeReq(msgStr, &subscribe, pMsg->contLen));
208!
646
  bool unSubscribe = (taosArrayGetSize(subscribe.topicNames) == 0);
104✔
647
  if(unSubscribe){
104✔
648
    SMqConsumerObj *pConsumerTmp = NULL;
66✔
649
    MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, subscribe.consumerId, &pConsumerTmp));
98!
650
    if (taosArrayGetSize(pConsumerTmp->assignedTopics) == 0){
66✔
651
      mndReleaseConsumer(pMnode, pConsumerTmp);
32✔
652
      goto END;
32✔
653
    }
654
    mndReleaseConsumer(pMnode, pConsumerTmp);
34✔
655
  }
656
  MND_TMQ_RETURN_CHECK(checkAndSortTopic(pMnode, subscribe.topicNames));
72!
657
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY,
72✔
658
                          (unSubscribe ? TRN_CONFLICT_NOTHING :TRN_CONFLICT_DB_INSIDE),
659
                          pMsg, "subscribe");
660
  MND_TMQ_NULL_CHECK(pTrans);
72!
661

662
  MND_TMQ_RETURN_CHECK(validateTopics(pTrans, &subscribe, pMnode, pMsg->info.conn.user));
72!
663
  MND_TMQ_RETURN_CHECK(buildSubConsumer(pMnode, &subscribe, &pConsumerNew));
72!
664
  MND_TMQ_RETURN_CHECK(mndSetConsumerCommitLogs(pTrans, pConsumerNew));
72!
665
  MND_TMQ_RETURN_CHECK(mndTransPrepare(pMnode, pTrans));
72!
666
  code = TSDB_CODE_ACTION_IN_PROGRESS;
72✔
667

668
END:
104✔
669
  mndTransDrop(pTrans);
104✔
670
  tDeleteSMqConsumerObj(pConsumerNew);
104✔
671
  taosArrayDestroyP(subscribe.topicNames, NULL);
104✔
672
  return (code == TSDB_CODE_TMQ_NO_NEED_REBALANCE || code == TSDB_CODE_MND_CONSUMER_NOT_EXIST) ? 0 : code;
104!
673
}
674

675
SSdbRaw *mndConsumerActionEncode(SMqConsumerObj *pConsumer) {
189✔
676
  if (pConsumer == NULL) {
189!
677
    return NULL;
×
678
  }
679
  int32_t code = 0;
189✔
680
  int32_t lino = 0;
189✔
681
  terrno = TSDB_CODE_OUT_OF_MEMORY;
189✔
682

683
  void   *buf = NULL;
189✔
684
  int32_t tlen = tEncodeSMqConsumerObj(NULL, pConsumer);
189✔
685
  int32_t size = sizeof(int32_t) + tlen + MND_CONSUMER_RESERVE_SIZE;
189✔
686

687
  SSdbRaw *pRaw = sdbAllocRaw(SDB_CONSUMER, MND_CONSUMER_VER_NUMBER, size);
189✔
688
  if (pRaw == NULL) goto CM_ENCODE_OVER;
189!
689

690
  buf = taosMemoryMalloc(tlen);
189!
691
  if (buf == NULL) goto CM_ENCODE_OVER;
189!
692

693
  void *abuf = buf;
189✔
694
  if(tEncodeSMqConsumerObj(&abuf, pConsumer) < 0){
189!
695
    goto CM_ENCODE_OVER;
×
696
  }
697

698
  int32_t dataPos = 0;
189✔
699
  SDB_SET_INT32(pRaw, dataPos, tlen, CM_ENCODE_OVER);
189!
700
  SDB_SET_BINARY(pRaw, dataPos, buf, tlen, CM_ENCODE_OVER);
189!
701
  SDB_SET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_ENCODE_OVER);
189!
702
  SDB_SET_DATALEN(pRaw, dataPos, CM_ENCODE_OVER);
189!
703

704
  terrno = TSDB_CODE_SUCCESS;
189✔
705

706
CM_ENCODE_OVER:
189✔
707
  taosMemoryFreeClear(buf);
189!
708
  if (terrno != 0) {
189!
709
    mError("consumer:0x%" PRIx64 " failed to encode to raw:%p since %s", pConsumer->consumerId, pRaw, terrstr());
×
710
    sdbFreeRaw(pRaw);
×
711
    return NULL;
×
712
  }
713

714
  mTrace("consumer:0x%" PRIx64 ", encode to raw:%p, row:%p", pConsumer->consumerId, pRaw, pConsumer);
189✔
715
  return pRaw;
189✔
716
}
717

718
SSdbRow *mndConsumerActionDecode(SSdbRaw *pRaw) {
193✔
719
  if (pRaw == NULL) {
193!
720
    return NULL;
×
721
  }
722
  int32_t         code = 0;
193✔
723
  int32_t         lino = 0;
193✔
724
  SSdbRow        *pRow = NULL;
193✔
725
  SMqConsumerObj *pConsumer = NULL;
193✔
726
  void           *buf = NULL;
193✔
727

728
  terrno = 0;
193✔
729
  int8_t sver = 0;
193✔
730
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
193!
731
    goto CM_DECODE_OVER;
×
732
  }
733

734
  if (sver < 1 || sver > MND_CONSUMER_VER_NUMBER) {
193!
735
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
736
    goto CM_DECODE_OVER;
×
737
  }
738

739
  pRow = sdbAllocRow(sizeof(SMqConsumerObj));
193✔
740
  if (pRow == NULL) {
193!
741
    goto CM_DECODE_OVER;
×
742
  }
743

744
  pConsumer = sdbGetRowObj(pRow);
193✔
745
  if (pConsumer == NULL) {
193!
746
    goto CM_DECODE_OVER;
×
747
  }
748

749
  int32_t dataPos = 0;
193✔
750
  int32_t len;
751
  SDB_GET_INT32(pRaw, dataPos, &len, CM_DECODE_OVER);
193!
752
  buf = taosMemoryMalloc(len);
193!
753
  if (buf == NULL) {
193!
754
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
755
    goto CM_DECODE_OVER;
×
756
  }
757

758
  SDB_GET_BINARY(pRaw, dataPos, buf, len, CM_DECODE_OVER);
193!
759
  SDB_GET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_DECODE_OVER);
193!
760

761
  if (tDecodeSMqConsumerObj(buf, pConsumer, sver) == NULL) {
193!
762
    terrno = TSDB_CODE_OUT_OF_MEMORY;  // TODO set correct error code
×
763
    goto CM_DECODE_OVER;
×
764
  }
765

766
  tmsgUpdateDnodeEpSet(&pConsumer->ep);
193✔
767

768
CM_DECODE_OVER:
193✔
769
  taosMemoryFreeClear(buf);
193!
770
  if (terrno != TSDB_CODE_SUCCESS) {
193!
771
    mError("consumer:0x%" PRIx64 " failed to decode from raw:%p since %s",
×
772
           pConsumer == NULL ? 0 : pConsumer->consumerId, pRaw, terrstr());
773
    taosMemoryFreeClear(pRow);
×
774
  }
775

776
  return pRow;
193✔
777
}
778

779
static int32_t mndConsumerActionInsert(SSdb *pSdb, SMqConsumerObj *pConsumer) {
39✔
780
  if (pConsumer == NULL) {
39!
781
    return TSDB_CODE_INVALID_PARA;
×
782
  }
783
  mInfo("consumer:0x%" PRIx64 " sub insert, cgroup:%s status:%d(%s) epoch:%d", pConsumer->consumerId, pConsumer->cgroup,
39!
784
        pConsumer->status, mndConsumerStatusName(pConsumer->status), pConsumer->epoch);
785
  pConsumer->subscribeTime = pConsumer->createTime;
39✔
786
  return 0;
39✔
787
}
788

789
static int32_t mndConsumerActionDelete(SSdb *pSdb, SMqConsumerObj *pConsumer) {
193✔
790
  if (pConsumer == NULL) {
193!
791
    return TSDB_CODE_INVALID_PARA;
×
792
  }
793
  mInfo("consumer:0x%" PRIx64 " perform delete action, status:(%d)%s", pConsumer->consumerId, pConsumer->status,
193!
794
        mndConsumerStatusName(pConsumer->status));
795
  tClearSMqConsumerObj(pConsumer);
193✔
796
  return 0;
193✔
797
}
798

799
//static void updateConsumerStatus(SMqConsumerObj *pConsumer) {
800
//  int32_t status = pConsumer->status;
801
//
802
//  if (taosArrayGetSize(pConsumer->rebNewTopics) == 0 && taosArrayGetSize(pConsumer->rebRemovedTopics) == 0) {
803
//    if (status == MQ_CONSUMER_STATUS_REBALANCE) {
804
//      pConsumer->status = MQ_CONSUMER_STATUS_READY;
805
//    } else if (status == MQ_CONSUMER_STATUS_READY && taosArrayGetSize(pConsumer->currentTopics) == 0) {
806
//      pConsumer->status = MQ_CONSUMER_STATUS_LOST;
807
//    }
808
//  }
809
//}
810

811
// remove from topic list
812
static void removeFromTopicList(SArray *topicList, const char *pTopic, int64_t consumerId, char *type) {
121✔
813
  if (topicList == NULL || pTopic == NULL) {
121!
814
    return;
×
815
  }
816
  int32_t size = taosArrayGetSize(topicList);
121✔
817
  for (int32_t i = 0; i < size; i++) {
123✔
818
    char *p = taosArrayGetP(topicList, i);
118✔
819
    if (strcmp(pTopic, p) == 0) {
118✔
820
      taosArrayRemove(topicList, i);
116✔
821
      taosMemoryFree(p);
116!
822

823
      mInfo("[rebalance] consumer:0x%" PRIx64 " remove topic:%s in the %s topic list, remain newTopics:%d",
116!
824
            consumerId, pTopic, type, (int)taosArrayGetSize(topicList));
825
      break;
116✔
826
    }
827
  }
828
}
829

830
static bool existInCurrentTopicList(const SMqConsumerObj *pConsumer, const char *pTopic) {
41✔
831
  if (pConsumer == NULL || pTopic == NULL) {
41!
832
    return false;
×
833
  }
834
  bool    existing = false;
41✔
835
  int32_t size = taosArrayGetSize(pConsumer->currentTopics);
41✔
836
  for (int32_t i = 0; i < size; i++) {
43✔
837
    char *topic = taosArrayGetP(pConsumer->currentTopics, i);
2✔
838
    if (topic && strcmp(topic, pTopic) == 0) {
2!
839
      existing = true;
×
840
      break;
×
841
    }
842
  }
843

844
  return existing;
41✔
845
}
846

847
static int32_t mndConsumerActionUpdate(SSdb *pSdb, SMqConsumerObj *pOldConsumer, SMqConsumerObj *pNewConsumer) {
116✔
848
  if (pOldConsumer == NULL || pNewConsumer == NULL) {
116!
849
    return TSDB_CODE_INVALID_PARA;
×
850
  }
851
  mInfo("consumer:0x%" PRIx64 " perform update action, update type:%d, subscribe-time:%" PRId64 ", createTime:%" PRId64,
116!
852
        pOldConsumer->consumerId, pNewConsumer->updateType, pOldConsumer->subscribeTime, pOldConsumer->createTime);
853

854
  taosWLockLatch(&pOldConsumer->lock);
116✔
855

856
  if (pNewConsumer->updateType == CONSUMER_UPDATE_SUB) {
116✔
857
    TSWAP(pOldConsumer->rebNewTopics, pNewConsumer->rebNewTopics);
35✔
858
    TSWAP(pOldConsumer->rebRemovedTopics, pNewConsumer->rebRemovedTopics);
35✔
859
    TSWAP(pOldConsumer->assignedTopics, pNewConsumer->assignedTopics);
35✔
860

861
    pOldConsumer->subscribeTime = taosGetTimestampMs();
35✔
862
    pOldConsumer->status = MQ_CONSUMER_STATUS_REBALANCE;
35✔
863
    mInfo("consumer:0x%" PRIx64 " subscribe update, modify existed consumer", pOldConsumer->consumerId);
35!
864
  } else if (pNewConsumer->updateType == CONSUMER_UPDATE_REB) {
81!
865
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
×
866
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
×
867
    mInfo("[rebalance] consumer:0x%" PRIx64 " rebalance update, only rebalance time", pOldConsumer->consumerId);
×
868
  } else if (pNewConsumer->updateType == CONSUMER_ADD_REB) {
81✔
869
    void *tmp = taosArrayGetP(pNewConsumer->rebNewTopics, 0);
41✔
870
    if (tmp == NULL){
41!
871
      return TSDB_CODE_TMQ_INVALID_MSG;
×
872
    }
873
    char *pNewTopic = taosStrdup(tmp);
41!
874
    if (pNewTopic == NULL) {
41!
875
      return terrno;
×
876
    }
877
    removeFromTopicList(pOldConsumer->rebNewTopics, pNewTopic, pOldConsumer->consumerId, "new");
41✔
878
    bool existing = existInCurrentTopicList(pOldConsumer, pNewTopic);
41✔
879
    if (existing) {
41!
880
      mError("[rebalance] consumer:0x%" PRIx64 " add new topic:%s should not in currentTopics", pOldConsumer->consumerId, pNewTopic);
×
881
      taosMemoryFree(pNewTopic);
×
882
    } else {
883
      if (taosArrayPush(pOldConsumer->currentTopics, &pNewTopic) == NULL) {
82!
884
        taosMemoryFree(pNewTopic);
×
885
        return TSDB_CODE_TMQ_INVALID_MSG;
×
886
      }
887
      taosArraySort(pOldConsumer->currentTopics, taosArrayCompareString);
41✔
888
    }
889

890
    int32_t status = pOldConsumer->status;
41✔
891
//    updateConsumerStatus(pOldConsumer);
892
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
41!
893
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
39✔
894
    }
895

896
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
41✔
897
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
41✔
898

899
    mInfo("[rebalance] consumer:0x%" PRIx64 " rebalance update add, state (%d)%s -> (%d)%s, new epoch:%d, reb-time:%" PRId64
41!
900
          ", current topics:%d, newTopics:%d, removeTopics:%d",
901
          pOldConsumer->consumerId, status, mndConsumerStatusName(status), pOldConsumer->status,
902
          mndConsumerStatusName(pOldConsumer->status), pOldConsumer->epoch, pOldConsumer->rebalanceTime,
903
          (int)taosArrayGetSize(pOldConsumer->currentTopics), (int)taosArrayGetSize(pOldConsumer->rebNewTopics),
904
          (int)taosArrayGetSize(pOldConsumer->rebRemovedTopics));
905

906
  } else if (pNewConsumer->updateType == CONSUMER_REMOVE_REB) {
40!
907
    char *topic = taosArrayGetP(pNewConsumer->rebRemovedTopics, 0);
40✔
908
    if (topic == NULL){
40!
909
      return TSDB_CODE_TMQ_INVALID_MSG;
×
910
    }
911
    removeFromTopicList(pOldConsumer->rebRemovedTopics, topic, pOldConsumer->consumerId, "remove");
40✔
912
    removeFromTopicList(pOldConsumer->currentTopics, topic, pOldConsumer->consumerId, "current");
40✔
913

914
    int32_t status = pOldConsumer->status;
40✔
915
//    updateConsumerStatus(pOldConsumer);
916
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
40!
917
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
40✔
918
    }
919
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
40✔
920
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
40✔
921

922
    mInfo("[rebalance]consumer:0x%" PRIx64 " rebalance update remove, state (%d)%s -> (%d)%s, new epoch:%d, reb-time:%" PRId64
40!
923
          ", current topics:%d, newTopics:%d, removeTopics:%d",
924
          pOldConsumer->consumerId, status, mndConsumerStatusName(status), pOldConsumer->status,
925
          mndConsumerStatusName(pOldConsumer->status), pOldConsumer->epoch, pOldConsumer->rebalanceTime,
926
          (int)taosArrayGetSize(pOldConsumer->currentTopics), (int)taosArrayGetSize(pOldConsumer->rebNewTopics),
927
          (int)taosArrayGetSize(pOldConsumer->rebRemovedTopics));
928
  }
929

930
  taosWUnLockLatch(&pOldConsumer->lock);
116✔
931
  return 0;
116✔
932
}
933

934
int32_t mndAcquireConsumer(SMnode *pMnode, int64_t consumerId, SMqConsumerObj** pConsumer) {
681✔
935
  if (pMnode == NULL || pConsumer == NULL) {
681!
936
    return TSDB_CODE_INVALID_PARA;
×
937
  }
938
  SSdb           *pSdb = pMnode->pSdb;
681✔
939
  *pConsumer = sdbAcquire(pSdb, SDB_CONSUMER, &consumerId);
681✔
940
  if (*pConsumer == NULL) {
681✔
941
    return TSDB_CODE_MND_CONSUMER_NOT_EXIST;
38✔
942
  }
943
  return 0;
643✔
944
}
945

946
void mndReleaseConsumer(SMnode *pMnode, SMqConsumerObj *pConsumer) {
836✔
947
  if (pMnode == NULL || pConsumer == NULL) {
836!
948
    return;
38✔
949
  }
950
  SSdb *pSdb = pMnode->pSdb;
798✔
951
  sdbRelease(pSdb, pConsumer);
798✔
952
}
953

954
static int32_t mndRetrieveConsumer(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
10✔
955
  if (pReq == NULL || pShow == NULL || pBlock == NULL) {
10!
956
    return TSDB_CODE_INVALID_PARA;
×
957
  }
958
  SMnode         *pMnode = pReq->info.node;
10✔
959
  SSdb           *pSdb = pMnode->pSdb;
10✔
960
  int32_t         numOfRows = 0;
10✔
961
  SMqConsumerObj *pConsumer = NULL;
10✔
962
  int32_t         code = 0;
10✔
963
  char           *parasStr = NULL;
10✔
964
  char           *status = NULL;
10✔
965

966
  while (numOfRows < rowsCapacity) {
24!
967
    pShow->pIter = sdbFetch(pSdb, SDB_CONSUMER, pShow->pIter, (void **)&pConsumer);
24✔
968
    if (pShow->pIter == NULL) {
24✔
969
      break;
10✔
970
    }
971

972
    if (taosArrayGetSize(pConsumer->assignedTopics) == 0) {
14!
973
      mInfo("showing consumer:0x%" PRIx64 " no assigned topic, skip", pConsumer->consumerId);
×
974
      sdbRelease(pSdb, pConsumer);
×
975
      continue;
×
976
    }
977

978
    taosRLockLatch(&pConsumer->lock);
14✔
979
    mInfo("showing consumer:0x%" PRIx64, pConsumer->consumerId);
14!
980

981
    int32_t topicSz = taosArrayGetSize(pConsumer->assignedTopics);
14✔
982
    bool    hasTopic = true;
14✔
983
    if (topicSz == 0) {
14!
984
      hasTopic = false;
×
985
      topicSz = 1;
×
986
    }
987

988
    if (numOfRows + topicSz > rowsCapacity) {
14!
989
      MND_TMQ_RETURN_CHECK(blockDataEnsureCapacity(pBlock, numOfRows + topicSz));
×
990
    }
991

992
    for (int32_t i = 0; i < topicSz; i++) {
42✔
993
      SColumnInfoData *pColInfo = NULL;
28✔
994
      int32_t          cols = 0;
28✔
995

996
      // consumer id
997
      char consumerIdHex[TSDB_CONSUMER_ID_LEN + VARSTR_HEADER_SIZE] = {0};
28✔
998
      (void)snprintf(varDataVal(consumerIdHex), TSDB_CONSUMER_ID_LEN, "0x%" PRIx64, pConsumer->consumerId);
28✔
999
      varDataSetLen(consumerIdHex, strlen(varDataVal(consumerIdHex)));
28✔
1000

1001
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
28✔
1002
      MND_TMQ_NULL_CHECK(pColInfo);
28!
1003
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)consumerIdHex, false));
28!
1004

1005
      // consumer group
1006
      char cgroup[TSDB_CGROUP_LEN + VARSTR_HEADER_SIZE] = {0};
28✔
1007
      STR_TO_VARSTR(cgroup, pConsumer->cgroup);
28✔
1008

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

1013
      // client id
1014
      char clientId[TSDB_CLIENT_ID_LEN + VARSTR_HEADER_SIZE] = {0};
28✔
1015
      STR_TO_VARSTR(clientId, pConsumer->clientId);
28✔
1016

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

1021
      // user
1022
      char user[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
28✔
1023
      STR_TO_VARSTR(user, pConsumer->user);
28✔
1024

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

1029
      // fqdn
1030
      char fqdn[TSDB_FQDN_LEN + VARSTR_HEADER_SIZE] = {0};
28✔
1031
      STR_TO_VARSTR(fqdn, pConsumer->fqdn);
28✔
1032

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

1037
      // status
1038
      const char *pStatusName = mndConsumerStatusName(pConsumer->status);
28✔
1039
      status = taosMemoryCalloc(1, pShow->pMeta->pSchemas[cols].bytes);
28!
1040
      MND_TMQ_NULL_CHECK(status);
28!
1041
      STR_TO_VARSTR(status, pStatusName);
28✔
1042

1043
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
28✔
1044
      MND_TMQ_NULL_CHECK(pColInfo);
28!
1045
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)status, false));
28!
1046
      taosMemoryFreeClear(status);
28!
1047

1048
      // one subscribed topic
1049
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
28✔
1050
      MND_TMQ_NULL_CHECK(pColInfo);
28!
1051
      if (hasTopic) {
28!
1052
        char        topic[TSDB_TOPIC_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
28✔
1053
        mndTopicGetShowName(taosArrayGetP(pConsumer->assignedTopics, i), topic + VARSTR_HEADER_SIZE);
28✔
1054
        *(VarDataLenT *)(topic) = strlen(topic + VARSTR_HEADER_SIZE);
28✔
1055
        MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)topic, false));
28!
1056
      } else {
1057
        MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, NULL, true));
×
1058
      }
1059

1060
      // up time
1061
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
28✔
1062
      MND_TMQ_NULL_CHECK(pColInfo);
28!
1063
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->createTime, false));
28!
1064

1065
      // subscribe time
1066
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
28✔
1067
      MND_TMQ_NULL_CHECK(pColInfo);
28!
1068
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->subscribeTime, false));
28!
1069

1070
      // rebalance time
1071
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
28✔
1072
      MND_TMQ_NULL_CHECK(pColInfo);
28!
1073
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->rebalanceTime, pConsumer->rebalanceTime == 0));
28!
1074

1075
      char         buf[TSDB_OFFSET_LEN] = {0};
28✔
1076
      STqOffsetVal pVal = {.type = pConsumer->resetOffsetCfg};
28✔
1077
      tFormatOffset(buf, TSDB_OFFSET_LEN, &pVal);
28✔
1078

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

1085
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
28✔
1086
      MND_TMQ_NULL_CHECK(pColInfo);
28!
1087
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)parasStr, false));
28!
1088
      taosMemoryFreeClear(parasStr);
28!
1089
      numOfRows++;
28✔
1090
    }
1091

1092
    taosRUnLockLatch(&pConsumer->lock);
14✔
1093
    sdbRelease(pSdb, pConsumer);
14✔
1094

1095
    pBlock->info.rows = numOfRows;
14✔
1096
  }
1097

1098
  pShow->numOfRows += numOfRows;
10✔
1099
  return numOfRows;
10✔
1100

1101
END:
×
1102
  taosMemoryFreeClear(status);
×
1103
  taosMemoryFreeClear(parasStr);
×
1104
  return code;
×
1105
}
1106

1107
static void mndCancelGetNextConsumer(SMnode *pMnode, void *pIter) {
×
1108
  if (pMnode == NULL || pIter == NULL) return;
×
1109
  SSdb *pSdb = pMnode->pSdb;
×
1110
  sdbCancelFetchByType(pSdb, pIter, SDB_CONSUMER);
×
1111
}
1112

1113
const char *mndConsumerStatusName(int status) {
837✔
1114
  switch (status) {
837!
1115
    case MQ_CONSUMER_STATUS_READY:
301✔
1116
      return "ready";
301✔
1117
//    case MQ_CONSUMER_STATUS_LOST:
1118
//      return "lost";
1119
    case MQ_CONSUMER_STATUS_REBALANCE:
536✔
1120
      return "rebalancing";
536✔
1121
    default:
×
1122
      return "unknown";
×
1123
  }
1124
}
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