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

taosdata / TDengine / #4983

13 Mar 2026 03:38AM UTC coverage: 68.653% (+0.07%) from 68.587%
#4983

push

travis-ci

web-flow
feat/6641435300-save-audit-in-self (#34738)

434 of 584 new or added lines in 10 files covered. (74.32%)

434 existing lines in 121 files now uncovered.

212745 of 309883 relevant lines covered (68.65%)

134272959.11 hits per line

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

48.43
/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) {
19,366,690✔
167
  time_t    tTime = taosGetTimestampSec();
19,366,690✔
168
  struct tm tm;
19,225,844✔
169
  taosLocalTime(&tTime, &tm, NULL, 0, NULL);
19,366,690✔
170
  sprintf(timeString, "%d-%02d-%02d %02d:%02d:%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
19,366,690✔
171
          tm.tm_min, tm.tm_sec);
172

173
  return timeString;
19,366,690✔
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() { 
69,663✔
183
  taosSetSignal(SIGINT, tmqStop); 
69,663✔
184
  taosSetSignal(SIGTERM, tmqStop); 
69,663✔
185
}
69,663✔
186

187
void initLogFile() {
69,663✔
188
  char filename[256];
65,631✔
189
  char tmpString[128];
65,631✔
190

191
  pid_t process_id = taosGetPId();
69,663✔
192

193
  if (0 != strlen(g_stConfInfo.topic)) {
69,663✔
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));
69,663✔
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);
69,663✔
205
  if (NULL == pFile) {
69,663✔
206
    fprintf(stderr, "Failed to open %s for save result\n", filename);
×
207
    exit(-1);
×
208
  }
209
  g_fp = pFile;
69,663✔
210
}
69,663✔
211

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

223
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; i++) {
145,737✔
224
    taosFprintfFile(g_fp, "# consumer %d info:\n", g_stConfInfo.stThreads[i].consumerId);
76,074✔
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: ");
76,074✔
229
    for (int j = 0; j < g_stConfInfo.stThreads[i].numOfTopic; j++) {
157,060✔
230
      taosFprintfFile(g_fp, "%s, ", g_stConfInfo.stThreads[i].topics[j]);
80,986✔
231
    }
232
    taosFprintfFile(g_fp, "\n");
76,074✔
233
    taosFprintfFile(g_fp, "  Key: ");
76,074✔
234
    for (int k = 0; k < g_stConfInfo.stThreads[i].numOfKey; k++) {
380,370✔
235
      taosFprintfFile(g_fp, "%s:%s, ", g_stConfInfo.stThreads[i].key[k], g_stConfInfo.stThreads[i].value[k]);
304,296✔
236
    }
237
    taosFprintfFile(g_fp, "\n");
76,074✔
238
    taosFprintfFile(g_fp, "  expect rows: %" PRId64 "\n", g_stConfInfo.stThreads[i].expectMsgCnt);
76,074✔
239
  }
240

241
  char tmpString[128];
65,631✔
242
  taosFprintfFile(g_fp, "# Test time:                %s\n", getCurrentTimeString(tmpString));
69,663✔
243
  taosFprintfFile(g_fp, "###################################################################\n");
69,663✔
244
}
69,663✔
245

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

256
  g_stConfInfo.nowTime = taosGetTimestampMs();
69,663✔
257

258
  for (int32_t i = 1; i < argc; i++) {
539,675✔
259
    if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
470,012✔
260
      printHelp();
×
261
      exit(0);
×
262
    } else if (strcmp(argv[i], "-d") == 0) {
470,012✔
263
      tstrncpy(g_stConfInfo.dbName, argv[++i], sizeof(g_stConfInfo.dbName));
69,663✔
264
    } else if (strcmp(argv[i], "-w") == 0) {
400,349✔
265
      tstrncpy(g_stConfInfo.cdbName, argv[++i], sizeof(g_stConfInfo.cdbName));
69,663✔
266
    } else if (strcmp(argv[i], "-c") == 0) {
330,686✔
267
      tstrncpy(configDir, argv[++i], PATH_MAX);
69,663✔
268
    } else if (strcmp(argv[i], "-g") == 0) {
261,023✔
269
      g_stConfInfo.showMsgFlag = atol(argv[++i]);
69,663✔
270
    } else if (strcmp(argv[i], "-r") == 0) {
191,360✔
271
      g_stConfInfo.showRowFlag = atol(argv[++i]);
69,663✔
272
    } else if (strcmp(argv[i], "-s") == 0) {
121,697✔
273
      g_stConfInfo.saveRowFlag = atol(argv[++i]);
×
274
    } else if (strcmp(argv[i], "-y") == 0) {
121,697✔
275
      g_stConfInfo.consumeDelay = atol(argv[++i]);
69,663✔
276
    } else if (strcmp(argv[i], "-e") == 0) {
52,034✔
277
      g_stConfInfo.useSnapshot = atol(argv[++i]);
52,034✔
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);
69,663✔
305
  if (NULL == g_payload) {
69,663✔
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++) {
69,663✔
311
    strcpy(&g_payload[i], "a");
×
312
  }
313

314
  initLogFile();
69,663✔
315

316
  taosFprintfFile(g_fp, "====parseArgument() success\n");
69,663✔
317

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

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

329
  pPrint("%s omb topic:%s %s", GREEN, g_stConfInfo.topic, NC);
69,663✔
330
  pPrint("%s numOfThread:%d %s", GREEN, g_stConfInfo.numOfThread, NC);
69,663✔
331
#endif
332
}
69,663✔
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) {
385,282✔
343
  if (str == NULL || *str == '\0') {
385,282✔
344
    return;
×
345
  }
346
  int   len = 0;
385,282✔
347
  char* p = str;
385,282✔
348
  while (*p != '\0' && isspace(*p)) {
3,134,116✔
349
    ++p;
2,748,834✔
350
    ++len;
2,748,834✔
351
  }
352
  memmove(str, p, strlen(str) - len + 1);
385,282✔
353
  // return str;
354
}
355

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

361
  while (retryCnt--) {
76,074✔
362
    pRes = taos_query(taos, command);
76,074✔
363
    code = taos_errno(pRes);
76,074✔
364
    if (code != 0) {
76,074✔
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);
76,074✔
372
    return 0;
76,074✔
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) {
13,807,723✔
381
  int32_t i;
382
  for (i = 0; i < pInfo->numOfVgroups; i++) {
26,975,310✔
383
    if (vgroupId == pInfo->rowsOfPerVgroups[i][0]) {
26,799,258✔
384
      pInfo->rowsOfPerVgroups[i][1] += rows;
13,631,671✔
385
      return;
13,631,671✔
386
    }
387
  }
388

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

393
  taosFprintfFile(g_fp, "consume id %d, add new vgroupId:%d\n", pInfo->consumerId, vgroupId);
176,052✔
394
  if (pInfo->numOfVgroups > MAX_VGROUP_CNT) {
176,052✔
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() {
145,737✔
403
  TAOS*   taos = NULL;
145,737✔
404
  int32_t retryCnt = 10;
145,737✔
405

406
  while (retryCnt--) {
145,737✔
407
    taos = taos_connect(NULL, "root", "taosdata", NULL, 0);
145,737✔
408
    if (NULL != taos) {
145,737✔
409
      return taos;
145,737✔
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);
14,400,000✔
458
    ms = val % 1000000000;
14,400,000✔
459
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
2,147,483,647✔
460
    tt = (time_t)(val / 1000000);
14,550,000✔
461
    ms = val % 1000000;
14,550,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);
14,391,072✔
486
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
2,147,483,647✔
487
    sprintf(buf + pos, ".%06d", ms);
14,550,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");
134,911,595✔
499
    return;
134,913,225✔
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:
78,420✔
537
      taosFprintfFile(pFile, "%.5f", GET_FLOAT_VAL(val));
78,420✔
538
      break;
78,420✔
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));
89,315,033✔
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) {
13,807,723✔
586
  char    buf[1024];
13,698,999✔
587
  int32_t totalRows = 0;
13,807,723✔
588

589
  int32_t     vgroupId = tmq_get_vgroup_id(msg);
13,807,723✔
590
  const char* dbName = tmq_get_db_name(msg);
13,807,723✔
591

592
  char timestring[128] = {0};
13,807,723✔
593
  taosFprintfFile(g_fp, "%s consumerId: %d, msg index:%d\n", getCurrentTimeString(timestring), pInfo->consumerId, msgIndex);
13,807,723✔
594
  int32_t index = 0;
13,807,723✔
595
  for (index = 0; index < pInfo->numOfVgroups; index++) {
26,975,310✔
596
    if (vgroupId == pInfo->rowsOfPerVgroups[index][0]) {
26,799,258✔
597
      break;
13,631,311✔
598
    }
599
  }
600

601
  taosFprintfFile(g_fp, "dbName: %s, topic: %s, vgroupId:%d, currentRows:%d\n", dbName != NULL ? dbName : "invalid table",
13,807,363✔
602
                  tmq_get_topic_name(msg), vgroupId, pInfo->rowsOfPerVgroups[index][1]);
13,698,639✔
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;
13,807,723✔
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);
13,807,723✔
640
  return totalRows;
13,807,723✔
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) {}
129,413✔
685

686
int32_t notifyMainScript(SThreadInfo* pInfo, int32_t cmdId) {
129,413✔
687
  char sqlStr[1024] = {0};
129,413✔
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,
129,413✔
691
          atomic_fetch_add_64(&g_stConfInfo.nowTime, 1), cmdId, pInfo->consumerId);
692

693
  taos_query_a(pInfo->taos, sqlStr, appNothing, NULL);
129,413✔
694

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

697
  return 0;
129,413✔
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,172,045✔
703
  taosFprintfFile(g_fp, "tmq_commit_cb_print() commit %d\n", code);
4,172,045✔
704

705
  if (0 == g_once_commit_flag && code == 0) {
4,172,045✔
706
    g_once_commit_flag = 1;
58,825✔
707
    notifyMainScript((SThreadInfo*)param, (int32_t)NOTIFY_CMD_START_COMMIT);
58,825✔
708
  }
709

710
  char tmpString[128];
4,164,856✔
711
  taosFprintfFile(g_fp, "%s tmq_commit_cb_print() be called\n", getCurrentTimeString(tmpString));
4,172,045✔
712
}
4,172,045✔
713

714
void build_consumer(SThreadInfo* pInfo) {
76,074✔
715
  tmq_conf_t* conf = tmq_conf_new();
76,074✔
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");
76,074✔
720
  tmq_conf_set(conf, "td.connect.pass", "taosdata");
76,021✔
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);
76,074✔
725

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

731
  tmq_conf_set(conf, "msg.with.table.name", "true");
76,074✔
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) {
76,074✔
745
    tmq_conf_set(conf, "experimental.snapshot.enable", "true");
24,692✔
746
  }
747

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

753
void build_topic_list(SThreadInfo* pInfo) {
76,074✔
754
  pInfo->topicList = tmq_list_new();
76,074✔
755
  // tmq_list_append(topic_list, "test_stb_topic_1");
756
  for (int32_t i = 0; i < pInfo->numOfTopic; i++) {
157,060✔
757
    tmq_list_append(pInfo->topicList, pInfo->topics[i]);
80,986✔
758
  }
759
  return;
76,074✔
760
}
761

762
int32_t saveConsumeResult(SThreadInfo* pInfo) {
76,074✔
763
  char sqlStr[1024] = {0};
76,074✔
764
  // schema: ts timestamp, consumerid int, consummsgcnt bigint, checkresult int
765
  sprintf(sqlStr, "insert into %s.consumeresult values (%" PRId64 ", %d, %" PRId64 ", %" PRId64 ", %d)",
76,074✔
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];
70,451✔
770
  taosFprintfFile(g_fp, "%s, consume id %d result: %s\n", getCurrentTimeString(tmpString), pInfo->consumerId, sqlStr);
76,074✔
771

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

778
  return 0;
76,074✔
779
}
780

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

784
  int32_t once_flag = 0;
76,074✔
785

786
  int64_t totalMsgs = 0;
76,074✔
787
  int64_t totalRows = 0;
76,074✔
788

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

793
  pInfo->ts = taosGetTimestampMs();
76,074✔
794

795
  if (pInfo->ifCheckData) {
76,074✔
796
    char filename[256] = {0};
46,226✔
797
    memset(tmpString, 0, tListLen(tmpString));
46,226✔
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);
46,226✔
802
    pInfo->pConsumeRowsFile = taosOpenFile(filename, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM);
46,226✔
803

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

807
    if (pInfo->pConsumeRowsFile == NULL || pInfo->pConsumeMetaFile == NULL) {
46,226✔
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;
76,074✔
814
  uint64_t lastPrintTime = taosGetTimestampMs();
76,074✔
815
  uint64_t startTs = taosGetTimestampMs();
76,074✔
816

817
  int32_t consumeDelay = g_stConfInfo.consumeDelay == -1 ? -1 : (g_stConfInfo.consumeDelay * 1000);
76,074✔
818
  while (running) {
14,860,020✔
819
    TAOS_RES* tmqMsg = tmq_consumer_poll(pInfo->tmq, consumeDelay);
14,860,020✔
820
    if (tmqMsg) {
14,861,513✔
821
      if (0 != g_stConfInfo.showMsgFlag) {
13,807,723✔
822
        tmq_res_t msgType = tmq_get_res_type(tmqMsg);
13,807,723✔
823
        if (msgType == TMQ_RES_TABLE_META) {
13,807,723✔
824
          totalRows += meta_msg_process(tmqMsg, pInfo, totalMsgs);
×
825
        } else if (msgType == TMQ_RES_DATA) {
13,807,723✔
826
          totalRows += data_msg_process(tmqMsg, pInfo, totalMsgs);
13,807,723✔
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);
13,807,723✔
834
      totalMsgs++;
13,805,923✔
835

836
      int64_t currentPrintTime = taosGetTimestampMs();
13,804,843✔
837
      if (currentPrintTime - lastPrintTime > 10 * 1000) {
13,804,843✔
838
        taosFprintfFile(
48,818✔
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));
48,818✔
841
        lastPrintTime = currentPrintTime;
48,818✔
842
        lastTotalMsgs = totalMsgs;
48,818✔
843
      }
844

845
      if (0 == once_flag) {
13,804,843✔
846
        once_flag = 1;
70,588✔
847
        notifyMainScript(pInfo, NOTIFY_CMD_START_CONSUM);
70,588✔
848
      }
849

850
      if ((totalRows >= pInfo->expectMsgCnt) || (totalMsgs >= pInfo->expectMsgCnt)) {
13,804,843✔
851
        memset(tmpString, 0, tListLen(tmpString));
40,938✔
852
        taosFprintfFile(g_fp, "%s over than expect rows, so break consume\n", getCurrentTimeString(tmpString));
40,938✔
853
        break;
41,658✔
854
      }
855
    } else {
856
      memset(tmpString, 0, tListLen(tmpString));
1,053,790✔
857
      code = taos_errno(NULL);
1,053,790✔
858
      if (code == 0) {
1,053,790✔
859
        taosFprintfFile(g_fp, "%s no poll more msg when time over, break consume, errMsg:%s\n", getCurrentTimeString(tmpString), taos_errstr(NULL));
33,033✔
860
        break;
33,033✔
861
      } else if (code == TSDB_CODE_VND_INVALID_VGROUP_ID ||
1,020,757✔
862
                 code == TSDB_CODE_SYN_NOT_LEADER ||
1,383✔
863
                 code == TSDB_CODE_APP_IS_STARTING ||
1,383✔
864
                 code == TSDB_CODE_APP_IS_STOPPING) {
865
        taosFprintfFile(g_fp, "%s poll err:%s, continue\n", getCurrentTimeString(tmpString), taos_errstr(NULL));
1,019,374✔
866
      } else {
867
        taosFprintfFile(g_fp, "%s poll err:%s, break\n", getCurrentTimeString(tmpString), taos_errstr(NULL));
1,383✔
868
        break;
1,383✔
869
      }
870
    }
871
  }
872

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

877
  pInfo->consumeMsgCnt = totalMsgs;
76,074✔
878
  pInfo->consumeRowCnt = totalRows;
76,074✔
879

880
  taosFprintfFile(g_fp, "==== consumerId: %d, consumeMsgCnt: %" PRId64 ", consumeRowCnt: %" PRId64 "\n",
76,074✔
881
                  pInfo->consumerId, pInfo->consumeMsgCnt, pInfo->consumeRowCnt);
882

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

889
void* consumeThreadFunc(void* param) {
76,074✔
890
  SThreadInfo* pInfo = (SThreadInfo*)param;
76,074✔
891

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

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

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

916
  tmq_list_destroy(pInfo->topicList);
76,074✔
917
  pInfo->topicList = NULL;
76,074✔
918

919
  loop_consume(pInfo);
76,074✔
920

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

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

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

943
  // save consume result into consumeresult table
944
  saveConsumeResult(pInfo);
76,074✔
945

946
  // save rows from per vgroup
947
  taosFprintfFile(g_fp, "======== consumerId: %d, consume rows from per vgroups ========\n", pInfo->consumerId);
76,074✔
948
  for (int32_t i = 0; i < pInfo->numOfVgroups; i++) {
252,126✔
949
    taosFprintfFile(g_fp, "vgroups: %04d, rows: %d\n", pInfo->rowsOfPerVgroups[i][0], pInfo->rowsOfPerVgroups[i][1]);
176,052✔
950
  }
951

952
  taos_close(pInfo->taos);
76,074✔
953
  pInfo->taos = NULL;
76,074✔
954

955
  return NULL;
76,074✔
956
}
957

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

963
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; i++) {
145,737✔
964
    token = strtok(g_stConfInfo.stThreads[i].topicString, delim);
76,074✔
965
    while (token != NULL) {
157,060✔
966
      // printf("%s\n", token );
967
      tstrncpy(g_stConfInfo.stThreads[i].topics[g_stConfInfo.stThreads[i].numOfTopic], token,
80,986✔
968
               sizeof(g_stConfInfo.stThreads[i].topics[g_stConfInfo.stThreads[i].numOfTopic]));
969
      ltrim(g_stConfInfo.stThreads[i].topics[g_stConfInfo.stThreads[i].numOfTopic]);
80,986✔
970
      // printf("%s\n", g_stConfInfo.topics[g_stConfInfo.numOfTopic]);
971
      g_stConfInfo.stThreads[i].numOfTopic++;
80,986✔
972

973
      token = strtok(NULL, delim);
80,986✔
974
    }
975

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

991
      token = strtok(NULL, delim);
304,296✔
992
    }
993
  }
994
}
69,663✔
995

996
int32_t getConsumeInfo() {
69,663✔
997
  char sqlStr[1024] = {0};
69,663✔
998

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

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

1015
  TAOS_ROW    row = NULL;
69,663✔
1016
  int         num_fields = taos_num_fields(pRes);
69,663✔
1017
  TAOS_FIELD* fields = taos_fetch_fields(pRes);
69,663✔
1018

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

1022
  int32_t numOfThread = 0;
69,663✔
1023
  while ((row = taos_fetch_row(pRes))) {
145,737✔
1024
    int32_t* lengths = taos_fetch_lengths(pRes);
76,074✔
1025

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

1031
    for (int i = 0; i < num_fields; ++i) {
607,512✔
1032
      if (row[i] == NULL || 0 == i) {
531,438✔
1033
        continue;
76,074✔
1034
      }
1035

1036
      if ((1 == i) && (fields[i].type == TSDB_DATA_TYPE_INT)) {
455,364✔
1037
        g_stConfInfo.stThreads[numOfThread].consumerId = *((int32_t*)row[i]);
76,074✔
1038
      } else if ((2 == i) && (fields[i].type == TSDB_DATA_TYPE_BINARY)) {
379,290✔
1039
        memcpy(g_stConfInfo.stThreads[numOfThread].topicString, row[i], lengths[i]);
76,074✔
1040
      } else if ((3 == i) && (fields[i].type == TSDB_DATA_TYPE_BINARY)) {
303,216✔
1041
        memcpy(g_stConfInfo.stThreads[numOfThread].keyString, row[i], lengths[i]);
76,074✔
1042
      } else if ((4 == i) && (fields[i].type == TSDB_DATA_TYPE_BIGINT)) {
227,142✔
1043
        g_stConfInfo.stThreads[numOfThread].expectMsgCnt = *((int64_t*)row[i]);
76,074✔
1044
      } else if ((5 == i) && (fields[i].type == TSDB_DATA_TYPE_INT)) {
151,068✔
1045
        g_stConfInfo.stThreads[numOfThread].ifCheckData = *((int32_t*)row[i]);
76,074✔
1046
      } else if ((6 == i) && (fields[i].type == TSDB_DATA_TYPE_INT)) {
74,994✔
1047
        g_stConfInfo.stThreads[numOfThread].ifManualCommit = *((int32_t*)row[i]);
74,994✔
1048
      }
1049
    }
1050
    numOfThread++;
76,074✔
1051
  }
1052
  g_stConfInfo.numOfThread = numOfThread;
69,663✔
1053

1054
  taos_free_result(pRes);
69,663✔
1055

1056
  parseConsumeInfo();
69,663✔
1057
  taos_close(pConn);
69,663✔
1058

1059
  return 0;
69,663✔
1060
}
1061

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

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

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

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

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

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

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

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

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

1098
  int32_t once_flag = 0;
×
1099

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

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

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

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

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

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

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

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

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

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

1190
  tmq_conf_destroy(conf);
×
1191

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

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

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

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

1211
  omb_loop_consume(pInfo);
×
1212

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

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

1226
  return NULL;
×
1227
}
1228

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1313
      affectedRowsTotal += affectedRows;
×
1314

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

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

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

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

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

1356
  int64_t end = taosGetTimestampUs();
×
1357

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1429
      printProduceInfo(start);
×
1430

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

1437
    taos_close(taos);
×
1438
  }
1439

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

1448
  int64_t start = taosGetTimestampUs();
×
1449

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

1455
  int64_t end = taosGetTimestampUs();
×
1456

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

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

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

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

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

1483
  return;
×
1484
}
1485

1486
int main(int32_t argc, char* argv[]) {
69,663✔
1487
  parseArgument(argc, argv);
69,663✔
1488

1489
  if (0 != strlen(g_stConfInfo.topic)) {
69,663✔
1490
    startOmbConsume();
×
1491
    return 0;
×
1492
  }
1493

1494
  int32_t retCode = getConsumeInfo();
69,663✔
1495
  if (0 != retCode) {
69,663✔
1496
    return -1;
×
1497
  }
1498

1499
  saveConfigToLogFile();
69,663✔
1500

1501
  tmqSetSignalHandle();
69,663✔
1502

1503
  TdThreadAttr thattr;
65,631✔
1504
  taosThreadAttrInit(&thattr);
69,663✔
1505
  taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE);
69,663✔
1506

1507
  // pthread_create one thread to consume
1508
  taosFprintfFile(g_fp, "==== create %d consume thread ====\n", g_stConfInfo.numOfThread);
69,663✔
1509
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; ++i) {
145,737✔
1510
    taosThreadCreate(&(g_stConfInfo.stThreads[i].thread), &thattr, consumeThreadFunc,
76,074✔
1511
                     (void*)(&(g_stConfInfo.stThreads[i])));
76,074✔
1512
  }
1513

1514
  int64_t start = taosGetTimestampUs();
69,663✔
1515

1516
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; i++) {
145,737✔
1517
    taosThreadJoin(g_stConfInfo.stThreads[i].thread, NULL);
76,074✔
1518
    taosThreadClear(&g_stConfInfo.stThreads[i].thread);
76,074✔
1519
  }
1520

1521
  int64_t end = taosGetTimestampUs();
69,663✔
1522

1523
  int64_t totalMsgs = 0;
69,663✔
1524
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; i++) {
145,737✔
1525
    totalMsgs += g_stConfInfo.stThreads[i].consumeMsgCnt;
76,074✔
1526
  }
1527

1528
  int64_t t = end - start;
69,663✔
1529
  if (0 == t) t = 1;
69,663✔
1530

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

1536
  taosFprintfFile(g_fp, "==== close tmqlog ====\n");
69,663✔
1537
  taosCloseFile(&g_fp);
69,663✔
1538

1539
  return 0;
69,663✔
1540
}
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