tmqSim.c 15.7 KB
Newer Older
P
plum-lihui 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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/>.
 */

#include <assert.h>
#include <stdio.h>
L
Liu Jicong 已提交
18
#include <stdlib.h>
P
plum-lihui 已提交
19 20 21
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
L
Liu Jicong 已提交
22
#include <time.h>
P
plum-lihui 已提交
23 24 25 26 27

#include "taos.h"
#include "taoserror.h"
#include "tlog.h"

L
Liu Jicong 已提交
28 29
#define GREEN     "\033[1;32m"
#define NC        "\033[0m"
P
plum-lihui 已提交
30 31
#define min(a, b) (((a) < (b)) ? (a) : (b))

P
plum-lihui 已提交
32 33 34
#define MAX_SQL_STR_LEN            (1024 * 1024)
#define MAX_ROW_STR_LEN            (16 * 1024)
#define MAX_CONSUMER_THREAD_CNT    (16)
P
plum-lihui 已提交
35

P
plum-lihui 已提交
36
typedef struct {
P
plum-lihui 已提交
37 38
  TdThread    thread;
  int32_t     consumerId;
P
plum-lihui 已提交
39

P
plum-lihui 已提交
40 41 42 43 44 45
  int32_t     ifCheckData;
  int64_t     expectMsgCnt; 
  
  int64_t     consumeMsgCnt;
  int64_t     consumeRowCnt;
  int32_t     checkresult;
P
plum-lihui 已提交
46

P
plum-lihui 已提交
47 48
  char        topicString[1024];
  char        keyString[1024];
P
plum-lihui 已提交
49

P
plum-lihui 已提交
50 51
  int32_t     numOfTopic;
  char        topics[32][64];
P
plum-lihui 已提交
52

P
plum-lihui 已提交
53 54 55
  int32_t     numOfKey;
  char        key[32][64];
  char        value[32][64];  
P
plum-lihui 已提交
56 57 58

  tmq_t*      tmq;
  tmq_list_t* topicList;
P
plum-lihui 已提交
59
  
P
plum-lihui 已提交
60 61
} SThreadInfo;

P
plum-lihui 已提交
62
typedef struct {
L
Liu Jicong 已提交
63
  // input from argvs
P
plum-lihui 已提交
64 65 66 67 68 69 70
  char           cdbName[32];
  char           dbName[32];
  int32_t        showMsgFlag;
  int32_t        showRowFlag;
  int32_t        consumeDelay;  // unit s
  int32_t        numOfThread;  
  SThreadInfo    stThreads[MAX_CONSUMER_THREAD_CNT];
P
plum-lihui 已提交
71 72 73
} SConfInfo;

static SConfInfo g_stConfInfo;
P
plum-lihui 已提交
74
TdFilePtr g_fp = NULL;
P
plum-lihui 已提交
75

L
Liu Jicong 已提交
76 77
// char* g_pRowValue = NULL;
// TdFilePtr g_fp = NULL;
P
plum-lihui 已提交
78 79 80 81 82 83 84 85 86 87 88

static void printHelp() {
  char indent[10] = "        ";
  printf("Used to test the tmq feature with sim cases\n");

  printf("%s%s\n", indent, "-c");
  printf("%s%s%s%s\n", indent, indent, "Configuration directory, default is ", configDir);
  printf("%s%s\n", indent, "-d");
  printf("%s%s%s\n", indent, indent, "The name of the database for cosumer, no default ");
  printf("%s%s\n", indent, "-g");
  printf("%s%s%s%d\n", indent, indent, "showMsgFlag, default is ", g_stConfInfo.showMsgFlag);
P
plum-lihui 已提交
89 90
  printf("%s%s\n", indent, "-r");
  printf("%s%s%s%d\n", indent, indent, "showRowFlag, default is ", g_stConfInfo.showRowFlag);
P
plum-lihui 已提交
91 92
  printf("%s%s\n", indent, "-y");
  printf("%s%s%s%d\n", indent, indent, "consume delay, default is s", g_stConfInfo.consumeDelay);
P
plum-lihui 已提交
93 94 95
  exit(EXIT_SUCCESS);
}

P
plum-lihui 已提交
96

P
plum-lihui 已提交
97 98
void initLogFile() {
  // FILE *fp = fopen(g_stConfInfo.resultFileName, "a");
P
plum-lihui 已提交
99 100
  char file[256];
  sprintf(file, "%s/../log/tmqlog.txt", configDir);
P
plum-lihui 已提交
101
  TdFilePtr pFile = taosOpenFile(file, TD_FILE_TEXT | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM);
P
plum-lihui 已提交
102 103
  if (NULL == pFile) {
    fprintf(stderr, "Failed to open %s for save result\n", "./tmqlog.txt");
104 105
    exit(-1);
  }
P
plum-lihui 已提交
106
  g_fp = pFile;
P
plum-lihui 已提交
107
}
P
plum-lihui 已提交
108

P
plum-lihui 已提交
109 110

void saveConfigToLogFile() {
P
plum-lihui 已提交
111 112 113
  time_t    tTime = taosGetTimestampSec();
  struct tm tm = *taosLocalTime(&tTime, NULL);

P
plum-lihui 已提交
114 115 116 117 118 119 120
  taosFprintfFile(g_fp, "###################################################################\n");
  taosFprintfFile(g_fp, "# configDir:           %s\n",  configDir);
  taosFprintfFile(g_fp, "# dbName:              %s\n",  g_stConfInfo.dbName);
  taosFprintfFile(g_fp, "# cdbName:             %s\n",  g_stConfInfo.cdbName);
  taosFprintfFile(g_fp, "# showMsgFlag:         %d\n",  g_stConfInfo.showMsgFlag);
  taosFprintfFile(g_fp, "# showRowFlag:         %d\n",  g_stConfInfo.showRowFlag);
  taosFprintfFile(g_fp, "# consumeDelay:        %d\n",  g_stConfInfo.consumeDelay);
P
plum-lihui 已提交
121
  taosFprintfFile(g_fp, "# numOfThread:         %d\n",  g_stConfInfo.numOfThread);
P
plum-lihui 已提交
122 123 124 125

  for (int32_t i = 0; i < g_stConfInfo.numOfThread; i++) {	  
    taosFprintfFile(g_fp, "# consumer %d info:\n", g_stConfInfo.stThreads[i].consumerId);
	taosFprintfFile(g_fp, "  Topics: ");
P
plum-lihui 已提交
126 127
    for (int j = 0 ; j < g_stConfInfo.stThreads[i].numOfTopic; j++) {
    taosFprintfFile(g_fp, "%s, ",  g_stConfInfo.stThreads[i].topics[j]);
P
plum-lihui 已提交
128
    }
P
plum-lihui 已提交
129 130
    taosFprintfFile(g_fp, "\n");  
    taosFprintfFile(g_fp, "  Key: ");
P
plum-lihui 已提交
131 132
    for (int k = 0 ; k < g_stConfInfo.stThreads[i].numOfKey; k++) {
      taosFprintfFile(g_fp, "%s:%s, ",  g_stConfInfo.stThreads[i].key[k], g_stConfInfo.stThreads[i].value[k]);
P
plum-lihui 已提交
133
    }
P
plum-lihui 已提交
134
    taosFprintfFile(g_fp, "\n");
P
plum-lihui 已提交
135
  }
P
plum-lihui 已提交
136 137 138 139
  
  taosFprintfFile(g_fp, "# Test time:                %d-%02d-%02d %02d:%02d:%02d\n", tm.tm_year + 1900, tm.tm_mon + 1,
                                                                           tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
  taosFprintfFile(g_fp, "###################################################################\n");
P
plum-lihui 已提交
140 141
}

L
Liu Jicong 已提交
142
void parseArgument(int32_t argc, char* argv[]) {
P
plum-lihui 已提交
143
  memset(&g_stConfInfo, 0, sizeof(SConfInfo));
L
Liu Jicong 已提交
144
  g_stConfInfo.showMsgFlag = 0;
P
plum-lihui 已提交
145
  g_stConfInfo.showRowFlag = 0;
P
plum-lihui 已提交
146
  g_stConfInfo.consumeDelay = 5;
L
Liu Jicong 已提交
147

P
plum-lihui 已提交
148 149 150 151 152 153
  for (int32_t i = 1; i < argc; i++) {
    if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
      printHelp();
      exit(0);
    } else if (strcmp(argv[i], "-d") == 0) {
      strcpy(g_stConfInfo.dbName, argv[++i]);
P
plum-lihui 已提交
154 155
    } else if (strcmp(argv[i], "-w") == 0) {
      strcpy(g_stConfInfo.cdbName, argv[++i]);
P
plum-lihui 已提交
156 157 158 159
    } else if (strcmp(argv[i], "-c") == 0) {
      strcpy(configDir, argv[++i]);
    } else if (strcmp(argv[i], "-g") == 0) {
      g_stConfInfo.showMsgFlag = atol(argv[++i]);
P
plum-lihui 已提交
160 161
    } else if (strcmp(argv[i], "-r") == 0) {
      g_stConfInfo.showRowFlag = atol(argv[++i]);
P
plum-lihui 已提交
162 163
    } else if (strcmp(argv[i], "-y") == 0) {
      g_stConfInfo.consumeDelay = atol(argv[++i]);
P
plum-lihui 已提交
164 165
    } else {
      printf("%s unknow para: %s %s", GREEN, argv[++i], NC);
L
Liu Jicong 已提交
166
      exit(-1);
P
plum-lihui 已提交
167 168 169
    }
  }

P
plum-lihui 已提交
170 171 172 173
  initLogFile();
  
  taosFprintfFile(g_fp, "====parseArgument() success\n");

P
plum-lihui 已提交
174
#if 1
P
plum-lihui 已提交
175 176
  pPrint("%s configDir:%s %s", GREEN, configDir, NC);
  pPrint("%s dbName:%s %s", GREEN, g_stConfInfo.dbName, NC);
P
plum-lihui 已提交
177
  pPrint("%s cdbName:%s %s", GREEN, g_stConfInfo.cdbName, NC);
P
plum-lihui 已提交
178
  pPrint("%s consumeDelay:%d %s", GREEN, g_stConfInfo.consumeDelay, NC);
P
plum-lihui 已提交
179
  pPrint("%s showMsgFlag:%d %s", GREEN, g_stConfInfo.showMsgFlag, NC);
P
plum-lihui 已提交
180
  pPrint("%s showRowFlag:%d %s", GREEN, g_stConfInfo.showRowFlag, NC);
L
Liu Jicong 已提交
181
#endif
P
plum-lihui 已提交
182 183
}

L
Liu Jicong 已提交
184 185 186
void splitStr(char** arr, char* str, const char* del) {
  char* s = strtok(str, del);
  while (s != NULL) {
P
plum-lihui 已提交
187 188 189 190 191
    *arr++ = s;
    s = strtok(NULL, del);
  }
}

L
Liu Jicong 已提交
192 193 194 195 196 197 198 199 200 201 202 203
void ltrim(char* str) {
  if (str == NULL || *str == '\0') {
    return;
  }
  int   len = 0;
  char* p = str;
  while (*p != '\0' && isspace(*p)) {
    ++p;
    ++len;
  }
  memmove(str, p, strlen(str) - len + 1);
  // return str;
P
plum-lihui 已提交
204 205
}

P
plum-lihui 已提交
206 207
static int running = 1;
static int32_t msg_process(TAOS_RES* msg, int64_t msgIndex, int32_t threadLable) {
P
plum-lihui 已提交
208
  char buf[1024];
P
plum-lihui 已提交
209
  int32_t totalRows = 0;
P
plum-lihui 已提交
210

P
plum-lihui 已提交
211 212 213 214 215
  //printf("topic: %s\n", tmq_get_topic_name(msg));
  //printf("vg:%d\n", tmq_get_vgroup_id(msg));
  taosFprintfFile(g_fp, "msg index:%" PRId64 ", threadLable: %d\n",  msgIndex, threadLable);
  taosFprintfFile(g_fp, "topic: %s, vgroupId: %d\n",  tmq_get_topic_name(msg), tmq_get_vgroup_id(msg));
  
P
plum-lihui 已提交
216 217 218
  while (1) {
    TAOS_ROW row = taos_fetch_row(msg);
    if (row == NULL) break;
P
plum-lihui 已提交
219 220 221 222 223 224 225
	if (0 != g_stConfInfo.showRowFlag) {
      TAOS_FIELD* fields      = taos_fetch_fields(msg);
      int32_t     numOfFields = taos_field_count(msg);
      taos_print_row(buf, row, fields, numOfFields);
  	  taosFprintfFile(g_fp, "rows[%d]: %s\n", totalRows, buf);
	}
	totalRows++;
P
plum-lihui 已提交
226
  }
P
plum-lihui 已提交
227 228

  return totalRows;
P
plum-lihui 已提交
229 230
}

L
Liu Jicong 已提交
231 232 233 234 235 236 237 238 239 240 241
int queryDB(TAOS* taos, char* command) {
  TAOS_RES* pRes = taos_query(taos, command);
  int       code = taos_errno(pRes);
  // if ((code != 0) && (code != TSDB_CODE_RPC_AUTH_REQUIRED)) {
  if (code != 0) {
    pError("failed to reason:%s, sql: %s", tstrerror(code), command);
    taos_free_result(pRes);
    return -1;
  }
  taos_free_result(pRes);
  return 0;
P
plum-lihui 已提交
242 243
}

P
plum-lihui 已提交
244 245 246
static void tmq_commit_cb_print(tmq_t* tmq, tmq_resp_err_t resp, tmq_topic_vgroup_list_t* offsets) {
  printf("tmq_commit_cb_print() commit %d\n", resp);
}
L
Liu Jicong 已提交
247

P
plum-lihui 已提交
248 249
void build_consumer(SThreadInfo *pInfo) {
  tmq_conf_t* conf = tmq_conf_new();
P
plum-lihui 已提交
250

P
plum-lihui 已提交
251 252 253 254
  //tmq_conf_set(conf, "td.connect.ip", "localhost");
  //tmq_conf_set(conf, "td.connect.port", "6030");
  tmq_conf_set(conf, "td.connect.user", "root");
  tmq_conf_set(conf, "td.connect.pass", "taosdata");
P
plum-lihui 已提交
255

P
plum-lihui 已提交
256 257 258 259 260
  tmq_conf_set(conf, "td.connect.db", g_stConfInfo.dbName);

  tmq_conf_set_offset_commit_cb(conf, tmq_commit_cb_print);

  // tmq_conf_set(conf, "group.id", "cgrp1");
P
plum-lihui 已提交
261 262
  for (int32_t i = 0; i < pInfo->numOfKey; i++) {
    tmq_conf_set(conf, pInfo->key[i], pInfo->value[i]);
P
plum-lihui 已提交
263
  }
P
plum-lihui 已提交
264 265 266 267 268 269 270 271 272 273 274 275

  //tmq_conf_set(conf, "client.id", "c-001");

  //tmq_conf_set(conf, "enable.auto.commit", "true");
  //tmq_conf_set(conf, "enable.auto.commit", "false");

  //tmq_conf_set(conf, "auto.commit.interval.ms", "1000");
  
  //tmq_conf_set(conf, "auto.offset.reset", "none");
  //tmq_conf_set(conf, "auto.offset.reset", "earliest");
  //tmq_conf_set(conf, "auto.offset.reset", "latest");
  
L
Liu Jicong 已提交
276
  pInfo->tmq = tmq_consumer_new(conf, NULL, 0);
P
plum-lihui 已提交
277
  return;
P
plum-lihui 已提交
278 279
}

P
plum-lihui 已提交
280
void build_topic_list(SThreadInfo *pInfo) {
P
plum-lihui 已提交
281
  pInfo->topicList = tmq_list_new();
L
Liu Jicong 已提交
282
  // tmq_list_append(topic_list, "test_stb_topic_1");
P
plum-lihui 已提交
283 284
  for (int32_t i = 0; i < pInfo->numOfTopic; i++) {
    tmq_list_append(pInfo->topicList, pInfo->topics[i]);
P
plum-lihui 已提交
285
  }
P
plum-lihui 已提交
286
  return;
P
plum-lihui 已提交
287 288
}

P
plum-lihui 已提交
289
int32_t saveConsumeResult(SThreadInfo *pInfo) {
P
plum-lihui 已提交
290
  char sqlStr[1024] = {0};
P
plum-lihui 已提交
291
  
P
plum-lihui 已提交
292 293
  TAOS* pConn = taos_connect(NULL, "root", "taosdata", NULL, 0);
  assert(pConn != NULL);
P
plum-lihui 已提交
294
  
P
plum-lihui 已提交
295
  // schema: ts timestamp, consumerid int, consummsgcnt bigint, checkresult int
P
plum-lihui 已提交
296 297 298 299 300 301 302
  sprintf(sqlStr, "insert into %s.consumeresult values (now, %d, %" PRId64 ", %" PRId64 ", %d)", 
                               g_stConfInfo.cdbName, 
                               pInfo->consumerId, 
                               pInfo->consumeMsgCnt, 
                               pInfo->consumeRowCnt, 
                               pInfo->checkresult);
  
P
plum-lihui 已提交
303 304
  TAOS_RES* pRes = taos_query(pConn, sqlStr);
  if (taos_errno(pRes) != 0) {
P
plum-lihui 已提交
305
    printf("error in save consumeinfo, reason:%s\n", taos_errstr(pRes));
L
Liu Jicong 已提交
306 307
    taos_free_result(pRes);
    exit(-1);
P
plum-lihui 已提交
308
  }
P
plum-lihui 已提交
309
  
P
plum-lihui 已提交
310 311
  taos_free_result(pRes);

P
plum-lihui 已提交
312
  return 0;
P
plum-lihui 已提交
313 314
}

P
plum-lihui 已提交
315
void loop_consume(SThreadInfo *pInfo) {
P
plum-lihui 已提交
316
  tmq_resp_err_t err;
P
plum-lihui 已提交
317
  
P
plum-lihui 已提交
318
  int64_t totalMsgs = 0;
P
plum-lihui 已提交
319
  int64_t totalRows = 0;
P
plum-lihui 已提交
320 321

  while (running) {
P
plum-lihui 已提交
322
    TAOS_RES* tmqMsg = tmq_consumer_poll(pInfo->tmq, g_stConfInfo.consumeDelay * 1000);
P
plum-lihui 已提交
323
    if (tmqMsg) {     
L
Liu Jicong 已提交
324
      if (0 != g_stConfInfo.showMsgFlag) {
P
plum-lihui 已提交
325
        totalRows += msg_process(tmqMsg, totalMsgs, pInfo->consumerId);
L
Liu Jicong 已提交
326
      }
L
Liu Jicong 已提交
327 328 329

      taos_free_result(tmqMsg);

L
Liu Jicong 已提交
330
      totalMsgs++;
P
plum-lihui 已提交
331
      
P
plum-lihui 已提交
332
      if (totalMsgs >= pInfo->expectMsgCnt) {
P
plum-lihui 已提交
333
        break;
L
Liu Jicong 已提交
334
      }
P
plum-lihui 已提交
335 336 337 338
    } else {
      break;
    }
  }
P
plum-lihui 已提交
339
  
P
plum-lihui 已提交
340
  err = tmq_consumer_close(pInfo->tmq);
P
plum-lihui 已提交
341 342
  if (err) {
    printf("tmq_consumer_close() fail, reason: %s\n", tmq_err2str(err));
L
Liu Jicong 已提交
343
    exit(-1);
P
plum-lihui 已提交
344
  }
L
Liu Jicong 已提交
345

P
plum-lihui 已提交
346
  pInfo->consumeMsgCnt = totalMsgs;
P
plum-lihui 已提交
347 348 349 350
  pInfo->consumeRowCnt = totalRows;

  taosFprintfFile(g_fp, "==== consumerId: %d, consumeMsgCnt: %"PRId64", consumeRowCnt: %"PRId64"\n", pInfo->consumerId, pInfo->consumeMsgCnt, pInfo->consumeRowCnt);
  
P
plum-lihui 已提交
351 352
}

P
plum-lihui 已提交
353
void *consumeThreadFunc(void *param) {
P
plum-lihui 已提交
354 355
  int32_t totalMsgs = 0;

P
plum-lihui 已提交
356
  SThreadInfo *pInfo = (SThreadInfo *)param;
P
plum-lihui 已提交
357

P
plum-lihui 已提交
358 359
  build_consumer(pInfo);
  build_topic_list(pInfo);
P
plum-lihui 已提交
360
  if ((NULL == pInfo->tmq) || (NULL == pInfo->topicList)){
P
plum-lihui 已提交
361 362
    return NULL;
  }
P
plum-lihui 已提交
363
  
P
plum-lihui 已提交
364
  tmq_resp_err_t err = tmq_subscribe(pInfo->tmq, pInfo->topicList);
P
plum-lihui 已提交
365 366 367 368
  if (err) {
    printf("tmq_subscribe() fail, reason: %s\n", tmq_err2str(err));
    exit(-1);
  }
P
plum-lihui 已提交
369
  
P
plum-lihui 已提交
370
  loop_consume(pInfo);
P
plum-lihui 已提交
371

P
plum-lihui 已提交
372 373
  tmq_commit(pInfo->tmq, NULL, 0);

P
plum-lihui 已提交
374
  err = tmq_unsubscribe(pInfo->tmq);
P
plum-lihui 已提交
375 376
  if (err) {
    printf("tmq_unsubscribe() fail, reason: %s\n", tmq_err2str(err));
P
plum-lihui 已提交
377
	pInfo->consumeMsgCnt = -1;
P
plum-lihui 已提交
378
    return NULL;
P
plum-lihui 已提交
379 380
  }  
  
P
plum-lihui 已提交
381 382
  // save consume result into consumeresult table
  saveConsumeResult(pInfo);
P
plum-lihui 已提交
383 384

  return NULL;
P
plum-lihui 已提交
385 386
}

P
plum-lihui 已提交
387 388 389 390
void parseConsumeInfo() {
  char*      token;
  const char delim[2] = ",";
  const char ch = ':';
L
Liu Jicong 已提交
391

P
plum-lihui 已提交
392
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; i++) {  	
P
plum-lihui 已提交
393 394 395 396 397 398 399
    token = strtok(g_stConfInfo.stThreads[i].topicString, delim);
    while (token != NULL) {
      // printf("%s\n", token );
      strcpy(g_stConfInfo.stThreads[i].topics[g_stConfInfo.stThreads[i].numOfTopic], token);
      ltrim(g_stConfInfo.stThreads[i].topics[g_stConfInfo.stThreads[i].numOfTopic]);
      // printf("%s\n", g_stConfInfo.topics[g_stConfInfo.numOfTopic]);
      g_stConfInfo.stThreads[i].numOfTopic++;
P
plum-lihui 已提交
400
  	
P
plum-lihui 已提交
401 402
      token = strtok(NULL, delim);
    }
P
plum-lihui 已提交
403
    
P
plum-lihui 已提交
404 405 406 407 408 409 410 411 412 413 414 415 416
    token = strtok(g_stConfInfo.stThreads[i].keyString, delim);
    while (token != NULL) {
      // printf("%s\n", token );
      {
        char* pstr = token;
        ltrim(pstr);
        char* ret = strchr(pstr, ch);
        memcpy(g_stConfInfo.stThreads[i].key[g_stConfInfo.stThreads[i].numOfKey], pstr, ret - pstr);
        strcpy(g_stConfInfo.stThreads[i].value[g_stConfInfo.stThreads[i].numOfKey], ret + 1);
        // printf("key: %s, value: %s\n", g_stConfInfo.key[g_stConfInfo.numOfKey],
        // g_stConfInfo.value[g_stConfInfo.numOfKey]);
        g_stConfInfo.stThreads[i].numOfKey++;
      }
P
plum-lihui 已提交
417
  	
P
plum-lihui 已提交
418
      token = strtok(NULL, delim);
P
plum-lihui 已提交
419 420
    }
  }
P
plum-lihui 已提交
421
}
P
plum-lihui 已提交
422

P
plum-lihui 已提交
423 424
int32_t getConsumeInfo() {
  char sqlStr[1024] = {0};
P
plum-lihui 已提交
425
  
P
plum-lihui 已提交
426 427
  TAOS* pConn = taos_connect(NULL, "root", "taosdata", NULL, 0);
  assert(pConn != NULL);
P
plum-lihui 已提交
428 429
  
  sprintf(sqlStr, "select * from %s.consumeinfo", g_stConfInfo.cdbName);
P
plum-lihui 已提交
430 431 432
  TAOS_RES* pRes = taos_query(pConn, sqlStr);
  if (taos_errno(pRes) != 0) {
    printf("error in get consumeinfo, reason:%s\n", taos_errstr(pRes));
P
plum-lihui 已提交
433 434
    taosFprintfFile(g_fp, "error in get consumeinfo, reason:%s\n", taos_errstr(pRes));
    taosCloseFile(&g_fp);  
P
plum-lihui 已提交
435
    taos_free_result(pRes);
P
plum-lihui 已提交
436
    exit(-1);
P
plum-lihui 已提交
437 438 439 440 441 442 443 444
  }  
  
  TAOS_ROW	   row        = NULL;
  int		   num_fields = taos_num_fields(pRes);
  TAOS_FIELD*  fields     = taos_fetch_fields(pRes);
  
  // schema: ts timestamp, consumerid int, topiclist binary(1024), keylist binary(1024), expectmsgcnt bigint, ifcheckdata int
  
P
plum-lihui 已提交
445 446
  int32_t numOfThread = 0;
  while ((row = taos_fetch_row(pRes))) {
P
plum-lihui 已提交
447 448 449
	int32_t*  lengths = taos_fetch_lengths(pRes);
	
    for (int i = 0; i < num_fields; ++i) {      
P
plum-lihui 已提交
450 451 452
      if (row[i] == NULL || 0 == i) {
        continue;
      }
P
plum-lihui 已提交
453
      
P
plum-lihui 已提交
454
      if ((1 == i) && (fields[i].type == TSDB_DATA_TYPE_INT)) {
P
plum-lihui 已提交
455
        g_stConfInfo.stThreads[numOfThread].consumerId = *((int32_t *)row[i]);
P
plum-lihui 已提交
456 457 458 459 460
      } else if ((2 == i) && (fields[i].type == TSDB_DATA_TYPE_BINARY)) {
        memcpy(g_stConfInfo.stThreads[numOfThread].topicString, row[i], lengths[i]);
      } else if ((3 == i) && (fields[i].type == TSDB_DATA_TYPE_BINARY)) {
        memcpy(g_stConfInfo.stThreads[numOfThread].keyString, row[i], lengths[i]);
      } else if ((4 == i) && (fields[i].type == TSDB_DATA_TYPE_BIGINT)) {
P
plum-lihui 已提交
461
        g_stConfInfo.stThreads[numOfThread].expectMsgCnt = *((int64_t *)row[i]);
P
plum-lihui 已提交
462
      } else if ((5 == i) && (fields[i].type == TSDB_DATA_TYPE_INT)) {
P
plum-lihui 已提交
463
        g_stConfInfo.stThreads[numOfThread].ifCheckData = *((int32_t *)row[i]);
P
plum-lihui 已提交
464 465
      }
    }
P
plum-lihui 已提交
466
    numOfThread ++;
P
plum-lihui 已提交
467
  }
P
plum-lihui 已提交
468
  g_stConfInfo.numOfThread = numOfThread;
P
plum-lihui 已提交
469

P
plum-lihui 已提交
470
  taos_free_result(pRes);
P
plum-lihui 已提交
471

P
plum-lihui 已提交
472
  parseConsumeInfo();
P
plum-lihui 已提交
473

P
plum-lihui 已提交
474 475
  return 0;
}
P
plum-lihui 已提交
476

P
plum-lihui 已提交
477

P
plum-lihui 已提交
478 479 480
int main(int32_t argc, char* argv[]) {
  parseArgument(argc, argv);
  getConsumeInfo();
P
plum-lihui 已提交
481
  saveConfigToLogFile();
P
plum-lihui 已提交
482 483 484 485 486 487

  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
  taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE);

  // pthread_create one thread to consume
P
plum-lihui 已提交
488
  taosFprintfFile(g_fp, "==== create %d consume thread ====\n", g_stConfInfo.numOfThread);	
P
plum-lihui 已提交
489
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; ++i) {
P
plum-lihui 已提交
490
    taosThreadCreate(&(g_stConfInfo.stThreads[i].thread), &thattr, consumeThreadFunc, (void *)(&(g_stConfInfo.stThreads[i])));
P
plum-lihui 已提交
491 492 493 494
  }

  for (int32_t i = 0; i < g_stConfInfo.numOfThread; i++) {
    taosThreadJoin(g_stConfInfo.stThreads[i].thread, NULL);
P
plum-lihui 已提交
495 496
  }

P
plum-lihui 已提交
497 498
  //printf("consumer: %d, cosumer1: %d\n", totalMsgs, pInfo->consumeMsgCnt);	
  
P
plum-lihui 已提交
499
  taosFprintfFile(g_fp, "==== close tmqlog ====\n");	
P
plum-lihui 已提交
500 501
  taosCloseFile(&g_fp);  
  
P
plum-lihui 已提交
502 503 504
  return 0;
}