• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

taosdata / TDengine / #3548

04 Dec 2024 01:03PM UTC coverage: 59.846% (-0.8%) from 60.691%
#3548

push

travis-ci

web-flow
Merge pull request #29033 from taosdata/fix/calculate-vnode-memory-used

fix/calculate-vnode-memory-used

118484 of 254183 branches covered (46.61%)

Branch coverage included in aggregate %.

199691 of 277471 relevant lines covered (71.97%)

18794141.86 hits per line

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

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

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

28
#define MND_CONSUMER_VER_NUMBER   3
29
#define MND_CONSUMER_RESERVE_SIZE 64
30

31
#define MND_MAX_GROUP_PER_TOPIC 100
32

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

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

44
int32_t mndInitConsumer(SMnode *pMnode) {
1,701✔
45
  SSdbTable table = {
1,701✔
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
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_SUBSCRIBE, mndProcessSubscribeReq);
1,701✔
56
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_HB, mndProcessMqHbReq);
1,701✔
57
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_ASK_EP, mndProcessAskEpReq);
1,701✔
58
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_LOST_CONSUMER_CLEAR, mndProcessConsumerClearMsg);
1,701✔
59

60
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndRetrieveConsumer);
1,701✔
61
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONSUMERS, mndCancelGetNextConsumer);
1,701✔
62

63
  return sdbSetTable(pMnode->pSdb, table);
1,701✔
64
}
65

66
void mndCleanupConsumer(SMnode *pMnode) {}
1,701✔
67

68
int32_t mndSendConsumerMsg(SMnode *pMnode, int64_t consumerId, uint16_t msgType, SRpcHandleInfo *info) {
385✔
69
  int32_t code = 0;
385✔
70
  void   *msg  = rpcMallocCont(sizeof(int64_t));
385✔
71
  MND_TMQ_NULL_CHECK(msg);
385!
72

73
  *(int64_t*)msg = consumerId;
385✔
74
  SRpcMsg rpcMsg = {
385✔
75
      .msgType = msgType,
76
      .pCont = msg,
77
      .contLen = sizeof(int64_t),
78
      .info = *info,
79
  };
80

81
  mInfo("mndSendConsumerMsg type:%d consumer:0x%" PRIx64, msgType, consumerId);
385!
82
  MND_TMQ_RETURN_CHECK(tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg));
385!
83
  return code;
385✔
84

85
END:
×
86
  taosMemoryFree(msg);
×
87
  return code;
×
88
}
89

90
static int32_t validateTopics(STrans* pTrans, SCMSubscribeReq *subscribe, SMnode *pMnode, const char *pUser) {
955✔
91
  SMqTopicObj *pTopic = NULL;
955✔
92
  int32_t      code = 0;
955✔
93

94
  int32_t numOfTopics = taosArrayGetSize(subscribe->topicNames);
955✔
95
  for (int32_t i = 0; i < numOfTopics; i++) {
1,550✔
96
    char *pOneTopic = taosArrayGetP(subscribe->topicNames, i);
598✔
97
    MND_TMQ_RETURN_CHECK(mndAcquireTopic(pMnode, pOneTopic, &pTopic));
601!
98
    MND_TMQ_RETURN_CHECK(mndCheckTopicPrivilege(pMnode, pUser, MND_OPER_SUBSCRIBE, pTopic));
598!
99
    MND_TMQ_RETURN_CHECK(grantCheckExpire(TSDB_GRANT_SUBSCRIPTION));
598!
100

101
    if (subscribe->enableReplay) {
598✔
102
      if (pTopic->subType != TOPIC_SUB_TYPE__COLUMN) {
7✔
103
        code = TSDB_CODE_TMQ_REPLAY_NOT_SUPPORT;
2✔
104
        goto END;
2✔
105
      } else if (pTopic->ntbUid == 0 && pTopic->ctbStbUid == 0) {
5!
106
        SDbObj *pDb = mndAcquireDb(pMnode, pTopic->db);
4✔
107
        if (pDb == NULL) {
4!
108
          code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
109
          goto END;
×
110
        }
111
        if (pDb->cfg.numOfVgroups != 1) {
4✔
112
          mndReleaseDb(pMnode, pDb);
1✔
113
          code = TSDB_CODE_TMQ_REPLAY_NEED_ONE_VGROUP;
1✔
114
          goto END;
1✔
115
        }
116
        mndReleaseDb(pMnode, pDb);
3✔
117
      }
118
    }
119
    char  key[TSDB_CONSUMER_ID_LEN] = {0};
595✔
120
    (void)snprintf(key, TSDB_CONSUMER_ID_LEN, "%"PRIx64, subscribe->consumerId);
595✔
121
    mndTransSetDbName(pTrans, pTopic->db, key);
595✔
122
    MND_TMQ_RETURN_CHECK(mndTransCheckConflict(pMnode, pTrans));
595!
123
    mndReleaseTopic(pMnode, pTopic);
595✔
124
  }
125
  return 0;
952✔
126

127
END:
3✔
128
  mndReleaseTopic(pMnode, pTopic);
3✔
129
  return code;
3✔
130
}
131

132
static int32_t mndProcessConsumerClearMsg(SRpcMsg *pMsg) {
385✔
133
  int32_t              code = 0;
385✔
134
  SMnode              *pMnode = pMsg->info.node;
385✔
135
  SMqConsumerClearMsg *pClearMsg = pMsg->pCont;
385✔
136
  SMqConsumerObj      *pConsumerNew = NULL;
385✔
137
  STrans              *pTrans = NULL;
385✔
138
  SMqConsumerObj      *pConsumer = NULL;
385✔
139

140
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, pClearMsg->consumerId, &pConsumer));
385✔
141
  mInfo("consumer:0x%" PRIx64 " needs to be cleared, status %s", pClearMsg->consumerId,
379!
142
        mndConsumerStatusName(pConsumer->status));
143

144
  MND_TMQ_RETURN_CHECK(tNewSMqConsumerObj(pConsumer->consumerId, pConsumer->cgroup, -1, NULL, NULL, &pConsumerNew));
379!
145
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, pMsg, "clear-csm");
379✔
146
  MND_TMQ_NULL_CHECK(pTrans);
379!
147
  MND_TMQ_RETURN_CHECK(mndSetConsumerDropLogs(pTrans, pConsumerNew));
379!
148
  code = mndTransPrepare(pMnode, pTrans);
379✔
149

150
END:
385✔
151
  mndReleaseConsumer(pMnode, pConsumer);
385✔
152
  tDeleteSMqConsumerObj(pConsumerNew);
385✔
153
  mndTransDrop(pTrans);
385✔
154
  return code;
385✔
155
}
156

157
static int32_t checkPrivilege(SMnode *pMnode, SMqConsumerObj *pConsumer, SMqHbRsp *rsp, char *user) {
2,807✔
158
  int32_t code = 0;
2,807✔
159
  rsp->topicPrivileges = taosArrayInit(taosArrayGetSize(pConsumer->currentTopics), sizeof(STopicPrivilege));
2,807✔
160
  MND_TMQ_NULL_CHECK(rsp->topicPrivileges);
2,807!
161
  for (int32_t i = 0; i < taosArrayGetSize(pConsumer->currentTopics); i++) {
5,404✔
162
    char        *topic = taosArrayGetP(pConsumer->currentTopics, i);
2,597✔
163
    SMqTopicObj *pTopic = NULL;
2,597✔
164
    code = mndAcquireTopic(pMnode, topic, &pTopic);
2,597✔
165
    if (code != TDB_CODE_SUCCESS) {
2,597!
166
      continue;
×
167
    }
168
    STopicPrivilege *data = taosArrayReserve(rsp->topicPrivileges, 1);
2,597✔
169
    MND_TMQ_NULL_CHECK(data);
2,597!
170
    tstrncpy(data->topic, topic, TSDB_TOPIC_FNAME_LEN);
2,597✔
171
    if (mndCheckTopicPrivilege(pMnode, user, MND_OPER_SUBSCRIBE, pTopic) != 0 ||
5,194!
172
        grantCheckExpire(TSDB_GRANT_SUBSCRIPTION) < 0) {
2,597✔
173
      data->noPrivilege = 1;
×
174
    } else {
175
      data->noPrivilege = 0;
2,597✔
176
    }
177
    mndReleaseTopic(pMnode, pTopic);
2,597✔
178
  }
179
END:
2,807✔
180
  return code;
2,807✔
181
}
182

183
static void storeOffsetRows(SMnode *pMnode, SMqHbReq *req, SMqConsumerObj *pConsumer){
2,807✔
184
  for (int i = 0; i < taosArrayGetSize(req->topics); i++) {
5,415✔
185
    TopicOffsetRows *data = taosArrayGet(req->topics, i);
2,608✔
186
    if (data == NULL){
2,608!
187
      continue;
×
188
    }
189
    mInfo("heartbeat report offset rows.%s:%s", pConsumer->cgroup, data->topicName);
2,608!
190

191
    SMqSubscribeObj *pSub = NULL;
2,608✔
192
    char  key[TSDB_SUBSCRIBE_KEY_LEN] = {0};
2,608✔
193
    (void)snprintf(key, TSDB_SUBSCRIBE_KEY_LEN, "%s%s%s", pConsumer->cgroup, TMQ_SEPARATOR, data->topicName);
2,608✔
194
    int32_t code = mndAcquireSubscribeByKey(pMnode, key, &pSub);
2,608✔
195
    if (code != 0) {
2,608!
196
      mError("failed to acquire subscribe by key:%s, code:%d", key, code);
×
197
      continue;
×
198
    }
199
    taosWLockLatch(&pSub->lock);
2,608✔
200
    SMqConsumerEp *pConsumerEp = taosHashGet(pSub->consumerHash, &pConsumer->consumerId, sizeof(int64_t));
2,608✔
201
    if (pConsumerEp) {
2,608✔
202
      taosArrayDestroy(pConsumerEp->offsetRows);
2,556✔
203
      pConsumerEp->offsetRows = data->offsetRows;
2,556✔
204
      data->offsetRows = NULL;
2,556✔
205
    }
206
    taosWUnLockLatch(&pSub->lock);
2,608✔
207

208
    mndReleaseSubscribe(pMnode, pSub);
2,608✔
209
  }
210
}
2,807✔
211

212
static int32_t buildMqHbRsp(SRpcMsg *pMsg, SMqHbRsp *rsp){
2,807✔
213
  int32_t tlen = tSerializeSMqHbRsp(NULL, 0, rsp);
2,807✔
214
  if (tlen <= 0){
2,807!
215
    return TSDB_CODE_TMQ_INVALID_MSG;
×
216
  }
217
  void   *buf = rpcMallocCont(tlen);
2,807✔
218
  if (buf == NULL) {
2,807!
219
    return terrno;
×
220
  }
221

222
  if(tSerializeSMqHbRsp(buf, tlen, rsp) <= 0){
2,807!
223
    rpcFreeCont(buf);
×
224
    return TSDB_CODE_TMQ_INVALID_MSG;
×
225
  }
226
  pMsg->info.rsp = buf;
2,807✔
227
  pMsg->info.rspLen = tlen;
2,807✔
228
  return 0;
2,807✔
229
}
230

231
static int32_t mndProcessMqHbReq(SRpcMsg *pMsg) {
2,825✔
232
  int32_t         code = 0;
2,825✔
233
  SMnode         *pMnode = pMsg->info.node;
2,825✔
234
  SMqHbReq        req = {0};
2,825✔
235
  SMqHbRsp        rsp = {0};
2,825✔
236
  SMqConsumerObj *pConsumer = NULL;
2,825✔
237
  MND_TMQ_RETURN_CHECK(tDeserializeSMqHbReq(pMsg->pCont, pMsg->contLen, &req));
2,825!
238
  int64_t consumerId = req.consumerId;
2,825✔
239
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
2,825✔
240
  MND_TMQ_RETURN_CHECK(checkPrivilege(pMnode, pConsumer, &rsp, pMsg->info.conn.user));
2,807!
241
  atomic_store_32(&pConsumer->hbStatus, 0);
2,807✔
242
  mDebug("consumer:0x%" PRIx64 " receive hb pollFlag:%d %d", consumerId, req.pollFlag, pConsumer->pollStatus);
2,807!
243
  if (req.pollFlag == 1){
2,807✔
244
    atomic_store_32(&pConsumer->pollStatus, 0);
853✔
245
  }
246

247
  storeOffsetRows(pMnode, &req, pConsumer);
2,807✔
248
  rsp.debugFlag = tqClientDebugFlag;
2,807✔
249
  code = buildMqHbRsp(pMsg, &rsp);
2,807✔
250

251
END:
2,825✔
252
  tDestroySMqHbRsp(&rsp);
2,825✔
253
  mndReleaseConsumer(pMnode, pConsumer);
2,825✔
254
  tDestroySMqHbReq(&req);
2,825✔
255
  return code;
2,825✔
256
}
257

258
static int32_t addEpSetInfo(SMnode *pMnode, SMqConsumerObj *pConsumer, int32_t epoch, SMqAskEpRsp *rsp){
3,686✔
259
  taosRLockLatch(&pConsumer->lock);
3,686✔
260

261
  int32_t numOfTopics = taosArrayGetSize(pConsumer->currentTopics);
3,686✔
262

263
  rsp->topics = taosArrayInit(numOfTopics, sizeof(SMqSubTopicEp));
3,686✔
264
  if (rsp->topics == NULL) {
3,686!
265
    taosRUnLockLatch(&pConsumer->lock);
×
266
    return terrno;
×
267
  }
268

269
  // handle all topics subscribed by this consumer
270
  for (int32_t i = 0; i < numOfTopics; i++) {
7,052✔
271
    char            *topic = taosArrayGetP(pConsumer->currentTopics, i);
3,366✔
272
    SMqSubscribeObj *pSub = NULL;
3,366✔
273
    char  key[TSDB_SUBSCRIBE_KEY_LEN] = {0};
3,366✔
274
    (void)snprintf(key, TSDB_SUBSCRIBE_KEY_LEN, "%s%s%s", pConsumer->cgroup, TMQ_SEPARATOR, topic);
3,366✔
275
    int32_t code = mndAcquireSubscribeByKey(pMnode, key, &pSub);
3,366✔
276
    if (code != 0) {
3,366!
277
      continue;
×
278
    }
279
    taosRLockLatch(&pSub->lock);
3,366✔
280

281
    SMqSubTopicEp topicEp = {0};
3,366✔
282
    tstrncpy(topicEp.topic, topic, TSDB_TOPIC_FNAME_LEN);
3,366✔
283

284
    // 2.1 fetch topic schema
285
    SMqTopicObj *pTopic = NULL;
3,366✔
286
    code = mndAcquireTopic(pMnode, topic, &pTopic);
3,366✔
287
    if (code != TDB_CODE_SUCCESS) {
3,366!
288
      taosRUnLockLatch(&pSub->lock);
×
289
      mndReleaseSubscribe(pMnode, pSub);
×
290
      continue;
×
291
    }
292
    taosRLockLatch(&pTopic->lock);
3,366✔
293
    tstrncpy(topicEp.db, pTopic->db, TSDB_DB_FNAME_LEN);
3,366✔
294
    topicEp.schema.nCols = pTopic->schema.nCols;
3,366✔
295
    if (topicEp.schema.nCols) {
3,366✔
296
      topicEp.schema.pSchema = taosMemoryCalloc(topicEp.schema.nCols, sizeof(SSchema));
2,291✔
297
      if (topicEp.schema.pSchema == NULL) {
2,291!
298
        taosRUnLockLatch(&pTopic->lock);
×
299
        taosRUnLockLatch(&pSub->lock);
×
300
        mndReleaseSubscribe(pMnode, pSub);
×
301
        mndReleaseTopic(pMnode, pTopic);
×
302
        return terrno;
×
303
      }
304
      (void)memcpy(topicEp.schema.pSchema, pTopic->schema.pSchema, topicEp.schema.nCols * sizeof(SSchema));
2,291✔
305
    }
306
    taosRUnLockLatch(&pTopic->lock);
3,366✔
307
    mndReleaseTopic(pMnode, pTopic);
3,366✔
308

309
    // 2.2 iterate all vg assigned to the consumer of that topic
310
    SMqConsumerEp *pConsumerEp = taosHashGet(pSub->consumerHash, &pConsumer->consumerId, sizeof(int64_t));
3,366✔
311
    if (pConsumerEp == NULL) {
3,366!
312
      taosRUnLockLatch(&pConsumer->lock);
×
313
      taosRUnLockLatch(&pSub->lock);
×
314
      mndReleaseSubscribe(pMnode, pSub);
×
315
      return TSDB_CODE_OUT_OF_MEMORY;
×
316
    }
317
    int32_t vgNum = taosArrayGetSize(pConsumerEp->vgs);
3,366✔
318
    topicEp.vgs = taosArrayInit(vgNum, sizeof(SMqSubVgEp));
3,366✔
319
    if (topicEp.vgs == NULL) {
3,366!
320
      taosRUnLockLatch(&pConsumer->lock);
×
321
      taosRUnLockLatch(&pSub->lock);
×
322
      mndReleaseSubscribe(pMnode, pSub);
×
323
      return terrno;
×
324
    }
325

326
    for (int32_t j = 0; j < vgNum; j++) {
7,596✔
327
      SMqVgEp *pVgEp = taosArrayGetP(pConsumerEp->vgs, j);
4,230✔
328
      if (pVgEp == NULL) {
4,230!
329
        continue;
×
330
      }
331
      if (epoch == -1) {
4,230✔
332
        SVgObj *pVgroup = mndAcquireVgroup(pMnode, pVgEp->vgId);
2,692✔
333
        if (pVgroup) {
2,692✔
334
          pVgEp->epSet = mndGetVgroupEpset(pMnode, pVgroup);
152✔
335
          mndReleaseVgroup(pMnode, pVgroup);
152✔
336
        }
337
      }
338
      SMqSubVgEp vgEp = {.epSet = pVgEp->epSet, .vgId = pVgEp->vgId, .offset = -1};
4,230✔
339
      if (taosArrayPush(topicEp.vgs, &vgEp) == NULL) {
8,460!
340
        taosArrayDestroy(topicEp.vgs);
×
341
        taosRUnLockLatch(&pConsumer->lock);
×
342
        taosRUnLockLatch(&pSub->lock);
×
343
        mndReleaseSubscribe(pMnode, pSub);
×
344
        return terrno;
×
345
      }
346
    }
347
    if (taosArrayPush(rsp->topics, &topicEp) == NULL) {
6,732!
348
      taosArrayDestroy(topicEp.vgs);
×
349
      taosRUnLockLatch(&pConsumer->lock);
×
350
      taosRUnLockLatch(&pSub->lock);
×
351
      mndReleaseSubscribe(pMnode, pSub);
×
352
      return terrno;
×
353
    }
354
    taosRUnLockLatch(&pSub->lock);
3,366✔
355
    mndReleaseSubscribe(pMnode, pSub);
3,366✔
356
  }
357
  taosRUnLockLatch(&pConsumer->lock);
3,686✔
358
  return 0;
3,686✔
359
}
360

361
static int32_t buildAskEpRsp(SRpcMsg *pMsg, SMqAskEpRsp *rsp, int32_t serverEpoch, int64_t consumerId){
9,152✔
362
  int32_t code = 0;
9,152✔
363
  // encode rsp
364
  int32_t tlen = sizeof(SMqRspHead) + tEncodeSMqAskEpRsp(NULL, rsp);
9,152✔
365
  void   *buf = rpcMallocCont(tlen);
9,152✔
366
  if (buf == NULL) {
9,156!
367
    return terrno;
×
368
  }
369

370
  SMqRspHead *pHead = buf;
9,156✔
371

372
  pHead->mqMsgType = TMQ_MSG_TYPE__EP_RSP;
9,156✔
373
  pHead->epoch = serverEpoch;
9,156✔
374
  pHead->consumerId = consumerId;
9,156✔
375
  pHead->walsver = 0;
9,156✔
376
  pHead->walever = 0;
9,156✔
377

378
  void *abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
9,156✔
379
  if (tEncodeSMqAskEpRsp(&abuf, rsp) < 0) {
9,155!
380
    rpcFreeCont(buf);
×
381
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
382
  }
383

384
  // send rsp
385
  pMsg->info.rsp = buf;
9,155✔
386
  pMsg->info.rspLen = tlen;
9,155✔
387
  return code;
9,155✔
388
}
389

390
static int32_t mndProcessAskEpReq(SRpcMsg *pMsg) {
12,069✔
391
  SMnode     *pMnode = pMsg->info.node;
12,069✔
392
  SMqAskEpReq req = {0};
12,069✔
393
  SMqAskEpRsp rsp = {0};
12,069✔
394
  int32_t     code = 0;
12,069✔
395
  SMqConsumerObj *pConsumer = NULL;
12,069✔
396

397
  MND_TMQ_RETURN_CHECK(tDeserializeSMqAskEpReq(pMsg->pCont, pMsg->contLen, &req));
12,069!
398
  int64_t consumerId = req.consumerId;
12,068✔
399
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
12,068✔
400
  if (strncmp(req.cgroup, pConsumer->cgroup, tListLen(pConsumer->cgroup)) != 0) {
11,975!
401
    mError("consumer:0x%" PRIx64 " group:%s not consistent with data in sdb, saved cgroup:%s", consumerId, req.cgroup,
×
402
           pConsumer->cgroup);
403
    code = TSDB_CODE_MND_CONSUMER_NOT_EXIST;
×
404
    goto END;
×
405
  }
406

407
  // 1. check consumer status
408
  int32_t status = atomic_load_32(&pConsumer->status);
11,975✔
409
  if (status != MQ_CONSUMER_STATUS_READY) {
11,975✔
410
    mInfo("consumer:0x%" PRIx64 " not ready, status: %s", consumerId, mndConsumerStatusName(status));
2,819!
411
    code = TSDB_CODE_MND_CONSUMER_NOT_READY;
2,819✔
412
    goto END;
2,819✔
413
  }
414

415
  int32_t epoch = req.epoch;
9,156✔
416
  int32_t serverEpoch = atomic_load_32(&pConsumer->epoch);
9,156✔
417

418
  // 2. check epoch, only send ep info when epochs do not match
419
  if (epoch != serverEpoch) {
9,154✔
420
    mInfo("process ask ep, consumer:0x%" PRIx64 "(epoch %d) update with server epoch %d",
3,686!
421
          consumerId, epoch, serverEpoch);
422
    MND_TMQ_RETURN_CHECK(addEpSetInfo(pMnode, pConsumer, epoch, &rsp));
3,686!
423
  }
424

425
  code = buildAskEpRsp(pMsg, &rsp, serverEpoch, consumerId);
9,154✔
426

427
END:
12,067✔
428
  tDeleteSMqAskEpRsp(&rsp);
429
  mndReleaseConsumer(pMnode, pConsumer);
12,068✔
430
  return code;
12,069✔
431
}
432

433
int32_t mndSetConsumerDropLogs(STrans *pTrans, SMqConsumerObj *pConsumer) {
379✔
434
  int32_t  code = 0;
379✔
435
  SSdbRaw *pCommitRaw = mndConsumerActionEncode(pConsumer);
379✔
436
  MND_TMQ_NULL_CHECK(pCommitRaw);
379!
437
  code = mndTransAppendCommitlog(pTrans, pCommitRaw);
379✔
438
  if (code != 0) {
379!
439
    sdbFreeRaw(pCommitRaw);
×
440
    goto END;
×
441
  }
442
  MND_TMQ_RETURN_CHECK(sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED));
379!
443
END:
379✔
444
  return code;
379✔
445
}
446

447
int32_t mndSetConsumerCommitLogs(STrans *pTrans, SMqConsumerObj *pConsumer) {
2,075✔
448
  int32_t  code = 0;
2,075✔
449
  SSdbRaw *pCommitRaw = mndConsumerActionEncode(pConsumer);
2,075✔
450
  MND_TMQ_NULL_CHECK(pCommitRaw);
2,075!
451
  code = mndTransAppendCommitlog(pTrans, pCommitRaw);
2,075✔
452
  if (code != 0) {
2,075!
453
    sdbFreeRaw(pCommitRaw);
×
454
    goto END;
×
455
  }
456
  MND_TMQ_RETURN_CHECK(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
2,075!
457
END:
2,075✔
458
  return code;
2,075✔
459
}
460

461
static void freeItem(void *param) {
1✔
462
  void *pItem = *(void **)param;
1✔
463
  if (pItem != NULL) {
1!
464
    taosMemoryFree(pItem);
1✔
465
  }
466
}
1✔
467

468
#define ADD_TOPIC_TO_ARRAY(element, array) \
469
char *newTopicCopy = taosStrdup(element); \
470
MND_TMQ_NULL_CHECK(newTopicCopy);\
471
if (taosArrayPush(pConsumerNew->array, &newTopicCopy) == NULL){\
472
  taosMemoryFree(newTopicCopy);\
473
  code = terrno;\
474
  goto END;\
475
}
476

477
static int32_t getTopicAddDelete(SMqConsumerObj *pExistedConsumer, SMqConsumerObj *pConsumerNew){
430✔
478
  int32_t code = 0;
430✔
479
  pConsumerNew->rebNewTopics = taosArrayInit(0, sizeof(void *));
430✔
480
  MND_TMQ_NULL_CHECK(pConsumerNew->rebNewTopics);
430!
481
  pConsumerNew->rebRemovedTopics = taosArrayInit(0, sizeof(void *));
430✔
482
  MND_TMQ_NULL_CHECK(pConsumerNew->rebRemovedTopics);
430!
483

484
  int32_t newTopicNum = taosArrayGetSize(pConsumerNew->assignedTopics);
430✔
485
  int32_t oldTopicNum = taosArrayGetSize(pExistedConsumer->currentTopics);
430✔
486
  int32_t i = 0, j = 0;
430✔
487
  while (i < oldTopicNum || j < newTopicNum) {
918✔
488
    if (i >= oldTopicNum) {
488✔
489
      void* tmp = taosArrayGetP(pConsumerNew->assignedTopics, j);
3✔
490
      MND_TMQ_NULL_CHECK(tmp);
3!
491
      ADD_TOPIC_TO_ARRAY(tmp, rebNewTopics);
6!
492
      j++;
3✔
493
      continue;
3✔
494
    } else if (j >= newTopicNum) {
485✔
495
      void* tmp = taosArrayGetP(pExistedConsumer->currentTopics, i);
475✔
496
      MND_TMQ_NULL_CHECK(tmp);
475!
497
      ADD_TOPIC_TO_ARRAY(tmp, rebRemovedTopics);
950!
498
      i++;
475✔
499
      continue;
475✔
500
    } else {
501
      char *oldTopic = taosArrayGetP(pExistedConsumer->currentTopics, i);
10✔
502
      MND_TMQ_NULL_CHECK(oldTopic);
10!
503
      char *newTopic = taosArrayGetP(pConsumerNew->assignedTopics, j);
10✔
504
      MND_TMQ_NULL_CHECK(newTopic);
10!
505
      int   comp = strcmp(oldTopic, newTopic);
10✔
506
      if (comp == 0) {
10!
507
        i++;
10✔
508
        j++;
10✔
509
        continue;
10✔
510
      } else if (comp < 0) {
×
511
        ADD_TOPIC_TO_ARRAY(oldTopic, rebRemovedTopics);
×
512
        i++;
×
513
        continue;
×
514
      } else {
×
515
        ADD_TOPIC_TO_ARRAY(newTopic, rebNewTopics);
×
516
        j++;
×
517
        continue;
×
518
      }
519
    }
520
  }
521
  // no topics need to be rebalanced
522
  if (taosArrayGetSize(pConsumerNew->rebNewTopics) == 0 && taosArrayGetSize(pConsumerNew->rebRemovedTopics) == 0) {
430✔
523
    code = TSDB_CODE_TMQ_NO_NEED_REBALANCE;
10✔
524
  }
525

526
END:
420✔
527
  return code;
430✔
528
}
529

530
static int32_t checkAndSortTopic(SMnode *pMnode, SArray *pTopicList){
956✔
531
  taosArraySort(pTopicList, taosArrayCompareString);
956✔
532
  taosArrayRemoveDuplicate(pTopicList, taosArrayCompareString, freeItem);
956✔
533

534
  int32_t newTopicNum = taosArrayGetSize(pTopicList);
956✔
535
  for (int i = 0; i < newTopicNum; i++) {
1,554✔
536
    int32_t gNum = mndGetGroupNumByTopic(pMnode, (const char *)taosArrayGetP(pTopicList, i));
599✔
537
    if (gNum >= MND_MAX_GROUP_PER_TOPIC) {
599✔
538
      return TSDB_CODE_TMQ_GROUP_OUT_OF_RANGE;
1✔
539
    }
540
  }
541
  return 0;
955✔
542
}
543

544
static int32_t buildSubConsumer(SMnode *pMnode, SCMSubscribeReq *subscribe, SMqConsumerObj** ppConsumer){
952✔
545
  int64_t         consumerId = subscribe->consumerId;
952✔
546
  char           *cgroup     = subscribe->cgroup;
952✔
547
  SMqConsumerObj *pConsumerNew     = NULL;
952✔
548
  SMqConsumerObj *pExistedConsumer = NULL;
952✔
549
  int32_t code = mndAcquireConsumer(pMnode, consumerId, &pExistedConsumer);
952✔
550
  if (code != 0) {
952✔
551
    mInfo("receive subscribe request from new consumer:0x%" PRIx64
522!
552
              ",cgroup:%s, numOfTopics:%d", consumerId,
553
          subscribe->cgroup, (int32_t)taosArrayGetSize(subscribe->topicNames));
554

555
    MND_TMQ_RETURN_CHECK(tNewSMqConsumerObj(consumerId, cgroup, CONSUMER_INSERT_SUB, NULL, subscribe, &pConsumerNew));
522!
556
  } else {
557
    int32_t status = atomic_load_32(&pExistedConsumer->status);
430✔
558

559
    mInfo("receive subscribe request from existed consumer:0x%" PRIx64
430!
560
              ",cgroup:%s, current status:%d(%s), subscribe topic num: %d",
561
          consumerId, subscribe->cgroup, status, mndConsumerStatusName(status),
562
          (int32_t)taosArrayGetSize(subscribe->topicNames));
563

564
    if (status != MQ_CONSUMER_STATUS_READY) {
430!
565
      code = TSDB_CODE_MND_CONSUMER_NOT_READY;
×
566
      goto END;
×
567
    }
568
    MND_TMQ_RETURN_CHECK(tNewSMqConsumerObj(consumerId, cgroup, CONSUMER_UPDATE_SUB, NULL, subscribe, &pConsumerNew));
430!
569
    MND_TMQ_RETURN_CHECK(getTopicAddDelete(pExistedConsumer, pConsumerNew));
430✔
570
  }
571
  mndReleaseConsumer(pMnode, pExistedConsumer);
942✔
572
  if (ppConsumer){
942!
573
    *ppConsumer = pConsumerNew;
942✔
574
  }
575
  return code;
942✔
576

577
END:
10✔
578
  mndReleaseConsumer(pMnode, pExistedConsumer);
10✔
579
  tDeleteSMqConsumerObj(pConsumerNew);
10✔
580
  return code;
10✔
581
}
582

583
int32_t mndProcessSubscribeReq(SRpcMsg *pMsg) {
1,314✔
584
  SMnode *pMnode = pMsg->info.node;
1,314✔
585
  char   *msgStr = pMsg->pCont;
1,314✔
586
  int32_t code = 0;
1,314✔
587
  SMqConsumerObj *pConsumerNew = NULL;
1,314✔
588
  STrans         *pTrans = NULL;
1,314✔
589

590
  SCMSubscribeReq subscribe = {0};
1,314✔
591
  MND_TMQ_RETURN_CHECK(tDeserializeSCMSubscribeReq(msgStr, &subscribe, pMsg->contLen));
2,628!
592
  bool unSubscribe = (taosArrayGetSize(subscribe.topicNames) == 0);
1,314✔
593
  if(unSubscribe){
1,314✔
594
    SMqConsumerObj *pConsumerTmp = NULL;
775✔
595
    MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, subscribe.consumerId, &pConsumerTmp));
1,130✔
596
    if (taosArrayGetSize(pConsumerTmp->assignedTopics) == 0){
772✔
597
      mndReleaseConsumer(pMnode, pConsumerTmp);
355✔
598
      goto END;
355✔
599
    }
600
    mndReleaseConsumer(pMnode, pConsumerTmp);
417✔
601
  }
602
  MND_TMQ_RETURN_CHECK(checkAndSortTopic(pMnode, subscribe.topicNames));
956✔
603
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY,
955✔
604
                          (unSubscribe ? TRN_CONFLICT_NOTHING :TRN_CONFLICT_DB_INSIDE),
605
                          pMsg, "subscribe");
606
  MND_TMQ_NULL_CHECK(pTrans);
955!
607

608
  MND_TMQ_RETURN_CHECK(validateTopics(pTrans, &subscribe, pMnode, pMsg->info.conn.user));
955✔
609
  MND_TMQ_RETURN_CHECK(buildSubConsumer(pMnode, &subscribe, &pConsumerNew));
952✔
610
  MND_TMQ_RETURN_CHECK(mndSetConsumerCommitLogs(pTrans, pConsumerNew));
942!
611
  MND_TMQ_RETURN_CHECK(mndTransPrepare(pMnode, pTrans));
942!
612
  code = TSDB_CODE_ACTION_IN_PROGRESS;
942✔
613

614
END:
1,314✔
615
  mndTransDrop(pTrans);
1,314✔
616
  tDeleteSMqConsumerObj(pConsumerNew);
1,314✔
617
  taosArrayDestroyP(subscribe.topicNames, (FDelete)taosMemoryFree);
1,314✔
618
  return (code == TSDB_CODE_TMQ_NO_NEED_REBALANCE || code == TSDB_CODE_MND_CONSUMER_NOT_EXIST) ? 0 : code;
1,314✔
619
}
620

621
SSdbRaw *mndConsumerActionEncode(SMqConsumerObj *pConsumer) {
2,823✔
622
  int32_t code = 0;
2,823✔
623
  int32_t lino = 0;
2,823✔
624
  terrno = TSDB_CODE_OUT_OF_MEMORY;
2,823✔
625

626
  void   *buf = NULL;
2,823✔
627
  int32_t tlen = tEncodeSMqConsumerObj(NULL, pConsumer);
2,823✔
628
  int32_t size = sizeof(int32_t) + tlen + MND_CONSUMER_RESERVE_SIZE;
2,823✔
629

630
  SSdbRaw *pRaw = sdbAllocRaw(SDB_CONSUMER, MND_CONSUMER_VER_NUMBER, size);
2,823✔
631
  if (pRaw == NULL) goto CM_ENCODE_OVER;
2,823!
632

633
  buf = taosMemoryMalloc(tlen);
2,823✔
634
  if (buf == NULL) goto CM_ENCODE_OVER;
2,823!
635

636
  void *abuf = buf;
2,823✔
637
  if(tEncodeSMqConsumerObj(&abuf, pConsumer) < 0){
2,823!
638
    goto CM_ENCODE_OVER;
×
639
  }
640

641
  int32_t dataPos = 0;
2,823✔
642
  SDB_SET_INT32(pRaw, dataPos, tlen, CM_ENCODE_OVER);
2,823!
643
  SDB_SET_BINARY(pRaw, dataPos, buf, tlen, CM_ENCODE_OVER);
2,823!
644
  SDB_SET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_ENCODE_OVER);
2,823!
645
  SDB_SET_DATALEN(pRaw, dataPos, CM_ENCODE_OVER);
2,823!
646

647
  terrno = TSDB_CODE_SUCCESS;
2,823✔
648

649
CM_ENCODE_OVER:
2,823✔
650
  taosMemoryFreeClear(buf);
2,823!
651
  if (terrno != 0) {
2,823!
652
    mError("consumer:0x%" PRIx64 " failed to encode to raw:%p since %s", pConsumer->consumerId, pRaw, terrstr());
×
653
    sdbFreeRaw(pRaw);
×
654
    return NULL;
×
655
  }
656

657
  mTrace("consumer:0x%" PRIx64 ", encode to raw:%p, row:%p", pConsumer->consumerId, pRaw, pConsumer);
2,823✔
658
  return pRaw;
2,823✔
659
}
660

661
SSdbRow *mndConsumerActionDecode(SSdbRaw *pRaw) {
2,498✔
662
  int32_t         code = 0;
2,498✔
663
  int32_t         lino = 0;
2,498✔
664
  SSdbRow        *pRow = NULL;
2,498✔
665
  SMqConsumerObj *pConsumer = NULL;
2,498✔
666
  void           *buf = NULL;
2,498✔
667

668
  terrno = 0;
2,498✔
669
  int8_t sver = 0;
2,498✔
670
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
2,498!
671
    goto CM_DECODE_OVER;
×
672
  }
673

674
  if (sver < 1 || sver > MND_CONSUMER_VER_NUMBER) {
2,498!
675
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
676
    goto CM_DECODE_OVER;
×
677
  }
678

679
  pRow = sdbAllocRow(sizeof(SMqConsumerObj));
2,498✔
680
  if (pRow == NULL) {
2,498!
681
    goto CM_DECODE_OVER;
×
682
  }
683

684
  pConsumer = sdbGetRowObj(pRow);
2,498✔
685
  if (pConsumer == NULL) {
2,498!
686
    goto CM_DECODE_OVER;
×
687
  }
688

689
  int32_t dataPos = 0;
2,498✔
690
  int32_t len;
691
  SDB_GET_INT32(pRaw, dataPos, &len, CM_DECODE_OVER);
2,498!
692
  buf = taosMemoryMalloc(len);
2,498✔
693
  if (buf == NULL) {
2,498!
694
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
695
    goto CM_DECODE_OVER;
×
696
  }
697

698
  SDB_GET_BINARY(pRaw, dataPos, buf, len, CM_DECODE_OVER);
2,498!
699
  SDB_GET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_DECODE_OVER);
2,498!
700

701
  if (tDecodeSMqConsumerObj(buf, pConsumer, sver) == NULL) {
2,498!
702
    terrno = TSDB_CODE_OUT_OF_MEMORY;  // TODO set correct error code
×
703
    goto CM_DECODE_OVER;
×
704
  }
705

706
  tmsgUpdateDnodeEpSet(&pConsumer->ep);
2,498✔
707

708
CM_DECODE_OVER:
2,498✔
709
  taosMemoryFreeClear(buf);
2,498!
710
  if (terrno != TSDB_CODE_SUCCESS) {
2,498!
711
    mError("consumer:0x%" PRIx64 " failed to decode from raw:%p since %s",
×
712
           pConsumer == NULL ? 0 : pConsumer->consumerId, pRaw, terrstr());
713
    taosMemoryFreeClear(pRow);
×
714
  }
715

716
  return pRow;
2,498✔
717
}
718

719
static int32_t mndConsumerActionInsert(SSdb *pSdb, SMqConsumerObj *pConsumer) {
548✔
720
  mInfo("consumer:0x%" PRIx64 " sub insert, cgroup:%s status:%d(%s) epoch:%d", pConsumer->consumerId, pConsumer->cgroup,
548!
721
        pConsumer->status, mndConsumerStatusName(pConsumer->status), pConsumer->epoch);
722
  pConsumer->subscribeTime = pConsumer->createTime;
548✔
723
  return 0;
548✔
724
}
725

726
static int32_t mndConsumerActionDelete(SSdb *pSdb, SMqConsumerObj *pConsumer) {
2,498✔
727
  mInfo("consumer:0x%" PRIx64 " perform delete action, status:(%d)%s", pConsumer->consumerId, pConsumer->status,
2,498!
728
        mndConsumerStatusName(pConsumer->status));
729
  tClearSMqConsumerObj(pConsumer);
2,498✔
730
  return 0;
2,498✔
731
}
732

733
//static void updateConsumerStatus(SMqConsumerObj *pConsumer) {
734
//  int32_t status = pConsumer->status;
735
//
736
//  if (taosArrayGetSize(pConsumer->rebNewTopics) == 0 && taosArrayGetSize(pConsumer->rebRemovedTopics) == 0) {
737
//    if (status == MQ_CONSUMER_STATUS_REBALANCE) {
738
//      pConsumer->status = MQ_CONSUMER_STATUS_READY;
739
//    } else if (status == MQ_CONSUMER_STATUS_READY && taosArrayGetSize(pConsumer->currentTopics) == 0) {
740
//      pConsumer->status = MQ_CONSUMER_STATUS_LOST;
741
//    }
742
//  }
743
//}
744

745
// remove from topic list
746
static void removeFromTopicList(SArray *topicList, const char *pTopic, int64_t consumerId, char *type) {
1,567✔
747
  int32_t size = taosArrayGetSize(topicList);
1,567✔
748
  for (int32_t i = 0; i < size; i++) {
1,693✔
749
    char *p = taosArrayGetP(topicList, i);
1,684✔
750
    if (strcmp(pTopic, p) == 0) {
1,684✔
751
      taosArrayRemove(topicList, i);
1,558✔
752
      taosMemoryFree(p);
1,558✔
753

754
      mInfo("[rebalance] consumer:0x%" PRIx64 " remove topic:%s in the %s topic list, remain newTopics:%d",
1,558!
755
            consumerId, pTopic, type, (int)taosArrayGetSize(topicList));
756
      break;
1,558✔
757
    }
758
  }
759
}
1,567✔
760

761
static bool existInCurrentTopicList(const SMqConsumerObj *pConsumer, const char *pTopic) {
591✔
762
  bool    existing = false;
591✔
763
  int32_t size = taosArrayGetSize(pConsumer->currentTopics);
591✔
764
  for (int32_t i = 0; i < size; i++) {
669✔
765
    char *topic = taosArrayGetP(pConsumer->currentTopics, i);
78✔
766
    if (topic && strcmp(topic, pTopic) == 0) {
78!
767
      existing = true;
×
768
      break;
×
769
    }
770
  }
771

772
  return existing;
591✔
773
}
774

775
static int32_t mndConsumerActionUpdate(SSdb *pSdb, SMqConsumerObj *pOldConsumer, SMqConsumerObj *pNewConsumer) {
1,567✔
776
  mInfo("consumer:0x%" PRIx64 " perform update action, update type:%d, subscribe-time:%" PRId64 ", createTime:%" PRId64,
1,567!
777
        pOldConsumer->consumerId, pNewConsumer->updateType, pOldConsumer->subscribeTime, pOldConsumer->createTime);
778

779
  taosWLockLatch(&pOldConsumer->lock);
1,567✔
780

781
  if (pNewConsumer->updateType == CONSUMER_UPDATE_SUB) {
1,567✔
782
    TSWAP(pOldConsumer->rebNewTopics, pNewConsumer->rebNewTopics);
424✔
783
    TSWAP(pOldConsumer->rebRemovedTopics, pNewConsumer->rebRemovedTopics);
424✔
784
    TSWAP(pOldConsumer->assignedTopics, pNewConsumer->assignedTopics);
424✔
785

786
    pOldConsumer->subscribeTime = taosGetTimestampMs();
424✔
787
    pOldConsumer->status = MQ_CONSUMER_STATUS_REBALANCE;
424✔
788
    mInfo("consumer:0x%" PRIx64 " subscribe update, modify existed consumer", pOldConsumer->consumerId);
424!
789
  } else if (pNewConsumer->updateType == CONSUMER_UPDATE_REB) {
1,143✔
790
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
64✔
791
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
64✔
792
    mInfo("[rebalance] consumer:0x%" PRIx64 " rebalance update, only rebalance time", pOldConsumer->consumerId);
64!
793
  } else if (pNewConsumer->updateType == CONSUMER_ADD_REB) {
1,079✔
794
    void *tmp = taosArrayGetP(pNewConsumer->rebNewTopics, 0);
591✔
795
    if (tmp == NULL){
591!
796
      return TSDB_CODE_TMQ_INVALID_MSG;
×
797
    }
798
    char *pNewTopic = taosStrdup(tmp);
591✔
799
    if (pNewTopic == NULL) {
591!
800
      return terrno;
×
801
    }
802
    removeFromTopicList(pOldConsumer->rebNewTopics, pNewTopic, pOldConsumer->consumerId, "new");
591✔
803
    bool existing = existInCurrentTopicList(pOldConsumer, pNewTopic);
591✔
804
    if (existing) {
591!
805
      mError("[rebalance] consumer:0x%" PRIx64 " add new topic:%s should not in currentTopics", pOldConsumer->consumerId, pNewTopic);
×
806
      taosMemoryFree(pNewTopic);
×
807
    } else {
808
      if (taosArrayPush(pOldConsumer->currentTopics, &pNewTopic) == NULL) {
1,182!
809
        taosMemoryFree(pNewTopic);
×
810
        return TSDB_CODE_TMQ_INVALID_MSG;
×
811
      }
812
      taosArraySort(pOldConsumer->currentTopics, taosArrayCompareString);
591✔
813
    }
814

815
    int32_t status = pOldConsumer->status;
591✔
816
//    updateConsumerStatus(pOldConsumer);
817
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
591!
818
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
531✔
819
    }
820

821
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
591✔
822
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
591✔
823

824
    mInfo("[rebalance] consumer:0x%" PRIx64 " rebalance update add, state (%d)%s -> (%d)%s, new epoch:%d, reb-time:%" PRId64
591!
825
          ", current topics:%d, newTopics:%d, removeTopics:%d",
826
          pOldConsumer->consumerId, status, mndConsumerStatusName(status), pOldConsumer->status,
827
          mndConsumerStatusName(pOldConsumer->status), pOldConsumer->epoch, pOldConsumer->rebalanceTime,
828
          (int)taosArrayGetSize(pOldConsumer->currentTopics), (int)taosArrayGetSize(pOldConsumer->rebNewTopics),
829
          (int)taosArrayGetSize(pOldConsumer->rebRemovedTopics));
830

831
  } else if (pNewConsumer->updateType == CONSUMER_REMOVE_REB) {
488!
832
    char *topic = taosArrayGetP(pNewConsumer->rebRemovedTopics, 0);
488✔
833
    if (topic == NULL){
488!
834
      return TSDB_CODE_TMQ_INVALID_MSG;
×
835
    }
836
    removeFromTopicList(pOldConsumer->rebRemovedTopics, topic, pOldConsumer->consumerId, "remove");
488✔
837
    removeFromTopicList(pOldConsumer->currentTopics, topic, pOldConsumer->consumerId, "current");
488✔
838

839
    int32_t status = pOldConsumer->status;
488✔
840
//    updateConsumerStatus(pOldConsumer);
841
    if (taosArrayGetSize(pOldConsumer->rebNewTopics) == 0 && taosArrayGetSize(pOldConsumer->rebRemovedTopics) == 0) {
488!
842
      pOldConsumer->status = MQ_CONSUMER_STATUS_READY;
430✔
843
    }
844
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
488✔
845
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
488✔
846

847
    mInfo("[rebalance]consumer:0x%" PRIx64 " rebalance update remove, state (%d)%s -> (%d)%s, new epoch:%d, reb-time:%" PRId64
488!
848
          ", current topics:%d, newTopics:%d, removeTopics:%d",
849
          pOldConsumer->consumerId, status, mndConsumerStatusName(status), pOldConsumer->status,
850
          mndConsumerStatusName(pOldConsumer->status), pOldConsumer->epoch, pOldConsumer->rebalanceTime,
851
          (int)taosArrayGetSize(pOldConsumer->currentTopics), (int)taosArrayGetSize(pOldConsumer->rebNewTopics),
852
          (int)taosArrayGetSize(pOldConsumer->rebRemovedTopics));
853
  }
854

855
  taosWUnLockLatch(&pOldConsumer->lock);
1,567✔
856
  return 0;
1,567✔
857
}
858

859
int32_t mndAcquireConsumer(SMnode *pMnode, int64_t consumerId, SMqConsumerObj** pConsumer) {
17,812✔
860
  SSdb           *pSdb = pMnode->pSdb;
17,812✔
861
  *pConsumer = sdbAcquire(pSdb, SDB_CONSUMER, &consumerId);
17,812✔
862
  if (*pConsumer == NULL) {
17,813✔
863
    return TSDB_CODE_MND_CONSUMER_NOT_EXIST;
642✔
864
  }
865
  return 0;
17,171✔
866
}
867

868
void mndReleaseConsumer(SMnode *pMnode, SMqConsumerObj *pConsumer) {
21,802✔
869
  SSdb *pSdb = pMnode->pSdb;
21,802✔
870
  sdbRelease(pSdb, pConsumer);
21,802✔
871
}
21,803✔
872

873
static int32_t mndRetrieveConsumer(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
7,987✔
874
  SMnode         *pMnode = pReq->info.node;
7,987✔
875
  SSdb           *pSdb = pMnode->pSdb;
7,987✔
876
  int32_t         numOfRows = 0;
7,987✔
877
  SMqConsumerObj *pConsumer = NULL;
7,987✔
878
  int32_t         code = 0;
7,987✔
879
  char           *parasStr = NULL;
7,987✔
880
  char           *status = NULL;
7,987✔
881

882
  while (numOfRows < rowsCapacity) {
8,170✔
883
    pShow->pIter = sdbFetch(pSdb, SDB_CONSUMER, pShow->pIter, (void **)&pConsumer);
8,169✔
884
    if (pShow->pIter == NULL) {
8,183✔
885
      break;
8,005✔
886
    }
887

888
    if (taosArrayGetSize(pConsumer->assignedTopics) == 0) {
178✔
889
      mInfo("showing consumer:0x%" PRIx64 " no assigned topic, skip", pConsumer->consumerId);
4!
890
      sdbRelease(pSdb, pConsumer);
4✔
891
      continue;
4✔
892
    }
893

894
    taosRLockLatch(&pConsumer->lock);
176✔
895
    mInfo("showing consumer:0x%" PRIx64, pConsumer->consumerId);
176!
896

897
    int32_t topicSz = taosArrayGetSize(pConsumer->assignedTopics);
176✔
898
    bool    hasTopic = true;
176✔
899
    if (topicSz == 0) {
176!
900
      hasTopic = false;
×
901
      topicSz = 1;
×
902
    }
903

904
    if (numOfRows + topicSz > rowsCapacity) {
176!
905
      MND_TMQ_RETURN_CHECK(blockDataEnsureCapacity(pBlock, numOfRows + topicSz));
×
906
    }
907

908
    for (int32_t i = 0; i < topicSz; i++) {
366✔
909
      SColumnInfoData *pColInfo = NULL;
190✔
910
      int32_t          cols = 0;
190✔
911

912
      // consumer id
913
      char consumerIdHex[TSDB_CONSUMER_ID_LEN + VARSTR_HEADER_SIZE] = {0};
190✔
914
      (void)snprintf(varDataVal(consumerIdHex), TSDB_CONSUMER_ID_LEN, "0x%" PRIx64, pConsumer->consumerId);
190✔
915
      varDataSetLen(consumerIdHex, strlen(varDataVal(consumerIdHex)));
190✔
916

917
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
190✔
918
      MND_TMQ_NULL_CHECK(pColInfo);
190!
919
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)consumerIdHex, false));
190!
920

921
      // consumer group
922
      char cgroup[TSDB_CGROUP_LEN + VARSTR_HEADER_SIZE] = {0};
190✔
923
      STR_TO_VARSTR(cgroup, pConsumer->cgroup);
190✔
924

925
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
190✔
926
      MND_TMQ_NULL_CHECK(pColInfo);
190!
927
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)cgroup, false));
190!
928

929
      // client id
930
      char clientId[TSDB_CLIENT_ID_LEN + VARSTR_HEADER_SIZE] = {0};
190✔
931
      STR_TO_VARSTR(clientId, pConsumer->clientId);
190✔
932

933
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
190✔
934
      MND_TMQ_NULL_CHECK(pColInfo);
190!
935
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)clientId, false));
190!
936

937
      // user
938
      char user[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
190✔
939
      STR_TO_VARSTR(user, pConsumer->user);
190✔
940

941
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
190✔
942
      MND_TMQ_NULL_CHECK(pColInfo);
190!
943
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)user, false));
190!
944

945
      // fqdn
946
      char fqdn[TSDB_FQDN_LEN + VARSTR_HEADER_SIZE] = {0};
190✔
947
      STR_TO_VARSTR(fqdn, pConsumer->fqdn);
190✔
948

949
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
190✔
950
      MND_TMQ_NULL_CHECK(pColInfo);
190!
951
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)fqdn, false));
190!
952

953
      // status
954
      const char *pStatusName = mndConsumerStatusName(pConsumer->status);
190✔
955
      status = taosMemoryCalloc(1, pShow->pMeta->pSchemas[cols].bytes);
190✔
956
      MND_TMQ_NULL_CHECK(status);
190!
957
      STR_TO_VARSTR(status, pStatusName);
190✔
958

959
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
190✔
960
      MND_TMQ_NULL_CHECK(pColInfo);
190!
961
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)status, false));
190!
962
      taosMemoryFreeClear(status);
190!
963

964
      // one subscribed topic
965
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
190✔
966
      MND_TMQ_NULL_CHECK(pColInfo);
190!
967
      if (hasTopic) {
190!
968
        char        topic[TSDB_TOPIC_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
190✔
969
        mndTopicGetShowName(taosArrayGetP(pConsumer->assignedTopics, i), topic + VARSTR_HEADER_SIZE);
190✔
970
        *(VarDataLenT *)(topic) = strlen(topic + VARSTR_HEADER_SIZE);
190✔
971
        MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)topic, false));
190!
972
      } else {
973
        MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, NULL, true));
×
974
      }
975

976
      // up time
977
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
190✔
978
      MND_TMQ_NULL_CHECK(pColInfo);
190!
979
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->createTime, false));
190!
980

981
      // subscribe time
982
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
190✔
983
      MND_TMQ_NULL_CHECK(pColInfo);
190!
984
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->subscribeTime, false));
190!
985

986
      // rebalance time
987
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
190✔
988
      MND_TMQ_NULL_CHECK(pColInfo);
190!
989
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)&pConsumer->rebalanceTime, pConsumer->rebalanceTime == 0));
190!
990

991
      char         buf[TSDB_OFFSET_LEN] = {0};
190✔
992
      STqOffsetVal pVal = {.type = pConsumer->resetOffsetCfg};
190✔
993
      tFormatOffset(buf, TSDB_OFFSET_LEN, &pVal);
190✔
994

995
      parasStr = taosMemoryCalloc(1, pShow->pMeta->pSchemas[cols].bytes);
190✔
996
      MND_TMQ_NULL_CHECK(parasStr);
190!
997
      (void)snprintf(varDataVal(parasStr), pShow->pMeta->pSchemas[cols].bytes - VARSTR_HEADER_SIZE, "tbname:%d,commit:%d,interval:%dms,reset:%s", pConsumer->withTbName,
190✔
998
              pConsumer->autoCommit, pConsumer->autoCommitInterval, buf);
190✔
999
      varDataSetLen(parasStr, strlen(varDataVal(parasStr)));
190✔
1000

1001
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
190✔
1002
      MND_TMQ_NULL_CHECK(pColInfo);
190!
1003
      MND_TMQ_RETURN_CHECK(colDataSetVal(pColInfo, numOfRows, (const char *)parasStr, false));
190!
1004
      taosMemoryFreeClear(parasStr);
190!
1005
      numOfRows++;
190✔
1006
    }
1007

1008
    taosRUnLockLatch(&pConsumer->lock);
176✔
1009
    sdbRelease(pSdb, pConsumer);
176✔
1010

1011
    pBlock->info.rows = numOfRows;
179✔
1012
  }
1013

1014
  pShow->numOfRows += numOfRows;
8,006✔
1015
  return numOfRows;
8,006✔
1016

1017
END:
×
1018
  taosMemoryFreeClear(status);
×
1019
  taosMemoryFreeClear(parasStr);
×
1020
  return code;
×
1021
}
1022

1023
static void mndCancelGetNextConsumer(SMnode *pMnode, void *pIter) {
×
1024
  SSdb *pSdb = pMnode->pSdb;
×
1025
  sdbCancelFetchByType(pSdb, pIter, SDB_CONSUMER);
×
1026
}
×
1027

1028
const char *mndConsumerStatusName(int status) {
13,014✔
1029
  switch (status) {
13,014!
1030
    case MQ_CONSUMER_STATUS_READY:
5,448✔
1031
      return "ready";
5,448✔
1032
//    case MQ_CONSUMER_STATUS_LOST:
1033
//      return "lost";
1034
    case MQ_CONSUMER_STATUS_REBALANCE:
7,566✔
1035
      return "rebalancing";
7,566✔
1036
    default:
×
1037
      return "unknown";
×
1038
  }
1039
}
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

© 2025 Coveralls, Inc