C

Dependencies

  • C compiler (e.g. gcc)

  • pthreads library (present on most Linux distributions)

  • mysqlclient library, available from the libmysqlclient-dev package on Debian-based distributions.

Code

/* Compile with:
*
* cc multi_threaded_inserts.c -lmysqlclient -pthread -o mti
*/
#include <stdlib.h>
#include <stdio.h>
#include <mysql/mysql.h>
const static char *host = "127.0.0.1";
const static char *user = "root";
const static char *passwd = "";
const static size_t port = 3306;
#define NUM_WORKERS 20
static volatile int keep_going = 1;
void *insert_worker(void *worker_id);
int main()
{
my_init();
MYSQL conn;
mysql_init(&conn);
printf("Connecting to SingleStore...\n");
if (mysql_real_connect(&conn, host, user, passwd, NULL, port, NULL, 0) != &conn)
{
printf("Could not connect to the SingleStore database!\n");
goto failure;
}
printf("Creating database 'test'...\n");
if (mysql_query(&conn, "create database test") || mysql_query(&conn, "use test"))
{
printf("Could not create 'test' database!\n");
goto failure;
}
printf("Creating table 'tbl' in database 'test'...\n");
if (mysql_query(&conn, "create table tbl (id bigint auto_increment primary key)"))
{
printf("Could not create 'tbl' table in the 'test' database!\n");
goto failure;
}
printf("Launching %lu insert workers...\n", NUM_WORKERS);
pthread_t workers[NUM_WORKERS];
size_t i;
for (i = 0; i < NUM_WORKERS; ++i)
{
pthread_create(&workers[i], NULL, &insert_worker, (void *)i);
}
printf("Running inserts for %lu seconds...\n", 10);
sleep(10);
keep_going = 0;
size_t rows_inserted = 0;
for (i = 0; i < NUM_WORKERS; ++i)
{
size_t rows_i;
pthread_join(workers[i], &rows_i);
rows_inserted += rows_i;
}
printf("Inserted %lu rows. Cleaning up...\n", rows_inserted);
if (mysql_query(&conn, "drop database test"))
{
printf("Could not drop the testing database 'test'!\n");
}
mysql_close(&conn);
return 0;
failure:
mysql_close(&conn);
return 1;
}
void *insert_worker(void *worker_id)
{
size_t id = (size_t) worker_id;
MYSQL conn;
mysql_init(&conn);
if (mysql_real_connect(&conn, host, user, passwd, "test", port, NULL, 0) != &conn)
{
printf("Worker %lu could not connect to the SingleStore database! Aborting...\n", id);
exit(1);
}
size_t i;
for (i = 0; keep_going; i += 8)
{
if (mysql_query(&conn, "insert into tbl values (null), (null), (null),"
"(null), (null), (null), (null), (null)"))
{
printf("Worker %lu failed to insert data, aborting...\n", id);
exit(1);
}
}
mysql_close(&conn);
return (void *)i;
}

Last modified: July 26, 2023

Was this article helpful?

Verification instructions

Note: You must install cosign to verify the authenticity of the SingleStore file.

Use the following steps to verify the authenticity of singlestoredb-server, singlestoredb-toolbox, singlestoredb-studio, and singlestore-client SingleStore files that have been downloaded.

You may perform the following steps on any computer that can run cosign, such as the main deployment host of the cluster.

  1. (Optional) Run the following command to view the associated signature files.

    curl undefined
  2. Download the signature file from the SingleStore release server.

    • Option 1: Click the Download Signature button next to the SingleStore file.

    • Option 2: Copy and paste the following URL into the address bar of your browser and save the signature file.

    • Option 3: Run the following command to download the signature file.

      curl -O undefined
  3. After the signature file has been downloaded, run the following command to verify the authenticity of the SingleStore file.

    echo -n undefined |
    cosign verify-blob --certificate-oidc-issuer https://oidc.eks.us-east-1.amazonaws.com/id/CCDCDBA1379A5596AB5B2E46DCA385BC \
    --certificate-identity https://kubernetes.io/namespaces/freya-production/serviceaccounts/job-worker \
    --bundle undefined \
    --new-bundle-format -
    Verified OK