0

When I use below code to insert into TDengine database ,sometimes it will lose connection.

you can compile it and run to reproduce issue ,anyone could help on this ?

I use the async taos_query_a API to test the response time .:

//
// Created by jia.wen on 8/15/22.
//

//#include <fmt/format.h>
#include <chrono>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unistd.h>
#include <vector>

#include <taos.h>

static unsigned long long get_nanosecs_since_epoch()
{
    using namespace std::chrono;
    return static_cast<unsigned long long>(duration_cast<nanoseconds>(
            system_clock::now().time_since_epoch()).count());
}

static std::string format_nanosecs_since_epoch(unsigned long long nanosecs_since_epoch)
{
    std::time_t secs_since_epoch(nanosecs_since_epoch / 1000000000);
    std::tm secs_since_epoch_tm;
    localtime_r(&secs_since_epoch, &secs_since_epoch_tm);
    unsigned long long remaining_nanosecs = nanosecs_since_epoch % 1000000000;

    char buffer[30];
    int year = secs_since_epoch_tm.tm_year + 1900;
    buffer[0] = (year / 1000) + '0';
    buffer[1] = (year % 1000) / 100 + '0';
    buffer[2] = (year % 100) / 10 + '0';
    buffer[3] = (year % 10) + '0';
    buffer[4] = '-';
    int month = secs_since_epoch_tm.tm_mon + 1;
    buffer[5] = (month / 10) + '0';
    buffer[6] = (month % 10) + '0';
    buffer[7] = '-';
    buffer[8] = (secs_since_epoch_tm.tm_mday / 10) + '0';
    buffer[9] = (secs_since_epoch_tm.tm_mday % 10) + '0';
    buffer[10] = ' ';
    buffer[11] = (secs_since_epoch_tm.tm_hour / 10) + '0';
    buffer[12] = (secs_since_epoch_tm.tm_hour % 10) + '0';
    buffer[13] = ':';
    buffer[14] = (secs_since_epoch_tm.tm_min / 10) + '0';
    buffer[15] = (secs_since_epoch_tm.tm_min % 10) + '0';
    buffer[16] = ':';
    buffer[17] = (secs_since_epoch_tm.tm_sec / 10) + '0';
    buffer[18] = (secs_since_epoch_tm.tm_sec % 10) + '0';
    buffer[19] = '.';
    buffer[20] = (remaining_nanosecs / 100000000) + '0';
    buffer[21] = (remaining_nanosecs % 100000000) / 10000000 + '0';
    buffer[22] = (remaining_nanosecs % 10000000) / 1000000 + '0';
    buffer[23] = (remaining_nanosecs % 1000000) / 100000 + '0';
    buffer[24] = (remaining_nanosecs % 100000) / 10000 + '0';
    buffer[25] = (remaining_nanosecs % 10000) / 1000 + '0';
    buffer[26] = (remaining_nanosecs % 1000) / 100 + '0';
    buffer[27] = (remaining_nanosecs % 100) / 10 + '0';
    buffer[28] = (remaining_nanosecs % 10) + '0';
    buffer[29] = 0;
    return std::string(buffer);
}

void taos_insert_call_back(void* param, TAOS_RES* tres, int code)
{
    unsigned long long* insert_time = (unsigned long long *)param;
    struct timeval systemTime;
    char sql[128];

    if (code < 0)
    {
        printf("Insert failed. code:%d, cause:%s\n", code, taos_errstr(tres));
    }
    else if (code == 0)
    {
        // printf("Not inserted.\n");
    }
    else
    {
        printf("Succeed.\n");
    }

    taos_free_result(tres);
}

int main()
{
    const char *host = "localhost:6030";
    const char *user = "root";
    const char *passwd = "taosdata";
    const char *db = "test_lts";
    uint16_t port = 0;  // 0 means use the default port
    TAOS *taos = taos_connect(host, user, passwd, db, port);

    if (taos == NULL)
    {
        int errno_n = taos_errno(NULL);
        auto msg = taos_errstr(NULL);
        printf("%d, %s\n", errno, msg);
    }
    else
    {
        printf("connected\n");

        std::string ins_sql;
        std::vector<int> test_cycles_;
        TAOS_RES* exec_result = NULL;

        int cycles = 60;
        int insert_times = 2000;
        for (auto i = 0; i < cycles; ++i)
        {
            for (auto j = 0; j < insert_times; ++j)
            {
                auto local_time = format_nanosecs_since_epoch(get_nanosecs_since_epoch());

                ins_sql = "INSERT INTO tick_jw VALUES ('" + local_time + "'," + std::to_string(i + 1) + ", 20221018, " + std::to_string(j)
                        + ", 'test', 1.2, 200, 1000, 0, 0, 0, "
                          "1.15, 1.14, 1.13, 1.12, 1.11, 5, 4, 3, 2, 1, 1.16, 1.17, 1.18, 1.19, 1.20, 5, 4, 3, 2, 1)";
                taos_query_a(taos, ins_sql.c_str(), taos_insert_call_back, (void *)(0));
//                usleep(60);
            }

            std::cout << "cycles = " << i + 1 << " finished." << std::endl;
            // sleep(1);
        }

        std::cout << "insert_times = " << insert_times << " test async finished." << std::endl;

        sleep(60);
        taos_close(taos);
    }

    taos_cleanup();
}

the core part is just :taos_query_a(taos, ins_sql.c_str(), taos_insert_call_back, (void *)(0));

Yu Chen
  • 70
  • 1
  • 5

0 Answers0