clientTmq.c 62.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

L
Liu Jicong 已提交
16
#include "cJSON.h"
17 18 19
#include "clientInt.h"
#include "clientLog.h"
#include "parser.h"
H
Haojun Liao 已提交
20
#include "tdatablock.h"
21 22 23
#include "tdef.h"
#include "tglobal.h"
#include "tmsgtype.h"
X
Xiaoyu Wang 已提交
24
#include "tqueue.h"
25
#include "tref.h"
L
Liu Jicong 已提交
26 27
#include "ttimer.h"

L
Liu Jicong 已提交
28 29 30 31 32 33 34
#if 0
#undef tsem_post
#define tsem_post(x)                                         \
  tscInfo("call sem post at %s %d", __FUNCTION__, __LINE__); \
  sem_post(x)
#endif

X
Xiaoyu Wang 已提交
35
struct SMqMgmt {
36 37 38
  int8_t  inited;
  tmr_h   timer;
  int32_t rsetId;
39
};
L
Liu Jicong 已提交
40

X
Xiaoyu Wang 已提交
41 42
static TdThreadOnce   tmqInit = PTHREAD_ONCE_INIT;  // initialize only once
volatile int32_t      tmqInitRes = 0;               // initialize rsp code
43
static struct SMqMgmt tmqMgmt = {0};
44

L
Liu Jicong 已提交
45 46 47 48 49 50
typedef struct {
  int8_t  tmqRspType;
  int32_t epoch;
} SMqRspWrapper;

typedef struct {
L
Liu Jicong 已提交
51 52 53
  int8_t      tmqRspType;
  int32_t     epoch;
  SMqAskEpRsp msg;
L
Liu Jicong 已提交
54 55
} SMqAskEpRspWrapper;

L
Liu Jicong 已提交
56
struct tmq_list_t {
L
Liu Jicong 已提交
57
  SArray container;
L
Liu Jicong 已提交
58
};
L
Liu Jicong 已提交
59

L
Liu Jicong 已提交
60
struct tmq_conf_t {
61 62 63 64 65 66 67 68
  char           clientId[256];
  char           groupId[TSDB_CGROUP_LEN];
  int8_t         autoCommit;
  int8_t         resetOffset;
  int8_t         withTbName;
  int8_t         snapEnable;
  int32_t        snapBatchSize;
  bool           hbBgEnable;
69 70 71 72 73
  uint16_t       port;
  int32_t        autoCommitInterval;
  char*          ip;
  char*          user;
  char*          pass;
74
  tmq_commit_cb* commitCb;
L
Liu Jicong 已提交
75
  void*          commitCbUserParam;
L
Liu Jicong 已提交
76 77 78
};

struct tmq_t {
79
  int64_t refId;
L
Liu Jicong 已提交
80
  // conf
81 82 83 84 85 86 87 88 89
  char     groupId[TSDB_CGROUP_LEN];
  char     clientId[256];
  int8_t   withTbName;
  int8_t   useSnapshot;
  int8_t   autoCommit;
  int32_t  autoCommitInterval;
  int32_t  resetOffsetCfg;
  uint64_t consumerId;
  bool     hbBgEnable;
90

L
Liu Jicong 已提交
91 92
  tmq_commit_cb* commitCb;
  void*          commitCbUserParam;
L
Liu Jicong 已提交
93 94 95 96

  // status
  int8_t  status;
  int32_t epoch;
L
Liu Jicong 已提交
97 98
#if 0
  int8_t  epStatus;
L
Liu Jicong 已提交
99
  int32_t epSkipCnt;
L
Liu Jicong 已提交
100
#endif
L
Liu Jicong 已提交
101 102
  int64_t pollCnt;

L
Liu Jicong 已提交
103
  // timer
104 105
  tmr_h hbLiveTimer;
  tmr_h epTimer;
L
Liu Jicong 已提交
106 107 108
  tmr_h reportTimer;
  tmr_h commitTimer;

L
Liu Jicong 已提交
109 110 111 112
  // connection
  STscObj* pTscObj;

  // container
L
Liu Jicong 已提交
113
  SArray*     clientTopics;  // SArray<SMqClientTopic>
L
Liu Jicong 已提交
114
  STaosQueue* mqueue;        // queue of rsp
L
Liu Jicong 已提交
115
  STaosQall*  qall;
L
Liu Jicong 已提交
116 117 118 119
  STaosQueue* delayedTask;  // delayed task queue for heartbeat and auto commit

  // ctl
  tsem_t rspSem;
L
Liu Jicong 已提交
120 121
};

X
Xiaoyu Wang 已提交
122 123 124 125 126 127 128 129
enum {
  TMQ_VG_STATUS__IDLE = 0,
  TMQ_VG_STATUS__WAIT,
};

enum {
  TMQ_CONSUMER_STATUS__INIT = 0,
  TMQ_CONSUMER_STATUS__READY,
130
  TMQ_CONSUMER_STATUS__NO_TOPIC,
L
Liu Jicong 已提交
131
  TMQ_CONSUMER_STATUS__RECOVER,
L
Liu Jicong 已提交
132 133
};

L
Liu Jicong 已提交
134
enum {
135
  TMQ_DELAYED_TASK__ASK_EP = 1,
L
Liu Jicong 已提交
136 137 138 139
  TMQ_DELAYED_TASK__REPORT,
  TMQ_DELAYED_TASK__COMMIT,
};

L
Liu Jicong 已提交
140
typedef struct {
141 142 143
  // statistics
  int64_t pollCnt;
  // offset
L
Liu Jicong 已提交
144 145
  STqOffsetVal committedOffset;
  STqOffsetVal currentOffset;
L
Liu Jicong 已提交
146
  // connection info
147
  int32_t vgId;
X
Xiaoyu Wang 已提交
148
  int32_t vgStatus;
L
Liu Jicong 已提交
149
  int32_t vgSkipCnt;
150 151 152
  SEpSet  epSet;
} SMqClientVg;

L
Liu Jicong 已提交
153
typedef struct {
154
  // subscribe info
155 156 157
  char           topicName[TSDB_TOPIC_FNAME_LEN];
  char           db[TSDB_DB_FNAME_LEN];
  SArray*        vgs;  // SArray<SMqClientVg>
L
Liu Jicong 已提交
158
  SSchemaWrapper schema;
159 160
} SMqClientTopic;

L
Liu Jicong 已提交
161 162 163 164 165
typedef struct {
  int8_t          tmqRspType;
  int32_t         epoch;
  SMqClientVg*    vgHandle;
  SMqClientTopic* topicHandle;
L
Liu Jicong 已提交
166
  union {
L
Liu Jicong 已提交
167 168
    SMqDataRsp dataRsp;
    SMqMetaRsp metaRsp;
L
Liu Jicong 已提交
169
    STaosxRsp  taosxRsp;
L
Liu Jicong 已提交
170
  };
L
Liu Jicong 已提交
171 172
} SMqPollRspWrapper;

L
Liu Jicong 已提交
173
typedef struct {
174 175
  int64_t refId;
  int32_t epoch;
L
Liu Jicong 已提交
176 177
  tsem_t  rspSem;
  int32_t rspErr;
L
Liu Jicong 已提交
178
} SMqSubscribeCbParam;
L
Liu Jicong 已提交
179

L
Liu Jicong 已提交
180
typedef struct {
181 182
  int64_t refId;
  int32_t epoch;
L
Liu Jicong 已提交
183
  int32_t code;
L
Liu Jicong 已提交
184
  int32_t async;
X
Xiaoyu Wang 已提交
185
  tsem_t  rspSem;
186 187
} SMqAskEpCbParam;

L
Liu Jicong 已提交
188
typedef struct {
189 190
  int64_t         refId;
  int32_t         epoch;
L
Liu Jicong 已提交
191
  SMqClientVg*    pVg;
L
Liu Jicong 已提交
192
  SMqClientTopic* pTopic;
L
Liu Jicong 已提交
193
  int32_t         vgId;
L
Liu Jicong 已提交
194
  tsem_t          rspSem;
H
Haojun Liao 已提交
195
  uint64_t        requestId; // request id for debug purpose
X
Xiaoyu Wang 已提交
196
} SMqPollCbParam;
197

198
typedef struct {
199 200
  int64_t        refId;
  int32_t        epoch;
L
Liu Jicong 已提交
201 202
  int8_t         automatic;
  int8_t         async;
L
Liu Jicong 已提交
203 204
  int32_t        waitingRspNum;
  int32_t        totalRspNum;
L
Liu Jicong 已提交
205
  int32_t        rspErr;
206
  tmq_commit_cb* userCb;
L
Liu Jicong 已提交
207 208 209 210
  /*SArray*        successfulOffsets;*/
  /*SArray*        failedOffsets;*/
  void*  userParam;
  tsem_t rspSem;
211 212 213 214 215
} SMqCommitCbParamSet;

typedef struct {
  SMqCommitCbParamSet* params;
  STqOffset*           pOffset;
216
  SMqClientVg*         pMqVg;
L
Liu Jicong 已提交
217 218
  /*char                 topicName[TSDB_TOPIC_FNAME_LEN];*/
  /*int32_t              vgId;*/
219
} SMqCommitCbParam;
220

221 222
static int32_t tmqAskEp(tmq_t* tmq, bool async);

223
tmq_conf_t* tmq_conf_new() {
wafwerar's avatar
wafwerar 已提交
224
  tmq_conf_t* conf = taosMemoryCalloc(1, sizeof(tmq_conf_t));
225 226 227 228 229
  if (conf == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return conf;
  }

230
  conf->withTbName = false;
L
Liu Jicong 已提交
231
  conf->autoCommit = true;
L
Liu Jicong 已提交
232
  conf->autoCommitInterval = 5000;
X
Xiaoyu Wang 已提交
233
  conf->resetOffset = TMQ_CONF__RESET_OFFSET__EARLIEAST;
234
  conf->hbBgEnable = true;
235

236 237 238
  return conf;
}

L
Liu Jicong 已提交
239
void tmq_conf_destroy(tmq_conf_t* conf) {
L
Liu Jicong 已提交
240 241 242 243 244 245
  if (conf) {
    if (conf->ip) taosMemoryFree(conf->ip);
    if (conf->user) taosMemoryFree(conf->user);
    if (conf->pass) taosMemoryFree(conf->pass);
    taosMemoryFree(conf);
  }
L
Liu Jicong 已提交
246 247 248
}

tmq_conf_res_t tmq_conf_set(tmq_conf_t* conf, const char* key, const char* value) {
249
  if (strcmp(key, "group.id") == 0) {
L
Liu Jicong 已提交
250
    tstrncpy(conf->groupId, value, TSDB_CGROUP_LEN);
L
Liu Jicong 已提交
251
    return TMQ_CONF_OK;
252
  }
L
Liu Jicong 已提交
253

254
  if (strcmp(key, "client.id") == 0) {
L
Liu Jicong 已提交
255
    tstrncpy(conf->clientId, value, 256);
L
Liu Jicong 已提交
256 257
    return TMQ_CONF_OK;
  }
L
Liu Jicong 已提交
258

L
Liu Jicong 已提交
259 260
  if (strcmp(key, "enable.auto.commit") == 0) {
    if (strcmp(value, "true") == 0) {
L
Liu Jicong 已提交
261
      conf->autoCommit = true;
L
Liu Jicong 已提交
262 263
      return TMQ_CONF_OK;
    } else if (strcmp(value, "false") == 0) {
L
Liu Jicong 已提交
264
      conf->autoCommit = false;
L
Liu Jicong 已提交
265 266 267 268
      return TMQ_CONF_OK;
    } else {
      return TMQ_CONF_INVALID;
    }
269
  }
L
Liu Jicong 已提交
270

L
Liu Jicong 已提交
271 272 273 274 275
  if (strcmp(key, "auto.commit.interval.ms") == 0) {
    conf->autoCommitInterval = atoi(value);
    return TMQ_CONF_OK;
  }

L
Liu Jicong 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289
  if (strcmp(key, "auto.offset.reset") == 0) {
    if (strcmp(value, "none") == 0) {
      conf->resetOffset = TMQ_CONF__RESET_OFFSET__NONE;
      return TMQ_CONF_OK;
    } else if (strcmp(value, "earliest") == 0) {
      conf->resetOffset = TMQ_CONF__RESET_OFFSET__EARLIEAST;
      return TMQ_CONF_OK;
    } else if (strcmp(value, "latest") == 0) {
      conf->resetOffset = TMQ_CONF__RESET_OFFSET__LATEST;
      return TMQ_CONF_OK;
    } else {
      return TMQ_CONF_INVALID;
    }
  }
L
Liu Jicong 已提交
290

291 292
  if (strcmp(key, "msg.with.table.name") == 0) {
    if (strcmp(value, "true") == 0) {
293
      conf->withTbName = true;
L
Liu Jicong 已提交
294
      return TMQ_CONF_OK;
295
    } else if (strcmp(value, "false") == 0) {
296
      conf->withTbName = false;
L
Liu Jicong 已提交
297
      return TMQ_CONF_OK;
298 299 300 301 302
    } else {
      return TMQ_CONF_INVALID;
    }
  }

L
Liu Jicong 已提交
303
  if (strcmp(key, "experimental.snapshot.enable") == 0) {
L
Liu Jicong 已提交
304
    if (strcmp(value, "true") == 0) {
L
Liu Jicong 已提交
305
      conf->snapEnable = true;
306 307
      return TMQ_CONF_OK;
    } else if (strcmp(value, "false") == 0) {
L
Liu Jicong 已提交
308
      conf->snapEnable = false;
309 310 311 312 313 314
      return TMQ_CONF_OK;
    } else {
      return TMQ_CONF_INVALID;
    }
  }

L
Liu Jicong 已提交
315 316 317 318 319
  if (strcmp(key, "experimental.snapshot.batch.size") == 0) {
    conf->snapBatchSize = atoi(value);
    return TMQ_CONF_OK;
  }

320 321 322
  if (strcmp(key, "enable.heartbeat.background") == 0) {
    if (strcmp(value, "true") == 0) {
      conf->hbBgEnable = true;
L
Liu Jicong 已提交
323 324
      return TMQ_CONF_OK;
    } else if (strcmp(value, "false") == 0) {
325
      conf->hbBgEnable = false;
L
Liu Jicong 已提交
326 327 328 329
      return TMQ_CONF_OK;
    } else {
      return TMQ_CONF_INVALID;
    }
330
    return TMQ_CONF_OK;
L
Liu Jicong 已提交
331 332
  }

L
Liu Jicong 已提交
333
  if (strcmp(key, "td.connect.ip") == 0) {
334
    conf->ip = taosStrdup(value);
L
Liu Jicong 已提交
335 336
    return TMQ_CONF_OK;
  }
L
Liu Jicong 已提交
337
  if (strcmp(key, "td.connect.user") == 0) {
338
    conf->user = taosStrdup(value);
L
Liu Jicong 已提交
339 340
    return TMQ_CONF_OK;
  }
L
Liu Jicong 已提交
341
  if (strcmp(key, "td.connect.pass") == 0) {
342
    conf->pass = taosStrdup(value);
L
Liu Jicong 已提交
343 344
    return TMQ_CONF_OK;
  }
L
Liu Jicong 已提交
345
  if (strcmp(key, "td.connect.port") == 0) {
L
Liu Jicong 已提交
346 347 348
    conf->port = atoi(value);
    return TMQ_CONF_OK;
  }
L
Liu Jicong 已提交
349
  if (strcmp(key, "td.connect.db") == 0) {
350
    /*conf->db = taosStrdup(value);*/
L
Liu Jicong 已提交
351 352 353
    return TMQ_CONF_OK;
  }

L
Liu Jicong 已提交
354
  return TMQ_CONF_UNKNOWN;
355 356 357
}

tmq_list_t* tmq_list_new() {
L
Liu Jicong 已提交
358 359
  //
  return (tmq_list_t*)taosArrayInit(0, sizeof(void*));
360 361
}

L
Liu Jicong 已提交
362 363
int32_t tmq_list_append(tmq_list_t* list, const char* src) {
  SArray* container = &list->container;
364
  if (src == NULL || src[0] == 0) return -1;
365
  char* topic = taosStrdup(src);
366 367 368
  if (topic[0] != '`') {
    strtolower(topic, src);
  }
L
fix  
Liu Jicong 已提交
369
  if (taosArrayPush(container, &topic) == NULL) return -1;
370 371 372
  return 0;
}

L
Liu Jicong 已提交
373
void tmq_list_destroy(tmq_list_t* list) {
L
Liu Jicong 已提交
374
  SArray* container = &list->container;
L
Liu Jicong 已提交
375
  taosArrayDestroyP(container, taosMemoryFree);
L
Liu Jicong 已提交
376 377
}

L
Liu Jicong 已提交
378 379 380 381 382 383 384 385 386 387
int32_t tmq_list_get_size(const tmq_list_t* list) {
  const SArray* container = &list->container;
  return taosArrayGetSize(container);
}

char** tmq_list_to_c_array(const tmq_list_t* list) {
  const SArray* container = &list->container;
  return container->pData;
}

L
Liu Jicong 已提交
388 389 390 391
static int32_t tmqMakeTopicVgKey(char* dst, const char* topicName, int32_t vg) {
  return sprintf(dst, "%s:%d", topicName, vg);
}

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
int32_t tmqCommitDone(SMqCommitCbParamSet* pParamSet) {
  tmq_t* tmq = taosAcquireRef(tmqMgmt.rsetId, pParamSet->refId);
  if (tmq == NULL) {
    if (!pParamSet->async) {
      tsem_destroy(&pParamSet->rspSem);
    }
    taosMemoryFree(pParamSet);
    terrno = TSDB_CODE_TMQ_CONSUMER_CLOSED;
    return -1;
  }

  // if no more waiting rsp
  if (pParamSet->async) {
    // call async cb func
    if (pParamSet->automatic && tmq->commitCb) {
      tmq->commitCb(tmq, pParamSet->rspErr, tmq->commitCbUserParam);
    } else if (!pParamSet->automatic && pParamSet->userCb) {
      // sem post
      pParamSet->userCb(tmq, pParamSet->rspErr, pParamSet->userParam);
    }
    taosMemoryFree(pParamSet);
  } else {
    tsem_post(&pParamSet->rspSem);
  }

#if 0
    taosArrayDestroyP(pParamSet->successfulOffsets, taosMemoryFree);
    taosArrayDestroyP(pParamSet->failedOffsets, taosMemoryFree);
#endif
  return 0;
}

L
Liu Jicong 已提交
424 425 426 427 428 429 430
static void tmqCommitRspCountDown(SMqCommitCbParamSet* pParamSet) {
  int32_t waitingRspNum = atomic_sub_fetch_32(&pParamSet->waitingRspNum, 1);
  if (waitingRspNum == 0) {
    tmqCommitDone(pParamSet);
  }
}

431 432
int32_t tmqCommitCb(void* param, SDataBuf* pBuf, int32_t code) {
  SMqCommitCbParam*    pParam = (SMqCommitCbParam*)param;
433 434
  SMqCommitCbParamSet* pParamSet = (SMqCommitCbParamSet*)pParam->params;
  // push into array
L
Liu Jicong 已提交
435
#if 0
436 437 438 439 440
  if (code == 0) {
    taosArrayPush(pParamSet->failedOffsets, &pParam->pOffset);
  } else {
    taosArrayPush(pParamSet->successfulOffsets, &pParam->pOffset);
  }
L
Liu Jicong 已提交
441
#endif
L
Liu Jicong 已提交
442

443 444 445 446 447 448 449 450 451 452 453
  // there may be race condition. fix it
  if (pBuf->pEpSet != NULL && pParam->pMqVg != NULL) {
    SMqClientVg* pMqVg = pParam->pMqVg;

    SEp* pEp = GET_ACTIVE_EP(pBuf->pEpSet);
    SEp* pOld = GET_ACTIVE_EP(&(pMqVg->epSet));
    uDebug("subKey:%s update the epset vgId:%d, ep:%s:%d, old ep:%s:%d", pParam->pOffset->subKey, pMqVg->vgId,
           pEp->fqdn, pEp->port, pOld->fqdn, pOld->port);
    pParam->pMqVg->epSet = *pBuf->pEpSet;
  }

L
Liu Jicong 已提交
454
  taosMemoryFree(pParam->pOffset);
L
Liu Jicong 已提交
455
  taosMemoryFree(pBuf->pData);
dengyihao's avatar
dengyihao 已提交
456
  taosMemoryFree(pBuf->pEpSet);
L
Liu Jicong 已提交
457

S
Shengliang Guan 已提交
458
  /*tscDebug("receive offset commit cb of %s on vgId:%d, offset is %" PRId64, pParam->pOffset->subKey, pParam->->vgId,
L
Liu Jicong 已提交
459 460
   * pOffset->version);*/

L
Liu Jicong 已提交
461
  tmqCommitRspCountDown(pParamSet);
462 463 464
  return 0;
}

L
Liu Jicong 已提交
465 466 467 468 469 470
static int32_t tmqSendCommitReq(tmq_t* tmq, SMqClientVg* pVg, SMqClientTopic* pTopic, SMqCommitCbParamSet* pParamSet) {
  STqOffset* pOffset = taosMemoryCalloc(1, sizeof(STqOffset));
  if (pOffset == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
471

L
Liu Jicong 已提交
472
  pOffset->val = pVg->currentOffset;
473

L
Liu Jicong 已提交
474 475 476 477
  int32_t groupLen = strlen(tmq->groupId);
  memcpy(pOffset->subKey, tmq->groupId, groupLen);
  pOffset->subKey[groupLen] = TMQ_SEPARATOR;
  strcpy(pOffset->subKey + groupLen + 1, pTopic->topicName);
L
Liu Jicong 已提交
478

L
Liu Jicong 已提交
479 480 481 482 483 484
  int32_t len;
  int32_t code;
  tEncodeSize(tEncodeSTqOffset, pOffset, len, code);
  if (code < 0) {
    return -1;
  }
485

L
Liu Jicong 已提交
486
  void* buf = taosMemoryCalloc(1, sizeof(SMsgHead) + len);
L
Liu Jicong 已提交
487 488 489 490
  if (buf == NULL) {
    taosMemoryFree(pOffset);
    return -1;
  }
491

L
Liu Jicong 已提交
492
  ((SMsgHead*)buf)->vgId = htonl(pVg->vgId);
L
Liu Jicong 已提交
493

L
Liu Jicong 已提交
494 495 496 497 498
  void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));

  SEncoder encoder;
  tEncoderInit(&encoder, abuf, len);
  tEncodeSTqOffset(&encoder, pOffset);
L
Liu Jicong 已提交
499
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
500 501

  // build param
502
  SMqCommitCbParam* pParam = taosMemoryCalloc(1, sizeof(SMqCommitCbParam));
L
Liu Jicong 已提交
503
  if (pParam == NULL) {
L
Liu Jicong 已提交
504
    taosMemoryFree(pOffset);
L
Liu Jicong 已提交
505 506 507
    taosMemoryFree(buf);
    return -1;
  }
508

L
Liu Jicong 已提交
509 510
  pParam->params = pParamSet;
  pParam->pOffset = pOffset;
511
  pParam->pMqVg = pVg;  // there may be an race condition
L
Liu Jicong 已提交
512 513 514 515

  // build send info
  SMsgSendInfo* pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
  if (pMsgSendInfo == NULL) {
L
Liu Jicong 已提交
516
    taosMemoryFree(pOffset);
L
Liu Jicong 已提交
517 518
    taosMemoryFree(buf);
    taosMemoryFree(pParam);
L
Liu Jicong 已提交
519 520
    return -1;
  }
521

L
Liu Jicong 已提交
522 523 524 525 526 527
  pMsgSendInfo->msgInfo = (SDataBuf){
      .pData = buf,
      .len = sizeof(SMsgHead) + len,
      .handle = NULL,
  };

528
  SEp* pEp = &pVg->epSet.eps[pVg->epSet.inUse];
X
Xiaoyu Wang 已提交
529 530
  tscDebug("consumer:0x%" PRIx64 " topic:%s on vgId:%d offset:%" PRId64 " prev:%" PRId64 ", ep:%s:%d", tmq->consumerId,
           pOffset->subKey, pVg->vgId, pOffset->val.version, pVg->committedOffset.version, pEp->fqdn, pEp->port);
L
Liu Jicong 已提交
531

532
  // TODO: put into cb, the commit offset should be move to the callback function
L
Liu Jicong 已提交
533
  pVg->committedOffset = pVg->currentOffset;
L
Liu Jicong 已提交
534 535 536 537

  pMsgSendInfo->requestId = generateRequestId();
  pMsgSendInfo->requestObjRefId = 0;
  pMsgSendInfo->param = pParam;
L
Liu Jicong 已提交
538
  pMsgSendInfo->paramFreeFp = taosMemoryFree;
539
  pMsgSendInfo->fp = tmqCommitCb;
L
Liu Jicong 已提交
540
  pMsgSendInfo->msgType = TDMT_VND_TMQ_COMMIT_OFFSET;
L
Liu Jicong 已提交
541 542
  // send msg

L
Liu Jicong 已提交
543 544 545
  atomic_add_fetch_32(&pParamSet->waitingRspNum, 1);
  atomic_add_fetch_32(&pParamSet->totalRspNum, 1);

L
Liu Jicong 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
  int64_t transporterId = 0;
  asyncSendMsgToServer(tmq->pTscObj->pAppInfo->pTransporter, &pVg->epSet, &transporterId, pMsgSendInfo);
  return 0;
}

int32_t tmqCommitMsgImpl(tmq_t* tmq, const TAOS_RES* msg, int8_t async, tmq_commit_cb* userCb, void* userParam) {
  char*   topic;
  int32_t vgId;
  if (TD_RES_TMQ(msg)) {
    SMqRspObj* pRspObj = (SMqRspObj*)msg;
    topic = pRspObj->topic;
    vgId = pRspObj->vgId;
  } else if (TD_RES_TMQ_META(msg)) {
    SMqMetaRspObj* pMetaRspObj = (SMqMetaRspObj*)msg;
    topic = pMetaRspObj->topic;
    vgId = pMetaRspObj->vgId;
L
Liu Jicong 已提交
562
  } else if (TD_RES_TMQ_METADATA(msg)) {
563 564 565
    SMqTaosxRspObj* pRspObj = (SMqTaosxRspObj*)msg;
    topic = pRspObj->topic;
    vgId = pRspObj->vgId;
L
Liu Jicong 已提交
566 567 568 569 570 571 572 573 574
  } else {
    return TSDB_CODE_TMQ_INVALID_MSG;
  }

  SMqCommitCbParamSet* pParamSet = taosMemoryCalloc(1, sizeof(SMqCommitCbParamSet));
  if (pParamSet == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
575 576
  pParamSet->refId = tmq->refId;
  pParamSet->epoch = tmq->epoch;
L
Liu Jicong 已提交
577 578 579 580 581 582
  pParamSet->automatic = 0;
  pParamSet->async = async;
  pParamSet->userCb = userCb;
  pParamSet->userParam = userParam;
  tsem_init(&pParamSet->rspSem, 0, 0);

L
Liu Jicong 已提交
583 584
  int32_t code = -1;

L
Liu Jicong 已提交
585 586 587 588 589 590 591
  for (int32_t i = 0; i < taosArrayGetSize(tmq->clientTopics); i++) {
    SMqClientTopic* pTopic = taosArrayGet(tmq->clientTopics, i);
    if (strcmp(pTopic->topicName, topic) != 0) continue;
    for (int32_t j = 0; j < taosArrayGetSize(pTopic->vgs); j++) {
      SMqClientVg* pVg = taosArrayGet(pTopic->vgs, j);
      if (pVg->vgId != vgId) continue;

L
Liu Jicong 已提交
592
      if (pVg->currentOffset.type > 0 && !tOffsetEqual(&pVg->currentOffset, &pVg->committedOffset)) {
L
Liu Jicong 已提交
593
        if (tmqSendCommitReq(tmq, pVg, pTopic, pParamSet) < 0) {
L
Liu Jicong 已提交
594 595
          tsem_destroy(&pParamSet->rspSem);
          taosMemoryFree(pParamSet);
L
Liu Jicong 已提交
596
          goto FAIL;
L
Liu Jicong 已提交
597
        }
L
Liu Jicong 已提交
598
        goto HANDLE_RSP;
L
Liu Jicong 已提交
599 600
      }
    }
L
Liu Jicong 已提交
601
  }
L
Liu Jicong 已提交
602

L
Liu Jicong 已提交
603 604 605 606
HANDLE_RSP:
  if (pParamSet->totalRspNum == 0) {
    tsem_destroy(&pParamSet->rspSem);
    taosMemoryFree(pParamSet);
L
Liu Jicong 已提交
607 608 609
    return 0;
  }

L
Liu Jicong 已提交
610 611 612 613
  if (!async) {
    tsem_wait(&pParamSet->rspSem);
    code = pParamSet->rspErr;
    tsem_destroy(&pParamSet->rspSem);
L
Liu Jicong 已提交
614
    taosMemoryFree(pParamSet);
L
Liu Jicong 已提交
615 616 617 618 619 620 621 622 623 624 625 626
    return code;
  } else {
    code = 0;
  }

FAIL:
  if (code != 0 && async) {
    userCb(tmq, code, userParam);
  }
  return 0;
}

L
Liu Jicong 已提交
627 628
static int32_t tmqCommitConsumerImpl(tmq_t* tmq, int8_t automatic, int8_t async, tmq_commit_cb* userCb,
                                     void* userParam) {
L
Liu Jicong 已提交
629 630
  int32_t code = -1;

631 632
  SMqCommitCbParamSet* pParamSet = taosMemoryCalloc(1, sizeof(SMqCommitCbParamSet));
  if (pParamSet == NULL) {
L
Liu Jicong 已提交
633 634 635 636 637 638 639 640
    code = TSDB_CODE_OUT_OF_MEMORY;
    if (async) {
      if (automatic) {
        tmq->commitCb(tmq, code, tmq->commitCbUserParam);
      } else {
        userCb(tmq, code, userParam);
      }
    }
641 642
    return -1;
  }
643 644 645 646

  pParamSet->refId = tmq->refId;
  pParamSet->epoch = tmq->epoch;

647 648 649 650 651 652
  pParamSet->automatic = automatic;
  pParamSet->async = async;
  pParamSet->userCb = userCb;
  pParamSet->userParam = userParam;
  tsem_init(&pParamSet->rspSem, 0, 0);

653 654 655
  // init as 1 to prevent concurrency issue
  pParamSet->waitingRspNum = 1;

656
  int32_t numOfTopics = taosArrayGetSize(tmq->clientTopics);
X
Xiaoyu Wang 已提交
657
  tscDebug("consumer:0x%" PRIx64 " start to commit offset for %d topics", tmq->consumerId, numOfTopics);
658 659

  for (int32_t i = 0; i < numOfTopics; i++) {
660
    SMqClientTopic* pTopic = taosArrayGet(tmq->clientTopics, i);
L
Liu Jicong 已提交
661

662 663
    int32_t numOfVgroups = taosArrayGetSize(pTopic->vgs);
    for (int32_t j = 0; j < numOfVgroups; j++) {
L
Liu Jicong 已提交
664
      SMqClientVg* pVg = taosArrayGet(pTopic->vgs, j);
L
Liu Jicong 已提交
665
      if (pVg->currentOffset.type > 0 && !tOffsetEqual(&pVg->currentOffset, &pVg->committedOffset)) {
L
Liu Jicong 已提交
666 667 668
        if (tmqSendCommitReq(tmq, pVg, pTopic, pParamSet) < 0) {
          continue;
        }
669 670 671
      } else {
        tscDebug("consumer:0x%" PRIx64 " topic:%s vgId:%d, not commit, current:%" PRId64 ", ordinal:%d/%d",
                 tmq->consumerId, pTopic->topicName, pVg->vgId, pVg->currentOffset.version, j + 1, numOfVgroups);
672 673 674 675
      }
    }
  }

L
Liu Jicong 已提交
676
  // no request is sent
L
Liu Jicong 已提交
677 678 679 680 681 682
  if (pParamSet->totalRspNum == 0) {
    tsem_destroy(&pParamSet->rspSem);
    taosMemoryFree(pParamSet);
    return 0;
  }

L
Liu Jicong 已提交
683 684
  // count down since waiting rsp num init as 1
  tmqCommitRspCountDown(pParamSet);
685

686 687 688 689
  if (!async) {
    tsem_wait(&pParamSet->rspSem);
    code = pParamSet->rspErr;
    tsem_destroy(&pParamSet->rspSem);
690
    taosMemoryFree(pParamSet);
L
Liu Jicong 已提交
691
#if 0
692 693
    taosArrayDestroyP(pParamSet->successfulOffsets, taosMemoryFree);
    taosArrayDestroyP(pParamSet->failedOffsets, taosMemoryFree);
L
Liu Jicong 已提交
694
#endif
L
Liu Jicong 已提交
695
  }
696

L
Liu Jicong 已提交
697 698 699 700 701 702 703 704 705 706
  return code;
}

int32_t tmqCommitInner(tmq_t* tmq, const TAOS_RES* msg, int8_t automatic, int8_t async, tmq_commit_cb* userCb,
                       void* userParam) {
  if (msg) {
    return tmqCommitMsgImpl(tmq, msg, async, userCb, userParam);
  } else {
    return tmqCommitConsumerImpl(tmq, automatic, async, userCb, userParam);
  }
707 708
}

709
void tmqAssignAskEpTask(void* param, void* tmrId) {
710 711 712
  int64_t refId = *(int64_t*)param;
  tmq_t*  tmq = taosAcquireRef(tmqMgmt.rsetId, refId);
  if (tmq != NULL) {
S
Shengliang Guan 已提交
713
    int8_t* pTaskType = taosAllocateQitem(sizeof(int8_t), DEF_QITEM, 0);
714 715 716 717 718
    *pTaskType = TMQ_DELAYED_TASK__ASK_EP;
    taosWriteQitem(tmq->delayedTask, pTaskType);
    tsem_post(&tmq->rspSem);
  }
  taosMemoryFree(param);
L
Liu Jicong 已提交
719 720 721
}

void tmqAssignDelayedCommitTask(void* param, void* tmrId) {
722 723 724
  int64_t refId = *(int64_t*)param;
  tmq_t*  tmq = taosAcquireRef(tmqMgmt.rsetId, refId);
  if (tmq != NULL) {
S
Shengliang Guan 已提交
725
    int8_t* pTaskType = taosAllocateQitem(sizeof(int8_t), DEF_QITEM, 0);
726 727 728 729 730
    *pTaskType = TMQ_DELAYED_TASK__COMMIT;
    taosWriteQitem(tmq->delayedTask, pTaskType);
    tsem_post(&tmq->rspSem);
  }
  taosMemoryFree(param);
L
Liu Jicong 已提交
731 732 733
}

void tmqAssignDelayedReportTask(void* param, void* tmrId) {
734 735 736
  int64_t refId = *(int64_t*)param;
  tmq_t*  tmq = taosAcquireRef(tmqMgmt.rsetId, refId);
  if (tmq != NULL) {
S
Shengliang Guan 已提交
737
    int8_t* pTaskType = taosAllocateQitem(sizeof(int8_t), DEF_QITEM, 0);
738 739 740 741 742
    *pTaskType = TMQ_DELAYED_TASK__REPORT;
    taosWriteQitem(tmq->delayedTask, pTaskType);
    tsem_post(&tmq->rspSem);
  }
  taosMemoryFree(param);
L
Liu Jicong 已提交
743 744
}

745
int32_t tmqHbCb(void* param, SDataBuf* pMsg, int32_t code) {
dengyihao's avatar
dengyihao 已提交
746 747 748 749
  if (pMsg) {
    taosMemoryFree(pMsg->pData);
    taosMemoryFree(pMsg->pEpSet);
  }
750 751 752 753
  return 0;
}

void tmqSendHbReq(void* param, void* tmrId) {
754 755 756
  int64_t refId = *(int64_t*)param;
  tmq_t*  tmq = taosAcquireRef(tmqMgmt.rsetId, refId);
  if (tmq == NULL) {
L
Liu Jicong 已提交
757
    taosMemoryFree(param);
758 759
    return;
  }
D
dapan1121 已提交
760 761 762 763 764

  SMqHbReq req = {0};
  req.consumerId = tmq->consumerId;
  req.epoch = tmq->epoch;

L
Liu Jicong 已提交
765
  int32_t tlen = tSerializeSMqHbReq(NULL, 0, &req);
D
dapan1121 已提交
766 767 768 769
  if (tlen < 0) {
    tscError("tSerializeSMqHbReq failed");
    return;
  }
L
Liu Jicong 已提交
770
  void* pReq = taosMemoryCalloc(1, tlen);
D
dapan1121 已提交
771 772 773 774 775 776 777 778 779
  if (tlen < 0) {
    tscError("failed to malloc MqHbReq msg, size:%d", tlen);
    return;
  }
  if (tSerializeSMqHbReq(pReq, tlen, &req) < 0) {
    tscError("tSerializeSMqHbReq %d failed", tlen);
    taosMemoryFree(pReq);
    return;
  }
780 781 782 783

  SMsgSendInfo* sendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
  if (sendInfo == NULL) {
    taosMemoryFree(pReq);
L
Liu Jicong 已提交
784
    goto OVER;
785 786 787
  }
  sendInfo->msgInfo = (SDataBuf){
      .pData = pReq,
D
dapan1121 已提交
788
      .len = tlen,
789 790 791 792 793 794 795
      .handle = NULL,
  };

  sendInfo->requestId = generateRequestId();
  sendInfo->requestObjRefId = 0;
  sendInfo->param = NULL;
  sendInfo->fp = tmqHbCb;
L
Liu Jicong 已提交
796
  sendInfo->msgType = TDMT_MND_TMQ_HB;
797 798 799 800 801 802 803

  SEpSet epSet = getEpSet_s(&tmq->pTscObj->pAppInfo->mgmtEp);

  int64_t transporterId = 0;
  asyncSendMsgToServer(tmq->pTscObj->pAppInfo->pTransporter, &epSet, &transporterId, sendInfo);

OVER:
804
  taosTmrReset(tmqSendHbReq, 1000, param, tmqMgmt.timer, &tmq->hbLiveTimer);
805 806
}

807
int32_t tmqHandleAllDelayedTask(tmq_t* pTmq) {
L
Liu Jicong 已提交
808
  STaosQall* qall = taosAllocateQall();
809
  taosReadAllQitems(pTmq->delayedTask, qall);
L
Liu Jicong 已提交
810

811 812 813 814
  if (qall->numOfItems == 0) {
    taosFreeQall(qall);
    return TSDB_CODE_SUCCESS;
  }
815

X
Xiaoyu Wang 已提交
816
  tscDebug("consumer:0x%" PRIx64 " handle delayed %d tasks before poll data", pTmq->consumerId, qall->numOfItems);
817 818
  int8_t* pTaskType = NULL;
  taosGetQitem(qall, (void**)&pTaskType);
L
Liu Jicong 已提交
819

820
  while (pTaskType != NULL) {
821
    if (*pTaskType == TMQ_DELAYED_TASK__ASK_EP) {
822
      tmqAskEp(pTmq, true);
823 824

      int64_t* pRefId = taosMemoryMalloc(sizeof(int64_t));
825
      *pRefId = pTmq->refId;
826

X
Xiaoyu Wang 已提交
827
      tscDebug("consumer:0x%" PRIx64 " retrieve ep from mnode in 1s", pTmq->consumerId);
828
      taosTmrReset(tmqAssignAskEpTask, 1000, pRefId, tmqMgmt.timer, &pTmq->epTimer);
L
Liu Jicong 已提交
829
    } else if (*pTaskType == TMQ_DELAYED_TASK__COMMIT) {
830
      tmqCommitInner(pTmq, NULL, 1, 1, pTmq->commitCb, pTmq->commitCbUserParam);
831 832

      int64_t* pRefId = taosMemoryMalloc(sizeof(int64_t));
833
      *pRefId = pTmq->refId;
834

X
Xiaoyu Wang 已提交
835
      tscDebug("consumer:0x%" PRIx64 " commit to vnode(s) in %.2fs", pTmq->consumerId,
X
Xiaoyu Wang 已提交
836
               pTmq->autoCommitInterval / 1000.0);
837
      taosTmrReset(tmqAssignDelayedCommitTask, pTmq->autoCommitInterval, pRefId, tmqMgmt.timer, &pTmq->commitTimer);
L
Liu Jicong 已提交
838 839
    } else if (*pTaskType == TMQ_DELAYED_TASK__REPORT) {
    }
840

L
Liu Jicong 已提交
841
    taosFreeQitem(pTaskType);
842
    taosGetQitem(qall, (void**)&pTaskType);
L
Liu Jicong 已提交
843
  }
844

L
Liu Jicong 已提交
845 846 847 848
  taosFreeQall(qall);
  return 0;
}

L
Liu Jicong 已提交
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
static void tmqFreeRspWrapper(SMqRspWrapper* rspWrapper) {
  if (rspWrapper->tmqRspType == TMQ_MSG_TYPE__END_RSP) {
    // do nothing
  } else if (rspWrapper->tmqRspType == TMQ_MSG_TYPE__EP_RSP) {
    SMqAskEpRspWrapper* pEpRspWrapper = (SMqAskEpRspWrapper*)rspWrapper;
    tDeleteSMqAskEpRsp(&pEpRspWrapper->msg);
  } else if (rspWrapper->tmqRspType == TMQ_MSG_TYPE__POLL_RSP) {
    SMqPollRspWrapper* pRsp = (SMqPollRspWrapper*)rspWrapper;
    taosArrayDestroyP(pRsp->dataRsp.blockData, taosMemoryFree);
    taosArrayDestroy(pRsp->dataRsp.blockDataLen);
    taosArrayDestroyP(pRsp->dataRsp.blockTbName, taosMemoryFree);
    taosArrayDestroyP(pRsp->dataRsp.blockSchema, (FDelete)tDeleteSSchemaWrapper);
  } else if (rspWrapper->tmqRspType == TMQ_MSG_TYPE__POLL_META_RSP) {
    SMqPollRspWrapper* pRsp = (SMqPollRspWrapper*)rspWrapper;
    taosMemoryFree(pRsp->metaRsp.metaRsp);
  } else if (rspWrapper->tmqRspType == TMQ_MSG_TYPE__TAOSX_RSP) {
    SMqPollRspWrapper* pRsp = (SMqPollRspWrapper*)rspWrapper;
    taosArrayDestroyP(pRsp->taosxRsp.blockData, taosMemoryFree);
    taosArrayDestroy(pRsp->taosxRsp.blockDataLen);
    taosArrayDestroyP(pRsp->taosxRsp.blockTbName, taosMemoryFree);
    taosArrayDestroyP(pRsp->taosxRsp.blockSchema, (FDelete)tDeleteSSchemaWrapper);
    // taosx
    taosArrayDestroy(pRsp->taosxRsp.createTableLen);
    taosArrayDestroyP(pRsp->taosxRsp.createTableReq, taosMemoryFree);
  }
}

L
Liu Jicong 已提交
876
void tmqClearUnhandleMsg(tmq_t* tmq) {
L
Liu Jicong 已提交
877
  SMqRspWrapper* rspWrapper = NULL;
L
Liu Jicong 已提交
878
  while (1) {
L
Liu Jicong 已提交
879 880 881 882 883
    taosGetQitem(tmq->qall, (void**)&rspWrapper);
    if (rspWrapper) {
      tmqFreeRspWrapper(rspWrapper);
      taosFreeQitem(rspWrapper);
    } else {
L
Liu Jicong 已提交
884
      break;
L
Liu Jicong 已提交
885
    }
L
Liu Jicong 已提交
886 887
  }

L
Liu Jicong 已提交
888
  rspWrapper = NULL;
L
Liu Jicong 已提交
889 890
  taosReadAllQitems(tmq->mqueue, tmq->qall);
  while (1) {
L
Liu Jicong 已提交
891 892 893 894 895
    taosGetQitem(tmq->qall, (void**)&rspWrapper);
    if (rspWrapper) {
      tmqFreeRspWrapper(rspWrapper);
      taosFreeQitem(rspWrapper);
    } else {
L
Liu Jicong 已提交
896
      break;
L
Liu Jicong 已提交
897
    }
L
Liu Jicong 已提交
898 899 900
  }
}

D
dapan1121 已提交
901
int32_t tmqSubscribeCb(void* param, SDataBuf* pMsg, int32_t code) {
L
Liu Jicong 已提交
902 903
  SMqSubscribeCbParam* pParam = (SMqSubscribeCbParam*)param;
  pParam->rspErr = code;
dengyihao's avatar
dengyihao 已提交
904 905

  taosMemoryFree(pMsg->pEpSet);
L
Liu Jicong 已提交
906 907 908
  tsem_post(&pParam->rspSem);
  return 0;
}
909

L
Liu Jicong 已提交
910
int32_t tmq_subscription(tmq_t* tmq, tmq_list_t** topics) {
X
Xiaoyu Wang 已提交
911 912 913 914
  if (*topics == NULL) {
    *topics = tmq_list_new();
  }
  for (int i = 0; i < taosArrayGetSize(tmq->clientTopics); i++) {
L
Liu Jicong 已提交
915
    SMqClientTopic* topic = taosArrayGet(tmq->clientTopics, i);
L
Liu Jicong 已提交
916
    tmq_list_append(*topics, strchr(topic->topicName, '.') + 1);
X
Xiaoyu Wang 已提交
917
  }
L
Liu Jicong 已提交
918
  return 0;
X
Xiaoyu Wang 已提交
919 920
}

L
Liu Jicong 已提交
921
int32_t tmq_unsubscribe(tmq_t* tmq) {
L
Liu Jicong 已提交
922 923
  int32_t     rsp;
  int32_t     retryCnt = 0;
L
Liu Jicong 已提交
924
  tmq_list_t* lst = tmq_list_new();
L
Liu Jicong 已提交
925 926 927 928 929 930 931 932 933 934
  while (1) {
    rsp = tmq_subscribe(tmq, lst);
    if (rsp != TSDB_CODE_MND_CONSUMER_NOT_READY || retryCnt > 5) {
      break;
    } else {
      retryCnt++;
      taosMsleep(500);
    }
  }

L
Liu Jicong 已提交
935 936
  tmq_list_destroy(lst);
  return rsp;
X
Xiaoyu Wang 已提交
937 938
}

939 940
void tmqFreeImpl(void* handle) {
  tmq_t* tmq = (tmq_t*)handle;
L
Liu Jicong 已提交
941

942
  // TODO stop timer
L
Liu Jicong 已提交
943 944 945 946
  if (tmq->mqueue) {
    tmqClearUnhandleMsg(tmq);
    taosCloseQueue(tmq->mqueue);
  }
947
  if (tmq->delayedTask) taosCloseQueue(tmq->delayedTask);
L
Liu Jicong 已提交
948
  taosFreeQall(tmq->qall);
L
Liu Jicong 已提交
949

950
  tsem_destroy(&tmq->rspSem);
L
Liu Jicong 已提交
951

952 953 954
  int32_t sz = taosArrayGetSize(tmq->clientTopics);
  for (int32_t i = 0; i < sz; i++) {
    SMqClientTopic* pTopic = taosArrayGet(tmq->clientTopics, i);
L
Liu Jicong 已提交
955
    taosMemoryFreeClear(pTopic->schema.pSchema);
956 957 958 959 960
    taosArrayDestroy(pTopic->vgs);
  }
  taosArrayDestroy(tmq->clientTopics);
  taos_close_internal(tmq->pTscObj);
  taosMemoryFree(tmq);
L
Liu Jicong 已提交
961 962
}

963 964 965 966 967 968 969 970 971
static void tmqMgmtInit(void) {
  tmqInitRes = 0;
  tmqMgmt.timer = taosTmrInit(1000, 100, 360000, "TMQ");

  if (tmqMgmt.timer == NULL) {
    tmqInitRes = TSDB_CODE_OUT_OF_MEMORY;
  }

  tmqMgmt.rsetId = taosOpenRef(10000, tmqFreeImpl);
972
  if (tmqMgmt.rsetId < 0) {
973 974 975 976
    tmqInitRes = terrno;
  }
}

L
Liu Jicong 已提交
977
tmq_t* tmq_consumer_new(tmq_conf_t* conf, char* errstr, int32_t errstrLen) {
978 979 980 981
  taosThreadOnce(&tmqInit, tmqMgmtInit);
  if (tmqInitRes != 0) {
    terrno = tmqInitRes;
    return NULL;
L
Liu Jicong 已提交
982 983
  }

L
Liu Jicong 已提交
984 985
  tmq_t* pTmq = taosMemoryCalloc(1, sizeof(tmq_t));
  if (pTmq == NULL) {
L
Liu Jicong 已提交
986
    terrno = TSDB_CODE_OUT_OF_MEMORY;
987
    tscError("failed to create consumer, consumer group %s, code:%s", conf->groupId, terrstr());
L
Liu Jicong 已提交
988 989
    return NULL;
  }
L
Liu Jicong 已提交
990

L
Liu Jicong 已提交
991 992 993
  const char* user = conf->user == NULL ? TSDB_DEFAULT_USER : conf->user;
  const char* pass = conf->pass == NULL ? TSDB_DEFAULT_PASS : conf->pass;

L
Liu Jicong 已提交
994 995 996 997 998
  pTmq->clientTopics = taosArrayInit(0, sizeof(SMqClientTopic));
  pTmq->mqueue = taosOpenQueue();
  pTmq->qall = taosAllocateQall();
  pTmq->delayedTask = taosOpenQueue();

X
Xiaoyu Wang 已提交
999 1000
  if (pTmq->clientTopics == NULL || pTmq->mqueue == NULL || pTmq->qall == NULL || pTmq->delayedTask == NULL ||
      conf->groupId[0] == 0) {
L
Liu Jicong 已提交
1001
    terrno = TSDB_CODE_OUT_OF_MEMORY;
1002
    tscError("consumer:0x%" PRIx64 " setup failed since %s, consumer group %s", pTmq->consumerId, terrstr(),
S
Shengliang Guan 已提交
1003
             pTmq->groupId);
L
Liu Jicong 已提交
1004 1005
    goto FAIL;
  }
L
Liu Jicong 已提交
1006

L
Liu Jicong 已提交
1007 1008
  // init status
  pTmq->status = TMQ_CONSUMER_STATUS__INIT;
L
Liu Jicong 已提交
1009 1010
  pTmq->pollCnt = 0;
  pTmq->epoch = 0;
L
Liu Jicong 已提交
1011 1012
  /*pTmq->epStatus = 0;*/
  /*pTmq->epSkipCnt = 0;*/
L
Liu Jicong 已提交
1013

L
Liu Jicong 已提交
1014 1015 1016
  // set conf
  strcpy(pTmq->clientId, conf->clientId);
  strcpy(pTmq->groupId, conf->groupId);
1017
  pTmq->withTbName = conf->withTbName;
L
Liu Jicong 已提交
1018
  pTmq->useSnapshot = conf->snapEnable;
L
Liu Jicong 已提交
1019
  pTmq->autoCommit = conf->autoCommit;
L
Liu Jicong 已提交
1020
  pTmq->autoCommitInterval = conf->autoCommitInterval;
L
Liu Jicong 已提交
1021 1022
  pTmq->commitCb = conf->commitCb;
  pTmq->commitCbUserParam = conf->commitCbUserParam;
L
Liu Jicong 已提交
1023 1024
  pTmq->resetOffsetCfg = conf->resetOffset;

1025 1026
  pTmq->hbBgEnable = conf->hbBgEnable;

L
Liu Jicong 已提交
1027
  // assign consumerId
L
Liu Jicong 已提交
1028
  pTmq->consumerId = tGenIdPI64();
X
Xiaoyu Wang 已提交
1029

L
Liu Jicong 已提交
1030 1031
  // init semaphore
  if (tsem_init(&pTmq->rspSem, 0, 0) != 0) {
1032
    tscError("consumer:0x %" PRIx64 " setup failed since %s, consumer group %s", pTmq->consumerId, terrstr(),
S
Shengliang Guan 已提交
1033
             pTmq->groupId);
L
Liu Jicong 已提交
1034 1035
    goto FAIL;
  }
L
Liu Jicong 已提交
1036

L
Liu Jicong 已提交
1037 1038 1039
  // init connection
  pTmq->pTscObj = taos_connect_internal(conf->ip, user, pass, NULL, NULL, conf->port, CONN_TYPE__TMQ);
  if (pTmq->pTscObj == NULL) {
1040
    tscError("consumer:0x %" PRIx64 " setup failed since %s, consumer group %s", pTmq->consumerId, terrstr(),
S
Shengliang Guan 已提交
1041
             pTmq->groupId);
L
Liu Jicong 已提交
1042 1043 1044
    tsem_destroy(&pTmq->rspSem);
    goto FAIL;
  }
L
Liu Jicong 已提交
1045

1046 1047 1048 1049 1050 1051
  pTmq->refId = taosAddRef(tmqMgmt.rsetId, pTmq);
  if (pTmq->refId < 0) {
    tmqFreeImpl(pTmq);
    return NULL;
  }

1052
  if (pTmq->hbBgEnable) {
L
Liu Jicong 已提交
1053 1054
    int64_t* pRefId = taosMemoryMalloc(sizeof(int64_t));
    *pRefId = pTmq->refId;
1055
    pTmq->hbLiveTimer = taosTmrStart(tmqSendHbReq, 1000, pRefId, tmqMgmt.timer);
1056 1057
  }

H
Haojun Liao 已提交
1058
  tscInfo("consumer:0x%" PRIx64 " is setup, groupId:%s", pTmq->consumerId, pTmq->groupId);
1059
  return pTmq;
L
Liu Jicong 已提交
1060 1061 1062 1063 1064 1065 1066

FAIL:
  if (pTmq->clientTopics) taosArrayDestroy(pTmq->clientTopics);
  if (pTmq->mqueue) taosCloseQueue(pTmq->mqueue);
  if (pTmq->delayedTask) taosCloseQueue(pTmq->delayedTask);
  if (pTmq->qall) taosFreeQall(pTmq->qall);
  taosMemoryFree(pTmq);
1067

L
Liu Jicong 已提交
1068
  return NULL;
1069 1070
}

L
Liu Jicong 已提交
1071
int32_t tmq_subscribe(tmq_t* tmq, const tmq_list_t* topic_list) {
L
Liu Jicong 已提交
1072 1073 1074
  const SArray*   container = &topic_list->container;
  int32_t         sz = taosArrayGetSize(container);
  void*           buf = NULL;
L
Liu Jicong 已提交
1075
  SMsgSendInfo*   sendInfo = NULL;
L
Liu Jicong 已提交
1076
  SCMSubscribeReq req = {0};
1077
  int32_t         code = 0;
1078

1079
  tscDebug("consumer:0x%" PRIx64 " cgroup:%s, subscribe %d topics", tmq->consumerId, tmq->groupId, sz);
L
Liu Jicong 已提交
1080

1081
  req.consumerId = tmq->consumerId;
L
Liu Jicong 已提交
1082
  tstrncpy(req.clientId, tmq->clientId, 256);
L
Liu Jicong 已提交
1083
  tstrncpy(req.cgroup, tmq->groupId, TSDB_CGROUP_LEN);
1084 1085
  req.topicNames = taosArrayInit(sz, sizeof(void*));

1086 1087 1088 1089
  if (req.topicNames == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto FAIL;
  }
1090

L
Liu Jicong 已提交
1091 1092
  for (int32_t i = 0; i < sz; i++) {
    char* topic = taosArrayGetP(container, i);
1093 1094

    SName name = {0};
L
Liu Jicong 已提交
1095 1096 1097 1098
    tNameSetDbName(&name, tmq->pTscObj->acctId, topic, strlen(topic));
    char* topicFName = taosMemoryCalloc(1, TSDB_TOPIC_FNAME_LEN);
    if (topicFName == NULL) {
      goto FAIL;
1099 1100
    }

1101
    tNameExtractFullName(&name, topicFName);
X
Xiaoyu Wang 已提交
1102
    tscDebug("consumer:0x%" PRIx64 " subscribe topic:%s", tmq->consumerId, topicFName);
L
Liu Jicong 已提交
1103 1104

    taosArrayPush(req.topicNames, &topicFName);
1105 1106
  }

L
Liu Jicong 已提交
1107
  int32_t tlen = tSerializeSCMSubscribeReq(NULL, &req);
1108

L
Liu Jicong 已提交
1109
  buf = taosMemoryMalloc(tlen);
1110 1111 1112 1113
  if (buf == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto FAIL;
  }
L
Liu Jicong 已提交
1114

1115 1116 1117
  void* abuf = buf;
  tSerializeSCMSubscribeReq(&abuf, &req);

L
Liu Jicong 已提交
1118
  sendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
1119 1120 1121 1122
  if (sendInfo == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto FAIL;
  }
1123

X
Xiaoyu Wang 已提交
1124
  SMqSubscribeCbParam param = {
L
Liu Jicong 已提交
1125
      .rspErr = 0,
1126 1127
      .refId = tmq->refId,
      .epoch = tmq->epoch,
X
Xiaoyu Wang 已提交
1128
  };
L
Liu Jicong 已提交
1129

1130 1131 1132
  if (tsem_init(&param.rspSem, 0, 0) != 0) {
    goto FAIL;
  }
L
Liu Jicong 已提交
1133 1134

  sendInfo->msgInfo = (SDataBuf){
X
Xiaoyu Wang 已提交
1135 1136 1137 1138
      .pData = buf,
      .len = tlen,
      .handle = NULL,
  };
1139

L
Liu Jicong 已提交
1140 1141
  sendInfo->requestId = generateRequestId();
  sendInfo->requestObjRefId = 0;
L
Liu Jicong 已提交
1142 1143
  sendInfo->param = &param;
  sendInfo->fp = tmqSubscribeCb;
L
Liu Jicong 已提交
1144
  sendInfo->msgType = TDMT_MND_TMQ_SUBSCRIBE;
L
Liu Jicong 已提交
1145

1146 1147 1148 1149 1150
  SEpSet epSet = getEpSet_s(&tmq->pTscObj->pAppInfo->mgmtEp);

  int64_t transporterId = 0;
  asyncSendMsgToServer(tmq->pTscObj->pAppInfo->pTransporter, &epSet, &transporterId, sendInfo);

L
Liu Jicong 已提交
1151 1152
  // avoid double free if msg is sent
  buf = NULL;
L
Liu Jicong 已提交
1153
  sendInfo = NULL;
L
Liu Jicong 已提交
1154

L
Liu Jicong 已提交
1155 1156
  tsem_wait(&param.rspSem);
  tsem_destroy(&param.rspSem);
1157

1158 1159 1160 1161
  if (param.rspErr != 0) {
    code = param.rspErr;
    goto FAIL;
  }
L
Liu Jicong 已提交
1162

L
Liu Jicong 已提交
1163
  int32_t retryCnt = 0;
L
Liu Jicong 已提交
1164
  while (TSDB_CODE_MND_CONSUMER_NOT_READY == tmqAskEp(tmq, false)) {
wmmhello's avatar
wmmhello 已提交
1165
    if (retryCnt++ > 40) {
L
Liu Jicong 已提交
1166 1167
      goto FAIL;
    }
1168

X
Xiaoyu Wang 已提交
1169
    tscDebug("consumer:0x%" PRIx64 ", mnd not ready for subscribe, retry:%d in 500ms", tmq->consumerId, retryCnt);
L
Liu Jicong 已提交
1170 1171
    taosMsleep(500);
  }
1172

1173 1174
  // init ep timer
  if (tmq->epTimer == NULL) {
1175 1176 1177
    int64_t* pRefId1 = taosMemoryMalloc(sizeof(int64_t));
    *pRefId1 = tmq->refId;
    tmq->epTimer = taosTmrStart(tmqAssignAskEpTask, 1000, pRefId1, tmqMgmt.timer);
1178
  }
L
Liu Jicong 已提交
1179 1180

  // init auto commit timer
1181
  if (tmq->autoCommit && tmq->commitTimer == NULL) {
1182 1183 1184
    int64_t* pRefId2 = taosMemoryMalloc(sizeof(int64_t));
    *pRefId2 = tmq->refId;
    tmq->commitTimer = taosTmrStart(tmqAssignDelayedCommitTask, tmq->autoCommitInterval, pRefId2, tmqMgmt.timer);
L
Liu Jicong 已提交
1185 1186
  }

L
Liu Jicong 已提交
1187
FAIL:
L
Liu Jicong 已提交
1188
  taosArrayDestroyP(req.topicNames, taosMemoryFree);
L
Liu Jicong 已提交
1189
  taosMemoryFree(buf);
L
Liu Jicong 已提交
1190
  taosMemoryFree(sendInfo);
L
Liu Jicong 已提交
1191

L
Liu Jicong 已提交
1192
  return code;
1193 1194
}

L
Liu Jicong 已提交
1195
void tmq_conf_set_auto_commit_cb(tmq_conf_t* conf, tmq_commit_cb* cb, void* param) {
L
Liu Jicong 已提交
1196
  //
1197
  conf->commitCb = cb;
L
Liu Jicong 已提交
1198
  conf->commitCbUserParam = param;
L
Liu Jicong 已提交
1199
}
1200

D
dapan1121 已提交
1201
int32_t tmqPollCb(void* param, SDataBuf* pMsg, int32_t code) {
X
Xiaoyu Wang 已提交
1202 1203
  SMqPollCbParam* pParam = (SMqPollCbParam*)param;
  SMqClientVg*    pVg = pParam->pVg;
L
Liu Jicong 已提交
1204
  SMqClientTopic* pTopic = pParam->pTopic;
1205 1206 1207 1208 1209 1210

  tmq_t* tmq = taosAcquireRef(tmqMgmt.rsetId, pParam->refId);
  if (tmq == NULL) {
    tsem_destroy(&pParam->rspSem);
    taosMemoryFree(pParam);
    taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
1211
    taosMemoryFree(pMsg->pEpSet);
1212 1213 1214 1215
    terrno = TSDB_CODE_TMQ_CONSUMER_CLOSED;
    return -1;
  }

H
Haojun Liao 已提交
1216 1217 1218 1219
  int32_t  epoch = pParam->epoch;
  int32_t  vgId = pParam->vgId;
  uint64_t requestId = pParam->requestId;

L
Liu Jicong 已提交
1220
  taosMemoryFree(pParam);
H
Haojun Liao 已提交
1221

L
Liu Jicong 已提交
1222
  if (code != 0) {
H
Haojun Liao 已提交
1223 1224 1225
    tscWarn("consumer:0x%"PRIx64" msg from vgId:%d discarded, epoch %d, since %s, reqId:0x%"PRIx64, tmq->consumerId, vgId,
        epoch, tstrerror(code), requestId);

L
Liu Jicong 已提交
1226
    if (pMsg->pData) taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
1227 1228
    if (pMsg->pEpSet) taosMemoryFree(pMsg->pEpSet);

H
Haojun Liao 已提交
1229
    // in case of consumer mismatch, wait for 500ms and retry
L
Liu Jicong 已提交
1230
    if (code == TSDB_CODE_TMQ_CONSUMER_MISMATCH) {
1231
      taosMsleep(500);
L
Liu Jicong 已提交
1232
      atomic_store_8(&tmq->status, TMQ_CONSUMER_STATUS__RECOVER);
1233
      tscDebug("consumer:0x%" PRIx64" wait for the re-balance, wait for 500ms and set status to be RECOVER", tmq->consumerId);
H
Haojun Liao 已提交
1234
    } else if (code == TSDB_CODE_TQ_NO_COMMITTED_OFFSET) {
S
Shengliang Guan 已提交
1235
      SMqPollRspWrapper* pRspWrapper = taosAllocateQitem(sizeof(SMqPollRspWrapper), DEF_QITEM, 0);
L
Liu Jicong 已提交
1236
      if (pRspWrapper == NULL) {
H
Haojun Liao 已提交
1237 1238
        tscWarn("consumer:0x%" PRIx64 " msg from vgId:%d discarded, epoch %d since out of memory, reqId:0x%" PRIx64,
                tmq->consumerId, vgId, epoch, requestId);
L
Liu Jicong 已提交
1239 1240
        goto CREATE_MSG_FAIL;
      }
H
Haojun Liao 已提交
1241

L
Liu Jicong 已提交
1242 1243 1244 1245 1246
      pRspWrapper->tmqRspType = TMQ_MSG_TYPE__END_RSP;
      /*pRspWrapper->vgHandle = pVg;*/
      /*pRspWrapper->topicHandle = pTopic;*/
      taosWriteQitem(tmq->mqueue, pRspWrapper);
    }
H
Haojun Liao 已提交
1247

L
fix txn  
Liu Jicong 已提交
1248
    goto CREATE_MSG_FAIL;
L
Liu Jicong 已提交
1249 1250
  }

X
Xiaoyu Wang 已提交
1251 1252 1253
  int32_t msgEpoch = ((SMqRspHead*)pMsg->pData)->epoch;
  int32_t tmqEpoch = atomic_load_32(&tmq->epoch);
  if (msgEpoch < tmqEpoch) {
L
Liu Jicong 已提交
1254
    // do not write into queue since updating epoch reset
H
Haojun Liao 已提交
1255 1256 1257
    tscWarn("consumer:0x%" PRIx64 " msg discard from vgId:%d since from earlier epoch, rsp epoch %d, current epoch %d, reqId:0x%"PRIx64,
            tmq->consumerId, vgId, msgEpoch, tmqEpoch, requestId);

1258
    tsem_post(&tmq->rspSem);
L
Liu Jicong 已提交
1259
    taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
1260
    taosMemoryFree(pMsg->pEpSet);
X
Xiaoyu Wang 已提交
1261 1262 1263 1264
    return 0;
  }

  if (msgEpoch != tmqEpoch) {
H
Haojun Liao 已提交
1265 1266
    tscWarn("consumer:0x%"PRIx64" mismatch rsp from vgId:%d, epoch %d, current epoch %d, reqId:0x%"PRIx64, tmq->consumerId, vgId,
        msgEpoch, tmqEpoch, requestId);
X
Xiaoyu Wang 已提交
1267 1268
  }

L
Liu Jicong 已提交
1269 1270 1271
  // handle meta rsp
  int8_t rspType = ((SMqRspHead*)pMsg->pData)->mqMsgType;

S
Shengliang Guan 已提交
1272
  SMqPollRspWrapper* pRspWrapper = taosAllocateQitem(sizeof(SMqPollRspWrapper), DEF_QITEM, 0);
L
Liu Jicong 已提交
1273
  if (pRspWrapper == NULL) {
L
Liu Jicong 已提交
1274
    taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
1275
    taosMemoryFree(pMsg->pEpSet);
H
Haojun Liao 已提交
1276
    tscWarn("consumer:0x%"PRIx64" msg discard from vgId:%d, epoch %d since out of memory", tmq->consumerId, vgId, epoch);
L
fix txn  
Liu Jicong 已提交
1277
    goto CREATE_MSG_FAIL;
L
Liu Jicong 已提交
1278
  }
L
Liu Jicong 已提交
1279

L
Liu Jicong 已提交
1280
  pRspWrapper->tmqRspType = rspType;
L
Liu Jicong 已提交
1281 1282
  pRspWrapper->vgHandle = pVg;
  pRspWrapper->topicHandle = pTopic;
L
Liu Jicong 已提交
1283

L
Liu Jicong 已提交
1284
  if (rspType == TMQ_MSG_TYPE__POLL_RSP) {
L
Liu Jicong 已提交
1285 1286 1287
    SDecoder decoder;
    tDecoderInit(&decoder, POINTER_SHIFT(pMsg->pData, sizeof(SMqRspHead)), pMsg->len - sizeof(SMqRspHead));
    tDecodeSMqDataRsp(&decoder, &pRspWrapper->dataRsp);
wmmhello's avatar
wmmhello 已提交
1288
    tDecoderClear(&decoder);
L
Liu Jicong 已提交
1289
    memcpy(&pRspWrapper->dataRsp, pMsg->pData, sizeof(SMqRspHead));
L
Liu Jicong 已提交
1290

H
Haojun Liao 已提交
1291
    tscDebug("consumer:0x%" PRIx64 " recv poll rsp, vgId:%d, req offset:%" PRId64 ", rsp offset:%" PRId64 " type %d, reqId:0x%"PRIx64,
L
Liu Jicong 已提交
1292
             tmq->consumerId, pVg->vgId, pRspWrapper->dataRsp.reqOffset.version, pRspWrapper->dataRsp.rspOffset.version,
1293
             rspType, requestId);
L
Liu Jicong 已提交
1294
  } else if (rspType == TMQ_MSG_TYPE__POLL_META_RSP) {
1295 1296 1297 1298
    SDecoder decoder;
    tDecoderInit(&decoder, POINTER_SHIFT(pMsg->pData, sizeof(SMqRspHead)), pMsg->len - sizeof(SMqRspHead));
    tDecodeSMqMetaRsp(&decoder, &pRspWrapper->metaRsp);
    tDecoderClear(&decoder);
L
Liu Jicong 已提交
1299
    memcpy(&pRspWrapper->metaRsp, pMsg->pData, sizeof(SMqRspHead));
L
Liu Jicong 已提交
1300 1301 1302 1303 1304 1305
  } else if (rspType == TMQ_MSG_TYPE__TAOSX_RSP) {
    SDecoder decoder;
    tDecoderInit(&decoder, POINTER_SHIFT(pMsg->pData, sizeof(SMqRspHead)), pMsg->len - sizeof(SMqRspHead));
    tDecodeSTaosxRsp(&decoder, &pRspWrapper->taosxRsp);
    tDecoderClear(&decoder);
    memcpy(&pRspWrapper->taosxRsp, pMsg->pData, sizeof(SMqRspHead));
L
Liu Jicong 已提交
1306
  }
L
Liu Jicong 已提交
1307

L
Liu Jicong 已提交
1308
  taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
1309
  taosMemoryFree(pMsg->pEpSet);
L
Liu Jicong 已提交
1310

H
Haojun Liao 已提交
1311 1312
  tscDebug("consumer:0x%" PRIx64 ", put poll res into mqueue, total in queue:%d, reqId:0x%" PRIx64, tmq->consumerId,
           tmq->mqueue->numOfItems, requestId);
L
Liu Jicong 已提交
1313

L
Liu Jicong 已提交
1314
  taosWriteQitem(tmq->mqueue, pRspWrapper);
1315
  tsem_post(&tmq->rspSem);
L
Liu Jicong 已提交
1316

L
Liu Jicong 已提交
1317
  return 0;
H
Haojun Liao 已提交
1318

L
fix txn  
Liu Jicong 已提交
1319
CREATE_MSG_FAIL:
L
Liu Jicong 已提交
1320
  if (epoch == tmq->epoch) {
L
Liu Jicong 已提交
1321 1322
    atomic_store_32(&pVg->vgStatus, TMQ_VG_STATUS__IDLE);
  }
H
Haojun Liao 已提交
1323

1324
  tsem_post(&tmq->rspSem);
L
Liu Jicong 已提交
1325
  return -1;
1326 1327
}

L
Liu Jicong 已提交
1328
bool tmqUpdateEp(tmq_t* tmq, int32_t epoch, const SMqAskEpRsp* pRsp) {
1329 1330
  bool set = false;

1331
  int32_t topicNumCur = taosArrayGetSize(tmq->clientTopics);
1332
  int32_t topicNumGet = taosArrayGetSize(pRsp->topics);
1333

X
Xiaoyu Wang 已提交
1334 1335
  char vgKey[TSDB_TOPIC_FNAME_LEN + 22];
  tscDebug("consumer:0x%" PRIx64 " update ep epoch from %d to epoch %d, incoming topics:%d, existed topics:%d",
1336
           tmq->consumerId, tmq->epoch, epoch, topicNumGet, topicNumCur);
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347

  SArray* newTopics = taosArrayInit(topicNumGet, sizeof(SMqClientTopic));
  if (newTopics == NULL) {
    return false;
  }

  SHashObj* pHash = taosHashInit(64, MurmurHash3_32, false, HASH_NO_LOCK);
  if (pHash == NULL) {
    taosArrayDestroy(newTopics);
    return false;
  }
1348

1349 1350 1351 1352 1353
  for (int32_t i = 0; i < topicNumCur; i++) {
    // find old topic
    SMqClientTopic* pTopicCur = taosArrayGet(tmq->clientTopics, i);
    if (pTopicCur->vgs) {
      int32_t vgNumCur = taosArrayGetSize(pTopicCur->vgs);
1354
      tscDebug("consumer:0x%" PRIx64 ", new vg num: %d", tmq->consumerId, vgNumCur);
1355 1356 1357
      for (int32_t j = 0; j < vgNumCur; j++) {
        SMqClientVg* pVgCur = taosArrayGet(pTopicCur->vgs, j);
        sprintf(vgKey, "%s:%d", pTopicCur->topicName, pVgCur->vgId);
L
Liu Jicong 已提交
1358
        char buf[80];
L
Liu Jicong 已提交
1359
        tFormatOffset(buf, 80, &pVgCur->currentOffset);
H
Haojun Liao 已提交
1360
        tscDebug("consumer:0x%" PRIx64 ", epoch:%d vgId:%d vgKey:%s, offset:%s", tmq->consumerId, epoch,
L
Liu Jicong 已提交
1361
                 pVgCur->vgId, vgKey, buf);
L
Liu Jicong 已提交
1362
        taosHashPut(pHash, vgKey, strlen(vgKey), &pVgCur->currentOffset, sizeof(STqOffsetVal));
1363 1364 1365 1366 1367 1368 1369 1370
      }
    }
  }

  for (int32_t i = 0; i < topicNumGet; i++) {
    SMqClientTopic topic = {0};
    SMqSubTopicEp* pTopicEp = taosArrayGet(pRsp->topics, i);
    topic.schema = pTopicEp->schema;
L
Liu Jicong 已提交
1371 1372
    pTopicEp->schema.nCols = 0;
    pTopicEp->schema.pSchema = NULL;
1373
    tstrncpy(topic.topicName, pTopicEp->topic, TSDB_TOPIC_FNAME_LEN);
1374 1375
    tstrncpy(topic.db, pTopicEp->db, TSDB_DB_FNAME_LEN);

1376
    tscDebug("consumer:0x%" PRIx64 ", update topic: %s", tmq->consumerId, topic.topicName);
1377 1378 1379 1380 1381 1382

    int32_t vgNumGet = taosArrayGetSize(pTopicEp->vgs);
    topic.vgs = taosArrayInit(vgNumGet, sizeof(SMqClientVg));
    for (int32_t j = 0; j < vgNumGet; j++) {
      SMqSubVgEp* pVgEp = taosArrayGet(pTopicEp->vgs, j);
      sprintf(vgKey, "%s:%d", topic.topicName, pVgEp->vgId);
L
Liu Jicong 已提交
1383 1384
      STqOffsetVal* pOffset = taosHashGet(pHash, vgKey, strlen(vgKey));
      STqOffsetVal  offsetNew = {.type = tmq->resetOffsetCfg};
1385
      if (pOffset != NULL) {
L
Liu Jicong 已提交
1386
        offsetNew = *pOffset;
1387 1388 1389 1390
      }

      SMqClientVg clientVg = {
          .pollCnt = 0,
L
Liu Jicong 已提交
1391
          .currentOffset = offsetNew,
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
          .vgId = pVgEp->vgId,
          .epSet = pVgEp->epSet,
          .vgStatus = TMQ_VG_STATUS__IDLE,
          .vgSkipCnt = 0,
      };
      taosArrayPush(topic.vgs, &clientVg);
      set = true;
    }
    taosArrayPush(newTopics, &topic);
  }
1402 1403

  // destroy current buffered existed topics info
1404 1405 1406 1407
  if (tmq->clientTopics) {
    int32_t sz = taosArrayGetSize(tmq->clientTopics);
    for (int32_t i = 0; i < sz; i++) {
      SMqClientTopic* pTopic = taosArrayGet(tmq->clientTopics, i);
L
Liu Jicong 已提交
1408
      if (pTopic->schema.nCols) taosMemoryFreeClear(pTopic->schema.pSchema);
1409
      taosArrayDestroy(pTopic->vgs);
L
Liu Jicong 已提交
1410
    }
1411

1412
    taosArrayDestroy(tmq->clientTopics);
X
Xiaoyu Wang 已提交
1413
  }
1414

L
Liu Jicong 已提交
1415
  taosHashCleanup(pHash);
L
Liu Jicong 已提交
1416
  tmq->clientTopics = newTopics;
1417

1418
  if (taosArrayGetSize(tmq->clientTopics) == 0) {
1419
    atomic_store_8(&tmq->status, TMQ_CONSUMER_STATUS__NO_TOPIC);
1420
  } else {
1421
    atomic_store_8(&tmq->status, TMQ_CONSUMER_STATUS__READY);
1422
  }
1423

X
Xiaoyu Wang 已提交
1424
  atomic_store_32(&tmq->epoch, epoch);
1425
  tscDebug("consumer:0x%" PRIx64 " update topic info completed", tmq->consumerId);
X
Xiaoyu Wang 已提交
1426 1427 1428
  return set;
}

D
dapan1121 已提交
1429
int32_t tmqAskEpCb(void* param, SDataBuf* pMsg, int32_t code) {
1430
  SMqAskEpCbParam* pParam = (SMqAskEpCbParam*)param;
L
Liu Jicong 已提交
1431
  int8_t           async = pParam->async;
1432 1433 1434 1435 1436 1437 1438 1439 1440
  tmq_t*           tmq = taosAcquireRef(tmqMgmt.rsetId, pParam->refId);

  if (tmq == NULL) {
    if (!async) {
      tsem_destroy(&pParam->rspSem);
    } else {
      taosMemoryFree(pParam);
    }
    taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
1441
    taosMemoryFree(pMsg->pEpSet);
1442 1443 1444 1445
    terrno = TSDB_CODE_TMQ_CONSUMER_CLOSED;
    return -1;
  }

L
Liu Jicong 已提交
1446
  pParam->code = code;
1447
  if (code != 0) {
X
Xiaoyu Wang 已提交
1448 1449
    tscError("consumer:0x%" PRIx64 ", get topic endpoint error, async:%d, code:%s", tmq->consumerId, pParam->async,
             tstrerror(code));
L
Liu Jicong 已提交
1450
    goto END;
1451
  }
L
Liu Jicong 已提交
1452

L
Liu Jicong 已提交
1453
  // tmq's epoch is monotonically increase,
L
Liu Jicong 已提交
1454
  // so it's safe to discard any old epoch msg.
L
Liu Jicong 已提交
1455
  // Epoch will only increase when received newer epoch ep msg
L
Liu Jicong 已提交
1456 1457 1458
  SMqRspHead* head = pMsg->pData;
  int32_t     epoch = atomic_load_32(&tmq->epoch);
  if (head->epoch <= epoch) {
1459 1460
    tscDebug("consumer:0x%" PRIx64 ", recv ep, msg epoch %d, current epoch %d, no need to update local ep",
             tmq->consumerId, head->epoch, epoch);
L
Liu Jicong 已提交
1461
    goto END;
1462
  }
L
Liu Jicong 已提交
1463

1464 1465 1466
  tscDebug("consumer:0x%" PRIx64 ", recv ep, msg epoch %d, current epoch %d, update local ep", tmq->consumerId,
           head->epoch, epoch);

L
Liu Jicong 已提交
1467
  if (!async) {
L
Liu Jicong 已提交
1468 1469
    SMqAskEpRsp rsp;
    tDecodeSMqAskEpRsp(POINTER_SHIFT(pMsg->pData, sizeof(SMqRspHead)), &rsp);
S
Shengliang Guan 已提交
1470 1471
    /*printf("rsp epoch %" PRId64 " sz %" PRId64 "\n", rsp.epoch, rsp.topics->size);*/
    /*printf("tmq epoch %" PRId64 " sz %" PRId64 "\n", tmq->epoch, tmq->clientTopics->size);*/
L
Liu Jicong 已提交
1472
    tmqUpdateEp(tmq, head->epoch, &rsp);
L
Liu Jicong 已提交
1473
    tDeleteSMqAskEpRsp(&rsp);
X
Xiaoyu Wang 已提交
1474
  } else {
S
Shengliang Guan 已提交
1475
    SMqAskEpRspWrapper* pWrapper = taosAllocateQitem(sizeof(SMqAskEpRspWrapper), DEF_QITEM, 0);
L
Liu Jicong 已提交
1476
    if (pWrapper == NULL) {
X
Xiaoyu Wang 已提交
1477
      terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
1478 1479
      code = -1;
      goto END;
X
Xiaoyu Wang 已提交
1480
    }
1481

L
Liu Jicong 已提交
1482 1483 1484
    pWrapper->tmqRspType = TMQ_MSG_TYPE__EP_RSP;
    pWrapper->epoch = head->epoch;
    memcpy(&pWrapper->msg, pMsg->pData, sizeof(SMqRspHead));
L
Liu Jicong 已提交
1485
    tDecodeSMqAskEpRsp(POINTER_SHIFT(pMsg->pData, sizeof(SMqRspHead)), &pWrapper->msg);
L
Liu Jicong 已提交
1486

L
Liu Jicong 已提交
1487
    taosWriteQitem(tmq->mqueue, pWrapper);
1488
    tsem_post(&tmq->rspSem);
1489
  }
L
Liu Jicong 已提交
1490 1491

END:
L
Liu Jicong 已提交
1492
  /*atomic_store_8(&tmq->epStatus, 0);*/
L
Liu Jicong 已提交
1493
  if (!async) {
L
Liu Jicong 已提交
1494
    tsem_post(&pParam->rspSem);
L
Liu Jicong 已提交
1495 1496
  } else {
    taosMemoryFree(pParam);
L
Liu Jicong 已提交
1497
  }
dengyihao's avatar
dengyihao 已提交
1498 1499

  taosMemoryFree(pMsg->pEpSet);
L
Liu Jicong 已提交
1500
  taosMemoryFree(pMsg->pData);
L
Liu Jicong 已提交
1501
  return code;
1502 1503
}

L
Liu Jicong 已提交
1504
int32_t tmqAskEp(tmq_t* tmq, bool async) {
1505
  int32_t code = TSDB_CODE_SUCCESS;
L
Liu Jicong 已提交
1506
#if 0
L
Liu Jicong 已提交
1507
  int8_t  epStatus = atomic_val_compare_exchange_8(&tmq->epStatus, 0, 1);
L
fix txn  
Liu Jicong 已提交
1508
  if (epStatus == 1) {
L
temp  
Liu Jicong 已提交
1509
    int32_t epSkipCnt = atomic_add_fetch_32(&tmq->epSkipCnt, 1);
1510
    tscTrace("consumer:0x%" PRIx64 ", skip ask ep cnt %d", tmq->consumerId, epSkipCnt);
L
Liu Jicong 已提交
1511
    if (epSkipCnt < 5000) return 0;
L
fix txn  
Liu Jicong 已提交
1512
  }
L
temp  
Liu Jicong 已提交
1513
  atomic_store_32(&tmq->epSkipCnt, 0);
L
Liu Jicong 已提交
1514
#endif
1515

D
dapan1121 已提交
1516 1517 1518 1519 1520
  SMqAskEpReq req = {0};
  req.consumerId = tmq->consumerId;
  req.epoch = tmq->epoch;
  strcpy(req.cgroup, tmq->groupId);

L
Liu Jicong 已提交
1521
  int32_t tlen = tSerializeSMqAskEpReq(NULL, 0, &req);
D
dapan1121 已提交
1522
  if (tlen < 0) {
X
Xiaoyu Wang 已提交
1523
    tscError("consumer:0x%" PRIx64 ", tSerializeSMqAskEpReq failed", tmq->consumerId);
D
dapan1121 已提交
1524 1525
    return -1;
  }
1526

L
Liu Jicong 已提交
1527
  void* pReq = taosMemoryCalloc(1, tlen);
L
Liu Jicong 已提交
1528
  if (pReq == NULL) {
X
Xiaoyu Wang 已提交
1529
    tscError("consumer:0x%" PRIx64 ", failed to malloc askEpReq msg, size:%d", tmq->consumerId, tlen);
1530
    terrno = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
1531 1532
    return -1;
  }
1533

D
dapan1121 已提交
1534
  if (tSerializeSMqAskEpReq(pReq, tlen, &req) < 0) {
X
Xiaoyu Wang 已提交
1535
    tscError("consumer:0x%" PRIx64 ", tSerializeSMqAskEpReq %d failed", tmq->consumerId, tlen);
D
dapan1121 已提交
1536
    taosMemoryFree(pReq);
L
Liu Jicong 已提交
1537
    return -1;
L
Liu Jicong 已提交
1538
  }
1539

L
Liu Jicong 已提交
1540
  SMqAskEpCbParam* pParam = taosMemoryCalloc(1, sizeof(SMqAskEpCbParam));
L
Liu Jicong 已提交
1541
  if (pParam == NULL) {
X
Xiaoyu Wang 已提交
1542
    tscError("consumer:0x%" PRIx64 ", failed to malloc subscribe param", tmq->consumerId);
D
dapan1121 已提交
1543
    taosMemoryFree(pReq);
L
Liu Jicong 已提交
1544
    /*atomic_store_8(&tmq->epStatus, 0);*/
L
Liu Jicong 已提交
1545
    return -1;
L
Liu Jicong 已提交
1546
  }
1547

1548 1549
  pParam->refId = tmq->refId;
  pParam->epoch = tmq->epoch;
L
Liu Jicong 已提交
1550
  pParam->async = async;
X
Xiaoyu Wang 已提交
1551
  tsem_init(&pParam->rspSem, 0, 0);
1552

1553
  SMsgSendInfo* sendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
L
Liu Jicong 已提交
1554 1555
  if (sendInfo == NULL) {
    tsem_destroy(&pParam->rspSem);
wafwerar's avatar
wafwerar 已提交
1556
    taosMemoryFree(pParam);
D
dapan1121 已提交
1557
    taosMemoryFree(pReq);
L
Liu Jicong 已提交
1558
    /*atomic_store_8(&tmq->epStatus, 0);*/
L
Liu Jicong 已提交
1559 1560 1561 1562
    return -1;
  }

  sendInfo->msgInfo = (SDataBuf){
D
dapan1121 已提交
1563
      .pData = pReq,
L
Liu Jicong 已提交
1564 1565 1566 1567
      .len = tlen,
      .handle = NULL,
  };

H
Haojun Liao 已提交
1568
  sendInfo->requestId = generateRequestId();
L
Liu Jicong 已提交
1569 1570 1571
  sendInfo->requestObjRefId = 0;
  sendInfo->param = pParam;
  sendInfo->fp = tmqAskEpCb;
L
Liu Jicong 已提交
1572
  sendInfo->msgType = TDMT_MND_TMQ_ASK_EP;
1573

L
Liu Jicong 已提交
1574
  SEpSet epSet = getEpSet_s(&tmq->pTscObj->pAppInfo->mgmtEp);
X
Xiaoyu Wang 已提交
1575
  tscDebug("consumer:0x%" PRIx64 " ask ep from mnode, async:%d, reqId:0x%" PRIx64, tmq->consumerId, async,
H
Haojun Liao 已提交
1576
           sendInfo->requestId);
L
add log  
Liu Jicong 已提交
1577

L
Liu Jicong 已提交
1578 1579
  int64_t transporterId = 0;
  asyncSendMsgToServer(tmq->pTscObj->pAppInfo->pTransporter, &epSet, &transporterId, sendInfo);
1580

L
Liu Jicong 已提交
1581
  if (!async) {
L
Liu Jicong 已提交
1582 1583 1584 1585
    tsem_wait(&pParam->rspSem);
    code = pParam->code;
    taosMemoryFree(pParam);
  }
1586

L
Liu Jicong 已提交
1587
  return code;
1588 1589
}

L
Liu Jicong 已提交
1590
void tmqBuildConsumeReqImpl(SMqPollReq* pReq, tmq_t* tmq, int64_t timeout, SMqClientTopic* pTopic, SMqClientVg* pVg) {
L
Liu Jicong 已提交
1591 1592 1593 1594
  int32_t groupLen = strlen(tmq->groupId);
  memcpy(pReq->subKey, tmq->groupId, groupLen);
  pReq->subKey[groupLen] = TMQ_SEPARATOR;
  strcpy(pReq->subKey + groupLen + 1, pTopic->topicName);
1595

1596
  pReq->withTbName = tmq->withTbName;
L
Liu Jicong 已提交
1597
  pReq->consumerId = tmq->consumerId;
1598
  pReq->timeout = timeout;
X
Xiaoyu Wang 已提交
1599
  pReq->epoch = tmq->epoch;
L
Liu Jicong 已提交
1600
  /*pReq->currentOffset = reqOffset;*/
L
Liu Jicong 已提交
1601
  pReq->reqOffset = pVg->currentOffset;
D
dapan1121 已提交
1602
  pReq->head.vgId = pVg->vgId;
1603 1604
  pReq->useSnapshot = tmq->useSnapshot;
  pReq->reqId = generateRequestId();
1605 1606
}

L
Liu Jicong 已提交
1607 1608
SMqMetaRspObj* tmqBuildMetaRspFromWrapper(SMqPollRspWrapper* pWrapper) {
  SMqMetaRspObj* pRspObj = taosMemoryCalloc(1, sizeof(SMqMetaRspObj));
L
Liu Jicong 已提交
1609
  pRspObj->resType = RES_TYPE__TMQ_META;
L
Liu Jicong 已提交
1610 1611 1612 1613 1614 1615 1616 1617
  tstrncpy(pRspObj->topic, pWrapper->topicHandle->topicName, TSDB_TOPIC_FNAME_LEN);
  tstrncpy(pRspObj->db, pWrapper->topicHandle->db, TSDB_DB_FNAME_LEN);
  pRspObj->vgId = pWrapper->vgHandle->vgId;

  memcpy(&pRspObj->metaRsp, &pWrapper->metaRsp, sizeof(SMqMetaRsp));
  return pRspObj;
}

L
Liu Jicong 已提交
1618 1619 1620
SMqRspObj* tmqBuildRspFromWrapper(SMqPollRspWrapper* pWrapper) {
  SMqRspObj* pRspObj = taosMemoryCalloc(1, sizeof(SMqRspObj));
  pRspObj->resType = RES_TYPE__TMQ;
L
Liu Jicong 已提交
1621 1622
  tstrncpy(pRspObj->topic, pWrapper->topicHandle->topicName, TSDB_TOPIC_FNAME_LEN);
  tstrncpy(pRspObj->db, pWrapper->topicHandle->db, TSDB_DB_FNAME_LEN);
L
Liu Jicong 已提交
1623
  pRspObj->vgId = pWrapper->vgHandle->vgId;
L
Liu Jicong 已提交
1624
  pRspObj->resIter = -1;
L
Liu Jicong 已提交
1625
  memcpy(&pRspObj->rsp, &pWrapper->dataRsp, sizeof(SMqDataRsp));
L
Liu Jicong 已提交
1626

L
Liu Jicong 已提交
1627 1628
  pRspObj->resInfo.totalRows = 0;
  pRspObj->resInfo.precision = TSDB_TIME_PRECISION_MILLI;
L
Liu Jicong 已提交
1629
  if (!pWrapper->dataRsp.withSchema) {
L
Liu Jicong 已提交
1630 1631
    setResSchemaInfo(&pRspObj->resInfo, pWrapper->topicHandle->schema.pSchema, pWrapper->topicHandle->schema.nCols);
  }
L
Liu Jicong 已提交
1632

L
Liu Jicong 已提交
1633
  return pRspObj;
X
Xiaoyu Wang 已提交
1634 1635
}

L
Liu Jicong 已提交
1636 1637
SMqTaosxRspObj* tmqBuildTaosxRspFromWrapper(SMqPollRspWrapper* pWrapper) {
  SMqTaosxRspObj* pRspObj = taosMemoryCalloc(1, sizeof(SMqTaosxRspObj));
1638
  pRspObj->resType = RES_TYPE__TMQ_METADATA;
L
Liu Jicong 已提交
1639 1640 1641 1642
  tstrncpy(pRspObj->topic, pWrapper->topicHandle->topicName, TSDB_TOPIC_FNAME_LEN);
  tstrncpy(pRspObj->db, pWrapper->topicHandle->db, TSDB_DB_FNAME_LEN);
  pRspObj->vgId = pWrapper->vgHandle->vgId;
  pRspObj->resIter = -1;
1643
  memcpy(&pRspObj->rsp, &pWrapper->taosxRsp, sizeof(STaosxRsp));
L
Liu Jicong 已提交
1644 1645 1646

  pRspObj->resInfo.totalRows = 0;
  pRspObj->resInfo.precision = TSDB_TIME_PRECISION_MILLI;
1647
  if (!pWrapper->taosxRsp.withSchema) {
L
Liu Jicong 已提交
1648 1649 1650 1651 1652 1653
    setResSchemaInfo(&pRspObj->resInfo, pWrapper->topicHandle->schema.pSchema, pWrapper->topicHandle->schema.nCols);
  }

  return pRspObj;
}

1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
static int32_t handleErrorBeforePoll(SMqClientVg* pVg, tmq_t* pTmq) {
  atomic_store_32(&pVg->vgStatus, TMQ_VG_STATUS__IDLE);
  tsem_post(&pTmq->rspSem);
  return -1;
}

static int32_t doTmqPollImpl(tmq_t* pTmq, SMqClientTopic* pTopic, SMqClientVg* pVg, int64_t timeout) {
  SMqPollReq req = {0};
  tmqBuildConsumeReqImpl(&req, pTmq, timeout, pTopic, pVg);

  int32_t msgSize = tSerializeSMqPollReq(NULL, 0, &req);
  if (msgSize < 0) {
    return handleErrorBeforePoll(pVg, pTmq);
  }

  char* msg = taosMemoryCalloc(1, msgSize);
  if (NULL == msg) {
    return handleErrorBeforePoll(pVg, pTmq);
  }

  if (tSerializeSMqPollReq(msg, msgSize, &req) < 0) {
    taosMemoryFree(msg);
    return handleErrorBeforePoll(pVg, pTmq);
  }

  SMqPollCbParam* pParam = taosMemoryMalloc(sizeof(SMqPollCbParam));
  if (pParam == NULL) {
    taosMemoryFree(msg);
    return handleErrorBeforePoll(pVg, pTmq);
  }

  pParam->refId = pTmq->refId;
  pParam->epoch = pTmq->epoch;
X
Xiaoyu Wang 已提交
1687
  pParam->pVg = pVg;  // pVg may be released,fix it
1688 1689
  pParam->pTopic = pTopic;
  pParam->vgId = pVg->vgId;
H
Haojun Liao 已提交
1690
  pParam->requestId = req.reqId;
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714

  SMsgSendInfo* sendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
  if (sendInfo == NULL) {
    taosMemoryFree(pParam);
    taosMemoryFree(msg);
    return handleErrorBeforePoll(pVg, pTmq);
  }

  sendInfo->msgInfo = (SDataBuf){
      .pData = msg,
      .len = msgSize,
      .handle = NULL,
  };

  sendInfo->requestId = req.reqId;
  sendInfo->requestObjRefId = 0;
  sendInfo->param = pParam;
  sendInfo->fp = tmqPollCb;
  sendInfo->msgType = TDMT_VND_TMQ_CONSUME;

  int64_t transporterId = 0;
  char    offsetFormatBuf[80];
  tFormatOffset(offsetFormatBuf, tListLen(offsetFormatBuf), &pVg->currentOffset);

H
Haojun Liao 已提交
1715
  tscDebug("consumer:0x%" PRIx64 " send poll to %s vgId:%d, epoch %d, req:%s, reqId:0x%" PRIx64,
1716 1717 1718 1719 1720 1721 1722 1723 1724
           pTmq->consumerId, pTopic->topicName, pVg->vgId, pTmq->epoch, offsetFormatBuf, req.reqId);
  asyncSendMsgToServer(pTmq->pTscObj->pAppInfo->pTransporter, &pVg->epSet, &transporterId, sendInfo);

  pVg->pollCnt++;
  pTmq->pollCnt++;

  return TSDB_CODE_SUCCESS;
}

1725
// broadcast the poll request to all related vnodes
1726
int32_t tmqPollImpl(tmq_t* tmq, int64_t timeout) {
1727
  int32_t numOfTopics = taosArrayGetSize(tmq->clientTopics);
X
Xiaoyu Wang 已提交
1728
  tscDebug("consumer:0x%" PRIx64 " start to poll data, numOfTopics:%d", tmq->consumerId, numOfTopics);
1729 1730

  for (int i = 0; i < numOfTopics; i++) {
X
Xiaoyu Wang 已提交
1731
    SMqClientTopic* pTopic = taosArrayGet(tmq->clientTopics, i);
X
Xiaoyu Wang 已提交
1732
    int32_t         numOfVg = taosArrayGetSize(pTopic->vgs);
1733 1734

    for (int j = 0; j < numOfVg; j++) {
X
Xiaoyu Wang 已提交
1735 1736
      SMqClientVg* pVg = taosArrayGet(pTopic->vgs, j);
      int32_t      vgStatus = atomic_val_compare_exchange_32(&pVg->vgStatus, TMQ_VG_STATUS__IDLE, TMQ_VG_STATUS__WAIT);
1737
      if (vgStatus == TMQ_VG_STATUS__WAIT) {
L
Liu Jicong 已提交
1738
        int32_t vgSkipCnt = atomic_add_fetch_32(&pVg->vgSkipCnt, 1);
H
Haojun Liao 已提交
1739
        tscTrace("consumer:0x%" PRIx64 " epoch %d wait poll-rsp, skip vgId:%d skip cnt %d", tmq->consumerId, tmq->epoch,
X
Xiaoyu Wang 已提交
1740
                 pVg->vgId, vgSkipCnt);
X
Xiaoyu Wang 已提交
1741
        continue;
L
temp  
Liu Jicong 已提交
1742 1743 1744 1745
#if 0
        if (skipCnt < 30000) {
          continue;
        } else {
1746
        tscDebug("consumer:0x%" PRIx64 ",skip vgId:%d skip too much reset", tmq->consumerId, pVg->vgId);
L
temp  
Liu Jicong 已提交
1747 1748
        }
#endif
X
Xiaoyu Wang 已提交
1749
      }
1750

L
Liu Jicong 已提交
1751
      atomic_store_32(&pVg->vgSkipCnt, 0);
1752 1753 1754
      int32_t code = doTmqPollImpl(tmq, pTopic, pVg, timeout);
      if (code != TSDB_CODE_SUCCESS) {
        return code;
D
dapan1121 已提交
1755
      }
X
Xiaoyu Wang 已提交
1756 1757
    }
  }
1758

X
Xiaoyu Wang 已提交
1759 1760 1761
  return 0;
}

L
Liu Jicong 已提交
1762 1763
int32_t tmqHandleNoPollRsp(tmq_t* tmq, SMqRspWrapper* rspWrapper, bool* pReset) {
  if (rspWrapper->tmqRspType == TMQ_MSG_TYPE__EP_RSP) {
L
fix  
Liu Jicong 已提交
1764
    /*printf("ep %d %d\n", rspMsg->head.epoch, tmq->epoch);*/
L
Liu Jicong 已提交
1765 1766
    if (rspWrapper->epoch > atomic_load_32(&tmq->epoch)) {
      SMqAskEpRspWrapper* pEpRspWrapper = (SMqAskEpRspWrapper*)rspWrapper;
L
Liu Jicong 已提交
1767
      SMqAskEpRsp*        rspMsg = &pEpRspWrapper->msg;
L
Liu Jicong 已提交
1768
      tmqUpdateEp(tmq, rspWrapper->epoch, rspMsg);
L
temp  
Liu Jicong 已提交
1769
      /*tmqClearUnhandleMsg(tmq);*/
L
Liu Jicong 已提交
1770
      tDeleteSMqAskEpRsp(rspMsg);
X
Xiaoyu Wang 已提交
1771 1772
      *pReset = true;
    } else {
L
Liu Jicong 已提交
1773
      tmqFreeRspWrapper(rspWrapper);
X
Xiaoyu Wang 已提交
1774 1775 1776 1777 1778 1779 1780 1781
      *pReset = false;
    }
  } else {
    return -1;
  }
  return 0;
}

L
Liu Jicong 已提交
1782
void* tmqHandleAllRsp(tmq_t* tmq, int64_t timeout, bool pollIfReset) {
H
Haojun Liao 已提交
1783
  tscDebug("consumer:0x%" PRIx64 " start to handle the rsp, total:%d", tmq->consumerId, tmq->qall->numOfItems);
1784

X
Xiaoyu Wang 已提交
1785
  while (1) {
L
Liu Jicong 已提交
1786 1787
    SMqRspWrapper* rspWrapper = NULL;
    taosGetQitem(tmq->qall, (void**)&rspWrapper);
1788

L
Liu Jicong 已提交
1789
    if (rspWrapper == NULL) {
L
Liu Jicong 已提交
1790
      taosReadAllQitems(tmq->mqueue, tmq->qall);
L
Liu Jicong 已提交
1791
      taosGetQitem(tmq->qall, (void**)&rspWrapper);
L
Liu Jicong 已提交
1792 1793 1794 1795

      if (rspWrapper == NULL) {
        return NULL;
      }
X
Xiaoyu Wang 已提交
1796 1797
    }

L
Liu Jicong 已提交
1798 1799 1800
    if (rspWrapper->tmqRspType == TMQ_MSG_TYPE__END_RSP) {
      taosFreeQitem(rspWrapper);
      terrno = TSDB_CODE_TQ_NO_COMMITTED_OFFSET;
H
Haojun Liao 已提交
1801
      tscError("consumer:0x%" PRIx64 " unexpected rsp from poll, code:%s", tmq->consumerId, tstrerror(terrno));
L
Liu Jicong 已提交
1802 1803
      return NULL;
    } else if (rspWrapper->tmqRspType == TMQ_MSG_TYPE__POLL_RSP) {
L
Liu Jicong 已提交
1804
      SMqPollRspWrapper* pollRspWrapper = (SMqPollRspWrapper*)rspWrapper;
H
Haojun Liao 已提交
1805

L
Liu Jicong 已提交
1806
      /*atomic_sub_fetch_32(&tmq->readyRequest, 1);*/
1807
      int32_t consumerEpoch = atomic_load_32(&tmq->epoch);
L
Liu Jicong 已提交
1808
      if (pollRspWrapper->dataRsp.head.epoch == consumerEpoch) {
L
Liu Jicong 已提交
1809
        SMqClientVg* pVg = pollRspWrapper->vgHandle;
L
Liu Jicong 已提交
1810
        pVg->currentOffset = pollRspWrapper->dataRsp.rspOffset;
X
Xiaoyu Wang 已提交
1811
        atomic_store_32(&pVg->vgStatus, TMQ_VG_STATUS__IDLE);
H
Haojun Liao 已提交
1812

L
Liu Jicong 已提交
1813
        if (pollRspWrapper->dataRsp.blockNum == 0) {
H
Haojun Liao 已提交
1814
          tscDebug("consumer:0x%" PRIx64 " empty block received, vgId:%d", tmq->consumerId, pVg->vgId);
L
Liu Jicong 已提交
1815 1816
          taosFreeQitem(pollRspWrapper);
          rspWrapper = NULL;
L
temp  
Liu Jicong 已提交
1817 1818
          continue;
        }
H
Haojun Liao 已提交
1819

L
Liu Jicong 已提交
1820
        // build rsp
H
Haojun Liao 已提交
1821 1822
        char buf[80];
        tFormatOffset(buf, 80, &pVg->currentOffset);
L
Liu Jicong 已提交
1823
        SMqRspObj* pRsp = tmqBuildRspFromWrapper(pollRspWrapper);
H
Haojun Liao 已提交
1824 1825 1826
        tscDebug("consumer:0x%" PRIx64 " process poll rsp, vgId:%d, offset:%s, blocks:%d", tmq->consumerId,
                     pVg->vgId, buf, pollRspWrapper->dataRsp.blockNum);

L
Liu Jicong 已提交
1827
        taosFreeQitem(pollRspWrapper);
L
Liu Jicong 已提交
1828
        return pRsp;
X
Xiaoyu Wang 已提交
1829
      } else {
X
Xiaoyu Wang 已提交
1830
        tscDebug("consumer:0x%" PRIx64 " msg discard since epoch mismatch: msg epoch %d, consumer epoch %d",
1831
                 tmq->consumerId, pollRspWrapper->dataRsp.head.epoch, consumerEpoch);
L
Liu Jicong 已提交
1832
        tmqFreeRspWrapper(rspWrapper);
L
Liu Jicong 已提交
1833 1834 1835 1836 1837
        taosFreeQitem(pollRspWrapper);
      }
    } else if (rspWrapper->tmqRspType == TMQ_MSG_TYPE__POLL_META_RSP) {
      SMqPollRspWrapper* pollRspWrapper = (SMqPollRspWrapper*)rspWrapper;
      int32_t            consumerEpoch = atomic_load_32(&tmq->epoch);
1838 1839 1840

      tscDebug("consumer:0x%" PRIx64 " process meta rsp", tmq->consumerId);

L
Liu Jicong 已提交
1841
      if (pollRspWrapper->metaRsp.head.epoch == consumerEpoch) {
L
Liu Jicong 已提交
1842
        SMqClientVg* pVg = pollRspWrapper->vgHandle;
S
Shengliang Guan 已提交
1843
        /*printf("vgId:%d, offset %" PRId64 " up to %" PRId64 "\n", pVg->vgId, pVg->currentOffset,
L
Liu Jicong 已提交
1844
         * rspMsg->msg.rspOffset);*/
wmmhello's avatar
wmmhello 已提交
1845
        pVg->currentOffset = pollRspWrapper->metaRsp.rspOffset;
L
Liu Jicong 已提交
1846 1847
        atomic_store_32(&pVg->vgStatus, TMQ_VG_STATUS__IDLE);
        // build rsp
L
Liu Jicong 已提交
1848
        SMqMetaRspObj* pRsp = tmqBuildMetaRspFromWrapper(pollRspWrapper);
L
Liu Jicong 已提交
1849 1850 1851
        taosFreeQitem(pollRspWrapper);
        return pRsp;
      } else {
X
Xiaoyu Wang 已提交
1852
        tscDebug("consumer:0x%" PRIx64 " msg discard since epoch mismatch: msg epoch %d, consumer epoch %d",
1853
                 tmq->consumerId, pollRspWrapper->metaRsp.head.epoch, consumerEpoch);
L
Liu Jicong 已提交
1854
        tmqFreeRspWrapper(rspWrapper);
L
Liu Jicong 已提交
1855
        taosFreeQitem(pollRspWrapper);
X
Xiaoyu Wang 已提交
1856
      }
L
Liu Jicong 已提交
1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871
    } else if (rspWrapper->tmqRspType == TMQ_MSG_TYPE__TAOSX_RSP) {
      SMqPollRspWrapper* pollRspWrapper = (SMqPollRspWrapper*)rspWrapper;
      /*atomic_sub_fetch_32(&tmq->readyRequest, 1);*/
      int32_t consumerEpoch = atomic_load_32(&tmq->epoch);
      if (pollRspWrapper->taosxRsp.head.epoch == consumerEpoch) {
        SMqClientVg* pVg = pollRspWrapper->vgHandle;
        /*printf("vgId:%d, offset %" PRId64 " up to %" PRId64 "\n", pVg->vgId, pVg->currentOffset,
         * rspMsg->msg.rspOffset);*/
        pVg->currentOffset = pollRspWrapper->taosxRsp.rspOffset;
        atomic_store_32(&pVg->vgStatus, TMQ_VG_STATUS__IDLE);
        if (pollRspWrapper->taosxRsp.blockNum == 0) {
          taosFreeQitem(pollRspWrapper);
          rspWrapper = NULL;
          continue;
        }
wmmhello's avatar
wmmhello 已提交
1872

L
Liu Jicong 已提交
1873
        // build rsp
wmmhello's avatar
wmmhello 已提交
1874
        void* pRsp = NULL;
L
Liu Jicong 已提交
1875
        if (pollRspWrapper->taosxRsp.createTableNum == 0) {
wmmhello's avatar
wmmhello 已提交
1876
          pRsp = tmqBuildRspFromWrapper(pollRspWrapper);
L
Liu Jicong 已提交
1877
        } else {
wmmhello's avatar
wmmhello 已提交
1878 1879
          pRsp = tmqBuildTaosxRspFromWrapper(pollRspWrapper);
        }
L
Liu Jicong 已提交
1880 1881 1882
        taosFreeQitem(pollRspWrapper);
        return pRsp;
      } else {
X
Xiaoyu Wang 已提交
1883
        tscDebug("consumer:0x%" PRIx64 " msg discard since epoch mismatch: msg epoch %d, consumer epoch %d",
1884
                 tmq->consumerId, pollRspWrapper->taosxRsp.head.epoch, consumerEpoch);
L
Liu Jicong 已提交
1885
        tmqFreeRspWrapper(rspWrapper);
L
Liu Jicong 已提交
1886 1887
        taosFreeQitem(pollRspWrapper);
      }
X
Xiaoyu Wang 已提交
1888
    } else {
L
fix  
Liu Jicong 已提交
1889
      /*printf("handle ep rsp %d\n", rspMsg->head.mqMsgType);*/
X
Xiaoyu Wang 已提交
1890
      bool reset = false;
L
Liu Jicong 已提交
1891 1892
      tmqHandleNoPollRsp(tmq, rspWrapper, &reset);
      taosFreeQitem(rspWrapper);
X
Xiaoyu Wang 已提交
1893
      if (pollIfReset && reset) {
1894
        tscDebug("consumer:0x%" PRIx64 ", reset and repoll", tmq->consumerId);
1895
        tmqPollImpl(tmq, timeout);
X
Xiaoyu Wang 已提交
1896 1897 1898
      }
    }
  }
H
Haojun Liao 已提交
1899 1900

  tscDebug("consumer:0x%" PRIx64 " handle the rsp completed", tmq->consumerId);
X
Xiaoyu Wang 已提交
1901 1902
}

1903
TAOS_RES* tmq_consumer_poll(tmq_t* tmq, int64_t timeout) {
L
Liu Jicong 已提交
1904 1905
  void*   rspObj;
  int64_t startTime = taosGetTimestampMs();
L
Liu Jicong 已提交
1906

1907
  tscDebug("consumer:0x%" PRIx64 " start to poll at %" PRId64, tmq->consumerId, startTime);
L
Liu Jicong 已提交
1908

1909 1910 1911
#if 0
  tmqHandleAllDelayedTask(tmq);
  tmqPollImpl(tmq, timeout);
1912
  rspObj = tmqHandleAllRsp(tmq, timeout, false);
L
Liu Jicong 已提交
1913 1914
  if (rspObj) {
    return (TAOS_RES*)rspObj;
L
fix  
Liu Jicong 已提交
1915
  }
1916
#endif
X
Xiaoyu Wang 已提交
1917

1918
  // in no topic status, delayed task also need to be processed
L
Liu Jicong 已提交
1919
  if (atomic_load_8(&tmq->status) == TMQ_CONSUMER_STATUS__INIT) {
1920
    tscDebug("consumer:0x%" PRIx64 " poll return since consumer is init", tmq->consumerId);
1921
    taosMsleep(500);  //     sleep for a while
1922 1923 1924
    return NULL;
  }

wmmhello's avatar
wmmhello 已提交
1925
  while (atomic_load_8(&tmq->status) == TMQ_CONSUMER_STATUS__RECOVER) {
L
Liu Jicong 已提交
1926 1927
    int32_t retryCnt = 0;
    while (TSDB_CODE_MND_CONSUMER_NOT_READY == tmqAskEp(tmq, false)) {
H
Haojun Liao 已提交
1928
      if (retryCnt++ > 40) {
L
Liu Jicong 已提交
1929 1930
        return NULL;
      }
1931

H
Haojun Liao 已提交
1932
      tscDebug("consumer:0x%" PRIx64 " not ready, retry:%d/40 in 500ms", tmq->consumerId, retryCnt);
L
Liu Jicong 已提交
1933 1934 1935 1936
      taosMsleep(500);
    }
  }

X
Xiaoyu Wang 已提交
1937
  while (1) {
L
Liu Jicong 已提交
1938
    tmqHandleAllDelayedTask(tmq);
1939

L
Liu Jicong 已提交
1940
    if (tmqPollImpl(tmq, timeout) < 0) {
1941
      tscDebug("consumer:0x%" PRIx64 " return due to poll error", tmq->consumerId);
L
Liu Jicong 已提交
1942 1943
      /*return NULL;*/
    }
L
Liu Jicong 已提交
1944

1945
    rspObj = tmqHandleAllRsp(tmq, timeout, false);
L
Liu Jicong 已提交
1946
    if (rspObj) {
1947
      tscDebug("consumer:0x%" PRIx64 " return rsp %p", tmq->consumerId, rspObj);
L
Liu Jicong 已提交
1948
      return (TAOS_RES*)rspObj;
L
Liu Jicong 已提交
1949
    } else if (terrno == TSDB_CODE_TQ_NO_COMMITTED_OFFSET) {
1950
      tscDebug("consumer:0x%" PRIx64 " return null since no committed offset", tmq->consumerId);
L
Liu Jicong 已提交
1951
      return NULL;
X
Xiaoyu Wang 已提交
1952
    }
1953

1954
    if (timeout != -1) {
L
Liu Jicong 已提交
1955
      int64_t currentTime = taosGetTimestampMs();
1956 1957 1958
      int64_t elapsedTime = currentTime - startTime;
      if (elapsedTime > timeout) {
        tscDebug("consumer:0x%" PRIx64 " (epoch %d) timeout, no rsp, start time %" PRId64 ", current time %" PRId64,
L
Liu Jicong 已提交
1959
                 tmq->consumerId, tmq->epoch, startTime, currentTime);
X
Xiaoyu Wang 已提交
1960 1961
        return NULL;
      }
1962
      /*tscInfo("consumer:0x%" PRIx64 ", (epoch %d) wait, start time %" PRId64 ", current time %" PRId64*/
L
Liu Jicong 已提交
1963
      /*", left time %" PRId64,*/
1964 1965
      /*tmq->consumerId, tmq->epoch, startTime, currentTime, (timeout - elapsedTime));*/
      tsem_timewait(&tmq->rspSem, (timeout - elapsedTime));
L
Liu Jicong 已提交
1966 1967
    } else {
      // use tsem_timewait instead of tsem_wait to avoid unexpected stuck
L
Liu Jicong 已提交
1968
      tsem_timewait(&tmq->rspSem, 1000);
X
Xiaoyu Wang 已提交
1969 1970 1971 1972
    }
  }
}

L
Liu Jicong 已提交
1973
int32_t tmq_consumer_close(tmq_t* tmq) {
1974
  if (tmq->status == TMQ_CONSUMER_STATUS__READY) {
L
Liu Jicong 已提交
1975 1976
    int32_t rsp = tmq_commit_sync(tmq, NULL);
    if (rsp != 0) {
L
Liu Jicong 已提交
1977
      return rsp;
1978 1979
    }

L
Liu Jicong 已提交
1980
    int32_t     retryCnt = 0;
1981
    tmq_list_t* lst = tmq_list_new();
L
Liu Jicong 已提交
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
    while (1) {
      rsp = tmq_subscribe(tmq, lst);
      if (rsp != TSDB_CODE_MND_CONSUMER_NOT_READY || retryCnt > 5) {
        break;
      } else {
        retryCnt++;
        taosMsleep(500);
      }
    }

1992
    tmq_list_destroy(lst);
L
Liu Jicong 已提交
1993
  }
1994
  taosRemoveRef(tmqMgmt.rsetId, tmq->refId);
L
Liu Jicong 已提交
1995
  return 0;
1996
}
L
Liu Jicong 已提交
1997

L
Liu Jicong 已提交
1998 1999
const char* tmq_err2str(int32_t err) {
  if (err == 0) {
L
Liu Jicong 已提交
2000
    return "success";
L
Liu Jicong 已提交
2001
  } else if (err == -1) {
L
Liu Jicong 已提交
2002 2003 2004
    return "fail";
  } else {
    return tstrerror(err);
L
Liu Jicong 已提交
2005 2006
  }
}
L
Liu Jicong 已提交
2007

L
Liu Jicong 已提交
2008 2009 2010 2011 2012
tmq_res_t tmq_get_res_type(TAOS_RES* res) {
  if (TD_RES_TMQ(res)) {
    return TMQ_RES_DATA;
  } else if (TD_RES_TMQ_META(res)) {
    return TMQ_RES_TABLE_META;
2013 2014
  } else if (TD_RES_TMQ_METADATA(res)) {
    return TMQ_RES_METADATA;
L
Liu Jicong 已提交
2015 2016 2017 2018 2019
  } else {
    return TMQ_RES_INVALID;
  }
}

L
Liu Jicong 已提交
2020
const char* tmq_get_topic_name(TAOS_RES* res) {
L
Liu Jicong 已提交
2021 2022
  if (TD_RES_TMQ(res)) {
    SMqRspObj* pRspObj = (SMqRspObj*)res;
L
Liu Jicong 已提交
2023
    return strchr(pRspObj->topic, '.') + 1;
L
Liu Jicong 已提交
2024 2025 2026
  } else if (TD_RES_TMQ_META(res)) {
    SMqMetaRspObj* pMetaRspObj = (SMqMetaRspObj*)res;
    return strchr(pMetaRspObj->topic, '.') + 1;
2027 2028 2029
  } else if (TD_RES_TMQ_METADATA(res)) {
    SMqTaosxRspObj* pRspObj = (SMqTaosxRspObj*)res;
    return strchr(pRspObj->topic, '.') + 1;
L
Liu Jicong 已提交
2030 2031 2032 2033 2034
  } else {
    return NULL;
  }
}

L
Liu Jicong 已提交
2035 2036 2037 2038
const char* tmq_get_db_name(TAOS_RES* res) {
  if (TD_RES_TMQ(res)) {
    SMqRspObj* pRspObj = (SMqRspObj*)res;
    return strchr(pRspObj->db, '.') + 1;
L
Liu Jicong 已提交
2039 2040 2041
  } else if (TD_RES_TMQ_META(res)) {
    SMqMetaRspObj* pMetaRspObj = (SMqMetaRspObj*)res;
    return strchr(pMetaRspObj->db, '.') + 1;
2042 2043 2044
  } else if (TD_RES_TMQ_METADATA(res)) {
    SMqTaosxRspObj* pRspObj = (SMqTaosxRspObj*)res;
    return strchr(pRspObj->db, '.') + 1;
L
Liu Jicong 已提交
2045 2046 2047 2048 2049
  } else {
    return NULL;
  }
}

L
Liu Jicong 已提交
2050 2051 2052 2053
int32_t tmq_get_vgroup_id(TAOS_RES* res) {
  if (TD_RES_TMQ(res)) {
    SMqRspObj* pRspObj = (SMqRspObj*)res;
    return pRspObj->vgId;
L
Liu Jicong 已提交
2054 2055 2056
  } else if (TD_RES_TMQ_META(res)) {
    SMqMetaRspObj* pMetaRspObj = (SMqMetaRspObj*)res;
    return pMetaRspObj->vgId;
2057
  } else if (TD_RES_TMQ_METADATA(res)) {
2058 2059
    SMqTaosxRspObj* pRspObj = (SMqTaosxRspObj*)res;
    return pRspObj->vgId;
L
Liu Jicong 已提交
2060 2061 2062 2063
  } else {
    return -1;
  }
}
L
Liu Jicong 已提交
2064 2065 2066 2067 2068 2069 2070 2071

const char* tmq_get_table_name(TAOS_RES* res) {
  if (TD_RES_TMQ(res)) {
    SMqRspObj* pRspObj = (SMqRspObj*)res;
    if (!pRspObj->rsp.withTbName || pRspObj->rsp.blockTbName == NULL || pRspObj->resIter < 0 ||
        pRspObj->resIter >= pRspObj->rsp.blockNum) {
      return NULL;
    }
2072
    return (const char*)taosArrayGetP(pRspObj->rsp.blockTbName, pRspObj->resIter);
2073 2074
  } else if (TD_RES_TMQ_METADATA(res)) {
    SMqTaosxRspObj* pRspObj = (SMqTaosxRspObj*)res;
L
Liu Jicong 已提交
2075 2076 2077
    if (!pRspObj->rsp.withTbName || pRspObj->rsp.blockTbName == NULL || pRspObj->resIter < 0 ||
        pRspObj->resIter >= pRspObj->rsp.blockNum) {
      return NULL;
2078
    }
L
Liu Jicong 已提交
2079 2080
    return (const char*)taosArrayGetP(pRspObj->rsp.blockTbName, pRspObj->resIter);
  }
L
Liu Jicong 已提交
2081 2082
  return NULL;
}
2083

L
Liu Jicong 已提交
2084
void tmq_commit_async(tmq_t* tmq, const TAOS_RES* msg, tmq_commit_cb* cb, void* param) {
2085
  //
L
Liu Jicong 已提交
2086
  tmqCommitInner(tmq, msg, 0, 1, cb, param);
L
Liu Jicong 已提交
2087 2088
}

2089 2090
int32_t tmq_commit_sync(tmq_t* tmq, const TAOS_RES* msg) {
  //
L
Liu Jicong 已提交
2091
  return tmqCommitInner(tmq, msg, 0, 0, NULL, NULL);
2092
}