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

taosdata / TDengine / #3572

02 Jan 2025 08:57AM UTC coverage: 63.077% (-0.2%) from 63.276%
#3572

push

travis-ci

web-flow
Merge pull request #29450 from taosdata/fix/TS-5651-skip-sync-heartbeat

fix:[TS-5651]skip-sync-heartbeat

139525 of 284348 branches covered (49.07%)

Branch coverage included in aggregate %.

217427 of 281548 relevant lines covered (77.23%)

18571459.85 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

400
  SMqRspHead *pHead = buf;
9,121✔
401

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

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

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

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

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

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

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

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

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

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

483
int32_t mndSetConsumerCommitLogs(STrans *pTrans, SMqConsumerObj *pConsumer) {
2,060✔
484
  if (pConsumer == NULL || pTrans == NULL) {
2,060!
485
    return TSDB_CODE_INVALID_PARA;
×
486
  }
487
  int32_t  code = 0;
2,060✔
488
  SSdbRaw *pCommitRaw = mndConsumerActionEncode(pConsumer);
2,060✔
489
  MND_TMQ_NULL_CHECK(pCommitRaw);
2,060!
490
  code = mndTransAppendCommitlog(pTrans, pCommitRaw);
2,060✔
491
  if (code != 0) {
2,060!
492
    sdbFreeRaw(pCommitRaw);
×
493
    goto END;
×
494
  }
495
  MND_TMQ_RETURN_CHECK(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
2,060!
496
END:
2,060✔
497
  return code;
2,060✔
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){
424✔
520
  if (pExistedConsumer == NULL || pConsumerNew == NULL) {
424!
521
    return TSDB_CODE_INVALID_PARA;
×
522
  }
523
  int32_t code = 0;
424✔
524
  pConsumerNew->rebNewTopics = taosArrayInit(0, sizeof(void *));
424✔
525
  MND_TMQ_NULL_CHECK(pConsumerNew->rebNewTopics);
424!
526
  pConsumerNew->rebRemovedTopics = taosArrayInit(0, sizeof(void *));
424✔
527
  MND_TMQ_NULL_CHECK(pConsumerNew->rebRemovedTopics);
424!
528

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

571
END:
413✔
572
  return code;
424✔
573
}
574

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

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

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

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

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

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

644
  SCMSubscribeReq subscribe = {0};
1,307✔
645
  MND_TMQ_RETURN_CHECK(tDeserializeSCMSubscribeReq(msgStr, &subscribe, pMsg->contLen));
2,614!
646
  bool unSubscribe = (taosArrayGetSize(subscribe.topicNames) == 0);
1,307✔
647
  if(unSubscribe){
1,307✔
648
    SMqConsumerObj *pConsumerTmp = NULL;
770✔
649
    MND_TMQ_RETURN_CHECK(mndAcquireConsumer(pMnode, subscribe.consumerId, &pConsumerTmp));
1,113✔
650
    if (taosArrayGetSize(pConsumerTmp->assignedTopics) == 0){
756✔
651
      mndReleaseConsumer(pMnode, pConsumerTmp);
343✔
652
      goto END;
343✔
653
    }
654
    mndReleaseConsumer(pMnode, pConsumerTmp);
413✔
655
  }
656
  MND_TMQ_RETURN_CHECK(checkAndSortTopic(pMnode, subscribe.topicNames));
950✔
657
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY,
949✔
658
                          (unSubscribe ? TRN_CONFLICT_NOTHING :TRN_CONFLICT_DB_INSIDE),
659
                          pMsg, "subscribe");
660
  MND_TMQ_NULL_CHECK(pTrans);
949!
661

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

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

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

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

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

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

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

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

704
  terrno = TSDB_CODE_SUCCESS;
2,811✔
705

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

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

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

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

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

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

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

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

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

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

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

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

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

830
static bool existInCurrentTopicList(const SMqConsumerObj *pConsumer, const char *pTopic) {
589✔
831
  if (pConsumer == NULL || pTopic == NULL) {
589!
832
    return false;
×
833
  }
834
  bool    existing = false;
589✔
835
  int32_t size = taosArrayGetSize(pConsumer->currentTopics);
589✔
836
  for (int32_t i = 0; i < size; i++) {
667✔
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;
589✔
845
}
846

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

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

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

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

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

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

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

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

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

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

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

972
    if (taosArrayGetSize(pConsumer->assignedTopics) == 0) {
182✔
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);
178✔
979
    mInfo("showing consumer:0x%" PRIx64, pConsumer->consumerId);
178!
980

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1098
  pShow->numOfRows += numOfRows;
8,006✔
1099
  return numOfRows;
8,006✔
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) {
13,094✔
1114
  switch (status) {
13,094!
1115
    case MQ_CONSUMER_STATUS_READY:
5,351✔
1116
      return "ready";
5,351✔
1117
//    case MQ_CONSUMER_STATUS_LOST:
1118
//      return "lost";
1119
    case MQ_CONSUMER_STATUS_REBALANCE:
7,743✔
1120
      return "rebalancing";
7,743✔
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