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

taosdata / TDengine / #3523

06 Nov 2024 02:29AM UTC coverage: 55.861% (-2.4%) from 58.216%
#3523

push

travis-ci

web-flow
Merge pull request #28551 from taosdata/feat/TS-5215-2

test(blob): testing & fixes for blob

106075 of 245834 branches covered (43.15%)

Branch coverage included in aggregate %.

0 of 15 new or added lines in 2 files covered. (0.0%)

17003 existing lines in 254 files now uncovered.

181910 of 269703 relevant lines covered (67.45%)

1527639.59 hits per line

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

55.07
/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) {
716✔
45
  SSdbTable table = {
716✔
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);
716✔
56
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_HB, mndProcessMqHbReq);
716✔
57
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_ASK_EP, mndProcessAskEpReq);
716✔
58
  mndSetMsgHandle(pMnode, TDMT_MND_TMQ_LOST_CONSUMER_CLEAR, mndProcessConsumerClearMsg);
716✔
59

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

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

66
void mndCleanupConsumer(SMnode *pMnode) {}
715✔
67

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

73
  *(int64_t*)msg = consumerId;
32✔
74
  SRpcMsg rpcMsg = {
32✔
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);
32!
82
  MND_TMQ_RETURN_CHECK(tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg));
32!
83
  return code;
32✔
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) {
89✔
91
  SMqTopicObj *pTopic = NULL;
89✔
92
  int32_t      code = 0;
89✔
93

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

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

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

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

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

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

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

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

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

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

208
    mndReleaseSubscribe(pMnode, pSub);
154✔
209
  }
210
}
125✔
211

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

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

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

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

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

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

261
  int32_t numOfTopics = taosArrayGetSize(pConsumer->currentTopics);
96✔
262

263
  rsp->topics = taosArrayInit(numOfTopics, sizeof(SMqSubTopicEp));
96✔
264
  if (rsp->topics == NULL) {
96!
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++) {
197✔
271
    char            *topic = taosArrayGetP(pConsumer->currentTopics, i);
101✔
272
    SMqSubscribeObj *pSub = NULL;
101✔
273
    char  key[TSDB_SUBSCRIBE_KEY_LEN] = {0};
101✔
274
    (void)snprintf(key, TSDB_SUBSCRIBE_KEY_LEN, "%s%s%s", pConsumer->cgroup, TMQ_SEPARATOR, topic);
101✔
275
    int32_t code = mndAcquireSubscribeByKey(pMnode, key, &pSub);
101✔
276
    if (code != 0) {
101!
277
      continue;
×
278
    }
279
    taosRLockLatch(&pSub->lock);
101✔
280

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

284
    // 2.1 fetch topic schema
285
    SMqTopicObj *pTopic = NULL;
101✔
286
    code = mndAcquireTopic(pMnode, topic, &pTopic);
101✔
287
    if (code != TDB_CODE_SUCCESS) {
101!
288
      taosRUnLockLatch(&pSub->lock);
×
289
      mndReleaseSubscribe(pMnode, pSub);
×
290
      continue;
×
291
    }
292
    taosRLockLatch(&pTopic->lock);
101✔
293
    tstrncpy(topicEp.db, pTopic->db, TSDB_DB_FNAME_LEN);
101✔
294
    topicEp.schema.nCols = pTopic->schema.nCols;
101✔
295
    if (topicEp.schema.nCols) {
101✔
296
      topicEp.schema.pSchema = taosMemoryCalloc(topicEp.schema.nCols, sizeof(SSchema));
91✔
297
      if (topicEp.schema.pSchema == NULL) {
91!
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));
91✔
305
    }
306
    taosRUnLockLatch(&pTopic->lock);
101✔
307
    mndReleaseTopic(pMnode, pTopic);
101✔
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));
101✔
311
    if (pConsumerEp == NULL) {
101!
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);
101✔
318
    topicEp.vgs = taosArrayInit(vgNum, sizeof(SMqSubVgEp));
101✔
319
    if (topicEp.vgs == NULL) {
101!
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++) {
291✔
327
      SMqVgEp *pVgEp = taosArrayGetP(pConsumerEp->vgs, j);
190✔
328
      if (pVgEp == NULL) {
190!
329
        continue;
×
330
      }
331
      if (epoch == -1) {
190!
UNCOV
332
        SVgObj *pVgroup = mndAcquireVgroup(pMnode, pVgEp->vgId);
×
UNCOV
333
        if (pVgroup) {
×
UNCOV
334
          pVgEp->epSet = mndGetVgroupEpset(pMnode, pVgroup);
×
UNCOV
335
          mndReleaseVgroup(pMnode, pVgroup);
×
336
        }
337
      }
338
      SMqSubVgEp vgEp = {.epSet = pVgEp->epSet, .vgId = pVgEp->vgId, .offset = -1};
190✔
339
      if (taosArrayPush(topicEp.vgs, &vgEp) == NULL) {
380!
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) {
202!
348
      taosArrayDestroy(topicEp.vgs);
×
349
      taosRUnLockLatch(&pConsumer->lock);
×
350
      taosRUnLockLatch(&pSub->lock);
×
351
      mndReleaseSubscribe(pMnode, pSub);
×
352
      return terrno;
×
353
    }
354
    taosRUnLockLatch(&pSub->lock);
101✔
355
    mndReleaseSubscribe(pMnode, pSub);
101✔
356
  }
357
  taosRUnLockLatch(&pConsumer->lock);
96✔
358
  return 0;
96✔
359
}
360

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

370
  SMqRspHead *pHead = buf;
219✔
371

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

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

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

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

397
  MND_TMQ_RETURN_CHECK(tDeserializeSMqAskEpReq(pMsg->pCont, pMsg->contLen, &req));
507!
398
  int64_t consumerId = req.consumerId;
507✔
399
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
507✔
400
  if (strncmp(req.cgroup, pConsumer->cgroup, tListLen(pConsumer->cgroup)) != 0) {
476!
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);
476✔
409
  if (status != MQ_CONSUMER_STATUS_READY) {
476✔
410
    mInfo("consumer:0x%" PRIx64 " not ready, status: %s", consumerId, mndConsumerStatusName(status));
257!
411
    code = TSDB_CODE_MND_CONSUMER_NOT_READY;
257✔
412
    goto END;
257✔
413
  }
414

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

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

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

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

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

447
int32_t mndSetConsumerCommitLogs(STrans *pTrans, SMqConsumerObj *pConsumer) {
247✔
448
  int32_t  code = 0;
247✔
449
  SSdbRaw *pCommitRaw = mndConsumerActionEncode(pConsumer);
247✔
450
  MND_TMQ_NULL_CHECK(pCommitRaw);
247!
451
  code = mndTransAppendCommitlog(pTrans, pCommitRaw);
247✔
452
  if (code != 0) {
247!
453
    sdbFreeRaw(pCommitRaw);
×
454
    goto END;
×
455
  }
456
  MND_TMQ_RETURN_CHECK(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
247!
457
END:
247✔
458
  return code;
247✔
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){
37✔
478
  int32_t code = 0;
37✔
479
  pConsumerNew->rebNewTopics = taosArrayInit(0, sizeof(void *));
37✔
480
  MND_TMQ_NULL_CHECK(pConsumerNew->rebNewTopics);
37!
481
  pConsumerNew->rebRemovedTopics = taosArrayInit(0, sizeof(void *));
37✔
482
  MND_TMQ_NULL_CHECK(pConsumerNew->rebRemovedTopics);
37!
483

484
  int32_t newTopicNum = taosArrayGetSize(pConsumerNew->assignedTopics);
37✔
485
  int32_t oldTopicNum = taosArrayGetSize(pExistedConsumer->currentTopics);
37✔
486
  int32_t i = 0, j = 0;
37✔
487
  while (i < oldTopicNum || j < newTopicNum) {
104!
488
    if (i >= oldTopicNum) {
67!
489
      void* tmp = taosArrayGetP(pConsumerNew->assignedTopics, j);
×
490
      MND_TMQ_NULL_CHECK(tmp);
×
491
      ADD_TOPIC_TO_ARRAY(tmp, rebNewTopics);
×
492
      j++;
×
493
      continue;
×
494
    } else if (j >= newTopicNum) {
67!
495
      void* tmp = taosArrayGetP(pExistedConsumer->currentTopics, i);
67✔
496
      MND_TMQ_NULL_CHECK(tmp);
67!
497
      ADD_TOPIC_TO_ARRAY(tmp, rebRemovedTopics);
134!
498
      i++;
67✔
499
      continue;
67✔
500
    } else {
501
      char *oldTopic = taosArrayGetP(pExistedConsumer->currentTopics, i);
×
502
      MND_TMQ_NULL_CHECK(oldTopic);
×
503
      char *newTopic = taosArrayGetP(pConsumerNew->assignedTopics, j);
×
504
      MND_TMQ_NULL_CHECK(newTopic);
×
505
      int   comp = strcmp(oldTopic, newTopic);
×
506
      if (comp == 0) {
×
507
        i++;
×
508
        j++;
×
509
        continue;
×
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) {
37!
523
    code = TSDB_CODE_TMQ_NO_NEED_REBALANCE;
×
524
  }
525

526
END:
37✔
527
  return code;
37✔
528
}
529

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

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

544
static int32_t buildSubConsumer(SMnode *pMnode, SCMSubscribeReq *subscribe, SMqConsumerObj** ppConsumer){
89✔
545
  int64_t         consumerId = subscribe->consumerId;
89✔
546
  char           *cgroup     = subscribe->cgroup;
89✔
547
  SMqConsumerObj *pConsumerNew     = NULL;
89✔
548
  SMqConsumerObj *pExistedConsumer = NULL;
89✔
549
  int32_t code = mndAcquireConsumer(pMnode, consumerId, &pExistedConsumer);
89✔
550
  if (code != 0) {
89✔
551
    mInfo("receive subscribe request from new consumer:0x%" PRIx64
52!
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));
52!
556
  } else {
557
    int32_t status = atomic_load_32(&pExistedConsumer->status);
37✔
558

559
    mInfo("receive subscribe request from existed consumer:0x%" PRIx64
37!
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) {
37!
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));
37!
569
    MND_TMQ_RETURN_CHECK(getTopicAddDelete(pExistedConsumer, pConsumerNew));
37!
570
  }
571
  mndReleaseConsumer(pMnode, pExistedConsumer);
89✔
572
  if (ppConsumer){
89!
573
    *ppConsumer = pConsumerNew;
89✔
574
  }
575
  return code;
89✔
576

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

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

590
  SCMSubscribeReq subscribe = {0};
117✔
591
  MND_TMQ_RETURN_CHECK(tDeserializeSCMSubscribeReq(msgStr, &subscribe, pMsg->contLen));
234!
592
  bool unSubscribe = (taosArrayGetSize(subscribe.topicNames) == 0);
117✔
593
  if(unSubscribe){
117✔
594
    SMqConsumerObj *pConsumerTmp = NULL;
65✔
595
    MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, subscribe.consumerId, &pConsumerTmp));
93!
596
    if (taosArrayGetSize(pConsumerTmp->assignedTopics) == 0){
65✔
597
      mndReleaseConsumer(pMnode, pConsumerTmp);
28✔
598
      goto END;
28✔
599
    }
600
    mndReleaseConsumer(pMnode, pConsumerTmp);
37✔
601
  }
602
  MND_TMQ_RETURN_CHECK(checkAndSortTopic(pMnode, subscribe.topicNames));
89!
603
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY,
89✔
604
                          (unSubscribe ? TRN_CONFLICT_NOTHING :TRN_CONFLICT_DB_INSIDE),
605
                          pMsg, "subscribe");
606
  MND_TMQ_NULL_CHECK(pTrans);
89!
607

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

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

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

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

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

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

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

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

647
  terrno = TSDB_CODE_SUCCESS;
302✔
648

649
CM_ENCODE_OVER:
302✔
650
  taosMemoryFreeClear(buf);
302!
651
  if (terrno != 0) {
302!
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);
302!
658
  return pRaw;
302✔
659
}
660

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

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

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

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

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

689
  int32_t dataPos = 0;
283✔
690
  int32_t len;
691
  SDB_GET_INT32(pRaw, dataPos, &len, CM_DECODE_OVER);
283!
692
  buf = taosMemoryMalloc(len);
283✔
693
  if (buf == NULL) {
283!
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);
283!
699
  SDB_GET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_DECODE_OVER);
283!
700

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

706
  tmsgUpdateDnodeEpSet(&pConsumer->ep);
283✔
707

708
CM_DECODE_OVER:
283✔
709
  taosMemoryFreeClear(buf);
283!
710
  if (terrno != TSDB_CODE_SUCCESS) {
283!
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;
283✔
717
}
718

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

726
static int32_t mndConsumerActionDelete(SSdb *pSdb, SMqConsumerObj *pConsumer) {
283✔
727
  mInfo("consumer:0x%" PRIx64 " perform delete action, status:(%d)%s", pConsumer->consumerId, pConsumer->status,
283!
728
        mndConsumerStatusName(pConsumer->status));
729
  tClearSMqConsumerObj(pConsumer);
283✔
730
  return 0;
283✔
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) {
217✔
747
  int32_t size = taosArrayGetSize(topicList);
217✔
748
  for (int32_t i = 0; i < size; i++) {
289!
749
    char *p = taosArrayGetP(topicList, i);
289✔
750
    if (strcmp(pTopic, p) == 0) {
289✔
751
      taosArrayRemove(topicList, i);
217✔
752
      taosMemoryFree(p);
217✔
753

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

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

772
  return existing;
83✔
773
}
774

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

779
  taosWLockLatch(&pOldConsumer->lock);
196✔
780

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

786
    pOldConsumer->subscribeTime = taosGetTimestampMs();
37✔
787
    pOldConsumer->status = MQ_CONSUMER_STATUS_REBALANCE;
37✔
788
    mInfo("consumer:0x%" PRIx64 " subscribe update, modify existed consumer", pOldConsumer->consumerId);
37!
789
  } else if (pNewConsumer->updateType == CONSUMER_UPDATE_REB) {
159✔
790
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
9✔
791
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
9✔
792
    mInfo("[rebalance] consumer:0x%" PRIx64 " rebalance update, only rebalance time", pOldConsumer->consumerId);
9!
793
  } else if (pNewConsumer->updateType == CONSUMER_ADD_REB) {
150✔
794
    void *tmp = taosArrayGetP(pNewConsumer->rebNewTopics, 0);
83✔
795
    if (tmp == NULL){
83!
796
      return TSDB_CODE_TMQ_INVALID_MSG;
×
797
    }
798
    char *pNewTopic = taosStrdup(tmp);
83✔
799
    if (pNewTopic == NULL) {
83!
800
      return terrno;
×
801
    }
802
    removeFromTopicList(pOldConsumer->rebNewTopics, pNewTopic, pOldConsumer->consumerId, "new");
83✔
803
    bool existing = existInCurrentTopicList(pOldConsumer, pNewTopic);
83✔
804
    if (existing) {
83!
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) {
166!
809
        taosMemoryFree(pNewTopic);
×
810
        return TSDB_CODE_TMQ_INVALID_MSG;
×
811
      }
812
      taosArraySort(pOldConsumer->currentTopics, taosArrayCompareString);
83✔
813
    }
814

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

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

824
    mInfo("[rebalance] consumer:0x%" PRIx64 " rebalance update add, state (%d)%s -> (%d)%s, new epoch:%d, reb-time:%" PRId64
83!
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) {
67!
832
    char *topic = taosArrayGetP(pNewConsumer->rebRemovedTopics, 0);
67✔
833
    if (topic == NULL){
67!
834
      return TSDB_CODE_TMQ_INVALID_MSG;
×
835
    }
836
    removeFromTopicList(pOldConsumer->rebRemovedTopics, topic, pOldConsumer->consumerId, "remove");
67✔
837
    removeFromTopicList(pOldConsumer->currentTopics, topic, pOldConsumer->consumerId, "current");
67✔
838

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

847
    mInfo("[rebalance]consumer:0x%" PRIx64 " rebalance update remove, state (%d)%s -> (%d)%s, new epoch:%d, reb-time:%" PRId64
67!
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);
196✔
856
  return 0;
196✔
857
}
858

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1014
  pShow->numOfRows += numOfRows;
15✔
1015
  return numOfRows;
15✔
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) {
1,132✔
1029
  switch (status) {
1,132!
1030
    case MQ_CONSUMER_STATUS_READY:
286✔
1031
      return "ready";
286✔
1032
//    case MQ_CONSUMER_STATUS_LOST:
1033
//      return "lost";
1034
    case MQ_CONSUMER_STATUS_REBALANCE:
846✔
1035
      return "rebalancing";
846✔
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

© 2026 Coveralls, Inc