From 779c991762b536bba9dc0db3236268f7999ddcec Mon Sep 17 00:00:00 2001 From: Cole Miller Date: Tue, 17 Oct 2023 14:04:41 -0400 Subject: [PATCH 1/2] Refactor the DB context code a bit Signed-off-by: Cole Miller --- src/revamp.c | 17 +++++++++++++++++ src/revamp.h | 10 ++++++++-- src/server.c | 13 ++++++++----- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/revamp.c b/src/revamp.c index 18745b686..14d4ea27f 100644 --- a/src/revamp.c +++ b/src/revamp.c @@ -1 +1,18 @@ #include "revamp.h" + +int dbContextInit(struct db_context *ctx, struct config *config) +{ + int rv; + rv = sem_init(&ctx->sem, 0, 0); + if (rv != 0) { + return DQLITE_ERROR; + } + registry__init(&ctx->registry, config); + return 0; +} + +void dbContextClose(struct db_context *ctx) +{ + registry__close(&ctx->registry); + sem_destroy(&ctx->sem); +} diff --git a/src/revamp.h b/src/revamp.h index fd5eed561..eb5536713 100644 --- a/src/revamp.h +++ b/src/revamp.h @@ -1,11 +1,17 @@ -#ifndef DQLITE_REVAMP_H -#define DQLITE_REVAMP_H +#ifndef DQLITE_REVAMP_H_ +#define DQLITE_REVAMP_H_ + +#include "registry.h" #include struct db_context { sem_t sem; + struct registry registry; }; +int dbContextInit(struct db_context *ctx, struct config *config); +void dbContextClose(struct db_context *ctx); + #endif diff --git a/src/server.c b/src/server.c index 760367d9e..4d4638589 100644 --- a/src/server.c +++ b/src/server.c @@ -167,8 +167,8 @@ void dqlite__close(struct dqlite_node *d) if (!d->initialized) { return; } - sem_destroy(&d->db_ctx->sem); - free(d->db_ctx); + dbContextClose(d->db_ctx); + sqlite3_free(d->db_ctx); raft_free(d->listener); rv = sem_destroy(&d->stopped); assert(rv == 0); /* Fails only if sem object is not valid */ @@ -687,12 +687,15 @@ static int taskRun(struct dqlite_node *d) { int rv; - d->db_ctx = malloc(sizeof *d->db_ctx); + d->db_ctx = sqlite3_malloc(sizeof *d->db_ctx); if (d->db_ctx == NULL) { return DQLITE_NOMEM; } - rv = sem_init(&d->db_ctx->sem, 0, 0); - assert(rv == 0); + rv = dbContextInit(d->db_ctx, &d->config); + if (rv != 0) { + sqlite3_free(d->db_ctx); + return rv; + } rv = pthread_create(&d->db_thread, NULL, dbTask, d->db_ctx); assert(rv == 0); From f638fd733c48f48019f5dff34716d4f441c51218 Mon Sep 17 00:00:00 2001 From: Cole Miller Date: Wed, 18 Oct 2023 00:01:57 -0400 Subject: [PATCH 2/2] WIP: start working on a way to send EXEC_SQL to the DB thread This has a deadlock bug that needs to be ironed out. Signed-off-by: Cole Miller --- src/revamp.c | 41 ++++++++++++++++++++++++++++++++++++----- src/revamp.h | 26 ++++++++++++++++++++++++-- src/server.c | 18 ++++++++++-------- 3 files changed, 70 insertions(+), 15 deletions(-) diff --git a/src/revamp.c b/src/revamp.c index 14d4ea27f..35ba9fc46 100644 --- a/src/revamp.c +++ b/src/revamp.c @@ -3,16 +3,47 @@ int dbContextInit(struct db_context *ctx, struct config *config) { int rv; - rv = sem_init(&ctx->sem, 0, 0); - if (rv != 0) { - return DQLITE_ERROR; - } + rv = pthread_mutex_init(&ctx->mutex, NULL); + assert(rv == 0); + rv = pthread_cond_init(&ctx->cond, NULL); + assert(rv == 0); registry__init(&ctx->registry, config); + ctx->shutdown = false; + return 0; +} + +int postExecSqlReq(struct db_context *ctx, + struct exec_sql_req req, + void *data, + void (*cb)(struct exec_sql_req, int, void *)) +{ + (void)ctx; + (void)req; + (void)data; + (void)cb; return 0; } void dbContextClose(struct db_context *ctx) { registry__close(&ctx->registry); - sem_destroy(&ctx->sem); + pthread_cond_destroy(&ctx->cond); + pthread_mutex_destroy(&ctx->mutex); +} + +void *dbTask(void *arg) +{ + struct db_context *ctx = arg; + int rv; + + rv = pthread_mutex_lock(&ctx->mutex); + assert(rv == 0); + for (;;) { + rv = pthread_cond_wait(&ctx->cond, &ctx->mutex); + assert(rv == 0); + if (ctx->shutdown) { + break; + } + } + return NULL; } diff --git a/src/revamp.h b/src/revamp.h index eb5536713..51bacb670 100644 --- a/src/revamp.h +++ b/src/revamp.h @@ -1,17 +1,39 @@ #ifndef DQLITE_REVAMP_H_ #define DQLITE_REVAMP_H_ +#include "lib/queue.h" #include "registry.h" +#include "tuple.h" -#include +#include +#include struct db_context { - sem_t sem; + pthread_mutex_t mutex; + pthread_cond_t cond; struct registry registry; + queue exec_sql_reqs; + bool shutdown; +}; + +struct exec_sql_req +{ + char *db_name; + char *sql; + struct value *params; + queue queue; }; int dbContextInit(struct db_context *ctx, struct config *config); + +int postExecSqlReq(struct db_context *ctx, + struct exec_sql_req req, + void *data, + void (*cb)(struct exec_sql_req, int, void *)); + void dbContextClose(struct db_context *ctx); +void *dbTask(void *arg); + #endif diff --git a/src/server.c b/src/server.c index 4d4638589..f88fb1b7a 100644 --- a/src/server.c +++ b/src/server.c @@ -15,6 +15,7 @@ #include "lib/fs.h" #include "logger.h" #include "protocol.h" +#include "revamp.h" #include "roles.h" #include "tracing.h" #include "translate.h" @@ -529,7 +530,10 @@ static void stopCb(uv_async_t *stop) } raft_close(&d->raft, raftCloseCb); - sem_post(&d->db_ctx->sem); + pthread_mutex_lock(&d->db_ctx->mutex); + d->db_ctx->shutdown = true; + pthread_cond_signal(&d->db_ctx->cond); + pthread_mutex_unlock(&d->db_ctx->mutex); pthread_join(d->db_thread, NULL); } @@ -676,13 +680,6 @@ static void roleManagementTimerCb(uv_timer_t *handle) RolesAdjust(d); } -static void *dbTask(void *arg) -{ - struct db_context *ctx = arg; - sem_wait(&ctx->sem); - return NULL; -} - static int taskRun(struct dqlite_node *d) { int rv; @@ -697,6 +694,8 @@ static int taskRun(struct dqlite_node *d) return rv; } + rv = pthread_mutex_lock(&d->db_ctx->mutex); + assert(rv == 0); rv = pthread_create(&d->db_thread, NULL, dbTask, d->db_ctx); assert(rv == 0); @@ -754,6 +753,9 @@ static int taskRun(struct dqlite_node *d) return rv; } + rv = pthread_mutex_unlock(&d->db_ctx->mutex); + assert(rv == 0); + rv = uv_run(&d->loop, UV_RUN_DEFAULT); assert(rv == 0);