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

taosdata / TDengine / #4788

14 Oct 2025 11:21AM UTC coverage: 60.992% (-2.3%) from 63.264%
#4788

push

travis-ci

web-flow
Merge 7ca9b50f9 into 19574fe21

154868 of 324306 branches covered (47.75%)

Branch coverage included in aggregate %.

207304 of 269498 relevant lines covered (76.92%)

125773493.22 hits per line

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

77.39
/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) {
841,446✔
22
  TAOS_CHECK_RETURN(tStartEncode(pEncoder));
841,446!
23

24
  TAOS_CHECK_RETURN(tEncodeCStr(pEncoder, pObj->name));
1,682,892!
25
  TAOS_CHECK_RETURN(tSerializeSCMCreateStreamReqImpl(pEncoder, pObj->pCreate));
841,446!
26

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

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

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

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

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

54
_exit:
232,635✔
55

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

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

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

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

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

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

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

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

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

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

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

124
    char *topicTmp = taosStrdup(topic);
45,525!
125
    if (taosArrayPush(pConsumer->rebNewTopics, &topicTmp) == NULL) {
91,050!
126
      code = terrno;
×
127
      goto END;
×
128
    }
129
  } else if (updateType == CONSUMER_REMOVE_REB) {
141,166✔
130
    pConsumer->rebRemovedTopics = taosArrayInit(0, sizeof(void *));
32,697✔
131
    if (pConsumer->rebRemovedTopics == NULL) {
32,697!
132
      code = terrno;
×
133
      goto END;
×
134
    }
135
    char *topicTmp = taosStrdup(topic);
32,697!
136
    if (taosArrayPush(pConsumer->rebRemovedTopics, &topicTmp) == NULL) {
65,394!
137
      code = terrno;
×
138
      goto END;
×
139
    }
140
  } else if (updateType == CONSUMER_INSERT_SUB) {
108,469✔
141
    tstrncpy(pConsumer->clientId, subscribe->clientId, tListLen(pConsumer->clientId));
44,856!
142
    pConsumer->withTbName = subscribe->withTbName;
44,856✔
143
    pConsumer->autoCommit = subscribe->autoCommit;
44,856✔
144
    pConsumer->autoCommitInterval = subscribe->autoCommitInterval;
44,856✔
145
    pConsumer->resetOffsetCfg = subscribe->resetOffsetCfg;
44,856✔
146
    pConsumer->maxPollIntervalMs = subscribe->maxPollIntervalMs;
44,856✔
147
    pConsumer->sessionTimeoutMs = subscribe->sessionTimeoutMs;
44,856✔
148
    tstrncpy(pConsumer->user, subscribe->user, TSDB_USER_LEN);
44,856!
149
    tstrncpy(pConsumer->fqdn, subscribe->fqdn, TSDB_FQDN_LEN);
44,856!
150

151
    pConsumer->rebNewTopics = taosArrayDup(subscribe->topicNames, topicNameDup);
44,856✔
152
    if (pConsumer->rebNewTopics == NULL) {
44,856!
153
      code = terrno;
×
154
      goto END;
×
155
    }
156
    pConsumer->assignedTopics = subscribe->topicNames;
44,856✔
157
    subscribe->topicNames = NULL;
44,856✔
158
  } else if (updateType == CONSUMER_UPDATE_SUB) {
63,613✔
159
    pConsumer->assignedTopics = subscribe->topicNames;
32,430✔
160
    subscribe->topicNames = NULL;
32,430✔
161
  }
162

163
  *ppConsumer = pConsumer;
186,691✔
164
  return 0;
186,691✔
165

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

171
void tClearSMqConsumerObj(SMqConsumerObj *pConsumer) {
665,023✔
172
  if (pConsumer == NULL) return;
665,023✔
173
  taosArrayDestroyP(pConsumer->currentTopics, NULL);
373,861✔
174
  taosArrayDestroyP(pConsumer->rebNewTopics, NULL);
373,861✔
175
  taosArrayDestroyP(pConsumer->rebRemovedTopics, NULL);
373,861✔
176
  taosArrayDestroyP(pConsumer->assignedTopics, NULL);
373,861✔
177
}
178

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

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

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

200
  // current topics
201
  if (pConsumer->currentTopics) {
447,098✔
202
    sz = taosArrayGetSize(pConsumer->currentTopics);
75,630✔
203
    tlen += taosEncodeFixedI32(buf, sz);
75,630✔
204
    for (int32_t i = 0; i < sz; i++) {
120,920✔
205
      char *topic = taosArrayGetP(pConsumer->currentTopics, i);
45,290✔
206
      tlen += taosEncodeString(buf, topic);
45,290✔
207
    }
208
  } else {
209
    tlen += taosEncodeFixedI32(buf, 0);
371,468✔
210
  }
211

212
  // reb new topics
213
  if (pConsumer->rebNewTopics) {
447,098✔
214
    sz = taosArrayGetSize(pConsumer->rebNewTopics);
319,338✔
215
    tlen += taosEncodeFixedI32(buf, sz);
319,338✔
216
    for (int32_t i = 0; i < sz; i++) {
521,730✔
217
      char *topic = taosArrayGetP(pConsumer->rebNewTopics, i);
202,392✔
218
      tlen += taosEncodeString(buf, topic);
202,392✔
219
    }
220
  } else {
221
    tlen += taosEncodeFixedI32(buf, 0);
127,760✔
222
  }
223

224
  // reb removed topics
225
  if (pConsumer->rebRemovedTopics) {
447,098✔
226
    sz = taosArrayGetSize(pConsumer->rebRemovedTopics);
203,970✔
227
    tlen += taosEncodeFixedI32(buf, sz);
203,970✔
228
    for (int32_t i = 0; i < sz; i++) {
333,450✔
229
      char *topic = taosArrayGetP(pConsumer->rebRemovedTopics, i);
129,480✔
230
      tlen += taosEncodeString(buf, topic);
129,480✔
231
    }
232
  } else {
233
    tlen += taosEncodeFixedI32(buf, 0);
243,128✔
234
  }
235

236
  // lost topics
237
  if (pConsumer->assignedTopics) {
447,098✔
238
    sz = taosArrayGetSize(pConsumer->assignedTopics);
228,288✔
239
    tlen += taosEncodeFixedI32(buf, sz);
228,288✔
240
    for (int32_t i = 0; i < sz; i++) {
383,694✔
241
      char *topic = taosArrayGetP(pConsumer->assignedTopics, i);
155,406✔
242
      tlen += taosEncodeString(buf, topic);
155,406✔
243
    }
244
  } else {
245
    tlen += taosEncodeFixedI32(buf, 0);
218,810✔
246
  }
247

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

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

268
  buf = taosDecodeFixedI32(buf, &pConsumer->pid);
187,170!
269
  buf = taosDecodeSEpSet(buf, &pConsumer->ep);
187,170✔
270
  buf = taosDecodeFixedI64(buf, &pConsumer->createTime);
187,170!
271
  buf = taosDecodeFixedI64(buf, &pConsumer->subscribeTime);
187,170!
272
  buf = taosDecodeFixedI64(buf, &pConsumer->rebalanceTime);
374,340!
273

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

288
  // reb new topics
289
  buf = taosDecodeFixedI32(buf, &sz);
187,170✔
290
  pConsumer->rebNewTopics = taosArrayInit(sz, sizeof(void *));
187,170✔
291
  for (int32_t i = 0; i < sz; i++) {
278,536✔
292
    char *topic;
91,294✔
293
    buf = taosDecodeString(buf, &topic);
91,366✔
294
    if (taosArrayPush(pConsumer->rebNewTopics, &topic) == NULL) {
182,732!
295
      return NULL;
×
296
    }
297
  }
298

299
  // reb removed topics
300
  buf = taosDecodeFixedI32(buf, &sz);
187,170✔
301
  pConsumer->rebRemovedTopics = taosArrayInit(sz, sizeof(void *));
187,170✔
302
  for (int32_t i = 0; i < sz; i++) {
251,481✔
303
    char *topic;
64,239✔
304
    buf = taosDecodeString(buf, &topic);
64,311✔
305
    if (taosArrayPush(pConsumer->rebRemovedTopics, &topic) == NULL) {
128,622!
306
      return NULL;
×
307
    }
308
  }
309

310
  // reb removed topics
311
  buf = taosDecodeFixedI32(buf, &sz);
187,170✔
312
  pConsumer->assignedTopics = taosArrayInit(sz, sizeof(void *));
187,170✔
313
  for (int32_t i = 0; i < sz; i++) {
233,638✔
314
    char *topic;
46,432✔
315
    buf = taosDecodeString(buf, &topic);
46,468✔
316
    if (taosArrayPush(pConsumer->assignedTopics, &topic) == NULL) {
92,936!
317
      return NULL;
×
318
    }
319
  }
320

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

337
  return (void *)buf;
187,170✔
338
}
339

340
int32_t tEncodeOffRows(void **buf, SArray *offsetRows) {
400,618✔
341
  int32_t tlen = 0;
400,618✔
342
  int32_t szVgs = taosArrayGetSize(offsetRows);
400,618✔
343
  tlen += taosEncodeFixedI32(buf, szVgs);
400,618✔
344
  for (int32_t j = 0; j < szVgs; ++j) {
918,144✔
345
    OffsetRows *offRows = taosArrayGet(offsetRows, j);
517,526✔
346
    tlen += taosEncodeFixedI32(buf, offRows->vgId);
517,526✔
347
    tlen += taosEncodeFixedI64(buf, offRows->rows);
517,526✔
348
    tlen += taosEncodeFixedI8(buf, offRows->offset.type);
517,526✔
349
    if (offRows->offset.type == TMQ_OFFSET__SNAPSHOT_DATA || offRows->offset.type == TMQ_OFFSET__SNAPSHOT_META) {
517,526!
350
      tlen += taosEncodeFixedI64(buf, offRows->offset.uid);
18,460✔
351
      tlen += taosEncodeFixedI64(buf, offRows->offset.ts);
36,920✔
352
    } else if (offRows->offset.type == TMQ_OFFSET__LOG) {
499,066✔
353
      tlen += taosEncodeFixedI64(buf, offRows->offset.version);
910,684✔
354
    } else {
355
      // do nothing
356
    }
357
    tlen += taosEncodeFixedI64(buf, offRows->ever);
1,035,052✔
358
  }
359

360
  return tlen;
400,618✔
361
}
362

363
int32_t tEncodeSMqConsumerEp(void **buf, const SMqConsumerEp *pConsumerEp) {
144,276✔
364
  int32_t tlen = 0;
144,276✔
365
  tlen += taosEncodeFixedI64(buf, pConsumerEp->consumerId);
144,276✔
366
  int32_t sz = taosArrayGetSize(pConsumerEp->vgs);
144,276✔
367
  tlen += taosEncodeFixedI32(buf, sz);
144,276✔
368
  for (int32_t i = 0; i < sz; i++) {
478,006✔
369
    void* data = taosArrayGet(pConsumerEp->vgs, i);
333,730✔
370
    tlen += tEncodeSMqVgEp(buf, data);
333,730✔
371
  }
372

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

376
void *tDecodeOffRows(const void *buf, SArray **offsetRows, int8_t sver) {
151,644✔
377
  int32_t szVgs = 0;
151,644!
378
  buf = taosDecodeFixedI32(buf, &szVgs);
151,644✔
379
  if (szVgs > 0) {
151,644✔
380
    *offsetRows = taosArrayInit(szVgs, sizeof(OffsetRows));
58,728✔
381
    if (NULL == *offsetRows) return NULL;
58,728!
382
    for (int32_t j = 0; j < szVgs; ++j) {
288,671✔
383
      OffsetRows *offRows = taosArrayReserve(*offsetRows, 1);
229,943✔
384
      buf = taosDecodeFixedI32(buf, &offRows->vgId);
229,943!
385
      buf = taosDecodeFixedI64(buf, &offRows->rows);
229,943!
386
      buf = taosDecodeFixedI8(buf, &offRows->offset.type);
229,943✔
387
      if (offRows->offset.type == TMQ_OFFSET__SNAPSHOT_DATA || offRows->offset.type == TMQ_OFFSET__SNAPSHOT_META) {
229,943!
388
        buf = taosDecodeFixedI64(buf, &offRows->offset.uid);
8,880!
389
        buf = taosDecodeFixedI64(buf, &offRows->offset.ts);
17,760!
390
      } else if (offRows->offset.type == TMQ_OFFSET__LOG) {
221,063✔
391
        buf = taosDecodeFixedI64(buf, &offRows->offset.version);
404,554!
392
      } else {
393
        // do nothing
394
      }
395
      if (sver > 2) {
229,943!
396
        buf = taosDecodeFixedI64(buf, &offRows->ever);
459,886!
397
      }
398
    }
399
  }
400
  return (void *)buf;
151,644✔
401
}
402

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

420
  return (void *)buf;
50,436✔
421
}
422

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

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

439
END:
40,025✔
440
  return code;
40,025✔
441
}
442

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

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

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

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

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

499
END:
40,976✔
500
  return code;
40,976✔
501
}
502

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

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

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

527
  int32_t cnt = 0;
256,342✔
528
  while (1) {
144,276✔
529
    pIter = taosHashIterate(pSub->consumerHash, pIter);
400,618✔
530
    if (pIter == NULL) break;
400,618✔
531
    SMqConsumerEp *pConsumerEp = (SMqConsumerEp *)pIter;
144,276✔
532
    tlen += tEncodeSMqConsumerEp(buf, pConsumerEp);
144,276✔
533
    cnt++;
144,276✔
534
  }
535
  if (cnt != sz) return -1;
256,342!
536
  int32_t len = taosArrayGetSize(pSub->unassignedVgs);
256,342✔
537
  tlen += taosEncodeFixedI32(buf, len);
256,342✔
538
  for (int32_t i = 0; i < len; i++) {
709,152✔
539
    void* data = taosArrayGet(pSub->unassignedVgs, i);
452,810✔
540
    tlen += tEncodeSMqVgEp(buf, data);
452,810✔
541
  }
542
  tlen += taosEncodeString(buf, pSub->dbName);
256,342!
543

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

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

558
  int32_t sz;
101,136✔
559
  buf = taosDecodeFixedI32(buf, &sz);
101,208✔
560

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

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

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

587
  buf = taosDecodeStringTo(buf, pSub->dbName);
101,208✔
588

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

598
int32_t mndInitConfigObj(SConfigItem *pItem, SConfigObj *pObj) {
39,602,112✔
599
  tstrncpy(pObj->name, pItem->name, CFG_NAME_MAX_LEN);
39,602,112!
600
  pObj->dtype = pItem->dtype;
39,602,112✔
601
  switch (pItem->dtype) {
39,602,112!
602
    case CFG_DTYPE_NONE:
×
603
      break;
×
604
    case CFG_DTYPE_BOOL:
7,376,864✔
605
      pObj->bval = pItem->bval;
7,376,864!
606
      break;
7,376,864✔
607
    case CFG_DTYPE_INT32:
22,130,592✔
608
      pObj->i32 = pItem->i32;
22,130,592✔
609
      break;
22,130,592✔
610
    case CFG_DTYPE_INT64:
2,329,536✔
611
      pObj->i64 = pItem->i64;
2,329,536✔
612
      break;
2,329,536✔
613
    case CFG_DTYPE_FLOAT:
776,512✔
614
    case CFG_DTYPE_DOUBLE:
615
      pObj->fval = pItem->fval;
776,512✔
616
      break;
776,512✔
617
    case CFG_DTYPE_STRING:
6,988,608✔
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);
6,988,608!
623
      if (pObj->str == NULL) {
6,988,608!
624
        taosMemoryFree(pObj);
×
625
        return TSDB_CODE_OUT_OF_MEMORY;
×
626
      }
627
      break;
6,988,608✔
628
  }
629
  return TSDB_CODE_SUCCESS;
39,602,112✔
630
}
631

632
int32_t mndUpdateObj(SConfigObj *pObjNew, const char *name, char *value) {
10,891✔
633
  int32_t code = 0;
10,891✔
634
  switch (pObjNew->dtype) {
10,891!
635
    case CFG_DTYPE_BOOL: {
3,298✔
636
      bool tmp = false;
3,298✔
637
      if (strcasecmp(value, "true") == 0) {
3,298!
638
        tmp = true;
×
639
      }
640
      if (taosStr2Int32(value, NULL, 10) > 0) {
3,298✔
641
        tmp = true;
1,423✔
642
      }
643
      pObjNew->bval = tmp;
3,298✔
644
      break;
3,298✔
645
    }
646
    case CFG_DTYPE_INT32: {
6,447✔
647
      int32_t ival;
6,447✔
648
      TAOS_CHECK_RETURN(taosStrHumanToInt32(value, &ival));
6,447!
649
      pObjNew->i32 = ival;
6,447✔
650
      break;
6,447✔
651
    }
652
    case CFG_DTYPE_INT64: {
675✔
653
      int64_t ival;
675✔
654
      TAOS_CHECK_RETURN(taosStrHumanToInt64(value, &ival));
675!
655
      pObjNew->i64 = ival;
675✔
656
      break;
675✔
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:
471✔
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);
471!
671
      if (pObjNew->str == NULL) {
471!
672
        code = terrno;
×
673
        return code;
×
674
      }
675
      break;
471✔
676
    }
677
    case CFG_DTYPE_NONE:
×
678
      break;
×
679
    default:
×
680
      code = TSDB_CODE_INVALID_CFG;
×
681
      break;
×
682
  }
683
  return code;
10,891✔
684
}
685

686
SConfigObj mndInitConfigVersion() {
388,328✔
687
  SConfigObj obj;
387,613✔
688
  memset(&obj, 0, sizeof(SConfigObj));
388,328!
689

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

696
int32_t tEncodeSConfigObj(SEncoder *pEncoder, const SConfigObj *pObj) {
282,649,610✔
697
  TAOS_CHECK_RETURN(tStartEncode(pEncoder));
282,649,610!
698
  TAOS_CHECK_RETURN(tEncodeCStr(pEncoder, pObj->name));
565,299,220!
699

700
  TAOS_CHECK_RETURN(tEncodeI32(pEncoder, pObj->dtype));
565,299,220!
701
  switch (pObj->dtype) {
282,649,610!
702
    case CFG_DTYPE_BOOL:
52,137,732✔
703
      TAOS_CHECK_RETURN(tEncodeI8(pEncoder, pObj->bval));
104,275,464!
704
      break;
52,137,732✔
705
    case CFG_DTYPE_INT32:
159,172,242✔
706
      TAOS_CHECK_RETURN(tEncodeI32(pEncoder, pObj->i32));
318,344,484!
707
      break;
159,172,242✔
708
    case CFG_DTYPE_INT64:
16,463,814✔
709
      TAOS_CHECK_RETURN(tEncodeI64(pEncoder, pObj->i64));
32,927,628!
710
      break;
16,463,814✔
711
    case CFG_DTYPE_FLOAT:
5,487,488✔
712
    case CFG_DTYPE_DOUBLE:
713
      TAOS_CHECK_RETURN(tEncodeFloat(pEncoder, pObj->fval));
10,974,976!
714
      break;
5,487,488✔
715
    case CFG_DTYPE_STRING:
49,388,334✔
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) {
49,388,334!
721
        TAOS_CHECK_RETURN(tEncodeCStr(pEncoder, pObj->str));
98,776,668!
722
      } else {
723
        TAOS_CHECK_RETURN(tEncodeCStr(pEncoder, ""));
×
724
      }
725
      break;
49,388,334✔
726
    default:
×
727
      break;
×
728
  }
729
  tEndEncode(pEncoder);
282,649,610✔
730
  return pEncoder->pos;
282,649,610✔
731
}
732

733
int32_t tDecodeSConfigObj(SDecoder *pDecoder, SConfigObj *pObj) {
56,869,419✔
734
  TAOS_CHECK_RETURN(tStartDecode(pDecoder));
56,869,419!
735
  TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, pObj->name));
56,869,419!
736
  TAOS_CHECK_RETURN(tDecodeI32(pDecoder, (int32_t *)&pObj->dtype));
113,738,838!
737
  switch (pObj->dtype) {
56,869,419!
738
    case CFG_DTYPE_NONE:
×
739
      break;
×
740
    case CFG_DTYPE_BOOL:
10,471,673✔
741
      TAOS_CHECK_RETURN(tDecodeBool(pDecoder, &pObj->bval));
10,471,673!
742
      break;
10,471,673✔
743
    case CFG_DTYPE_INT32:
32,085,710✔
744
      TAOS_CHECK_RETURN(tDecodeI32(pDecoder, &pObj->i32));
64,171,420!
745
      break;
32,085,710✔
746
    case CFG_DTYPE_INT64:
3,308,115✔
747
      TAOS_CHECK_RETURN(tDecodeI64(pDecoder, &pObj->i64));
6,616,230!
748
      break;
3,308,115✔
749
    case CFG_DTYPE_FLOAT:
1,103,090✔
750
    case CFG_DTYPE_DOUBLE:
751
      TAOS_CHECK_RETURN(tDecodeFloat(pDecoder, &pObj->fval));
2,206,180!
752
      break;
1,103,090✔
753
    case CFG_DTYPE_STRING:
9,900,831✔
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));
19,801,662!
759
      break;
9,900,831✔
760
  }
761
  tEndDecode(pDecoder);
56,869,419✔
762
  TAOS_RETURN(TSDB_CODE_SUCCESS);
56,869,419✔
763
}
764

765
void tFreeSConfigObj(SConfigObj *obj) {
96,861,350✔
766
  if (obj == NULL) {
96,861,350!
767
    return;
×
768
  }
769
  if (obj->dtype == CFG_DTYPE_STRING || obj->dtype == CFG_DTYPE_DIR || obj->dtype == CFG_DTYPE_LOCALE ||
96,861,350!
770
      obj->dtype == CFG_DTYPE_CHARSET || obj->dtype == CFG_DTYPE_TIMEZONE) {
81,851,476✔
771
    taosMemoryFree(obj->str);
16,886,364!
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