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

taosdata / TDengine / #3578

11 Jan 2025 11:19AM UTC coverage: 63.183% (-0.03%) from 63.211%
#3578

push

travis-ci

web-flow
Merge pull request #29546 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

139873 of 284461 branches covered (49.17%)

Branch coverage included in aggregate %.

20 of 26 new or added lines in 2 files covered. (76.92%)

717 existing lines in 102 files now uncovered.

217827 of 281671 relevant lines covered (77.33%)

19620733.66 hits per line

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

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

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

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

69
void mndCleanupConsumer(SMnode *pMnode) {}
1,729✔
70

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

288
  int32_t numOfTopics = taosArrayGetSize(pConsumer->currentTopics);
3,533✔
289

290
  rsp->topics = taosArrayInit(numOfTopics, sizeof(SMqSubTopicEp));
3,533✔
291
  if (rsp->topics == NULL) {
3,533!
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++) {
6,744✔
298
    char            *topic = taosArrayGetP(pConsumer->currentTopics, i);
3,211✔
299
    SMqSubscribeObj *pSub = NULL;
3,211✔
300
    char  key[TSDB_SUBSCRIBE_KEY_LEN] = {0};
3,211✔
301
    (void)snprintf(key, TSDB_SUBSCRIBE_KEY_LEN, "%s%s%s", pConsumer->cgroup, TMQ_SEPARATOR, topic);
3,211✔
302
    int32_t code = mndAcquireSubscribeByKey(pMnode, key, &pSub);
3,211✔
303
    if (code != 0) {
3,211!
304
      continue;
×
305
    }
306
    taosRLockLatch(&pSub->lock);
3,211✔
307

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

311
    // 2.1 fetch topic schema
312
    SMqTopicObj *pTopic = NULL;
3,211✔
313
    code = mndAcquireTopic(pMnode, topic, &pTopic);
3,211✔
314
    if (code != TDB_CODE_SUCCESS) {
3,211!
315
      taosRUnLockLatch(&pSub->lock);
×
316
      mndReleaseSubscribe(pMnode, pSub);
×
317
      continue;
×
318
    }
319
    taosRLockLatch(&pTopic->lock);
3,211✔
320
    tstrncpy(topicEp.db, pTopic->db, TSDB_DB_FNAME_LEN);
3,211✔
321
    topicEp.schema.nCols = pTopic->schema.nCols;
3,211✔
322
    if (topicEp.schema.nCols) {
3,211✔
323
      topicEp.schema.pSchema = taosMemoryCalloc(topicEp.schema.nCols, sizeof(SSchema));
2,060!
324
      if (topicEp.schema.pSchema == NULL) {
2,060!
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));
2,060✔
332
    }
333
    taosRUnLockLatch(&pTopic->lock);
3,211✔
334
    mndReleaseTopic(pMnode, pTopic);
3,211✔
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));
3,211✔
338
    if (pConsumerEp == NULL) {
3,211!
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);
3,211✔
345
    topicEp.vgs = taosArrayInit(vgNum, sizeof(SMqSubVgEp));
3,211✔
346
    if (topicEp.vgs == NULL) {
3,211!
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++) {
7,275✔
354
      SMqVgEp *pVgEp = taosArrayGetP(pConsumerEp->vgs, j);
4,064✔
355
      if (pVgEp == NULL) {
4,064!
356
        continue;
×
357
      }
358
      if (epoch == -1) {
4,064✔
359
        SVgObj *pVgroup = mndAcquireVgroup(pMnode, pVgEp->vgId);
2,638✔
360
        if (pVgroup) {
2,638✔
361
          pVgEp->epSet = mndGetVgroupEpset(pMnode, pVgroup);
150✔
362
          mndReleaseVgroup(pMnode, pVgroup);
150✔
363
        }
364
      }
365
      SMqSubVgEp vgEp = {.epSet = pVgEp->epSet, .vgId = pVgEp->vgId, .offset = -1};
4,064✔
366
      if (taosArrayPush(topicEp.vgs, &vgEp) == NULL) {
8,128!
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) {
6,422!
375
      taosArrayDestroy(topicEp.vgs);
×
376
      taosRUnLockLatch(&pConsumer->lock);
×
377
      taosRUnLockLatch(&pSub->lock);
×
378
      mndReleaseSubscribe(pMnode, pSub);
×
379
      return terrno;
×
380
    }
381
    taosRUnLockLatch(&pSub->lock);
3,211✔
382
    mndReleaseSubscribe(pMnode, pSub);
3,211✔
383
  }
384
  taosRUnLockLatch(&pConsumer->lock);
3,533✔
385
  return 0;
3,533✔
386
}
387

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

400
  SMqRspHead *pHead = buf;
7,951✔
401

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

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

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

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

430
  MND_TMQ_RETURN_CHECK(tDeserializeSMqAskEpReq(pMsg->pCont, pMsg->contLen, &req));
10,394!
431
  int64_t consumerId = req.consumerId;
10,394✔
432
  MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, consumerId, &pConsumer));
10,394✔
433
  if (strncmp(req.cgroup, pConsumer->cgroup, tListLen(pConsumer->cgroup)) != 0) {
10,371!
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);
10,371✔
442
  if (status != MQ_CONSUMER_STATUS_READY) {
10,371✔
443
    mInfo("consumer:0x%" PRIx64 " not ready, status: %s", consumerId, mndConsumerStatusName(status));
2,419!
444
    code = TSDB_CODE_MND_CONSUMER_NOT_READY;
2,419✔
445
    goto END;
2,419✔
446
  }
447

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

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

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

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

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

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

500
static void freeItem(void *param) {
1✔
501
  if (param == NULL) {
1!
502
    return;
×
503
  }
504
  void *pItem = *(void **)param;
1✔
505
  if (pItem != NULL) {
1!
506
    taosMemoryFree(pItem);
1!
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){
431✔
520
  if (pExistedConsumer == NULL || pConsumerNew == NULL) {
431!
521
    return TSDB_CODE_INVALID_PARA;
×
522
  }
523
  int32_t code = 0;
431✔
524
  pConsumerNew->rebNewTopics = taosArrayInit(0, sizeof(void *));
431✔
525
  MND_TMQ_NULL_CHECK(pConsumerNew->rebNewTopics);
431!
526
  pConsumerNew->rebRemovedTopics = taosArrayInit(0, sizeof(void *));
431✔
527
  MND_TMQ_NULL_CHECK(pConsumerNew->rebRemovedTopics);
431!
528

529
  int32_t newTopicNum = taosArrayGetSize(pConsumerNew->assignedTopics);
431✔
530
  int32_t oldTopicNum = taosArrayGetSize(pExistedConsumer->currentTopics);
431✔
531
  int32_t i = 0, j = 0;
431✔
532
  while (i < oldTopicNum || j < newTopicNum) {
920✔
533
    if (i >= oldTopicNum) {
489✔
534
      void* tmp = taosArrayGetP(pConsumerNew->assignedTopics, j);
5✔
535
      MND_TMQ_NULL_CHECK(tmp);
5!
536
      ADD_TOPIC_TO_ARRAY(tmp, rebNewTopics);
10!
537
      j++;
5✔
538
      continue;
5✔
539
    } else if (j >= newTopicNum) {
484✔
540
      void* tmp = taosArrayGetP(pExistedConsumer->currentTopics, i);
470✔
541
      MND_TMQ_NULL_CHECK(tmp);
470!
542
      ADD_TOPIC_TO_ARRAY(tmp, rebRemovedTopics);
940!
543
      i++;
470✔
544
      continue;
470✔
545
    } else {
546
      char *oldTopic = taosArrayGetP(pExistedConsumer->currentTopics, i);
14✔
547
      MND_TMQ_NULL_CHECK(oldTopic);
14!
548
      char *newTopic = taosArrayGetP(pConsumerNew->assignedTopics, j);
14✔
549
      MND_TMQ_NULL_CHECK(newTopic);
14!
550
      int   comp = strcmp(oldTopic, newTopic);
14✔
551
      if (comp == 0) {
14!
552
        i++;
14✔
553
        j++;
14✔
554
        continue;
14✔
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) {
431✔
568
    code = TSDB_CODE_TMQ_NO_NEED_REBALANCE;
14✔
569
  }
570

571
END:
417✔
572
  return code;
431✔
573
}
574

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

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

592
static int32_t buildSubConsumer(SMnode *pMnode, SCMSubscribeReq *subscribe, SMqConsumerObj** ppConsumer){
849✔
593
  if (pMnode == NULL || subscribe == NULL) {
849!
594
    return TSDB_CODE_INVALID_PARA;
×
595
  }
596
  int64_t         consumerId = subscribe->consumerId;
849✔
597
  char           *cgroup     = subscribe->cgroup;
849✔
598
  SMqConsumerObj *pConsumerNew     = NULL;
849✔
599
  SMqConsumerObj *pExistedConsumer = NULL;
849✔
600
  int32_t code = mndAcquireConsumer(pMnode, consumerId, &pExistedConsumer);
849✔
601
  if (code != 0) {
849✔
602
    mInfo("receive subscribe request from new consumer:0x%" PRIx64
418!
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));
418!
607
  } else {
608
    int32_t status = atomic_load_32(&pExistedConsumer->status);
431✔
609

610
    mInfo("receive subscribe request from existed consumer:0x%" PRIx64
431!
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) {
431!
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));
431!
620
    MND_TMQ_RETURN_CHECK(getTopicAddDelete(pExistedConsumer, pConsumerNew));
431✔
621
  }
622
  mndReleaseConsumer(pMnode, pExistedConsumer);
835✔
623
  if (ppConsumer){
835!
624
    *ppConsumer = pConsumerNew;
835✔
625
  }
626
  return code;
835✔
627

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

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

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

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

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

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

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

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

690
  buf = taosMemoryMalloc(tlen);
2,317!
691
  if (buf == NULL) goto CM_ENCODE_OVER;
2,317!
692

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

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

704
  terrno = TSDB_CODE_SUCCESS;
2,317✔
705

706
CM_ENCODE_OVER:
2,317✔
707
  taosMemoryFreeClear(buf);
2,317!
708
  if (terrno != 0) {
2,317!
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);
2,317✔
715
  return pRaw;
2,317✔
716
}
717

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

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

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

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

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

749
  int32_t dataPos = 0;
2,292✔
750
  int32_t len;
751
  SDB_GET_INT32(pRaw, dataPos, &len, CM_DECODE_OVER);
2,292!
752
  buf = taosMemoryMalloc(len);
2,292!
753
  if (buf == NULL) {
2,292!
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);
2,292!
759
  SDB_GET_RESERVE(pRaw, dataPos, MND_CONSUMER_RESERVE_SIZE, CM_DECODE_OVER);
2,292!
760

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

766
  tmsgUpdateDnodeEpSet(&pConsumer->ep);
2,292✔
767

768
CM_DECODE_OVER:
2,292✔
769
  taosMemoryFreeClear(buf);
2,292!
770
  if (terrno != TSDB_CODE_SUCCESS) {
2,292!
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;
2,292✔
777
}
778

779
static int32_t mndConsumerActionInsert(SSdb *pSdb, SMqConsumerObj *pConsumer) {
446✔
780
  if (pConsumer == NULL) {
446!
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,
446!
784
        pConsumer->status, mndConsumerStatusName(pConsumer->status), pConsumer->epoch);
785
  pConsumer->subscribeTime = pConsumer->createTime;
446✔
786
  return 0;
446✔
787
}
788

789
static int32_t mndConsumerActionDelete(SSdb *pSdb, SMqConsumerObj *pConsumer) {
2,292✔
790
  if (pConsumer == NULL) {
2,292!
791
    return TSDB_CODE_INVALID_PARA;
×
792
  }
793
  mInfo("consumer:0x%" PRIx64 " perform delete action, status:(%d)%s", pConsumer->consumerId, pConsumer->status,
2,292!
794
        mndConsumerStatusName(pConsumer->status));
795
  tClearSMqConsumerObj(pConsumer);
2,292✔
796
  return 0;
2,292✔
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) {
1,462✔
813
  if (topicList == NULL || pTopic == NULL) {
1,462!
814
    return;
×
815
  }
816
  int32_t size = taosArrayGetSize(topicList);
1,462✔
817
  for (int32_t i = 0; i < size; i++) {
1,590✔
818
    char *p = taosArrayGetP(topicList, i);
1,579✔
819
    if (strcmp(pTopic, p) == 0) {
1,579✔
820
      taosArrayRemove(topicList, i);
1,451✔
821
      taosMemoryFree(p);
1,451!
822

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

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

844
  return existing;
490✔
845
}
846

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

854
  taosWLockLatch(&pOldConsumer->lock);
1,464✔
855

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

861
    pOldConsumer->subscribeTime = taosGetTimestampMs();
422✔
862
    pOldConsumer->status = MQ_CONSUMER_STATUS_REBALANCE;
422✔
863
    mInfo("consumer:0x%" PRIx64 " subscribe update, modify existed consumer", pOldConsumer->consumerId);
422!
864
  } else if (pNewConsumer->updateType == CONSUMER_UPDATE_REB) {
1,042✔
865
    (void)atomic_add_fetch_32(&pOldConsumer->epoch, 1);
66✔
866
    pOldConsumer->rebalanceTime = taosGetTimestampMs();
66✔
867
    mInfo("[rebalance] consumer:0x%" PRIx64 " rebalance update, only rebalance time", pOldConsumer->consumerId);
66!
868
  } else if (pNewConsumer->updateType == CONSUMER_ADD_REB) {
976✔
869
    void *tmp = taosArrayGetP(pNewConsumer->rebNewTopics, 0);
490✔
870
    if (tmp == NULL){
490!
871
      return TSDB_CODE_TMQ_INVALID_MSG;
×
872
    }
873
    char *pNewTopic = taosStrdup(tmp);
490!
874
    if (pNewTopic == NULL) {
490!
875
      return terrno;
×
876
    }
877
    removeFromTopicList(pOldConsumer->rebNewTopics, pNewTopic, pOldConsumer->consumerId, "new");
490✔
878
    bool existing = existInCurrentTopicList(pOldConsumer, pNewTopic);
490✔
879
    if (existing) {
490!
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) {
980!
884
        taosMemoryFree(pNewTopic);
×
885
        return TSDB_CODE_TMQ_INVALID_MSG;
×
886
      }
887
      taosArraySort(pOldConsumer->currentTopics, taosArrayCompareString);
490✔
888
    }
889

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

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

899
    mInfo("[rebalance] consumer:0x%" PRIx64 " rebalance update add, state (%d)%s -> (%d)%s, new epoch:%d, reb-time:%" PRId64
490!
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) {
486!
907
    char *topic = taosArrayGetP(pNewConsumer->rebRemovedTopics, 0);
486✔
908
    if (topic == NULL){
486!
909
      return TSDB_CODE_TMQ_INVALID_MSG;
×
910
    }
911
    removeFromTopicList(pOldConsumer->rebRemovedTopics, topic, pOldConsumer->consumerId, "remove");
486✔
912
    removeFromTopicList(pOldConsumer->currentTopics, topic, pOldConsumer->consumerId, "current");
486✔
913

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

922
    mInfo("[rebalance]consumer:0x%" PRIx64 " rebalance update remove, state (%d)%s -> (%d)%s, new epoch:%d, reb-time:%" PRId64
486!
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);
1,464✔
931
  return 0;
1,464✔
932
}
933

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1098
  pShow->numOfRows += numOfRows;
8,002✔
1099
  return numOfRows;
8,002✔
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) {
11,306✔
1114
  switch (status) {
11,306!
1115
    case MQ_CONSUMER_STATUS_READY:
4,651✔
1116
      return "ready";
4,651✔
1117
//    case MQ_CONSUMER_STATUS_LOST:
1118
//      return "lost";
1119
    case MQ_CONSUMER_STATUS_REBALANCE:
6,655✔
1120
      return "rebalancing";
6,655✔
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