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

taosdata / TDengine / #4800

16 Oct 2025 09:19AM UTC coverage: 53.935% (-7.1%) from 61.083%
#4800

push

travis-ci

web-flow
Merge b32e3a393 into a190048d5

134724 of 323629 branches covered (41.63%)

Branch coverage included in aggregate %.

184803 of 268802 relevant lines covered (68.75%)

69058627.2 hits per line

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

71.35
/source/dnode/mnode/impl/src/mndDef.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
#define _DEFAULT_SOURCE
16
#include "mndConsumer.h"
17
#include "mndDef.h"
18
#include "taoserror.h"
19
#include "tunit.h"
20

21
int32_t tEncodeSStreamObj(SEncoder *pEncoder, const SStreamObj *pObj) {
230,136✔
22
  TAOS_CHECK_RETURN(tStartEncode(pEncoder));
230,136!
23

24
  TAOS_CHECK_RETURN(tEncodeCStr(pEncoder, pObj->name));
460,272!
25
  TAOS_CHECK_RETURN(tSerializeSCMCreateStreamReqImpl(pEncoder, pObj->pCreate));
230,136!
26

27
  TAOS_CHECK_RETURN(tEncodeI32(pEncoder, pObj->mainSnodeId));
460,272!
28
  TAOS_CHECK_RETURN(tEncodeI8(pEncoder, pObj->userStopped));
460,272!
29
  TAOS_CHECK_RETURN(tEncodeI64(pEncoder, pObj->createTime));
460,272!
30
  TAOS_CHECK_RETURN(tEncodeI64(pEncoder, pObj->updateTime));
460,272!
31

32
  tEndEncode(pEncoder);
230,136✔
33
  return pEncoder->pos;
230,136✔
34
}
35

36
int32_t tDecodeSStreamObj(SDecoder *pDecoder, SStreamObj *pObj, int32_t sver) {
63,301✔
37
  int32_t code = 0;
63,301✔
38
  int32_t lino = 0;
63,301✔
39
  TAOS_CHECK_RETURN(tStartDecode(pDecoder));
63,301!
40

41
  TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, pObj->name));
63,301!
42
  pObj->pCreate = taosMemoryCalloc(1, sizeof(*pObj->pCreate));
63,301!
43
  if (NULL == pObj) {
63,301!
44
    TAOS_CHECK_EXIT(terrno);
×
45
  }
46
  
47
  TAOS_CHECK_RETURN(tDeserializeSCMCreateStreamReqImpl(pDecoder, pObj->pCreate));
63,301!
48

49
  TAOS_CHECK_RETURN(tDecodeI32(pDecoder, &pObj->mainSnodeId));
126,602!
50
  TAOS_CHECK_RETURN(tDecodeI8(pDecoder, &pObj->userStopped));
126,602!
51
  TAOS_CHECK_RETURN(tDecodeI64(pDecoder, &pObj->createTime));
126,602!
52
  TAOS_CHECK_RETURN(tDecodeI64(pDecoder, &pObj->updateTime));
126,602!
53

54
_exit:
63,301✔
55

56
  tEndDecode(pDecoder);
63,301✔
57
  tDecoderClear(pDecoder);  
63,301✔
58
  
59
  TAOS_RETURN(code);
63,301✔
60
}
61

62
void tFreeStreamObj(SStreamObj *pStream) {
129,955✔
63
  tFreeSCMCreateStreamReq(pStream->pCreate);
129,955✔
64
  taosMemoryFreeClear(pStream->pCreate);
129,955!
65
}
129,955✔
66

67
void freeSMqConsumerEp(void* data) {
10,449✔
68
  if (data == NULL) return;
10,449!
69
  SMqConsumerEp *pConsumerEp = (SMqConsumerEp *)data;
10,449✔
70
  taosArrayDestroy(pConsumerEp->vgs);
10,449✔
71
  pConsumerEp->vgs = NULL;
10,449✔
72
  taosArrayDestroy(pConsumerEp->offsetRows);
10,449✔
73
  pConsumerEp->offsetRows = NULL;
10,449✔
74
}
75

76
int32_t tEncodeSMqVgEp(void **buf, const SMqVgEp *pVgEp) {
101,476✔
77
  int32_t tlen = 0;
101,476✔
78
  tlen += taosEncodeFixedI32(buf, pVgEp->vgId);
101,476✔
79
  tlen += taosEncodeSEpSet(buf, &pVgEp->epSet);
101,476✔
80
  return tlen;
101,476✔
81
}
82

83
void *tDecodeSMqVgEp(const void *buf, SMqVgEp *pVgEp, int8_t sver) {
50,154✔
84
  buf = taosDecodeFixedI32(buf, &pVgEp->vgId);
50,154!
85
  if (sver == 1) {
50,154!
86
    uint64_t size = 0;
×
87
    buf = taosDecodeVariantU64(buf, &size);
×
88
    buf = POINTER_SHIFT(buf, size);
×
89
  }
90
  buf = taosDecodeSEpSet(buf, &pVgEp->epSet);
50,154✔
91
  return (void *)buf;
50,154✔
92
}
93

94
static void *topicNameDup(void *p) { return taosStrdup((char *)p); }
3,483!
95

96
int32_t tNewSMqConsumerObj(int64_t consumerId, char *cgroup, int8_t updateType, char *topic, SCMSubscribeReq *subscribe,
15,809✔
97
                           SMqConsumerObj **ppConsumer) {
98
  int32_t         code = 0;
15,809✔
99
  SMqConsumerObj *pConsumer = taosMemoryCalloc(1, sizeof(SMqConsumerObj));
15,809!
100
  if (pConsumer == NULL) {
15,809!
101
    code = terrno;
×
102
    goto END;
×
103
  }
104

105
  pConsumer->consumerId = consumerId;
15,809✔
106
  (void)memcpy(pConsumer->cgroup, cgroup, TSDB_CGROUP_LEN);
15,809!
107

108
  pConsumer->epoch = 0;
15,809✔
109
  pConsumer->status = MQ_CONSUMER_STATUS_REBALANCE;
15,809✔
110
  pConsumer->hbStatus = 0;
15,809✔
111
  pConsumer->pollStatus = 0;
15,809✔
112

113
  taosInitRWLatch(&pConsumer->lock);
15,809✔
114
  pConsumer->createTime = taosGetTimestampMs();
15,809✔
115
  pConsumer->updateType = updateType;
15,809✔
116

117
  if (updateType == CONSUMER_ADD_REB) {
15,809✔
118
    pConsumer->rebNewTopics = taosArrayInit(0, sizeof(void *));
3,483✔
119
    if (pConsumer->rebNewTopics == NULL) {
3,483!
120
      code = terrno;
×
121
      goto END;
×
122
    }
123

124
    char *topicTmp = taosStrdup(topic);
3,483!
125
    if (taosArrayPush(pConsumer->rebNewTopics, &topicTmp) == NULL) {
6,966!
126
      code = terrno;
×
127
      goto END;
×
128
    }
129
  } else if (updateType == CONSUMER_REMOVE_REB) {
12,326✔
130
    pConsumer->rebRemovedTopics = taosArrayInit(0, sizeof(void *));
3,281✔
131
    if (pConsumer->rebRemovedTopics == NULL) {
3,281!
132
      code = terrno;
×
133
      goto END;
×
134
    }
135
    char *topicTmp = taosStrdup(topic);
3,281!
136
    if (taosArrayPush(pConsumer->rebRemovedTopics, &topicTmp) == NULL) {
6,562!
137
      code = terrno;
×
138
      goto END;
×
139
    }
140
  } else if (updateType == CONSUMER_INSERT_SUB) {
9,045✔
141
    tstrncpy(pConsumer->clientId, subscribe->clientId, tListLen(pConsumer->clientId));
3,483!
142
    pConsumer->withTbName = subscribe->withTbName;
3,483✔
143
    pConsumer->autoCommit = subscribe->autoCommit;
3,483✔
144
    pConsumer->autoCommitInterval = subscribe->autoCommitInterval;
3,483✔
145
    pConsumer->resetOffsetCfg = subscribe->resetOffsetCfg;
3,483✔
146
    pConsumer->maxPollIntervalMs = subscribe->maxPollIntervalMs;
3,483✔
147
    pConsumer->sessionTimeoutMs = subscribe->sessionTimeoutMs;
3,483✔
148
    tstrncpy(pConsumer->user, subscribe->user, TSDB_USER_LEN);
3,483!
149
    tstrncpy(pConsumer->fqdn, subscribe->fqdn, TSDB_FQDN_LEN);
3,483!
150

151
    pConsumer->rebNewTopics = taosArrayDup(subscribe->topicNames, topicNameDup);
3,483✔
152
    if (pConsumer->rebNewTopics == NULL) {
3,483!
153
      code = terrno;
×
154
      goto END;
×
155
    }
156
    pConsumer->assignedTopics = subscribe->topicNames;
3,483✔
157
    subscribe->topicNames = NULL;
3,483✔
158
  } else if (updateType == CONSUMER_UPDATE_SUB) {
5,562✔
159
    pConsumer->assignedTopics = subscribe->topicNames;
2,877✔
160
    subscribe->topicNames = NULL;
2,877✔
161
  }
162

163
  *ppConsumer = pConsumer;
15,809✔
164
  return 0;
15,809✔
165

166
END:
×
167
  tDeleteSMqConsumerObj(pConsumer);
×
168
  return code;
×
169
}
170

171
void tClearSMqConsumerObj(SMqConsumerObj *pConsumer) {
58,155✔
172
  if (pConsumer == NULL) return;
58,155✔
173
  taosArrayDestroyP(pConsumer->currentTopics, NULL);
31,427✔
174
  taosArrayDestroyP(pConsumer->rebNewTopics, NULL);
31,427✔
175
  taosArrayDestroyP(pConsumer->rebRemovedTopics, NULL);
31,427✔
176
  taosArrayDestroyP(pConsumer->assignedTopics, NULL);
31,427✔
177
}
178

179
void tDeleteSMqConsumerObj(SMqConsumerObj *pConsumer) {
42,537✔
180
  tClearSMqConsumerObj(pConsumer);
42,537✔
181
  taosMemoryFree(pConsumer);
42,537!
182
}
42,537✔
183

184
int32_t tEncodeSMqConsumerObj(void **buf, const SMqConsumerObj *pConsumer) {
33,214✔
185
  int32_t tlen = 0;
33,214✔
186
  int32_t sz;
187
  tlen += taosEncodeFixedI64(buf, pConsumer->consumerId);
33,214✔
188
  tlen += taosEncodeString(buf, pConsumer->clientId);
33,214!
189
  tlen += taosEncodeString(buf, pConsumer->cgroup);
33,214!
190
  tlen += taosEncodeFixedI8(buf, pConsumer->updateType);
33,214✔
191
  tlen += taosEncodeFixedI32(buf, pConsumer->epoch);
33,214✔
192
  tlen += taosEncodeFixedI32(buf, pConsumer->status);
33,214✔
193

194
  tlen += taosEncodeFixedI32(buf, pConsumer->pid);
33,214✔
195
  tlen += taosEncodeSEpSet(buf, &pConsumer->ep);
33,214✔
196
  tlen += taosEncodeFixedI64(buf, pConsumer->createTime);
33,214✔
197
  tlen += taosEncodeFixedI64(buf, pConsumer->subscribeTime);
33,214✔
198
  tlen += taosEncodeFixedI64(buf, pConsumer->rebalanceTime);
33,214✔
199

200
  // current topics
201
  if (pConsumer->currentTopics) {
33,214✔
202
    sz = taosArrayGetSize(pConsumer->currentTopics);
1,596✔
203
    tlen += taosEncodeFixedI32(buf, sz);
1,596✔
204
    for (int32_t i = 0; i < sz; i++) {
1,978✔
205
      char *topic = taosArrayGetP(pConsumer->currentTopics, i);
382✔
206
      tlen += taosEncodeString(buf, topic);
382✔
207
    }
208
  } else {
209
    tlen += taosEncodeFixedI32(buf, 0);
31,618✔
210
  }
211

212
  // reb new topics
213
  if (pConsumer->rebNewTopics) {
33,214✔
214
    sz = taosArrayGetSize(pConsumer->rebNewTopics);
21,282✔
215
    tlen += taosEncodeFixedI32(buf, sz);
21,282✔
216
    for (int32_t i = 0; i < sz; i++) {
35,214✔
217
      char *topic = taosArrayGetP(pConsumer->rebNewTopics, i);
13,932✔
218
      tlen += taosEncodeString(buf, topic);
13,932✔
219
    }
220
  } else {
221
    tlen += taosEncodeFixedI32(buf, 0);
11,932✔
222
  }
223

224
  // reb removed topics
225
  if (pConsumer->rebRemovedTopics) {
33,214✔
226
    sz = taosArrayGetSize(pConsumer->rebRemovedTopics);
13,912✔
227
    tlen += taosEncodeFixedI32(buf, sz);
13,912✔
228
    for (int32_t i = 0; i < sz; i++) {
26,610✔
229
      char *topic = taosArrayGetP(pConsumer->rebRemovedTopics, i);
12,698✔
230
      tlen += taosEncodeString(buf, topic);
12,698✔
231
    }
232
  } else {
233
    tlen += taosEncodeFixedI32(buf, 0);
19,302✔
234
  }
235

236
  // lost topics
237
  if (pConsumer->assignedTopics) {
33,214✔
238
    sz = taosArrayGetSize(pConsumer->assignedTopics);
14,316✔
239
    tlen += taosEncodeFixedI32(buf, sz);
14,316✔
240
    for (int32_t i = 0; i < sz; i++) {
21,282✔
241
      char *topic = taosArrayGetP(pConsumer->assignedTopics, i);
6,966✔
242
      tlen += taosEncodeString(buf, topic);
6,966✔
243
    }
244
  } else {
245
    tlen += taosEncodeFixedI32(buf, 0);
18,898✔
246
  }
247

248
  tlen += taosEncodeFixedI8(buf, pConsumer->withTbName);
33,214✔
249
  tlen += taosEncodeFixedI8(buf, pConsumer->autoCommit);
33,214✔
250
  tlen += taosEncodeFixedI32(buf, pConsumer->autoCommitInterval);
33,214✔
251
  tlen += taosEncodeFixedI32(buf, pConsumer->resetOffsetCfg);
33,214✔
252
  tlen += taosEncodeFixedI32(buf, pConsumer->maxPollIntervalMs);
33,214✔
253
  tlen += taosEncodeFixedI32(buf, pConsumer->sessionTimeoutMs);
33,214✔
254
  tlen += taosEncodeString(buf, pConsumer->user);
33,214!
255
  tlen += taosEncodeString(buf, pConsumer->fqdn);
33,214!
256
  return tlen;
33,214✔
257
}
258

259
void *tDecodeSMqConsumerObj(const void *buf, SMqConsumerObj *pConsumer, int8_t sver) {
15,618✔
260
  int32_t sz;
15,618✔
261
  buf = taosDecodeFixedI64(buf, &pConsumer->consumerId);
15,618!
262
  buf = taosDecodeStringTo(buf, pConsumer->clientId);
15,618✔
263
  buf = taosDecodeStringTo(buf, pConsumer->cgroup);
15,618✔
264
  buf = taosDecodeFixedI8(buf, &pConsumer->updateType);
15,618✔
265
  buf = taosDecodeFixedI32(buf, &pConsumer->epoch);
15,618!
266
  buf = taosDecodeFixedI32(buf, &pConsumer->status);
15,618!
267

268
  buf = taosDecodeFixedI32(buf, &pConsumer->pid);
15,618!
269
  buf = taosDecodeSEpSet(buf, &pConsumer->ep);
15,618✔
270
  buf = taosDecodeFixedI64(buf, &pConsumer->createTime);
15,618!
271
  buf = taosDecodeFixedI64(buf, &pConsumer->subscribeTime);
15,618!
272
  buf = taosDecodeFixedI64(buf, &pConsumer->rebalanceTime);
31,236!
273

274
  // current topics
275
  buf = taosDecodeFixedI32(buf, &sz);
15,618✔
276
  pConsumer->currentTopics = taosArrayInit(sz, sizeof(void *));
15,618✔
277
  if (pConsumer->currentTopics == NULL) {
15,618!
278
    return NULL;
×
279
  }
280
  for (int32_t i = 0; i < sz; i++) {
15,618!
281
    char *topic;
×
282
    buf = taosDecodeString(buf, &topic);
×
283
    if (taosArrayPush(pConsumer->currentTopics, &topic) == NULL) {
×
284
      return NULL;
×
285
    }
286
  }
287

288
  // reb new topics
289
  buf = taosDecodeFixedI32(buf, &sz);
15,618✔
290
  pConsumer->rebNewTopics = taosArrayInit(sz, sizeof(void *));
15,618✔
291
  for (int32_t i = 0; i < sz; i++) {
22,584✔
292
    char *topic;
6,966✔
293
    buf = taosDecodeString(buf, &topic);
6,966✔
294
    if (taosArrayPush(pConsumer->rebNewTopics, &topic) == NULL) {
13,932!
295
      return NULL;
×
296
    }
297
  }
298

299
  // reb removed topics
300
  buf = taosDecodeFixedI32(buf, &sz);
15,618✔
301
  pConsumer->rebRemovedTopics = taosArrayInit(sz, sizeof(void *));
15,618✔
302
  for (int32_t i = 0; i < sz; i++) {
21,585✔
303
    char *topic;
5,967✔
304
    buf = taosDecodeString(buf, &topic);
5,967✔
305
    if (taosArrayPush(pConsumer->rebRemovedTopics, &topic) == NULL) {
11,934!
306
      return NULL;
×
307
    }
308
  }
309

310
  // reb removed topics
311
  buf = taosDecodeFixedI32(buf, &sz);
15,618✔
312
  pConsumer->assignedTopics = taosArrayInit(sz, sizeof(void *));
15,618✔
313
  for (int32_t i = 0; i < sz; i++) {
19,101✔
314
    char *topic;
3,483✔
315
    buf = taosDecodeString(buf, &topic);
3,483✔
316
    if (taosArrayPush(pConsumer->assignedTopics, &topic) == NULL) {
6,966!
317
      return NULL;
×
318
    }
319
  }
320

321
  if (sver > 1) {
15,618!
322
    buf = taosDecodeFixedI8(buf, &pConsumer->withTbName);
15,618✔
323
    buf = taosDecodeFixedI8(buf, &pConsumer->autoCommit);
15,618✔
324
    buf = taosDecodeFixedI32(buf, &pConsumer->autoCommitInterval);
15,618!
325
    buf = taosDecodeFixedI32(buf, &pConsumer->resetOffsetCfg);
31,236!
326
  }
327
  if (sver > 2) {
15,618!
328
    buf = taosDecodeFixedI32(buf, &pConsumer->maxPollIntervalMs);
15,618!
329
    buf = taosDecodeFixedI32(buf, &pConsumer->sessionTimeoutMs);
15,618!
330
    buf = taosDecodeStringTo(buf, pConsumer->user);
15,618✔
331
    buf = taosDecodeStringTo(buf, pConsumer->fqdn);
31,236✔
332
  } else {
333
    pConsumer->maxPollIntervalMs = DEFAULT_MAX_POLL_INTERVAL;
×
334
    pConsumer->sessionTimeoutMs = DEFAULT_SESSION_TIMEOUT;
×
335
  }
336

337
  return (void *)buf;
15,618✔
338
}
339

340
int32_t tEncodeOffRows(void **buf, SArray *offsetRows) {
28,246✔
341
  int32_t tlen = 0;
28,246✔
342
  int32_t szVgs = taosArrayGetSize(offsetRows);
28,246✔
343
  tlen += taosEncodeFixedI32(buf, szVgs);
28,246✔
344
  for (int32_t j = 0; j < szVgs; ++j) {
95,358✔
345
    OffsetRows *offRows = taosArrayGet(offsetRows, j);
67,112✔
346
    tlen += taosEncodeFixedI32(buf, offRows->vgId);
67,112✔
347
    tlen += taosEncodeFixedI64(buf, offRows->rows);
67,112✔
348
    tlen += taosEncodeFixedI8(buf, offRows->offset.type);
67,112✔
349
    if (offRows->offset.type == TMQ_OFFSET__SNAPSHOT_DATA || offRows->offset.type == TMQ_OFFSET__SNAPSHOT_META) {
67,112!
350
      tlen += taosEncodeFixedI64(buf, offRows->offset.uid);
×
351
      tlen += taosEncodeFixedI64(buf, offRows->offset.ts);
×
352
    } else if (offRows->offset.type == TMQ_OFFSET__LOG) {
67,112✔
353
      tlen += taosEncodeFixedI64(buf, offRows->offset.version);
132,712✔
354
    } else {
355
      // do nothing
356
    }
357
    tlen += taosEncodeFixedI64(buf, offRows->ever);
134,224✔
358
  }
359

360
  return tlen;
28,246✔
361
}
362

363
int32_t tEncodeSMqConsumerEp(void **buf, const SMqConsumerEp *pConsumerEp) {
7,752✔
364
  int32_t tlen = 0;
7,752✔
365
  tlen += taosEncodeFixedI64(buf, pConsumerEp->consumerId);
7,752✔
366
  int32_t sz = taosArrayGetSize(pConsumerEp->vgs);
7,752✔
367
  tlen += taosEncodeFixedI32(buf, sz);
7,752✔
368
  for (int32_t i = 0; i < sz; i++) {
42,498✔
369
    void* data = taosArrayGet(pConsumerEp->vgs, i);
34,746✔
370
    tlen += tEncodeSMqVgEp(buf, data);
34,746✔
371
  }
372

373
  return tlen + tEncodeOffRows(buf, pConsumerEp->offsetRows);
7,752✔
374
}
375

376
void *tDecodeOffRows(const void *buf, SArray **offsetRows, int8_t sver) {
13,348✔
377
  int32_t szVgs = 0;
13,348!
378
  buf = taosDecodeFixedI32(buf, &szVgs);
13,348✔
379
  if (szVgs > 0) {
13,348✔
380
    *offsetRows = taosArrayInit(szVgs, sizeof(OffsetRows));
5,978✔
381
    if (NULL == *offsetRows) return NULL;
5,978!
382
    for (int32_t j = 0; j < szVgs; ++j) {
38,950✔
383
      OffsetRows *offRows = taosArrayReserve(*offsetRows, 1);
32,972✔
384
      buf = taosDecodeFixedI32(buf, &offRows->vgId);
32,972!
385
      buf = taosDecodeFixedI64(buf, &offRows->rows);
32,972!
386
      buf = taosDecodeFixedI8(buf, &offRows->offset.type);
32,972✔
387
      if (offRows->offset.type == TMQ_OFFSET__SNAPSHOT_DATA || offRows->offset.type == TMQ_OFFSET__SNAPSHOT_META) {
32,972!
388
        buf = taosDecodeFixedI64(buf, &offRows->offset.uid);
×
389
        buf = taosDecodeFixedI64(buf, &offRows->offset.ts);
×
390
      } else if (offRows->offset.type == TMQ_OFFSET__LOG) {
32,972✔
391
        buf = taosDecodeFixedI64(buf, &offRows->offset.version);
65,188!
392
      } else {
393
        // do nothing
394
      }
395
      if (sver > 2) {
32,972!
396
        buf = taosDecodeFixedI64(buf, &offRows->ever);
65,944!
397
      }
398
    }
399
  }
400
  return (void *)buf;
13,348✔
401
}
402

403
void *tDecodeSMqConsumerEp(const void *buf, SMqConsumerEp *pConsumerEp, int8_t sver) {
3,685✔
404
  buf = taosDecodeFixedI64(buf, &pConsumerEp->consumerId);
3,685!
405
  int32_t sz = 0;
3,685!
406
  buf = taosDecodeFixedI32(buf, &sz);
3,685✔
407
  pConsumerEp->vgs = taosArrayInit(sz, sizeof(SMqVgEp));
3,685✔
408
  if (pConsumerEp->vgs == NULL) {
3,685!
409
    return NULL;
×
410
  }
411
  for (int32_t i = 0; i < sz; i++) {
20,867✔
412
    SMqVgEp* vgEp = taosArrayReserve(pConsumerEp->vgs, 1);
17,182✔
413
    if (vgEp != NULL)
17,182!
414
      buf = tDecodeSMqVgEp(buf, vgEp, sver);
17,182✔
415
  }
416
  if (sver > 1) {
3,685!
417
    buf = tDecodeOffRows(buf, &pConsumerEp->offsetRows, sver);
3,685✔
418
  }
419

420
  return (void *)buf;
3,685✔
421
}
422

423
int32_t tNewSubscribeObj(const char *key, SMqSubscribeObj **ppSub) {
3,483✔
424
  int32_t          code = 0;
3,483✔
425
  SMqSubscribeObj *pSubObj = taosMemoryCalloc(1, sizeof(SMqSubscribeObj));
3,483!
426
  MND_TMQ_NULL_CHECK(pSubObj);
3,483!
427
  *ppSub = pSubObj;
3,483✔
428
  
429
  (void)memcpy(pSubObj->key, key, TSDB_SUBSCRIBE_KEY_LEN);
3,483!
430
  taosInitRWLatch(&pSubObj->lock);
3,483✔
431
  pSubObj->vgNum = 0;
3,483✔
432
  pSubObj->consumerHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
3,483✔
433
  MND_TMQ_NULL_CHECK(pSubObj->consumerHash);
3,483!
434
  taosHashSetFreeFp(pSubObj->consumerHash, freeSMqConsumerEp);
3,483✔
435

436
  pSubObj->unassignedVgs = taosArrayInit(0, sizeof(SMqVgEp));
3,483✔
437
  MND_TMQ_NULL_CHECK(pSubObj->unassignedVgs);
3,483!
438

439
END:
3,483✔
440
  return code;
3,483✔
441
}
442

443
int32_t tCloneSubscribeObj(const SMqSubscribeObj *pSub, SMqSubscribeObj **ppSub) {
3,281✔
444
  int32_t          code = 0;
3,281✔
445
  SMqSubscribeObj *pSubNew = taosMemoryMalloc(sizeof(SMqSubscribeObj));
3,281!
446
  if (pSubNew == NULL) {
3,281!
447
    code = terrno;
×
448
    goto END;
×
449
  }
450
  *ppSub = pSubNew;
3,281✔
451

452
  (void)memcpy(pSubNew->key, pSub->key, TSDB_SUBSCRIBE_KEY_LEN);
3,281!
453
  taosInitRWLatch(&pSubNew->lock);
3,281✔
454

455
  pSubNew->dbUid = pSub->dbUid;
3,281✔
456
  pSubNew->stbUid = pSub->stbUid;
3,281✔
457
  pSubNew->subType = pSub->subType;
3,281✔
458
  pSubNew->withMeta = pSub->withMeta;
3,281✔
459

460
  pSubNew->vgNum = pSub->vgNum;
3,281✔
461
  pSubNew->consumerHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
3,281✔
462
  if (pSubNew->consumerHash == NULL) {
3,281!
463
    code = terrno;
×
464
    goto END;
×
465
  }
466
  taosHashSetFreeFp(pSubNew->consumerHash, freeSMqConsumerEp);
3,281✔
467

468
  void          *pIter = NULL;
3,281✔
469
  SMqConsumerEp *pConsumerEp = NULL;
3,281✔
470
  while (1) {
3,281✔
471
    pIter = taosHashIterate(pSub->consumerHash, pIter);
6,562✔
472
    if (pIter == NULL) break;
6,562✔
473
    pConsumerEp = (SMqConsumerEp *)pIter;
3,281✔
474
    SMqConsumerEp newEp = {
3,281✔
475
        .consumerId = pConsumerEp->consumerId,
3,281✔
476
        .vgs = taosArrayDup(pConsumerEp->vgs, NULL),
3,281✔
477
    };
478
    if ((code = taosHashPut(pSubNew->consumerHash, &newEp.consumerId, sizeof(int64_t), &newEp,
3,281!
479
                            sizeof(SMqConsumerEp))) != 0)
480
      goto END;
×
481
  }
482
  pSubNew->unassignedVgs = taosArrayDup(pSub->unassignedVgs, NULL);
3,281✔
483
  if (pSubNew->unassignedVgs == NULL) {
3,281!
484
    code = terrno;
×
485
    goto END;
×
486
  }
487
  pSubNew->offsetRows = taosArrayDup(pSub->offsetRows, NULL);
3,281✔
488
  if (pSub->offsetRows != NULL && pSubNew->offsetRows == NULL) {
3,281!
489
    code = terrno;
×
490
    goto END;
×
491
  }
492
  (void)memcpy(pSubNew->dbName, pSub->dbName, TSDB_DB_FNAME_LEN);
3,281!
493
  pSubNew->qmsg = taosStrdup(pSub->qmsg);
3,281!
494
  if (pSubNew->qmsg == NULL) {
3,281!
495
    code = terrno;
×
496
    goto END;
×
497
  }
498

499
END:
3,281✔
500
  return code;
3,281✔
501
}
502

503
void tDeleteSubscribeObj(SMqSubscribeObj *pSub) {
16,427✔
504
  if (pSub == NULL) return;
16,427!
505
  taosHashCleanup(pSub->consumerHash);
16,427✔
506
  pSub->consumerHash = NULL;
16,427✔
507
  taosArrayDestroy(pSub->unassignedVgs);
16,427✔
508
  pSub->unassignedVgs = NULL;
16,427✔
509
  taosMemoryFreeClear(pSub->qmsg);
16,427!
510
  taosArrayDestroy(pSub->offsetRows);
16,427✔
511
  pSub->offsetRows = NULL;
16,427✔
512
}
513

514
int32_t tEncodeSubscribeObj(void **buf, const SMqSubscribeObj *pSub) {
20,494✔
515
  int32_t tlen = 0;
20,494✔
516
  tlen += taosEncodeString(buf, pSub->key);
20,494!
517
  tlen += taosEncodeFixedI64(buf, pSub->dbUid);
20,494✔
518
  tlen += taosEncodeFixedI32(buf, pSub->vgNum);
20,494✔
519
  tlen += taosEncodeFixedI8(buf, pSub->subType);
20,494✔
520
  tlen += taosEncodeFixedI8(buf, pSub->withMeta);
20,494✔
521
  tlen += taosEncodeFixedI64(buf, pSub->stbUid);
20,494✔
522

523
  void   *pIter = NULL;
20,494✔
524
  int32_t sz = taosHashGetSize(pSub->consumerHash);
20,494✔
525
  tlen += taosEncodeFixedI32(buf, sz);
20,494✔
526

527
  int32_t cnt = 0;
20,494✔
528
  while (1) {
7,752✔
529
    pIter = taosHashIterate(pSub->consumerHash, pIter);
28,246✔
530
    if (pIter == NULL) break;
28,246✔
531
    SMqConsumerEp *pConsumerEp = (SMqConsumerEp *)pIter;
7,752✔
532
    tlen += tEncodeSMqConsumerEp(buf, pConsumerEp);
7,752✔
533
    cnt++;
7,752✔
534
  }
535
  if (cnt != sz) return -1;
20,494!
536
  int32_t len = taosArrayGetSize(pSub->unassignedVgs);
20,494✔
537
  tlen += taosEncodeFixedI32(buf, len);
20,494✔
538
  for (int32_t i = 0; i < len; i++) {
87,224✔
539
    void* data = taosArrayGet(pSub->unassignedVgs, i);
66,730✔
540
    tlen += tEncodeSMqVgEp(buf, data);
66,730✔
541
  }
542
  tlen += taosEncodeString(buf, pSub->dbName);
20,494!
543

544
  tlen += tEncodeOffRows(buf, pSub->offsetRows);
20,494✔
545
  tlen += taosEncodeString(buf, pSub->qmsg);
20,494!
546
  return tlen;
20,494✔
547
}
548

549
void *tDecodeSubscribeObj(const void *buf, SMqSubscribeObj *pSub, int8_t sver) {
9,663✔
550
  //
551
  buf = taosDecodeStringTo(buf, pSub->key);
9,663✔
552
  buf = taosDecodeFixedI64(buf, &pSub->dbUid);
9,663!
553
  buf = taosDecodeFixedI32(buf, &pSub->vgNum);
9,663!
554
  buf = taosDecodeFixedI8(buf, &pSub->subType);
9,663✔
555
  buf = taosDecodeFixedI8(buf, &pSub->withMeta);
9,663✔
556
  buf = taosDecodeFixedI64(buf, &pSub->stbUid);
9,663!
557

558
  int32_t sz;
9,663✔
559
  buf = taosDecodeFixedI32(buf, &sz);
9,663✔
560

561
  pSub->consumerHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
9,663✔
562
  if (pSub->consumerHash == NULL) {
9,663!
563
    return NULL;
×
564
  }
565
  taosHashSetFreeFp(pSub->consumerHash, freeSMqConsumerEp);
9,663✔
566

567
  for (int32_t i = 0; i < sz; i++) {
13,348✔
568
    SMqConsumerEp consumerEp = {0};
3,685✔
569
    buf = tDecodeSMqConsumerEp(buf, &consumerEp, sver);
3,685✔
570
    if (taosHashPut(pSub->consumerHash, &consumerEp.consumerId, sizeof(int64_t), &consumerEp, sizeof(SMqConsumerEp)) !=
3,685!
571
        0)
572
      return NULL;
×
573
  }
574

575
  int32_t len = 0;
9,663!
576
  buf = taosDecodeFixedI32(buf, &len);
9,663✔
577
  pSub->unassignedVgs = taosArrayInit(len, sizeof(SMqVgEp));
9,663✔
578
  if (pSub->unassignedVgs == NULL) {
9,663!
579
    return NULL;
×
580
  }
581
  for (int32_t i = 0; i < len; i++) {
42,635✔
582
    SMqVgEp* vgEp = taosArrayReserve(pSub->unassignedVgs, 1);
32,972✔
583
    if (vgEp != NULL)
32,972!
584
      buf = tDecodeSMqVgEp(buf, vgEp, sver);
32,972✔
585
  }
586

587
  buf = taosDecodeStringTo(buf, pSub->dbName);
9,663✔
588

589
  if (sver > 1) {
9,663!
590
    buf = tDecodeOffRows(buf, &pSub->offsetRows, sver);
9,663✔
591
    buf = taosDecodeString(buf, &pSub->qmsg);
19,326✔
592
  } else {
593
    pSub->qmsg = taosStrdup("");
×
594
  }
595
  return (void *)buf;
9,663✔
596
}
597

598
int32_t mndInitConfigObj(SConfigItem *pItem, SConfigObj *pObj) {
12,446,958✔
599
  tstrncpy(pObj->name, pItem->name, CFG_NAME_MAX_LEN);
12,446,958!
600
  pObj->dtype = pItem->dtype;
12,446,958✔
601
  switch (pItem->dtype) {
12,446,958!
602
    case CFG_DTYPE_NONE:
×
603
      break;
×
604
    case CFG_DTYPE_BOOL:
2,318,551✔
605
      pObj->bval = pItem->bval;
2,318,551!
606
      break;
2,318,551✔
607
    case CFG_DTYPE_INT32:
6,955,653✔
608
      pObj->i32 = pItem->i32;
6,955,653✔
609
      break;
6,955,653✔
610
    case CFG_DTYPE_INT64:
732,174✔
611
      pObj->i64 = pItem->i64;
732,174✔
612
      break;
732,174✔
613
    case CFG_DTYPE_FLOAT:
244,058✔
614
    case CFG_DTYPE_DOUBLE:
615
      pObj->fval = pItem->fval;
244,058✔
616
      break;
244,058✔
617
    case CFG_DTYPE_STRING:
2,196,522✔
618
    case CFG_DTYPE_DIR:
619
    case CFG_DTYPE_LOCALE:
620
    case CFG_DTYPE_CHARSET:
621
    case CFG_DTYPE_TIMEZONE:
622
      pObj->str = taosStrdup(pItem->str);
2,196,522!
623
      if (pObj->str == NULL) {
2,196,522!
624
        taosMemoryFree(pObj);
×
625
        return TSDB_CODE_OUT_OF_MEMORY;
×
626
      }
627
      break;
2,196,522✔
628
  }
629
  return TSDB_CODE_SUCCESS;
12,446,958✔
630
}
631

632
int32_t mndUpdateObj(SConfigObj *pObjNew, const char *name, char *value) {
1,316✔
633
  int32_t code = 0;
1,316✔
634
  switch (pObjNew->dtype) {
1,316!
635
    case CFG_DTYPE_BOOL: {
529✔
636
      bool tmp = false;
529✔
637
      if (strcasecmp(value, "true") == 0) {
529!
638
        tmp = true;
×
639
      }
640
      if (taosStr2Int32(value, NULL, 10) > 0) {
529✔
641
        tmp = true;
232✔
642
      }
643
      pObjNew->bval = tmp;
529✔
644
      break;
529✔
645
    }
646
    case CFG_DTYPE_INT32: {
708✔
647
      int32_t ival;
708✔
648
      TAOS_CHECK_RETURN(taosStrHumanToInt32(value, &ival));
708!
649
      pObjNew->i32 = ival;
708✔
650
      break;
708✔
651
    }
652
    case CFG_DTYPE_INT64: {
×
653
      int64_t ival;
×
654
      TAOS_CHECK_RETURN(taosStrHumanToInt64(value, &ival));
×
655
      pObjNew->i64 = ival;
×
656
      break;
×
657
    }
658
    case CFG_DTYPE_FLOAT:
×
659
    case CFG_DTYPE_DOUBLE: {
660
      float dval = 0;
×
661
      TAOS_CHECK_RETURN(parseCfgReal(value, &dval));
×
662
      pObjNew->fval = dval;
×
663
      break;
×
664
    }
665
    case CFG_DTYPE_DIR:
79✔
666
    case CFG_DTYPE_TIMEZONE:
667
    case CFG_DTYPE_CHARSET:
668
    case CFG_DTYPE_LOCALE:
669
    case CFG_DTYPE_STRING: {
670
      pObjNew->str = taosStrdup(value);
79!
671
      if (pObjNew->str == NULL) {
79!
672
        code = terrno;
×
673
        return code;
×
674
      }
675
      break;
79✔
676
    }
677
    case CFG_DTYPE_NONE:
×
678
      break;
×
679
    default:
×
680
      code = TSDB_CODE_INVALID_CFG;
×
681
      break;
×
682
  }
683
  return code;
1,316✔
684
}
685

686
SConfigObj mndInitConfigVersion() {
122,029✔
687
  SConfigObj obj;
121,939✔
688
  memset(&obj, 0, sizeof(SConfigObj));
122,029!
689

690
  tstrncpy(obj.name, "tsmmConfigVersion", CFG_NAME_MAX_LEN);
122,029!
691
  obj.dtype = CFG_DTYPE_INT32;
122,029✔
692
  obj.i32 = 0;
122,029✔
693
  return obj;
122,029✔
694
}
695

696
int32_t tEncodeSConfigObj(SEncoder *pEncoder, const SConfigObj *pObj) {
87,691,842✔
697
  TAOS_CHECK_RETURN(tStartEncode(pEncoder));
87,691,842!
698
  TAOS_CHECK_RETURN(tEncodeCStr(pEncoder, pObj->name));
175,383,684!
699

700
  TAOS_CHECK_RETURN(tEncodeI32(pEncoder, pObj->dtype));
175,383,684!
701
  switch (pObj->dtype) {
87,691,842!
702
    case CFG_DTYPE_BOOL:
16,176,252✔
703
      TAOS_CHECK_RETURN(tEncodeI8(pEncoder, pObj->bval));
32,352,504!
704
      break;
16,176,252✔
705
    case CFG_DTYPE_INT32:
49,380,956✔
706
      TAOS_CHECK_RETURN(tEncodeI32(pEncoder, pObj->i32));
98,761,912!
707
      break;
49,380,956✔
708
    case CFG_DTYPE_INT64:
5,107,956✔
709
      TAOS_CHECK_RETURN(tEncodeI64(pEncoder, pObj->i64));
10,215,912!
710
      break;
5,107,956✔
711
    case CFG_DTYPE_FLOAT:
1,702,652✔
712
    case CFG_DTYPE_DOUBLE:
713
      TAOS_CHECK_RETURN(tEncodeFloat(pEncoder, pObj->fval));
3,405,304!
714
      break;
1,702,652✔
715
    case CFG_DTYPE_STRING:
15,324,026✔
716
    case CFG_DTYPE_DIR:
717
    case CFG_DTYPE_LOCALE:
718
    case CFG_DTYPE_CHARSET:
719
    case CFG_DTYPE_TIMEZONE:
720
      if (pObj->str != NULL) {
15,324,026!
721
        TAOS_CHECK_RETURN(tEncodeCStr(pEncoder, pObj->str));
30,648,052!
722
      } else {
723
        TAOS_CHECK_RETURN(tEncodeCStr(pEncoder, ""));
×
724
      }
725
      break;
15,324,026✔
726
    default:
×
727
      break;
×
728
  }
729
  tEndEncode(pEncoder);
87,691,842✔
730
  return pEncoder->pos;
87,691,842✔
731
}
732

733
int32_t tDecodeSConfigObj(SDecoder *pDecoder, SConfigObj *pObj) {
17,194,940✔
734
  TAOS_CHECK_RETURN(tStartDecode(pDecoder));
17,194,940!
735
  TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, pObj->name));
17,194,940!
736
  TAOS_CHECK_RETURN(tDecodeI32(pDecoder, (int32_t *)&pObj->dtype));
34,389,880!
737
  switch (pObj->dtype) {
17,194,940!
738
    case CFG_DTYPE_NONE:
×
739
      break;
×
740
    case CFG_DTYPE_BOOL:
3,168,845✔
741
      TAOS_CHECK_RETURN(tDecodeBool(pDecoder, &pObj->bval));
3,168,845!
742
      break;
3,168,845✔
743
    case CFG_DTYPE_INT32:
9,692,856✔
744
      TAOS_CHECK_RETURN(tDecodeI32(pDecoder, &pObj->i32));
19,385,712!
745
      break;
9,692,856✔
746
    case CFG_DTYPE_INT64:
1,000,800✔
747
      TAOS_CHECK_RETURN(tDecodeI64(pDecoder, &pObj->i64));
2,001,600!
748
      break;
1,000,800✔
749
    case CFG_DTYPE_FLOAT:
333,704✔
750
    case CFG_DTYPE_DOUBLE:
751
      TAOS_CHECK_RETURN(tDecodeFloat(pDecoder, &pObj->fval));
667,408!
752
      break;
333,704✔
753
    case CFG_DTYPE_STRING:
2,998,735✔
754
    case CFG_DTYPE_DIR:
755
    case CFG_DTYPE_LOCALE:
756
    case CFG_DTYPE_CHARSET:
757
    case CFG_DTYPE_TIMEZONE:
758
      TAOS_CHECK_RETURN(tDecodeCStrAlloc(pDecoder, &pObj->str));
5,997,470!
759
      break;
2,998,735✔
760
  }
761
  tEndDecode(pDecoder);
17,194,940✔
762
  TAOS_RETURN(TSDB_CODE_SUCCESS);
17,194,940✔
763
}
764

765
void tFreeSConfigObj(SConfigObj *obj) {
29,761,821✔
766
  if (obj == NULL) {
29,761,821!
767
    return;
×
768
  }
769
  if (obj->dtype == CFG_DTYPE_STRING || obj->dtype == CFG_DTYPE_DIR || obj->dtype == CFG_DTYPE_LOCALE ||
29,761,821!
770
      obj->dtype == CFG_DTYPE_CHARSET || obj->dtype == CFG_DTYPE_TIMEZONE) {
25,144,517✔
771
    taosMemoryFree(obj->str);
5,194,508!
772
  }
773
}
774

775
// SMqSubActionLogEntry *tCloneSMqSubActionLogEntry(SMqSubActionLogEntry *pEntry) {
776
//   SMqSubActionLogEntry *pEntryNew = taosMemoryMalloc(sizeof(SMqSubActionLogEntry));
777
//   if (pEntryNew == NULL) return NULL;
778
//   pEntryNew->epoch = pEntry->epoch;
779
//   pEntryNew->consumers = taosArrayDup(pEntry->consumers, (__array_item_dup_fn_t)tCloneSMqConsumerEp);
780
//   return pEntryNew;
781
// }
782
//
783
// void tDeleteSMqSubActionLogEntry(SMqSubActionLogEntry *pEntry) {
784
//   taosArrayDestroyEx(pEntry->consumers, (FDelete)tDeleteSMqConsumerEp);
785
// }
786

787
// int32_t tEncodeSMqSubActionLogEntry(void **buf, const SMqSubActionLogEntry *pEntry) {
788
//   int32_t tlen = 0;
789
//   tlen += taosEncodeFixedI32(buf, pEntry->epoch);
790
//   tlen += taosEncodeArray(buf, pEntry->consumers, (FEncode)tEncodeSMqSubActionLogEntry);
791
//   return tlen;
792
// }
793
//
794
// void *tDecodeSMqSubActionLogEntry(const void *buf, SMqSubActionLogEntry *pEntry) {
795
//   buf = taosDecodeFixedI32(buf, &pEntry->epoch);
796
//   buf = taosDecodeArray(buf, &pEntry->consumers, (FDecode)tDecodeSMqSubActionLogEntry, sizeof(SMqSubActionLogEntry));
797
//   return (void *)buf;
798
// }
799

800
// SMqSubActionLogObj *tCloneSMqSubActionLogObj(SMqSubActionLogObj *pLog) {
801
//   SMqSubActionLogObj *pLogNew = taosMemoryMalloc(sizeof(SMqSubActionLogObj));
802
//   if (pLogNew == NULL) return pLogNew;
803
//   memcpy(pLogNew->key, pLog->key, TSDB_SUBSCRIBE_KEY_LEN);
804
//   pLogNew->logs = taosArrayDup(pLog->logs, (__array_item_dup_fn_t)tCloneSMqConsumerEp);
805
//   return pLogNew;
806
// }
807
//
808
// void tDeleteSMqSubActionLogObj(SMqSubActionLogObj *pLog) {
809
//   taosArrayDestroyEx(pLog->logs, (FDelete)tDeleteSMqConsumerEp);
810
// }
811

812
// int32_t tEncodeSMqSubActionLogObj(void **buf, const SMqSubActionLogObj *pLog) {
813
//   int32_t tlen = 0;
814
//   tlen += taosEncodeString(buf, pLog->key);
815
//   tlen += taosEncodeArray(buf, pLog->logs, (FEncode)tEncodeSMqSubActionLogEntry);
816
//   return tlen;
817
// }
818
//
819
// void *tDecodeSMqSubActionLogObj(const void *buf, SMqSubActionLogObj *pLog) {
820
//   buf = taosDecodeStringTo(buf, pLog->key);
821
//   buf = taosDecodeArray(buf, &pLog->logs, (FDecode)tDecodeSMqSubActionLogEntry, sizeof(SMqSubActionLogEntry));
822
//   return (void *)buf;
823
// }
824
//
825
// int32_t tEncodeSMqOffsetObj(void **buf, const SMqOffsetObj *pOffset) {
826
//   int32_t tlen = 0;
827
//   tlen += taosEncodeString(buf, pOffset->key);
828
//   tlen += taosEncodeFixedI64(buf, pOffset->offset);
829
//   return tlen;
830
// }
831
//
832
// void *tDecodeSMqOffsetObj(void *buf, SMqOffsetObj *pOffset) {
833
//   buf = taosDecodeStringTo(buf, pOffset->key);
834
//   buf = taosDecodeFixedI64(buf, &pOffset->offset);
835
//   return buf;
836
// }
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