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

taosdata / TDengine / #4928

15 Jan 2026 04:09PM UTC coverage: 66.708% (+0.6%) from 66.12%
#4928

push

travis-ci

web-flow
merge: from main to 3.0 branch #34317

840 of 1255 new or added lines in 23 files covered. (66.93%)

826 existing lines in 119 files now uncovered.

203035 of 304362 relevant lines covered (66.71%)

130886757.17 hits per line

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

47.78
/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) {
14,333,874✔
167
  time_t    tTime = taosGetTimestampSec();
14,333,874✔
168
  struct tm tm;
14,221,515✔
169
  taosLocalTime(&tTime, &tm, NULL, 0, NULL);
14,333,564✔
170
  sprintf(timeString, "%d-%02d-%02d %02d:%02d:%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
14,333,238✔
171
          tm.tm_min, tm.tm_sec);
172

173
  return timeString;
14,333,238✔
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() { taosSetSignal(SIGINT, tmqStop); }
64,776✔
183

184
void initLogFile() {
64,776✔
185
  char filename[256];
61,190✔
186
  char tmpString[128];
61,190✔
187

188
  pid_t process_id = taosGetPId();
64,776✔
189

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

209
void saveConfigToLogFile() {
64,776✔
210
  taosFprintfFile(g_fp, "###################################################################\n");
64,776✔
211
  taosFprintfFile(g_fp, "# configDir:           %s\n", configDir);
64,776✔
212
  taosFprintfFile(g_fp, "# dbName:              %s\n", g_stConfInfo.dbName);
64,776✔
213
  taosFprintfFile(g_fp, "# cdbName:             %s\n", g_stConfInfo.cdbName);
64,776✔
214
  taosFprintfFile(g_fp, "# showMsgFlag:         %d\n", g_stConfInfo.showMsgFlag);
64,776✔
215
  taosFprintfFile(g_fp, "# showRowFlag:         %d\n", g_stConfInfo.showRowFlag);
64,776✔
216
  taosFprintfFile(g_fp, "# saveRowFlag:         %d\n", g_stConfInfo.saveRowFlag);
64,776✔
217
  taosFprintfFile(g_fp, "# consumeDelay:        %d\n", g_stConfInfo.consumeDelay);
64,776✔
218
  taosFprintfFile(g_fp, "# numOfThread:         %d\n", g_stConfInfo.numOfThread);
64,776✔
219

220
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; i++) {
135,341✔
221
    taosFprintfFile(g_fp, "# consumer %d info:\n", g_stConfInfo.stThreads[i].consumerId);
70,565✔
222
    // taosFprintfFile(g_fp, "  auto commit:              %s\n", g_stConfInfo.stThreads[i].autoCommit);
223
    // taosFprintfFile(g_fp, "  auto commit interval ms:  %d\n", g_stConfInfo.stThreads[i].autoCommitIntervalMs);
224
    // taosFprintfFile(g_fp, "  auto offset rest:         %s\n", g_stConfInfo.stThreads[i].autoOffsetRest);
225
    taosFprintfFile(g_fp, "  Topics: ");
70,565✔
226
    for (int j = 0; j < g_stConfInfo.stThreads[i].numOfTopic; j++) {
145,550✔
227
      taosFprintfFile(g_fp, "%s, ", g_stConfInfo.stThreads[i].topics[j]);
74,985✔
228
    }
229
    taosFprintfFile(g_fp, "\n");
70,565✔
230
    taosFprintfFile(g_fp, "  Key: ");
70,565✔
231
    for (int k = 0; k < g_stConfInfo.stThreads[i].numOfKey; k++) {
352,825✔
232
      taosFprintfFile(g_fp, "%s:%s, ", g_stConfInfo.stThreads[i].key[k], g_stConfInfo.stThreads[i].value[k]);
282,260✔
233
    }
234
    taosFprintfFile(g_fp, "\n");
70,565✔
235
    taosFprintfFile(g_fp, "  expect rows: %" PRId64 "\n", g_stConfInfo.stThreads[i].expectMsgCnt);
70,565✔
236
  }
237

238
  char tmpString[128];
61,190✔
239
  taosFprintfFile(g_fp, "# Test time:                %s\n", getCurrentTimeString(tmpString));
64,776✔
240
  taosFprintfFile(g_fp, "###################################################################\n");
64,776✔
241
}
64,776✔
242

243
void parseArgument(int32_t argc, char* argv[]) {
64,776✔
244
  memset(&g_stConfInfo, 0, sizeof(SConfInfo));
64,776✔
245
  g_stConfInfo.showMsgFlag = 0;
64,776✔
246
  g_stConfInfo.showRowFlag = 0;
64,776✔
247
  g_stConfInfo.saveRowFlag = 0;
64,776✔
248
  g_stConfInfo.consumeDelay = 5;
64,776✔
249
  g_stConfInfo.numOfThread = 1;
64,776✔
250
  g_stConfInfo.batchSize = 1;
64,776✔
251
  g_stConfInfo.producers = 0;
64,776✔
252

253
  g_stConfInfo.nowTime = taosGetTimestampMs();
64,776✔
254

255
  for (int32_t i = 1; i < argc; i++) {
501,771✔
256
    if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
436,995✔
257
      printHelp();
×
258
      exit(0);
×
259
    } else if (strcmp(argv[i], "-d") == 0) {
436,995✔
260
      tstrncpy(g_stConfInfo.dbName, argv[++i], sizeof(g_stConfInfo.dbName));
64,776✔
261
    } else if (strcmp(argv[i], "-w") == 0) {
372,219✔
262
      tstrncpy(g_stConfInfo.cdbName, argv[++i], sizeof(g_stConfInfo.cdbName));
64,776✔
263
    } else if (strcmp(argv[i], "-c") == 0) {
307,443✔
264
      tstrncpy(configDir, argv[++i], PATH_MAX);
64,776✔
265
    } else if (strcmp(argv[i], "-g") == 0) {
242,667✔
266
      g_stConfInfo.showMsgFlag = atol(argv[++i]);
64,776✔
267
    } else if (strcmp(argv[i], "-r") == 0) {
177,891✔
268
      g_stConfInfo.showRowFlag = atol(argv[++i]);
64,776✔
269
    } else if (strcmp(argv[i], "-s") == 0) {
113,115✔
270
      g_stConfInfo.saveRowFlag = atol(argv[++i]);
×
271
    } else if (strcmp(argv[i], "-y") == 0) {
113,115✔
272
      g_stConfInfo.consumeDelay = atol(argv[++i]);
64,776✔
273
    } else if (strcmp(argv[i], "-e") == 0) {
48,339✔
274
      g_stConfInfo.useSnapshot = atol(argv[++i]);
48,339✔
275
    } else if (strcmp(argv[i], "-t") == 0) {
×
276
      char tmpBuf[56] = {0};
×
277
      tstrncpy(tmpBuf, argv[++i], sizeof(tmpBuf));
×
278
      sprintf(g_stConfInfo.topic, "`%s`", tmpBuf);
×
279
    } else if (strcmp(argv[i], "-x") == 0) {
×
280
      g_stConfInfo.numOfThread = atol(argv[++i]);
×
281
    } else if (strcmp(argv[i], "-l") == 0) {
×
282
      g_stConfInfo.runDurationMinutes = atol(argv[++i]);
×
283
    } else if (strcmp(argv[i], "-p") == 0) {
×
284
      g_stConfInfo.producers = atol(argv[++i]);
×
285
    } else if (strcmp(argv[i], "-b") == 0) {
×
286
      g_stConfInfo.batchSize = atol(argv[++i]);
×
287
    } else if (strcmp(argv[i], "-i") == 0) {
×
288
      g_stConfInfo.producerRate = atol(argv[++i]);
×
289
    } else if (strcmp(argv[i], "-n") == 0) {
×
290
      g_stConfInfo.payloadLen = atol(argv[++i]);
×
291
      if (g_stConfInfo.payloadLen <= 0 || g_stConfInfo.payloadLen > 1024 * 1024 * 1024) {
×
292
        pError("%s calloc size is too large: %s %s", GREEN, argv[++i], NC);
×
293
        exit(-1);
×
294
      }
295
    } else {
296
      pError("%s unknow para: %s %s", GREEN, argv[++i], NC);
×
297
      exit(-1);
×
298
    }
299
  }
300

301
  g_payload = taosMemoryCalloc(g_stConfInfo.payloadLen + 1, 1);
64,776✔
302
  if (NULL == g_payload) {
64,776✔
303
    pPrint("%s failed to malloc for payload %s", GREEN, NC);
×
304
    exit(-1);
×
305
  }
306

307
  for (int32_t i = 0; i < g_stConfInfo.payloadLen; i++) {
64,776✔
308
    strcpy(&g_payload[i], "a");
×
309
  }
310

311
  initLogFile();
64,776✔
312

313
  taosFprintfFile(g_fp, "====parseArgument() success\n");
64,776✔
314

315
#if 1
316
  pPrint("%s configDir:%s %s", GREEN, configDir, NC);
64,776✔
317
  pPrint("%s dbName:%s %s", GREEN, g_stConfInfo.dbName, NC);
64,776✔
318
  pPrint("%s cdbName:%s %s", GREEN, g_stConfInfo.cdbName, NC);
64,776✔
319
  pPrint("%s consumeDelay:%d %s", GREEN, g_stConfInfo.consumeDelay, NC);
64,776✔
320
  pPrint("%s showMsgFlag:%d %s", GREEN, g_stConfInfo.showMsgFlag, NC);
64,776✔
321
  pPrint("%s showRowFlag:%d %s", GREEN, g_stConfInfo.showRowFlag, NC);
64,776✔
322
  pPrint("%s saveRowFlag:%d %s", GREEN, g_stConfInfo.saveRowFlag, NC);
64,776✔
323

324
  pPrint("%s snapshot:%d %s", GREEN, g_stConfInfo.useSnapshot, NC);
64,776✔
325

326
  pPrint("%s omb topic:%s %s", GREEN, g_stConfInfo.topic, NC);
64,776✔
327
  pPrint("%s numOfThread:%d %s", GREEN, g_stConfInfo.numOfThread, NC);
64,776✔
328
#endif
329
}
64,776✔
330

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

339
void ltrim(char* str) {
357,245✔
340
  if (str == NULL || *str == '\0') {
357,245✔
341
    return;
×
342
  }
343
  int   len = 0;
357,245✔
344
  char* p = str;
357,245✔
345
  while (*p != '\0' && isspace(*p)) {
2,968,353✔
346
    ++p;
2,611,108✔
347
    ++len;
2,611,108✔
348
  }
349
  memmove(str, p, strlen(str) - len + 1);
357,245✔
350
  // return str;
351
}
352

353
int queryDB(TAOS* taos, char* command) {
70,565✔
354
  int       retryCnt = 10;
70,565✔
355
  int       code = 0;
70,565✔
356
  TAOS_RES* pRes = NULL;
70,565✔
357

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

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

377
void addRowsToVgroupId(SThreadInfo* pInfo, int32_t vgroupId, int32_t rows) {
10,904,597✔
378
  int32_t i;
379
  for (i = 0; i < pInfo->numOfVgroups; i++) {
22,413,957✔
380
    if (vgroupId == pInfo->rowsOfPerVgroups[i][0]) {
22,252,214✔
381
      pInfo->rowsOfPerVgroups[i][1] += rows;
10,742,854✔
382
      return;
10,742,854✔
383
    }
384
  }
385

386
  pInfo->rowsOfPerVgroups[pInfo->numOfVgroups][0] = vgroupId;
161,743✔
387
  pInfo->rowsOfPerVgroups[pInfo->numOfVgroups][1] += rows;
161,743✔
388
  pInfo->numOfVgroups++;
161,743✔
389

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

399
TAOS* createNewTaosConnect() {
135,341✔
400
  TAOS*   taos = NULL;
135,341✔
401
  int32_t retryCnt = 10;
135,341✔
402

403
  while (retryCnt--) {
135,341✔
404
    taos = taos_connect(NULL, "root", "taosdata", NULL, 0);
135,341✔
405
    if (NULL != taos) {
135,341✔
406
      return taos;
135,341✔
407
    }
408
    taosSsleep(1);
×
409
  }
410

411
  taosFprintfFile(g_fp, "taos_connect() fail\n");
×
412
  return NULL;
×
413
}
414

415
int32_t saveConsumeContentToTbl(SThreadInfo* pInfo, char* buf) {
×
416
  char sqlStr[1100] = {0};
×
417

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

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

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

440
  taos_close(pConn);
×
441

442
  return 0;
×
443
}
444

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

451
  time_t  tt;
2,147,483,647✔
452
  int32_t ms = 0;
2,147,483,647✔
453
  if (precision == TSDB_TIME_PRECISION_NANO) {
2,147,483,647✔
454
    tt = (time_t)(val / 1000000000);
13,050,000✔
455
    ms = val % 1000000000;
13,050,000✔
456
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
2,147,483,647✔
457
    tt = (time_t)(val / 1000000);
13,500,000✔
458
    ms = val % 1000000;
13,500,000✔
459
  } else {
460
    tt = (time_t)(val / 1000);
2,147,483,647✔
461
    ms = val % 1000;
2,147,483,647✔
462
  }
463

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

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

481
  if (precision == TSDB_TIME_PRECISION_NANO) {
2,147,483,647✔
482
    sprintf(buf + pos, ".%09d", ms);
13,303,774✔
483
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
2,147,483,647✔
484
    sprintf(buf + pos, ".%06d", ms);
13,500,000✔
485
  } else {
486
    sprintf(buf + pos, ".%03d", ms);
2,147,483,647✔
487
  }
488

489
  return buf;
2,147,483,647✔
490
}
491

492
static void shellDumpFieldToFile(TdFilePtr pFile, const char* val, TAOS_FIELD* field, int32_t length,
2,147,483,647✔
493
                                 int32_t precision) {
494
  if (val == NULL) {
2,147,483,647✔
495
    taosFprintfFile(pFile, "NULL");
127,957,503✔
496
    return;
127,951,245✔
497
  }
498

499
  char quotationStr[2];
2,147,483,647✔
500
  quotationStr[0] = '\"';
2,147,483,647✔
501
  quotationStr[1] = 0;
2,147,483,647✔
502

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

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

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

582
static int32_t data_msg_process(TAOS_RES* msg, SThreadInfo* pInfo, int32_t msgIndex) {
10,904,597✔
583
  char    buf[1024];
10,821,049✔
584
  int32_t totalRows = 0;
10,904,597✔
585

586
  int32_t     vgroupId = tmq_get_vgroup_id(msg);
10,904,597✔
587
  const char* dbName = tmq_get_db_name(msg);
10,904,597✔
588

589
  char timestring[128] = {0};
10,904,597✔
590
  taosFprintfFile(g_fp, "%s consumerId: %d, msg index:%d\n", getCurrentTimeString(timestring), pInfo->consumerId, msgIndex);
10,904,597✔
591
  int32_t index = 0;
10,904,597✔
592
  for (index = 0; index < pInfo->numOfVgroups; index++) {
22,413,957✔
593
    if (vgroupId == pInfo->rowsOfPerVgroups[index][0]) {
22,252,214✔
594
      break;
10,742,854✔
595
    }
596
  }
597

598
  taosFprintfFile(g_fp, "dbName: %s, topic: %s, vgroupId:%d, currentRows:%d\n", dbName != NULL ? dbName : "invalid table",
10,904,597✔
599
                  tmq_get_topic_name(msg), vgroupId, pInfo->rowsOfPerVgroups[index][1]);
10,821,049✔
600

601
  while (1) {
2,147,483,647✔
602
    TAOS_ROW row = taos_fetch_row(msg);
2,147,483,647✔
603
    if (row == NULL) {
2,147,483,647✔
604
      break;
10,904,597✔
605
    }
606

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

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

622
    dumpToFileForCheck(pInfo->pConsumeRowsFile, row, fields, length, numOfFields, precision);
2,147,483,647✔
623
    taos_print_row(buf, row, fields, numOfFields);
2,147,483,647✔
624

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

633
    totalRows++;
2,147,483,647✔
634
  }
635

636
  addRowsToVgroupId(pInfo, vgroupId, totalRows);
10,904,597✔
637
  return totalRows;
10,904,597✔
638
}
639

640
static int32_t meta_msg_process(TAOS_RES* msg, SThreadInfo* pInfo, int32_t msgIndex) {
×
UNCOV
641
  char    buf[1024];
×
642
  int32_t totalRows = 0;
×
643

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

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

652
  {
653
    tmq_raw_data raw = {0};
×
654
    int32_t      code = tmq_get_raw(msg, &raw);
×
655

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

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

676
  totalRows++;
×
677

678
  return totalRows;
×
679
}
680

681
static void appNothing(void* param, TAOS_RES* res, int32_t numOfRows) {}
120,567✔
682

683
int32_t notifyMainScript(SThreadInfo* pInfo, int32_t cmdId) {
120,567✔
684
  char sqlStr[1024] = {0};
120,567✔
685

686
  // schema: ts timestamp, consumerid int, consummsgcnt bigint, checkresult int
687
  sprintf(sqlStr, "insert into %s.notifyinfo values (%" PRId64 ", %d, %d)", g_stConfInfo.cdbName,
120,567✔
688
          atomic_fetch_add_64(&g_stConfInfo.nowTime, 1), cmdId, pInfo->consumerId);
689

690
  taos_query_a(pInfo->taos, sqlStr, appNothing, NULL);
120,567✔
691

692
  taosFprintfFile(g_fp, "notifyMainScript success, sql: %s\n", sqlStr);
120,567✔
693

694
  return 0;
120,567✔
695
}
696

697
static int32_t g_once_commit_flag = 0;
698

699
static void tmq_commit_cb_print(tmq_t* tmq, int32_t code, void* param) {
3,088,030✔
700
  taosFprintfFile(g_fp, "tmq_commit_cb_print() commit %d\n", code);
3,088,030✔
701

702
  if (0 == g_once_commit_flag && code == 0) {
3,088,030✔
703
    g_once_commit_flag = 1;
55,001✔
704
    notifyMainScript((SThreadInfo*)param, (int32_t)NOTIFY_CMD_START_COMMIT);
55,001✔
705
  }
706

707
  char tmpString[128];
3,081,470✔
708
  taosFprintfFile(g_fp, "%s tmq_commit_cb_print() be called\n", getCurrentTimeString(tmpString));
3,088,030✔
709
}
3,088,030✔
710

711
void build_consumer(SThreadInfo* pInfo) {
70,565✔
712
  tmq_conf_t* conf = tmq_conf_new();
70,565✔
713

714
  // tmq_conf_set(conf, "td.connect.ip", "localhost");
715
  // tmq_conf_set(conf, "td.connect.port", "6030");
716
  tmq_conf_set(conf, "td.connect.user", "root");
70,565✔
717
  tmq_conf_set(conf, "td.connect.pass", "taosdata");
70,565✔
718

719
  // tmq_conf_set(conf, "td.connect.db", g_stConfInfo.dbName);
720

721
  tmq_conf_set_auto_commit_cb(conf, tmq_commit_cb_print, pInfo);
70,565✔
722

723
  // tmq_conf_set(conf, "group.id", "cgrp1");
724
  for (int32_t i = 0; i < pInfo->numOfKey; i++) {
352,780✔
725
    tmq_conf_set(conf, pInfo->key[i], pInfo->value[i]);
282,215✔
726
  }
727

728
  tmq_conf_set(conf, "msg.with.table.name", "true");
70,565✔
729

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

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

735
  // tmq_conf_set(conf, "auto.commit.interval.ms", "1000");
736

737
  // tmq_conf_set(conf, "auto.offset.reset", "none");
738
  // tmq_conf_set(conf, "auto.offset.reset", "earliest");
739
  // tmq_conf_set(conf, "auto.offset.reset", "latest");
740
  //
741
  if (g_stConfInfo.useSnapshot) {
70,565✔
742
    tmq_conf_set(conf, "experimental.snapshot.enable", "true");
23,290✔
743
  }
744

745
  pInfo->tmq = tmq_consumer_new(conf, NULL, 0);
70,565✔
746
  tmq_conf_destroy(conf);
70,565✔
747
  return;
70,565✔
748
}
749

750
void build_topic_list(SThreadInfo* pInfo) {
70,565✔
751
  pInfo->topicList = tmq_list_new();
70,565✔
752
  // tmq_list_append(topic_list, "test_stb_topic_1");
753
  for (int32_t i = 0; i < pInfo->numOfTopic; i++) {
145,550✔
754
    tmq_list_append(pInfo->topicList, pInfo->topics[i]);
74,985✔
755
  }
756
  return;
70,565✔
757
}
758

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

766
  char tmpString[128];
65,642✔
767
  taosFprintfFile(g_fp, "%s, consume id %d result: %s\n", getCurrentTimeString(tmpString), pInfo->consumerId, sqlStr);
70,565✔
768

769
  int retCode = queryDB(pInfo->taos, sqlStr);
70,565✔
770
  if (retCode != 0) {
70,565✔
771
    taosFprintfFile(g_fp, "consume id %d error in save consume result\n", pInfo->consumerId);
×
772
    return -1;
×
773
  }
774

775
  return 0;
70,565✔
776
}
777

778
void loop_consume(SThreadInfo* pInfo) {
70,565✔
779
  int32_t code;
780

781
  int32_t once_flag = 0;
70,565✔
782

783
  int64_t totalMsgs = 0;
70,565✔
784
  int64_t totalRows = 0;
70,565✔
785

786
  char tmpString[128];
65,642✔
787
  taosFprintfFile(g_fp, "%s consumer id %d start to loop pull msg\n", getCurrentTimeString(tmpString),
70,565✔
788
                  pInfo->consumerId);
789

790
  pInfo->ts = taosGetTimestampMs();
70,565✔
791

792
  if (pInfo->ifCheckData) {
70,565✔
793
    char filename[256] = {0};
43,007✔
794
    memset(tmpString, 0, tListLen(tmpString));
43,007✔
795

796
    // sprintf(filename, "%s/../log/consumerid_%d_%s.txt", configDir, pInfo->consumerId,
797
    // getCurrentTimeString(tmpString));
798
    sprintf(filename, "%s/../log/consumerid_%d.txt", configDir, pInfo->consumerId);
43,007✔
799
    pInfo->pConsumeRowsFile = taosOpenFile(filename, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM);
43,007✔
800

801
    sprintf(filename, "%s/../log/meta_consumerid_%d.txt", configDir, pInfo->consumerId);
43,007✔
802
    pInfo->pConsumeMetaFile = taosOpenFile(filename, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM);
43,007✔
803

804
    if (pInfo->pConsumeRowsFile == NULL || pInfo->pConsumeMetaFile == NULL) {
43,007✔
805
      taosFprintfFile(g_fp, "%s create file fail for save rows or save meta\n", getCurrentTimeString(tmpString));
×
806
      return;
×
807
    }
808
  }
809

810
  int64_t  lastTotalMsgs = 0;
70,565✔
811
  uint64_t lastPrintTime = taosGetTimestampMs();
70,565✔
812
  uint64_t startTs = taosGetTimestampMs();
70,565✔
813

814
  int32_t consumeDelay = g_stConfInfo.consumeDelay == -1 ? -1 : (g_stConfInfo.consumeDelay * 1000);
70,565✔
815
  while (running) {
10,933,358✔
816
    TAOS_RES* tmqMsg = tmq_consumer_poll(pInfo->tmq, consumeDelay);
10,933,358✔
817
    if (tmqMsg) {
10,937,270✔
818
      if (0 != g_stConfInfo.showMsgFlag) {
10,904,597✔
819
        tmq_res_t msgType = tmq_get_res_type(tmqMsg);
10,904,597✔
820
        if (msgType == TMQ_RES_TABLE_META) {
10,904,597✔
821
          totalRows += meta_msg_process(tmqMsg, pInfo, totalMsgs);
×
822
        } else if (msgType == TMQ_RES_DATA) {
10,904,597✔
823
          totalRows += data_msg_process(tmqMsg, pInfo, totalMsgs);
10,904,597✔
824
        } else if (msgType == TMQ_RES_METADATA) {
×
825
          meta_msg_process(tmqMsg, pInfo, totalMsgs);
×
826
          totalRows += data_msg_process(tmqMsg, pInfo, totalMsgs);
×
827
        }
828
      }
829

830
      taos_free_result(tmqMsg);
10,904,597✔
831
      totalMsgs++;
10,901,337✔
832

833
      int64_t currentPrintTime = taosGetTimestampMs();
10,902,967✔
834
      if (currentPrintTime - lastPrintTime > 10 * 1000) {
10,902,967✔
835
        taosFprintfFile(
43,062✔
836
            g_fp, "consumer id %d has currently poll total msgs: %" PRId64 ", period rate: %.3f msgs/second\n",
837
            pInfo->consumerId, totalMsgs, (totalMsgs - lastTotalMsgs) * 1000.0 / (currentPrintTime - lastPrintTime));
43,062✔
838
        lastPrintTime = currentPrintTime;
43,062✔
839
        lastTotalMsgs = totalMsgs;
43,062✔
840
      }
841

842
      if (0 == once_flag) {
10,902,967✔
843
        once_flag = 1;
65,566✔
844
        notifyMainScript(pInfo, NOTIFY_CMD_START_CONSUM);
65,566✔
845
      }
846

847
      if ((totalRows >= pInfo->expectMsgCnt) || (totalMsgs >= pInfo->expectMsgCnt)) {
10,902,967✔
848
        memset(tmpString, 0, tListLen(tmpString));
39,848✔
849
        taosFprintfFile(g_fp, "%s over than expect rows, so break consume\n", getCurrentTimeString(tmpString));
39,848✔
850
        break;
37,892✔
851
      }
852
    } else {
853
      memset(tmpString, 0, tListLen(tmpString));
32,673✔
854
      taosFprintfFile(g_fp, "%s no poll more msg when time over, break consume\n", getCurrentTimeString(tmpString));
32,673✔
855
      break;
32,673✔
856
    }
857
  }
858

859
  if (0 == running) {
70,565✔
860
    taosFprintfFile(g_fp, "receive stop signal and not continue consume\n");
×
861
  }
862

863
  pInfo->consumeMsgCnt = totalMsgs;
70,565✔
864
  pInfo->consumeRowCnt = totalRows;
70,565✔
865

866
  taosFprintfFile(g_fp, "==== consumerId: %d, consumeMsgCnt: %" PRId64 ", consumeRowCnt: %" PRId64 "\n",
70,565✔
867
                  pInfo->consumerId, pInfo->consumeMsgCnt, pInfo->consumeRowCnt);
868

869
  if(taosFsyncFile(pInfo->pConsumeRowsFile) < 0){
70,565✔
870
    printf("taosFsyncFile error:%s", strerror(errno));
×
871
  }
872
  taosCloseFile(&pInfo->pConsumeRowsFile);
70,565✔
873
}
874

875
void* consumeThreadFunc(void* param) {
70,565✔
876
  SThreadInfo* pInfo = (SThreadInfo*)param;
70,565✔
877

878
  pInfo->taos = createNewTaosConnect();
70,565✔
879
  if (pInfo->taos == NULL) {
70,565✔
880
    taosFprintfFile(g_fp, "taos_connect() fail, can not notify and save consume result to main scripte\n");
×
881
    return NULL;
×
882
  }
883

884
  build_consumer(pInfo);
70,565✔
885
  build_topic_list(pInfo);
70,565✔
886
  if ((NULL == pInfo->tmq) || (NULL == pInfo->topicList)) {
70,565✔
887
    taosFprintfFile(g_fp, "create consumer fail! tmq is null or topicList is null\n");
×
888
    taos_close(pInfo->taos);
×
889
    pInfo->taos = NULL;
×
890
    return NULL;
×
891
  }
892

893
  int32_t err = tmq_subscribe(pInfo->tmq, pInfo->topicList);
70,565✔
894
  if (err != 0) {
70,565✔
895
    pError("tmq_subscribe() fail, reason: %s\n", tmq_err2str(err));
×
896
    taosFprintfFile(g_fp, "tmq_subscribe() fail! reason: %s\n", tmq_err2str(err));
×
897
    taos_close(pInfo->taos);
×
898
    pInfo->taos = NULL;
×
899
    return NULL;
×
900
  }
901

902
  tmq_list_destroy(pInfo->topicList);
70,565✔
903
  pInfo->topicList = NULL;
70,565✔
904

905
  loop_consume(pInfo);
70,565✔
906

907
  if (pInfo->ifManualCommit) {
70,565✔
908
    pPrint("tmq_commit() manual commit when consume end.\n");
57,449✔
909
    /*tmq_commit(pInfo->tmq, NULL, 0);*/
910
    tmq_commit_sync(pInfo->tmq, NULL);
57,449✔
911
    tmq_commit_cb_print(pInfo->tmq, 0, pInfo);
57,449✔
912
    taosFprintfFile(g_fp, "tmq_commit() manual commit over.\n");
57,449✔
913
    pPrint("tmq_commit() manual commit over.\n");
57,449✔
914
  }
915

916
  err = tmq_unsubscribe(pInfo->tmq);
70,565✔
917
  if (err != 0) {
70,565✔
918
    pError("tmq_unsubscribe() fail, reason: %s\n", tmq_err2str(err));
×
919
    taosFprintfFile(g_fp, "tmq_unsubscribe()! reason: %s\n", tmq_err2str(err));
×
920
  }
921

922
  err = tmq_consumer_close(pInfo->tmq);
70,565✔
923
  if (err != 0) {
70,565✔
924
    pError("tmq_consumer_close() fail, reason: %s\n", tmq_err2str(err));
×
925
    taosFprintfFile(g_fp, "tmq_consumer_close()! reason: %s\n", tmq_err2str(err));
×
926
  }
927
  pInfo->tmq = NULL;
70,565✔
928

929
  // save consume result into consumeresult table
930
  saveConsumeResult(pInfo);
70,565✔
931

932
  // save rows from per vgroup
933
  taosFprintfFile(g_fp, "======== consumerId: %d, consume rows from per vgroups ========\n", pInfo->consumerId);
70,565✔
934
  for (int32_t i = 0; i < pInfo->numOfVgroups; i++) {
232,308✔
935
    taosFprintfFile(g_fp, "vgroups: %04d, rows: %d\n", pInfo->rowsOfPerVgroups[i][0], pInfo->rowsOfPerVgroups[i][1]);
161,743✔
936
  }
937

938
  taos_close(pInfo->taos);
70,565✔
939
  pInfo->taos = NULL;
70,565✔
940

941
  return NULL;
70,565✔
942
}
943

944
void parseConsumeInfo() {
64,776✔
945
  char*      token;
946
  const char delim[2] = ",";
64,776✔
947
  const char ch = ':';
64,776✔
948

949
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; i++) {
135,341✔
950
    token = strtok(g_stConfInfo.stThreads[i].topicString, delim);
70,565✔
951
    while (token != NULL) {
145,550✔
952
      // printf("%s\n", token );
953
      tstrncpy(g_stConfInfo.stThreads[i].topics[g_stConfInfo.stThreads[i].numOfTopic], token,
74,985✔
954
               sizeof(g_stConfInfo.stThreads[i].topics[g_stConfInfo.stThreads[i].numOfTopic]));
955
      ltrim(g_stConfInfo.stThreads[i].topics[g_stConfInfo.stThreads[i].numOfTopic]);
74,985✔
956
      // printf("%s\n", g_stConfInfo.topics[g_stConfInfo.numOfTopic]);
957
      g_stConfInfo.stThreads[i].numOfTopic++;
74,985✔
958

959
      token = strtok(NULL, delim);
74,985✔
960
    }
961

962
    token = strtok(g_stConfInfo.stThreads[i].keyString, delim);
70,565✔
963
    while (token != NULL) {
352,825✔
964
      // printf("%s\n", token );
965
      {
966
        char* pstr = token;
282,260✔
967
        ltrim(pstr);
282,260✔
968
        char* ret = strchr(pstr, ch);
282,260✔
969
        memcpy(g_stConfInfo.stThreads[i].key[g_stConfInfo.stThreads[i].numOfKey], pstr, ret - pstr);
282,260✔
970
        tstrncpy(g_stConfInfo.stThreads[i].value[g_stConfInfo.stThreads[i].numOfKey], ret + 1,
282,260✔
971
                 sizeof(g_stConfInfo.stThreads[i].value[g_stConfInfo.stThreads[i].numOfKey]));
972
        // printf("key: %s, value: %s\n", g_stConfInfo.key[g_stConfInfo.numOfKey],
973
        // g_stConfInfo.value[g_stConfInfo.numOfKey]);
974
        g_stConfInfo.stThreads[i].numOfKey++;
282,260✔
975
      }
976

977
      token = strtok(NULL, delim);
282,260✔
978
    }
979
  }
980
}
64,776✔
981

982
int32_t getConsumeInfo() {
64,776✔
983
  char sqlStr[1024] = {0};
64,776✔
984

985
  TAOS* pConn = createNewTaosConnect();
64,776✔
986
  if (pConn == NULL) {
64,776✔
987
    taosFprintfFile(g_fp, "taos_connect() fail, can not get consume info for start consumer\n");
×
988
    return -1;
×
989
  }
990

991
  sprintf(sqlStr, "select * from %s.consumeinfo", g_stConfInfo.cdbName);
64,776✔
992
  TAOS_RES* pRes = taos_query(pConn, sqlStr);
64,776✔
993
  if (taos_errno(pRes) != 0) {
64,776✔
994
    taosFprintfFile(g_fp, "error in get consumeinfo for %s\n", taos_errstr(pRes));
×
995
    taosCloseFile(&g_fp);
×
996
    taos_free_result(pRes);
×
997
    taos_close(pConn);
×
998
    return -1;
×
999
  }
1000

1001
  TAOS_ROW    row = NULL;
64,776✔
1002
  int         num_fields = taos_num_fields(pRes);
64,776✔
1003
  TAOS_FIELD* fields = taos_fetch_fields(pRes);
64,776✔
1004

1005
  // schema: ts timestamp, consumerid int, topiclist binary(1024), keylist binary(1024), expectmsgcnt bigint,
1006
  // ifcheckdata int
1007

1008
  int32_t numOfThread = 0;
64,776✔
1009
  while ((row = taos_fetch_row(pRes))) {
135,341✔
1010
    int32_t* lengths = taos_fetch_lengths(pRes);
70,565✔
1011

1012
    // set default value
1013
    // g_stConfInfo.stThreads[numOfThread].autoCommitIntervalMs = 5000;
1014
    // memcpy(g_stConfInfo.stThreads[numOfThread].autoCommit, "true", strlen("true"));
1015
    // memcpy(g_stConfInfo.stThreads[numOfThread].autoOffsetRest, "earlieast", strlen("earlieast"));
1016

1017
    for (int i = 0; i < num_fields; ++i) {
563,514✔
1018
      if (row[i] == NULL || 0 == i) {
492,949✔
1019
        continue;
70,565✔
1020
      }
1021

1022
      if ((1 == i) && (fields[i].type == TSDB_DATA_TYPE_INT)) {
422,384✔
1023
        g_stConfInfo.stThreads[numOfThread].consumerId = *((int32_t*)row[i]);
70,565✔
1024
      } else if ((2 == i) && (fields[i].type == TSDB_DATA_TYPE_BINARY)) {
351,819✔
1025
        memcpy(g_stConfInfo.stThreads[numOfThread].topicString, row[i], lengths[i]);
70,565✔
1026
      } else if ((3 == i) && (fields[i].type == TSDB_DATA_TYPE_BINARY)) {
281,254✔
1027
        memcpy(g_stConfInfo.stThreads[numOfThread].keyString, row[i], lengths[i]);
70,565✔
1028
      } else if ((4 == i) && (fields[i].type == TSDB_DATA_TYPE_BIGINT)) {
210,689✔
1029
        g_stConfInfo.stThreads[numOfThread].expectMsgCnt = *((int64_t*)row[i]);
70,565✔
1030
      } else if ((5 == i) && (fields[i].type == TSDB_DATA_TYPE_INT)) {
140,124✔
1031
        g_stConfInfo.stThreads[numOfThread].ifCheckData = *((int32_t*)row[i]);
70,565✔
1032
      } else if ((6 == i) && (fields[i].type == TSDB_DATA_TYPE_INT)) {
69,559✔
1033
        g_stConfInfo.stThreads[numOfThread].ifManualCommit = *((int32_t*)row[i]);
69,559✔
1034
      }
1035
    }
1036
    numOfThread++;
70,565✔
1037
  }
1038
  g_stConfInfo.numOfThread = numOfThread;
64,776✔
1039

1040
  taos_free_result(pRes);
64,776✔
1041

1042
  parseConsumeInfo();
64,776✔
1043
  taos_close(pConn);
64,776✔
1044

1045
  return 0;
64,776✔
1046
}
1047

1048
static int32_t omb_data_msg_process(TAOS_RES* msg, SThreadInfo* pInfo, int32_t msgIndex, int64_t* lenOfRows) {
×
1049
  char    buf[16 * 1024];
×
1050
  int32_t totalRows = 0;
×
1051
  int32_t totalLen = 0;
×
1052

1053
  // printf("topic: %s\n", tmq_get_topic_name(msg));
1054
  // int32_t     vgroupId = tmq_get_vgroup_id(msg);
1055
  // const char* dbName = tmq_get_db_name(msg);
1056

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

1061
  while (1) {
×
1062
    TAOS_ROW row = taos_fetch_row(msg);
×
1063

1064
    if (row == NULL) break;
×
1065

1066
    TAOS_FIELD* fields = taos_fetch_fields(msg);
×
1067
    int32_t     numOfFields = taos_field_count(msg);
×
1068
    // int32_t*    length = taos_fetch_lengths(msg);
1069
    // int32_t     precision = taos_result_precision(msg);
1070
    // const char* tbName = tmq_get_table_name(msg);
1071

1072
    taos_print_row(buf, row, fields, numOfFields);
×
1073
    totalLen += strlen(buf);
×
1074
    totalRows++;
×
1075
  }
1076

1077
  *lenOfRows = totalLen;
×
1078
  return totalRows;
×
1079
}
1080

1081
void omb_loop_consume(SThreadInfo* pInfo) {
×
1082
  int32_t code;
1083

1084
  int32_t once_flag = 0;
×
1085

1086
  int64_t totalMsgs = 0;
×
1087
  int64_t totalRows = 0;
×
1088

1089
  char tmpString[128];
×
1090
  taosFprintfFile(g_fp, "%s consumer id %d start to loop pull msg\n", getCurrentTimeString(tmpString),
×
1091
                  pInfo->consumerId);
1092
  printf("%s consumer id %d start to loop pull msg\n", getCurrentTimeString(tmpString), pInfo->consumerId);
×
1093

1094
  pInfo->ts = taosGetTimestampMs();
×
1095

1096
  int64_t  lastTotalMsgs = 0;
×
1097
  uint64_t lastPrintTime = taosGetTimestampMs();
×
1098
  uint64_t startTs = taosGetTimestampMs();
×
1099

1100
  int64_t totalLenOfMsg = 0;
×
1101
  int64_t lastTotalLenOfMsg = 0;
×
1102
  int32_t consumeDelay = g_stConfInfo.consumeDelay == -1 ? -1 : (g_stConfInfo.consumeDelay * 1000);
×
1103
  while (running) {
×
1104
    TAOS_RES* tmqMsg = tmq_consumer_poll(pInfo->tmq, consumeDelay);
×
1105
    if (tmqMsg) {
×
1106
      int64_t lenOfMsg = 0;
×
1107
      totalRows += omb_data_msg_process(tmqMsg, pInfo, totalMsgs, &lenOfMsg);
×
1108
      totalLenOfMsg += lenOfMsg;
×
1109
      taos_free_result(tmqMsg);
×
1110
      totalMsgs++;
×
1111
      int64_t currentPrintTime = taosGetTimestampMs();
×
1112
      if (currentPrintTime - lastPrintTime > 10 * 1000) {
×
1113
        int64_t currentLenOfMsg = totalLenOfMsg - lastTotalLenOfMsg;
×
1114
        int64_t deltaTime = currentPrintTime - lastPrintTime;
×
1115
        printf("consumer id %d has currently cons total rows: %" PRId64 ", msgs: %" PRId64
×
1116
               ", rate: %.3f msgs/s, %.1f MB/s\n",
1117
               pInfo->consumerId, totalRows, totalMsgs, (totalMsgs - lastTotalMsgs) * 1000.0 / deltaTime,
×
1118
               currentLenOfMsg * 1000.0 / (1024 * 1024) / deltaTime);
×
1119

1120
        taosFprintfFile(g_fp,
×
1121
                        "consumer id %d has currently poll total msgs: %" PRId64
1122
                        ", period cons rate: %.3f msgs/s, %.1f MB/s\n",
1123
                        pInfo->consumerId, totalMsgs, (totalMsgs - lastTotalMsgs) * 1000.0 / deltaTime,
×
1124
                        currentLenOfMsg * 1000.0 / deltaTime);
×
1125
        lastPrintTime = currentPrintTime;
×
1126
        lastTotalMsgs = totalMsgs;
×
1127
        lastTotalLenOfMsg = totalLenOfMsg;
×
1128
      }
1129
    } else {
1130
      memset(tmpString, 0, tListLen(tmpString));
×
1131
      taosFprintfFile(g_fp, "%s no poll more msg when time over, break consume\n", getCurrentTimeString(tmpString));
×
1132
      printf("%s no poll more msg when time over, break consume\n", getCurrentTimeString(tmpString));
×
1133
      int64_t currentPrintTime = taosGetTimestampMs();
×
1134
      int64_t currentLenOfMsg = totalLenOfMsg - lastTotalLenOfMsg;
×
1135
      int64_t deltaTime = currentPrintTime - lastPrintTime;
×
1136
      printf("consumer id %d has currently cons total rows: %" PRId64 ", msgs: %" PRId64
×
1137
             ", rate: %.3f msgs/s, %.1f MB/s\n",
1138
             pInfo->consumerId, totalRows, totalMsgs, (totalMsgs - lastTotalMsgs) * 1000.0 / deltaTime,
×
1139
             currentLenOfMsg * 1000.0 / (1024 * 1024) / deltaTime);
×
1140
      break;
×
1141
    }
1142
  }
1143

1144
  pInfo->consumeMsgCnt = totalMsgs;
×
1145
  pInfo->consumeRowCnt = totalRows;
×
1146
  pInfo->consumeLen = totalLenOfMsg;
×
1147
}
×
1148

1149
void* ombConsumeThreadFunc(void* param) {
×
1150
  SThreadInfo* pInfo = (SThreadInfo*)param;
×
1151

1152
  //################### set key ########################
1153
  tmq_conf_t* conf = tmq_conf_new();
×
1154
  // tmq_conf_set(conf, "td.connect.ip", "localhost");
1155
  // tmq_conf_set(conf, "td.connect.port", "6030");
1156
  tmq_conf_set(conf, "td.connect.user", "root");
×
1157
  tmq_conf_set(conf, "td.connect.pass", "taosdata");
×
1158
  // tmq_conf_set(conf, "td.connect.db", g_stConfInfo.dbName);
1159
  tmq_conf_set_auto_commit_cb(conf, tmq_commit_cb_print, pInfo);
×
1160
  tmq_conf_set(conf, "group.id", "ombCgrp");
×
1161
  // tmq_conf_set(conf, "msg.with.table.name", "true");
1162
  // tmq_conf_set(conf, "client.id", "c-001");
1163
  // tmq_conf_set(conf, "enable.auto.commit", "true");
1164
  tmq_conf_set(conf, "enable.auto.commit", "false");
×
1165
  // tmq_conf_set(conf, "auto.commit.interval.ms", "1000");
1166
  // tmq_conf_set(conf, "auto.offset.reset", "none");
1167
  // tmq_conf_set(conf, "auto.offset.reset", "earliest");
1168
  tmq_conf_set(conf, "auto.offset.reset", "earliest");
×
1169
  //
1170
  if (g_stConfInfo.useSnapshot) {
×
1171
    tmq_conf_set(conf, "experimental.snapshot.enable", "true");
×
1172
  }
1173

1174
  pInfo->tmq = tmq_consumer_new(conf, NULL, 0);
×
1175

1176
  tmq_conf_destroy(conf);
×
1177

1178
  //################### set topic ##########################
1179
  pInfo->topicList = tmq_list_new();
×
1180
  tmq_list_append(pInfo->topicList, g_stConfInfo.topic);
×
1181

1182
  if ((NULL == pInfo->tmq) || (NULL == pInfo->topicList)) {
×
1183
    taosFprintfFile(g_fp, "create consumer fail! tmq is null or topicList is null\n");
×
1184
    return NULL;
×
1185
  }
1186

1187
  int32_t err = tmq_subscribe(pInfo->tmq, pInfo->topicList);
×
1188
  if (err != 0) {
×
1189
    pError("tmq_subscribe() fail, reason: %s\n", tmq_err2str(err));
×
1190
    taosFprintfFile(g_fp, "tmq_subscribe() fail! reason: %s\n", tmq_err2str(err));
×
1191
    return NULL;
×
1192
  }
1193

1194
  tmq_list_destroy(pInfo->topicList);
×
1195
  pInfo->topicList = NULL;
×
1196

1197
  omb_loop_consume(pInfo);
×
1198

1199
  err = tmq_unsubscribe(pInfo->tmq);
×
1200
  if (err != 0) {
×
1201
    pError("tmq_unsubscribe() fail, reason: %s\n", tmq_err2str(err));
×
1202
    taosFprintfFile(g_fp, "tmq_unsubscribe()! reason: %s\n", tmq_err2str(err));
×
1203
  }
1204

1205
  err = tmq_consumer_close(pInfo->tmq);
×
1206
  if (err != 0) {
×
1207
    pError("tmq_consumer_close() fail, reason: %s\n", tmq_err2str(err));
×
1208
    taosFprintfFile(g_fp, "tmq_consumer_close()! reason: %s\n", tmq_err2str(err));
×
1209
  }
1210
  pInfo->tmq = NULL;
×
1211

1212
  return NULL;
×
1213
}
1214

1215
static int queryDbExec(TAOS* taos, char* command, QUERY_TYPE type) {
×
1216
  TAOS_RES* res = taos_query(taos, command);
×
1217
  int32_t   code = taos_errno(res);
×
1218

1219
  if (code != 0) {
×
1220
    pPrint("%s Failed to execute <%s>, reason: %s %s", GREEN, command, taos_errstr(res), NC);
×
1221
    taos_free_result(res);
×
1222
    return -1;
×
1223
  }
1224

1225
  if (INSERT_TYPE == type) {
×
1226
    int affectedRows = taos_affected_rows(res);
×
1227
    taos_free_result(res);
×
1228
    return affectedRows;
×
1229
  }
1230

1231
  taos_free_result(res);
×
1232
  return 0;
×
1233
}
1234

1235
void* ombProduceThreadFunc(void* param) {
×
1236
  SThreadInfo* pInfo = (SThreadInfo*)param;
×
1237

1238
  pInfo->taos = createNewTaosConnect();
×
1239
  if (pInfo->taos == NULL) {
×
1240
    taosFprintfFile(g_fp, "taos_connect() fail, can not start producers!\n");
×
1241
    return NULL;
×
1242
  }
1243

1244
  int64_t affectedRowsTotal = 0;
×
1245
  int64_t sendMsgs = 0;
×
1246

1247
  uint32_t totalSendLoopTimes =
×
1248
      g_stConfInfo.runDurationMinutes * 60 * 1000 / SEND_TIME_UNIT;  // send some msgs per 10ms
×
1249
  uint32_t batchPerTblTimes = pInfo->producerRate / 100 / g_stConfInfo.batchSize;
×
1250
  uint32_t remainder = (pInfo->producerRate / 100) % g_stConfInfo.batchSize;
×
1251
  if (remainder) {
×
1252
    batchPerTblTimes += 1;
×
1253
  }
1254

1255
  char* sqlBuf = taosMemoryMalloc(MAX_SQL_LEN);
×
1256
  if (NULL == sqlBuf) {
×
1257
    printf("malloc fail for sqlBuf\n");
×
1258
    taos_close(pInfo->taos);
×
1259
    pInfo->taos = NULL;
×
1260
    return NULL;
×
1261
  }
1262

1263
  printf("Produce Info: totalSendLoopTimes: %d, batchPerTblTimes: %d, producerRate: %d\n", totalSendLoopTimes,
×
1264
         batchPerTblTimes, pInfo->producerRate);
1265

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

1269
  int64_t lastPrintTime = taosGetTimestampUs();
×
1270
  int64_t totalMsgLen = 0;
×
1271
  // int64_t timeStamp = taosGetTimestampUs();
1272
  while (totalSendLoopTimes) {
×
1273
    int64_t startTs = taosGetTimestampUs();
×
1274
    for (int i = 0; i < batchPerTblTimes; ++i) {
×
1275
      uint32_t msgsOfSql = g_stConfInfo.batchSize;
×
1276
      if ((i == batchPerTblTimes - 1) && (0 != remainder)) {
×
1277
        msgsOfSql = remainder;
×
1278
      }
1279
      int len = 0;
×
1280
      len += tsnprintf(sqlBuf + len, MAX_SQL_LEN - len, "insert into %s values ", ctbName);
×
1281
      for (int j = 0; j < msgsOfSql; j++) {
×
1282
        int64_t timeStamp = taosGetTimestampNs();
×
1283
        len += tsnprintf(sqlBuf + len, MAX_SQL_LEN - len, "(%" PRId64 ", \"%s\")", timeStamp, g_payload);
×
1284
        sendMsgs++;
×
1285
        pInfo->totalProduceMsgs++;
×
1286
      }
1287

1288
      totalMsgLen += len;
×
1289
      pInfo->totalMsgsLen += len;
×
1290

1291
      int64_t affectedRows = queryDbExec(pInfo->taos, sqlBuf, INSERT_TYPE);
×
1292
      if (affectedRows < 0) {
×
1293
        taos_close(pInfo->taos);
×
1294
        pInfo->taos = NULL;
×
1295
        taosMemoryFree(sqlBuf);
×
1296
        return NULL;
×
1297
      }
1298

1299
      affectedRowsTotal += affectedRows;
×
1300

1301
      // printf("Produce Info: affectedRows: %" PRId64 "\n", affectedRows);
1302
    }
1303
    totalSendLoopTimes -= 1;
×
1304

1305
    // calc spent time
1306
    int64_t currentTs = taosGetTimestampUs();
×
1307
    int64_t delta = currentTs - startTs;
×
1308
    if (delta < SEND_TIME_UNIT * 1000) {
×
1309
      int64_t sleepLen = (int32_t)(SEND_TIME_UNIT * 1000 - delta);
×
1310
      // printf("sleep %" PRId64 " us, use time: %" PRId64 " us\n", sleepLen, delta);
1311
      taosUsleep((int32_t)sleepLen);
×
1312
    }
1313

1314
    currentTs = taosGetTimestampUs();
×
1315
    delta = currentTs - lastPrintTime;
×
1316
    if (delta > 10 * 1000 * 1000) {
×
1317
      printf("producer[%d] info: %" PRId64 " msgs, %" PRId64 " Byte, %" PRId64 " us, totalSendLoopTimes: %d\n",
×
1318
             pInfo->consumerId, sendMsgs, totalMsgLen, delta, totalSendLoopTimes);
1319
      printf("producer[%d] rate: %1.f msgs/s, %1.f KB/s\n", pInfo->consumerId, sendMsgs * 1000.0 * 1000 / delta,
×
1320
             (totalMsgLen / 1024.0) / (delta / (1000 * 1000)));
×
1321
      lastPrintTime = currentTs;
×
1322
      sendMsgs = 0;
×
1323
      totalMsgLen = 0;
×
1324
    }
1325
  }
1326

1327
  printf("affectedRowsTotal: %" PRId64 "\n", affectedRowsTotal);
×
1328
  taos_close(pInfo->taos);
×
1329
  pInfo->taos = NULL;
×
1330
  taosMemoryFree(sqlBuf);
×
1331
  return NULL;
×
1332
}
1333

1334
void printProduceInfo(int64_t start) {
×
1335
  int64_t totalMsgs = 0;
×
1336
  int64_t totalLenOfMsgs = 0;
×
1337
  for (int i = 0; i < g_stConfInfo.producers; i++) {
×
1338
    totalMsgs += g_stConfInfo.stProdThreads[i].totalProduceMsgs;
×
1339
    totalLenOfMsgs += g_stConfInfo.stProdThreads[i].totalMsgsLen;
×
1340
  }
1341

1342
  int64_t end = taosGetTimestampUs();
×
1343

1344
  int64_t t = end - start;
×
1345
  if (0 == t) t = 1;
×
1346

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

1350
  printf("Spent %.3f seconds to prod %" PRIu64 " msgs with %d producer(s), throughput: %.3f msgs/s, %.1f MB/s\n\n",
×
1351
         tInMs, totalMsgs, g_stConfInfo.producers, (double)totalMsgs / tInMs,
×
1352
         (double)totalLenOfMsgs / (1024.0 * 1024) / tInMs);
×
1353
  return;
×
1354
}
1355

1356
void startOmbConsume() {
×
1357
  TdThreadAttr thattr;
×
1358
  taosThreadAttrInit(&thattr);
×
1359
  taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE);
×
1360

1361
  if (0 != g_stConfInfo.producers) {
×
1362
    TAOS* taos = createNewTaosConnect();
×
1363
    if (taos == NULL) {
×
1364
      taosFprintfFile(g_fp, "taos_connect() fail, can not create db, stbl, ctbl, topic!\n");
×
1365
      return;
×
1366
    }
1367

1368
    char stbName[16] = "stb";
×
1369
    char ctbPrefix[16] = "ctb";
×
1370

1371
    char sql[256] = {0};
×
1372
    sprintf(sql, "drop database if exists %s", g_stConfInfo.dbName);
×
1373
    printf("SQL: %s\n", sql);
×
1374
    queryDbExec(taos, sql, NO_INSERT_TYPE);
×
1375

1376
    sprintf(sql, "create database if not exists %s precision 'ns' vgroups %d", g_stConfInfo.dbName,
×
1377
            g_stConfInfo.producers);
1378
    printf("SQL: %s\n", sql);
×
1379
    queryDbExec(taos, sql, NO_INSERT_TYPE);
×
1380

1381
    sprintf(sql, "create stable %s.%s (ts timestamp, payload binary(%d)) tags (t bigint) ", g_stConfInfo.dbName,
×
1382
            stbName, g_stConfInfo.payloadLen);
1383
    printf("SQL: %s\n", sql);
×
1384
    queryDbExec(taos, sql, NO_INSERT_TYPE);
×
1385

1386
    for (int i = 0; i < g_stConfInfo.producers; i++) {
×
1387
      sprintf(sql, "create table %s.%s%d using %s.stb tags(%d) ", g_stConfInfo.dbName, ctbPrefix, i,
×
1388
              g_stConfInfo.dbName, i);
1389
      printf("SQL: %s\n", sql);
×
1390
      queryDbExec(taos, sql, NO_INSERT_TYPE);
×
1391
    }
1392

1393
    // create topic
1394
    sprintf(sql, "create topic %s as stable %s.%s", g_stConfInfo.topic, g_stConfInfo.dbName, stbName);
×
1395
    printf("SQL: %s\n", sql);
×
1396
    queryDbExec(taos, sql, NO_INSERT_TYPE);
×
1397

1398
    int32_t producerRate = ceil(((double)g_stConfInfo.producerRate) / g_stConfInfo.producers);
×
1399

1400
    printf("==== create %d produce thread ====\n", g_stConfInfo.producers);
×
1401
    for (int32_t i = 0; i < g_stConfInfo.producers; ++i) {
×
1402
      g_stConfInfo.stProdThreads[i].consumerId = i;
×
1403
      g_stConfInfo.stProdThreads[i].producerRate = producerRate;
×
1404
      taosThreadCreate(&(g_stConfInfo.stProdThreads[i].thread), &thattr, ombProduceThreadFunc,
×
1405
                       (void*)(&(g_stConfInfo.stProdThreads[i])));
×
1406
    }
1407

1408
    if (0 == g_stConfInfo.numOfThread) {
×
1409
      int64_t start = taosGetTimestampUs();
×
1410
      for (int32_t i = 0; i < g_stConfInfo.producers; i++) {
×
1411
        taosThreadJoin(g_stConfInfo.stProdThreads[i].thread, NULL);
×
1412
        taosThreadClear(&g_stConfInfo.stProdThreads[i].thread);
×
1413
      }
1414

1415
      printProduceInfo(start);
×
1416

1417
      taosFprintfFile(g_fp, "==== close tmqlog ====\n");
×
1418
      taosCloseFile(&g_fp);
×
1419
      taos_close(taos);
×
1420
      return;
×
1421
    }
1422

1423
    taos_close(taos);
×
1424
  }
1425

1426
  // pthread_create one thread to consume
1427
  taosFprintfFile(g_fp, "==== create %d consume thread ====\n", g_stConfInfo.numOfThread);
×
1428
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; ++i) {
×
1429
    g_stConfInfo.stThreads[i].consumerId = i;
×
1430
    taosThreadCreate(&(g_stConfInfo.stThreads[i].thread), &thattr, ombConsumeThreadFunc,
×
1431
                     (void*)(&(g_stConfInfo.stThreads[i])));
×
1432
  }
1433

1434
  int64_t start = taosGetTimestampUs();
×
1435

1436
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; i++) {
×
1437
    taosThreadJoin(g_stConfInfo.stThreads[i].thread, NULL);
×
1438
    taosThreadClear(&g_stConfInfo.stThreads[i].thread);
×
1439
  }
1440

1441
  int64_t end = taosGetTimestampUs();
×
1442

1443
  int64_t totalRows = 0;
×
1444
  int64_t totalMsgs = 0;
×
1445
  int64_t totalLenOfMsgs = 0;
×
1446
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; i++) {
×
1447
    totalMsgs += g_stConfInfo.stThreads[i].consumeMsgCnt;
×
1448
    totalLenOfMsgs += g_stConfInfo.stThreads[i].consumeLen;
×
1449
    totalRows += g_stConfInfo.stThreads[i].consumeRowCnt;
×
1450
  }
1451

1452
  int64_t t = end - start;
×
1453
  if (0 == t) t = 1;
×
1454

1455
  double tInMs = (double)t / 1000000.0;
×
1456
  taosFprintfFile(
×
1457
      g_fp, "Spent %.3f seconds to poll msgs: %" PRIu64 " with %d thread(s), throughput: %.3f msgs/s, %.1f MB/s\n\n",
1458
      tInMs, totalMsgs, g_stConfInfo.numOfThread, (double)(totalMsgs / tInMs),
×
1459
      (double)totalLenOfMsgs / (1024 * 1024) / tInMs);
×
1460

1461
  printf("Spent %.3f seconds to cons rows: %" PRIu64 " msgs: %" PRIu64
×
1462
         " with %d thread(s), throughput: %.3f msgs/s, %.1f MB/s\n\n",
1463
         tInMs, totalRows, totalMsgs, g_stConfInfo.numOfThread, (double)(totalMsgs / tInMs),
×
1464
         (double)totalLenOfMsgs / (1024 * 1024) / tInMs);
×
1465

1466
  taosFprintfFile(g_fp, "==== close tmqlog ====\n");
×
1467
  taosCloseFile(&g_fp);
×
1468

1469
  return;
×
1470
}
1471

1472
int main(int32_t argc, char* argv[]) {
64,776✔
1473
  parseArgument(argc, argv);
64,776✔
1474

1475
  if (0 != strlen(g_stConfInfo.topic)) {
64,776✔
1476
    startOmbConsume();
×
1477
    return 0;
×
1478
  }
1479

1480
  int32_t retCode = getConsumeInfo();
64,776✔
1481
  if (0 != retCode) {
64,776✔
1482
    return -1;
×
1483
  }
1484

1485
  saveConfigToLogFile();
64,776✔
1486

1487
  tmqSetSignalHandle();
64,776✔
1488

1489
  TdThreadAttr thattr;
61,190✔
1490
  taosThreadAttrInit(&thattr);
64,776✔
1491
  taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE);
64,776✔
1492

1493
  // pthread_create one thread to consume
1494
  taosFprintfFile(g_fp, "==== create %d consume thread ====\n", g_stConfInfo.numOfThread);
64,776✔
1495
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; ++i) {
135,341✔
1496
    taosThreadCreate(&(g_stConfInfo.stThreads[i].thread), &thattr, consumeThreadFunc,
70,565✔
1497
                     (void*)(&(g_stConfInfo.stThreads[i])));
70,565✔
1498
  }
1499

1500
  int64_t start = taosGetTimestampUs();
64,776✔
1501

1502
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; i++) {
135,341✔
1503
    taosThreadJoin(g_stConfInfo.stThreads[i].thread, NULL);
70,565✔
1504
    taosThreadClear(&g_stConfInfo.stThreads[i].thread);
70,565✔
1505
  }
1506

1507
  int64_t end = taosGetTimestampUs();
64,776✔
1508

1509
  int64_t totalMsgs = 0;
64,776✔
1510
  for (int32_t i = 0; i < g_stConfInfo.numOfThread; i++) {
135,341✔
1511
    totalMsgs += g_stConfInfo.stThreads[i].consumeMsgCnt;
70,565✔
1512
  }
1513

1514
  int64_t t = end - start;
64,776✔
1515
  if (0 == t) t = 1;
64,776✔
1516

1517
  double tInMs = (double)t / 1000000.0;
64,776✔
1518
  taosFprintfFile(g_fp,
64,776✔
1519
                  "Spent %.3f seconds to poll msgs: %" PRIu64 " with %d thread(s), throughput: %.3f msgs/second\n\n",
1520
                  tInMs, totalMsgs, g_stConfInfo.numOfThread, (double)(totalMsgs / tInMs));
61,190✔
1521

1522
  taosFprintfFile(g_fp, "==== close tmqlog ====\n");
64,776✔
1523
  taosCloseFile(&g_fp);
64,776✔
1524

1525
  return 0;
64,776✔
1526
}
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