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

taosdata / TDengine / #5006

29 Mar 2026 04:32AM UTC coverage: 72.274% (+0.1%) from 72.152%
#5006

push

travis-ci

web-flow
refactor: do some internal refactor for TDgpt. (#34955)

253711 of 351039 relevant lines covered (72.27%)

131490495.89 hits per line

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

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

16
#include <assert.h>
17
#include <math.h>
18
#include <stdio.h>
19
#include <stdlib.h>
20
#include <string.h>
21
#include <sys/stat.h>
22
#include <sys/types.h>
23
#include <time.h>
24

25
#include "taos.h"
26
#include "taosdef.h"
27
#include "taoserror.h"
28
#include "tlog.h"
29
#include "types.h"
30

31
#define GREEN     "\033[1;32m"
32
#define NC        "\033[0m"
33
#define min(a, b) (((a) < (b)) ? (a) : (b))
34

35
#define MAX_SQL_STR_LEN         (1024 * 1024)
36
#define MAX_ROW_STR_LEN         (16 * 1024)
37
#define MAX_CONSUMER_THREAD_CNT (16)
38
#define MAX_VGROUP_CNT          (32)
39
#define SEND_TIME_UNIT          10  // ms
40
#define MAX_SQL_LEN             1048576
41

42
typedef enum {
43
  NOTIFY_CMD_START_CONSUM,
44
  NOTIFY_CMD_START_COMMIT,
45
  NOTIFY_CMD_ID_BUTT,
46
} NOTIFY_CMD_ID;
47

48
typedef enum enumQUERY_TYPE { NO_INSERT_TYPE, INSERT_TYPE, QUERY_TYPE_BUT } QUERY_TYPE;
49

50
typedef struct {
51
  TdThread thread;
52
  int32_t  consumerId;
53

54
  int32_t ifManualCommit;
55
  // int32_t  autoCommitIntervalMs;  // 1000 ms
56
  // char     autoCommit[8];         // true, false
57
  // char     autoOffsetRest[16];    // none, earliest, latest
58

59
  TdFilePtr pConsumeRowsFile;
60
  TdFilePtr pConsumeMetaFile;
61
  int32_t   ifCheckData;
62
  int64_t   expectMsgCnt;
63

64
  int64_t consumeMsgCnt;
65
  int64_t consumeRowCnt;
66
  int64_t consumeLen;
67
  int32_t checkresult;
68

69
  char topicString[1024];
70
  char keyString[1024];
71

72
  int32_t numOfTopic;
73
  char    topics[32][64];
74

75
  int32_t numOfKey;
76
  char    key[32][64];
77
  char    value[32][64];
78

79
  tmq_t*      tmq;
80
  tmq_list_t* topicList;
81

82
  int32_t numOfVgroups;
83
  int32_t rowsOfPerVgroups[MAX_VGROUP_CNT][2];  // [i][0]: vgroup id, [i][1]: rows of consume
84
  int64_t ts;
85

86
  TAOS* taos;
87

88
  // below parameters is used by omb test
89
  int32_t producerRate;  // unit: msgs/s
90
  int64_t totalProduceMsgs;
91
  int64_t totalMsgsLen;
92

93
} SThreadInfo;
94

95
typedef struct {
96
  // input from argvs
97
  char        cdbName[32];
98
  char        dbName[64];
99
  int32_t     showMsgFlag;
100
  int32_t     showRowFlag;
101
  int32_t     saveRowFlag;
102
  int32_t     consumeDelay;  // unit s
103
  int32_t     numOfThread;
104
  int32_t     useSnapshot;
105
  int64_t     nowTime;
106
  SThreadInfo stThreads[MAX_CONSUMER_THREAD_CNT];
107

108
  SThreadInfo stProdThreads[MAX_CONSUMER_THREAD_CNT];
109

110
  // below parameters is used by omb test
111
  char    topic[64];
112
  int32_t producers;
113
  int32_t producerRate;
114
  int32_t runDurationMinutes;
115
  int32_t batchSize;
116
  int32_t payloadLen;
117
} SConfInfo;
118

119
static SConfInfo g_stConfInfo;
120
TdFilePtr        g_fp = NULL;
121
static int       running = 1;
122
char*            g_payload = NULL;
123

124
// char* g_pRowValue = NULL;
125
// TdFilePtr g_fp = NULL;
126

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

131
  printf("%s%s\n", indent, "-c");
×
132
  printf("%s%s%s%s\n", indent, indent, "Configuration directory, default is ", configDir);
×
133
  printf("%s%s\n", indent, "-d");
×
134
  printf("%s%s%s\n", indent, indent, "The name of the database for cosumer, no default ");
×
135
  printf("%s%s\n", indent, "-g");
×
136
  printf("%s%s%s%d\n", indent, indent, "showMsgFlag, default is ", g_stConfInfo.showMsgFlag);
×
137
  printf("%s%s\n", indent, "-r");
×
138
  printf("%s%s%s%d\n", indent, indent, "showRowFlag, default is ", g_stConfInfo.showRowFlag);
×
139
  printf("%s%s\n", indent, "-s");
×
140
  printf("%s%s%s%d\n", indent, indent, "saveRowFlag, default is ", g_stConfInfo.saveRowFlag);
×
141
  printf("%s%s\n", indent, "-y");
×
142
  printf("%s%s%s%ds\n", indent, indent, "consume delay, default is ", g_stConfInfo.consumeDelay);
×
143
  printf("%s%s\n", indent, "-e");
×
144
  printf("%s%s%s%d\n", indent, indent, "snapshot, default is ", g_stConfInfo.useSnapshot);
×
145

146
  printf("%s%s\n", indent, "-t");
×
147
  printf("%s%s%s\n", indent, indent, "topic name, default is null");
×
148

149
  printf("%s%s\n", indent, "-x");
×
150
  printf("%s%s%s\n", indent, indent, "consume thread number, default is 1");
×
151

152
  printf("%s%s\n", indent, "-l");
×
153
  printf("%s%s%s%d\n", indent, indent, "run duration unit is minutes, default is ", g_stConfInfo.runDurationMinutes);
×
154
  printf("%s%s\n", indent, "-p");
×
155
  printf("%s%s%s\n", indent, indent, "producer thread number, default is 0");
×
156
  printf("%s%s\n", indent, "-b");
×
157
  printf("%s%s%s\n", indent, indent, "batch size, default is 1");
×
158
  printf("%s%s\n", indent, "-i");
×
159
  printf("%s%s%s\n", indent, indent, "produce rate unit is msgs /s, default is 100000");
×
160
  printf("%s%s\n", indent, "-n");
×
161
  printf("%s%s%s\n", indent, indent, "payload len unit is byte, default is 1000");
×
162

163
  exit(EXIT_SUCCESS);
×
164
}
165

166
char* getCurrentTimeString(char* timeString) {
9,082,166✔
167
  time_t    tTime = taosGetTimestampSec();
9,082,166✔
168
  struct tm tm;
9,018,322✔
169
  taosLocalTime(&tTime, &tm, NULL, 0, NULL);
9,082,166✔
170
  sprintf(timeString, "%d-%02d-%02d %02d:%02d:%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
9,082,166✔
171
          tm.tm_min, tm.tm_sec);
172

173
  return timeString;
9,082,166✔
174
}
175

176
static void tmqStop(int signum, void* info, void* ctx) {
×
177
  running = 0;
×
178
  char tmpString[128];
×
179
  taosFprintfFile(g_fp, "%s tmqStop() receive stop signal[%d]\n", getCurrentTimeString(tmpString), signum);
×
180
}
×
181

182
static void tmqSetSignalHandle() { 
73,182✔
183
  taosSetSignal(SIGINT, tmqStop); 
73,182✔
184
  taosSetSignal(SIGTERM, tmqStop); 
73,182✔
185
}
73,182✔
186

187
void initLogFile() {
73,182✔
188
  char filename[256];
68,971✔
189
  char tmpString[128];
68,971✔
190

191
  pid_t process_id = taosGetPId();
73,182✔
192

193
  if (0 != strlen(g_stConfInfo.topic)) {
73,182✔
194
    sprintf(filename, "/tmp/tmqlog-%d-%s.txt", process_id, getCurrentTimeString(tmpString));
×
195
  } else {
196
    sprintf(filename, "%s/../log/tmqlog-%d-%s.txt", configDir, process_id, getCurrentTimeString(tmpString));
73,182✔
197
  }
198
#ifdef WINDOWS
199
  for (int i = 2; i < sizeof(filename); i++) {
200
    if (filename[i] == ':') filename[i] = '-';
201
    if (filename[i] == '\0') break;
202
  }
203
#endif
204
  TdFilePtr pFile = taosOpenFile(filename, TD_FILE_TEXT | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM);
73,182✔
205
  if (NULL == pFile) {
73,182✔
206
    fprintf(stderr, "Failed to open %s for save result\n", filename);
×
207
    exit(-1);
×
208
  }
209
  g_fp = pFile;
73,182✔
210
}
73,182✔
211

212
void saveConfigToLogFile() {
73,182✔
213
  taosFprintfFile(g_fp, "###################################################################\n");
73,182✔
214
  taosFprintfFile(g_fp, "# configDir:           %s\n", configDir);
73,182✔
215
  taosFprintfFile(g_fp, "# dbName:              %s\n", g_stConfInfo.dbName);
73,182✔
216
  taosFprintfFile(g_fp, "# cdbName:             %s\n", g_stConfInfo.cdbName);
73,182✔
217
  taosFprintfFile(g_fp, "# showMsgFlag:         %d\n", g_stConfInfo.showMsgFlag);
73,182✔
218
  taosFprintfFile(g_fp, "# showRowFlag:         %d\n", g_stConfInfo.showRowFlag);
73,182✔
219
  taosFprintfFile(g_fp, "# saveRowFlag:         %d\n", g_stConfInfo.saveRowFlag);
73,182✔
220
  taosFprintfFile(g_fp, "# consumeDelay:        %d\n", g_stConfInfo.consumeDelay);
73,182✔
221
  taosFprintfFile(g_fp, "# numOfThread:         %d\n", g_stConfInfo.numOfThread);
73,182✔
222

223
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; i++) {
153,094✔
224
    taosFprintfFile(g_fp, "# consumer %d info:\n", g_stConfInfo.stThreads[i].consumerId);
79,912✔
225
    // taosFprintfFile(g_fp, "  auto commit:              %s\n", g_stConfInfo.stThreads[i].autoCommit);
226
    // taosFprintfFile(g_fp, "  auto commit interval ms:  %d\n", g_stConfInfo.stThreads[i].autoCommitIntervalMs);
227
    // taosFprintfFile(g_fp, "  auto offset rest:         %s\n", g_stConfInfo.stThreads[i].autoOffsetRest);
228
    taosFprintfFile(g_fp, "  Topics: ");
79,912✔
229
    for (int j = 0; j < g_stConfInfo.stThreads[i].numOfTopic; j++) {
164,974✔
230
      taosFprintfFile(g_fp, "%s, ", g_stConfInfo.stThreads[i].topics[j]);
85,062✔
231
    }
232
    taosFprintfFile(g_fp, "\n");
79,912✔
233
    taosFprintfFile(g_fp, "  Key: ");
79,912✔
234
    for (int k = 0; k < g_stConfInfo.stThreads[i].numOfKey; k++) {
399,560✔
235
      taosFprintfFile(g_fp, "%s:%s, ", g_stConfInfo.stThreads[i].key[k], g_stConfInfo.stThreads[i].value[k]);
319,648✔
236
    }
237
    taosFprintfFile(g_fp, "\n");
79,912✔
238
    taosFprintfFile(g_fp, "  expect rows: %" PRId64 "\n", g_stConfInfo.stThreads[i].expectMsgCnt);
79,912✔
239
  }
240

241
  char tmpString[128];
68,971✔
242
  taosFprintfFile(g_fp, "# Test time:                %s\n", getCurrentTimeString(tmpString));
73,182✔
243
  taosFprintfFile(g_fp, "###################################################################\n");
73,182✔
244
}
73,182✔
245

246
void parseArgument(int32_t argc, char* argv[]) {
73,182✔
247
  memset(&g_stConfInfo, 0, sizeof(SConfInfo));
73,182✔
248
  g_stConfInfo.showMsgFlag = 0;
73,182✔
249
  g_stConfInfo.showRowFlag = 0;
73,182✔
250
  g_stConfInfo.saveRowFlag = 0;
73,182✔
251
  g_stConfInfo.consumeDelay = 5;
73,182✔
252
  g_stConfInfo.numOfThread = 1;
73,182✔
253
  g_stConfInfo.batchSize = 1;
73,182✔
254
  g_stConfInfo.producers = 0;
73,182✔
255

256
  g_stConfInfo.nowTime = taosGetTimestampMs();
73,182✔
257

258
  for (int32_t i = 1; i < argc; i++) {
567,136✔
259
    if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
493,954✔
260
      printHelp();
×
261
      exit(0);
×
262
    } else if (strcmp(argv[i], "-d") == 0) {
493,954✔
263
      tstrncpy(g_stConfInfo.dbName, argv[++i], sizeof(g_stConfInfo.dbName));
73,182✔
264
    } else if (strcmp(argv[i], "-w") == 0) {
420,772✔
265
      tstrncpy(g_stConfInfo.cdbName, argv[++i], sizeof(g_stConfInfo.cdbName));
73,182✔
266
    } else if (strcmp(argv[i], "-c") == 0) {
347,590✔
267
      tstrncpy(configDir, argv[++i], PATH_MAX);
73,182✔
268
    } else if (strcmp(argv[i], "-g") == 0) {
274,408✔
269
      g_stConfInfo.showMsgFlag = atol(argv[++i]);
73,182✔
270
    } else if (strcmp(argv[i], "-r") == 0) {
201,226✔
271
      g_stConfInfo.showRowFlag = atol(argv[++i]);
73,182✔
272
    } else if (strcmp(argv[i], "-s") == 0) {
128,044✔
273
      g_stConfInfo.saveRowFlag = atol(argv[++i]);
×
274
    } else if (strcmp(argv[i], "-y") == 0) {
128,044✔
275
      g_stConfInfo.consumeDelay = atol(argv[++i]);
73,182✔
276
    } else if (strcmp(argv[i], "-e") == 0) {
54,862✔
277
      g_stConfInfo.useSnapshot = atol(argv[++i]);
54,862✔
278
    } else if (strcmp(argv[i], "-t") == 0) {
×
279
      char tmpBuf[56] = {0};
×
280
      tstrncpy(tmpBuf, argv[++i], sizeof(tmpBuf));
×
281
      sprintf(g_stConfInfo.topic, "`%s`", tmpBuf);
×
282
    } else if (strcmp(argv[i], "-x") == 0) {
×
283
      g_stConfInfo.numOfThread = atol(argv[++i]);
×
284
    } else if (strcmp(argv[i], "-l") == 0) {
×
285
      g_stConfInfo.runDurationMinutes = atol(argv[++i]);
×
286
    } else if (strcmp(argv[i], "-p") == 0) {
×
287
      g_stConfInfo.producers = atol(argv[++i]);
×
288
    } else if (strcmp(argv[i], "-b") == 0) {
×
289
      g_stConfInfo.batchSize = atol(argv[++i]);
×
290
    } else if (strcmp(argv[i], "-i") == 0) {
×
291
      g_stConfInfo.producerRate = atol(argv[++i]);
×
292
    } else if (strcmp(argv[i], "-n") == 0) {
×
293
      g_stConfInfo.payloadLen = atol(argv[++i]);
×
294
      if (g_stConfInfo.payloadLen <= 0 || g_stConfInfo.payloadLen > 1024 * 1024 * 1024) {
×
295
        pError("%s calloc size is too large: %s %s", GREEN, argv[++i], NC);
×
296
        exit(-1);
×
297
      }
298
    } else {
299
      pError("%s unknow para: %s %s", GREEN, argv[++i], NC);
×
300
      exit(-1);
×
301
    }
302
  }
303

304
  g_payload = taosMemoryCalloc(g_stConfInfo.payloadLen + 1, 1);
73,182✔
305
  if (NULL == g_payload) {
73,182✔
306
    pPrint("%s failed to malloc for payload %s", GREEN, NC);
×
307
    exit(-1);
×
308
  }
309

310
  for (int32_t i = 0; i < g_stConfInfo.payloadLen; i++) {
73,182✔
311
    strcpy(&g_payload[i], "a");
×
312
  }
313

314
  initLogFile();
73,182✔
315

316
  taosFprintfFile(g_fp, "====parseArgument() success\n");
73,182✔
317

318
#if 1
319
  pPrint("%s configDir:%s %s", GREEN, configDir, NC);
73,182✔
320
  pPrint("%s dbName:%s %s", GREEN, g_stConfInfo.dbName, NC);
73,182✔
321
  pPrint("%s cdbName:%s %s", GREEN, g_stConfInfo.cdbName, NC);
73,182✔
322
  pPrint("%s consumeDelay:%d %s", GREEN, g_stConfInfo.consumeDelay, NC);
73,182✔
323
  pPrint("%s showMsgFlag:%d %s", GREEN, g_stConfInfo.showMsgFlag, NC);
73,182✔
324
  pPrint("%s showRowFlag:%d %s", GREEN, g_stConfInfo.showRowFlag, NC);
73,182✔
325
  pPrint("%s saveRowFlag:%d %s", GREEN, g_stConfInfo.saveRowFlag, NC);
73,182✔
326

327
  pPrint("%s snapshot:%d %s", GREEN, g_stConfInfo.useSnapshot, NC);
73,182✔
328

329
  pPrint("%s omb topic:%s %s", GREEN, g_stConfInfo.topic, NC);
73,182✔
330
  pPrint("%s numOfThread:%d %s", GREEN, g_stConfInfo.numOfThread, NC);
73,182✔
331
#endif
332
}
73,182✔
333

334
void splitStr(char** arr, char* str, const char* del) {
×
335
  char* s = strtok(str, del);
×
336
  while (s != NULL) {
×
337
    *arr++ = s;
×
338
    s = strtok(NULL, del);
×
339
  }
340
}
×
341

342
void ltrim(char* str) {
404,710✔
343
  if (str == NULL || *str == '\0') {
404,710✔
344
    return;
×
345
  }
346
  int   len = 0;
404,710✔
347
  char* p = str;
404,710✔
348
  while (*p != '\0' && isspace(*p)) {
3,281,414✔
349
    ++p;
2,876,704✔
350
    ++len;
2,876,704✔
351
  }
352
  memmove(str, p, strlen(str) - len + 1);
404,710✔
353
  // return str;
354
}
355

356
int queryDB(TAOS* taos, char* command) {
79,912✔
357
  int       retryCnt = 10;
79,912✔
358
  int       code = 0;
79,912✔
359
  TAOS_RES* pRes = NULL;
79,912✔
360

361
  while (retryCnt--) {
79,912✔
362
    pRes = taos_query(taos, command);
79,912✔
363
    code = taos_errno(pRes);
79,912✔
364
    if (code != 0) {
79,912✔
365
      taosSsleep(1);
×
366
      taosFprintfFile(g_fp, "queryDB continue, command:%s, code:%d\n", command, code);
×
367
      taos_free_result(pRes);
×
368
      pRes = NULL;
×
369
      continue;
×
370
    }
371
    taos_free_result(pRes);
79,912✔
372
    return 0;
79,912✔
373
  }
374

375
  pError("failed to reason:%s, sql: %s", tstrerror(code), command);
×
376
  taos_free_result(pRes);
×
377
  return -1;
×
378
}
379

380
void addRowsToVgroupId(SThreadInfo* pInfo, int32_t vgroupId, int32_t rows) {
3,508,336✔
381
  int32_t i;
382
  for (i = 0; i < pInfo->numOfVgroups; i++) {
7,350,592✔
383
    if (vgroupId == pInfo->rowsOfPerVgroups[i][0]) {
7,165,780✔
384
      pInfo->rowsOfPerVgroups[i][1] += rows;
3,323,524✔
385
      return;
3,323,524✔
386
    }
387
  }
388

389
  pInfo->rowsOfPerVgroups[pInfo->numOfVgroups][0] = vgroupId;
184,812✔
390
  pInfo->rowsOfPerVgroups[pInfo->numOfVgroups][1] += rows;
184,812✔
391
  pInfo->numOfVgroups++;
184,812✔
392

393
  taosFprintfFile(g_fp, "consume id %d, add new vgroupId:%d\n", pInfo->consumerId, vgroupId);
184,812✔
394
  if (pInfo->numOfVgroups > MAX_VGROUP_CNT) {
184,812✔
395
    taosFprintfFile(g_fp, "====consume id %d, vgroup num %d over than 32. new vgroupId: %d\n", pInfo->consumerId,
×
396
                    pInfo->numOfVgroups, vgroupId);
397
    taosCloseFile(&g_fp);
×
398
    exit(-1);
×
399
  }
400
}
401

402
TAOS* createNewTaosConnect() {
153,094✔
403
  TAOS*   taos = NULL;
153,094✔
404
  int32_t retryCnt = 10;
153,094✔
405

406
  while (retryCnt--) {
153,094✔
407
    taos = taos_connect(NULL, "root", "taosdata", NULL, 0);
153,094✔
408
    if (NULL != taos) {
153,094✔
409
      return taos;
153,094✔
410
    }
411
    taosSsleep(1);
×
412
  }
413

414
  taosFprintfFile(g_fp, "taos_connect() fail\n");
×
415
  return NULL;
×
416
}
417

418
int32_t saveConsumeContentToTbl(SThreadInfo* pInfo, char* buf) {
×
419
  char sqlStr[1100] = {0};
×
420

421
  if (strlen(buf) > 1024) {
×
422
    taosFprintfFile(g_fp, "The length of one row[%d] is overflow 1024\n", (int)strlen(buf));
×
423
    taosCloseFile(&g_fp);
×
424
    return -1;
×
425
  }
426

427
  TAOS* pConn = taos_connect(NULL, "root", "taosdata", NULL, 0);
×
428
  if (pConn == NULL) {
×
429
    taosFprintfFile(g_fp, "taos_connect() fail, can not save consume result to main script\n");
×
430
    return -1;
×
431
  }
432

433
  sprintf(sqlStr, "insert into %s.content_%d values (%" PRId64 ", \'%s\')", g_stConfInfo.cdbName, pInfo->consumerId,
×
434
          pInfo->ts++, buf);
×
435
  int retCode = queryDB(pConn, sqlStr);
×
436
  if (retCode != 0) {
×
437
    taosFprintfFile(g_fp, "error in save consume content\n");
×
438
    taosCloseFile(&g_fp);
×
439
    taos_close(pConn);
×
440
    exit(-1);
×
441
  }
442

443
  taos_close(pConn);
×
444

445
  return 0;
×
446
}
447

448
static char* shellFormatTimestamp(char* buf, int32_t bufSize, int64_t val, int32_t precision) {
2,147,483,647✔
449
  // if (shell.args.is_raw_time) {
450
  //   sprintf(buf, "%" PRId64, val);
451
  //   return buf;
452
  // }
453

454
  time_t  tt;
2,147,483,647✔
455
  int32_t ms = 0;
2,147,483,647✔
456
  if (precision == TSDB_TIME_PRECISION_NANO) {
2,147,483,647✔
457
    tt = (time_t)(val / 1000000000);
15,200,000✔
458
    ms = val % 1000000000;
15,200,000✔
459
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
2,147,483,647✔
460
    tt = (time_t)(val / 1000000);
15,300,000✔
461
    ms = val % 1000000;
15,300,000✔
462
  } else {
463
    tt = (time_t)(val / 1000);
2,147,483,647✔
464
    ms = val % 1000;
2,147,483,647✔
465
  }
466

467
  if (tt <= 0 && ms < 0) {
2,147,483,647✔
468
    tt--;
×
469
    if (precision == TSDB_TIME_PRECISION_NANO) {
×
470
      ms += 1000000000;
×
471
    } else if (precision == TSDB_TIME_PRECISION_MICRO) {
×
472
      ms += 1000000;
×
473
    } else {
474
      ms += 1000;
×
475
    }
476
  }
477

478
  struct tm ptm;
2,147,483,647✔
479
  if (taosLocalTime(&tt, &ptm, buf, bufSize, NULL) == NULL) {
2,147,483,647✔
480
    return buf;
×
481
  }
482
  size_t pos = taosStrfTime(buf, 35, "%Y-%m-%d %H:%M:%S", &ptm);
2,147,483,647✔
483

484
  if (precision == TSDB_TIME_PRECISION_NANO) {
2,147,483,647✔
485
    sprintf(buf + pos, ".%09d", ms);
15,186,928✔
486
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
2,147,483,647✔
487
    sprintf(buf + pos, ".%06d", ms);
15,300,000✔
488
  } else {
489
    sprintf(buf + pos, ".%03d", ms);
2,147,483,647✔
490
  }
491

492
  return buf;
2,147,483,647✔
493
}
494

495
static void shellDumpFieldToFile(TdFilePtr pFile, const char* val, TAOS_FIELD* field, int32_t length,
2,147,483,647✔
496
                                 int32_t precision) {
497
  if (val == NULL) {
2,147,483,647✔
498
    taosFprintfFile(pFile, "NULL");
144,223,460✔
499
    return;
144,228,980✔
500
  }
501

502
  char quotationStr[2];
2,147,483,647✔
503
  quotationStr[0] = '\"';
2,147,483,647✔
504
  quotationStr[1] = 0;
2,147,483,647✔
505

506
  int  n;
507
  char buf[TSDB_MAX_BYTES_PER_ROW];
2,147,483,647✔
508
  switch (field->type) {
2,147,483,647✔
509
    case TSDB_DATA_TYPE_BOOL:
×
510
      taosFprintfFile(pFile, "%d", ((((int32_t)(*((char*)val))) == 1) ? 1 : 0));
×
511
      break;
×
512
    case TSDB_DATA_TYPE_TINYINT:
×
513
      taosFprintfFile(pFile, "%d", *((int8_t*)val));
×
514
      break;
×
515
    case TSDB_DATA_TYPE_UTINYINT:
×
516
      taosFprintfFile(pFile, "%u", *((uint8_t*)val));
×
517
      break;
×
518
    case TSDB_DATA_TYPE_SMALLINT:
×
519
      taosFprintfFile(pFile, "%d", *((int16_t*)val));
×
520
      break;
×
521
    case TSDB_DATA_TYPE_USMALLINT:
×
522
      taosFprintfFile(pFile, "%u", *((uint16_t*)val));
×
523
      break;
×
524
    case TSDB_DATA_TYPE_INT:
2,147,483,647✔
525
      taosFprintfFile(pFile, "%d", *((int32_t*)val));
2,147,483,647✔
526
      break;
2,147,483,647✔
527
    case TSDB_DATA_TYPE_UINT:
×
528
      taosFprintfFile(pFile, "%u", *((uint32_t*)val));
×
529
      break;
×
530
    case TSDB_DATA_TYPE_BIGINT:
2,147,483,647✔
531
      taosFprintfFile(pFile, "%" PRId64, *((int64_t*)val));
2,147,483,647✔
532
      break;
2,147,483,647✔
533
    case TSDB_DATA_TYPE_UBIGINT:
×
534
      taosFprintfFile(pFile, "%" PRIu64, *((uint64_t*)val));
×
535
      break;
×
536
    case TSDB_DATA_TYPE_FLOAT:
82,174✔
537
      taosFprintfFile(pFile, "%.5f", GET_FLOAT_VAL(val));
82,174✔
538
      break;
82,174✔
539
    case TSDB_DATA_TYPE_DOUBLE:
2,147,483,647✔
540
      n = snprintf(buf, TSDB_MAX_BYTES_PER_ROW, "%*.9f", length, GET_DOUBLE_VAL(val));
2,147,483,647✔
541
      if (n > TMAX(25, length)) {
2,147,483,647✔
542
        taosFprintfFile(pFile, "%*.15e", length, GET_DOUBLE_VAL(val));
94,418,075✔
543
      } else {
544
        taosFprintfFile(pFile, "%s", buf);
2,147,483,647✔
545
      }
546
      break;
2,147,483,647✔
547
    case TSDB_DATA_TYPE_BINARY:
2,147,483,647✔
548
    case TSDB_DATA_TYPE_VARBINARY:
549
    case TSDB_DATA_TYPE_NCHAR:
550
    case TSDB_DATA_TYPE_JSON:
551
    case TSDB_DATA_TYPE_GEOMETRY: {
552
      int32_t bufIndex = 0;
2,147,483,647✔
553
      for (int32_t i = 0; i < length; i++) {
2,147,483,647✔
554
        buf[bufIndex] = val[i];
2,147,483,647✔
555
        bufIndex++;
2,147,483,647✔
556
        if (val[i] == '\"') {
2,147,483,647✔
557
          buf[bufIndex] = val[i];
×
558
          bufIndex++;
×
559
        }
560
      }
561
      buf[bufIndex] = 0;
2,147,483,647✔
562

563
      taosFprintfFile(pFile, "%s%s%s", quotationStr, buf, quotationStr);
2,147,483,647✔
564
    } break;
2,147,483,647✔
565
    case TSDB_DATA_TYPE_TIMESTAMP:
2,147,483,647✔
566
      shellFormatTimestamp(buf, sizeof(buf), *(int64_t*)val, precision);
2,147,483,647✔
567
      taosFprintfFile(pFile, "%s%s%s", quotationStr, buf, quotationStr);
2,147,483,647✔
568
      break;
2,147,483,647✔
569
    default:
×
570
      break;
×
571
  }
572
}
573

574
static void dumpToFileForCheck(TdFilePtr pFile, TAOS_ROW row, TAOS_FIELD* fields, int32_t* length, int32_t num_fields,
2,147,483,647✔
575
                               int32_t precision) {
576
  for (int32_t i = 0; i < num_fields; i++) {
2,147,483,647✔
577
    if (i > 0) {
2,147,483,647✔
578
      taosFprintfFile(pFile, ",");
2,147,483,647✔
579
    }
580
    shellDumpFieldToFile(pFile, (const char*)row[i], fields + i, length[i], precision);
2,147,483,647✔
581
  }
582
  taosFprintfFile(pFile, "\n");
2,147,483,647✔
583
}
2,147,483,647✔
584

585
static int32_t data_msg_process(TAOS_RES* msg, SThreadInfo* pInfo, int32_t msgIndex) {
3,508,336✔
586
  char    buf[1024];
3,478,067✔
587
  int32_t totalRows = 0;
3,508,336✔
588

589
  int32_t     vgroupId = tmq_get_vgroup_id(msg);
3,508,336✔
590
  const char* dbName = tmq_get_db_name(msg);
3,508,336✔
591

592
  char timestring[128] = {0};
3,508,336✔
593
  taosFprintfFile(g_fp, "%s consumerId: %d, msg index:%d\n", getCurrentTimeString(timestring), pInfo->consumerId, msgIndex);
3,508,336✔
594
  int32_t index = 0;
3,508,336✔
595
  for (index = 0; index < pInfo->numOfVgroups; index++) {
7,350,592✔
596
    if (vgroupId == pInfo->rowsOfPerVgroups[index][0]) {
7,165,780✔
597
      break;
3,323,524✔
598
    }
599
  }
600

601
  taosFprintfFile(g_fp, "dbName: %s, topic: %s, vgroupId:%d, currentRows:%d\n", dbName != NULL ? dbName : "invalid table",
3,508,336✔
602
                  tmq_get_topic_name(msg), vgroupId, pInfo->rowsOfPerVgroups[index][1]);
3,478,067✔
603

604
  while (1) {
2,147,483,647✔
605
    TAOS_ROW row = taos_fetch_row(msg);
2,147,483,647✔
606
    if (row == NULL) {
2,147,483,647✔
607
      break;
3,508,336✔
608
    }
609

610
    TAOS_FIELD* fields = taos_fetch_fields(msg);
2,147,483,647✔
611
    int32_t     numOfFields = taos_field_count(msg);
2,147,483,647✔
612
    int32_t*    length = taos_fetch_lengths(msg);
2,147,483,647✔
613
    int32_t     precision = taos_result_precision(msg);
2,147,483,647✔
614
    const char* tbName = tmq_get_table_name(msg);
2,147,483,647✔
615

616
#if 0
617
        // get schema
618
        //============================== stub =================================================//
619
        for (int32_t i = 0; i < numOfFields; i++) {
620
          taosFprintfFile(g_fp, "%02d: name: %s, type: %d, len: %d\n", i, fields[i].name, fields[i].type, fields[i].bytes);
621
        }
622
        //============================== stub =================================================//
623
#endif
624

625
    dumpToFileForCheck(pInfo->pConsumeRowsFile, row, fields, length, numOfFields, precision);
2,147,483,647✔
626
    taos_print_row(buf, row, fields, numOfFields);
2,147,483,647✔
627

628
    if (0 != g_stConfInfo.showRowFlag) {
2,147,483,647✔
629
      taosFprintfFile(g_fp, "time:%" PRId64 " tbname:%s, rows[%d]: %s\n", taosGetTimestampMs(), (tbName != NULL ? tbName : "null table"), totalRows, buf);
2,147,483,647✔
630
      // if (0 != g_stConfInfo.saveRowFlag) {
631
      //   saveConsumeContentToTbl(pInfo, buf);
632
      // }
633
//      taosFsyncFile(g_fp);
634
    }
635

636
    totalRows++;
2,147,483,647✔
637
  }
638

639
  addRowsToVgroupId(pInfo, vgroupId, totalRows);
3,508,336✔
640
  return totalRows;
3,508,336✔
641
}
642

643
static int32_t meta_msg_process(TAOS_RES* msg, SThreadInfo* pInfo, int32_t msgIndex) {
×
644
  char    buf[1024];
×
645
  int32_t totalRows = 0;
×
646

647
  // printf("topic: %s\n", tmq_get_topic_name(msg));
648
  int32_t     vgroupId = tmq_get_vgroup_id(msg);
×
649
  const char* dbName = tmq_get_db_name(msg);
×
650

651
  taosFprintfFile(g_fp, "%s consumerId: %d, msg index:%d\n", getCurrentTimeString(buf), pInfo->consumerId, msgIndex);
×
652
  taosFprintfFile(g_fp, "%s dbName: %s, topic: %s, vgroupId: %d\n", getCurrentTimeString(buf),dbName != NULL ? dbName : "invalid table",
×
653
                  tmq_get_topic_name(msg), vgroupId);
654

655
  {
656
    tmq_raw_data raw = {0};
×
657
    int32_t      code = tmq_get_raw(msg, &raw);
×
658

659
    if (code == TSDB_CODE_SUCCESS) {
660
      //          int retCode = queryDB(pInfo->taos, "use metadb");
661
      //          if (retCode != 0) {
662
      //                taosFprintfFile(g_fp, "error when use metadb\n");
663
      //                taosCloseFile(&g_fp);
664
      //                exit(-1);
665
      //          }
666
      //          taosFprintfFile(g_fp, "raw:%p\n", &raw);
667
      //
668
      //      tmq_write_raw(pInfo->taos, raw);
669
    }
670

671
    char* result = tmq_get_json_meta(msg);
×
672
    if (result && strcmp(result, "") != 0) {
×
673
      // printf("meta result: %s\n", result);
674
      taosFprintfFile(pInfo->pConsumeMetaFile, "%s\n", result);
×
675
    }
676
    tmq_free_json_meta(result);
×
677
  }
678

679
  totalRows++;
×
680

681
  return totalRows;
×
682
}
683

684
static void appNothing(void* param, TAOS_RES* res, int32_t numOfRows) {}
136,059✔
685

686
int32_t notifyMainScript(SThreadInfo* pInfo, int32_t cmdId) {
136,059✔
687
  char sqlStr[1024] = {0};
136,059✔
688

689
  // schema: ts timestamp, consumerid int, consummsgcnt bigint, checkresult int
690
  sprintf(sqlStr, "insert into %s.notifyinfo values (%" PRId64 ", %d, %d)", g_stConfInfo.cdbName,
136,059✔
691
          atomic_fetch_add_64(&g_stConfInfo.nowTime, 1), cmdId, pInfo->consumerId);
692

693
  taos_query_a(pInfo->taos, sqlStr, appNothing, NULL);
136,059✔
694

695
  taosFprintfFile(g_fp, "notifyMainScript success, sql: %s\n", sqlStr);
136,059✔
696

697
  return 0;
136,059✔
698
}
699

700
static int32_t g_once_commit_flag = 0;
701

702
static void tmq_commit_cb_print(tmq_t* tmq, int32_t code, void* param) {
4,395,915✔
703
  taosFprintfFile(g_fp, "tmq_commit_cb_print() commit %d\n", code);
4,395,915✔
704

705
  if (0 == g_once_commit_flag && code == 0) {
4,395,915✔
706
    g_once_commit_flag = 1;
61,892✔
707
    notifyMainScript((SThreadInfo*)param, (int32_t)NOTIFY_CMD_START_COMMIT);
61,892✔
708
  }
709

710
  char tmpString[128];
4,388,387✔
711
  taosFprintfFile(g_fp, "%s tmq_commit_cb_print() be called\n", getCurrentTimeString(tmpString));
4,395,915✔
712
}
4,395,915✔
713

714
void build_consumer(SThreadInfo* pInfo) {
79,912✔
715
  tmq_conf_t* conf = tmq_conf_new();
79,912✔
716

717
  // tmq_conf_set(conf, "td.connect.ip", "localhost");
718
  // tmq_conf_set(conf, "td.connect.port", "6030");
719
  tmq_conf_set(conf, "td.connect.user", "root");
79,912✔
720
  tmq_conf_set(conf, "td.connect.pass", "taosdata");
79,857✔
721

722
  // tmq_conf_set(conf, "td.connect.db", g_stConfInfo.dbName);
723

724
  tmq_conf_set_auto_commit_cb(conf, tmq_commit_cb_print, pInfo);
79,912✔
725

726
  // tmq_conf_set(conf, "group.id", "cgrp1");
727
  for (int32_t i = 0; i < pInfo->numOfKey; i++) {
399,505✔
728
    tmq_conf_set(conf, pInfo->key[i], pInfo->value[i]);
319,593✔
729
  }
730

731
  tmq_conf_set(conf, "msg.with.table.name", "true");
79,912✔
732

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

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

738
  // tmq_conf_set(conf, "auto.commit.interval.ms", "1000");
739

740
  // tmq_conf_set(conf, "auto.offset.reset", "none");
741
  // tmq_conf_set(conf, "auto.offset.reset", "earliest");
742
  // tmq_conf_set(conf, "auto.offset.reset", "latest");
743
  //
744
  if (g_stConfInfo.useSnapshot) {
79,912✔
745
    tmq_conf_set(conf, "experimental.snapshot.enable", "true");
26,107✔
746
  }
747

748
  pInfo->tmq = tmq_consumer_new(conf, NULL, 0);
79,912✔
749
  tmq_conf_destroy(conf);
79,912✔
750
  return;
79,912✔
751
}
752

753
void build_topic_list(SThreadInfo* pInfo) {
79,912✔
754
  pInfo->topicList = tmq_list_new();
79,912✔
755
  // tmq_list_append(topic_list, "test_stb_topic_1");
756
  for (int32_t i = 0; i < pInfo->numOfTopic; i++) {
164,974✔
757
    tmq_list_append(pInfo->topicList, pInfo->topics[i]);
85,062✔
758
  }
759
  return;
79,912✔
760
}
761

762
int32_t saveConsumeResult(SThreadInfo* pInfo) {
79,912✔
763
  char sqlStr[1024] = {0};
79,912✔
764
  // schema: ts timestamp, consumerid int, consummsgcnt bigint, checkresult int
765
  sprintf(sqlStr, "insert into %s.consumeresult values (%" PRId64 ", %d, %" PRId64 ", %" PRId64 ", %d)",
79,912✔
766
          g_stConfInfo.cdbName, atomic_fetch_add_64(&g_stConfInfo.nowTime, 1), pInfo->consumerId, pInfo->consumeMsgCnt,
767
          pInfo->consumeRowCnt, pInfo->checkresult);
768

769
  char tmpString[128];
74,037✔
770
  taosFprintfFile(g_fp, "%s, consume id %d result: %s\n", getCurrentTimeString(tmpString), pInfo->consumerId, sqlStr);
79,912✔
771

772
  int retCode = queryDB(pInfo->taos, sqlStr);
79,912✔
773
  if (retCode != 0) {
79,912✔
774
    taosFprintfFile(g_fp, "consume id %d error in save consume result\n", pInfo->consumerId);
×
775
    return -1;
×
776
  }
777

778
  return 0;
79,912✔
779
}
780

781
void loop_consume(SThreadInfo* pInfo) {
79,912✔
782
  int32_t code;
783

784
  int32_t once_flag = 0;
79,912✔
785

786
  int64_t totalMsgs = 0;
79,912✔
787
  int64_t totalRows = 0;
79,912✔
788

789
  char tmpString[128];
74,037✔
790
  taosFprintfFile(g_fp, "%s consumer id %d start to loop pull msg\n", getCurrentTimeString(tmpString),
79,912✔
791
                  pInfo->consumerId);
792

793
  pInfo->ts = taosGetTimestampMs();
79,912✔
794

795
  if (pInfo->ifCheckData) {
79,912✔
796
    char filename[256] = {0};
48,769✔
797
    memset(tmpString, 0, tListLen(tmpString));
48,769✔
798

799
    // sprintf(filename, "%s/../log/consumerid_%d_%s.txt", configDir, pInfo->consumerId,
800
    // getCurrentTimeString(tmpString));
801
    sprintf(filename, "%s/../log/consumerid_%d.txt", configDir, pInfo->consumerId);
48,769✔
802
    pInfo->pConsumeRowsFile = taosOpenFile(filename, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM);
48,769✔
803

804
    sprintf(filename, "%s/../log/meta_consumerid_%d.txt", configDir, pInfo->consumerId);
48,769✔
805
    pInfo->pConsumeMetaFile = taosOpenFile(filename, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM);
48,769✔
806

807
    if (pInfo->pConsumeRowsFile == NULL || pInfo->pConsumeMetaFile == NULL) {
48,769✔
808
      taosFprintfFile(g_fp, "%s create file fail for save rows or save meta\n", getCurrentTimeString(tmpString));
×
809
      return;
×
810
    }
811
  }
812

813
  int64_t  lastTotalMsgs = 0;
79,912✔
814
  uint64_t lastPrintTime = taosGetTimestampMs();
79,912✔
815
  uint64_t startTs = taosGetTimestampMs();
79,912✔
816

817
  int32_t consumeDelay = g_stConfInfo.consumeDelay == -1 ? -1 : (g_stConfInfo.consumeDelay * 1000);
79,912✔
818
  while (running) {
4,337,109✔
819
    TAOS_RES* tmqMsg = tmq_consumer_poll(pInfo->tmq, consumeDelay);
4,337,109✔
820
    if (tmqMsg) {
4,337,109✔
821
      if (0 != g_stConfInfo.showMsgFlag) {
3,508,336✔
822
        tmq_res_t msgType = tmq_get_res_type(tmqMsg);
3,508,336✔
823
        if (msgType == TMQ_RES_TABLE_META) {
3,508,336✔
824
          totalRows += meta_msg_process(tmqMsg, pInfo, totalMsgs);
×
825
        } else if (msgType == TMQ_RES_DATA) {
3,508,336✔
826
          totalRows += data_msg_process(tmqMsg, pInfo, totalMsgs);
3,508,336✔
827
        } else if (msgType == TMQ_RES_METADATA) {
×
828
          meta_msg_process(tmqMsg, pInfo, totalMsgs);
×
829
          totalRows += data_msg_process(tmqMsg, pInfo, totalMsgs);
×
830
        }
831
      }
832

833
      taos_free_result(tmqMsg);
3,508,336✔
834
      totalMsgs++;
3,508,336✔
835

836
      int64_t currentPrintTime = taosGetTimestampMs();
3,508,336✔
837
      if (currentPrintTime - lastPrintTime > 10 * 1000) {
3,508,336✔
838
        taosFprintfFile(
44,718✔
839
            g_fp, "consumer id %d has currently poll total msgs: %" PRId64 ", period rate: %.3f msgs/second\n",
840
            pInfo->consumerId, totalMsgs, (totalMsgs - lastTotalMsgs) * 1000.0 / (currentPrintTime - lastPrintTime));
44,718✔
841
        lastPrintTime = currentPrintTime;
44,718✔
842
        lastTotalMsgs = totalMsgs;
44,718✔
843
      }
844

845
      if (0 == once_flag) {
3,508,336✔
846
        once_flag = 1;
74,167✔
847
        notifyMainScript(pInfo, NOTIFY_CMD_START_CONSUM);
74,167✔
848
      }
849

850
      if ((totalRows >= pInfo->expectMsgCnt) || (totalMsgs >= pInfo->expectMsgCnt)) {
3,508,336✔
851
        memset(tmpString, 0, tListLen(tmpString));
42,954✔
852
        taosFprintfFile(g_fp, "%s over than expect rows, so break consume\n", getCurrentTimeString(tmpString));
42,954✔
853
        break;
42,954✔
854
      }
855
    } else {
856
      memset(tmpString, 0, tListLen(tmpString));
828,773✔
857
      code = taos_errno(NULL);
828,773✔
858
      if (code == 0) {
828,773✔
859
        taosFprintfFile(g_fp, "%s no poll more msg when time over, break consume, errMsg:%s\n", getCurrentTimeString(tmpString), taos_errstr(NULL));
35,501✔
860
        break;
35,501✔
861
      } else if (code == TSDB_CODE_VND_INVALID_VGROUP_ID ||
793,272✔
862
                 code == TSDB_CODE_SYN_NOT_LEADER ||
1,457✔
863
                 code == TSDB_CODE_APP_IS_STARTING ||
1,457✔
864
                 code == TSDB_CODE_APP_IS_STOPPING ||
1,457✔
865
                 code == TSDB_CODE_RPC_NETWORK_UNAVAIL) {
866
        taosFprintfFile(g_fp, "%s poll err:%s, continue\n", getCurrentTimeString(tmpString), taos_errstr(NULL));
791,815✔
867
      } else {
868
        taosFprintfFile(g_fp, "%s poll err:%s, break\n", getCurrentTimeString(tmpString), taos_errstr(NULL));
1,457✔
869
        break;
1,457✔
870
      }
871
    }
872
  }
873

874
  if (0 == running) {
79,912✔
875
    taosFprintfFile(g_fp, "receive stop signal and not continue consume\n");
×
876
  }
877

878
  pInfo->consumeMsgCnt = totalMsgs;
79,912✔
879
  pInfo->consumeRowCnt = totalRows;
79,912✔
880

881
  taosFprintfFile(g_fp, "==== consumerId: %d, consumeMsgCnt: %" PRId64 ", consumeRowCnt: %" PRId64 "\n",
79,912✔
882
                  pInfo->consumerId, pInfo->consumeMsgCnt, pInfo->consumeRowCnt);
883

884
  if(taosFsyncFile(pInfo->pConsumeRowsFile) < 0){
79,912✔
885
    printf("taosFsyncFile error:%s", strerror(errno));
×
886
  }
887
  taosCloseFile(&pInfo->pConsumeRowsFile);
79,912✔
888
}
889

890
void* consumeThreadFunc(void* param) {
79,912✔
891
  SThreadInfo* pInfo = (SThreadInfo*)param;
79,912✔
892

893
  pInfo->taos = createNewTaosConnect();
79,912✔
894
  if (pInfo->taos == NULL) {
79,912✔
895
    taosFprintfFile(g_fp, "taos_connect() fail, can not notify and save consume result to main scripte\n");
×
896
    return NULL;
×
897
  }
898

899
  build_consumer(pInfo);
79,912✔
900
  build_topic_list(pInfo);
79,912✔
901
  if ((NULL == pInfo->tmq) || (NULL == pInfo->topicList)) {
79,912✔
902
    taosFprintfFile(g_fp, "create consumer fail! tmq is null or topicList is null\n");
×
903
    taos_close(pInfo->taos);
×
904
    pInfo->taos = NULL;
×
905
    return NULL;
×
906
  }
907

908
  int32_t err = tmq_subscribe(pInfo->tmq, pInfo->topicList);
79,912✔
909
  if (err != 0) {
79,912✔
910
    pError("tmq_subscribe() fail, reason: %s\n", tmq_err2str(err));
×
911
    taosFprintfFile(g_fp, "tmq_subscribe() fail! reason: %s\n", tmq_err2str(err));
×
912
    taos_close(pInfo->taos);
×
913
    pInfo->taos = NULL;
×
914
    return NULL;
×
915
  }
916

917
  tmq_list_destroy(pInfo->topicList);
79,912✔
918
  pInfo->topicList = NULL;
79,912✔
919

920
  loop_consume(pInfo);
79,912✔
921

922
  if (pInfo->ifManualCommit) {
79,912✔
923
    pPrint("tmq_commit() manual commit when consume end.\n");
65,101✔
924
    /*tmq_commit(pInfo->tmq, NULL, 0);*/
925
    tmq_commit_sync(pInfo->tmq, NULL);
65,101✔
926
    tmq_commit_cb_print(pInfo->tmq, 0, pInfo);
65,101✔
927
    taosFprintfFile(g_fp, "tmq_commit() manual commit over.\n");
65,101✔
928
    pPrint("tmq_commit() manual commit over.\n");
65,101✔
929
  }
930

931
  err = tmq_unsubscribe(pInfo->tmq);
79,912✔
932
  if (err != 0) {
79,912✔
933
    pError("tmq_unsubscribe() fail, reason: %s\n", tmq_err2str(err));
×
934
    taosFprintfFile(g_fp, "tmq_unsubscribe()! reason: %s\n", tmq_err2str(err));
×
935
  }
936

937
  err = tmq_consumer_close(pInfo->tmq);
79,912✔
938
  if (err != 0) {
79,912✔
939
    pError("tmq_consumer_close() fail, reason: %s\n", tmq_err2str(err));
×
940
    taosFprintfFile(g_fp, "tmq_consumer_close()! reason: %s\n", tmq_err2str(err));
×
941
  }
942
  pInfo->tmq = NULL;
79,912✔
943

944
  // save consume result into consumeresult table
945
  saveConsumeResult(pInfo);
79,912✔
946

947
  // save rows from per vgroup
948
  taosFprintfFile(g_fp, "======== consumerId: %d, consume rows from per vgroups ========\n", pInfo->consumerId);
79,912✔
949
  for (int32_t i = 0; i < pInfo->numOfVgroups; i++) {
264,724✔
950
    taosFprintfFile(g_fp, "vgroups: %04d, rows: %d\n", pInfo->rowsOfPerVgroups[i][0], pInfo->rowsOfPerVgroups[i][1]);
184,812✔
951
  }
952

953
  taos_close(pInfo->taos);
79,912✔
954
  pInfo->taos = NULL;
79,912✔
955

956
  return NULL;
79,912✔
957
}
958

959
void parseConsumeInfo() {
73,182✔
960
  char*      token;
961
  const char delim[2] = ",";
73,182✔
962
  const char ch = ':';
73,182✔
963

964
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; i++) {
153,094✔
965
    token = strtok(g_stConfInfo.stThreads[i].topicString, delim);
79,912✔
966
    while (token != NULL) {
164,974✔
967
      // printf("%s\n", token );
968
      tstrncpy(g_stConfInfo.stThreads[i].topics[g_stConfInfo.stThreads[i].numOfTopic], token,
85,062✔
969
               sizeof(g_stConfInfo.stThreads[i].topics[g_stConfInfo.stThreads[i].numOfTopic]));
970
      ltrim(g_stConfInfo.stThreads[i].topics[g_stConfInfo.stThreads[i].numOfTopic]);
85,062✔
971
      // printf("%s\n", g_stConfInfo.topics[g_stConfInfo.numOfTopic]);
972
      g_stConfInfo.stThreads[i].numOfTopic++;
85,062✔
973

974
      token = strtok(NULL, delim);
85,062✔
975
    }
976

977
    token = strtok(g_stConfInfo.stThreads[i].keyString, delim);
79,912✔
978
    while (token != NULL) {
399,560✔
979
      // printf("%s\n", token );
980
      {
981
        char* pstr = token;
319,648✔
982
        ltrim(pstr);
319,648✔
983
        char* ret = strchr(pstr, ch);
319,648✔
984
        memcpy(g_stConfInfo.stThreads[i].key[g_stConfInfo.stThreads[i].numOfKey], pstr, ret - pstr);
319,648✔
985
        tstrncpy(g_stConfInfo.stThreads[i].value[g_stConfInfo.stThreads[i].numOfKey], ret + 1,
319,648✔
986
                 sizeof(g_stConfInfo.stThreads[i].value[g_stConfInfo.stThreads[i].numOfKey]));
987
        // printf("key: %s, value: %s\n", g_stConfInfo.key[g_stConfInfo.numOfKey],
988
        // g_stConfInfo.value[g_stConfInfo.numOfKey]);
989
        g_stConfInfo.stThreads[i].numOfKey++;
319,648✔
990
      }
991

992
      token = strtok(NULL, delim);
319,648✔
993
    }
994
  }
995
}
73,182✔
996

997
int32_t getConsumeInfo() {
73,182✔
998
  char sqlStr[1024] = {0};
73,182✔
999

1000
  TAOS* pConn = createNewTaosConnect();
73,182✔
1001
  if (pConn == NULL) {
73,182✔
1002
    taosFprintfFile(g_fp, "taos_connect() fail, can not get consume info for start consumer\n");
×
1003
    return -1;
×
1004
  }
1005

1006
  sprintf(sqlStr, "select * from %s.consumeinfo", g_stConfInfo.cdbName);
73,182✔
1007
  TAOS_RES* pRes = taos_query(pConn, sqlStr);
73,182✔
1008
  if (taos_errno(pRes) != 0) {
73,182✔
1009
    taosFprintfFile(g_fp, "error in get consumeinfo for %s\n", taos_errstr(pRes));
×
1010
    taosCloseFile(&g_fp);
×
1011
    taos_free_result(pRes);
×
1012
    taos_close(pConn);
×
1013
    return -1;
×
1014
  }
1015

1016
  TAOS_ROW    row = NULL;
73,182✔
1017
  int         num_fields = taos_num_fields(pRes);
73,182✔
1018
  TAOS_FIELD* fields = taos_fetch_fields(pRes);
73,182✔
1019

1020
  // schema: ts timestamp, consumerid int, topiclist binary(1024), keylist binary(1024), expectmsgcnt bigint,
1021
  // ifcheckdata int
1022

1023
  int32_t numOfThread = 0;
73,182✔
1024
  while ((row = taos_fetch_row(pRes))) {
153,094✔
1025
    int32_t* lengths = taos_fetch_lengths(pRes);
79,912✔
1026

1027
    // set default value
1028
    // g_stConfInfo.stThreads[numOfThread].autoCommitIntervalMs = 5000;
1029
    // memcpy(g_stConfInfo.stThreads[numOfThread].autoCommit, "true", strlen("true"));
1030
    // memcpy(g_stConfInfo.stThreads[numOfThread].autoOffsetRest, "earlieast", strlen("earlieast"));
1031

1032
    for (int i = 0; i < num_fields; ++i) {
638,174✔
1033
      if (row[i] == NULL || 0 == i) {
558,262✔
1034
        continue;
79,912✔
1035
      }
1036

1037
      if ((1 == i) && (fields[i].type == TSDB_DATA_TYPE_INT)) {
478,350✔
1038
        g_stConfInfo.stThreads[numOfThread].consumerId = *((int32_t*)row[i]);
79,912✔
1039
      } else if ((2 == i) && (fields[i].type == TSDB_DATA_TYPE_BINARY)) {
398,438✔
1040
        memcpy(g_stConfInfo.stThreads[numOfThread].topicString, row[i], lengths[i]);
79,912✔
1041
      } else if ((3 == i) && (fields[i].type == TSDB_DATA_TYPE_BINARY)) {
318,526✔
1042
        memcpy(g_stConfInfo.stThreads[numOfThread].keyString, row[i], lengths[i]);
79,912✔
1043
      } else if ((4 == i) && (fields[i].type == TSDB_DATA_TYPE_BIGINT)) {
238,614✔
1044
        g_stConfInfo.stThreads[numOfThread].expectMsgCnt = *((int64_t*)row[i]);
79,912✔
1045
      } else if ((5 == i) && (fields[i].type == TSDB_DATA_TYPE_INT)) {
158,702✔
1046
        g_stConfInfo.stThreads[numOfThread].ifCheckData = *((int32_t*)row[i]);
79,912✔
1047
      } else if ((6 == i) && (fields[i].type == TSDB_DATA_TYPE_INT)) {
78,790✔
1048
        g_stConfInfo.stThreads[numOfThread].ifManualCommit = *((int32_t*)row[i]);
78,790✔
1049
      }
1050
    }
1051
    numOfThread++;
79,912✔
1052
  }
1053
  g_stConfInfo.numOfThread = numOfThread;
73,182✔
1054

1055
  taos_free_result(pRes);
73,182✔
1056

1057
  parseConsumeInfo();
73,182✔
1058
  taos_close(pConn);
73,182✔
1059

1060
  return 0;
73,182✔
1061
}
1062

1063
static int32_t omb_data_msg_process(TAOS_RES* msg, SThreadInfo* pInfo, int32_t msgIndex, int64_t* lenOfRows) {
×
1064
  char    buf[16 * 1024];
×
1065
  int32_t totalRows = 0;
×
1066
  int32_t totalLen = 0;
×
1067

1068
  // printf("topic: %s\n", tmq_get_topic_name(msg));
1069
  // int32_t     vgroupId = tmq_get_vgroup_id(msg);
1070
  // const char* dbName = tmq_get_db_name(msg);
1071

1072
  // taosFprintfFile(g_fp, "consumerId: %d, msg index:%" PRId64 "\n", pInfo->consumerId, msgIndex);
1073
  // taosFprintfFile(g_fp, "dbName: %s, topic: %s, vgroupId: %d\n", dbName != NULL ? dbName : "invalid table",
1074
  //                 tmq_get_topic_name(msg), vgroupId);
1075

1076
  while (1) {
×
1077
    TAOS_ROW row = taos_fetch_row(msg);
×
1078

1079
    if (row == NULL) break;
×
1080

1081
    TAOS_FIELD* fields = taos_fetch_fields(msg);
×
1082
    int32_t     numOfFields = taos_field_count(msg);
×
1083
    // int32_t*    length = taos_fetch_lengths(msg);
1084
    // int32_t     precision = taos_result_precision(msg);
1085
    // const char* tbName = tmq_get_table_name(msg);
1086

1087
    taos_print_row(buf, row, fields, numOfFields);
×
1088
    totalLen += strlen(buf);
×
1089
    totalRows++;
×
1090
  }
1091

1092
  *lenOfRows = totalLen;
×
1093
  return totalRows;
×
1094
}
1095

1096
void omb_loop_consume(SThreadInfo* pInfo) {
×
1097
  int32_t code;
1098

1099
  int32_t once_flag = 0;
×
1100

1101
  int64_t totalMsgs = 0;
×
1102
  int64_t totalRows = 0;
×
1103

1104
  char tmpString[128];
×
1105
  taosFprintfFile(g_fp, "%s consumer id %d start to loop pull msg\n", getCurrentTimeString(tmpString),
×
1106
                  pInfo->consumerId);
1107
  printf("%s consumer id %d start to loop pull msg\n", getCurrentTimeString(tmpString), pInfo->consumerId);
×
1108

1109
  pInfo->ts = taosGetTimestampMs();
×
1110

1111
  int64_t  lastTotalMsgs = 0;
×
1112
  uint64_t lastPrintTime = taosGetTimestampMs();
×
1113
  uint64_t startTs = taosGetTimestampMs();
×
1114

1115
  int64_t totalLenOfMsg = 0;
×
1116
  int64_t lastTotalLenOfMsg = 0;
×
1117
  int32_t consumeDelay = g_stConfInfo.consumeDelay == -1 ? -1 : (g_stConfInfo.consumeDelay * 1000);
×
1118
  while (running) {
×
1119
    TAOS_RES* tmqMsg = tmq_consumer_poll(pInfo->tmq, consumeDelay);
×
1120
    if (tmqMsg) {
×
1121
      int64_t lenOfMsg = 0;
×
1122
      totalRows += omb_data_msg_process(tmqMsg, pInfo, totalMsgs, &lenOfMsg);
×
1123
      totalLenOfMsg += lenOfMsg;
×
1124
      taos_free_result(tmqMsg);
×
1125
      totalMsgs++;
×
1126
      int64_t currentPrintTime = taosGetTimestampMs();
×
1127
      if (currentPrintTime - lastPrintTime > 10 * 1000) {
×
1128
        int64_t currentLenOfMsg = totalLenOfMsg - lastTotalLenOfMsg;
×
1129
        int64_t deltaTime = currentPrintTime - lastPrintTime;
×
1130
        printf("consumer id %d has currently cons total rows: %" PRId64 ", msgs: %" PRId64
×
1131
               ", rate: %.3f msgs/s, %.1f MB/s\n",
1132
               pInfo->consumerId, totalRows, totalMsgs, (totalMsgs - lastTotalMsgs) * 1000.0 / deltaTime,
×
1133
               currentLenOfMsg * 1000.0 / (1024 * 1024) / deltaTime);
×
1134

1135
        taosFprintfFile(g_fp,
×
1136
                        "consumer id %d has currently poll total msgs: %" PRId64
1137
                        ", period cons rate: %.3f msgs/s, %.1f MB/s\n",
1138
                        pInfo->consumerId, totalMsgs, (totalMsgs - lastTotalMsgs) * 1000.0 / deltaTime,
×
1139
                        currentLenOfMsg * 1000.0 / deltaTime);
×
1140
        lastPrintTime = currentPrintTime;
×
1141
        lastTotalMsgs = totalMsgs;
×
1142
        lastTotalLenOfMsg = totalLenOfMsg;
×
1143
      }
1144
    } else {
1145
      memset(tmpString, 0, tListLen(tmpString));
×
1146
      taosFprintfFile(g_fp, "%s no poll more msg when time over, break consume\n", getCurrentTimeString(tmpString));
×
1147
      printf("%s no poll more msg when time over, break consume\n", getCurrentTimeString(tmpString));
×
1148
      int64_t currentPrintTime = taosGetTimestampMs();
×
1149
      int64_t currentLenOfMsg = totalLenOfMsg - lastTotalLenOfMsg;
×
1150
      int64_t deltaTime = currentPrintTime - lastPrintTime;
×
1151
      printf("consumer id %d has currently cons total rows: %" PRId64 ", msgs: %" PRId64
×
1152
             ", rate: %.3f msgs/s, %.1f MB/s\n",
1153
             pInfo->consumerId, totalRows, totalMsgs, (totalMsgs - lastTotalMsgs) * 1000.0 / deltaTime,
×
1154
             currentLenOfMsg * 1000.0 / (1024 * 1024) / deltaTime);
×
1155
      break;
×
1156
    }
1157
  }
1158

1159
  pInfo->consumeMsgCnt = totalMsgs;
×
1160
  pInfo->consumeRowCnt = totalRows;
×
1161
  pInfo->consumeLen = totalLenOfMsg;
×
1162
}
×
1163

1164
void* ombConsumeThreadFunc(void* param) {
×
1165
  SThreadInfo* pInfo = (SThreadInfo*)param;
×
1166

1167
  //################### set key ########################
1168
  tmq_conf_t* conf = tmq_conf_new();
×
1169
  // tmq_conf_set(conf, "td.connect.ip", "localhost");
1170
  // tmq_conf_set(conf, "td.connect.port", "6030");
1171
  tmq_conf_set(conf, "td.connect.user", "root");
×
1172
  tmq_conf_set(conf, "td.connect.pass", "taosdata");
×
1173
  // tmq_conf_set(conf, "td.connect.db", g_stConfInfo.dbName);
1174
  tmq_conf_set_auto_commit_cb(conf, tmq_commit_cb_print, pInfo);
×
1175
  tmq_conf_set(conf, "group.id", "ombCgrp");
×
1176
  // tmq_conf_set(conf, "msg.with.table.name", "true");
1177
  // tmq_conf_set(conf, "client.id", "c-001");
1178
  // tmq_conf_set(conf, "enable.auto.commit", "true");
1179
  tmq_conf_set(conf, "enable.auto.commit", "false");
×
1180
  // tmq_conf_set(conf, "auto.commit.interval.ms", "1000");
1181
  // tmq_conf_set(conf, "auto.offset.reset", "none");
1182
  // tmq_conf_set(conf, "auto.offset.reset", "earliest");
1183
  tmq_conf_set(conf, "auto.offset.reset", "earliest");
×
1184
  //
1185
  if (g_stConfInfo.useSnapshot) {
×
1186
    tmq_conf_set(conf, "experimental.snapshot.enable", "true");
×
1187
  }
1188

1189
  pInfo->tmq = tmq_consumer_new(conf, NULL, 0);
×
1190

1191
  tmq_conf_destroy(conf);
×
1192

1193
  //################### set topic ##########################
1194
  pInfo->topicList = tmq_list_new();
×
1195
  tmq_list_append(pInfo->topicList, g_stConfInfo.topic);
×
1196

1197
  if ((NULL == pInfo->tmq) || (NULL == pInfo->topicList)) {
×
1198
    taosFprintfFile(g_fp, "create consumer fail! tmq is null or topicList is null\n");
×
1199
    return NULL;
×
1200
  }
1201

1202
  int32_t err = tmq_subscribe(pInfo->tmq, pInfo->topicList);
×
1203
  if (err != 0) {
×
1204
    pError("tmq_subscribe() fail, reason: %s\n", tmq_err2str(err));
×
1205
    taosFprintfFile(g_fp, "tmq_subscribe() fail! reason: %s\n", tmq_err2str(err));
×
1206
    return NULL;
×
1207
  }
1208

1209
  tmq_list_destroy(pInfo->topicList);
×
1210
  pInfo->topicList = NULL;
×
1211

1212
  omb_loop_consume(pInfo);
×
1213

1214
  err = tmq_unsubscribe(pInfo->tmq);
×
1215
  if (err != 0) {
×
1216
    pError("tmq_unsubscribe() fail, reason: %s\n", tmq_err2str(err));
×
1217
    taosFprintfFile(g_fp, "tmq_unsubscribe()! reason: %s\n", tmq_err2str(err));
×
1218
  }
1219

1220
  err = tmq_consumer_close(pInfo->tmq);
×
1221
  if (err != 0) {
×
1222
    pError("tmq_consumer_close() fail, reason: %s\n", tmq_err2str(err));
×
1223
    taosFprintfFile(g_fp, "tmq_consumer_close()! reason: %s\n", tmq_err2str(err));
×
1224
  }
1225
  pInfo->tmq = NULL;
×
1226

1227
  return NULL;
×
1228
}
1229

1230
static int queryDbExec(TAOS* taos, char* command, QUERY_TYPE type) {
×
1231
  TAOS_RES* res = taos_query(taos, command);
×
1232
  int32_t   code = taos_errno(res);
×
1233

1234
  if (code != 0) {
×
1235
    pPrint("%s Failed to execute <%s>, reason: %s %s", GREEN, command, taos_errstr(res), NC);
×
1236
    taos_free_result(res);
×
1237
    return -1;
×
1238
  }
1239

1240
  if (INSERT_TYPE == type) {
×
1241
    int affectedRows = taos_affected_rows(res);
×
1242
    taos_free_result(res);
×
1243
    return affectedRows;
×
1244
  }
1245

1246
  taos_free_result(res);
×
1247
  return 0;
×
1248
}
1249

1250
void* ombProduceThreadFunc(void* param) {
×
1251
  SThreadInfo* pInfo = (SThreadInfo*)param;
×
1252

1253
  pInfo->taos = createNewTaosConnect();
×
1254
  if (pInfo->taos == NULL) {
×
1255
    taosFprintfFile(g_fp, "taos_connect() fail, can not start producers!\n");
×
1256
    return NULL;
×
1257
  }
1258

1259
  int64_t affectedRowsTotal = 0;
×
1260
  int64_t sendMsgs = 0;
×
1261

1262
  uint32_t totalSendLoopTimes =
×
1263
      g_stConfInfo.runDurationMinutes * 60 * 1000 / SEND_TIME_UNIT;  // send some msgs per 10ms
×
1264
  uint32_t batchPerTblTimes = pInfo->producerRate / 100 / g_stConfInfo.batchSize;
×
1265
  uint32_t remainder = (pInfo->producerRate / 100) % g_stConfInfo.batchSize;
×
1266
  if (remainder) {
×
1267
    batchPerTblTimes += 1;
×
1268
  }
1269

1270
  char* sqlBuf = taosMemoryMalloc(MAX_SQL_LEN);
×
1271
  if (NULL == sqlBuf) {
×
1272
    printf("malloc fail for sqlBuf\n");
×
1273
    taos_close(pInfo->taos);
×
1274
    pInfo->taos = NULL;
×
1275
    return NULL;
×
1276
  }
1277

1278
  printf("Produce Info: totalSendLoopTimes: %d, batchPerTblTimes: %d, producerRate: %d\n", totalSendLoopTimes,
×
1279
         batchPerTblTimes, pInfo->producerRate);
1280

1281
  char ctbName[128] = {0};
×
1282
  sprintf(ctbName, "%s.ctb%d", g_stConfInfo.dbName, pInfo->consumerId);
×
1283

1284
  int64_t lastPrintTime = taosGetTimestampUs();
×
1285
  int64_t totalMsgLen = 0;
×
1286
  // int64_t timeStamp = taosGetTimestampUs();
1287
  while (totalSendLoopTimes) {
×
1288
    int64_t startTs = taosGetTimestampUs();
×
1289
    for (int i = 0; i < batchPerTblTimes; ++i) {
×
1290
      uint32_t msgsOfSql = g_stConfInfo.batchSize;
×
1291
      if ((i == batchPerTblTimes - 1) && (0 != remainder)) {
×
1292
        msgsOfSql = remainder;
×
1293
      }
1294
      int len = 0;
×
1295
      len += tsnprintf(sqlBuf + len, MAX_SQL_LEN - len, "insert into %s values ", ctbName);
×
1296
      for (int j = 0; j < msgsOfSql; j++) {
×
1297
        int64_t timeStamp = taosGetTimestampNs();
×
1298
        len += tsnprintf(sqlBuf + len, MAX_SQL_LEN - len, "(%" PRId64 ", \"%s\")", timeStamp, g_payload);
×
1299
        sendMsgs++;
×
1300
        pInfo->totalProduceMsgs++;
×
1301
      }
1302

1303
      totalMsgLen += len;
×
1304
      pInfo->totalMsgsLen += len;
×
1305

1306
      int64_t affectedRows = queryDbExec(pInfo->taos, sqlBuf, INSERT_TYPE);
×
1307
      if (affectedRows < 0) {
×
1308
        taos_close(pInfo->taos);
×
1309
        pInfo->taos = NULL;
×
1310
        taosMemoryFree(sqlBuf);
×
1311
        return NULL;
×
1312
      }
1313

1314
      affectedRowsTotal += affectedRows;
×
1315

1316
      // printf("Produce Info: affectedRows: %" PRId64 "\n", affectedRows);
1317
    }
1318
    totalSendLoopTimes -= 1;
×
1319

1320
    // calc spent time
1321
    int64_t currentTs = taosGetTimestampUs();
×
1322
    int64_t delta = currentTs - startTs;
×
1323
    if (delta < SEND_TIME_UNIT * 1000) {
×
1324
      int64_t sleepLen = (int32_t)(SEND_TIME_UNIT * 1000 - delta);
×
1325
      // printf("sleep %" PRId64 " us, use time: %" PRId64 " us\n", sleepLen, delta);
1326
      taosUsleep((int32_t)sleepLen);
×
1327
    }
1328

1329
    currentTs = taosGetTimestampUs();
×
1330
    delta = currentTs - lastPrintTime;
×
1331
    if (delta > 10 * 1000 * 1000) {
×
1332
      printf("producer[%d] info: %" PRId64 " msgs, %" PRId64 " Byte, %" PRId64 " us, totalSendLoopTimes: %d\n",
×
1333
             pInfo->consumerId, sendMsgs, totalMsgLen, delta, totalSendLoopTimes);
1334
      printf("producer[%d] rate: %1.f msgs/s, %1.f KB/s\n", pInfo->consumerId, sendMsgs * 1000.0 * 1000 / delta,
×
1335
             (totalMsgLen / 1024.0) / (delta / (1000 * 1000)));
×
1336
      lastPrintTime = currentTs;
×
1337
      sendMsgs = 0;
×
1338
      totalMsgLen = 0;
×
1339
    }
1340
  }
1341

1342
  printf("affectedRowsTotal: %" PRId64 "\n", affectedRowsTotal);
×
1343
  taos_close(pInfo->taos);
×
1344
  pInfo->taos = NULL;
×
1345
  taosMemoryFree(sqlBuf);
×
1346
  return NULL;
×
1347
}
1348

1349
void printProduceInfo(int64_t start) {
×
1350
  int64_t totalMsgs = 0;
×
1351
  int64_t totalLenOfMsgs = 0;
×
1352
  for (int i = 0; i < g_stConfInfo.producers; i++) {
×
1353
    totalMsgs += g_stConfInfo.stProdThreads[i].totalProduceMsgs;
×
1354
    totalLenOfMsgs += g_stConfInfo.stProdThreads[i].totalMsgsLen;
×
1355
  }
1356

1357
  int64_t end = taosGetTimestampUs();
×
1358

1359
  int64_t t = end - start;
×
1360
  if (0 == t) t = 1;
×
1361

1362
  double tInMs = (double)t / 1000000.0;
×
1363
  printf("Spent %.3f seconds to prod %" PRIu64 " msgs, %" PRIu64 " Byte\n\n", tInMs, totalMsgs, totalLenOfMsgs);
×
1364

1365
  printf("Spent %.3f seconds to prod %" PRIu64 " msgs with %d producer(s), throughput: %.3f msgs/s, %.1f MB/s\n\n",
×
1366
         tInMs, totalMsgs, g_stConfInfo.producers, (double)totalMsgs / tInMs,
×
1367
         (double)totalLenOfMsgs / (1024.0 * 1024) / tInMs);
×
1368
  return;
×
1369
}
1370

1371
void startOmbConsume() {
×
1372
  TdThreadAttr thattr;
×
1373
  taosThreadAttrInit(&thattr);
×
1374
  taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE);
×
1375

1376
  if (0 != g_stConfInfo.producers) {
×
1377
    TAOS* taos = createNewTaosConnect();
×
1378
    if (taos == NULL) {
×
1379
      taosFprintfFile(g_fp, "taos_connect() fail, can not create db, stbl, ctbl, topic!\n");
×
1380
      return;
×
1381
    }
1382

1383
    char stbName[16] = "stb";
×
1384
    char ctbPrefix[16] = "ctb";
×
1385

1386
    char sql[256] = {0};
×
1387
    sprintf(sql, "drop database if exists %s", g_stConfInfo.dbName);
×
1388
    printf("SQL: %s\n", sql);
×
1389
    queryDbExec(taos, sql, NO_INSERT_TYPE);
×
1390

1391
    sprintf(sql, "create database if not exists %s precision 'ns' vgroups %d", g_stConfInfo.dbName,
×
1392
            g_stConfInfo.producers);
1393
    printf("SQL: %s\n", sql);
×
1394
    queryDbExec(taos, sql, NO_INSERT_TYPE);
×
1395

1396
    sprintf(sql, "create stable %s.%s (ts timestamp, payload binary(%d)) tags (t bigint) ", g_stConfInfo.dbName,
×
1397
            stbName, g_stConfInfo.payloadLen);
1398
    printf("SQL: %s\n", sql);
×
1399
    queryDbExec(taos, sql, NO_INSERT_TYPE);
×
1400

1401
    for (int i = 0; i < g_stConfInfo.producers; i++) {
×
1402
      sprintf(sql, "create table %s.%s%d using %s.stb tags(%d) ", g_stConfInfo.dbName, ctbPrefix, i,
×
1403
              g_stConfInfo.dbName, i);
1404
      printf("SQL: %s\n", sql);
×
1405
      queryDbExec(taos, sql, NO_INSERT_TYPE);
×
1406
    }
1407

1408
    // create topic
1409
    sprintf(sql, "create topic %s as stable %s.%s", g_stConfInfo.topic, g_stConfInfo.dbName, stbName);
×
1410
    printf("SQL: %s\n", sql);
×
1411
    queryDbExec(taos, sql, NO_INSERT_TYPE);
×
1412

1413
    int32_t producerRate = ceil(((double)g_stConfInfo.producerRate) / g_stConfInfo.producers);
×
1414

1415
    printf("==== create %d produce thread ====\n", g_stConfInfo.producers);
×
1416
    for (int32_t i = 0; i < g_stConfInfo.producers; ++i) {
×
1417
      g_stConfInfo.stProdThreads[i].consumerId = i;
×
1418
      g_stConfInfo.stProdThreads[i].producerRate = producerRate;
×
1419
      taosThreadCreate(&(g_stConfInfo.stProdThreads[i].thread), &thattr, ombProduceThreadFunc,
×
1420
                       (void*)(&(g_stConfInfo.stProdThreads[i])));
×
1421
    }
1422

1423
    if (0 == g_stConfInfo.numOfThread) {
×
1424
      int64_t start = taosGetTimestampUs();
×
1425
      for (int32_t i = 0; i < g_stConfInfo.producers; i++) {
×
1426
        taosThreadJoin(g_stConfInfo.stProdThreads[i].thread, NULL);
×
1427
        taosThreadClear(&g_stConfInfo.stProdThreads[i].thread);
×
1428
      }
1429

1430
      printProduceInfo(start);
×
1431

1432
      taosFprintfFile(g_fp, "==== close tmqlog ====\n");
×
1433
      taosCloseFile(&g_fp);
×
1434
      taos_close(taos);
×
1435
      return;
×
1436
    }
1437

1438
    taos_close(taos);
×
1439
  }
1440

1441
  // pthread_create one thread to consume
1442
  taosFprintfFile(g_fp, "==== create %d consume thread ====\n", g_stConfInfo.numOfThread);
×
1443
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; ++i) {
×
1444
    g_stConfInfo.stThreads[i].consumerId = i;
×
1445
    taosThreadCreate(&(g_stConfInfo.stThreads[i].thread), &thattr, ombConsumeThreadFunc,
×
1446
                     (void*)(&(g_stConfInfo.stThreads[i])));
×
1447
  }
1448

1449
  int64_t start = taosGetTimestampUs();
×
1450

1451
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; i++) {
×
1452
    taosThreadJoin(g_stConfInfo.stThreads[i].thread, NULL);
×
1453
    taosThreadClear(&g_stConfInfo.stThreads[i].thread);
×
1454
  }
1455

1456
  int64_t end = taosGetTimestampUs();
×
1457

1458
  int64_t totalRows = 0;
×
1459
  int64_t totalMsgs = 0;
×
1460
  int64_t totalLenOfMsgs = 0;
×
1461
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; i++) {
×
1462
    totalMsgs += g_stConfInfo.stThreads[i].consumeMsgCnt;
×
1463
    totalLenOfMsgs += g_stConfInfo.stThreads[i].consumeLen;
×
1464
    totalRows += g_stConfInfo.stThreads[i].consumeRowCnt;
×
1465
  }
1466

1467
  int64_t t = end - start;
×
1468
  if (0 == t) t = 1;
×
1469

1470
  double tInMs = (double)t / 1000000.0;
×
1471
  taosFprintfFile(
×
1472
      g_fp, "Spent %.3f seconds to poll msgs: %" PRIu64 " with %d thread(s), throughput: %.3f msgs/s, %.1f MB/s\n\n",
1473
      tInMs, totalMsgs, g_stConfInfo.numOfThread, (double)(totalMsgs / tInMs),
×
1474
      (double)totalLenOfMsgs / (1024 * 1024) / tInMs);
×
1475

1476
  printf("Spent %.3f seconds to cons rows: %" PRIu64 " msgs: %" PRIu64
×
1477
         " with %d thread(s), throughput: %.3f msgs/s, %.1f MB/s\n\n",
1478
         tInMs, totalRows, totalMsgs, g_stConfInfo.numOfThread, (double)(totalMsgs / tInMs),
×
1479
         (double)totalLenOfMsgs / (1024 * 1024) / tInMs);
×
1480

1481
  taosFprintfFile(g_fp, "==== close tmqlog ====\n");
×
1482
  taosCloseFile(&g_fp);
×
1483

1484
  return;
×
1485
}
1486

1487
int main(int32_t argc, char* argv[]) {
73,182✔
1488
  parseArgument(argc, argv);
73,182✔
1489

1490
  if (0 != strlen(g_stConfInfo.topic)) {
73,182✔
1491
    startOmbConsume();
×
1492
    return 0;
×
1493
  }
1494

1495
  int32_t retCode = getConsumeInfo();
73,182✔
1496
  if (0 != retCode) {
73,182✔
1497
    return -1;
×
1498
  }
1499

1500
  saveConfigToLogFile();
73,182✔
1501

1502
  tmqSetSignalHandle();
73,182✔
1503

1504
  TdThreadAttr thattr;
68,971✔
1505
  taosThreadAttrInit(&thattr);
73,182✔
1506
  taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE);
73,182✔
1507

1508
  // pthread_create one thread to consume
1509
  taosFprintfFile(g_fp, "==== create %d consume thread ====\n", g_stConfInfo.numOfThread);
73,182✔
1510
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; ++i) {
153,094✔
1511
    taosThreadCreate(&(g_stConfInfo.stThreads[i].thread), &thattr, consumeThreadFunc,
79,912✔
1512
                     (void*)(&(g_stConfInfo.stThreads[i])));
79,912✔
1513
  }
1514

1515
  int64_t start = taosGetTimestampUs();
73,182✔
1516

1517
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; i++) {
153,094✔
1518
    taosThreadJoin(g_stConfInfo.stThreads[i].thread, NULL);
79,912✔
1519
    taosThreadClear(&g_stConfInfo.stThreads[i].thread);
79,912✔
1520
  }
1521

1522
  int64_t end = taosGetTimestampUs();
73,182✔
1523

1524
  int64_t totalMsgs = 0;
73,182✔
1525
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; i++) {
153,094✔
1526
    totalMsgs += g_stConfInfo.stThreads[i].consumeMsgCnt;
79,912✔
1527
  }
1528

1529
  int64_t t = end - start;
73,182✔
1530
  if (0 == t) t = 1;
73,182✔
1531

1532
  double tInMs = (double)t / 1000000.0;
73,182✔
1533
  taosFprintfFile(g_fp,
73,182✔
1534
                  "Spent %.3f seconds to poll msgs: %" PRIu64 " with %d thread(s), throughput: %.3f msgs/second\n\n",
1535
                  tInMs, totalMsgs, g_stConfInfo.numOfThread, (double)(totalMsgs / tInMs));
68,971✔
1536

1537
  taosFprintfFile(g_fp, "==== close tmqlog ====\n");
73,182✔
1538
  taosCloseFile(&g_fp);
73,182✔
1539

1540
  return 0;
73,182✔
1541
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc