diff --git a/mx_mysql.c b/mx_mysql.c index 87e4789b..686eb0c4 100644 --- a/mx_mysql.c +++ b/mx_mysql.c @@ -15,158 +15,95 @@ #include #include +#include #include "mx_mysql.h" #include "mx_util.h" #include "mx_log.h" -#define mx__mysql_log(lvl, mysql) \ - mx_log_ ## lvl("MySQL %s(): ERROR %u (%s): %s", \ - (mysql)->func, \ - mx__mysql_errno(mysql), \ - mx__mysql_sqlstate(mysql), \ - mx__mysql_error(mysql)) - -#define mx__mysql_stmt_log(lvl, stmt) \ - mx_log_ ## lvl("MySQL %s(): ERROR %u (%s): %s", \ - (stmt)->func, \ - mx__mysql_stmt_errno(stmt), \ - mx__mysql_stmt_sqlstate(stmt), \ - mx__mysql_stmt_error(stmt)) - -#define mx__mysql_log_emerg(mysql) mx__mysql_log(emerg, (mysql)) -#define mx__mysql_log_err(mysql) mx__mysql_log(err, (mysql)) -#define mx__mysql_log_warning(mysql) mx__mysql_log(warning, (mysql)) -#define mx__mysql_log_info(mysql) mx__mysql_log(info, (mysql)) -#define mx__mysql_log_debug(mysql) mx__mysql_log(debug, (mysql)) - -#define mx__mysql_stmt_log_emerg(stmt) mx__mysql_stmt_log(emerg, (stmt)) -#define mx__mysql_stmt_log_err(stmt) mx__mysql_stmt_log(err, (stmt)) -#define mx__mysql_stmt_log_warning(stmt) mx__mysql_stmt_log(warning, (stmt)) -#define mx__mysql_stmt_log_info(stmt) mx__mysql_stmt_log(info, (stmt)) -#define mx__mysql_stmt_log_debug(stmt) mx__mysql_stmt_log(debug, (stmt)) - static struct mx_mysql_stmt *mx_mysql_statement_prepare_with_bindings(struct mx_mysql *mysql, char *statement, struct mx_mysql_bind *param, struct mx_mysql_bind *result); /**********************************************************************/ -static int mx__mysql_errno(struct mx_mysql *mysql) -{ - unsigned int error; - - mx_assert_return_minus_errno(mysql, EINVAL); - mx_assert_return_minus_errno(mysql->mysql, EBADF); +static char* mx_mysql_last_error; - error = mysql_errno(mysql->mysql); - mx_assert_return_minus_errno((unsigned int)(int)error == error, ERANGE); - - /* no mysql errors possible */ - return (int)error; +__attribute__((destructor)) +static void mx_mysql_exit(void) { + free(mx_mysql_last_error); } -static const char *mx__mysql_error(struct mx_mysql *mysql) -{ - mx_assert_return_NULL(mysql, EINVAL); - mx_assert_return_NULL(mysql->mysql, EBADF); - - /* no mysql errors possible */ - return mysql_error(mysql->mysql); -} - -static const char *mx__mysql_sqlstate(struct mx_mysql *mysql) -{ - mx_assert_return_NULL(mysql, EINVAL); - mx_assert_return_NULL(mysql->mysql, EBADF); - - /* no mysql errors possible */ - return mysql_sqlstate(mysql->mysql); +static void mx_mysql_save_error(const char *err) { + size_t len = strlen(err); + mx_mysql_last_error = realloc(mx_mysql_last_error, len+1); + if (mx_mysql_last_error == NULL) { + mx_log_crit("out of memory"); + return; + } + strncpy(mx_mysql_last_error, err, len+1); } -static int mx__mysql_stmt_errno(struct mx_mysql_stmt *stmt) -{ - unsigned int error; - - mx_assert_return_minus_errno(stmt, EINVAL); - mx_assert_return_minus_errno(stmt->stmt, EBADF); - - error = mysql_stmt_errno(stmt->stmt); - mx_assert_return_minus_errno((unsigned int)(int)error == error, ERANGE); - - /* no mysql errors possible */ - return (int)error; +__attribute__ ((format (printf, 1, 2))) +static void mx_mysql_save_error_va(const char *restrict fmt, ...) { + char *s; + va_list ap; + va_start(ap, fmt); + int res = vasprintf(&s, fmt, ap); + va_end(ap); + if (res == -1) + return; + mx_mysql_save_error(s); + free(s); } -static const char *mx__mysql_stmt_error(struct mx_mysql_stmt *stmt) -{ - mx_assert_return_NULL(stmt, EINVAL); - mx_assert_return_NULL(stmt->stmt, EBADF); - - /* no mysql errors possible */ - return mysql_stmt_error(stmt->stmt); -} - -static const char *mx__mysql_stmt_sqlstate(struct mx_mysql_stmt *stmt) -{ - mx_assert_return_NULL(stmt, EINVAL); - mx_assert_return_NULL(stmt->stmt, EBADF); - - /* no mysql errors possible */ - return mysql_stmt_sqlstate(stmt->stmt); +char *mx_mysql_error(void) { + return mx_mysql_last_error == NULL ? "no error information" : mx_mysql_last_error; } static int mx__mysql_init(struct mx_mysql *mysql) { - mx_assert_return_minus_errno(mysql, EINVAL); - mx_assert_return_minus_errno(!mysql->mysql, EUCLEAN); + assert(mysql); + assert(!mysql->mysql); - mysql->func = "mysql_init"; mysql->mysql = mysql_init(NULL); if (mysql->mysql) return 0; - - errno = ENOMEM; - - mx_log_debug("Error: MySQL mysql_init(): %m"); - - return -errno; + mx_mysql_save_error("out of memory"); + return -ENOMEM; } static int mx__mysql_options(struct mx_mysql *mysql, enum mysql_option option, const void *arg) { int res; - mx_assert_return_minus_errno(mysql, EINVAL); - mx_assert_return_minus_errno(mysql->mysql, EBADF); + assert(mysql); + assert(mysql->mysql); - mysql->func = "mysql_options"; res = mysql_options(mysql->mysql, option, arg); if (res == 0) return 0; - - mx__mysql_log_emerg(mysql); - return -(errno=EBADE); + mx_mysql_save_error(mysql_error(mysql->mysql)); + return -EBADE; } static int mx__mysql_real_connect(struct mx_mysql *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag) { MYSQL *m; - mx_assert_return_minus_errno(mysql, EINVAL); - mx_assert_return_minus_errno(mysql->mysql, EBADF); + assert(mysql); + assert(mysql->mysql); - mysql->func = "mysql_real_connect"; m = mysql_real_connect(mysql->mysql, host, user, passwd, db, port, unix_socket, client_flag); - if (m && m == mysql->mysql) + if (m) return 0; - mx__mysql_log_warning(mysql); + mx_mysql_save_error(mysql_error(mysql->mysql)); - switch (mx__mysql_errno(mysql)) { + switch (mysql_errno(mysql->mysql)) { case CR_ALREADY_CONNECTED: - return -(errno=EALREADY); + return -EALREADY; case CR_OUT_OF_MEMORY: - return -(errno=ENOMEM); + return -ENOMEM; case CR_CONN_HOST_ERROR: case CR_CONNECTION_ERROR: @@ -176,42 +113,30 @@ static int mx__mysql_real_connect(struct mx_mysql *mysql, const char *host, cons case CR_VERSION_ERROR: case CR_SERVER_LOST: case CR_SERVER_GONE_ERROR: - return -(errno=EAGAIN); - - case CR_NAMEDPIPEOPEN_ERROR: - case CR_NAMEDPIPEWAIT_ERROR: - case CR_NAMEDPIPESETSTATE_ERROR: - mx__mysql_log_emerg(mysql); - mx_log_emerg("ERROR: mysql_real_connect() returned Windows error number: %d", mx__mysql_errno(mysql)); - return -(errno=EBADE); /* we are not on windows*/ + return -EAGAIN; } - - mx__mysql_log_emerg(mysql); - mx_log_emerg("ERROR: mysql_real_connect() returned undefined error number: %d", mx__mysql_errno(mysql)); - return -(errno=EBADE); + return -EBADE; } static int mx__mysql_ping(struct mx_mysql *mysql) { - mx_assert_return_minus_errno(mysql, EINVAL); - mx_assert_return_minus_errno(mysql->mysql, EBADF); + assert(mysql); + assert(mysql->mysql); int res; - mysql->func = "mysql_ping"; res = mysql_ping(mysql->mysql); if (res == 0) return 0; - mx__mysql_log_warning(mysql); + mx_mysql_save_error(mysql_error(mysql->mysql)); - switch (mx__mysql_errno(mysql)) { + switch (mysql_errno(mysql->mysql)) { case CR_COMMANDS_OUT_OF_SYNC: - mx__mysql_log_emerg(mysql); - return -(errno=EPROTO); + return -EPROTO; case CR_OUT_OF_MEMORY: - return -(errno=ENOMEM); + return -ENOMEM; case CR_CONN_HOST_ERROR: case CR_CONNECTION_ERROR: @@ -221,319 +146,268 @@ static int mx__mysql_ping(struct mx_mysql *mysql) case CR_VERSION_ERROR: case CR_SERVER_LOST: case CR_SERVER_GONE_ERROR: - return -(errno=EAGAIN); + return -EAGAIN; case CR_UNKNOWN_ERROR: - return -(errno=EIO); + return -EIO; } - - mx__mysql_log_emerg(mysql); - mx_log_emerg("ERROR: mysql_ping() returned undefined error number: %d", mx__mysql_errno(mysql)); - return -(errno=EBADE); + return -EBADE; } static int mx__mysql_stmt_init(struct mx_mysql_stmt *stmt) { - mx_assert_return_minus_errno(stmt, EINVAL); - mx_assert_return_minus_errno(!stmt->stmt, EUCLEAN); - mx_assert_return_minus_errno(stmt->mysql, EBADF); + assert(stmt); + assert(!stmt->stmt); + assert(stmt->mysql); - stmt->mysql->func = "mysql_stmt_init"; stmt->stmt = mysql_stmt_init(stmt->mysql->mysql); if (stmt->stmt) return 0; - - mx__mysql_log_warning(stmt->mysql); - - if (mx__mysql_errno(stmt->mysql) == CR_OUT_OF_MEMORY) - return -(errno=ENOMEM); - - mx__mysql_log_emerg(stmt->mysql); - mx_log_emerg("ERROR: mysql_stmt_init() returned undefined error number: %d", mx__mysql_errno(stmt->mysql)); - return -(errno=EBADE); + mx_mysql_save_error("out of memory"); + return -ENOMEM; } static int mx__mysql_stmt_prepare(struct mx_mysql_stmt *stmt, char *statement) { int res; - mx_assert_return_minus_errno(stmt, EINVAL); - mx_assert_return_minus_errno(statement, EINVAL); - mx_assert_return_minus_errno(*statement, EINVAL); + assert(stmt); + assert(statement); + assert(*statement); - mx_assert_return_minus_errno(!stmt->statement, EUCLEAN); + assert(!stmt->statement); - mx_assert_return_minus_errno(stmt->mysql, EBADF); - mx_assert_return_minus_errno(stmt->stmt, EBADF); + assert(stmt->mysql); + assert(stmt->stmt); - stmt->func = "mysql_stmt_prepare"; res = mysql_stmt_prepare(stmt->stmt, statement, strlen(statement)); if (res == 0) { stmt->statement = statement; return 0; } - mx__mysql_stmt_log_warning(stmt); + mx_mysql_save_error(mysql_stmt_error(stmt->stmt)); - switch (mx__mysql_stmt_errno(stmt)) { + switch (mysql_stmt_errno(stmt->stmt)) { case CR_OUT_OF_MEMORY: - return -(errno=ENOMEM); + return -ENOMEM; case CR_COMMANDS_OUT_OF_SYNC: - mx__mysql_stmt_log_emerg(stmt); - return -(errno=EPROTO); + return -EPROTO; case CR_SERVER_GONE_ERROR: case CR_SERVER_LOST: - return -(errno=EAGAIN); + return -EAGAIN; case CR_UNKNOWN_ERROR: - return -(errno=EIO); + return -EIO; case ER_PARSE_ERROR: - mx__mysql_stmt_log_emerg(stmt); - return -(errno=EBADRQC); + return -EBADRQC; } - - mx__mysql_stmt_log_emerg(stmt); - mx_log_emerg("ERROR: mysql_stmt_prepare() returned undefined error number: %d", mx__mysql_stmt_errno(stmt)); - return -(errno=EBADE); + return -EBADE; } static int mx__mysql_stmt_bind_param(struct mx_mysql_stmt *stmt) { int res; - mx_assert_return_minus_errno(stmt, EINVAL); - mx_assert_return_minus_errno(stmt->stmt, EBADF); + assert(stmt); + assert(stmt->stmt); - stmt->func = "mysql_stmt_bind_param"; res = (int)mysql_stmt_bind_param(stmt->stmt, stmt->param.bind); if (res == 0) return 0; - mx__mysql_stmt_log_warning(stmt); + mx_mysql_save_error(mysql_stmt_error(stmt->stmt)); - switch (mx__mysql_stmt_errno(stmt)) { + switch (mysql_stmt_errno(stmt->stmt)) { case CR_OUT_OF_MEMORY: - return -(errno=ENOMEM); + return -ENOMEM; case CR_UNSUPPORTED_PARAM_TYPE: - mx__mysql_stmt_log_emerg(stmt); - return -(errno=EPROTO); + return -EPROTO; case CR_UNKNOWN_ERROR: - return -(errno=EIO); + return -EIO; } - - mx__mysql_stmt_log_emerg(stmt); - mx_log_emerg("ERROR: mysql_stmt_bind_param() returned undefined error number: %d", mx__mysql_stmt_errno(stmt)); - return -(errno=EBADE); + return -EBADE; } static int mx__mysql_stmt_bind_result(struct mx_mysql_stmt *stmt) { int res; - mx_assert_return_minus_errno(stmt, EINVAL); - mx_assert_return_minus_errno(stmt->stmt, EBADF); + assert(stmt); + assert(stmt->stmt); - stmt->func = "mysql_stmt_bind_result"; res = (int)mysql_stmt_bind_result(stmt->stmt, stmt->result.bind); if (res == 0) return 0; - mx__mysql_stmt_log_warning(stmt); + mx_mysql_save_error(mysql_stmt_error(stmt->stmt)); - switch (mx__mysql_stmt_errno(stmt)) { + switch (mysql_stmt_errno(stmt->stmt)) { case CR_OUT_OF_MEMORY: - return -(errno=ENOMEM); + return -ENOMEM; case CR_UNSUPPORTED_PARAM_TYPE: - mx__mysql_stmt_log_emerg(stmt); - return -(errno=EPROTO); + return -EPROTO; case CR_UNKNOWN_ERROR: - return -(errno=EIO); + return -EIO; } - mx__mysql_stmt_log_emerg(stmt); - mx_log_emerg("ERROR: mysql_stmt_bind_result() returned undefined error number: %d", mx__mysql_stmt_errno(stmt)); - return -(errno=EBADE); + return -EBADE; } static int mx__mysql_stmt_execute(struct mx_mysql_stmt *stmt) { int res; - mx_assert_return_minus_errno(stmt, EINVAL); - mx_assert_return_minus_errno(stmt->stmt, EBADF); + assert(stmt); + assert(stmt->stmt); - stmt->func = "mysql_stmt_execute"; res = mysql_stmt_execute(stmt->stmt); if (res == 0) return 0; - mx__mysql_stmt_log_warning(stmt); + mx_mysql_save_error(mysql_stmt_error(stmt->stmt)); - switch (mx__mysql_stmt_errno(stmt)) { + switch (mysql_stmt_errno(stmt->stmt)) { case CR_COMMANDS_OUT_OF_SYNC: - mx__mysql_stmt_log_emerg(stmt); - return -(errno=EPROTO); + return -EPROTO; case CR_OUT_OF_MEMORY: - return -(errno=ENOMEM); + return -ENOMEM; case CR_SERVER_GONE_ERROR: case CR_SERVER_LOST: - return -(errno=EAGAIN); + return -EAGAIN; case CR_UNKNOWN_ERROR: - return -(errno=EIO); + return -EIO; } - mx__mysql_stmt_log_emerg(stmt); - mx_log_emerg("ERROR: mysql_stmt_execute() returned undefined error number: %d", mx__mysql_stmt_errno(stmt)); - return -(errno=EBADE); + return -EBADE; } static int mx__mysql_stmt_store_result(struct mx_mysql_stmt *stmt) { int res; - mx_assert_return_minus_errno(stmt, EINVAL); - mx_assert_return_minus_errno(stmt->stmt, EBADF); + assert(stmt); + assert(stmt->stmt); - stmt->func = "mysql_stmt_store_result"; res = mysql_stmt_store_result(stmt->stmt); if (res == 0) return 0; - mx__mysql_stmt_log_warning(stmt); + mx_mysql_save_error(mysql_stmt_error(stmt->stmt)); - switch (mx__mysql_stmt_errno(stmt)) { + switch (mysql_stmt_errno(stmt->stmt)) { case CR_COMMANDS_OUT_OF_SYNC: - mx__mysql_stmt_log_emerg(stmt); - return -(errno=EPROTO); + return -EPROTO; case CR_OUT_OF_MEMORY: - return -(errno=ENOMEM); + return -ENOMEM; case CR_SERVER_GONE_ERROR: case CR_SERVER_LOST: - return -(errno=EAGAIN); + return -EAGAIN; case CR_UNKNOWN_ERROR: - return -(errno=EIO); + return -EIO; } - - mx__mysql_stmt_log_emerg(stmt); - mx_log_emerg("ERROR: mysql_stmt_store_result() returned undefined error number: %d", mx__mysql_stmt_errno(stmt)); - return -(errno=EBADE); + return -EBADE; } static int mx__mysql_stmt_free_result(struct mx_mysql_stmt *stmt) { int res; - mx_assert_return_minus_errno(stmt, EINVAL); - mx_assert_return_minus_errno(stmt->stmt, EBADF); + assert(stmt); + assert(stmt->stmt); - stmt->func = "mysql_stmt_free_result"; res = (int)mysql_stmt_free_result(stmt->stmt); if (res == 0) return 0; - mx__mysql_stmt_log_emerg(stmt); - mx_log_emerg("ERROR: mysql_stmt_free_result() returned undefined error number: %d", mx__mysql_stmt_errno(stmt)); - return -(errno=EBADE); + mx_mysql_save_error(mysql_stmt_error(stmt->stmt)); + return -EBADE; } static int mx__mysql_stmt_fetch(struct mx_mysql_stmt *stmt) { int res; - mx_assert_return_minus_errno(stmt, EINVAL); - mx_assert_return_minus_errno(stmt->stmt, EBADF); + assert(stmt); + assert(stmt->stmt); - stmt->func = "mysql_stmt_fetch"; res = mysql_stmt_fetch(stmt->stmt); if (res == 0) return 0; if (res == 1) { - mx__mysql_stmt_log_warning(stmt); + mx_mysql_save_error(mysql_stmt_error(stmt->stmt)); - switch (mx__mysql_stmt_errno(stmt)) { + switch (mysql_stmt_errno(stmt->stmt)) { case CR_OUT_OF_MEMORY: - return -(errno=ENOMEM); + return -ENOMEM; case CR_SERVER_GONE_ERROR: case CR_SERVER_LOST: - return -(errno=EAGAIN); + return -EAGAIN; case CR_UNKNOWN_ERROR: - mx__mysql_stmt_log_emerg(stmt); - return -(errno=EIO); + return -EIO; case CR_COMMANDS_OUT_OF_SYNC: case CR_UNSUPPORTED_PARAM_TYPE: - mx__mysql_stmt_log_emerg(stmt); - return -(errno=EPROTO); + return -EPROTO; } - mx__mysql_stmt_log_emerg(stmt); - mx_log_emerg("ERROR: mysql_stmt_fetch() returned undefined error number: %d", mx__mysql_stmt_errno(stmt)); - return -(errno=EBADE); + return -EBADE; } switch (res) { case MYSQL_NO_DATA: - return -(errno=ENOENT); + return -ENOENT; case MYSQL_DATA_TRUNCATED: - return -(errno=ERANGE); + return -ERANGE; } - - mx__mysql_stmt_log_emerg(stmt); - mx_log_emerg("ERROR: mysql_stmt_fetch() returned undefined result code: %d", res); - return -(errno=EBADE); + return -EBADE; } static int mx__mysql_stmt_fetch_column(struct mx_mysql_stmt *stmt, unsigned int column, unsigned long offset) { int res; - mx_assert_return_minus_errno(stmt, EINVAL); - mx_assert_return_minus_errno(stmt->stmt, EBADF); + assert(stmt); + assert(stmt->stmt); - stmt->func = "mysql_stmt_fetch_column"; res = mysql_stmt_fetch_column(stmt->stmt, &(stmt->result.bind[column]), column, offset); if (res == 0) return 0; - mx__mysql_stmt_log_warning(stmt); + mx_mysql_save_error(mysql_stmt_error(stmt->stmt)); - switch (mx__mysql_stmt_errno(stmt)) { + switch (mysql_stmt_errno(stmt->stmt)) { case CR_INVALID_PARAMETER_NO: - mx__mysql_stmt_log_emerg(stmt); - return -(errno=EPROTO); + return -EPROTO; case CR_NO_DATA: - return -(errno=ENOENT); + return -ENOENT; } - - mx__mysql_stmt_log_emerg(stmt); - mx_log_emerg("ERROR: mysql_stmt_fetch_column() returned undefined error number: %d", mx__mysql_stmt_errno(stmt)); - return -(errno=EBADE); + return -EBADE; } static int mx__mysql_stmt_param_count(struct mx_mysql_stmt *stmt) { unsigned long count; - mx_assert_return_minus_errno(stmt, EINVAL); - mx_assert_return_minus_errno(stmt->stmt, EBADF); + assert(stmt); + assert(stmt->stmt); - stmt->func = "mysql_stmt_param_count"; count = mysql_stmt_param_count(stmt->stmt); - mx_assert_return_minus_errno((unsigned long)(int)count == count, ERANGE); + assert(count <= INT_MAX); /* no mysql errors possible */ return (int)count; @@ -543,12 +417,11 @@ static int mx__mysql_stmt_field_count(struct mx_mysql_stmt *stmt) { unsigned long count; - mx_assert_return_minus_errno(stmt, EINVAL); - mx_assert_return_minus_errno(stmt->stmt, EBADF); + assert(stmt); + assert(stmt->stmt); - stmt->func = "mysql_stmt_field_count"; count = mysql_stmt_field_count(stmt->stmt); - mx_assert_return_minus_errno((unsigned long)(int)count == count, ERANGE); + assert(count <= INT_MAX); /* no mysql errors possible */ return (int)count; @@ -558,10 +431,9 @@ static int mx__mysql_stmt_affected_rows(struct mx_mysql_stmt *stmt, unsigned lon { my_ulonglong c; - mx_assert_return_minus_errno(stmt, EINVAL); - mx_assert_return_minus_errno(stmt->stmt, EBADF); + assert(stmt); + assert(stmt->stmt); - stmt->func = "mysql_stmt_affected_rows"; c = mysql_stmt_affected_rows(stmt->stmt); *count = (unsigned long long)c; @@ -574,10 +446,9 @@ static int mx__mysql_stmt_insert_id(struct mx_mysql_stmt *stmt, unsigned long lo { my_ulonglong c; - mx_assert_return_minus_errno(stmt, EINVAL); - mx_assert_return_minus_errno(stmt->stmt, EBADF); + assert(stmt); + assert(stmt->stmt); - stmt->func = "mysql_stmt_insert_id"; c = mysql_stmt_insert_id(stmt->stmt); *count = (unsigned long long)c; @@ -590,35 +461,29 @@ static int mx__mysql_stmt_close(struct mx_mysql_stmt *stmt) { bool res; - mx_assert_return_minus_errno(stmt, EINVAL); + assert(stmt); - stmt->func = "mysql_stmt_close"; res = mysql_stmt_close(stmt->stmt); if (res == 0) { stmt->stmt = NULL; return 0; } + mx_mysql_save_error(mysql_stmt_error(stmt->stmt)); - mx__mysql_stmt_log_warning(stmt); - - switch (mx__mysql_stmt_errno(stmt)) { + switch (mysql_stmt_errno(stmt->stmt)) { case CR_SERVER_GONE_ERROR: - return -(errno=ECONNABORTED); + return -ECONNABORTED; case CR_UNKNOWN_ERROR : - return -(errno=EIO); + return -EIO; } - - mx__mysql_stmt_log_emerg(stmt); - mx_log_emerg("ERROR: mysql_stmt_close() returned undefined error number: %d", mx__mysql_stmt_errno(stmt)); - return -(errno=EBADE); + return -EBADE; } static int mx__mysql_close(struct mx_mysql *mysql) { - mx_assert_return_minus_errno(mysql, EINVAL); + assert(mysql); if (mysql->mysql) { - mysql->func = "mysql_close"; mysql_close(mysql->mysql); mysql->mysql = NULL; } @@ -637,12 +502,12 @@ static int mx__mysql_library_end(void) { static int _mx_mysql_bind_integer(struct mx_mysql_bind *b, unsigned int index, void *value, int type, int is_unsigned) { - mx_assert_return_minus_errno(b, EINVAL); - mx_assert_return_minus_errno(value, EINVAL); + assert(b); + assert(value); - mx_assert_return_minus_errno(index < b->count, ERANGE); + assert(index < b->count); - mx_assert_return_minus_errno(!(b->data[index].flags), EUCLEAN); + assert(!(b->data[index].flags)); memset(&(b->bind[index]), 0, sizeof(b->bind[index])); @@ -658,16 +523,16 @@ static int _mx_mysql_bind_integer(struct mx_mysql_bind *b, unsigned int index, v return 0; } -static int _mx_mysql_bind_string(struct mx_mysql_bind *b, unsigned int index, char **value) +void mx_mysql_bind_string(struct mx_mysql_bind *b, unsigned int index, char **value) { - mx_assert_return_minus_errno(b, EINVAL); - mx_assert_return_minus_errno(value, EINVAL); + assert(b); + assert(value); - mx_assert_return_minus_errno(index < b->count, ERANGE); + assert(index < b->count); - mx_assert_return_minus_errno(!(b->data[index].flags), EUCLEAN); + assert(!(b->data[index].flags)); - mx_assert_return_minus_errno((*value && b->type == MX_MYSQL_BIND_TYPE_PARAM) || (!*value && b->type == MX_MYSQL_BIND_TYPE_RESULT), EBADF); + assert((*value && b->type == MX_MYSQL_BIND_TYPE_PARAM) || (!*value && b->type == MX_MYSQL_BIND_TYPE_RESULT)); memset(&(b->bind[index]), 0, sizeof(b->bind[index])); @@ -694,16 +559,16 @@ static int _mx_mysql_bind_string(struct mx_mysql_bind *b, unsigned int index, ch } b->data[index].flags = 1; - return 0; } static int _mx_mysql_bind_validate(struct mx_mysql_bind *b) { - mx_assert_return_minus_errno(b, EINVAL); + assert(b); for (unsigned long i=0; i < b->count; i++) { if (!(b->data[i].flags)) { - return -(errno=EBADSLT); + mx_mysql_save_error("binding not fully initialized"); + return -EBADSLT; } } @@ -716,8 +581,8 @@ int mx_mysql_initialize(struct mx_mysql **mysql) { struct mx_mysql *m; - mx_assert_return_minus_errno(mysql, EINVAL); - mx_assert_return_minus_errno(!(*mysql), EUCLEAN); + assert(mysql); + assert(!(*mysql)); m = mx_calloc_forever(1, sizeof(*m)); @@ -730,7 +595,7 @@ static int mx_mysql_init(struct mx_mysql *mysql) { int res; - mx_assert_return_minus_errno(mysql, EINVAL); + assert(mysql); do { res = mx__mysql_init(mysql); @@ -740,7 +605,7 @@ static int mx_mysql_init(struct mx_mysql *mysql) if (res != -ENOMEM) return res; - mx_log_debug("mx__mysql_init() failed: %m - retrying (forever) in %d second(s).", MX_MYSQL_FAIL_WAIT_DEFAULT); + mx_log_debug("mx__mysql_init() failed: %s - retrying (forever) in %d second(s).", strerror(-res), MX_MYSQL_FAIL_WAIT_DEFAULT); mx_sleep(MX_MYSQL_FAIL_WAIT_DEFAULT); } while (1); @@ -750,11 +615,10 @@ static int mx_mysql_init(struct mx_mysql *mysql) int mx_mysql_option_set_default_file(struct mx_mysql *mysql, char *fname) { - mx_assert_return_minus_errno(mysql, EINVAL); + assert(mysql); if (fname && (*fname == '/') && (euidaccess(fname, R_OK) != 0)) { - mx_log_warning("MySQL ignoring defaults file: euidaccess(\"%s\", R_OK) failed: %m", fname); - mx_log_warning("MySQL falling back to mysql default config search path."); + mx_mysql_save_error_va("%s: %m - falling back to mysql default config search path", fname); return -errno; } @@ -768,7 +632,7 @@ int mx_mysql_option_set_default_file(struct mx_mysql *mysql, char *fname) int mx_mysql_option_set_default_group(struct mx_mysql *mysql, char *group) { - mx_assert_return_minus_errno(mysql, EINVAL); + assert(mysql); if (group && !(*group)) group = NULL; @@ -780,7 +644,7 @@ int mx_mysql_option_set_default_group(struct mx_mysql *mysql, char *group) int mx_mysql_option_set_reconnect(struct mx_mysql *mysql, int reconnect) { - mx_assert_return_minus_errno(mysql, EINVAL); + assert(mysql); mysql->reconnect = (bool)!!reconnect; return 0; @@ -790,8 +654,8 @@ static int mx_mysql_real_connect(struct mx_mysql *mysql, const char *host, const { int res; - mx_assert_return_minus_errno(mysql, EINVAL); - mx_assert_return_minus_errno(mysql->mysql, EBADF); + assert(mysql); + assert(mysql->mysql); if (mysql->default_file) { res = mx__mysql_options(mysql, MYSQL_READ_DEFAULT_FILE, mysql->default_file); @@ -815,7 +679,7 @@ static int mx_mysql_real_connect(struct mx_mysql *mysql, const char *host, const return 0; if (res == -EALREADY) { - mx_log_debug("WARNING: %s", mx__mysql_error(mysql)); + mx_log_debug("WARNING: %s", mysql_error(mysql->mysql)); return 0; } @@ -826,7 +690,7 @@ int mx_mysql_connect(struct mx_mysql **mysql) { int res; - mx_assert_return_minus_errno(mysql, EINVAL); + assert(mysql); if (!(*mysql)) { res = mx_mysql_initialize(mysql); @@ -848,9 +712,8 @@ int mx_mysql_connect_forever_sec(struct mx_mysql **mysql, unsigned int seconds) int res; while ((res = mx_mysql_connect(mysql)) < 0) { - mx__mysql_log_warning(*mysql); mx_mysql_assert_usage_ok(res); - mx_log_warning("mx_mysql_connect() failed: %m - retrying (forever) in %d second(s).", seconds); + mx_log_warning("mx_mysql_connect() failed: %s - retrying (forever) in %d second(s).", mx_mysql_error(), seconds); mx_sleep(seconds); } @@ -858,7 +721,7 @@ int mx_mysql_connect_forever_sec(struct mx_mysql **mysql, unsigned int seconds) } int mx_mysql_disconnect(struct mx_mysql *mysql) { - mx_assert_return_minus_errno(mysql, EINVAL); + assert(mysql); return mx__mysql_close(mysql); } @@ -869,9 +732,9 @@ static int mx_mysql_end(void) { static int mx_mysql_free(struct mx_mysql **mysql) { - mx_assert_return_minus_errno(mysql, EINVAL); - mx_assert_return_minus_errno(*mysql, EBADF); - mx_assert_return_minus_errno(!((*mysql)->mysql), EUCLEAN); + assert(mysql); + assert(*mysql); + assert(!((*mysql)->mysql)); mx_free_null(*mysql); @@ -902,7 +765,7 @@ int mx_mysql_finish(struct mx_mysql **mysql) static int mx_mysql_ping(struct mx_mysql *mysql) { - mx_assert_return_minus_errno(mysql, EINVAL); + assert(mysql); return mx__mysql_ping(mysql); } @@ -912,7 +775,7 @@ static int mx_mysql_ping_forever(struct mx_mysql *mysql) int res; int fail = 0; - mx_assert_return_minus_errno(mysql, EINVAL); + assert(mysql); while (1) { res = mx_mysql_ping(mysql); @@ -921,9 +784,8 @@ static int mx_mysql_ping_forever(struct mx_mysql *mysql) fail++; - mx__mysql_log_warning(mysql); mx_mysql_assert_usage_ok(res); - mx_log_warning("mx_mysql_ping() failed: %m - retrying again (forever) in %d second(s).", MX_MYSQL_FAIL_WAIT_DEFAULT); + mx_log_warning("mx_mysql_ping() failed: %s - retrying again (forever) in %d second(s).", mx_mysql_error(), MX_MYSQL_FAIL_WAIT_DEFAULT); mx_sleep(MX_MYSQL_FAIL_WAIT_DEFAULT); } @@ -938,14 +800,13 @@ static int mx_mysql_statement_init(struct mx_mysql *mysql, struct mx_mysql_stmt struct mx_mysql_stmt *s; int res; - mx_assert_return_minus_errno(stmt, EINVAL); - mx_assert_return_minus_errno(mysql, EINVAL); - mx_assert_return_minus_errno(!(*stmt), EUCLEAN); + assert(stmt); + assert(mysql); + assert(!(*stmt)); s = mx_calloc_forever(1, sizeof(*s)); s->mysql = mysql; - s->func = ""; do { res = mx__mysql_stmt_init(s); @@ -955,7 +816,7 @@ static int mx_mysql_statement_init(struct mx_mysql *mysql, struct mx_mysql_stmt if (res != -ENOMEM) return res; - mx_log_debug("mx__mysql_stmt_init() failed: %m - retrying (forever) in %d second(s).", MX_MYSQL_FAIL_WAIT_DEFAULT); + mx_log_debug("mx__mysql_stmt_init() failed: %s - retrying (forever) in %d second(s).", strerror(-res), MX_MYSQL_FAIL_WAIT_DEFAULT); mx_sleep(MX_MYSQL_FAIL_WAIT_DEFAULT); } while (1); @@ -968,37 +829,36 @@ int mx_mysql_statement_execute(struct mx_mysql_stmt *stmt, unsigned long long *c { int res; - mx_assert_return_minus_errno(stmt, EINVAL); - mx_assert_return_minus_errno(stmt->stmt, EBADF); + assert(stmt); + assert(stmt->stmt); res = _mx_mysql_bind_validate(&stmt->param); if (res < 0) { - mx_log_crit("MxSQL: parameter list for prepared statement not initialized completely."); return res; } res = mx__mysql_stmt_bind_param(stmt); if (res < 0) { - mx_log_debug("ERROR: mx__mysql_stmt_bind_param: %m"); + mx_log_debug("ERROR: mx__mysql_stmt_bind_param: %s", strerror(-res)); return res; } res = mx__mysql_stmt_execute(stmt); if (res < 0) { - mx_log_debug("ERROR: mx__mysql_stmt_execute: %m"); + mx_log_debug("ERROR: mx__mysql_stmt_execute: %s", strerror(-res)); return res; } res = mx__mysql_stmt_store_result(stmt); if (res < 0) { - mx_log_debug("ERROR: mx__mysql_stmt_store_result: %m"); + mx_log_debug("ERROR: mx__mysql_stmt_store_result: %s", strerror(-res)); return res; } if (count) { res = mx__mysql_stmt_affected_rows(stmt, count); if (res < 0) { - mx_log_debug("ERROR: mx__mysql_stmt_affected_rows(): %m"); + mx_log_debug("ERROR: mx__mysql_stmt_affected_rows(): %s", strerror(-res)); return res; } } @@ -1017,8 +877,8 @@ int mx_mysql_statement_fetch(struct mx_mysql_stmt *stmt) char *str; int no_error = 1; - mx_assert_return_minus_errno(stmt, EINVAL); - mx_assert_return_minus_errno(stmt->stmt, EBADF); + assert(stmt); + assert(stmt->stmt); res = _mx_mysql_bind_validate(&stmt->result); if (res < 0) { @@ -1028,7 +888,7 @@ int mx_mysql_statement_fetch(struct mx_mysql_stmt *stmt) res = mx__mysql_stmt_bind_result(stmt); if (res < 0) { - mx_log_debug("ERROR: mx__mysql_stmt_bind_result: %m"); + mx_log_debug("ERROR: mx__mysql_stmt_bind_result: %s", strerror(-res)); return res; } @@ -1037,7 +897,7 @@ int mx_mysql_statement_fetch(struct mx_mysql_stmt *stmt) return 0; if (res < 0 && res != -ERANGE) { - mx_log_debug("ERROR: mx__mysql_stmt_fetch: %m"); + mx_log_debug("ERROR: mx__mysql_stmt_fetch: %s", strerror(-res)); return res; } @@ -1067,28 +927,28 @@ int mx_mysql_statement_fetch(struct mx_mysql_stmt *stmt) } if (!no_error) - return -(errno=ERANGE); + return -ERANGE; return 0; } int mx_mysql_statement_param_count(struct mx_mysql_stmt *stmt) { - mx_assert_return_minus_errno(stmt, EINVAL); + assert(stmt); return mx__mysql_stmt_param_count(stmt); } int mx_mysql_statement_field_count(struct mx_mysql_stmt *stmt) { - mx_assert_return_minus_errno(stmt, EINVAL); + assert(stmt); return mx__mysql_stmt_field_count(stmt); } static int mx_mysql_stmt_field_count_set(struct mx_mysql_stmt *stmt) { - mx_assert_return_minus_errno(stmt, EINVAL); - mx_assert_return_minus_errno(stmt->stmt, EBADF); + assert(stmt); + assert(stmt->stmt); stmt->field_count = mysql_stmt_field_count(stmt->stmt); @@ -1097,8 +957,8 @@ static int mx_mysql_stmt_field_count_set(struct mx_mysql_stmt *stmt) static int mx_mysql_stmt_param_count_set(struct mx_mysql_stmt *stmt) { - mx_assert_return_minus_errno(stmt, EINVAL); - mx_assert_return_minus_errno(stmt->stmt, EBADF); + assert(stmt); + assert(stmt->stmt); stmt->param_count = mysql_stmt_param_count(stmt->stmt); @@ -1118,13 +978,13 @@ static int mx_mysql_bind_cleanup(struct mx_mysql_bind *bind) return 0; } -static int mx_mysql_bind_init_from(struct mx_mysql_bind *bind, unsigned long count, enum mx_mysql_bind_type type, struct mx_mysql_bind *from) +static void mx_mysql_bind_init_from(struct mx_mysql_bind *bind, unsigned long count, enum mx_mysql_bind_type type, struct mx_mysql_bind *from) { - mx_assert_return_minus_errno(bind, EINVAL); + assert(bind); - mx_assert_return_minus_errno(!bind->count, EUCLEAN); - mx_assert_return_minus_errno(!bind->bind, EUCLEAN); - mx_assert_return_minus_errno(!bind->data, EUCLEAN); + assert(!bind->count); + assert(!bind->bind); + assert(!bind->data); if (from) { assert(count == from->count); @@ -1132,30 +992,28 @@ static int mx_mysql_bind_init_from(struct mx_mysql_bind *bind, unsigned long cou assert(from->bind); assert(from->data); memcpy(bind, from, sizeof(*bind)); - return 0; + return; } - return mx_mysql_bind_init(bind, count, type); + mx_mysql_bind_init(bind, count, type); } -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) { - mx_assert_return_minus_errno(bind, EINVAL); + assert(bind); - mx_assert_return_minus_errno(!bind->count, EUCLEAN); - mx_assert_return_minus_errno(!bind->bind, EUCLEAN); - mx_assert_return_minus_errno(!bind->data, EUCLEAN); + assert(!bind->count); + assert(!bind->bind); + assert(!bind->data); bind->type = type; bind->count = count; if (!count) - return 0; + return; bind->bind = mx_calloc_forever(bind->count, sizeof(*bind->bind)); bind->data = mx_calloc_forever(bind->count, sizeof(*bind->data)); - - return 0; } static int _mx_mysql_do_statement(struct mx_mysql *mysql, char *query, struct mx_mysql_bind *param, struct mx_mysql_bind *result, void *from, void **to, size_t size, char cleanup) @@ -1171,23 +1029,20 @@ static int _mx_mysql_do_statement(struct mx_mysql *mysql, char *query, struct mx stmt = mx_mysql_statement_prepare_with_bindings(mysql, query, param, result); if (!stmt) { - mx_log_err("mx_mysql_statement_prepare_with_bindings(): %m"); - mx_log_err("mx_mysql_statement_prepare_with_bindings(): query was: %s", query); if (cleanup) { mx_mysql_bind_cleanup(param); mx_mysql_bind_cleanup(result); } - return -errno; + return -EINVAL; } res = mx_mysql_statement_execute(stmt, &num_rows); if (res < 0) { - mx_log_err("mx_mysql_statement_execute(): %m"); if (cleanup) mx_mysql_statement_close(&stmt); else mx_mysql_statement_close_no_bind_cleanup(&stmt); - return -(errno=-res); + return res; } if (result && result->count && num_rows) { @@ -1196,13 +1051,12 @@ static int _mx_mysql_do_statement(struct mx_mysql *mysql, char *query, struct mx for (unsigned long cnt = 0; cnt < num_rows; cnt++) { res = mx_mysql_statement_fetch(stmt); if (res < 0) { - mx_log_err("mx_mysql_statement_fetch(): %m"); mx_free_null(tmpdata); if (cleanup) mx_mysql_statement_close(&stmt); else mx_mysql_statement_close_no_bind_cleanup(&stmt); - return -(errno=-res); + return res; } memcpy(tmpdata+(cnt*size), from, size); } @@ -1238,7 +1092,7 @@ int mx_mysql_do_statement_retry_on_fail(struct mx_mysql *mysql, char *query, str mx_mysql_assert_usage_ok(res); - mx_log_warning("mx_mysql_do_statement() failed: %m"); + mx_log_warning("mx_mysql_do_statement() failed: %s", strerror(-res)); if (res != -EAGAIN) break; @@ -1257,9 +1111,9 @@ static struct mx_mysql_stmt *mx_mysql_statement_prepare_with_bindings(struct mx_ int res; struct mx_mysql_stmt *stmt = NULL; - mx_assert_return_NULL(mysql, EINVAL); - mx_assert_return_NULL(statement, EINVAL); - mx_assert_return_NULL(*statement, EINVAL); + assert(mysql); + assert(statement); + assert(*statement); res = mx_mysql_statement_init(mysql, &stmt); if (res < 0) @@ -1278,20 +1132,12 @@ static struct mx_mysql_stmt *mx_mysql_statement_prepare_with_bindings(struct mx_ if (res < 0) break; - res = mx_mysql_bind_init_from(&stmt->param, stmt->param_count, MX_MYSQL_BIND_TYPE_PARAM, param); - if (res < 0) - break; - - res = mx_mysql_bind_init_from(&stmt->result, stmt->field_count, MX_MYSQL_BIND_TYPE_RESULT, result); - if (res < 0) - break; + mx_mysql_bind_init_from(&stmt->param, stmt->param_count, MX_MYSQL_BIND_TYPE_PARAM, param); + mx_mysql_bind_init_from(&stmt->result, stmt->field_count, MX_MYSQL_BIND_TYPE_RESULT, result); return stmt; }; - if (res < 0) - mx__mysql_stmt_log_warning(stmt); - mx_mysql_statement_close_no_bind_cleanup(&stmt); return NULL; @@ -1299,17 +1145,17 @@ static struct mx_mysql_stmt *mx_mysql_statement_prepare_with_bindings(struct mx_ struct mx_mysql_stmt *mx_mysql_statement_prepare(struct mx_mysql *mysql, char *statement) { - mx_assert_return_NULL(mysql, EINVAL); - mx_assert_return_NULL(statement, EINVAL); - mx_assert_return_NULL(*statement, EINVAL); + assert(mysql); + assert(statement); + assert(*statement); return mx_mysql_statement_prepare_with_bindings(mysql, statement, NULL, NULL); } int mx_mysql_statement_close(struct mx_mysql_stmt **stmt) { - mx_assert_return_minus_errno(stmt, EINVAL); - mx_assert_return_minus_errno(*stmt, EINVAL); + assert(stmt); + assert(*stmt); mx__mysql_stmt_free_result(*stmt); mx__mysql_stmt_close(*stmt); @@ -1323,8 +1169,8 @@ int mx_mysql_statement_close(struct mx_mysql_stmt **stmt) int mx_mysql_statement_close_no_bind_cleanup(struct mx_mysql_stmt **stmt) { - mx_assert_return_minus_errno(stmt, EINVAL); - mx_assert_return_minus_errno(*stmt, EINVAL); + assert(stmt); + assert(*stmt); mx__mysql_stmt_free_result(*stmt); mx__mysql_stmt_close(*stmt); @@ -1342,59 +1188,46 @@ static int mx_mysql_bind_integer(struct mx_mysql_bind *b, unsigned int index, vo if (res == 0) return 0; - mx_log_debug("Failed to set index %d: %m", index); + mx_log_debug("Failed to set index %d: %s", index, strerror(-res)); return res; } -int mx_mysql_bind_string(struct mx_mysql_bind *b, unsigned int index, char **value) -{ - int res; - - res = _mx_mysql_bind_string(b, index, value); - if (res == 0) - return 0; - - mx_log_debug("Failed to set index %d: %m", index); - return res; - -} - -int mx_mysql_bind_int8(struct mx_mysql_bind *b, unsigned int index, int8_t *value) +void mx_mysql_bind_int8(struct mx_mysql_bind *b, unsigned int index, int8_t *value) { - return mx_mysql_bind_integer(b, index, (void *)value, MYSQL_TYPE_TINY, 0); + mx_mysql_bind_integer(b, index, (void *)value, MYSQL_TYPE_TINY, 0); } -int mx_mysql_bind_uint8(struct mx_mysql_bind *b, unsigned int index, uint8_t *value) +void mx_mysql_bind_uint8(struct mx_mysql_bind *b, unsigned int index, uint8_t *value) { - return mx_mysql_bind_integer(b, index, (void *)value, MYSQL_TYPE_TINY, 1); + mx_mysql_bind_integer(b, index, (void *)value, MYSQL_TYPE_TINY, 1); } -int mx_mysql_bind_int16(struct mx_mysql_bind *b, unsigned int index, int16_t *value) +void mx_mysql_bind_int16(struct mx_mysql_bind *b, unsigned int index, int16_t *value) { - return mx_mysql_bind_integer(b, index, (void *)value, MYSQL_TYPE_SHORT, 0); + mx_mysql_bind_integer(b, index, (void *)value, MYSQL_TYPE_SHORT, 0); } -int mx_mysql_bind_uint16(struct mx_mysql_bind *b, unsigned int index, uint16_t *value) +void mx_mysql_bind_uint16(struct mx_mysql_bind *b, unsigned int index, uint16_t *value) { - return mx_mysql_bind_integer(b, index, (void *)value, MYSQL_TYPE_SHORT, 1); + mx_mysql_bind_integer(b, index, (void *)value, MYSQL_TYPE_SHORT, 1); } -int mx_mysql_bind_int32(struct mx_mysql_bind *b, unsigned int index, int32_t *value) +void mx_mysql_bind_int32(struct mx_mysql_bind *b, unsigned int index, int32_t *value) { - return mx_mysql_bind_integer(b, index, (void *)value, MYSQL_TYPE_LONG, 0); + mx_mysql_bind_integer(b, index, (void *)value, MYSQL_TYPE_LONG, 0); } -int mx_mysql_bind_uint32(struct mx_mysql_bind *b, unsigned int index, uint32_t *value) +void mx_mysql_bind_uint32(struct mx_mysql_bind *b, unsigned int index, uint32_t *value) { - return mx_mysql_bind_integer(b, index, (void *)value, MYSQL_TYPE_LONG, 1); + mx_mysql_bind_integer(b, index, (void *)value, MYSQL_TYPE_LONG, 1); } -int mx_mysql_bind_int64(struct mx_mysql_bind *b, unsigned int index, int64_t *value) +void mx_mysql_bind_int64(struct mx_mysql_bind *b, unsigned int index, int64_t *value) { - return mx_mysql_bind_integer(b, index, (void *)value, MYSQL_TYPE_LONGLONG, 0); + mx_mysql_bind_integer(b, index, (void *)value, MYSQL_TYPE_LONGLONG, 0); } -int mx_mysql_bind_uint64(struct mx_mysql_bind *b, unsigned int index, uint64_t *value) +void mx_mysql_bind_uint64(struct mx_mysql_bind *b, unsigned int index, uint64_t *value) { - return mx_mysql_bind_integer(b, index, (void *)value, MYSQL_TYPE_LONGLONG, 1); + mx_mysql_bind_integer(b, index, (void *)value, MYSQL_TYPE_LONGLONG, 1); } diff --git a/mx_mysql.h b/mx_mysql.h index ae5f79e4..17f5a06b 100644 --- a/mx_mysql.h +++ b/mx_mysql.h @@ -6,41 +6,21 @@ #include "mx_util.h" -#ifdef MX_NDEBUG_MYSQL -# include -# 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; - - unsigned int saved_errno; - const char *error; - const char *sqlstate; }; struct mx_mysql_bind_data { @@ -74,8 +54,6 @@ struct mx_mysql_stmt { MYSQL_STMT *stmt; - const char *func; - unsigned long field_count; unsigned long param_count; @@ -135,18 +113,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 diff --git a/mx_proc.c b/mx_proc.c index 72330df4..9f486b2d 100644 --- a/mx_proc.c +++ b/mx_proc.c @@ -137,7 +137,7 @@ int mx_proc_pid_stat_read(struct mx_proc_pid_stat *pps, char *fmt, ...) assert(pps); va_start(ap, fmt); - _mx_cleanup_free_ char *fname = fname = mx_vasprintf_forever(fmt, ap); + _mx_cleanup_free_ char *fname = mx_vasprintf_forever(fmt, ap); va_end(ap); res = mx_read_first_line_from_file(fname, &line); diff --git a/mxq_daemon.c b/mxq_daemon.c index d0236887..ead607b5 100644 --- a/mxq_daemon.c +++ b/mxq_daemon.c @@ -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; } @@ -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(¶m, 1); - assert(res == 0); + mx_mysql_bind_init_param(¶m, 1); idx = 0; - res = 0; - res += mx_mysql_bind_var(¶m, idx++, uint32, &(daemon->daemon_id)); + mx_mysql_bind_var(¶m, idx++, uint32, &(daemon->daemon_id)); assert(res == 0); - res += mx_mysql_do_statement_noresult_retry_on_fail(mysql, query, ¶m); + res = mx_mysql_do_statement_noresult_retry_on_fail(mysql, query, ¶m); if (res < 0) { - mx_log_err("mx_mysql_do_statement(): %m"); + mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error()); return res; } @@ -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(¶m, 3); - assert(res == 0); + mx_mysql_bind_init_param(¶m, 3); idx = 0; - res = 0; - res += mx_mysql_bind_var(¶m, idx++, uint32, &daemon->daemon_id); - res += mx_mysql_bind_var(¶m, idx++, string, &daemon->hostname); - res += mx_mysql_bind_var(¶m, idx++, string, &daemon->daemon_name); - assert(res == 0); + mx_mysql_bind_var(¶m, idx++, uint32, &daemon->daemon_id); + mx_mysql_bind_var(¶m, idx++, string, &daemon->hostname); + mx_mysql_bind_var(¶m, idx++, string, &daemon->daemon_name); - res += mx_mysql_do_statement_noresult_retry_on_fail(mysql, query, ¶m); + res = mx_mysql_do_statement_noresult_retry_on_fail(mysql, query, ¶m); if (res < 0) { - mx_log_err("mx_mysql_do_statement(): %m"); + mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error()); return res; } @@ -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(¶m, 2); - assert(res == 0); + mx_mysql_bind_init_param(¶m, 2); idx = 0; - res = 0; - res += mx_mysql_bind_var(¶m, idx++, uint8, &(status)); - res += mx_mysql_bind_var(¶m, idx++, uint32, &(daemon->daemon_id)); - assert(res == 0); + mx_mysql_bind_var(¶m, idx++, uint8, &(status)); + mx_mysql_bind_var(¶m, idx++, uint32, &(daemon->daemon_id)); - res += mx_mysql_do_statement_noresult_retry_on_fail(mysql, query, ¶m); + res = mx_mysql_do_statement_noresult_retry_on_fail(mysql, query, ¶m); if (res < 0) { - mx_log_err("mx_mysql_do_statement(): %m"); + mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error()); return res; } @@ -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(¶m, 6); - assert(res == 0); + mx_mysql_bind_init_param(¶m, 6); idx = 0; - res = 0; - res += mx_mysql_bind_var(¶m, idx++, uint32, &(daemon->daemon_jobs_running)); - res += mx_mysql_bind_var(¶m, idx++, uint32, &(daemon->daemon_slots_running)); - res += mx_mysql_bind_var(¶m, idx++, uint32, &(daemon->daemon_threads_running)); - res += mx_mysql_bind_var(¶m, idx++, uint64, &(daemon->daemon_memory_used)); - res += mx_mysql_bind_var(¶m, idx++, uint16, &(daemon->gpus_used)); - res += mx_mysql_bind_var(¶m, idx++, uint32, &(daemon->daemon_id)); - assert(res == 0); + mx_mysql_bind_var(¶m, idx++, uint32, &(daemon->daemon_jobs_running)); + mx_mysql_bind_var(¶m, idx++, uint32, &(daemon->daemon_slots_running)); + mx_mysql_bind_var(¶m, idx++, uint32, &(daemon->daemon_threads_running)); + mx_mysql_bind_var(¶m, idx++, uint64, &(daemon->daemon_memory_used)); + mx_mysql_bind_var(¶m, idx++, uint16, &(daemon->gpus_used)); + mx_mysql_bind_var(¶m, idx++, uint32, &(daemon->daemon_id)); res = mx_mysql_do_statement_noresult_retry_on_fail(mysql, query, ¶m); if (res < 0) { - mx_log_err("mx_mysql_do_statement(): %m"); + mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error()); return res; } diff --git a/mxq_group.c b/mxq_group.c index 066eff38..d34a575a 100644 --- a/mxq_group.c +++ b/mxq_group.c @@ -11,7 +11,7 @@ #include "mx_util.h" #include "mx_mysql.h" -#define GROUP_FIELDS_CNT 38 +#define GROUP_FIELDS_CNT 36 #define GROUP_FIELDS \ " group_id," \ " group_name," \ @@ -40,9 +40,7 @@ " group_jobs_failed," \ " group_jobs_cancelled," \ " group_jobs_unknown," \ - " group_jobs_restarted," \ " group_slots_running," \ - " group_sum_starttime," \ " stats_max_sumrss," \ " stats_max_maxrss," \ " stats_max_utime_sec," \ @@ -53,63 +51,56 @@ " stats_idle_sec" -static int bind_result_group_fields(struct mx_mysql_bind *result, struct mxq_group *g) +static void bind_result_group_fields(struct mx_mysql_bind *result, struct mxq_group *g) { - int res = 0; int idx = 0; - res = mx_mysql_bind_init_result(result, GROUP_FIELDS_CNT); - assert(res >= 0); - - res += mx_mysql_bind_var(result, idx++, uint64, &(g->group_id)); - res += mx_mysql_bind_var(result, idx++, string, &(g->group_name)); - res += mx_mysql_bind_var(result, idx++, uint8, &(g->group_status)); - res += mx_mysql_bind_var(result, idx++, uint64, &(g->group_flags)); - res += mx_mysql_bind_var(result, idx++, uint16, &(g->group_priority)); - res += mx_mysql_bind_var(result, idx++, string, &(g->group_blacklist)); - res += mx_mysql_bind_var(result, idx++, string, &(g->group_whitelist)); - res += mx_mysql_bind_var(result, idx++, string, &(g->prerequisites)); - res += mx_mysql_bind_var(result, idx++, string, &(g->tags)); - - res += mx_mysql_bind_var(result, idx++, uint32, &(g->user_uid)); - res += mx_mysql_bind_var(result, idx++, string, &(g->user_name)); - res += mx_mysql_bind_var(result, idx++, uint32, &(g->user_gid)); - res += mx_mysql_bind_var(result, idx++, string, &(g->user_group)); - - res += mx_mysql_bind_var(result, idx++, string, &(g->job_command)); - - res += mx_mysql_bind_var(result, idx++, uint16, &(g->job_threads)); - res += mx_mysql_bind_var(result, idx++, uint64, &(g->job_memory)); - res += mx_mysql_bind_var(result, idx++, uint32, &(g->job_time)); - res += mx_mysql_bind_var(result, idx++, uint32, &(g->job_tmpdir_size)); - - res += mx_mysql_bind_var(result, idx++, uint16, &(g->job_max_per_node)); - res += mx_mysql_bind_var(result, idx++, uint16, &(g->job_gpu)); - - res += mx_mysql_bind_var(result, idx++, uint64, &(g->group_jobs)); - res += mx_mysql_bind_var(result, idx++, uint64, &(g->group_jobs_inq)); - res += mx_mysql_bind_var(result, idx++, uint64, &(g->group_jobs_running)); - res += mx_mysql_bind_var(result, idx++, uint64, &(g->group_jobs_finished)); - res += mx_mysql_bind_var(result, idx++, uint64, &(g->group_jobs_failed)); - res += mx_mysql_bind_var(result, idx++, uint64, &(g->group_jobs_cancelled)); - res += mx_mysql_bind_var(result, idx++, uint64, &(g->group_jobs_unknown)); - - res += mx_mysql_bind_var(result, idx++, uint64, &(g->group_jobs_restarted)); - - res += mx_mysql_bind_var(result, idx++, uint64, &(g->group_slots_running)); - res += mx_mysql_bind_var(result, idx++, uint64, &(g->group_sum_starttime)); - - res += mx_mysql_bind_var(result, idx++, uint64, &(g->stats_max_sumrss)); - res += mx_mysql_bind_var(result, idx++, uint64, &(g->stats_max_maxrss)); - res += mx_mysql_bind_var(result, idx++, int64, &(g->stats_max_utime.tv_sec)); - res += mx_mysql_bind_var(result, idx++, int64, &(g->stats_max_stime.tv_sec)); - res += mx_mysql_bind_var(result, idx++, int64, &(g->stats_max_real.tv_sec)); - - res += mx_mysql_bind_var(result, idx++, uint64, &(g->stats_wait_sec)); - res += mx_mysql_bind_var(result, idx++, uint64, &(g->stats_run_sec)); - res += mx_mysql_bind_var(result, idx++, uint64, &(g->stats_idle_sec)); - - return res; + mx_mysql_bind_init_result(result, GROUP_FIELDS_CNT); + + mx_mysql_bind_var(result, idx++, uint64, &(g->group_id)); + mx_mysql_bind_var(result, idx++, string, &(g->group_name)); + mx_mysql_bind_var(result, idx++, uint8, &(g->group_status)); + mx_mysql_bind_var(result, idx++, uint64, &(g->group_flags)); + mx_mysql_bind_var(result, idx++, uint16, &(g->group_priority)); + mx_mysql_bind_var(result, idx++, string, &(g->group_blacklist)); + mx_mysql_bind_var(result, idx++, string, &(g->group_whitelist)); + mx_mysql_bind_var(result, idx++, string, &(g->prerequisites)); + mx_mysql_bind_var(result, idx++, string, &(g->tags)); + + mx_mysql_bind_var(result, idx++, uint32, &(g->user_uid)); + mx_mysql_bind_var(result, idx++, string, &(g->user_name)); + mx_mysql_bind_var(result, idx++, uint32, &(g->user_gid)); + mx_mysql_bind_var(result, idx++, string, &(g->user_group)); + + mx_mysql_bind_var(result, idx++, string, &(g->job_command)); + + mx_mysql_bind_var(result, idx++, uint16, &(g->job_threads)); + mx_mysql_bind_var(result, idx++, uint64, &(g->job_memory)); + mx_mysql_bind_var(result, idx++, uint32, &(g->job_time)); + mx_mysql_bind_var(result, idx++, uint32, &(g->job_tmpdir_size)); + + mx_mysql_bind_var(result, idx++, uint16, &(g->job_max_per_node)); + mx_mysql_bind_var(result, idx++, uint16, &(g->job_gpu)); + + mx_mysql_bind_var(result, idx++, uint64, &(g->group_jobs)); + mx_mysql_bind_var(result, idx++, uint64, &(g->group_jobs_inq)); + mx_mysql_bind_var(result, idx++, uint64, &(g->group_jobs_running)); + mx_mysql_bind_var(result, idx++, uint64, &(g->group_jobs_finished)); + mx_mysql_bind_var(result, idx++, uint64, &(g->group_jobs_failed)); + mx_mysql_bind_var(result, idx++, uint64, &(g->group_jobs_cancelled)); + mx_mysql_bind_var(result, idx++, uint64, &(g->group_jobs_unknown)); + + mx_mysql_bind_var(result, idx++, uint64, &(g->group_slots_running)); + + mx_mysql_bind_var(result, idx++, uint64, &(g->stats_max_sumrss)); + mx_mysql_bind_var(result, idx++, uint64, &(g->stats_max_maxrss)); + mx_mysql_bind_var(result, idx++, int64, &(g->stats_max_utime.tv_sec)); + mx_mysql_bind_var(result, idx++, int64, &(g->stats_max_stime.tv_sec)); + mx_mysql_bind_var(result, idx++, int64, &(g->stats_max_real.tv_sec)); + + mx_mysql_bind_var(result, idx++, uint64, &(g->stats_wait_sec)); + mx_mysql_bind_var(result, idx++, uint64, &(g->stats_run_sec)); + mx_mysql_bind_var(result, idx++, uint64, &(g->stats_idle_sec)); } void mxq_group_free_content(struct mxq_group *g) @@ -184,17 +175,14 @@ int mxq_load_group(struct mx_mysql *mysql, struct mxq_group **mxq_groups, uint64 " WHERE group_id = ?" " LIMIT 1"; - res = mx_mysql_bind_init_param(¶m, 1); - assert(res == 0); - res = mx_mysql_bind_var(¶m, 0, uint64, &group_id); - assert(res == 0); + mx_mysql_bind_init_param(¶m, 1); + mx_mysql_bind_var(¶m, 0, uint64, &group_id); - res = bind_result_group_fields(&result, &g); - assert(res == 0); + bind_result_group_fields(&result, &g); res = mx_mysql_do_statement(mysql, query, ¶m, &result, &g, (void **)&groups, sizeof(*groups)); if (res < 0) { - mx_log_err("mx_mysql_do_statement(): %m"); + mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error()); return res; } @@ -219,12 +207,11 @@ int mxq_load_all_groups(struct mx_mysql *mysql, struct mxq_group **mxq_groups) " FROM mxq_group" " ORDER BY user_name, group_mtime"; - res = bind_result_group_fields(&result, &g); - assert(res == 0); + bind_result_group_fields(&result, &g); res = mx_mysql_do_statement(mysql, query, NULL, &result, &g, (void **)&groups, sizeof(*groups)); if (res < 0) { - mx_log_err("mx_mysql_do_statement(): %m"); + mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error()); return res; } @@ -251,17 +238,14 @@ int mxq_load_all_groups_for_user(struct mx_mysql *mysql, struct mxq_group **mxq_ " WHERE user_uid = ?" " ORDER BY user_name, group_mtime"; - res = mx_mysql_bind_init_param(¶m, 1); - assert(res == 0); - res = mx_mysql_bind_var(¶m, 0, uint64, &user_uid); - assert(res == 0); + mx_mysql_bind_init_param(¶m, 1); + mx_mysql_bind_var(¶m, 0, uint64, &user_uid); - res = bind_result_group_fields(&result, &g); - assert(res == 0); + bind_result_group_fields(&result, &g); res = mx_mysql_do_statement(mysql, query, ¶m, &result, &g, (void **)&groups, sizeof(*groups)); if (res < 0) { - mx_log_err("mx_mysql_do_statement(): %m"); + mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error()); return res; } @@ -290,17 +274,14 @@ int mxq_load_active_groups_for_user(struct mx_mysql *mysql, struct mxq_group **m " AND user_uid = ?" " ORDER BY user_name, group_mtime"; - res = mx_mysql_bind_init_param(¶m, 1); - assert(res == 0); - res = mx_mysql_bind_var(¶m, 0, uint64, &user_uid); - assert(res == 0); + mx_mysql_bind_init_param(¶m, 1); + mx_mysql_bind_var(¶m, 0, uint64, &user_uid); - res = bind_result_group_fields(&result, &g); - assert(res == 0); + bind_result_group_fields(&result, &g); res = mx_mysql_do_statement(mysql, query, ¶m, &result, &g, (void **)&groups, sizeof(*groups)); if (res < 0) { - mx_log_err("mx_mysql_do_statement(): %m"); + mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error()); return res; } @@ -326,12 +307,11 @@ int mxq_load_running_groups(struct mx_mysql *mysql, struct mxq_group **mxq_group " WHERE (group_jobs_inq > 0 OR group_jobs_running > 0)" " ORDER BY user_name, group_mtime"; - res = bind_result_group_fields(&result, &g); - assert(res == 0); + bind_result_group_fields(&result, &g); res = mx_mysql_do_statement(mysql, query, NULL, &result, &g, (void **)&groups, sizeof(*groups)); if (res < 0) { - mx_log_err("mx_mysql_do_statement(): %m"); + mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error()); return res; } @@ -359,17 +339,14 @@ int mxq_load_running_groups_for_user(struct mx_mysql *mysql, struct mxq_group ** " AND user_uid = ?" " ORDER BY user_name, group_mtime"; - res = mx_mysql_bind_init_param(¶m, 1); - assert(res == 0); - res = mx_mysql_bind_var(¶m, 0, uint64, &user_uid); - assert(res == 0); + mx_mysql_bind_init_param(¶m, 1); + mx_mysql_bind_var(¶m, 0, uint64, &user_uid); - res = bind_result_group_fields(&result, &g); - assert(res == 0); + bind_result_group_fields(&result, &g); res = mx_mysql_do_statement(mysql, query, ¶m, &result, &g, (void **)&groups, sizeof(*groups)); if (res < 0) { - mx_log_err("mx_mysql_do_statement(): %m"); + mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error()); return res; } diff --git a/mxq_group.h b/mxq_group.h index 5eb23b2e..d0f32663 100644 --- a/mxq_group.h +++ b/mxq_group.h @@ -43,10 +43,7 @@ struct mxq_group { uint64_t group_jobs_cancelled; uint64_t group_jobs_unknown; - uint64_t group_jobs_restarted; - uint64_t group_slots_running; - uint64_t group_sum_starttime; uint64_t stats_max_sumrss; uint64_t stats_max_maxrss; diff --git a/mxq_job.c b/mxq_job.c index ba8a606a..daf4061e 100644 --- a/mxq_job.c +++ b/mxq_job.c @@ -18,11 +18,10 @@ #include "mxq_group.h" #include "mxq_job.h" -#define JOB_FIELDS_CNT 38 +#define JOB_FIELDS_CNT 37 #define JOB_FIELDS \ " job_id, " \ " job_status, " \ - " job_flags, " \ " job_priority, " \ " group_id, " \ " job_workdir, " \ @@ -59,54 +58,49 @@ " stats_nvcsw, " \ " stats_nivcsw" -static int bind_result_job_fields(struct mx_mysql_bind *result, struct mxq_job *j) +static void bind_result_job_fields(struct mx_mysql_bind *result, struct mxq_job *j) { - int res = 0; int idx = 0; - res = mx_mysql_bind_init_result(result, JOB_FIELDS_CNT); - assert(res >= 0); - - res += mx_mysql_bind_var(result, idx++, uint64, &(j->job_id)); - res += mx_mysql_bind_var(result, idx++, uint16, &(j->job_status)); - res += mx_mysql_bind_var(result, idx++, uint64, &(j->job_flags)); - res += mx_mysql_bind_var(result, idx++, uint16, &(j->job_priority)); - res += mx_mysql_bind_var(result, idx++, uint64, &(j->group_id)); - res += mx_mysql_bind_var(result, idx++, string, &(j->job_workdir)); - res += mx_mysql_bind_var(result, idx++, uint32, &(j->job_argc)); - res += mx_mysql_bind_var(result, idx++, string, &(j->job_argv_str)); - res += mx_mysql_bind_var(result, idx++, string, &(j->job_stdout)); - res += mx_mysql_bind_var(result, idx++, string, &(j->job_stderr)); - res += mx_mysql_bind_var(result, idx++, uint32, &(j->job_umask)); - res += mx_mysql_bind_var(result, idx++, string, &(j->host_submit)); - res += mx_mysql_bind_var(result, idx++, string, &(j->host_id)); - res += mx_mysql_bind_var(result, idx++, uint32, &(j->daemon_id)); - res += mx_mysql_bind_var(result, idx++, string, &(j->daemon_name)); - res += mx_mysql_bind_var(result, idx++, string, &(j->host_hostname)); - res += mx_mysql_bind_var(result, idx++, uint32, &(j->host_pid)); - res += mx_mysql_bind_var(result, idx++, uint32, &(j->host_slots)); - res += mx_mysql_bind_var(result, idx++, string, &(j->host_cpu_set_str)); - res += mx_mysql_bind_var(result, idx++, int64, &(j->date_submit)); - res += mx_mysql_bind_var(result, idx++, int64, &(j->date_start)); - res += mx_mysql_bind_var(result, idx++, int64, &(j->date_end)); - res += mx_mysql_bind_var(result, idx++, uint64, &(j->stats_max_sumrss)); - res += mx_mysql_bind_var(result, idx++, int32, &(j->stats_status)); - res += mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_utime.tv_sec)); - res += mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_utime.tv_usec)); - res += mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_stime.tv_sec)); - res += mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_stime.tv_usec)); - res += mx_mysql_bind_var(result, idx++, int64, &(j->stats_realtime.tv_sec)); - res += mx_mysql_bind_var(result, idx++, int64, &(j->stats_realtime.tv_usec)); - res += mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_maxrss)); - res += mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_minflt)); - res += mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_majflt)); - res += mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_nswap)); - res += mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_inblock)); - res += mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_oublock)); - res += mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_nvcsw)); - res += mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_nivcsw)); - - return res; + mx_mysql_bind_init_result(result, JOB_FIELDS_CNT); + + mx_mysql_bind_var(result, idx++, uint64, &(j->job_id)); + mx_mysql_bind_var(result, idx++, uint16, &(j->job_status)); + mx_mysql_bind_var(result, idx++, uint16, &(j->job_priority)); + mx_mysql_bind_var(result, idx++, uint64, &(j->group_id)); + mx_mysql_bind_var(result, idx++, string, &(j->job_workdir)); + mx_mysql_bind_var(result, idx++, uint32, &(j->job_argc)); + mx_mysql_bind_var(result, idx++, string, &(j->job_argv_str)); + mx_mysql_bind_var(result, idx++, string, &(j->job_stdout)); + mx_mysql_bind_var(result, idx++, string, &(j->job_stderr)); + mx_mysql_bind_var(result, idx++, uint32, &(j->job_umask)); + mx_mysql_bind_var(result, idx++, string, &(j->host_submit)); + mx_mysql_bind_var(result, idx++, string, &(j->host_id)); + mx_mysql_bind_var(result, idx++, uint32, &(j->daemon_id)); + mx_mysql_bind_var(result, idx++, string, &(j->daemon_name)); + mx_mysql_bind_var(result, idx++, string, &(j->host_hostname)); + mx_mysql_bind_var(result, idx++, uint32, &(j->host_pid)); + mx_mysql_bind_var(result, idx++, uint32, &(j->host_slots)); + mx_mysql_bind_var(result, idx++, string, &(j->host_cpu_set_str)); + mx_mysql_bind_var(result, idx++, int64, &(j->date_submit)); + mx_mysql_bind_var(result, idx++, int64, &(j->date_start)); + mx_mysql_bind_var(result, idx++, int64, &(j->date_end)); + mx_mysql_bind_var(result, idx++, uint64, &(j->stats_max_sumrss)); + mx_mysql_bind_var(result, idx++, int32, &(j->stats_status)); + mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_utime.tv_sec)); + mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_utime.tv_usec)); + mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_stime.tv_sec)); + mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_stime.tv_usec)); + mx_mysql_bind_var(result, idx++, int64, &(j->stats_realtime.tv_sec)); + mx_mysql_bind_var(result, idx++, int64, &(j->stats_realtime.tv_usec)); + mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_maxrss)); + mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_minflt)); + mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_majflt)); + mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_nswap)); + mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_inblock)); + mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_oublock)); + mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_nvcsw)); + mx_mysql_bind_var(result, idx++, int64, &(j->stats_rusage.ru_nivcsw)); } char *mxq_job_status_to_name(uint64_t status) @@ -126,8 +120,6 @@ char *mxq_job_status_to_name(uint64_t status) return "failed"; case MXQ_JOB_STATUS_CANCELLED: return "cancelled"; - case MXQ_JOB_STATUS_CANCELLING: - return "cancelling"; case MXQ_JOB_STATUS_UNKNOWN: return "unknown"; case MXQ_JOB_STATUS_FINISHED: @@ -164,12 +156,11 @@ static int do_jobs_statement(struct mx_mysql *mysql, char *query, struct mx_mysq struct mxq_job j = {0}; struct mx_mysql_bind result = {0}; - res = bind_result_job_fields(&result, &j); - assert(res == 0); + bind_result_job_fields(&result, &j); res = mx_mysql_do_statement(mysql, query, param, &result, &j, (void **)jobs, sizeof(**jobs)); if (res < 0) { - mx_log_err("mx_mysql_do_statement(): %m"); + mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error()); return res; } for (i=0;i= 0) @@ -228,11 +216,9 @@ int mxq_load_jobs_in_group(struct mx_mysql *mysql, struct mxq_job **jobs_result, " WHERE group_id = ? OR 1 = 0" " ORDER BY server_id, host_hostname, job_id"; - res = mx_mysql_bind_init_param(¶m, 1); - assert(res == 0); + mx_mysql_bind_init_param(¶m, 1); - res = mx_mysql_bind_var(¶m, 0, uint64, &(grp->group_id)); - assert(res == 0); + mx_mysql_bind_var(¶m, 0, uint64, &(grp->group_id)); res = do_jobs_statement(mysql, query, ¶m, &jobs_tmp); if (res >= 0) @@ -263,14 +249,11 @@ int mxq_load_jobs_in_group_with_status(struct mx_mysql *mysql, struct mxq_job ** " host_hostname," " job_id"; - res = mx_mysql_bind_init_param(¶m, 2); - assert(res == 0); + mx_mysql_bind_init_param(¶m, 2); idx = 0; - res = 0; - res += mx_mysql_bind_var(¶m, idx++, uint64, &(grp->group_id)); - res += mx_mysql_bind_var(¶m, idx++, uint64, &job_status); - assert(res == 0); + mx_mysql_bind_var(¶m, idx++, uint64, &(grp->group_id)); + mx_mysql_bind_var(¶m, idx++, uint64, &job_status); res = do_jobs_statement(mysql, query, ¶m, &jobs_tmp); if (res >= 0) @@ -303,17 +286,13 @@ static uint64_t mxq_select_job_from_group(struct mx_mysql *mysql, uint64_t group " job_id" " LIMIT 1"; - res = mx_mysql_bind_init_param(¶m, 1); - assert(res==0); + mx_mysql_bind_init_param(¶m, 1); - res += mx_mysql_bind_var(¶m,0, uint64, &group_id); - assert(res==0); + mx_mysql_bind_var(¶m,0, uint64, &group_id); - res = mx_mysql_bind_init_result(&result, 1); - assert(res==0); + mx_mysql_bind_init_result(&result, 1); - res += mx_mysql_bind_var(&result,0,uint64,&job_id); - assert(res==0); + mx_mysql_bind_var(&result,0,uint64,&job_id); res = mx_mysql_do_statement(mysql, query, ¶m, &result, &job_id, (void **)&job_id_out,sizeof(*job_id_out)); if (res==1) { @@ -358,21 +337,18 @@ static int mxq_assign_job_from_group_to_daemon(struct mx_mysql *mysql, uint64_t return(0); } - res = mx_mysql_bind_init_param(¶m, 5); - assert(res == 0); + mx_mysql_bind_init_param(¶m, 5); idx = 0; - res = 0; - res += mx_mysql_bind_var(¶m, idx++, uint32, &daemon->daemon_id); - res += mx_mysql_bind_var(¶m, idx++, string, &daemon->hostname); - res += mx_mysql_bind_var(¶m, idx++, uint64, &slots_per_job); - res += mx_mysql_bind_var(¶m, idx++, string, &daemon->daemon_name); - res += mx_mysql_bind_var(¶m, idx++, uint64, &job_id); - assert(res == 0); + mx_mysql_bind_var(¶m, idx++, uint32, &daemon->daemon_id); + mx_mysql_bind_var(¶m, idx++, string, &daemon->hostname); + mx_mysql_bind_var(¶m, idx++, uint64, &slots_per_job); + mx_mysql_bind_var(¶m, idx++, string, &daemon->daemon_name); + mx_mysql_bind_var(¶m, idx++, uint64, &job_id); res = mx_mysql_do_statement_noresult(mysql, query, ¶m); if (res < 0) - mx_log_err("mx_mysql_do_statement(): %m"); + mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error()); return res; } @@ -405,18 +381,15 @@ int mxq_unassign_jobs_of_server(struct mx_mysql *mysql, struct mxq_daemon *daemo " AND host_hostname = ?" " AND server_id = ?"; - res = mx_mysql_bind_init_param(¶m, 2); - assert(res == 0); + mx_mysql_bind_init_param(¶m, 2); idx = 0; - res = 0; - res += mx_mysql_bind_var(¶m, idx++, string, &daemon->hostname); - res += mx_mysql_bind_var(¶m, idx++, string, &daemon->daemon_name); - assert(res == 0); + mx_mysql_bind_var(¶m, idx++, string, &daemon->hostname); + mx_mysql_bind_var(¶m, idx++, string, &daemon->daemon_name); res = mx_mysql_do_statement_noresult_retry_on_fail(mysql, query, ¶m); if (res < 0) { - mx_log_err("mx_mysql_do_statement(): %m"); + mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error()); return res; } @@ -447,19 +420,16 @@ static int mxq_set_job_status_loaded_on_server(struct mx_mysql *mysql, struct mx " AND daemon_id = ?" " AND host_pid = 0"; - res = mx_mysql_bind_init_param(¶m, 3); - assert(res == 0); + mx_mysql_bind_init_param(¶m, 3); idx = 0; - res = 0; - res += mx_mysql_bind_var(¶m, idx++, string, &host_id); - res += mx_mysql_bind_var(¶m, idx++, uint64, &(job->job_id)); - res += mx_mysql_bind_var(¶m, idx++, uint32, &(job->daemon_id)); - assert(res == 0); + mx_mysql_bind_var(¶m, idx++, string, &host_id); + mx_mysql_bind_var(¶m, idx++, uint64, &(job->job_id)); + mx_mysql_bind_var(¶m, idx++, uint32, &(job->daemon_id)); res = mx_mysql_do_statement_noresult_retry_on_fail(mysql, query, ¶m); if (res < 0) { - mx_log_err("mx_mysql_do_statement(): %m"); + mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error()); return res; } @@ -501,22 +471,19 @@ int mxq_set_job_status_running(struct mx_mysql *mysql, struct mxq_job *job) " AND daemon_id = ?" " AND host_pid = 0"; - res = mx_mysql_bind_init_param(¶m, 5); - assert(res == 0); + mx_mysql_bind_init_param(¶m, 5); idx = 0; - res = 0; - res += mx_mysql_bind_var(¶m, idx++, uint32, &(job->host_pid)); - res += mx_mysql_bind_var(¶m, idx++, uint32, &(job->host_slots)); - res += mx_mysql_bind_var(¶m, idx++, string, &(job->host_cpu_set_str)); + mx_mysql_bind_var(¶m, idx++, uint32, &(job->host_pid)); + mx_mysql_bind_var(¶m, idx++, uint32, &(job->host_slots)); + mx_mysql_bind_var(¶m, idx++, string, &(job->host_cpu_set_str)); - res += mx_mysql_bind_var(¶m, idx++, uint64, &(job->job_id)); - res += mx_mysql_bind_var(¶m, idx++, uint32, &(job->daemon_id)); - assert(res == 0); + mx_mysql_bind_var(¶m, idx++, uint64, &(job->job_id)); + mx_mysql_bind_var(¶m, idx++, uint32, &(job->daemon_id)); res = mx_mysql_do_statement_noresult_retry_on_fail(mysql, query, ¶m); if (res < 0) { - mx_log_err("mx_mysql_do_statement(): %m"); + mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error()); return res; } @@ -579,36 +546,33 @@ int mxq_set_job_status_exited(struct mx_mysql *mysql, struct mxq_job *job) " AND daemon_id = ?" " AND host_pid = ?"; - res = mx_mysql_bind_init_param(¶m, 20); - assert(res == 0); + mx_mysql_bind_init_param(¶m, 20); idx = 0; - res = 0; - res += mx_mysql_bind_var(¶m, idx++, uint64, &(job->stats_max_sumrss)); - res += mx_mysql_bind_var(¶m, idx++, int32, &(job->stats_status)); - res += mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_utime.tv_sec)); - res += mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_utime.tv_usec)); - res += mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_stime.tv_sec)); - res += mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_stime.tv_usec)); - res += mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_realtime.tv_sec)); - res += mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_realtime.tv_usec)); - res += mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_maxrss)); - res += mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_minflt)); - res += mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_majflt)); - res += mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_nswap)); - res += mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_inblock)); - res += mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_oublock)); - res += mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_nvcsw)); - res += mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_nivcsw)); - res += mx_mysql_bind_var(¶m, idx++, uint16, &(newstatus)); - res += mx_mysql_bind_var(¶m, idx++, uint64, &(job->job_id)); - res += mx_mysql_bind_var(¶m, idx++, uint32, &(job->daemon_id)); - res += mx_mysql_bind_var(¶m, idx++, uint32, &(job->host_pid)); - assert(res == 0); + mx_mysql_bind_var(¶m, idx++, uint64, &(job->stats_max_sumrss)); + mx_mysql_bind_var(¶m, idx++, int32, &(job->stats_status)); + mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_utime.tv_sec)); + mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_utime.tv_usec)); + mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_stime.tv_sec)); + mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_stime.tv_usec)); + mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_realtime.tv_sec)); + mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_realtime.tv_usec)); + mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_maxrss)); + mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_minflt)); + mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_majflt)); + mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_nswap)); + mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_inblock)); + mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_oublock)); + mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_nvcsw)); + mx_mysql_bind_var(¶m, idx++, int64, &(job->stats_rusage.ru_nivcsw)); + mx_mysql_bind_var(¶m, idx++, uint16, &(newstatus)); + mx_mysql_bind_var(¶m, idx++, uint64, &(job->job_id)); + mx_mysql_bind_var(¶m, idx++, uint32, &(job->daemon_id)); + mx_mysql_bind_var(¶m, idx++, uint32, &(job->host_pid)); res = mx_mysql_do_statement_noresult_retry_on_fail(mysql, query, ¶m); if (res < 0) { - mx_log_err("mx_mysql_do_statement(): %m"); + mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error()); return res; } @@ -630,17 +594,14 @@ int mxq_set_job_status_unknown(struct mx_mysql *mysql, struct mxq_job *job) " job_status = " status_str(MXQ_JOB_STATUS_UNKNOWN) " WHERE job_id = ?"; - res = mx_mysql_bind_init_param(¶m, 1); - assert(res == 0); + mx_mysql_bind_init_param(¶m, 1); idx = 0; - res = 0; - res += mx_mysql_bind_var(¶m, idx++, uint64, &job->job_id); - assert(res == 0); + mx_mysql_bind_var(¶m, idx++, uint64, &job->job_id); - res += mx_mysql_do_statement_noresult_retry_on_fail(mysql, query, ¶m); + res = mx_mysql_do_statement_noresult_retry_on_fail(mysql, query, ¶m); if (res < 0) { - mx_log_err("mx_mysql_do_statement(): %m"); + mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error()); return res; } @@ -698,14 +659,11 @@ static int mxq_load_job_from_group_assigned_to_daemon(struct mx_mysql *mysql, st " AND daemon_id = ?" " LIMIT 1"; - res = mx_mysql_bind_init_param(¶m, 2); - assert(res == 0); + mx_mysql_bind_init_param(¶m, 2); idx = 0; - res = 0; - res += mx_mysql_bind_var(¶m, idx++, uint64, &group_id); - res += mx_mysql_bind_var(¶m, idx++, uint32, &daemon->daemon_id); - assert(res == 0); + mx_mysql_bind_var(¶m, idx++, uint64, &group_id); + mx_mysql_bind_var(¶m, idx++, uint32, &daemon->daemon_id); res = do_jobs_statement(mysql, query, ¶m, &jobs_tmp); if (res >= 0) @@ -728,7 +686,6 @@ int mxq_load_job_from_group_for_daemon(struct mx_mysql *mysql, struct mxq_job *j res = mxq_load_job_from_group_assigned_to_daemon(mysql, &jobs_tmp, group_id, daemon); if(res < 0) { - mx_log_err(" group_id=%lu :: mxq_load_job_from_group_assigned_to_daemon: %m", group_id); return 0; } if(res == 1) { @@ -739,7 +696,6 @@ int mxq_load_job_from_group_for_daemon(struct mx_mysql *mysql, struct mxq_job *j res = mxq_assign_job_from_group_to_daemon(mysql, group_id, daemon, slots_per_job); if (res < 0) { - mx_log_err(" group_id=%lu :: mxq_assign_job_from_group_to_daemon(): %m", group_id); return 0; } if (res == 0) { @@ -750,7 +706,6 @@ int mxq_load_job_from_group_for_daemon(struct mx_mysql *mysql, struct mxq_job *j res = mxq_set_job_status_loaded_on_server(mysql, job); if (res < 0) { - mx_log_err(" group_id=%lu job_id=%lu :: mxq_set_job_status_loaded_on_server(): %m", group_id, job->job_id); return 0; } if (res == 0) { @@ -786,14 +741,11 @@ int mxq_load_jobs_running_on_server(struct mx_mysql *mysql, struct mxq_job **job status_str(MXQ_JOB_STATUS_RUNNING) ")" " AND host_hostname = ?" " AND server_id = ?"; - res = mx_mysql_bind_init_param(¶m, 2); - assert(res == 0); + mx_mysql_bind_init_param(¶m, 2); idx = 0; - res = 0; - res += mx_mysql_bind_var(¶m, idx++, string, &daemon->hostname); - res += mx_mysql_bind_var(¶m, idx++, string, &daemon->daemon_name); - assert(res == 0); + mx_mysql_bind_var(¶m, idx++, string, &daemon->hostname); + mx_mysql_bind_var(¶m, idx++, string, &daemon->daemon_name); res=do_jobs_statement(mysql, query, ¶m, &jobs_tmp); if (res >= 0) @@ -837,7 +789,7 @@ int mxq_unload_job_from_server(struct mx_mysql *mysql, uint64_t job_id) { res = mx_mysql_do_statement_noresult_retry_on_fail(mysql, query, ¶m); if (res < 0) { - mx_log_err("mx_mysql_do_statement(): %m"); + mx_log_err("mx_mysql_do_statement(): %s", mx_mysql_error()); return res; } return res; diff --git a/mxq_job.h b/mxq_job.h index 15699b65..d73561cb 100644 --- a/mxq_job.h +++ b/mxq_job.h @@ -15,7 +15,6 @@ struct mxq_job { uint64_t job_id; uint16_t job_status; - uint64_t job_flags; uint16_t job_priority; uint64_t group_id; @@ -70,8 +69,6 @@ struct mxq_job { #define MXQ_JOB_STATUS_KILLED 400 #define MXQ_JOB_STATUS_FAILED 750 -#define MXQ_JOB_STATUS_CANCELLING 989 - #define MXQ_JOB_STATUS_CANCELLED 990 #define MXQ_JOB_STATUS_UNKNOWN 999 #define MXQ_JOB_STATUS_FINISHED 1000 diff --git a/mxqadmin.c b/mxqadmin.c index 713f209f..c4746f3c 100644 --- a/mxqadmin.c +++ b/mxqadmin.c @@ -89,19 +89,18 @@ static int update_group_flags_closed(struct mx_mysql *mysql, uint64_t group_id, " AND user_uid = ?" ); if (!stmt) { - mx_log_err("mx_mysql_statement_prepare(): %m"); + mx_log_err("mx_mysql_statement_prepare(): %s", mx_mysql_error()); return -(errno=EIO); } - res = mx_mysql_statement_param_bind(stmt, 0, uint64, &(newflags)); - res += mx_mysql_statement_param_bind(stmt, 1, uint64, &(group_id)); - res += mx_mysql_statement_param_bind(stmt, 2, uint32, &(user_uid)); - assert(res == 0); + mx_mysql_statement_param_bind(stmt, 0, uint64, &(newflags)); + mx_mysql_statement_param_bind(stmt, 1, uint64, &(group_id)); + mx_mysql_statement_param_bind(stmt, 2, uint32, &(user_uid)); 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); @@ -128,19 +127,18 @@ static int update_group_flags_reopen(struct mx_mysql *mysql, uint64_t group_id, " AND user_uid = ?" ); if (!stmt) { - mx_log_err("mx_mysql_statement_prepare(): %m"); + mx_log_err("mx_mysql_statement_prepare(): %s", mx_mysql_error()); return -(errno=EIO); } - res = mx_mysql_statement_param_bind(stmt, 0, uint64, &(newflags)); - res += mx_mysql_statement_param_bind(stmt, 1, uint64, &(group_id)); - res += mx_mysql_statement_param_bind(stmt, 2, uint32, &(user_uid)); - assert(res == 0); + mx_mysql_statement_param_bind(stmt, 0, uint64, &(newflags)); + mx_mysql_statement_param_bind(stmt, 1, uint64, &(group_id)); + mx_mysql_statement_param_bind(stmt, 2, uint32, &(user_uid)); 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); @@ -164,7 +162,7 @@ int _close_group_for_user(struct mx_mysql *mysql, uint64_t group_id, uint64_t us } if (res < 0) { - mx_log_err("closing group failed: %m"); + mx_log_err("closing group failed"); return res; } @@ -187,7 +185,7 @@ int _reopen_group_for_user(struct mx_mysql *mysql, uint64_t group_id, uint64_t u } if (res < 0) { - mx_log_err("opening group failed: %m"); + mx_log_err("opening group failed"); return res; } diff --git a/mxqd.c b/mxqd.c index 021bf786..4c65a2a9 100644 --- a/mxqd.c +++ b/mxqd.c @@ -1809,6 +1809,60 @@ static int killall_cancelled(struct ppidcache *ppidcache, struct mxq_server *ser return 0; } +static void kill_by_jobid(struct ppidcache *ppidcache, struct mxq_server *server, unsigned long job_id) { + for (struct mxq_user_list *ulist = server->users ; ulist ; ulist = ulist->next) + for (struct mxq_group_list *glist = ulist->groups ; glist ; glist = glist->next) + for (struct mxq_job_list *jlist = glist->jobs; jlist ; jlist = jlist->next) + if (jlist->job.job_id == job_id) { + killstate_event(ppidcache, jlist, KILLEVENT_CANCEL); + return; + } +} + +static void kill_cancelled_jobs(struct ppidcache *ppidcache, struct mxq_server *server) { + struct mx_mysql *mysql = server->mysql; + struct mxq_daemon *daemon = &server->daemon; + (void)ppidcache; + + __attribute__((cleanup(mx_mysql_statement_close))) + struct mx_mysql_stmt *stmt = mx_mysql_statement_prepare(mysql, + "SELECT job_id FROM mxq_job" + " WHERE job_status = " status_str(MXQ_JOB_STATUS_RUNNING) + " AND job_cancelled" + " AND host_hostname = ?" + " AND server_id = ?" + ); + if (!stmt) { + mx_log_err("mx_mysql_stmt_prepare: %s\n", mx_mysql_error()); + return; + } + + mx_mysql_statement_param_bind(stmt, 0, string, &daemon->hostname); + mx_mysql_statement_param_bind(stmt, 1, string, &daemon->daemon_name); + + unsigned long long num_rows; + int res = mx_mysql_statement_execute(stmt, &num_rows); + if (res < 0) { + mx_log_err("mx_mysql_statement_execute: %s\n", mx_mysql_error()); + return; + } + if (num_rows == 0) + return; + + unsigned long job_id; + mx_mysql_statement_result_bind(stmt, 0, uint64, &job_id); + + for (unsigned long i = 0 ; i < num_rows ; i++) { + res = mx_mysql_statement_fetch(stmt); + if (res < 0) { + mx_log_err("mx_mysql_statement_fetch: %s\n", mx_mysql_error()); + return; + } + mx_log_debug("kill running job id %lu", job_id); + kill_by_jobid(ppidcache, server, job_id); + } +} + static void rename_outfiles(struct mxq_server *server, struct mxq_group *group, struct mxq_job *job) { int res; @@ -2541,6 +2595,7 @@ static void monitor_jobs(struct mxq_server *server) killall_cancelled(ppidcache, server); killall_over_time(ppidcache, server); killall_over_memory(ppidcache, server); + kill_cancelled_jobs(ppidcache, server); for (ulist = server->users; ulist; ulist = ulist->next) { for (glist = ulist->groups; glist; glist = glist->next) { for (jlist = glist->jobs; jlist; jlist = jlist->next) diff --git a/mxqkill.c b/mxqkill.c index af4b602b..26cb79bb 100644 --- a/mxqkill.c +++ b/mxqkill.c @@ -33,6 +33,16 @@ #define UINT64_SPECIAL_MIN (uint64_t)(-2) #define UINT64_HASVALUE(x) ((x) < UINT64_SPECIAL_MIN) +__attribute__ ((noreturn)) +__attribute__ ((format (printf, 1, 2))) +static void die(const char *restrict fmt, ...) { + va_list ap; + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + _exit(1); +} + static void print_usage(void) { mxq_print_generic_version(); @@ -90,18 +100,17 @@ static int update_group_status_cancelled(struct mx_mysql *mysql, struct mxq_grou " AND group_jobs-group_jobs_finished-group_jobs_failed-group_jobs_cancelled-group_jobs_unknown > 0" ); if (!stmt) { - mx_log_err("mx_mysql_statement_prepare(): %m"); + mx_log_err("mx_mysql_statement_prepare(): %s", mx_mysql_error()); return -(errno=EIO); } - res = mx_mysql_statement_param_bind(stmt, 0, uint64, &(g->group_id)); - res += mx_mysql_statement_param_bind(stmt, 1, uint32, &(g->user_uid)); - assert(res == 0); + mx_mysql_statement_param_bind(stmt, 0, uint64, &(g->group_id)); + mx_mysql_statement_param_bind(stmt, 1, uint32, &(g->user_uid)); 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); @@ -130,17 +139,16 @@ static int update_job_status_cancelled_by_group(struct mx_mysql *mysql, struct m " AND host_pid = 0" ); if (!stmt) { - mx_log_err("mx_mysql_statement_prepare(): %m"); + mx_log_err("mx_mysql_statement_prepare(): %s", mx_mysql_error()); return -(errno=EIO); } - res = mx_mysql_statement_param_bind(stmt, 0, uint64, &(g->group_id)); - assert(res == 0); + mx_mysql_statement_param_bind(stmt, 0, uint64, &(g->group_id)); 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); @@ -150,85 +158,62 @@ static int update_job_status_cancelled_by_group(struct mx_mysql *mysql, struct m return (int)num_rows; } -static int update_job_status_cancelling_by_job_id_for_user(struct mx_mysql *mysql, uint64_t job_id, uint64_t user_uid) -{ - struct mx_mysql_stmt *stmt = NULL; - unsigned long long num_rows = 0; - int res; +static void verify_job_owner(struct mx_mysql *mysql, uint64_t job_id, uint64_t user_uid) { + struct mx_mysql_stmt *stmt = mx_mysql_statement_prepare(mysql, + "SELECT user_uid FROM mxq_job, mxq_group" + " WHERE mxq_job.group_id = mxq_group.group_id" + " AND job_id = ?" + ); + if (!stmt) + die("mx_mysql_statement_prepare(): %s\n", mx_mysql_error()); - assert(job_id); + mx_mysql_statement_param_bind(stmt, 0, uint64, &job_id); - stmt = mx_mysql_statement_prepare(mysql, - "UPDATE mxq_job AS j" - " LEFT JOIN mxq_group AS g" - " ON j.group_id = g.group_id" - " SET" - " job_status = " status_str(MXQ_JOB_STATUS_CANCELLING) - " WHERE job_id = ?" - " AND user_uid = ?" - " AND job_status IN (" status_str(MXQ_JOB_STATUS_INQ) "," status_str(MXQ_JOB_STATUS_ASSIGNED) ")" - " AND host_hostname = ''" - " AND server_id = ''" - " AND host_pid = 0" - ); - if (!stmt) { - mx_log_err("mx_mysql_statement_prepare(): %m"); - return -(errno=EIO); - } + unsigned long long num_rows; + int res = mx_mysql_statement_execute(stmt, &num_rows); + if (res < 0) + die("mx_mysql_statement_execute(): %s\n", mx_mysql_error()); - res = mx_mysql_statement_param_bind(stmt, 0, uint64, &(job_id)); - res += mx_mysql_statement_param_bind(stmt, 1, uint64, &(user_uid)); - assert(res == 0); + if (num_rows == 0) + die("no such job_id %lu\n", job_id); - res = mx_mysql_statement_execute(stmt, &num_rows); + uint64_t uid; + mx_mysql_statement_result_bind(stmt, 0, uint64, &uid); + res = mx_mysql_statement_fetch(stmt); if (res < 0) - mx_log_err("mx_mysql_statement_execute(): %m"); + die("mx_mysql_statement_fetch: %s\n", mx_mysql_error()); - mx_mysql_statement_close(&stmt); - - if (res < 0) - return -(errno=-res); + if (uid != user_uid) + die("job %lu: permission denied\n", job_id); - return (int)num_rows; + mx_mysql_statement_close(&stmt); } -static int update_job_status_cancelled_by_job_id(struct mx_mysql *mysql, uint64_t job_id) -{ - struct mx_mysql_stmt *stmt = NULL; - unsigned long long num_rows = 0; - int res; - - assert(job_id); - - stmt = mx_mysql_statement_prepare(mysql, - "UPDATE mxq_job SET" - " job_status = " status_str(MXQ_JOB_STATUS_CANCELLED) - " WHERE job_id = ?" - " AND job_status = " status_str(MXQ_JOB_STATUS_CANCELLING) - " AND host_hostname = ''" - " AND server_id = ''" - " AND host_pid = 0" - ); - if (!stmt) { - mx_log_err("mx_mysql_statement_prepare(): %m"); - return -(errno=EIO); - } - - res = mx_mysql_statement_param_bind(stmt, 0, uint64, &(job_id)); - assert(res == 0); +static void set_job_cancelled(struct mx_mysql *mysql, uint64_t job_id) { + struct mx_mysql_stmt *stmt = mx_mysql_statement_prepare(mysql, + "UPDATE mxq_job SET job_cancelled = TRUE" + " WHERE job_id = ?" + ); + if (!stmt) + die("mx_mysql_statement_prepare(): %s\n", mx_mysql_error()); - res = mx_mysql_statement_execute(stmt, &num_rows); + mx_mysql_statement_param_bind(stmt, 0, uint64, &job_id); + unsigned long long num_rows; + int res = mx_mysql_statement_execute(stmt, &num_rows); if (res < 0) - mx_log_err("mx_mysql_statement_execute(): %m"); + die("mx_mysql_statement_execute(): %s\n", mx_mysql_error()); + if (num_rows == 0) + die("no such job_id %lu\n", job_id); mx_mysql_statement_close(&stmt); +} - if (res < 0) - return -(errno=-res); +static void cancel_job(struct mx_mysql *mysql, uint64_t job_id, uint64_t user_uid) { - return (int)num_rows; + verify_job_owner(mysql, job_id, user_uid); + set_job_cancelled(mysql, job_id); } int main(int argc, char *argv[]) @@ -412,48 +397,9 @@ int main(int argc, char *argv[]) mx_log_info("MySQL: Connection to database established."); if (arg_job_id) { - int res1, res2; - - res1 = update_job_status_cancelling_by_job_id_for_user(mysql, arg_job_id, passwd->pw_uid); - res2 = update_job_status_cancelled_by_job_id(mysql, arg_job_id); - + cancel_job(mysql, arg_job_id, passwd->pw_uid); mx_mysql_finish(&mysql); - mx_log_info("MySQL: Connection to database closed."); - - if (res1 == -ENOENT) - res1=0; - - if (res2 == -ENOENT) - res1=0; - - if (res1 < 0) - mx_log_err("setting status of job %lu to CANCELLING failed: %s", arg_job_id, strerror(-res1)); - - if (res2 < 0) - mx_log_err("setting status of job %lu to CANCELLED failed: %s", arg_job_id, strerror(-res2)); - - if (res2 > 0) { - mx_log_notice("Job %lu cancelled!", arg_job_id); - return 0; - } - - if (res1 > 0) { - mx_log_notice("Updated status of job %lu to CANCELLING.", arg_job_id); - if (res2 == 0) { - mx_log_warning("Updating status of job %lu to CANCELLED failed. Job vanished. Please retry.", arg_job_id); - return 2; - } - return 1; - } - - if (res1 == 0 && res2 == 0) { - mx_log_notice("No queued job with job_id=%lu for user %s(%d) found in q.", arg_job_id, passwd->pw_name, passwd->pw_uid); - mx_log_warning("Killing a single job is not implemented yet."); - mx_log_warning("See https://github.molgen.mpg.de/mariux64/mxq/issues/4 for more details."); - return 0; - } - - return 1; + return 0; } if (arg_group_id) { @@ -473,7 +419,7 @@ int main(int argc, char *argv[]) mx_log_warning("no active group with group_id=%lu found for user=%s(%d)", group.group_id, group.user_name, group.user_uid); else - mx_log_err("cancelling group failed: %m"); + mx_log_err("cancelling group failed"); return 1; } @@ -497,7 +443,7 @@ int main(int argc, char *argv[]) group.group_id); return 0; } else { - mx_log_err("cancelling jobs failed: %m"); + mx_log_err("cancelling jobs failed"); return 1; } } diff --git a/mxqset.c b/mxqset.c index 126471f6..bf205385 100644 --- a/mxqset.c +++ b/mxqset.c @@ -28,15 +28,15 @@ static void verify_group_permission(struct mx_mysql *mysql , long unsigned group stmt = mx_mysql_statement_prepare(mysql,"SELECT user_uid FROM mxq_group WHERE group_id = ? LIMIT 1"); if (!stmt) - die("%m\n"); + die("%s\n", mx_mysql_error()); mx_mysql_statement_param_bind(stmt, 0, uint64, &(groupid)); if (mx_mysql_statement_execute(stmt, &num_rows) < 0) - die("%m\n"); + die("%s\n", mx_mysql_error()); if (num_rows < 1) die("no such group %ld\n", groupid); mx_mysql_statement_result_bind(stmt, 0, uint32, &uid); if (mx_mysql_statement_fetch(stmt) < 0) - die("%m\n"); + die("%s\n", mx_mysql_error()); if ( client_uid != 0 && client_uid != uid ) die("no permission to access this group\n"); mx_mysql_statement_close(&stmt); @@ -59,12 +59,12 @@ static void update_closed_flag(struct mx_mysql *mysql, long unsigned groupid, en stmt = mx_mysql_statement_prepare(mysql, "UPDATE mxq_group SET group_flags = ( group_flags & ?) | ? WHERE group_id = ?"); if (!stmt) - die("%m\n"); + die("%s\n", mx_mysql_error()); mx_mysql_statement_param_bind(stmt, 0, uint64, &(mask)); mx_mysql_statement_param_bind(stmt, 1, uint64, &(value)); mx_mysql_statement_param_bind(stmt, 2, uint64, &(groupid)); if (mx_mysql_statement_execute(stmt, &num_rows) < 0) - die("%m\n"); + die("%s\n", mx_mysql_error()); if (num_rows < 1) die("no such group %ld\n", groupid); mx_mysql_statement_close(&stmt); @@ -81,15 +81,15 @@ static void update_blacklist(struct mx_mysql *mysql, long unsigned groupid, char stmt = mx_mysql_statement_prepare(mysql, "SELECT group_blacklist FROM mxq_group WHERE group_id = ? LIMIT 1"); if (!stmt) - die("%m\n"); + die("%s\n", mx_mysql_error()); mx_mysql_statement_param_bind(stmt, 0, uint64, &groupid); if (mx_mysql_statement_execute(stmt, &num_rows) < 0) - die("%m\n"); + die("%s\n", mx_mysql_error()); if (num_rows < 1) die("no such group %ld\n", groupid); mx_mysql_statement_result_bind(stmt, 0, string, &group_blacklist); if (mx_mysql_statement_fetch(stmt) < 0) - die("%m\n"); + die("%s\n", mx_mysql_error()); mx_mysql_statement_close(&stmt); blacklist = keywordset_new(group_blacklist); free(group_blacklist); @@ -103,11 +103,11 @@ static void update_blacklist(struct mx_mysql *mysql, long unsigned groupid, char stmt = mx_mysql_statement_prepare(mysql, "UPDATE mxq_group SET group_blacklist = ? WHERE group_id = ?"); if (!stmt) - die("%m\n"); + die("%s\n", mx_mysql_error()); mx_mysql_statement_param_bind(stmt, 0, string, &group_blacklist); mx_mysql_statement_param_bind(stmt, 1, uint64, &groupid); if (mx_mysql_statement_execute(stmt, &num_rows) < 0) - die("%m\n"); + die("%s\n", mx_mysql_error()); if (num_rows < 1) die("no such group %ld\n", groupid); mx_mysql_statement_close(&stmt); @@ -126,15 +126,15 @@ static void update_whitelist(struct mx_mysql *mysql, long unsigned groupid, char stmt = mx_mysql_statement_prepare(mysql, "SELECT group_whitelist FROM mxq_group WHERE group_id = ? LIMIT 1"); if (!stmt) - die("%m\n"); + die("%s\n", mx_mysql_error()); mx_mysql_statement_param_bind(stmt, 0, uint64, &groupid); if (mx_mysql_statement_execute(stmt, &num_rows) < 0) - die("%m\n"); + die("%s\n", mx_mysql_error()); if (num_rows < 1) die("no such group %ld\n", groupid); mx_mysql_statement_result_bind(stmt, 0, string, &group_whitelist); if (mx_mysql_statement_fetch(stmt) < 0) - die("%m\n"); + die("%s\n", mx_mysql_error()); mx_mysql_statement_close(&stmt); whitelist = keywordset_new(group_whitelist); free(group_whitelist); @@ -148,11 +148,11 @@ static void update_whitelist(struct mx_mysql *mysql, long unsigned groupid, char stmt = mx_mysql_statement_prepare(mysql, "UPDATE mxq_group SET group_whitelist = ? WHERE group_id = ?"); if (!stmt) - die("%m\n"); + die("%s\n", mx_mysql_error()); mx_mysql_statement_param_bind(stmt, 0, string, &group_whitelist); mx_mysql_statement_param_bind(stmt, 1, uint64, &groupid); if (mx_mysql_statement_execute(stmt, &num_rows) < 0) - die("%m\n"); + die("%s\n", mx_mysql_error()); if (num_rows < 1) die("no such group %ld\n", groupid); mx_mysql_statement_close(&stmt); diff --git a/mxqsub.c b/mxqsub.c index a23383be..26ae3b75 100644 --- a/mxqsub.c +++ b/mxqsub.c @@ -179,33 +179,32 @@ static int load_group_id(struct mx_mysql *mysql, struct mxq_group *g) " ORDER BY group_id DESC" " LIMIT 1"); if (!stmt) { - mx_log_err("mx_mysql_statement_prepare(): %m"); + mx_log_err("mx_mysql_statement_prepare(): %s", mx_mysql_error()); return -errno; } - res = mx_mysql_statement_param_bind(stmt, 0, string, &(g->group_name)); - res += mx_mysql_statement_param_bind(stmt, 1, uint32, &(g->user_uid)); - res += mx_mysql_statement_param_bind(stmt, 2, string, &(g->user_name)); - res += mx_mysql_statement_param_bind(stmt, 3, uint32, &(g->user_gid)); - res += mx_mysql_statement_param_bind(stmt, 4, string, &(g->user_group)); - res += mx_mysql_statement_param_bind(stmt, 5, string, &(g->job_command)); - res += mx_mysql_statement_param_bind(stmt, 6, uint16, &(g->job_threads)); - res += mx_mysql_statement_param_bind(stmt, 7, uint64, &(g->job_memory)); - res += mx_mysql_statement_param_bind(stmt, 8, uint32, &(g->job_time)); - res += mx_mysql_statement_param_bind(stmt, 9, uint32, &(g->job_tmpdir_size)); - res += mx_mysql_statement_param_bind(stmt, 10, uint16, &(g->job_gpu)); - res += mx_mysql_statement_param_bind(stmt, 11, uint16, &(g->job_max_per_node)); - res += mx_mysql_statement_param_bind(stmt, 12, uint16, &(g->group_priority)); - res += mx_mysql_statement_param_bind(stmt, 13, string, &(g->group_blacklist)); - res += mx_mysql_statement_param_bind(stmt, 14, string, &(g->group_whitelist)); - res += mx_mysql_statement_param_bind(stmt, 15, string, &(g->prerequisites)); - res += mx_mysql_statement_param_bind(stmt, 16, string, &(g->tags)); - res += mx_mysql_statement_param_bind(stmt, 17, uint64, &(flags)); - assert(res == 0); + mx_mysql_statement_param_bind(stmt, 0, string, &(g->group_name)); + mx_mysql_statement_param_bind(stmt, 1, uint32, &(g->user_uid)); + mx_mysql_statement_param_bind(stmt, 2, string, &(g->user_name)); + mx_mysql_statement_param_bind(stmt, 3, uint32, &(g->user_gid)); + mx_mysql_statement_param_bind(stmt, 4, string, &(g->user_group)); + mx_mysql_statement_param_bind(stmt, 5, string, &(g->job_command)); + mx_mysql_statement_param_bind(stmt, 6, uint16, &(g->job_threads)); + mx_mysql_statement_param_bind(stmt, 7, uint64, &(g->job_memory)); + mx_mysql_statement_param_bind(stmt, 8, uint32, &(g->job_time)); + mx_mysql_statement_param_bind(stmt, 9, uint32, &(g->job_tmpdir_size)); + mx_mysql_statement_param_bind(stmt, 10, uint16, &(g->job_gpu)); + mx_mysql_statement_param_bind(stmt, 11, uint16, &(g->job_max_per_node)); + mx_mysql_statement_param_bind(stmt, 12, uint16, &(g->group_priority)); + mx_mysql_statement_param_bind(stmt, 13, string, &(g->group_blacklist)); + mx_mysql_statement_param_bind(stmt, 14, string, &(g->group_whitelist)); + mx_mysql_statement_param_bind(stmt, 15, string, &(g->prerequisites)); + mx_mysql_statement_param_bind(stmt, 16, string, &(g->tags)); + mx_mysql_statement_param_bind(stmt, 17, uint64, &(flags)); 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; } @@ -217,7 +216,7 @@ static int load_group_id(struct mx_mysql *mysql, struct mxq_group *g) res = mx_mysql_statement_fetch(stmt); if (res < 0) { - mx_log_err("mx_mysql_statement_fetch(): %m"); + mx_log_err("mx_mysql_statement_fetch(): %s", mx_mysql_error()); mx_mysql_statement_close(&stmt); return res; } @@ -275,34 +274,33 @@ static int load_group_id_by_group_id(struct mx_mysql *mysql, struct mxq_group *g " ORDER BY group_id DESC" " LIMIT 1"); if (!stmt) { - mx_log_err("mx_mysql_statement_prepare(): %m"); + mx_log_err("mx_mysql_statement_prepare(): %s", mx_mysql_error()); return -errno; } - res = mx_mysql_statement_param_bind(stmt, 0, string, &(g->group_name)); - res += mx_mysql_statement_param_bind(stmt, 1, uint32, &(g->user_uid)); - res += mx_mysql_statement_param_bind(stmt, 2, string, &(g->user_name)); - res += mx_mysql_statement_param_bind(stmt, 3, uint32, &(g->user_gid)); - res += mx_mysql_statement_param_bind(stmt, 4, string, &(g->user_group)); - res += mx_mysql_statement_param_bind(stmt, 5, string, &(g->job_command)); - res += mx_mysql_statement_param_bind(stmt, 6, uint16, &(g->job_threads)); - res += mx_mysql_statement_param_bind(stmt, 7, uint64, &(g->job_memory)); - res += mx_mysql_statement_param_bind(stmt, 8, uint32, &(g->job_time)); - res += mx_mysql_statement_param_bind(stmt, 9, uint32, &(g->job_tmpdir_size)); - res += mx_mysql_statement_param_bind(stmt, 10, uint16, &(g->job_gpu)); - res += mx_mysql_statement_param_bind(stmt, 11, uint16, &(g->job_max_per_node)); - res += mx_mysql_statement_param_bind(stmt, 12, uint16, &(g->group_priority)); - res += mx_mysql_statement_param_bind(stmt, 13, string, &(g->group_blacklist)); - res += mx_mysql_statement_param_bind(stmt, 14, string, &(g->group_whitelist)); - res += mx_mysql_statement_param_bind(stmt, 15, string, &(g->prerequisites)); - res += mx_mysql_statement_param_bind(stmt, 16, string, &(g->tags)); - res += mx_mysql_statement_param_bind(stmt, 17, uint64, &(g->group_id)); - res += mx_mysql_statement_param_bind(stmt, 18, uint64, &(flags)); - assert(res == 0); + mx_mysql_statement_param_bind(stmt, 0, string, &(g->group_name)); + mx_mysql_statement_param_bind(stmt, 1, uint32, &(g->user_uid)); + mx_mysql_statement_param_bind(stmt, 2, string, &(g->user_name)); + mx_mysql_statement_param_bind(stmt, 3, uint32, &(g->user_gid)); + mx_mysql_statement_param_bind(stmt, 4, string, &(g->user_group)); + mx_mysql_statement_param_bind(stmt, 5, string, &(g->job_command)); + mx_mysql_statement_param_bind(stmt, 6, uint16, &(g->job_threads)); + mx_mysql_statement_param_bind(stmt, 7, uint64, &(g->job_memory)); + mx_mysql_statement_param_bind(stmt, 8, uint32, &(g->job_time)); + mx_mysql_statement_param_bind(stmt, 9, uint32, &(g->job_tmpdir_size)); + mx_mysql_statement_param_bind(stmt, 10, uint16, &(g->job_gpu)); + mx_mysql_statement_param_bind(stmt, 11, uint16, &(g->job_max_per_node)); + mx_mysql_statement_param_bind(stmt, 12, uint16, &(g->group_priority)); + mx_mysql_statement_param_bind(stmt, 13, string, &(g->group_blacklist)); + mx_mysql_statement_param_bind(stmt, 14, string, &(g->group_whitelist)); + mx_mysql_statement_param_bind(stmt, 15, string, &(g->prerequisites)); + mx_mysql_statement_param_bind(stmt, 16, string, &(g->tags)); + mx_mysql_statement_param_bind(stmt, 17, uint64, &(g->group_id)); + mx_mysql_statement_param_bind(stmt, 18, uint64, &(flags)); 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; } @@ -368,33 +366,32 @@ static int load_group_id_run_or_wait(struct mx_mysql *mysql, struct mxq_group *g " ORDER BY group_id DESC" " LIMIT 1"); if (!stmt) { - mx_log_err("mx_mysql_statement_prepare(): %m"); + mx_log_err("mx_mysql_statement_prepare(): %s", mx_mysql_error()); return -errno; } - res = mx_mysql_statement_param_bind(stmt, 0, string, &(g->group_name)); - res += mx_mysql_statement_param_bind(stmt, 1, uint32, &(g->user_uid)); - res += mx_mysql_statement_param_bind(stmt, 2, string, &(g->user_name)); - res += mx_mysql_statement_param_bind(stmt, 3, uint32, &(g->user_gid)); - res += mx_mysql_statement_param_bind(stmt, 4, string, &(g->user_group)); - res += mx_mysql_statement_param_bind(stmt, 5, string, &(g->job_command)); - res += mx_mysql_statement_param_bind(stmt, 6, uint16, &(g->job_threads)); - res += mx_mysql_statement_param_bind(stmt, 7, uint64, &(g->job_memory)); - res += mx_mysql_statement_param_bind(stmt, 8, uint32, &(g->job_time)); - res += mx_mysql_statement_param_bind(stmt, 9, uint32, &(g->job_tmpdir_size)); - res += mx_mysql_statement_param_bind(stmt, 10, uint16, &(g->job_gpu)); - res += mx_mysql_statement_param_bind(stmt, 11, uint16, &(g->job_max_per_node)); - res += mx_mysql_statement_param_bind(stmt, 12, uint16, &(g->group_priority)); - res += mx_mysql_statement_param_bind(stmt, 13, string, &(g->group_blacklist)); - res += mx_mysql_statement_param_bind(stmt, 14, string, &(g->group_whitelist)); - res += mx_mysql_statement_param_bind(stmt, 15, string, &(g->prerequisites)); - res += mx_mysql_statement_param_bind(stmt, 16, string, &(g->tags)); - res += mx_mysql_statement_param_bind(stmt, 17, uint64, &(flags)); - assert(res == 0); + mx_mysql_statement_param_bind(stmt, 0, string, &(g->group_name)); + mx_mysql_statement_param_bind(stmt, 1, uint32, &(g->user_uid)); + mx_mysql_statement_param_bind(stmt, 2, string, &(g->user_name)); + mx_mysql_statement_param_bind(stmt, 3, uint32, &(g->user_gid)); + mx_mysql_statement_param_bind(stmt, 4, string, &(g->user_group)); + mx_mysql_statement_param_bind(stmt, 5, string, &(g->job_command)); + mx_mysql_statement_param_bind(stmt, 6, uint16, &(g->job_threads)); + mx_mysql_statement_param_bind(stmt, 7, uint64, &(g->job_memory)); + mx_mysql_statement_param_bind(stmt, 8, uint32, &(g->job_time)); + mx_mysql_statement_param_bind(stmt, 9, uint32, &(g->job_tmpdir_size)); + mx_mysql_statement_param_bind(stmt, 10, uint16, &(g->job_gpu)); + mx_mysql_statement_param_bind(stmt, 11, uint16, &(g->job_max_per_node)); + mx_mysql_statement_param_bind(stmt, 12, uint16, &(g->group_priority)); + mx_mysql_statement_param_bind(stmt, 13, string, &(g->group_blacklist)); + mx_mysql_statement_param_bind(stmt, 14, string, &(g->group_whitelist)); + mx_mysql_statement_param_bind(stmt, 15, string, &(g->prerequisites)); + mx_mysql_statement_param_bind(stmt, 16, string, &(g->tags)); + mx_mysql_statement_param_bind(stmt, 17, uint64, &(flags)); 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; } @@ -405,7 +402,7 @@ static int load_group_id_run_or_wait(struct mx_mysql *mysql, struct mxq_group *g res = mx_mysql_statement_fetch(stmt); if (res < 0) { - mx_log_err("mx_mysql_statement_fetch(): %m"); + mx_log_err("mx_mysql_statement_fetch(): %s", mx_mysql_error()); mx_mysql_statement_close(&stmt); return res; } @@ -453,32 +450,31 @@ static int add_group(struct mx_mysql *mysql, struct mxq_group *g) " prerequisites = ?," " tags = ?"); if (!stmt) { - mx_log_err("mx_mysql_statement_prepare(): %m"); + mx_log_err("mx_mysql_statement_prepare(): %s", mx_mysql_error()); return -errno; } - res = mx_mysql_statement_param_bind(stmt, 0, string, &(g->group_name)); - res += mx_mysql_statement_param_bind(stmt, 1, uint32, &(g->user_uid)); - res += mx_mysql_statement_param_bind(stmt, 2, string, &(g->user_name)); - res += mx_mysql_statement_param_bind(stmt, 3, uint32, &(g->user_gid)); - res += mx_mysql_statement_param_bind(stmt, 4, string, &(g->user_group)); - res += mx_mysql_statement_param_bind(stmt, 5, string, &(g->job_command)); - res += mx_mysql_statement_param_bind(stmt, 6, uint16, &(g->job_threads)); - res += mx_mysql_statement_param_bind(stmt, 7, uint64, &(g->job_memory)); - res += mx_mysql_statement_param_bind(stmt, 8, uint32, &(g->job_time)); - res += mx_mysql_statement_param_bind(stmt, 9, uint32, &(g->job_tmpdir_size)); - res += mx_mysql_statement_param_bind(stmt,10, uint16, &(g->job_gpu)); - res += mx_mysql_statement_param_bind(stmt,11, uint16, &(g->job_max_per_node)); - res += mx_mysql_statement_param_bind(stmt,12, uint16, &(g->group_priority)); - res += mx_mysql_statement_param_bind(stmt,13, string, &(g->group_blacklist)); - res += mx_mysql_statement_param_bind(stmt,14, string, &(g->group_whitelist)); - res += mx_mysql_statement_param_bind(stmt,15, string, &(g->prerequisites)); - res += mx_mysql_statement_param_bind(stmt,16, string, &(g->tags)); - assert(res == 0); + mx_mysql_statement_param_bind(stmt, 0, string, &(g->group_name)); + mx_mysql_statement_param_bind(stmt, 1, uint32, &(g->user_uid)); + mx_mysql_statement_param_bind(stmt, 2, string, &(g->user_name)); + mx_mysql_statement_param_bind(stmt, 3, uint32, &(g->user_gid)); + mx_mysql_statement_param_bind(stmt, 4, string, &(g->user_group)); + mx_mysql_statement_param_bind(stmt, 5, string, &(g->job_command)); + mx_mysql_statement_param_bind(stmt, 6, uint16, &(g->job_threads)); + mx_mysql_statement_param_bind(stmt, 7, uint64, &(g->job_memory)); + mx_mysql_statement_param_bind(stmt, 8, uint32, &(g->job_time)); + mx_mysql_statement_param_bind(stmt, 9, uint32, &(g->job_tmpdir_size)); + mx_mysql_statement_param_bind(stmt,10, uint16, &(g->job_gpu)); + mx_mysql_statement_param_bind(stmt,11, uint16, &(g->job_max_per_node)); + mx_mysql_statement_param_bind(stmt,12, uint16, &(g->group_priority)); + mx_mysql_statement_param_bind(stmt,13, string, &(g->group_blacklist)); + mx_mysql_statement_param_bind(stmt,14, string, &(g->group_whitelist)); + mx_mysql_statement_param_bind(stmt,15, string, &(g->prerequisites)); + mx_mysql_statement_param_bind(stmt,16, string, &(g->tags)); 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; } @@ -525,31 +521,27 @@ static int add_job(struct mx_mysql *mysql, struct mxq_job *j) " job_umask = ?," - " host_submit = ?," - - " job_flags = ?" + " host_submit = ?" ); 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; } - res = mx_mysql_statement_param_bind(stmt, 0, uint16, &(j->job_priority)); - res += mx_mysql_statement_param_bind(stmt, 1, uint64, &(j->group_id)); - res += mx_mysql_statement_param_bind(stmt, 2, string, &(j->job_workdir)); - res += mx_mysql_statement_param_bind(stmt, 3, uint32, &(j->job_argc)); - res += mx_mysql_statement_param_bind(stmt, 4, string, &(j->job_argv_str)); - res += mx_mysql_statement_param_bind(stmt, 5, string, &(j->job_stdout)); - res += mx_mysql_statement_param_bind(stmt, 6, string, &(j->job_stderr)); - res += mx_mysql_statement_param_bind(stmt, 7, uint32, &(j->job_umask)); - res += mx_mysql_statement_param_bind(stmt, 8, string, &(j->host_submit)); - res += mx_mysql_statement_param_bind(stmt, 9, uint64, &(j->job_flags)); - assert(res ==0); + mx_mysql_statement_param_bind(stmt, 0, uint16, &(j->job_priority)); + mx_mysql_statement_param_bind(stmt, 1, uint64, &(j->group_id)); + mx_mysql_statement_param_bind(stmt, 2, string, &(j->job_workdir)); + mx_mysql_statement_param_bind(stmt, 3, uint32, &(j->job_argc)); + mx_mysql_statement_param_bind(stmt, 4, string, &(j->job_argv_str)); + mx_mysql_statement_param_bind(stmt, 5, string, &(j->job_stdout)); + mx_mysql_statement_param_bind(stmt, 6, string, &(j->job_stderr)); + mx_mysql_statement_param_bind(stmt, 7, uint32, &(j->job_umask)); + mx_mysql_statement_param_bind(stmt, 8, string, &(j->host_submit)); 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; } @@ -579,13 +571,13 @@ static int get_active_groups_for_user(struct mx_mysql *mysql, char *username) " WHERE user_name = ?" " AND (group_jobs_inq > 0 OR group_jobs_running > 0)"); if (!stmt) { - mx_log_err("mx_mysql_statement_prepare(): %m"); + mx_log_err("mx_mysql_statement_prepare(): %s", mx_mysql_error()); return -errno; } mx_mysql_statement_param_bind(stmt, 0, string, &username); 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; } @@ -593,7 +585,7 @@ static int get_active_groups_for_user(struct mx_mysql *mysql, char *username) mx_mysql_statement_result_bind(stmt, 0, uint64, &count); res = mx_mysql_statement_fetch(stmt); if (res < 0) { - mx_log_err("mx_mysql_statement_fetch(): %m"); + mx_log_err("mx_mysql_statement_fetch(): %s", mx_mysql_error()); mx_mysql_statement_close(&stmt); return res; } @@ -712,7 +704,6 @@ int main(int argc, char *argv[]) char *arg_mysql_default_file; char *arg_mysql_default_group; char arg_debug; - char arg_jobflags; u_int32_t arg_tmpdir; u_int16_t arg_gpu; @@ -801,7 +792,6 @@ int main(int argc, char *argv[]) arg_stderr = "stdout"; arg_umask = getumask(); arg_debug = 0; - arg_jobflags = 0; arg_groupid = UINT64_UNSET; arg_tmpdir = 10; // 10G arg_blacklist = NULL; @@ -1137,7 +1127,6 @@ int main(int argc, char *argv[]) group.job_max_per_node = arg_max_per_node; - job.job_flags = arg_jobflags; job.job_priority = arg_priority; job.job_workdir = arg_workdir; job.job_stdout = arg_stdout; @@ -1193,7 +1182,7 @@ int main(int argc, char *argv[]) if (res < 0) { if (res != -ENOENT && res != -EDQUOT) - mx_log_err("Job submission failed: %m"); + mx_log_err("Job submission failed."); return 1; } diff --git a/mysql/create_tables.sql b/mysql/create_tables.sql index 549b55a7..3b2bcd06 100644 --- a/mysql/create_tables.sql +++ b/mysql/create_tables.sql @@ -2,7 +2,6 @@ CREATE TABLE IF NOT EXISTS mxq_group ( group_id INT8 UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, group_name VARCHAR(128) NOT NULL DEFAULT 'default', group_status INT1 UNSIGNED NOT NULL DEFAULT 0, - group_flags INT8 UNSIGNED NOT NULL DEFAULT 0, group_priority INT2 UNSIGNED NOT NULL DEFAULT 127, group_blacklist VARCHAR(1000) NOT NULL DEFAULT '', group_whitelist VARCHAR(1000) NOT NULL DEFAULT '', @@ -32,10 +31,7 @@ CREATE TABLE IF NOT EXISTS mxq_group ( group_jobs_cancelled INT8 UNSIGNED NOT NULL DEFAULT 0, group_jobs_unknown INT8 UNSIGNED NOT NULL DEFAULT 0, - group_jobs_restarted INT8 UNSIGNED NOT NULL DEFAULT 0, - group_slots_running INT8 UNSIGNED NOT NULL DEFAULT 0, - group_sum_starttime INT8 UNSIGNED NOT NULL DEFAULT 0, group_mtime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, @@ -72,9 +68,10 @@ CREATE TABLE IF NOT EXISTS mxq_group ( CREATE TABLE IF NOT EXISTS mxq_job ( job_id INT8 UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, job_status INT2 UNSIGNED NOT NULL DEFAULT 0, - job_flags INT8 UNSIGNED NOT NULL DEFAULT 0, job_priority INT2 UNSIGNED NOT NULL DEFAULT 127, + job_cancelled BOOLEAN NOT NULL DEFAULT FALSE, + group_id INT8 UNSIGNED NOT NULL, job_workdir VARCHAR(4096) NOT NULL, diff --git a/mysql/create_trigger.sql b/mysql/create_trigger.sql index e4a35dbb..afc016ae 100644 --- a/mysql/create_trigger.sql +++ b/mysql/create_trigger.sql @@ -53,7 +53,6 @@ CREATE TRIGGER mxq_update_job BEFORE UPDATE ON mxq_job SET NEW.date_start = NOW(); UPDATE mxq_group SET - group_sum_starttime = group_sum_starttime + UNIX_TIMESTAMP(NEW.date_start) * OLD.host_slots, group_jobs_inq = group_jobs_inq - 1, group_jobs_running = group_jobs_running + 1, group_slots_running = group_slots_running + NEW.host_slots @@ -68,17 +67,13 @@ CREATE TRIGGER mxq_update_job BEFORE UPDATE ON mxq_job UPDATE mxq_group SET group_slots_running = group_slots_running - OLD.host_slots - + NEW.host_slots, - group_sum_starttime = group_sum_starttime - - UNIX_TIMESTAMP(OLD.date_start) * OLD.host_slots - + UNIX_TIMESTAMP(OLD.date_start) * NEW.host_slots + + NEW.host_slots WHERE group_id = NEW.group_id; -- LOADED(150) | RUNNING(200) -> KILLED(400) | FAILED(750) ELSEIF NEW.job_status IN (400, 750) AND OLD.job_status IN (150, 200) THEN UPDATE mxq_group SET - group_sum_starttime = group_sum_starttime - LEAST(group_sum_starttime, UNIX_TIMESTAMP(OLD.date_start) * OLD.host_slots), group_slots_running = group_slots_running - OLD.host_slots, group_jobs_running = group_jobs_running - 1, group_jobs_failed = group_jobs_failed + 1, @@ -92,22 +87,10 @@ CREATE TRIGGER mxq_update_job BEFORE UPDATE ON mxq_job stats_total_real_sec = stats_total_real_sec + NEW.stats_real_sec WHERE group_id = NEW.group_id; - -- INQ(0) | ASSIGNED(100) | CANCELLING(989) -> CANCELLED(990) - ELSEIF NEW.job_status = 990 AND OLD.job_status IN (0, 100, 989) THEN - - SET NEW.date_start = NOW(); - SET NEW.date_end = NEW.date_start; - - UPDATE mxq_group SET - group_jobs_inq = group_jobs_inq - 1, - group_jobs_cancelled = group_jobs_cancelled + 1 - WHERE group_id = NEW.group_id; - -- LOADED(150) | RUNNING(200) -> UNKNOWN(999) ELSEIF NEW.job_status = 999 AND OLD.job_status IN (150, 200) THEN UPDATE mxq_group SET - group_sum_starttime = group_sum_starttime - LEAST(group_sum_starttime, UNIX_TIMESTAMP(OLD.date_start) * OLD.host_slots), group_slots_running = group_slots_running - OLD.host_slots, group_jobs_running = group_jobs_running - 1, group_jobs_unknown = group_jobs_unknown + 1 @@ -125,7 +108,6 @@ CREATE TRIGGER mxq_update_job BEFORE UPDATE ON mxq_job ELSEIF NEW.job_status = 1000 AND OLD.job_status IN (150, 200) THEN UPDATE mxq_group SET - group_sum_starttime = group_sum_starttime - LEAST(group_sum_starttime, UNIX_TIMESTAMP(OLD.date_start) * OLD.host_slots), group_slots_running = group_slots_running - OLD.host_slots, group_jobs_running = group_jobs_running - 1, group_jobs_finished = group_jobs_finished + 1, @@ -146,29 +128,25 @@ CREATE TRIGGER mxq_update_job BEFORE UPDATE ON mxq_job ELSEIF NEW.job_status = 0 AND OLD.job_status IN (150) THEN UPDATE mxq_group SET - group_sum_starttime = group_sum_starttime - UNIX_TIMESTAMP(OLD.date_start) * NEW.host_slots, group_jobs_inq = group_jobs_inq + 1, group_jobs_running = group_jobs_running - 1, group_slots_running = group_slots_running - OLD.host_slots WHERE group_id = NEW.group_id; - - -- * -> NOT IN [ CANCELLING(989) | CANCELLED(990) ] - ELSEIF NEW.job_status NOT IN (989, 990) THEN - - UPDATE mxq_group SET - stats_max_sumrss = GREATEST(stats_max_sumrss, NEW.stats_max_sumrss), - stats_max_maxrss = GREATEST(stats_max_maxrss, NEW.stats_maxrss), - stats_max_utime_sec = GREATEST(stats_max_utime_sec, NEW.stats_utime_sec), - stats_max_stime_sec = GREATEST(stats_max_stime_sec, NEW.stats_stime_sec), - stats_max_real_sec = GREATEST(stats_max_real_sec, NEW.stats_real_sec), - stats_total_utime_sec = stats_total_utime_sec+NEW.stats_utime_sec, - stats_total_stime_sec = stats_total_stime_sec+NEW.stats_stime_sec, - stats_total_real_sec = stats_total_real_sec+NEW.stats_real_sec - WHERE group_id = NEW.group_id; - END IF; END IF; END; | + +DROP TRIGGER IF EXISTS mxq_update_job2| +CREATE TRIGGER mxq_update_job2 BEFORE UPDATE ON mxq_job + FOR EACH ROW FOLLOWS mxq_update_job + IF NEW.job_status = 0 AND NEW.job_cancelled THEN + SET NEW.job_status = 990; -- CANCELLED + UPDATE mxq_group SET + group_jobs_inq = group_jobs_inq - 1, + group_jobs_cancelled = group_jobs_cancelled + 1 + WHERE group_id = NEW.group_id; + END IF; +| DELIMITER ; UNLOCK TABLES; diff --git a/mysql/migrate_015_add_job_cancelled.sql b/mysql/migrate_015_add_job_cancelled.sql new file mode 100644 index 00000000..adc3c0d6 --- /dev/null +++ b/mysql/migrate_015_add_job_cancelled.sql @@ -0,0 +1,5 @@ +ALTER TABLE mxq_job + ADD COLUMN + job_cancelled BOOLEAN NOT NULL DEFAULT FALSE + AFTER + job_priority; diff --git a/mysql/migrate_016_remove_job_flags.sql b/mysql/migrate_016_remove_job_flags.sql new file mode 100644 index 00000000..50a06716 --- /dev/null +++ b/mysql/migrate_016_remove_job_flags.sql @@ -0,0 +1,2 @@ +ALTER TABLE mxq_job + DROP COLUMN job_flags; diff --git a/mysql/migrate_017_remove_group_jobs_restarted.sql b/mysql/migrate_017_remove_group_jobs_restarted.sql new file mode 100644 index 00000000..b8a91756 --- /dev/null +++ b/mysql/migrate_017_remove_group_jobs_restarted.sql @@ -0,0 +1,2 @@ +ALTER TABLE mxq_group + DROP COLUMN group_jobs_restarted; diff --git a/test_mx_mysql.c b/test_mx_mysql.c index 3d1d0c9f..03a4714c 100644 --- a/test_mx_mysql.c +++ b/test_mx_mysql.c @@ -53,7 +53,7 @@ int main(int argc, char *argv[]) res = mx_mysql_statement_fetch(stmt); assert(res == 0); - mx_log_debug("mx_mysql_statement_fetch(): %m"); + mx_log_debug("mx_mysql_statement_fetch(): %s", mx_mysql_error()); mx_log_debug("&group_name = 0x%x", &group_name); diff --git a/web/pages/mxq/mxq.in b/web/pages/mxq/mxq.in index eac6ac1f..1aecc0ea 100755 --- a/web/pages/mxq/mxq.in +++ b/web/pages/mxq/mxq.in @@ -87,7 +87,6 @@ sub db_init { KILLED => 400, FAILED => 750, UNKNOWN_PRE => 755, - CANCELLING => 989, CANCELLED => 990, UNKNOWN => 999, FINISHED => 1000, @@ -408,7 +407,6 @@ group_jobs_finished : $o->{group_jobs_finished} group_jobs_failed : $o->{group_jobs_failed} group_jobs_cancelled : $o->{group_jobs_cancelled} group_jobs_unknown : $o->{group_jobs_unknown} -group_jobs_restarted : $o->{group_jobs_restarted} group_slots_running: : $o->{group_slots_running} @@ -572,6 +570,7 @@ sub job { my %o=%{$sth->fetchrow_hashref('NAME_lc')}; my $job_status_text=job_status($o{'job_status'}); + my $job_cancelled = $o{job_cancelled} ? "true" : "false"; my $job_umask_text=sprintf('%03O',$o{job_umask}); my $link_group_id=a({href=>selfurl("/group/$o{group_id}")},$o{group_id}); my $job_argv=escapeHTML(split_cmd($o{job_argv})); @@ -587,7 +586,7 @@ sub job { $out.=<<"EOF";
 job_status       : $job_status_text
-job_flags        : $o{job_flags}
+job_cancelled    : $job_cancelled
 job_priority     : $o{job_priority}
 
 group_id         : $link_group_id