clientTmq.c 71.7 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
#include "tdef.h"
#include "tglobal.h"
X
Xiaoyu Wang 已提交
23
#include "tqueue.h"
24
#include "tref.h"
L
Liu Jicong 已提交
25 26
#include "ttimer.h"

D
dapan1121 已提交
27 28
#define EMPTY_BLOCK_POLL_IDLE_DURATION  10
#define DEFAULT_AUTO_COMMIT_INTERVAL    5000
L
Liu Jicong 已提交
29

X
Xiaoyu Wang 已提交
30
struct SMqMgmt {
31 32 33
  int8_t  inited;
  tmr_h   timer;
  int32_t rsetId;
34
};
L
Liu Jicong 已提交
35

X
Xiaoyu Wang 已提交
36 37
static TdThreadOnce   tmqInit = PTHREAD_ONCE_INIT;  // initialize only once
volatile int32_t      tmqInitRes = 0;               // initialize rsp code
38
static struct SMqMgmt tmqMgmt = {0};
39

L
Liu Jicong 已提交
40 41 42 43 44 45
typedef struct {
  int8_t  tmqRspType;
  int32_t epoch;
} SMqRspWrapper;

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

L
Liu Jicong 已提交
51
struct tmq_list_t {
L
Liu Jicong 已提交
52
  SArray container;
L
Liu Jicong 已提交
53
};
L
Liu Jicong 已提交
54

L
Liu Jicong 已提交
55
struct tmq_conf_t {
56 57 58 59 60 61 62 63
  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;
64 65 66 67 68
  uint16_t       port;
  int32_t        autoCommitInterval;
  char*          ip;
  char*          user;
  char*          pass;
69
  tmq_commit_cb* commitCb;
L
Liu Jicong 已提交
70
  void*          commitCbUserParam;
L
Liu Jicong 已提交
71 72 73
};

struct tmq_t {
D
dapan1121 已提交
74 75 76 77 78 79 80 81 82 83
  int64_t        refId;
  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;
L
Liu Jicong 已提交
84 85
  tmq_commit_cb* commitCb;
  void*          commitCbUserParam;
L
Liu Jicong 已提交
86 87 88 89

  // status
  int8_t  status;
  int32_t epoch;
L
Liu Jicong 已提交
90 91
#if 0
  int8_t  epStatus;
L
Liu Jicong 已提交
92
  int32_t epSkipCnt;
L
Liu Jicong 已提交
93
#endif
D
dapan1121 已提交
94 95 96
  // poll info
  int64_t       pollCnt;
  int64_t       totalRows;
L
Liu Jicong 已提交
97

L
Liu Jicong 已提交
98
  // timer
D
dapan1121 已提交
99 100 101 102 103 104 105 106 107 108 109
  tmr_h         hbLiveTimer;
  tmr_h         epTimer;
  tmr_h         reportTimer;
  tmr_h         commitTimer;
  STscObj*      pTscObj;       // connection
  SArray*       clientTopics;  // SArray<SMqClientTopic>
  STaosQueue*   mqueue;        // queue of rsp
  STaosQall*    qall;
  STaosQueue*   delayedTask;   // delayed task queue for heartbeat and auto commit
  TdThreadMutex lock;          // used to protect the operation on each topic, when updating the epsets.
  tsem_t        rspSem;
L
Liu Jicong 已提交
110 111
};

X
Xiaoyu Wang 已提交
112 113 114 115 116 117 118 119
enum {
  TMQ_VG_STATUS__IDLE = 0,
  TMQ_VG_STATUS__WAIT,
};

enum {
  TMQ_CONSUMER_STATUS__INIT = 0,
  TMQ_CONSUMER_STATUS__READY,
120
  TMQ_CONSUMER_STATUS__NO_TOPIC,
L
Liu Jicong 已提交
121
  TMQ_CONSUMER_STATUS__RECOVER,
L
Liu Jicong 已提交
122 123
};

L
Liu Jicong 已提交
124
enum {
125
  TMQ_DELAYED_TASK__ASK_EP = 1,
L
Liu Jicong 已提交
126 127 128 129
  TMQ_DELAYED_TASK__REPORT,
  TMQ_DELAYED_TASK__COMMIT,
};

L
Liu Jicong 已提交
130
typedef struct {
D
dapan1121 已提交
131 132
  int64_t      pollCnt;
  int64_t      numOfRows;
L
Liu Jicong 已提交
133 134
  STqOffsetVal committedOffset;
  STqOffsetVal currentOffset;
D
dapan1121 已提交
135 136 137 138 139
  int32_t      vgId;
  int32_t      vgStatus;
  int32_t      vgSkipCnt;
  int64_t      emptyBlockReceiveTs; // once empty block is received, idle for ignoreCnt then start to poll data
  SEpSet       epSet;
140 141
} SMqClientVg;

L
Liu Jicong 已提交
142
typedef struct {
143 144 145
  char           topicName[TSDB_TOPIC_FNAME_LEN];
  char           db[TSDB_DB_FNAME_LEN];
  SArray*        vgs;  // SArray<SMqClientVg>
L
Liu Jicong 已提交
146
  SSchemaWrapper schema;
147 148
} SMqClientTopic;

L
Liu Jicong 已提交
149 150
typedef struct {
  int8_t          tmqRspType;
D
dapan1121 已提交
151 152
  int32_t         epoch;         // epoch can be used to guard the vgHandle
  int32_t         vgId;
L
Liu Jicong 已提交
153 154
  SMqClientVg*    vgHandle;
  SMqClientTopic* topicHandle;
D
dapan1121 已提交
155 156
  uint64_t        reqId;
  SEpSet*         pEpset;
L
Liu Jicong 已提交
157
  union {
L
Liu Jicong 已提交
158 159
    SMqDataRsp dataRsp;
    SMqMetaRsp metaRsp;
L
Liu Jicong 已提交
160
    STaosxRsp  taosxRsp;
L
Liu Jicong 已提交
161
  };
L
Liu Jicong 已提交
162 163
} SMqPollRspWrapper;

L
Liu Jicong 已提交
164
typedef struct {
165 166
  int64_t refId;
  int32_t epoch;
L
Liu Jicong 已提交
167 168
  tsem_t  rspSem;
  int32_t rspErr;
L
Liu Jicong 已提交
169
} SMqSubscribeCbParam;
L
Liu Jicong 已提交
170

L
Liu Jicong 已提交
171
typedef struct {
172 173
  int64_t refId;
  int32_t epoch;
L
Liu Jicong 已提交
174
  int32_t code;
L
Liu Jicong 已提交
175
  int32_t async;
X
Xiaoyu Wang 已提交
176
  tsem_t  rspSem;
177 178
} SMqAskEpCbParam;

L
Liu Jicong 已提交
179
typedef struct {
180 181
  int64_t         refId;
  int32_t         epoch;
L
Liu Jicong 已提交
182
  SMqClientVg*    pVg;
L
Liu Jicong 已提交
183
  SMqClientTopic* pTopic;
L
Liu Jicong 已提交
184
  int32_t         vgId;
L
Liu Jicong 已提交
185
  tsem_t          rspSem;
H
Haojun Liao 已提交
186
  uint64_t        requestId; // request id for debug purpose
X
Xiaoyu Wang 已提交
187
} SMqPollCbParam;
188

189
typedef struct {
190 191
  int64_t        refId;
  int32_t        epoch;
L
Liu Jicong 已提交
192 193
  int8_t         automatic;
  int8_t         async;
L
Liu Jicong 已提交
194 195
  int32_t        waitingRspNum;
  int32_t        totalRspNum;
L
Liu Jicong 已提交
196
  int32_t        rspErr;
197
  tmq_commit_cb* userCb;
L
Liu Jicong 已提交
198 199 200 201
  /*SArray*        successfulOffsets;*/
  /*SArray*        failedOffsets;*/
  void*  userParam;
  tsem_t rspSem;
202 203 204 205 206
} SMqCommitCbParamSet;

typedef struct {
  SMqCommitCbParamSet* params;
  STqOffset*           pOffset;
D
dapan1121 已提交
207 208 209
  char                 topicName[TSDB_TOPIC_FNAME_LEN];
  int32_t              vgId;
  tmq_t*               pTmq;
210
} SMqCommitCbParam;
211

212
static int32_t tmqAskEp(tmq_t* tmq, bool async);
D
dapan1121 已提交
213 214 215 216 217
static int32_t makeTopicVgroupKey(char* dst, const char* topicName, int32_t vg);
static int32_t tmqCommitDone(SMqCommitCbParamSet* pParamSet);
static int32_t doSendCommitMsg(tmq_t* tmq, SMqClientVg* pVg, const char* pTopicName, SMqCommitCbParamSet* pParamSet,
                               int32_t index, int32_t totalVgroups);
static void tmqCommitRspCountDown(SMqCommitCbParamSet* pParamSet, int64_t consumerId, const char* pTopic, int32_t vgId);
218

219
tmq_conf_t* tmq_conf_new() {
wafwerar's avatar
wafwerar 已提交
220
  tmq_conf_t* conf = taosMemoryCalloc(1, sizeof(tmq_conf_t));
221 222 223 224 225
  if (conf == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return conf;
  }

226
  conf->withTbName = false;
L
Liu Jicong 已提交
227
  conf->autoCommit = true;
D
dapan1121 已提交
228 229
  conf->autoCommitInterval = DEFAULT_AUTO_COMMIT_INTERVAL;
  conf->resetOffset = TMQ_OFFSET__RESET_EARLIEAST;
230
  conf->hbBgEnable = true;
231

232 233 234
  return conf;
}

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

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

D
dapan1121 已提交
256
  if (strcasecmp(key, "client.id") == 0) {
L
Liu Jicong 已提交
257
    tstrncpy(conf->clientId, value, 256);
L
Liu Jicong 已提交
258 259
    return TMQ_CONF_OK;
  }
L
Liu Jicong 已提交
260

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

D
dapan1121 已提交
273 274
  if (strcasecmp(key, "auto.commit.interval.ms") == 0) {
    conf->autoCommitInterval = taosStr2int64(value);
L
Liu Jicong 已提交
275 276 277
    return TMQ_CONF_OK;
  }

D
dapan1121 已提交
278 279 280
  if (strcasecmp(key, "auto.offset.reset") == 0) {
    if (strcasecmp(value, "none") == 0) {
      conf->resetOffset = TMQ_OFFSET__RESET_NONE;
L
Liu Jicong 已提交
281
      return TMQ_CONF_OK;
D
dapan1121 已提交
282 283
    } else if (strcasecmp(value, "earliest") == 0) {
      conf->resetOffset = TMQ_OFFSET__RESET_EARLIEAST;
L
Liu Jicong 已提交
284
      return TMQ_CONF_OK;
D
dapan1121 已提交
285 286
    } else if (strcasecmp(value, "latest") == 0) {
      conf->resetOffset = TMQ_OFFSET__RESET_LATEST;
L
Liu Jicong 已提交
287 288 289 290 291
      return TMQ_CONF_OK;
    } else {
      return TMQ_CONF_INVALID;
    }
  }
L
Liu Jicong 已提交
292

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

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

D
dapan1121 已提交
317 318
  if (strcasecmp(key, "experimental.snapshot.batch.size") == 0) {
    conf->snapBatchSize = taosStr2int64(value);
L
Liu Jicong 已提交
319 320 321
    return TMQ_CONF_OK;
  }

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

D
dapan1121 已提交
334
  if (strcasecmp(key, "td.connect.ip") == 0) {
335
    conf->ip = taosStrdup(value);
L
Liu Jicong 已提交
336 337
    return TMQ_CONF_OK;
  }
D
dapan1121 已提交
338 339

  if (strcasecmp(key, "td.connect.user") == 0) {
340
    conf->user = taosStrdup(value);
L
Liu Jicong 已提交
341 342
    return TMQ_CONF_OK;
  }
D
dapan1121 已提交
343 344

  if (strcasecmp(key, "td.connect.pass") == 0) {
345
    conf->pass = taosStrdup(value);
L
Liu Jicong 已提交
346 347
    return TMQ_CONF_OK;
  }
D
dapan1121 已提交
348 349 350

  if (strcasecmp(key, "td.connect.port") == 0) {
    conf->port = taosStr2int64(value);
L
Liu Jicong 已提交
351 352
    return TMQ_CONF_OK;
  }
D
dapan1121 已提交
353 354

  if (strcasecmp(key, "td.connect.db") == 0) {
L
Liu Jicong 已提交
355 356 357
    return TMQ_CONF_OK;
  }

L
Liu Jicong 已提交
358
  return TMQ_CONF_UNKNOWN;
359 360 361
}

tmq_list_t* tmq_list_new() {
L
Liu Jicong 已提交
362
  return (tmq_list_t*)taosArrayInit(0, sizeof(void*));
363 364
}

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

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

L
Liu Jicong 已提交
381 382 383 384 385 386 387 388 389 390
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;
}

D
dapan1121 已提交
391 392 393 394
static SMqClientVg* foundClientVg(SArray* pTopicList, const char* pName, int32_t vgId, int32_t* index, int32_t* numOfVgroups) {
  int32_t numOfTopics = taosArrayGetSize(pTopicList);
  *index = -1;
  *numOfVgroups = 0;
L
Liu Jicong 已提交
395

D
dapan1121 已提交
396 397 398 399
  for(int32_t i = 0; i < numOfTopics; ++i) {
    SMqClientTopic* pTopic = taosArrayGet(pTopicList, i);
    if (strcmp(pTopic->topicName, pName) != 0) {
      continue;
400 401
    }

D
dapan1121 已提交
402 403 404 405 406 407 408
    *numOfVgroups = taosArrayGetSize(pTopic->vgs);
    for (int32_t j = 0; j < (*numOfVgroups); ++j) {
      SMqClientVg* pClientVg = taosArrayGet(pTopic->vgs, j);
      if (pClientVg->vgId == vgId) {
        *index = j;
        return pClientVg;
      }
409 410 411
    }
  }

D
dapan1121 已提交
412
  return NULL;
L
Liu Jicong 已提交
413 414
}

D
dapan1121 已提交
415 416 417 418
// Two problems do not need to be addressed here
// 1. update to of epset. the response of poll request will automatically handle this problem
// 2. commit failure. This one needs to be resolved.
static int32_t tmqCommitCb(void* param, SDataBuf* pBuf, int32_t code) {
419
  SMqCommitCbParam*    pParam = (SMqCommitCbParam*)param;
420
  SMqCommitCbParamSet* pParamSet = (SMqCommitCbParamSet*)pParam->params;
L
Liu Jicong 已提交
421

D
dapan1121 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
//  if (code != TSDB_CODE_SUCCESS) { // if commit offset failed, let's try again
//    taosThreadMutexLock(&pParam->pTmq->lock);
//    int32_t numOfVgroups, index;
//    SMqClientVg* pVg = foundClientVg(pParam->pTmq->clientTopics, pParam->topicName, pParam->vgId, &index, &numOfVgroups);
//    if (pVg == NULL) {
//      tscDebug("consumer:0x%" PRIx64
//               " subKey:%s vgId:%d commit failed, code:%s has been transferred to other consumer, no need retry ordinal:%d/%d",
//               pParam->pTmq->consumerId, pParam->pOffset->subKey, pParam->vgId, tstrerror(code), index + 1, numOfVgroups);
//    } else { // let's retry the commit
//      int32_t code1 = doSendCommitMsg(pParam->pTmq, pVg, pParam->topicName, pParamSet, index, numOfVgroups);
//      if (code1 != TSDB_CODE_SUCCESS) {  // retry failed.
//        tscError("consumer:0x%" PRIx64 " topic:%s vgId:%d offset:%" PRId64
//                 " retry failed, ignore this commit. code:%s ordinal:%d/%d",
//                 pParam->pTmq->consumerId, pParam->topicName, pVg->vgId, pVg->committedOffset.version,
//                 tstrerror(terrno), index + 1, numOfVgroups);
//      }
//    }
//
//    taosThreadMutexUnlock(&pParam->pTmq->lock);
//
//    taosMemoryFree(pParam->pOffset);
//    taosMemoryFree(pBuf->pData);
//    taosMemoryFree(pBuf->pEpSet);
//
//    tmqCommitRspCountDown(pParamSet, pParam->pTmq->consumerId, pParam->topicName, pParam->vgId);
//    return 0;
//  }
//
//  // todo replace the pTmq with refId
451

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

D
dapan1121 已提交
456
  tmqCommitRspCountDown(pParamSet, pParam->pTmq->consumerId, pParam->topicName, pParam->vgId);
457 458 459
  return 0;
}

D
dapan1121 已提交
460 461
static int32_t doSendCommitMsg(tmq_t* tmq, SMqClientVg* pVg, const char* pTopicName, SMqCommitCbParamSet* pParamSet,
                               int32_t index, int32_t totalVgroups) {
L
Liu Jicong 已提交
462 463 464 465 466
  STqOffset* pOffset = taosMemoryCalloc(1, sizeof(STqOffset));
  if (pOffset == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
467

L
Liu Jicong 已提交
468
  pOffset->val = pVg->currentOffset;
469

L
Liu Jicong 已提交
470 471 472
  int32_t groupLen = strlen(tmq->groupId);
  memcpy(pOffset->subKey, tmq->groupId, groupLen);
  pOffset->subKey[groupLen] = TMQ_SEPARATOR;
D
dapan1121 已提交
473
  strcpy(pOffset->subKey + groupLen + 1, pTopicName);
L
Liu Jicong 已提交
474

D
dapan1121 已提交
475 476
  int32_t len = 0;
  int32_t code = 0;
L
Liu Jicong 已提交
477 478 479 480
  tEncodeSize(tEncodeSTqOffset, pOffset, len, code);
  if (code < 0) {
    return -1;
  }
481

L
Liu Jicong 已提交
482
  void* buf = taosMemoryCalloc(1, sizeof(SMsgHead) + len);
L
Liu Jicong 已提交
483 484 485 486
  if (buf == NULL) {
    taosMemoryFree(pOffset);
    return -1;
  }
487

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

L
Liu Jicong 已提交
490 491 492 493 494
  void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));

  SEncoder encoder;
  tEncoderInit(&encoder, abuf, len);
  tEncodeSTqOffset(&encoder, pOffset);
L
Liu Jicong 已提交
495
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
496 497

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

L
Liu Jicong 已提交
505 506
  pParam->params = pParamSet;
  pParam->pOffset = pOffset;
D
dapan1121 已提交
507 508 509 510
  pParam->vgId = pVg->vgId;
  pParam->pTmq = tmq;

  tstrncpy(pParam->topicName, pTopicName, tListLen(pParam->topicName));
L
Liu Jicong 已提交
511 512 513 514

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

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

  pMsgSendInfo->requestId = generateRequestId();
  pMsgSendInfo->requestObjRefId = 0;
  pMsgSendInfo->param = pParam;
L
Liu Jicong 已提交
530
  pMsgSendInfo->paramFreeFp = taosMemoryFree;
531
  pMsgSendInfo->fp = tmqCommitCb;
L
Liu Jicong 已提交
532
  pMsgSendInfo->msgType = TDMT_VND_TMQ_COMMIT_OFFSET;
L
Liu Jicong 已提交
533

L
Liu Jicong 已提交
534 535 536
  atomic_add_fetch_32(&pParamSet->waitingRspNum, 1);
  atomic_add_fetch_32(&pParamSet->totalRspNum, 1);

D
dapan1121 已提交
537 538 539 540 541 542
  SEp* pEp = GET_ACTIVE_EP(&pVg->epSet);
  tscDebug("consumer:0x%" PRIx64 " topic:%s on vgId:%d send offset:%" PRId64 " prev:%" PRId64
           ", ep:%s:%d, ordinal:%d/%d, req:0x%" PRIx64,
           tmq->consumerId, pOffset->subKey, pVg->vgId, pOffset->val.version, pVg->committedOffset.version, pEp->fqdn,
           pEp->port, index + 1, totalVgroups, pMsgSendInfo->requestId);

L
Liu Jicong 已提交
543 544 545 546 547
  int64_t transporterId = 0;
  asyncSendMsgToServer(tmq->pTscObj->pAppInfo->pTransporter, &pVg->epSet, &transporterId, pMsgSendInfo);
  return 0;
}

D
dapan1121 已提交
548
static int32_t tmqCommitMsgImpl(tmq_t* tmq, const TAOS_RES* msg, int8_t async, tmq_commit_cb* userCb, void* userParam) {
L
Liu Jicong 已提交
549 550 551 552 553 554 555 556 557 558
  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 已提交
559
  } else if (TD_RES_TMQ_METADATA(msg)) {
560 561 562
    SMqTaosxRspObj* pRspObj = (SMqTaosxRspObj*)msg;
    topic = pRspObj->topic;
    vgId = pRspObj->vgId;
L
Liu Jicong 已提交
563 564 565 566 567 568 569 570 571
  } else {
    return TSDB_CODE_TMQ_INVALID_MSG;
  }

  SMqCommitCbParamSet* pParamSet = taosMemoryCalloc(1, sizeof(SMqCommitCbParamSet));
  if (pParamSet == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
D
dapan1121 已提交
572

573 574
  pParamSet->refId = tmq->refId;
  pParamSet->epoch = tmq->epoch;
L
Liu Jicong 已提交
575 576 577 578 579 580
  pParamSet->automatic = 0;
  pParamSet->async = async;
  pParamSet->userCb = userCb;
  pParamSet->userParam = userParam;
  tsem_init(&pParamSet->rspSem, 0, 0);

L
Liu Jicong 已提交
581 582
  int32_t code = -1;

D
dapan1121 已提交
583 584 585 586 587
  taosThreadMutexLock(&tmq->lock);
  int32_t numOfTopics = taosArrayGetSize(tmq->clientTopics);

  tscDebug("consumer:0x%" PRIx64 " user invoked commit offset for %d", tmq->consumerId, numOfTopics);
  for (int32_t i = 0; i < numOfTopics; i++) {
L
Liu Jicong 已提交
588
    SMqClientTopic* pTopic = taosArrayGet(tmq->clientTopics, i);
D
dapan1121 已提交
589 590 591 592 593 594
    if (strcmp(pTopic->topicName, topic) != 0) {
      continue;
    }

    int32_t numOfVgroups = taosArrayGetSize(pTopic->vgs);
    for (int32_t j = 0; j < numOfVgroups; j++) {
L
Liu Jicong 已提交
595
      SMqClientVg* pVg = taosArrayGet(pTopic->vgs, j);
D
dapan1121 已提交
596 597 598
      if (pVg->vgId != vgId) {
        continue;
      }
L
Liu Jicong 已提交
599

L
Liu Jicong 已提交
600
      if (pVg->currentOffset.type > 0 && !tOffsetEqual(&pVg->currentOffset, &pVg->committedOffset)) {
D
dapan1121 已提交
601
        if (doSendCommitMsg(tmq, pVg, pTopic->topicName, pParamSet, j, numOfVgroups) < 0) {
L
Liu Jicong 已提交
602 603
          tsem_destroy(&pParamSet->rspSem);
          taosMemoryFree(pParamSet);
L
Liu Jicong 已提交
604
          goto FAIL;
L
Liu Jicong 已提交
605
        }
L
Liu Jicong 已提交
606
        goto HANDLE_RSP;
L
Liu Jicong 已提交
607 608
      }
    }
L
Liu Jicong 已提交
609
  }
L
Liu Jicong 已提交
610

L
Liu Jicong 已提交
611 612 613 614
HANDLE_RSP:
  if (pParamSet->totalRspNum == 0) {
    tsem_destroy(&pParamSet->rspSem);
    taosMemoryFree(pParamSet);
D
dapan1121 已提交
615
    taosThreadMutexUnlock(&tmq->lock);
L
Liu Jicong 已提交
616 617 618
    return 0;
  }

L
Liu Jicong 已提交
619
  if (!async) {
D
dapan1121 已提交
620
    taosThreadMutexUnlock(&tmq->lock);
L
Liu Jicong 已提交
621 622 623
    tsem_wait(&pParamSet->rspSem);
    code = pParamSet->rspErr;
    tsem_destroy(&pParamSet->rspSem);
L
Liu Jicong 已提交
624
    taosMemoryFree(pParamSet);
L
Liu Jicong 已提交
625 626 627 628 629 630
    return code;
  } else {
    code = 0;
  }

FAIL:
D
dapan1121 已提交
631
  taosThreadMutexUnlock(&tmq->lock);
L
Liu Jicong 已提交
632 633 634
  if (code != 0 && async) {
    userCb(tmq, code, userParam);
  }
D
dapan1121 已提交
635

L
Liu Jicong 已提交
636 637 638
  return 0;
}

D
dapan1121 已提交
639
static int32_t doAutoCommit(tmq_t* tmq, int8_t automatic, int8_t async, tmq_commit_cb* userCb, void* userParam) {
L
Liu Jicong 已提交
640 641
  int32_t code = -1;

642 643
  SMqCommitCbParamSet* pParamSet = taosMemoryCalloc(1, sizeof(SMqCommitCbParamSet));
  if (pParamSet == NULL) {
L
Liu Jicong 已提交
644 645 646 647 648 649 650 651
    code = TSDB_CODE_OUT_OF_MEMORY;
    if (async) {
      if (automatic) {
        tmq->commitCb(tmq, code, tmq->commitCbUserParam);
      } else {
        userCb(tmq, code, userParam);
      }
    }
652 653
    return -1;
  }
654 655 656 657

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

658 659 660 661 662 663
  pParamSet->automatic = automatic;
  pParamSet->async = async;
  pParamSet->userCb = userCb;
  pParamSet->userParam = userParam;
  tsem_init(&pParamSet->rspSem, 0, 0);

664 665 666
  // init as 1 to prevent concurrency issue
  pParamSet->waitingRspNum = 1;

D
dapan1121 已提交
667
  taosThreadMutexLock(&tmq->lock);
668
  int32_t numOfTopics = taosArrayGetSize(tmq->clientTopics);
X
Xiaoyu Wang 已提交
669
  tscDebug("consumer:0x%" PRIx64 " start to commit offset for %d topics", tmq->consumerId, numOfTopics);
670 671

  for (int32_t i = 0; i < numOfTopics; i++) {
672
    SMqClientTopic* pTopic = taosArrayGet(tmq->clientTopics, i);
D
dapan1121 已提交
673
    int32_t         numOfVgroups = taosArrayGetSize(pTopic->vgs);
L
Liu Jicong 已提交
674

D
dapan1121 已提交
675 676
    tscDebug("consumer:0x%" PRIx64 " commit offset for topics:%s, numOfVgs:%d", tmq->consumerId, pTopic->topicName,
             numOfVgroups);
677
    for (int32_t j = 0; j < numOfVgroups; j++) {
L
Liu Jicong 已提交
678
      SMqClientVg* pVg = taosArrayGet(pTopic->vgs, j);
D
dapan1121 已提交
679

L
Liu Jicong 已提交
680
      if (pVg->currentOffset.type > 0 && !tOffsetEqual(&pVg->currentOffset, &pVg->committedOffset)) {
D
dapan1121 已提交
681 682 683 684 685
        code = doSendCommitMsg(tmq, pVg, pTopic->topicName, pParamSet, j, numOfVgroups);
        if (code != TSDB_CODE_SUCCESS) {
          tscError("consumer:0x%" PRIx64 " topic:%s vgId:%d offset:%" PRId64 " failed, code:%s ordinal:%d/%d",
                   tmq->consumerId, pTopic->topicName, pVg->vgId, pVg->committedOffset.version, tstrerror(terrno),
                   j + 1, numOfVgroups);
L
Liu Jicong 已提交
686 687
          continue;
        }
D
dapan1121 已提交
688 689 690

        // update the offset value.
        pVg->committedOffset = pVg->currentOffset;
691
      } else {
D
dapan1121 已提交
692
        tscDebug("consumer:0x%" PRIx64 " topic:%s vgId:%d, no commit, current:%" PRId64 ", ordinal:%d/%d",
693
                 tmq->consumerId, pTopic->topicName, pVg->vgId, pVg->currentOffset.version, j + 1, numOfVgroups);
694 695 696 697
      }
    }
  }

D
dapan1121 已提交
698 699 700 701
  tscDebug("consumer:0x%" PRIx64 " total commit:%d for %d topics", tmq->consumerId, pParamSet->waitingRspNum - 1,
           numOfTopics);
  taosThreadMutexUnlock(&tmq->lock);

L
Liu Jicong 已提交
702
  // no request is sent
L
Liu Jicong 已提交
703 704 705 706 707 708
  if (pParamSet->totalRspNum == 0) {
    tsem_destroy(&pParamSet->rspSem);
    taosMemoryFree(pParamSet);
    return 0;
  }

L
Liu Jicong 已提交
709
  // count down since waiting rsp num init as 1
D
dapan1121 已提交
710
  tmqCommitRspCountDown(pParamSet, tmq->consumerId, "", 0);
711

712 713 714 715
  if (!async) {
    tsem_wait(&pParamSet->rspSem);
    code = pParamSet->rspErr;
    tsem_destroy(&pParamSet->rspSem);
716
    taosMemoryFree(pParamSet);
L
Liu Jicong 已提交
717
#if 0
718 719
    taosArrayDestroyP(pParamSet->successfulOffsets, taosMemoryFree);
    taosArrayDestroyP(pParamSet->failedOffsets, taosMemoryFree);
L
Liu Jicong 已提交
720
#endif
L
Liu Jicong 已提交
721
  }
722

L
Liu Jicong 已提交
723 724 725
  return code;
}

D
dapan1121 已提交
726 727 728
static int32_t tmqCommitInner(tmq_t* tmq, const TAOS_RES* msg, int8_t automatic, int8_t async, tmq_commit_cb* userCb,
                              void* userParam) {
  if (msg) { // user invoked commit
L
Liu Jicong 已提交
729
    return tmqCommitMsgImpl(tmq, msg, async, userCb, userParam);
D
dapan1121 已提交
730 731
  } else {  // this for auto commit
    return doAutoCommit(tmq, automatic, async, userCb, userParam);
L
Liu Jicong 已提交
732
  }
733 734
}

D
dapan1121 已提交
735 736
static void generateTimedTask(int64_t refId, int32_t type) {
  tmq_t* tmq = taosAcquireRef(tmqMgmt.rsetId, refId);
737
  if (tmq != NULL) {
S
Shengliang Guan 已提交
738
    int8_t* pTaskType = taosAllocateQitem(sizeof(int8_t), DEF_QITEM, 0);
D
dapan1121 已提交
739
    *pTaskType = type;
740 741 742
    taosWriteQitem(tmq->delayedTask, pTaskType);
    tsem_post(&tmq->rspSem);
  }
D
dapan1121 已提交
743 744 745 746 747 748
  taosReleaseRef(tmqMgmt.rsetId, refId);
}

void tmqAssignAskEpTask(void* param, void* tmrId) {
  int64_t refId = *(int64_t*)param;
  generateTimedTask(refId, TMQ_DELAYED_TASK__ASK_EP);
749
  taosMemoryFree(param);
L
Liu Jicong 已提交
750 751 752
}

void tmqAssignDelayedCommitTask(void* param, void* tmrId) {
753
  int64_t refId = *(int64_t*)param;
D
dapan1121 已提交
754
  generateTimedTask(refId, TMQ_DELAYED_TASK__COMMIT);
755
  taosMemoryFree(param);
L
Liu Jicong 已提交
756 757 758
}

void tmqAssignDelayedReportTask(void* param, void* tmrId) {
759 760 761
  int64_t refId = *(int64_t*)param;
  tmq_t*  tmq = taosAcquireRef(tmqMgmt.rsetId, refId);
  if (tmq != NULL) {
S
Shengliang Guan 已提交
762
    int8_t* pTaskType = taosAllocateQitem(sizeof(int8_t), DEF_QITEM, 0);
763 764 765 766
    *pTaskType = TMQ_DELAYED_TASK__REPORT;
    taosWriteQitem(tmq->delayedTask, pTaskType);
    tsem_post(&tmq->rspSem);
  }
D
dapan1121 已提交
767 768

  taosReleaseRef(tmqMgmt.rsetId, refId);
769
  taosMemoryFree(param);
L
Liu Jicong 已提交
770 771
}

772
int32_t tmqHbCb(void* param, SDataBuf* pMsg, int32_t code) {
dengyihao's avatar
dengyihao 已提交
773 774 775 776
  if (pMsg) {
    taosMemoryFree(pMsg->pData);
    taosMemoryFree(pMsg->pEpSet);
  }
777 778 779 780
  return 0;
}

void tmqSendHbReq(void* param, void* tmrId) {
781
  int64_t refId = *(int64_t*)param;
D
dapan1121 已提交
782

783 784
  tmq_t*  tmq = taosAcquireRef(tmqMgmt.rsetId, refId);
  if (tmq == NULL) {
L
Liu Jicong 已提交
785
    taosMemoryFree(param);
786 787
    return;
  }
D
dapan1121 已提交
788 789 790 791 792

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

L
Liu Jicong 已提交
793
  int32_t tlen = tSerializeSMqHbReq(NULL, 0, &req);
D
dapan1121 已提交
794 795
  if (tlen < 0) {
    tscError("tSerializeSMqHbReq failed");
D
dapan1121 已提交
796
    goto OVER;
D
dapan1121 已提交
797
  }
D
dapan1121 已提交
798

L
Liu Jicong 已提交
799
  void* pReq = taosMemoryCalloc(1, tlen);
D
dapan1121 已提交
800 801
  if (tlen < 0) {
    tscError("failed to malloc MqHbReq msg, size:%d", tlen);
D
dapan1121 已提交
802
    goto OVER;
D
dapan1121 已提交
803
  }
D
dapan1121 已提交
804

D
dapan1121 已提交
805 806 807
  if (tSerializeSMqHbReq(pReq, tlen, &req) < 0) {
    tscError("tSerializeSMqHbReq %d failed", tlen);
    taosMemoryFree(pReq);
D
dapan1121 已提交
808
    goto OVER;
D
dapan1121 已提交
809
  }
810 811 812 813

  SMsgSendInfo* sendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
  if (sendInfo == NULL) {
    taosMemoryFree(pReq);
L
Liu Jicong 已提交
814
    goto OVER;
815
  }
D
dapan1121 已提交
816

817 818
  sendInfo->msgInfo = (SDataBuf){
      .pData = pReq,
D
dapan1121 已提交
819
      .len = tlen,
820 821 822 823 824 825 826
      .handle = NULL,
  };

  sendInfo->requestId = generateRequestId();
  sendInfo->requestObjRefId = 0;
  sendInfo->param = NULL;
  sendInfo->fp = tmqHbCb;
L
Liu Jicong 已提交
827
  sendInfo->msgType = TDMT_MND_TMQ_HB;
828 829 830 831 832 833 834

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

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

OVER:
835
  taosTmrReset(tmqSendHbReq, 1000, param, tmqMgmt.timer, &tmq->hbLiveTimer);
D
dapan1121 已提交
836
  taosReleaseRef(tmqMgmt.rsetId, refId);
837 838
}

839
int32_t tmqHandleAllDelayedTask(tmq_t* pTmq) {
L
Liu Jicong 已提交
840
  STaosQall* qall = taosAllocateQall();
841
  taosReadAllQitems(pTmq->delayedTask, qall);
L
Liu Jicong 已提交
842

843 844 845 846
  if (qall->numOfItems == 0) {
    taosFreeQall(qall);
    return TSDB_CODE_SUCCESS;
  }
847

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

852
  while (pTaskType != NULL) {
853
    if (*pTaskType == TMQ_DELAYED_TASK__ASK_EP) {
854
      tmqAskEp(pTmq, true);
855 856

      int64_t* pRefId = taosMemoryMalloc(sizeof(int64_t));
857
      *pRefId = pTmq->refId;
858

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

      int64_t* pRefId = taosMemoryMalloc(sizeof(int64_t));
865
      *pRefId = pTmq->refId;
866

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

L
Liu Jicong 已提交
873
    taosFreeQitem(pTaskType);
874
    taosGetQitem(qall, (void**)&pTaskType);
L
Liu Jicong 已提交
875
  }
876

L
Liu Jicong 已提交
877 878 879 880
  taosFreeQall(qall);
  return 0;
}

D
dapan1121 已提交
881
static void* tmqFreeRspWrapper(SMqRspWrapper* rspWrapper) {
L
Liu Jicong 已提交
882 883 884 885 886 887 888
  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;
D
dapan1121 已提交
889 890
    taosMemoryFreeClear(pRsp->pEpset);

L
Liu Jicong 已提交
891 892 893 894 895 896
    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;
D
dapan1121 已提交
897 898
    taosMemoryFreeClear(pRsp->pEpset);

L
Liu Jicong 已提交
899 900 901
    taosMemoryFree(pRsp->metaRsp.metaRsp);
  } else if (rspWrapper->tmqRspType == TMQ_MSG_TYPE__TAOSX_RSP) {
    SMqPollRspWrapper* pRsp = (SMqPollRspWrapper*)rspWrapper;
D
dapan1121 已提交
902 903
    taosMemoryFreeClear(pRsp->pEpset);

L
Liu Jicong 已提交
904 905 906 907 908 909 910 911
    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);
  }
D
dapan1121 已提交
912 913

  return NULL;
L
Liu Jicong 已提交
914 915
}

L
Liu Jicong 已提交
916
void tmqClearUnhandleMsg(tmq_t* tmq) {
L
Liu Jicong 已提交
917
  SMqRspWrapper* rspWrapper = NULL;
L
Liu Jicong 已提交
918
  while (1) {
L
Liu Jicong 已提交
919 920 921 922 923
    taosGetQitem(tmq->qall, (void**)&rspWrapper);
    if (rspWrapper) {
      tmqFreeRspWrapper(rspWrapper);
      taosFreeQitem(rspWrapper);
    } else {
L
Liu Jicong 已提交
924
      break;
L
Liu Jicong 已提交
925
    }
L
Liu Jicong 已提交
926 927
  }

L
Liu Jicong 已提交
928
  rspWrapper = NULL;
L
Liu Jicong 已提交
929 930
  taosReadAllQitems(tmq->mqueue, tmq->qall);
  while (1) {
L
Liu Jicong 已提交
931 932 933 934 935
    taosGetQitem(tmq->qall, (void**)&rspWrapper);
    if (rspWrapper) {
      tmqFreeRspWrapper(rspWrapper);
      taosFreeQitem(rspWrapper);
    } else {
L
Liu Jicong 已提交
936
      break;
L
Liu Jicong 已提交
937
    }
L
Liu Jicong 已提交
938 939 940
  }
}

D
dapan1121 已提交
941
int32_t tmqSubscribeCb(void* param, SDataBuf* pMsg, int32_t code) {
L
Liu Jicong 已提交
942 943
  SMqSubscribeCbParam* pParam = (SMqSubscribeCbParam*)param;
  pParam->rspErr = code;
dengyihao's avatar
dengyihao 已提交
944 945

  taosMemoryFree(pMsg->pEpSet);
L
Liu Jicong 已提交
946 947 948
  tsem_post(&pParam->rspSem);
  return 0;
}
949

L
Liu Jicong 已提交
950
int32_t tmq_subscription(tmq_t* tmq, tmq_list_t** topics) {
X
Xiaoyu Wang 已提交
951 952 953 954
  if (*topics == NULL) {
    *topics = tmq_list_new();
  }
  for (int i = 0; i < taosArrayGetSize(tmq->clientTopics); i++) {
L
Liu Jicong 已提交
955
    SMqClientTopic* topic = taosArrayGet(tmq->clientTopics, i);
L
Liu Jicong 已提交
956
    tmq_list_append(*topics, strchr(topic->topicName, '.') + 1);
X
Xiaoyu Wang 已提交
957
  }
L
Liu Jicong 已提交
958
  return 0;
X
Xiaoyu Wang 已提交
959 960
}

L
Liu Jicong 已提交
961
int32_t tmq_unsubscribe(tmq_t* tmq) {
L
Liu Jicong 已提交
962 963
  int32_t     rsp;
  int32_t     retryCnt = 0;
L
Liu Jicong 已提交
964
  tmq_list_t* lst = tmq_list_new();
L
Liu Jicong 已提交
965 966 967 968 969 970 971 972 973 974
  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 已提交
975 976
  tmq_list_destroy(lst);
  return rsp;
X
Xiaoyu Wang 已提交
977 978
}

D
dapan1121 已提交
979 980 981 982 983 984
static void freeClientVgImpl(void* param) {
  SMqClientTopic* pTopic = param;
  taosMemoryFreeClear(pTopic->schema.pSchema);
  taosArrayDestroy(pTopic->vgs);
}

985
void tmqFreeImpl(void* handle) {
D
dapan1121 已提交
986 987
  tmq_t*  tmq = (tmq_t*)handle;
  int64_t id = tmq->consumerId;
L
Liu Jicong 已提交
988

989
  // TODO stop timer
L
Liu Jicong 已提交
990 991 992 993
  if (tmq->mqueue) {
    tmqClearUnhandleMsg(tmq);
    taosCloseQueue(tmq->mqueue);
  }
L
Liu Jicong 已提交
994

D
dapan1121 已提交
995 996 997 998 999
  if (tmq->delayedTask) {
    taosCloseQueue(tmq->delayedTask);
  }

  taosFreeQall(tmq->qall);
1000
  tsem_destroy(&tmq->rspSem);
D
dapan1121 已提交
1001
  taosThreadMutexDestroy(&tmq->lock);
L
Liu Jicong 已提交
1002

D
dapan1121 已提交
1003
  taosArrayDestroyEx(tmq->clientTopics, freeClientVgImpl);
1004 1005
  taos_close_internal(tmq->pTscObj);
  taosMemoryFree(tmq);
D
dapan1121 已提交
1006 1007

  tscDebug("consumer:0x%" PRIx64 " closed", id);
L
Liu Jicong 已提交
1008 1009
}

1010 1011 1012 1013 1014 1015 1016 1017 1018
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);
1019
  if (tmqMgmt.rsetId < 0) {
1020 1021 1022 1023
    tmqInitRes = terrno;
  }
}

L
Liu Jicong 已提交
1024
tmq_t* tmq_consumer_new(tmq_conf_t* conf, char* errstr, int32_t errstrLen) {
1025 1026 1027 1028
  taosThreadOnce(&tmqInit, tmqMgmtInit);
  if (tmqInitRes != 0) {
    terrno = tmqInitRes;
    return NULL;
L
Liu Jicong 已提交
1029 1030
  }

L
Liu Jicong 已提交
1031 1032
  tmq_t* pTmq = taosMemoryCalloc(1, sizeof(tmq_t));
  if (pTmq == NULL) {
L
Liu Jicong 已提交
1033
    terrno = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
1034
    tscError("failed to create consumer, groupId:%s, code:%s", conf->groupId, terrstr());
L
Liu Jicong 已提交
1035 1036
    return NULL;
  }
L
Liu Jicong 已提交
1037

L
Liu Jicong 已提交
1038 1039 1040
  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 已提交
1041 1042 1043
  pTmq->clientTopics = taosArrayInit(0, sizeof(SMqClientTopic));
  pTmq->mqueue = taosOpenQueue();
  pTmq->delayedTask = taosOpenQueue();
D
dapan1121 已提交
1044
  pTmq->qall = taosAllocateQall();
L
Liu Jicong 已提交
1045

D
dapan1121 已提交
1046
  taosThreadMutexInit(&pTmq->lock, NULL);
X
Xiaoyu Wang 已提交
1047 1048
  if (pTmq->clientTopics == NULL || pTmq->mqueue == NULL || pTmq->qall == NULL || pTmq->delayedTask == NULL ||
      conf->groupId[0] == 0) {
L
Liu Jicong 已提交
1049
    terrno = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
1050
    tscError("consumer:0x%" PRIx64 " setup failed since %s, groupId:%s", pTmq->consumerId, terrstr(),
S
Shengliang Guan 已提交
1051
             pTmq->groupId);
D
dapan1121 已提交
1052
    goto _failed;
L
Liu Jicong 已提交
1053
  }
L
Liu Jicong 已提交
1054

L
Liu Jicong 已提交
1055 1056
  // init status
  pTmq->status = TMQ_CONSUMER_STATUS__INIT;
L
Liu Jicong 已提交
1057 1058
  pTmq->pollCnt = 0;
  pTmq->epoch = 0;
L
Liu Jicong 已提交
1059 1060
  /*pTmq->epStatus = 0;*/
  /*pTmq->epSkipCnt = 0;*/
L
Liu Jicong 已提交
1061

L
Liu Jicong 已提交
1062 1063 1064
  // set conf
  strcpy(pTmq->clientId, conf->clientId);
  strcpy(pTmq->groupId, conf->groupId);
1065
  pTmq->withTbName = conf->withTbName;
L
Liu Jicong 已提交
1066
  pTmq->useSnapshot = conf->snapEnable;
L
Liu Jicong 已提交
1067
  pTmq->autoCommit = conf->autoCommit;
L
Liu Jicong 已提交
1068
  pTmq->autoCommitInterval = conf->autoCommitInterval;
L
Liu Jicong 已提交
1069 1070
  pTmq->commitCb = conf->commitCb;
  pTmq->commitCbUserParam = conf->commitCbUserParam;
L
Liu Jicong 已提交
1071 1072
  pTmq->resetOffsetCfg = conf->resetOffset;

1073 1074
  pTmq->hbBgEnable = conf->hbBgEnable;

L
Liu Jicong 已提交
1075
  // assign consumerId
L
Liu Jicong 已提交
1076
  pTmq->consumerId = tGenIdPI64();
X
Xiaoyu Wang 已提交
1077

L
Liu Jicong 已提交
1078 1079
  // init semaphore
  if (tsem_init(&pTmq->rspSem, 0, 0) != 0) {
1080
    tscError("consumer:0x %" PRIx64 " setup failed since %s, consumer group %s", pTmq->consumerId, terrstr(),
S
Shengliang Guan 已提交
1081
             pTmq->groupId);
D
dapan1121 已提交
1082
    goto _failed;
L
Liu Jicong 已提交
1083
  }
L
Liu Jicong 已提交
1084

L
Liu Jicong 已提交
1085 1086 1087
  // init connection
  pTmq->pTscObj = taos_connect_internal(conf->ip, user, pass, NULL, NULL, conf->port, CONN_TYPE__TMQ);
  if (pTmq->pTscObj == NULL) {
D
dapan1121 已提交
1088
    tscError("consumer:0x%" PRIx64 " setup failed since %s, groupId:%s", pTmq->consumerId, terrstr(), pTmq->groupId);
L
Liu Jicong 已提交
1089
    tsem_destroy(&pTmq->rspSem);
D
dapan1121 已提交
1090
    goto _failed;
L
Liu Jicong 已提交
1091
  }
L
Liu Jicong 已提交
1092

1093 1094
  pTmq->refId = taosAddRef(tmqMgmt.rsetId, pTmq);
  if (pTmq->refId < 0) {
D
dapan1121 已提交
1095
    goto _failed;
1096 1097
  }

1098
  if (pTmq->hbBgEnable) {
L
Liu Jicong 已提交
1099 1100
    int64_t* pRefId = taosMemoryMalloc(sizeof(int64_t));
    *pRefId = pTmq->refId;
1101
    pTmq->hbLiveTimer = taosTmrStart(tmqSendHbReq, 1000, pRefId, tmqMgmt.timer);
1102 1103
  }

D
dapan1121 已提交
1104 1105 1106 1107 1108 1109
  char         buf[80] = {0};
  STqOffsetVal offset = {.type = pTmq->resetOffsetCfg};
  tFormatOffset(buf, tListLen(buf), &offset);
  tscInfo("consumer:0x%" PRIx64 " is setup, refId:%"PRId64", groupId:%s, snapshot:%d, autoCommit:%d, commitInterval:%dms, offset:%s, backgroudHB:%d",
          pTmq->consumerId, pTmq->refId, pTmq->groupId, pTmq->useSnapshot, pTmq->autoCommit, pTmq->autoCommitInterval, buf,
          pTmq->hbBgEnable);
L
Liu Jicong 已提交
1110

D
dapan1121 已提交
1111
  return pTmq;
1112

D
dapan1121 已提交
1113 1114
_failed:
  tmqFreeImpl(pTmq);
L
Liu Jicong 已提交
1115
  return NULL;
1116 1117
}

L
Liu Jicong 已提交
1118
int32_t tmq_subscribe(tmq_t* tmq, const tmq_list_t* topic_list) {
L
Liu Jicong 已提交
1119 1120 1121
  const SArray*   container = &topic_list->container;
  int32_t         sz = taosArrayGetSize(container);
  void*           buf = NULL;
L
Liu Jicong 已提交
1122
  SMsgSendInfo*   sendInfo = NULL;
L
Liu Jicong 已提交
1123
  SCMSubscribeReq req = {0};
1124
  int32_t         code = 0;
1125

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

1128
  req.consumerId = tmq->consumerId;
L
Liu Jicong 已提交
1129
  tstrncpy(req.clientId, tmq->clientId, 256);
L
Liu Jicong 已提交
1130
  tstrncpy(req.cgroup, tmq->groupId, TSDB_CGROUP_LEN);
1131 1132
  req.topicNames = taosArrayInit(sz, sizeof(void*));

1133 1134 1135 1136
  if (req.topicNames == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto FAIL;
  }
1137

L
Liu Jicong 已提交
1138 1139
  for (int32_t i = 0; i < sz; i++) {
    char* topic = taosArrayGetP(container, i);
1140 1141

    SName name = {0};
L
Liu Jicong 已提交
1142 1143 1144 1145
    tNameSetDbName(&name, tmq->pTscObj->acctId, topic, strlen(topic));
    char* topicFName = taosMemoryCalloc(1, TSDB_TOPIC_FNAME_LEN);
    if (topicFName == NULL) {
      goto FAIL;
1146 1147
    }

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

    taosArrayPush(req.topicNames, &topicFName);
1152 1153
  }

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

L
Liu Jicong 已提交
1156
  buf = taosMemoryMalloc(tlen);
1157 1158 1159 1160
  if (buf == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto FAIL;
  }
L
Liu Jicong 已提交
1161

1162 1163 1164
  void* abuf = buf;
  tSerializeSCMSubscribeReq(&abuf, &req);

L
Liu Jicong 已提交
1165
  sendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
1166 1167 1168 1169
  if (sendInfo == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto FAIL;
  }
1170

X
Xiaoyu Wang 已提交
1171
  SMqSubscribeCbParam param = {
L
Liu Jicong 已提交
1172
      .rspErr = 0,
1173 1174
      .refId = tmq->refId,
      .epoch = tmq->epoch,
X
Xiaoyu Wang 已提交
1175
  };
L
Liu Jicong 已提交
1176

1177 1178 1179
  if (tsem_init(&param.rspSem, 0, 0) != 0) {
    goto FAIL;
  }
L
Liu Jicong 已提交
1180 1181

  sendInfo->msgInfo = (SDataBuf){
X
Xiaoyu Wang 已提交
1182 1183 1184 1185
      .pData = buf,
      .len = tlen,
      .handle = NULL,
  };
1186

L
Liu Jicong 已提交
1187 1188
  sendInfo->requestId = generateRequestId();
  sendInfo->requestObjRefId = 0;
L
Liu Jicong 已提交
1189 1190
  sendInfo->param = &param;
  sendInfo->fp = tmqSubscribeCb;
L
Liu Jicong 已提交
1191
  sendInfo->msgType = TDMT_MND_TMQ_SUBSCRIBE;
L
Liu Jicong 已提交
1192

1193 1194 1195 1196 1197
  SEpSet epSet = getEpSet_s(&tmq->pTscObj->pAppInfo->mgmtEp);

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

L
Liu Jicong 已提交
1198 1199
  // avoid double free if msg is sent
  buf = NULL;
L
Liu Jicong 已提交
1200
  sendInfo = NULL;
L
Liu Jicong 已提交
1201

L
Liu Jicong 已提交
1202 1203
  tsem_wait(&param.rspSem);
  tsem_destroy(&param.rspSem);
1204

1205 1206 1207 1208
  if (param.rspErr != 0) {
    code = param.rspErr;
    goto FAIL;
  }
L
Liu Jicong 已提交
1209

L
Liu Jicong 已提交
1210
  int32_t retryCnt = 0;
L
Liu Jicong 已提交
1211
  while (TSDB_CODE_MND_CONSUMER_NOT_READY == tmqAskEp(tmq, false)) {
wmmhello's avatar
wmmhello 已提交
1212
    if (retryCnt++ > 40) {
L
Liu Jicong 已提交
1213 1214
      goto FAIL;
    }
1215

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

1220 1221
  // init ep timer
  if (tmq->epTimer == NULL) {
1222 1223 1224
    int64_t* pRefId1 = taosMemoryMalloc(sizeof(int64_t));
    *pRefId1 = tmq->refId;
    tmq->epTimer = taosTmrStart(tmqAssignAskEpTask, 1000, pRefId1, tmqMgmt.timer);
1225
  }
L
Liu Jicong 已提交
1226 1227

  // init auto commit timer
1228
  if (tmq->autoCommit && tmq->commitTimer == NULL) {
1229 1230 1231
    int64_t* pRefId2 = taosMemoryMalloc(sizeof(int64_t));
    *pRefId2 = tmq->refId;
    tmq->commitTimer = taosTmrStart(tmqAssignDelayedCommitTask, tmq->autoCommitInterval, pRefId2, tmqMgmt.timer);
L
Liu Jicong 已提交
1232 1233
  }

L
Liu Jicong 已提交
1234
FAIL:
L
Liu Jicong 已提交
1235
  taosArrayDestroyP(req.topicNames, taosMemoryFree);
L
Liu Jicong 已提交
1236
  taosMemoryFree(buf);
L
Liu Jicong 已提交
1237
  taosMemoryFree(sendInfo);
L
Liu Jicong 已提交
1238

L
Liu Jicong 已提交
1239
  return code;
1240 1241
}

L
Liu Jicong 已提交
1242
void tmq_conf_set_auto_commit_cb(tmq_conf_t* conf, tmq_commit_cb* cb, void* param) {
1243
  conf->commitCb = cb;
L
Liu Jicong 已提交
1244
  conf->commitCbUserParam = param;
L
Liu Jicong 已提交
1245
}
1246

D
dapan1121 已提交
1247
int32_t tmqPollCb(void* param, SDataBuf* pMsg, int32_t code) {
X
Xiaoyu Wang 已提交
1248
  SMqPollCbParam* pParam = (SMqPollCbParam*)param;
D
dapan1121 已提交
1249 1250

  int64_t         refId = pParam->refId;
X
Xiaoyu Wang 已提交
1251
  SMqClientVg*    pVg = pParam->pVg;
L
Liu Jicong 已提交
1252
  SMqClientTopic* pTopic = pParam->pTopic;
1253

D
dapan1121 已提交
1254
  tmq_t* tmq = taosAcquireRef(tmqMgmt.rsetId, refId);
1255 1256 1257 1258
  if (tmq == NULL) {
    tsem_destroy(&pParam->rspSem);
    taosMemoryFree(pParam);
    taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
1259
    taosMemoryFree(pMsg->pEpSet);
1260 1261 1262 1263
    terrno = TSDB_CODE_TMQ_CONSUMER_CLOSED;
    return -1;
  }

H
Haojun Liao 已提交
1264 1265 1266 1267
  int32_t  epoch = pParam->epoch;
  int32_t  vgId = pParam->vgId;
  uint64_t requestId = pParam->requestId;

L
Liu Jicong 已提交
1268
  taosMemoryFree(pParam);
H
Haojun Liao 已提交
1269

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

L
Liu Jicong 已提交
1274
    if (pMsg->pData) taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
1275 1276
    if (pMsg->pEpSet) taosMemoryFree(pMsg->pEpSet);

H
Haojun Liao 已提交
1277
    // in case of consumer mismatch, wait for 500ms and retry
L
Liu Jicong 已提交
1278
    if (code == TSDB_CODE_TMQ_CONSUMER_MISMATCH) {
1279
      taosMsleep(500);
L
Liu Jicong 已提交
1280
      atomic_store_8(&tmq->status, TMQ_CONSUMER_STATUS__RECOVER);
1281
      tscDebug("consumer:0x%" PRIx64" wait for the re-balance, wait for 500ms and set status to be RECOVER", tmq->consumerId);
H
Haojun Liao 已提交
1282
    } else if (code == TSDB_CODE_TQ_NO_COMMITTED_OFFSET) {
S
Shengliang Guan 已提交
1283
      SMqPollRspWrapper* pRspWrapper = taosAllocateQitem(sizeof(SMqPollRspWrapper), DEF_QITEM, 0);
L
Liu Jicong 已提交
1284
      if (pRspWrapper == NULL) {
H
Haojun Liao 已提交
1285 1286
        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 已提交
1287 1288
        goto CREATE_MSG_FAIL;
      }
H
Haojun Liao 已提交
1289

L
Liu Jicong 已提交
1290 1291 1292
      pRspWrapper->tmqRspType = TMQ_MSG_TYPE__END_RSP;
      taosWriteQitem(tmq->mqueue, pRspWrapper);
    }
H
Haojun Liao 已提交
1293

L
fix txn  
Liu Jicong 已提交
1294
    goto CREATE_MSG_FAIL;
L
Liu Jicong 已提交
1295 1296
  }

X
Xiaoyu Wang 已提交
1297 1298 1299
  int32_t msgEpoch = ((SMqRspHead*)pMsg->pData)->epoch;
  int32_t tmqEpoch = atomic_load_32(&tmq->epoch);
  if (msgEpoch < tmqEpoch) {
L
Liu Jicong 已提交
1300
    // do not write into queue since updating epoch reset
H
Haojun Liao 已提交
1301 1302 1303
    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);

1304
    tsem_post(&tmq->rspSem);
D
dapan1121 已提交
1305 1306
    taosReleaseRef(tmqMgmt.rsetId, refId);

L
Liu Jicong 已提交
1307
    taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
1308
    taosMemoryFree(pMsg->pEpSet);
X
Xiaoyu Wang 已提交
1309 1310 1311 1312
    return 0;
  }

  if (msgEpoch != tmqEpoch) {
D
dapan1121 已提交
1313 1314
    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 已提交
1315 1316
  }

L
Liu Jicong 已提交
1317 1318 1319
  // handle meta rsp
  int8_t rspType = ((SMqRspHead*)pMsg->pData)->mqMsgType;

S
Shengliang Guan 已提交
1320
  SMqPollRspWrapper* pRspWrapper = taosAllocateQitem(sizeof(SMqPollRspWrapper), DEF_QITEM, 0);
L
Liu Jicong 已提交
1321
  if (pRspWrapper == NULL) {
L
Liu Jicong 已提交
1322
    taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
1323
    taosMemoryFree(pMsg->pEpSet);
H
Haojun Liao 已提交
1324
    tscWarn("consumer:0x%"PRIx64" msg discard from vgId:%d, epoch %d since out of memory", tmq->consumerId, vgId, epoch);
L
fix txn  
Liu Jicong 已提交
1325
    goto CREATE_MSG_FAIL;
L
Liu Jicong 已提交
1326
  }
L
Liu Jicong 已提交
1327

L
Liu Jicong 已提交
1328
  pRspWrapper->tmqRspType = rspType;
L
Liu Jicong 已提交
1329 1330
  pRspWrapper->vgHandle = pVg;
  pRspWrapper->topicHandle = pTopic;
D
dapan1121 已提交
1331 1332 1333
  pRspWrapper->reqId = requestId;
  pRspWrapper->pEpset = pMsg->pEpSet;
  pRspWrapper->vgId = pVg->vgId;
L
Liu Jicong 已提交
1334

D
dapan1121 已提交
1335
  pMsg->pEpSet = NULL;
L
Liu Jicong 已提交
1336
  if (rspType == TMQ_MSG_TYPE__POLL_RSP) {
L
Liu Jicong 已提交
1337 1338 1339
    SDecoder decoder;
    tDecoderInit(&decoder, POINTER_SHIFT(pMsg->pData, sizeof(SMqRspHead)), pMsg->len - sizeof(SMqRspHead));
    tDecodeSMqDataRsp(&decoder, &pRspWrapper->dataRsp);
wmmhello's avatar
wmmhello 已提交
1340
    tDecoderClear(&decoder);
L
Liu Jicong 已提交
1341
    memcpy(&pRspWrapper->dataRsp, pMsg->pData, sizeof(SMqRspHead));
L
Liu Jicong 已提交
1342

D
dapan1121 已提交
1343 1344 1345 1346
    char buf[80];
    tFormatOffset(buf, 80, &pRspWrapper->dataRsp.rspOffset);
    tscDebug("consumer:0x%" PRIx64 " recv poll rsp, vgId:%d, req:%" PRId64 ", rsp:%s type %d, reqId:0x%" PRIx64,
             tmq->consumerId, vgId, pRspWrapper->dataRsp.reqOffset.version, buf, rspType, requestId);
L
Liu Jicong 已提交
1347
  } else if (rspType == TMQ_MSG_TYPE__POLL_META_RSP) {
1348 1349 1350 1351
    SDecoder decoder;
    tDecoderInit(&decoder, POINTER_SHIFT(pMsg->pData, sizeof(SMqRspHead)), pMsg->len - sizeof(SMqRspHead));
    tDecodeSMqMetaRsp(&decoder, &pRspWrapper->metaRsp);
    tDecoderClear(&decoder);
L
Liu Jicong 已提交
1352
    memcpy(&pRspWrapper->metaRsp, pMsg->pData, sizeof(SMqRspHead));
L
Liu Jicong 已提交
1353 1354 1355 1356 1357 1358
  } 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));
D
dapan1121 已提交
1359 1360
  } else { // invalid rspType
    tscError("consumer:0x%"PRIx64" invalid rsp msg received, type:%d ignored", tmq->consumerId, rspType);
L
Liu Jicong 已提交
1361
  }
L
Liu Jicong 已提交
1362

L
Liu Jicong 已提交
1363
  taosMemoryFree(pMsg->pData);
D
dapan1121 已提交
1364
  taosWriteQitem(tmq->mqueue, pRspWrapper);
L
Liu Jicong 已提交
1365

D
dapan1121 已提交
1366 1367
  tscDebug("consumer:0x%" PRIx64 " put poll res into mqueue, type:%d, vgId:%d, total in queue:%d, reqId:0x%" PRIx64,
           tmq->consumerId, rspType, vgId, tmq->mqueue->numOfItems, requestId);
L
Liu Jicong 已提交
1368

1369
  tsem_post(&tmq->rspSem);
D
dapan1121 已提交
1370
  taosReleaseRef(tmqMgmt.rsetId, refId);
L
Liu Jicong 已提交
1371

L
Liu Jicong 已提交
1372
  return 0;
H
Haojun Liao 已提交
1373

L
fix txn  
Liu Jicong 已提交
1374
CREATE_MSG_FAIL:
L
Liu Jicong 已提交
1375
  if (epoch == tmq->epoch) {
L
Liu Jicong 已提交
1376 1377
    atomic_store_32(&pVg->vgStatus, TMQ_VG_STATUS__IDLE);
  }
H
Haojun Liao 已提交
1378

1379
  tsem_post(&tmq->rspSem);
D
dapan1121 已提交
1380 1381
  taosReleaseRef(tmqMgmt.rsetId, refId);

L
Liu Jicong 已提交
1382
  return -1;
1383 1384
}

D
dapan1121 已提交
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
typedef struct SVgroupSaveInfo {
  STqOffsetVal offset;
  int64_t      numOfRows;
} SVgroupSaveInfo;

static void initClientTopicFromRsp(SMqClientTopic* pTopic, SMqSubTopicEp* pTopicEp, SHashObj* pVgOffsetHashMap,
                                   tmq_t* tmq) {
  pTopic->schema = pTopicEp->schema;
  pTopicEp->schema.nCols = 0;
  pTopicEp->schema.pSchema = NULL;

  char vgKey[TSDB_TOPIC_FNAME_LEN + 22];
  int32_t vgNumGet = taosArrayGetSize(pTopicEp->vgs);

  tstrncpy(pTopic->topicName, pTopicEp->topic, TSDB_TOPIC_FNAME_LEN);
  tstrncpy(pTopic->db, pTopicEp->db, TSDB_DB_FNAME_LEN);

  tscDebug("consumer:0x%" PRIx64 ", update topic:%s, numOfVgs:%d", tmq->consumerId, pTopic->topicName, vgNumGet);
  pTopic->vgs = taosArrayInit(vgNumGet, sizeof(SMqClientVg));

  for (int32_t j = 0; j < vgNumGet; j++) {
    SMqSubVgEp* pVgEp = taosArrayGet(pTopicEp->vgs, j);

    makeTopicVgroupKey(vgKey, pTopic->topicName, pVgEp->vgId);
    SVgroupSaveInfo* pInfo = taosHashGet(pVgOffsetHashMap, vgKey, strlen(vgKey));

    int64_t numOfRows = 0;
    STqOffsetVal  offsetNew = {.type = tmq->resetOffsetCfg};
    if (pInfo != NULL) {
      offsetNew = pInfo->offset;
      numOfRows = pInfo->numOfRows;
    }

    SMqClientVg clientVg = {
        .pollCnt = 0,
        .currentOffset = offsetNew,
        .vgId = pVgEp->vgId,
        .epSet = pVgEp->epSet,
        .vgStatus = TMQ_VG_STATUS__IDLE,
        .vgSkipCnt = 0,
        .emptyBlockReceiveTs = 0,
        .numOfRows = numOfRows,
    };

    taosArrayPush(pTopic->vgs, &clientVg);
  }
}

static void freeClientVgInfo(void* param) {
  SMqClientTopic* pTopic = param;
  if (pTopic->schema.nCols) {
    taosMemoryFreeClear(pTopic->schema.pSchema);
  }

  taosArrayDestroy(pTopic->vgs);
}

static bool tmqUpdateEp(tmq_t* tmq, int32_t epoch, const SMqAskEpRsp* pRsp) {
1443 1444
  bool set = false;

1445
  int32_t topicNumCur = taosArrayGetSize(tmq->clientTopics);
1446
  int32_t topicNumGet = taosArrayGetSize(pRsp->topics);
1447

X
Xiaoyu Wang 已提交
1448 1449
  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",
1450
           tmq->consumerId, tmq->epoch, epoch, topicNumGet, topicNumCur);
1451 1452 1453 1454 1455 1456

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

D
dapan1121 已提交
1457 1458
  SHashObj* pVgOffsetHashMap = taosHashInit(64, MurmurHash3_32, false, HASH_NO_LOCK);
  if (pVgOffsetHashMap == NULL) {
1459 1460 1461
    taosArrayDestroy(newTopics);
    return false;
  }
1462

D
dapan1121 已提交
1463
  // todo extract method
1464 1465 1466 1467 1468
  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);
1469
      tscDebug("consumer:0x%" PRIx64 ", new vg num: %d", tmq->consumerId, vgNumCur);
1470 1471
      for (int32_t j = 0; j < vgNumCur; j++) {
        SMqClientVg* pVgCur = taosArrayGet(pTopicCur->vgs, j);
D
dapan1121 已提交
1472 1473
        makeTopicVgroupKey(vgKey, pTopicCur->topicName, pVgCur->vgId);

L
Liu Jicong 已提交
1474
        char buf[80];
L
Liu Jicong 已提交
1475
        tFormatOffset(buf, 80, &pVgCur->currentOffset);
H
Haojun Liao 已提交
1476
        tscDebug("consumer:0x%" PRIx64 ", epoch:%d vgId:%d vgKey:%s, offset:%s", tmq->consumerId, epoch,
L
Liu Jicong 已提交
1477
                 pVgCur->vgId, vgKey, buf);
D
dapan1121 已提交
1478 1479 1480

        SVgroupSaveInfo info = {.offset = pVgCur->currentOffset, .numOfRows = pVgCur->numOfRows};
        taosHashPut(pVgOffsetHashMap, vgKey, strlen(vgKey), &info, sizeof(SVgroupSaveInfo));
1481 1482 1483 1484 1485 1486 1487
      }
    }
  }

  for (int32_t i = 0; i < topicNumGet; i++) {
    SMqClientTopic topic = {0};
    SMqSubTopicEp* pTopicEp = taosArrayGet(pRsp->topics, i);
D
dapan1121 已提交
1488
    initClientTopicFromRsp(&topic, pTopicEp, pVgOffsetHashMap, tmq);
1489 1490
    taosArrayPush(newTopics, &topic);
  }
1491

D
dapan1121 已提交
1492 1493 1494
  taosHashCleanup(pVgOffsetHashMap);

  taosThreadMutexLock(&tmq->lock);
1495
  // destroy current buffered existed topics info
1496
  if (tmq->clientTopics) {
D
dapan1121 已提交
1497
    taosArrayDestroyEx(tmq->clientTopics, freeClientVgInfo);
X
Xiaoyu Wang 已提交
1498
  }
1499

L
Liu Jicong 已提交
1500
  tmq->clientTopics = newTopics;
D
dapan1121 已提交
1501
  taosThreadMutexUnlock(&tmq->lock);
1502

D
dapan1121 已提交
1503 1504
  int8_t flag = (topicNumGet == 0)? TMQ_CONSUMER_STATUS__NO_TOPIC:TMQ_CONSUMER_STATUS__READY;
  atomic_store_8(&tmq->status, flag);
X
Xiaoyu Wang 已提交
1505
  atomic_store_32(&tmq->epoch, epoch);
D
dapan1121 已提交
1506

1507
  tscDebug("consumer:0x%" PRIx64 " update topic info completed", tmq->consumerId);
X
Xiaoyu Wang 已提交
1508 1509 1510
  return set;
}

D
dapan1121 已提交
1511
static int32_t tmqAskEpCb(void* param, SDataBuf* pMsg, int32_t code) {
1512
  SMqAskEpCbParam* pParam = (SMqAskEpCbParam*)param;
L
Liu Jicong 已提交
1513
  int8_t           async = pParam->async;
1514 1515 1516 1517 1518 1519 1520 1521 1522
  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 已提交
1523
    taosMemoryFree(pMsg->pEpSet);
1524 1525 1526 1527
    terrno = TSDB_CODE_TMQ_CONSUMER_CLOSED;
    return -1;
  }

L
Liu Jicong 已提交
1528
  pParam->code = code;
D
dapan1121 已提交
1529
  if (code != TSDB_CODE_SUCCESS) {
X
Xiaoyu Wang 已提交
1530 1531
    tscError("consumer:0x%" PRIx64 ", get topic endpoint error, async:%d, code:%s", tmq->consumerId, pParam->async,
             tstrerror(code));
L
Liu Jicong 已提交
1532
    goto END;
1533
  }
L
Liu Jicong 已提交
1534

L
Liu Jicong 已提交
1535
  // tmq's epoch is monotonically increase,
L
Liu Jicong 已提交
1536
  // so it's safe to discard any old epoch msg.
L
Liu Jicong 已提交
1537
  // Epoch will only increase when received newer epoch ep msg
L
Liu Jicong 已提交
1538 1539 1540
  SMqRspHead* head = pMsg->pData;
  int32_t     epoch = atomic_load_32(&tmq->epoch);
  if (head->epoch <= epoch) {
1541 1542
    tscDebug("consumer:0x%" PRIx64 ", recv ep, msg epoch %d, current epoch %d, no need to update local ep",
             tmq->consumerId, head->epoch, epoch);
D
dapan1121 已提交
1543 1544 1545 1546 1547 1548 1549 1550
    if (tmq->status == TMQ_CONSUMER_STATUS__RECOVER) {
      SMqAskEpRsp rsp;
      tDecodeSMqAskEpRsp(POINTER_SHIFT(pMsg->pData, sizeof(SMqRspHead)), &rsp);
      int8_t flag = (taosArrayGetSize(rsp.topics) == 0) ? TMQ_CONSUMER_STATUS__NO_TOPIC : TMQ_CONSUMER_STATUS__READY;
      atomic_store_8(&tmq->status, flag);
      tDeleteSMqAskEpRsp(&rsp);
    }

L
Liu Jicong 已提交
1551
    goto END;
1552
  }
L
Liu Jicong 已提交
1553

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

L
Liu Jicong 已提交
1557
  if (!async) {
L
Liu Jicong 已提交
1558 1559
    SMqAskEpRsp rsp;
    tDecodeSMqAskEpRsp(POINTER_SHIFT(pMsg->pData, sizeof(SMqRspHead)), &rsp);
L
Liu Jicong 已提交
1560
    tmqUpdateEp(tmq, head->epoch, &rsp);
L
Liu Jicong 已提交
1561
    tDeleteSMqAskEpRsp(&rsp);
X
Xiaoyu Wang 已提交
1562
  } else {
S
Shengliang Guan 已提交
1563
    SMqAskEpRspWrapper* pWrapper = taosAllocateQitem(sizeof(SMqAskEpRspWrapper), DEF_QITEM, 0);
L
Liu Jicong 已提交
1564
    if (pWrapper == NULL) {
X
Xiaoyu Wang 已提交
1565
      terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
1566 1567
      code = -1;
      goto END;
X
Xiaoyu Wang 已提交
1568
    }
1569

L
Liu Jicong 已提交
1570 1571 1572
    pWrapper->tmqRspType = TMQ_MSG_TYPE__EP_RSP;
    pWrapper->epoch = head->epoch;
    memcpy(&pWrapper->msg, pMsg->pData, sizeof(SMqRspHead));
L
Liu Jicong 已提交
1573
    tDecodeSMqAskEpRsp(POINTER_SHIFT(pMsg->pData, sizeof(SMqRspHead)), &pWrapper->msg);
L
Liu Jicong 已提交
1574

L
Liu Jicong 已提交
1575
    taosWriteQitem(tmq->mqueue, pWrapper);
1576
    tsem_post(&tmq->rspSem);
1577
  }
L
Liu Jicong 已提交
1578 1579

END:
D
dapan1121 已提交
1580 1581
  taosReleaseRef(tmqMgmt.rsetId, pParam->refId);

L
Liu Jicong 已提交
1582
  if (!async) {
L
Liu Jicong 已提交
1583
    tsem_post(&pParam->rspSem);
L
Liu Jicong 已提交
1584 1585
  } else {
    taosMemoryFree(pParam);
L
Liu Jicong 已提交
1586
  }
dengyihao's avatar
dengyihao 已提交
1587 1588

  taosMemoryFree(pMsg->pEpSet);
L
Liu Jicong 已提交
1589
  taosMemoryFree(pMsg->pData);
L
Liu Jicong 已提交
1590
  return code;
1591 1592
}

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

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

L
Liu Jicong 已提交
1610 1611
SMqMetaRspObj* tmqBuildMetaRspFromWrapper(SMqPollRspWrapper* pWrapper) {
  SMqMetaRspObj* pRspObj = taosMemoryCalloc(1, sizeof(SMqMetaRspObj));
L
Liu Jicong 已提交
1612
  pRspObj->resType = RES_TYPE__TMQ_META;
L
Liu Jicong 已提交
1613 1614 1615 1616 1617 1618 1619 1620
  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;
}

D
dapan1121 已提交
1621
SMqRspObj* tmqBuildRspFromWrapper(SMqPollRspWrapper* pWrapper, SMqClientVg* pVg, int64_t* numOfRows) {
L
Liu Jicong 已提交
1622 1623
  SMqRspObj* pRspObj = taosMemoryCalloc(1, sizeof(SMqRspObj));
  pRspObj->resType = RES_TYPE__TMQ;
D
dapan1121 已提交
1624 1625

  (*numOfRows) = 0;
L
Liu Jicong 已提交
1626 1627
  tstrncpy(pRspObj->topic, pWrapper->topicHandle->topicName, TSDB_TOPIC_FNAME_LEN);
  tstrncpy(pRspObj->db, pWrapper->topicHandle->db, TSDB_DB_FNAME_LEN);
D
dapan1121 已提交
1628

L
Liu Jicong 已提交
1629
  pRspObj->vgId = pWrapper->vgHandle->vgId;
L
Liu Jicong 已提交
1630
  pRspObj->resIter = -1;
L
Liu Jicong 已提交
1631
  memcpy(&pRspObj->rsp, &pWrapper->dataRsp, sizeof(SMqDataRsp));
L
Liu Jicong 已提交
1632

L
Liu Jicong 已提交
1633 1634
  pRspObj->resInfo.totalRows = 0;
  pRspObj->resInfo.precision = TSDB_TIME_PRECISION_MILLI;
D
dapan1121 已提交
1635

L
Liu Jicong 已提交
1636
  if (!pWrapper->dataRsp.withSchema) {
L
Liu Jicong 已提交
1637 1638
    setResSchemaInfo(&pRspObj->resInfo, pWrapper->topicHandle->schema.pSchema, pWrapper->topicHandle->schema.nCols);
  }
L
Liu Jicong 已提交
1639

D
dapan1121 已提交
1640 1641 1642 1643 1644 1645 1646 1647
  // extract the rows in this data packet
  for(int32_t i = 0; i < pRspObj->rsp.blockNum; ++i) {
    SRetrieveTableRsp* pRetrieve = (SRetrieveTableRsp*)taosArrayGetP(pRspObj->rsp.blockData, i);
    int64_t rows = htobe64(pRetrieve->numOfRows);
    pVg->numOfRows += rows;
    (*numOfRows) += rows;
  }

L
Liu Jicong 已提交
1648
  return pRspObj;
X
Xiaoyu Wang 已提交
1649 1650
}

L
Liu Jicong 已提交
1651 1652
SMqTaosxRspObj* tmqBuildTaosxRspFromWrapper(SMqPollRspWrapper* pWrapper) {
  SMqTaosxRspObj* pRspObj = taosMemoryCalloc(1, sizeof(SMqTaosxRspObj));
1653
  pRspObj->resType = RES_TYPE__TMQ_METADATA;
L
Liu Jicong 已提交
1654 1655 1656 1657
  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;
1658
  memcpy(&pRspObj->rsp, &pWrapper->taosxRsp, sizeof(STaosxRsp));
L
Liu Jicong 已提交
1659 1660 1661

  pRspObj->resInfo.totalRows = 0;
  pRspObj->resInfo.precision = TSDB_TIME_PRECISION_MILLI;
1662
  if (!pWrapper->taosxRsp.withSchema) {
L
Liu Jicong 已提交
1663 1664 1665 1666 1667 1668
    setResSchemaInfo(&pRspObj->resInfo, pWrapper->topicHandle->schema.pSchema, pWrapper->topicHandle->schema.nCols);
  }

  return pRspObj;
}

1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
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 已提交
1702
  pParam->pVg = pVg;  // pVg may be released,fix it
1703 1704
  pParam->pTopic = pTopic;
  pParam->vgId = pVg->vgId;
H
Haojun Liao 已提交
1705
  pParam->requestId = req.reqId;
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729

  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 已提交
1730
  tscDebug("consumer:0x%" PRIx64 " send poll to %s vgId:%d, epoch %d, req:%s, reqId:0x%" PRIx64,
1731 1732 1733 1734 1735 1736 1737 1738 1739
           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;
}

1740
// broadcast the poll request to all related vnodes
D
dapan1121 已提交
1741
static int32_t tmqPollImpl(tmq_t* tmq, int64_t timeout) {
1742
  int32_t numOfTopics = taosArrayGetSize(tmq->clientTopics);
X
Xiaoyu Wang 已提交
1743
  tscDebug("consumer:0x%" PRIx64 " start to poll data, numOfTopics:%d", tmq->consumerId, numOfTopics);
1744 1745

  for (int i = 0; i < numOfTopics; i++) {
X
Xiaoyu Wang 已提交
1746
    SMqClientTopic* pTopic = taosArrayGet(tmq->clientTopics, i);
X
Xiaoyu Wang 已提交
1747
    int32_t         numOfVg = taosArrayGetSize(pTopic->vgs);
1748 1749

    for (int j = 0; j < numOfVg; j++) {
X
Xiaoyu Wang 已提交
1750
      SMqClientVg* pVg = taosArrayGet(pTopic->vgs, j);
D
dapan1121 已提交
1751 1752 1753 1754 1755 1756 1757
      if (taosGetTimestampMs() - pVg->emptyBlockReceiveTs < EMPTY_BLOCK_POLL_IDLE_DURATION) { // less than 100ms
        tscTrace("consumer:0x%" PRIx64 " epoch %d, vgId:%d idle for 10ms before start next poll", tmq->consumerId, tmq->epoch,
                 pVg->vgId);
        continue;
      }

      int32_t vgStatus = atomic_val_compare_exchange_32(&pVg->vgStatus, TMQ_VG_STATUS__IDLE, TMQ_VG_STATUS__WAIT);
1758
      if (vgStatus == TMQ_VG_STATUS__WAIT) {
L
Liu Jicong 已提交
1759
        int32_t vgSkipCnt = atomic_add_fetch_32(&pVg->vgSkipCnt, 1);
H
Haojun Liao 已提交
1760
        tscTrace("consumer:0x%" PRIx64 " epoch %d wait poll-rsp, skip vgId:%d skip cnt %d", tmq->consumerId, tmq->epoch,
X
Xiaoyu Wang 已提交
1761
                 pVg->vgId, vgSkipCnt);
X
Xiaoyu Wang 已提交
1762
        continue;
L
temp  
Liu Jicong 已提交
1763 1764 1765 1766
#if 0
        if (skipCnt < 30000) {
          continue;
        } else {
1767
        tscDebug("consumer:0x%" PRIx64 ",skip vgId:%d skip too much reset", tmq->consumerId, pVg->vgId);
L
temp  
Liu Jicong 已提交
1768 1769
        }
#endif
X
Xiaoyu Wang 已提交
1770
      }
1771

L
Liu Jicong 已提交
1772
      atomic_store_32(&pVg->vgSkipCnt, 0);
1773 1774 1775
      int32_t code = doTmqPollImpl(tmq, pTopic, pVg, timeout);
      if (code != TSDB_CODE_SUCCESS) {
        return code;
D
dapan1121 已提交
1776
      }
X
Xiaoyu Wang 已提交
1777 1778
    }
  }
1779

D
dapan1121 已提交
1780
  tscDebug("consumer:0x%" PRIx64 " end to poll data", tmq->consumerId);
X
Xiaoyu Wang 已提交
1781 1782 1783
  return 0;
}

D
dapan1121 已提交
1784
static int32_t tmqHandleNoPollRsp(tmq_t* tmq, SMqRspWrapper* rspWrapper, bool* pReset) {
L
Liu Jicong 已提交
1785
  if (rspWrapper->tmqRspType == TMQ_MSG_TYPE__EP_RSP) {
L
fix  
Liu Jicong 已提交
1786
    /*printf("ep %d %d\n", rspMsg->head.epoch, tmq->epoch);*/
L
Liu Jicong 已提交
1787 1788
    if (rspWrapper->epoch > atomic_load_32(&tmq->epoch)) {
      SMqAskEpRspWrapper* pEpRspWrapper = (SMqAskEpRspWrapper*)rspWrapper;
L
Liu Jicong 已提交
1789
      SMqAskEpRsp*        rspMsg = &pEpRspWrapper->msg;
L
Liu Jicong 已提交
1790
      tmqUpdateEp(tmq, rspWrapper->epoch, rspMsg);
L
temp  
Liu Jicong 已提交
1791
      /*tmqClearUnhandleMsg(tmq);*/
L
Liu Jicong 已提交
1792
      tDeleteSMqAskEpRsp(rspMsg);
X
Xiaoyu Wang 已提交
1793 1794
      *pReset = true;
    } else {
L
Liu Jicong 已提交
1795
      tmqFreeRspWrapper(rspWrapper);
X
Xiaoyu Wang 已提交
1796 1797 1798 1799 1800 1801 1802 1803
      *pReset = false;
    }
  } else {
    return -1;
  }
  return 0;
}

D
dapan1121 已提交
1804
static void* tmqHandleAllRsp(tmq_t* tmq, int64_t timeout, bool pollIfReset) {
H
Haojun Liao 已提交
1805
  tscDebug("consumer:0x%" PRIx64 " start to handle the rsp, total:%d", tmq->consumerId, tmq->qall->numOfItems);
1806

X
Xiaoyu Wang 已提交
1807
  while (1) {
D
dapan1121 已提交
1808 1809
    SMqRspWrapper* pRspWrapper = NULL;
    taosGetQitem(tmq->qall, (void**)&pRspWrapper);
1810

D
dapan1121 已提交
1811
    if (pRspWrapper == NULL) {
L
Liu Jicong 已提交
1812
      taosReadAllQitems(tmq->mqueue, tmq->qall);
D
dapan1121 已提交
1813
      taosGetQitem(tmq->qall, (void**)&pRspWrapper);
L
Liu Jicong 已提交
1814

D
dapan1121 已提交
1815
      if (pRspWrapper == NULL) {
L
Liu Jicong 已提交
1816 1817
        return NULL;
      }
X
Xiaoyu Wang 已提交
1818 1819
    }

D
dapan1121 已提交
1820 1821 1822 1823
    tscDebug("consumer:0x%"PRIx64" handle rsp, type:%d", tmq->consumerId, pRspWrapper->tmqRspType);

    if (pRspWrapper->tmqRspType == TMQ_MSG_TYPE__END_RSP) {
      taosFreeQitem(pRspWrapper);
L
Liu Jicong 已提交
1824
      terrno = TSDB_CODE_TQ_NO_COMMITTED_OFFSET;
H
Haojun Liao 已提交
1825
      tscError("consumer:0x%" PRIx64 " unexpected rsp from poll, code:%s", tmq->consumerId, tstrerror(terrno));
L
Liu Jicong 已提交
1826
      return NULL;
D
dapan1121 已提交
1827 1828
    } else if (pRspWrapper->tmqRspType == TMQ_MSG_TYPE__POLL_RSP) {
      SMqPollRspWrapper* pollRspWrapper = (SMqPollRspWrapper*)pRspWrapper;
H
Haojun Liao 已提交
1829

1830
      int32_t consumerEpoch = atomic_load_32(&tmq->epoch);
D
dapan1121 已提交
1831 1832 1833 1834
      SMqDataRsp* pDataRsp = &pollRspWrapper->dataRsp;

      if (pDataRsp->head.epoch == consumerEpoch) {
        // todo fix it: race condition
L
Liu Jicong 已提交
1835
        SMqClientVg* pVg = pollRspWrapper->vgHandle;
H
Haojun Liao 已提交
1836

D
dapan1121 已提交
1837 1838 1839 1840 1841 1842 1843
        // update the epset
        if (pollRspWrapper->pEpset != NULL) {
          SEp* pEp = GET_ACTIVE_EP(pollRspWrapper->pEpset);
          SEp* pOld = GET_ACTIVE_EP(&(pVg->epSet));
          tscDebug("consumer:0x%" PRIx64 " update epset vgId:%d, ep:%s:%d, old ep:%s:%d", tmq->consumerId,
                   pVg->vgId, pEp->fqdn, pEp->port, pOld->fqdn, pOld->port);
          pVg->epSet = *pollRspWrapper->pEpset;
L
temp  
Liu Jicong 已提交
1844
        }
H
Haojun Liao 已提交
1845

D
dapan1121 已提交
1846 1847
        pVg->currentOffset = pDataRsp->rspOffset;
        atomic_store_32(&pVg->vgStatus, TMQ_VG_STATUS__IDLE);
H
Haojun Liao 已提交
1848

D
dapan1121 已提交
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
        char buf[80];
        tFormatOffset(buf, 80, &pDataRsp->rspOffset);
        if (pDataRsp->blockNum == 0) {
          tscDebug("consumer:0x%" PRIx64 " empty block received, vgId:%d, offset:%s, vg total:%"PRId64" total:%"PRId64" reqId:0x%" PRIx64, tmq->consumerId,
                   pVg->vgId, buf, pVg->numOfRows, tmq->totalRows, pollRspWrapper->reqId);
          pRspWrapper = tmqFreeRspWrapper(pRspWrapper);
          taosFreeQitem(pollRspWrapper);
        } else {  // build rsp
          int64_t numOfRows = 0;
          SMqRspObj* pRsp = tmqBuildRspFromWrapper(pollRspWrapper, pVg, &numOfRows);
          tmq->totalRows += numOfRows;

          tscDebug("consumer:0x%" PRIx64 " process poll rsp, vgId:%d, offset:%s, blocks:%d, rows:%" PRId64
                   " vg total:%" PRId64 " total:%" PRId64 ", reqId:0x%" PRIx64,
                   tmq->consumerId, pVg->vgId, buf, pDataRsp->blockNum, numOfRows, pVg->numOfRows, tmq->totalRows,
                   pollRspWrapper->reqId);
          taosFreeQitem(pollRspWrapper);
          return pRsp;
        }
X
Xiaoyu Wang 已提交
1868
      } else {
D
dapan1121 已提交
1869 1870 1871
        tscDebug("consumer:0x%" PRIx64 " vgId:%d msg discard since epoch mismatch: msg epoch %d, consumer epoch %d",
                 tmq->consumerId, pollRspWrapper->vgId, pDataRsp->head.epoch, consumerEpoch);
        pRspWrapper = tmqFreeRspWrapper(pRspWrapper);
L
Liu Jicong 已提交
1872 1873
        taosFreeQitem(pollRspWrapper);
      }
D
dapan1121 已提交
1874 1875
    } else if (pRspWrapper->tmqRspType == TMQ_MSG_TYPE__POLL_META_RSP) {
      SMqPollRspWrapper* pollRspWrapper = (SMqPollRspWrapper*)pRspWrapper;
L
Liu Jicong 已提交
1876
      int32_t            consumerEpoch = atomic_load_32(&tmq->epoch);
1877 1878 1879

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

L
Liu Jicong 已提交
1880
      if (pollRspWrapper->metaRsp.head.epoch == consumerEpoch) {
L
Liu Jicong 已提交
1881
        SMqClientVg* pVg = pollRspWrapper->vgHandle;
wmmhello's avatar
wmmhello 已提交
1882
        pVg->currentOffset = pollRspWrapper->metaRsp.rspOffset;
L
Liu Jicong 已提交
1883 1884
        atomic_store_32(&pVg->vgStatus, TMQ_VG_STATUS__IDLE);
        // build rsp
L
Liu Jicong 已提交
1885
        SMqMetaRspObj* pRsp = tmqBuildMetaRspFromWrapper(pollRspWrapper);
L
Liu Jicong 已提交
1886 1887 1888
        taosFreeQitem(pollRspWrapper);
        return pRsp;
      } else {
D
dapan1121 已提交
1889 1890 1891
        tscDebug("consumer:0x%" PRIx64 " vgId:%d msg discard since epoch mismatch: msg epoch %d, consumer epoch %d",
                 tmq->consumerId, pollRspWrapper->vgId, pollRspWrapper->metaRsp.head.epoch, consumerEpoch);
        pRspWrapper = tmqFreeRspWrapper(pRspWrapper);
L
Liu Jicong 已提交
1892
        taosFreeQitem(pollRspWrapper);
X
Xiaoyu Wang 已提交
1893
      }
D
dapan1121 已提交
1894 1895
    } else if (pRspWrapper->tmqRspType == TMQ_MSG_TYPE__TAOSX_RSP) {
      SMqPollRspWrapper* pollRspWrapper = (SMqPollRspWrapper*)pRspWrapper;
L
Liu Jicong 已提交
1896
      int32_t consumerEpoch = atomic_load_32(&tmq->epoch);
D
dapan1121 已提交
1897

L
Liu Jicong 已提交
1898 1899 1900 1901
      if (pollRspWrapper->taosxRsp.head.epoch == consumerEpoch) {
        SMqClientVg* pVg = pollRspWrapper->vgHandle;
        pVg->currentOffset = pollRspWrapper->taosxRsp.rspOffset;
        atomic_store_32(&pVg->vgStatus, TMQ_VG_STATUS__IDLE);
D
dapan1121 已提交
1902

L
Liu Jicong 已提交
1903
        if (pollRspWrapper->taosxRsp.blockNum == 0) {
D
dapan1121 已提交
1904 1905 1906 1907
          tscDebug("consumer:0x%" PRIx64 " taosx empty block received, vgId:%d, vg total:%" PRId64 " reqId:0x%" PRIx64,
                   tmq->consumerId, pVg->vgId, pVg->numOfRows, pollRspWrapper->reqId);
          pVg->emptyBlockReceiveTs = taosGetTimestampMs();
          pRspWrapper = tmqFreeRspWrapper(pRspWrapper);
L
Liu Jicong 已提交
1908 1909
          taosFreeQitem(pollRspWrapper);
          continue;
D
dapan1121 已提交
1910 1911
        } else {
          pVg->emptyBlockReceiveTs = 0; // reset the ts
L
Liu Jicong 已提交
1912
        }
wmmhello's avatar
wmmhello 已提交
1913

L
Liu Jicong 已提交
1914
        // build rsp
wmmhello's avatar
wmmhello 已提交
1915
        void* pRsp = NULL;
D
dapan1121 已提交
1916
        int64_t numOfRows = 0;
L
Liu Jicong 已提交
1917
        if (pollRspWrapper->taosxRsp.createTableNum == 0) {
D
dapan1121 已提交
1918
          pRsp = tmqBuildRspFromWrapper(pollRspWrapper, pVg, &numOfRows);
L
Liu Jicong 已提交
1919
        } else {
wmmhello's avatar
wmmhello 已提交
1920 1921
          pRsp = tmqBuildTaosxRspFromWrapper(pollRspWrapper);
        }
D
dapan1121 已提交
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931

        tmq->totalRows += numOfRows;

        char buf[80];
        tFormatOffset(buf, 80, &pVg->currentOffset);
        tscDebug("consumer:0x%" PRIx64 " process taosx poll rsp, vgId:%d, offset:%s, blocks:%d, rows:%" PRId64
                 ", vg total:%" PRId64 " total:%"PRId64" reqId:0x%" PRIx64,
                 tmq->consumerId, pVg->vgId, buf, pollRspWrapper->dataRsp.blockNum, numOfRows, pVg->numOfRows,
                 tmq->totalRows, pollRspWrapper->reqId);

L
Liu Jicong 已提交
1932 1933
        taosFreeQitem(pollRspWrapper);
        return pRsp;
D
dapan1121 已提交
1934

L
Liu Jicong 已提交
1935
      } else {
D
dapan1121 已提交
1936 1937 1938
        tscDebug("consumer:0x%" PRIx64 " vgId:%d msg discard since epoch mismatch: msg epoch %d, consumer epoch %d",
                 tmq->consumerId, pollRspWrapper->vgId, pollRspWrapper->taosxRsp.head.epoch, consumerEpoch);
        pRspWrapper = tmqFreeRspWrapper(pRspWrapper);
L
Liu Jicong 已提交
1939 1940
        taosFreeQitem(pollRspWrapper);
      }
X
Xiaoyu Wang 已提交
1941
    } else {
D
dapan1121 已提交
1942 1943
      tscDebug("consumer:0x%" PRIx64 " not data msg received", tmq->consumerId);

X
Xiaoyu Wang 已提交
1944
      bool reset = false;
D
dapan1121 已提交
1945 1946
      tmqHandleNoPollRsp(tmq, pRspWrapper, &reset);
      taosFreeQitem(pRspWrapper);
X
Xiaoyu Wang 已提交
1947
      if (pollIfReset && reset) {
1948
        tscDebug("consumer:0x%" PRIx64 ", reset and repoll", tmq->consumerId);
1949
        tmqPollImpl(tmq, timeout);
X
Xiaoyu Wang 已提交
1950 1951 1952 1953 1954
      }
    }
  }
}

1955
TAOS_RES* tmq_consumer_poll(tmq_t* tmq, int64_t timeout) {
L
Liu Jicong 已提交
1956 1957
  void*   rspObj;
  int64_t startTime = taosGetTimestampMs();
L
Liu Jicong 已提交
1958

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

1961 1962 1963
#if 0
  tmqHandleAllDelayedTask(tmq);
  tmqPollImpl(tmq, timeout);
1964
  rspObj = tmqHandleAllRsp(tmq, timeout, false);
L
Liu Jicong 已提交
1965 1966
  if (rspObj) {
    return (TAOS_RES*)rspObj;
L
fix  
Liu Jicong 已提交
1967
  }
1968
#endif
X
Xiaoyu Wang 已提交
1969

1970
  // in no topic status, delayed task also need to be processed
L
Liu Jicong 已提交
1971
  if (atomic_load_8(&tmq->status) == TMQ_CONSUMER_STATUS__INIT) {
1972
    tscDebug("consumer:0x%" PRIx64 " poll return since consumer is init", tmq->consumerId);
1973
    taosMsleep(500);  //     sleep for a while
1974 1975 1976
    return NULL;
  }

D
dapan1121 已提交
1977
  while (atomic_load_8(&tmq->status) == TMQ_CONSUMER_STATUS__RECOVER) {
L
Liu Jicong 已提交
1978 1979
    int32_t retryCnt = 0;
    while (TSDB_CODE_MND_CONSUMER_NOT_READY == tmqAskEp(tmq, false)) {
H
Haojun Liao 已提交
1980
      if (retryCnt++ > 40) {
L
Liu Jicong 已提交
1981 1982
        return NULL;
      }
1983

H
Haojun Liao 已提交
1984
      tscDebug("consumer:0x%" PRIx64 " not ready, retry:%d/40 in 500ms", tmq->consumerId, retryCnt);
L
Liu Jicong 已提交
1985 1986 1987 1988
      taosMsleep(500);
    }
  }

X
Xiaoyu Wang 已提交
1989
  while (1) {
L
Liu Jicong 已提交
1990
    tmqHandleAllDelayedTask(tmq);
1991

L
Liu Jicong 已提交
1992
    if (tmqPollImpl(tmq, timeout) < 0) {
1993
      tscDebug("consumer:0x%" PRIx64 " return due to poll error", tmq->consumerId);
L
Liu Jicong 已提交
1994
    }
L
Liu Jicong 已提交
1995

1996
    rspObj = tmqHandleAllRsp(tmq, timeout, false);
L
Liu Jicong 已提交
1997
    if (rspObj) {
1998
      tscDebug("consumer:0x%" PRIx64 " return rsp %p", tmq->consumerId, rspObj);
L
Liu Jicong 已提交
1999
      return (TAOS_RES*)rspObj;
L
Liu Jicong 已提交
2000
    } else if (terrno == TSDB_CODE_TQ_NO_COMMITTED_OFFSET) {
2001
      tscDebug("consumer:0x%" PRIx64 " return null since no committed offset", tmq->consumerId);
L
Liu Jicong 已提交
2002
      return NULL;
X
Xiaoyu Wang 已提交
2003
    }
2004

D
dapan1121 已提交
2005
    if (timeout >= 0) {
L
Liu Jicong 已提交
2006
      int64_t currentTime = taosGetTimestampMs();
2007 2008 2009
      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 已提交
2010
                 tmq->consumerId, tmq->epoch, startTime, currentTime);
X
Xiaoyu Wang 已提交
2011 2012
        return NULL;
      }
2013
      tsem_timewait(&tmq->rspSem, (timeout - elapsedTime));
L
Liu Jicong 已提交
2014 2015
    } else {
      // use tsem_timewait instead of tsem_wait to avoid unexpected stuck
L
Liu Jicong 已提交
2016
      tsem_timewait(&tmq->rspSem, 1000);
X
Xiaoyu Wang 已提交
2017 2018 2019 2020
    }
  }
}

D
dapan1121 已提交
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
static void displayConsumeStatistics(const tmq_t* pTmq) {
  int32_t numOfTopics = taosArrayGetSize(pTmq->clientTopics);
  tscDebug("consumer:0x%" PRIx64 " closing poll:%" PRId64 " rows:%" PRId64 " topics:%d, final epoch:%d",
           pTmq->consumerId, pTmq->pollCnt, pTmq->totalRows, numOfTopics, pTmq->epoch);

  tscDebug("consumer:0x%" PRIx64 " rows dist begin: ", pTmq->consumerId);
  for (int32_t i = 0; i < numOfTopics; ++i) {
    SMqClientTopic* pTopics = taosArrayGet(pTmq->clientTopics, i);

    tscDebug("consumer:0x%" PRIx64 " topic:%d", pTmq->consumerId, i);
    int32_t numOfVgs = taosArrayGetSize(pTopics->vgs);
    for (int32_t j = 0; j < numOfVgs; ++j) {
      SMqClientVg* pVg = taosArrayGet(pTopics->vgs, j);
      tscDebug("topic:%s, %d. vgId:%d rows:%" PRId64, pTopics->topicName, j, pVg->vgId, pVg->numOfRows);
    }
  }

  tscDebug("consumer:0x%" PRIx64 " rows dist end", pTmq->consumerId);
}

L
Liu Jicong 已提交
2041
int32_t tmq_consumer_close(tmq_t* tmq) {
D
dapan1121 已提交
2042 2043 2044
  tscDebug("consumer:0x%" PRIx64" start to close consumer, status:%d", tmq->consumerId, tmq->status);
  displayConsumeStatistics(tmq);

2045
  if (tmq->status == TMQ_CONSUMER_STATUS__READY) {
D
dapan1121 已提交
2046 2047 2048 2049 2050 2051
    // if auto commit is set, commit before close consumer. Otherwise, do nothing.
    if (tmq->autoCommit) {
      int32_t rsp = tmq_commit_sync(tmq, NULL);
      if (rsp != 0) {
        return rsp;
      }
2052 2053
    }

L
Liu Jicong 已提交
2054
    int32_t     retryCnt = 0;
2055
    tmq_list_t* lst = tmq_list_new();
L
Liu Jicong 已提交
2056
    while (1) {
D
dapan1121 已提交
2057
      int32_t rsp = tmq_subscribe(tmq, lst);
L
Liu Jicong 已提交
2058 2059 2060 2061 2062 2063 2064 2065
      if (rsp != TSDB_CODE_MND_CONSUMER_NOT_READY || retryCnt > 5) {
        break;
      } else {
        retryCnt++;
        taosMsleep(500);
      }
    }

2066
    tmq_list_destroy(lst);
D
dapan1121 已提交
2067 2068
  } else {
    tscWarn("consumer:0x%" PRIx64" not in ready state, close it directly", tmq->consumerId);
L
Liu Jicong 已提交
2069
  }
D
dapan1121 已提交
2070

2071
  taosRemoveRef(tmqMgmt.rsetId, tmq->refId);
L
Liu Jicong 已提交
2072
  return 0;
2073
}
L
Liu Jicong 已提交
2074

L
Liu Jicong 已提交
2075 2076
const char* tmq_err2str(int32_t err) {
  if (err == 0) {
L
Liu Jicong 已提交
2077
    return "success";
L
Liu Jicong 已提交
2078
  } else if (err == -1) {
L
Liu Jicong 已提交
2079 2080 2081
    return "fail";
  } else {
    return tstrerror(err);
L
Liu Jicong 已提交
2082 2083
  }
}
L
Liu Jicong 已提交
2084

L
Liu Jicong 已提交
2085 2086 2087 2088 2089
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;
2090 2091
  } else if (TD_RES_TMQ_METADATA(res)) {
    return TMQ_RES_METADATA;
L
Liu Jicong 已提交
2092 2093 2094 2095 2096
  } else {
    return TMQ_RES_INVALID;
  }
}

L
Liu Jicong 已提交
2097
const char* tmq_get_topic_name(TAOS_RES* res) {
L
Liu Jicong 已提交
2098 2099
  if (TD_RES_TMQ(res)) {
    SMqRspObj* pRspObj = (SMqRspObj*)res;
L
Liu Jicong 已提交
2100
    return strchr(pRspObj->topic, '.') + 1;
L
Liu Jicong 已提交
2101 2102 2103
  } else if (TD_RES_TMQ_META(res)) {
    SMqMetaRspObj* pMetaRspObj = (SMqMetaRspObj*)res;
    return strchr(pMetaRspObj->topic, '.') + 1;
2104 2105 2106
  } else if (TD_RES_TMQ_METADATA(res)) {
    SMqTaosxRspObj* pRspObj = (SMqTaosxRspObj*)res;
    return strchr(pRspObj->topic, '.') + 1;
L
Liu Jicong 已提交
2107 2108 2109 2110 2111
  } else {
    return NULL;
  }
}

L
Liu Jicong 已提交
2112 2113 2114 2115
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 已提交
2116 2117 2118
  } else if (TD_RES_TMQ_META(res)) {
    SMqMetaRspObj* pMetaRspObj = (SMqMetaRspObj*)res;
    return strchr(pMetaRspObj->db, '.') + 1;
2119 2120 2121
  } else if (TD_RES_TMQ_METADATA(res)) {
    SMqTaosxRspObj* pRspObj = (SMqTaosxRspObj*)res;
    return strchr(pRspObj->db, '.') + 1;
L
Liu Jicong 已提交
2122 2123 2124 2125 2126
  } else {
    return NULL;
  }
}

L
Liu Jicong 已提交
2127 2128 2129 2130
int32_t tmq_get_vgroup_id(TAOS_RES* res) {
  if (TD_RES_TMQ(res)) {
    SMqRspObj* pRspObj = (SMqRspObj*)res;
    return pRspObj->vgId;
L
Liu Jicong 已提交
2131 2132 2133
  } else if (TD_RES_TMQ_META(res)) {
    SMqMetaRspObj* pMetaRspObj = (SMqMetaRspObj*)res;
    return pMetaRspObj->vgId;
2134
  } else if (TD_RES_TMQ_METADATA(res)) {
2135 2136
    SMqTaosxRspObj* pRspObj = (SMqTaosxRspObj*)res;
    return pRspObj->vgId;
L
Liu Jicong 已提交
2137 2138 2139 2140
  } else {
    return -1;
  }
}
L
Liu Jicong 已提交
2141 2142 2143 2144 2145 2146 2147 2148

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;
    }
2149
    return (const char*)taosArrayGetP(pRspObj->rsp.blockTbName, pRspObj->resIter);
2150 2151
  } else if (TD_RES_TMQ_METADATA(res)) {
    SMqTaosxRspObj* pRspObj = (SMqTaosxRspObj*)res;
L
Liu Jicong 已提交
2152 2153 2154
    if (!pRspObj->rsp.withTbName || pRspObj->rsp.blockTbName == NULL || pRspObj->resIter < 0 ||
        pRspObj->resIter >= pRspObj->rsp.blockNum) {
      return NULL;
2155
    }
L
Liu Jicong 已提交
2156 2157
    return (const char*)taosArrayGetP(pRspObj->rsp.blockTbName, pRspObj->resIter);
  }
L
Liu Jicong 已提交
2158 2159
  return NULL;
}
2160

L
Liu Jicong 已提交
2161
void tmq_commit_async(tmq_t* tmq, const TAOS_RES* msg, tmq_commit_cb* cb, void* param) {
L
Liu Jicong 已提交
2162
  tmqCommitInner(tmq, msg, 0, 1, cb, param);
L
Liu Jicong 已提交
2163 2164
}

2165
int32_t tmq_commit_sync(tmq_t* tmq, const TAOS_RES* msg) {
L
Liu Jicong 已提交
2166
  return tmqCommitInner(tmq, msg, 0, 0, NULL, NULL);
2167
}
D
dapan1121 已提交
2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303

int32_t tmqAskEp(tmq_t* tmq, bool async) {
  int32_t code = TSDB_CODE_SUCCESS;
#if 0
  int8_t  epStatus = atomic_val_compare_exchange_8(&tmq->epStatus, 0, 1);
  if (epStatus == 1) {
    int32_t epSkipCnt = atomic_add_fetch_32(&tmq->epSkipCnt, 1);
    tscTrace("consumer:0x%" PRIx64 ", skip ask ep cnt %d", tmq->consumerId, epSkipCnt);
    if (epSkipCnt < 5000) return 0;
  }
  atomic_store_32(&tmq->epSkipCnt, 0);
#endif

  SMqAskEpReq req = {0};
  req.consumerId = tmq->consumerId;
  req.epoch = tmq->epoch;
  strcpy(req.cgroup, tmq->groupId);

  int32_t tlen = tSerializeSMqAskEpReq(NULL, 0, &req);
  if (tlen < 0) {
    tscError("consumer:0x%" PRIx64 ", tSerializeSMqAskEpReq failed", tmq->consumerId);
    return -1;
  }

  void* pReq = taosMemoryCalloc(1, tlen);
  if (pReq == NULL) {
    tscError("consumer:0x%" PRIx64 ", failed to malloc askEpReq msg, size:%d", tmq->consumerId, tlen);
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  if (tSerializeSMqAskEpReq(pReq, tlen, &req) < 0) {
    tscError("consumer:0x%" PRIx64 ", tSerializeSMqAskEpReq %d failed", tmq->consumerId, tlen);
    taosMemoryFree(pReq);
    return -1;
  }

  SMqAskEpCbParam* pParam = taosMemoryCalloc(1, sizeof(SMqAskEpCbParam));
  if (pParam == NULL) {
    tscError("consumer:0x%" PRIx64 ", failed to malloc subscribe param", tmq->consumerId);
    taosMemoryFree(pReq);
    return -1;
  }

  pParam->refId = tmq->refId;
  pParam->epoch = tmq->epoch;
  pParam->async = async;
  tsem_init(&pParam->rspSem, 0, 0);

  SMsgSendInfo* sendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
  if (sendInfo == NULL) {
    tsem_destroy(&pParam->rspSem);
    taosMemoryFree(pParam);
    taosMemoryFree(pReq);
    return -1;
  }

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

  sendInfo->requestId = generateRequestId();
  sendInfo->requestObjRefId = 0;
  sendInfo->param = pParam;
  sendInfo->fp = tmqAskEpCb;
  sendInfo->msgType = TDMT_MND_TMQ_ASK_EP;

  SEpSet epSet = getEpSet_s(&tmq->pTscObj->pAppInfo->mgmtEp);
  tscDebug("consumer:0x%" PRIx64 " ask ep from mnode, async:%d, reqId:0x%" PRIx64, tmq->consumerId, async,
           sendInfo->requestId);

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

  if (!async) {
    tsem_wait(&pParam->rspSem);
    code = pParam->code;
    taosMemoryFree(pParam);
  }

  return code;
}

int32_t makeTopicVgroupKey(char* dst, const char* topicName, int32_t vg) {
  return sprintf(dst, "%s:%d", topicName, vg);
}

int32_t tmqCommitDone(SMqCommitCbParamSet* pParamSet) {
  int64_t refId = pParamSet->refId;

  tmq_t* tmq = taosAcquireRef(tmqMgmt.rsetId, 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

  taosReleaseRef(tmqMgmt.rsetId, refId);
  return 0;
}

void tmqCommitRspCountDown(SMqCommitCbParamSet* pParamSet, int64_t consumerId, const char* pTopic, int32_t vgId) {
  int32_t waitingRspNum = atomic_sub_fetch_32(&pParamSet->waitingRspNum, 1);
  if (waitingRspNum == 0) {
    tscDebug("consumer:0x%" PRIx64 " topic:%s vgId:%d all commit-rsp received, commit completed", consumerId, pTopic,
             vgId);
    tmqCommitDone(pParamSet);
  } else {
    tscDebug("consumer:0x%" PRIx64 " topic:%s vgId:%d commit-rsp received, remain:%d", consumerId, pTopic, vgId,
             waitingRspNum);
  }
}