Skip to content

0.31.0 #149

Merged
merged 20 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
652 changes: 247 additions & 405 deletions mx_mysql.c

Large diffs are not rendered by default.

46 changes: 15 additions & 31 deletions mx_mysql.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,18 @@

#include "mx_util.h"

#ifdef MX_NDEBUG_MYSQL
# include <assert.h>
# define mx_mysql_assert_return_minus_errno(test, eno) \
assert(test)
# define mx_mysql_assert_return_NULL(test, eno) \
assert(test)
#else
# define mx_mysql_assert_return_minus_errno(test, eno) \
mx_assert_return_minus_errno(test, eno)
# define mx_mysql_assert_return_NULL(test, eno) \
mx_assert_return_NULL(test, eno)
#endif

#define mx_mysql_assert_usage_ok(res) \
do { \
if ((res) < 0) { \
assert((res) == -errno); \
assert(errno != EINVAL); \
assert(errno != EBADF); \
assert(errno != EUCLEAN); \
assert(res != EINVAL); \
assert(res != EBADF); \
assert(res != EUCLEAN); \
} \
} while (0)

struct mx_mysql {
MYSQL *mysql;

const char *func;

char *default_file;
char *default_group;
bool reconnect;
Expand Down Expand Up @@ -74,8 +58,6 @@ struct mx_mysql_stmt {

MYSQL_STMT *stmt;

const char *func;

unsigned long field_count;
unsigned long param_count;

Expand Down Expand Up @@ -135,18 +117,20 @@ int mx_mysql_statement_close_no_bind_cleanup(struct mx_mysql_stmt **stmt);
#define mx_mysql_bind_init_param(b, c) mx_mysql_bind_init((b), (c), MX_MYSQL_BIND_TYPE_PARAM)
#define mx_mysql_bind_init_result(b, c) mx_mysql_bind_init((b), (c), MX_MYSQL_BIND_TYPE_RESULT)

int mx_mysql_bind_init(struct mx_mysql_bind *bind, unsigned long count, enum mx_mysql_bind_type type);
void mx_mysql_bind_init(struct mx_mysql_bind *bind, unsigned long count, enum mx_mysql_bind_type type);

void mx_mysql_bind_string(struct mx_mysql_bind *b, unsigned int index, char **value);

int mx_mysql_bind_string(struct mx_mysql_bind *b, unsigned int index, char **value);
void mx_mysql_bind_int8(struct mx_mysql_bind *b, unsigned int index, int8_t *value);
void mx_mysql_bind_int16(struct mx_mysql_bind *b, unsigned int index, int16_t *value);
void mx_mysql_bind_int32(struct mx_mysql_bind *b, unsigned int index, int32_t *value);
void mx_mysql_bind_int64(struct mx_mysql_bind *b, unsigned int index, int64_t *value);

int mx_mysql_bind_int8(struct mx_mysql_bind *b, unsigned int index, int8_t *value);
int mx_mysql_bind_int16(struct mx_mysql_bind *b, unsigned int index, int16_t *value);
int mx_mysql_bind_int32(struct mx_mysql_bind *b, unsigned int index, int32_t *value);
int mx_mysql_bind_int64(struct mx_mysql_bind *b, unsigned int index, int64_t *value);
void mx_mysql_bind_uint8(struct mx_mysql_bind *b, unsigned int index, uint8_t *value);
void mx_mysql_bind_uint16(struct mx_mysql_bind *b, unsigned int index, uint16_t *value);
void mx_mysql_bind_uint32(struct mx_mysql_bind *b, unsigned int index, uint32_t *value);
void mx_mysql_bind_uint64(struct mx_mysql_bind *b, unsigned int index, uint64_t *value);

int mx_mysql_bind_uint8(struct mx_mysql_bind *b, unsigned int index, uint8_t *value);
int mx_mysql_bind_uint16(struct mx_mysql_bind *b, unsigned int index, uint16_t *value);
int mx_mysql_bind_uint32(struct mx_mysql_bind *b, unsigned int index, uint32_t *value);
int mx_mysql_bind_uint64(struct mx_mysql_bind *b, unsigned int index, uint64_t *value);
char *mx_mysql_error(void);

#endif
97 changes: 42 additions & 55 deletions mxq_daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,38 +103,36 @@ int mxq_daemon_register(struct mx_mysql *mysql, struct mxq_daemon *daemon)
" prerequisites = ?"
);
if (!stmt) {
mx_log_err("mx_mysql_statement_prepare(): %m");
mx_log_err("mx_mysql_statement_prepare(): %s", mx_mysql_error());
mx_mysql_statement_close(&stmt);
return -errno;
return -EINVAL;
}

idx = 0;

res = mx_mysql_statement_param_bind(stmt, idx++, string, &(daemon->daemon_name));
res += mx_mysql_statement_param_bind(stmt, idx++, uint8, &(daemon->status));
res += mx_mysql_statement_param_bind(stmt, idx++, string, &(daemon->hostname));
res += mx_mysql_statement_param_bind(stmt, idx++, string, &(daemon->mxq_version));
res += mx_mysql_statement_param_bind(stmt, idx++, string, &(daemon->boot_id));
mx_mysql_statement_param_bind(stmt, idx++, string, &(daemon->daemon_name));
mx_mysql_statement_param_bind(stmt, idx++, uint8, &(daemon->status));
mx_mysql_statement_param_bind(stmt, idx++, string, &(daemon->hostname));
mx_mysql_statement_param_bind(stmt, idx++, string, &(daemon->mxq_version));
mx_mysql_statement_param_bind(stmt, idx++, string, &(daemon->boot_id));

res += mx_mysql_statement_param_bind(stmt, idx++, uint64, &(daemon->pid_starttime));
res += mx_mysql_statement_param_bind(stmt, idx++, uint32, &(daemon->daemon_pid));
mx_mysql_statement_param_bind(stmt, idx++, uint64, &(daemon->pid_starttime));
mx_mysql_statement_param_bind(stmt, idx++, uint32, &(daemon->daemon_pid));

res += mx_mysql_statement_param_bind(stmt, idx++, uint32, &(daemon->daemon_slots));
res += mx_mysql_statement_param_bind(stmt, idx++, uint64, &(daemon->daemon_memory));
res += mx_mysql_statement_param_bind(stmt, idx++, uint64, &(daemon->daemon_maxtime));
mx_mysql_statement_param_bind(stmt, idx++, uint32, &(daemon->daemon_slots));
mx_mysql_statement_param_bind(stmt, idx++, uint64, &(daemon->daemon_memory));
mx_mysql_statement_param_bind(stmt, idx++, uint64, &(daemon->daemon_maxtime));

res += mx_mysql_statement_param_bind(stmt, idx++, uint64, &(daemon->daemon_memory_limit_slot_soft));
res += mx_mysql_statement_param_bind(stmt, idx++, uint64, &(daemon->daemon_memory_limit_slot_hard));
res += mx_mysql_statement_param_bind(stmt, idx++, uint16, &(daemon->gpus_max));
res += mx_mysql_statement_param_bind(stmt, idx++, int32, &(daemon->daemon_flags));
res += mx_mysql_statement_param_bind(stmt, idx++, string, &(daemon->tags));
res += mx_mysql_statement_param_bind(stmt, idx++, string, &(daemon->prerequisites));

assert(res ==0);
mx_mysql_statement_param_bind(stmt, idx++, uint64, &(daemon->daemon_memory_limit_slot_soft));
mx_mysql_statement_param_bind(stmt, idx++, uint64, &(daemon->daemon_memory_limit_slot_hard));
mx_mysql_statement_param_bind(stmt, idx++, uint16, &(daemon->gpus_max));
mx_mysql_statement_param_bind(stmt, idx++, int32, &(daemon->daemon_flags));
mx_mysql_statement_param_bind(stmt, idx++, string, &(daemon->tags));
mx_mysql_statement_param_bind(stmt, idx++, string, &(daemon->prerequisites));

res = mx_mysql_statement_execute(stmt, &num_rows);
if (res < 0) {
mx_log_err("mx_mysql_statement_execute(): %m");
mx_log_err("mx_mysql_statement_execute(): %s", mx_mysql_error());
mx_mysql_statement_close(&stmt);
return res;
}
Expand Down Expand Up @@ -168,17 +166,15 @@ int mxq_daemon_shutdown(struct mx_mysql *mysql, struct mxq_daemon *daemon)
" status = " status_str(MXQ_DAEMON_STATUS_EXITED)
" WHERE daemon_id = ?";

res = mx_mysql_bind_init_param(&param, 1);
assert(res == 0);
mx_mysql_bind_init_param(&param, 1);

idx = 0;
res = 0;
res += mx_mysql_bind_var(&param, idx++, uint32, &(daemon->daemon_id));
mx_mysql_bind_var(&param, idx++, uint32, &(daemon->daemon_id));
assert(res == 0);
donald marked this conversation as resolved.
Show resolved Hide resolved

res += mx_mysql_do_statement_noresult_retry_on_fail(mysql, query, &param);
res = mx_mysql_do_statement_noresult_retry_on_fail(mysql, query, &param);
if (res < 0) {
mx_log_err("mx_mysql_do_statement(): %m");
mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error());
return res;
}

Expand Down Expand Up @@ -209,19 +205,16 @@ int mxq_daemon_mark_crashed(struct mx_mysql *mysql, struct mxq_daemon *daemon)
" AND hostname = ?"
" AND daemon_name = ?";

res = mx_mysql_bind_init_param(&param, 3);
assert(res == 0);
mx_mysql_bind_init_param(&param, 3);

idx = 0;
res = 0;
res += mx_mysql_bind_var(&param, idx++, uint32, &daemon->daemon_id);
res += mx_mysql_bind_var(&param, idx++, string, &daemon->hostname);
res += mx_mysql_bind_var(&param, idx++, string, &daemon->daemon_name);
assert(res == 0);
mx_mysql_bind_var(&param, idx++, uint32, &daemon->daemon_id);
mx_mysql_bind_var(&param, idx++, string, &daemon->hostname);
mx_mysql_bind_var(&param, idx++, string, &daemon->daemon_name);

res += mx_mysql_do_statement_noresult_retry_on_fail(mysql, query, &param);
res = mx_mysql_do_statement_noresult_retry_on_fail(mysql, query, &param);
if (res < 0) {
mx_log_err("mx_mysql_do_statement(): %m");
mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error());
return res;
}

Expand Down Expand Up @@ -249,18 +242,15 @@ int mxq_daemon_set_status(struct mx_mysql *mysql, struct mxq_daemon *daemon, uin
" status = ?"
" WHERE daemon_id = ?";

res = mx_mysql_bind_init_param(&param, 2);
assert(res == 0);
mx_mysql_bind_init_param(&param, 2);

idx = 0;
res = 0;
res += mx_mysql_bind_var(&param, idx++, uint8, &(status));
res += mx_mysql_bind_var(&param, idx++, uint32, &(daemon->daemon_id));
assert(res == 0);
mx_mysql_bind_var(&param, idx++, uint8, &(status));
mx_mysql_bind_var(&param, idx++, uint32, &(daemon->daemon_id));

res += mx_mysql_do_statement_noresult_retry_on_fail(mysql, query, &param);
res = mx_mysql_do_statement_noresult_retry_on_fail(mysql, query, &param);
if (res < 0) {
mx_log_err("mx_mysql_do_statement(): %m");
mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error());
return res;
}

Expand Down Expand Up @@ -290,22 +280,19 @@ int mxq_daemon_update_statistics(struct mx_mysql *mysql, struct mxq_daemon *daem
" daemon_gpus_used = ?"
" WHERE daemon_id = ?";

res = mx_mysql_bind_init_param(&param, 6);
assert(res == 0);
mx_mysql_bind_init_param(&param, 6);

idx = 0;
res = 0;
res += mx_mysql_bind_var(&param, idx++, uint32, &(daemon->daemon_jobs_running));
res += mx_mysql_bind_var(&param, idx++, uint32, &(daemon->daemon_slots_running));
res += mx_mysql_bind_var(&param, idx++, uint32, &(daemon->daemon_threads_running));
res += mx_mysql_bind_var(&param, idx++, uint64, &(daemon->daemon_memory_used));
res += mx_mysql_bind_var(&param, idx++, uint16, &(daemon->gpus_used));
res += mx_mysql_bind_var(&param, idx++, uint32, &(daemon->daemon_id));
assert(res == 0);
mx_mysql_bind_var(&param, idx++, uint32, &(daemon->daemon_jobs_running));
mx_mysql_bind_var(&param, idx++, uint32, &(daemon->daemon_slots_running));
mx_mysql_bind_var(&param, idx++, uint32, &(daemon->daemon_threads_running));
mx_mysql_bind_var(&param, idx++, uint64, &(daemon->daemon_memory_used));
mx_mysql_bind_var(&param, idx++, uint16, &(daemon->gpus_used));
mx_mysql_bind_var(&param, idx++, uint32, &(daemon->daemon_id));

res = mx_mysql_do_statement_noresult_retry_on_fail(mysql, query, &param);
if (res < 0) {
mx_log_err("mx_mysql_do_statement(): %m");
mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error());
return res;
}

Expand Down